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>
23 static const struct crypto_type crypto_shash_type;
25 static int shash_no_setkey(struct crypto_shash *tfm, const u8 *key,
31 static int shash_setkey_unaligned(struct crypto_shash *tfm, const u8 *key,
34 struct shash_alg *shash = crypto_shash_alg(tfm);
35 unsigned long alignmask = crypto_shash_alignmask(tfm);
37 u8 *buffer, *alignbuffer;
40 absize = keylen + (alignmask & ~(CRYPTO_MINALIGN - 1));
41 buffer = kmalloc(absize, GFP_KERNEL);
45 alignbuffer = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1);
46 memcpy(alignbuffer, key, keylen);
47 err = shash->setkey(tfm, alignbuffer, keylen);
48 memset(alignbuffer, 0, keylen);
53 int crypto_shash_setkey(struct crypto_shash *tfm, const u8 *key,
56 struct shash_alg *shash = crypto_shash_alg(tfm);
57 unsigned long alignmask = crypto_shash_alignmask(tfm);
59 if ((unsigned long)key & alignmask)
60 return shash_setkey_unaligned(tfm, key, keylen);
62 return shash->setkey(tfm, key, keylen);
64 EXPORT_SYMBOL_GPL(crypto_shash_setkey);
66 static inline unsigned int shash_align_buffer_size(unsigned len,
69 return len + (mask & ~(__alignof__(u8 __attribute__ ((aligned))) - 1));
72 static int shash_update_unaligned(struct shash_desc *desc, const u8 *data,
75 struct crypto_shash *tfm = desc->tfm;
76 struct shash_alg *shash = crypto_shash_alg(tfm);
77 unsigned long alignmask = crypto_shash_alignmask(tfm);
78 unsigned int unaligned_len = alignmask + 1 -
79 ((unsigned long)data & alignmask);
80 u8 buf[shash_align_buffer_size(unaligned_len, alignmask)]
81 __attribute__ ((aligned));
83 if (unaligned_len > len)
86 memcpy(buf, data, unaligned_len);
88 return shash->update(desc, buf, unaligned_len) ?:
89 shash->update(desc, data + unaligned_len, len - unaligned_len);
92 int crypto_shash_update(struct shash_desc *desc, const u8 *data,
95 struct crypto_shash *tfm = desc->tfm;
96 struct shash_alg *shash = crypto_shash_alg(tfm);
97 unsigned long alignmask = crypto_shash_alignmask(tfm);
99 if ((unsigned long)data & alignmask)
100 return shash_update_unaligned(desc, data, len);
102 return shash->update(desc, data, len);
104 EXPORT_SYMBOL_GPL(crypto_shash_update);
106 static int shash_final_unaligned(struct shash_desc *desc, u8 *out)
108 struct crypto_shash *tfm = desc->tfm;
109 unsigned long alignmask = crypto_shash_alignmask(tfm);
110 struct shash_alg *shash = crypto_shash_alg(tfm);
111 unsigned int ds = crypto_shash_digestsize(tfm);
112 u8 buf[shash_align_buffer_size(ds, alignmask)]
113 __attribute__ ((aligned));
116 err = shash->final(desc, buf);
117 memcpy(out, buf, ds);
121 int crypto_shash_final(struct shash_desc *desc, u8 *out)
123 struct crypto_shash *tfm = desc->tfm;
124 struct shash_alg *shash = crypto_shash_alg(tfm);
125 unsigned long alignmask = crypto_shash_alignmask(tfm);
127 if ((unsigned long)out & alignmask)
128 return shash_final_unaligned(desc, out);
130 return shash->final(desc, out);
132 EXPORT_SYMBOL_GPL(crypto_shash_final);
134 static int shash_finup_unaligned(struct shash_desc *desc, const u8 *data,
135 unsigned int len, u8 *out)
137 return crypto_shash_update(desc, data, len) ?:
138 crypto_shash_final(desc, out);
141 int crypto_shash_finup(struct shash_desc *desc, const u8 *data,
142 unsigned int len, u8 *out)
144 struct crypto_shash *tfm = desc->tfm;
145 struct shash_alg *shash = crypto_shash_alg(tfm);
146 unsigned long alignmask = crypto_shash_alignmask(tfm);
148 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_finup(desc, data, len, out);
162 int crypto_shash_digest(struct shash_desc *desc, const u8 *data,
163 unsigned int len, u8 *out)
165 struct crypto_shash *tfm = desc->tfm;
166 struct shash_alg *shash = crypto_shash_alg(tfm);
167 unsigned long alignmask = crypto_shash_alignmask(tfm);
169 if (((unsigned long)data | (unsigned long)out) & alignmask)
170 return shash_digest_unaligned(desc, data, len, out);
172 return shash->digest(desc, data, len, out);
174 EXPORT_SYMBOL_GPL(crypto_shash_digest);
176 static int shash_no_export(struct shash_desc *desc, void *out)
181 static int shash_no_import(struct shash_desc *desc, const void *in)
186 static int shash_async_setkey(struct crypto_ahash *tfm, const u8 *key,
189 struct crypto_shash **ctx = crypto_ahash_ctx(tfm);
191 return crypto_shash_setkey(*ctx, key, keylen);
194 static int shash_async_init(struct ahash_request *req)
196 struct crypto_shash **ctx = crypto_ahash_ctx(crypto_ahash_reqtfm(req));
197 struct shash_desc *desc = ahash_request_ctx(req);
200 desc->flags = req->base.flags;
202 return crypto_shash_init(desc);
205 int shash_ahash_update(struct ahash_request *req, struct shash_desc *desc)
207 struct crypto_hash_walk walk;
210 for (nbytes = crypto_hash_walk_first(req, &walk); nbytes > 0;
211 nbytes = crypto_hash_walk_done(&walk, nbytes))
212 nbytes = crypto_shash_update(desc, walk.data, nbytes);
216 EXPORT_SYMBOL_GPL(shash_ahash_update);
218 static int shash_async_update(struct ahash_request *req)
220 return shash_ahash_update(req, ahash_request_ctx(req));
223 static int shash_async_final(struct ahash_request *req)
225 return crypto_shash_final(ahash_request_ctx(req), req->result);
228 int shash_ahash_digest(struct ahash_request *req, struct shash_desc *desc)
230 struct scatterlist *sg = req->src;
231 unsigned int offset = sg->offset;
232 unsigned int nbytes = req->nbytes;
235 if (nbytes < min(sg->length, ((unsigned int)(PAGE_SIZE)) - offset)) {
238 data = crypto_kmap(sg_page(sg), 0);
239 err = crypto_shash_digest(desc, data + offset, nbytes,
241 crypto_kunmap(data, 0);
242 crypto_yield(desc->flags);
244 err = crypto_shash_init(desc) ?:
245 shash_ahash_update(req, desc) ?:
246 crypto_shash_final(desc, req->result);
250 EXPORT_SYMBOL_GPL(shash_ahash_digest);
252 static int shash_async_digest(struct ahash_request *req)
254 struct crypto_shash **ctx = crypto_ahash_ctx(crypto_ahash_reqtfm(req));
255 struct shash_desc *desc = ahash_request_ctx(req);
258 desc->flags = req->base.flags;
260 return shash_ahash_digest(req, desc);
263 static void crypto_exit_shash_ops_async(struct crypto_tfm *tfm)
265 struct crypto_shash **ctx = crypto_tfm_ctx(tfm);
267 crypto_free_shash(*ctx);
270 int crypto_init_shash_ops_async(struct crypto_tfm *tfm)
272 struct crypto_alg *calg = tfm->__crt_alg;
273 struct shash_alg *alg = __crypto_shash_alg(calg);
274 struct crypto_ahash *crt = __crypto_ahash_cast(tfm);
275 struct crypto_shash **ctx = crypto_tfm_ctx(tfm);
276 struct crypto_shash *shash;
278 if (!crypto_mod_get(calg))
281 shash = crypto_create_tfm(calg, &crypto_shash_type);
283 crypto_mod_put(calg);
284 return PTR_ERR(shash);
288 tfm->exit = crypto_exit_shash_ops_async;
290 crt->init = shash_async_init;
291 crt->update = shash_async_update;
292 crt->final = shash_async_final;
293 crt->digest = shash_async_digest;
294 crt->setkey = shash_async_setkey;
296 crt->digestsize = alg->digestsize;
297 crt->reqsize = sizeof(struct shash_desc) + crypto_shash_descsize(shash);
302 static int shash_compat_setkey(struct crypto_hash *tfm, const u8 *key,
305 struct shash_desc **descp = crypto_hash_ctx(tfm);
306 struct shash_desc *desc = *descp;
308 return crypto_shash_setkey(desc->tfm, key, keylen);
311 static int shash_compat_init(struct hash_desc *hdesc)
313 struct shash_desc **descp = crypto_hash_ctx(hdesc->tfm);
314 struct shash_desc *desc = *descp;
316 desc->flags = hdesc->flags;
318 return crypto_shash_init(desc);
321 static int shash_compat_update(struct hash_desc *hdesc, struct scatterlist *sg,
324 struct shash_desc **descp = crypto_hash_ctx(hdesc->tfm);
325 struct shash_desc *desc = *descp;
326 struct crypto_hash_walk walk;
329 for (nbytes = crypto_hash_walk_first_compat(hdesc, &walk, sg, len);
330 nbytes > 0; nbytes = crypto_hash_walk_done(&walk, nbytes))
331 nbytes = crypto_shash_update(desc, walk.data, nbytes);
336 static int shash_compat_final(struct hash_desc *hdesc, u8 *out)
338 struct shash_desc **descp = crypto_hash_ctx(hdesc->tfm);
340 return crypto_shash_final(*descp, 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 **descp = crypto_hash_ctx(hdesc->tfm);
351 struct shash_desc *desc = *descp;
354 desc->flags = hdesc->flags;
356 data = crypto_kmap(sg_page(sg), 0);
357 err = crypto_shash_digest(desc, data + offset, nbytes, out);
358 crypto_kunmap(data, 0);
359 crypto_yield(desc->flags);
363 err = shash_compat_init(hdesc);
367 err = shash_compat_update(hdesc, sg, nbytes);
371 err = shash_compat_final(hdesc, out);
377 static void crypto_exit_shash_ops_compat(struct crypto_tfm *tfm)
379 struct shash_desc **descp = crypto_tfm_ctx(tfm);
380 struct shash_desc *desc = *descp;
382 crypto_free_shash(desc->tfm);
386 static int crypto_init_shash_ops_compat(struct crypto_tfm *tfm)
388 struct hash_tfm *crt = &tfm->crt_hash;
389 struct crypto_alg *calg = tfm->__crt_alg;
390 struct shash_alg *alg = __crypto_shash_alg(calg);
391 struct shash_desc **descp = crypto_tfm_ctx(tfm);
392 struct crypto_shash *shash;
393 struct shash_desc *desc;
395 if (!crypto_mod_get(calg))
398 shash = crypto_create_tfm(calg, &crypto_shash_type);
400 crypto_mod_put(calg);
401 return PTR_ERR(shash);
404 desc = kmalloc(sizeof(*desc) + crypto_shash_descsize(shash),
407 crypto_free_shash(shash);
413 tfm->exit = crypto_exit_shash_ops_compat;
415 crt->init = shash_compat_init;
416 crt->update = shash_compat_update;
417 crt->final = shash_compat_final;
418 crt->digest = shash_compat_digest;
419 crt->setkey = shash_compat_setkey;
421 crt->digestsize = alg->digestsize;
426 static int crypto_init_shash_ops(struct crypto_tfm *tfm, u32 type, u32 mask)
428 switch (mask & CRYPTO_ALG_TYPE_MASK) {
429 case CRYPTO_ALG_TYPE_HASH_MASK:
430 return crypto_init_shash_ops_compat(tfm);
436 static unsigned int crypto_shash_ctxsize(struct crypto_alg *alg, u32 type,
439 switch (mask & CRYPTO_ALG_TYPE_MASK) {
440 case CRYPTO_ALG_TYPE_HASH_MASK:
441 return sizeof(struct shash_desc *);
447 static int crypto_shash_init_tfm(struct crypto_tfm *tfm)
449 struct crypto_shash *hash = __crypto_shash_cast(tfm);
451 hash->descsize = crypto_shash_alg(hash)->descsize;
455 static unsigned int crypto_shash_extsize(struct crypto_alg *alg)
457 return alg->cra_ctxsize;
460 static void crypto_shash_show(struct seq_file *m, struct crypto_alg *alg)
461 __attribute__ ((unused));
462 static void crypto_shash_show(struct seq_file *m, struct crypto_alg *alg)
464 struct shash_alg *salg = __crypto_shash_alg(alg);
466 seq_printf(m, "type : shash\n");
467 seq_printf(m, "blocksize : %u\n", alg->cra_blocksize);
468 seq_printf(m, "digestsize : %u\n", salg->digestsize);
471 static const struct crypto_type crypto_shash_type = {
472 .ctxsize = crypto_shash_ctxsize,
473 .extsize = crypto_shash_extsize,
474 .init = crypto_init_shash_ops,
475 .init_tfm = crypto_shash_init_tfm,
476 #ifdef CONFIG_PROC_FS
477 .show = crypto_shash_show,
479 .maskclear = ~CRYPTO_ALG_TYPE_MASK,
480 .maskset = CRYPTO_ALG_TYPE_MASK,
481 .type = CRYPTO_ALG_TYPE_SHASH,
482 .tfmsize = offsetof(struct crypto_shash, base),
485 struct crypto_shash *crypto_alloc_shash(const char *alg_name, u32 type,
488 return crypto_alloc_tfm(alg_name, &crypto_shash_type, type, mask);
490 EXPORT_SYMBOL_GPL(crypto_alloc_shash);
492 static int shash_prepare_alg(struct shash_alg *alg)
494 struct crypto_alg *base = &alg->base;
496 if (alg->digestsize > PAGE_SIZE / 8 ||
497 alg->descsize > PAGE_SIZE / 8 ||
498 alg->statesize > PAGE_SIZE / 8)
501 base->cra_type = &crypto_shash_type;
502 base->cra_flags &= ~CRYPTO_ALG_TYPE_MASK;
503 base->cra_flags |= CRYPTO_ALG_TYPE_SHASH;
506 alg->finup = shash_finup_unaligned;
508 alg->digest = shash_digest_unaligned;
510 alg->import = shash_no_import;
512 alg->export = shash_no_export;
514 alg->setkey = shash_no_setkey;
519 int crypto_register_shash(struct shash_alg *alg)
521 struct crypto_alg *base = &alg->base;
524 err = shash_prepare_alg(alg);
528 return crypto_register_alg(base);
530 EXPORT_SYMBOL_GPL(crypto_register_shash);
532 int crypto_unregister_shash(struct shash_alg *alg)
534 return crypto_unregister_alg(&alg->base);
536 EXPORT_SYMBOL_GPL(crypto_unregister_shash);
538 int shash_register_instance(struct crypto_template *tmpl,
539 struct shash_instance *inst)
543 err = shash_prepare_alg(&inst->alg);
547 return crypto_register_instance(tmpl, shash_crypto_instance(inst));
549 EXPORT_SYMBOL_GPL(shash_register_instance);
551 void shash_free_instance(struct crypto_instance *inst)
553 crypto_drop_spawn(crypto_instance_ctx(inst));
554 kfree(shash_instance(inst));
556 EXPORT_SYMBOL_GPL(shash_free_instance);
558 int crypto_init_shash_spawn(struct crypto_shash_spawn *spawn,
559 struct shash_alg *alg,
560 struct crypto_instance *inst)
562 return crypto_init_spawn2(&spawn->base, &alg->base, inst,
565 EXPORT_SYMBOL_GPL(crypto_init_shash_spawn);
567 struct shash_alg *shash_attr_alg(struct rtattr *rta, u32 type, u32 mask)
569 struct crypto_alg *alg;
571 alg = crypto_attr_alg2(rta, &crypto_shash_type, type, mask);
572 return IS_ERR(alg) ? ERR_CAST(alg) :
573 container_of(alg, struct shash_alg, base);
575 EXPORT_SYMBOL_GPL(shash_attr_alg);
577 MODULE_LICENSE("GPL");
578 MODULE_DESCRIPTION("Synchronous cryptographic hash type");