SP math all: sp_exch fixed up

This commit is contained in:
Sean Parkinson
2021-02-15 10:29:45 +10:00
parent 505514415d
commit e4f8545e36
3 changed files with 18 additions and 1 deletions

View File

@ -2456,7 +2456,12 @@ int wc_DhGenerateParams(WC_RNG *rng, int modSz, DhKey *dh)
if (ret == 0) {
/* at this point tmp generates a group of order q mod p */
#ifndef USE_FAST_MATH
/* Exchanging is quick when the data pointer can be copied. */
mp_exch(&tmp, &dh->g);
#else
mp_copy(&tmp, &dh->g);
#endif
}
/* clear the parameters if there was an error */

View File

@ -412,7 +412,12 @@ int wc_MakeDsaParameters(WC_RNG *rng, int modulus_size, DsaKey *dsa)
} while (mp_cmp_d(&tmp, 1) == MP_EQ);
/* at this point tmp generates a group of order q mod p */
#ifndef USE_FAST_MATH
/* Exchanging is quick when the data pointer can be copied. */
mp_exch(&tmp, &dsa->g);
#else
mp_copy(&tmp, &dsa->g);
#endif
mp_clear(&tmp);
mp_clear(&tmp2);

View File

@ -2425,12 +2425,19 @@ int sp_exch(sp_int* a, sp_int* b)
if ((a == NULL) || (b == NULL)) {
err = MP_VAL;
}
if ((err == MP_OKAY) && ((a->size < b->used) || (b->size < a->used))) {
err = MP_VAL;
}
ALLOC_SP_INT(t, a->used, err, NULL);
if (err == MP_OKAY) {
int asize = a->size;
int bsize = b->size;
XMEMCPY(t, a, MP_INT_SIZEOF(a->used));
XMEMCPY(a, b, MP_INT_SIZEOF(b->used));
XMEMCPY(b, t, MP_INT_SIZEOF(a->used));
XMEMCPY(b, t, MP_INT_SIZEOF(t->used));
a->size = asize;
b->size = bsize;
}
FREE_SP_INT(t, NULL);