summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--auto_tests/crypto_test.c51
1 files changed, 51 insertions, 0 deletions
diff --git a/auto_tests/crypto_test.c b/auto_tests/crypto_test.c
index 2fb08b8b..4a77b49a 100644
--- a/auto_tests/crypto_test.c
+++ b/auto_tests/crypto_test.c
@@ -272,6 +272,56 @@ START_TEST(test_large_data_symmetric)
272} 272}
273END_TEST 273END_TEST
274 274
275void 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
297START_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}
323END_TEST
324
275Suite *crypto_suite(void) 325Suite *crypto_suite(void)
276{ 326{
277 Suite *s = suite_create("Crypto"); 327 Suite *s = suite_create("Crypto");
@@ -281,6 +331,7 @@ Suite *crypto_suite(void)
281 DEFTESTCASE_SLOW(endtoend, 15); /* waiting up to 15 seconds */ 331 DEFTESTCASE_SLOW(endtoend, 15); /* waiting up to 15 seconds */
282 DEFTESTCASE(large_data); 332 DEFTESTCASE(large_data);
283 DEFTESTCASE(large_data_symmetric); 333 DEFTESTCASE(large_data_symmetric);
334 DEFTESTCASE_SLOW(increment_nonce, 20);
284 335
285 return s; 336 return s;
286} 337}