Cleanup around include of wolfmath.h and shared math macros. Fix for SP math case with WOLFSSL_BIGNUM.

This commit is contained in:
David Garske
2019-06-21 16:07:17 -07:00
parent a9fff57063
commit dcdd6d6d6f
4 changed files with 105 additions and 119 deletions

View File

@@ -47,23 +47,6 @@
#include <wolfssl/wolfcrypt/mpi_class.h> #include <wolfssl/wolfcrypt/mpi_class.h>
/* wolf big int and common functions */
#include <wolfssl/wolfcrypt/wolfmath.h>
#ifdef WOLFSSL_PUBLIC_MP
#define MP_API WOLFSSL_API
#else
#define MP_API
#endif
#ifndef MIN
#define MIN(x,y) ((x)<(y)?(x):(y))
#endif
#ifndef MAX
#define MAX(x,y) ((x)>(y)?(x):(y))
#endif
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
@@ -204,7 +187,13 @@ typedef int mp_err;
#define MP_WARRAY ((mp_word)1 << (sizeof(mp_word) * CHAR_BIT - 2 * DIGIT_BIT + 1)) #define MP_WARRAY ((mp_word)1 << (sizeof(mp_word) * CHAR_BIT - 2 * DIGIT_BIT + 1))
#ifdef HAVE_WOLF_BIGINT #ifdef HAVE_WOLF_BIGINT
struct WC_BIGINT; /* raw big integer */
typedef struct WC_BIGINT {
byte* buf;
word32 len;
void* heap;
} WC_BIGINT;
#define WOLF_BIGINT_DEFINED
#endif #endif
/* the mp_int structure */ /* the mp_int structure */
@@ -216,7 +205,10 @@ typedef struct mp_int {
struct WC_BIGINT raw; /* unsigned binary (big endian) */ struct WC_BIGINT raw; /* unsigned binary (big endian) */
#endif #endif
} mp_int; } mp_int;
#define MP_INT_DEFINED
/* wolf big int and common functions */
#include <wolfssl/wolfcrypt/wolfmath.h>
/* callback for mp_prime_random, should fill dst with random bytes and return /* callback for mp_prime_random, should fill dst with random bytes and return
how many read [up to len] */ how many read [up to len] */
@@ -399,10 +391,6 @@ MP_API int mp_cnt_lsb(mp_int *a);
MP_API int mp_mod_d(mp_int* a, mp_digit b, mp_digit* c); MP_API 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 #ifdef __cplusplus
} }
#endif #endif

View File

@@ -85,20 +85,6 @@
#ifdef WOLFSSL_SP_MATH #ifdef WOLFSSL_SP_MATH
#include <wolfssl/wolfcrypt/random.h> #include <wolfssl/wolfcrypt/random.h>
#ifndef MIN
#define MIN(x,y) ((x)<(y)?(x):(y))
#endif
#ifndef MAX
#define MAX(x,y) ((x)>(y)?(x):(y))
#endif
#ifdef WOLFSSL_PUBLIC_MP
#define MP_API WOLFSSL_API
#else
#define MP_API WOLFSSL_LOCAL
#endif
#if !defined(WOLFSSL_HAVE_SP_RSA) && !defined(WOLFSSL_HAVE_SP_DH) #if !defined(WOLFSSL_HAVE_SP_RSA) && !defined(WOLFSSL_HAVE_SP_DH)
#if !defined(NO_PWDBASED) && defined(WOLFSSL_SHA512) #if !defined(NO_PWDBASED) && defined(WOLFSSL_SHA512)
#define SP_INT_DIGITS ((512 + SP_WORD_SIZE) / SP_WORD_SIZE) #define SP_INT_DIGITS ((512 + SP_WORD_SIZE) / SP_WORD_SIZE)
@@ -113,12 +99,30 @@
#define sp_isodd(a) (a->used != 0 && (a->dp[0] & 1)) #define sp_isodd(a) (a->used != 0 && (a->dp[0] & 1))
#ifdef HAVE_WOLF_BIGINT
/* raw big integer */
typedef struct WC_BIGINT {
byte* buf;
word32 len;
void* heap;
} WC_BIGINT;
#define WOLF_BIGINT_DEFINED
#endif
typedef struct sp_int { typedef struct sp_int {
int used; int used;
int size; int size;
sp_int_digit dp[SP_INT_DIGITS]; sp_int_digit dp[SP_INT_DIGITS];
#ifdef HAVE_WOLF_BIGINT
struct WC_BIGINT raw; /* unsigned binary (big endian) */
#endif
} sp_int; } sp_int;
typedef sp_int mp_int;
typedef sp_digit mp_digit;
#include <wolfssl/wolfcrypt/wolfmath.h>
MP_API int sp_init(sp_int* a); MP_API int sp_init(sp_int* a);
MP_API int sp_init_multi(sp_int* a, sp_int* b, sp_int* c, sp_int* d, MP_API int sp_init_multi(sp_int* a, sp_int* b, sp_int* c, sp_int* d,
@@ -148,8 +152,6 @@ MP_API int sp_add(sp_int* a, sp_int* b, sp_int* r);
MP_API int sp_set_int(sp_int* a, unsigned long b); MP_API int sp_set_int(sp_int* a, unsigned long b);
MP_API int sp_tohex(sp_int* a, char* str); MP_API int sp_tohex(sp_int* a, char* str);
typedef sp_int mp_int;
typedef sp_digit mp_digit;
#define MP_OKAY 0 #define MP_OKAY 0
#define MP_NO 0 #define MP_NO 0
@@ -198,9 +200,6 @@ typedef sp_digit mp_digit;
#define mp_set_int sp_set_int #define mp_set_int sp_set_int
#define mp_tohex sp_tohex #define mp_tohex sp_tohex
#define MP_INT_DEFINED
#include <wolfssl/wolfcrypt/wolfmath.h>
#endif #endif
#endif /* WOLF_CRYPT_SP_H */ #endif /* WOLF_CRYPT_SP_H */

View File

@@ -46,27 +46,10 @@
#include <wolfssl/wolfcrypt/random.h> #include <wolfssl/wolfcrypt/random.h>
/* wolf big int and common functions */
#include <wolfssl/wolfcrypt/wolfmath.h>
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
#ifdef WOLFSSL_PUBLIC_MP
#define MP_API WOLFSSL_API
#else
#define MP_API
#endif
#ifndef MIN
#define MIN(x,y) ((x)<(y)?(x):(y))
#endif
#ifndef MAX
#define MAX(x,y) ((x)>(y)?(x):(y))
#endif
#ifdef WOLFSSL_NO_ASM #ifdef WOLFSSL_NO_ASM
#undef TFM_NO_ASM #undef TFM_NO_ASM
#define TFM_NO_ASM #define TFM_NO_ASM
@@ -256,6 +239,7 @@
#endif /* WOLFSSL_BIGINT_TYPES */ #endif /* WOLFSSL_BIGINT_TYPES */
/* # of digits this is */ /* # of digits this is */
#define DIGIT_BIT ((CHAR_BIT) * SIZEOF_FP_DIGIT) #define DIGIT_BIT ((CHAR_BIT) * SIZEOF_FP_DIGIT)
@@ -310,7 +294,13 @@
#define FP_NO 0 /* no response */ #define FP_NO 0 /* no response */
#ifdef HAVE_WOLF_BIGINT #ifdef HAVE_WOLF_BIGINT
struct WC_BIGINT; /* raw big integer */
typedef struct WC_BIGINT {
byte* buf;
word32 len;
void* heap;
} WC_BIGINT;
#define WOLF_BIGINT_DEFINED
#endif #endif
/* a FP type */ /* a FP type */
@@ -327,6 +317,16 @@ typedef struct fp_int {
#endif #endif
} fp_int; } fp_int;
/* Types */
typedef fp_digit mp_digit;
typedef fp_word mp_word;
typedef fp_int mp_int;
/* wolf big int and common functions */
#include <wolfssl/wolfcrypt/wolfmath.h>
/* externally define this symbol to ignore the default settings, useful for changing the build from the make process */ /* externally define this symbol to ignore the default settings, useful for changing the build from the make process */
#ifndef TFM_ALREADY_SET #ifndef TFM_ALREADY_SET
@@ -679,12 +679,6 @@ int fp_sqr_comba64(fp_int *a, fp_int *b);
* Used by wolfSSL * Used by wolfSSL
*/ */
/* Types */
typedef fp_digit mp_digit;
typedef fp_word mp_word;
typedef fp_int mp_int;
#define MP_INT_DEFINED
/* Constants */ /* Constants */
#define MP_LT FP_LT /* less than */ #define MP_LT FP_LT /* less than */
#define MP_EQ FP_EQ /* equal to */ #define MP_EQ FP_EQ /* equal to */
@@ -816,10 +810,6 @@ WOLFSSL_API word32 CheckRunTimeFastMath(void);
#define CheckFastMathSettings() (FP_SIZE == CheckRunTimeFastMath()) #define CheckFastMathSettings() (FP_SIZE == CheckRunTimeFastMath())
/* wolf big int and common functions */
#include <wolfssl/wolfcrypt/wolfmath.h>
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@@ -19,28 +19,26 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
*/ */
#ifndef __WOLFMATH_H__
#define __WOLFMATH_H__
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
/* This section is included prior to wolfmath.h below */ #ifdef WOLFSSL_PUBLIC_MP
#if defined(HAVE_WOLF_BIGINT) && !defined(WOLF_BIGINT_DEFINED) #define MP_API WOLFSSL_API
/* raw big integer */ #else
typedef struct WC_BIGINT { #define MP_API WOLFSSL_LOCAL
byte* buf;
word32 len;
void* heap;
} WC_BIGINT;
#define WOLF_BIGINT_DEFINED
#endif #endif
/* only define functions if mp_int has been declared */ #ifndef MIN
/* The MP_INT_DEFINED is defined in tfm.h and integer.h after mp_int */ #define MIN(x,y) ((x)<(y)?(x):(y))
#ifdef MP_INT_DEFINED #endif
#ifndef __WOLFMATH_H__ #ifndef MAX
#define __WOLFMATH_H__ #define MAX(x,y) ((x)>(y)?(x):(y))
#endif
/* timing resistance array */ /* timing resistance array */
#if !defined(WC_NO_CACHE_RESISTANT) && \ #if !defined(WC_NO_CACHE_RESISTANT) && \
@@ -50,11 +48,13 @@
extern const wolfssl_word wc_off_on_addr[2]; extern const wolfssl_word wc_off_on_addr[2];
#endif #endif
/* common math functions */ /* common math functions */
int get_digit_count(mp_int* a); MP_API int get_digit_count(mp_int* a);
mp_digit get_digit(mp_int* a, int n); MP_API mp_digit get_digit(mp_int* a, int n);
int get_rand_digit(WC_RNG* rng, mp_digit* d); MP_API int get_rand_digit(WC_RNG* rng, mp_digit* d);
int mp_rand(mp_int* a, int digits, WC_RNG* rng);
WOLFSSL_API int mp_rand(mp_int* a, int digits, WC_RNG* rng);
enum { enum {
/* format type */ /* format type */
@@ -66,22 +66,31 @@
word32 keySz, int encType); word32 keySz, int encType);
#ifdef HAVE_WOLF_BIGINT #ifdef HAVE_WOLF_BIGINT
void wc_bigint_init(WC_BIGINT* a); #if !defined(WOLF_BIGINT_DEFINED)
int wc_bigint_alloc(WC_BIGINT* a, word32 sz); /* raw big integer */
int wc_bigint_from_unsigned_bin(WC_BIGINT* a, const byte* in, word32 inlen); typedef struct WC_BIGINT {
int wc_bigint_to_unsigned_bin(WC_BIGINT* a, byte* out, word32* outlen); byte* buf;
void wc_bigint_zero(WC_BIGINT* a); word32 len;
void wc_bigint_free(WC_BIGINT* a); void* heap;
} WC_BIGINT;
#define WOLF_BIGINT_DEFINED
#endif
int wc_mp_to_bigint(mp_int* src, WC_BIGINT* dst); WOLFSSL_LOCAL void wc_bigint_init(WC_BIGINT* a);
int wc_mp_to_bigint_sz(mp_int* src, WC_BIGINT* dst, word32 sz); WOLFSSL_LOCAL int wc_bigint_alloc(WC_BIGINT* a, word32 sz);
int wc_bigint_to_mp(WC_BIGINT* src, mp_int* dst); WOLFSSL_LOCAL int wc_bigint_from_unsigned_bin(WC_BIGINT* a, const byte* in, word32 inlen);
WOLFSSL_LOCAL int wc_bigint_to_unsigned_bin(WC_BIGINT* a, byte* out, word32* outlen);
WOLFSSL_LOCAL void wc_bigint_zero(WC_BIGINT* a);
WOLFSSL_LOCAL void wc_bigint_free(WC_BIGINT* a);
WOLFSSL_LOCAL int wc_mp_to_bigint(mp_int* src, WC_BIGINT* dst);
WOLFSSL_LOCAL int wc_mp_to_bigint_sz(mp_int* src, WC_BIGINT* dst, word32 sz);
WOLFSSL_LOCAL int wc_bigint_to_mp(WC_BIGINT* src, mp_int* dst);
#endif /* HAVE_WOLF_BIGINT */ #endif /* HAVE_WOLF_BIGINT */
#endif /* __WOLFMATH_H__ */
#endif /* MP_INT_DEFINED */
#ifdef __cplusplus #ifdef __cplusplus
} /* extern "C" */ } /* extern "C" */
#endif #endif
#endif /* __WOLFMATH_H__ */