blob: 1af4a5ab1cb667d9d706927db815b736e12e2029 [file] [log] [blame]
Adam Langleyf4e42722015-06-04 17:45:09 -07001/* 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
Kenny Rootb8494592015-09-25 02:29:14 +000015#include <openssl/ssl.h>
16
Adam Langleyf4e42722015-06-04 17:45:09 -070017#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
Robert Sloan69939df2017-01-09 10:53:07 -080025#include "../crypto/internal.h"
Adam Langleyf4e42722015-06-04 17:45:09 -070026#include "internal.h"
27
28
Adam Langleyf4e42722015-06-04 17:45:09 -070029SSL_AEAD_CTX *SSL_AEAD_CTX_new(enum evp_aead_direction_t direction,
30 uint16_t version, const SSL_CIPHER *cipher,
31 const uint8_t *enc_key, size_t enc_key_len,
32 const uint8_t *mac_key, size_t mac_key_len,
33 const uint8_t *fixed_iv, size_t fixed_iv_len) {
34 const EVP_AEAD *aead;
Steven Valdez909b19f2016-11-21 15:35:44 -050035 size_t expected_mac_key_len, expected_fixed_iv_len;
36 if (!ssl_cipher_get_evp_aead(&aead, &expected_mac_key_len,
37 &expected_fixed_iv_len, cipher, version) ||
38 /* Ensure the caller returned correct key sizes. */
39 expected_fixed_iv_len != fixed_iv_len ||
40 expected_mac_key_len != mac_key_len) {
Kenny Rootb8494592015-09-25 02:29:14 +000041 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langleyf4e42722015-06-04 17:45:09 -070042 return 0;
43 }
44
45 uint8_t merged_key[EVP_AEAD_MAX_KEY_LENGTH];
46 if (mac_key_len > 0) {
47 /* This is a "stateful" AEAD (for compatibility with pre-AEAD cipher
48 * suites). */
49 if (mac_key_len + enc_key_len + fixed_iv_len > sizeof(merged_key)) {
Kenny Rootb8494592015-09-25 02:29:14 +000050 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langleyf4e42722015-06-04 17:45:09 -070051 return 0;
52 }
Robert Sloan69939df2017-01-09 10:53:07 -080053 OPENSSL_memcpy(merged_key, mac_key, mac_key_len);
54 OPENSSL_memcpy(merged_key + mac_key_len, enc_key, enc_key_len);
55 OPENSSL_memcpy(merged_key + mac_key_len + enc_key_len, fixed_iv,
56 fixed_iv_len);
Adam Langleyf4e42722015-06-04 17:45:09 -070057 enc_key = merged_key;
58 enc_key_len += mac_key_len;
59 enc_key_len += fixed_iv_len;
60 }
61
David Benjamin4969cc92016-04-22 15:02:23 -040062 SSL_AEAD_CTX *aead_ctx = OPENSSL_malloc(sizeof(SSL_AEAD_CTX));
Adam Langleyf4e42722015-06-04 17:45:09 -070063 if (aead_ctx == NULL) {
Kenny Rootb8494592015-09-25 02:29:14 +000064 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Adam Langleyf4e42722015-06-04 17:45:09 -070065 return NULL;
66 }
Robert Sloan69939df2017-01-09 10:53:07 -080067 OPENSSL_memset(aead_ctx, 0, sizeof(SSL_AEAD_CTX));
Adam Langleyf4e42722015-06-04 17:45:09 -070068 aead_ctx->cipher = cipher;
Robert Sloan1c9db532017-03-13 08:03:59 -070069 aead_ctx->version = version;
Adam Langleyf4e42722015-06-04 17:45:09 -070070
71 if (!EVP_AEAD_CTX_init_with_direction(
72 &aead_ctx->ctx, aead, enc_key, enc_key_len,
73 EVP_AEAD_DEFAULT_TAG_LENGTH, direction)) {
74 OPENSSL_free(aead_ctx);
75 return NULL;
76 }
77
78 assert(EVP_AEAD_nonce_length(aead) <= EVP_AEAD_MAX_NONCE_LENGTH);
David Benjaminf31229b2017-01-25 14:08:15 -050079 OPENSSL_COMPILE_ASSERT(EVP_AEAD_MAX_NONCE_LENGTH < 256,
80 variable_nonce_len_doesnt_fit_in_uint8_t);
Adam Langleyf4e42722015-06-04 17:45:09 -070081 aead_ctx->variable_nonce_len = (uint8_t)EVP_AEAD_nonce_length(aead);
82 if (mac_key_len == 0) {
Adam Langley4139edb2016-01-13 15:00:54 -080083 assert(fixed_iv_len <= sizeof(aead_ctx->fixed_nonce));
Robert Sloan69939df2017-01-09 10:53:07 -080084 OPENSSL_memcpy(aead_ctx->fixed_nonce, fixed_iv, fixed_iv_len);
Adam Langleyf4e42722015-06-04 17:45:09 -070085 aead_ctx->fixed_nonce_len = fixed_iv_len;
Adam Langley4139edb2016-01-13 15:00:54 -080086
87 if (cipher->algorithm_enc & SSL_CHACHA20POLY1305) {
88 /* The fixed nonce into the actual nonce (the sequence number). */
89 aead_ctx->xor_fixed_nonce = 1;
90 aead_ctx->variable_nonce_len = 8;
91 } else {
92 /* The fixed IV is prepended to the nonce. */
93 assert(fixed_iv_len <= aead_ctx->variable_nonce_len);
94 aead_ctx->variable_nonce_len -= fixed_iv_len;
95 }
96
Kenny Rootb8494592015-09-25 02:29:14 +000097 /* AES-GCM uses an explicit nonce. */
98 if (cipher->algorithm_enc & (SSL_AES128GCM | SSL_AES256GCM)) {
99 aead_ctx->variable_nonce_included_in_record = 1;
100 }
David Benjamind316cba2016-06-02 16:17:39 -0400101
102 /* The TLS 1.3 construction XORs the fixed nonce into the sequence number
103 * and omits the additional data. */
104 if (version >= TLS1_3_VERSION) {
105 aead_ctx->xor_fixed_nonce = 1;
106 aead_ctx->variable_nonce_len = 8;
107 aead_ctx->variable_nonce_included_in_record = 0;
108 aead_ctx->omit_ad = 1;
David Benjaminc895d6b2016-08-11 13:26:41 -0400109 assert(fixed_iv_len >= aead_ctx->variable_nonce_len);
David Benjamind316cba2016-06-02 16:17:39 -0400110 }
Adam Langleyf4e42722015-06-04 17:45:09 -0700111 } else {
David Benjaminc895d6b2016-08-11 13:26:41 -0400112 assert(version < TLS1_3_VERSION);
Adam Langleyf4e42722015-06-04 17:45:09 -0700113 aead_ctx->variable_nonce_included_in_record = 1;
114 aead_ctx->random_variable_nonce = 1;
115 aead_ctx->omit_length_in_ad = 1;
116 aead_ctx->omit_version_in_ad = (version == SSL3_VERSION);
117 }
118
119 return aead_ctx;
120}
121
122void SSL_AEAD_CTX_free(SSL_AEAD_CTX *aead) {
123 if (aead == NULL) {
124 return;
125 }
126 EVP_AEAD_CTX_cleanup(&aead->ctx);
127 OPENSSL_free(aead);
128}
129
David Benjaminf31229b2017-01-25 14:08:15 -0500130size_t SSL_AEAD_CTX_explicit_nonce_len(const SSL_AEAD_CTX *aead) {
David Benjamin4969cc92016-04-22 15:02:23 -0400131#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
132 aead = NULL;
133#endif
134
Adam Langleyf4e42722015-06-04 17:45:09 -0700135 if (aead != NULL && aead->variable_nonce_included_in_record) {
136 return aead->variable_nonce_len;
137 }
138 return 0;
139}
140
David Benjaminf31229b2017-01-25 14:08:15 -0500141size_t SSL_AEAD_CTX_max_overhead(const SSL_AEAD_CTX *aead) {
David Benjamin4969cc92016-04-22 15:02:23 -0400142#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
143 aead = NULL;
144#endif
145
Adam Langleyf4e42722015-06-04 17:45:09 -0700146 if (aead == NULL) {
147 return 0;
148 }
149 return EVP_AEAD_max_overhead(aead->ctx.aead) +
David Benjamin4969cc92016-04-22 15:02:23 -0400150 SSL_AEAD_CTX_explicit_nonce_len(aead);
Adam Langleyf4e42722015-06-04 17:45:09 -0700151}
152
153/* ssl_aead_ctx_get_ad writes the additional data for |aead| into |out| and
154 * returns the number of bytes written. */
155static size_t ssl_aead_ctx_get_ad(SSL_AEAD_CTX *aead, uint8_t out[13],
156 uint8_t type, uint16_t wire_version,
157 const uint8_t seqnum[8],
158 size_t plaintext_len) {
David Benjamind316cba2016-06-02 16:17:39 -0400159 if (aead->omit_ad) {
160 return 0;
161 }
162
Robert Sloan69939df2017-01-09 10:53:07 -0800163 OPENSSL_memcpy(out, seqnum, 8);
Adam Langleyf4e42722015-06-04 17:45:09 -0700164 size_t len = 8;
165 out[len++] = type;
166 if (!aead->omit_version_in_ad) {
167 out[len++] = (uint8_t)(wire_version >> 8);
168 out[len++] = (uint8_t)wire_version;
169 }
170 if (!aead->omit_length_in_ad) {
171 out[len++] = (uint8_t)(plaintext_len >> 8);
172 out[len++] = (uint8_t)plaintext_len;
173 }
174 return len;
175}
176
David Benjamin6e899c72016-06-09 18:02:18 -0400177int SSL_AEAD_CTX_open(SSL_AEAD_CTX *aead, CBS *out, uint8_t type,
178 uint16_t wire_version, const uint8_t seqnum[8],
179 uint8_t *in, size_t in_len) {
David Benjamin4969cc92016-04-22 15:02:23 -0400180#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
181 aead = NULL;
182#endif
183
Adam Langleyf4e42722015-06-04 17:45:09 -0700184 if (aead == NULL) {
185 /* Handle the initial NULL cipher. */
David Benjamin6e899c72016-06-09 18:02:18 -0400186 CBS_init(out, in, in_len);
Adam Langleyf4e42722015-06-04 17:45:09 -0700187 return 1;
188 }
189
190 /* TLS 1.2 AEADs include the length in the AD and are assumed to have fixed
191 * overhead. Otherwise the parameter is unused. */
192 size_t plaintext_len = 0;
193 if (!aead->omit_length_in_ad) {
194 size_t overhead = SSL_AEAD_CTX_max_overhead(aead);
195 if (in_len < overhead) {
196 /* Publicly invalid. */
Kenny Rootb8494592015-09-25 02:29:14 +0000197 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_PACKET_LENGTH);
Adam Langleyf4e42722015-06-04 17:45:09 -0700198 return 0;
199 }
200 plaintext_len = in_len - overhead;
201 }
202 uint8_t ad[13];
203 size_t ad_len = ssl_aead_ctx_get_ad(aead, ad, type, wire_version, seqnum,
204 plaintext_len);
205
206 /* Assemble the nonce. */
207 uint8_t nonce[EVP_AEAD_MAX_NONCE_LENGTH];
208 size_t nonce_len = 0;
Adam Langley4139edb2016-01-13 15:00:54 -0800209
210 /* Prepend the fixed nonce, or left-pad with zeros if XORing. */
211 if (aead->xor_fixed_nonce) {
212 nonce_len = aead->fixed_nonce_len - aead->variable_nonce_len;
Robert Sloan69939df2017-01-09 10:53:07 -0800213 OPENSSL_memset(nonce, 0, nonce_len);
Adam Langley4139edb2016-01-13 15:00:54 -0800214 } else {
Robert Sloan69939df2017-01-09 10:53:07 -0800215 OPENSSL_memcpy(nonce, aead->fixed_nonce, aead->fixed_nonce_len);
Adam Langley4139edb2016-01-13 15:00:54 -0800216 nonce_len += aead->fixed_nonce_len;
217 }
218
219 /* Add the variable nonce. */
Adam Langleyf4e42722015-06-04 17:45:09 -0700220 if (aead->variable_nonce_included_in_record) {
221 if (in_len < aead->variable_nonce_len) {
222 /* Publicly invalid. */
Kenny Rootb8494592015-09-25 02:29:14 +0000223 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_PACKET_LENGTH);
Adam Langleyf4e42722015-06-04 17:45:09 -0700224 return 0;
225 }
Robert Sloan69939df2017-01-09 10:53:07 -0800226 OPENSSL_memcpy(nonce + nonce_len, in, aead->variable_nonce_len);
Adam Langleyf4e42722015-06-04 17:45:09 -0700227 in += aead->variable_nonce_len;
228 in_len -= aead->variable_nonce_len;
229 } else {
230 assert(aead->variable_nonce_len == 8);
Robert Sloan69939df2017-01-09 10:53:07 -0800231 OPENSSL_memcpy(nonce + nonce_len, seqnum, aead->variable_nonce_len);
Adam Langleyf4e42722015-06-04 17:45:09 -0700232 }
233 nonce_len += aead->variable_nonce_len;
234
Adam Langley4139edb2016-01-13 15:00:54 -0800235 /* XOR the fixed nonce, if necessary. */
236 if (aead->xor_fixed_nonce) {
237 assert(nonce_len == aead->fixed_nonce_len);
David Benjamin7c0d06c2016-08-11 13:26:41 -0400238 for (size_t i = 0; i < aead->fixed_nonce_len; i++) {
Adam Langley4139edb2016-01-13 15:00:54 -0800239 nonce[i] ^= aead->fixed_nonce[i];
240 }
241 }
242
David Benjamin6e899c72016-06-09 18:02:18 -0400243 /* Decrypt in-place. */
244 size_t len;
245 if (!EVP_AEAD_CTX_open(&aead->ctx, in, &len, in_len, nonce, nonce_len,
246 in, in_len, ad, ad_len)) {
247 return 0;
248 }
249 CBS_init(out, in, len);
250 return 1;
Adam Langleyf4e42722015-06-04 17:45:09 -0700251}
252
253int SSL_AEAD_CTX_seal(SSL_AEAD_CTX *aead, uint8_t *out, size_t *out_len,
254 size_t max_out, uint8_t type, uint16_t wire_version,
255 const uint8_t seqnum[8], const uint8_t *in,
256 size_t in_len) {
David Benjamin4969cc92016-04-22 15:02:23 -0400257#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
258 aead = NULL;
259#endif
260
Adam Langleyf4e42722015-06-04 17:45:09 -0700261 if (aead == NULL) {
262 /* Handle the initial NULL cipher. */
263 if (in_len > max_out) {
Kenny Rootb8494592015-09-25 02:29:14 +0000264 OPENSSL_PUT_ERROR(SSL, SSL_R_BUFFER_TOO_SMALL);
Adam Langleyf4e42722015-06-04 17:45:09 -0700265 return 0;
266 }
Robert Sloan69939df2017-01-09 10:53:07 -0800267 OPENSSL_memmove(out, in, in_len);
Adam Langleyf4e42722015-06-04 17:45:09 -0700268 *out_len = in_len;
269 return 1;
270 }
271
272 uint8_t ad[13];
273 size_t ad_len = ssl_aead_ctx_get_ad(aead, ad, type, wire_version, seqnum,
274 in_len);
275
276 /* Assemble the nonce. */
277 uint8_t nonce[EVP_AEAD_MAX_NONCE_LENGTH];
278 size_t nonce_len = 0;
Adam Langley4139edb2016-01-13 15:00:54 -0800279
280 /* Prepend the fixed nonce, or left-pad with zeros if XORing. */
281 if (aead->xor_fixed_nonce) {
282 nonce_len = aead->fixed_nonce_len - aead->variable_nonce_len;
Robert Sloan69939df2017-01-09 10:53:07 -0800283 OPENSSL_memset(nonce, 0, nonce_len);
Adam Langley4139edb2016-01-13 15:00:54 -0800284 } else {
Robert Sloan69939df2017-01-09 10:53:07 -0800285 OPENSSL_memcpy(nonce, aead->fixed_nonce, aead->fixed_nonce_len);
Adam Langley4139edb2016-01-13 15:00:54 -0800286 nonce_len += aead->fixed_nonce_len;
287 }
288
289 /* Select the variable nonce. */
Adam Langleyf4e42722015-06-04 17:45:09 -0700290 if (aead->random_variable_nonce) {
291 assert(aead->variable_nonce_included_in_record);
292 if (!RAND_bytes(nonce + nonce_len, aead->variable_nonce_len)) {
293 return 0;
294 }
295 } else {
296 /* When sending we use the sequence number as the variable part of the
297 * nonce. */
298 assert(aead->variable_nonce_len == 8);
Robert Sloan69939df2017-01-09 10:53:07 -0800299 OPENSSL_memcpy(nonce + nonce_len, seqnum, aead->variable_nonce_len);
Adam Langleyf4e42722015-06-04 17:45:09 -0700300 }
301 nonce_len += aead->variable_nonce_len;
302
303 /* Emit the variable nonce if included in the record. */
304 size_t extra_len = 0;
305 if (aead->variable_nonce_included_in_record) {
Adam Langley4139edb2016-01-13 15:00:54 -0800306 assert(!aead->xor_fixed_nonce);
Adam Langleyf4e42722015-06-04 17:45:09 -0700307 if (max_out < aead->variable_nonce_len) {
Kenny Rootb8494592015-09-25 02:29:14 +0000308 OPENSSL_PUT_ERROR(SSL, SSL_R_BUFFER_TOO_SMALL);
Adam Langleyf4e42722015-06-04 17:45:09 -0700309 return 0;
310 }
311 if (out < in + in_len && in < out + aead->variable_nonce_len) {
Kenny Rootb8494592015-09-25 02:29:14 +0000312 OPENSSL_PUT_ERROR(SSL, SSL_R_OUTPUT_ALIASES_INPUT);
Adam Langleyf4e42722015-06-04 17:45:09 -0700313 return 0;
314 }
Robert Sloan69939df2017-01-09 10:53:07 -0800315 OPENSSL_memcpy(out, nonce + aead->fixed_nonce_len,
316 aead->variable_nonce_len);
Adam Langleyf4e42722015-06-04 17:45:09 -0700317 extra_len = aead->variable_nonce_len;
318 out += aead->variable_nonce_len;
319 max_out -= aead->variable_nonce_len;
320 }
321
Adam Langley4139edb2016-01-13 15:00:54 -0800322 /* XOR the fixed nonce, if necessary. */
323 if (aead->xor_fixed_nonce) {
324 assert(nonce_len == aead->fixed_nonce_len);
David Benjamin7c0d06c2016-08-11 13:26:41 -0400325 for (size_t i = 0; i < aead->fixed_nonce_len; i++) {
Adam Langley4139edb2016-01-13 15:00:54 -0800326 nonce[i] ^= aead->fixed_nonce[i];
327 }
328 }
329
Adam Langleyf4e42722015-06-04 17:45:09 -0700330 if (!EVP_AEAD_CTX_seal(&aead->ctx, out, out_len, max_out, nonce, nonce_len,
331 in, in_len, ad, ad_len)) {
332 return 0;
333 }
334 *out_len += extra_len;
335 return 1;
336}