crypto: ahash - Use GFP_KERNEL in unaligned setkey
[deliverable/linux.git] / crypto / shash.c
CommitLineData
7b5a080b
HX
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
3b2f6df0 13#include <crypto/scatterwalk.h>
7b5a080b
HX
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
3b2f6df0
HX
21#include "internal.h"
22
3f683d61
HX
23static const struct crypto_type crypto_shash_type;
24
57cfe44b
HX
25static int shash_no_setkey(struct crypto_shash *tfm, const u8 *key,
26 unsigned int keylen)
27{
28 return -ENOSYS;
29}
30
7b5a080b
HX
31static 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);
8c32c516 48 kzfree(buffer);
7b5a080b
HX
49 return err;
50}
51
52int 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}
63EXPORT_SYMBOL_GPL(crypto_shash_setkey);
64
65static 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
71static 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);
0e2d3a12 79 u8 ubuf[shash_align_buffer_size(unaligned_len, alignmask)]
7b5a080b 80 __attribute__ ((aligned));
0e2d3a12 81 u8 *buf = PTR_ALIGN(&ubuf[0], alignmask + 1);
8c32c516 82 int err;
7b5a080b 83
f4f68993
YS
84 if (unaligned_len > len)
85 unaligned_len = len;
86
7b5a080b 87 memcpy(buf, data, unaligned_len);
8c32c516
HX
88 err = shash->update(desc, buf, unaligned_len);
89 memset(buf, 0, unaligned_len);
7b5a080b 90
8c32c516 91 return err ?:
7b5a080b
HX
92 shash->update(desc, data + unaligned_len, len - unaligned_len);
93}
94
95int 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}
107EXPORT_SYMBOL_GPL(crypto_shash_update);
108
109static 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);
0e2d3a12 115 u8 ubuf[shash_align_buffer_size(ds, alignmask)]
7b5a080b 116 __attribute__ ((aligned));
0e2d3a12 117 u8 *buf = PTR_ALIGN(&ubuf[0], alignmask + 1);
7b5a080b
HX
118 int err;
119
120 err = shash->final(desc, buf);
8c32c516
HX
121 if (err)
122 goto out;
123
7b5a080b 124 memcpy(out, buf, ds);
8c32c516
HX
125
126out:
127 memset(buf, 0, ds);
7b5a080b
HX
128 return err;
129}
130
131int 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}
142EXPORT_SYMBOL_GPL(crypto_shash_final);
143
144static 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
151int 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
8267adab 158 if (((unsigned long)data | (unsigned long)out) & alignmask)
7b5a080b
HX
159 return shash_finup_unaligned(desc, data, len, out);
160
161 return shash->finup(desc, data, len, out);
162}
163EXPORT_SYMBOL_GPL(crypto_shash_finup);
164
165static int shash_digest_unaligned(struct shash_desc *desc, const u8 *data,
166 unsigned int len, u8 *out)
167{
168 return crypto_shash_init(desc) ?:
f88ad8de 169 crypto_shash_finup(desc, data, len, out);
7b5a080b
HX
170}
171
172int 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
8267adab 179 if (((unsigned long)data | (unsigned long)out) & alignmask)
7b5a080b
HX
180 return shash_digest_unaligned(desc, data, len, out);
181
182 return shash->digest(desc, data, len, out);
183}
184EXPORT_SYMBOL_GPL(crypto_shash_digest);
185
99d27e1c 186static int shash_no_export(struct shash_desc *desc, void *out)
dec8b786 187{
99d27e1c
HX
188 return -ENOSYS;
189}
dec8b786 190
99d27e1c
HX
191static int shash_no_import(struct shash_desc *desc, const void *in)
192{
193 return -ENOSYS;
dec8b786 194}
dec8b786 195
3b2f6df0
HX
196static 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
204static 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
7eddf95e 215int shash_ahash_update(struct ahash_request *req, struct shash_desc *desc)
3b2f6df0 216{
3b2f6df0
HX
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}
7eddf95e
HX
226EXPORT_SYMBOL_GPL(shash_ahash_update);
227
228static int shash_async_update(struct ahash_request *req)
229{
230 return shash_ahash_update(req, ahash_request_ctx(req));
231}
3b2f6df0
HX
232
233static int shash_async_final(struct ahash_request *req)
234{
235 return crypto_shash_final(ahash_request_ctx(req), req->result);
236}
237
7eddf95e 238int shash_ahash_digest(struct ahash_request *req, struct shash_desc *desc)
3b2f6df0
HX
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)) {
3b2f6df0
HX
246 void *data;
247
3b2f6df0
HX
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);
7eddf95e
HX
253 } else
254 err = crypto_shash_init(desc) ?:
255 shash_ahash_update(req, desc) ?:
256 crypto_shash_final(desc, req->result);
3b2f6df0 257
7eddf95e
HX
258 return err;
259}
260EXPORT_SYMBOL_GPL(shash_ahash_digest);
3b2f6df0 261
7eddf95e
HX
262static 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);
3b2f6df0 266
7eddf95e
HX
267 desc->tfm = *ctx;
268 desc->flags = req->base.flags;
3b2f6df0 269
7eddf95e 270 return shash_ahash_digest(req, desc);
3b2f6df0
HX
271}
272
273static 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
88056ec3 280int crypto_init_shash_ops_async(struct crypto_tfm *tfm)
3b2f6df0
HX
281{
282 struct crypto_alg *calg = tfm->__crt_alg;
88056ec3 283 struct crypto_ahash *crt = __crypto_ahash_cast(tfm);
3b2f6df0
HX
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
3f683d61 290 shash = crypto_create_tfm(calg, &crypto_shash_type);
3b2f6df0
HX
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
3b2f6df0
HX
305 crt->reqsize = sizeof(struct shash_desc) + crypto_shash_descsize(shash);
306
307 return 0;
308}
309
5f7082ed
HX
310static int shash_compat_setkey(struct crypto_hash *tfm, const u8 *key,
311 unsigned int keylen)
312{
113adefc
HX
313 struct shash_desc **descp = crypto_hash_ctx(tfm);
314 struct shash_desc *desc = *descp;
5f7082ed
HX
315
316 return crypto_shash_setkey(desc->tfm, key, keylen);
317}
318
319static int shash_compat_init(struct hash_desc *hdesc)
320{
113adefc
HX
321 struct shash_desc **descp = crypto_hash_ctx(hdesc->tfm);
322 struct shash_desc *desc = *descp;
5f7082ed
HX
323
324 desc->flags = hdesc->flags;
325
326 return crypto_shash_init(desc);
327}
328
329static int shash_compat_update(struct hash_desc *hdesc, struct scatterlist *sg,
330 unsigned int len)
331{
113adefc
HX
332 struct shash_desc **descp = crypto_hash_ctx(hdesc->tfm);
333 struct shash_desc *desc = *descp;
5f7082ed
HX
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
344static int shash_compat_final(struct hash_desc *hdesc, u8 *out)
345{
113adefc
HX
346 struct shash_desc **descp = crypto_hash_ctx(hdesc->tfm);
347
348 return crypto_shash_final(*descp, out);
5f7082ed
HX
349}
350
351static 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)) {
113adefc
HX
358 struct shash_desc **descp = crypto_hash_ctx(hdesc->tfm);
359 struct shash_desc *desc = *descp;
5f7082ed
HX
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
381out:
382 return err;
383}
384
385static void crypto_exit_shash_ops_compat(struct crypto_tfm *tfm)
386{
113adefc
HX
387 struct shash_desc **descp = crypto_tfm_ctx(tfm);
388 struct shash_desc *desc = *descp;
5f7082ed
HX
389
390 crypto_free_shash(desc->tfm);
113adefc 391 kzfree(desc);
5f7082ed
HX
392}
393
394static 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);
113adefc 399 struct shash_desc **descp = crypto_tfm_ctx(tfm);
5f7082ed 400 struct crypto_shash *shash;
113adefc 401 struct shash_desc *desc;
5f7082ed 402
4abfd73e
AKR
403 if (!crypto_mod_get(calg))
404 return -EAGAIN;
405
3f683d61 406 shash = crypto_create_tfm(calg, &crypto_shash_type);
4abfd73e
AKR
407 if (IS_ERR(shash)) {
408 crypto_mod_put(calg);
5f7082ed 409 return PTR_ERR(shash);
4abfd73e 410 }
5f7082ed 411
113adefc
HX
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;
5f7082ed
HX
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
3b2f6df0
HX
434static int crypto_init_shash_ops(struct crypto_tfm *tfm, u32 type, u32 mask)
435{
436 switch (mask & CRYPTO_ALG_TYPE_MASK) {
5f7082ed
HX
437 case CRYPTO_ALG_TYPE_HASH_MASK:
438 return crypto_init_shash_ops_compat(tfm);
3b2f6df0
HX
439 }
440
441 return -EINVAL;
442}
443
444static unsigned int crypto_shash_ctxsize(struct crypto_alg *alg, u32 type,
445 u32 mask)
446{
447 switch (mask & CRYPTO_ALG_TYPE_MASK) {
5f7082ed 448 case CRYPTO_ALG_TYPE_HASH_MASK:
113adefc 449 return sizeof(struct shash_desc *);
3b2f6df0
HX
450 }
451
452 return 0;
453}
454
2ca33da1 455static int crypto_shash_init_tfm(struct crypto_tfm *tfm)
7b5a080b 456{
113adefc
HX
457 struct crypto_shash *hash = __crypto_shash_cast(tfm);
458
459 hash->descsize = crypto_shash_alg(hash)->descsize;
7b5a080b
HX
460 return 0;
461}
462
2ca33da1 463static unsigned int crypto_shash_extsize(struct crypto_alg *alg)
7b5a080b
HX
464{
465 return alg->cra_ctxsize;
466}
467
468static void crypto_shash_show(struct seq_file *m, struct crypto_alg *alg)
469 __attribute__ ((unused));
470static 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);
7b5a080b
HX
477}
478
479static const struct crypto_type crypto_shash_type = {
3b2f6df0 480 .ctxsize = crypto_shash_ctxsize,
7b5a080b 481 .extsize = crypto_shash_extsize,
3b2f6df0 482 .init = crypto_init_shash_ops,
7b5a080b
HX
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
493struct crypto_shash *crypto_alloc_shash(const char *alg_name, u32 type,
494 u32 mask)
495{
3f683d61 496 return crypto_alloc_tfm(alg_name, &crypto_shash_type, type, mask);
7b5a080b
HX
497}
498EXPORT_SYMBOL_GPL(crypto_alloc_shash);
499
619a6ebd 500static int shash_prepare_alg(struct shash_alg *alg)
7b5a080b
HX
501{
502 struct crypto_alg *base = &alg->base;
503
504 if (alg->digestsize > PAGE_SIZE / 8 ||
99d27e1c
HX
505 alg->descsize > PAGE_SIZE / 8 ||
506 alg->statesize > PAGE_SIZE / 8)
7b5a080b
HX
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;
99d27e1c 512
8267adab
HX
513 if (!alg->finup)
514 alg->finup = shash_finup_unaligned;
515 if (!alg->digest)
516 alg->digest = shash_digest_unaligned;
99d27e1c
HX
517 if (!alg->import)
518 alg->import = shash_no_import;
519 if (!alg->export)
520 alg->export = shash_no_export;
57cfe44b
HX
521 if (!alg->setkey)
522 alg->setkey = shash_no_setkey;
99d27e1c 523
619a6ebd
HX
524 return 0;
525}
526
527int 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;
7b5a080b
HX
535
536 return crypto_register_alg(base);
537}
538EXPORT_SYMBOL_GPL(crypto_register_shash);
539
540int crypto_unregister_shash(struct shash_alg *alg)
541{
542 return crypto_unregister_alg(&alg->base);
543}
544EXPORT_SYMBOL_GPL(crypto_unregister_shash);
545
619a6ebd
HX
546int 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}
557EXPORT_SYMBOL_GPL(shash_register_instance);
558
2e4fddd8
HX
559void shash_free_instance(struct crypto_instance *inst)
560{
561 crypto_drop_spawn(crypto_instance_ctx(inst));
562 kfree(shash_instance(inst));
563}
564EXPORT_SYMBOL_GPL(shash_free_instance);
565
94296999
HX
566int 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}
573EXPORT_SYMBOL_GPL(crypto_init_shash_spawn);
574
7d6f5640
HX
575struct 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}
583EXPORT_SYMBOL_GPL(shash_attr_alg);
584
7b5a080b
HX
585MODULE_LICENSE("GPL");
586MODULE_DESCRIPTION("Synchronous cryptographic hash type");
This page took 0.078091 seconds and 5 git commands to generate.