blob: 8f48b4d58fca76d45de7c42fe2c83949606c82cb [file] [log] [blame]
Herbert Xub5b7f082007-04-16 20:48:54 +10001/*
2 * Asynchronous block chaining cipher operations.
3 *
4 * This is the asynchronous version of blkcipher.c indicating completion
5 * via a callback.
6 *
7 * Copyright (c) 2006 Herbert Xu <herbert@gondor.apana.org.au>
8 *
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the Free
11 * Software Foundation; either version 2 of the License, or (at your option)
12 * any later version.
13 *
14 */
15
Herbert Xu378f4f52007-12-17 20:07:31 +080016#include <crypto/internal/skcipher.h>
17#include <linux/err.h>
Herbert Xub5b7f082007-04-16 20:48:54 +100018#include <linux/init.h>
Herbert Xu791b4d52007-08-23 16:23:01 +080019#include <linux/kernel.h>
Herbert Xub5b7f082007-04-16 20:48:54 +100020#include <linux/module.h>
Herbert Xu791b4d52007-08-23 16:23:01 +080021#include <linux/slab.h>
Herbert Xub5b7f082007-04-16 20:48:54 +100022#include <linux/seq_file.h>
23
Herbert Xu378f4f52007-12-17 20:07:31 +080024#include "internal.h"
25
Herbert Xu791b4d52007-08-23 16:23:01 +080026static int setkey_unaligned(struct crypto_ablkcipher *tfm, const u8 *key,
27 unsigned int keylen)
Sebastian Siewiorca7c3932007-05-19 19:51:21 +100028{
29 struct ablkcipher_alg *cipher = crypto_ablkcipher_alg(tfm);
30 unsigned long alignmask = crypto_ablkcipher_alignmask(tfm);
31 int ret;
32 u8 *buffer, *alignbuffer;
33 unsigned long absize;
34
35 absize = keylen + alignmask;
36 buffer = kmalloc(absize, GFP_ATOMIC);
37 if (!buffer)
38 return -ENOMEM;
39
40 alignbuffer = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1);
41 memcpy(alignbuffer, key, keylen);
42 ret = cipher->setkey(tfm, alignbuffer, keylen);
Sebastian Siewior06817172007-08-03 20:33:47 +080043 memset(alignbuffer, 0, keylen);
Sebastian Siewiorca7c3932007-05-19 19:51:21 +100044 kfree(buffer);
45 return ret;
46}
47
Herbert Xub5b7f082007-04-16 20:48:54 +100048static int setkey(struct crypto_ablkcipher *tfm, const u8 *key,
49 unsigned int keylen)
50{
51 struct ablkcipher_alg *cipher = crypto_ablkcipher_alg(tfm);
Sebastian Siewiorca7c3932007-05-19 19:51:21 +100052 unsigned long alignmask = crypto_ablkcipher_alignmask(tfm);
Herbert Xub5b7f082007-04-16 20:48:54 +100053
54 if (keylen < cipher->min_keysize || keylen > cipher->max_keysize) {
55 crypto_ablkcipher_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
56 return -EINVAL;
57 }
58
Sebastian Siewiorca7c3932007-05-19 19:51:21 +100059 if ((unsigned long)key & alignmask)
60 return setkey_unaligned(tfm, key, keylen);
61
Herbert Xub5b7f082007-04-16 20:48:54 +100062 return cipher->setkey(tfm, key, keylen);
63}
64
65static unsigned int crypto_ablkcipher_ctxsize(struct crypto_alg *alg, u32 type,
66 u32 mask)
67{
68 return alg->cra_ctxsize;
69}
70
71static int crypto_init_ablkcipher_ops(struct crypto_tfm *tfm, u32 type,
72 u32 mask)
73{
74 struct ablkcipher_alg *alg = &tfm->__crt_alg->cra_ablkcipher;
75 struct ablkcipher_tfm *crt = &tfm->crt_ablkcipher;
76
77 if (alg->ivsize > PAGE_SIZE / 8)
78 return -EINVAL;
79
80 crt->setkey = setkey;
81 crt->encrypt = alg->encrypt;
82 crt->decrypt = alg->decrypt;
83 crt->ivsize = alg->ivsize;
84
85 return 0;
86}
87
88static void crypto_ablkcipher_show(struct seq_file *m, struct crypto_alg *alg)
89 __attribute__ ((unused));
90static void crypto_ablkcipher_show(struct seq_file *m, struct crypto_alg *alg)
91{
92 struct ablkcipher_alg *ablkcipher = &alg->cra_ablkcipher;
93
94 seq_printf(m, "type : ablkcipher\n");
95 seq_printf(m, "blocksize : %u\n", alg->cra_blocksize);
96 seq_printf(m, "min keysize : %u\n", ablkcipher->min_keysize);
97 seq_printf(m, "max keysize : %u\n", ablkcipher->max_keysize);
98 seq_printf(m, "ivsize : %u\n", ablkcipher->ivsize);
Herbert Xu23508e12007-11-27 21:33:24 +080099 seq_printf(m, "geniv : %s\n", ablkcipher->geniv ?: "<default>");
Herbert Xub5b7f082007-04-16 20:48:54 +1000100}
101
102const struct crypto_type crypto_ablkcipher_type = {
103 .ctxsize = crypto_ablkcipher_ctxsize,
104 .init = crypto_init_ablkcipher_ops,
105#ifdef CONFIG_PROC_FS
106 .show = crypto_ablkcipher_show,
107#endif
108};
109EXPORT_SYMBOL_GPL(crypto_ablkcipher_type);
110
Herbert Xu61da88e2007-12-17 21:51:27 +0800111static int no_givdecrypt(struct skcipher_givcrypt_request *req)
112{
113 return -ENOSYS;
114}
115
116static int crypto_init_givcipher_ops(struct crypto_tfm *tfm, u32 type,
117 u32 mask)
118{
119 struct ablkcipher_alg *alg = &tfm->__crt_alg->cra_ablkcipher;
120 struct ablkcipher_tfm *crt = &tfm->crt_ablkcipher;
121
122 if (alg->ivsize > PAGE_SIZE / 8)
123 return -EINVAL;
124
125 crt->setkey = setkey;
126 crt->encrypt = alg->encrypt;
127 crt->decrypt = alg->decrypt;
128 crt->givencrypt = alg->givencrypt;
129 crt->givdecrypt = alg->givdecrypt ?: no_givdecrypt;
130 crt->ivsize = alg->ivsize;
131
132 return 0;
133}
134
135static void crypto_givcipher_show(struct seq_file *m, struct crypto_alg *alg)
136 __attribute__ ((unused));
137static void crypto_givcipher_show(struct seq_file *m, struct crypto_alg *alg)
138{
139 struct ablkcipher_alg *ablkcipher = &alg->cra_ablkcipher;
140
141 seq_printf(m, "type : givcipher\n");
142 seq_printf(m, "blocksize : %u\n", alg->cra_blocksize);
143 seq_printf(m, "min keysize : %u\n", ablkcipher->min_keysize);
144 seq_printf(m, "max keysize : %u\n", ablkcipher->max_keysize);
145 seq_printf(m, "ivsize : %u\n", ablkcipher->ivsize);
Herbert Xu23508e12007-11-27 21:33:24 +0800146 seq_printf(m, "geniv : %s\n", ablkcipher->geniv ?: "<built-in>");
Herbert Xu61da88e2007-12-17 21:51:27 +0800147}
148
149const struct crypto_type crypto_givcipher_type = {
150 .ctxsize = crypto_ablkcipher_ctxsize,
151 .init = crypto_init_givcipher_ops,
152#ifdef CONFIG_PROC_FS
153 .show = crypto_givcipher_show,
154#endif
155};
156EXPORT_SYMBOL_GPL(crypto_givcipher_type);
157
Herbert Xu378f4f52007-12-17 20:07:31 +0800158int crypto_grab_skcipher(struct crypto_skcipher_spawn *spawn, const char *name,
159 u32 type, u32 mask)
160{
161 struct crypto_alg *alg;
162 int err;
163
164 type = crypto_skcipher_type(type);
165 mask = crypto_skcipher_mask(mask);
166
167 alg = crypto_alg_mod_lookup(name, type, mask);
168 if (IS_ERR(alg))
169 return PTR_ERR(alg);
170
171 err = crypto_init_spawn(&spawn->base, alg, spawn->base.inst, mask);
172 crypto_mod_put(alg);
173 return err;
174}
175EXPORT_SYMBOL_GPL(crypto_grab_skcipher);
176
Herbert Xub5b7f082007-04-16 20:48:54 +1000177MODULE_LICENSE("GPL");
178MODULE_DESCRIPTION("Asynchronous block chaining cipher type");