sshprng.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /*
  2. * sshprng.c: PuTTY's cryptographic pseudorandom number generator.
  3. *
  4. * This module just defines the PRNG object type and its methods. The
  5. * usual global instance of it is managed by sshrand.c.
  6. */
  7. #include "putty.h"
  8. #include "ssh.h"
  9. #include "mpint.h"
  10. #ifdef PRNG_DIAGNOSTICS
  11. #define prngdebug debug
  12. #else
  13. #define prngdebug(...) ((void)0)
  14. #endif
  15. /*
  16. * This random number generator is based on the 'Fortuna' design by
  17. * Niels Ferguson and Bruce Schneier. The biggest difference is that I
  18. * use SHA-256 in place of a block cipher: the generator side of the
  19. * system works by computing HASH(key || counter) instead of
  20. * ENCRYPT(counter, key).
  21. *
  22. * Rationale: the Fortuna description itself suggests that using
  23. * SHA-256 would be nice but people wouldn't accept it because it's
  24. * too slow - but PuTTY isn't a heavy enough user of random numbers to
  25. * make that a serious worry. In fact even with SHA-256 this generator
  26. * is faster than the one we previously used. Also the Fortuna
  27. * description worries about periodic rekeying to avoid the barely
  28. * detectable pattern of never repeating a cipher block - but with
  29. * SHA-256, even that shouldn't be a worry, because the output
  30. * 'blocks' are twice the size, and also SHA-256 has no guarantee of
  31. * bijectivity, so it surely _could_ be possible to generate the same
  32. * block from two counter values. Thirdly, Fortuna has to have a hash
  33. * function anyway, for reseeding and entropy collection, so reusing
  34. * the same one means it only depends on one underlying primitive and
  35. * can be easily reinstantiated with a larger hash function if you
  36. * decide you'd like to do that on a particular occasion.
  37. */
  38. #define NCOLLECTORS 32
  39. #define RESEED_DATA_SIZE 64
  40. typedef struct prng_impl prng_impl;
  41. struct prng_impl {
  42. prng Prng;
  43. const ssh_hashalg *hashalg;
  44. /*
  45. * Generation side:
  46. *
  47. * 'generator' is a hash object with the current key preloaded
  48. * into it. The counter-mode generation is achieved by copying
  49. * that hash object, appending the counter value to the copy, and
  50. * calling ssh_hash_final.
  51. *
  52. * pending_output is a buffer of size equal to the hash length,
  53. * which receives each of those hashes as it's generated. The
  54. * bytes of the hash are returned in reverse order, just because
  55. * that made it marginally easier to deal with the
  56. * pending_output_remaining field.
  57. */
  58. ssh_hash *generator;
  59. mp_int *counter;
  60. uint8_t *pending_output;
  61. size_t pending_output_remaining;
  62. /*
  63. * When re-seeding the generator, you call prng_seed_begin(),
  64. * which sets up a hash object in 'keymaker'. You write your new
  65. * seed data into it (which you can do by calling put_data on the
  66. * PRNG object itself) and then call prng_seed_finish(), which
  67. * finalises this hash and uses the output to set up the new
  68. * generator.
  69. *
  70. * The keymaker hash preimage includes the previous key, so if you
  71. * just want to change keys for the sake of not keeping the same
  72. * one for too long, you don't have to put any extra seed data in
  73. * at all.
  74. */
  75. ssh_hash *keymaker;
  76. /*
  77. * Collection side:
  78. *
  79. * There are NCOLLECTORS hash objects collecting entropy. Each
  80. * separately numbered entropy source puts its output into those
  81. * hash objects in the order 0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,4,...,
  82. * that is to say, each entropy source has a separate counter
  83. * which is incremented every time that source generates an event,
  84. * and the event data is added to the collector corresponding to
  85. * the index of the lowest set bit in the current counter value.
  86. *
  87. * Whenever collector #0 has at least RESEED_DATA_SIZE bytes (and
  88. * it's not at least 100ms since the last reseed), the PRNG is
  89. * reseeded, with seed data on reseed #n taken from the first j
  90. * collectors, where j is one more than the number of factors of 2
  91. * in n. That is, collector #0 is used in every reseed; #1 in
  92. * every other one, #2 in every fourth, etc.
  93. *
  94. * 'until_reseed' counts the amount of data that still needs to be
  95. * added to collector #0 before a reseed will be triggered.
  96. */
  97. uint32_t source_counters[NOISE_MAX_SOURCES];
  98. ssh_hash *collectors[NCOLLECTORS];
  99. size_t until_reseed;
  100. uint32_t reseeds;
  101. uint64_t last_reseed_time;
  102. };
  103. static void prng_seed_BinarySink_write(
  104. BinarySink *bs, const void *data, size_t len);
  105. prng *prng_new(const ssh_hashalg *hashalg)
  106. {
  107. prng_impl *pi = snew(prng_impl);
  108. memset(pi, 0, sizeof(prng_impl));
  109. pi->hashalg = hashalg;
  110. pi->keymaker = NULL;
  111. pi->generator = NULL;
  112. pi->pending_output = snewn(pi->hashalg->hlen, uint8_t);
  113. pi->pending_output_remaining = 0;
  114. pi->counter = mp_new(128);
  115. { // WINSCP
  116. size_t i; // WINSCP
  117. for (i = 0; i < NCOLLECTORS; i++)
  118. pi->collectors[i] = ssh_hash_new(pi->hashalg);
  119. pi->until_reseed = 0;
  120. BinarySink_INIT(&pi->Prng, prng_seed_BinarySink_write);
  121. pi->Prng.savesize = pi->hashalg->hlen * 4;
  122. return &pi->Prng;
  123. } // WINSCP
  124. }
  125. void prng_free(prng *pr)
  126. {
  127. prng_impl *pi = container_of(pr, prng_impl, Prng);
  128. sfree(pi->pending_output);
  129. mp_free(pi->counter);
  130. { // WINSCP
  131. size_t i; // WINSCP
  132. for (i = 0; i < NCOLLECTORS; i++)
  133. ssh_hash_free(pi->collectors[i]);
  134. if (pi->generator)
  135. ssh_hash_free(pi->generator);
  136. if (pi->keymaker)
  137. ssh_hash_free(pi->keymaker);
  138. smemclr(pi, sizeof(*pi));
  139. sfree(pi);
  140. } // WINSCP
  141. }
  142. void prng_seed_begin(prng *pr)
  143. {
  144. prng_impl *pi = container_of(pr, prng_impl, Prng);
  145. assert(!pi->keymaker);
  146. prngdebug("prng: reseed begin\n");
  147. /*
  148. * Make a hash instance that will generate the key for the new one.
  149. */
  150. if (pi->generator) {
  151. pi->keymaker = pi->generator;
  152. pi->generator = NULL;
  153. } else {
  154. pi->keymaker = ssh_hash_new(pi->hashalg);
  155. }
  156. put_byte(pi->keymaker, 'R');
  157. }
  158. static void prng_seed_BinarySink_write(
  159. BinarySink *bs, const void *data, size_t len)
  160. {
  161. prng *pr = BinarySink_DOWNCAST(bs, prng);
  162. prng_impl *pi = container_of(pr, prng_impl, Prng);
  163. assert(pi->keymaker);
  164. prngdebug("prng: got %zu bytes of seed\n", len);
  165. put_data(pi->keymaker, data, len);
  166. }
  167. void prng_seed_finish(prng *pr)
  168. {
  169. prng_impl *pi = container_of(pr, prng_impl, Prng);
  170. assert(pi->keymaker);
  171. prngdebug("prng: reseed finish\n");
  172. /*
  173. * Actually generate the key.
  174. */
  175. ssh_hash_final(pi->keymaker, pi->pending_output);
  176. pi->keymaker = NULL;
  177. /*
  178. * Load that key into a fresh hash instance, which will become the
  179. * new generator.
  180. */
  181. assert(!pi->generator);
  182. pi->generator = ssh_hash_new(pi->hashalg);
  183. put_data(pi->generator, pi->pending_output, pi->hashalg->hlen);
  184. smemclr(pi->pending_output, pi->hashalg->hlen);
  185. pi->until_reseed = RESEED_DATA_SIZE;
  186. pi->last_reseed_time = prng_reseed_time_ms();
  187. pi->pending_output_remaining = 0;
  188. }
  189. static inline void prng_generate(prng_impl *pi)
  190. {
  191. ssh_hash *h = ssh_hash_copy(pi->generator);
  192. prngdebug("prng_generate\n");
  193. put_byte(h, 'G');
  194. put_mp_ssh2(h, pi->counter);
  195. mp_add_integer_into(pi->counter, pi->counter, 1);
  196. ssh_hash_final(h, pi->pending_output);
  197. pi->pending_output_remaining = pi->hashalg->hlen;
  198. }
  199. void prng_read(prng *pr, void *vout, size_t size)
  200. {
  201. prng_impl *pi = container_of(pr, prng_impl, Prng);
  202. assert(!pi->keymaker);
  203. prngdebug("prng_read %zu\n", size);
  204. { // WINSCP
  205. uint8_t *out = (uint8_t *)vout;
  206. for (; size > 0; size--) {
  207. if (pi->pending_output_remaining == 0)
  208. prng_generate(pi);
  209. pi->pending_output_remaining--;
  210. *out++ = pi->pending_output[pi->pending_output_remaining];
  211. pi->pending_output[pi->pending_output_remaining] = 0;
  212. }
  213. prng_seed_begin(&pi->Prng);
  214. prng_seed_finish(&pi->Prng);
  215. } // WINSCP
  216. }
  217. void prng_add_entropy(prng *pr, unsigned source_id, ptrlen data)
  218. {
  219. prng_impl *pi = container_of(pr, prng_impl, Prng);
  220. pinitassert(source_id < NOISE_MAX_SOURCES);
  221. uint32_t counter = ++pi->source_counters[source_id];
  222. size_t index = 0;
  223. while (index+1 < NCOLLECTORS && !(counter & 1)) {
  224. counter >>= 1;
  225. index++;
  226. }
  227. prngdebug("prng_add_entropy source=%u size=%zu -> collector %zi\n",
  228. source_id, data.len, index);
  229. put_datapl(pi->collectors[index], data);
  230. if (index == 0)
  231. pi->until_reseed = (pi->until_reseed < data.len ? 0 :
  232. pi->until_reseed - data.len);
  233. if (pi->until_reseed == 0 &&
  234. prng_reseed_time_ms() - pi->last_reseed_time >= 100) {
  235. prng_seed_begin(&pi->Prng);
  236. { // WINSCP
  237. uint32_t reseed_index = ++pi->reseeds;
  238. prngdebug("prng entropy reseed #%"PRIu32"\n", reseed_index);
  239. { // WINSCP
  240. size_t i; // WINSCP
  241. for (i = 0; i < NCOLLECTORS; i++) {
  242. prngdebug("emptying collector %zu\n", i);
  243. ssh_hash_final(pi->collectors[i], pi->pending_output);
  244. put_data(&pi->Prng, pi->pending_output, pi->hashalg->hlen);
  245. pi->collectors[i] = ssh_hash_new(pi->hashalg);
  246. if (reseed_index & 1)
  247. break;
  248. reseed_index >>= 1;
  249. }
  250. prng_seed_finish(&pi->Prng);
  251. } // WINSCP
  252. } // WINSCP
  253. }
  254. }
  255. size_t prng_seed_bits(prng *pr)
  256. {
  257. prng_impl *pi = container_of(pr, prng_impl, Prng);
  258. return pi->hashalg->hlen * 8;
  259. }