Skip to content

Commit 17864a9

Browse files
Disallow partial block decryption for AES-CBC.
1 parent 9c56f18 commit 17864a9

2 files changed

Lines changed: 26 additions & 24 deletions

File tree

‎src/ssl_crypto.c‎

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3358,9 +3358,12 @@ void wolfSSL_AES_cbc_encrypt(const unsigned char *in, unsigned char* out,
33583358
Aes* aes = (Aes*)key;
33593359
word32 whole = (word32)len & ~(word32)(WC_AES_BLOCK_SIZE - 1);
33603360
word32 rem = (word32)len - whole;
3361-
byte blk[WC_AES_BLOCK_SIZE];
33623361

3363-
if ((ret = wc_AesSetIV(aes, (const byte*)iv)) != 0) {
3362+
if ((enc != AES_ENCRYPTION) && (rem > 0)) {
3363+
ret = BAD_LENGTH_E;
3364+
WOLFSSL_MSG("AES CBC decrypt length not a block multiple");
3365+
}
3366+
else if ((ret = wc_AesSetIV(aes, (const byte*)iv)) != 0) {
33643367
WOLFSSL_MSG("Error with setting iv");
33653368
}
33663369
else if (enc == AES_ENCRYPTION) {
@@ -3371,34 +3374,23 @@ void wolfSSL_AES_cbc_encrypt(const unsigned char *in, unsigned char* out,
33713374
/* A partial final block is zero-padded and written as
33723375
* a whole block, so out must have room up to the block end. */
33733376
if ((ret == 0) && (rem > 0)) {
3377+
byte blk[WC_AES_BLOCK_SIZE];
3378+
33743379
XMEMSET(blk, 0, sizeof(blk));
33753380
XMEMCPY(blk, in + whole, rem);
33763381
ret = wc_AesCbcEncrypt(aes, out + whole, blk, sizeof(blk));
3382+
ForceZero(blk, sizeof(blk));
33773383
}
33783384
if (ret != 0) {
33793385
WOLFSSL_MSG("Error with AES CBC encrypt");
33803386
}
33813387
}
33823388
else {
33833389
/* Decrypt with wolfCrypt AES object. */
3384-
if (whole > 0) {
3385-
ret = wc_AesCbcDecrypt(aes, out, in, whole);
3386-
}
3387-
/* The whole final block is read and only the
3388-
* remaining bytes are written. */
3389-
if ((ret == 0) && (rem > 0)) {
3390-
ret = wc_AesCbcDecrypt(aes, blk, in + whole, sizeof(blk));
3391-
if (ret == 0) {
3392-
XMEMCPY(out + whole, blk, rem);
3393-
}
3394-
}
3395-
if (ret != 0) {
3390+
if ((ret = wc_AesCbcDecrypt(aes, out, in, whole)) != 0) {
33963391
WOLFSSL_MSG("Error with AES CBC decrypt");
33973392
}
33983393
}
3399-
if (rem > 0) {
3400-
ForceZero(blk, sizeof(blk));
3401-
}
34023394

34033395
if (ret == 0) {
34043396
/* Get IV for next operation. */

‎tests/api/test_ossl_cipher.c‎

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -607,22 +607,32 @@ int test_wolfSSL_AES_cbc_encrypt(void)
607607
wc_AesFree((Aes*)&aes);
608608

609609
#ifdef HAVE_AES_DECRYPT
610-
/* decrypting the same length reads the whole final block but writes
611-
* only len bytes */
610+
{
611+
byte zeroTail[AES_BLOCK_SIZE - 1];
612+
613+
XMEMSET(zeroTail, 0, sizeof(zeroTail));
612614
ExpectIntEQ(wolfSSL_AES_set_decrypt_key(cbcKey, 128, &aes), 0);
615+
616+
/* a partial decrypt length is rejected: nothing written, IV kept */
613617
XMEMSET(buf, 0xDD, sizeof(buf));
614618
XMEMCPY(ivw, cbcIv, AES_BLOCK_SIZE);
615619
wolfSSL_AES_cbc_encrypt(ct17, buf, 17, &aes, ivw, AES_DECRYPT);
616-
ExpectBufEQ(buf, pt, 17);
617-
ExpectIntEQ(buf[17], 0xDD);
618-
ExpectBufEQ(ivw, ct17 + AES_BLOCK_SIZE, AES_BLOCK_SIZE);
620+
ExpectIntEQ(buf[0], 0xDD);
621+
ExpectIntEQ(XMEMCMP(buf, buf + 1, sizeof(buf) - 1), 0);
622+
ExpectBufEQ(ivw, cbcIv, AES_BLOCK_SIZE);
619623

620-
/* in place */
624+
/* the padded ciphertext decrypts whole, in place */
625+
XMEMSET(buf, 0xDD, sizeof(buf));
621626
XMEMCPY(buf, ct17, sizeof(ct17));
622627
XMEMCPY(ivw, cbcIv, AES_BLOCK_SIZE);
623-
wolfSSL_AES_cbc_encrypt(buf, buf, 17, &aes, ivw, AES_DECRYPT);
628+
wolfSSL_AES_cbc_encrypt(buf, buf, sizeof(ct17), &aes, ivw,
629+
AES_DECRYPT);
624630
ExpectBufEQ(buf, pt, 17);
631+
ExpectBufEQ(buf + 17, zeroTail, sizeof(zeroTail));
632+
ExpectIntEQ(buf[sizeof(ct17)], 0xDD);
633+
ExpectBufEQ(ivw, ct17 + AES_BLOCK_SIZE, AES_BLOCK_SIZE);
625634
wc_AesFree((Aes*)&aes);
635+
}
626636
#endif
627637
}
628638

0 commit comments

Comments
 (0)