xref: /netbsd-src/external/bsd/ntp/dist/sntp/libevent/evutil_rand.c (revision 6a493d6bc668897c91594964a732d38505b70cbb)
1 /*	$NetBSD: evutil_rand.c,v 1.1.1.1 2013/12/27 23:31:19 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
49 evutil_secure_rng_init(void)
50 {
51 	/* call arc4random() now to force it to self-initialize */
52 	(void) arc4random();
53 	return 0;
54 }
55 #ifndef EVENT__DISABLE_THREAD_SUPPORT
56 int
57 evutil_secure_rng_global_setup_locks_(const int enable_locks)
58 {
59 	return 0;
60 }
61 #endif
62 static void
63 evutil_free_secure_rng_globals_locks(void)
64 {
65 }
66 
67 static void
68 ev_arc4random_buf(void *buf, size_t n)
69 {
70 #if defined(EVENT__HAVE_ARC4RANDOM_BUF) && !defined(__APPLE__)
71 	return arc4random_buf(buf, n);
72 #else
73 	unsigned char *b = buf;
74 
75 #if defined(EVENT__HAVE_ARC4RANDOM_BUF)
76 	/* OSX 10.7 introducd arc4random_buf, so if you build your program
77 	 * there, you'll get surprised when older versions of OSX fail to run.
78 	 * To solve this, we can check whether the function pointer is set,
79 	 * and fall back otherwise.  (OSX does this using some linker
80 	 * trickery.)
81 	 */
82 	if (arc4random_buf != NULL) {
83 		return arc4random_buf(buf, n);
84 	}
85 #endif
86 	/* Make sure that we start out with b at a 4-byte alignment; plenty
87 	 * of CPUs care about this for 32-bit access. */
88 	if (n >= 4 && ((ev_uintptr_t)b) & 3) {
89 		ev_uint32_t u = arc4random();
90 		int n_bytes = 4 - (((ev_uintptr_t)b) & 3);
91 		memcpy(b, &u, n_bytes);
92 		b += n_bytes;
93 		n -= n_bytes;
94 	}
95 	while (n >= 4) {
96 		*(ev_uint32_t*)b = arc4random();
97 		b += 4;
98 		n -= 4;
99 	}
100 	if (n) {
101 		ev_uint32_t u = arc4random();
102 		memcpy(b, &u, n);
103 	}
104 #endif
105 }
106 
107 #else /* !EVENT__HAVE_ARC4RANDOM { */
108 
109 #ifdef EVENT__ssize_t
110 #define ssize_t EVENT__ssize_t
111 #endif
112 #define ARC4RANDOM_EXPORT static
113 #define ARC4_LOCK_() EVLOCK_LOCK(arc4rand_lock, 0)
114 #define ARC4_UNLOCK_() EVLOCK_UNLOCK(arc4rand_lock, 0)
115 #ifndef EVENT__DISABLE_THREAD_SUPPORT
116 static void *arc4rand_lock;
117 #endif
118 
119 #define ARC4RANDOM_UINT32 ev_uint32_t
120 #define ARC4RANDOM_NOSTIR
121 #define ARC4RANDOM_NORANDOM
122 #define ARC4RANDOM_NOUNIFORM
123 
124 #include "./arc4random.c"
125 
126 #ifndef EVENT__DISABLE_THREAD_SUPPORT
127 int
128 evutil_secure_rng_global_setup_locks_(const int enable_locks)
129 {
130 	EVTHREAD_SETUP_GLOBAL_LOCK(arc4rand_lock, 0);
131 	return 0;
132 }
133 #endif
134 
135 static void
136 evutil_free_secure_rng_globals_locks(void)
137 {
138 #ifndef EVENT__DISABLE_THREAD_SUPPORT
139 	if (arc4rand_lock != NULL) {
140 		EVTHREAD_FREE_LOCK(arc4rand_lock, 0);
141 		arc4rand_lock = NULL;
142 	}
143 #endif
144 	return;
145 }
146 
147 int
148 evutil_secure_rng_init(void)
149 {
150 	int val;
151 
152 	ARC4_LOCK_();
153 	if (!arc4_seeded_ok)
154 		arc4_stir();
155 	val = arc4_seeded_ok ? 0 : -1;
156 	ARC4_UNLOCK_();
157 	return val;
158 }
159 
160 static void
161 ev_arc4random_buf(void *buf, size_t n)
162 {
163 	arc4random_buf(buf, n);
164 }
165 
166 #endif /* } !EVENT__HAVE_ARC4RANDOM */
167 
168 void
169 evutil_secure_rng_get_bytes(void *buf, size_t n)
170 {
171 	ev_arc4random_buf(buf, n);
172 }
173 
174 void
175 evutil_secure_rng_add_bytes(const char *buf, size_t n)
176 {
177 	arc4random_addrandom((unsigned char*)buf,
178 	    n>(size_t)INT_MAX ? INT_MAX : (int)n);
179 }
180 
181 void
182 evutil_free_secure_rng_globals_(void)
183 {
184     evutil_free_secure_rng_globals_locks();
185 }
186