From a6a4bdd5e02f27fbd66fcd577f0e869fdbcbebc9 Mon Sep 17 00:00:00 2001 From: Ken MacKay Date: Sun, 23 Aug 2015 12:47:30 -0700 Subject: [PATCH] Default POSIX/Windows RNG should be treated as user-set RNG. The default RNG functions on POSIX and windows are secure, and should be used as if the user had set an RNG. Also, don't set an RNG at all on other platforms. --- platform-specific.inc | 8 ++------ uECC.c | 10 +++++++--- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/platform-specific.inc b/platform-specific.inc index 207ac8b..9d42534 100644 --- a/platform-specific.inc +++ b/platform-specific.inc @@ -120,6 +120,7 @@ static int default_RNG(uint8_t *dest, unsigned size) { CryptReleaseContext(prov, 0); return 1; } +#define default_RNG_defined 1 #elif defined(unix) || defined(__linux__) || defined(__unix__) || defined(__unix) || \ (defined(__APPLE__) && defined(__MACH__)) || defined(uECC_POSIX) @@ -157,12 +158,7 @@ static int default_RNG(uint8_t *dest, unsigned size) { close(fd); return 1; } - -#else /* Some other platform */ - -static int default_RNG(uint8_t *dest, unsigned size) { - return 0; -} +#define default_RNG_defined 1 #endif /* platform */ diff --git a/uECC.c b/uECC.c index e5336a0..a3bb418 100644 --- a/uECC.c +++ b/uECC.c @@ -64,7 +64,11 @@ struct uECC_Curve_t { #include "asm_arm_small.inc" #endif +#if default_RNG_defined static uECC_RNG_Function g_rng_function = &default_RNG; +#else +static uECC_RNG_Function g_rng_function = 0; +#endif void uECC_set_rng(uECC_RNG_Function rng_function) { g_rng_function = rng_function; @@ -827,7 +831,7 @@ static void vli_bytesToNative(uint64_t *native, const uint8_t *bytes, uECC_Curve static cmpresult_t generate_random_int(uECC_word_t *random, wordcount_t num_words, const wordcount_t num_bits) { - if (!g_rng_function((uint8_t *)random, num_words * uECC_WORD_SIZE)) { + if (!g_rng_function || !g_rng_function((uint8_t *)random, num_words * uECC_WORD_SIZE)) { return 0; } if (num_words * uECC_WORD_SIZE * 8 > num_bits) { @@ -887,7 +891,7 @@ int uECC_shared_secret(const uint8_t *public_key, /* If an RNG function was specified, try to get a random initial Z value to improve protection against side-channel attacks. */ - if (g_rng_function != &default_RNG) { + if (g_rng_function) { for (tries = 0; tries < MAX_TRIES; ++tries) { if (!generate_random_int(p2[carry], curve->num_words, curve->num_bytes * 8)) { return 0; @@ -999,7 +1003,7 @@ static int uECC_sign_with_k(const uint8_t *private_key, } /* Attempt to get a random number to prevent side channel analysis of k. */ - if (g_rng_function == &default_RNG) { + if (!g_rng_function) { vli_clear(tmp, curve->num_n_words); tmp[0] = 1; } else {