xref: /freebsd-src/crypto/openssh/openbsd-compat/arc4random_uniform.c (revision 38a52bd3b5cac3da6f7f6eef3dd050e6aa08ebb3)
1*38a52bd3SEd Maste /*	$OpenBSD: arc4random_uniform.c,v 1.3 2019/01/20 02:59:07 bcook Exp $	*/
2*38a52bd3SEd Maste 
3*38a52bd3SEd Maste /*
4*38a52bd3SEd Maste  * Copyright (c) 2008, Damien Miller <djm@openbsd.org>
5*38a52bd3SEd Maste  *
6*38a52bd3SEd Maste  * Permission to use, copy, modify, and distribute this software for any
7*38a52bd3SEd Maste  * purpose with or without fee is hereby granted, provided that the above
8*38a52bd3SEd Maste  * copyright notice and this permission notice appear in all copies.
9*38a52bd3SEd Maste  *
10*38a52bd3SEd Maste  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11*38a52bd3SEd Maste  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12*38a52bd3SEd Maste  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13*38a52bd3SEd Maste  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14*38a52bd3SEd Maste  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15*38a52bd3SEd Maste  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16*38a52bd3SEd Maste  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*38a52bd3SEd Maste  */
18*38a52bd3SEd Maste 
19*38a52bd3SEd Maste /* OPENBSD ORIGINAL: lib/libc/crypto/arc4random_uniform.c */
20*38a52bd3SEd Maste 
21*38a52bd3SEd Maste #include "includes.h"
22*38a52bd3SEd Maste 
23*38a52bd3SEd Maste #ifdef HAVE_STDINT_H
24*38a52bd3SEd Maste # include <stdint.h>
25*38a52bd3SEd Maste #endif
26*38a52bd3SEd Maste #include <stdlib.h>
27*38a52bd3SEd Maste 
28*38a52bd3SEd Maste #ifndef HAVE_ARC4RANDOM_UNIFORM
29*38a52bd3SEd Maste /*
30*38a52bd3SEd Maste  * Calculate a uniformly distributed random number less than upper_bound
31*38a52bd3SEd Maste  * avoiding "modulo bias".
32*38a52bd3SEd Maste  *
33*38a52bd3SEd Maste  * Uniformity is achieved by generating new random numbers until the one
34*38a52bd3SEd Maste  * returned is outside the range [0, 2**32 % upper_bound).  This
35*38a52bd3SEd Maste  * guarantees the selected random number will be inside
36*38a52bd3SEd Maste  * [2**32 % upper_bound, 2**32) which maps back to [0, upper_bound)
37*38a52bd3SEd Maste  * after reduction modulo upper_bound.
38*38a52bd3SEd Maste  */
39*38a52bd3SEd Maste uint32_t
arc4random_uniform(uint32_t upper_bound)40*38a52bd3SEd Maste arc4random_uniform(uint32_t upper_bound)
41*38a52bd3SEd Maste {
42*38a52bd3SEd Maste 	uint32_t r, min;
43*38a52bd3SEd Maste 
44*38a52bd3SEd Maste 	if (upper_bound < 2)
45*38a52bd3SEd Maste 		return 0;
46*38a52bd3SEd Maste 
47*38a52bd3SEd Maste 	/* 2**32 % x == (2**32 - x) % x */
48*38a52bd3SEd Maste 	min = -upper_bound % upper_bound;
49*38a52bd3SEd Maste 
50*38a52bd3SEd Maste 	/*
51*38a52bd3SEd Maste 	 * This could theoretically loop forever but each retry has
52*38a52bd3SEd Maste 	 * p > 0.5 (worst case, usually far better) of selecting a
53*38a52bd3SEd Maste 	 * number inside the range we need, so it should rarely need
54*38a52bd3SEd Maste 	 * to re-roll.
55*38a52bd3SEd Maste 	 */
56*38a52bd3SEd Maste 	for (;;) {
57*38a52bd3SEd Maste 		r = arc4random();
58*38a52bd3SEd Maste 		if (r >= min)
59*38a52bd3SEd Maste 			break;
60*38a52bd3SEd Maste 	}
61*38a52bd3SEd Maste 
62*38a52bd3SEd Maste 	return r % upper_bound;
63*38a52bd3SEd Maste }
64*38a52bd3SEd Maste #endif /* !HAVE_ARC4RANDOM_UNIFORM */
65