crypto: shash - Fix alignment in unaligned operations
[safe/jmp/linux-2.6] / crypto / shash.c
1 /*
2  * Synchronous Cryptographic Hash operations.
3  *
4  * Copyright (c) 2008 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/scatterwalk.h>
14 #include <crypto/internal/hash.h>
15 #include <linux/err.h>
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/slab.h>
19 #include <linux/seq_file.h>
20
21 #include "internal.h"
22
23 static const struct crypto_type crypto_shash_type;
24
25 static int shash_no_setkey(struct crypto_shash *tfm, const u8 *key,
26                            unsigned int keylen)
27 {
28         return -ENOSYS;
29 }
30
31 static int shash_setkey_unaligned(struct crypto_shash *tfm, const u8 *key,
32                                   unsigned int keylen)
33 {
34         struct shash_alg *shash = crypto_shash_alg(tfm);
35         unsigned long alignmask = crypto_shash_alignmask(tfm);
36         unsigned long absize;
37         u8 *buffer, *alignbuffer;
38         int err;
39
40         absize = keylen + (alignmask & ~(CRYPTO_MINALIGN - 1));
41         buffer = kmalloc(absize, GFP_KERNEL);
42         if (!buffer)
43                 return -ENOMEM;
44
45         alignbuffer = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1);
46         memcpy(alignbuffer, key, keylen);
47         err = shash->setkey(tfm, alignbuffer, keylen);
48         kzfree(buffer);
49         return err;
50 }
51
52 int crypto_shash_setkey(struct crypto_shash *tfm, const u8 *key,
53                         unsigned int keylen)
54 {
55         struct shash_alg *shash = crypto_shash_alg(tfm);
56         unsigned long alignmask = crypto_shash_alignmask(tfm);
57
58         if ((unsigned long)key & alignmask)
59                 return shash_setkey_unaligned(tfm, key, keylen);
60
61         return shash->setkey(tfm, key, keylen);
62 }
63 EXPORT_SYMBOL_GPL(crypto_shash_setkey);
64
65 static inline unsigned int shash_align_buffer_size(unsigned len,
66                                                    unsigned long mask)
67 {
68         return len + (mask & ~(__alignof__(u8 __attribute__ ((aligned))) - 1));
69 }
70
71 static int shash_update_unaligned(struct shash_desc *desc, const u8 *data,
72                                   unsigned int len)
73 {
74         struct crypto_shash *tfm = desc->tfm;
75         struct shash_alg *shash = crypto_shash_alg(tfm);
76         unsigned long alignmask = crypto_shash_alignmask(tfm);
77         unsigned int unaligned_len = alignmask + 1 -
78                                      ((unsigned long)data & alignmask);
79         u8 ubuf[shash_align_buffer_size(unaligned_len, alignmask)]
80                 __attribute__ ((aligned));
81         u8 *buf = PTR_ALIGN(&ubuf[0], alignmask + 1);
82         int err;
83
84         if (unaligned_len > len)
85                 unaligned_len = len;
86
87         memcpy(buf, data, unaligned_len);
88         err = shash->update(desc, buf, unaligned_len);
89         memset(buf, 0, unaligned_len);
90
91         return err ?:
92                shash->update(desc, data + unaligned_len, len - unaligned_len);
93 }
94
95 int crypto_shash_update(struct shash_desc *desc, const u8 *data,
96                         unsigned int len)
97 {
98         struct crypto_shash *tfm = desc->tfm;
99         struct shash_alg *shash = crypto_shash_alg(tfm);
100         unsigned long alignmask = crypto_shash_alignmask(tfm);
101
102         if ((unsigned long)data & alignmask)
103                 return shash_update_unaligned(desc, data, len);
104
105         return shash->update(desc, data, len);
106 }
107 EXPORT_SYMBOL_GPL(crypto_shash_update);
108
109 static int shash_final_unaligned(struct shash_desc *desc, u8 *out)
110 {
111         struct crypto_shash *tfm = desc->tfm;
112         unsigned long alignmask = crypto_shash_alignmask(tfm);
113         struct shash_alg *shash = crypto_shash_alg(tfm);
114         unsigned int ds = crypto_shash_digestsize(tfm);
115         u8 ubuf[shash_align_buffer_size(ds, alignmask)]
116                 __attribute__ ((aligned));
117         u8 *buf = PTR_ALIGN(&ubuf[0], alignmask + 1);
118         int err;
119
120         err = shash->final(desc, buf);
121         if (err)
122                 goto out;
123
124         memcpy(out, buf, ds);
125
126 out:
127         memset(buf, 0, ds);
128         return err;
129 }
130
131 int crypto_shash_final(struct shash_desc *desc, u8 *out)
132 {
133         struct crypto_shash *tfm = desc->tfm;
134         struct shash_alg *shash = crypto_shash_alg(tfm);
135         unsigned long alignmask = crypto_shash_alignmask(tfm);
136
137         if ((unsigned long)out & alignmask)
138                 return shash_final_unaligned(desc, out);
139
140         return shash->final(desc, out);
141 }
142 EXPORT_SYMBOL_GPL(crypto_shash_final);
143
144 static int shash_finup_unaligned(struct shash_desc *desc, const u8 *data,
145                                  unsigned int len, u8 *out)
146 {
147         return crypto_shash_update(desc, data, len) ?:
148                crypto_shash_final(desc, out);
149 }
150
151 int crypto_shash_finup(struct shash_desc *desc, const u8 *data,
152                        unsigned int len, u8 *out)
153 {
154         struct crypto_shash *tfm = desc->tfm;
155         struct shash_alg *shash = crypto_shash_alg(tfm);
156         unsigned long alignmask = crypto_shash_alignmask(tfm);
157
158         if (((unsigned long)data | (unsigned long)out) & alignmask)
159                 return shash_finup_unaligned(desc, data, len, out);
160
161         return shash->finup(desc, data, len, out);
162 }
163 EXPORT_SYMBOL_GPL(crypto_shash_finup);
164
165 static int shash_digest_unaligned(struct shash_desc *desc, const u8 *data,
166                                   unsigned int len, u8 *out)
167 {
168         return crypto_shash_init(desc) ?:
169                crypto_shash_finup(desc, data, len, out);
170 }
171
172 int crypto_shash_digest(struct shash_desc *desc, const u8 *data,
173                         unsigned int len, u8 *out)
174 {
175         struct crypto_shash *tfm = desc->tfm;
176         struct shash_alg *shash = crypto_shash_alg(tfm);
177         unsigned long alignmask = crypto_shash_alignmask(tfm);
178
179         if (((unsigned long)data | (unsigned long)out) & alignmask)
180                 return shash_digest_unaligned(desc, data, len, out);
181
182         return shash->digest(desc, data, len, out);
183 }
184 EXPORT_SYMBOL_GPL(crypto_shash_digest);
185
186 static int shash_no_export(struct shash_desc *desc, void *out)
187 {
188         return -ENOSYS;
189 }
190
191 static int shash_no_import(struct shash_desc *desc, const void *in)
192 {
193         return -ENOSYS;
194 }
195
196 static int shash_async_setkey(struct crypto_ahash *tfm, const u8 *key,
197                               unsigned int keylen)
198 {
199         struct crypto_shash **ctx = crypto_ahash_ctx(tfm);
200
201         return crypto_shash_setkey(*ctx, key, keylen);
202 }
203
204 static int shash_async_init(struct ahash_request *req)
205 {
206         struct crypto_shash **ctx = crypto_ahash_ctx(crypto_ahash_reqtfm(req));
207         struct shash_desc *desc = ahash_request_ctx(req);
208
209         desc->tfm = *ctx;
210         desc->flags = req->base.flags;
211
212         return crypto_shash_init(desc);
213 }
214
215 int shash_ahash_update(struct ahash_request *req, struct shash_desc *desc)
216 {
217         struct crypto_hash_walk walk;
218         int nbytes;
219
220         for (nbytes = crypto_hash_walk_first(req, &walk); nbytes > 0;
221              nbytes = crypto_hash_walk_done(&walk, nbytes))
222                 nbytes = crypto_shash_update(desc, walk.data, nbytes);
223
224         return nbytes;
225 }
226 EXPORT_SYMBOL_GPL(shash_ahash_update);
227
228 static int shash_async_update(struct ahash_request *req)
229 {
230         return shash_ahash_update(req, ahash_request_ctx(req));
231 }
232
233 static int shash_async_final(struct ahash_request *req)
234 {
235         return crypto_shash_final(ahash_request_ctx(req), req->result);
236 }
237
238 int shash_ahash_digest(struct ahash_request *req, struct shash_desc *desc)
239 {
240         struct scatterlist *sg = req->src;
241         unsigned int offset = sg->offset;
242         unsigned int nbytes = req->nbytes;
243         int err;
244
245         if (nbytes < min(sg->length, ((unsigned int)(PAGE_SIZE)) - offset)) {
246                 void *data;
247
248                 data = crypto_kmap(sg_page(sg), 0);
249                 err = crypto_shash_digest(desc, data + offset, nbytes,
250                                           req->result);
251                 crypto_kunmap(data, 0);
252                 crypto_yield(desc->flags);
253         } else
254                 err = crypto_shash_init(desc) ?:
255                       shash_ahash_update(req, desc) ?:
256                       crypto_shash_final(desc, req->result);
257
258         return err;
259 }
260 EXPORT_SYMBOL_GPL(shash_ahash_digest);
261
262 static int shash_async_digest(struct ahash_request *req)
263 {
264         struct crypto_shash **ctx = crypto_ahash_ctx(crypto_ahash_reqtfm(req));
265         struct shash_desc *desc = ahash_request_ctx(req);
266
267         desc->tfm = *ctx;
268         desc->flags = req->base.flags;
269
270         return shash_ahash_digest(req, desc);
271 }
272
273 static void crypto_exit_shash_ops_async(struct crypto_tfm *tfm)
274 {
275         struct crypto_shash **ctx = crypto_tfm_ctx(tfm);
276
277         crypto_free_shash(*ctx);
278 }
279
280 int crypto_init_shash_ops_async(struct crypto_tfm *tfm)
281 {
282         struct crypto_alg *calg = tfm->__crt_alg;
283         struct crypto_ahash *crt = __crypto_ahash_cast(tfm);
284         struct crypto_shash **ctx = crypto_tfm_ctx(tfm);
285         struct crypto_shash *shash;
286
287         if (!crypto_mod_get(calg))
288                 return -EAGAIN;
289
290         shash = crypto_create_tfm(calg, &crypto_shash_type);
291         if (IS_ERR(shash)) {
292                 crypto_mod_put(calg);
293                 return PTR_ERR(shash);
294         }
295
296         *ctx = shash;
297         tfm->exit = crypto_exit_shash_ops_async;
298
299         crt->init = shash_async_init;
300         crt->update = shash_async_update;
301         crt->final  = shash_async_final;
302         crt->digest = shash_async_digest;
303         crt->setkey = shash_async_setkey;
304
305         crt->reqsize = sizeof(struct shash_desc) + crypto_shash_descsize(shash);
306
307         return 0;
308 }
309
310 static int shash_compat_setkey(struct crypto_hash *tfm, const u8 *key,
311                                unsigned int keylen)
312 {
313         struct shash_desc **descp = crypto_hash_ctx(tfm);
314         struct shash_desc *desc = *descp;
315
316         return crypto_shash_setkey(desc->tfm, key, keylen);
317 }
318
319 static int shash_compat_init(struct hash_desc *hdesc)
320 {
321         struct shash_desc **descp = crypto_hash_ctx(hdesc->tfm);
322         struct shash_desc *desc = *descp;
323
324         desc->flags = hdesc->flags;
325
326         return crypto_shash_init(desc);
327 }
328
329 static int shash_compat_update(struct hash_desc *hdesc, struct scatterlist *sg,
330                                unsigned int len)
331 {
332         struct shash_desc **descp = crypto_hash_ctx(hdesc->tfm);
333         struct shash_desc *desc = *descp;
334         struct crypto_hash_walk walk;
335         int nbytes;
336
337         for (nbytes = crypto_hash_walk_first_compat(hdesc, &walk, sg, len);
338              nbytes > 0; nbytes = crypto_hash_walk_done(&walk, nbytes))
339                 nbytes = crypto_shash_update(desc, walk.data, nbytes);
340
341         return nbytes;
342 }
343
344 static int shash_compat_final(struct hash_desc *hdesc, u8 *out)
345 {
346         struct shash_desc **descp = crypto_hash_ctx(hdesc->tfm);
347
348         return crypto_shash_final(*descp, out);
349 }
350
351 static int shash_compat_digest(struct hash_desc *hdesc, struct scatterlist *sg,
352                                unsigned int nbytes, u8 *out)
353 {
354         unsigned int offset = sg->offset;
355         int err;
356
357         if (nbytes < min(sg->length, ((unsigned int)(PAGE_SIZE)) - offset)) {
358                 struct shash_desc **descp = crypto_hash_ctx(hdesc->tfm);
359                 struct shash_desc *desc = *descp;
360                 void *data;
361
362                 desc->flags = hdesc->flags;
363
364                 data = crypto_kmap(sg_page(sg), 0);
365                 err = crypto_shash_digest(desc, data + offset, nbytes, out);
366                 crypto_kunmap(data, 0);
367                 crypto_yield(desc->flags);
368                 goto out;
369         }
370
371         err = shash_compat_init(hdesc);
372         if (err)
373                 goto out;
374
375         err = shash_compat_update(hdesc, sg, nbytes);
376         if (err)
377                 goto out;
378
379         err = shash_compat_final(hdesc, out);
380
381 out:
382         return err;
383 }
384
385 static void crypto_exit_shash_ops_compat(struct crypto_tfm *tfm)
386 {
387         struct shash_desc **descp = crypto_tfm_ctx(tfm);
388         struct shash_desc *desc = *descp;
389
390         crypto_free_shash(desc->tfm);
391         kzfree(desc);
392 }
393
394 static int crypto_init_shash_ops_compat(struct crypto_tfm *tfm)
395 {
396         struct hash_tfm *crt = &tfm->crt_hash;
397         struct crypto_alg *calg = tfm->__crt_alg;
398         struct shash_alg *alg = __crypto_shash_alg(calg);
399         struct shash_desc **descp = crypto_tfm_ctx(tfm);
400         struct crypto_shash *shash;
401         struct shash_desc *desc;
402
403         if (!crypto_mod_get(calg))
404                 return -EAGAIN;
405
406         shash = crypto_create_tfm(calg, &crypto_shash_type);
407         if (IS_ERR(shash)) {
408                 crypto_mod_put(calg);
409                 return PTR_ERR(shash);
410         }
411
412         desc = kmalloc(sizeof(*desc) + crypto_shash_descsize(shash),
413                        GFP_KERNEL);
414         if (!desc) {
415                 crypto_free_shash(shash);
416                 return -ENOMEM;
417         }
418
419         *descp = desc;
420         desc->tfm = shash;
421         tfm->exit = crypto_exit_shash_ops_compat;
422
423         crt->init = shash_compat_init;
424         crt->update = shash_compat_update;
425         crt->final  = shash_compat_final;
426         crt->digest = shash_compat_digest;
427         crt->setkey = shash_compat_setkey;
428
429         crt->digestsize = alg->digestsize;
430
431         return 0;
432 }
433
434 static int crypto_init_shash_ops(struct crypto_tfm *tfm, u32 type, u32 mask)
435 {
436         switch (mask & CRYPTO_ALG_TYPE_MASK) {
437         case CRYPTO_ALG_TYPE_HASH_MASK:
438                 return crypto_init_shash_ops_compat(tfm);
439         }
440
441         return -EINVAL;
442 }
443
444 static unsigned int crypto_shash_ctxsize(struct crypto_alg *alg, u32 type,
445                                          u32 mask)
446 {
447         switch (mask & CRYPTO_ALG_TYPE_MASK) {
448         case CRYPTO_ALG_TYPE_HASH_MASK:
449                 return sizeof(struct shash_desc *);
450         }
451
452         return 0;
453 }
454
455 static int crypto_shash_init_tfm(struct crypto_tfm *tfm)
456 {
457         struct crypto_shash *hash = __crypto_shash_cast(tfm);
458
459         hash->descsize = crypto_shash_alg(hash)->descsize;
460         return 0;
461 }
462
463 static unsigned int crypto_shash_extsize(struct crypto_alg *alg)
464 {
465         return alg->cra_ctxsize;
466 }
467
468 static void crypto_shash_show(struct seq_file *m, struct crypto_alg *alg)
469         __attribute__ ((unused));
470 static void crypto_shash_show(struct seq_file *m, struct crypto_alg *alg)
471 {
472         struct shash_alg *salg = __crypto_shash_alg(alg);
473
474         seq_printf(m, "type         : shash\n");
475         seq_printf(m, "blocksize    : %u\n", alg->cra_blocksize);
476         seq_printf(m, "digestsize   : %u\n", salg->digestsize);
477 }
478
479 static const struct crypto_type crypto_shash_type = {
480         .ctxsize = crypto_shash_ctxsize,
481         .extsize = crypto_shash_extsize,
482         .init = crypto_init_shash_ops,
483         .init_tfm = crypto_shash_init_tfm,
484 #ifdef CONFIG_PROC_FS
485         .show = crypto_shash_show,
486 #endif
487         .maskclear = ~CRYPTO_ALG_TYPE_MASK,
488         .maskset = CRYPTO_ALG_TYPE_MASK,
489         .type = CRYPTO_ALG_TYPE_SHASH,
490         .tfmsize = offsetof(struct crypto_shash, base),
491 };
492
493 struct crypto_shash *crypto_alloc_shash(const char *alg_name, u32 type,
494                                         u32 mask)
495 {
496         return crypto_alloc_tfm(alg_name, &crypto_shash_type, type, mask);
497 }
498 EXPORT_SYMBOL_GPL(crypto_alloc_shash);
499
500 static int shash_prepare_alg(struct shash_alg *alg)
501 {
502         struct crypto_alg *base = &alg->base;
503
504         if (alg->digestsize > PAGE_SIZE / 8 ||
505             alg->descsize > PAGE_SIZE / 8 ||
506             alg->statesize > PAGE_SIZE / 8)
507                 return -EINVAL;
508
509         base->cra_type = &crypto_shash_type;
510         base->cra_flags &= ~CRYPTO_ALG_TYPE_MASK;
511         base->cra_flags |= CRYPTO_ALG_TYPE_SHASH;
512
513         if (!alg->finup)
514                 alg->finup = shash_finup_unaligned;
515         if (!alg->digest)
516                 alg->digest = shash_digest_unaligned;
517         if (!alg->import)
518                 alg->import = shash_no_import;
519         if (!alg->export)
520                 alg->export = shash_no_export;
521         if (!alg->setkey)
522                 alg->setkey = shash_no_setkey;
523
524         return 0;
525 }
526
527 int crypto_register_shash(struct shash_alg *alg)
528 {
529         struct crypto_alg *base = &alg->base;
530         int err;
531
532         err = shash_prepare_alg(alg);
533         if (err)
534                 return err;
535
536         return crypto_register_alg(base);
537 }
538 EXPORT_SYMBOL_GPL(crypto_register_shash);
539
540 int crypto_unregister_shash(struct shash_alg *alg)
541 {
542         return crypto_unregister_alg(&alg->base);
543 }
544 EXPORT_SYMBOL_GPL(crypto_unregister_shash);
545
546 int shash_register_instance(struct crypto_template *tmpl,
547                             struct shash_instance *inst)
548 {
549         int err;
550
551         err = shash_prepare_alg(&inst->alg);
552         if (err)
553                 return err;
554
555         return crypto_register_instance(tmpl, shash_crypto_instance(inst));
556 }
557 EXPORT_SYMBOL_GPL(shash_register_instance);
558
559 void shash_free_instance(struct crypto_instance *inst)
560 {
561         crypto_drop_spawn(crypto_instance_ctx(inst));
562         kfree(shash_instance(inst));
563 }
564 EXPORT_SYMBOL_GPL(shash_free_instance);
565
566 int crypto_init_shash_spawn(struct crypto_shash_spawn *spawn,
567                             struct shash_alg *alg,
568                             struct crypto_instance *inst)
569 {
570         return crypto_init_spawn2(&spawn->base, &alg->base, inst,
571                                   &crypto_shash_type);
572 }
573 EXPORT_SYMBOL_GPL(crypto_init_shash_spawn);
574
575 struct shash_alg *shash_attr_alg(struct rtattr *rta, u32 type, u32 mask)
576 {
577         struct crypto_alg *alg;
578
579         alg = crypto_attr_alg2(rta, &crypto_shash_type, type, mask);
580         return IS_ERR(alg) ? ERR_CAST(alg) :
581                container_of(alg, struct shash_alg, base);
582 }
583 EXPORT_SYMBOL_GPL(shash_attr_alg);
584
585 MODULE_LICENSE("GPL");
586 MODULE_DESCRIPTION("Synchronous cryptographic hash type");