summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorderaadt <>2014-07-12 13:24:54 +0000
committerderaadt <>2014-07-12 13:24:54 +0000
commit0b572b36cd1a45a713c9eef52318dcae68872a88 (patch)
tree87c53630638b48e9ce6aa888a3b3528c4cfb417b
parent63ded69bef1d48c9f7c0876d19c00d670afa65ae (diff)
downloadopenbsd-0b572b36cd1a45a713c9eef52318dcae68872a88.tar.gz
openbsd-0b572b36cd1a45a713c9eef52318dcae68872a88.tar.bz2
openbsd-0b572b36cd1a45a713c9eef52318dcae68872a88.zip
Split arc4random_uniform into it's own file, to assist other projects
now using this as upstream code. The particular problem is systems that contain older arc4random derivations lacking arc4random_uniform(). ok tedu miod
-rw-r--r--src/lib/libc/crypt/Makefile.inc5
-rw-r--r--src/lib/libc/crypt/arc4random.c38
-rw-r--r--src/lib/libc/crypt/arc4random_uniform.c56
3 files changed, 60 insertions, 39 deletions
diff --git a/src/lib/libc/crypt/Makefile.inc b/src/lib/libc/crypt/Makefile.inc
index 2ade095162..b4f25648e3 100644
--- a/src/lib/libc/crypt/Makefile.inc
+++ b/src/lib/libc/crypt/Makefile.inc
@@ -1,8 +1,9 @@
1# $OpenBSD: Makefile.inc,v 1.21 2014/05/16 22:11:00 jmc Exp $ 1# $OpenBSD: Makefile.inc,v 1.22 2014/07/12 13:24:54 deraadt Exp $
2 2
3.PATH: ${LIBCSRCDIR}/arch/${MACHINE_CPU}/crypt ${LIBCSRCDIR}/crypt 3.PATH: ${LIBCSRCDIR}/arch/${MACHINE_CPU}/crypt ${LIBCSRCDIR}/crypt
4 4
5SRCS+= crypt.c crypt2.c cryptutil.c arc4random.c blowfish.c bcrypt.c 5SRCS+= crypt.c crypt2.c cryptutil.c arc4random.c arc4random_uniform.c \
6 blowfish.c bcrypt.c
6 7
7MAN+= crypt.3 blowfish.3 arc4random.3 8MAN+= crypt.3 blowfish.3 arc4random.3
8MLINKS+=crypt.3 setkey.3 crypt.3 crypt_checkpass.3 crypt.3 encrypt.3 9MLINKS+=crypt.3 setkey.3 crypt.3 crypt_checkpass.3 crypt.3 encrypt.3
diff --git a/src/lib/libc/crypt/arc4random.c b/src/lib/libc/crypt/arc4random.c
index 13b94ed111..9460af1e54 100644
--- a/src/lib/libc/crypt/arc4random.c
+++ b/src/lib/libc/crypt/arc4random.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: arc4random.c,v 1.40 2014/07/09 16:52:09 bcook Exp $ */ 1/* $OpenBSD: arc4random.c,v 1.41 2014/07/12 13:24:54 deraadt Exp $ */
2 2
3/* 3/*
4 * Copyright (c) 1996, David Mazieres <dm@uun.org> 4 * Copyright (c) 1996, David Mazieres <dm@uun.org>
@@ -211,39 +211,3 @@ arc4random_buf(void *buf, size_t n)
211 _rs_random_buf(buf, n); 211 _rs_random_buf(buf, n);
212 _ARC4_UNLOCK(); 212 _ARC4_UNLOCK();
213} 213}
214
215/*
216 * Calculate a uniformly distributed random number less than upper_bound
217 * avoiding "modulo bias".
218 *
219 * Uniformity is achieved by generating new random numbers until the one
220 * returned is outside the range [0, 2**32 % upper_bound). This
221 * guarantees the selected random number will be inside
222 * [2**32 % upper_bound, 2**32) which maps back to [0, upper_bound)
223 * after reduction modulo upper_bound.
224 */
225uint32_t
226arc4random_uniform(uint32_t upper_bound)
227{
228 uint32_t r, min;
229
230 if (upper_bound < 2)
231 return 0;
232
233 /* 2**32 % x == (2**32 - x) % x */
234 min = -upper_bound % upper_bound;
235
236 /*
237 * This could theoretically loop forever but each retry has
238 * p > 0.5 (worst case, usually far better) of selecting a
239 * number inside the range we need, so it should rarely need
240 * to re-roll.
241 */
242 for (;;) {
243 r = arc4random();
244 if (r >= min)
245 break;
246 }
247
248 return r % upper_bound;
249}
diff --git a/src/lib/libc/crypt/arc4random_uniform.c b/src/lib/libc/crypt/arc4random_uniform.c
new file mode 100644
index 0000000000..1aa9a622f1
--- /dev/null
+++ b/src/lib/libc/crypt/arc4random_uniform.c
@@ -0,0 +1,56 @@
1/* $OpenBSD: arc4random_uniform.c,v 1.1 2014/07/12 13:24:54 deraadt Exp $ */
2
3/*
4 * Copyright (c) 2008, Damien Miller <djm@openbsd.org>
5 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19#include <sys/types.h>
20#include <stdlib.h>
21
22/*
23 * Calculate a uniformly distributed random number less than upper_bound
24 * avoiding "modulo bias".
25 *
26 * Uniformity is achieved by generating new random numbers until the one
27 * returned is outside the range [0, 2**32 % upper_bound). This
28 * guarantees the selected random number will be inside
29 * [2**32 % upper_bound, 2**32) which maps back to [0, upper_bound)
30 * after reduction modulo upper_bound.
31 */
32uint32_t
33arc4random_uniform(uint32_t upper_bound)
34{
35 uint32_t r, min;
36
37 if (upper_bound < 2)
38 return 0;
39
40 /* 2**32 % x == (2**32 - x) % x */
41 min = -upper_bound % upper_bound;
42
43 /*
44 * This could theoretically loop forever but each retry has
45 * p > 0.5 (worst case, usually far better) of selecting a
46 * number inside the range we need, so it should rarely need
47 * to re-roll.
48 */
49 for (;;) {
50 r = arc4random();
51 if (r >= min)
52 break;
53 }
54
55 return r % upper_bound;
56}