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 |
Adam Langley | 88bb848 | 2017-04-10 16:53:20 -0700 | [diff] [blame^] | 37 | * the process or moving a VM, so we can keep thread-local RNG state and use it |
| 38 | * as an additional-data input to CTR-DRBG. |
Adam Langley | 310d4dd | 2015-04-13 11:04:21 -0700 | [diff] [blame] | 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 | |
Adam Langley | 88bb848 | 2017-04-10 16:53:20 -0700 | [diff] [blame^] | 44 | /* kReseedInterval is the number of generate calls made to CTR-DRBG before |
| 45 | * reseeding. */ |
| 46 | static const unsigned kReseedInterval = 4096; |
| 47 | |
| 48 | /* rand_thread_state contains the per-thread state for the RNG. */ |
Adam Langley | 310d4dd | 2015-04-13 11:04:21 -0700 | [diff] [blame] | 49 | struct rand_thread_state { |
Adam Langley | 88bb848 | 2017-04-10 16:53:20 -0700 | [diff] [blame^] | 50 | CTR_DRBG_STATE drbg; |
| 51 | /* calls is the number of generate calls made on |drbg| since it was last |
| 52 | * (re)seeded. This is bound by |kReseedInterval|. */ |
| 53 | unsigned calls; |
Adam Langley | 310d4dd | 2015-04-13 11:04:21 -0700 | [diff] [blame] | 54 | }; |
| 55 | |
Adam Langley | 310d4dd | 2015-04-13 11:04:21 -0700 | [diff] [blame] | 56 | /* rand_thread_state_free frees a |rand_thread_state|. This is called when a |
| 57 | * thread exits. */ |
Adam Langley | 88bb848 | 2017-04-10 16:53:20 -0700 | [diff] [blame^] | 58 | static void rand_thread_state_free(void *state_in) { |
| 59 | if (state_in == NULL) { |
Adam Langley | 310d4dd | 2015-04-13 11:04:21 -0700 | [diff] [blame] | 60 | return; |
| 61 | } |
| 62 | |
Adam Langley | 88bb848 | 2017-04-10 16:53:20 -0700 | [diff] [blame^] | 63 | struct rand_thread_state *state = state_in; |
| 64 | CTR_DRBG_clear(&state->drbg); |
Adam Langley | 310d4dd | 2015-04-13 11:04:21 -0700 | [diff] [blame] | 65 | OPENSSL_free(state); |
| 66 | } |
| 67 | |
David Benjamin | bc5b2a2 | 2016-03-01 22:57:32 -0500 | [diff] [blame] | 68 | #if defined(OPENSSL_X86_64) && !defined(OPENSSL_NO_ASM) && \ |
David Benjamin | ec978dd | 2016-11-04 18:59:33 -0400 | [diff] [blame] | 69 | !defined(BORINGSSL_UNSAFE_DETERMINISTIC_MODE) |
Adam Langley | d755456 | 2015-09-24 15:03:14 -0700 | [diff] [blame] | 70 | |
| 71 | /* These functions are defined in asm/rdrand-x86_64.pl */ |
| 72 | extern int CRYPTO_rdrand(uint8_t out[8]); |
| 73 | extern int CRYPTO_rdrand_multiple8_buf(uint8_t *buf, size_t len); |
| 74 | |
| 75 | static int have_rdrand(void) { |
| 76 | return (OPENSSL_ia32cap_P[1] & (1u << 30)) != 0; |
| 77 | } |
| 78 | |
| 79 | static int hwrand(uint8_t *buf, size_t len) { |
| 80 | if (!have_rdrand()) { |
| 81 | return 0; |
| 82 | } |
| 83 | |
| 84 | const size_t len_multiple8 = len & ~7; |
| 85 | if (!CRYPTO_rdrand_multiple8_buf(buf, len_multiple8)) { |
| 86 | return 0; |
| 87 | } |
| 88 | len -= len_multiple8; |
| 89 | |
| 90 | if (len != 0) { |
| 91 | assert(len < 8); |
| 92 | |
| 93 | uint8_t rand_buf[8]; |
| 94 | if (!CRYPTO_rdrand(rand_buf)) { |
| 95 | return 0; |
| 96 | } |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 97 | OPENSSL_memcpy(buf + len_multiple8, rand_buf, len); |
Adam Langley | d755456 | 2015-09-24 15:03:14 -0700 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | return 1; |
| 101 | } |
| 102 | |
| 103 | #else |
| 104 | |
| 105 | static int hwrand(uint8_t *buf, size_t len) { |
| 106 | return 0; |
| 107 | } |
| 108 | |
| 109 | #endif |
| 110 | |
Adam Langley | 88bb848 | 2017-04-10 16:53:20 -0700 | [diff] [blame^] | 111 | #if defined(BORINGSSL_FIPS) |
| 112 | |
| 113 | static void rand_get_seed(uint8_t seed[CTR_DRBG_ENTROPY_LEN]) { |
| 114 | /* We overread from /dev/urandom by a factor of 10 and XOR to whiten. */ |
| 115 | #define FIPS_OVERREAD 10 |
| 116 | uint8_t entropy[CTR_DRBG_ENTROPY_LEN * FIPS_OVERREAD]; |
| 117 | CRYPTO_sysrand(entropy, sizeof(entropy)); |
| 118 | |
| 119 | OPENSSL_memcpy(seed, entropy, CTR_DRBG_ENTROPY_LEN); |
| 120 | |
| 121 | for (size_t i = 1; i < FIPS_OVERREAD; i++) { |
| 122 | for (size_t j = 0; j < CTR_DRBG_ENTROPY_LEN; j++) { |
| 123 | seed[j] ^= entropy[CTR_DRBG_ENTROPY_LEN * i + j]; |
| 124 | } |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | #else |
| 129 | |
| 130 | static void rand_get_seed(uint8_t seed[CTR_DRBG_ENTROPY_LEN]) { |
| 131 | /* If not in FIPS mode, we don't overread from the system entropy source. */ |
| 132 | CRYPTO_sysrand(seed, CTR_DRBG_ENTROPY_LEN); |
| 133 | } |
| 134 | |
| 135 | #endif |
| 136 | |
| 137 | void RAND_bytes_with_additional_data(uint8_t *out, size_t out_len, |
| 138 | const uint8_t user_additional_data[32]) { |
| 139 | if (out_len == 0) { |
| 140 | return; |
Adam Langley | 310d4dd | 2015-04-13 11:04:21 -0700 | [diff] [blame] | 141 | } |
| 142 | |
Adam Langley | 88bb848 | 2017-04-10 16:53:20 -0700 | [diff] [blame^] | 143 | struct rand_thread_state stack_state; |
Adam Langley | 310d4dd | 2015-04-13 11:04:21 -0700 | [diff] [blame] | 144 | struct rand_thread_state *state = |
| 145 | CRYPTO_get_thread_local(OPENSSL_THREAD_LOCAL_RAND); |
Adam Langley | 88bb848 | 2017-04-10 16:53:20 -0700 | [diff] [blame^] | 146 | |
Adam Langley | 310d4dd | 2015-04-13 11:04:21 -0700 | [diff] [blame] | 147 | if (state == NULL) { |
| 148 | state = OPENSSL_malloc(sizeof(struct rand_thread_state)); |
| 149 | if (state == NULL || |
| 150 | !CRYPTO_set_thread_local(OPENSSL_THREAD_LOCAL_RAND, state, |
| 151 | rand_thread_state_free)) { |
Adam Langley | 88bb848 | 2017-04-10 16:53:20 -0700 | [diff] [blame^] | 152 | /* If the system is out of memory, use an ephemeral state on the |
| 153 | * stack. */ |
| 154 | state = &stack_state; |
Adam Langley | 310d4dd | 2015-04-13 11:04:21 -0700 | [diff] [blame] | 155 | } |
| 156 | |
Adam Langley | 88bb848 | 2017-04-10 16:53:20 -0700 | [diff] [blame^] | 157 | uint8_t seed[CTR_DRBG_ENTROPY_LEN]; |
| 158 | rand_get_seed(seed); |
| 159 | if (!CTR_DRBG_init(&state->drbg, seed, NULL, 0)) { |
| 160 | abort(); |
| 161 | } |
| 162 | state->calls = 0; |
Adam Langley | 310d4dd | 2015-04-13 11:04:21 -0700 | [diff] [blame] | 163 | } |
| 164 | |
Adam Langley | 88bb848 | 2017-04-10 16:53:20 -0700 | [diff] [blame^] | 165 | if (state->calls >= kReseedInterval) { |
| 166 | uint8_t seed[CTR_DRBG_ENTROPY_LEN]; |
| 167 | rand_get_seed(seed); |
| 168 | if (!CTR_DRBG_reseed(&state->drbg, seed, NULL, 0)) { |
| 169 | abort(); |
| 170 | } |
| 171 | state->calls = 0; |
Adam Langley | 310d4dd | 2015-04-13 11:04:21 -0700 | [diff] [blame] | 172 | } |
| 173 | |
Adam Langley | 88bb848 | 2017-04-10 16:53:20 -0700 | [diff] [blame^] | 174 | /* Additional data is mixed into every CTR-DRBG call to protect, as best we |
| 175 | * can, against forks & VM clones. We do not over-read this information and |
| 176 | * don't reseed with it so, from the point of view of FIPS, this doesn't |
| 177 | * provide “prediction resistance”. But, in practice, it does. */ |
| 178 | uint8_t additional_data[32]; |
| 179 | if (!hwrand(additional_data, sizeof(additional_data))) { |
| 180 | /* Without a hardware RNG to save us from address-space duplication, the OS |
| 181 | * entropy is used. */ |
| 182 | CRYPTO_sysrand(additional_data, sizeof(additional_data)); |
Adam Langley | 310d4dd | 2015-04-13 11:04:21 -0700 | [diff] [blame] | 183 | } |
Adam Langley | 310d4dd | 2015-04-13 11:04:21 -0700 | [diff] [blame] | 184 | |
Adam Langley | 88bb848 | 2017-04-10 16:53:20 -0700 | [diff] [blame^] | 185 | for (size_t i = 0; i < sizeof(additional_data); i++) { |
| 186 | additional_data[i] ^= user_additional_data[i]; |
| 187 | } |
| 188 | |
| 189 | int first_call = 1; |
| 190 | while (out_len > 0) { |
| 191 | size_t todo = out_len; |
| 192 | if (todo > CTR_DRBG_MAX_GENERATE_LENGTH) { |
| 193 | todo = CTR_DRBG_MAX_GENERATE_LENGTH; |
| 194 | } |
| 195 | |
| 196 | if (!CTR_DRBG_generate(&state->drbg, out, todo, additional_data, |
| 197 | first_call ? sizeof(additional_data) : 0)) { |
| 198 | abort(); |
| 199 | } |
| 200 | |
| 201 | out += todo; |
| 202 | out_len -= todo; |
| 203 | state->calls++; |
| 204 | first_call = 0; |
| 205 | } |
| 206 | |
| 207 | if (state == &stack_state) { |
| 208 | CTR_DRBG_clear(&state->drbg); |
| 209 | } |
| 210 | |
| 211 | return; |
| 212 | } |
| 213 | |
| 214 | int RAND_bytes(uint8_t *out, size_t out_len) { |
| 215 | static const uint8_t kZeroAdditionalData[32] = {0}; |
| 216 | RAND_bytes_with_additional_data(out, out_len, kZeroAdditionalData); |
Adam Langley | 310d4dd | 2015-04-13 11:04:21 -0700 | [diff] [blame] | 217 | return 1; |
| 218 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 219 | |
| 220 | int RAND_pseudo_bytes(uint8_t *buf, size_t len) { |
| 221 | return RAND_bytes(buf, len); |
| 222 | } |
| 223 | |
Adam Langley | c5c85de | 2015-11-16 10:10:59 -0800 | [diff] [blame] | 224 | void RAND_seed(const void *buf, int num) { |
| 225 | /* OpenSSH calls |RAND_seed| before jailing on the assumption that any needed |
| 226 | * file descriptors etc will be opened. */ |
| 227 | uint8_t unused; |
| 228 | RAND_bytes(&unused, sizeof(unused)); |
| 229 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 230 | |
Matt Braithwaite | af3d5bd | 2015-04-24 17:40:19 -0700 | [diff] [blame] | 231 | int RAND_load_file(const char *path, long num) { |
| 232 | if (num < 0) { /* read the "whole file" */ |
| 233 | return 1; |
| 234 | } else if (num <= INT_MAX) { |
| 235 | return (int) num; |
| 236 | } else { |
| 237 | return INT_MAX; |
| 238 | } |
| 239 | } |
| 240 | |
David Benjamin | e5aa791 | 2016-01-26 01:09:19 -0500 | [diff] [blame] | 241 | const char *RAND_file_name(char *buf, size_t num) { return NULL; } |
| 242 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 243 | void RAND_add(const void *buf, int num, double entropy) {} |
| 244 | |
Matt Braithwaite | 3e5e99d | 2015-06-24 16:41:10 -0700 | [diff] [blame] | 245 | int RAND_egd(const char *path) { |
| 246 | return 255; |
| 247 | } |
| 248 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 249 | int RAND_poll(void) { |
| 250 | return 1; |
| 251 | } |
Adam Langley | c3ef76f | 2015-04-13 14:34:17 -0700 | [diff] [blame] | 252 | |
| 253 | int RAND_status(void) { |
| 254 | return 1; |
| 255 | } |
Matt Braithwaite | 3e5e99d | 2015-06-24 16:41:10 -0700 | [diff] [blame] | 256 | |
Matt Braithwaite | bc97c69 | 2015-07-22 18:16:18 -0700 | [diff] [blame] | 257 | static const struct rand_meth_st kSSLeayMethod = { |
| 258 | RAND_seed, |
| 259 | RAND_bytes, |
| 260 | RAND_cleanup, |
| 261 | RAND_add, |
| 262 | RAND_pseudo_bytes, |
| 263 | RAND_status, |
| 264 | }; |
Matt Braithwaite | 3e5e99d | 2015-06-24 16:41:10 -0700 | [diff] [blame] | 265 | |
| 266 | RAND_METHOD *RAND_SSLeay(void) { |
| 267 | return (RAND_METHOD*) &kSSLeayMethod; |
| 268 | } |
| 269 | |
Adam Langley | a59347e | 2015-06-24 17:02:15 -0700 | [diff] [blame] | 270 | void RAND_set_rand_method(const RAND_METHOD *method) {} |
Alessandro Ghedini | 0d099f0 | 2016-07-10 01:57:44 +0100 | [diff] [blame] | 271 | |
| 272 | void RAND_cleanup(void) {} |