2 * Copyright 1995-2017 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
10 #include "internal/cryptlib.h"
11 #include <openssl/rand.h>
14 #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_WIN32)
16 # ifndef OPENSSL_RAND_SEED_OS
17 # error "Unsupported seeding method configured; must be os"
21 /* On Windows 7 or higher use BCrypt instead of the legacy CryptoAPI */
22 # if defined(_MSC_VER) && defined(_WIN32_WINNT) && _WIN32_WINNT >= 0x0601
23 # define USE_BCRYPTGENRANDOM
26 # ifdef USE_BCRYPTGENRANDOM
28 # pragma comment(lib, "bcrypt.lib")
29 # ifndef STATUS_SUCCESS
30 # define STATUS_SUCCESS ((NTSTATUS)0x00000000L)
33 # include <wincrypt.h>
35 * Intel hardware RNG CSP -- available from
36 * http://developer.intel.com/design/security/rng/redist_license.htm
38 # define PROV_INTEL_SEC 22
39 # define INTEL_DEF_PROV L"Intel Hardware Cryptographic Service Provider"
42 int RAND_poll_ex(RAND_poll_cb rand_add, void *arg)
44 # ifndef USE_BCRYPTGENRANDOM
48 BYTE buf[RANDOMNESS_NEEDED];
50 # ifdef OPENSSL_RAND_SEED_RDTSC
51 rand_read_tsc(cb, arg);
53 # ifdef OPENSSL_RAND_SEED_RDCPU
54 if (rand_read_cpu(cb, arg))
58 # ifdef USE_BCRYPTGENRANDOM
59 if (BCryptGenRandom(NULL, buf, (ULONG)sizeof(buf),
60 BCRYPT_USE_SYSTEM_PREFERRED_RNG) == STATUS_SUCCESS) {
61 rand_add(arg, buf, sizeof(buf), sizeof(buf));
65 /* poll the CryptoAPI PRNG */
66 if (CryptAcquireContextW(&hProvider, NULL, NULL, PROV_RSA_FULL,
67 CRYPT_VERIFYCONTEXT | CRYPT_SILENT) != 0) {
68 if (CryptGenRandom(hProvider, (DWORD)sizeof(buf), buf) != 0) {
69 rand_add(arg, buf, sizeof(buf), sizeof(buf));
72 CryptReleaseContext(hProvider, 0);
77 /* poll the Pentium PRG with CryptoAPI */
78 if (CryptAcquireContextW(&hProvider, NULL, INTEL_DEF_PROV, PROV_INTEL_SEC,
79 CRYPT_VERIFYCONTEXT | CRYPT_SILENT) != 0) {
80 if (CryptGenRandom(hProvider, (DWORD)sizeof(buf), buf) != 0) {
81 rand_add(arg, buf, sizeof(buf), sizeof(buf));
84 CryptReleaseContext(hProvider, 0);
93 # if OPENSSL_API_COMPAT < 0x10100000L
94 int RAND_event(UINT iMsg, WPARAM wParam, LPARAM lParam)
100 void RAND_screen(void)