mirror of
https://github.com/wolfSSL/wolfssl.git
synced 2025-07-30 02:37:28 +02:00
SP int: sp_invmod_div check div result before proceeding
This commit is contained in:
@ -11430,8 +11430,8 @@ static int _sp_invmod_div(sp_int* a, sp_int* m, sp_int* x, sp_int* y, sp_int* b,
|
||||
sp_int* c, sp_int* inv)
|
||||
{
|
||||
int err = MP_OKAY;
|
||||
sp_int* d;
|
||||
sp_int* r;
|
||||
sp_int* d = NULL;
|
||||
sp_int* r = NULL;
|
||||
sp_int* s;
|
||||
#ifndef WOLFSSL_SP_INT_NEGATIVE
|
||||
int bneg = 0;
|
||||
@ -11456,6 +11456,7 @@ static int _sp_invmod_div(sp_int* a, sp_int* m, sp_int* x, sp_int* y, sp_int* b,
|
||||
while ((err == MP_OKAY) && (!sp_isone(x)) && (!sp_iszero(x))) {
|
||||
/* 2.1. d = x / y, r = x mod y */
|
||||
err = sp_div(x, y, d, r);
|
||||
if (err == MP_OKAY) {
|
||||
/* 2.2. c -= d * b */
|
||||
if (sp_isone(d)) {
|
||||
/* c -= 1 * b */
|
||||
@ -11474,6 +11475,7 @@ static int _sp_invmod_div(sp_int* a, sp_int* m, sp_int* x, sp_int* y, sp_int* b,
|
||||
/* 2.4. s = b, b = c, c = s */
|
||||
s = b; b = c; c = s;
|
||||
}
|
||||
}
|
||||
/* 3. If y != 0 then NO_INVERSE */
|
||||
if ((err == MP_OKAY) && (!sp_iszero(y))) {
|
||||
err = MP_VAL;
|
||||
@ -11491,6 +11493,7 @@ static int _sp_invmod_div(sp_int* a, sp_int* m, sp_int* x, sp_int* y, sp_int* b,
|
||||
while ((err == MP_OKAY) && (!sp_isone(x)) && (!sp_iszero(x))) {
|
||||
/* 2.1. d = x / y, r = x mod y */
|
||||
err = sp_div(x, y, d, r);
|
||||
if (err == MP_OKAY) {
|
||||
if (sp_isone(d)) {
|
||||
/* c -= 1 * b */
|
||||
if ((bneg ^ cneg) == 1) {
|
||||
@ -11517,7 +11520,8 @@ static int _sp_invmod_div(sp_int* a, sp_int* m, sp_int* x, sp_int* y, sp_int* b,
|
||||
_sp_add_off(c, d, c, 0);
|
||||
}
|
||||
else if (_sp_cmp_abs(c, d) == MP_LT) {
|
||||
/* |c| < |d| and same sign, reverse subtract and negate. */
|
||||
/* |c| < |d| and same sign, reverse subtract and negate.
|
||||
*/
|
||||
_sp_sub_off(d, c, c, 0);
|
||||
cneg = !cneg;
|
||||
}
|
||||
@ -11532,6 +11536,7 @@ static int _sp_invmod_div(sp_int* a, sp_int* m, sp_int* x, sp_int* y, sp_int* b,
|
||||
s = b; b = c; c = s;
|
||||
neg = bneg; bneg = cneg; cneg = neg;
|
||||
}
|
||||
}
|
||||
/* 3. If y != 0 then NO_INVERSE */
|
||||
if ((err == MP_OKAY) && (!sp_iszero(y))) {
|
||||
err = MP_VAL;
|
||||
|
Reference in New Issue
Block a user