sshrand.c 4.1 KB

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