1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824
|
/* test_hash.c
*
* Copyright (C) 2006-2025 wolfSSL Inc.
*
* This file is part of wolfSSL.
*
* wolfSSL is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* wolfSSL is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
*/
#include <tests/unit.h>
#ifdef NO_INLINE
#include <wolfssl/wolfcrypt/misc.h>
#else
#define WOLFSSL_MISC_INCLUDED
#include <wolfcrypt/src/misc.c>
#endif
#include <wolfssl/wolfcrypt/hash.h>
#include <wolfssl/wolfcrypt/types.h>
#include <tests/api/api.h>
#include <tests/api/test_hash.h>
#include <tests/api/test_digest.h>
#ifndef NO_HASH_WRAPPER
/* enum for holding supported algorithms, #ifndef's restrict if disabled */
static const enum wc_HashType supportedHash[] = {
#ifndef NO_MD5
WC_HASH_TYPE_MD5,
#endif
#ifndef NO_SHA
WC_HASH_TYPE_SHA,
#endif
#ifdef WOLFSSL_SHA224
WC_HASH_TYPE_SHA224,
#endif
#ifndef NO_SHA256
WC_HASH_TYPE_SHA256,
#endif
#ifdef WOLFSSL_SHA384
WC_HASH_TYPE_SHA384,
#endif
#ifdef WOLFSSL_SHA512
WC_HASH_TYPE_SHA512,
#if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
#ifndef WOLFSSL_NOSHA512_224
WC_HASH_TYPE_SHA512_224,
#endif
#ifndef WOLFSSL_NOSHA512_256
WC_HASH_TYPE_SHA512_256,
#endif
#endif
#endif
#ifdef WOLFSSL_SHA3
WC_HASH_TYPE_SHA3_224,
WC_HASH_TYPE_SHA3_256,
WC_HASH_TYPE_SHA3_384,
WC_HASH_TYPE_SHA3_512,
#endif
#ifdef WOLFSSL_SM3
WC_HASH_TYPE_SM3,
#endif
WC_HASH_TYPE_NONE /* Dummy value to ensure list is non-zero. */
};
static const int supportedHashLen = (sizeof(supportedHash) /
sizeof(enum wc_HashType)) - 1;
static const enum wc_HashType notCompiledHash[] = {
#ifdef NO_MD5
WC_HASH_TYPE_MD5,
#endif
#ifdef NO_SHA
WC_HASH_TYPE_SHA,
#endif
#ifndef WOLFSSL_SHA224
WC_HASH_TYPE_SHA224,
#endif
#ifdef NO_SHA256
WC_HASH_TYPE_SHA256,
#endif
#ifndef WOLFSSL_SHA384
WC_HASH_TYPE_SHA384,
#endif
#ifndef WOLFSSL_SHA512
WC_HASH_TYPE_SHA512,
#endif
#ifndef WOLFSSL_SHA3
WC_HASH_TYPE_SHA3_224,
WC_HASH_TYPE_SHA3_256,
WC_HASH_TYPE_SHA3_384,
WC_HASH_TYPE_SHA3_512,
#endif
WC_HASH_TYPE_NONE /* Dummy value to ensure list is non-zero. */
};
static const int notCompiledHashLen = (sizeof(notCompiledHash) /
sizeof(enum wc_HashType)) - 1;
static const enum wc_HashType notSupportedHash[] = {
#if defined(WOLFSSL_SHA3) && defined(WOLFSSL_SHAKE128)
WC_HASH_TYPE_SHAKE128,
#endif
#if defined(WOLFSSL_SHA3) && defined(WOLFSSL_SHAKE256)
WC_HASH_TYPE_SHAKE256,
#endif
WC_HASH_TYPE_MD5_SHA,
WC_HASH_TYPE_MD2,
WC_HASH_TYPE_MD4,
WC_HASH_TYPE_BLAKE2B,
WC_HASH_TYPE_BLAKE2S,
WC_HASH_TYPE_NONE
};
static const int notSupportedHashLen = (sizeof(notSupportedHash) /
sizeof(enum wc_HashType));
static const enum wc_HashType sizeSupportedHash[] = {
#if !defined(NO_MD5) && !defined(NO_SHA)
WC_HASH_TYPE_MD5_SHA,
#endif
#ifdef WOLFSSL_MD2
WC_HASH_TYPE_MD2,
#endif
#ifndef NO_MD4
WC_HASH_TYPE_MD4,
#endif
#if defined(HAVE_BLAKE2) || defined(HAVE_BLAKE2S)
WC_HASH_TYPE_BLAKE2B,
WC_HASH_TYPE_BLAKE2S,
#endif
WC_HASH_TYPE_NONE /* Dummy value to ensure list is non-zero. */
};
static const int sizeSupportedHashLen = (sizeof(sizeSupportedHash) /
sizeof(enum wc_HashType)) - 1;
static const enum wc_HashType sizeNotCompiledHash[] = {
#if defined(NO_MD5) || defined(NO_SHA)
WC_HASH_TYPE_MD5_SHA,
#endif
#ifndef WOLFSSL_MD2
WC_HASH_TYPE_MD2,
#endif
#ifdef NO_MD4
WC_HASH_TYPE_MD4,
#endif
#if !defined(HAVE_BLAKE2) && !defined(HAVE_BLAKE2S)
WC_HASH_TYPE_BLAKE2B,
WC_HASH_TYPE_BLAKE2S,
#endif
WC_HASH_TYPE_NONE /* Dummy value to ensure list is non-zero. */
};
static const int sizeNotCompiledHashLen = (sizeof(sizeNotCompiledHash) /
sizeof(enum wc_HashType)) - 1;
static const enum wc_HashType sizeNotSupportedHash[] = {
#if defined(WOLFSSL_SHA3) && defined(WOLFSSL_SHAKE128)
WC_HASH_TYPE_SHAKE128,
#endif
#if defined(WOLFSSL_SHA3) && defined(WOLFSSL_SHAKE256)
WC_HASH_TYPE_SHAKE256,
#endif
WC_HASH_TYPE_NONE
};
static const int sizeNotSupportedHashLen = (sizeof(sizeNotSupportedHash) /
sizeof(enum wc_HashType));
#endif /* NO_HASH_WRAPPER */
int test_wc_HashInit(void)
{
EXPECT_DECLS;
#ifndef NO_HASH_WRAPPER
wc_HashAlg hash;
int i; /* 0 indicates tests passed, 1 indicates failure */
/* For loop to test various arguments... */
for (i = 0; i < supportedHashLen; i++) {
/* check for null ptr */
ExpectIntEQ(wc_HashInit(NULL, supportedHash[i]),
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
ExpectIntEQ(wc_HashInit_ex(NULL, supportedHash[i], HEAP_HINT,
INVALID_DEVID), WC_NO_ERR_TRACE(BAD_FUNC_ARG));
ExpectIntEQ(wc_HashInit(&hash, supportedHash[i]), 0);
wc_HashFree(&hash, supportedHash[i]);
ExpectIntEQ(wc_HashInit_ex(&hash, supportedHash[i], HEAP_HINT,
INVALID_DEVID), 0);
wc_HashFree(&hash, supportedHash[i]);
wc_HashFree(NULL, supportedHash[i]);
} /* end of for loop */
for (i = 0; i < notCompiledHashLen; i++) {
/* check for null ptr */
ExpectIntEQ(wc_HashInit(NULL, notCompiledHash[i]),
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
ExpectIntEQ(wc_HashInit_ex(NULL, notCompiledHash[i], HEAP_HINT,
INVALID_DEVID), WC_NO_ERR_TRACE(BAD_FUNC_ARG));
ExpectIntEQ(wc_HashInit(&hash, notCompiledHash[i]),
WC_NO_ERR_TRACE(HASH_TYPE_E));
ExpectIntEQ(wc_HashInit_ex(&hash, notCompiledHash[i], HEAP_HINT,
INVALID_DEVID), WC_NO_ERR_TRACE(HASH_TYPE_E));
wc_HashFree(NULL, notCompiledHash[i]);
}
for (i = 0; i < notSupportedHashLen; i++) {
/* check for null ptr */
ExpectIntEQ(wc_HashInit(NULL, notSupportedHash[i]),
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
ExpectIntEQ(wc_HashInit_ex(NULL, notSupportedHash[i], HEAP_HINT,
INVALID_DEVID), WC_NO_ERR_TRACE(BAD_FUNC_ARG));
ExpectIntEQ(wc_HashInit(&hash, notSupportedHash[i]),
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
wc_HashFree(&hash, notSupportedHash[i]);
ExpectIntEQ(wc_HashInit_ex(&hash, notSupportedHash[i], HEAP_HINT,
INVALID_DEVID), WC_NO_ERR_TRACE(BAD_FUNC_ARG));
wc_HashFree(&hash, notSupportedHash[i]);
wc_HashFree(NULL, notSupportedHash[i]);
} /* end of for loop */
#endif
return EXPECT_RESULT();
} /* end of test_wc_HashInit */
int test_wc_HashUpdate(void)
{
EXPECT_DECLS;
#ifndef NO_HASH_WRAPPER
wc_HashAlg hash;
int i; /* 0 indicates tests passed, 1 indicates failure */
for (i = 0; i < supportedHashLen; i++) {
ExpectIntEQ(wc_HashInit(&hash, supportedHash[i]), 0);
/* Invalid parameters */
ExpectIntEQ(wc_HashUpdate(NULL, supportedHash[i], NULL, 1),
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
ExpectIntEQ(wc_HashUpdate(&hash, supportedHash[i], NULL, 1),
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
ExpectIntEQ(wc_HashUpdate(NULL, supportedHash[i], NULL, 0),
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
ExpectIntEQ(wc_HashUpdate(NULL, supportedHash[i], (byte*)"a", 1),
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
ExpectIntEQ(wc_HashUpdate(&hash, supportedHash[i], NULL, 0), 0);
ExpectIntEQ(wc_HashUpdate(&hash, supportedHash[i], (byte*)"a", 1), 0);
wc_HashFree(&hash, supportedHash[i]);
}
for (i = 0; i < notCompiledHashLen; i++) {
ExpectIntEQ(wc_HashInit(&hash, notCompiledHash[i]),
WC_NO_ERR_TRACE(HASH_TYPE_E));
/* Invalid parameters */
ExpectIntEQ(wc_HashUpdate(NULL, notCompiledHash[i], NULL, 1),
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
ExpectIntEQ(wc_HashUpdate(&hash, notCompiledHash[i], NULL, 1),
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
ExpectIntEQ(wc_HashUpdate(NULL, notCompiledHash[i], NULL, 0),
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
ExpectIntEQ(wc_HashUpdate(NULL, notCompiledHash[i], (byte*)"a", 1),
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
ExpectIntEQ(wc_HashUpdate(&hash, notCompiledHash[i], NULL, 0),
WC_NO_ERR_TRACE(HASH_TYPE_E));
ExpectIntEQ(wc_HashUpdate(&hash, notCompiledHash[i], (byte*)"a", 1),
WC_NO_ERR_TRACE(HASH_TYPE_E));
wc_HashFree(&hash, notCompiledHash[i]);
}
for (i = 0; i < notSupportedHashLen; i++) {
ExpectIntEQ(wc_HashInit(&hash, notSupportedHash[i]),
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
/* Invalid parameters */
ExpectIntEQ(wc_HashUpdate(NULL, notSupportedHash[i], NULL, 1),
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
ExpectIntEQ(wc_HashUpdate(&hash, notSupportedHash[i], NULL, 1),
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
ExpectIntEQ(wc_HashUpdate(NULL, notSupportedHash[i], NULL, 0),
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
ExpectIntEQ(wc_HashUpdate(NULL, notSupportedHash[i], (byte*)"a", 1),
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
ExpectIntEQ(wc_HashUpdate(&hash, notSupportedHash[i], NULL, 0),
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
ExpectIntEQ(wc_HashUpdate(&hash, notSupportedHash[i], (byte*)"a", 1),
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
wc_HashFree(&hash, notSupportedHash[i]);
}
#if defined(DEBUG_WOLFSSL) && !defined(NO_SHA256) && defined(WOLFSSL_SHA512)
ExpectIntEQ(wc_HashInit(&hash, WC_HASH_TYPE_SHA256), 0);
ExpectIntEQ(wc_HashUpdate(&hash, WC_HASH_TYPE_SHA512, NULL, 0),
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
ExpectIntEQ(wc_HashUpdate(&hash, WC_HASH_TYPE_SHA512, (byte*)"a", 1),
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
#endif
#endif
return EXPECT_RESULT();
}
int test_wc_HashFinal(void)
{
EXPECT_DECLS;
#ifndef NO_HASH_WRAPPER
wc_HashAlg hash;
byte digest[WC_MAX_DIGEST_SIZE];
int i; /* 0 indicates tests passed, 1 indicates failure */
for (i = 0; i < supportedHashLen; i++) {
ExpectIntEQ(wc_HashInit(&hash, supportedHash[i]), 0);
/* Invalid parameters */
ExpectIntEQ(wc_HashFinal(NULL, supportedHash[i], NULL),
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
ExpectIntEQ(wc_HashFinal(&hash, supportedHash[i], NULL),
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
ExpectIntEQ(wc_HashFinal(NULL, supportedHash[i], digest),
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
ExpectIntEQ(wc_HashFinal(&hash, supportedHash[i], digest), 0);
wc_HashFree(&hash, supportedHash[i]);
}
for (i = 0; i < notCompiledHashLen; i++) {
ExpectIntEQ(wc_HashInit(&hash, notCompiledHash[i]),
WC_NO_ERR_TRACE(HASH_TYPE_E));
/* Invalid parameters */
ExpectIntEQ(wc_HashFinal(NULL, notCompiledHash[i], NULL),
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
ExpectIntEQ(wc_HashFinal(&hash, notCompiledHash[i], NULL),
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
ExpectIntEQ(wc_HashFinal(NULL, notCompiledHash[i], digest),
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
ExpectIntEQ(wc_HashFinal(&hash, notCompiledHash[i], digest),
WC_NO_ERR_TRACE(HASH_TYPE_E));
wc_HashFree(&hash, notCompiledHash[i]);
}
for (i = 0; i < notSupportedHashLen; i++) {
ExpectIntEQ(wc_HashInit(&hash, notSupportedHash[i]),
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
/* Invalid parameters */
ExpectIntEQ(wc_HashFinal(NULL, notSupportedHash[i], NULL),
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
ExpectIntEQ(wc_HashFinal(&hash, notSupportedHash[i], NULL),
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
ExpectIntEQ(wc_HashFinal(NULL, notSupportedHash[i], digest),
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
ExpectIntEQ(wc_HashFinal(&hash, notSupportedHash[i], digest),
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
wc_HashFree(&hash, notSupportedHash[i]);
}
#if defined(DEBUG_WOLFSSL) && !defined(NO_SHA256) && defined(WOLFSSL_SHA512)
ExpectIntEQ(wc_HashInit(&hash, WC_HASH_TYPE_SHA256), 0);
ExpectIntEQ(wc_HashFinal(&hash, WC_HASH_TYPE_SHA512, digest),
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
#endif
#endif
return EXPECT_RESULT();
}
int test_wc_HashNewDelete(void)
{
EXPECT_DECLS;
#if !defined(NO_HASH_WRAPPER) && !defined(WC_NO_CONSTRUCTORS)
wc_HashAlg* hash;
byte digest[WC_MAX_DIGEST_SIZE];
int ret;
int i;
for (i = 0; i < supportedHashLen; i++) {
ExpectNotNull(hash = wc_HashNew(supportedHash[i], HEAP_HINT,
INVALID_DEVID, &ret));
ExpectIntEQ(ret, 0);
ExpectIntEQ(wc_HashUpdate(hash, supportedHash[i], (byte*)"a", 1), 0);
ExpectIntEQ(wc_HashFinal(hash, supportedHash[i], digest), 0);
ExpectIntEQ(wc_HashDelete(hash, &hash), 0);
ExpectNull(hash);
ExpectNotNull(hash = wc_HashNew(supportedHash[i], HEAP_HINT,
INVALID_DEVID, &ret));
ExpectIntEQ(ret, 0);
ExpectIntEQ(wc_HashDelete(hash, NULL), 0);
ExpectIntEQ(wc_HashDelete(NULL, NULL), WC_NO_ERR_TRACE(BAD_FUNC_ARG));
}
for (i = 0; i < notCompiledHashLen; i++) {
ExpectNull(wc_HashNew(notCompiledHash[i], HEAP_HINT, INVALID_DEVID,
&ret));
ExpectIntEQ(ret, WC_NO_ERR_TRACE(HASH_TYPE_E));
}
for (i = 0; i < notSupportedHashLen; i++) {
ExpectNull(wc_HashNew(notSupportedHash[i], HEAP_HINT, INVALID_DEVID,
&ret));
ExpectIntEQ(ret, WC_NO_ERR_TRACE(BAD_FUNC_ARG));
}
#endif
return EXPECT_RESULT();
}
int test_wc_HashGetDigestSize(void)
{
EXPECT_DECLS;
#ifndef NO_HASH_WRAPPER
int i;
for (i = 0; i < supportedHashLen; i++) {
ExpectIntGT(wc_HashGetDigestSize(supportedHash[i]), 0);
}
for (i = 0; i < sizeSupportedHashLen; i++) {
ExpectIntGT(wc_HashGetDigestSize(sizeSupportedHash[i]), 0);
}
for (i = 0; i < notCompiledHashLen; i++) {
ExpectIntEQ(wc_HashGetDigestSize(notCompiledHash[i]),
WC_NO_ERR_TRACE(HASH_TYPE_E));
}
for (i = 0; i < sizeNotCompiledHashLen; i++) {
ExpectIntEQ(wc_HashGetDigestSize(sizeNotCompiledHash[i]),
WC_NO_ERR_TRACE(HASH_TYPE_E));
}
for (i = 0; i < sizeNotSupportedHashLen; i++) {
ExpectIntEQ(wc_HashGetDigestSize(sizeNotSupportedHash[i]),
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
}
#endif
return EXPECT_RESULT();
}
int test_wc_HashGetBlockSize(void)
{
EXPECT_DECLS;
#ifndef NO_HASH_WRAPPER
int i;
for (i = 0; i < supportedHashLen; i++) {
ExpectIntGT(wc_HashGetBlockSize(supportedHash[i]), 0);
}
for (i = 0; i < sizeSupportedHashLen; i++) {
ExpectIntGT(wc_HashGetBlockSize(sizeSupportedHash[i]), 0);
}
for (i = 0; i < notCompiledHashLen; i++) {
ExpectIntEQ(wc_HashGetBlockSize(notCompiledHash[i]),
WC_NO_ERR_TRACE(HASH_TYPE_E));
}
for (i = 0; i < sizeNotCompiledHashLen; i++) {
ExpectIntEQ(wc_HashGetBlockSize(sizeNotCompiledHash[i]),
WC_NO_ERR_TRACE(HASH_TYPE_E));
}
for (i = 0; i < sizeNotSupportedHashLen; i++) {
ExpectIntEQ(wc_HashGetBlockSize(sizeNotSupportedHash[i]),
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
}
#endif
return EXPECT_RESULT();
}
int test_wc_Hash(void)
{
EXPECT_DECLS;
#if !defined(NO_HASH_WRAPPER) && !defined(WC_NO_CONSTRUCTORS)
byte digest[WC_MAX_DIGEST_SIZE];
int i;
for (i = 0; i < supportedHashLen; i++) {
ExpectIntEQ(wc_Hash(supportedHash[i], (byte*)"a", 1,
digest, sizeof(digest)), 0);
ExpectIntEQ(wc_Hash_ex(supportedHash[i], (byte*)"a", 1,
digest, sizeof(digest), HEAP_HINT, INVALID_DEVID), 0);
}
#if !defined(NO_MD5) && !defined(NO_SHA)
ExpectIntEQ(wc_Hash(WC_HASH_TYPE_MD5_SHA, (byte*)"a", 1,
digest, sizeof(digest)), 0);
ExpectIntEQ(wc_Hash_ex(WC_HASH_TYPE_MD5_SHA, (byte*)"a", 1,
digest, sizeof(digest), HEAP_HINT, INVALID_DEVID), 0);
#endif
for (i = 0; i < notCompiledHashLen; i++) {
ExpectIntEQ(wc_Hash(notCompiledHash[i], (byte*)"a", 1,
digest, sizeof(digest)), WC_NO_ERR_TRACE(HASH_TYPE_E));
ExpectIntEQ(wc_Hash_ex(notCompiledHash[i], (byte*)"a", 1,
digest, sizeof(digest), HEAP_HINT, INVALID_DEVID),
WC_NO_ERR_TRACE(HASH_TYPE_E));
}
for (i = 0; i < sizeNotCompiledHashLen; i++) {
ExpectIntEQ(wc_Hash(sizeNotCompiledHash[i], (byte*)"a", 1,
digest, sizeof(digest)), WC_NO_ERR_TRACE(HASH_TYPE_E));
ExpectIntEQ(wc_Hash_ex(sizeNotCompiledHash[i], (byte*)"a", 1,
digest, sizeof(digest), HEAP_HINT, INVALID_DEVID),
WC_NO_ERR_TRACE(HASH_TYPE_E));
}
for (i = 0; i < sizeNotSupportedHashLen; i++) {
if (notSupportedHash[i] == WC_HASH_TYPE_MD5_SHA) {
/* Algorithm only supported with wc_Hash() and wc_Hash_ex(). */
continue;
}
ExpectIntEQ(wc_Hash(sizeNotSupportedHash[i], (byte*)"a", 1,
digest, sizeof(digest)), WC_NO_ERR_TRACE(BAD_FUNC_ARG));
ExpectIntEQ(wc_Hash_ex(sizeNotSupportedHash[i], (byte*)"a", 1,
digest, sizeof(digest), HEAP_HINT, INVALID_DEVID),
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
}
#endif
return EXPECT_RESULT();
}
/*
* Unit test function for wc_HashSetFlags()
*/
int test_wc_HashSetFlags(void)
{
EXPECT_DECLS;
#if !defined(NO_HASH_WRAPPER) && defined(WOLFSSL_HASH_FLAGS)
wc_HashAlg hash;
word32 flags = 0;
int i;
/* For loop to test various arguments... */
for (i = 0; i < supportedHashLen; i++) {
ExpectIntEQ(wc_HashInit(&hash, supportedHash[i]), 0);
ExpectIntEQ(wc_HashSetFlags(&hash, supportedHash[i], flags), 0);
ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
ExpectIntEQ(wc_HashSetFlags(NULL, supportedHash[i], flags),
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
wc_HashFree(&hash, supportedHash[i]);
}
for (i = 0; i < notCompiledHashLen; i++) {
ExpectIntEQ(wc_HashInit(&hash, notCompiledHash[i]),
WC_NO_ERR_TRACE(HASH_TYPE_E));
ExpectIntEQ(wc_HashSetFlags(&hash, notCompiledHash[i], flags),
WC_NO_ERR_TRACE(HASH_TYPE_E));
ExpectIntEQ(wc_HashFree(&hash, notCompiledHash[i]),
WC_NO_ERR_TRACE(HASH_TYPE_E));
}
/* For loop to test not supported cases */
for (i = 0; i < notSupportedHashLen; i++) {
ExpectIntEQ(wc_HashInit(&hash, notSupportedHash[i]),
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
ExpectIntEQ(wc_HashSetFlags(&hash, notSupportedHash[i], flags),
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
ExpectIntEQ(wc_HashFree(&hash, notSupportedHash[i]),
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
}
#endif
return EXPECT_RESULT();
} /* END test_wc_HashSetFlags */
/*
* Unit test function for wc_HashGetFlags()
*/
int test_wc_HashGetFlags(void)
{
EXPECT_DECLS;
#if !defined(NO_HASH_WRAPPER) && defined(WOLFSSL_HASH_FLAGS)
wc_HashAlg hash;
word32 flags = 0;
int i;
/* For loop to test various arguments... */
for (i = 0; i < supportedHashLen; i++) {
ExpectIntEQ(wc_HashInit(&hash, supportedHash[i]), 0);
ExpectIntEQ(wc_HashGetFlags(&hash, supportedHash[i], &flags), 0);
ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
ExpectIntEQ(wc_HashGetFlags(NULL, supportedHash[i], &flags),
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
wc_HashFree(&hash, supportedHash[i]);
}
for (i = 0; i < notCompiledHashLen; i++) {
ExpectIntEQ(wc_HashInit(&hash, notCompiledHash[i]),
WC_NO_ERR_TRACE(HASH_TYPE_E));
ExpectIntEQ(wc_HashGetFlags(&hash, notCompiledHash[i], &flags),
WC_NO_ERR_TRACE(HASH_TYPE_E));
ExpectIntEQ(wc_HashFree(&hash, notCompiledHash[i]),
WC_NO_ERR_TRACE(HASH_TYPE_E));
}
/* For loop to test not supported cases */
for (i = 0; i < notSupportedHashLen; i++) {
ExpectIntEQ(wc_HashInit(&hash, notSupportedHash[i]),
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
ExpectIntEQ(wc_HashGetFlags(&hash, notSupportedHash[i], &flags),
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
ExpectIntEQ(wc_HashFree(&hash, notSupportedHash[i]),
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
}
#endif
return EXPECT_RESULT();
} /* END test_wc_HashGetFlags */
int test_wc_Hash_Algs(void)
{
EXPECT_DECLS;
#ifndef NO_HASH_WRAPPER
#ifndef NO_MD5
DIGEST_HASH_TEST(Md5, MD5);
#endif
#ifndef NO_SHA
DIGEST_HASH_TEST(Sha, SHA);
#endif
#ifdef WOLFSSL_SHA224
DIGEST_HASH_TEST(Sha224, SHA224);
#endif
#ifndef NO_SHA256
DIGEST_HASH_TEST(Sha256, SHA256);
#endif
#ifdef WOLFSSL_SHA384
DIGEST_HASH_TEST(Sha384, SHA384);
#endif
#ifdef WOLFSSL_SHA512
DIGEST_HASH_TEST(Sha512, SHA512);
#ifndef WOLFSSL_NOSHA512_224
DIGEST_HASH_TEST(Sha512_224, SHA512_224);
#endif
#ifndef WOLFSSL_NOSHA512_256
DIGEST_HASH_TEST(Sha512_256, SHA512_256);
#endif
#endif /* WOLFSSL_SHA512 */
#ifdef WOLFSSL_SHA3
#ifndef WOLFSSL_NOSHA3_224
DIGEST_COUNT_HASH_TEST(Sha3_224, SHA3_224);
#endif
#ifndef WOLFSSL_NOSHA3_256
DIGEST_COUNT_HASH_TEST(Sha3_256, SHA3_256);
#endif
#ifndef WOLFSSL_NOSHA3_384
DIGEST_COUNT_HASH_TEST(Sha3_384, SHA3_384);
#endif
#ifndef WOLFSSL_NOSHA3_512
DIGEST_COUNT_HASH_TEST(Sha3_512, SHA3_512);
#endif
#endif
#ifdef WOLFSSL_SM3
DIGEST_HASH_TEST(Sm3, SM3);
#endif
#endif /* !NO_HASH_WRAPPER */
return EXPECT_RESULT();
}
int test_wc_HashGetOID(void)
{
EXPECT_DECLS;
#if !defined(NO_HASH_WRAPPER) && (!defined(NO_ASN) || !defined(NO_DH) || \
defined(HAVE_ECC))
static const enum wc_HashType oidOnlySupportedHash[] = {
#ifdef WOLFSSL_MD2
WC_HASH_TYPE_MD2,
#endif
#ifndef NO_MD5
WC_HASH_TYPE_MD5_SHA,
#endif
WC_HASH_TYPE_NONE /* Dummy value to ensure list is non-zero. */
};
static const int oidOnlySupportedHashLen = (sizeof(oidOnlySupportedHash) /
sizeof(enum wc_HashType)) - 1;
static const enum wc_HashType oidOnlyNotCompiledHash[] = {
#ifndef WOLFSSL_MD2
WC_HASH_TYPE_MD2,
#endif
#ifdef NO_MD5
WC_HASH_TYPE_MD5_SHA,
#endif
WC_HASH_TYPE_NONE /* Dummy value to ensure list is non-zero. */
};
static const int oidOnlyNotCompiledHashLen =
(sizeof(oidOnlyNotCompiledHash) / sizeof(enum wc_HashType)) - 1;
static const enum wc_HashType oidNotSupportedHash[] = {
WC_HASH_TYPE_MD4,
WC_HASH_TYPE_BLAKE2B,
WC_HASH_TYPE_BLAKE2S,
WC_HASH_TYPE_NONE
};
static const int oidNotSupportedHashLen = (sizeof(oidNotSupportedHash) /
sizeof(enum wc_HashType));
int i;
for (i = 0; i < supportedHashLen; i++) {
ExpectIntGT(wc_HashGetOID(supportedHash[i]), 0);
}
for (i = 0; i < oidOnlySupportedHashLen; i++) {
ExpectIntGT(wc_HashGetOID(oidOnlySupportedHash[i]), 0);
}
for (i = 0; i < notCompiledHashLen; i++) {
ExpectIntEQ(wc_HashGetOID(notCompiledHash[i]),
WC_NO_ERR_TRACE(HASH_TYPE_E));
}
for (i = 0; i < oidOnlyNotCompiledHashLen; i++) {
ExpectIntEQ(wc_HashGetOID(oidOnlyNotCompiledHash[i]),
WC_NO_ERR_TRACE(HASH_TYPE_E));
}
for (i = 0; i < oidNotSupportedHashLen; i++) {
ExpectIntEQ(wc_HashGetOID(oidNotSupportedHash[i]),
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
}
#endif
return EXPECT_RESULT();
}
int test_wc_OidGetHash(void)
{
EXPECT_DECLS;
#if !defined(NO_HASH_WRAPPER) && !defined(NO_ASN)
static const int sumSupportedHash[] = {
#ifdef WOLFSSL_MD2
MD2h,
#endif
#ifndef NO_MD5
MD5h,
#endif
#ifndef NO_SHA
SHAh,
#endif
#ifdef WOLFSSL_SHA224
SHA224h,
#endif
#ifndef NO_SHA256
SHA256h,
#endif
#ifdef WOLFSSL_SHA384
SHA384h,
#endif
#ifdef WOLFSSL_SHA512
SHA512h,
#endif
#ifdef WOLFSSL_SHA3
SHA3_224h,
SHA3_256h,
SHA3_384h,
SHA3_512h,
#endif
#ifdef WOLFSSL_SM3
SM3h,
#endif
0 /* Dummy value to ensure list is non-zero. */
};
static const int sumSupportedHashLen = (sizeof(sumSupportedHash) /
sizeof(enum wc_HashType)) - 1;
static const int sumNotSupportedHash[] = {
MD4h,
#ifdef NO_MD5
MD5h,
#endif
#ifdef NO_SHA
SHAh,
#endif
#ifndef WOLFSSL_SHA224
SHA224h,
#endif
#ifdef NO_SHA256
SHA256h,
#endif
#ifndef WOLFSSL_SHA384
SHA384h,
#endif
#ifndef WOLFSSL_SHA512
SHA512h,
#endif
#ifndef WOLFSSL_SHA3
SHA3_224h,
SHA3_256h,
SHA3_384h,
SHA3_512h,
#endif
#ifndef WOLFSSL_SM3
SM3h,
#endif
0
};
static const int sumNotSupportedHashLen = (sizeof(sumNotSupportedHash) /
sizeof(enum wc_HashType));
int i;
enum wc_HashType hash;
for (i = 0; i < sumSupportedHashLen; i++) {
hash = wc_OidGetHash(sumSupportedHash[i]);
ExpectTrue(hash != WC_HASH_TYPE_NONE);
}
for (i = 0; i < sumNotSupportedHashLen; i++) {
hash = wc_OidGetHash(sumNotSupportedHash[i]);
ExpectTrue(hash == WC_HASH_TYPE_NONE);
}
#endif
return EXPECT_RESULT();
}
|