blob: 8856f7445ee65b7c05349675579f8878a98d8074 [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
David Benjamin86e95b82017-07-18 16:34:25 -040034namespace bssl {
35
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),
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
50SSLAEADContext::~SSLAEADContext() {}
51
Steven Valdezc7d4d212017-09-11 13:53:08 -040052UniquePtr<SSLAEADContext> SSLAEADContext::CreateNullCipher(bool is_dtls) {
53 return MakeUnique<SSLAEADContext>(0 /* version */, is_dtls,
54 nullptr /* cipher */);
David Benjamincfc11c22017-07-18 22:45:18 -040055}
56
57UniquePtr<SSLAEADContext> SSLAEADContext::Create(
58 enum evp_aead_direction_t direction, uint16_t version, int is_dtls,
David Benjaminb9493552017-09-27 19:02:51 -040059 const SSL_CIPHER *cipher, Span<const uint8_t> enc_key,
60 Span<const uint8_t> mac_key, Span<const uint8_t> fixed_iv) {
David Benjamin31a07792015-03-03 14:20:26 -050061 const EVP_AEAD *aead;
Steven Valdezc7d4d212017-09-11 13:53:08 -040062 uint16_t protocol_version;
David Benjamin4b0d0e42016-10-28 17:17:14 -040063 size_t expected_mac_key_len, expected_fixed_iv_len;
Steven Valdezc7d4d212017-09-11 13:53:08 -040064 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 Valdez2f3404b2017-05-24 16:54:35 -040067 is_dtls) ||
David Benjaminc11ea9422017-08-29 16:33:21 -040068 // Ensure the caller returned correct key sizes.
David Benjaminb9493552017-09-27 19:02:51 -040069 expected_fixed_iv_len != fixed_iv.size() ||
70 expected_mac_key_len != mac_key.size()) {
David Benjamin3570d732015-06-29 00:28:17 -040071 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamincfc11c22017-07-18 22:45:18 -040072 return nullptr;
David Benjamin31a07792015-03-03 14:20:26 -050073 }
74
75 uint8_t merged_key[EVP_AEAD_MAX_KEY_LENGTH];
David Benjaminb9493552017-09-27 19:02:51 -040076 if (!mac_key.empty()) {
David Benjaminc11ea9422017-08-29 16:33:21 -040077 // This is a "stateful" AEAD (for compatibility with pre-AEAD cipher
78 // suites).
David Benjaminb9493552017-09-27 19:02:51 -040079 if (mac_key.size() + enc_key.size() + fixed_iv.size() >
80 sizeof(merged_key)) {
David Benjamin3570d732015-06-29 00:28:17 -040081 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamincfc11c22017-07-18 22:45:18 -040082 return nullptr;
David Benjamin31a07792015-03-03 14:20:26 -050083 }
David Benjaminb9493552017-09-27 19:02:51 -040084 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 Benjamin31a07792015-03-03 14:20:26 -050090 }
91
David Benjamincfc11c22017-07-18 22:45:18 -040092 UniquePtr<SSLAEADContext> aead_ctx =
Steven Valdezc7d4d212017-09-11 13:53:08 -040093 MakeUnique<SSLAEADContext>(version, is_dtls, cipher);
David Benjamincfc11c22017-07-18 22:45:18 -040094 if (!aead_ctx) {
David Benjamin3570d732015-06-29 00:28:17 -040095 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamincfc11c22017-07-18 22:45:18 -040096 return nullptr;
David Benjamin31a07792015-03-03 14:20:26 -050097 }
David Benjamin31a07792015-03-03 14:20:26 -050098
Steven Valdezc7d4d212017-09-11 13:53:08 -040099 assert(aead_ctx->ProtocolVersion() == protocol_version);
100
David Benjamin31a07792015-03-03 14:20:26 -0500101 if (!EVP_AEAD_CTX_init_with_direction(
David Benjaminb9493552017-09-27 19:02:51 -0400102 aead_ctx->ctx_.get(), aead, enc_key.data(), enc_key.size(),
David Benjamin31a07792015-03-03 14:20:26 -0500103 EVP_AEAD_DEFAULT_TAG_LENGTH, direction)) {
David Benjamincfc11c22017-07-18 22:45:18 -0400104 return nullptr;
David Benjamin31a07792015-03-03 14:20:26 -0500105 }
106
107 assert(EVP_AEAD_nonce_length(aead) <= EVP_AEAD_MAX_NONCE_LENGTH);
David Benjamina3d76d02017-07-14 19:36:07 -0400108 static_assert(EVP_AEAD_MAX_NONCE_LENGTH < 256,
109 "variable_nonce_len doesn't fit in uint8_t");
David Benjamincfc11c22017-07-18 22:45:18 -0400110 aead_ctx->variable_nonce_len_ = (uint8_t)EVP_AEAD_nonce_length(aead);
David Benjaminb9493552017-09-27 19:02:51 -0400111 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 Benjamin13414b32015-12-09 23:02:39 -0500115
116 if (cipher->algorithm_enc & SSL_CHACHA20POLY1305) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400117 // The fixed nonce into the actual nonce (the sequence number).
David Benjamincfc11c22017-07-18 22:45:18 -0400118 aead_ctx->xor_fixed_nonce_ = true;
119 aead_ctx->variable_nonce_len_ = 8;
David Benjamin13414b32015-12-09 23:02:39 -0500120 } else {
David Benjaminc11ea9422017-08-29 16:33:21 -0400121 // The fixed IV is prepended to the nonce.
David Benjaminb9493552017-09-27 19:02:51 -0400122 assert(fixed_iv.size() <= aead_ctx->variable_nonce_len_);
123 aead_ctx->variable_nonce_len_ -= fixed_iv.size();
David Benjamin13414b32015-12-09 23:02:39 -0500124 }
125
David Benjaminc11ea9422017-08-29 16:33:21 -0400126 // AES-GCM uses an explicit nonce.
David Benjaminb2a985b2015-06-21 15:13:57 -0400127 if (cipher->algorithm_enc & (SSL_AES128GCM | SSL_AES256GCM)) {
David Benjamincfc11c22017-07-18 22:45:18 -0400128 aead_ctx->variable_nonce_included_in_record_ = true;
David Benjaminb2a985b2015-06-21 15:13:57 -0400129 }
Steven Valdez494650c2016-05-24 12:43:04 -0400130
David Benjaminc11ea9422017-08-29 16:33:21 -0400131 // The TLS 1.3 construction XORs the fixed nonce into the sequence number
132 // and omits the additional data.
Steven Valdezc7d4d212017-09-11 13:53:08 -0400133 if (protocol_version >= TLS1_3_VERSION) {
David Benjamincfc11c22017-07-18 22:45:18 -0400134 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 Benjaminb9493552017-09-27 19:02:51 -0400138 assert(fixed_iv.size() >= aead_ctx->variable_nonce_len_);
Steven Valdez494650c2016-05-24 12:43:04 -0400139 }
David Benjamin31a07792015-03-03 14:20:26 -0500140 } else {
Steven Valdezc7d4d212017-09-11 13:53:08 -0400141 assert(protocol_version < TLS1_3_VERSION);
David Benjamincfc11c22017-07-18 22:45:18 -0400142 aead_ctx->variable_nonce_included_in_record_ = true;
143 aead_ctx->random_variable_nonce_ = true;
144 aead_ctx->omit_length_in_ad_ = true;
Steven Valdezc7d4d212017-09-11 13:53:08 -0400145 aead_ctx->omit_version_in_ad_ = (protocol_version == SSL3_VERSION);
David Benjamin31a07792015-03-03 14:20:26 -0500146 }
147
148 return aead_ctx;
149}
150
Steven Valdezc7d4d212017-09-11 13:53:08 -0400151void SSLAEADContext::SetVersionIfNullCipher(uint16_t version) {
152 if (is_null_cipher()) {
153 version_ = version;
154 }
155}
156
157uint16_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
166uint16_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 Benjamincfc11c22017-07-18 22:45:18 -0400182size_t SSLAEADContext::ExplicitNonceLen() const {
183 if (!FUZZER_MODE && variable_nonce_included_in_record_) {
184 return variable_nonce_len_;
David Benjamin31a07792015-03-03 14:20:26 -0500185 }
186 return 0;
187}
188
Martin Kreichgauerabbf3652017-07-21 16:27:54 -0700189bool 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 Kreichgauer9f2bffb2017-06-30 05:29:50 -0700197}
198
David Benjamincfc11c22017-07-18 22:45:18 -0400199size_t SSLAEADContext::MaxOverhead() const {
Martin Kreichgauerabbf3652017-07-21 16:27:54 -0700200 return ExplicitNonceLen() +
201 (is_null_cipher() || FUZZER_MODE
202 ? 0
203 : EVP_AEAD_max_overhead(EVP_AEAD_CTX_aead(ctx_.get())));
David Benjamin31a07792015-03-03 14:20:26 -0500204}
205
David Benjamincfc11c22017-07-18 22:45:18 -0400206size_t SSLAEADContext::GetAdditionalData(uint8_t out[13], uint8_t type,
Steven Valdezc7d4d212017-09-11 13:53:08 -0400207 uint16_t record_version,
David Benjamincfc11c22017-07-18 22:45:18 -0400208 const uint8_t seqnum[8],
209 size_t plaintext_len) {
210 if (omit_ad_) {
Steven Valdez494650c2016-05-24 12:43:04 -0400211 return 0;
212 }
213
David Benjamin17cf2cb2016-12-13 01:07:13 -0500214 OPENSSL_memcpy(out, seqnum, 8);
David Benjamin31a07792015-03-03 14:20:26 -0500215 size_t len = 8;
216 out[len++] = type;
David Benjamincfc11c22017-07-18 22:45:18 -0400217 if (!omit_version_in_ad_) {
Steven Valdezc7d4d212017-09-11 13:53:08 -0400218 out[len++] = static_cast<uint8_t>((record_version >> 8));
219 out[len++] = static_cast<uint8_t>(record_version);
David Benjamin31a07792015-03-03 14:20:26 -0500220 }
David Benjamincfc11c22017-07-18 22:45:18 -0400221 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 Benjamin31a07792015-03-03 14:20:26 -0500224 }
225 return len;
226}
227
Steven Valdezc7d4d212017-09-11 13:53:08 -0400228bool SSLAEADContext::Open(CBS *out, uint8_t type, uint16_t record_version,
David Benjamincfc11c22017-07-18 22:45:18 -0400229 const uint8_t seqnum[8], uint8_t *in, size_t in_len) {
230 if (is_null_cipher() || FUZZER_MODE) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400231 // Handle the initial NULL cipher.
David Benjamina7810c12016-06-06 18:54:51 -0400232 CBS_init(out, in, in_len);
David Benjamincfc11c22017-07-18 22:45:18 -0400233 return true;
David Benjamin31a07792015-03-03 14:20:26 -0500234 }
235
David Benjaminc11ea9422017-08-29 16:33:21 -0400236 // TLS 1.2 AEADs include the length in the AD and are assumed to have fixed
237 // overhead. Otherwise the parameter is unused.
David Benjamin31a07792015-03-03 14:20:26 -0500238 size_t plaintext_len = 0;
David Benjamincfc11c22017-07-18 22:45:18 -0400239 if (!omit_length_in_ad_) {
240 size_t overhead = MaxOverhead();
David Benjamin31a07792015-03-03 14:20:26 -0500241 if (in_len < overhead) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400242 // Publicly invalid.
David Benjamin3570d732015-06-29 00:28:17 -0400243 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_PACKET_LENGTH);
David Benjamincfc11c22017-07-18 22:45:18 -0400244 return false;
David Benjamin31a07792015-03-03 14:20:26 -0500245 }
246 plaintext_len = in_len - overhead;
247 }
248 uint8_t ad[13];
David Benjamincfc11c22017-07-18 22:45:18 -0400249 size_t ad_len =
Steven Valdezc7d4d212017-09-11 13:53:08 -0400250 GetAdditionalData(ad, type, record_version, seqnum, plaintext_len);
David Benjamin31a07792015-03-03 14:20:26 -0500251
David Benjaminc11ea9422017-08-29 16:33:21 -0400252 // Assemble the nonce.
David Benjamin31a07792015-03-03 14:20:26 -0500253 uint8_t nonce[EVP_AEAD_MAX_NONCE_LENGTH];
254 size_t nonce_len = 0;
David Benjamin13414b32015-12-09 23:02:39 -0500255
David Benjaminc11ea9422017-08-29 16:33:21 -0400256 // Prepend the fixed nonce, or left-pad with zeros if XORing.
David Benjamincfc11c22017-07-18 22:45:18 -0400257 if (xor_fixed_nonce_) {
258 nonce_len = fixed_nonce_len_ - variable_nonce_len_;
David Benjamin17cf2cb2016-12-13 01:07:13 -0500259 OPENSSL_memset(nonce, 0, nonce_len);
David Benjamin13414b32015-12-09 23:02:39 -0500260 } else {
David Benjamincfc11c22017-07-18 22:45:18 -0400261 OPENSSL_memcpy(nonce, fixed_nonce_, fixed_nonce_len_);
262 nonce_len += fixed_nonce_len_;
David Benjamin13414b32015-12-09 23:02:39 -0500263 }
264
David Benjaminc11ea9422017-08-29 16:33:21 -0400265 // Add the variable nonce.
David Benjamincfc11c22017-07-18 22:45:18 -0400266 if (variable_nonce_included_in_record_) {
267 if (in_len < variable_nonce_len_) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400268 // Publicly invalid.
David Benjamin3570d732015-06-29 00:28:17 -0400269 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_PACKET_LENGTH);
David Benjamincfc11c22017-07-18 22:45:18 -0400270 return false;
David Benjamin31a07792015-03-03 14:20:26 -0500271 }
David Benjamincfc11c22017-07-18 22:45:18 -0400272 OPENSSL_memcpy(nonce + nonce_len, in, variable_nonce_len_);
273 in += variable_nonce_len_;
274 in_len -= variable_nonce_len_;
David Benjamin31a07792015-03-03 14:20:26 -0500275 } else {
David Benjamincfc11c22017-07-18 22:45:18 -0400276 assert(variable_nonce_len_ == 8);
277 OPENSSL_memcpy(nonce + nonce_len, seqnum, variable_nonce_len_);
David Benjamin31a07792015-03-03 14:20:26 -0500278 }
David Benjamincfc11c22017-07-18 22:45:18 -0400279 nonce_len += variable_nonce_len_;
David Benjamin31a07792015-03-03 14:20:26 -0500280
David Benjaminc11ea9422017-08-29 16:33:21 -0400281 // XOR the fixed nonce, if necessary.
David Benjamincfc11c22017-07-18 22:45:18 -0400282 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 Benjamin13414b32015-12-09 23:02:39 -0500286 }
287 }
288
David Benjaminc11ea9422017-08-29 16:33:21 -0400289 // Decrypt in-place.
David Benjamina7810c12016-06-06 18:54:51 -0400290 size_t len;
David Benjamincfc11c22017-07-18 22:45:18 -0400291 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 Benjamina7810c12016-06-06 18:54:51 -0400294 }
295 CBS_init(out, in, len);
David Benjamincfc11c22017-07-18 22:45:18 -0400296 return true;
David Benjamin31a07792015-03-03 14:20:26 -0500297}
298
David Benjamincfc11c22017-07-18 22:45:18 -0400299bool SSLAEADContext::SealScatter(uint8_t *out_prefix, uint8_t *out,
Martin Kreichgauerabbf3652017-07-21 16:27:54 -0700300 uint8_t *out_suffix, uint8_t type,
Steven Valdezc7d4d212017-09-11 13:53:08 -0400301 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 Kreichgauerabbf3652017-07-21 16:27:54 -0700305 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 Benjamincfc11c22017-07-18 22:45:18 -0400309 return false;
Martin Kreichgauer9f2bffb2017-06-30 05:29:50 -0700310 }
Martin Kreichgauerabbf3652017-07-21 16:27:54 -0700311 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 Benjamincfc11c22017-07-18 22:45:18 -0400315 return false;
Martin Kreichgauer9f2bffb2017-06-30 05:29:50 -0700316 }
317
David Benjamincfc11c22017-07-18 22:45:18 -0400318 if (is_null_cipher() || FUZZER_MODE) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400319 // Handle the initial NULL cipher.
David Benjamin17cf2cb2016-12-13 01:07:13 -0500320 OPENSSL_memmove(out, in, in_len);
Martin Kreichgauer9f2bffb2017-06-30 05:29:50 -0700321 OPENSSL_memmove(out_suffix, extra_in, extra_in_len);
David Benjamincfc11c22017-07-18 22:45:18 -0400322 return true;
David Benjamin31a07792015-03-03 14:20:26 -0500323 }
324
325 uint8_t ad[13];
Steven Valdezc7d4d212017-09-11 13:53:08 -0400326 size_t ad_len = GetAdditionalData(ad, type, record_version, seqnum, in_len);
David Benjamin31a07792015-03-03 14:20:26 -0500327
David Benjaminc11ea9422017-08-29 16:33:21 -0400328 // Assemble the nonce.
David Benjamin31a07792015-03-03 14:20:26 -0500329 uint8_t nonce[EVP_AEAD_MAX_NONCE_LENGTH];
330 size_t nonce_len = 0;
David Benjamin13414b32015-12-09 23:02:39 -0500331
David Benjaminc11ea9422017-08-29 16:33:21 -0400332 // Prepend the fixed nonce, or left-pad with zeros if XORing.
David Benjamincfc11c22017-07-18 22:45:18 -0400333 if (xor_fixed_nonce_) {
334 nonce_len = fixed_nonce_len_ - variable_nonce_len_;
David Benjamin17cf2cb2016-12-13 01:07:13 -0500335 OPENSSL_memset(nonce, 0, nonce_len);
David Benjamin13414b32015-12-09 23:02:39 -0500336 } else {
David Benjamincfc11c22017-07-18 22:45:18 -0400337 OPENSSL_memcpy(nonce, fixed_nonce_, fixed_nonce_len_);
338 nonce_len += fixed_nonce_len_;
David Benjamin13414b32015-12-09 23:02:39 -0500339 }
340
David Benjaminc11ea9422017-08-29 16:33:21 -0400341 // Select the variable nonce.
David Benjamincfc11c22017-07-18 22:45:18 -0400342 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 Benjamin31a07792015-03-03 14:20:26 -0500346 }
347 } else {
David Benjaminc11ea9422017-08-29 16:33:21 -0400348 // When sending we use the sequence number as the variable part of the
349 // nonce.
David Benjamincfc11c22017-07-18 22:45:18 -0400350 assert(variable_nonce_len_ == 8);
351 OPENSSL_memcpy(nonce + nonce_len, seqnum, variable_nonce_len_);
David Benjamin31a07792015-03-03 14:20:26 -0500352 }
David Benjamincfc11c22017-07-18 22:45:18 -0400353 nonce_len += variable_nonce_len_;
David Benjamin31a07792015-03-03 14:20:26 -0500354
David Benjaminc11ea9422017-08-29 16:33:21 -0400355 // Emit the variable nonce if included in the record.
David Benjamincfc11c22017-07-18 22:45:18 -0400356 if (variable_nonce_included_in_record_) {
357 assert(!xor_fixed_nonce_);
358 if (buffers_alias(in, in_len, out_prefix, variable_nonce_len_)) {
David Benjamin3570d732015-06-29 00:28:17 -0400359 OPENSSL_PUT_ERROR(SSL, SSL_R_OUTPUT_ALIASES_INPUT);
David Benjamincfc11c22017-07-18 22:45:18 -0400360 return false;
David Benjamin31a07792015-03-03 14:20:26 -0500361 }
David Benjamincfc11c22017-07-18 22:45:18 -0400362 OPENSSL_memcpy(out_prefix, nonce + fixed_nonce_len_,
363 variable_nonce_len_);
David Benjamin31a07792015-03-03 14:20:26 -0500364 }
365
David Benjaminc11ea9422017-08-29 16:33:21 -0400366 // XOR the fixed nonce, if necessary.
David Benjamincfc11c22017-07-18 22:45:18 -0400367 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 Benjamin13414b32015-12-09 23:02:39 -0500371 }
372 }
373
Martin Kreichgauerabbf3652017-07-21 16:27:54 -0700374 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 Benjamincfc11c22017-07-18 22:45:18 -0400377 nonce_len, in, in_len, extra_in, extra_in_len, ad, ad_len);
Martin Kreichgauerabbf3652017-07-21 16:27:54 -0700378 assert(!result || written_suffix_len == suffix_len);
379 return result;
Martin Kreichgauer9f2bffb2017-06-30 05:29:50 -0700380}
381
David Benjamincfc11c22017-07-18 22:45:18 -0400382bool SSLAEADContext::Seal(uint8_t *out, size_t *out_len, size_t max_out_len,
Steven Valdezc7d4d212017-09-11 13:53:08 -0400383 uint8_t type, uint16_t record_version,
David Benjamincfc11c22017-07-18 22:45:18 -0400384 const uint8_t seqnum[8], const uint8_t *in,
385 size_t in_len) {
Martin Kreichgauerabbf3652017-07-21 16:27:54 -0700386 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 Kreichgauer9f2bffb2017-06-30 05:29:50 -0700394 OPENSSL_PUT_ERROR(CIPHER, SSL_R_RECORD_TOO_LARGE);
David Benjamincfc11c22017-07-18 22:45:18 -0400395 return false;
David Benjamin31a07792015-03-03 14:20:26 -0500396 }
Martin Kreichgauerabbf3652017-07-21 16:27:54 -0700397 if (in_len + prefix_len + suffix_len > max_out_len) {
Martin Kreichgauer9f2bffb2017-06-30 05:29:50 -0700398 OPENSSL_PUT_ERROR(SSL, SSL_R_BUFFER_TOO_SMALL);
David Benjamincfc11c22017-07-18 22:45:18 -0400399 return false;
Martin Kreichgauer9f2bffb2017-06-30 05:29:50 -0700400 }
401
Martin Kreichgauerabbf3652017-07-21 16:27:54 -0700402 if (!SealScatter(out, out + prefix_len, out + prefix_len + in_len, type,
Steven Valdezc7d4d212017-09-11 13:53:08 -0400403 record_version, seqnum, in, in_len, 0, 0)) {
David Benjamincfc11c22017-07-18 22:45:18 -0400404 return false;
Martin Kreichgauer9f2bffb2017-06-30 05:29:50 -0700405 }
Martin Kreichgauer9f2bffb2017-06-30 05:29:50 -0700406 *out_len = prefix_len + in_len + suffix_len;
David Benjamincfc11c22017-07-18 22:45:18 -0400407 return true;
408}
409
410bool 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 Benjamin31a07792015-03-03 14:20:26 -0500413}
David Benjamin86e95b82017-07-18 16:34:25 -0400414
415} // namespace bssl