aboutsummaryrefslogtreecommitdiffstats
path: root/arch/x86/crypto
diff options
context:
space:
mode:
authorJussi Kivilinna <jussi.kivilinna@iki.fi>2013-04-08 14:51:00 -0400
committerHerbert Xu <herbert@gondor.apana.org.au>2013-04-25 09:01:51 -0400
commit18be45270a80ab489d9402b63e1f103428f0afde (patch)
tree4335e792dd1e612fff4838619698263dfce4344e /arch/x86/crypto
parenta05248ed2d9a83ae7c3e6db7c4ef9331c3dedc81 (diff)
crypto: x86/twofish-avx - use optimized XTS code
Change twofish-avx to use the new XTS code, for smaller stack usage and small boost to performance. tcrypt results, with Intel i5-2450M: enc dec 16B 1.03x 1.02x 64B 0.91x 0.91x 256B 1.10x 1.09x 1024B 1.12x 1.11x 8192B 1.12x 1.11x Since XTS is practically always used with data blocks of size 512 bytes or more, I chose to not make use of twofish-3way for block sized smaller than 128 bytes. This causes slower result in tcrypt for 64 bytes. Signed-off-by: Jussi Kivilinna <jussi.kivilinna@iki.fi> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
Diffstat (limited to 'arch/x86/crypto')
-rw-r--r--arch/x86/crypto/twofish-avx-x86_64-asm_64.S48
-rw-r--r--arch/x86/crypto/twofish_avx_glue.c91
2 files changed, 98 insertions, 41 deletions
diff --git a/arch/x86/crypto/twofish-avx-x86_64-asm_64.S b/arch/x86/crypto/twofish-avx-x86_64-asm_64.S
index 8d3e113b2c95..05058134c443 100644
--- a/arch/x86/crypto/twofish-avx-x86_64-asm_64.S
+++ b/arch/x86/crypto/twofish-avx-x86_64-asm_64.S
@@ -4,7 +4,7 @@
4 * Copyright (C) 2012 Johannes Goetzfried 4 * Copyright (C) 2012 Johannes Goetzfried
5 * <Johannes.Goetzfried@informatik.stud.uni-erlangen.de> 5 * <Johannes.Goetzfried@informatik.stud.uni-erlangen.de>
6 * 6 *
7 * Copyright © 2012 Jussi Kivilinna <jussi.kivilinna@mbnet.fi> 7 * Copyright © 2012-2013 Jussi Kivilinna <jussi.kivilinna@iki.fi>
8 * 8 *
9 * This program is free software; you can redistribute it and/or modify 9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by 10 * it under the terms of the GNU General Public License as published by
@@ -33,6 +33,8 @@
33 33
34.Lbswap128_mask: 34.Lbswap128_mask:
35 .byte 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 35 .byte 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0
36.Lxts_gf128mul_and_shl1_mask:
37 .byte 0x87, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0
36 38
37.text 39.text
38 40
@@ -408,3 +410,47 @@ ENTRY(twofish_ctr_8way)
408 410
409 ret; 411 ret;
410ENDPROC(twofish_ctr_8way) 412ENDPROC(twofish_ctr_8way)
413
414ENTRY(twofish_xts_enc_8way)
415 /* input:
416 * %rdi: ctx, CTX
417 * %rsi: dst
418 * %rdx: src
419 * %rcx: iv (t ⊕ αⁿ ∈ GF(2¹²⁸))
420 */
421
422 movq %rsi, %r11;
423
424 /* regs <= src, dst <= IVs, regs <= regs xor IVs */
425 load_xts_8way(%rcx, %rdx, %rsi, RA1, RB1, RC1, RD1, RA2, RB2, RC2, RD2,
426 RX0, RX1, RY0, .Lxts_gf128mul_and_shl1_mask);
427
428 call __twofish_enc_blk8;
429
430 /* dst <= regs xor IVs(in dst) */
431 store_xts_8way(%r11, RC1, RD1, RA1, RB1, RC2, RD2, RA2, RB2);
432
433 ret;
434ENDPROC(twofish_xts_enc_8way)
435
436ENTRY(twofish_xts_dec_8way)
437 /* input:
438 * %rdi: ctx, CTX
439 * %rsi: dst
440 * %rdx: src
441 * %rcx: iv (t ⊕ αⁿ ∈ GF(2¹²⁸))
442 */
443
444 movq %rsi, %r11;
445
446 /* regs <= src, dst <= IVs, regs <= regs xor IVs */
447 load_xts_8way(%rcx, %rdx, %rsi, RC1, RD1, RA1, RB1, RC2, RD2, RA2, RB2,
448 RX0, RX1, RY0, .Lxts_gf128mul_and_shl1_mask);
449
450 call __twofish_dec_blk8;
451
452 /* dst <= regs xor IVs(in dst) */
453 store_xts_8way(%r11, RA1, RB1, RC1, RD1, RA2, RB2, RC2, RD2);
454
455 ret;
456ENDPROC(twofish_xts_dec_8way)
diff --git a/arch/x86/crypto/twofish_avx_glue.c b/arch/x86/crypto/twofish_avx_glue.c
index 94ac91d26e47..a62ba541884e 100644
--- a/arch/x86/crypto/twofish_avx_glue.c
+++ b/arch/x86/crypto/twofish_avx_glue.c
@@ -4,6 +4,8 @@
4 * Copyright (C) 2012 Johannes Goetzfried 4 * Copyright (C) 2012 Johannes Goetzfried
5 * <Johannes.Goetzfried@informatik.stud.uni-erlangen.de> 5 * <Johannes.Goetzfried@informatik.stud.uni-erlangen.de>
6 * 6 *
7 * Copyright © 2013 Jussi Kivilinna <jussi.kivilinna@iki.fi>
8 *
7 * This program is free software; you can redistribute it and/or modify 9 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by 10 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or 11 * the Free Software Foundation; either version 2 of the License, or
@@ -56,12 +58,29 @@ asmlinkage void twofish_cbc_dec_8way(struct twofish_ctx *ctx, u8 *dst,
56asmlinkage void twofish_ctr_8way(struct twofish_ctx *ctx, u8 *dst, 58asmlinkage void twofish_ctr_8way(struct twofish_ctx *ctx, u8 *dst,
57 const u8 *src, le128 *iv); 59 const u8 *src, le128 *iv);
58 60
61asmlinkage void twofish_xts_enc_8way(struct twofish_ctx *ctx, u8 *dst,
62 const u8 *src, le128 *iv);
63asmlinkage void twofish_xts_dec_8way(struct twofish_ctx *ctx, u8 *dst,
64 const u8 *src, le128 *iv);
65
59static inline void twofish_enc_blk_3way(struct twofish_ctx *ctx, u8 *dst, 66static inline void twofish_enc_blk_3way(struct twofish_ctx *ctx, u8 *dst,
60 const u8 *src) 67 const u8 *src)
61{ 68{
62 __twofish_enc_blk_3way(ctx, dst, src, false); 69 __twofish_enc_blk_3way(ctx, dst, src, false);
63} 70}
64 71
72static void twofish_xts_enc(void *ctx, u128 *dst, const u128 *src, le128 *iv)
73{
74 glue_xts_crypt_128bit_one(ctx, dst, src, iv,
75 GLUE_FUNC_CAST(twofish_enc_blk));
76}
77
78static void twofish_xts_dec(void *ctx, u128 *dst, const u128 *src, le128 *iv)
79{
80 glue_xts_crypt_128bit_one(ctx, dst, src, iv,
81 GLUE_FUNC_CAST(twofish_dec_blk));
82}
83
65 84
66static const struct common_glue_ctx twofish_enc = { 85static const struct common_glue_ctx twofish_enc = {
67 .num_funcs = 3, 86 .num_funcs = 3,
@@ -95,6 +114,19 @@ static const struct common_glue_ctx twofish_ctr = {
95 } } 114 } }
96}; 115};
97 116
117static const struct common_glue_ctx twofish_enc_xts = {
118 .num_funcs = 2,
119 .fpu_blocks_limit = TWOFISH_PARALLEL_BLOCKS,
120
121 .funcs = { {
122 .num_blocks = TWOFISH_PARALLEL_BLOCKS,
123 .fn_u = { .xts = GLUE_XTS_FUNC_CAST(twofish_xts_enc_8way) }
124 }, {
125 .num_blocks = 1,
126 .fn_u = { .xts = GLUE_XTS_FUNC_CAST(twofish_xts_enc) }
127 } }
128};
129
98static const struct common_glue_ctx twofish_dec = { 130static const struct common_glue_ctx twofish_dec = {
99 .num_funcs = 3, 131 .num_funcs = 3,
100 .fpu_blocks_limit = TWOFISH_PARALLEL_BLOCKS, 132 .fpu_blocks_limit = TWOFISH_PARALLEL_BLOCKS,
@@ -127,6 +159,19 @@ static const struct common_glue_ctx twofish_dec_cbc = {
127 } } 159 } }
128}; 160};
129 161
162static const struct common_glue_ctx twofish_dec_xts = {
163 .num_funcs = 2,
164 .fpu_blocks_limit = TWOFISH_PARALLEL_BLOCKS,
165
166 .funcs = { {
167 .num_blocks = TWOFISH_PARALLEL_BLOCKS,
168 .fn_u = { .xts = GLUE_XTS_FUNC_CAST(twofish_xts_dec_8way) }
169 }, {
170 .num_blocks = 1,
171 .fn_u = { .xts = GLUE_XTS_FUNC_CAST(twofish_xts_dec) }
172 } }
173};
174
130static int ecb_encrypt(struct blkcipher_desc *desc, struct scatterlist *dst, 175static int ecb_encrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
131 struct scatterlist *src, unsigned int nbytes) 176 struct scatterlist *src, unsigned int nbytes)
132{ 177{
@@ -275,54 +320,20 @@ static int xts_encrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
275 struct scatterlist *src, unsigned int nbytes) 320 struct scatterlist *src, unsigned int nbytes)
276{ 321{
277 struct twofish_xts_ctx *ctx = crypto_blkcipher_ctx(desc->tfm); 322 struct twofish_xts_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
278 be128 buf[TWOFISH_PARALLEL_BLOCKS];
279 struct crypt_priv crypt_ctx = {
280 .ctx = &ctx->crypt_ctx,
281 .fpu_enabled = false,
282 };
283 struct xts_crypt_req req = {
284 .tbuf = buf,
285 .tbuflen = sizeof(buf),
286
287 .tweak_ctx = &ctx->tweak_ctx,
288 .tweak_fn = XTS_TWEAK_CAST(twofish_enc_blk),
289 .crypt_ctx = &crypt_ctx,
290 .crypt_fn = encrypt_callback,
291 };
292 int ret;
293 323
294 desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP; 324 return glue_xts_crypt_128bit(&twofish_enc_xts, desc, dst, src, nbytes,
295 ret = xts_crypt(desc, dst, src, nbytes, &req); 325 XTS_TWEAK_CAST(twofish_enc_blk),
296 twofish_fpu_end(crypt_ctx.fpu_enabled); 326 &ctx->tweak_ctx, &ctx->crypt_ctx);
297
298 return ret;
299} 327}
300 328
301static int xts_decrypt(struct blkcipher_desc *desc, struct scatterlist *dst, 329static int xts_decrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
302 struct scatterlist *src, unsigned int nbytes) 330 struct scatterlist *src, unsigned int nbytes)
303{ 331{
304 struct twofish_xts_ctx *ctx = crypto_blkcipher_ctx(desc->tfm); 332 struct twofish_xts_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
305 be128 buf[TWOFISH_PARALLEL_BLOCKS];
306 struct crypt_priv crypt_ctx = {
307 .ctx = &ctx->crypt_ctx,
308 .fpu_enabled = false,
309 };
310 struct xts_crypt_req req = {
311 .tbuf = buf,
312 .tbuflen = sizeof(buf),
313
314 .tweak_ctx = &ctx->tweak_ctx,
315 .tweak_fn = XTS_TWEAK_CAST(twofish_enc_blk),
316 .crypt_ctx = &crypt_ctx,
317 .crypt_fn = decrypt_callback,
318 };
319 int ret;
320 333
321 desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP; 334 return glue_xts_crypt_128bit(&twofish_dec_xts, desc, dst, src, nbytes,
322 ret = xts_crypt(desc, dst, src, nbytes, &req); 335 XTS_TWEAK_CAST(twofish_enc_blk),
323 twofish_fpu_end(crypt_ctx.fpu_enabled); 336 &ctx->tweak_ctx, &ctx->crypt_ctx);
324
325 return ret;
326} 337}
327 338
328static struct crypto_alg twofish_algs[10] = { { 339static struct crypto_alg twofish_algs[10] = { {