Index: asp3_tinet_ecnl_rx/trunk/wolfssl-3.12.2/wolfcrypt/src/wolfmath.c
===================================================================
--- asp3_tinet_ecnl_rx/trunk/wolfssl-3.12.2/wolfcrypt/src/wolfmath.c	(revision 337)
+++ asp3_tinet_ecnl_rx/trunk/wolfssl-3.12.2/wolfcrypt/src/wolfmath.c	(revision 372)
@@ -30,9 +30,5 @@
 #include <wolfssl/wolfcrypt/settings.h>
 
-#ifdef USE_FAST_MATH
-    #include <wolfssl/wolfcrypt/tfm.h>
-#else
     #include <wolfssl/wolfcrypt/integer.h>
-#endif
 
 #include <wolfssl/wolfcrypt/error-crypt.h>
@@ -92,4 +88,5 @@
 }
 
+#ifndef WC_NO_RNG
 int get_rand_digit(WC_RNG* rng, mp_digit* d)
 {
@@ -100,45 +97,89 @@
 int mp_rand(mp_int* a, int digits, WC_RNG* rng)
 {
-    int ret;
-    mp_digit d;
-
-    if (rng == NULL)
-        return MISSING_RNG_E;
-
-    if (a == NULL)
-        return BAD_FUNC_ARG;
+    int ret = 0;
+    DECLARE_VAR(d, mp_digit, 1, rng ? rng->heap : NULL);
+
+    if (rng == NULL) {
+        ret = MISSING_RNG_E; goto exit;
+    }
+
+    if (a == NULL
+    #ifdef WOLFSSL_ASYNC_CRYPT
+        || d == NULL
+    #endif
+    ) {
+        ret = BAD_FUNC_ARG; goto exit;
+    }
 
     mp_zero(a);
     if (digits <= 0) {
-        return MP_OKAY;
+        ret = MP_OKAY; goto exit;
     }
 
     /* first place a random non-zero digit */
     do {
-        ret = get_rand_digit(rng, &d);
+        ret = get_rand_digit(rng, d);
         if (ret != 0) {
-            return ret;
-        }
-    } while (d == 0);
-
-    if ((ret = mp_add_d(a, d, a)) != MP_OKAY) {
-        return ret;
+            goto exit;
+        }
+    } while (*d == 0);
+
+    if ((ret = mp_add_d(a, *d, a)) != MP_OKAY) {
+        goto exit;
     }
 
     while (--digits > 0) {
         if ((ret = mp_lshd(a, 1)) != MP_OKAY) {
-            return ret;
-        }
-        if ((ret = get_rand_digit(rng, &d)) != 0) {
-            return ret;
-        }
-        if ((ret = mp_add_d(a, d, a)) != MP_OKAY) {
-            return ret;
-        }
-    }
+            goto exit;
+        }
+        if ((ret = get_rand_digit(rng, d)) != 0) {
+            goto exit;
+        }
+        if ((ret = mp_add_d(a, *d, a)) != MP_OKAY) {
+            goto exit;
+        }
+    }
+
+exit:
+    FREE_VAR(d, rng ? rng->heap : NULL);
 
     return ret;
 }
 #endif /* WC_RSA_BLINDING */
+#endif
+
+/* export an mp_int as unsigned char or hex string
+ * encType is WC_TYPE_UNSIGNED_BIN or WC_TYPE_HEX_STR
+ * return MP_OKAY on success */
+int wc_export_int(mp_int* mp, byte* buf, word32* len, word32 keySz,
+    int encType)
+{
+    int err;
+
+    if (mp == NULL)
+        return BAD_FUNC_ARG;
+
+    /* check buffer size */
+    if (*len < keySz) {
+        *len = keySz;
+        return BUFFER_E;
+    }
+
+    *len = keySz;
+    XMEMSET(buf, 0, *len);
+
+    if (encType == WC_TYPE_HEX_STR) {
+    #ifdef WC_MP_TO_RADIX
+        err = mp_tohex(mp, (char*)buf);
+    #else
+        err = NOT_COMPILED_IN;
+    #endif
+    }
+    else {
+        err = mp_to_unsigned_bin(mp, buf + (keySz - mp_unsigned_bin_size(mp)));
+    }
+
+    return err;
+}
 
 
@@ -236,18 +277,42 @@
 }
 
+/* sz: make sure the buffer is at least that size and zero padded.
+ *     A `sz == 0` will use the size of `src`.
+ *     The calulcates sz is stored into dst->len in `wc_bigint_alloc`.
+ */
+int wc_mp_to_bigint_sz(mp_int* src, WC_BIGINT* dst, word32 sz)
+{
+    int err;
+    word32 x, y;
+
+    if (src == NULL || dst == NULL)
+        return BAD_FUNC_ARG;
+
+    /* get size of source */
+    x = mp_unsigned_bin_size(src);
+    if (sz < x)
+        sz = x;
+
+    /* make sure destination is allocated and large enough */
+    err = wc_bigint_alloc(dst, sz);
+    if (err == MP_OKAY) {
+
+        /* leading zero pad */
+        y = sz - x;
+        XMEMSET(dst->buf, 0, y);
+
+        /* export src as unsigned bin to destination buf */
+        err = mp_to_unsigned_bin(src, dst->buf + y);
+    }
+
+    return err;
+}
+
 int wc_mp_to_bigint(mp_int* src, WC_BIGINT* dst)
 {
-    int err;
-    word32 sz;
-
     if (src == NULL || dst == NULL)
         return BAD_FUNC_ARG;
 
-    sz = mp_unsigned_bin_size(src);
-    err = wc_bigint_alloc(dst, sz);
-    if (err == MP_OKAY)
-        err = mp_to_unsigned_bin(src, dst->buf);
-
-    return err;
+    return wc_mp_to_bigint_sz(src, dst, 0);
 }
 
