diff --git a/IDE/IAR-EWARM/Projects/lib/wolfSSL-Lib.ewp b/IDE/IAR-EWARM/Projects/lib/wolfSSL-Lib.ewp
index 61982d704..219a61c9a 100644
--- a/IDE/IAR-EWARM/Projects/lib/wolfSSL-Lib.ewp
+++ b/IDE/IAR-EWARM/Projects/lib/wolfSSL-Lib.ewp
@@ -2040,6 +2040,12 @@
$PROJ_DIR$\..\..\..\..\wolfcrypt\src\wc_port.c
+
+ $PROJ_DIR$\..\..\..\..\wolfcrypt\src\wolfmath.c
+
+
+ $PROJ_DIR$\..\..\..\..\wolfcrypt\src\wolfevent.c
+
wolfSSL
diff --git a/IDE/ROWLEY-CROSSWORKS-ARM/wolfssl.hzp b/IDE/ROWLEY-CROSSWORKS-ARM/wolfssl.hzp
index ad5c68af8..3deb98b3e 100644
--- a/IDE/ROWLEY-CROSSWORKS-ARM/wolfssl.hzp
+++ b/IDE/ROWLEY-CROSSWORKS-ARM/wolfssl.hzp
@@ -102,6 +102,8 @@
+
+
diff --git a/IDE/WIN/wolfssl-fips.vcxproj b/IDE/WIN/wolfssl-fips.vcxproj
index 8575aeb9a..10977ceb3 100644
--- a/IDE/WIN/wolfssl-fips.vcxproj
+++ b/IDE/WIN/wolfssl-fips.vcxproj
@@ -300,6 +300,7 @@
+
@@ -324,4 +325,4 @@
-
\ No newline at end of file
+
diff --git a/src/include.am b/src/include.am
index 82be0c1a0..031e9645c 100644
--- a/src/include.am
+++ b/src/include.am
@@ -120,7 +120,8 @@ src_libwolfssl_la_SOURCES += \
wolfcrypt/src/wc_encrypt.c \
wolfcrypt/src/wc_port.c \
wolfcrypt/src/error.c \
- wolfcrypt/src/signature.c
+ wolfcrypt/src/signature.c \
+ wolfcrypt/src/wolfmath.c
if BUILD_MEMORY
src_libwolfssl_la_SOURCES += wolfcrypt/src/memory.c
diff --git a/wolfcrypt/src/ecc.c b/wolfcrypt/src/ecc.c
index ecdc408a2..0ebfbda56 100644
--- a/wolfcrypt/src/ecc.c
+++ b/wolfcrypt/src/ecc.c
@@ -982,24 +982,6 @@ static int wc_ecc_set_curve(ecc_key* key, int keysize, int curve_id)
#ifndef WOLFSSL_ATECC508A
-/* helper for either lib */
-static int get_digit_count(mp_int* a)
-{
- if (a == NULL)
- return 0;
-
- return a->used;
-}
-
-/* helper for either lib */
-static mp_digit get_digit(mp_int* a, int n)
-{
- if (a == NULL)
- return 0;
-
- return (n >= a->used || n < 0) ? 0 : a->dp[n];
-}
-
/**
Add two ECC points
P The point to add
diff --git a/wolfcrypt/src/rsa.c b/wolfcrypt/src/rsa.c
index df475d800..fba54b011 100644
--- a/wolfcrypt/src/rsa.c
+++ b/wolfcrypt/src/rsa.c
@@ -846,72 +846,6 @@ static int wc_RsaUnPad_ex(byte* pkcsBlock, word32 pkcsBlockLen, byte** out,
return ret;
}
-
-#ifdef WC_RSA_BLINDING
-
-/* helper for either lib */
-static int get_digit_count(mp_int* a)
-{
- if (a == NULL)
- return 0;
-
- return a->used;
-}
-
-
-static int get_rand_digit(WC_RNG* rng, mp_digit* d)
-{
- return wc_RNG_GenerateBlock(rng, (byte*)d, sizeof(mp_digit));
-}
-
-
-static 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;
-
- mp_zero(a);
- if (digits <= 0) {
- return MP_OKAY;
- }
-
- /* first place a random non-zero digit */
- do {
- 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;
- }
-
- 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;
- }
- }
-
- return ret;
-}
-
-
-#endif /* WC_RSA_BLINGING */
-
-
static int wc_RsaFunctionSync(const byte* in, word32 inLen, byte* out,
word32* outLen, int type, RsaKey* key, WC_RNG* rng)
{
diff --git a/wolfcrypt/src/tfm.c b/wolfcrypt/src/tfm.c
index 71b5735ea..bafcc8029 100644
--- a/wolfcrypt/src/tfm.c
+++ b/wolfcrypt/src/tfm.c
@@ -49,6 +49,7 @@
#include
#include
#include /* will define asm MACROS or C ones */
+#include /* common functions */
#if defined(FREESCALE_LTC_TFM)
#include
@@ -1004,12 +1005,12 @@ int fp_mulmod(fp_int *a, fp_int *b, fp_int *c, fp_int *d)
fp_init(&t);
fp_mul(a, b, &t);
-#ifdef ALT_ECC_SIZE
- err = fp_mod(&t, c, &t);
- fp_copy(&t, d);
-#else
- err = fp_mod(&t, c, d);
-#endif
+ if (d->size < FP_SIZE) {
+ err = fp_mod(&t, c, &t);
+ fp_copy(&t, d);
+ } else {
+ err = fp_mod(&t, c, d);
+ }
return err;
}
@@ -1022,12 +1023,12 @@ int fp_submod(fp_int *a, fp_int *b, fp_int *c, fp_int *d)
fp_init(&t);
fp_sub(a, b, &t);
-#ifdef ALT_ECC_SIZE
- err = fp_mod(&t, c, &t);
- fp_copy(&t, d);
-#else
- err = fp_mod(&t, c, d);
-#endif
+ if (d->size < FP_SIZE) {
+ err = fp_mod(&t, c, &t);
+ fp_copy(&t, d);
+ } else {
+ err = fp_mod(&t, c, d);
+ }
return err;
}
@@ -1040,12 +1041,12 @@ int fp_addmod(fp_int *a, fp_int *b, fp_int *c, fp_int *d)
fp_init(&t);
fp_add(a, b, &t);
-#ifdef ALT_ECC_SIZE
- err = fp_mod(&t, c, &t);
- fp_copy(&t, d);
-#else
- err = fp_mod(&t, c, d);
-#endif
+ if (d->size < FP_SIZE) {
+ err = fp_mod(&t, c, &t);
+ fp_copy(&t, d);
+ } else {
+ err = fp_mod(&t, c, d);
+ }
return err;
}
@@ -2167,12 +2168,12 @@ void fp_sub_d(fp_int *a, fp_digit b, fp_int *c)
fp_int tmp;
fp_init(&tmp);
fp_set(&tmp, b);
-#ifdef ALT_ECC_SIZE
- fp_sub(a, &tmp, &tmp);
- fp_copy(&tmp, c);
-#else
- fp_sub(a, &tmp, c);
- #endif
+ if (c->size < FP_SIZE) {
+ fp_sub(a, &tmp, &tmp);
+ fp_copy(&tmp, c);
+ } else {
+ fp_sub(a, &tmp, c);
+ }
}
@@ -2186,7 +2187,6 @@ int mp_init (mp_int * a)
return MP_OKAY;
}
-#ifdef ALT_ECC_SIZE
void fp_init(fp_int *a)
{
a->size = FP_SIZE;
@@ -2206,7 +2206,6 @@ void fp_clear(fp_int *a)
a->sign = FP_ZPOS;
ForceZero(a->dp, a->size * sizeof(fp_digit));
}
-#endif
/* clear one (frees) */
@@ -2347,7 +2346,6 @@ int mp_div_2d(fp_int* a, int b, fp_int* c, fp_int* d)
return MP_OKAY;
}
-#ifdef ALT_ECC_SIZE
void fp_copy(fp_int *a, fp_int *b)
{
if (a != b && b->size >= a->used) {
@@ -2372,7 +2370,6 @@ void fp_init_copy(fp_int *a, fp_int* b)
fp_copy(b, a);
}
}
-#endif
/* fast math wrappers */
int mp_copy(fp_int* a, fp_int* b)
@@ -2432,12 +2429,14 @@ int fp_sqrmod(fp_int *a, fp_int *b, fp_int *c)
fp_init(&t);
fp_sqr(a, &t);
-#ifdef ALT_ECC_SIZE
- err = fp_mod(&t, b, &t);
- fp_copy(&t, c);
-#else
- err = fp_mod(&t, b, c);
-#endif
+
+ if (c->size < FP_SIZE) {
+ err = fp_mod(&t, b, &t);
+ fp_copy(&t, c);
+ }
+ else {
+ err = fp_mod(&t, b, c);
+ }
return err;
}
@@ -2850,7 +2849,7 @@ int fp_randprime(fp_int* N, int len, WC_RNG* rng, void* heap)
XMEMSET(buf, 0, len);
XFREE(buf, heap, DYNAMIC_TYPE_TMP_BUFFER);
-
+
return FP_OKAY;
}
@@ -3172,14 +3171,9 @@ int mp_toradix (mp_int *a, char *str, int radix)
void mp_dump(const char* desc, mp_int* a, byte verbose)
{
char buffer[FP_SIZE * sizeof(fp_digit) * 2];
- int size = FP_SIZE;
-
-#ifdef ALT_ECC_SIZE
- size = a->size;
-#endif
printf("%s: ptr=%p, used=%d, sign=%d, size=%d, fpd=%d\n",
- desc, a, a->used, a->sign, size, (int)sizeof(fp_digit));
+ desc, a, a->used, a->sign, a->size, (int)sizeof(fp_digit));
mp_toradix(a, buffer, 16);
printf(" %s\n ", buffer);
diff --git a/wolfcrypt/src/wolfmath.c b/wolfcrypt/src/wolfmath.c
new file mode 100644
index 000000000..9b4ede53a
--- /dev/null
+++ b/wolfcrypt/src/wolfmath.c
@@ -0,0 +1,104 @@
+/* wolfmath.c
+ *
+ * Copyright (C) 2006-2016 wolfSSL Inc.
+ *
+ * This file is part of wolfSSL.
+ *
+ * wolfSSL is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * wolfSSL is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
+ */
+
+
+/* common functions for either math library */
+
+#ifdef HAVE_CONFIG_H
+ #include
+#endif
+
+/* in case user set USE_FAST_MATH there */
+#include
+
+#ifdef USE_FAST_MATH
+ #include
+#else
+ #include
+#endif
+
+#include
+#include
+
+
+int get_digit_count(mp_int* a)
+{
+ if (a == NULL)
+ return 0;
+
+ return a->used;
+}
+
+mp_digit get_digit(mp_int* a, int n)
+{
+ if (a == NULL)
+ return 0;
+
+ return (n >= a->used || n < 0) ? 0 : a->dp[n];
+}
+
+int get_rand_digit(WC_RNG* rng, mp_digit* d)
+{
+ return wc_RNG_GenerateBlock(rng, (byte*)d, sizeof(mp_digit));
+}
+
+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;
+
+ mp_zero(a);
+ if (digits <= 0) {
+ return MP_OKAY;
+ }
+
+ /* first place a random non-zero digit */
+ do {
+ 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;
+ }
+
+ 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;
+ }
+ }
+
+ return ret;
+}
diff --git a/wolfssl-ntru.vcproj b/wolfssl-ntru.vcproj
index a9f5c4577..3b7703c1b 100755
--- a/wolfssl-ntru.vcproj
+++ b/wolfssl-ntru.vcproj
@@ -274,6 +274,10 @@
RelativePath=".\wolfcrypt\src\wc_port.c"
>
+
+
diff --git a/wolfssl.vcproj b/wolfssl.vcproj
index 106ba29fe..6843f4072 100755
--- a/wolfssl.vcproj
+++ b/wolfssl.vcproj
@@ -271,6 +271,10 @@
RelativePath=".\wolfcrypt\src\wc_port.c"
>
+
+
diff --git a/wolfssl.vcxproj b/wolfssl.vcxproj
index 985f3383b..7824a9b18 100644
--- a/wolfssl.vcxproj
+++ b/wolfssl.vcxproj
@@ -318,6 +318,7 @@
+
diff --git a/wolfssl/wolfcrypt/include.am b/wolfssl/wolfcrypt/include.am
index 7c9c0fb7f..ca33c8b1e 100644
--- a/wolfssl/wolfcrypt/include.am
+++ b/wolfssl/wolfcrypt/include.am
@@ -58,7 +58,8 @@ nobase_include_HEADERS+= \
wolfssl/wolfcrypt/mpi_superclass.h \
wolfssl/wolfcrypt/mem_track.h \
wolfssl/wolfcrypt/wolfevent.h \
- wolfssl/wolfcrypt/pkcs12.h
+ wolfssl/wolfcrypt/pkcs12.h \
+ wolfssl/wolfcrypt/wolfmath.h
noinst_HEADERS+= \
wolfssl/wolfcrypt/port/pic32/pic32mz-crypt.h \
diff --git a/wolfssl/wolfcrypt/integer.h b/wolfssl/wolfcrypt/integer.h
index c965330ea..7cd447a4c 100644
--- a/wolfssl/wolfcrypt/integer.h
+++ b/wolfssl/wolfcrypt/integer.h
@@ -64,7 +64,7 @@ extern "C" {
/* C on the other hand doesn't care */
#define OPT_CAST(x)
-#endif
+#endif /* __cplusplus */
/* detect 64-bit mode if possible */
@@ -179,7 +179,7 @@ typedef int mp_err;
#define MP_WARRAY (1 << (sizeof(mp_word) * CHAR_BIT - 2 * DIGIT_BIT + 1))
/* the infamous mp_int structure */
-typedef struct {
+typedef struct mp_int {
int used, alloc, sign;
mp_digit *dp;
#ifdef WOLFSSL_ASYNC_CRYPT
@@ -342,6 +342,11 @@ int mp_radix_size (mp_int * a, int radix, int *size);
int mp_cnt_lsb(mp_int *a);
int mp_mod_d(mp_int* a, mp_digit b, mp_digit* c);
+
+/* wolf big int and common functions */
+#include
+
+
#ifdef __cplusplus
}
#endif
diff --git a/wolfssl/wolfcrypt/tfm.h b/wolfssl/wolfcrypt/tfm.h
index c7cf9fa06..688c07cc2 100644
--- a/wolfssl/wolfcrypt/tfm.h
+++ b/wolfssl/wolfcrypt/tfm.h
@@ -282,12 +282,10 @@
#define FP_NO 0 /* no response */
/* a FP type */
-typedef struct {
+typedef struct fp_int {
int used,
sign;
-#ifdef ALT_ECC_SIZE
int size;
-#endif
fp_digit dp[FP_SIZE];
#ifdef WOLFSSL_ASYNC_CRYPT
byte *dpraw; /* Used for hardware crypto */
@@ -370,15 +368,9 @@ typedef struct {
/*const char *fp_ident(void);*/
/* initialize [or zero] an fp int */
-#ifdef ALT_ECC_SIZE
- void fp_init(fp_int *a);
- void fp_zero(fp_int *a);
- void fp_clear(fp_int *a); /* uses ForceZero to clear sensitive memory */
-#else
- #define fp_init(a) (void)XMEMSET((a), 0, sizeof(fp_int))
- #define fp_zero(a) fp_init(a)
- #define fp_clear(a) ForceZero((a), sizeof(fp_int));
-#endif
+void fp_init(fp_int *a);
+void fp_zero(fp_int *a);
+void fp_clear(fp_int *a); /* uses ForceZero to clear sensitive memory */
/* zero/even/odd ? */
#define fp_iszero(a) (((a)->used == 0) ? FP_YES : FP_NO)
@@ -397,13 +389,8 @@ int fp_is_bit_set(fp_int *a, fp_digit b);
int fp_set_bit (fp_int * a, fp_digit b);
/* copy from a to b */
-#ifndef ALT_ECC_SIZE
- #define fp_copy(a, b) (void)(((a) != (b)) ? ((void)XMEMCPY((b), (a), sizeof(fp_int))) : (void)0)
- #define fp_init_copy(a, b) fp_copy(b, a)
-#else
- void fp_copy(fp_int *a, fp_int *b);
- void fp_init_copy(fp_int *a, fp_int *b);
-#endif
+void fp_copy(fp_int *a, fp_int *b);
+void fp_init_copy(fp_int *a, fp_int *b);
/* clamp digits */
#define fp_clamp(a) { while ((a)->used && (a)->dp[(a)->used-1] == 0) --((a)->used); (a)->sign = (a)->used ? (a)->sign : FP_ZPOS; }
@@ -703,6 +690,12 @@ WOLFSSL_API word32 CheckRunTimeFastMath(void);
/* If user uses RSA, DH, DSA, or ECC math lib directly then fast math FP_SIZE
must match, return 1 if a match otherwise 0 */
#define CheckFastMathSettings() (FP_SIZE == CheckRunTimeFastMath())
+
+
+/* wolf big int and common functions */
+#include
+
+
#ifdef __cplusplus
}
#endif
diff --git a/wolfssl/wolfcrypt/wolfmath.h b/wolfssl/wolfcrypt/wolfmath.h
new file mode 100644
index 000000000..e6a348653
--- /dev/null
+++ b/wolfssl/wolfcrypt/wolfmath.h
@@ -0,0 +1,33 @@
+/* wolfmath.h
+ *
+ * Copyright (C) 2006-2016 wolfSSL Inc.
+ *
+ * This file is part of wolfSSL.
+ *
+ * wolfSSL is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * wolfSSL is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
+ */
+
+#ifndef __WOLFMATH_H__
+#define __WOLFMATH_H__
+
+
+/* common math functions */
+WOLFSSL_LOCAL int get_digit_count(mp_int* a);
+WOLFSSL_LOCAL mp_digit get_digit(mp_int* a, int n);
+WOLFSSL_LOCAL int get_rand_digit(WC_RNG* rng, mp_digit* d);
+WOLFSSL_LOCAL int mp_rand(mp_int* a, int digits, WC_RNG* rng);
+
+
+#endif /* __WOLFMATH_H__ */