rand_win.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. * Copyright 1995-2022 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 "crypto/rand_pool.h"
  12. #include "crypto/rand.h"
  13. #include "prov/seeding.h"
  14. #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_WIN32)
  15. # ifndef OPENSSL_RAND_SEED_OS
  16. # error "Unsupported seeding method configured; must be os"
  17. # endif
  18. # include <windows.h>
  19. /* On Windows Vista or higher use BCrypt instead of the legacy CryptoAPI */
  20. # if defined(_MSC_VER) && _MSC_VER > 1500 /* 1500 = Visual Studio 2008 */ \
  21. && defined(_WIN32_WINNT) && _WIN32_WINNT >= 0x0600
  22. # define USE_BCRYPTGENRANDOM
  23. # endif
  24. # ifdef USE_BCRYPTGENRANDOM
  25. # include <bcrypt.h>
  26. # ifdef _MSC_VER
  27. # pragma comment(lib, "bcrypt.lib")
  28. # endif
  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. size_t ossl_pool_acquire_entropy(RAND_POOL *pool)
  42. {
  43. # ifndef USE_BCRYPTGENRANDOM
  44. HCRYPTPROV hProvider;
  45. # endif
  46. unsigned char *buffer;
  47. size_t bytes_needed;
  48. size_t entropy_available = 0;
  49. # ifdef OPENSSL_RAND_SEED_RDTSC
  50. entropy_available = ossl_prov_acquire_entropy_from_tsc(pool);
  51. if (entropy_available > 0)
  52. return entropy_available;
  53. # endif
  54. # ifdef OPENSSL_RAND_SEED_RDCPU
  55. entropy_available = ossl_prov_acquire_entropy_from_cpu(pool);
  56. if (entropy_available > 0)
  57. return entropy_available;
  58. # endif
  59. # ifdef USE_BCRYPTGENRANDOM
  60. bytes_needed = ossl_rand_pool_bytes_needed(pool, 1 /*entropy_factor*/);
  61. buffer = ossl_rand_pool_add_begin(pool, bytes_needed);
  62. if (buffer != NULL) {
  63. size_t bytes = 0;
  64. if (BCryptGenRandom(NULL, buffer, bytes_needed,
  65. BCRYPT_USE_SYSTEM_PREFERRED_RNG) == STATUS_SUCCESS)
  66. bytes = bytes_needed;
  67. ossl_rand_pool_add_end(pool, bytes, 8 * bytes);
  68. entropy_available = ossl_rand_pool_entropy_available(pool);
  69. }
  70. if (entropy_available > 0)
  71. return entropy_available;
  72. # else
  73. bytes_needed = ossl_rand_pool_bytes_needed(pool, 1 /*entropy_factor*/);
  74. buffer = ossl_rand_pool_add_begin(pool, bytes_needed);
  75. if (buffer != NULL) {
  76. size_t bytes = 0;
  77. /* poll the CryptoAPI PRNG */
  78. if (CryptAcquireContextW(&hProvider, NULL, NULL, PROV_RSA_FULL,
  79. CRYPT_VERIFYCONTEXT | CRYPT_SILENT) != 0) {
  80. if (CryptGenRandom(hProvider, bytes_needed, buffer) != 0)
  81. bytes = bytes_needed;
  82. CryptReleaseContext(hProvider, 0);
  83. }
  84. ossl_rand_pool_add_end(pool, bytes, 8 * bytes);
  85. entropy_available = ossl_rand_pool_entropy_available(pool);
  86. }
  87. if (entropy_available > 0)
  88. return entropy_available;
  89. bytes_needed = ossl_rand_pool_bytes_needed(pool, 1 /*entropy_factor*/);
  90. buffer = ossl_rand_pool_add_begin(pool, bytes_needed);
  91. if (buffer != NULL) {
  92. size_t bytes = 0;
  93. /* poll the Pentium PRG with CryptoAPI */
  94. if (CryptAcquireContextW(&hProvider, NULL,
  95. INTEL_DEF_PROV, PROV_INTEL_SEC,
  96. CRYPT_VERIFYCONTEXT | CRYPT_SILENT) != 0) {
  97. if (CryptGenRandom(hProvider, bytes_needed, buffer) != 0)
  98. bytes = bytes_needed;
  99. CryptReleaseContext(hProvider, 0);
  100. }
  101. ossl_rand_pool_add_end(pool, bytes, 8 * bytes);
  102. entropy_available = ossl_rand_pool_entropy_available(pool);
  103. }
  104. if (entropy_available > 0)
  105. return entropy_available;
  106. # endif
  107. return ossl_rand_pool_entropy_available(pool);
  108. }
  109. int ossl_pool_add_nonce_data(RAND_POOL *pool)
  110. {
  111. struct {
  112. DWORD pid;
  113. DWORD tid;
  114. FILETIME time;
  115. } data;
  116. /* Erase the entire structure including any padding */
  117. memset(&data, 0, sizeof(data));
  118. /*
  119. * Add process id, thread id, and a high resolution timestamp to
  120. * ensure that the nonce is unique with high probability for
  121. * different process instances.
  122. */
  123. data.pid = GetCurrentProcessId();
  124. data.tid = GetCurrentThreadId();
  125. GetSystemTimeAsFileTime(&data.time);
  126. return ossl_rand_pool_add(pool, (unsigned char *)&data, sizeof(data), 0);
  127. }
  128. int ossl_rand_pool_add_additional_data(RAND_POOL *pool)
  129. {
  130. struct {
  131. DWORD tid;
  132. LARGE_INTEGER time;
  133. } data;
  134. /* Erase the entire structure including any padding */
  135. memset(&data, 0, sizeof(data));
  136. /*
  137. * Add some noise from the thread id and a high resolution timer.
  138. * The thread id adds a little randomness if the drbg is accessed
  139. * concurrently (which is the case for the <master> drbg).
  140. */
  141. data.tid = GetCurrentThreadId();
  142. QueryPerformanceCounter(&data.time);
  143. return ossl_rand_pool_add(pool, (unsigned char *)&data, sizeof(data), 0);
  144. }
  145. int ossl_rand_pool_init(void)
  146. {
  147. return 1;
  148. }
  149. void ossl_rand_pool_cleanup(void)
  150. {
  151. }
  152. void ossl_rand_pool_keep_random_devices_open(int keep)
  153. {
  154. }
  155. #endif