1
0

sshrand.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. random_create(&ssh_sha256);
  87. WINSCP_PUTTY_SECTION_LEAVE;
  88. }
  89. void random_setup_custom(const ssh_hashalg *hash)
  90. {
  91. unreachable_internal(); // used by PuTTYgen only
  92. WINSCP_PUTTY_SECTION_ENTER;
  93. random_active++;
  94. random_create(hash);
  95. WINSCP_PUTTY_SECTION_LEAVE;
  96. }
  97. void random_reseed(ptrlen seed)
  98. {
  99. unreachable_internal(); // used by PuTTYgen only
  100. WINSCP_PUTTY_SECTION_ENTER;
  101. prng_seed_begin(global_prng);
  102. put_datapl(global_prng, seed);
  103. prng_seed_finish(global_prng);
  104. WINSCP_PUTTY_SECTION_LEAVE;
  105. }
  106. // Never called directly in WINSCP
  107. void random_clear(void)
  108. {
  109. if (global_prng) {
  110. #ifndef WINSCP
  111. // We control this on our own in PuttyFinalize()
  112. random_save_seed();
  113. #endif
  114. expire_timer_context(&random_timer_ctx);
  115. prng_free(global_prng);
  116. global_prng = NULL;
  117. random_active = 0;
  118. }
  119. }
  120. void random_unref(void)
  121. {
  122. WINSCP_PUTTY_SECTION_ENTER;
  123. assert(random_active > 0);
  124. if (--random_active == 0)
  125. random_clear();
  126. WINSCP_PUTTY_SECTION_LEAVE;
  127. }
  128. void random_read(void *buf, size_t size)
  129. {
  130. WINSCP_PUTTY_SECTION_ENTER;
  131. assert(random_active > 0);
  132. prng_read(global_prng, buf, size);
  133. WINSCP_PUTTY_SECTION_LEAVE;
  134. }
  135. void random_get_savedata(void **data, int *len)
  136. {
  137. WINSCP_PUTTY_SECTION_ENTER; // though called from our finalizer, when the app is single threaded only
  138. { // WINSCP
  139. void *buf = snewn(global_prng->savesize, char);
  140. random_read(buf, global_prng->savesize);
  141. *len = global_prng->savesize;
  142. *data = buf;
  143. } // WINSCP
  144. WINSCP_PUTTY_SECTION_LEAVE;
  145. }
  146. size_t random_seed_bits(void)
  147. {
  148. assert(random_active > 0);
  149. return prng_seed_bits(global_prng);
  150. }
  151. #endif /* FUZZING */