David Benjamin | 044abb0 | 2014-12-23 10:57:17 -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> |
| 23 | #include <openssl/md5.h> |
| 24 | #include <openssl/mem.h> |
| 25 | #include <openssl/sha.h> |
| 26 | |
| 27 | #include "internal.h" |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 28 | #include "../internal.h" |
Adam Langley | 2e2a226 | 2017-05-03 13:23:37 -0700 | [diff] [blame] | 29 | #include "../fipsmodule/cipher/internal.h" |
David Benjamin | 044abb0 | 2014-12-23 10:57:17 -0500 | [diff] [blame] | 30 | |
| 31 | |
| 32 | typedef struct { |
| 33 | EVP_CIPHER_CTX cipher_ctx; |
| 34 | EVP_MD_CTX md_ctx; |
David Benjamin | 044abb0 | 2014-12-23 10:57:17 -0500 | [diff] [blame] | 35 | } AEAD_SSL3_CTX; |
| 36 | |
| 37 | static int ssl3_mac(AEAD_SSL3_CTX *ssl3_ctx, uint8_t *out, unsigned *out_len, |
| 38 | const uint8_t *ad, size_t ad_len, const uint8_t *in, |
| 39 | size_t in_len) { |
| 40 | size_t md_size = EVP_MD_CTX_size(&ssl3_ctx->md_ctx); |
| 41 | size_t pad_len = (md_size == 20) ? 40 : 48; |
| 42 | |
| 43 | /* To allow for CBC mode which changes cipher length, |ad| doesn't include the |
| 44 | * length for legacy ciphers. */ |
| 45 | uint8_t ad_extra[2]; |
| 46 | ad_extra[0] = (uint8_t)(in_len >> 8); |
| 47 | ad_extra[1] = (uint8_t)(in_len & 0xff); |
| 48 | |
| 49 | EVP_MD_CTX md_ctx; |
| 50 | EVP_MD_CTX_init(&md_ctx); |
| 51 | |
David Benjamin | 7f1d5d5 | 2015-01-14 17:24:53 -0500 | [diff] [blame] | 52 | uint8_t pad[48]; |
David Benjamin | 044abb0 | 2014-12-23 10:57:17 -0500 | [diff] [blame] | 53 | uint8_t tmp[EVP_MAX_MD_SIZE]; |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 54 | OPENSSL_memset(pad, 0x36, pad_len); |
David Benjamin | 044abb0 | 2014-12-23 10:57:17 -0500 | [diff] [blame] | 55 | if (!EVP_MD_CTX_copy_ex(&md_ctx, &ssl3_ctx->md_ctx) || |
| 56 | !EVP_DigestUpdate(&md_ctx, pad, pad_len) || |
| 57 | !EVP_DigestUpdate(&md_ctx, ad, ad_len) || |
| 58 | !EVP_DigestUpdate(&md_ctx, ad_extra, sizeof(ad_extra)) || |
| 59 | !EVP_DigestUpdate(&md_ctx, in, in_len) || |
| 60 | !EVP_DigestFinal_ex(&md_ctx, tmp, NULL)) { |
| 61 | EVP_MD_CTX_cleanup(&md_ctx); |
| 62 | return 0; |
| 63 | } |
| 64 | |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 65 | OPENSSL_memset(pad, 0x5c, pad_len); |
David Benjamin | 044abb0 | 2014-12-23 10:57:17 -0500 | [diff] [blame] | 66 | if (!EVP_MD_CTX_copy_ex(&md_ctx, &ssl3_ctx->md_ctx) || |
| 67 | !EVP_DigestUpdate(&md_ctx, pad, pad_len) || |
| 68 | !EVP_DigestUpdate(&md_ctx, tmp, md_size) || |
| 69 | !EVP_DigestFinal_ex(&md_ctx, out, out_len)) { |
| 70 | EVP_MD_CTX_cleanup(&md_ctx); |
| 71 | return 0; |
| 72 | } |
| 73 | EVP_MD_CTX_cleanup(&md_ctx); |
| 74 | return 1; |
| 75 | } |
| 76 | |
| 77 | static void aead_ssl3_cleanup(EVP_AEAD_CTX *ctx) { |
| 78 | AEAD_SSL3_CTX *ssl3_ctx = (AEAD_SSL3_CTX *)ctx->aead_state; |
| 79 | EVP_CIPHER_CTX_cleanup(&ssl3_ctx->cipher_ctx); |
| 80 | EVP_MD_CTX_cleanup(&ssl3_ctx->md_ctx); |
David Benjamin | 044abb0 | 2014-12-23 10:57:17 -0500 | [diff] [blame] | 81 | OPENSSL_free(ssl3_ctx); |
| 82 | ctx->aead_state = NULL; |
| 83 | } |
| 84 | |
| 85 | static int aead_ssl3_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] | 86 | size_t tag_len, enum evp_aead_direction_t dir, |
| 87 | const EVP_CIPHER *cipher, const EVP_MD *md) { |
David Benjamin | 044abb0 | 2014-12-23 10:57:17 -0500 | [diff] [blame] | 88 | if (tag_len != EVP_AEAD_DEFAULT_TAG_LENGTH && |
| 89 | tag_len != EVP_MD_size(md)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 90 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_UNSUPPORTED_TAG_SIZE); |
David Benjamin | 044abb0 | 2014-12-23 10:57:17 -0500 | [diff] [blame] | 91 | return 0; |
| 92 | } |
| 93 | |
| 94 | if (key_len != EVP_AEAD_key_length(ctx->aead)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 95 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_KEY_LENGTH); |
David Benjamin | 044abb0 | 2014-12-23 10:57:17 -0500 | [diff] [blame] | 96 | return 0; |
| 97 | } |
| 98 | |
| 99 | size_t mac_key_len = EVP_MD_size(md); |
| 100 | size_t enc_key_len = EVP_CIPHER_key_length(cipher); |
David Benjamin | b34f510 | 2015-02-28 03:59:33 -0500 | [diff] [blame] | 101 | assert(mac_key_len + enc_key_len + EVP_CIPHER_iv_length(cipher) == key_len); |
David Benjamin | 044abb0 | 2014-12-23 10:57:17 -0500 | [diff] [blame] | 102 | |
| 103 | AEAD_SSL3_CTX *ssl3_ctx = OPENSSL_malloc(sizeof(AEAD_SSL3_CTX)); |
| 104 | if (ssl3_ctx == NULL) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 105 | OPENSSL_PUT_ERROR(CIPHER, ERR_R_MALLOC_FAILURE); |
David Benjamin | 044abb0 | 2014-12-23 10:57:17 -0500 | [diff] [blame] | 106 | return 0; |
| 107 | } |
| 108 | EVP_CIPHER_CTX_init(&ssl3_ctx->cipher_ctx); |
| 109 | EVP_MD_CTX_init(&ssl3_ctx->md_ctx); |
David Benjamin | 044abb0 | 2014-12-23 10:57:17 -0500 | [diff] [blame] | 110 | |
| 111 | ctx->aead_state = ssl3_ctx; |
David Benjamin | b34f510 | 2015-02-28 03:59:33 -0500 | [diff] [blame] | 112 | if (!EVP_CipherInit_ex(&ssl3_ctx->cipher_ctx, cipher, NULL, &key[mac_key_len], |
| 113 | &key[mac_key_len + enc_key_len], |
| 114 | dir == evp_aead_seal) || |
David Benjamin | 044abb0 | 2014-12-23 10:57:17 -0500 | [diff] [blame] | 115 | !EVP_DigestInit_ex(&ssl3_ctx->md_ctx, md, NULL) || |
| 116 | !EVP_DigestUpdate(&ssl3_ctx->md_ctx, key, mac_key_len)) { |
| 117 | aead_ssl3_cleanup(ctx); |
Adam Langley | 5aa8a86 | 2015-05-11 14:28:12 -0700 | [diff] [blame] | 118 | ctx->aead_state = NULL; |
David Benjamin | 044abb0 | 2014-12-23 10:57:17 -0500 | [diff] [blame] | 119 | return 0; |
| 120 | } |
| 121 | EVP_CIPHER_CTX_set_padding(&ssl3_ctx->cipher_ctx, 0); |
| 122 | |
| 123 | return 1; |
| 124 | } |
| 125 | |
Martin Kreichgauer | abbf365 | 2017-07-21 16:27:54 -0700 | [diff] [blame] | 126 | static size_t aead_ssl3_tag_len(const EVP_AEAD_CTX *ctx, const size_t in_len, |
| 127 | const size_t extra_in_len) { |
| 128 | assert(extra_in_len == 0); |
| 129 | const AEAD_SSL3_CTX *ssl3_ctx = (AEAD_SSL3_CTX*)ctx->aead_state; |
| 130 | |
| 131 | const size_t digest_len = EVP_MD_CTX_size(&ssl3_ctx->md_ctx); |
| 132 | if (EVP_CIPHER_CTX_mode(&ssl3_ctx->cipher_ctx) != EVP_CIPH_CBC_MODE) { |
| 133 | // The NULL cipher. |
| 134 | return digest_len; |
| 135 | } |
| 136 | |
| 137 | const size_t block_size = EVP_CIPHER_CTX_block_size(&ssl3_ctx->cipher_ctx); |
| 138 | /* An overflow of |in_len + digest_len| doesn't affect the result mod |
| 139 | * |block_size|, provided that |block_size| is a smaller power of two. */ |
| 140 | assert(block_size != 0 && (block_size & (block_size - 1)) == 0); |
| 141 | const size_t pad_len = block_size - ((in_len + digest_len) % block_size); |
| 142 | return digest_len + pad_len; |
| 143 | } |
| 144 | |
Martin Kreichgauer | 18d9f28 | 2017-06-06 12:29:48 -0700 | [diff] [blame] | 145 | static int aead_ssl3_seal_scatter(const EVP_AEAD_CTX *ctx, uint8_t *out, |
| 146 | uint8_t *out_tag, size_t *out_tag_len, |
Martin Kreichgauer | abbf365 | 2017-07-21 16:27:54 -0700 | [diff] [blame] | 147 | const size_t max_out_tag_len, |
| 148 | const uint8_t *nonce, const size_t nonce_len, |
| 149 | const uint8_t *in, const size_t in_len, |
| 150 | const uint8_t *extra_in, |
| 151 | const size_t extra_in_len, const uint8_t *ad, |
| 152 | const size_t ad_len) { |
David Benjamin | 044abb0 | 2014-12-23 10:57:17 -0500 | [diff] [blame] | 153 | AEAD_SSL3_CTX *ssl3_ctx = (AEAD_SSL3_CTX *)ctx->aead_state; |
David Benjamin | 044abb0 | 2014-12-23 10:57:17 -0500 | [diff] [blame] | 154 | |
David Benjamin | b34f510 | 2015-02-28 03:59:33 -0500 | [diff] [blame] | 155 | if (!ssl3_ctx->cipher_ctx.encrypt) { |
| 156 | /* Unlike a normal AEAD, an SSL3 AEAD may only be used in one direction. */ |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 157 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_INVALID_OPERATION); |
David Benjamin | b34f510 | 2015-02-28 03:59:33 -0500 | [diff] [blame] | 158 | return 0; |
| 159 | } |
| 160 | |
Martin Kreichgauer | 18d9f28 | 2017-06-06 12:29:48 -0700 | [diff] [blame] | 161 | if (in_len > INT_MAX) { |
David Benjamin | 044abb0 | 2014-12-23 10:57:17 -0500 | [diff] [blame] | 162 | /* EVP_CIPHER takes int as input. */ |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 163 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TOO_LARGE); |
David Benjamin | 044abb0 | 2014-12-23 10:57:17 -0500 | [diff] [blame] | 164 | return 0; |
| 165 | } |
| 166 | |
Martin Kreichgauer | abbf365 | 2017-07-21 16:27:54 -0700 | [diff] [blame] | 167 | if (max_out_tag_len < aead_ssl3_tag_len(ctx, in_len, extra_in_len)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 168 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BUFFER_TOO_SMALL); |
David Benjamin | 044abb0 | 2014-12-23 10:57:17 -0500 | [diff] [blame] | 169 | return 0; |
| 170 | } |
| 171 | |
| 172 | if (nonce_len != 0) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 173 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_IV_TOO_LARGE); |
David Benjamin | 044abb0 | 2014-12-23 10:57:17 -0500 | [diff] [blame] | 174 | return 0; |
| 175 | } |
| 176 | |
David Benjamin | 7f1d5d5 | 2015-01-14 17:24:53 -0500 | [diff] [blame] | 177 | if (ad_len != 11 - 2 /* length bytes */) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 178 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_INVALID_AD_SIZE); |
David Benjamin | 044abb0 | 2014-12-23 10:57:17 -0500 | [diff] [blame] | 179 | return 0; |
| 180 | } |
| 181 | |
David Benjamin | 044abb0 | 2014-12-23 10:57:17 -0500 | [diff] [blame] | 182 | /* Compute the MAC. This must be first in case the operation is being done |
| 183 | * in-place. */ |
| 184 | uint8_t mac[EVP_MAX_MD_SIZE]; |
| 185 | unsigned mac_len; |
| 186 | if (!ssl3_mac(ssl3_ctx, mac, &mac_len, ad, ad_len, in, in_len)) { |
| 187 | return 0; |
| 188 | } |
| 189 | |
| 190 | /* Encrypt the input. */ |
| 191 | int len; |
| 192 | if (!EVP_EncryptUpdate(&ssl3_ctx->cipher_ctx, out, &len, in, |
| 193 | (int)in_len)) { |
| 194 | return 0; |
| 195 | } |
David Benjamin | 044abb0 | 2014-12-23 10:57:17 -0500 | [diff] [blame] | 196 | |
Martin Kreichgauer | 18d9f28 | 2017-06-06 12:29:48 -0700 | [diff] [blame] | 197 | const size_t block_size = EVP_CIPHER_CTX_block_size(&ssl3_ctx->cipher_ctx); |
| 198 | |
| 199 | /* Feed the MAC into the cipher in two steps. First complete the final partial |
| 200 | * block from encrypting the input and split the result between |out| and |
| 201 | * |out_tag|. Then encrypt the remainder. */ |
| 202 | |
| 203 | size_t early_mac_len = (block_size - (in_len % block_size)) % block_size; |
| 204 | if (early_mac_len != 0) { |
| 205 | assert(len + block_size - early_mac_len == in_len); |
| 206 | uint8_t buf[EVP_MAX_BLOCK_LENGTH]; |
| 207 | int buf_len; |
| 208 | if (!EVP_EncryptUpdate(&ssl3_ctx->cipher_ctx, buf, &buf_len, mac, |
| 209 | (int)early_mac_len)) { |
| 210 | return 0; |
| 211 | } |
| 212 | assert(buf_len == (int)block_size); |
| 213 | OPENSSL_memcpy(out + len, buf, block_size - early_mac_len); |
| 214 | OPENSSL_memcpy(out_tag, buf + block_size - early_mac_len, early_mac_len); |
| 215 | } |
| 216 | size_t tag_len = early_mac_len; |
| 217 | |
| 218 | if (!EVP_EncryptUpdate(&ssl3_ctx->cipher_ctx, out_tag + tag_len, &len, |
| 219 | mac + tag_len, mac_len - tag_len)) { |
David Benjamin | 044abb0 | 2014-12-23 10:57:17 -0500 | [diff] [blame] | 220 | return 0; |
| 221 | } |
Martin Kreichgauer | 18d9f28 | 2017-06-06 12:29:48 -0700 | [diff] [blame] | 222 | tag_len += len; |
David Benjamin | 044abb0 | 2014-12-23 10:57:17 -0500 | [diff] [blame] | 223 | |
David Benjamin | 044abb0 | 2014-12-23 10:57:17 -0500 | [diff] [blame] | 224 | if (block_size > 1) { |
| 225 | assert(block_size <= 256); |
| 226 | assert(EVP_CIPHER_CTX_mode(&ssl3_ctx->cipher_ctx) == EVP_CIPH_CBC_MODE); |
| 227 | |
| 228 | /* Compute padding and feed that into the cipher. */ |
| 229 | uint8_t padding[256]; |
Martin Kreichgauer | 18d9f28 | 2017-06-06 12:29:48 -0700 | [diff] [blame] | 230 | size_t padding_len = block_size - ((in_len + mac_len) % block_size); |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 231 | OPENSSL_memset(padding, 0, padding_len - 1); |
David Benjamin | 044abb0 | 2014-12-23 10:57:17 -0500 | [diff] [blame] | 232 | padding[padding_len - 1] = padding_len - 1; |
Martin Kreichgauer | 18d9f28 | 2017-06-06 12:29:48 -0700 | [diff] [blame] | 233 | if (!EVP_EncryptUpdate(&ssl3_ctx->cipher_ctx, out_tag + tag_len, &len, padding, |
David Benjamin | 044abb0 | 2014-12-23 10:57:17 -0500 | [diff] [blame] | 234 | (int)padding_len)) { |
| 235 | return 0; |
| 236 | } |
Martin Kreichgauer | 18d9f28 | 2017-06-06 12:29:48 -0700 | [diff] [blame] | 237 | tag_len += len; |
David Benjamin | 044abb0 | 2014-12-23 10:57:17 -0500 | [diff] [blame] | 238 | } |
| 239 | |
Martin Kreichgauer | 18d9f28 | 2017-06-06 12:29:48 -0700 | [diff] [blame] | 240 | if (!EVP_EncryptFinal_ex(&ssl3_ctx->cipher_ctx, out_tag + tag_len, &len)) { |
David Benjamin | 044abb0 | 2014-12-23 10:57:17 -0500 | [diff] [blame] | 241 | return 0; |
| 242 | } |
Martin Kreichgauer | 18d9f28 | 2017-06-06 12:29:48 -0700 | [diff] [blame] | 243 | tag_len += len; |
Martin Kreichgauer | abbf365 | 2017-07-21 16:27:54 -0700 | [diff] [blame] | 244 | assert(tag_len == aead_ssl3_tag_len(ctx, in_len, extra_in_len)); |
David Benjamin | 044abb0 | 2014-12-23 10:57:17 -0500 | [diff] [blame] | 245 | |
Martin Kreichgauer | 18d9f28 | 2017-06-06 12:29:48 -0700 | [diff] [blame] | 246 | *out_tag_len = tag_len; |
David Benjamin | 044abb0 | 2014-12-23 10:57:17 -0500 | [diff] [blame] | 247 | return 1; |
| 248 | } |
| 249 | |
| 250 | static int aead_ssl3_open(const EVP_AEAD_CTX *ctx, uint8_t *out, |
| 251 | size_t *out_len, size_t max_out_len, |
| 252 | const uint8_t *nonce, size_t nonce_len, |
| 253 | const uint8_t *in, size_t in_len, |
| 254 | const uint8_t *ad, size_t ad_len) { |
| 255 | AEAD_SSL3_CTX *ssl3_ctx = (AEAD_SSL3_CTX *)ctx->aead_state; |
| 256 | |
David Benjamin | b34f510 | 2015-02-28 03:59:33 -0500 | [diff] [blame] | 257 | if (ssl3_ctx->cipher_ctx.encrypt) { |
| 258 | /* Unlike a normal AEAD, an SSL3 AEAD may only be used in one direction. */ |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 259 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_INVALID_OPERATION); |
David Benjamin | b34f510 | 2015-02-28 03:59:33 -0500 | [diff] [blame] | 260 | return 0; |
| 261 | } |
| 262 | |
David Benjamin | 044abb0 | 2014-12-23 10:57:17 -0500 | [diff] [blame] | 263 | size_t mac_len = EVP_MD_CTX_size(&ssl3_ctx->md_ctx); |
| 264 | if (in_len < mac_len) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 265 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT); |
David Benjamin | 044abb0 | 2014-12-23 10:57:17 -0500 | [diff] [blame] | 266 | return 0; |
| 267 | } |
| 268 | |
| 269 | if (max_out_len < in_len) { |
| 270 | /* This requires that the caller provide space for the MAC, even though it |
| 271 | * will always be removed on return. */ |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 272 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BUFFER_TOO_SMALL); |
David Benjamin | 044abb0 | 2014-12-23 10:57:17 -0500 | [diff] [blame] | 273 | return 0; |
| 274 | } |
| 275 | |
| 276 | if (nonce_len != 0) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 277 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TOO_LARGE); |
David Benjamin | 044abb0 | 2014-12-23 10:57:17 -0500 | [diff] [blame] | 278 | return 0; |
| 279 | } |
| 280 | |
David Benjamin | 7f1d5d5 | 2015-01-14 17:24:53 -0500 | [diff] [blame] | 281 | if (ad_len != 11 - 2 /* length bytes */) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 282 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_INVALID_AD_SIZE); |
David Benjamin | 044abb0 | 2014-12-23 10:57:17 -0500 | [diff] [blame] | 283 | return 0; |
| 284 | } |
| 285 | |
| 286 | if (in_len > INT_MAX) { |
| 287 | /* EVP_CIPHER takes int as input. */ |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 288 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TOO_LARGE); |
David Benjamin | 044abb0 | 2014-12-23 10:57:17 -0500 | [diff] [blame] | 289 | return 0; |
| 290 | } |
| 291 | |
David Benjamin | 044abb0 | 2014-12-23 10:57:17 -0500 | [diff] [blame] | 292 | /* Decrypt to get the plaintext + MAC + padding. */ |
| 293 | size_t total = 0; |
| 294 | int len; |
| 295 | if (!EVP_DecryptUpdate(&ssl3_ctx->cipher_ctx, out, &len, in, (int)in_len)) { |
| 296 | return 0; |
| 297 | } |
| 298 | total += len; |
| 299 | if (!EVP_DecryptFinal_ex(&ssl3_ctx->cipher_ctx, out + total, &len)) { |
| 300 | return 0; |
| 301 | } |
| 302 | total += len; |
| 303 | assert(total == in_len); |
| 304 | |
David Benjamin | 22edd87 | 2016-08-04 21:38:40 +0000 | [diff] [blame] | 305 | /* Remove CBC padding and MAC. This would normally be timing-sensitive, but |
| 306 | * SSLv3 CBC ciphers are already broken. Support will be removed eventually. |
David Benjamin | 044abb0 | 2014-12-23 10:57:17 -0500 | [diff] [blame] | 307 | * https://www.openssl.org/~bodo/ssl-poodle.pdf */ |
David Benjamin | 22edd87 | 2016-08-04 21:38:40 +0000 | [diff] [blame] | 308 | size_t data_len; |
David Benjamin | 044abb0 | 2014-12-23 10:57:17 -0500 | [diff] [blame] | 309 | if (EVP_CIPHER_CTX_mode(&ssl3_ctx->cipher_ctx) == EVP_CIPH_CBC_MODE) { |
| 310 | unsigned padding_length = out[total - 1]; |
| 311 | if (total < padding_length + 1 + mac_len) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 312 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT); |
David Benjamin | 044abb0 | 2014-12-23 10:57:17 -0500 | [diff] [blame] | 313 | return 0; |
| 314 | } |
| 315 | /* The padding must be minimal. */ |
| 316 | if (padding_length + 1 > EVP_CIPHER_CTX_block_size(&ssl3_ctx->cipher_ctx)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 317 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT); |
David Benjamin | 044abb0 | 2014-12-23 10:57:17 -0500 | [diff] [blame] | 318 | return 0; |
| 319 | } |
| 320 | data_len = total - padding_length - 1 - mac_len; |
| 321 | } else { |
| 322 | data_len = total - mac_len; |
| 323 | } |
| 324 | |
| 325 | /* Compute the MAC and compare against the one in the record. */ |
| 326 | uint8_t mac[EVP_MAX_MD_SIZE]; |
| 327 | if (!ssl3_mac(ssl3_ctx, mac, NULL, ad, ad_len, out, data_len)) { |
| 328 | return 0; |
| 329 | } |
| 330 | if (CRYPTO_memcmp(&out[data_len], mac, mac_len) != 0) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 331 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT); |
David Benjamin | 044abb0 | 2014-12-23 10:57:17 -0500 | [diff] [blame] | 332 | return 0; |
| 333 | } |
| 334 | |
| 335 | *out_len = data_len; |
| 336 | return 1; |
| 337 | } |
| 338 | |
Adam Langley | c2d3280 | 2015-11-03 18:36:10 -0800 | [diff] [blame] | 339 | static int aead_ssl3_get_iv(const EVP_AEAD_CTX *ctx, const uint8_t **out_iv, |
| 340 | size_t *out_iv_len) { |
| 341 | AEAD_SSL3_CTX *ssl3_ctx = (AEAD_SSL3_CTX *)ctx->aead_state; |
| 342 | const size_t iv_len = EVP_CIPHER_CTX_iv_length(&ssl3_ctx->cipher_ctx); |
| 343 | if (iv_len <= 1) { |
| 344 | return 0; |
| 345 | } |
| 346 | |
| 347 | *out_iv = ssl3_ctx->cipher_ctx.iv; |
| 348 | *out_iv_len = iv_len; |
| 349 | return 1; |
| 350 | } |
| 351 | |
David Benjamin | 044abb0 | 2014-12-23 10:57:17 -0500 | [diff] [blame] | 352 | static int aead_aes_128_cbc_sha1_ssl3_init(EVP_AEAD_CTX *ctx, const uint8_t *key, |
David Benjamin | b34f510 | 2015-02-28 03:59:33 -0500 | [diff] [blame] | 353 | size_t key_len, size_t tag_len, |
| 354 | enum evp_aead_direction_t dir) { |
| 355 | return aead_ssl3_init(ctx, key, key_len, tag_len, dir, EVP_aes_128_cbc(), |
David Benjamin | 044abb0 | 2014-12-23 10:57:17 -0500 | [diff] [blame] | 356 | EVP_sha1()); |
| 357 | } |
| 358 | |
| 359 | static int aead_aes_256_cbc_sha1_ssl3_init(EVP_AEAD_CTX *ctx, const uint8_t *key, |
David Benjamin | b34f510 | 2015-02-28 03:59:33 -0500 | [diff] [blame] | 360 | size_t key_len, size_t tag_len, |
| 361 | enum evp_aead_direction_t dir) { |
| 362 | return aead_ssl3_init(ctx, key, key_len, tag_len, dir, EVP_aes_256_cbc(), |
David Benjamin | 044abb0 | 2014-12-23 10:57:17 -0500 | [diff] [blame] | 363 | EVP_sha1()); |
| 364 | } |
| 365 | static int aead_des_ede3_cbc_sha1_ssl3_init(EVP_AEAD_CTX *ctx, |
David Benjamin | b34f510 | 2015-02-28 03:59:33 -0500 | [diff] [blame] | 366 | const uint8_t *key, size_t key_len, |
| 367 | size_t tag_len, |
| 368 | enum evp_aead_direction_t dir) { |
| 369 | return aead_ssl3_init(ctx, key, key_len, tag_len, dir, EVP_des_ede3_cbc(), |
David Benjamin | 044abb0 | 2014-12-23 10:57:17 -0500 | [diff] [blame] | 370 | EVP_sha1()); |
| 371 | } |
| 372 | |
Matt Braithwaite | af09675 | 2015-09-02 19:48:16 -0700 | [diff] [blame] | 373 | static int aead_null_sha1_ssl3_init(EVP_AEAD_CTX *ctx, const uint8_t *key, |
| 374 | size_t key_len, size_t tag_len, |
| 375 | enum evp_aead_direction_t dir) { |
| 376 | return aead_ssl3_init(ctx, key, key_len, tag_len, dir, EVP_enc_null(), |
| 377 | EVP_sha1()); |
| 378 | } |
| 379 | |
David Benjamin | 044abb0 | 2014-12-23 10:57:17 -0500 | [diff] [blame] | 380 | static const EVP_AEAD aead_aes_128_cbc_sha1_ssl3 = { |
| 381 | SHA_DIGEST_LENGTH + 16 + 16, /* key len (SHA1 + AES128 + IV) */ |
| 382 | 0, /* nonce len */ |
| 383 | 16 + SHA_DIGEST_LENGTH, /* overhead (padding + SHA1) */ |
| 384 | SHA_DIGEST_LENGTH, /* max tag length */ |
Martin Kreichgauer | 74bce29 | 2017-06-23 14:49:22 -0700 | [diff] [blame] | 385 | 0, /* seal_scatter_supports_extra_in */ |
| 386 | |
David Benjamin | b34f510 | 2015-02-28 03:59:33 -0500 | [diff] [blame] | 387 | NULL, /* init */ |
David Benjamin | 044abb0 | 2014-12-23 10:57:17 -0500 | [diff] [blame] | 388 | aead_aes_128_cbc_sha1_ssl3_init, |
| 389 | aead_ssl3_cleanup, |
David Benjamin | 044abb0 | 2014-12-23 10:57:17 -0500 | [diff] [blame] | 390 | aead_ssl3_open, |
Martin Kreichgauer | 18d9f28 | 2017-06-06 12:29:48 -0700 | [diff] [blame] | 391 | aead_ssl3_seal_scatter, |
| 392 | NULL, /* open_gather */ |
Adam Langley | c2d3280 | 2015-11-03 18:36:10 -0800 | [diff] [blame] | 393 | aead_ssl3_get_iv, |
Martin Kreichgauer | abbf365 | 2017-07-21 16:27:54 -0700 | [diff] [blame] | 394 | aead_ssl3_tag_len, |
David Benjamin | 044abb0 | 2014-12-23 10:57:17 -0500 | [diff] [blame] | 395 | }; |
| 396 | |
| 397 | static const EVP_AEAD aead_aes_256_cbc_sha1_ssl3 = { |
| 398 | SHA_DIGEST_LENGTH + 32 + 16, /* key len (SHA1 + AES256 + IV) */ |
| 399 | 0, /* nonce len */ |
| 400 | 16 + SHA_DIGEST_LENGTH, /* overhead (padding + SHA1) */ |
| 401 | SHA_DIGEST_LENGTH, /* max tag length */ |
Martin Kreichgauer | 74bce29 | 2017-06-23 14:49:22 -0700 | [diff] [blame] | 402 | 0, /* seal_scatter_supports_extra_in */ |
| 403 | |
David Benjamin | b34f510 | 2015-02-28 03:59:33 -0500 | [diff] [blame] | 404 | NULL, /* init */ |
David Benjamin | 044abb0 | 2014-12-23 10:57:17 -0500 | [diff] [blame] | 405 | aead_aes_256_cbc_sha1_ssl3_init, |
| 406 | aead_ssl3_cleanup, |
David Benjamin | 044abb0 | 2014-12-23 10:57:17 -0500 | [diff] [blame] | 407 | aead_ssl3_open, |
Martin Kreichgauer | 18d9f28 | 2017-06-06 12:29:48 -0700 | [diff] [blame] | 408 | aead_ssl3_seal_scatter, |
| 409 | NULL, /* open_gather */ |
Adam Langley | c2d3280 | 2015-11-03 18:36:10 -0800 | [diff] [blame] | 410 | aead_ssl3_get_iv, |
Martin Kreichgauer | abbf365 | 2017-07-21 16:27:54 -0700 | [diff] [blame] | 411 | aead_ssl3_tag_len, |
David Benjamin | 044abb0 | 2014-12-23 10:57:17 -0500 | [diff] [blame] | 412 | }; |
| 413 | |
| 414 | static const EVP_AEAD aead_des_ede3_cbc_sha1_ssl3 = { |
| 415 | SHA_DIGEST_LENGTH + 24 + 8, /* key len (SHA1 + 3DES + IV) */ |
| 416 | 0, /* nonce len */ |
| 417 | 8 + SHA_DIGEST_LENGTH, /* overhead (padding + SHA1) */ |
| 418 | SHA_DIGEST_LENGTH, /* max tag length */ |
Martin Kreichgauer | 74bce29 | 2017-06-23 14:49:22 -0700 | [diff] [blame] | 419 | 0, /* seal_scatter_supports_extra_in */ |
| 420 | |
David Benjamin | b34f510 | 2015-02-28 03:59:33 -0500 | [diff] [blame] | 421 | NULL, /* init */ |
David Benjamin | 044abb0 | 2014-12-23 10:57:17 -0500 | [diff] [blame] | 422 | aead_des_ede3_cbc_sha1_ssl3_init, |
| 423 | aead_ssl3_cleanup, |
David Benjamin | 044abb0 | 2014-12-23 10:57:17 -0500 | [diff] [blame] | 424 | aead_ssl3_open, |
Martin Kreichgauer | 18d9f28 | 2017-06-06 12:29:48 -0700 | [diff] [blame] | 425 | aead_ssl3_seal_scatter, |
| 426 | NULL, /* open_gather */ |
Adam Langley | c2d3280 | 2015-11-03 18:36:10 -0800 | [diff] [blame] | 427 | aead_ssl3_get_iv, |
Martin Kreichgauer | abbf365 | 2017-07-21 16:27:54 -0700 | [diff] [blame] | 428 | aead_ssl3_tag_len, |
David Benjamin | 044abb0 | 2014-12-23 10:57:17 -0500 | [diff] [blame] | 429 | }; |
| 430 | |
Matt Braithwaite | af09675 | 2015-09-02 19:48:16 -0700 | [diff] [blame] | 431 | static const EVP_AEAD aead_null_sha1_ssl3 = { |
Martin Kreichgauer | 74bce29 | 2017-06-23 14:49:22 -0700 | [diff] [blame] | 432 | SHA_DIGEST_LENGTH, /* key len */ |
| 433 | 0, /* nonce len */ |
| 434 | SHA_DIGEST_LENGTH, /* overhead (SHA1) */ |
| 435 | SHA_DIGEST_LENGTH, /* max tag length */ |
| 436 | 0, /* seal_scatter_supports_extra_in */ |
| 437 | |
| 438 | NULL, /* init */ |
Matt Braithwaite | af09675 | 2015-09-02 19:48:16 -0700 | [diff] [blame] | 439 | aead_null_sha1_ssl3_init, |
| 440 | aead_ssl3_cleanup, |
Matt Braithwaite | af09675 | 2015-09-02 19:48:16 -0700 | [diff] [blame] | 441 | aead_ssl3_open, |
Martin Kreichgauer | 18d9f28 | 2017-06-06 12:29:48 -0700 | [diff] [blame] | 442 | aead_ssl3_seal_scatter, |
| 443 | NULL, /* open_gather */ |
| 444 | NULL, /* get_iv */ |
Martin Kreichgauer | abbf365 | 2017-07-21 16:27:54 -0700 | [diff] [blame] | 445 | aead_ssl3_tag_len, |
Matt Braithwaite | af09675 | 2015-09-02 19:48:16 -0700 | [diff] [blame] | 446 | }; |
| 447 | |
David Benjamin | 044abb0 | 2014-12-23 10:57:17 -0500 | [diff] [blame] | 448 | const EVP_AEAD *EVP_aead_aes_128_cbc_sha1_ssl3(void) { |
| 449 | return &aead_aes_128_cbc_sha1_ssl3; |
| 450 | } |
| 451 | |
| 452 | const EVP_AEAD *EVP_aead_aes_256_cbc_sha1_ssl3(void) { |
| 453 | return &aead_aes_256_cbc_sha1_ssl3; |
| 454 | } |
| 455 | |
| 456 | const EVP_AEAD *EVP_aead_des_ede3_cbc_sha1_ssl3(void) { |
| 457 | return &aead_des_ede3_cbc_sha1_ssl3; |
| 458 | } |
Matt Braithwaite | af09675 | 2015-09-02 19:48:16 -0700 | [diff] [blame] | 459 | |
| 460 | const EVP_AEAD *EVP_aead_null_sha1_ssl3(void) { return &aead_null_sha1_ssl3; } |