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