2 * Synchronous Cryptographic Hash operations.
4 * Copyright (c) 2008 Herbert Xu <herbert@gondor.apana.org.au>
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)
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>
21 static const struct crypto_type crypto_shash_type;
23 static inline struct crypto_shash *__crypto_shash_cast(struct crypto_tfm *tfm)
25 return container_of(tfm, struct crypto_shash, base);
30 static int shash_setkey_unaligned(struct crypto_shash *tfm, const u8 *key,
33 struct shash_alg *shash = crypto_shash_alg(tfm);
34 unsigned long alignmask = crypto_shash_alignmask(tfm);
36 u8 *buffer, *alignbuffer;
39 absize = keylen + (alignmask & ~(CRYPTO_MINALIGN - 1));
40 buffer = kmalloc(absize, GFP_KERNEL);
44 alignbuffer = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1);
45 memcpy(alignbuffer, key, keylen);
46 err = shash->setkey(tfm, alignbuffer, keylen);
47 memset(alignbuffer, 0, keylen);
52 int crypto_shash_setkey(struct crypto_shash *tfm, const u8 *key,
55 struct shash_alg *shash = crypto_shash_alg(tfm);
56 unsigned long alignmask = crypto_shash_alignmask(tfm);
61 if ((unsigned long)key & alignmask)
62 return shash_setkey_unaligned(tfm, key, keylen);
64 return shash->setkey(tfm, key, keylen);
66 EXPORT_SYMBOL_GPL(crypto_shash_setkey);
68 static inline unsigned int shash_align_buffer_size(unsigned len,
71 return len + (mask & ~(__alignof__(u8 __attribute__ ((aligned))) - 1));
74 static int shash_update_unaligned(struct shash_desc *desc, const u8 *data,
77 struct crypto_shash *tfm = desc->tfm;
78 struct shash_alg *shash = crypto_shash_alg(tfm);
79 unsigned long alignmask = crypto_shash_alignmask(tfm);
80 unsigned int unaligned_len = alignmask + 1 -
81 ((unsigned long)data & alignmask);
82 u8 buf[shash_align_buffer_size(unaligned_len, alignmask)]
83 __attribute__ ((aligned));
85 memcpy(buf, data, unaligned_len);
87 return shash->update(desc, buf, unaligned_len) ?:
88 shash->update(desc, data + unaligned_len, len - unaligned_len);
91 int crypto_shash_update(struct shash_desc *desc, const u8 *data,
94 struct crypto_shash *tfm = desc->tfm;
95 struct shash_alg *shash = crypto_shash_alg(tfm);
96 unsigned long alignmask = crypto_shash_alignmask(tfm);
98 if ((unsigned long)data & alignmask)
99 return shash_update_unaligned(desc, data, len);
101 return shash->update(desc, data, len);
103 EXPORT_SYMBOL_GPL(crypto_shash_update);
105 static int shash_final_unaligned(struct shash_desc *desc, u8 *out)
107 struct crypto_shash *tfm = desc->tfm;
108 unsigned long alignmask = crypto_shash_alignmask(tfm);
109 struct shash_alg *shash = crypto_shash_alg(tfm);
110 unsigned int ds = crypto_shash_digestsize(tfm);
111 u8 buf[shash_align_buffer_size(ds, alignmask)]
112 __attribute__ ((aligned));
115 err = shash->final(desc, buf);
116 memcpy(out, buf, ds);
120 int crypto_shash_final(struct shash_desc *desc, u8 *out)
122 struct crypto_shash *tfm = desc->tfm;
123 struct shash_alg *shash = crypto_shash_alg(tfm);
124 unsigned long alignmask = crypto_shash_alignmask(tfm);
126 if ((unsigned long)out & alignmask)
127 return shash_final_unaligned(desc, out);
129 return shash->final(desc, out);
131 EXPORT_SYMBOL_GPL(crypto_shash_final);
133 static int shash_finup_unaligned(struct shash_desc *desc, const u8 *data,
134 unsigned int len, u8 *out)
136 return crypto_shash_update(desc, data, len) ?:
137 crypto_shash_final(desc, out);
140 int crypto_shash_finup(struct shash_desc *desc, const u8 *data,
141 unsigned int len, u8 *out)
143 struct crypto_shash *tfm = desc->tfm;
144 struct shash_alg *shash = crypto_shash_alg(tfm);
145 unsigned long alignmask = crypto_shash_alignmask(tfm);
147 if (((unsigned long)data | (unsigned long)out) & alignmask ||
149 return shash_finup_unaligned(desc, data, len, out);
151 return shash->finup(desc, data, len, out);
153 EXPORT_SYMBOL_GPL(crypto_shash_finup);
155 static int shash_digest_unaligned(struct shash_desc *desc, const u8 *data,
156 unsigned int len, u8 *out)
158 return crypto_shash_init(desc) ?:
159 crypto_shash_update(desc, data, len) ?:
160 crypto_shash_final(desc, out);
163 int crypto_shash_digest(struct shash_desc *desc, const u8 *data,
164 unsigned int len, u8 *out)
166 struct crypto_shash *tfm = desc->tfm;
167 struct shash_alg *shash = crypto_shash_alg(tfm);
168 unsigned long alignmask = crypto_shash_alignmask(tfm);
170 if (((unsigned long)data | (unsigned long)out) & alignmask ||
172 return shash_digest_unaligned(desc, data, len, out);
174 return shash->digest(desc, data, len, out);
176 EXPORT_SYMBOL_GPL(crypto_shash_digest);
178 int crypto_shash_import(struct shash_desc *desc, const u8 *in)
180 struct crypto_shash *tfm = desc->tfm;
181 struct shash_alg *alg = crypto_shash_alg(tfm);
183 memcpy(shash_desc_ctx(desc), in, crypto_shash_descsize(tfm));
190 EXPORT_SYMBOL_GPL(crypto_shash_import);
192 static int shash_async_setkey(struct crypto_ahash *tfm, const u8 *key,
195 struct crypto_shash **ctx = crypto_ahash_ctx(tfm);
197 return crypto_shash_setkey(*ctx, key, keylen);
200 static int shash_async_init(struct ahash_request *req)
202 struct crypto_shash **ctx = crypto_ahash_ctx(crypto_ahash_reqtfm(req));
203 struct shash_desc *desc = ahash_request_ctx(req);
206 desc->flags = req->base.flags;
208 return crypto_shash_init(desc);
211 static int shash_async_update(struct ahash_request *req)
213 struct shash_desc *desc = ahash_request_ctx(req);
214 struct crypto_hash_walk walk;
217 for (nbytes = crypto_hash_walk_first(req, &walk); nbytes > 0;
218 nbytes = crypto_hash_walk_done(&walk, nbytes))
219 nbytes = crypto_shash_update(desc, walk.data, nbytes);
224 static int shash_async_final(struct ahash_request *req)
226 return crypto_shash_final(ahash_request_ctx(req), req->result);
229 static int shash_async_digest(struct ahash_request *req)
231 struct scatterlist *sg = req->src;
232 unsigned int offset = sg->offset;
233 unsigned int nbytes = req->nbytes;
236 if (nbytes < min(sg->length, ((unsigned int)(PAGE_SIZE)) - offset)) {
237 struct crypto_shash **ctx =
238 crypto_ahash_ctx(crypto_ahash_reqtfm(req));
239 struct shash_desc *desc = ahash_request_ctx(req);
243 desc->flags = req->base.flags;
245 data = crypto_kmap(sg_page(sg), 0);
246 err = crypto_shash_digest(desc, data + offset, nbytes,
248 crypto_kunmap(data, 0);
249 crypto_yield(desc->flags);
253 err = shash_async_init(req);
257 err = shash_async_update(req);
261 err = shash_async_final(req);
267 static void crypto_exit_shash_ops_async(struct crypto_tfm *tfm)
269 struct crypto_shash **ctx = crypto_tfm_ctx(tfm);
271 crypto_free_shash(*ctx);
274 static int crypto_init_shash_ops_async(struct crypto_tfm *tfm)
276 struct crypto_alg *calg = tfm->__crt_alg;
277 struct shash_alg *alg = __crypto_shash_alg(calg);
278 struct ahash_tfm *crt = &tfm->crt_ahash;
279 struct crypto_shash **ctx = crypto_tfm_ctx(tfm);
280 struct crypto_shash *shash;
282 if (!crypto_mod_get(calg))
285 shash = __crypto_shash_cast(crypto_create_tfm(
286 calg, &crypto_shash_type));
288 crypto_mod_put(calg);
289 return PTR_ERR(shash);
293 tfm->exit = crypto_exit_shash_ops_async;
295 crt->init = shash_async_init;
296 crt->update = shash_async_update;
297 crt->final = shash_async_final;
298 crt->digest = shash_async_digest;
299 crt->setkey = shash_async_setkey;
301 crt->digestsize = alg->digestsize;
302 crt->reqsize = sizeof(struct shash_desc) + crypto_shash_descsize(shash);
307 static int shash_compat_setkey(struct crypto_hash *tfm, const u8 *key,
310 struct shash_desc *desc = crypto_hash_ctx(tfm);
312 return crypto_shash_setkey(desc->tfm, key, keylen);
315 static int shash_compat_init(struct hash_desc *hdesc)
317 struct shash_desc *desc = crypto_hash_ctx(hdesc->tfm);
319 desc->flags = hdesc->flags;
321 return crypto_shash_init(desc);
324 static int shash_compat_update(struct hash_desc *hdesc, struct scatterlist *sg,
327 struct shash_desc *desc = crypto_hash_ctx(hdesc->tfm);
328 struct crypto_hash_walk walk;
331 for (nbytes = crypto_hash_walk_first_compat(hdesc, &walk, sg, len);
332 nbytes > 0; nbytes = crypto_hash_walk_done(&walk, nbytes))
333 nbytes = crypto_shash_update(desc, walk.data, nbytes);
338 static int shash_compat_final(struct hash_desc *hdesc, u8 *out)
340 return crypto_shash_final(crypto_hash_ctx(hdesc->tfm), out);
343 static int shash_compat_digest(struct hash_desc *hdesc, struct scatterlist *sg,
344 unsigned int nbytes, u8 *out)
346 unsigned int offset = sg->offset;
349 if (nbytes < min(sg->length, ((unsigned int)(PAGE_SIZE)) - offset)) {
350 struct shash_desc *desc = crypto_hash_ctx(hdesc->tfm);
353 desc->flags = hdesc->flags;
355 data = crypto_kmap(sg_page(sg), 0);
356 err = crypto_shash_digest(desc, data + offset, nbytes, out);
357 crypto_kunmap(data, 0);
358 crypto_yield(desc->flags);
362 err = shash_compat_init(hdesc);
366 err = shash_compat_update(hdesc, sg, nbytes);
370 err = shash_compat_final(hdesc, out);
376 static void crypto_exit_shash_ops_compat(struct crypto_tfm *tfm)
378 struct shash_desc *desc= crypto_tfm_ctx(tfm);
380 crypto_free_shash(desc->tfm);
383 static int crypto_init_shash_ops_compat(struct crypto_tfm *tfm)
385 struct hash_tfm *crt = &tfm->crt_hash;
386 struct crypto_alg *calg = tfm->__crt_alg;
387 struct shash_alg *alg = __crypto_shash_alg(calg);
388 struct shash_desc *desc = crypto_tfm_ctx(tfm);
389 struct crypto_shash *shash;
391 if (!crypto_mod_get(calg))
394 shash = __crypto_shash_cast(crypto_create_tfm(
395 calg, &crypto_shash_type));
397 crypto_mod_put(calg);
398 return PTR_ERR(shash);
402 tfm->exit = crypto_exit_shash_ops_compat;
404 crt->init = shash_compat_init;
405 crt->update = shash_compat_update;
406 crt->final = shash_compat_final;
407 crt->digest = shash_compat_digest;
408 crt->setkey = shash_compat_setkey;
410 crt->digestsize = alg->digestsize;
415 static int crypto_init_shash_ops(struct crypto_tfm *tfm, u32 type, u32 mask)
417 switch (mask & CRYPTO_ALG_TYPE_MASK) {
418 case CRYPTO_ALG_TYPE_HASH_MASK:
419 return crypto_init_shash_ops_compat(tfm);
420 case CRYPTO_ALG_TYPE_AHASH_MASK:
421 return crypto_init_shash_ops_async(tfm);
427 static unsigned int crypto_shash_ctxsize(struct crypto_alg *alg, u32 type,
430 struct shash_alg *salg = __crypto_shash_alg(alg);
432 switch (mask & CRYPTO_ALG_TYPE_MASK) {
433 case CRYPTO_ALG_TYPE_HASH_MASK:
434 return sizeof(struct shash_desc) + salg->descsize;
435 case CRYPTO_ALG_TYPE_AHASH_MASK:
436 return sizeof(struct crypto_shash *);
442 static int crypto_shash_init_tfm(struct crypto_tfm *tfm,
443 const struct crypto_type *frontend)
445 if (frontend->type != CRYPTO_ALG_TYPE_SHASH)
450 static unsigned int crypto_shash_extsize(struct crypto_alg *alg,
451 const struct crypto_type *frontend)
453 return alg->cra_ctxsize;
456 static void crypto_shash_show(struct seq_file *m, struct crypto_alg *alg)
457 __attribute__ ((unused));
458 static void crypto_shash_show(struct seq_file *m, struct crypto_alg *alg)
460 struct shash_alg *salg = __crypto_shash_alg(alg);
462 seq_printf(m, "type : shash\n");
463 seq_printf(m, "blocksize : %u\n", alg->cra_blocksize);
464 seq_printf(m, "digestsize : %u\n", salg->digestsize);
465 seq_printf(m, "descsize : %u\n", salg->descsize);
468 static const struct crypto_type crypto_shash_type = {
469 .ctxsize = crypto_shash_ctxsize,
470 .extsize = crypto_shash_extsize,
471 .init = crypto_init_shash_ops,
472 .init_tfm = crypto_shash_init_tfm,
473 #ifdef CONFIG_PROC_FS
474 .show = crypto_shash_show,
476 .maskclear = ~CRYPTO_ALG_TYPE_MASK,
477 .maskset = CRYPTO_ALG_TYPE_MASK,
478 .type = CRYPTO_ALG_TYPE_SHASH,
479 .tfmsize = offsetof(struct crypto_shash, base),
482 struct crypto_shash *crypto_alloc_shash(const char *alg_name, u32 type,
485 return __crypto_shash_cast(
486 crypto_alloc_tfm(alg_name, &crypto_shash_type, type, mask));
488 EXPORT_SYMBOL_GPL(crypto_alloc_shash);
490 int crypto_register_shash(struct shash_alg *alg)
492 struct crypto_alg *base = &alg->base;
494 if (alg->digestsize > PAGE_SIZE / 8 ||
495 alg->descsize > PAGE_SIZE / 8)
498 base->cra_type = &crypto_shash_type;
499 base->cra_flags &= ~CRYPTO_ALG_TYPE_MASK;
500 base->cra_flags |= CRYPTO_ALG_TYPE_SHASH;
502 return crypto_register_alg(base);
504 EXPORT_SYMBOL_GPL(crypto_register_shash);
506 int crypto_unregister_shash(struct shash_alg *alg)
508 return crypto_unregister_alg(&alg->base);
510 EXPORT_SYMBOL_GPL(crypto_unregister_shash);
512 MODULE_LICENSE("GPL");
513 MODULE_DESCRIPTION("Synchronous cryptographic hash type");