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" |
Adam Langley | 7784104 | 2017-04-14 11:16:20 -0700 | [diff] [blame] | 26 | #include "../../internal.h" |
Adam Langley | 310d4dd | 2015-04-13 11:04:21 -0700 | [diff] [blame] | 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 | |
Adam Langley | 90ada2f | 2017-04-11 16:19:27 -0700 | [diff] [blame] | 48 | /* CRNGT_BLOCK_SIZE is the number of bytes in a “block” for the purposes of the |
| 49 | * continuous random number generator test in FIPS 140-2, section 4.9.2. */ |
| 50 | #define CRNGT_BLOCK_SIZE 16 |
| 51 | |
Adam Langley | 88bb848 | 2017-04-10 16:53:20 -0700 | [diff] [blame] | 52 | /* rand_thread_state contains the per-thread state for the RNG. */ |
Adam Langley | 310d4dd | 2015-04-13 11:04:21 -0700 | [diff] [blame] | 53 | struct rand_thread_state { |
Adam Langley | 88bb848 | 2017-04-10 16:53:20 -0700 | [diff] [blame] | 54 | CTR_DRBG_STATE drbg; |
| 55 | /* calls is the number of generate calls made on |drbg| since it was last |
| 56 | * (re)seeded. This is bound by |kReseedInterval|. */ |
| 57 | unsigned calls; |
Adam Langley | 90ada2f | 2017-04-11 16:19:27 -0700 | [diff] [blame] | 58 | /* last_block contains the previous block from |CRYPTO_sysrand|. */ |
| 59 | uint8_t last_block[CRNGT_BLOCK_SIZE]; |
| 60 | /* last_block_valid is non-zero iff |last_block| contains data from |
| 61 | * |CRYPTO_sysrand|. */ |
| 62 | int last_block_valid; |
Adam Langley | 310d4dd | 2015-04-13 11:04:21 -0700 | [diff] [blame] | 63 | }; |
| 64 | |
Adam Langley | 310d4dd | 2015-04-13 11:04:21 -0700 | [diff] [blame] | 65 | /* rand_thread_state_free frees a |rand_thread_state|. This is called when a |
| 66 | * thread exits. */ |
Adam Langley | 88bb848 | 2017-04-10 16:53:20 -0700 | [diff] [blame] | 67 | static void rand_thread_state_free(void *state_in) { |
| 68 | if (state_in == NULL) { |
Adam Langley | 310d4dd | 2015-04-13 11:04:21 -0700 | [diff] [blame] | 69 | return; |
| 70 | } |
| 71 | |
Adam Langley | 88bb848 | 2017-04-10 16:53:20 -0700 | [diff] [blame] | 72 | struct rand_thread_state *state = state_in; |
| 73 | CTR_DRBG_clear(&state->drbg); |
Adam Langley | 310d4dd | 2015-04-13 11:04:21 -0700 | [diff] [blame] | 74 | OPENSSL_free(state); |
| 75 | } |
| 76 | |
David Benjamin | bc5b2a2 | 2016-03-01 22:57:32 -0500 | [diff] [blame] | 77 | #if defined(OPENSSL_X86_64) && !defined(OPENSSL_NO_ASM) && \ |
David Benjamin | ec978dd | 2016-11-04 18:59:33 -0400 | [diff] [blame] | 78 | !defined(BORINGSSL_UNSAFE_DETERMINISTIC_MODE) |
Adam Langley | d755456 | 2015-09-24 15:03:14 -0700 | [diff] [blame] | 79 | |
| 80 | /* These functions are defined in asm/rdrand-x86_64.pl */ |
| 81 | extern int CRYPTO_rdrand(uint8_t out[8]); |
| 82 | extern int CRYPTO_rdrand_multiple8_buf(uint8_t *buf, size_t len); |
| 83 | |
| 84 | static int have_rdrand(void) { |
David Benjamin | 9187101 | 2017-04-25 15:37:53 -0400 | [diff] [blame^] | 85 | return (OPENSSL_ia32cap_get()[1] & (1u << 30)) != 0; |
Adam Langley | d755456 | 2015-09-24 15:03:14 -0700 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | static int hwrand(uint8_t *buf, size_t len) { |
| 89 | if (!have_rdrand()) { |
| 90 | return 0; |
| 91 | } |
| 92 | |
| 93 | const size_t len_multiple8 = len & ~7; |
| 94 | if (!CRYPTO_rdrand_multiple8_buf(buf, len_multiple8)) { |
| 95 | return 0; |
| 96 | } |
| 97 | len -= len_multiple8; |
| 98 | |
| 99 | if (len != 0) { |
| 100 | assert(len < 8); |
| 101 | |
| 102 | uint8_t rand_buf[8]; |
| 103 | if (!CRYPTO_rdrand(rand_buf)) { |
| 104 | return 0; |
| 105 | } |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 106 | OPENSSL_memcpy(buf + len_multiple8, rand_buf, len); |
Adam Langley | d755456 | 2015-09-24 15:03:14 -0700 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | return 1; |
| 110 | } |
| 111 | |
| 112 | #else |
| 113 | |
| 114 | static int hwrand(uint8_t *buf, size_t len) { |
| 115 | return 0; |
| 116 | } |
| 117 | |
| 118 | #endif |
| 119 | |
Adam Langley | 88bb848 | 2017-04-10 16:53:20 -0700 | [diff] [blame] | 120 | #if defined(BORINGSSL_FIPS) |
| 121 | |
Adam Langley | 90ada2f | 2017-04-11 16:19:27 -0700 | [diff] [blame] | 122 | static void rand_get_seed(struct rand_thread_state *state, |
| 123 | uint8_t seed[CTR_DRBG_ENTROPY_LEN]) { |
| 124 | if (!state->last_block_valid) { |
| 125 | CRYPTO_sysrand(state->last_block, sizeof(state->last_block)); |
| 126 | state->last_block_valid = 1; |
| 127 | } |
| 128 | |
Adam Langley | 88bb848 | 2017-04-10 16:53:20 -0700 | [diff] [blame] | 129 | /* We overread from /dev/urandom by a factor of 10 and XOR to whiten. */ |
| 130 | #define FIPS_OVERREAD 10 |
| 131 | uint8_t entropy[CTR_DRBG_ENTROPY_LEN * FIPS_OVERREAD]; |
| 132 | CRYPTO_sysrand(entropy, sizeof(entropy)); |
| 133 | |
Adam Langley | 90ada2f | 2017-04-11 16:19:27 -0700 | [diff] [blame] | 134 | /* See FIPS 140-2, section 4.9.2. This is the “continuous random number |
| 135 | * generator test” which causes the program to randomly abort. Hopefully the |
| 136 | * rate of failure is small enough not to be a problem in practice. */ |
| 137 | if (CRYPTO_memcmp(state->last_block, entropy, CRNGT_BLOCK_SIZE) == 0) { |
| 138 | abort(); |
| 139 | } |
| 140 | |
| 141 | for (size_t i = CRNGT_BLOCK_SIZE; i < sizeof(entropy); |
| 142 | i += CRNGT_BLOCK_SIZE) { |
| 143 | if (CRYPTO_memcmp(entropy + i - CRNGT_BLOCK_SIZE, entropy + i, |
| 144 | CRNGT_BLOCK_SIZE) == 0) { |
| 145 | abort(); |
| 146 | } |
| 147 | } |
| 148 | OPENSSL_memcpy(state->last_block, |
| 149 | entropy + sizeof(entropy) - CRNGT_BLOCK_SIZE, |
| 150 | CRNGT_BLOCK_SIZE); |
| 151 | |
Adam Langley | 88bb848 | 2017-04-10 16:53:20 -0700 | [diff] [blame] | 152 | OPENSSL_memcpy(seed, entropy, CTR_DRBG_ENTROPY_LEN); |
| 153 | |
| 154 | for (size_t i = 1; i < FIPS_OVERREAD; i++) { |
| 155 | for (size_t j = 0; j < CTR_DRBG_ENTROPY_LEN; j++) { |
| 156 | seed[j] ^= entropy[CTR_DRBG_ENTROPY_LEN * i + j]; |
| 157 | } |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | #else |
| 162 | |
Adam Langley | 90ada2f | 2017-04-11 16:19:27 -0700 | [diff] [blame] | 163 | static void rand_get_seed(struct rand_thread_state *state, |
| 164 | uint8_t seed[CTR_DRBG_ENTROPY_LEN]) { |
Adam Langley | 88bb848 | 2017-04-10 16:53:20 -0700 | [diff] [blame] | 165 | /* If not in FIPS mode, we don't overread from the system entropy source. */ |
| 166 | CRYPTO_sysrand(seed, CTR_DRBG_ENTROPY_LEN); |
| 167 | } |
| 168 | |
| 169 | #endif |
| 170 | |
| 171 | void RAND_bytes_with_additional_data(uint8_t *out, size_t out_len, |
| 172 | const uint8_t user_additional_data[32]) { |
| 173 | if (out_len == 0) { |
| 174 | return; |
Adam Langley | 310d4dd | 2015-04-13 11:04:21 -0700 | [diff] [blame] | 175 | } |
| 176 | |
Adam Langley | 88bb848 | 2017-04-10 16:53:20 -0700 | [diff] [blame] | 177 | struct rand_thread_state stack_state; |
Adam Langley | 310d4dd | 2015-04-13 11:04:21 -0700 | [diff] [blame] | 178 | struct rand_thread_state *state = |
| 179 | CRYPTO_get_thread_local(OPENSSL_THREAD_LOCAL_RAND); |
Adam Langley | 88bb848 | 2017-04-10 16:53:20 -0700 | [diff] [blame] | 180 | |
Adam Langley | 310d4dd | 2015-04-13 11:04:21 -0700 | [diff] [blame] | 181 | if (state == NULL) { |
| 182 | state = OPENSSL_malloc(sizeof(struct rand_thread_state)); |
| 183 | if (state == NULL || |
| 184 | !CRYPTO_set_thread_local(OPENSSL_THREAD_LOCAL_RAND, state, |
| 185 | rand_thread_state_free)) { |
Adam Langley | 88bb848 | 2017-04-10 16:53:20 -0700 | [diff] [blame] | 186 | /* If the system is out of memory, use an ephemeral state on the |
| 187 | * stack. */ |
| 188 | state = &stack_state; |
Adam Langley | 310d4dd | 2015-04-13 11:04:21 -0700 | [diff] [blame] | 189 | } |
| 190 | |
Adam Langley | 90ada2f | 2017-04-11 16:19:27 -0700 | [diff] [blame] | 191 | state->last_block_valid = 0; |
Adam Langley | 88bb848 | 2017-04-10 16:53:20 -0700 | [diff] [blame] | 192 | uint8_t seed[CTR_DRBG_ENTROPY_LEN]; |
Adam Langley | 90ada2f | 2017-04-11 16:19:27 -0700 | [diff] [blame] | 193 | rand_get_seed(state, seed); |
Adam Langley | 88bb848 | 2017-04-10 16:53:20 -0700 | [diff] [blame] | 194 | if (!CTR_DRBG_init(&state->drbg, seed, NULL, 0)) { |
| 195 | abort(); |
| 196 | } |
| 197 | state->calls = 0; |
Adam Langley | 310d4dd | 2015-04-13 11:04:21 -0700 | [diff] [blame] | 198 | } |
| 199 | |
Adam Langley | 88bb848 | 2017-04-10 16:53:20 -0700 | [diff] [blame] | 200 | if (state->calls >= kReseedInterval) { |
| 201 | uint8_t seed[CTR_DRBG_ENTROPY_LEN]; |
Adam Langley | 90ada2f | 2017-04-11 16:19:27 -0700 | [diff] [blame] | 202 | rand_get_seed(state, seed); |
Adam Langley | 88bb848 | 2017-04-10 16:53:20 -0700 | [diff] [blame] | 203 | if (!CTR_DRBG_reseed(&state->drbg, seed, NULL, 0)) { |
| 204 | abort(); |
| 205 | } |
| 206 | state->calls = 0; |
Adam Langley | 310d4dd | 2015-04-13 11:04:21 -0700 | [diff] [blame] | 207 | } |
| 208 | |
Adam Langley | 88bb848 | 2017-04-10 16:53:20 -0700 | [diff] [blame] | 209 | /* Additional data is mixed into every CTR-DRBG call to protect, as best we |
| 210 | * can, against forks & VM clones. We do not over-read this information and |
| 211 | * don't reseed with it so, from the point of view of FIPS, this doesn't |
| 212 | * provide “prediction resistance”. But, in practice, it does. */ |
| 213 | uint8_t additional_data[32]; |
| 214 | if (!hwrand(additional_data, sizeof(additional_data))) { |
| 215 | /* Without a hardware RNG to save us from address-space duplication, the OS |
Adam Langley | 92f888e | 2017-04-11 15:06:47 -0700 | [diff] [blame] | 216 | * entropy is used. This can be expensive (one read per |RAND_bytes| call) |
| 217 | * and so can be disabled by applications that we have ensured don't fork |
| 218 | * and aren't at risk of VM cloning. */ |
| 219 | if (!rand_fork_unsafe_buffering_enabled()) { |
| 220 | CRYPTO_sysrand(additional_data, sizeof(additional_data)); |
| 221 | } else { |
| 222 | OPENSSL_memset(additional_data, 0, sizeof(additional_data)); |
| 223 | } |
Adam Langley | 310d4dd | 2015-04-13 11:04:21 -0700 | [diff] [blame] | 224 | } |
Adam Langley | 310d4dd | 2015-04-13 11:04:21 -0700 | [diff] [blame] | 225 | |
Adam Langley | 88bb848 | 2017-04-10 16:53:20 -0700 | [diff] [blame] | 226 | for (size_t i = 0; i < sizeof(additional_data); i++) { |
| 227 | additional_data[i] ^= user_additional_data[i]; |
| 228 | } |
| 229 | |
| 230 | int first_call = 1; |
| 231 | while (out_len > 0) { |
| 232 | size_t todo = out_len; |
| 233 | if (todo > CTR_DRBG_MAX_GENERATE_LENGTH) { |
| 234 | todo = CTR_DRBG_MAX_GENERATE_LENGTH; |
| 235 | } |
| 236 | |
| 237 | if (!CTR_DRBG_generate(&state->drbg, out, todo, additional_data, |
| 238 | first_call ? sizeof(additional_data) : 0)) { |
| 239 | abort(); |
| 240 | } |
| 241 | |
| 242 | out += todo; |
| 243 | out_len -= todo; |
| 244 | state->calls++; |
| 245 | first_call = 0; |
| 246 | } |
| 247 | |
| 248 | if (state == &stack_state) { |
| 249 | CTR_DRBG_clear(&state->drbg); |
| 250 | } |
| 251 | |
| 252 | return; |
| 253 | } |
| 254 | |
| 255 | int RAND_bytes(uint8_t *out, size_t out_len) { |
| 256 | static const uint8_t kZeroAdditionalData[32] = {0}; |
| 257 | RAND_bytes_with_additional_data(out, out_len, kZeroAdditionalData); |
Adam Langley | 310d4dd | 2015-04-13 11:04:21 -0700 | [diff] [blame] | 258 | return 1; |
| 259 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 260 | |
| 261 | int RAND_pseudo_bytes(uint8_t *buf, size_t len) { |
| 262 | return RAND_bytes(buf, len); |
| 263 | } |