|
31 | 31 | #include <crypto/sha1.h>
|
32 | 32 | #include <crypto/sha2.h>
|
33 | 33 |
|
| 34 | +#ifdef CONFIG_TESTING_CRYPTO_HASH_HUGE_BLOCK |
| 35 | +# define HASH_HUGE_BLOCK_SIZE (600 * 1024) /* 600k */ |
| 36 | +#endif |
| 37 | + |
34 | 38 | typedef struct crypto_context
|
35 | 39 | {
|
36 | 40 | int fd;
|
@@ -93,7 +97,7 @@ tb sha_testcase[] =
|
93 | 97 | {
|
94 | 98 | "",
|
95 | 99 | 1000,
|
96 |
| - }, |
| 100 | + } |
97 | 101 | };
|
98 | 102 |
|
99 | 103 | tb sha512_testcase[] =
|
@@ -193,6 +197,40 @@ static const unsigned char sha512_result[3][64] =
|
193 | 197 | 0x4e, 0xad, 0xb2, 0x17, 0xad, 0x8c, 0xc0, 0x9b }
|
194 | 198 | };
|
195 | 199 |
|
| 200 | +#ifdef CONFIG_TESTING_CRYPTO_HASH_HUGE_BLOCK |
| 201 | +static const unsigned char md5_huge_block_result[16] = |
| 202 | +{ |
| 203 | + 0xef, 0x6d, 0xcc, 0xc8, 0xe1, 0xcc, 0x7f, 0x08, |
| 204 | + 0xc2, 0x71, 0xd4, 0xc4, 0xe0, 0x13, 0xa3, 0x9b |
| 205 | +}; |
| 206 | + |
| 207 | +static const unsigned char sha1_huge_block_result[20] = |
| 208 | +{ |
| 209 | + 0xf2, 0x35, 0x65, 0x81, 0x79, 0x4d, 0xac, 0x20, 0x79, 0x7b, |
| 210 | + 0xff, 0x38, 0xee, 0x2b, 0xdc, 0x44, 0x24, 0xd3, 0xf0, 0x4a |
| 211 | +}; |
| 212 | + |
| 213 | +static const unsigned char sha256_huge_block_result[32] = |
| 214 | +{ |
| 215 | + 0x79, 0xb1, 0xf2, 0x65, 0x7e, 0x33, 0x25, 0xff, |
| 216 | + 0x16, 0xdb, 0x5d, 0x3c, 0x65, 0xa4, 0x7b, 0x78, |
| 217 | + 0x0d, 0xd5, 0xa1, 0x45, 0xb5, 0x30, 0xe0, 0x91, |
| 218 | + 0x54, 0x01, 0x40, 0x0c, 0xff, 0x35, 0x1d, 0xd3 |
| 219 | +}; |
| 220 | + |
| 221 | +static const unsigned char sha512_huge_block_result[64] = |
| 222 | +{ |
| 223 | + 0xa4, 0x3a, 0x66, 0xe8, 0xf7, 0x59, 0x95, 0x6d, |
| 224 | + 0x09, 0x55, 0xdd, 0xad, 0x84, 0x7c, 0xd5, 0xe7, |
| 225 | + 0xd2, 0xbe, 0xac, 0x49, 0xa9, 0x4b, 0xa3, 0x72, |
| 226 | + 0xe1, 0x92, 0xa0, 0x70, 0x83, 0x17, 0x85, 0x5e, |
| 227 | + 0x90, 0x9e, 0x1d, 0x91, 0x6a, 0x93, 0xd9, 0xae, |
| 228 | + 0xb8, 0x1a, 0x43, 0xb5, 0x51, 0x53, 0x10, 0xf1, |
| 229 | + 0xce, 0x3a, 0xcf, 0xb6, 0x9c, 0x8b, 0x6e, 0x07, |
| 230 | + 0x13, 0xca, 0xe1, 0x94, 0x3c, 0x41, 0x50, 0xcc |
| 231 | +}; |
| 232 | +#endif |
| 233 | + |
196 | 234 | static void syshash_free(FAR crypto_context *ctx)
|
197 | 235 | {
|
198 | 236 | if (ctx->crypto_fd != 0)
|
@@ -305,6 +343,36 @@ static int match(const unsigned char *a, const unsigned char *b, size_t len)
|
305 | 343 | return 1;
|
306 | 344 | }
|
307 | 345 |
|
| 346 | +#ifdef CONFIG_TESTING_CRYPTO_HASH_HUGE_BLOCK |
| 347 | +static int testing_hash_huge_block(crypto_context *ctx, int op, |
| 348 | + FAR const unsigned char *block, size_t len, |
| 349 | + FAR const unsigned char *result, size_t reslen) |
| 350 | +{ |
| 351 | + int ret = 0; |
| 352 | + unsigned char output[64]; |
| 353 | + |
| 354 | + ret = syshash_start(ctx, op); |
| 355 | + if (ret != 0) |
| 356 | + { |
| 357 | + return ret; |
| 358 | + } |
| 359 | + |
| 360 | + ret = syshash_update(ctx, (char *)block, len); |
| 361 | + if (ret != 0) |
| 362 | + { |
| 363 | + return ret; |
| 364 | + } |
| 365 | + |
| 366 | + ret = syshash_finish(ctx, output); |
| 367 | + if (ret != 0) |
| 368 | + { |
| 369 | + return ret; |
| 370 | + } |
| 371 | + |
| 372 | + return match(result, output, reslen); |
| 373 | +} |
| 374 | +#endif |
| 375 | + |
308 | 376 | /****************************************************************************
|
309 | 377 | * Public Functions
|
310 | 378 | ****************************************************************************/
|
@@ -529,11 +597,75 @@ int main(void)
|
529 | 597 | }
|
530 | 598 | }
|
531 | 599 |
|
| 600 | +#ifdef CONFIG_TESTING_CRYPTO_HASH_HUGE_BLOCK |
| 601 | + unsigned char *huge_block; |
| 602 | + huge_block = (unsigned char *)malloc(HASH_HUGE_BLOCK_SIZE); |
| 603 | + if (huge_block == NULL) |
| 604 | + { |
| 605 | + printf("huge block test no memory\n"); |
| 606 | + goto err; |
| 607 | + } |
| 608 | + |
| 609 | + memset(huge_block, 'a', HASH_HUGE_BLOCK_SIZE); |
| 610 | + ret = testing_hash_huge_block(&md5_ctx, CRYPTO_MD5, |
| 611 | + huge_block, HASH_HUGE_BLOCK_SIZE, |
| 612 | + md5_huge_block_result, |
| 613 | + MD5_DIGEST_LENGTH); |
| 614 | + if (ret != 0) |
| 615 | + { |
| 616 | + printf("md5 huge block test failed\n"); |
| 617 | + } |
| 618 | + else |
| 619 | + { |
| 620 | + printf("md5 huge block test success\n"); |
| 621 | + } |
| 622 | + |
| 623 | + ret = testing_hash_huge_block(&sha1_ctx, CRYPTO_SHA1, |
| 624 | + huge_block, HASH_HUGE_BLOCK_SIZE, |
| 625 | + sha1_huge_block_result, |
| 626 | + SHA1_DIGEST_LENGTH); |
| 627 | + if (ret != 0) |
| 628 | + { |
| 629 | + printf("sha1 huge block test failed\n"); |
| 630 | + } |
| 631 | + else |
| 632 | + { |
| 633 | + printf("sha1 huge block test success\n"); |
| 634 | + } |
| 635 | + |
| 636 | + ret = testing_hash_huge_block(&sha2_256_ctx, CRYPTO_SHA2_256, |
| 637 | + huge_block, HASH_HUGE_BLOCK_SIZE, |
| 638 | + sha256_huge_block_result, |
| 639 | + SHA256_DIGEST_LENGTH); |
| 640 | + if (ret != 0) |
| 641 | + { |
| 642 | + printf("sha256 huge block test failed\n"); |
| 643 | + } |
| 644 | + else |
| 645 | + { |
| 646 | + printf("sha256 huge block test success\n"); |
| 647 | + } |
| 648 | + |
| 649 | + ret = testing_hash_huge_block(&sha2_512_ctx, CRYPTO_SHA2_512, |
| 650 | + huge_block, HASH_HUGE_BLOCK_SIZE, |
| 651 | + sha512_huge_block_result, |
| 652 | + SHA512_DIGEST_LENGTH); |
| 653 | + if (ret != 0) |
| 654 | + { |
| 655 | + printf("sha512 huge block test failed\n"); |
| 656 | + } |
| 657 | + else |
| 658 | + { |
| 659 | + printf("sha512 huge block test success\n"); |
| 660 | + } |
| 661 | + |
| 662 | + free(huge_block); |
| 663 | +#endif |
| 664 | + |
532 | 665 | err:
|
533 | 666 | syshash_free(&md5_ctx);
|
534 | 667 | syshash_free(&sha1_ctx);
|
535 | 668 | syshash_free(&sha2_256_ctx);
|
536 | 669 | syshash_free(&sha2_512_ctx);
|
537 |
| - |
538 | 670 | return 0;
|
539 | 671 | }
|
0 commit comments