hc
2024-12-19 9370bb92b2d16684ee45cf24e879c93c509162da
kernel/crypto/ahash.c
....@@ -1,3 +1,4 @@
1
+// SPDX-License-Identifier: GPL-2.0-or-later
12 /*
23 * Asynchronous Cryptographic Hash operations.
34 *
....@@ -5,17 +6,10 @@
56 * completion via a callback.
67 *
78 * Copyright (c) 2008 Loc Ho <lho@amcc.com>
8
- *
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)
12
- * any later version.
13
- *
149 */
1510
1611 #include <crypto/internal/hash.h>
1712 #include <crypto/scatterwalk.h>
18
-#include <linux/bug.h>
1913 #include <linux/err.h>
2014 #include <linux/kernel.h>
2115 #include <linux/module.h>
....@@ -27,6 +21,8 @@
2721 #include <net/netlink.h>
2822
2923 #include "internal.h"
24
+
25
+static const struct crypto_type crypto_ahash_type;
3026
3127 struct ahash_request_priv {
3228 crypto_completion_t complete;
....@@ -49,10 +45,7 @@
4945 unsigned int nbytes = min(walk->entrylen,
5046 ((unsigned int)(PAGE_SIZE)) - offset);
5147
52
- if (walk->flags & CRYPTO_ALG_ASYNC)
53
- walk->data = kmap(walk->pg);
54
- else
55
- walk->data = kmap_atomic(walk->pg);
48
+ walk->data = kmap_atomic(walk->pg);
5649 walk->data += offset;
5750
5851 if (offset & alignmask) {
....@@ -102,16 +95,8 @@
10295 }
10396 }
10497
105
- if (walk->flags & CRYPTO_ALG_ASYNC)
106
- kunmap(walk->pg);
107
- else {
108
- kunmap_atomic(walk->data);
109
- /*
110
- * The may sleep test only makes sense for sync users.
111
- * Async users don't need to sleep here anyway.
112
- */
113
- crypto_yield(walk->flags);
114
- }
98
+ kunmap_atomic(walk->data);
99
+ crypto_yield(walk->flags);
115100
116101 if (err)
117102 return err;
....@@ -143,32 +128,11 @@
143128
144129 walk->alignmask = crypto_ahash_alignmask(crypto_ahash_reqtfm(req));
145130 walk->sg = req->src;
146
- walk->flags = req->base.flags & CRYPTO_TFM_REQ_MASK;
131
+ walk->flags = req->base.flags;
147132
148133 return hash_walk_new_entry(walk);
149134 }
150135 EXPORT_SYMBOL_GPL(crypto_hash_walk_first);
151
-
152
-int crypto_ahash_walk_first(struct ahash_request *req,
153
- struct crypto_hash_walk *walk)
154
-{
155
- walk->total = req->nbytes;
156
-
157
- if (!walk->total) {
158
- walk->entrylen = 0;
159
- return 0;
160
- }
161
-
162
- walk->alignmask = crypto_ahash_alignmask(crypto_ahash_reqtfm(req));
163
- walk->sg = req->src;
164
- walk->flags = req->base.flags & CRYPTO_TFM_REQ_MASK;
165
- walk->flags |= CRYPTO_ALG_ASYNC;
166
-
167
- BUILD_BUG_ON(CRYPTO_TFM_REQ_MASK & CRYPTO_ALG_ASYNC);
168
-
169
- return hash_walk_new_entry(walk);
170
-}
171
-EXPORT_SYMBOL_GPL(crypto_ahash_walk_first);
172136
173137 static int ahash_setkey_unaligned(struct crypto_ahash *tfm, const u8 *key,
174138 unsigned int keylen)
....@@ -186,7 +150,7 @@
186150 alignbuffer = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1);
187151 memcpy(alignbuffer, key, keylen);
188152 ret = tfm->setkey(tfm, alignbuffer, keylen);
189
- kzfree(buffer);
153
+ kfree_sensitive(buffer);
190154 return ret;
191155 }
192156
....@@ -305,7 +269,7 @@
305269 req->priv = NULL;
306270
307271 /* Free the req->priv.priv from the ADJUSTED request. */
308
- kzfree(priv);
272
+ kfree_sensitive(priv);
309273 }
310274
311275 static void ahash_notify_einprogress(struct ahash_request *req)
....@@ -375,24 +339,46 @@
375339
376340 int crypto_ahash_final(struct ahash_request *req)
377341 {
378
- return crypto_ahash_op(req, crypto_ahash_reqtfm(req)->final);
342
+ struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
343
+ struct crypto_alg *alg = tfm->base.__crt_alg;
344
+ unsigned int nbytes = req->nbytes;
345
+ int ret;
346
+
347
+ crypto_stats_get(alg);
348
+ ret = crypto_ahash_op(req, crypto_ahash_reqtfm(req)->final);
349
+ crypto_stats_ahash_final(nbytes, ret, alg);
350
+ return ret;
379351 }
380352 EXPORT_SYMBOL_GPL(crypto_ahash_final);
381353
382354 int crypto_ahash_finup(struct ahash_request *req)
383355 {
384
- return crypto_ahash_op(req, crypto_ahash_reqtfm(req)->finup);
356
+ struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
357
+ struct crypto_alg *alg = tfm->base.__crt_alg;
358
+ unsigned int nbytes = req->nbytes;
359
+ int ret;
360
+
361
+ crypto_stats_get(alg);
362
+ ret = crypto_ahash_op(req, crypto_ahash_reqtfm(req)->finup);
363
+ crypto_stats_ahash_final(nbytes, ret, alg);
364
+ return ret;
385365 }
386366 EXPORT_SYMBOL_GPL(crypto_ahash_finup);
387367
388368 int crypto_ahash_digest(struct ahash_request *req)
389369 {
390370 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
371
+ struct crypto_alg *alg = tfm->base.__crt_alg;
372
+ unsigned int nbytes = req->nbytes;
373
+ int ret;
391374
375
+ crypto_stats_get(alg);
392376 if (crypto_ahash_get_flags(tfm) & CRYPTO_TFM_NEED_KEY)
393
- return -ENOKEY;
394
-
395
- return crypto_ahash_op(req, tfm->digest);
377
+ ret = -ENOKEY;
378
+ else
379
+ ret = crypto_ahash_op(req, tfm->digest);
380
+ crypto_stats_ahash_final(nbytes, ret, alg);
381
+ return ret;
396382 }
397383 EXPORT_SYMBOL_GPL(crypto_ahash_digest);
398384
....@@ -458,6 +444,14 @@
458444 return ahash_def_finup_finish1(req, err);
459445 }
460446
447
+static void crypto_ahash_exit_tfm(struct crypto_tfm *tfm)
448
+{
449
+ struct crypto_ahash *hash = __crypto_ahash_cast(tfm);
450
+ struct ahash_alg *alg = crypto_ahash_alg(hash);
451
+
452
+ alg->exit_tfm(hash);
453
+}
454
+
461455 static int crypto_ahash_init_tfm(struct crypto_tfm *tfm)
462456 {
463457 struct crypto_ahash *hash = __crypto_ahash_cast(tfm);
....@@ -481,7 +475,10 @@
481475 ahash_set_needkey(hash);
482476 }
483477
484
- return 0;
478
+ if (alg->exit_tfm)
479
+ tfm->exit = crypto_ahash_exit_tfm;
480
+
481
+ return alg->init_tfm ? alg->init_tfm(hash) : 0;
485482 }
486483
487484 static unsigned int crypto_ahash_extsize(struct crypto_alg *alg)
....@@ -492,23 +489,26 @@
492489 return crypto_alg_extsize(alg);
493490 }
494491
492
+static void crypto_ahash_free_instance(struct crypto_instance *inst)
493
+{
494
+ struct ahash_instance *ahash = ahash_instance(inst);
495
+
496
+ ahash->free(ahash);
497
+}
498
+
495499 #ifdef CONFIG_NET
496500 static int crypto_ahash_report(struct sk_buff *skb, struct crypto_alg *alg)
497501 {
498502 struct crypto_report_hash rhash;
499503
500
- strncpy(rhash.type, "ahash", sizeof(rhash.type));
504
+ memset(&rhash, 0, sizeof(rhash));
505
+
506
+ strscpy(rhash.type, "ahash", sizeof(rhash.type));
501507
502508 rhash.blocksize = alg->cra_blocksize;
503509 rhash.digestsize = __crypto_hash_alg_common(alg)->digestsize;
504510
505
- if (nla_put(skb, CRYPTOCFGA_REPORT_HASH,
506
- sizeof(struct crypto_report_hash), &rhash))
507
- goto nla_put_failure;
508
- return 0;
509
-
510
-nla_put_failure:
511
- return -EMSGSIZE;
511
+ return nla_put(skb, CRYPTOCFGA_REPORT_HASH, sizeof(rhash), &rhash);
512512 }
513513 #else
514514 static int crypto_ahash_report(struct sk_buff *skb, struct crypto_alg *alg)
....@@ -529,9 +529,10 @@
529529 __crypto_hash_alg_common(alg)->digestsize);
530530 }
531531
532
-const struct crypto_type crypto_ahash_type = {
532
+static const struct crypto_type crypto_ahash_type = {
533533 .extsize = crypto_ahash_extsize,
534534 .init_tfm = crypto_ahash_init_tfm,
535
+ .free = crypto_ahash_free_instance,
535536 #ifdef CONFIG_PROC_FS
536537 .show = crypto_ahash_show,
537538 #endif
....@@ -541,7 +542,15 @@
541542 .type = CRYPTO_ALG_TYPE_AHASH,
542543 .tfmsize = offsetof(struct crypto_ahash, base),
543544 };
544
-EXPORT_SYMBOL_GPL(crypto_ahash_type);
545
+
546
+int crypto_grab_ahash(struct crypto_ahash_spawn *spawn,
547
+ struct crypto_instance *inst,
548
+ const char *name, u32 type, u32 mask)
549
+{
550
+ spawn->base.frontend = &crypto_ahash_type;
551
+ return crypto_grab_spawn(&spawn->base, inst, name, type, mask);
552
+}
553
+EXPORT_SYMBOL_GPL(crypto_grab_ahash);
545554
546555 struct crypto_ahash *crypto_alloc_ahash(const char *alg_name, u32 type,
547556 u32 mask)
....@@ -560,8 +569,8 @@
560569 {
561570 struct crypto_alg *base = &alg->halg.base;
562571
563
- if (alg->halg.digestsize > PAGE_SIZE / 8 ||
564
- alg->halg.statesize > PAGE_SIZE / 8 ||
572
+ if (alg->halg.digestsize > HASH_MAX_DIGESTSIZE ||
573
+ alg->halg.statesize > HASH_MAX_STATESIZE ||
565574 alg->halg.statesize == 0)
566575 return -EINVAL;
567576
....@@ -585,9 +594,9 @@
585594 }
586595 EXPORT_SYMBOL_GPL(crypto_register_ahash);
587596
588
-int crypto_unregister_ahash(struct ahash_alg *alg)
597
+void crypto_unregister_ahash(struct ahash_alg *alg)
589598 {
590
- return crypto_unregister_alg(&alg->halg.base);
599
+ crypto_unregister_alg(&alg->halg.base);
591600 }
592601 EXPORT_SYMBOL_GPL(crypto_unregister_ahash);
593602
....@@ -625,6 +634,9 @@
625634 {
626635 int err;
627636
637
+ if (WARN_ON(!inst->free))
638
+ return -EINVAL;
639
+
628640 err = ahash_prepare_alg(&inst->alg);
629641 if (err)
630642 return err;
....@@ -632,31 +644,6 @@
632644 return crypto_register_instance(tmpl, ahash_crypto_instance(inst));
633645 }
634646 EXPORT_SYMBOL_GPL(ahash_register_instance);
635
-
636
-void ahash_free_instance(struct crypto_instance *inst)
637
-{
638
- crypto_drop_spawn(crypto_instance_ctx(inst));
639
- kfree(ahash_instance(inst));
640
-}
641
-EXPORT_SYMBOL_GPL(ahash_free_instance);
642
-
643
-int crypto_init_ahash_spawn(struct crypto_ahash_spawn *spawn,
644
- struct hash_alg_common *alg,
645
- struct crypto_instance *inst)
646
-{
647
- return crypto_init_spawn2(&spawn->base, &alg->base, inst,
648
- &crypto_ahash_type);
649
-}
650
-EXPORT_SYMBOL_GPL(crypto_init_ahash_spawn);
651
-
652
-struct hash_alg_common *ahash_attr_alg(struct rtattr *rta, u32 type, u32 mask)
653
-{
654
- struct crypto_alg *alg;
655
-
656
- alg = crypto_attr_alg2(rta, &crypto_ahash_type, type, mask);
657
- return IS_ERR(alg) ? ERR_CAST(alg) : __crypto_hash_alg_common(alg);
658
-}
659
-EXPORT_SYMBOL_GPL(ahash_attr_alg);
660647
661648 bool crypto_hash_alg_has_setkey(struct hash_alg_common *halg)
662649 {