blob: d03a4a0548ab53c6d4944f8dd351d5af4a8c35d1 [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,
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 Valdezc7d4d212017-09-11 13:53:08 -040062
David Benjamin31a07792015-03-03 14:20:26 -050063 const EVP_AEAD *aead;
Steven Valdezc7d4d212017-09-11 13:53:08 -040064 uint16_t protocol_version;
David Benjamin4b0d0e42016-10-28 17:17:14 -040065 size_t expected_mac_key_len, expected_fixed_iv_len;
Steven Valdezc7d4d212017-09-11 13:53:08 -040066 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 Valdez2f3404b2017-05-24 16:54:35 -040069 is_dtls) ||
David Benjaminc11ea9422017-08-29 16:33:21 -040070 // Ensure the caller returned correct key sizes.
David Benjamin4b0d0e42016-10-28 17:17:14 -040071 expected_fixed_iv_len != fixed_iv_len ||
72 expected_mac_key_len != mac_key_len) {
David Benjamin3570d732015-06-29 00:28:17 -040073 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamincfc11c22017-07-18 22:45:18 -040074 return nullptr;
David Benjamin31a07792015-03-03 14:20:26 -050075 }
76
77 uint8_t merged_key[EVP_AEAD_MAX_KEY_LENGTH];
78 if (mac_key_len > 0) {
David Benjaminc11ea9422017-08-29 16:33:21 -040079 // This is a "stateful" AEAD (for compatibility with pre-AEAD cipher
80 // suites).
David Benjamin31a07792015-03-03 14:20:26 -050081 if (mac_key_len + enc_key_len + fixed_iv_len > sizeof(merged_key)) {
David Benjamin3570d732015-06-29 00:28:17 -040082 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamincfc11c22017-07-18 22:45:18 -040083 return nullptr;
David Benjamin31a07792015-03-03 14:20:26 -050084 }
David Benjamin17cf2cb2016-12-13 01:07:13 -050085 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 Benjamin31a07792015-03-03 14:20:26 -050089 enc_key = merged_key;
90 enc_key_len += mac_key_len;
91 enc_key_len += fixed_iv_len;
92 }
93
David Benjamincfc11c22017-07-18 22:45:18 -040094 UniquePtr<SSLAEADContext> aead_ctx =
Steven Valdezc7d4d212017-09-11 13:53:08 -040095 MakeUnique<SSLAEADContext>(version, is_dtls, cipher);
David Benjamincfc11c22017-07-18 22:45:18 -040096 if (!aead_ctx) {
David Benjamin3570d732015-06-29 00:28:17 -040097 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamincfc11c22017-07-18 22:45:18 -040098 return nullptr;
David Benjamin31a07792015-03-03 14:20:26 -050099 }
David Benjamin31a07792015-03-03 14:20:26 -0500100
Steven Valdezc7d4d212017-09-11 13:53:08 -0400101 assert(aead_ctx->ProtocolVersion() == protocol_version);
102
David Benjamin31a07792015-03-03 14:20:26 -0500103 if (!EVP_AEAD_CTX_init_with_direction(
David Benjamincfc11c22017-07-18 22:45:18 -0400104 aead_ctx->ctx_.get(), aead, enc_key, enc_key_len,
David Benjamin31a07792015-03-03 14:20:26 -0500105 EVP_AEAD_DEFAULT_TAG_LENGTH, direction)) {
David Benjamincfc11c22017-07-18 22:45:18 -0400106 return nullptr;
David Benjamin31a07792015-03-03 14:20:26 -0500107 }
108
109 assert(EVP_AEAD_nonce_length(aead) <= EVP_AEAD_MAX_NONCE_LENGTH);
David Benjamina3d76d02017-07-14 19:36:07 -0400110 static_assert(EVP_AEAD_MAX_NONCE_LENGTH < 256,
111 "variable_nonce_len doesn't fit in uint8_t");
David Benjamincfc11c22017-07-18 22:45:18 -0400112 aead_ctx->variable_nonce_len_ = (uint8_t)EVP_AEAD_nonce_length(aead);
David Benjamin51a01a52015-10-29 13:19:56 -0400113 if (mac_key_len == 0) {
David Benjamincfc11c22017-07-18 22:45:18 -0400114 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 Benjamin13414b32015-12-09 23:02:39 -0500117
118 if (cipher->algorithm_enc & SSL_CHACHA20POLY1305) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400119 // The fixed nonce into the actual nonce (the sequence number).
David Benjamincfc11c22017-07-18 22:45:18 -0400120 aead_ctx->xor_fixed_nonce_ = true;
121 aead_ctx->variable_nonce_len_ = 8;
David Benjamin13414b32015-12-09 23:02:39 -0500122 } else {
David Benjaminc11ea9422017-08-29 16:33:21 -0400123 // The fixed IV is prepended to the nonce.
David Benjamincfc11c22017-07-18 22:45:18 -0400124 assert(fixed_iv_len <= aead_ctx->variable_nonce_len_);
125 aead_ctx->variable_nonce_len_ -= fixed_iv_len;
David Benjamin13414b32015-12-09 23:02:39 -0500126 }
127
David Benjaminc11ea9422017-08-29 16:33:21 -0400128 // AES-GCM uses an explicit nonce.
David Benjaminb2a985b2015-06-21 15:13:57 -0400129 if (cipher->algorithm_enc & (SSL_AES128GCM | SSL_AES256GCM)) {
David Benjamincfc11c22017-07-18 22:45:18 -0400130 aead_ctx->variable_nonce_included_in_record_ = true;
David Benjaminb2a985b2015-06-21 15:13:57 -0400131 }
Steven Valdez494650c2016-05-24 12:43:04 -0400132
David Benjaminc11ea9422017-08-29 16:33:21 -0400133 // The TLS 1.3 construction XORs the fixed nonce into the sequence number
134 // and omits the additional data.
Steven Valdezc7d4d212017-09-11 13:53:08 -0400135 if (protocol_version >= TLS1_3_VERSION) {
David Benjamincfc11c22017-07-18 22:45:18 -0400136 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 Valdez494650c2016-05-24 12:43:04 -0400141 }
David Benjamin31a07792015-03-03 14:20:26 -0500142 } else {
Steven Valdezc7d4d212017-09-11 13:53:08 -0400143 assert(protocol_version < TLS1_3_VERSION);
David Benjamincfc11c22017-07-18 22:45:18 -0400144 aead_ctx->variable_nonce_included_in_record_ = true;
145 aead_ctx->random_variable_nonce_ = true;
146 aead_ctx->omit_length_in_ad_ = true;
Steven Valdezc7d4d212017-09-11 13:53:08 -0400147 aead_ctx->omit_version_in_ad_ = (protocol_version == SSL3_VERSION);
David Benjamin31a07792015-03-03 14:20:26 -0500148 }
149
150 return aead_ctx;
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
178 if (ssl_is_resumption_record_version_experiment(version_)) {
179 return TLS1_2_VERSION;
180 }
181 return TLS1_VERSION;
182}
183
David Benjamincfc11c22017-07-18 22:45:18 -0400184size_t SSLAEADContext::ExplicitNonceLen() const {
185 if (!FUZZER_MODE && variable_nonce_included_in_record_) {
186 return variable_nonce_len_;
David Benjamin31a07792015-03-03 14:20:26 -0500187 }
188 return 0;
189}
190
Martin Kreichgauerabbf3652017-07-21 16:27:54 -0700191bool 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 Kreichgauer9f2bffb2017-06-30 05:29:50 -0700199}
200
David Benjamincfc11c22017-07-18 22:45:18 -0400201size_t SSLAEADContext::MaxOverhead() const {
Martin Kreichgauerabbf3652017-07-21 16:27:54 -0700202 return ExplicitNonceLen() +
203 (is_null_cipher() || FUZZER_MODE
204 ? 0
205 : EVP_AEAD_max_overhead(EVP_AEAD_CTX_aead(ctx_.get())));
David Benjamin31a07792015-03-03 14:20:26 -0500206}
207
David Benjamincfc11c22017-07-18 22:45:18 -0400208size_t SSLAEADContext::GetAdditionalData(uint8_t out[13], uint8_t type,
Steven Valdezc7d4d212017-09-11 13:53:08 -0400209 uint16_t record_version,
David Benjamincfc11c22017-07-18 22:45:18 -0400210 const uint8_t seqnum[8],
211 size_t plaintext_len) {
212 if (omit_ad_) {
Steven Valdez494650c2016-05-24 12:43:04 -0400213 return 0;
214 }
215
David Benjamin17cf2cb2016-12-13 01:07:13 -0500216 OPENSSL_memcpy(out, seqnum, 8);
David Benjamin31a07792015-03-03 14:20:26 -0500217 size_t len = 8;
218 out[len++] = type;
David Benjamincfc11c22017-07-18 22:45:18 -0400219 if (!omit_version_in_ad_) {
Steven Valdezc7d4d212017-09-11 13:53:08 -0400220 out[len++] = static_cast<uint8_t>((record_version >> 8));
221 out[len++] = static_cast<uint8_t>(record_version);
David Benjamin31a07792015-03-03 14:20:26 -0500222 }
David Benjamincfc11c22017-07-18 22:45:18 -0400223 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 Benjamin31a07792015-03-03 14:20:26 -0500226 }
227 return len;
228}
229
Steven Valdezc7d4d212017-09-11 13:53:08 -0400230bool SSLAEADContext::Open(CBS *out, uint8_t type, uint16_t record_version,
David Benjamincfc11c22017-07-18 22:45:18 -0400231 const uint8_t seqnum[8], uint8_t *in, size_t in_len) {
232 if (is_null_cipher() || FUZZER_MODE) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400233 // Handle the initial NULL cipher.
David Benjamina7810c12016-06-06 18:54:51 -0400234 CBS_init(out, in, in_len);
David Benjamincfc11c22017-07-18 22:45:18 -0400235 return true;
David Benjamin31a07792015-03-03 14:20:26 -0500236 }
237
David Benjaminc11ea9422017-08-29 16:33:21 -0400238 // TLS 1.2 AEADs include the length in the AD and are assumed to have fixed
239 // overhead. Otherwise the parameter is unused.
David Benjamin31a07792015-03-03 14:20:26 -0500240 size_t plaintext_len = 0;
David Benjamincfc11c22017-07-18 22:45:18 -0400241 if (!omit_length_in_ad_) {
242 size_t overhead = MaxOverhead();
David Benjamin31a07792015-03-03 14:20:26 -0500243 if (in_len < overhead) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400244 // Publicly invalid.
David Benjamin3570d732015-06-29 00:28:17 -0400245 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_PACKET_LENGTH);
David Benjamincfc11c22017-07-18 22:45:18 -0400246 return false;
David Benjamin31a07792015-03-03 14:20:26 -0500247 }
248 plaintext_len = in_len - overhead;
249 }
250 uint8_t ad[13];
David Benjamincfc11c22017-07-18 22:45:18 -0400251 size_t ad_len =
Steven Valdezc7d4d212017-09-11 13:53:08 -0400252 GetAdditionalData(ad, type, record_version, seqnum, plaintext_len);
David Benjamin31a07792015-03-03 14:20:26 -0500253
David Benjaminc11ea9422017-08-29 16:33:21 -0400254 // Assemble the nonce.
David Benjamin31a07792015-03-03 14:20:26 -0500255 uint8_t nonce[EVP_AEAD_MAX_NONCE_LENGTH];
256 size_t nonce_len = 0;
David Benjamin13414b32015-12-09 23:02:39 -0500257
David Benjaminc11ea9422017-08-29 16:33:21 -0400258 // Prepend the fixed nonce, or left-pad with zeros if XORing.
David Benjamincfc11c22017-07-18 22:45:18 -0400259 if (xor_fixed_nonce_) {
260 nonce_len = fixed_nonce_len_ - variable_nonce_len_;
David Benjamin17cf2cb2016-12-13 01:07:13 -0500261 OPENSSL_memset(nonce, 0, nonce_len);
David Benjamin13414b32015-12-09 23:02:39 -0500262 } else {
David Benjamincfc11c22017-07-18 22:45:18 -0400263 OPENSSL_memcpy(nonce, fixed_nonce_, fixed_nonce_len_);
264 nonce_len += fixed_nonce_len_;
David Benjamin13414b32015-12-09 23:02:39 -0500265 }
266
David Benjaminc11ea9422017-08-29 16:33:21 -0400267 // Add the variable nonce.
David Benjamincfc11c22017-07-18 22:45:18 -0400268 if (variable_nonce_included_in_record_) {
269 if (in_len < variable_nonce_len_) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400270 // Publicly invalid.
David Benjamin3570d732015-06-29 00:28:17 -0400271 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_PACKET_LENGTH);
David Benjamincfc11c22017-07-18 22:45:18 -0400272 return false;
David Benjamin31a07792015-03-03 14:20:26 -0500273 }
David Benjamincfc11c22017-07-18 22:45:18 -0400274 OPENSSL_memcpy(nonce + nonce_len, in, variable_nonce_len_);
275 in += variable_nonce_len_;
276 in_len -= variable_nonce_len_;
David Benjamin31a07792015-03-03 14:20:26 -0500277 } else {
David Benjamincfc11c22017-07-18 22:45:18 -0400278 assert(variable_nonce_len_ == 8);
279 OPENSSL_memcpy(nonce + nonce_len, seqnum, variable_nonce_len_);
David Benjamin31a07792015-03-03 14:20:26 -0500280 }
David Benjamincfc11c22017-07-18 22:45:18 -0400281 nonce_len += variable_nonce_len_;
David Benjamin31a07792015-03-03 14:20:26 -0500282
David Benjaminc11ea9422017-08-29 16:33:21 -0400283 // XOR the fixed nonce, if necessary.
David Benjamincfc11c22017-07-18 22:45:18 -0400284 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 Benjamin13414b32015-12-09 23:02:39 -0500288 }
289 }
290
David Benjaminc11ea9422017-08-29 16:33:21 -0400291 // Decrypt in-place.
David Benjamina7810c12016-06-06 18:54:51 -0400292 size_t len;
David Benjamincfc11c22017-07-18 22:45:18 -0400293 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 Benjamina7810c12016-06-06 18:54:51 -0400296 }
297 CBS_init(out, in, len);
David Benjamincfc11c22017-07-18 22:45:18 -0400298 return true;
David Benjamin31a07792015-03-03 14:20:26 -0500299}
300
David Benjamincfc11c22017-07-18 22:45:18 -0400301bool SSLAEADContext::SealScatter(uint8_t *out_prefix, uint8_t *out,
Martin Kreichgauerabbf3652017-07-21 16:27:54 -0700302 uint8_t *out_suffix, uint8_t type,
Steven Valdezc7d4d212017-09-11 13:53:08 -0400303 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 Kreichgauerabbf3652017-07-21 16:27:54 -0700307 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 Benjamincfc11c22017-07-18 22:45:18 -0400311 return false;
Martin Kreichgauer9f2bffb2017-06-30 05:29:50 -0700312 }
Martin Kreichgauerabbf3652017-07-21 16:27:54 -0700313 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 Benjamincfc11c22017-07-18 22:45:18 -0400317 return false;
Martin Kreichgauer9f2bffb2017-06-30 05:29:50 -0700318 }
319
David Benjamincfc11c22017-07-18 22:45:18 -0400320 if (is_null_cipher() || FUZZER_MODE) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400321 // Handle the initial NULL cipher.
David Benjamin17cf2cb2016-12-13 01:07:13 -0500322 OPENSSL_memmove(out, in, in_len);
Martin Kreichgauer9f2bffb2017-06-30 05:29:50 -0700323 OPENSSL_memmove(out_suffix, extra_in, extra_in_len);
David Benjamincfc11c22017-07-18 22:45:18 -0400324 return true;
David Benjamin31a07792015-03-03 14:20:26 -0500325 }
326
327 uint8_t ad[13];
Steven Valdezc7d4d212017-09-11 13:53:08 -0400328 size_t ad_len = GetAdditionalData(ad, type, record_version, seqnum, in_len);
David Benjamin31a07792015-03-03 14:20:26 -0500329
David Benjaminc11ea9422017-08-29 16:33:21 -0400330 // Assemble the nonce.
David Benjamin31a07792015-03-03 14:20:26 -0500331 uint8_t nonce[EVP_AEAD_MAX_NONCE_LENGTH];
332 size_t nonce_len = 0;
David Benjamin13414b32015-12-09 23:02:39 -0500333
David Benjaminc11ea9422017-08-29 16:33:21 -0400334 // Prepend the fixed nonce, or left-pad with zeros if XORing.
David Benjamincfc11c22017-07-18 22:45:18 -0400335 if (xor_fixed_nonce_) {
336 nonce_len = fixed_nonce_len_ - variable_nonce_len_;
David Benjamin17cf2cb2016-12-13 01:07:13 -0500337 OPENSSL_memset(nonce, 0, nonce_len);
David Benjamin13414b32015-12-09 23:02:39 -0500338 } else {
David Benjamincfc11c22017-07-18 22:45:18 -0400339 OPENSSL_memcpy(nonce, fixed_nonce_, fixed_nonce_len_);
340 nonce_len += fixed_nonce_len_;
David Benjamin13414b32015-12-09 23:02:39 -0500341 }
342
David Benjaminc11ea9422017-08-29 16:33:21 -0400343 // Select the variable nonce.
David Benjamincfc11c22017-07-18 22:45:18 -0400344 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 Benjamin31a07792015-03-03 14:20:26 -0500348 }
349 } else {
David Benjaminc11ea9422017-08-29 16:33:21 -0400350 // When sending we use the sequence number as the variable part of the
351 // nonce.
David Benjamincfc11c22017-07-18 22:45:18 -0400352 assert(variable_nonce_len_ == 8);
353 OPENSSL_memcpy(nonce + nonce_len, seqnum, variable_nonce_len_);
David Benjamin31a07792015-03-03 14:20:26 -0500354 }
David Benjamincfc11c22017-07-18 22:45:18 -0400355 nonce_len += variable_nonce_len_;
David Benjamin31a07792015-03-03 14:20:26 -0500356
David Benjaminc11ea9422017-08-29 16:33:21 -0400357 // Emit the variable nonce if included in the record.
David Benjamincfc11c22017-07-18 22:45:18 -0400358 if (variable_nonce_included_in_record_) {
359 assert(!xor_fixed_nonce_);
360 if (buffers_alias(in, in_len, out_prefix, variable_nonce_len_)) {
David Benjamin3570d732015-06-29 00:28:17 -0400361 OPENSSL_PUT_ERROR(SSL, SSL_R_OUTPUT_ALIASES_INPUT);
David Benjamincfc11c22017-07-18 22:45:18 -0400362 return false;
David Benjamin31a07792015-03-03 14:20:26 -0500363 }
David Benjamincfc11c22017-07-18 22:45:18 -0400364 OPENSSL_memcpy(out_prefix, nonce + fixed_nonce_len_,
365 variable_nonce_len_);
David Benjamin31a07792015-03-03 14:20:26 -0500366 }
367
David Benjaminc11ea9422017-08-29 16:33:21 -0400368 // XOR the fixed nonce, if necessary.
David Benjamincfc11c22017-07-18 22:45:18 -0400369 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 Benjamin13414b32015-12-09 23:02:39 -0500373 }
374 }
375
Martin Kreichgauerabbf3652017-07-21 16:27:54 -0700376 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 Benjamincfc11c22017-07-18 22:45:18 -0400379 nonce_len, in, in_len, extra_in, extra_in_len, ad, ad_len);
Martin Kreichgauerabbf3652017-07-21 16:27:54 -0700380 assert(!result || written_suffix_len == suffix_len);
381 return result;
Martin Kreichgauer9f2bffb2017-06-30 05:29:50 -0700382}
383
David Benjamincfc11c22017-07-18 22:45:18 -0400384bool SSLAEADContext::Seal(uint8_t *out, size_t *out_len, size_t max_out_len,
Steven Valdezc7d4d212017-09-11 13:53:08 -0400385 uint8_t type, uint16_t record_version,
David Benjamincfc11c22017-07-18 22:45:18 -0400386 const uint8_t seqnum[8], const uint8_t *in,
387 size_t in_len) {
Martin Kreichgauerabbf3652017-07-21 16:27:54 -0700388 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 Kreichgauer9f2bffb2017-06-30 05:29:50 -0700396 OPENSSL_PUT_ERROR(CIPHER, SSL_R_RECORD_TOO_LARGE);
David Benjamincfc11c22017-07-18 22:45:18 -0400397 return false;
David Benjamin31a07792015-03-03 14:20:26 -0500398 }
Martin Kreichgauerabbf3652017-07-21 16:27:54 -0700399 if (in_len + prefix_len + suffix_len > max_out_len) {
Martin Kreichgauer9f2bffb2017-06-30 05:29:50 -0700400 OPENSSL_PUT_ERROR(SSL, SSL_R_BUFFER_TOO_SMALL);
David Benjamincfc11c22017-07-18 22:45:18 -0400401 return false;
Martin Kreichgauer9f2bffb2017-06-30 05:29:50 -0700402 }
403
Martin Kreichgauerabbf3652017-07-21 16:27:54 -0700404 if (!SealScatter(out, out + prefix_len, out + prefix_len + in_len, type,
Steven Valdezc7d4d212017-09-11 13:53:08 -0400405 record_version, seqnum, in, in_len, 0, 0)) {
David Benjamincfc11c22017-07-18 22:45:18 -0400406 return false;
Martin Kreichgauer9f2bffb2017-06-30 05:29:50 -0700407 }
Martin Kreichgauer9f2bffb2017-06-30 05:29:50 -0700408 *out_len = prefix_len + in_len + suffix_len;
David Benjamincfc11c22017-07-18 22:45:18 -0400409 return true;
410}
411
412bool 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 Benjamin31a07792015-03-03 14:20:26 -0500415}
David Benjamin86e95b82017-07-18 16:34:25 -0400416
417} // namespace bssl