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