| 1 |
// Copyright (c) 2014, Google Inc. |
| 2 |
// SPDX-License-Identifier: ISC |
| 3 |
|
| 4 |
#include <openssl/rand.h> |
| 5 |
|
| 6 |
#include "internal.h" |
| 7 |
#include "../internal.h" |
| 8 |
|
| 9 |
#if defined(OPENSSL_RAND_WINDOWS) |
| 10 |
|
| 11 |
#include <limits.h> |
| 12 |
#include <stdlib.h> |
| 13 |
|
| 14 |
OPENSSL_MSVC_PRAGMA(warning(push, 3)) |
| 15 |
|
| 16 |
#include <windows.h> |
| 17 |
|
| 18 |
// ProcessPrng (from `bcryptprimitives.dll`) is only available on Windows 8+. |
| 19 |
#if !defined(__MINGW32__) && defined(_WIN32_WINNT) && _WIN32_WINNT <= _WIN32_WINNT_WIN7 |
| 20 |
#define AWSLC_WINDOWS_7_COMPAT |
| 21 |
#endif |
| 22 |
|
| 23 |
#if defined(AWSLC_WINDOWS_7_COMPAT) || \ |
| 24 |
(WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_APP) && \ |
| 25 |
!WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)) |
| 26 |
#include <bcrypt.h> |
| 27 |
OPENSSL_MSVC_PRAGMA(comment(lib, "bcrypt.lib")) |
| 28 |
#endif // AWSLC_WINDOWS_7_COMPAT || (WINAPI_PARTITION_APP && !WINAPI_PARTITION_DESKTOP) |
| 29 |
|
| 30 |
OPENSSL_MSVC_PRAGMA(warning(pop)) |
| 31 |
|
| 32 |
#if defined(AWSLC_WINDOWS_7_COMPAT) || \ |
| 33 |
(WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_APP) && \ |
| 34 |
!WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)) |
| 35 |
|
| 36 |
void CRYPTO_sysrand(uint8_t *out, size_t requested) { |
| 37 |
while (requested > 0) { |
| 38 |
ULONG output_bytes_this_pass = ULONG_MAX; |
| 39 |
if (requested < output_bytes_this_pass) { |
| 40 |
output_bytes_this_pass = (ULONG)requested; |
| 41 |
} |
| 42 |
if (!BCRYPT_SUCCESS(BCryptGenRandom( |
| 43 |
/*hAlgorithm=*/NULL, out, output_bytes_this_pass, |
| 44 |
BCRYPT_USE_SYSTEM_PREFERRED_RNG))) { |
| 45 |
abort(); |
| 46 |
} |
| 47 |
requested -= output_bytes_this_pass; |
| 48 |
out += output_bytes_this_pass; |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
#else |
| 53 |
|
| 54 |
// See: https://learn.microsoft.com/en-us/windows/win32/seccng/processprng |
| 55 |
typedef BOOL (WINAPI *ProcessPrngFunction)(PBYTE pbData, SIZE_T cbData); |
| 56 |
static ProcessPrngFunction g_processprng_fn = NULL; |
| 57 |
static CRYPTO_once_t once = CRYPTO_ONCE_INIT; |
| 58 |
|
| 59 |
static void init_processprng(void) { |
| 60 |
HMODULE hmod = LoadLibraryW(L"bcryptprimitives"); |
| 61 |
if (hmod == NULL) { |
| 62 |
abort(); |
| 63 |
} |
| 64 |
g_processprng_fn = (ProcessPrngFunction)(void(*)(void))GetProcAddress(hmod, "ProcessPrng"); |
| 65 |
if (g_processprng_fn == NULL) { |
| 66 |
abort(); |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
void CRYPTO_sysrand(uint8_t *out, size_t requested) { |
| 71 |
|
| 72 |
CRYPTO_once(&once, init_processprng); |
| 73 |
|
| 74 |
// On non-UWP configurations, use ProcessPrng instead of BCryptGenRandom |
| 75 |
// to avoid accessing resources that may be unavailable inside the |
| 76 |
// Chromium sandbox. See https://crbug.com/74242 |
| 77 |
if (!g_processprng_fn(out, requested)) { |
| 78 |
abort(); |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
#endif // AWSLC_WINDOWS_7_COMPAT || (WINAPI_PARTITION_APP && !WINAPI_PARTITION_DESKTOP) |
| 83 |
#endif // OPENSSL_RAND_WINDOWS |