/src/tdengine/source/util/src/tsha.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2025 TAOS Data, Inc. <jhtao@taosdata.com> |
3 | | * |
4 | | * SPDX-License-Identifier: AGPL-3.0-or-later |
5 | | * |
6 | | * This program is free software: you can use, redistribute, and/or modify |
7 | | * it under the terms of the GNU Affero General Public License, version 3 |
8 | | * or later ("AGPL"), as published by the Free Software Foundation. |
9 | | * |
10 | | * This program is distributed in the hope that it will be useful, but WITHOUT |
11 | | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
12 | | * FITNESS FOR A PARTICULAR PURPOSE. |
13 | | * |
14 | | * You should have received a copy of the GNU Affero General Public License |
15 | | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
16 | | */ |
17 | | |
18 | | #define _DEFAULT_SOURCE |
19 | | #include "tsha.h" |
20 | | |
21 | | #define SHA1HANDSOFF |
22 | | |
23 | 0 | #define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits)))) |
24 | | |
25 | | /* blk0() and blk() perform the initial expand. */ |
26 | | /* I got the idea of expanding during the round function from SSLeay */ |
27 | 0 | #define blk0_le(i) (block->l[i] = (rol(block->l[i], 24) & 0xFF00FF00) | (rol(block->l[i], 8) & 0x00FF00FF)) |
28 | | #define blk0_be(i) block->l[i] |
29 | | #if BYTE_ORDER == LITTLE_ENDIAN |
30 | 0 | #define blk0(i) blk0_le(i) |
31 | | #elif BYTE_ORDER == BIG_ENDIAN |
32 | | #define blk0(i) blk0_be(i) |
33 | | #else |
34 | | /* Fall back to a runtime endian check */ |
35 | | const union { |
36 | | long l; |
37 | | char c; |
38 | | } sha1_endian = {1}; |
39 | | #define blk0(i) (sha1_endian.c == 0 ? blk0_be(i) : blk0_le(i)) |
40 | | #endif |
41 | | |
42 | | #define blk(i) \ |
43 | 0 | (block->l[i & 15] = \ |
44 | 0 | rol(block->l[(i + 13) & 15] ^ block->l[(i + 8) & 15] ^ block->l[(i + 2) & 15] ^ block->l[i & 15], 1)) |
45 | | |
46 | | /* (R0+R1), R2, R3, R4 are the different operations used in SHA1 */ |
47 | | #define R0(v, w, x, y, z, i) \ |
48 | 0 | z += ((w & (x ^ y)) ^ y) + blk0(i) + 0x5A827999 + rol(v, 5); \ |
49 | 0 | w = rol(w, 30); |
50 | | #define R1(v, w, x, y, z, i) \ |
51 | 0 | z += ((w & (x ^ y)) ^ y) + blk(i) + 0x5A827999 + rol(v, 5); \ |
52 | 0 | w = rol(w, 30); |
53 | | #define R2(v, w, x, y, z, i) \ |
54 | 0 | z += (w ^ x ^ y) + blk(i) + 0x6ED9EBA1 + rol(v, 5); \ |
55 | 0 | w = rol(w, 30); |
56 | | #define R3(v, w, x, y, z, i) \ |
57 | 0 | z += (((w | x) & y) | (w & x)) + blk(i) + 0x8F1BBCDC + rol(v, 5); \ |
58 | 0 | w = rol(w, 30); |
59 | | #define R4(v, w, x, y, z, i) \ |
60 | 0 | z += (w ^ x ^ y) + blk(i) + 0xCA62C1D6 + rol(v, 5); \ |
61 | 0 | w = rol(w, 30); |
62 | | |
63 | | /* Hash a single 512-bit block. This is the core of the algorithm. */ |
64 | 0 | void tSHA1Transform(uint32_t state[5], const unsigned char buffer[64]) { |
65 | 0 | uint32_t a, b, c, d, e; |
66 | |
|
67 | 0 | typedef union { |
68 | 0 | unsigned char c[64]; |
69 | 0 | uint32_t l[16]; |
70 | 0 | } CHAR64LONG16; |
71 | |
|
72 | 0 | #ifdef SHA1HANDSOFF |
73 | 0 | CHAR64LONG16 block[1]; /* use array to appear as a pointer */ |
74 | |
|
75 | 0 | memcpy(block, buffer, 64); |
76 | | #else |
77 | | /* The following had better never be used because it causes the |
78 | | * pointer-to-const buffer to be cast into a pointer to non-const. |
79 | | * And the result is written through. I threw a "const" in, hoping |
80 | | * this will cause a diagnostic. |
81 | | */ |
82 | | CHAR64LONG16 *block = (const CHAR64LONG16 *)buffer; |
83 | | #endif |
84 | | /* Copy context->state[] to working vars */ |
85 | 0 | a = state[0]; |
86 | 0 | b = state[1]; |
87 | 0 | c = state[2]; |
88 | 0 | d = state[3]; |
89 | 0 | e = state[4]; |
90 | | /* 4 rounds of 20 operations each. Loop unrolled. */ |
91 | 0 | R0(a, b, c, d, e, 0); |
92 | 0 | R0(e, a, b, c, d, 1); |
93 | 0 | R0(d, e, a, b, c, 2); |
94 | 0 | R0(c, d, e, a, b, 3); |
95 | 0 | R0(b, c, d, e, a, 4); |
96 | 0 | R0(a, b, c, d, e, 5); |
97 | 0 | R0(e, a, b, c, d, 6); |
98 | 0 | R0(d, e, a, b, c, 7); |
99 | 0 | R0(c, d, e, a, b, 8); |
100 | 0 | R0(b, c, d, e, a, 9); |
101 | 0 | R0(a, b, c, d, e, 10); |
102 | 0 | R0(e, a, b, c, d, 11); |
103 | 0 | R0(d, e, a, b, c, 12); |
104 | 0 | R0(c, d, e, a, b, 13); |
105 | 0 | R0(b, c, d, e, a, 14); |
106 | 0 | R0(a, b, c, d, e, 15); |
107 | 0 | R1(e, a, b, c, d, 16); |
108 | 0 | R1(d, e, a, b, c, 17); |
109 | 0 | R1(c, d, e, a, b, 18); |
110 | 0 | R1(b, c, d, e, a, 19); |
111 | 0 | R2(a, b, c, d, e, 20); |
112 | 0 | R2(e, a, b, c, d, 21); |
113 | 0 | R2(d, e, a, b, c, 22); |
114 | 0 | R2(c, d, e, a, b, 23); |
115 | 0 | R2(b, c, d, e, a, 24); |
116 | 0 | R2(a, b, c, d, e, 25); |
117 | 0 | R2(e, a, b, c, d, 26); |
118 | 0 | R2(d, e, a, b, c, 27); |
119 | 0 | R2(c, d, e, a, b, 28); |
120 | 0 | R2(b, c, d, e, a, 29); |
121 | 0 | R2(a, b, c, d, e, 30); |
122 | 0 | R2(e, a, b, c, d, 31); |
123 | 0 | R2(d, e, a, b, c, 32); |
124 | 0 | R2(c, d, e, a, b, 33); |
125 | 0 | R2(b, c, d, e, a, 34); |
126 | 0 | R2(a, b, c, d, e, 35); |
127 | 0 | R2(e, a, b, c, d, 36); |
128 | 0 | R2(d, e, a, b, c, 37); |
129 | 0 | R2(c, d, e, a, b, 38); |
130 | 0 | R2(b, c, d, e, a, 39); |
131 | 0 | R3(a, b, c, d, e, 40); |
132 | 0 | R3(e, a, b, c, d, 41); |
133 | 0 | R3(d, e, a, b, c, 42); |
134 | 0 | R3(c, d, e, a, b, 43); |
135 | 0 | R3(b, c, d, e, a, 44); |
136 | 0 | R3(a, b, c, d, e, 45); |
137 | 0 | R3(e, a, b, c, d, 46); |
138 | 0 | R3(d, e, a, b, c, 47); |
139 | 0 | R3(c, d, e, a, b, 48); |
140 | 0 | R3(b, c, d, e, a, 49); |
141 | 0 | R3(a, b, c, d, e, 50); |
142 | 0 | R3(e, a, b, c, d, 51); |
143 | 0 | R3(d, e, a, b, c, 52); |
144 | 0 | R3(c, d, e, a, b, 53); |
145 | 0 | R3(b, c, d, e, a, 54); |
146 | 0 | R3(a, b, c, d, e, 55); |
147 | 0 | R3(e, a, b, c, d, 56); |
148 | 0 | R3(d, e, a, b, c, 57); |
149 | 0 | R3(c, d, e, a, b, 58); |
150 | 0 | R3(b, c, d, e, a, 59); |
151 | 0 | R4(a, b, c, d, e, 60); |
152 | 0 | R4(e, a, b, c, d, 61); |
153 | 0 | R4(d, e, a, b, c, 62); |
154 | 0 | R4(c, d, e, a, b, 63); |
155 | 0 | R4(b, c, d, e, a, 64); |
156 | 0 | R4(a, b, c, d, e, 65); |
157 | 0 | R4(e, a, b, c, d, 66); |
158 | 0 | R4(d, e, a, b, c, 67); |
159 | 0 | R4(c, d, e, a, b, 68); |
160 | 0 | R4(b, c, d, e, a, 69); |
161 | 0 | R4(a, b, c, d, e, 70); |
162 | 0 | R4(e, a, b, c, d, 71); |
163 | 0 | R4(d, e, a, b, c, 72); |
164 | 0 | R4(c, d, e, a, b, 73); |
165 | 0 | R4(b, c, d, e, a, 74); |
166 | 0 | R4(a, b, c, d, e, 75); |
167 | 0 | R4(e, a, b, c, d, 76); |
168 | 0 | R4(d, e, a, b, c, 77); |
169 | 0 | R4(c, d, e, a, b, 78); |
170 | 0 | R4(b, c, d, e, a, 79); |
171 | | /* Add the working vars back into context.state[] */ |
172 | 0 | state[0] += a; |
173 | 0 | state[1] += b; |
174 | 0 | state[2] += c; |
175 | 0 | state[3] += d; |
176 | 0 | state[4] += e; |
177 | | /* Wipe variables */ |
178 | 0 | a = b = c = d = e = 0; |
179 | 0 | #ifdef SHA1HANDSOFF |
180 | 0 | memset(block, '\0', sizeof(block)); |
181 | 0 | #endif |
182 | 0 | } |
183 | | |
184 | | /* SHA1Init - Initialize new context */ |
185 | | |
186 | 0 | void tSHA1Init(T_SHA1_CTX *context) { |
187 | | /* SHA1 initialization constants */ |
188 | 0 | context->state[0] = 0x67452301; |
189 | 0 | context->state[1] = 0xEFCDAB89; |
190 | 0 | context->state[2] = 0x98BADCFE; |
191 | 0 | context->state[3] = 0x10325476; |
192 | 0 | context->state[4] = 0xC3D2E1F0; |
193 | 0 | context->count[0] = context->count[1] = 0; |
194 | 0 | } |
195 | | |
196 | | /* Run your data through this. */ |
197 | | |
198 | 0 | void tSHA1Update(T_SHA1_CTX *context, const unsigned char *data, uint32_t len) { |
199 | 0 | uint32_t i; |
200 | |
|
201 | 0 | uint32_t j; |
202 | |
|
203 | 0 | j = context->count[0]; |
204 | 0 | if ((context->count[0] += len << 3) < j) context->count[1]++; |
205 | 0 | context->count[1] += (len >> 29); |
206 | 0 | j = (j >> 3) & 63; |
207 | 0 | if ((j + len) > 63) { |
208 | 0 | memcpy(&context->buffer[j], data, (i = 64 - j)); |
209 | 0 | tSHA1Transform(context->state, context->buffer); |
210 | 0 | for (; i + 63 < len; i += 64) { |
211 | 0 | tSHA1Transform(context->state, &data[i]); |
212 | 0 | } |
213 | 0 | j = 0; |
214 | 0 | } else |
215 | 0 | i = 0; |
216 | 0 | memcpy(&context->buffer[j], &data[i], len - i); |
217 | 0 | } |
218 | | |
219 | | /* Add padding and return the message digest. */ |
220 | | |
221 | 0 | void tSHA1Final(unsigned char digest[20], T_SHA1_CTX *context) { |
222 | 0 | unsigned i; |
223 | |
|
224 | 0 | unsigned char finalcount[8]; |
225 | |
|
226 | 0 | unsigned char c; |
227 | |
|
228 | | #if 0 |
229 | | /* Convert context->count to a sequence of bytes |
230 | | * in finalcount. Second element first, but |
231 | | * big-endian order within element. |
232 | | * But we do it all backwards. |
233 | | */ |
234 | | unsigned char *fcp = &finalcount[8]; |
235 | | |
236 | | for (i = 0; i < 2; i++) |
237 | | { |
238 | | uint32_t t = context->count[i]; |
239 | | |
240 | | int j; |
241 | | |
242 | | for (j = 0; j < 4; t >>= 8, j++) |
243 | | *--fcp = (unsigned char) t} |
244 | | #else |
245 | 0 | for (i = 0; i < 8; i++) { |
246 | 0 | finalcount[i] = |
247 | 0 | (unsigned char)((context->count[(i >= 4 ? 0 : 1)] >> ((3 - (i & 3)) * 8)) & 255); /* Endian independent */ |
248 | 0 | } |
249 | 0 | #endif |
250 | 0 | c = 0200; |
251 | 0 | tSHA1Update(context, &c, 1); |
252 | 0 | while ((context->count[0] & 504) != 448) { |
253 | 0 | c = 0000; |
254 | 0 | tSHA1Update(context, &c, 1); |
255 | 0 | } |
256 | 0 | tSHA1Update(context, finalcount, 8); /* Should cause a tSHA1Transform() */ |
257 | 0 | for (i = 0; i < 20; i++) { |
258 | 0 | digest[i] = (unsigned char)((context->state[i >> 2] >> ((3 - (i & 3)) * 8)) & 255); |
259 | 0 | } |
260 | | /* Wipe variables */ |
261 | 0 | memset(context, '\0', sizeof(*context)); |
262 | 0 | memset(&finalcount, '\0', sizeof(finalcount)); |
263 | 0 | } |
264 | | |
265 | 0 | void tSHA1(char *hash_out, const char *str, uint32_t len) { |
266 | 0 | T_SHA1_CTX ctx; |
267 | 0 | unsigned int ii; |
268 | |
|
269 | 0 | tSHA1Init(&ctx); |
270 | 0 | for (ii = 0; ii < len; ii += 1) tSHA1Update(&ctx, (const unsigned char *)str + ii, 1); |
271 | 0 | tSHA1Final((unsigned char *)hash_out, &ctx); |
272 | 0 | } |
273 | | |
274 | | /* SHA2 algos */ |
275 | | |
276 | | #if 0 |
277 | | #define UNROLL_LOOPS /* Enable loops unrolling */ |
278 | | #endif |
279 | | |
280 | 0 | #define SHFR(x, n) (x >> n) |
281 | 0 | #define ROTR(x, n) ((x >> n) | (x << ((sizeof(x) << 3) - n))) |
282 | | #define ROTL(x, n) ((x << n) | (x >> ((sizeof(x) << 3) - n))) |
283 | 0 | #define CH(x, y, z) ((x & y) ^ (~x & z)) |
284 | 0 | #define MAJ(x, y, z) ((x & y) ^ (x & z) ^ (y & z)) |
285 | | |
286 | 0 | #define SHA256_F1(x) (ROTR(x, 2) ^ ROTR(x, 13) ^ ROTR(x, 22)) |
287 | 0 | #define SHA256_F2(x) (ROTR(x, 6) ^ ROTR(x, 11) ^ ROTR(x, 25)) |
288 | 0 | #define SHA256_F3(x) (ROTR(x, 7) ^ ROTR(x, 18) ^ SHFR(x, 3)) |
289 | 0 | #define SHA256_F4(x) (ROTR(x, 17) ^ ROTR(x, 19) ^ SHFR(x, 10)) |
290 | | |
291 | 0 | #define SHA512_F1(x) (ROTR(x, 28) ^ ROTR(x, 34) ^ ROTR(x, 39)) |
292 | 0 | #define SHA512_F2(x) (ROTR(x, 14) ^ ROTR(x, 18) ^ ROTR(x, 41)) |
293 | 0 | #define SHA512_F3(x) (ROTR(x, 1) ^ ROTR(x, 8) ^ SHFR(x, 7)) |
294 | 0 | #define SHA512_F4(x) (ROTR(x, 19) ^ ROTR(x, 61) ^ SHFR(x, 6)) |
295 | | |
296 | | #define UNPACK32(x, str) \ |
297 | 0 | { \ |
298 | 0 | *((str) + 3) = (uint8_t)((x)); \ |
299 | 0 | *((str) + 2) = (uint8_t)((x) >> 8); \ |
300 | 0 | *((str) + 1) = (uint8_t)((x) >> 16); \ |
301 | 0 | *((str) + 0) = (uint8_t)((x) >> 24); \ |
302 | 0 | } |
303 | | |
304 | | #define PACK32(str, x) \ |
305 | 0 | { \ |
306 | 0 | *(x) = ((uint32_t) * ((str) + 3)) | ((uint32_t) * ((str) + 2) << 8) | ((uint32_t) * ((str) + 1) << 16) | \ |
307 | 0 | ((uint32_t) * ((str) + 0) << 24); \ |
308 | 0 | } |
309 | | |
310 | | #define UNPACK64(x, str) \ |
311 | 0 | { \ |
312 | 0 | *((str) + 7) = (uint8_t)((x)); \ |
313 | 0 | *((str) + 6) = (uint8_t)((x) >> 8); \ |
314 | 0 | *((str) + 5) = (uint8_t)((x) >> 16); \ |
315 | 0 | *((str) + 4) = (uint8_t)((x) >> 24); \ |
316 | 0 | *((str) + 3) = (uint8_t)((x) >> 32); \ |
317 | 0 | *((str) + 2) = (uint8_t)((x) >> 40); \ |
318 | 0 | *((str) + 1) = (uint8_t)((x) >> 48); \ |
319 | 0 | *((str) + 0) = (uint8_t)((x) >> 56); \ |
320 | 0 | } |
321 | | |
322 | | #define PACK64(str, x) \ |
323 | 0 | { \ |
324 | 0 | *(x) = ((uint64_t) * ((str) + 7)) | ((uint64_t) * ((str) + 6) << 8) | ((uint64_t) * ((str) + 5) << 16) | \ |
325 | 0 | ((uint64_t) * ((str) + 4) << 24) | ((uint64_t) * ((str) + 3) << 32) | ((uint64_t) * ((str) + 2) << 40) | \ |
326 | 0 | ((uint64_t) * ((str) + 1) << 48) | ((uint64_t) * ((str) + 0) << 56); \ |
327 | 0 | } |
328 | | |
329 | | /* Macros used for loops unrolling */ |
330 | | |
331 | | #define SHA256_SCR(i) \ |
332 | 0 | { w[i] = SHA256_F4(w[i - 2]) + w[i - 7] + SHA256_F3(w[i - 15]) + w[i - 16]; } |
333 | | |
334 | | #define SHA512_SCR(i) \ |
335 | 0 | { w[i] = SHA512_F4(w[i - 2]) + w[i - 7] + SHA512_F3(w[i - 15]) + w[i - 16]; } |
336 | | |
337 | | #define SHA256_EXP(a, b, c, d, e, f, g, h, j) \ |
338 | | { \ |
339 | | t1 = wv[h] + SHA256_F2(wv[e]) + CH(wv[e], wv[f], wv[g]) + sha256_k[j] + w[j]; \ |
340 | | t2 = SHA256_F1(wv[a]) + MAJ(wv[a], wv[b], wv[c]); \ |
341 | | wv[d] += t1; \ |
342 | | wv[h] = t1 + t2; \ |
343 | | } |
344 | | |
345 | | #define SHA512_EXP(a, b, c, d, e, f, g, h, j) \ |
346 | | { \ |
347 | | t1 = wv[h] + SHA512_F2(wv[e]) + CH(wv[e], wv[f], wv[g]) + sha512_k[j] + w[j]; \ |
348 | | t2 = SHA512_F1(wv[a]) + MAJ(wv[a], wv[b], wv[c]); \ |
349 | | wv[d] += t1; \ |
350 | | wv[h] = t1 + t2; \ |
351 | | } |
352 | | |
353 | | static const uint32_t sha224_h0[8] = {0xc1059ed8, 0x367cd507, 0x3070dd17, 0xf70e5939, |
354 | | 0xffc00b31, 0x68581511, 0x64f98fa7, 0xbefa4fa4}; |
355 | | |
356 | | static const uint32_t sha256_h0[8] = {0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, |
357 | | 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19}; |
358 | | |
359 | | static const uint64_t sha384_h0[8] = {0xcbbb9d5dc1059ed8ULL, 0x629a292a367cd507ULL, 0x9159015a3070dd17ULL, |
360 | | 0x152fecd8f70e5939ULL, 0x67332667ffc00b31ULL, 0x8eb44a8768581511ULL, |
361 | | 0xdb0c2e0d64f98fa7ULL, 0x47b5481dbefa4fa4ULL}; |
362 | | |
363 | | static const uint64_t sha512_h0[8] = {0x6a09e667f3bcc908ULL, 0xbb67ae8584caa73bULL, 0x3c6ef372fe94f82bULL, |
364 | | 0xa54ff53a5f1d36f1ULL, 0x510e527fade682d1ULL, 0x9b05688c2b3e6c1fULL, |
365 | | 0x1f83d9abfb41bd6bULL, 0x5be0cd19137e2179ULL}; |
366 | | |
367 | | static const uint32_t sha256_k[64] = { |
368 | | 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, |
369 | | 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, |
370 | | 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, |
371 | | 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967, |
372 | | 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, |
373 | | 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, |
374 | | 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3, |
375 | | 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2}; |
376 | | |
377 | | static const uint64_t sha512_k[80] = { |
378 | | 0x428a2f98d728ae22ULL, 0x7137449123ef65cdULL, 0xb5c0fbcfec4d3b2fULL, 0xe9b5dba58189dbbcULL, 0x3956c25bf348b538ULL, |
379 | | 0x59f111f1b605d019ULL, 0x923f82a4af194f9bULL, 0xab1c5ed5da6d8118ULL, 0xd807aa98a3030242ULL, 0x12835b0145706fbeULL, |
380 | | 0x243185be4ee4b28cULL, 0x550c7dc3d5ffb4e2ULL, 0x72be5d74f27b896fULL, 0x80deb1fe3b1696b1ULL, 0x9bdc06a725c71235ULL, |
381 | | 0xc19bf174cf692694ULL, 0xe49b69c19ef14ad2ULL, 0xefbe4786384f25e3ULL, 0x0fc19dc68b8cd5b5ULL, 0x240ca1cc77ac9c65ULL, |
382 | | 0x2de92c6f592b0275ULL, 0x4a7484aa6ea6e483ULL, 0x5cb0a9dcbd41fbd4ULL, 0x76f988da831153b5ULL, 0x983e5152ee66dfabULL, |
383 | | 0xa831c66d2db43210ULL, 0xb00327c898fb213fULL, 0xbf597fc7beef0ee4ULL, 0xc6e00bf33da88fc2ULL, 0xd5a79147930aa725ULL, |
384 | | 0x06ca6351e003826fULL, 0x142929670a0e6e70ULL, 0x27b70a8546d22ffcULL, 0x2e1b21385c26c926ULL, 0x4d2c6dfc5ac42aedULL, |
385 | | 0x53380d139d95b3dfULL, 0x650a73548baf63deULL, 0x766a0abb3c77b2a8ULL, 0x81c2c92e47edaee6ULL, 0x92722c851482353bULL, |
386 | | 0xa2bfe8a14cf10364ULL, 0xa81a664bbc423001ULL, 0xc24b8b70d0f89791ULL, 0xc76c51a30654be30ULL, 0xd192e819d6ef5218ULL, |
387 | | 0xd69906245565a910ULL, 0xf40e35855771202aULL, 0x106aa07032bbd1b8ULL, 0x19a4c116b8d2d0c8ULL, 0x1e376c085141ab53ULL, |
388 | | 0x2748774cdf8eeb99ULL, 0x34b0bcb5e19b48a8ULL, 0x391c0cb3c5c95a63ULL, 0x4ed8aa4ae3418acbULL, 0x5b9cca4f7763e373ULL, |
389 | | 0x682e6ff3d6b2b8a3ULL, 0x748f82ee5defb2fcULL, 0x78a5636f43172f60ULL, 0x84c87814a1f0ab72ULL, 0x8cc702081a6439ecULL, |
390 | | 0x90befffa23631e28ULL, 0xa4506cebde82bde9ULL, 0xbef9a3f7b2c67915ULL, 0xc67178f2e372532bULL, 0xca273eceea26619cULL, |
391 | | 0xd186b8c721c0c207ULL, 0xeada7dd6cde0eb1eULL, 0xf57d4f7fee6ed178ULL, 0x06f067aa72176fbaULL, 0x0a637dc5a2c898a6ULL, |
392 | | 0x113f9804bef90daeULL, 0x1b710b35131c471bULL, 0x28db77f523047d84ULL, 0x32caab7b40c72493ULL, 0x3c9ebe0a15c9bebcULL, |
393 | | 0x431d67c49c100d4cULL, 0x4cc5d4becb3e42b6ULL, 0x597f299cfc657e2aULL, 0x5fcb6fab3ad6faecULL, 0x6c44198c4a475817ULL}; |
394 | | |
395 | | /* SHA-2 internal function */ |
396 | | |
397 | 0 | static void sha256_transf(sha256_ctx *ctx, const uint8_t *message, uint64_t block_nb) { |
398 | 0 | uint32_t w[64]; |
399 | 0 | uint32_t wv[8]; |
400 | 0 | uint32_t t1, t2; |
401 | 0 | const uint8_t *sub_block; |
402 | 0 | uint64_t i; |
403 | |
|
404 | 0 | #ifndef UNROLL_LOOPS |
405 | 0 | int j; |
406 | 0 | #endif |
407 | |
|
408 | 0 | for (i = 0; i < block_nb; i++) { |
409 | 0 | sub_block = message + (i << 6); |
410 | |
|
411 | 0 | #ifndef UNROLL_LOOPS |
412 | 0 | for (j = 0; j < 16; j++) { |
413 | 0 | PACK32(&sub_block[j << 2], &w[j]); |
414 | 0 | } |
415 | |
|
416 | 0 | for (j = 16; j < 64; j++) { |
417 | 0 | SHA256_SCR(j); |
418 | 0 | } |
419 | |
|
420 | 0 | for (j = 0; j < 8; j++) { |
421 | 0 | wv[j] = ctx->h[j]; |
422 | 0 | } |
423 | |
|
424 | 0 | for (j = 0; j < 64; j++) { |
425 | 0 | t1 = wv[7] + SHA256_F2(wv[4]) + CH(wv[4], wv[5], wv[6]) + sha256_k[j] + w[j]; |
426 | 0 | t2 = SHA256_F1(wv[0]) + MAJ(wv[0], wv[1], wv[2]); |
427 | 0 | wv[7] = wv[6]; |
428 | 0 | wv[6] = wv[5]; |
429 | 0 | wv[5] = wv[4]; |
430 | 0 | wv[4] = wv[3] + t1; |
431 | 0 | wv[3] = wv[2]; |
432 | 0 | wv[2] = wv[1]; |
433 | 0 | wv[1] = wv[0]; |
434 | 0 | wv[0] = t1 + t2; |
435 | 0 | } |
436 | |
|
437 | 0 | for (j = 0; j < 8; j++) { |
438 | 0 | ctx->h[j] += wv[j]; |
439 | 0 | } |
440 | | #else |
441 | | PACK32(&sub_block[0], &w[0]); |
442 | | PACK32(&sub_block[4], &w[1]); |
443 | | PACK32(&sub_block[8], &w[2]); |
444 | | PACK32(&sub_block[12], &w[3]); |
445 | | PACK32(&sub_block[16], &w[4]); |
446 | | PACK32(&sub_block[20], &w[5]); |
447 | | PACK32(&sub_block[24], &w[6]); |
448 | | PACK32(&sub_block[28], &w[7]); |
449 | | PACK32(&sub_block[32], &w[8]); |
450 | | PACK32(&sub_block[36], &w[9]); |
451 | | PACK32(&sub_block[40], &w[10]); |
452 | | PACK32(&sub_block[44], &w[11]); |
453 | | PACK32(&sub_block[48], &w[12]); |
454 | | PACK32(&sub_block[52], &w[13]); |
455 | | PACK32(&sub_block[56], &w[14]); |
456 | | PACK32(&sub_block[60], &w[15]); |
457 | | |
458 | | SHA256_SCR(16); |
459 | | SHA256_SCR(17); |
460 | | SHA256_SCR(18); |
461 | | SHA256_SCR(19); |
462 | | SHA256_SCR(20); |
463 | | SHA256_SCR(21); |
464 | | SHA256_SCR(22); |
465 | | SHA256_SCR(23); |
466 | | SHA256_SCR(24); |
467 | | SHA256_SCR(25); |
468 | | SHA256_SCR(26); |
469 | | SHA256_SCR(27); |
470 | | SHA256_SCR(28); |
471 | | SHA256_SCR(29); |
472 | | SHA256_SCR(30); |
473 | | SHA256_SCR(31); |
474 | | SHA256_SCR(32); |
475 | | SHA256_SCR(33); |
476 | | SHA256_SCR(34); |
477 | | SHA256_SCR(35); |
478 | | SHA256_SCR(36); |
479 | | SHA256_SCR(37); |
480 | | SHA256_SCR(38); |
481 | | SHA256_SCR(39); |
482 | | SHA256_SCR(40); |
483 | | SHA256_SCR(41); |
484 | | SHA256_SCR(42); |
485 | | SHA256_SCR(43); |
486 | | SHA256_SCR(44); |
487 | | SHA256_SCR(45); |
488 | | SHA256_SCR(46); |
489 | | SHA256_SCR(47); |
490 | | SHA256_SCR(48); |
491 | | SHA256_SCR(49); |
492 | | SHA256_SCR(50); |
493 | | SHA256_SCR(51); |
494 | | SHA256_SCR(52); |
495 | | SHA256_SCR(53); |
496 | | SHA256_SCR(54); |
497 | | SHA256_SCR(55); |
498 | | SHA256_SCR(56); |
499 | | SHA256_SCR(57); |
500 | | SHA256_SCR(58); |
501 | | SHA256_SCR(59); |
502 | | SHA256_SCR(60); |
503 | | SHA256_SCR(61); |
504 | | SHA256_SCR(62); |
505 | | SHA256_SCR(63); |
506 | | |
507 | | wv[0] = ctx->h[0]; |
508 | | wv[1] = ctx->h[1]; |
509 | | wv[2] = ctx->h[2]; |
510 | | wv[3] = ctx->h[3]; |
511 | | wv[4] = ctx->h[4]; |
512 | | wv[5] = ctx->h[5]; |
513 | | wv[6] = ctx->h[6]; |
514 | | wv[7] = ctx->h[7]; |
515 | | |
516 | | SHA256_EXP(0, 1, 2, 3, 4, 5, 6, 7, 0); |
517 | | SHA256_EXP(7, 0, 1, 2, 3, 4, 5, 6, 1); |
518 | | SHA256_EXP(6, 7, 0, 1, 2, 3, 4, 5, 2); |
519 | | SHA256_EXP(5, 6, 7, 0, 1, 2, 3, 4, 3); |
520 | | SHA256_EXP(4, 5, 6, 7, 0, 1, 2, 3, 4); |
521 | | SHA256_EXP(3, 4, 5, 6, 7, 0, 1, 2, 5); |
522 | | SHA256_EXP(2, 3, 4, 5, 6, 7, 0, 1, 6); |
523 | | SHA256_EXP(1, 2, 3, 4, 5, 6, 7, 0, 7); |
524 | | SHA256_EXP(0, 1, 2, 3, 4, 5, 6, 7, 8); |
525 | | SHA256_EXP(7, 0, 1, 2, 3, 4, 5, 6, 9); |
526 | | SHA256_EXP(6, 7, 0, 1, 2, 3, 4, 5, 10); |
527 | | SHA256_EXP(5, 6, 7, 0, 1, 2, 3, 4, 11); |
528 | | SHA256_EXP(4, 5, 6, 7, 0, 1, 2, 3, 12); |
529 | | SHA256_EXP(3, 4, 5, 6, 7, 0, 1, 2, 13); |
530 | | SHA256_EXP(2, 3, 4, 5, 6, 7, 0, 1, 14); |
531 | | SHA256_EXP(1, 2, 3, 4, 5, 6, 7, 0, 15); |
532 | | SHA256_EXP(0, 1, 2, 3, 4, 5, 6, 7, 16); |
533 | | SHA256_EXP(7, 0, 1, 2, 3, 4, 5, 6, 17); |
534 | | SHA256_EXP(6, 7, 0, 1, 2, 3, 4, 5, 18); |
535 | | SHA256_EXP(5, 6, 7, 0, 1, 2, 3, 4, 19); |
536 | | SHA256_EXP(4, 5, 6, 7, 0, 1, 2, 3, 20); |
537 | | SHA256_EXP(3, 4, 5, 6, 7, 0, 1, 2, 21); |
538 | | SHA256_EXP(2, 3, 4, 5, 6, 7, 0, 1, 22); |
539 | | SHA256_EXP(1, 2, 3, 4, 5, 6, 7, 0, 23); |
540 | | SHA256_EXP(0, 1, 2, 3, 4, 5, 6, 7, 24); |
541 | | SHA256_EXP(7, 0, 1, 2, 3, 4, 5, 6, 25); |
542 | | SHA256_EXP(6, 7, 0, 1, 2, 3, 4, 5, 26); |
543 | | SHA256_EXP(5, 6, 7, 0, 1, 2, 3, 4, 27); |
544 | | SHA256_EXP(4, 5, 6, 7, 0, 1, 2, 3, 28); |
545 | | SHA256_EXP(3, 4, 5, 6, 7, 0, 1, 2, 29); |
546 | | SHA256_EXP(2, 3, 4, 5, 6, 7, 0, 1, 30); |
547 | | SHA256_EXP(1, 2, 3, 4, 5, 6, 7, 0, 31); |
548 | | SHA256_EXP(0, 1, 2, 3, 4, 5, 6, 7, 32); |
549 | | SHA256_EXP(7, 0, 1, 2, 3, 4, 5, 6, 33); |
550 | | SHA256_EXP(6, 7, 0, 1, 2, 3, 4, 5, 34); |
551 | | SHA256_EXP(5, 6, 7, 0, 1, 2, 3, 4, 35); |
552 | | SHA256_EXP(4, 5, 6, 7, 0, 1, 2, 3, 36); |
553 | | SHA256_EXP(3, 4, 5, 6, 7, 0, 1, 2, 37); |
554 | | SHA256_EXP(2, 3, 4, 5, 6, 7, 0, 1, 38); |
555 | | SHA256_EXP(1, 2, 3, 4, 5, 6, 7, 0, 39); |
556 | | SHA256_EXP(0, 1, 2, 3, 4, 5, 6, 7, 40); |
557 | | SHA256_EXP(7, 0, 1, 2, 3, 4, 5, 6, 41); |
558 | | SHA256_EXP(6, 7, 0, 1, 2, 3, 4, 5, 42); |
559 | | SHA256_EXP(5, 6, 7, 0, 1, 2, 3, 4, 43); |
560 | | SHA256_EXP(4, 5, 6, 7, 0, 1, 2, 3, 44); |
561 | | SHA256_EXP(3, 4, 5, 6, 7, 0, 1, 2, 45); |
562 | | SHA256_EXP(2, 3, 4, 5, 6, 7, 0, 1, 46); |
563 | | SHA256_EXP(1, 2, 3, 4, 5, 6, 7, 0, 47); |
564 | | SHA256_EXP(0, 1, 2, 3, 4, 5, 6, 7, 48); |
565 | | SHA256_EXP(7, 0, 1, 2, 3, 4, 5, 6, 49); |
566 | | SHA256_EXP(6, 7, 0, 1, 2, 3, 4, 5, 50); |
567 | | SHA256_EXP(5, 6, 7, 0, 1, 2, 3, 4, 51); |
568 | | SHA256_EXP(4, 5, 6, 7, 0, 1, 2, 3, 52); |
569 | | SHA256_EXP(3, 4, 5, 6, 7, 0, 1, 2, 53); |
570 | | SHA256_EXP(2, 3, 4, 5, 6, 7, 0, 1, 54); |
571 | | SHA256_EXP(1, 2, 3, 4, 5, 6, 7, 0, 55); |
572 | | SHA256_EXP(0, 1, 2, 3, 4, 5, 6, 7, 56); |
573 | | SHA256_EXP(7, 0, 1, 2, 3, 4, 5, 6, 57); |
574 | | SHA256_EXP(6, 7, 0, 1, 2, 3, 4, 5, 58); |
575 | | SHA256_EXP(5, 6, 7, 0, 1, 2, 3, 4, 59); |
576 | | SHA256_EXP(4, 5, 6, 7, 0, 1, 2, 3, 60); |
577 | | SHA256_EXP(3, 4, 5, 6, 7, 0, 1, 2, 61); |
578 | | SHA256_EXP(2, 3, 4, 5, 6, 7, 0, 1, 62); |
579 | | SHA256_EXP(1, 2, 3, 4, 5, 6, 7, 0, 63); |
580 | | |
581 | | ctx->h[0] += wv[0]; |
582 | | ctx->h[1] += wv[1]; |
583 | | ctx->h[2] += wv[2]; |
584 | | ctx->h[3] += wv[3]; |
585 | | ctx->h[4] += wv[4]; |
586 | | ctx->h[5] += wv[5]; |
587 | | ctx->h[6] += wv[6]; |
588 | | ctx->h[7] += wv[7]; |
589 | | #endif /* !UNROLL_LOOPS */ |
590 | 0 | } |
591 | 0 | } |
592 | | |
593 | 0 | static void sha512_transf(sha512_ctx *ctx, const uint8_t *message, uint64_t block_nb) { |
594 | 0 | uint64_t w[80]; |
595 | 0 | uint64_t wv[8]; |
596 | 0 | uint64_t t1, t2; |
597 | 0 | const uint8_t *sub_block; |
598 | 0 | uint64_t i; |
599 | 0 | int j; |
600 | |
|
601 | 0 | for (i = 0; i < block_nb; i++) { |
602 | 0 | sub_block = message + (i << 7); |
603 | |
|
604 | 0 | #ifndef UNROLL_LOOPS |
605 | 0 | for (j = 0; j < 16; j++) { |
606 | 0 | PACK64(&sub_block[j << 3], &w[j]); |
607 | 0 | } |
608 | |
|
609 | 0 | for (j = 16; j < 80; j++) { |
610 | 0 | SHA512_SCR(j); |
611 | 0 | } |
612 | |
|
613 | 0 | for (j = 0; j < 8; j++) { |
614 | 0 | wv[j] = ctx->h[j]; |
615 | 0 | } |
616 | |
|
617 | 0 | for (j = 0; j < 80; j++) { |
618 | 0 | t1 = wv[7] + SHA512_F2(wv[4]) + CH(wv[4], wv[5], wv[6]) + sha512_k[j] + w[j]; |
619 | 0 | t2 = SHA512_F1(wv[0]) + MAJ(wv[0], wv[1], wv[2]); |
620 | 0 | wv[7] = wv[6]; |
621 | 0 | wv[6] = wv[5]; |
622 | 0 | wv[5] = wv[4]; |
623 | 0 | wv[4] = wv[3] + t1; |
624 | 0 | wv[3] = wv[2]; |
625 | 0 | wv[2] = wv[1]; |
626 | 0 | wv[1] = wv[0]; |
627 | 0 | wv[0] = t1 + t2; |
628 | 0 | } |
629 | |
|
630 | 0 | for (j = 0; j < 8; j++) { |
631 | 0 | ctx->h[j] += wv[j]; |
632 | 0 | } |
633 | | #else |
634 | | PACK64(&sub_block[0], &w[0]); |
635 | | PACK64(&sub_block[8], &w[1]); |
636 | | PACK64(&sub_block[16], &w[2]); |
637 | | PACK64(&sub_block[24], &w[3]); |
638 | | PACK64(&sub_block[32], &w[4]); |
639 | | PACK64(&sub_block[40], &w[5]); |
640 | | PACK64(&sub_block[48], &w[6]); |
641 | | PACK64(&sub_block[56], &w[7]); |
642 | | PACK64(&sub_block[64], &w[8]); |
643 | | PACK64(&sub_block[72], &w[9]); |
644 | | PACK64(&sub_block[80], &w[10]); |
645 | | PACK64(&sub_block[88], &w[11]); |
646 | | PACK64(&sub_block[96], &w[12]); |
647 | | PACK64(&sub_block[104], &w[13]); |
648 | | PACK64(&sub_block[112], &w[14]); |
649 | | PACK64(&sub_block[120], &w[15]); |
650 | | |
651 | | SHA512_SCR(16); |
652 | | SHA512_SCR(17); |
653 | | SHA512_SCR(18); |
654 | | SHA512_SCR(19); |
655 | | SHA512_SCR(20); |
656 | | SHA512_SCR(21); |
657 | | SHA512_SCR(22); |
658 | | SHA512_SCR(23); |
659 | | SHA512_SCR(24); |
660 | | SHA512_SCR(25); |
661 | | SHA512_SCR(26); |
662 | | SHA512_SCR(27); |
663 | | SHA512_SCR(28); |
664 | | SHA512_SCR(29); |
665 | | SHA512_SCR(30); |
666 | | SHA512_SCR(31); |
667 | | SHA512_SCR(32); |
668 | | SHA512_SCR(33); |
669 | | SHA512_SCR(34); |
670 | | SHA512_SCR(35); |
671 | | SHA512_SCR(36); |
672 | | SHA512_SCR(37); |
673 | | SHA512_SCR(38); |
674 | | SHA512_SCR(39); |
675 | | SHA512_SCR(40); |
676 | | SHA512_SCR(41); |
677 | | SHA512_SCR(42); |
678 | | SHA512_SCR(43); |
679 | | SHA512_SCR(44); |
680 | | SHA512_SCR(45); |
681 | | SHA512_SCR(46); |
682 | | SHA512_SCR(47); |
683 | | SHA512_SCR(48); |
684 | | SHA512_SCR(49); |
685 | | SHA512_SCR(50); |
686 | | SHA512_SCR(51); |
687 | | SHA512_SCR(52); |
688 | | SHA512_SCR(53); |
689 | | SHA512_SCR(54); |
690 | | SHA512_SCR(55); |
691 | | SHA512_SCR(56); |
692 | | SHA512_SCR(57); |
693 | | SHA512_SCR(58); |
694 | | SHA512_SCR(59); |
695 | | SHA512_SCR(60); |
696 | | SHA512_SCR(61); |
697 | | SHA512_SCR(62); |
698 | | SHA512_SCR(63); |
699 | | SHA512_SCR(64); |
700 | | SHA512_SCR(65); |
701 | | SHA512_SCR(66); |
702 | | SHA512_SCR(67); |
703 | | SHA512_SCR(68); |
704 | | SHA512_SCR(69); |
705 | | SHA512_SCR(70); |
706 | | SHA512_SCR(71); |
707 | | SHA512_SCR(72); |
708 | | SHA512_SCR(73); |
709 | | SHA512_SCR(74); |
710 | | SHA512_SCR(75); |
711 | | SHA512_SCR(76); |
712 | | SHA512_SCR(77); |
713 | | SHA512_SCR(78); |
714 | | SHA512_SCR(79); |
715 | | |
716 | | wv[0] = ctx->h[0]; |
717 | | wv[1] = ctx->h[1]; |
718 | | wv[2] = ctx->h[2]; |
719 | | wv[3] = ctx->h[3]; |
720 | | wv[4] = ctx->h[4]; |
721 | | wv[5] = ctx->h[5]; |
722 | | wv[6] = ctx->h[6]; |
723 | | wv[7] = ctx->h[7]; |
724 | | |
725 | | j = 0; |
726 | | |
727 | | do { |
728 | | SHA512_EXP(0, 1, 2, 3, 4, 5, 6, 7, j); |
729 | | j++; |
730 | | SHA512_EXP(7, 0, 1, 2, 3, 4, 5, 6, j); |
731 | | j++; |
732 | | SHA512_EXP(6, 7, 0, 1, 2, 3, 4, 5, j); |
733 | | j++; |
734 | | SHA512_EXP(5, 6, 7, 0, 1, 2, 3, 4, j); |
735 | | j++; |
736 | | SHA512_EXP(4, 5, 6, 7, 0, 1, 2, 3, j); |
737 | | j++; |
738 | | SHA512_EXP(3, 4, 5, 6, 7, 0, 1, 2, j); |
739 | | j++; |
740 | | SHA512_EXP(2, 3, 4, 5, 6, 7, 0, 1, j); |
741 | | j++; |
742 | | SHA512_EXP(1, 2, 3, 4, 5, 6, 7, 0, j); |
743 | | j++; |
744 | | } while (j < 80); |
745 | | |
746 | | ctx->h[0] += wv[0]; |
747 | | ctx->h[1] += wv[1]; |
748 | | ctx->h[2] += wv[2]; |
749 | | ctx->h[3] += wv[3]; |
750 | | ctx->h[4] += wv[4]; |
751 | | ctx->h[5] += wv[5]; |
752 | | ctx->h[6] += wv[6]; |
753 | | ctx->h[7] += wv[7]; |
754 | | #endif /* !UNROLL_LOOPS */ |
755 | 0 | } |
756 | 0 | } |
757 | | |
758 | | /* SHA-224 functions */ |
759 | | |
760 | 0 | void sha224(const uint8_t *message, uint64_t len, uint8_t *digest) { |
761 | 0 | sha224_ctx ctx; |
762 | |
|
763 | 0 | sha224_init(&ctx); |
764 | 0 | sha224_update(&ctx, message, len); |
765 | 0 | sha224_final(&ctx, digest); |
766 | 0 | } |
767 | | |
768 | 0 | void sha224_init(sha224_ctx *ctx) { |
769 | 0 | #ifndef UNROLL_LOOPS |
770 | 0 | int i; |
771 | 0 | for (i = 0; i < 8; i++) { |
772 | 0 | ctx->h[i] = sha224_h0[i]; |
773 | 0 | } |
774 | | #else |
775 | | ctx->h[0] = sha224_h0[0]; |
776 | | ctx->h[1] = sha224_h0[1]; |
777 | | ctx->h[2] = sha224_h0[2]; |
778 | | ctx->h[3] = sha224_h0[3]; |
779 | | ctx->h[4] = sha224_h0[4]; |
780 | | ctx->h[5] = sha224_h0[5]; |
781 | | ctx->h[6] = sha224_h0[6]; |
782 | | ctx->h[7] = sha224_h0[7]; |
783 | | #endif /* !UNROLL_LOOPS */ |
784 | |
|
785 | 0 | ctx->len = 0; |
786 | 0 | ctx->tot_len = 0; |
787 | 0 | } |
788 | | |
789 | 0 | void sha224_update(sha224_ctx *ctx, const uint8_t *message, uint64_t len) { |
790 | 0 | uint64_t block_nb; |
791 | 0 | uint64_t new_len, rem_len, tmp_len; |
792 | 0 | const uint8_t *shifted_message; |
793 | |
|
794 | 0 | tmp_len = SHA224_BLOCK_SIZE - ctx->len; |
795 | 0 | rem_len = len < tmp_len ? len : tmp_len; |
796 | |
|
797 | 0 | memcpy(&ctx->block[ctx->len], message, rem_len); |
798 | |
|
799 | 0 | if (ctx->len + len < SHA224_BLOCK_SIZE) { |
800 | 0 | ctx->len += len; |
801 | 0 | return; |
802 | 0 | } |
803 | | |
804 | 0 | new_len = len - rem_len; |
805 | 0 | block_nb = new_len / SHA224_BLOCK_SIZE; |
806 | |
|
807 | 0 | shifted_message = message + rem_len; |
808 | |
|
809 | 0 | sha256_transf(ctx, ctx->block, 1); |
810 | 0 | sha256_transf(ctx, shifted_message, block_nb); |
811 | |
|
812 | 0 | rem_len = new_len % SHA224_BLOCK_SIZE; |
813 | |
|
814 | 0 | memcpy(ctx->block, &shifted_message[block_nb << 6], rem_len); |
815 | |
|
816 | 0 | ctx->len = rem_len; |
817 | 0 | ctx->tot_len += (block_nb + 1) << 6; |
818 | 0 | } |
819 | | |
820 | 0 | void sha224_final(sha224_ctx *ctx, uint8_t *digest) { |
821 | 0 | uint64_t block_nb; |
822 | 0 | uint64_t pm_len; |
823 | 0 | uint64_t len_b; |
824 | 0 | uint64_t tot_len; |
825 | |
|
826 | 0 | #ifndef UNROLL_LOOPS |
827 | 0 | int i; |
828 | 0 | #endif |
829 | |
|
830 | 0 | block_nb = (1 + ((SHA224_BLOCK_SIZE - 9) < (ctx->len % SHA224_BLOCK_SIZE))); |
831 | |
|
832 | 0 | tot_len = ctx->tot_len + ctx->len; |
833 | 0 | ctx->tot_len = tot_len; |
834 | |
|
835 | 0 | len_b = tot_len << 3; |
836 | 0 | pm_len = block_nb << 6; |
837 | |
|
838 | 0 | memset(ctx->block + ctx->len, 0, pm_len - ctx->len); |
839 | 0 | ctx->block[ctx->len] = 0x80; |
840 | 0 | UNPACK64(len_b, ctx->block + pm_len - 8); |
841 | |
|
842 | 0 | sha256_transf(ctx, ctx->block, block_nb); |
843 | |
|
844 | 0 | #ifndef UNROLL_LOOPS |
845 | 0 | for (i = 0; i < 7; i++) { |
846 | 0 | UNPACK32(ctx->h[i], &digest[i << 2]); |
847 | 0 | } |
848 | | #else |
849 | | UNPACK32(ctx->h[0], &digest[0]); |
850 | | UNPACK32(ctx->h[1], &digest[4]); |
851 | | UNPACK32(ctx->h[2], &digest[8]); |
852 | | UNPACK32(ctx->h[3], &digest[12]); |
853 | | UNPACK32(ctx->h[4], &digest[16]); |
854 | | UNPACK32(ctx->h[5], &digest[20]); |
855 | | UNPACK32(ctx->h[6], &digest[24]); |
856 | | #endif /* !UNROLL_LOOPS */ |
857 | 0 | } |
858 | | |
859 | | /* SHA-256 functions */ |
860 | | |
861 | 0 | void sha256(const uint8_t *message, uint64_t len, uint8_t *digest) { |
862 | 0 | sha256_ctx ctx; |
863 | |
|
864 | 0 | sha256_init(&ctx); |
865 | 0 | sha256_update(&ctx, message, len); |
866 | 0 | sha256_final(&ctx, digest); |
867 | 0 | } |
868 | | |
869 | 0 | void sha256_init(sha256_ctx *ctx) { |
870 | 0 | #ifndef UNROLL_LOOPS |
871 | 0 | int i; |
872 | 0 | for (i = 0; i < 8; i++) { |
873 | 0 | ctx->h[i] = sha256_h0[i]; |
874 | 0 | } |
875 | | #else |
876 | | ctx->h[0] = sha256_h0[0]; |
877 | | ctx->h[1] = sha256_h0[1]; |
878 | | ctx->h[2] = sha256_h0[2]; |
879 | | ctx->h[3] = sha256_h0[3]; |
880 | | ctx->h[4] = sha256_h0[4]; |
881 | | ctx->h[5] = sha256_h0[5]; |
882 | | ctx->h[6] = sha256_h0[6]; |
883 | | ctx->h[7] = sha256_h0[7]; |
884 | | #endif /* !UNROLL_LOOPS */ |
885 | |
|
886 | 0 | ctx->len = 0; |
887 | 0 | ctx->tot_len = 0; |
888 | 0 | } |
889 | | |
890 | 0 | void sha256_update(sha256_ctx *ctx, const uint8_t *message, uint64_t len) { |
891 | 0 | uint64_t block_nb; |
892 | 0 | uint64_t new_len, rem_len, tmp_len; |
893 | 0 | const uint8_t *shifted_message; |
894 | |
|
895 | 0 | tmp_len = SHA256_BLOCK_SIZE - ctx->len; |
896 | 0 | rem_len = len < tmp_len ? len : tmp_len; |
897 | |
|
898 | 0 | memcpy(&ctx->block[ctx->len], message, rem_len); |
899 | |
|
900 | 0 | if (ctx->len + len < SHA256_BLOCK_SIZE) { |
901 | 0 | ctx->len += len; |
902 | 0 | return; |
903 | 0 | } |
904 | | |
905 | 0 | new_len = len - rem_len; |
906 | 0 | block_nb = new_len / SHA256_BLOCK_SIZE; |
907 | |
|
908 | 0 | shifted_message = message + rem_len; |
909 | |
|
910 | 0 | sha256_transf(ctx, ctx->block, 1); |
911 | 0 | sha256_transf(ctx, shifted_message, block_nb); |
912 | |
|
913 | 0 | rem_len = new_len % SHA256_BLOCK_SIZE; |
914 | |
|
915 | 0 | memcpy(ctx->block, &shifted_message[block_nb << 6], rem_len); |
916 | |
|
917 | 0 | ctx->len = rem_len; |
918 | 0 | ctx->tot_len += (block_nb + 1) << 6; |
919 | 0 | } |
920 | | |
921 | 0 | void sha256_final(sha256_ctx *ctx, uint8_t *digest) { |
922 | 0 | uint64_t block_nb; |
923 | 0 | uint64_t pm_len; |
924 | 0 | uint64_t len_b; |
925 | 0 | uint64_t tot_len; |
926 | |
|
927 | 0 | #ifndef UNROLL_LOOPS |
928 | 0 | int i; |
929 | 0 | #endif |
930 | |
|
931 | 0 | block_nb = (1 + ((SHA256_BLOCK_SIZE - 9) < (ctx->len % SHA256_BLOCK_SIZE))); |
932 | |
|
933 | 0 | tot_len = ctx->tot_len + ctx->len; |
934 | 0 | ctx->tot_len = tot_len; |
935 | |
|
936 | 0 | len_b = tot_len << 3; |
937 | 0 | pm_len = block_nb << 6; |
938 | |
|
939 | 0 | memset(ctx->block + ctx->len, 0, pm_len - ctx->len); |
940 | 0 | ctx->block[ctx->len] = 0x80; |
941 | 0 | UNPACK64(len_b, ctx->block + pm_len - 8); |
942 | |
|
943 | 0 | sha256_transf(ctx, ctx->block, block_nb); |
944 | |
|
945 | 0 | #ifndef UNROLL_LOOPS |
946 | 0 | for (i = 0; i < 8; i++) { |
947 | 0 | UNPACK32(ctx->h[i], &digest[i << 2]); |
948 | 0 | } |
949 | | #else |
950 | | UNPACK32(ctx->h[0], &digest[0]); |
951 | | UNPACK32(ctx->h[1], &digest[4]); |
952 | | UNPACK32(ctx->h[2], &digest[8]); |
953 | | UNPACK32(ctx->h[3], &digest[12]); |
954 | | UNPACK32(ctx->h[4], &digest[16]); |
955 | | UNPACK32(ctx->h[5], &digest[20]); |
956 | | UNPACK32(ctx->h[6], &digest[24]); |
957 | | UNPACK32(ctx->h[7], &digest[28]); |
958 | | #endif /* !UNROLL_LOOPS */ |
959 | 0 | } |
960 | | |
961 | | /* SHA-384 functions */ |
962 | | |
963 | 0 | void sha384(const uint8_t *message, uint64_t len, uint8_t *digest) { |
964 | 0 | sha384_ctx ctx; |
965 | |
|
966 | 0 | sha384_init(&ctx); |
967 | 0 | sha384_update(&ctx, message, len); |
968 | 0 | sha384_final(&ctx, digest); |
969 | 0 | } |
970 | | |
971 | 0 | void sha384_init(sha384_ctx *ctx) { |
972 | 0 | #ifndef UNROLL_LOOPS |
973 | 0 | int i; |
974 | 0 | for (i = 0; i < 8; i++) { |
975 | 0 | ctx->h[i] = sha384_h0[i]; |
976 | 0 | } |
977 | | #else |
978 | | ctx->h[0] = sha384_h0[0]; |
979 | | ctx->h[1] = sha384_h0[1]; |
980 | | ctx->h[2] = sha384_h0[2]; |
981 | | ctx->h[3] = sha384_h0[3]; |
982 | | ctx->h[4] = sha384_h0[4]; |
983 | | ctx->h[5] = sha384_h0[5]; |
984 | | ctx->h[6] = sha384_h0[6]; |
985 | | ctx->h[7] = sha384_h0[7]; |
986 | | #endif /* !UNROLL_LOOPS */ |
987 | |
|
988 | 0 | ctx->len = 0; |
989 | 0 | ctx->tot_len = 0; |
990 | 0 | } |
991 | | |
992 | 0 | void sha384_update(sha384_ctx *ctx, const uint8_t *message, uint64_t len) { |
993 | 0 | uint64_t block_nb; |
994 | 0 | uint64_t new_len, rem_len, tmp_len; |
995 | 0 | const uint8_t *shifted_message; |
996 | |
|
997 | 0 | tmp_len = SHA384_BLOCK_SIZE - ctx->len; |
998 | 0 | rem_len = len < tmp_len ? len : tmp_len; |
999 | |
|
1000 | 0 | memcpy(&ctx->block[ctx->len], message, rem_len); |
1001 | |
|
1002 | 0 | if (ctx->len + len < SHA384_BLOCK_SIZE) { |
1003 | 0 | ctx->len += len; |
1004 | 0 | return; |
1005 | 0 | } |
1006 | | |
1007 | 0 | new_len = len - rem_len; |
1008 | 0 | block_nb = new_len / SHA384_BLOCK_SIZE; |
1009 | |
|
1010 | 0 | shifted_message = message + rem_len; |
1011 | |
|
1012 | 0 | sha512_transf(ctx, ctx->block, 1); |
1013 | 0 | sha512_transf(ctx, shifted_message, block_nb); |
1014 | |
|
1015 | 0 | rem_len = new_len % SHA384_BLOCK_SIZE; |
1016 | |
|
1017 | 0 | memcpy(ctx->block, &shifted_message[block_nb << 7], rem_len); |
1018 | |
|
1019 | 0 | ctx->len = rem_len; |
1020 | 0 | ctx->tot_len += (block_nb + 1) << 7; |
1021 | 0 | } |
1022 | | |
1023 | 0 | void sha384_final(sha384_ctx *ctx, uint8_t *digest) { |
1024 | 0 | uint64_t block_nb; |
1025 | 0 | uint64_t pm_len; |
1026 | 0 | uint64_t len_b; |
1027 | 0 | uint64_t tot_len; |
1028 | |
|
1029 | 0 | #ifndef UNROLL_LOOPS |
1030 | 0 | int i; |
1031 | 0 | #endif |
1032 | |
|
1033 | 0 | block_nb = (1 + ((SHA384_BLOCK_SIZE - 17) < (ctx->len % SHA384_BLOCK_SIZE))); |
1034 | |
|
1035 | 0 | tot_len = ctx->tot_len + ctx->len; |
1036 | 0 | ctx->tot_len = tot_len; |
1037 | |
|
1038 | 0 | len_b = tot_len << 3; |
1039 | 0 | pm_len = block_nb << 7; |
1040 | |
|
1041 | 0 | memset(ctx->block + ctx->len, 0, pm_len - ctx->len); |
1042 | 0 | ctx->block[ctx->len] = 0x80; |
1043 | 0 | UNPACK64(len_b, ctx->block + pm_len - 8); |
1044 | |
|
1045 | 0 | sha512_transf(ctx, ctx->block, block_nb); |
1046 | |
|
1047 | 0 | #ifndef UNROLL_LOOPS |
1048 | 0 | for (i = 0; i < 6; i++) { |
1049 | 0 | UNPACK64(ctx->h[i], &digest[i << 3]); |
1050 | 0 | } |
1051 | | #else |
1052 | | UNPACK64(ctx->h[0], &digest[0]); |
1053 | | UNPACK64(ctx->h[1], &digest[8]); |
1054 | | UNPACK64(ctx->h[2], &digest[16]); |
1055 | | UNPACK64(ctx->h[3], &digest[24]); |
1056 | | UNPACK64(ctx->h[4], &digest[32]); |
1057 | | UNPACK64(ctx->h[5], &digest[40]); |
1058 | | #endif /* !UNROLL_LOOPS */ |
1059 | 0 | } |
1060 | | |
1061 | | /* SHA-512 functions */ |
1062 | | |
1063 | 0 | void sha512(const uint8_t *message, uint64_t len, uint8_t *digest) { |
1064 | 0 | sha512_ctx ctx; |
1065 | |
|
1066 | 0 | sha512_init(&ctx); |
1067 | 0 | sha512_update(&ctx, message, len); |
1068 | 0 | sha512_final(&ctx, digest); |
1069 | 0 | } |
1070 | | |
1071 | 0 | void sha512_init(sha512_ctx *ctx) { |
1072 | 0 | #ifndef UNROLL_LOOPS |
1073 | 0 | int i; |
1074 | 0 | for (i = 0; i < 8; i++) { |
1075 | 0 | ctx->h[i] = sha512_h0[i]; |
1076 | 0 | } |
1077 | | #else |
1078 | | ctx->h[0] = sha512_h0[0]; |
1079 | | ctx->h[1] = sha512_h0[1]; |
1080 | | ctx->h[2] = sha512_h0[2]; |
1081 | | ctx->h[3] = sha512_h0[3]; |
1082 | | ctx->h[4] = sha512_h0[4]; |
1083 | | ctx->h[5] = sha512_h0[5]; |
1084 | | ctx->h[6] = sha512_h0[6]; |
1085 | | ctx->h[7] = sha512_h0[7]; |
1086 | | #endif /* !UNROLL_LOOPS */ |
1087 | |
|
1088 | 0 | ctx->len = 0; |
1089 | 0 | ctx->tot_len = 0; |
1090 | 0 | } |
1091 | | |
1092 | 0 | void sha512_update(sha512_ctx *ctx, const uint8_t *message, uint64_t len) { |
1093 | 0 | uint64_t block_nb; |
1094 | 0 | uint64_t new_len, rem_len, tmp_len; |
1095 | 0 | const uint8_t *shifted_message; |
1096 | |
|
1097 | 0 | tmp_len = SHA512_BLOCK_SIZE - ctx->len; |
1098 | 0 | rem_len = len < tmp_len ? len : tmp_len; |
1099 | |
|
1100 | 0 | memcpy(&ctx->block[ctx->len], message, rem_len); |
1101 | |
|
1102 | 0 | if (ctx->len + len < SHA512_BLOCK_SIZE) { |
1103 | 0 | ctx->len += len; |
1104 | 0 | return; |
1105 | 0 | } |
1106 | | |
1107 | 0 | new_len = len - rem_len; |
1108 | 0 | block_nb = new_len / SHA512_BLOCK_SIZE; |
1109 | |
|
1110 | 0 | shifted_message = message + rem_len; |
1111 | |
|
1112 | 0 | sha512_transf(ctx, ctx->block, 1); |
1113 | 0 | sha512_transf(ctx, shifted_message, block_nb); |
1114 | |
|
1115 | 0 | rem_len = new_len % SHA512_BLOCK_SIZE; |
1116 | |
|
1117 | 0 | memcpy(ctx->block, &shifted_message[block_nb << 7], rem_len); |
1118 | |
|
1119 | 0 | ctx->len = rem_len; |
1120 | 0 | ctx->tot_len += (block_nb + 1) << 7; |
1121 | 0 | } |
1122 | | |
1123 | 0 | void sha512_final(sha512_ctx *ctx, uint8_t *digest) { |
1124 | 0 | uint64_t block_nb; |
1125 | 0 | uint64_t pm_len; |
1126 | 0 | uint64_t len_b; |
1127 | 0 | uint64_t tot_len; |
1128 | |
|
1129 | 0 | #ifndef UNROLL_LOOPS |
1130 | 0 | int i; |
1131 | 0 | #endif |
1132 | |
|
1133 | 0 | block_nb = 1 + ((SHA512_BLOCK_SIZE - 17) < (ctx->len % SHA512_BLOCK_SIZE)); |
1134 | |
|
1135 | 0 | tot_len = ctx->tot_len + ctx->len; |
1136 | 0 | ctx->tot_len = tot_len; |
1137 | |
|
1138 | 0 | len_b = tot_len << 3; |
1139 | 0 | pm_len = block_nb << 7; |
1140 | |
|
1141 | 0 | memset(ctx->block + ctx->len, 0, pm_len - ctx->len); |
1142 | 0 | ctx->block[ctx->len] = 0x80; |
1143 | 0 | UNPACK64(len_b, ctx->block + pm_len - 8); |
1144 | |
|
1145 | 0 | sha512_transf(ctx, ctx->block, block_nb); |
1146 | |
|
1147 | 0 | #ifndef UNROLL_LOOPS |
1148 | 0 | for (i = 0; i < 8; i++) { |
1149 | 0 | UNPACK64(ctx->h[i], &digest[i << 3]); |
1150 | 0 | } |
1151 | | #else |
1152 | | UNPACK64(ctx->h[0], &digest[0]); |
1153 | | UNPACK64(ctx->h[1], &digest[8]); |
1154 | | UNPACK64(ctx->h[2], &digest[16]); |
1155 | | UNPACK64(ctx->h[3], &digest[24]); |
1156 | | UNPACK64(ctx->h[4], &digest[32]); |
1157 | | UNPACK64(ctx->h[5], &digest[40]); |
1158 | | UNPACK64(ctx->h[6], &digest[48]); |
1159 | | UNPACK64(ctx->h[7], &digest[56]); |
1160 | | #endif /* !UNROLL_LOOPS */ |
1161 | 0 | } |
1162 | | |
1163 | | #ifdef TEST_VECTORS |
1164 | | |
1165 | | /* FIPS 180-2 Validation tests */ |
1166 | | |
1167 | | #include <stdio.h> |
1168 | | #include <stdlib.h> |
1169 | | |
1170 | | void test(const char *vector, uint8_t *digest, uint32_t digest_size) { |
1171 | | char output[2 * SHA512_DIGEST_SIZE + 1]; |
1172 | | int i; |
1173 | | |
1174 | | output[2 * digest_size] = '\0'; |
1175 | | int32_t len = 0; |
1176 | | for (i = 0; i < (int)digest_size; i++) { |
1177 | | len += snprintf(output + 2 * i, sizeof(output) - len, "%02x", digest[i]); |
1178 | | } |
1179 | | |
1180 | | printf("H: %s\n", output); |
1181 | | if (strcmp(vector, output)) { |
1182 | | fprintf(stderr, "Test failed.\n"); |
1183 | | exit(EXIT_FAILURE); |
1184 | | } |
1185 | | } |
1186 | | |
1187 | | static void test_sha224_message4(uint8_t *digest) { |
1188 | | /* Message of 929271 bytes */ |
1189 | | |
1190 | | sha224_ctx ctx; |
1191 | | uint8_t message[1000]; |
1192 | | int i; |
1193 | | |
1194 | | memset(message, 'a', sizeof(message)); |
1195 | | |
1196 | | sha224_init(&ctx); |
1197 | | for (i = 0; i < 929; i++) { |
1198 | | sha224_update(&ctx, message, sizeof(message)); |
1199 | | } |
1200 | | sha224_update(&ctx, message, 271); |
1201 | | |
1202 | | sha224_final(&ctx, digest); |
1203 | | } |
1204 | | |
1205 | | static void test_sha256_message4(uint8_t *digest) { |
1206 | | /* Message of 929271 bytes */ |
1207 | | |
1208 | | sha256_ctx ctx; |
1209 | | uint8_t message[1000]; |
1210 | | int i; |
1211 | | |
1212 | | memset(message, 'a', sizeof(message)); |
1213 | | |
1214 | | sha256_init(&ctx); |
1215 | | for (i = 0; i < 929; i++) { |
1216 | | sha256_update(&ctx, message, sizeof(message)); |
1217 | | } |
1218 | | sha256_update(&ctx, message, 271); |
1219 | | |
1220 | | sha256_final(&ctx, digest); |
1221 | | } |
1222 | | |
1223 | | static void test_sha384_message4(uint8_t *digest) { |
1224 | | /* Message of 929271 bytes */ |
1225 | | |
1226 | | sha384_ctx ctx; |
1227 | | uint8_t message[1000]; |
1228 | | int i; |
1229 | | |
1230 | | memset(message, 'a', sizeof(message)); |
1231 | | |
1232 | | sha384_init(&ctx); |
1233 | | for (i = 0; i < 929; i++) { |
1234 | | sha384_update(&ctx, message, sizeof(message)); |
1235 | | } |
1236 | | sha384_update(&ctx, message, 271); |
1237 | | |
1238 | | sha384_final(&ctx, digest); |
1239 | | } |
1240 | | |
1241 | | static void test_sha512_message4(uint8_t *digest) { |
1242 | | /* Message of 929271 bytes */ |
1243 | | |
1244 | | sha512_ctx ctx; |
1245 | | uint8_t message[1000]; |
1246 | | int i; |
1247 | | |
1248 | | memset(message, 'a', sizeof(message)); |
1249 | | |
1250 | | sha512_init(&ctx); |
1251 | | for (i = 0; i < 929; i++) { |
1252 | | sha512_update(&ctx, message, sizeof(message)); |
1253 | | } |
1254 | | sha512_update(&ctx, message, 271); |
1255 | | |
1256 | | sha512_final(&ctx, digest); |
1257 | | } |
1258 | | |
1259 | | #ifdef TEST_VECTORS_LONG |
1260 | | |
1261 | | /* Validation tests with a message of 10 GB */ |
1262 | | |
1263 | | static void test_sha224_long_message(uint8_t *digest) { |
1264 | | sha224_ctx ctx; |
1265 | | uint8_t message[1000]; |
1266 | | int i; |
1267 | | |
1268 | | memset(message, 'a', sizeof(message)); |
1269 | | |
1270 | | sha224_init(&ctx); |
1271 | | for (i = 0; i < 10000000; i++) { |
1272 | | sha224_update(&ctx, message, sizeof(message)); |
1273 | | } |
1274 | | sha224_final(&ctx, digest); |
1275 | | } |
1276 | | |
1277 | | static void test_sha256_long_message(uint8_t *digest) { |
1278 | | sha256_ctx ctx; |
1279 | | uint8_t message[1000]; |
1280 | | int i; |
1281 | | |
1282 | | memset(message, 'a', sizeof(message)); |
1283 | | |
1284 | | sha256_init(&ctx); |
1285 | | for (i = 0; i < 10000000; i++) { |
1286 | | sha256_update(&ctx, message, sizeof(message)); |
1287 | | } |
1288 | | sha256_final(&ctx, digest); |
1289 | | } |
1290 | | |
1291 | | static void test_sha384_long_message(uint8_t *digest) { |
1292 | | sha384_ctx ctx; |
1293 | | uint8_t message[1000]; |
1294 | | int i; |
1295 | | |
1296 | | memset(message, 'a', sizeof(message)); |
1297 | | |
1298 | | sha384_init(&ctx); |
1299 | | for (i = 0; i < 10000000; i++) { |
1300 | | sha384_update(&ctx, message, sizeof(message)); |
1301 | | } |
1302 | | sha384_final(&ctx, digest); |
1303 | | } |
1304 | | |
1305 | | static void test_sha512_long_message(uint8_t *digest) { |
1306 | | sha512_ctx ctx; |
1307 | | uint8_t message[1000]; |
1308 | | int i; |
1309 | | |
1310 | | memset(message, 'a', sizeof(message)); |
1311 | | |
1312 | | sha512_init(&ctx); |
1313 | | for (i = 0; i < 10000000; i++) { |
1314 | | sha512_update(&ctx, message, sizeof(message)); |
1315 | | } |
1316 | | sha512_final(&ctx, digest); |
1317 | | } |
1318 | | |
1319 | | #endif /* TEST_VECTORS_LONG */ |
1320 | | |
1321 | | int main(void) { |
1322 | | static const char *vectors[4][5] = {/* SHA-224 */ |
1323 | | { |
1324 | | "23097d223405d8228642a477bda255b32aadbce4bda0b3f7e36c9da7", |
1325 | | "75388b16512776cc5dba5da1fd890150b0c6455cb4f58b1952522525", |
1326 | | "20794655980c91d8bbb4c1ea97618a4bf03f42581948b2ee4ee7ad67", |
1327 | | "c84cf4761583afa849ffd562c52a2e2a5104f1a4071dab6c53560d4f", |
1328 | | "b7bdc6c1f4f789f1456e68a005779a6c1f6199211008bee2801baf0d", |
1329 | | }, |
1330 | | /* SHA-256 */ |
1331 | | { |
1332 | | "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad", |
1333 | | "248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1", |
1334 | | "cdc76e5c9914fb9281a1c7e284d73e67f1809a48a497200e046d39ccc7112cd0", |
1335 | | "8c14f43ad81026351e9b60025b5420e6072ff617f5c72145b179599211514947", |
1336 | | "53748286337dbe36f5df22e7ef1af3ad71530398cf569adc7eb5fefa7af7003c", |
1337 | | }, |
1338 | | /* SHA-384 */ |
1339 | | { |
1340 | | "cb00753f45a35e8bb5a03d699ac65007272c32ab0eded1631a8b605a43ff5bed" |
1341 | | "8086072ba1e7cc2358baeca134c825a7", |
1342 | | "09330c33f71147e83d192fc782cd1b4753111b173b3b05d22fa08086e3b0f712" |
1343 | | "fcc7c71a557e2db966c3e9fa91746039", |
1344 | | "9d0e1809716474cb086e834e310a4a1ced149e9c00f248527972cec5704c2a5b" |
1345 | | "07b8b3dc38ecc4ebae97ddd87f3d8985", |
1346 | | "3de4a44dba8627278c376148b6d45be0a3a410337330ef3e1d9ca34c4593ecfc" |
1347 | | "8ce7a8415aefaca6b39d1112078cc3e0", |
1348 | | "073de8e641532032b2922c4af165baa88dfe5fdafb09575657406894b4b94996" |
1349 | | "8975eef50c1ef5be59ca0ecdaa996496", |
1350 | | }, |
1351 | | /* SHA-512 */ |
1352 | | {"ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a" |
1353 | | "2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f", |
1354 | | "8e959b75dae313da8cf4f72814fc143f8f7779c6eb9f7fa17299aeadb6889018" |
1355 | | "501d289e4900f7e4331b99dec4b5433ac7d329eeb6dd26545e96e55b874be909", |
1356 | | "e718483d0ce769644e2e42c7bc15b4638e1f98b13b2044285632a803afa973eb" |
1357 | | "de0ff244877ea60a4cb0432ce577c31beb009c5c2c49aa2e4eadb217ad8cc09b", |
1358 | | "a7e464bfd1f27201d7d0575c1a302cecef0004828e923e4255c8de6bae958a01" |
1359 | | "9294e3f851bf9a03013963cd1268687c549916438e465433957d4480bcaa8572", |
1360 | | "922637075a3ee5d87a7ce3ae7e04083d6daea7b541f264512290157ce3f81f9b" |
1361 | | "afcd3f9dc2d4fe0a6248a071709b96d0128d96c48220b2ab919b99187cb16fbf"}}; |
1362 | | |
1363 | | static const char message1[] = "abc"; |
1364 | | static const char message2a[] = |
1365 | | "abcdbcdecdefdefgefghfghighijhi" |
1366 | | "jkijkljklmklmnlmnomnopnopq"; |
1367 | | static const char message2b[] = |
1368 | | "abcdefghbcdefghicdefghijdefghijkefghij" |
1369 | | "klfghijklmghijklmnhijklmnoijklmnopjklm" |
1370 | | "nopqklmnopqrlmnopqrsmnopqrstnopqrstu"; |
1371 | | uint8_t *message3; |
1372 | | uint32_t message3_len = 1000000; |
1373 | | uint8_t digest[SHA512_DIGEST_SIZE]; |
1374 | | |
1375 | | message3 = malloc(message3_len); |
1376 | | if (message3 == NULL) { |
1377 | | fprintf(stderr, "Can't allocate memory\n"); |
1378 | | return -1; |
1379 | | } |
1380 | | memset(message3, 'a', message3_len); |
1381 | | |
1382 | | printf("SHA-2 FIPS 180-2 Validation tests\n\n"); |
1383 | | printf("SHA-224 Test vectors\n"); |
1384 | | |
1385 | | sha224((const uint8_t *)message1, strlen(message1), digest); |
1386 | | test(vectors[0][0], digest, SHA224_DIGEST_SIZE); |
1387 | | sha224((const uint8_t *)message2a, strlen(message2a), digest); |
1388 | | test(vectors[0][1], digest, SHA224_DIGEST_SIZE); |
1389 | | sha224(message3, message3_len, digest); |
1390 | | test(vectors[0][2], digest, SHA224_DIGEST_SIZE); |
1391 | | test_sha224_message4(digest); |
1392 | | test(vectors[0][3], digest, SHA224_DIGEST_SIZE); |
1393 | | #ifdef TEST_VECTORS_LONG |
1394 | | test_sha224_long_message(digest); |
1395 | | test(vectors[0][4], digest, SHA224_DIGEST_SIZE); |
1396 | | #endif |
1397 | | printf("\n"); |
1398 | | |
1399 | | printf("SHA-256 Test vectors\n"); |
1400 | | |
1401 | | sha256((const uint8_t *)message1, strlen(message1), digest); |
1402 | | test(vectors[1][0], digest, SHA256_DIGEST_SIZE); |
1403 | | sha256((const uint8_t *)message2a, strlen(message2a), digest); |
1404 | | test(vectors[1][1], digest, SHA256_DIGEST_SIZE); |
1405 | | sha256(message3, message3_len, digest); |
1406 | | test(vectors[1][2], digest, SHA256_DIGEST_SIZE); |
1407 | | test_sha256_message4(digest); |
1408 | | test(vectors[1][3], digest, SHA256_DIGEST_SIZE); |
1409 | | #ifdef TEST_VECTORS_LONG |
1410 | | test_sha256_long_message(digest); |
1411 | | test(vectors[1][4], digest, SHA256_DIGEST_SIZE); |
1412 | | #endif |
1413 | | printf("\n"); |
1414 | | |
1415 | | printf("SHA-384 Test vectors\n"); |
1416 | | |
1417 | | sha384((const uint8_t *)message1, strlen(message1), digest); |
1418 | | test(vectors[2][0], digest, SHA384_DIGEST_SIZE); |
1419 | | sha384((const uint8_t *)message2b, strlen(message2b), digest); |
1420 | | test(vectors[2][1], digest, SHA384_DIGEST_SIZE); |
1421 | | sha384(message3, message3_len, digest); |
1422 | | test(vectors[2][2], digest, SHA384_DIGEST_SIZE); |
1423 | | test_sha384_message4(digest); |
1424 | | test(vectors[2][3], digest, SHA384_DIGEST_SIZE); |
1425 | | #ifdef TEST_VECTORS_LONG |
1426 | | test_sha384_long_message(digest); |
1427 | | test(vectors[2][4], digest, SHA384_DIGEST_SIZE); |
1428 | | #endif |
1429 | | printf("\n"); |
1430 | | |
1431 | | printf("SHA-512 Test vectors\n"); |
1432 | | |
1433 | | sha512((const uint8_t *)message1, strlen(message1), digest); |
1434 | | test(vectors[3][0], digest, SHA512_DIGEST_SIZE); |
1435 | | sha512((const uint8_t *)message2b, strlen(message2b), digest); |
1436 | | test(vectors[3][1], digest, SHA512_DIGEST_SIZE); |
1437 | | sha512(message3, message3_len, digest); |
1438 | | test(vectors[3][2], digest, SHA512_DIGEST_SIZE); |
1439 | | test_sha512_message4(digest); |
1440 | | test(vectors[3][3], digest, SHA512_DIGEST_SIZE); |
1441 | | #ifdef TEST_VECTORS_LONG |
1442 | | test_sha512_long_message(digest); |
1443 | | test(vectors[3][4], digest, SHA512_DIGEST_SIZE); |
1444 | | #endif |
1445 | | printf("\n"); |
1446 | | |
1447 | | printf("All tests passed.\n"); |
1448 | | |
1449 | | return 0; |
1450 | | } |
1451 | | |
1452 | | #endif /* TEST_VECTORS */ |