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

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /*
michael@0 2 * Copyright (c) 2007-2012 Niels Provos and Nick Mathewson
michael@0 3 *
michael@0 4 * Redistribution and use in source and binary forms, with or without
michael@0 5 * modification, are permitted provided that the following conditions
michael@0 6 * are met:
michael@0 7 * 1. Redistributions of source code must retain the above copyright
michael@0 8 * notice, this list of conditions and the following disclaimer.
michael@0 9 * 2. Redistributions in binary form must reproduce the above copyright
michael@0 10 * notice, this list of conditions and the following disclaimer in the
michael@0 11 * documentation and/or other materials provided with the distribution.
michael@0 12 * 3. The name of the author may not be used to endorse or promote products
michael@0 13 * derived from this software without specific prior written permission.
michael@0 14 *
michael@0 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
michael@0 16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
michael@0 17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
michael@0 18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
michael@0 19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
michael@0 20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
michael@0 21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
michael@0 22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
michael@0 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
michael@0 24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
michael@0 25 */
michael@0 26
michael@0 27 /* This file has our secure PRNG code. On platforms that have arc4random(),
michael@0 28 * we just use that. Otherwise, we include arc4random.c as a bunch of static
michael@0 29 * functions, and wrap it lightly. We don't expose the arc4random*() APIs
michael@0 30 * because A) they aren't in our namespace, and B) it's not nice to name your
michael@0 31 * APIs after their implementations. We keep them in a separate file
michael@0 32 * so that other people can rip it out and use it for whatever.
michael@0 33 */
michael@0 34
michael@0 35 #include "event2/event-config.h"
michael@0 36
michael@0 37 #include <limits.h>
michael@0 38
michael@0 39 #include "util-internal.h"
michael@0 40 #include "evthread-internal.h"
michael@0 41
michael@0 42 #ifdef _EVENT_HAVE_ARC4RANDOM
michael@0 43 #include <stdlib.h>
michael@0 44 #include <string.h>
michael@0 45 int
michael@0 46 evutil_secure_rng_init(void)
michael@0 47 {
michael@0 48 /* call arc4random() now to force it to self-initialize */
michael@0 49 (void) arc4random();
michael@0 50 return 0;
michael@0 51 }
michael@0 52 int
michael@0 53 evutil_secure_rng_global_setup_locks_(const int enable_locks)
michael@0 54 {
michael@0 55 return 0;
michael@0 56 }
michael@0 57
michael@0 58 static void
michael@0 59 ev_arc4random_buf(void *buf, size_t n)
michael@0 60 {
michael@0 61 #if defined(_EVENT_HAVE_ARC4RANDOM_BUF) && !defined(__APPLE__)
michael@0 62 return arc4random_buf(buf, n);
michael@0 63 #else
michael@0 64 unsigned char *b = buf;
michael@0 65
michael@0 66 /* Make sure that we start out with b at a 4-byte alignment; plenty
michael@0 67 * of CPUs care about this for 32-bit access. */
michael@0 68 if (n >= 4 && ((ev_uintptr_t)b) & 3) {
michael@0 69 ev_uint32_t u = arc4random();
michael@0 70 int n_bytes = 4 - (((ev_uintptr_t)b) & 3);
michael@0 71 memcpy(b, &u, n_bytes);
michael@0 72 b += n_bytes;
michael@0 73 n -= n_bytes;
michael@0 74 }
michael@0 75 while (n >= 4) {
michael@0 76 *(ev_uint32_t*)b = arc4random();
michael@0 77 b += 4;
michael@0 78 n -= 4;
michael@0 79 }
michael@0 80 if (n) {
michael@0 81 ev_uint32_t u = arc4random();
michael@0 82 memcpy(b, &u, n);
michael@0 83 }
michael@0 84 #endif
michael@0 85 }
michael@0 86
michael@0 87 #else /* !_EVENT_HAVE_ARC4RANDOM { */
michael@0 88
michael@0 89 #ifdef _EVENT_ssize_t
michael@0 90 #define ssize_t _EVENT_SSIZE_t
michael@0 91 #endif
michael@0 92 #define ARC4RANDOM_EXPORT static
michael@0 93 #define _ARC4_LOCK() EVLOCK_LOCK(arc4rand_lock, 0)
michael@0 94 #define _ARC4_UNLOCK() EVLOCK_UNLOCK(arc4rand_lock, 0)
michael@0 95 #ifndef _EVENT_DISABLE_THREAD_SUPPORT
michael@0 96 static void *arc4rand_lock;
michael@0 97 #endif
michael@0 98
michael@0 99 #define ARC4RANDOM_UINT32 ev_uint32_t
michael@0 100 #define ARC4RANDOM_NOSTIR
michael@0 101 #define ARC4RANDOM_NORANDOM
michael@0 102 #define ARC4RANDOM_NOUNIFORM
michael@0 103
michael@0 104 #include "./arc4random.c"
michael@0 105
michael@0 106 #ifndef _EVENT_DISABLE_THREAD_SUPPORT
michael@0 107 int
michael@0 108 evutil_secure_rng_global_setup_locks_(const int enable_locks)
michael@0 109 {
michael@0 110 EVTHREAD_SETUP_GLOBAL_LOCK(arc4rand_lock, 0);
michael@0 111 return 0;
michael@0 112 }
michael@0 113 #endif
michael@0 114
michael@0 115 int
michael@0 116 evutil_secure_rng_init(void)
michael@0 117 {
michael@0 118 int val;
michael@0 119
michael@0 120 _ARC4_LOCK();
michael@0 121 if (!arc4_seeded_ok)
michael@0 122 arc4_stir();
michael@0 123 val = arc4_seeded_ok ? 0 : -1;
michael@0 124 _ARC4_UNLOCK();
michael@0 125 return val;
michael@0 126 }
michael@0 127
michael@0 128 static void
michael@0 129 ev_arc4random_buf(void *buf, size_t n)
michael@0 130 {
michael@0 131 arc4random_buf(buf, n);
michael@0 132 }
michael@0 133
michael@0 134 #endif /* } !_EVENT_HAVE_ARC4RANDOM */
michael@0 135
michael@0 136 void
michael@0 137 evutil_secure_rng_get_bytes(void *buf, size_t n)
michael@0 138 {
michael@0 139 ev_arc4random_buf(buf, n);
michael@0 140 }
michael@0 141
michael@0 142 #ifndef __OpenBSD__
michael@0 143 void
michael@0 144 evutil_secure_rng_add_bytes(const char *buf, size_t n)
michael@0 145 {
michael@0 146 arc4random_addrandom((unsigned char*)buf,
michael@0 147 n>(size_t)INT_MAX ? INT_MAX : (int)n);
michael@0 148 }
michael@0 149 #endif

mercurial