diff --git a/tinycrypt/lib/source/ecc_platform_specific.c b/tinycrypt/lib/source/ecc_platform_specific.c index 97d2d201..93b7edbf 100644 --- a/tinycrypt/lib/source/ecc_platform_specific.c +++ b/tinycrypt/lib/source/ecc_platform_specific.c @@ -55,9 +55,33 @@ * uECC_platform_specific.c -- Implementation of platform specific functions */ +#include + +#if defined(_WIN32) +/* Windows implementation using CryptGenRandom */ +#include +#include + +int default_CSPRNG(uint8_t *dest, unsigned int size) { + /* input sanity check: */ + if (dest == (uint8_t *) 0 || (size <= 0)) + return 0; + + HCRYPTPROV hProvider = 0; + + if (!CryptAcquireContext(&hProvider, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT | CRYPT_SILENT)) { + return 0; + } + + BOOL result = CryptGenRandom(hProvider, size, dest); + + CryptReleaseContext(hProvider, 0); + + return result ? 1 : 0; +} -#if defined(unix) || defined(__linux__) || defined(__unix__) || \ - defined(__unix) | (defined(__APPLE__) && defined(__MACH__)) || \ +#elif defined(unix) || defined(__linux__) || defined(__unix__) || \ + defined(__unix) || (defined(__APPLE__) && defined(__MACH__)) || \ defined(uECC_POSIX) /* Some POSIX-like system with /dev/urandom or /dev/random. */ @@ -101,29 +125,6 @@ int default_CSPRNG(uint8_t *dest, unsigned int size) { return 1; } -#elif defined(_WIN32) -/* Windows implementation using CryptGenRandom */ -#include -#include - -int default_CSPRNG(uint8_t *dest, unsigned int size) { - /* input sanity check: */ - if (dest == (uint8_t *) 0 || (size <= 0)) - return 0; - - HCRYPTPROV hProvider = 0; - - if (!CryptAcquireContext(&hProvider, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT | CRYPT_SILENT)) { - return 0; - } - - BOOL result = CryptGenRandom(hProvider, size, dest); - - CryptReleaseContext(hProvider, 0); - - return result ? 1 : 0; -} - #else /* No CSPRNG available - return error */ int default_CSPRNG(uint8_t *dest, unsigned int size) {