- Timestamp:
- Feb 7, 2019, 8:36:33 AM (5 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
asp3_tinet_ecnl_arm/trunk/wolfssl-3.12.2/wolfcrypt/src/hmac.c
r352 r372 1 /* hmac. h1 /* hmac.c 2 2 * 3 3 * Copyright (C) 2006-2017 wolfSSL Inc. … … 30 30 #ifndef NO_HMAC 31 31 32 #if defined(HAVE_FIPS) && \ 33 defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2) 34 35 /* set NO_WRAPPERS before headers, use direct internal f()s not wrappers */ 36 #define FIPS_NO_WRAPPERS 37 38 #ifdef USE_WINDOWS_API 39 #pragma code_seg(".fipsA$b") 40 #pragma const_seg(".fipsB$b") 41 #endif 42 #endif 43 32 44 #include <wolfssl/wolfcrypt/hmac.h> 33 45 … … 41 53 42 54 /* fips wrapper calls, user can call direct */ 43 #ifdef HAVE_FIPS 55 /* If building for old FIPS. */ 56 #if defined(HAVE_FIPS) && \ 57 (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 2)) 58 44 59 /* does init */ 45 60 int wc_HmacSetKey(Hmac* hmac, int type, const byte* key, word32 keySz) … … 47 62 if (hmac == NULL || (key == NULL && keySz != 0) || 48 63 !(type == WC_MD5 || type == WC_SHA || type == WC_SHA256 || 49 type == WC_SHA384 || type == WC_SHA512 || type == BLAKE2B_ID)) { 64 type == WC_SHA384 || type == WC_SHA512 || 65 type == BLAKE2B_ID)) { 50 66 return BAD_FUNC_ARG; 51 67 } … … 101 117 #endif /* HAVE_HKDF */ 102 118 103 #else /* else build without fips */ 104 105 106 #include <wolfssl/wolfcrypt/error-crypt.h> 119 #else /* else build without fips, or for new fips */ 107 120 108 121 … … 111 124 int ret; 112 125 113 if (!(type == WC_MD5 || type == WC_SHA || type == WC_SHA224 || 114 type == WC_SHA256 || type == WC_SHA384 || type == WC_SHA512 || 126 if (!(type == WC_MD5 || type == WC_SHA || 127 type == WC_SHA224 || type == WC_SHA256 || 128 type == WC_SHA384 || type == WC_SHA512 || 129 type == WC_SHA3_224 || type == WC_SHA3_256 || 130 type == WC_SHA3_384 || type == WC_SHA3_512 || 115 131 type == BLAKE2B_ID)) { 116 132 return BAD_FUNC_ARG; … … 142 158 #endif /* !NO_SHA256 */ 143 159 144 #ifdef WOLFSSL_SHA512145 160 #ifdef WOLFSSL_SHA384 146 161 case WC_SHA384: … … 148 163 break; 149 164 #endif /* WOLFSSL_SHA384 */ 165 #ifdef WOLFSSL_SHA512 150 166 case WC_SHA512: 151 167 ret = WC_SHA512_DIGEST_SIZE; … … 159 175 #endif /* HAVE_BLAKE2 */ 160 176 177 #ifdef WOLFSSL_SHA3 178 case WC_SHA3_224: 179 ret = WC_SHA3_224_DIGEST_SIZE; 180 break; 181 182 case WC_SHA3_256: 183 ret = WC_SHA3_256_DIGEST_SIZE; 184 break; 185 186 case WC_SHA3_384: 187 ret = WC_SHA3_384_DIGEST_SIZE; 188 break; 189 190 case WC_SHA3_512: 191 ret = WC_SHA3_512_DIGEST_SIZE; 192 break; 193 194 #endif 195 161 196 default: 162 197 ret = BAD_FUNC_ARG; … … 167 202 } 168 203 169 staticint _InitHmac(Hmac* hmac, int type, void* heap)204 int _InitHmac(Hmac* hmac, int type, void* heap) 170 205 { 171 206 int ret = 0; … … 196 231 #endif /* !NO_SHA256 */ 197 232 198 #ifdef WOLFSSL_SHA512199 233 #ifdef WOLFSSL_SHA384 200 234 case WC_SHA384: … … 202 236 break; 203 237 #endif /* WOLFSSL_SHA384 */ 238 #ifdef WOLFSSL_SHA512 204 239 case WC_SHA512: 205 240 ret = wc_InitSha512(&hmac->hash.sha512); … … 212 247 break; 213 248 #endif /* HAVE_BLAKE2 */ 249 250 #ifdef WOLFSSL_SHA3 251 case WC_SHA3_224: 252 ret = wc_InitSha3_224(&hmac->hash.sha3, heap, INVALID_DEVID); 253 break; 254 case WC_SHA3_256: 255 ret = wc_InitSha3_256(&hmac->hash.sha3, heap, INVALID_DEVID); 256 break; 257 case WC_SHA3_384: 258 ret = wc_InitSha3_384(&hmac->hash.sha3, heap, INVALID_DEVID); 259 break; 260 case WC_SHA3_512: 261 ret = wc_InitSha3_512(&hmac->hash.sha3, heap, INVALID_DEVID); 262 break; 263 #endif 214 264 215 265 default: … … 238 288 239 289 if (hmac == NULL || (key == NULL && length != 0) || 240 !(type == WC_MD5 || type == WC_SHA || type == WC_SHA224 || 241 type == WC_SHA256 || type == WC_SHA384 || type == WC_SHA512 || 290 !(type == WC_MD5 || type == WC_SHA || 291 type == WC_SHA224 || type == WC_SHA256 || 292 type == WC_SHA384 || type == WC_SHA512 || 293 type == WC_SHA3_224 || type == WC_SHA3_256 || 294 type == WC_SHA3_384 || type == WC_SHA3_512 || 242 295 type == BLAKE2B_ID)) { 243 296 return BAD_FUNC_ARG; 244 297 } 245 298 299 /* if set key has already been run then make sure and free existing */ 300 if (hmac->macType != 0) { 301 wc_HmacFree(hmac); 302 } 303 246 304 hmac->innerHashKeyed = 0; 247 305 hmac->macType = (byte)type; 248 249 #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_HMAC)250 if (hmac->asyncDev.marker == WOLFSSL_ASYNC_MARKER_HMAC) {251 #if defined(HAVE_CAVIUM)252 if (length > HMAC_BLOCK_SIZE) {253 return WC_KEY_SIZE_E;254 }255 256 if (key != NULL) {257 XMEMCPY(hmac->ipad, key, length);258 }259 hmac->keyLen = (word16)length;260 261 return 0; /* nothing to do here */262 #endif /* HAVE_CAVIUM */263 }264 #endif /* WOLFSSL_ASYNC_CRYPT */265 306 266 307 ret = _InitHmac(hmac, type, heap); … … 362 403 #endif /* !NO_SHA256 */ 363 404 364 #ifdef WOLFSSL_SHA512365 405 #ifdef WOLFSSL_SHA384 366 406 case WC_SHA384: … … 383 423 break; 384 424 #endif /* WOLFSSL_SHA384 */ 425 #ifdef WOLFSSL_SHA512 385 426 case WC_SHA512: 386 427 hmac_block_size = WC_SHA512_BLOCK_SIZE; … … 424 465 #endif /* HAVE_BLAKE2 */ 425 466 467 #ifdef WOLFSSL_SHA3 468 case WC_SHA3_224: 469 hmac_block_size = WC_SHA3_224_BLOCK_SIZE; 470 if (length <= WC_SHA3_224_BLOCK_SIZE) { 471 if (key != NULL) { 472 XMEMCPY(ip, key, length); 473 } 474 } 475 else { 476 ret = wc_Sha3_224_Update(&hmac->hash.sha3, key, length); 477 if (ret != 0) 478 break; 479 ret = wc_Sha3_224_Final(&hmac->hash.sha3, ip); 480 if (ret != 0) 481 break; 482 483 length = WC_SHA3_224_DIGEST_SIZE; 484 } 485 break; 486 case WC_SHA3_256: 487 hmac_block_size = WC_SHA3_256_BLOCK_SIZE; 488 if (length <= WC_SHA3_256_BLOCK_SIZE) { 489 if (key != NULL) { 490 XMEMCPY(ip, key, length); 491 } 492 } 493 else { 494 ret = wc_Sha3_256_Update(&hmac->hash.sha3, key, length); 495 if (ret != 0) 496 break; 497 ret = wc_Sha3_256_Final(&hmac->hash.sha3, ip); 498 if (ret != 0) 499 break; 500 501 length = WC_SHA3_256_DIGEST_SIZE; 502 } 503 break; 504 case WC_SHA3_384: 505 hmac_block_size = WC_SHA3_384_BLOCK_SIZE; 506 if (length <= WC_SHA3_384_BLOCK_SIZE) { 507 if (key != NULL) { 508 XMEMCPY(ip, key, length); 509 } 510 } 511 else { 512 ret = wc_Sha3_384_Update(&hmac->hash.sha3, key, length); 513 if (ret != 0) 514 break; 515 ret = wc_Sha3_384_Final(&hmac->hash.sha3, ip); 516 if (ret != 0) 517 break; 518 519 length = WC_SHA3_384_DIGEST_SIZE; 520 } 521 break; 522 case WC_SHA3_512: 523 hmac_block_size = WC_SHA3_512_BLOCK_SIZE; 524 if (length <= WC_SHA3_512_BLOCK_SIZE) { 525 if (key != NULL) { 526 XMEMCPY(ip, key, length); 527 } 528 } 529 else { 530 ret = wc_Sha3_512_Update(&hmac->hash.sha3, key, length); 531 if (ret != 0) 532 break; 533 ret = wc_Sha3_512_Final(&hmac->hash.sha3, ip); 534 if (ret != 0) 535 break; 536 537 length = WC_SHA3_512_DIGEST_SIZE; 538 } 539 break; 540 #endif /* WOLFSSL_SHA3 */ 541 426 542 default: 427 543 return BAD_FUNC_ARG; … … 430 546 #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_HMAC) 431 547 if (hmac->asyncDev.marker == WOLFSSL_ASYNC_MARKER_HMAC) { 432 #if defined(HAVE_INTEL_QA) 548 #if defined(HAVE_INTEL_QA) || defined(HAVE_CAVIUM) 549 #ifdef HAVE_INTEL_QA 550 if (IntelQaHmacGetType(hmac->macType, NULL) == 0) 551 #endif 552 { 433 553 if (length > hmac_block_size) 434 554 length = hmac_block_size; … … 437 557 438 558 return ret; 559 } 439 560 /* no need to pad below */ 440 561 #endif … … 489 610 #endif /* !NO_SHA256 */ 490 611 491 #ifdef WOLFSSL_SHA512492 612 #ifdef WOLFSSL_SHA384 493 613 case WC_SHA384: … … 496 616 break; 497 617 #endif /* WOLFSSL_SHA384 */ 618 #ifdef WOLFSSL_SHA512 498 619 case WC_SHA512: 499 620 ret = wc_Sha512Update(&hmac->hash.sha512, (byte*)hmac->ipad, … … 508 629 break; 509 630 #endif /* HAVE_BLAKE2 */ 631 632 #ifdef WOLFSSL_SHA3 633 case WC_SHA3_224: 634 ret = wc_Sha3_224_Update(&hmac->hash.sha3, (byte*)hmac->ipad, 635 WC_SHA3_224_BLOCK_SIZE); 636 break; 637 case WC_SHA3_256: 638 ret = wc_Sha3_256_Update(&hmac->hash.sha3, (byte*)hmac->ipad, 639 WC_SHA3_256_BLOCK_SIZE); 640 break; 641 case WC_SHA3_384: 642 ret = wc_Sha3_384_Update(&hmac->hash.sha3, (byte*)hmac->ipad, 643 WC_SHA3_384_BLOCK_SIZE); 644 break; 645 case WC_SHA3_512: 646 ret = wc_Sha3_512_Update(&hmac->hash.sha3, (byte*)hmac->ipad, 647 WC_SHA3_512_BLOCK_SIZE); 648 break; 649 #endif /* WOLFSSL_SHA3 */ 510 650 511 651 default: … … 533 673 return NitroxHmacUpdate(hmac, msg, length); 534 674 #elif defined(HAVE_INTEL_QA) 675 if (IntelQaHmacGetType(hmac->macType, NULL) == 0) { 535 676 return IntelQaHmac(&hmac->asyncDev, hmac->macType, 536 677 (byte*)hmac->ipad, hmac->keyLen, NULL, msg, length); 678 } 537 679 #endif 538 680 } … … 570 712 #endif /* !NO_SHA256 */ 571 713 572 #ifdef WOLFSSL_SHA512573 714 #ifdef WOLFSSL_SHA384 574 715 case WC_SHA384: … … 576 717 break; 577 718 #endif /* WOLFSSL_SHA384 */ 719 #ifdef WOLFSSL_SHA512 578 720 case WC_SHA512: 579 721 ret = wc_Sha512Update(&hmac->hash.sha512, msg, length); … … 586 728 break; 587 729 #endif /* HAVE_BLAKE2 */ 730 731 #ifdef WOLFSSL_SHA3 732 case WC_SHA3_224: 733 ret = wc_Sha3_224_Update(&hmac->hash.sha3, msg, length); 734 break; 735 case WC_SHA3_256: 736 ret = wc_Sha3_256_Update(&hmac->hash.sha3, msg, length); 737 break; 738 case WC_SHA3_384: 739 ret = wc_Sha3_384_Update(&hmac->hash.sha3, msg, length); 740 break; 741 case WC_SHA3_512: 742 ret = wc_Sha3_512_Update(&hmac->hash.sha3, msg, length); 743 break; 744 #endif /* WOLFSSL_SHA3 */ 588 745 589 746 default: … … 610 767 611 768 #if defined(HAVE_CAVIUM) 612 return NitroxHmacFinal(hmac, h mac->macType, hash, hashLen);769 return NitroxHmacFinal(hmac, hash, hashLen); 613 770 #elif defined(HAVE_INTEL_QA) 771 if (IntelQaHmacGetType(hmac->macType, NULL) == 0) { 614 772 return IntelQaHmac(&hmac->asyncDev, hmac->macType, 615 773 (byte*)hmac->ipad, hmac->keyLen, hash, NULL, hashLen); 774 } 616 775 #endif 617 776 } … … 697 856 #endif /* !NO_SHA256 */ 698 857 699 #ifdef WOLFSSL_SHA512700 858 #ifdef WOLFSSL_SHA384 701 859 case WC_SHA384: … … 714 872 break; 715 873 #endif /* WOLFSSL_SHA384 */ 874 #ifdef WOLFSSL_SHA512 716 875 case WC_SHA512: 717 876 ret = wc_Sha512Final(&hmac->hash.sha512, (byte*)hmac->innerHash); … … 748 907 #endif /* HAVE_BLAKE2 */ 749 908 909 #ifdef WOLFSSL_SHA3 910 case WC_SHA3_224: 911 ret = wc_Sha3_224_Final(&hmac->hash.sha3, (byte*)hmac->innerHash); 912 if (ret != 0) 913 break; 914 ret = wc_Sha3_224_Update(&hmac->hash.sha3, (byte*)hmac->opad, 915 WC_SHA3_224_BLOCK_SIZE); 916 if (ret != 0) 917 break; 918 ret = wc_Sha3_224_Update(&hmac->hash.sha3, (byte*)hmac->innerHash, 919 WC_SHA3_224_DIGEST_SIZE); 920 if (ret != 0) 921 break; 922 ret = wc_Sha3_224_Final(&hmac->hash.sha3, hash); 923 break; 924 case WC_SHA3_256: 925 ret = wc_Sha3_256_Final(&hmac->hash.sha3, (byte*)hmac->innerHash); 926 if (ret != 0) 927 break; 928 ret = wc_Sha3_256_Update(&hmac->hash.sha3, (byte*)hmac->opad, 929 WC_SHA3_256_BLOCK_SIZE); 930 if (ret != 0) 931 break; 932 ret = wc_Sha3_256_Update(&hmac->hash.sha3, (byte*)hmac->innerHash, 933 WC_SHA3_256_DIGEST_SIZE); 934 if (ret != 0) 935 break; 936 ret = wc_Sha3_256_Final(&hmac->hash.sha3, hash); 937 break; 938 case WC_SHA3_384: 939 ret = wc_Sha3_384_Final(&hmac->hash.sha3, (byte*)hmac->innerHash); 940 if (ret != 0) 941 break; 942 ret = wc_Sha3_384_Update(&hmac->hash.sha3, (byte*)hmac->opad, 943 WC_SHA3_384_BLOCK_SIZE); 944 if (ret != 0) 945 break; 946 ret = wc_Sha3_384_Update(&hmac->hash.sha3, (byte*)hmac->innerHash, 947 WC_SHA3_384_DIGEST_SIZE); 948 if (ret != 0) 949 break; 950 ret = wc_Sha3_384_Final(&hmac->hash.sha3, hash); 951 break; 952 case WC_SHA3_512: 953 ret = wc_Sha3_512_Final(&hmac->hash.sha3, (byte*)hmac->innerHash); 954 if (ret != 0) 955 break; 956 ret = wc_Sha3_512_Update(&hmac->hash.sha3, (byte*)hmac->opad, 957 WC_SHA3_512_BLOCK_SIZE); 958 if (ret != 0) 959 break; 960 ret = wc_Sha3_512_Update(&hmac->hash.sha3, (byte*)hmac->innerHash, 961 WC_SHA3_512_DIGEST_SIZE); 962 if (ret != 0) 963 break; 964 ret = wc_Sha3_512_Final(&hmac->hash.sha3, hash); 965 break; 966 #endif /* WOLFSSL_SHA3 */ 967 750 968 default: 751 969 ret = BAD_FUNC_ARG; … … 774 992 #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_HMAC) 775 993 hmac->keyLen = 0; 776 #ifdef HAVE_CAVIUM777 hmac->dataLen = 0;778 hmac->data = NULL; /* buffered input data */779 #endif /* HAVE_CAVIUM */780 994 781 995 ret = wolfAsync_DevCtxInit(&hmac->asyncDev, WOLFSSL_ASYNC_MARKER_HMAC, … … 794 1008 return; 795 1009 1010 switch (hmac->macType) { 1011 #ifndef NO_MD5 1012 case WC_MD5: 1013 wc_Md5Free(&hmac->hash.md5); 1014 break; 1015 #endif /* !NO_MD5 */ 1016 1017 #ifndef NO_SHA 1018 case WC_SHA: 1019 wc_ShaFree(&hmac->hash.sha); 1020 break; 1021 #endif /* !NO_SHA */ 1022 1023 #ifdef WOLFSSL_SHA224 1024 case WC_SHA224: 1025 wc_Sha224Free(&hmac->hash.sha224); 1026 break; 1027 #endif /* WOLFSSL_SHA224 */ 1028 1029 #ifndef NO_SHA256 1030 case WC_SHA256: 1031 wc_Sha256Free(&hmac->hash.sha256); 1032 break; 1033 #endif /* !NO_SHA256 */ 1034 1035 #ifdef WOLFSSL_SHA384 1036 case WC_SHA384: 1037 wc_Sha384Free(&hmac->hash.sha384); 1038 break; 1039 #endif /* WOLFSSL_SHA384 */ 1040 #ifdef WOLFSSL_SHA512 1041 case WC_SHA512: 1042 wc_Sha512Free(&hmac->hash.sha512); 1043 break; 1044 #endif /* WOLFSSL_SHA512 */ 1045 1046 #ifdef HAVE_BLAKE2 1047 case BLAKE2B_ID: 1048 break; 1049 #endif /* HAVE_BLAKE2 */ 1050 1051 #ifdef WOLFSSL_SHA3 1052 case WC_SHA3_224: 1053 wc_Sha3_224_Free(&hmac->hash.sha3); 1054 break; 1055 case WC_SHA3_256: 1056 wc_Sha3_256_Free(&hmac->hash.sha3); 1057 break; 1058 case WC_SHA3_384: 1059 wc_Sha3_384_Free(&hmac->hash.sha3); 1060 break; 1061 case WC_SHA3_512: 1062 wc_Sha3_512_Free(&hmac->hash.sha3); 1063 break; 1064 #endif /* WOLFSSL_SHA3 */ 1065 1066 default: 1067 break; 1068 } 1069 796 1070 #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_HMAC) 797 1071 wolfAsync_DevCtxFree(&hmac->asyncDev, WOLFSSL_ASYNC_MARKER_HMAC); 798 799 #ifdef HAVE_CAVIUM800 XFREE(hmac->data, hmac->heap, DYNAMIC_TYPE_HMAC);801 hmac->data = NULL;802 #endif /* HAVE_CAVIUM */803 1072 #endif /* WOLFSSL_ASYNC_CRYPT */ 1073 1074 switch (hmac->macType) { 1075 #ifndef NO_MD5 1076 case WC_MD5: 1077 wc_Md5Free(&hmac->hash.md5); 1078 break; 1079 #endif /* !NO_MD5 */ 1080 1081 #ifndef NO_SHA 1082 case WC_SHA: 1083 wc_ShaFree(&hmac->hash.sha); 1084 break; 1085 #endif /* !NO_SHA */ 1086 1087 #ifdef WOLFSSL_SHA224 1088 case WC_SHA224: 1089 wc_Sha224Free(&hmac->hash.sha224); 1090 break; 1091 #endif /* WOLFSSL_SHA224 */ 1092 1093 #ifndef NO_SHA256 1094 case WC_SHA256: 1095 wc_Sha256Free(&hmac->hash.sha256); 1096 break; 1097 #endif /* !NO_SHA256 */ 1098 1099 #ifdef WOLFSSL_SHA512 1100 #ifdef WOLFSSL_SHA384 1101 case WC_SHA384: 1102 wc_Sha384Free(&hmac->hash.sha384); 1103 break; 1104 #endif /* WOLFSSL_SHA384 */ 1105 case WC_SHA512: 1106 wc_Sha512Free(&hmac->hash.sha512); 1107 break; 1108 #endif /* WOLFSSL_SHA512 */ 1109 } 804 1110 } 805 1111 806 1112 int wolfSSL_GetHmacMaxSize(void) 807 1113 { 808 return MAX_DIGEST_SIZE;1114 return WC_MAX_DIGEST_SIZE; 809 1115 } 810 1116 … … 824 1130 const byte* inKey, word32 inKeySz, byte* out) 825 1131 { 826 byte tmp[ MAX_DIGEST_SIZE]; /* localSalt helper */1132 byte tmp[WC_MAX_DIGEST_SIZE]; /* localSalt helper */ 827 1133 Hmac myHmac; 828 1134 int ret; … … 869 1175 const byte* info, word32 infoSz, byte* out, word32 outSz) 870 1176 { 871 byte tmp[ MAX_DIGEST_SIZE];1177 byte tmp[WC_MAX_DIGEST_SIZE]; 872 1178 Hmac myHmac; 873 1179 int ret = 0; … … 930 1236 byte* out, word32 outSz) 931 1237 { 932 byte prk[ MAX_DIGEST_SIZE];1238 byte prk[WC_MAX_DIGEST_SIZE]; 933 1239 int hashSz = wc_HmacSizeByType(type); 934 1240 int ret;
Note:
See TracChangeset
for help on using the changeset viewer.