blob: 5264a6536fcd60575f55ef49f339e9a287fa5b1c [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 Benjamin31a07792015-03-03 14:20:26 -050028SSL_AEAD_CTX *SSL_AEAD_CTX_new(enum evp_aead_direction_t direction,
Steven Valdez2f3404b2017-05-24 16:54:35 -040029 uint16_t version, int is_dtls,
30 const SSL_CIPHER *cipher, const uint8_t *enc_key,
31 size_t enc_key_len, const uint8_t *mac_key,
32 size_t mac_key_len, const uint8_t *fixed_iv,
33 size_t fixed_iv_len) {
David Benjamin31a07792015-03-03 14:20:26 -050034 const EVP_AEAD *aead;
David Benjamin4b0d0e42016-10-28 17:17:14 -040035 size_t expected_mac_key_len, expected_fixed_iv_len;
36 if (!ssl_cipher_get_evp_aead(&aead, &expected_mac_key_len,
Steven Valdez2f3404b2017-05-24 16:54:35 -040037 &expected_fixed_iv_len, cipher, version,
38 is_dtls) ||
David Benjamin4b0d0e42016-10-28 17:17:14 -040039 /* Ensure the caller returned correct key sizes. */
40 expected_fixed_iv_len != fixed_iv_len ||
41 expected_mac_key_len != mac_key_len) {
David Benjamin3570d732015-06-29 00:28:17 -040042 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamin31a07792015-03-03 14:20:26 -050043 return 0;
44 }
45
46 uint8_t merged_key[EVP_AEAD_MAX_KEY_LENGTH];
47 if (mac_key_len > 0) {
48 /* This is a "stateful" AEAD (for compatibility with pre-AEAD cipher
49 * suites). */
50 if (mac_key_len + enc_key_len + fixed_iv_len > sizeof(merged_key)) {
David Benjamin3570d732015-06-29 00:28:17 -040051 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamin31a07792015-03-03 14:20:26 -050052 return 0;
53 }
David Benjamin17cf2cb2016-12-13 01:07:13 -050054 OPENSSL_memcpy(merged_key, mac_key, mac_key_len);
55 OPENSSL_memcpy(merged_key + mac_key_len, enc_key, enc_key_len);
56 OPENSSL_memcpy(merged_key + mac_key_len + enc_key_len, fixed_iv,
57 fixed_iv_len);
David Benjamin31a07792015-03-03 14:20:26 -050058 enc_key = merged_key;
59 enc_key_len += mac_key_len;
60 enc_key_len += fixed_iv_len;
61 }
62
David Benjaminf5260812017-07-12 17:16:39 -040063 SSL_AEAD_CTX *aead_ctx = (SSL_AEAD_CTX *)OPENSSL_malloc(sizeof(SSL_AEAD_CTX));
David Benjamin31a07792015-03-03 14:20:26 -050064 if (aead_ctx == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -040065 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin31a07792015-03-03 14:20:26 -050066 return NULL;
67 }
David Benjamin17cf2cb2016-12-13 01:07:13 -050068 OPENSSL_memset(aead_ctx, 0, sizeof(SSL_AEAD_CTX));
David Benjamin31a07792015-03-03 14:20:26 -050069 aead_ctx->cipher = cipher;
Steven Valdez130d5292017-03-07 16:58:04 -050070 aead_ctx->version = version;
David Benjamin31a07792015-03-03 14:20:26 -050071
72 if (!EVP_AEAD_CTX_init_with_direction(
73 &aead_ctx->ctx, aead, enc_key, enc_key_len,
74 EVP_AEAD_DEFAULT_TAG_LENGTH, direction)) {
75 OPENSSL_free(aead_ctx);
76 return NULL;
77 }
78
79 assert(EVP_AEAD_nonce_length(aead) <= EVP_AEAD_MAX_NONCE_LENGTH);
David Benjamina3d76d02017-07-14 19:36:07 -040080 static_assert(EVP_AEAD_MAX_NONCE_LENGTH < 256,
81 "variable_nonce_len doesn't fit in uint8_t");
David Benjamin31a07792015-03-03 14:20:26 -050082 aead_ctx->variable_nonce_len = (uint8_t)EVP_AEAD_nonce_length(aead);
David Benjamin51a01a52015-10-29 13:19:56 -040083 if (mac_key_len == 0) {
David Benjamin13414b32015-12-09 23:02:39 -050084 assert(fixed_iv_len <= sizeof(aead_ctx->fixed_nonce));
David Benjamin17cf2cb2016-12-13 01:07:13 -050085 OPENSSL_memcpy(aead_ctx->fixed_nonce, fixed_iv, fixed_iv_len);
David Benjamin31a07792015-03-03 14:20:26 -050086 aead_ctx->fixed_nonce_len = fixed_iv_len;
David Benjamin13414b32015-12-09 23:02:39 -050087
88 if (cipher->algorithm_enc & SSL_CHACHA20POLY1305) {
89 /* The fixed nonce into the actual nonce (the sequence number). */
90 aead_ctx->xor_fixed_nonce = 1;
91 aead_ctx->variable_nonce_len = 8;
92 } else {
93 /* The fixed IV is prepended to the nonce. */
94 assert(fixed_iv_len <= aead_ctx->variable_nonce_len);
95 aead_ctx->variable_nonce_len -= fixed_iv_len;
96 }
97
David Benjaminb2a985b2015-06-21 15:13:57 -040098 /* AES-GCM uses an explicit nonce. */
99 if (cipher->algorithm_enc & (SSL_AES128GCM | SSL_AES256GCM)) {
100 aead_ctx->variable_nonce_included_in_record = 1;
101 }
Steven Valdez494650c2016-05-24 12:43:04 -0400102
103 /* The TLS 1.3 construction XORs the fixed nonce into the sequence number
104 * and omits the additional data. */
105 if (version >= TLS1_3_VERSION) {
106 aead_ctx->xor_fixed_nonce = 1;
107 aead_ctx->variable_nonce_len = 8;
108 aead_ctx->variable_nonce_included_in_record = 0;
109 aead_ctx->omit_ad = 1;
Steven Valdez79750562016-06-16 06:38:04 -0400110 assert(fixed_iv_len >= aead_ctx->variable_nonce_len);
Steven Valdez494650c2016-05-24 12:43:04 -0400111 }
David Benjamin31a07792015-03-03 14:20:26 -0500112 } else {
Steven Valdez79750562016-06-16 06:38:04 -0400113 assert(version < TLS1_3_VERSION);
David Benjamin31a07792015-03-03 14:20:26 -0500114 aead_ctx->variable_nonce_included_in_record = 1;
115 aead_ctx->random_variable_nonce = 1;
116 aead_ctx->omit_length_in_ad = 1;
117 aead_ctx->omit_version_in_ad = (version == SSL3_VERSION);
118 }
119
120 return aead_ctx;
121}
122
123void SSL_AEAD_CTX_free(SSL_AEAD_CTX *aead) {
124 if (aead == NULL) {
125 return;
126 }
127 EVP_AEAD_CTX_cleanup(&aead->ctx);
128 OPENSSL_free(aead);
129}
130
David Benjamina772b162017-01-24 17:51:33 -0500131size_t SSL_AEAD_CTX_explicit_nonce_len(const SSL_AEAD_CTX *aead) {
David Benjaminbf82aed2016-03-01 22:57:40 -0500132#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
133 aead = NULL;
134#endif
135
David Benjamin31a07792015-03-03 14:20:26 -0500136 if (aead != NULL && aead->variable_nonce_included_in_record) {
137 return aead->variable_nonce_len;
138 }
139 return 0;
140}
141
Martin Kreichgauer9f2bffb2017-06-30 05:29:50 -0700142size_t SSL_AEAD_CTX_max_suffix_len(const SSL_AEAD_CTX *aead,
143 size_t extra_in_len) {
David Benjaminbf82aed2016-03-01 22:57:40 -0500144#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
145 aead = NULL;
146#endif
147
Martin Kreichgauer9f2bffb2017-06-30 05:29:50 -0700148 return extra_in_len +
149 (aead == NULL ? 0 : EVP_AEAD_max_overhead(aead->ctx.aead));
150}
151
152size_t SSL_AEAD_CTX_max_overhead(const SSL_AEAD_CTX *aead) {
153 return SSL_AEAD_CTX_explicit_nonce_len(aead) +
154 SSL_AEAD_CTX_max_suffix_len(aead, 0);
David Benjamin31a07792015-03-03 14:20:26 -0500155}
156
157/* ssl_aead_ctx_get_ad writes the additional data for |aead| into |out| and
158 * returns the number of bytes written. */
159static size_t ssl_aead_ctx_get_ad(SSL_AEAD_CTX *aead, uint8_t out[13],
160 uint8_t type, uint16_t wire_version,
161 const uint8_t seqnum[8],
162 size_t plaintext_len) {
Steven Valdez494650c2016-05-24 12:43:04 -0400163 if (aead->omit_ad) {
164 return 0;
165 }
166
David Benjamin17cf2cb2016-12-13 01:07:13 -0500167 OPENSSL_memcpy(out, seqnum, 8);
David Benjamin31a07792015-03-03 14:20:26 -0500168 size_t len = 8;
169 out[len++] = type;
170 if (!aead->omit_version_in_ad) {
171 out[len++] = (uint8_t)(wire_version >> 8);
172 out[len++] = (uint8_t)wire_version;
173 }
174 if (!aead->omit_length_in_ad) {
175 out[len++] = (uint8_t)(plaintext_len >> 8);
176 out[len++] = (uint8_t)plaintext_len;
177 }
178 return len;
179}
180
David Benjamina7810c12016-06-06 18:54:51 -0400181int SSL_AEAD_CTX_open(SSL_AEAD_CTX *aead, CBS *out, uint8_t type,
182 uint16_t wire_version, const uint8_t seqnum[8],
183 uint8_t *in, size_t in_len) {
David Benjaminbf82aed2016-03-01 22:57:40 -0500184#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
185 aead = NULL;
186#endif
187
David Benjamin31a07792015-03-03 14:20:26 -0500188 if (aead == NULL) {
189 /* Handle the initial NULL cipher. */
David Benjamina7810c12016-06-06 18:54:51 -0400190 CBS_init(out, in, in_len);
David Benjamin31a07792015-03-03 14:20:26 -0500191 return 1;
192 }
193
194 /* TLS 1.2 AEADs include the length in the AD and are assumed to have fixed
195 * overhead. Otherwise the parameter is unused. */
196 size_t plaintext_len = 0;
197 if (!aead->omit_length_in_ad) {
198 size_t overhead = SSL_AEAD_CTX_max_overhead(aead);
199 if (in_len < overhead) {
200 /* Publicly invalid. */
David Benjamin3570d732015-06-29 00:28:17 -0400201 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_PACKET_LENGTH);
David Benjamin31a07792015-03-03 14:20:26 -0500202 return 0;
203 }
204 plaintext_len = in_len - overhead;
205 }
206 uint8_t ad[13];
207 size_t ad_len = ssl_aead_ctx_get_ad(aead, ad, type, wire_version, seqnum,
208 plaintext_len);
209
210 /* Assemble the nonce. */
211 uint8_t nonce[EVP_AEAD_MAX_NONCE_LENGTH];
212 size_t nonce_len = 0;
David Benjamin13414b32015-12-09 23:02:39 -0500213
214 /* Prepend the fixed nonce, or left-pad with zeros if XORing. */
215 if (aead->xor_fixed_nonce) {
216 nonce_len = aead->fixed_nonce_len - aead->variable_nonce_len;
David Benjamin17cf2cb2016-12-13 01:07:13 -0500217 OPENSSL_memset(nonce, 0, nonce_len);
David Benjamin13414b32015-12-09 23:02:39 -0500218 } else {
David Benjamin17cf2cb2016-12-13 01:07:13 -0500219 OPENSSL_memcpy(nonce, aead->fixed_nonce, aead->fixed_nonce_len);
David Benjamin13414b32015-12-09 23:02:39 -0500220 nonce_len += aead->fixed_nonce_len;
221 }
222
223 /* Add the variable nonce. */
David Benjamin31a07792015-03-03 14:20:26 -0500224 if (aead->variable_nonce_included_in_record) {
225 if (in_len < aead->variable_nonce_len) {
226 /* Publicly invalid. */
David Benjamin3570d732015-06-29 00:28:17 -0400227 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_PACKET_LENGTH);
David Benjamin31a07792015-03-03 14:20:26 -0500228 return 0;
229 }
David Benjamin17cf2cb2016-12-13 01:07:13 -0500230 OPENSSL_memcpy(nonce + nonce_len, in, aead->variable_nonce_len);
David Benjamin31a07792015-03-03 14:20:26 -0500231 in += aead->variable_nonce_len;
232 in_len -= aead->variable_nonce_len;
233 } else {
234 assert(aead->variable_nonce_len == 8);
David Benjamin17cf2cb2016-12-13 01:07:13 -0500235 OPENSSL_memcpy(nonce + nonce_len, seqnum, aead->variable_nonce_len);
David Benjamin31a07792015-03-03 14:20:26 -0500236 }
237 nonce_len += aead->variable_nonce_len;
238
David Benjamin13414b32015-12-09 23:02:39 -0500239 /* XOR the fixed nonce, if necessary. */
240 if (aead->xor_fixed_nonce) {
241 assert(nonce_len == aead->fixed_nonce_len);
David Benjamin54091232016-09-05 12:47:25 -0400242 for (size_t i = 0; i < aead->fixed_nonce_len; i++) {
David Benjamin13414b32015-12-09 23:02:39 -0500243 nonce[i] ^= aead->fixed_nonce[i];
244 }
245 }
246
David Benjamina7810c12016-06-06 18:54:51 -0400247 /* Decrypt in-place. */
248 size_t len;
249 if (!EVP_AEAD_CTX_open(&aead->ctx, in, &len, in_len, nonce, nonce_len,
250 in, in_len, ad, ad_len)) {
251 return 0;
252 }
253 CBS_init(out, in, len);
254 return 1;
David Benjamin31a07792015-03-03 14:20:26 -0500255}
256
Martin Kreichgauer9f2bffb2017-06-30 05:29:50 -0700257int SSL_AEAD_CTX_seal_scatter(SSL_AEAD_CTX *aead, uint8_t *out_prefix,
258 uint8_t *out, uint8_t *out_suffix,
259 size_t *out_suffix_len, size_t max_out_suffix_len,
260 uint8_t type, uint16_t wire_version,
261 const uint8_t seqnum[8], const uint8_t *in,
262 size_t in_len, const uint8_t *extra_in,
263 size_t extra_in_len) {
David Benjaminbf82aed2016-03-01 22:57:40 -0500264#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
265 aead = NULL;
266#endif
267
Martin Kreichgauer9f2bffb2017-06-30 05:29:50 -0700268 if ((in != out && buffers_alias(in, in_len, out, in_len)) ||
269 buffers_alias(in, in_len, out_suffix, max_out_suffix_len)) {
270 OPENSSL_PUT_ERROR(SSL, SSL_R_OUTPUT_ALIASES_INPUT);
271 return 0;
272 }
273 if (extra_in_len > max_out_suffix_len) {
274 OPENSSL_PUT_ERROR(SSL, SSL_R_BUFFER_TOO_SMALL);
275 return 0;
276 }
277
David Benjamin31a07792015-03-03 14:20:26 -0500278 if (aead == NULL) {
279 /* Handle the initial NULL cipher. */
David Benjamin17cf2cb2016-12-13 01:07:13 -0500280 OPENSSL_memmove(out, in, in_len);
Martin Kreichgauer9f2bffb2017-06-30 05:29:50 -0700281 OPENSSL_memmove(out_suffix, extra_in, extra_in_len);
282 *out_suffix_len = extra_in_len;
David Benjamin31a07792015-03-03 14:20:26 -0500283 return 1;
284 }
285
286 uint8_t ad[13];
287 size_t ad_len = ssl_aead_ctx_get_ad(aead, ad, type, wire_version, seqnum,
288 in_len);
289
290 /* Assemble the nonce. */
291 uint8_t nonce[EVP_AEAD_MAX_NONCE_LENGTH];
292 size_t nonce_len = 0;
David Benjamin13414b32015-12-09 23:02:39 -0500293
294 /* Prepend the fixed nonce, or left-pad with zeros if XORing. */
295 if (aead->xor_fixed_nonce) {
296 nonce_len = aead->fixed_nonce_len - aead->variable_nonce_len;
David Benjamin17cf2cb2016-12-13 01:07:13 -0500297 OPENSSL_memset(nonce, 0, nonce_len);
David Benjamin13414b32015-12-09 23:02:39 -0500298 } else {
David Benjamin17cf2cb2016-12-13 01:07:13 -0500299 OPENSSL_memcpy(nonce, aead->fixed_nonce, aead->fixed_nonce_len);
David Benjamin13414b32015-12-09 23:02:39 -0500300 nonce_len += aead->fixed_nonce_len;
301 }
302
303 /* Select the variable nonce. */
David Benjamin31a07792015-03-03 14:20:26 -0500304 if (aead->random_variable_nonce) {
305 assert(aead->variable_nonce_included_in_record);
306 if (!RAND_bytes(nonce + nonce_len, aead->variable_nonce_len)) {
307 return 0;
308 }
309 } else {
310 /* When sending we use the sequence number as the variable part of the
311 * nonce. */
312 assert(aead->variable_nonce_len == 8);
David Benjamin17cf2cb2016-12-13 01:07:13 -0500313 OPENSSL_memcpy(nonce + nonce_len, seqnum, aead->variable_nonce_len);
David Benjamin31a07792015-03-03 14:20:26 -0500314 }
315 nonce_len += aead->variable_nonce_len;
316
317 /* Emit the variable nonce if included in the record. */
David Benjamin31a07792015-03-03 14:20:26 -0500318 if (aead->variable_nonce_included_in_record) {
David Benjamin13414b32015-12-09 23:02:39 -0500319 assert(!aead->xor_fixed_nonce);
Martin Kreichgauer9f2bffb2017-06-30 05:29:50 -0700320 if (buffers_alias(in, in_len, out_prefix, aead->variable_nonce_len)) {
David Benjamin3570d732015-06-29 00:28:17 -0400321 OPENSSL_PUT_ERROR(SSL, SSL_R_OUTPUT_ALIASES_INPUT);
David Benjamin31a07792015-03-03 14:20:26 -0500322 return 0;
323 }
Martin Kreichgauer9f2bffb2017-06-30 05:29:50 -0700324 OPENSSL_memcpy(out_prefix, nonce + aead->fixed_nonce_len,
David Benjamin17cf2cb2016-12-13 01:07:13 -0500325 aead->variable_nonce_len);
David Benjamin31a07792015-03-03 14:20:26 -0500326 }
327
David Benjamin13414b32015-12-09 23:02:39 -0500328 /* XOR the fixed nonce, if necessary. */
329 if (aead->xor_fixed_nonce) {
330 assert(nonce_len == aead->fixed_nonce_len);
David Benjamin54091232016-09-05 12:47:25 -0400331 for (size_t i = 0; i < aead->fixed_nonce_len; i++) {
David Benjamin13414b32015-12-09 23:02:39 -0500332 nonce[i] ^= aead->fixed_nonce[i];
333 }
334 }
335
Martin Kreichgauer9f2bffb2017-06-30 05:29:50 -0700336 return EVP_AEAD_CTX_seal_scatter(&aead->ctx, out, out_suffix, out_suffix_len,
337 max_out_suffix_len, nonce, nonce_len, in,
338 in_len, extra_in, extra_in_len, ad, ad_len);
339}
340
341int SSL_AEAD_CTX_seal(SSL_AEAD_CTX *aead, uint8_t *out, size_t *out_len,
342 size_t max_out_len, uint8_t type, uint16_t wire_version,
343 const uint8_t seqnum[8], const uint8_t *in,
344 size_t in_len) {
345 size_t prefix_len = SSL_AEAD_CTX_explicit_nonce_len(aead);
346 if (in_len + prefix_len < in_len) {
347 OPENSSL_PUT_ERROR(CIPHER, SSL_R_RECORD_TOO_LARGE);
David Benjamin31a07792015-03-03 14:20:26 -0500348 return 0;
349 }
Martin Kreichgauer9f2bffb2017-06-30 05:29:50 -0700350 if (in_len + prefix_len > max_out_len) {
351 OPENSSL_PUT_ERROR(SSL, SSL_R_BUFFER_TOO_SMALL);
352 return 0;
353 }
354
355 size_t suffix_len;
356 if (!SSL_AEAD_CTX_seal_scatter(aead, out, out + prefix_len,
357 out + prefix_len + in_len, &suffix_len,
358 max_out_len - prefix_len - in_len, type,
359 wire_version, seqnum, in, in_len, 0, 0)) {
360 return 0;
361 }
362 assert(suffix_len <= SSL_AEAD_CTX_max_suffix_len(aead, 0));
363 *out_len = prefix_len + in_len + suffix_len;
David Benjamin31a07792015-03-03 14:20:26 -0500364 return 1;
365}