blob: 6d84f7f02c411a5ea7d7099808e1ff692b92ade0 [file] [log] [blame]
David Benjaminea72bd02014-12-21 21:27:41 -05001/* Copyright (c) 2014, 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 <limits.h>
17#include <string.h>
18
19#include <openssl/aead.h>
20#include <openssl/cipher.h>
21#include <openssl/err.h>
22#include <openssl/hmac.h>
David Benjamin9f897b22015-12-07 19:34:31 -050023#include <openssl/md5.h>
David Benjaminea72bd02014-12-21 21:27:41 -050024#include <openssl/mem.h>
25#include <openssl/sha.h>
David Benjaminb34f5102015-02-28 03:59:33 -050026#include <openssl/type_check.h>
David Benjaminea72bd02014-12-21 21:27:41 -050027
Martin Kreichgauer18d9f282017-06-06 12:29:48 -070028#include "../fipsmodule/cipher/internal.h"
Steven Valdezcb966542016-08-17 16:56:14 -040029#include "../internal.h"
David Benjaminea72bd02014-12-21 21:27:41 -050030#include "internal.h"
31
32
33typedef struct {
34 EVP_CIPHER_CTX cipher_ctx;
35 HMAC_CTX hmac_ctx;
David Benjamin808f8322017-08-18 14:06:02 -040036 // mac_key is the portion of the key used for the MAC. It is retained
37 // separately for the constant-time CBC code.
David Benjaminea72bd02014-12-21 21:27:41 -050038 uint8_t mac_key[EVP_MAX_MD_SIZE];
39 uint8_t mac_key_len;
David Benjamin808f8322017-08-18 14:06:02 -040040 // implicit_iv is one iff this is a pre-TLS-1.1 CBC cipher without an explicit
41 // IV.
David Benjaminea72bd02014-12-21 21:27:41 -050042 char implicit_iv;
David Benjaminea72bd02014-12-21 21:27:41 -050043} AEAD_TLS_CTX;
44
David Benjamin5ecfb102018-10-24 17:08:00 -050045OPENSSL_STATIC_ASSERT(EVP_MAX_MD_SIZE < 256,
46 "mac_key_len does not fit in uint8_t");
David Benjaminea72bd02014-12-21 21:27:41 -050047
David Benjamin5ecfb102018-10-24 17:08:00 -050048OPENSSL_STATIC_ASSERT(sizeof(((EVP_AEAD_CTX *)NULL)->state) >=
49 sizeof(AEAD_TLS_CTX),
50 "AEAD state is too small");
Adam Langley35fb5912018-10-16 12:11:51 -070051#if defined(__GNUC__) || defined(__clang__)
David Benjamin5ecfb102018-10-24 17:08:00 -050052OPENSSL_STATIC_ASSERT(alignof(union evp_aead_ctx_st_state) >=
53 alignof(AEAD_TLS_CTX),
54 "AEAD state has insufficient alignment");
Adam Langley35fb5912018-10-16 12:11:51 -070055#endif
56
David Benjaminea72bd02014-12-21 21:27:41 -050057static void aead_tls_cleanup(EVP_AEAD_CTX *ctx) {
Adam Langley35fb5912018-10-16 12:11:51 -070058 AEAD_TLS_CTX *tls_ctx = (AEAD_TLS_CTX *)&ctx->state;
David Benjaminea72bd02014-12-21 21:27:41 -050059 EVP_CIPHER_CTX_cleanup(&tls_ctx->cipher_ctx);
60 HMAC_CTX_cleanup(&tls_ctx->hmac_ctx);
David Benjaminea72bd02014-12-21 21:27:41 -050061}
62
63static int aead_tls_init(EVP_AEAD_CTX *ctx, const uint8_t *key, size_t key_len,
David Benjaminb34f5102015-02-28 03:59:33 -050064 size_t tag_len, enum evp_aead_direction_t dir,
David Benjamind03b5ed2015-03-07 00:10:27 -080065 const EVP_CIPHER *cipher, const EVP_MD *md,
66 char implicit_iv) {
David Benjaminea72bd02014-12-21 21:27:41 -050067 if (tag_len != EVP_AEAD_DEFAULT_TAG_LENGTH &&
68 tag_len != EVP_MD_size(md)) {
David Benjamin3570d732015-06-29 00:28:17 -040069 OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_UNSUPPORTED_TAG_SIZE);
David Benjaminea72bd02014-12-21 21:27:41 -050070 return 0;
71 }
72
73 if (key_len != EVP_AEAD_key_length(ctx->aead)) {
David Benjamin3570d732015-06-29 00:28:17 -040074 OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_KEY_LENGTH);
David Benjaminea72bd02014-12-21 21:27:41 -050075 return 0;
76 }
77
78 size_t mac_key_len = EVP_MD_size(md);
79 size_t enc_key_len = EVP_CIPHER_key_length(cipher);
David Benjaminb34f5102015-02-28 03:59:33 -050080 assert(mac_key_len + enc_key_len +
81 (implicit_iv ? EVP_CIPHER_iv_length(cipher) : 0) == key_len);
David Benjaminea72bd02014-12-21 21:27:41 -050082
Adam Langley35fb5912018-10-16 12:11:51 -070083 AEAD_TLS_CTX *tls_ctx = (AEAD_TLS_CTX *)&ctx->state;
David Benjaminea72bd02014-12-21 21:27:41 -050084 EVP_CIPHER_CTX_init(&tls_ctx->cipher_ctx);
85 HMAC_CTX_init(&tls_ctx->hmac_ctx);
David Benjaminb34f5102015-02-28 03:59:33 -050086 assert(mac_key_len <= EVP_MAX_MD_SIZE);
David Benjamin17cf2cb2016-12-13 01:07:13 -050087 OPENSSL_memcpy(tls_ctx->mac_key, key, mac_key_len);
David Benjaminea72bd02014-12-21 21:27:41 -050088 tls_ctx->mac_key_len = (uint8_t)mac_key_len;
David Benjaminea72bd02014-12-21 21:27:41 -050089 tls_ctx->implicit_iv = implicit_iv;
David Benjaminea72bd02014-12-21 21:27:41 -050090
David Benjaminb34f5102015-02-28 03:59:33 -050091 if (!EVP_CipherInit_ex(&tls_ctx->cipher_ctx, cipher, NULL, &key[mac_key_len],
92 implicit_iv ? &key[mac_key_len + enc_key_len] : NULL,
93 dir == evp_aead_seal) ||
David Benjaminea72bd02014-12-21 21:27:41 -050094 !HMAC_Init_ex(&tls_ctx->hmac_ctx, key, mac_key_len, md, NULL)) {
95 aead_tls_cleanup(ctx);
96 return 0;
97 }
98 EVP_CIPHER_CTX_set_padding(&tls_ctx->cipher_ctx, 0);
99
100 return 1;
101}
102
Martin Kreichgauerabbf3652017-07-21 16:27:54 -0700103static size_t aead_tls_tag_len(const EVP_AEAD_CTX *ctx, const size_t in_len,
104 const size_t extra_in_len) {
105 assert(extra_in_len == 0);
Adam Langley35fb5912018-10-16 12:11:51 -0700106 const AEAD_TLS_CTX *tls_ctx = (AEAD_TLS_CTX *)&ctx->state;
Martin Kreichgauerabbf3652017-07-21 16:27:54 -0700107
108 const size_t hmac_len = HMAC_size(&tls_ctx->hmac_ctx);
109 if (EVP_CIPHER_CTX_mode(&tls_ctx->cipher_ctx) != EVP_CIPH_CBC_MODE) {
110 // The NULL cipher.
111 return hmac_len;
112 }
113
114 const size_t block_size = EVP_CIPHER_CTX_block_size(&tls_ctx->cipher_ctx);
David Benjamin808f8322017-08-18 14:06:02 -0400115 // An overflow of |in_len + hmac_len| doesn't affect the result mod
116 // |block_size|, provided that |block_size| is a smaller power of two.
Martin Kreichgauerabbf3652017-07-21 16:27:54 -0700117 assert(block_size != 0 && (block_size & (block_size - 1)) == 0);
118 const size_t pad_len = block_size - (in_len + hmac_len) % block_size;
119 return hmac_len + pad_len;
120}
121
Martin Kreichgauer18d9f282017-06-06 12:29:48 -0700122static int aead_tls_seal_scatter(const EVP_AEAD_CTX *ctx, uint8_t *out,
123 uint8_t *out_tag, size_t *out_tag_len,
Martin Kreichgauerabbf3652017-07-21 16:27:54 -0700124 const size_t max_out_tag_len,
125 const uint8_t *nonce, const size_t nonce_len,
126 const uint8_t *in, const size_t in_len,
127 const uint8_t *extra_in,
128 const size_t extra_in_len, const uint8_t *ad,
129 const size_t ad_len) {
Adam Langley35fb5912018-10-16 12:11:51 -0700130 AEAD_TLS_CTX *tls_ctx = (AEAD_TLS_CTX *)&ctx->state;
David Benjaminea72bd02014-12-21 21:27:41 -0500131
David Benjaminb34f5102015-02-28 03:59:33 -0500132 if (!tls_ctx->cipher_ctx.encrypt) {
David Benjamin808f8322017-08-18 14:06:02 -0400133 // Unlike a normal AEAD, a TLS AEAD may only be used in one direction.
David Benjamin3570d732015-06-29 00:28:17 -0400134 OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_INVALID_OPERATION);
David Benjaminb34f5102015-02-28 03:59:33 -0500135 return 0;
David Benjaminb34f5102015-02-28 03:59:33 -0500136 }
137
Martin Kreichgauer18d9f282017-06-06 12:29:48 -0700138 if (in_len > INT_MAX) {
David Benjamin808f8322017-08-18 14:06:02 -0400139 // EVP_CIPHER takes int as input.
David Benjamin3570d732015-06-29 00:28:17 -0400140 OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TOO_LARGE);
David Benjaminea72bd02014-12-21 21:27:41 -0500141 return 0;
142 }
143
Martin Kreichgauerabbf3652017-07-21 16:27:54 -0700144 if (max_out_tag_len < aead_tls_tag_len(ctx, in_len, extra_in_len)) {
David Benjamin3570d732015-06-29 00:28:17 -0400145 OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BUFFER_TOO_SMALL);
David Benjaminea72bd02014-12-21 21:27:41 -0500146 return 0;
147 }
148
149 if (nonce_len != EVP_AEAD_nonce_length(ctx->aead)) {
David Benjamin3570d732015-06-29 00:28:17 -0400150 OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_INVALID_NONCE_SIZE);
David Benjaminea72bd02014-12-21 21:27:41 -0500151 return 0;
152 }
153
David Benjamin7f1d5d52015-01-14 17:24:53 -0500154 if (ad_len != 13 - 2 /* length bytes */) {
David Benjamin3570d732015-06-29 00:28:17 -0400155 OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_INVALID_AD_SIZE);
David Benjaminea72bd02014-12-21 21:27:41 -0500156 return 0;
157 }
158
David Benjamin808f8322017-08-18 14:06:02 -0400159 // To allow for CBC mode which changes cipher length, |ad| doesn't include the
160 // length for legacy ciphers.
David Benjaminea72bd02014-12-21 21:27:41 -0500161 uint8_t ad_extra[2];
162 ad_extra[0] = (uint8_t)(in_len >> 8);
163 ad_extra[1] = (uint8_t)(in_len & 0xff);
164
David Benjamin808f8322017-08-18 14:06:02 -0400165 // Compute the MAC. This must be first in case the operation is being done
166 // in-place.
David Benjaminea72bd02014-12-21 21:27:41 -0500167 uint8_t mac[EVP_MAX_MD_SIZE];
168 unsigned mac_len;
David Benjamin1741a9d2015-12-07 19:52:56 -0500169 if (!HMAC_Init_ex(&tls_ctx->hmac_ctx, NULL, 0, NULL, NULL) ||
170 !HMAC_Update(&tls_ctx->hmac_ctx, ad, ad_len) ||
171 !HMAC_Update(&tls_ctx->hmac_ctx, ad_extra, sizeof(ad_extra)) ||
172 !HMAC_Update(&tls_ctx->hmac_ctx, in, in_len) ||
173 !HMAC_Final(&tls_ctx->hmac_ctx, mac, &mac_len)) {
David Benjaminea72bd02014-12-21 21:27:41 -0500174 return 0;
175 }
David Benjaminea72bd02014-12-21 21:27:41 -0500176
David Benjamin808f8322017-08-18 14:06:02 -0400177 // Configure the explicit IV.
David Benjaminea72bd02014-12-21 21:27:41 -0500178 if (EVP_CIPHER_CTX_mode(&tls_ctx->cipher_ctx) == EVP_CIPH_CBC_MODE &&
179 !tls_ctx->implicit_iv &&
180 !EVP_EncryptInit_ex(&tls_ctx->cipher_ctx, NULL, NULL, NULL, nonce)) {
181 return 0;
182 }
183
David Benjamin808f8322017-08-18 14:06:02 -0400184 // Encrypt the input.
David Benjaminea72bd02014-12-21 21:27:41 -0500185 int len;
Martin Kreichgauer18d9f282017-06-06 12:29:48 -0700186 if (!EVP_EncryptUpdate(&tls_ctx->cipher_ctx, out, &len, in, (int)in_len)) {
David Benjaminea72bd02014-12-21 21:27:41 -0500187 return 0;
188 }
David Benjaminea72bd02014-12-21 21:27:41 -0500189
190 unsigned block_size = EVP_CIPHER_CTX_block_size(&tls_ctx->cipher_ctx);
Martin Kreichgauer18d9f282017-06-06 12:29:48 -0700191
David Benjamin808f8322017-08-18 14:06:02 -0400192 // Feed the MAC into the cipher in two steps. First complete the final partial
193 // block from encrypting the input and split the result between |out| and
194 // |out_tag|. Then feed the rest.
Martin Kreichgauer18d9f282017-06-06 12:29:48 -0700195
David Benjamin80ede1d2017-12-21 16:30:28 -0500196 const size_t early_mac_len = (block_size - (in_len % block_size)) % block_size;
Martin Kreichgauer18d9f282017-06-06 12:29:48 -0700197 if (early_mac_len != 0) {
198 assert(len + block_size - early_mac_len == in_len);
199 uint8_t buf[EVP_MAX_BLOCK_LENGTH];
200 int buf_len;
201 if (!EVP_EncryptUpdate(&tls_ctx->cipher_ctx, buf, &buf_len, mac,
202 (int)early_mac_len)) {
203 return 0;
204 }
205 assert(buf_len == (int)block_size);
206 OPENSSL_memcpy(out + len, buf, block_size - early_mac_len);
207 OPENSSL_memcpy(out_tag, buf + block_size - early_mac_len, early_mac_len);
208 }
209 size_t tag_len = early_mac_len;
210
211 if (!EVP_EncryptUpdate(&tls_ctx->cipher_ctx, out_tag + tag_len, &len,
212 mac + tag_len, mac_len - tag_len)) {
213 return 0;
214 }
215 tag_len += len;
216
David Benjaminea72bd02014-12-21 21:27:41 -0500217 if (block_size > 1) {
218 assert(block_size <= 256);
219 assert(EVP_CIPHER_CTX_mode(&tls_ctx->cipher_ctx) == EVP_CIPH_CBC_MODE);
220
David Benjamin808f8322017-08-18 14:06:02 -0400221 // Compute padding and feed that into the cipher.
David Benjaminea72bd02014-12-21 21:27:41 -0500222 uint8_t padding[256];
223 unsigned padding_len = block_size - ((in_len + mac_len) % block_size);
David Benjamin17cf2cb2016-12-13 01:07:13 -0500224 OPENSSL_memset(padding, padding_len - 1, padding_len);
Martin Kreichgauer18d9f282017-06-06 12:29:48 -0700225 if (!EVP_EncryptUpdate(&tls_ctx->cipher_ctx, out_tag + tag_len, &len,
226 padding, (int)padding_len)) {
David Benjaminea72bd02014-12-21 21:27:41 -0500227 return 0;
228 }
Martin Kreichgauer18d9f282017-06-06 12:29:48 -0700229 tag_len += len;
David Benjaminea72bd02014-12-21 21:27:41 -0500230 }
231
Martin Kreichgauer18d9f282017-06-06 12:29:48 -0700232 if (!EVP_EncryptFinal_ex(&tls_ctx->cipher_ctx, out_tag + tag_len, &len)) {
David Benjaminea72bd02014-12-21 21:27:41 -0500233 return 0;
234 }
David Benjamin808f8322017-08-18 14:06:02 -0400235 assert(len == 0); // Padding is explicit.
Martin Kreichgauerabbf3652017-07-21 16:27:54 -0700236 assert(tag_len == aead_tls_tag_len(ctx, in_len, extra_in_len));
David Benjaminea72bd02014-12-21 21:27:41 -0500237
Martin Kreichgauer18d9f282017-06-06 12:29:48 -0700238 *out_tag_len = tag_len;
David Benjaminea72bd02014-12-21 21:27:41 -0500239 return 1;
240}
241
Martin Kreichgauer18d9f282017-06-06 12:29:48 -0700242static int aead_tls_open(const EVP_AEAD_CTX *ctx, uint8_t *out, size_t *out_len,
243 size_t max_out_len, const uint8_t *nonce,
244 size_t nonce_len, const uint8_t *in, size_t in_len,
David Benjaminea72bd02014-12-21 21:27:41 -0500245 const uint8_t *ad, size_t ad_len) {
Adam Langley35fb5912018-10-16 12:11:51 -0700246 AEAD_TLS_CTX *tls_ctx = (AEAD_TLS_CTX *)&ctx->state;
David Benjaminea72bd02014-12-21 21:27:41 -0500247
David Benjaminb34f5102015-02-28 03:59:33 -0500248 if (tls_ctx->cipher_ctx.encrypt) {
David Benjamin808f8322017-08-18 14:06:02 -0400249 // Unlike a normal AEAD, a TLS AEAD may only be used in one direction.
David Benjamin3570d732015-06-29 00:28:17 -0400250 OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_INVALID_OPERATION);
David Benjaminb34f5102015-02-28 03:59:33 -0500251 return 0;
David Benjaminb34f5102015-02-28 03:59:33 -0500252 }
253
David Benjaminea72bd02014-12-21 21:27:41 -0500254 if (in_len < HMAC_size(&tls_ctx->hmac_ctx)) {
David Benjamin3570d732015-06-29 00:28:17 -0400255 OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT);
David Benjaminea72bd02014-12-21 21:27:41 -0500256 return 0;
257 }
258
259 if (max_out_len < in_len) {
David Benjamin808f8322017-08-18 14:06:02 -0400260 // This requires that the caller provide space for the MAC, even though it
261 // will always be removed on return.
David Benjamin3570d732015-06-29 00:28:17 -0400262 OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BUFFER_TOO_SMALL);
David Benjaminea72bd02014-12-21 21:27:41 -0500263 return 0;
264 }
265
266 if (nonce_len != EVP_AEAD_nonce_length(ctx->aead)) {
David Benjamin3570d732015-06-29 00:28:17 -0400267 OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_INVALID_NONCE_SIZE);
David Benjaminea72bd02014-12-21 21:27:41 -0500268 return 0;
269 }
270
David Benjamin7f1d5d52015-01-14 17:24:53 -0500271 if (ad_len != 13 - 2 /* length bytes */) {
David Benjamin3570d732015-06-29 00:28:17 -0400272 OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_INVALID_AD_SIZE);
David Benjaminea72bd02014-12-21 21:27:41 -0500273 return 0;
274 }
275
276 if (in_len > INT_MAX) {
David Benjamin808f8322017-08-18 14:06:02 -0400277 // EVP_CIPHER takes int as input.
David Benjamin3570d732015-06-29 00:28:17 -0400278 OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TOO_LARGE);
David Benjaminea72bd02014-12-21 21:27:41 -0500279 return 0;
280 }
281
David Benjamin808f8322017-08-18 14:06:02 -0400282 // Configure the explicit IV.
David Benjaminea72bd02014-12-21 21:27:41 -0500283 if (EVP_CIPHER_CTX_mode(&tls_ctx->cipher_ctx) == EVP_CIPH_CBC_MODE &&
284 !tls_ctx->implicit_iv &&
285 !EVP_DecryptInit_ex(&tls_ctx->cipher_ctx, NULL, NULL, NULL, nonce)) {
286 return 0;
287 }
288
David Benjamin808f8322017-08-18 14:06:02 -0400289 // Decrypt to get the plaintext + MAC + padding.
David Benjaminea72bd02014-12-21 21:27:41 -0500290 size_t total = 0;
291 int len;
292 if (!EVP_DecryptUpdate(&tls_ctx->cipher_ctx, out, &len, in, (int)in_len)) {
293 return 0;
294 }
295 total += len;
296 if (!EVP_DecryptFinal_ex(&tls_ctx->cipher_ctx, out + total, &len)) {
297 return 0;
298 }
299 total += len;
300 assert(total == in_len);
301
Adam Langleya6a049a2018-12-06 17:15:58 -0800302 CONSTTIME_SECRET(out, total);
303
David Benjamin808f8322017-08-18 14:06:02 -0400304 // Remove CBC padding. Code from here on is timing-sensitive with respect to
305 // |padding_ok| and |data_plus_mac_len| for CBC ciphers.
Adam Langley518ba072017-04-20 13:51:11 -0700306 size_t data_plus_mac_len;
307 crypto_word_t padding_ok;
David Benjaminea72bd02014-12-21 21:27:41 -0500308 if (EVP_CIPHER_CTX_mode(&tls_ctx->cipher_ctx) == EVP_CIPH_CBC_MODE) {
David Benjamin3f26a492016-08-09 23:36:43 -0400309 if (!EVP_tls_cbc_remove_padding(
310 &padding_ok, &data_plus_mac_len, out, total,
311 EVP_CIPHER_CTX_block_size(&tls_ctx->cipher_ctx),
David Benjamin643b77e2017-03-16 13:46:54 -0400312 HMAC_size(&tls_ctx->hmac_ctx))) {
David Benjamin808f8322017-08-18 14:06:02 -0400313 // Publicly invalid. This can be rejected in non-constant time.
David Benjamin3570d732015-06-29 00:28:17 -0400314 OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT);
David Benjaminea72bd02014-12-21 21:27:41 -0500315 return 0;
316 }
317 } else {
Adam Langley518ba072017-04-20 13:51:11 -0700318 padding_ok = CONSTTIME_TRUE_W;
David Benjaminea72bd02014-12-21 21:27:41 -0500319 data_plus_mac_len = total;
David Benjamin808f8322017-08-18 14:06:02 -0400320 // |data_plus_mac_len| = |total| = |in_len| at this point. |in_len| has
321 // already been checked against the MAC size at the top of the function.
David Benjaminea72bd02014-12-21 21:27:41 -0500322 assert(data_plus_mac_len >= HMAC_size(&tls_ctx->hmac_ctx));
323 }
David Benjamin643b77e2017-03-16 13:46:54 -0400324 size_t data_len = data_plus_mac_len - HMAC_size(&tls_ctx->hmac_ctx);
David Benjaminea72bd02014-12-21 21:27:41 -0500325
David Benjamin808f8322017-08-18 14:06:02 -0400326 // At this point, if the padding is valid, the first |data_plus_mac_len| bytes
327 // after |out| are the plaintext and MAC. Otherwise, |data_plus_mac_len| is
328 // still large enough to extract a MAC, but it will be irrelevant.
David Benjaminea72bd02014-12-21 21:27:41 -0500329
David Benjamin808f8322017-08-18 14:06:02 -0400330 // To allow for CBC mode which changes cipher length, |ad| doesn't include the
331 // length for legacy ciphers.
David Benjaminea72bd02014-12-21 21:27:41 -0500332 uint8_t ad_fixed[13];
David Benjamin17cf2cb2016-12-13 01:07:13 -0500333 OPENSSL_memcpy(ad_fixed, ad, 11);
David Benjaminea72bd02014-12-21 21:27:41 -0500334 ad_fixed[11] = (uint8_t)(data_len >> 8);
335 ad_fixed[12] = (uint8_t)(data_len & 0xff);
336 ad_len += 2;
337
David Benjamin808f8322017-08-18 14:06:02 -0400338 // Compute the MAC and extract the one in the record.
David Benjaminea72bd02014-12-21 21:27:41 -0500339 uint8_t mac[EVP_MAX_MD_SIZE];
340 size_t mac_len;
341 uint8_t record_mac_tmp[EVP_MAX_MD_SIZE];
342 uint8_t *record_mac;
343 if (EVP_CIPHER_CTX_mode(&tls_ctx->cipher_ctx) == EVP_CIPH_CBC_MODE &&
David Benjaminbbd84442014-12-22 07:23:54 -0500344 EVP_tls_cbc_record_digest_supported(tls_ctx->hmac_ctx.md)) {
345 if (!EVP_tls_cbc_digest_record(tls_ctx->hmac_ctx.md, mac, &mac_len,
David Benjamin669ffe62021-04-07 16:17:50 -0400346 ad_fixed, out, data_len, total,
David Benjaminbbd84442014-12-22 07:23:54 -0500347 tls_ctx->mac_key, tls_ctx->mac_key_len)) {
David Benjamin3570d732015-06-29 00:28:17 -0400348 OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT);
David Benjaminea72bd02014-12-21 21:27:41 -0500349 return 0;
350 }
351 assert(mac_len == HMAC_size(&tls_ctx->hmac_ctx));
352
353 record_mac = record_mac_tmp;
David Benjaminbbd84442014-12-22 07:23:54 -0500354 EVP_tls_cbc_copy_mac(record_mac, mac_len, out, data_plus_mac_len, total);
David Benjaminea72bd02014-12-21 21:27:41 -0500355 } else {
David Benjamin808f8322017-08-18 14:06:02 -0400356 // We should support the constant-time path for all CBC-mode ciphers
357 // implemented.
David Benjaminea72bd02014-12-21 21:27:41 -0500358 assert(EVP_CIPHER_CTX_mode(&tls_ctx->cipher_ctx) != EVP_CIPH_CBC_MODE);
359
David Benjaminea72bd02014-12-21 21:27:41 -0500360 unsigned mac_len_u;
David Benjamin1741a9d2015-12-07 19:52:56 -0500361 if (!HMAC_Init_ex(&tls_ctx->hmac_ctx, NULL, 0, NULL, NULL) ||
362 !HMAC_Update(&tls_ctx->hmac_ctx, ad_fixed, ad_len) ||
363 !HMAC_Update(&tls_ctx->hmac_ctx, out, data_len) ||
364 !HMAC_Final(&tls_ctx->hmac_ctx, mac, &mac_len_u)) {
David Benjaminea72bd02014-12-21 21:27:41 -0500365 return 0;
366 }
367 mac_len = mac_len_u;
David Benjaminea72bd02014-12-21 21:27:41 -0500368
369 assert(mac_len == HMAC_size(&tls_ctx->hmac_ctx));
370 record_mac = &out[data_len];
371 }
372
David Benjamin808f8322017-08-18 14:06:02 -0400373 // Perform the MAC check and the padding check in constant-time. It should be
374 // safe to simply perform the padding check first, but it would not be under a
375 // different choice of MAC location on padding failure. See
376 // EVP_tls_cbc_remove_padding.
Adam Langley518ba072017-04-20 13:51:11 -0700377 crypto_word_t good =
David Benjamin643b77e2017-03-16 13:46:54 -0400378 constant_time_eq_int(CRYPTO_memcmp(record_mac, mac, mac_len), 0);
David Benjamin3f26a492016-08-09 23:36:43 -0400379 good &= padding_ok;
Adam Langleya6a049a2018-12-06 17:15:58 -0800380 CONSTTIME_DECLASSIFY(&good, sizeof(good));
David Benjaminea72bd02014-12-21 21:27:41 -0500381 if (!good) {
David Benjamin3570d732015-06-29 00:28:17 -0400382 OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT);
David Benjaminea72bd02014-12-21 21:27:41 -0500383 return 0;
384 }
385
Adam Langleya6a049a2018-12-06 17:15:58 -0800386 CONSTTIME_DECLASSIFY(&data_len, sizeof(data_len));
387 CONSTTIME_DECLASSIFY(out, data_len);
388
David Benjamin808f8322017-08-18 14:06:02 -0400389 // End of timing-sensitive code.
David Benjaminea72bd02014-12-21 21:27:41 -0500390
391 *out_len = data_len;
392 return 1;
393}
394
David Benjaminea72bd02014-12-21 21:27:41 -0500395static int aead_aes_128_cbc_sha1_tls_init(EVP_AEAD_CTX *ctx, const uint8_t *key,
David Benjaminb34f5102015-02-28 03:59:33 -0500396 size_t key_len, size_t tag_len,
397 enum evp_aead_direction_t dir) {
398 return aead_tls_init(ctx, key, key_len, tag_len, dir, EVP_aes_128_cbc(),
David Benjaminea72bd02014-12-21 21:27:41 -0500399 EVP_sha1(), 0);
400}
401
David Benjaminb34f5102015-02-28 03:59:33 -0500402static int aead_aes_128_cbc_sha1_tls_implicit_iv_init(
403 EVP_AEAD_CTX *ctx, const uint8_t *key, size_t key_len, size_t tag_len,
404 enum evp_aead_direction_t dir) {
405 return aead_tls_init(ctx, key, key_len, tag_len, dir, EVP_aes_128_cbc(),
David Benjaminea72bd02014-12-21 21:27:41 -0500406 EVP_sha1(), 1);
407}
408
David Benjaminea72bd02014-12-21 21:27:41 -0500409static int aead_aes_256_cbc_sha1_tls_init(EVP_AEAD_CTX *ctx, const uint8_t *key,
David Benjaminb34f5102015-02-28 03:59:33 -0500410 size_t key_len, size_t tag_len,
411 enum evp_aead_direction_t dir) {
412 return aead_tls_init(ctx, key, key_len, tag_len, dir, EVP_aes_256_cbc(),
David Benjaminea72bd02014-12-21 21:27:41 -0500413 EVP_sha1(), 0);
414}
415
David Benjaminb34f5102015-02-28 03:59:33 -0500416static int aead_aes_256_cbc_sha1_tls_implicit_iv_init(
417 EVP_AEAD_CTX *ctx, const uint8_t *key, size_t key_len, size_t tag_len,
418 enum evp_aead_direction_t dir) {
419 return aead_tls_init(ctx, key, key_len, tag_len, dir, EVP_aes_256_cbc(),
David Benjaminea72bd02014-12-21 21:27:41 -0500420 EVP_sha1(), 1);
421}
422
David Benjaminea72bd02014-12-21 21:27:41 -0500423static int aead_des_ede3_cbc_sha1_tls_init(EVP_AEAD_CTX *ctx,
424 const uint8_t *key, size_t key_len,
David Benjaminb34f5102015-02-28 03:59:33 -0500425 size_t tag_len,
426 enum evp_aead_direction_t dir) {
427 return aead_tls_init(ctx, key, key_len, tag_len, dir, EVP_des_ede3_cbc(),
David Benjaminea72bd02014-12-21 21:27:41 -0500428 EVP_sha1(), 0);
429}
430
David Benjaminb34f5102015-02-28 03:59:33 -0500431static int aead_des_ede3_cbc_sha1_tls_implicit_iv_init(
432 EVP_AEAD_CTX *ctx, const uint8_t *key, size_t key_len, size_t tag_len,
433 enum evp_aead_direction_t dir) {
434 return aead_tls_init(ctx, key, key_len, tag_len, dir, EVP_des_ede3_cbc(),
David Benjaminea72bd02014-12-21 21:27:41 -0500435 EVP_sha1(), 1);
436}
437
Adam Langleyc2d32802015-11-03 18:36:10 -0800438static int aead_tls_get_iv(const EVP_AEAD_CTX *ctx, const uint8_t **out_iv,
439 size_t *out_iv_len) {
Adam Langley35fb5912018-10-16 12:11:51 -0700440 const AEAD_TLS_CTX *tls_ctx = (AEAD_TLS_CTX *)&ctx->state;
Adam Langleyc2d32802015-11-03 18:36:10 -0800441 const size_t iv_len = EVP_CIPHER_CTX_iv_length(&tls_ctx->cipher_ctx);
442 if (iv_len <= 1) {
443 return 0;
444 }
445
446 *out_iv = tls_ctx->cipher_ctx.iv;
447 *out_iv_len = iv_len;
448 return 1;
449}
450
Matt Braithwaiteaf096752015-09-02 19:48:16 -0700451static int aead_null_sha1_tls_init(EVP_AEAD_CTX *ctx, const uint8_t *key,
452 size_t key_len, size_t tag_len,
453 enum evp_aead_direction_t dir) {
454 return aead_tls_init(ctx, key, key_len, tag_len, dir, EVP_enc_null(),
455 EVP_sha1(), 1 /* implicit iv */);
456}
457
David Benjaminea72bd02014-12-21 21:27:41 -0500458static const EVP_AEAD aead_aes_128_cbc_sha1_tls = {
David Benjamin808f8322017-08-18 14:06:02 -0400459 SHA_DIGEST_LENGTH + 16, // key len (SHA1 + AES128)
460 16, // nonce len (IV)
461 16 + SHA_DIGEST_LENGTH, // overhead (padding + SHA1)
462 SHA_DIGEST_LENGTH, // max tag length
463 0, // seal_scatter_supports_extra_in
Martin Kreichgauer74bce292017-06-23 14:49:22 -0700464
David Benjamin808f8322017-08-18 14:06:02 -0400465 NULL, // init
David Benjaminea72bd02014-12-21 21:27:41 -0500466 aead_aes_128_cbc_sha1_tls_init,
467 aead_tls_cleanup,
David Benjaminea72bd02014-12-21 21:27:41 -0500468 aead_tls_open,
Martin Kreichgauer18d9f282017-06-06 12:29:48 -0700469 aead_tls_seal_scatter,
David Benjamin808f8322017-08-18 14:06:02 -0400470 NULL, // open_gather
471 NULL, // get_iv
Martin Kreichgauerabbf3652017-07-21 16:27:54 -0700472 aead_tls_tag_len,
David Benjaminea72bd02014-12-21 21:27:41 -0500473};
474
475static const EVP_AEAD aead_aes_128_cbc_sha1_tls_implicit_iv = {
David Benjamin808f8322017-08-18 14:06:02 -0400476 SHA_DIGEST_LENGTH + 16 + 16, // key len (SHA1 + AES128 + IV)
477 0, // nonce len
478 16 + SHA_DIGEST_LENGTH, // overhead (padding + SHA1)
479 SHA_DIGEST_LENGTH, // max tag length
480 0, // seal_scatter_supports_extra_in
Martin Kreichgauer74bce292017-06-23 14:49:22 -0700481
David Benjamin808f8322017-08-18 14:06:02 -0400482 NULL, // init
David Benjaminea72bd02014-12-21 21:27:41 -0500483 aead_aes_128_cbc_sha1_tls_implicit_iv_init,
484 aead_tls_cleanup,
David Benjaminea72bd02014-12-21 21:27:41 -0500485 aead_tls_open,
Martin Kreichgauer18d9f282017-06-06 12:29:48 -0700486 aead_tls_seal_scatter,
David Benjamin808f8322017-08-18 14:06:02 -0400487 NULL, // open_gather
488 aead_tls_get_iv, // get_iv
Martin Kreichgauerabbf3652017-07-21 16:27:54 -0700489 aead_tls_tag_len,
David Benjaminea72bd02014-12-21 21:27:41 -0500490};
491
David Benjaminea72bd02014-12-21 21:27:41 -0500492static const EVP_AEAD aead_aes_256_cbc_sha1_tls = {
David Benjamin808f8322017-08-18 14:06:02 -0400493 SHA_DIGEST_LENGTH + 32, // key len (SHA1 + AES256)
494 16, // nonce len (IV)
495 16 + SHA_DIGEST_LENGTH, // overhead (padding + SHA1)
496 SHA_DIGEST_LENGTH, // max tag length
497 0, // seal_scatter_supports_extra_in
Martin Kreichgauer74bce292017-06-23 14:49:22 -0700498
David Benjamin808f8322017-08-18 14:06:02 -0400499 NULL, // init
David Benjaminea72bd02014-12-21 21:27:41 -0500500 aead_aes_256_cbc_sha1_tls_init,
501 aead_tls_cleanup,
David Benjaminea72bd02014-12-21 21:27:41 -0500502 aead_tls_open,
Martin Kreichgauer18d9f282017-06-06 12:29:48 -0700503 aead_tls_seal_scatter,
David Benjamin808f8322017-08-18 14:06:02 -0400504 NULL, // open_gather
505 NULL, // get_iv
Martin Kreichgauerabbf3652017-07-21 16:27:54 -0700506 aead_tls_tag_len,
David Benjaminea72bd02014-12-21 21:27:41 -0500507};
508
509static const EVP_AEAD aead_aes_256_cbc_sha1_tls_implicit_iv = {
David Benjamin808f8322017-08-18 14:06:02 -0400510 SHA_DIGEST_LENGTH + 32 + 16, // key len (SHA1 + AES256 + IV)
511 0, // nonce len
512 16 + SHA_DIGEST_LENGTH, // overhead (padding + SHA1)
513 SHA_DIGEST_LENGTH, // max tag length
514 0, // seal_scatter_supports_extra_in
Martin Kreichgauer74bce292017-06-23 14:49:22 -0700515
David Benjamin808f8322017-08-18 14:06:02 -0400516 NULL, // init
David Benjaminea72bd02014-12-21 21:27:41 -0500517 aead_aes_256_cbc_sha1_tls_implicit_iv_init,
518 aead_tls_cleanup,
David Benjaminea72bd02014-12-21 21:27:41 -0500519 aead_tls_open,
Martin Kreichgauer18d9f282017-06-06 12:29:48 -0700520 aead_tls_seal_scatter,
David Benjamin808f8322017-08-18 14:06:02 -0400521 NULL, // open_gather
522 aead_tls_get_iv, // get_iv
Martin Kreichgauerabbf3652017-07-21 16:27:54 -0700523 aead_tls_tag_len,
David Benjaminea72bd02014-12-21 21:27:41 -0500524};
525
David Benjaminea72bd02014-12-21 21:27:41 -0500526static const EVP_AEAD aead_des_ede3_cbc_sha1_tls = {
David Benjamin808f8322017-08-18 14:06:02 -0400527 SHA_DIGEST_LENGTH + 24, // key len (SHA1 + 3DES)
528 8, // nonce len (IV)
529 8 + SHA_DIGEST_LENGTH, // overhead (padding + SHA1)
530 SHA_DIGEST_LENGTH, // max tag length
531 0, // seal_scatter_supports_extra_in
Martin Kreichgauer74bce292017-06-23 14:49:22 -0700532
David Benjamin808f8322017-08-18 14:06:02 -0400533 NULL, // init
David Benjaminea72bd02014-12-21 21:27:41 -0500534 aead_des_ede3_cbc_sha1_tls_init,
535 aead_tls_cleanup,
David Benjaminea72bd02014-12-21 21:27:41 -0500536 aead_tls_open,
Martin Kreichgauer18d9f282017-06-06 12:29:48 -0700537 aead_tls_seal_scatter,
David Benjamin808f8322017-08-18 14:06:02 -0400538 NULL, // open_gather
539 NULL, // get_iv
Martin Kreichgauerabbf3652017-07-21 16:27:54 -0700540 aead_tls_tag_len,
David Benjaminea72bd02014-12-21 21:27:41 -0500541};
542
543static const EVP_AEAD aead_des_ede3_cbc_sha1_tls_implicit_iv = {
David Benjamin808f8322017-08-18 14:06:02 -0400544 SHA_DIGEST_LENGTH + 24 + 8, // key len (SHA1 + 3DES + IV)
545 0, // nonce len
546 8 + SHA_DIGEST_LENGTH, // overhead (padding + SHA1)
547 SHA_DIGEST_LENGTH, // max tag length
548 0, // seal_scatter_supports_extra_in
Martin Kreichgauer74bce292017-06-23 14:49:22 -0700549
David Benjamin808f8322017-08-18 14:06:02 -0400550 NULL, // init
David Benjaminea72bd02014-12-21 21:27:41 -0500551 aead_des_ede3_cbc_sha1_tls_implicit_iv_init,
552 aead_tls_cleanup,
David Benjaminea72bd02014-12-21 21:27:41 -0500553 aead_tls_open,
Martin Kreichgauer18d9f282017-06-06 12:29:48 -0700554 aead_tls_seal_scatter,
David Benjamin808f8322017-08-18 14:06:02 -0400555 NULL, // open_gather
556 aead_tls_get_iv, // get_iv
Martin Kreichgauerabbf3652017-07-21 16:27:54 -0700557 aead_tls_tag_len,
David Benjaminea72bd02014-12-21 21:27:41 -0500558};
559
Matt Braithwaiteaf096752015-09-02 19:48:16 -0700560static const EVP_AEAD aead_null_sha1_tls = {
David Benjamin808f8322017-08-18 14:06:02 -0400561 SHA_DIGEST_LENGTH, // key len
562 0, // nonce len
563 SHA_DIGEST_LENGTH, // overhead (SHA1)
564 SHA_DIGEST_LENGTH, // max tag length
565 0, // seal_scatter_supports_extra_in
Martin Kreichgauer74bce292017-06-23 14:49:22 -0700566
David Benjamin808f8322017-08-18 14:06:02 -0400567 NULL, // init
Matt Braithwaiteaf096752015-09-02 19:48:16 -0700568 aead_null_sha1_tls_init,
569 aead_tls_cleanup,
Matt Braithwaiteaf096752015-09-02 19:48:16 -0700570 aead_tls_open,
Martin Kreichgauer18d9f282017-06-06 12:29:48 -0700571 aead_tls_seal_scatter,
David Benjamin808f8322017-08-18 14:06:02 -0400572 NULL, // open_gather
573 NULL, // get_iv
Martin Kreichgauerabbf3652017-07-21 16:27:54 -0700574 aead_tls_tag_len,
Matt Braithwaiteaf096752015-09-02 19:48:16 -0700575};
576
David Benjaminea72bd02014-12-21 21:27:41 -0500577const EVP_AEAD *EVP_aead_aes_128_cbc_sha1_tls(void) {
578 return &aead_aes_128_cbc_sha1_tls;
579}
580
581const EVP_AEAD *EVP_aead_aes_128_cbc_sha1_tls_implicit_iv(void) {
582 return &aead_aes_128_cbc_sha1_tls_implicit_iv;
583}
584
David Benjaminea72bd02014-12-21 21:27:41 -0500585const EVP_AEAD *EVP_aead_aes_256_cbc_sha1_tls(void) {
586 return &aead_aes_256_cbc_sha1_tls;
587}
588
589const EVP_AEAD *EVP_aead_aes_256_cbc_sha1_tls_implicit_iv(void) {
590 return &aead_aes_256_cbc_sha1_tls_implicit_iv;
591}
592
David Benjaminea72bd02014-12-21 21:27:41 -0500593const EVP_AEAD *EVP_aead_des_ede3_cbc_sha1_tls(void) {
594 return &aead_des_ede3_cbc_sha1_tls;
595}
596
597const EVP_AEAD *EVP_aead_des_ede3_cbc_sha1_tls_implicit_iv(void) {
598 return &aead_des_ede3_cbc_sha1_tls_implicit_iv;
599}
Matt Braithwaiteaf096752015-09-02 19:48:16 -0700600
601const EVP_AEAD *EVP_aead_null_sha1_tls(void) { return &aead_null_sha1_tls; }