2 * Copyright 1995-2018 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 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 size_t rand_pool_acquire_entropy(RAND_POOL
*pool
)
44 # ifndef USE_BCRYPTGENRANDOM
47 unsigned char *buffer
;
49 size_t entropy_available
= 0;
52 # ifdef OPENSSL_RAND_SEED_RDTSC
53 entropy_available
= rand_acquire_entropy_from_tsc(pool
);
54 if (entropy_available
> 0)
55 return entropy_available
;
58 # ifdef OPENSSL_RAND_SEED_RDCPU
59 entropy_available
= rand_acquire_entropy_from_cpu(pool
);
60 if (entropy_available
> 0)
61 return entropy_available
;
64 # ifdef USE_BCRYPTGENRANDOM
65 bytes_needed
= rand_pool_bytes_needed(pool
, 8 /*entropy_per_byte*/);
66 buffer
= rand_pool_add_begin(pool
, bytes_needed
);
69 if (BCryptGenRandom(NULL
, buffer
, bytes_needed
,
70 BCRYPT_USE_SYSTEM_PREFERRED_RNG
) == STATUS_SUCCESS
)
73 rand_pool_add_end(pool
, bytes
, 8 * bytes
);
74 entropy_available
= rand_pool_entropy_available(pool
);
76 if (entropy_available
> 0)
77 return entropy_available
;
79 bytes_needed
= rand_pool_bytes_needed(pool
, 8 /*entropy_per_byte*/);
80 buffer
= rand_pool_add_begin(pool
, bytes_needed
);
83 /* poll the CryptoAPI PRNG */
84 if (CryptAcquireContextW(&hProvider
, NULL
, NULL
, PROV_RSA_FULL
,
85 CRYPT_VERIFYCONTEXT
| CRYPT_SILENT
) != 0) {
86 if (CryptGenRandom(hProvider
, bytes_needed
, buffer
) != 0)
89 CryptReleaseContext(hProvider
, 0);
92 rand_pool_add_end(pool
, bytes
, 8 * bytes
);
93 entropy_available
= rand_pool_entropy_available(pool
);
95 if (entropy_available
> 0)
96 return entropy_available
;
98 bytes_needed
= rand_pool_bytes_needed(pool
, 8 /*entropy_per_byte*/);
99 buffer
= rand_pool_add_begin(pool
, bytes_needed
);
100 if (buffer
!= NULL
) {
102 /* poll the Pentium PRG with CryptoAPI */
103 if (CryptAcquireContextW(&hProvider
, NULL
,
104 INTEL_DEF_PROV
, PROV_INTEL_SEC
,
105 CRYPT_VERIFYCONTEXT
| CRYPT_SILENT
) != 0) {
106 if (CryptGenRandom(hProvider
, bytes_needed
, buffer
) != 0)
107 bytes
= bytes_needed
;
109 CryptReleaseContext(hProvider
, 0);
111 rand_pool_add_end(pool
, bytes
, 8 * bytes
);
112 entropy_available
= rand_pool_entropy_available(pool
);
114 if (entropy_available
> 0)
115 return entropy_available
;
118 return rand_pool_entropy_available(pool
);
122 int rand_pool_add_nonce_data(RAND_POOL
*pool
)
131 * Add process id, thread id, and a high resolution timestamp to
132 * ensure that the nonce is unique whith high probability for
133 * different process instances.
135 data
.pid
= GetCurrentProcessId();
136 data
.tid
= GetCurrentThreadId();
137 GetSystemTimeAsFileTime(&data
.time
);
139 return rand_pool_add(pool
, (unsigned char *)&data
, sizeof(data
), 0);
142 int rand_pool_add_additional_data(RAND_POOL
*pool
)
150 * Add some noise from the thread id and a high resolution timer.
151 * The thread id adds a little randomness if the drbg is accessed
152 * concurrently (which is the case for the <master> drbg).
154 data
.tid
= GetCurrentThreadId();
155 QueryPerformanceCounter(&data
.time
);
156 return rand_pool_add(pool
, (unsigned char *)&data
, sizeof(data
), 0);
159 # if OPENSSL_API_COMPAT < 0x10100000L
160 int RAND_event(UINT iMsg
, WPARAM wParam
, LPARAM lParam
)
163 return RAND_status();
166 void RAND_screen(void)