.. | .. |
---|
32 | 32 | |
---|
33 | 33 | #include <crypto/b128ops.h> |
---|
34 | 34 | #include <crypto/chacha.h> |
---|
| 35 | +#include <crypto/internal/cipher.h> |
---|
35 | 36 | #include <crypto/internal/hash.h> |
---|
36 | 37 | #include <crypto/internal/poly1305.h> |
---|
37 | 38 | #include <crypto/internal/skcipher.h> |
---|
38 | 39 | #include <crypto/nhpoly1305.h> |
---|
39 | 40 | #include <crypto/scatterwalk.h> |
---|
40 | 41 | #include <linux/module.h> |
---|
41 | | - |
---|
42 | | -#include "internal.h" |
---|
43 | 42 | |
---|
44 | 43 | /* |
---|
45 | 44 | * Size of right-hand part of input data, in bytes; also the size of the block |
---|
.. | .. |
---|
64 | 63 | |
---|
65 | 64 | struct adiantum_instance_ctx { |
---|
66 | 65 | struct crypto_skcipher_spawn streamcipher_spawn; |
---|
67 | | - struct crypto_spawn blockcipher_spawn; |
---|
| 66 | + struct crypto_cipher_spawn blockcipher_spawn; |
---|
68 | 67 | struct crypto_shash_spawn hash_spawn; |
---|
69 | 68 | }; |
---|
70 | 69 | |
---|
.. | .. |
---|
135 | 134 | crypto_skcipher_get_flags(tfm) & |
---|
136 | 135 | CRYPTO_TFM_REQ_MASK); |
---|
137 | 136 | err = crypto_skcipher_setkey(tctx->streamcipher, key, keylen); |
---|
138 | | - crypto_skcipher_set_flags(tfm, |
---|
139 | | - crypto_skcipher_get_flags(tctx->streamcipher) & |
---|
140 | | - CRYPTO_TFM_RES_MASK); |
---|
141 | 137 | if (err) |
---|
142 | 138 | return err; |
---|
143 | 139 | |
---|
.. | .. |
---|
167 | 163 | CRYPTO_TFM_REQ_MASK); |
---|
168 | 164 | err = crypto_cipher_setkey(tctx->blockcipher, keyp, |
---|
169 | 165 | BLOCKCIPHER_KEY_SIZE); |
---|
170 | | - crypto_skcipher_set_flags(tfm, |
---|
171 | | - crypto_cipher_get_flags(tctx->blockcipher) & |
---|
172 | | - CRYPTO_TFM_RES_MASK); |
---|
173 | 166 | if (err) |
---|
174 | 167 | goto out; |
---|
175 | 168 | keyp += BLOCKCIPHER_KEY_SIZE; |
---|
.. | .. |
---|
182 | 175 | crypto_shash_set_flags(tctx->hash, crypto_skcipher_get_flags(tfm) & |
---|
183 | 176 | CRYPTO_TFM_REQ_MASK); |
---|
184 | 177 | err = crypto_shash_setkey(tctx->hash, keyp, NHPOLY1305_KEY_SIZE); |
---|
185 | | - crypto_skcipher_set_flags(tfm, crypto_shash_get_flags(tctx->hash) & |
---|
186 | | - CRYPTO_TFM_RES_MASK); |
---|
187 | 178 | keyp += NHPOLY1305_KEY_SIZE; |
---|
188 | 179 | WARN_ON(keyp != &data->derived_keys[ARRAY_SIZE(data->derived_keys)]); |
---|
189 | 180 | out: |
---|
190 | | - kzfree(data); |
---|
| 181 | + kfree_sensitive(data); |
---|
191 | 182 | return err; |
---|
192 | 183 | } |
---|
193 | 184 | |
---|
.. | .. |
---|
266 | 257 | int err; |
---|
267 | 258 | |
---|
268 | 259 | hash_desc->tfm = tctx->hash; |
---|
269 | | - hash_desc->flags = 0; |
---|
270 | 260 | |
---|
271 | 261 | err = crypto_shash_init(hash_desc); |
---|
272 | 262 | if (err) |
---|
.. | .. |
---|
437 | 427 | |
---|
438 | 428 | BUILD_BUG_ON(offsetofend(struct adiantum_request_ctx, u) != |
---|
439 | 429 | sizeof(struct adiantum_request_ctx)); |
---|
440 | | - subreq_size = max(FIELD_SIZEOF(struct adiantum_request_ctx, |
---|
| 430 | + subreq_size = max(sizeof_field(struct adiantum_request_ctx, |
---|
441 | 431 | u.hash_desc) + |
---|
442 | 432 | crypto_shash_descsize(hash), |
---|
443 | | - FIELD_SIZEOF(struct adiantum_request_ctx, |
---|
| 433 | + sizeof_field(struct adiantum_request_ctx, |
---|
444 | 434 | u.streamcipher_req) + |
---|
445 | 435 | crypto_skcipher_reqsize(streamcipher)); |
---|
446 | 436 | |
---|
.. | .. |
---|
470 | 460 | struct adiantum_instance_ctx *ictx = skcipher_instance_ctx(inst); |
---|
471 | 461 | |
---|
472 | 462 | crypto_drop_skcipher(&ictx->streamcipher_spawn); |
---|
473 | | - crypto_drop_spawn(&ictx->blockcipher_spawn); |
---|
| 463 | + crypto_drop_cipher(&ictx->blockcipher_spawn); |
---|
474 | 464 | crypto_drop_shash(&ictx->hash_spawn); |
---|
475 | 465 | kfree(inst); |
---|
476 | 466 | } |
---|
.. | .. |
---|
501 | 491 | |
---|
502 | 492 | static int adiantum_create(struct crypto_template *tmpl, struct rtattr **tb) |
---|
503 | 493 | { |
---|
504 | | - struct crypto_attr_type *algt; |
---|
505 | | - const char *streamcipher_name; |
---|
506 | | - const char *blockcipher_name; |
---|
| 494 | + u32 mask; |
---|
507 | 495 | const char *nhpoly1305_name; |
---|
508 | 496 | struct skcipher_instance *inst; |
---|
509 | 497 | struct adiantum_instance_ctx *ictx; |
---|
510 | 498 | struct skcipher_alg *streamcipher_alg; |
---|
511 | 499 | struct crypto_alg *blockcipher_alg; |
---|
512 | | - struct crypto_alg *_hash_alg; |
---|
513 | 500 | struct shash_alg *hash_alg; |
---|
514 | 501 | int err; |
---|
515 | 502 | |
---|
516 | | - algt = crypto_get_attr_type(tb); |
---|
517 | | - if (IS_ERR(algt)) |
---|
518 | | - return PTR_ERR(algt); |
---|
519 | | - |
---|
520 | | - if ((algt->type ^ CRYPTO_ALG_TYPE_SKCIPHER) & algt->mask) |
---|
521 | | - return -EINVAL; |
---|
522 | | - |
---|
523 | | - streamcipher_name = crypto_attr_alg_name(tb[1]); |
---|
524 | | - if (IS_ERR(streamcipher_name)) |
---|
525 | | - return PTR_ERR(streamcipher_name); |
---|
526 | | - |
---|
527 | | - blockcipher_name = crypto_attr_alg_name(tb[2]); |
---|
528 | | - if (IS_ERR(blockcipher_name)) |
---|
529 | | - return PTR_ERR(blockcipher_name); |
---|
530 | | - |
---|
531 | | - nhpoly1305_name = crypto_attr_alg_name(tb[3]); |
---|
532 | | - if (nhpoly1305_name == ERR_PTR(-ENOENT)) |
---|
533 | | - nhpoly1305_name = "nhpoly1305"; |
---|
534 | | - if (IS_ERR(nhpoly1305_name)) |
---|
535 | | - return PTR_ERR(nhpoly1305_name); |
---|
| 503 | + err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_SKCIPHER, &mask); |
---|
| 504 | + if (err) |
---|
| 505 | + return err; |
---|
536 | 506 | |
---|
537 | 507 | inst = kzalloc(sizeof(*inst) + sizeof(*ictx), GFP_KERNEL); |
---|
538 | 508 | if (!inst) |
---|
.. | .. |
---|
540 | 510 | ictx = skcipher_instance_ctx(inst); |
---|
541 | 511 | |
---|
542 | 512 | /* Stream cipher, e.g. "xchacha12" */ |
---|
543 | | - crypto_set_skcipher_spawn(&ictx->streamcipher_spawn, |
---|
544 | | - skcipher_crypto_instance(inst)); |
---|
545 | | - err = crypto_grab_skcipher(&ictx->streamcipher_spawn, streamcipher_name, |
---|
546 | | - 0, crypto_requires_sync(algt->type, |
---|
547 | | - algt->mask)); |
---|
| 513 | + err = crypto_grab_skcipher(&ictx->streamcipher_spawn, |
---|
| 514 | + skcipher_crypto_instance(inst), |
---|
| 515 | + crypto_attr_alg_name(tb[1]), 0, mask); |
---|
548 | 516 | if (err) |
---|
549 | | - goto out_free_inst; |
---|
| 517 | + goto err_free_inst; |
---|
550 | 518 | streamcipher_alg = crypto_spawn_skcipher_alg(&ictx->streamcipher_spawn); |
---|
551 | 519 | |
---|
552 | 520 | /* Block cipher, e.g. "aes" */ |
---|
553 | | - crypto_set_spawn(&ictx->blockcipher_spawn, |
---|
554 | | - skcipher_crypto_instance(inst)); |
---|
555 | | - err = crypto_grab_spawn(&ictx->blockcipher_spawn, blockcipher_name, |
---|
556 | | - CRYPTO_ALG_TYPE_CIPHER, CRYPTO_ALG_TYPE_MASK); |
---|
| 521 | + err = crypto_grab_cipher(&ictx->blockcipher_spawn, |
---|
| 522 | + skcipher_crypto_instance(inst), |
---|
| 523 | + crypto_attr_alg_name(tb[2]), 0, mask); |
---|
557 | 524 | if (err) |
---|
558 | | - goto out_drop_streamcipher; |
---|
559 | | - blockcipher_alg = ictx->blockcipher_spawn.alg; |
---|
| 525 | + goto err_free_inst; |
---|
| 526 | + blockcipher_alg = crypto_spawn_cipher_alg(&ictx->blockcipher_spawn); |
---|
560 | 527 | |
---|
561 | 528 | /* NHPoly1305 ε-∆U hash function */ |
---|
562 | | - _hash_alg = crypto_alg_mod_lookup(nhpoly1305_name, |
---|
563 | | - CRYPTO_ALG_TYPE_SHASH, |
---|
564 | | - CRYPTO_ALG_TYPE_MASK); |
---|
565 | | - if (IS_ERR(_hash_alg)) { |
---|
566 | | - err = PTR_ERR(_hash_alg); |
---|
567 | | - goto out_drop_blockcipher; |
---|
568 | | - } |
---|
569 | | - hash_alg = __crypto_shash_alg(_hash_alg); |
---|
570 | | - err = crypto_init_shash_spawn(&ictx->hash_spawn, hash_alg, |
---|
571 | | - skcipher_crypto_instance(inst)); |
---|
| 529 | + nhpoly1305_name = crypto_attr_alg_name(tb[3]); |
---|
| 530 | + if (nhpoly1305_name == ERR_PTR(-ENOENT)) |
---|
| 531 | + nhpoly1305_name = "nhpoly1305"; |
---|
| 532 | + err = crypto_grab_shash(&ictx->hash_spawn, |
---|
| 533 | + skcipher_crypto_instance(inst), |
---|
| 534 | + nhpoly1305_name, 0, mask); |
---|
572 | 535 | if (err) |
---|
573 | | - goto out_put_hash; |
---|
| 536 | + goto err_free_inst; |
---|
| 537 | + hash_alg = crypto_spawn_shash_alg(&ictx->hash_spawn); |
---|
574 | 538 | |
---|
575 | 539 | /* Check the set of algorithms */ |
---|
576 | 540 | if (!adiantum_supported_algorithms(streamcipher_alg, blockcipher_alg, |
---|
.. | .. |
---|
579 | 543 | streamcipher_alg->base.cra_name, |
---|
580 | 544 | blockcipher_alg->cra_name, hash_alg->base.cra_name); |
---|
581 | 545 | err = -EINVAL; |
---|
582 | | - goto out_drop_hash; |
---|
| 546 | + goto err_free_inst; |
---|
583 | 547 | } |
---|
584 | 548 | |
---|
585 | 549 | /* Instance fields */ |
---|
.. | .. |
---|
588 | 552 | if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME, |
---|
589 | 553 | "adiantum(%s,%s)", streamcipher_alg->base.cra_name, |
---|
590 | 554 | blockcipher_alg->cra_name) >= CRYPTO_MAX_ALG_NAME) |
---|
591 | | - goto out_drop_hash; |
---|
| 555 | + goto err_free_inst; |
---|
592 | 556 | if (snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME, |
---|
593 | 557 | "adiantum(%s,%s,%s)", |
---|
594 | 558 | streamcipher_alg->base.cra_driver_name, |
---|
595 | 559 | blockcipher_alg->cra_driver_name, |
---|
596 | 560 | hash_alg->base.cra_driver_name) >= CRYPTO_MAX_ALG_NAME) |
---|
597 | | - goto out_drop_hash; |
---|
| 561 | + goto err_free_inst; |
---|
598 | 562 | |
---|
599 | | - inst->alg.base.cra_flags = streamcipher_alg->base.cra_flags & |
---|
600 | | - CRYPTO_ALG_ASYNC; |
---|
601 | 563 | inst->alg.base.cra_blocksize = BLOCKCIPHER_BLOCK_SIZE; |
---|
602 | 564 | inst->alg.base.cra_ctxsize = sizeof(struct adiantum_tfm_ctx); |
---|
603 | 565 | inst->alg.base.cra_alignmask = streamcipher_alg->base.cra_alignmask | |
---|
.. | .. |
---|
624 | 586 | inst->free = adiantum_free_instance; |
---|
625 | 587 | |
---|
626 | 588 | err = skcipher_register_instance(tmpl, inst); |
---|
627 | | - if (err) |
---|
628 | | - goto out_drop_hash; |
---|
629 | | - |
---|
630 | | - crypto_mod_put(_hash_alg); |
---|
631 | | - return 0; |
---|
632 | | - |
---|
633 | | -out_drop_hash: |
---|
634 | | - crypto_drop_shash(&ictx->hash_spawn); |
---|
635 | | -out_put_hash: |
---|
636 | | - crypto_mod_put(_hash_alg); |
---|
637 | | -out_drop_blockcipher: |
---|
638 | | - crypto_drop_spawn(&ictx->blockcipher_spawn); |
---|
639 | | -out_drop_streamcipher: |
---|
640 | | - crypto_drop_skcipher(&ictx->streamcipher_spawn); |
---|
641 | | -out_free_inst: |
---|
642 | | - kfree(inst); |
---|
| 589 | + if (err) { |
---|
| 590 | +err_free_inst: |
---|
| 591 | + adiantum_free_instance(inst); |
---|
| 592 | + } |
---|
643 | 593 | return err; |
---|
644 | 594 | } |
---|
645 | 595 | |
---|
.. | .. |
---|
660 | 610 | crypto_unregister_template(&adiantum_tmpl); |
---|
661 | 611 | } |
---|
662 | 612 | |
---|
663 | | -module_init(adiantum_module_init); |
---|
| 613 | +subsys_initcall(adiantum_module_init); |
---|
664 | 614 | module_exit(adiantum_module_exit); |
---|
665 | 615 | |
---|
666 | 616 | MODULE_DESCRIPTION("Adiantum length-preserving encryption mode"); |
---|
667 | 617 | MODULE_LICENSE("GPL v2"); |
---|
668 | 618 | MODULE_AUTHOR("Eric Biggers <ebiggers@google.com>"); |
---|
669 | 619 | MODULE_ALIAS_CRYPTO("adiantum"); |
---|
| 620 | +MODULE_IMPORT_NS(CRYPTO_INTERNAL); |
---|