WINNOISE.C 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. #ifdef MPEXT
  10. extern CRITICAL_SECTION noise_section;
  11. #endif
  12. /*
  13. * This function is called once, at PuTTY startup, and will do some
  14. * seriously silly things like listing directories and getting disk
  15. * free space and a process snapshot.
  16. */
  17. void noise_get_heavy(void (*func) (void *, int))
  18. {
  19. HANDLE srch;
  20. WIN32_FIND_DATA finddata;
  21. DWORD pid;
  22. char winpath[MAX_PATH + 3];
  23. GetWindowsDirectory(winpath, sizeof(winpath));
  24. strcat(winpath, "\\*");
  25. srch = FindFirstFile(winpath, &finddata);
  26. if (srch != INVALID_HANDLE_VALUE) {
  27. do {
  28. func(&finddata, sizeof(finddata));
  29. } while (FindNextFile(srch, &finddata));
  30. FindClose(srch);
  31. }
  32. pid = GetCurrentProcessId();
  33. func(&pid, sizeof(pid));
  34. read_random_seed(func);
  35. /* Update the seed immediately, in case another instance uses it. */
  36. random_save_seed();
  37. }
  38. void random_save_seed(void)
  39. {
  40. int len;
  41. void *data;
  42. if (random_active) {
  43. random_get_savedata(&data, &len);
  44. write_random_seed(data, len);
  45. sfree(data);
  46. }
  47. }
  48. /*
  49. * This function is called every time the random pool needs
  50. * stirring, and will acquire the system time in all available
  51. * forms.
  52. */
  53. void noise_get_light(void (*func) (void *, int))
  54. {
  55. SYSTEMTIME systime;
  56. DWORD adjust[2];
  57. BOOL rubbish;
  58. GetSystemTime(&systime);
  59. func(&systime, sizeof(systime));
  60. GetSystemTimeAdjustment(&adjust[0], &adjust[1], &rubbish);
  61. func(&adjust, sizeof(adjust));
  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. #ifdef MPEXT
  77. EnterCriticalSection(&noise_section);
  78. #endif
  79. w = GetForegroundWindow();
  80. random_add_noise(&w, sizeof(w));
  81. w = GetCapture();
  82. random_add_noise(&w, sizeof(w));
  83. w = GetClipboardOwner();
  84. random_add_noise(&w, sizeof(w));
  85. z = GetQueueStatus(QS_ALLEVENTS);
  86. random_add_noise(&z, sizeof(z));
  87. GetCursorPos(&pt);
  88. random_add_noise(&pt, sizeof(pt));
  89. GlobalMemoryStatus(&memstat);
  90. random_add_noise(&memstat, sizeof(memstat));
  91. GetThreadTimes(GetCurrentThread(), times, times + 1, times + 2,
  92. times + 3);
  93. random_add_noise(&times, sizeof(times));
  94. GetProcessTimes(GetCurrentProcess(), times, times + 1, times + 2,
  95. times + 3);
  96. random_add_noise(&times, sizeof(times));
  97. #ifdef MPEXT
  98. LeaveCriticalSection(&noise_section);
  99. #endif
  100. }
  101. /*
  102. * This function is called on every keypress or mouse move, and
  103. * will add the current Windows time and performance monitor
  104. * counter to the noise pool. It gets the scan code or mouse
  105. * position passed in.
  106. */
  107. void noise_ultralight(unsigned long data)
  108. {
  109. DWORD wintime;
  110. LARGE_INTEGER perftime;
  111. #ifdef MPEXT
  112. EnterCriticalSection(&noise_section);
  113. #endif
  114. random_add_noise(&data, sizeof(DWORD));
  115. wintime = GetTickCount();
  116. random_add_noise(&wintime, sizeof(DWORD));
  117. if (QueryPerformanceCounter(&perftime))
  118. random_add_noise(&perftime, sizeof(perftime));
  119. #ifdef MPEXT
  120. LeaveCriticalSection(&noise_section);
  121. #endif
  122. }