2 * Copyright 1995-2018 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 "rand_local.h"
13 #include "crypto/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 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
)
131 /* Erase the entire structure including any padding */
132 memset(&data
, 0, sizeof(data
));
135 * Add process id, thread id, and a high resolution timestamp to
136 * ensure that the nonce is unique whith high probability for
137 * different process instances.
139 data
.pid
= GetCurrentProcessId();
140 data
.tid
= GetCurrentThreadId();
141 GetSystemTimeAsFileTime(&data
.time
);
143 return rand_pool_add(pool
, (unsigned char *)&data
, sizeof(data
), 0);
146 int rand_pool_add_additional_data(RAND_POOL
*pool
)
153 /* Erase the entire structure including any padding */
154 memset(&data
, 0, sizeof(data
));
157 * Add some noise from the thread id and a high resolution timer.
158 * The thread id adds a little randomness if the drbg is accessed
159 * concurrently (which is the case for the <master> drbg).
161 data
.tid
= GetCurrentThreadId();
162 QueryPerformanceCounter(&data
.time
);
163 return rand_pool_add(pool
, (unsigned char *)&data
, sizeof(data
), 0);
166 # if !OPENSSL_API_1_1_0 && !defined(FIPS_MODE)
167 int RAND_event(UINT iMsg
, WPARAM wParam
, LPARAM lParam
)
170 return RAND_status();
173 void RAND_screen(void)
179 int rand_pool_init(void)
184 void rand_pool_cleanup(void)
188 void rand_pool_keep_random_devices_open(int keep
)