Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1 | /* Copyright (c) 2014, Google Inc. |
| 2 | * |
| 3 | * Permission to use, copy, modify, and/or distribute this software for any |
| 4 | * purpose with or without fee is hereby granted, provided that the above |
| 5 | * copyright notice and this permission notice appear in all copies. |
| 6 | * |
| 7 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 8 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 9 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
| 10 | * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 11 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION |
| 12 | * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN |
| 13 | * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ |
| 14 | |
| 15 | #include <openssl/rand.h> |
| 16 | |
Adam Langley | d755456 | 2015-09-24 15:03:14 -0700 | [diff] [blame] | 17 | #include <assert.h> |
Matt Braithwaite | af3d5bd | 2015-04-24 17:40:19 -0700 | [diff] [blame] | 18 | #include <limits.h> |
Adam Langley | 310d4dd | 2015-04-13 11:04:21 -0700 | [diff] [blame] | 19 | #include <string.h> |
| 20 | |
David Benjamin | 1db476e | 2015-06-17 00:53:09 -0400 | [diff] [blame] | 21 | #include <openssl/chacha.h> |
Adam Langley | d755456 | 2015-09-24 15:03:14 -0700 | [diff] [blame] | 22 | #include <openssl/cpu.h> |
Adam Langley | 310d4dd | 2015-04-13 11:04:21 -0700 | [diff] [blame] | 23 | #include <openssl/mem.h> |
| 24 | |
| 25 | #include "internal.h" |
| 26 | #include "../internal.h" |
| 27 | |
| 28 | |
| 29 | /* It's assumed that the operating system always has an unfailing source of |
| 30 | * entropy which is accessed via |CRYPTO_sysrand|. (If the operating system |
| 31 | * entropy source fails, it's up to |CRYPTO_sysrand| to abort the process—we |
| 32 | * don't try to handle it.) |
| 33 | * |
| 34 | * In addition, the hardware may provide a low-latency RNG. Intel's rdrand |
| 35 | * instruction is the canonical example of this. When a hardware RNG is |
| 36 | * available we don't need to worry about an RNG failure arising from fork()ing |
| 37 | * the process or moving a VM, so we can keep thread-local RNG state and XOR |
| 38 | * the hardware entropy in. |
| 39 | * |
| 40 | * (We assume that the OS entropy is safe from fork()ing and VM duplication. |
| 41 | * This might be a bit of a leap of faith, esp on Windows, but there's nothing |
| 42 | * that we can do about it.) */ |
| 43 | |
| 44 | /* rand_thread_state contains the per-thread state for the RNG. This is only |
| 45 | * used if the system has support for a hardware RNG. */ |
| 46 | struct rand_thread_state { |
| 47 | uint8_t key[32]; |
| 48 | uint64_t calls_used; |
| 49 | size_t bytes_used; |
| 50 | uint8_t partial_block[64]; |
| 51 | unsigned partial_block_used; |
| 52 | }; |
| 53 | |
| 54 | /* kMaxCallsPerRefresh is the maximum number of |RAND_bytes| calls that we'll |
| 55 | * serve before reading a new key from the operating system. This only applies |
| 56 | * if we have a hardware RNG. */ |
| 57 | static const unsigned kMaxCallsPerRefresh = 1024; |
| 58 | |
| 59 | /* kMaxBytesPerRefresh is the maximum number of bytes that we'll return from |
| 60 | * |RAND_bytes| before reading a new key from the operating system. This only |
| 61 | * applies if we have a hardware RNG. */ |
| 62 | static const uint64_t kMaxBytesPerRefresh = 1024 * 1024; |
| 63 | |
| 64 | /* rand_thread_state_free frees a |rand_thread_state|. This is called when a |
| 65 | * thread exits. */ |
| 66 | static void rand_thread_state_free(void *state) { |
| 67 | if (state == NULL) { |
| 68 | return; |
| 69 | } |
| 70 | |
| 71 | OPENSSL_cleanse(state, sizeof(struct rand_thread_state)); |
| 72 | OPENSSL_free(state); |
| 73 | } |
| 74 | |
David Benjamin | bc5b2a2 | 2016-03-01 22:57:32 -0500 | [diff] [blame] | 75 | #if defined(OPENSSL_X86_64) && !defined(OPENSSL_NO_ASM) && \ |
David Benjamin | ec978dd | 2016-11-04 18:59:33 -0400 | [diff] [blame] | 76 | !defined(BORINGSSL_UNSAFE_DETERMINISTIC_MODE) |
Adam Langley | d755456 | 2015-09-24 15:03:14 -0700 | [diff] [blame] | 77 | |
| 78 | /* These functions are defined in asm/rdrand-x86_64.pl */ |
| 79 | extern int CRYPTO_rdrand(uint8_t out[8]); |
| 80 | extern int CRYPTO_rdrand_multiple8_buf(uint8_t *buf, size_t len); |
| 81 | |
| 82 | static int have_rdrand(void) { |
| 83 | return (OPENSSL_ia32cap_P[1] & (1u << 30)) != 0; |
| 84 | } |
| 85 | |
| 86 | static int hwrand(uint8_t *buf, size_t len) { |
| 87 | if (!have_rdrand()) { |
| 88 | return 0; |
| 89 | } |
| 90 | |
| 91 | const size_t len_multiple8 = len & ~7; |
| 92 | if (!CRYPTO_rdrand_multiple8_buf(buf, len_multiple8)) { |
| 93 | return 0; |
| 94 | } |
| 95 | len -= len_multiple8; |
| 96 | |
| 97 | if (len != 0) { |
| 98 | assert(len < 8); |
| 99 | |
| 100 | uint8_t rand_buf[8]; |
| 101 | if (!CRYPTO_rdrand(rand_buf)) { |
| 102 | return 0; |
| 103 | } |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 104 | OPENSSL_memcpy(buf + len_multiple8, rand_buf, len); |
Adam Langley | d755456 | 2015-09-24 15:03:14 -0700 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | return 1; |
| 108 | } |
| 109 | |
| 110 | #else |
| 111 | |
| 112 | static int hwrand(uint8_t *buf, size_t len) { |
| 113 | return 0; |
| 114 | } |
| 115 | |
| 116 | #endif |
| 117 | |
Brian Smith | 3700778 | 2015-04-14 16:21:27 -1000 | [diff] [blame] | 118 | int RAND_bytes(uint8_t *buf, size_t len) { |
Adam Langley | 310d4dd | 2015-04-13 11:04:21 -0700 | [diff] [blame] | 119 | if (len == 0) { |
| 120 | return 1; |
| 121 | } |
| 122 | |
Adam Langley | d755456 | 2015-09-24 15:03:14 -0700 | [diff] [blame] | 123 | if (!hwrand(buf, len)) { |
Adam Langley | 310d4dd | 2015-04-13 11:04:21 -0700 | [diff] [blame] | 124 | /* Without a hardware RNG to save us from address-space duplication, the OS |
| 125 | * entropy is used directly. */ |
| 126 | CRYPTO_sysrand(buf, len); |
| 127 | return 1; |
| 128 | } |
| 129 | |
| 130 | struct rand_thread_state *state = |
| 131 | CRYPTO_get_thread_local(OPENSSL_THREAD_LOCAL_RAND); |
| 132 | if (state == NULL) { |
| 133 | state = OPENSSL_malloc(sizeof(struct rand_thread_state)); |
| 134 | if (state == NULL || |
| 135 | !CRYPTO_set_thread_local(OPENSSL_THREAD_LOCAL_RAND, state, |
| 136 | rand_thread_state_free)) { |
| 137 | CRYPTO_sysrand(buf, len); |
| 138 | return 1; |
| 139 | } |
| 140 | |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 141 | OPENSSL_memset(state->partial_block, 0, sizeof(state->partial_block)); |
Adam Langley | 310d4dd | 2015-04-13 11:04:21 -0700 | [diff] [blame] | 142 | state->calls_used = kMaxCallsPerRefresh; |
| 143 | } |
| 144 | |
| 145 | if (state->calls_used >= kMaxCallsPerRefresh || |
| 146 | state->bytes_used >= kMaxBytesPerRefresh) { |
| 147 | CRYPTO_sysrand(state->key, sizeof(state->key)); |
| 148 | state->calls_used = 0; |
| 149 | state->bytes_used = 0; |
| 150 | state->partial_block_used = sizeof(state->partial_block); |
| 151 | } |
| 152 | |
Adam Langley | 310d4dd | 2015-04-13 11:04:21 -0700 | [diff] [blame] | 153 | if (len >= sizeof(state->partial_block)) { |
| 154 | size_t remaining = len; |
| 155 | while (remaining > 0) { |
David Benjamin | 1b92f64 | 2015-10-17 22:26:54 -0400 | [diff] [blame] | 156 | /* kMaxBytesPerCall is only 2GB, while ChaCha can handle 256GB. But this |
| 157 | * is sufficient and easier on 32-bit. */ |
Adam Langley | 310d4dd | 2015-04-13 11:04:21 -0700 | [diff] [blame] | 158 | static const size_t kMaxBytesPerCall = 0x80000000; |
| 159 | size_t todo = remaining; |
| 160 | if (todo > kMaxBytesPerCall) { |
| 161 | todo = kMaxBytesPerCall; |
| 162 | } |
Brian Smith | e80a2ec | 2015-10-03 08:13:36 -1000 | [diff] [blame] | 163 | uint8_t nonce[12]; |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 164 | OPENSSL_memset(nonce, 0, 4); |
| 165 | OPENSSL_memcpy(nonce + 4, &state->calls_used, sizeof(state->calls_used)); |
Brian Smith | e80a2ec | 2015-10-03 08:13:36 -1000 | [diff] [blame] | 166 | CRYPTO_chacha_20(buf, buf, todo, state->key, nonce, 0); |
Adam Langley | 310d4dd | 2015-04-13 11:04:21 -0700 | [diff] [blame] | 167 | buf += todo; |
| 168 | remaining -= todo; |
| 169 | state->calls_used++; |
| 170 | } |
| 171 | } else { |
| 172 | if (sizeof(state->partial_block) - state->partial_block_used < len) { |
Brian Smith | e80a2ec | 2015-10-03 08:13:36 -1000 | [diff] [blame] | 173 | uint8_t nonce[12]; |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 174 | OPENSSL_memset(nonce, 0, 4); |
| 175 | OPENSSL_memcpy(nonce + 4, &state->calls_used, sizeof(state->calls_used)); |
Adam Langley | 310d4dd | 2015-04-13 11:04:21 -0700 | [diff] [blame] | 176 | CRYPTO_chacha_20(state->partial_block, state->partial_block, |
Brian Smith | e80a2ec | 2015-10-03 08:13:36 -1000 | [diff] [blame] | 177 | sizeof(state->partial_block), state->key, nonce, 0); |
Adam Langley | 310d4dd | 2015-04-13 11:04:21 -0700 | [diff] [blame] | 178 | state->partial_block_used = 0; |
| 179 | } |
| 180 | |
| 181 | unsigned i; |
| 182 | for (i = 0; i < len; i++) { |
| 183 | buf[i] ^= state->partial_block[state->partial_block_used++]; |
| 184 | } |
| 185 | state->calls_used++; |
| 186 | } |
| 187 | state->bytes_used += len; |
| 188 | |
| 189 | return 1; |
| 190 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 191 | |
| 192 | int RAND_pseudo_bytes(uint8_t *buf, size_t len) { |
| 193 | return RAND_bytes(buf, len); |
| 194 | } |
| 195 | |
Adam Langley | c5c85de | 2015-11-16 10:10:59 -0800 | [diff] [blame] | 196 | void RAND_seed(const void *buf, int num) { |
| 197 | /* OpenSSH calls |RAND_seed| before jailing on the assumption that any needed |
| 198 | * file descriptors etc will be opened. */ |
| 199 | uint8_t unused; |
| 200 | RAND_bytes(&unused, sizeof(unused)); |
| 201 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 202 | |
Matt Braithwaite | af3d5bd | 2015-04-24 17:40:19 -0700 | [diff] [blame] | 203 | int RAND_load_file(const char *path, long num) { |
| 204 | if (num < 0) { /* read the "whole file" */ |
| 205 | return 1; |
| 206 | } else if (num <= INT_MAX) { |
| 207 | return (int) num; |
| 208 | } else { |
| 209 | return INT_MAX; |
| 210 | } |
| 211 | } |
| 212 | |
David Benjamin | e5aa791 | 2016-01-26 01:09:19 -0500 | [diff] [blame] | 213 | const char *RAND_file_name(char *buf, size_t num) { return NULL; } |
| 214 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 215 | void RAND_add(const void *buf, int num, double entropy) {} |
| 216 | |
Matt Braithwaite | 3e5e99d | 2015-06-24 16:41:10 -0700 | [diff] [blame] | 217 | int RAND_egd(const char *path) { |
| 218 | return 255; |
| 219 | } |
| 220 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 221 | int RAND_poll(void) { |
| 222 | return 1; |
| 223 | } |
Adam Langley | c3ef76f | 2015-04-13 14:34:17 -0700 | [diff] [blame] | 224 | |
| 225 | int RAND_status(void) { |
| 226 | return 1; |
| 227 | } |
Matt Braithwaite | 3e5e99d | 2015-06-24 16:41:10 -0700 | [diff] [blame] | 228 | |
Matt Braithwaite | bc97c69 | 2015-07-22 18:16:18 -0700 | [diff] [blame] | 229 | static const struct rand_meth_st kSSLeayMethod = { |
| 230 | RAND_seed, |
| 231 | RAND_bytes, |
| 232 | RAND_cleanup, |
| 233 | RAND_add, |
| 234 | RAND_pseudo_bytes, |
| 235 | RAND_status, |
| 236 | }; |
Matt Braithwaite | 3e5e99d | 2015-06-24 16:41:10 -0700 | [diff] [blame] | 237 | |
| 238 | RAND_METHOD *RAND_SSLeay(void) { |
| 239 | return (RAND_METHOD*) &kSSLeayMethod; |
| 240 | } |
| 241 | |
Adam Langley | a59347e | 2015-06-24 17:02:15 -0700 | [diff] [blame] | 242 | void RAND_set_rand_method(const RAND_METHOD *method) {} |
Alessandro Ghedini | 0d099f0 | 2016-07-10 01:57:44 +0100 | [diff] [blame] | 243 | |
| 244 | void RAND_cleanup(void) {} |