7#ifndef SECP256K1_HASH_IMPL_H
8#define SECP256K1_HASH_IMPL_H
17#define Ch(x,y,z) ((z) ^ ((x) & ((y) ^ (z))))
18#define Maj(x,y,z) (((x) & (y)) | ((z) & ((x) | (y))))
19#define Sigma0(x) (((x) >> 2 | (x) << 30) ^ ((x) >> 13 | (x) << 19) ^ ((x) >> 22 | (x) << 10))
20#define Sigma1(x) (((x) >> 6 | (x) << 26) ^ ((x) >> 11 | (x) << 21) ^ ((x) >> 25 | (x) << 7))
21#define sigma0(x) (((x) >> 7 | (x) << 25) ^ ((x) >> 18 | (x) << 14) ^ ((x) >> 3))
22#define sigma1(x) (((x) >> 17 | (x) << 15) ^ ((x) >> 19 | (x) << 13) ^ ((x) >> 10))
24#define Round(a,b,c,d,e,f,g,h,k,w) do { \
25 uint32_t t1 = (h) + Sigma1(e) + Ch((e), (f), (g)) + (k) + (w); \
26 uint32_t t2 = Sigma0(a) + Maj((a), (b), (c)); \
31#if defined(SECP256K1_BIG_ENDIAN)
33#elif defined(SECP256K1_LITTLE_ENDIAN)
34#define BE32(p) ((((p) & 0xFF) << 24) | (((p) & 0xFF00) << 8) | (((p) & 0xFF0000) >> 8) | (((p) & 0xFF000000) >> 24))
38 hash->
s[0] = 0x6a09e667ul;
39 hash->
s[1] = 0xbb67ae85ul;
40 hash->
s[2] = 0x3c6ef372ul;
41 hash->
s[3] = 0xa54ff53aul;
42 hash->
s[4] = 0x510e527ful;
43 hash->
s[5] = 0x9b05688cul;
44 hash->
s[6] = 0x1f83d9abul;
45 hash->
s[7] = 0x5be0cd19ul;
51 uint32_t a = s[0], b = s[1], c = s[2], d = s[3], e = s[4], f = s[5], g = s[6], h = s[7];
52 uint32_t w0, w1, w2, w3, w4, w5, w6, w7, w8, w9, w10, w11, w12, w13, w14, w15;
54 Round(a, b, c, d, e, f, g, h, 0x428a2f98, w0 = BE32(chunk[0]));
55 Round(h, a, b, c, d, e, f, g, 0x71374491, w1 = BE32(chunk[1]));
56 Round(g, h, a, b, c, d, e, f, 0xb5c0fbcf, w2 = BE32(chunk[2]));
57 Round(f, g, h, a, b, c, d, e, 0xe9b5dba5, w3 = BE32(chunk[3]));
58 Round(e, f, g, h, a, b, c, d, 0x3956c25b, w4 = BE32(chunk[4]));
59 Round(d, e, f, g, h, a, b, c, 0x59f111f1, w5 = BE32(chunk[5]));
60 Round(c, d, e, f, g, h, a, b, 0x923f82a4, w6 = BE32(chunk[6]));
61 Round(b, c, d, e, f, g, h, a, 0xab1c5ed5, w7 = BE32(chunk[7]));
62 Round(a, b, c, d, e, f, g, h, 0xd807aa98, w8 = BE32(chunk[8]));
63 Round(h, a, b, c, d, e, f, g, 0x12835b01, w9 = BE32(chunk[9]));
64 Round(g, h, a, b, c, d, e, f, 0x243185be, w10 = BE32(chunk[10]));
65 Round(f, g, h, a, b, c, d, e, 0x550c7dc3, w11 = BE32(chunk[11]));
66 Round(e, f, g, h, a, b, c, d, 0x72be5d74, w12 = BE32(chunk[12]));
67 Round(d, e, f, g, h, a, b, c, 0x80deb1fe, w13 = BE32(chunk[13]));
68 Round(c, d, e, f, g, h, a, b, 0x9bdc06a7, w14 = BE32(chunk[14]));
69 Round(b, c, d, e, f, g, h, a, 0xc19bf174, w15 = BE32(chunk[15]));
100 Round(e, f, g, h, a, b, c, d, 0xd192e819, w12 +=
sigma1(w10) + w5 +
sigma0(w13));
101 Round(d, e, f, g, h, a, b, c, 0xd6990624, w13 +=
sigma1(w11) + w6 +
sigma0(w14));
102 Round(c, d, e, f, g, h, a, b, 0xf40e3585, w14 +=
sigma1(w12) + w7 +
sigma0(w15));
117 Round(e, f, g, h, a, b, c, d, 0x90befffa, w12 +=
sigma1(w10) + w5 +
sigma0(w13));
118 Round(d, e, f, g, h, a, b, c, 0xa4506ceb, w13 +=
sigma1(w11) + w6 +
sigma0(w14));
133 size_t bufsize = hash->
bytes & 0x3F;
136 while (len >= 64 - bufsize) {
138 size_t chunk_len = 64 - bufsize;
139 memcpy(((
unsigned char*)hash->
buf) + bufsize, data, chunk_len);
147 memcpy(((
unsigned char*)hash->
buf) + bufsize, data, len);
152 static const unsigned char pad[64] = {0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
153 uint32_t sizedesc[2];
156 sizedesc[0] = BE32(hash->
bytes >> 29);
157 sizedesc[1] = BE32(hash->
bytes << 3);
160 for (i = 0; i < 8; i++) {
161 out[i] = BE32(hash->
s[i]);
164 memcpy(out32, (
const unsigned char*)out, 32);
170 unsigned char buf[32];
182 unsigned char rkey[64];
183 if (keylen <=
sizeof(rkey)) {
184 memcpy(rkey, key, keylen);
185 memset(rkey + keylen, 0,
sizeof(rkey) - keylen);
191 memset(rkey + 32, 0, 32);
195 for (n = 0; n <
sizeof(rkey); n++) {
201 for (n = 0; n <
sizeof(rkey); n++) {
202 rkey[n] ^= 0x5c ^ 0x36;
205 memset(rkey, 0,
sizeof(rkey));
213 unsigned char temp[32];
223 static const unsigned char zero[1] = {0x00};
224 static const unsigned char one[1] = {0x01};
226 memset(rng->
v, 0x01, 32);
227 memset(rng->
k, 0x00, 32);
253 static const unsigned char zero[1] = {0x00};
274 memcpy(out, rng->
v, now);
283 memset(rng->
k, 0, 32);
284 memset(rng->
v, 0, 32);
static void secp256k1_sha256_initialize(secp256k1_sha256 *hash)
static void secp256k1_rfc6979_hmac_sha256_generate(secp256k1_rfc6979_hmac_sha256 *rng, unsigned char *out, size_t outlen)
static void secp256k1_hmac_sha256_finalize(secp256k1_hmac_sha256 *hash, unsigned char *out32)
#define Round(a, b, c, d, e, f, g, h, k, w)
static void secp256k1_sha256_finalize(secp256k1_sha256 *hash, unsigned char *out32)
static void secp256k1_rfc6979_hmac_sha256_initialize(secp256k1_rfc6979_hmac_sha256 *rng, const unsigned char *key, size_t keylen)
static void secp256k1_rfc6979_hmac_sha256_finalize(secp256k1_rfc6979_hmac_sha256 *rng)
static void secp256k1_sha256_write(secp256k1_sha256 *hash, const unsigned char *data, size_t len)
static void secp256k1_hmac_sha256_write(secp256k1_hmac_sha256 *hash, const unsigned char *data, size_t size)
static void secp256k1_sha256_transform(uint32_t *s, const uint32_t *chunk)
Perform one SHA-256 transformation, processing 16 big endian 32-bit words.
static void secp256k1_sha256_initialize_tagged(secp256k1_sha256 *hash, const unsigned char *tag, size_t taglen)
static void secp256k1_hmac_sha256_initialize(secp256k1_hmac_sha256 *hash, const unsigned char *key, size_t keylen)
Internal SHA-256 implementation.
#define VERIFY_CHECK(cond)