Debian Codegraph @

terps/tads/tads3/sha2.cpp

raw
1 /* From libtomcrypt 1.18.2 */
2
3 /* LibTomCrypt, modular cryptographic library -- Tom St Denis */
4 /* SPDX-License-Identifier: Unlicense */
5 #if 0
6 #include "tomcrypt_private.h"
7 #endif
8
9 /**
10 @file sha256.c
11 LTC_SHA256 by Tom St Denis
12 */
13 #include <assert.h>
14 #include <stddef.h>
15 #include <stdint.h>
16 #include <string.h>
17
18 #include "sha2.h"
19
20 #define LTC_SHA256
21
22 #define MIN(x, y) ( ((x)<(y))?(x):(y) )
23 #define XMEMCPY memcpy
24 #define XSTRLEN strlen
25
26 #define STORE32H(x, y) \
27 do { (y)[0] = (unsigned char)(((x)>>24)&255); (y)[1] = (unsigned char)(((x)>>16)&255); \
28 (y)[2] = (unsigned char)(((x)>>8)&255); (y)[3] = (unsigned char)((x)&255); } while(0)
29
30 #define STORE64H(x, y) \
31 do { (y)[0] = (unsigned char)(((x)>>56)&255); (y)[1] = (unsigned char)(((x)>>48)&255); \
32 (y)[2] = (unsigned char)(((x)>>40)&255); (y)[3] = (unsigned char)(((x)>>32)&255); \
33 (y)[4] = (unsigned char)(((x)>>24)&255); (y)[5] = (unsigned char)(((x)>>16)&255); \
34 (y)[6] = (unsigned char)(((x)>>8)&255); (y)[7] = (unsigned char)((x)&255); } while(0)
35
36 typedef uint32_t ulong32;
37
38 #define LOAD32H(x, y) \
39 do { x = ((ulong32)((y)[0] & 255)<<24) | \
40 ((ulong32)((y)[1] & 255)<<16) | \
41 ((ulong32)((y)[2] & 255)<<8) | \
42 ((ulong32)((y)[3] & 255)); } while(0)
43
44 #define RORc(x, y) ( ((((ulong32)(x)&0xFFFFFFFFUL)>>(ulong32)((y)&31)) | ((ulong32)(x)<<(ulong32)((32-((y)&31))&31))) & 0xFFFFFFFFUL)
45
46 #define LTC_ARGCHK(x) assert(x)
47
48 #define HASH_PROCESS(func_name, compress_name, state_var, block_size) \
49 int func_name (hash_state * md, const unsigned char *in, unsigned long inlen) \
50 { \
51 unsigned long n; \
52 int err; \
53 LTC_ARGCHK(md != NULL); \
54 LTC_ARGCHK(in != NULL); \
55 if (md-> state_var .curlen > sizeof(md-> state_var .buf)) { \
56 return CRYPT_INVALID_ARG; \
57 } \
58 if ((md-> state_var .length + inlen) < md-> state_var .length) { \
59 return CRYPT_HASH_OVERFLOW; \
60 } \
61 while (inlen > 0) { \
62 if (md-> state_var .curlen == 0 && inlen >= block_size) { \
63 if ((err = compress_name (md, (unsigned char *)in)) != CRYPT_OK) { \
64 return err; \
65 } \
66 md-> state_var .length += block_size * 8; \
67 in += block_size; \
68 inlen -= block_size; \
69 } else { \
70 n = MIN(inlen, (block_size - md-> state_var .curlen)); \
71 XMEMCPY(md-> state_var .buf + md-> state_var.curlen, in, (size_t)n); \
72 md-> state_var .curlen += n; \
73 in += n; \
74 inlen -= n; \
75 if (md-> state_var .curlen == block_size) { \
76 if ((err = compress_name (md, md-> state_var .buf)) != CRYPT_OK) { \
77 return err; \
78 } \
79 md-> state_var .length += 8*block_size; \
80 md-> state_var .curlen = 0; \
81 } \
82 } \
83 } \
84 return CRYPT_OK; \
85 }
86
87 #ifdef LTC_SHA256
88
89 #ifdef LTC_SMALL_CODE
90 /* the K array */
91 static const ulong32 K[64] = {
92 0x428a2f98UL, 0x71374491UL, 0xb5c0fbcfUL, 0xe9b5dba5UL, 0x3956c25bUL,
93 0x59f111f1UL, 0x923f82a4UL, 0xab1c5ed5UL, 0xd807aa98UL, 0x12835b01UL,
94 0x243185beUL, 0x550c7dc3UL, 0x72be5d74UL, 0x80deb1feUL, 0x9bdc06a7UL,
95 0xc19bf174UL, 0xe49b69c1UL, 0xefbe4786UL, 0x0fc19dc6UL, 0x240ca1ccUL,
96 0x2de92c6fUL, 0x4a7484aaUL, 0x5cb0a9dcUL, 0x76f988daUL, 0x983e5152UL,
97 0xa831c66dUL, 0xb00327c8UL, 0xbf597fc7UL, 0xc6e00bf3UL, 0xd5a79147UL,
98 0x06ca6351UL, 0x14292967UL, 0x27b70a85UL, 0x2e1b2138UL, 0x4d2c6dfcUL,
99 0x53380d13UL, 0x650a7354UL, 0x766a0abbUL, 0x81c2c92eUL, 0x92722c85UL,
100 0xa2bfe8a1UL, 0xa81a664bUL, 0xc24b8b70UL, 0xc76c51a3UL, 0xd192e819UL,
101 0xd6990624UL, 0xf40e3585UL, 0x106aa070UL, 0x19a4c116UL, 0x1e376c08UL,
102 0x2748774cUL, 0x34b0bcb5UL, 0x391c0cb3UL, 0x4ed8aa4aUL, 0x5b9cca4fUL,
103 0x682e6ff3UL, 0x748f82eeUL, 0x78a5636fUL, 0x84c87814UL, 0x8cc70208UL,
104 0x90befffaUL, 0xa4506cebUL, 0xbef9a3f7UL, 0xc67178f2UL
105 };
106 #endif
107
108 /* Various logical functions */
109 #define Ch(x,y,z) (z ^ (x & (y ^ z)))
110 #define Maj(x,y,z) (((x | y) & z) | (x & y))
111 #define S(x, n) RORc((x),(n))
112 #define R(x, n) (((x)&0xFFFFFFFFUL)>>(n))
113 #define Sigma0(x) (S(x, 2) ^ S(x, 13) ^ S(x, 22))
114 #define Sigma1(x) (S(x, 6) ^ S(x, 11) ^ S(x, 25))
115 #define Gamma0(x) (S(x, 7) ^ S(x, 18) ^ R(x, 3))
116 #define Gamma1(x) (S(x, 17) ^ S(x, 19) ^ R(x, 10))
117
118 /* compress 512-bits */
119 #ifdef LTC_CLEAN_STACK
120 static int ss_sha256_compress(hash_state * md, const unsigned char *buf)
121 #else
122 static int s_sha256_compress(hash_state * md, const unsigned char *buf)
123 #endif
124 {
125 ulong32 S[8], W[64], t0, t1;
126 #ifdef LTC_SMALL_CODE
127 ulong32 t;
128 #endif
129 int i;
130
131 /* copy state into S */
132 for (i = 0; i < 8; i++) {
133 S[i] = md->sha256.state[i];
134 }
135
136 /* copy the state into 512-bits into W[0..15] */
137 for (i = 0; i < 16; i++) {
138 LOAD32H(W[i], buf + (4*i));
139 }
140
141 /* fill W[16..63] */
142 for (i = 16; i < 64; i++) {
143 W[i] = Gamma1(W[i - 2]) + W[i - 7] + Gamma0(W[i - 15]) + W[i - 16];
144 }
145
146 /* Compress */
147 #ifdef LTC_SMALL_CODE
148 #define RND(a,b,c,d,e,f,g,h,i) \
149 t0 = h + Sigma1(e) + Ch(e, f, g) + K[i] + W[i]; \
150 t1 = Sigma0(a) + Maj(a, b, c); \
151 d += t0; \
152 h = t0 + t1;
153
154 for (i = 0; i < 64; ++i) {
155 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],i);
156 t = S[7]; S[7] = S[6]; S[6] = S[5]; S[5] = S[4];
157 S[4] = S[3]; S[3] = S[2]; S[2] = S[1]; S[1] = S[0]; S[0] = t;
158 }
159 #else
160 #define RND(a,b,c,d,e,f,g,h,i,ki) \
161 t0 = h + Sigma1(e) + Ch(e, f, g) + ki + W[i]; \
162 t1 = Sigma0(a) + Maj(a, b, c); \
163 d += t0; \
164 h = t0 + t1;
165
166 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],0,0x428a2f98);
167 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],1,0x71374491);
168 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],2,0xb5c0fbcf);
169 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],3,0xe9b5dba5);
170 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],4,0x3956c25b);
171 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],5,0x59f111f1);
172 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],6,0x923f82a4);
173 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],7,0xab1c5ed5);
174 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],8,0xd807aa98);
175 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],9,0x12835b01);
176 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],10,0x243185be);
177 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],11,0x550c7dc3);
178 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],12,0x72be5d74);
179 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],13,0x80deb1fe);
180 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],14,0x9bdc06a7);
181 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],15,0xc19bf174);
182 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],16,0xe49b69c1);
183 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],17,0xefbe4786);
184 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],18,0x0fc19dc6);
185 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],19,0x240ca1cc);
186 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],20,0x2de92c6f);
187 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],21,0x4a7484aa);
188 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],22,0x5cb0a9dc);
189 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],23,0x76f988da);
190 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],24,0x983e5152);
191 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],25,0xa831c66d);
192 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],26,0xb00327c8);
193 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],27,0xbf597fc7);
194 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],28,0xc6e00bf3);
195 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],29,0xd5a79147);
196 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],30,0x06ca6351);
197 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],31,0x14292967);
198 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],32,0x27b70a85);
199 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],33,0x2e1b2138);
200 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],34,0x4d2c6dfc);
201 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],35,0x53380d13);
202 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],36,0x650a7354);
203 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],37,0x766a0abb);
204 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],38,0x81c2c92e);
205 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],39,0x92722c85);
206 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],40,0xa2bfe8a1);
207 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],41,0xa81a664b);
208 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],42,0xc24b8b70);
209 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],43,0xc76c51a3);
210 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],44,0xd192e819);
211 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],45,0xd6990624);
212 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],46,0xf40e3585);
213 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],47,0x106aa070);
214 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],48,0x19a4c116);
215 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],49,0x1e376c08);
216 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],50,0x2748774c);
217 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],51,0x34b0bcb5);
218 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],52,0x391c0cb3);
219 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],53,0x4ed8aa4a);
220 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],54,0x5b9cca4f);
221 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],55,0x682e6ff3);
222 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],56,0x748f82ee);
223 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],57,0x78a5636f);
224 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],58,0x84c87814);
225 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],59,0x8cc70208);
226 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],60,0x90befffa);
227 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],61,0xa4506ceb);
228 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],62,0xbef9a3f7);
229 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],63,0xc67178f2);
230 #endif
231 #undef RND
232
233 /* feedback */
234 for (i = 0; i < 8; i++) {
235 md->sha256.state[i] = md->sha256.state[i] + S[i];
236 }
237 return CRYPT_OK;
238 }
239
240 #ifdef LTC_CLEAN_STACK
241 static int s_sha256_compress(hash_state * md, const unsigned char *buf)
242 {
243 int err;
244 err = ss_sha256_compress(md, buf);
245 burn_stack(sizeof(ulong32) * 74);
246 return err;
247 }
248 #endif
249
250 /**
251 Initialize the hash state
252 @param md The hash state you wish to initialize
253 @return CRYPT_OK if successful
254 */
255 int sha256_init(hash_state * md)
256 {
257 LTC_ARGCHK(md != NULL);
258
259 md->sha256.curlen = 0;
260 md->sha256.length = 0;
261 md->sha256.state[0] = 0x6A09E667UL;
262 md->sha256.state[1] = 0xBB67AE85UL;
263 md->sha256.state[2] = 0x3C6EF372UL;
264 md->sha256.state[3] = 0xA54FF53AUL;
265 md->sha256.state[4] = 0x510E527FUL;
266 md->sha256.state[5] = 0x9B05688CUL;
267 md->sha256.state[6] = 0x1F83D9ABUL;
268 md->sha256.state[7] = 0x5BE0CD19UL;
269 return CRYPT_OK;
270 }
271
272 /**
273 Process a block of memory though the hash
274 @param md The hash state
275 @param in The data to hash
276 @param inlen The length of the data (octets)
277 @return CRYPT_OK if successful
278 */
279 HASH_PROCESS(sha256_process,s_sha256_compress, sha256, 64)
280
281 /**
282 Terminate the hash to get the digest
283 @param md The hash state
284 @param out [out] The destination of the hash (32 bytes)
285 @return CRYPT_OK if successful
286 */
287 int sha256_done(hash_state * md, unsigned char *out)
288 {
289 int i;
290
291 LTC_ARGCHK(md != NULL);
292 LTC_ARGCHK(out != NULL);
293
294 if (md->sha256.curlen >= sizeof(md->sha256.buf)) {
295 return CRYPT_INVALID_ARG;
296 }
297
298
299 /* increase the length of the message */
300 md->sha256.length += md->sha256.curlen * 8;
301
302 /* append the '1' bit */
303 md->sha256.buf[md->sha256.curlen++] = (unsigned char)0x80;
304
305 /* if the length is currently above 56 bytes we append zeros
306 * then compress. Then we can fall back to padding zeros and length
307 * encoding like normal.
308 */
309 if (md->sha256.curlen > 56) {
310 while (md->sha256.curlen < 64) {
311 md->sha256.buf[md->sha256.curlen++] = (unsigned char)0;
312 }
313 s_sha256_compress(md, md->sha256.buf);
314 md->sha256.curlen = 0;
315 }
316
317 /* pad upto 56 bytes of zeroes */
318 while (md->sha256.curlen < 56) {
319 md->sha256.buf[md->sha256.curlen++] = (unsigned char)0;
320 }
321
322 /* store length */
323 STORE64H(md->sha256.length, md->sha256.buf+56);
324 s_sha256_compress(md, md->sha256.buf);
325
326 /* copy output */
327 for (i = 0; i < 8; i++) {
328 STORE32H(md->sha256.state[i], out+(4*i));
329 }
330 #ifdef LTC_CLEAN_STACK
331 zeromem(md, sizeof(hash_state));
332 #endif
333 return CRYPT_OK;
334 }
335
336 static int compare_testvector(const unsigned char *a, size_t asize, const unsigned char *b, size_t bsize, const char *name, size_t i)
337 {
338 return !(asize == bsize && memcmp(a, b, asize) == 0);
339 }
340 /**
341 Self-test the hash
342 @return CRYPT_OK if successful, CRYPT_NOP if self-tests have been disabled
343 */
344 int sha256_test(void)
345 {
346 #ifndef LTC_TEST
347 return CRYPT_NOP;
348 #else
349 static const struct {
350 const char *msg;
351 unsigned char hash[32];
352 } tests[] = {
353 { "abc",
354 { 0xba, 0x78, 0x16, 0xbf, 0x8f, 0x01, 0xcf, 0xea,
355 0x41, 0x41, 0x40, 0xde, 0x5d, 0xae, 0x22, 0x23,
356 0xb0, 0x03, 0x61, 0xa3, 0x96, 0x17, 0x7a, 0x9c,
357 0xb4, 0x10, 0xff, 0x61, 0xf2, 0x00, 0x15, 0xad }
358 },
359 { "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
360 { 0x24, 0x8d, 0x6a, 0x61, 0xd2, 0x06, 0x38, 0xb8,
361 0xe5, 0xc0, 0x26, 0x93, 0x0c, 0x3e, 0x60, 0x39,
362 0xa3, 0x3c, 0xe4, 0x59, 0x64, 0xff, 0x21, 0x67,
363 0xf6, 0xec, 0xed, 0xd4, 0x19, 0xdb, 0x06, 0xc1 }
364 },
365 };
366
367 int i;
368 unsigned char tmp[32];
369 hash_state md;
370
371 for (i = 0; i < (int)(sizeof(tests) / sizeof(tests[0])); i++) {
372 sha256_init(&md);
373 sha256_process(&md, (unsigned char*)tests[i].msg, (unsigned long)XSTRLEN(tests[i].msg));
374 sha256_done(&md, tmp);
375 if (compare_testvector(tmp, sizeof(tmp), tests[i].hash, sizeof(tests[i].hash), "SHA256", i)) {
376 return CRYPT_FAIL_TESTVECTOR;
377 }
378 }
379 return CRYPT_OK;
380 #endif
381 }
382
383 /* ------------------------------------------------------------------------ */
384 /*
385 * MJR additions
386 */
387
388 #include <stdarg.h>
389
390 #include "t3std.h"
391 #include "vmdatasrc.h"
392
393 /*
394 * E-Z sha256 - hash a buffer and generate a printable hash string [mjr]
395 */
396 void sha256_ez(char *hash, const char *data, size_t len)
397 {
398 sha256_ctx ctx;
399 const int HASH_BYTES = 32;
400 unsigned char hval[HASH_BYTES];
401
402 /* calculate the hash in binary format */
403 sha256_begin(&ctx);
404 sha256_hash((const unsigned char *)data, len, &ctx);
405 sha256_end(hval, &ctx);
406
407 /* convert the binary hash to printable hex digits */
408 for (int i = 0 ; i < HASH_BYTES ; ++i, hash += 2)
409 byte_to_xdigits(hash, hval[i]);
410
411 /* null-terminate the string */
412 *hash = '\0';
413 }
414
415 /*
416 * Printf-style sha256 hashing [mjr]
417 */
418 void sha256_ezf(char *hash, const char *fmt, ...)
419 {
420 /* generate the hash source */
421 va_list args;
422 va_start(args, fmt);
423 char *src = t3vsprintf_alloc(fmt, args);
424 va_end(args);
425
426 /* hash the result */
427 sha256_ez(hash, src, strlen(src));
428
429 /* done with the source buffer */
430 t3free(src);
431 }
432
433 /*
434 * E-Z sha256 - hash from a data source [mjr]
435 */
436 void sha256_datasrc(char *buf, CVmDataSource *src, unsigned long len)
437 {
438 /* set up the hash accumulator */
439 sha256_ctx ctx;
440 sha256_begin(&ctx);
441
442 /* feed the selected range of bytes into the hash */
443 while (len != 0)
444 {
445 /* get a chunk up to one buffer-full, or the total remaining */
446 unsigned char buf[1024];
447 size_t cur = (len > sizeof(buf) ? sizeof(buf) : (size_t)len);
448
449 /* read the chunk */
450 if (cur != 0)
451 cur = src->readc(buf, cur);
452
453 /* if there's nothing left, we're done */
454 if (cur == 0)
455 break;
456
457 /* feed this chunk into the hash */
458 sha256_hash(buf, cur, &ctx);
459
460 /* deduct this chunk from the remaining length */
461 len -= cur;
462 }
463
464 /* calculate the hash result */
465 const int HASH_BYTES = 32;
466 unsigned char hash[HASH_BYTES];
467 sha256_end(hash, &ctx);
468
469 /* convert the binary hash to printable hex digits */
470 char *bufp = buf;
471 for (int i = 0 ; i < HASH_BYTES ; ++i, bufp += 2)
472 byte_to_xdigits(bufp, hash[i]);
473
474 /* null-terminate the string */
475 *bufp = '\0';
476 }
477
478 #endif