"Fossies" - the Fresh Open Source Software Archive

Member "sudo-1.9.11p3/lib/util/arc4random_buf.c" (12 Jun 2022, 1780 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_buf.c" see the Fossies "Dox" file reference documentation.

    1 /*
    2  * SPDX-License-Identifier: ISC
    3  *
    4  * Copyright (c) 2020 Todd C. Miller <Todd.Miller@sudo.ws>
    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 /*
   20  * This is an open source non-commercial project. Dear PVS-Studio, please check it.
   21  * PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
   22  */
   23 
   24 #include <config.h>
   25 
   26 #ifndef HAVE_ARC4RANDOM_BUF
   27 
   28 #include <stdlib.h>
   29 #if defined(HAVE_STDINT_H)
   30 # include <stdint.h>
   31 #elif defined(HAVE_INTTYPES_H)
   32 # include <inttypes.h>
   33 #endif
   34 
   35 #include "sudo_compat.h"
   36 #include "sudo_rand.h"
   37 
   38 #define minimum(a, b) ((a) < (b) ? (a) : (b))
   39 
   40 /*
   41  * Call arc4random() repeatedly to fill buf with n bytes of random data.
   42  */
   43 void
   44 sudo_arc4random_buf(void *buf, size_t n)
   45 {
   46     char *cp = buf;
   47 
   48     while (n != 0) {
   49         size_t m = minimum(n, 4);
   50         uint32_t val = arc4random();
   51 
   52         switch (m) {
   53         case 4:
   54             *cp++ = (val >> 24) & 0xff;
   55             FALLTHROUGH;
   56         case 3:
   57             *cp++ = (val >> 16) & 0xff;
   58             FALLTHROUGH;
   59         case 2:
   60             *cp++ = (val >> 8) & 0xff;
   61             FALLTHROUGH;
   62         case 1:
   63             *cp++ = val & 0xff;
   64             break;
   65         }
   66         n -= m;
   67     }
   68 }
   69 
   70 #endif /* HAVE_ARC4RANDOM_BUF */