rand_win.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. * Copyright 1995-2022 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. #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. # ifdef _MSC_VER
  26. # pragma comment(lib, "bcrypt.lib")
  27. # endif
  28. # ifndef STATUS_SUCCESS
  29. # define STATUS_SUCCESS ((NTSTATUS)0x00000000L)
  30. # endif
  31. # else
  32. # include <wincrypt.h>
  33. /*
  34. * Intel hardware RNG CSP -- available from
  35. * http://developer.intel.com/design/security/rng/redist_license.htm
  36. */
  37. # define PROV_INTEL_SEC 22
  38. # define INTEL_DEF_PROV L"Intel Hardware Cryptographic Service Provider"
  39. # endif
  40. size_t rand_pool_acquire_entropy(RAND_POOL *pool)
  41. {
  42. # ifndef USE_BCRYPTGENRANDOM
  43. HCRYPTPROV hProvider;
  44. # endif
  45. unsigned char *buffer;
  46. size_t bytes_needed;
  47. size_t entropy_available = 0;
  48. # ifdef OPENSSL_RAND_SEED_RDTSC
  49. entropy_available = rand_acquire_entropy_from_tsc(pool);
  50. if (entropy_available > 0)
  51. return entropy_available;
  52. # endif
  53. # ifdef OPENSSL_RAND_SEED_RDCPU
  54. entropy_available = rand_acquire_entropy_from_cpu(pool);
  55. if (entropy_available > 0)
  56. return entropy_available;
  57. # endif
  58. # ifdef USE_BCRYPTGENRANDOM
  59. bytes_needed = rand_pool_bytes_needed(pool, 1 /*entropy_factor*/);
  60. buffer = rand_pool_add_begin(pool, bytes_needed);
  61. if (buffer != NULL) {
  62. size_t bytes = 0;
  63. if (BCryptGenRandom(NULL, buffer, bytes_needed,
  64. BCRYPT_USE_SYSTEM_PREFERRED_RNG) == STATUS_SUCCESS)
  65. bytes = bytes_needed;
  66. rand_pool_add_end(pool, bytes, 8 * bytes);
  67. entropy_available = rand_pool_entropy_available(pool);
  68. }
  69. if (entropy_available > 0)
  70. return entropy_available;
  71. # else
  72. bytes_needed = rand_pool_bytes_needed(pool, 1 /*entropy_factor*/);
  73. buffer = rand_pool_add_begin(pool, bytes_needed);
  74. if (buffer != NULL) {
  75. size_t bytes = 0;
  76. /* poll the CryptoAPI PRNG */
  77. if (CryptAcquireContextW(&hProvider, NULL, NULL, PROV_RSA_FULL,
  78. CRYPT_VERIFYCONTEXT | CRYPT_SILENT) != 0) {
  79. if (CryptGenRandom(hProvider, bytes_needed, buffer) != 0)
  80. bytes = bytes_needed;
  81. CryptReleaseContext(hProvider, 0);
  82. }
  83. rand_pool_add_end(pool, bytes, 8 * bytes);
  84. entropy_available = rand_pool_entropy_available(pool);
  85. }
  86. if (entropy_available > 0)
  87. return entropy_available;
  88. bytes_needed = rand_pool_bytes_needed(pool, 1 /*entropy_factor*/);
  89. buffer = rand_pool_add_begin(pool, bytes_needed);
  90. if (buffer != NULL) {
  91. size_t bytes = 0;
  92. /* poll the Pentium PRG with CryptoAPI */
  93. if (CryptAcquireContextW(&hProvider, NULL,
  94. INTEL_DEF_PROV, PROV_INTEL_SEC,
  95. CRYPT_VERIFYCONTEXT | CRYPT_SILENT) != 0) {
  96. if (CryptGenRandom(hProvider, bytes_needed, buffer) != 0)
  97. bytes = bytes_needed;
  98. CryptReleaseContext(hProvider, 0);
  99. }
  100. rand_pool_add_end(pool, bytes, 8 * bytes);
  101. entropy_available = rand_pool_entropy_available(pool);
  102. }
  103. if (entropy_available > 0)
  104. return entropy_available;
  105. # endif
  106. return rand_pool_entropy_available(pool);
  107. }
  108. int rand_pool_add_nonce_data(RAND_POOL *pool)
  109. {
  110. struct {
  111. DWORD pid;
  112. DWORD tid;
  113. FILETIME time;
  114. } data = { 0 };
  115. /*
  116. * Add process id, thread id, and a high resolution timestamp to
  117. * ensure that the nonce is unique with 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 = { 0 };
  131. /*
  132. * Add some noise from the thread id and a high resolution timer.
  133. * The thread id adds a little randomness if the drbg is accessed
  134. * concurrently (which is the case for the <master> drbg).
  135. */
  136. data.tid = GetCurrentThreadId();
  137. QueryPerformanceCounter(&data.time);
  138. return rand_pool_add(pool, (unsigned char *)&data, sizeof(data), 0);
  139. }
  140. # if OPENSSL_API_COMPAT < 0x10100000L
  141. int RAND_event(UINT iMsg, WPARAM wParam, LPARAM lParam)
  142. {
  143. RAND_poll();
  144. return RAND_status();
  145. }
  146. void RAND_screen(void)
  147. {
  148. RAND_poll();
  149. }
  150. # endif
  151. int rand_pool_init(void)
  152. {
  153. return 1;
  154. }
  155. void rand_pool_cleanup(void)
  156. {
  157. }
  158. void rand_pool_keep_random_devices_open(int keep)
  159. {
  160. }
  161. #endif