Skip to content

Commit 235e831

Browse files
authored
Merge pull request #255 from libtom/hardening
hardening: add MP_ZERO_BUFFER, MP_ZERO_DIGITS
2 parents 55e312b + 61d9e52 commit 235e831

22 files changed

+68
-74
lines changed

astylerc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
# usage:
55
# astyle --options=astylerc *.[ch]
66

7+
# Do not create backup, annonying in the times of git
8+
suffix=none
9+
710
## Bracket Style Options
811
style=kr
912

bn_mp_add_d.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,7 @@ int mp_add_d(const mp_int *a, mp_digit b, mp_int *c)
8585
c->sign = MP_ZPOS;
8686

8787
/* now zero to oldused */
88-
while (ix++ < oldused) {
89-
*tmpc++ = 0;
90-
}
88+
MP_ZERO_DIGITS(tmpc, oldused - ix);
9189
mp_clamp(c);
9290

9391
return MP_OKAY;

bn_mp_and.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,7 @@ int mp_and(const mp_int *a, const mp_int *b, mp_int *c)
2929
}
3030

3131
/* zero digits above the last from the smallest mp_int */
32-
for (; ix < t.used; ix++) {
33-
t.dp[ix] = 0;
34-
}
32+
MP_ZERO_DIGITS(t.dp + ix, t.used - ix);
3533

3634
mp_clamp(&t);
3735
mp_exch(c, &t);

bn_mp_clear.c

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,10 @@
66
/* clear one (frees) */
77
void mp_clear(mp_int *a)
88
{
9-
int i;
10-
119
/* only do anything if a hasn't been freed previously */
1210
if (a->dp != NULL) {
13-
/* first zero the digits */
14-
for (i = 0; i < a->used; i++) {
15-
a->dp[i] = 0;
16-
}
17-
1811
/* free ram */
19-
MP_FREE(a->dp, sizeof(mp_digit) * (size_t)a->alloc);
12+
MP_FREE_DIGITS(a->dp, a->alloc);
2013

2114
/* reset members to make debugging easier */
2215
a->dp = NULL;

bn_mp_copy.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,7 @@ int mp_copy(const mp_int *a, mp_int *b)
3838
}
3939

4040
/* clear high digits */
41-
for (; n < b->used; n++) {
42-
*tmpb++ = 0;
43-
}
41+
MP_ZERO_DIGITS(tmpb, b->used - n);
4442
}
4543

4644
/* copy used count and sign */

bn_mp_div_2.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,7 @@ int mp_div_2(const mp_int *a, mp_int *b)
4040
}
4141

4242
/* zero excess digits */
43-
tmpb = b->dp + b->used;
44-
for (x = b->used; x < oldused; x++) {
45-
*tmpb++ = 0;
46-
}
43+
MP_ZERO_DIGITS(b->dp + b->used, oldused - b->used);
4744
}
4845
b->sign = a->sign;
4946
mp_clamp(b);

bn_mp_dr_reduce.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,7 @@ int mp_dr_reduce(mp_int *x, const mp_int *n, mp_digit k)
5858
*tmpx1++ = mu;
5959

6060
/* zero words above m */
61-
for (i = m + 1; i < x->used; i++) {
62-
*tmpx1++ = 0;
63-
}
61+
MP_ZERO_DIGITS(tmpx1, x->used - m - 1);
6462

6563
/* clamp, sub and return */
6664
mp_clamp(x);

bn_mp_fwrite.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,18 @@ int mp_fwrite(const mp_int *a, int radix, FILE *stream)
1919
}
2020

2121
if ((err = mp_toradix(a, buf, radix)) != MP_OKAY) {
22-
MP_FREE(buf, len);
22+
MP_FREE_BUFFER(buf, (size_t)len);
2323
return err;
2424
}
2525

2626
for (x = 0; x < len; x++) {
2727
if (fputc((int)buf[x], stream) == EOF) {
28-
MP_FREE(buf, len);
28+
MP_FREE_BUFFER(buf, (size_t)len);
2929
return MP_VAL;
3030
}
3131
}
3232

33-
MP_FREE(buf, len);
33+
MP_FREE_BUFFER(buf, (size_t)len);
3434
return MP_OKAY;
3535
}
3636
#endif

bn_mp_grow.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,7 @@ int mp_grow(mp_int *a, int size)
3434
/* zero excess digits */
3535
i = a->alloc;
3636
a->alloc = size;
37-
for (; i < a->alloc; i++) {
38-
a->dp[i] = 0;
39-
}
37+
MP_ZERO_DIGITS(a->dp + i, a->alloc - i);
4038
}
4139
return MP_OKAY;
4240
}

bn_mp_mod_2d.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ int mp_mod_2d(const mp_int *a, int b, mp_int *c)
2525
}
2626

2727
/* zero digits above the last digit of the modulus */
28-
for (x = (b / MP_DIGIT_BIT) + (((b % MP_DIGIT_BIT) == 0) ? 0 : 1); x < c->used; x++) {
29-
c->dp[x] = 0;
30-
}
28+
x = (b / MP_DIGIT_BIT) + (((b % MP_DIGIT_BIT) == 0) ? 0 : 1);
29+
MP_ZERO_DIGITS(c->dp + x, c->used - x);
30+
3131
/* clear the digit that is not completely outside/inside the modulus */
3232
c->dp[b / MP_DIGIT_BIT] &=
3333
((mp_digit)1 << (mp_digit)(b % MP_DIGIT_BIT)) - (mp_digit)1;

0 commit comments

Comments
 (0)