Combine generic math functions into new wolfmath.c/.h. Cleanup of the !ALT_ECC_SIZE code so fp_int always has size. This is in prep for async changes for new WC_BIGINT type for hardware crypto.

This commit is contained in:
David Garske 2016-12-21 13:39:33 -08:00
parent 1a5c5d0011
commit d73338851d
15 changed files with 214 additions and 149 deletions

View File

@ -2040,6 +2040,12 @@
<file>
<name>$PROJ_DIR$\..\..\..\..\wolfcrypt\src\wc_port.c</name>
</file>
<file>
<name>$PROJ_DIR$\..\..\..\..\wolfcrypt\src\wolfmath.c</name>
</file>
<file>
<name>$PROJ_DIR$\..\..\..\..\wolfcrypt\src\wolfevent.c</name>
</file>
</group>
<group>
<name>wolfSSL</name>

View File

@ -102,6 +102,8 @@
<file file_name="../../wolfcrypt/src/tfm.c" />
<file file_name="../../wolfcrypt/src/wc_encrypt.c" />
<file file_name="../../wolfcrypt/src/wc_port.c" />
<file file_name="../../wolfcrypt/src/wolfmath.c" />
<file file_name="../../wolfcrypt/src/wolfevent.c" />
</folder>
<folder Name="test">
<file file_name="../../wolfcrypt/test/include.am" />

View File

@ -300,6 +300,7 @@
<ClCompile Include="..\..\src\ssl.c" />
<ClCompile Include="..\..\src\tls.c" />
<ClCompile Include="..\..\wolfcrypt\src\wc_encrypt.c" />
<ClCompile Include="..\..\wolfcrypt\src\wolfmath.c" />
<ClCompile Include="..\..\wolfcrypt\src\wolfevent.c" />
</ItemGroup>
<ItemGroup>
@ -324,4 +325,4 @@
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>
</Project>

View File

@ -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

View File

@ -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

View File

@ -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)
{

View File

@ -49,6 +49,7 @@
#include <wolfssl/wolfcrypt/random.h>
#include <wolfssl/wolfcrypt/tfm.h>
#include <wolfcrypt/src/asm.c> /* will define asm MACROS or C ones */
#include <wolfssl/wolfcrypt/wolfmath.h> /* common functions */
#if defined(FREESCALE_LTC_TFM)
#include <wolfssl/wolfcrypt/port/nxp/ksdk_port.h>
@ -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);

104
wolfcrypt/src/wolfmath.c Normal file
View File

@ -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 <config.h>
#endif
/* in case user set USE_FAST_MATH there */
#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>
#include <wolfssl/wolfcrypt/logging.h>
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;
}

View File

@ -274,6 +274,10 @@
RelativePath=".\wolfcrypt\src\wc_port.c"
>
</File>
<File
RelativePath=".\wolfcrypt\src\wolfmath.c"
>
</File>
<File
RelativePath=".\wolfcrypt\src\pwdbased.c"
>

View File

@ -271,6 +271,10 @@
RelativePath=".\wolfcrypt\src\wc_port.c"
>
</File>
<File
RelativePath=".\wolfcrypt\src\wolfmath.c"
>
</File>
<File
RelativePath=".\wolfcrypt\src\pwdbased.c"
>

View File

@ -318,6 +318,7 @@
<ClCompile Include="wolfcrypt\src\signature.c" />
<ClCompile Include="wolfcrypt\src\wc_encrypt.c" />
<ClCompile Include="wolfcrypt\src\wc_port.c" />
<ClCompile Include="wolfcrypt\src\wolfmath.c" />
<ClCompile Include="wolfcrypt\src\wolfevent.c" />
</ItemGroup>
<ItemGroup>

View File

@ -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 \

View File

@ -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 <wolfssl/wolfcrypt/wolfmath.h>
#ifdef __cplusplus
}
#endif

View File

@ -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 <wolfssl/wolfcrypt/wolfmath.h>
#ifdef __cplusplus
}
#endif

View File

@ -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__ */