libbtc
bitcoinclibrary
sha2.c
Go to the documentation of this file.
1 
32 #include <string.h>
33 #include <stdint.h>
34 #include "sha2.h"
35 
36 #include "btc/hash.h"
37 /*
38  * ASSERT NOTE:
39  * Some sanity checking code is included using assert(). On my FreeBSD
40  * system, this additional code can be removed by compiling with NDEBUG
41  * defined. Check your own systems manpage on assert() to see how to
42  * compile WITHOUT the sanity checking code on your system.
43  *
44  * UNROLLED TRANSFORM LOOP NOTE:
45  * You can define SHA2_UNROLL_TRANSFORM to use the unrolled transform
46  * loop version for the hash transform rounds (defined using macros
47  * later in this file). Either define on the command line, for example:
48  *
49  * cc -DSHA2_UNROLL_TRANSFORM -o sha2 sha2.c sha2prog.c
50  *
51  * or define below:
52  *
53  * #define SHA2_UNROLL_TRANSFORM
54  *
55  */
56 
57 
58 /*** SHA-256/384/512 Machine Architecture Definitions *****************/
59 /*
60  * BYTE_ORDER NOTE:
61  *
62  * Please make sure that your system defines BYTE_ORDER. If your
63  * architecture is little-endian, make sure it also defines
64  * LITTLE_ENDIAN and that the two (BYTE_ORDER and LITTLE_ENDIAN) are
65  * equivilent.
66  *
67  * If your system does not define the above, then you can do so by
68  * hand like this:
69  *
70  * #define LITTLE_ENDIAN 1234
71  * #define BIG_ENDIAN 4321
72  *
73  * And for little-endian machines, add:
74  *
75  * #define BYTE_ORDER LITTLE_ENDIAN
76  *
77  * Or for big-endian machines:
78  *
79  * #define BYTE_ORDER BIG_ENDIAN
80  *
81  * The FreeBSD machine this was written on defines BYTE_ORDER
82  * appropriately by including <sys/types.h> (which in turn includes
83  * <machine/endian.h> where the appropriate definitions are actually
84  * made).
85  */
86 
87 #ifndef LITTLE_ENDIAN
88 #define LITTLE_ENDIAN 1234
89 #define BIG_ENDIAN 4321
90 #endif
91 
92 #ifndef BYTE_ORDER
93 #define BYTE_ORDER LITTLE_ENDIAN
94 #endif
95 
96 #if !defined(BYTE_ORDER) || (BYTE_ORDER != LITTLE_ENDIAN && BYTE_ORDER != BIG_ENDIAN)
97 #error Define BYTE_ORDER to be equal to either LITTLE_ENDIAN or BIG_ENDIAN
98 #endif
99 
100 typedef uint8_t sha2_byte; /* Exactly 1 byte */
101 typedef uint32_t sha2_word32; /* Exactly 4 bytes */
102 typedef uint64_t sha2_word64; /* Exactly 8 bytes */
103 
104 /*** SHA-256/384/512 Various Length Definitions ***********************/
105 /* NOTE: Most of these are in sha2.h */
106 #define SHA256_SHORT_BLOCK_LENGTH (SHA256_BLOCK_LENGTH - 8)
107 #define SHA512_SHORT_BLOCK_LENGTH (SHA512_BLOCK_LENGTH - 16)
108 
109 
110 /*** ENDIAN REVERSAL MACROS *******************************************/
111 #if BYTE_ORDER == LITTLE_ENDIAN
112 #define REVERSE32(w, x) \
113  { \
114  sha2_word32 tmp = (w); \
115  tmp = (tmp >> 16) | (tmp << 16); \
116  (x) = ((tmp & 0xff00ff00UL) >> 8) | ((tmp & 0x00ff00ffUL) << 8); \
117  }
118 #define REVERSE64(w, x) \
119  { \
120  sha2_word64 tmp = (w); \
121  tmp = (tmp >> 32) | (tmp << 32); \
122  tmp = ((tmp & 0xff00ff00ff00ff00ULL) >> 8) | ((tmp & 0x00ff00ff00ff00ffULL) << 8); \
123  (x) = ((tmp & 0xffff0000ffff0000ULL) >> 16) | ((tmp & 0x0000ffff0000ffffULL) << 16); \
124  }
125 #endif /* BYTE_ORDER == LITTLE_ENDIAN */
126 
127 /*
128  * Macro for incrementally adding the unsigned 64-bit integer n to the
129  * unsigned 128-bit integer (represented using a two-element array of
130  * 64-bit words):
131  */
132 #define ADDINC128(w, n) \
133  { \
134  (w)[0] += (sha2_word64)(n); \
135  if ((w)[0] < (n)) { \
136  (w)[1]++; \
137  } \
138  }
139 
140 #define MEMSET_BZERO(p, l) memset((p), 0, (l))
141 #define MEMCPY_BCOPY(d, s, l) memcpy((d), (s), (l))
142 
143 /*** THE SIX LOGICAL FUNCTIONS ****************************************/
144 /*
145  * Bit shifting and rotation (used by the six SHA-XYZ logical functions:
146  *
147  * NOTE: The naming of R and S appears backwards here (R is a SHIFT and
148  * S is a ROTATION) because the SHA-256/384/512 description document
149  * (see http://csrc.nist.gov/cryptval/shs/sha256-384-512.pdf) uses this
150  * same "backwards" definition.
151  */
152 /* Shift-right (used in SHA-256, SHA-384, and SHA-512): */
153 #define R(b, x) ((x) >> (b))
154 /* 32-bit Rotate-right (used in SHA-256): */
155 #define S32(b, x) (((x) >> (b)) | ((x) << (32 - (b))))
156 /* 64-bit Rotate-right (used in SHA-384 and SHA-512): */
157 #define S64(b, x) (((x) >> (b)) | ((x) << (64 - (b))))
158 
159 /* Two of six logical functions used in SHA-256, SHA-384, and SHA-512: */
160 #define Ch(x, y, z) (((x) & (y)) ^ ((~(x)) & (z)))
161 #define Maj(x, y, z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
162 
163 /* Four of six logical functions used in SHA-256: */
164 #define Sigma0_256(x) (S32(2, (x)) ^ S32(13, (x)) ^ S32(22, (x)))
165 #define Sigma1_256(x) (S32(6, (x)) ^ S32(11, (x)) ^ S32(25, (x)))
166 #define sigma0_256(x) (S32(7, (x)) ^ S32(18, (x)) ^ R(3, (x)))
167 #define sigma1_256(x) (S32(17, (x)) ^ S32(19, (x)) ^ R(10, (x)))
168 
169 /* Four of six logical functions used in SHA-384 and SHA-512: */
170 #define Sigma0_512(x) (S64(28, (x)) ^ S64(34, (x)) ^ S64(39, (x)))
171 #define Sigma1_512(x) (S64(14, (x)) ^ S64(18, (x)) ^ S64(41, (x)))
172 #define sigma0_512(x) (S64(1, (x)) ^ S64(8, (x)) ^ R(7, (x)))
173 #define sigma1_512(x) (S64(19, (x)) ^ S64(61, (x)) ^ R(6, (x)))
174 
175 /*** INTERNAL FUNCTION PROTOTYPES *************************************/
176 /* NOTE: These should not be accessed directly from outside this
177  * library -- they are intended for private internal visibility/use
178  * only.
179  */
180 void sha512_Last(SHA512_CTX*);
183 
184 
185 /*** SHA-XYZ INITIAL HASH VALUES AND CONSTANTS ************************/
186 /* Hash constant words K for SHA-256: */
187 static const sha2_word32 K256[64] = {
188  0x428a2f98UL,
189  0x71374491UL,
190  0xb5c0fbcfUL,
191  0xe9b5dba5UL,
192  0x3956c25bUL,
193  0x59f111f1UL,
194  0x923f82a4UL,
195  0xab1c5ed5UL,
196  0xd807aa98UL,
197  0x12835b01UL,
198  0x243185beUL,
199  0x550c7dc3UL,
200  0x72be5d74UL,
201  0x80deb1feUL,
202  0x9bdc06a7UL,
203  0xc19bf174UL,
204  0xe49b69c1UL,
205  0xefbe4786UL,
206  0x0fc19dc6UL,
207  0x240ca1ccUL,
208  0x2de92c6fUL,
209  0x4a7484aaUL,
210  0x5cb0a9dcUL,
211  0x76f988daUL,
212  0x983e5152UL,
213  0xa831c66dUL,
214  0xb00327c8UL,
215  0xbf597fc7UL,
216  0xc6e00bf3UL,
217  0xd5a79147UL,
218  0x06ca6351UL,
219  0x14292967UL,
220  0x27b70a85UL,
221  0x2e1b2138UL,
222  0x4d2c6dfcUL,
223  0x53380d13UL,
224  0x650a7354UL,
225  0x766a0abbUL,
226  0x81c2c92eUL,
227  0x92722c85UL,
228  0xa2bfe8a1UL,
229  0xa81a664bUL,
230  0xc24b8b70UL,
231  0xc76c51a3UL,
232  0xd192e819UL,
233  0xd6990624UL,
234  0xf40e3585UL,
235  0x106aa070UL,
236  0x19a4c116UL,
237  0x1e376c08UL,
238  0x2748774cUL,
239  0x34b0bcb5UL,
240  0x391c0cb3UL,
241  0x4ed8aa4aUL,
242  0x5b9cca4fUL,
243  0x682e6ff3UL,
244  0x748f82eeUL,
245  0x78a5636fUL,
246  0x84c87814UL,
247  0x8cc70208UL,
248  0x90befffaUL,
249  0xa4506cebUL,
250  0xbef9a3f7UL,
251  0xc67178f2UL};
252 
253 /* Initial hash value H for SHA-256: */
255  0x6a09e667UL,
256  0xbb67ae85UL,
257  0x3c6ef372UL,
258  0xa54ff53aUL,
259  0x510e527fUL,
260  0x9b05688cUL,
261  0x1f83d9abUL,
262  0x5be0cd19UL};
263 
264 /* Hash constant words K for SHA-384 and SHA-512: */
265 static const sha2_word64 K512[80] = {
266  0x428a2f98d728ae22ULL,
267  0x7137449123ef65cdULL,
268  0xb5c0fbcfec4d3b2fULL,
269  0xe9b5dba58189dbbcULL,
270  0x3956c25bf348b538ULL,
271  0x59f111f1b605d019ULL,
272  0x923f82a4af194f9bULL,
273  0xab1c5ed5da6d8118ULL,
274  0xd807aa98a3030242ULL,
275  0x12835b0145706fbeULL,
276  0x243185be4ee4b28cULL,
277  0x550c7dc3d5ffb4e2ULL,
278  0x72be5d74f27b896fULL,
279  0x80deb1fe3b1696b1ULL,
280  0x9bdc06a725c71235ULL,
281  0xc19bf174cf692694ULL,
282  0xe49b69c19ef14ad2ULL,
283  0xefbe4786384f25e3ULL,
284  0x0fc19dc68b8cd5b5ULL,
285  0x240ca1cc77ac9c65ULL,
286  0x2de92c6f592b0275ULL,
287  0x4a7484aa6ea6e483ULL,
288  0x5cb0a9dcbd41fbd4ULL,
289  0x76f988da831153b5ULL,
290  0x983e5152ee66dfabULL,
291  0xa831c66d2db43210ULL,
292  0xb00327c898fb213fULL,
293  0xbf597fc7beef0ee4ULL,
294  0xc6e00bf33da88fc2ULL,
295  0xd5a79147930aa725ULL,
296  0x06ca6351e003826fULL,
297  0x142929670a0e6e70ULL,
298  0x27b70a8546d22ffcULL,
299  0x2e1b21385c26c926ULL,
300  0x4d2c6dfc5ac42aedULL,
301  0x53380d139d95b3dfULL,
302  0x650a73548baf63deULL,
303  0x766a0abb3c77b2a8ULL,
304  0x81c2c92e47edaee6ULL,
305  0x92722c851482353bULL,
306  0xa2bfe8a14cf10364ULL,
307  0xa81a664bbc423001ULL,
308  0xc24b8b70d0f89791ULL,
309  0xc76c51a30654be30ULL,
310  0xd192e819d6ef5218ULL,
311  0xd69906245565a910ULL,
312  0xf40e35855771202aULL,
313  0x106aa07032bbd1b8ULL,
314  0x19a4c116b8d2d0c8ULL,
315  0x1e376c085141ab53ULL,
316  0x2748774cdf8eeb99ULL,
317  0x34b0bcb5e19b48a8ULL,
318  0x391c0cb3c5c95a63ULL,
319  0x4ed8aa4ae3418acbULL,
320  0x5b9cca4f7763e373ULL,
321  0x682e6ff3d6b2b8a3ULL,
322  0x748f82ee5defb2fcULL,
323  0x78a5636f43172f60ULL,
324  0x84c87814a1f0ab72ULL,
325  0x8cc702081a6439ecULL,
326  0x90befffa23631e28ULL,
327  0xa4506cebde82bde9ULL,
328  0xbef9a3f7b2c67915ULL,
329  0xc67178f2e372532bULL,
330  0xca273eceea26619cULL,
331  0xd186b8c721c0c207ULL,
332  0xeada7dd6cde0eb1eULL,
333  0xf57d4f7fee6ed178ULL,
334  0x06f067aa72176fbaULL,
335  0x0a637dc5a2c898a6ULL,
336  0x113f9804bef90daeULL,
337  0x1b710b35131c471bULL,
338  0x28db77f523047d84ULL,
339  0x32caab7b40c72493ULL,
340  0x3c9ebe0a15c9bebcULL,
341  0x431d67c49c100d4cULL,
342  0x4cc5d4becb3e42b6ULL,
343  0x597f299cfc657e2aULL,
344  0x5fcb6fab3ad6faecULL,
345  0x6c44198c4a475817ULL};
346 
347 /* Initial hash value H for SHA-512 */
349  0x6a09e667f3bcc908ULL,
350  0xbb67ae8584caa73bULL,
351  0x3c6ef372fe94f82bULL,
352  0xa54ff53a5f1d36f1ULL,
353  0x510e527fade682d1ULL,
354  0x9b05688c2b3e6c1fULL,
355  0x1f83d9abfb41bd6bULL,
356  0x5be0cd19137e2179ULL};
357 
358 
359 /*** SHA-256: *********************************************************/
360 void sha256_Init(SHA256_CTX* context)
361 {
362  if (context == (SHA256_CTX*)0) {
363  return;
364  }
367  context->bitcount = 0;
368 }
369 
370 #ifdef SHA2_UNROLL_TRANSFORM
371 
372 /* Unrolled SHA-256 round macros: */
373 
374 #if BYTE_ORDER == LITTLE_ENDIAN
375 
376 #define ROUND256_0_TO_15(a, b, c, d, e, f, g, h) \
377  REVERSE32(*data++, W256[j]); \
378  T1 = (h)+Sigma1_256(e) + Ch((e), (f), (g)) + K256[j] + W256[j]; \
379  (d) += T1; \
380  (h) = T1 + Sigma0_256(a) + Maj((a), (b), (c)); \
381  j++
382 
383 
384 #else /* BYTE_ORDER == LITTLE_ENDIAN */
385 
386 #define ROUND256_0_TO_15(a, b, c, d, e, f, g, h) \
387  T1 = (h)+Sigma1_256(e) + Ch((e), (f), (g)) + K256[j] + (W256[j] = *data++); \
388  (d) += T1; \
389  (h) = T1 + Sigma0_256(a) + Maj((a), (b), (c)); \
390  j++
391 
392 #endif /* BYTE_ORDER == LITTLE_ENDIAN */
393 
394 #define ROUND256(a, b, c, d, e, f, g, h) \
395  s0 = W256[(j + 1) & 0x0f]; \
396  s0 = sigma0_256(s0); \
397  s1 = W256[(j + 14) & 0x0f]; \
398  s1 = sigma1_256(s1); \
399  T1 = (h)+Sigma1_256(e) + Ch((e), (f), (g)) + K256[j] + (W256[j & 0x0f] += s1 + W256[(j + 9) & 0x0f] + s0); \
400  (d) += T1; \
401  (h) = T1 + Sigma0_256(a) + Maj((a), (b), (c)); \
402  j++
403 
404 void sha256_Transform(SHA256_CTX* context, const sha2_word32* data)
405 {
406  sha2_word32 a, b, c, d, e, f, g, h, s0, s1;
407  sha2_word32 T1, *W256;
408  int j;
409 
410  W256 = (sha2_word32*)context->buffer;
411 
412  /* Initialize registers with the prev. intermediate value */
413  a = context->state[0];
414  b = context->state[1];
415  c = context->state[2];
416  d = context->state[3];
417  e = context->state[4];
418  f = context->state[5];
419  g = context->state[6];
420  h = context->state[7];
421 
422  j = 0;
423  do {
424  /* Rounds 0 to 15 (unrolled): */
425  ROUND256_0_TO_15(a, b, c, d, e, f, g, h);
426  ROUND256_0_TO_15(h, a, b, c, d, e, f, g);
427  ROUND256_0_TO_15(g, h, a, b, c, d, e, f);
428  ROUND256_0_TO_15(f, g, h, a, b, c, d, e);
429  ROUND256_0_TO_15(e, f, g, h, a, b, c, d);
430  ROUND256_0_TO_15(d, e, f, g, h, a, b, c);
431  ROUND256_0_TO_15(c, d, e, f, g, h, a, b);
432  ROUND256_0_TO_15(b, c, d, e, f, g, h, a);
433  } while (j < 16);
434 
435  /* Now for the remaining rounds to 64: */
436  do {
437  ROUND256(a, b, c, d, e, f, g, h);
438  ROUND256(h, a, b, c, d, e, f, g);
439  ROUND256(g, h, a, b, c, d, e, f);
440  ROUND256(f, g, h, a, b, c, d, e);
441  ROUND256(e, f, g, h, a, b, c, d);
442  ROUND256(d, e, f, g, h, a, b, c);
443  ROUND256(c, d, e, f, g, h, a, b);
444  ROUND256(b, c, d, e, f, g, h, a);
445  } while (j < 64);
446 
447  /* Compute the current intermediate hash value */
448  context->state[0] += a;
449  context->state[1] += b;
450  context->state[2] += c;
451  context->state[3] += d;
452  context->state[4] += e;
453  context->state[5] += f;
454  context->state[6] += g;
455  context->state[7] += h;
456 
457  /* Clean up */
458  a = b = c = d = e = f = g = h = T1 = 0;
459 }
460 
461 #else /* SHA2_UNROLL_TRANSFORM */
462 
463 void sha256_Transform(SHA256_CTX* context, const sha2_word32* data)
464 {
465  sha2_word32 a, b, c, d, e, f, g, h, s0, s1;
466  sha2_word32 T1, T2, *W256;
467  int j;
468 
469  W256 = (sha2_word32*)context->buffer;
470 
471  /* Initialize registers with the prev. intermediate value */
472  a = context->state[0];
473  b = context->state[1];
474  c = context->state[2];
475  d = context->state[3];
476  e = context->state[4];
477  f = context->state[5];
478  g = context->state[6];
479  h = context->state[7];
480 
481  j = 0;
482  do {
484  /* Copy data while converting to host byte order */
485  REVERSE32(*data++, W256[j]);
486  /* Apply the SHA-256 compression function to update a..h */
487  T1 = h + Sigma1_256(e) + Ch(e, f, g) + K256[j] + W256[j];
488 #else /* BYTE_ORDER == LITTLE_ENDIAN */
489  /* Apply the SHA-256 compression function to update a..h with copy */
490  T1 = h + Sigma1_256(e) + Ch(e, f, g) + K256[j] + (W256[j] = *data++);
491 #endif /* BYTE_ORDER == LITTLE_ENDIAN */
492  T2 = Sigma0_256(a) + Maj(a, b, c);
493  h = g;
494  g = f;
495  f = e;
496  e = d + T1;
497  d = c;
498  c = b;
499  b = a;
500  a = T1 + T2;
501 
502  j++;
503  } while (j < 16);
504 
505  do {
506  /* Part of the message block expansion: */
507  s0 = W256[(j + 1) & 0x0f];
508  s0 = sigma0_256(s0);
509  s1 = W256[(j + 14) & 0x0f];
510  s1 = sigma1_256(s1);
511 
512  /* Apply the SHA-256 compression function to update a..h */
513  T1 = h + Sigma1_256(e) + Ch(e, f, g) + K256[j] +
514  (W256[j & 0x0f] += s1 + W256[(j + 9) & 0x0f] + s0);
515  T2 = Sigma0_256(a) + Maj(a, b, c);
516  h = g;
517  g = f;
518  f = e;
519  e = d + T1;
520  d = c;
521  c = b;
522  b = a;
523  a = T1 + T2;
524 
525  j++;
526  } while (j < 64);
527 
528  /* Compute the current intermediate hash value */
529  context->state[0] += a;
530  context->state[1] += b;
531  context->state[2] += c;
532  context->state[3] += d;
533  context->state[4] += e;
534  context->state[5] += f;
535  context->state[6] += g;
536  context->state[7] += h;
537 
538  /* Clean up */
539  a = b = c = d = e = f = g = h = T1 = T2 = 0;
540 }
541 
542 #endif /* SHA2_UNROLL_TRANSFORM */
543 
544 void sha256_Update(SHA256_CTX* context, const sha2_byte* data, size_t len)
545 {
546  unsigned int freespace, usedspace;
547 
548  if (len == 0) {
549  /* Calling with no data is valid - we do nothing */
550  return;
551  }
552 
553  usedspace = (context->bitcount >> 3) % SHA256_BLOCK_LENGTH;
554  if (usedspace > 0) {
555  /* Calculate how much free space is available in the buffer */
556  freespace = SHA256_BLOCK_LENGTH - usedspace;
557 
558  if (len >= freespace) {
559  /* Fill the buffer completely and process it */
560  MEMCPY_BCOPY(&context->buffer[usedspace], data, freespace);
561  context->bitcount += freespace << 3;
562  len -= freespace;
563  data += freespace;
564  sha256_Transform(context, (sha2_word32*)context->buffer);
565  } else {
566  /* The buffer is not yet full */
567  MEMCPY_BCOPY(&context->buffer[usedspace], data, len);
568  context->bitcount += len << 3;
569  /* Clean up: */
570  usedspace = freespace = 0;
571  return;
572  }
573  }
574  while (len >= SHA256_BLOCK_LENGTH) {
575  /* Process as many complete blocks as we can */
576  sha256_Transform(context, (const sha2_word32*)data);
577  context->bitcount += SHA256_BLOCK_LENGTH << 3;
578  len -= SHA256_BLOCK_LENGTH;
579  data += SHA256_BLOCK_LENGTH;
580  }
581  if (len > 0) {
582  /* There's left-overs, so save 'em */
583  MEMCPY_BCOPY(context->buffer, data, len);
584  context->bitcount += len << 3;
585  }
586  /* Clean up: */
587  usedspace = freespace = 0;
588 }
589 
590 void sha256_Final(sha2_byte digest[], SHA256_CTX* context)
591 {
592  sha2_word32* d = (sha2_word32*)digest;
593  unsigned int usedspace;
594 
595  /* If no digest buffer is passed, we don't bother doing this: */
596  if (digest != (sha2_byte*)0) {
597  usedspace = (context->bitcount >> 3) % SHA256_BLOCK_LENGTH;
598 #if BYTE_ORDER == LITTLE_ENDIAN
599  /* Convert FROM host byte order */
600  REVERSE64(context->bitcount, context->bitcount);
601 #endif
602  if (usedspace > 0) {
603  /* Begin padding with a 1 bit: */
604  context->buffer[usedspace++] = 0x80;
605 
606  if (usedspace <= SHA256_SHORT_BLOCK_LENGTH) {
607  /* Set-up for the last transform: */
608  MEMSET_BZERO(&context->buffer[usedspace], SHA256_SHORT_BLOCK_LENGTH - usedspace);
609  } else {
610  if (usedspace < SHA256_BLOCK_LENGTH) {
611  MEMSET_BZERO(&context->buffer[usedspace], SHA256_BLOCK_LENGTH - usedspace);
612  }
613  /* Do second-to-last transform: */
614  sha256_Transform(context, (sha2_word32*)context->buffer);
615 
616  /* And set-up for the last transform: */
618  }
619  } else {
620  /* Set-up for the last transform: */
622 
623  /* Begin padding with a 1 bit: */
624  *context->buffer = 0x80;
625  }
626  /* Set the bit count: */
628  *t = context->bitcount;
629 
630  /* Final transform: */
631  sha256_Transform(context, (sha2_word32*)context->buffer);
632 
633 #if BYTE_ORDER == LITTLE_ENDIAN
634  {
635  /* Convert TO host byte order */
636  int j;
637  for (j = 0; j < 8; j++) {
638  REVERSE32(context->state[j], context->state[j]);
639  *d++ = context->state[j];
640  }
641  }
642 #else
644 #endif
645  }
646 
647  /* Clean up state data: */
648  MEMSET_BZERO(context, sizeof(SHA256_CTX));
649  usedspace = 0;
650 }
651 
652 void sha256_Raw(const sha2_byte* data, size_t len, uint8_t digest[SHA256_DIGEST_LENGTH])
653 {
654  SHA256_CTX context;
655  sha256_Init(&context);
656  sha256_Update(&context, data, len);
657  sha256_Final(digest, &context);
658 }
659 
660 
661 /*** SHA-512: *********************************************************/
662 void sha512_Init(SHA512_CTX* context)
663 {
664  if (context == (SHA512_CTX*)0) {
665  return;
666  }
669  context->bitcount[0] = context->bitcount[1] = 0;
670 }
671 
672 #ifdef SHA2_UNROLL_TRANSFORM
673 
674 /* Unrolled SHA-512 round macros: */
675 #if BYTE_ORDER == LITTLE_ENDIAN
676 
677 #define ROUND512_0_TO_15(a, b, c, d, e, f, g, h) \
678  REVERSE64(*data++, W512[j]); \
679  T1 = (h)+Sigma1_512(e) + Ch((e), (f), (g)) + K512[j] + W512[j]; \
680  (d) += T1, \
681  (h) = T1 + Sigma0_512(a) + Maj((a), (b), (c)), \
682  j++
683 
684 
685 #else /* BYTE_ORDER == LITTLE_ENDIAN */
686 
687 #define ROUND512_0_TO_15(a, b, c, d, e, f, g, h) \
688  T1 = (h)+Sigma1_512(e) + Ch((e), (f), (g)) + K512[j] + (W512[j] = *data++); \
689  (d) += T1; \
690  (h) = T1 + Sigma0_512(a) + Maj((a), (b), (c)); \
691  j++
692 
693 #endif /* BYTE_ORDER == LITTLE_ENDIAN */
694 
695 #define ROUND512(a, b, c, d, e, f, g, h) \
696  s0 = W512[(j + 1) & 0x0f]; \
697  s0 = sigma0_512(s0); \
698  s1 = W512[(j + 14) & 0x0f]; \
699  s1 = sigma1_512(s1); \
700  T1 = (h)+Sigma1_512(e) + Ch((e), (f), (g)) + K512[j] + (W512[j & 0x0f] += s1 + W512[(j + 9) & 0x0f] + s0); \
701  (d) += T1; \
702  (h) = T1 + Sigma0_512(a) + Maj((a), (b), (c)); \
703  j++
704 
705 void sha512_Transform(SHA512_CTX* context, const sha2_word64* data)
706 {
707  sha2_word64 a, b, c, d, e, f, g, h, s0, s1;
708  sha2_word64 T1, *W512 = (sha2_word64*)context->buffer;
709  int j;
710 
711  /* Initialize registers with the prev. intermediate value */
712  a = context->state[0];
713  b = context->state[1];
714  c = context->state[2];
715  d = context->state[3];
716  e = context->state[4];
717  f = context->state[5];
718  g = context->state[6];
719  h = context->state[7];
720 
721  j = 0;
722  do {
723  ROUND512_0_TO_15(a, b, c, d, e, f, g, h);
724  ROUND512_0_TO_15(h, a, b, c, d, e, f, g);
725  ROUND512_0_TO_15(g, h, a, b, c, d, e, f);
726  ROUND512_0_TO_15(f, g, h, a, b, c, d, e);
727  ROUND512_0_TO_15(e, f, g, h, a, b, c, d);
728  ROUND512_0_TO_15(d, e, f, g, h, a, b, c);
729  ROUND512_0_TO_15(c, d, e, f, g, h, a, b);
730  ROUND512_0_TO_15(b, c, d, e, f, g, h, a);
731  } while (j < 16);
732 
733  /* Now for the remaining rounds up to 79: */
734  do {
735  ROUND512(a, b, c, d, e, f, g, h);
736  ROUND512(h, a, b, c, d, e, f, g);
737  ROUND512(g, h, a, b, c, d, e, f);
738  ROUND512(f, g, h, a, b, c, d, e);
739  ROUND512(e, f, g, h, a, b, c, d);
740  ROUND512(d, e, f, g, h, a, b, c);
741  ROUND512(c, d, e, f, g, h, a, b);
742  ROUND512(b, c, d, e, f, g, h, a);
743  } while (j < 80);
744 
745  /* Compute the current intermediate hash value */
746  context->state[0] += a;
747  context->state[1] += b;
748  context->state[2] += c;
749  context->state[3] += d;
750  context->state[4] += e;
751  context->state[5] += f;
752  context->state[6] += g;
753  context->state[7] += h;
754 
755  /* Clean up */
756  a = b = c = d = e = f = g = h = T1 = 0;
757 }
758 
759 #else /* SHA2_UNROLL_TRANSFORM */
760 
761 void sha512_Transform(SHA512_CTX* context, const sha2_word64* data)
762 {
763  sha2_word64 a, b, c, d, e, f, g, h, s0, s1;
764  sha2_word64 T1, T2, *W512 = (sha2_word64*)context->buffer;
765  int j;
766 
767  /* Initialize registers with the prev. intermediate value */
768  a = context->state[0];
769  b = context->state[1];
770  c = context->state[2];
771  d = context->state[3];
772  e = context->state[4];
773  f = context->state[5];
774  g = context->state[6];
775  h = context->state[7];
776 
777  j = 0;
778  do {
780  /* Convert TO host byte order */
781  REVERSE64(*data++, W512[j]);
782  /* Apply the SHA-512 compression function to update a..h */
783  T1 = h + Sigma1_512(e) + Ch(e, f, g) + K512[j] + W512[j];
784 #else /* BYTE_ORDER == LITTLE_ENDIAN */
785  /* Apply the SHA-512 compression function to update a..h with copy */
786  T1 = h + Sigma1_512(e) + Ch(e, f, g) + K512[j] + (W512[j] = *data++);
787 #endif /* BYTE_ORDER == LITTLE_ENDIAN */
788  T2 = Sigma0_512(a) + Maj(a, b, c);
789  h = g;
790  g = f;
791  f = e;
792  e = d + T1;
793  d = c;
794  c = b;
795  b = a;
796  a = T1 + T2;
797 
798  j++;
799  } while (j < 16);
800 
801  do {
802  /* Part of the message block expansion: */
803  s0 = W512[(j + 1) & 0x0f];
804  s0 = sigma0_512(s0);
805  s1 = W512[(j + 14) & 0x0f];
806  s1 = sigma1_512(s1);
807 
808  /* Apply the SHA-512 compression function to update a..h */
809  T1 = h + Sigma1_512(e) + Ch(e, f, g) + K512[j] +
810  (W512[j & 0x0f] += s1 + W512[(j + 9) & 0x0f] + s0);
811  T2 = Sigma0_512(a) + Maj(a, b, c);
812  h = g;
813  g = f;
814  f = e;
815  e = d + T1;
816  d = c;
817  c = b;
818  b = a;
819  a = T1 + T2;
820 
821  j++;
822  } while (j < 80);
823 
824  /* Compute the current intermediate hash value */
825  context->state[0] += a;
826  context->state[1] += b;
827  context->state[2] += c;
828  context->state[3] += d;
829  context->state[4] += e;
830  context->state[5] += f;
831  context->state[6] += g;
832  context->state[7] += h;
833 
834  /* Clean up */
835  a = b = c = d = e = f = g = h = T1 = T2 = 0;
836 }
837 
838 #endif /* SHA2_UNROLL_TRANSFORM */
839 
840 void sha512_Update(SHA512_CTX* context, const sha2_byte* data, size_t len)
841 {
842  unsigned int freespace, usedspace;
843 
844  if (len == 0) {
845  /* Calling with no data is valid - we do nothing */
846  return;
847  }
848 
849  usedspace = (context->bitcount[0] >> 3) % SHA512_BLOCK_LENGTH;
850  if (usedspace > 0) {
851  /* Calculate how much free space is available in the buffer */
852  freespace = SHA512_BLOCK_LENGTH - usedspace;
853 
854  if (len >= freespace) {
855  /* Fill the buffer completely and process it */
856  MEMCPY_BCOPY(&context->buffer[usedspace], data, freespace);
857  ADDINC128(context->bitcount, freespace << 3);
858  len -= freespace;
859  data += freespace;
860  sha512_Transform(context, (sha2_word64*)context->buffer);
861  } else {
862  /* The buffer is not yet full */
863  MEMCPY_BCOPY(&context->buffer[usedspace], data, len);
864  ADDINC128(context->bitcount, len << 3);
865  /* Clean up: */
866  usedspace = freespace = 0;
867  return;
868  }
869  }
870  while (len >= SHA512_BLOCK_LENGTH) {
871  /* Process as many complete blocks as we can */
872  sha512_Transform(context, (const sha2_word64*)data);
873  ADDINC128(context->bitcount, SHA512_BLOCK_LENGTH << 3);
874  len -= SHA512_BLOCK_LENGTH;
875  data += SHA512_BLOCK_LENGTH;
876  }
877  if (len > 0) {
878  /* There's left-overs, so save 'em */
879  MEMCPY_BCOPY(context->buffer, data, len);
880  ADDINC128(context->bitcount, len << 3);
881  }
882  /* Clean up: */
883  usedspace = freespace = 0;
884 }
885 
886 void sha512_Last(SHA512_CTX* context)
887 {
888  unsigned int usedspace;
889 
890  usedspace = (context->bitcount[0] >> 3) % SHA512_BLOCK_LENGTH;
891 #if BYTE_ORDER == LITTLE_ENDIAN
892  /* Convert FROM host byte order */
893  REVERSE64(context->bitcount[0], context->bitcount[0]);
894  REVERSE64(context->bitcount[1], context->bitcount[1]);
895 #endif
896  if (usedspace > 0) {
897  /* Begin padding with a 1 bit: */
898  context->buffer[usedspace++] = 0x80;
899 
900  if (usedspace <= SHA512_SHORT_BLOCK_LENGTH) {
901  /* Set-up for the last transform: */
902  MEMSET_BZERO(&context->buffer[usedspace], SHA512_SHORT_BLOCK_LENGTH - usedspace);
903  } else {
904  if (usedspace < SHA512_BLOCK_LENGTH) {
905  MEMSET_BZERO(&context->buffer[usedspace], SHA512_BLOCK_LENGTH - usedspace);
906  }
907  /* Do second-to-last transform: */
908  sha512_Transform(context, (sha2_word64*)context->buffer);
909 
910  /* And set-up for the last transform: */
911  MEMSET_BZERO(context->buffer, SHA512_BLOCK_LENGTH - 2);
912  }
913  } else {
914  /* Prepare for final transform: */
916 
917  /* Begin padding with a 1 bit: */
918  *context->buffer = 0x80;
919  }
920  /* Store the length of input data (in bits): */
921  sha2_word64* t;
923  *t = context->bitcount[1];
924  t = (sha2_word64*)&context->buffer[SHA512_SHORT_BLOCK_LENGTH + 8];
925  *t = context->bitcount[0];
926 
927  /* Final transform: */
928  sha512_Transform(context, (sha2_word64*)context->buffer);
929 }
930 
931 void sha512_Final(sha2_byte digest[], SHA512_CTX* context)
932 {
933  sha2_word64* d = (sha2_word64*)digest;
934 
935  /* If no digest buffer is passed, we don't bother doing this: */
936  if (digest != (sha2_byte*)0) {
937  sha512_Last(context);
938 
939 /* Save the hash data for output: */
940 #if BYTE_ORDER == LITTLE_ENDIAN
941  {
942  /* Convert TO host byte order */
943  int j;
944  for (j = 0; j < 8; j++) {
945  REVERSE64(context->state[j], context->state[j]);
946  *d++ = context->state[j];
947  }
948  }
949 #else
951 #endif
952  }
953 
954  /* Zero out state data */
955  MEMSET_BZERO(context, sizeof(SHA512_CTX));
956 }
957 
958 void sha512_Raw(const sha2_byte* data, size_t len, uint8_t digest[SHA512_DIGEST_LENGTH])
959 {
960  SHA512_CTX context;
961  sha512_Init(&context);
962  sha512_Update(&context, data, len);
963  sha512_Final(digest, &context);
964 }
965 
966 void hmac_sha256(const uint8_t* key, const uint32_t keylen, const uint8_t* msg, const uint32_t msglen, uint8_t* hmac)
967 {
968  int i;
969  uint8_t buf[SHA256_BLOCK_LENGTH], o_key_pad[SHA256_BLOCK_LENGTH],
970  i_key_pad[SHA256_BLOCK_LENGTH];
971  SHA256_CTX ctx;
972 
973  memset(buf, 0, SHA256_BLOCK_LENGTH);
974  if (keylen > SHA256_BLOCK_LENGTH) {
975  sha256_Raw(key, keylen, buf);
976  } else {
977  memcpy(buf, key, keylen);
978  }
979 
980  for (i = 0; i < SHA256_BLOCK_LENGTH; i++) {
981  o_key_pad[i] = buf[i] ^ 0x5c;
982  i_key_pad[i] = buf[i] ^ 0x36;
983  }
984 
985  sha256_Init(&ctx);
986  sha256_Update(&ctx, i_key_pad, SHA256_BLOCK_LENGTH);
987  sha256_Update(&ctx, msg, msglen);
988  sha256_Final(buf, &ctx);
989 
990  sha256_Init(&ctx);
991  sha256_Update(&ctx, o_key_pad, SHA256_BLOCK_LENGTH);
993  sha256_Final(hmac, &ctx);
994 }
995 
996 void hmac_sha512(const uint8_t* key, const uint32_t keylen, const uint8_t* msg, const uint32_t msglen, uint8_t* hmac)
997 {
998  int i;
999  uint8_t buf[SHA512_BLOCK_LENGTH], o_key_pad[SHA512_BLOCK_LENGTH],
1000  i_key_pad[SHA512_BLOCK_LENGTH];
1001  SHA512_CTX ctx;
1002 
1003  memset(buf, 0, SHA512_BLOCK_LENGTH);
1004  if (keylen > SHA512_BLOCK_LENGTH) {
1005  sha512_Raw(key, keylen, buf);
1006  } else {
1007  memcpy(buf, key, keylen);
1008  }
1009 
1010  for (i = 0; i < SHA512_BLOCK_LENGTH; i++) {
1011  o_key_pad[i] = buf[i] ^ 0x5c;
1012  i_key_pad[i] = buf[i] ^ 0x36;
1013  }
1014 
1015  sha512_Init(&ctx);
1016  sha512_Update(&ctx, i_key_pad, SHA512_BLOCK_LENGTH);
1017  sha512_Update(&ctx, msg, msglen);
1018  sha512_Final(buf, &ctx);
1019 
1020  sha512_Init(&ctx);
1021  sha512_Update(&ctx, o_key_pad, SHA512_BLOCK_LENGTH);
1022  sha512_Update(&ctx, buf, SHA512_DIGEST_LENGTH);
1023  sha512_Final(hmac, &ctx);
1024 }
1025 
1026 void btc_hash(const unsigned char* datain, size_t length, uint256 hashout)
1027 {
1028  //bitcoin double sha256 hash
1029  sha256_Raw((const uint8_t*)datain, length, hashout);
1030  sha256_Raw(hashout, 32, hashout);
1031 }
1032 
1033 void btc_hash_sngl_sha256(const unsigned char* datain, size_t length, uint256 hashout)
1034 {
1035  sha256_Raw((const uint8_t*)datain, length, hashout);
1036 }
#define Sigma0_512(x)
Definition: sha2.c:170
size_t len
Definition: buffer.h:22
void sha512_Init(SHA512_CTX *context)
Definition: sha2.c:662
#define BYTE_ORDER
Definition: sha2.c:93
void sha256_Update(SHA256_CTX *context, const sha2_byte *data, size_t len)
Definition: sha2.c:544
static const sha2_word64 sha512_initial_hash_value[8]
Definition: sha2.c:348
uint32_t state[8]
Definition: sha2.h:47
uint32_t sha2_word32
Definition: sha2.c:101
void sha512_Last(SHA512_CTX *)
Definition: sha2.c:886
#define SHA256_BLOCK_LENGTH
Definition: sha2.h:38
static const sha2_word32 K256[64]
Definition: sha2.c:187
#define Ch(x, y, z)
Definition: sha2.c:160
void sha256_Raw(const sha2_byte *data, size_t len, uint8_t digest[SHA256_DIGEST_LENGTH])
Definition: sha2.c:652
void sha256_Final(sha2_byte digest[], SHA256_CTX *context)
Definition: sha2.c:590
uint64_t bitcount[2]
Definition: sha2.h:54
#define sigma1_512(x)
Definition: sha2.c:173
static const sha2_word64 K512[80]
Definition: sha2.c:265
#define Sigma1_256(x)
Definition: sha2.c:165
#define sigma1_256(x)
Definition: sha2.c:167
uint8_t buffer[SHA256_BLOCK_LENGTH]
Definition: sha2.h:49
uint64_t bitcount
Definition: sha2.h:48
void hmac_sha256(const uint8_t *key, const uint32_t keylen, const uint8_t *msg, const uint32_t msglen, uint8_t *hmac)
Definition: sha2.c:966
#define MEMCPY_BCOPY(d, s, l)
Definition: sha2.c:141
void sha256_Transform(SHA256_CTX *, const sha2_word32 *)
Definition: sha2.c:463
#define MEMSET_BZERO(p, l)
Definition: sha2.c:140
void sha512_Update(SHA512_CTX *context, const sha2_byte *data, size_t len)
Definition: sha2.c:840
void sha512_Transform(SHA512_CTX *, const sha2_word64 *)
Definition: sha2.c:761
#define sigma0_256(x)
Definition: sha2.c:166
#define ADDINC128(w, n)
Definition: sha2.c:132
static const sha2_word32 sha256_initial_hash_value[8]
Definition: sha2.c:254
void sha512_Final(sha2_byte digest[], SHA512_CTX *context)
Definition: sha2.c:931
#define REVERSE64(w, x)
Definition: sha2.c:118
void sha256_Init(SHA256_CTX *context)
Definition: sha2.c:360
#define Sigma0_256(x)
Definition: sha2.c:164
uint64_t sha2_word64
Definition: sha2.c:102
#define SHA256_DIGEST_LENGTH
Definition: sha2.h:39
#define SHA512_SHORT_BLOCK_LENGTH
Definition: sha2.c:107
#define sigma0_512(x)
Definition: sha2.c:172
#define LITTLE_ENDIAN
Definition: sha2.c:88
uint8_t sha2_byte
Definition: sha2.c:100
#define Maj(x, y, z)
Definition: sha2.c:161
void btc_hash_sngl_sha256(const unsigned char *datain, size_t length, uint256 hashout)
Definition: sha2.c:1033
uint64_t state[8]
Definition: sha2.h:53
#define REVERSE32(w, x)
Definition: sha2.c:112
#define Sigma1_512(x)
Definition: sha2.c:171
void btc_hash(const unsigned char *datain, size_t length, uint256 hashout)
Definition: sha2.c:1026
void hmac_sha512(const uint8_t *key, const uint32_t keylen, const uint8_t *msg, const uint32_t msglen, uint8_t *hmac)
Definition: sha2.c:996
void sha512_Raw(const sha2_byte *data, size_t len, uint8_t digest[SHA512_DIGEST_LENGTH])
Definition: sha2.c:958
#define SHA512_DIGEST_LENGTH
Definition: sha2.h:42
uint8_t buffer[SHA512_BLOCK_LENGTH]
Definition: sha2.h:55
#define SHA512_BLOCK_LENGTH
Definition: sha2.h:41
#define SHA256_SHORT_BLOCK_LENGTH
Definition: sha2.c:106
uint8_t uint256[32]
Definition: hash.h:44