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