@@ -272,6 +272,56 @@ START_TEST(test_large_data_symmetric)
272272}
273273END_TEST
274274
275+ void increment_nonce_number_cmp (uint8_t * nonce , uint32_t num )
276+ {
277+ uint32_t num1 , num2 ;
278+ memcpy (& num1 , nonce + (crypto_box_NONCEBYTES - sizeof (num1 )), sizeof (num1 ));
279+ num1 = ntohl (num1 );
280+ num2 = num + num1 ;
281+
282+ if (num2 < num1 ) {
283+ uint32_t i ;
284+
285+ for (i = crypto_box_NONCEBYTES - sizeof (num1 ); i != 0 ; -- i ) {
286+ ++ nonce [i - 1 ];
287+
288+ if (nonce [i - 1 ] != 0 )
289+ break ;
290+ }
291+ }
292+
293+ num2 = htonl (num2 );
294+ memcpy (nonce + (crypto_box_NONCEBYTES - sizeof (num2 )), & num2 , sizeof (num2 ));
295+ }
296+
297+ START_TEST (test_increment_nonce )
298+ {
299+ long long unsigned int i ;
300+
301+ uint8_t n [crypto_box_NONCEBYTES ];
302+
303+ for (i = 0 ; i < crypto_box_NONCEBYTES ; ++ i )
304+ n [i ] = rand ();
305+
306+ uint8_t n1 [crypto_box_NONCEBYTES ];
307+
308+ memcpy (n1 , n , crypto_box_NONCEBYTES );
309+
310+ for (i = 0 ; i < (1 << 18 ); ++ i ) {
311+ increment_nonce_number_cmp (n , 1 );
312+ increment_nonce (n1 );
313+ ck_assert_msg (memcmp (n , n1 , crypto_box_NONCEBYTES ) == 0 , "Bad increment_nonce function" );
314+ }
315+
316+ for (i = 0 ; i < (1 << 18 ); ++ i ) {
317+ uint32_t r = rand ();
318+ increment_nonce_number_cmp (n , r );
319+ increment_nonce_number (n1 , r );
320+ ck_assert_msg (memcmp (n , n1 , crypto_box_NONCEBYTES ) == 0 , "Bad increment_nonce_number function" );
321+ }
322+ }
323+ END_TEST
324+
275325Suite * crypto_suite (void )
276326{
277327 Suite * s = suite_create ("Crypto" );
@@ -281,6 +331,7 @@ Suite *crypto_suite(void)
281331 DEFTESTCASE_SLOW (endtoend , 15 ); /* waiting up to 15 seconds */
282332 DEFTESTCASE (large_data );
283333 DEFTESTCASE (large_data_symmetric );
334+ DEFTESTCASE_SLOW (increment_nonce , 20 );
284335
285336 return s ;
286337}
0 commit comments