blob: 9bdf3417e0891b8dbca3b53eedf225ab999f2b63 [file] [log] [blame]
Elliott Hughes0468feb2014-06-20 22:49:20 -07001/* $OpenBSD: arc4random.c,v 1.33 2014/06/13 18:58:58 deraadt Exp $ */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002
3/*
4 * Copyright (c) 1996, David Mazieres <dm@uun.org>
5 * Copyright (c) 2008, Damien Miller <djm@openbsd.org>
Elliott Hughes0468feb2014-06-20 22:49:20 -07006 * Copyright (c) 2013, Markus Friedl <markus@openbsd.org>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08007 *
8 * Permission to use, copy, modify, and distribute this software for any
9 * purpose with or without fee is hereby granted, provided that the above
10 * copyright notice and this permission notice appear in all copies.
11 *
12 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 */
20
21/*
Elliott Hughes0468feb2014-06-20 22:49:20 -070022 * ChaCha based random number generator for OpenBSD.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080023 */
24
25#include <fcntl.h>
26#include <limits.h>
27#include <stdlib.h>
Elliott Hughes0468feb2014-06-20 22:49:20 -070028#include <string.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080029#include <unistd.h>
30#include <sys/types.h>
31#include <sys/param.h>
32#include <sys/time.h>
Elliott Hughes0468feb2014-06-20 22:49:20 -070033#include <sys/mman.h>
34
35#if defined(__ANDROID__)
36#include <sys/stat.h>
37#include <linux/random.h>
38#include "private/libc_logging.h"
Elliott Hugheseb847bc2013-10-09 15:50:50 -070039#include "private/thread_private.h"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080040
Elliott Hughes0468feb2014-06-20 22:49:20 -070041#define explicit_bzero(p, s) memset(p, 0, s)
42
43#undef MAP_ANON
44#define MAP_ANON (MAP_PRIVATE | MAP_ANONYMOUS)
45
46/*
47 * XXX Should be replaced with a proper entropy measure.
48 */
49static int
50gotdata(u_char *buf, size_t len)
51{
52 char any_set = 0;
53 size_t i;
54
55 for (i = 0; i < len; ++i)
56 any_set |= buf[i];
57 if (any_set == 0)
58 return -1;
59 return 0;
60}
61
62static int
63getentropy/*_urandom*/(u_char *buf, size_t len)
64{
65 int save_errno = errno;
66
67 int fd = TEMP_FAILURE_RETRY(open("/dev/urandom", O_RDONLY|O_CLOEXEC|O_NOFOLLOW, 0));
68 if (fd == -1) {
69 __libc_fatal("getentropy_urandom failed to open \"/dev/urandom\": %s",
70 strerror(errno));
71 }
72
73 /* Lightly verify that the device node looks sane */
74 struct stat st;
75 if (fstat(fd, &st) == -1 || !S_ISCHR(st.st_mode)) {
76 __libc_fatal("getentropy_urandom failed to fstat \"/dev/urandom\": %s",
77 strerror(errno));
78 }
79 int cnt;
80 if (ioctl(fd, RNDGETENTCNT, &cnt) == -1) {
81 __libc_fatal("getentropy_urandom failed to ioctl \"/dev/urandom\": %s",
82 strerror(errno));
83 }
84 for (size_t i = 0; i < len; ) {
85 size_t wanted = len - i;
86 ssize_t ret = TEMP_FAILURE_RETRY(read(fd, buf + i, wanted));
87
88 if (ret == -1) {
89 __libc_fatal("getentropy_urandom failed to read \"/dev/urandom\": %s",
90 strerror(errno));
91 }
92 i += ret;
93 }
94 close(fd);
95 if (gotdata(buf, len) == -1) {
96 __libc_fatal("getentropy_urandom failed to get enough entropy: %s",
97 strerror(errno));
98 }
99
100 errno = save_errno;
101 return 0;
102}
103#endif /* __ANDROID__ */
104
105#define KEYSTREAM_ONLY
106#pragma GCC diagnostic push
107#pragma GCC diagnostic ignored "-Wunused-parameter"
108#include "../upstream-openbsd/lib/libc/crypt/chacha_private.h"
109#pragma GCC diagnostic pop
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800110
111#ifdef __GNUC__
112#define inline __inline
Elliott Hughes0468feb2014-06-20 22:49:20 -0700113#else /* !__GNUC__ */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800114#define inline
Elliott Hughes0468feb2014-06-20 22:49:20 -0700115#endif /* !__GNUC__ */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800116
Elliott Hughes0468feb2014-06-20 22:49:20 -0700117#define KEYSZ 32
118#define IVSZ 8
119#define BLOCKSZ 64
120#define RSBUFSZ (16*BLOCKSZ)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800121static int rs_initialized;
Elliott Hughes0468feb2014-06-20 22:49:20 -0700122static pid_t rs_stir_pid;
123static chacha_ctx *rs; /* chacha context for random keystream */
124static u_char *rs_buf; /* keystream blocks */
125static size_t rs_have; /* valid bytes at end of rs_buf */
126static size_t rs_count; /* bytes till reseed */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800127
Elliott Hughes0468feb2014-06-20 22:49:20 -0700128static inline void _rs_rekey(u_char *dat, size_t datlen);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800129
130static inline void
Elliott Hughes0468feb2014-06-20 22:49:20 -0700131_rs_init(u_char *buf, size_t n)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800132{
Elliott Hughes0468feb2014-06-20 22:49:20 -0700133 if (n < KEYSZ + IVSZ)
134 return;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800135
Elliott Hughes0468feb2014-06-20 22:49:20 -0700136 if (rs == NULL && (rs = mmap(NULL, sizeof(*rs), PROT_READ|PROT_WRITE,
137 MAP_ANON, -1, 0)) == MAP_FAILED)
138 abort();
139 if (rs_buf == NULL && (rs_buf = mmap(NULL, RSBUFSZ, PROT_READ|PROT_WRITE,
140 MAP_ANON, -1, 0)) == MAP_FAILED)
141 abort();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800142
Elliott Hughes0468feb2014-06-20 22:49:20 -0700143 chacha_keysetup(rs, buf, KEYSZ * 8, 0);
144 chacha_ivsetup(rs, buf + KEYSZ);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800145}
146
147static void
Elliott Hughes0468feb2014-06-20 22:49:20 -0700148_rs_stir(void)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800149{
Elliott Hughes0468feb2014-06-20 22:49:20 -0700150 u_char rnd[KEYSZ + IVSZ];
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800151
Elliott Hughes0468feb2014-06-20 22:49:20 -0700152 /* XXX */
153 (void) getentropy(rnd, sizeof rnd);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800154
Elliott Hughes0468feb2014-06-20 22:49:20 -0700155 if (!rs_initialized) {
156 rs_initialized = 1;
157 _rs_init(rnd, sizeof(rnd));
158 } else
159 _rs_rekey(rnd, sizeof(rnd));
160 explicit_bzero(rnd, sizeof(rnd));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800161
Elliott Hughes0468feb2014-06-20 22:49:20 -0700162 /* invalidate rs_buf */
163 rs_have = 0;
164 memset(rs_buf, 0, RSBUFSZ);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800165
Elliott Hughes0468feb2014-06-20 22:49:20 -0700166 rs_count = 1600000;
167}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800168
Elliott Hughes0468feb2014-06-20 22:49:20 -0700169static inline void
170_rs_stir_if_needed(size_t len)
171{
172 pid_t pid = getpid();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800173
Elliott Hughes0468feb2014-06-20 22:49:20 -0700174 if (rs_count <= len || !rs_initialized || rs_stir_pid != pid) {
175 rs_stir_pid = pid;
176 _rs_stir();
177 } else
178 rs_count -= len;
179}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800180
Elliott Hughes0468feb2014-06-20 22:49:20 -0700181static inline void
182_rs_rekey(u_char *dat, size_t datlen)
183{
184#ifndef KEYSTREAM_ONLY
185 memset(rs_buf, 0,RSBUFSZ);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800186#endif
Elliott Hughes0468feb2014-06-20 22:49:20 -0700187 /* fill rs_buf with the keystream */
188 chacha_encrypt_bytes(rs, rs_buf, rs_buf, RSBUFSZ);
189 /* mix in optional user provided data */
190 if (dat) {
191 size_t i, m;
192
193 m = MIN(datlen, KEYSZ + IVSZ);
194 for (i = 0; i < m; i++)
195 rs_buf[i] ^= dat[i];
196 }
197 /* immediately reinit for backtracking resistance */
198 _rs_init(rs_buf, KEYSZ + IVSZ);
199 memset(rs_buf, 0, KEYSZ + IVSZ);
200 rs_have = RSBUFSZ - KEYSZ - IVSZ;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800201}
202
Elliott Hughes0468feb2014-06-20 22:49:20 -0700203static inline void
204_rs_random_buf(void *_buf, size_t n)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800205{
Elliott Hughes0468feb2014-06-20 22:49:20 -0700206 u_char *buf = (u_char *)_buf;
207 size_t m;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800208
Elliott Hughes0468feb2014-06-20 22:49:20 -0700209 _rs_stir_if_needed(n);
210 while (n > 0) {
211 if (rs_have > 0) {
212 m = MIN(n, rs_have);
213 memcpy(buf, rs_buf + RSBUFSZ - rs_have, m);
214 memset(rs_buf + RSBUFSZ - rs_have, 0, m);
215 buf += m;
216 n -= m;
217 rs_have -= m;
218 }
219 if (rs_have == 0)
220 _rs_rekey(NULL, 0);
221 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800222}
223
Elliott Hughes0468feb2014-06-20 22:49:20 -0700224static inline void
225_rs_random_u32(u_int32_t *val)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800226{
Elliott Hughes0468feb2014-06-20 22:49:20 -0700227 _rs_stir_if_needed(sizeof(*val));
228 if (rs_have < sizeof(*val))
229 _rs_rekey(NULL, 0);
230 memcpy(val, rs_buf + RSBUFSZ - rs_have, sizeof(*val));
231 memset(rs_buf + RSBUFSZ - rs_have, 0, sizeof(*val));
232 rs_have -= sizeof(*val);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800233}
234
235u_int32_t
236arc4random(void)
237{
Elliott Hughes0468feb2014-06-20 22:49:20 -0700238 u_int32_t val;
239
240 _ARC4_LOCK();
241 _rs_random_u32(&val);
242 _ARC4_UNLOCK();
243 return val;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800244}
245
246void
Elliott Hughes0468feb2014-06-20 22:49:20 -0700247arc4random_buf(void *buf, size_t n)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800248{
Elliott Hughes0468feb2014-06-20 22:49:20 -0700249 _ARC4_LOCK();
250 _rs_random_buf(buf, n);
251 _ARC4_UNLOCK();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800252}
253
254/*
255 * Calculate a uniformly distributed random number less than upper_bound
256 * avoiding "modulo bias".
257 *
258 * Uniformity is achieved by generating new random numbers until the one
259 * returned is outside the range [0, 2**32 % upper_bound). This
260 * guarantees the selected random number will be inside
261 * [2**32 % upper_bound, 2**32) which maps back to [0, upper_bound)
262 * after reduction modulo upper_bound.
263 */
264u_int32_t
265arc4random_uniform(u_int32_t upper_bound)
266{
Elliott Hughes0468feb2014-06-20 22:49:20 -0700267 u_int32_t r, min;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800268
Elliott Hughes0468feb2014-06-20 22:49:20 -0700269 if (upper_bound < 2)
270 return 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800271
Elliott Hughes0468feb2014-06-20 22:49:20 -0700272 /* 2**32 % x == (2**32 - x) % x */
273 min = -upper_bound % upper_bound;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800274
Elliott Hughes0468feb2014-06-20 22:49:20 -0700275 /*
276 * This could theoretically loop forever but each retry has
277 * p > 0.5 (worst case, usually far better) of selecting a
278 * number inside the range we need, so it should rarely need
279 * to re-roll.
280 */
281 for (;;) {
282 r = arc4random();
283 if (r >= min)
284 break;
285 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800286
Elliott Hughes0468feb2014-06-20 22:49:20 -0700287 return r % upper_bound;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800288}