2 * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (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>
12 #include "prov/rand_pool.h"
13 #include "crypto/rand.h"
14 #include "prov/seeding.h"
16 #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_WIN32)
18 # ifndef OPENSSL_RAND_SEED_OS
19 # error "Unsupported seeding method configured; must be os"
23 /* On Windows Vista or higher use BCrypt instead of the legacy CryptoAPI */
24 # if defined(_MSC_VER) && _MSC_VER > 1500 /* 1500 = Visual Studio 2008 */ \
25 && defined(_WIN32_WINNT) && _WIN32_WINNT >= 0x0600
26 # define USE_BCRYPTGENRANDOM
29 # ifdef USE_BCRYPTGENRANDOM
31 # pragma comment(lib, "bcrypt.lib")
32 # ifndef STATUS_SUCCESS
33 # define STATUS_SUCCESS ((NTSTATUS)0x00000000L)
36 # include <wincrypt.h>
38 * Intel hardware RNG CSP -- available from
39 * http://developer.intel.com/design/security/rng/redist_license.htm
41 # define PROV_INTEL_SEC 22
42 # define INTEL_DEF_PROV L"Intel Hardware Cryptographic Service Provider"
45 size_t prov_pool_acquire_entropy(RAND_POOL *pool)
47 # ifndef USE_BCRYPTGENRANDOM
50 unsigned char *buffer;
52 size_t entropy_available = 0;
55 # ifdef OPENSSL_RAND_SEED_RDTSC
56 entropy_available = rand_acquire_entropy_from_tsc(pool);
57 if (entropy_available > 0)
58 return entropy_available;
61 # ifdef OPENSSL_RAND_SEED_RDCPU
62 entropy_available = rand_acquire_entropy_from_cpu(pool);
63 if (entropy_available > 0)
64 return entropy_available;
67 # ifdef USE_BCRYPTGENRANDOM
68 bytes_needed = rand_pool_bytes_needed(pool, 1 /*entropy_factor*/);
69 buffer = rand_pool_add_begin(pool, bytes_needed);
72 if (BCryptGenRandom(NULL, buffer, bytes_needed,
73 BCRYPT_USE_SYSTEM_PREFERRED_RNG) == STATUS_SUCCESS)
76 rand_pool_add_end(pool, bytes, 8 * bytes);
77 entropy_available = rand_pool_entropy_available(pool);
79 if (entropy_available > 0)
80 return entropy_available;
82 bytes_needed = rand_pool_bytes_needed(pool, 1 /*entropy_factor*/);
83 buffer = rand_pool_add_begin(pool, bytes_needed);
86 /* poll the CryptoAPI PRNG */
87 if (CryptAcquireContextW(&hProvider, NULL, NULL, PROV_RSA_FULL,
88 CRYPT_VERIFYCONTEXT | CRYPT_SILENT) != 0) {
89 if (CryptGenRandom(hProvider, bytes_needed, buffer) != 0)
92 CryptReleaseContext(hProvider, 0);
95 rand_pool_add_end(pool, bytes, 8 * bytes);
96 entropy_available = rand_pool_entropy_available(pool);
98 if (entropy_available > 0)
99 return entropy_available;
101 bytes_needed = rand_pool_bytes_needed(pool, 1 /*entropy_factor*/);
102 buffer = rand_pool_add_begin(pool, bytes_needed);
103 if (buffer != NULL) {
105 /* poll the Pentium PRG with CryptoAPI */
106 if (CryptAcquireContextW(&hProvider, NULL,
107 INTEL_DEF_PROV, PROV_INTEL_SEC,
108 CRYPT_VERIFYCONTEXT | CRYPT_SILENT) != 0) {
109 if (CryptGenRandom(hProvider, bytes_needed, buffer) != 0)
110 bytes = bytes_needed;
112 CryptReleaseContext(hProvider, 0);
114 rand_pool_add_end(pool, bytes, 8 * bytes);
115 entropy_available = rand_pool_entropy_available(pool);
117 if (entropy_available > 0)
118 return entropy_available;
121 return rand_pool_entropy_available(pool);
125 int prov_pool_add_nonce_data(RAND_POOL *pool)
133 /* Erase the entire structure including any padding */
134 memset(&data, 0, sizeof(data));
137 * Add process id, thread id, and a high resolution timestamp to
138 * ensure that the nonce is unique with high probability for
139 * different process instances.
141 data.pid = GetCurrentProcessId();
142 data.tid = GetCurrentThreadId();
143 GetSystemTimeAsFileTime(&data.time);
145 return rand_pool_add(pool, (unsigned char *)&data, sizeof(data), 0);
148 int rand_pool_add_additional_data(RAND_POOL *pool)
155 /* Erase the entire structure including any padding */
156 memset(&data, 0, sizeof(data));
159 * Add some noise from the thread id and a high resolution timer.
160 * The thread id adds a little randomness if the drbg is accessed
161 * concurrently (which is the case for the <master> drbg).
163 data.tid = GetCurrentThreadId();
164 QueryPerformanceCounter(&data.time);
165 return rand_pool_add(pool, (unsigned char *)&data, sizeof(data), 0);
168 int rand_pool_init(void)
173 void rand_pool_cleanup(void)
177 void rand_pool_keep_random_devices_open(int keep)