Skip to content

Commit 20571a9

Browse files
Force-zero wc_AesSivDecrypt*() output buffer on authentication failure
1 parent bd78a42 commit 20571a9

2 files changed

Lines changed: 24 additions & 10 deletions

File tree

wolfcrypt/src/aes.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17094,10 +17094,14 @@ static WARN_UNUSED_RESULT int AesSivCipher(
1709417094
WOLFSSL_MSG("S2V failed.");
1709517095
}
1709617096

17097-
if (ConstantCompare(siv, sivTmp, WC_AES_BLOCK_SIZE) != 0) {
17097+
if (ret == 0 && ConstantCompare(siv, sivTmp, WC_AES_BLOCK_SIZE) != 0) {
1709817098
WOLFSSL_MSG("Computed SIV doesn't match received SIV.");
1709917099
ret = AES_SIV_AUTH_E;
1710017100
}
17101+
17102+
if (ret != 0) {
17103+
ForceZero(out, dataSz);
17104+
}
1710117105
}
1710217106

1710317107
#ifdef WOLFSSL_SMALL_STACK

wolfcrypt/test/test.c

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -74789,28 +74789,38 @@ static wc_test_ret_t aes_siv_negative_test(const AesSivTestVector* testVectors)
7478974789
byte computedCiphertext[82];
7479074790
byte computedPlaintext[82];
7479174791
byte siv[WC_AES_BLOCK_SIZE];
74792+
word32 j;
7479274793
wc_test_ret_t ret;
7479374794

7479474795
/* Negative test: corrupted SIV must be rejected with AES_SIV_AUTH_E. */
74795-
ret = wc_AesSivEncrypt(testVectors[0].key, testVectors[0].keySz,
74796-
testVectors[0].assoc1, testVectors[0].assoc1Sz,
74797-
testVectors[0].nonce, testVectors[0].nonceSz,
74798-
testVectors[0].plaintext,
74799-
testVectors[0].plaintextSz, siv,
74796+
ret = wc_AesSivEncrypt(testVectors[5].key, testVectors[5].keySz,
74797+
testVectors[5].assoc1, testVectors[5].assoc1Sz,
74798+
testVectors[5].nonce, testVectors[5].nonceSz,
74799+
testVectors[5].plaintext,
74800+
testVectors[5].plaintextSz, siv,
7480074801
computedCiphertext);
7480174802
if (ret != 0) {
7480274803
return WC_TEST_RET_ENC_EC(ret);
7480374804
}
74805+
XMEMSET(computedPlaintext, 0xFF, sizeof(computedPlaintext));
7480474806
/* Corrupt one byte of the SIV tag. */
7480574807
siv[0] ^= 0x01;
74806-
ret = wc_AesSivDecrypt(testVectors[0].key, testVectors[0].keySz,
74807-
testVectors[0].assoc1, testVectors[0].assoc1Sz,
74808-
testVectors[0].nonce, testVectors[0].nonceSz,
74809-
computedCiphertext, testVectors[0].plaintextSz,
74808+
ret = wc_AesSivDecrypt(testVectors[5].key, testVectors[5].keySz,
74809+
testVectors[5].assoc1, testVectors[5].assoc1Sz,
74810+
testVectors[5].nonce, testVectors[5].nonceSz,
74811+
computedCiphertext, testVectors[5].plaintextSz,
7481074812
siv, computedPlaintext);
7481174813
if (ret != WC_NO_ERR_TRACE(AES_SIV_AUTH_E)) {
7481274814
return WC_TEST_RET_ENC_EC(ret);
7481374815
}
74816+
if (testVectors[5].plaintextSz == 0U) {
74817+
return WC_TEST_RET_ENC_NC;
74818+
}
74819+
for (j = 0; j < testVectors[5].plaintextSz; ++j) {
74820+
if (computedPlaintext[j] != 0) {
74821+
return WC_TEST_RET_ENC_NC;
74822+
}
74823+
}
7481474824
return 0;
7481574825
}
7481674826

0 commit comments

Comments
 (0)