sshrand.c 9.6 KB

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