]> andersk Git - gssapi-openssh.git/blame - openssh/openbsd-compat/bsd-arc4random.c
Import of OpenSSH 3.8p1
[gssapi-openssh.git] / openssh / openbsd-compat / bsd-arc4random.c
CommitLineData
3c0ef626 1/*
cdd66111 2 * Copyright (c) 1999,2000,2004 Damien Miller <djm@mindrot.org>
3c0ef626 3 *
cdd66111 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.
3c0ef626 7 *
cdd66111 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.
3c0ef626 15 */
16
17#include "includes.h"
18#include "log.h"
19
20RCSID("$Id$");
21
22#ifndef HAVE_ARC4RANDOM
23
24#include <openssl/rand.h>
25#include <openssl/rc4.h>
26#include <openssl/err.h>
27
28/* Size of key to use */
29#define SEED_SIZE 20
30
31/* Number of bytes to reseed after */
32#define REKEY_BYTES (1 << 24)
33
34static int rc4_ready = 0;
35static RC4_KEY rc4;
36
37unsigned int arc4random(void)
38{
39 unsigned int r = 0;
40 static int first_time = 1;
41
42 if (rc4_ready <= 0) {
700318f3 43 if (first_time)
3c0ef626 44 seed_rng();
45 first_time = 0;
46 arc4random_stir();
47 }
48
49 RC4(&rc4, sizeof(r), (unsigned char *)&r, (unsigned char *)&r);
50
51 rc4_ready -= sizeof(r);
52
53 return(r);
54}
55
56void arc4random_stir(void)
57{
58 unsigned char rand_buf[SEED_SIZE];
59
60 memset(&rc4, 0, sizeof(rc4));
6a9b3198 61 if (RAND_bytes(rand_buf, sizeof(rand_buf)) <= 0)
3c0ef626 62 fatal("Couldn't obtain random bytes (error %ld)",
63 ERR_get_error());
64 RC4_set_key(&rc4, sizeof(rand_buf), rand_buf);
0fff78ff 65 RC4(&rc4, sizeof(rand_buf), rand_buf, rand_buf);
3c0ef626 66 memset(rand_buf, 0, sizeof(rand_buf));
67
68 rc4_ready = REKEY_BYTES;
69}
70#endif /* !HAVE_ARC4RANDOM */
This page took 0.057458 seconds and 5 git commands to generate.