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