Fix TFM mp_set_int to handle long. Enhance mp_set_int to use mp_set if less than max mp_digit. Added new MP_SET_CHUNK_BITS to eliminate hard coded const’s and allow build time adjustment.

This commit is contained in:
David Garske
2017-03-15 12:23:50 -07:00
parent 4725a8aea6
commit 5a24fd9237
4 changed files with 57 additions and 14 deletions

View File

@ -3903,25 +3903,32 @@ int fast_s_mp_mul_high_digs (mp_int * a, mp_int * b, mp_int * c, int digs)
} }
/* set a 32-bit const */ #ifndef MP_SET_CHUNK_BITS
#define MP_SET_CHUNK_BITS 4
#endif
int mp_set_int (mp_int * a, unsigned long b) int mp_set_int (mp_int * a, unsigned long b)
{ {
int x, res; int x, res;
/* use direct mp_set if b is less than mp_digit max */
if (b < MP_DIGIT_MAX) {
return mp_set (a, b);
}
mp_zero (a); mp_zero (a);
/* set four bits at a time */ /* set chunk bits at a time */
for (x = 0; x < 8; x++) { for (x = 0; x < (int)(sizeof(long) * 8) / MP_SET_CHUNK_BITS; x++) {
/* shift the number up four bits */ /* shift the number up chunk bits */
if ((res = mp_mul_2d (a, 4, a)) != MP_OKAY) { if ((res = mp_mul_2d (a, MP_SET_CHUNK_BITS, a)) != MP_OKAY) {
return res; return res;
} }
/* OR in the top four bits of the source */ /* OR in the top bits of the source */
a->dp[0] |= (b >> 28) & 15; a->dp[0] |= (b >> (32 - MP_SET_CHUNK_BITS)) & ((1 << MP_SET_CHUNK_BITS) - 1);
/* shift the source up to the next four bits */ /* shift the source up to the next chunk bits */
b <<= 4; b <<= MP_SET_CHUNK_BITS;
/* ensure that digits are not clamped off */ /* ensure that digits are not clamped off */
a->used += 1; a->used += 1;

View File

@ -1484,7 +1484,7 @@ int wc_MakeRsaKey(RsaKey* key, int size, long e, WC_RNG* rng)
if ((err = mp_init_multi(&p, &q, &tmp1, &tmp2, &tmp3, NULL)) != MP_OKAY) if ((err = mp_init_multi(&p, &q, &tmp1, &tmp2, &tmp3, NULL)) != MP_OKAY)
return err; return err;
err = mp_set_int(&tmp3, (mp_digit)e); err = mp_set_int(&tmp3, e);
/* make p */ /* make p */
if (err == MP_OKAY) { if (err == MP_OKAY) {

View File

@ -1963,6 +1963,40 @@ void fp_set(fp_int *a, fp_digit b)
a->used = a->dp[0] ? 1 : 0; a->used = a->dp[0] ? 1 : 0;
} }
#ifndef MP_SET_CHUNK_BITS
#define MP_SET_CHUNK_BITS 4
#endif
void fp_set_int(fp_int *a, unsigned long b)
{
int x;
/* use direct fp_set if b is less than fp_digit max */
if (b < FP_DIGIT_MAX) {
fp_set (a, b);
return;
}
fp_zero (a);
/* set chunk bits at a time */
for (x = 0; x < (int)(sizeof(long) * 8) / MP_SET_CHUNK_BITS; x++) {
fp_mul_2d (a, MP_SET_CHUNK_BITS, a);
/* OR in the top bits of the source */
a->dp[0] |= (b >> (32 - MP_SET_CHUNK_BITS)) & ((1 << MP_SET_CHUNK_BITS) - 1);
/* shift the source up to the next chunk bits */
b <<= MP_SET_CHUNK_BITS;
/* ensure that digits are not clamped off */
a->used += 1;
}
/* clamp digits */
fp_clamp(a);
}
/* check if a bit is set */ /* check if a bit is set */
int fp_is_bit_set (fp_int *a, fp_digit b) int fp_is_bit_set (fp_int *a, fp_digit b)
{ {
@ -2440,9 +2474,9 @@ void mp_rshd (mp_int* a, int x)
fp_rshd(a, x); fp_rshd(a, x);
} }
int mp_set_int(mp_int *a, mp_digit b) int mp_set_int(mp_int *a, unsigned long b)
{ {
fp_set(a, b); fp_set_int(a, b);
return MP_OKAY; return MP_OKAY;
} }

View File

@ -260,6 +260,7 @@
#endif #endif
#define FP_MASK (fp_digit)(-1) #define FP_MASK (fp_digit)(-1)
#define FP_DIGIT_MAX FP_MASK
#define FP_SIZE (FP_MAX_SIZE/DIGIT_BIT) #define FP_SIZE (FP_MAX_SIZE/DIGIT_BIT)
/* signs */ /* signs */
@ -382,6 +383,7 @@ void fp_clear(fp_int *a); /* uses ForceZero to clear sensitive memory */
/* set to a small digit */ /* set to a small digit */
void fp_set(fp_int *a, fp_digit b); void fp_set(fp_int *a, fp_digit b);
void fp_set_int(fp_int *a, unsigned long b);
/* check if a bit is set */ /* check if a bit is set */
int fp_is_bit_set(fp_int *a, fp_digit b); int fp_is_bit_set(fp_int *a, fp_digit b);
@ -650,7 +652,7 @@ int mp_isodd(mp_int* a);
int mp_iszero(mp_int* a); int mp_iszero(mp_int* a);
int mp_count_bits(mp_int *a); int mp_count_bits(mp_int *a);
int mp_leading_bit(mp_int *a); int mp_leading_bit(mp_int *a);
int mp_set_int(mp_int *a, mp_digit b); int mp_set_int(mp_int *a, unsigned long b);
int mp_is_bit_set (mp_int * a, mp_digit b); int mp_is_bit_set (mp_int * a, mp_digit b);
int mp_set_bit (mp_int * a, mp_digit b); int mp_set_bit (mp_int * a, mp_digit b);
void mp_rshb(mp_int *a, int x); void mp_rshb(mp_int *a, int x);