diff options
author | tb <> | 2022-09-02 11:13:34 +0000 |
---|---|---|
committer | tb <> | 2022-09-02 11:13:34 +0000 |
commit | ed0eb63efa2ae52cba0d728b20cff3ca87712b83 (patch) | |
tree | 58566c5ee77e2be807236c69ba0b34b715d5ce42 | |
parent | 4920b2737b931329f22a832f32d3dcd07c0ac460 (diff) | |
download | openbsd-ed0eb63efa2ae52cba0d728b20cff3ca87712b83.tar.gz openbsd-ed0eb63efa2ae52cba0d728b20cff3ca87712b83.tar.bz2 openbsd-ed0eb63efa2ae52cba0d728b20cff3ca87712b83.zip |
Add tests for 1 million repeated "a"
From Joshua Sing
-rw-r--r-- | src/regress/lib/libcrypto/sha/sha_test.c | 259 |
1 files changed, 220 insertions, 39 deletions
diff --git a/src/regress/lib/libcrypto/sha/sha_test.c b/src/regress/lib/libcrypto/sha/sha_test.c index cf954aa50c..0d823a3db8 100644 --- a/src/regress/lib/libcrypto/sha/sha_test.c +++ b/src/regress/lib/libcrypto/sha/sha_test.c | |||
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: sha_test.c,v 1.1 2022/09/01 14:02:41 tb Exp $ */ | 1 | /* $OpenBSD: sha_test.c,v 1.2 2022/09/02 11:13:34 tb Exp $ */ |
2 | /* | 2 | /* |
3 | * Copyright (c) 2022 Joshua Sing <joshua@hypera.dev> | 3 | * Copyright (c) 2022 Joshua Sing <joshua@hypera.dev> |
4 | * | 4 | * |
@@ -101,7 +101,8 @@ struct sha_test sha_tests[] = { | |||
101 | }, | 101 | }, |
102 | { | 102 | { |
103 | .algorithm = NID_sha224, | 103 | .algorithm = NID_sha224, |
104 | .in = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmno" | 104 | .in = |
105 | "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmno" | ||
105 | "mnopnopq", | 106 | "mnopnopq", |
106 | .in_len = 56, | 107 | .in_len = 56, |
107 | .out = { | 108 | .out = { |
@@ -304,12 +305,154 @@ struct sha_test sha_tests[] = { | |||
304 | }, | 305 | }, |
305 | }; | 306 | }; |
306 | 307 | ||
308 | struct sha_repetition_test | ||
309 | { | ||
310 | const int algorithm; | ||
311 | const uint8_t in; | ||
312 | const size_t in_repetitions; | ||
313 | const uint8_t out[EVP_MAX_MD_SIZE]; | ||
314 | }; | ||
315 | |||
316 | struct sha_repetition_test sha_repetition_tests[] = { | ||
317 | /* SHA-1 */ | ||
318 | { | ||
319 | .algorithm = NID_sha1, | ||
320 | .in = 'a', | ||
321 | .in_repetitions = 1000000, | ||
322 | .out = { | ||
323 | 0x34, 0xaa, 0x97, 0x3c, 0xd4, 0xc4, 0xda, 0xa4, | ||
324 | 0xf6, 0x1e, 0xeb, 0x2b, 0xdb, 0xad, 0x27, 0x31, | ||
325 | 0x65, 0x34, 0x01, 0x6f, | ||
326 | } | ||
327 | }, | ||
328 | |||
329 | /* SHA-224 */ | ||
330 | { | ||
331 | .algorithm = NID_sha224, | ||
332 | .in = 'a', | ||
333 | .in_repetitions = 1000000, | ||
334 | .out = { | ||
335 | 0x20, 0x79, 0x46, 0x55, 0x98, 0x0c, 0x91, 0xd8, | ||
336 | 0xbb, 0xb4, 0xc1, 0xea, 0x97, 0x61, 0x8a, 0x4b, | ||
337 | 0xf0, 0x3f, 0x42, 0x58, 0x19, 0x48, 0xb2, 0xee, | ||
338 | 0x4e, 0xe7, 0xad, 0x67, | ||
339 | } | ||
340 | }, | ||
341 | |||
342 | /* SHA-256 */ | ||
343 | { | ||
344 | .algorithm = NID_sha256, | ||
345 | .in = 'a', | ||
346 | .in_repetitions = 1000000, | ||
347 | .out = { | ||
348 | 0xcd, 0xc7, 0x6e, 0x5c, 0x99, 0x14, 0xfb, 0x92, | ||
349 | 0x81, 0xa1, 0xc7, 0xe2, 0x84, 0xd7, 0x3e, 0x67, | ||
350 | 0xf1, 0x80, 0x9a, 0x48, 0xa4, 0x97, 0x20, 0x0e, | ||
351 | 0x04, 0x6d, 0x39, 0xcc, 0xc7, 0x11, 0x2c, 0xd0, | ||
352 | } | ||
353 | }, | ||
354 | |||
355 | /* SHA-384 */ | ||
356 | { | ||
357 | .algorithm = NID_sha384, | ||
358 | .in = 'a', | ||
359 | .in_repetitions = 1000000, | ||
360 | .out = { | ||
361 | 0x9d, 0x0e, 0x18, 0x09, 0x71, 0x64, 0x74, 0xcb, | ||
362 | 0x08, 0x6e, 0x83, 0x4e, 0x31, 0x0a, 0x4a, 0x1c, | ||
363 | 0xed, 0x14, 0x9e, 0x9c, 0x00, 0xf2, 0x48, 0x52, | ||
364 | 0x79, 0x72, 0xce, 0xc5, 0x70, 0x4c, 0x2a, 0x5b, | ||
365 | 0x07, 0xb8, 0xb3, 0xdc, 0x38, 0xec, 0xc4, 0xeb, | ||
366 | 0xae, 0x97, 0xdd, 0xd8, 0x7f, 0x3d, 0x89, 0x85, | ||
367 | } | ||
368 | }, | ||
369 | |||
370 | /* SHA-512 */ | ||
371 | { | ||
372 | .algorithm = NID_sha512, | ||
373 | .in = 'a', | ||
374 | .in_repetitions = 1000000, | ||
375 | .out = { | ||
376 | 0xe7, 0x18, 0x48, 0x3d, 0x0c, 0xe7, 0x69, 0x64, | ||
377 | 0x4e, 0x2e, 0x42, 0xc7, 0xbc, 0x15, 0xb4, 0x63, | ||
378 | 0x8e, 0x1f, 0x98, 0xb1, 0x3b, 0x20, 0x44, 0x28, | ||
379 | 0x56, 0x32, 0xa8, 0x03, 0xaf, 0xa9, 0x73, 0xeb, | ||
380 | 0xde, 0x0f, 0xf2, 0x44, 0x87, 0x7e, 0xa6, 0x0a, | ||
381 | 0x4c, 0xb0, 0x43, 0x2c, 0xe5, 0x77, 0xc3, 0x1b, | ||
382 | 0xeb, 0x00, 0x9c, 0x5c, 0x2c, 0x49, 0xaa, 0x2e, | ||
383 | 0x4e, 0xad, 0xb2, 0x17, 0xad, 0x8c, 0xc0, 0x9b, | ||
384 | } | ||
385 | }, | ||
386 | }; | ||
387 | |||
307 | #define N_SHA_TESTS (sizeof(sha_tests) / sizeof(sha_tests[0])) | 388 | #define N_SHA_TESTS (sizeof(sha_tests) / sizeof(sha_tests[0])) |
389 | #define N_SHA_REPETITION_TESTS (sizeof(sha_repetition_tests) / sizeof(sha_repetition_tests[0])) | ||
390 | |||
391 | typedef unsigned char *(*sha_hash_func)(const unsigned char *, size_t, | ||
392 | unsigned char *); | ||
393 | |||
394 | static int | ||
395 | sha_hash_from_algorithm(int algorithm, const char **out_label, | ||
396 | sha_hash_func *out_func, const EVP_MD **out_md, size_t *out_len) | ||
397 | { | ||
398 | const char *label; | ||
399 | sha_hash_func sha_func; | ||
400 | const EVP_MD *md; | ||
401 | size_t len; | ||
402 | |||
403 | switch (algorithm) { | ||
404 | case NID_sha1: | ||
405 | label = SN_sha1; | ||
406 | sha_func = SHA1; | ||
407 | md = EVP_sha1(); | ||
408 | len = SHA_DIGEST_LENGTH; | ||
409 | break; | ||
410 | case NID_sha224: | ||
411 | label = SN_sha224; | ||
412 | sha_func = SHA224; | ||
413 | md = EVP_sha224(); | ||
414 | len = SHA224_DIGEST_LENGTH; | ||
415 | break; | ||
416 | case NID_sha256: | ||
417 | label = SN_sha256; | ||
418 | sha_func = SHA256; | ||
419 | md = EVP_sha256(); | ||
420 | len = SHA256_DIGEST_LENGTH; | ||
421 | break; | ||
422 | case NID_sha384: | ||
423 | label = SN_sha384; | ||
424 | sha_func = SHA384; | ||
425 | md = EVP_sha384(); | ||
426 | len = SHA384_DIGEST_LENGTH; | ||
427 | break; | ||
428 | case NID_sha512: | ||
429 | label = SN_sha512; | ||
430 | sha_func = SHA512; | ||
431 | md = EVP_sha512(); | ||
432 | len = SHA512_DIGEST_LENGTH; | ||
433 | break; | ||
434 | default: | ||
435 | fprintf(stderr, "FAIL: unknown algorithm (%d)\n", | ||
436 | algorithm); | ||
437 | return 0; | ||
438 | } | ||
439 | |||
440 | if (out_label != NULL) | ||
441 | *out_label = label; | ||
442 | if (out_func != NULL) | ||
443 | *out_func = sha_func; | ||
444 | if (out_md != NULL) | ||
445 | *out_md = md; | ||
446 | if (out_len != NULL) | ||
447 | *out_len = len; | ||
448 | |||
449 | return 1; | ||
450 | } | ||
308 | 451 | ||
309 | static int | 452 | static int |
310 | sha_test(void) | 453 | sha_test(void) |
311 | { | 454 | { |
312 | unsigned char *(*sha_func)(const unsigned char *, size_t, unsigned char *); | 455 | sha_hash_func sha_func; |
313 | struct sha_test *st; | 456 | struct sha_test *st; |
314 | EVP_MD_CTX *hash = NULL; | 457 | EVP_MD_CTX *hash = NULL; |
315 | const EVP_MD *md; | 458 | const EVP_MD *md; |
@@ -326,42 +469,9 @@ sha_test(void) | |||
326 | 469 | ||
327 | for (i = 0; i < N_SHA_TESTS; i++) { | 470 | for (i = 0; i < N_SHA_TESTS; i++) { |
328 | st = &sha_tests[i]; | 471 | st = &sha_tests[i]; |
329 | switch (st->algorithm) { | 472 | if (!sha_hash_from_algorithm(st->algorithm, &label, &sha_func, |
330 | case NID_sha1: | 473 | &md, &out_len)) |
331 | sha_func = SHA1; | ||
332 | md = EVP_sha1(); | ||
333 | out_len = SHA_DIGEST_LENGTH; | ||
334 | label = SN_sha1; | ||
335 | break; | ||
336 | case NID_sha224: | ||
337 | sha_func = SHA224; | ||
338 | md = EVP_sha224(); | ||
339 | out_len = SHA224_DIGEST_LENGTH; | ||
340 | label = SN_sha224; | ||
341 | break; | ||
342 | case NID_sha256: | ||
343 | sha_func = SHA256; | ||
344 | md = EVP_sha256(); | ||
345 | out_len = SHA256_DIGEST_LENGTH; | ||
346 | label = SN_sha256; | ||
347 | break; | ||
348 | case NID_sha384: | ||
349 | sha_func = SHA384; | ||
350 | md = EVP_sha384(); | ||
351 | out_len = SHA384_DIGEST_LENGTH; | ||
352 | label = SN_sha384; | ||
353 | break; | ||
354 | case NID_sha512: | ||
355 | sha_func = SHA512; | ||
356 | md = EVP_sha512(); | ||
357 | out_len = SHA512_DIGEST_LENGTH; | ||
358 | label = SN_sha512; | ||
359 | break; | ||
360 | default: | ||
361 | fprintf(stderr, "FAIL: unknown algorithm (%d)\n", | ||
362 | st->algorithm); | ||
363 | goto failed; | 474 | goto failed; |
364 | } | ||
365 | 475 | ||
366 | /* Digest */ | 476 | /* Digest */ |
367 | memset(out, 0, sizeof(out)); | 477 | memset(out, 0, sizeof(out)); |
@@ -374,7 +484,8 @@ sha_test(void) | |||
374 | /* EVP single-shot digest */ | 484 | /* EVP single-shot digest */ |
375 | memset(out, 0, sizeof(out)); | 485 | memset(out, 0, sizeof(out)); |
376 | if (!EVP_Digest(st->in, st->in_len, out, NULL, md, NULL)) { | 486 | if (!EVP_Digest(st->in, st->in_len, out, NULL, md, NULL)) { |
377 | fprintf(stderr, "FAIL (%s): EVP_Digest failed\n", label); | 487 | fprintf(stderr, "FAIL (%s): EVP_Digest failed\n", |
488 | label); | ||
378 | goto failed; | 489 | goto failed; |
379 | } | 490 | } |
380 | 491 | ||
@@ -428,12 +539,82 @@ sha_test(void) | |||
428 | return failed; | 539 | return failed; |
429 | } | 540 | } |
430 | 541 | ||
542 | static int | ||
543 | sha_repetition_test(void) | ||
544 | { | ||
545 | struct sha_repetition_test *st; | ||
546 | EVP_MD_CTX *hash = NULL; | ||
547 | const EVP_MD *md; | ||
548 | uint8_t buf[1024]; | ||
549 | uint8_t out[EVP_MAX_MD_SIZE]; | ||
550 | size_t out_len, part_len; | ||
551 | size_t i, j; | ||
552 | const char *label; | ||
553 | int failed = 1; | ||
554 | |||
555 | if ((hash = EVP_MD_CTX_new()) == NULL) { | ||
556 | fprintf(stderr, "FAIL: EVP_MD_CTX_new() failed\n"); | ||
557 | goto failed; | ||
558 | } | ||
559 | |||
560 | for (i = 0; i < N_SHA_REPETITION_TESTS; i++) { | ||
561 | st = &sha_repetition_tests[i]; | ||
562 | if (!sha_hash_from_algorithm(st->algorithm, &label, NULL, &md, | ||
563 | &out_len)) | ||
564 | goto failed; | ||
565 | |||
566 | /* EVP digest */ | ||
567 | if (!EVP_DigestInit_ex(hash, md, NULL)) { | ||
568 | fprintf(stderr, | ||
569 | "FAIL (%s): EVP_DigestInit_ex failed\n", | ||
570 | label); | ||
571 | goto failed; | ||
572 | } | ||
573 | |||
574 | memset(buf, st->in, sizeof(buf)); | ||
575 | |||
576 | for (j = 0; j < st->in_repetitions;) { | ||
577 | part_len = arc4random_uniform(sizeof(buf)); | ||
578 | if (part_len > st->in_repetitions - j) | ||
579 | part_len = st->in_repetitions - j; | ||
580 | |||
581 | if (!EVP_DigestUpdate(hash, buf, part_len)) { | ||
582 | fprintf(stderr, | ||
583 | "FAIL (%s): EVP_DigestUpdate failed\n", | ||
584 | label); | ||
585 | goto failed; | ||
586 | } | ||
587 | |||
588 | j += part_len; | ||
589 | } | ||
590 | |||
591 | if (!EVP_DigestFinal_ex(hash, out, NULL)) { | ||
592 | fprintf(stderr, | ||
593 | "FAIL (%s): EVP_DigestFinal_ex failed\n", | ||
594 | label); | ||
595 | goto failed; | ||
596 | } | ||
597 | |||
598 | if (memcmp(st->out, out, out_len) != 0) { | ||
599 | fprintf(stderr, "FAIL (%s): EVP mismatch\n", label); | ||
600 | goto failed; | ||
601 | } | ||
602 | } | ||
603 | |||
604 | failed = 0; | ||
605 | |||
606 | failed: | ||
607 | EVP_MD_CTX_free(hash); | ||
608 | return failed; | ||
609 | } | ||
610 | |||
431 | int | 611 | int |
432 | main(int argc, char **argv) | 612 | main(int argc, char **argv) |
433 | { | 613 | { |
434 | int failed = 0; | 614 | int failed = 0; |
435 | 615 | ||
436 | failed |= sha_test(); | 616 | failed |= sha_test(); |
617 | failed |= sha_repetition_test(); | ||
437 | 618 | ||
438 | return failed; | 619 | return failed; |
439 | } | 620 | } |