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