1 /*        $NetBSD: evutil_rand.c,v 1.6 2021/04/07 03:36:48 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 <sys/cdefs.h>
39 __RCSID("$NetBSD: evutil_rand.c,v 1.6 2021/04/07 03:36:48 christos Exp $");
40 #include "evconfig-private.h"
41 
42 #include <limits.h>
43 
44 #include "util-internal.h"
45 #include "evthread-internal.h"
46 
47 #ifdef EVENT__HAVE_ARC4RANDOM
48 #include <stdlib.h>
49 #include <string.h>
50 int
evutil_secure_rng_set_urandom_device_file(char * fname)51 evutil_secure_rng_set_urandom_device_file(char *fname)
52 {
53           (void) fname;
54           return -1;
55 }
56 int
evutil_secure_rng_init(void)57 evutil_secure_rng_init(void)
58 {
59           /* call arc4random() now to force it to self-initialize */
60           (void) arc4random();
61           return 0;
62 }
63 #ifndef EVENT__DISABLE_THREAD_SUPPORT
64 int
65 /*ARGSUSED*/
evutil_secure_rng_global_setup_locks_(const int enable_locks)66 evutil_secure_rng_global_setup_locks_(const int enable_locks)
67 {
68           return 0;
69 }
70 #endif
71 static void
evutil_free_secure_rng_globals_locks(void)72 evutil_free_secure_rng_globals_locks(void)
73 {
74 }
75 
76 static void
77 /*ARGSUSED*/
ev_arc4random_buf(void * buf,size_t n)78 ev_arc4random_buf(void *buf, size_t n)
79 {
80 #if defined(EVENT__HAVE_ARC4RANDOM_BUF) && !defined(__APPLE__)
81           arc4random_buf(buf, n);
82           return;
83 #else
84           unsigned char *b = buf;
85 
86 #if defined(EVENT__HAVE_ARC4RANDOM_BUF)
87           /* OSX 10.7 introducd arc4random_buf, so if you build your program
88            * there, you'll get surprised when older versions of OSX fail to run.
89            * To solve this, we can check whether the function pointer is set,
90            * and fall back otherwise.  (OSX does this using some linker
91            * trickery.)
92            */
93           {
94                     void (*tptr)(void *,size_t) =
95                         (void (*)(void*,size_t))arc4random_buf;
96                     if (tptr != NULL) {
97                               arc4random_buf(buf, n);
98                               return;
99                     }
100           }
101 #endif
102           /* Make sure that we start out with b at a 4-byte alignment; plenty
103            * of CPUs care about this for 32-bit access. */
104           if (n >= 4 && ((ev_uintptr_t)b) & 3) {
105                     ev_uint32_t u = arc4random();
106                     int n_bytes = 4 - (((ev_uintptr_t)b) & 3);
107                     memcpy(b, &u, n_bytes);
108                     b += n_bytes;
109                     n -= n_bytes;
110           }
111           while (n >= 4) {
112                     *(ev_uint32_t*)b = arc4random();
113                     b += 4;
114                     n -= 4;
115           }
116           if (n) {
117                     ev_uint32_t u = arc4random();
118                     memcpy(b, &u, n);
119           }
120 #endif
121 }
122 
123 #else /* !EVENT__HAVE_ARC4RANDOM { */
124 
125 #ifdef EVENT__ssize_t
126 #define ssize_t EVENT__ssize_t
127 #endif
128 #define ARC4RANDOM_EXPORT static
129 #define ARC4_LOCK_() EVLOCK_LOCK(arc4rand_lock, 0)
130 #define ARC4_UNLOCK_() EVLOCK_UNLOCK(arc4rand_lock, 0)
131 #ifndef EVENT__DISABLE_THREAD_SUPPORT
132 static void *arc4rand_lock;
133 #endif
134 
135 #define ARC4RANDOM_UINT32 ev_uint32_t
136 #define ARC4RANDOM_NOSTIR
137 #define ARC4RANDOM_NORANDOM
138 #define ARC4RANDOM_NOUNIFORM
139 
140 #include "./arc4random.c"
141 
142 #ifndef EVENT__DISABLE_THREAD_SUPPORT
143 int
evutil_secure_rng_global_setup_locks_(const int enable_locks)144 evutil_secure_rng_global_setup_locks_(const int enable_locks)
145 {
146           EVTHREAD_SETUP_GLOBAL_LOCK(arc4rand_lock, 0);
147           return 0;
148 }
149 #endif
150 
151 static void
evutil_free_secure_rng_globals_locks(void)152 evutil_free_secure_rng_globals_locks(void)
153 {
154 #ifndef EVENT__DISABLE_THREAD_SUPPORT
155           if (arc4rand_lock != NULL) {
156                     EVTHREAD_FREE_LOCK(arc4rand_lock, 0);
157                     arc4rand_lock = NULL;
158           }
159 #endif
160           return;
161 }
162 
163 int
164 /*ARGSUSED*/
evutil_secure_rng_set_urandom_device_file(char * fname)165 evutil_secure_rng_set_urandom_device_file(char *fname)
166 {
167 #ifdef TRY_SEED_URANDOM
168           ARC4_LOCK_();
169           arc4random_urandom_filename = fname;
170           ARC4_UNLOCK_();
171 #endif
172           return 0;
173 }
174 
175 int
evutil_secure_rng_init(void)176 evutil_secure_rng_init(void)
177 {
178           int val;
179 
180           ARC4_LOCK_();
181           val = (!arc4_stir()) ? 0 : -1;
182           ARC4_UNLOCK_();
183           return val;
184 }
185 
186 static void
ev_arc4random_buf(void * buf,size_t n)187 ev_arc4random_buf(void *buf, size_t n)
188 {
189           arc4random_buf(buf, n);
190 }
191 
192 #endif /* } !EVENT__HAVE_ARC4RANDOM */
193 
194 void
evutil_secure_rng_get_bytes(void * buf,size_t n)195 evutil_secure_rng_get_bytes(void *buf, size_t n)
196 {
197           ev_arc4random_buf(buf, n);
198 }
199 
200 #if !defined(EVENT__HAVE_ARC4RANDOM) || defined(EVENT__HAVE_ARC4RANDOM_ADDRANDOM)
201 void
evutil_secure_rng_add_bytes(const char * buf,size_t n)202 evutil_secure_rng_add_bytes(const char *buf, size_t n)
203 {
204           arc4random_addrandom(__UNCONST(buf),
205               n>(size_t)INT_MAX ? INT_MAX : (int)n);
206 }
207 #endif
208 
209 void
evutil_free_secure_rng_globals_(void)210 evutil_free_secure_rng_globals_(void)
211 {
212     evutil_free_secure_rng_globals_locks();
213 }
214