forked from wolfSSL/wolfssl
merge conflict
This commit is contained in:
@@ -45,7 +45,23 @@
|
||||
#endif
|
||||
#endif
|
||||
|
||||
static void bn_reverse (unsigned char *s, int len);
|
||||
/* reverse an array, used for radix code */
|
||||
static void
|
||||
bn_reverse (unsigned char *s, int len)
|
||||
{
|
||||
int ix, iy;
|
||||
unsigned char t;
|
||||
|
||||
ix = 0;
|
||||
iy = len - 1;
|
||||
while (ix < iy) {
|
||||
t = s[ix];
|
||||
s[ix] = s[iy];
|
||||
s[iy] = t;
|
||||
++ix;
|
||||
--iy;
|
||||
}
|
||||
}
|
||||
|
||||
/* math settings check */
|
||||
word32 CheckRunTimeSettings(void)
|
||||
@@ -327,25 +343,6 @@ int mp_grow (mp_int * a, int size)
|
||||
}
|
||||
|
||||
|
||||
/* reverse an array, used for radix code */
|
||||
void
|
||||
bn_reverse (unsigned char *s, int len)
|
||||
{
|
||||
int ix, iy;
|
||||
unsigned char t;
|
||||
|
||||
ix = 0;
|
||||
iy = len - 1;
|
||||
while (ix < iy) {
|
||||
t = s[ix];
|
||||
s[ix] = s[iy];
|
||||
s[iy] = t;
|
||||
++ix;
|
||||
--iy;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* shift right by a certain bit count (store quotient in c, optional
|
||||
remainder in d) */
|
||||
int mp_div_2d (mp_int * a, int b, mp_int * c, mp_int * d)
|
||||
@@ -1251,6 +1248,14 @@ void mp_set (mp_int * a, mp_digit b)
|
||||
a->used = (a->dp[0] != 0) ? 1 : 0;
|
||||
}
|
||||
|
||||
/* chek if a bit is set */
|
||||
int mp_is_bit_set (mp_int *a, mp_digit b)
|
||||
{
|
||||
if ((mp_digit)a->used < b/DIGIT_BIT)
|
||||
return 0;
|
||||
|
||||
return (int)((a->dp[b/DIGIT_BIT] >> b%DIGIT_BIT) & (mp_digit)1);
|
||||
}
|
||||
|
||||
/* c = a mod b, 0 <= c < b */
|
||||
int
|
||||
@@ -2514,6 +2519,25 @@ mp_2expt (mp_int * a, int b)
|
||||
return MP_OKAY;
|
||||
}
|
||||
|
||||
/* set the b bit of a */
|
||||
int
|
||||
mp_set_bit (mp_int * a, int b)
|
||||
{
|
||||
int i = b / DIGIT_BIT, res;
|
||||
|
||||
/* grow a to accomodate the single bit */
|
||||
if ((res = mp_grow (a, i + 1)) != MP_OKAY) {
|
||||
return res;
|
||||
}
|
||||
|
||||
/* set the used count of where the bit will go */
|
||||
a->used = i + 1;
|
||||
|
||||
/* put the single bit in its place */
|
||||
a->dp[i] |= ((mp_digit)1) << (b % DIGIT_BIT);
|
||||
|
||||
return MP_OKAY;
|
||||
}
|
||||
|
||||
/* multiply by a digit */
|
||||
int
|
||||
@@ -3961,7 +3985,7 @@ int mp_sub_d (mp_int * a, mp_digit b, mp_int * c)
|
||||
#endif /* defined(HAVE_ECC) || !defined(NO_PWDBASED) */
|
||||
|
||||
|
||||
#if defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)
|
||||
#if defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY) || defined(HAVE_ECC)
|
||||
|
||||
static const int lnz[16] = {
|
||||
4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0
|
||||
@@ -4096,7 +4120,7 @@ int mp_mod_d (mp_int * a, mp_digit b, mp_digit * c)
|
||||
return mp_div_d(a, b, NULL, c);
|
||||
}
|
||||
|
||||
#endif /* defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY) */
|
||||
#endif /* defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY) || defined(HAVE_ECC) */
|
||||
|
||||
#ifdef WOLFSSL_KEY_GEN
|
||||
|
||||
@@ -4514,6 +4538,115 @@ int mp_read_radix (mp_int * a, const char *str, int radix)
|
||||
return MP_OKAY;
|
||||
}
|
||||
|
||||
/* returns size of ASCII representation */
|
||||
int mp_radix_size (mp_int *a, int radix, int *size)
|
||||
{
|
||||
int res, digs;
|
||||
mp_int t;
|
||||
mp_digit d;
|
||||
|
||||
*size = 0;
|
||||
|
||||
/* special case for binary */
|
||||
if (radix == 2) {
|
||||
*size = mp_count_bits (a) + (a->sign == MP_NEG ? 1 : 0) + 1;
|
||||
return MP_OKAY;
|
||||
}
|
||||
|
||||
/* make sure the radix is in range */
|
||||
if (radix < 2 || radix > 64) {
|
||||
return MP_VAL;
|
||||
}
|
||||
|
||||
if (mp_iszero(a) == MP_YES) {
|
||||
*size = 2;
|
||||
return MP_OKAY;
|
||||
}
|
||||
|
||||
/* digs is the digit count */
|
||||
digs = 0;
|
||||
|
||||
/* if it's negative add one for the sign */
|
||||
if (a->sign == MP_NEG) {
|
||||
++digs;
|
||||
}
|
||||
|
||||
/* init a copy of the input */
|
||||
if ((res = mp_init_copy (&t, a)) != MP_OKAY) {
|
||||
return res;
|
||||
}
|
||||
|
||||
/* force temp to positive */
|
||||
t.sign = MP_ZPOS;
|
||||
|
||||
/* fetch out all of the digits */
|
||||
while (mp_iszero (&t) == MP_NO) {
|
||||
if ((res = mp_div_d (&t, (mp_digit) radix, &t, &d)) != MP_OKAY) {
|
||||
mp_clear (&t);
|
||||
return res;
|
||||
}
|
||||
++digs;
|
||||
}
|
||||
mp_clear (&t);
|
||||
|
||||
/* return digs + 1, the 1 is for the NULL byte that would be required. */
|
||||
*size = digs + 1;
|
||||
return MP_OKAY;
|
||||
}
|
||||
|
||||
/* stores a bignum as a ASCII string in a given radix (2..64) */
|
||||
int mp_toradix (mp_int *a, char *str, int radix)
|
||||
{
|
||||
int res, digs;
|
||||
mp_int t;
|
||||
mp_digit d;
|
||||
char *_s = str;
|
||||
|
||||
/* check range of the radix */
|
||||
if (radix < 2 || radix > 64) {
|
||||
return MP_VAL;
|
||||
}
|
||||
|
||||
/* quick out if its zero */
|
||||
if (mp_iszero(a) == 1) {
|
||||
*str++ = '0';
|
||||
*str = '\0';
|
||||
return MP_OKAY;
|
||||
}
|
||||
|
||||
if ((res = mp_init_copy (&t, a)) != MP_OKAY) {
|
||||
return res;
|
||||
}
|
||||
|
||||
/* if it is negative output a - */
|
||||
if (t.sign == MP_NEG) {
|
||||
++_s;
|
||||
*str++ = '-';
|
||||
t.sign = MP_ZPOS;
|
||||
}
|
||||
|
||||
digs = 0;
|
||||
while (mp_iszero (&t) == 0) {
|
||||
if ((res = mp_div_d (&t, (mp_digit) radix, &t, &d)) != MP_OKAY) {
|
||||
mp_clear (&t);
|
||||
return res;
|
||||
}
|
||||
*str++ = mp_s_rmap[d];
|
||||
++digs;
|
||||
}
|
||||
|
||||
/* reverse the digits of the string. In this case _s points
|
||||
* to the first digit [exluding the sign] of the number]
|
||||
*/
|
||||
bn_reverse ((unsigned char *)_s, digs);
|
||||
|
||||
/* append a NULL so the string is properly terminated */
|
||||
*str = '\0';
|
||||
|
||||
mp_clear (&t);
|
||||
return MP_OKAY;
|
||||
}
|
||||
|
||||
#endif /* HAVE_ECC */
|
||||
|
||||
#endif /* USE_FAST_MATH */
|
||||
|
||||
Reference in New Issue
Block a user