EVP_CipherInit: allow NULL iv for openSSL compatibility

This commit is contained in:
Takashi Kojo
2018-12-24 12:00:21 +09:00
parent 3eff20904a
commit ae09fbe8a2

View File

@ -13314,7 +13314,7 @@ int wolfSSL_EVP_MD_type(const WOLFSSL_EVP_MD *md)
{
WOLFSSL_ENTER("EVP_CIPHER_CTX_init");
if (ctx) {
ctx->cipherType = 0xff; /* no init */
ctx->cipherType = WOLFSSL_EVP_CIPH_TYPE_INIT; /* not yet initialized */
ctx->keyLen = 0;
ctx->enc = 1; /* start in encrypt mode */
}
@ -13326,13 +13326,26 @@ int wolfSSL_EVP_MD_type(const WOLFSSL_EVP_MD *md)
{
WOLFSSL_ENTER("EVP_CIPHER_CTX_cleanup");
if (ctx) {
ctx->cipherType = 0xff; /* no more init */
ctx->cipherType = WOLFSSL_EVP_CIPH_TYPE_INIT; /* not yet initialized */
ctx->keyLen = 0;
}
return WOLFSSL_SUCCESS;
}
static int AesSetKey(Aes* aes, const byte* key, word32 len,
const byte* iv, int dir)
{
int ret;
/* wc_AesSetKey clear aes.reg if iv == NULL.
Keep IV for openSSL compatibility */
if(iv == NULL)
XMEMCPY((byte *)aes->tmp, (byte *)aes->reg, AES_BLOCK_SIZE);
ret = wc_AesSetKey(aes, key, len, iv, dir);
if(iv == NULL)
XMEMCPY((byte *)aes->reg, (byte *)aes->tmp, AES_BLOCK_SIZE);
return ret;
}
/* return WOLFSSL_SUCCESS on ok, 0 on failure to match API compatibility */
int wolfSSL_EVP_CipherInit(WOLFSSL_EVP_CIPHER_CTX* ctx,
@ -13355,13 +13368,13 @@ int wolfSSL_EVP_MD_type(const WOLFSSL_EVP_MD *md)
return 0; /* failure */
}
if (ctx->cipherType == WOLFSSL_EVP_CIPH_TYPE_INIT){
/* only first EVP_CipherInit invoke. ctx->cipherType is set below */
XMEMSET(&ctx->cipher, 0, sizeof(ctx->cipher));
ctx->bufUsed = 0;
ctx->lastUsed = 0;
ctx->flags = 0;
}
XMEMSET(&ctx->cipher, 0, sizeof(ctx->cipher));
#ifndef NO_AES
#ifdef HAVE_AES_CBC
#ifdef WOLFSSL_AES_128
@ -13376,8 +13389,8 @@ int wolfSSL_EVP_MD_type(const WOLFSSL_EVP_MD *md)
if (enc == 0 || enc == 1)
ctx->enc = enc ? 1 : 0;
if (key) {
ret = wc_AesSetKey(&ctx->cipher.aes, key, ctx->keyLen, iv,
ctx->enc ? AES_ENCRYPTION : AES_DECRYPTION);
ret = AesSetKey(&ctx->cipher.aes, key, ctx->keyLen, iv,
ctx->enc ? AES_ENCRYPTION : AES_DECRYPTION);
if (ret != 0)
return ret;
}
@ -13400,7 +13413,7 @@ int wolfSSL_EVP_MD_type(const WOLFSSL_EVP_MD *md)
if (enc == 0 || enc == 1)
ctx->enc = enc ? 1 : 0;
if (key) {
ret = wc_AesSetKey(&ctx->cipher.aes, key, ctx->keyLen, iv,
ret = AesSetKey(&ctx->cipher.aes, key, ctx->keyLen, iv,
ctx->enc ? AES_ENCRYPTION : AES_DECRYPTION);
if (ret != 0)
return ret;
@ -13424,10 +13437,10 @@ int wolfSSL_EVP_MD_type(const WOLFSSL_EVP_MD *md)
if (enc == 0 || enc == 1)
ctx->enc = enc ? 1 : 0;
if (key) {
ret = wc_AesSetKey(&ctx->cipher.aes, key, ctx->keyLen, iv,
ret = AesSetKey(&ctx->cipher.aes, key, ctx->keyLen, iv,
ctx->enc ? AES_ENCRYPTION : AES_DECRYPTION);
if (ret != 0){
WOLFSSL_MSG("wc_AesSetKey() failed");
WOLFSSL_MSG("AesSetKey() failed");
return ret;
}
}
@ -13454,7 +13467,7 @@ int wolfSSL_EVP_MD_type(const WOLFSSL_EVP_MD *md)
if (enc == 0 || enc == 1)
ctx->enc = enc ? 1 : 0;
if (key) {
ret = wc_AesSetKey(&ctx->cipher.aes, key, ctx->keyLen, iv,
ret = AesSetKey(&ctx->cipher.aes, key, ctx->keyLen, iv,
AES_ENCRYPTION);
if (ret != 0)
return ret;
@ -13478,7 +13491,7 @@ int wolfSSL_EVP_MD_type(const WOLFSSL_EVP_MD *md)
if (enc == 0 || enc == 1)
ctx->enc = enc ? 1 : 0;
if (key) {
ret = wc_AesSetKey(&ctx->cipher.aes, key, ctx->keyLen, iv,
ret = AesSetKey(&ctx->cipher.aes, key, ctx->keyLen, iv,
AES_ENCRYPTION);
if (ret != 0)
return ret;
@ -13502,7 +13515,7 @@ int wolfSSL_EVP_MD_type(const WOLFSSL_EVP_MD *md)
if (enc == 0 || enc == 1)
ctx->enc = enc ? 1 : 0;
if (key) {
ret = wc_AesSetKey(&ctx->cipher.aes, key, ctx->keyLen, iv,
ret = AesSetKey(&ctx->cipher.aes, key, ctx->keyLen, iv,
AES_ENCRYPTION);
if (ret != 0)
return ret;
@ -13527,7 +13540,7 @@ int wolfSSL_EVP_MD_type(const WOLFSSL_EVP_MD *md)
if (enc == 0 || enc == 1)
ctx->enc = enc ? 1 : 0;
if (key) {
ret = wc_AesSetKey(&ctx->cipher.aes, key, ctx->keyLen, NULL,
ret = AesSetKey(&ctx->cipher.aes, key, ctx->keyLen, NULL,
ctx->enc ? AES_ENCRYPTION : AES_DECRYPTION);
}
if (ret != 0)
@ -13546,7 +13559,7 @@ int wolfSSL_EVP_MD_type(const WOLFSSL_EVP_MD *md)
if (enc == 0 || enc == 1)
ctx->enc = enc ? 1 : 0;
if (key) {
ret = wc_AesSetKey(&ctx->cipher.aes, key, ctx->keyLen, NULL,
ret = AesSetKey(&ctx->cipher.aes, key, ctx->keyLen, NULL,
ctx->enc ? AES_ENCRYPTION : AES_DECRYPTION);
}
if (ret != 0)
@ -13565,7 +13578,7 @@ int wolfSSL_EVP_MD_type(const WOLFSSL_EVP_MD *md)
if (enc == 0 || enc == 1)
ctx->enc = enc ? 1 : 0;
if (key) {
ret = wc_AesSetKey(&ctx->cipher.aes, key, ctx->keyLen, NULL,
ret = AesSetKey(&ctx->cipher.aes, key, ctx->keyLen, NULL,
ctx->enc ? AES_ENCRYPTION : AES_DECRYPTION);
}
if (ret != 0)