1 /* randomness machinery
2 * Copyright (C) 1997 Angelos D. Keromytis.
3 * Copyright (C) 1998-2001 D. Hugh Redelmeier.
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 * RCSID $Id: rnd.c,v 1.17 2002/01/21 03:14:21 dhr Exp $
18 /* A true random number generator (we hope)
20 * Under LINUX ("linux" predefined), use /dev/urandom.
21 * Under OpenBSD ("__OpenBSD__" predefined), use arc4random().
22 * Otherwise use our own random number generator based on clock skew.
23 * I (ADK) first heard of the idea from John Ioannidis, who heard it
24 * from Matt Blaze and/or Jack Lacy.
25 * ??? Why is mixing need for linux but not OpenBSD?
28 /* Pluto's uses of randomness:
30 * - Setting up the "secret_of_the_day". This changes every hour! 20
31 * bytes a shot. It is used in building responder cookies.
33 * - generating initiator cookies (8 bytes, once per Phase 1 initiation).
35 * - 32 bytes per DH local secret. Once per Main Mode exchange and once
36 * per Quick Mode Exchange with PFS. (Size is our choice, with
39 * - 16 bytes per nonce we generate. Once per Main Mode exchange and
40 * once per Quick Mode exchange. (Again, we choose the size.)
42 * - 4 bytes per SPI number that we generate. We choose the SPIs for all
43 * inbound SPIs, one to three per IPSEC SA (one for AH (rare, probably)
44 * one for ESP (almost always), and one for tunnel (very common)).
45 * I don't actually know how the kernel would generate these numbers --
46 * currently Pluto generates them; this isn't the way things will be
49 * - 4 bytes per Message ID we need to generate. One per Quick Mode
50 * exchange. Eventually, one per informational exchange.
64 #include "constants.h"
71 # define USE_DEV_RANDOM 1
72 # define RANDOM_PATH "/dev/urandom"
75 # define USE_ARC4RANDOM
77 # define USE_CLOCK_SLEW
83 #define get_rnd_byte() (arc4random() % 256)
85 #else /**** start of large #else ****/
88 static int random_fd = NULL_FD;
91 #define RANDOM_POOL_SIZE SHA1_DIGEST_SIZE
92 static u_char random_pool[RANDOM_POOL_SIZE];
96 /* Generate (what we hope is) a true random byte using /dev/urandom */
98 generate_rnd_byte(void)
102 if (read(random_fd, &c, sizeof(c)) == -1)
103 exit_log_errno((e, "read() failed in get_rnd_byte()"));
108 #else /* !USE_DEV_RANDOM */
110 /* Generate (what we hope is) a true random byte using the clock skew trick.
111 * Note: this code is not maintained! In particular, LINUX signal(2)
112 * semantics changed with glibc2 (and not for the better). It isn't clear
113 * that this code will work. We keep the code because someday it might
116 # error "This code is not maintained. Please define USE_DEV_RANDOM."
118 static volatile sig_atomic_t i, j, k;
120 /* timer signal handler */
122 rnd_handler(int ignore_me UNUSED)
124 k <<= 1; /* Shift left by 1 */
126 k |= (i & 0x1); /* Get lsbit of counter */
129 signal(SIGVTALRM, rnd_handler);
133 generate_rnd_byte(void)
135 struct itimerval tmval, ntmval;
137 # ifdef NEVER /* ??? */
139 int mask = siggetmask();
149 ntmval.it_interval.tv_sec = 0;
150 ntmval.it_interval.tv_usec = 1;
151 ntmval.it_value.tv_sec = 0;
152 ntmval.it_value.tv_usec = 1;
153 signal(SIGVTALRM, rnd_handler);
154 setitimer(ITIMER_VIRTUAL, &ntmval, &tmval);
159 setitimer(ITIMER_VIRTUAL, &tmval, &ntmval);
160 signal(SIGVTALRM, SIG_IGN);
162 # ifdef NEVER /* ??? */
172 #endif /* !USE_DEV_RANDOM */
180 SHA1Update(&ctx, random_pool, RANDOM_POOL_SIZE);
181 SHA1Final(random_pool, &ctx);
185 * Get a single random byte.
190 random_pool[RANDOM_POOL_SIZE - 1] = generate_rnd_byte();
191 random_pool[0] = generate_rnd_byte();
193 return random_pool[0];
196 #endif /* !USE_ARC4RANDOM */ /**** end of large #else ****/
199 get_rnd_bytes(u_char *buffer, int length)
203 for (i = 0; i < length; i++)
204 buffer[i] = get_rnd_byte();
208 * Initialize the random pool.
213 #ifndef USE_ARC4RANDOM
214 # ifdef USE_DEV_RANDOM
215 DBG(DBG_KLIPS, DBG_log("opening %s", RANDOM_PATH));
216 random_fd = open(RANDOM_PATH, O_RDONLY);
218 exit_log_errno((e, "open of %s failed in init_rnd_pool()", RANDOM_PATH));
221 get_rnd_bytes(random_pool, RANDOM_POOL_SIZE);
223 #endif /* !USE_ARC4RANDOM */
225 /* start of rand(3) on the right foot */
229 get_rnd_bytes((void *)&seed, sizeof(seed));
234 u_char secret_of_the_day[SHA1_DIGEST_SIZE];
240 * Generate the secret value for responder cookies, and
241 * schedule an event for refresh.
243 get_rnd_bytes(secret_of_the_day, sizeof(secret_of_the_day));
244 event_schedule(EVENT_REINIT_SECRET, EVENT_REINIT_SECRET_DELAY, NULL);