forked from wolfSSL/wolfssl
Merge pull request #679 from dgarske/wolfmath
Combine generic math functions into new wolfmath.c/.h
This commit is contained in:
@ -2040,6 +2040,12 @@
|
|||||||
<file>
|
<file>
|
||||||
<name>$PROJ_DIR$\..\..\..\..\wolfcrypt\src\wc_port.c</name>
|
<name>$PROJ_DIR$\..\..\..\..\wolfcrypt\src\wc_port.c</name>
|
||||||
</file>
|
</file>
|
||||||
|
<file>
|
||||||
|
<name>$PROJ_DIR$\..\..\..\..\wolfcrypt\src\wolfmath.c</name>
|
||||||
|
</file>
|
||||||
|
<file>
|
||||||
|
<name>$PROJ_DIR$\..\..\..\..\wolfcrypt\src\wolfevent.c</name>
|
||||||
|
</file>
|
||||||
</group>
|
</group>
|
||||||
<group>
|
<group>
|
||||||
<name>wolfSSL</name>
|
<name>wolfSSL</name>
|
||||||
|
@ -102,6 +102,8 @@
|
|||||||
<file file_name="../../wolfcrypt/src/tfm.c" />
|
<file file_name="../../wolfcrypt/src/tfm.c" />
|
||||||
<file file_name="../../wolfcrypt/src/wc_encrypt.c" />
|
<file file_name="../../wolfcrypt/src/wc_encrypt.c" />
|
||||||
<file file_name="../../wolfcrypt/src/wc_port.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>
|
||||||
<folder Name="test">
|
<folder Name="test">
|
||||||
<file file_name="../../wolfcrypt/test/include.am" />
|
<file file_name="../../wolfcrypt/test/include.am" />
|
||||||
|
@ -104,6 +104,8 @@
|
|||||||
<file file_name="../../wolfcrypt/src/tfm.c" />
|
<file file_name="../../wolfcrypt/src/tfm.c" />
|
||||||
<file file_name="../../wolfcrypt/src/wc_encrypt.c" />
|
<file file_name="../../wolfcrypt/src/wc_encrypt.c" />
|
||||||
<file file_name="../../wolfcrypt/src/wc_port.c" />
|
<file file_name="../../wolfcrypt/src/wc_port.c" />
|
||||||
|
<file file_name="../../wolfcrypt/src/wolfmath.c" />
|
||||||
|
<file file_name="../../wolfcrypt/src/wolfevent.c" />
|
||||||
<folder Name="port">
|
<folder Name="port">
|
||||||
<folder Name="nxp">
|
<folder Name="nxp">
|
||||||
<file file_name="../../wolfcrypt/src/port/nxp/ksdk_port.c" />
|
<file file_name="../../wolfcrypt/src/port/nxp/ksdk_port.c" />
|
||||||
|
@ -300,6 +300,7 @@
|
|||||||
<ClCompile Include="..\..\src\ssl.c" />
|
<ClCompile Include="..\..\src\ssl.c" />
|
||||||
<ClCompile Include="..\..\src\tls.c" />
|
<ClCompile Include="..\..\src\tls.c" />
|
||||||
<ClCompile Include="..\..\wolfcrypt\src\wc_encrypt.c" />
|
<ClCompile Include="..\..\wolfcrypt\src\wc_encrypt.c" />
|
||||||
|
<ClCompile Include="..\..\wolfcrypt\src\wolfmath.c" />
|
||||||
<ClCompile Include="..\..\wolfcrypt\src\wolfevent.c" />
|
<ClCompile Include="..\..\wolfcrypt\src\wolfevent.c" />
|
||||||
<ClCompile Include="..\..\wolfcrypt\src\pkcs12.c" />
|
<ClCompile Include="..\..\wolfcrypt\src\pkcs12.c" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
@ -325,4 +326,4 @@
|
|||||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||||
<ImportGroup Label="ExtensionTargets">
|
<ImportGroup Label="ExtensionTargets">
|
||||||
</ImportGroup>
|
</ImportGroup>
|
||||||
</Project>
|
</Project>
|
||||||
|
@ -120,7 +120,8 @@ src_libwolfssl_la_SOURCES += \
|
|||||||
wolfcrypt/src/wc_encrypt.c \
|
wolfcrypt/src/wc_encrypt.c \
|
||||||
wolfcrypt/src/wc_port.c \
|
wolfcrypt/src/wc_port.c \
|
||||||
wolfcrypt/src/error.c \
|
wolfcrypt/src/error.c \
|
||||||
wolfcrypt/src/signature.c
|
wolfcrypt/src/signature.c \
|
||||||
|
wolfcrypt/src/wolfmath.c
|
||||||
|
|
||||||
if BUILD_MEMORY
|
if BUILD_MEMORY
|
||||||
src_libwolfssl_la_SOURCES += wolfcrypt/src/memory.c
|
src_libwolfssl_la_SOURCES += wolfcrypt/src/memory.c
|
||||||
|
@ -982,24 +982,6 @@ static int wc_ecc_set_curve(ecc_key* key, int keysize, int curve_id)
|
|||||||
|
|
||||||
#ifndef WOLFSSL_ATECC508A
|
#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
|
Add two ECC points
|
||||||
P The point to add
|
P The point to add
|
||||||
|
@ -846,72 +846,6 @@ static int wc_RsaUnPad_ex(byte* pkcsBlock, word32 pkcsBlockLen, byte** out,
|
|||||||
return ret;
|
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,
|
static int wc_RsaFunctionSync(const byte* in, word32 inLen, byte* out,
|
||||||
word32* outLen, int type, RsaKey* key, WC_RNG* rng)
|
word32* outLen, int type, RsaKey* key, WC_RNG* rng)
|
||||||
{
|
{
|
||||||
|
@ -49,6 +49,7 @@
|
|||||||
#include <wolfssl/wolfcrypt/random.h>
|
#include <wolfssl/wolfcrypt/random.h>
|
||||||
#include <wolfssl/wolfcrypt/tfm.h>
|
#include <wolfssl/wolfcrypt/tfm.h>
|
||||||
#include <wolfcrypt/src/asm.c> /* will define asm MACROS or C ones */
|
#include <wolfcrypt/src/asm.c> /* will define asm MACROS or C ones */
|
||||||
|
#include <wolfssl/wolfcrypt/wolfmath.h> /* common functions */
|
||||||
|
|
||||||
#if defined(FREESCALE_LTC_TFM)
|
#if defined(FREESCALE_LTC_TFM)
|
||||||
#include <wolfssl/wolfcrypt/port/nxp/ksdk_port.h>
|
#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_init(&t);
|
||||||
fp_mul(a, b, &t);
|
fp_mul(a, b, &t);
|
||||||
#ifdef ALT_ECC_SIZE
|
if (d->size < FP_SIZE) {
|
||||||
err = fp_mod(&t, c, &t);
|
err = fp_mod(&t, c, &t);
|
||||||
fp_copy(&t, d);
|
fp_copy(&t, d);
|
||||||
#else
|
} else {
|
||||||
err = fp_mod(&t, c, d);
|
err = fp_mod(&t, c, d);
|
||||||
#endif
|
}
|
||||||
|
|
||||||
return err;
|
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_init(&t);
|
||||||
fp_sub(a, b, &t);
|
fp_sub(a, b, &t);
|
||||||
#ifdef ALT_ECC_SIZE
|
if (d->size < FP_SIZE) {
|
||||||
err = fp_mod(&t, c, &t);
|
err = fp_mod(&t, c, &t);
|
||||||
fp_copy(&t, d);
|
fp_copy(&t, d);
|
||||||
#else
|
} else {
|
||||||
err = fp_mod(&t, c, d);
|
err = fp_mod(&t, c, d);
|
||||||
#endif
|
}
|
||||||
|
|
||||||
return err;
|
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_init(&t);
|
||||||
fp_add(a, b, &t);
|
fp_add(a, b, &t);
|
||||||
#ifdef ALT_ECC_SIZE
|
if (d->size < FP_SIZE) {
|
||||||
err = fp_mod(&t, c, &t);
|
err = fp_mod(&t, c, &t);
|
||||||
fp_copy(&t, d);
|
fp_copy(&t, d);
|
||||||
#else
|
} else {
|
||||||
err = fp_mod(&t, c, d);
|
err = fp_mod(&t, c, d);
|
||||||
#endif
|
}
|
||||||
|
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
@ -2167,12 +2168,12 @@ void fp_sub_d(fp_int *a, fp_digit b, fp_int *c)
|
|||||||
fp_int tmp;
|
fp_int tmp;
|
||||||
fp_init(&tmp);
|
fp_init(&tmp);
|
||||||
fp_set(&tmp, b);
|
fp_set(&tmp, b);
|
||||||
#ifdef ALT_ECC_SIZE
|
if (c->size < FP_SIZE) {
|
||||||
fp_sub(a, &tmp, &tmp);
|
fp_sub(a, &tmp, &tmp);
|
||||||
fp_copy(&tmp, c);
|
fp_copy(&tmp, c);
|
||||||
#else
|
} else {
|
||||||
fp_sub(a, &tmp, c);
|
fp_sub(a, &tmp, c);
|
||||||
#endif
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -2186,7 +2187,6 @@ int mp_init (mp_int * a)
|
|||||||
return MP_OKAY;
|
return MP_OKAY;
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef ALT_ECC_SIZE
|
|
||||||
void fp_init(fp_int *a)
|
void fp_init(fp_int *a)
|
||||||
{
|
{
|
||||||
a->size = FP_SIZE;
|
a->size = FP_SIZE;
|
||||||
@ -2206,7 +2206,6 @@ void fp_clear(fp_int *a)
|
|||||||
a->sign = FP_ZPOS;
|
a->sign = FP_ZPOS;
|
||||||
ForceZero(a->dp, a->size * sizeof(fp_digit));
|
ForceZero(a->dp, a->size * sizeof(fp_digit));
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
|
|
||||||
|
|
||||||
/* clear one (frees) */
|
/* clear one (frees) */
|
||||||
@ -2347,21 +2346,34 @@ int mp_div_2d(fp_int* a, int b, fp_int* c, fp_int* d)
|
|||||||
return MP_OKAY;
|
return MP_OKAY;
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef ALT_ECC_SIZE
|
|
||||||
void fp_copy(fp_int *a, fp_int *b)
|
void fp_copy(fp_int *a, fp_int *b)
|
||||||
{
|
{
|
||||||
if (a != b && b->size >= a->used) {
|
/* if source and destination are different */
|
||||||
int x, oldused;
|
if (a != b) {
|
||||||
oldused = b->used;
|
#ifdef ALT_ECC_SIZE
|
||||||
|
/* verify a will fit in b */
|
||||||
|
if (b->size >= a->used) {
|
||||||
|
int x, oldused;
|
||||||
|
oldused = b->used;
|
||||||
|
b->used = a->used;
|
||||||
|
b->sign = a->sign;
|
||||||
|
|
||||||
|
XMEMCPY(b->dp, a->dp, a->used * sizeof(fp_digit));
|
||||||
|
|
||||||
|
/* zero any excess digits on the destination that we didn't write to */
|
||||||
|
for (x = b->used; x < oldused; x++) {
|
||||||
|
b->dp[x] = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
/* TODO: Handle error case */
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
/* all dp's are same size, so do straight copy */
|
||||||
b->used = a->used;
|
b->used = a->used;
|
||||||
b->sign = a->sign;
|
b->sign = a->sign;
|
||||||
|
XMEMCPY(b->dp, a->dp, FP_SIZE * sizeof(fp_digit));
|
||||||
XMEMCPY(b->dp, a->dp, a->used * sizeof(fp_digit));
|
#endif
|
||||||
|
|
||||||
/* zero any excess digits on the destination that we didn't write to */
|
|
||||||
for (x = b->used; x < oldused; x++) {
|
|
||||||
b->dp[x] = 0;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -2372,7 +2384,6 @@ void fp_init_copy(fp_int *a, fp_int* b)
|
|||||||
fp_copy(b, a);
|
fp_copy(b, a);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
|
|
||||||
/* fast math wrappers */
|
/* fast math wrappers */
|
||||||
int mp_copy(fp_int* a, fp_int* b)
|
int mp_copy(fp_int* a, fp_int* b)
|
||||||
@ -2432,12 +2443,14 @@ int fp_sqrmod(fp_int *a, fp_int *b, fp_int *c)
|
|||||||
|
|
||||||
fp_init(&t);
|
fp_init(&t);
|
||||||
fp_sqr(a, &t);
|
fp_sqr(a, &t);
|
||||||
#ifdef ALT_ECC_SIZE
|
|
||||||
err = fp_mod(&t, b, &t);
|
if (c->size < FP_SIZE) {
|
||||||
fp_copy(&t, c);
|
err = fp_mod(&t, b, &t);
|
||||||
#else
|
fp_copy(&t, c);
|
||||||
err = fp_mod(&t, b, c);
|
}
|
||||||
#endif
|
else {
|
||||||
|
err = fp_mod(&t, b, c);
|
||||||
|
}
|
||||||
|
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
@ -2850,7 +2863,7 @@ int fp_randprime(fp_int* N, int len, WC_RNG* rng, void* heap)
|
|||||||
|
|
||||||
XMEMSET(buf, 0, len);
|
XMEMSET(buf, 0, len);
|
||||||
XFREE(buf, heap, DYNAMIC_TYPE_TMP_BUFFER);
|
XFREE(buf, heap, DYNAMIC_TYPE_TMP_BUFFER);
|
||||||
|
|
||||||
return FP_OKAY;
|
return FP_OKAY;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -3172,14 +3185,9 @@ int mp_toradix (mp_int *a, char *str, int radix)
|
|||||||
void mp_dump(const char* desc, mp_int* a, byte verbose)
|
void mp_dump(const char* desc, mp_int* a, byte verbose)
|
||||||
{
|
{
|
||||||
char buffer[FP_SIZE * sizeof(fp_digit) * 2];
|
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",
|
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);
|
mp_toradix(a, buffer, 16);
|
||||||
printf(" %s\n ", buffer);
|
printf(" %s\n ", buffer);
|
||||||
|
104
wolfcrypt/src/wolfmath.c
Normal file
104
wolfcrypt/src/wolfmath.c
Normal 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;
|
||||||
|
}
|
@ -274,6 +274,10 @@
|
|||||||
RelativePath=".\wolfcrypt\src\wc_port.c"
|
RelativePath=".\wolfcrypt\src\wc_port.c"
|
||||||
>
|
>
|
||||||
</File>
|
</File>
|
||||||
|
<File
|
||||||
|
RelativePath=".\wolfcrypt\src\wolfmath.c"
|
||||||
|
>
|
||||||
|
</File>
|
||||||
<File
|
<File
|
||||||
RelativePath=".\wolfcrypt\src\pwdbased.c"
|
RelativePath=".\wolfcrypt\src\pwdbased.c"
|
||||||
>
|
>
|
||||||
|
@ -271,6 +271,10 @@
|
|||||||
RelativePath=".\wolfcrypt\src\wc_port.c"
|
RelativePath=".\wolfcrypt\src\wc_port.c"
|
||||||
>
|
>
|
||||||
</File>
|
</File>
|
||||||
|
<File
|
||||||
|
RelativePath=".\wolfcrypt\src\wolfmath.c"
|
||||||
|
>
|
||||||
|
</File>
|
||||||
<File
|
<File
|
||||||
RelativePath=".\wolfcrypt\src\pwdbased.c"
|
RelativePath=".\wolfcrypt\src\pwdbased.c"
|
||||||
>
|
>
|
||||||
|
@ -318,6 +318,7 @@
|
|||||||
<ClCompile Include="wolfcrypt\src\signature.c" />
|
<ClCompile Include="wolfcrypt\src\signature.c" />
|
||||||
<ClCompile Include="wolfcrypt\src\wc_encrypt.c" />
|
<ClCompile Include="wolfcrypt\src\wc_encrypt.c" />
|
||||||
<ClCompile Include="wolfcrypt\src\wc_port.c" />
|
<ClCompile Include="wolfcrypt\src\wc_port.c" />
|
||||||
|
<ClCompile Include="wolfcrypt\src\wolfmath.c" />
|
||||||
<ClCompile Include="wolfcrypt\src\wolfevent.c" />
|
<ClCompile Include="wolfcrypt\src\wolfevent.c" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
@ -58,7 +58,8 @@ nobase_include_HEADERS+= \
|
|||||||
wolfssl/wolfcrypt/mpi_superclass.h \
|
wolfssl/wolfcrypt/mpi_superclass.h \
|
||||||
wolfssl/wolfcrypt/mem_track.h \
|
wolfssl/wolfcrypt/mem_track.h \
|
||||||
wolfssl/wolfcrypt/wolfevent.h \
|
wolfssl/wolfcrypt/wolfevent.h \
|
||||||
wolfssl/wolfcrypt/pkcs12.h
|
wolfssl/wolfcrypt/pkcs12.h \
|
||||||
|
wolfssl/wolfcrypt/wolfmath.h
|
||||||
|
|
||||||
noinst_HEADERS+= \
|
noinst_HEADERS+= \
|
||||||
wolfssl/wolfcrypt/port/pic32/pic32mz-crypt.h \
|
wolfssl/wolfcrypt/port/pic32/pic32mz-crypt.h \
|
||||||
|
@ -64,7 +64,7 @@ extern "C" {
|
|||||||
/* C on the other hand doesn't care */
|
/* C on the other hand doesn't care */
|
||||||
#define OPT_CAST(x)
|
#define OPT_CAST(x)
|
||||||
|
|
||||||
#endif
|
#endif /* __cplusplus */
|
||||||
|
|
||||||
|
|
||||||
/* detect 64-bit mode if possible */
|
/* 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))
|
#define MP_WARRAY (1 << (sizeof(mp_word) * CHAR_BIT - 2 * DIGIT_BIT + 1))
|
||||||
|
|
||||||
/* the infamous mp_int structure */
|
/* the infamous mp_int structure */
|
||||||
typedef struct {
|
typedef struct mp_int {
|
||||||
int used, alloc, sign;
|
int used, alloc, sign;
|
||||||
mp_digit *dp;
|
mp_digit *dp;
|
||||||
#ifdef WOLFSSL_ASYNC_CRYPT
|
#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_cnt_lsb(mp_int *a);
|
||||||
int mp_mod_d(mp_int* a, mp_digit b, mp_digit* c);
|
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
|
||||||
|
@ -282,12 +282,10 @@
|
|||||||
#define FP_NO 0 /* no response */
|
#define FP_NO 0 /* no response */
|
||||||
|
|
||||||
/* a FP type */
|
/* a FP type */
|
||||||
typedef struct {
|
typedef struct fp_int {
|
||||||
int used,
|
int used;
|
||||||
sign;
|
int sign;
|
||||||
#ifdef ALT_ECC_SIZE
|
|
||||||
int size;
|
int size;
|
||||||
#endif
|
|
||||||
fp_digit dp[FP_SIZE];
|
fp_digit dp[FP_SIZE];
|
||||||
#ifdef WOLFSSL_ASYNC_CRYPT
|
#ifdef WOLFSSL_ASYNC_CRYPT
|
||||||
byte *dpraw; /* Used for hardware crypto */
|
byte *dpraw; /* Used for hardware crypto */
|
||||||
@ -370,15 +368,9 @@ typedef struct {
|
|||||||
/*const char *fp_ident(void);*/
|
/*const char *fp_ident(void);*/
|
||||||
|
|
||||||
/* initialize [or zero] an fp int */
|
/* initialize [or zero] an fp int */
|
||||||
#ifdef ALT_ECC_SIZE
|
void fp_init(fp_int *a);
|
||||||
void fp_init(fp_int *a);
|
void fp_zero(fp_int *a);
|
||||||
void fp_zero(fp_int *a);
|
void fp_clear(fp_int *a); /* uses ForceZero to clear sensitive memory */
|
||||||
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
|
|
||||||
|
|
||||||
/* zero/even/odd ? */
|
/* zero/even/odd ? */
|
||||||
#define fp_iszero(a) (((a)->used == 0) ? FP_YES : FP_NO)
|
#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);
|
int fp_set_bit (fp_int * a, fp_digit b);
|
||||||
|
|
||||||
/* copy from a to b */
|
/* copy from a to b */
|
||||||
#ifndef ALT_ECC_SIZE
|
void fp_copy(fp_int *a, fp_int *b);
|
||||||
#define fp_copy(a, b) (void)(((a) != (b)) ? ((void)XMEMCPY((b), (a), sizeof(fp_int))) : (void)0)
|
void fp_init_copy(fp_int *a, fp_int *b);
|
||||||
#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
|
|
||||||
|
|
||||||
/* clamp digits */
|
/* 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; }
|
#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
|
/* 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 */
|
must match, return 1 if a match otherwise 0 */
|
||||||
#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
|
||||||
|
33
wolfssl/wolfcrypt/wolfmath.h
Normal file
33
wolfssl/wolfcrypt/wolfmath.h
Normal 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__ */
|
Reference in New Issue
Block a user