Main Page | Modules | Class List | Directories | File List | Class Members | File Members | Related Pages

rand.c

Go to the documentation of this file.
00001 /*
00002  * rand.c
00003  *
00004  * Pseudorandom number generation, based on OpenBSD arc4random().
00005  *
00006  * Copyright (c) 2000 Dug Song <dugsong@monkey.org>
00007  * Copyright (c) 1996 David Mazieres <dm@lcs.mit.edu>
00008  *
00009  * $Id: rand.c,v 1.15 2005/02/15 06:37:07 dugsong Exp $
00010  */
00011 
00012 #include "config.h"
00013 
00014 #ifdef _WIN32
00015 /* XXX */
00016 # undef _WIN32_WINNT
00017 # define _WIN32_WINNT 0x0400
00018 # include <wincrypt.h>
00019 # define inline __inline
00020 #else
00021 # include <sys/types.h>
00022 # include <sys/time.h>
00023 # include <unistd.h>
00024 #endif
00025 #include <fcntl.h>
00026 #include <stdlib.h>
00027 #include <string.h>
00028 
00029 #include "dnet.h"
00030 
00031 struct rand_handle {
00032         uint8_t          i;
00033         uint8_t          j;
00034         uint8_t          s[256];
00035         u_char          *tmp;
00036         int              tmplen;
00037 };
00038 
00039 static inline void
00040 rand_init(rand_t *rand)
00041 {
00042         int i;
00043         
00044         for (i = 0; i < 256; i++)
00045                 rand->s[i] = i;
00046         rand->i = rand->j = 0;
00047 }
00048 
00049 static inline void
00050 rand_addrandom(rand_t *rand, u_char *buf, int len)
00051 {
00052         int i;
00053         uint8_t si;
00054         
00055         rand->i--;
00056         for (i = 0; i < 256; i++) {
00057                 rand->i = (rand->i + 1);
00058                 si = rand->s[rand->i];
00059                 rand->j = (rand->j + si + buf[i % len]);
00060                 rand->s[rand->i] = rand->s[rand->j];
00061                 rand->s[rand->j] = si;
00062         }
00063         rand->j = rand->i;
00064 }
00065 
00066 rand_t *
00067 rand_open(void)
00068 {
00069         rand_t *r;
00070         u_char seed[256];
00071 #ifdef _WIN32
00072         HCRYPTPROV hcrypt = 0;
00073 
00074         CryptAcquireContext(&hcrypt, NULL, NULL, PROV_RSA_FULL,
00075             CRYPT_VERIFYCONTEXT);
00076         CryptGenRandom(hcrypt, sizeof(seed), seed);
00077         CryptReleaseContext(hcrypt, 0);
00078 #else
00079         struct timeval *tv = (struct timeval *)seed;
00080         int fd;
00081 
00082         if ((fd = open("/dev/arandom", O_RDONLY)) != -1 ||
00083             (fd = open("/dev/urandom", O_RDONLY)) != -1) {
00084                 read(fd, seed + sizeof(*tv), sizeof(seed) - sizeof(*tv));
00085                 close(fd);
00086         }
00087         gettimeofday(tv, NULL);
00088 #endif
00089         if ((r = malloc(sizeof(*r))) != NULL) {
00090                 rand_init(r);
00091                 rand_addrandom(r, seed, 128);
00092                 rand_addrandom(r, seed + 128, 128);
00093                 r->tmp = NULL;
00094                 r->tmplen = 0;
00095         }
00096         return (r);
00097 }
00098 
00099 static uint8_t
00100 rand_getbyte(rand_t *r)
00101 {
00102         uint8_t si, sj;
00103 
00104         r->i = (r->i + 1);
00105         si = r->s[r->i];
00106         r->j = (r->j + si);
00107         sj = r->s[r->j];
00108         r->s[r->i] = sj;
00109         r->s[r->j] = si;
00110         return (r->s[(si + sj) & 0xff]);
00111 }
00112 
00113 int
00114 rand_get(rand_t *r, void *buf, size_t len)
00115 {
00116         u_char *p;
00117         u_int i;
00118 
00119         for (p = buf, i = 0; i < len; i++) {
00120                 p[i] = rand_getbyte(r);
00121         }
00122         return (0);
00123 }
00124 
00125 int
00126 rand_set(rand_t *r, const void *buf, size_t len)
00127 {
00128         rand_init(r);
00129         rand_addrandom(r, (u_char *)buf, len);
00130         rand_addrandom(r, (u_char *)buf, len);
00131         return (0);
00132 }
00133 
00134 int
00135 rand_add(rand_t *r, const void *buf, size_t len)
00136 {
00137         rand_addrandom(r, (u_char *)buf, len);
00138         return (0);
00139 }
00140 
00141 uint8_t
00142 rand_uint8(rand_t *r)
00143 {
00144         return (rand_getbyte(r));
00145 }
00146 
00147 uint16_t
00148 rand_uint16(rand_t *r)
00149 {
00150         uint16_t val;
00151 
00152         val = rand_getbyte(r) << 8;
00153         val |= rand_getbyte(r);
00154         return (val);
00155 }
00156 
00157 uint32_t
00158 rand_uint32(rand_t *r)
00159 {
00160         uint32_t val;
00161 
00162         val = rand_getbyte(r) << 24;
00163         val |= rand_getbyte(r) << 16;
00164         val |= rand_getbyte(r) << 8;
00165         val |= rand_getbyte(r);
00166         return (val);
00167 }
00168 
00169 int
00170 rand_shuffle(rand_t *r, void *base, size_t nmemb, size_t size)
00171 {
00172         u_char *save, *src, *dst, *start = (u_char *)base;
00173         u_int i, j;
00174 
00175         if (nmemb < 2)
00176                 return (0);
00177         
00178         if ((u_int)r->tmplen < size) {
00179                 if (r->tmp == NULL) {
00180                         if ((save = malloc(size)) == NULL)
00181                                 return (-1);
00182                 } else if ((save = realloc(r->tmp, size)) == NULL)
00183                         return (-1);
00184                 
00185                 r->tmp = save;
00186                 r->tmplen = size;
00187         } else
00188                 save = r->tmp;
00189         
00190         for (i = 0; i < nmemb; i++) {
00191                 if ((j = rand_uint32(r) % (nmemb - 1)) != i) {
00192                         src = start + (size * i);
00193                         dst = start + (size * j);
00194                         memcpy(save, dst, size);
00195                         memcpy(dst, src, size);
00196                         memcpy(src, save, size);
00197                 }
00198         }
00199         return (0);
00200 }
00201 
00202 rand_t *
00203 rand_close(rand_t *r)
00204 {
00205         if (r != NULL) {
00206                 if (r->tmp != NULL)
00207                         free(r->tmp);
00208                 free(r);
00209         }
00210         return (NULL);
00211 }

Generated on Sun May 14 14:51:12 2006 by  doxygen 1.4.2