sshrand.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * sshrand.c: manage the global live PRNG instance.
  3. */
  4. #include "putty.h"
  5. #include "ssh.h"
  6. #include "storage.h"
  7. #include <assert.h>
  8. /* Collect environmental noise every 5 minutes */
  9. #define NOISE_REGULAR_INTERVAL (5*60*TICKSPERSEC)
  10. int random_active = 0;
  11. #ifdef FUZZING
  12. /*
  13. * Special dummy version of the RNG for use when fuzzing.
  14. */
  15. void random_add_noise(NoiseSourceId source, const void *noise, int length) { }
  16. void random_ref(void) { }
  17. void random_setup_custom(const ssh_hashalg *hash) { }
  18. void random_unref(void) { }
  19. void random_read(void *out, size_t size)
  20. {
  21. memset(out, 0x45, size); /* Chosen by eight fair coin tosses */
  22. }
  23. void random_get_savedata(void **data, int *len) { }
  24. #else /* !FUZZING */
  25. /* Dummy structure for the sake of having something to expire_timer_context */
  26. static struct random_timer_context { int dummy; } random_timer_ctx;
  27. static prng *global_prng;
  28. static unsigned long next_noise_collection;
  29. void random_add_noise(NoiseSourceId source, const void *noise, int length)
  30. {
  31. WINSCP_PUTTY_SECTION_ENTER;
  32. if (!random_active)
  33. return;
  34. prng_add_entropy(global_prng, source, make_ptrlen(noise, length));
  35. WINSCP_PUTTY_SECTION_LEAVE;
  36. }
  37. static void random_timer(void *ctx, unsigned long now)
  38. {
  39. unreachable_internal(); // WINSCP
  40. WINSCP_PUTTY_SECTION_ENTER;
  41. if (random_active > 0 && now == next_noise_collection) {
  42. noise_regular();
  43. next_noise_collection =
  44. schedule_timer(NOISE_REGULAR_INTERVAL, random_timer,
  45. &random_timer_ctx);
  46. }
  47. WINSCP_PUTTY_SECTION_LEAVE;
  48. }
  49. static void random_seed_callback(void *noise, int length)
  50. {
  51. WINSCP_PUTTY_SECTION_ENTER;
  52. put_data(global_prng, noise, length);
  53. WINSCP_PUTTY_SECTION_LEAVE;
  54. }
  55. static void random_create(const ssh_hashalg *hashalg)
  56. {
  57. assert(!global_prng);
  58. global_prng = prng_new(hashalg);
  59. prng_seed_begin(global_prng);
  60. noise_get_heavy(random_seed_callback);
  61. prng_seed_finish(global_prng);
  62. next_noise_collection =
  63. schedule_timer(NOISE_REGULAR_INTERVAL, random_timer,
  64. &random_timer_ctx);
  65. /* noise_get_heavy probably read our random seed file.
  66. * Therefore (in fact, even if it didn't), we should write a
  67. * fresh one, in case another instance of ourself starts up
  68. * before we finish, and also in case an attacker gets hold of
  69. * the seed data we used. */
  70. random_save_seed();
  71. }
  72. void random_save_seed(void)
  73. {
  74. int len;
  75. void *data;
  76. if (random_active) {
  77. random_get_savedata(&data, &len);
  78. write_random_seed(data, len);
  79. sfree(data);
  80. }
  81. }
  82. void random_ref(void)
  83. {
  84. WINSCP_PUTTY_SECTION_ENTER;
  85. if (!random_active++) {
  86. enable_dit(); /* just in case main() forgot */
  87. random_create(&ssh_sha256);
  88. }
  89. WINSCP_PUTTY_SECTION_LEAVE;
  90. }
  91. void random_setup_custom(const ssh_hashalg *hash)
  92. {
  93. unreachable_internal(); // used by PuTTYgen only
  94. WINSCP_PUTTY_SECTION_ENTER;
  95. random_active++;
  96. random_create(hash);
  97. WINSCP_PUTTY_SECTION_LEAVE;
  98. }
  99. void random_reseed(ptrlen seed)
  100. {
  101. unreachable_internal(); // used by PuTTYgen only
  102. WINSCP_PUTTY_SECTION_ENTER;
  103. prng_seed_begin(global_prng);
  104. put_datapl(global_prng, seed);
  105. prng_seed_finish(global_prng);
  106. WINSCP_PUTTY_SECTION_LEAVE;
  107. }
  108. // Never called directly in WINSCP
  109. void random_clear(void)
  110. {
  111. if (global_prng) {
  112. #ifndef WINSCP
  113. // We control this on our own in PuttyFinalize()
  114. random_save_seed();
  115. #endif
  116. expire_timer_context(&random_timer_ctx);
  117. prng_free(global_prng);
  118. global_prng = NULL;
  119. random_active = 0;
  120. }
  121. }
  122. void random_unref(void)
  123. {
  124. WINSCP_PUTTY_SECTION_ENTER;
  125. assert(random_active > 0);
  126. if (--random_active == 0)
  127. random_clear();
  128. WINSCP_PUTTY_SECTION_LEAVE;
  129. }
  130. void random_read(void *buf, size_t size)
  131. {
  132. WINSCP_PUTTY_SECTION_ENTER;
  133. assert(random_active > 0);
  134. prng_read(global_prng, buf, size);
  135. WINSCP_PUTTY_SECTION_LEAVE;
  136. }
  137. void random_get_savedata(void **data, int *len)
  138. {
  139. WINSCP_PUTTY_SECTION_ENTER; // though called from our finalizer, when the app is single threaded only
  140. { // WINSCP
  141. void *buf = snewn(global_prng->savesize, char);
  142. random_read(buf, global_prng->savesize);
  143. *len = global_prng->savesize;
  144. *data = buf;
  145. } // WINSCP
  146. WINSCP_PUTTY_SECTION_LEAVE;
  147. }
  148. size_t random_seed_bits(void)
  149. {
  150. assert(random_active > 0);
  151. return prng_seed_bits(global_prng);
  152. }
  153. #endif /* FUZZING */