2 * cryptographic random number generator for PuTTY's ssh client
7 void noise_get_heavy(void (*func) (void *, int));
8 void noise_get_light(void (*func) (void *, int));
11 * `pool' itself is a pool of random data which we actually use: we
12 * return bytes from `pool', at position `poolpos', until `poolpos'
13 * reaches the end of the pool. At this point we generate more
14 * random data, by adding noise, stirring well, and resetting
15 * `poolpos' to point to just past the beginning of the pool (not
16 * _the_ beginning, since otherwise we'd give away the whole
17 * contents of our pool, and attackers would just have to guess the
20 * `incomingb' buffers acquired noise data, until it gets full, at
21 * which point the acquired noise is SHA'ed into `incoming' and
22 * `incomingb' is cleared. The noise in `incoming' is used as part
23 * of the noise for each stirring of the pool, in addition to local
24 * time, process listings, and other such stuff.
27 #define HASHINPUT 64 /* 64 bytes SHA input */
28 #define HASHSIZE 20 /* 160 bits SHA output */
29 #define POOLSIZE 1200 /* size of random pool */
32 unsigned char pool[POOLSIZE];
35 unsigned char incoming[HASHSIZE];
37 unsigned char incomingb[HASHINPUT];
41 static struct RandPool pool;
43 void random_stir(void) {
44 word32 block[HASHINPUT/sizeof(word32)];
45 word32 digest[HASHSIZE/sizeof(word32)];
48 noise_get_light(random_add_noise);
50 SHATransform((word32 *)pool.incoming, (word32 *)pool.incomingb);
54 * Chunks of this code are blatantly endianness-dependent, but
55 * as it's all random bits anyway, WHO CARES?
57 memcpy(digest, pool.incoming, sizeof(digest));
60 * Make two passes over the pool.
62 for (i = 0; i < 2; i++) {
65 * We operate SHA in CFB mode, repeatedly adding the same
66 * block of data to the digest. But we're also fiddling
67 * with the digest-so-far, so this shouldn't be Bad or
70 memcpy(block, pool.pool, sizeof(block));
73 * Each pass processes the pool backwards in blocks of
74 * HASHSIZE, just so that in general we get the output of
75 * SHA before the corresponding input, in the hope that
76 * things will be that much less predictable that way
77 * round, when we subsequently return bytes ...
79 for (j = POOLSIZE; (j -= HASHSIZE) >= 0 ;) {
81 * XOR the bit of the pool we're processing into the
85 for (k = 0; k < sizeof(digest)/sizeof(*digest); k++)
86 digest[k] ^= ((word32 *)(pool.pool+j))[k];
89 * Munge our unrevealed first block of the pool into
92 SHATransform(digest, block);
95 * Stick the result back into the pool.
98 for (k = 0; k < sizeof(digest)/sizeof(*digest); k++)
99 ((word32 *)(pool.pool+j))[k] = digest[k];
104 * Might as well save this value back into `incoming', just so
105 * there'll be some extra bizarreness there.
107 SHATransform(digest, block);
108 memcpy(pool.incoming, digest, sizeof(digest));
110 pool.poolpos = sizeof(pool.incoming);
113 void random_add_noise(void *noise, int length) {
114 unsigned char *p = noise;
118 * This function processes HASHINPUT bytes into only HASHSIZE
119 * bytes, so _if_ we were getting incredibly high entropy
120 * sources then we would be throwing away valuable stuff.
122 while (length >= (HASHINPUT - pool.incomingpos)) {
123 memcpy(pool.incomingb + pool.incomingpos, p,
124 HASHINPUT - pool.incomingpos);
125 p += HASHINPUT - pool.incomingpos;
126 length -= HASHINPUT - pool.incomingpos;
127 SHATransform((word32 *)pool.incoming, (word32 *)pool.incomingb);
128 for (i = 0; i < HASHSIZE; i++) {
129 pool.pool[pool.poolpos++] ^= pool.incomingb[i];
130 if (pool.poolpos >= POOLSIZE)
133 if (pool.poolpos < HASHSIZE)
136 pool.incomingpos = 0;
139 memcpy(pool.incomingb + pool.incomingpos, p, length);
140 pool.incomingpos += length;
143 void random_add_heavynoise(void *noise, int length) {
144 unsigned char *p = noise;
147 while (length >= POOLSIZE) {
148 for (i = 0; i < POOLSIZE; i++)
149 pool.pool[i] ^= *p++;
154 for (i = 0; i < length; i++)
155 pool.pool[i] ^= *p++;
159 static void random_add_heavynoise_bitbybit(void *noise, int length) {
160 unsigned char *p = noise;
163 while (length >= POOLSIZE - pool.poolpos) {
164 for (i = 0; i < POOLSIZE - pool.poolpos; i++)
165 pool.pool[pool.poolpos + i] ^= *p++;
167 length -= POOLSIZE - pool.poolpos;
171 for (i = 0; i < length; i++)
172 pool.pool[i] ^= *p++;
176 void random_init(void) {
177 memset(&pool, 0, sizeof(pool)); /* just to start with */
179 noise_get_heavy(random_add_heavynoise_bitbybit);
182 int random_byte(void) {
183 if (pool.poolpos >= POOLSIZE)
186 return pool.pool[pool.poolpos++];
189 void random_get_savedata(void **data, int *len) {
191 *data = pool.pool+pool.poolpos;