]>
Commit | Line | Data |
---|---|---|
b1322259 | 1 | /* |
8389ec4b | 2 | * Copyright 1995-2017 The OpenSSL Project Authors. All Rights Reserved. |
2ace287d | 3 | * |
b1322259 RS |
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 | |
2ace287d BM |
8 | */ |
9 | ||
b39fc560 | 10 | #include "internal/cryptlib.h" |
c0722725 UM |
11 | #include <openssl/rand.h> |
12 | #include "rand_lcl.h" | |
2ace287d | 13 | |
7a8c7288 | 14 | #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_WIN32) |
8389ec4b RS |
15 | |
16 | # ifndef OPENSSL_RAND_SEED_OS | |
17 | # error "Unsupported seeding method configured; must be os" | |
18 | # endif | |
19 | ||
0f113f3e | 20 | # include <windows.h> |
0814afcf | 21 | /* On Windows 7 or higher use BCrypt instead of the legacy CryptoAPI */ |
8389ec4b RS |
22 | # if defined(_MSC_VER) && defined(_WIN32_WINNT) && _WIN32_WINNT >= 0x0601 |
23 | # define USE_BCRYPTGENRANDOM | |
0814afcf M |
24 | # endif |
25 | ||
8389ec4b | 26 | # ifdef USE_BCRYPTGENRANDOM |
fa64e633 M |
27 | # include <bcrypt.h> |
28 | # pragma comment(lib, "bcrypt.lib") | |
6191fc86 M |
29 | # ifndef STATUS_SUCCESS |
30 | # define STATUS_SUCCESS ((NTSTATUS)0x00000000L) | |
31 | # endif | |
fa64e633 | 32 | # else |
fa64e633 | 33 | # include <wincrypt.h> |
0f113f3e MC |
34 | /* |
35 | * Intel hardware RNG CSP -- available from | |
c0722725 UM |
36 | * http://developer.intel.com/design/security/rng/redist_license.htm |
37 | */ | |
fa64e633 M |
38 | # define PROV_INTEL_SEC 22 |
39 | # define INTEL_DEF_PROV L"Intel Hardware Cryptographic Service Provider" | |
40 | # endif | |
c0722725 | 41 | |
75e2c877 | 42 | int RAND_poll_ex(RAND_poll_fn cb, void *arg) |
c0722725 | 43 | { |
8389ec4b | 44 | # ifndef USE_BCRYPTGENRANDOM |
e56f956e | 45 | HCRYPTPROV hProvider; |
fa64e633 | 46 | # endif |
0f113f3e | 47 | DWORD w; |
8389ec4b RS |
48 | BYTE buf[RANDOMNESS_NEEDED]; |
49 | int ok = 0; | |
0f113f3e | 50 | |
8389ec4b | 51 | # ifdef OPENSSL_RAND_SEED_RDTSC |
75e2c877 | 52 | rand_read_tsc(cb, arg); |
8389ec4b RS |
53 | # endif |
54 | # ifdef OPENSSL_RAND_SEED_RDCPU | |
75e2c877 | 55 | if (rand_read_cpu(cb, arg)) |
8389ec4b RS |
56 | ok++; |
57 | # endif | |
58 | ||
59 | # ifdef USE_BCRYPTGENRANDOM | |
60 | if (BCryptGenRandom(NULL, buf, (ULONG)sizeof(buf), | |
61 | BCRYPT_USE_SYSTEM_PREFERRED_RNG) != STATUS_SUCCESS) | |
62 | return 0; | |
75e2c877 | 63 | cb(arg, buf, sizeof(buf), sizeof(buf)); |
8389ec4b | 64 | return 1; |
fa64e633 | 65 | # else |
eb9b92ec | 66 | /* poll the CryptoAPI PRNG */ |
8389ec4b RS |
67 | if (CryptAcquireContextW(&hProvider, NULL, NULL, PROV_RSA_FULL, |
68 | CRYPT_VERIFYCONTEXT | CRYPT_SILENT) != 0) { | |
e56f956e | 69 | if (CryptGenRandom(hProvider, (DWORD)sizeof(buf), buf) != 0) { |
75e2c877 | 70 | cb(arg, buf, sizeof(buf), sizeof(buf)); |
8389ec4b | 71 | ok++; |
0f113f3e | 72 | } |
eb9b92ec | 73 | CryptReleaseContext(hProvider, 0); |
0f113f3e | 74 | } |
0f113f3e | 75 | |
eb9b92ec | 76 | /* poll the Pentium PRG with CryptoAPI */ |
8389ec4b RS |
77 | if (CryptAcquireContextW(&hProvider, NULL, INTEL_DEF_PROV, PROV_INTEL_SEC, |
78 | CRYPT_VERIFYCONTEXT | CRYPT_SILENT) != 0) { | |
e56f956e | 79 | if (CryptGenRandom(hProvider, (DWORD)sizeof(buf), buf) != 0) { |
75e2c877 | 80 | cb(arg, buf, sizeof(buf), sizeof(buf)); |
8389ec4b | 81 | ok++; |
0f113f3e | 82 | } |
eb9b92ec | 83 | CryptReleaseContext(hProvider, 0); |
0f113f3e | 84 | } |
fa64e633 | 85 | # endif |
0f113f3e | 86 | |
8389ec4b | 87 | return ok ? 1 : 0; |
0f113f3e | 88 | } |
2ace287d | 89 | |
56add338 | 90 | #if OPENSSL_API_COMPAT < 0x10100000L |
73241290 JY |
91 | int RAND_event(UINT iMsg, WPARAM wParam, LPARAM lParam) |
92 | { | |
93 | RAND_poll(); | |
94 | return RAND_status(); | |
95 | } | |
96 | ||
97 | void RAND_screen(void) | |
98 | { | |
99 | RAND_poll(); | |
100 | } | |
101 | #endif | |
102 | ||
2ace287d | 103 | #endif |