libevent/evutil_rand.c
Nick Mathewson d4de062efc Add an arc4random implementation for use by evdns
Previously, evdns was at the mercy of the user for providing a good
entropy source; without one, it would be vulnerable to various
active attacks.

This patch adds a port of OpenBSD's arc4random() calls to Libevent
[port by Chris Davis], and wraps it up a little bit so we can use it
more safely.
2010-02-11 12:53:32 -05:00

93 lines
2.9 KiB
C

/*
* Copyright (c) 2007-2010 Niels Provos and Nick Mathewson
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/* This file has our secure PRNG code. On platforms that have arc4random(),
* we just use that. Otherwise, we include arc4random.c as a bunch of static
* functions, and wrap it lightly. We don't expose the arc4random*() APIs
* because A) they aren't in our namespace, and B) it's not nice to name your
* APIs after their implementations. We keep them in a separate file
* so that other people can rip it out and use it for whatever.
*/
#include "event-config.h"
#include "util-internal.h"
#include "evthread-internal.h"
#ifdef _EVENT_HAVE_ARC4RANDOM
#include <stdlib.h>
int
evutil_secure_rng_init(void)
{
return 0;
}
#else /* !_EVENT_HAVE_ARC4RANDOM { */
#ifdef _EVENT_ssize_t
#define ssize_t _EVENT_SSIZE_t
#endif
#define ARC4RANDOM_EXPORT static
#define _ARC4_LOCK() EVLOCK_LOCK(arc4rand_lock, 0)
#define _ARC4_UNLOCK() EVLOCK_UNLOCK(arc4rand_lock, 0)
static void *arc4rand_lock;
#define ARC4RANDOM_NOSTIR
#define ARC4RANDOM_NORANDOM
#define ARC4RANDOM_NOUNIFORM
#include "./arc4random.c"
int
evutil_secure_rng_init(void)
{
int val;
if (!arc4rand_lock) {
EVTHREAD_ALLOC_LOCK(arc4rand_lock, 0);
}
_ARC4_LOCK();
if (!arc4_seeded_ok)
arc4_stir();
val = arc4_seeded_ok ? 0 : -1;
_ARC4_UNLOCK();
return val;
}
#endif /* } !_EVENT_HAVE_ARC4RANDOM */
void
evutil_secure_rng_get_bytes(void *buf, size_t n)
{
arc4random_buf(buf, n);
}
void
evutil_secure_rng_add_bytes(const char *buf, size_t n)
{
arc4random_addrandom((const unsigned char*)buf, n);
}