Added return codes to wc_InitDhKey, wc_InitDsaKey and mp_set. Added missing return code checks on mp_copy in ecc.c. Fixed build with DSA and no ECC where mp_set function def would be missing.

This commit is contained in:
David Garske
2017-02-01 10:13:47 -08:00
parent d14be65315
commit 9c7407d18c
11 changed files with 292 additions and 201 deletions

View File

@@ -49,12 +49,17 @@
#endif
void wc_InitDhKey(DhKey* key)
int wc_InitDhKey(DhKey* key)
{
if (key) {
mp_init(&key->p);
mp_init(&key->g);
}
int ret = 0;
if (key == NULL)
return BAD_FUNC_ARG;
if (mp_init_multi(&key->p, &key->g, NULL, NULL, NULL, NULL) != MP_OKAY)
ret = MEMORY_E;
return ret;
}

View File

@@ -50,31 +50,35 @@ enum {
void wc_InitDsaKey(DsaKey* key)
int wc_InitDsaKey(DsaKey* key)
{
if (key == NULL)
return;
return BAD_FUNC_ARG;
key->type = -1; /* haven't decided yet */
key->heap = NULL;
/* public alloc parts */
mp_init(&key->p);
mp_init(&key->q);
mp_init(&key->g);
mp_init(&key->y);
return mp_init_multi(
/* public alloc parts */
&key->p,
&key->q,
&key->g,
&key->y,
/* private alloc parts */
mp_init(&key->x);
/* private alloc parts */
&key->x,
NULL
);
}
int wc_InitDsaKey_h(DsaKey* key, void* h)
{
wc_InitDsaKey(key);
key->heap = h;
int ret = wc_InitDsaKey(key);
if (ret == 0)
key->heap = h;
return 0;
return ret;
}
@@ -317,7 +321,13 @@ int wc_MakeDsaParameters(WC_RNG *rng, int modulus_size, DsaKey *dsa)
}
/* find a value g for which g^tmp2 != 1 */
mp_set(&dsa->g, 1);
if (mp_set(&dsa->g, 1) != MP_OKAY) {
mp_clear(&dsa->q);
mp_clear(&dsa->p);
mp_clear(&tmp);
mp_clear(&tmp2);
return MP_INIT_E;
}
do {
err = mp_add_d(&dsa->g, 1, &dsa->g);

View File

@@ -1807,10 +1807,12 @@ int ecc_map(ecc_point* P, mp_int* modulus, mp_digit mp)
/* special case for point at infinity */
if (mp_cmp_d(P->z, 0) == MP_EQ) {
mp_set(P->x, 0);
mp_set(P->y, 0);
mp_set(P->z, 1);
return MP_OKAY;
err = mp_set(P->x, 0);
if (err == MP_OKAY)
err = mp_set(P->y, 0);
if (err == MP_OKAY)
err = mp_set(P->z, 1);
return err;
}
if ((err = mp_init_multi(&t1, &t2, NULL, NULL, NULL, NULL)) != MP_OKAY) {
@@ -1872,13 +1874,16 @@ int ecc_map(ecc_point* P, mp_int* modulus, mp_digit mp)
err = mp_montgomery_reduce(y, modulus, mp);
if (err == MP_OKAY)
mp_set(z, 1);
err = mp_set(z, 1);
#ifdef ALT_ECC_SIZE
/* return result */
mp_copy(x, P->x);
mp_copy(y, P->y);
mp_copy(z, P->z);
if (err == MP_OKAY)
err = mp_copy(x, P->x);
if (err == MP_OKAY)
err = mp_copy(y, P->y);
if (err == MP_OKAY)
err = mp_copy(z, P->z);
#endif
done:
@@ -2861,7 +2866,7 @@ int wc_ecc_make_key_ex(WC_RNG* rng, int keysize, ecc_key* key, int curve_id)
if (err == MP_OKAY)
err = mp_copy(curve->Gy, base->y);
if (err == MP_OKAY)
mp_set(base->z, 1);
err = mp_set(base->z, 1);
/* generate k */
if (err == MP_OKAY)
@@ -3797,7 +3802,7 @@ int wc_ecc_verify_hash_ex(mp_int *r, mp_int *s, const byte* hash,
if (err == MP_OKAY)
err = mp_copy(curve->Gy, mG->y);
if (err == MP_OKAY)
mp_set(mG->z, 1);
err = mp_set(mG->z, 1);
if (err == MP_OKAY)
err = mp_copy(key->pubkey.x, mQ->x);
@@ -3987,7 +3992,7 @@ int wc_ecc_import_point_der(byte* in, word32 inLen, const int curve_idx,
err = mp_read_unsigned_bin(point->y,
(byte*)in+1+((inLen-1)>>1), (inLen-1)>>1);
if (err == MP_OKAY)
mp_set(point->z, 1);
err = mp_set(point->z, 1);
if (err != MP_OKAY) {
mp_clear(point->x);
@@ -4211,8 +4216,9 @@ int wc_ecc_is_point(ecc_point* ecp, mp_int* a, mp_int* b, mp_int* prime)
#ifdef WOLFSSL_CUSTOM_CURVES
if (err == MP_OKAY) {
/* Use a and prime to determine if a == 3 */
mp_set(&t2, 0);
err = mp_submod(prime, a, prime, &t2);
err = mp_set(&t2, 0);
if (err == MP_OKAY)
err = mp_submod(prime, a, prime, &t2);
}
if (err == MP_OKAY && mp_cmp_d(&t2, 3) != MP_EQ) {
/* compute y^2 - x^3 + a*x */
@@ -4289,7 +4295,7 @@ static int ecc_check_privkey_gen(ecc_key* key, mp_int* a, mp_int* prime)
if (err == MP_OKAY)
err = mp_copy(curve->Gy, base->y);
if (err == MP_OKAY)
mp_set(base->z, 1);
err = mp_set(base->z, 1);
if (err == MP_OKAY) {
res = wc_ecc_new_point_h(key->heap);
@@ -4576,7 +4582,8 @@ int wc_ecc_import_x963_ex(const byte* in, word32 inLen, ecc_key* key,
else {
err = mp_submod(curve->prime, &t2, curve->prime, &t2);
}
mp_copy(&t2, key->pubkey.y);
if (err == MP_OKAY)
err = mp_copy(&t2, key->pubkey.y);
}
if (did_init) {
@@ -4594,7 +4601,7 @@ int wc_ecc_import_x963_ex(const byte* in, word32 inLen, ecc_key* key,
err = mp_read_unsigned_bin(key->pubkey.y, (byte*)in+1+((inLen-1)>>1),
(inLen-1)>>1);
if (err == MP_OKAY)
mp_set(key->pubkey.z, 1);
err = mp_set(key->pubkey.z, 1);
#ifdef WOLFSSL_VALIDATE_ECC_IMPORT
if (err == MP_OKAY)
@@ -4935,7 +4942,7 @@ static int wc_ecc_import_raw_private(ecc_key* key, const char* qx,
err = mp_read_radix(key->pubkey.y, qy, 16);
if (err == MP_OKAY)
mp_set(key->pubkey.z, 1);
err = mp_set(key->pubkey.z, 1);
/* import private key */
if (err == MP_OKAY) {
@@ -5886,10 +5893,14 @@ static int accel_fp_mul(int idx, mp_int* k, ecc_point *R, mp_int* a,
goto done;
}
} else {
mp_copy(k, &tk);
if ((err = mp_copy(k, &tk)) != MP_OKAY) {
goto done;
}
}
} else {
mp_copy(k, &tk);
if ((err = mp_copy(k, &tk)) != MP_OKAY) {
goto done;
}
}
/* get bitlen and round up to next multiple of FP_LUT */
@@ -6035,10 +6046,14 @@ static int accel_fp_mul2add(int idx1, int idx2,
goto done;
}
} else {
mp_copy(kA, &tka);
if ((err = mp_copy(kA, &tka)) != MP_OKAY) {
goto done;
}
}
} else {
mp_copy(kA, &tka);
if ((err = mp_copy(kA, &tka)) != MP_OKAY) {
goto done;
}
}
/* if it's smaller than modulus we fine */
@@ -6062,10 +6077,14 @@ static int accel_fp_mul2add(int idx1, int idx2,
goto done;
}
} else {
mp_copy(kB, &tkb);
if ((err = mp_copy(kB, &tkb)) != MP_OKAY) {
goto done;
}
}
} else {
mp_copy(kB, &tkb);
if ((err = mp_copy(kB, &tkb)) != MP_OKAY) {
goto done;
}
}
/* get bitlen and round up to next multiple of FP_LUT */
@@ -7176,8 +7195,7 @@ int mp_sqrtmod_prime(mp_int* n, mp_int* prime, mp_int* ret)
return MP_OKAY;
}
if (mp_cmp_d(n, 1) == MP_EQ) {
mp_set(ret, 1);
return MP_OKAY;
return mp_set(ret, 1);
}
/* prime must be odd */
@@ -7328,7 +7346,7 @@ int mp_sqrtmod_prime(mp_int* n, mp_int* prime, mp_int* ret)
/* M = i */
if (res == MP_OKAY)
mp_set(&M, i);
res = mp_set(&M, i);
}
}
}

View File

@@ -146,7 +146,7 @@ int mp_init (mp_int * a)
if (a == NULL)
return MP_VAL;
/* defer memory allocation */
/* defer allocation until mp_grow */
a->dp = NULL;
/* set the used to zero, allocated digits to the default precision
@@ -175,7 +175,7 @@ void mp_clear (mp_int * a)
}
/* free ram */
XFREE(a->dp, 0, DYNAMIC_TYPE_BIGINT);
XFREE(a->dp, NULL, DYNAMIC_TYPE_BIGINT);
/* reset members to make debugging easier */
a->dp = NULL;
@@ -195,7 +195,7 @@ void mp_forcezero(mp_int * a)
ForceZero(a->dp, a->used * sizeof(mp_digit));
/* free ram */
XFREE(a->dp, 0, DYNAMIC_TYPE_BIGINT);
XFREE(a->dp, NULL, DYNAMIC_TYPE_BIGINT);
/* reset members to make debugging easier */
a->dp = NULL;
@@ -317,7 +317,7 @@ int mp_copy (mp_int * a, mp_int * b)
}
/* grow dest */
if (b->alloc < a->used || b->alloc == 0) {
if (b->alloc < a->used || a->used == 0) {
if ((res = mp_grow (b, a->used)) != MP_OKAY) {
return res;
}
@@ -371,7 +371,7 @@ int mp_grow (mp_int * a, int size)
* to overwrite the dp member of a.
*/
tmp = OPT_CAST(mp_digit) XREALLOC (a->dp, sizeof (mp_digit) * size, NULL,
DYNAMIC_TYPE_BIGINT);
DYNAMIC_TYPE_BIGINT);
if (tmp == NULL) {
/* reallocation failed but "a" is still valid [can be freed] */
return MP_MEM;
@@ -939,7 +939,9 @@ int fast_mp_invmod (mp_int * a, mp_int * b, mp_int * c)
if ((res = mp_copy (&y, &v)) != MP_OKAY) {
goto LBL_ERR;
}
mp_set (&D, 1);
if ((res = mp_set (&D, 1)) != MP_OKAY) {
goto LBL_ERR;
}
top:
/* 4. while u is even do */
@@ -1093,8 +1095,12 @@ int mp_invmod_slow (mp_int * a, mp_int * b, mp_int * c)
if ((res = mp_copy (&y, &v)) != MP_OKAY) {
goto LBL_ERR;
}
mp_set (&A, 1);
mp_set (&D, 1);
if ((res = mp_set (&A, 1)) != MP_OKAY) {
goto LBL_ERR;
}
if ((res = mp_set (&D, 1)) != MP_OKAY) {
goto LBL_ERR;
}
top:
/* 4. while u is even do */
@@ -1299,12 +1305,16 @@ int mp_cmp_d(mp_int * a, mp_digit b)
/* set to a digit */
void mp_set (mp_int * a, mp_digit b)
int mp_set (mp_int * a, mp_digit b)
{
int res;
mp_zero (a);
mp_grow(a, 1);
a->dp[0] = (mp_digit)(b & MP_MASK);
a->used = (a->dp[0] != 0) ? 1 : 0;
res = mp_grow (a, 1);
if (res == MP_OKAY) {
a->dp[0] = (mp_digit)(b & MP_MASK);
a->used = (a->dp[0] != 0) ? 1 : 0;
}
return res;
}
/* chek if a bit is set */
@@ -1372,8 +1382,9 @@ int mp_div(mp_int * a, mp_int * b, mp_int * c, mp_int * d)
return res;
}
mp_set(&tq, 1);
if ((res = mp_set(&tq, 1)) != MP_OKAY) {
return res;
}
n = mp_count_bits(a) - mp_count_bits(b);
if (((res = mp_abs(a, &ta)) != MP_OKAY) ||
((res = mp_abs(b, &tb)) != MP_OKAY) ||
@@ -1931,7 +1942,9 @@ int mp_exptmod_fast (mp_int * G, mp_int * X, mp_int * P, mp_int * Y,
goto LBL_RES;
}
} else {
mp_set(&res, 1);
if ((err = mp_set(&res, 1)) != MP_OKAY) {
goto LBL_RES;
}
if ((err = mp_mod(G, P, &M[1])) != MP_OKAY) {
goto LBL_RES;
}
@@ -2159,7 +2172,7 @@ int fast_mp_montgomery_reduce (mp_int * x, mp_int * n, mp_digit rho)
}
#ifdef WOLFSSL_SMALL_STACK
W = (mp_word*)XMALLOC(sizeof(mp_word) * MP_WARRAY, 0, DYNAMIC_TYPE_BIGINT);
W = (mp_word*)XMALLOC(sizeof(mp_word) * MP_WARRAY, NULL, DYNAMIC_TYPE_BIGINT);
if (W == NULL)
return MP_MEM;
#endif
@@ -2286,7 +2299,7 @@ int fast_mp_montgomery_reduce (mp_int * x, mp_int * n, mp_digit rho)
mp_clamp (x);
#ifdef WOLFSSL_SMALL_STACK
XFREE(W, 0, DYNAMIC_TYPE_BIGINT);
XFREE(W, NULL, DYNAMIC_TYPE_BIGINT);
#endif
/* if A >= m then A = A - m */
@@ -2883,7 +2896,7 @@ int mp_init_size (mp_int * a, int size)
size += (MP_PREC * 2) - (size % MP_PREC);
/* alloc mem */
a->dp = OPT_CAST(mp_digit) XMALLOC (sizeof (mp_digit) * size, 0,
a->dp = OPT_CAST(mp_digit) XMALLOC (sizeof (mp_digit) * size, NULL,
DYNAMIC_TYPE_BIGINT);
if (a->dp == NULL) {
return MP_MEM;
@@ -2936,7 +2949,7 @@ int fast_s_mp_sqr (mp_int * a, mp_int * b)
return MP_RANGE; /* TAO range check */
#ifdef WOLFSSL_SMALL_STACK
W = (mp_digit*)XMALLOC(sizeof(mp_digit) * MP_WARRAY, 0, DYNAMIC_TYPE_BIGINT);
W = (mp_digit*)XMALLOC(sizeof(mp_digit) * MP_WARRAY, NULL, DYNAMIC_TYPE_BIGINT);
if (W == NULL)
return MP_MEM;
#endif
@@ -3009,7 +3022,7 @@ int fast_s_mp_sqr (mp_int * a, mp_int * b)
mp_clamp (b);
#ifdef WOLFSSL_SMALL_STACK
XFREE(W, 0, DYNAMIC_TYPE_BIGINT);
XFREE(W, NULL, DYNAMIC_TYPE_BIGINT);
#endif
return MP_OKAY;
@@ -3055,7 +3068,7 @@ int fast_s_mp_mul_digs (mp_int * a, mp_int * b, mp_int * c, int digs)
return MP_RANGE; /* TAO range check */
#ifdef WOLFSSL_SMALL_STACK
W = (mp_digit*)XMALLOC(sizeof(mp_digit) * MP_WARRAY, 0, DYNAMIC_TYPE_BIGINT);
W = (mp_digit*)XMALLOC(sizeof(mp_digit) * MP_WARRAY, NULL, DYNAMIC_TYPE_BIGINT);
if (W == NULL)
return MP_MEM;
#endif
@@ -3113,7 +3126,7 @@ int fast_s_mp_mul_digs (mp_int * a, mp_int * b, mp_int * c, int digs)
mp_clamp (c);
#ifdef WOLFSSL_SMALL_STACK
XFREE(W, 0, DYNAMIC_TYPE_BIGINT);
XFREE(W, NULL, DYNAMIC_TYPE_BIGINT);
#endif
return MP_OKAY;
@@ -3273,7 +3286,9 @@ int mp_montgomery_calc_normalization (mp_int * a, mp_int * b)
return res;
}
} else {
mp_set(a, 1);
if ((res = mp_set(a, 1)) != MP_OKAY) {
return res;
}
bits = 1;
}
@@ -3412,7 +3427,9 @@ int s_mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode)
if ((err = mp_init (&res)) != MP_OKAY) {
goto LBL_MU;
}
mp_set (&res, 1);
if ((err = mp_set (&res, 1)) != MP_OKAY) {
goto LBL_MU;
}
/* set initial mode and bit cnt */
mode = 0;
@@ -3599,7 +3616,8 @@ int mp_reduce (mp_int * x, mp_int * m, mp_int * mu)
/* If x < 0, add b**(k+1) to it */
if (mp_cmp_d (x, 0) == MP_LT) {
mp_set (&q, 1);
if ((res = mp_set (&q, 1)) != MP_OKAY)
goto CLEANUP;
if ((res = mp_lshd (&q, um + 1)) != MP_OKAY)
goto CLEANUP;
if ((res = mp_add (x, &q, x)) != MP_OKAY)
@@ -3777,7 +3795,7 @@ int fast_s_mp_mul_high_digs (mp_int * a, mp_int * b, mp_int * c, int digs)
return MP_RANGE; /* TAO range check */
#ifdef WOLFSSL_SMALL_STACK
W = (mp_digit*)XMALLOC(sizeof(mp_digit) * MP_WARRAY, 0, DYNAMIC_TYPE_BIGINT);
W = (mp_digit*)XMALLOC(sizeof(mp_digit) * MP_WARRAY, NULL, DYNAMIC_TYPE_BIGINT);
if (W == NULL)
return MP_MEM;
#endif
@@ -3835,7 +3853,7 @@ int fast_s_mp_mul_high_digs (mp_int * a, mp_int * b, mp_int * c, int digs)
mp_clamp (c);
#ifdef WOLFSSL_SMALL_STACK
XFREE(W, 0, DYNAMIC_TYPE_BIGINT);
XFREE(W, NULL, DYNAMIC_TYPE_BIGINT);
#endif
return MP_OKAY;
@@ -4466,7 +4484,9 @@ int mp_prime_is_prime (mp_int * a, int t, int *result)
for (ix = 0; ix < t; ix++) {
/* set the prime */
mp_set (&b, ltm_prime_tab[ix]);
if ((err = mp_set (&b, ltm_prime_tab[ix])) != MP_OKAY) {
goto LBL_B;
}
if ((err = mp_prime_miller_rabin (a, &b, &res)) != MP_OKAY) {
goto LBL_B;