Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions bn_mp_addmod.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ mp_err mp_addmod(const mp_int *a, const mp_int *b, const mp_int *c, mp_int *d)
}

if ((err = mp_add(a, b, &t)) != MP_OKAY) {
mp_clear(&t);
return err;
goto LBL_ERR;
}
err = mp_mod(&t, c, d);

LBL_ERR:
mp_clear(&t);
return err;
}
Expand Down
27 changes: 12 additions & 15 deletions bn_mp_copy.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
mp_err mp_copy(const mp_int *a, mp_int *b)
{
int n;
mp_digit *tmpa, *tmpb;
mp_err err;

/* if dst == src do nothing */
Expand All @@ -22,26 +23,22 @@ mp_err mp_copy(const mp_int *a, mp_int *b)
}

/* zero b and copy the parameters over */
{
mp_digit *tmpa, *tmpb;
/* pointer aliases */

/* pointer aliases */
/* source */
tmpa = a->dp;

/* source */
tmpa = a->dp;
/* destination */
tmpb = b->dp;

/* destination */
tmpb = b->dp;

/* copy all the digits */
for (n = 0; n < a->used; n++) {
*tmpb++ = *tmpa++;
}

/* clear high digits */
MP_ZERO_DIGITS(tmpb, b->used - n);
/* copy all the digits */
for (n = 0; n < a->used; n++) {
*tmpb++ = *tmpa++;
}

/* clear high digits */
MP_ZERO_DIGITS(tmpb, b->used - n);

/* copy used count and sign */
b->used = a->used;
b->sign = a->sign;
Expand Down
37 changes: 18 additions & 19 deletions bn_mp_div_2.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
mp_err mp_div_2(const mp_int *a, mp_int *b)
{
int x, oldused;
mp_digit r, rr, *tmpa, *tmpb;
mp_err err;

/* copy */
Expand All @@ -18,31 +19,29 @@ mp_err mp_div_2(const mp_int *a, mp_int *b)

oldused = b->used;
b->used = a->used;
{
mp_digit r, rr, *tmpa, *tmpb;

/* source alias */
tmpa = a->dp + b->used - 1;
/* source alias */
tmpa = a->dp + b->used - 1;

/* dest alias */
tmpb = b->dp + b->used - 1;
/* dest alias */
tmpb = b->dp + b->used - 1;

/* carry */
r = 0;
for (x = b->used - 1; x >= 0; x--) {
/* get the carry for the next iteration */
rr = *tmpa & 1u;
/* carry */
r = 0;
for (x = b->used - 1; x >= 0; x--) {
/* get the carry for the next iteration */
rr = *tmpa & 1u;

/* shift the current digit, add in carry and store */
*tmpb-- = (*tmpa-- >> 1) | (r << (MP_DIGIT_BIT - 1));
/* shift the current digit, add in carry and store */
*tmpb-- = (*tmpa-- >> 1) | (r << (MP_DIGIT_BIT - 1));

/* forward carry to next iteration */
r = rr;
}

/* zero excess digits */
MP_ZERO_DIGITS(b->dp + b->used, oldused - b->used);
/* forward carry to next iteration */
r = rr;
}

/* zero excess digits */
MP_ZERO_DIGITS(b->dp + b->used, oldused - b->used);

b->sign = a->sign;
mp_clamp(b);
return MP_OKAY;
Expand Down
10 changes: 5 additions & 5 deletions bn_mp_export.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,19 +53,19 @@ mp_err mp_export(void *rop, size_t *countp, int order, size_t size,
*byte = (unsigned char)((j == ((size - nail_bytes) - 1u)) ? (t.dp[0] & odd_nail_mask) : (t.dp[0] & 0xFFuL));

if ((err = mp_div_2d(&t, (j == ((size - nail_bytes) - 1u)) ? (int)(8u - odd_nails) : 8, &t, NULL)) != MP_OKAY) {
mp_clear(&t);
return err;
goto LBL_ERR;
}
}
}

mp_clear(&t);

if (countp != NULL) {
*countp = count;
}
err = MP_OKAY;

return MP_OKAY;
LBL_ERR:
mp_clear(&t);
return err;
}

#endif
11 changes: 6 additions & 5 deletions bn_mp_expt_u32.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,26 @@ mp_err mp_expt_u32(const mp_int *a, uint32_t b, mp_int *c)
/* if the bit is set multiply */
if ((b & 1u) != 0u) {
if ((err = mp_mul(c, &g, c)) != MP_OKAY) {
mp_clear(&g);
return err;
goto LBL_ERR;
}
}

/* square */
if (b > 1u) {
if ((err = mp_sqr(&g, &g)) != MP_OKAY) {
mp_clear(&g);
return err;
goto LBL_ERR;
}
}

/* shift to next bit */
b >>= 1;
}

err = MP_OKAY;

LBL_ERR:
mp_clear(&g);
return MP_OKAY;
return err;
}

#endif
16 changes: 6 additions & 10 deletions bn_mp_exptmod.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,27 +26,23 @@ mp_err mp_exptmod(const mp_int *G, const mp_int *X, const mp_int *P, mp_int *Y)
return MP_VAL;
}

/* first compute 1/G mod P */
if ((err = mp_init(&tmpG)) != MP_OKAY) {
if ((err = mp_init_multi(&tmpG, &tmpX, NULL)) != MP_OKAY) {
return err;
}

/* first compute 1/G mod P */
if ((err = mp_invmod(G, P, &tmpG)) != MP_OKAY) {
mp_clear(&tmpG);
return err;
goto LBL_ERR;
}

/* now get |X| */
if ((err = mp_init(&tmpX)) != MP_OKAY) {
mp_clear(&tmpG);
return err;
}
if ((err = mp_abs(X, &tmpX)) != MP_OKAY) {
mp_clear_multi(&tmpG, &tmpX, NULL);
return err;
goto LBL_ERR;
}

/* and now compute (1/G)**|X| instead of G**X [X < 0] */
err = mp_exptmod(&tmpG, &tmpX, P, Y);
LBL_ERR:
mp_clear_multi(&tmpG, &tmpX, NULL);
return err;
}
Expand Down
11 changes: 6 additions & 5 deletions bn_mp_fwrite.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,18 @@ mp_err mp_fwrite(const mp_int *a, int radix, FILE *stream)
}

if ((err = mp_to_radix(a, buf, (size_t)len, radix)) != MP_OKAY) {
MP_FREE_BUFFER(buf, (size_t)len);
return err;
goto LBL_ERR;
}

if (fwrite(buf, (size_t)len, 1uL, stream) != 1uL) {
MP_FREE_BUFFER(buf, (size_t)len);
return MP_ERR;
err = MP_ERR;
goto LBL_ERR;
}
err = MP_OKAY;

LBL_ERR:
MP_FREE_BUFFER(buf, (size_t)len);
return MP_OKAY;
return err;
}
#endif

Expand Down
4 changes: 2 additions & 2 deletions bn_mp_mod.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ mp_err mp_mod(const mp_int *a, const mp_int *b, mp_int *c)
}

if ((err = mp_div(a, b, NULL, &t)) != MP_OKAY) {
mp_clear(&t);
return err;
goto LBL_ERR;
}

if (MP_IS_ZERO(&t) || (t.sign == b->sign)) {
Expand All @@ -25,6 +24,7 @@ mp_err mp_mod(const mp_int *a, const mp_int *b, mp_int *c)
err = mp_add(b, &t, c);
}

LBL_ERR:
mp_clear(&t);
return err;
}
Expand Down
5 changes: 3 additions & 2 deletions bn_mp_mulmod.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ mp_err mp_mulmod(const mp_int *a, const mp_int *b, const mp_int *c, mp_int *d)
}

if ((err = mp_mul(a, b, &t)) != MP_OKAY) {
mp_clear(&t);
return err;
goto LBL_ERR;
}
err = mp_mod(&t, c, d);

LBL_ERR:
mp_clear(&t);
return err;
}
Expand Down
10 changes: 6 additions & 4 deletions bn_mp_radix_size.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,18 @@ mp_err mp_radix_size(const mp_int *a, int radix, int *size)
/* fetch out all of the digits */
while (!MP_IS_ZERO(&t)) {
if ((err = mp_div_d(&t, (mp_digit)radix, &t, &d)) != MP_OKAY) {
mp_clear(&t);
return err;
goto LBL_ERR;
}
++digs;
}
mp_clear(&t);

/* return digs + 1, the 1 is for the NULL byte that would be required. */
*size = digs + 1;
return MP_OKAY;
err = MP_OKAY;

LBL_ERR:
mp_clear(&t);
return err;
}

#endif
5 changes: 3 additions & 2 deletions bn_mp_sqrmod.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ mp_err mp_sqrmod(const mp_int *a, const mp_int *b, mp_int *c)
}

if ((err = mp_sqr(a, &t)) != MP_OKAY) {
mp_clear(&t);
return err;
goto LBL_ERR;
}
err = mp_mod(&t, b, c);

LBL_ERR:
mp_clear(&t);
return err;
}
Expand Down
6 changes: 3 additions & 3 deletions bn_mp_sqrtmod_prime.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ mp_err mp_sqrtmod_prime(const mp_int *n, const mp_int *prime, mp_int *ret)
/* find a Z such that the Legendre symbol (Z|prime) == -1 */
mp_set_u32(&Z, 2u);
/* Z = 2 */
while (1) {
for (;;) {
if ((err = mp_kronecker(&Z, prime, &legendre)) != MP_OKAY) goto cleanup;
if (legendre == -1) break;
if ((err = mp_add_d(&Z, 1uL, &Z)) != MP_OKAY) goto cleanup;
Expand All @@ -81,10 +81,10 @@ mp_err mp_sqrtmod_prime(const mp_int *n, const mp_int *prime, mp_int *ret)
/* M = S */
mp_set_u32(&two, 2u);

while (1) {
for (;;) {
if ((err = mp_copy(&T, &t1)) != MP_OKAY) goto cleanup;
i = 0;
while (1) {
for (;;) {
if (mp_cmp_d(&t1, 1uL) == MP_EQ) break;
if ((err = mp_exptmod(&t1, &two, prime, &t1)) != MP_OKAY) goto cleanup;
i++;
Expand Down
5 changes: 3 additions & 2 deletions bn_mp_submod.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ mp_err mp_submod(const mp_int *a, const mp_int *b, const mp_int *c, mp_int *d)
}

if ((err = mp_sub(a, b, &t)) != MP_OKAY) {
mp_clear(&t);
return err;
goto LBL_ERR;
}
err = mp_mod(&t, c, d);

LBL_ERR:
mp_clear(&t);
return err;
}
Expand Down
6 changes: 4 additions & 2 deletions bn_mp_to_radix.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ mp_err mp_to_radix(const mp_int *a, char *str, size_t maxlen, int radix)
break;
}
if ((err = mp_div_d(&t, (mp_digit)radix, &t, &d)) != MP_OKAY) {
mp_clear(&t);
return err;
goto LBL_ERR;
}
*str++ = mp_s_rmap[d];
++digs;
Expand All @@ -67,6 +66,9 @@ mp_err mp_to_radix(const mp_int *a, char *str, size_t maxlen, int radix)
/* append a NULL so the string is properly terminated */
*str = '\0';

err = MP_OKAY;

LBL_ERR:
mp_clear(&t);
return err;
}
Expand Down
8 changes: 5 additions & 3 deletions bn_mp_to_unsigned_bin.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@ mp_err mp_to_unsigned_bin(const mp_int *a, unsigned char *b)
b[x++] = (unsigned char)(t.dp[0] | ((t.dp[1] & 1u) << 7));
#endif
if ((err = mp_div_2d(&t, 8, &t, NULL)) != MP_OKAY) {
mp_clear(&t);
return err;
goto LBL_ERR;
}
}
s_mp_reverse(b, x);
err = MP_OKAY;

LBL_ERR:
mp_clear(&t);
return MP_OKAY;
return err;
}
#endif
Loading