| 6 | // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 |
| 15 | #include <openssl/rand.h> |
| 16 |
| 17 | #include "../bcm_support.h" |
| 18 | #include "../internal.h" |
| 19 | #include "internal.h" |
| 20 |
| 21 | #if defined(OPENSSL_RAND_WINDOWS) |
| 22 |
| 23 | #include <limits.h> |
| 24 | #include <stdlib.h> |
| 25 |
| 26 | #include <windows.h> |
| 27 |
| 28 | #if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_APP) && \ |
| 29 | !WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP) |
| 30 | #include <bcrypt.h> |
| 31 | OPENSSL_MSVC_PRAGMA(comment(lib, "bcrypt.lib")) |
| 32 | #endif // WINAPI_PARTITION_APP && !WINAPI_PARTITION_DESKTOP |
| 33 |
| 34 | #if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_APP) && \ |
| 35 | !WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP) |
| 36 |
| 37 | void CRYPTO_init_sysrand(void) {} |
| 38 |
| 39 | void CRYPTO_sysrand(uint8_t *out, size_t requested) { |
| 40 | while (requested > 0) { |
| 41 | ULONG output_bytes_this_pass = ULONG_MAX; |
| 42 | if (requested < output_bytes_this_pass) { |
| 43 | output_bytes_this_pass = (ULONG)requested; |
| 44 | } |
| 45 | if (!BCRYPT_SUCCESS(BCryptGenRandom( |
| 46 | /*hAlgorithm=*/NULL, out, output_bytes_this_pass, |
| 47 | BCRYPT_USE_SYSTEM_PREFERRED_RNG))) { |
| 48 | abort(); |
| 49 | } |
| 50 | requested -= output_bytes_this_pass; |
| 51 | out += output_bytes_this_pass; |
| 52 | } |
| 53 | } |
| 54 |
| 55 | #else |
| 56 |
| 57 | // See: https://learn.microsoft.com/en-us/windows/win32/seccng/processprng |
| 58 | typedef BOOL (WINAPI *ProcessPrngFunction)(PBYTE pbData, SIZE_T cbData); |
| 59 | static ProcessPrngFunction g_processprng_fn = NULL; |
| 60 |
| 61 | static void init_processprng(void) { |
| 62 | HMODULE hmod = LoadLibraryW(L"bcryptprimitives"); |
| 63 | if (hmod == NULL) { |
| 64 | abort(); |
| 65 | } |
| 66 | g_processprng_fn = (ProcessPrngFunction)GetProcAddress(hmod, "ProcessPrng"); |
| 67 | if (g_processprng_fn == NULL) { |
| 68 | abort(); |
| 69 | } |
| 70 | } |
| 71 |
| 72 | void CRYPTO_init_sysrand(void) { |
| 73 | static CRYPTO_once_t once = CRYPTO_ONCE_INIT; |
| 74 | CRYPTO_once(&once, init_processprng); |
| 75 | } |
| 76 |
| 77 | void CRYPTO_sysrand(uint8_t *out, size_t requested) { |
| 78 | CRYPTO_init_sysrand(); |
| 79 | // On non-UWP configurations, use ProcessPrng instead of BCryptGenRandom |
| 80 | // to avoid accessing resources that may be unavailable inside the |
| 81 | // Chromium sandbox. See https://crbug.com/74242 |
| 82 | if (!g_processprng_fn(out, requested)) { |
| 83 | abort(); |
| 84 | } |
| 85 | } |
| 86 |
| 87 | #endif // WINAPI_PARTITION_APP && !WINAPI_PARTITION_DESKTOP |
| 88 |
| 89 | int CRYPTO_sysrand_if_available(uint8_t *buf, size_t len) { |
| 90 | CRYPTO_sysrand(buf, len); |
| 91 | return 1; |
| 92 | } |
| 93 |
| 94 | void CRYPTO_sysrand_for_seed(uint8_t *out, size_t requested) { |
| 95 | CRYPTO_sysrand(out, requested); |
| 96 | } |
| 97 |
| 98 | #endif // OPENSSL_RAND_WINDOWS |
| 99 |