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