| .. | .. |
|---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-or-later |
|---|
| 1 | 2 | /* |
|---|
| 2 | 3 | * Asynchronous Cryptographic Hash operations. |
|---|
| 3 | 4 | * |
|---|
| .. | .. |
|---|
| 5 | 6 | * completion via a callback. |
|---|
| 6 | 7 | * |
|---|
| 7 | 8 | * 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 | | - * |
|---|
| 14 | 9 | */ |
|---|
| 15 | 10 | |
|---|
| 16 | 11 | #include <crypto/internal/hash.h> |
|---|
| 17 | 12 | #include <crypto/scatterwalk.h> |
|---|
| 18 | | -#include <linux/bug.h> |
|---|
| 19 | 13 | #include <linux/err.h> |
|---|
| 20 | 14 | #include <linux/kernel.h> |
|---|
| 21 | 15 | #include <linux/module.h> |
|---|
| .. | .. |
|---|
| 27 | 21 | #include <net/netlink.h> |
|---|
| 28 | 22 | |
|---|
| 29 | 23 | #include "internal.h" |
|---|
| 24 | + |
|---|
| 25 | +static const struct crypto_type crypto_ahash_type; |
|---|
| 30 | 26 | |
|---|
| 31 | 27 | struct ahash_request_priv { |
|---|
| 32 | 28 | crypto_completion_t complete; |
|---|
| .. | .. |
|---|
| 49 | 45 | unsigned int nbytes = min(walk->entrylen, |
|---|
| 50 | 46 | ((unsigned int)(PAGE_SIZE)) - offset); |
|---|
| 51 | 47 | |
|---|
| 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); |
|---|
| 56 | 49 | walk->data += offset; |
|---|
| 57 | 50 | |
|---|
| 58 | 51 | if (offset & alignmask) { |
|---|
| .. | .. |
|---|
| 102 | 95 | } |
|---|
| 103 | 96 | } |
|---|
| 104 | 97 | |
|---|
| 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); |
|---|
| 115 | 100 | |
|---|
| 116 | 101 | if (err) |
|---|
| 117 | 102 | return err; |
|---|
| .. | .. |
|---|
| 143 | 128 | |
|---|
| 144 | 129 | walk->alignmask = crypto_ahash_alignmask(crypto_ahash_reqtfm(req)); |
|---|
| 145 | 130 | walk->sg = req->src; |
|---|
| 146 | | - walk->flags = req->base.flags & CRYPTO_TFM_REQ_MASK; |
|---|
| 131 | + walk->flags = req->base.flags; |
|---|
| 147 | 132 | |
|---|
| 148 | 133 | return hash_walk_new_entry(walk); |
|---|
| 149 | 134 | } |
|---|
| 150 | 135 | 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); |
|---|
| 172 | 136 | |
|---|
| 173 | 137 | static int ahash_setkey_unaligned(struct crypto_ahash *tfm, const u8 *key, |
|---|
| 174 | 138 | unsigned int keylen) |
|---|
| .. | .. |
|---|
| 186 | 150 | alignbuffer = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1); |
|---|
| 187 | 151 | memcpy(alignbuffer, key, keylen); |
|---|
| 188 | 152 | ret = tfm->setkey(tfm, alignbuffer, keylen); |
|---|
| 189 | | - kzfree(buffer); |
|---|
| 153 | + kfree_sensitive(buffer); |
|---|
| 190 | 154 | return ret; |
|---|
| 191 | 155 | } |
|---|
| 192 | 156 | |
|---|
| .. | .. |
|---|
| 305 | 269 | req->priv = NULL; |
|---|
| 306 | 270 | |
|---|
| 307 | 271 | /* Free the req->priv.priv from the ADJUSTED request. */ |
|---|
| 308 | | - kzfree(priv); |
|---|
| 272 | + kfree_sensitive(priv); |
|---|
| 309 | 273 | } |
|---|
| 310 | 274 | |
|---|
| 311 | 275 | static void ahash_notify_einprogress(struct ahash_request *req) |
|---|
| .. | .. |
|---|
| 375 | 339 | |
|---|
| 376 | 340 | int crypto_ahash_final(struct ahash_request *req) |
|---|
| 377 | 341 | { |
|---|
| 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; |
|---|
| 379 | 351 | } |
|---|
| 380 | 352 | EXPORT_SYMBOL_GPL(crypto_ahash_final); |
|---|
| 381 | 353 | |
|---|
| 382 | 354 | int crypto_ahash_finup(struct ahash_request *req) |
|---|
| 383 | 355 | { |
|---|
| 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; |
|---|
| 385 | 365 | } |
|---|
| 386 | 366 | EXPORT_SYMBOL_GPL(crypto_ahash_finup); |
|---|
| 387 | 367 | |
|---|
| 388 | 368 | int crypto_ahash_digest(struct ahash_request *req) |
|---|
| 389 | 369 | { |
|---|
| 390 | 370 | 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; |
|---|
| 391 | 374 | |
|---|
| 375 | + crypto_stats_get(alg); |
|---|
| 392 | 376 | 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; |
|---|
| 396 | 382 | } |
|---|
| 397 | 383 | EXPORT_SYMBOL_GPL(crypto_ahash_digest); |
|---|
| 398 | 384 | |
|---|
| .. | .. |
|---|
| 458 | 444 | return ahash_def_finup_finish1(req, err); |
|---|
| 459 | 445 | } |
|---|
| 460 | 446 | |
|---|
| 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 | + |
|---|
| 461 | 455 | static int crypto_ahash_init_tfm(struct crypto_tfm *tfm) |
|---|
| 462 | 456 | { |
|---|
| 463 | 457 | struct crypto_ahash *hash = __crypto_ahash_cast(tfm); |
|---|
| .. | .. |
|---|
| 481 | 475 | ahash_set_needkey(hash); |
|---|
| 482 | 476 | } |
|---|
| 483 | 477 | |
|---|
| 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; |
|---|
| 485 | 482 | } |
|---|
| 486 | 483 | |
|---|
| 487 | 484 | static unsigned int crypto_ahash_extsize(struct crypto_alg *alg) |
|---|
| .. | .. |
|---|
| 492 | 489 | return crypto_alg_extsize(alg); |
|---|
| 493 | 490 | } |
|---|
| 494 | 491 | |
|---|
| 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 | + |
|---|
| 495 | 499 | #ifdef CONFIG_NET |
|---|
| 496 | 500 | static int crypto_ahash_report(struct sk_buff *skb, struct crypto_alg *alg) |
|---|
| 497 | 501 | { |
|---|
| 498 | 502 | struct crypto_report_hash rhash; |
|---|
| 499 | 503 | |
|---|
| 500 | | - strncpy(rhash.type, "ahash", sizeof(rhash.type)); |
|---|
| 504 | + memset(&rhash, 0, sizeof(rhash)); |
|---|
| 505 | + |
|---|
| 506 | + strscpy(rhash.type, "ahash", sizeof(rhash.type)); |
|---|
| 501 | 507 | |
|---|
| 502 | 508 | rhash.blocksize = alg->cra_blocksize; |
|---|
| 503 | 509 | rhash.digestsize = __crypto_hash_alg_common(alg)->digestsize; |
|---|
| 504 | 510 | |
|---|
| 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); |
|---|
| 512 | 512 | } |
|---|
| 513 | 513 | #else |
|---|
| 514 | 514 | static int crypto_ahash_report(struct sk_buff *skb, struct crypto_alg *alg) |
|---|
| .. | .. |
|---|
| 529 | 529 | __crypto_hash_alg_common(alg)->digestsize); |
|---|
| 530 | 530 | } |
|---|
| 531 | 531 | |
|---|
| 532 | | -const struct crypto_type crypto_ahash_type = { |
|---|
| 532 | +static const struct crypto_type crypto_ahash_type = { |
|---|
| 533 | 533 | .extsize = crypto_ahash_extsize, |
|---|
| 534 | 534 | .init_tfm = crypto_ahash_init_tfm, |
|---|
| 535 | + .free = crypto_ahash_free_instance, |
|---|
| 535 | 536 | #ifdef CONFIG_PROC_FS |
|---|
| 536 | 537 | .show = crypto_ahash_show, |
|---|
| 537 | 538 | #endif |
|---|
| .. | .. |
|---|
| 541 | 542 | .type = CRYPTO_ALG_TYPE_AHASH, |
|---|
| 542 | 543 | .tfmsize = offsetof(struct crypto_ahash, base), |
|---|
| 543 | 544 | }; |
|---|
| 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); |
|---|
| 545 | 554 | |
|---|
| 546 | 555 | struct crypto_ahash *crypto_alloc_ahash(const char *alg_name, u32 type, |
|---|
| 547 | 556 | u32 mask) |
|---|
| .. | .. |
|---|
| 560 | 569 | { |
|---|
| 561 | 570 | struct crypto_alg *base = &alg->halg.base; |
|---|
| 562 | 571 | |
|---|
| 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 || |
|---|
| 565 | 574 | alg->halg.statesize == 0) |
|---|
| 566 | 575 | return -EINVAL; |
|---|
| 567 | 576 | |
|---|
| .. | .. |
|---|
| 585 | 594 | } |
|---|
| 586 | 595 | EXPORT_SYMBOL_GPL(crypto_register_ahash); |
|---|
| 587 | 596 | |
|---|
| 588 | | -int crypto_unregister_ahash(struct ahash_alg *alg) |
|---|
| 597 | +void crypto_unregister_ahash(struct ahash_alg *alg) |
|---|
| 589 | 598 | { |
|---|
| 590 | | - return crypto_unregister_alg(&alg->halg.base); |
|---|
| 599 | + crypto_unregister_alg(&alg->halg.base); |
|---|
| 591 | 600 | } |
|---|
| 592 | 601 | EXPORT_SYMBOL_GPL(crypto_unregister_ahash); |
|---|
| 593 | 602 | |
|---|
| .. | .. |
|---|
| 625 | 634 | { |
|---|
| 626 | 635 | int err; |
|---|
| 627 | 636 | |
|---|
| 637 | + if (WARN_ON(!inst->free)) |
|---|
| 638 | + return -EINVAL; |
|---|
| 639 | + |
|---|
| 628 | 640 | err = ahash_prepare_alg(&inst->alg); |
|---|
| 629 | 641 | if (err) |
|---|
| 630 | 642 | return err; |
|---|
| .. | .. |
|---|
| 632 | 644 | return crypto_register_instance(tmpl, ahash_crypto_instance(inst)); |
|---|
| 633 | 645 | } |
|---|
| 634 | 646 | 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); |
|---|
| 660 | 647 | |
|---|
| 661 | 648 | bool crypto_hash_alg_has_setkey(struct hash_alg_common *halg) |
|---|
| 662 | 649 | { |
|---|