Index: asp3_tinet_ecnl_rx/trunk/wolfssl-3.12.2/wolfcrypt/src/sha.c
===================================================================
--- asp3_tinet_ecnl_rx/trunk/wolfssl-3.12.2/wolfcrypt/src/sha.c	(revision 337)
+++ asp3_tinet_ecnl_rx/trunk/wolfssl-3.12.2/wolfcrypt/src/sha.c	(revision 372)
@@ -29,9 +29,23 @@
 #if !defined(NO_SHA)
 
+#if defined(HAVE_FIPS) && \
+	defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)
+
+    /* set NO_WRAPPERS before headers, use direct internal f()s not wrappers */
+    #define FIPS_NO_WRAPPERS
+
+    #ifdef USE_WINDOWS_API
+        #pragma code_seg(".fipsA$j")
+        #pragma const_seg(".fipsB$j")
+    #endif
+#endif
+
 #include <wolfssl/wolfcrypt/sha.h>
 #include <wolfssl/wolfcrypt/error-crypt.h>
 
 /* fips wrapper calls, user can call direct */
-#ifdef HAVE_FIPS
+#if defined(HAVE_FIPS) && \
+    (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 2))
+
     int wc_InitSha(wc_Sha* sha)
     {
@@ -72,5 +86,5 @@
     }
 
-#else /* else build without fips */
+#else /* else build without fips, or for FIPS v2 */
 
 
@@ -88,6 +102,4 @@
 #endif
 
-static INLINE void AddLength(wc_Sha* sha, word32 len);
-
 
 /* Hardware Acceleration */
@@ -97,57 +109,15 @@
 #elif defined(STM32_HASH)
 
-    /*
-     * STM32F2/F4/F7 hardware SHA1 support through the HASH_* API's from the
-     * Standard Peripheral Library or CubeMX (See note in README).
-     */
-
-    /* STM32 register size, bytes */
-    #ifdef WOLFSSL_STM32_CUBEMX
-        #define SHA_REG_SIZE  WC_SHA_BLOCK_SIZE
-    #else
-        #define SHA_REG_SIZE  4
-        /* STM32 struct notes:
-         * sha->buffer  = first 4 bytes used to hold partial block if needed
-         * sha->buffLen = num bytes currently stored in sha->buffer
-         * sha->loLen   = num bytes that have been written to STM32 FIFO
-         */
-    #endif
-    #define SHA_HW_TIMEOUT 0xFF
-
+    /* Supports CubeMX HAL or Standard Peripheral Library */
 	int wc_InitSha_ex(wc_Sha* sha, void* heap, int devId)
     {
-		if (sha == NULL)
+        if (sha == NULL) {
 			return BAD_FUNC_ARG;
-
-		sha->heap = heap;
-        XMEMSET(sha->buffer, 0, sizeof(sha->buffer));
-        sha->buffLen = 0;
-        sha->loLen = 0;
-        sha->hiLen = 0;
-
-        /* initialize HASH peripheral */
-    #ifdef WOLFSSL_STM32_CUBEMX
-        HAL_HASH_DeInit(&sha->hashHandle);
-        sha->hashHandle.Init.DataType = HASH_DATATYPE_8B;
-        if (HAL_HASH_Init(&sha->hashHandle) != HAL_OK) {
-            return ASYNC_INIT_E;
-        }
-
-        /* reset the hash control register */
-        /* required because Cube MX is not clearing algo bits */
-        HASH->CR &= ~HASH_CR_ALGO;
-    #else
-        HASH_DeInit();
-
-        /* reset the hash control register */
-        HASH->CR &= ~ (HASH_CR_ALGO | HASH_CR_DATATYPE | HASH_CR_MODE);
-
-        /* configure algo used, algo mode, datatype */
-        HASH->CR |= (HASH_AlgoSelection_SHA1 | HASH_AlgoMode_HASH
-                   | HASH_DataType_8b);
-
-        /* reset HASH processor */
-        HASH->CR |= HASH_CR_INIT;
-    #endif
+        }
+
+        (void)devId;
+        (void)heap;
+
+        wc_Stm32_Hash_Init(&sha->stmCtx);
 
         return 0;
@@ -156,6 +126,5 @@
     int wc_ShaUpdate(wc_Sha* sha, const byte* data, word32 len)
     {
-        int ret = 0;
-        byte* local;
+        int ret;
 
         if (sha == NULL || (data == NULL && len > 0)) {
@@ -163,32 +132,9 @@
         }
 
-        /* do block size increments */
-        local = (byte*)sha->buffer;
-
-        /* check that internal buffLen is valid */
-        if (sha->buffLen >= SHA_REG_SIZE)
-            return BUFFER_E;
-
-        while (len) {
-            word32 add = min(len, SHA_REG_SIZE - sha->buffLen);
-            XMEMCPY(&local[sha->buffLen], data, add);
-
-            sha->buffLen += add;
-            data         += add;
-            len          -= add;
-
-            if (sha->buffLen == SHA_REG_SIZE) {
-            #ifdef WOLFSSL_STM32_CUBEMX
-                if (HAL_HASH_SHA1_Accumulate(
-                        &sha->hashHandle, local, SHA_REG_SIZE) != HAL_OK) {
-                    ret = ASYNC_OP_E;
-                }
-            #else
-                HASH_DataIn(*(uint32_t*)local);
-            #endif
-
-                AddLength(sha, SHA_REG_SIZE);
-                sha->buffLen = 0;
-            }
+        ret = wolfSSL_CryptHwMutexLock();
+        if (ret == 0) {
+            ret = wc_Stm32_Hash_Update(&sha->stmCtx, HASH_AlgoSelection_SHA1,
+                data, len);
+            wolfSSL_CryptHwMutexUnLock();
         }
         return ret;
@@ -197,50 +143,18 @@
     int wc_ShaFinal(wc_Sha* sha, byte* hash)
     {
-        int ret = 0;
-
-        if (sha == NULL || hash == NULL)
+        int ret;
+
+        if (sha == NULL || hash == NULL) {
             return BAD_FUNC_ARG;
-
-    #ifdef WOLFSSL_STM32_CUBEMX
-        if (HAL_HASH_SHA1_Start(&sha->hashHandle,
-                (byte*)sha->buffer, sha->buffLen,
-                (byte*)sha->digest, SHA_HW_TIMEOUT) != HAL_OK) {
-            ret = ASYNC_OP_E;
-        }
-        HAL_HASH_DeInit(&sha->hashHandle);
-    #else
-        __IO uint16_t nbvalidbitsdata = 0;
-
-        /* finish reading any trailing bytes into FIFO */
-        if (sha->buffLen > 0) {
-            HASH_DataIn(*(uint32_t*)sha->buffer);
-            AddLength(sha, sha->buffLen);
-        }
-
-        /* calculate number of valid bits in last word of input data */
-        nbvalidbitsdata = 8 * (sha->loLen % SHA_REG_SIZE);
-
-        /* configure number of valid bits in last word of the data */
-        HASH_SetLastWordValidBitsNbr(nbvalidbitsdata);
-
-        /* start HASH processor */
-        HASH_StartDigest();
-
-        /* wait until Busy flag == RESET */
-        while (HASH_GetFlagStatus(HASH_FLAG_BUSY) != RESET) {}
-
-        /* read message digest */
-        sha->digest[0] = HASH->HR[0];
-        sha->digest[1] = HASH->HR[1];
-        sha->digest[2] = HASH->HR[2];
-        sha->digest[3] = HASH->HR[3];
-        sha->digest[4] = HASH->HR[4];
-
-        ByteReverseWords(sha->digest, sha->digest, WC_SHA_DIGEST_SIZE);
-    #endif /* WOLFSSL_STM32_CUBEMX */
-
-        XMEMCPY(hash, sha->digest, WC_SHA_DIGEST_SIZE);
-
-        (void)wc_InitSha_ex(sha, sha->heap, INVALID_DEVID);  /* reset state */
+        }
+
+        ret = wolfSSL_CryptHwMutexLock();
+        if (ret == 0) {
+            ret = wc_Stm32_Hash_Final(&sha->stmCtx, HASH_AlgoSelection_SHA1,
+                hash, WC_SHA_DIGEST_SIZE);
+            wolfSSL_CryptHwMutexUnLock();
+        }
+
+        (void)wc_InitSha(sha);  /* reset state */
 
         return ret;
@@ -251,6 +165,13 @@
 
     #include "fsl_ltc.h"
-    static int InitSha(wc_Sha* sha)
-    {
+    int wc_InitSha_ex(wc_Sha* sha, void* heap, int devId)
+    {
+        if (sha == NULL) {
+            return BAD_FUNC_ARG;
+        }
+
+        (void)devId;
+        (void)heap;
+
         LTC_HASH_Init(LTC_BASE, &sha->ctx, kLTC_Sha1, NULL, 0);
         return 0;
@@ -317,4 +238,6 @@
     }
 
+#elif defined(WOLFSSL_IMX6_CAAM) && !defined(NO_IMX6_CAAM_HASH)
+    /* wolfcrypt/src/port/caam/caam_sha.c */
 #else
 
@@ -342,6 +265,8 @@
 
 
-#if defined(USE_SHA_SOFTWARE_IMPL) || defined(STM32_HASH)
-static INLINE void AddLength(wc_Sha* sha, word32 len)
+/* Software implementation */
+#ifdef USE_SHA_SOFTWARE_IMPL
+
+static WC_INLINE void AddLength(wc_Sha* sha, word32 len)
 {
     word32 tmp = sha->loLen;
@@ -349,9 +274,4 @@
         sha->hiLen++;                       /* carry low to high */
 }
-#endif
-
-
-/* Software implementation */
-#ifdef USE_SHA_SOFTWARE_IMPL
 
 /* Check if custom wc_Sha transform is used */
@@ -367,4 +287,12 @@
     #define f3(x,y,z) (((x)&(y))|((z)&((x)|(y))))
     #define f4(x,y,z) ((x)^(y)^(z))
+
+    #ifdef WOLFSSL_NUCLEUS_1_2
+        /* nucleus.h also defines R1-R4 */
+        #undef R1
+        #undef R2
+        #undef R3
+        #undef R4
+    #endif
 
     /* (R0+R1), R2, R3, R4 are the different operations used in SHA1 */
@@ -526,4 +454,24 @@
 }
 
+int wc_ShaFinalRaw(wc_Sha* sha, byte* hash)
+{
+#ifdef LITTLE_ENDIAN_ORDER
+    word32 digest[WC_SHA_DIGEST_SIZE / sizeof(word32)];
+#endif
+
+    if (sha == NULL || hash == NULL) {
+        return BAD_FUNC_ARG;
+    }
+
+#ifdef LITTLE_ENDIAN_ORDER
+    ByteReverseWords((word32*)digest, (word32*)sha->digest, WC_SHA_DIGEST_SIZE);
+    XMEMCPY(hash, digest, WC_SHA_DIGEST_SIZE);
+#else
+    XMEMCPY(hash, sha->digest, WC_SHA_DIGEST_SIZE);
+#endif
+
+    return 0;
+}
+
 int wc_ShaFinal(wc_Sha* sha, byte* hash)
 {
@@ -606,4 +554,8 @@
     wolfAsync_DevCtxFree(&sha->asyncDev, WOLFSSL_ASYNC_MARKER_SHA);
 #endif /* WOLFSSL_ASYNC_CRYPT */
+
+#ifdef WOLFSSL_PIC32MZ_HASH
+    wc_ShaPic32Free(sha);
+#endif
 }
 
