"Fossies" - the Fresh Open Source Software Archive

Member "sudo-1.9.11p3/lib/util/arc4random_uniform.c" (12 Jun 2022, 2236 Bytes) of package /linux/misc/sudo-1.9.11p3.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "arc4random_uniform.c" see the Fossies "Dox" file reference documentation.

    1 /*  $OpenBSD: arc4random_uniform.c,v 1.2 2015/09/13 08:31:47 guenther Exp $ */
    2 
    3 /*
    4  * SPDX-License-Identifier: ISC
    5  *
    6  * Copyright (c) 2008, Damien Miller <djm@openbsd.org>
    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 /*
   22  * This is an open source non-commercial project. Dear PVS-Studio, please check it.
   23  * PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
   24  */
   25 
   26 #include <config.h>
   27 
   28 #ifndef HAVE_ARC4RANDOM_UNIFORM
   29 
   30 #include <stdlib.h>
   31 #if defined(HAVE_STDINT_H)
   32 # include <stdint.h>
   33 #elif defined(HAVE_INTTYPES_H)
   34 # include <inttypes.h>
   35 #endif
   36 
   37 #include "sudo_compat.h"
   38 #include "sudo_rand.h"
   39 
   40 /*
   41  * Calculate a uniformly distributed random number less than upper_bound
   42  * avoiding "modulo bias".
   43  *
   44  * Uniformity is achieved by generating new random numbers until the one
   45  * returned is outside the range [0, 2**32 % upper_bound).  This
   46  * guarantees the selected random number will be inside
   47  * [2**32 % upper_bound, 2**32) which maps back to [0, upper_bound)
   48  * after reduction modulo upper_bound.
   49  */
   50 uint32_t
   51 sudo_arc4random_uniform(uint32_t upper_bound)
   52 {
   53     uint32_t r, min;
   54 
   55     if (upper_bound < 2)
   56         return 0;
   57 
   58     /* 2**32 % x == (2**32 - x) % x */
   59     min = -upper_bound % upper_bound;
   60 
   61     /*
   62      * This could theoretically loop forever but each retry has
   63      * p > 0.5 (worst case, usually far better) of selecting a
   64      * number inside the range we need, so it should rarely need
   65      * to re-roll.
   66      */
   67     for (;;) {
   68         r = arc4random();
   69         if (r >= min)
   70             break;
   71     }
   72 
   73     return r % upper_bound;
   74 }
   75 
   76 #endif /* HAVE_ARC4RANDOM_UNIFORM */