Index: azure_iot_hub_f767zi/trunk/wolfssl-4.7.0/wolfcrypt/src/blake2b.c
===================================================================
--- azure_iot_hub_f767zi/trunk/wolfssl-4.4.0/wolfcrypt/src/blake2b.c	(revision 457)
+++ azure_iot_hub_f767zi/trunk/wolfssl-4.7.0/wolfcrypt/src/blake2b.c	(revision 464)
@@ -44,4 +44,5 @@
 #include <wolfssl/wolfcrypt/blake2.h>
 #include <wolfssl/wolfcrypt/blake2-impl.h>
+#include <wolfssl/wolfcrypt/error-crypt.h>
 
 
@@ -125,5 +126,5 @@
   blake2b_param P[1];
 
-  if ( ( !outlen ) || ( outlen > BLAKE2B_OUTBYTES ) ) return -1;
+  if ( ( !outlen ) || ( outlen > BLAKE2B_OUTBYTES ) ) return BAD_FUNC_ARG;
 
 #ifdef WOLFSSL_BLAKE2B_INIT_EACH_FIELD
@@ -154,7 +155,7 @@
   blake2b_param P[1];
 
-  if ( ( !outlen ) || ( outlen > BLAKE2B_OUTBYTES ) ) return -1;
-
-  if ( !key || !keylen || keylen > BLAKE2B_KEYBYTES ) return -1;
+  if ( ( !outlen ) || ( outlen > BLAKE2B_OUTBYTES ) ) return BAD_FUNC_ARG;
+
+  if ( !key || !keylen || keylen > BLAKE2B_KEYBYTES ) return BAD_FUNC_ARG;
 
 #ifdef WOLFSSL_BLAKE2B_INIT_EACH_FIELD
@@ -178,5 +179,8 @@
 #endif
 
-  if( blake2b_init_param( S, P ) < 0 ) return -1;
+  {
+    int ret = blake2b_init_param( S, P );
+    if ( ret < 0 ) return ret;
+  }
 
   {
@@ -186,5 +190,5 @@
     block = (byte*)XMALLOC(BLAKE2B_BLOCKBYTES, NULL, DYNAMIC_TYPE_TMP_BUFFER);
 
-    if ( block == NULL ) return -1;
+    if ( block == NULL ) return MEMORY_E;
 #else
     byte block[BLAKE2B_BLOCKBYTES];
@@ -204,28 +208,11 @@
 }
 
-static int blake2b_compress( blake2b_state *S,
-                             const byte block[BLAKE2B_BLOCKBYTES] )
+static WC_INLINE int blake2b_compress(
+    blake2b_state *S,
+    const byte block[BLAKE2B_BLOCKBYTES],
+    word64* m,
+    word64* v)
 {
   int i;
-
-#ifdef WOLFSSL_SMALL_STACK
-  word64* m;
-  word64* v;
-
-  m = (word64*)XMALLOC(sizeof(word64) * 16, NULL, DYNAMIC_TYPE_TMP_BUFFER);
-
-  if ( m == NULL ) return -1;
-
-  v = (word64*)XMALLOC(sizeof(word64) * 16, NULL, DYNAMIC_TYPE_TMP_BUFFER);
-
-  if ( v == NULL )
-  {
-    XFREE(m, NULL, DYNAMIC_TYPE_TMP_BUFFER);
-    return -1;
-  }
-#else
-  word64 m[16];
-  word64 v[16];
-#endif
 
   for( i = 0; i < 16; ++i )
@@ -284,9 +271,4 @@
 #undef ROUND
 
-#ifdef WOLFSSL_SMALL_STACK
-  XFREE(m, NULL, DYNAMIC_TYPE_TMP_BUFFER);
-  XFREE(v, NULL, DYNAMIC_TYPE_TMP_BUFFER);
-#endif
-
   return 0;
 }
@@ -295,4 +277,19 @@
 int blake2b_update( blake2b_state *S, const byte *in, word64 inlen )
 {
+  int ret = 0;
+#ifdef WOLFSSL_SMALL_STACK
+  word64* m;
+  word64* v;
+
+  m = (word64*)XMALLOC(sizeof(word64) * 32, NULL, DYNAMIC_TYPE_TMP_BUFFER);
+
+  if ( m == NULL ) return MEMORY_E;
+
+  v = &m[16];
+#else
+  word64 m[16];
+  word64 v[16];
+#endif
+
   while( inlen > 0 )
   {
@@ -306,5 +303,8 @@
       blake2b_increment_counter( S, BLAKE2B_BLOCKBYTES );
 
-      if ( blake2b_compress( S, S->buf ) < 0 ) return -1; /* Compress */
+      {
+	ret = blake2b_compress( S, S->buf, m, v );
+	if (ret < 0) break;
+      }
 
       XMEMCPY( S->buf, S->buf + BLAKE2B_BLOCKBYTES, BLAKE2B_BLOCKBYTES );
@@ -322,5 +322,9 @@
   }
 
-  return 0;
+#ifdef WOLFSSL_SMALL_STACK
+  XFREE(m, NULL, DYNAMIC_TYPE_TMP_BUFFER);
+#endif
+
+  return ret;
 }
 
@@ -328,6 +332,20 @@
 int blake2b_final( blake2b_state *S, byte *out, byte outlen )
 {
+  int ret = 0;
   byte buffer[BLAKE2B_OUTBYTES];
   int     i;
+#ifdef WOLFSSL_SMALL_STACK
+  word64* m;
+  word64* v;
+
+  m = (word64*)XMALLOC(sizeof(word64) * 32, NULL, DYNAMIC_TYPE_TMP_BUFFER);
+
+  if ( m == NULL ) return MEMORY_E;
+
+  v = &m[16];
+#else
+  word64 m[16];
+  word64 v[16];
+#endif
 
   if( S->buflen > BLAKE2B_BLOCKBYTES )
@@ -335,5 +353,8 @@
     blake2b_increment_counter( S, BLAKE2B_BLOCKBYTES );
 
-    if ( blake2b_compress( S, S->buf ) < 0 ) return -1;
+    {
+      ret = blake2b_compress( S, S->buf, m, v );
+      if (ret < 0) goto out;
+    }
 
     S->buflen -= BLAKE2B_BLOCKBYTES;
@@ -345,5 +366,8 @@
   XMEMSET( S->buf + S->buflen, 0, (wolfssl_word)(2 * BLAKE2B_BLOCKBYTES - S->buflen) );
          /* Padding */
-  if ( blake2b_compress( S, S->buf ) < 0 ) return -1;
+  {
+    ret = blake2b_compress( S, S->buf, m, v );
+    if (ret < 0) goto out;
+  }
 
   for( i = 0; i < 8; ++i ) /* Output full hash to temp buffer */
@@ -351,5 +375,12 @@
 
   XMEMCPY( out, buffer, outlen );
-  return 0;
+
+ out:
+
+#ifdef WOLFSSL_SMALL_STACK
+  XFREE(m, NULL, DYNAMIC_TYPE_TMP_BUFFER);
+#endif
+
+  return ret;
 }
 
@@ -361,7 +392,7 @@
 
   /* Verify parameters */
-  if ( NULL == in ) return -1;
-
-  if ( NULL == out ) return -1;
+  if ( NULL == in ) return BAD_FUNC_ARG;
+
+  if ( NULL == out ) return BAD_FUNC_ARG;
 
   if( NULL == key ) keylen = 0;
@@ -369,12 +400,17 @@
   if( keylen > 0 )
   {
-    if( blake2b_init_key( S, outlen, key, keylen ) < 0 ) return -1;
+    int ret = blake2b_init_key( S, outlen, key, keylen );
+    if (ret < 0) return ret;
   }
   else
   {
-    if( blake2b_init( S, outlen ) < 0 ) return -1;
-  }
-
-  if ( blake2b_update( S, ( byte * )in, inlen ) < 0) return -1;
+    int ret = blake2b_init( S, outlen );
+    if (ret < 0) return ret;
+  }
+
+  {
+    int ret = blake2b_update( S, ( byte * )in, inlen );
+    if (ret < 0) return ret;
+  }
 
   return blake2b_final( S, out, outlen );
@@ -423,5 +459,5 @@
 {
     if (b2b == NULL){
-        return -1;
+        return BAD_FUNC_ARG;
     }
     b2b->digestSz = digestSz;
@@ -430,4 +466,20 @@
 }
 
+/* Init Blake2b digest with key, track size in case final doesn't want to "remember" */
+int wc_InitBlake2b_WithKey(Blake2b* b2b, word32 digestSz, const byte *key, word32 keylen)
+{
+    if (b2b == NULL){
+        return BAD_FUNC_ARG;
+    }
+    b2b->digestSz = digestSz;
+
+    if (keylen >= 256)
+        return BAD_FUNC_ARG;
+
+    if (key)
+        return blake2b_init_key(b2b->S, (byte)digestSz, key, (byte)keylen);
+    else
+        return blake2b_init(b2b->S, (byte)digestSz);
+}
 
 /* Blake2b Update */
