- 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/sha.c
r352 r372 29 29 #if !defined(NO_SHA) 30 30 31 #if defined(HAVE_FIPS) && \ 32 defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2) 33 34 /* set NO_WRAPPERS before headers, use direct internal f()s not wrappers */ 35 #define FIPS_NO_WRAPPERS 36 37 #ifdef USE_WINDOWS_API 38 #pragma code_seg(".fipsA$j") 39 #pragma const_seg(".fipsB$j") 40 #endif 41 #endif 42 31 43 #include <wolfssl/wolfcrypt/sha.h> 32 44 #include <wolfssl/wolfcrypt/error-crypt.h> 33 45 34 46 /* fips wrapper calls, user can call direct */ 35 #ifdef HAVE_FIPS 47 #if defined(HAVE_FIPS) && \ 48 (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 2)) 49 36 50 int wc_InitSha(wc_Sha* sha) 37 51 { … … 72 86 } 73 87 74 #else /* else build without fips */88 #else /* else build without fips, or for FIPS v2 */ 75 89 76 90 … … 88 102 #endif 89 103 90 static INLINE void AddLength(wc_Sha* sha, word32 len);91 92 104 93 105 /* Hardware Acceleration */ … … 97 109 #elif defined(STM32_HASH) 98 110 99 /* 100 * STM32F2/F4/F7 hardware SHA1 support through the HASH_* API's from the 101 * Standard Peripheral Library or CubeMX (See note in README). 102 */ 103 104 /* STM32 register size, bytes */ 105 #ifdef WOLFSSL_STM32_CUBEMX 106 #define SHA_REG_SIZE WC_SHA_BLOCK_SIZE 107 #else 108 #define SHA_REG_SIZE 4 109 /* STM32 struct notes: 110 * sha->buffer = first 4 bytes used to hold partial block if needed 111 * sha->buffLen = num bytes currently stored in sha->buffer 112 * sha->loLen = num bytes that have been written to STM32 FIFO 113 */ 114 #endif 115 #define SHA_HW_TIMEOUT 0xFF 116 111 /* Supports CubeMX HAL or Standard Peripheral Library */ 117 112 int wc_InitSha_ex(wc_Sha* sha, void* heap, int devId) 118 113 { 119 if (sha == NULL) 114 if (sha == NULL) { 120 115 return BAD_FUNC_ARG; 121 122 sha->heap = heap; 123 XMEMSET(sha->buffer, 0, sizeof(sha->buffer)); 124 sha->buffLen = 0; 125 sha->loLen = 0; 126 sha->hiLen = 0; 127 128 /* initialize HASH peripheral */ 129 #ifdef WOLFSSL_STM32_CUBEMX 130 HAL_HASH_DeInit(&sha->hashHandle); 131 sha->hashHandle.Init.DataType = HASH_DATATYPE_8B; 132 if (HAL_HASH_Init(&sha->hashHandle) != HAL_OK) { 133 return ASYNC_INIT_E; 134 } 135 136 /* reset the hash control register */ 137 /* required because Cube MX is not clearing algo bits */ 138 HASH->CR &= ~HASH_CR_ALGO; 139 #else 140 HASH_DeInit(); 141 142 /* reset the hash control register */ 143 HASH->CR &= ~ (HASH_CR_ALGO | HASH_CR_DATATYPE | HASH_CR_MODE); 144 145 /* configure algo used, algo mode, datatype */ 146 HASH->CR |= (HASH_AlgoSelection_SHA1 | HASH_AlgoMode_HASH 147 | HASH_DataType_8b); 148 149 /* reset HASH processor */ 150 HASH->CR |= HASH_CR_INIT; 151 #endif 116 } 117 118 (void)devId; 119 (void)heap; 120 121 wc_Stm32_Hash_Init(&sha->stmCtx); 152 122 153 123 return 0; … … 156 126 int wc_ShaUpdate(wc_Sha* sha, const byte* data, word32 len) 157 127 { 158 int ret = 0; 159 byte* local; 128 int ret; 160 129 161 130 if (sha == NULL || (data == NULL && len > 0)) { … … 163 132 } 164 133 165 /* do block size increments */ 166 local = (byte*)sha->buffer; 167 168 /* check that internal buffLen is valid */ 169 if (sha->buffLen >= SHA_REG_SIZE) 170 return BUFFER_E; 171 172 while (len) { 173 word32 add = min(len, SHA_REG_SIZE - sha->buffLen); 174 XMEMCPY(&local[sha->buffLen], data, add); 175 176 sha->buffLen += add; 177 data += add; 178 len -= add; 179 180 if (sha->buffLen == SHA_REG_SIZE) { 181 #ifdef WOLFSSL_STM32_CUBEMX 182 if (HAL_HASH_SHA1_Accumulate( 183 &sha->hashHandle, local, SHA_REG_SIZE) != HAL_OK) { 184 ret = ASYNC_OP_E; 185 } 186 #else 187 HASH_DataIn(*(uint32_t*)local); 188 #endif 189 190 AddLength(sha, SHA_REG_SIZE); 191 sha->buffLen = 0; 192 } 134 ret = wolfSSL_CryptHwMutexLock(); 135 if (ret == 0) { 136 ret = wc_Stm32_Hash_Update(&sha->stmCtx, HASH_AlgoSelection_SHA1, 137 data, len); 138 wolfSSL_CryptHwMutexUnLock(); 193 139 } 194 140 return ret; … … 197 143 int wc_ShaFinal(wc_Sha* sha, byte* hash) 198 144 { 199 int ret = 0;200 201 if (sha == NULL || hash == NULL) 145 int ret; 146 147 if (sha == NULL || hash == NULL) { 202 148 return BAD_FUNC_ARG; 203 204 #ifdef WOLFSSL_STM32_CUBEMX 205 if (HAL_HASH_SHA1_Start(&sha->hashHandle, 206 (byte*)sha->buffer, sha->buffLen, 207 (byte*)sha->digest, SHA_HW_TIMEOUT) != HAL_OK) { 208 ret = ASYNC_OP_E; 209 } 210 HAL_HASH_DeInit(&sha->hashHandle); 211 #else 212 __IO uint16_t nbvalidbitsdata = 0; 213 214 /* finish reading any trailing bytes into FIFO */ 215 if (sha->buffLen > 0) { 216 HASH_DataIn(*(uint32_t*)sha->buffer); 217 AddLength(sha, sha->buffLen); 218 } 219 220 /* calculate number of valid bits in last word of input data */ 221 nbvalidbitsdata = 8 * (sha->loLen % SHA_REG_SIZE); 222 223 /* configure number of valid bits in last word of the data */ 224 HASH_SetLastWordValidBitsNbr(nbvalidbitsdata); 225 226 /* start HASH processor */ 227 HASH_StartDigest(); 228 229 /* wait until Busy flag == RESET */ 230 while (HASH_GetFlagStatus(HASH_FLAG_BUSY) != RESET) {} 231 232 /* read message digest */ 233 sha->digest[0] = HASH->HR[0]; 234 sha->digest[1] = HASH->HR[1]; 235 sha->digest[2] = HASH->HR[2]; 236 sha->digest[3] = HASH->HR[3]; 237 sha->digest[4] = HASH->HR[4]; 238 239 ByteReverseWords(sha->digest, sha->digest, WC_SHA_DIGEST_SIZE); 240 #endif /* WOLFSSL_STM32_CUBEMX */ 241 242 XMEMCPY(hash, sha->digest, WC_SHA_DIGEST_SIZE); 243 244 (void)wc_InitSha_ex(sha, sha->heap, INVALID_DEVID); /* reset state */ 149 } 150 151 ret = wolfSSL_CryptHwMutexLock(); 152 if (ret == 0) { 153 ret = wc_Stm32_Hash_Final(&sha->stmCtx, HASH_AlgoSelection_SHA1, 154 hash, WC_SHA_DIGEST_SIZE); 155 wolfSSL_CryptHwMutexUnLock(); 156 } 157 158 (void)wc_InitSha(sha); /* reset state */ 245 159 246 160 return ret; … … 251 165 252 166 #include "fsl_ltc.h" 253 static int InitSha(wc_Sha* sha) 254 { 167 int wc_InitSha_ex(wc_Sha* sha, void* heap, int devId) 168 { 169 if (sha == NULL) { 170 return BAD_FUNC_ARG; 171 } 172 173 (void)devId; 174 (void)heap; 175 255 176 LTC_HASH_Init(LTC_BASE, &sha->ctx, kLTC_Sha1, NULL, 0); 256 177 return 0; … … 317 238 } 318 239 240 #elif defined(WOLFSSL_IMX6_CAAM) && !defined(NO_IMX6_CAAM_HASH) 241 /* wolfcrypt/src/port/caam/caam_sha.c */ 319 242 #else 320 243 … … 342 265 343 266 344 #if defined(USE_SHA_SOFTWARE_IMPL) || defined(STM32_HASH) 345 static INLINE void AddLength(wc_Sha* sha, word32 len) 267 /* Software implementation */ 268 #ifdef USE_SHA_SOFTWARE_IMPL 269 270 static WC_INLINE void AddLength(wc_Sha* sha, word32 len) 346 271 { 347 272 word32 tmp = sha->loLen; … … 349 274 sha->hiLen++; /* carry low to high */ 350 275 } 351 #endif352 353 354 /* Software implementation */355 #ifdef USE_SHA_SOFTWARE_IMPL356 276 357 277 /* Check if custom wc_Sha transform is used */ … … 367 287 #define f3(x,y,z) (((x)&(y))|((z)&((x)|(y)))) 368 288 #define f4(x,y,z) ((x)^(y)^(z)) 289 290 #ifdef WOLFSSL_NUCLEUS_1_2 291 /* nucleus.h also defines R1-R4 */ 292 #undef R1 293 #undef R2 294 #undef R3 295 #undef R4 296 #endif 369 297 370 298 /* (R0+R1), R2, R3, R4 are the different operations used in SHA1 */ … … 526 454 } 527 455 456 int wc_ShaFinalRaw(wc_Sha* sha, byte* hash) 457 { 458 #ifdef LITTLE_ENDIAN_ORDER 459 word32 digest[WC_SHA_DIGEST_SIZE / sizeof(word32)]; 460 #endif 461 462 if (sha == NULL || hash == NULL) { 463 return BAD_FUNC_ARG; 464 } 465 466 #ifdef LITTLE_ENDIAN_ORDER 467 ByteReverseWords((word32*)digest, (word32*)sha->digest, WC_SHA_DIGEST_SIZE); 468 XMEMCPY(hash, digest, WC_SHA_DIGEST_SIZE); 469 #else 470 XMEMCPY(hash, sha->digest, WC_SHA_DIGEST_SIZE); 471 #endif 472 473 return 0; 474 } 475 528 476 int wc_ShaFinal(wc_Sha* sha, byte* hash) 529 477 { … … 606 554 wolfAsync_DevCtxFree(&sha->asyncDev, WOLFSSL_ASYNC_MARKER_SHA); 607 555 #endif /* WOLFSSL_ASYNC_CRYPT */ 556 557 #ifdef WOLFSSL_PIC32MZ_HASH 558 wc_ShaPic32Free(sha); 559 #endif 608 560 } 609 561
Note:
See TracChangeset
for help on using the changeset viewer.