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