blob: f50ed79df4733af4cb8f491b9cb4de551d35b232 [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
15#include <assert.h>
16#include <string.h>
17
18#include <openssl/aead.h>
19#include <openssl/err.h>
20#include <openssl/rand.h>
21#include <openssl/type_check.h>
22
23#include "internal.h"
24
25
26OPENSSL_COMPILE_ASSERT(EVP_AEAD_MAX_NONCE_LENGTH < 256,
27 variable_nonce_len_doesnt_fit_in_uint8_t);
28
29SSL_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;
35 size_t discard;
36 if (!ssl_cipher_get_evp_aead(&aead, &discard, &discard, cipher, version)) {
David Benjamin3570d732015-06-29 00:28:17 -040037 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamin31a07792015-03-03 14:20:26 -050038 return 0;
39 }
40
41 uint8_t merged_key[EVP_AEAD_MAX_KEY_LENGTH];
42 if (mac_key_len > 0) {
43 /* This is a "stateful" AEAD (for compatibility with pre-AEAD cipher
44 * suites). */
45 if (mac_key_len + enc_key_len + fixed_iv_len > sizeof(merged_key)) {
David Benjamin3570d732015-06-29 00:28:17 -040046 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamin31a07792015-03-03 14:20:26 -050047 return 0;
48 }
49 memcpy(merged_key, mac_key, mac_key_len);
50 memcpy(merged_key + mac_key_len, enc_key, enc_key_len);
51 memcpy(merged_key + mac_key_len + enc_key_len, fixed_iv, fixed_iv_len);
52 enc_key = merged_key;
53 enc_key_len += mac_key_len;
54 enc_key_len += fixed_iv_len;
55 }
56
57 SSL_AEAD_CTX *aead_ctx = (SSL_AEAD_CTX *)OPENSSL_malloc(sizeof(SSL_AEAD_CTX));
58 if (aead_ctx == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -040059 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
David Benjamin31a07792015-03-03 14:20:26 -050060 return NULL;
61 }
62 memset(aead_ctx, 0, sizeof(SSL_AEAD_CTX));
63 aead_ctx->cipher = cipher;
64
65 if (!EVP_AEAD_CTX_init_with_direction(
66 &aead_ctx->ctx, aead, enc_key, enc_key_len,
67 EVP_AEAD_DEFAULT_TAG_LENGTH, direction)) {
68 OPENSSL_free(aead_ctx);
69 return NULL;
70 }
71
72 assert(EVP_AEAD_nonce_length(aead) <= EVP_AEAD_MAX_NONCE_LENGTH);
73 aead_ctx->variable_nonce_len = (uint8_t)EVP_AEAD_nonce_length(aead);
74 if (mac_key_len == 0) {
75 /* For a real AEAD, the IV is the fixed part of the nonce. */
76 if (fixed_iv_len > sizeof(aead_ctx->fixed_nonce) ||
77 fixed_iv_len > aead_ctx->variable_nonce_len) {
78 SSL_AEAD_CTX_free(aead_ctx);
David Benjamin3570d732015-06-29 00:28:17 -040079 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamin31a07792015-03-03 14:20:26 -050080 return 0;
81 }
82 aead_ctx->variable_nonce_len -= fixed_iv_len;
83
84 memcpy(aead_ctx->fixed_nonce, fixed_iv, fixed_iv_len);
85 aead_ctx->fixed_nonce_len = fixed_iv_len;
David Benjaminb2a985b2015-06-21 15:13:57 -040086 /* AES-GCM uses an explicit nonce. */
87 if (cipher->algorithm_enc & (SSL_AES128GCM | SSL_AES256GCM)) {
88 aead_ctx->variable_nonce_included_in_record = 1;
89 }
David Benjamin31a07792015-03-03 14:20:26 -050090 } else {
91 aead_ctx->variable_nonce_included_in_record = 1;
92 aead_ctx->random_variable_nonce = 1;
93 aead_ctx->omit_length_in_ad = 1;
94 aead_ctx->omit_version_in_ad = (version == SSL3_VERSION);
95 }
96
97 return aead_ctx;
98}
99
100void SSL_AEAD_CTX_free(SSL_AEAD_CTX *aead) {
101 if (aead == NULL) {
102 return;
103 }
104 EVP_AEAD_CTX_cleanup(&aead->ctx);
105 OPENSSL_free(aead);
106}
107
108size_t SSL_AEAD_CTX_explicit_nonce_len(SSL_AEAD_CTX *aead) {
109 if (aead != NULL && aead->variable_nonce_included_in_record) {
110 return aead->variable_nonce_len;
111 }
112 return 0;
113}
114
115size_t SSL_AEAD_CTX_max_overhead(SSL_AEAD_CTX *aead) {
116 if (aead == NULL) {
117 return 0;
118 }
119 return EVP_AEAD_max_overhead(aead->ctx.aead) +
120 SSL_AEAD_CTX_explicit_nonce_len(aead);
121}
122
123/* ssl_aead_ctx_get_ad writes the additional data for |aead| into |out| and
124 * returns the number of bytes written. */
125static size_t ssl_aead_ctx_get_ad(SSL_AEAD_CTX *aead, uint8_t out[13],
126 uint8_t type, uint16_t wire_version,
127 const uint8_t seqnum[8],
128 size_t plaintext_len) {
129 memcpy(out, seqnum, 8);
130 size_t len = 8;
131 out[len++] = type;
132 if (!aead->omit_version_in_ad) {
133 out[len++] = (uint8_t)(wire_version >> 8);
134 out[len++] = (uint8_t)wire_version;
135 }
136 if (!aead->omit_length_in_ad) {
137 out[len++] = (uint8_t)(plaintext_len >> 8);
138 out[len++] = (uint8_t)plaintext_len;
139 }
140 return len;
141}
142
143int SSL_AEAD_CTX_open(SSL_AEAD_CTX *aead, uint8_t *out, size_t *out_len,
144 size_t max_out, uint8_t type, uint16_t wire_version,
145 const uint8_t seqnum[8], const uint8_t *in,
146 size_t in_len) {
147 if (aead == NULL) {
148 /* Handle the initial NULL cipher. */
149 if (in_len > max_out) {
David Benjamin3570d732015-06-29 00:28:17 -0400150 OPENSSL_PUT_ERROR(SSL, SSL_R_BUFFER_TOO_SMALL);
David Benjamin31a07792015-03-03 14:20:26 -0500151 return 0;
152 }
153 memmove(out, in, in_len);
154 *out_len = in_len;
155 return 1;
156 }
157
158 /* TLS 1.2 AEADs include the length in the AD and are assumed to have fixed
159 * overhead. Otherwise the parameter is unused. */
160 size_t plaintext_len = 0;
161 if (!aead->omit_length_in_ad) {
162 size_t overhead = SSL_AEAD_CTX_max_overhead(aead);
163 if (in_len < overhead) {
164 /* Publicly invalid. */
David Benjamin3570d732015-06-29 00:28:17 -0400165 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_PACKET_LENGTH);
David Benjamin31a07792015-03-03 14:20:26 -0500166 return 0;
167 }
168 plaintext_len = in_len - overhead;
169 }
170 uint8_t ad[13];
171 size_t ad_len = ssl_aead_ctx_get_ad(aead, ad, type, wire_version, seqnum,
172 plaintext_len);
173
174 /* Assemble the nonce. */
175 uint8_t nonce[EVP_AEAD_MAX_NONCE_LENGTH];
176 size_t nonce_len = 0;
177 memcpy(nonce, aead->fixed_nonce, aead->fixed_nonce_len);
178 nonce_len += aead->fixed_nonce_len;
179 if (aead->variable_nonce_included_in_record) {
180 if (in_len < aead->variable_nonce_len) {
181 /* Publicly invalid. */
David Benjamin3570d732015-06-29 00:28:17 -0400182 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_PACKET_LENGTH);
David Benjamin31a07792015-03-03 14:20:26 -0500183 return 0;
184 }
185 memcpy(nonce + nonce_len, in, aead->variable_nonce_len);
186 in += aead->variable_nonce_len;
187 in_len -= aead->variable_nonce_len;
188 } else {
189 assert(aead->variable_nonce_len == 8);
190 memcpy(nonce + nonce_len, seqnum, aead->variable_nonce_len);
191 }
192 nonce_len += aead->variable_nonce_len;
193
194 return EVP_AEAD_CTX_open(&aead->ctx, out, out_len, max_out, nonce, nonce_len,
195 in, in_len, ad, ad_len);
196}
197
198int SSL_AEAD_CTX_seal(SSL_AEAD_CTX *aead, uint8_t *out, size_t *out_len,
199 size_t max_out, uint8_t type, uint16_t wire_version,
200 const uint8_t seqnum[8], const uint8_t *in,
201 size_t in_len) {
202 if (aead == NULL) {
203 /* Handle the initial NULL cipher. */
204 if (in_len > max_out) {
David Benjamin3570d732015-06-29 00:28:17 -0400205 OPENSSL_PUT_ERROR(SSL, SSL_R_BUFFER_TOO_SMALL);
David Benjamin31a07792015-03-03 14:20:26 -0500206 return 0;
207 }
208 memmove(out, in, in_len);
209 *out_len = in_len;
210 return 1;
211 }
212
213 uint8_t ad[13];
214 size_t ad_len = ssl_aead_ctx_get_ad(aead, ad, type, wire_version, seqnum,
215 in_len);
216
217 /* Assemble the nonce. */
218 uint8_t nonce[EVP_AEAD_MAX_NONCE_LENGTH];
219 size_t nonce_len = 0;
220 memcpy(nonce, aead->fixed_nonce, aead->fixed_nonce_len);
221 nonce_len += aead->fixed_nonce_len;
222 if (aead->random_variable_nonce) {
223 assert(aead->variable_nonce_included_in_record);
224 if (!RAND_bytes(nonce + nonce_len, aead->variable_nonce_len)) {
225 return 0;
226 }
227 } else {
228 /* When sending we use the sequence number as the variable part of the
229 * nonce. */
230 assert(aead->variable_nonce_len == 8);
231 memcpy(nonce + nonce_len, ad, aead->variable_nonce_len);
232 }
233 nonce_len += aead->variable_nonce_len;
234
235 /* Emit the variable nonce if included in the record. */
236 size_t extra_len = 0;
237 if (aead->variable_nonce_included_in_record) {
238 if (max_out < aead->variable_nonce_len) {
David Benjamin3570d732015-06-29 00:28:17 -0400239 OPENSSL_PUT_ERROR(SSL, SSL_R_BUFFER_TOO_SMALL);
David Benjamin31a07792015-03-03 14:20:26 -0500240 return 0;
241 }
242 if (out < in + in_len && in < out + aead->variable_nonce_len) {
David Benjamin3570d732015-06-29 00:28:17 -0400243 OPENSSL_PUT_ERROR(SSL, SSL_R_OUTPUT_ALIASES_INPUT);
David Benjamin31a07792015-03-03 14:20:26 -0500244 return 0;
245 }
246 memcpy(out, nonce + aead->fixed_nonce_len, aead->variable_nonce_len);
247 extra_len = aead->variable_nonce_len;
248 out += aead->variable_nonce_len;
249 max_out -= aead->variable_nonce_len;
250 }
251
252 if (!EVP_AEAD_CTX_seal(&aead->ctx, out, out_len, max_out, nonce, nonce_len,
253 in, in_len, ad, ad_len)) {
254 return 0;
255 }
256 *out_len += extra_len;
257 return 1;
258}