- Timestamp:
- Feb 7, 2019, 8:36:33 AM (5 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
asp3_tinet_ecnl_rx/trunk/wolfssl-3.12.2/wolfcrypt/src/hash.c
r337 r372 47 47 SHA512h = 416 48 48 }; 49 #endif 49 #endif /* !NO_ASN */ 50 51 #ifdef HAVE_SELFTEST 52 enum { 53 /* CAVP selftest includes these in hmac.h instead of sha3.h, 54 copied here for that build */ 55 WC_SHA3_224_BLOCK_SIZE = 144, 56 WC_SHA3_256_BLOCK_SIZE = 136, 57 WC_SHA3_384_BLOCK_SIZE = 104, 58 WC_SHA3_512_BLOCK_SIZE = 72, 59 }; 60 #endif 61 62 63 /* function converts int hash type to enum */ 64 enum wc_HashType wc_HashTypeConvert(int hashType) 65 { 66 /* Default to hash type none as error */ 67 enum wc_HashType eHashType = WC_HASH_TYPE_NONE; 68 #if defined(HAVE_FIPS) || defined(HAVE_SELFTEST) 69 /* original FIPSv1 and CAVP selftest require a mapping for unique hash 70 type to wc_HashType */ 71 switch (hashType) { 72 #ifndef NO_MD5 73 case WC_MD5: 74 eHashType = WC_HASH_TYPE_MD5; 75 break; 76 #endif /* !NO_MD5 */ 77 #ifndef NO_SHA 78 case WC_SHA: 79 eHashType = WC_HASH_TYPE_SHA; 80 break; 81 #endif /* !NO_SHA */ 82 83 #ifdef WOLFSSL_SHA224 84 case WC_SHA224: 85 eHashType = WC_HASH_TYPE_SHA224; 86 break; 87 #endif /* WOLFSSL_SHA224 */ 88 89 #ifndef NO_SHA256 90 case WC_SHA256: 91 eHashType = WC_HASH_TYPE_SHA256; 92 break; 93 #endif /* !NO_SHA256 */ 94 95 #ifdef WOLFSSL_SHA384 96 case WC_SHA384: 97 eHashType = WC_HASH_TYPE_SHA384; 98 break; 99 #endif /* WOLFSSL_SHA384 */ 100 #ifdef WOLFSSL_SHA512 101 case WC_SHA512: 102 eHashType = WC_HASH_TYPE_SHA512; 103 break; 104 #endif /* WOLFSSL_SHA512 */ 105 default: 106 eHashType = WC_HASH_TYPE_NONE; 107 break; 108 } 109 #else 110 /* current master uses same unique types as wc_HashType */ 111 if (hashType > 0 && hashType <= WC_HASH_TYPE_MAX) { 112 eHashType = (enum wc_HashType)hashType; 113 } 114 #endif 115 return eHashType; 116 } 117 50 118 51 119 int wc_HashGetOID(enum wc_HashType hash_type) … … 71 139 break; 72 140 case WC_HASH_TYPE_SHA224: 73 #if defined(WOLFSSL_SHA224)141 #ifdef WOLFSSL_SHA224 74 142 oid = SHA224h; 75 143 #endif … … 81 149 break; 82 150 case WC_HASH_TYPE_SHA384: 83 #if defined(WOLFSSL_SHA512) && defined(WOLFSSL_SHA384)151 #ifdef WOLFSSL_SHA384 84 152 oid = SHA384h; 85 153 #endif … … 93 161 /* Not Supported */ 94 162 case WC_HASH_TYPE_MD4: 163 case WC_HASH_TYPE_SHA3_224: 164 case WC_HASH_TYPE_SHA3_256: 165 case WC_HASH_TYPE_SHA3_384: 166 case WC_HASH_TYPE_SHA3_512: 167 case WC_HASH_TYPE_BLAKE2B: 95 168 case WC_HASH_TYPE_NONE: 96 169 default: … … 100 173 return oid; 101 174 } 102 #endif 175 176 enum wc_HashType wc_OidGetHash(int oid) 177 { 178 enum wc_HashType hash_type = WC_HASH_TYPE_NONE; 179 switch (oid) 180 { 181 #ifdef WOLFSSL_MD2 182 case MD2h: 183 hash_type = WC_HASH_TYPE_MD2; 184 break; 185 #endif 186 case MD5h: 187 #ifndef NO_MD5 188 hash_type = WC_HASH_TYPE_MD5; 189 #endif 190 break; 191 case SHAh: 192 #ifndef NO_SHA 193 hash_type = WC_HASH_TYPE_SHA; 194 #endif 195 break; 196 case SHA224h: 197 #ifdef WOLFSSL_SHA224 198 hash_type = WC_HASH_TYPE_SHA224; 199 #endif 200 break; 201 case SHA256h: 202 #ifndef NO_SHA256 203 hash_type = WC_HASH_TYPE_SHA256; 204 #endif 205 break; 206 case SHA384h: 207 #ifdef WOLFSSL_SHA384 208 hash_type = WC_HASH_TYPE_SHA384; 209 #endif 210 break; 211 case SHA512h: 212 #ifdef WOLFSSL_SHA512 213 hash_type = WC_HASH_TYPE_SHA512; 214 #endif 215 break; 216 default: 217 break; 218 } 219 return hash_type; 220 } 221 #endif /* !NO_ASN || !NO_DH || HAVE_ECC */ 222 223 103 224 104 225 /* Get Hash digest size */ … … 108 229 switch(hash_type) 109 230 { 231 case WC_HASH_TYPE_MD2: 232 #ifdef WOLFSSL_MD2 233 dig_size = MD2_DIGEST_SIZE; 234 #endif 235 break; 236 case WC_HASH_TYPE_MD4: 237 #ifndef NO_MD4 238 dig_size = MD4_DIGEST_SIZE; 239 #endif 240 break; 110 241 case WC_HASH_TYPE_MD5: 111 242 #ifndef NO_MD5 … … 129 260 break; 130 261 case WC_HASH_TYPE_SHA384: 131 #if defined(WOLFSSL_SHA512) && defined(WOLFSSL_SHA384) 262 #ifdef WOLFSSL_SHA384 132 263 dig_size = WC_SHA384_DIGEST_SIZE; 133 264 #endif … … 138 269 #endif 139 270 break; 140 case WC_HASH_TYPE_MD5_SHA: 271 case WC_HASH_TYPE_MD5_SHA: /* Old TLS Specific */ 141 272 #if !defined(NO_MD5) && !defined(NO_SHA) 142 dig_size = WC_MD5_DIGEST_SIZE + WC_SHA_DIGEST_SIZE; 273 dig_size = (int)WC_MD5_DIGEST_SIZE + (int)WC_SHA_DIGEST_SIZE; 274 #endif 275 break; 276 277 case WC_HASH_TYPE_SHA3_224: 278 #ifdef WOLFSSL_SHA3 279 dig_size = WC_SHA3_224_DIGEST_SIZE; 280 #endif 281 break; 282 case WC_HASH_TYPE_SHA3_256: 283 #ifdef WOLFSSL_SHA3 284 dig_size = WC_SHA3_256_DIGEST_SIZE; 285 #endif 286 break; 287 case WC_HASH_TYPE_SHA3_384: 288 #ifdef WOLFSSL_SHA3 289 dig_size = WC_SHA3_384_DIGEST_SIZE; 290 #endif 291 break; 292 case WC_HASH_TYPE_SHA3_512: 293 #ifdef WOLFSSL_SHA3 294 dig_size = WC_SHA3_512_DIGEST_SIZE; 143 295 #endif 144 296 break; 145 297 146 298 /* Not Supported */ 147 case WC_HASH_TYPE_MD2: 148 case WC_HASH_TYPE_MD4: 299 case WC_HASH_TYPE_BLAKE2B: 149 300 case WC_HASH_TYPE_NONE: 150 301 default: … … 153 304 } 154 305 return dig_size; 306 } 307 308 309 /* Get Hash block size */ 310 int wc_HashGetBlockSize(enum wc_HashType hash_type) 311 { 312 int block_size = HASH_TYPE_E; /* Default to hash type error */ 313 switch (hash_type) 314 { 315 case WC_HASH_TYPE_MD2: 316 #ifdef WOLFSSL_MD2 317 block_size = MD2_BLOCK_SIZE; 318 #endif 319 break; 320 case WC_HASH_TYPE_MD4: 321 #ifndef NO_MD4 322 block_size = MD4_BLOCK_SIZE; 323 #endif 324 break; 325 case WC_HASH_TYPE_MD5: 326 #ifndef NO_MD5 327 block_size = WC_MD5_BLOCK_SIZE; 328 #endif 329 break; 330 case WC_HASH_TYPE_SHA: 331 #ifndef NO_SHA 332 block_size = WC_SHA_BLOCK_SIZE; 333 #endif 334 break; 335 case WC_HASH_TYPE_SHA224: 336 #ifdef WOLFSSL_SHA224 337 block_size = WC_SHA224_BLOCK_SIZE; 338 #endif 339 break; 340 case WC_HASH_TYPE_SHA256: 341 #ifndef NO_SHA256 342 block_size = WC_SHA256_BLOCK_SIZE; 343 #endif 344 break; 345 case WC_HASH_TYPE_SHA384: 346 #ifdef WOLFSSL_SHA384 347 block_size = WC_SHA384_BLOCK_SIZE; 348 #endif 349 break; 350 case WC_HASH_TYPE_SHA512: 351 #ifdef WOLFSSL_SHA512 352 block_size = WC_SHA512_BLOCK_SIZE; 353 #endif 354 break; 355 case WC_HASH_TYPE_MD5_SHA: /* Old TLS Specific */ 356 #if !defined(NO_MD5) && !defined(NO_SHA) 357 block_size = (int)WC_MD5_BLOCK_SIZE + (int)WC_SHA_BLOCK_SIZE; 358 #endif 359 break; 360 361 case WC_HASH_TYPE_SHA3_224: 362 #ifdef WOLFSSL_SHA3 363 block_size = WC_SHA3_224_BLOCK_SIZE; 364 #endif 365 break; 366 case WC_HASH_TYPE_SHA3_256: 367 #ifdef WOLFSSL_SHA3 368 block_size = WC_SHA3_256_BLOCK_SIZE; 369 #endif 370 break; 371 case WC_HASH_TYPE_SHA3_384: 372 #ifdef WOLFSSL_SHA3 373 block_size = WC_SHA3_384_BLOCK_SIZE; 374 #endif 375 break; 376 case WC_HASH_TYPE_SHA3_512: 377 #ifdef WOLFSSL_SHA3 378 block_size = WC_SHA3_512_BLOCK_SIZE; 379 #endif 380 break; 381 382 /* Not Supported */ 383 case WC_HASH_TYPE_BLAKE2B: 384 case WC_HASH_TYPE_NONE: 385 default: 386 block_size = BAD_FUNC_ARG; 387 break; 388 } 389 return block_size; 155 390 } 156 391 … … 197 432 break; 198 433 case WC_HASH_TYPE_SHA384: 199 #if defined(WOLFSSL_SHA512) && defined(WOLFSSL_SHA384)434 #ifdef WOLFSSL_SHA384 200 435 ret = wc_Sha384Hash(data, data_len, hash); 201 436 #endif … … 218 453 case WC_HASH_TYPE_MD2: 219 454 case WC_HASH_TYPE_MD4: 455 case WC_HASH_TYPE_SHA3_224: 456 case WC_HASH_TYPE_SHA3_256: 457 case WC_HASH_TYPE_SHA3_384: 458 case WC_HASH_TYPE_SHA3_512: 459 case WC_HASH_TYPE_BLAKE2B: 220 460 case WC_HASH_TYPE_NONE: 221 461 default: … … 236 476 case WC_HASH_TYPE_MD5: 237 477 #ifndef NO_MD5 238 wc_InitMd5(&hash->md5); 239 ret = 0; 478 ret = wc_InitMd5(&hash->md5); 240 479 #endif 241 480 break; … … 270 509 case WC_HASH_TYPE_MD2: 271 510 case WC_HASH_TYPE_MD4: 511 case WC_HASH_TYPE_SHA3_224: 512 case WC_HASH_TYPE_SHA3_256: 513 case WC_HASH_TYPE_SHA3_384: 514 case WC_HASH_TYPE_SHA3_512: 515 case WC_HASH_TYPE_BLAKE2B: 272 516 case WC_HASH_TYPE_NONE: 273 517 default: … … 289 533 case WC_HASH_TYPE_MD5: 290 534 #ifndef NO_MD5 291 wc_Md5Update(&hash->md5, data, dataSz); 292 ret = 0; 535 ret = wc_Md5Update(&hash->md5, data, dataSz); 293 536 #endif 294 537 break; … … 296 539 #ifndef NO_SHA 297 540 ret = wc_ShaUpdate(&hash->sha, data, dataSz); 298 if (ret != 0)299 return ret;300 541 #endif 301 542 break; … … 325 566 case WC_HASH_TYPE_MD2: 326 567 case WC_HASH_TYPE_MD4: 568 case WC_HASH_TYPE_SHA3_224: 569 case WC_HASH_TYPE_SHA3_256: 570 case WC_HASH_TYPE_SHA3_384: 571 case WC_HASH_TYPE_SHA3_512: 572 case WC_HASH_TYPE_BLAKE2B: 327 573 case WC_HASH_TYPE_NONE: 328 574 default: … … 343 589 case WC_HASH_TYPE_MD5: 344 590 #ifndef NO_MD5 345 wc_Md5Final(&hash->md5, out); 346 ret = 0; 591 ret = wc_Md5Final(&hash->md5, out); 347 592 #endif 348 593 break; … … 377 622 case WC_HASH_TYPE_MD2: 378 623 case WC_HASH_TYPE_MD4: 624 case WC_HASH_TYPE_SHA3_224: 625 case WC_HASH_TYPE_SHA3_256: 626 case WC_HASH_TYPE_SHA3_384: 627 case WC_HASH_TYPE_SHA3_512: 628 case WC_HASH_TYPE_BLAKE2B: 379 629 case WC_HASH_TYPE_NONE: 380 630 default: … … 385 635 } 386 636 637 int wc_HashFree(wc_HashAlg* hash, enum wc_HashType type) 638 { 639 int ret = HASH_TYPE_E; /* Default to hash type error */ 640 641 if (hash == NULL) 642 return BAD_FUNC_ARG; 643 644 switch (type) { 645 case WC_HASH_TYPE_MD5: 646 #ifndef NO_MD5 647 wc_Md5Free(&hash->md5); 648 ret = 0; 649 #endif 650 break; 651 case WC_HASH_TYPE_SHA: 652 #ifndef NO_SHA 653 wc_ShaFree(&hash->sha); 654 ret = 0; 655 #endif 656 break; 657 case WC_HASH_TYPE_SHA224: 658 #ifdef WOLFSSL_SHA224 659 wc_Sha224Free(&hash->sha224); 660 ret = 0; 661 #endif 662 break; 663 case WC_HASH_TYPE_SHA256: 664 #ifndef NO_SHA256 665 wc_Sha256Free(&hash->sha256); 666 ret = 0; 667 #endif 668 break; 669 case WC_HASH_TYPE_SHA384: 670 #ifdef WOLFSSL_SHA384 671 wc_Sha384Free(&hash->sha384); 672 ret = 0; 673 #endif 674 break; 675 case WC_HASH_TYPE_SHA512: 676 #ifdef WOLFSSL_SHA512 677 wc_Sha512Free(&hash->sha512); 678 ret = 0; 679 #endif 680 break; 681 682 /* not supported */ 683 case WC_HASH_TYPE_MD5_SHA: 684 case WC_HASH_TYPE_MD2: 685 case WC_HASH_TYPE_MD4: 686 case WC_HASH_TYPE_SHA3_224: 687 case WC_HASH_TYPE_SHA3_256: 688 case WC_HASH_TYPE_SHA3_384: 689 case WC_HASH_TYPE_SHA3_512: 690 case WC_HASH_TYPE_BLAKE2B: 691 case WC_HASH_TYPE_NONE: 692 default: 693 ret = BAD_FUNC_ARG; 694 }; 695 696 return ret; 697 } 698 387 699 388 700 #if !defined(WOLFSSL_TI_HASH) … … 404 716 #endif 405 717 406 ret = wc_InitMd5(md5); 407 if (ret == 0) { 408 ret = wc_Md5Update(md5, data, len); 409 if (ret == 0) { 410 ret = wc_Md5Final(md5, hash); 718 if ((ret = wc_InitMd5(md5)) != 0) { 719 WOLFSSL_MSG("InitMd5 failed"); 720 } 721 else { 722 if ((ret = wc_Md5Update(md5, data, len)) != 0) { 723 WOLFSSL_MSG("Md5Update failed"); 411 724 } 725 else if ((ret = wc_Md5Final(md5, hash)) != 0) { 726 WOLFSSL_MSG("Md5Final failed"); 727 } 728 wc_Md5Free(md5); 412 729 } 413 730 … … 437 754 438 755 if ((ret = wc_InitSha(sha)) != 0) { 439 WOLFSSL_MSG(" wc_InitSha failed");756 WOLFSSL_MSG("InitSha failed"); 440 757 } 441 758 else { 442 wc_ShaUpdate(sha, data, len); 443 wc_ShaFinal(sha, hash); 759 if ((ret = wc_ShaUpdate(sha, data, len)) != 0) { 760 WOLFSSL_MSG("ShaUpdate failed"); 761 } 762 else if ((ret = wc_ShaFinal(sha, hash)) != 0) { 763 WOLFSSL_MSG("ShaFinal failed"); 764 } 765 wc_ShaFree(sha); 444 766 } 445 767 … … 472 794 WOLFSSL_MSG("InitSha224 failed"); 473 795 } 474 else if ((ret = wc_Sha224Update(sha224, data, len)) != 0) { 796 else { 797 if ((ret = wc_Sha224Update(sha224, data, len)) != 0) { 475 798 WOLFSSL_MSG("Sha224Update failed"); 476 799 } … … 478 801 WOLFSSL_MSG("Sha224Final failed"); 479 802 } 803 wc_Sha224Free(sha224); 804 } 480 805 481 806 #ifdef WOLFSSL_SMALL_STACK … … 507 832 WOLFSSL_MSG("InitSha256 failed"); 508 833 } 509 else if ((ret = wc_Sha256Update(sha256, data, len)) != 0) { 834 else { 835 if ((ret = wc_Sha256Update(sha256, data, len)) != 0) { 510 836 WOLFSSL_MSG("Sha256Update failed"); 511 837 } … … 513 839 WOLFSSL_MSG("Sha256Final failed"); 514 840 } 841 wc_Sha256Free(sha256); 842 } 843 515 844 516 845 #ifdef WOLFSSL_SMALL_STACK … … 545 874 WOLFSSL_MSG("InitSha512 failed"); 546 875 } 547 else if ((ret = wc_Sha512Update(sha512, data, len)) != 0) { 876 else { 877 if ((ret = wc_Sha512Update(sha512, data, len)) != 0) { 548 878 WOLFSSL_MSG("Sha512Update failed"); 549 879 } … … 551 881 WOLFSSL_MSG("Sha512Final failed"); 552 882 } 883 wc_Sha512Free(sha512); 884 } 553 885 554 886 #ifdef WOLFSSL_SMALL_STACK … … 558 890 return ret; 559 891 } 892 #endif /* WOLFSSL_SHA512 */ 560 893 561 894 #if defined(WOLFSSL_SHA384) … … 579 912 WOLFSSL_MSG("InitSha384 failed"); 580 913 } 581 else if ((ret = wc_Sha384Update(sha384, data, len)) != 0) { 914 else { 915 if ((ret = wc_Sha384Update(sha384, data, len)) != 0) { 582 916 WOLFSSL_MSG("Sha384Update failed"); 583 917 } … … 585 919 WOLFSSL_MSG("Sha384Final failed"); 586 920 } 921 wc_Sha384Free(sha384); 922 } 587 923 588 924 #ifdef WOLFSSL_SMALL_STACK … … 593 929 } 594 930 #endif /* WOLFSSL_SHA384 */ 595 #endif /* WOLFSSL_SHA512 */
Note:
See TracChangeset
for help on using the changeset viewer.