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