Avoid out-of-bounds read
[openssl.git] / crypto / rand / rand_win.c
1 /*
2  * Copyright 1995-2017 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_lcl.h"
13
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 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
24 # endif
25
26 # ifdef USE_BCRYPTGENRANDOM
27 #  include <bcrypt.h>
28 #  pragma comment(lib, "bcrypt.lib")
29 #  ifndef STATUS_SUCCESS
30 #   define STATUS_SUCCESS ((NTSTATUS)0x00000000L)
31 #  endif
32 # else
33 #  include <wincrypt.h>
34 /*
35  * Intel hardware RNG CSP -- available from
36  * http://developer.intel.com/design/security/rng/redist_license.htm
37  */
38 #  define PROV_INTEL_SEC 22
39 #  define INTEL_DEF_PROV L"Intel Hardware Cryptographic Service Provider"
40 # endif
41
42 int RAND_poll_ex(RAND_poll_cb rand_add, void *arg)
43 {
44 # ifndef USE_BCRYPTGENRANDOM
45     HCRYPTPROV hProvider;
46     int ok = 0;
47 # endif
48     BYTE buf[RANDOMNESS_NEEDED];
49
50 # ifdef OPENSSL_RAND_SEED_RDTSC
51     rand_read_tsc(cb, arg);
52 # endif
53 # ifdef OPENSSL_RAND_SEED_RDCPU
54     if (rand_read_cpu(cb, arg))
55         return 1;
56 # endif
57
58 # ifdef USE_BCRYPTGENRANDOM
59     if (BCryptGenRandom(NULL, buf, (ULONG)sizeof(buf),
60                         BCRYPT_USE_SYSTEM_PREFERRED_RNG) == STATUS_SUCCESS) {
61         rand_add(arg, buf, sizeof(buf), sizeof(buf));
62         return 1;
63     }
64 # else
65     /* poll the CryptoAPI PRNG */
66     if (CryptAcquireContextW(&hProvider, NULL, NULL, PROV_RSA_FULL,
67                              CRYPT_VERIFYCONTEXT | CRYPT_SILENT) != 0) {
68         if (CryptGenRandom(hProvider, (DWORD)sizeof(buf), buf) != 0) {
69             rand_add(arg, buf, sizeof(buf), sizeof(buf));
70             ok = 1;
71         }
72         CryptReleaseContext(hProvider, 0);
73         if (ok)
74             return 1;
75     }
76
77     /* poll the Pentium PRG with CryptoAPI */
78     if (CryptAcquireContextW(&hProvider, NULL, INTEL_DEF_PROV, PROV_INTEL_SEC,
79                              CRYPT_VERIFYCONTEXT | CRYPT_SILENT) != 0) {
80         if (CryptGenRandom(hProvider, (DWORD)sizeof(buf), buf) != 0) {
81             rand_add(arg, buf, sizeof(buf), sizeof(buf));
82             ok = 1;
83         }
84         CryptReleaseContext(hProvider, 0);
85         if (ok)
86             return 1;
87     }
88 # endif
89
90     return 0;
91 }
92
93 # if OPENSSL_API_COMPAT < 0x10100000L
94 int RAND_event(UINT iMsg, WPARAM wParam, LPARAM lParam)
95 {
96     RAND_poll();
97     return RAND_status();
98 }
99
100 void RAND_screen(void)
101 {
102     RAND_poll();
103 }
104 # endif
105
106 #endif