aboutsummaryrefslogtreecommitdiffstats
path: root/arch
diff options
context:
space:
mode:
authorJussi Kivilinna <jussi.kivilinna@mbnet.fi>2012-10-20 08:06:36 -0400
committerHerbert Xu <herbert@gondor.apana.org.au>2012-10-24 09:10:54 -0400
commit58990986f1cba40c23c0c10592ace08616de3ffa (patch)
tree84466698a28860d1457c804b857b2e97d1995fcb /arch
parente080b17a8cec92ef42343989ae65c73c25529346 (diff)
crypto: x86/glue_helper - use le128 instead of u128 for CTR mode
'u128' currently used for CTR mode is on little-endian 'long long' swapped and would require extra swap operations by SSE/AVX code. Use of le128 instead of u128 allows IV calculations to be done with vector registers easier. Signed-off-by: Jussi Kivilinna <jussi.kivilinna@mbnet.fi> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
Diffstat (limited to 'arch')
-rw-r--r--arch/x86/crypto/camellia_glue.c16
-rw-r--r--arch/x86/crypto/cast6_avx_glue.c12
-rw-r--r--arch/x86/crypto/glue_helper.c12
-rw-r--r--arch/x86/crypto/serpent_avx_glue.c12
-rw-r--r--arch/x86/crypto/serpent_sse2_glue.c12
-rw-r--r--arch/x86/crypto/twofish_avx_glue.c6
-rw-r--r--arch/x86/crypto/twofish_glue_3way.c20
-rw-r--r--arch/x86/include/asm/crypto/glue_helper.h28
-rw-r--r--arch/x86/include/asm/crypto/twofish.h4
9 files changed, 64 insertions, 58 deletions
diff --git a/arch/x86/crypto/camellia_glue.c b/arch/x86/crypto/camellia_glue.c
index 42ffd2bbab5..021a0086186 100644
--- a/arch/x86/crypto/camellia_glue.c
+++ b/arch/x86/crypto/camellia_glue.c
@@ -1317,21 +1317,21 @@ static void camellia_decrypt_cbc_2way(void *ctx, u128 *dst, const u128 *src)
1317 u128_xor(&dst[1], &dst[1], &iv); 1317 u128_xor(&dst[1], &dst[1], &iv);
1318} 1318}
1319 1319
1320static void camellia_crypt_ctr(void *ctx, u128 *dst, const u128 *src, u128 *iv) 1320static void camellia_crypt_ctr(void *ctx, u128 *dst, const u128 *src, le128 *iv)
1321{ 1321{
1322 be128 ctrblk; 1322 be128 ctrblk;
1323 1323
1324 if (dst != src) 1324 if (dst != src)
1325 *dst = *src; 1325 *dst = *src;
1326 1326
1327 u128_to_be128(&ctrblk, iv); 1327 le128_to_be128(&ctrblk, iv);
1328 u128_inc(iv); 1328 le128_inc(iv);
1329 1329
1330 camellia_enc_blk_xor(ctx, (u8 *)dst, (u8 *)&ctrblk); 1330 camellia_enc_blk_xor(ctx, (u8 *)dst, (u8 *)&ctrblk);
1331} 1331}
1332 1332
1333static void camellia_crypt_ctr_2way(void *ctx, u128 *dst, const u128 *src, 1333static void camellia_crypt_ctr_2way(void *ctx, u128 *dst, const u128 *src,
1334 u128 *iv) 1334 le128 *iv)
1335{ 1335{
1336 be128 ctrblks[2]; 1336 be128 ctrblks[2];
1337 1337
@@ -1340,10 +1340,10 @@ static void camellia_crypt_ctr_2way(void *ctx, u128 *dst, const u128 *src,
1340 dst[1] = src[1]; 1340 dst[1] = src[1];
1341 } 1341 }
1342 1342
1343 u128_to_be128(&ctrblks[0], iv); 1343 le128_to_be128(&ctrblks[0], iv);
1344 u128_inc(iv); 1344 le128_inc(iv);
1345 u128_to_be128(&ctrblks[1], iv); 1345 le128_to_be128(&ctrblks[1], iv);
1346 u128_inc(iv); 1346 le128_inc(iv);
1347 1347
1348 camellia_enc_blk_xor_2way(ctx, (u8 *)dst, (u8 *)ctrblks); 1348 camellia_enc_blk_xor_2way(ctx, (u8 *)dst, (u8 *)ctrblks);
1349} 1349}
diff --git a/arch/x86/crypto/cast6_avx_glue.c b/arch/x86/crypto/cast6_avx_glue.c
index 15e5f85a501..1dfd33b5b4f 100644
--- a/arch/x86/crypto/cast6_avx_glue.c
+++ b/arch/x86/crypto/cast6_avx_glue.c
@@ -78,19 +78,19 @@ static void cast6_decrypt_cbc_xway(void *ctx, u128 *dst, const u128 *src)
78 u128_xor(dst + (j + 1), dst + (j + 1), ivs + j); 78 u128_xor(dst + (j + 1), dst + (j + 1), ivs + j);
79} 79}
80 80
81static void cast6_crypt_ctr(void *ctx, u128 *dst, const u128 *src, u128 *iv) 81static void cast6_crypt_ctr(void *ctx, u128 *dst, const u128 *src, le128 *iv)
82{ 82{
83 be128 ctrblk; 83 be128 ctrblk;
84 84
85 u128_to_be128(&ctrblk, iv); 85 le128_to_be128(&ctrblk, iv);
86 u128_inc(iv); 86 le128_inc(iv);
87 87
88 __cast6_encrypt(ctx, (u8 *)&ctrblk, (u8 *)&ctrblk); 88 __cast6_encrypt(ctx, (u8 *)&ctrblk, (u8 *)&ctrblk);
89 u128_xor(dst, src, (u128 *)&ctrblk); 89 u128_xor(dst, src, (u128 *)&ctrblk);
90} 90}
91 91
92static void cast6_crypt_ctr_xway(void *ctx, u128 *dst, const u128 *src, 92static void cast6_crypt_ctr_xway(void *ctx, u128 *dst, const u128 *src,
93 u128 *iv) 93 le128 *iv)
94{ 94{
95 be128 ctrblks[CAST6_PARALLEL_BLOCKS]; 95 be128 ctrblks[CAST6_PARALLEL_BLOCKS];
96 unsigned int i; 96 unsigned int i;
@@ -99,8 +99,8 @@ static void cast6_crypt_ctr_xway(void *ctx, u128 *dst, const u128 *src,
99 if (dst != src) 99 if (dst != src)
100 dst[i] = src[i]; 100 dst[i] = src[i];
101 101
102 u128_to_be128(&ctrblks[i], iv); 102 le128_to_be128(&ctrblks[i], iv);
103 u128_inc(iv); 103 le128_inc(iv);
104 } 104 }
105 105
106 cast6_enc_blk_xway_xor(ctx, (u8 *)dst, (u8 *)ctrblks); 106 cast6_enc_blk_xway_xor(ctx, (u8 *)dst, (u8 *)ctrblks);
diff --git a/arch/x86/crypto/glue_helper.c b/arch/x86/crypto/glue_helper.c
index 30b3927bd73..22ce4f683e5 100644
--- a/arch/x86/crypto/glue_helper.c
+++ b/arch/x86/crypto/glue_helper.c
@@ -221,16 +221,16 @@ static void glue_ctr_crypt_final_128bit(const common_glue_ctr_func_t fn_ctr,
221 u8 *src = (u8 *)walk->src.virt.addr; 221 u8 *src = (u8 *)walk->src.virt.addr;
222 u8 *dst = (u8 *)walk->dst.virt.addr; 222 u8 *dst = (u8 *)walk->dst.virt.addr;
223 unsigned int nbytes = walk->nbytes; 223 unsigned int nbytes = walk->nbytes;
224 u128 ctrblk; 224 le128 ctrblk;
225 u128 tmp; 225 u128 tmp;
226 226
227 be128_to_u128(&ctrblk, (be128 *)walk->iv); 227 be128_to_le128(&ctrblk, (be128 *)walk->iv);
228 228
229 memcpy(&tmp, src, nbytes); 229 memcpy(&tmp, src, nbytes);
230 fn_ctr(ctx, &tmp, &tmp, &ctrblk); 230 fn_ctr(ctx, &tmp, &tmp, &ctrblk);
231 memcpy(dst, &tmp, nbytes); 231 memcpy(dst, &tmp, nbytes);
232 232
233 u128_to_be128((be128 *)walk->iv, &ctrblk); 233 le128_to_be128((be128 *)walk->iv, &ctrblk);
234} 234}
235EXPORT_SYMBOL_GPL(glue_ctr_crypt_final_128bit); 235EXPORT_SYMBOL_GPL(glue_ctr_crypt_final_128bit);
236 236
@@ -243,11 +243,11 @@ static unsigned int __glue_ctr_crypt_128bit(const struct common_glue_ctx *gctx,
243 unsigned int nbytes = walk->nbytes; 243 unsigned int nbytes = walk->nbytes;
244 u128 *src = (u128 *)walk->src.virt.addr; 244 u128 *src = (u128 *)walk->src.virt.addr;
245 u128 *dst = (u128 *)walk->dst.virt.addr; 245 u128 *dst = (u128 *)walk->dst.virt.addr;
246 u128 ctrblk; 246 le128 ctrblk;
247 unsigned int num_blocks, func_bytes; 247 unsigned int num_blocks, func_bytes;
248 unsigned int i; 248 unsigned int i;
249 249
250 be128_to_u128(&ctrblk, (be128 *)walk->iv); 250 be128_to_le128(&ctrblk, (be128 *)walk->iv);
251 251
252 /* Process multi-block batch */ 252 /* Process multi-block batch */
253 for (i = 0; i < gctx->num_funcs; i++) { 253 for (i = 0; i < gctx->num_funcs; i++) {
@@ -269,7 +269,7 @@ static unsigned int __glue_ctr_crypt_128bit(const struct common_glue_ctx *gctx,
269 } 269 }
270 270
271done: 271done:
272 u128_to_be128((be128 *)walk->iv, &ctrblk); 272 le128_to_be128((be128 *)walk->iv, &ctrblk);
273 return nbytes; 273 return nbytes;
274} 274}
275 275
diff --git a/arch/x86/crypto/serpent_avx_glue.c b/arch/x86/crypto/serpent_avx_glue.c
index 3f543a04cf1..2aa31ade1e6 100644
--- a/arch/x86/crypto/serpent_avx_glue.c
+++ b/arch/x86/crypto/serpent_avx_glue.c
@@ -56,19 +56,19 @@ static void serpent_decrypt_cbc_xway(void *ctx, u128 *dst, const u128 *src)
56 u128_xor(dst + (j + 1), dst + (j + 1), ivs + j); 56 u128_xor(dst + (j + 1), dst + (j + 1), ivs + j);
57} 57}
58 58
59static void serpent_crypt_ctr(void *ctx, u128 *dst, const u128 *src, u128 *iv) 59static void serpent_crypt_ctr(void *ctx, u128 *dst, const u128 *src, le128 *iv)
60{ 60{
61 be128 ctrblk; 61 be128 ctrblk;
62 62
63 u128_to_be128(&ctrblk, iv); 63 le128_to_be128(&ctrblk, iv);
64 u128_inc(iv); 64 le128_inc(iv);
65 65
66 __serpent_encrypt(ctx, (u8 *)&ctrblk, (u8 *)&ctrblk); 66 __serpent_encrypt(ctx, (u8 *)&ctrblk, (u8 *)&ctrblk);
67 u128_xor(dst, src, (u128 *)&ctrblk); 67 u128_xor(dst, src, (u128 *)&ctrblk);
68} 68}
69 69
70static void serpent_crypt_ctr_xway(void *ctx, u128 *dst, const u128 *src, 70static void serpent_crypt_ctr_xway(void *ctx, u128 *dst, const u128 *src,
71 u128 *iv) 71 le128 *iv)
72{ 72{
73 be128 ctrblks[SERPENT_PARALLEL_BLOCKS]; 73 be128 ctrblks[SERPENT_PARALLEL_BLOCKS];
74 unsigned int i; 74 unsigned int i;
@@ -77,8 +77,8 @@ static void serpent_crypt_ctr_xway(void *ctx, u128 *dst, const u128 *src,
77 if (dst != src) 77 if (dst != src)
78 dst[i] = src[i]; 78 dst[i] = src[i];
79 79
80 u128_to_be128(&ctrblks[i], iv); 80 le128_to_be128(&ctrblks[i], iv);
81 u128_inc(iv); 81 le128_inc(iv);
82 } 82 }
83 83
84 serpent_enc_blk_xway_xor(ctx, (u8 *)dst, (u8 *)ctrblks); 84 serpent_enc_blk_xway_xor(ctx, (u8 *)dst, (u8 *)ctrblks);
diff --git a/arch/x86/crypto/serpent_sse2_glue.c b/arch/x86/crypto/serpent_sse2_glue.c
index 9107a9908c4..97a356ece24 100644
--- a/arch/x86/crypto/serpent_sse2_glue.c
+++ b/arch/x86/crypto/serpent_sse2_glue.c
@@ -59,19 +59,19 @@ static void serpent_decrypt_cbc_xway(void *ctx, u128 *dst, const u128 *src)
59 u128_xor(dst + (j + 1), dst + (j + 1), ivs + j); 59 u128_xor(dst + (j + 1), dst + (j + 1), ivs + j);
60} 60}
61 61
62static void serpent_crypt_ctr(void *ctx, u128 *dst, const u128 *src, u128 *iv) 62static void serpent_crypt_ctr(void *ctx, u128 *dst, const u128 *src, le128 *iv)
63{ 63{
64 be128 ctrblk; 64 be128 ctrblk;
65 65
66 u128_to_be128(&ctrblk, iv); 66 le128_to_be128(&ctrblk, iv);
67 u128_inc(iv); 67 le128_inc(iv);
68 68
69 __serpent_encrypt(ctx, (u8 *)&ctrblk, (u8 *)&ctrblk); 69 __serpent_encrypt(ctx, (u8 *)&ctrblk, (u8 *)&ctrblk);
70 u128_xor(dst, src, (u128 *)&ctrblk); 70 u128_xor(dst, src, (u128 *)&ctrblk);
71} 71}
72 72
73static void serpent_crypt_ctr_xway(void *ctx, u128 *dst, const u128 *src, 73static void serpent_crypt_ctr_xway(void *ctx, u128 *dst, const u128 *src,
74 u128 *iv) 74 le128 *iv)
75{ 75{
76 be128 ctrblks[SERPENT_PARALLEL_BLOCKS]; 76 be128 ctrblks[SERPENT_PARALLEL_BLOCKS];
77 unsigned int i; 77 unsigned int i;
@@ -80,8 +80,8 @@ static void serpent_crypt_ctr_xway(void *ctx, u128 *dst, const u128 *src,
80 if (dst != src) 80 if (dst != src)
81 dst[i] = src[i]; 81 dst[i] = src[i];
82 82
83 u128_to_be128(&ctrblks[i], iv); 83 le128_to_be128(&ctrblks[i], iv);
84 u128_inc(iv); 84 le128_inc(iv);
85 } 85 }
86 86
87 serpent_enc_blk_xway_xor(ctx, (u8 *)dst, (u8 *)ctrblks); 87 serpent_enc_blk_xway_xor(ctx, (u8 *)dst, (u8 *)ctrblks);
diff --git a/arch/x86/crypto/twofish_avx_glue.c b/arch/x86/crypto/twofish_avx_glue.c
index e7708b5442e..810e45d5118 100644
--- a/arch/x86/crypto/twofish_avx_glue.c
+++ b/arch/x86/crypto/twofish_avx_glue.c
@@ -90,7 +90,7 @@ static void twofish_dec_blk_cbc_xway(void *ctx, u128 *dst, const u128 *src)
90} 90}
91 91
92static void twofish_enc_blk_ctr_xway(void *ctx, u128 *dst, const u128 *src, 92static void twofish_enc_blk_ctr_xway(void *ctx, u128 *dst, const u128 *src,
93 u128 *iv) 93 le128 *iv)
94{ 94{
95 be128 ctrblks[TWOFISH_PARALLEL_BLOCKS]; 95 be128 ctrblks[TWOFISH_PARALLEL_BLOCKS];
96 unsigned int i; 96 unsigned int i;
@@ -99,8 +99,8 @@ static void twofish_enc_blk_ctr_xway(void *ctx, u128 *dst, const u128 *src,
99 if (dst != src) 99 if (dst != src)
100 dst[i] = src[i]; 100 dst[i] = src[i];
101 101
102 u128_to_be128(&ctrblks[i], iv); 102 le128_to_be128(&ctrblks[i], iv);
103 u128_inc(iv); 103 le128_inc(iv);
104 } 104 }
105 105
106 twofish_enc_blk_xway_xor(ctx, (u8 *)dst, (u8 *)ctrblks); 106 twofish_enc_blk_xway_xor(ctx, (u8 *)dst, (u8 *)ctrblks);
diff --git a/arch/x86/crypto/twofish_glue_3way.c b/arch/x86/crypto/twofish_glue_3way.c
index aa3eb358b7e..13e63b3e1df 100644
--- a/arch/x86/crypto/twofish_glue_3way.c
+++ b/arch/x86/crypto/twofish_glue_3way.c
@@ -62,15 +62,15 @@ void twofish_dec_blk_cbc_3way(void *ctx, u128 *dst, const u128 *src)
62} 62}
63EXPORT_SYMBOL_GPL(twofish_dec_blk_cbc_3way); 63EXPORT_SYMBOL_GPL(twofish_dec_blk_cbc_3way);
64 64
65void twofish_enc_blk_ctr(void *ctx, u128 *dst, const u128 *src, u128 *iv) 65void twofish_enc_blk_ctr(void *ctx, u128 *dst, const u128 *src, le128 *iv)
66{ 66{
67 be128 ctrblk; 67 be128 ctrblk;
68 68
69 if (dst != src) 69 if (dst != src)
70 *dst = *src; 70 *dst = *src;
71 71
72 u128_to_be128(&ctrblk, iv); 72 le128_to_be128(&ctrblk, iv);
73 u128_inc(iv); 73 le128_inc(iv);
74 74
75 twofish_enc_blk(ctx, (u8 *)&ctrblk, (u8 *)&ctrblk); 75 twofish_enc_blk(ctx, (u8 *)&ctrblk, (u8 *)&ctrblk);
76 u128_xor(dst, dst, (u128 *)&ctrblk); 76 u128_xor(dst, dst, (u128 *)&ctrblk);
@@ -78,7 +78,7 @@ void twofish_enc_blk_ctr(void *ctx, u128 *dst, const u128 *src, u128 *iv)
78EXPORT_SYMBOL_GPL(twofish_enc_blk_ctr); 78EXPORT_SYMBOL_GPL(twofish_enc_blk_ctr);
79 79
80void twofish_enc_blk_ctr_3way(void *ctx, u128 *dst, const u128 *src, 80void twofish_enc_blk_ctr_3way(void *ctx, u128 *dst, const u128 *src,
81 u128 *iv) 81 le128 *iv)
82{ 82{
83 be128 ctrblks[3]; 83 be128 ctrblks[3];
84 84
@@ -88,12 +88,12 @@ void twofish_enc_blk_ctr_3way(void *ctx, u128 *dst, const u128 *src,
88 dst[2] = src[2]; 88 dst[2] = src[2];
89 } 89 }
90 90
91 u128_to_be128(&ctrblks[0], iv); 91 le128_to_be128(&ctrblks[0], iv);
92 u128_inc(iv); 92 le128_inc(iv);
93 u128_to_be128(&ctrblks[1], iv); 93 le128_to_be128(&ctrblks[1], iv);
94 u128_inc(iv); 94 le128_inc(iv);
95 u128_to_be128(&ctrblks[2], iv); 95 le128_to_be128(&ctrblks[2], iv);
96 u128_inc(iv); 96 le128_inc(iv);
97 97
98 twofish_enc_blk_xor_3way(ctx, (u8 *)dst, (u8 *)ctrblks); 98 twofish_enc_blk_xor_3way(ctx, (u8 *)dst, (u8 *)ctrblks);
99} 99}
diff --git a/arch/x86/include/asm/crypto/glue_helper.h b/arch/x86/include/asm/crypto/glue_helper.h
index 3e408bddc96..e2d65b061d2 100644
--- a/arch/x86/include/asm/crypto/glue_helper.h
+++ b/arch/x86/include/asm/crypto/glue_helper.h
@@ -13,7 +13,7 @@
13typedef void (*common_glue_func_t)(void *ctx, u8 *dst, const u8 *src); 13typedef void (*common_glue_func_t)(void *ctx, u8 *dst, const u8 *src);
14typedef void (*common_glue_cbc_func_t)(void *ctx, u128 *dst, const u128 *src); 14typedef void (*common_glue_cbc_func_t)(void *ctx, u128 *dst, const u128 *src);
15typedef void (*common_glue_ctr_func_t)(void *ctx, u128 *dst, const u128 *src, 15typedef void (*common_glue_ctr_func_t)(void *ctx, u128 *dst, const u128 *src,
16 u128 *iv); 16 le128 *iv);
17 17
18#define GLUE_FUNC_CAST(fn) ((common_glue_func_t)(fn)) 18#define GLUE_FUNC_CAST(fn) ((common_glue_func_t)(fn))
19#define GLUE_CBC_FUNC_CAST(fn) ((common_glue_cbc_func_t)(fn)) 19#define GLUE_CBC_FUNC_CAST(fn) ((common_glue_cbc_func_t)(fn))
@@ -71,23 +71,29 @@ static inline void glue_fpu_end(bool fpu_enabled)
71 kernel_fpu_end(); 71 kernel_fpu_end();
72} 72}
73 73
74static inline void u128_to_be128(be128 *dst, const u128 *src) 74static inline void le128_to_be128(be128 *dst, const le128 *src)
75{ 75{
76 dst->a = cpu_to_be64(src->a); 76 dst->a = cpu_to_be64(le64_to_cpu(src->a));
77 dst->b = cpu_to_be64(src->b); 77 dst->b = cpu_to_be64(le64_to_cpu(src->b));
78} 78}
79 79
80static inline void be128_to_u128(u128 *dst, const be128 *src) 80static inline void be128_to_le128(le128 *dst, const be128 *src)
81{ 81{
82 dst->a = be64_to_cpu(src->a); 82 dst->a = cpu_to_le64(be64_to_cpu(src->a));
83 dst->b = be64_to_cpu(src->b); 83 dst->b = cpu_to_le64(be64_to_cpu(src->b));
84} 84}
85 85
86static inline void u128_inc(u128 *i) 86static inline void le128_inc(le128 *i)
87{ 87{
88 i->b++; 88 u64 a = le64_to_cpu(i->a);
89 if (!i->b) 89 u64 b = le64_to_cpu(i->b);
90 i->a++; 90
91 b++;
92 if (!b)
93 a++;
94
95 i->a = cpu_to_le64(a);
96 i->b = cpu_to_le64(b);
91} 97}
92 98
93extern int glue_ecb_crypt_128bit(const struct common_glue_ctx *gctx, 99extern int glue_ecb_crypt_128bit(const struct common_glue_ctx *gctx,
diff --git a/arch/x86/include/asm/crypto/twofish.h b/arch/x86/include/asm/crypto/twofish.h
index 9d2c514bd5f..878c51ceebb 100644
--- a/arch/x86/include/asm/crypto/twofish.h
+++ b/arch/x86/include/asm/crypto/twofish.h
@@ -31,9 +31,9 @@ asmlinkage void twofish_dec_blk_3way(struct twofish_ctx *ctx, u8 *dst,
31/* helpers from twofish_x86_64-3way module */ 31/* helpers from twofish_x86_64-3way module */
32extern void twofish_dec_blk_cbc_3way(void *ctx, u128 *dst, const u128 *src); 32extern void twofish_dec_blk_cbc_3way(void *ctx, u128 *dst, const u128 *src);
33extern void twofish_enc_blk_ctr(void *ctx, u128 *dst, const u128 *src, 33extern void twofish_enc_blk_ctr(void *ctx, u128 *dst, const u128 *src,
34 u128 *iv); 34 le128 *iv);
35extern void twofish_enc_blk_ctr_3way(void *ctx, u128 *dst, const u128 *src, 35extern void twofish_enc_blk_ctr_3way(void *ctx, u128 *dst, const u128 *src,
36 u128 *iv); 36 le128 *iv);
37 37
38extern int lrw_twofish_setkey(struct crypto_tfm *tfm, const u8 *key, 38extern int lrw_twofish_setkey(struct crypto_tfm *tfm, const u8 *key,
39 unsigned int keylen); 39 unsigned int keylen);