]> asedeno.scripts.mit.edu Git - PuTTY.git/blob - sshrand.c
Loose end from timing shakeup: sshrand.c is now a client of
[PuTTY.git] / sshrand.c
1 /*
2  * cryptographic random number generator for PuTTY's ssh client
3  */
4
5 #include "putty.h"
6 #include "ssh.h"
7
8 /* Collect environmental noise every 5 minutes */
9 #define NOISE_REGULAR_INTERVAL (5*60*TICKSPERSEC)
10
11 void noise_get_heavy(void (*func) (void *, int));
12 void 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
35 struct RandPool {
36     unsigned char pool[POOLSIZE];
37     int poolpos;
38
39     unsigned char incoming[HASHSIZE];
40
41     unsigned char incomingb[HASHINPUT];
42     int incomingpos;
43 };
44
45 static struct RandPool pool;
46 int random_active = 0;
47 long next_noise_collection;
48
49 static void random_stir(void)
50 {
51     word32 block[HASHINPUT / sizeof(word32)];
52     word32 digest[HASHSIZE / sizeof(word32)];
53     int i, j, k;
54
55     noise_get_light(random_add_noise);
56
57     SHATransform((word32 *) pool.incoming, (word32 *) pool.incomingb);
58     pool.incomingpos = 0;
59
60     /*
61      * Chunks of this code are blatantly endianness-dependent, but
62      * as it's all random bits anyway, WHO CARES?
63      */
64     memcpy(digest, pool.incoming, sizeof(digest));
65
66     /*
67      * Make two passes over the pool.
68      */
69     for (i = 0; i < 2; i++) {
70
71         /*
72          * We operate SHA in CFB mode, repeatedly adding the same
73          * block of data to the digest. But we're also fiddling
74          * with the digest-so-far, so this shouldn't be Bad or
75          * anything.
76          */
77         memcpy(block, pool.pool, sizeof(block));
78
79         /*
80          * Each pass processes the pool backwards in blocks of
81          * HASHSIZE, just so that in general we get the output of
82          * SHA before the corresponding input, in the hope that
83          * things will be that much less predictable that way
84          * round, when we subsequently return bytes ...
85          */
86         for (j = POOLSIZE; (j -= HASHSIZE) >= 0;) {
87             /*
88              * XOR the bit of the pool we're processing into the
89              * digest.
90              */
91
92             for (k = 0; k < sizeof(digest) / sizeof(*digest); k++)
93                 digest[k] ^= ((word32 *) (pool.pool + j))[k];
94
95             /*
96              * Munge our unrevealed first block of the pool into
97              * it.
98              */
99             SHATransform(digest, block);
100
101             /*
102              * Stick the result back into the pool.
103              */
104
105             for (k = 0; k < sizeof(digest) / sizeof(*digest); k++)
106                 ((word32 *) (pool.pool + j))[k] = digest[k];
107         }
108     }
109
110     /*
111      * Might as well save this value back into `incoming', just so
112      * there'll be some extra bizarreness there.
113      */
114     SHATransform(digest, block);
115     memcpy(pool.incoming, digest, sizeof(digest));
116
117     pool.poolpos = sizeof(pool.incoming);
118 }
119
120 void random_add_noise(void *noise, int length)
121 {
122     unsigned char *p = noise;
123     int i;
124
125     if (!random_active)
126         return;
127
128     /*
129      * This function processes HASHINPUT bytes into only HASHSIZE
130      * bytes, so _if_ we were getting incredibly high entropy
131      * sources then we would be throwing away valuable stuff.
132      */
133     while (length >= (HASHINPUT - pool.incomingpos)) {
134         memcpy(pool.incomingb + pool.incomingpos, p,
135                HASHINPUT - pool.incomingpos);
136         p += HASHINPUT - pool.incomingpos;
137         length -= HASHINPUT - pool.incomingpos;
138         SHATransform((word32 *) pool.incoming, (word32 *) pool.incomingb);
139         for (i = 0; i < HASHSIZE; i++) {
140             pool.pool[pool.poolpos++] ^= pool.incomingb[i];
141             if (pool.poolpos >= POOLSIZE)
142                 pool.poolpos = 0;
143         }
144         if (pool.poolpos < HASHSIZE)
145             random_stir();
146
147         pool.incomingpos = 0;
148     }
149
150     memcpy(pool.incomingb + pool.incomingpos, p, length);
151     pool.incomingpos += length;
152 }
153
154 void random_add_heavynoise(void *noise, int length)
155 {
156     unsigned char *p = noise;
157     int i;
158
159     while (length >= POOLSIZE) {
160         for (i = 0; i < POOLSIZE; i++)
161             pool.pool[i] ^= *p++;
162         random_stir();
163         length -= POOLSIZE;
164     }
165
166     for (i = 0; i < length; i++)
167         pool.pool[i] ^= *p++;
168     random_stir();
169 }
170
171 static void random_add_heavynoise_bitbybit(void *noise, int length)
172 {
173     unsigned char *p = noise;
174     int i;
175
176     while (length >= POOLSIZE - pool.poolpos) {
177         for (i = 0; i < POOLSIZE - pool.poolpos; i++)
178             pool.pool[pool.poolpos + i] ^= *p++;
179         random_stir();
180         length -= POOLSIZE - pool.poolpos;
181         pool.poolpos = 0;
182     }
183
184     for (i = 0; i < length; i++)
185         pool.pool[i] ^= *p++;
186     pool.poolpos = i;
187 }
188
189 static void random_timer(void *ctx, long now)
190 {
191     if (random_active > 0 && now - next_noise_collection >= 0) {
192         noise_regular();
193         next_noise_collection =
194             schedule_timer(NOISE_REGULAR_INTERVAL, random_timer, &pool);
195     }
196 }
197
198 void random_ref(void)
199 {
200     if (!random_active) {
201         memset(&pool, 0, sizeof(pool));    /* just to start with */
202
203         noise_get_heavy(random_add_heavynoise_bitbybit);
204         random_stir();
205
206         next_noise_collection =
207             schedule_timer(NOISE_REGULAR_INTERVAL, random_timer, &pool);
208     }
209
210     random_active++;
211 }
212
213 void random_unref(void)
214 {
215     random_active--;
216 }
217
218 int random_byte(void)
219 {
220     if (pool.poolpos >= POOLSIZE)
221         random_stir();
222
223     return pool.pool[pool.poolpos++];
224 }
225
226 void random_get_savedata(void **data, int *len)
227 {
228     void *buf = snewn(POOLSIZE / 2, char);
229     random_stir();
230     memcpy(buf, pool.pool + pool.poolpos, POOLSIZE / 2);
231     *len = POOLSIZE / 2;
232     *data = buf;
233     random_stir();
234 }