crypto: x86/twofish - drop dependency on glue helper
authorArd Biesheuvel <ardb@kernel.org>
Tue, 5 Jan 2021 16:48:07 +0000 (17:48 +0100)
committerHerbert Xu <herbert@gondor.apana.org.au>
Thu, 14 Jan 2021 06:10:29 +0000 (17:10 +1100)
Replace the glue helper dependency with implementations of ECB and CBC
based on the new CPP macros, which avoid the need for indirect calls.

Acked-by: Eric Biggers <ebiggers@google.com>
Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
arch/x86/crypto/twofish_avx_glue.c
arch/x86/crypto/twofish_glue_3way.c
crypto/Kconfig

index 13f810b61034db30a6ab029a145c68c3644edd20..6ce198f808a5bea4cf1dd306235406ddaf60ff12 100644 (file)
 #include <crypto/algapi.h>
 #include <crypto/internal/simd.h>
 #include <crypto/twofish.h>
-#include <asm/crypto/glue_helper.h>
 #include <asm/crypto/twofish.h>
 
+#include "ecb_cbc_helpers.h"
+
 #define TWOFISH_PARALLEL_BLOCKS 8
 
 /* 8-way parallel cipher functions */
@@ -37,72 +38,38 @@ static inline void twofish_enc_blk_3way(const void *ctx, u8 *dst, const u8 *src)
        __twofish_enc_blk_3way(ctx, dst, src, false);
 }
 
-static const struct common_glue_ctx twofish_enc = {
-       .num_funcs = 3,
-       .fpu_blocks_limit = TWOFISH_PARALLEL_BLOCKS,
-
-       .funcs = { {
-               .num_blocks = TWOFISH_PARALLEL_BLOCKS,
-               .fn_u = { .ecb = twofish_ecb_enc_8way }
-       }, {
-               .num_blocks = 3,
-               .fn_u = { .ecb = twofish_enc_blk_3way }
-       }, {
-               .num_blocks = 1,
-               .fn_u = { .ecb = twofish_enc_blk }
-       } }
-};
-
-static const struct common_glue_ctx twofish_dec = {
-       .num_funcs = 3,
-       .fpu_blocks_limit = TWOFISH_PARALLEL_BLOCKS,
-
-       .funcs = { {
-               .num_blocks = TWOFISH_PARALLEL_BLOCKS,
-               .fn_u = { .ecb = twofish_ecb_dec_8way }
-       }, {
-               .num_blocks = 3,
-               .fn_u = { .ecb = twofish_dec_blk_3way }
-       }, {
-               .num_blocks = 1,
-               .fn_u = { .ecb = twofish_dec_blk }
-       } }
-};
-
-static const struct common_glue_ctx twofish_dec_cbc = {
-       .num_funcs = 3,
-       .fpu_blocks_limit = TWOFISH_PARALLEL_BLOCKS,
-
-       .funcs = { {
-               .num_blocks = TWOFISH_PARALLEL_BLOCKS,
-               .fn_u = { .cbc = twofish_cbc_dec_8way }
-       }, {
-               .num_blocks = 3,
-               .fn_u = { .cbc = twofish_dec_blk_cbc_3way }
-       }, {
-               .num_blocks = 1,
-               .fn_u = { .cbc = twofish_dec_blk }
-       } }
-};
-
 static int ecb_encrypt(struct skcipher_request *req)
 {
-       return glue_ecb_req_128bit(&twofish_enc, req);
+       ECB_WALK_START(req, TF_BLOCK_SIZE, TWOFISH_PARALLEL_BLOCKS);
+       ECB_BLOCK(TWOFISH_PARALLEL_BLOCKS, twofish_ecb_enc_8way);
+       ECB_BLOCK(3, twofish_enc_blk_3way);
+       ECB_BLOCK(1, twofish_enc_blk);
+       ECB_WALK_END();
 }
 
 static int ecb_decrypt(struct skcipher_request *req)
 {
-       return glue_ecb_req_128bit(&twofish_dec, req);
+       ECB_WALK_START(req, TF_BLOCK_SIZE, TWOFISH_PARALLEL_BLOCKS);
+       ECB_BLOCK(TWOFISH_PARALLEL_BLOCKS, twofish_ecb_dec_8way);
+       ECB_BLOCK(3, twofish_dec_blk_3way);
+       ECB_BLOCK(1, twofish_dec_blk);
+       ECB_WALK_END();
 }
 
 static int cbc_encrypt(struct skcipher_request *req)
 {
-       return glue_cbc_encrypt_req_128bit(twofish_enc_blk, req);
+       CBC_WALK_START(req, TF_BLOCK_SIZE, -1);
+       CBC_ENC_BLOCK(twofish_enc_blk);
+       CBC_WALK_END();
 }
 
 static int cbc_decrypt(struct skcipher_request *req)
 {
-       return glue_cbc_decrypt_req_128bit(&twofish_dec_cbc, req);
+       CBC_WALK_START(req, TF_BLOCK_SIZE, TWOFISH_PARALLEL_BLOCKS);
+       CBC_DEC_BLOCK(TWOFISH_PARALLEL_BLOCKS, twofish_cbc_dec_8way);
+       CBC_DEC_BLOCK(3, twofish_dec_blk_cbc_3way);
+       CBC_DEC_BLOCK(1, twofish_dec_blk);
+       CBC_WALK_END();
 }
 
 static struct skcipher_alg twofish_algs[] = {
index 88252370db0a56cb682224a883bf34f1ed19592e..d1fdefa5195ac22af94d8b66e5e2551c073eea60 100644 (file)
@@ -5,17 +5,16 @@
  * Copyright (c) 2011 Jussi Kivilinna <jussi.kivilinna@mbnet.fi>
  */
 
-#include <asm/crypto/glue_helper.h>
 #include <asm/crypto/twofish.h>
 #include <crypto/algapi.h>
-#include <crypto/b128ops.h>
-#include <crypto/internal/skcipher.h>
 #include <crypto/twofish.h>
 #include <linux/crypto.h>
 #include <linux/init.h>
 #include <linux/module.h>
 #include <linux/types.h>
 
+#include "ecb_cbc_helpers.h"
+
 EXPORT_SYMBOL_GPL(__twofish_enc_blk_3way);
 EXPORT_SYMBOL_GPL(twofish_dec_blk_3way);
 
@@ -30,79 +29,48 @@ static inline void twofish_enc_blk_3way(const void *ctx, u8 *dst, const u8 *src)
        __twofish_enc_blk_3way(ctx, dst, src, false);
 }
 
-void twofish_dec_blk_cbc_3way(const void *ctx, u8 *d, const u8 *s)
+void twofish_dec_blk_cbc_3way(const void *ctx, u8 *dst, const u8 *src)
 {
-       u128 ivs[2];
-       u128 *dst = (u128 *)d;
-       const u128 *src = (const u128 *)s;
-
-       ivs[0] = src[0];
-       ivs[1] = src[1];
+       u8 buf[2][TF_BLOCK_SIZE];
+       const u8 *s = src;
 
-       twofish_dec_blk_3way(ctx, (u8 *)dst, (u8 *)src);
+       if (dst == src)
+               s = memcpy(buf, src, sizeof(buf));
+       twofish_dec_blk_3way(ctx, dst, src);
+       crypto_xor(dst + TF_BLOCK_SIZE, s, sizeof(buf));
 
-       u128_xor(&dst[1], &dst[1], &ivs[0]);
-       u128_xor(&dst[2], &dst[2], &ivs[1]);
 }
 EXPORT_SYMBOL_GPL(twofish_dec_blk_cbc_3way);
 
-static const struct common_glue_ctx twofish_enc = {
-       .num_funcs = 2,
-       .fpu_blocks_limit = -1,
-
-       .funcs = { {
-               .num_blocks = 3,
-               .fn_u = { .ecb = twofish_enc_blk_3way }
-       }, {
-               .num_blocks = 1,
-               .fn_u = { .ecb = twofish_enc_blk }
-       } }
-};
-
-static const struct common_glue_ctx twofish_dec = {
-       .num_funcs = 2,
-       .fpu_blocks_limit = -1,
-
-       .funcs = { {
-               .num_blocks = 3,
-               .fn_u = { .ecb = twofish_dec_blk_3way }
-       }, {
-               .num_blocks = 1,
-               .fn_u = { .ecb = twofish_dec_blk }
-       } }
-};
-
-static const struct common_glue_ctx twofish_dec_cbc = {
-       .num_funcs = 2,
-       .fpu_blocks_limit = -1,
-
-       .funcs = { {
-               .num_blocks = 3,
-               .fn_u = { .cbc = twofish_dec_blk_cbc_3way }
-       }, {
-               .num_blocks = 1,
-               .fn_u = { .cbc = twofish_dec_blk }
-       } }
-};
-
 static int ecb_encrypt(struct skcipher_request *req)
 {
-       return glue_ecb_req_128bit(&twofish_enc, req);
+       ECB_WALK_START(req, TF_BLOCK_SIZE, -1);
+       ECB_BLOCK(3, twofish_enc_blk_3way);
+       ECB_BLOCK(1, twofish_enc_blk);
+       ECB_WALK_END();
 }
 
 static int ecb_decrypt(struct skcipher_request *req)
 {
-       return glue_ecb_req_128bit(&twofish_dec, req);
+       ECB_WALK_START(req, TF_BLOCK_SIZE, -1);
+       ECB_BLOCK(3, twofish_dec_blk_3way);
+       ECB_BLOCK(1, twofish_dec_blk);
+       ECB_WALK_END();
 }
 
 static int cbc_encrypt(struct skcipher_request *req)
 {
-       return glue_cbc_encrypt_req_128bit(twofish_enc_blk, req);
+       CBC_WALK_START(req, TF_BLOCK_SIZE, -1);
+       CBC_ENC_BLOCK(twofish_enc_blk);
+       CBC_WALK_END();
 }
 
 static int cbc_decrypt(struct skcipher_request *req)
 {
-       return glue_cbc_decrypt_req_128bit(&twofish_dec_cbc, req);
+       CBC_WALK_START(req, TF_BLOCK_SIZE, -1);
+       CBC_DEC_BLOCK(3, twofish_dec_blk_cbc_3way);
+       CBC_DEC_BLOCK(1, twofish_dec_blk);
+       CBC_WALK_END();
 }
 
 static struct skcipher_alg tf_skciphers[] = {
index 25101558acb5ceeeb5260f91c16f5ec9ad86a6d2..b2182658c55efea5f4b10a1113bbea4882cd4d04 100644 (file)
@@ -1711,7 +1711,6 @@ config CRYPTO_TWOFISH_X86_64_3WAY
        select CRYPTO_SKCIPHER
        select CRYPTO_TWOFISH_COMMON
        select CRYPTO_TWOFISH_X86_64
-       select CRYPTO_GLUE_HELPER_X86
        help
          Twofish cipher algorithm (x86_64, 3-way parallel).
 
@@ -1730,7 +1729,6 @@ config CRYPTO_TWOFISH_AVX_X86_64
        tristate "Twofish cipher algorithm (x86_64/AVX)"
        depends on X86 && 64BIT
        select CRYPTO_SKCIPHER
-       select CRYPTO_GLUE_HELPER_X86
        select CRYPTO_SIMD
        select CRYPTO_TWOFISH_COMMON
        select CRYPTO_TWOFISH_X86_64