Branch data Line data Source code
1 : : /*
2 : : *
3 : : * FIPS-180-2 compliant SHA-256 implementation
4 : : * Copyright (C) 2010 Paul Bakker.
5 : : * Originally written (2003-2006) by Christophe Devine.
6 : : *
7 : : * This code is free software; you can redistribute it and/or
8 : : * modify it, as a part of the International Chemical Identifier (InChI)
9 : : * software, under the terms of the MIT license
10 : : * https://opensource.org/license/mit
11 : : *
12 : : * Note that this licensing is only valid in combination with the InChI
13 : : * software package, for all other cases contact Paul Bakker.
14 : : */
15 : :
16 : : /*
17 : : * The SHA-256 standard was published by NIST in 2002.
18 : : *
19 : : * http://csrc.nist.gov/publications/fips/fips180-2/fips180-2.pdf
20 : : */
21 : :
22 : : #ifndef _CRT_SECURE_NO_DEPRECATE
23 : : #define _CRT_SECURE_NO_DEPRECATE 1
24 : : #endif
25 : :
26 : : #include <string.h>
27 : : #include <stdio.h>
28 : :
29 : : #include "sha2.h"
30 : :
31 : : #include "mode.h"
32 : :
33 : : #include "bcf_s.h"
34 : :
35 : : /*
36 : : * 32-bit integer manipulation macros (big endian)
37 : : */
38 : : #ifndef GET_UINT32_BE
39 : : #define GET_UINT32_BE(n, b, i) \
40 : : { \
41 : : (n) = ((unsigned long)(b)[(i)] << 24) | ((unsigned long)(b)[(i) + 1] << 16) | ((unsigned long)(b)[(i) + 2] << 8) | ((unsigned long)(b)[(i) + 3]); \
42 : : }
43 : : #endif
44 : : #ifndef PUT_UINT32_BE
45 : : #define PUT_UINT32_BE(n, b, i) \
46 : : { \
47 : : (b)[(i)] = (unsigned char)((n) >> 24); \
48 : : (b)[(i) + 1] = (unsigned char)((n) >> 16); \
49 : : (b)[(i) + 2] = (unsigned char)((n) >> 8); \
50 : : (b)[(i) + 3] = (unsigned char)((n)); \
51 : : }
52 : : #endif
53 : :
54 : : /*
55 : : * SHA-2 context setup
56 : : */
57 : 0 : void sha2_starts(sha2_context *ctx)
58 : : {
59 : 0 : ctx->total[0] = 0;
60 : 0 : ctx->total[1] = 0;
61 : :
62 : 0 : ctx->state[0] = 0x6A09E667;
63 : 0 : ctx->state[1] = 0xBB67AE85;
64 : 0 : ctx->state[2] = 0x3C6EF372;
65 : 0 : ctx->state[3] = 0xA54FF53A;
66 : 0 : ctx->state[4] = 0x510E527F;
67 : 0 : ctx->state[5] = 0x9B05688C;
68 : 0 : ctx->state[6] = 0x1F83D9AB;
69 : 0 : ctx->state[7] = 0x5BE0CD19;
70 : 0 : }
71 : :
72 : 0 : static void sha2_process(sha2_context *ctx, unsigned char data[64])
73 : : {
74 : : unsigned long temp1, temp2, W[64];
75 : : unsigned long A, B, C, D, E, F, G, H;
76 : :
77 : 0 : GET_UINT32_BE(W[0], data, 0);
78 : 0 : GET_UINT32_BE(W[1], data, 4);
79 : 0 : GET_UINT32_BE(W[2], data, 8);
80 : 0 : GET_UINT32_BE(W[3], data, 12);
81 : 0 : GET_UINT32_BE(W[4], data, 16);
82 : 0 : GET_UINT32_BE(W[5], data, 20);
83 : 0 : GET_UINT32_BE(W[6], data, 24);
84 : 0 : GET_UINT32_BE(W[7], data, 28);
85 : 0 : GET_UINT32_BE(W[8], data, 32);
86 : 0 : GET_UINT32_BE(W[9], data, 36);
87 : 0 : GET_UINT32_BE(W[10], data, 40);
88 : 0 : GET_UINT32_BE(W[11], data, 44);
89 : 0 : GET_UINT32_BE(W[12], data, 48);
90 : 0 : GET_UINT32_BE(W[13], data, 52);
91 : 0 : GET_UINT32_BE(W[14], data, 56);
92 : 0 : GET_UINT32_BE(W[15], data, 60);
93 : :
94 : : #define SHR(x, n) ((x & 0xFFFFFFFF) >> n)
95 : : #define ROTR(x, n) (SHR(x, n) | (x << (32 - n)))
96 : :
97 : : #define S0(x) (ROTR(x, 7) ^ ROTR(x, 18) ^ SHR(x, 3))
98 : : #define S1(x) (ROTR(x, 17) ^ ROTR(x, 19) ^ SHR(x, 10))
99 : :
100 : : #define S2(x) (ROTR(x, 2) ^ ROTR(x, 13) ^ ROTR(x, 22))
101 : : #define S3(x) (ROTR(x, 6) ^ ROTR(x, 11) ^ ROTR(x, 25))
102 : :
103 : : #define F0(x, y, z) ((x & y) | (z & (x | y)))
104 : : #define F1(x, y, z) (z ^ (x & (y ^ z)))
105 : :
106 : : #define R(t) \
107 : : ( \
108 : : W[t] = S1(W[t - 2]) + W[t - 7] + \
109 : : S0(W[t - 15]) + W[t - 16])
110 : :
111 : : #define P(a, b, c, d, e, f, g, h, x, K) \
112 : : { \
113 : : temp1 = h + S3(e) + F1(e, f, g) + K + x; \
114 : : temp2 = S2(a) + F0(a, b, c); \
115 : : d += temp1; \
116 : : h = temp1 + temp2; \
117 : : }
118 : :
119 : 0 : A = ctx->state[0];
120 : 0 : B = ctx->state[1];
121 : 0 : C = ctx->state[2];
122 : 0 : D = ctx->state[3];
123 : 0 : E = ctx->state[4];
124 : 0 : F = ctx->state[5];
125 : 0 : G = ctx->state[6];
126 : 0 : H = ctx->state[7];
127 : :
128 : 0 : P(A, B, C, D, E, F, G, H, W[0], 0x428A2F98);
129 : 0 : P(H, A, B, C, D, E, F, G, W[1], 0x71374491);
130 : 0 : P(G, H, A, B, C, D, E, F, W[2], 0xB5C0FBCF);
131 : 0 : P(F, G, H, A, B, C, D, E, W[3], 0xE9B5DBA5);
132 : 0 : P(E, F, G, H, A, B, C, D, W[4], 0x3956C25B);
133 : 0 : P(D, E, F, G, H, A, B, C, W[5], 0x59F111F1);
134 : 0 : P(C, D, E, F, G, H, A, B, W[6], 0x923F82A4);
135 : 0 : P(B, C, D, E, F, G, H, A, W[7], 0xAB1C5ED5);
136 : 0 : P(A, B, C, D, E, F, G, H, W[8], 0xD807AA98);
137 : 0 : P(H, A, B, C, D, E, F, G, W[9], 0x12835B01);
138 : 0 : P(G, H, A, B, C, D, E, F, W[10], 0x243185BE);
139 : 0 : P(F, G, H, A, B, C, D, E, W[11], 0x550C7DC3);
140 : 0 : P(E, F, G, H, A, B, C, D, W[12], 0x72BE5D74);
141 : 0 : P(D, E, F, G, H, A, B, C, W[13], 0x80DEB1FE);
142 : 0 : P(C, D, E, F, G, H, A, B, W[14], 0x9BDC06A7);
143 : 0 : P(B, C, D, E, F, G, H, A, W[15], 0xC19BF174);
144 : 0 : P(A, B, C, D, E, F, G, H, R(16), 0xE49B69C1);
145 : 0 : P(H, A, B, C, D, E, F, G, R(17), 0xEFBE4786);
146 : 0 : P(G, H, A, B, C, D, E, F, R(18), 0x0FC19DC6);
147 : 0 : P(F, G, H, A, B, C, D, E, R(19), 0x240CA1CC);
148 : 0 : P(E, F, G, H, A, B, C, D, R(20), 0x2DE92C6F);
149 : 0 : P(D, E, F, G, H, A, B, C, R(21), 0x4A7484AA);
150 : 0 : P(C, D, E, F, G, H, A, B, R(22), 0x5CB0A9DC);
151 : 0 : P(B, C, D, E, F, G, H, A, R(23), 0x76F988DA);
152 : 0 : P(A, B, C, D, E, F, G, H, R(24), 0x983E5152);
153 : 0 : P(H, A, B, C, D, E, F, G, R(25), 0xA831C66D);
154 : 0 : P(G, H, A, B, C, D, E, F, R(26), 0xB00327C8);
155 : 0 : P(F, G, H, A, B, C, D, E, R(27), 0xBF597FC7);
156 : 0 : P(E, F, G, H, A, B, C, D, R(28), 0xC6E00BF3);
157 : 0 : P(D, E, F, G, H, A, B, C, R(29), 0xD5A79147);
158 : 0 : P(C, D, E, F, G, H, A, B, R(30), 0x06CA6351);
159 : 0 : P(B, C, D, E, F, G, H, A, R(31), 0x14292967);
160 : 0 : P(A, B, C, D, E, F, G, H, R(32), 0x27B70A85);
161 : 0 : P(H, A, B, C, D, E, F, G, R(33), 0x2E1B2138);
162 : 0 : P(G, H, A, B, C, D, E, F, R(34), 0x4D2C6DFC);
163 : 0 : P(F, G, H, A, B, C, D, E, R(35), 0x53380D13);
164 : 0 : P(E, F, G, H, A, B, C, D, R(36), 0x650A7354);
165 : 0 : P(D, E, F, G, H, A, B, C, R(37), 0x766A0ABB);
166 : 0 : P(C, D, E, F, G, H, A, B, R(38), 0x81C2C92E);
167 : 0 : P(B, C, D, E, F, G, H, A, R(39), 0x92722C85);
168 : 0 : P(A, B, C, D, E, F, G, H, R(40), 0xA2BFE8A1);
169 : 0 : P(H, A, B, C, D, E, F, G, R(41), 0xA81A664B);
170 : 0 : P(G, H, A, B, C, D, E, F, R(42), 0xC24B8B70);
171 : 0 : P(F, G, H, A, B, C, D, E, R(43), 0xC76C51A3);
172 : 0 : P(E, F, G, H, A, B, C, D, R(44), 0xD192E819);
173 : 0 : P(D, E, F, G, H, A, B, C, R(45), 0xD6990624);
174 : 0 : P(C, D, E, F, G, H, A, B, R(46), 0xF40E3585);
175 : 0 : P(B, C, D, E, F, G, H, A, R(47), 0x106AA070);
176 : 0 : P(A, B, C, D, E, F, G, H, R(48), 0x19A4C116);
177 : 0 : P(H, A, B, C, D, E, F, G, R(49), 0x1E376C08);
178 : 0 : P(G, H, A, B, C, D, E, F, R(50), 0x2748774C);
179 : 0 : P(F, G, H, A, B, C, D, E, R(51), 0x34B0BCB5);
180 : 0 : P(E, F, G, H, A, B, C, D, R(52), 0x391C0CB3);
181 : 0 : P(D, E, F, G, H, A, B, C, R(53), 0x4ED8AA4A);
182 : 0 : P(C, D, E, F, G, H, A, B, R(54), 0x5B9CCA4F);
183 : 0 : P(B, C, D, E, F, G, H, A, R(55), 0x682E6FF3);
184 : 0 : P(A, B, C, D, E, F, G, H, R(56), 0x748F82EE);
185 : 0 : P(H, A, B, C, D, E, F, G, R(57), 0x78A5636F);
186 : 0 : P(G, H, A, B, C, D, E, F, R(58), 0x84C87814);
187 : 0 : P(F, G, H, A, B, C, D, E, R(59), 0x8CC70208);
188 : 0 : P(E, F, G, H, A, B, C, D, R(60), 0x90BEFFFA);
189 : 0 : P(D, E, F, G, H, A, B, C, R(61), 0xA4506CEB);
190 : 0 : P(C, D, E, F, G, H, A, B, R(62), 0xBEF9A3F7);
191 : 0 : P(B, C, D, E, F, G, H, A, R(63), 0xC67178F2);
192 : :
193 : 0 : ctx->state[0] += A;
194 : 0 : ctx->state[1] += B;
195 : 0 : ctx->state[2] += C;
196 : 0 : ctx->state[3] += D;
197 : 0 : ctx->state[4] += E;
198 : 0 : ctx->state[5] += F;
199 : 0 : ctx->state[6] += G;
200 : 0 : ctx->state[7] += H;
201 : 0 : }
202 : :
203 : : /*
204 : : * SHA-2 process buffer
205 : : */
206 : 0 : void sha2_update(sha2_context *ctx, unsigned char *input, int ilen)
207 : : {
208 : : int fill;
209 : : unsigned long left;
210 : :
211 [ # # ]: 0 : if (ilen <= 0)
212 : 0 : return;
213 : :
214 : 0 : left = ctx->total[0] & 0x3F;
215 : 0 : fill = 64 - left;
216 : :
217 : 0 : ctx->total[0] += ilen;
218 : 0 : ctx->total[0] &= 0xFFFFFFFF;
219 : :
220 [ # # ]: 0 : if (ctx->total[0] < (unsigned long)ilen)
221 : 0 : ctx->total[1]++;
222 : :
223 [ # # # # ]: 0 : if (left && ilen >= fill)
224 : : {
225 : 0 : memcpy((void *)(ctx->buffer + left),
226 : : (void *)input, fill);
227 : 0 : sha2_process(ctx, ctx->buffer);
228 : 0 : input += fill;
229 : 0 : ilen -= fill;
230 : 0 : left = 0;
231 : : }
232 : :
233 [ # # ]: 0 : while (ilen >= 64)
234 : : {
235 : 0 : sha2_process(ctx, input); /* djb-rwth: ignoring LLVM warning as ilen >= 64 just in test case */
236 : 0 : input += 64;
237 : 0 : ilen -= 64;
238 : : }
239 : :
240 [ # # ]: 0 : if (ilen > 0)
241 : : {
242 : 0 : memcpy((void *)(ctx->buffer + left),
243 : : (void *)input, ilen);
244 : : }
245 : : }
246 : :
247 : : static const unsigned char sha2_padding[64] =
248 : : {
249 : : 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
250 : : 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
251 : : 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
252 : : 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
253 : :
254 : : /*
255 : : * SHA-2 final digest
256 : : */
257 : 0 : void sha2_finish(sha2_context *ctx, unsigned char output[32])
258 : : {
259 : : unsigned long last, padn;
260 : : unsigned long high, low;
261 : : unsigned char msglen[8];
262 : :
263 : 0 : high = (ctx->total[0] >> 29) | (ctx->total[1] << 3);
264 : 0 : low = (ctx->total[0] << 3);
265 : :
266 : 0 : PUT_UINT32_BE(high, msglen, 0);
267 : 0 : PUT_UINT32_BE(low, msglen, 4);
268 : :
269 : 0 : last = ctx->total[0] & 0x3F;
270 [ # # ]: 0 : padn = (last < 56) ? (56 - last) : (120 - last);
271 : :
272 : 0 : sha2_update(ctx, (unsigned char *)sha2_padding, padn);
273 : 0 : sha2_update(ctx, msglen, 8);
274 : :
275 : 0 : PUT_UINT32_BE(ctx->state[0], output, 0);
276 : 0 : PUT_UINT32_BE(ctx->state[1], output, 4);
277 : 0 : PUT_UINT32_BE(ctx->state[2], output, 8);
278 : 0 : PUT_UINT32_BE(ctx->state[3], output, 12);
279 : 0 : PUT_UINT32_BE(ctx->state[4], output, 16);
280 : 0 : PUT_UINT32_BE(ctx->state[5], output, 20);
281 : 0 : PUT_UINT32_BE(ctx->state[6], output, 24);
282 : 0 : PUT_UINT32_BE(ctx->state[7], output, 28);
283 : 0 : }
284 : :
285 : : /*
286 : : * Output = SHA-2( file contents )
287 : : */
288 : 0 : int sha2_file(char *path, unsigned char output[32])
289 : : {
290 : : FILE *f;
291 : : size_t n;
292 : : sha2_context ctx;
293 : : unsigned char buf[1024];
294 : :
295 [ # # ]: 0 : if ((f = fopen(path, "rb")) == NULL)
296 : 0 : return (1);
297 : :
298 : 0 : sha2_starts(&ctx);
299 : :
300 [ # # # # ]: 0 : while (!feof(f) && (n = fread(buf, 1, sizeof(buf), f)) > 0) /* djb-rwth: addressing LLVM warning */
301 : 0 : sha2_update(&ctx, buf, (int)n);
302 : :
303 : 0 : sha2_finish(&ctx, output);
304 : :
305 : 0 : fclose(f);
306 : 0 : return (0);
307 : : }
308 : :
309 : : /*
310 : : * Output = SHA-2( input buffer )
311 : : */
312 : 0 : void sha2_csum(unsigned char *input, int ilen,
313 : : unsigned char output[32])
314 : : {
315 : : sha2_context ctx;
316 : :
317 : 0 : sha2_starts(&ctx);
318 : 0 : sha2_update(&ctx, input, ilen);
319 : 0 : sha2_finish(&ctx, output);
320 : 0 : }
321 : :
322 : : /*
323 : : * Output = HMAC-SHA-2( input buffer, hmac key )
324 : : */
325 : 0 : void sha2_hmac(unsigned char *key, int keylen,
326 : : unsigned char *input, int ilen,
327 : : unsigned char output[32])
328 : : {
329 : : int i;
330 : : sha2_context ctx;
331 : : unsigned char k_ipad[64];
332 : : unsigned char k_opad[64];
333 : : unsigned char tmpbuf[32];
334 : :
335 : 0 : memset(k_ipad, 0x36, 64); /* djb-rwth: memset_s C11/Annex K variant? */
336 : 0 : memset(k_opad, 0x5C, 64); /* djb-rwth: memset_s C11/Annex K variant? */
337 : :
338 [ # # ]: 0 : for (i = 0; i < keylen; i++)
339 : : {
340 [ # # ]: 0 : if (i >= 64)
341 : 0 : break;
342 : :
343 : 0 : k_ipad[i] ^= key[i];
344 : 0 : k_opad[i] ^= key[i];
345 : : }
346 : :
347 : 0 : sha2_starts(&ctx);
348 : 0 : sha2_update(&ctx, k_ipad, 64);
349 : 0 : sha2_update(&ctx, input, ilen);
350 : 0 : sha2_finish(&ctx, tmpbuf);
351 : :
352 : 0 : sha2_starts(&ctx);
353 : 0 : sha2_update(&ctx, k_opad, 64);
354 : 0 : sha2_update(&ctx, tmpbuf, 32);
355 : 0 : sha2_finish(&ctx, output);
356 : :
357 : 0 : memset(k_ipad, 0, 64); /* djb-rwth: memset_s C11/Annex K variant? */
358 : 0 : memset(k_opad, 0, 64); /* djb-rwth: memset_s C11/Annex K variant? */
359 : 0 : memset(tmpbuf, 0, 32); /* djb-rwth: memset_s C11/Annex K variant? */
360 : 0 : memset(&ctx, 0, sizeof(sha2_context)); /* djb-rwth: memset_s C11/Annex K variant? */
361 : 0 : }
362 : :
363 : : #ifdef SELF_TEST
364 : : /*
365 : : * FIPS-180-2 test vectors
366 : : */
367 : : static const char sha2_test_str[3][57] =
368 : : {
369 : : {"abc"},
370 : : {"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"},
371 : : {""}};
372 : :
373 : : static const unsigned char sha2_test_sum[3][32] =
374 : : {
375 : : {0xBA, 0x78, 0x16, 0xBF, 0x8F, 0x01, 0xCF, 0xEA,
376 : : 0x41, 0x41, 0x40, 0xDE, 0x5D, 0xAE, 0x22, 0x23,
377 : : 0xB0, 0x03, 0x61, 0xA3, 0x96, 0x17, 0x7A, 0x9C,
378 : : 0xB4, 0x10, 0xFF, 0x61, 0xF2, 0x00, 0x15, 0xAD},
379 : : {0x24, 0x8D, 0x6A, 0x61, 0xD2, 0x06, 0x38, 0xB8,
380 : : 0xE5, 0xC0, 0x26, 0x93, 0x0C, 0x3E, 0x60, 0x39,
381 : : 0xA3, 0x3C, 0xE4, 0x59, 0x64, 0xFF, 0x21, 0x67,
382 : : 0xF6, 0xEC, 0xED, 0xD4, 0x19, 0xDB, 0x06, 0xC1},
383 : : {0xCD, 0xC7, 0x6E, 0x5C, 0x99, 0x14, 0xFB, 0x92,
384 : : 0x81, 0xA1, 0xC7, 0xE2, 0x84, 0xD7, 0x3E, 0x67,
385 : : 0xF1, 0x80, 0x9A, 0x48, 0xA4, 0x97, 0x20, 0x0E,
386 : : 0x04, 0x6D, 0x39, 0xCC, 0xC7, 0x11, 0x2C, 0xD0}};
387 : :
388 : : /*
389 : : * Checkup routine
390 : : */
391 : : int sha2_self_test(void)
392 : : {
393 : : int i, j;
394 : : unsigned char buf[1000];
395 : : unsigned char sha2sum[32];
396 : : sha2_context ctx;
397 : :
398 : : for (i = 0; i < 3; i++)
399 : : {
400 : : printf(" SHA-256 test #%d: ", i + 1);
401 : :
402 : : sha2_starts(&ctx);
403 : :
404 : : if (i < 2)
405 : : sha2_update(&ctx, (unsigned char *)sha2_test_str[i],
406 : : strlen(sha2_test_str[i]));
407 : : else
408 : : {
409 : : memset(buf, 'a', 1000);
410 : : for (j = 0; j < 1000; j++)
411 : : sha2_update(&ctx, buf, 1000);
412 : : }
413 : :
414 : : sha2_finish(&ctx, sha2sum);
415 : :
416 : : if (memcmp(sha2sum, sha2_test_sum[i], 20) != 0)
417 : : {
418 : : printf("failed\n");
419 : : return (1);
420 : : }
421 : :
422 : : printf("passed\n");
423 : : }
424 : :
425 : : printf("\n");
426 : : return (0);
427 : : }
428 : : #else
429 : 0 : int sha2_self_test(void)
430 : : {
431 : 0 : return (0);
432 : : }
433 : :
434 : : #endif
|