diff options
| author | Jussi Kivilinna <jussi.kivilinna@mbnet.fi> | 2012-06-18 07:06:58 -0400 |
|---|---|---|
| committer | Herbert Xu <herbert@gondor.apana.org.au> | 2012-06-27 02:42:00 -0400 |
| commit | ffaf9156320a077ebf9c5b9a5cf987689dc1a6b3 (patch) | |
| tree | 22a3602bdedfc8809ff09bc9241b98ae3264ff13 | |
| parent | 7c76bdd7c3baf6d2431bb801f5b11d2ac195fdd6 (diff) | |
crypto: ablk_helper - move ablk_* functions from serpent-sse2/avx glue code to shared module
Move ablk-* functions to separate module to share common code between cipher
implementations.
Signed-off-by: Jussi Kivilinna <jussi.kivilinna@mbnet.fi>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
| -rw-r--r-- | arch/x86/crypto/Makefile | 2 | ||||
| -rw-r--r-- | arch/x86/crypto/ablk_helper.c | 150 | ||||
| -rw-r--r-- | arch/x86/crypto/serpent_avx_glue.c | 115 | ||||
| -rw-r--r-- | arch/x86/crypto/serpent_sse2_glue.c | 115 | ||||
| -rw-r--r-- | arch/x86/include/asm/crypto/ablk_helper.h | 29 | ||||
| -rw-r--r-- | crypto/Kconfig | 8 |
6 files changed, 201 insertions, 218 deletions
diff --git a/arch/x86/crypto/Makefile b/arch/x86/crypto/Makefile index 83caa4b948c8..ad746916f912 100644 --- a/arch/x86/crypto/Makefile +++ b/arch/x86/crypto/Makefile | |||
| @@ -2,6 +2,8 @@ | |||
| 2 | # Arch-specific CryptoAPI modules. | 2 | # Arch-specific CryptoAPI modules. |
| 3 | # | 3 | # |
| 4 | 4 | ||
| 5 | obj-$(CONFIG_CRYPTO_ABLK_HELPER_X86) += ablk_helper.o | ||
| 6 | |||
| 5 | obj-$(CONFIG_CRYPTO_AES_586) += aes-i586.o | 7 | obj-$(CONFIG_CRYPTO_AES_586) += aes-i586.o |
| 6 | obj-$(CONFIG_CRYPTO_TWOFISH_586) += twofish-i586.o | 8 | obj-$(CONFIG_CRYPTO_TWOFISH_586) += twofish-i586.o |
| 7 | obj-$(CONFIG_CRYPTO_SALSA20_586) += salsa20-i586.o | 9 | obj-$(CONFIG_CRYPTO_SALSA20_586) += salsa20-i586.o |
diff --git a/arch/x86/crypto/ablk_helper.c b/arch/x86/crypto/ablk_helper.c new file mode 100644 index 000000000000..284ca3bc2383 --- /dev/null +++ b/arch/x86/crypto/ablk_helper.c | |||
| @@ -0,0 +1,150 @@ | |||
| 1 | /* | ||
| 2 | * Shared async block cipher helpers | ||
| 3 | * | ||
| 4 | * Copyright (c) 2012 Jussi Kivilinna <jussi.kivilinna@mbnet.fi> | ||
| 5 | * | ||
| 6 | * Based on aesni-intel_glue.c by: | ||
| 7 | * Copyright (C) 2008, Intel Corp. | ||
| 8 | * Author: Huang Ying <ying.huang@intel.com> | ||
| 9 | * | ||
| 10 | * This program is free software; you can redistribute it and/or modify | ||
| 11 | * it under the terms of the GNU General Public License as published by | ||
| 12 | * the Free Software Foundation; either version 2 of the License, or | ||
| 13 | * (at your option) any later version. | ||
| 14 | * | ||
| 15 | * This program is distributed in the hope that it will be useful, | ||
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 18 | * GNU General Public License for more details. | ||
| 19 | * | ||
| 20 | * You should have received a copy of the GNU General Public License | ||
| 21 | * along with this program; if not, write to the Free Software | ||
| 22 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 | ||
| 23 | * USA | ||
| 24 | * | ||
| 25 | */ | ||
| 26 | |||
| 27 | #include <linux/kernel.h> | ||
| 28 | #include <linux/crypto.h> | ||
| 29 | #include <linux/init.h> | ||
| 30 | #include <linux/module.h> | ||
| 31 | #include <crypto/algapi.h> | ||
| 32 | #include <crypto/cryptd.h> | ||
| 33 | #include <asm/i387.h> | ||
| 34 | #include <asm/crypto/ablk_helper.h> | ||
| 35 | |||
| 36 | int ablk_set_key(struct crypto_ablkcipher *tfm, const u8 *key, | ||
| 37 | unsigned int key_len) | ||
| 38 | { | ||
| 39 | struct async_helper_ctx *ctx = crypto_ablkcipher_ctx(tfm); | ||
| 40 | struct crypto_ablkcipher *child = &ctx->cryptd_tfm->base; | ||
| 41 | int err; | ||
| 42 | |||
| 43 | crypto_ablkcipher_clear_flags(child, CRYPTO_TFM_REQ_MASK); | ||
| 44 | crypto_ablkcipher_set_flags(child, crypto_ablkcipher_get_flags(tfm) | ||
| 45 | & CRYPTO_TFM_REQ_MASK); | ||
| 46 | err = crypto_ablkcipher_setkey(child, key, key_len); | ||
| 47 | crypto_ablkcipher_set_flags(tfm, crypto_ablkcipher_get_flags(child) | ||
| 48 | & CRYPTO_TFM_RES_MASK); | ||
| 49 | return err; | ||
| 50 | } | ||
| 51 | EXPORT_SYMBOL_GPL(ablk_set_key); | ||
| 52 | |||
| 53 | int __ablk_encrypt(struct ablkcipher_request *req) | ||
| 54 | { | ||
| 55 | struct crypto_ablkcipher *tfm = crypto_ablkcipher_reqtfm(req); | ||
| 56 | struct async_helper_ctx *ctx = crypto_ablkcipher_ctx(tfm); | ||
| 57 | struct blkcipher_desc desc; | ||
| 58 | |||
| 59 | desc.tfm = cryptd_ablkcipher_child(ctx->cryptd_tfm); | ||
| 60 | desc.info = req->info; | ||
| 61 | desc.flags = 0; | ||
| 62 | |||
| 63 | return crypto_blkcipher_crt(desc.tfm)->encrypt( | ||
| 64 | &desc, req->dst, req->src, req->nbytes); | ||
| 65 | } | ||
| 66 | EXPORT_SYMBOL_GPL(__ablk_encrypt); | ||
| 67 | |||
| 68 | int ablk_encrypt(struct ablkcipher_request *req) | ||
| 69 | { | ||
| 70 | struct crypto_ablkcipher *tfm = crypto_ablkcipher_reqtfm(req); | ||
| 71 | struct async_helper_ctx *ctx = crypto_ablkcipher_ctx(tfm); | ||
| 72 | |||
| 73 | if (!irq_fpu_usable()) { | ||
| 74 | struct ablkcipher_request *cryptd_req = | ||
| 75 | ablkcipher_request_ctx(req); | ||
| 76 | |||
| 77 | memcpy(cryptd_req, req, sizeof(*req)); | ||
| 78 | ablkcipher_request_set_tfm(cryptd_req, &ctx->cryptd_tfm->base); | ||
| 79 | |||
| 80 | return crypto_ablkcipher_encrypt(cryptd_req); | ||
| 81 | } else { | ||
| 82 | return __ablk_encrypt(req); | ||
| 83 | } | ||
| 84 | } | ||
| 85 | EXPORT_SYMBOL_GPL(ablk_encrypt); | ||
| 86 | |||
| 87 | int ablk_decrypt(struct ablkcipher_request *req) | ||
| 88 | { | ||
| 89 | struct crypto_ablkcipher *tfm = crypto_ablkcipher_reqtfm(req); | ||
| 90 | struct async_helper_ctx *ctx = crypto_ablkcipher_ctx(tfm); | ||
| 91 | |||
| 92 | if (!irq_fpu_usable()) { | ||
| 93 | struct ablkcipher_request *cryptd_req = | ||
| 94 | ablkcipher_request_ctx(req); | ||
| 95 | |||
| 96 | memcpy(cryptd_req, req, sizeof(*req)); | ||
| 97 | ablkcipher_request_set_tfm(cryptd_req, &ctx->cryptd_tfm->base); | ||
| 98 | |||
| 99 | return crypto_ablkcipher_decrypt(cryptd_req); | ||
| 100 | } else { | ||
| 101 | struct blkcipher_desc desc; | ||
| 102 | |||
| 103 | desc.tfm = cryptd_ablkcipher_child(ctx->cryptd_tfm); | ||
| 104 | desc.info = req->info; | ||
| 105 | desc.flags = 0; | ||
| 106 | |||
| 107 | return crypto_blkcipher_crt(desc.tfm)->decrypt( | ||
| 108 | &desc, req->dst, req->src, req->nbytes); | ||
| 109 | } | ||
| 110 | } | ||
| 111 | EXPORT_SYMBOL_GPL(ablk_decrypt); | ||
| 112 | |||
| 113 | void ablk_exit(struct crypto_tfm *tfm) | ||
| 114 | { | ||
| 115 | struct async_helper_ctx *ctx = crypto_tfm_ctx(tfm); | ||
| 116 | |||
| 117 | cryptd_free_ablkcipher(ctx->cryptd_tfm); | ||
| 118 | } | ||
| 119 | EXPORT_SYMBOL_GPL(ablk_exit); | ||
| 120 | |||
| 121 | void ablk_init_common(struct crypto_tfm *tfm, | ||
| 122 | struct cryptd_ablkcipher *cryptd_tfm) | ||
| 123 | { | ||
| 124 | struct async_helper_ctx *ctx = crypto_tfm_ctx(tfm); | ||
| 125 | |||
| 126 | ctx->cryptd_tfm = cryptd_tfm; | ||
| 127 | tfm->crt_ablkcipher.reqsize = sizeof(struct ablkcipher_request) + | ||
| 128 | crypto_ablkcipher_reqsize(&cryptd_tfm->base); | ||
| 129 | } | ||
| 130 | EXPORT_SYMBOL_GPL(ablk_init_common); | ||
| 131 | |||
| 132 | int ablk_init(struct crypto_tfm *tfm) | ||
| 133 | { | ||
| 134 | struct cryptd_ablkcipher *cryptd_tfm; | ||
| 135 | char drv_name[CRYPTO_MAX_ALG_NAME]; | ||
| 136 | |||
| 137 | snprintf(drv_name, sizeof(drv_name), "__driver-%s", | ||
| 138 | crypto_tfm_alg_driver_name(tfm)); | ||
| 139 | |||
| 140 | cryptd_tfm = cryptd_alloc_ablkcipher(drv_name, 0, 0); | ||
| 141 | if (IS_ERR(cryptd_tfm)) | ||
| 142 | return PTR_ERR(cryptd_tfm); | ||
| 143 | |||
| 144 | ablk_init_common(tfm, cryptd_tfm); | ||
| 145 | |||
| 146 | return 0; | ||
| 147 | } | ||
| 148 | EXPORT_SYMBOL_GPL(ablk_init); | ||
| 149 | |||
| 150 | MODULE_LICENSE("GPL"); | ||
diff --git a/arch/x86/crypto/serpent_avx_glue.c b/arch/x86/crypto/serpent_avx_glue.c index dd81bab4f11a..31eb567cc893 100644 --- a/arch/x86/crypto/serpent_avx_glue.c +++ b/arch/x86/crypto/serpent_avx_glue.c | |||
| @@ -40,14 +40,11 @@ | |||
| 40 | #include <asm/xcr.h> | 40 | #include <asm/xcr.h> |
| 41 | #include <asm/xsave.h> | 41 | #include <asm/xsave.h> |
| 42 | #include <asm/serpent-avx.h> | 42 | #include <asm/serpent-avx.h> |
| 43 | #include <asm/crypto/ablk_helper.h> | ||
| 43 | #include <crypto/scatterwalk.h> | 44 | #include <crypto/scatterwalk.h> |
| 44 | #include <linux/workqueue.h> | 45 | #include <linux/workqueue.h> |
| 45 | #include <linux/spinlock.h> | 46 | #include <linux/spinlock.h> |
| 46 | 47 | ||
| 47 | struct async_serpent_ctx { | ||
| 48 | struct cryptd_ablkcipher *cryptd_tfm; | ||
| 49 | }; | ||
| 50 | |||
| 51 | static inline bool serpent_fpu_begin(bool fpu_enabled, unsigned int nbytes) | 48 | static inline bool serpent_fpu_begin(bool fpu_enabled, unsigned int nbytes) |
| 52 | { | 49 | { |
| 53 | if (fpu_enabled) | 50 | if (fpu_enabled) |
| @@ -593,106 +590,6 @@ static int xts_decrypt(struct blkcipher_desc *desc, struct scatterlist *dst, | |||
| 593 | return ret; | 590 | return ret; |
| 594 | } | 591 | } |
| 595 | 592 | ||
| 596 | static int ablk_set_key(struct crypto_ablkcipher *tfm, const u8 *key, | ||
| 597 | unsigned int key_len) | ||
| 598 | { | ||
| 599 | struct async_serpent_ctx *ctx = crypto_ablkcipher_ctx(tfm); | ||
| 600 | struct crypto_ablkcipher *child = &ctx->cryptd_tfm->base; | ||
| 601 | int err; | ||
| 602 | |||
| 603 | crypto_ablkcipher_clear_flags(child, CRYPTO_TFM_REQ_MASK); | ||
| 604 | crypto_ablkcipher_set_flags(child, crypto_ablkcipher_get_flags(tfm) | ||
| 605 | & CRYPTO_TFM_REQ_MASK); | ||
| 606 | err = crypto_ablkcipher_setkey(child, key, key_len); | ||
| 607 | crypto_ablkcipher_set_flags(tfm, crypto_ablkcipher_get_flags(child) | ||
| 608 | & CRYPTO_TFM_RES_MASK); | ||
| 609 | return err; | ||
| 610 | } | ||
| 611 | |||
| 612 | static int __ablk_encrypt(struct ablkcipher_request *req) | ||
| 613 | { | ||
| 614 | struct crypto_ablkcipher *tfm = crypto_ablkcipher_reqtfm(req); | ||
| 615 | struct async_serpent_ctx *ctx = crypto_ablkcipher_ctx(tfm); | ||
| 616 | struct blkcipher_desc desc; | ||
| 617 | |||
| 618 | desc.tfm = cryptd_ablkcipher_child(ctx->cryptd_tfm); | ||
| 619 | desc.info = req->info; | ||
| 620 | desc.flags = 0; | ||
| 621 | |||
| 622 | return crypto_blkcipher_crt(desc.tfm)->encrypt( | ||
| 623 | &desc, req->dst, req->src, req->nbytes); | ||
| 624 | } | ||
| 625 | |||
| 626 | static int ablk_encrypt(struct ablkcipher_request *req) | ||
| 627 | { | ||
| 628 | struct crypto_ablkcipher *tfm = crypto_ablkcipher_reqtfm(req); | ||
| 629 | struct async_serpent_ctx *ctx = crypto_ablkcipher_ctx(tfm); | ||
| 630 | |||
| 631 | if (!irq_fpu_usable()) { | ||
| 632 | struct ablkcipher_request *cryptd_req = | ||
| 633 | ablkcipher_request_ctx(req); | ||
| 634 | |||
| 635 | memcpy(cryptd_req, req, sizeof(*req)); | ||
| 636 | ablkcipher_request_set_tfm(cryptd_req, &ctx->cryptd_tfm->base); | ||
| 637 | |||
| 638 | return crypto_ablkcipher_encrypt(cryptd_req); | ||
| 639 | } else { | ||
| 640 | return __ablk_encrypt(req); | ||
| 641 | } | ||
| 642 | } | ||
| 643 | |||
| 644 | static int ablk_decrypt(struct ablkcipher_request *req) | ||
| 645 | { | ||
| 646 | struct crypto_ablkcipher *tfm = crypto_ablkcipher_reqtfm(req); | ||
| 647 | struct async_serpent_ctx *ctx = crypto_ablkcipher_ctx(tfm); | ||
| 648 | |||
| 649 | if (!irq_fpu_usable()) { | ||
| 650 | struct ablkcipher_request *cryptd_req = | ||
| 651 | ablkcipher_request_ctx(req); | ||
| 652 | |||
| 653 | memcpy(cryptd_req, req, sizeof(*req)); | ||
| 654 | ablkcipher_request_set_tfm(cryptd_req, &ctx->cryptd_tfm->base); | ||
| 655 | |||
| 656 | return crypto_ablkcipher_decrypt(cryptd_req); | ||
| 657 | } else { | ||
| 658 | struct blkcipher_desc desc; | ||
| 659 | |||
| 660 | desc.tfm = cryptd_ablkcipher_child(ctx->cryptd_tfm); | ||
| 661 | desc.info = req->info; | ||
| 662 | desc.flags = 0; | ||
| 663 | |||
| 664 | return crypto_blkcipher_crt(desc.tfm)->decrypt( | ||
| 665 | &desc, req->dst, req->src, req->nbytes); | ||
| 666 | } | ||
| 667 | } | ||
| 668 | |||
| 669 | static void ablk_exit(struct crypto_tfm *tfm) | ||
| 670 | { | ||
| 671 | struct async_serpent_ctx *ctx = crypto_tfm_ctx(tfm); | ||
| 672 | |||
| 673 | cryptd_free_ablkcipher(ctx->cryptd_tfm); | ||
| 674 | } | ||
| 675 | |||
| 676 | static int ablk_init(struct crypto_tfm *tfm) | ||
| 677 | { | ||
| 678 | struct async_serpent_ctx *ctx = crypto_tfm_ctx(tfm); | ||
| 679 | struct cryptd_ablkcipher *cryptd_tfm; | ||
| 680 | char drv_name[CRYPTO_MAX_ALG_NAME]; | ||
| 681 | |||
| 682 | snprintf(drv_name, sizeof(drv_name), "__driver-%s", | ||
| 683 | crypto_tfm_alg_driver_name(tfm)); | ||
| 684 | |||
| 685 | cryptd_tfm = cryptd_alloc_ablkcipher(drv_name, 0, 0); | ||
| 686 | if (IS_ERR(cryptd_tfm)) | ||
| 687 | return PTR_ERR(cryptd_tfm); | ||
| 688 | |||
| 689 | ctx->cryptd_tfm = cryptd_tfm; | ||
| 690 | tfm->crt_ablkcipher.reqsize = sizeof(struct ablkcipher_request) + | ||
| 691 | crypto_ablkcipher_reqsize(&cryptd_tfm->base); | ||
| 692 | |||
| 693 | return 0; | ||
| 694 | } | ||
| 695 | |||
| 696 | static struct crypto_alg serpent_algs[10] = { { | 593 | static struct crypto_alg serpent_algs[10] = { { |
| 697 | .cra_name = "__ecb-serpent-avx", | 594 | .cra_name = "__ecb-serpent-avx", |
| 698 | .cra_driver_name = "__driver-ecb-serpent-avx", | 595 | .cra_driver_name = "__driver-ecb-serpent-avx", |
| @@ -805,7 +702,7 @@ static struct crypto_alg serpent_algs[10] = { { | |||
| 805 | .cra_priority = 500, | 702 | .cra_priority = 500, |
| 806 | .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC, | 703 | .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC, |
| 807 | .cra_blocksize = SERPENT_BLOCK_SIZE, | 704 | .cra_blocksize = SERPENT_BLOCK_SIZE, |
| 808 | .cra_ctxsize = sizeof(struct async_serpent_ctx), | 705 | .cra_ctxsize = sizeof(struct async_helper_ctx), |
| 809 | .cra_alignmask = 0, | 706 | .cra_alignmask = 0, |
| 810 | .cra_type = &crypto_ablkcipher_type, | 707 | .cra_type = &crypto_ablkcipher_type, |
| 811 | .cra_module = THIS_MODULE, | 708 | .cra_module = THIS_MODULE, |
| @@ -827,7 +724,7 @@ static struct crypto_alg serpent_algs[10] = { { | |||
| 827 | .cra_priority = 500, | 724 | .cra_priority = 500, |
| 828 | .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC, | 725 | .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC, |
| 829 | .cra_blocksize = SERPENT_BLOCK_SIZE, | 726 | .cra_blocksize = SERPENT_BLOCK_SIZE, |
| 830 | .cra_ctxsize = sizeof(struct async_serpent_ctx), | 727 | .cra_ctxsize = sizeof(struct async_helper_ctx), |
| 831 | .cra_alignmask = 0, | 728 | .cra_alignmask = 0, |
| 832 | .cra_type = &crypto_ablkcipher_type, | 729 | .cra_type = &crypto_ablkcipher_type, |
| 833 | .cra_module = THIS_MODULE, | 730 | .cra_module = THIS_MODULE, |
| @@ -850,7 +747,7 @@ static struct crypto_alg serpent_algs[10] = { { | |||
| 850 | .cra_priority = 500, | 747 | .cra_priority = 500, |
| 851 | .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC, | 748 | .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC, |
| 852 | .cra_blocksize = 1, | 749 | .cra_blocksize = 1, |
| 853 | .cra_ctxsize = sizeof(struct async_serpent_ctx), | 750 | .cra_ctxsize = sizeof(struct async_helper_ctx), |
| 854 | .cra_alignmask = 0, | 751 | .cra_alignmask = 0, |
| 855 | .cra_type = &crypto_ablkcipher_type, | 752 | .cra_type = &crypto_ablkcipher_type, |
| 856 | .cra_module = THIS_MODULE, | 753 | .cra_module = THIS_MODULE, |
| @@ -874,7 +771,7 @@ static struct crypto_alg serpent_algs[10] = { { | |||
| 874 | .cra_priority = 500, | 771 | .cra_priority = 500, |
| 875 | .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC, | 772 | .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC, |
| 876 | .cra_blocksize = SERPENT_BLOCK_SIZE, | 773 | .cra_blocksize = SERPENT_BLOCK_SIZE, |
| 877 | .cra_ctxsize = sizeof(struct async_serpent_ctx), | 774 | .cra_ctxsize = sizeof(struct async_helper_ctx), |
| 878 | .cra_alignmask = 0, | 775 | .cra_alignmask = 0, |
| 879 | .cra_type = &crypto_ablkcipher_type, | 776 | .cra_type = &crypto_ablkcipher_type, |
| 880 | .cra_module = THIS_MODULE, | 777 | .cra_module = THIS_MODULE, |
| @@ -899,7 +796,7 @@ static struct crypto_alg serpent_algs[10] = { { | |||
| 899 | .cra_priority = 500, | 796 | .cra_priority = 500, |
| 900 | .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC, | 797 | .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC, |
| 901 | .cra_blocksize = SERPENT_BLOCK_SIZE, | 798 | .cra_blocksize = SERPENT_BLOCK_SIZE, |
| 902 | .cra_ctxsize = sizeof(struct async_serpent_ctx), | 799 | .cra_ctxsize = sizeof(struct async_helper_ctx), |
| 903 | .cra_alignmask = 0, | 800 | .cra_alignmask = 0, |
| 904 | .cra_type = &crypto_ablkcipher_type, | 801 | .cra_type = &crypto_ablkcipher_type, |
| 905 | .cra_module = THIS_MODULE, | 802 | .cra_module = THIS_MODULE, |
diff --git a/arch/x86/crypto/serpent_sse2_glue.c b/arch/x86/crypto/serpent_sse2_glue.c index deecd25c1299..805c91fda7a2 100644 --- a/arch/x86/crypto/serpent_sse2_glue.c +++ b/arch/x86/crypto/serpent_sse2_glue.c | |||
| @@ -43,14 +43,11 @@ | |||
| 43 | #include <crypto/xts.h> | 43 | #include <crypto/xts.h> |
| 44 | #include <asm/i387.h> | 44 | #include <asm/i387.h> |
| 45 | #include <asm/serpent-sse2.h> | 45 | #include <asm/serpent-sse2.h> |
| 46 | #include <asm/crypto/ablk_helper.h> | ||
| 46 | #include <crypto/scatterwalk.h> | 47 | #include <crypto/scatterwalk.h> |
| 47 | #include <linux/workqueue.h> | 48 | #include <linux/workqueue.h> |
| 48 | #include <linux/spinlock.h> | 49 | #include <linux/spinlock.h> |
| 49 | 50 | ||
| 50 | struct async_serpent_ctx { | ||
| 51 | struct cryptd_ablkcipher *cryptd_tfm; | ||
| 52 | }; | ||
| 53 | |||
| 54 | static inline bool serpent_fpu_begin(bool fpu_enabled, unsigned int nbytes) | 51 | static inline bool serpent_fpu_begin(bool fpu_enabled, unsigned int nbytes) |
| 55 | { | 52 | { |
| 56 | if (fpu_enabled) | 53 | if (fpu_enabled) |
| @@ -596,106 +593,6 @@ static int xts_decrypt(struct blkcipher_desc *desc, struct scatterlist *dst, | |||
| 596 | return ret; | 593 | return ret; |
| 597 | } | 594 | } |
| 598 | 595 | ||
| 599 | static int ablk_set_key(struct crypto_ablkcipher *tfm, const u8 *key, | ||
| 600 | unsigned int key_len) | ||
| 601 | { | ||
| 602 | struct async_serpent_ctx *ctx = crypto_ablkcipher_ctx(tfm); | ||
| 603 | struct crypto_ablkcipher *child = &ctx->cryptd_tfm->base; | ||
| 604 | int err; | ||
| 605 | |||
| 606 | crypto_ablkcipher_clear_flags(child, CRYPTO_TFM_REQ_MASK); | ||
| 607 | crypto_ablkcipher_set_flags(child, crypto_ablkcipher_get_flags(tfm) | ||
| 608 | & CRYPTO_TFM_REQ_MASK); | ||
| 609 | err = crypto_ablkcipher_setkey(child, key, key_len); | ||
| 610 | crypto_ablkcipher_set_flags(tfm, crypto_ablkcipher_get_flags(child) | ||
| 611 | & CRYPTO_TFM_RES_MASK); | ||
| 612 | return err; | ||
| 613 | } | ||
| 614 | |||
| 615 | static int __ablk_encrypt(struct ablkcipher_request *req) | ||
| 616 | { | ||
| 617 | struct crypto_ablkcipher *tfm = crypto_ablkcipher_reqtfm(req); | ||
| 618 | struct async_serpent_ctx *ctx = crypto_ablkcipher_ctx(tfm); | ||
| 619 | struct blkcipher_desc desc; | ||
| 620 | |||
| 621 | desc.tfm = cryptd_ablkcipher_child(ctx->cryptd_tfm); | ||
| 622 | desc.info = req->info; | ||
| 623 | desc.flags = 0; | ||
| 624 | |||
| 625 | return crypto_blkcipher_crt(desc.tfm)->encrypt( | ||
| 626 | &desc, req->dst, req->src, req->nbytes); | ||
| 627 | } | ||
| 628 | |||
| 629 | static int ablk_encrypt(struct ablkcipher_request *req) | ||
| 630 | { | ||
| 631 | struct crypto_ablkcipher *tfm = crypto_ablkcipher_reqtfm(req); | ||
| 632 | struct async_serpent_ctx *ctx = crypto_ablkcipher_ctx(tfm); | ||
| 633 | |||
| 634 | if (!irq_fpu_usable()) { | ||
| 635 | struct ablkcipher_request *cryptd_req = | ||
| 636 | ablkcipher_request_ctx(req); | ||
| 637 | |||
| 638 | memcpy(cryptd_req, req, sizeof(*req)); | ||
| 639 | ablkcipher_request_set_tfm(cryptd_req, &ctx->cryptd_tfm->base); | ||
| 640 | |||
| 641 | return crypto_ablkcipher_encrypt(cryptd_req); | ||
| 642 | } else { | ||
| 643 | return __ablk_encrypt(req); | ||
| 644 | } | ||
| 645 | } | ||
| 646 | |||
| 647 | static int ablk_decrypt(struct ablkcipher_request *req) | ||
| 648 | { | ||
| 649 | struct crypto_ablkcipher *tfm = crypto_ablkcipher_reqtfm(req); | ||
| 650 | struct async_serpent_ctx *ctx = crypto_ablkcipher_ctx(tfm); | ||
| 651 | |||
| 652 | if (!irq_fpu_usable()) { | ||
| 653 | struct ablkcipher_request *cryptd_req = | ||
| 654 | ablkcipher_request_ctx(req); | ||
| 655 | |||
| 656 | memcpy(cryptd_req, req, sizeof(*req)); | ||
| 657 | ablkcipher_request_set_tfm(cryptd_req, &ctx->cryptd_tfm->base); | ||
| 658 | |||
| 659 | return crypto_ablkcipher_decrypt(cryptd_req); | ||
| 660 | } else { | ||
| 661 | struct blkcipher_desc desc; | ||
| 662 | |||
| 663 | desc.tfm = cryptd_ablkcipher_child(ctx->cryptd_tfm); | ||
| 664 | desc.info = req->info; | ||
| 665 | desc.flags = 0; | ||
| 666 | |||
| 667 | return crypto_blkcipher_crt(desc.tfm)->decrypt( | ||
| 668 | &desc, req->dst, req->src, req->nbytes); | ||
| 669 | } | ||
| 670 | } | ||
| 671 | |||
| 672 | static void ablk_exit(struct crypto_tfm *tfm) | ||
| 673 | { | ||
| 674 | struct async_serpent_ctx *ctx = crypto_tfm_ctx(tfm); | ||
| 675 | |||
| 676 | cryptd_free_ablkcipher(ctx->cryptd_tfm); | ||
| 677 | } | ||
| 678 | |||
| 679 | static int ablk_init(struct crypto_tfm *tfm) | ||
| 680 | { | ||
| 681 | struct async_serpent_ctx *ctx = crypto_tfm_ctx(tfm); | ||
| 682 | struct cryptd_ablkcipher *cryptd_tfm; | ||
| 683 | char drv_name[CRYPTO_MAX_ALG_NAME]; | ||
| 684 | |||
| 685 | snprintf(drv_name, sizeof(drv_name), "__driver-%s", | ||
| 686 | crypto_tfm_alg_driver_name(tfm)); | ||
| 687 | |||
| 688 | cryptd_tfm = cryptd_alloc_ablkcipher(drv_name, 0, 0); | ||
| 689 | if (IS_ERR(cryptd_tfm)) | ||
| 690 | return PTR_ERR(cryptd_tfm); | ||
| 691 | |||
| 692 | ctx->cryptd_tfm = cryptd_tfm; | ||
| 693 | tfm->crt_ablkcipher.reqsize = sizeof(struct ablkcipher_request) + | ||
| 694 | crypto_ablkcipher_reqsize(&cryptd_tfm->base); | ||
| 695 | |||
| 696 | return 0; | ||
| 697 | } | ||
| 698 | |||
| 699 | static struct crypto_alg serpent_algs[10] = { { | 596 | static struct crypto_alg serpent_algs[10] = { { |
| 700 | .cra_name = "__ecb-serpent-sse2", | 597 | .cra_name = "__ecb-serpent-sse2", |
| 701 | .cra_driver_name = "__driver-ecb-serpent-sse2", | 598 | .cra_driver_name = "__driver-ecb-serpent-sse2", |
| @@ -808,7 +705,7 @@ static struct crypto_alg serpent_algs[10] = { { | |||
| 808 | .cra_priority = 400, | 705 | .cra_priority = 400, |
| 809 | .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC, | 706 | .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC, |
| 810 | .cra_blocksize = SERPENT_BLOCK_SIZE, | 707 | .cra_blocksize = SERPENT_BLOCK_SIZE, |
| 811 | .cra_ctxsize = sizeof(struct async_serpent_ctx), | 708 | .cra_ctxsize = sizeof(struct async_helper_ctx), |
| 812 | .cra_alignmask = 0, | 709 | .cra_alignmask = 0, |
| 813 | .cra_type = &crypto_ablkcipher_type, | 710 | .cra_type = &crypto_ablkcipher_type, |
| 814 | .cra_module = THIS_MODULE, | 711 | .cra_module = THIS_MODULE, |
| @@ -830,7 +727,7 @@ static struct crypto_alg serpent_algs[10] = { { | |||
| 830 | .cra_priority = 400, | 727 | .cra_priority = 400, |
| 831 | .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC, | 728 | .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC, |
| 832 | .cra_blocksize = SERPENT_BLOCK_SIZE, | 729 | .cra_blocksize = SERPENT_BLOCK_SIZE, |
| 833 | .cra_ctxsize = sizeof(struct async_serpent_ctx), | 730 | .cra_ctxsize = sizeof(struct async_helper_ctx), |
| 834 | .cra_alignmask = 0, | 731 | .cra_alignmask = 0, |
| 835 | .cra_type = &crypto_ablkcipher_type, | 732 | .cra_type = &crypto_ablkcipher_type, |
| 836 | .cra_module = THIS_MODULE, | 733 | .cra_module = THIS_MODULE, |
| @@ -853,7 +750,7 @@ static struct crypto_alg serpent_algs[10] = { { | |||
| 853 | .cra_priority = 400, | 750 | .cra_priority = 400, |
| 854 | .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC, | 751 | .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC, |
| 855 | .cra_blocksize = 1, | 752 | .cra_blocksize = 1, |
| 856 | .cra_ctxsize = sizeof(struct async_serpent_ctx), | 753 | .cra_ctxsize = sizeof(struct async_helper_ctx), |
| 857 | .cra_alignmask = 0, | 754 | .cra_alignmask = 0, |
| 858 | .cra_type = &crypto_ablkcipher_type, | 755 | .cra_type = &crypto_ablkcipher_type, |
| 859 | .cra_module = THIS_MODULE, | 756 | .cra_module = THIS_MODULE, |
| @@ -877,7 +774,7 @@ static struct crypto_alg serpent_algs[10] = { { | |||
| 877 | .cra_priority = 400, | 774 | .cra_priority = 400, |
| 878 | .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC, | 775 | .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC, |
| 879 | .cra_blocksize = SERPENT_BLOCK_SIZE, | 776 | .cra_blocksize = SERPENT_BLOCK_SIZE, |
| 880 | .cra_ctxsize = sizeof(struct async_serpent_ctx), | 777 | .cra_ctxsize = sizeof(struct async_helper_ctx), |
| 881 | .cra_alignmask = 0, | 778 | .cra_alignmask = 0, |
| 882 | .cra_type = &crypto_ablkcipher_type, | 779 | .cra_type = &crypto_ablkcipher_type, |
| 883 | .cra_module = THIS_MODULE, | 780 | .cra_module = THIS_MODULE, |
| @@ -902,7 +799,7 @@ static struct crypto_alg serpent_algs[10] = { { | |||
| 902 | .cra_priority = 400, | 799 | .cra_priority = 400, |
| 903 | .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC, | 800 | .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC, |
| 904 | .cra_blocksize = SERPENT_BLOCK_SIZE, | 801 | .cra_blocksize = SERPENT_BLOCK_SIZE, |
| 905 | .cra_ctxsize = sizeof(struct async_serpent_ctx), | 802 | .cra_ctxsize = sizeof(struct async_helper_ctx), |
| 906 | .cra_alignmask = 0, | 803 | .cra_alignmask = 0, |
| 907 | .cra_type = &crypto_ablkcipher_type, | 804 | .cra_type = &crypto_ablkcipher_type, |
| 908 | .cra_module = THIS_MODULE, | 805 | .cra_module = THIS_MODULE, |
diff --git a/arch/x86/include/asm/crypto/ablk_helper.h b/arch/x86/include/asm/crypto/ablk_helper.h new file mode 100644 index 000000000000..6d6b37c6f5d3 --- /dev/null +++ b/arch/x86/include/asm/crypto/ablk_helper.h | |||
| @@ -0,0 +1,29 @@ | |||
| 1 | /* | ||
| 2 | * Shared async block cipher helpers | ||
| 3 | */ | ||
| 4 | |||
| 5 | #ifndef _CRYPTO_ABLK_HELPER_H | ||
| 6 | #define _CRYPTO_ABLK_HELPER_H | ||
| 7 | |||
| 8 | #include <linux/crypto.h> | ||
| 9 | #include <linux/kernel.h> | ||
| 10 | #include <crypto/cryptd.h> | ||
| 11 | |||
| 12 | struct async_helper_ctx { | ||
| 13 | struct cryptd_ablkcipher *cryptd_tfm; | ||
| 14 | }; | ||
| 15 | |||
| 16 | extern int ablk_set_key(struct crypto_ablkcipher *tfm, const u8 *key, | ||
| 17 | unsigned int key_len); | ||
| 18 | |||
| 19 | extern int __ablk_encrypt(struct ablkcipher_request *req); | ||
| 20 | |||
| 21 | extern int ablk_encrypt(struct ablkcipher_request *req); | ||
| 22 | |||
| 23 | extern int ablk_decrypt(struct ablkcipher_request *req); | ||
| 24 | |||
| 25 | extern void ablk_exit(struct crypto_tfm *tfm); | ||
| 26 | |||
| 27 | extern int ablk_init(struct crypto_tfm *tfm); | ||
| 28 | |||
| 29 | #endif /* _CRYPTO_ABLK_HELPER_H */ | ||
diff --git a/crypto/Kconfig b/crypto/Kconfig index 2c1c2dfcc02a..8e9145c07d87 100644 --- a/crypto/Kconfig +++ b/crypto/Kconfig | |||
| @@ -174,6 +174,11 @@ config CRYPTO_TEST | |||
| 174 | help | 174 | help |
| 175 | Quick & dirty crypto test module. | 175 | Quick & dirty crypto test module. |
| 176 | 176 | ||
| 177 | config CRYPTO_ABLK_HELPER_X86 | ||
| 178 | tristate | ||
| 179 | depends on X86 | ||
| 180 | select CRYPTO_CRYPTD | ||
| 181 | |||
| 177 | comment "Authenticated Encryption with Associated Data" | 182 | comment "Authenticated Encryption with Associated Data" |
| 178 | 183 | ||
| 179 | config CRYPTO_CCM | 184 | config CRYPTO_CCM |
| @@ -786,6 +791,7 @@ config CRYPTO_SERPENT_SSE2_X86_64 | |||
| 786 | depends on X86 && 64BIT | 791 | depends on X86 && 64BIT |
| 787 | select CRYPTO_ALGAPI | 792 | select CRYPTO_ALGAPI |
| 788 | select CRYPTO_CRYPTD | 793 | select CRYPTO_CRYPTD |
| 794 | select CRYPTO_ABLK_HELPER_X86 | ||
| 789 | select CRYPTO_SERPENT | 795 | select CRYPTO_SERPENT |
| 790 | select CRYPTO_LRW | 796 | select CRYPTO_LRW |
| 791 | select CRYPTO_XTS | 797 | select CRYPTO_XTS |
| @@ -806,6 +812,7 @@ config CRYPTO_SERPENT_SSE2_586 | |||
| 806 | depends on X86 && !64BIT | 812 | depends on X86 && !64BIT |
| 807 | select CRYPTO_ALGAPI | 813 | select CRYPTO_ALGAPI |
| 808 | select CRYPTO_CRYPTD | 814 | select CRYPTO_CRYPTD |
| 815 | select CRYPTO_ABLK_HELPER_X86 | ||
| 809 | select CRYPTO_SERPENT | 816 | select CRYPTO_SERPENT |
| 810 | select CRYPTO_LRW | 817 | select CRYPTO_LRW |
| 811 | select CRYPTO_XTS | 818 | select CRYPTO_XTS |
| @@ -826,6 +833,7 @@ config CRYPTO_SERPENT_AVX_X86_64 | |||
| 826 | depends on X86 && 64BIT | 833 | depends on X86 && 64BIT |
| 827 | select CRYPTO_ALGAPI | 834 | select CRYPTO_ALGAPI |
| 828 | select CRYPTO_CRYPTD | 835 | select CRYPTO_CRYPTD |
| 836 | select CRYPTO_ABLK_HELPER_X86 | ||
| 829 | select CRYPTO_SERPENT | 837 | select CRYPTO_SERPENT |
| 830 | select CRYPTO_LRW | 838 | select CRYPTO_LRW |
| 831 | select CRYPTO_XTS | 839 | select CRYPTO_XTS |
