|
|
|
@ -55,9 +55,33 @@ |
|
|
|
* uECC_platform_specific.c -- Implementation of platform specific functions |
|
|
|
* uECC_platform_specific.c -- Implementation of platform specific functions |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include <stdint.h> |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#if defined(_WIN32) |
|
|
|
|
|
|
|
/* Windows implementation using CryptGenRandom */ |
|
|
|
|
|
|
|
#include <windows.h> |
|
|
|
|
|
|
|
#include <wincrypt.h> |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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__) || \ |
|
|
|
#elif defined(unix) || defined(__linux__) || defined(__unix__) || \ |
|
|
|
defined(__unix) | (defined(__APPLE__) && defined(__MACH__)) || \
|
|
|
|
defined(__unix) || (defined(__APPLE__) && defined(__MACH__)) || \
|
|
|
|
defined(uECC_POSIX) |
|
|
|
defined(uECC_POSIX) |
|
|
|
|
|
|
|
|
|
|
|
/* Some POSIX-like system with /dev/urandom or /dev/random. */ |
|
|
|
/* 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; |
|
|
|
return 1; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
#elif defined(_WIN32) |
|
|
|
|
|
|
|
/* Windows implementation using CryptGenRandom */ |
|
|
|
|
|
|
|
#include <windows.h> |
|
|
|
|
|
|
|
#include <wincrypt.h> |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
|
|
|
#else |
|
|
|
/* No CSPRNG available - return error */ |
|
|
|
/* No CSPRNG available - return error */ |
|
|
|
int default_CSPRNG(uint8_t *dest, unsigned int size) { |
|
|
|
int default_CSPRNG(uint8_t *dest, unsigned int size) { |
|
|
|
|