rand_win.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. #include "internal/cryptlib.h"
  10. #include <openssl/rand.h>
  11. #include "rand_lcl.h"
  12. #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_WIN32)
  13. # include <windows.h>
  14. /* On Windows 7 or higher use BCrypt instead of the legacy CryptoAPI */
  15. # if defined(_MSC_VER) && defined(_WIN32_WINNT) && _WIN32_WINNT>=0x0601
  16. # define RAND_WINDOWS_USE_BCRYPT
  17. # endif
  18. # ifdef RAND_WINDOWS_USE_BCRYPT
  19. # include <bcrypt.h>
  20. # pragma comment(lib, "bcrypt.lib")
  21. # ifndef STATUS_SUCCESS
  22. # define STATUS_SUCCESS ((NTSTATUS)0x00000000L)
  23. # endif
  24. # else
  25. # include <wincrypt.h>
  26. /*
  27. * Intel hardware RNG CSP -- available from
  28. * http://developer.intel.com/design/security/rng/redist_license.htm
  29. */
  30. # define PROV_INTEL_SEC 22
  31. # define INTEL_DEF_PROV L"Intel Hardware Cryptographic Service Provider"
  32. # endif
  33. static void readtimer(void);
  34. int RAND_poll(void)
  35. {
  36. MEMORYSTATUS mst;
  37. # ifndef RAND_WINDOWS_USE_BCRYPT
  38. HCRYPTPROV hProvider;
  39. # endif
  40. DWORD w;
  41. BYTE buf[64];
  42. # ifdef RAND_WINDOWS_USE_BCRYPT
  43. if (BCryptGenRandom(NULL, buf, (ULONG)sizeof(buf), BCRYPT_USE_SYSTEM_PREFERRED_RNG) == STATUS_SUCCESS) {
  44. RAND_add(buf, sizeof(buf), sizeof(buf));
  45. }
  46. # else
  47. /* poll the CryptoAPI PRNG */
  48. /* The CryptoAPI returns sizeof(buf) bytes of randomness */
  49. if (CryptAcquireContextW(&hProvider, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT | CRYPT_SILENT)) {
  50. if (CryptGenRandom(hProvider, (DWORD)sizeof(buf), buf) != 0) {
  51. RAND_add(buf, sizeof(buf), sizeof(buf));
  52. }
  53. CryptReleaseContext(hProvider, 0);
  54. }
  55. /* poll the Pentium PRG with CryptoAPI */
  56. if (CryptAcquireContextW(&hProvider, NULL, INTEL_DEF_PROV, PROV_INTEL_SEC, CRYPT_VERIFYCONTEXT | CRYPT_SILENT)) {
  57. if (CryptGenRandom(hProvider, (DWORD)sizeof(buf), buf) != 0) {
  58. RAND_add(buf, sizeof(buf), sizeof(buf));
  59. }
  60. CryptReleaseContext(hProvider, 0);
  61. }
  62. # endif
  63. /* timer data */
  64. readtimer();
  65. /* memory usage statistics */
  66. GlobalMemoryStatus(&mst);
  67. RAND_add(&mst, sizeof(mst), 1);
  68. /* process ID */
  69. w = GetCurrentProcessId();
  70. RAND_add(&w, sizeof(w), 1);
  71. return (1);
  72. }
  73. #if OPENSSL_API_COMPAT < 0x10100000L
  74. int RAND_event(UINT iMsg, WPARAM wParam, LPARAM lParam)
  75. {
  76. RAND_poll();
  77. return RAND_status();
  78. }
  79. void RAND_screen(void)
  80. {
  81. RAND_poll();
  82. }
  83. #endif
  84. /* feed timing information to the PRNG */
  85. static void readtimer(void)
  86. {
  87. DWORD w;
  88. LARGE_INTEGER l;
  89. static int have_perfc = 1;
  90. # if defined(_MSC_VER) && defined(_M_X86)
  91. static int have_tsc = 1;
  92. DWORD cyclecount;
  93. if (have_tsc) {
  94. __try {
  95. __asm {
  96. _emit 0x0f _emit 0x31 mov cyclecount, eax}
  97. RAND_add(&cyclecount, sizeof(cyclecount), 1);
  98. }
  99. __except(EXCEPTION_EXECUTE_HANDLER) {
  100. have_tsc = 0;
  101. }
  102. }
  103. # else
  104. # define have_tsc 0
  105. # endif
  106. if (have_perfc) {
  107. if (QueryPerformanceCounter(&l) == 0)
  108. have_perfc = 0;
  109. else
  110. RAND_add(&l, sizeof(l), 0);
  111. }
  112. if (!have_tsc && !have_perfc) {
  113. w = GetTickCount();
  114. RAND_add(&w, sizeof(w), 0);
  115. }
  116. }
  117. #endif