diff --git a/src/we_aes_cbc_hmac.c b/src/we_aes_cbc_hmac.c index 0feb1231..d79fccf8 100644 --- a/src/we_aes_cbc_hmac.c +++ b/src/we_aes_cbc_hmac.c @@ -38,8 +38,13 @@ typedef struct we_AesCbcHmac int pLen; /** Flag to indicate whether wolfSSL AES object initialized. */ unsigned int init:1; + /** Flags indicating installed AES and HMAC keys. */ + unsigned int keySet:1; + unsigned int hmacKeySet:1; /** Flag to indicate whether we are doing encrypt (1) or decrpyt (0). */ unsigned int enc:1; + /** Flag to indicate whether TLS AAD has been configured. */ + unsigned int tls:1; /** Flag to indicate whether we are doing TLS 1.1 or above. */ unsigned int tls11:1; } we_AesCbcHmac; @@ -53,7 +58,7 @@ typedef struct we_AesCbcHmac * * @param ctx [in,out] EVP cipher context of operation. * @param key [in] AES key - 16/24/32 bytes. - * @param iv [in] Initialization Vector - 12 bytes. + * @param iv [in] Initialization Vector - 16 bytes. * @param enc [in] 1 when initializing for encrypt and 0 when decrypt. * @return 1 on success and 0 on failure. */ @@ -63,21 +68,27 @@ static int we_aes_cbc_hmac_init(EVP_CIPHER_CTX *ctx, const unsigned char *key, int ret = 1; int rc; we_AesCbcHmac *aes; + unsigned char digest[SHA256_DIGEST_LENGTH]; WOLFENGINE_ENTER(WE_LOG_CIPHER, "we_aes_cbc_hmac_init"); WOLFENGINE_MSG_VERBOSE(WE_LOG_CIPHER, "ARGS [ctx = %p, key = %p, iv = %p, " "enc = %d]", ctx, key, iv, enc); - if ((iv == NULL) && (key == NULL)) { - WOLFENGINE_ERROR_MSG(WE_LOG_CIPHER, "iv == NULL && key == NULL"); + aes = (we_AesCbcHmac *)EVP_CIPHER_CTX_get_cipher_data(ctx); + if (aes == NULL) { + WOLFENGINE_ERROR_FUNC_NULL(WE_LOG_CIPHER, + "EVP_CIPHER_CTX_get_cipher_data", aes); ret = 0; } - - if (ret == 1) { - aes = (we_AesCbcHmac *)EVP_CIPHER_CTX_get_cipher_data(ctx); - if (aes == NULL) { - WOLFENGINE_ERROR_FUNC_NULL(WE_LOG_CIPHER, - "EVP_CIPHER_CTX_get_cipher_data", aes); + if ((ret == 1) && aes->keySet && (key == NULL) && + (aes->enc != (unsigned int)enc)) { + ret = 0; + } + if ((ret == 1) && aes->init && aes->hmacKeySet) { + rc = wc_HmacFinal(&aes->hmac, digest); + OPENSSL_cleanse(digest, sizeof(digest)); + if (rc != 0) { + WOLFENGINE_ERROR_FUNC(WE_LOG_CIPHER, "wc_HmacFinal", rc); ret = 0; } } @@ -99,6 +110,7 @@ static int we_aes_cbc_hmac_init(EVP_CIPHER_CTX *ctx, const unsigned char *key, rc = wc_HmacInit(&aes->hmac, NULL, INVALID_DEVID); if (rc != 0) { WOLFENGINE_ERROR_FUNC(WE_LOG_CIPHER, "wc_HmacInit", rc); + wc_AesFree(&aes->aes); ret = 0; } } @@ -110,6 +122,13 @@ static int we_aes_cbc_hmac_init(EVP_CIPHER_CTX *ctx, const unsigned char *key, aes->enc = enc; /* No payload yet. */ aes->pLen = 0; + aes->tls = 0; + aes->tls11 = 0; + + /* EVP restores the effective CBC IV before calling init. */ + if (iv == NULL) { + iv = EVP_CIPHER_CTX_iv(ctx); + } if (key != NULL) { /* Set the key into wolfSSL AES object. */ @@ -117,12 +136,13 @@ static int we_aes_cbc_hmac_init(EVP_CIPHER_CTX *ctx, const unsigned char *key, EVP_CIPHER_CTX_key_length(ctx)); rc = wc_AesSetKey(&aes->aes, key, EVP_CIPHER_CTX_key_length(ctx), iv, enc ? AES_ENCRYPTION : AES_DECRYPTION); + aes->keySet = (rc == 0); if (rc != 0) { WOLFENGINE_ERROR_FUNC(WE_LOG_CIPHER, "wc_AesSetKey", rc); ret = 0; } } - else { + else if (iv != NULL) { /* Set the IV into wolfSSL AES object. */ WOLFENGINE_MSG(WE_LOG_CIPHER, "Setting AES IV"); rc = wc_AesSetIV(&aes->aes, iv); @@ -138,6 +158,35 @@ static int we_aes_cbc_hmac_init(EVP_CIPHER_CTX *ctx, const unsigned char *key, return ret; } +/** + * Cleanup the internal AES-CBC HMAC object. + * + * @param ctx [in] EVP cipher context. + * @return 1 on success and 0 on failure. + */ +static int we_aes_cbc_hmac_cleanup(EVP_CIPHER_CTX *ctx) +{ + int ret = 1; + we_AesCbcHmac *aes; + + aes = (we_AesCbcHmac *)EVP_CIPHER_CTX_get_cipher_data(ctx); + if (aes == NULL) { + WOLFENGINE_ERROR_FUNC_NULL(WE_LOG_CIPHER, + "EVP_CIPHER_CTX_get_cipher_data", aes); + ret = 0; + } + + if ((ret == 1) && aes->init) { + wc_HmacFree(&aes->hmac); + wc_AesFree(&aes->aes); + aes->init = 0; + aes->keySet = 0; + aes->hmacKeySet = 0; + } + + return ret; +} + /** * Encrypt and MAC using wolfSSL. * @@ -162,7 +211,16 @@ static int we_aes_cbc_hmac_enc(we_AesCbcHmac* aes, unsigned char *out, /* Get payload length to get TLS. */ pLen = aes->pLen; - tls = (pLen != 0); + tls = aes->tls; + off = (tls && aes->tls11) ? WC_AES_BLOCK_SIZE : 0; + if ((len > INT_MAX) || ((len % WC_AES_BLOCK_SIZE) != 0) || + (tls && ((pLen < off) || + (len != (size_t)off + + (((size_t)(pLen - off) + SHA256_DIGEST_LENGTH + + WC_AES_BLOCK_SIZE) & ~(size_t)(WC_AES_BLOCK_SIZE - 1)))))) { + WOLFENGINE_ERROR_MSG(WE_LOG_CIPHER, "Invalid AES-CBC-HMAC length"); + return -1; + } if (!tls) { WOLFENGINE_MSG(WE_LOG_CIPHER, "Not being called from TLS"); pLen = (int)len; @@ -178,7 +236,7 @@ static int we_aes_cbc_hmac_enc(we_AesCbcHmac* aes, unsigned char *out, ret = -1; } } - if (ret != -1) { + if ((ret != -1) && tls) { /* Record layer MACed in ctrl function. */ /* MAC the handshake message/data. */ WOLFENGINE_MSG(WE_LOG_CIPHER, "MAC handshake message/data: len = %d", @@ -218,19 +276,101 @@ static int we_aes_cbc_hmac_enc(we_AesCbcHmac* aes, unsigned char *out, rc = wc_AesCbcEncrypt(&aes->aes, out + off, in + off, (int)len - off); if (rc != 0) { WOLFENGINE_ERROR_FUNC(WE_LOG_CIPHER, "wc_AesCbcEncrypt", rc); - ret = 0; + ret = -1; } else { ret = (int)len; WOLFENGINE_BUFFER(WE_LOG_CIPHER, out + off, (int)(len - off)); } } + if (tls) { + aes->pLen = 0; + aes->tls = 0; + aes->tls11 = 0; + } WOLFENGINE_LEAVE(WE_LOG_CIPHER, "we_aes_cbc_hmac_enc", ret); return ret; } +/* Return an all-ones mask for equal values. */ +static word32 we_cbc_ct_eq(word32 a, word32 b) +{ + word32 diff = a ^ b; + + return 0U - (1U ^ ((diff | (0U - diff)) >> 31)); +} + +/* Arguments are bounded by the 16-bit TLS record length. */ +static word32 we_cbc_ct_lt(word32 a, word32 b) +{ + return 0U - ((a - b) >> 31); +} + +/* Balance HMAC work and compare the MAC at public candidate offsets. */ +static int we_aes_cbc_hmac_verify_ct(Hmac* hmac, + const unsigned char* header, const unsigned char* plain, + word32 maxMsgLen, word32 msgLen, word32 pad) +{ + unsigned char candidateHeader[EVP_AEAD_TLS1_AAD_LEN]; + unsigned char digest[SHA256_DIGEST_LENGTH]; + unsigned char dummy[WC_SHA256_BLOCK_SIZE] = {0}; + unsigned char dummyDigest[SHA256_DIGEST_LENGTH]; + word32 maxBlocks = (EVP_AEAD_TLS1_AAD_LEN + maxMsgLen + 72) / + WC_SHA256_BLOCK_SIZE; + word32 realBlocks = (EVP_AEAD_TLS1_AAD_LEN + msgLen + 72) / + WC_SHA256_BLOCK_SIZE; + word32 dummyBlocks = maxBlocks - realBlocks + 1; + word32 matched = 0; + word32 i; + word32 j; + int rc = 0; + + XMEMCPY(candidateHeader, header, sizeof(candidateHeader)); + candidateHeader[sizeof(candidateHeader) - 2] = (unsigned char)(msgLen >> 8); + candidateHeader[sizeof(candidateHeader) - 1] = (unsigned char)msgLen; + rc = wc_HmacUpdate(hmac, candidateHeader, sizeof(candidateHeader)); + if (rc == 0) { + rc = wc_HmacUpdate(hmac, plain, msgLen); + } + if (rc == 0) { + rc = wc_HmacFinal(hmac, digest); + } + /* A padding byte changes the message by at most 255 bytes, so at most + * five dummy blocks balance the SHA-256 compression count. Finalize the + * dummy HMAC so the context is ready for the next record. */ + for (i = 0; (rc == 0) && (i < 5); i++) { + word32 size = sizeof(dummy) & we_cbc_ct_lt(i, dummyBlocks); + + rc = wc_HmacUpdate(hmac, dummy, size); + } + if (rc == 0) { + rc = wc_HmacFinal(hmac, dummyDigest); + } + + if (rc == 0) { + /* Probe every possible MAC position at public offsets. */ + for (i = 0; i < 256; i++) { + word32 candidateLen = i <= maxMsgLen ? maxMsgLen - i : 0; + word32 mismatch = 0; + + for (j = 0; j < sizeof(digest); j++) { + mismatch |= digest[j] ^ plain[candidateLen + j]; + } + matched |= we_cbc_ct_eq(i, pad) & + we_cbc_ct_eq(mismatch, 0) & + we_cbc_ct_lt(i, maxMsgLen + 1); + } + } + + OPENSSL_cleanse(candidateHeader, sizeof(candidateHeader)); + OPENSSL_cleanse(digest, sizeof(digest)); + OPENSSL_cleanse(dummy, sizeof(dummy)); + OPENSSL_cleanse(dummyDigest, sizeof(dummyDigest)); + return (rc == 0) && (matched != 0); +} + /** * Decrypt and verify MAC using wolfSSL. * @@ -247,22 +387,34 @@ static int we_aes_cbc_hmac_dec(we_AesCbcHmac* aes, unsigned char *out, int ret = 0; int rc; int off = 0; - unsigned char pb; int pLen; int tls; - unsigned char mac[SHA256_DIGEST_LENGTH]; - int i; + word32 payloadLen; + word32 maxMsgLen; + word32 msgLen; + word32 pad; + word32 invalid; + word32 i; + word32 scan; WOLFENGINE_ENTER(WE_LOG_CIPHER, "we_aes_cbc_hmac_dec"); /* Get payload length. */ pLen = aes->pLen; tls = (pLen != 0); - if (aes->tls11) { + off = (tls && aes->tls11) ? WC_AES_BLOCK_SIZE : 0; + if ((len > INT_MAX) || (len < (size_t)off) || + (((len - (size_t)off) % WC_AES_BLOCK_SIZE) != 0) || + (tls && ((len - (size_t)off < SHA256_DIGEST_LENGTH + 1) || + (len != (size_t)((aes->tlsAAD[pLen - 2] << 8) | + aes->tlsAAD[pLen - 1]))))) { + WOLFENGINE_ERROR_MSG(WE_LOG_CIPHER, "Invalid AES-CBC-HMAC length"); + ret = -1; + } + if ((ret != -1) && tls && aes->tls11) { /* TLS v1.1 and v1.2 have IV before message. */ WOLFENGINE_MSG(WE_LOG_CIPHER, "Setting AES IV, aes->tls11 = %d", aes->tls11); - off = WC_AES_BLOCK_SIZE; rc = wc_AesSetIV(&aes->aes, in); if (rc != 0) { WOLFENGINE_ERROR_FUNC(WE_LOG_CIPHER, "wc_AesSetIV", rc); @@ -286,72 +438,34 @@ static int we_aes_cbc_hmac_dec(we_AesCbcHmac* aes, unsigned char *out, } } if ((ret != -1) && tls) { - /* TODO: not constant time. */ - /* Remove padding. */ - pb = out[off + ret - 1]; - if (pb >= WC_AES_BLOCK_SIZE) { - ret = -1; - } - for (i = 1; (ret != -1) && (i <= pb); i++) { - if (out[off + ret - 1 - i] != pb) - ret = -1; - } - if (ret != -1) { - /* Record must hold the padding and MAC or the length underflows. */ - if (ret < pb + 1 + SHA256_DIGEST_LENGTH) { - ret = -1; - } - else { - /* Remove padding and MAC length. */ - ret -= pb + 1 + SHA256_DIGEST_LENGTH; - } + payloadLen = (word32)ret; + maxMsgLen = payloadLen - SHA256_DIGEST_LENGTH - 1; + pad = out[off + payloadLen - 1]; + msgLen = maxMsgLen - pad; + invalid = msgLen >> 31; + msgLen &= 0U - (1U ^ invalid); + scan = payloadLen < 256 ? payloadLen : 256; + for (i = 0; i < scan; i++) { + word32 mask = we_cbc_ct_lt(i, pad + 1); + + invalid |= (out[off + payloadLen - 1 - i] ^ pad) & mask; } - /* Update record header to have correct message length. */ - aes->tlsAAD[aes->pLen - 2] = ret >> 8; - aes->tlsAAD[aes->pLen - 1] = ret; - - /* MAC the record header. */ - WOLFENGINE_MSG(WE_LOG_CIPHER, "Generate MAC over record header: " - "len = %d", aes->pLen); - rc = we_hmac_update(&aes->hmac, aes->tlsAAD, aes->pLen); - if (rc != 1) { - WOLFENGINE_ERROR_FUNC(WE_LOG_CIPHER, "we_hmac_update", rc); - ret = -1; - } - } - /* TODO: not constant time. */ - if (ret != -1) { - /* MAC the message/input. */ - WOLFENGINE_MSG(WE_LOG_CIPHER, "Generate MAC over message/input, " - "len = %d", ret); - rc = we_hmac_update(&aes->hmac, out + off, ret); - if (rc != 1) { - WOLFENGINE_ERROR_FUNC(WE_LOG_CIPHER, "we_hmac_update", rc); + rc = we_aes_cbc_hmac_verify_ct(&aes->hmac, aes->tlsAAD, + out + off, maxMsgLen, msgLen, pad); + if ((rc == 0) || (invalid != 0)) { + WOLFENGINE_ERROR_MSG(WE_LOG_CIPHER, "MAC check failed"); ret = -1; } - } - if (ret != -1) { - /* Calculate MAC. */ - rc = wc_HmacFinal(&aes->hmac, mac); - if (rc != 0) { - WOLFENGINE_ERROR_FUNC(WE_LOG_CIPHER, "wc_HmacFinal", rc); - ret = -1; + else { + ret = (int)msgLen; } } - if (ret != -1) { - WOLFENGINE_MSG_VERBOSE(WE_LOG_CIPHER, "Generated MAC:"); - WOLFENGINE_BUFFER(WE_LOG_CIPHER, mac, SHA256_DIGEST_LENGTH); - /* Check MAC. */ - pb = 0; - for (i = 0; i < SHA256_DIGEST_LENGTH; i++) { - pb |= mac[i] ^ out[off + ret + i]; - } - if (pb != 0) { - WOLFENGINE_ERROR_MSG(WE_LOG_CIPHER, "MAC check failed"); - ret = -1; - } + if (tls) { + aes->pLen = 0; + aes->tls = 0; + aes->tls11 = 0; } WOLFENGINE_LEAVE(WE_LOG_CIPHER, "we_aes_cbc_hmac_dec", ret); @@ -375,24 +489,33 @@ static int we_aes_cbc_hmac_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, size_t len) { int ret = 1; - we_AesCbcHmac* aes; + we_AesCbcHmac* aes = NULL; WOLFENGINE_ENTER(WE_LOG_CIPHER, "we_aes_cbc_hmac_cipher"); WOLFENGINE_MSG_VERBOSE(WE_LOG_CIPHER, "ARGS [ctx = %p, out = %p, in = %p, " "len = %zu]", ctx, out, in, len); - /* Get the AES-CBC object to work with. */ - aes = (we_AesCbcHmac *)EVP_CIPHER_CTX_get_cipher_data(ctx); - if (aes == NULL) { + if ((in == NULL) && (len == 0)) { + ret = 0; + } + else { + /* Get the AES-CBC object to work with. */ + aes = (we_AesCbcHmac *)EVP_CIPHER_CTX_get_cipher_data(ctx); + } + if ((ret != 0) && (aes == NULL)) { WOLFENGINE_ERROR_FUNC_NULL(WE_LOG_CIPHER, "EVP_CIPHER_CTX_get_cipher_data", aes); ret = -1; } - else if (aes->enc) { + else if ((ret != 0) && ((!aes->keySet) || (in == NULL) || + (out == NULL) || (aes->tls && !aes->hmacKeySet))) { + ret = -1; + } + else if ((ret != 0) && aes->enc) { ret = we_aes_cbc_hmac_enc(aes, out, in, len); } - else { + else if (ret != 0) { ret = we_aes_cbc_hmac_dec(aes, out, in, len); } @@ -419,6 +542,8 @@ static int we_aes_cbc_hmac_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg, unsigned char *tls; int tlsVer; int len; + unsigned char header[EVP_AEAD_TLS1_AAD_LEN]; + we_AesCbcHmac *copy; char errBuff[WOLFENGINE_MAX_LOG_WIDTH]; WOLFENGINE_ENTER(WE_LOG_CIPHER, "we_aes_cbc_hmac_ctrl"); @@ -437,11 +562,31 @@ static int we_aes_cbc_hmac_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg, } if (ret == 1) { switch (type) { + case EVP_CTRL_INIT: + XMEMSET(aes, 0, sizeof(*aes)); + break; + case EVP_CTRL_COPY: + copy = NULL; + if (ptr != NULL) { + copy = (we_AesCbcHmac *)EVP_CIPHER_CTX_get_cipher_data( + (EVP_CIPHER_CTX *)ptr); + } + if (copy != NULL) { + OPENSSL_cleanse(copy, sizeof(*copy)); + } + ret = 0; + break; case EVP_CTRL_AEAD_SET_MAC_KEY: WOLFENGINE_MSG(WE_LOG_CIPHER, "EVP_CTRL_AEAD_SET_MAC_KEY"); + if (!aes->init || (arg < 0) || + ((ptr == NULL) && (arg != 0))) { + ret = 0; + break; + } /* Set the HMAC key. */ rc = wc_HmacSetKey(&aes->hmac, WC_SHA256, (const byte*)ptr, arg); + aes->hmacKeySet = (rc == 0); if (rc != 0) { WOLFENGINE_ERROR_FUNC(WE_LOG_CIPHER, "wc_HmacSetKey", rc); ret = 0; @@ -450,48 +595,50 @@ static int we_aes_cbc_hmac_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg, case EVP_CTRL_AEAD_TLS1_AAD: WOLFENGINE_MSG(WE_LOG_CIPHER, "EVP_CTRL_AEAD_TLS1_AAD"); tls = (unsigned char *)ptr; - if (arg != EVP_AEAD_TLS1_AAD_LEN) { + if ((arg != EVP_AEAD_TLS1_AAD_LEN) || (ptr == NULL) || + !aes->hmacKeySet) { WOLFENGINE_ERROR_MSG(WE_LOG_CIPHER, "AAD len != EVP_AEAD_TLS1_AAD_LEN"); - ret = -1; + ret = 0; } if (ret == 1) { /* Get the verion from the record layer. */ tlsVer = (tls[arg - 4] << 8) | tls[arg - 3]; - /* Record whether it is TLS v1.1 or above. */ - aes->tls11 = (tlsVer >= TLS1_1_VERSION); WOLFENGINE_MSG(WE_LOG_CIPHER, "tlsVer = %d, aes->enc = %d", tlsVer, aes->enc); if (aes->enc) { /* Get length from record layer. */ len = (tls[arg - 2] << 8) | tls[arg - 1]; - /* Store payload length. */ - aes->pLen = len; - if (aes->tls11) { + XMEMCPY(header, tls, sizeof(header)); + if (tlsVer >= TLS1_1_VERSION) { /* Must be space for IV. */ if (len < WC_AES_BLOCK_SIZE) { - ret = -1; + ret = 0; } else { /* Remove IV and update record header. */ len -= WC_AES_BLOCK_SIZE; - tls[arg - 2] = len >> 8; - tls[arg - 1] = len; + header[arg - 2] = len >> 8; + header[arg - 1] = len; } } if (ret == 1) { /* MAC the record header. */ WOLFENGINE_MSG(WE_LOG_CIPHER, "Updating MAC with record header"); - rc = we_hmac_update(&aes->hmac, tls, arg); + rc = we_hmac_update(&aes->hmac, header, arg); if (rc != 1) { WOLFENGINE_ERROR_FUNC(WE_LOG_CIPHER, "we_hmac_update", rc); - ret = -1; + ret = 0; } } if (ret == 1) { + aes->pLen = (tls[arg - 2] << 8) | tls[arg - 1]; + aes->tls = 1; + aes->tls11 = (tlsVer >= TLS1_1_VERSION); + XMEMCPY(tls, header, sizeof(header)); /* Calculate length with padding. */ ret = ((len + SHA256_DIGEST_LENGTH + WC_AES_BLOCK_SIZE) & -WC_AES_BLOCK_SIZE) - len; @@ -502,6 +649,8 @@ static int we_aes_cbc_hmac_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg, WOLFENGINE_MSG(WE_LOG_CIPHER, "Storing record header " "for later"); aes->pLen = arg; + aes->tls = 1; + aes->tls11 = (tlsVer >= TLS1_1_VERSION); XMEMCPY(aes->tlsAAD, ptr, arg); /* MAC size. */ ret = SHA256_DIGEST_LENGTH; @@ -526,9 +675,11 @@ static int we_aes_cbc_hmac_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg, #define AES_CBC_HMAC_FLAGS \ (EVP_CIPH_FLAG_CUSTOM_CIPHER | \ EVP_CIPH_ALWAYS_CALL_INIT | \ + EVP_CIPH_CTRL_INIT | \ EVP_CIPH_CBC_MODE | \ EVP_CIPH_FLAG_DEFAULT_ASN1 | \ - EVP_CIPH_FLAG_AEAD_CIPHER) + EVP_CIPH_FLAG_AEAD_CIPHER | \ + EVP_CIPH_CUSTOM_COPY) /** AES128-CBC HMAC SHA256 EVP cipher method. */ EVP_CIPHER* we_aes128_cbc_hmac_ciph = NULL; @@ -554,6 +705,15 @@ static int we_init_aescbc_hmac_meth(EVP_CIPHER *cipher) if (ret == 1) { ret = EVP_CIPHER_meth_set_init(cipher, we_aes_cbc_hmac_init); } +#if OPENSSL_VERSION_NUMBER >= 0x10100000L + if (ret == 1) { + ret = EVP_CIPHER_meth_set_cleanup(cipher, we_aes_cbc_hmac_cleanup); + } +#else + if (ret == 1) { + cipher->cleanup = we_aes_cbc_hmac_cleanup; + } +#endif if (ret == 1) { ret = EVP_CIPHER_meth_set_do_cipher(cipher, we_aes_cbc_hmac_cipher); } diff --git a/test/test_cipher.c b/test/test_cipher.c index 34436b21..6dd7e0dc 100644 --- a/test/test_cipher.c +++ b/test/test_cipher.c @@ -20,6 +20,7 @@ */ #include "unit.h" +#include #if defined(WE_HAVE_DES3CBC) || defined(WE_HAVE_AESCBC) @@ -390,7 +391,6 @@ int test_aes128_ecb_stream(ENGINE *e, void *data) return err; } - /******************************************************************************/ int test_aes192_ecb_stream(ENGINE *e, void *data) @@ -483,8 +483,7 @@ int test_aes256_cbc_stream(ENGINE *e, void *data) /******************************************************************************/ -#if OPENSSL_VERSION_NUMBER >= 0x10100000L && \ - !defined(HAVE_FIPS) && !defined(HAVE_FIPS_VERSION) +#if !defined(HAVE_FIPS) && !defined(HAVE_FIPS_VERSION) int test_aes128_cbc_hmac_tls_short(ENGINE *e, void *data) { int err = 0; @@ -492,7 +491,8 @@ int test_aes128_cbc_hmac_tls_short(ENGINE *e, void *data) int decLen = 0; EVP_CIPHER_CTX *encCtx = NULL; EVP_CIPHER_CTX *ctx = NULL; - const EVP_CIPHER *cipher = EVP_aes_128_cbc_hmac_sha256(); + const EVP_CIPHER *cipher = ENGINE_get_cipher(e, + NID_aes_128_cbc_hmac_sha256); unsigned char key[16]; unsigned char macKey[SHA256_DIGEST_LENGTH]; unsigned char iv[16]; @@ -503,12 +503,8 @@ int test_aes128_cbc_hmac_tls_short(ENGINE *e, void *data) (void)data; - /* OpenSSL only provides the stitched AES-CBC-HMAC-SHA256 cipher on - * platforms where it has a hand-written assembly implementation (e.g. - * AES-NI x86_64); elsewhere EVP_aes_128_cbc_hmac_sha256() returns NULL, - * so there is nothing to exercise. */ if (cipher == NULL) { - return 0; + return 1; } XMEMSET(key, 0, sizeof(key)); @@ -570,17 +566,942 @@ int test_aes128_cbc_hmac_tls_short(ENGINE *e, void *data) return err; } + +static int test_cbc_hmac_plain_reuse(EVP_CIPHER_CTX *ctx, + unsigned char *key, unsigned char *iv) +{ + int err; + unsigned char msg[AES_BLOCK_SIZE] = {1, 2, 3, 4}; + unsigned char enc[sizeof(msg)]; + unsigned char out[sizeof(msg)]; + + PRINT_MSG("CBC-HMAC consumes TLS framing once per record"); + err = test_cipher_enc(NULL, EVP_aes_128_cbc(), key, iv, msg, + sizeof(msg), enc, 0); + if (err == 0) { + err = EVP_Cipher(ctx, out, enc, sizeof(enc)) != (int)sizeof(msg); + } + if (err == 0) { + err = XMEMCMP(out, msg, sizeof(msg)) != 0; + } + + return err; +} + +int test_aes128_cbc_hmac_tls_lengths(ENGINE *e, void *data) +{ + int err = 0; + EVP_CIPHER_CTX *ctx = NULL; + const EVP_CIPHER *cipher = ENGINE_get_cipher(e, + NID_aes_128_cbc_hmac_sha256); + int (*doCipher)(EVP_CIPHER_CTX *, unsigned char *, + const unsigned char *, size_t) = NULL; + unsigned char key[16] = {0}; + unsigned char macKey[SHA256_DIGEST_LENGTH] = {0}; + unsigned char iv[16] = {0}; + unsigned char record[64] = {0}; + unsigned char out[64] = {0}; + unsigned char aad[EVP_AEAD_TLS1_AAD_LEN] = {0}; + + (void)data; + + if (cipher == NULL) { + return 1; + } + aad[9] = (unsigned char)(TLS1_2_VERSION >> 8); + aad[10] = (unsigned char)TLS1_2_VERSION; + + err = (ctx = EVP_CIPHER_CTX_new()) == NULL; + if (err == 0) { + err = EVP_DecryptInit_ex(ctx, cipher, e, key, iv) != 1; + } + if (err == 0) { + err = EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_MAC_KEY, + sizeof(macKey), macKey) != 1; + } + if (err == 0) { + err = EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_TLS1_AAD, + sizeof(aad), aad) != SHA256_DIGEST_LENGTH; + } + if (err == 0) { +#if OPENSSL_VERSION_NUMBER >= 0x10100000L + doCipher = EVP_CIPHER_meth_get_do_cipher(EVP_CIPHER_CTX_cipher(ctx)); +#else + doCipher = EVP_CIPHER_CTX_cipher(ctx)->do_cipher; +#endif + err = doCipher == NULL; + } + if (err == 0) { + err = doCipher(ctx, out, record, 0) >= 0; + } + if (err == 0) { + err = EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_TLS1_AAD, + sizeof(aad), aad) != SHA256_DIGEST_LENGTH; + } + if (err == 0) { + err = doCipher(ctx, out, record, 17) >= 0; + } + if (err == 0) { + err = EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_TLS1_AAD, + sizeof(aad), aad) != SHA256_DIGEST_LENGTH; + } + if (err == 0) { + err = doCipher(ctx, out, record, (size_t)INT_MAX + 1U) >= 0; + } + if (err == 0) { + err = test_cbc_hmac_plain_reuse(ctx, key, iv); + } + + EVP_CIPHER_CTX_free(ctx); + return err; +} + +int test_aes128_cbc_hmac_no_tls_mac(ENGINE *e, void *data) +{ + int err = 0; + int len = 0; + EVP_CIPHER_CTX *ctx = NULL; + const EVP_CIPHER *cipher = ENGINE_get_cipher(e, + NID_aes_128_cbc_hmac_sha256); + unsigned char key[16] = {0}; + unsigned char macKey[SHA256_DIGEST_LENGTH] = {0}; + unsigned char iv[16] = {0}; + unsigned char msg[16] = {1, 2, 3, 4}; + unsigned char enc[sizeof(msg)] = {0}; + unsigned char out[sizeof(msg) + SHA256_DIGEST_LENGTH] = {0}; + + (void)data; + + if (cipher == NULL) { + return 1; + } + err = test_cipher_enc(NULL, EVP_aes_128_cbc(), key, iv, msg, + sizeof(msg), enc, 0); + if (err == 0) { + err = (ctx = EVP_CIPHER_CTX_new()) == NULL; + } + if (err == 0) { + err = EVP_DecryptInit_ex(ctx, cipher, e, key, iv) != 1; + } + if (err == 0) { + err = EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_MAC_KEY, + sizeof(macKey), macKey) != 1; + } + if (err == 0) { + len = EVP_Cipher(ctx, out, enc, sizeof(enc)); + err = len != sizeof(msg); + } + if (err == 0) { + err = XMEMCMP(out, msg, sizeof(msg)) != 0; + } + + EVP_CIPHER_CTX_free(ctx); + return err; +} + +int test_aes128_cbc_hmac_tls_extended_padding(ENGINE *e, void *data) +{ + int err = 0; + int len = 0; + unsigned int macLen = 0; + EVP_CIPHER_CTX *ctx = NULL; + const EVP_CIPHER *cipher = ENGINE_get_cipher(e, + NID_aes_128_cbc_hmac_sha256); + unsigned char key[16] = {0}; + unsigned char macKey[SHA256_DIGEST_LENGTH] = {0}; + unsigned char iv[16] = {0}; + unsigned char msg[16] = {1, 2, 3, 4}; + unsigned char aad[EVP_AEAD_TLS1_AAD_LEN] = {0}; + unsigned char hmacInput[EVP_AEAD_TLS1_AAD_LEN + sizeof(msg)] = {0}; + unsigned char mac[SHA256_DIGEST_LENGTH] = {0}; + unsigned char plain[sizeof(msg) + SHA256_DIGEST_LENGTH + 32] = {0}; + unsigned char record[sizeof(iv) + sizeof(plain)] = {0}; + unsigned char out[sizeof(record)] = {0}; + + (void)data; + + if (cipher == NULL) { + return 1; + } + aad[9] = (unsigned char)(TLS1_2_VERSION >> 8); + aad[10] = (unsigned char)TLS1_2_VERSION; + XMEMCPY(hmacInput, aad, sizeof(aad)); + hmacInput[sizeof(aad) - 1] = sizeof(msg); + XMEMCPY(hmacInput + sizeof(aad), msg, sizeof(msg)); + err = HMAC(EVP_sha256(), macKey, sizeof(macKey), hmacInput, + sizeof(hmacInput), mac, &macLen) == NULL; + if (err == 0) { + err = macLen != sizeof(mac); + } + if (err == 0) { + XMEMCPY(plain, msg, sizeof(msg)); + XMEMCPY(plain + sizeof(msg), mac, sizeof(mac)); + XMEMSET(plain + sizeof(msg) + sizeof(mac), 31, 32); + XMEMCPY(record, iv, sizeof(iv)); + err = test_cipher_enc(NULL, EVP_aes_128_cbc(), key, iv, plain, + sizeof(plain), record + sizeof(iv), 0); + } + + aad[sizeof(aad) - 1] = sizeof(record); + if (err == 0) { + err = (ctx = EVP_CIPHER_CTX_new()) == NULL; + } + if (err == 0) { + err = EVP_DecryptInit_ex(ctx, cipher, e, key, iv) != 1; + } + if (err == 0) { + err = EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_MAC_KEY, + sizeof(macKey), macKey) != 1; + } + if (err == 0) { + err = EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_TLS1_AAD, + sizeof(aad), aad) != SHA256_DIGEST_LENGTH; + } + if (err == 0) { + len = EVP_Cipher(ctx, out, record, sizeof(record)); + err = len != sizeof(msg); + } + if (err == 0) { + err = XMEMCMP(out + sizeof(iv), msg, sizeof(msg)) != 0; + } + if (err == 0) { + err = test_cbc_hmac_plain_reuse(ctx, key, + record + sizeof(record) - sizeof(iv)); + } + + EVP_CIPHER_CTX_free(ctx); + return err; +} + +int test_aes128_cbc_hmac_tls_retry_after_bad_padding(ENGINE *e, void *data) +{ + int err = 0; + int len; + unsigned int macLen = 0; + EVP_CIPHER_CTX *ctx = NULL; + const EVP_CIPHER *cipher = ENGINE_get_cipher(e, + NID_aes_128_cbc_hmac_sha256); + int (*doCipher)(EVP_CIPHER_CTX *, unsigned char *, + const unsigned char *, size_t) = NULL; + unsigned char key[16] = {0}; + unsigned char macKey[SHA256_DIGEST_LENGTH] = {0}; + unsigned char iv[16] = {0}; + unsigned char msg[16] = {1, 2, 3, 4}; + unsigned char aad[EVP_AEAD_TLS1_AAD_LEN] = {0}; + unsigned char hmacInput[EVP_AEAD_TLS1_AAD_LEN + sizeof(msg)] = {0}; + unsigned char mac[SHA256_DIGEST_LENGTH] = {0}; + unsigned char plain[sizeof(msg) + SHA256_DIGEST_LENGTH + 16] = {0}; + unsigned char validRecord[sizeof(iv) + sizeof(plain)] = {0}; + unsigned char badRecord[sizeof(validRecord)] = {0}; + unsigned char badMacRecord[sizeof(validRecord)] = {0}; + unsigned char out[sizeof(validRecord)] = {0}; + + (void)data; + + if (cipher == NULL) { + return 1; + } + aad[9] = (unsigned char)(TLS1_2_VERSION >> 8); + aad[10] = (unsigned char)TLS1_2_VERSION; + XMEMCPY(hmacInput, aad, sizeof(aad)); + hmacInput[sizeof(aad) - 1] = sizeof(msg); + XMEMCPY(hmacInput + sizeof(aad), msg, sizeof(msg)); + err = HMAC(EVP_sha256(), macKey, sizeof(macKey), hmacInput, + sizeof(hmacInput), mac, &macLen) == NULL; + if (err == 0) { + err = macLen != sizeof(mac); + } + if (err == 0) { + XMEMCPY(plain, msg, sizeof(msg)); + XMEMCPY(plain + sizeof(msg), mac, sizeof(mac)); + XMEMSET(plain + sizeof(msg) + sizeof(mac), 15, 16); + XMEMCPY(validRecord, iv, sizeof(iv)); + err = test_cipher_enc(NULL, EVP_aes_128_cbc(), key, iv, plain, + sizeof(plain), validRecord + sizeof(iv), 0); + } + if (err == 0) { + plain[sizeof(msg)] ^= 1; + XMEMCPY(badMacRecord, iv, sizeof(iv)); + err = test_cipher_enc(NULL, EVP_aes_128_cbc(), key, iv, plain, + sizeof(plain), badMacRecord + sizeof(iv), 0); + plain[sizeof(msg)] ^= 1; + } + if (err == 0) { + plain[sizeof(plain) - 2] ^= 1; + XMEMCPY(badRecord, iv, sizeof(iv)); + err = test_cipher_enc(NULL, EVP_aes_128_cbc(), key, iv, plain, + sizeof(plain), badRecord + sizeof(iv), 0); + } + + aad[sizeof(aad) - 1] = sizeof(validRecord); + if (err == 0) { + err = (ctx = EVP_CIPHER_CTX_new()) == NULL; + } + if (err == 0) { + err = EVP_DecryptInit_ex(ctx, cipher, e, key, iv) != 1; + } + if (err == 0) { + err = EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_MAC_KEY, + sizeof(macKey), macKey) != 1; + } + if (err == 0) { + err = EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_TLS1_AAD, + sizeof(aad), aad) != SHA256_DIGEST_LENGTH; + } + if (err == 0) { +#if OPENSSL_VERSION_NUMBER >= 0x10100000L + doCipher = EVP_CIPHER_meth_get_do_cipher(EVP_CIPHER_CTX_cipher(ctx)); +#else + doCipher = EVP_CIPHER_CTX_cipher(ctx)->do_cipher; +#endif + err = doCipher == NULL; + } + if (err == 0) { + err = doCipher(ctx, out, badRecord, sizeof(badRecord)) >= 0; + } + if (err == 0) { + err = test_cbc_hmac_plain_reuse(ctx, key, + badRecord + sizeof(badRecord) - sizeof(iv)); + } + if (err == 0) { + err = EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_TLS1_AAD, + sizeof(aad), aad) != SHA256_DIGEST_LENGTH; + } + if (err == 0) { + err = doCipher(ctx, out, badMacRecord, sizeof(badMacRecord)) >= 0; + } + if (err == 0) { + err = test_cbc_hmac_plain_reuse(ctx, key, + badMacRecord + sizeof(badMacRecord) - sizeof(iv)); + } + if (err == 0) { + err = EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_TLS1_AAD, + sizeof(aad), aad) != SHA256_DIGEST_LENGTH; + } + if (err == 0) { + len = doCipher(ctx, out, validRecord, sizeof(validRecord)); + err = len != sizeof(msg); + } + if (err == 0) { + err = XMEMCMP(out + sizeof(iv), msg, sizeof(msg)) != 0; + } + + EVP_CIPHER_CTX_free(ctx); + return err; +} + +int test_aes128_cbc_hmac_tls_padding_range(ENGINE *e, void *data) +{ + int err = 0; + int len; + int n; + int msgLen; + unsigned int macLen; + EVP_CIPHER_CTX *ctx = NULL; + const EVP_CIPHER *cipher = ENGINE_get_cipher(e, + NID_aes_128_cbc_hmac_sha256); + const int pads[] = {0, 15, 31, 47, 63, 127, 191, 255}; + unsigned char key[16] = {0}; + unsigned char macKey[SHA256_DIGEST_LENGTH] = {1, 2, 3, 4}; + unsigned char iv[16] = {0}; + unsigned char msg[271] = {1, 2, 3, 4}; + unsigned char aad[EVP_AEAD_TLS1_AAD_LEN] = {0}; + unsigned char hmacInput[EVP_AEAD_TLS1_AAD_LEN + sizeof(msg)] = {0}; + unsigned char mac[SHA256_DIGEST_LENGTH] = {0}; + unsigned char plain[sizeof(msg) + SHA256_DIGEST_LENGTH + 1] = {0}; + unsigned char record[sizeof(iv) + sizeof(plain)] = {0}; + unsigned char out[sizeof(record)] = {0}; + + (void)data; + + if (cipher == NULL) { + return 1; + } + aad[9] = (unsigned char)(TLS1_2_VERSION >> 8); + aad[10] = (unsigned char)TLS1_2_VERSION; + aad[sizeof(aad) - 2] = (unsigned char)(sizeof(record) >> 8); + aad[sizeof(aad) - 1] = (unsigned char)sizeof(record); + err = (ctx = EVP_CIPHER_CTX_new()) == NULL; + if (err == 0) { + err = EVP_DecryptInit_ex(ctx, cipher, e, key, iv) != 1; + } + if (err == 0) { + err = EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_MAC_KEY, + sizeof(macKey), macKey) != 1; + } + for (n = 0; (err == 0) && (n < (int)(sizeof(pads) / sizeof(pads[0]))); + n++) { + msgLen = (int)sizeof(msg) - pads[n]; + XMEMSET(hmacInput, 0, sizeof(hmacInput)); + XMEMCPY(hmacInput, aad, sizeof(aad)); + hmacInput[sizeof(aad) - 2] = (unsigned char)(msgLen >> 8); + hmacInput[sizeof(aad) - 1] = (unsigned char)msgLen; + XMEMCPY(hmacInput + sizeof(aad), msg, msgLen); + err = HMAC(EVP_sha256(), macKey, sizeof(macKey), hmacInput, + sizeof(aad) + msgLen, mac, &macLen) == NULL; + if (err == 0) { + err = macLen != sizeof(mac); + } + if (err == 0) { + XMEMCPY(plain, msg, msgLen); + XMEMCPY(plain + msgLen, mac, sizeof(mac)); + XMEMSET(plain + msgLen + sizeof(mac), pads[n], pads[n] + 1); + XMEMCPY(record, iv, sizeof(iv)); + err = test_cipher_enc(NULL, EVP_aes_128_cbc(), key, iv, plain, + sizeof(plain), record + sizeof(iv), 0); + } + if (err == 0) { + err = EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_TLS1_AAD, + sizeof(aad), aad) != SHA256_DIGEST_LENGTH; + } + if (err == 0) { + len = EVP_Cipher(ctx, out, record, sizeof(record)); + err = len != msgLen; + } + if (err == 0) { + err = XMEMCMP(out + sizeof(iv), msg, msgLen) != 0; + } + } + + /* A short record cannot contain the largest claimed padding. */ + if (err == 0) { + XMEMSET(plain, 0, sizeof(iv) * 3); + plain[sizeof(iv) * 3 - 1] = 255; + XMEMCPY(record, iv, sizeof(iv)); + err = test_cipher_enc(NULL, EVP_aes_128_cbc(), key, iv, plain, + sizeof(iv) * 3, record + sizeof(iv), 0); + } + if (err == 0) { + aad[sizeof(aad) - 2] = 0; + aad[sizeof(aad) - 1] = sizeof(iv) * 4; + err = EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_TLS1_AAD, + sizeof(aad), aad) != SHA256_DIGEST_LENGTH; + } + if (err == 0) { + len = EVP_Cipher(ctx, out, record, sizeof(iv) * 4); + err = len >= 0; + } + + /* TLS 1.0 carries no explicit IV in the record. */ + if (err == 0) { + EVP_CIPHER_CTX_free(ctx); + ctx = NULL; + aad[9] = (unsigned char)(TLS1_VERSION >> 8); + aad[10] = (unsigned char)TLS1_VERSION; + aad[sizeof(aad) - 1] = sizeof(iv) * 5; + XMEMSET(hmacInput, 0, sizeof(hmacInput)); + XMEMCPY(hmacInput, aad, sizeof(aad)); + hmacInput[sizeof(aad) - 1] = sizeof(iv); + XMEMCPY(hmacInput + sizeof(aad), msg, sizeof(iv)); + err = HMAC(EVP_sha256(), macKey, sizeof(macKey), hmacInput, + sizeof(aad) + sizeof(iv), mac, &macLen) == NULL; + } + if (err == 0) { + err = macLen != sizeof(mac); + } + if (err == 0) { + XMEMCPY(plain, msg, sizeof(iv)); + XMEMCPY(plain + sizeof(iv), mac, sizeof(mac)); + XMEMSET(plain + sizeof(iv) + sizeof(mac), 31, 32); + err = test_cipher_enc(NULL, EVP_aes_128_cbc(), key, iv, plain, + sizeof(iv) * 5, record, 0); + } + if (err == 0) { + err = (ctx = EVP_CIPHER_CTX_new()) == NULL; + } + if (err == 0) { + err = EVP_DecryptInit_ex(ctx, cipher, e, key, iv) != 1; + } + if (err == 0) { + err = EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_MAC_KEY, + sizeof(macKey), macKey) != 1; + } + if (err == 0) { + err = EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_TLS1_AAD, + sizeof(aad), aad) != SHA256_DIGEST_LENGTH; + } + if (err == 0) { + len = EVP_Cipher(ctx, out, record, sizeof(iv) * 5); + err = len != sizeof(iv); + } + if (err == 0) { + err = XMEMCMP(out, msg, sizeof(iv)) != 0; + } + if (err == 0) { + err = test_cbc_hmac_plain_reuse(ctx, key, record + sizeof(iv) * 4); + } + if (err == 0) { + plain[sizeof(iv) * 5 - 1] = 255; + err = test_cipher_enc(NULL, EVP_aes_128_cbc(), key, iv, plain, + sizeof(iv) * 5, record, 0); + } + if (err == 0) { + err = EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_TLS1_AAD, + sizeof(aad), aad) != SHA256_DIGEST_LENGTH; + } + if (err == 0) { + len = EVP_Cipher(ctx, out, record, sizeof(iv) * 5); + err = len >= 0; + } + + EVP_CIPHER_CTX_free(ctx); + return err; +} #else int test_aes128_cbc_hmac_tls_short(ENGINE *e, void *data) { - /* Stitched AES-CBC-HMAC is not a FIPS-validated cipher, so this path is - * not exercised under FIPS. */ (void)e; (void)data; return 0; } -#endif +int test_aes128_cbc_hmac_tls_lengths(ENGINE *e, void *data) +{ + (void)e; + (void)data; + return 0; +} + +int test_aes128_cbc_hmac_no_tls_mac(ENGINE *e, void *data) +{ + (void)e; + (void)data; + return 0; +} + +int test_aes128_cbc_hmac_tls_extended_padding(ENGINE *e, void *data) +{ + (void)e; + (void)data; + return 0; +} + +int test_aes128_cbc_hmac_tls_retry_after_bad_padding(ENGINE *e, void *data) +{ + (void)e; + (void)data; + return 0; +} + +int test_aes128_cbc_hmac_tls_padding_range(ENGINE *e, void *data) +{ + (void)e; + (void)data; + return 0; +} +#endif + +/******************************************************************************/ + +int test_aes128_cbc_hmac_staged_init(ENGINE *e, void *data) +{ + int err = 0; + EVP_CIPHER_CTX *ctx = NULL; + const EVP_CIPHER *cipher = ENGINE_get_cipher( + e, NID_aes_128_cbc_hmac_sha256); + unsigned char key[16]; + unsigned char iv[16]; + unsigned char plain[16] = {1, 2, 3, 4}; + unsigned char first[16]; + unsigned char repeated[16]; + + (void)data; + + XMEMSET(key, 0, sizeof(key)); + XMEMSET(iv, 0, sizeof(iv)); + + err = (cipher == NULL) || + ((EVP_CIPHER_flags(cipher) & EVP_CIPH_CTRL_INIT) == 0); + if (err == 0) + err = (ctx = EVP_CIPHER_CTX_new()) == NULL; + if (err == 0) { + err = EVP_EncryptInit_ex(ctx, cipher, e, NULL, NULL) != 1; + } + if (err == 0) { + err = EVP_EncryptInit_ex(ctx, NULL, e, key, iv) != 1; + } + if (err == 0) { + err = EVP_Cipher(ctx, first, plain, sizeof(plain)) != sizeof(plain); + } + if (err == 0) { + err = EVP_EncryptInit_ex(ctx, NULL, e, NULL, NULL) != 1; + } + if (err == 0) { + err = EVP_Cipher(ctx, repeated, plain, sizeof(plain)) != sizeof(plain); + } + if (err == 0) { + err = XMEMCMP(first, repeated, sizeof(first)) != 0; + } + if (err == 0) { + err = EVP_DecryptInit_ex(ctx, NULL, e, NULL, NULL) != 0; + } + + EVP_CIPHER_CTX_free(ctx); + + return err; +} + +/******************************************************************************/ + +int test_aes128_cbc_hmac_plain_encrypt(ENGINE *e, void *data) +{ + int err = 0; + int len; + EVP_CIPHER_CTX *usedCtx = NULL; + EVP_CIPHER_CTX *cleanCtx = NULL; + const EVP_CIPHER *cipher = ENGINE_get_cipher( + e, NID_aes_128_cbc_hmac_sha256); + unsigned char key[16]; + unsigned char macKey[SHA256_DIGEST_LENGTH]; + unsigned char iv[16]; + unsigned char aadUsed[EVP_AEAD_TLS1_AAD_LEN]; + unsigned char aadClean[EVP_AEAD_TLS1_AAD_LEN]; + unsigned char in[64]; + unsigned char usedOut[64]; + unsigned char cleanOut[64]; + + (void)data; + + XMEMSET(key, 0, sizeof(key)); + XMEMSET(macKey, 0, sizeof(macKey)); + XMEMSET(iv, 0, sizeof(iv)); + XMEMSET(aadUsed, 0, sizeof(aadUsed)); + XMEMSET(in, 0, sizeof(in)); + + aadUsed[9] = (unsigned char)(TLS1_VERSION >> 8); + aadUsed[10] = (unsigned char)TLS1_VERSION; + aadUsed[12] = 16; + XMEMCPY(aadClean, aadUsed, sizeof(aadClean)); + + err = (usedCtx = EVP_CIPHER_CTX_new()) == NULL; + if (err == 0) + err = (cleanCtx = EVP_CIPHER_CTX_new()) == NULL; + if (err == 0) { + err = EVP_EncryptInit_ex(usedCtx, cipher, e, key, iv) != 1; + } + if (err == 0) { + err = EVP_CIPHER_CTX_ctrl(usedCtx, EVP_CTRL_AEAD_SET_MAC_KEY, + sizeof(macKey), macKey) != 1; + } + if (err == 0) + err = EVP_Cipher(usedCtx, usedOut, in, 16) != 16; + if (err == 0) + err = EVP_EncryptInit_ex(usedCtx, NULL, e, key, iv) != 1; + if (err == 0) { + err = EVP_EncryptInit_ex(cleanCtx, cipher, e, key, iv) != 1; + } + if (err == 0) { + err = EVP_CIPHER_CTX_ctrl(cleanCtx, EVP_CTRL_AEAD_SET_MAC_KEY, + sizeof(macKey), macKey) != 1; + } + if (err == 0) { + len = EVP_CIPHER_CTX_ctrl(usedCtx, EVP_CTRL_AEAD_TLS1_AAD, + sizeof(aadUsed), aadUsed); + err = len != 48; + } + if (err == 0) { + err = EVP_EncryptInit_ex(usedCtx, NULL, e, NULL, NULL) != 1; + } + if (err == 0) { + len = EVP_CIPHER_CTX_ctrl(usedCtx, EVP_CTRL_AEAD_TLS1_AAD, + sizeof(aadUsed), aadUsed); + err = len != 48; + } + if (err == 0) { + len = EVP_CIPHER_CTX_ctrl(cleanCtx, EVP_CTRL_AEAD_TLS1_AAD, + sizeof(aadClean), aadClean); + err = len != 48; + } + if (err == 0) + err = EVP_Cipher(usedCtx, usedOut, in, sizeof(in)) != (int)sizeof(in); + if (err == 0) { + err = EVP_Cipher(cleanCtx, cleanOut, in, sizeof(in)) != + (int)sizeof(in); + } + if (err == 0) + err = XMEMCMP(usedOut, cleanOut, sizeof(usedOut)) != 0; + + EVP_CIPHER_CTX_free(usedCtx); + EVP_CIPHER_CTX_free(cleanCtx); + + return err; +} + +/******************************************************************************/ + +int test_aes128_cbc_hmac_encrypt_error(ENGINE *e, void *data) +{ + int err = 0; + int outLen = 0; + EVP_CIPHER_CTX *ctx = NULL; + const EVP_CIPHER *cipher = ENGINE_get_cipher( + e, NID_aes_128_cbc_hmac_sha256); + unsigned char in[16]; + unsigned char out[16]; + + (void)data; + + XMEMSET(in, 0, sizeof(in)); + + err = (ctx = EVP_CIPHER_CTX_new()) == NULL; + if (err == 0) { + err = EVP_EncryptInit_ex(ctx, cipher, e, NULL, NULL) != 1; + } + if (err == 0) { + err = EVP_EncryptUpdate(ctx, out, &outLen, in, sizeof(in)) != 0; + } + + EVP_CIPHER_CTX_free(ctx); + + return err; +} + +/******************************************************************************/ + +int test_aes128_cbc_hmac_ctrl_error(ENGINE *e, void *data) +{ + int err = 0; + EVP_CIPHER_CTX *ctx = NULL; + const EVP_CIPHER *cipher = ENGINE_get_cipher( + e, NID_aes_128_cbc_hmac_sha256); + unsigned char aad[EVP_AEAD_TLS1_AAD_LEN]; + + (void)data; + + XMEMSET(aad, 0, sizeof(aad)); + + err = (ctx = EVP_CIPHER_CTX_new()) == NULL; + if (err == 0) { + err = EVP_EncryptInit_ex(ctx, cipher, e, NULL, NULL) != 1; + } + if (err == 0) { + ERR_clear_error(); + err = EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_TLS1_AAD, + sizeof(aad) - 1, aad) != 0; + } + if (err == 0) + err = ERR_peek_last_error() != 0; + + EVP_CIPHER_CTX_free(ctx); + + return err; +} + +/******************************************************************************/ + +int test_aes128_cbc_hmac_tls_empty(ENGINE *e, void *data) +{ + int err = 0; + int len; + int engineLen = 0; + int finalLen = 0; + int i; + EVP_CIPHER_CTX *engineCtx = NULL; + const EVP_CIPHER *cipher = ENGINE_get_cipher( + e, NID_aes_128_cbc_hmac_sha256); + unsigned int macLen = 0; + unsigned char key[16]; + unsigned char macKey[SHA256_DIGEST_LENGTH]; + unsigned char iv[16]; + unsigned char engineAAD[EVP_AEAD_TLS1_AAD_LEN]; + unsigned char rejectedAAD[EVP_AEAD_TLS1_AAD_LEN]; + unsigned char plain[48]; + unsigned char in[48]; + unsigned char expected[48]; + unsigned char engineOut[80]; + + (void)data; + + XMEMSET(key, 0, sizeof(key)); + XMEMSET(macKey, 0, sizeof(macKey)); + XMEMSET(iv, 0, sizeof(iv)); + XMEMSET(engineAAD, 0, sizeof(engineAAD)); + XMEMSET(plain, 0, sizeof(plain)); + XMEMSET(in, 0, sizeof(in)); + XMEMSET(engineOut, 0xa5, sizeof(engineOut)); + + engineAAD[9] = (unsigned char)(TLS1_VERSION >> 8); + engineAAD[10] = (unsigned char)TLS1_VERSION; + + if (err == 0) { + err = HMAC(EVP_sha256(), macKey, sizeof(macKey), engineAAD, + sizeof(engineAAD), plain, &macLen) == NULL; + } + if (err == 0) + err = macLen != SHA256_DIGEST_LENGTH; + if (err == 0) { + XMEMSET(plain + macLen, 15, sizeof(plain) - macLen); + err = test_cipher_enc(NULL, EVP_aes_128_cbc(), key, iv, plain, + sizeof(plain), expected, 0); + } + if (err == 0) + err = (engineCtx = EVP_CIPHER_CTX_new()) == NULL; + if (err == 0) { + err = EVP_EncryptInit_ex(engineCtx, cipher, e, key, iv) != 1; + } + if (err == 0) { + err = EVP_CIPHER_CTX_ctrl(engineCtx, EVP_CTRL_AEAD_SET_MAC_KEY, + sizeof(macKey), macKey) != 1; + } + if (err == 0) { + len = EVP_CIPHER_CTX_ctrl(engineCtx, EVP_CTRL_AEAD_TLS1_AAD, + sizeof(engineAAD), engineAAD); + err = len != (int)sizeof(in); + } + if (err == 0) { + XMEMCPY(rejectedAAD, engineAAD, sizeof(rejectedAAD)); + rejectedAAD[9] = (unsigned char)(TLS1_1_VERSION >> 8); + rejectedAAD[10] = (unsigned char)TLS1_1_VERSION; + err = EVP_CIPHER_CTX_ctrl(engineCtx, EVP_CTRL_AEAD_TLS1_AAD, + sizeof(rejectedAAD), rejectedAAD) != 0; + } + if (err == 0) { + err = EVP_Cipher(engineCtx, engineOut, in, 0) >= 0; + } + if (err == 0) { + err = EVP_Cipher(engineCtx, engineOut, in, AES_BLOCK_SIZE) >= 0; + } + if (err == 0) { + err = EVP_Cipher(engineCtx, engineOut, in, + 2 * AES_BLOCK_SIZE) >= 0; + } + for (i = 0; (err == 0) && (i < (int)sizeof(engineOut)); i++) { + err = engineOut[i] != 0xa5; + } + if (err == 0) { + err = EVP_EncryptUpdate(engineCtx, engineOut, &engineLen, in, + sizeof(in)) != 1; + } + if (err == 0) + err = engineLen != (int)sizeof(expected); + if (err == 0) + err = XMEMCMP(expected, engineOut, sizeof(expected)) != 0; + if (err == 0) { + err = EVP_EncryptFinal_ex(engineCtx, engineOut + engineLen, + &finalLen) != 1; + } + if (err == 0) + err = finalLen != 0; + for (i = engineLen; (err == 0) && (i < (int)sizeof(engineOut)); i++) { + err = engineOut[i] != 0xa5; + } + + EVP_CIPHER_CTX_free(engineCtx); + + return err; +} + +/******************************************************************************/ + +int test_aes128_cbc_hmac_reinit_state(ENGINE *e, void *data) +{ + int err = 0; + int len; + unsigned int macLen = 0; + EVP_CIPHER_CTX *ctx = NULL; + const EVP_CIPHER *cipher = ENGINE_get_cipher( + e, NID_aes_128_cbc_hmac_sha256); + unsigned char key[16]; + unsigned char macKey[SHA256_DIGEST_LENGTH]; + unsigned char iv[16]; + unsigned char aad[EVP_AEAD_TLS1_AAD_LEN]; + unsigned char in[16]; + unsigned char enc[16]; + unsigned char mac[SHA256_DIGEST_LENGTH]; + unsigned char out[sizeof(in) + sizeof(mac)]; + + (void)data; + + XMEMSET(key, 0, sizeof(key)); + XMEMSET(macKey, 0, sizeof(macKey)); + XMEMSET(iv, 0, sizeof(iv)); + XMEMSET(aad, 0, sizeof(aad)); + XMEMSET(in, 0, sizeof(in)); + XMEMSET(out, 0, sizeof(out)); + + err = test_cipher_enc(NULL, EVP_aes_128_cbc(), key, iv, in, sizeof(in), + enc, 0); + if (err == 0) { + err = HMAC(EVP_sha256(), macKey, sizeof(macKey), in, sizeof(in), mac, + &macLen) == NULL; + } + if (err == 0) + err = macLen != sizeof(mac); + + aad[9] = (unsigned char)(TLS1_1_VERSION >> 8); + aad[10] = (unsigned char)TLS1_1_VERSION; + aad[12] = sizeof(in); + + if (err == 0) + err = (ctx = EVP_CIPHER_CTX_new()) == NULL; + if (err == 0) { + err = EVP_DecryptInit_ex(ctx, cipher, e, key, iv) != 1; + } + if (err == 0) { + err = EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_MAC_KEY, + sizeof(macKey), macKey) != 1; + } + if (err == 0) { + len = EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_TLS1_AAD, sizeof(aad), + aad); + err = len != SHA256_DIGEST_LENGTH; + } + if (err == 0) { + err = EVP_DecryptInit_ex(ctx, NULL, e, key, iv) != 1; + } + if (err == 0) { + XMEMCPY(out + sizeof(in), mac, sizeof(mac)); + err = EVP_Cipher(ctx, out, enc, sizeof(enc)) != (int)sizeof(in); + } + if (err == 0) + err = XMEMCMP(out, in, sizeof(in)) != 0; + + EVP_CIPHER_CTX_free(ctx); + + return err; +} + +/******************************************************************************/ + +int test_aes128_cbc_hmac_cleanup(ENGINE *e, void *data) +{ + int err = 0; + EVP_CIPHER_CTX *ctx = NULL; + EVP_CIPHER_CTX *copyCtx = NULL; + const EVP_CIPHER *cipher; + unsigned char key[16]; + unsigned char iv[16]; + + (void)data; + + cipher = ENGINE_get_cipher(e, NID_aes_128_cbc_hmac_sha256); + err = cipher == NULL; + if (err == 0) { +#if OPENSSL_VERSION_NUMBER >= 0x10100000L + err = EVP_CIPHER_meth_get_cleanup(cipher) == NULL; +#else + err = cipher->cleanup == NULL; +#endif + } + + XMEMSET(key, 0, sizeof(key)); + XMEMSET(iv, 0, sizeof(iv)); + + if (err == 0) + err = (ctx = EVP_CIPHER_CTX_new()) == NULL; + if (err == 0) { + err = EVP_EncryptInit_ex(ctx, cipher, e, key, iv) != 1; + } + if (err == 0) + err = (copyCtx = EVP_CIPHER_CTX_new()) == NULL; + if (err == 0) + err = EVP_CIPHER_CTX_copy(copyCtx, ctx) != 0; + + EVP_CIPHER_CTX_free(copyCtx); + EVP_CIPHER_CTX_free(ctx); + + return err; +} #endif /* WE_HAVE_AESCBC */ /******************************************************************************/ diff --git a/test/unit.c b/test/unit.c index f04e32fc..54d14537 100644 --- a/test/unit.c +++ b/test/unit.c @@ -115,6 +115,18 @@ TEST_CASE test_case[] = { TEST_DECL(test_aes192_cbc_stream, NULL), TEST_DECL(test_aes256_cbc_stream, NULL), TEST_DECL(test_aes128_cbc_hmac_tls_short, NULL), + TEST_DECL(test_aes128_cbc_hmac_staged_init, NULL), + TEST_DECL(test_aes128_cbc_hmac_plain_encrypt, NULL), + TEST_DECL(test_aes128_cbc_hmac_encrypt_error, NULL), + TEST_DECL(test_aes128_cbc_hmac_ctrl_error, NULL), + TEST_DECL(test_aes128_cbc_hmac_tls_empty, NULL), + TEST_DECL(test_aes128_cbc_hmac_reinit_state, NULL), + TEST_DECL(test_aes128_cbc_hmac_cleanup, NULL), + TEST_DECL(test_aes128_cbc_hmac_tls_lengths, NULL), + TEST_DECL(test_aes128_cbc_hmac_no_tls_mac, NULL), + TEST_DECL(test_aes128_cbc_hmac_tls_extended_padding, NULL), + TEST_DECL(test_aes128_cbc_hmac_tls_retry_after_bad_padding, NULL), + TEST_DECL(test_aes128_cbc_hmac_tls_padding_range, NULL), #endif #ifdef WE_HAVE_AESCTR TEST_DECL(test_aes128_ctr_stream, NULL), diff --git a/test/unit.h b/test/unit.h index 2d519726..afdc7bcd 100644 --- a/test/unit.h +++ b/test/unit.h @@ -151,6 +151,18 @@ int test_aes128_cbc_stream(ENGINE *e, void *data); int test_aes192_cbc_stream(ENGINE *e, void *data); int test_aes256_cbc_stream(ENGINE *e, void *data); int test_aes128_cbc_hmac_tls_short(ENGINE *e, void *data); +int test_aes128_cbc_hmac_staged_init(ENGINE *e, void *data); +int test_aes128_cbc_hmac_plain_encrypt(ENGINE *e, void *data); +int test_aes128_cbc_hmac_encrypt_error(ENGINE *e, void *data); +int test_aes128_cbc_hmac_ctrl_error(ENGINE *e, void *data); +int test_aes128_cbc_hmac_tls_empty(ENGINE *e, void *data); +int test_aes128_cbc_hmac_reinit_state(ENGINE *e, void *data); +int test_aes128_cbc_hmac_cleanup(ENGINE *e, void *data); +int test_aes128_cbc_hmac_tls_lengths(ENGINE *e, void *data); +int test_aes128_cbc_hmac_no_tls_mac(ENGINE *e, void *data); +int test_aes128_cbc_hmac_tls_extended_padding(ENGINE *e, void *data); +int test_aes128_cbc_hmac_tls_retry_after_bad_padding(ENGINE *e, void *data); +int test_aes128_cbc_hmac_tls_padding_range(ENGINE *e, void *data); #endif