yuezonghe | 824eb0c | 2024-06-27 02:32:26 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 1995-2019 The OpenSSL Project Authors. All Rights Reserved. |
| 3 | * |
| 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 |
| 8 | */ |
| 9 | |
| 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) |
| 15 | |
| 16 | # ifndef OPENSSL_RAND_SEED_OS |
| 17 | # error "Unsupported seeding method configured; must be os" |
| 18 | # endif |
| 19 | |
| 20 | # include <windows.h> |
| 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 |
| 25 | # endif |
| 26 | |
| 27 | # ifdef USE_BCRYPTGENRANDOM |
| 28 | # include <bcrypt.h> |
| 29 | # pragma comment(lib, "bcrypt.lib") |
| 30 | # ifndef STATUS_SUCCESS |
| 31 | # define STATUS_SUCCESS ((NTSTATUS)0x00000000L) |
| 32 | # endif |
| 33 | # else |
| 34 | # include <wincrypt.h> |
| 35 | /* |
| 36 | * Intel hardware RNG CSP -- available from |
| 37 | * http://developer.intel.com/design/security/rng/redist_license.htm |
| 38 | */ |
| 39 | # define PROV_INTEL_SEC 22 |
| 40 | # define INTEL_DEF_PROV L"Intel Hardware Cryptographic Service Provider" |
| 41 | # endif |
| 42 | |
| 43 | size_t rand_pool_acquire_entropy(RAND_POOL *pool) |
| 44 | { |
| 45 | # ifndef USE_BCRYPTGENRANDOM |
| 46 | HCRYPTPROV hProvider; |
| 47 | # endif |
| 48 | unsigned char *buffer; |
| 49 | size_t bytes_needed; |
| 50 | size_t entropy_available = 0; |
| 51 | |
| 52 | |
| 53 | # ifdef OPENSSL_RAND_SEED_RDTSC |
| 54 | entropy_available = rand_acquire_entropy_from_tsc(pool); |
| 55 | if (entropy_available > 0) |
| 56 | return entropy_available; |
| 57 | # endif |
| 58 | |
| 59 | # ifdef OPENSSL_RAND_SEED_RDCPU |
| 60 | entropy_available = rand_acquire_entropy_from_cpu(pool); |
| 61 | if (entropy_available > 0) |
| 62 | return entropy_available; |
| 63 | # endif |
| 64 | |
| 65 | # ifdef USE_BCRYPTGENRANDOM |
| 66 | bytes_needed = rand_pool_bytes_needed(pool, 1 /*entropy_factor*/); |
| 67 | buffer = rand_pool_add_begin(pool, bytes_needed); |
| 68 | if (buffer != NULL) { |
| 69 | size_t bytes = 0; |
| 70 | if (BCryptGenRandom(NULL, buffer, bytes_needed, |
| 71 | BCRYPT_USE_SYSTEM_PREFERRED_RNG) == STATUS_SUCCESS) |
| 72 | bytes = bytes_needed; |
| 73 | |
| 74 | rand_pool_add_end(pool, bytes, 8 * bytes); |
| 75 | entropy_available = rand_pool_entropy_available(pool); |
| 76 | } |
| 77 | if (entropy_available > 0) |
| 78 | return entropy_available; |
| 79 | # else |
| 80 | bytes_needed = rand_pool_bytes_needed(pool, 1 /*entropy_factor*/); |
| 81 | buffer = rand_pool_add_begin(pool, bytes_needed); |
| 82 | if (buffer != NULL) { |
| 83 | size_t bytes = 0; |
| 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) |
| 88 | bytes = bytes_needed; |
| 89 | |
| 90 | CryptReleaseContext(hProvider, 0); |
| 91 | } |
| 92 | |
| 93 | rand_pool_add_end(pool, bytes, 8 * bytes); |
| 94 | entropy_available = rand_pool_entropy_available(pool); |
| 95 | } |
| 96 | if (entropy_available > 0) |
| 97 | return entropy_available; |
| 98 | |
| 99 | bytes_needed = rand_pool_bytes_needed(pool, 1 /*entropy_factor*/); |
| 100 | buffer = rand_pool_add_begin(pool, bytes_needed); |
| 101 | if (buffer != NULL) { |
| 102 | size_t bytes = 0; |
| 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; |
| 109 | |
| 110 | CryptReleaseContext(hProvider, 0); |
| 111 | } |
| 112 | rand_pool_add_end(pool, bytes, 8 * bytes); |
| 113 | entropy_available = rand_pool_entropy_available(pool); |
| 114 | } |
| 115 | if (entropy_available > 0) |
| 116 | return entropy_available; |
| 117 | # endif |
| 118 | |
| 119 | return rand_pool_entropy_available(pool); |
| 120 | } |
| 121 | |
| 122 | |
| 123 | int rand_pool_add_nonce_data(RAND_POOL *pool) |
| 124 | { |
| 125 | struct { |
| 126 | DWORD pid; |
| 127 | DWORD tid; |
| 128 | FILETIME time; |
| 129 | } data = { 0 }; |
| 130 | |
| 131 | /* |
| 132 | * Add process id, thread id, and a high resolution timestamp to |
| 133 | * ensure that the nonce is unique with high probability for |
| 134 | * different process instances. |
| 135 | */ |
| 136 | data.pid = GetCurrentProcessId(); |
| 137 | data.tid = GetCurrentThreadId(); |
| 138 | GetSystemTimeAsFileTime(&data.time); |
| 139 | |
| 140 | return rand_pool_add(pool, (unsigned char *)&data, sizeof(data), 0); |
| 141 | } |
| 142 | |
| 143 | int rand_pool_add_additional_data(RAND_POOL *pool) |
| 144 | { |
| 145 | struct { |
| 146 | DWORD tid; |
| 147 | LARGE_INTEGER time; |
| 148 | } data = { 0 }; |
| 149 | |
| 150 | /* |
| 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). |
| 154 | */ |
| 155 | data.tid = GetCurrentThreadId(); |
| 156 | QueryPerformanceCounter(&data.time); |
| 157 | return rand_pool_add(pool, (unsigned char *)&data, sizeof(data), 0); |
| 158 | } |
| 159 | |
| 160 | # if OPENSSL_API_COMPAT < 0x10100000L |
| 161 | int RAND_event(UINT iMsg, WPARAM wParam, LPARAM lParam) |
| 162 | { |
| 163 | RAND_poll(); |
| 164 | return RAND_status(); |
| 165 | } |
| 166 | |
| 167 | void RAND_screen(void) |
| 168 | { |
| 169 | RAND_poll(); |
| 170 | } |
| 171 | # endif |
| 172 | |
| 173 | int rand_pool_init(void) |
| 174 | { |
| 175 | return 1; |
| 176 | } |
| 177 | |
| 178 | void rand_pool_cleanup(void) |
| 179 | { |
| 180 | } |
| 181 | |
| 182 | void rand_pool_keep_random_devices_open(int keep) |
| 183 | { |
| 184 | } |
| 185 | |
| 186 | #endif |