Merge git://git.kernel.org/pub/scm/linux/kernel/git/herbert/crypto-2.6
[linux-2.6] / crypto / blkcipher.c
1 /*
2  * Block chaining cipher operations.
3  * 
4  * Generic encrypt/decrypt wrapper for ciphers, handles operations across
5  * multiple page boundaries by using temporary blocks.  In user context,
6  * the kernel is given a chance to schedule us once per page.
7  *
8  * Copyright (c) 2006 Herbert Xu <herbert@gondor.apana.org.au>
9  *
10  * This program is free software; you can redistribute it and/or modify it
11  * under the terms of the GNU General Public License as published by the Free
12  * Software Foundation; either version 2 of the License, or (at your option) 
13  * any later version.
14  *
15  */
16
17 #include <linux/crypto.h>
18 #include <linux/errno.h>
19 #include <linux/hardirq.h>
20 #include <linux/kernel.h>
21 #include <linux/module.h>
22 #include <linux/scatterlist.h>
23 #include <linux/seq_file.h>
24 #include <linux/slab.h>
25 #include <linux/string.h>
26
27 #include "internal.h"
28 #include "scatterwalk.h"
29
30 enum {
31         BLKCIPHER_WALK_PHYS = 1 << 0,
32         BLKCIPHER_WALK_SLOW = 1 << 1,
33         BLKCIPHER_WALK_COPY = 1 << 2,
34         BLKCIPHER_WALK_DIFF = 1 << 3,
35 };
36
37 static int blkcipher_walk_next(struct blkcipher_desc *desc,
38                                struct blkcipher_walk *walk);
39 static int blkcipher_walk_first(struct blkcipher_desc *desc,
40                                 struct blkcipher_walk *walk);
41
42 static inline void blkcipher_map_src(struct blkcipher_walk *walk)
43 {
44         walk->src.virt.addr = scatterwalk_map(&walk->in, 0);
45 }
46
47 static inline void blkcipher_map_dst(struct blkcipher_walk *walk)
48 {
49         walk->dst.virt.addr = scatterwalk_map(&walk->out, 1);
50 }
51
52 static inline void blkcipher_unmap_src(struct blkcipher_walk *walk)
53 {
54         scatterwalk_unmap(walk->src.virt.addr, 0);
55 }
56
57 static inline void blkcipher_unmap_dst(struct blkcipher_walk *walk)
58 {
59         scatterwalk_unmap(walk->dst.virt.addr, 1);
60 }
61
62 /* Get a spot of the specified length that does not straddle a page.
63  * The caller needs to ensure that there is enough space for this operation.
64  */
65 static inline u8 *blkcipher_get_spot(u8 *start, unsigned int len)
66 {
67         u8 *end_page = (u8 *)(((unsigned long)(start + len - 1)) & PAGE_MASK);
68         return start > end_page ? start : end_page;
69 }
70
71 static inline unsigned int blkcipher_done_slow(struct crypto_blkcipher *tfm,
72                                                struct blkcipher_walk *walk,
73                                                unsigned int bsize)
74 {
75         u8 *addr;
76         unsigned int alignmask = crypto_blkcipher_alignmask(tfm);
77
78         addr = (u8 *)ALIGN((unsigned long)walk->buffer, alignmask + 1);
79         addr = blkcipher_get_spot(addr, bsize);
80         scatterwalk_copychunks(addr, &walk->out, bsize, 1);
81         return bsize;
82 }
83
84 static inline unsigned int blkcipher_done_fast(struct blkcipher_walk *walk,
85                                                unsigned int n)
86 {
87         n = walk->nbytes - n;
88
89         if (walk->flags & BLKCIPHER_WALK_COPY) {
90                 blkcipher_map_dst(walk);
91                 memcpy(walk->dst.virt.addr, walk->page, n);
92                 blkcipher_unmap_dst(walk);
93         } else if (!(walk->flags & BLKCIPHER_WALK_PHYS)) {
94                 blkcipher_unmap_src(walk);
95                 if (walk->flags & BLKCIPHER_WALK_DIFF)
96                         blkcipher_unmap_dst(walk);
97         }
98
99         scatterwalk_advance(&walk->in, n);
100         scatterwalk_advance(&walk->out, n);
101
102         return n;
103 }
104
105 int blkcipher_walk_done(struct blkcipher_desc *desc,
106                         struct blkcipher_walk *walk, int err)
107 {
108         struct crypto_blkcipher *tfm = desc->tfm;
109         unsigned int nbytes = 0;
110
111         if (likely(err >= 0)) {
112                 unsigned int bsize = crypto_blkcipher_blocksize(tfm);
113                 unsigned int n;
114
115                 if (likely(!(walk->flags & BLKCIPHER_WALK_SLOW)))
116                         n = blkcipher_done_fast(walk, err);
117                 else
118                         n = blkcipher_done_slow(tfm, walk, bsize);
119
120                 nbytes = walk->total - n;
121                 err = 0;
122         }
123
124         scatterwalk_done(&walk->in, 0, nbytes);
125         scatterwalk_done(&walk->out, 1, nbytes);
126
127         walk->total = nbytes;
128         walk->nbytes = nbytes;
129
130         if (nbytes) {
131                 crypto_yield(desc->flags);
132                 return blkcipher_walk_next(desc, walk);
133         }
134
135         if (walk->iv != desc->info)
136                 memcpy(desc->info, walk->iv, crypto_blkcipher_ivsize(tfm));
137         if (walk->buffer != walk->page)
138                 kfree(walk->buffer);
139         if (walk->page)
140                 free_page((unsigned long)walk->page);
141
142         return err;
143 }
144 EXPORT_SYMBOL_GPL(blkcipher_walk_done);
145
146 static inline int blkcipher_next_slow(struct blkcipher_desc *desc,
147                                       struct blkcipher_walk *walk,
148                                       unsigned int bsize,
149                                       unsigned int alignmask)
150 {
151         unsigned int n;
152
153         if (walk->buffer)
154                 goto ok;
155
156         walk->buffer = walk->page;
157         if (walk->buffer)
158                 goto ok;
159
160         n = bsize * 3 - (alignmask + 1) +
161             (alignmask & ~(crypto_tfm_ctx_alignment() - 1));
162         walk->buffer = kmalloc(n, GFP_ATOMIC);
163         if (!walk->buffer)
164                 return blkcipher_walk_done(desc, walk, -ENOMEM);
165
166 ok:
167         walk->dst.virt.addr = (u8 *)ALIGN((unsigned long)walk->buffer,
168                                           alignmask + 1);
169         walk->dst.virt.addr = blkcipher_get_spot(walk->dst.virt.addr, bsize);
170         walk->src.virt.addr = blkcipher_get_spot(walk->dst.virt.addr + bsize,
171                                                  bsize);
172
173         scatterwalk_copychunks(walk->src.virt.addr, &walk->in, bsize, 0);
174
175         walk->nbytes = bsize;
176         walk->flags |= BLKCIPHER_WALK_SLOW;
177
178         return 0;
179 }
180
181 static inline int blkcipher_next_copy(struct blkcipher_walk *walk)
182 {
183         u8 *tmp = walk->page;
184
185         blkcipher_map_src(walk);
186         memcpy(tmp, walk->src.virt.addr, walk->nbytes);
187         blkcipher_unmap_src(walk);
188
189         walk->src.virt.addr = tmp;
190         walk->dst.virt.addr = tmp;
191
192         return 0;
193 }
194
195 static inline int blkcipher_next_fast(struct blkcipher_desc *desc,
196                                       struct blkcipher_walk *walk)
197 {
198         unsigned long diff;
199
200         walk->src.phys.page = scatterwalk_page(&walk->in);
201         walk->src.phys.offset = offset_in_page(walk->in.offset);
202         walk->dst.phys.page = scatterwalk_page(&walk->out);
203         walk->dst.phys.offset = offset_in_page(walk->out.offset);
204
205         if (walk->flags & BLKCIPHER_WALK_PHYS)
206                 return 0;
207
208         diff = walk->src.phys.offset - walk->dst.phys.offset;
209         diff |= walk->src.virt.page - walk->dst.virt.page;
210
211         blkcipher_map_src(walk);
212         walk->dst.virt.addr = walk->src.virt.addr;
213
214         if (diff) {
215                 walk->flags |= BLKCIPHER_WALK_DIFF;
216                 blkcipher_map_dst(walk);
217         }
218
219         return 0;
220 }
221
222 static int blkcipher_walk_next(struct blkcipher_desc *desc,
223                                struct blkcipher_walk *walk)
224 {
225         struct crypto_blkcipher *tfm = desc->tfm;
226         unsigned int alignmask = crypto_blkcipher_alignmask(tfm);
227         unsigned int bsize = crypto_blkcipher_blocksize(tfm);
228         unsigned int n;
229         int err;
230
231         n = walk->total;
232         if (unlikely(n < bsize)) {
233                 desc->flags |= CRYPTO_TFM_RES_BAD_BLOCK_LEN;
234                 return blkcipher_walk_done(desc, walk, -EINVAL);
235         }
236
237         walk->flags &= ~(BLKCIPHER_WALK_SLOW | BLKCIPHER_WALK_COPY |
238                          BLKCIPHER_WALK_DIFF);
239         if (!scatterwalk_aligned(&walk->in, alignmask) ||
240             !scatterwalk_aligned(&walk->out, alignmask)) {
241                 walk->flags |= BLKCIPHER_WALK_COPY;
242                 if (!walk->page) {
243                         walk->page = (void *)__get_free_page(GFP_ATOMIC);
244                         if (!walk->page)
245                                 n = 0;
246                 }
247         }
248
249         n = scatterwalk_clamp(&walk->in, n);
250         n = scatterwalk_clamp(&walk->out, n);
251
252         if (unlikely(n < bsize)) {
253                 err = blkcipher_next_slow(desc, walk, bsize, alignmask);
254                 goto set_phys_lowmem;
255         }
256
257         walk->nbytes = n;
258         if (walk->flags & BLKCIPHER_WALK_COPY) {
259                 err = blkcipher_next_copy(walk);
260                 goto set_phys_lowmem;
261         }
262
263         return blkcipher_next_fast(desc, walk);
264
265 set_phys_lowmem:
266         if (walk->flags & BLKCIPHER_WALK_PHYS) {
267                 walk->src.phys.page = virt_to_page(walk->src.virt.addr);
268                 walk->dst.phys.page = virt_to_page(walk->dst.virt.addr);
269                 walk->src.phys.offset &= PAGE_SIZE - 1;
270                 walk->dst.phys.offset &= PAGE_SIZE - 1;
271         }
272         return err;
273 }
274
275 static inline int blkcipher_copy_iv(struct blkcipher_walk *walk,
276                                     struct crypto_blkcipher *tfm,
277                                     unsigned int alignmask)
278 {
279         unsigned bs = crypto_blkcipher_blocksize(tfm);
280         unsigned int ivsize = crypto_blkcipher_ivsize(tfm);
281         unsigned int size = bs * 2 + ivsize + max(bs, ivsize) - (alignmask + 1);
282         u8 *iv;
283
284         size += alignmask & ~(crypto_tfm_ctx_alignment() - 1);
285         walk->buffer = kmalloc(size, GFP_ATOMIC);
286         if (!walk->buffer)
287                 return -ENOMEM;
288
289         iv = (u8 *)ALIGN((unsigned long)walk->buffer, alignmask + 1);
290         iv = blkcipher_get_spot(iv, bs) + bs;
291         iv = blkcipher_get_spot(iv, bs) + bs;
292         iv = blkcipher_get_spot(iv, ivsize);
293
294         walk->iv = memcpy(iv, walk->iv, ivsize);
295         return 0;
296 }
297
298 int blkcipher_walk_virt(struct blkcipher_desc *desc,
299                         struct blkcipher_walk *walk)
300 {
301         walk->flags &= ~BLKCIPHER_WALK_PHYS;
302         return blkcipher_walk_first(desc, walk);
303 }
304 EXPORT_SYMBOL_GPL(blkcipher_walk_virt);
305
306 int blkcipher_walk_phys(struct blkcipher_desc *desc,
307                         struct blkcipher_walk *walk)
308 {
309         walk->flags |= BLKCIPHER_WALK_PHYS;
310         return blkcipher_walk_first(desc, walk);
311 }
312 EXPORT_SYMBOL_GPL(blkcipher_walk_phys);
313
314 static int blkcipher_walk_first(struct blkcipher_desc *desc,
315                                 struct blkcipher_walk *walk)
316 {
317         struct crypto_blkcipher *tfm = desc->tfm;
318         unsigned int alignmask = crypto_blkcipher_alignmask(tfm);
319
320         if (WARN_ON_ONCE(in_irq()))
321                 return -EDEADLK;
322
323         walk->nbytes = walk->total;
324         if (unlikely(!walk->total))
325                 return 0;
326
327         walk->buffer = NULL;
328         walk->iv = desc->info;
329         if (unlikely(((unsigned long)walk->iv & alignmask))) {
330                 int err = blkcipher_copy_iv(walk, tfm, alignmask);
331                 if (err)
332                         return err;
333         }
334
335         scatterwalk_start(&walk->in, walk->in.sg);
336         scatterwalk_start(&walk->out, walk->out.sg);
337         walk->page = NULL;
338
339         return blkcipher_walk_next(desc, walk);
340 }
341
342 static int setkey_unaligned(struct crypto_tfm *tfm, const u8 *key, unsigned int keylen)
343 {
344         struct blkcipher_alg *cipher = &tfm->__crt_alg->cra_blkcipher;
345         unsigned long alignmask = crypto_tfm_alg_alignmask(tfm);
346         int ret;
347         u8 *buffer, *alignbuffer;
348         unsigned long absize;
349
350         absize = keylen + alignmask;
351         buffer = kmalloc(absize, GFP_ATOMIC);
352         if (!buffer)
353                 return -ENOMEM;
354
355         alignbuffer = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1);
356         memcpy(alignbuffer, key, keylen);
357         ret = cipher->setkey(tfm, alignbuffer, keylen);
358         memset(alignbuffer, 0, keylen);
359         kfree(buffer);
360         return ret;
361 }
362
363 static int setkey(struct crypto_tfm *tfm, const u8 *key,
364                   unsigned int keylen)
365 {
366         struct blkcipher_alg *cipher = &tfm->__crt_alg->cra_blkcipher;
367         unsigned long alignmask = crypto_tfm_alg_alignmask(tfm);
368
369         if (keylen < cipher->min_keysize || keylen > cipher->max_keysize) {
370                 tfm->crt_flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
371                 return -EINVAL;
372         }
373
374         if ((unsigned long)key & alignmask)
375                 return setkey_unaligned(tfm, key, keylen);
376
377         return cipher->setkey(tfm, key, keylen);
378 }
379
380 static int async_setkey(struct crypto_ablkcipher *tfm, const u8 *key,
381                         unsigned int keylen)
382 {
383         return setkey(crypto_ablkcipher_tfm(tfm), key, keylen);
384 }
385
386 static int async_encrypt(struct ablkcipher_request *req)
387 {
388         struct crypto_tfm *tfm = req->base.tfm;
389         struct blkcipher_alg *alg = &tfm->__crt_alg->cra_blkcipher;
390         struct blkcipher_desc desc = {
391                 .tfm = __crypto_blkcipher_cast(tfm),
392                 .info = req->info,
393                 .flags = req->base.flags,
394         };
395
396
397         return alg->encrypt(&desc, req->dst, req->src, req->nbytes);
398 }
399
400 static int async_decrypt(struct ablkcipher_request *req)
401 {
402         struct crypto_tfm *tfm = req->base.tfm;
403         struct blkcipher_alg *alg = &tfm->__crt_alg->cra_blkcipher;
404         struct blkcipher_desc desc = {
405                 .tfm = __crypto_blkcipher_cast(tfm),
406                 .info = req->info,
407                 .flags = req->base.flags,
408         };
409
410         return alg->decrypt(&desc, req->dst, req->src, req->nbytes);
411 }
412
413 static unsigned int crypto_blkcipher_ctxsize(struct crypto_alg *alg, u32 type,
414                                              u32 mask)
415 {
416         struct blkcipher_alg *cipher = &alg->cra_blkcipher;
417         unsigned int len = alg->cra_ctxsize;
418
419         type ^= CRYPTO_ALG_ASYNC;
420         mask &= CRYPTO_ALG_ASYNC;
421         if ((type & mask) && cipher->ivsize) {
422                 len = ALIGN(len, (unsigned long)alg->cra_alignmask + 1);
423                 len += cipher->ivsize;
424         }
425
426         return len;
427 }
428
429 static int crypto_init_blkcipher_ops_async(struct crypto_tfm *tfm)
430 {
431         struct ablkcipher_tfm *crt = &tfm->crt_ablkcipher;
432         struct blkcipher_alg *alg = &tfm->__crt_alg->cra_blkcipher;
433
434         crt->setkey = async_setkey;
435         crt->encrypt = async_encrypt;
436         crt->decrypt = async_decrypt;
437         crt->ivsize = alg->ivsize;
438
439         return 0;
440 }
441
442 static int crypto_init_blkcipher_ops_sync(struct crypto_tfm *tfm)
443 {
444         struct blkcipher_tfm *crt = &tfm->crt_blkcipher;
445         struct blkcipher_alg *alg = &tfm->__crt_alg->cra_blkcipher;
446         unsigned long align = crypto_tfm_alg_alignmask(tfm) + 1;
447         unsigned long addr;
448
449         crt->setkey = setkey;
450         crt->encrypt = alg->encrypt;
451         crt->decrypt = alg->decrypt;
452
453         addr = (unsigned long)crypto_tfm_ctx(tfm);
454         addr = ALIGN(addr, align);
455         addr += ALIGN(tfm->__crt_alg->cra_ctxsize, align);
456         crt->iv = (void *)addr;
457
458         return 0;
459 }
460
461 static int crypto_init_blkcipher_ops(struct crypto_tfm *tfm, u32 type, u32 mask)
462 {
463         struct blkcipher_alg *alg = &tfm->__crt_alg->cra_blkcipher;
464
465         if (alg->ivsize > PAGE_SIZE / 8)
466                 return -EINVAL;
467
468         type ^= CRYPTO_ALG_ASYNC;
469         mask &= CRYPTO_ALG_ASYNC;
470         if (type & mask)
471                 return crypto_init_blkcipher_ops_sync(tfm);
472         else
473                 return crypto_init_blkcipher_ops_async(tfm);
474 }
475
476 static void crypto_blkcipher_show(struct seq_file *m, struct crypto_alg *alg)
477         __attribute__ ((unused));
478 static void crypto_blkcipher_show(struct seq_file *m, struct crypto_alg *alg)
479 {
480         seq_printf(m, "type         : blkcipher\n");
481         seq_printf(m, "blocksize    : %u\n", alg->cra_blocksize);
482         seq_printf(m, "min keysize  : %u\n", alg->cra_blkcipher.min_keysize);
483         seq_printf(m, "max keysize  : %u\n", alg->cra_blkcipher.max_keysize);
484         seq_printf(m, "ivsize       : %u\n", alg->cra_blkcipher.ivsize);
485 }
486
487 const struct crypto_type crypto_blkcipher_type = {
488         .ctxsize = crypto_blkcipher_ctxsize,
489         .init = crypto_init_blkcipher_ops,
490 #ifdef CONFIG_PROC_FS
491         .show = crypto_blkcipher_show,
492 #endif
493 };
494 EXPORT_SYMBOL_GPL(crypto_blkcipher_type);
495
496 MODULE_LICENSE("GPL");
497 MODULE_DESCRIPTION("Generic block chaining cipher type");