2 * Dropbear - a SSH2 server
4 * Copyright (c) 2002,2003 Matt Johnston
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32 /* this is used to generate unique output from the same hashpool */
33 static uint32_t counter
= 0;
34 /* the max value for the counter, so it won't integer overflow */
35 #define MAX_COUNTER 1<<30
37 static unsigned char hashpool
[SHA1_HASH_SIZE
] = {0};
38 static int donerandinit
= 0;
40 #define INIT_SEED_SIZE 32 /* 256 bits */
42 /* The basic setup is we read some data from /dev/(u)random or prngd and hash it
43 * into hashpool. To read data, we hash together current hashpool contents,
44 * and a counter. We feed more data in by hashing the current pool and new
47 * It is important to ensure that counter doesn't wrap around before we
48 * feed in new entropy.
52 /* Pass len=0 to hash an entire file */
54 process_file(hash_state
*hs
, const char *filename
,
55 unsigned int len
, int prngd
)
57 static int already_blocked
= 0;
59 unsigned int readcount
;
60 int ret
= DROPBEAR_FAILURE
;
62 #ifdef DROPBEAR_PRNGD_SOCKET
65 readfd
= connect_unix(filename
);
70 readfd
= open(filename
, O_RDONLY
);
78 while (len
== 0 || readcount
< len
)
80 int readlen
, wantread
;
81 unsigned char readbuf
[4096];
82 if (!already_blocked
&& !prngd
)
85 struct timeval timeout
;
92 FD_SET(readfd
, &read_fds
);
93 res
= select(readfd
+ 1, &read_fds
, NULL
, NULL
, &timeout
);
96 dropbear_log(LOG_WARNING
, "Warning: Reading the randomness source '%s' seems to have blocked.\nYou may need to find a better entropy source.", filename
);
103 wantread
= sizeof(readbuf
);
107 wantread
= MIN(sizeof(readbuf
), len
-readcount
);
110 #ifdef DROPBEAR_PRNGD_SOCKET
114 egdcmd
[0] = 0x02; /* blocking read */
115 egdcmd
[1] = (unsigned char)wantread
;
116 if (write(readfd
, egdcmd
, 2) < 0)
118 dropbear_exit("Can't send command to egd");
123 readlen
= read(readfd
, readbuf
, wantread
);
125 if (readlen
< 0 && errno
== EINTR
) {
128 if (readlen
== 0 && len
== 0)
130 /* whole file was read as requested */
135 sha1_process(hs
, readbuf
, readlen
);
136 readcount
+= readlen
;
138 ret
= DROPBEAR_SUCCESS
;
144 void addrandom(char * buf
, unsigned int len
)
148 /* hash in the new seed data */
150 /* existing state (zeroes on startup) */
151 sha1_process(&hs
, (void*)hashpool
, sizeof(hashpool
));
154 sha1_process(&hs
, buf
, len
);
155 sha1_done(&hs
, hashpool
);
158 static void write_urandom()
160 #ifndef DROPBEAR_PRNGD_SOCKET
161 /* This is opportunistic, don't worry about failure */
162 unsigned char buf
[INIT_SEED_SIZE
];
163 FILE *f
= fopen(DROPBEAR_URANDOM_DEV
, "w");
167 genrandom(buf
, sizeof(buf
));
168 fwrite(buf
, sizeof(buf
), 1, f
);
173 /* Initialise the prng from /dev/urandom or prngd. This function can
174 * be called multiple times */
183 /* hash in the new seed data */
186 sha1_process(&hs
, (void*)hashpool
, sizeof(hashpool
));
188 #ifdef DROPBEAR_PRNGD_SOCKET
189 if (process_file(&hs
, DROPBEAR_PRNGD_SOCKET
, INIT_SEED_SIZE
, 1)
190 != DROPBEAR_SUCCESS
) {
191 dropbear_exit("Failure reading random device %s",
192 DROPBEAR_PRNGD_SOCKET
);
195 /* non-blocking random source (probably /dev/urandom) */
196 if (process_file(&hs
, DROPBEAR_URANDOM_DEV
, INIT_SEED_SIZE
, 0)
197 != DROPBEAR_SUCCESS
) {
198 dropbear_exit("Failure reading random device %s",
199 DROPBEAR_URANDOM_DEV
);
203 /* A few other sources to fall back on.
204 * Add more here for other platforms */
206 /* Seems to be a reasonable source of entropy from timers. Possibly hard
207 * for even local attackers to reproduce */
208 process_file(&hs
, "/proc/timer_list", 0, 0);
209 /* Might help on systems with wireless */
210 process_file(&hs
, "/proc/interrupts", 0, 0);
212 process_file(&hs
, "/proc/loadavg", 0, 0);
213 process_file(&hs
, "/proc/sys/kernel/random/entropy_avail", 0, 0);
215 /* Mostly network visible but useful in some situations.
216 * Limit size to avoid slowdowns on systems with lots of routes */
217 process_file(&hs
, "/proc/net/netstat", 4096, 0);
218 process_file(&hs
, "/proc/net/dev", 4096, 0);
219 process_file(&hs
, "/proc/net/tcp", 4096, 0);
220 /* Also includes interface lo */
221 process_file(&hs
, "/proc/net/rt_cache", 4096, 0);
222 process_file(&hs
, "/proc/vmstat", 0, 0);
226 sha1_process(&hs
, (void*)&pid
, sizeof(pid
));
228 /* gettimeofday() doesn't completely fill out struct timeval on
229 OS X (10.8.3), avoid valgrind warnings by clearing it first */
230 memset(&tv
, 0x0, sizeof(tv
));
231 gettimeofday(&tv
, NULL
);
232 sha1_process(&hs
, (void*)&tv
, sizeof(tv
));
235 sha1_process(&hs
, (void*)&clockval
, sizeof(clockval
));
237 /* When a private key is read by the client or server it will
238 * be added to the hashpool - see runopts.c */
240 sha1_done(&hs
, hashpool
);
245 /* Feed it all back into /dev/urandom - this might help if Dropbear
246 * is running from inetd and gets new state each time */
250 /* return len bytes of pseudo-random data */
251 void genrandom(unsigned char* buf
, unsigned int len
) {
254 unsigned char hash
[SHA1_HASH_SIZE
];
255 unsigned int copylen
;
258 dropbear_exit("seedrandom not done");
263 sha1_process(&hs
, (void*)hashpool
, sizeof(hashpool
));
264 sha1_process(&hs
, (void*)&counter
, sizeof(counter
));
265 sha1_done(&hs
, hash
);
268 if (counter
> MAX_COUNTER
) {
272 copylen
= MIN(len
, SHA1_HASH_SIZE
);
273 memcpy(buf
, hash
, copylen
);
277 m_burn(hash
, sizeof(hash
));
280 /* Generates a random mp_int.
281 * max is a *mp_int specifying an upper bound.
282 * rand must be an initialised *mp_int for the result.
283 * the result rand satisfies: 0 < rand < max
285 void gen_random_mpint(mp_int
*max
, mp_int
*rand
) {
287 unsigned char *randbuf
= NULL
;
288 unsigned int len
= 0;
289 const unsigned char masks
[] = {0xff, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f};
291 const int size_bits
= mp_count_bits(max
);
294 if ((size_bits
% 8) != 0) {
298 randbuf
= (unsigned char*)m_malloc(len
);
300 genrandom(randbuf
, len
);
301 /* Mask out the unrequired bits - mp_read_unsigned_bin expects
303 randbuf
[0] &= masks
[size_bits
% 8];
305 bytes_to_mp(rand
, randbuf
, len
);
307 /* keep regenerating until we get one satisfying
309 } while (mp_cmp(rand
, max
) != MP_LT
);
310 m_burn(randbuf
, len
);