Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improvements to random seed in src/tests.c #516

Merged
merged 3 commits into from
Feb 21, 2019
Merged

Conversation

hdon
Copy link
Contributor

@hdon hdon commented Mar 6, 2018

I've made two small changes to src/tests.c circa random seed generation.

Added a warning when /dev/urandom fails, mostly to defend against the case that someone should use the code verbatim, but also to enhance its illustrative power.

Also I fixed a bug with how the return value of fread() was being evaluated. In fact, /dev/urandom was never being applied before as the check on the return value of fread() always failed!

hdon added 2 commits March 5, 2018 18:43
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
the two middle arguments to fread() are easily confused, and cause the
checking of return value to fail incorrectly (and possibly succeed
incorrectly.)
@hdon hdon changed the title Address some issues with src/tests.c improvements to random seed in src/tests.c Mar 6, 2018
@sipa
Copy link
Contributor

sipa commented Mar 25, 2018

utACK 8b3841c

@hdon
Copy link
Contributor Author

hdon commented Apr 8, 2018

bump

src/tests.c Outdated
@@ -4918,7 +4918,8 @@ 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");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

src/tests.c: In function ‘main’:
src/tests.c:4923:13: warning: ISO C90 forbids mixed declarations and code [-Wdeclaration-after-statement]
uint64_t t = time(NULL) * (uint64_t)1337;

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hdon to fix this, move the fprint down a line. Then we should be good to merge.

Reported-by: Jonas Nick <jonasd.nick@gmail.com>
@gmaxwell
Copy link
Contributor

@sipa @jonasnick @real-or-random @hdon please at least utACK my trivial fixup commit so I can merge. :P

@real-or-random
Copy link
Contributor

utACK

@gmaxwell gmaxwell merged commit be40c4d into bitcoin-core:master Feb 21, 2019
gmaxwell added a commit that referenced this pull request Feb 21, 2019
be40c4d Fixup for C90 mixed declarations. (Gregory Maxwell)
8b3841c fix bug in fread() failure check (Don Viszneki)
cddef0c tests: add warning message when /dev/urandom fails (Don Viszneki)

Pull request description:

  I've made two small changes to `src/tests.c` circa random seed generation.

  Added a warning when `/dev/urandom` fails, mostly to defend against the case that someone should use the code verbatim, but also to enhance its illustrative power.

  Also I fixed a bug with how the return value of `fread()` was being evaluated. In fact, `/dev/urandom` was never being applied before as the check on the return value of `fread()` always failed!

Tree-SHA512: 239dbe8316220c2f0e5b370bf9a18f78196e96cc4a7edea58cf2521b2c9cbc8da065be96aa859f90324d57e388d30f7670ce6bc1cca52e5162e5ca66b1a55b34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants