diff options
author | Harvey Harrison <harvey.harrison@gmail.com> | 2008-12-17 00:53:49 -0500 |
---|---|---|
committer | Herbert Xu <herbert@gondor.apana.org.au> | 2008-12-24 19:02:30 -0500 |
commit | f0d1ec3a227e01a27ce20719bf7b58de86d44f0f (patch) | |
tree | 369f7a7cd11c1bb87d2e8288b4a79a875511f88d /crypto/salsa20_generic.c | |
parent | ad79cdd77fc1466e45cf923890f66bcfe7c43f12 (diff) |
crypto: salsa20 - Remove private wrappers around various operations
ROTATE -> rol32
XOR was always used with the same destination, use ^=
PLUS/PLUSONE use ++ or +=
Signed-off-by: Harvey Harrison <harvey.harrison@gmail.com>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
Diffstat (limited to 'crypto/salsa20_generic.c')
-rw-r--r-- | crypto/salsa20_generic.c | 75 |
1 files changed, 36 insertions, 39 deletions
diff --git a/crypto/salsa20_generic.c b/crypto/salsa20_generic.c index b07d55981741..eac10c11685c 100644 --- a/crypto/salsa20_generic.c +++ b/crypto/salsa20_generic.c | |||
@@ -24,6 +24,7 @@ | |||
24 | #include <linux/errno.h> | 24 | #include <linux/errno.h> |
25 | #include <linux/crypto.h> | 25 | #include <linux/crypto.h> |
26 | #include <linux/types.h> | 26 | #include <linux/types.h> |
27 | #include <linux/bitops.h> | ||
27 | #include <crypto/algapi.h> | 28 | #include <crypto/algapi.h> |
28 | #include <asm/byteorder.h> | 29 | #include <asm/byteorder.h> |
29 | 30 | ||
@@ -42,10 +43,6 @@ D. J. Bernstein | |||
42 | Public domain. | 43 | Public domain. |
43 | */ | 44 | */ |
44 | 45 | ||
45 | #define ROTATE(v,n) (((v) << (n)) | ((v) >> (32 - (n)))) | ||
46 | #define XOR(v,w) ((v) ^ (w)) | ||
47 | #define PLUS(v,w) (((v) + (w))) | ||
48 | #define PLUSONE(v) (PLUS((v),1)) | ||
49 | #define U32TO8_LITTLE(p, v) \ | 46 | #define U32TO8_LITTLE(p, v) \ |
50 | { (p)[0] = (v >> 0) & 0xff; (p)[1] = (v >> 8) & 0xff; \ | 47 | { (p)[0] = (v >> 0) & 0xff; (p)[1] = (v >> 8) & 0xff; \ |
51 | (p)[2] = (v >> 16) & 0xff; (p)[3] = (v >> 24) & 0xff; } | 48 | (p)[2] = (v >> 16) & 0xff; (p)[3] = (v >> 24) & 0xff; } |
@@ -65,41 +62,41 @@ static void salsa20_wordtobyte(u8 output[64], const u32 input[16]) | |||
65 | 62 | ||
66 | memcpy(x, input, sizeof(x)); | 63 | memcpy(x, input, sizeof(x)); |
67 | for (i = 20; i > 0; i -= 2) { | 64 | for (i = 20; i > 0; i -= 2) { |
68 | x[ 4] = XOR(x[ 4],ROTATE(PLUS(x[ 0],x[12]), 7)); | 65 | x[ 4] ^= rol32((x[ 0] + x[12]), 7); |
69 | x[ 8] = XOR(x[ 8],ROTATE(PLUS(x[ 4],x[ 0]), 9)); | 66 | x[ 8] ^= rol32((x[ 4] + x[ 0]), 9); |
70 | x[12] = XOR(x[12],ROTATE(PLUS(x[ 8],x[ 4]),13)); | 67 | x[12] ^= rol32((x[ 8] + x[ 4]), 13); |
71 | x[ 0] = XOR(x[ 0],ROTATE(PLUS(x[12],x[ 8]),18)); | 68 | x[ 0] ^= rol32((x[12] + x[ 8]), 18); |
72 | x[ 9] = XOR(x[ 9],ROTATE(PLUS(x[ 5],x[ 1]), 7)); | 69 | x[ 9] ^= rol32((x[ 5] + x[ 1]), 7); |
73 | x[13] = XOR(x[13],ROTATE(PLUS(x[ 9],x[ 5]), 9)); | 70 | x[13] ^= rol32((x[ 9] + x[ 5]), 9); |
74 | x[ 1] = XOR(x[ 1],ROTATE(PLUS(x[13],x[ 9]),13)); | 71 | x[ 1] ^= rol32((x[13] + x[ 9]), 13); |
75 | x[ 5] = XOR(x[ 5],ROTATE(PLUS(x[ 1],x[13]),18)); | 72 | x[ 5] ^= rol32((x[ 1] + x[13]), 18); |
76 | x[14] = XOR(x[14],ROTATE(PLUS(x[10],x[ 6]), 7)); | 73 | x[14] ^= rol32((x[10] + x[ 6]), 7); |
77 | x[ 2] = XOR(x[ 2],ROTATE(PLUS(x[14],x[10]), 9)); | 74 | x[ 2] ^= rol32((x[14] + x[10]), 9); |
78 | x[ 6] = XOR(x[ 6],ROTATE(PLUS(x[ 2],x[14]),13)); | 75 | x[ 6] ^= rol32((x[ 2] + x[14]), 13); |
79 | x[10] = XOR(x[10],ROTATE(PLUS(x[ 6],x[ 2]),18)); | 76 | x[10] ^= rol32((x[ 6] + x[ 2]), 18); |
80 | x[ 3] = XOR(x[ 3],ROTATE(PLUS(x[15],x[11]), 7)); | 77 | x[ 3] ^= rol32((x[15] + x[11]), 7); |
81 | x[ 7] = XOR(x[ 7],ROTATE(PLUS(x[ 3],x[15]), 9)); | 78 | x[ 7] ^= rol32((x[ 3] + x[15]), 9); |
82 | x[11] = XOR(x[11],ROTATE(PLUS(x[ 7],x[ 3]),13)); | 79 | x[11] ^= rol32((x[ 7] + x[ 3]), 13); |
83 | x[15] = XOR(x[15],ROTATE(PLUS(x[11],x[ 7]),18)); | 80 | x[15] ^= rol32((x[11] + x[ 7]), 18); |
84 | x[ 1] = XOR(x[ 1],ROTATE(PLUS(x[ 0],x[ 3]), 7)); | 81 | x[ 1] ^= rol32((x[ 0] + x[ 3]), 7); |
85 | x[ 2] = XOR(x[ 2],ROTATE(PLUS(x[ 1],x[ 0]), 9)); | 82 | x[ 2] ^= rol32((x[ 1] + x[ 0]), 9); |
86 | x[ 3] = XOR(x[ 3],ROTATE(PLUS(x[ 2],x[ 1]),13)); | 83 | x[ 3] ^= rol32((x[ 2] + x[ 1]), 13); |
87 | x[ 0] = XOR(x[ 0],ROTATE(PLUS(x[ 3],x[ 2]),18)); | 84 | x[ 0] ^= rol32((x[ 3] + x[ 2]), 18); |
88 | x[ 6] = XOR(x[ 6],ROTATE(PLUS(x[ 5],x[ 4]), 7)); | 85 | x[ 6] ^= rol32((x[ 5] + x[ 4]), 7); |
89 | x[ 7] = XOR(x[ 7],ROTATE(PLUS(x[ 6],x[ 5]), 9)); | 86 | x[ 7] ^= rol32((x[ 6] + x[ 5]), 9); |
90 | x[ 4] = XOR(x[ 4],ROTATE(PLUS(x[ 7],x[ 6]),13)); | 87 | x[ 4] ^= rol32((x[ 7] + x[ 6]), 13); |
91 | x[ 5] = XOR(x[ 5],ROTATE(PLUS(x[ 4],x[ 7]),18)); | 88 | x[ 5] ^= rol32((x[ 4] + x[ 7]), 18); |
92 | x[11] = XOR(x[11],ROTATE(PLUS(x[10],x[ 9]), 7)); | 89 | x[11] ^= rol32((x[10] + x[ 9]), 7); |
93 | x[ 8] = XOR(x[ 8],ROTATE(PLUS(x[11],x[10]), 9)); | 90 | x[ 8] ^= rol32((x[11] + x[10]), 9); |
94 | x[ 9] = XOR(x[ 9],ROTATE(PLUS(x[ 8],x[11]),13)); | 91 | x[ 9] ^= rol32((x[ 8] + x[11]), 13); |
95 | x[10] = XOR(x[10],ROTATE(PLUS(x[ 9],x[ 8]),18)); | 92 | x[10] ^= rol32((x[ 9] + x[ 8]), 18); |
96 | x[12] = XOR(x[12],ROTATE(PLUS(x[15],x[14]), 7)); | 93 | x[12] ^= rol32((x[15] + x[14]), 7); |
97 | x[13] = XOR(x[13],ROTATE(PLUS(x[12],x[15]), 9)); | 94 | x[13] ^= rol32((x[12] + x[15]), 9); |
98 | x[14] = XOR(x[14],ROTATE(PLUS(x[13],x[12]),13)); | 95 | x[14] ^= rol32((x[13] + x[12]), 13); |
99 | x[15] = XOR(x[15],ROTATE(PLUS(x[14],x[13]),18)); | 96 | x[15] ^= rol32((x[14] + x[13]), 18); |
100 | } | 97 | } |
101 | for (i = 0; i < 16; ++i) | 98 | for (i = 0; i < 16; ++i) |
102 | x[i] = PLUS(x[i],input[i]); | 99 | x[i] += input[i]; |
103 | for (i = 0; i < 16; ++i) | 100 | for (i = 0; i < 16; ++i) |
104 | U32TO8_LITTLE(output + 4 * i,x[i]); | 101 | U32TO8_LITTLE(output + 4 * i,x[i]); |
105 | } | 102 | } |
@@ -150,9 +147,9 @@ static void salsa20_encrypt_bytes(struct salsa20_ctx *ctx, u8 *dst, | |||
150 | while (bytes) { | 147 | while (bytes) { |
151 | salsa20_wordtobyte(buf, ctx->input); | 148 | salsa20_wordtobyte(buf, ctx->input); |
152 | 149 | ||
153 | ctx->input[8] = PLUSONE(ctx->input[8]); | 150 | ctx->input[8]++; |
154 | if (!ctx->input[8]) | 151 | if (!ctx->input[8]) |
155 | ctx->input[9] = PLUSONE(ctx->input[9]); | 152 | ctx->input[9]++; |
156 | 153 | ||
157 | if (bytes <= 64) { | 154 | if (bytes <= 64) { |
158 | crypto_xor(dst, buf, bytes); | 155 | crypto_xor(dst, buf, bytes); |