remove RAND_screen and friends
[openssl.git] / crypto / rand / rand_win.c
1 /*
2  * Copyright 1995-2016 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 # include <windows.h>
16 # ifndef _WIN32_WINNT
17 #  define _WIN32_WINNT 0x0400
18 # endif
19 # include <wincrypt.h>
20 # include <tlhelp32.h>
21
22 /*
23  * Intel hardware RNG CSP -- available from
24  * http://developer.intel.com/design/security/rng/redist_license.htm
25  */
26 # define PROV_INTEL_SEC 22
27 # define INTEL_DEF_PROV L"Intel Hardware Cryptographic Service Provider"
28
29 static void readtimer(void);
30
31 int RAND_poll(void)
32 {
33     MEMORYSTATUS mst;
34     HCRYPTPROV hProvider = 0;
35     DWORD w;
36     BYTE buf[64];
37
38     /* poll the CryptoAPI PRNG */
39     /* The CryptoAPI returns sizeof(buf) bytes of randomness */
40     if (CryptAcquireContextW(&hProvider, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT | CRYPT_SILENT)) {
41         if (CryptGenRandom(hProvider, sizeof(buf), buf) != 0) {
42             RAND_add(buf, sizeof(buf), sizeof(buf));
43         }
44         CryptReleaseContext(hProvider, 0);
45     }
46
47     /* poll the Pentium PRG with CryptoAPI */
48     if (CryptAcquireContextW(&hProvider, NULL, INTEL_DEF_PROV, PROV_INTEL_SEC, CRYPT_VERIFYCONTEXT | CRYPT_SILENT)) {
49         if (CryptGenRandom(hProvider, sizeof(buf), buf) != 0) {
50             RAND_add(buf, sizeof(buf), sizeof(buf));
51         }
52         CryptReleaseContext(hProvider, 0);
53     }
54
55     /* timer data */
56     readtimer();
57
58     /* memory usage statistics */
59     GlobalMemoryStatus(&mst);
60     RAND_add(&mst, sizeof(mst), 1);
61
62     /* process ID */
63     w = GetCurrentProcessId();
64     RAND_add(&w, sizeof(w), 1);
65
66     return (1);
67 }
68
69 /* feed timing information to the PRNG */
70 static void readtimer(void)
71 {
72     DWORD w;
73     LARGE_INTEGER l;
74     static int have_perfc = 1;
75 # if defined(_MSC_VER) && defined(_M_X86)
76     static int have_tsc = 1;
77     DWORD cyclecount;
78
79     if (have_tsc) {
80         __try {
81             __asm {
82             _emit 0x0f _emit 0x31 mov cyclecount, eax}
83             RAND_add(&cyclecount, sizeof(cyclecount), 1);
84         }
85         __except(EXCEPTION_EXECUTE_HANDLER) {
86             have_tsc = 0;
87         }
88     }
89 # else
90 #  define have_tsc 0
91 # endif
92
93     if (have_perfc) {
94         if (QueryPerformanceCounter(&l) == 0)
95             have_perfc = 0;
96         else
97             RAND_add(&l, sizeof(l), 0);
98     }
99
100     if (!have_tsc && !have_perfc) {
101         w = GetTickCount();
102         RAND_add(&w, sizeof(w), 0);
103     }
104 }
105
106 #endif