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 | b34f510 | 2015-02-28 03:59:33 -0500 | [diff] [blame] | 45 | OPENSSL_COMPILE_ASSERT(EVP_MAX_MD_SIZE < 256, mac_key_len_fits_in_uint8_t); |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 46 | |
| 47 | static void aead_tls_cleanup(EVP_AEAD_CTX *ctx) { |
| 48 | AEAD_TLS_CTX *tls_ctx = (AEAD_TLS_CTX *)ctx->aead_state; |
| 49 | EVP_CIPHER_CTX_cleanup(&tls_ctx->cipher_ctx); |
| 50 | HMAC_CTX_cleanup(&tls_ctx->hmac_ctx); |
| 51 | OPENSSL_cleanse(&tls_ctx->mac_key, sizeof(tls_ctx->mac_key)); |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 52 | OPENSSL_free(tls_ctx); |
| 53 | ctx->aead_state = NULL; |
| 54 | } |
| 55 | |
| 56 | 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] | 57 | size_t tag_len, enum evp_aead_direction_t dir, |
David Benjamin | d03b5ed | 2015-03-07 00:10:27 -0800 | [diff] [blame] | 58 | const EVP_CIPHER *cipher, const EVP_MD *md, |
| 59 | char implicit_iv) { |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 60 | if (tag_len != EVP_AEAD_DEFAULT_TAG_LENGTH && |
| 61 | tag_len != EVP_MD_size(md)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 62 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_UNSUPPORTED_TAG_SIZE); |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 63 | return 0; |
| 64 | } |
| 65 | |
| 66 | if (key_len != EVP_AEAD_key_length(ctx->aead)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 67 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_KEY_LENGTH); |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 68 | return 0; |
| 69 | } |
| 70 | |
| 71 | size_t mac_key_len = EVP_MD_size(md); |
| 72 | size_t enc_key_len = EVP_CIPHER_key_length(cipher); |
David Benjamin | b34f510 | 2015-02-28 03:59:33 -0500 | [diff] [blame] | 73 | assert(mac_key_len + enc_key_len + |
| 74 | (implicit_iv ? EVP_CIPHER_iv_length(cipher) : 0) == key_len); |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 75 | |
| 76 | AEAD_TLS_CTX *tls_ctx = OPENSSL_malloc(sizeof(AEAD_TLS_CTX)); |
| 77 | if (tls_ctx == NULL) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 78 | OPENSSL_PUT_ERROR(CIPHER, ERR_R_MALLOC_FAILURE); |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 79 | return 0; |
| 80 | } |
| 81 | EVP_CIPHER_CTX_init(&tls_ctx->cipher_ctx); |
| 82 | HMAC_CTX_init(&tls_ctx->hmac_ctx); |
David Benjamin | b34f510 | 2015-02-28 03:59:33 -0500 | [diff] [blame] | 83 | assert(mac_key_len <= EVP_MAX_MD_SIZE); |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 84 | OPENSSL_memcpy(tls_ctx->mac_key, key, mac_key_len); |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 85 | tls_ctx->mac_key_len = (uint8_t)mac_key_len; |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 86 | tls_ctx->implicit_iv = implicit_iv; |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 87 | |
| 88 | ctx->aead_state = tls_ctx; |
David Benjamin | b34f510 | 2015-02-28 03:59:33 -0500 | [diff] [blame] | 89 | if (!EVP_CipherInit_ex(&tls_ctx->cipher_ctx, cipher, NULL, &key[mac_key_len], |
| 90 | implicit_iv ? &key[mac_key_len + enc_key_len] : NULL, |
| 91 | dir == evp_aead_seal) || |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 92 | !HMAC_Init_ex(&tls_ctx->hmac_ctx, key, mac_key_len, md, NULL)) { |
| 93 | aead_tls_cleanup(ctx); |
Adam Langley | 5aa8a86 | 2015-05-11 14:28:12 -0700 | [diff] [blame] | 94 | ctx->aead_state = NULL; |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 95 | return 0; |
| 96 | } |
| 97 | EVP_CIPHER_CTX_set_padding(&tls_ctx->cipher_ctx, 0); |
| 98 | |
| 99 | return 1; |
| 100 | } |
| 101 | |
Martin Kreichgauer | abbf365 | 2017-07-21 16:27:54 -0700 | [diff] [blame] | 102 | static size_t aead_tls_tag_len(const EVP_AEAD_CTX *ctx, const size_t in_len, |
| 103 | const size_t extra_in_len) { |
| 104 | assert(extra_in_len == 0); |
| 105 | AEAD_TLS_CTX *tls_ctx = (AEAD_TLS_CTX *)ctx->aead_state; |
| 106 | |
| 107 | const size_t hmac_len = HMAC_size(&tls_ctx->hmac_ctx); |
| 108 | if (EVP_CIPHER_CTX_mode(&tls_ctx->cipher_ctx) != EVP_CIPH_CBC_MODE) { |
| 109 | // The NULL cipher. |
| 110 | return hmac_len; |
| 111 | } |
| 112 | |
| 113 | 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] | 114 | // An overflow of |in_len + hmac_len| doesn't affect the result mod |
| 115 | // |block_size|, provided that |block_size| is a smaller power of two. |
Martin Kreichgauer | abbf365 | 2017-07-21 16:27:54 -0700 | [diff] [blame] | 116 | assert(block_size != 0 && (block_size & (block_size - 1)) == 0); |
| 117 | const size_t pad_len = block_size - (in_len + hmac_len) % block_size; |
| 118 | return hmac_len + pad_len; |
| 119 | } |
| 120 | |
Martin Kreichgauer | 18d9f28 | 2017-06-06 12:29:48 -0700 | [diff] [blame] | 121 | static int aead_tls_seal_scatter(const EVP_AEAD_CTX *ctx, uint8_t *out, |
| 122 | uint8_t *out_tag, size_t *out_tag_len, |
Martin Kreichgauer | abbf365 | 2017-07-21 16:27:54 -0700 | [diff] [blame] | 123 | const size_t max_out_tag_len, |
| 124 | const uint8_t *nonce, const size_t nonce_len, |
| 125 | const uint8_t *in, const size_t in_len, |
| 126 | const uint8_t *extra_in, |
| 127 | const size_t extra_in_len, const uint8_t *ad, |
| 128 | const size_t ad_len) { |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 129 | AEAD_TLS_CTX *tls_ctx = (AEAD_TLS_CTX *)ctx->aead_state; |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 130 | |
David Benjamin | b34f510 | 2015-02-28 03:59:33 -0500 | [diff] [blame] | 131 | if (!tls_ctx->cipher_ctx.encrypt) { |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 132 | // 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] | 133 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_INVALID_OPERATION); |
David Benjamin | b34f510 | 2015-02-28 03:59:33 -0500 | [diff] [blame] | 134 | return 0; |
David Benjamin | b34f510 | 2015-02-28 03:59:33 -0500 | [diff] [blame] | 135 | } |
| 136 | |
Martin Kreichgauer | 18d9f28 | 2017-06-06 12:29:48 -0700 | [diff] [blame] | 137 | if (in_len > INT_MAX) { |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 138 | // EVP_CIPHER takes int as input. |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 139 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TOO_LARGE); |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 140 | return 0; |
| 141 | } |
| 142 | |
Martin Kreichgauer | abbf365 | 2017-07-21 16:27:54 -0700 | [diff] [blame] | 143 | 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] | 144 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BUFFER_TOO_SMALL); |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 145 | return 0; |
| 146 | } |
| 147 | |
| 148 | if (nonce_len != EVP_AEAD_nonce_length(ctx->aead)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 149 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_INVALID_NONCE_SIZE); |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 150 | return 0; |
| 151 | } |
| 152 | |
David Benjamin | 7f1d5d5 | 2015-01-14 17:24:53 -0500 | [diff] [blame] | 153 | if (ad_len != 13 - 2 /* length bytes */) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 154 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_INVALID_AD_SIZE); |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 155 | return 0; |
| 156 | } |
| 157 | |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 158 | // To allow for CBC mode which changes cipher length, |ad| doesn't include the |
| 159 | // length for legacy ciphers. |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 160 | uint8_t ad_extra[2]; |
| 161 | ad_extra[0] = (uint8_t)(in_len >> 8); |
| 162 | ad_extra[1] = (uint8_t)(in_len & 0xff); |
| 163 | |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 164 | // Compute the MAC. This must be first in case the operation is being done |
| 165 | // in-place. |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 166 | uint8_t mac[EVP_MAX_MD_SIZE]; |
| 167 | unsigned mac_len; |
David Benjamin | 1741a9d | 2015-12-07 19:52:56 -0500 | [diff] [blame] | 168 | if (!HMAC_Init_ex(&tls_ctx->hmac_ctx, NULL, 0, NULL, NULL) || |
| 169 | !HMAC_Update(&tls_ctx->hmac_ctx, ad, ad_len) || |
| 170 | !HMAC_Update(&tls_ctx->hmac_ctx, ad_extra, sizeof(ad_extra)) || |
| 171 | !HMAC_Update(&tls_ctx->hmac_ctx, in, in_len) || |
| 172 | !HMAC_Final(&tls_ctx->hmac_ctx, mac, &mac_len)) { |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 173 | return 0; |
| 174 | } |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 175 | |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 176 | // Configure the explicit IV. |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 177 | if (EVP_CIPHER_CTX_mode(&tls_ctx->cipher_ctx) == EVP_CIPH_CBC_MODE && |
| 178 | !tls_ctx->implicit_iv && |
| 179 | !EVP_EncryptInit_ex(&tls_ctx->cipher_ctx, NULL, NULL, NULL, nonce)) { |
| 180 | return 0; |
| 181 | } |
| 182 | |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 183 | // Encrypt the input. |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 184 | int len; |
Martin Kreichgauer | 18d9f28 | 2017-06-06 12:29:48 -0700 | [diff] [blame] | 185 | 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] | 186 | return 0; |
| 187 | } |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 188 | |
| 189 | unsigned block_size = EVP_CIPHER_CTX_block_size(&tls_ctx->cipher_ctx); |
Martin Kreichgauer | 18d9f28 | 2017-06-06 12:29:48 -0700 | [diff] [blame] | 190 | |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 191 | // Feed the MAC into the cipher in two steps. First complete the final partial |
| 192 | // block from encrypting the input and split the result between |out| and |
| 193 | // |out_tag|. Then feed the rest. |
Martin Kreichgauer | 18d9f28 | 2017-06-06 12:29:48 -0700 | [diff] [blame] | 194 | |
Martin Kreichgauer | abbf365 | 2017-07-21 16:27:54 -0700 | [diff] [blame] | 195 | const size_t early_mac_len = |
| 196 | (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) { |
| 246 | AEAD_TLS_CTX *tls_ctx = (AEAD_TLS_CTX *)ctx->aead_state; |
| 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 | |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 302 | // Remove CBC padding. Code from here on is timing-sensitive with respect to |
| 303 | // |padding_ok| and |data_plus_mac_len| for CBC ciphers. |
Adam Langley | 518ba07 | 2017-04-20 13:51:11 -0700 | [diff] [blame] | 304 | size_t data_plus_mac_len; |
| 305 | crypto_word_t padding_ok; |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 306 | 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] | 307 | if (!EVP_tls_cbc_remove_padding( |
| 308 | &padding_ok, &data_plus_mac_len, out, total, |
| 309 | EVP_CIPHER_CTX_block_size(&tls_ctx->cipher_ctx), |
David Benjamin | 643b77e | 2017-03-16 13:46:54 -0400 | [diff] [blame] | 310 | HMAC_size(&tls_ctx->hmac_ctx))) { |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 311 | // Publicly invalid. This can be rejected in non-constant time. |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 312 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT); |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 313 | return 0; |
| 314 | } |
| 315 | } else { |
Adam Langley | 518ba07 | 2017-04-20 13:51:11 -0700 | [diff] [blame] | 316 | padding_ok = CONSTTIME_TRUE_W; |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 317 | data_plus_mac_len = total; |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 318 | // |data_plus_mac_len| = |total| = |in_len| at this point. |in_len| has |
| 319 | // 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] | 320 | assert(data_plus_mac_len >= HMAC_size(&tls_ctx->hmac_ctx)); |
| 321 | } |
David Benjamin | 643b77e | 2017-03-16 13:46:54 -0400 | [diff] [blame] | 322 | 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] | 323 | |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 324 | // At this point, if the padding is valid, the first |data_plus_mac_len| bytes |
| 325 | // after |out| are the plaintext and MAC. Otherwise, |data_plus_mac_len| is |
| 326 | // still large enough to extract a MAC, but it will be irrelevant. |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 327 | |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 328 | // To allow for CBC mode which changes cipher length, |ad| doesn't include the |
| 329 | // length for legacy ciphers. |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 330 | uint8_t ad_fixed[13]; |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 331 | OPENSSL_memcpy(ad_fixed, ad, 11); |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 332 | ad_fixed[11] = (uint8_t)(data_len >> 8); |
| 333 | ad_fixed[12] = (uint8_t)(data_len & 0xff); |
| 334 | ad_len += 2; |
| 335 | |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 336 | // Compute the MAC and extract the one in the record. |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 337 | uint8_t mac[EVP_MAX_MD_SIZE]; |
| 338 | size_t mac_len; |
| 339 | uint8_t record_mac_tmp[EVP_MAX_MD_SIZE]; |
| 340 | uint8_t *record_mac; |
| 341 | 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] | 342 | EVP_tls_cbc_record_digest_supported(tls_ctx->hmac_ctx.md)) { |
| 343 | if (!EVP_tls_cbc_digest_record(tls_ctx->hmac_ctx.md, mac, &mac_len, |
| 344 | ad_fixed, out, data_plus_mac_len, total, |
| 345 | tls_ctx->mac_key, tls_ctx->mac_key_len)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 346 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT); |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 347 | return 0; |
| 348 | } |
| 349 | assert(mac_len == HMAC_size(&tls_ctx->hmac_ctx)); |
| 350 | |
| 351 | record_mac = record_mac_tmp; |
David Benjamin | bbd8444 | 2014-12-22 07:23:54 -0500 | [diff] [blame] | 352 | 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] | 353 | } else { |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 354 | // We should support the constant-time path for all CBC-mode ciphers |
| 355 | // implemented. |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 356 | assert(EVP_CIPHER_CTX_mode(&tls_ctx->cipher_ctx) != EVP_CIPH_CBC_MODE); |
| 357 | |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 358 | unsigned mac_len_u; |
David Benjamin | 1741a9d | 2015-12-07 19:52:56 -0500 | [diff] [blame] | 359 | if (!HMAC_Init_ex(&tls_ctx->hmac_ctx, NULL, 0, NULL, NULL) || |
| 360 | !HMAC_Update(&tls_ctx->hmac_ctx, ad_fixed, ad_len) || |
| 361 | !HMAC_Update(&tls_ctx->hmac_ctx, out, data_len) || |
| 362 | !HMAC_Final(&tls_ctx->hmac_ctx, mac, &mac_len_u)) { |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 363 | return 0; |
| 364 | } |
| 365 | mac_len = mac_len_u; |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 366 | |
| 367 | assert(mac_len == HMAC_size(&tls_ctx->hmac_ctx)); |
| 368 | record_mac = &out[data_len]; |
| 369 | } |
| 370 | |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 371 | // Perform the MAC check and the padding check in constant-time. It should be |
| 372 | // safe to simply perform the padding check first, but it would not be under a |
| 373 | // different choice of MAC location on padding failure. See |
| 374 | // EVP_tls_cbc_remove_padding. |
Adam Langley | 518ba07 | 2017-04-20 13:51:11 -0700 | [diff] [blame] | 375 | crypto_word_t good = |
David Benjamin | 643b77e | 2017-03-16 13:46:54 -0400 | [diff] [blame] | 376 | constant_time_eq_int(CRYPTO_memcmp(record_mac, mac, mac_len), 0); |
David Benjamin | 3f26a49 | 2016-08-09 23:36:43 -0400 | [diff] [blame] | 377 | good &= padding_ok; |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 378 | if (!good) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 379 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT); |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 380 | return 0; |
| 381 | } |
| 382 | |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 383 | // End of timing-sensitive code. |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 384 | |
| 385 | *out_len = data_len; |
| 386 | return 1; |
| 387 | } |
| 388 | |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 389 | 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] | 390 | size_t key_len, size_t tag_len, |
| 391 | enum evp_aead_direction_t dir) { |
| 392 | 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] | 393 | EVP_sha1(), 0); |
| 394 | } |
| 395 | |
David Benjamin | b34f510 | 2015-02-28 03:59:33 -0500 | [diff] [blame] | 396 | static int aead_aes_128_cbc_sha1_tls_implicit_iv_init( |
| 397 | EVP_AEAD_CTX *ctx, const uint8_t *key, size_t key_len, size_t tag_len, |
| 398 | enum evp_aead_direction_t dir) { |
| 399 | 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] | 400 | EVP_sha1(), 1); |
| 401 | } |
| 402 | |
| 403 | static int aead_aes_128_cbc_sha256_tls_init(EVP_AEAD_CTX *ctx, |
| 404 | const uint8_t *key, size_t key_len, |
David Benjamin | b34f510 | 2015-02-28 03:59:33 -0500 | [diff] [blame] | 405 | size_t tag_len, |
| 406 | enum evp_aead_direction_t dir) { |
| 407 | 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] | 408 | EVP_sha256(), 0); |
| 409 | } |
| 410 | |
| 411 | 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] | 412 | size_t key_len, size_t tag_len, |
| 413 | enum evp_aead_direction_t dir) { |
| 414 | 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] | 415 | EVP_sha1(), 0); |
| 416 | } |
| 417 | |
David Benjamin | b34f510 | 2015-02-28 03:59:33 -0500 | [diff] [blame] | 418 | static int aead_aes_256_cbc_sha1_tls_implicit_iv_init( |
| 419 | EVP_AEAD_CTX *ctx, const uint8_t *key, size_t key_len, size_t tag_len, |
| 420 | enum evp_aead_direction_t dir) { |
| 421 | 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] | 422 | EVP_sha1(), 1); |
| 423 | } |
| 424 | |
| 425 | static int aead_aes_256_cbc_sha256_tls_init(EVP_AEAD_CTX *ctx, |
| 426 | const uint8_t *key, size_t key_len, |
David Benjamin | b34f510 | 2015-02-28 03:59:33 -0500 | [diff] [blame] | 427 | size_t tag_len, |
| 428 | enum evp_aead_direction_t dir) { |
| 429 | 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] | 430 | EVP_sha256(), 0); |
| 431 | } |
| 432 | |
| 433 | static int aead_aes_256_cbc_sha384_tls_init(EVP_AEAD_CTX *ctx, |
| 434 | const uint8_t *key, size_t key_len, |
David Benjamin | b34f510 | 2015-02-28 03:59:33 -0500 | [diff] [blame] | 435 | size_t tag_len, |
| 436 | enum evp_aead_direction_t dir) { |
| 437 | 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] | 438 | EVP_sha384(), 0); |
| 439 | } |
| 440 | |
| 441 | static int aead_des_ede3_cbc_sha1_tls_init(EVP_AEAD_CTX *ctx, |
| 442 | const uint8_t *key, size_t key_len, |
David Benjamin | b34f510 | 2015-02-28 03:59:33 -0500 | [diff] [blame] | 443 | size_t tag_len, |
| 444 | enum evp_aead_direction_t dir) { |
| 445 | 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] | 446 | EVP_sha1(), 0); |
| 447 | } |
| 448 | |
David Benjamin | b34f510 | 2015-02-28 03:59:33 -0500 | [diff] [blame] | 449 | static int aead_des_ede3_cbc_sha1_tls_implicit_iv_init( |
| 450 | EVP_AEAD_CTX *ctx, const uint8_t *key, size_t key_len, size_t tag_len, |
| 451 | enum evp_aead_direction_t dir) { |
| 452 | 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] | 453 | EVP_sha1(), 1); |
| 454 | } |
| 455 | |
Adam Langley | c2d3280 | 2015-11-03 18:36:10 -0800 | [diff] [blame] | 456 | static int aead_tls_get_iv(const EVP_AEAD_CTX *ctx, const uint8_t **out_iv, |
| 457 | size_t *out_iv_len) { |
| 458 | const AEAD_TLS_CTX *tls_ctx = (AEAD_TLS_CTX*) ctx->aead_state; |
| 459 | const size_t iv_len = EVP_CIPHER_CTX_iv_length(&tls_ctx->cipher_ctx); |
| 460 | if (iv_len <= 1) { |
| 461 | return 0; |
| 462 | } |
| 463 | |
| 464 | *out_iv = tls_ctx->cipher_ctx.iv; |
| 465 | *out_iv_len = iv_len; |
| 466 | return 1; |
| 467 | } |
| 468 | |
Matt Braithwaite | af09675 | 2015-09-02 19:48:16 -0700 | [diff] [blame] | 469 | static int aead_null_sha1_tls_init(EVP_AEAD_CTX *ctx, const uint8_t *key, |
| 470 | size_t key_len, size_t tag_len, |
| 471 | enum evp_aead_direction_t dir) { |
| 472 | return aead_tls_init(ctx, key, key_len, tag_len, dir, EVP_enc_null(), |
| 473 | EVP_sha1(), 1 /* implicit iv */); |
| 474 | } |
| 475 | |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 476 | static const EVP_AEAD aead_aes_128_cbc_sha1_tls = { |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 477 | SHA_DIGEST_LENGTH + 16, // key len (SHA1 + AES128) |
| 478 | 16, // nonce len (IV) |
| 479 | 16 + SHA_DIGEST_LENGTH, // overhead (padding + SHA1) |
| 480 | SHA_DIGEST_LENGTH, // max tag length |
| 481 | 0, // seal_scatter_supports_extra_in |
Martin Kreichgauer | 74bce29 | 2017-06-23 14:49:22 -0700 | [diff] [blame] | 482 | |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 483 | NULL, // init |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 484 | aead_aes_128_cbc_sha1_tls_init, |
| 485 | aead_tls_cleanup, |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 486 | aead_tls_open, |
Martin Kreichgauer | 18d9f28 | 2017-06-06 12:29:48 -0700 | [diff] [blame] | 487 | aead_tls_seal_scatter, |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 488 | NULL, // open_gather |
| 489 | NULL, // get_iv |
Martin Kreichgauer | abbf365 | 2017-07-21 16:27:54 -0700 | [diff] [blame] | 490 | aead_tls_tag_len, |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 491 | }; |
| 492 | |
| 493 | static const EVP_AEAD aead_aes_128_cbc_sha1_tls_implicit_iv = { |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 494 | SHA_DIGEST_LENGTH + 16 + 16, // key len (SHA1 + AES128 + IV) |
| 495 | 0, // nonce len |
| 496 | 16 + SHA_DIGEST_LENGTH, // overhead (padding + SHA1) |
| 497 | SHA_DIGEST_LENGTH, // max tag length |
| 498 | 0, // seal_scatter_supports_extra_in |
Martin Kreichgauer | 74bce29 | 2017-06-23 14:49:22 -0700 | [diff] [blame] | 499 | |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 500 | NULL, // init |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 501 | aead_aes_128_cbc_sha1_tls_implicit_iv_init, |
| 502 | aead_tls_cleanup, |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 503 | aead_tls_open, |
Martin Kreichgauer | 18d9f28 | 2017-06-06 12:29:48 -0700 | [diff] [blame] | 504 | aead_tls_seal_scatter, |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 505 | NULL, // open_gather |
| 506 | aead_tls_get_iv, // get_iv |
Martin Kreichgauer | abbf365 | 2017-07-21 16:27:54 -0700 | [diff] [blame] | 507 | aead_tls_tag_len, |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 508 | }; |
| 509 | |
| 510 | static const EVP_AEAD aead_aes_128_cbc_sha256_tls = { |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 511 | SHA256_DIGEST_LENGTH + 16, // key len (SHA256 + AES128) |
| 512 | 16, // nonce len (IV) |
| 513 | 16 + SHA256_DIGEST_LENGTH, // overhead (padding + SHA256) |
| 514 | SHA256_DIGEST_LENGTH, // max tag length |
| 515 | 0, // seal_scatter_supports_extra_in |
Martin Kreichgauer | 74bce29 | 2017-06-23 14:49:22 -0700 | [diff] [blame] | 516 | |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 517 | NULL, // init |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 518 | aead_aes_128_cbc_sha256_tls_init, |
| 519 | aead_tls_cleanup, |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 520 | aead_tls_open, |
Martin Kreichgauer | 18d9f28 | 2017-06-06 12:29:48 -0700 | [diff] [blame] | 521 | aead_tls_seal_scatter, |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 522 | NULL, // open_gather |
| 523 | NULL, // get_iv |
Martin Kreichgauer | abbf365 | 2017-07-21 16:27:54 -0700 | [diff] [blame] | 524 | aead_tls_tag_len, |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 525 | }; |
| 526 | |
| 527 | static const EVP_AEAD aead_aes_256_cbc_sha1_tls = { |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 528 | SHA_DIGEST_LENGTH + 32, // key len (SHA1 + AES256) |
| 529 | 16, // nonce len (IV) |
| 530 | 16 + SHA_DIGEST_LENGTH, // overhead (padding + SHA1) |
| 531 | SHA_DIGEST_LENGTH, // max tag length |
| 532 | 0, // seal_scatter_supports_extra_in |
Martin Kreichgauer | 74bce29 | 2017-06-23 14:49:22 -0700 | [diff] [blame] | 533 | |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 534 | NULL, // init |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 535 | aead_aes_256_cbc_sha1_tls_init, |
| 536 | aead_tls_cleanup, |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 537 | aead_tls_open, |
Martin Kreichgauer | 18d9f28 | 2017-06-06 12:29:48 -0700 | [diff] [blame] | 538 | aead_tls_seal_scatter, |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 539 | NULL, // open_gather |
| 540 | NULL, // get_iv |
Martin Kreichgauer | abbf365 | 2017-07-21 16:27:54 -0700 | [diff] [blame] | 541 | aead_tls_tag_len, |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 542 | }; |
| 543 | |
| 544 | static const EVP_AEAD aead_aes_256_cbc_sha1_tls_implicit_iv = { |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 545 | SHA_DIGEST_LENGTH + 32 + 16, // key len (SHA1 + AES256 + IV) |
| 546 | 0, // nonce len |
| 547 | 16 + SHA_DIGEST_LENGTH, // overhead (padding + SHA1) |
| 548 | SHA_DIGEST_LENGTH, // max tag length |
| 549 | 0, // seal_scatter_supports_extra_in |
Martin Kreichgauer | 74bce29 | 2017-06-23 14:49:22 -0700 | [diff] [blame] | 550 | |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 551 | NULL, // init |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 552 | aead_aes_256_cbc_sha1_tls_implicit_iv_init, |
| 553 | aead_tls_cleanup, |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 554 | aead_tls_open, |
Martin Kreichgauer | 18d9f28 | 2017-06-06 12:29:48 -0700 | [diff] [blame] | 555 | aead_tls_seal_scatter, |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 556 | NULL, // open_gather |
| 557 | aead_tls_get_iv, // get_iv |
Martin Kreichgauer | abbf365 | 2017-07-21 16:27:54 -0700 | [diff] [blame] | 558 | aead_tls_tag_len, |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 559 | }; |
| 560 | |
| 561 | static const EVP_AEAD aead_aes_256_cbc_sha256_tls = { |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 562 | SHA256_DIGEST_LENGTH + 32, // key len (SHA256 + AES256) |
| 563 | 16, // nonce len (IV) |
| 564 | 16 + SHA256_DIGEST_LENGTH, // overhead (padding + SHA256) |
| 565 | SHA256_DIGEST_LENGTH, // max tag length |
| 566 | 0, // seal_scatter_supports_extra_in |
Martin Kreichgauer | 74bce29 | 2017-06-23 14:49:22 -0700 | [diff] [blame] | 567 | |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 568 | NULL, // init |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 569 | aead_aes_256_cbc_sha256_tls_init, |
| 570 | aead_tls_cleanup, |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 571 | aead_tls_open, |
Martin Kreichgauer | 18d9f28 | 2017-06-06 12:29:48 -0700 | [diff] [blame] | 572 | aead_tls_seal_scatter, |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 573 | NULL, // open_gather |
| 574 | NULL, // get_iv |
Martin Kreichgauer | abbf365 | 2017-07-21 16:27:54 -0700 | [diff] [blame] | 575 | aead_tls_tag_len, |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 576 | }; |
| 577 | |
| 578 | static const EVP_AEAD aead_aes_256_cbc_sha384_tls = { |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 579 | SHA384_DIGEST_LENGTH + 32, // key len (SHA384 + AES256) |
| 580 | 16, // nonce len (IV) |
| 581 | 16 + SHA384_DIGEST_LENGTH, // overhead (padding + SHA384) |
| 582 | SHA384_DIGEST_LENGTH, // max tag length |
| 583 | 0, // seal_scatter_supports_extra_in |
Martin Kreichgauer | 74bce29 | 2017-06-23 14:49:22 -0700 | [diff] [blame] | 584 | |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 585 | NULL, // init |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 586 | aead_aes_256_cbc_sha384_tls_init, |
| 587 | aead_tls_cleanup, |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 588 | aead_tls_open, |
Martin Kreichgauer | 18d9f28 | 2017-06-06 12:29:48 -0700 | [diff] [blame] | 589 | aead_tls_seal_scatter, |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 590 | NULL, // open_gather |
| 591 | NULL, // get_iv |
Martin Kreichgauer | abbf365 | 2017-07-21 16:27:54 -0700 | [diff] [blame] | 592 | aead_tls_tag_len, |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 593 | }; |
| 594 | |
| 595 | static const EVP_AEAD aead_des_ede3_cbc_sha1_tls = { |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 596 | SHA_DIGEST_LENGTH + 24, // key len (SHA1 + 3DES) |
| 597 | 8, // nonce len (IV) |
| 598 | 8 + SHA_DIGEST_LENGTH, // overhead (padding + SHA1) |
| 599 | SHA_DIGEST_LENGTH, // max tag length |
| 600 | 0, // seal_scatter_supports_extra_in |
Martin Kreichgauer | 74bce29 | 2017-06-23 14:49:22 -0700 | [diff] [blame] | 601 | |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 602 | NULL, // init |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 603 | aead_des_ede3_cbc_sha1_tls_init, |
| 604 | aead_tls_cleanup, |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 605 | aead_tls_open, |
Martin Kreichgauer | 18d9f28 | 2017-06-06 12:29:48 -0700 | [diff] [blame] | 606 | aead_tls_seal_scatter, |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 607 | NULL, // open_gather |
| 608 | NULL, // get_iv |
Martin Kreichgauer | abbf365 | 2017-07-21 16:27:54 -0700 | [diff] [blame] | 609 | aead_tls_tag_len, |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 610 | }; |
| 611 | |
| 612 | static const EVP_AEAD aead_des_ede3_cbc_sha1_tls_implicit_iv = { |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 613 | SHA_DIGEST_LENGTH + 24 + 8, // key len (SHA1 + 3DES + IV) |
| 614 | 0, // nonce len |
| 615 | 8 + SHA_DIGEST_LENGTH, // overhead (padding + SHA1) |
| 616 | SHA_DIGEST_LENGTH, // max tag length |
| 617 | 0, // seal_scatter_supports_extra_in |
Martin Kreichgauer | 74bce29 | 2017-06-23 14:49:22 -0700 | [diff] [blame] | 618 | |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 619 | NULL, // init |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 620 | aead_des_ede3_cbc_sha1_tls_implicit_iv_init, |
| 621 | aead_tls_cleanup, |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 622 | aead_tls_open, |
Martin Kreichgauer | 18d9f28 | 2017-06-06 12:29:48 -0700 | [diff] [blame] | 623 | aead_tls_seal_scatter, |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 624 | NULL, // open_gather |
| 625 | aead_tls_get_iv, // get_iv |
Martin Kreichgauer | abbf365 | 2017-07-21 16:27:54 -0700 | [diff] [blame] | 626 | aead_tls_tag_len, |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 627 | }; |
| 628 | |
Matt Braithwaite | af09675 | 2015-09-02 19:48:16 -0700 | [diff] [blame] | 629 | static const EVP_AEAD aead_null_sha1_tls = { |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 630 | SHA_DIGEST_LENGTH, // key len |
| 631 | 0, // nonce len |
| 632 | SHA_DIGEST_LENGTH, // overhead (SHA1) |
| 633 | SHA_DIGEST_LENGTH, // max tag length |
| 634 | 0, // seal_scatter_supports_extra_in |
Martin Kreichgauer | 74bce29 | 2017-06-23 14:49:22 -0700 | [diff] [blame] | 635 | |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 636 | NULL, // init |
Matt Braithwaite | af09675 | 2015-09-02 19:48:16 -0700 | [diff] [blame] | 637 | aead_null_sha1_tls_init, |
| 638 | aead_tls_cleanup, |
Matt Braithwaite | af09675 | 2015-09-02 19:48:16 -0700 | [diff] [blame] | 639 | aead_tls_open, |
Martin Kreichgauer | 18d9f28 | 2017-06-06 12:29:48 -0700 | [diff] [blame] | 640 | aead_tls_seal_scatter, |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 641 | NULL, // open_gather |
| 642 | NULL, // get_iv |
Martin Kreichgauer | abbf365 | 2017-07-21 16:27:54 -0700 | [diff] [blame] | 643 | aead_tls_tag_len, |
Matt Braithwaite | af09675 | 2015-09-02 19:48:16 -0700 | [diff] [blame] | 644 | }; |
| 645 | |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 646 | const EVP_AEAD *EVP_aead_aes_128_cbc_sha1_tls(void) { |
| 647 | return &aead_aes_128_cbc_sha1_tls; |
| 648 | } |
| 649 | |
| 650 | const EVP_AEAD *EVP_aead_aes_128_cbc_sha1_tls_implicit_iv(void) { |
| 651 | return &aead_aes_128_cbc_sha1_tls_implicit_iv; |
| 652 | } |
| 653 | |
| 654 | const EVP_AEAD *EVP_aead_aes_128_cbc_sha256_tls(void) { |
| 655 | return &aead_aes_128_cbc_sha256_tls; |
| 656 | } |
| 657 | |
| 658 | const EVP_AEAD *EVP_aead_aes_256_cbc_sha1_tls(void) { |
| 659 | return &aead_aes_256_cbc_sha1_tls; |
| 660 | } |
| 661 | |
| 662 | const EVP_AEAD *EVP_aead_aes_256_cbc_sha1_tls_implicit_iv(void) { |
| 663 | return &aead_aes_256_cbc_sha1_tls_implicit_iv; |
| 664 | } |
| 665 | |
| 666 | const EVP_AEAD *EVP_aead_aes_256_cbc_sha256_tls(void) { |
| 667 | return &aead_aes_256_cbc_sha256_tls; |
| 668 | } |
| 669 | |
| 670 | const EVP_AEAD *EVP_aead_aes_256_cbc_sha384_tls(void) { |
| 671 | return &aead_aes_256_cbc_sha384_tls; |
| 672 | } |
| 673 | |
| 674 | const EVP_AEAD *EVP_aead_des_ede3_cbc_sha1_tls(void) { |
| 675 | return &aead_des_ede3_cbc_sha1_tls; |
| 676 | } |
| 677 | |
| 678 | const EVP_AEAD *EVP_aead_des_ede3_cbc_sha1_tls_implicit_iv(void) { |
| 679 | return &aead_des_ede3_cbc_sha1_tls_implicit_iv; |
| 680 | } |
Matt Braithwaite | af09675 | 2015-09-02 19:48:16 -0700 | [diff] [blame] | 681 | |
| 682 | const EVP_AEAD *EVP_aead_null_sha1_tls(void) { return &aead_null_sha1_tls; } |