forked from wolfSSL/wolfssl
Adds TFM_MIPS
(tested on PIC32MZ2048EFM144 with XC32 v1.44). Adds check for mp_read_radix
to return failure for invalid character. Adds checking for s_mp_sub
response code. Improved mp_init_copy
error handling. Improved init to use size where applicable. Asm PPC 32 changes to add r
to register. Asm formatting cleanups.
This commit is contained in:
@ -156,7 +156,7 @@ __asm__( \
|
||||
#define MONT_FINI
|
||||
#define LOOP_END
|
||||
#define LOOP_START \
|
||||
mu = c[x] * mp;
|
||||
mu = c[x] * mp
|
||||
|
||||
#define INNERMUL \
|
||||
__asm__( \
|
||||
@ -333,7 +333,7 @@ __asm__( \
|
||||
\
|
||||
:"=r"(_c), "=r"(cy) \
|
||||
: "0"(_c), "1"(cy), "g"(mu), "r"(tmpm)\
|
||||
: "%rax", "%rdx", "%r10", "%r11", "cc")\
|
||||
: "%rax", "%rdx", "%r10", "%r11", "cc")
|
||||
|
||||
#define PROPCARRY \
|
||||
__asm__( \
|
||||
@ -560,24 +560,24 @@ __asm__( \
|
||||
|
||||
#define INNERMUL \
|
||||
__asm__( \
|
||||
" mulld 16,%3,%4 \n\t" \
|
||||
" mulhdu 17,%3,%4 \n\t" \
|
||||
" addc 16,16,%0 \n\t" \
|
||||
" addze 17,17 \n\t" \
|
||||
" ldx 18,0,%1 \n\t" \
|
||||
" addc 16,16,18 \n\t" \
|
||||
" addze %0,17 \n\t" \
|
||||
" sdx 16,0,%1 \n\t" \
|
||||
:"=r"(cy),"=m"(_c[0]):"0"(cy),"r"(mu),"r"(tmpm[0]),"1"(_c[0]):"16", "17", "18","cc"); ++tmpm;
|
||||
" mulld r16,%3,%4 \n\t" \
|
||||
" mulhdu r17,%3,%4 \n\t" \
|
||||
" addc r16,16,%0 \n\t" \
|
||||
" addze r17,r17 \n\t" \
|
||||
" ldx r18,0,%1 \n\t" \
|
||||
" addc r16,r16,r18 \n\t" \
|
||||
" addze %0,r17 \n\t" \
|
||||
" sdx r16,0,%1 \n\t" \
|
||||
:"=r"(cy),"=m"(_c[0]):"0"(cy),"r"(mu),"r"(tmpm[0]),"1"(_c[0]):"r16", "r17", "r18","cc"); ++tmpm;
|
||||
|
||||
#define PROPCARRY \
|
||||
__asm__( \
|
||||
" ldx 16,0,%1 \n\t" \
|
||||
" addc 16,16,%0 \n\t" \
|
||||
" sdx 16,0,%1 \n\t" \
|
||||
" ldx r16,0,%1 \n\t" \
|
||||
" addc r16,r16,%0 \n\t" \
|
||||
" sdx r16,0,%1 \n\t" \
|
||||
" xor %0,%0,%0 \n\t" \
|
||||
" addze %0,%0 \n\t" \
|
||||
:"=r"(cy),"=m"(_c[0]):"0"(cy),"1"(_c[0]):"16","cc");
|
||||
:"=r"(cy),"=m"(_c[0]):"0"(cy),"1"(_c[0]):"r16","cc");
|
||||
|
||||
/******************************************************************/
|
||||
|
||||
@ -610,6 +610,40 @@ __asm__( \
|
||||
" acr %0 \n\t" \
|
||||
:"=r"(cy),"=r"(&_c[0]):"0"(cy),"1"(&_c[0]):"r2","cc");
|
||||
|
||||
/******************************************************************/
|
||||
#elif defined(TFM_MIPS)
|
||||
|
||||
/* MIPS */
|
||||
#define MONT_START
|
||||
#define MONT_FINI
|
||||
#define LOOP_END
|
||||
#define LOOP_START \
|
||||
mu = c[x] * mp
|
||||
|
||||
#define INNERMUL \
|
||||
__asm__( \
|
||||
" multu %3,%4 \n\t" \
|
||||
" mflo $12 \n\t" \
|
||||
" mfhi $13 \n\t" \
|
||||
" addu $12,$12,%0 \n\t" \
|
||||
" sltu $10,$12,%0 \n\t" \
|
||||
" addu $13,$13,$10 \n\t" \
|
||||
" lw $10,%1 \n\t" \
|
||||
" addu $12,$12,$10 \n\t" \
|
||||
" sltu $10,$12,$10 \n\t" \
|
||||
" addu %0,$13,$10 \n\t" \
|
||||
" sw $12,%1 \n\t" \
|
||||
:"+r"(cy),"+m"(_c[0]):""(cy),"r"(mu),"r"(tmpm[0]),""(_c[0]):"$10","$12","$13"); ++tmpm;
|
||||
|
||||
#define PROPCARRY \
|
||||
__asm__( \
|
||||
" lw $10,%1 \n\t" \
|
||||
" addu $10,$10,%0 \n\t" \
|
||||
" sw $10,%1 \n\t" \
|
||||
" sltu %0,$10,%0 \n\t" \
|
||||
:"+r"(cy),"+m"(_c[0]):""(cy),""(_c[0]):"$10");
|
||||
|
||||
/******************************************************************/
|
||||
#else
|
||||
|
||||
/* ISO C code */
|
||||
@ -689,8 +723,6 @@ __asm__( \
|
||||
"xorl %2,%2 \n\t" \
|
||||
:"=r"(sc0), "=r"(sc1), "=r"(sc2): "g"(i), "g"(j) :"%eax","%edx","cc");
|
||||
|
||||
/* TAO removed sc0,1,2 as input to remove warning so %6,%7 become %3,%4 */
|
||||
|
||||
#define SQRADDAC(i, j) \
|
||||
__asm__( \
|
||||
"movl %6,%%eax \n\t" \
|
||||
@ -759,8 +791,6 @@ __asm__( \
|
||||
"xorq %2,%2 \n\t" \
|
||||
:"=r"(sc0), "=r"(sc1), "=r"(sc2): "g"(i), "g"(j) :"%rax","%rdx","cc");
|
||||
|
||||
/* TAO removed sc0,1,2 as input to remove warning so %6,%7 become %3,%4 */
|
||||
|
||||
#define SQRADDAC(i, j) \
|
||||
__asm__( \
|
||||
"movq %6,%%rax \n\t" \
|
||||
@ -1022,25 +1052,25 @@ __asm__( \
|
||||
/* multiplies point i and j, updates carry "c1" and digit c2 */
|
||||
#define SQRADD(i, j) \
|
||||
__asm__( \
|
||||
" mulld 16,%6,%6 \n\t" \
|
||||
" addc %0,%0,16 \n\t" \
|
||||
" mulhdu 16,%6,%6 \n\t" \
|
||||
" adde %1,%1,16 \n\t" \
|
||||
" mulld r16,%6,%6 \n\t" \
|
||||
" addc %0,%0,r16 \n\t" \
|
||||
" mulhdu r16,%6,%6 \n\t" \
|
||||
" adde %1,%1,r16 \n\t" \
|
||||
" addze %2,%2 \n\t" \
|
||||
:"=r"(c0), "=r"(c1), "=r"(c2):"0"(c0), "1"(c1), "2"(c2), "r"(i):"16","cc");
|
||||
:"=r"(c0), "=r"(c1), "=r"(c2):"0"(c0), "1"(c1), "2"(c2), "r"(i):"r16","cc");
|
||||
|
||||
/* for squaring some of the terms are doubled... */
|
||||
#define SQRADD2(i, j) \
|
||||
__asm__( \
|
||||
" mulld 16,%6,%7 \n\t" \
|
||||
" mulhdu 17,%6,%7 \n\t" \
|
||||
" addc %0,%0,16 \n\t" \
|
||||
" adde %1,%1,17 \n\t" \
|
||||
" mulld r16,%6,%7 \n\t" \
|
||||
" mulhdu r17,%6,%7 \n\t" \
|
||||
" addc %0,%0,r16 \n\t" \
|
||||
" adde %1,%1,r17 \n\t" \
|
||||
" addze %2,%2 \n\t" \
|
||||
" addc %0,%0,16 \n\t" \
|
||||
" adde %1,%1,17 \n\t" \
|
||||
" addc %0,%0,r16 \n\t" \
|
||||
" adde %1,%1,r17 \n\t" \
|
||||
" addze %2,%2 \n\t" \
|
||||
:"=r"(c0), "=r"(c1), "=r"(c2):"0"(c0), "1"(c1), "2"(c2), "r"(i), "r"(j):"16", "17","cc");
|
||||
:"=r"(c0), "=r"(c1), "=r"(c2):"0"(c0), "1"(c1), "2"(c2), "r"(i), "r"(j):"r16", "r17","cc");
|
||||
|
||||
#define SQRADDSC(i, j) \
|
||||
__asm__( \
|
||||
@ -1051,12 +1081,12 @@ __asm__( \
|
||||
|
||||
#define SQRADDAC(i, j) \
|
||||
__asm__( \
|
||||
" mulld 16,%6,%7 \n\t" \
|
||||
" addc %0,%0,16 \n\t" \
|
||||
" mulhdu 16,%6,%7 \n\t" \
|
||||
" adde %1,%1,16 \n\t" \
|
||||
" mulld r16,%6,%7 \n\t" \
|
||||
" addc %0,%0,r16 \n\t" \
|
||||
" mulhdu r16,%6,%7 \n\t" \
|
||||
" adde %1,%1,r16 \n\t" \
|
||||
" addze %2,%2 \n\t" \
|
||||
:"=r"(sc0), "=r"(sc1), "=r"(sc2):"0"(sc0), "1"(sc1), "2"(sc2), "r"(i), "r"(j):"16", "cc");
|
||||
:"=r"(sc0), "=r"(sc1), "=r"(sc2):"0"(sc0), "1"(sc1), "2"(sc2), "r"(i), "r"(j):"r16", "cc");
|
||||
|
||||
#define SQRADDDB \
|
||||
__asm__( \
|
||||
@ -1136,6 +1166,112 @@ __asm__( \
|
||||
" adc %2,%2,%5 \n\t" \
|
||||
:"=r"(c0), "=r"(c1), "=r"(c2) : "r"(sc0), "r"(sc1), "r"(sc2), "0"(c0), "1"(c1), "2"(c2) : "cc");
|
||||
|
||||
#elif defined(TFM_MIPS)
|
||||
|
||||
/* MIPS */
|
||||
#define COMBA_START
|
||||
|
||||
#define CLEAR_CARRY \
|
||||
c0 = c1 = c2 = 0;
|
||||
|
||||
#define COMBA_STORE(x) \
|
||||
x = c0;
|
||||
|
||||
#define COMBA_STORE2(x) \
|
||||
x = c1;
|
||||
|
||||
#define CARRY_FORWARD \
|
||||
do { c0 = c1; c1 = c2; c2 = 0; } while (0);
|
||||
|
||||
#define COMBA_FINI
|
||||
|
||||
/* multiplies point i and j, updates carry "c1" and digit c2 */
|
||||
#define SQRADD(i, j) \
|
||||
__asm__( \
|
||||
" multu %6,%6 \n\t" \
|
||||
" mflo $12 \n\t" \
|
||||
" mfhi $13 \n\t" \
|
||||
" addu %0,%0,$12 \n\t" \
|
||||
" sltu $12,%0,$12 \n\t" \
|
||||
" addu %1,%1,$13 \n\t" \
|
||||
" sltu $13,%1,$13 \n\t" \
|
||||
" addu %1,%1,$12 \n\t" \
|
||||
" sltu $12,%1,$12 \n\t" \
|
||||
" addu %2,%2,$13 \n\t" \
|
||||
" addu %2,%2,$12 \n\t" \
|
||||
:"=r"(c0), "=r"(c1), "=r"(c2):"0"(c0), "1"(c1), "2"(c2), "r"(i):"$12","$13");
|
||||
|
||||
/* for squaring some of the terms are doubled... */
|
||||
#define SQRADD2(i, j) \
|
||||
__asm__( \
|
||||
" multu %6,%7 \n\t" \
|
||||
" mflo $12 \n\t" \
|
||||
" mfhi $13 \n\t" \
|
||||
\
|
||||
" addu %0,%0,$12 \n\t" \
|
||||
" sltu $14,%0,$12 \n\t" \
|
||||
" addu %1,%1,$13 \n\t" \
|
||||
" sltu $15,%1,$13 \n\t" \
|
||||
" addu %1,%1,$14 \n\t" \
|
||||
" sltu $14,%1,$14 \n\t" \
|
||||
" addu %2,%2,$15 \n\t" \
|
||||
" addu %2,%2,$14 \n\t" \
|
||||
\
|
||||
" addu %0,%0,$12 \n\t" \
|
||||
" sltu $14,%0,$12 \n\t" \
|
||||
" addu %1,%1,$13 \n\t" \
|
||||
" sltu $15,%1,$13 \n\t" \
|
||||
" addu %1,%1,$14 \n\t" \
|
||||
" sltu $14,%1,$14 \n\t" \
|
||||
" addu %2,%2,$15 \n\t" \
|
||||
" addu %2,%2,$14 \n\t" \
|
||||
:"=r"(c0), "=r"(c1), "=r"(c2):"0"(c0), "1"(c1), "2"(c2), "r"(i), "r"(j):"$12", "$13", "$14", "$15");
|
||||
|
||||
#define SQRADDSC(i, j) \
|
||||
__asm__( \
|
||||
" multu %6,%7 \n\t" \
|
||||
" mflo %0 \n\t" \
|
||||
" mfhi %1 \n\t" \
|
||||
" xor %2,%2,%2 \n\t" \
|
||||
:"=r"(sc0), "=r"(sc1), "=r"(sc2):"0"(sc0), "1"(sc1), "2"(sc2), "r"(i),"r"(j) : "cc");
|
||||
|
||||
#define SQRADDAC(i, j) \
|
||||
__asm__( \
|
||||
" multu %6,%7 \n\t" \
|
||||
" mflo $12 \n\t" \
|
||||
" mfhi $13 \n\t" \
|
||||
" addu %0,%0,$12 \n\t" \
|
||||
" sltu $12,%0,$12 \n\t" \
|
||||
" addu %1,%1,$13 \n\t" \
|
||||
" sltu $13,%1,$13 \n\t" \
|
||||
" addu %1,%1,$12 \n\t" \
|
||||
" sltu $12,%1,$12 \n\t" \
|
||||
" addu %2,%2,$13 \n\t" \
|
||||
" addu %2,%2,$12 \n\t" \
|
||||
:"=r"(sc0), "=r"(sc1), "=r"(sc2):"0"(sc0), "1"(sc1), "2"(sc2), "r"(i), "r"(j):"$12", "$13", "$14");
|
||||
|
||||
#define SQRADDDB \
|
||||
__asm__( \
|
||||
" addu %0,%0,%3 \n\t" \
|
||||
" sltu $10,%0,%3 \n\t" \
|
||||
" addu %1,%1,$10 \n\t" \
|
||||
" sltu $10,%1,$10 \n\t" \
|
||||
" addu %1,%1,%4 \n\t" \
|
||||
" sltu $11,%1,%4 \n\t" \
|
||||
" addu %2,%2,$10 \n\t" \
|
||||
" addu %2,%2,$11 \n\t" \
|
||||
" addu %2,%2,%5 \n\t" \
|
||||
\
|
||||
" addu %0,%0,%3 \n\t" \
|
||||
" sltu $10,%0,%3 \n\t" \
|
||||
" addu %1,%1,$10 \n\t" \
|
||||
" sltu $10,%1,$10 \n\t" \
|
||||
" addu %1,%1,%4 \n\t" \
|
||||
" sltu $11,%1,%4 \n\t" \
|
||||
" addu %2,%2,$10 \n\t" \
|
||||
" addu %2,%2,$11 \n\t" \
|
||||
" addu %2,%2,%5 \n\t" \
|
||||
:"=r"(c0), "=r"(c1), "=r"(c2) : "r"(sc0), "r"(sc1), "r"(sc2), "0"(c0), "1"(c1), "2"(c2) : "$10", "$11");
|
||||
|
||||
#else
|
||||
|
||||
@ -1174,10 +1310,10 @@ __asm__( \
|
||||
t = ((fp_word)i) * ((fp_word)j); \
|
||||
tt = (fp_word)c0 + t; c0 = (fp_digit)tt; \
|
||||
tt = (fp_word)c1 + (tt >> DIGIT_BIT); c1 = (fp_digit)tt; \
|
||||
c2 +=(fp_digit)( tt >> DIGIT_BIT); \
|
||||
c2 +=(fp_digit)(tt >> DIGIT_BIT); \
|
||||
tt = (fp_word)c0 + t; c0 = (fp_digit)tt; \
|
||||
tt = (fp_word)c1 + (tt >> DIGIT_BIT); c1 = (fp_digit)tt; \
|
||||
c2 +=(fp_digit) (tt >> DIGIT_BIT); \
|
||||
c2 +=(fp_digit)(tt >> DIGIT_BIT); \
|
||||
} while (0);
|
||||
|
||||
#define SQRADDSC(i, j) \
|
||||
@ -1497,15 +1633,15 @@ __asm__( \
|
||||
|
||||
#define COMBA_FINI
|
||||
|
||||
/* untested: will mulhwu change the flags? Docs say no */
|
||||
/* untested: will mulhdu change the flags? Docs say no */
|
||||
#define MULADD(i, j) \
|
||||
____asm__( \
|
||||
" mulld 16,%6,%7 \n\t" \
|
||||
" mulld r16,%6,%7 \n\t" \
|
||||
" addc %0,%0,16 \n\t" \
|
||||
" mulhdu 16,%6,%7 \n\t" \
|
||||
" mulhdu r16,%6,%7 \n\t" \
|
||||
" adde %1,%1,16 \n\t" \
|
||||
" addze %2,%2 \n\t" \
|
||||
:"=r"(c0), "=r"(c1), "=r"(c2):"0"(c0), "1"(c1), "2"(c2), "r"(i), "r"(j):"16");
|
||||
:"=r"(c0), "=r"(c1), "=r"(c2):"0"(c0), "1"(c1), "2"(c2), "r"(i), "r"(j):"r16");
|
||||
|
||||
#elif defined(TFM_AVR32)
|
||||
|
||||
@ -1535,6 +1671,40 @@ ____asm__( \
|
||||
" acr %2 \n\t"\
|
||||
:"=r"(c0), "=r"(c1), "=r"(c2):"0"(c0), "1"(c1), "2"(c2), "r"(i), "r"(j):"r2","r3");
|
||||
|
||||
#elif defined(TFM_MIPS)
|
||||
|
||||
/* MIPS */
|
||||
#define COMBA_START
|
||||
|
||||
#define COMBA_CLEAR \
|
||||
c0 = c1 = c2 = 0;
|
||||
|
||||
#define COMBA_FORWARD \
|
||||
do { c0 = c1; c1 = c2; c2 = 0; } while (0);
|
||||
|
||||
#define COMBA_STORE(x) \
|
||||
x = c0;
|
||||
|
||||
#define COMBA_STORE2(x) \
|
||||
x = c1;
|
||||
|
||||
#define COMBA_FINI
|
||||
|
||||
#define MULADD(i, j) \
|
||||
__asm__( \
|
||||
" multu %6,%7 \n\t" \
|
||||
" mflo $12 \n\t" \
|
||||
" mfhi $13 \n\t" \
|
||||
" addu %0,%0,$12 \n\t" \
|
||||
" sltu $12,%0,$12 \n\t" \
|
||||
" addu %1,%1,$13 \n\t" \
|
||||
" sltu $13,%1,$13 \n\t" \
|
||||
" addu %1,%1,$12 \n\t" \
|
||||
" sltu $12,%1,$12 \n\t" \
|
||||
" addu %2,%2,$13 \n\t" \
|
||||
" addu %2,%2,$12 \n\t" \
|
||||
:"=r"(c0), "=r"(c1), "=r"(c2):"0"(c0), "1"(c1), "2"(c2), "r"(i), "r"(j):"$12","$13");
|
||||
|
||||
#else
|
||||
/* ISO C code */
|
||||
|
||||
@ -1556,9 +1726,11 @@ ____asm__( \
|
||||
|
||||
#define MULADD(i, j) \
|
||||
do { fp_word t; \
|
||||
t = (fp_word)c0 + ((fp_word)i) * ((fp_word)j); c0 = (fp_digit)t; \
|
||||
t = (fp_word)c0 + ((fp_word)i) * ((fp_word)j); \
|
||||
c0 = (fp_digit)t; \
|
||||
t = (fp_word)c1 + (t >> DIGIT_BIT); \
|
||||
c1 = (fp_digit)t; c2 += (fp_digit)(t >> DIGIT_BIT); \
|
||||
c1 = (fp_digit)t; \
|
||||
c2 += (fp_digit)(t >> DIGIT_BIT); \
|
||||
} while (0);
|
||||
|
||||
#endif
|
||||
|
@ -29,7 +29,7 @@ void fp_montgomery_reduce_small(fp_int *a, fp_int *m, fp_digit mp)
|
||||
int oldused, x, y, pa;
|
||||
|
||||
/* now zero the buff */
|
||||
XMEMSET(c, 0, sizeof c);
|
||||
XMEMSET(c, 0, sizeof(c));
|
||||
|
||||
pa = m->used;
|
||||
|
||||
|
@ -325,10 +325,15 @@ int mp_init_copy (mp_int * a, mp_int * b)
|
||||
{
|
||||
int res;
|
||||
|
||||
if ((res = mp_init (a)) != MP_OKAY) {
|
||||
if ((res = mp_init_size (a, b->used)) != MP_OKAY) {
|
||||
return res;
|
||||
}
|
||||
return mp_copy (b, a);
|
||||
|
||||
if((res = mp_copy (b, a)) != MP_OKAY) {
|
||||
mp_clear(a);
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
@ -929,13 +934,15 @@ int mp_invmod (mp_int * a, mp_int * b, mp_int * c)
|
||||
|
||||
#ifdef BN_FAST_MP_INVMOD_C
|
||||
/* if the modulus is odd we can use a faster routine instead */
|
||||
if (mp_isodd (b) == MP_YES) {
|
||||
if ((mp_isodd(b) == MP_YES) && (mp_cmp_d(b, 1) != MP_EQ)) {
|
||||
return fast_mp_invmod (a, b, c);
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef BN_MP_INVMOD_SLOW_C
|
||||
return mp_invmod_slow(a, b, c);
|
||||
#else
|
||||
return MP_VAL;
|
||||
#endif
|
||||
}
|
||||
|
||||
@ -1379,7 +1386,7 @@ int mp_mod (mp_int * a, mp_int * b, mp_int * c)
|
||||
mp_int t;
|
||||
int res;
|
||||
|
||||
if ((res = mp_init (&t)) != MP_OKAY) {
|
||||
if ((res = mp_init_size (&t, b->used)) != MP_OKAY) {
|
||||
return res;
|
||||
}
|
||||
|
||||
@ -1388,11 +1395,11 @@ int mp_mod (mp_int * a, mp_int * b, mp_int * c)
|
||||
return res;
|
||||
}
|
||||
|
||||
if (t.sign != b->sign) {
|
||||
res = mp_add (b, &t, c);
|
||||
} else {
|
||||
if ((mp_iszero(&t) != MP_NO) || (t.sign == b->sign)) {
|
||||
res = MP_OKAY;
|
||||
mp_exch (&t, c);
|
||||
} else {
|
||||
res = mp_add (b, &t, c);
|
||||
}
|
||||
|
||||
mp_clear (&t);
|
||||
@ -1891,7 +1898,7 @@ int mp_exptmod_fast (mp_int * G, mp_int * X, mp_int * P, mp_int * Y,
|
||||
|
||||
/* init M array */
|
||||
/* init first cell */
|
||||
if ((err = mp_init(&M[1])) != MP_OKAY) {
|
||||
if ((err = mp_init_size(&M[1], P->alloc)) != MP_OKAY) {
|
||||
#ifdef WOLFSSL_SMALL_STACK
|
||||
XFREE(M, NULL, DYNAMIC_TYPE_TMP_BUFFER);
|
||||
#endif
|
||||
@ -1901,7 +1908,7 @@ int mp_exptmod_fast (mp_int * G, mp_int * X, mp_int * P, mp_int * Y,
|
||||
|
||||
/* now init the second half of the array */
|
||||
for (x = 1<<(winsize-1); x < (1 << winsize); x++) {
|
||||
if ((err = mp_init(&M[x])) != MP_OKAY) {
|
||||
if ((err = mp_init_size(&M[x], P->alloc)) != MP_OKAY) {
|
||||
for (y = 1<<(winsize-1); y < x; y++) {
|
||||
mp_clear (&M[y]);
|
||||
}
|
||||
@ -1967,7 +1974,7 @@ int mp_exptmod_fast (mp_int * G, mp_int * X, mp_int * P, mp_int * Y,
|
||||
}
|
||||
|
||||
/* setup result */
|
||||
if ((err = mp_init (&res)) != MP_OKAY) {
|
||||
if ((err = mp_init_size (&res, P->alloc)) != MP_OKAY) {
|
||||
goto LBL_M;
|
||||
}
|
||||
|
||||
@ -1984,15 +1991,15 @@ int mp_exptmod_fast (mp_int * G, mp_int * X, mp_int * P, mp_int * Y,
|
||||
if ((err = mp_montgomery_calc_normalization (&res, P)) != MP_OKAY) {
|
||||
goto LBL_RES;
|
||||
}
|
||||
#else
|
||||
err = MP_VAL;
|
||||
goto LBL_RES;
|
||||
#endif
|
||||
|
||||
/* now set M[1] to G * R mod m */
|
||||
if ((err = mp_mulmod (G, &res, P, &M[1])) != MP_OKAY) {
|
||||
goto LBL_RES;
|
||||
}
|
||||
#else
|
||||
err = MP_VAL;
|
||||
goto LBL_RES;
|
||||
#endif
|
||||
} else {
|
||||
if ((err = mp_set(&res, 1)) != MP_OKAY) {
|
||||
goto LBL_RES;
|
||||
@ -2536,7 +2543,9 @@ top:
|
||||
* Each successive "recursion" makes the input smaller and smaller.
|
||||
*/
|
||||
if (mp_cmp_mag (x, n) != MP_LT) {
|
||||
s_mp_sub(x, n, x);
|
||||
if ((err = s_mp_sub(x, n, x)) != MP_OKAY) {
|
||||
return err;
|
||||
}
|
||||
goto top;
|
||||
}
|
||||
return MP_OKAY;
|
||||
@ -2573,7 +2582,9 @@ top:
|
||||
}
|
||||
|
||||
if (mp_cmp_mag(a, n) != MP_LT) {
|
||||
s_mp_sub(a, n, a);
|
||||
if ((res = s_mp_sub(a, n, a)) != MP_OKAY) {
|
||||
goto ERR;
|
||||
}
|
||||
goto top;
|
||||
}
|
||||
|
||||
@ -2711,7 +2722,7 @@ int mp_mulmod (mp_int * a, mp_int * b, mp_int * c, mp_int * d)
|
||||
int res;
|
||||
mp_int t;
|
||||
|
||||
if ((res = mp_init (&t)) != MP_OKAY) {
|
||||
if ((res = mp_init_size (&t, c->used)) != MP_OKAY) {
|
||||
return res;
|
||||
}
|
||||
|
||||
@ -3732,7 +3743,9 @@ top:
|
||||
}
|
||||
|
||||
if (mp_cmp_mag(a, n) != MP_LT) {
|
||||
s_mp_sub(a, n, a);
|
||||
if ((res = s_mp_sub(a, n, a)) != MP_OKAY) {
|
||||
goto ERR;
|
||||
}
|
||||
goto top;
|
||||
}
|
||||
|
||||
@ -4751,7 +4764,7 @@ int mp_read_radix (mp_int * a, const char *str, int radix)
|
||||
mp_zero (a);
|
||||
|
||||
/* process each digit of the string */
|
||||
while (*str) {
|
||||
while (*str != '\0') {
|
||||
/* if the radix <= 36 the conversion is case insensitive
|
||||
* this allows numbers like 1AB and 1ab to represent the same value
|
||||
* [e.g. in hex]
|
||||
@ -4780,6 +4793,12 @@ int mp_read_radix (mp_int * a, const char *str, int radix)
|
||||
++str;
|
||||
}
|
||||
|
||||
/* if digit in isn't null term, then invalid character was found */
|
||||
if (*str != '\0') {
|
||||
mp_zero (a);
|
||||
return MP_VAL;
|
||||
}
|
||||
|
||||
/* set the sign only if a != 0 */
|
||||
if (mp_iszero(a) != MP_YES) {
|
||||
a->sign = neg;
|
||||
|
Reference in New Issue
Block a user