2 * Copyright (c) 1999,2000,2004 Damien Miller <djm@mindrot.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 RCSID("$Id: bsd-arc4random.c,v 1.10 2005/02/16 02:01:28 djm Exp $");
22 #ifndef HAVE_ARC4RANDOM
24 #include <openssl/rand.h>
25 #include <openssl/rc4.h>
26 #include <openssl/err.h>
28 /* Size of key to use */
31 /* Number of bytes to reseed after */
32 #define REKEY_BYTES (1 << 24)
34 static int rc4_ready = 0;
41 static int first_time = 1;
50 RC4(&rc4, sizeof(r), (unsigned char *)&r, (unsigned char *)&r);
52 rc4_ready -= sizeof(r);
60 unsigned char rand_buf[SEED_SIZE];
63 memset(&rc4, 0, sizeof(rc4));
64 if (RAND_bytes(rand_buf, sizeof(rand_buf)) <= 0)
65 fatal("Couldn't obtain random bytes (error %ld)",
67 RC4_set_key(&rc4, sizeof(rand_buf), rand_buf);
70 * Discard early keystream, as per recommendations in:
71 * http://www.wisdom.weizmann.ac.il/~itsik/RC4/Papers/Rc4_ksa.ps
73 for(i = 0; i <= 256; i += sizeof(rand_buf))
74 RC4(&rc4, sizeof(rand_buf), rand_buf, rand_buf);
76 memset(rand_buf, 0, sizeof(rand_buf));
78 rc4_ready = REKEY_BYTES;
80 #endif /* !HAVE_ARC4RANDOM */