Merge pull request #2373 from dgarske/mpint

Improvements to atoi, mp_int allocations and STSAFE-A100 error handling
This commit is contained in:
toddouska
2019-08-02 15:43:20 -07:00
committed by GitHub
7 changed files with 160 additions and 103 deletions

View File

@@ -39,7 +39,7 @@
#include <wolfssl/wolfio.h>
#if defined(HAVE_HTTP_CLIENT)
#include <stdlib.h> /* atoi(), strtol() */
#include <stdlib.h> /* strtol() */
#endif
/*
@@ -1101,7 +1101,7 @@ int wolfIO_HttpProcessResponse(int sfd, const char** appStrList,
else if (XSTRNCASECMP(start, "Content-Length:", 15) == 0) {
start += 15;
while (*start == ' ' && *start != '\0') start++;
chunkSz = atoi(start);
chunkSz = XATOI(start);
state = (state == phr_http_start) ? phr_have_length : phr_wait_end;
}
else if (XSTRNCASECMP(start, "Transfer-Encoding:", 18) == 0) {

View File

@@ -5793,7 +5793,7 @@ int main(int argc, char** argv)
while (argc > 1) {
if (string_matches(argv[1], "-?")) {
if(--argc>1){
lng_index = atoi((++argv)[1]);
lng_index = XATOI((++argv)[1]);
if(lng_index<0||lng_index>1) {
lng_index = 0;
}
@@ -5812,7 +5812,7 @@ int main(int argc, char** argv)
argc--;
argv++;
if(argc>1) {
lng_index = atoi(argv[1]);
lng_index = XATOI(argv[1]);
if(lng_index<0||lng_index>1){
printf("invalid number(%d) is specified. [<num> :0-1]\n",lng_index);
lng_index = 0;
@@ -5850,7 +5850,7 @@ int main(int argc, char** argv)
argc--;
argv++;
if (argc > 1) {
g_threadCount = atoi(argv[1]);
g_threadCount = XATOI(argv[1]);
if (g_threadCount < 1 || lng_index > 128){
printf("invalid number(%d) is specified. [<num> :1-128]\n",
g_threadCount);
@@ -5915,7 +5915,7 @@ int main(int argc, char** argv)
}
else {
/* parse for block size */
benchmark_configure(atoi(argv[1]));
benchmark_configure(XATOI(argv[1]));
}
argc--;
argv++;

View File

@@ -10973,7 +10973,7 @@ int EncodePolicyOID(byte *out, word32 *outSz, const char *in, void* heap)
token = XSTRTOK(str, ".", &ptr);
while (token != NULL)
{
val = (word32)atoi(token);
val = (word32)XATOI(token);
if (nb_val == 0) {
if (val > 2) {

View File

@@ -1902,7 +1902,7 @@ int mp_exptmod_fast (mp_int * G, mp_int * X, mp_int * P, mp_int * Y,
#ifdef WOLFSSL_SMALL_STACK
M = (mp_int*) XMALLOC(sizeof(mp_int) * TAB_SIZE, NULL,
DYNAMIC_TYPE_TMP_BUFFER);
DYNAMIC_TYPE_BIGINT);
if (M == NULL)
return MP_MEM;
#endif
@@ -1935,7 +1935,7 @@ int mp_exptmod_fast (mp_int * G, mp_int * X, mp_int * P, mp_int * Y,
/* init first cell */
if ((err = mp_init_size(&M[1], P->alloc)) != MP_OKAY) {
#ifdef WOLFSSL_SMALL_STACK
XFREE(M, NULL, DYNAMIC_TYPE_TMP_BUFFER);
XFREE(M, NULL, DYNAMIC_TYPE_BIGINT);
#endif
return err;
@@ -1950,7 +1950,7 @@ int mp_exptmod_fast (mp_int * G, mp_int * X, mp_int * P, mp_int * Y,
mp_clear(&M[1]);
#ifdef WOLFSSL_SMALL_STACK
XFREE(M, NULL, DYNAMIC_TYPE_TMP_BUFFER);
XFREE(M, NULL, DYNAMIC_TYPE_BIGINT);
#endif
return err;
@@ -2192,7 +2192,7 @@ LBL_M:
}
#ifdef WOLFSSL_SMALL_STACK
XFREE(M, NULL, DYNAMIC_TYPE_TMP_BUFFER);
XFREE(M, NULL, DYNAMIC_TYPE_BIGINT);
#endif
return err;

View File

@@ -44,7 +44,7 @@ int SSL_STSAFE_LoadDeviceCertificate(byte** pRawCertificate,
/* Try reading device certificate from ST-SAFE Zone 0 */
err = stsafe_interface_read_device_certificate_raw(
pRawCertificate, (uint32_t*)pRawCertificateLen);
if (err == 0) {
if (err == STSAFE_A_OK) {
#if 0
/* example for loading into WOLFSSL_CTX */
err = wolfSSL_CTX_use_certificate_buffer(ctx,
@@ -57,6 +57,9 @@ int SSL_STSAFE_LoadDeviceCertificate(byte** pRawCertificate,
*pRawCertificate = NULL;
#endif
}
else {
err = WC_HW_E;
}
return err;
}
@@ -86,7 +89,11 @@ int SSL_STSAFE_CreateKeyCb(WOLFSSL* ssl, ecc_key* key, word32 keySz,
/* generate new ephemeral key on device */
err = stsafe_interface_create_key(&slot, curve_id, (uint8_t*)&pubKeyRaw[0]);
if (err != 0) {
if (err != STSAFE_A_OK) {
#ifdef USE_STSAFE_VERBOSE
STSAFE_INTERFACE_PRINTF("stsafe_interface_create_key error: %d\n", err);
#endif
err = WC_HW_E;
return err;
}
@@ -159,6 +166,12 @@ int SSL_STSAFE_VerifyPeerCertCb(WOLFSSL* ssl,
/* Verify signature */
err = stsafe_interface_verify(curve_id, (uint8_t*)hash, sigRS,
pubKeyX, pubKeyY, (int32_t*)result);
if (err != STSAFE_A_OK) {
#ifdef USE_STSAFE_VERBOSE
STSAFE_INTERFACE_PRINTF("stsafe_interface_verify error: %d\n", err);
#endif
err = WC_HW_E;
}
}
wc_ecc_free(&key);
@@ -199,7 +212,11 @@ int SSL_STSAFE_SignCertificateCb(WOLFSSL* ssl, const byte* in,
/* Sign will always use the curve type in slot 0 (the TLS curve needs to match) */
XMEMSET(sigRS, 0, sizeof(sigRS));
err = stsafe_interface_sign(STSAFE_A_SLOT_0, curve_id, digest, sigRS);
if (err != 0) {
if (err != STSAFE_A_OK) {
#ifdef USE_STSAFE_VERBOSE
STSAFE_INTERFACE_PRINTF("stsafe_interface_sign error: %d\n", err);
#endif
err = WC_HW_E;
return err;
}
@@ -208,7 +225,7 @@ int SSL_STSAFE_SignCertificateCb(WOLFSSL* ssl, const byte* in,
s = &sigRS[key_sz];
err = wc_ecc_rs_raw_to_sig((const byte*)r, key_sz, (const byte*)s, key_sz,
out, outSz);
if (err !=0) {
if (err != 0) {
#ifdef USE_STSAFE_VERBOSE
WOLFSSL_MSG("Error converting RS to Signature");
#endif
@@ -266,7 +283,11 @@ int SSL_STSAFE_SharedSecretCb(WOLFSSL* ssl, ecc_key* otherKey,
}
err = stsafe_interface_create_key(&slot, curve_id, (uint8_t*)&pubKeyRaw[0]);
if (err != 0) {
if (err != STSAFE_A_OK) {
#ifdef USE_STSAFE_VERBOSE
STSAFE_INTERFACE_PRINTF("stsafe_interface_create_key error: %d\n", err);
#endif
err = WC_HW_E;
return err;
}
@@ -303,6 +324,12 @@ int SSL_STSAFE_SharedSecretCb(WOLFSSL* ssl, ecc_key* otherKey,
/* Compute shared secret */
err = stsafe_interface_shared_secret(curve_id, &otherKeyX[0], &otherKeyY[0],
out, (int32_t*)outlen);
if (err != STSAFE_A_OK) {
#ifdef USE_STSAFE_VERBOSE
STSAFE_INTERFACE_PRINTF("stsafe_interface_shared_secret error: %d\n", err);
#endif
err = WC_HW_E;
}
return err;
}
@@ -380,7 +407,11 @@ int wolfSSL_STSAFE_CryptoDevCb(int devId, wc_CryptoInfo* info, void* ctx)
/* generate new ephemeral key on device */
rc = stsafe_interface_create_key(&slot, curve_id,
(uint8_t*)pubKeyRaw);
if (rc != 0) {
if (rc != STSAFE_A_OK) {
#ifdef USE_STSAFE_VERBOSE
STSAFE_INTERFACE_PRINTF("stsafe_interface_create_key error: %d\n", rc);
#endif
rc = WC_HW_E;
return rc;
}
@@ -415,7 +446,11 @@ int wolfSSL_STSAFE_CryptoDevCb(int devId, wc_CryptoInfo* info, void* ctx)
XMEMSET(sigRS, 0, sizeof(sigRS));
rc = stsafe_interface_sign(STSAFE_A_SLOT_0, curve_id,
(uint8_t*)info->pk.eccsign.in, sigRS);
if (rc != 0) {
if (rc != STSAFE_A_OK) {
#ifdef USE_STSAFE_VERBOSE
STSAFE_INTERFACE_PRINTF("stsafe_interface_sign error: %d\n", rc);
#endif
rc = WC_HW_E;
return rc;
}
@@ -468,6 +503,12 @@ int wolfSSL_STSAFE_CryptoDevCb(int devId, wc_CryptoInfo* info, void* ctx)
rc = stsafe_interface_verify(curve_id,
(uint8_t*)info->pk.eccverify.hash, sigRS, pubKeyX, pubKeyY,
(int32_t*)info->pk.eccverify.res);
if (rc != STSAFE_A_OK) {
#ifdef USE_STSAFE_VERBOSE
STSAFE_INTERFACE_PRINTF("stsafe_interface_verify error: %d\n", rc);
#endif
rc = WC_HW_E;
}
}
}
else if (info->pk.type == WC_PK_TYPE_ECDH) {
@@ -497,6 +538,12 @@ int wolfSSL_STSAFE_CryptoDevCb(int devId, wc_CryptoInfo* info, void* ctx)
rc = stsafe_interface_shared_secret(curve_id,
otherKeyX, otherKeyY,
info->pk.ecdh.out, (int32_t*)info->pk.ecdh.outlen);
if (rc != STSAFE_A_OK) {
#ifdef USE_STSAFE_VERBOSE
STSAFE_INTERFACE_PRINTF("stsafe_interface_shared_secret error: %d\n", rc);
#endif
rc = WC_HW_E;
}
}
}
}

View File

@@ -478,7 +478,7 @@ WC_INLINE static int fp_mul_comba_mulx(fp_int *A, fp_int *B, fp_int *C)
#endif
#ifdef WOLFSSL_SMALL_STACK
tmp = (fp_int*)XMALLOC(sizeof(fp_int), NULL, DYNAMIC_TYPE_TMP_BUFFER);
tmp = (fp_int*)XMALLOC(sizeof(fp_int), NULL, DYNAMIC_TYPE_BIGINT);
if (tmp == NULL)
return FP_MEM;
#endif
@@ -504,7 +504,7 @@ WC_INLINE static int fp_mul_comba_mulx(fp_int *A, fp_int *B, fp_int *C)
fp_copy(dst, C);
#ifdef WOLFSSL_SMALL_STACK
XFREE(tmp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
XFREE(tmp, NULL, DYNAMIC_TYPE_BIGINT);
#endif
return FP_OKAY;
@@ -526,7 +526,7 @@ int fp_mul_comba(fp_int *A, fp_int *B, fp_int *C)
IF_HAVE_INTEL_MULX(ret = fp_mul_comba_mulx(A, B, C), return ret) ;
#ifdef WOLFSSL_SMALL_STACK
tmp = (fp_int*)XMALLOC(sizeof(fp_int), NULL, DYNAMIC_TYPE_TMP_BUFFER);
tmp = (fp_int*)XMALLOC(sizeof(fp_int), NULL, DYNAMIC_TYPE_BIGINT);
if (tmp == NULL)
return FP_MEM;
#endif
@@ -580,7 +580,7 @@ int fp_mul_comba(fp_int *A, fp_int *B, fp_int *C)
fp_copy(dst, C);
#ifdef WOLFSSL_SMALL_STACK
XFREE(tmp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
XFREE(tmp, NULL, DYNAMIC_TYPE_BIGINT);
#endif
return ret;
}
@@ -612,7 +612,7 @@ int fp_div(fp_int *a, fp_int *b, fp_int *c, fp_int *d)
}
#ifdef WOLFSSL_SMALL_STACK
q = (fp_int*)XMALLOC(sizeof(fp_int) * 5, NULL, DYNAMIC_TYPE_TMP_BUFFER);
q = (fp_int*)XMALLOC(sizeof(fp_int) * 5, NULL, DYNAMIC_TYPE_BIGINT);
if (q == NULL) {
return FP_MEM;
}
@@ -736,7 +736,7 @@ int fp_div(fp_int *a, fp_int *b, fp_int *c, fp_int *d)
}
#ifdef WOLFSSL_SMALL_STACK
XFREE(q, NULL, DYNAMIC_TYPE_TMP_BUFFER);
XFREE(q, NULL, DYNAMIC_TYPE_BIGINT);
#endif
return FP_OKAY;
}
@@ -832,7 +832,7 @@ int fp_mod(fp_int *a, fp_int *b, fp_int *c)
int err;
#ifdef WOLFSSL_SMALL_STACK
t = (fp_int*)XMALLOC(sizeof(fp_int), NULL, DYNAMIC_TYPE_TMP_BUFFER);
t = (fp_int*)XMALLOC(sizeof(fp_int), NULL, DYNAMIC_TYPE_BIGINT);
if (t == NULL)
return FP_MEM;
#endif
@@ -848,7 +848,7 @@ int fp_mod(fp_int *a, fp_int *b, fp_int *c)
}
#ifdef WOLFSSL_SMALL_STACK
XFREE(t, NULL, DYNAMIC_TYPE_TMP_BUFFER);
XFREE(t, NULL, DYNAMIC_TYPE_BIGINT);
#endif
return err;
}
@@ -896,7 +896,7 @@ static int fp_invmod_slow (fp_int * a, fp_int * b, fp_int * c)
}
#ifdef WOLFSSL_SMALL_STACK
x = (fp_int*)XMALLOC(sizeof(fp_int) * 8, NULL, DYNAMIC_TYPE_TMP_BUFFER);
x = (fp_int*)XMALLOC(sizeof(fp_int) * 8, NULL, DYNAMIC_TYPE_BIGINT);
if (x == NULL) {
return FP_MEM;
}
@@ -912,7 +912,7 @@ static int fp_invmod_slow (fp_int * a, fp_int * b, fp_int * c)
/* x = a, y = b */
if ((err = fp_mod(a, b, x)) != FP_OKAY) {
#ifdef WOLFSSL_SMALL_STACK
XFREE(x, NULL, DYNAMIC_TYPE_TMP_BUFFER);
XFREE(x, NULL, DYNAMIC_TYPE_BIGINT);
#endif
return err;
}
@@ -921,7 +921,7 @@ static int fp_invmod_slow (fp_int * a, fp_int * b, fp_int * c)
/* 2. [modified] if x,y are both even then return an error! */
if (fp_iseven (x) == FP_YES && fp_iseven (y) == FP_YES) {
#ifdef WOLFSSL_SMALL_STACK
XFREE(x, NULL, DYNAMIC_TYPE_TMP_BUFFER);
XFREE(x, NULL, DYNAMIC_TYPE_BIGINT);
#endif
return FP_VAL;
}
@@ -987,7 +987,7 @@ top:
/* if v != 1 then there is no inverse */
if (fp_cmp_d (v, 1) != FP_EQ) {
#ifdef WOLFSSL_SMALL_STACK
XFREE(x, NULL, DYNAMIC_TYPE_TMP_BUFFER);
XFREE(x, NULL, DYNAMIC_TYPE_BIGINT);
#endif
return FP_VAL;
}
@@ -1005,7 +1005,7 @@ top:
/* C is now the inverse */
fp_copy(C, c);
#ifdef WOLFSSL_SMALL_STACK
XFREE(x, NULL, DYNAMIC_TYPE_TMP_BUFFER);
XFREE(x, NULL, DYNAMIC_TYPE_BIGINT);
#endif
return FP_OKAY;
}
@@ -1026,7 +1026,7 @@ int fp_invmod(fp_int *a, fp_int *b, fp_int *c)
}
#ifdef WOLFSSL_SMALL_STACK
x = (fp_int*)XMALLOC(sizeof(fp_int) * 6, NULL, DYNAMIC_TYPE_TMP_BUFFER);
x = (fp_int*)XMALLOC(sizeof(fp_int) * 6, NULL, DYNAMIC_TYPE_BIGINT);
if (x == NULL) {
return FP_MEM;
}
@@ -1098,7 +1098,7 @@ top:
/* if v != 1 then there is no inverse */
if (fp_cmp_d (v, 1) != FP_EQ) {
#ifdef WOLFSSL_SMALL_STACK
XFREE(x, NULL, DYNAMIC_TYPE_TMP_BUFFER);
XFREE(x, NULL, DYNAMIC_TYPE_BIGINT);
#endif
return FP_VAL;
}
@@ -1115,7 +1115,7 @@ top:
fp_copy (D, c);
c->sign = neg;
#ifdef WOLFSSL_SMALL_STACK
XFREE(x, NULL, DYNAMIC_TYPE_TMP_BUFFER);
XFREE(x, NULL, DYNAMIC_TYPE_BIGINT);
#endif
return FP_OKAY;
}
@@ -1131,7 +1131,7 @@ int fp_mulmod(fp_int *a, fp_int *b, fp_int *c, fp_int *d)
#endif
#ifdef WOLFSSL_SMALL_STACK
t = (fp_int*)XMALLOC(sizeof(fp_int), NULL, DYNAMIC_TYPE_TMP_BUFFER);
t = (fp_int*)XMALLOC(sizeof(fp_int), NULL, DYNAMIC_TYPE_BIGINT);
if (t == NULL)
return FP_MEM;
#endif
@@ -1151,7 +1151,7 @@ int fp_mulmod(fp_int *a, fp_int *b, fp_int *c, fp_int *d)
}
#ifdef WOLFSSL_SMALL_STACK
XFREE(t, NULL, DYNAMIC_TYPE_TMP_BUFFER);
XFREE(t, NULL, DYNAMIC_TYPE_BIGINT);
#endif
return err;
}
@@ -1167,7 +1167,7 @@ int fp_submod(fp_int *a, fp_int *b, fp_int *c, fp_int *d)
#endif
#ifdef WOLFSSL_SMALL_STACK
t = (fp_int*)XMALLOC(sizeof(fp_int), NULL, DYNAMIC_TYPE_TMP_BUFFER);
t = (fp_int*)XMALLOC(sizeof(fp_int), NULL, DYNAMIC_TYPE_BIGINT);
if (t == NULL)
return FP_MEM;
#endif
@@ -1185,7 +1185,7 @@ int fp_submod(fp_int *a, fp_int *b, fp_int *c, fp_int *d)
}
#ifdef WOLFSSL_SMALL_STACK
XFREE(t, NULL, DYNAMIC_TYPE_TMP_BUFFER);
XFREE(t, NULL, DYNAMIC_TYPE_BIGINT);
#endif
return err;
}
@@ -1201,7 +1201,7 @@ int fp_addmod(fp_int *a, fp_int *b, fp_int *c, fp_int *d)
#endif
#ifdef WOLFSSL_SMALL_STACK
t = (fp_int*)XMALLOC(sizeof(fp_int), NULL, DYNAMIC_TYPE_TMP_BUFFER);
t = (fp_int*)XMALLOC(sizeof(fp_int), NULL, DYNAMIC_TYPE_BIGINT);
if (t == NULL)
return FP_MEM;
#endif
@@ -1219,7 +1219,7 @@ int fp_addmod(fp_int *a, fp_int *b, fp_int *c, fp_int *d)
}
#ifdef WOLFSSL_SMALL_STACK
XFREE(t, NULL, DYNAMIC_TYPE_TMP_BUFFER);
XFREE(t, NULL, DYNAMIC_TYPE_BIGINT);
#endif
return err;
}
@@ -1476,9 +1476,9 @@ static int _fp_exptmod(fp_int * G, fp_int * X, int digits, fp_int * P, fp_int *
#ifdef WOLFSSL_SMALL_STACK
#ifndef WC_NO_CACHE_RESISTANT
R = (fp_int*)XMALLOC(sizeof(fp_int) * 3, NULL, DYNAMIC_TYPE_TMP_BUFFER);
R = (fp_int*)XMALLOC(sizeof(fp_int) * 3, NULL, DYNAMIC_TYPE_BIGINT);
#else
R = (fp_int*)XMALLOC(sizeof(fp_int) * 2, NULL, DYNAMIC_TYPE_TMP_BUFFER);
R = (fp_int*)XMALLOC(sizeof(fp_int) * 2, NULL, DYNAMIC_TYPE_BIGINT);
#endif
if (R == NULL)
return FP_MEM;
@@ -1530,14 +1530,14 @@ static int _fp_exptmod(fp_int * G, fp_int * X, int digits, fp_int * P, fp_int *
err = fp_mul(&R[0], &R[1], &R[y^1]);
if (err != FP_OKAY) {
#ifdef WOLFSSL_SMALL_STACK
XFREE(R, NULL, DYNAMIC_TYPE_TMP_BUFFER);
XFREE(R, NULL, DYNAMIC_TYPE_BIGINT);
#endif
return err;
}
err = fp_montgomery_reduce(&R[y^1], P, mp);
if (err != FP_OKAY) {
#ifdef WOLFSSL_SMALL_STACK
XFREE(R, NULL, DYNAMIC_TYPE_TMP_BUFFER);
XFREE(R, NULL, DYNAMIC_TYPE_BIGINT);
#endif
return err;
}
@@ -1546,14 +1546,14 @@ static int _fp_exptmod(fp_int * G, fp_int * X, int digits, fp_int * P, fp_int *
err = fp_sqr(&R[y], &R[y]);
if (err != FP_OKAY) {
#ifdef WOLFSSL_SMALL_STACK
XFREE(R, NULL, DYNAMIC_TYPE_TMP_BUFFER);
XFREE(R, NULL, DYNAMIC_TYPE_BIGINT);
#endif
return err;
}
err = fp_montgomery_reduce(&R[y], P, mp);
if (err != FP_OKAY) {
#ifdef WOLFSSL_SMALL_STACK
XFREE(R, NULL, DYNAMIC_TYPE_TMP_BUFFER);
XFREE(R, NULL, DYNAMIC_TYPE_BIGINT);
#endif
return err;
}
@@ -1567,14 +1567,14 @@ static int _fp_exptmod(fp_int * G, fp_int * X, int digits, fp_int * P, fp_int *
err = fp_sqr(&R[2], &R[2]);
if (err != FP_OKAY) {
#ifdef WOLFSSL_SMALL_STACK
XFREE(R, NULL, DYNAMIC_TYPE_TMP_BUFFER);
XFREE(R, NULL, DYNAMIC_TYPE_BIGINT);
#endif
return err;
}
err = fp_montgomery_reduce(&R[2], P, mp);
if (err != FP_OKAY) {
#ifdef WOLFSSL_SMALL_STACK
XFREE(R, NULL, DYNAMIC_TYPE_TMP_BUFFER);
XFREE(R, NULL, DYNAMIC_TYPE_BIGINT);
#endif
return err;
}
@@ -1587,7 +1587,7 @@ static int _fp_exptmod(fp_int * G, fp_int * X, int digits, fp_int * P, fp_int *
err = fp_montgomery_reduce(&R[0], P, mp);
fp_copy(&R[0], Y);
#ifdef WOLFSSL_SMALL_STACK
XFREE(R, NULL, DYNAMIC_TYPE_TMP_BUFFER);
XFREE(R, NULL, DYNAMIC_TYPE_BIGINT);
#endif
return err;
}
@@ -1633,7 +1633,7 @@ static int _fp_exptmod(fp_int * G, fp_int * X, int digits, fp_int * P,
#ifdef WOLFSSL_SMALL_STACK
/* only allocate space for what's needed for window plus res */
M = (fp_int*)XMALLOC(sizeof(fp_int)*((1 << winsize) + 1), NULL, DYNAMIC_TYPE_TMP_BUFFER);
M = (fp_int*)XMALLOC(sizeof(fp_int)*((1 << winsize) + 1), NULL, DYNAMIC_TYPE_BIGINT);
if (M == NULL) {
return FP_MEM;
}
@@ -1674,7 +1674,7 @@ static int _fp_exptmod(fp_int * G, fp_int * X, int digits, fp_int * P,
err = fp_montgomery_reduce (&M[1 << (winsize - 1)], P, mp);
if (err != FP_OKAY) {
#ifdef WOLFSSL_SMALL_STACK
XFREE(M, NULL, DYNAMIC_TYPE_TMP_BUFFER);
XFREE(M, NULL, DYNAMIC_TYPE_BIGINT);
#endif
return err;
}
@@ -1685,14 +1685,14 @@ static int _fp_exptmod(fp_int * G, fp_int * X, int digits, fp_int * P,
err = fp_mul(&M[x - 1], &M[1], &M[x]);
if (err != FP_OKAY) {
#ifdef WOLFSSL_SMALL_STACK
XFREE(M, NULL, DYNAMIC_TYPE_TMP_BUFFER);
XFREE(M, NULL, DYNAMIC_TYPE_BIGINT);
#endif
return err;
}
err = fp_montgomery_reduce(&M[x], P, mp);
if (err != FP_OKAY) {
#ifdef WOLFSSL_SMALL_STACK
XFREE(M, NULL, DYNAMIC_TYPE_TMP_BUFFER);
XFREE(M, NULL, DYNAMIC_TYPE_BIGINT);
#endif
return err;
}
@@ -1736,14 +1736,14 @@ static int _fp_exptmod(fp_int * G, fp_int * X, int digits, fp_int * P,
err = fp_sqr(res, res);
if (err != FP_OKAY) {
#ifdef WOLFSSL_SMALL_STACK
XFREE(M, NULL, DYNAMIC_TYPE_TMP_BUFFER);
XFREE(M, NULL, DYNAMIC_TYPE_BIGINT);
#endif
return err;
}
fp_montgomery_reduce(res, P, mp);
if (err != FP_OKAY) {
#ifdef WOLFSSL_SMALL_STACK
XFREE(M, NULL, DYNAMIC_TYPE_TMP_BUFFER);
XFREE(M, NULL, DYNAMIC_TYPE_BIGINT);
#endif
return err;
}
@@ -1761,14 +1761,14 @@ static int _fp_exptmod(fp_int * G, fp_int * X, int digits, fp_int * P,
err = fp_sqr(res, res);
if (err != FP_OKAY) {
#ifdef WOLFSSL_SMALL_STACK
XFREE(M, NULL, DYNAMIC_TYPE_TMP_BUFFER);
XFREE(M, NULL, DYNAMIC_TYPE_BIGINT);
#endif
return err;
}
err = fp_montgomery_reduce(res, P, mp);
if (err != FP_OKAY) {
#ifdef WOLFSSL_SMALL_STACK
XFREE(M, NULL, DYNAMIC_TYPE_TMP_BUFFER);
XFREE(M, NULL, DYNAMIC_TYPE_BIGINT);
#endif
return err;
}
@@ -1778,14 +1778,14 @@ static int _fp_exptmod(fp_int * G, fp_int * X, int digits, fp_int * P,
err = fp_mul(res, &M[bitbuf], res);
if (err != FP_OKAY) {
#ifdef WOLFSSL_SMALL_STACK
XFREE(M, NULL, DYNAMIC_TYPE_TMP_BUFFER);
XFREE(M, NULL, DYNAMIC_TYPE_BIGINT);
#endif
return err;
}
err = fp_montgomery_reduce(res, P, mp);
if (err != FP_OKAY) {
#ifdef WOLFSSL_SMALL_STACK
XFREE(M, NULL, DYNAMIC_TYPE_TMP_BUFFER);
XFREE(M, NULL, DYNAMIC_TYPE_BIGINT);
#endif
return err;
}
@@ -1804,14 +1804,14 @@ static int _fp_exptmod(fp_int * G, fp_int * X, int digits, fp_int * P,
err = fp_sqr(res, res);
if (err != FP_OKAY) {
#ifdef WOLFSSL_SMALL_STACK
XFREE(M, NULL, DYNAMIC_TYPE_TMP_BUFFER);
XFREE(M, NULL, DYNAMIC_TYPE_BIGINT);
#endif
return err;
}
err = fp_montgomery_reduce(res, P, mp);
if (err != FP_OKAY) {
#ifdef WOLFSSL_SMALL_STACK
XFREE(M, NULL, DYNAMIC_TYPE_TMP_BUFFER);
XFREE(M, NULL, DYNAMIC_TYPE_BIGINT);
#endif
return err;
}
@@ -1823,14 +1823,14 @@ static int _fp_exptmod(fp_int * G, fp_int * X, int digits, fp_int * P,
err = fp_mul(res, &M[1], res);
if (err != FP_OKAY) {
#ifdef WOLFSSL_SMALL_STACK
XFREE(M, NULL, DYNAMIC_TYPE_TMP_BUFFER);
XFREE(M, NULL, DYNAMIC_TYPE_BIGINT);
#endif
return err;
}
err = fp_montgomery_reduce(res, P, mp);
if (err != FP_OKAY) {
#ifdef WOLFSSL_SMALL_STACK
XFREE(M, NULL, DYNAMIC_TYPE_TMP_BUFFER);
XFREE(M, NULL, DYNAMIC_TYPE_BIGINT);
#endif
return err;
}
@@ -1850,7 +1850,7 @@ static int _fp_exptmod(fp_int * G, fp_int * X, int digits, fp_int * P,
fp_copy (res, Y);
#ifdef WOLFSSL_SMALL_STACK
XFREE(M, NULL, DYNAMIC_TYPE_TMP_BUFFER);
XFREE(M, NULL, DYNAMIC_TYPE_BIGINT);
#endif
return err;
}
@@ -1887,7 +1887,7 @@ int fp_exptmod(fp_int * G, fp_int * X, fp_int * P, fp_int * Y)
#endif
#ifdef WOLFSSL_SMALL_STACK
tmp = (fp_int*)XMALLOC(sizeof(fp_int), NULL, DYNAMIC_TYPE_TMP_BUFFER);
tmp = (fp_int*)XMALLOC(sizeof(fp_int), NULL, DYNAMIC_TYPE_BIGINT);
if (tmp == NULL)
return FP_MEM;
#endif
@@ -1962,7 +1962,7 @@ int fp_exptmod_ex(fp_int * G, fp_int * X, int digits, fp_int * P, fp_int * Y)
}
}
#ifdef WOLFSSL_SMALL_STACK
XFREE(tmp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
XFREE(tmp, NULL, DYNAMIC_TYPE_BIGINT);
#endif
return err;
#else
@@ -2132,7 +2132,7 @@ int fp_sqr_comba(fp_int *A, fp_int *B)
#endif
#ifdef WOLFSSL_SMALL_STACK
tmp = (fp_int*)XMALLOC(sizeof(fp_int), NULL, DYNAMIC_TYPE_TMP_BUFFER);
tmp = (fp_int*)XMALLOC(sizeof(fp_int), NULL, DYNAMIC_TYPE_BIGINT);
if (tmp == NULL)
return FP_MEM;
#endif
@@ -2207,7 +2207,7 @@ int fp_sqr_comba(fp_int *A, fp_int *B)
}
#ifdef WOLFSSL_SMALL_STACK
XFREE(tmp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
XFREE(tmp, NULL, DYNAMIC_TYPE_BIGINT);
#endif
return FP_OKAY;
}
@@ -2376,7 +2376,7 @@ static int fp_montgomery_reduce_mulx(fp_int *a, fp_int *m, fp_digit mp)
#ifdef WOLFSSL_SMALL_STACK
/* only allocate space for what's needed for window plus res */
c = (fp_digit*)XMALLOC(sizeof(fp_digit)*(FP_SIZE + 1), NULL, DYNAMIC_TYPE_TMP_BUFFER);
c = (fp_digit*)XMALLOC(sizeof(fp_digit)*(FP_SIZE + 1), NULL, DYNAMIC_TYPE_BIGINT);
if (c == NULL) {
return FP_MEM;
}
@@ -2439,7 +2439,7 @@ static int fp_montgomery_reduce_mulx(fp_int *a, fp_int *m, fp_digit mp)
}
#ifdef WOLFSSL_SMALL_STACK
XFREE(c, NULL, DYNAMIC_TYPE_TMP_BUFFER);
XFREE(c, NULL, DYNAMIC_TYPE_BIGINT);
#endif
return FP_OKAY;
}
@@ -2473,7 +2473,7 @@ int fp_montgomery_reduce(fp_int *a, fp_int *m, fp_digit mp)
#ifdef WOLFSSL_SMALL_STACK
/* only allocate space for what's needed for window plus res */
c = (fp_digit*)XMALLOC(sizeof(fp_digit)*(FP_SIZE + 1), NULL, DYNAMIC_TYPE_TMP_BUFFER);
c = (fp_digit*)XMALLOC(sizeof(fp_digit)*(FP_SIZE + 1), NULL, DYNAMIC_TYPE_BIGINT);
if (c == NULL) {
return FP_MEM;
}
@@ -2538,7 +2538,7 @@ int fp_montgomery_reduce(fp_int *a, fp_int *m, fp_digit mp)
}
#ifdef WOLFSSL_SMALL_STACK
XFREE(c, NULL, DYNAMIC_TYPE_TMP_BUFFER);
XFREE(c, NULL, DYNAMIC_TYPE_BIGINT);
#endif
return FP_OKAY;
}
@@ -2644,7 +2644,7 @@ int fp_to_unsigned_bin(fp_int *a, unsigned char *b)
#endif
#ifdef WOLFSSL_SMALL_STACK
t = (fp_int*)XMALLOC(sizeof(fp_int), NULL, DYNAMIC_TYPE_TMP_BUFFER);
t = (fp_int*)XMALLOC(sizeof(fp_int), NULL, DYNAMIC_TYPE_BIGINT);
if (t == NULL)
return FP_MEM;
#endif
@@ -2655,7 +2655,7 @@ int fp_to_unsigned_bin(fp_int *a, unsigned char *b)
fp_reverse (b, x);
#ifdef WOLFSSL_SMALL_STACK
XFREE(t, NULL, DYNAMIC_TYPE_TMP_BUFFER);
XFREE(t, NULL, DYNAMIC_TYPE_BIGINT);
#endif
return FP_OKAY;
}
@@ -2682,7 +2682,7 @@ int fp_to_unsigned_bin_len(fp_int *a, unsigned char *b, int c)
#endif
#ifdef WOLFSSL_SMALL_STACK
t = (fp_int*)XMALLOC(sizeof(fp_int), NULL, DYNAMIC_TYPE_TMP_BUFFER);
t = (fp_int*)XMALLOC(sizeof(fp_int), NULL, DYNAMIC_TYPE_BIGINT);
if (t == NULL)
return FP_MEM;
#endif
@@ -2696,7 +2696,7 @@ int fp_to_unsigned_bin_len(fp_int *a, unsigned char *b, int c)
fp_reverse (b, x);
#ifdef WOLFSSL_SMALL_STACK
XFREE(t, NULL, DYNAMIC_TYPE_TMP_BUFFER);
XFREE(t, NULL, DYNAMIC_TYPE_BIGINT);
#endif
return FP_OKAY;
#endif
@@ -2941,7 +2941,7 @@ int fp_sub_d(fp_int *a, fp_digit b, fp_int *c)
#endif
#ifdef WOLFSSL_SMALL_STACK
tmp = (fp_int*)XMALLOC(sizeof(fp_int), NULL, DYNAMIC_TYPE_TMP_BUFFER);
tmp = (fp_int*)XMALLOC(sizeof(fp_int), NULL, DYNAMIC_TYPE_BIGINT);
if (tmp == NULL)
return FP_MEM;
#endif
@@ -2959,7 +2959,7 @@ int fp_sub_d(fp_int *a, fp_digit b, fp_int *c)
}
#ifdef WOLFSSL_SMALL_STACK
XFREE(tmp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
XFREE(tmp, NULL, DYNAMIC_TYPE_BIGINT);
#endif
return FP_OKAY;
}
@@ -3347,7 +3347,7 @@ int fp_sqrmod(fp_int *a, fp_int *b, fp_int *c)
#endif
#ifdef WOLFSSL_SMALL_STACK
t = (fp_int*)XMALLOC(sizeof(fp_int), NULL, DYNAMIC_TYPE_TMP_BUFFER);
t = (fp_int*)XMALLOC(sizeof(fp_int), NULL, DYNAMIC_TYPE_BIGINT);
if (t == NULL)
return FP_MEM;
#endif
@@ -3368,7 +3368,7 @@ int fp_sqrmod(fp_int *a, fp_int *b, fp_int *c)
}
#ifdef WOLFSSL_SMALL_STACK
XFREE(t, NULL, DYNAMIC_TYPE_TMP_BUFFER);
XFREE(t, NULL, DYNAMIC_TYPE_BIGINT);
#endif
return err;
}
@@ -3405,7 +3405,7 @@ static int fp_exch (fp_int * a, fp_int * b)
#endif
#ifdef WOLFSSL_SMALL_STACK
t = (fp_int*)XMALLOC(sizeof(fp_int), NULL, DYNAMIC_TYPE_TMP_BUFFER);
t = (fp_int*)XMALLOC(sizeof(fp_int), NULL, DYNAMIC_TYPE_BIGINT);
if (t == NULL)
return FP_MEM;
#endif
@@ -3415,7 +3415,7 @@ static int fp_exch (fp_int * a, fp_int * b)
*b = *t;
#ifdef WOLFSSL_SMALL_STACK
XFREE(t, NULL, DYNAMIC_TYPE_TMP_BUFFER);
XFREE(t, NULL, DYNAMIC_TYPE_BIGINT);
#endif
return FP_OKAY;
}
@@ -3511,7 +3511,7 @@ static int fp_div_d(fp_int *a, fp_digit b, fp_int *c, fp_digit *d)
}
#ifdef WOLFSSL_SMALL_STACK
q = (fp_int*)XMALLOC(sizeof(fp_int), NULL, DYNAMIC_TYPE_TMP_BUFFER);
q = (fp_int*)XMALLOC(sizeof(fp_int), NULL, DYNAMIC_TYPE_BIGINT);
if (q == NULL)
return FP_MEM;
#endif
@@ -3547,7 +3547,7 @@ static int fp_div_d(fp_int *a, fp_digit b, fp_int *c, fp_digit *d)
}
#ifdef WOLFSSL_SMALL_STACK
XFREE(q, NULL, DYNAMIC_TYPE_TMP_BUFFER);
XFREE(q, NULL, DYNAMIC_TYPE_BIGINT);
#endif
return FP_OKAY;
}
@@ -3674,7 +3674,7 @@ static int fp_prime_miller_rabin(fp_int * a, fp_int * b, int *result)
#endif
#ifdef WOLFSSL_SMALL_STACK
n1 = (fp_int*)XMALLOC(sizeof(fp_int) * 3, NULL, DYNAMIC_TYPE_TMP_BUFFER);
n1 = (fp_int*)XMALLOC(sizeof(fp_int) * 3, NULL, DYNAMIC_TYPE_BIGINT);
if (n1 == NULL) {
return FP_MEM;
}
@@ -3692,7 +3692,7 @@ static int fp_prime_miller_rabin(fp_int * a, fp_int * b, int *result)
fp_clear(r);
#ifdef WOLFSSL_SMALL_STACK
XFREE(n1, NULL, DYNAMIC_TYPE_TMP_BUFFER);
XFREE(n1, NULL, DYNAMIC_TYPE_BIGINT);
#endif
return err;
@@ -3770,7 +3770,7 @@ int fp_isprime_ex(fp_int *a, int t, int* result)
}
#ifdef WOLFSSL_SMALL_STACK
b = (fp_int*)XMALLOC(sizeof(fp_int), NULL, DYNAMIC_TYPE_TMP_BUFFER);
b = (fp_int*)XMALLOC(sizeof(fp_int), NULL, DYNAMIC_TYPE_BIGINT);
if (b == NULL)
return FP_MEM;
#endif
@@ -3782,14 +3782,14 @@ int fp_isprime_ex(fp_int *a, int t, int* result)
if (res == FP_NO) {
*result = FP_NO;
#ifdef WOLFSSL_SMALL_STACK
XFREE(b, NULL, DYNAMIC_TYPE_TMP_BUFFER);
XFREE(b, NULL, DYNAMIC_TYPE_BIGINT);
#endif
return FP_OKAY;
}
}
*result = FP_YES;
#ifdef WOLFSSL_SMALL_STACK
XFREE(b, NULL, DYNAMIC_TYPE_TMP_BUFFER);
XFREE(b, NULL, DYNAMIC_TYPE_BIGINT);
#endif
return FP_OKAY;
}
@@ -3854,7 +3854,7 @@ int mp_prime_is_prime_ex(mp_int* a, int t, int* result, WC_RNG* rng)
if (base == NULL)
return FP_MEM;
b = (fp_int*)XMALLOC(sizeof(fp_int) * 5, NULL, DYNAMIC_TYPE_TMP_BUFFER);
b = (fp_int*)XMALLOC(sizeof(fp_int) * 5, NULL, DYNAMIC_TYPE_BIGINT);
if (b == NULL) {
return FP_MEM;
}
@@ -3870,7 +3870,7 @@ int mp_prime_is_prime_ex(mp_int* a, int t, int* result, WC_RNG* rng)
err = fp_sub_d(a, 2, c);
if (err != FP_OKAY) {
#ifdef WOLFSSL_SMALL_STACK
XFREE(b, NULL, DYNAMIC_TYPE_TMP_BUFFER);
XFREE(b, NULL, DYNAMIC_TYPE_BIGINT);
XFREE(base, NULL, DYNAMIC_TYPE_TMP_BUFFER);
#endif
return err;
@@ -3878,7 +3878,7 @@ int mp_prime_is_prime_ex(mp_int* a, int t, int* result, WC_RNG* rng)
while (t > 0) {
if ((err = wc_RNG_GenerateBlock(rng, base, baseSz)) != 0) {
#ifdef WOLFSSL_SMALL_STACK
XFREE(b, NULL, DYNAMIC_TYPE_TMP_BUFFER);
XFREE(b, NULL, DYNAMIC_TYPE_BIGINT);
XFREE(base, NULL, DYNAMIC_TYPE_TMP_BUFFER);
#endif
return err;
@@ -3902,7 +3902,7 @@ int mp_prime_is_prime_ex(mp_int* a, int t, int* result, WC_RNG* rng)
fp_clear(b);
fp_clear(c);
#ifdef WOLFSSL_SMALL_STACK
XFREE(b, NULL, DYNAMIC_TYPE_TMP_BUFFER);
XFREE(b, NULL, DYNAMIC_TYPE_BIGINT);
XFREE(base, NULL, DYNAMIC_TYPE_TMP_BUFFER);
#endif
}
@@ -4030,7 +4030,7 @@ int fp_lcm(fp_int *a, fp_int *b, fp_int *c)
#endif
#ifdef WOLFSSL_SMALL_STACK
t = (fp_int*)XMALLOC(sizeof(fp_int) * 2, NULL, DYNAMIC_TYPE_TMP_BUFFER);
t = (fp_int*)XMALLOC(sizeof(fp_int) * 2, NULL, DYNAMIC_TYPE_BIGINT);
if (t == NULL) {
return FP_MEM;
}
@@ -4052,7 +4052,7 @@ int fp_lcm(fp_int *a, fp_int *b, fp_int *c)
}
#ifdef WOLFSSL_SMALL_STACK
XFREE(t, NULL, DYNAMIC_TYPE_TMP_BUFFER);
XFREE(t, NULL, DYNAMIC_TYPE_BIGINT);
#endif
return err;
}
@@ -4087,7 +4087,7 @@ int fp_gcd(fp_int *a, fp_int *b, fp_int *c)
}
#ifdef WOLFSSL_SMALL_STACK
u = (fp_int*)XMALLOC(sizeof(fp_int) * 3, NULL, DYNAMIC_TYPE_TMP_BUFFER);
u = (fp_int*)XMALLOC(sizeof(fp_int) * 3, NULL, DYNAMIC_TYPE_BIGINT);
if (u == NULL) {
return FP_MEM;
}
@@ -4112,7 +4112,7 @@ int fp_gcd(fp_int *a, fp_int *b, fp_int *c)
fp_copy(u, c);
#ifdef WOLFSSL_SMALL_STACK
XFREE(u, NULL, DYNAMIC_TYPE_TMP_BUFFER);
XFREE(u, NULL, DYNAMIC_TYPE_BIGINT);
#endif
return FP_OKAY;
}
@@ -4378,7 +4378,7 @@ int mp_radix_size (mp_int *a, int radix, int *size)
}
#ifdef WOLFSSL_SMALL_STACK
t = (fp_int*)XMALLOC(sizeof(fp_int), NULL, DYNAMIC_TYPE_TMP_BUFFER);
t = (fp_int*)XMALLOC(sizeof(fp_int), NULL, DYNAMIC_TYPE_BIGINT);
if (t == NULL)
return FP_MEM;
#endif
@@ -4394,7 +4394,7 @@ int mp_radix_size (mp_int *a, int radix, int *size)
if ((res = fp_div_d (t, (mp_digit) radix, t, &d)) != FP_OKAY) {
fp_zero (t);
#ifdef WOLFSSL_SMALL_STACK
XFREE(t, NULL, DYNAMIC_TYPE_TMP_BUFFER);
XFREE(t, NULL, DYNAMIC_TYPE_BIGINT);
#endif
return res;
}
@@ -4405,7 +4405,7 @@ int mp_radix_size (mp_int *a, int radix, int *size)
/* return digs + 1, the 1 is for the NULL byte that would be required. */
*size = digs + 1;
#ifdef WOLFSSL_SMALL_STACK
XFREE(t, NULL, DYNAMIC_TYPE_TMP_BUFFER);
XFREE(t, NULL, DYNAMIC_TYPE_BIGINT);
#endif
return FP_OKAY;
}
@@ -4435,7 +4435,7 @@ int mp_toradix (mp_int *a, char *str, int radix)
}
#ifdef WOLFSSL_SMALL_STACK
t = (fp_int*)XMALLOC(sizeof(fp_int), NULL, DYNAMIC_TYPE_TMP_BUFFER);
t = (fp_int*)XMALLOC(sizeof(fp_int), NULL, DYNAMIC_TYPE_BIGINT);
if (t == NULL)
return FP_MEM;
#endif
@@ -4455,7 +4455,7 @@ int mp_toradix (mp_int *a, char *str, int radix)
if ((res = fp_div_d (t, (fp_digit) radix, t, &d)) != FP_OKAY) {
fp_zero (t);
#ifdef WOLFSSL_SMALL_STACK
XFREE(t, NULL, DYNAMIC_TYPE_TMP_BUFFER);
XFREE(t, NULL, DYNAMIC_TYPE_BIGINT);
#endif
return res;
}
@@ -4473,7 +4473,7 @@ int mp_toradix (mp_int *a, char *str, int radix)
fp_zero (t);
#ifdef WOLFSSL_SMALL_STACK
XFREE(t, NULL, DYNAMIC_TYPE_TMP_BUFFER);
XFREE(t, NULL, DYNAMIC_TYPE_BIGINT);
#endif
return FP_OKAY;
}

View File

@@ -480,6 +480,16 @@
#define XSTRTOK(s1,d,ptr) strtok_r((s1),(d),(ptr))
#endif
#endif
#if defined(WOLFSSL_CERT_EXT) || defined(HAVE_OCSP) || \
defined(HAVE_CRL_IO) || defined(HAVE_HTTP_CLIENT) || \
!defined(NO_CRYPT_BENCHMARK)
#ifndef XATOI /* if custom XATOI is not already defined */
#include <stdlib.h>
#define XATOI(s) atoi((s))
#endif
#endif
#endif
#ifdef USE_WOLF_STRTOK