68a9e65db8f85d72e14a044dd23d257fdce326fd
[project/usign.git] / sha512.c
1 /*
2 * Copyright (C) 2015 Felix Fietkau <nbd@openwrt.org>
3 *
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16
17 /* SHA512
18 * Daniel Beer <dlbeer@gmail.com>, 22 Apr 2014
19 *
20 * This file is in the public domain.
21 */
22
23 #include "sha512.h"
24
25 static const uint64_t sha512_initial_state[8] = {
26 0x6a09e667f3bcc908LL, 0xbb67ae8584caa73bLL,
27 0x3c6ef372fe94f82bLL, 0xa54ff53a5f1d36f1LL,
28 0x510e527fade682d1LL, 0x9b05688c2b3e6c1fLL,
29 0x1f83d9abfb41bd6bLL, 0x5be0cd19137e2179LL,
30 };
31
32 static const uint64_t round_k[80] = {
33 0x428a2f98d728ae22LL, 0x7137449123ef65cdLL,
34 0xb5c0fbcfec4d3b2fLL, 0xe9b5dba58189dbbcLL,
35 0x3956c25bf348b538LL, 0x59f111f1b605d019LL,
36 0x923f82a4af194f9bLL, 0xab1c5ed5da6d8118LL,
37 0xd807aa98a3030242LL, 0x12835b0145706fbeLL,
38 0x243185be4ee4b28cLL, 0x550c7dc3d5ffb4e2LL,
39 0x72be5d74f27b896fLL, 0x80deb1fe3b1696b1LL,
40 0x9bdc06a725c71235LL, 0xc19bf174cf692694LL,
41 0xe49b69c19ef14ad2LL, 0xefbe4786384f25e3LL,
42 0x0fc19dc68b8cd5b5LL, 0x240ca1cc77ac9c65LL,
43 0x2de92c6f592b0275LL, 0x4a7484aa6ea6e483LL,
44 0x5cb0a9dcbd41fbd4LL, 0x76f988da831153b5LL,
45 0x983e5152ee66dfabLL, 0xa831c66d2db43210LL,
46 0xb00327c898fb213fLL, 0xbf597fc7beef0ee4LL,
47 0xc6e00bf33da88fc2LL, 0xd5a79147930aa725LL,
48 0x06ca6351e003826fLL, 0x142929670a0e6e70LL,
49 0x27b70a8546d22ffcLL, 0x2e1b21385c26c926LL,
50 0x4d2c6dfc5ac42aedLL, 0x53380d139d95b3dfLL,
51 0x650a73548baf63deLL, 0x766a0abb3c77b2a8LL,
52 0x81c2c92e47edaee6LL, 0x92722c851482353bLL,
53 0xa2bfe8a14cf10364LL, 0xa81a664bbc423001LL,
54 0xc24b8b70d0f89791LL, 0xc76c51a30654be30LL,
55 0xd192e819d6ef5218LL, 0xd69906245565a910LL,
56 0xf40e35855771202aLL, 0x106aa07032bbd1b8LL,
57 0x19a4c116b8d2d0c8LL, 0x1e376c085141ab53LL,
58 0x2748774cdf8eeb99LL, 0x34b0bcb5e19b48a8LL,
59 0x391c0cb3c5c95a63LL, 0x4ed8aa4ae3418acbLL,
60 0x5b9cca4f7763e373LL, 0x682e6ff3d6b2b8a3LL,
61 0x748f82ee5defb2fcLL, 0x78a5636f43172f60LL,
62 0x84c87814a1f0ab72LL, 0x8cc702081a6439ecLL,
63 0x90befffa23631e28LL, 0xa4506cebde82bde9LL,
64 0xbef9a3f7b2c67915LL, 0xc67178f2e372532bLL,
65 0xca273eceea26619cLL, 0xd186b8c721c0c207LL,
66 0xeada7dd6cde0eb1eLL, 0xf57d4f7fee6ed178LL,
67 0x06f067aa72176fbaLL, 0x0a637dc5a2c898a6LL,
68 0x113f9804bef90daeLL, 0x1b710b35131c471bLL,
69 0x28db77f523047d84LL, 0x32caab7b40c72493LL,
70 0x3c9ebe0a15c9bebcLL, 0x431d67c49c100d4cLL,
71 0x4cc5d4becb3e42b6LL, 0x597f299cfc657e2aLL,
72 0x5fcb6fab3ad6faecLL, 0x6c44198c4a475817LL,
73 };
74
75 static inline uint64_t load64(const uint8_t *x)
76 {
77 uint64_t r;
78
79 r = *(x++);
80 r = (r << 8) | *(x++);
81 r = (r << 8) | *(x++);
82 r = (r << 8) | *(x++);
83 r = (r << 8) | *(x++);
84 r = (r << 8) | *(x++);
85 r = (r << 8) | *(x++);
86 r = (r << 8) | *(x++);
87
88 return r;
89 }
90
91 static inline void store64(uint8_t *x, uint64_t v)
92 {
93 x += 7;
94 *(x--) = v;
95 v >>= 8;
96 *(x--) = v;
97 v >>= 8;
98 *(x--) = v;
99 v >>= 8;
100 *(x--) = v;
101 v >>= 8;
102 *(x--) = v;
103 v >>= 8;
104 *(x--) = v;
105 v >>= 8;
106 *(x--) = v;
107 v >>= 8;
108 *(x--) = v;
109 }
110
111 static inline uint64_t rot64(uint64_t x, int bits)
112 {
113 return (x >> bits) | (x << (64 - bits));
114 }
115
116 static void
117 sha512_block(struct sha512_state *s, const uint8_t *blk)
118 {
119 uint64_t w[16];
120 uint64_t a, b, c, d, e, f, g, h;
121 int i;
122
123 for (i = 0; i < 16; i++) {
124 w[i] = load64(blk);
125 blk += 8;
126 }
127
128 /* Load state */
129 a = s->h[0];
130 b = s->h[1];
131 c = s->h[2];
132 d = s->h[3];
133 e = s->h[4];
134 f = s->h[5];
135 g = s->h[6];
136 h = s->h[7];
137
138 for (i = 0; i < 80; i++) {
139 /* Compute value of w[i + 16]. w[wrap(i)] is currently w[i] */
140 const uint64_t wi = w[i & 15];
141 const uint64_t wi15 = w[(i + 1) & 15];
142 const uint64_t wi2 = w[(i + 14) & 15];
143 const uint64_t wi7 = w[(i + 9) & 15];
144 const uint64_t s0 =
145 rot64(wi15, 1) ^ rot64(wi15, 8) ^ (wi15 >> 7);
146 const uint64_t s1 =
147 rot64(wi2, 19) ^ rot64(wi2, 61) ^ (wi2 >> 6);
148
149 /* Round calculations */
150 const uint64_t S0 = rot64(a, 28) ^ rot64(a, 34) ^ rot64(a, 39);
151 const uint64_t S1 = rot64(e, 14) ^ rot64(e, 18) ^ rot64(e, 41);
152 const uint64_t ch = (e & f) ^ ((~e) & g);
153 const uint64_t temp1 = h + S1 + ch + round_k[i] + wi;
154 const uint64_t maj = (a & b) ^ (a & c) ^ (b & c);
155 const uint64_t temp2 = S0 + maj;
156
157 /* Update round state */
158 h = g;
159 g = f;
160 f = e;
161 e = d + temp1;
162 d = c;
163 c = b;
164 b = a;
165 a = temp1 + temp2;
166
167 /* w[wrap(i)] becomes w[i + 16] */
168 w[i & 15] = wi + s0 + wi7 + s1;
169 }
170
171 /* Store state */
172 s->h[0] += a;
173 s->h[1] += b;
174 s->h[2] += c;
175 s->h[3] += d;
176 s->h[4] += e;
177 s->h[5] += f;
178 s->h[6] += g;
179 s->h[7] += h;
180 }
181
182 void sha512_init(struct sha512_state *s)
183 {
184 memcpy(s->h, &sha512_initial_state, sizeof(s->h));
185 s->len = 0;
186 }
187
188 void sha512_add(struct sha512_state *s, const void *data, size_t len)
189 {
190 unsigned int partial = s->len & (SHA512_BLOCK_SIZE - 1);
191
192 if (partial) {
193 unsigned int cur = SHA512_BLOCK_SIZE - partial;
194
195 if (cur > len)
196 cur = len;
197
198 memcpy(&s->partial[partial], data, cur);
199
200 s->len += cur;
201 data += cur;
202 len -= cur;
203
204 partial = s->len & (SHA512_BLOCK_SIZE - 1);
205 if (!partial)
206 sha512_block(s, s->partial);
207 }
208
209 while (len >= SHA512_BLOCK_SIZE) {
210 sha512_block(s, data);
211
212 s->len += SHA512_BLOCK_SIZE;
213 data += SHA512_BLOCK_SIZE;
214 len -= SHA512_BLOCK_SIZE;
215 }
216
217 if (!len)
218 return;
219
220 memcpy(s->partial, data, len);
221 s->len += len;
222 }
223
224 void sha512_final(struct sha512_state *s, uint8_t *hash)
225 {
226 size_t last_size = s->len & (SHA512_BLOCK_SIZE - 1);
227 unsigned int len = SHA512_HASH_SIZE;
228 int i = 0;
229
230 s->partial[last_size++] = 0x80;
231 if (last_size < SHA512_BLOCK_SIZE)
232 memset(&s->partial[last_size], 0,
233 SHA512_BLOCK_SIZE - last_size);
234
235 if (last_size > 110) {
236 sha512_block(s, s->partial);
237 memset(s->partial, 0, sizeof(s->partial));
238 }
239
240 /* Note: we assume total_size fits in 61 bits */
241 store64(s->partial + SHA512_BLOCK_SIZE - 8, s->len << 3);
242 sha512_block(s, s->partial);
243
244 /* Read out whole words */
245 while (len >= 8) {
246 store64(hash, s->h[i++]);
247 hash += 8;
248 len -= 8;
249 }
250
251 /* Read out bytes */
252 if (len) {
253 uint8_t tmp[8];
254
255 store64(tmp, s->h[i]);
256 memcpy(hash, tmp, len);
257 }
258 }