Skip to content

Commit 165f357

Browse files
ardbiesheuvelherbertx
authored andcommitted
crypto: x86/twofish - drop dependency on glue helper
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>
1 parent ea55cfc commit 165f357

File tree

3 files changed

+44
-111
lines changed

3 files changed

+44
-111
lines changed

arch/x86/crypto/twofish_avx_glue.c

Lines changed: 20 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@
1515
#include <crypto/algapi.h>
1616
#include <crypto/internal/simd.h>
1717
#include <crypto/twofish.h>
18-
#include <asm/crypto/glue_helper.h>
1918
#include <asm/crypto/twofish.h>
2019

20+
#include "ecb_cbc_helpers.h"
21+
2122
#define TWOFISH_PARALLEL_BLOCKS 8
2223

2324
/* 8-way parallel cipher functions */
@@ -37,72 +38,38 @@ static inline void twofish_enc_blk_3way(const void *ctx, u8 *dst, const u8 *src)
3738
__twofish_enc_blk_3way(ctx, dst, src, false);
3839
}
3940

40-
static const struct common_glue_ctx twofish_enc = {
41-
.num_funcs = 3,
42-
.fpu_blocks_limit = TWOFISH_PARALLEL_BLOCKS,
43-
44-
.funcs = { {
45-
.num_blocks = TWOFISH_PARALLEL_BLOCKS,
46-
.fn_u = { .ecb = twofish_ecb_enc_8way }
47-
}, {
48-
.num_blocks = 3,
49-
.fn_u = { .ecb = twofish_enc_blk_3way }
50-
}, {
51-
.num_blocks = 1,
52-
.fn_u = { .ecb = twofish_enc_blk }
53-
} }
54-
};
55-
56-
static const struct common_glue_ctx twofish_dec = {
57-
.num_funcs = 3,
58-
.fpu_blocks_limit = TWOFISH_PARALLEL_BLOCKS,
59-
60-
.funcs = { {
61-
.num_blocks = TWOFISH_PARALLEL_BLOCKS,
62-
.fn_u = { .ecb = twofish_ecb_dec_8way }
63-
}, {
64-
.num_blocks = 3,
65-
.fn_u = { .ecb = twofish_dec_blk_3way }
66-
}, {
67-
.num_blocks = 1,
68-
.fn_u = { .ecb = twofish_dec_blk }
69-
} }
70-
};
71-
72-
static const struct common_glue_ctx twofish_dec_cbc = {
73-
.num_funcs = 3,
74-
.fpu_blocks_limit = TWOFISH_PARALLEL_BLOCKS,
75-
76-
.funcs = { {
77-
.num_blocks = TWOFISH_PARALLEL_BLOCKS,
78-
.fn_u = { .cbc = twofish_cbc_dec_8way }
79-
}, {
80-
.num_blocks = 3,
81-
.fn_u = { .cbc = twofish_dec_blk_cbc_3way }
82-
}, {
83-
.num_blocks = 1,
84-
.fn_u = { .cbc = twofish_dec_blk }
85-
} }
86-
};
87-
8841
static int ecb_encrypt(struct skcipher_request *req)
8942
{
90-
return glue_ecb_req_128bit(&twofish_enc, req);
43+
ECB_WALK_START(req, TF_BLOCK_SIZE, TWOFISH_PARALLEL_BLOCKS);
44+
ECB_BLOCK(TWOFISH_PARALLEL_BLOCKS, twofish_ecb_enc_8way);
45+
ECB_BLOCK(3, twofish_enc_blk_3way);
46+
ECB_BLOCK(1, twofish_enc_blk);
47+
ECB_WALK_END();
9148
}
9249

9350
static int ecb_decrypt(struct skcipher_request *req)
9451
{
95-
return glue_ecb_req_128bit(&twofish_dec, req);
52+
ECB_WALK_START(req, TF_BLOCK_SIZE, TWOFISH_PARALLEL_BLOCKS);
53+
ECB_BLOCK(TWOFISH_PARALLEL_BLOCKS, twofish_ecb_dec_8way);
54+
ECB_BLOCK(3, twofish_dec_blk_3way);
55+
ECB_BLOCK(1, twofish_dec_blk);
56+
ECB_WALK_END();
9657
}
9758

9859
static int cbc_encrypt(struct skcipher_request *req)
9960
{
100-
return glue_cbc_encrypt_req_128bit(twofish_enc_blk, req);
61+
CBC_WALK_START(req, TF_BLOCK_SIZE, -1);
62+
CBC_ENC_BLOCK(twofish_enc_blk);
63+
CBC_WALK_END();
10164
}
10265

10366
static int cbc_decrypt(struct skcipher_request *req)
10467
{
105-
return glue_cbc_decrypt_req_128bit(&twofish_dec_cbc, req);
68+
CBC_WALK_START(req, TF_BLOCK_SIZE, TWOFISH_PARALLEL_BLOCKS);
69+
CBC_DEC_BLOCK(TWOFISH_PARALLEL_BLOCKS, twofish_cbc_dec_8way);
70+
CBC_DEC_BLOCK(3, twofish_dec_blk_cbc_3way);
71+
CBC_DEC_BLOCK(1, twofish_dec_blk);
72+
CBC_WALK_END();
10673
}
10774

10875
static struct skcipher_alg twofish_algs[] = {

arch/x86/crypto/twofish_glue_3way.c

Lines changed: 24 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,16 @@
55
* Copyright (c) 2011 Jussi Kivilinna <jussi.kivilinna@mbnet.fi>
66
*/
77

8-
#include <asm/crypto/glue_helper.h>
98
#include <asm/crypto/twofish.h>
109
#include <crypto/algapi.h>
11-
#include <crypto/b128ops.h>
12-
#include <crypto/internal/skcipher.h>
1310
#include <crypto/twofish.h>
1411
#include <linux/crypto.h>
1512
#include <linux/init.h>
1613
#include <linux/module.h>
1714
#include <linux/types.h>
1815

16+
#include "ecb_cbc_helpers.h"
17+
1918
EXPORT_SYMBOL_GPL(__twofish_enc_blk_3way);
2019
EXPORT_SYMBOL_GPL(twofish_dec_blk_3way);
2120

@@ -30,79 +29,48 @@ static inline void twofish_enc_blk_3way(const void *ctx, u8 *dst, const u8 *src)
3029
__twofish_enc_blk_3way(ctx, dst, src, false);
3130
}
3231

33-
void twofish_dec_blk_cbc_3way(const void *ctx, u8 *d, const u8 *s)
32+
void twofish_dec_blk_cbc_3way(const void *ctx, u8 *dst, const u8 *src)
3433
{
35-
u128 ivs[2];
36-
u128 *dst = (u128 *)d;
37-
const u128 *src = (const u128 *)s;
38-
39-
ivs[0] = src[0];
40-
ivs[1] = src[1];
34+
u8 buf[2][TF_BLOCK_SIZE];
35+
const u8 *s = src;
4136

42-
twofish_dec_blk_3way(ctx, (u8 *)dst, (u8 *)src);
37+
if (dst == src)
38+
s = memcpy(buf, src, sizeof(buf));
39+
twofish_dec_blk_3way(ctx, dst, src);
40+
crypto_xor(dst + TF_BLOCK_SIZE, s, sizeof(buf));
4341

44-
u128_xor(&dst[1], &dst[1], &ivs[0]);
45-
u128_xor(&dst[2], &dst[2], &ivs[1]);
4642
}
4743
EXPORT_SYMBOL_GPL(twofish_dec_blk_cbc_3way);
4844

49-
static const struct common_glue_ctx twofish_enc = {
50-
.num_funcs = 2,
51-
.fpu_blocks_limit = -1,
52-
53-
.funcs = { {
54-
.num_blocks = 3,
55-
.fn_u = { .ecb = twofish_enc_blk_3way }
56-
}, {
57-
.num_blocks = 1,
58-
.fn_u = { .ecb = twofish_enc_blk }
59-
} }
60-
};
61-
62-
static const struct common_glue_ctx twofish_dec = {
63-
.num_funcs = 2,
64-
.fpu_blocks_limit = -1,
65-
66-
.funcs = { {
67-
.num_blocks = 3,
68-
.fn_u = { .ecb = twofish_dec_blk_3way }
69-
}, {
70-
.num_blocks = 1,
71-
.fn_u = { .ecb = twofish_dec_blk }
72-
} }
73-
};
74-
75-
static const struct common_glue_ctx twofish_dec_cbc = {
76-
.num_funcs = 2,
77-
.fpu_blocks_limit = -1,
78-
79-
.funcs = { {
80-
.num_blocks = 3,
81-
.fn_u = { .cbc = twofish_dec_blk_cbc_3way }
82-
}, {
83-
.num_blocks = 1,
84-
.fn_u = { .cbc = twofish_dec_blk }
85-
} }
86-
};
87-
8845
static int ecb_encrypt(struct skcipher_request *req)
8946
{
90-
return glue_ecb_req_128bit(&twofish_enc, req);
47+
ECB_WALK_START(req, TF_BLOCK_SIZE, -1);
48+
ECB_BLOCK(3, twofish_enc_blk_3way);
49+
ECB_BLOCK(1, twofish_enc_blk);
50+
ECB_WALK_END();
9151
}
9252

9353
static int ecb_decrypt(struct skcipher_request *req)
9454
{
95-
return glue_ecb_req_128bit(&twofish_dec, req);
55+
ECB_WALK_START(req, TF_BLOCK_SIZE, -1);
56+
ECB_BLOCK(3, twofish_dec_blk_3way);
57+
ECB_BLOCK(1, twofish_dec_blk);
58+
ECB_WALK_END();
9659
}
9760

9861
static int cbc_encrypt(struct skcipher_request *req)
9962
{
100-
return glue_cbc_encrypt_req_128bit(twofish_enc_blk, req);
63+
CBC_WALK_START(req, TF_BLOCK_SIZE, -1);
64+
CBC_ENC_BLOCK(twofish_enc_blk);
65+
CBC_WALK_END();
10166
}
10267

10368
static int cbc_decrypt(struct skcipher_request *req)
10469
{
105-
return glue_cbc_decrypt_req_128bit(&twofish_dec_cbc, req);
70+
CBC_WALK_START(req, TF_BLOCK_SIZE, -1);
71+
CBC_DEC_BLOCK(3, twofish_dec_blk_cbc_3way);
72+
CBC_DEC_BLOCK(1, twofish_dec_blk);
73+
CBC_WALK_END();
10674
}
10775

10876
static struct skcipher_alg tf_skciphers[] = {

crypto/Kconfig

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1711,7 +1711,6 @@ config CRYPTO_TWOFISH_X86_64_3WAY
17111711
select CRYPTO_SKCIPHER
17121712
select CRYPTO_TWOFISH_COMMON
17131713
select CRYPTO_TWOFISH_X86_64
1714-
select CRYPTO_GLUE_HELPER_X86
17151714
help
17161715
Twofish cipher algorithm (x86_64, 3-way parallel).
17171716

@@ -1730,7 +1729,6 @@ config CRYPTO_TWOFISH_AVX_X86_64
17301729
tristate "Twofish cipher algorithm (x86_64/AVX)"
17311730
depends on X86 && 64BIT
17321731
select CRYPTO_SKCIPHER
1733-
select CRYPTO_GLUE_HELPER_X86
17341732
select CRYPTO_SIMD
17351733
select CRYPTO_TWOFISH_COMMON
17361734
select CRYPTO_TWOFISH_X86_64

0 commit comments

Comments
 (0)