David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 1 | /* 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 Benjamin | 9f897b2 | 2015-12-07 19:34:31 -0500 | [diff] [blame] | 23 | #include <openssl/md5.h> |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 24 | #include <openssl/mem.h> |
| 25 | #include <openssl/sha.h> |
David Benjamin | b34f510 | 2015-02-28 03:59:33 -0500 | [diff] [blame] | 26 | #include <openssl/type_check.h> |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 27 | |
Martin Kreichgauer | 18d9f28 | 2017-06-06 12:29:48 -0700 | [diff] [blame] | 28 | #include "../fipsmodule/cipher/internal.h" |
Steven Valdez | cb96654 | 2016-08-17 16:56:14 -0400 | [diff] [blame] | 29 | #include "../internal.h" |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 30 | #include "internal.h" |
| 31 | |
| 32 | |
| 33 | typedef struct { |
| 34 | EVP_CIPHER_CTX cipher_ctx; |
| 35 | HMAC_CTX hmac_ctx; |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 36 | // mac_key is the portion of the key used for the MAC. It is retained |
| 37 | // separately for the constant-time CBC code. |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 38 | uint8_t mac_key[EVP_MAX_MD_SIZE]; |
| 39 | uint8_t mac_key_len; |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 40 | // implicit_iv is one iff this is a pre-TLS-1.1 CBC cipher without an explicit |
| 41 | // IV. |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 42 | char implicit_iv; |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 43 | } AEAD_TLS_CTX; |
| 44 | |
David Benjamin | 5ecfb10 | 2018-10-24 17:08:00 -0500 | [diff] [blame] | 45 | OPENSSL_STATIC_ASSERT(EVP_MAX_MD_SIZE < 256, |
| 46 | "mac_key_len does not fit in uint8_t"); |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 47 | |
David Benjamin | 5ecfb10 | 2018-10-24 17:08:00 -0500 | [diff] [blame] | 48 | OPENSSL_STATIC_ASSERT(sizeof(((EVP_AEAD_CTX *)NULL)->state) >= |
| 49 | sizeof(AEAD_TLS_CTX), |
| 50 | "AEAD state is too small"); |
Adam Langley | 35fb591 | 2018-10-16 12:11:51 -0700 | [diff] [blame] | 51 | #if defined(__GNUC__) || defined(__clang__) |
David Benjamin | 5ecfb10 | 2018-10-24 17:08:00 -0500 | [diff] [blame] | 52 | OPENSSL_STATIC_ASSERT(alignof(union evp_aead_ctx_st_state) >= |
| 53 | alignof(AEAD_TLS_CTX), |
| 54 | "AEAD state has insufficient alignment"); |
Adam Langley | 35fb591 | 2018-10-16 12:11:51 -0700 | [diff] [blame] | 55 | #endif |
| 56 | |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 57 | static void aead_tls_cleanup(EVP_AEAD_CTX *ctx) { |
Adam Langley | 35fb591 | 2018-10-16 12:11:51 -0700 | [diff] [blame] | 58 | AEAD_TLS_CTX *tls_ctx = (AEAD_TLS_CTX *)&ctx->state; |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 59 | EVP_CIPHER_CTX_cleanup(&tls_ctx->cipher_ctx); |
| 60 | HMAC_CTX_cleanup(&tls_ctx->hmac_ctx); |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | static int aead_tls_init(EVP_AEAD_CTX *ctx, const uint8_t *key, size_t key_len, |
David Benjamin | b34f510 | 2015-02-28 03:59:33 -0500 | [diff] [blame] | 64 | size_t tag_len, enum evp_aead_direction_t dir, |
David Benjamin | d03b5ed | 2015-03-07 00:10:27 -0800 | [diff] [blame] | 65 | const EVP_CIPHER *cipher, const EVP_MD *md, |
| 66 | char implicit_iv) { |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 67 | if (tag_len != EVP_AEAD_DEFAULT_TAG_LENGTH && |
| 68 | tag_len != EVP_MD_size(md)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 69 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_UNSUPPORTED_TAG_SIZE); |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 70 | return 0; |
| 71 | } |
| 72 | |
| 73 | if (key_len != EVP_AEAD_key_length(ctx->aead)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 74 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_KEY_LENGTH); |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 75 | 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 Benjamin | b34f510 | 2015-02-28 03:59:33 -0500 | [diff] [blame] | 80 | assert(mac_key_len + enc_key_len + |
| 81 | (implicit_iv ? EVP_CIPHER_iv_length(cipher) : 0) == key_len); |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 82 | |
Adam Langley | 35fb591 | 2018-10-16 12:11:51 -0700 | [diff] [blame] | 83 | AEAD_TLS_CTX *tls_ctx = (AEAD_TLS_CTX *)&ctx->state; |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 84 | EVP_CIPHER_CTX_init(&tls_ctx->cipher_ctx); |
| 85 | HMAC_CTX_init(&tls_ctx->hmac_ctx); |
David Benjamin | b34f510 | 2015-02-28 03:59:33 -0500 | [diff] [blame] | 86 | assert(mac_key_len <= EVP_MAX_MD_SIZE); |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 87 | OPENSSL_memcpy(tls_ctx->mac_key, key, mac_key_len); |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 88 | tls_ctx->mac_key_len = (uint8_t)mac_key_len; |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 89 | tls_ctx->implicit_iv = implicit_iv; |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 90 | |
David Benjamin | b34f510 | 2015-02-28 03:59:33 -0500 | [diff] [blame] | 91 | 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 Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 94 | !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 Kreichgauer | abbf365 | 2017-07-21 16:27:54 -0700 | [diff] [blame] | 103 | static 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 Langley | 35fb591 | 2018-10-16 12:11:51 -0700 | [diff] [blame] | 106 | const AEAD_TLS_CTX *tls_ctx = (AEAD_TLS_CTX *)&ctx->state; |
Martin Kreichgauer | abbf365 | 2017-07-21 16:27:54 -0700 | [diff] [blame] | 107 | |
| 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 Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 115 | // 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 Kreichgauer | abbf365 | 2017-07-21 16:27:54 -0700 | [diff] [blame] | 117 | 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 Kreichgauer | 18d9f28 | 2017-06-06 12:29:48 -0700 | [diff] [blame] | 122 | static int aead_tls_seal_scatter(const EVP_AEAD_CTX *ctx, uint8_t *out, |
| 123 | uint8_t *out_tag, size_t *out_tag_len, |
Martin Kreichgauer | abbf365 | 2017-07-21 16:27:54 -0700 | [diff] [blame] | 124 | 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 Langley | 35fb591 | 2018-10-16 12:11:51 -0700 | [diff] [blame] | 130 | AEAD_TLS_CTX *tls_ctx = (AEAD_TLS_CTX *)&ctx->state; |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 131 | |
David Benjamin | b34f510 | 2015-02-28 03:59:33 -0500 | [diff] [blame] | 132 | if (!tls_ctx->cipher_ctx.encrypt) { |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 133 | // Unlike a normal AEAD, a TLS AEAD may only be used in one direction. |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 134 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_INVALID_OPERATION); |
David Benjamin | b34f510 | 2015-02-28 03:59:33 -0500 | [diff] [blame] | 135 | return 0; |
David Benjamin | b34f510 | 2015-02-28 03:59:33 -0500 | [diff] [blame] | 136 | } |
| 137 | |
Martin Kreichgauer | 18d9f28 | 2017-06-06 12:29:48 -0700 | [diff] [blame] | 138 | if (in_len > INT_MAX) { |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 139 | // EVP_CIPHER takes int as input. |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 140 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TOO_LARGE); |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 141 | return 0; |
| 142 | } |
| 143 | |
Martin Kreichgauer | abbf365 | 2017-07-21 16:27:54 -0700 | [diff] [blame] | 144 | if (max_out_tag_len < aead_tls_tag_len(ctx, in_len, extra_in_len)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 145 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BUFFER_TOO_SMALL); |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 146 | return 0; |
| 147 | } |
| 148 | |
| 149 | if (nonce_len != EVP_AEAD_nonce_length(ctx->aead)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 150 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_INVALID_NONCE_SIZE); |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 151 | return 0; |
| 152 | } |
| 153 | |
David Benjamin | 7f1d5d5 | 2015-01-14 17:24:53 -0500 | [diff] [blame] | 154 | if (ad_len != 13 - 2 /* length bytes */) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 155 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_INVALID_AD_SIZE); |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 156 | return 0; |
| 157 | } |
| 158 | |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 159 | // To allow for CBC mode which changes cipher length, |ad| doesn't include the |
| 160 | // length for legacy ciphers. |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 161 | 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 Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 165 | // Compute the MAC. This must be first in case the operation is being done |
| 166 | // in-place. |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 167 | uint8_t mac[EVP_MAX_MD_SIZE]; |
| 168 | unsigned mac_len; |
David Benjamin | 1741a9d | 2015-12-07 19:52:56 -0500 | [diff] [blame] | 169 | 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 Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 174 | return 0; |
| 175 | } |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 176 | |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 177 | // Configure the explicit IV. |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 178 | 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 Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 184 | // Encrypt the input. |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 185 | int len; |
Martin Kreichgauer | 18d9f28 | 2017-06-06 12:29:48 -0700 | [diff] [blame] | 186 | if (!EVP_EncryptUpdate(&tls_ctx->cipher_ctx, out, &len, in, (int)in_len)) { |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 187 | return 0; |
| 188 | } |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 189 | |
| 190 | unsigned block_size = EVP_CIPHER_CTX_block_size(&tls_ctx->cipher_ctx); |
Martin Kreichgauer | 18d9f28 | 2017-06-06 12:29:48 -0700 | [diff] [blame] | 191 | |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 192 | // 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 Kreichgauer | 18d9f28 | 2017-06-06 12:29:48 -0700 | [diff] [blame] | 195 | |
David Benjamin | 80ede1d | 2017-12-21 16:30:28 -0500 | [diff] [blame] | 196 | const size_t early_mac_len = (block_size - (in_len % block_size)) % block_size; |
Martin Kreichgauer | 18d9f28 | 2017-06-06 12:29:48 -0700 | [diff] [blame] | 197 | 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 Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 217 | 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 Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 221 | // Compute padding and feed that into the cipher. |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 222 | uint8_t padding[256]; |
| 223 | unsigned padding_len = block_size - ((in_len + mac_len) % block_size); |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 224 | OPENSSL_memset(padding, padding_len - 1, padding_len); |
Martin Kreichgauer | 18d9f28 | 2017-06-06 12:29:48 -0700 | [diff] [blame] | 225 | if (!EVP_EncryptUpdate(&tls_ctx->cipher_ctx, out_tag + tag_len, &len, |
| 226 | padding, (int)padding_len)) { |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 227 | return 0; |
| 228 | } |
Martin Kreichgauer | 18d9f28 | 2017-06-06 12:29:48 -0700 | [diff] [blame] | 229 | tag_len += len; |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 230 | } |
| 231 | |
Martin Kreichgauer | 18d9f28 | 2017-06-06 12:29:48 -0700 | [diff] [blame] | 232 | if (!EVP_EncryptFinal_ex(&tls_ctx->cipher_ctx, out_tag + tag_len, &len)) { |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 233 | return 0; |
| 234 | } |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 235 | assert(len == 0); // Padding is explicit. |
Martin Kreichgauer | abbf365 | 2017-07-21 16:27:54 -0700 | [diff] [blame] | 236 | assert(tag_len == aead_tls_tag_len(ctx, in_len, extra_in_len)); |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 237 | |
Martin Kreichgauer | 18d9f28 | 2017-06-06 12:29:48 -0700 | [diff] [blame] | 238 | *out_tag_len = tag_len; |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 239 | return 1; |
| 240 | } |
| 241 | |
Martin Kreichgauer | 18d9f28 | 2017-06-06 12:29:48 -0700 | [diff] [blame] | 242 | static 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 Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 245 | const uint8_t *ad, size_t ad_len) { |
Adam Langley | 35fb591 | 2018-10-16 12:11:51 -0700 | [diff] [blame] | 246 | AEAD_TLS_CTX *tls_ctx = (AEAD_TLS_CTX *)&ctx->state; |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 247 | |
David Benjamin | b34f510 | 2015-02-28 03:59:33 -0500 | [diff] [blame] | 248 | if (tls_ctx->cipher_ctx.encrypt) { |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 249 | // Unlike a normal AEAD, a TLS AEAD may only be used in one direction. |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 250 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_INVALID_OPERATION); |
David Benjamin | b34f510 | 2015-02-28 03:59:33 -0500 | [diff] [blame] | 251 | return 0; |
David Benjamin | b34f510 | 2015-02-28 03:59:33 -0500 | [diff] [blame] | 252 | } |
| 253 | |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 254 | if (in_len < HMAC_size(&tls_ctx->hmac_ctx)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 255 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT); |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 256 | return 0; |
| 257 | } |
| 258 | |
| 259 | if (max_out_len < in_len) { |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 260 | // This requires that the caller provide space for the MAC, even though it |
| 261 | // will always be removed on return. |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 262 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BUFFER_TOO_SMALL); |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 263 | return 0; |
| 264 | } |
| 265 | |
| 266 | if (nonce_len != EVP_AEAD_nonce_length(ctx->aead)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 267 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_INVALID_NONCE_SIZE); |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 268 | return 0; |
| 269 | } |
| 270 | |
David Benjamin | 7f1d5d5 | 2015-01-14 17:24:53 -0500 | [diff] [blame] | 271 | if (ad_len != 13 - 2 /* length bytes */) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 272 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_INVALID_AD_SIZE); |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 273 | return 0; |
| 274 | } |
| 275 | |
| 276 | if (in_len > INT_MAX) { |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 277 | // EVP_CIPHER takes int as input. |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 278 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TOO_LARGE); |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 279 | return 0; |
| 280 | } |
| 281 | |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 282 | // Configure the explicit IV. |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 283 | 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 Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 289 | // Decrypt to get the plaintext + MAC + padding. |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 290 | 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 Langley | a6a049a | 2018-12-06 17:15:58 -0800 | [diff] [blame] | 302 | CONSTTIME_SECRET(out, total); |
| 303 | |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 304 | // 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 Langley | 518ba07 | 2017-04-20 13:51:11 -0700 | [diff] [blame] | 306 | size_t data_plus_mac_len; |
| 307 | crypto_word_t padding_ok; |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 308 | if (EVP_CIPHER_CTX_mode(&tls_ctx->cipher_ctx) == EVP_CIPH_CBC_MODE) { |
David Benjamin | 3f26a49 | 2016-08-09 23:36:43 -0400 | [diff] [blame] | 309 | 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 Benjamin | 643b77e | 2017-03-16 13:46:54 -0400 | [diff] [blame] | 312 | HMAC_size(&tls_ctx->hmac_ctx))) { |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 313 | // Publicly invalid. This can be rejected in non-constant time. |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 314 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT); |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 315 | return 0; |
| 316 | } |
| 317 | } else { |
Adam Langley | 518ba07 | 2017-04-20 13:51:11 -0700 | [diff] [blame] | 318 | padding_ok = CONSTTIME_TRUE_W; |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 319 | data_plus_mac_len = total; |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 320 | // |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 Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 322 | assert(data_plus_mac_len >= HMAC_size(&tls_ctx->hmac_ctx)); |
| 323 | } |
David Benjamin | 643b77e | 2017-03-16 13:46:54 -0400 | [diff] [blame] | 324 | size_t data_len = data_plus_mac_len - HMAC_size(&tls_ctx->hmac_ctx); |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 325 | |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 326 | // 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 Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 329 | |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 330 | // To allow for CBC mode which changes cipher length, |ad| doesn't include the |
| 331 | // length for legacy ciphers. |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 332 | uint8_t ad_fixed[13]; |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 333 | OPENSSL_memcpy(ad_fixed, ad, 11); |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 334 | ad_fixed[11] = (uint8_t)(data_len >> 8); |
| 335 | ad_fixed[12] = (uint8_t)(data_len & 0xff); |
| 336 | ad_len += 2; |
| 337 | |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 338 | // Compute the MAC and extract the one in the record. |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 339 | 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 Benjamin | bbd8444 | 2014-12-22 07:23:54 -0500 | [diff] [blame] | 344 | 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 Benjamin | 669ffe6 | 2021-04-07 16:17:50 -0400 | [diff] [blame] | 346 | ad_fixed, out, data_len, total, |
David Benjamin | bbd8444 | 2014-12-22 07:23:54 -0500 | [diff] [blame] | 347 | tls_ctx->mac_key, tls_ctx->mac_key_len)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 348 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT); |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 349 | return 0; |
| 350 | } |
| 351 | assert(mac_len == HMAC_size(&tls_ctx->hmac_ctx)); |
| 352 | |
| 353 | record_mac = record_mac_tmp; |
David Benjamin | bbd8444 | 2014-12-22 07:23:54 -0500 | [diff] [blame] | 354 | EVP_tls_cbc_copy_mac(record_mac, mac_len, out, data_plus_mac_len, total); |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 355 | } else { |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 356 | // We should support the constant-time path for all CBC-mode ciphers |
| 357 | // implemented. |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 358 | assert(EVP_CIPHER_CTX_mode(&tls_ctx->cipher_ctx) != EVP_CIPH_CBC_MODE); |
| 359 | |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 360 | unsigned mac_len_u; |
David Benjamin | 1741a9d | 2015-12-07 19:52:56 -0500 | [diff] [blame] | 361 | 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 Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 365 | return 0; |
| 366 | } |
| 367 | mac_len = mac_len_u; |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 368 | |
| 369 | assert(mac_len == HMAC_size(&tls_ctx->hmac_ctx)); |
| 370 | record_mac = &out[data_len]; |
| 371 | } |
| 372 | |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 373 | // 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 Langley | 518ba07 | 2017-04-20 13:51:11 -0700 | [diff] [blame] | 377 | crypto_word_t good = |
David Benjamin | 643b77e | 2017-03-16 13:46:54 -0400 | [diff] [blame] | 378 | constant_time_eq_int(CRYPTO_memcmp(record_mac, mac, mac_len), 0); |
David Benjamin | 3f26a49 | 2016-08-09 23:36:43 -0400 | [diff] [blame] | 379 | good &= padding_ok; |
Adam Langley | a6a049a | 2018-12-06 17:15:58 -0800 | [diff] [blame] | 380 | CONSTTIME_DECLASSIFY(&good, sizeof(good)); |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 381 | if (!good) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 382 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT); |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 383 | return 0; |
| 384 | } |
| 385 | |
Adam Langley | a6a049a | 2018-12-06 17:15:58 -0800 | [diff] [blame] | 386 | CONSTTIME_DECLASSIFY(&data_len, sizeof(data_len)); |
| 387 | CONSTTIME_DECLASSIFY(out, data_len); |
| 388 | |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 389 | // End of timing-sensitive code. |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 390 | |
| 391 | *out_len = data_len; |
| 392 | return 1; |
| 393 | } |
| 394 | |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 395 | static int aead_aes_128_cbc_sha1_tls_init(EVP_AEAD_CTX *ctx, const uint8_t *key, |
David Benjamin | b34f510 | 2015-02-28 03:59:33 -0500 | [diff] [blame] | 396 | 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 Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 399 | EVP_sha1(), 0); |
| 400 | } |
| 401 | |
David Benjamin | b34f510 | 2015-02-28 03:59:33 -0500 | [diff] [blame] | 402 | static 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 Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 406 | EVP_sha1(), 1); |
| 407 | } |
| 408 | |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 409 | static int aead_aes_256_cbc_sha1_tls_init(EVP_AEAD_CTX *ctx, const uint8_t *key, |
David Benjamin | b34f510 | 2015-02-28 03:59:33 -0500 | [diff] [blame] | 410 | 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 Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 413 | EVP_sha1(), 0); |
| 414 | } |
| 415 | |
David Benjamin | b34f510 | 2015-02-28 03:59:33 -0500 | [diff] [blame] | 416 | static 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 Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 420 | EVP_sha1(), 1); |
| 421 | } |
| 422 | |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 423 | static int aead_des_ede3_cbc_sha1_tls_init(EVP_AEAD_CTX *ctx, |
| 424 | const uint8_t *key, size_t key_len, |
David Benjamin | b34f510 | 2015-02-28 03:59:33 -0500 | [diff] [blame] | 425 | 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 Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 428 | EVP_sha1(), 0); |
| 429 | } |
| 430 | |
David Benjamin | b34f510 | 2015-02-28 03:59:33 -0500 | [diff] [blame] | 431 | static 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 Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 435 | EVP_sha1(), 1); |
| 436 | } |
| 437 | |
Adam Langley | c2d3280 | 2015-11-03 18:36:10 -0800 | [diff] [blame] | 438 | static int aead_tls_get_iv(const EVP_AEAD_CTX *ctx, const uint8_t **out_iv, |
| 439 | size_t *out_iv_len) { |
Adam Langley | 35fb591 | 2018-10-16 12:11:51 -0700 | [diff] [blame] | 440 | const AEAD_TLS_CTX *tls_ctx = (AEAD_TLS_CTX *)&ctx->state; |
Adam Langley | c2d3280 | 2015-11-03 18:36:10 -0800 | [diff] [blame] | 441 | 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 Braithwaite | af09675 | 2015-09-02 19:48:16 -0700 | [diff] [blame] | 451 | static 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 Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 458 | static const EVP_AEAD aead_aes_128_cbc_sha1_tls = { |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 459 | 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 Kreichgauer | 74bce29 | 2017-06-23 14:49:22 -0700 | [diff] [blame] | 464 | |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 465 | NULL, // init |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 466 | aead_aes_128_cbc_sha1_tls_init, |
| 467 | aead_tls_cleanup, |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 468 | aead_tls_open, |
Martin Kreichgauer | 18d9f28 | 2017-06-06 12:29:48 -0700 | [diff] [blame] | 469 | aead_tls_seal_scatter, |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 470 | NULL, // open_gather |
| 471 | NULL, // get_iv |
Martin Kreichgauer | abbf365 | 2017-07-21 16:27:54 -0700 | [diff] [blame] | 472 | aead_tls_tag_len, |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 473 | }; |
| 474 | |
| 475 | static const EVP_AEAD aead_aes_128_cbc_sha1_tls_implicit_iv = { |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 476 | 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 Kreichgauer | 74bce29 | 2017-06-23 14:49:22 -0700 | [diff] [blame] | 481 | |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 482 | NULL, // init |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 483 | aead_aes_128_cbc_sha1_tls_implicit_iv_init, |
| 484 | aead_tls_cleanup, |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 485 | aead_tls_open, |
Martin Kreichgauer | 18d9f28 | 2017-06-06 12:29:48 -0700 | [diff] [blame] | 486 | aead_tls_seal_scatter, |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 487 | NULL, // open_gather |
| 488 | aead_tls_get_iv, // get_iv |
Martin Kreichgauer | abbf365 | 2017-07-21 16:27:54 -0700 | [diff] [blame] | 489 | aead_tls_tag_len, |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 490 | }; |
| 491 | |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 492 | static const EVP_AEAD aead_aes_256_cbc_sha1_tls = { |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 493 | 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 Kreichgauer | 74bce29 | 2017-06-23 14:49:22 -0700 | [diff] [blame] | 498 | |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 499 | NULL, // init |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 500 | aead_aes_256_cbc_sha1_tls_init, |
| 501 | aead_tls_cleanup, |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 502 | aead_tls_open, |
Martin Kreichgauer | 18d9f28 | 2017-06-06 12:29:48 -0700 | [diff] [blame] | 503 | aead_tls_seal_scatter, |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 504 | NULL, // open_gather |
| 505 | NULL, // get_iv |
Martin Kreichgauer | abbf365 | 2017-07-21 16:27:54 -0700 | [diff] [blame] | 506 | aead_tls_tag_len, |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 507 | }; |
| 508 | |
| 509 | static const EVP_AEAD aead_aes_256_cbc_sha1_tls_implicit_iv = { |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 510 | 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 Kreichgauer | 74bce29 | 2017-06-23 14:49:22 -0700 | [diff] [blame] | 515 | |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 516 | NULL, // init |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 517 | aead_aes_256_cbc_sha1_tls_implicit_iv_init, |
| 518 | aead_tls_cleanup, |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 519 | aead_tls_open, |
Martin Kreichgauer | 18d9f28 | 2017-06-06 12:29:48 -0700 | [diff] [blame] | 520 | aead_tls_seal_scatter, |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 521 | NULL, // open_gather |
| 522 | aead_tls_get_iv, // get_iv |
Martin Kreichgauer | abbf365 | 2017-07-21 16:27:54 -0700 | [diff] [blame] | 523 | aead_tls_tag_len, |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 524 | }; |
| 525 | |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 526 | static const EVP_AEAD aead_des_ede3_cbc_sha1_tls = { |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 527 | 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 Kreichgauer | 74bce29 | 2017-06-23 14:49:22 -0700 | [diff] [blame] | 532 | |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 533 | NULL, // init |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 534 | aead_des_ede3_cbc_sha1_tls_init, |
| 535 | aead_tls_cleanup, |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 536 | aead_tls_open, |
Martin Kreichgauer | 18d9f28 | 2017-06-06 12:29:48 -0700 | [diff] [blame] | 537 | aead_tls_seal_scatter, |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 538 | NULL, // open_gather |
| 539 | NULL, // get_iv |
Martin Kreichgauer | abbf365 | 2017-07-21 16:27:54 -0700 | [diff] [blame] | 540 | aead_tls_tag_len, |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 541 | }; |
| 542 | |
| 543 | static const EVP_AEAD aead_des_ede3_cbc_sha1_tls_implicit_iv = { |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 544 | 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 Kreichgauer | 74bce29 | 2017-06-23 14:49:22 -0700 | [diff] [blame] | 549 | |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 550 | NULL, // init |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 551 | aead_des_ede3_cbc_sha1_tls_implicit_iv_init, |
| 552 | aead_tls_cleanup, |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 553 | aead_tls_open, |
Martin Kreichgauer | 18d9f28 | 2017-06-06 12:29:48 -0700 | [diff] [blame] | 554 | aead_tls_seal_scatter, |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 555 | NULL, // open_gather |
| 556 | aead_tls_get_iv, // get_iv |
Martin Kreichgauer | abbf365 | 2017-07-21 16:27:54 -0700 | [diff] [blame] | 557 | aead_tls_tag_len, |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 558 | }; |
| 559 | |
Matt Braithwaite | af09675 | 2015-09-02 19:48:16 -0700 | [diff] [blame] | 560 | static const EVP_AEAD aead_null_sha1_tls = { |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 561 | 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 Kreichgauer | 74bce29 | 2017-06-23 14:49:22 -0700 | [diff] [blame] | 566 | |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 567 | NULL, // init |
Matt Braithwaite | af09675 | 2015-09-02 19:48:16 -0700 | [diff] [blame] | 568 | aead_null_sha1_tls_init, |
| 569 | aead_tls_cleanup, |
Matt Braithwaite | af09675 | 2015-09-02 19:48:16 -0700 | [diff] [blame] | 570 | aead_tls_open, |
Martin Kreichgauer | 18d9f28 | 2017-06-06 12:29:48 -0700 | [diff] [blame] | 571 | aead_tls_seal_scatter, |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 572 | NULL, // open_gather |
| 573 | NULL, // get_iv |
Martin Kreichgauer | abbf365 | 2017-07-21 16:27:54 -0700 | [diff] [blame] | 574 | aead_tls_tag_len, |
Matt Braithwaite | af09675 | 2015-09-02 19:48:16 -0700 | [diff] [blame] | 575 | }; |
| 576 | |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 577 | const EVP_AEAD *EVP_aead_aes_128_cbc_sha1_tls(void) { |
| 578 | return &aead_aes_128_cbc_sha1_tls; |
| 579 | } |
| 580 | |
| 581 | const 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 Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 585 | const EVP_AEAD *EVP_aead_aes_256_cbc_sha1_tls(void) { |
| 586 | return &aead_aes_256_cbc_sha1_tls; |
| 587 | } |
| 588 | |
| 589 | const 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 Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 593 | const EVP_AEAD *EVP_aead_des_ede3_cbc_sha1_tls(void) { |
| 594 | return &aead_des_ede3_cbc_sha1_tls; |
| 595 | } |
| 596 | |
| 597 | const EVP_AEAD *EVP_aead_des_ede3_cbc_sha1_tls_implicit_iv(void) { |
| 598 | return &aead_des_ede3_cbc_sha1_tls_implicit_iv; |
| 599 | } |
Matt Braithwaite | af09675 | 2015-09-02 19:48:16 -0700 | [diff] [blame] | 600 | |
| 601 | const EVP_AEAD *EVP_aead_null_sha1_tls(void) { return &aead_null_sha1_tls; } |