diff --git a/src/we_aes_block.c b/src/we_aes_block.c index 61374edf..161073ea 100644 --- a/src/we_aes_block.c +++ b/src/we_aes_block.c @@ -30,14 +30,112 @@ typedef struct we_AesBlock { /** The wolfSSL AES data object. */ Aes aes; + /** Raw key used to recreate the AES object when copying the context. */ + unsigned char key[AES_256_KEY_SIZE]; + /** Number of bytes in the raw key. */ + unsigned int keyLen; /** Buffer for streaming. */ unsigned char lastBlock[WC_AES_BLOCK_SIZE]; /** Number of buffered bytes. */ unsigned int over; + /** Flag to indicate whether the AES object is initialized. */ + unsigned int inited:1; /** Flag to indicate whether we are doing encrypt (1) or decrpyt (0). */ unsigned int enc:1; } we_AesBlock; +/** + * Cleanup the internal AES block object. Does not free object. + * + * @param ctx [in] EVP cipher context. + * @returns 1 on success and 0 on failure. + */ +static int we_aes_block_cleanup(EVP_CIPHER_CTX *ctx) +{ + int ret = 1; + we_AesBlock *aes; + + aes = (we_AesBlock *)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) { + if (aes->inited != 0) { + wc_AesFree(&aes->aes); + } + XMEMSET(aes, 0, sizeof(we_AesBlock)); + } + + return ret; +} + +static int we_aes_block_copy(EVP_CIPHER_CTX *srcCtx, + EVP_CIPHER_CTX *dstCtx) +{ + int ret = 1; + int rc; + we_AesBlock *src; + we_AesBlock *dst; + + src = (we_AesBlock *)EVP_CIPHER_CTX_get_cipher_data(srcCtx); + dst = (we_AesBlock *)EVP_CIPHER_CTX_get_cipher_data(dstCtx); + if ((src == NULL) || (dst == NULL)) { + WOLFENGINE_ERROR_MSG(WE_LOG_CIPHER, + "AES block cipher data is NULL"); + ret = 0; + } + + if (ret == 1) { + XMEMSET(dst, 0, sizeof(we_AesBlock)); + XMEMCPY(dst->key, src->key, sizeof(dst->key)); + XMEMCPY(dst->lastBlock, src->lastBlock, sizeof(dst->lastBlock)); + dst->keyLen = src->keyLen; + dst->over = src->over; + dst->enc = src->enc; + } + if ((ret == 1) && (src->inited != 0)) { + if ((src->keyLen == 0) || (src->keyLen > sizeof(src->key))) { + WOLFENGINE_ERROR_MSG(WE_LOG_CIPHER, + "Invalid AES block key length"); + ret = 0; + } + } + if ((ret == 1) && (src->inited != 0)) { + rc = wc_AesInit(&dst->aes, NULL, INVALID_DEVID); + if (rc != 0) { + WOLFENGINE_ERROR_FUNC(WE_LOG_CIPHER, "wc_AesInit", rc); + ret = 0; + } + else { + dst->inited = 1; + } + } + if ((ret == 1) && (src->inited != 0)) { + rc = wc_AesSetKey(&dst->aes, dst->key, dst->keyLen, + (const unsigned char *)src->aes.reg, + src->enc ? AES_ENCRYPTION : AES_DECRYPTION); + if (rc != 0) { + WOLFENGINE_ERROR_FUNC(WE_LOG_CIPHER, "wc_AesSetKey", rc); + ret = 0; + } + } + if (ret == 0) { + if ((dst != NULL) && (dst->inited != 0)) { + wc_AesFree(&dst->aes); + } + if (dst != NULL) { + XMEMSET(dst, 0, sizeof(we_AesBlock)); + } + } + + return ret; +} + +#define AES_BLOCK_COPY_FLAG EVP_CIPH_CUSTOM_COPY + #endif @@ -52,7 +150,7 @@ typedef struct we_AesBlock * * @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. */ @@ -67,22 +165,26 @@ static int we_aes_cbc_init(EVP_CIPHER_CTX *ctx, const unsigned char *key, WOLFENGINE_MSG_VERBOSE(WE_LOG_CIPHER, "ARGS [ctx = %p, key = %p, " "iv = %p, enc = %d]", ctx, key, iv, enc); - if (ret == 1) { - aes = (we_AesBlock *)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; - } + aes = (we_AesBlock *)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) && (key != NULL)) { - WOLFENGINE_MSG(WE_LOG_CIPHER, - "Initializing wolfCrypt Aes structure: %p", &aes->aes); - rc = wc_AesInit(&aes->aes, NULL, INVALID_DEVID); - if (rc != 0) { - WOLFENGINE_ERROR_FUNC(WE_LOG_CIPHER, "wc_AesInit", rc); - ret = 0; + if (aes->inited == 0) { + WOLFENGINE_MSG(WE_LOG_CIPHER, + "Initializing wolfCrypt Aes structure: %p", + &aes->aes); + rc = wc_AesInit(&aes->aes, NULL, INVALID_DEVID); + if (rc != 0) { + WOLFENGINE_ERROR_FUNC(WE_LOG_CIPHER, "wc_AesInit", rc); + ret = 0; + } + else { + aes->inited = 1; + } } if (ret == 1) { WOLFENGINE_MSG(WE_LOG_CIPHER, "Setting AES key (%d bytes)", @@ -93,6 +195,11 @@ static int we_aes_cbc_init(EVP_CIPHER_CTX *ctx, const unsigned char *key, WOLFENGINE_ERROR_FUNC(WE_LOG_CIPHER, "wc_AesSetKey", rc); ret = 0; } + else { + XMEMSET(aes->key, 0, sizeof(aes->key)); + XMEMCPY(aes->key, key, EVP_CIPHER_CTX_key_length(ctx)); + aes->keyLen = EVP_CIPHER_CTX_key_length(ctx); + } } } @@ -115,7 +222,7 @@ static int we_aes_cbc_init(EVP_CIPHER_CTX *ctx, const unsigned char *key, * @param out [out] Buffer to store enciphered result. * @param in [in] Data to encrypt/decrypt. * @param len [in] Length of data to encrypt/decrypt. - * @return -1 on failure. + * @return 0 on failure. * @return 1 on success. */ static int we_aes_cbc_encrypt(we_AesBlock* aes, unsigned char *out, @@ -130,7 +237,7 @@ static int we_aes_cbc_encrypt(we_AesBlock* aes, unsigned char *out, rc = wc_AesCbcEncrypt(&aes->aes, out, in, (unsigned int)len); if (rc != 0) { WOLFENGINE_ERROR_FUNC(WE_LOG_CIPHER, "wc_AesCbcEncrypt", rc); - ret = -1; + ret = 0; } WOLFENGINE_LEAVE(WE_LOG_CIPHER, "we_aes_cbc_encrypt", ret); @@ -146,7 +253,7 @@ static int we_aes_cbc_encrypt(we_AesBlock* aes, unsigned char *out, * @param out [out] Buffer to store enciphered result. * @param in [in] Data to encrypt/decrypt. * @param len [in] Length of data to encrypt/decrypt. - * @return -1 on failure. + * @return 0 on failure. * @return Number of bytes put in out on success. */ static int we_aes_cbc_decrypt(we_AesBlock* aes, unsigned char *out, @@ -163,7 +270,7 @@ static int we_aes_cbc_decrypt(we_AesBlock* aes, unsigned char *out, ret = (int)len; } else { - ret = -1; + ret = 0; } WOLFENGINE_LEAVE(WE_LOG_CIPHER, "we_aes_cbc_decrypt", ret); @@ -179,7 +286,7 @@ static int we_aes_cbc_decrypt(we_AesBlock* aes, unsigned char *out, * @param out [out] Buffer to store enciphered result. * @param in [in] Data to encrypt/decrypt. * @param len [in] Length of data to encrypt/decrypt. - * @return -1 on failure. + * @return 0 on failure. * @return Number of bytes put in out on success. */ static int we_aes_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, @@ -198,7 +305,7 @@ static int we_aes_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, if (aes == NULL) { WOLFENGINE_ERROR_FUNC_NULL(WE_LOG_CIPHER, "EVP_CIPHER_CTX_get_cipher_data", aes); - ret = -1; + ret = 0; } if (ret == 1) { WOLFENGINE_MSG(WE_LOG_CIPHER, "Setting AES IV"); @@ -227,7 +334,7 @@ static int we_aes_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, /** * Extra operations for AES-CBC. * - * No supported operations yet. + * Handles initialization and context copies. * * @param ctx [in] EVP cipher context of operation. * @param type [in] Type of operation to perform. @@ -255,6 +362,12 @@ static int we_aes_cbc_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr) } if (ret == 1) { switch (type) { + case EVP_CTRL_INIT: + XMEMSET(aes, 0, sizeof(we_AesBlock)); + break; + case EVP_CTRL_COPY: + ret = we_aes_block_copy(ctx, (EVP_CIPHER_CTX *)ptr); + break; default: XSNPRINTF(errBuff, sizeof(errBuff), "Unsupported ctrl type %d", type); @@ -273,6 +386,8 @@ static int we_aes_cbc_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr) #define AES_CBC_FLAGS \ (EVP_CIPH_FLAG_DEFAULT_ASN1 | \ EVP_CIPH_CBC_MODE | \ + EVP_CIPH_CTRL_INIT | \ + AES_BLOCK_COPY_FLAG | \ EVP_CIPH_ALWAYS_CALL_INIT) /** AES128-CBC EVP cipher method. */ @@ -301,6 +416,15 @@ static int we_init_aescbc_meth(EVP_CIPHER *cipher) if (ret == 1) { ret = EVP_CIPHER_meth_set_init(cipher, we_aes_cbc_init); } +#if OPENSSL_VERSION_NUMBER >= 0x10100000L + if (ret == 1) { + ret = EVP_CIPHER_meth_set_cleanup(cipher, we_aes_block_cleanup); + } +#else + if (ret == 1) { + cipher->cleanup = we_aes_block_cleanup; + } +#endif if (ret == 1) { ret = EVP_CIPHER_meth_set_do_cipher(cipher, we_aes_cbc_cipher); } @@ -429,12 +553,18 @@ static int we_aes_ecb_init(EVP_CIPHER_CTX *ctx, const unsigned char *key, } if ((ret == 1) && (key != NULL)) { - WOLFENGINE_MSG(WE_LOG_CIPHER, - "Initializing wolfCrypt Aes structure: %p", &aes->aes); - rc = wc_AesInit(&aes->aes, NULL, INVALID_DEVID); - if (rc != 0) { - WOLFENGINE_ERROR_FUNC(WE_LOG_CIPHER, "wc_AesInit", rc); - ret = 0; + if (aes->inited == 0) { + WOLFENGINE_MSG(WE_LOG_CIPHER, + "Initializing wolfCrypt Aes structure: %p", + &aes->aes); + rc = wc_AesInit(&aes->aes, NULL, INVALID_DEVID); + if (rc != 0) { + WOLFENGINE_ERROR_FUNC(WE_LOG_CIPHER, "wc_AesInit", rc); + ret = 0; + } + else { + aes->inited = 1; + } } if (ret == 1) { WOLFENGINE_MSG(WE_LOG_CIPHER, "Setting AES key (%d bytes)", @@ -445,6 +575,11 @@ static int we_aes_ecb_init(EVP_CIPHER_CTX *ctx, const unsigned char *key, WOLFENGINE_ERROR_FUNC(WE_LOG_CIPHER, "wc_AesSetKey", rc); ret = 0; } + else { + XMEMSET(aes->key, 0, sizeof(aes->key)); + XMEMCPY(aes->key, key, EVP_CIPHER_CTX_key_length(ctx)); + aes->keyLen = EVP_CIPHER_CTX_key_length(ctx); + } } } @@ -468,7 +603,7 @@ static int we_aes_ecb_init(EVP_CIPHER_CTX *ctx, const unsigned char *key, * @param out [out] Buffer to store enciphered result. * @param in [in] Data to encrypt/decrypt. * @param len [in] Length of data to encrypt/decrypt. - * @return -1 on failure. + * @return 0 on failure. * @return 1 on success. */ static int we_aes_ecb_encrypt(we_AesBlock* aes, unsigned char *out, @@ -571,7 +706,7 @@ static int we_aes_ecb_encrypt(we_AesBlock* aes, unsigned char *out, * @param out [out] Buffer to store enciphered result. * @param in [in] Data to encrypt/decrypt. * @param len [in] Length of data to encrypt/decrypt. - * @return -1 on failure. + * @return 0 on failure. * @return 1 on success. */ static int we_aes_ecb_decrypt(we_AesBlock* aes, unsigned char *out, @@ -611,7 +746,7 @@ static int we_aes_ecb_decrypt(we_AesBlock* aes, unsigned char *out, len -= l; } /* Padding and not last full block or not padding and full block. */ - if ((aes->over == WC_AES_BLOCK_SIZE) || len > 0) { + if (aes->over == WC_AES_BLOCK_SIZE) { /* Decrypt block cached block. */ rc = wc_AesEcbDecrypt(&aes->aes, out, aes->lastBlock, WC_AES_BLOCK_SIZE); @@ -637,18 +772,16 @@ static int we_aes_ecb_decrypt(we_AesBlock* aes, unsigned char *out, /* Calculate full blocks. */ l = (int)len & (~(WC_AES_BLOCK_SIZE - 1)); - if (l > 0) { - rc = wc_AesEcbDecrypt(&aes->aes, out, in, l); - if (rc != 0) { - WOLFENGINE_ERROR_FUNC(WE_LOG_CIPHER, - "wc_AesEcbDecrypt", rc); - ret = 0; - } - else { - WOLFENGINE_MSG_VERBOSE(WE_LOG_CIPHER, - "Decrypted %d bytes (AES-ECB)", l); - WOLFENGINE_BUFFER(WE_LOG_CIPHER, out, l); - } + rc = wc_AesEcbDecrypt(&aes->aes, out, in, l); + if (rc != 0) { + WOLFENGINE_ERROR_FUNC(WE_LOG_CIPHER, + "wc_AesEcbDecrypt", rc); + ret = 0; + } + else { + WOLFENGINE_MSG_VERBOSE(WE_LOG_CIPHER, + "Decrypted %d bytes (AES-ECB)", l); + WOLFENGINE_BUFFER(WE_LOG_CIPHER, out, l); } in += l; @@ -675,7 +808,7 @@ static int we_aes_ecb_decrypt(we_AesBlock* aes, unsigned char *out, * @param out [out] Buffer to store enciphered result. * @param in [in] Data to encrypt/decrypt. * @param len [in] Length of data to encrypt/decrypt. - * @return -1 on failure. + * @return 0 on failure. * @return 1 on success. */ static int we_aes_ecb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, @@ -693,7 +826,7 @@ static int we_aes_ecb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, if (aes == NULL) { WOLFENGINE_ERROR_FUNC_NULL(WE_LOG_CIPHER, "EVP_CIPHER_CTX_get_cipher_data", aes); - ret = -1; + ret = 0; } else if (aes->enc) { ret = we_aes_ecb_encrypt(aes, out, in, len); @@ -710,7 +843,7 @@ static int we_aes_ecb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, /** * Extra operations for AES-ECB. * - * No supported operations yet. + * Handles initialization and context copies. * * @param ctx [in] EVP cipher context of operation. * @param type [in] Type of operation to perform. @@ -738,6 +871,12 @@ static int we_aes_ecb_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr) } if (ret == 1) { switch (type) { + case EVP_CTRL_INIT: + XMEMSET(aes, 0, sizeof(we_AesBlock)); + break; + case EVP_CTRL_COPY: + ret = we_aes_block_copy(ctx, (EVP_CIPHER_CTX *)ptr); + break; default: XSNPRINTF(errBuff, sizeof(errBuff), "Unsupported ctrl type %d", type); @@ -756,6 +895,8 @@ static int we_aes_ecb_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr) #define AES_ECB_FLAGS \ (EVP_CIPH_ALWAYS_CALL_INIT | \ EVP_CIPH_FLAG_DEFAULT_ASN1 | \ + EVP_CIPH_CTRL_INIT | \ + AES_BLOCK_COPY_FLAG | \ EVP_CIPH_ECB_MODE) /** AES128-ECB EVP cipher method. */ @@ -784,6 +925,15 @@ static int we_init_aesecb_meth(EVP_CIPHER *cipher) if (ret == 1) { ret = EVP_CIPHER_meth_set_init(cipher, we_aes_ecb_init); } +#if OPENSSL_VERSION_NUMBER >= 0x10100000L + if (ret == 1) { + ret = EVP_CIPHER_meth_set_cleanup(cipher, we_aes_block_cleanup); + } +#else + if (ret == 1) { + cipher->cleanup = we_aes_block_cleanup; + } +#endif if (ret == 1) { ret = EVP_CIPHER_meth_set_do_cipher(cipher, we_aes_ecb_cipher); } diff --git a/test/test_cipher.c b/test/test_cipher.c index 34436b21..9497c5f7 100644 --- a/test/test_cipher.c +++ b/test/test_cipher.c @@ -357,11 +357,86 @@ int test_des3_cbc_stream(ENGINE *e, void *data) /******************************************************************************/ +#if defined(WE_HAVE_AESCBC) || defined(WE_HAVE_AESECB) +static int test_aes_block_lifecycle(ENGINE *e, const EVP_CIPHER *cipher, + const unsigned char *iv) +{ + int err = 0; + EVP_CIPHER_CTX *ctx = NULL; + EVP_CIPHER_CTX *copyCtx = NULL; + unsigned char key[32] = { 0 }; + unsigned char input[32] = { 0 }; + unsigned char output[16]; + unsigned char copyOutput[16]; + int outputLen = 0; + int copyOutputLen = 0; + + ctx = EVP_CIPHER_CTX_new(); + err = ctx == NULL; + if (err == 0) { + err = EVP_CipherInit_ex(ctx, cipher, e, key, iv, 1) != 1; + } +#if OPENSSL_VERSION_NUMBER >= 0x10100000L + if (err == 0) { + err = EVP_CIPHER_meth_get_cleanup(EVP_CIPHER_CTX_cipher(ctx)) == NULL; + } +#else + if (err == 0) { + err = EVP_CIPHER_CTX_cipher(ctx)->cleanup == NULL; + } +#endif + if (err == 0) { + err = EVP_CipherInit_ex(ctx, NULL, e, key, iv, -1) != 1; + } + if (err == 0) { + err = EVP_CIPHER_CTX_set_padding(ctx, 0) != 1; + } + if (err == 0) { + err = EVP_CipherUpdate(ctx, output, &outputLen, input, + sizeof(output)) != 1; + } + if (err == 0) { + copyCtx = EVP_CIPHER_CTX_new(); + err = copyCtx == NULL; + } + if (err == 0) { + err = EVP_CIPHER_CTX_copy(copyCtx, ctx) != 1; + } + if (err == 0) { + err = EVP_CipherUpdate(ctx, output, &outputLen, + input + sizeof(output), sizeof(output)) != 1; + } + if (err == 0) { + err = EVP_CipherUpdate(copyCtx, copyOutput, ©OutputLen, + input + sizeof(output), + sizeof(copyOutput)) != 1; + } + if (err == 0) { + err = (outputLen != copyOutputLen) || + (memcmp(output, copyOutput, outputLen) != 0); + } + + EVP_CIPHER_CTX_free(copyCtx); + EVP_CIPHER_CTX_free(ctx); + + return err; +} +#endif + +/******************************************************************************/ + #ifdef WE_HAVE_AESECB int test_aes128_ecb(ENGINE *e, void *data) { - return test_cipher_enc_dec(e, data, EVP_aes_128_ecb(), 16, 16); + int err; + + err = test_cipher_enc_dec(e, data, EVP_aes_128_ecb(), 16, 16); + if (err == 0) { + err = test_aes_block_lifecycle(e, EVP_aes_128_ecb(), NULL); + } + + return err; } /******************************************************************************/ @@ -425,7 +500,56 @@ int test_aes256_ecb_stream(ENGINE *e, void *data) int test_aes128_cbc(ENGINE *e, void *data) { - return test_cipher_enc_dec(e, data, EVP_aes_128_cbc(), 16, 16); + int err; + + err = test_cipher_enc_dec(e, data, EVP_aes_128_cbc(), 16, 16); + + if (err == 0) { + EVP_CIPHER_CTX *ctx = NULL; + const EVP_CIPHER *cipher; + int (*doCipher)(EVP_CIPHER_CTX *, unsigned char *, + const unsigned char *, size_t); + unsigned char key[16] = { 0 }; + unsigned char iv[16] = { 0 }; + unsigned char in[16] = { 0 }; + + ctx = EVP_CIPHER_CTX_new(); + err = ctx == NULL; + if (err == 0) { + err = EVP_CipherInit_ex(ctx, EVP_aes_128_cbc(), e, key, iv, 1) + != 1; + } + if (err == 0) { + err = EVP_CIPHER_CTX_iv_length(ctx) != (int)sizeof(iv); + } + if (err == 0) { + cipher = EVP_CIPHER_CTX_cipher(ctx); +#if OPENSSL_VERSION_NUMBER >= 0x10100000L + doCipher = EVP_CIPHER_meth_get_do_cipher(cipher); +#else + doCipher = cipher->do_cipher; +#endif + err = doCipher == NULL; + } + if (err == 0) { + err = doCipher(ctx, NULL, in, sizeof(in)) != 0; + } + if (err == 0) { + err = EVP_CipherInit_ex(ctx, NULL, e, key, iv, 0) != 1; + } + if (err == 0) { + err = doCipher(ctx, NULL, in, sizeof(in)) != 0; + } + EVP_CIPHER_CTX_free(ctx); + } + + if (err == 0) { + unsigned char iv[16] = { 0 }; + + err = test_aes_block_lifecycle(e, EVP_aes_128_cbc(), iv); + } + + return err; } /******************************************************************************/