David Benjamin | 31a0779 | 2015-03-03 14:20:26 -0500 | [diff] [blame] | 1 | /* Copyright (c) 2015, 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 | |
David Benjamin | 9e4e01e | 2015-09-15 01:48:04 -0400 | [diff] [blame] | 15 | #include <openssl/ssl.h> |
| 16 | |
David Benjamin | 31a0779 | 2015-03-03 14:20:26 -0500 | [diff] [blame] | 17 | #include <assert.h> |
| 18 | #include <string.h> |
| 19 | |
| 20 | #include <openssl/aead.h> |
| 21 | #include <openssl/err.h> |
| 22 | #include <openssl/rand.h> |
David Benjamin | 31a0779 | 2015-03-03 14:20:26 -0500 | [diff] [blame] | 23 | |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 24 | #include "../crypto/internal.h" |
David Benjamin | 31a0779 | 2015-03-03 14:20:26 -0500 | [diff] [blame] | 25 | #include "internal.h" |
| 26 | |
| 27 | |
David Benjamin | cfc11c2 | 2017-07-18 22:45:18 -0400 | [diff] [blame] | 28 | #if defined(BORINGSSL_UNSAFE_FUZZER_MODE) |
| 29 | #define FUZZER_MODE true |
| 30 | #else |
| 31 | #define FUZZER_MODE false |
| 32 | #endif |
| 33 | |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 34 | namespace bssl { |
| 35 | |
David Benjamin | cfc11c2 | 2017-07-18 22:45:18 -0400 | [diff] [blame] | 36 | SSLAEADContext::SSLAEADContext(uint16_t version_arg, |
| 37 | const SSL_CIPHER *cipher_arg) |
| 38 | : cipher_(cipher_arg), |
| 39 | version_(version_arg), |
| 40 | variable_nonce_included_in_record_(false), |
| 41 | random_variable_nonce_(false), |
| 42 | omit_length_in_ad_(false), |
| 43 | omit_version_in_ad_(false), |
| 44 | omit_ad_(false), |
| 45 | xor_fixed_nonce_(false) { |
| 46 | OPENSSL_memset(fixed_nonce_, 0, sizeof(fixed_nonce_)); |
| 47 | } |
| 48 | |
| 49 | SSLAEADContext::~SSLAEADContext() {} |
| 50 | |
| 51 | UniquePtr<SSLAEADContext> SSLAEADContext::CreateNullCipher() { |
| 52 | return MakeUnique<SSLAEADContext>(0 /* version */, nullptr /* cipher */); |
| 53 | } |
| 54 | |
| 55 | UniquePtr<SSLAEADContext> SSLAEADContext::Create( |
| 56 | enum evp_aead_direction_t direction, uint16_t version, int is_dtls, |
| 57 | const SSL_CIPHER *cipher, const uint8_t *enc_key, size_t enc_key_len, |
| 58 | const uint8_t *mac_key, size_t mac_key_len, const uint8_t *fixed_iv, |
| 59 | size_t fixed_iv_len) { |
David Benjamin | 31a0779 | 2015-03-03 14:20:26 -0500 | [diff] [blame] | 60 | const EVP_AEAD *aead; |
David Benjamin | 4b0d0e4 | 2016-10-28 17:17:14 -0400 | [diff] [blame] | 61 | size_t expected_mac_key_len, expected_fixed_iv_len; |
| 62 | if (!ssl_cipher_get_evp_aead(&aead, &expected_mac_key_len, |
Steven Valdez | 2f3404b | 2017-05-24 16:54:35 -0400 | [diff] [blame] | 63 | &expected_fixed_iv_len, cipher, version, |
| 64 | is_dtls) || |
David Benjamin | 4b0d0e4 | 2016-10-28 17:17:14 -0400 | [diff] [blame] | 65 | /* Ensure the caller returned correct key sizes. */ |
| 66 | expected_fixed_iv_len != fixed_iv_len || |
| 67 | expected_mac_key_len != mac_key_len) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 68 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
David Benjamin | cfc11c2 | 2017-07-18 22:45:18 -0400 | [diff] [blame] | 69 | return nullptr; |
David Benjamin | 31a0779 | 2015-03-03 14:20:26 -0500 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | uint8_t merged_key[EVP_AEAD_MAX_KEY_LENGTH]; |
| 73 | if (mac_key_len > 0) { |
| 74 | /* This is a "stateful" AEAD (for compatibility with pre-AEAD cipher |
| 75 | * suites). */ |
| 76 | if (mac_key_len + enc_key_len + fixed_iv_len > sizeof(merged_key)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 77 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
David Benjamin | cfc11c2 | 2017-07-18 22:45:18 -0400 | [diff] [blame] | 78 | return nullptr; |
David Benjamin | 31a0779 | 2015-03-03 14:20:26 -0500 | [diff] [blame] | 79 | } |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 80 | OPENSSL_memcpy(merged_key, mac_key, mac_key_len); |
| 81 | OPENSSL_memcpy(merged_key + mac_key_len, enc_key, enc_key_len); |
| 82 | OPENSSL_memcpy(merged_key + mac_key_len + enc_key_len, fixed_iv, |
| 83 | fixed_iv_len); |
David Benjamin | 31a0779 | 2015-03-03 14:20:26 -0500 | [diff] [blame] | 84 | enc_key = merged_key; |
| 85 | enc_key_len += mac_key_len; |
| 86 | enc_key_len += fixed_iv_len; |
| 87 | } |
| 88 | |
David Benjamin | cfc11c2 | 2017-07-18 22:45:18 -0400 | [diff] [blame] | 89 | UniquePtr<SSLAEADContext> aead_ctx = |
| 90 | MakeUnique<SSLAEADContext>(version, cipher); |
| 91 | if (!aead_ctx) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 92 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
David Benjamin | cfc11c2 | 2017-07-18 22:45:18 -0400 | [diff] [blame] | 93 | return nullptr; |
David Benjamin | 31a0779 | 2015-03-03 14:20:26 -0500 | [diff] [blame] | 94 | } |
David Benjamin | 31a0779 | 2015-03-03 14:20:26 -0500 | [diff] [blame] | 95 | |
| 96 | if (!EVP_AEAD_CTX_init_with_direction( |
David Benjamin | cfc11c2 | 2017-07-18 22:45:18 -0400 | [diff] [blame] | 97 | aead_ctx->ctx_.get(), aead, enc_key, enc_key_len, |
David Benjamin | 31a0779 | 2015-03-03 14:20:26 -0500 | [diff] [blame] | 98 | EVP_AEAD_DEFAULT_TAG_LENGTH, direction)) { |
David Benjamin | cfc11c2 | 2017-07-18 22:45:18 -0400 | [diff] [blame] | 99 | return nullptr; |
David Benjamin | 31a0779 | 2015-03-03 14:20:26 -0500 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | assert(EVP_AEAD_nonce_length(aead) <= EVP_AEAD_MAX_NONCE_LENGTH); |
David Benjamin | a3d76d0 | 2017-07-14 19:36:07 -0400 | [diff] [blame] | 103 | static_assert(EVP_AEAD_MAX_NONCE_LENGTH < 256, |
| 104 | "variable_nonce_len doesn't fit in uint8_t"); |
David Benjamin | cfc11c2 | 2017-07-18 22:45:18 -0400 | [diff] [blame] | 105 | aead_ctx->variable_nonce_len_ = (uint8_t)EVP_AEAD_nonce_length(aead); |
David Benjamin | 51a01a5 | 2015-10-29 13:19:56 -0400 | [diff] [blame] | 106 | if (mac_key_len == 0) { |
David Benjamin | cfc11c2 | 2017-07-18 22:45:18 -0400 | [diff] [blame] | 107 | assert(fixed_iv_len <= sizeof(aead_ctx->fixed_nonce_)); |
| 108 | OPENSSL_memcpy(aead_ctx->fixed_nonce_, fixed_iv, fixed_iv_len); |
| 109 | aead_ctx->fixed_nonce_len_ = fixed_iv_len; |
David Benjamin | 13414b3 | 2015-12-09 23:02:39 -0500 | [diff] [blame] | 110 | |
| 111 | if (cipher->algorithm_enc & SSL_CHACHA20POLY1305) { |
| 112 | /* The fixed nonce into the actual nonce (the sequence number). */ |
David Benjamin | cfc11c2 | 2017-07-18 22:45:18 -0400 | [diff] [blame] | 113 | aead_ctx->xor_fixed_nonce_ = true; |
| 114 | aead_ctx->variable_nonce_len_ = 8; |
David Benjamin | 13414b3 | 2015-12-09 23:02:39 -0500 | [diff] [blame] | 115 | } else { |
| 116 | /* The fixed IV is prepended to the nonce. */ |
David Benjamin | cfc11c2 | 2017-07-18 22:45:18 -0400 | [diff] [blame] | 117 | assert(fixed_iv_len <= aead_ctx->variable_nonce_len_); |
| 118 | aead_ctx->variable_nonce_len_ -= fixed_iv_len; |
David Benjamin | 13414b3 | 2015-12-09 23:02:39 -0500 | [diff] [blame] | 119 | } |
| 120 | |
David Benjamin | b2a985b | 2015-06-21 15:13:57 -0400 | [diff] [blame] | 121 | /* AES-GCM uses an explicit nonce. */ |
| 122 | if (cipher->algorithm_enc & (SSL_AES128GCM | SSL_AES256GCM)) { |
David Benjamin | cfc11c2 | 2017-07-18 22:45:18 -0400 | [diff] [blame] | 123 | aead_ctx->variable_nonce_included_in_record_ = true; |
David Benjamin | b2a985b | 2015-06-21 15:13:57 -0400 | [diff] [blame] | 124 | } |
Steven Valdez | 494650c | 2016-05-24 12:43:04 -0400 | [diff] [blame] | 125 | |
| 126 | /* The TLS 1.3 construction XORs the fixed nonce into the sequence number |
| 127 | * and omits the additional data. */ |
| 128 | if (version >= TLS1_3_VERSION) { |
David Benjamin | cfc11c2 | 2017-07-18 22:45:18 -0400 | [diff] [blame] | 129 | aead_ctx->xor_fixed_nonce_ = true; |
| 130 | aead_ctx->variable_nonce_len_ = 8; |
| 131 | aead_ctx->variable_nonce_included_in_record_ = false; |
| 132 | aead_ctx->omit_ad_ = true; |
| 133 | assert(fixed_iv_len >= aead_ctx->variable_nonce_len_); |
Steven Valdez | 494650c | 2016-05-24 12:43:04 -0400 | [diff] [blame] | 134 | } |
David Benjamin | 31a0779 | 2015-03-03 14:20:26 -0500 | [diff] [blame] | 135 | } else { |
Steven Valdez | 7975056 | 2016-06-16 06:38:04 -0400 | [diff] [blame] | 136 | assert(version < TLS1_3_VERSION); |
David Benjamin | cfc11c2 | 2017-07-18 22:45:18 -0400 | [diff] [blame] | 137 | aead_ctx->variable_nonce_included_in_record_ = true; |
| 138 | aead_ctx->random_variable_nonce_ = true; |
| 139 | aead_ctx->omit_length_in_ad_ = true; |
| 140 | aead_ctx->omit_version_in_ad_ = (version == SSL3_VERSION); |
David Benjamin | 31a0779 | 2015-03-03 14:20:26 -0500 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | return aead_ctx; |
| 144 | } |
| 145 | |
David Benjamin | cfc11c2 | 2017-07-18 22:45:18 -0400 | [diff] [blame] | 146 | size_t SSLAEADContext::ExplicitNonceLen() const { |
| 147 | if (!FUZZER_MODE && variable_nonce_included_in_record_) { |
| 148 | return variable_nonce_len_; |
David Benjamin | 31a0779 | 2015-03-03 14:20:26 -0500 | [diff] [blame] | 149 | } |
| 150 | return 0; |
| 151 | } |
| 152 | |
Martin Kreichgauer | abbf365 | 2017-07-21 16:27:54 -0700 | [diff] [blame] | 153 | bool SSLAEADContext::SuffixLen(size_t *out_suffix_len, const size_t in_len, |
| 154 | const size_t extra_in_len) const { |
| 155 | if (is_null_cipher() || FUZZER_MODE) { |
| 156 | *out_suffix_len = extra_in_len; |
| 157 | return true; |
| 158 | } |
| 159 | return !!EVP_AEAD_CTX_tag_len(ctx_.get(), out_suffix_len, in_len, |
| 160 | extra_in_len); |
Martin Kreichgauer | 9f2bffb | 2017-06-30 05:29:50 -0700 | [diff] [blame] | 161 | } |
| 162 | |
David Benjamin | cfc11c2 | 2017-07-18 22:45:18 -0400 | [diff] [blame] | 163 | size_t SSLAEADContext::MaxOverhead() const { |
Martin Kreichgauer | abbf365 | 2017-07-21 16:27:54 -0700 | [diff] [blame] | 164 | return ExplicitNonceLen() + |
| 165 | (is_null_cipher() || FUZZER_MODE |
| 166 | ? 0 |
| 167 | : EVP_AEAD_max_overhead(EVP_AEAD_CTX_aead(ctx_.get()))); |
David Benjamin | 31a0779 | 2015-03-03 14:20:26 -0500 | [diff] [blame] | 168 | } |
| 169 | |
David Benjamin | cfc11c2 | 2017-07-18 22:45:18 -0400 | [diff] [blame] | 170 | size_t SSLAEADContext::GetAdditionalData(uint8_t out[13], uint8_t type, |
| 171 | uint16_t wire_version, |
| 172 | const uint8_t seqnum[8], |
| 173 | size_t plaintext_len) { |
| 174 | if (omit_ad_) { |
Steven Valdez | 494650c | 2016-05-24 12:43:04 -0400 | [diff] [blame] | 175 | return 0; |
| 176 | } |
| 177 | |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 178 | OPENSSL_memcpy(out, seqnum, 8); |
David Benjamin | 31a0779 | 2015-03-03 14:20:26 -0500 | [diff] [blame] | 179 | size_t len = 8; |
| 180 | out[len++] = type; |
David Benjamin | cfc11c2 | 2017-07-18 22:45:18 -0400 | [diff] [blame] | 181 | if (!omit_version_in_ad_) { |
| 182 | out[len++] = static_cast<uint8_t>((wire_version >> 8)); |
| 183 | out[len++] = static_cast<uint8_t>(wire_version); |
David Benjamin | 31a0779 | 2015-03-03 14:20:26 -0500 | [diff] [blame] | 184 | } |
David Benjamin | cfc11c2 | 2017-07-18 22:45:18 -0400 | [diff] [blame] | 185 | if (!omit_length_in_ad_) { |
| 186 | out[len++] = static_cast<uint8_t>((plaintext_len >> 8)); |
| 187 | out[len++] = static_cast<uint8_t>(plaintext_len); |
David Benjamin | 31a0779 | 2015-03-03 14:20:26 -0500 | [diff] [blame] | 188 | } |
| 189 | return len; |
| 190 | } |
| 191 | |
David Benjamin | cfc11c2 | 2017-07-18 22:45:18 -0400 | [diff] [blame] | 192 | bool SSLAEADContext::Open(CBS *out, uint8_t type, uint16_t wire_version, |
| 193 | const uint8_t seqnum[8], uint8_t *in, size_t in_len) { |
| 194 | if (is_null_cipher() || FUZZER_MODE) { |
David Benjamin | 31a0779 | 2015-03-03 14:20:26 -0500 | [diff] [blame] | 195 | /* Handle the initial NULL cipher. */ |
David Benjamin | a7810c1 | 2016-06-06 18:54:51 -0400 | [diff] [blame] | 196 | CBS_init(out, in, in_len); |
David Benjamin | cfc11c2 | 2017-07-18 22:45:18 -0400 | [diff] [blame] | 197 | return true; |
David Benjamin | 31a0779 | 2015-03-03 14:20:26 -0500 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | /* TLS 1.2 AEADs include the length in the AD and are assumed to have fixed |
| 201 | * overhead. Otherwise the parameter is unused. */ |
| 202 | size_t plaintext_len = 0; |
David Benjamin | cfc11c2 | 2017-07-18 22:45:18 -0400 | [diff] [blame] | 203 | if (!omit_length_in_ad_) { |
| 204 | size_t overhead = MaxOverhead(); |
David Benjamin | 31a0779 | 2015-03-03 14:20:26 -0500 | [diff] [blame] | 205 | if (in_len < overhead) { |
| 206 | /* Publicly invalid. */ |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 207 | OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_PACKET_LENGTH); |
David Benjamin | cfc11c2 | 2017-07-18 22:45:18 -0400 | [diff] [blame] | 208 | return false; |
David Benjamin | 31a0779 | 2015-03-03 14:20:26 -0500 | [diff] [blame] | 209 | } |
| 210 | plaintext_len = in_len - overhead; |
| 211 | } |
| 212 | uint8_t ad[13]; |
David Benjamin | cfc11c2 | 2017-07-18 22:45:18 -0400 | [diff] [blame] | 213 | size_t ad_len = |
| 214 | GetAdditionalData(ad, type, wire_version, seqnum, plaintext_len); |
David Benjamin | 31a0779 | 2015-03-03 14:20:26 -0500 | [diff] [blame] | 215 | |
| 216 | /* Assemble the nonce. */ |
| 217 | uint8_t nonce[EVP_AEAD_MAX_NONCE_LENGTH]; |
| 218 | size_t nonce_len = 0; |
David Benjamin | 13414b3 | 2015-12-09 23:02:39 -0500 | [diff] [blame] | 219 | |
| 220 | /* Prepend the fixed nonce, or left-pad with zeros if XORing. */ |
David Benjamin | cfc11c2 | 2017-07-18 22:45:18 -0400 | [diff] [blame] | 221 | if (xor_fixed_nonce_) { |
| 222 | nonce_len = fixed_nonce_len_ - variable_nonce_len_; |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 223 | OPENSSL_memset(nonce, 0, nonce_len); |
David Benjamin | 13414b3 | 2015-12-09 23:02:39 -0500 | [diff] [blame] | 224 | } else { |
David Benjamin | cfc11c2 | 2017-07-18 22:45:18 -0400 | [diff] [blame] | 225 | OPENSSL_memcpy(nonce, fixed_nonce_, fixed_nonce_len_); |
| 226 | nonce_len += fixed_nonce_len_; |
David Benjamin | 13414b3 | 2015-12-09 23:02:39 -0500 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | /* Add the variable nonce. */ |
David Benjamin | cfc11c2 | 2017-07-18 22:45:18 -0400 | [diff] [blame] | 230 | if (variable_nonce_included_in_record_) { |
| 231 | if (in_len < variable_nonce_len_) { |
David Benjamin | 31a0779 | 2015-03-03 14:20:26 -0500 | [diff] [blame] | 232 | /* Publicly invalid. */ |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 233 | OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_PACKET_LENGTH); |
David Benjamin | cfc11c2 | 2017-07-18 22:45:18 -0400 | [diff] [blame] | 234 | return false; |
David Benjamin | 31a0779 | 2015-03-03 14:20:26 -0500 | [diff] [blame] | 235 | } |
David Benjamin | cfc11c2 | 2017-07-18 22:45:18 -0400 | [diff] [blame] | 236 | OPENSSL_memcpy(nonce + nonce_len, in, variable_nonce_len_); |
| 237 | in += variable_nonce_len_; |
| 238 | in_len -= variable_nonce_len_; |
David Benjamin | 31a0779 | 2015-03-03 14:20:26 -0500 | [diff] [blame] | 239 | } else { |
David Benjamin | cfc11c2 | 2017-07-18 22:45:18 -0400 | [diff] [blame] | 240 | assert(variable_nonce_len_ == 8); |
| 241 | OPENSSL_memcpy(nonce + nonce_len, seqnum, variable_nonce_len_); |
David Benjamin | 31a0779 | 2015-03-03 14:20:26 -0500 | [diff] [blame] | 242 | } |
David Benjamin | cfc11c2 | 2017-07-18 22:45:18 -0400 | [diff] [blame] | 243 | nonce_len += variable_nonce_len_; |
David Benjamin | 31a0779 | 2015-03-03 14:20:26 -0500 | [diff] [blame] | 244 | |
David Benjamin | 13414b3 | 2015-12-09 23:02:39 -0500 | [diff] [blame] | 245 | /* XOR the fixed nonce, if necessary. */ |
David Benjamin | cfc11c2 | 2017-07-18 22:45:18 -0400 | [diff] [blame] | 246 | if (xor_fixed_nonce_) { |
| 247 | assert(nonce_len == fixed_nonce_len_); |
| 248 | for (size_t i = 0; i < fixed_nonce_len_; i++) { |
| 249 | nonce[i] ^= fixed_nonce_[i]; |
David Benjamin | 13414b3 | 2015-12-09 23:02:39 -0500 | [diff] [blame] | 250 | } |
| 251 | } |
| 252 | |
David Benjamin | a7810c1 | 2016-06-06 18:54:51 -0400 | [diff] [blame] | 253 | /* Decrypt in-place. */ |
| 254 | size_t len; |
David Benjamin | cfc11c2 | 2017-07-18 22:45:18 -0400 | [diff] [blame] | 255 | if (!EVP_AEAD_CTX_open(ctx_.get(), in, &len, in_len, nonce, nonce_len, in, |
| 256 | in_len, ad, ad_len)) { |
| 257 | return false; |
David Benjamin | a7810c1 | 2016-06-06 18:54:51 -0400 | [diff] [blame] | 258 | } |
| 259 | CBS_init(out, in, len); |
David Benjamin | cfc11c2 | 2017-07-18 22:45:18 -0400 | [diff] [blame] | 260 | return true; |
David Benjamin | 31a0779 | 2015-03-03 14:20:26 -0500 | [diff] [blame] | 261 | } |
| 262 | |
David Benjamin | cfc11c2 | 2017-07-18 22:45:18 -0400 | [diff] [blame] | 263 | bool SSLAEADContext::SealScatter(uint8_t *out_prefix, uint8_t *out, |
Martin Kreichgauer | abbf365 | 2017-07-21 16:27:54 -0700 | [diff] [blame] | 264 | uint8_t *out_suffix, uint8_t type, |
David Benjamin | cfc11c2 | 2017-07-18 22:45:18 -0400 | [diff] [blame] | 265 | uint16_t wire_version, const uint8_t seqnum[8], |
| 266 | const uint8_t *in, size_t in_len, |
| 267 | const uint8_t *extra_in, size_t extra_in_len) { |
Martin Kreichgauer | abbf365 | 2017-07-21 16:27:54 -0700 | [diff] [blame] | 268 | const size_t prefix_len = ExplicitNonceLen(); |
| 269 | size_t suffix_len; |
| 270 | if (!SuffixLen(&suffix_len, in_len, extra_in_len)) { |
| 271 | OPENSSL_PUT_ERROR(SSL, SSL_R_RECORD_TOO_LARGE); |
David Benjamin | cfc11c2 | 2017-07-18 22:45:18 -0400 | [diff] [blame] | 272 | return false; |
Martin Kreichgauer | 9f2bffb | 2017-06-30 05:29:50 -0700 | [diff] [blame] | 273 | } |
Martin Kreichgauer | abbf365 | 2017-07-21 16:27:54 -0700 | [diff] [blame] | 274 | if ((in != out && buffers_alias(in, in_len, out, in_len)) || |
| 275 | buffers_alias(in, in_len, out_prefix, prefix_len) || |
| 276 | buffers_alias(in, in_len, out_suffix, suffix_len)) { |
| 277 | OPENSSL_PUT_ERROR(SSL, SSL_R_OUTPUT_ALIASES_INPUT); |
David Benjamin | cfc11c2 | 2017-07-18 22:45:18 -0400 | [diff] [blame] | 278 | return false; |
Martin Kreichgauer | 9f2bffb | 2017-06-30 05:29:50 -0700 | [diff] [blame] | 279 | } |
| 280 | |
David Benjamin | cfc11c2 | 2017-07-18 22:45:18 -0400 | [diff] [blame] | 281 | if (is_null_cipher() || FUZZER_MODE) { |
David Benjamin | 31a0779 | 2015-03-03 14:20:26 -0500 | [diff] [blame] | 282 | /* Handle the initial NULL cipher. */ |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 283 | OPENSSL_memmove(out, in, in_len); |
Martin Kreichgauer | 9f2bffb | 2017-06-30 05:29:50 -0700 | [diff] [blame] | 284 | OPENSSL_memmove(out_suffix, extra_in, extra_in_len); |
David Benjamin | cfc11c2 | 2017-07-18 22:45:18 -0400 | [diff] [blame] | 285 | return true; |
David Benjamin | 31a0779 | 2015-03-03 14:20:26 -0500 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | uint8_t ad[13]; |
David Benjamin | cfc11c2 | 2017-07-18 22:45:18 -0400 | [diff] [blame] | 289 | size_t ad_len = GetAdditionalData(ad, type, wire_version, seqnum, in_len); |
David Benjamin | 31a0779 | 2015-03-03 14:20:26 -0500 | [diff] [blame] | 290 | |
| 291 | /* Assemble the nonce. */ |
| 292 | uint8_t nonce[EVP_AEAD_MAX_NONCE_LENGTH]; |
| 293 | size_t nonce_len = 0; |
David Benjamin | 13414b3 | 2015-12-09 23:02:39 -0500 | [diff] [blame] | 294 | |
| 295 | /* Prepend the fixed nonce, or left-pad with zeros if XORing. */ |
David Benjamin | cfc11c2 | 2017-07-18 22:45:18 -0400 | [diff] [blame] | 296 | if (xor_fixed_nonce_) { |
| 297 | nonce_len = fixed_nonce_len_ - variable_nonce_len_; |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 298 | OPENSSL_memset(nonce, 0, nonce_len); |
David Benjamin | 13414b3 | 2015-12-09 23:02:39 -0500 | [diff] [blame] | 299 | } else { |
David Benjamin | cfc11c2 | 2017-07-18 22:45:18 -0400 | [diff] [blame] | 300 | OPENSSL_memcpy(nonce, fixed_nonce_, fixed_nonce_len_); |
| 301 | nonce_len += fixed_nonce_len_; |
David Benjamin | 13414b3 | 2015-12-09 23:02:39 -0500 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | /* Select the variable nonce. */ |
David Benjamin | cfc11c2 | 2017-07-18 22:45:18 -0400 | [diff] [blame] | 305 | if (random_variable_nonce_) { |
| 306 | assert(variable_nonce_included_in_record_); |
| 307 | if (!RAND_bytes(nonce + nonce_len, variable_nonce_len_)) { |
| 308 | return false; |
David Benjamin | 31a0779 | 2015-03-03 14:20:26 -0500 | [diff] [blame] | 309 | } |
| 310 | } else { |
| 311 | /* When sending we use the sequence number as the variable part of the |
| 312 | * nonce. */ |
David Benjamin | cfc11c2 | 2017-07-18 22:45:18 -0400 | [diff] [blame] | 313 | assert(variable_nonce_len_ == 8); |
| 314 | OPENSSL_memcpy(nonce + nonce_len, seqnum, variable_nonce_len_); |
David Benjamin | 31a0779 | 2015-03-03 14:20:26 -0500 | [diff] [blame] | 315 | } |
David Benjamin | cfc11c2 | 2017-07-18 22:45:18 -0400 | [diff] [blame] | 316 | nonce_len += variable_nonce_len_; |
David Benjamin | 31a0779 | 2015-03-03 14:20:26 -0500 | [diff] [blame] | 317 | |
| 318 | /* Emit the variable nonce if included in the record. */ |
David Benjamin | cfc11c2 | 2017-07-18 22:45:18 -0400 | [diff] [blame] | 319 | if (variable_nonce_included_in_record_) { |
| 320 | assert(!xor_fixed_nonce_); |
| 321 | if (buffers_alias(in, in_len, out_prefix, variable_nonce_len_)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 322 | OPENSSL_PUT_ERROR(SSL, SSL_R_OUTPUT_ALIASES_INPUT); |
David Benjamin | cfc11c2 | 2017-07-18 22:45:18 -0400 | [diff] [blame] | 323 | return false; |
David Benjamin | 31a0779 | 2015-03-03 14:20:26 -0500 | [diff] [blame] | 324 | } |
David Benjamin | cfc11c2 | 2017-07-18 22:45:18 -0400 | [diff] [blame] | 325 | OPENSSL_memcpy(out_prefix, nonce + fixed_nonce_len_, |
| 326 | variable_nonce_len_); |
David Benjamin | 31a0779 | 2015-03-03 14:20:26 -0500 | [diff] [blame] | 327 | } |
| 328 | |
David Benjamin | 13414b3 | 2015-12-09 23:02:39 -0500 | [diff] [blame] | 329 | /* XOR the fixed nonce, if necessary. */ |
David Benjamin | cfc11c2 | 2017-07-18 22:45:18 -0400 | [diff] [blame] | 330 | if (xor_fixed_nonce_) { |
| 331 | assert(nonce_len == fixed_nonce_len_); |
| 332 | for (size_t i = 0; i < fixed_nonce_len_; i++) { |
| 333 | nonce[i] ^= fixed_nonce_[i]; |
David Benjamin | 13414b3 | 2015-12-09 23:02:39 -0500 | [diff] [blame] | 334 | } |
| 335 | } |
| 336 | |
Martin Kreichgauer | abbf365 | 2017-07-21 16:27:54 -0700 | [diff] [blame] | 337 | size_t written_suffix_len; |
| 338 | bool result = !!EVP_AEAD_CTX_seal_scatter( |
| 339 | ctx_.get(), out, out_suffix, &written_suffix_len, suffix_len, nonce, |
David Benjamin | cfc11c2 | 2017-07-18 22:45:18 -0400 | [diff] [blame] | 340 | nonce_len, in, in_len, extra_in, extra_in_len, ad, ad_len); |
Martin Kreichgauer | abbf365 | 2017-07-21 16:27:54 -0700 | [diff] [blame] | 341 | assert(!result || written_suffix_len == suffix_len); |
| 342 | return result; |
Martin Kreichgauer | 9f2bffb | 2017-06-30 05:29:50 -0700 | [diff] [blame] | 343 | } |
| 344 | |
David Benjamin | cfc11c2 | 2017-07-18 22:45:18 -0400 | [diff] [blame] | 345 | bool SSLAEADContext::Seal(uint8_t *out, size_t *out_len, size_t max_out_len, |
| 346 | uint8_t type, uint16_t wire_version, |
| 347 | const uint8_t seqnum[8], const uint8_t *in, |
| 348 | size_t in_len) { |
Martin Kreichgauer | abbf365 | 2017-07-21 16:27:54 -0700 | [diff] [blame] | 349 | const size_t prefix_len = ExplicitNonceLen(); |
| 350 | size_t suffix_len; |
| 351 | if (!SuffixLen(&suffix_len, in_len, 0)) { |
| 352 | OPENSSL_PUT_ERROR(SSL, SSL_R_RECORD_TOO_LARGE); |
| 353 | return false; |
| 354 | } |
| 355 | if (in_len + prefix_len < in_len || |
| 356 | in_len + prefix_len + suffix_len < in_len + prefix_len) { |
Martin Kreichgauer | 9f2bffb | 2017-06-30 05:29:50 -0700 | [diff] [blame] | 357 | OPENSSL_PUT_ERROR(CIPHER, SSL_R_RECORD_TOO_LARGE); |
David Benjamin | cfc11c2 | 2017-07-18 22:45:18 -0400 | [diff] [blame] | 358 | return false; |
David Benjamin | 31a0779 | 2015-03-03 14:20:26 -0500 | [diff] [blame] | 359 | } |
Martin Kreichgauer | abbf365 | 2017-07-21 16:27:54 -0700 | [diff] [blame] | 360 | if (in_len + prefix_len + suffix_len > max_out_len) { |
Martin Kreichgauer | 9f2bffb | 2017-06-30 05:29:50 -0700 | [diff] [blame] | 361 | OPENSSL_PUT_ERROR(SSL, SSL_R_BUFFER_TOO_SMALL); |
David Benjamin | cfc11c2 | 2017-07-18 22:45:18 -0400 | [diff] [blame] | 362 | return false; |
Martin Kreichgauer | 9f2bffb | 2017-06-30 05:29:50 -0700 | [diff] [blame] | 363 | } |
| 364 | |
Martin Kreichgauer | abbf365 | 2017-07-21 16:27:54 -0700 | [diff] [blame] | 365 | if (!SealScatter(out, out + prefix_len, out + prefix_len + in_len, type, |
David Benjamin | cfc11c2 | 2017-07-18 22:45:18 -0400 | [diff] [blame] | 366 | wire_version, seqnum, in, in_len, 0, 0)) { |
| 367 | return false; |
Martin Kreichgauer | 9f2bffb | 2017-06-30 05:29:50 -0700 | [diff] [blame] | 368 | } |
Martin Kreichgauer | 9f2bffb | 2017-06-30 05:29:50 -0700 | [diff] [blame] | 369 | *out_len = prefix_len + in_len + suffix_len; |
David Benjamin | cfc11c2 | 2017-07-18 22:45:18 -0400 | [diff] [blame] | 370 | return true; |
| 371 | } |
| 372 | |
| 373 | bool SSLAEADContext::GetIV(const uint8_t **out_iv, size_t *out_iv_len) const { |
| 374 | return !is_null_cipher() && |
| 375 | EVP_AEAD_CTX_get_iv(ctx_.get(), out_iv, out_iv_len); |
David Benjamin | 31a0779 | 2015-03-03 14:20:26 -0500 | [diff] [blame] | 376 | } |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 377 | |
| 378 | } // namespace bssl |