Skip to content

Impropper initialization of RNG on Windows #8

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions cryptcommon/ZrtpRandom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/

#include <fcntl.h>
#include <time.h>

#include <cryptcommon/ZrtpRandom.h>
#include <cryptcommon/aescpp.h>
Expand Down Expand Up @@ -128,6 +129,29 @@ void ZrtpRandom::initialize() {

sha512_begin(&mainCtx);
initialized = true;

// Use the processor time consumed by the program and the
// current time for additional entropy
clock_t clock1 = clock();
time_t time1 = time(NULL);
sha512_hash((unsigned char*)&clock1, sizeof(clock1), &mainCtx);
sha512_hash((unsigned char*)&time1, sizeof(time1), &mainCtx);
#if defined(_WIN32) || defined(_WIN64)
// On Windows the /dev/urandom is not used so additional
// entropy has to be gathered from timers. The run time
// of this loop should be not deterministic, because
// it depends on CPU frequency, cache status, context
// switching speed.
// It runs under 1 second.
clock_t clock2;
time_t time2;
do {
clock2 = clock();
time2 = time(NULL);
sha512_hash((unsigned char*)&clock2, sizeof(clock2), &mainCtx);
sha512_hash((unsigned char*)&time2, sizeof(time2), &mainCtx);
} while (clock1 == clock1 && time1 == time2);
#endif
}

/*
Expand All @@ -148,6 +172,8 @@ size_t ZrtpRandom::getSystemSeed(uint8_t *seed, size_t length)
}
else
return num;
#else
#error This random number generator can not be used on Windows platform without seeding!
#endif
return num;
}
Expand Down