2 * Copyright 1995-2019 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>
13 #include "internal/rand_int.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 Vista or higher use BCrypt instead of the legacy CryptoAPI */
22 # if defined(_MSC_VER) && _MSC_VER > 1500 /* 1500 = Visual Studio 2008 */ \
23 && defined(_WIN32_WINNT) && _WIN32_WINNT >= 0x0600
24 # define USE_BCRYPTGENRANDOM
27 # ifdef USE_BCRYPTGENRANDOM
29 # pragma comment(lib, "bcrypt.lib")
30 # ifndef STATUS_SUCCESS
31 # define STATUS_SUCCESS ((NTSTATUS)0x00000000L)
34 # include <wincrypt.h>
36 * Intel hardware RNG CSP -- available from
37 * http://developer.intel.com/design/security/rng/redist_license.htm
39 # define PROV_INTEL_SEC 22
40 # define INTEL_DEF_PROV L"Intel Hardware Cryptographic Service Provider"
43 size_t rand_pool_acquire_entropy(RAND_POOL *pool)
45 # ifndef USE_BCRYPTGENRANDOM
48 unsigned char *buffer;
50 size_t entropy_available = 0;
53 # ifdef OPENSSL_RAND_SEED_RDTSC
54 entropy_available = rand_acquire_entropy_from_tsc(pool);
55 if (entropy_available > 0)
56 return entropy_available;
59 # ifdef OPENSSL_RAND_SEED_RDCPU
60 entropy_available = rand_acquire_entropy_from_cpu(pool);
61 if (entropy_available > 0)
62 return entropy_available;
65 # ifdef USE_BCRYPTGENRANDOM
66 bytes_needed = rand_pool_bytes_needed(pool, 1 /*entropy_factor*/);
67 buffer = rand_pool_add_begin(pool, bytes_needed);
70 if (BCryptGenRandom(NULL, buffer, bytes_needed,
71 BCRYPT_USE_SYSTEM_PREFERRED_RNG) == STATUS_SUCCESS)
74 rand_pool_add_end(pool, bytes, 8 * bytes);
75 entropy_available = rand_pool_entropy_available(pool);
77 if (entropy_available > 0)
78 return entropy_available;
80 bytes_needed = rand_pool_bytes_needed(pool, 1 /*entropy_factor*/);
81 buffer = rand_pool_add_begin(pool, bytes_needed);
84 /* poll the CryptoAPI PRNG */
85 if (CryptAcquireContextW(&hProvider, NULL, NULL, PROV_RSA_FULL,
86 CRYPT_VERIFYCONTEXT | CRYPT_SILENT) != 0) {
87 if (CryptGenRandom(hProvider, bytes_needed, buffer) != 0)
90 CryptReleaseContext(hProvider, 0);
93 rand_pool_add_end(pool, bytes, 8 * bytes);
94 entropy_available = rand_pool_entropy_available(pool);
96 if (entropy_available > 0)
97 return entropy_available;
99 bytes_needed = rand_pool_bytes_needed(pool, 1 /*entropy_factor*/);
100 buffer = rand_pool_add_begin(pool, bytes_needed);
101 if (buffer != NULL) {
103 /* poll the Pentium PRG with CryptoAPI */
104 if (CryptAcquireContextW(&hProvider, NULL,
105 INTEL_DEF_PROV, PROV_INTEL_SEC,
106 CRYPT_VERIFYCONTEXT | CRYPT_SILENT) != 0) {
107 if (CryptGenRandom(hProvider, bytes_needed, buffer) != 0)
108 bytes = bytes_needed;
110 CryptReleaseContext(hProvider, 0);
112 rand_pool_add_end(pool, bytes, 8 * bytes);
113 entropy_available = rand_pool_entropy_available(pool);
115 if (entropy_available > 0)
116 return entropy_available;
119 return rand_pool_entropy_available(pool);
123 int rand_pool_add_nonce_data(RAND_POOL *pool)
132 * Add process id, thread id, and a high resolution timestamp to
133 * ensure that the nonce is unique whith high probability for
134 * different process instances.
136 data.pid = GetCurrentProcessId();
137 data.tid = GetCurrentThreadId();
138 GetSystemTimeAsFileTime(&data.time);
140 return rand_pool_add(pool, (unsigned char *)&data, sizeof(data), 0);
143 int rand_pool_add_additional_data(RAND_POOL *pool)
151 * Add some noise from the thread id and a high resolution timer.
152 * The thread id adds a little randomness if the drbg is accessed
153 * concurrently (which is the case for the <master> drbg).
155 data.tid = GetCurrentThreadId();
156 QueryPerformanceCounter(&data.time);
157 return rand_pool_add(pool, (unsigned char *)&data, sizeof(data), 0);
160 # if OPENSSL_API_COMPAT < 0x10100000L
161 int RAND_event(UINT iMsg, WPARAM wParam, LPARAM lParam)
164 return RAND_status();
167 void RAND_screen(void)
173 int rand_pool_init(void)
178 void rand_pool_cleanup(void)
182 void rand_pool_keep_random_devices_open(int keep)