Skip to content

Commit 49e6630

Browse files
committed
refactor: move RNG seeding to testrand
1 parent b110c10 commit 49e6630

File tree

3 files changed

+56
-42
lines changed

3 files changed

+56
-42
lines changed

src/testrand.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,10 @@ static void secp256k1_rand_bytes_test(unsigned char *bytes, size_t len);
3838
/** Flip a single random bit in a byte array */
3939
static void secp256k1_rand_flip(unsigned char *b, size_t len);
4040

41+
/** Initialize the test RNG using (hex encoded) array up to 16 bytes, or randomly if hexseed is NULL. */
42+
static void secp256k1_rand_init(const char* hexseed);
43+
44+
/** Print final test information. */
45+
static void secp256k1_rand_finish(void);
46+
4147
#endif /* SECP256K1_TESTRAND_H */

src/testrand_impl.h

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#define SECP256K1_TESTRAND_IMPL_H
99

1010
#include <stdint.h>
11+
#include <stdio.h>
1112
#include <string.h>
1213

1314
#include "testrand.h"
@@ -111,4 +112,47 @@ static void secp256k1_rand_flip(unsigned char *b, size_t len) {
111112
b[secp256k1_rand_int(len)] ^= (1 << secp256k1_rand_int(8));
112113
}
113114

115+
static void secp256k1_rand_init(const char* hexseed) {
116+
unsigned char seed16[16] = {0};
117+
if (hexseed) {
118+
int pos = 0;
119+
while (pos < 16 && hexseed[0] != 0 && hexseed[1] != 0) {
120+
unsigned short sh;
121+
if ((sscanf(hexseed, "%2hx", &sh)) == 1) {
122+
seed16[pos] = sh;
123+
} else {
124+
break;
125+
}
126+
hexseed += 2;
127+
pos++;
128+
}
129+
} else {
130+
FILE *frand = fopen("/dev/urandom", "r");
131+
if ((frand == NULL) || fread(&seed16, 1, sizeof(seed16), frand) != sizeof(seed16)) {
132+
uint64_t t = time(NULL) * (uint64_t)1337;
133+
fprintf(stderr, "WARNING: could not read 16 bytes from /dev/urandom; falling back to insecure PRNG\n");
134+
seed16[0] ^= t;
135+
seed16[1] ^= t >> 8;
136+
seed16[2] ^= t >> 16;
137+
seed16[3] ^= t >> 24;
138+
seed16[4] ^= t >> 32;
139+
seed16[5] ^= t >> 40;
140+
seed16[6] ^= t >> 48;
141+
seed16[7] ^= t >> 56;
142+
}
143+
if (frand) {
144+
fclose(frand);
145+
}
146+
}
147+
148+
printf("random seed = %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x\n", seed16[0], seed16[1], seed16[2], seed16[3], seed16[4], seed16[5], seed16[6], seed16[7], seed16[8], seed16[9], seed16[10], seed16[11], seed16[12], seed16[13], seed16[14], seed16[15]);
149+
secp256k1_rand_seed(seed16);
150+
}
151+
152+
static void secp256k1_rand_finish(void) {
153+
unsigned char run32[32];
154+
secp256k1_rand256(run32);
155+
printf("random run = %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x\n", run32[0], run32[1], run32[2], run32[3], run32[4], run32[5], run32[6], run32[7], run32[8], run32[9], run32[10], run32[11], run32[12], run32[13], run32[14], run32[15]);
156+
}
157+
114158
#endif /* SECP256K1_TESTRAND_IMPL_H */

src/tests.c

Lines changed: 6 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -5530,9 +5530,6 @@ void run_cmov_tests(void) {
55305530
}
55315531

55325532
int main(int argc, char **argv) {
5533-
unsigned char seed16[16] = {0};
5534-
unsigned char run32[32] = {0};
5535-
55365533
/* Disable buffering for stdout to improve reliability of getting
55375534
* diagnostic information. Happens right at the start of main because
55385535
* setbuf must be used before any other operation on the stream. */
@@ -5545,52 +5542,20 @@ int main(int argc, char **argv) {
55455542
if (argc > 1) {
55465543
count = strtol(argv[1], NULL, 0);
55475544
}
5545+
printf("test count = %i\n", count);
55485546

55495547
/* find random seed */
5550-
if (argc > 2) {
5551-
int pos = 0;
5552-
const char* ch = argv[2];
5553-
while (pos < 16 && ch[0] != 0 && ch[1] != 0) {
5554-
unsigned short sh;
5555-
if ((sscanf(ch, "%2hx", &sh)) == 1) {
5556-
seed16[pos] = sh;
5557-
} else {
5558-
break;
5559-
}
5560-
ch += 2;
5561-
pos++;
5562-
}
5563-
} else {
5564-
FILE *frand = fopen("/dev/urandom", "r");
5565-
if ((frand == NULL) || fread(&seed16, 1, sizeof(seed16), frand) != sizeof(seed16)) {
5566-
uint64_t t = time(NULL) * (uint64_t)1337;
5567-
fprintf(stderr, "WARNING: could not read 16 bytes from /dev/urandom; falling back to insecure PRNG\n");
5568-
seed16[0] ^= t;
5569-
seed16[1] ^= t >> 8;
5570-
seed16[2] ^= t >> 16;
5571-
seed16[3] ^= t >> 24;
5572-
seed16[4] ^= t >> 32;
5573-
seed16[5] ^= t >> 40;
5574-
seed16[6] ^= t >> 48;
5575-
seed16[7] ^= t >> 56;
5576-
}
5577-
if (frand) {
5578-
fclose(frand);
5579-
}
5580-
}
5581-
secp256k1_rand_seed(seed16);
5582-
5583-
printf("test count = %i\n", count);
5584-
printf("random seed = %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x\n", seed16[0], seed16[1], seed16[2], seed16[3], seed16[4], seed16[5], seed16[6], seed16[7], seed16[8], seed16[9], seed16[10], seed16[11], seed16[12], seed16[13], seed16[14], seed16[15]);
5548+
secp256k1_rand_init(argc > 2 ? argv[2] : NULL);
55855549

55865550
/* initialize */
55875551
run_context_tests(0);
55885552
run_context_tests(1);
55895553
run_scratch_tests();
55905554
ctx = secp256k1_context_create(SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY);
55915555
if (secp256k1_rand_bits(1)) {
5592-
secp256k1_rand256(run32);
5593-
CHECK(secp256k1_context_randomize(ctx, secp256k1_rand_bits(1) ? run32 : NULL));
5556+
unsigned char rand32[32];
5557+
secp256k1_rand256(rand32);
5558+
CHECK(secp256k1_context_randomize(ctx, secp256k1_rand_bits(1) ? rand32 : NULL));
55945559
}
55955560

55965561
run_rand_bits();
@@ -5678,8 +5643,7 @@ int main(int argc, char **argv) {
56785643

56795644
run_cmov_tests();
56805645

5681-
secp256k1_rand256(run32);
5682-
printf("random run = %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x\n", run32[0], run32[1], run32[2], run32[3], run32[4], run32[5], run32[6], run32[7], run32[8], run32[9], run32[10], run32[11], run32[12], run32[13], run32[14], run32[15]);
5646+
secp256k1_rand_finish();
56835647

56845648
/* shutdown */
56855649
secp256k1_context_destroy(ctx);

0 commit comments

Comments
 (0)