Skip to content

Commit ca5a953

Browse files
committed
add AEAD bad tag tests
1 parent b573823 commit ca5a953

4 files changed

Lines changed: 132 additions & 0 deletions

File tree

tests/api/test_aes.c

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4392,6 +4392,72 @@ int test_wc_AesGcmStream_ReinitAfterFinal(void)
43924392
return EXPECT_RESULT();
43934393
} /* END test_wc_AesGcmStream_ReinitAfterFinal */
43944394

4395+
int test_wc_AesGcmStream_BadAuthTag(void)
4396+
{
4397+
EXPECT_DECLS;
4398+
#if !defined(NO_AES) && defined(HAVE_AESGCM) && defined(HAVE_AES_DECRYPT) && \
4399+
defined(WOLFSSL_AES_128) && defined(WOLFSSL_AESGCM_STREAM)
4400+
static const byte key[AES_128_KEY_SIZE] = {
4401+
0xfe,0xff,0xe9,0x92, 0x86,0x65,0x73,0x1c,
4402+
0x6d,0x6a,0x8f,0x94, 0x67,0x30,0x83,0x08
4403+
};
4404+
static const byte iv[GCM_NONCE_MID_SZ] = {
4405+
0xca,0xfe,0xba,0xbe, 0xfa,0xce,0xdb,0xad,
4406+
0xde,0xca,0xf8,0x88
4407+
};
4408+
static const byte aad[20] = {
4409+
0xfe,0xed,0xfa,0xce, 0xde,0xad,0xbe,0xef,
4410+
0xfe,0xed,0xfa,0xce, 0xde,0xad,0xbe,0xef,
4411+
0xab,0xad,0xda,0xd2
4412+
};
4413+
static const byte plain[16] = {
4414+
0xd9,0x31,0x32,0x25, 0xf8,0x84,0x06,0xe5,
4415+
0xa5,0x59,0x09,0xc5, 0xaf,0xf5,0x26,0x9a
4416+
};
4417+
Aes enc[1];
4418+
Aes dec[1];
4419+
byte ct[sizeof(plain)];
4420+
byte pt[sizeof(plain)];
4421+
byte tag[WC_AES_BLOCK_SIZE];
4422+
4423+
XMEMSET(enc, 0, sizeof(Aes));
4424+
XMEMSET(dec, 0, sizeof(Aes));
4425+
XMEMSET(tag, 0, sizeof(tag));
4426+
4427+
ExpectIntEQ(wc_AesInit(enc, NULL, INVALID_DEVID), 0);
4428+
ExpectIntEQ(wc_AesGcmInit(enc, key, sizeof(key), iv, sizeof(iv)), 0);
4429+
ExpectIntEQ(wc_AesGcmEncryptUpdate(enc, ct, plain, sizeof(plain),
4430+
aad, sizeof(aad)), 0);
4431+
ExpectIntEQ(wc_AesGcmEncryptFinal(enc, tag, sizeof(tag)), 0);
4432+
wc_AesFree(enc);
4433+
4434+
tag[0] ^= 0x01;
4435+
4436+
ExpectIntEQ(wc_AesInit(dec, NULL, INVALID_DEVID), 0);
4437+
ExpectIntEQ(wc_AesGcmDecryptInit(dec, key, sizeof(key), iv, sizeof(iv)), 0);
4438+
ExpectIntEQ(wc_AesGcmDecryptUpdate(dec, pt, ct, sizeof(ct),
4439+
aad, sizeof(aad)), 0);
4440+
ExpectIntEQ(wc_AesGcmDecryptFinal(dec, tag, sizeof(tag)),
4441+
WC_NO_ERR_TRACE(AES_GCM_AUTH_E));
4442+
wc_AesFree(dec);
4443+
4444+
tag[0] ^= 0x01;
4445+
ExpectIntEQ(wc_AesInit(dec, NULL, INVALID_DEVID), 0);
4446+
ExpectIntEQ(wc_AesGcmDecryptInit(dec, key, sizeof(key), iv, sizeof(iv)), 0);
4447+
{
4448+
byte bad_aad[sizeof(aad)];
4449+
XMEMCPY(bad_aad, aad, sizeof(aad));
4450+
bad_aad[0] ^= 0x01;
4451+
ExpectIntEQ(wc_AesGcmDecryptUpdate(dec, pt, ct, sizeof(ct),
4452+
bad_aad, sizeof(bad_aad)), 0);
4453+
}
4454+
ExpectIntEQ(wc_AesGcmDecryptFinal(dec, tag, sizeof(tag)),
4455+
WC_NO_ERR_TRACE(AES_GCM_AUTH_E));
4456+
wc_AesFree(dec);
4457+
#endif
4458+
return EXPECT_RESULT();
4459+
}
4460+
43954461
/*******************************************************************************
43964462
* GMAC
43974463
******************************************************************************/

tests/api/test_aes.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ int test_wc_AesGcmNonStdNonce(void);
5454
int test_wc_AesGcmStream(void);
5555
int test_wc_AesGcmStream_MidStreamState(void);
5656
int test_wc_AesGcmStream_ReinitAfterFinal(void);
57+
int test_wc_AesGcmStream_BadAuthTag(void);
5758
int test_wc_AesCcmSetKey(void);
5859
int test_wc_AesCcmEncryptDecrypt(void);
5960
int test_wc_AesCcmEncryptDecrypt_InPlace(void);
@@ -133,6 +134,7 @@ int test_wc_CryptoCb_AesGcm_EncryptDecrypt(void);
133134
TEST_DECL_GROUP("aes", test_wc_AesGcmStream), \
134135
TEST_DECL_GROUP("aes", test_wc_AesGcmStream_MidStreamState), \
135136
TEST_DECL_GROUP("aes", test_wc_AesGcmStream_ReinitAfterFinal), \
137+
TEST_DECL_GROUP("aes", test_wc_AesGcmStream_BadAuthTag), \
136138
TEST_DECL_GROUP("aes", test_wc_AesCcmSetKey), \
137139
TEST_DECL_GROUP("aes", test_wc_AesCcmEncryptDecrypt), \
138140
TEST_DECL_GROUP("aes", test_wc_AesCcmEncryptDecrypt_InPlace), \

tests/api/test_chacha20_poly1305.c

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,68 @@ int test_wc_XChaCha20Poly1305_aead(void)
284284
return EXPECT_RESULT();
285285
} /* END test_wc_XChaCha20Poly1305_aead */
286286

287+
int test_wc_XChaCha20Poly1305_BadAuthTag(void)
288+
{
289+
EXPECT_DECLS;
290+
#if defined(HAVE_POLY1305) && defined(HAVE_XCHACHA)
291+
const byte key[32] = {
292+
0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
293+
0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
294+
0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
295+
0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f
296+
};
297+
const byte nonce[24] = {
298+
0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
299+
0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
300+
0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57
301+
};
302+
const byte plaintext[] = {
303+
0x4c, 0x61, 0x64, 0x69, 0x65, 0x73, 0x20, 0x61,
304+
0x6e, 0x64, 0x20, 0x47, 0x65, 0x6e, 0x74, 0x73
305+
};
306+
const byte aad[] = {
307+
0x50, 0x51, 0x52, 0x53, 0xc0, 0xc1, 0xc2, 0xc3
308+
};
309+
byte ct[sizeof(plaintext) + 16];
310+
byte pt[sizeof(plaintext)];
311+
byte ct_bad[sizeof(ct)];
312+
313+
XMEMSET(ct, 0, sizeof(ct));
314+
315+
ExpectIntEQ(wc_XChaCha20Poly1305_Encrypt(ct, sizeof(ct),
316+
plaintext, sizeof(plaintext), aad, sizeof(aad),
317+
nonce, sizeof(nonce), key, sizeof(key)), 0);
318+
319+
ExpectIntEQ(wc_XChaCha20Poly1305_Decrypt(pt, sizeof(pt), ct, sizeof(ct),
320+
aad, sizeof(aad), nonce, sizeof(nonce), key, sizeof(key)), 0);
321+
322+
XMEMCPY(ct_bad, ct, sizeof(ct));
323+
ct_bad[sizeof(ct) - 1] ^= 0x01;
324+
ExpectIntEQ(wc_XChaCha20Poly1305_Decrypt(pt, sizeof(pt), ct_bad,
325+
sizeof(ct_bad), aad, sizeof(aad), nonce, sizeof(nonce),
326+
key, sizeof(key)),
327+
WC_NO_ERR_TRACE(MAC_CMP_FAILED_E));
328+
329+
XMEMCPY(ct_bad, ct, sizeof(ct));
330+
ct_bad[0] ^= 0x01;
331+
ExpectIntEQ(wc_XChaCha20Poly1305_Decrypt(pt, sizeof(pt), ct_bad,
332+
sizeof(ct_bad), aad, sizeof(aad), nonce, sizeof(nonce),
333+
key, sizeof(key)),
334+
WC_NO_ERR_TRACE(MAC_CMP_FAILED_E));
335+
336+
{
337+
byte aad_bad[sizeof(aad)];
338+
XMEMCPY(aad_bad, aad, sizeof(aad));
339+
aad_bad[0] ^= 0x01;
340+
ExpectIntEQ(wc_XChaCha20Poly1305_Decrypt(pt, sizeof(pt), ct, sizeof(ct),
341+
aad_bad, sizeof(aad_bad), nonce, sizeof(nonce),
342+
key, sizeof(key)),
343+
WC_NO_ERR_TRACE(MAC_CMP_FAILED_E));
344+
}
345+
#endif
346+
return EXPECT_RESULT();
347+
}
348+
287349
#include <wolfssl/wolfcrypt/random.h>
288350

289351
#define MC_CIPHER_TEST_COUNT 100

tests/api/test_chacha20_poly1305.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
int test_wc_ChaCha20Poly1305_aead(void);
2828
int test_wc_XChaCha20Poly1305_aead(void);
29+
int test_wc_XChaCha20Poly1305_BadAuthTag(void);
2930
int test_wc_ChaCha20Poly1305_MonteCarlo(void);
3031
int test_wc_ChaCha20Poly1305_Stream(void);
3132
int test_wc_ChaCha20Poly1305_AeadEdgeCases(void);
@@ -38,6 +39,7 @@ int test_wc_ChaCha20Poly1305_CrossCipher(void);
3839
#define TEST_CHACHA20_POLY1305_DECLS \
3940
TEST_DECL_GROUP("chacha20-poly1305", test_wc_ChaCha20Poly1305_aead), \
4041
TEST_DECL_GROUP("xchacha20-poly1305", test_wc_XChaCha20Poly1305_aead), \
42+
TEST_DECL_GROUP("xchacha20-poly1305", test_wc_XChaCha20Poly1305_BadAuthTag), \
4143
TEST_DECL_GROUP("chacha20-poly1305", test_wc_ChaCha20Poly1305_MonteCarlo), \
4244
TEST_DECL_GROUP("chacha20-poly1305", test_wc_ChaCha20Poly1305_Stream), \
4345
TEST_DECL_GROUP("chacha20-poly1305", test_wc_ChaCha20Poly1305_AeadEdgeCases), \

0 commit comments

Comments
 (0)