xref: /titanic_50/usr/src/cmd/ssh/libopenbsd-compat/common/bsd-arc4random.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
3*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
4*7c478bd9Sstevel@tonic-gate  */
5*7c478bd9Sstevel@tonic-gate /*
6*7c478bd9Sstevel@tonic-gate  * Copyright (c) 1999-2000 Damien Miller.  All rights reserved.
7*7c478bd9Sstevel@tonic-gate  *
8*7c478bd9Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
9*7c478bd9Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
10*7c478bd9Sstevel@tonic-gate  * are met:
11*7c478bd9Sstevel@tonic-gate  * 1. Redistributions of source code must retain the above copyright
12*7c478bd9Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
13*7c478bd9Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
14*7c478bd9Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in the
15*7c478bd9Sstevel@tonic-gate  *    documentation and/or other materials provided with the distribution.
16*7c478bd9Sstevel@tonic-gate  *
17*7c478bd9Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18*7c478bd9Sstevel@tonic-gate  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19*7c478bd9Sstevel@tonic-gate  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20*7c478bd9Sstevel@tonic-gate  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21*7c478bd9Sstevel@tonic-gate  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22*7c478bd9Sstevel@tonic-gate  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23*7c478bd9Sstevel@tonic-gate  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24*7c478bd9Sstevel@tonic-gate  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25*7c478bd9Sstevel@tonic-gate  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26*7c478bd9Sstevel@tonic-gate  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27*7c478bd9Sstevel@tonic-gate  */
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate #include "includes.h"
30*7c478bd9Sstevel@tonic-gate #include "log.h"
31*7c478bd9Sstevel@tonic-gate 
32*7c478bd9Sstevel@tonic-gate RCSID("$Id: bsd-arc4random.c,v 1.5 2002/05/08 22:57:18 tim Exp $");
33*7c478bd9Sstevel@tonic-gate 
34*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
35*7c478bd9Sstevel@tonic-gate 
36*7c478bd9Sstevel@tonic-gate #ifndef HAVE_ARC4RANDOM
37*7c478bd9Sstevel@tonic-gate 
38*7c478bd9Sstevel@tonic-gate #include <openssl/rand.h>
39*7c478bd9Sstevel@tonic-gate #include <openssl/rc4.h>
40*7c478bd9Sstevel@tonic-gate #include <openssl/err.h>
41*7c478bd9Sstevel@tonic-gate 
42*7c478bd9Sstevel@tonic-gate /* Size of key to use */
43*7c478bd9Sstevel@tonic-gate #define SEED_SIZE 20
44*7c478bd9Sstevel@tonic-gate 
45*7c478bd9Sstevel@tonic-gate /* Number of bytes to reseed after */
46*7c478bd9Sstevel@tonic-gate #define REKEY_BYTES	(1 << 24)
47*7c478bd9Sstevel@tonic-gate 
48*7c478bd9Sstevel@tonic-gate static int rc4_ready = 0;
49*7c478bd9Sstevel@tonic-gate static RC4_KEY rc4;
50*7c478bd9Sstevel@tonic-gate 
arc4random(void)51*7c478bd9Sstevel@tonic-gate unsigned int arc4random(void)
52*7c478bd9Sstevel@tonic-gate {
53*7c478bd9Sstevel@tonic-gate 	unsigned int r = 0;
54*7c478bd9Sstevel@tonic-gate 	static int first_time = 1;
55*7c478bd9Sstevel@tonic-gate 
56*7c478bd9Sstevel@tonic-gate 	if (rc4_ready <= 0) {
57*7c478bd9Sstevel@tonic-gate 		if (first_time)
58*7c478bd9Sstevel@tonic-gate 			seed_rng();
59*7c478bd9Sstevel@tonic-gate 		first_time = 0;
60*7c478bd9Sstevel@tonic-gate 		arc4random_stir();
61*7c478bd9Sstevel@tonic-gate 	}
62*7c478bd9Sstevel@tonic-gate 
63*7c478bd9Sstevel@tonic-gate 	RC4(&rc4, sizeof(r), (unsigned char *)&r, (unsigned char *)&r);
64*7c478bd9Sstevel@tonic-gate 
65*7c478bd9Sstevel@tonic-gate 	rc4_ready -= sizeof(r);
66*7c478bd9Sstevel@tonic-gate 
67*7c478bd9Sstevel@tonic-gate 	return(r);
68*7c478bd9Sstevel@tonic-gate }
69*7c478bd9Sstevel@tonic-gate 
arc4random_stir(void)70*7c478bd9Sstevel@tonic-gate void arc4random_stir(void)
71*7c478bd9Sstevel@tonic-gate {
72*7c478bd9Sstevel@tonic-gate 	unsigned char rand_buf[SEED_SIZE];
73*7c478bd9Sstevel@tonic-gate 
74*7c478bd9Sstevel@tonic-gate 	memset(&rc4, 0, sizeof(rc4));
75*7c478bd9Sstevel@tonic-gate 	if (!RAND_bytes(rand_buf, sizeof(rand_buf)))
76*7c478bd9Sstevel@tonic-gate 		fatal("Couldn't obtain random bytes (error %ld)",
77*7c478bd9Sstevel@tonic-gate 		    ERR_get_error());
78*7c478bd9Sstevel@tonic-gate 	RC4_set_key(&rc4, sizeof(rand_buf), rand_buf);
79*7c478bd9Sstevel@tonic-gate 	memset(rand_buf, 0, sizeof(rand_buf));
80*7c478bd9Sstevel@tonic-gate 
81*7c478bd9Sstevel@tonic-gate 	rc4_ready = REKEY_BYTES;
82*7c478bd9Sstevel@tonic-gate }
83*7c478bd9Sstevel@tonic-gate #endif /* !HAVE_ARC4RANDOM */
84