Merge pull request #3248 from SparkiDev/aes_cbc_oob

AES-CBC check for input size of 0
This commit is contained in:
toddouska
2020-09-03 13:40:34 -07:00
committed by GitHub
2 changed files with 35 additions and 6 deletions

View File

@@ -11934,6 +11934,7 @@ static int test_wc_AesCbcEncryptDecrypt (void)
int cbcD = WOLFSSL_FATAL_ERROR; int cbcD = WOLFSSL_FATAL_ERROR;
int cbcDWK = WOLFSSL_FATAL_ERROR; int cbcDWK = WOLFSSL_FATAL_ERROR;
byte dec2[sizeof(vector)]; byte dec2[sizeof(vector)];
int i;
/* Init stack variables. */ /* Init stack variables. */
XMEMSET(enc, 0, sizeof(enc)); XMEMSET(enc, 0, sizeof(enc));
@@ -11989,6 +11990,16 @@ static int test_wc_AesCbcEncryptDecrypt (void)
cbcE = WOLFSSL_FATAL_ERROR; cbcE = WOLFSSL_FATAL_ERROR;
} }
} }
if (cbcE == 0) {
/* Test passing in size of 0 */
XMEMSET(enc, 0, sizeof(enc));
cbcE = wc_AesCbcEncrypt(&aes, enc, vector, 0);
if (cbcE == 0) {
/* Check enc was not modified */
for (i = 0; i < (int)sizeof(enc); i++)
cbcE |= enc[0];
}
}
printf(resultFmt, cbcE == 0 ? passed : failed); printf(resultFmt, cbcE == 0 ? passed : failed);
if (cbcE != 0) { if (cbcE != 0) {
wc_AesFree(&aes); wc_AesFree(&aes);
@@ -12013,6 +12024,16 @@ static int test_wc_AesCbcEncryptDecrypt (void)
cbcD = WOLFSSL_FATAL_ERROR; cbcD = WOLFSSL_FATAL_ERROR;
} }
} }
if (cbcD == 0) {
/* Test passing in size of 0 */
XMEMSET(dec, 0, sizeof(dec));
cbcD = wc_AesCbcDecrypt(&aes, dec, enc, 0);
if (cbcD == 0) {
/* Check dec was not modified */
for (i = 0; i < (int)sizeof(dec); i++)
cbcD |= dec[0];
}
}
printf(resultFmt, cbcD == 0 ? passed : failed); printf(resultFmt, cbcD == 0 ? passed : failed);
if (cbcD != 0) { if (cbcD != 0) {
wc_AesFree(&aes); wc_AesFree(&aes);

View File

@@ -3538,6 +3538,10 @@ int wc_AesSetIV(Aes* aes, const byte* iv)
return BAD_FUNC_ARG; return BAD_FUNC_ARG;
} }
if (sz == 0) {
return 0;
}
#ifdef WOLF_CRYPTO_CB #ifdef WOLF_CRYPTO_CB
if (aes->devId != INVALID_DEVID) { if (aes->devId != INVALID_DEVID) {
int ret = wc_CryptoCb_AesCbcEncrypt(aes, out, in, sz); int ret = wc_CryptoCb_AesCbcEncrypt(aes, out, in, sz);
@@ -3637,6 +3641,10 @@ int wc_AesSetIV(Aes* aes, const byte* iv)
return BAD_FUNC_ARG; return BAD_FUNC_ARG;
} }
if (sz == 0) {
return 0;
}
#ifdef WOLF_CRYPTO_CB #ifdef WOLF_CRYPTO_CB
if (aes->devId != INVALID_DEVID) { if (aes->devId != INVALID_DEVID) {
int ret = wc_CryptoCb_AesCbcDecrypt(aes, out, in, sz); int ret = wc_CryptoCb_AesCbcDecrypt(aes, out, in, sz);
@@ -7649,7 +7657,7 @@ int wc_AesEcbEncrypt(Aes* aes, byte* out, const byte* in, word32 sz)
if ((in == NULL) || (out == NULL) || (aes == NULL)) if ((in == NULL) || (out == NULL) || (aes == NULL))
return BAD_FUNC_ARG; return BAD_FUNC_ARG;
while (blocks>0) { while (blocks > 0) {
wc_AesEncryptDirect(aes, out, in); wc_AesEncryptDirect(aes, out, in);
out += AES_BLOCK_SIZE; out += AES_BLOCK_SIZE;
in += AES_BLOCK_SIZE; in += AES_BLOCK_SIZE;
@@ -7666,7 +7674,7 @@ int wc_AesEcbDecrypt(Aes* aes, byte* out, const byte* in, word32 sz)
if ((in == NULL) || (out == NULL) || (aes == NULL)) if ((in == NULL) || (out == NULL) || (aes == NULL))
return BAD_FUNC_ARG; return BAD_FUNC_ARG;
while (blocks>0) { while (blocks > 0) {
wc_AesDecryptDirect(aes, out, in); wc_AesDecryptDirect(aes, out, in);
out += AES_BLOCK_SIZE; out += AES_BLOCK_SIZE;
in += AES_BLOCK_SIZE; in += AES_BLOCK_SIZE;