@@ -61,6 +61,26 @@ int test_wc_Chacha_SetKey(void)
6161 /* Test bad args. */
6262 ExpectIntEQ (wc_Chacha_SetIV (NULL , cipher , 0 ),
6363 WC_NO_ERR_TRACE (BAD_FUNC_ARG ));
64+ /* ctx valid, inIv NULL: independence pair for the SetIV OR's 2nd arg. */
65+ ExpectIntEQ (wc_Chacha_SetIV (& ctx , NULL , 0 ),
66+ WC_NO_ERR_TRACE (BAD_FUNC_ARG ));
67+
68+ /* ctx valid, key NULL: independence pair for the SetKey OR's 2nd arg. */
69+ ExpectIntEQ (wc_Chacha_SetKey (& ctx , NULL , keySz ),
70+ WC_NO_ERR_TRACE (BAD_FUNC_ARG ));
71+ /* half-length key (16 bytes / tau constant) is also valid. */
72+ ExpectIntEQ (wc_Chacha_SetKey (& ctx , key , CHACHA_MAX_KEY_SZ / 2 ), 0 );
73+
74+ /* misaligned key pointer: exercises the (wc_ptr_t)key % 4 realignment
75+ * decision in wc_Chacha_SetKey when XSTREAM_ALIGN is forced on (the
76+ * xstream_align campaign variant). settings.h compiles XSTREAM_ALIGN out
77+ * by default on x86_64/i386/ia64 (NO_XSTREAM_ALIGN), so this call is a
78+ * harmless no-op realignment-free copy on every other build. */
79+ {
80+ byte keybuf [CHACHA_MAX_KEY_SZ + 1 ];
81+ XMEMCPY (keybuf + 1 , key , sizeof (key ));
82+ ExpectIntEQ (wc_Chacha_SetKey (& ctx , keybuf + 1 , keySz ), 0 );
83+ }
6484#endif
6585 return EXPECT_RESULT ();
6686} /* END test_wc_Chacha_SetKey */
@@ -179,9 +199,25 @@ int test_wc_Chacha_Process(void)
179199 }
180200#endif
181201
182- /* Test bad args. */
202+ /* Test bad args: independence pairs for each operand of the 3-way OR . */
183203 ExpectIntEQ (wc_Chacha_Process (NULL , cipher , (byte * )input , (word32 )inlen ),
184204 WC_NO_ERR_TRACE (BAD_FUNC_ARG ));
205+ ExpectIntEQ (wc_Chacha_Process (& enc , cipher , NULL , (word32 )inlen ),
206+ WC_NO_ERR_TRACE (BAD_FUNC_ARG ));
207+ ExpectIntEQ (wc_Chacha_Process (& enc , NULL , (byte * )input , (word32 )inlen ),
208+ WC_NO_ERR_TRACE (BAD_FUNC_ARG ));
209+
210+ /* msglen==0 with a pending leftover keystream block: independence pair
211+ * for the (msglen > 0 && ctx->left > 0) decision's first operand, held
212+ * with ctx->left > 0 (the other operand) true. Deliberately NOT under
213+ * the USE_INTEL_CHACHA_SPEEDUP-exclude guard above: portable C and the
214+ * intel-speedup dispatch each carry their own copy of this decision at
215+ * a different line, and this call is variant-agnostic so it closes
216+ * whichever one a given build compiled. */
217+ ExpectIntEQ (wc_Chacha_SetKey (& enc , key , keySz ), 0 );
218+ ExpectIntEQ (wc_Chacha_SetIV (& enc , cipher , 0 ), 0 );
219+ ExpectIntEQ (wc_Chacha_Process (& enc , cipher , (byte * )input , 1 ), 0 );
220+ ExpectIntEQ (wc_Chacha_Process (& enc , cipher , (byte * )input , 0 ), 0 );
185221#endif
186222 return EXPECT_RESULT ();
187223} /* END test_wc_Chacha_Process */
@@ -617,3 +653,53 @@ int test_wc_Chacha_UnalignedBuffers(void)
617653#endif
618654 return EXPECT_RESULT ();
619655} /* END test_wc_Chacha_UnalignedBuffers */
656+
657+ /*
658+ * Testing wc_XChacha_SetKey(). Exercises the XChaCha20 24-byte-nonce setup
659+ * (wc_HChacha_block, the two internal wc_Chacha_SetIV calls) and its
660+ * nonceSz argument check.
661+ */
662+ int test_wc_Chacha_XChachaSetKey (void )
663+ {
664+ EXPECT_DECLS ;
665+ #if defined(HAVE_CHACHA ) && defined(HAVE_XCHACHA )
666+ ChaCha enc , dec ;
667+ static const byte key [CHACHA_MAX_KEY_SZ ] = {
668+ 0x00 ,0x01 ,0x02 ,0x03 , 0x04 ,0x05 ,0x06 ,0x07 ,
669+ 0x08 ,0x09 ,0x0a ,0x0b , 0x0c ,0x0d ,0x0e ,0x0f ,
670+ 0x10 ,0x11 ,0x12 ,0x13 , 0x14 ,0x15 ,0x16 ,0x17 ,
671+ 0x18 ,0x19 ,0x1a ,0x1b , 0x1c ,0x1d ,0x1e ,0x1f
672+ };
673+ static const byte nonce [XCHACHA_NONCE_BYTES ] = {
674+ 0x00 ,0x01 ,0x02 ,0x03 , 0x04 ,0x05 ,0x06 ,0x07 ,
675+ 0x08 ,0x09 ,0x0a ,0x0b , 0x0c ,0x0d ,0x0e ,0x0f ,
676+ 0x10 ,0x11 ,0x12 ,0x13 , 0x14 ,0x15 ,0x16 ,0x17
677+ };
678+ static const byte plain [32 ] = { 0 };
679+ byte cipher [sizeof (plain )];
680+ byte roundtrip [sizeof (plain )];
681+
682+ XMEMSET (& enc , 0 , sizeof (enc ));
683+ XMEMSET (& dec , 0 , sizeof (dec ));
684+
685+ /* Valid 24-byte nonce round trip. */
686+ ExpectIntEQ (wc_XChacha_SetKey (& enc , key , sizeof (key ), nonce ,
687+ sizeof (nonce ), 0 ), 0 );
688+ ExpectIntEQ (wc_XChacha_SetKey (& dec , key , sizeof (key ), nonce ,
689+ sizeof (nonce ), 0 ), 0 );
690+ ExpectIntEQ (wc_Chacha_Process (& enc , cipher , plain , sizeof (plain )), 0 );
691+ ExpectIntEQ (wc_Chacha_Process (& dec , roundtrip , cipher , sizeof (plain )), 0 );
692+ ExpectBufEQ (roundtrip , plain , sizeof (plain ));
693+
694+ /* Bad nonceSz: independence pair for the nonceSz != XCHACHA_NONCE_BYTES
695+ * decision (single-condition, but still needs both outcomes for branch
696+ * coverage). */
697+ ExpectIntEQ (wc_XChacha_SetKey (& enc , key , sizeof (key ), nonce ,
698+ sizeof (nonce ) - 1 , 0 ), WC_NO_ERR_TRACE (BAD_FUNC_ARG ));
699+
700+ /* Bad keySz propagates the wc_Chacha_SetKey() failure through. */
701+ ExpectIntEQ (wc_XChacha_SetKey (& enc , key , 18 , nonce , sizeof (nonce ), 0 ),
702+ WC_NO_ERR_TRACE (BAD_FUNC_ARG ));
703+ #endif
704+ return EXPECT_RESULT ();
705+ } /* END test_wc_Chacha_XChachaSetKey */
0 commit comments