On Windows, character set specifications of the form 'IBM437' would never have
[u/mdw/putty] / sshrand.c
CommitLineData
374330e2 1/*
2 * cryptographic random number generator for PuTTY's ssh client
3 */
4
19ad0a17 5#include "putty.h"
374330e2 6#include "ssh.h"
7
5d17ccfc 8/* Collect environmental noise every 5 minutes */
9#define NOISE_REGULAR_INTERVAL (5*60*TICKSPERSEC)
10
374330e2 11void noise_get_heavy(void (*func) (void *, int));
12void noise_get_light(void (*func) (void *, int));
13
14/*
15 * `pool' itself is a pool of random data which we actually use: we
16 * return bytes from `pool', at position `poolpos', until `poolpos'
17 * reaches the end of the pool. At this point we generate more
18 * random data, by adding noise, stirring well, and resetting
19 * `poolpos' to point to just past the beginning of the pool (not
20 * _the_ beginning, since otherwise we'd give away the whole
21 * contents of our pool, and attackers would just have to guess the
22 * next lot of noise).
23 *
24 * `incomingb' buffers acquired noise data, until it gets full, at
25 * which point the acquired noise is SHA'ed into `incoming' and
26 * `incomingb' is cleared. The noise in `incoming' is used as part
27 * of the noise for each stirring of the pool, in addition to local
28 * time, process listings, and other such stuff.
29 */
30
31#define HASHINPUT 64 /* 64 bytes SHA input */
32#define HASHSIZE 20 /* 160 bits SHA output */
33#define POOLSIZE 1200 /* size of random pool */
34
35struct RandPool {
36 unsigned char pool[POOLSIZE];
37 int poolpos;
38
39 unsigned char incoming[HASHSIZE];
40
41 unsigned char incomingb[HASHINPUT];
42 int incomingpos;
ab076e02 43
44 int stir_pending;
374330e2 45};
46
47static struct RandPool pool;
93b581bd 48int random_active = 0;
5d17ccfc 49long next_noise_collection;
374330e2 50
19ad0a17 51static void random_stir(void)
32874aea 52{
53 word32 block[HASHINPUT / sizeof(word32)];
54 word32 digest[HASHSIZE / sizeof(word32)];
374330e2 55 int i, j, k;
56
ab076e02 57 /*
58 * noise_get_light will call random_add_noise, which may call
59 * back to here. Prevent recursive stirs.
60 */
61 if (pool.stir_pending)
62 return;
63 pool.stir_pending = TRUE;
64
374330e2 65 noise_get_light(random_add_noise);
66
32874aea 67 SHATransform((word32 *) pool.incoming, (word32 *) pool.incomingb);
374330e2 68 pool.incomingpos = 0;
69
70 /*
71 * Chunks of this code are blatantly endianness-dependent, but
72 * as it's all random bits anyway, WHO CARES?
73 */
74 memcpy(digest, pool.incoming, sizeof(digest));
75
76 /*
77 * Make two passes over the pool.
78 */
79 for (i = 0; i < 2; i++) {
80
81 /*
82 * We operate SHA in CFB mode, repeatedly adding the same
83 * block of data to the digest. But we're also fiddling
84 * with the digest-so-far, so this shouldn't be Bad or
85 * anything.
86 */
87 memcpy(block, pool.pool, sizeof(block));
88
89 /*
90 * Each pass processes the pool backwards in blocks of
91 * HASHSIZE, just so that in general we get the output of
92 * SHA before the corresponding input, in the hope that
93 * things will be that much less predictable that way
94 * round, when we subsequently return bytes ...
95 */
32874aea 96 for (j = POOLSIZE; (j -= HASHSIZE) >= 0;) {
374330e2 97 /*
98 * XOR the bit of the pool we're processing into the
99 * digest.
100 */
101
32874aea 102 for (k = 0; k < sizeof(digest) / sizeof(*digest); k++)
103 digest[k] ^= ((word32 *) (pool.pool + j))[k];
374330e2 104
105 /*
106 * Munge our unrevealed first block of the pool into
107 * it.
108 */
109 SHATransform(digest, block);
110
111 /*
112 * Stick the result back into the pool.
113 */
114
32874aea 115 for (k = 0; k < sizeof(digest) / sizeof(*digest); k++)
116 ((word32 *) (pool.pool + j))[k] = digest[k];
374330e2 117 }
118 }
119
120 /*
121 * Might as well save this value back into `incoming', just so
122 * there'll be some extra bizarreness there.
123 */
124 SHATransform(digest, block);
a1a27668 125 memcpy(pool.incoming, digest, sizeof(digest));
374330e2 126
127 pool.poolpos = sizeof(pool.incoming);
ab076e02 128
129 pool.stir_pending = FALSE;
374330e2 130}
131
32874aea 132void random_add_noise(void *noise, int length)
133{
374330e2 134 unsigned char *p = noise;
6e522441 135 int i;
374330e2 136
7d6ee6ff 137 if (!random_active)
32874aea 138 return;
7d6ee6ff 139
6e522441 140 /*
141 * This function processes HASHINPUT bytes into only HASHSIZE
142 * bytes, so _if_ we were getting incredibly high entropy
143 * sources then we would be throwing away valuable stuff.
144 */
145 while (length >= (HASHINPUT - pool.incomingpos)) {
146 memcpy(pool.incomingb + pool.incomingpos, p,
147 HASHINPUT - pool.incomingpos);
148 p += HASHINPUT - pool.incomingpos;
149 length -= HASHINPUT - pool.incomingpos;
32874aea 150 SHATransform((word32 *) pool.incoming, (word32 *) pool.incomingb);
151 for (i = 0; i < HASHSIZE; i++) {
152 pool.pool[pool.poolpos++] ^= pool.incomingb[i];
153 if (pool.poolpos >= POOLSIZE)
154 pool.poolpos = 0;
155 }
156 if (pool.poolpos < HASHSIZE)
157 random_stir();
6e522441 158
159 pool.incomingpos = 0;
160 }
161
162 memcpy(pool.incomingb + pool.incomingpos, p, length);
163 pool.incomingpos += length;
164}
165
32874aea 166void random_add_heavynoise(void *noise, int length)
167{
6e522441 168 unsigned char *p = noise;
169 int i;
170
171 while (length >= POOLSIZE) {
32874aea 172 for (i = 0; i < POOLSIZE; i++)
173 pool.pool[i] ^= *p++;
6e522441 174 random_stir();
175 length -= POOLSIZE;
176 }
177
178 for (i = 0; i < length; i++)
32874aea 179 pool.pool[i] ^= *p++;
6e522441 180 random_stir();
181}
182
32874aea 183static void random_add_heavynoise_bitbybit(void *noise, int length)
184{
6e522441 185 unsigned char *p = noise;
186 int i;
187
188 while (length >= POOLSIZE - pool.poolpos) {
32874aea 189 for (i = 0; i < POOLSIZE - pool.poolpos; i++)
190 pool.pool[pool.poolpos + i] ^= *p++;
374330e2 191 random_stir();
6e522441 192 length -= POOLSIZE - pool.poolpos;
32874aea 193 pool.poolpos = 0;
374330e2 194 }
195
6e522441 196 for (i = 0; i < length; i++)
32874aea 197 pool.pool[i] ^= *p++;
6e522441 198 pool.poolpos = i;
374330e2 199}
200
5d17ccfc 201static void random_timer(void *ctx, long now)
202{
203 if (random_active > 0 && now - next_noise_collection >= 0) {
204 noise_regular();
205 next_noise_collection =
206 schedule_timer(NOISE_REGULAR_INTERVAL, random_timer, &pool);
207 }
208}
209
210void random_ref(void)
32874aea 211{
bc0e1c6c 212 if (!random_active) {
213 memset(&pool, 0, sizeof(pool)); /* just to start with */
374330e2 214
bc0e1c6c 215 noise_get_heavy(random_add_heavynoise_bitbybit);
216 random_stir();
5d17ccfc 217
218 next_noise_collection =
219 schedule_timer(NOISE_REGULAR_INTERVAL, random_timer, &pool);
bc0e1c6c 220 }
5d17ccfc 221
222 random_active++;
223}
224
225void random_unref(void)
226{
227 random_active--;
374330e2 228}
229
32874aea 230int random_byte(void)
231{
374330e2 232 if (pool.poolpos >= POOLSIZE)
233 random_stir();
234
235 return pool.pool[pool.poolpos++];
236}
237
32874aea 238void random_get_savedata(void **data, int *len)
239{
3d88e64d 240 void *buf = snewn(POOLSIZE / 2, char);
374330e2 241 random_stir();
e3ac3c05 242 memcpy(buf, pool.pool + pool.poolpos, POOLSIZE / 2);
32874aea 243 *len = POOLSIZE / 2;
e3ac3c05 244 *data = buf;
245 random_stir();
374330e2 246}