From cddef0c0be1c30f45ea525af7a42b54d1a78786c Mon Sep 17 00:00:00 2001 From: Don Viszneki Date: Mon, 5 Mar 2018 18:43:31 -0800 Subject: [PATCH 1/3] tests: add warning message when /dev/urandom fails in case this code should ever be used as an example, a warning is a nice way of helping ensure insecure keys are not generated --- src/tests.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/tests.c b/src/tests.c index 893d297..0531457 100644 --- a/src/tests.c +++ b/src/tests.c @@ -4919,6 +4919,7 @@ int main(int argc, char **argv) { } else { FILE *frand = fopen("/dev/urandom", "r"); if ((frand == NULL) || fread(&seed16, sizeof(seed16), 1, frand) != sizeof(seed16)) { + fprintf(stderr, "WARNING: could not read 16 bytes from /dev/urandom; falling back to insecure PRNG\n"); uint64_t t = time(NULL) * (uint64_t)1337; seed16[0] ^= t; seed16[1] ^= t >> 8; From 8b3841c91daf7b75ed1bf81fa07db69d1688e8b6 Mon Sep 17 00:00:00 2001 From: Don Viszneki Date: Mon, 5 Mar 2018 18:45:00 -0800 Subject: [PATCH 2/3] fix bug in fread() failure check the two middle arguments to fread() are easily confused, and cause the checking of return value to fail incorrectly (and possibly succeed incorrectly.) --- src/tests.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/tests.c b/src/tests.c index 0531457..67559ea 100644 --- a/src/tests.c +++ b/src/tests.c @@ -4918,7 +4918,7 @@ int main(int argc, char **argv) { } } else { FILE *frand = fopen("/dev/urandom", "r"); - if ((frand == NULL) || fread(&seed16, sizeof(seed16), 1, frand) != sizeof(seed16)) { + if ((frand == NULL) || fread(&seed16, 1, sizeof(seed16), frand) != sizeof(seed16)) { fprintf(stderr, "WARNING: could not read 16 bytes from /dev/urandom; falling back to insecure PRNG\n"); uint64_t t = time(NULL) * (uint64_t)1337; seed16[0] ^= t; From be40c4d0b5ee394e23a1bb24cb38d8d34fedd946 Mon Sep 17 00:00:00 2001 From: Gregory Maxwell Date: Thu, 21 Feb 2019 05:00:29 +0000 Subject: [PATCH 3/3] Fixup for C90 mixed declarations. Reported-by: Jonas Nick --- src/tests.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/tests.c b/src/tests.c index 67559ea..d48aa07 100644 --- a/src/tests.c +++ b/src/tests.c @@ -4919,8 +4919,8 @@ int main(int argc, char **argv) { } else { FILE *frand = fopen("/dev/urandom", "r"); if ((frand == NULL) || fread(&seed16, 1, sizeof(seed16), frand) != sizeof(seed16)) { - fprintf(stderr, "WARNING: could not read 16 bytes from /dev/urandom; falling back to insecure PRNG\n"); uint64_t t = time(NULL) * (uint64_t)1337; + fprintf(stderr, "WARNING: could not read 16 bytes from /dev/urandom; falling back to insecure PRNG\n"); seed16[0] ^= t; seed16[1] ^= t >> 8; seed16[2] ^= t >> 16;