From 7fa2d49b16b261e5ab2a39f70d65abdeb1eef76e Mon Sep 17 00:00:00 2001 From: Fedor Brunner Date: Mon, 16 Sep 2013 10:45:33 +0200 Subject: [PATCH 1/2] On Windows platform when using GNU ZRTP library in standalone, without OpenSSL, the integrated random number generator is not initializated with enought entropy. This code will add entropy using the system timers https://en.wikipedia.org/wiki/Clock_drift#Random_number_generators --- cryptcommon/ZrtpRandom.cpp | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/cryptcommon/ZrtpRandom.cpp b/cryptcommon/ZrtpRandom.cpp index 7078b72e..aad10c73 100644 --- a/cryptcommon/ZrtpRandom.cpp +++ b/cryptcommon/ZrtpRandom.cpp @@ -16,6 +16,7 @@ */ #include +#include #include #include @@ -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 } /* @@ -148,6 +172,13 @@ size_t ZrtpRandom::getSystemSeed(uint8_t *seed, size_t length) } else return num; +#else + clock_t c = clock(); + if (length > sizeof(c)) + { + memcpy(seed, &c, sizeof(c)); + num = sizeof(c); + } #endif return num; } From 544b77128e2cd326ed7d02bb56d0aac2dd720ece Mon Sep 17 00:00:00 2001 From: Fedor Brunner Date: Fri, 20 Sep 2013 16:24:45 +0200 Subject: [PATCH 2/2] The build in random number generator is not properly seeded on the Windows platform. Throw an compiler error message for Windows platform. --- cryptcommon/ZrtpRandom.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/cryptcommon/ZrtpRandom.cpp b/cryptcommon/ZrtpRandom.cpp index 7078b72e..a657ed5a 100644 --- a/cryptcommon/ZrtpRandom.cpp +++ b/cryptcommon/ZrtpRandom.cpp @@ -148,6 +148,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; }