@@ -284,6 +284,66 @@ 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+ byte aad_bad [sizeof (aad )];
313+
314+ XMEMSET (ct , 0 , sizeof (ct ));
315+
316+ ExpectIntEQ (wc_XChaCha20Poly1305_Encrypt (ct , sizeof (ct ),
317+ plaintext , sizeof (plaintext ), aad , sizeof (aad ),
318+ nonce , sizeof (nonce ), key , sizeof (key )), 0 );
319+
320+ ExpectIntEQ (wc_XChaCha20Poly1305_Decrypt (pt , sizeof (pt ), ct , sizeof (ct ),
321+ aad , sizeof (aad ), nonce , sizeof (nonce ), key , sizeof (key )), 0 );
322+
323+ XMEMCPY (ct_bad , ct , sizeof (ct ));
324+ ct_bad [sizeof (ct ) - 1 ] ^= 0x01 ;
325+ ExpectIntEQ (wc_XChaCha20Poly1305_Decrypt (pt , sizeof (pt ), ct_bad ,
326+ sizeof (ct_bad ), aad , sizeof (aad ), nonce , sizeof (nonce ),
327+ key , sizeof (key )),
328+ WC_NO_ERR_TRACE (MAC_CMP_FAILED_E ));
329+
330+ XMEMCPY (ct_bad , ct , sizeof (ct ));
331+ ct_bad [0 ] ^= 0x01 ;
332+ ExpectIntEQ (wc_XChaCha20Poly1305_Decrypt (pt , sizeof (pt ), ct_bad ,
333+ sizeof (ct_bad ), aad , sizeof (aad ), nonce , sizeof (nonce ),
334+ key , sizeof (key )),
335+ WC_NO_ERR_TRACE (MAC_CMP_FAILED_E ));
336+
337+ XMEMCPY (aad_bad , aad , sizeof (aad ));
338+ aad_bad [0 ] ^= 0x01 ;
339+ ExpectIntEQ (wc_XChaCha20Poly1305_Decrypt (pt , sizeof (pt ), ct , sizeof (ct ),
340+ aad_bad , sizeof (aad_bad ), nonce , sizeof (nonce ),
341+ key , sizeof (key )),
342+ WC_NO_ERR_TRACE (MAC_CMP_FAILED_E ));
343+ #endif
344+ return EXPECT_RESULT ();
345+ }
346+
287347#include <wolfssl/wolfcrypt/random.h>
288348
289349#define MC_CIPHER_TEST_COUNT 100
0 commit comments