aboutsummaryrefslogtreecommitdiffstats
path: root/crypto/sha512_generic.c
diff options
context:
space:
mode:
Diffstat (limited to 'crypto/sha512_generic.c')
-rw-r--r--crypto/sha512_generic.c83
1 files changed, 37 insertions, 46 deletions
diff --git a/crypto/sha512_generic.c b/crypto/sha512_generic.c
index 88f160b77b1f..107f6f7be5e1 100644
--- a/crypto/sha512_generic.c
+++ b/crypto/sha512_generic.c
@@ -31,11 +31,6 @@ static inline u64 Maj(u64 x, u64 y, u64 z)
31 return (x & y) | (z & (x | y)); 31 return (x & y) | (z & (x | y));
32} 32}
33 33
34static inline u64 RORu64(u64 x, u64 y)
35{
36 return (x >> y) | (x << (64 - y));
37}
38
39static const u64 sha512_K[80] = { 34static const u64 sha512_K[80] = {
40 0x428a2f98d728ae22ULL, 0x7137449123ef65cdULL, 0xb5c0fbcfec4d3b2fULL, 35 0x428a2f98d728ae22ULL, 0x7137449123ef65cdULL, 0xb5c0fbcfec4d3b2fULL,
41 0xe9b5dba58189dbbcULL, 0x3956c25bf348b538ULL, 0x59f111f1b605d019ULL, 36 0xe9b5dba58189dbbcULL, 0x3956c25bf348b538ULL, 0x59f111f1b605d019ULL,
@@ -66,10 +61,10 @@ static const u64 sha512_K[80] = {
66 0x5fcb6fab3ad6faecULL, 0x6c44198c4a475817ULL, 61 0x5fcb6fab3ad6faecULL, 0x6c44198c4a475817ULL,
67}; 62};
68 63
69#define e0(x) (RORu64(x,28) ^ RORu64(x,34) ^ RORu64(x,39)) 64#define e0(x) (ror64(x,28) ^ ror64(x,34) ^ ror64(x,39))
70#define e1(x) (RORu64(x,14) ^ RORu64(x,18) ^ RORu64(x,41)) 65#define e1(x) (ror64(x,14) ^ ror64(x,18) ^ ror64(x,41))
71#define s0(x) (RORu64(x, 1) ^ RORu64(x, 8) ^ (x >> 7)) 66#define s0(x) (ror64(x, 1) ^ ror64(x, 8) ^ (x >> 7))
72#define s1(x) (RORu64(x,19) ^ RORu64(x,61) ^ (x >> 6)) 67#define s1(x) (ror64(x,19) ^ ror64(x,61) ^ (x >> 6))
73 68
74static inline void LOAD_OP(int I, u64 *W, const u8 *input) 69static inline void LOAD_OP(int I, u64 *W, const u8 *input)
75{ 70{
@@ -78,7 +73,7 @@ static inline void LOAD_OP(int I, u64 *W, const u8 *input)
78 73
79static inline void BLEND_OP(int I, u64 *W) 74static inline void BLEND_OP(int I, u64 *W)
80{ 75{
81 W[I % 16] += s1(W[(I-2) % 16]) + W[(I-7) % 16] + s0(W[(I-15) % 16]); 76 W[I & 15] += s1(W[(I-2) & 15]) + W[(I-7) & 15] + s0(W[(I-15) & 15]);
82} 77}
83 78
84static void 79static void
@@ -89,46 +84,42 @@ sha512_transform(u64 *state, const u8 *input)
89 int i; 84 int i;
90 u64 W[16]; 85 u64 W[16];
91 86
92 /* load the input */
93 for (i = 0; i < 16; i++)
94 LOAD_OP(i, W, input);
95
96 /* load the state into our registers */ 87 /* load the state into our registers */
97 a=state[0]; b=state[1]; c=state[2]; d=state[3]; 88 a=state[0]; b=state[1]; c=state[2]; d=state[3];
98 e=state[4]; f=state[5]; g=state[6]; h=state[7]; 89 e=state[4]; f=state[5]; g=state[6]; h=state[7];
99 90
100#define SHA512_0_15(i, a, b, c, d, e, f, g, h) \ 91 /* now iterate */
101 t1 = h + e1(e) + Ch(e, f, g) + sha512_K[i] + W[i]; \ 92 for (i=0; i<80; i+=8) {
102 t2 = e0(a) + Maj(a, b, c); \ 93 if (!(i & 8)) {
103 d += t1; \ 94 int j;
104 h = t1 + t2 95
105 96 if (i < 16) {
106#define SHA512_16_79(i, a, b, c, d, e, f, g, h) \ 97 /* load the input */
107 BLEND_OP(i, W); \ 98 for (j = 0; j < 16; j++)
108 t1 = h + e1(e) + Ch(e, f, g) + sha512_K[i] + W[(i)%16]; \ 99 LOAD_OP(i + j, W, input);
109 t2 = e0(a) + Maj(a, b, c); \ 100 } else {
110 d += t1; \ 101 for (j = 0; j < 16; j++) {
111 h = t1 + t2 102 BLEND_OP(i + j, W);
112 103 }
113 for (i = 0; i < 16; i += 8) { 104 }
114 SHA512_0_15(i, a, b, c, d, e, f, g, h); 105 }
115 SHA512_0_15(i + 1, h, a, b, c, d, e, f, g); 106
116 SHA512_0_15(i + 2, g, h, a, b, c, d, e, f); 107 t1 = h + e1(e) + Ch(e,f,g) + sha512_K[i ] + W[(i & 15)];
117 SHA512_0_15(i + 3, f, g, h, a, b, c, d, e); 108 t2 = e0(a) + Maj(a,b,c); d+=t1; h=t1+t2;
118 SHA512_0_15(i + 4, e, f, g, h, a, b, c, d); 109 t1 = g + e1(d) + Ch(d,e,f) + sha512_K[i+1] + W[(i & 15) + 1];
119 SHA512_0_15(i + 5, d, e, f, g, h, a, b, c); 110 t2 = e0(h) + Maj(h,a,b); c+=t1; g=t1+t2;
120 SHA512_0_15(i + 6, c, d, e, f, g, h, a, b); 111 t1 = f + e1(c) + Ch(c,d,e) + sha512_K[i+2] + W[(i & 15) + 2];
121 SHA512_0_15(i + 7, b, c, d, e, f, g, h, a); 112 t2 = e0(g) + Maj(g,h,a); b+=t1; f=t1+t2;
122 } 113 t1 = e + e1(b) + Ch(b,c,d) + sha512_K[i+3] + W[(i & 15) + 3];
123 for (i = 16; i < 80; i += 8) { 114 t2 = e0(f) + Maj(f,g,h); a+=t1; e=t1+t2;
124 SHA512_16_79(i, a, b, c, d, e, f, g, h); 115 t1 = d + e1(a) + Ch(a,b,c) + sha512_K[i+4] + W[(i & 15) + 4];
125 SHA512_16_79(i + 1, h, a, b, c, d, e, f, g); 116 t2 = e0(e) + Maj(e,f,g); h+=t1; d=t1+t2;
126 SHA512_16_79(i + 2, g, h, a, b, c, d, e, f); 117 t1 = c + e1(h) + Ch(h,a,b) + sha512_K[i+5] + W[(i & 15) + 5];
127 SHA512_16_79(i + 3, f, g, h, a, b, c, d, e); 118 t2 = e0(d) + Maj(d,e,f); g+=t1; c=t1+t2;
128 SHA512_16_79(i + 4, e, f, g, h, a, b, c, d); 119 t1 = b + e1(g) + Ch(g,h,a) + sha512_K[i+6] + W[(i & 15) + 6];
129 SHA512_16_79(i + 5, d, e, f, g, h, a, b, c); 120 t2 = e0(c) + Maj(c,d,e); f+=t1; b=t1+t2;
130 SHA512_16_79(i + 6, c, d, e, f, g, h, a, b); 121 t1 = a + e1(f) + Ch(f,g,h) + sha512_K[i+7] + W[(i & 15) + 7];
131 SHA512_16_79(i + 7, b, c, d, e, f, g, h, a); 122 t2 = e0(b) + Maj(b,c,d); e+=t1; a=t1+t2;
132 } 123 }
133 124
134 state[0] += a; state[1] += b; state[2] += c; state[3] += d; 125 state[0] += a; state[1] += b; state[2] += c; state[3] += d;