blob: 25569221bca82e5fbf5c808d743b30167ee3e3dc [file] [log] [blame]
David Benjamin31a07792015-03-03 14:20:26 -05001/* 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 Benjamin9e4e01e2015-09-15 01:48:04 -040015#include <openssl/ssl.h>
16
David Benjamin31a07792015-03-03 14:20:26 -050017#include <assert.h>
18#include <string.h>
19
20#include <openssl/aead.h>
21#include <openssl/err.h>
22#include <openssl/rand.h>
David Benjamin31a07792015-03-03 14:20:26 -050023
David Benjamin17cf2cb2016-12-13 01:07:13 -050024#include "../crypto/internal.h"
David Benjamin31a07792015-03-03 14:20:26 -050025#include "internal.h"
26
27
David Benjamincfc11c22017-07-18 22:45:18 -040028#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
29#define FUZZER_MODE true
30#else
31#define FUZZER_MODE false
32#endif
33
Joshua Liebow-Feeser8c7c6352018-08-26 18:53:36 -070034BSSL_NAMESPACE_BEGIN
David Benjamin86e95b82017-07-18 16:34:25 -040035
Steven Valdezc7d4d212017-09-11 13:53:08 -040036SSLAEADContext::SSLAEADContext(uint16_t version_arg, bool is_dtls_arg,
David Benjamincfc11c22017-07-18 22:45:18 -040037 const SSL_CIPHER *cipher_arg)
38 : cipher_(cipher_arg),
39 version_(version_arg),
Steven Valdezc7d4d212017-09-11 13:53:08 -040040 is_dtls_(is_dtls_arg),
David Benjamincfc11c22017-07-18 22:45:18 -040041 variable_nonce_included_in_record_(false),
42 random_variable_nonce_(false),
David Benjamine2ab21d2018-04-04 23:55:06 -040043 xor_fixed_nonce_(false),
David Benjamincfc11c22017-07-18 22:45:18 -040044 omit_length_in_ad_(false),
David Benjamine2ab21d2018-04-04 23:55:06 -040045 ad_is_header_(false) {
David Benjamincfc11c22017-07-18 22:45:18 -040046 OPENSSL_memset(fixed_nonce_, 0, sizeof(fixed_nonce_));
47}
48
49SSLAEADContext::~SSLAEADContext() {}
50
Steven Valdezc7d4d212017-09-11 13:53:08 -040051UniquePtr<SSLAEADContext> SSLAEADContext::CreateNullCipher(bool is_dtls) {
52 return MakeUnique<SSLAEADContext>(0 /* version */, is_dtls,
53 nullptr /* cipher */);
David Benjamincfc11c22017-07-18 22:45:18 -040054}
55
56UniquePtr<SSLAEADContext> SSLAEADContext::Create(
David Benjamin8525ff32018-09-05 18:44:15 -050057 enum evp_aead_direction_t direction, uint16_t version, bool is_dtls,
David Benjaminb9493552017-09-27 19:02:51 -040058 const SSL_CIPHER *cipher, Span<const uint8_t> enc_key,
59 Span<const uint8_t> mac_key, Span<const uint8_t> fixed_iv) {
David Benjamin31a07792015-03-03 14:20:26 -050060 const EVP_AEAD *aead;
Steven Valdezc7d4d212017-09-11 13:53:08 -040061 uint16_t protocol_version;
David Benjamin4b0d0e42016-10-28 17:17:14 -040062 size_t expected_mac_key_len, expected_fixed_iv_len;
Steven Valdezc7d4d212017-09-11 13:53:08 -040063 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 Valdez2f3404b2017-05-24 16:54:35 -040066 is_dtls) ||
David Benjaminc11ea9422017-08-29 16:33:21 -040067 // Ensure the caller returned correct key sizes.
David Benjaminb9493552017-09-27 19:02:51 -040068 expected_fixed_iv_len != fixed_iv.size() ||
69 expected_mac_key_len != mac_key.size()) {
David Benjamin3570d732015-06-29 00:28:17 -040070 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamincfc11c22017-07-18 22:45:18 -040071 return nullptr;
David Benjamin31a07792015-03-03 14:20:26 -050072 }
73
74 uint8_t merged_key[EVP_AEAD_MAX_KEY_LENGTH];
David Benjaminb9493552017-09-27 19:02:51 -040075 if (!mac_key.empty()) {
David Benjaminc11ea9422017-08-29 16:33:21 -040076 // This is a "stateful" AEAD (for compatibility with pre-AEAD cipher
77 // suites).
David Benjaminb9493552017-09-27 19:02:51 -040078 if (mac_key.size() + enc_key.size() + fixed_iv.size() >
79 sizeof(merged_key)) {
David Benjamin3570d732015-06-29 00:28:17 -040080 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamincfc11c22017-07-18 22:45:18 -040081 return nullptr;
David Benjamin31a07792015-03-03 14:20:26 -050082 }
David Benjaminb9493552017-09-27 19:02:51 -040083 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 Benjamin31a07792015-03-03 14:20:26 -050089 }
90
David Benjamincfc11c22017-07-18 22:45:18 -040091 UniquePtr<SSLAEADContext> aead_ctx =
Steven Valdezc7d4d212017-09-11 13:53:08 -040092 MakeUnique<SSLAEADContext>(version, is_dtls, cipher);
David Benjamincfc11c22017-07-18 22:45:18 -040093 if (!aead_ctx) {
David Benjamincfc11c22017-07-18 22:45:18 -040094 return nullptr;
David Benjamin31a07792015-03-03 14:20:26 -050095 }
David Benjamin31a07792015-03-03 14:20:26 -050096
Steven Valdezc7d4d212017-09-11 13:53:08 -040097 assert(aead_ctx->ProtocolVersion() == protocol_version);
98
David Benjamin31a07792015-03-03 14:20:26 -050099 if (!EVP_AEAD_CTX_init_with_direction(
David Benjaminb9493552017-09-27 19:02:51 -0400100 aead_ctx->ctx_.get(), aead, enc_key.data(), enc_key.size(),
David Benjamin31a07792015-03-03 14:20:26 -0500101 EVP_AEAD_DEFAULT_TAG_LENGTH, direction)) {
David Benjamincfc11c22017-07-18 22:45:18 -0400102 return nullptr;
David Benjamin31a07792015-03-03 14:20:26 -0500103 }
104
105 assert(EVP_AEAD_nonce_length(aead) <= EVP_AEAD_MAX_NONCE_LENGTH);
David Benjamina3d76d02017-07-14 19:36:07 -0400106 static_assert(EVP_AEAD_MAX_NONCE_LENGTH < 256,
107 "variable_nonce_len doesn't fit in uint8_t");
David Benjamincfc11c22017-07-18 22:45:18 -0400108 aead_ctx->variable_nonce_len_ = (uint8_t)EVP_AEAD_nonce_length(aead);
David Benjaminb9493552017-09-27 19:02:51 -0400109 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 Benjamin13414b32015-12-09 23:02:39 -0500113
114 if (cipher->algorithm_enc & SSL_CHACHA20POLY1305) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400115 // The fixed nonce into the actual nonce (the sequence number).
David Benjamincfc11c22017-07-18 22:45:18 -0400116 aead_ctx->xor_fixed_nonce_ = true;
117 aead_ctx->variable_nonce_len_ = 8;
David Benjamin13414b32015-12-09 23:02:39 -0500118 } else {
David Benjaminc11ea9422017-08-29 16:33:21 -0400119 // The fixed IV is prepended to the nonce.
David Benjaminb9493552017-09-27 19:02:51 -0400120 assert(fixed_iv.size() <= aead_ctx->variable_nonce_len_);
121 aead_ctx->variable_nonce_len_ -= fixed_iv.size();
David Benjamin13414b32015-12-09 23:02:39 -0500122 }
123
David Benjaminc11ea9422017-08-29 16:33:21 -0400124 // AES-GCM uses an explicit nonce.
David Benjaminb2a985b2015-06-21 15:13:57 -0400125 if (cipher->algorithm_enc & (SSL_AES128GCM | SSL_AES256GCM)) {
David Benjamincfc11c22017-07-18 22:45:18 -0400126 aead_ctx->variable_nonce_included_in_record_ = true;
David Benjaminb2a985b2015-06-21 15:13:57 -0400127 }
Steven Valdez494650c2016-05-24 12:43:04 -0400128
David Benjaminc11ea9422017-08-29 16:33:21 -0400129 // The TLS 1.3 construction XORs the fixed nonce into the sequence number
130 // and omits the additional data.
Steven Valdezc7d4d212017-09-11 13:53:08 -0400131 if (protocol_version >= TLS1_3_VERSION) {
David Benjamincfc11c22017-07-18 22:45:18 -0400132 aead_ctx->xor_fixed_nonce_ = true;
133 aead_ctx->variable_nonce_len_ = 8;
134 aead_ctx->variable_nonce_included_in_record_ = false;
Steven Valdezb84674b2018-08-28 10:14:07 -0400135 aead_ctx->ad_is_header_ = true;
David Benjaminb9493552017-09-27 19:02:51 -0400136 assert(fixed_iv.size() >= aead_ctx->variable_nonce_len_);
Steven Valdez494650c2016-05-24 12:43:04 -0400137 }
David Benjamin31a07792015-03-03 14:20:26 -0500138 } else {
Steven Valdezc7d4d212017-09-11 13:53:08 -0400139 assert(protocol_version < TLS1_3_VERSION);
David Benjamincfc11c22017-07-18 22:45:18 -0400140 aead_ctx->variable_nonce_included_in_record_ = true;
141 aead_ctx->random_variable_nonce_ = true;
142 aead_ctx->omit_length_in_ad_ = true;
David Benjamin31a07792015-03-03 14:20:26 -0500143 }
144
145 return aead_ctx;
146}
147
Steven Valdezc8e0f902018-07-14 11:23:01 -0400148UniquePtr<SSLAEADContext> SSLAEADContext::CreatePlaceholderForQUIC(
149 uint16_t version, const SSL_CIPHER *cipher) {
150 return MakeUnique<SSLAEADContext>(version, false, cipher);
151}
152
Steven Valdezc7d4d212017-09-11 13:53:08 -0400153void SSLAEADContext::SetVersionIfNullCipher(uint16_t version) {
154 if (is_null_cipher()) {
155 version_ = version;
156 }
157}
158
159uint16_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
168uint16_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 Valdez64cc1212017-12-04 11:15:37 -0500178 return TLS1_2_VERSION;
Steven Valdezc7d4d212017-09-11 13:53:08 -0400179}
180
David Benjamincfc11c22017-07-18 22:45:18 -0400181size_t SSLAEADContext::ExplicitNonceLen() const {
182 if (!FUZZER_MODE && variable_nonce_included_in_record_) {
183 return variable_nonce_len_;
David Benjamin31a07792015-03-03 14:20:26 -0500184 }
185 return 0;
186}
187
Martin Kreichgauerabbf3652017-07-21 16:27:54 -0700188bool 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 Kreichgauer9f2bffb2017-06-30 05:29:50 -0700196}
197
David Benjamine2ab21d2018-04-04 23:55:06 -0400198bool 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 Benjamincfc11c22017-07-18 22:45:18 -0400214size_t SSLAEADContext::MaxOverhead() const {
Martin Kreichgauerabbf3652017-07-21 16:27:54 -0700215 return ExplicitNonceLen() +
216 (is_null_cipher() || FUZZER_MODE
217 ? 0
218 : EVP_AEAD_max_overhead(EVP_AEAD_CTX_aead(ctx_.get())));
David Benjamin31a07792015-03-03 14:20:26 -0500219}
220
David Benjamine2ab21d2018-04-04 23:55:06 -0400221Span<const uint8_t> SSLAEADContext::GetAdditionalData(
David Benjamin32013e82022-09-22 16:55:34 -0400222 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 Benjamine2ab21d2018-04-04 23:55:06 -0400224 if (ad_is_header_) {
225 return header;
Steven Valdez494650c2016-05-24 12:43:04 -0400226 }
227
David Benjamin32013e82022-09-22 16:55:34 -0400228 CRYPTO_store_u64_be(storage, seqnum);
David Benjamine2ab21d2018-04-04 23:55:06 -0400229 size_t len = 8;
230 storage[len++] = type;
David Benjamin9bb15f52018-06-26 00:07:40 -0400231 storage[len++] = static_cast<uint8_t>((record_version >> 8));
232 storage[len++] = static_cast<uint8_t>(record_version);
David Benjamine2ab21d2018-04-04 23:55:06 -0400233 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 Benjamin31a07792015-03-03 14:20:26 -0500236 }
David Benjamine2ab21d2018-04-04 23:55:06 -0400237 return MakeConstSpan(storage, len);
David Benjamin31a07792015-03-03 14:20:26 -0500238}
239
David Benjaminc64d1232017-10-04 18:14:28 -0400240bool SSLAEADContext::Open(Span<uint8_t> *out, uint8_t type,
David Benjamin32013e82022-09-22 16:55:34 -0400241 uint16_t record_version, uint64_t seqnum,
David Benjamine2ab21d2018-04-04 23:55:06 -0400242 Span<const uint8_t> header, Span<uint8_t> in) {
David Benjamincfc11c22017-07-18 22:45:18 -0400243 if (is_null_cipher() || FUZZER_MODE) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400244 // Handle the initial NULL cipher.
David Benjaminc64d1232017-10-04 18:14:28 -0400245 *out = in;
David Benjamincfc11c22017-07-18 22:45:18 -0400246 return true;
David Benjamin31a07792015-03-03 14:20:26 -0500247 }
248
David Benjaminc11ea9422017-08-29 16:33:21 -0400249 // TLS 1.2 AEADs include the length in the AD and are assumed to have fixed
250 // overhead. Otherwise the parameter is unused.
David Benjamin31a07792015-03-03 14:20:26 -0500251 size_t plaintext_len = 0;
David Benjamincfc11c22017-07-18 22:45:18 -0400252 if (!omit_length_in_ad_) {
253 size_t overhead = MaxOverhead();
David Benjaminc64d1232017-10-04 18:14:28 -0400254 if (in.size() < overhead) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400255 // Publicly invalid.
David Benjamin3570d732015-06-29 00:28:17 -0400256 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_PACKET_LENGTH);
David Benjamincfc11c22017-07-18 22:45:18 -0400257 return false;
David Benjamin31a07792015-03-03 14:20:26 -0500258 }
David Benjaminc64d1232017-10-04 18:14:28 -0400259 plaintext_len = in.size() - overhead;
David Benjamin31a07792015-03-03 14:20:26 -0500260 }
David Benjamine2ab21d2018-04-04 23:55:06 -0400261
262 uint8_t ad_storage[13];
263 Span<const uint8_t> ad = GetAdditionalData(ad_storage, type, record_version,
264 seqnum, plaintext_len, header);
David Benjamin31a07792015-03-03 14:20:26 -0500265
David Benjaminc11ea9422017-08-29 16:33:21 -0400266 // Assemble the nonce.
David Benjamin31a07792015-03-03 14:20:26 -0500267 uint8_t nonce[EVP_AEAD_MAX_NONCE_LENGTH];
268 size_t nonce_len = 0;
David Benjamin13414b32015-12-09 23:02:39 -0500269
David Benjaminc11ea9422017-08-29 16:33:21 -0400270 // Prepend the fixed nonce, or left-pad with zeros if XORing.
David Benjamincfc11c22017-07-18 22:45:18 -0400271 if (xor_fixed_nonce_) {
272 nonce_len = fixed_nonce_len_ - variable_nonce_len_;
David Benjamin17cf2cb2016-12-13 01:07:13 -0500273 OPENSSL_memset(nonce, 0, nonce_len);
David Benjamin13414b32015-12-09 23:02:39 -0500274 } else {
David Benjamincfc11c22017-07-18 22:45:18 -0400275 OPENSSL_memcpy(nonce, fixed_nonce_, fixed_nonce_len_);
276 nonce_len += fixed_nonce_len_;
David Benjamin13414b32015-12-09 23:02:39 -0500277 }
278
David Benjaminc11ea9422017-08-29 16:33:21 -0400279 // Add the variable nonce.
David Benjamincfc11c22017-07-18 22:45:18 -0400280 if (variable_nonce_included_in_record_) {
David Benjaminc64d1232017-10-04 18:14:28 -0400281 if (in.size() < variable_nonce_len_) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400282 // Publicly invalid.
David Benjamin3570d732015-06-29 00:28:17 -0400283 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_PACKET_LENGTH);
David Benjamincfc11c22017-07-18 22:45:18 -0400284 return false;
David Benjamin31a07792015-03-03 14:20:26 -0500285 }
David Benjaminc64d1232017-10-04 18:14:28 -0400286 OPENSSL_memcpy(nonce + nonce_len, in.data(), variable_nonce_len_);
287 in = in.subspan(variable_nonce_len_);
David Benjamin31a07792015-03-03 14:20:26 -0500288 } else {
David Benjamincfc11c22017-07-18 22:45:18 -0400289 assert(variable_nonce_len_ == 8);
David Benjamin32013e82022-09-22 16:55:34 -0400290 CRYPTO_store_u64_be(nonce + nonce_len, seqnum);
David Benjamin31a07792015-03-03 14:20:26 -0500291 }
David Benjamincfc11c22017-07-18 22:45:18 -0400292 nonce_len += variable_nonce_len_;
David Benjamin31a07792015-03-03 14:20:26 -0500293
David Benjaminc11ea9422017-08-29 16:33:21 -0400294 // XOR the fixed nonce, if necessary.
David Benjamincfc11c22017-07-18 22:45:18 -0400295 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 Benjamin13414b32015-12-09 23:02:39 -0500299 }
300 }
301
David Benjaminc11ea9422017-08-29 16:33:21 -0400302 // Decrypt in-place.
David Benjamina7810c12016-06-06 18:54:51 -0400303 size_t len;
David Benjaminc64d1232017-10-04 18:14:28 -0400304 if (!EVP_AEAD_CTX_open(ctx_.get(), in.data(), &len, in.size(), nonce,
David Benjamine2ab21d2018-04-04 23:55:06 -0400305 nonce_len, in.data(), in.size(), ad.data(),
306 ad.size())) {
David Benjamincfc11c22017-07-18 22:45:18 -0400307 return false;
David Benjamina7810c12016-06-06 18:54:51 -0400308 }
David Benjaminc64d1232017-10-04 18:14:28 -0400309 *out = in.subspan(0, len);
David Benjamincfc11c22017-07-18 22:45:18 -0400310 return true;
David Benjamin31a07792015-03-03 14:20:26 -0500311}
312
David Benjamincfc11c22017-07-18 22:45:18 -0400313bool SSLAEADContext::SealScatter(uint8_t *out_prefix, uint8_t *out,
Martin Kreichgauerabbf3652017-07-21 16:27:54 -0700314 uint8_t *out_suffix, uint8_t type,
David Benjamin32013e82022-09-22 16:55:34 -0400315 uint16_t record_version, uint64_t seqnum,
David Benjamine2ab21d2018-04-04 23:55:06 -0400316 Span<const uint8_t> header, const uint8_t *in,
Steven Valdezc7d4d212017-09-11 13:53:08 -0400317 size_t in_len, const uint8_t *extra_in,
318 size_t extra_in_len) {
Martin Kreichgauerabbf3652017-07-21 16:27:54 -0700319 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 Benjamincfc11c22017-07-18 22:45:18 -0400323 return false;
Martin Kreichgauer9f2bffb2017-06-30 05:29:50 -0700324 }
Martin Kreichgauerabbf3652017-07-21 16:27:54 -0700325 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 Benjamincfc11c22017-07-18 22:45:18 -0400329 return false;
Martin Kreichgauer9f2bffb2017-06-30 05:29:50 -0700330 }
331
David Benjamincfc11c22017-07-18 22:45:18 -0400332 if (is_null_cipher() || FUZZER_MODE) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400333 // Handle the initial NULL cipher.
David Benjamin17cf2cb2016-12-13 01:07:13 -0500334 OPENSSL_memmove(out, in, in_len);
Martin Kreichgauer9f2bffb2017-06-30 05:29:50 -0700335 OPENSSL_memmove(out_suffix, extra_in, extra_in_len);
David Benjamincfc11c22017-07-18 22:45:18 -0400336 return true;
David Benjamin31a07792015-03-03 14:20:26 -0500337 }
338
David Benjamine2ab21d2018-04-04 23:55:06 -0400339 uint8_t ad_storage[13];
340 Span<const uint8_t> ad = GetAdditionalData(ad_storage, type, record_version,
341 seqnum, in_len, header);
David Benjamin31a07792015-03-03 14:20:26 -0500342
David Benjaminc11ea9422017-08-29 16:33:21 -0400343 // Assemble the nonce.
David Benjamin31a07792015-03-03 14:20:26 -0500344 uint8_t nonce[EVP_AEAD_MAX_NONCE_LENGTH];
345 size_t nonce_len = 0;
David Benjamin13414b32015-12-09 23:02:39 -0500346
David Benjaminc11ea9422017-08-29 16:33:21 -0400347 // Prepend the fixed nonce, or left-pad with zeros if XORing.
David Benjamincfc11c22017-07-18 22:45:18 -0400348 if (xor_fixed_nonce_) {
349 nonce_len = fixed_nonce_len_ - variable_nonce_len_;
David Benjamin17cf2cb2016-12-13 01:07:13 -0500350 OPENSSL_memset(nonce, 0, nonce_len);
David Benjamin13414b32015-12-09 23:02:39 -0500351 } else {
David Benjamincfc11c22017-07-18 22:45:18 -0400352 OPENSSL_memcpy(nonce, fixed_nonce_, fixed_nonce_len_);
353 nonce_len += fixed_nonce_len_;
David Benjamin13414b32015-12-09 23:02:39 -0500354 }
355
David Benjaminc11ea9422017-08-29 16:33:21 -0400356 // Select the variable nonce.
David Benjamincfc11c22017-07-18 22:45:18 -0400357 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 Benjamin31a07792015-03-03 14:20:26 -0500361 }
362 } else {
David Benjaminc11ea9422017-08-29 16:33:21 -0400363 // When sending we use the sequence number as the variable part of the
364 // nonce.
David Benjamincfc11c22017-07-18 22:45:18 -0400365 assert(variable_nonce_len_ == 8);
David Benjamin32013e82022-09-22 16:55:34 -0400366 CRYPTO_store_u64_be(nonce + nonce_len, seqnum);
David Benjamin31a07792015-03-03 14:20:26 -0500367 }
David Benjamincfc11c22017-07-18 22:45:18 -0400368 nonce_len += variable_nonce_len_;
David Benjamin31a07792015-03-03 14:20:26 -0500369
David Benjaminc11ea9422017-08-29 16:33:21 -0400370 // Emit the variable nonce if included in the record.
David Benjamincfc11c22017-07-18 22:45:18 -0400371 if (variable_nonce_included_in_record_) {
372 assert(!xor_fixed_nonce_);
373 if (buffers_alias(in, in_len, out_prefix, variable_nonce_len_)) {
David Benjamin3570d732015-06-29 00:28:17 -0400374 OPENSSL_PUT_ERROR(SSL, SSL_R_OUTPUT_ALIASES_INPUT);
David Benjamincfc11c22017-07-18 22:45:18 -0400375 return false;
David Benjamin31a07792015-03-03 14:20:26 -0500376 }
David Benjamincfc11c22017-07-18 22:45:18 -0400377 OPENSSL_memcpy(out_prefix, nonce + fixed_nonce_len_,
378 variable_nonce_len_);
David Benjamin31a07792015-03-03 14:20:26 -0500379 }
380
David Benjaminc11ea9422017-08-29 16:33:21 -0400381 // XOR the fixed nonce, if necessary.
David Benjamincfc11c22017-07-18 22:45:18 -0400382 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 Benjamin13414b32015-12-09 23:02:39 -0500386 }
387 }
388
Martin Kreichgauerabbf3652017-07-21 16:27:54 -0700389 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 Benjamine2ab21d2018-04-04 23:55:06 -0400392 nonce_len, in, in_len, extra_in, extra_in_len, ad.data(), ad.size());
Martin Kreichgauerabbf3652017-07-21 16:27:54 -0700393 assert(!result || written_suffix_len == suffix_len);
394 return result;
Martin Kreichgauer9f2bffb2017-06-30 05:29:50 -0700395}
396
David Benjamincfc11c22017-07-18 22:45:18 -0400397bool SSLAEADContext::Seal(uint8_t *out, size_t *out_len, size_t max_out_len,
Steven Valdezc7d4d212017-09-11 13:53:08 -0400398 uint8_t type, uint16_t record_version,
David Benjamin32013e82022-09-22 16:55:34 -0400399 uint64_t seqnum, Span<const uint8_t> header,
David Benjamine2ab21d2018-04-04 23:55:06 -0400400 const uint8_t *in, size_t in_len) {
Martin Kreichgauerabbf3652017-07-21 16:27:54 -0700401 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 Kreichgauer9f2bffb2017-06-30 05:29:50 -0700409 OPENSSL_PUT_ERROR(CIPHER, SSL_R_RECORD_TOO_LARGE);
David Benjamincfc11c22017-07-18 22:45:18 -0400410 return false;
David Benjamin31a07792015-03-03 14:20:26 -0500411 }
Martin Kreichgauerabbf3652017-07-21 16:27:54 -0700412 if (in_len + prefix_len + suffix_len > max_out_len) {
Martin Kreichgauer9f2bffb2017-06-30 05:29:50 -0700413 OPENSSL_PUT_ERROR(SSL, SSL_R_BUFFER_TOO_SMALL);
David Benjamincfc11c22017-07-18 22:45:18 -0400414 return false;
Martin Kreichgauer9f2bffb2017-06-30 05:29:50 -0700415 }
416
Martin Kreichgauerabbf3652017-07-21 16:27:54 -0700417 if (!SealScatter(out, out + prefix_len, out + prefix_len + in_len, type,
David Benjamine2ab21d2018-04-04 23:55:06 -0400418 record_version, seqnum, header, in, in_len, 0, 0)) {
David Benjamincfc11c22017-07-18 22:45:18 -0400419 return false;
Martin Kreichgauer9f2bffb2017-06-30 05:29:50 -0700420 }
Martin Kreichgauer9f2bffb2017-06-30 05:29:50 -0700421 *out_len = prefix_len + in_len + suffix_len;
David Benjamincfc11c22017-07-18 22:45:18 -0400422 return true;
423}
424
425bool 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 Benjamin31a07792015-03-03 14:20:26 -0500428}
David Benjamin86e95b82017-07-18 16:34:25 -0400429
Joshua Liebow-Feeser8c7c6352018-08-26 18:53:36 -0700430BSSL_NAMESPACE_END