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