Skip to content

Commit f42a698

Browse files
authored
Merge pull request #10668 from holtrop-wolfssl/f-5394
Force-zero wc_AesSivDecrypt*() output buffer on authentication failure
2 parents 86ba8f7 + f420c66 commit f42a698

2 files changed

Lines changed: 41 additions & 14 deletions

File tree

wolfcrypt/src/aes.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17094,12 +17094,16 @@ 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
}
1710117101
}
1710217102

17103+
if (ret != 0) {
17104+
ForceZero(out, dataSz);
17105+
}
17106+
1710317107
#ifdef WOLFSSL_SMALL_STACK
1710417108
wc_AesDelete(aes, NULL);
1710517109
#else

wolfcrypt/test/test.c

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -74943,33 +74943,56 @@ static wc_test_ret_t aes_siv_oneassoc_test(const AesSivTestVector* testVectors,
7494374943
return 0;
7494474944
}
7494574945

74946-
static wc_test_ret_t aes_siv_negative_test(const AesSivTestVector* testVectors)
74946+
static wc_test_ret_t aes_siv_negative_test(const AesSivTestVector* testVectors,
74947+
int n_vectors)
7494774948
{
7494874949
byte computedCiphertext[82];
7494974950
byte computedPlaintext[82];
7495074951
byte siv[WC_AES_BLOCK_SIZE];
74952+
word32 j;
7495174953
wc_test_ret_t ret;
74954+
int vector_idx;
74955+
74956+
/* Find a test vector that has a non-empty plaintext size */
74957+
for (vector_idx = 0; vector_idx < n_vectors; vector_idx++) {
74958+
if (testVectors[vector_idx].plaintextSz > 0U)
74959+
break;
74960+
}
74961+
if (vector_idx == n_vectors) {
74962+
return WC_TEST_RET_ENC_NC;
74963+
}
7495274964

7495374965
/* Negative test: corrupted SIV must be rejected with AES_SIV_AUTH_E. */
74954-
ret = wc_AesSivEncrypt(testVectors[0].key, testVectors[0].keySz,
74955-
testVectors[0].assoc1, testVectors[0].assoc1Sz,
74956-
testVectors[0].nonce, testVectors[0].nonceSz,
74957-
testVectors[0].plaintext,
74958-
testVectors[0].plaintextSz, siv,
74959-
computedCiphertext);
74966+
ret = wc_AesSivEncrypt(
74967+
testVectors[vector_idx].key,
74968+
testVectors[vector_idx].keySz,
74969+
testVectors[vector_idx].assoc1,
74970+
testVectors[vector_idx].assoc1Sz,
74971+
testVectors[vector_idx].nonce,
74972+
testVectors[vector_idx].nonceSz,
74973+
testVectors[vector_idx].plaintext,
74974+
testVectors[vector_idx].plaintextSz,
74975+
siv, computedCiphertext);
7496074976
if (ret != 0) {
7496174977
return WC_TEST_RET_ENC_EC(ret);
7496274978
}
74979+
XMEMSET(computedPlaintext, 0xFF, sizeof(computedPlaintext));
7496374980
/* Corrupt one byte of the SIV tag. */
7496474981
siv[0] ^= 0x01;
74965-
ret = wc_AesSivDecrypt(testVectors[0].key, testVectors[0].keySz,
74966-
testVectors[0].assoc1, testVectors[0].assoc1Sz,
74967-
testVectors[0].nonce, testVectors[0].nonceSz,
74968-
computedCiphertext, testVectors[0].plaintextSz,
74969-
siv, computedPlaintext);
74982+
ret = wc_AesSivDecrypt(
74983+
testVectors[vector_idx].key, testVectors[vector_idx].keySz,
74984+
testVectors[vector_idx].assoc1, testVectors[vector_idx].assoc1Sz,
74985+
testVectors[vector_idx].nonce, testVectors[vector_idx].nonceSz,
74986+
computedCiphertext, testVectors[vector_idx].plaintextSz,
74987+
siv, computedPlaintext);
7497074988
if (ret != WC_NO_ERR_TRACE(AES_SIV_AUTH_E)) {
7497174989
return WC_TEST_RET_ENC_EC(ret);
7497274990
}
74991+
for (j = 0; j < testVectors[vector_idx].plaintextSz; ++j) {
74992+
if (computedPlaintext[j] != 0) {
74993+
return WC_TEST_RET_ENC_NC;
74994+
}
74995+
}
7497374996
return 0;
7497474997
}
7497574998

@@ -75161,7 +75184,7 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aes_siv_test(void)
7516175184
ret = aes_siv_multiassoc_test(testVectors, AES_SIV_TEST_VECTORS);
7516275185
if (ret != 0)
7516375186
return ret;
75164-
ret = aes_siv_negative_test(testVectors);
75187+
ret = aes_siv_negative_test(testVectors, AES_SIV_TEST_VECTORS);
7516575188
if (ret != 0)
7516675189
return ret;
7516775190
return 0;

0 commit comments

Comments
 (0)