Commit b078bf57 authored by Davide Galassi's avatar Davide Galassi

Various MPI bug fixes on not initialized values and mul by zero

parent ec3f37c9
...@@ -43,7 +43,7 @@ int cry_mpi_inv(cry_mpi *r, const cry_mpi *a, const cry_mpi *m) ...@@ -43,7 +43,7 @@ int cry_mpi_inv(cry_mpi *r, const cry_mpi *a, const cry_mpi *m)
cry_mpi_zero(&s0); cry_mpi_zero(&s0);
ret = -1; ret = -1;
} }
cry_mpi_clear_list(&r0, &r1, &s0, &s1, NULL); cry_mpi_clear_list(&r0, &r1, &s0, &s1, &q, NULL);
return ret; return ret;
} }
......
...@@ -55,11 +55,13 @@ int cry_mpi_mul_comba(cry_mpi *r, const cry_mpi *a, const cry_mpi *b) ...@@ -55,11 +55,13 @@ int cry_mpi_mul_comba(cry_mpi *r, const cry_mpi *a, const cry_mpi *b)
int res, ix, iy, iz, tx, ty, pa; int res, ix, iy, iz, tx, ty, pa;
cry_mpi_digit c0, c1, c2, *tmpx, *tmpy; cry_mpi_digit c0, c1, c2, *tmpx, *tmpy;
cry_mpi tmp, *dst; cry_mpi tmp, *dst;
size_t digs = a->used + b->used;
c0 = c1 = c2 = 0; if (cry_mpi_is_zero(a) || cry_mpi_is_zero(b)) {
pa = CRY_MIN(digs, a->used + b->used); cry_mpi_zero(r);
return 0;
}
pa = a->used + b->used;
if (r == a || r == b) { if (r == a || r == b) {
if ((res = cry_mpi_init_size(&tmp, pa)) != 0) if ((res = cry_mpi_init_size(&tmp, pa)) != 0)
return res; return res;
...@@ -70,6 +72,7 @@ int cry_mpi_mul_comba(cry_mpi *r, const cry_mpi *a, const cry_mpi *b) ...@@ -70,6 +72,7 @@ int cry_mpi_mul_comba(cry_mpi *r, const cry_mpi *a, const cry_mpi *b)
} }
cry_mpi_set_used(dst, pa); cry_mpi_set_used(dst, pa);
c0 = c1 = c2 = 0;
for (ix = 0; ix < pa; ix++) { for (ix = 0; ix < pa; ix++) {
/* get offsets into the two bignums */ /* get offsets into the two bignums */
......
...@@ -7,6 +7,11 @@ int cry_mpi_mul_karatsuba(cry_mpi *r, const cry_mpi *a, const cry_mpi *b) ...@@ -7,6 +7,11 @@ int cry_mpi_mul_karatsuba(cry_mpi *r, const cry_mpi *a, const cry_mpi *b)
int B, hB, res; int B, hB, res;
cry_mpi x0, x1, y0, y1, z0, z1, z2; cry_mpi x0, x1, y0, y1, z0, z1, z2;
if (cry_mpi_is_zero(a) || cry_mpi_is_zero(b)) {
cry_mpi_zero(r);
return 0;
}
/* minimum number of digits */ /* minimum number of digits */
B = CRY_MIN(a->used, b->used); B = CRY_MIN(a->used, b->used);
/* divide by two */ /* divide by two */
......
...@@ -90,6 +90,11 @@ int cry_mpi_mul_toom3(cry_mpi *r, const cry_mpi *a, const cry_mpi *b) ...@@ -90,6 +90,11 @@ int cry_mpi_mul_toom3(cry_mpi *r, const cry_mpi *a, const cry_mpi *b)
int res, B; int res, B;
cry_mpi w0, w1, w2, w3, w4, a0, a1, a2, b0, b1, b2, t1, t2; cry_mpi w0, w1, w2, w3, w4, a0, a1, a2, b0, b1, b2, t1, t2;
if (cry_mpi_is_zero(a) || cry_mpi_is_zero(b)) {
cry_mpi_zero(r);
return 0;
}
/* init temps */ /* init temps */
if ((res = cry_mpi_init_list(&w0, &w1, &w2, &w3, &w4, if ((res = cry_mpi_init_list(&w0, &w1, &w2, &w3, &w4,
&a0, &a1, &a2, &b0, &b1, &a0, &a1, &a2, &b0, &b1,
......
...@@ -106,11 +106,11 @@ static int calc_b_and_m(cry_mpi *x, const cry_mpi *p) ...@@ -106,11 +106,11 @@ static int calc_b_and_m(cry_mpi *x, const cry_mpi *p)
one.alloc = 1; one.alloc = 1;
one.sign = 0; one.sign = 0;
if (cry_mpi_init_copy(x, p) < 0) if ((ret = cry_mpi_copy(x, p)) < 0)
return -1; return ret;
if ((ret = cry_mpi_sub(x, x, &one)) < 0) if ((ret = cry_mpi_sub(x, x, &one)) < 0)
goto e; return ret;
for (ret = 0; !cry_mpi_is_odd(x); ret++) { for (ret = 0; !cry_mpi_is_odd(x); ret++) {
if (cry_mpi_shr(x, x, 1) < 0) { /* div by 2 */ if (cry_mpi_shr(x, x, 1) < 0) { /* div by 2 */
...@@ -118,8 +118,6 @@ static int calc_b_and_m(cry_mpi *x, const cry_mpi *p) ...@@ -118,8 +118,6 @@ static int calc_b_and_m(cry_mpi *x, const cry_mpi *p)
break; break;
} }
} }
e: if (ret < 0)
cry_mpi_clear(x);
return ret; return ret;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment