Merge pull request #3645 from douzzer/sp_copy_pedantic_error_handling

sp_copy() pedantic error handling
This commit is contained in:
Sean Parkinson
2021-01-13 10:05:35 +10:00
committed by GitHub

View File

@ -4651,8 +4651,9 @@ int sp_mod(sp_int* a, sp_int* m, sp_int* r)
t->dp[k+1] = h;
t->used = k + 2;
sp_copy(t, r);
sp_clamp(r);
err = sp_copy(t, r);
if (err == MP_OKAY)
sp_clamp(r);
}
#ifdef WOLFSSL_SMALL_STACK
@ -4728,8 +4729,9 @@ int sp_mod(sp_int* a, sp_int* m, sp_int* r)
t->dp[k+1] = h;
t->used = k + 2;
sp_copy(t, r);
sp_clamp(r);
err = sp_copy(t, r);
if (err == MP_OKAY)
sp_clamp(r);
}
#ifdef WOLFSSL_SMALL_STACK
@ -4798,8 +4800,9 @@ int sp_mod(sp_int* a, sp_int* m, sp_int* r)
t->dp[k+1] = (sp_int_digit)h;
t->used = k + 2;
sp_copy(t, r);
sp_clamp(r);
err = sp_copy(t, r);
if (err == MP_OKAY)
sp_clamp(r);
}
#ifdef WOLFSSL_SMALL_STACK
@ -5002,13 +5005,13 @@ int sp_mod(sp_int* a, sp_int* m, sp_int* r)
t->dp[5] = l;
l = h;
h = o;
o = 0;
SP_ASM_MUL_ADD_NO(l, h, a->dp[3], b->dp[3]);
t->dp[6] = l;
t->dp[7] = h;
t->used = 8;
sp_copy(t, r);
sp_clamp(r);
err = sp_copy(t, r);
if (err == MP_OKAY)
sp_clamp(r);
}
#ifdef WOLFSSL_SMALL_STACK
@ -5126,13 +5129,13 @@ int sp_mod(sp_int* a, sp_int* m, sp_int* r)
t->dp[9] = l;
l = h;
h = o;
o = 0;
SP_ASM_MUL_ADD_NO(l, h, a->dp[5], b->dp[5]);
t->dp[10] = l;
t->dp[11] = h;
t->used = 12;
sp_copy(t, r);
sp_clamp(r);
err = sp_copy(t, r);
if (err == MP_OKAY)
sp_clamp(r);
}
#ifdef WOLFSSL_SMALL_STACK
@ -5299,8 +5302,9 @@ int sp_mod(sp_int* a, sp_int* m, sp_int* r)
t->dp[14] = l;
t->dp[15] = h;
t->used = 16;
sp_copy(t, r);
sp_clamp(r);
err = sp_copy(t, r);
if (err == MP_OKAY)
sp_clamp(r);
}
#ifdef WOLFSSL_SMALL_STACK
@ -5579,8 +5583,9 @@ int sp_mod(sp_int* a, sp_int* m, sp_int* r)
t->dp[22] = l;
t->dp[23] = h;
t->used = 24;
sp_copy(t, r);
sp_clamp(r);
err = sp_copy(t, r);
if (err == MP_OKAY)
sp_clamp(r);
}
#ifdef WOLFSSL_SMALL_STACK
@ -6005,8 +6010,9 @@ int sp_mod(sp_int* a, sp_int* m, sp_int* r)
t->dp[30] = l;
t->dp[31] = h;
t->used = 32;
sp_copy(t, r);
sp_clamp(r);
err = sp_copy(t, r);
if (err == MP_OKAY)
sp_clamp(r);
}
#ifdef WOLFSSL_SMALL_STACK
@ -6812,8 +6818,9 @@ int sp_mod(sp_int* a, sp_int* m, sp_int* r)
t->dp[46] = l;
t->dp[47] = h;
t->used = 48;
sp_copy(t, r);
sp_clamp(r);
err = sp_copy(t, r);
if (err == MP_OKAY)
sp_clamp(r);
}
#ifdef WOLFSSL_SMALL_STACK
@ -7881,7 +7888,7 @@ int sp_invmod(sp_int* a, sp_int* m, sp_int* r)
err = MP_VAL;
}
else {
sp_copy(c, r);
err = sp_copy(c, r);
}
}
@ -7956,7 +7963,7 @@ int sp_invmod_mont_ct(sp_int* a, sp_int* m, sp_int* r, sp_int_digit mp)
sp_init(e);
sp_init(&pre[0]);
sp_copy(a, &pre[0]);
err = sp_copy(a, &pre[0]);
for (i = 1; (err == MP_OKAY) && (i < CT_INV_MOD_PRE_CNT); i++) {
sp_init(&pre[i]);
err = sp_sqr(&pre[i-1], &pre[i]);
@ -7979,7 +7986,7 @@ int sp_invmod_mont_ct(sp_int* a, sp_int* m, sp_int* r, sp_int_digit mp)
break;
}
}
sp_copy(&pre[j-1], t);
err = sp_copy(&pre[j-1], t);
for (j = 0; (err == MP_OKAY) && (i >= 0); i--) {
int set = sp_is_bit_set(e, i);
@ -8007,7 +8014,7 @@ int sp_invmod_mont_ct(sp_int* a, sp_int* m, sp_int* r, sp_int_digit mp)
}
}
else {
sp_copy(t, r);
err = sp_copy(t, r);
}
}
@ -8087,13 +8094,13 @@ static int _sp_exptmod_ex(sp_int* b, sp_int* e, int bits, sp_int* m, sp_int* r)
}
}
else {
sp_copy(b, &t[0]);
err = sp_copy(b, &t[0]);
}
}
if ((!done) && (err == MP_OKAY)) {
/* t[0] is dummy value and t[1] is result */
sp_copy(&t[0], &t[1]);
err = sp_copy(&t[0], &t[1]);
for (i = bits - 1; (err == MP_OKAY) && (i >= 0); i--) {
#ifdef WC_NO_CACHE_RESISTANT
@ -8132,7 +8139,7 @@ static int _sp_exptmod_ex(sp_int* b, sp_int* e, int bits, sp_int* m, sp_int* r)
}
}
if ((!done) && (err == MP_OKAY)) {
sp_copy(&t[1], r);
err = sp_copy(&t[1], r);
}
#ifdef WOLFSSL_SMALL_STACK
@ -8197,7 +8204,7 @@ static int _sp_exptmod_mont_ex(sp_int* b, sp_int* e, int bits, sp_int* m,
}
}
else {
sp_copy(b, &t[0]);
err = sp_copy(b, &t[0]);
}
}
@ -8254,7 +8261,7 @@ static int _sp_exptmod_mont_ex(sp_int* b, sp_int* e, int bits, sp_int* m,
}
}
if ((!done) && (err == MP_OKAY)) {
sp_copy(&t[1], r);
err = sp_copy(&t[1], r);
}
#ifdef WOLFSSL_SMALL_STACK
@ -8340,7 +8347,7 @@ static int _sp_exptmod_mont_ex(sp_int* b, sp_int* e, int bits, sp_int* m,
}
}
else {
sp_copy(b, &t[1]);
err = sp_copy(b, &t[1]);
}
}
@ -8429,7 +8436,7 @@ static int _sp_exptmod_mont_ex(sp_int* b, sp_int* e, int bits, sp_int* m,
}
}
if ((!done) && (err == MP_OKAY)) {
sp_copy(tr, r);
err = sp_copy(tr, r);
}
if (t != NULL) {
@ -8599,7 +8606,7 @@ static int _sp_exptmod_base_2(sp_int* e, int digits, sp_int* m, sp_int* r)
}
}
if (err == MP_OKAY) {
sp_copy(tr, r);
err = sp_copy(tr, r);
}
#ifdef WOLFSSL_SMALL_STACK
@ -8863,7 +8870,7 @@ static int _sp_exptmod_nct(sp_int* b, sp_int* e, sp_int* m, sp_int* r)
}
}
else {
sp_copy(b, bm);
err = sp_copy(b, bm);
}
}
@ -8919,7 +8926,7 @@ static int _sp_exptmod_nct(sp_int* b, sp_int* e, sp_int* m, sp_int* r)
n <<= winBits;
c -= winBits;
}
sp_copy(&t[y], tr);
err = sp_copy(&t[y], tr);
}
else {
/* 1 in Montgomery form. */
@ -9027,7 +9034,7 @@ static int _sp_exptmod_nct(sp_int* b, sp_int* e, sp_int* m, sp_int* r)
}
}
if ((!done) && (err == MP_OKAY)) {
sp_copy(tr, r);
err = sp_copy(tr, r);
}
if (t != NULL) {
@ -9084,7 +9091,7 @@ static int _sp_exptmod_nct(sp_int* b, sp_int* e, sp_int* m, sp_int* r)
}
}
else {
sp_copy(b, &t[0]);
err = sp_copy(b, &t[0]);
}
}
@ -9124,7 +9131,7 @@ static int _sp_exptmod_nct(sp_int* b, sp_int* e, sp_int* m, sp_int* r)
}
}
if ((!done) && (err == MP_OKAY)) {
sp_copy(&t[0], r);
err = sp_copy(&t[0], r);
}
#ifdef WOLFSSL_SMALL_STACK
@ -9233,7 +9240,7 @@ int sp_div_2d(sp_int* a, int e, sp_int* r, sp_int* rem)
else {
if (rem != NULL) {
/* Copy a in to remainder. */
sp_copy(a, rem);
err = sp_copy(a, rem);
}
/* Shift a down by into result. */
sp_rshb(a, e, r);
@ -9318,9 +9325,11 @@ int sp_mul_2d(sp_int* a, int e, sp_int* r)
if (err == MP_OKAY) {
/* Copy a into r as left shift function works on the number. */
if (a != r) {
sp_copy(a, r);
err = sp_copy(a, r);
}
}
if (err == MP_OKAY) {
if (0) {
sp_print(a, "a");
sp_print_int(e, "n");
@ -9430,8 +9439,9 @@ int sp_mul_2d(sp_int* a, int e, sp_int* r)
t->dp[k * 2] = h;
t->used = a->used * 2;
sp_copy(t, r);
sp_clamp(r);
err = sp_copy(t, r);
if (err == MP_OKAY)
sp_clamp(r);
}
else if (err == MP_OKAY) {
sp_int_digit l;
@ -9440,8 +9450,9 @@ int sp_mul_2d(sp_int* a, int e, sp_int* r)
t->dp[1] = l;
t->used = a->used * 2;
sp_copy(t, r);
sp_clamp(r);
err = sp_copy(t, r);
if (err == MP_OKAY)
sp_clamp(r);
}
#ifdef WOLFSSL_SMALL_STACK
@ -9512,8 +9523,9 @@ int sp_mul_2d(sp_int* a, int e, sp_int* r)
t->dp[k+1] = (sp_int_digit)h;
t->used = k + 2;
sp_copy(t, r);
sp_clamp(r);
err = sp_copy(t, r);
if (err == MP_OKAY)
sp_clamp(r);
}
#ifdef WOLFSSL_SMALL_STACK
if (t != NULL) {
@ -9695,13 +9707,13 @@ int sp_mul_2d(sp_int* a, int e, sp_int* r)
t->dp[5] = l;
l = h;
h = o;
o = 0;
SP_ASM_SQR_ADD_NO(l, h, a->dp[3]);
t->dp[6] = l;
t->dp[7] = h;
t->used = 8;
sp_copy(t, r);
sp_clamp(r);
err = sp_copy(t, r);
if (err == MP_OKAY)
sp_clamp(r);
}
#ifdef WOLFSSL_SMALL_STACK
@ -9811,13 +9823,13 @@ int sp_mul_2d(sp_int* a, int e, sp_int* r)
t->dp[9] = l;
l = h;
h = o;
o = 0;
SP_ASM_SQR_ADD_NO(l, h, a->dp[5]);
t->dp[10] = l;
t->dp[11] = h;
t->used = 12;
sp_copy(t, r);
sp_clamp(r);
err = sp_copy(t, r);
if (err == MP_OKAY)
sp_clamp(r);
}
#ifdef WOLFSSL_SMALL_STACK
@ -9967,8 +9979,9 @@ int sp_mul_2d(sp_int* a, int e, sp_int* r)
t->dp[14] = l;
t->dp[15] = h;
t->used = 16;
sp_copy(t, r);
sp_clamp(r);
err = sp_copy(t, r);
if (err == MP_OKAY)
sp_clamp(r);
}
#ifdef WOLFSSL_SMALL_STACK
@ -10200,8 +10213,9 @@ int sp_mul_2d(sp_int* a, int e, sp_int* r)
t->dp[22] = l;
t->dp[23] = h;
t->used = 24;
sp_copy(t, r);
sp_clamp(r);
err = sp_copy(t, r);
if (err == MP_OKAY)
sp_clamp(r);
}
#ifdef WOLFSSL_SMALL_STACK
@ -10533,8 +10547,9 @@ int sp_mul_2d(sp_int* a, int e, sp_int* r)
t->dp[30] = l;
t->dp[31] = h;
t->used = 32;
sp_copy(t, r);
sp_clamp(r);
err = sp_copy(t, r);
if (err == MP_OKAY)
sp_clamp(r);
}
#ifdef WOLFSSL_SMALL_STACK
@ -11107,8 +11122,9 @@ int sp_mul_2d(sp_int* a, int e, sp_int* r)
t->dp[46] = l;
t->dp[47] = h;
t->used = 48;
sp_copy(t, r);
sp_clamp(r);
err = sp_copy(t, r);
if (err == MP_OKAY)
sp_clamp(r);
}
#ifdef WOLFSSL_SMALL_STACK
@ -12793,8 +12809,9 @@ int sp_todecimal(sp_int* a, char* str)
err = MP_MEM;
}
#endif /* WOLFSSL_SMALL_STACK */
if (err == MP_OKAY)
err = sp_copy(a, t);
if (err == MP_OKAY) {
sp_copy(a, t);
#ifdef WOLFSSL_SP_INT_NEGATIVE
if (a->sign == MP_NEG) {
@ -12933,8 +12950,10 @@ int sp_radix_size(sp_int* a, int radix, int* size)
}
}
#endif /* WOLFSSL_SMALL_STACK */
if (err == MP_OKAY)
err = sp_copy(a, t);
if (err == MP_OKAY) {
sp_copy(a, t);
for (i = 0; !sp_iszero(t); i++) {
sp_div_d(t, 10, t, &d);
@ -13545,11 +13564,11 @@ int sp_gcd(sp_int* a, sp_int* b, sp_int* r)
err = MP_VAL;
}
else {
sp_copy(b, r);
err = sp_copy(b, r);
}
}
else if (sp_iszero(b)) {
sp_copy(a, r);
err = sp_copy(a, r);
}
else {
#ifdef WOLFSSL_SMALL_STACK
@ -13625,7 +13644,8 @@ int sp_gcd(sp_int* a, sp_int* b, sp_int* r)
sp_copy(v, u);
sp_copy(t, v);
}
sp_copy(u, r);
if (err == MP_OKAY)
err = sp_copy(u, r);
}
#ifdef WOLFSSL_SMALL_STACK