Elliott Hughes | 0468feb | 2014-06-20 22:49:20 -0700 | [diff] [blame^] | 1 | /* $OpenBSD: arc4random.c,v 1.33 2014/06/13 18:58:58 deraadt Exp $ */ |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 2 | |
| 3 | /* |
| 4 | * Copyright (c) 1996, David Mazieres <dm@uun.org> |
| 5 | * Copyright (c) 2008, Damien Miller <djm@openbsd.org> |
Elliott Hughes | 0468feb | 2014-06-20 22:49:20 -0700 | [diff] [blame^] | 6 | * Copyright (c) 2013, Markus Friedl <markus@openbsd.org> |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 7 | * |
| 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 Hughes | 0468feb | 2014-06-20 22:49:20 -0700 | [diff] [blame^] | 22 | * ChaCha based random number generator for OpenBSD. |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 23 | */ |
| 24 | |
| 25 | #include <fcntl.h> |
| 26 | #include <limits.h> |
| 27 | #include <stdlib.h> |
Elliott Hughes | 0468feb | 2014-06-20 22:49:20 -0700 | [diff] [blame^] | 28 | #include <string.h> |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 29 | #include <unistd.h> |
| 30 | #include <sys/types.h> |
| 31 | #include <sys/param.h> |
| 32 | #include <sys/time.h> |
Elliott Hughes | 0468feb | 2014-06-20 22:49:20 -0700 | [diff] [blame^] | 33 | #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 Hughes | eb847bc | 2013-10-09 15:50:50 -0700 | [diff] [blame] | 39 | #include "private/thread_private.h" |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 40 | |
Elliott Hughes | 0468feb | 2014-06-20 22:49:20 -0700 | [diff] [blame^] | 41 | #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 | */ |
| 49 | static int |
| 50 | gotdata(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 | |
| 62 | static int |
| 63 | getentropy/*_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 Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 110 | |
| 111 | #ifdef __GNUC__ |
| 112 | #define inline __inline |
Elliott Hughes | 0468feb | 2014-06-20 22:49:20 -0700 | [diff] [blame^] | 113 | #else /* !__GNUC__ */ |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 114 | #define inline |
Elliott Hughes | 0468feb | 2014-06-20 22:49:20 -0700 | [diff] [blame^] | 115 | #endif /* !__GNUC__ */ |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 116 | |
Elliott Hughes | 0468feb | 2014-06-20 22:49:20 -0700 | [diff] [blame^] | 117 | #define KEYSZ 32 |
| 118 | #define IVSZ 8 |
| 119 | #define BLOCKSZ 64 |
| 120 | #define RSBUFSZ (16*BLOCKSZ) |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 121 | static int rs_initialized; |
Elliott Hughes | 0468feb | 2014-06-20 22:49:20 -0700 | [diff] [blame^] | 122 | static pid_t rs_stir_pid; |
| 123 | static chacha_ctx *rs; /* chacha context for random keystream */ |
| 124 | static u_char *rs_buf; /* keystream blocks */ |
| 125 | static size_t rs_have; /* valid bytes at end of rs_buf */ |
| 126 | static size_t rs_count; /* bytes till reseed */ |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 127 | |
Elliott Hughes | 0468feb | 2014-06-20 22:49:20 -0700 | [diff] [blame^] | 128 | static inline void _rs_rekey(u_char *dat, size_t datlen); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 129 | |
| 130 | static inline void |
Elliott Hughes | 0468feb | 2014-06-20 22:49:20 -0700 | [diff] [blame^] | 131 | _rs_init(u_char *buf, size_t n) |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 132 | { |
Elliott Hughes | 0468feb | 2014-06-20 22:49:20 -0700 | [diff] [blame^] | 133 | if (n < KEYSZ + IVSZ) |
| 134 | return; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 135 | |
Elliott Hughes | 0468feb | 2014-06-20 22:49:20 -0700 | [diff] [blame^] | 136 | 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 Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 142 | |
Elliott Hughes | 0468feb | 2014-06-20 22:49:20 -0700 | [diff] [blame^] | 143 | chacha_keysetup(rs, buf, KEYSZ * 8, 0); |
| 144 | chacha_ivsetup(rs, buf + KEYSZ); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | static void |
Elliott Hughes | 0468feb | 2014-06-20 22:49:20 -0700 | [diff] [blame^] | 148 | _rs_stir(void) |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 149 | { |
Elliott Hughes | 0468feb | 2014-06-20 22:49:20 -0700 | [diff] [blame^] | 150 | u_char rnd[KEYSZ + IVSZ]; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 151 | |
Elliott Hughes | 0468feb | 2014-06-20 22:49:20 -0700 | [diff] [blame^] | 152 | /* XXX */ |
| 153 | (void) getentropy(rnd, sizeof rnd); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 154 | |
Elliott Hughes | 0468feb | 2014-06-20 22:49:20 -0700 | [diff] [blame^] | 155 | 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 Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 161 | |
Elliott Hughes | 0468feb | 2014-06-20 22:49:20 -0700 | [diff] [blame^] | 162 | /* invalidate rs_buf */ |
| 163 | rs_have = 0; |
| 164 | memset(rs_buf, 0, RSBUFSZ); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 165 | |
Elliott Hughes | 0468feb | 2014-06-20 22:49:20 -0700 | [diff] [blame^] | 166 | rs_count = 1600000; |
| 167 | } |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 168 | |
Elliott Hughes | 0468feb | 2014-06-20 22:49:20 -0700 | [diff] [blame^] | 169 | static inline void |
| 170 | _rs_stir_if_needed(size_t len) |
| 171 | { |
| 172 | pid_t pid = getpid(); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 173 | |
Elliott Hughes | 0468feb | 2014-06-20 22:49:20 -0700 | [diff] [blame^] | 174 | 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 Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 180 | |
Elliott Hughes | 0468feb | 2014-06-20 22:49:20 -0700 | [diff] [blame^] | 181 | static 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 Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 186 | #endif |
Elliott Hughes | 0468feb | 2014-06-20 22:49:20 -0700 | [diff] [blame^] | 187 | /* 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 Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 201 | } |
| 202 | |
Elliott Hughes | 0468feb | 2014-06-20 22:49:20 -0700 | [diff] [blame^] | 203 | static inline void |
| 204 | _rs_random_buf(void *_buf, size_t n) |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 205 | { |
Elliott Hughes | 0468feb | 2014-06-20 22:49:20 -0700 | [diff] [blame^] | 206 | u_char *buf = (u_char *)_buf; |
| 207 | size_t m; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 208 | |
Elliott Hughes | 0468feb | 2014-06-20 22:49:20 -0700 | [diff] [blame^] | 209 | _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 Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 222 | } |
| 223 | |
Elliott Hughes | 0468feb | 2014-06-20 22:49:20 -0700 | [diff] [blame^] | 224 | static inline void |
| 225 | _rs_random_u32(u_int32_t *val) |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 226 | { |
Elliott Hughes | 0468feb | 2014-06-20 22:49:20 -0700 | [diff] [blame^] | 227 | _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 Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | u_int32_t |
| 236 | arc4random(void) |
| 237 | { |
Elliott Hughes | 0468feb | 2014-06-20 22:49:20 -0700 | [diff] [blame^] | 238 | u_int32_t val; |
| 239 | |
| 240 | _ARC4_LOCK(); |
| 241 | _rs_random_u32(&val); |
| 242 | _ARC4_UNLOCK(); |
| 243 | return val; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | void |
Elliott Hughes | 0468feb | 2014-06-20 22:49:20 -0700 | [diff] [blame^] | 247 | arc4random_buf(void *buf, size_t n) |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 248 | { |
Elliott Hughes | 0468feb | 2014-06-20 22:49:20 -0700 | [diff] [blame^] | 249 | _ARC4_LOCK(); |
| 250 | _rs_random_buf(buf, n); |
| 251 | _ARC4_UNLOCK(); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 252 | } |
| 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 | */ |
| 264 | u_int32_t |
| 265 | arc4random_uniform(u_int32_t upper_bound) |
| 266 | { |
Elliott Hughes | 0468feb | 2014-06-20 22:49:20 -0700 | [diff] [blame^] | 267 | u_int32_t r, min; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 268 | |
Elliott Hughes | 0468feb | 2014-06-20 22:49:20 -0700 | [diff] [blame^] | 269 | if (upper_bound < 2) |
| 270 | return 0; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 271 | |
Elliott Hughes | 0468feb | 2014-06-20 22:49:20 -0700 | [diff] [blame^] | 272 | /* 2**32 % x == (2**32 - x) % x */ |
| 273 | min = -upper_bound % upper_bound; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 274 | |
Elliott Hughes | 0468feb | 2014-06-20 22:49:20 -0700 | [diff] [blame^] | 275 | /* |
| 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 Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 286 | |
Elliott Hughes | 0468feb | 2014-06-20 22:49:20 -0700 | [diff] [blame^] | 287 | return r % upper_bound; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 288 | } |