blob: b78b06b5b25ee604760daf00dcf23f214eb3599b [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>
23#include <openssl/type_check.h>
24
David Benjamin17cf2cb2016-12-13 01:07:13 -050025#include "../crypto/internal.h"
David Benjamin31a07792015-03-03 14:20:26 -050026#include "internal.h"
27
28
David Benjamin31a07792015-03-03 14:20:26 -050029SSL_AEAD_CTX *SSL_AEAD_CTX_new(enum evp_aead_direction_t direction,
Steven Valdez2f3404b2017-05-24 16:54:35 -040030 uint16_t version, int is_dtls,
31 const SSL_CIPHER *cipher, const uint8_t *enc_key,
32 size_t enc_key_len, const uint8_t *mac_key,
33 size_t mac_key_len, const uint8_t *fixed_iv,
34 size_t fixed_iv_len) {
David Benjamin31a07792015-03-03 14:20:26 -050035 const EVP_AEAD *aead;
David Benjamin4b0d0e42016-10-28 17:17:14 -040036 size_t expected_mac_key_len, expected_fixed_iv_len;
37 if (!ssl_cipher_get_evp_aead(&aead, &expected_mac_key_len,
Steven Valdez2f3404b2017-05-24 16:54:35 -040038 &expected_fixed_iv_len, cipher, version,
39 is_dtls) ||
David Benjamin4b0d0e42016-10-28 17:17:14 -040040 /* Ensure the caller returned correct key sizes. */
41 expected_fixed_iv_len != fixed_iv_len ||
42 expected_mac_key_len != mac_key_len) {
David Benjamin3570d732015-06-29 00:28:17 -040043 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamin31a07792015-03-03 14:20:26 -050044 return 0;
45 }
46
47 uint8_t merged_key[EVP_AEAD_MAX_KEY_LENGTH];
48 if (mac_key_len > 0) {
49 /* This is a "stateful" AEAD (for compatibility with pre-AEAD cipher
50 * suites). */
51 if (mac_key_len + enc_key_len + fixed_iv_len > sizeof(merged_key)) {
David Benjamin3570d732015-06-29 00:28:17 -040052 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamin31a07792015-03-03 14:20:26 -050053 return 0;
54 }
David Benjamin17cf2cb2016-12-13 01:07:13 -050055 OPENSSL_memcpy(merged_key, mac_key, mac_key_len);
56 OPENSSL_memcpy(merged_key + mac_key_len, enc_key, enc_key_len);
57 OPENSSL_memcpy(merged_key + mac_key_len + enc_key_len, fixed_iv,
58 fixed_iv_len);
David Benjamin31a07792015-03-03 14:20:26 -050059 enc_key = merged_key;
60 enc_key_len += mac_key_len;
61 enc_key_len += fixed_iv_len;
62 }
63
David Benjaminf5260812017-07-12 17:16:39 -040064 SSL_AEAD_CTX *aead_ctx = (SSL_AEAD_CTX *)OPENSSL_malloc(sizeof(SSL_AEAD_CTX));
David Benjamin31a07792015-03-03 14:20:26 -050065 if (aead_ctx == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -040066 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin31a07792015-03-03 14:20:26 -050067 return NULL;
68 }
David Benjamin17cf2cb2016-12-13 01:07:13 -050069 OPENSSL_memset(aead_ctx, 0, sizeof(SSL_AEAD_CTX));
David Benjamin31a07792015-03-03 14:20:26 -050070 aead_ctx->cipher = cipher;
Steven Valdez130d5292017-03-07 16:58:04 -050071 aead_ctx->version = version;
David Benjamin31a07792015-03-03 14:20:26 -050072
73 if (!EVP_AEAD_CTX_init_with_direction(
74 &aead_ctx->ctx, aead, enc_key, enc_key_len,
75 EVP_AEAD_DEFAULT_TAG_LENGTH, direction)) {
76 OPENSSL_free(aead_ctx);
77 return NULL;
78 }
79
80 assert(EVP_AEAD_nonce_length(aead) <= EVP_AEAD_MAX_NONCE_LENGTH);
David Benjamin5db7c9b2017-01-24 16:17:03 -050081 OPENSSL_COMPILE_ASSERT(EVP_AEAD_MAX_NONCE_LENGTH < 256,
82 variable_nonce_len_doesnt_fit_in_uint8_t);
David Benjamin31a07792015-03-03 14:20:26 -050083 aead_ctx->variable_nonce_len = (uint8_t)EVP_AEAD_nonce_length(aead);
David Benjamin51a01a52015-10-29 13:19:56 -040084 if (mac_key_len == 0) {
David Benjamin13414b32015-12-09 23:02:39 -050085 assert(fixed_iv_len <= sizeof(aead_ctx->fixed_nonce));
David Benjamin17cf2cb2016-12-13 01:07:13 -050086 OPENSSL_memcpy(aead_ctx->fixed_nonce, fixed_iv, fixed_iv_len);
David Benjamin31a07792015-03-03 14:20:26 -050087 aead_ctx->fixed_nonce_len = fixed_iv_len;
David Benjamin13414b32015-12-09 23:02:39 -050088
89 if (cipher->algorithm_enc & SSL_CHACHA20POLY1305) {
90 /* The fixed nonce into the actual nonce (the sequence number). */
91 aead_ctx->xor_fixed_nonce = 1;
92 aead_ctx->variable_nonce_len = 8;
93 } else {
94 /* The fixed IV is prepended to the nonce. */
95 assert(fixed_iv_len <= aead_ctx->variable_nonce_len);
96 aead_ctx->variable_nonce_len -= fixed_iv_len;
97 }
98
David Benjaminb2a985b2015-06-21 15:13:57 -040099 /* AES-GCM uses an explicit nonce. */
100 if (cipher->algorithm_enc & (SSL_AES128GCM | SSL_AES256GCM)) {
101 aead_ctx->variable_nonce_included_in_record = 1;
102 }
Steven Valdez494650c2016-05-24 12:43:04 -0400103
104 /* The TLS 1.3 construction XORs the fixed nonce into the sequence number
105 * and omits the additional data. */
106 if (version >= TLS1_3_VERSION) {
107 aead_ctx->xor_fixed_nonce = 1;
108 aead_ctx->variable_nonce_len = 8;
109 aead_ctx->variable_nonce_included_in_record = 0;
110 aead_ctx->omit_ad = 1;
Steven Valdez79750562016-06-16 06:38:04 -0400111 assert(fixed_iv_len >= aead_ctx->variable_nonce_len);
Steven Valdez494650c2016-05-24 12:43:04 -0400112 }
David Benjamin31a07792015-03-03 14:20:26 -0500113 } else {
Steven Valdez79750562016-06-16 06:38:04 -0400114 assert(version < TLS1_3_VERSION);
David Benjamin31a07792015-03-03 14:20:26 -0500115 aead_ctx->variable_nonce_included_in_record = 1;
116 aead_ctx->random_variable_nonce = 1;
117 aead_ctx->omit_length_in_ad = 1;
118 aead_ctx->omit_version_in_ad = (version == SSL3_VERSION);
119 }
120
121 return aead_ctx;
122}
123
124void SSL_AEAD_CTX_free(SSL_AEAD_CTX *aead) {
125 if (aead == NULL) {
126 return;
127 }
128 EVP_AEAD_CTX_cleanup(&aead->ctx);
129 OPENSSL_free(aead);
130}
131
David Benjamina772b162017-01-24 17:51:33 -0500132size_t SSL_AEAD_CTX_explicit_nonce_len(const SSL_AEAD_CTX *aead) {
David Benjaminbf82aed2016-03-01 22:57:40 -0500133#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
134 aead = NULL;
135#endif
136
David Benjamin31a07792015-03-03 14:20:26 -0500137 if (aead != NULL && aead->variable_nonce_included_in_record) {
138 return aead->variable_nonce_len;
139 }
140 return 0;
141}
142
Martin Kreichgauer9f2bffb2017-06-30 05:29:50 -0700143size_t SSL_AEAD_CTX_max_suffix_len(const SSL_AEAD_CTX *aead,
144 size_t extra_in_len) {
David Benjaminbf82aed2016-03-01 22:57:40 -0500145#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
146 aead = NULL;
147#endif
148
Martin Kreichgauer9f2bffb2017-06-30 05:29:50 -0700149 return extra_in_len +
150 (aead == NULL ? 0 : EVP_AEAD_max_overhead(aead->ctx.aead));
151}
152
153size_t SSL_AEAD_CTX_max_overhead(const SSL_AEAD_CTX *aead) {
154 return SSL_AEAD_CTX_explicit_nonce_len(aead) +
155 SSL_AEAD_CTX_max_suffix_len(aead, 0);
David Benjamin31a07792015-03-03 14:20:26 -0500156}
157
158/* ssl_aead_ctx_get_ad writes the additional data for |aead| into |out| and
159 * returns the number of bytes written. */
160static size_t ssl_aead_ctx_get_ad(SSL_AEAD_CTX *aead, uint8_t out[13],
161 uint8_t type, uint16_t wire_version,
162 const uint8_t seqnum[8],
163 size_t plaintext_len) {
Steven Valdez494650c2016-05-24 12:43:04 -0400164 if (aead->omit_ad) {
165 return 0;
166 }
167
David Benjamin17cf2cb2016-12-13 01:07:13 -0500168 OPENSSL_memcpy(out, seqnum, 8);
David Benjamin31a07792015-03-03 14:20:26 -0500169 size_t len = 8;
170 out[len++] = type;
171 if (!aead->omit_version_in_ad) {
172 out[len++] = (uint8_t)(wire_version >> 8);
173 out[len++] = (uint8_t)wire_version;
174 }
175 if (!aead->omit_length_in_ad) {
176 out[len++] = (uint8_t)(plaintext_len >> 8);
177 out[len++] = (uint8_t)plaintext_len;
178 }
179 return len;
180}
181
David Benjamina7810c12016-06-06 18:54:51 -0400182int SSL_AEAD_CTX_open(SSL_AEAD_CTX *aead, CBS *out, uint8_t type,
183 uint16_t wire_version, const uint8_t seqnum[8],
184 uint8_t *in, size_t in_len) {
David Benjaminbf82aed2016-03-01 22:57:40 -0500185#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
186 aead = NULL;
187#endif
188
David Benjamin31a07792015-03-03 14:20:26 -0500189 if (aead == NULL) {
190 /* Handle the initial NULL cipher. */
David Benjamina7810c12016-06-06 18:54:51 -0400191 CBS_init(out, in, in_len);
David Benjamin31a07792015-03-03 14:20:26 -0500192 return 1;
193 }
194
195 /* TLS 1.2 AEADs include the length in the AD and are assumed to have fixed
196 * overhead. Otherwise the parameter is unused. */
197 size_t plaintext_len = 0;
198 if (!aead->omit_length_in_ad) {
199 size_t overhead = SSL_AEAD_CTX_max_overhead(aead);
200 if (in_len < overhead) {
201 /* Publicly invalid. */
David Benjamin3570d732015-06-29 00:28:17 -0400202 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_PACKET_LENGTH);
David Benjamin31a07792015-03-03 14:20:26 -0500203 return 0;
204 }
205 plaintext_len = in_len - overhead;
206 }
207 uint8_t ad[13];
208 size_t ad_len = ssl_aead_ctx_get_ad(aead, ad, type, wire_version, seqnum,
209 plaintext_len);
210
211 /* Assemble the nonce. */
212 uint8_t nonce[EVP_AEAD_MAX_NONCE_LENGTH];
213 size_t nonce_len = 0;
David Benjamin13414b32015-12-09 23:02:39 -0500214
215 /* Prepend the fixed nonce, or left-pad with zeros if XORing. */
216 if (aead->xor_fixed_nonce) {
217 nonce_len = aead->fixed_nonce_len - aead->variable_nonce_len;
David Benjamin17cf2cb2016-12-13 01:07:13 -0500218 OPENSSL_memset(nonce, 0, nonce_len);
David Benjamin13414b32015-12-09 23:02:39 -0500219 } else {
David Benjamin17cf2cb2016-12-13 01:07:13 -0500220 OPENSSL_memcpy(nonce, aead->fixed_nonce, aead->fixed_nonce_len);
David Benjamin13414b32015-12-09 23:02:39 -0500221 nonce_len += aead->fixed_nonce_len;
222 }
223
224 /* Add the variable nonce. */
David Benjamin31a07792015-03-03 14:20:26 -0500225 if (aead->variable_nonce_included_in_record) {
226 if (in_len < aead->variable_nonce_len) {
227 /* Publicly invalid. */
David Benjamin3570d732015-06-29 00:28:17 -0400228 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_PACKET_LENGTH);
David Benjamin31a07792015-03-03 14:20:26 -0500229 return 0;
230 }
David Benjamin17cf2cb2016-12-13 01:07:13 -0500231 OPENSSL_memcpy(nonce + nonce_len, in, aead->variable_nonce_len);
David Benjamin31a07792015-03-03 14:20:26 -0500232 in += aead->variable_nonce_len;
233 in_len -= aead->variable_nonce_len;
234 } else {
235 assert(aead->variable_nonce_len == 8);
David Benjamin17cf2cb2016-12-13 01:07:13 -0500236 OPENSSL_memcpy(nonce + nonce_len, seqnum, aead->variable_nonce_len);
David Benjamin31a07792015-03-03 14:20:26 -0500237 }
238 nonce_len += aead->variable_nonce_len;
239
David Benjamin13414b32015-12-09 23:02:39 -0500240 /* XOR the fixed nonce, if necessary. */
241 if (aead->xor_fixed_nonce) {
242 assert(nonce_len == aead->fixed_nonce_len);
David Benjamin54091232016-09-05 12:47:25 -0400243 for (size_t i = 0; i < aead->fixed_nonce_len; i++) {
David Benjamin13414b32015-12-09 23:02:39 -0500244 nonce[i] ^= aead->fixed_nonce[i];
245 }
246 }
247
David Benjamina7810c12016-06-06 18:54:51 -0400248 /* Decrypt in-place. */
249 size_t len;
250 if (!EVP_AEAD_CTX_open(&aead->ctx, in, &len, in_len, nonce, nonce_len,
251 in, in_len, ad, ad_len)) {
252 return 0;
253 }
254 CBS_init(out, in, len);
255 return 1;
David Benjamin31a07792015-03-03 14:20:26 -0500256}
257
Martin Kreichgauer9f2bffb2017-06-30 05:29:50 -0700258int SSL_AEAD_CTX_seal_scatter(SSL_AEAD_CTX *aead, uint8_t *out_prefix,
259 uint8_t *out, uint8_t *out_suffix,
260 size_t *out_suffix_len, size_t max_out_suffix_len,
261 uint8_t type, uint16_t wire_version,
262 const uint8_t seqnum[8], const uint8_t *in,
263 size_t in_len, const uint8_t *extra_in,
264 size_t extra_in_len) {
David Benjaminbf82aed2016-03-01 22:57:40 -0500265#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
266 aead = NULL;
267#endif
268
Martin Kreichgauer9f2bffb2017-06-30 05:29:50 -0700269 if ((in != out && buffers_alias(in, in_len, out, in_len)) ||
270 buffers_alias(in, in_len, out_suffix, max_out_suffix_len)) {
271 OPENSSL_PUT_ERROR(SSL, SSL_R_OUTPUT_ALIASES_INPUT);
272 return 0;
273 }
274 if (extra_in_len > max_out_suffix_len) {
275 OPENSSL_PUT_ERROR(SSL, SSL_R_BUFFER_TOO_SMALL);
276 return 0;
277 }
278
David Benjamin31a07792015-03-03 14:20:26 -0500279 if (aead == NULL) {
280 /* Handle the initial NULL cipher. */
David Benjamin17cf2cb2016-12-13 01:07:13 -0500281 OPENSSL_memmove(out, in, in_len);
Martin Kreichgauer9f2bffb2017-06-30 05:29:50 -0700282 OPENSSL_memmove(out_suffix, extra_in, extra_in_len);
283 *out_suffix_len = extra_in_len;
David Benjamin31a07792015-03-03 14:20:26 -0500284 return 1;
285 }
286
287 uint8_t ad[13];
288 size_t ad_len = ssl_aead_ctx_get_ad(aead, ad, type, wire_version, seqnum,
289 in_len);
290
291 /* Assemble the nonce. */
292 uint8_t nonce[EVP_AEAD_MAX_NONCE_LENGTH];
293 size_t nonce_len = 0;
David Benjamin13414b32015-12-09 23:02:39 -0500294
295 /* Prepend the fixed nonce, or left-pad with zeros if XORing. */
296 if (aead->xor_fixed_nonce) {
297 nonce_len = aead->fixed_nonce_len - aead->variable_nonce_len;
David Benjamin17cf2cb2016-12-13 01:07:13 -0500298 OPENSSL_memset(nonce, 0, nonce_len);
David Benjamin13414b32015-12-09 23:02:39 -0500299 } else {
David Benjamin17cf2cb2016-12-13 01:07:13 -0500300 OPENSSL_memcpy(nonce, aead->fixed_nonce, aead->fixed_nonce_len);
David Benjamin13414b32015-12-09 23:02:39 -0500301 nonce_len += aead->fixed_nonce_len;
302 }
303
304 /* Select the variable nonce. */
David Benjamin31a07792015-03-03 14:20:26 -0500305 if (aead->random_variable_nonce) {
306 assert(aead->variable_nonce_included_in_record);
307 if (!RAND_bytes(nonce + nonce_len, aead->variable_nonce_len)) {
308 return 0;
309 }
310 } else {
311 /* When sending we use the sequence number as the variable part of the
312 * nonce. */
313 assert(aead->variable_nonce_len == 8);
David Benjamin17cf2cb2016-12-13 01:07:13 -0500314 OPENSSL_memcpy(nonce + nonce_len, seqnum, aead->variable_nonce_len);
David Benjamin31a07792015-03-03 14:20:26 -0500315 }
316 nonce_len += aead->variable_nonce_len;
317
318 /* Emit the variable nonce if included in the record. */
David Benjamin31a07792015-03-03 14:20:26 -0500319 if (aead->variable_nonce_included_in_record) {
David Benjamin13414b32015-12-09 23:02:39 -0500320 assert(!aead->xor_fixed_nonce);
Martin Kreichgauer9f2bffb2017-06-30 05:29:50 -0700321 if (buffers_alias(in, in_len, out_prefix, aead->variable_nonce_len)) {
David Benjamin3570d732015-06-29 00:28:17 -0400322 OPENSSL_PUT_ERROR(SSL, SSL_R_OUTPUT_ALIASES_INPUT);
David Benjamin31a07792015-03-03 14:20:26 -0500323 return 0;
324 }
Martin Kreichgauer9f2bffb2017-06-30 05:29:50 -0700325 OPENSSL_memcpy(out_prefix, nonce + aead->fixed_nonce_len,
David Benjamin17cf2cb2016-12-13 01:07:13 -0500326 aead->variable_nonce_len);
David Benjamin31a07792015-03-03 14:20:26 -0500327 }
328
David Benjamin13414b32015-12-09 23:02:39 -0500329 /* XOR the fixed nonce, if necessary. */
330 if (aead->xor_fixed_nonce) {
331 assert(nonce_len == aead->fixed_nonce_len);
David Benjamin54091232016-09-05 12:47:25 -0400332 for (size_t i = 0; i < aead->fixed_nonce_len; i++) {
David Benjamin13414b32015-12-09 23:02:39 -0500333 nonce[i] ^= aead->fixed_nonce[i];
334 }
335 }
336
Martin Kreichgauer9f2bffb2017-06-30 05:29:50 -0700337 return EVP_AEAD_CTX_seal_scatter(&aead->ctx, out, out_suffix, out_suffix_len,
338 max_out_suffix_len, nonce, nonce_len, in,
339 in_len, extra_in, extra_in_len, ad, ad_len);
340}
341
342int SSL_AEAD_CTX_seal(SSL_AEAD_CTX *aead, uint8_t *out, size_t *out_len,
343 size_t max_out_len, uint8_t type, uint16_t wire_version,
344 const uint8_t seqnum[8], const uint8_t *in,
345 size_t in_len) {
346 size_t prefix_len = SSL_AEAD_CTX_explicit_nonce_len(aead);
347 if (in_len + prefix_len < in_len) {
348 OPENSSL_PUT_ERROR(CIPHER, SSL_R_RECORD_TOO_LARGE);
David Benjamin31a07792015-03-03 14:20:26 -0500349 return 0;
350 }
Martin Kreichgauer9f2bffb2017-06-30 05:29:50 -0700351 if (in_len + prefix_len > max_out_len) {
352 OPENSSL_PUT_ERROR(SSL, SSL_R_BUFFER_TOO_SMALL);
353 return 0;
354 }
355
356 size_t suffix_len;
357 if (!SSL_AEAD_CTX_seal_scatter(aead, out, out + prefix_len,
358 out + prefix_len + in_len, &suffix_len,
359 max_out_len - prefix_len - in_len, type,
360 wire_version, seqnum, in, in_len, 0, 0)) {
361 return 0;
362 }
363 assert(suffix_len <= SSL_AEAD_CTX_max_suffix_len(aead, 0));
364 *out_len = prefix_len + in_len + suffix_len;
David Benjamin31a07792015-03-03 14:20:26 -0500365 return 1;
366}