88 #define LITTLE_ENDIAN 1234
89 #define BIG_ENDIAN 4321
93 #define BYTE_ORDER LITTLE_ENDIAN
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
106 #define SHA256_SHORT_BLOCK_LENGTH (SHA256_BLOCK_LENGTH - 8)
107 #define SHA512_SHORT_BLOCK_LENGTH (SHA512_BLOCK_LENGTH - 16)
111 #if BYTE_ORDER == LITTLE_ENDIAN
112 #define REVERSE32(w, x) \
114 sha2_word32 tmp = (w); \
115 tmp = (tmp >> 16) | (tmp << 16); \
116 (x) = ((tmp & 0xff00ff00UL) >> 8) | ((tmp & 0x00ff00ffUL) << 8); \
118 #define REVERSE64(w, x) \
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); \
132 #define ADDINC128(w, n) \
134 (w)[0] += (sha2_word64)(n); \
135 if ((w)[0] < (n)) { \
140 #define MEMSET_BZERO(p, l) memset((p), 0, (l))
141 #define MEMCPY_BCOPY(d, s, l) memcpy((d), (s), (l))
153 #define R(b, x) ((x) >> (b))
155 #define S32(b, x) (((x) >> (b)) | ((x) << (32 - (b))))
157 #define S64(b, x) (((x) >> (b)) | ((x) << (64 - (b))))
160 #define Ch(x, y, z) (((x) & (y)) ^ ((~(x)) & (z)))
161 #define Maj(x, y, z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
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)))
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)))
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};
349 0x6a09e667f3bcc908ULL,
350 0xbb67ae8584caa73bULL,
351 0x3c6ef372fe94f82bULL,
352 0xa54ff53a5f1d36f1ULL,
353 0x510e527fade682d1ULL,
354 0x9b05688c2b3e6c1fULL,
355 0x1f83d9abfb41bd6bULL,
356 0x5be0cd19137e2179ULL};
370 #ifdef SHA2_UNROLL_TRANSFORM
374 #if BYTE_ORDER == LITTLE_ENDIAN
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]; \
380 (h) = T1 + Sigma0_256(a) + Maj((a), (b), (c)); \
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++); \
389 (h) = T1 + Sigma0_256(a) + Maj((a), (b), (c)); \
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); \
401 (h) = T1 + Sigma0_256(a) + Maj((a), (b), (c)); \
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];
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);
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);
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;
458 a = b = c = d = e = f = g = h = T1 = 0;
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];
507 s0 = W256[(j + 1) & 0x0f];
509 s1 = W256[(j + 14) & 0x0f];
514 (W256[j & 0x0f] += s1 + W256[(j + 9) & 0x0f] + s0);
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;
539 a = b = c = d = e = f = g = h = T1 = T2 = 0;
546 unsigned int freespace, usedspace;
558 if (len >= freespace) {
561 context->
bitcount += freespace << 3;
570 usedspace = freespace = 0;
587 usedspace = freespace = 0;
593 unsigned int usedspace;
598 #if BYTE_ORDER == LITTLE_ENDIAN
604 context->
buffer[usedspace++] = 0x80;
633 #if BYTE_ORDER == LITTLE_ENDIAN
637 for (j = 0; j < 8; j++) {
639 *d++ = context->
state[j];
672 #ifdef SHA2_UNROLL_TRANSFORM
675 #if BYTE_ORDER == LITTLE_ENDIAN
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]; \
681 (h) = T1 + Sigma0_512(a) + Maj((a), (b), (c)), \
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++); \
690 (h) = T1 + Sigma0_512(a) + Maj((a), (b), (c)); \
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); \
702 (h) = T1 + Sigma0_512(a) + Maj((a), (b), (c)); \
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];
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);
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);
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;
756 a = b = c = d = e = f = g = h = T1 = 0;
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];
803 s0 = W512[(j + 1) & 0x0f];
805 s1 = W512[(j + 14) & 0x0f];
810 (W512[j & 0x0f] += s1 + W512[(j + 9) & 0x0f] + s0);
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;
835 a = b = c = d = e = f = g = h = T1 = T2 = 0;
842 unsigned int freespace, usedspace;
854 if (len >= freespace) {
866 usedspace = freespace = 0;
883 usedspace = freespace = 0;
888 unsigned int usedspace;
891 #if BYTE_ORDER == LITTLE_ENDIAN
898 context->
buffer[usedspace++] = 0x80;
940 #if BYTE_ORDER == LITTLE_ENDIAN
944 for (j = 0; j < 8; j++) {
946 *d++ = context->
state[j];
966 void hmac_sha256(
const uint8_t* key,
const uint32_t keylen,
const uint8_t* msg,
const uint32_t msglen, uint8_t* hmac)
977 memcpy(buf, key, keylen);
981 o_key_pad[i] = buf[i] ^ 0x5c;
982 i_key_pad[i] = buf[i] ^ 0x36;
996 void hmac_sha512(
const uint8_t* key,
const uint32_t keylen,
const uint8_t* msg,
const uint32_t msglen, uint8_t* hmac)
1007 memcpy(buf, key, keylen);
1011 o_key_pad[i] = buf[i] ^ 0x5c;
1012 i_key_pad[i] = buf[i] ^ 0x36;
1029 sha256_Raw((
const uint8_t*)datain, length, hashout);
1035 sha256_Raw((
const uint8_t*)datain, length, hashout);
void sha512_Init(SHA512_CTX *context)
void sha256_Update(SHA256_CTX *context, const sha2_byte *data, size_t len)
static const sha2_word64 sha512_initial_hash_value[8]
void sha512_Last(SHA512_CTX *)
#define SHA256_BLOCK_LENGTH
static const sha2_word32 K256[64]
void sha256_Raw(const sha2_byte *data, size_t len, uint8_t digest[SHA256_DIGEST_LENGTH])
void sha256_Final(sha2_byte digest[], SHA256_CTX *context)
static const sha2_word64 K512[80]
uint8_t buffer[SHA256_BLOCK_LENGTH]
void hmac_sha256(const uint8_t *key, const uint32_t keylen, const uint8_t *msg, const uint32_t msglen, uint8_t *hmac)
#define MEMCPY_BCOPY(d, s, l)
void sha256_Transform(SHA256_CTX *, const sha2_word32 *)
#define MEMSET_BZERO(p, l)
void sha512_Update(SHA512_CTX *context, const sha2_byte *data, size_t len)
void sha512_Transform(SHA512_CTX *, const sha2_word64 *)
static const sha2_word32 sha256_initial_hash_value[8]
void sha512_Final(sha2_byte digest[], SHA512_CTX *context)
void sha256_Init(SHA256_CTX *context)
#define SHA256_DIGEST_LENGTH
#define SHA512_SHORT_BLOCK_LENGTH
void btc_hash_sngl_sha256(const unsigned char *datain, size_t length, uint256 hashout)
void btc_hash(const unsigned char *datain, size_t length, uint256 hashout)
void hmac_sha512(const uint8_t *key, const uint32_t keylen, const uint8_t *msg, const uint32_t msglen, uint8_t *hmac)
void sha512_Raw(const sha2_byte *data, size_t len, uint8_t digest[SHA512_DIGEST_LENGTH])
#define SHA512_DIGEST_LENGTH
uint8_t buffer[SHA512_BLOCK_LENGTH]
#define SHA512_BLOCK_LENGTH
#define SHA256_SHORT_BLOCK_LENGTH