| 1 |
/* |
| 2 |
* Copyright (c) 1996, David Mazieres <[email protected]> |
| 3 |
* Copyright (c) 2008, Damien Miller <[email protected]> |
| 4 |
* Copyright (c) 2013, Markus Friedl <[email protected]> |
| 5 |
* Copyright (c) 2014, Theo de Raadt <[email protected]> |
| 6 |
* Copyright (c) 2015, Sudhi Herle <[email protected]> |
| 7 |
* |
| 8 |
* Permission to use, copy, modify, and distribute this software for any |
| 9 |
* purpose with or without fee is hereby granted, provided that the above |
| 10 |
* copyright notice and this permission notice appear in all copies. |
| 11 |
* |
| 12 |
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 13 |
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 14 |
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 15 |
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 16 |
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 17 |
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 18 |
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 19 |
*/ |
| 20 |
|
| 21 |
// MP support header |
| 22 |
#include "MacportsLegacySupport.h" |
| 23 |
#if __MP_LEGACY_SUPPORT_ARC4RANDOM__ |
| 24 |
|
| 25 |
/* |
| 26 |
* ChaCha based random number generator from OpenBSD. |
| 27 |
* |
| 28 |
* Made fully portable and thread-safe by Sudhi Herle. |
| 29 |
*/ |
| 30 |
|
| 31 |
#include <fcntl.h> |
| 32 |
#include <limits.h> |
| 33 |
#include <signal.h> |
| 34 |
#include <stdint.h> |
| 35 |
#include <stdlib.h> |
| 36 |
#include <string.h> |
| 37 |
#include <unistd.h> |
| 38 |
#include <sys/types.h> |
| 39 |
#include <sys/time.h> |
| 40 |
#include <sys/random.h> |
| 41 |
#include <assert.h> |
| 42 |
#include <pthread.h> |
| 43 |
#include <stdio.h> |
| 44 |
#include <stdarg.h> |
| 45 |
|
| 46 |
|
| 47 |
#define ARC4R_KEYSZ 32 |
| 48 |
#define ARC4R_IVSZ 8 |
| 49 |
#define ARC4R_BLOCKSZ 64 |
| 50 |
#define ARC4R_RSBUFSZ (16*ARC4R_BLOCKSZ) |
| 51 |
|
| 52 |
typedef struct |
| 53 |
{ |
| 54 |
uint32_t input[16]; /* could be compressed */ |
| 55 |
} chacha_ctx; |
| 56 |
|
| 57 |
struct rand_state |
| 58 |
{ |
| 59 |
size_t rs_have; /* valid bytes at end of rs_buf */ |
| 60 |
size_t rs_count; /* bytes till reseed */ |
| 61 |
pid_t rs_pid; /* My PID */ |
| 62 |
chacha_ctx rs_chacha; /* chacha context for random keystream */ |
| 63 |
u_char rs_buf[ARC4R_RSBUFSZ]; /* keystream blocks */ |
| 64 |
}; |
| 65 |
typedef struct rand_state rand_state; |
| 66 |
|
| 67 |
#define KEYSTREAM_ONLY |
| 68 |
|
| 69 |
typedef unsigned char u8; |
| 70 |
typedef uint32_t u32; |
| 71 |
|
| 72 |
|
| 73 |
#define U8C(v) (v##U) |
| 74 |
#define U32C(v) (v##U) |
| 75 |
|
| 76 |
#define U8V(v) ((u8)(v) & U8C(0xFF)) |
| 77 |
#define U32V(v) ((u32)(v) & U32C(0xFFFFFFFF)) |
| 78 |
|
| 79 |
#define ROTL32(v, n) \ |
| 80 |
(U32V((v) << (n)) | ((v) >> (32 - (n)))) |
| 81 |
|
| 82 |
#define U8TO32_LITTLE(p) \ |
| 83 |
(((u32)((p)[0]) ) | \ |
| 84 |
((u32)((p)[1]) << 8) | \ |
| 85 |
((u32)((p)[2]) << 16) | \ |
| 86 |
((u32)((p)[3]) << 24)) |
| 87 |
|
| 88 |
#define U32TO8_LITTLE(p, v) \ |
| 89 |
do { \ |
| 90 |
(p)[0] = U8V((v) ); \ |
| 91 |
(p)[1] = U8V((v) >> 8); \ |
| 92 |
(p)[2] = U8V((v) >> 16); \ |
| 93 |
(p)[3] = U8V((v) >> 24); \ |
| 94 |
} while (0) |
| 95 |
|
| 96 |
#define ROTATE(v,c) (ROTL32(v,c)) |
| 97 |
#define XOR(v,w) ((v) ^ (w)) |
| 98 |
#define PLUS(v,w) (U32V((v) + (w))) |
| 99 |
#define PLUSONE(v) (PLUS((v),1)) |
| 100 |
|
| 101 |
#define QUARTERROUND(a,b,c,d) \ |
| 102 |
a = PLUS(a,b); d = ROTATE(XOR(d,a),16); \ |
| 103 |
c = PLUS(c,d); b = ROTATE(XOR(b,c),12); \ |
| 104 |
a = PLUS(a,b); d = ROTATE(XOR(d,a), 8); \ |
| 105 |
c = PLUS(c,d); b = ROTATE(XOR(b,c), 7); |
| 106 |
|
| 107 |
static const char sigma[16] = "expand 32-byte k"; |
| 108 |
static const char tau[16] = "expand 16-byte k"; |
| 109 |
|
| 110 |
static void |
| 111 |
_chacha_keysetup(chacha_ctx *x,const u8 *k,u32 kbits,u32 ivbits) |
| 112 |
{ |
| 113 |
const char *constants; |
| 114 |
|
| 115 |
(void)ivbits; |
| 116 |
|
| 117 |
x->input[4] = U8TO32_LITTLE(k + 0); |
| 118 |
x->input[5] = U8TO32_LITTLE(k + 4); |
| 119 |
x->input[6] = U8TO32_LITTLE(k + 8); |
| 120 |
x->input[7] = U8TO32_LITTLE(k + 12); |
| 121 |
if (kbits == 256) { /* recommended */ |
| 122 |
k += 16; |
| 123 |
constants = sigma; |
| 124 |
} else { /* kbits == 128 */ |
| 125 |
constants = tau; |
| 126 |
} |
| 127 |
x->input[8] = U8TO32_LITTLE(k + 0); |
| 128 |
x->input[9] = U8TO32_LITTLE(k + 4); |
| 129 |
x->input[10] = U8TO32_LITTLE(k + 8); |
| 130 |
x->input[11] = U8TO32_LITTLE(k + 12); |
| 131 |
x->input[0] = U8TO32_LITTLE(constants + 0); |
| 132 |
x->input[1] = U8TO32_LITTLE(constants + 4); |
| 133 |
x->input[2] = U8TO32_LITTLE(constants + 8); |
| 134 |
x->input[3] = U8TO32_LITTLE(constants + 12); |
| 135 |
} |
| 136 |
|
| 137 |
static void |
| 138 |
_chacha_ivsetup(chacha_ctx *x,const u8 *iv) |
| 139 |
{ |
| 140 |
x->input[12] = 0; |
| 141 |
x->input[13] = 0; |
| 142 |
x->input[14] = U8TO32_LITTLE(iv + 0); |
| 143 |
x->input[15] = U8TO32_LITTLE(iv + 4); |
| 144 |
} |
| 145 |
|
| 146 |
static void |
| 147 |
_chacha_encrypt_bytes(chacha_ctx *x,const u8 *m,u8 *c,u32 bytes) |
| 148 |
{ |
| 149 |
u32 x0, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15; |
| 150 |
u32 j0, j1, j2, j3, j4, j5, j6, j7, j8, j9, j10, j11, j12, j13, j14, j15; |
| 151 |
u8 *ctarget = NULL; |
| 152 |
u8 tmp[64]; |
| 153 |
u_int i; |
| 154 |
|
| 155 |
if (!bytes) return; |
| 156 |
|
| 157 |
j0 = x->input[0]; |
| 158 |
j1 = x->input[1]; |
| 159 |
j2 = x->input[2]; |
| 160 |
j3 = x->input[3]; |
| 161 |
j4 = x->input[4]; |
| 162 |
j5 = x->input[5]; |
| 163 |
j6 = x->input[6]; |
| 164 |
j7 = x->input[7]; |
| 165 |
j8 = x->input[8]; |
| 166 |
j9 = x->input[9]; |
| 167 |
j10 = x->input[10]; |
| 168 |
j11 = x->input[11]; |
| 169 |
j12 = x->input[12]; |
| 170 |
j13 = x->input[13]; |
| 171 |
j14 = x->input[14]; |
| 172 |
j15 = x->input[15]; |
| 173 |
|
| 174 |
for (;;) { |
| 175 |
if (bytes < 64) { |
| 176 |
for (i = 0;i < bytes;++i) tmp[i] = m[i]; |
| 177 |
m = tmp; |
| 178 |
ctarget = c; |
| 179 |
c = tmp; |
| 180 |
} |
| 181 |
x0 = j0; |
| 182 |
x1 = j1; |
| 183 |
x2 = j2; |
| 184 |
x3 = j3; |
| 185 |
x4 = j4; |
| 186 |
x5 = j5; |
| 187 |
x6 = j6; |
| 188 |
x7 = j7; |
| 189 |
x8 = j8; |
| 190 |
x9 = j9; |
| 191 |
x10 = j10; |
| 192 |
x11 = j11; |
| 193 |
x12 = j12; |
| 194 |
x13 = j13; |
| 195 |
x14 = j14; |
| 196 |
x15 = j15; |
| 197 |
for (i = 20;i > 0;i -= 2) { |
| 198 |
QUARTERROUND( x0, x4, x8,x12) |
| 199 |
QUARTERROUND( x1, x5, x9,x13) |
| 200 |
QUARTERROUND( x2, x6,x10,x14) |
| 201 |
QUARTERROUND( x3, x7,x11,x15) |
| 202 |
QUARTERROUND( x0, x5,x10,x15) |
| 203 |
QUARTERROUND( x1, x6,x11,x12) |
| 204 |
QUARTERROUND( x2, x7, x8,x13) |
| 205 |
QUARTERROUND( x3, x4, x9,x14) |
| 206 |
} |
| 207 |
x0 = PLUS(x0,j0); |
| 208 |
x1 = PLUS(x1,j1); |
| 209 |
x2 = PLUS(x2,j2); |
| 210 |
x3 = PLUS(x3,j3); |
| 211 |
x4 = PLUS(x4,j4); |
| 212 |
x5 = PLUS(x5,j5); |
| 213 |
x6 = PLUS(x6,j6); |
| 214 |
x7 = PLUS(x7,j7); |
| 215 |
x8 = PLUS(x8,j8); |
| 216 |
x9 = PLUS(x9,j9); |
| 217 |
x10 = PLUS(x10,j10); |
| 218 |
x11 = PLUS(x11,j11); |
| 219 |
x12 = PLUS(x12,j12); |
| 220 |
x13 = PLUS(x13,j13); |
| 221 |
x14 = PLUS(x14,j14); |
| 222 |
x15 = PLUS(x15,j15); |
| 223 |
|
| 224 |
#ifndef KEYSTREAM_ONLY |
| 225 |
x0 = XOR(x0,U8TO32_LITTLE(m + 0)); |
| 226 |
x1 = XOR(x1,U8TO32_LITTLE(m + 4)); |
| 227 |
x2 = XOR(x2,U8TO32_LITTLE(m + 8)); |
| 228 |
x3 = XOR(x3,U8TO32_LITTLE(m + 12)); |
| 229 |
x4 = XOR(x4,U8TO32_LITTLE(m + 16)); |
| 230 |
x5 = XOR(x5,U8TO32_LITTLE(m + 20)); |
| 231 |
x6 = XOR(x6,U8TO32_LITTLE(m + 24)); |
| 232 |
x7 = XOR(x7,U8TO32_LITTLE(m + 28)); |
| 233 |
x8 = XOR(x8,U8TO32_LITTLE(m + 32)); |
| 234 |
x9 = XOR(x9,U8TO32_LITTLE(m + 36)); |
| 235 |
x10 = XOR(x10,U8TO32_LITTLE(m + 40)); |
| 236 |
x11 = XOR(x11,U8TO32_LITTLE(m + 44)); |
| 237 |
x12 = XOR(x12,U8TO32_LITTLE(m + 48)); |
| 238 |
x13 = XOR(x13,U8TO32_LITTLE(m + 52)); |
| 239 |
x14 = XOR(x14,U8TO32_LITTLE(m + 56)); |
| 240 |
x15 = XOR(x15,U8TO32_LITTLE(m + 60)); |
| 241 |
#endif |
| 242 |
|
| 243 |
j12 = PLUSONE(j12); |
| 244 |
if (!j12) { |
| 245 |
j13 = PLUSONE(j13); |
| 246 |
/* stopping at 2^70 bytes per nonce is user's responsibility */ |
| 247 |
} |
| 248 |
|
| 249 |
U32TO8_LITTLE(c + 0,x0); |
| 250 |
U32TO8_LITTLE(c + 4,x1); |
| 251 |
U32TO8_LITTLE(c + 8,x2); |
| 252 |
U32TO8_LITTLE(c + 12,x3); |
| 253 |
U32TO8_LITTLE(c + 16,x4); |
| 254 |
U32TO8_LITTLE(c + 20,x5); |
| 255 |
U32TO8_LITTLE(c + 24,x6); |
| 256 |
U32TO8_LITTLE(c + 28,x7); |
| 257 |
U32TO8_LITTLE(c + 32,x8); |
| 258 |
U32TO8_LITTLE(c + 36,x9); |
| 259 |
U32TO8_LITTLE(c + 40,x10); |
| 260 |
U32TO8_LITTLE(c + 44,x11); |
| 261 |
U32TO8_LITTLE(c + 48,x12); |
| 262 |
U32TO8_LITTLE(c + 52,x13); |
| 263 |
U32TO8_LITTLE(c + 56,x14); |
| 264 |
U32TO8_LITTLE(c + 60,x15); |
| 265 |
|
| 266 |
if (bytes <= 64) { |
| 267 |
if (bytes < 64) { |
| 268 |
for (i = 0;i < bytes;++i) ctarget[i] = c[i]; |
| 269 |
} |
| 270 |
x->input[12] = j12; |
| 271 |
x->input[13] = j13; |
| 272 |
return; |
| 273 |
} |
| 274 |
bytes -= 64; |
| 275 |
c += 64; |
| 276 |
#ifndef KEYSTREAM_ONLY |
| 277 |
m += 64; |
| 278 |
#endif |
| 279 |
} |
| 280 |
} |
| 281 |
|
| 282 |
#define minimum(a, b) ((a) < (b) ? (a) : (b)) |
| 283 |
|
| 284 |
#include "arc4random.h" |
| 285 |
|
| 286 |
|
| 287 |
static inline void |
| 288 |
_rs_init(rand_state* st, u8 *buf, size_t n) |
| 289 |
{ |
| 290 |
assert(n >= (ARC4R_KEYSZ + ARC4R_IVSZ)); |
| 291 |
|
| 292 |
_chacha_keysetup(&st->rs_chacha, buf, ARC4R_KEYSZ * 8, 0); |
| 293 |
_chacha_ivsetup(&st->rs_chacha, buf + ARC4R_KEYSZ); |
| 294 |
} |
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
static inline void |
| 299 |
_rs_rekey(rand_state* st, u8 *dat, size_t datlen) |
| 300 |
{ |
| 301 |
/* fill rs_buf with the keystream */ |
| 302 |
_chacha_encrypt_bytes(&st->rs_chacha, st->rs_buf, st->rs_buf, sizeof st->rs_buf); |
| 303 |
|
| 304 |
/* mix in optional user provided data */ |
| 305 |
if (dat) { |
| 306 |
size_t i, m; |
| 307 |
|
| 308 |
m = minimum(datlen, ARC4R_KEYSZ + ARC4R_IVSZ); |
| 309 |
for (i = 0; i < m; i++) |
| 310 |
st->rs_buf[i] ^= dat[i]; |
| 311 |
|
| 312 |
memset(dat, 0, datlen); |
| 313 |
} |
| 314 |
|
| 315 |
/* immediately reinit for backtracking resistance */ |
| 316 |
_rs_init(st, st->rs_buf, ARC4R_KEYSZ + ARC4R_IVSZ); |
| 317 |
memset(st->rs_buf, 0, ARC4R_KEYSZ + ARC4R_IVSZ); |
| 318 |
st->rs_have = (sizeof st->rs_buf) - ARC4R_KEYSZ - ARC4R_IVSZ; |
| 319 |
} |
| 320 |
|
| 321 |
|
| 322 |
static void |
| 323 |
_rs_stir(rand_state* st) |
| 324 |
{ |
| 325 |
u8 rnd[ARC4R_KEYSZ + ARC4R_IVSZ]; |
| 326 |
|
| 327 |
|
| 328 |
int r = getentropy(rnd, sizeof rnd); |
| 329 |
assert(r == 0); |
| 330 |
|
| 331 |
_rs_rekey(st, rnd, sizeof(rnd)); |
| 332 |
|
| 333 |
/* invalidate rs_buf */ |
| 334 |
st->rs_have = 0; |
| 335 |
memset(st->rs_buf, 0, sizeof st->rs_buf); |
| 336 |
|
| 337 |
st->rs_count = 1600000; |
| 338 |
} |
| 339 |
|
| 340 |
|
| 341 |
static inline void |
| 342 |
_rs_stir_if_needed(rand_state* st, size_t len) |
| 343 |
{ |
| 344 |
if (st->rs_count <= len) |
| 345 |
_rs_stir(st); |
| 346 |
|
| 347 |
st->rs_count -= len; |
| 348 |
} |
| 349 |
|
| 350 |
|
| 351 |
static inline void |
| 352 |
_rs_random_buf(rand_state* rs, void *_buf, size_t n) |
| 353 |
{ |
| 354 |
u8 *buf = (u8 *)_buf; |
| 355 |
u8 *keystream; |
| 356 |
size_t m; |
| 357 |
|
| 358 |
_rs_stir_if_needed(rs, n); |
| 359 |
while (n > 0) { |
| 360 |
if (rs->rs_have > 0) { |
| 361 |
m = minimum(n, rs->rs_have); |
| 362 |
keystream = rs->rs_buf + sizeof(rs->rs_buf) - rs->rs_have; |
| 363 |
memcpy(buf, keystream, m); |
| 364 |
memset(keystream, 0, m); |
| 365 |
buf += m; |
| 366 |
n -= m; |
| 367 |
rs->rs_have -= m; |
| 368 |
} else |
| 369 |
_rs_rekey(rs, NULL, 0); |
| 370 |
} |
| 371 |
} |
| 372 |
|
| 373 |
static inline uint32_t |
| 374 |
_rs_random_u32(rand_state* rs) |
| 375 |
{ |
| 376 |
u8 *keystream; |
| 377 |
uint32_t val; |
| 378 |
|
| 379 |
_rs_stir_if_needed(rs, sizeof(val)); |
| 380 |
if (rs->rs_have < sizeof(val)) |
| 381 |
_rs_rekey(rs, NULL, 0); |
| 382 |
keystream = rs->rs_buf + sizeof(rs->rs_buf) - rs->rs_have; |
| 383 |
memcpy(&val, keystream, sizeof(val)); |
| 384 |
memset(keystream, 0, sizeof(val)); |
| 385 |
rs->rs_have -= sizeof(val); |
| 386 |
|
| 387 |
return val; |
| 388 |
} |
| 389 |
|
| 390 |
|
| 391 |
#if defined(__Darwin__) || defined(__APPLE__) |
| 392 |
|
| 393 |
/* |
| 394 |
* Multi-threaded support using pthread API. Needed for OS X: |
| 395 |
* |
| 396 |
* https://www.reddit.com/r/cpp/comments/3bg8jc/anyone_know_if_and_when_applexcode_will_support/ |
| 397 |
*/ |
| 398 |
static pthread_key_t Rkey; |
| 399 |
static pthread_once_t Ronce = PTHREAD_ONCE_INIT; |
| 400 |
static volatile uint32_t Rforked = 0; |
| 401 |
|
| 402 |
/* |
| 403 |
* Fork handler to reset my context |
| 404 |
*/ |
| 405 |
static void |
| 406 |
_atfork() |
| 407 |
{ |
| 408 |
// the pthread_atfork() callbacks called once per process. |
| 409 |
// We set it to be called by the child process. |
| 410 |
Rforked++; |
| 411 |
} |
| 412 |
|
| 413 |
/* |
| 414 |
* Run once and only once by pthread lib. We use the opportunity to |
| 415 |
* create the thread-specific key. |
| 416 |
*/ |
| 417 |
static void |
| 418 |
_screate() |
| 419 |
{ |
| 420 |
pthread_key_create(&Rkey, 0); |
| 421 |
pthread_atfork(0, 0, _atfork); |
| 422 |
|
| 423 |
/* |
| 424 |
* Get entropy once to initialize the fd - for non OpenBSD |
| 425 |
* systems. |
| 426 |
*/ |
| 427 |
uint8_t buf[8]; |
| 428 |
getentropy(buf, sizeof buf); |
| 429 |
} |
| 430 |
|
| 431 |
|
| 432 |
/* |
| 433 |
* Get the per-thread rand state. Initialize if needed. |
| 434 |
*/ |
| 435 |
static rand_state* |
| 436 |
_sget() |
| 437 |
{ |
| 438 |
pthread_once(&Ronce, _screate); |
| 439 |
|
| 440 |
volatile pthread_key_t* k = &Rkey; |
| 441 |
rand_state * z = (rand_state *)pthread_getspecific(*k); |
| 442 |
if (!z) { |
| 443 |
z = (rand_state*)calloc(sizeof *z, 1); |
| 444 |
assert(z); |
| 445 |
|
| 446 |
_rs_stir(z); |
| 447 |
z->rs_pid = getpid(); |
| 448 |
|
| 449 |
pthread_setspecific(*k, z); |
| 450 |
} |
| 451 |
|
| 452 |
/* Detect if a fork has happened */ |
| 453 |
if (Rforked > 0 || getpid() != z->rs_pid) { |
| 454 |
Rforked = 0; |
| 455 |
z->rs_pid = getpid(); |
| 456 |
_rs_stir(z); |
| 457 |
} |
| 458 |
|
| 459 |
return z; |
| 460 |
} |
| 461 |
|
| 462 |
#else |
| 463 |
|
| 464 |
/* |
| 465 |
* Use gcc extension to declare a thread-local variable. |
| 466 |
* |
| 467 |
* On most systems (including x86_64), thread-local access is |
| 468 |
* essentially free for non .so use cases. |
| 469 |
* |
| 470 |
*/ |
| 471 |
static __thread rand_state st = { .rs_count = 0, .rs_pid = 0 }; |
| 472 |
static inline rand_state* |
| 473 |
_sget() |
| 474 |
{ |
| 475 |
rand_state* s = &st; |
| 476 |
|
| 477 |
if (s->rs_count == 0 || getpid() != s->rs_pid) { |
| 478 |
_rs_stir(s); |
| 479 |
s->rs_pid = getpid(); |
| 480 |
} |
| 481 |
return s; |
| 482 |
} |
| 483 |
|
| 484 |
#endif /* __Darwin__ */ |
| 485 |
|
| 486 |
|
| 487 |
/* |
| 488 |
* Public API. |
| 489 |
*/ |
| 490 |
|
| 491 |
|
| 492 |
void |
| 493 |
arc4random_buf(void* b, size_t n) |
| 494 |
{ |
| 495 |
rand_state* z = _sget(); |
| 496 |
|
| 497 |
_rs_random_buf(z, b, n); |
| 498 |
} |
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
/* |
| 503 |
* Calculate a uniformly distributed random number less than upper_bound |
| 504 |
* avoiding "modulo bias". |
| 505 |
* |
| 506 |
* Uniformity is achieved by generating new random numbers until the one |
| 507 |
* returned is outside the range [0, 2**32 % upper_bound). This |
| 508 |
* guarantees the selected random number will be inside |
| 509 |
* [2**32 % upper_bound, 2**32) which maps back to [0, upper_bound) |
| 510 |
* after reduction modulo upper_bound. |
| 511 |
*/ |
| 512 |
uint32_t |
| 513 |
arc4random_uniform(uint32_t upper_bound) |
| 514 |
{ |
| 515 |
rand_state* z = _sget(); |
| 516 |
uint32_t r, min; |
| 517 |
|
| 518 |
if (upper_bound < 2) |
| 519 |
return 0; |
| 520 |
|
| 521 |
/* 2**32 % x == (2**32 - x) % x */ |
| 522 |
min = -upper_bound % upper_bound; |
| 523 |
|
| 524 |
/* |
| 525 |
* This could theoretically loop forever but each retry has |
| 526 |
* p > 0.5 (worst case, usually far better) of selecting a |
| 527 |
* number inside the range we need, so it should rarely need |
| 528 |
* to re-roll. |
| 529 |
*/ |
| 530 |
for (;;) { |
| 531 |
r = _rs_random_u32(z); |
| 532 |
if (r >= min) |
| 533 |
break; |
| 534 |
} |
| 535 |
|
| 536 |
return r % upper_bound; |
| 537 |
} |
| 538 |
|
| 539 |
|
| 540 |
#endif /* __MP_LEGACY_SUPPORT_ARC4RANDOM__ */ |