1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Cryptographic API for algorithms (i.e., low-level API). 4 * 5 * Copyright (c) 2006 Herbert Xu <herbert@gondor.apana.org.au> 6 */ 7 8 #include <crypto/algapi.h> 9 #include <linux/err.h> 10 #include <linux/errno.h> 11 #include <linux/fips.h> 12 #include <linux/init.h> 13 #include <linux/kernel.h> 14 #include <linux/list.h> 15 #include <linux/module.h> 16 #include <linux/rtnetlink.h> 17 #include <linux/slab.h> 18 #include <linux/string.h> 19 #include <linux/workqueue.h> 20 21 #include "internal.h" 22 23 static LIST_HEAD(crypto_template_list); 24 25 static inline void crypto_check_module_sig(struct module *mod) 26 { 27 if (fips_enabled && mod && !module_sig_ok(mod)) 28 panic("Module %s signature verification failed in FIPS mode\n", 29 module_name(mod)); 30 } 31 32 static int crypto_check_alg(struct crypto_alg *alg) 33 { 34 crypto_check_module_sig(alg->cra_module); 35 36 if (!alg->cra_name[0] || !alg->cra_driver_name[0]) 37 return -EINVAL; 38 39 if (alg->cra_alignmask & (alg->cra_alignmask + 1)) 40 return -EINVAL; 41 42 /* General maximums for all algs. */ 43 if (alg->cra_alignmask > MAX_ALGAPI_ALIGNMASK) 44 return -EINVAL; 45 46 if (alg->cra_blocksize > MAX_ALGAPI_BLOCKSIZE) 47 return -EINVAL; 48 49 /* Lower maximums for specific alg types. */ 50 if (!alg->cra_type && (alg->cra_flags & CRYPTO_ALG_TYPE_MASK) == 51 CRYPTO_ALG_TYPE_CIPHER) { 52 if (alg->cra_alignmask > MAX_CIPHER_ALIGNMASK) 53 return -EINVAL; 54 55 if (alg->cra_blocksize > MAX_CIPHER_BLOCKSIZE) 56 return -EINVAL; 57 } 58 59 if (alg->cra_priority < 0) 60 return -EINVAL; 61 62 refcount_set(&alg->cra_refcnt, 1); 63 64 return 0; 65 } 66 67 static void crypto_free_instance(struct crypto_instance *inst) 68 { 69 struct crypto_alg *alg = &inst->alg; 70 const struct crypto_type *type; 71 72 type = alg->cra_type; 73 if (type->destroy) 74 type->destroy(alg); 75 type->free(inst); 76 } 77 78 static void crypto_destroy_instance_workfn(struct work_struct *w) 79 { 80 struct crypto_template *tmpl = container_of(w, struct crypto_template, 81 free_work); 82 struct crypto_instance *inst; 83 struct hlist_node *n; 84 HLIST_HEAD(list); 85 86 down_write(&crypto_alg_sem); 87 hlist_for_each_entry_safe(inst, n, &tmpl->dead, list) { 88 if (refcount_read(&inst->alg.cra_refcnt) != -1) 89 continue; 90 hlist_del(&inst->list); 91 hlist_add_head(&inst->list, &list); 92 } 93 up_write(&crypto_alg_sem); 94 95 hlist_for_each_entry_safe(inst, n, &list, list) 96 crypto_free_instance(inst); 97 } 98 99 static void crypto_destroy_instance(struct crypto_alg *alg) 100 { 101 struct crypto_instance *inst = container_of(alg, 102 struct crypto_instance, 103 alg); 104 struct crypto_template *tmpl = inst->tmpl; 105 106 refcount_set(&alg->cra_refcnt, -1); 107 schedule_work(&tmpl->free_work); 108 } 109 110 /* 111 * This function adds a spawn to the list secondary_spawns which 112 * will be used at the end of crypto_remove_spawns to unregister 113 * instances, unless the spawn happens to be one that is depended 114 * on by the new algorithm (nalg in crypto_remove_spawns). 115 * 116 * This function is also responsible for resurrecting any algorithms 117 * in the dependency chain of nalg by unsetting n->dead. 118 */ 119 static struct list_head *crypto_more_spawns(struct crypto_alg *alg, 120 struct list_head *stack, 121 struct list_head *top, 122 struct list_head *secondary_spawns) 123 { 124 struct crypto_spawn *spawn, *n; 125 126 spawn = list_first_entry_or_null(stack, struct crypto_spawn, list); 127 if (!spawn) 128 return NULL; 129 130 n = list_prev_entry(spawn, list); 131 list_move(&spawn->list, secondary_spawns); 132 133 if (list_is_last(&n->list, stack)) 134 return top; 135 136 n = list_next_entry(n, list); 137 if (!spawn->dead) 138 n->dead = false; 139 140 return &n->inst->alg.cra_users; 141 } 142 143 static void crypto_remove_instance(struct crypto_instance *inst, 144 struct list_head *list) 145 { 146 struct crypto_template *tmpl = inst->tmpl; 147 148 if (crypto_is_dead(&inst->alg)) 149 return; 150 151 inst->alg.cra_flags |= CRYPTO_ALG_DEAD; 152 153 if (!tmpl) 154 return; 155 156 list_del_init(&inst->alg.cra_list); 157 hlist_del(&inst->list); 158 hlist_add_head(&inst->list, &tmpl->dead); 159 160 BUG_ON(!list_empty(&inst->alg.cra_users)); 161 162 crypto_alg_put(&inst->alg); 163 } 164 165 /* 166 * Given an algorithm alg, remove all algorithms that depend on it 167 * through spawns. If nalg is not null, then exempt any algorithms 168 * that is depended on by nalg. This is useful when nalg itself 169 * depends on alg. 170 */ 171 void crypto_remove_spawns(struct crypto_alg *alg, struct list_head *list, 172 struct crypto_alg *nalg) 173 { 174 u32 new_type = (nalg ?: alg)->cra_flags; 175 struct crypto_spawn *spawn, *n; 176 LIST_HEAD(secondary_spawns); 177 struct list_head *spawns; 178 LIST_HEAD(stack); 179 LIST_HEAD(top); 180 181 spawns = &alg->cra_users; 182 list_for_each_entry_safe(spawn, n, spawns, list) { 183 if ((spawn->alg->cra_flags ^ new_type) & spawn->mask) 184 continue; 185 186 list_move(&spawn->list, &top); 187 } 188 189 /* 190 * Perform a depth-first walk starting from alg through 191 * the cra_users tree. The list stack records the path 192 * from alg to the current spawn. 193 */ 194 spawns = ⊤ 195 do { 196 while (!list_empty(spawns)) { 197 struct crypto_instance *inst; 198 199 spawn = list_first_entry(spawns, struct crypto_spawn, 200 list); 201 inst = spawn->inst; 202 203 list_move(&spawn->list, &stack); 204 spawn->dead = !spawn->registered || &inst->alg != nalg; 205 206 if (!spawn->registered) 207 break; 208 209 BUG_ON(&inst->alg == alg); 210 211 if (&inst->alg == nalg) 212 break; 213 214 spawns = &inst->alg.cra_users; 215 216 /* 217 * Even if spawn->registered is true, the 218 * instance itself may still be unregistered. 219 * This is because it may have failed during 220 * registration. Therefore we still need to 221 * make the following test. 222 * 223 * We may encounter an unregistered instance here, since 224 * an instance's spawns are set up prior to the instance 225 * being registered. An unregistered instance will have 226 * NULL ->cra_users.next, since ->cra_users isn't 227 * properly initialized until registration. But an 228 * unregistered instance cannot have any users, so treat 229 * it the same as ->cra_users being empty. 230 */ 231 if (spawns->next == NULL) 232 break; 233 } 234 } while ((spawns = crypto_more_spawns(alg, &stack, &top, 235 &secondary_spawns))); 236 237 /* 238 * Remove all instances that are marked as dead. Also 239 * complete the resurrection of the others by moving them 240 * back to the cra_users list. 241 */ 242 list_for_each_entry_safe(spawn, n, &secondary_spawns, list) { 243 if (!spawn->dead) 244 list_move(&spawn->list, &spawn->alg->cra_users); 245 else if (spawn->registered) 246 crypto_remove_instance(spawn->inst, list); 247 } 248 } 249 EXPORT_SYMBOL_GPL(crypto_remove_spawns); 250 251 static void crypto_alg_finish_registration(struct crypto_alg *alg, 252 struct list_head *algs_to_put) 253 { 254 struct crypto_alg *q; 255 256 list_for_each_entry(q, &crypto_alg_list, cra_list) { 257 if (q == alg) 258 continue; 259 260 if (crypto_is_moribund(q)) 261 continue; 262 263 if (crypto_is_larval(q)) 264 continue; 265 266 if (strcmp(alg->cra_name, q->cra_name)) 267 continue; 268 269 if (strcmp(alg->cra_driver_name, q->cra_driver_name) && 270 q->cra_priority > alg->cra_priority) 271 continue; 272 273 crypto_remove_spawns(q, algs_to_put, alg); 274 } 275 276 crypto_notify(CRYPTO_MSG_ALG_LOADED, alg); 277 } 278 279 static struct crypto_larval *crypto_alloc_test_larval(struct crypto_alg *alg) 280 { 281 struct crypto_larval *larval; 282 283 if (!IS_ENABLED(CONFIG_CRYPTO_MANAGER) || 284 IS_ENABLED(CONFIG_CRYPTO_MANAGER_DISABLE_TESTS) || 285 (alg->cra_flags & CRYPTO_ALG_INTERNAL)) 286 return NULL; /* No self-test needed */ 287 288 larval = crypto_larval_alloc(alg->cra_name, 289 alg->cra_flags | CRYPTO_ALG_TESTED, 0); 290 if (IS_ERR(larval)) 291 return larval; 292 293 larval->adult = crypto_mod_get(alg); 294 if (!larval->adult) { 295 kfree(larval); 296 return ERR_PTR(-ENOENT); 297 } 298 299 refcount_set(&larval->alg.cra_refcnt, 1); 300 memcpy(larval->alg.cra_driver_name, alg->cra_driver_name, 301 CRYPTO_MAX_ALG_NAME); 302 larval->alg.cra_priority = alg->cra_priority; 303 304 return larval; 305 } 306 307 static struct crypto_larval * 308 __crypto_register_alg(struct crypto_alg *alg, struct list_head *algs_to_put) 309 { 310 struct crypto_alg *q; 311 struct crypto_larval *larval; 312 int ret = -EAGAIN; 313 314 if (crypto_is_dead(alg)) 315 goto err; 316 317 INIT_LIST_HEAD(&alg->cra_users); 318 319 ret = -EEXIST; 320 321 list_for_each_entry(q, &crypto_alg_list, cra_list) { 322 if (q == alg) 323 goto err; 324 325 if (crypto_is_moribund(q)) 326 continue; 327 328 if (crypto_is_larval(q)) { 329 if (!strcmp(alg->cra_driver_name, q->cra_driver_name)) 330 goto err; 331 continue; 332 } 333 334 if (!strcmp(q->cra_driver_name, alg->cra_name) || 335 !strcmp(q->cra_driver_name, alg->cra_driver_name) || 336 !strcmp(q->cra_name, alg->cra_driver_name)) 337 goto err; 338 } 339 340 larval = crypto_alloc_test_larval(alg); 341 if (IS_ERR(larval)) 342 goto out; 343 344 list_add(&alg->cra_list, &crypto_alg_list); 345 346 if (larval) { 347 /* No cheating! */ 348 alg->cra_flags &= ~CRYPTO_ALG_TESTED; 349 350 list_add(&larval->alg.cra_list, &crypto_alg_list); 351 } else { 352 alg->cra_flags |= CRYPTO_ALG_TESTED; 353 crypto_alg_finish_registration(alg, algs_to_put); 354 } 355 356 out: 357 return larval; 358 359 err: 360 larval = ERR_PTR(ret); 361 goto out; 362 } 363 364 void crypto_alg_tested(const char *name, int err) 365 { 366 struct crypto_larval *test; 367 struct crypto_alg *alg; 368 struct crypto_alg *q; 369 LIST_HEAD(list); 370 371 down_write(&crypto_alg_sem); 372 list_for_each_entry(q, &crypto_alg_list, cra_list) { 373 if (crypto_is_moribund(q) || !crypto_is_larval(q)) 374 continue; 375 376 test = (struct crypto_larval *)q; 377 378 if (!strcmp(q->cra_driver_name, name)) 379 goto found; 380 } 381 382 pr_err("alg: Unexpected test result for %s: %d\n", name, err); 383 up_write(&crypto_alg_sem); 384 return; 385 386 found: 387 q->cra_flags |= CRYPTO_ALG_DEAD; 388 alg = test->adult; 389 390 if (crypto_is_dead(alg)) 391 goto complete; 392 393 if (err == -ECANCELED) 394 alg->cra_flags |= CRYPTO_ALG_FIPS_INTERNAL; 395 else if (err) 396 goto complete; 397 else 398 alg->cra_flags &= ~CRYPTO_ALG_FIPS_INTERNAL; 399 400 alg->cra_flags |= CRYPTO_ALG_TESTED; 401 402 crypto_alg_finish_registration(alg, &list); 403 404 complete: 405 list_del_init(&test->alg.cra_list); 406 complete_all(&test->completion); 407 408 up_write(&crypto_alg_sem); 409 410 crypto_alg_put(&test->alg); 411 crypto_remove_final(&list); 412 } 413 EXPORT_SYMBOL_GPL(crypto_alg_tested); 414 415 void crypto_remove_final(struct list_head *list) 416 { 417 struct crypto_alg *alg; 418 struct crypto_alg *n; 419 420 list_for_each_entry_safe(alg, n, list, cra_list) { 421 list_del_init(&alg->cra_list); 422 crypto_alg_put(alg); 423 } 424 } 425 EXPORT_SYMBOL_GPL(crypto_remove_final); 426 427 int crypto_register_alg(struct crypto_alg *alg) 428 { 429 struct crypto_larval *larval; 430 bool test_started = false; 431 LIST_HEAD(algs_to_put); 432 int err; 433 434 alg->cra_flags &= ~CRYPTO_ALG_DEAD; 435 err = crypto_check_alg(alg); 436 if (err) 437 return err; 438 439 down_write(&crypto_alg_sem); 440 larval = __crypto_register_alg(alg, &algs_to_put); 441 if (!IS_ERR_OR_NULL(larval)) { 442 test_started = crypto_boot_test_finished(); 443 larval->test_started = test_started; 444 } 445 up_write(&crypto_alg_sem); 446 447 if (IS_ERR(larval)) 448 return PTR_ERR(larval); 449 450 if (test_started) 451 crypto_schedule_test(larval); 452 else 453 crypto_remove_final(&algs_to_put); 454 455 return 0; 456 } 457 EXPORT_SYMBOL_GPL(crypto_register_alg); 458 459 static int crypto_remove_alg(struct crypto_alg *alg, struct list_head *list) 460 { 461 if (unlikely(list_empty(&alg->cra_list))) 462 return -ENOENT; 463 464 alg->cra_flags |= CRYPTO_ALG_DEAD; 465 466 list_del_init(&alg->cra_list); 467 crypto_remove_spawns(alg, list, NULL); 468 469 return 0; 470 } 471 472 void crypto_unregister_alg(struct crypto_alg *alg) 473 { 474 int ret; 475 LIST_HEAD(list); 476 477 down_write(&crypto_alg_sem); 478 ret = crypto_remove_alg(alg, &list); 479 up_write(&crypto_alg_sem); 480 481 if (WARN(ret, "Algorithm %s is not registered", alg->cra_driver_name)) 482 return; 483 484 if (WARN_ON(refcount_read(&alg->cra_refcnt) != 1)) 485 return; 486 487 if (alg->cra_type && alg->cra_type->destroy) 488 alg->cra_type->destroy(alg); 489 490 crypto_remove_final(&list); 491 } 492 EXPORT_SYMBOL_GPL(crypto_unregister_alg); 493 494 int crypto_register_algs(struct crypto_alg *algs, int count) 495 { 496 int i, ret; 497 498 for (i = 0; i < count; i++) { 499 ret = crypto_register_alg(&algs[i]); 500 if (ret) 501 goto err; 502 } 503 504 return 0; 505 506 err: 507 for (--i; i >= 0; --i) 508 crypto_unregister_alg(&algs[i]); 509 510 return ret; 511 } 512 EXPORT_SYMBOL_GPL(crypto_register_algs); 513 514 void crypto_unregister_algs(struct crypto_alg *algs, int count) 515 { 516 int i; 517 518 for (i = 0; i < count; i++) 519 crypto_unregister_alg(&algs[i]); 520 } 521 EXPORT_SYMBOL_GPL(crypto_unregister_algs); 522 523 int crypto_register_template(struct crypto_template *tmpl) 524 { 525 struct crypto_template *q; 526 int err = -EEXIST; 527 528 INIT_WORK(&tmpl->free_work, crypto_destroy_instance_workfn); 529 530 down_write(&crypto_alg_sem); 531 532 crypto_check_module_sig(tmpl->module); 533 534 list_for_each_entry(q, &crypto_template_list, list) { 535 if (q == tmpl) 536 goto out; 537 } 538 539 list_add(&tmpl->list, &crypto_template_list); 540 err = 0; 541 out: 542 up_write(&crypto_alg_sem); 543 return err; 544 } 545 EXPORT_SYMBOL_GPL(crypto_register_template); 546 547 int crypto_register_templates(struct crypto_template *tmpls, int count) 548 { 549 int i, err; 550 551 for (i = 0; i < count; i++) { 552 err = crypto_register_template(&tmpls[i]); 553 if (err) 554 goto out; 555 } 556 return 0; 557 558 out: 559 for (--i; i >= 0; --i) 560 crypto_unregister_template(&tmpls[i]); 561 return err; 562 } 563 EXPORT_SYMBOL_GPL(crypto_register_templates); 564 565 void crypto_unregister_template(struct crypto_template *tmpl) 566 { 567 struct crypto_instance *inst; 568 struct hlist_node *n; 569 struct hlist_head *list; 570 LIST_HEAD(users); 571 572 down_write(&crypto_alg_sem); 573 574 BUG_ON(list_empty(&tmpl->list)); 575 list_del_init(&tmpl->list); 576 577 list = &tmpl->instances; 578 hlist_for_each_entry(inst, list, list) { 579 int err = crypto_remove_alg(&inst->alg, &users); 580 581 BUG_ON(err); 582 } 583 584 up_write(&crypto_alg_sem); 585 586 hlist_for_each_entry_safe(inst, n, list, list) { 587 BUG_ON(refcount_read(&inst->alg.cra_refcnt) != 1); 588 crypto_free_instance(inst); 589 } 590 crypto_remove_final(&users); 591 592 flush_work(&tmpl->free_work); 593 } 594 EXPORT_SYMBOL_GPL(crypto_unregister_template); 595 596 void crypto_unregister_templates(struct crypto_template *tmpls, int count) 597 { 598 int i; 599 600 for (i = count - 1; i >= 0; --i) 601 crypto_unregister_template(&tmpls[i]); 602 } 603 EXPORT_SYMBOL_GPL(crypto_unregister_templates); 604 605 static struct crypto_template *__crypto_lookup_template(const char *name) 606 { 607 struct crypto_template *q, *tmpl = NULL; 608 609 down_read(&crypto_alg_sem); 610 list_for_each_entry(q, &crypto_template_list, list) { 611 if (strcmp(q->name, name)) 612 continue; 613 if (unlikely(!crypto_tmpl_get(q))) 614 continue; 615 616 tmpl = q; 617 break; 618 } 619 up_read(&crypto_alg_sem); 620 621 return tmpl; 622 } 623 624 struct crypto_template *crypto_lookup_template(const char *name) 625 { 626 return try_then_request_module(__crypto_lookup_template(name), 627 "crypto-%s", name); 628 } 629 EXPORT_SYMBOL_GPL(crypto_lookup_template); 630 631 int crypto_register_instance(struct crypto_template *tmpl, 632 struct crypto_instance *inst) 633 { 634 struct crypto_larval *larval; 635 struct crypto_spawn *spawn; 636 u32 fips_internal = 0; 637 LIST_HEAD(algs_to_put); 638 int err; 639 640 err = crypto_check_alg(&inst->alg); 641 if (err) 642 return err; 643 644 inst->alg.cra_module = tmpl->module; 645 inst->alg.cra_flags |= CRYPTO_ALG_INSTANCE; 646 inst->alg.cra_destroy = crypto_destroy_instance; 647 648 down_write(&crypto_alg_sem); 649 650 larval = ERR_PTR(-EAGAIN); 651 for (spawn = inst->spawns; spawn;) { 652 struct crypto_spawn *next; 653 654 if (spawn->dead) 655 goto unlock; 656 657 next = spawn->next; 658 spawn->inst = inst; 659 spawn->registered = true; 660 661 fips_internal |= spawn->alg->cra_flags; 662 663 crypto_mod_put(spawn->alg); 664 665 spawn = next; 666 } 667 668 inst->alg.cra_flags |= (fips_internal & CRYPTO_ALG_FIPS_INTERNAL); 669 670 larval = __crypto_register_alg(&inst->alg, &algs_to_put); 671 if (IS_ERR(larval)) 672 goto unlock; 673 else if (larval) 674 larval->test_started = true; 675 676 hlist_add_head(&inst->list, &tmpl->instances); 677 inst->tmpl = tmpl; 678 679 unlock: 680 up_write(&crypto_alg_sem); 681 682 if (IS_ERR(larval)) 683 return PTR_ERR(larval); 684 685 if (larval) 686 crypto_schedule_test(larval); 687 else 688 crypto_remove_final(&algs_to_put); 689 690 return 0; 691 } 692 EXPORT_SYMBOL_GPL(crypto_register_instance); 693 694 void crypto_unregister_instance(struct crypto_instance *inst) 695 { 696 LIST_HEAD(list); 697 698 down_write(&crypto_alg_sem); 699 700 crypto_remove_spawns(&inst->alg, &list, NULL); 701 crypto_remove_instance(inst, &list); 702 703 up_write(&crypto_alg_sem); 704 705 crypto_remove_final(&list); 706 } 707 EXPORT_SYMBOL_GPL(crypto_unregister_instance); 708 709 int crypto_grab_spawn(struct crypto_spawn *spawn, struct crypto_instance *inst, 710 const char *name, u32 type, u32 mask) 711 { 712 struct crypto_alg *alg; 713 int err = -EAGAIN; 714 715 if (WARN_ON_ONCE(inst == NULL)) 716 return -EINVAL; 717 718 /* Allow the result of crypto_attr_alg_name() to be passed directly */ 719 if (IS_ERR(name)) 720 return PTR_ERR(name); 721 722 alg = crypto_find_alg(name, spawn->frontend, 723 type | CRYPTO_ALG_FIPS_INTERNAL, mask); 724 if (IS_ERR(alg)) 725 return PTR_ERR(alg); 726 727 down_write(&crypto_alg_sem); 728 if (!crypto_is_moribund(alg)) { 729 list_add(&spawn->list, &alg->cra_users); 730 spawn->alg = alg; 731 spawn->mask = mask; 732 spawn->next = inst->spawns; 733 inst->spawns = spawn; 734 inst->alg.cra_flags |= 735 (alg->cra_flags & CRYPTO_ALG_INHERITED_FLAGS); 736 err = 0; 737 } 738 up_write(&crypto_alg_sem); 739 if (err) 740 crypto_mod_put(alg); 741 return err; 742 } 743 EXPORT_SYMBOL_GPL(crypto_grab_spawn); 744 745 void crypto_drop_spawn(struct crypto_spawn *spawn) 746 { 747 if (!spawn->alg) /* not yet initialized? */ 748 return; 749 750 down_write(&crypto_alg_sem); 751 if (!spawn->dead) 752 list_del(&spawn->list); 753 up_write(&crypto_alg_sem); 754 755 if (!spawn->registered) 756 crypto_mod_put(spawn->alg); 757 } 758 EXPORT_SYMBOL_GPL(crypto_drop_spawn); 759 760 static struct crypto_alg *crypto_spawn_alg(struct crypto_spawn *spawn) 761 { 762 struct crypto_alg *alg = ERR_PTR(-EAGAIN); 763 struct crypto_alg *target; 764 bool shoot = false; 765 766 down_read(&crypto_alg_sem); 767 if (!spawn->dead) { 768 alg = spawn->alg; 769 if (!crypto_mod_get(alg)) { 770 target = crypto_alg_get(alg); 771 shoot = true; 772 alg = ERR_PTR(-EAGAIN); 773 } 774 } 775 up_read(&crypto_alg_sem); 776 777 if (shoot) { 778 crypto_shoot_alg(target); 779 crypto_alg_put(target); 780 } 781 782 return alg; 783 } 784 785 struct crypto_tfm *crypto_spawn_tfm(struct crypto_spawn *spawn, u32 type, 786 u32 mask) 787 { 788 struct crypto_alg *alg; 789 struct crypto_tfm *tfm; 790 791 alg = crypto_spawn_alg(spawn); 792 if (IS_ERR(alg)) 793 return ERR_CAST(alg); 794 795 tfm = ERR_PTR(-EINVAL); 796 if (unlikely((alg->cra_flags ^ type) & mask)) 797 goto out_put_alg; 798 799 tfm = __crypto_alloc_tfm(alg, type, mask); 800 if (IS_ERR(tfm)) 801 goto out_put_alg; 802 803 return tfm; 804 805 out_put_alg: 806 crypto_mod_put(alg); 807 return tfm; 808 } 809 EXPORT_SYMBOL_GPL(crypto_spawn_tfm); 810 811 void *crypto_spawn_tfm2(struct crypto_spawn *spawn) 812 { 813 struct crypto_alg *alg; 814 struct crypto_tfm *tfm; 815 816 alg = crypto_spawn_alg(spawn); 817 if (IS_ERR(alg)) 818 return ERR_CAST(alg); 819 820 tfm = crypto_create_tfm(alg, spawn->frontend); 821 if (IS_ERR(tfm)) 822 goto out_put_alg; 823 824 return tfm; 825 826 out_put_alg: 827 crypto_mod_put(alg); 828 return tfm; 829 } 830 EXPORT_SYMBOL_GPL(crypto_spawn_tfm2); 831 832 int crypto_register_notifier(struct notifier_block *nb) 833 { 834 return blocking_notifier_chain_register(&crypto_chain, nb); 835 } 836 EXPORT_SYMBOL_GPL(crypto_register_notifier); 837 838 int crypto_unregister_notifier(struct notifier_block *nb) 839 { 840 return blocking_notifier_chain_unregister(&crypto_chain, nb); 841 } 842 EXPORT_SYMBOL_GPL(crypto_unregister_notifier); 843 844 struct crypto_attr_type *crypto_get_attr_type(struct rtattr **tb) 845 { 846 struct rtattr *rta = tb[0]; 847 struct crypto_attr_type *algt; 848 849 if (!rta) 850 return ERR_PTR(-ENOENT); 851 if (RTA_PAYLOAD(rta) < sizeof(*algt)) 852 return ERR_PTR(-EINVAL); 853 if (rta->rta_type != CRYPTOA_TYPE) 854 return ERR_PTR(-EINVAL); 855 856 algt = RTA_DATA(rta); 857 858 return algt; 859 } 860 EXPORT_SYMBOL_GPL(crypto_get_attr_type); 861 862 /** 863 * crypto_check_attr_type() - check algorithm type and compute inherited mask 864 * @tb: the template parameters 865 * @type: the algorithm type the template would be instantiated as 866 * @mask_ret: (output) the mask that should be passed to crypto_grab_*() 867 * to restrict the flags of any inner algorithms 868 * 869 * Validate that the algorithm type the user requested is compatible with the 870 * one the template would actually be instantiated as. E.g., if the user is 871 * doing crypto_alloc_shash("cbc(aes)", ...), this would return an error because 872 * the "cbc" template creates an "skcipher" algorithm, not an "shash" algorithm. 873 * 874 * Also compute the mask to use to restrict the flags of any inner algorithms. 875 * 876 * Return: 0 on success; -errno on failure 877 */ 878 int crypto_check_attr_type(struct rtattr **tb, u32 type, u32 *mask_ret) 879 { 880 struct crypto_attr_type *algt; 881 882 algt = crypto_get_attr_type(tb); 883 if (IS_ERR(algt)) 884 return PTR_ERR(algt); 885 886 if ((algt->type ^ type) & algt->mask) 887 return -EINVAL; 888 889 *mask_ret = crypto_algt_inherited_mask(algt); 890 return 0; 891 } 892 EXPORT_SYMBOL_GPL(crypto_check_attr_type); 893 894 const char *crypto_attr_alg_name(struct rtattr *rta) 895 { 896 struct crypto_attr_alg *alga; 897 898 if (!rta) 899 return ERR_PTR(-ENOENT); 900 if (RTA_PAYLOAD(rta) < sizeof(*alga)) 901 return ERR_PTR(-EINVAL); 902 if (rta->rta_type != CRYPTOA_ALG) 903 return ERR_PTR(-EINVAL); 904 905 alga = RTA_DATA(rta); 906 alga->name[CRYPTO_MAX_ALG_NAME - 1] = 0; 907 908 return alga->name; 909 } 910 EXPORT_SYMBOL_GPL(crypto_attr_alg_name); 911 912 int crypto_inst_setname(struct crypto_instance *inst, const char *name, 913 struct crypto_alg *alg) 914 { 915 if (snprintf(inst->alg.cra_name, CRYPTO_MAX_ALG_NAME, "%s(%s)", name, 916 alg->cra_name) >= CRYPTO_MAX_ALG_NAME) 917 return -ENAMETOOLONG; 918 919 if (snprintf(inst->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s(%s)", 920 name, alg->cra_driver_name) >= CRYPTO_MAX_ALG_NAME) 921 return -ENAMETOOLONG; 922 923 return 0; 924 } 925 EXPORT_SYMBOL_GPL(crypto_inst_setname); 926 927 void crypto_init_queue(struct crypto_queue *queue, unsigned int max_qlen) 928 { 929 INIT_LIST_HEAD(&queue->list); 930 queue->backlog = &queue->list; 931 queue->qlen = 0; 932 queue->max_qlen = max_qlen; 933 } 934 EXPORT_SYMBOL_GPL(crypto_init_queue); 935 936 int crypto_enqueue_request(struct crypto_queue *queue, 937 struct crypto_async_request *request) 938 { 939 int err = -EINPROGRESS; 940 941 if (unlikely(queue->qlen >= queue->max_qlen)) { 942 if (!(request->flags & CRYPTO_TFM_REQ_MAY_BACKLOG)) { 943 err = -ENOSPC; 944 goto out; 945 } 946 err = -EBUSY; 947 if (queue->backlog == &queue->list) 948 queue->backlog = &request->list; 949 } 950 951 queue->qlen++; 952 list_add_tail(&request->list, &queue->list); 953 954 out: 955 return err; 956 } 957 EXPORT_SYMBOL_GPL(crypto_enqueue_request); 958 959 void crypto_enqueue_request_head(struct crypto_queue *queue, 960 struct crypto_async_request *request) 961 { 962 if (unlikely(queue->qlen >= queue->max_qlen)) 963 queue->backlog = queue->backlog->prev; 964 965 queue->qlen++; 966 list_add(&request->list, &queue->list); 967 } 968 EXPORT_SYMBOL_GPL(crypto_enqueue_request_head); 969 970 struct crypto_async_request *crypto_dequeue_request(struct crypto_queue *queue) 971 { 972 struct list_head *request; 973 974 if (unlikely(!queue->qlen)) 975 return NULL; 976 977 queue->qlen--; 978 979 if (queue->backlog != &queue->list) 980 queue->backlog = queue->backlog->next; 981 982 request = queue->list.next; 983 list_del_init(request); 984 985 return list_entry(request, struct crypto_async_request, list); 986 } 987 EXPORT_SYMBOL_GPL(crypto_dequeue_request); 988 989 static inline void crypto_inc_byte(u8 *a, unsigned int size) 990 { 991 u8 *b = (a + size); 992 u8 c; 993 994 for (; size; size--) { 995 c = *--b + 1; 996 *b = c; 997 if (c) 998 break; 999 } 1000 } 1001 1002 void crypto_inc(u8 *a, unsigned int size) 1003 { 1004 __be32 *b = (__be32 *)(a + size); 1005 u32 c; 1006 1007 if (IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) || 1008 IS_ALIGNED((unsigned long)b, __alignof__(*b))) 1009 for (; size >= 4; size -= 4) { 1010 c = be32_to_cpu(*--b) + 1; 1011 *b = cpu_to_be32(c); 1012 if (likely(c)) 1013 return; 1014 } 1015 1016 crypto_inc_byte(a, size); 1017 } 1018 EXPORT_SYMBOL_GPL(crypto_inc); 1019 1020 unsigned int crypto_alg_extsize(struct crypto_alg *alg) 1021 { 1022 return alg->cra_ctxsize + 1023 (alg->cra_alignmask & ~(crypto_tfm_ctx_alignment() - 1)); 1024 } 1025 EXPORT_SYMBOL_GPL(crypto_alg_extsize); 1026 1027 int crypto_type_has_alg(const char *name, const struct crypto_type *frontend, 1028 u32 type, u32 mask) 1029 { 1030 int ret = 0; 1031 struct crypto_alg *alg = crypto_find_alg(name, frontend, type, mask); 1032 1033 if (!IS_ERR(alg)) { 1034 crypto_mod_put(alg); 1035 ret = 1; 1036 } 1037 1038 return ret; 1039 } 1040 EXPORT_SYMBOL_GPL(crypto_type_has_alg); 1041 1042 static void __init crypto_start_tests(void) 1043 { 1044 if (!IS_BUILTIN(CONFIG_CRYPTO_ALGAPI)) 1045 return; 1046 1047 if (IS_ENABLED(CONFIG_CRYPTO_MANAGER_DISABLE_TESTS)) 1048 return; 1049 1050 set_crypto_boot_test_finished(); 1051 1052 for (;;) { 1053 struct crypto_larval *larval = NULL; 1054 struct crypto_alg *q; 1055 1056 down_write(&crypto_alg_sem); 1057 1058 list_for_each_entry(q, &crypto_alg_list, cra_list) { 1059 struct crypto_larval *l; 1060 1061 if (!crypto_is_larval(q)) 1062 continue; 1063 1064 l = (void *)q; 1065 1066 if (!crypto_is_test_larval(l)) 1067 continue; 1068 1069 if (l->test_started) 1070 continue; 1071 1072 l->test_started = true; 1073 larval = l; 1074 break; 1075 } 1076 1077 up_write(&crypto_alg_sem); 1078 1079 if (!larval) 1080 break; 1081 1082 crypto_schedule_test(larval); 1083 } 1084 } 1085 1086 static int __init crypto_algapi_init(void) 1087 { 1088 crypto_init_proc(); 1089 crypto_start_tests(); 1090 return 0; 1091 } 1092 1093 static void __exit crypto_algapi_exit(void) 1094 { 1095 crypto_exit_proc(); 1096 } 1097 1098 /* 1099 * We run this at late_initcall so that all the built-in algorithms 1100 * have had a chance to register themselves first. 1101 */ 1102 late_initcall(crypto_algapi_init); 1103 module_exit(crypto_algapi_exit); 1104 1105 MODULE_LICENSE("GPL"); 1106 MODULE_DESCRIPTION("Cryptographic algorithms API"); 1107 MODULE_SOFTDEP("pre: cryptomgr"); 1108