12b15cb3dSCy Schubert /*
22b15cb3dSCy Schubert * Copyright (c) 2007-2012 Niels Provos and Nick Mathewson
32b15cb3dSCy Schubert *
42b15cb3dSCy Schubert * Redistribution and use in source and binary forms, with or without
52b15cb3dSCy Schubert * modification, are permitted provided that the following conditions
62b15cb3dSCy Schubert * are met:
72b15cb3dSCy Schubert * 1. Redistributions of source code must retain the above copyright
82b15cb3dSCy Schubert * notice, this list of conditions and the following disclaimer.
92b15cb3dSCy Schubert * 2. Redistributions in binary form must reproduce the above copyright
102b15cb3dSCy Schubert * notice, this list of conditions and the following disclaimer in the
112b15cb3dSCy Schubert * documentation and/or other materials provided with the distribution.
122b15cb3dSCy Schubert * 3. The name of the author may not be used to endorse or promote products
132b15cb3dSCy Schubert * derived from this software without specific prior written permission.
142b15cb3dSCy Schubert *
152b15cb3dSCy Schubert * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
162b15cb3dSCy Schubert * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
172b15cb3dSCy Schubert * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
182b15cb3dSCy Schubert * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
192b15cb3dSCy Schubert * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
202b15cb3dSCy Schubert * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
212b15cb3dSCy Schubert * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
222b15cb3dSCy Schubert * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
232b15cb3dSCy Schubert * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
242b15cb3dSCy Schubert * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
252b15cb3dSCy Schubert */
262b15cb3dSCy Schubert
272b15cb3dSCy Schubert /* This file has our secure PRNG code. On platforms that have arc4random(),
282b15cb3dSCy Schubert * we just use that. Otherwise, we include arc4random.c as a bunch of static
292b15cb3dSCy Schubert * functions, and wrap it lightly. We don't expose the arc4random*() APIs
302b15cb3dSCy Schubert * because A) they aren't in our namespace, and B) it's not nice to name your
312b15cb3dSCy Schubert * APIs after their implementations. We keep them in a separate file
322b15cb3dSCy Schubert * so that other people can rip it out and use it for whatever.
332b15cb3dSCy Schubert */
342b15cb3dSCy Schubert
352b15cb3dSCy Schubert #include "event2/event-config.h"
362b15cb3dSCy Schubert #include "evconfig-private.h"
372b15cb3dSCy Schubert
382b15cb3dSCy Schubert #include <limits.h>
392b15cb3dSCy Schubert
402b15cb3dSCy Schubert #include "util-internal.h"
412b15cb3dSCy Schubert #include "evthread-internal.h"
422b15cb3dSCy Schubert
432b15cb3dSCy Schubert #ifdef EVENT__HAVE_ARC4RANDOM
442b15cb3dSCy Schubert #include <stdlib.h>
452b15cb3dSCy Schubert #include <string.h>
462b15cb3dSCy Schubert int
evutil_secure_rng_set_urandom_device_file(char * fname)472b15cb3dSCy Schubert evutil_secure_rng_set_urandom_device_file(char *fname)
482b15cb3dSCy Schubert {
492b15cb3dSCy Schubert (void) fname;
502b15cb3dSCy Schubert return -1;
512b15cb3dSCy Schubert }
522b15cb3dSCy Schubert int
evutil_secure_rng_init(void)532b15cb3dSCy Schubert evutil_secure_rng_init(void)
542b15cb3dSCy Schubert {
552b15cb3dSCy Schubert /* call arc4random() now to force it to self-initialize */
562b15cb3dSCy Schubert (void) arc4random();
572b15cb3dSCy Schubert return 0;
582b15cb3dSCy Schubert }
592b15cb3dSCy Schubert #ifndef EVENT__DISABLE_THREAD_SUPPORT
602b15cb3dSCy Schubert int
evutil_secure_rng_global_setup_locks_(const int enable_locks)612b15cb3dSCy Schubert evutil_secure_rng_global_setup_locks_(const int enable_locks)
622b15cb3dSCy Schubert {
632b15cb3dSCy Schubert return 0;
642b15cb3dSCy Schubert }
652b15cb3dSCy Schubert #endif
662b15cb3dSCy Schubert static void
evutil_free_secure_rng_globals_locks(void)672b15cb3dSCy Schubert evutil_free_secure_rng_globals_locks(void)
682b15cb3dSCy Schubert {
692b15cb3dSCy Schubert }
702b15cb3dSCy Schubert
712b15cb3dSCy Schubert static void
ev_arc4random_buf(void * buf,size_t n)722b15cb3dSCy Schubert ev_arc4random_buf(void *buf, size_t n)
732b15cb3dSCy Schubert {
742b15cb3dSCy Schubert #if defined(EVENT__HAVE_ARC4RANDOM_BUF) && !defined(__APPLE__)
752b15cb3dSCy Schubert arc4random_buf(buf, n);
762b15cb3dSCy Schubert return;
772b15cb3dSCy Schubert #else
782b15cb3dSCy Schubert unsigned char *b = buf;
792b15cb3dSCy Schubert
802b15cb3dSCy Schubert #if defined(EVENT__HAVE_ARC4RANDOM_BUF)
812b15cb3dSCy Schubert /* OSX 10.7 introducd arc4random_buf, so if you build your program
822b15cb3dSCy Schubert * there, you'll get surprised when older versions of OSX fail to run.
832b15cb3dSCy Schubert * To solve this, we can check whether the function pointer is set,
842b15cb3dSCy Schubert * and fall back otherwise. (OSX does this using some linker
852b15cb3dSCy Schubert * trickery.)
862b15cb3dSCy Schubert */
872b15cb3dSCy Schubert {
882b15cb3dSCy Schubert void (*tptr)(void *,size_t) =
892b15cb3dSCy Schubert (void (*)(void*,size_t))arc4random_buf;
902b15cb3dSCy Schubert if (tptr != NULL) {
912b15cb3dSCy Schubert arc4random_buf(buf, n);
922b15cb3dSCy Schubert return;
932b15cb3dSCy Schubert }
942b15cb3dSCy Schubert }
952b15cb3dSCy Schubert #endif
962b15cb3dSCy Schubert /* Make sure that we start out with b at a 4-byte alignment; plenty
972b15cb3dSCy Schubert * of CPUs care about this for 32-bit access. */
982b15cb3dSCy Schubert if (n >= 4 && ((ev_uintptr_t)b) & 3) {
992b15cb3dSCy Schubert ev_uint32_t u = arc4random();
1002b15cb3dSCy Schubert int n_bytes = 4 - (((ev_uintptr_t)b) & 3);
1012b15cb3dSCy Schubert memcpy(b, &u, n_bytes);
1022b15cb3dSCy Schubert b += n_bytes;
1032b15cb3dSCy Schubert n -= n_bytes;
1042b15cb3dSCy Schubert }
1052b15cb3dSCy Schubert while (n >= 4) {
1062b15cb3dSCy Schubert *(ev_uint32_t*)b = arc4random();
1072b15cb3dSCy Schubert b += 4;
1082b15cb3dSCy Schubert n -= 4;
1092b15cb3dSCy Schubert }
1102b15cb3dSCy Schubert if (n) {
1112b15cb3dSCy Schubert ev_uint32_t u = arc4random();
1122b15cb3dSCy Schubert memcpy(b, &u, n);
1132b15cb3dSCy Schubert }
1142b15cb3dSCy Schubert #endif
1152b15cb3dSCy Schubert }
1162b15cb3dSCy Schubert
1172b15cb3dSCy Schubert #else /* !EVENT__HAVE_ARC4RANDOM { */
1182b15cb3dSCy Schubert
1192b15cb3dSCy Schubert #ifdef EVENT__ssize_t
1202b15cb3dSCy Schubert #define ssize_t EVENT__ssize_t
1212b15cb3dSCy Schubert #endif
1222b15cb3dSCy Schubert #define ARC4RANDOM_EXPORT static
1232b15cb3dSCy Schubert #define ARC4_LOCK_() EVLOCK_LOCK(arc4rand_lock, 0)
1242b15cb3dSCy Schubert #define ARC4_UNLOCK_() EVLOCK_UNLOCK(arc4rand_lock, 0)
1252b15cb3dSCy Schubert #ifndef EVENT__DISABLE_THREAD_SUPPORT
1262b15cb3dSCy Schubert static void *arc4rand_lock;
1272b15cb3dSCy Schubert #endif
1282b15cb3dSCy Schubert
1292b15cb3dSCy Schubert #define ARC4RANDOM_UINT32 ev_uint32_t
1302b15cb3dSCy Schubert #define ARC4RANDOM_NOSTIR
1312b15cb3dSCy Schubert #define ARC4RANDOM_NORANDOM
1322b15cb3dSCy Schubert #define ARC4RANDOM_NOUNIFORM
1332b15cb3dSCy Schubert
1342b15cb3dSCy Schubert #include "./arc4random.c"
1352b15cb3dSCy Schubert
1362b15cb3dSCy Schubert #ifndef EVENT__DISABLE_THREAD_SUPPORT
1372b15cb3dSCy Schubert int
evutil_secure_rng_global_setup_locks_(const int enable_locks)1382b15cb3dSCy Schubert evutil_secure_rng_global_setup_locks_(const int enable_locks)
1392b15cb3dSCy Schubert {
1402b15cb3dSCy Schubert EVTHREAD_SETUP_GLOBAL_LOCK(arc4rand_lock, 0);
1412b15cb3dSCy Schubert return 0;
1422b15cb3dSCy Schubert }
1432b15cb3dSCy Schubert #endif
1442b15cb3dSCy Schubert
1452b15cb3dSCy Schubert static void
evutil_free_secure_rng_globals_locks(void)1462b15cb3dSCy Schubert evutil_free_secure_rng_globals_locks(void)
1472b15cb3dSCy Schubert {
1482b15cb3dSCy Schubert #ifndef EVENT__DISABLE_THREAD_SUPPORT
1492b15cb3dSCy Schubert if (arc4rand_lock != NULL) {
1502b15cb3dSCy Schubert EVTHREAD_FREE_LOCK(arc4rand_lock, 0);
1512b15cb3dSCy Schubert arc4rand_lock = NULL;
1522b15cb3dSCy Schubert }
1532b15cb3dSCy Schubert #endif
1542b15cb3dSCy Schubert return;
1552b15cb3dSCy Schubert }
1562b15cb3dSCy Schubert
1572b15cb3dSCy Schubert int
evutil_secure_rng_set_urandom_device_file(char * fname)1582b15cb3dSCy Schubert evutil_secure_rng_set_urandom_device_file(char *fname)
1592b15cb3dSCy Schubert {
1602b15cb3dSCy Schubert #ifdef TRY_SEED_URANDOM
1612b15cb3dSCy Schubert ARC4_LOCK_();
1622b15cb3dSCy Schubert arc4random_urandom_filename = fname;
1632b15cb3dSCy Schubert ARC4_UNLOCK_();
1642b15cb3dSCy Schubert #endif
1652b15cb3dSCy Schubert return 0;
1662b15cb3dSCy Schubert }
1672b15cb3dSCy Schubert
1682b15cb3dSCy Schubert int
evutil_secure_rng_init(void)1692b15cb3dSCy Schubert evutil_secure_rng_init(void)
1702b15cb3dSCy Schubert {
1712b15cb3dSCy Schubert int val;
1722b15cb3dSCy Schubert
1732b15cb3dSCy Schubert ARC4_LOCK_();
174*a466cc55SCy Schubert val = (!arc4_stir()) ? 0 : -1;
1752b15cb3dSCy Schubert ARC4_UNLOCK_();
1762b15cb3dSCy Schubert return val;
1772b15cb3dSCy Schubert }
1782b15cb3dSCy Schubert
1792b15cb3dSCy Schubert static void
ev_arc4random_buf(void * buf,size_t n)1802b15cb3dSCy Schubert ev_arc4random_buf(void *buf, size_t n)
1812b15cb3dSCy Schubert {
1822b15cb3dSCy Schubert arc4random_buf(buf, n);
1832b15cb3dSCy Schubert }
1842b15cb3dSCy Schubert
1852b15cb3dSCy Schubert #endif /* } !EVENT__HAVE_ARC4RANDOM */
1862b15cb3dSCy Schubert
1872b15cb3dSCy Schubert void
evutil_secure_rng_get_bytes(void * buf,size_t n)1882b15cb3dSCy Schubert evutil_secure_rng_get_bytes(void *buf, size_t n)
1892b15cb3dSCy Schubert {
1902b15cb3dSCy Schubert ev_arc4random_buf(buf, n);
1912b15cb3dSCy Schubert }
1922b15cb3dSCy Schubert
193*a466cc55SCy Schubert #if !defined(EVENT__HAVE_ARC4RANDOM) || defined(EVENT__HAVE_ARC4RANDOM_ADDRANDOM)
1942b15cb3dSCy Schubert void
evutil_secure_rng_add_bytes(const char * buf,size_t n)1952b15cb3dSCy Schubert evutil_secure_rng_add_bytes(const char *buf, size_t n)
1962b15cb3dSCy Schubert {
1972b15cb3dSCy Schubert }
198*a466cc55SCy Schubert #endif
1992b15cb3dSCy Schubert
2002b15cb3dSCy Schubert void
evutil_free_secure_rng_globals_(void)2012b15cb3dSCy Schubert evutil_free_secure_rng_globals_(void)
2022b15cb3dSCy Schubert {
2032b15cb3dSCy Schubert evutil_free_secure_rng_globals_locks();
2042b15cb3dSCy Schubert }
205