blob: 88daddd9a9972b9ee30a6a5ad3c1adc40af1233f [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
25#include "internal.h"
26
27
28OPENSSL_COMPILE_ASSERT(EVP_AEAD_MAX_NONCE_LENGTH < 256,
29 variable_nonce_len_doesnt_fit_in_uint8_t);
30
31SSL_AEAD_CTX *SSL_AEAD_CTX_new(enum evp_aead_direction_t direction,
32 uint16_t version, const SSL_CIPHER *cipher,
33 const uint8_t *enc_key, size_t enc_key_len,
34 const uint8_t *mac_key, size_t mac_key_len,
35 const uint8_t *fixed_iv, size_t fixed_iv_len) {
36 const EVP_AEAD *aead;
37 size_t discard;
38 if (!ssl_cipher_get_evp_aead(&aead, &discard, &discard, cipher, version)) {
David Benjamin3570d732015-06-29 00:28:17 -040039 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamin31a07792015-03-03 14:20:26 -050040 return 0;
41 }
42
43 uint8_t merged_key[EVP_AEAD_MAX_KEY_LENGTH];
44 if (mac_key_len > 0) {
45 /* This is a "stateful" AEAD (for compatibility with pre-AEAD cipher
46 * suites). */
47 if (mac_key_len + enc_key_len + fixed_iv_len > sizeof(merged_key)) {
David Benjamin3570d732015-06-29 00:28:17 -040048 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamin31a07792015-03-03 14:20:26 -050049 return 0;
50 }
51 memcpy(merged_key, mac_key, mac_key_len);
52 memcpy(merged_key + mac_key_len, enc_key, enc_key_len);
53 memcpy(merged_key + mac_key_len + enc_key_len, fixed_iv, fixed_iv_len);
54 enc_key = merged_key;
55 enc_key_len += mac_key_len;
56 enc_key_len += fixed_iv_len;
57 }
58
Brian Smith5ba06892016-02-07 09:36:04 -100059 SSL_AEAD_CTX *aead_ctx = OPENSSL_malloc(sizeof(SSL_AEAD_CTX));
David Benjamin31a07792015-03-03 14:20:26 -050060 if (aead_ctx == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -040061 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin31a07792015-03-03 14:20:26 -050062 return NULL;
63 }
64 memset(aead_ctx, 0, sizeof(SSL_AEAD_CTX));
65 aead_ctx->cipher = cipher;
66
67 if (!EVP_AEAD_CTX_init_with_direction(
68 &aead_ctx->ctx, aead, enc_key, enc_key_len,
69 EVP_AEAD_DEFAULT_TAG_LENGTH, direction)) {
70 OPENSSL_free(aead_ctx);
71 return NULL;
72 }
73
74 assert(EVP_AEAD_nonce_length(aead) <= EVP_AEAD_MAX_NONCE_LENGTH);
75 aead_ctx->variable_nonce_len = (uint8_t)EVP_AEAD_nonce_length(aead);
David Benjamin51a01a52015-10-29 13:19:56 -040076 if (mac_key_len == 0) {
David Benjamin13414b32015-12-09 23:02:39 -050077 assert(fixed_iv_len <= sizeof(aead_ctx->fixed_nonce));
David Benjamin31a07792015-03-03 14:20:26 -050078 memcpy(aead_ctx->fixed_nonce, fixed_iv, fixed_iv_len);
79 aead_ctx->fixed_nonce_len = fixed_iv_len;
David Benjamin13414b32015-12-09 23:02:39 -050080
81 if (cipher->algorithm_enc & SSL_CHACHA20POLY1305) {
82 /* The fixed nonce into the actual nonce (the sequence number). */
83 aead_ctx->xor_fixed_nonce = 1;
84 aead_ctx->variable_nonce_len = 8;
85 } else {
86 /* The fixed IV is prepended to the nonce. */
87 assert(fixed_iv_len <= aead_ctx->variable_nonce_len);
88 aead_ctx->variable_nonce_len -= fixed_iv_len;
89 }
90
David Benjaminb2a985b2015-06-21 15:13:57 -040091 /* AES-GCM uses an explicit nonce. */
92 if (cipher->algorithm_enc & (SSL_AES128GCM | SSL_AES256GCM)) {
93 aead_ctx->variable_nonce_included_in_record = 1;
94 }
Steven Valdez494650c2016-05-24 12:43:04 -040095
96 /* The TLS 1.3 construction XORs the fixed nonce into the sequence number
97 * and omits the additional data. */
98 if (version >= TLS1_3_VERSION) {
99 aead_ctx->xor_fixed_nonce = 1;
100 aead_ctx->variable_nonce_len = 8;
101 aead_ctx->variable_nonce_included_in_record = 0;
102 aead_ctx->omit_ad = 1;
103 }
David Benjamin31a07792015-03-03 14:20:26 -0500104 } else {
105 aead_ctx->variable_nonce_included_in_record = 1;
106 aead_ctx->random_variable_nonce = 1;
107 aead_ctx->omit_length_in_ad = 1;
108 aead_ctx->omit_version_in_ad = (version == SSL3_VERSION);
109 }
110
111 return aead_ctx;
112}
113
114void SSL_AEAD_CTX_free(SSL_AEAD_CTX *aead) {
115 if (aead == NULL) {
116 return;
117 }
118 EVP_AEAD_CTX_cleanup(&aead->ctx);
119 OPENSSL_free(aead);
120}
121
122size_t SSL_AEAD_CTX_explicit_nonce_len(SSL_AEAD_CTX *aead) {
David Benjaminbf82aed2016-03-01 22:57:40 -0500123#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
124 aead = NULL;
125#endif
126
David Benjamin31a07792015-03-03 14:20:26 -0500127 if (aead != NULL && aead->variable_nonce_included_in_record) {
128 return aead->variable_nonce_len;
129 }
130 return 0;
131}
132
133size_t SSL_AEAD_CTX_max_overhead(SSL_AEAD_CTX *aead) {
David Benjaminbf82aed2016-03-01 22:57:40 -0500134#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
135 aead = NULL;
136#endif
137
David Benjamin31a07792015-03-03 14:20:26 -0500138 if (aead == NULL) {
139 return 0;
140 }
141 return EVP_AEAD_max_overhead(aead->ctx.aead) +
David Benjaminbf82aed2016-03-01 22:57:40 -0500142 SSL_AEAD_CTX_explicit_nonce_len(aead);
David Benjamin31a07792015-03-03 14:20:26 -0500143}
144
145/* ssl_aead_ctx_get_ad writes the additional data for |aead| into |out| and
146 * returns the number of bytes written. */
147static size_t ssl_aead_ctx_get_ad(SSL_AEAD_CTX *aead, uint8_t out[13],
148 uint8_t type, uint16_t wire_version,
149 const uint8_t seqnum[8],
150 size_t plaintext_len) {
Steven Valdez494650c2016-05-24 12:43:04 -0400151 if (aead->omit_ad) {
152 return 0;
153 }
154
David Benjamin31a07792015-03-03 14:20:26 -0500155 memcpy(out, seqnum, 8);
156 size_t len = 8;
157 out[len++] = type;
158 if (!aead->omit_version_in_ad) {
159 out[len++] = (uint8_t)(wire_version >> 8);
160 out[len++] = (uint8_t)wire_version;
161 }
162 if (!aead->omit_length_in_ad) {
163 out[len++] = (uint8_t)(plaintext_len >> 8);
164 out[len++] = (uint8_t)plaintext_len;
165 }
166 return len;
167}
168
David Benjamina7810c12016-06-06 18:54:51 -0400169int SSL_AEAD_CTX_open(SSL_AEAD_CTX *aead, CBS *out, uint8_t type,
170 uint16_t wire_version, const uint8_t seqnum[8],
171 uint8_t *in, size_t in_len) {
David Benjaminbf82aed2016-03-01 22:57:40 -0500172#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
173 aead = NULL;
174#endif
175
David Benjamin31a07792015-03-03 14:20:26 -0500176 if (aead == NULL) {
177 /* Handle the initial NULL cipher. */
David Benjamina7810c12016-06-06 18:54:51 -0400178 CBS_init(out, in, in_len);
David Benjamin31a07792015-03-03 14:20:26 -0500179 return 1;
180 }
181
182 /* TLS 1.2 AEADs include the length in the AD and are assumed to have fixed
183 * overhead. Otherwise the parameter is unused. */
184 size_t plaintext_len = 0;
185 if (!aead->omit_length_in_ad) {
186 size_t overhead = SSL_AEAD_CTX_max_overhead(aead);
187 if (in_len < overhead) {
188 /* Publicly invalid. */
David Benjamin3570d732015-06-29 00:28:17 -0400189 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_PACKET_LENGTH);
David Benjamin31a07792015-03-03 14:20:26 -0500190 return 0;
191 }
192 plaintext_len = in_len - overhead;
193 }
194 uint8_t ad[13];
195 size_t ad_len = ssl_aead_ctx_get_ad(aead, ad, type, wire_version, seqnum,
196 plaintext_len);
197
198 /* Assemble the nonce. */
199 uint8_t nonce[EVP_AEAD_MAX_NONCE_LENGTH];
200 size_t nonce_len = 0;
David Benjamin13414b32015-12-09 23:02:39 -0500201
202 /* Prepend the fixed nonce, or left-pad with zeros if XORing. */
203 if (aead->xor_fixed_nonce) {
204 nonce_len = aead->fixed_nonce_len - aead->variable_nonce_len;
205 memset(nonce, 0, nonce_len);
206 } else {
207 memcpy(nonce, aead->fixed_nonce, aead->fixed_nonce_len);
208 nonce_len += aead->fixed_nonce_len;
209 }
210
211 /* Add the variable nonce. */
David Benjamin31a07792015-03-03 14:20:26 -0500212 if (aead->variable_nonce_included_in_record) {
213 if (in_len < aead->variable_nonce_len) {
214 /* Publicly invalid. */
David Benjamin3570d732015-06-29 00:28:17 -0400215 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_PACKET_LENGTH);
David Benjamin31a07792015-03-03 14:20:26 -0500216 return 0;
217 }
218 memcpy(nonce + nonce_len, in, aead->variable_nonce_len);
219 in += aead->variable_nonce_len;
220 in_len -= aead->variable_nonce_len;
221 } else {
222 assert(aead->variable_nonce_len == 8);
223 memcpy(nonce + nonce_len, seqnum, aead->variable_nonce_len);
224 }
225 nonce_len += aead->variable_nonce_len;
226
David Benjamin13414b32015-12-09 23:02:39 -0500227 /* XOR the fixed nonce, if necessary. */
228 if (aead->xor_fixed_nonce) {
229 assert(nonce_len == aead->fixed_nonce_len);
230 size_t i;
231 for (i = 0; i < aead->fixed_nonce_len; i++) {
232 nonce[i] ^= aead->fixed_nonce[i];
233 }
234 }
235
David Benjamina7810c12016-06-06 18:54:51 -0400236 /* Decrypt in-place. */
237 size_t len;
238 if (!EVP_AEAD_CTX_open(&aead->ctx, in, &len, in_len, nonce, nonce_len,
239 in, in_len, ad, ad_len)) {
240 return 0;
241 }
242 CBS_init(out, in, len);
243 return 1;
David Benjamin31a07792015-03-03 14:20:26 -0500244}
245
246int SSL_AEAD_CTX_seal(SSL_AEAD_CTX *aead, uint8_t *out, size_t *out_len,
247 size_t max_out, uint8_t type, uint16_t wire_version,
248 const uint8_t seqnum[8], const uint8_t *in,
249 size_t in_len) {
David Benjaminbf82aed2016-03-01 22:57:40 -0500250#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
251 aead = NULL;
252#endif
253
David Benjamin31a07792015-03-03 14:20:26 -0500254 if (aead == NULL) {
255 /* Handle the initial NULL cipher. */
256 if (in_len > max_out) {
David Benjamin3570d732015-06-29 00:28:17 -0400257 OPENSSL_PUT_ERROR(SSL, SSL_R_BUFFER_TOO_SMALL);
David Benjamin31a07792015-03-03 14:20:26 -0500258 return 0;
259 }
260 memmove(out, in, in_len);
261 *out_len = in_len;
262 return 1;
263 }
264
265 uint8_t ad[13];
266 size_t ad_len = ssl_aead_ctx_get_ad(aead, ad, type, wire_version, seqnum,
267 in_len);
268
269 /* Assemble the nonce. */
270 uint8_t nonce[EVP_AEAD_MAX_NONCE_LENGTH];
271 size_t nonce_len = 0;
David Benjamin13414b32015-12-09 23:02:39 -0500272
273 /* Prepend the fixed nonce, or left-pad with zeros if XORing. */
274 if (aead->xor_fixed_nonce) {
275 nonce_len = aead->fixed_nonce_len - aead->variable_nonce_len;
276 memset(nonce, 0, nonce_len);
277 } else {
278 memcpy(nonce, aead->fixed_nonce, aead->fixed_nonce_len);
279 nonce_len += aead->fixed_nonce_len;
280 }
281
282 /* Select the variable nonce. */
David Benjamin31a07792015-03-03 14:20:26 -0500283 if (aead->random_variable_nonce) {
284 assert(aead->variable_nonce_included_in_record);
285 if (!RAND_bytes(nonce + nonce_len, aead->variable_nonce_len)) {
286 return 0;
287 }
288 } else {
289 /* When sending we use the sequence number as the variable part of the
290 * nonce. */
291 assert(aead->variable_nonce_len == 8);
David Benjamin13414b32015-12-09 23:02:39 -0500292 memcpy(nonce + nonce_len, seqnum, aead->variable_nonce_len);
David Benjamin31a07792015-03-03 14:20:26 -0500293 }
294 nonce_len += aead->variable_nonce_len;
295
296 /* Emit the variable nonce if included in the record. */
297 size_t extra_len = 0;
298 if (aead->variable_nonce_included_in_record) {
David Benjamin13414b32015-12-09 23:02:39 -0500299 assert(!aead->xor_fixed_nonce);
David Benjamin31a07792015-03-03 14:20:26 -0500300 if (max_out < aead->variable_nonce_len) {
David Benjamin3570d732015-06-29 00:28:17 -0400301 OPENSSL_PUT_ERROR(SSL, SSL_R_BUFFER_TOO_SMALL);
David Benjamin31a07792015-03-03 14:20:26 -0500302 return 0;
303 }
304 if (out < in + in_len && in < out + aead->variable_nonce_len) {
David Benjamin3570d732015-06-29 00:28:17 -0400305 OPENSSL_PUT_ERROR(SSL, SSL_R_OUTPUT_ALIASES_INPUT);
David Benjamin31a07792015-03-03 14:20:26 -0500306 return 0;
307 }
308 memcpy(out, nonce + aead->fixed_nonce_len, aead->variable_nonce_len);
309 extra_len = aead->variable_nonce_len;
310 out += aead->variable_nonce_len;
311 max_out -= aead->variable_nonce_len;
312 }
313
David Benjamin13414b32015-12-09 23:02:39 -0500314 /* XOR the fixed nonce, if necessary. */
315 if (aead->xor_fixed_nonce) {
316 assert(nonce_len == aead->fixed_nonce_len);
317 size_t i;
318 for (i = 0; i < aead->fixed_nonce_len; i++) {
319 nonce[i] ^= aead->fixed_nonce[i];
320 }
321 }
322
David Benjamin31a07792015-03-03 14:20:26 -0500323 if (!EVP_AEAD_CTX_seal(&aead->ctx, out, out_len, max_out, nonce, nonce_len,
324 in, in_len, ad, ad_len)) {
325 return 0;
326 }
327 *out_len += extra_len;
328 return 1;
329}