2014-12-22 02:27:41 +00:00
|
|
|
/* Copyright (c) 2014, Google Inc.
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
|
|
|
|
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
|
|
|
|
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
|
|
|
|
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <openssl/aead.h>
|
|
|
|
#include <openssl/cipher.h>
|
|
|
|
#include <openssl/err.h>
|
|
|
|
#include <openssl/hmac.h>
|
2015-12-08 00:34:31 +00:00
|
|
|
#include <openssl/md5.h>
|
2014-12-22 02:27:41 +00:00
|
|
|
#include <openssl/mem.h>
|
|
|
|
#include <openssl/sha.h>
|
2015-02-28 08:59:33 +00:00
|
|
|
#include <openssl/type_check.h>
|
2014-12-22 02:27:41 +00:00
|
|
|
|
2017-06-06 20:29:48 +01:00
|
|
|
#include "../fipsmodule/cipher/internal.h"
|
2016-08-17 21:56:14 +01:00
|
|
|
#include "../internal.h"
|
2014-12-22 02:27:41 +00:00
|
|
|
#include "internal.h"
|
|
|
|
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
EVP_CIPHER_CTX cipher_ctx;
|
|
|
|
HMAC_CTX hmac_ctx;
|
|
|
|
/* mac_key is the portion of the key used for the MAC. It is retained
|
|
|
|
* separately for the constant-time CBC code. */
|
|
|
|
uint8_t mac_key[EVP_MAX_MD_SIZE];
|
|
|
|
uint8_t mac_key_len;
|
|
|
|
/* implicit_iv is one iff this is a pre-TLS-1.1 CBC cipher without an explicit
|
|
|
|
* IV. */
|
|
|
|
char implicit_iv;
|
|
|
|
} AEAD_TLS_CTX;
|
|
|
|
|
2015-02-28 08:59:33 +00:00
|
|
|
OPENSSL_COMPILE_ASSERT(EVP_MAX_MD_SIZE < 256, mac_key_len_fits_in_uint8_t);
|
2014-12-22 02:27:41 +00:00
|
|
|
|
|
|
|
static void aead_tls_cleanup(EVP_AEAD_CTX *ctx) {
|
|
|
|
AEAD_TLS_CTX *tls_ctx = (AEAD_TLS_CTX *)ctx->aead_state;
|
|
|
|
EVP_CIPHER_CTX_cleanup(&tls_ctx->cipher_ctx);
|
|
|
|
HMAC_CTX_cleanup(&tls_ctx->hmac_ctx);
|
|
|
|
OPENSSL_cleanse(&tls_ctx->mac_key, sizeof(tls_ctx->mac_key));
|
|
|
|
OPENSSL_free(tls_ctx);
|
|
|
|
ctx->aead_state = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int aead_tls_init(EVP_AEAD_CTX *ctx, const uint8_t *key, size_t key_len,
|
2015-02-28 08:59:33 +00:00
|
|
|
size_t tag_len, enum evp_aead_direction_t dir,
|
2015-03-07 08:10:27 +00:00
|
|
|
const EVP_CIPHER *cipher, const EVP_MD *md,
|
|
|
|
char implicit_iv) {
|
2014-12-22 02:27:41 +00:00
|
|
|
if (tag_len != EVP_AEAD_DEFAULT_TAG_LENGTH &&
|
|
|
|
tag_len != EVP_MD_size(md)) {
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_UNSUPPORTED_TAG_SIZE);
|
2014-12-22 02:27:41 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (key_len != EVP_AEAD_key_length(ctx->aead)) {
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_KEY_LENGTH);
|
2014-12-22 02:27:41 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t mac_key_len = EVP_MD_size(md);
|
|
|
|
size_t enc_key_len = EVP_CIPHER_key_length(cipher);
|
2015-02-28 08:59:33 +00:00
|
|
|
assert(mac_key_len + enc_key_len +
|
|
|
|
(implicit_iv ? EVP_CIPHER_iv_length(cipher) : 0) == key_len);
|
2014-12-22 02:27:41 +00:00
|
|
|
|
|
|
|
AEAD_TLS_CTX *tls_ctx = OPENSSL_malloc(sizeof(AEAD_TLS_CTX));
|
|
|
|
if (tls_ctx == NULL) {
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(CIPHER, ERR_R_MALLOC_FAILURE);
|
2014-12-22 02:27:41 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
EVP_CIPHER_CTX_init(&tls_ctx->cipher_ctx);
|
|
|
|
HMAC_CTX_init(&tls_ctx->hmac_ctx);
|
2015-02-28 08:59:33 +00:00
|
|
|
assert(mac_key_len <= EVP_MAX_MD_SIZE);
|
2016-12-13 06:07:13 +00:00
|
|
|
OPENSSL_memcpy(tls_ctx->mac_key, key, mac_key_len);
|
2014-12-22 02:27:41 +00:00
|
|
|
tls_ctx->mac_key_len = (uint8_t)mac_key_len;
|
|
|
|
tls_ctx->implicit_iv = implicit_iv;
|
|
|
|
|
|
|
|
ctx->aead_state = tls_ctx;
|
2015-02-28 08:59:33 +00:00
|
|
|
if (!EVP_CipherInit_ex(&tls_ctx->cipher_ctx, cipher, NULL, &key[mac_key_len],
|
|
|
|
implicit_iv ? &key[mac_key_len + enc_key_len] : NULL,
|
|
|
|
dir == evp_aead_seal) ||
|
2014-12-22 02:27:41 +00:00
|
|
|
!HMAC_Init_ex(&tls_ctx->hmac_ctx, key, mac_key_len, md, NULL)) {
|
|
|
|
aead_tls_cleanup(ctx);
|
2015-05-11 22:28:12 +01:00
|
|
|
ctx->aead_state = NULL;
|
2014-12-22 02:27:41 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
EVP_CIPHER_CTX_set_padding(&tls_ctx->cipher_ctx, 0);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2017-06-06 20:29:48 +01:00
|
|
|
static int aead_tls_seal_scatter(const EVP_AEAD_CTX *ctx, uint8_t *out,
|
|
|
|
uint8_t *out_tag, size_t *out_tag_len,
|
|
|
|
size_t max_out_tag_len, const uint8_t *nonce,
|
|
|
|
size_t nonce_len, const uint8_t *in,
|
|
|
|
size_t in_len, const uint8_t *ad,
|
|
|
|
size_t ad_len) {
|
2014-12-22 02:27:41 +00:00
|
|
|
AEAD_TLS_CTX *tls_ctx = (AEAD_TLS_CTX *)ctx->aead_state;
|
|
|
|
|
2015-02-28 08:59:33 +00:00
|
|
|
if (!tls_ctx->cipher_ctx.encrypt) {
|
|
|
|
/* Unlike a normal AEAD, a TLS AEAD may only be used in one direction. */
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_INVALID_OPERATION);
|
2015-02-28 08:59:33 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-06-06 20:29:48 +01:00
|
|
|
if (in_len > INT_MAX) {
|
2014-12-22 02:27:41 +00:00
|
|
|
/* EVP_CIPHER takes int as input. */
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TOO_LARGE);
|
2014-12-22 02:27:41 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-06-06 20:29:48 +01:00
|
|
|
const size_t max_overhead = EVP_AEAD_max_overhead(ctx->aead);
|
|
|
|
if (max_out_tag_len < max_overhead) {
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BUFFER_TOO_SMALL);
|
2014-12-22 02:27:41 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nonce_len != EVP_AEAD_nonce_length(ctx->aead)) {
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_INVALID_NONCE_SIZE);
|
2014-12-22 02:27:41 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-01-14 22:24:53 +00:00
|
|
|
if (ad_len != 13 - 2 /* length bytes */) {
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_INVALID_AD_SIZE);
|
2014-12-22 02:27:41 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* To allow for CBC mode which changes cipher length, |ad| doesn't include the
|
|
|
|
* length for legacy ciphers. */
|
|
|
|
uint8_t ad_extra[2];
|
|
|
|
ad_extra[0] = (uint8_t)(in_len >> 8);
|
|
|
|
ad_extra[1] = (uint8_t)(in_len & 0xff);
|
|
|
|
|
|
|
|
/* Compute the MAC. This must be first in case the operation is being done
|
|
|
|
* in-place. */
|
|
|
|
uint8_t mac[EVP_MAX_MD_SIZE];
|
|
|
|
unsigned mac_len;
|
2015-12-08 00:52:56 +00:00
|
|
|
if (!HMAC_Init_ex(&tls_ctx->hmac_ctx, NULL, 0, NULL, NULL) ||
|
|
|
|
!HMAC_Update(&tls_ctx->hmac_ctx, ad, ad_len) ||
|
|
|
|
!HMAC_Update(&tls_ctx->hmac_ctx, ad_extra, sizeof(ad_extra)) ||
|
|
|
|
!HMAC_Update(&tls_ctx->hmac_ctx, in, in_len) ||
|
|
|
|
!HMAC_Final(&tls_ctx->hmac_ctx, mac, &mac_len)) {
|
2014-12-22 02:27:41 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Configure the explicit IV. */
|
|
|
|
if (EVP_CIPHER_CTX_mode(&tls_ctx->cipher_ctx) == EVP_CIPH_CBC_MODE &&
|
|
|
|
!tls_ctx->implicit_iv &&
|
|
|
|
!EVP_EncryptInit_ex(&tls_ctx->cipher_ctx, NULL, NULL, NULL, nonce)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Encrypt the input. */
|
|
|
|
int len;
|
2017-06-06 20:29:48 +01:00
|
|
|
if (!EVP_EncryptUpdate(&tls_ctx->cipher_ctx, out, &len, in, (int)in_len)) {
|
2014-12-22 02:27:41 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-06-06 20:29:48 +01:00
|
|
|
unsigned block_size = EVP_CIPHER_CTX_block_size(&tls_ctx->cipher_ctx);
|
|
|
|
|
|
|
|
/* Feed the MAC into the cipher in two steps. First complete the final partial
|
|
|
|
* block from encrypting the input and split the result between |out| and
|
|
|
|
* |out_tag|. Then feed the rest. */
|
|
|
|
|
|
|
|
size_t early_mac_len = (block_size - (in_len % block_size)) % block_size;
|
|
|
|
if (early_mac_len != 0) {
|
|
|
|
assert(len + block_size - early_mac_len == in_len);
|
|
|
|
uint8_t buf[EVP_MAX_BLOCK_LENGTH];
|
|
|
|
int buf_len;
|
|
|
|
if (!EVP_EncryptUpdate(&tls_ctx->cipher_ctx, buf, &buf_len, mac,
|
|
|
|
(int)early_mac_len)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
assert(buf_len == (int)block_size);
|
|
|
|
OPENSSL_memcpy(out + len, buf, block_size - early_mac_len);
|
|
|
|
OPENSSL_memcpy(out_tag, buf + block_size - early_mac_len, early_mac_len);
|
|
|
|
}
|
|
|
|
size_t tag_len = early_mac_len;
|
|
|
|
|
|
|
|
if (!EVP_EncryptUpdate(&tls_ctx->cipher_ctx, out_tag + tag_len, &len,
|
|
|
|
mac + tag_len, mac_len - tag_len)) {
|
2014-12-22 02:27:41 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2017-06-06 20:29:48 +01:00
|
|
|
tag_len += len;
|
2014-12-22 02:27:41 +00:00
|
|
|
|
|
|
|
if (block_size > 1) {
|
|
|
|
assert(block_size <= 256);
|
|
|
|
assert(EVP_CIPHER_CTX_mode(&tls_ctx->cipher_ctx) == EVP_CIPH_CBC_MODE);
|
|
|
|
|
|
|
|
/* Compute padding and feed that into the cipher. */
|
|
|
|
uint8_t padding[256];
|
|
|
|
unsigned padding_len = block_size - ((in_len + mac_len) % block_size);
|
2016-12-13 06:07:13 +00:00
|
|
|
OPENSSL_memset(padding, padding_len - 1, padding_len);
|
2017-06-06 20:29:48 +01:00
|
|
|
if (!EVP_EncryptUpdate(&tls_ctx->cipher_ctx, out_tag + tag_len, &len,
|
|
|
|
padding, (int)padding_len)) {
|
2014-12-22 02:27:41 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2017-06-06 20:29:48 +01:00
|
|
|
tag_len += len;
|
2014-12-22 02:27:41 +00:00
|
|
|
}
|
|
|
|
|
2017-06-06 20:29:48 +01:00
|
|
|
if (!EVP_EncryptFinal_ex(&tls_ctx->cipher_ctx, out_tag + tag_len, &len)) {
|
2014-12-22 02:27:41 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2017-06-06 20:29:48 +01:00
|
|
|
tag_len += len;
|
|
|
|
assert(tag_len <= max_overhead);
|
2014-12-22 02:27:41 +00:00
|
|
|
|
2017-06-06 20:29:48 +01:00
|
|
|
*out_tag_len = tag_len;
|
2014-12-22 02:27:41 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2017-06-06 20:29:48 +01:00
|
|
|
static int aead_tls_open(const EVP_AEAD_CTX *ctx, uint8_t *out, size_t *out_len,
|
|
|
|
size_t max_out_len, const uint8_t *nonce,
|
|
|
|
size_t nonce_len, const uint8_t *in, size_t in_len,
|
2014-12-22 02:27:41 +00:00
|
|
|
const uint8_t *ad, size_t ad_len) {
|
|
|
|
AEAD_TLS_CTX *tls_ctx = (AEAD_TLS_CTX *)ctx->aead_state;
|
|
|
|
|
2015-02-28 08:59:33 +00:00
|
|
|
if (tls_ctx->cipher_ctx.encrypt) {
|
|
|
|
/* Unlike a normal AEAD, a TLS AEAD may only be used in one direction. */
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_INVALID_OPERATION);
|
2015-02-28 08:59:33 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-12-22 02:27:41 +00:00
|
|
|
if (in_len < HMAC_size(&tls_ctx->hmac_ctx)) {
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT);
|
2014-12-22 02:27:41 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (max_out_len < in_len) {
|
|
|
|
/* This requires that the caller provide space for the MAC, even though it
|
|
|
|
* will always be removed on return. */
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BUFFER_TOO_SMALL);
|
2014-12-22 02:27:41 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nonce_len != EVP_AEAD_nonce_length(ctx->aead)) {
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_INVALID_NONCE_SIZE);
|
2014-12-22 02:27:41 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-01-14 22:24:53 +00:00
|
|
|
if (ad_len != 13 - 2 /* length bytes */) {
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_INVALID_AD_SIZE);
|
2014-12-22 02:27:41 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (in_len > INT_MAX) {
|
|
|
|
/* EVP_CIPHER takes int as input. */
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TOO_LARGE);
|
2014-12-22 02:27:41 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Configure the explicit IV. */
|
|
|
|
if (EVP_CIPHER_CTX_mode(&tls_ctx->cipher_ctx) == EVP_CIPH_CBC_MODE &&
|
|
|
|
!tls_ctx->implicit_iv &&
|
|
|
|
!EVP_DecryptInit_ex(&tls_ctx->cipher_ctx, NULL, NULL, NULL, nonce)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Decrypt to get the plaintext + MAC + padding. */
|
|
|
|
size_t total = 0;
|
|
|
|
int len;
|
|
|
|
if (!EVP_DecryptUpdate(&tls_ctx->cipher_ctx, out, &len, in, (int)in_len)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
total += len;
|
|
|
|
if (!EVP_DecryptFinal_ex(&tls_ctx->cipher_ctx, out + total, &len)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
total += len;
|
|
|
|
assert(total == in_len);
|
|
|
|
|
|
|
|
/* Remove CBC padding. Code from here on is timing-sensitive with respect to
|
|
|
|
* |padding_ok| and |data_plus_mac_len| for CBC ciphers. */
|
2017-04-20 21:51:11 +01:00
|
|
|
size_t data_plus_mac_len;
|
|
|
|
crypto_word_t padding_ok;
|
2014-12-22 02:27:41 +00:00
|
|
|
if (EVP_CIPHER_CTX_mode(&tls_ctx->cipher_ctx) == EVP_CIPH_CBC_MODE) {
|
2016-08-10 04:36:43 +01:00
|
|
|
if (!EVP_tls_cbc_remove_padding(
|
|
|
|
&padding_ok, &data_plus_mac_len, out, total,
|
|
|
|
EVP_CIPHER_CTX_block_size(&tls_ctx->cipher_ctx),
|
2017-03-16 17:46:54 +00:00
|
|
|
HMAC_size(&tls_ctx->hmac_ctx))) {
|
2016-08-10 04:36:43 +01:00
|
|
|
/* Publicly invalid. This can be rejected in non-constant time. */
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT);
|
2014-12-22 02:27:41 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} else {
|
2017-04-20 21:51:11 +01:00
|
|
|
padding_ok = CONSTTIME_TRUE_W;
|
2014-12-22 02:27:41 +00:00
|
|
|
data_plus_mac_len = total;
|
|
|
|
/* |data_plus_mac_len| = |total| = |in_len| at this point. |in_len| has
|
|
|
|
* already been checked against the MAC size at the top of the function. */
|
|
|
|
assert(data_plus_mac_len >= HMAC_size(&tls_ctx->hmac_ctx));
|
|
|
|
}
|
2017-03-16 17:46:54 +00:00
|
|
|
size_t data_len = data_plus_mac_len - HMAC_size(&tls_ctx->hmac_ctx);
|
2014-12-22 02:27:41 +00:00
|
|
|
|
2016-08-10 04:36:43 +01:00
|
|
|
/* At this point, if the padding is valid, the first |data_plus_mac_len| bytes
|
|
|
|
* after |out| are the plaintext and MAC. Otherwise, |data_plus_mac_len| is
|
|
|
|
* still large enough to extract a MAC, but it will be irrelevant. */
|
2014-12-22 02:27:41 +00:00
|
|
|
|
|
|
|
/* To allow for CBC mode which changes cipher length, |ad| doesn't include the
|
|
|
|
* length for legacy ciphers. */
|
|
|
|
uint8_t ad_fixed[13];
|
2016-12-13 06:07:13 +00:00
|
|
|
OPENSSL_memcpy(ad_fixed, ad, 11);
|
2014-12-22 02:27:41 +00:00
|
|
|
ad_fixed[11] = (uint8_t)(data_len >> 8);
|
|
|
|
ad_fixed[12] = (uint8_t)(data_len & 0xff);
|
|
|
|
ad_len += 2;
|
|
|
|
|
|
|
|
/* Compute the MAC and extract the one in the record. */
|
|
|
|
uint8_t mac[EVP_MAX_MD_SIZE];
|
|
|
|
size_t mac_len;
|
|
|
|
uint8_t record_mac_tmp[EVP_MAX_MD_SIZE];
|
|
|
|
uint8_t *record_mac;
|
|
|
|
if (EVP_CIPHER_CTX_mode(&tls_ctx->cipher_ctx) == EVP_CIPH_CBC_MODE &&
|
2014-12-22 12:23:54 +00:00
|
|
|
EVP_tls_cbc_record_digest_supported(tls_ctx->hmac_ctx.md)) {
|
|
|
|
if (!EVP_tls_cbc_digest_record(tls_ctx->hmac_ctx.md, mac, &mac_len,
|
|
|
|
ad_fixed, out, data_plus_mac_len, total,
|
|
|
|
tls_ctx->mac_key, tls_ctx->mac_key_len)) {
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT);
|
2014-12-22 02:27:41 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
assert(mac_len == HMAC_size(&tls_ctx->hmac_ctx));
|
|
|
|
|
|
|
|
record_mac = record_mac_tmp;
|
2014-12-22 12:23:54 +00:00
|
|
|
EVP_tls_cbc_copy_mac(record_mac, mac_len, out, data_plus_mac_len, total);
|
2014-12-22 02:27:41 +00:00
|
|
|
} else {
|
|
|
|
/* We should support the constant-time path for all CBC-mode ciphers
|
|
|
|
* implemented. */
|
|
|
|
assert(EVP_CIPHER_CTX_mode(&tls_ctx->cipher_ctx) != EVP_CIPH_CBC_MODE);
|
|
|
|
|
|
|
|
unsigned mac_len_u;
|
2015-12-08 00:52:56 +00:00
|
|
|
if (!HMAC_Init_ex(&tls_ctx->hmac_ctx, NULL, 0, NULL, NULL) ||
|
|
|
|
!HMAC_Update(&tls_ctx->hmac_ctx, ad_fixed, ad_len) ||
|
|
|
|
!HMAC_Update(&tls_ctx->hmac_ctx, out, data_len) ||
|
|
|
|
!HMAC_Final(&tls_ctx->hmac_ctx, mac, &mac_len_u)) {
|
2014-12-22 02:27:41 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
mac_len = mac_len_u;
|
|
|
|
|
|
|
|
assert(mac_len == HMAC_size(&tls_ctx->hmac_ctx));
|
|
|
|
record_mac = &out[data_len];
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Perform the MAC check and the padding check in constant-time. It should be
|
|
|
|
* safe to simply perform the padding check first, but it would not be under a
|
|
|
|
* different choice of MAC location on padding failure. See
|
2014-12-22 12:23:54 +00:00
|
|
|
* EVP_tls_cbc_remove_padding. */
|
2017-04-20 21:51:11 +01:00
|
|
|
crypto_word_t good =
|
2017-03-16 17:46:54 +00:00
|
|
|
constant_time_eq_int(CRYPTO_memcmp(record_mac, mac, mac_len), 0);
|
2016-08-10 04:36:43 +01:00
|
|
|
good &= padding_ok;
|
2014-12-22 02:27:41 +00:00
|
|
|
if (!good) {
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT);
|
2014-12-22 02:27:41 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* End of timing-sensitive code. */
|
|
|
|
|
|
|
|
*out_len = data_len;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int aead_aes_128_cbc_sha1_tls_init(EVP_AEAD_CTX *ctx, const uint8_t *key,
|
2015-02-28 08:59:33 +00:00
|
|
|
size_t key_len, size_t tag_len,
|
|
|
|
enum evp_aead_direction_t dir) {
|
|
|
|
return aead_tls_init(ctx, key, key_len, tag_len, dir, EVP_aes_128_cbc(),
|
2014-12-22 02:27:41 +00:00
|
|
|
EVP_sha1(), 0);
|
|
|
|
}
|
|
|
|
|
2015-02-28 08:59:33 +00:00
|
|
|
static int aead_aes_128_cbc_sha1_tls_implicit_iv_init(
|
|
|
|
EVP_AEAD_CTX *ctx, const uint8_t *key, size_t key_len, size_t tag_len,
|
|
|
|
enum evp_aead_direction_t dir) {
|
|
|
|
return aead_tls_init(ctx, key, key_len, tag_len, dir, EVP_aes_128_cbc(),
|
2014-12-22 02:27:41 +00:00
|
|
|
EVP_sha1(), 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int aead_aes_128_cbc_sha256_tls_init(EVP_AEAD_CTX *ctx,
|
|
|
|
const uint8_t *key, size_t key_len,
|
2015-02-28 08:59:33 +00:00
|
|
|
size_t tag_len,
|
|
|
|
enum evp_aead_direction_t dir) {
|
|
|
|
return aead_tls_init(ctx, key, key_len, tag_len, dir, EVP_aes_128_cbc(),
|
2014-12-22 02:27:41 +00:00
|
|
|
EVP_sha256(), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int aead_aes_256_cbc_sha1_tls_init(EVP_AEAD_CTX *ctx, const uint8_t *key,
|
2015-02-28 08:59:33 +00:00
|
|
|
size_t key_len, size_t tag_len,
|
|
|
|
enum evp_aead_direction_t dir) {
|
|
|
|
return aead_tls_init(ctx, key, key_len, tag_len, dir, EVP_aes_256_cbc(),
|
2014-12-22 02:27:41 +00:00
|
|
|
EVP_sha1(), 0);
|
|
|
|
}
|
|
|
|
|
2015-02-28 08:59:33 +00:00
|
|
|
static int aead_aes_256_cbc_sha1_tls_implicit_iv_init(
|
|
|
|
EVP_AEAD_CTX *ctx, const uint8_t *key, size_t key_len, size_t tag_len,
|
|
|
|
enum evp_aead_direction_t dir) {
|
|
|
|
return aead_tls_init(ctx, key, key_len, tag_len, dir, EVP_aes_256_cbc(),
|
2014-12-22 02:27:41 +00:00
|
|
|
EVP_sha1(), 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int aead_aes_256_cbc_sha256_tls_init(EVP_AEAD_CTX *ctx,
|
|
|
|
const uint8_t *key, size_t key_len,
|
2015-02-28 08:59:33 +00:00
|
|
|
size_t tag_len,
|
|
|
|
enum evp_aead_direction_t dir) {
|
|
|
|
return aead_tls_init(ctx, key, key_len, tag_len, dir, EVP_aes_256_cbc(),
|
2014-12-22 02:27:41 +00:00
|
|
|
EVP_sha256(), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int aead_aes_256_cbc_sha384_tls_init(EVP_AEAD_CTX *ctx,
|
|
|
|
const uint8_t *key, size_t key_len,
|
2015-02-28 08:59:33 +00:00
|
|
|
size_t tag_len,
|
|
|
|
enum evp_aead_direction_t dir) {
|
|
|
|
return aead_tls_init(ctx, key, key_len, tag_len, dir, EVP_aes_256_cbc(),
|
2014-12-22 02:27:41 +00:00
|
|
|
EVP_sha384(), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int aead_des_ede3_cbc_sha1_tls_init(EVP_AEAD_CTX *ctx,
|
|
|
|
const uint8_t *key, size_t key_len,
|
2015-02-28 08:59:33 +00:00
|
|
|
size_t tag_len,
|
|
|
|
enum evp_aead_direction_t dir) {
|
|
|
|
return aead_tls_init(ctx, key, key_len, tag_len, dir, EVP_des_ede3_cbc(),
|
2014-12-22 02:27:41 +00:00
|
|
|
EVP_sha1(), 0);
|
|
|
|
}
|
|
|
|
|
2015-02-28 08:59:33 +00:00
|
|
|
static int aead_des_ede3_cbc_sha1_tls_implicit_iv_init(
|
|
|
|
EVP_AEAD_CTX *ctx, const uint8_t *key, size_t key_len, size_t tag_len,
|
|
|
|
enum evp_aead_direction_t dir) {
|
|
|
|
return aead_tls_init(ctx, key, key_len, tag_len, dir, EVP_des_ede3_cbc(),
|
2014-12-22 02:27:41 +00:00
|
|
|
EVP_sha1(), 1);
|
|
|
|
}
|
|
|
|
|
2015-11-04 02:36:10 +00:00
|
|
|
static int aead_tls_get_iv(const EVP_AEAD_CTX *ctx, const uint8_t **out_iv,
|
|
|
|
size_t *out_iv_len) {
|
|
|
|
const AEAD_TLS_CTX *tls_ctx = (AEAD_TLS_CTX*) ctx->aead_state;
|
|
|
|
const size_t iv_len = EVP_CIPHER_CTX_iv_length(&tls_ctx->cipher_ctx);
|
|
|
|
if (iv_len <= 1) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
*out_iv = tls_ctx->cipher_ctx.iv;
|
|
|
|
*out_iv_len = iv_len;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2015-09-03 03:48:16 +01:00
|
|
|
static int aead_null_sha1_tls_init(EVP_AEAD_CTX *ctx, const uint8_t *key,
|
|
|
|
size_t key_len, size_t tag_len,
|
|
|
|
enum evp_aead_direction_t dir) {
|
|
|
|
return aead_tls_init(ctx, key, key_len, tag_len, dir, EVP_enc_null(),
|
|
|
|
EVP_sha1(), 1 /* implicit iv */);
|
|
|
|
}
|
|
|
|
|
2014-12-22 02:27:41 +00:00
|
|
|
static const EVP_AEAD aead_aes_128_cbc_sha1_tls = {
|
|
|
|
SHA_DIGEST_LENGTH + 16, /* key len (SHA1 + AES128) */
|
|
|
|
16, /* nonce len (IV) */
|
|
|
|
16 + SHA_DIGEST_LENGTH, /* overhead (padding + SHA1) */
|
|
|
|
SHA_DIGEST_LENGTH, /* max tag length */
|
2017-06-06 20:29:48 +01:00
|
|
|
NULL, /* init */
|
2014-12-22 02:27:41 +00:00
|
|
|
aead_aes_128_cbc_sha1_tls_init,
|
|
|
|
aead_tls_cleanup,
|
|
|
|
aead_tls_open,
|
2017-06-06 20:29:48 +01:00
|
|
|
aead_tls_seal_scatter,
|
|
|
|
NULL, /* open_gather */
|
|
|
|
NULL, /* get_iv */
|
2014-12-22 02:27:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static const EVP_AEAD aead_aes_128_cbc_sha1_tls_implicit_iv = {
|
|
|
|
SHA_DIGEST_LENGTH + 16 + 16, /* key len (SHA1 + AES128 + IV) */
|
|
|
|
0, /* nonce len */
|
|
|
|
16 + SHA_DIGEST_LENGTH, /* overhead (padding + SHA1) */
|
|
|
|
SHA_DIGEST_LENGTH, /* max tag length */
|
2017-06-06 20:29:48 +01:00
|
|
|
NULL, /* init */
|
2014-12-22 02:27:41 +00:00
|
|
|
aead_aes_128_cbc_sha1_tls_implicit_iv_init,
|
|
|
|
aead_tls_cleanup,
|
|
|
|
aead_tls_open,
|
2017-06-06 20:29:48 +01:00
|
|
|
aead_tls_seal_scatter,
|
|
|
|
NULL, /* open_gather */
|
|
|
|
aead_tls_get_iv, /* get_iv */
|
2014-12-22 02:27:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static const EVP_AEAD aead_aes_128_cbc_sha256_tls = {
|
|
|
|
SHA256_DIGEST_LENGTH + 16, /* key len (SHA256 + AES128) */
|
|
|
|
16, /* nonce len (IV) */
|
|
|
|
16 + SHA256_DIGEST_LENGTH, /* overhead (padding + SHA256) */
|
2015-10-01 00:37:56 +01:00
|
|
|
SHA256_DIGEST_LENGTH, /* max tag length */
|
2017-06-06 20:29:48 +01:00
|
|
|
NULL, /* init */
|
2014-12-22 02:27:41 +00:00
|
|
|
aead_aes_128_cbc_sha256_tls_init,
|
|
|
|
aead_tls_cleanup,
|
|
|
|
aead_tls_open,
|
2017-06-06 20:29:48 +01:00
|
|
|
aead_tls_seal_scatter,
|
|
|
|
NULL, /* open_gather */
|
|
|
|
NULL, /* get_iv */
|
2014-12-22 02:27:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static const EVP_AEAD aead_aes_256_cbc_sha1_tls = {
|
|
|
|
SHA_DIGEST_LENGTH + 32, /* key len (SHA1 + AES256) */
|
|
|
|
16, /* nonce len (IV) */
|
|
|
|
16 + SHA_DIGEST_LENGTH, /* overhead (padding + SHA1) */
|
|
|
|
SHA_DIGEST_LENGTH, /* max tag length */
|
2017-06-06 20:29:48 +01:00
|
|
|
NULL, /* init */
|
2014-12-22 02:27:41 +00:00
|
|
|
aead_aes_256_cbc_sha1_tls_init,
|
|
|
|
aead_tls_cleanup,
|
|
|
|
aead_tls_open,
|
2017-06-06 20:29:48 +01:00
|
|
|
aead_tls_seal_scatter,
|
|
|
|
NULL, /* open_gather */
|
|
|
|
NULL, /* get_iv */
|
2014-12-22 02:27:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static const EVP_AEAD aead_aes_256_cbc_sha1_tls_implicit_iv = {
|
|
|
|
SHA_DIGEST_LENGTH + 32 + 16, /* key len (SHA1 + AES256 + IV) */
|
|
|
|
0, /* nonce len */
|
|
|
|
16 + SHA_DIGEST_LENGTH, /* overhead (padding + SHA1) */
|
|
|
|
SHA_DIGEST_LENGTH, /* max tag length */
|
2017-06-06 20:29:48 +01:00
|
|
|
NULL, /* init */
|
2014-12-22 02:27:41 +00:00
|
|
|
aead_aes_256_cbc_sha1_tls_implicit_iv_init,
|
|
|
|
aead_tls_cleanup,
|
|
|
|
aead_tls_open,
|
2017-06-06 20:29:48 +01:00
|
|
|
aead_tls_seal_scatter,
|
|
|
|
NULL, /* open_gather */
|
|
|
|
aead_tls_get_iv, /* get_iv */
|
2014-12-22 02:27:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static const EVP_AEAD aead_aes_256_cbc_sha256_tls = {
|
|
|
|
SHA256_DIGEST_LENGTH + 32, /* key len (SHA256 + AES256) */
|
|
|
|
16, /* nonce len (IV) */
|
|
|
|
16 + SHA256_DIGEST_LENGTH, /* overhead (padding + SHA256) */
|
2015-10-01 00:37:56 +01:00
|
|
|
SHA256_DIGEST_LENGTH, /* max tag length */
|
2017-06-06 20:29:48 +01:00
|
|
|
NULL, /* init */
|
2014-12-22 02:27:41 +00:00
|
|
|
aead_aes_256_cbc_sha256_tls_init,
|
|
|
|
aead_tls_cleanup,
|
|
|
|
aead_tls_open,
|
2017-06-06 20:29:48 +01:00
|
|
|
aead_tls_seal_scatter,
|
|
|
|
NULL, /* open_gather */
|
|
|
|
NULL, /* get_iv */
|
2014-12-22 02:27:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static const EVP_AEAD aead_aes_256_cbc_sha384_tls = {
|
|
|
|
SHA384_DIGEST_LENGTH + 32, /* key len (SHA384 + AES256) */
|
|
|
|
16, /* nonce len (IV) */
|
|
|
|
16 + SHA384_DIGEST_LENGTH, /* overhead (padding + SHA384) */
|
2015-10-01 00:37:56 +01:00
|
|
|
SHA384_DIGEST_LENGTH, /* max tag length */
|
2017-06-06 20:29:48 +01:00
|
|
|
NULL, /* init */
|
2014-12-22 02:27:41 +00:00
|
|
|
aead_aes_256_cbc_sha384_tls_init,
|
|
|
|
aead_tls_cleanup,
|
|
|
|
aead_tls_open,
|
2017-06-06 20:29:48 +01:00
|
|
|
aead_tls_seal_scatter,
|
|
|
|
NULL, /* open_gather */
|
|
|
|
NULL, /* get_iv */
|
2014-12-22 02:27:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static const EVP_AEAD aead_des_ede3_cbc_sha1_tls = {
|
|
|
|
SHA_DIGEST_LENGTH + 24, /* key len (SHA1 + 3DES) */
|
|
|
|
8, /* nonce len (IV) */
|
|
|
|
8 + SHA_DIGEST_LENGTH, /* overhead (padding + SHA1) */
|
|
|
|
SHA_DIGEST_LENGTH, /* max tag length */
|
2017-06-06 20:29:48 +01:00
|
|
|
NULL, /* init */
|
2014-12-22 02:27:41 +00:00
|
|
|
aead_des_ede3_cbc_sha1_tls_init,
|
|
|
|
aead_tls_cleanup,
|
|
|
|
aead_tls_open,
|
2017-06-06 20:29:48 +01:00
|
|
|
aead_tls_seal_scatter,
|
|
|
|
NULL, /* open_gather */
|
|
|
|
NULL, /* get_iv */
|
2014-12-22 02:27:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static const EVP_AEAD aead_des_ede3_cbc_sha1_tls_implicit_iv = {
|
|
|
|
SHA_DIGEST_LENGTH + 24 + 8, /* key len (SHA1 + 3DES + IV) */
|
|
|
|
0, /* nonce len */
|
|
|
|
8 + SHA_DIGEST_LENGTH, /* overhead (padding + SHA1) */
|
|
|
|
SHA_DIGEST_LENGTH, /* max tag length */
|
2017-06-06 20:29:48 +01:00
|
|
|
NULL, /* init */
|
2014-12-22 02:27:41 +00:00
|
|
|
aead_des_ede3_cbc_sha1_tls_implicit_iv_init,
|
|
|
|
aead_tls_cleanup,
|
|
|
|
aead_tls_open,
|
2017-06-06 20:29:48 +01:00
|
|
|
aead_tls_seal_scatter,
|
|
|
|
NULL, /* open_gather */
|
|
|
|
aead_tls_get_iv, /* get_iv */
|
2014-12-22 02:27:41 +00:00
|
|
|
};
|
|
|
|
|
2015-09-03 03:48:16 +01:00
|
|
|
static const EVP_AEAD aead_null_sha1_tls = {
|
2017-06-06 20:29:48 +01:00
|
|
|
SHA_DIGEST_LENGTH, /* key len */
|
|
|
|
0, /* nonce len */
|
|
|
|
SHA_DIGEST_LENGTH, /* overhead (SHA1) */
|
|
|
|
SHA_DIGEST_LENGTH, /* max tag length */
|
|
|
|
NULL, /* init */
|
2015-09-03 03:48:16 +01:00
|
|
|
aead_null_sha1_tls_init,
|
|
|
|
aead_tls_cleanup,
|
|
|
|
aead_tls_open,
|
2017-06-06 20:29:48 +01:00
|
|
|
aead_tls_seal_scatter,
|
|
|
|
NULL, /* open_gather */
|
|
|
|
NULL, /* get_iv */
|
2015-09-03 03:48:16 +01:00
|
|
|
};
|
|
|
|
|
2014-12-22 02:27:41 +00:00
|
|
|
const EVP_AEAD *EVP_aead_aes_128_cbc_sha1_tls(void) {
|
|
|
|
return &aead_aes_128_cbc_sha1_tls;
|
|
|
|
}
|
|
|
|
|
|
|
|
const EVP_AEAD *EVP_aead_aes_128_cbc_sha1_tls_implicit_iv(void) {
|
|
|
|
return &aead_aes_128_cbc_sha1_tls_implicit_iv;
|
|
|
|
}
|
|
|
|
|
|
|
|
const EVP_AEAD *EVP_aead_aes_128_cbc_sha256_tls(void) {
|
|
|
|
return &aead_aes_128_cbc_sha256_tls;
|
|
|
|
}
|
|
|
|
|
|
|
|
const EVP_AEAD *EVP_aead_aes_256_cbc_sha1_tls(void) {
|
|
|
|
return &aead_aes_256_cbc_sha1_tls;
|
|
|
|
}
|
|
|
|
|
|
|
|
const EVP_AEAD *EVP_aead_aes_256_cbc_sha1_tls_implicit_iv(void) {
|
|
|
|
return &aead_aes_256_cbc_sha1_tls_implicit_iv;
|
|
|
|
}
|
|
|
|
|
|
|
|
const EVP_AEAD *EVP_aead_aes_256_cbc_sha256_tls(void) {
|
|
|
|
return &aead_aes_256_cbc_sha256_tls;
|
|
|
|
}
|
|
|
|
|
|
|
|
const EVP_AEAD *EVP_aead_aes_256_cbc_sha384_tls(void) {
|
|
|
|
return &aead_aes_256_cbc_sha384_tls;
|
|
|
|
}
|
|
|
|
|
|
|
|
const EVP_AEAD *EVP_aead_des_ede3_cbc_sha1_tls(void) {
|
|
|
|
return &aead_des_ede3_cbc_sha1_tls;
|
|
|
|
}
|
|
|
|
|
|
|
|
const EVP_AEAD *EVP_aead_des_ede3_cbc_sha1_tls_implicit_iv(void) {
|
|
|
|
return &aead_des_ede3_cbc_sha1_tls_implicit_iv;
|
|
|
|
}
|
2015-09-03 03:48:16 +01:00
|
|
|
|
|
|
|
const EVP_AEAD *EVP_aead_null_sha1_tls(void) { return &aead_null_sha1_tls; }
|