blob: 46e0a020fca889bd1e13eb8314d415d2456ae9da [file] [log] [blame]
Damien Miller11fa2cc2000-08-16 10:35:58 +10001/*
Damien Miller98225c22004-02-17 16:49:41 +11002 * Copyright (c) 1999,2000,2004 Damien Miller <djm@mindrot.org>
Damien Miller11fa2cc2000-08-16 10:35:58 +10003 *
Damien Miller98225c22004-02-17 16:49:41 +11004 * 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.
Damien Miller11fa2cc2000-08-16 10:35:58 +10007 *
Damien Miller98225c22004-02-17 16:49:41 +11008 * 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.
Damien Miller11fa2cc2000-08-16 10:35:58 +100015 */
16
17#include "includes.h"
Damien Millera2dc6032001-03-19 10:00:53 +110018#include "log.h"
Damien Miller11fa2cc2000-08-16 10:35:58 +100019
20#ifndef HAVE_ARC4RANDOM
21
Damien Miller72c9a7e2000-09-24 11:10:13 +110022#include <openssl/rand.h>
23#include <openssl/rc4.h>
Damien Millera2dc6032001-03-19 10:00:53 +110024#include <openssl/err.h>
Damien Miller72c9a7e2000-09-24 11:10:13 +110025
Damien Millerc30d35c2000-08-30 09:40:09 +110026/* Size of key to use */
27#define SEED_SIZE 20
28
29/* Number of bytes to reseed after */
Damien Miller656d7172000-10-27 09:27:32 +110030#define REKEY_BYTES (1 << 24)
Damien Millerc30d35c2000-08-30 09:40:09 +110031
Damien Miller11fa2cc2000-08-16 10:35:58 +100032static int rc4_ready = 0;
33static RC4_KEY rc4;
34
Damien Miller66df70c2005-02-16 13:01:28 +110035unsigned int
36arc4random(void)
Damien Miller11fa2cc2000-08-16 10:35:58 +100037{
38 unsigned int r = 0;
Damien Miller60bc5172001-03-19 09:38:15 +110039 static int first_time = 1;
Damien Miller11fa2cc2000-08-16 10:35:58 +100040
Damien Miller60bc5172001-03-19 09:38:15 +110041 if (rc4_ready <= 0) {
Tim Rice63cf8412002-05-08 15:57:18 -070042 if (first_time)
Damien Miller60bc5172001-03-19 09:38:15 +110043 seed_rng();
44 first_time = 0;
Damien Miller11fa2cc2000-08-16 10:35:58 +100045 arc4random_stir();
Damien Miller60bc5172001-03-19 09:38:15 +110046 }
47
Damien Miller11fa2cc2000-08-16 10:35:58 +100048 RC4(&rc4, sizeof(r), (unsigned char *)&r, (unsigned char *)&r);
Damien Millerc30d35c2000-08-30 09:40:09 +110049
50 rc4_ready -= sizeof(r);
Damien Miller11fa2cc2000-08-16 10:35:58 +100051
52 return(r);
53}
54
Damien Miller66df70c2005-02-16 13:01:28 +110055void
56arc4random_stir(void)
Damien Miller11fa2cc2000-08-16 10:35:58 +100057{
Damien Millerc30d35c2000-08-30 09:40:09 +110058 unsigned char rand_buf[SEED_SIZE];
Damien Miller65df1742004-07-19 09:30:38 +100059 int i;
Damien Miller60bc5172001-03-19 09:38:15 +110060
Damien Miller11fa2cc2000-08-16 10:35:58 +100061 memset(&rc4, 0, sizeof(rc4));
Damien Millercafbcc72003-03-17 16:13:53 +110062 if (RAND_bytes(rand_buf, sizeof(rand_buf)) <= 0)
Damien Miller60bc5172001-03-19 09:38:15 +110063 fatal("Couldn't obtain random bytes (error %ld)",
64 ERR_get_error());
Damien Miller11fa2cc2000-08-16 10:35:58 +100065 RC4_set_key(&rc4, sizeof(rand_buf), rand_buf);
Damien Miller65df1742004-07-19 09:30:38 +100066
67 /*
68 * Discard early keystream, as per recommendations in:
69 * http://www.wisdom.weizmann.ac.il/~itsik/RC4/Papers/Rc4_ksa.ps
70 */
71 for(i = 0; i <= 256; i += sizeof(rand_buf))
72 RC4(&rc4, sizeof(rand_buf), rand_buf, rand_buf);
73
Damien Miller11fa2cc2000-08-16 10:35:58 +100074 memset(rand_buf, 0, sizeof(rand_buf));
Damien Miller60bc5172001-03-19 09:38:15 +110075
Damien Millerc30d35c2000-08-30 09:40:09 +110076 rc4_ready = REKEY_BYTES;
Damien Miller11fa2cc2000-08-16 10:35:58 +100077}
78#endif /* !HAVE_ARC4RANDOM */