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 | |
Adam Langley | d755456 | 2015-09-24 15:03:14 -0700 | [diff] [blame^] | 75 | #if defined(OPENSSL_X86_64) && !defined(OPENSSL_NO_ASM) |
| 76 | |
| 77 | /* These functions are defined in asm/rdrand-x86_64.pl */ |
| 78 | extern int CRYPTO_rdrand(uint8_t out[8]); |
| 79 | extern int CRYPTO_rdrand_multiple8_buf(uint8_t *buf, size_t len); |
| 80 | |
| 81 | static int have_rdrand(void) { |
| 82 | return (OPENSSL_ia32cap_P[1] & (1u << 30)) != 0; |
| 83 | } |
| 84 | |
| 85 | static int hwrand(uint8_t *buf, size_t len) { |
| 86 | if (!have_rdrand()) { |
| 87 | return 0; |
| 88 | } |
| 89 | |
| 90 | const size_t len_multiple8 = len & ~7; |
| 91 | if (!CRYPTO_rdrand_multiple8_buf(buf, len_multiple8)) { |
| 92 | return 0; |
| 93 | } |
| 94 | len -= len_multiple8; |
| 95 | |
| 96 | if (len != 0) { |
| 97 | assert(len < 8); |
| 98 | |
| 99 | uint8_t rand_buf[8]; |
| 100 | if (!CRYPTO_rdrand(rand_buf)) { |
| 101 | return 0; |
| 102 | } |
| 103 | memcpy(buf + len_multiple8, rand_buf, len); |
| 104 | } |
| 105 | |
| 106 | return 1; |
| 107 | } |
| 108 | |
| 109 | #else |
| 110 | |
| 111 | static int hwrand(uint8_t *buf, size_t len) { |
| 112 | return 0; |
| 113 | } |
| 114 | |
| 115 | #endif |
| 116 | |
Brian Smith | 3700778 | 2015-04-14 16:21:27 -1000 | [diff] [blame] | 117 | int RAND_bytes(uint8_t *buf, size_t len) { |
Adam Langley | 310d4dd | 2015-04-13 11:04:21 -0700 | [diff] [blame] | 118 | if (len == 0) { |
| 119 | return 1; |
| 120 | } |
| 121 | |
Adam Langley | d755456 | 2015-09-24 15:03:14 -0700 | [diff] [blame^] | 122 | if (!hwrand(buf, len)) { |
Adam Langley | 310d4dd | 2015-04-13 11:04:21 -0700 | [diff] [blame] | 123 | /* Without a hardware RNG to save us from address-space duplication, the OS |
| 124 | * entropy is used directly. */ |
| 125 | CRYPTO_sysrand(buf, len); |
| 126 | return 1; |
| 127 | } |
| 128 | |
| 129 | struct rand_thread_state *state = |
| 130 | CRYPTO_get_thread_local(OPENSSL_THREAD_LOCAL_RAND); |
| 131 | if (state == NULL) { |
| 132 | state = OPENSSL_malloc(sizeof(struct rand_thread_state)); |
| 133 | if (state == NULL || |
| 134 | !CRYPTO_set_thread_local(OPENSSL_THREAD_LOCAL_RAND, state, |
| 135 | rand_thread_state_free)) { |
| 136 | CRYPTO_sysrand(buf, len); |
| 137 | return 1; |
| 138 | } |
| 139 | |
David Benjamin | 81091d5 | 2015-05-15 15:50:22 -0400 | [diff] [blame] | 140 | memset(state->partial_block, 0, sizeof(state->partial_block)); |
Adam Langley | 310d4dd | 2015-04-13 11:04:21 -0700 | [diff] [blame] | 141 | state->calls_used = kMaxCallsPerRefresh; |
| 142 | } |
| 143 | |
| 144 | if (state->calls_used >= kMaxCallsPerRefresh || |
| 145 | state->bytes_used >= kMaxBytesPerRefresh) { |
| 146 | CRYPTO_sysrand(state->key, sizeof(state->key)); |
| 147 | state->calls_used = 0; |
| 148 | state->bytes_used = 0; |
| 149 | state->partial_block_used = sizeof(state->partial_block); |
| 150 | } |
| 151 | |
Adam Langley | 310d4dd | 2015-04-13 11:04:21 -0700 | [diff] [blame] | 152 | if (len >= sizeof(state->partial_block)) { |
| 153 | size_t remaining = len; |
| 154 | while (remaining > 0) { |
| 155 | // kMaxBytesPerCall is only 2GB, while ChaCha can handle 256GB. But this |
| 156 | // is sufficient and easier on 32-bit. |
| 157 | static const size_t kMaxBytesPerCall = 0x80000000; |
| 158 | size_t todo = remaining; |
| 159 | if (todo > kMaxBytesPerCall) { |
| 160 | todo = kMaxBytesPerCall; |
| 161 | } |
| 162 | CRYPTO_chacha_20(buf, buf, todo, state->key, |
| 163 | (uint8_t *)&state->calls_used, 0); |
| 164 | buf += todo; |
| 165 | remaining -= todo; |
| 166 | state->calls_used++; |
| 167 | } |
| 168 | } else { |
| 169 | if (sizeof(state->partial_block) - state->partial_block_used < len) { |
| 170 | CRYPTO_chacha_20(state->partial_block, state->partial_block, |
| 171 | sizeof(state->partial_block), state->key, |
| 172 | (uint8_t *)&state->calls_used, 0); |
| 173 | state->partial_block_used = 0; |
| 174 | } |
| 175 | |
| 176 | unsigned i; |
| 177 | for (i = 0; i < len; i++) { |
| 178 | buf[i] ^= state->partial_block[state->partial_block_used++]; |
| 179 | } |
| 180 | state->calls_used++; |
| 181 | } |
| 182 | state->bytes_used += len; |
| 183 | |
| 184 | return 1; |
| 185 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 186 | |
| 187 | int RAND_pseudo_bytes(uint8_t *buf, size_t len) { |
| 188 | return RAND_bytes(buf, len); |
| 189 | } |
| 190 | |
| 191 | void RAND_seed(const void *buf, int num) {} |
| 192 | |
Matt Braithwaite | af3d5bd | 2015-04-24 17:40:19 -0700 | [diff] [blame] | 193 | int RAND_load_file(const char *path, long num) { |
| 194 | if (num < 0) { /* read the "whole file" */ |
| 195 | return 1; |
| 196 | } else if (num <= INT_MAX) { |
| 197 | return (int) num; |
| 198 | } else { |
| 199 | return INT_MAX; |
| 200 | } |
| 201 | } |
| 202 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 203 | void RAND_add(const void *buf, int num, double entropy) {} |
| 204 | |
Matt Braithwaite | 3e5e99d | 2015-06-24 16:41:10 -0700 | [diff] [blame] | 205 | int RAND_egd(const char *path) { |
| 206 | return 255; |
| 207 | } |
| 208 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 209 | int RAND_poll(void) { |
| 210 | return 1; |
| 211 | } |
Adam Langley | c3ef76f | 2015-04-13 14:34:17 -0700 | [diff] [blame] | 212 | |
| 213 | int RAND_status(void) { |
| 214 | return 1; |
| 215 | } |
Matt Braithwaite | 3e5e99d | 2015-06-24 16:41:10 -0700 | [diff] [blame] | 216 | |
Matt Braithwaite | bc97c69 | 2015-07-22 18:16:18 -0700 | [diff] [blame] | 217 | static const struct rand_meth_st kSSLeayMethod = { |
| 218 | RAND_seed, |
| 219 | RAND_bytes, |
| 220 | RAND_cleanup, |
| 221 | RAND_add, |
| 222 | RAND_pseudo_bytes, |
| 223 | RAND_status, |
| 224 | }; |
Matt Braithwaite | 3e5e99d | 2015-06-24 16:41:10 -0700 | [diff] [blame] | 225 | |
| 226 | RAND_METHOD *RAND_SSLeay(void) { |
| 227 | return (RAND_METHOD*) &kSSLeayMethod; |
| 228 | } |
| 229 | |
Adam Langley | a59347e | 2015-06-24 17:02:15 -0700 | [diff] [blame] | 230 | void RAND_set_rand_method(const RAND_METHOD *method) {} |