rand_win.c 5.2 KB

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