Skip to content

Commit 5bc5251

Browse files
Dynamically find AES-SIV test vector index instead of hardcoding in unit test
1 parent 7064eec commit 5bc5251

1 file changed

Lines changed: 24 additions & 15 deletions

File tree

wolfcrypt/test/test.c

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -74784,39 +74784,48 @@ static wc_test_ret_t aes_siv_oneassoc_test(const AesSivTestVector* testVectors,
7478474784
return 0;
7478574785
}
7478674786

74787-
static wc_test_ret_t aes_siv_negative_test(const AesSivTestVector* testVectors)
74787+
static wc_test_ret_t aes_siv_negative_test(const AesSivTestVector* testVectors, size_t n_vectors)
7478874788
{
7478974789
byte computedCiphertext[82];
7479074790
byte computedPlaintext[82];
7479174791
byte siv[WC_AES_BLOCK_SIZE];
7479274792
word32 j;
7479374793
wc_test_ret_t ret;
74794+
size_t vector_idx;
74795+
74796+
/* Find a test vector that has a non-empty plaintext size */
74797+
for (vector_idx = 0U; vector_idx < n_vectors; vector_idx++)
74798+
{
74799+
if (testVectors[vector_idx].plaintextSz > 0U)
74800+
break;
74801+
}
74802+
if (vector_idx == n_vectors)
74803+
{
74804+
return WC_TEST_RET_ENC_NC;
74805+
}
7479474806

7479574807
/* Negative test: corrupted SIV must be rejected with AES_SIV_AUTH_E. */
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,
74808+
ret = wc_AesSivEncrypt(testVectors[vector_idx].key, testVectors[vector_idx].keySz,
74809+
testVectors[vector_idx].assoc1, testVectors[vector_idx].assoc1Sz,
74810+
testVectors[vector_idx].nonce, testVectors[vector_idx].nonceSz,
74811+
testVectors[vector_idx].plaintext,
74812+
testVectors[vector_idx].plaintextSz, siv,
7480174813
computedCiphertext);
7480274814
if (ret != 0) {
7480374815
return WC_TEST_RET_ENC_EC(ret);
7480474816
}
7480574817
XMEMSET(computedPlaintext, 0xFF, sizeof(computedPlaintext));
7480674818
/* Corrupt one byte of the SIV tag. */
7480774819
siv[0] ^= 0x01;
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,
74820+
ret = wc_AesSivDecrypt(testVectors[vector_idx].key, testVectors[vector_idx].keySz,
74821+
testVectors[vector_idx].assoc1, testVectors[vector_idx].assoc1Sz,
74822+
testVectors[vector_idx].nonce, testVectors[vector_idx].nonceSz,
74823+
computedCiphertext, testVectors[vector_idx].plaintextSz,
7481274824
siv, computedPlaintext);
7481374825
if (ret != WC_NO_ERR_TRACE(AES_SIV_AUTH_E)) {
7481474826
return WC_TEST_RET_ENC_EC(ret);
7481574827
}
74816-
if (testVectors[5].plaintextSz == 0U) {
74817-
return WC_TEST_RET_ENC_NC;
74818-
}
74819-
for (j = 0; j < testVectors[5].plaintextSz; ++j) {
74828+
for (j = 0; j < testVectors[vector_idx].plaintextSz; ++j) {
7482074829
if (computedPlaintext[j] != 0) {
7482174830
return WC_TEST_RET_ENC_NC;
7482274831
}
@@ -75012,7 +75021,7 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aes_siv_test(void)
7501275021
ret = aes_siv_multiassoc_test(testVectors, AES_SIV_TEST_VECTORS);
7501375022
if (ret != 0)
7501475023
return ret;
75015-
ret = aes_siv_negative_test(testVectors);
75024+
ret = aes_siv_negative_test(testVectors, AES_SIV_TEST_VECTORS);
7501675025
if (ret != 0)
7501775026
return ret;
7501875027
return 0;

0 commit comments

Comments
 (0)