2 * Copyright (c) 2000 Damien Miller. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * 3. All advertising materials mentioning features or use of this software
13 * must display the following acknowledgement:
14 * This product includes software developed by Markus Friedl.
15 * 4. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 #include <openssl/rand.h>
36 #include <openssl/sha.h>
42 # define offsetof(type, member) ((size_t) &((type *)0)->member)
44 /* Collect entropy from EGD */
45 void get_random_bytes(unsigned char *buf, int len)
47 static int egd_socket = -1;
49 char egd_message[2] = { 0x02, 0x00 };
50 struct sockaddr_un addr;
53 memset(&addr, '\0', sizeof(addr));
54 addr.sun_family = AF_UNIX;
56 /* FIXME: compile time check? */
57 if (sizeof(EGD_SOCKET) > sizeof(addr.sun_path))
58 fatal("Random pool path is too long");
60 strcpy(addr.sun_path, EGD_SOCKET);
62 addr_len = offsetof(struct sockaddr_un, sun_path) + sizeof(EGD_SOCKET);
64 if (egd_socket == -1) {
65 egd_socket = socket(AF_UNIX, SOCK_STREAM, 0);
67 fatal("Couldn't create AF_UNIX socket: %s", strerror(errno));
68 if (connect(egd_socket, (struct sockaddr*)&addr, addr_len) == -1)
69 fatal("Couldn't connect to EGD socket \"%s\": %s", addr.sun_path, strerror(errno));
73 fatal("Too many bytes to read from EGD");
75 /* Send blocking read request to EGD */
78 c = atomicio(write, egd_socket, egd_message, sizeof(egd_message));
80 fatal("Couldn't write to EGD socket \"%s\": %s", EGD_SOCKET, strerror(errno));
82 c = atomicio(read, egd_socket, buf, len);
84 fatal("Couldn't read from EGD socket \"%s\": %s", EGD_SOCKET, strerror(errno));
88 #else /* !EGD_SOCKET */
90 /* Collect entropy from /dev/urandom or pipe */
91 void get_random_bytes(unsigned char *buf, int len)
93 static int random_pool = -1;
96 if (random_pool == -1) {
97 random_pool = open(RANDOM_POOL, O_RDONLY);
98 if (random_pool == -1)
99 fatal("Couldn't open random pool \"%s\": %s", RANDOM_POOL, strerror(errno));
102 c = atomicio(read, random_pool, buf, len);
104 fatal("Couldn't read from random pool \"%s\": %s", RANDOM_POOL, strerror(errno));
106 #endif /* RANDOM_POOL */
107 #endif /* EGD_SOCKET */
109 #if !defined(EGD_SOCKET) && !defined(RANDOM_POOL)
111 * FIXME: proper entropy estimations. All current values are guesses
112 * FIXME: (ATL) do estimates at compile time?
113 * FIXME: More entropy sources
114 * FIXME: (ATL) bring in entropy sources from file
115 * FIXME: (ATL) add heuristic to increase the timeout if needed
118 /* slow command timeouts (all in milliseconds) */
119 /* static int entropy_timeout_default = ENTROPY_TIMEOUT_MSEC; */
120 static int entropy_timeout_current = ENTROPY_TIMEOUT_MSEC;
122 static int prng_seed_loaded = 0;
123 static int prng_seed_saved = 0;
127 /* Proportion of data that is entropy */
129 /* Counter goes positive if this command times out */
130 unsigned int badness;
131 /* Increases by factor of two each timeout */
132 unsigned int sticky_badness;
133 /* Path to executable */
135 /* argv to pass to executable */
139 double stir_from_system(void);
140 double stir_from_programs(void);
141 double stir_gettimeofday(double entropy_estimate);
142 double stir_clock(double entropy_estimate);
143 double stir_rusage(int who, double entropy_estimate);
144 double hash_output_from_command(entropy_source_t *src, char *hash);
146 entropy_source_t entropy_sources[] = {
148 { 0.002, 0, 1, PROG_LS, { "ls", "-alni", "/var/log", NULL } },
149 { 0.002, 0, 1, PROG_LS, { "ls", "-alni", "/var/adm", NULL } },
150 { 0.002, 0, 1, PROG_LS, { "ls", "-alni", "/var/mail", NULL } },
151 { 0.002, 0, 1, PROG_LS, { "ls", "-alni", "/var/spool/mail", NULL } },
152 { 0.002, 0, 1, PROG_LS, { "ls", "-alni", "/proc", NULL } },
153 { 0.002, 0, 1, PROG_LS, { "ls", "-alni", "/tmp", NULL } },
156 { 0.005, 0, 1, PROG_NETSTAT, { "netstat","-an", NULL, NULL } },
157 { 0.010, 0, 1, PROG_NETSTAT, { "netstat","-in", NULL, NULL } },
158 { 0.002, 0, 1, PROG_NETSTAT, { "netstat","-rn", NULL, NULL } },
159 { 0.002, 0, 1, PROG_NETSTAT, { "netstat","-s", NULL, NULL } },
162 { 0.002, 0, 1, PROG_ARP, { "arp","-a","-n", NULL } },
165 { 0.002, 0, 1, PROG_IFCONFIG, { "ifconfig", "-a", NULL, NULL } },
168 { 0.003, 0, 1, PROG_PS, { "ps", "laxww", NULL, NULL } },
169 { 0.003, 0, 1, PROG_PS, { "ps", "-al", NULL, NULL } },
170 { 0.003, 0, 1, PROG_PS, { "ps", "-efl", NULL, NULL } },
173 { 0.005, 0, 1, PROG_W, { "w", NULL, NULL, NULL } },
176 { 0.001, 0, 1, PROG_WHO, { "who","-i", NULL, NULL } },
179 { 0.001, 0, 1, PROG_LAST, { "last", NULL, NULL, NULL } },
182 { 0.001, 0, 1, PROG_LASTLOG, { "lastlog", NULL, NULL, NULL } },
185 { 0.010, 0, 1, PROG_DF, { "df", NULL, NULL, NULL } },
186 { 0.010, 0, 1, PROG_DF, { "df", "-i", NULL, NULL } },
189 { 0.010, 0, 1, PROG_VMSTAT, { "vmstat", NULL, NULL, NULL } },
192 { 0.001, 0, 1, PROG_UPTIME, { "uptime", NULL, NULL, NULL } },
195 { 0.001, 0, 1, PROG_IPCS, { "-a", NULL, NULL, NULL } },
198 { 0.001, 0, 1, PROG_TAIL, { "tail", "-200", "/var/log/messages", NULL, NULL } },
199 { 0.001, 0, 1, PROG_TAIL, { "tail", "-200", "/var/log/syslog", NULL, NULL } },
200 { 0.001, 0, 1, PROG_TAIL, { "tail", "-200", "/var/adm/messages", NULL, NULL } },
201 { 0.001, 0, 1, PROG_TAIL, { "tail", "-200", "/var/adm/syslog", NULL, NULL } },
202 { 0.001, 0, 1, PROG_TAIL, { "tail", "-200", "/var/log/maillog", NULL, NULL } },
203 { 0.001, 0, 1, PROG_TAIL, { "tail", "-200", "/var/adm/maillog", NULL, NULL } },
205 { 0.000, 0, 0, NULL, { NULL, NULL, NULL, NULL, NULL } },
209 stir_from_system(void)
211 double total_entropy_estimate;
214 total_entropy_estimate = 0;
217 RAND_add(&i, sizeof(i), 0.1);
218 total_entropy_estimate += 0.1;
221 RAND_add(&i, sizeof(i), 0.1);
222 total_entropy_estimate += 0.1;
225 RAND_add(&i, sizeof(i), 0.0);
227 RAND_add(&i, sizeof(i), 0.0);
229 total_entropy_estimate += stir_gettimeofday(1.0);
230 total_entropy_estimate += stir_clock(0.2);
231 total_entropy_estimate += stir_rusage(RUSAGE_SELF, 2.0);
233 return(total_entropy_estimate);
237 stir_from_programs(void)
241 double entropy_estimate;
242 double total_entropy_estimate;
243 char hash[SHA_DIGEST_LENGTH];
246 * Run through list of programs twice to catch differences
248 total_entropy_estimate = 0;
249 for(i = 0; i < 2; i++) {
251 while (entropy_sources[c].path != NULL) {
253 if (!entropy_sources[c].badness) {
254 /* Hash output from command */
255 entropy_estimate = hash_output_from_command(&entropy_sources[c], hash);
257 /* Scale back entropy estimate according to command's rate */
258 entropy_estimate *= entropy_sources[c].rate;
260 /* Upper bound of entropy estimate is SHA_DIGEST_LENGTH */
261 if (entropy_estimate > SHA_DIGEST_LENGTH)
262 entropy_estimate = SHA_DIGEST_LENGTH;
264 /* * Scale back estimates for subsequent passes through list */
265 entropy_estimate /= 10.0 * (i + 1.0);
268 RAND_add(hash, sizeof(hash), entropy_estimate);
270 /* FIXME: turn this off later */
272 debug("Got %0.2f bytes of entropy from %s", entropy_estimate,
273 entropy_sources[c].path);
276 total_entropy_estimate += entropy_estimate;
278 /* Execution times should be a little unpredictable */
279 total_entropy_estimate += stir_gettimeofday(0.05);
280 total_entropy_estimate += stir_clock(0.05);
281 total_entropy_estimate += stir_rusage(RUSAGE_SELF, 0.1);
282 total_entropy_estimate += stir_rusage(RUSAGE_CHILDREN, 0.1);
284 /* FIXME: turn this off later */
286 debug("Command '%s %s %s' disabled (badness %d)",
287 entropy_sources[c].path, entropy_sources[c].args[1],
288 entropy_sources[c].args[2], entropy_sources[c].badness);
291 if (entropy_sources[c].badness > 0)
292 entropy_sources[c].badness--;
299 return(total_entropy_estimate);
303 stir_gettimeofday(double entropy_estimate)
307 if (gettimeofday(&tv, NULL) == -1)
308 fatal("Couldn't gettimeofday: %s", strerror(errno));
310 RAND_add(&tv, sizeof(tv), entropy_estimate);
312 return(entropy_estimate);
316 stir_clock(double entropy_estimate)
322 RAND_add(&c, sizeof(c), entropy_estimate);
324 return(entropy_estimate);
325 #else /* _HAVE_CLOCK */
327 #endif /* _HAVE_CLOCK */
331 stir_rusage(int who, double entropy_estimate)
333 #ifdef HAVE_GETRUSAGE
336 if (getrusage(who, &ru) == -1)
337 fatal("Couldn't getrusage: %s", strerror(errno));
339 RAND_add(&ru, sizeof(ru), 0.1);
341 return(entropy_estimate);
342 #else /* _HAVE_GETRUSAGE */
344 #endif /* _HAVE_GETRUSAGE */
348 hash_output_from_command(entropy_source_t *src, char *hash)
350 static int devnull = -1;
353 int cmd_eof = 0, error_abort = 0;
358 int total_bytes_read;
362 devnull = open("/dev/null", O_RDWR);
364 fatal("Couldn't open /dev/null: %s", strerror(errno));
368 fatal("Couldn't open pipe: %s", strerror(errno));
370 switch (pid = fork()) {
374 fatal("Couldn't fork: %s", strerror(errno));
377 dup2(devnull, STDIN_FILENO);
378 dup2(p[1], STDOUT_FILENO);
379 dup2(p[1], STDERR_FILENO);
384 execv(src->path, (char**)(src->args));
385 debug("(child) Couldn't exec '%s %s %s': %s", src->path,
386 src->args[1], src->args[2], strerror(errno));
387 src->badness = src->sticky_badness = 128;
389 default: /* Parent */
393 RAND_add(&pid, sizeof(&pid), 0.0);
397 /* Hash output from child */
399 total_bytes_read = 0;
401 while (!error_abort && !cmd_eof) {
406 FD_SET(p[0], &rdset);
407 tv.tv_sec = entropy_timeout_current / 1000;
408 tv.tv_usec = (entropy_timeout_current % 1000) * 1000;
410 ret = select(p[0]+1, &rdset, NULL, NULL, &tv);
419 bytes_read = read(p[0], buf, sizeof(buf));
420 if (bytes_read == -1) {
424 SHA1_Update(&sha, buf, bytes_read);
425 total_bytes_read += bytes_read;
426 RAND_add(&bytes_read, sizeof(&bytes_read), 0.0);
427 cmd_eof = bytes_read ? 0 : 1;
433 error("Command '%s %s': select() failed: %s", src->path, src->args[1],
439 RAND_add(&tv, sizeof(&tv), 0.0);
440 } /* while !error_abort && !cmd_eof */
442 SHA1_Final(hash, &sha);
446 if (waitpid(pid, &status, 0) == -1) {
447 error("Couldn't wait for child '%s %s' completion: %s", src->path,
448 src->args[1], strerror(errno));
449 /* return(-1); */ /* FIXME: (ATL) this doesn't feel right */
453 RAND_add(&status, sizeof(&status), 0.0);
456 /* closing p[0] on timeout causes the entropy command to
457 * SIGPIPE. Take whatever output we got, and mark this command
459 debug("Command %s %s timed out", src->path, src->args[1]);
460 src->sticky_badness *= 2;
461 src->badness = src->sticky_badness;
462 return(total_bytes_read);
465 if (WIFEXITED(status)) {
466 if (WEXITSTATUS(status)==0) {
467 return(total_bytes_read);
469 debug("Exit status was %d", WEXITSTATUS(status));
470 src->badness = src->sticky_badness = 128;
473 } else if (WIFSIGNALED(status)) {
474 debug("Returned on uncaught signal %d !", status);
475 src->badness = src->sticky_badness = 128;
482 * prng seedfile functions
485 prng_check_seedfile(char *filename) {
489 /* FIXME raceable: eg replace seed between this stat and subsequent open */
490 /* Not such a problem because we don't trust the seed file anyway */
491 if (lstat(filename, &st) == -1) {
492 /* Fail on hard errors */
494 fatal("Couldn't stat random seed file \"%s\": %s", filename,
501 if (!S_ISREG(st.st_mode))
502 fatal("PRNG seedfile %.100s is not a regular file", filename);
504 /* mode 0600, owned by root or the current user? */
505 if (((st.st_mode & 0177) != 0) || !(st.st_uid == geteuid()))
506 fatal("PRNG seedfile %.100s must be mode 0600, owned by uid %d",
513 prng_write_seedfile(void) {
519 /* Don't bother if we have already saved a seed */
525 pw = getpwuid(getuid());
527 fatal("Couldn't get password entry for current user (%i): %s",
528 getuid(), strerror(errno));
530 /* Try to ensure that the parent directory is there */
531 snprintf(filename, sizeof(filename), "%.512s/%s", pw->pw_dir,
533 mkdir(filename, 0700);
535 snprintf(filename, sizeof(filename), "%.512s/%s", pw->pw_dir,
538 debug("writing PRNG seed to file %.100s", filename);
540 RAND_bytes(seed, sizeof(seed));
542 /* Don't care if the seed doesn't exist */
543 prng_check_seedfile(filename);
545 if ((fd = open(filename, O_WRONLY|O_TRUNC|O_CREAT, 0600)) == -1)
546 fatal("couldn't access PRNG seedfile %.100s (%.100s)", filename,
549 if (atomicio(write, fd, &seed, sizeof(seed)) != sizeof(seed))
550 fatal("problem writing PRNG seedfile %.100s (%.100s)", filename,
557 prng_read_seedfile(void) {
563 pw = getpwuid(getuid());
565 fatal("Couldn't get password entry for current user (%i): %s",
566 getuid(), strerror(errno));
568 snprintf(filename, sizeof(filename), "%.512s/%s", pw->pw_dir,
571 debug("loading PRNG seed from file %.100s", filename);
573 if (!prng_check_seedfile(filename)) {
574 verbose("Random seed file not found, creating new");
575 prng_write_seedfile();
577 /* Reseed immediatly */
578 (void)stir_from_system();
579 (void)stir_from_programs();
583 /* open the file and read in the seed */
584 fd = open(filename, O_RDONLY);
586 fatal("could not open PRNG seedfile %.100s (%.100s)", filename,
589 if (atomicio(read, fd, &seed, sizeof(seed)) != sizeof(seed)) {
590 verbose("invalid or short read from PRNG seedfile %.100s - ignoring",
592 memset(seed, '\0', sizeof(seed));
596 /* stir in the seed, with estimated entropy zero */
597 RAND_add(&seed, sizeof(seed), 0.0);
600 #endif /* defined(EGD_SOCKET) || defined(RANDOM_POOL) */
602 #if defined(EGD_SOCKET) || defined(RANDOM_POOL)
605 * Seed OpenSSL's random number pool from Kernel random number generator
613 debug("Seeding random number generator");
614 get_random_bytes(buf, sizeof(buf));
615 RAND_add(buf, sizeof(buf), sizeof(buf));
616 memset(buf, '\0', sizeof(buf));
619 #else /* defined(EGD_SOCKET) || defined(RANDOM_POOL) */
622 * Write a keyfile at exit
625 prng_seed_cleanup(void *junk)
627 prng_write_seedfile();
631 * Conditionally Seed OpenSSL's random number pool from
632 * syscalls and program output
637 debug("Seeding random number generator.");
638 debug("OpenSSL random status is now %i\n", RAND_status());
639 debug("%i bytes from system calls", (int)stir_from_system());
640 debug("%i bytes from programs", (int)stir_from_programs());
641 debug("OpenSSL random status is now %i\n", RAND_status());
643 if (!prng_seed_loaded)
645 prng_seed_loaded = 1;
647 prng_read_seedfile();
648 fatal_add_cleanup(prng_seed_cleanup, NULL);
649 atexit(prng_write_seedfile);
652 #endif /* defined(EGD_SOCKET) || defined(RANDOM_POOL) */