summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEric Biggers <ebiggers@google.com>2017-12-31 19:02:45 -0500
committerHerbert Xu <herbert@gondor.apana.org.au>2018-01-12 07:03:01 -0500
commit7660b1fb367eb3723b48d3980451fc4f25a05021 (patch)
treebacea5539f6c0d2590e158fbef956bfed8971da0
parent75d68369b544acc5d14c18a827654dfff248d09d (diff)
crypto: chacha20 - use rol32() macro from bitops.h
For chacha20_block(), use the existing 32-bit left-rotate function instead of defining one ourselves. Signed-off-by: Eric Biggers <ebiggers@google.com> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
-rw-r--r--lib/chacha20.c69
1 files changed, 32 insertions, 37 deletions
diff --git a/lib/chacha20.c b/lib/chacha20.c
index 29d3801dee24..c1cc50fb68c9 100644
--- a/lib/chacha20.c
+++ b/lib/chacha20.c
@@ -16,11 +16,6 @@
16#include <asm/unaligned.h> 16#include <asm/unaligned.h>
17#include <crypto/chacha20.h> 17#include <crypto/chacha20.h>
18 18
19static inline u32 rotl32(u32 v, u8 n)
20{
21 return (v << n) | (v >> (sizeof(v) * 8 - n));
22}
23
24void chacha20_block(u32 *state, u32 *stream) 19void chacha20_block(u32 *state, u32 *stream)
25{ 20{
26 u32 x[16], *out = stream; 21 u32 x[16], *out = stream;
@@ -30,45 +25,45 @@ void chacha20_block(u32 *state, u32 *stream)
30 x[i] = state[i]; 25 x[i] = state[i];
31 26
32 for (i = 0; i < 20; i += 2) { 27 for (i = 0; i < 20; i += 2) {
33 x[0] += x[4]; x[12] = rotl32(x[12] ^ x[0], 16); 28 x[0] += x[4]; x[12] = rol32(x[12] ^ x[0], 16);
34 x[1] += x[5]; x[13] = rotl32(x[13] ^ x[1], 16); 29 x[1] += x[5]; x[13] = rol32(x[13] ^ x[1], 16);
35 x[2] += x[6]; x[14] = rotl32(x[14] ^ x[2], 16); 30 x[2] += x[6]; x[14] = rol32(x[14] ^ x[2], 16);
36 x[3] += x[7]; x[15] = rotl32(x[15] ^ x[3], 16); 31 x[3] += x[7]; x[15] = rol32(x[15] ^ x[3], 16);
37 32
38 x[8] += x[12]; x[4] = rotl32(x[4] ^ x[8], 12); 33 x[8] += x[12]; x[4] = rol32(x[4] ^ x[8], 12);
39 x[9] += x[13]; x[5] = rotl32(x[5] ^ x[9], 12); 34 x[9] += x[13]; x[5] = rol32(x[5] ^ x[9], 12);
40 x[10] += x[14]; x[6] = rotl32(x[6] ^ x[10], 12); 35 x[10] += x[14]; x[6] = rol32(x[6] ^ x[10], 12);
41 x[11] += x[15]; x[7] = rotl32(x[7] ^ x[11], 12); 36 x[11] += x[15]; x[7] = rol32(x[7] ^ x[11], 12);
42 37
43 x[0] += x[4]; x[12] = rotl32(x[12] ^ x[0], 8); 38 x[0] += x[4]; x[12] = rol32(x[12] ^ x[0], 8);
44 x[1] += x[5]; x[13] = rotl32(x[13] ^ x[1], 8); 39 x[1] += x[5]; x[13] = rol32(x[13] ^ x[1], 8);
45 x[2] += x[6]; x[14] = rotl32(x[14] ^ x[2], 8); 40 x[2] += x[6]; x[14] = rol32(x[14] ^ x[2], 8);
46 x[3] += x[7]; x[15] = rotl32(x[15] ^ x[3], 8); 41 x[3] += x[7]; x[15] = rol32(x[15] ^ x[3], 8);
47 42
48 x[8] += x[12]; x[4] = rotl32(x[4] ^ x[8], 7); 43 x[8] += x[12]; x[4] = rol32(x[4] ^ x[8], 7);
49 x[9] += x[13]; x[5] = rotl32(x[5] ^ x[9], 7); 44 x[9] += x[13]; x[5] = rol32(x[5] ^ x[9], 7);
50 x[10] += x[14]; x[6] = rotl32(x[6] ^ x[10], 7); 45 x[10] += x[14]; x[6] = rol32(x[6] ^ x[10], 7);
51 x[11] += x[15]; x[7] = rotl32(x[7] ^ x[11], 7); 46 x[11] += x[15]; x[7] = rol32(x[7] ^ x[11], 7);
52 47
53 x[0] += x[5]; x[15] = rotl32(x[15] ^ x[0], 16); 48 x[0] += x[5]; x[15] = rol32(x[15] ^ x[0], 16);
54 x[1] += x[6]; x[12] = rotl32(x[12] ^ x[1], 16); 49 x[1] += x[6]; x[12] = rol32(x[12] ^ x[1], 16);
55 x[2] += x[7]; x[13] = rotl32(x[13] ^ x[2], 16); 50 x[2] += x[7]; x[13] = rol32(x[13] ^ x[2], 16);
56 x[3] += x[4]; x[14] = rotl32(x[14] ^ x[3], 16); 51 x[3] += x[4]; x[14] = rol32(x[14] ^ x[3], 16);
57 52
58 x[10] += x[15]; x[5] = rotl32(x[5] ^ x[10], 12); 53 x[10] += x[15]; x[5] = rol32(x[5] ^ x[10], 12);
59 x[11] += x[12]; x[6] = rotl32(x[6] ^ x[11], 12); 54 x[11] += x[12]; x[6] = rol32(x[6] ^ x[11], 12);
60 x[8] += x[13]; x[7] = rotl32(x[7] ^ x[8], 12); 55 x[8] += x[13]; x[7] = rol32(x[7] ^ x[8], 12);
61 x[9] += x[14]; x[4] = rotl32(x[4] ^ x[9], 12); 56 x[9] += x[14]; x[4] = rol32(x[4] ^ x[9], 12);
62 57
63 x[0] += x[5]; x[15] = rotl32(x[15] ^ x[0], 8); 58 x[0] += x[5]; x[15] = rol32(x[15] ^ x[0], 8);
64 x[1] += x[6]; x[12] = rotl32(x[12] ^ x[1], 8); 59 x[1] += x[6]; x[12] = rol32(x[12] ^ x[1], 8);
65 x[2] += x[7]; x[13] = rotl32(x[13] ^ x[2], 8); 60 x[2] += x[7]; x[13] = rol32(x[13] ^ x[2], 8);
66 x[3] += x[4]; x[14] = rotl32(x[14] ^ x[3], 8); 61 x[3] += x[4]; x[14] = rol32(x[14] ^ x[3], 8);
67 62
68 x[10] += x[15]; x[5] = rotl32(x[5] ^ x[10], 7); 63 x[10] += x[15]; x[5] = rol32(x[5] ^ x[10], 7);
69 x[11] += x[12]; x[6] = rotl32(x[6] ^ x[11], 7); 64 x[11] += x[12]; x[6] = rol32(x[6] ^ x[11], 7);
70 x[8] += x[13]; x[7] = rotl32(x[7] ^ x[8], 7); 65 x[8] += x[13]; x[7] = rol32(x[7] ^ x[8], 7);
71 x[9] += x[14]; x[4] = rotl32(x[4] ^ x[9], 7); 66 x[9] += x[14]; x[4] = rol32(x[4] ^ x[9], 7);
72 } 67 }
73 68
74 for (i = 0; i < ARRAY_SIZE(x); i++) 69 for (i = 0; i < ARRAY_SIZE(x); i++)