blob: f9001c7cb473337854554161452d62869e844835 [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
59 SSL_AEAD_CTX *aead_ctx = (SSL_AEAD_CTX *)OPENSSL_malloc(sizeof(SSL_AEAD_CTX));
60 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 Benjamin31a07792015-03-03 14:20:26 -050077 /* For a real AEAD, the IV is the fixed part of the nonce. */
78 if (fixed_iv_len > sizeof(aead_ctx->fixed_nonce) ||
79 fixed_iv_len > aead_ctx->variable_nonce_len) {
80 SSL_AEAD_CTX_free(aead_ctx);
David Benjamin3570d732015-06-29 00:28:17 -040081 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamin31a07792015-03-03 14:20:26 -050082 return 0;
83 }
84 aead_ctx->variable_nonce_len -= fixed_iv_len;
85
86 memcpy(aead_ctx->fixed_nonce, fixed_iv, fixed_iv_len);
87 aead_ctx->fixed_nonce_len = fixed_iv_len;
David Benjaminb2a985b2015-06-21 15:13:57 -040088 /* AES-GCM uses an explicit nonce. */
89 if (cipher->algorithm_enc & (SSL_AES128GCM | SSL_AES256GCM)) {
90 aead_ctx->variable_nonce_included_in_record = 1;
91 }
David Benjamin31a07792015-03-03 14:20:26 -050092 } else {
93 aead_ctx->variable_nonce_included_in_record = 1;
94 aead_ctx->random_variable_nonce = 1;
95 aead_ctx->omit_length_in_ad = 1;
96 aead_ctx->omit_version_in_ad = (version == SSL3_VERSION);
97 }
98
99 return aead_ctx;
100}
101
102void SSL_AEAD_CTX_free(SSL_AEAD_CTX *aead) {
103 if (aead == NULL) {
104 return;
105 }
106 EVP_AEAD_CTX_cleanup(&aead->ctx);
107 OPENSSL_free(aead);
108}
109
110size_t SSL_AEAD_CTX_explicit_nonce_len(SSL_AEAD_CTX *aead) {
111 if (aead != NULL && aead->variable_nonce_included_in_record) {
112 return aead->variable_nonce_len;
113 }
114 return 0;
115}
116
117size_t SSL_AEAD_CTX_max_overhead(SSL_AEAD_CTX *aead) {
118 if (aead == NULL) {
119 return 0;
120 }
121 return EVP_AEAD_max_overhead(aead->ctx.aead) +
122 SSL_AEAD_CTX_explicit_nonce_len(aead);
123}
124
125/* ssl_aead_ctx_get_ad writes the additional data for |aead| into |out| and
126 * returns the number of bytes written. */
127static size_t ssl_aead_ctx_get_ad(SSL_AEAD_CTX *aead, uint8_t out[13],
128 uint8_t type, uint16_t wire_version,
129 const uint8_t seqnum[8],
130 size_t plaintext_len) {
131 memcpy(out, seqnum, 8);
132 size_t len = 8;
133 out[len++] = type;
134 if (!aead->omit_version_in_ad) {
135 out[len++] = (uint8_t)(wire_version >> 8);
136 out[len++] = (uint8_t)wire_version;
137 }
138 if (!aead->omit_length_in_ad) {
139 out[len++] = (uint8_t)(plaintext_len >> 8);
140 out[len++] = (uint8_t)plaintext_len;
141 }
142 return len;
143}
144
145int SSL_AEAD_CTX_open(SSL_AEAD_CTX *aead, uint8_t *out, size_t *out_len,
146 size_t max_out, uint8_t type, uint16_t wire_version,
147 const uint8_t seqnum[8], const uint8_t *in,
148 size_t in_len) {
149 if (aead == NULL) {
150 /* Handle the initial NULL cipher. */
151 if (in_len > max_out) {
David Benjamin3570d732015-06-29 00:28:17 -0400152 OPENSSL_PUT_ERROR(SSL, SSL_R_BUFFER_TOO_SMALL);
David Benjamin31a07792015-03-03 14:20:26 -0500153 return 0;
154 }
155 memmove(out, in, in_len);
156 *out_len = in_len;
157 return 1;
158 }
159
160 /* TLS 1.2 AEADs include the length in the AD and are assumed to have fixed
161 * overhead. Otherwise the parameter is unused. */
162 size_t plaintext_len = 0;
163 if (!aead->omit_length_in_ad) {
164 size_t overhead = SSL_AEAD_CTX_max_overhead(aead);
165 if (in_len < overhead) {
166 /* Publicly invalid. */
David Benjamin3570d732015-06-29 00:28:17 -0400167 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_PACKET_LENGTH);
David Benjamin31a07792015-03-03 14:20:26 -0500168 return 0;
169 }
170 plaintext_len = in_len - overhead;
171 }
172 uint8_t ad[13];
173 size_t ad_len = ssl_aead_ctx_get_ad(aead, ad, type, wire_version, seqnum,
174 plaintext_len);
175
176 /* Assemble the nonce. */
177 uint8_t nonce[EVP_AEAD_MAX_NONCE_LENGTH];
178 size_t nonce_len = 0;
179 memcpy(nonce, aead->fixed_nonce, aead->fixed_nonce_len);
180 nonce_len += aead->fixed_nonce_len;
181 if (aead->variable_nonce_included_in_record) {
182 if (in_len < aead->variable_nonce_len) {
183 /* Publicly invalid. */
David Benjamin3570d732015-06-29 00:28:17 -0400184 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_PACKET_LENGTH);
David Benjamin31a07792015-03-03 14:20:26 -0500185 return 0;
186 }
187 memcpy(nonce + nonce_len, in, aead->variable_nonce_len);
188 in += aead->variable_nonce_len;
189 in_len -= aead->variable_nonce_len;
190 } else {
191 assert(aead->variable_nonce_len == 8);
192 memcpy(nonce + nonce_len, seqnum, aead->variable_nonce_len);
193 }
194 nonce_len += aead->variable_nonce_len;
195
196 return EVP_AEAD_CTX_open(&aead->ctx, out, out_len, max_out, nonce, nonce_len,
197 in, in_len, ad, ad_len);
198}
199
200int SSL_AEAD_CTX_seal(SSL_AEAD_CTX *aead, uint8_t *out, size_t *out_len,
201 size_t max_out, uint8_t type, uint16_t wire_version,
202 const uint8_t seqnum[8], const uint8_t *in,
203 size_t in_len) {
204 if (aead == NULL) {
205 /* Handle the initial NULL cipher. */
206 if (in_len > max_out) {
David Benjamin3570d732015-06-29 00:28:17 -0400207 OPENSSL_PUT_ERROR(SSL, SSL_R_BUFFER_TOO_SMALL);
David Benjamin31a07792015-03-03 14:20:26 -0500208 return 0;
209 }
210 memmove(out, in, in_len);
211 *out_len = in_len;
212 return 1;
213 }
214
215 uint8_t ad[13];
216 size_t ad_len = ssl_aead_ctx_get_ad(aead, ad, type, wire_version, seqnum,
217 in_len);
218
219 /* Assemble the nonce. */
220 uint8_t nonce[EVP_AEAD_MAX_NONCE_LENGTH];
221 size_t nonce_len = 0;
222 memcpy(nonce, aead->fixed_nonce, aead->fixed_nonce_len);
223 nonce_len += aead->fixed_nonce_len;
224 if (aead->random_variable_nonce) {
225 assert(aead->variable_nonce_included_in_record);
226 if (!RAND_bytes(nonce + nonce_len, aead->variable_nonce_len)) {
227 return 0;
228 }
229 } else {
230 /* When sending we use the sequence number as the variable part of the
231 * nonce. */
232 assert(aead->variable_nonce_len == 8);
233 memcpy(nonce + nonce_len, ad, aead->variable_nonce_len);
234 }
235 nonce_len += aead->variable_nonce_len;
236
237 /* Emit the variable nonce if included in the record. */
238 size_t extra_len = 0;
239 if (aead->variable_nonce_included_in_record) {
240 if (max_out < aead->variable_nonce_len) {
David Benjamin3570d732015-06-29 00:28:17 -0400241 OPENSSL_PUT_ERROR(SSL, SSL_R_BUFFER_TOO_SMALL);
David Benjamin31a07792015-03-03 14:20:26 -0500242 return 0;
243 }
244 if (out < in + in_len && in < out + aead->variable_nonce_len) {
David Benjamin3570d732015-06-29 00:28:17 -0400245 OPENSSL_PUT_ERROR(SSL, SSL_R_OUTPUT_ALIASES_INPUT);
David Benjamin31a07792015-03-03 14:20:26 -0500246 return 0;
247 }
248 memcpy(out, nonce + aead->fixed_nonce_len, aead->variable_nonce_len);
249 extra_len = aead->variable_nonce_len;
250 out += aead->variable_nonce_len;
251 max_out -= aead->variable_nonce_len;
252 }
253
254 if (!EVP_AEAD_CTX_seal(&aead->ctx, out, out_len, max_out, nonce, nonce_len,
255 in, in_len, ad, ad_len)) {
256 return 0;
257 }
258 *out_len += extra_len;
259 return 1;
260}