xref: /libevent-2.1.12/evutil_rand.c (revision 6602a97d)
1d4de062eSNick Mathewson /*
2e49e2891SNick Mathewson  * Copyright (c) 2007-2012 Niels Provos and Nick Mathewson
3d4de062eSNick Mathewson  *
4d4de062eSNick Mathewson  * Redistribution and use in source and binary forms, with or without
5d4de062eSNick Mathewson  * modification, are permitted provided that the following conditions
6d4de062eSNick Mathewson  * are met:
7d4de062eSNick Mathewson  * 1. Redistributions of source code must retain the above copyright
8d4de062eSNick Mathewson  *    notice, this list of conditions and the following disclaimer.
9d4de062eSNick Mathewson  * 2. Redistributions in binary form must reproduce the above copyright
10d4de062eSNick Mathewson  *    notice, this list of conditions and the following disclaimer in the
11d4de062eSNick Mathewson  *    documentation and/or other materials provided with the distribution.
12d4de062eSNick Mathewson  * 3. The name of the author may not be used to endorse or promote products
13d4de062eSNick Mathewson  *    derived from this software without specific prior written permission.
14d4de062eSNick Mathewson  *
15d4de062eSNick Mathewson  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16d4de062eSNick Mathewson  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17d4de062eSNick Mathewson  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18d4de062eSNick Mathewson  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19d4de062eSNick Mathewson  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20d4de062eSNick Mathewson  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21d4de062eSNick Mathewson  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22d4de062eSNick Mathewson  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23d4de062eSNick Mathewson  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24d4de062eSNick Mathewson  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25d4de062eSNick Mathewson  */
26d4de062eSNick Mathewson 
27d4de062eSNick Mathewson /* This file has our secure PRNG code.  On platforms that have arc4random(),
28d4de062eSNick Mathewson  * we just use that.  Otherwise, we include arc4random.c as a bunch of static
29d4de062eSNick Mathewson  * functions, and wrap it lightly.  We don't expose the arc4random*() APIs
30d4de062eSNick Mathewson  * because A) they aren't in our namespace, and B) it's not nice to name your
31d4de062eSNick Mathewson  * APIs after their implementations.  We keep them in a separate file
32d4de062eSNick Mathewson  * so that other people can rip it out and use it for whatever.
33d4de062eSNick Mathewson  */
34d4de062eSNick Mathewson 
35ec347b92SNick Mathewson #include "event2/event-config.h"
360915ca0aSKevin Bowling #include "evconfig-private.h"
37d4de062eSNick Mathewson 
38598d1336SNick Mathewson #include <limits.h>
39598d1336SNick Mathewson 
40d4de062eSNick Mathewson #include "util-internal.h"
41d4de062eSNick Mathewson #include "evthread-internal.h"
42d4de062eSNick Mathewson 
4368120d9bSNick Mathewson #ifdef EVENT__HAVE_ARC4RANDOM
44d4de062eSNick Mathewson #include <stdlib.h>
454ec8fea6SNick Mathewson #include <string.h>
46d4de062eSNick Mathewson int
evutil_secure_rng_set_urandom_device_file(char * fname)472bbb5d76SNick Mathewson evutil_secure_rng_set_urandom_device_file(char *fname)
482bbb5d76SNick Mathewson {
492bbb5d76SNick Mathewson 	(void) fname;
502bbb5d76SNick Mathewson 	return -1;
512bbb5d76SNick Mathewson }
522bbb5d76SNick Mathewson int
evutil_secure_rng_init(void)53d4de062eSNick Mathewson evutil_secure_rng_init(void)
54d4de062eSNick Mathewson {
55f9807167SNick Mathewson 	/* call arc4random() now to force it to self-initialize */
56f9807167SNick Mathewson 	(void) arc4random();
57d4de062eSNick Mathewson 	return 0;
58d4de062eSNick Mathewson }
59041ca00cSMark Ellzey #ifndef EVENT__DISABLE_THREAD_SUPPORT
60b683cae3SNick Mathewson int
evutil_secure_rng_global_setup_locks_(const int enable_locks)61b683cae3SNick Mathewson evutil_secure_rng_global_setup_locks_(const int enable_locks)
62b683cae3SNick Mathewson {
63b683cae3SNick Mathewson 	return 0;
64b683cae3SNick Mathewson }
65041ca00cSMark Ellzey #endif
6617289683SNick Mathewson static void
evutil_free_secure_rng_globals_locks(void)6717289683SNick Mathewson evutil_free_secure_rng_globals_locks(void)
6817289683SNick Mathewson {
6917289683SNick Mathewson }
704ec8fea6SNick Mathewson 
714ec8fea6SNick Mathewson static void
ev_arc4random_buf(void * buf,size_t n)72b4423029SMitchell Livingston ev_arc4random_buf(void *buf, size_t n)
734ec8fea6SNick Mathewson {
74f1284e73SNick Mathewson #if defined(EVENT__HAVE_ARC4RANDOM_BUF) && !defined(__APPLE__)
75838161dcSMakoto Kato 	arc4random_buf(buf, n);
761ea1f26bSMakoto Kato 	return;
77b4423029SMitchell Livingston #else
78c0720c1bSNick Mathewson 	unsigned char *b = buf;
79bff5f940SGreg Hazel 
80f1284e73SNick Mathewson #if defined(EVENT__HAVE_ARC4RANDOM_BUF)
81bff5f940SGreg Hazel 	/* OSX 10.7 introducd arc4random_buf, so if you build your program
82bff5f940SGreg Hazel 	 * there, you'll get surprised when older versions of OSX fail to run.
83bff5f940SGreg Hazel 	 * To solve this, we can check whether the function pointer is set,
84bff5f940SGreg Hazel 	 * and fall back otherwise.  (OSX does this using some linker
85bff5f940SGreg Hazel 	 * trickery.)
86bff5f940SGreg Hazel 	 */
875cb3865aSNick Mathewson 	{
88e64a2b0bSNick Mathewson 		void (*tptr)(void *,size_t) =
89e64a2b0bSNick Mathewson 		    (void (*)(void*,size_t))arc4random_buf;
905cb3865aSNick Mathewson 		if (tptr != NULL) {
91838161dcSMakoto Kato 			arc4random_buf(buf, n);
921ea1f26bSMakoto Kato 			return;
93bff5f940SGreg Hazel 		}
945cb3865aSNick Mathewson 	}
95bff5f940SGreg Hazel #endif
964ec8fea6SNick Mathewson 	/* Make sure that we start out with b at a 4-byte alignment; plenty
974ec8fea6SNick Mathewson 	 * of CPUs care about this for 32-bit access. */
984ec8fea6SNick Mathewson 	if (n >= 4 && ((ev_uintptr_t)b) & 3) {
994ec8fea6SNick Mathewson 		ev_uint32_t u = arc4random();
1004ec8fea6SNick Mathewson 		int n_bytes = 4 - (((ev_uintptr_t)b) & 3);
1014ec8fea6SNick Mathewson 		memcpy(b, &u, n_bytes);
1024ec8fea6SNick Mathewson 		b += n_bytes;
1034ec8fea6SNick Mathewson 		n -= n_bytes;
1044ec8fea6SNick Mathewson 	}
1054ec8fea6SNick Mathewson 	while (n >= 4) {
1064ec8fea6SNick Mathewson 		*(ev_uint32_t*)b = arc4random();
1074ec8fea6SNick Mathewson 		b += 4;
1084ec8fea6SNick Mathewson 		n -= 4;
1094ec8fea6SNick Mathewson 	}
1104ec8fea6SNick Mathewson 	if (n) {
1114ec8fea6SNick Mathewson 		ev_uint32_t u = arc4random();
1124ec8fea6SNick Mathewson 		memcpy(b, &u, n);
1134ec8fea6SNick Mathewson 	}
1144ec8fea6SNick Mathewson #endif
115b4423029SMitchell Livingston }
1164ec8fea6SNick Mathewson 
11768120d9bSNick Mathewson #else /* !EVENT__HAVE_ARC4RANDOM { */
118d4de062eSNick Mathewson 
11968120d9bSNick Mathewson #ifdef EVENT__ssize_t
120946b5841SNick Mathewson #define ssize_t EVENT__ssize_t
121d4de062eSNick Mathewson #endif
122d4de062eSNick Mathewson #define ARC4RANDOM_EXPORT static
123cb9da0bfSNick Mathewson #define ARC4_LOCK_() EVLOCK_LOCK(arc4rand_lock, 0)
124cb9da0bfSNick Mathewson #define ARC4_UNLOCK_() EVLOCK_UNLOCK(arc4rand_lock, 0)
12568120d9bSNick Mathewson #ifndef EVENT__DISABLE_THREAD_SUPPORT
126d4de062eSNick Mathewson static void *arc4rand_lock;
12700a7a0e4SNick Mathewson #endif
128d4de062eSNick Mathewson 
1294ec8fea6SNick Mathewson #define ARC4RANDOM_UINT32 ev_uint32_t
130d4de062eSNick Mathewson #define ARC4RANDOM_NOSTIR
131d4de062eSNick Mathewson #define ARC4RANDOM_NORANDOM
132d4de062eSNick Mathewson #define ARC4RANDOM_NOUNIFORM
133d4de062eSNick Mathewson 
134d4de062eSNick Mathewson #include "./arc4random.c"
135d4de062eSNick Mathewson 
13668120d9bSNick Mathewson #ifndef EVENT__DISABLE_THREAD_SUPPORT
137b683cae3SNick Mathewson int
evutil_secure_rng_global_setup_locks_(const int enable_locks)138b683cae3SNick Mathewson evutil_secure_rng_global_setup_locks_(const int enable_locks)
139b683cae3SNick Mathewson {
140b683cae3SNick Mathewson 	EVTHREAD_SETUP_GLOBAL_LOCK(arc4rand_lock, 0);
141b683cae3SNick Mathewson 	return 0;
142b683cae3SNick Mathewson }
143b683cae3SNick Mathewson #endif
144b683cae3SNick Mathewson 
145041ca00cSMark Ellzey static void
evutil_free_secure_rng_globals_locks(void)146041ca00cSMark Ellzey evutil_free_secure_rng_globals_locks(void)
147041ca00cSMark Ellzey {
148041ca00cSMark Ellzey #ifndef EVENT__DISABLE_THREAD_SUPPORT
149041ca00cSMark Ellzey 	if (arc4rand_lock != NULL) {
150041ca00cSMark Ellzey 		EVTHREAD_FREE_LOCK(arc4rand_lock, 0);
15155e991b2SNick Mathewson 		arc4rand_lock = NULL;
152041ca00cSMark Ellzey 	}
153041ca00cSMark Ellzey #endif
154041ca00cSMark Ellzey 	return;
155041ca00cSMark Ellzey }
156f98c1588SNick Mathewson 
157d4de062eSNick Mathewson int
evutil_secure_rng_set_urandom_device_file(char * fname)1582bbb5d76SNick Mathewson evutil_secure_rng_set_urandom_device_file(char *fname)
1592bbb5d76SNick Mathewson {
1602bbb5d76SNick Mathewson #ifdef TRY_SEED_URANDOM
161197abd8bSNick Mathewson 	ARC4_LOCK_();
1622bbb5d76SNick Mathewson 	arc4random_urandom_filename = fname;
163197abd8bSNick Mathewson 	ARC4_UNLOCK_();
1642bbb5d76SNick Mathewson #endif
1652bbb5d76SNick Mathewson 	return 0;
1662bbb5d76SNick Mathewson }
1672bbb5d76SNick Mathewson 
1682bbb5d76SNick Mathewson int
evutil_secure_rng_init(void)169d4de062eSNick Mathewson evutil_secure_rng_init(void)
170d4de062eSNick Mathewson {
171d4de062eSNick Mathewson 	int val;
172d4de062eSNick Mathewson 
173cb9da0bfSNick Mathewson 	ARC4_LOCK_();
174*6602a97dSSeong-Joong Kim 	val = (!arc4_stir()) ? 0 : -1;
175cb9da0bfSNick Mathewson 	ARC4_UNLOCK_();
176d4de062eSNick Mathewson 	return val;
177d4de062eSNick Mathewson }
178d4de062eSNick Mathewson 
179b4423029SMitchell Livingston static void
ev_arc4random_buf(void * buf,size_t n)180b4423029SMitchell Livingston ev_arc4random_buf(void *buf, size_t n)
181b4423029SMitchell Livingston {
182b4423029SMitchell Livingston 	arc4random_buf(buf, n);
183b4423029SMitchell Livingston }
184b4423029SMitchell Livingston 
18568120d9bSNick Mathewson #endif /* } !EVENT__HAVE_ARC4RANDOM */
186d4de062eSNick Mathewson 
187d4de062eSNick Mathewson void
evutil_secure_rng_get_bytes(void * buf,size_t n)188d4de062eSNick Mathewson evutil_secure_rng_get_bytes(void *buf, size_t n)
189d4de062eSNick Mathewson {
190b4423029SMitchell Livingston 	ev_arc4random_buf(buf, n);
191d4de062eSNick Mathewson }
192d4de062eSNick Mathewson 
193a0bfe2c4SAzat Khuzhin #if !defined(EVENT__HAVE_ARC4RANDOM) || defined(EVENT__HAVE_ARC4RANDOM_ADDRANDOM)
194d4de062eSNick Mathewson void
evutil_secure_rng_add_bytes(const char * buf,size_t n)195d4de062eSNick Mathewson evutil_secure_rng_add_bytes(const char *buf, size_t n)
196d4de062eSNick Mathewson {
197598d1336SNick Mathewson 	arc4random_addrandom((unsigned char*)buf,
1987484df61SNick Mathewson 	    n>(size_t)INT_MAX ? INT_MAX : (int)n);
199d4de062eSNick Mathewson }
200a0bfe2c4SAzat Khuzhin #endif
201d4de062eSNick Mathewson 
20217289683SNick Mathewson void
evutil_free_secure_rng_globals_(void)20317289683SNick Mathewson evutil_free_secure_rng_globals_(void)
20417289683SNick Mathewson {
20517289683SNick Mathewson     evutil_free_secure_rng_globals_locks();
20617289683SNick Mathewson }
207