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 /*
39 * Call arc4random() repeatedly to fill buf with n bytes of random data.
40 */
41 void
sudo_arc4random_buf(void * buf,size_t n)42 sudo_arc4random_buf(void *buf, size_t n)
43 {
44 char *cp = buf;
45
46 while (n != 0) {
47 size_t m = minimum(n, 4);
48 uint32_t val = arc4random();
49
50 switch (m) {
51 case 4:
52 *cp++ = (val >> 24) & 0xff;
53 FALLTHROUGH;
54 case 3:
55 *cp++ = (val >> 16) & 0xff;
56 FALLTHROUGH;
57 case 2:
58 *cp++ = (val >> 8) & 0xff;
59 FALLTHROUGH;
60 case 1:
61 *cp++ = val & 0xff;
62 break;
63 }
64 n -= m;
65 }
66 }
67
68 #endif /* HAVE_ARC4RANDOM_BUF */
69