From 11985f8c59cccc0364bde7dd314e246ea53cff90 Mon Sep 17 00:00:00 2001 From: Garrett D'Amore Date: Wed, 1 Jan 2020 17:57:12 -0800 Subject: fixes #1083 Random number improvements --- src/platform/windows/win_rand.c | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) (limited to 'src/platform/windows') diff --git a/src/platform/windows/win_rand.c b/src/platform/windows/win_rand.c index afe703ef..21568a81 100644 --- a/src/platform/windows/win_rand.c +++ b/src/platform/windows/win_rand.c @@ -1,5 +1,5 @@ // -// Copyright 2017 Garrett D'Amore +// Copyright 2020 Staysail Systems, Inc. // // This software is supplied under the terms of the MIT License, a // copy of which should be located in the distribution where this @@ -11,22 +11,24 @@ #ifdef NNG_PLATFORM_WINDOWS +#ifndef _CRT_RAND_S +#define _CRT_RAND_S +#endif + #include -void -nni_plat_seed_prng(void *buf, size_t bufsz) +uint32_t +nni_random(void) { unsigned val; - // The rand_s routine uses RtlGenRandom to get high quality - // pseudo random numbers (i.e. numbers that should be good enough - // for use with crypto keying.) - while (bufsz > sizeof(val)) { - rand_s(&val); - memcpy(buf, &val, sizeof(val)); - buf = (((char *) buf) + sizeof(val)); - bufsz -= sizeof(val); - } + // rand_s is claimed by Microsoft to generate cryptographically + // secure numbers. It also is claimed that this will only fail + // for EINVAL if val is NULL (not the case here). Other error + // conditions might be possible, but we have no way to tell. + // For now we just ignore that possibility. + rand_s(&val); + return ((uint32_t)val); } -#endif // NNG_PLATFORM_WINDOWS +#endif // NNG_PLATFORM_WINDOWS \ No newline at end of file -- cgit v1.2.3-70-g09d2