Skip to content

Commit c2c7aa8

Browse files
Disallow partial block decryption for AES-CBC.
1 parent 1391188 commit c2c7aa8

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
@@ -3222,9 +3222,12 @@ void wolfSSL_AES_cbc_encrypt(const unsigned char *in, unsigned char* out,
32223222
Aes* aes = (Aes*)key;
32233223
word32 whole = (word32)len & ~(word32)(WC_AES_BLOCK_SIZE - 1);
32243224
word32 rem = (word32)len - whole;
3225-
byte blk[WC_AES_BLOCK_SIZE];
32263225

3227-
if ((ret = wc_AesSetIV(aes, (const byte*)iv)) != 0) {
3226+
if ((enc != AES_ENCRYPTION) && (rem > 0)) {
3227+
ret = BAD_LENGTH_E;
3228+
WOLFSSL_MSG("AES CBC decrypt length not a block multiple");
3229+
}
3230+
else if ((ret = wc_AesSetIV(aes, (const byte*)iv)) != 0) {
32283231
WOLFSSL_MSG("Error with setting iv");
32293232
}
32303233
else if (enc == AES_ENCRYPTION) {
@@ -3235,34 +3238,23 @@ void wolfSSL_AES_cbc_encrypt(const unsigned char *in, unsigned char* out,
32353238
/* A partial final block is zero-padded and written as
32363239
* a whole block, so out must have room up to the block end. */
32373240
if ((ret == 0) && (rem > 0)) {
3241+
byte blk[WC_AES_BLOCK_SIZE];
3242+
32383243
XMEMSET(blk, 0, sizeof(blk));
32393244
XMEMCPY(blk, in + whole, rem);
32403245
ret = wc_AesCbcEncrypt(aes, out + whole, blk, sizeof(blk));
3246+
ForceZero(blk, sizeof(blk));
32413247
}
32423248
if (ret != 0) {
32433249
WOLFSSL_MSG("Error with AES CBC encrypt");
32443250
}
32453251
}
32463252
else {
32473253
/* Decrypt with wolfCrypt AES object. */
3248-
if (whole > 0) {
3249-
ret = wc_AesCbcDecrypt(aes, out, in, whole);
3250-
}
3251-
/* The whole final block is read and only the
3252-
* remaining bytes are written. */
3253-
if ((ret == 0) && (rem > 0)) {
3254-
ret = wc_AesCbcDecrypt(aes, blk, in + whole, sizeof(blk));
3255-
if (ret == 0) {
3256-
XMEMCPY(out + whole, blk, rem);
3257-
}
3258-
}
3259-
if (ret != 0) {
3254+
if ((ret = wc_AesCbcDecrypt(aes, out, in, whole)) != 0) {
32603255
WOLFSSL_MSG("Error with AES CBC decrypt");
32613256
}
32623257
}
3263-
if (rem > 0) {
3264-
ForceZero(blk, sizeof(blk));
3265-
}
32663258

32673259
if (ret == 0) {
32683260
/* 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
@@ -582,22 +582,32 @@ int test_wolfSSL_AES_cbc_encrypt(void)
582582
wc_AesFree((Aes*)&aes);
583583

584584
#ifdef HAVE_AES_DECRYPT
585-
/* decrypting the same length reads the whole final block but writes
586-
* only len bytes */
585+
{
586+
byte zeroTail[AES_BLOCK_SIZE - 1];
587+
588+
XMEMSET(zeroTail, 0, sizeof(zeroTail));
587589
ExpectIntEQ(wolfSSL_AES_set_decrypt_key(cbcKey, 128, &aes), 0);
590+
591+
/* a partial decrypt length is rejected: nothing written, IV kept */
588592
XMEMSET(buf, 0xDD, sizeof(buf));
589593
XMEMCPY(ivw, cbcIv, AES_BLOCK_SIZE);
590594
wolfSSL_AES_cbc_encrypt(ct17, buf, 17, &aes, ivw, AES_DECRYPT);
591-
ExpectBufEQ(buf, pt, 17);
592-
ExpectIntEQ(buf[17], 0xDD);
593-
ExpectBufEQ(ivw, ct17 + AES_BLOCK_SIZE, AES_BLOCK_SIZE);
595+
ExpectIntEQ(buf[0], 0xDD);
596+
ExpectIntEQ(XMEMCMP(buf, buf + 1, sizeof(buf) - 1), 0);
597+
ExpectBufEQ(ivw, cbcIv, AES_BLOCK_SIZE);
594598

595-
/* in place */
599+
/* the padded ciphertext decrypts whole, in place */
600+
XMEMSET(buf, 0xDD, sizeof(buf));
596601
XMEMCPY(buf, ct17, sizeof(ct17));
597602
XMEMCPY(ivw, cbcIv, AES_BLOCK_SIZE);
598-
wolfSSL_AES_cbc_encrypt(buf, buf, 17, &aes, ivw, AES_DECRYPT);
603+
wolfSSL_AES_cbc_encrypt(buf, buf, sizeof(ct17), &aes, ivw,
604+
AES_DECRYPT);
599605
ExpectBufEQ(buf, pt, 17);
606+
ExpectBufEQ(buf + 17, zeroTail, sizeof(zeroTail));
607+
ExpectIntEQ(buf[sizeof(ct17)], 0xDD);
608+
ExpectBufEQ(ivw, ct17 + AES_BLOCK_SIZE, AES_BLOCK_SIZE);
600609
wc_AesFree((Aes*)&aes);
610+
}
601611
#endif
602612
}
603613

0 commit comments

Comments
 (0)