winnoise.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. /* Update the seed immediately, in case another instance uses it. */
  63. random_save_seed();
  64. }
  65. void random_save_seed(void)
  66. {
  67. int len;
  68. void *data;
  69. if (random_active) {
  70. random_get_savedata(&data, &len);
  71. write_random_seed(data, len);
  72. sfree(data);
  73. }
  74. }
  75. /*
  76. * This function is called every time the random pool needs
  77. * stirring, and will acquire the system time in all available
  78. * forms.
  79. */
  80. void noise_get_light(void (*func) (void *, int))
  81. {
  82. SYSTEMTIME systime;
  83. DWORD adjust[2];
  84. BOOL rubbish;
  85. GetSystemTime(&systime);
  86. func(&systime, sizeof(systime));
  87. GetSystemTimeAdjustment(&adjust[0], &adjust[1], &rubbish);
  88. func(&adjust, sizeof(adjust));
  89. }
  90. /*
  91. * This function is called on a timer, and it will monitor
  92. * frequently changing quantities such as the state of physical and
  93. * virtual memory, the state of the process's message queue, which
  94. * window is in the foreground, which owns the clipboard, etc.
  95. */
  96. void noise_regular(void)
  97. {
  98. HWND w;
  99. DWORD z;
  100. POINT pt;
  101. MEMORYSTATUS memstat;
  102. FILETIME times[4];
  103. MPEXT_PUTTY_SECTION_ENTER;
  104. w = GetForegroundWindow();
  105. random_add_noise(&w, sizeof(w));
  106. w = GetCapture();
  107. random_add_noise(&w, sizeof(w));
  108. w = GetClipboardOwner();
  109. random_add_noise(&w, sizeof(w));
  110. z = GetQueueStatus(QS_ALLEVENTS);
  111. random_add_noise(&z, sizeof(z));
  112. GetCursorPos(&pt);
  113. random_add_noise(&pt, sizeof(pt));
  114. GlobalMemoryStatus(&memstat);
  115. random_add_noise(&memstat, sizeof(memstat));
  116. GetThreadTimes(GetCurrentThread(), times, times + 1, times + 2,
  117. times + 3);
  118. random_add_noise(&times, sizeof(times));
  119. GetProcessTimes(GetCurrentProcess(), times, times + 1, times + 2,
  120. times + 3);
  121. random_add_noise(&times, sizeof(times));
  122. MPEXT_PUTTY_SECTION_LEAVE;
  123. }
  124. /*
  125. * This function is called on every keypress or mouse move, and
  126. * will add the current Windows time and performance monitor
  127. * counter to the noise pool. It gets the scan code or mouse
  128. * position passed in.
  129. */
  130. void noise_ultralight(unsigned long data)
  131. {
  132. DWORD wintime;
  133. LARGE_INTEGER perftime;
  134. MPEXT_PUTTY_SECTION_ENTER;
  135. random_add_noise(&data, sizeof(DWORD));
  136. wintime = GetTickCount();
  137. random_add_noise(&wintime, sizeof(DWORD));
  138. if (QueryPerformanceCounter(&perftime))
  139. random_add_noise(&perftime, sizeof(perftime));
  140. MPEXT_PUTTY_SECTION_LEAVE;
  141. }