1 /* $NetBSD: evutil_rand.c,v 1.6 2024/08/18 20:47:21 christos Exp $ */
2
3 /*
4 * Copyright (c) 2007-2012 Niels Provos and Nick Mathewson
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 /* This file has our secure PRNG code. On platforms that have arc4random(),
30 * we just use that. Otherwise, we include arc4random.c as a bunch of static
31 * functions, and wrap it lightly. We don't expose the arc4random*() APIs
32 * because A) they aren't in our namespace, and B) it's not nice to name your
33 * APIs after their implementations. We keep them in a separate file
34 * so that other people can rip it out and use it for whatever.
35 */
36
37 #include "event2/event-config.h"
38 #include "evconfig-private.h"
39
40 #include <limits.h>
41
42 #include "util-internal.h"
43 #include "evthread-internal.h"
44
45 #ifdef EVENT__HAVE_ARC4RANDOM
46 #include <stdlib.h>
47 #include <string.h>
48 int
evutil_secure_rng_set_urandom_device_file(char * fname)49 evutil_secure_rng_set_urandom_device_file(char *fname)
50 {
51 (void) fname;
52 return -1;
53 }
54 int
evutil_secure_rng_init(void)55 evutil_secure_rng_init(void)
56 {
57 /* call arc4random() now to force it to self-initialize */
58 (void) arc4random();
59 return 0;
60 }
61 #ifndef EVENT__DISABLE_THREAD_SUPPORT
62 int
evutil_secure_rng_global_setup_locks_(const int enable_locks)63 evutil_secure_rng_global_setup_locks_(const int enable_locks)
64 {
65 return 0;
66 }
67 #endif
68 static void
evutil_free_secure_rng_globals_locks(void)69 evutil_free_secure_rng_globals_locks(void)
70 {
71 }
72
73 static void
ev_arc4random_buf(void * buf,size_t n)74 ev_arc4random_buf(void *buf, size_t n)
75 {
76 #if defined(EVENT__HAVE_ARC4RANDOM_BUF) && !defined(__APPLE__)
77 arc4random_buf(buf, n);
78 return;
79 #else
80 unsigned char *b = buf;
81
82 #if defined(EVENT__HAVE_ARC4RANDOM_BUF)
83 /* OSX 10.7 introducd arc4random_buf, so if you build your program
84 * there, you'll get surprised when older versions of OSX fail to run.
85 * To solve this, we can check whether the function pointer is set,
86 * and fall back otherwise. (OSX does this using some linker
87 * trickery.)
88 */
89 {
90 void (*tptr)(void *,size_t) =
91 (void (*)(void*,size_t))arc4random_buf;
92 if (tptr != NULL) {
93 arc4random_buf(buf, n);
94 return;
95 }
96 }
97 #endif
98 /* Make sure that we start out with b at a 4-byte alignment; plenty
99 * of CPUs care about this for 32-bit access. */
100 if (n >= 4 && ((ev_uintptr_t)b) & 3) {
101 ev_uint32_t u = arc4random();
102 int n_bytes = 4 - (((ev_uintptr_t)b) & 3);
103 memcpy(b, &u, n_bytes);
104 b += n_bytes;
105 n -= n_bytes;
106 }
107 while (n >= 4) {
108 *(ev_uint32_t*)b = arc4random();
109 b += 4;
110 n -= 4;
111 }
112 if (n) {
113 ev_uint32_t u = arc4random();
114 memcpy(b, &u, n);
115 }
116 #endif
117 }
118
119 #else /* !EVENT__HAVE_ARC4RANDOM { */
120
121 #ifdef EVENT__ssize_t
122 #define ssize_t EVENT__ssize_t
123 #endif
124 #define ARC4RANDOM_EXPORT static
125 #define ARC4_LOCK_() EVLOCK_LOCK(arc4rand_lock, 0)
126 #define ARC4_UNLOCK_() EVLOCK_UNLOCK(arc4rand_lock, 0)
127 #ifndef EVENT__DISABLE_THREAD_SUPPORT
128 static void *arc4rand_lock;
129 #endif
130
131 #define ARC4RANDOM_UINT32 ev_uint32_t
132 #define ARC4RANDOM_NOSTIR
133 #define ARC4RANDOM_NORANDOM
134 #define ARC4RANDOM_NOUNIFORM
135
136 #include "./arc4random.c"
137
138 #ifndef EVENT__DISABLE_THREAD_SUPPORT
139 int
evutil_secure_rng_global_setup_locks_(const int enable_locks)140 evutil_secure_rng_global_setup_locks_(const int enable_locks)
141 {
142 EVTHREAD_SETUP_GLOBAL_LOCK(arc4rand_lock, 0);
143 return 0;
144 }
145 #endif
146
147 static void
evutil_free_secure_rng_globals_locks(void)148 evutil_free_secure_rng_globals_locks(void)
149 {
150 #ifndef EVENT__DISABLE_THREAD_SUPPORT
151 if (arc4rand_lock != NULL) {
152 EVTHREAD_FREE_LOCK(arc4rand_lock, 0);
153 arc4rand_lock = NULL;
154 }
155 #endif
156 return;
157 }
158
159 int
evutil_secure_rng_set_urandom_device_file(char * fname)160 evutil_secure_rng_set_urandom_device_file(char *fname)
161 {
162 #ifdef TRY_SEED_URANDOM
163 ARC4_LOCK_();
164 arc4random_urandom_filename = fname;
165 ARC4_UNLOCK_();
166 #endif
167 return 0;
168 }
169
170 int
evutil_secure_rng_init(void)171 evutil_secure_rng_init(void)
172 {
173 int val;
174
175 ARC4_LOCK_();
176 val = (!arc4_stir()) ? 0 : -1;
177 ARC4_UNLOCK_();
178 return val;
179 }
180
181 static void
ev_arc4random_buf(void * buf,size_t n)182 ev_arc4random_buf(void *buf, size_t n)
183 {
184 arc4random_buf(buf, n);
185 }
186
187 #endif /* } !EVENT__HAVE_ARC4RANDOM */
188
189 void
evutil_secure_rng_get_bytes(void * buf,size_t n)190 evutil_secure_rng_get_bytes(void *buf, size_t n)
191 {
192 ev_arc4random_buf(buf, n);
193 }
194
195 #if !defined(EVENT__HAVE_ARC4RANDOM) || defined(EVENT__HAVE_ARC4RANDOM_ADDRANDOM)
196 void
evutil_secure_rng_add_bytes(const char * buf,size_t n)197 evutil_secure_rng_add_bytes(const char *buf, size_t n)
198 {
199 arc4random_addrandom((unsigned char*)buf,
200 n>(size_t)INT_MAX ? INT_MAX : (int)n);
201 }
202 #endif
203
204 void
evutil_free_secure_rng_globals_(void)205 evutil_free_secure_rng_globals_(void)
206 {
207 evutil_free_secure_rng_globals_locks();
208 }
209