1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Asynchronous Cryptographic Hash operations.
4 *
5 * This is the asynchronous version of hash.c with notification of
6 * completion via a callback.
7 *
8 * Copyright (c) 2008 Loc Ho <lho@amcc.com>
9 */
10
11 #include <crypto/scatterwalk.h>
12 #include <linux/cryptouser.h>
13 #include <linux/err.h>
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/sched.h>
17 #include <linux/slab.h>
18 #include <linux/seq_file.h>
19 #include <linux/string.h>
20 #include <net/netlink.h>
21
22 #include "hash.h"
23
24 static const struct crypto_type crypto_ahash_type;
25
26 struct ahash_request_priv {
27 crypto_completion_t complete;
28 void *data;
29 u8 *result;
30 u32 flags;
31 void *ubuf[] CRYPTO_MINALIGN_ATTR;
32 };
33
hash_walk_next(struct crypto_hash_walk * walk)34 static int hash_walk_next(struct crypto_hash_walk *walk)
35 {
36 unsigned int alignmask = walk->alignmask;
37 unsigned int offset = walk->offset;
38 unsigned int nbytes = min(walk->entrylen,
39 ((unsigned int)(PAGE_SIZE)) - offset);
40
41 walk->data = kmap_local_page(walk->pg);
42 walk->data += offset;
43
44 if (offset & alignmask) {
45 unsigned int unaligned = alignmask + 1 - (offset & alignmask);
46
47 if (nbytes > unaligned)
48 nbytes = unaligned;
49 }
50
51 walk->entrylen -= nbytes;
52 return nbytes;
53 }
54
hash_walk_new_entry(struct crypto_hash_walk * walk)55 static int hash_walk_new_entry(struct crypto_hash_walk *walk)
56 {
57 struct scatterlist *sg;
58
59 sg = walk->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;
64
65 if (walk->entrylen > walk->total)
66 walk->entrylen = walk->total;
67 walk->total -= walk->entrylen;
68
69 return hash_walk_next(walk);
70 }
71
crypto_hash_walk_done(struct crypto_hash_walk * walk,int err)72 int crypto_hash_walk_done(struct crypto_hash_walk *walk, int err)
73 {
74 unsigned int alignmask = walk->alignmask;
75
76 walk->data -= walk->offset;
77
78 if (walk->entrylen && (walk->offset & alignmask) && !err) {
79 unsigned int nbytes;
80
81 walk->offset = ALIGN(walk->offset, alignmask + 1);
82 nbytes = min(walk->entrylen,
83 (unsigned int)(PAGE_SIZE - walk->offset));
84 if (nbytes) {
85 walk->entrylen -= nbytes;
86 walk->data += walk->offset;
87 return nbytes;
88 }
89 }
90
91 kunmap_local(walk->data);
92 crypto_yield(walk->flags);
93
94 if (err)
95 return err;
96
97 if (walk->entrylen) {
98 walk->offset = 0;
99 walk->pg++;
100 return hash_walk_next(walk);
101 }
102
103 if (!walk->total)
104 return 0;
105
106 walk->sg = sg_next(walk->sg);
107
108 return hash_walk_new_entry(walk);
109 }
110 EXPORT_SYMBOL_GPL(crypto_hash_walk_done);
111
crypto_hash_walk_first(struct ahash_request * req,struct crypto_hash_walk * walk)112 int crypto_hash_walk_first(struct ahash_request *req,
113 struct crypto_hash_walk *walk)
114 {
115 walk->total = req->nbytes;
116
117 if (!walk->total) {
118 walk->entrylen = 0;
119 return 0;
120 }
121
122 walk->alignmask = crypto_ahash_alignmask(crypto_ahash_reqtfm(req));
123 walk->sg = req->src;
124 walk->flags = req->base.flags;
125
126 return hash_walk_new_entry(walk);
127 }
128 EXPORT_SYMBOL_GPL(crypto_hash_walk_first);
129
ahash_setkey_unaligned(struct crypto_ahash * tfm,const u8 * key,unsigned int keylen)130 static int ahash_setkey_unaligned(struct crypto_ahash *tfm, const u8 *key,
131 unsigned int keylen)
132 {
133 unsigned long alignmask = crypto_ahash_alignmask(tfm);
134 int ret;
135 u8 *buffer, *alignbuffer;
136 unsigned long absize;
137
138 absize = keylen + alignmask;
139 buffer = kmalloc(absize, GFP_KERNEL);
140 if (!buffer)
141 return -ENOMEM;
142
143 alignbuffer = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1);
144 memcpy(alignbuffer, key, keylen);
145 ret = tfm->setkey(tfm, alignbuffer, keylen);
146 kfree_sensitive(buffer);
147 return ret;
148 }
149
ahash_nosetkey(struct crypto_ahash * tfm,const u8 * key,unsigned int keylen)150 static int ahash_nosetkey(struct crypto_ahash *tfm, const u8 *key,
151 unsigned int keylen)
152 {
153 return -ENOSYS;
154 }
155
ahash_set_needkey(struct crypto_ahash * tfm)156 static void ahash_set_needkey(struct crypto_ahash *tfm)
157 {
158 const struct hash_alg_common *alg = crypto_hash_alg_common(tfm);
159
160 if (tfm->setkey != ahash_nosetkey &&
161 !(alg->base.cra_flags & CRYPTO_ALG_OPTIONAL_KEY))
162 crypto_ahash_set_flags(tfm, CRYPTO_TFM_NEED_KEY);
163 }
164
crypto_ahash_setkey(struct crypto_ahash * tfm,const u8 * key,unsigned int keylen)165 int crypto_ahash_setkey(struct crypto_ahash *tfm, const u8 *key,
166 unsigned int keylen)
167 {
168 unsigned long alignmask = crypto_ahash_alignmask(tfm);
169 int err;
170
171 if ((unsigned long)key & alignmask)
172 err = ahash_setkey_unaligned(tfm, key, keylen);
173 else
174 err = tfm->setkey(tfm, key, keylen);
175
176 if (unlikely(err)) {
177 ahash_set_needkey(tfm);
178 return err;
179 }
180
181 crypto_ahash_clear_flags(tfm, CRYPTO_TFM_NEED_KEY);
182 return 0;
183 }
184 EXPORT_SYMBOL_GPL(crypto_ahash_setkey);
185
ahash_save_req(struct ahash_request * req,crypto_completion_t cplt,bool has_state)186 static int ahash_save_req(struct ahash_request *req, crypto_completion_t cplt,
187 bool has_state)
188 {
189 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
190 unsigned long alignmask = crypto_ahash_alignmask(tfm);
191 unsigned int ds = crypto_ahash_digestsize(tfm);
192 struct ahash_request *subreq;
193 unsigned int subreq_size;
194 unsigned int reqsize;
195 u8 *result;
196 gfp_t gfp;
197 u32 flags;
198
199 subreq_size = sizeof(*subreq);
200 reqsize = crypto_ahash_reqsize(tfm);
201 reqsize = ALIGN(reqsize, crypto_tfm_ctx_alignment());
202 subreq_size += reqsize;
203 subreq_size += ds;
204 subreq_size += alignmask & ~(crypto_tfm_ctx_alignment() - 1);
205
206 flags = ahash_request_flags(req);
207 gfp = (flags & CRYPTO_TFM_REQ_MAY_SLEEP) ? GFP_KERNEL : GFP_ATOMIC;
208 subreq = kmalloc(subreq_size, gfp);
209 if (!subreq)
210 return -ENOMEM;
211
212 ahash_request_set_tfm(subreq, tfm);
213 ahash_request_set_callback(subreq, flags, cplt, req);
214
215 result = (u8 *)(subreq + 1) + reqsize;
216 result = PTR_ALIGN(result, alignmask + 1);
217
218 ahash_request_set_crypt(subreq, req->src, result, req->nbytes);
219
220 if (has_state) {
221 void *state;
222
223 state = kmalloc(crypto_ahash_statesize(tfm), gfp);
224 if (!state) {
225 kfree(subreq);
226 return -ENOMEM;
227 }
228
229 crypto_ahash_export(req, state);
230 crypto_ahash_import(subreq, state);
231 kfree_sensitive(state);
232 }
233
234 req->priv = subreq;
235
236 return 0;
237 }
238
ahash_restore_req(struct ahash_request * req,int err)239 static void ahash_restore_req(struct ahash_request *req, int err)
240 {
241 struct ahash_request *subreq = req->priv;
242
243 if (!err)
244 memcpy(req->result, subreq->result,
245 crypto_ahash_digestsize(crypto_ahash_reqtfm(req)));
246
247 req->priv = NULL;
248
249 kfree_sensitive(subreq);
250 }
251
ahash_op_unaligned_done(void * data,int err)252 static void ahash_op_unaligned_done(void *data, int err)
253 {
254 struct ahash_request *areq = data;
255
256 if (err == -EINPROGRESS)
257 goto out;
258
259 /* First copy req->result into req->priv.result */
260 ahash_restore_req(areq, err);
261
262 out:
263 /* Complete the ORIGINAL request. */
264 ahash_request_complete(areq, err);
265 }
266
ahash_op_unaligned(struct ahash_request * req,int (* op)(struct ahash_request *),bool has_state)267 static int ahash_op_unaligned(struct ahash_request *req,
268 int (*op)(struct ahash_request *),
269 bool has_state)
270 {
271 int err;
272
273 err = ahash_save_req(req, ahash_op_unaligned_done, has_state);
274 if (err)
275 return err;
276
277 err = op(req->priv);
278 if (err == -EINPROGRESS || err == -EBUSY)
279 return err;
280
281 ahash_restore_req(req, err);
282
283 return err;
284 }
285
crypto_ahash_op(struct ahash_request * req,int (* op)(struct ahash_request *),bool has_state)286 static int crypto_ahash_op(struct ahash_request *req,
287 int (*op)(struct ahash_request *),
288 bool has_state)
289 {
290 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
291 unsigned long alignmask = crypto_ahash_alignmask(tfm);
292 int err;
293
294 if ((unsigned long)req->result & alignmask)
295 err = ahash_op_unaligned(req, op, has_state);
296 else
297 err = op(req);
298
299 return crypto_hash_errstat(crypto_hash_alg_common(tfm), err);
300 }
301
crypto_ahash_final(struct ahash_request * req)302 int crypto_ahash_final(struct ahash_request *req)
303 {
304 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
305 struct hash_alg_common *alg = crypto_hash_alg_common(tfm);
306
307 if (IS_ENABLED(CONFIG_CRYPTO_STATS))
308 atomic64_inc(&hash_get_stat(alg)->hash_cnt);
309
310 return crypto_ahash_op(req, tfm->final, true);
311 }
312 EXPORT_SYMBOL_GPL(crypto_ahash_final);
313
crypto_ahash_finup(struct ahash_request * req)314 int crypto_ahash_finup(struct ahash_request *req)
315 {
316 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
317 struct hash_alg_common *alg = crypto_hash_alg_common(tfm);
318
319 if (IS_ENABLED(CONFIG_CRYPTO_STATS)) {
320 struct crypto_istat_hash *istat = hash_get_stat(alg);
321
322 atomic64_inc(&istat->hash_cnt);
323 atomic64_add(req->nbytes, &istat->hash_tlen);
324 }
325
326 return crypto_ahash_op(req, tfm->finup, true);
327 }
328 EXPORT_SYMBOL_GPL(crypto_ahash_finup);
329
crypto_ahash_digest(struct ahash_request * req)330 int crypto_ahash_digest(struct ahash_request *req)
331 {
332 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
333 struct hash_alg_common *alg = crypto_hash_alg_common(tfm);
334
335 if (IS_ENABLED(CONFIG_CRYPTO_STATS)) {
336 struct crypto_istat_hash *istat = hash_get_stat(alg);
337
338 atomic64_inc(&istat->hash_cnt);
339 atomic64_add(req->nbytes, &istat->hash_tlen);
340 }
341
342 if (crypto_ahash_get_flags(tfm) & CRYPTO_TFM_NEED_KEY)
343 return crypto_hash_errstat(alg, -ENOKEY);
344
345 return crypto_ahash_op(req, tfm->digest, false);
346 }
347 EXPORT_SYMBOL_GPL(crypto_ahash_digest);
348
ahash_def_finup_done2(void * data,int err)349 static void ahash_def_finup_done2(void *data, int err)
350 {
351 struct ahash_request *areq = data;
352
353 if (err == -EINPROGRESS)
354 return;
355
356 ahash_restore_req(areq, err);
357
358 ahash_request_complete(areq, err);
359 }
360
ahash_def_finup_finish1(struct ahash_request * req,int err)361 static int ahash_def_finup_finish1(struct ahash_request *req, int err)
362 {
363 struct ahash_request *subreq = req->priv;
364
365 if (err)
366 goto out;
367
368 subreq->base.complete = ahash_def_finup_done2;
369
370 err = crypto_ahash_reqtfm(req)->final(subreq);
371 if (err == -EINPROGRESS || err == -EBUSY)
372 return err;
373
374 out:
375 ahash_restore_req(req, err);
376 return err;
377 }
378
ahash_def_finup_done1(void * data,int err)379 static void ahash_def_finup_done1(void *data, int err)
380 {
381 struct ahash_request *areq = data;
382 struct ahash_request *subreq;
383
384 if (err == -EINPROGRESS)
385 goto out;
386
387 subreq = areq->priv;
388 subreq->base.flags &= CRYPTO_TFM_REQ_MAY_BACKLOG;
389
390 err = ahash_def_finup_finish1(areq, err);
391 if (err == -EINPROGRESS || err == -EBUSY)
392 return;
393
394 out:
395 ahash_request_complete(areq, err);
396 }
397
ahash_def_finup(struct ahash_request * req)398 static int ahash_def_finup(struct ahash_request *req)
399 {
400 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
401 int err;
402
403 err = ahash_save_req(req, ahash_def_finup_done1, true);
404 if (err)
405 return err;
406
407 err = tfm->update(req->priv);
408 if (err == -EINPROGRESS || err == -EBUSY)
409 return err;
410
411 return ahash_def_finup_finish1(req, err);
412 }
413
crypto_ahash_exit_tfm(struct crypto_tfm * tfm)414 static void crypto_ahash_exit_tfm(struct crypto_tfm *tfm)
415 {
416 struct crypto_ahash *hash = __crypto_ahash_cast(tfm);
417 struct ahash_alg *alg = crypto_ahash_alg(hash);
418
419 alg->exit_tfm(hash);
420 }
421
crypto_ahash_init_tfm(struct crypto_tfm * tfm)422 static int crypto_ahash_init_tfm(struct crypto_tfm *tfm)
423 {
424 struct crypto_ahash *hash = __crypto_ahash_cast(tfm);
425 struct ahash_alg *alg = crypto_ahash_alg(hash);
426
427 hash->setkey = ahash_nosetkey;
428
429 crypto_ahash_set_statesize(hash, alg->halg.statesize);
430 crypto_ahash_set_reqsize(hash, alg->reqsize);
431
432 if (tfm->__crt_alg->cra_type != &crypto_ahash_type)
433 return crypto_init_shash_ops_async(tfm);
434
435 hash->init = alg->init;
436 hash->update = alg->update;
437 hash->final = alg->final;
438 hash->finup = alg->finup ?: ahash_def_finup;
439 hash->digest = alg->digest;
440 hash->export = alg->export;
441 hash->import = alg->import;
442
443 if (alg->setkey) {
444 hash->setkey = alg->setkey;
445 ahash_set_needkey(hash);
446 }
447
448 if (alg->exit_tfm)
449 tfm->exit = crypto_ahash_exit_tfm;
450
451 return alg->init_tfm ? alg->init_tfm(hash) : 0;
452 }
453
crypto_ahash_extsize(struct crypto_alg * alg)454 static unsigned int crypto_ahash_extsize(struct crypto_alg *alg)
455 {
456 if (alg->cra_type != &crypto_ahash_type)
457 return sizeof(struct crypto_shash *);
458
459 return crypto_alg_extsize(alg);
460 }
461
crypto_ahash_free_instance(struct crypto_instance * inst)462 static void crypto_ahash_free_instance(struct crypto_instance *inst)
463 {
464 struct ahash_instance *ahash = ahash_instance(inst);
465
466 ahash->free(ahash);
467 }
468
crypto_ahash_report(struct sk_buff * skb,struct crypto_alg * alg)469 static int __maybe_unused crypto_ahash_report(
470 struct sk_buff *skb, struct crypto_alg *alg)
471 {
472 struct crypto_report_hash rhash;
473
474 memset(&rhash, 0, sizeof(rhash));
475
476 strscpy(rhash.type, "ahash", sizeof(rhash.type));
477
478 rhash.blocksize = alg->cra_blocksize;
479 rhash.digestsize = __crypto_hash_alg_common(alg)->digestsize;
480
481 return nla_put(skb, CRYPTOCFGA_REPORT_HASH, sizeof(rhash), &rhash);
482 }
483
484 static void crypto_ahash_show(struct seq_file *m, struct crypto_alg *alg)
485 __maybe_unused;
crypto_ahash_show(struct seq_file * m,struct crypto_alg * alg)486 static void crypto_ahash_show(struct seq_file *m, struct crypto_alg *alg)
487 {
488 seq_printf(m, "type : ahash\n");
489 seq_printf(m, "async : %s\n", alg->cra_flags & CRYPTO_ALG_ASYNC ?
490 "yes" : "no");
491 seq_printf(m, "blocksize : %u\n", alg->cra_blocksize);
492 seq_printf(m, "digestsize : %u\n",
493 __crypto_hash_alg_common(alg)->digestsize);
494 }
495
crypto_ahash_report_stat(struct sk_buff * skb,struct crypto_alg * alg)496 static int __maybe_unused crypto_ahash_report_stat(
497 struct sk_buff *skb, struct crypto_alg *alg)
498 {
499 return crypto_hash_report_stat(skb, alg, "ahash");
500 }
501
502 static const struct crypto_type crypto_ahash_type = {
503 .extsize = crypto_ahash_extsize,
504 .init_tfm = crypto_ahash_init_tfm,
505 .free = crypto_ahash_free_instance,
506 #ifdef CONFIG_PROC_FS
507 .show = crypto_ahash_show,
508 #endif
509 #if IS_ENABLED(CONFIG_CRYPTO_USER)
510 .report = crypto_ahash_report,
511 #endif
512 #ifdef CONFIG_CRYPTO_STATS
513 .report_stat = crypto_ahash_report_stat,
514 #endif
515 .maskclear = ~CRYPTO_ALG_TYPE_MASK,
516 .maskset = CRYPTO_ALG_TYPE_AHASH_MASK,
517 .type = CRYPTO_ALG_TYPE_AHASH,
518 .tfmsize = offsetof(struct crypto_ahash, base),
519 };
520
crypto_grab_ahash(struct crypto_ahash_spawn * spawn,struct crypto_instance * inst,const char * name,u32 type,u32 mask)521 int crypto_grab_ahash(struct crypto_ahash_spawn *spawn,
522 struct crypto_instance *inst,
523 const char *name, u32 type, u32 mask)
524 {
525 spawn->base.frontend = &crypto_ahash_type;
526 return crypto_grab_spawn(&spawn->base, inst, name, type, mask);
527 }
528 EXPORT_SYMBOL_GPL(crypto_grab_ahash);
529
crypto_alloc_ahash(const char * alg_name,u32 type,u32 mask)530 struct crypto_ahash *crypto_alloc_ahash(const char *alg_name, u32 type,
531 u32 mask)
532 {
533 return crypto_alloc_tfm(alg_name, &crypto_ahash_type, type, mask);
534 }
535 EXPORT_SYMBOL_GPL(crypto_alloc_ahash);
536
crypto_has_ahash(const char * alg_name,u32 type,u32 mask)537 int crypto_has_ahash(const char *alg_name, u32 type, u32 mask)
538 {
539 return crypto_type_has_alg(alg_name, &crypto_ahash_type, type, mask);
540 }
541 EXPORT_SYMBOL_GPL(crypto_has_ahash);
542
crypto_clone_ahash(struct crypto_ahash * hash)543 struct crypto_ahash *crypto_clone_ahash(struct crypto_ahash *hash)
544 {
545 struct hash_alg_common *halg = crypto_hash_alg_common(hash);
546 struct crypto_tfm *tfm = crypto_ahash_tfm(hash);
547 struct crypto_ahash *nhash;
548 struct ahash_alg *alg;
549 int err;
550
551 if (!crypto_hash_alg_has_setkey(halg)) {
552 tfm = crypto_tfm_get(tfm);
553 if (IS_ERR(tfm))
554 return ERR_CAST(tfm);
555
556 return hash;
557 }
558
559 nhash = crypto_clone_tfm(&crypto_ahash_type, tfm);
560
561 if (IS_ERR(nhash))
562 return nhash;
563
564 nhash->init = hash->init;
565 nhash->update = hash->update;
566 nhash->final = hash->final;
567 nhash->finup = hash->finup;
568 nhash->digest = hash->digest;
569 nhash->export = hash->export;
570 nhash->import = hash->import;
571 nhash->setkey = hash->setkey;
572 nhash->reqsize = hash->reqsize;
573 nhash->statesize = hash->statesize;
574
575 if (tfm->__crt_alg->cra_type != &crypto_ahash_type)
576 return crypto_clone_shash_ops_async(nhash, hash);
577
578 err = -ENOSYS;
579 alg = crypto_ahash_alg(hash);
580 if (!alg->clone_tfm)
581 goto out_free_nhash;
582
583 err = alg->clone_tfm(nhash, hash);
584 if (err)
585 goto out_free_nhash;
586
587 return nhash;
588
589 out_free_nhash:
590 crypto_free_ahash(nhash);
591 return ERR_PTR(err);
592 }
593 EXPORT_SYMBOL_GPL(crypto_clone_ahash);
594
ahash_prepare_alg(struct ahash_alg * alg)595 static int ahash_prepare_alg(struct ahash_alg *alg)
596 {
597 struct crypto_alg *base = &alg->halg.base;
598 int err;
599
600 if (alg->halg.statesize == 0)
601 return -EINVAL;
602
603 if (alg->reqsize && alg->reqsize < alg->halg.statesize)
604 return -EINVAL;
605
606 err = hash_prepare_alg(&alg->halg);
607 if (err)
608 return err;
609
610 base->cra_type = &crypto_ahash_type;
611 base->cra_flags |= CRYPTO_ALG_TYPE_AHASH;
612
613 return 0;
614 }
615
crypto_register_ahash(struct ahash_alg * alg)616 int crypto_register_ahash(struct ahash_alg *alg)
617 {
618 struct crypto_alg *base = &alg->halg.base;
619 int err;
620
621 err = ahash_prepare_alg(alg);
622 if (err)
623 return err;
624
625 return crypto_register_alg(base);
626 }
627 EXPORT_SYMBOL_GPL(crypto_register_ahash);
628
crypto_unregister_ahash(struct ahash_alg * alg)629 void crypto_unregister_ahash(struct ahash_alg *alg)
630 {
631 crypto_unregister_alg(&alg->halg.base);
632 }
633 EXPORT_SYMBOL_GPL(crypto_unregister_ahash);
634
crypto_register_ahashes(struct ahash_alg * algs,int count)635 int crypto_register_ahashes(struct ahash_alg *algs, int count)
636 {
637 int i, ret;
638
639 for (i = 0; i < count; i++) {
640 ret = crypto_register_ahash(&algs[i]);
641 if (ret)
642 goto err;
643 }
644
645 return 0;
646
647 err:
648 for (--i; i >= 0; --i)
649 crypto_unregister_ahash(&algs[i]);
650
651 return ret;
652 }
653 EXPORT_SYMBOL_GPL(crypto_register_ahashes);
654
crypto_unregister_ahashes(struct ahash_alg * algs,int count)655 void crypto_unregister_ahashes(struct ahash_alg *algs, int count)
656 {
657 int i;
658
659 for (i = count - 1; i >= 0; --i)
660 crypto_unregister_ahash(&algs[i]);
661 }
662 EXPORT_SYMBOL_GPL(crypto_unregister_ahashes);
663
ahash_register_instance(struct crypto_template * tmpl,struct ahash_instance * inst)664 int ahash_register_instance(struct crypto_template *tmpl,
665 struct ahash_instance *inst)
666 {
667 int err;
668
669 if (WARN_ON(!inst->free))
670 return -EINVAL;
671
672 err = ahash_prepare_alg(&inst->alg);
673 if (err)
674 return err;
675
676 return crypto_register_instance(tmpl, ahash_crypto_instance(inst));
677 }
678 EXPORT_SYMBOL_GPL(ahash_register_instance);
679
crypto_hash_alg_has_setkey(struct hash_alg_common * halg)680 bool crypto_hash_alg_has_setkey(struct hash_alg_common *halg)
681 {
682 struct crypto_alg *alg = &halg->base;
683
684 if (alg->cra_type != &crypto_ahash_type)
685 return crypto_shash_alg_has_setkey(__crypto_shash_alg(alg));
686
687 return __crypto_ahash_alg(alg)->setkey != NULL;
688 }
689 EXPORT_SYMBOL_GPL(crypto_hash_alg_has_setkey);
690
691 MODULE_LICENSE("GPL");
692 MODULE_DESCRIPTION("Asynchronous cryptographic hash type");
693