2 * Asynchronous Cryptographic Hash operations.
4 * This is the asynchronous version of hash.c with notification of
5 * completion via a callback.
7 * Copyright (c) 2008 Loc Ho <lho@amcc.com>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the Free
11 * Software Foundation; either version 2 of the License, or (at your option)
16 #include <crypto/internal/hash.h>
17 #include <crypto/scatterwalk.h>
18 #include <linux/err.h>
19 #include <linux/kernel.h>
20 #include <linux/module.h>
21 #include <linux/sched.h>
22 #include <linux/slab.h>
23 #include <linux/seq_file.h>
24 #include <linux/cryptouser.h>
25 #include <net/netlink.h>
29 struct ahash_request_priv {
30 crypto_completion_t complete;
34 void *ubuf[] CRYPTO_MINALIGN_ATTR;
37 static inline struct ahash_alg *crypto_ahash_alg(struct crypto_ahash *hash)
39 return container_of(crypto_hash_alg_common(hash), struct ahash_alg,
43 static int hash_walk_next(struct crypto_hash_walk *walk)
45 unsigned int alignmask = walk->alignmask;
46 unsigned int offset = walk->offset;
47 unsigned int nbytes = min(walk->entrylen,
48 ((unsigned int)(PAGE_SIZE)) - offset);
50 walk->data = crypto_kmap(walk->pg, 0);
53 if (offset & alignmask) {
54 unsigned int unaligned = alignmask + 1 - (offset & alignmask);
55 if (nbytes > unaligned)
59 walk->entrylen -= nbytes;
63 static int hash_walk_new_entry(struct crypto_hash_walk *walk)
65 struct scatterlist *sg;
68 walk->offset = sg->offset;
69 walk->pg = sg_page(walk->sg) + (walk->offset >> PAGE_SHIFT);
70 walk->offset = offset_in_page(walk->offset);
71 walk->entrylen = sg->length;
73 if (walk->entrylen > walk->total)
74 walk->entrylen = walk->total;
75 walk->total -= walk->entrylen;
77 return hash_walk_next(walk);
80 int crypto_hash_walk_done(struct crypto_hash_walk *walk, int err)
82 unsigned int alignmask = walk->alignmask;
83 unsigned int nbytes = walk->entrylen;
85 walk->data -= walk->offset;
87 if (nbytes && walk->offset & alignmask && !err) {
88 walk->offset = ALIGN(walk->offset, alignmask + 1);
89 walk->data += walk->offset;
92 ((unsigned int)(PAGE_SIZE)) - walk->offset);
93 walk->entrylen -= nbytes;
98 crypto_kunmap(walk->data, 0);
99 crypto_yield(walk->flags);
107 return hash_walk_next(walk);
113 walk->sg = scatterwalk_sg_next(walk->sg);
115 return hash_walk_new_entry(walk);
117 EXPORT_SYMBOL_GPL(crypto_hash_walk_done);
119 int crypto_hash_walk_first(struct ahash_request *req,
120 struct crypto_hash_walk *walk)
122 walk->total = req->nbytes;
127 walk->alignmask = crypto_ahash_alignmask(crypto_ahash_reqtfm(req));
129 walk->flags = req->base.flags;
131 return hash_walk_new_entry(walk);
133 EXPORT_SYMBOL_GPL(crypto_hash_walk_first);
135 int crypto_hash_walk_first_compat(struct hash_desc *hdesc,
136 struct crypto_hash_walk *walk,
137 struct scatterlist *sg, unsigned int len)
144 walk->alignmask = crypto_hash_alignmask(hdesc->tfm);
146 walk->flags = hdesc->flags;
148 return hash_walk_new_entry(walk);
151 static int ahash_setkey_unaligned(struct crypto_ahash *tfm, const u8 *key,
154 unsigned long alignmask = crypto_ahash_alignmask(tfm);
156 u8 *buffer, *alignbuffer;
157 unsigned long absize;
159 absize = keylen + alignmask;
160 buffer = kmalloc(absize, GFP_KERNEL);
164 alignbuffer = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1);
165 memcpy(alignbuffer, key, keylen);
166 ret = tfm->setkey(tfm, alignbuffer, keylen);
171 int crypto_ahash_setkey(struct crypto_ahash *tfm, const u8 *key,
174 unsigned long alignmask = crypto_ahash_alignmask(tfm);
177 if ((unsigned long)key & alignmask)
178 err = ahash_setkey_unaligned(tfm, key, keylen);
180 err = tfm->setkey(tfm, key, keylen);
185 crypto_ahash_clear_flags(tfm, CRYPTO_TFM_NEED_KEY);
188 EXPORT_SYMBOL_GPL(crypto_ahash_setkey);
190 static int ahash_nosetkey(struct crypto_ahash *tfm, const u8 *key,
196 static inline unsigned int ahash_align_buffer_size(unsigned len,
199 return len + (mask & ~(crypto_tfm_ctx_alignment() - 1));
202 static int ahash_save_req(struct ahash_request *req, crypto_completion_t cplt)
204 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
205 unsigned long alignmask = crypto_ahash_alignmask(tfm);
206 unsigned int ds = crypto_ahash_digestsize(tfm);
207 struct ahash_request_priv *priv;
209 priv = kmalloc(sizeof(*priv) + ahash_align_buffer_size(ds, alignmask),
210 (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ?
211 GFP_KERNEL : GFP_ATOMIC);
216 * WARNING: Voodoo programming below!
218 * The code below is obscure and hard to understand, thus explanation
219 * is necessary. See include/crypto/hash.h and include/linux/crypto.h
220 * to understand the layout of structures used here!
222 * The code here will replace portions of the ORIGINAL request with
223 * pointers to new code and buffers so the hashing operation can store
224 * the result in aligned buffer. We will call the modified request
225 * an ADJUSTED request.
227 * The newly mangled request will look as such:
230 * .result = ADJUSTED[new aligned buffer]
231 * .base.complete = ADJUSTED[pointer to completion function]
232 * .base.data = ADJUSTED[*req (pointer to self)]
233 * .priv = ADJUSTED[new priv] {
234 * .result = ORIGINAL(result)
235 * .complete = ORIGINAL(base.complete)
236 * .data = ORIGINAL(base.data)
240 priv->result = req->result;
241 priv->complete = req->base.complete;
242 priv->data = req->base.data;
243 priv->flags = req->base.flags;
246 * WARNING: We do not backup req->priv here! The req->priv
247 * is for internal use of the Crypto API and the
248 * user must _NOT_ _EVER_ depend on it's content!
251 req->result = PTR_ALIGN((u8 *)priv->ubuf, alignmask + 1);
252 req->base.complete = cplt;
253 req->base.data = req;
259 static void ahash_restore_req(struct ahash_request *req, int err)
261 struct ahash_request_priv *priv = req->priv;
264 memcpy(priv->result, req->result,
265 crypto_ahash_digestsize(crypto_ahash_reqtfm(req)));
267 /* Restore the original crypto request. */
268 req->result = priv->result;
270 ahash_request_set_callback(req, priv->flags,
271 priv->complete, priv->data);
274 /* Free the req->priv.priv from the ADJUSTED request. */
278 static void ahash_notify_einprogress(struct ahash_request *req)
280 struct ahash_request_priv *priv = req->priv;
281 struct crypto_async_request oreq;
283 oreq.data = priv->data;
285 priv->complete(&oreq, -EINPROGRESS);
288 static void ahash_op_unaligned_done(struct crypto_async_request *req, int err)
290 struct ahash_request *areq = req->data;
292 if (err == -EINPROGRESS) {
293 ahash_notify_einprogress(areq);
298 * Restore the original request, see ahash_op_unaligned() for what
301 * The "struct ahash_request *req" here is in fact the "req.base"
302 * from the ADJUSTED request from ahash_op_unaligned(), thus as it
303 * is a pointer to self, it is also the ADJUSTED "req" .
306 /* First copy req->result into req->priv.result */
307 ahash_restore_req(areq, err);
309 /* Complete the ORIGINAL request. */
310 areq->base.complete(&areq->base, err);
313 static int ahash_op_unaligned(struct ahash_request *req,
314 int (*op)(struct ahash_request *))
318 err = ahash_save_req(req, ahash_op_unaligned_done);
323 if (err == -EINPROGRESS ||
324 (err == -EBUSY && (ahash_request_flags(req) &
325 CRYPTO_TFM_REQ_MAY_BACKLOG)))
328 ahash_restore_req(req, err);
333 static int crypto_ahash_op(struct ahash_request *req,
334 int (*op)(struct ahash_request *))
336 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
337 unsigned long alignmask = crypto_ahash_alignmask(tfm);
339 if ((unsigned long)req->result & alignmask)
340 return ahash_op_unaligned(req, op);
345 int crypto_ahash_final(struct ahash_request *req)
347 return crypto_ahash_op(req, crypto_ahash_reqtfm(req)->final);
349 EXPORT_SYMBOL_GPL(crypto_ahash_final);
351 int crypto_ahash_finup(struct ahash_request *req)
353 return crypto_ahash_op(req, crypto_ahash_reqtfm(req)->finup);
355 EXPORT_SYMBOL_GPL(crypto_ahash_finup);
357 int crypto_ahash_digest(struct ahash_request *req)
359 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
361 if (crypto_ahash_get_flags(tfm) & CRYPTO_TFM_NEED_KEY)
364 return crypto_ahash_op(req, tfm->digest);
366 EXPORT_SYMBOL_GPL(crypto_ahash_digest);
368 static void ahash_def_finup_done2(struct crypto_async_request *req, int err)
370 struct ahash_request *areq = req->data;
372 if (err == -EINPROGRESS)
375 ahash_restore_req(areq, err);
377 areq->base.complete(&areq->base, err);
380 static int ahash_def_finup_finish1(struct ahash_request *req, int err)
385 req->base.complete = ahash_def_finup_done2;
387 err = crypto_ahash_reqtfm(req)->final(req);
388 if (err == -EINPROGRESS ||
389 (err == -EBUSY && (ahash_request_flags(req) &
390 CRYPTO_TFM_REQ_MAY_BACKLOG)))
394 ahash_restore_req(req, err);
398 static void ahash_def_finup_done1(struct crypto_async_request *req, int err)
400 struct ahash_request *areq = req->data;
402 if (err == -EINPROGRESS) {
403 ahash_notify_einprogress(areq);
407 areq->base.flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
409 err = ahash_def_finup_finish1(areq, err);
413 areq->base.complete(&areq->base, err);
416 static int ahash_def_finup(struct ahash_request *req)
418 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
421 err = ahash_save_req(req, ahash_def_finup_done1);
425 err = tfm->update(req);
426 if (err == -EINPROGRESS ||
427 (err == -EBUSY && (ahash_request_flags(req) &
428 CRYPTO_TFM_REQ_MAY_BACKLOG)))
431 return ahash_def_finup_finish1(req, err);
434 static int ahash_no_export(struct ahash_request *req, void *out)
439 static int ahash_no_import(struct ahash_request *req, const void *in)
444 static int crypto_ahash_init_tfm(struct crypto_tfm *tfm)
446 struct crypto_ahash *hash = __crypto_ahash_cast(tfm);
447 struct ahash_alg *alg = crypto_ahash_alg(hash);
449 hash->setkey = ahash_nosetkey;
450 hash->export = ahash_no_export;
451 hash->import = ahash_no_import;
453 if (tfm->__crt_alg->cra_type != &crypto_ahash_type)
454 return crypto_init_shash_ops_async(tfm);
456 hash->init = alg->init;
457 hash->update = alg->update;
458 hash->final = alg->final;
459 hash->finup = alg->finup ?: ahash_def_finup;
460 hash->digest = alg->digest;
463 hash->setkey = alg->setkey;
464 if (!(alg->halg.base.cra_flags & CRYPTO_ALG_OPTIONAL_KEY))
465 crypto_ahash_set_flags(hash, CRYPTO_TFM_NEED_KEY);
468 hash->export = alg->export;
470 hash->import = alg->import;
475 static unsigned int crypto_ahash_extsize(struct crypto_alg *alg)
477 if (alg->cra_type == &crypto_ahash_type)
478 return alg->cra_ctxsize;
480 return sizeof(struct crypto_shash *);
484 static int crypto_ahash_report(struct sk_buff *skb, struct crypto_alg *alg)
486 struct crypto_report_hash rhash;
488 strncpy(rhash.type, "ahash", sizeof(rhash.type));
490 rhash.blocksize = alg->cra_blocksize;
491 rhash.digestsize = __crypto_hash_alg_common(alg)->digestsize;
493 NLA_PUT(skb, CRYPTOCFGA_REPORT_HASH,
494 sizeof(struct crypto_report_hash), &rhash);
502 static int crypto_ahash_report(struct sk_buff *skb, struct crypto_alg *alg)
508 static void crypto_ahash_show(struct seq_file *m, struct crypto_alg *alg)
509 __attribute__ ((unused));
510 static void crypto_ahash_show(struct seq_file *m, struct crypto_alg *alg)
512 seq_printf(m, "type : ahash\n");
513 seq_printf(m, "async : %s\n", alg->cra_flags & CRYPTO_ALG_ASYNC ?
515 seq_printf(m, "blocksize : %u\n", alg->cra_blocksize);
516 seq_printf(m, "digestsize : %u\n",
517 __crypto_hash_alg_common(alg)->digestsize);
520 const struct crypto_type crypto_ahash_type = {
521 .extsize = crypto_ahash_extsize,
522 .init_tfm = crypto_ahash_init_tfm,
523 #ifdef CONFIG_PROC_FS
524 .show = crypto_ahash_show,
526 .report = crypto_ahash_report,
527 .maskclear = ~CRYPTO_ALG_TYPE_MASK,
528 .maskset = CRYPTO_ALG_TYPE_AHASH_MASK,
529 .type = CRYPTO_ALG_TYPE_AHASH,
530 .tfmsize = offsetof(struct crypto_ahash, base),
532 EXPORT_SYMBOL_GPL(crypto_ahash_type);
534 struct crypto_ahash *crypto_alloc_ahash(const char *alg_name, u32 type,
537 return crypto_alloc_tfm(alg_name, &crypto_ahash_type, type, mask);
539 EXPORT_SYMBOL_GPL(crypto_alloc_ahash);
541 static int ahash_prepare_alg(struct ahash_alg *alg)
543 struct crypto_alg *base = &alg->halg.base;
545 if (alg->halg.digestsize > PAGE_SIZE / 8 ||
546 alg->halg.statesize > PAGE_SIZE / 8 ||
547 alg->halg.statesize == 0)
550 base->cra_type = &crypto_ahash_type;
551 base->cra_flags &= ~CRYPTO_ALG_TYPE_MASK;
552 base->cra_flags |= CRYPTO_ALG_TYPE_AHASH;
557 int crypto_register_ahash(struct ahash_alg *alg)
559 struct crypto_alg *base = &alg->halg.base;
562 err = ahash_prepare_alg(alg);
566 return crypto_register_alg(base);
568 EXPORT_SYMBOL_GPL(crypto_register_ahash);
570 int crypto_unregister_ahash(struct ahash_alg *alg)
572 return crypto_unregister_alg(&alg->halg.base);
574 EXPORT_SYMBOL_GPL(crypto_unregister_ahash);
576 int ahash_register_instance(struct crypto_template *tmpl,
577 struct ahash_instance *inst)
581 err = ahash_prepare_alg(&inst->alg);
585 return crypto_register_instance(tmpl, ahash_crypto_instance(inst));
587 EXPORT_SYMBOL_GPL(ahash_register_instance);
589 void ahash_free_instance(struct crypto_instance *inst)
591 crypto_drop_spawn(crypto_instance_ctx(inst));
592 kfree(ahash_instance(inst));
594 EXPORT_SYMBOL_GPL(ahash_free_instance);
596 int crypto_init_ahash_spawn(struct crypto_ahash_spawn *spawn,
597 struct hash_alg_common *alg,
598 struct crypto_instance *inst)
600 return crypto_init_spawn2(&spawn->base, &alg->base, inst,
603 EXPORT_SYMBOL_GPL(crypto_init_ahash_spawn);
605 struct hash_alg_common *ahash_attr_alg(struct rtattr *rta, u32 type, u32 mask)
607 struct crypto_alg *alg;
609 alg = crypto_attr_alg2(rta, &crypto_ahash_type, type, mask);
610 return IS_ERR(alg) ? ERR_CAST(alg) : __crypto_hash_alg_common(alg);
612 EXPORT_SYMBOL_GPL(ahash_attr_alg);
614 bool crypto_hash_alg_has_setkey(struct hash_alg_common *halg)
616 struct crypto_alg *alg = &halg->base;
618 if (alg->cra_type != &crypto_ahash_type)
619 return crypto_shash_alg_has_setkey(__crypto_shash_alg(alg));
621 return __crypto_ahash_alg(alg)->setkey != NULL;
623 EXPORT_SYMBOL_GPL(crypto_hash_alg_has_setkey);
625 MODULE_LICENSE("GPL");
626 MODULE_DESCRIPTION("Asynchronous cryptographic hash type");