/src/open5gs/lib/core/ogs-rand.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright (C) 2019 by Sukchan Lee <acetcom@gmail.com> |
3 | | * |
4 | | * This file is part of Open5GS. |
5 | | * |
6 | | * This program is free software: you can redistribute it and/or modify |
7 | | * it under the terms of the GNU Affero General Public License as published by |
8 | | * the Free Software Foundation, either version 3 of the License, or |
9 | | * (at your option) any later version. |
10 | | * |
11 | | * This program is distributed in the hope that it will be useful, |
12 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | | * GNU General Public License for more details. |
15 | | * |
16 | | * You should have received a copy of the GNU General Public License |
17 | | * along with this program. If not, see <https://www.gnu.org/licenses/>. |
18 | | */ |
19 | | |
20 | | #include "core-config-private.h" |
21 | | |
22 | | #if HAVE_SYS_PARAM_H |
23 | | #include <sys/param.h> |
24 | | #endif |
25 | | |
26 | | #if HAVE_SYS_RANDOM_H |
27 | | #include <sys/random.h> |
28 | | #endif |
29 | | |
30 | | #if HAVE_FCNTL_H |
31 | | #include <fcntl.h> |
32 | | #endif |
33 | | |
34 | | #if HAVE_UNISTD_H |
35 | | #include <unistd.h> |
36 | | #endif |
37 | | |
38 | | #include "ogs-core.h" |
39 | | |
40 | | void ogs_random(void *buf, size_t buflen) |
41 | 0 | { |
42 | | #ifdef _WIN32 |
43 | | void arc4random_buf(void *buf, size_t n); |
44 | | arc4random_buf(buf, buflen); |
45 | | #elif defined(HAVE_GETRANDOM) |
46 | 0 | int rc = getrandom(buf, buflen, GRND_NONBLOCK); |
47 | 0 | if (rc < 0) { |
48 | 0 | ogs_log_message(OGS_LOG_FATAL, ogs_errno, "getrandom() failed"); |
49 | 0 | ogs_assert_if_reached(); |
50 | 0 | } |
51 | | #elif defined(HAVE_ARC4RANDOM_BUF) |
52 | | arc4random_buf(buf, buflen); |
53 | | #elif defined(OGS_DEV_RANDOM) |
54 | | int fd = -1; |
55 | | |
56 | | /* On BSD/OS 4.1, /dev/random gives out 8 bytes at a time, then |
57 | | * gives EOF, so reading 'buflen' bytes may require opening the |
58 | | * device several times. */ |
59 | | do { |
60 | | int rc; |
61 | | |
62 | | if (fd == -1) |
63 | | if ((fd = open(OGS_DEV_RANDOM, O_RDONLY)) == -1) { |
64 | | ogs_log_message(OGS_LOG_FATAL, ogs_errno, "open() failed"); |
65 | | ogs_assert_if_reached(); |
66 | | } |
67 | | |
68 | | do { |
69 | | rc = read(fd, buf, buflen); |
70 | | } while (rc == -1 && errno == EINTR); |
71 | | |
72 | | if (rc < 0) { |
73 | | ogs_log_message(OGS_LOG_ERROR, ogs_errno, "read() failed"); |
74 | | close(fd); |
75 | | return; |
76 | | } |
77 | | else if (rc == 0) { |
78 | | close(fd); |
79 | | fd = -1; /* force open() again */ |
80 | | } |
81 | | else { |
82 | | buf = (unsigned char *)buf + rc; |
83 | | buflen -= rc; |
84 | | } |
85 | | } while (buflen > 0); |
86 | | |
87 | | close(fd); |
88 | | #else |
89 | | #error cannot find a method which finds a pseudo-random number |
90 | | #endif |
91 | 0 | } |
92 | | |
93 | | uint32_t ogs_random32(void) |
94 | 0 | { |
95 | | #ifdef _WIN32 |
96 | | uint32_t arc4random(void); |
97 | | return arc4random(); |
98 | | #elif defined(HAVE_ARC4RANDOM_BUF) |
99 | | return arc4random(); |
100 | | #else |
101 | 0 | uint32_t rand32; |
102 | |
|
103 | 0 | ogs_random(&rand32, sizeof(rand32)); |
104 | 0 | return rand32; |
105 | 0 | #endif |
106 | 0 | } |
107 | | |
108 | | #ifdef _WIN32 |
109 | | #define ARC4RANDOM_NO_INCLUDES |
110 | | #define ARC4_LOCK_() do {} while(0) |
111 | | #define ARC4_UNLOCK_() do {} while(0) |
112 | | |
113 | | #define ARC4RANDOM_UINT32 uint32_t |
114 | | #define ARC4RANDOM_NOSTIR |
115 | | #define ARC4RANDOM_NOADDRANDOM |
116 | | #define ARC4RANDOM_NOUNIFORM |
117 | | |
118 | | /** |
119 | | * Stolen from libevent library. |
120 | | * https://github.com/libevent/libevent/blob/master/evutil.c |
121 | | */ |
122 | | void *(*volatile evutil_memset_volatile_)(void *, int, size_t) = memset; |
123 | | |
124 | | static void evutil_memclear_(void *mem, size_t len) |
125 | | { |
126 | | evutil_memset_volatile_(mem, 0, len); |
127 | | } |
128 | | |
129 | | #include "arc4random.c" |
130 | | #endif |
131 | | |