sshrand.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. /*
  2. * cryptographic random number generator for PuTTY's ssh client
  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. void noise_get_heavy(void (*func) (void *, int));
  10. void noise_get_light(void (*func) (void *, int));
  11. /*
  12. * `pool' itself is a pool of random data which we actually use: we
  13. * return bytes from `pool', at position `poolpos', until `poolpos'
  14. * reaches the end of the pool. At this point we generate more
  15. * random data, by adding noise, stirring well, and resetting
  16. * `poolpos' to point to just past the beginning of the pool (not
  17. * _the_ beginning, since otherwise we'd give away the whole
  18. * contents of our pool, and attackers would just have to guess the
  19. * next lot of noise).
  20. *
  21. * `incomingb' buffers acquired noise data, until it gets full, at
  22. * which point the acquired noise is SHA'ed into `incoming' and
  23. * `incomingb' is cleared. The noise in `incoming' is used as part
  24. * of the noise for each stirring of the pool, in addition to local
  25. * time, process listings, and other such stuff.
  26. */
  27. #define HASHINPUT 64 /* 64 bytes SHA input */
  28. #define HASHSIZE 20 /* 160 bits SHA output */
  29. #define POOLSIZE 1200 /* size of random pool */
  30. struct RandPool {
  31. unsigned char pool[POOLSIZE];
  32. int poolpos;
  33. unsigned char incoming[HASHSIZE];
  34. unsigned char incomingb[HASHINPUT];
  35. int incomingpos;
  36. int stir_pending;
  37. };
  38. int random_active = 0;
  39. #ifdef FUZZING
  40. /*
  41. * Special dummy version of the RNG for use when fuzzing.
  42. */
  43. void random_add_noise(void *noise, int length) { }
  44. void random_add_heavynoise(void *noise, int length) { }
  45. void random_ref(void) { }
  46. void random_unref(void) { }
  47. int random_byte(void)
  48. {
  49. return 0x45; /* Chosen by eight fair coin tosses */
  50. }
  51. void random_get_savedata(void **data, int *len) { }
  52. #else /* !FUZZING */
  53. static struct RandPool pool;
  54. long next_noise_collection;
  55. #ifdef RANDOM_DIAGNOSTICS
  56. int random_diagnostics = 0;
  57. #endif
  58. static void random_stir(void)
  59. {
  60. word32 block[HASHINPUT / sizeof(word32)];
  61. word32 digest[HASHSIZE / sizeof(word32)];
  62. int i, j, k;
  63. /*
  64. * noise_get_light will call random_add_noise, which may call
  65. * back to here. Prevent recursive stirs.
  66. */
  67. if (pool.stir_pending)
  68. return;
  69. pool.stir_pending = TRUE;
  70. noise_get_light(random_add_noise);
  71. #ifdef RANDOM_DIAGNOSTICS
  72. {
  73. int p, q;
  74. printf("random stir starting\npool:\n");
  75. for (p = 0; p < POOLSIZE; p += HASHSIZE) {
  76. printf(" ");
  77. for (q = 0; q < HASHSIZE; q += 4) {
  78. printf(" %08x", *(word32 *)(pool.pool + p + q));
  79. }
  80. printf("\n");
  81. }
  82. printf("incoming:\n ");
  83. for (q = 0; q < HASHSIZE; q += 4) {
  84. printf(" %08x", *(word32 *)(pool.incoming + q));
  85. }
  86. printf("\nincomingb:\n ");
  87. for (q = 0; q < HASHINPUT; q += 4) {
  88. printf(" %08x", *(word32 *)(pool.incomingb + q));
  89. }
  90. printf("\n");
  91. random_diagnostics++;
  92. }
  93. #endif
  94. SHATransform((word32 *) pool.incoming, (word32 *) pool.incomingb);
  95. pool.incomingpos = 0;
  96. /*
  97. * Chunks of this code are blatantly endianness-dependent, but
  98. * as it's all random bits anyway, WHO CARES?
  99. */
  100. memcpy(digest, pool.incoming, sizeof(digest));
  101. /*
  102. * Make two passes over the pool.
  103. */
  104. for (i = 0; i < 2; i++) {
  105. /*
  106. * We operate SHA in CFB mode, repeatedly adding the same
  107. * block of data to the digest. But we're also fiddling
  108. * with the digest-so-far, so this shouldn't be Bad or
  109. * anything.
  110. */
  111. memcpy(block, pool.pool, sizeof(block));
  112. /*
  113. * Each pass processes the pool backwards in blocks of
  114. * HASHSIZE, just so that in general we get the output of
  115. * SHA before the corresponding input, in the hope that
  116. * things will be that much less predictable that way
  117. * round, when we subsequently return bytes ...
  118. */
  119. for (j = POOLSIZE; (j -= HASHSIZE) >= 0;) {
  120. /*
  121. * XOR the bit of the pool we're processing into the
  122. * digest.
  123. */
  124. for (k = 0; k < sizeof(digest) / sizeof(*digest); k++)
  125. digest[k] ^= ((word32 *) (pool.pool + j))[k];
  126. /*
  127. * Munge our unrevealed first block of the pool into
  128. * it.
  129. */
  130. SHATransform(digest, block);
  131. /*
  132. * Stick the result back into the pool.
  133. */
  134. for (k = 0; k < sizeof(digest) / sizeof(*digest); k++)
  135. ((word32 *) (pool.pool + j))[k] = digest[k];
  136. }
  137. #ifdef RANDOM_DIAGNOSTICS
  138. if (i == 0) {
  139. int p, q;
  140. printf("random stir midpoint\npool:\n");
  141. for (p = 0; p < POOLSIZE; p += HASHSIZE) {
  142. printf(" ");
  143. for (q = 0; q < HASHSIZE; q += 4) {
  144. printf(" %08x", *(word32 *)(pool.pool + p + q));
  145. }
  146. printf("\n");
  147. }
  148. printf("incoming:\n ");
  149. for (q = 0; q < HASHSIZE; q += 4) {
  150. printf(" %08x", *(word32 *)(pool.incoming + q));
  151. }
  152. printf("\nincomingb:\n ");
  153. for (q = 0; q < HASHINPUT; q += 4) {
  154. printf(" %08x", *(word32 *)(pool.incomingb + q));
  155. }
  156. printf("\n");
  157. }
  158. #endif
  159. }
  160. /*
  161. * Might as well save this value back into `incoming', just so
  162. * there'll be some extra bizarreness there.
  163. */
  164. SHATransform(digest, block);
  165. memcpy(pool.incoming, digest, sizeof(digest));
  166. pool.poolpos = sizeof(pool.incoming);
  167. pool.stir_pending = FALSE;
  168. #ifdef RANDOM_DIAGNOSTICS
  169. {
  170. int p, q;
  171. printf("random stir done\npool:\n");
  172. for (p = 0; p < POOLSIZE; p += HASHSIZE) {
  173. printf(" ");
  174. for (q = 0; q < HASHSIZE; q += 4) {
  175. printf(" %08x", *(word32 *)(pool.pool + p + q));
  176. }
  177. printf("\n");
  178. }
  179. printf("incoming:\n ");
  180. for (q = 0; q < HASHSIZE; q += 4) {
  181. printf(" %08x", *(word32 *)(pool.incoming + q));
  182. }
  183. printf("\nincomingb:\n ");
  184. for (q = 0; q < HASHINPUT; q += 4) {
  185. printf(" %08x", *(word32 *)(pool.incomingb + q));
  186. }
  187. printf("\n");
  188. random_diagnostics--;
  189. }
  190. #endif
  191. }
  192. void random_add_noise(void *noise, int length)
  193. {
  194. unsigned char *p = noise;
  195. int i;
  196. if (!random_active)
  197. return;
  198. /*
  199. * This function processes HASHINPUT bytes into only HASHSIZE
  200. * bytes, so _if_ we were getting incredibly high entropy
  201. * sources then we would be throwing away valuable stuff.
  202. */
  203. while (length >= (HASHINPUT - pool.incomingpos)) {
  204. memcpy(pool.incomingb + pool.incomingpos, p,
  205. HASHINPUT - pool.incomingpos);
  206. p += HASHINPUT - pool.incomingpos;
  207. length -= HASHINPUT - pool.incomingpos;
  208. SHATransform((word32 *) pool.incoming, (word32 *) pool.incomingb);
  209. for (i = 0; i < HASHSIZE; i++) {
  210. if (pool.poolpos >= POOLSIZE)
  211. pool.poolpos = 0;
  212. pool.pool[pool.poolpos++] ^= pool.incoming[i];
  213. if (pool.poolpos >= POOLSIZE)
  214. pool.poolpos = 0;
  215. }
  216. if (pool.poolpos < HASHSIZE)
  217. random_stir();
  218. pool.incomingpos = 0;
  219. }
  220. #ifdef MPEXT
  221. if (length > 0)
  222. #endif
  223. memcpy(pool.incomingb + pool.incomingpos, p, length);
  224. pool.incomingpos += length;
  225. }
  226. void random_add_heavynoise(void *noise, int length)
  227. {
  228. unsigned char *p = noise;
  229. int i;
  230. while (length >= POOLSIZE) {
  231. for (i = 0; i < POOLSIZE; i++)
  232. pool.pool[i] ^= *p++;
  233. random_stir();
  234. length -= POOLSIZE;
  235. }
  236. for (i = 0; i < length; i++)
  237. pool.pool[i] ^= *p++;
  238. random_stir();
  239. }
  240. static void random_add_heavynoise_bitbybit(void *noise, int length)
  241. {
  242. unsigned char *p = noise;
  243. int i;
  244. while (length >= POOLSIZE - pool.poolpos) {
  245. for (i = 0; i < POOLSIZE - pool.poolpos; i++)
  246. pool.pool[pool.poolpos + i] ^= *p++;
  247. random_stir();
  248. length -= POOLSIZE - pool.poolpos;
  249. pool.poolpos = 0;
  250. }
  251. for (i = 0; i < length; i++)
  252. pool.pool[i] ^= *p++;
  253. pool.poolpos = i;
  254. }
  255. static void random_timer(void *ctx, unsigned long now)
  256. {
  257. if (random_active > 0 && now == next_noise_collection) {
  258. noise_regular();
  259. next_noise_collection =
  260. schedule_timer(NOISE_REGULAR_INTERVAL, random_timer, &pool);
  261. }
  262. }
  263. void random_ref(void)
  264. {
  265. MPEXT_PUTTY_SECTION_ENTER;
  266. if (!random_active) {
  267. memset(&pool, 0, sizeof(pool)); /* just to start with */
  268. noise_get_heavy(random_add_heavynoise_bitbybit);
  269. random_stir();
  270. next_noise_collection =
  271. schedule_timer(NOISE_REGULAR_INTERVAL, random_timer, &pool);
  272. }
  273. random_active++;
  274. MPEXT_PUTTY_SECTION_LEAVE;
  275. }
  276. void random_unref(void)
  277. {
  278. MPEXT_PUTTY_SECTION_ENTER;
  279. assert(random_active > 0);
  280. if (random_active == 1) {
  281. #ifndef MPEXT
  282. // We control this on our own in PuttyFinalize()
  283. random_save_seed();
  284. #endif
  285. expire_timer_context(&pool);
  286. }
  287. random_active--;
  288. MPEXT_PUTTY_SECTION_LEAVE;
  289. }
  290. int random_byte(void)
  291. {
  292. #ifdef MPEXT
  293. int pos;
  294. assert(random_active);
  295. pos = pool.poolpos;
  296. if (pos < sizeof(pool.incoming) || pos >= POOLSIZE)
  297. {
  298. MPEXT_PUTTY_SECTION_ENTER;
  299. if (pool.poolpos >= POOLSIZE)
  300. {
  301. random_stir();
  302. }
  303. pos = pool.poolpos;
  304. pool.poolpos++;
  305. MPEXT_PUTTY_SECTION_LEAVE;
  306. }
  307. else
  308. {
  309. pool.poolpos++;
  310. }
  311. return pool.pool[pos];
  312. #else
  313. assert(random_active);
  314. if (pool.poolpos >= POOLSIZE)
  315. random_stir();
  316. return pool.pool[pool.poolpos++];
  317. #endif
  318. }
  319. void random_get_savedata(void **data, int *len)
  320. {
  321. void *buf = snewn(POOLSIZE / 2, char);
  322. MPEXT_PUTTY_SECTION_ENTER;
  323. random_stir();
  324. memcpy(buf, pool.pool + pool.poolpos, POOLSIZE / 2);
  325. *len = POOLSIZE / 2;
  326. *data = buf;
  327. random_stir();
  328. MPEXT_PUTTY_SECTION_LEAVE;
  329. }
  330. #endif