Skip to content

Commit f2991d1

Browse files
committed
Change rfc6979 implementation to be a generic PRNG
1 parent 0cbc860 commit f2991d1

File tree

6 files changed

+23
-23
lines changed

6 files changed

+23
-23
lines changed

src/ecmult_gen_impl.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ static void secp256k1_ecmult_gen_blind(secp256k1_ecmult_gen_context_t *ctx, cons
143143
unsigned char nonce32[32];
144144
secp256k1_rfc6979_hmac_sha256_t rng;
145145
int retry;
146+
unsigned char keydata[64] = {0};
146147
if (!seed32) {
147148
/* When seed is NULL, reset the initial point and blinding value. */
148149
secp256k1_gej_set_ge(&ctx->initial, &secp256k1_ge_const_g);
@@ -155,7 +156,12 @@ static void secp256k1_ecmult_gen_blind(secp256k1_ecmult_gen_context_t *ctx, cons
155156
* and guards against weak or adversarial seeds. This is a simpler and safer interface than
156157
* asking the caller for blinding values directly and expecting them to retry on failure.
157158
*/
158-
secp256k1_rfc6979_hmac_sha256_initialize(&rng, seed32 ? seed32 : nonce32, 32, nonce32, 32, NULL, 0);
159+
memcpy(keydata, nonce32, 32);
160+
if (seed32) {
161+
memcpy(keydata + 32, seed32, 32);
162+
}
163+
secp256k1_rfc6979_hmac_sha256_initialize(&rng, keydata, seed32 ? 64 : 32);
164+
memset(keydata, 0, sizeof(keydata));
159165
/* Retry for out of range results to achieve uniformity. */
160166
do {
161167
secp256k1_rfc6979_hmac_sha256_generate(&rng, nonce32, 32);

src/hash.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ typedef struct {
3434
int retry;
3535
} secp256k1_rfc6979_hmac_sha256_t;
3636

37-
static void secp256k1_rfc6979_hmac_sha256_initialize(secp256k1_rfc6979_hmac_sha256_t *rng, const unsigned char *key, size_t keylen, const unsigned char *msg, size_t msglen, const unsigned char *rnd, size_t rndlen);
37+
static void secp256k1_rfc6979_hmac_sha256_initialize(secp256k1_rfc6979_hmac_sha256_t *rng, const unsigned char *key, size_t keylen);
3838
static void secp256k1_rfc6979_hmac_sha256_generate(secp256k1_rfc6979_hmac_sha256_t *rng, unsigned char *out, size_t outlen);
3939
static void secp256k1_rfc6979_hmac_sha256_finalize(secp256k1_rfc6979_hmac_sha256_t *rng);
4040

src/hash_impl.h

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ static void secp256k1_hmac_sha256_finalize(secp256k1_hmac_sha256_t *hash, unsign
202202
}
203203

204204

205-
static void secp256k1_rfc6979_hmac_sha256_initialize(secp256k1_rfc6979_hmac_sha256_t *rng, const unsigned char *key, size_t keylen, const unsigned char *msg, size_t msglen, const unsigned char *rnd, size_t rndlen) {
205+
static void secp256k1_rfc6979_hmac_sha256_initialize(secp256k1_rfc6979_hmac_sha256_t *rng, const unsigned char *key, size_t keylen) {
206206
secp256k1_hmac_sha256_t hmac;
207207
static const unsigned char zero[1] = {0x00};
208208
static const unsigned char one[1] = {0x01};
@@ -215,11 +215,6 @@ static void secp256k1_rfc6979_hmac_sha256_initialize(secp256k1_rfc6979_hmac_sha2
215215
secp256k1_hmac_sha256_write(&hmac, rng->v, 32);
216216
secp256k1_hmac_sha256_write(&hmac, zero, 1);
217217
secp256k1_hmac_sha256_write(&hmac, key, keylen);
218-
secp256k1_hmac_sha256_write(&hmac, msg, msglen);
219-
if (rnd && rndlen) {
220-
/* RFC6979 3.6 "Additional data". */
221-
secp256k1_hmac_sha256_write(&hmac, rnd, rndlen);
222-
}
223218
secp256k1_hmac_sha256_finalize(&hmac, rng->k);
224219
secp256k1_hmac_sha256_initialize(&hmac, rng->k, 32);
225220
secp256k1_hmac_sha256_write(&hmac, rng->v, 32);
@@ -230,11 +225,6 @@ static void secp256k1_rfc6979_hmac_sha256_initialize(secp256k1_rfc6979_hmac_sha2
230225
secp256k1_hmac_sha256_write(&hmac, rng->v, 32);
231226
secp256k1_hmac_sha256_write(&hmac, one, 1);
232227
secp256k1_hmac_sha256_write(&hmac, key, keylen);
233-
secp256k1_hmac_sha256_write(&hmac, msg, msglen);
234-
if (rnd && rndlen) {
235-
/* RFC6979 3.6 "Additional data". */
236-
secp256k1_hmac_sha256_write(&hmac, rnd, rndlen);
237-
}
238228
secp256k1_hmac_sha256_finalize(&hmac, rng->k);
239229
secp256k1_hmac_sha256_initialize(&hmac, rng->k, 32);
240230
secp256k1_hmac_sha256_write(&hmac, rng->v, 32);

src/secp256k1.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,16 @@ int secp256k1_ecdsa_verify(const secp256k1_context_t* ctx, const unsigned char *
8686
}
8787

8888
static int nonce_function_rfc6979(unsigned char *nonce32, const unsigned char *msg32, const unsigned char *key32, unsigned int counter, const void *data) {
89+
unsigned char keydata[96];
8990
secp256k1_rfc6979_hmac_sha256_t rng;
9091
unsigned int i;
91-
secp256k1_rfc6979_hmac_sha256_initialize(&rng, key32, 32, msg32, 32, (const unsigned char*)data, data != NULL ? 32 : 0);
92+
memcpy(keydata, key32, 32);
93+
memcpy(keydata + 32, msg32, 32);
94+
if (data != NULL) {
95+
memcpy(keydata + 64, data, 32);
96+
}
97+
secp256k1_rfc6979_hmac_sha256_initialize(&rng, keydata, data != NULL ? 96 : 64);
98+
memset(keydata, 0, sizeof(keydata));
9299
for (i = 0; i <= counter; i++) {
93100
secp256k1_rfc6979_hmac_sha256_generate(&rng, nonce32, 32);
94101
}

src/testrand_impl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ static uint32_t secp256k1_test_rng_precomputed[8];
1818
static int secp256k1_test_rng_precomputed_used = 8;
1919

2020
SECP256K1_INLINE static void secp256k1_rand_seed(const unsigned char *seed16) {
21-
secp256k1_rfc6979_hmac_sha256_initialize(&secp256k1_test_rng, (const unsigned char*)"TestRNG", 7, seed16, 16, NULL, 0);
21+
secp256k1_rfc6979_hmac_sha256_initialize(&secp256k1_test_rng, seed16, 16);
2222
}
2323

2424
SECP256K1_INLINE static uint32_t secp256k1_rand32(void) {

src/tests.c

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -231,16 +231,14 @@ void run_hmac_sha256_tests(void) {
231231
}
232232

233233
void run_rfc6979_hmac_sha256_tests(void) {
234-
static const unsigned char key1[32] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x00};
235-
static const unsigned char msg1[32] = {0x4b, 0xf5, 0x12, 0x2f, 0x34, 0x45, 0x54, 0xc5, 0x3b, 0xde, 0x2e, 0xbb, 0x8c, 0xd2, 0xb7, 0xe3, 0xd1, 0x60, 0x0a, 0xd6, 0x31, 0xc3, 0x85, 0xa5, 0xd7, 0xcc, 0xe2, 0x3c, 0x77, 0x85, 0x45, 0x9a};
234+
static const unsigned char key1[65] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x00, 0x4b, 0xf5, 0x12, 0x2f, 0x34, 0x45, 0x54, 0xc5, 0x3b, 0xde, 0x2e, 0xbb, 0x8c, 0xd2, 0xb7, 0xe3, 0xd1, 0x60, 0x0a, 0xd6, 0x31, 0xc3, 0x85, 0xa5, 0xd7, 0xcc, 0xe2, 0x3c, 0x77, 0x85, 0x45, 0x9a, 0};
236235
static const unsigned char out1[3][32] = {
237236
{0x4f, 0xe2, 0x95, 0x25, 0xb2, 0x08, 0x68, 0x09, 0x15, 0x9a, 0xcd, 0xf0, 0x50, 0x6e, 0xfb, 0x86, 0xb0, 0xec, 0x93, 0x2c, 0x7b, 0xa4, 0x42, 0x56, 0xab, 0x32, 0x1e, 0x42, 0x1e, 0x67, 0xe9, 0xfb},
238237
{0x2b, 0xf0, 0xff, 0xf1, 0xd3, 0xc3, 0x78, 0xa2, 0x2d, 0xc5, 0xde, 0x1d, 0x85, 0x65, 0x22, 0x32, 0x5c, 0x65, 0xb5, 0x04, 0x49, 0x1a, 0x0c, 0xbd, 0x01, 0xcb, 0x8f, 0x3a, 0xa6, 0x7f, 0xfd, 0x4a},
239238
{0xf5, 0x28, 0xb4, 0x10, 0xcb, 0x54, 0x1f, 0x77, 0x00, 0x0d, 0x7a, 0xfb, 0x6c, 0x5b, 0x53, 0xc5, 0xc4, 0x71, 0xea, 0xb4, 0x3e, 0x46, 0x6d, 0x9a, 0xc5, 0x19, 0x0c, 0x39, 0xc8, 0x2f, 0xd8, 0x2e}
240239
};
241240

242-
static const unsigned char key2[32] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
243-
static const unsigned char msg2[32] = {0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55};
241+
static const unsigned char key2[64] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55};
244242
static const unsigned char out2[3][32] = {
245243
{0x9c, 0x23, 0x6c, 0x16, 0x5b, 0x82, 0xae, 0x0c, 0xd5, 0x90, 0x65, 0x9e, 0x10, 0x0b, 0x6b, 0xab, 0x30, 0x36, 0xe7, 0xba, 0x8b, 0x06, 0x74, 0x9b, 0xaf, 0x69, 0x81, 0xe1, 0x6f, 0x1a, 0x2b, 0x95},
246244
{0xdf, 0x47, 0x10, 0x61, 0x62, 0x5b, 0xc0, 0xea, 0x14, 0xb6, 0x82, 0xfe, 0xee, 0x2c, 0x9c, 0x02, 0xf2, 0x35, 0xda, 0x04, 0x20, 0x4c, 0x1d, 0x62, 0xa1, 0x53, 0x6c, 0x6e, 0x17, 0xae, 0xd7, 0xa9},
@@ -249,24 +247,23 @@ void run_rfc6979_hmac_sha256_tests(void) {
249247

250248
secp256k1_rfc6979_hmac_sha256_t rng;
251249
unsigned char out[32];
252-
unsigned char zero[1] = {0};
253250
int i;
254251

255-
secp256k1_rfc6979_hmac_sha256_initialize(&rng, key1, 32, msg1, 32, NULL, 1);
252+
secp256k1_rfc6979_hmac_sha256_initialize(&rng, key1, 64);
256253
for (i = 0; i < 3; i++) {
257254
secp256k1_rfc6979_hmac_sha256_generate(&rng, out, 32);
258255
CHECK(memcmp(out, out1[i], 32) == 0);
259256
}
260257
secp256k1_rfc6979_hmac_sha256_finalize(&rng);
261258

262-
secp256k1_rfc6979_hmac_sha256_initialize(&rng, key1, 32, msg1, 32, zero, 1);
259+
secp256k1_rfc6979_hmac_sha256_initialize(&rng, key1, 65);
263260
for (i = 0; i < 3; i++) {
264261
secp256k1_rfc6979_hmac_sha256_generate(&rng, out, 32);
265262
CHECK(memcmp(out, out1[i], 32) != 0);
266263
}
267264
secp256k1_rfc6979_hmac_sha256_finalize(&rng);
268265

269-
secp256k1_rfc6979_hmac_sha256_initialize(&rng, key2, 32, msg2, 32, zero, 0);
266+
secp256k1_rfc6979_hmac_sha256_initialize(&rng, key2, 64);
270267
for (i = 0; i < 3; i++) {
271268
secp256k1_rfc6979_hmac_sha256_generate(&rng, out, 32);
272269
CHECK(memcmp(out, out2[i], 32) == 0);

0 commit comments

Comments
 (0)