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>
13 #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_WIN32)
15 # ifndef OPENSSL_RAND_SEED_OS
16 # error "Unsupported seeding method configured; must be os"
20 /* On Windows 7 or higher use BCrypt instead of the legacy CryptoAPI */
21 # if defined(_MSC_VER) && defined(_WIN32_WINNT) && _WIN32_WINNT >= 0x0601
22 # define USE_BCRYPTGENRANDOM
25 # ifdef USE_BCRYPTGENRANDOM
27 # pragma comment(lib, "bcrypt.lib")
28 # ifndef STATUS_SUCCESS
29 # define STATUS_SUCCESS ((NTSTATUS)0x00000000L)
32 # include <wincrypt.h>
34 * Intel hardware RNG CSP -- available from
35 * http://developer.intel.com/design/security/rng/redist_license.htm
37 # define PROV_INTEL_SEC 22
38 # define INTEL_DEF_PROV L"Intel Hardware Cryptographic Service Provider"
41 size_t RAND_POOL_acquire_entropy(RAND_POOL *pool)
43 # ifndef USE_BCRYPTGENRANDOM
46 unsigned char *buffer;
48 size_t entropy_available = 0;
51 # ifdef OPENSSL_RAND_SEED_RDTSC
52 entropy_available = rand_acquire_entropy_from_tsc(pool);
53 if (entropy_available > 0)
54 return entropy_available;
57 # ifdef OPENSSL_RAND_SEED_RDCPU
58 entropy_available = rand_acquire_entropy_from_cpu(pool);
59 if (entropy_available > 0)
60 return entropy_available;
63 # ifdef USE_BCRYPTGENRANDOM
64 bytes_needed = RAND_POOL_bytes_needed(pool, 8 /*entropy_per_byte*/);
65 buffer = RAND_POOL_add_begin(pool, bytes_needed);
68 if (BCryptGenRandom(NULL, buffer, bytes_needed,
69 BCRYPT_USE_SYSTEM_PREFERRED_RNG) == STATUS_SUCCESS)
72 entropy_available = RAND_POOL_add_end(pool, bytes, 8 * bytes);
74 if (entropy_available > 0)
75 return entropy_available;
77 bytes_needed = RAND_POOL_bytes_needed(pool, 8 /*entropy_per_byte*/);
78 buffer = RAND_POOL_add_begin(pool, bytes_needed);
81 /* poll the CryptoAPI PRNG */
82 if (CryptAcquireContextW(&hProvider, NULL, NULL, PROV_RSA_FULL,
83 CRYPT_VERIFYCONTEXT | CRYPT_SILENT) != 0) {
84 if (CryptGenRandom(hProvider, bytes_needed, buffer) != 0)
87 CryptReleaseContext(hProvider, 0);
90 entropy_available = RAND_POOL_add_end(pool, bytes, 8 * bytes);
92 if (entropy_available > 0)
93 return entropy_available;
95 bytes_needed = RAND_POOL_bytes_needed(pool, 8 /*entropy_per_byte*/);
96 buffer = RAND_POOL_add_begin(pool, bytes_needed);
99 /* poll the Pentium PRG with CryptoAPI */
100 if (CryptAcquireContextW(&hProvider, NULL,
101 INTEL_DEF_PROV, PROV_INTEL_SEC,
102 CRYPT_VERIFYCONTEXT | CRYPT_SILENT) != 0) {
103 if (CryptGenRandom(hProvider, bytes_needed, buffer) != 0)
104 bytes = bytes_needed;
106 CryptReleaseContext(hProvider, 0);
108 entropy_available = RAND_POOL_add_end(pool, bytes, 8 * bytes);
110 if (entropy_available > 0)
111 return entropy_available;
114 return RAND_POOL_entropy_available(pool);
117 # if OPENSSL_API_COMPAT < 0x10100000L
118 int RAND_event(UINT iMsg, WPARAM wParam, LPARAM lParam)
121 return RAND_status();
124 void RAND_screen(void)