sshrand.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. if (!random_active)
  32. return;
  33. prng_add_entropy(global_prng, source, make_ptrlen(noise, length));
  34. }
  35. static void random_timer(void *ctx, unsigned long now)
  36. {
  37. if (random_active > 0 && now == next_noise_collection) {
  38. noise_regular();
  39. next_noise_collection =
  40. schedule_timer(NOISE_REGULAR_INTERVAL, random_timer,
  41. &random_timer_ctx);
  42. }
  43. }
  44. static void random_seed_callback(void *noise, int length)
  45. {
  46. put_data(global_prng, noise, length);
  47. }
  48. static void random_create(const ssh_hashalg *hashalg)
  49. {
  50. assert(!global_prng);
  51. global_prng = prng_new(hashalg);
  52. prng_seed_begin(global_prng);
  53. noise_get_heavy(random_seed_callback);
  54. prng_seed_finish(global_prng);
  55. next_noise_collection =
  56. schedule_timer(NOISE_REGULAR_INTERVAL, random_timer,
  57. &random_timer_ctx);
  58. /* noise_get_heavy probably read our random seed file.
  59. * Therefore (in fact, even if it didn't), we should write a
  60. * fresh one, in case another instance of ourself starts up
  61. * before we finish, and also in case an attacker gets hold of
  62. * the seed data we used. */
  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. void random_ref(void)
  76. {
  77. if (!random_active++)
  78. random_create(&ssh_sha256);
  79. }
  80. void random_setup_custom(const ssh_hashalg *hash)
  81. {
  82. random_active++;
  83. random_create(hash);
  84. }
  85. void random_reseed(ptrlen seed)
  86. {
  87. prng_seed_begin(global_prng);
  88. put_datapl(global_prng, seed);
  89. prng_seed_finish(global_prng);
  90. }
  91. void random_clear(void)
  92. {
  93. if (global_prng) {
  94. random_save_seed();
  95. expire_timer_context(&random_timer_ctx);
  96. prng_free(global_prng);
  97. global_prng = NULL;
  98. random_active = 0;
  99. }
  100. }
  101. void random_unref(void)
  102. {
  103. assert(random_active > 0);
  104. if (--random_active == 0)
  105. random_clear();
  106. }
  107. void random_read(void *buf, size_t size)
  108. {
  109. assert(random_active > 0);
  110. prng_read(global_prng, buf, size);
  111. }
  112. void random_get_savedata(void **data, int *len)
  113. {
  114. void *buf = snewn(global_prng->savesize, char);
  115. random_read(buf, global_prng->savesize);
  116. *len = global_prng->savesize;
  117. *data = buf;
  118. }
  119. size_t random_seed_bits(void)
  120. {
  121. assert(random_active > 0);
  122. return prng_seed_bits(global_prng);
  123. }
  124. #endif /* FUZZING */