rand_win.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*
  2. * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (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. #include "internal/cryptlib.h"
  10. #include <openssl/rand.h>
  11. #include "rand_local.h"
  12. #include "crypto/rand.h"
  13. #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_WIN32)
  14. # ifndef OPENSSL_RAND_SEED_OS
  15. # error "Unsupported seeding method configured; must be os"
  16. # endif
  17. # include <windows.h>
  18. /* On Windows Vista or higher use BCrypt instead of the legacy CryptoAPI */
  19. # if defined(_MSC_VER) && _MSC_VER > 1500 /* 1500 = Visual Studio 2008 */ \
  20. && defined(_WIN32_WINNT) && _WIN32_WINNT >= 0x0600
  21. # define USE_BCRYPTGENRANDOM
  22. # endif
  23. # ifdef USE_BCRYPTGENRANDOM
  24. # include <bcrypt.h>
  25. # pragma comment(lib, "bcrypt.lib")
  26. # ifndef STATUS_SUCCESS
  27. # define STATUS_SUCCESS ((NTSTATUS)0x00000000L)
  28. # endif
  29. # else
  30. # include <wincrypt.h>
  31. /*
  32. * Intel hardware RNG CSP -- available from
  33. * http://developer.intel.com/design/security/rng/redist_license.htm
  34. */
  35. # define PROV_INTEL_SEC 22
  36. # define INTEL_DEF_PROV L"Intel Hardware Cryptographic Service Provider"
  37. # endif
  38. size_t rand_pool_acquire_entropy(RAND_POOL *pool)
  39. {
  40. # ifndef USE_BCRYPTGENRANDOM
  41. HCRYPTPROV hProvider;
  42. # endif
  43. unsigned char *buffer;
  44. size_t bytes_needed;
  45. size_t entropy_available = 0;
  46. # ifdef OPENSSL_RAND_SEED_RDTSC
  47. entropy_available = rand_acquire_entropy_from_tsc(pool);
  48. if (entropy_available > 0)
  49. return entropy_available;
  50. # endif
  51. # ifdef OPENSSL_RAND_SEED_RDCPU
  52. entropy_available = rand_acquire_entropy_from_cpu(pool);
  53. if (entropy_available > 0)
  54. return entropy_available;
  55. # endif
  56. # ifdef USE_BCRYPTGENRANDOM
  57. bytes_needed = rand_pool_bytes_needed(pool, 1 /*entropy_factor*/);
  58. buffer = rand_pool_add_begin(pool, bytes_needed);
  59. if (buffer != NULL) {
  60. size_t bytes = 0;
  61. if (BCryptGenRandom(NULL, buffer, bytes_needed,
  62. BCRYPT_USE_SYSTEM_PREFERRED_RNG) == STATUS_SUCCESS)
  63. bytes = bytes_needed;
  64. rand_pool_add_end(pool, bytes, 8 * bytes);
  65. entropy_available = rand_pool_entropy_available(pool);
  66. }
  67. if (entropy_available > 0)
  68. return entropy_available;
  69. # else
  70. bytes_needed = rand_pool_bytes_needed(pool, 1 /*entropy_factor*/);
  71. buffer = rand_pool_add_begin(pool, bytes_needed);
  72. if (buffer != NULL) {
  73. size_t bytes = 0;
  74. /* poll the CryptoAPI PRNG */
  75. if (CryptAcquireContextW(&hProvider, NULL, NULL, PROV_RSA_FULL,
  76. CRYPT_VERIFYCONTEXT | CRYPT_SILENT) != 0) {
  77. if (CryptGenRandom(hProvider, bytes_needed, buffer) != 0)
  78. bytes = bytes_needed;
  79. CryptReleaseContext(hProvider, 0);
  80. }
  81. rand_pool_add_end(pool, bytes, 8 * bytes);
  82. entropy_available = rand_pool_entropy_available(pool);
  83. }
  84. if (entropy_available > 0)
  85. return entropy_available;
  86. bytes_needed = rand_pool_bytes_needed(pool, 1 /*entropy_factor*/);
  87. buffer = rand_pool_add_begin(pool, bytes_needed);
  88. if (buffer != NULL) {
  89. size_t bytes = 0;
  90. /* poll the Pentium PRG with CryptoAPI */
  91. if (CryptAcquireContextW(&hProvider, NULL,
  92. INTEL_DEF_PROV, PROV_INTEL_SEC,
  93. CRYPT_VERIFYCONTEXT | CRYPT_SILENT) != 0) {
  94. if (CryptGenRandom(hProvider, bytes_needed, buffer) != 0)
  95. bytes = bytes_needed;
  96. CryptReleaseContext(hProvider, 0);
  97. }
  98. rand_pool_add_end(pool, bytes, 8 * bytes);
  99. entropy_available = rand_pool_entropy_available(pool);
  100. }
  101. if (entropy_available > 0)
  102. return entropy_available;
  103. # endif
  104. return rand_pool_entropy_available(pool);
  105. }
  106. int rand_pool_add_nonce_data(RAND_POOL *pool)
  107. {
  108. struct {
  109. DWORD pid;
  110. DWORD tid;
  111. FILETIME time;
  112. } data;
  113. /* Erase the entire structure including any padding */
  114. memset(&data, 0, sizeof(data));
  115. /*
  116. * Add process id, thread id, and a high resolution timestamp to
  117. * ensure that the nonce is unique whith high probability for
  118. * different process instances.
  119. */
  120. data.pid = GetCurrentProcessId();
  121. data.tid = GetCurrentThreadId();
  122. GetSystemTimeAsFileTime(&data.time);
  123. return rand_pool_add(pool, (unsigned char *)&data, sizeof(data), 0);
  124. }
  125. int rand_pool_add_additional_data(RAND_POOL *pool)
  126. {
  127. struct {
  128. DWORD tid;
  129. LARGE_INTEGER time;
  130. } data;
  131. /* Erase the entire structure including any padding */
  132. memset(&data, 0, sizeof(data));
  133. /*
  134. * Add some noise from the thread id and a high resolution timer.
  135. * The thread id adds a little randomness if the drbg is accessed
  136. * concurrently (which is the case for the <master> drbg).
  137. */
  138. data.tid = GetCurrentThreadId();
  139. QueryPerformanceCounter(&data.time);
  140. return rand_pool_add(pool, (unsigned char *)&data, sizeof(data), 0);
  141. }
  142. # if !OPENSSL_API_1_1_0 && !defined(FIPS_MODE)
  143. int RAND_event(UINT iMsg, WPARAM wParam, LPARAM lParam)
  144. {
  145. RAND_poll();
  146. return RAND_status();
  147. }
  148. void RAND_screen(void)
  149. {
  150. RAND_poll();
  151. }
  152. # endif
  153. int rand_pool_init(void)
  154. {
  155. return 1;
  156. }
  157. void rand_pool_cleanup(void)
  158. {
  159. }
  160. void rand_pool_keep_random_devices_open(int keep)
  161. {
  162. }
  163. #endif