diff options
author | Herbert Xu <herbert@gondor.apana.org.au> | 2006-09-20 21:44:08 -0400 |
---|---|---|
committer | Herbert Xu <herbert@gondor.apana.org.au> | 2006-09-20 21:44:08 -0400 |
commit | db131ef9084110d9e82549c0a627e157e8bb99d7 (patch) | |
tree | 65330d3557a7dda47fa48876b7ea9cac1461301d | |
parent | 5cde0af2a9825dd1edaca233bd9590566579ef21 (diff) |
[CRYPTO] cipher: Added block ciphers for CBC/ECB
This patch adds two block cipher algorithms, CBC and ECB. These
are implemented as templates on top of existing single-block cipher
algorithms. They invoke the single-block cipher through the new
encrypt_one/decrypt_one interface.
This also optimises the in-place encryption and decryption to remove
the cost of an IV copy each round.
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
-rw-r--r-- | crypto/Kconfig | 17 | ||||
-rw-r--r-- | crypto/Makefile | 2 | ||||
-rw-r--r-- | crypto/cbc.c | 344 | ||||
-rw-r--r-- | crypto/ecb.c | 181 | ||||
-rw-r--r-- | crypto/internal.h | 1 | ||||
-rw-r--r-- | include/crypto/algapi.h | 2 |
6 files changed, 546 insertions, 1 deletions
diff --git a/crypto/Kconfig b/crypto/Kconfig index 68790ad7308d..90d467c99c2c 100644 --- a/crypto/Kconfig +++ b/crypto/Kconfig | |||
@@ -123,6 +123,23 @@ config CRYPTO_TGR192 | |||
123 | See also: | 123 | See also: |
124 | <http://www.cs.technion.ac.il/~biham/Reports/Tiger/>. | 124 | <http://www.cs.technion.ac.il/~biham/Reports/Tiger/>. |
125 | 125 | ||
126 | config CRYPTO_ECB | ||
127 | tristate "ECB support" | ||
128 | select CRYPTO_BLKCIPHER | ||
129 | default m | ||
130 | help | ||
131 | ECB: Electronic CodeBook mode | ||
132 | This is the simplest block cipher algorithm. It simply encrypts | ||
133 | the input block by block. | ||
134 | |||
135 | config CRYPTO_CBC | ||
136 | tristate "CBC support" | ||
137 | select CRYPTO_BLKCIPHER | ||
138 | default m | ||
139 | help | ||
140 | CBC: Cipher Block Chaining mode | ||
141 | This block cipher algorithm is required for IPSec. | ||
142 | |||
126 | config CRYPTO_DES | 143 | config CRYPTO_DES |
127 | tristate "DES and Triple DES EDE cipher algorithms" | 144 | tristate "DES and Triple DES EDE cipher algorithms" |
128 | select CRYPTO_ALGAPI | 145 | select CRYPTO_ALGAPI |
diff --git a/crypto/Makefile b/crypto/Makefile index b5051951c636..5e1ff4e0b1fc 100644 --- a/crypto/Makefile +++ b/crypto/Makefile | |||
@@ -20,6 +20,8 @@ obj-$(CONFIG_CRYPTO_SHA256) += sha256.o | |||
20 | obj-$(CONFIG_CRYPTO_SHA512) += sha512.o | 20 | obj-$(CONFIG_CRYPTO_SHA512) += sha512.o |
21 | obj-$(CONFIG_CRYPTO_WP512) += wp512.o | 21 | obj-$(CONFIG_CRYPTO_WP512) += wp512.o |
22 | obj-$(CONFIG_CRYPTO_TGR192) += tgr192.o | 22 | obj-$(CONFIG_CRYPTO_TGR192) += tgr192.o |
23 | obj-$(CONFIG_CRYPTO_ECB) += ecb.o | ||
24 | obj-$(CONFIG_CRYPTO_CBC) += cbc.o | ||
23 | obj-$(CONFIG_CRYPTO_DES) += des.o | 25 | obj-$(CONFIG_CRYPTO_DES) += des.o |
24 | obj-$(CONFIG_CRYPTO_BLOWFISH) += blowfish.o | 26 | obj-$(CONFIG_CRYPTO_BLOWFISH) += blowfish.o |
25 | obj-$(CONFIG_CRYPTO_TWOFISH) += twofish.o | 27 | obj-$(CONFIG_CRYPTO_TWOFISH) += twofish.o |
diff --git a/crypto/cbc.c b/crypto/cbc.c new file mode 100644 index 000000000000..f5542b4db387 --- /dev/null +++ b/crypto/cbc.c | |||
@@ -0,0 +1,344 @@ | |||
1 | /* | ||
2 | * CBC: Cipher Block Chaining mode | ||
3 | * | ||
4 | * Copyright (c) 2006 Herbert Xu <herbert@gondor.apana.org.au> | ||
5 | * | ||
6 | * This program is free software; you can redistribute it and/or modify it | ||
7 | * under the terms of the GNU General Public License as published by the Free | ||
8 | * Software Foundation; either version 2 of the License, or (at your option) | ||
9 | * any later version. | ||
10 | * | ||
11 | */ | ||
12 | |||
13 | #include <crypto/algapi.h> | ||
14 | #include <linux/err.h> | ||
15 | #include <linux/init.h> | ||
16 | #include <linux/kernel.h> | ||
17 | #include <linux/module.h> | ||
18 | #include <linux/scatterlist.h> | ||
19 | #include <linux/slab.h> | ||
20 | |||
21 | struct crypto_cbc_ctx { | ||
22 | struct crypto_cipher *child; | ||
23 | void (*xor)(u8 *dst, const u8 *src, unsigned int bs); | ||
24 | }; | ||
25 | |||
26 | static int crypto_cbc_setkey(struct crypto_tfm *parent, const u8 *key, | ||
27 | unsigned int keylen) | ||
28 | { | ||
29 | struct crypto_cbc_ctx *ctx = crypto_tfm_ctx(parent); | ||
30 | struct crypto_cipher *child = ctx->child; | ||
31 | int err; | ||
32 | |||
33 | crypto_cipher_clear_flags(child, CRYPTO_TFM_REQ_MASK); | ||
34 | crypto_cipher_set_flags(child, crypto_tfm_get_flags(parent) & | ||
35 | CRYPTO_TFM_REQ_MASK); | ||
36 | err = crypto_cipher_setkey(child, key, keylen); | ||
37 | crypto_tfm_set_flags(parent, crypto_cipher_get_flags(child) & | ||
38 | CRYPTO_TFM_RES_MASK); | ||
39 | return err; | ||
40 | } | ||
41 | |||
42 | static int crypto_cbc_encrypt_segment(struct blkcipher_desc *desc, | ||
43 | struct blkcipher_walk *walk, | ||
44 | struct crypto_cipher *tfm, | ||
45 | void (*xor)(u8 *, const u8 *, | ||
46 | unsigned int)) | ||
47 | { | ||
48 | void (*fn)(struct crypto_tfm *, u8 *, const u8 *) = | ||
49 | crypto_cipher_alg(tfm)->cia_encrypt; | ||
50 | int bsize = crypto_cipher_blocksize(tfm); | ||
51 | unsigned int nbytes = walk->nbytes; | ||
52 | u8 *src = walk->src.virt.addr; | ||
53 | u8 *dst = walk->dst.virt.addr; | ||
54 | u8 *iv = walk->iv; | ||
55 | |||
56 | do { | ||
57 | xor(iv, src, bsize); | ||
58 | fn(crypto_cipher_tfm(tfm), dst, iv); | ||
59 | memcpy(iv, dst, bsize); | ||
60 | |||
61 | src += bsize; | ||
62 | dst += bsize; | ||
63 | } while ((nbytes -= bsize) >= bsize); | ||
64 | |||
65 | return nbytes; | ||
66 | } | ||
67 | |||
68 | static int crypto_cbc_encrypt_inplace(struct blkcipher_desc *desc, | ||
69 | struct blkcipher_walk *walk, | ||
70 | struct crypto_cipher *tfm, | ||
71 | void (*xor)(u8 *, const u8 *, | ||
72 | unsigned int)) | ||
73 | { | ||
74 | void (*fn)(struct crypto_tfm *, u8 *, const u8 *) = | ||
75 | crypto_cipher_alg(tfm)->cia_encrypt; | ||
76 | int bsize = crypto_cipher_blocksize(tfm); | ||
77 | unsigned int nbytes = walk->nbytes; | ||
78 | u8 *src = walk->src.virt.addr; | ||
79 | u8 *iv = walk->iv; | ||
80 | |||
81 | do { | ||
82 | xor(src, iv, bsize); | ||
83 | fn(crypto_cipher_tfm(tfm), src, src); | ||
84 | iv = src; | ||
85 | |||
86 | src += bsize; | ||
87 | } while ((nbytes -= bsize) >= bsize); | ||
88 | |||
89 | memcpy(walk->iv, iv, bsize); | ||
90 | |||
91 | return nbytes; | ||
92 | } | ||
93 | |||
94 | static int crypto_cbc_encrypt(struct blkcipher_desc *desc, | ||
95 | struct scatterlist *dst, struct scatterlist *src, | ||
96 | unsigned int nbytes) | ||
97 | { | ||
98 | struct blkcipher_walk walk; | ||
99 | struct crypto_blkcipher *tfm = desc->tfm; | ||
100 | struct crypto_cbc_ctx *ctx = crypto_blkcipher_ctx(tfm); | ||
101 | struct crypto_cipher *child = ctx->child; | ||
102 | void (*xor)(u8 *, const u8 *, unsigned int bs) = ctx->xor; | ||
103 | int err; | ||
104 | |||
105 | blkcipher_walk_init(&walk, dst, src, nbytes); | ||
106 | err = blkcipher_walk_virt(desc, &walk); | ||
107 | |||
108 | while ((nbytes = walk.nbytes)) { | ||
109 | if (walk.src.virt.addr == walk.dst.virt.addr) | ||
110 | nbytes = crypto_cbc_encrypt_inplace(desc, &walk, child, | ||
111 | xor); | ||
112 | else | ||
113 | nbytes = crypto_cbc_encrypt_segment(desc, &walk, child, | ||
114 | xor); | ||
115 | err = blkcipher_walk_done(desc, &walk, nbytes); | ||
116 | } | ||
117 | |||
118 | return err; | ||
119 | } | ||
120 | |||
121 | static int crypto_cbc_decrypt_segment(struct blkcipher_desc *desc, | ||
122 | struct blkcipher_walk *walk, | ||
123 | struct crypto_cipher *tfm, | ||
124 | void (*xor)(u8 *, const u8 *, | ||
125 | unsigned int)) | ||
126 | { | ||
127 | void (*fn)(struct crypto_tfm *, u8 *, const u8 *) = | ||
128 | crypto_cipher_alg(tfm)->cia_decrypt; | ||
129 | int bsize = crypto_cipher_blocksize(tfm); | ||
130 | unsigned int nbytes = walk->nbytes; | ||
131 | u8 *src = walk->src.virt.addr; | ||
132 | u8 *dst = walk->dst.virt.addr; | ||
133 | u8 *iv = walk->iv; | ||
134 | |||
135 | do { | ||
136 | fn(crypto_cipher_tfm(tfm), dst, src); | ||
137 | xor(dst, iv, bsize); | ||
138 | iv = src; | ||
139 | |||
140 | src += bsize; | ||
141 | dst += bsize; | ||
142 | } while ((nbytes -= bsize) >= bsize); | ||
143 | |||
144 | memcpy(walk->iv, iv, bsize); | ||
145 | |||
146 | return nbytes; | ||
147 | } | ||
148 | |||
149 | static int crypto_cbc_decrypt_inplace(struct blkcipher_desc *desc, | ||
150 | struct blkcipher_walk *walk, | ||
151 | struct crypto_cipher *tfm, | ||
152 | void (*xor)(u8 *, const u8 *, | ||
153 | unsigned int)) | ||
154 | { | ||
155 | void (*fn)(struct crypto_tfm *, u8 *, const u8 *) = | ||
156 | crypto_cipher_alg(tfm)->cia_decrypt; | ||
157 | int bsize = crypto_cipher_blocksize(tfm); | ||
158 | unsigned long alignmask = crypto_cipher_alignmask(tfm); | ||
159 | unsigned int nbytes = walk->nbytes; | ||
160 | u8 *src = walk->src.virt.addr; | ||
161 | u8 stack[bsize + alignmask]; | ||
162 | u8 *first_iv = (u8 *)ALIGN((unsigned long)stack, alignmask + 1); | ||
163 | |||
164 | memcpy(first_iv, walk->iv, bsize); | ||
165 | |||
166 | /* Start of the last block. */ | ||
167 | src += nbytes - nbytes % bsize - bsize; | ||
168 | memcpy(walk->iv, src, bsize); | ||
169 | |||
170 | for (;;) { | ||
171 | fn(crypto_cipher_tfm(tfm), src, src); | ||
172 | if ((nbytes -= bsize) < bsize) | ||
173 | break; | ||
174 | xor(src, src - bsize, bsize); | ||
175 | src -= bsize; | ||
176 | } | ||
177 | |||
178 | xor(src, first_iv, bsize); | ||
179 | |||
180 | return nbytes; | ||
181 | } | ||
182 | |||
183 | static int crypto_cbc_decrypt(struct blkcipher_desc *desc, | ||
184 | struct scatterlist *dst, struct scatterlist *src, | ||
185 | unsigned int nbytes) | ||
186 | { | ||
187 | struct blkcipher_walk walk; | ||
188 | struct crypto_blkcipher *tfm = desc->tfm; | ||
189 | struct crypto_cbc_ctx *ctx = crypto_blkcipher_ctx(tfm); | ||
190 | struct crypto_cipher *child = ctx->child; | ||
191 | void (*xor)(u8 *, const u8 *, unsigned int bs) = ctx->xor; | ||
192 | int err; | ||
193 | |||
194 | blkcipher_walk_init(&walk, dst, src, nbytes); | ||
195 | err = blkcipher_walk_virt(desc, &walk); | ||
196 | |||
197 | while ((nbytes = walk.nbytes)) { | ||
198 | if (walk.src.virt.addr == walk.dst.virt.addr) | ||
199 | nbytes = crypto_cbc_decrypt_inplace(desc, &walk, child, | ||
200 | xor); | ||
201 | else | ||
202 | nbytes = crypto_cbc_decrypt_segment(desc, &walk, child, | ||
203 | xor); | ||
204 | err = blkcipher_walk_done(desc, &walk, nbytes); | ||
205 | } | ||
206 | |||
207 | return err; | ||
208 | } | ||
209 | |||
210 | static void xor_byte(u8 *a, const u8 *b, unsigned int bs) | ||
211 | { | ||
212 | do { | ||
213 | *a++ ^= *b++; | ||
214 | } while (--bs); | ||
215 | } | ||
216 | |||
217 | static void xor_quad(u8 *dst, const u8 *src, unsigned int bs) | ||
218 | { | ||
219 | u32 *a = (u32 *)dst; | ||
220 | u32 *b = (u32 *)src; | ||
221 | |||
222 | do { | ||
223 | *a++ ^= *b++; | ||
224 | } while ((bs -= 4)); | ||
225 | } | ||
226 | |||
227 | static void xor_64(u8 *a, const u8 *b, unsigned int bs) | ||
228 | { | ||
229 | ((u32 *)a)[0] ^= ((u32 *)b)[0]; | ||
230 | ((u32 *)a)[1] ^= ((u32 *)b)[1]; | ||
231 | } | ||
232 | |||
233 | static void xor_128(u8 *a, const u8 *b, unsigned int bs) | ||
234 | { | ||
235 | ((u32 *)a)[0] ^= ((u32 *)b)[0]; | ||
236 | ((u32 *)a)[1] ^= ((u32 *)b)[1]; | ||
237 | ((u32 *)a)[2] ^= ((u32 *)b)[2]; | ||
238 | ((u32 *)a)[3] ^= ((u32 *)b)[3]; | ||
239 | } | ||
240 | |||
241 | static int crypto_cbc_init_tfm(struct crypto_tfm *tfm) | ||
242 | { | ||
243 | struct crypto_instance *inst = (void *)tfm->__crt_alg; | ||
244 | struct crypto_spawn *spawn = crypto_instance_ctx(inst); | ||
245 | struct crypto_cbc_ctx *ctx = crypto_tfm_ctx(tfm); | ||
246 | |||
247 | switch (crypto_tfm_alg_blocksize(tfm)) { | ||
248 | case 8: | ||
249 | ctx->xor = xor_64; | ||
250 | break; | ||
251 | |||
252 | case 16: | ||
253 | ctx->xor = xor_128; | ||
254 | break; | ||
255 | |||
256 | default: | ||
257 | if (crypto_tfm_alg_blocksize(tfm) % 4) | ||
258 | ctx->xor = xor_byte; | ||
259 | else | ||
260 | ctx->xor = xor_quad; | ||
261 | } | ||
262 | |||
263 | tfm = crypto_spawn_tfm(spawn); | ||
264 | if (IS_ERR(tfm)) | ||
265 | return PTR_ERR(tfm); | ||
266 | |||
267 | ctx->child = crypto_cipher_cast(tfm); | ||
268 | return 0; | ||
269 | } | ||
270 | |||
271 | static void crypto_cbc_exit_tfm(struct crypto_tfm *tfm) | ||
272 | { | ||
273 | struct crypto_cbc_ctx *ctx = crypto_tfm_ctx(tfm); | ||
274 | crypto_free_cipher(ctx->child); | ||
275 | } | ||
276 | |||
277 | static struct crypto_instance *crypto_cbc_alloc(void *param, unsigned int len) | ||
278 | { | ||
279 | struct crypto_instance *inst; | ||
280 | struct crypto_alg *alg; | ||
281 | |||
282 | alg = crypto_get_attr_alg(param, len, CRYPTO_ALG_TYPE_CIPHER, | ||
283 | CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_ASYNC); | ||
284 | if (IS_ERR(alg)) | ||
285 | return ERR_PTR(PTR_ERR(alg)); | ||
286 | |||
287 | inst = crypto_alloc_instance("cbc", alg); | ||
288 | if (IS_ERR(inst)) | ||
289 | goto out_put_alg; | ||
290 | |||
291 | inst->alg.cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER; | ||
292 | inst->alg.cra_priority = alg->cra_priority; | ||
293 | inst->alg.cra_blocksize = alg->cra_blocksize; | ||
294 | inst->alg.cra_alignmask = alg->cra_alignmask; | ||
295 | inst->alg.cra_type = &crypto_blkcipher_type; | ||
296 | |||
297 | if (!(alg->cra_blocksize % 4)) | ||
298 | inst->alg.cra_alignmask |= 3; | ||
299 | inst->alg.cra_blkcipher.ivsize = alg->cra_blocksize; | ||
300 | inst->alg.cra_blkcipher.min_keysize = alg->cra_cipher.cia_min_keysize; | ||
301 | inst->alg.cra_blkcipher.max_keysize = alg->cra_cipher.cia_max_keysize; | ||
302 | |||
303 | inst->alg.cra_ctxsize = sizeof(struct crypto_cbc_ctx); | ||
304 | |||
305 | inst->alg.cra_init = crypto_cbc_init_tfm; | ||
306 | inst->alg.cra_exit = crypto_cbc_exit_tfm; | ||
307 | |||
308 | inst->alg.cra_blkcipher.setkey = crypto_cbc_setkey; | ||
309 | inst->alg.cra_blkcipher.encrypt = crypto_cbc_encrypt; | ||
310 | inst->alg.cra_blkcipher.decrypt = crypto_cbc_decrypt; | ||
311 | |||
312 | out_put_alg: | ||
313 | crypto_mod_put(alg); | ||
314 | return inst; | ||
315 | } | ||
316 | |||
317 | static void crypto_cbc_free(struct crypto_instance *inst) | ||
318 | { | ||
319 | crypto_drop_spawn(crypto_instance_ctx(inst)); | ||
320 | kfree(inst); | ||
321 | } | ||
322 | |||
323 | static struct crypto_template crypto_cbc_tmpl = { | ||
324 | .name = "cbc", | ||
325 | .alloc = crypto_cbc_alloc, | ||
326 | .free = crypto_cbc_free, | ||
327 | .module = THIS_MODULE, | ||
328 | }; | ||
329 | |||
330 | static int __init crypto_cbc_module_init(void) | ||
331 | { | ||
332 | return crypto_register_template(&crypto_cbc_tmpl); | ||
333 | } | ||
334 | |||
335 | static void __exit crypto_cbc_module_exit(void) | ||
336 | { | ||
337 | crypto_unregister_template(&crypto_cbc_tmpl); | ||
338 | } | ||
339 | |||
340 | module_init(crypto_cbc_module_init); | ||
341 | module_exit(crypto_cbc_module_exit); | ||
342 | |||
343 | MODULE_LICENSE("GPL"); | ||
344 | MODULE_DESCRIPTION("CBC block cipher algorithm"); | ||
diff --git a/crypto/ecb.c b/crypto/ecb.c new file mode 100644 index 000000000000..f239aa9c4017 --- /dev/null +++ b/crypto/ecb.c | |||
@@ -0,0 +1,181 @@ | |||
1 | /* | ||
2 | * ECB: Electronic CodeBook mode | ||
3 | * | ||
4 | * Copyright (c) 2006 Herbert Xu <herbert@gondor.apana.org.au> | ||
5 | * | ||
6 | * This program is free software; you can redistribute it and/or modify it | ||
7 | * under the terms of the GNU General Public License as published by the Free | ||
8 | * Software Foundation; either version 2 of the License, or (at your option) | ||
9 | * any later version. | ||
10 | * | ||
11 | */ | ||
12 | |||
13 | #include <crypto/algapi.h> | ||
14 | #include <linux/err.h> | ||
15 | #include <linux/init.h> | ||
16 | #include <linux/kernel.h> | ||
17 | #include <linux/module.h> | ||
18 | #include <linux/scatterlist.h> | ||
19 | #include <linux/slab.h> | ||
20 | |||
21 | struct crypto_ecb_ctx { | ||
22 | struct crypto_cipher *child; | ||
23 | }; | ||
24 | |||
25 | static int crypto_ecb_setkey(struct crypto_tfm *parent, const u8 *key, | ||
26 | unsigned int keylen) | ||
27 | { | ||
28 | struct crypto_ecb_ctx *ctx = crypto_tfm_ctx(parent); | ||
29 | struct crypto_cipher *child = ctx->child; | ||
30 | int err; | ||
31 | |||
32 | crypto_cipher_clear_flags(child, CRYPTO_TFM_REQ_MASK); | ||
33 | crypto_cipher_set_flags(child, crypto_tfm_get_flags(parent) & | ||
34 | CRYPTO_TFM_REQ_MASK); | ||
35 | err = crypto_cipher_setkey(child, key, keylen); | ||
36 | crypto_tfm_set_flags(parent, crypto_cipher_get_flags(child) & | ||
37 | CRYPTO_TFM_RES_MASK); | ||
38 | return err; | ||
39 | } | ||
40 | |||
41 | static int crypto_ecb_crypt(struct blkcipher_desc *desc, | ||
42 | struct blkcipher_walk *walk, | ||
43 | struct crypto_cipher *tfm, | ||
44 | void (*fn)(struct crypto_tfm *, u8 *, const u8 *)) | ||
45 | { | ||
46 | int bsize = crypto_cipher_blocksize(tfm); | ||
47 | unsigned int nbytes; | ||
48 | int err; | ||
49 | |||
50 | err = blkcipher_walk_virt(desc, walk); | ||
51 | |||
52 | while ((nbytes = walk->nbytes)) { | ||
53 | u8 *wsrc = walk->src.virt.addr; | ||
54 | u8 *wdst = walk->dst.virt.addr; | ||
55 | |||
56 | do { | ||
57 | fn(crypto_cipher_tfm(tfm), wdst, wsrc); | ||
58 | |||
59 | wsrc += bsize; | ||
60 | wdst += bsize; | ||
61 | } while ((nbytes -= bsize) >= bsize); | ||
62 | |||
63 | err = blkcipher_walk_done(desc, walk, nbytes); | ||
64 | } | ||
65 | |||
66 | return err; | ||
67 | } | ||
68 | |||
69 | static int crypto_ecb_encrypt(struct blkcipher_desc *desc, | ||
70 | struct scatterlist *dst, struct scatterlist *src, | ||
71 | unsigned int nbytes) | ||
72 | { | ||
73 | struct blkcipher_walk walk; | ||
74 | struct crypto_blkcipher *tfm = desc->tfm; | ||
75 | struct crypto_ecb_ctx *ctx = crypto_blkcipher_ctx(tfm); | ||
76 | struct crypto_cipher *child = ctx->child; | ||
77 | |||
78 | blkcipher_walk_init(&walk, dst, src, nbytes); | ||
79 | return crypto_ecb_crypt(desc, &walk, child, | ||
80 | crypto_cipher_alg(child)->cia_encrypt); | ||
81 | } | ||
82 | |||
83 | static int crypto_ecb_decrypt(struct blkcipher_desc *desc, | ||
84 | struct scatterlist *dst, struct scatterlist *src, | ||
85 | unsigned int nbytes) | ||
86 | { | ||
87 | struct blkcipher_walk walk; | ||
88 | struct crypto_blkcipher *tfm = desc->tfm; | ||
89 | struct crypto_ecb_ctx *ctx = crypto_blkcipher_ctx(tfm); | ||
90 | struct crypto_cipher *child = ctx->child; | ||
91 | |||
92 | blkcipher_walk_init(&walk, dst, src, nbytes); | ||
93 | return crypto_ecb_crypt(desc, &walk, child, | ||
94 | crypto_cipher_alg(child)->cia_decrypt); | ||
95 | } | ||
96 | |||
97 | static int crypto_ecb_init_tfm(struct crypto_tfm *tfm) | ||
98 | { | ||
99 | struct crypto_instance *inst = (void *)tfm->__crt_alg; | ||
100 | struct crypto_spawn *spawn = crypto_instance_ctx(inst); | ||
101 | struct crypto_ecb_ctx *ctx = crypto_tfm_ctx(tfm); | ||
102 | |||
103 | tfm = crypto_spawn_tfm(spawn); | ||
104 | if (IS_ERR(tfm)) | ||
105 | return PTR_ERR(tfm); | ||
106 | |||
107 | ctx->child = crypto_cipher_cast(tfm); | ||
108 | return 0; | ||
109 | } | ||
110 | |||
111 | static void crypto_ecb_exit_tfm(struct crypto_tfm *tfm) | ||
112 | { | ||
113 | struct crypto_ecb_ctx *ctx = crypto_tfm_ctx(tfm); | ||
114 | crypto_free_cipher(ctx->child); | ||
115 | } | ||
116 | |||
117 | static struct crypto_instance *crypto_ecb_alloc(void *param, unsigned int len) | ||
118 | { | ||
119 | struct crypto_instance *inst; | ||
120 | struct crypto_alg *alg; | ||
121 | |||
122 | alg = crypto_get_attr_alg(param, len, CRYPTO_ALG_TYPE_CIPHER, | ||
123 | CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_ASYNC); | ||
124 | if (IS_ERR(alg)) | ||
125 | return ERR_PTR(PTR_ERR(alg)); | ||
126 | |||
127 | inst = crypto_alloc_instance("ecb", alg); | ||
128 | if (IS_ERR(inst)) | ||
129 | goto out_put_alg; | ||
130 | |||
131 | inst->alg.cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER; | ||
132 | inst->alg.cra_priority = alg->cra_priority; | ||
133 | inst->alg.cra_blocksize = alg->cra_blocksize; | ||
134 | inst->alg.cra_alignmask = alg->cra_alignmask; | ||
135 | inst->alg.cra_type = &crypto_blkcipher_type; | ||
136 | |||
137 | inst->alg.cra_blkcipher.min_keysize = alg->cra_cipher.cia_min_keysize; | ||
138 | inst->alg.cra_blkcipher.max_keysize = alg->cra_cipher.cia_max_keysize; | ||
139 | |||
140 | inst->alg.cra_ctxsize = sizeof(struct crypto_ecb_ctx); | ||
141 | |||
142 | inst->alg.cra_init = crypto_ecb_init_tfm; | ||
143 | inst->alg.cra_exit = crypto_ecb_exit_tfm; | ||
144 | |||
145 | inst->alg.cra_blkcipher.setkey = crypto_ecb_setkey; | ||
146 | inst->alg.cra_blkcipher.encrypt = crypto_ecb_encrypt; | ||
147 | inst->alg.cra_blkcipher.decrypt = crypto_ecb_decrypt; | ||
148 | |||
149 | out_put_alg: | ||
150 | crypto_mod_put(alg); | ||
151 | return inst; | ||
152 | } | ||
153 | |||
154 | static void crypto_ecb_free(struct crypto_instance *inst) | ||
155 | { | ||
156 | crypto_drop_spawn(crypto_instance_ctx(inst)); | ||
157 | kfree(inst); | ||
158 | } | ||
159 | |||
160 | static struct crypto_template crypto_ecb_tmpl = { | ||
161 | .name = "ecb", | ||
162 | .alloc = crypto_ecb_alloc, | ||
163 | .free = crypto_ecb_free, | ||
164 | .module = THIS_MODULE, | ||
165 | }; | ||
166 | |||
167 | static int __init crypto_ecb_module_init(void) | ||
168 | { | ||
169 | return crypto_register_template(&crypto_ecb_tmpl); | ||
170 | } | ||
171 | |||
172 | static void __exit crypto_ecb_module_exit(void) | ||
173 | { | ||
174 | crypto_unregister_template(&crypto_ecb_tmpl); | ||
175 | } | ||
176 | |||
177 | module_init(crypto_ecb_module_init); | ||
178 | module_exit(crypto_ecb_module_exit); | ||
179 | |||
180 | MODULE_LICENSE("GPL"); | ||
181 | MODULE_DESCRIPTION("ECB block cipher algorithm"); | ||
diff --git a/crypto/internal.h b/crypto/internal.h index 7dc04efb55c6..93d9b10ff914 100644 --- a/crypto/internal.h +++ b/crypto/internal.h | |||
@@ -131,7 +131,6 @@ static inline unsigned int crypto_compress_ctxsize(struct crypto_alg *alg, | |||
131 | } | 131 | } |
132 | 132 | ||
133 | struct crypto_alg *crypto_mod_get(struct crypto_alg *alg); | 133 | struct crypto_alg *crypto_mod_get(struct crypto_alg *alg); |
134 | void crypto_mod_put(struct crypto_alg *alg); | ||
135 | struct crypto_alg *__crypto_alg_lookup(const char *name, u32 type, u32 mask); | 134 | struct crypto_alg *__crypto_alg_lookup(const char *name, u32 type, u32 mask); |
136 | struct crypto_alg *crypto_alg_mod_lookup(const char *name, u32 type, u32 mask); | 135 | struct crypto_alg *crypto_alg_mod_lookup(const char *name, u32 type, u32 mask); |
137 | 136 | ||
diff --git a/include/crypto/algapi.h b/include/crypto/algapi.h index f3946baf0c07..444f602724db 100644 --- a/include/crypto/algapi.h +++ b/include/crypto/algapi.h | |||
@@ -83,6 +83,8 @@ struct blkcipher_walk { | |||
83 | 83 | ||
84 | extern const struct crypto_type crypto_blkcipher_type; | 84 | extern const struct crypto_type crypto_blkcipher_type; |
85 | 85 | ||
86 | void crypto_mod_put(struct crypto_alg *alg); | ||
87 | |||
86 | int crypto_register_template(struct crypto_template *tmpl); | 88 | int crypto_register_template(struct crypto_template *tmpl); |
87 | void crypto_unregister_template(struct crypto_template *tmpl); | 89 | void crypto_unregister_template(struct crypto_template *tmpl); |
88 | struct crypto_template *crypto_lookup_template(const char *name); | 90 | struct crypto_template *crypto_lookup_template(const char *name); |