]> andersk Git - openssh.git/blob - openbsd-compat/bsd-arc4random.c
- (djm) [openbsd-compat/bsd-arc4random.c openbsd-compat/openbsd-compat.c]
[openssh.git] / openbsd-compat / bsd-arc4random.c
1 /*
2  * Copyright (c) 1999,2000,2004 Damien Miller <djm@mindrot.org>
3  *
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.
7  *
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.
15  */
16
17 #include "includes.h"
18
19 #include <sys/types.h>
20
21 #include <string.h>
22 #include <stdarg.h>
23
24 #include "log.h"
25
26 #ifndef HAVE_ARC4RANDOM
27
28 #include <openssl/rand.h>
29 #include <openssl/rc4.h>
30 #include <openssl/err.h>
31
32 /* Size of key to use */
33 #define SEED_SIZE 20
34
35 /* Number of bytes to reseed after */
36 #define REKEY_BYTES     (1 << 24)
37
38 static int rc4_ready = 0;
39 static RC4_KEY rc4;
40
41 unsigned int
42 arc4random(void)
43 {
44         unsigned int r = 0;
45         static int first_time = 1;
46
47         if (rc4_ready <= 0) {
48                 if (first_time)
49                         seed_rng();
50                 first_time = 0;
51                 arc4random_stir();
52         }
53
54         RC4(&rc4, sizeof(r), (unsigned char *)&r, (unsigned char *)&r);
55
56         rc4_ready -= sizeof(r);
57         
58         return(r);
59 }
60
61 void
62 arc4random_stir(void)
63 {
64         unsigned char rand_buf[SEED_SIZE];
65         int i;
66
67         memset(&rc4, 0, sizeof(rc4));
68         if (RAND_bytes(rand_buf, sizeof(rand_buf)) <= 0)
69                 fatal("Couldn't obtain random bytes (error %ld)",
70                     ERR_get_error());
71         RC4_set_key(&rc4, sizeof(rand_buf), rand_buf);
72
73         /*
74          * Discard early keystream, as per recommendations in:
75          * http://www.wisdom.weizmann.ac.il/~itsik/RC4/Papers/Rc4_ksa.ps
76          */
77         for(i = 0; i <= 256; i += sizeof(rand_buf))
78                 RC4(&rc4, sizeof(rand_buf), rand_buf, rand_buf);
79
80         memset(rand_buf, 0, sizeof(rand_buf));
81
82         rc4_ready = REKEY_BYTES;
83 }
84 #endif /* !HAVE_ARC4RANDOM */
85
86 #ifndef ARC4RANDOM_BUF
87 void
88 arc4random_buf(void *_buf, size_t n)
89 {
90         size_t i;
91         u_int32_t r;
92         char *buf = (char *)_buf;
93
94         for (i = 0; i < n; i++) {
95                 if (i % 4 == 0)
96                         r = arc4random();
97                 buf[i] = r & 0xff;
98                 r >>= 8;
99         }
100         i = r = 0;
101 }
102 #endif /* !HAVE_ARC4RANDOM_BUF */
103
104 #ifndef ARC4RANDOM_UNIFORM
105 /*
106  * Calculate a uniformly distributed random number less than upper_bound
107  * avoiding "modulo bias".
108  *
109  * Uniformity is achieved by generating new random numbers until the one
110  * returned is outside the range [0, 2**32 % upper_bound).  This
111  * guarantees the selected random number will be inside
112  * [2**32 % upper_bound, 2**32) which maps back to [0, upper_bound)
113  * after reduction modulo upper_bound.
114  */
115 u_int32_t
116 arc4random_uniform(u_int32_t upper_bound)
117 {
118         u_int32_t r, min;
119
120         if (upper_bound < 2)
121                 return 0;
122
123 #if (ULONG_MAX > 0xffffffffUL)
124         min = 0x100000000UL % upper_bound;
125 #else
126         /* Calculate (2**32 % upper_bound) avoiding 64-bit math */
127         if (upper_bound > 0x80000000)
128                 min = 1 + ~upper_bound;         /* 2**32 - upper_bound */
129         else {
130                 /* (2**32 - (x * 2)) % x == 2**32 % x when x <= 2**31 */
131                 min = ((0xffffffff - (upper_bound << 2)) + 1) % upper_bound;
132         }
133 #endif
134
135         /*
136          * This could theoretically loop forever but each retry has
137          * p > 0.5 (worst case, usually far better) of selecting a
138          * number inside the range we need, so it should rarely need
139          * to re-roll.
140          */
141         for (;;) {
142                 r = arc4random();
143                 if (r >= min)
144                         break;
145         }
146
147         return r % upper_bound;
148 }
149 #endif /* !HAVE_ARC4RANDOM_UNIFORM */
This page took 0.047739 seconds and 5 git commands to generate.