sshrand.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. * cryptographic random number generator for PuTTY's ssh client
  3. */
  4. #include "ssh.h"
  5. void noise_get_heavy(void (*func) (void *, int));
  6. void noise_get_light(void (*func) (void *, int));
  7. /*
  8. * `pool' itself is a pool of random data which we actually use: we
  9. * return bytes from `pool', at position `poolpos', until `poolpos'
  10. * reaches the end of the pool. At this point we generate more
  11. * random data, by adding noise, stirring well, and resetting
  12. * `poolpos' to point to just past the beginning of the pool (not
  13. * _the_ beginning, since otherwise we'd give away the whole
  14. * contents of our pool, and attackers would just have to guess the
  15. * next lot of noise).
  16. *
  17. * `incomingb' buffers acquired noise data, until it gets full, at
  18. * which point the acquired noise is SHA'ed into `incoming' and
  19. * `incomingb' is cleared. The noise in `incoming' is used as part
  20. * of the noise for each stirring of the pool, in addition to local
  21. * time, process listings, and other such stuff.
  22. */
  23. #define HASHINPUT 64 /* 64 bytes SHA input */
  24. #define HASHSIZE 20 /* 160 bits SHA output */
  25. #define POOLSIZE 1200 /* size of random pool */
  26. struct RandPool {
  27. unsigned char pool[POOLSIZE];
  28. int poolpos;
  29. unsigned char incoming[HASHSIZE];
  30. unsigned char incomingb[HASHINPUT];
  31. int incomingpos;
  32. };
  33. static struct RandPool pool;
  34. void random_add_noise(void *noise, int length) {
  35. unsigned char *p = noise;
  36. while (length >= (HASHINPUT - pool.incomingpos)) {
  37. memcpy(pool.incomingb + pool.incomingpos, p,
  38. HASHINPUT - pool.incomingpos);
  39. p += HASHINPUT - pool.incomingpos;
  40. length -= HASHINPUT - pool.incomingpos;
  41. SHATransform((word32 *)pool.incoming, (word32 *)pool.incomingb);
  42. pool.incomingpos = 0;
  43. }
  44. memcpy(pool.incomingb + pool.incomingpos, p, length);
  45. pool.incomingpos += length;
  46. }
  47. void random_stir(void) {
  48. word32 block[HASHINPUT/sizeof(word32)];
  49. word32 digest[HASHSIZE/sizeof(word32)];
  50. int i, j, k;
  51. noise_get_light(random_add_noise);
  52. SHATransform((word32 *)pool.incoming, (word32 *)pool.incomingb);
  53. pool.incomingpos = 0;
  54. /*
  55. * Chunks of this code are blatantly endianness-dependent, but
  56. * as it's all random bits anyway, WHO CARES?
  57. */
  58. memcpy(digest, pool.incoming, sizeof(digest));
  59. /*
  60. * Make two passes over the pool.
  61. */
  62. for (i = 0; i < 2; i++) {
  63. /*
  64. * We operate SHA in CFB mode, repeatedly adding the same
  65. * block of data to the digest. But we're also fiddling
  66. * with the digest-so-far, so this shouldn't be Bad or
  67. * anything.
  68. */
  69. memcpy(block, pool.pool, sizeof(block));
  70. /*
  71. * Each pass processes the pool backwards in blocks of
  72. * HASHSIZE, just so that in general we get the output of
  73. * SHA before the corresponding input, in the hope that
  74. * things will be that much less predictable that way
  75. * round, when we subsequently return bytes ...
  76. */
  77. for (j = POOLSIZE; (j -= HASHSIZE) >= 0 ;) {
  78. /*
  79. * XOR the bit of the pool we're processing into the
  80. * digest.
  81. */
  82. for (k = 0; k < sizeof(digest)/sizeof(*digest); k++)
  83. digest[k] ^= ((word32 *)(pool.pool+j))[k];
  84. /*
  85. * Munge our unrevealed first block of the pool into
  86. * it.
  87. */
  88. SHATransform(digest, block);
  89. /*
  90. * Stick the result back into the pool.
  91. */
  92. for (k = 0; k < sizeof(digest)/sizeof(*digest); k++)
  93. ((word32 *)(pool.pool+j))[k] = digest[k];
  94. }
  95. }
  96. /*
  97. * Might as well save this value back into `incoming', just so
  98. * there'll be some extra bizarreness there.
  99. */
  100. SHATransform(digest, block);
  101. memcpy(pool.incoming, digest, sizeof(digest));
  102. pool.poolpos = sizeof(pool.incoming);
  103. }
  104. static void random_add_heavynoise(void *noise, int length) {
  105. unsigned char *p = noise;
  106. while (length >= (POOLSIZE - pool.poolpos)) {
  107. memcpy(pool.pool + pool.poolpos, p, POOLSIZE - pool.poolpos);
  108. p += POOLSIZE - pool.poolpos;
  109. length -= POOLSIZE - pool.poolpos;
  110. random_stir();
  111. pool.poolpos = 0;
  112. }
  113. memcpy(pool.pool + pool.poolpos, p, length);
  114. pool.poolpos += length;
  115. }
  116. void random_init(void) {
  117. memset(&pool, 0, sizeof(pool)); /* just to start with */
  118. /*
  119. * For noise_get_heavy, we temporarily use `poolpos' as the
  120. * pointer for addition of noise, rather than extraction of
  121. * random numbers.
  122. */
  123. pool.poolpos = 0;
  124. noise_get_heavy(random_add_heavynoise);
  125. random_stir();
  126. }
  127. int random_byte(void) {
  128. if (pool.poolpos >= POOLSIZE)
  129. random_stir();
  130. return pool.pool[pool.poolpos++];
  131. }
  132. void random_get_savedata(void **data, int *len) {
  133. random_stir();
  134. *data = pool.pool+pool.poolpos;
  135. *len = POOLSIZE/2;
  136. }