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 | |
8 | void noise_get_heavy(void (*func) (void *, int)); |
9 | void noise_get_light(void (*func) (void *, int)); |
10 | |
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 | |
28 | #define HASHINPUT 64 /* 64 bytes SHA input */ |
29 | #define HASHSIZE 20 /* 160 bits SHA output */ |
30 | #define POOLSIZE 1200 /* size of random pool */ |
31 | |
32 | struct RandPool { |
33 | unsigned char pool[POOLSIZE]; |
34 | int poolpos; |
35 | |
36 | unsigned char incoming[HASHSIZE]; |
37 | |
38 | unsigned char incomingb[HASHINPUT]; |
39 | int incomingpos; |
40 | }; |
41 | |
42 | static struct RandPool pool; |
93b581bd |
43 | int random_active = 0; |
374330e2 |
44 | |
19ad0a17 |
45 | static void random_stir(void) |
32874aea |
46 | { |
47 | word32 block[HASHINPUT / sizeof(word32)]; |
48 | word32 digest[HASHSIZE / sizeof(word32)]; |
374330e2 |
49 | int i, j, k; |
50 | |
51 | noise_get_light(random_add_noise); |
52 | |
32874aea |
53 | SHATransform((word32 *) pool.incoming, (word32 *) pool.incomingb); |
374330e2 |
54 | pool.incomingpos = 0; |
55 | |
56 | /* |
57 | * Chunks of this code are blatantly endianness-dependent, but |
58 | * as it's all random bits anyway, WHO CARES? |
59 | */ |
60 | memcpy(digest, pool.incoming, sizeof(digest)); |
61 | |
62 | /* |
63 | * Make two passes over the pool. |
64 | */ |
65 | for (i = 0; i < 2; i++) { |
66 | |
67 | /* |
68 | * We operate SHA in CFB mode, repeatedly adding the same |
69 | * block of data to the digest. But we're also fiddling |
70 | * with the digest-so-far, so this shouldn't be Bad or |
71 | * anything. |
72 | */ |
73 | memcpy(block, pool.pool, sizeof(block)); |
74 | |
75 | /* |
76 | * Each pass processes the pool backwards in blocks of |
77 | * HASHSIZE, just so that in general we get the output of |
78 | * SHA before the corresponding input, in the hope that |
79 | * things will be that much less predictable that way |
80 | * round, when we subsequently return bytes ... |
81 | */ |
32874aea |
82 | for (j = POOLSIZE; (j -= HASHSIZE) >= 0;) { |
374330e2 |
83 | /* |
84 | * XOR the bit of the pool we're processing into the |
85 | * digest. |
86 | */ |
87 | |
32874aea |
88 | for (k = 0; k < sizeof(digest) / sizeof(*digest); k++) |
89 | digest[k] ^= ((word32 *) (pool.pool + j))[k]; |
374330e2 |
90 | |
91 | /* |
92 | * Munge our unrevealed first block of the pool into |
93 | * it. |
94 | */ |
95 | SHATransform(digest, block); |
96 | |
97 | /* |
98 | * Stick the result back into the pool. |
99 | */ |
100 | |
32874aea |
101 | for (k = 0; k < sizeof(digest) / sizeof(*digest); k++) |
102 | ((word32 *) (pool.pool + j))[k] = digest[k]; |
374330e2 |
103 | } |
104 | } |
105 | |
106 | /* |
107 | * Might as well save this value back into `incoming', just so |
108 | * there'll be some extra bizarreness there. |
109 | */ |
110 | SHATransform(digest, block); |
a1a27668 |
111 | memcpy(pool.incoming, digest, sizeof(digest)); |
374330e2 |
112 | |
113 | pool.poolpos = sizeof(pool.incoming); |
114 | } |
115 | |
32874aea |
116 | void random_add_noise(void *noise, int length) |
117 | { |
374330e2 |
118 | unsigned char *p = noise; |
6e522441 |
119 | int i; |
374330e2 |
120 | |
7d6ee6ff |
121 | if (!random_active) |
32874aea |
122 | return; |
7d6ee6ff |
123 | |
6e522441 |
124 | /* |
125 | * This function processes HASHINPUT bytes into only HASHSIZE |
126 | * bytes, so _if_ we were getting incredibly high entropy |
127 | * sources then we would be throwing away valuable stuff. |
128 | */ |
129 | while (length >= (HASHINPUT - pool.incomingpos)) { |
130 | memcpy(pool.incomingb + pool.incomingpos, p, |
131 | HASHINPUT - pool.incomingpos); |
132 | p += HASHINPUT - pool.incomingpos; |
133 | length -= HASHINPUT - pool.incomingpos; |
32874aea |
134 | SHATransform((word32 *) pool.incoming, (word32 *) pool.incomingb); |
135 | for (i = 0; i < HASHSIZE; i++) { |
136 | pool.pool[pool.poolpos++] ^= pool.incomingb[i]; |
137 | if (pool.poolpos >= POOLSIZE) |
138 | pool.poolpos = 0; |
139 | } |
140 | if (pool.poolpos < HASHSIZE) |
141 | random_stir(); |
6e522441 |
142 | |
143 | pool.incomingpos = 0; |
144 | } |
145 | |
146 | memcpy(pool.incomingb + pool.incomingpos, p, length); |
147 | pool.incomingpos += length; |
148 | } |
149 | |
32874aea |
150 | void random_add_heavynoise(void *noise, int length) |
151 | { |
6e522441 |
152 | unsigned char *p = noise; |
153 | int i; |
154 | |
155 | while (length >= POOLSIZE) { |
32874aea |
156 | for (i = 0; i < POOLSIZE; i++) |
157 | pool.pool[i] ^= *p++; |
6e522441 |
158 | random_stir(); |
159 | length -= POOLSIZE; |
160 | } |
161 | |
162 | for (i = 0; i < length; i++) |
32874aea |
163 | pool.pool[i] ^= *p++; |
6e522441 |
164 | random_stir(); |
165 | } |
166 | |
32874aea |
167 | static void random_add_heavynoise_bitbybit(void *noise, int length) |
168 | { |
6e522441 |
169 | unsigned char *p = noise; |
170 | int i; |
171 | |
172 | while (length >= POOLSIZE - pool.poolpos) { |
32874aea |
173 | for (i = 0; i < POOLSIZE - pool.poolpos; i++) |
174 | pool.pool[pool.poolpos + i] ^= *p++; |
374330e2 |
175 | random_stir(); |
6e522441 |
176 | length -= POOLSIZE - pool.poolpos; |
32874aea |
177 | pool.poolpos = 0; |
374330e2 |
178 | } |
179 | |
6e522441 |
180 | for (i = 0; i < length; i++) |
32874aea |
181 | pool.pool[i] ^= *p++; |
6e522441 |
182 | pool.poolpos = i; |
374330e2 |
183 | } |
184 | |
32874aea |
185 | void random_init(void) |
186 | { |
374330e2 |
187 | memset(&pool, 0, sizeof(pool)); /* just to start with */ |
188 | |
7d6ee6ff |
189 | random_active = 1; |
190 | |
6e522441 |
191 | noise_get_heavy(random_add_heavynoise_bitbybit); |
c7d40766 |
192 | random_stir(); |
374330e2 |
193 | } |
194 | |
32874aea |
195 | int random_byte(void) |
196 | { |
374330e2 |
197 | if (pool.poolpos >= POOLSIZE) |
198 | random_stir(); |
199 | |
200 | return pool.pool[pool.poolpos++]; |
201 | } |
202 | |
32874aea |
203 | void random_get_savedata(void **data, int *len) |
204 | { |
e3ac3c05 |
205 | void *buf = smalloc(POOLSIZE / 2); |
374330e2 |
206 | random_stir(); |
e3ac3c05 |
207 | memcpy(buf, pool.pool + pool.poolpos, POOLSIZE / 2); |
32874aea |
208 | *len = POOLSIZE / 2; |
e3ac3c05 |
209 | *data = buf; |
210 | random_stir(); |
374330e2 |
211 | } |