noise.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * Noise generation for PuTTY's cryptographic random number
  3. * generator.
  4. */
  5. #include <stdio.h>
  6. #include "putty.h"
  7. #include "ssh.h"
  8. #include "storage.h"
  9. #include <wincrypt.h>
  10. DECL_WINDOWS_FUNCTION(static, BOOL, CryptAcquireContextA,
  11. (HCRYPTPROV *, LPCTSTR, LPCTSTR, DWORD, DWORD));
  12. DECL_WINDOWS_FUNCTION(static, BOOL, CryptGenRandom,
  13. (HCRYPTPROV, DWORD, BYTE *));
  14. DECL_WINDOWS_FUNCTION(static, BOOL, CryptReleaseContext,
  15. (HCRYPTPROV, DWORD));
  16. static HMODULE wincrypt_module = NULL;
  17. bool win_read_random(void *buf, unsigned wanted)
  18. {
  19. bool toret = false;
  20. HCRYPTPROV crypt_provider;
  21. if (!wincrypt_module) {
  22. wincrypt_module = load_system32_dll("advapi32.dll");
  23. GET_WINDOWS_FUNCTION(wincrypt_module, CryptAcquireContextA);
  24. GET_WINDOWS_FUNCTION(wincrypt_module, CryptGenRandom);
  25. GET_WINDOWS_FUNCTION(wincrypt_module, CryptReleaseContext);
  26. }
  27. if (wincrypt_module && p_CryptAcquireContextA &&
  28. p_CryptGenRandom && p_CryptReleaseContext &&
  29. p_CryptAcquireContextA(&crypt_provider, NULL, NULL, PROV_RSA_FULL,
  30. CRYPT_VERIFYCONTEXT)) {
  31. toret = p_CryptGenRandom(crypt_provider, wanted, buf);
  32. p_CryptReleaseContext(crypt_provider, 0);
  33. }
  34. return toret;
  35. }
  36. /*
  37. * This function is called once, at PuTTY startup.
  38. */
  39. void noise_get_heavy(void (*func) (void *, int))
  40. {
  41. HANDLE srch;
  42. WIN32_FIND_DATA finddata;
  43. DWORD pid;
  44. char winpath[MAX_PATH + 3];
  45. BYTE buf[32];
  46. GetWindowsDirectory(winpath, sizeof(winpath));
  47. strcat(winpath, "\\*");
  48. srch = FindFirstFile(winpath, &finddata);
  49. if (srch != INVALID_HANDLE_VALUE) {
  50. do {
  51. func(&finddata, sizeof(finddata));
  52. } while (FindNextFile(srch, &finddata));
  53. FindClose(srch);
  54. }
  55. pid = GetCurrentProcessId();
  56. func(&pid, sizeof(pid));
  57. if (win_read_random(buf, sizeof(buf))) {
  58. func(buf, sizeof(buf));
  59. smemclr(buf, sizeof(buf));
  60. }
  61. read_random_seed(func);
  62. }
  63. /*
  64. * This function is called on a timer, and it will monitor
  65. * frequently changing quantities such as the state of physical and
  66. * virtual memory, the state of the process's message queue, which
  67. * window is in the foreground, which owns the clipboard, etc.
  68. */
  69. void noise_regular(void)
  70. {
  71. HWND w;
  72. DWORD z;
  73. POINT pt;
  74. MEMORYSTATUS memstat;
  75. FILETIME times[4];
  76. w = GetForegroundWindow();
  77. random_add_noise(NOISE_SOURCE_FGWINDOW, &w, sizeof(w));
  78. w = GetCapture();
  79. random_add_noise(NOISE_SOURCE_CAPTURE, &w, sizeof(w));
  80. w = GetClipboardOwner();
  81. random_add_noise(NOISE_SOURCE_CLIPBOARD, &w, sizeof(w));
  82. z = GetQueueStatus(QS_ALLEVENTS);
  83. random_add_noise(NOISE_SOURCE_QUEUE, &z, sizeof(z));
  84. GetCursorPos(&pt);
  85. random_add_noise(NOISE_SOURCE_CURSORPOS, &pt, sizeof(pt));
  86. GlobalMemoryStatus(&memstat);
  87. random_add_noise(NOISE_SOURCE_MEMINFO, &memstat, sizeof(memstat));
  88. GetThreadTimes(GetCurrentThread(), times, times + 1, times + 2,
  89. times + 3);
  90. random_add_noise(NOISE_SOURCE_THREADTIME, &times, sizeof(times));
  91. GetProcessTimes(GetCurrentProcess(), times, times + 1, times + 2,
  92. times + 3);
  93. random_add_noise(NOISE_SOURCE_PROCTIME, &times, sizeof(times));
  94. }
  95. /*
  96. * This function is called on every keypress or mouse move, and
  97. * will add the current Windows time and performance monitor
  98. * counter to the noise pool. It gets the scan code or mouse
  99. * position passed in.
  100. */
  101. void noise_ultralight(NoiseSourceId id, unsigned long data)
  102. {
  103. DWORD wintime;
  104. LARGE_INTEGER perftime;
  105. random_add_noise(id, &data, sizeof(DWORD));
  106. wintime = GetTickCount();
  107. random_add_noise(NOISE_SOURCE_TIME, &wintime, sizeof(DWORD));
  108. if (QueryPerformanceCounter(&perftime))
  109. random_add_noise(NOISE_SOURCE_PERFCOUNT, &perftime, sizeof(perftime));
  110. }
  111. uint64_t prng_reseed_time_ms(void)
  112. {
  113. FILETIME ft;
  114. GetSystemTimeAsFileTime(&ft);
  115. uint64_t value = ft.dwHighDateTime;
  116. value = (value << 32) + ft.dwLowDateTime;
  117. return value / 10000; /* 1 millisecond / 100ns */
  118. }