1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Asynchronous Cryptographic Hash operations.
5 * This is the implementation of the ahash (asynchronous hash) API. It differs
6 * from shash (synchronous hash) in that ahash supports asynchronous operations,
7 * and it hashes data from scatterlists instead of virtually addressed buffers.
9 * The ahash API provides access to both ahash and shash algorithms. The shash
10 * API only provides access to shash algorithms.
12 * Copyright (c) 2008 Loc Ho <lho@amcc.com>
15 #include <crypto/scatterwalk.h>
16 #include <linux/cryptouser.h>
17 #include <linux/err.h>
18 #include <linux/kernel.h>
19 #include <linux/module.h>
20 #include <linux/sched.h>
21 #include <linux/slab.h>
22 #include <linux/seq_file.h>
23 #include <linux/string.h>
24 #include <net/netlink.h>
28 #define CRYPTO_ALG_TYPE_AHASH_MASK 0x0000000e
30 struct crypto_hash_walk
{
37 unsigned int entrylen
;
40 struct scatterlist
*sg
;
43 static int hash_walk_next(struct crypto_hash_walk
*walk
)
45 unsigned int offset
= walk
->offset
;
46 unsigned int nbytes
= min(walk
->entrylen
,
47 ((unsigned int)(PAGE_SIZE
)) - offset
);
49 walk
->data
= kmap_local_page(walk
->pg
);
51 walk
->entrylen
-= nbytes
;
55 static int hash_walk_new_entry(struct crypto_hash_walk
*walk
)
57 struct scatterlist
*sg
;
60 walk
->offset
= sg
->offset
;
61 walk
->pg
= sg_page(walk
->sg
) + (walk
->offset
>> PAGE_SHIFT
);
62 walk
->offset
= offset_in_page(walk
->offset
);
63 walk
->entrylen
= sg
->length
;
65 if (walk
->entrylen
> walk
->total
)
66 walk
->entrylen
= walk
->total
;
67 walk
->total
-= walk
->entrylen
;
69 return hash_walk_next(walk
);
72 static int crypto_hash_walk_first(struct ahash_request
*req
,
73 struct crypto_hash_walk
*walk
)
75 walk
->total
= req
->nbytes
;
83 walk
->flags
= req
->base
.flags
;
85 return hash_walk_new_entry(walk
);
88 static int crypto_hash_walk_done(struct crypto_hash_walk
*walk
, int err
)
90 walk
->data
-= walk
->offset
;
92 kunmap_local(walk
->data
);
93 crypto_yield(walk
->flags
);
101 return hash_walk_next(walk
);
107 walk
->sg
= sg_next(walk
->sg
);
109 return hash_walk_new_entry(walk
);
112 static inline int crypto_hash_walk_last(struct crypto_hash_walk
*walk
)
114 return !(walk
->entrylen
| walk
->total
);
118 * For an ahash tfm that is using an shash algorithm (instead of an ahash
119 * algorithm), this returns the underlying shash tfm.
121 static inline struct crypto_shash
*ahash_to_shash(struct crypto_ahash
*tfm
)
123 return *(struct crypto_shash
**)crypto_ahash_ctx(tfm
);
126 static inline struct shash_desc
*prepare_shash_desc(struct ahash_request
*req
,
127 struct crypto_ahash
*tfm
)
129 struct shash_desc
*desc
= ahash_request_ctx(req
);
131 desc
->tfm
= ahash_to_shash(tfm
);
135 int shash_ahash_update(struct ahash_request
*req
, struct shash_desc
*desc
)
137 struct crypto_hash_walk walk
;
140 for (nbytes
= crypto_hash_walk_first(req
, &walk
); nbytes
> 0;
141 nbytes
= crypto_hash_walk_done(&walk
, nbytes
))
142 nbytes
= crypto_shash_update(desc
, walk
.data
, nbytes
);
146 EXPORT_SYMBOL_GPL(shash_ahash_update
);
148 int shash_ahash_finup(struct ahash_request
*req
, struct shash_desc
*desc
)
150 struct crypto_hash_walk walk
;
153 nbytes
= crypto_hash_walk_first(req
, &walk
);
155 return crypto_shash_final(desc
, req
->result
);
158 nbytes
= crypto_hash_walk_last(&walk
) ?
159 crypto_shash_finup(desc
, walk
.data
, nbytes
,
161 crypto_shash_update(desc
, walk
.data
, nbytes
);
162 nbytes
= crypto_hash_walk_done(&walk
, nbytes
);
163 } while (nbytes
> 0);
167 EXPORT_SYMBOL_GPL(shash_ahash_finup
);
169 int shash_ahash_digest(struct ahash_request
*req
, struct shash_desc
*desc
)
171 unsigned int nbytes
= req
->nbytes
;
172 struct scatterlist
*sg
;
177 (sg
= req
->src
, offset
= sg
->offset
,
178 nbytes
<= min(sg
->length
, ((unsigned int)(PAGE_SIZE
)) - offset
))) {
181 data
= kmap_local_page(sg_page(sg
));
182 err
= crypto_shash_digest(desc
, data
+ offset
, nbytes
,
186 err
= crypto_shash_init(desc
) ?:
187 shash_ahash_finup(req
, desc
);
191 EXPORT_SYMBOL_GPL(shash_ahash_digest
);
193 static void crypto_exit_ahash_using_shash(struct crypto_tfm
*tfm
)
195 struct crypto_shash
**ctx
= crypto_tfm_ctx(tfm
);
197 crypto_free_shash(*ctx
);
200 static int crypto_init_ahash_using_shash(struct crypto_tfm
*tfm
)
202 struct crypto_alg
*calg
= tfm
->__crt_alg
;
203 struct crypto_ahash
*crt
= __crypto_ahash_cast(tfm
);
204 struct crypto_shash
**ctx
= crypto_tfm_ctx(tfm
);
205 struct crypto_shash
*shash
;
207 if (!crypto_mod_get(calg
))
210 shash
= crypto_create_tfm(calg
, &crypto_shash_type
);
212 crypto_mod_put(calg
);
213 return PTR_ERR(shash
);
216 crt
->using_shash
= true;
218 tfm
->exit
= crypto_exit_ahash_using_shash
;
220 crypto_ahash_set_flags(crt
, crypto_shash_get_flags(shash
) &
221 CRYPTO_TFM_NEED_KEY
);
222 crt
->reqsize
= sizeof(struct shash_desc
) + crypto_shash_descsize(shash
);
227 static int ahash_nosetkey(struct crypto_ahash
*tfm
, const u8
*key
,
233 static void ahash_set_needkey(struct crypto_ahash
*tfm
, struct ahash_alg
*alg
)
235 if (alg
->setkey
!= ahash_nosetkey
&&
236 !(alg
->halg
.base
.cra_flags
& CRYPTO_ALG_OPTIONAL_KEY
))
237 crypto_ahash_set_flags(tfm
, CRYPTO_TFM_NEED_KEY
);
240 int crypto_ahash_setkey(struct crypto_ahash
*tfm
, const u8
*key
,
243 if (likely(tfm
->using_shash
)) {
244 struct crypto_shash
*shash
= ahash_to_shash(tfm
);
247 err
= crypto_shash_setkey(shash
, key
, keylen
);
249 crypto_ahash_set_flags(tfm
,
250 crypto_shash_get_flags(shash
) &
251 CRYPTO_TFM_NEED_KEY
);
255 struct ahash_alg
*alg
= crypto_ahash_alg(tfm
);
258 err
= alg
->setkey(tfm
, key
, keylen
);
260 ahash_set_needkey(tfm
, alg
);
264 crypto_ahash_clear_flags(tfm
, CRYPTO_TFM_NEED_KEY
);
267 EXPORT_SYMBOL_GPL(crypto_ahash_setkey
);
269 int crypto_ahash_init(struct ahash_request
*req
)
271 struct crypto_ahash
*tfm
= crypto_ahash_reqtfm(req
);
273 if (likely(tfm
->using_shash
))
274 return crypto_shash_init(prepare_shash_desc(req
, tfm
));
275 if (crypto_ahash_get_flags(tfm
) & CRYPTO_TFM_NEED_KEY
)
277 return crypto_ahash_alg(tfm
)->init(req
);
279 EXPORT_SYMBOL_GPL(crypto_ahash_init
);
281 static int ahash_save_req(struct ahash_request
*req
, crypto_completion_t cplt
,
284 struct crypto_ahash
*tfm
= crypto_ahash_reqtfm(req
);
285 unsigned int ds
= crypto_ahash_digestsize(tfm
);
286 struct ahash_request
*subreq
;
287 unsigned int subreq_size
;
288 unsigned int reqsize
;
293 subreq_size
= sizeof(*subreq
);
294 reqsize
= crypto_ahash_reqsize(tfm
);
295 reqsize
= ALIGN(reqsize
, crypto_tfm_ctx_alignment());
296 subreq_size
+= reqsize
;
299 flags
= ahash_request_flags(req
);
300 gfp
= (flags
& CRYPTO_TFM_REQ_MAY_SLEEP
) ? GFP_KERNEL
: GFP_ATOMIC
;
301 subreq
= kmalloc(subreq_size
, gfp
);
305 ahash_request_set_tfm(subreq
, tfm
);
306 ahash_request_set_callback(subreq
, flags
, cplt
, req
);
308 result
= (u8
*)(subreq
+ 1) + reqsize
;
310 ahash_request_set_crypt(subreq
, req
->src
, result
, req
->nbytes
);
315 state
= kmalloc(crypto_ahash_statesize(tfm
), gfp
);
321 crypto_ahash_export(req
, state
);
322 crypto_ahash_import(subreq
, state
);
323 kfree_sensitive(state
);
331 static void ahash_restore_req(struct ahash_request
*req
, int err
)
333 struct ahash_request
*subreq
= req
->priv
;
336 memcpy(req
->result
, subreq
->result
,
337 crypto_ahash_digestsize(crypto_ahash_reqtfm(req
)));
341 kfree_sensitive(subreq
);
344 int crypto_ahash_update(struct ahash_request
*req
)
346 struct crypto_ahash
*tfm
= crypto_ahash_reqtfm(req
);
348 if (likely(tfm
->using_shash
))
349 return shash_ahash_update(req
, ahash_request_ctx(req
));
351 return crypto_ahash_alg(tfm
)->update(req
);
353 EXPORT_SYMBOL_GPL(crypto_ahash_update
);
355 int crypto_ahash_final(struct ahash_request
*req
)
357 struct crypto_ahash
*tfm
= crypto_ahash_reqtfm(req
);
359 if (likely(tfm
->using_shash
))
360 return crypto_shash_final(ahash_request_ctx(req
), req
->result
);
362 return crypto_ahash_alg(tfm
)->final(req
);
364 EXPORT_SYMBOL_GPL(crypto_ahash_final
);
366 int crypto_ahash_finup(struct ahash_request
*req
)
368 struct crypto_ahash
*tfm
= crypto_ahash_reqtfm(req
);
370 if (likely(tfm
->using_shash
))
371 return shash_ahash_finup(req
, ahash_request_ctx(req
));
373 return crypto_ahash_alg(tfm
)->finup(req
);
375 EXPORT_SYMBOL_GPL(crypto_ahash_finup
);
377 int crypto_ahash_digest(struct ahash_request
*req
)
379 struct crypto_ahash
*tfm
= crypto_ahash_reqtfm(req
);
381 if (likely(tfm
->using_shash
))
382 return shash_ahash_digest(req
, prepare_shash_desc(req
, tfm
));
384 if (crypto_ahash_get_flags(tfm
) & CRYPTO_TFM_NEED_KEY
)
387 return crypto_ahash_alg(tfm
)->digest(req
);
389 EXPORT_SYMBOL_GPL(crypto_ahash_digest
);
391 static void ahash_def_finup_done2(void *data
, int err
)
393 struct ahash_request
*areq
= data
;
395 if (err
== -EINPROGRESS
)
398 ahash_restore_req(areq
, err
);
400 ahash_request_complete(areq
, err
);
403 static int ahash_def_finup_finish1(struct ahash_request
*req
, int err
)
405 struct ahash_request
*subreq
= req
->priv
;
410 subreq
->base
.complete
= ahash_def_finup_done2
;
412 err
= crypto_ahash_alg(crypto_ahash_reqtfm(req
))->final(subreq
);
413 if (err
== -EINPROGRESS
|| err
== -EBUSY
)
417 ahash_restore_req(req
, err
);
421 static void ahash_def_finup_done1(void *data
, int err
)
423 struct ahash_request
*areq
= data
;
424 struct ahash_request
*subreq
;
426 if (err
== -EINPROGRESS
)
430 subreq
->base
.flags
&= CRYPTO_TFM_REQ_MAY_BACKLOG
;
432 err
= ahash_def_finup_finish1(areq
, err
);
433 if (err
== -EINPROGRESS
|| err
== -EBUSY
)
437 ahash_request_complete(areq
, err
);
440 static int ahash_def_finup(struct ahash_request
*req
)
442 struct crypto_ahash
*tfm
= crypto_ahash_reqtfm(req
);
445 err
= ahash_save_req(req
, ahash_def_finup_done1
, true);
449 err
= crypto_ahash_alg(tfm
)->update(req
->priv
);
450 if (err
== -EINPROGRESS
|| err
== -EBUSY
)
453 return ahash_def_finup_finish1(req
, err
);
456 int crypto_ahash_export(struct ahash_request
*req
, void *out
)
458 struct crypto_ahash
*tfm
= crypto_ahash_reqtfm(req
);
460 if (likely(tfm
->using_shash
))
461 return crypto_shash_export(ahash_request_ctx(req
), out
);
462 return crypto_ahash_alg(tfm
)->export(req
, out
);
464 EXPORT_SYMBOL_GPL(crypto_ahash_export
);
466 int crypto_ahash_import(struct ahash_request
*req
, const void *in
)
468 struct crypto_ahash
*tfm
= crypto_ahash_reqtfm(req
);
470 if (likely(tfm
->using_shash
))
471 return crypto_shash_import(prepare_shash_desc(req
, tfm
), in
);
472 if (crypto_ahash_get_flags(tfm
) & CRYPTO_TFM_NEED_KEY
)
474 return crypto_ahash_alg(tfm
)->import(req
, in
);
476 EXPORT_SYMBOL_GPL(crypto_ahash_import
);
478 static void crypto_ahash_exit_tfm(struct crypto_tfm
*tfm
)
480 struct crypto_ahash
*hash
= __crypto_ahash_cast(tfm
);
481 struct ahash_alg
*alg
= crypto_ahash_alg(hash
);
486 static int crypto_ahash_init_tfm(struct crypto_tfm
*tfm
)
488 struct crypto_ahash
*hash
= __crypto_ahash_cast(tfm
);
489 struct ahash_alg
*alg
= crypto_ahash_alg(hash
);
491 crypto_ahash_set_statesize(hash
, alg
->halg
.statesize
);
493 if (tfm
->__crt_alg
->cra_type
== &crypto_shash_type
)
494 return crypto_init_ahash_using_shash(tfm
);
496 ahash_set_needkey(hash
, alg
);
499 tfm
->exit
= crypto_ahash_exit_tfm
;
501 return alg
->init_tfm
? alg
->init_tfm(hash
) : 0;
504 static unsigned int crypto_ahash_extsize(struct crypto_alg
*alg
)
506 if (alg
->cra_type
== &crypto_shash_type
)
507 return sizeof(struct crypto_shash
*);
509 return crypto_alg_extsize(alg
);
512 static void crypto_ahash_free_instance(struct crypto_instance
*inst
)
514 struct ahash_instance
*ahash
= ahash_instance(inst
);
519 static int __maybe_unused
crypto_ahash_report(
520 struct sk_buff
*skb
, struct crypto_alg
*alg
)
522 struct crypto_report_hash rhash
;
524 memset(&rhash
, 0, sizeof(rhash
));
526 strscpy(rhash
.type
, "ahash", sizeof(rhash
.type
));
528 rhash
.blocksize
= alg
->cra_blocksize
;
529 rhash
.digestsize
= __crypto_hash_alg_common(alg
)->digestsize
;
531 return nla_put(skb
, CRYPTOCFGA_REPORT_HASH
, sizeof(rhash
), &rhash
);
534 static void crypto_ahash_show(struct seq_file
*m
, struct crypto_alg
*alg
)
536 static void crypto_ahash_show(struct seq_file
*m
, struct crypto_alg
*alg
)
538 seq_printf(m
, "type : ahash\n");
539 seq_printf(m
, "async : %s\n", alg
->cra_flags
& CRYPTO_ALG_ASYNC
?
541 seq_printf(m
, "blocksize : %u\n", alg
->cra_blocksize
);
542 seq_printf(m
, "digestsize : %u\n",
543 __crypto_hash_alg_common(alg
)->digestsize
);
546 static const struct crypto_type crypto_ahash_type
= {
547 .extsize
= crypto_ahash_extsize
,
548 .init_tfm
= crypto_ahash_init_tfm
,
549 .free
= crypto_ahash_free_instance
,
550 #ifdef CONFIG_PROC_FS
551 .show
= crypto_ahash_show
,
553 #if IS_ENABLED(CONFIG_CRYPTO_USER)
554 .report
= crypto_ahash_report
,
556 .maskclear
= ~CRYPTO_ALG_TYPE_MASK
,
557 .maskset
= CRYPTO_ALG_TYPE_AHASH_MASK
,
558 .type
= CRYPTO_ALG_TYPE_AHASH
,
559 .tfmsize
= offsetof(struct crypto_ahash
, base
),
562 int crypto_grab_ahash(struct crypto_ahash_spawn
*spawn
,
563 struct crypto_instance
*inst
,
564 const char *name
, u32 type
, u32 mask
)
566 spawn
->base
.frontend
= &crypto_ahash_type
;
567 return crypto_grab_spawn(&spawn
->base
, inst
, name
, type
, mask
);
569 EXPORT_SYMBOL_GPL(crypto_grab_ahash
);
571 struct crypto_ahash
*crypto_alloc_ahash(const char *alg_name
, u32 type
,
574 return crypto_alloc_tfm(alg_name
, &crypto_ahash_type
, type
, mask
);
576 EXPORT_SYMBOL_GPL(crypto_alloc_ahash
);
578 int crypto_has_ahash(const char *alg_name
, u32 type
, u32 mask
)
580 return crypto_type_has_alg(alg_name
, &crypto_ahash_type
, type
, mask
);
582 EXPORT_SYMBOL_GPL(crypto_has_ahash
);
584 static bool crypto_hash_alg_has_setkey(struct hash_alg_common
*halg
)
586 struct crypto_alg
*alg
= &halg
->base
;
588 if (alg
->cra_type
== &crypto_shash_type
)
589 return crypto_shash_alg_has_setkey(__crypto_shash_alg(alg
));
591 return __crypto_ahash_alg(alg
)->setkey
!= ahash_nosetkey
;
594 struct crypto_ahash
*crypto_clone_ahash(struct crypto_ahash
*hash
)
596 struct hash_alg_common
*halg
= crypto_hash_alg_common(hash
);
597 struct crypto_tfm
*tfm
= crypto_ahash_tfm(hash
);
598 struct crypto_ahash
*nhash
;
599 struct ahash_alg
*alg
;
602 if (!crypto_hash_alg_has_setkey(halg
)) {
603 tfm
= crypto_tfm_get(tfm
);
605 return ERR_CAST(tfm
);
610 nhash
= crypto_clone_tfm(&crypto_ahash_type
, tfm
);
615 nhash
->reqsize
= hash
->reqsize
;
616 nhash
->statesize
= hash
->statesize
;
618 if (likely(hash
->using_shash
)) {
619 struct crypto_shash
**nctx
= crypto_ahash_ctx(nhash
);
620 struct crypto_shash
*shash
;
622 shash
= crypto_clone_shash(ahash_to_shash(hash
));
624 err
= PTR_ERR(shash
);
627 nhash
->using_shash
= true;
633 alg
= crypto_ahash_alg(hash
);
637 err
= alg
->clone_tfm(nhash
, hash
);
644 crypto_free_ahash(nhash
);
647 EXPORT_SYMBOL_GPL(crypto_clone_ahash
);
649 static int ahash_prepare_alg(struct ahash_alg
*alg
)
651 struct crypto_alg
*base
= &alg
->halg
.base
;
654 if (alg
->halg
.statesize
== 0)
657 err
= hash_prepare_alg(&alg
->halg
);
661 base
->cra_type
= &crypto_ahash_type
;
662 base
->cra_flags
|= CRYPTO_ALG_TYPE_AHASH
;
665 alg
->finup
= ahash_def_finup
;
667 alg
->setkey
= ahash_nosetkey
;
672 int crypto_register_ahash(struct ahash_alg
*alg
)
674 struct crypto_alg
*base
= &alg
->halg
.base
;
677 err
= ahash_prepare_alg(alg
);
681 return crypto_register_alg(base
);
683 EXPORT_SYMBOL_GPL(crypto_register_ahash
);
685 void crypto_unregister_ahash(struct ahash_alg
*alg
)
687 crypto_unregister_alg(&alg
->halg
.base
);
689 EXPORT_SYMBOL_GPL(crypto_unregister_ahash
);
691 int crypto_register_ahashes(struct ahash_alg
*algs
, int count
)
695 for (i
= 0; i
< count
; i
++) {
696 ret
= crypto_register_ahash(&algs
[i
]);
704 for (--i
; i
>= 0; --i
)
705 crypto_unregister_ahash(&algs
[i
]);
709 EXPORT_SYMBOL_GPL(crypto_register_ahashes
);
711 void crypto_unregister_ahashes(struct ahash_alg
*algs
, int count
)
715 for (i
= count
- 1; i
>= 0; --i
)
716 crypto_unregister_ahash(&algs
[i
]);
718 EXPORT_SYMBOL_GPL(crypto_unregister_ahashes
);
720 int ahash_register_instance(struct crypto_template
*tmpl
,
721 struct ahash_instance
*inst
)
725 if (WARN_ON(!inst
->free
))
728 err
= ahash_prepare_alg(&inst
->alg
);
732 return crypto_register_instance(tmpl
, ahash_crypto_instance(inst
));
734 EXPORT_SYMBOL_GPL(ahash_register_instance
);
736 MODULE_LICENSE("GPL");
737 MODULE_DESCRIPTION("Asynchronous cryptographic hash type");