7 #ifndef SECP256K1_HASH_IMPL_H 8 #define SECP256K1_HASH_IMPL_H 16 #define Ch(x,y,z) ((z) ^ ((x) & ((y) ^ (z)))) 17 #define Maj(x,y,z) (((x) & (y)) | ((z) & ((x) | (y)))) 18 #define Sigma0(x) (((x) >> 2 | (x) << 30) ^ ((x) >> 13 | (x) << 19) ^ ((x) >> 22 | (x) << 10)) 19 #define Sigma1(x) (((x) >> 6 | (x) << 26) ^ ((x) >> 11 | (x) << 21) ^ ((x) >> 25 | (x) << 7)) 20 #define sigma0(x) (((x) >> 7 | (x) << 25) ^ ((x) >> 18 | (x) << 14) ^ ((x) >> 3)) 21 #define sigma1(x) (((x) >> 17 | (x) << 15) ^ ((x) >> 19 | (x) << 13) ^ ((x) >> 10)) 23 #define Round(a,b,c,d,e,f,g,h,k,w) do { \ 24 uint32_t t1 = (h) + Sigma1(e) + Ch((e), (f), (g)) + (k) + (w); \ 25 uint32_t t2 = Sigma0(a) + Maj((a), (b), (c)); \ 30 #ifdef WORDS_BIGENDIAN 33 #define BE32(p) ((((p) & 0xFF) << 24) | (((p) & 0xFF00) << 8) | (((p) & 0xFF0000) >> 8) | (((p) & 0xFF000000) >> 24)) 37 hash->
s[0] = 0x6a09e667ul;
38 hash->
s[1] = 0xbb67ae85ul;
39 hash->
s[2] = 0x3c6ef372ul;
40 hash->
s[3] = 0xa54ff53aul;
41 hash->
s[4] = 0x510e527ful;
42 hash->
s[5] = 0x9b05688cul;
43 hash->
s[6] = 0x1f83d9abul;
44 hash->
s[7] = 0x5be0cd19ul;
49 static void secp256k1_sha256_transform(uint32_t* s,
const uint32_t* chunk) {
50 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];
51 uint32_t w0, w1, w2, w3, w4, w5, w6, w7, w8, w9, w10, w11, w12, w13, w14, w15;
53 Round(a, b, c, d, e, f, g, h, 0x428a2f98, w0 =
BE32(chunk[0]));
54 Round(h, a, b, c, d, e, f, g, 0x71374491, w1 =
BE32(chunk[1]));
55 Round(g, h, a, b, c, d, e, f, 0xb5c0fbcf, w2 =
BE32(chunk[2]));
56 Round(f, g, h, a, b, c, d, e, 0xe9b5dba5, w3 =
BE32(chunk[3]));
57 Round(e, f, g, h, a, b, c, d, 0x3956c25b, w4 =
BE32(chunk[4]));
58 Round(d, e, f, g, h, a, b, c, 0x59f111f1, w5 =
BE32(chunk[5]));
59 Round(c, d, e, f, g, h, a, b, 0x923f82a4, w6 =
BE32(chunk[6]));
60 Round(b, c, d, e, f, g, h, a, 0xab1c5ed5, w7 =
BE32(chunk[7]));
61 Round(a, b, c, d, e, f, g, h, 0xd807aa98, w8 =
BE32(chunk[8]));
62 Round(h, a, b, c, d, e, f, g, 0x12835b01, w9 =
BE32(chunk[9]));
63 Round(g, h, a, b, c, d, e, f, 0x243185be, w10 =
BE32(chunk[10]));
64 Round(f, g, h, a, b, c, d, e, 0x550c7dc3, w11 =
BE32(chunk[11]));
65 Round(e, f, g, h, a, b, c, d, 0x72be5d74, w12 =
BE32(chunk[12]));
66 Round(d, e, f, g, h, a, b, c, 0x80deb1fe, w13 =
BE32(chunk[13]));
67 Round(c, d, e, f, g, h, a, b, 0x9bdc06a7, w14 =
BE32(chunk[14]));
68 Round(b, c, d, e, f, g, h, a, 0xc19bf174, w15 =
BE32(chunk[15]));
100 Round(d, e, f, g, h, a, b, c, 0xd6990624, w13 +=
sigma1(w11) + w6 +
sigma0(w14));
101 Round(c, d, e, f, g, h, a, b, 0xf40e3585, w14 +=
sigma1(w12) + w7 +
sigma0(w15));
116 Round(e, f, g, h, a, b, c, d, 0x90befffa, w12 +=
sigma1(w10) + w5 +
sigma0(w13));
117 Round(d, e, f, g, h, a, b, c, 0xa4506ceb, w13 +=
sigma1(w11) + w6 +
sigma0(w14));
132 size_t bufsize = hash->
bytes & 0x3F;
134 while (bufsize + len >= 64) {
136 memcpy(((
unsigned char*)hash->
buf) + bufsize,
data, 64 - bufsize);
137 data += 64 - bufsize;
139 secp256k1_sha256_transform(hash->
s, hash->
buf);
148 static void secp256k1_sha256_finalize(
secp256k1_sha256_t *hash,
unsigned char *out32) {
149 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};
150 uint32_t sizedesc[2];
155 secp256k1_sha256_write(hash, pad, 1 + ((119 - (hash->
bytes % 64)) % 64));
156 secp256k1_sha256_write(hash, (
const unsigned char*)sizedesc, 8);
157 for (i = 0; i < 8; i++) {
158 out[i] =
BE32(hash->
s[i]);
161 memcpy(out32, (
const unsigned char*)out, 32);
164 static void secp256k1_hmac_sha256_initialize(
secp256k1_hmac_sha256_t *hash,
const unsigned char *key,
size_t keylen) {
166 unsigned char rkey[64];
168 memcpy(rkey, key, keylen);
169 memset(rkey + keylen, 0, 64 - keylen);
172 secp256k1_sha256_initialize(&sha256);
173 secp256k1_sha256_write(&sha256, key, keylen);
174 secp256k1_sha256_finalize(&sha256, rkey);
175 memset(rkey + 32, 0, 32);
178 secp256k1_sha256_initialize(&hash->
outer);
179 for (n = 0; n < 64; n++) {
182 secp256k1_sha256_write(&hash->
outer, rkey, 64);
184 secp256k1_sha256_initialize(&hash->
inner);
185 for (n = 0; n < 64; n++) {
186 rkey[n] ^= 0x5c ^ 0x36;
188 secp256k1_sha256_write(&hash->
inner, rkey, 64);
193 secp256k1_sha256_write(&hash->
inner,
data, size);
197 unsigned char temp[32];
198 secp256k1_sha256_finalize(&hash->
inner, temp);
199 secp256k1_sha256_write(&hash->
outer, temp, 32);
201 secp256k1_sha256_finalize(&hash->
outer, out32);
207 static const unsigned char zero[1] = {0x00};
208 static const unsigned char one[1] = {0x01};
210 memset(rng->
v, 0x01, 32);
211 memset(rng->
k, 0x00, 32);
214 secp256k1_hmac_sha256_initialize(&hmac, rng->
k, 32);
215 secp256k1_hmac_sha256_write(&hmac, rng->
v, 32);
216 secp256k1_hmac_sha256_write(&hmac, zero, 1);
217 secp256k1_hmac_sha256_write(&hmac, key, keylen);
218 secp256k1_hmac_sha256_finalize(&hmac, rng->
k);
219 secp256k1_hmac_sha256_initialize(&hmac, rng->
k, 32);
220 secp256k1_hmac_sha256_write(&hmac, rng->
v, 32);
221 secp256k1_hmac_sha256_finalize(&hmac, rng->
v);
224 secp256k1_hmac_sha256_initialize(&hmac, rng->
k, 32);
225 secp256k1_hmac_sha256_write(&hmac, rng->
v, 32);
226 secp256k1_hmac_sha256_write(&hmac, one, 1);
227 secp256k1_hmac_sha256_write(&hmac, key, keylen);
228 secp256k1_hmac_sha256_finalize(&hmac, rng->
k);
229 secp256k1_hmac_sha256_initialize(&hmac, rng->
k, 32);
230 secp256k1_hmac_sha256_write(&hmac, rng->
v, 32);
231 secp256k1_hmac_sha256_finalize(&hmac, rng->
v);
237 static const unsigned char zero[1] = {0x00};
240 secp256k1_hmac_sha256_initialize(&hmac, rng->
k, 32);
241 secp256k1_hmac_sha256_write(&hmac, rng->
v, 32);
242 secp256k1_hmac_sha256_write(&hmac, zero, 1);
243 secp256k1_hmac_sha256_finalize(&hmac, rng->
k);
244 secp256k1_hmac_sha256_initialize(&hmac, rng->
k, 32);
245 secp256k1_hmac_sha256_write(&hmac, rng->
v, 32);
246 secp256k1_hmac_sha256_finalize(&hmac, rng->
v);
252 secp256k1_hmac_sha256_initialize(&hmac, rng->
k, 32);
253 secp256k1_hmac_sha256_write(&hmac, rng->
v, 32);
254 secp256k1_hmac_sha256_finalize(&hmac, rng->
v);
267 memset(rng->
k, 0, 32);
268 memset(rng->
v, 0, 32);
#define Round(a, b, c, d, e, f, g, h, k, w)
uint8_t const size_t const size
void * memcpy(void *a, const void *b, size_t c)
h256 sha256(bytesConstRef _input)