@@ -160,6 +160,118 @@ int test_gmac_create(void *data)
160160 return ret ;
161161}
162162
163- #endif /* WP_HAVE_GMAC */
163+ int test_gmac_dup (void * data )
164+ {
165+ int ret = 0 ;
166+ EVP_MAC * emac = NULL ;
167+ EVP_MAC_CTX * src = NULL ;
168+ EVP_MAC_CTX * dup = NULL ;
169+ OSSL_PARAM params [4 ];
170+ char cipher [] = "AES-256-GCM" ;
171+ unsigned char key [] = {
172+ 0x00 , 0x01 , 0x02 , 0x03 , 0x04 , 0x05 , 0x06 , 0x07 ,
173+ 0x00 , 0x01 , 0x02 , 0x03 , 0x04 , 0x05 , 0x06 , 0x07 ,
174+ 0x00 , 0x01 , 0x02 , 0x03 , 0x04 , 0x05 , 0x06 , 0x07 ,
175+ 0x00 , 0x01 , 0x02 , 0x03 , 0x04 , 0x05 , 0x06 , 0x07
176+ };
177+ unsigned char iv [] = {
178+ 0x00 , 0x01 , 0x02 , 0x03 , 0x04 , 0x05 , 0x06 , 0x07 ,
179+ 0x00 , 0x01 , 0x02 , 0x03
180+ };
181+ unsigned char prefix [] = "dup-prefix" ;
182+ unsigned char tailA [] = "-tail-a" ;
183+ unsigned char tailB [] = "-tail-b" ;
184+ unsigned char msgA [sizeof (prefix ) + sizeof (tailA )];
185+ unsigned char msgB [sizeof (prefix ) + sizeof (tailB )];
186+ unsigned char macA [16 ];
187+ unsigned char macB [16 ];
188+ unsigned char expA [16 ];
189+ unsigned char expB [16 ];
190+ size_t macASz = sizeof (macA );
191+ size_t macBSz = sizeof (macB );
192+ int expASz = sizeof (expA );
193+ int expBSz = sizeof (expB );
194+
195+ (void )data ;
196+
197+ /* Build full messages used for one-shot expected MAC calculations. */
198+ XMEMCPY (msgA , prefix , sizeof (prefix ));
199+ XMEMCPY (msgA + sizeof (prefix ), tailA , sizeof (tailA ));
200+ XMEMCPY (msgB , prefix , sizeof (prefix ));
201+ XMEMCPY (msgB + sizeof (prefix ), tailB , sizeof (tailB ));
202+
203+ /* Compute expected MACs for each post-duplication branch. */
204+ ret = test_gmac_gen_mac (wpLibCtx , cipher , iv , (int )sizeof (iv ), key ,
205+ (int )sizeof (key ), msgA , (int )sizeof (msgA ), expA , & expASz );
206+ if (ret != 0 ) {
207+ PRINT_MSG ("Generate expected MAC A failed" );
208+ }
209+ if (ret == 0 ) {
210+ ret = test_gmac_gen_mac (wpLibCtx , cipher , iv , (int )sizeof (iv ),
211+ key , (int )sizeof (key ), msgB , (int )sizeof (msgB ), expB , & expBSz );
212+ if (ret != 0 ) {
213+ PRINT_MSG ("Generate expected MAC B failed" );
214+ }
215+ }
216+
217+ params [0 ] = OSSL_PARAM_construct_utf8_string (OSSL_MAC_PARAM_CIPHER ,
218+ cipher , 0 );
219+ params [1 ] = OSSL_PARAM_construct_octet_string (OSSL_MAC_PARAM_KEY ,
220+ (void * )key , sizeof (key ));
221+ params [2 ] = OSSL_PARAM_construct_octet_string (OSSL_MAC_PARAM_IV ,
222+ (void * )iv , sizeof (iv ));
223+ params [3 ] = OSSL_PARAM_construct_end ();
224+
225+ if (ret == 0 ) {
226+ ret = (emac = EVP_MAC_fetch (wpLibCtx , "GMAC" , NULL )) == NULL ;
227+ }
228+ if (ret == 0 ) {
229+ ret = (src = EVP_MAC_CTX_new (emac )) == NULL ;
230+ }
231+ if (ret == 0 ) {
232+ ret = EVP_MAC_CTX_set_params (src , params ) != 1 ;
233+ }
234+ if (ret == 0 ) {
235+ ret = EVP_MAC_init (src , NULL , 0 , NULL ) != 1 ;
236+ }
237+ if (ret == 0 ) {
238+ ret = EVP_MAC_update (src , prefix , sizeof (prefix )) != 1 ;
239+ }
240+ /* Duplicate after partial update so both contexts start from same state. */
241+ if (ret == 0 ) {
242+ ret = (dup = EVP_MAC_CTX_dup (src )) == NULL ;
243+ }
244+ if (ret == 0 ) {
245+ ret = EVP_MAC_update (src , tailA , sizeof (tailA )) != 1 ;
246+ }
247+ if (ret == 0 ) {
248+ ret = EVP_MAC_update (dup , tailB , sizeof (tailB )) != 1 ;
249+ }
250+ if (ret == 0 ) {
251+ ret = EVP_MAC_final (src , macA , & macASz , sizeof (macA )) != 1 ;
252+ }
253+ if (ret == 0 ) {
254+ ret = EVP_MAC_final (dup , macB , & macBSz , sizeof (macB )) != 1 ;
255+ }
256+ /* Verify each branch matches its independently generated expected MAC. */
257+ if (ret == 0 ) {
258+ if ((macASz != (size_t )expASz ) || (memcmp (macA , expA , macASz ) != 0 )) {
259+ PRINT_MSG ("Duplicated source context MAC mismatch" );
260+ ret = -1 ;
261+ }
262+ }
263+ if (ret == 0 ) {
264+ if ((macBSz != (size_t )expBSz ) || (memcmp (macB , expB , macBSz ) != 0 )) {
265+ PRINT_MSG ("Duplicated destination context MAC mismatch" );
266+ ret = -1 ;
267+ }
268+ }
164269
270+ EVP_MAC_CTX_free (dup );
271+ EVP_MAC_CTX_free (src );
272+ EVP_MAC_free (emac );
165273
274+ return ret ;
275+ }
276+
277+ #endif /* WP_HAVE_GMAC */
0 commit comments