ipc/chromium/src/third_party/libevent/evutil_rand.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/ipc/chromium/src/third_party/libevent/evutil_rand.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,149 @@
     1.4 +/*
     1.5 + * Copyright (c) 2007-2012 Niels Provos and Nick Mathewson
     1.6 + *
     1.7 + * Redistribution and use in source and binary forms, with or without
     1.8 + * modification, are permitted provided that the following conditions
     1.9 + * are met:
    1.10 + * 1. Redistributions of source code must retain the above copyright
    1.11 + *    notice, this list of conditions and the following disclaimer.
    1.12 + * 2. Redistributions in binary form must reproduce the above copyright
    1.13 + *    notice, this list of conditions and the following disclaimer in the
    1.14 + *    documentation and/or other materials provided with the distribution.
    1.15 + * 3. The name of the author may not be used to endorse or promote products
    1.16 + *    derived from this software without specific prior written permission.
    1.17 + *
    1.18 + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
    1.19 + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
    1.20 + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
    1.21 + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
    1.22 + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
    1.23 + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
    1.24 + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
    1.25 + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    1.26 + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
    1.27 + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    1.28 + */
    1.29 +
    1.30 +/* This file has our secure PRNG code.  On platforms that have arc4random(),
    1.31 + * we just use that.  Otherwise, we include arc4random.c as a bunch of static
    1.32 + * functions, and wrap it lightly.  We don't expose the arc4random*() APIs
    1.33 + * because A) they aren't in our namespace, and B) it's not nice to name your
    1.34 + * APIs after their implementations.  We keep them in a separate file
    1.35 + * so that other people can rip it out and use it for whatever.
    1.36 + */
    1.37 +
    1.38 +#include "event2/event-config.h"
    1.39 +
    1.40 +#include <limits.h>
    1.41 +
    1.42 +#include "util-internal.h"
    1.43 +#include "evthread-internal.h"
    1.44 +
    1.45 +#ifdef _EVENT_HAVE_ARC4RANDOM
    1.46 +#include <stdlib.h>
    1.47 +#include <string.h>
    1.48 +int
    1.49 +evutil_secure_rng_init(void)
    1.50 +{
    1.51 +	/* call arc4random() now to force it to self-initialize */
    1.52 +	(void) arc4random();
    1.53 +	return 0;
    1.54 +}
    1.55 +int
    1.56 +evutil_secure_rng_global_setup_locks_(const int enable_locks)
    1.57 +{
    1.58 +	return 0;
    1.59 +}
    1.60 +
    1.61 +static void
    1.62 +ev_arc4random_buf(void *buf, size_t n)
    1.63 +{
    1.64 +#if defined(_EVENT_HAVE_ARC4RANDOM_BUF) && !defined(__APPLE__)
    1.65 +	return arc4random_buf(buf, n);
    1.66 +#else
    1.67 +	unsigned char *b = buf;
    1.68 +
    1.69 +	/* Make sure that we start out with b at a 4-byte alignment; plenty
    1.70 +	 * of CPUs care about this for 32-bit access. */
    1.71 +	if (n >= 4 && ((ev_uintptr_t)b) & 3) {
    1.72 +		ev_uint32_t u = arc4random();
    1.73 +		int n_bytes = 4 - (((ev_uintptr_t)b) & 3);
    1.74 +		memcpy(b, &u, n_bytes);
    1.75 +		b += n_bytes;
    1.76 +		n -= n_bytes;
    1.77 +	}
    1.78 +	while (n >= 4) {
    1.79 +		*(ev_uint32_t*)b = arc4random();
    1.80 +		b += 4;
    1.81 +		n -= 4;
    1.82 +	}
    1.83 +	if (n) {
    1.84 +		ev_uint32_t u = arc4random();
    1.85 +		memcpy(b, &u, n);
    1.86 +	}
    1.87 +#endif
    1.88 +}
    1.89 +
    1.90 +#else /* !_EVENT_HAVE_ARC4RANDOM { */
    1.91 +
    1.92 +#ifdef _EVENT_ssize_t
    1.93 +#define ssize_t _EVENT_SSIZE_t
    1.94 +#endif
    1.95 +#define ARC4RANDOM_EXPORT static
    1.96 +#define _ARC4_LOCK() EVLOCK_LOCK(arc4rand_lock, 0)
    1.97 +#define _ARC4_UNLOCK() EVLOCK_UNLOCK(arc4rand_lock, 0)
    1.98 +#ifndef _EVENT_DISABLE_THREAD_SUPPORT
    1.99 +static void *arc4rand_lock;
   1.100 +#endif
   1.101 +
   1.102 +#define ARC4RANDOM_UINT32 ev_uint32_t
   1.103 +#define ARC4RANDOM_NOSTIR
   1.104 +#define ARC4RANDOM_NORANDOM
   1.105 +#define ARC4RANDOM_NOUNIFORM
   1.106 +
   1.107 +#include "./arc4random.c"
   1.108 +
   1.109 +#ifndef _EVENT_DISABLE_THREAD_SUPPORT
   1.110 +int
   1.111 +evutil_secure_rng_global_setup_locks_(const int enable_locks)
   1.112 +{
   1.113 +	EVTHREAD_SETUP_GLOBAL_LOCK(arc4rand_lock, 0);
   1.114 +	return 0;
   1.115 +}
   1.116 +#endif
   1.117 +
   1.118 +int
   1.119 +evutil_secure_rng_init(void)
   1.120 +{
   1.121 +	int val;
   1.122 +
   1.123 +	_ARC4_LOCK();
   1.124 +	if (!arc4_seeded_ok)
   1.125 +		arc4_stir();
   1.126 +	val = arc4_seeded_ok ? 0 : -1;
   1.127 +	_ARC4_UNLOCK();
   1.128 +	return val;
   1.129 +}
   1.130 +
   1.131 +static void
   1.132 +ev_arc4random_buf(void *buf, size_t n)
   1.133 +{
   1.134 +	arc4random_buf(buf, n);
   1.135 +}
   1.136 +
   1.137 +#endif /* } !_EVENT_HAVE_ARC4RANDOM */
   1.138 +
   1.139 +void
   1.140 +evutil_secure_rng_get_bytes(void *buf, size_t n)
   1.141 +{
   1.142 +	ev_arc4random_buf(buf, n);
   1.143 +}
   1.144 +
   1.145 +#ifndef __OpenBSD__
   1.146 +void
   1.147 +evutil_secure_rng_add_bytes(const char *buf, size_t n)
   1.148 +{
   1.149 +	arc4random_addrandom((unsigned char*)buf,
   1.150 +	    n>(size_t)INT_MAX ? INT_MAX : (int)n);
   1.151 +}
   1.152 +#endif

mercurial