wolfcrypt/src/sp_int.c: address peer review around _sp_zero(), sp_init(), and sp_init_size(), re sp_int_minimal.

This commit is contained in:
Daniel Pouzzner
2022-09-15 14:33:45 -05:00
parent 5d2610c96d
commit d18a654f74

View File

@ -4352,13 +4352,45 @@ static int _sp_mont_red(sp_int* a, sp_int* m, sp_int_digit mp);
*/ */
static void _sp_zero(sp_int* a) static void _sp_zero(sp_int* a)
{ {
((sp_int_minimal *)a)->used = 0; sp_int_minimal* am = (sp_int_minimal *)a;
((sp_int_minimal *)a)->dp[0] = 0; am->used = 0;
am->dp[0] = 0;
#ifdef WOLFSSL_SP_INT_NEGATIVE #ifdef WOLFSSL_SP_INT_NEGATIVE
((sp_int_minimal *)a)->sign = MP_ZPOS; am->sign = MP_ZPOS;
#endif #endif
} }
/* Initialize the multi-precision number to be zero with a given max size.
*
* @param [out] a SP integer.
* @param [in] size Number of words to say are available.
*
* @return MP_OKAY on success.
* @return MP_VAL when a is NULL.
*/
int sp_init_size(sp_int* a, int size)
{
sp_int_minimal* am = (sp_int_minimal *)a;
int err = MP_OKAY;
if (a == NULL) {
err = MP_VAL;
}
if (err == MP_OKAY) {
#ifdef HAVE_WOLF_BIGINT
wc_bigint_init(&am->raw);
#endif
_sp_zero(a);
}
if (err == MP_OKAY) {
am->size = size;
}
return err;
}
/* Initialize the multi-precision number to be zero. /* Initialize the multi-precision number to be zero.
* *
* @param [out] a SP integer. * @param [out] a SP integer.
@ -4368,49 +4400,7 @@ static void _sp_zero(sp_int* a)
*/ */
int sp_init(sp_int* a) int sp_init(sp_int* a)
{ {
int err = MP_OKAY; return sp_init_size(a, SP_INT_DIGITS);
if (a == NULL) {
err = MP_VAL;
}
if (err == MP_OKAY) {
#ifdef HAVE_WOLF_BIGINT
wc_bigint_init(&a->raw);
#endif
_sp_zero(a);
a->size = SP_INT_DIGITS;
}
return err;
}
/* Initialize the multi-precision number to be zero and have a maximum size.
*
* @param [out] a SP integer.
* @param [in] size Number of words to say are available.
*
* @return MP_OKAY on success.
* @return MP_VAL when a is NULL.
*/
int sp_init_size(sp_int* a, int size)
{
int err = MP_OKAY;
if (a == NULL) {
err = MP_VAL;
}
if (err == MP_OKAY) {
#ifdef HAVE_WOLF_BIGINT
wc_bigint_init(&a->raw);
#endif
_sp_zero(a);
}
if (err == MP_OKAY) {
((sp_int_minimal *)a)->size = size;
}
return err;
} }
#if !defined(WOLFSSL_RSA_PUBLIC_ONLY) || !defined(NO_DH) || defined(HAVE_ECC) #if !defined(WOLFSSL_RSA_PUBLIC_ONLY) || !defined(NO_DH) || defined(HAVE_ECC)