Skip to content
Closed
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
2 changes: 1 addition & 1 deletion bn_mp_get_long.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ unsigned long mp_get_long(const mp_int *a)
}

/* get number of digits of the lsb we have to read */
i = MIN(a->used, ((((int)sizeof(unsigned long) * CHAR_BIT) + DIGIT_BIT - 1) / DIGIT_BIT)) - 1;
i = MIN(a->used, (((CHAR_BIT * (int)sizeof(unsigned long)) + DIGIT_BIT - 1) / DIGIT_BIT)) - 1;

/* get most significant digit of result */
res = (unsigned long)a->dp[i];
Expand Down
2 changes: 1 addition & 1 deletion bn_mp_get_long_long.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ unsigned long long mp_get_long_long(const mp_int *a)
}

/* get number of digits of the lsb we have to read */
i = MIN(a->used, ((((int)sizeof(unsigned long long) * CHAR_BIT) + DIGIT_BIT - 1) / DIGIT_BIT)) - 1;
i = MIN(a->used, (((CHAR_BIT * (int)sizeof(unsigned long long)) + DIGIT_BIT - 1) / DIGIT_BIT)) - 1;

/* get most significant digit of result */
res = (unsigned long long)a->dp[i];
Expand Down
2 changes: 1 addition & 1 deletion bn_mp_grow.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ int mp_grow(mp_int *a, int size)
* in case the operation failed we don't want
* to overwrite the dp member of a.
*/
tmp = (mp_digit *) XREALLOC(a->dp, sizeof(mp_digit) * (size_t)size);
tmp = (mp_digit *) XREALLOC(a->dp, (size_t)size * sizeof(mp_digit));
if (tmp == NULL) {
/* reallocation failed but "a" is still valid [can be freed] */
return MP_MEM;
Expand Down
2 changes: 1 addition & 1 deletion bn_mp_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ int mp_init(mp_int *a)
int i;

/* allocate memory required and clear it */
a->dp = (mp_digit *) XMALLOC(sizeof(mp_digit) * (size_t)MP_PREC);
a->dp = (mp_digit *) XMALLOC(MP_PREC * sizeof(mp_digit));
if (a->dp == NULL) {
return MP_MEM;
}
Expand Down
2 changes: 1 addition & 1 deletion bn_mp_init_size.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ int mp_init_size(mp_int *a, int size)
size += (MP_PREC * 2) - (size % MP_PREC);

/* alloc mem */
a->dp = (mp_digit *) XMALLOC(sizeof(mp_digit) * (size_t)size);
a->dp = (mp_digit *) XMALLOC((size_t)size * sizeof(mp_digit));
if (a->dp == NULL) {
return MP_MEM;
}
Expand Down
2 changes: 1 addition & 1 deletion bn_mp_montgomery_reduce.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ int mp_montgomery_reduce(mp_int *x, const mp_int *n, mp_digit rho)
if ((digs < (int)MP_WARRAY) &&
(x->used <= (int)MP_WARRAY) &&
(n->used <
(int)(1u << (((size_t)CHAR_BIT * sizeof(mp_word)) - (2u * (size_t)DIGIT_BIT))))) {
(int)(1u << ((CHAR_BIT * sizeof(mp_word)) - (2u * (size_t)DIGIT_BIT))))) {
return fast_mp_montgomery_reduce(x, n, rho);
}

Expand Down
2 changes: 1 addition & 1 deletion bn_mp_mul.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ int mp_mul(const mp_int *a, const mp_int *b, mp_int *c)
#ifdef BN_FAST_S_MP_MUL_DIGS_C
if ((digs < (int)MP_WARRAY) &&
(MIN(a->used, b->used) <=
(int)(1u << (((size_t)CHAR_BIT * sizeof(mp_word)) - (2u * (size_t)DIGIT_BIT))))) {
(int)(1u << ((CHAR_BIT * sizeof(mp_word)) - (2u * (size_t)DIGIT_BIT))))) {
res = fast_s_mp_mul_digs(a, b, c, digs);
} else
#endif
Expand Down
2 changes: 1 addition & 1 deletion bn_mp_prime_is_prime.c
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ int mp_prime_is_prime(const mp_int *a, int t, int *result)
if ((err = mp_rand(&b, 1)) != MP_OKAY) {
goto LBL_B;
}
fips_rand <<= sizeof(mp_digit) * CHAR_BIT;
fips_rand <<= CHAR_BIT * sizeof(mp_digit);
fips_rand |= (unsigned int) b.dp[0];
fips_rand &= mask;
}
Expand Down
6 changes: 3 additions & 3 deletions bn_mp_prime_random_ex.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,13 @@ int mp_prime_random_ex(mp_int *a, int t, int size, int flags, ltm_prime_callback
}

/* calc the maskAND value for the MSbyte*/
maskAND = ((size&7) == 0) ? 0xFF : (0xFF >> (8 - (size & 7)));
maskAND = ((size&7) == 0) ? 0xFF : (unsigned char)(0xFF >> (8 - (size & 7)));

/* calc the maskOR_msb */
maskOR_msb = 0;
maskOR_msb_offset = ((size & 7) == 1) ? 1 : 0;
if ((flags & LTM_PRIME_2MSB_ON) != 0) {
maskOR_msb |= 0x80 >> ((9 - size) & 7);
maskOR_msb |= (unsigned char)(0x80 >> ((9 - size) & 7));
}

/* get the maskOR_lsb */
Expand All @@ -76,7 +76,7 @@ int mp_prime_random_ex(mp_int *a, int t, int size, int flags, ltm_prime_callback

/* work over the MSbyte */
tmp[0] &= maskAND;
tmp[0] |= 1 << ((size - 1) & 7);
tmp[0] |= (unsigned char)(1 << ((size - 1) & 7));

/* mix in the maskORs */
tmp[maskOR_msb_offset] |= maskOR_msb;
Expand Down
2 changes: 1 addition & 1 deletion bn_mp_rand.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ static int s_read_win_csp(mp_digit *p)

static int s_read_getrandom(mp_digit *p)
{
int ret;
ssize_t ret;
do {
ret = getrandom(p, sizeof(*p), 0);
} while ((ret == -1) && (errno == EINTR));
Expand Down
18 changes: 0 additions & 18 deletions bn_mp_set_long.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,7 @@
* SPDX-License-Identifier: Unlicense
*/

/* set a platform dependent unsigned long int */
#if (ULONG_MAX != 0xFFFFFFFFUL) || (DIGIT_BIT < 32)
MP_SET_XLONG(mp_set_long, unsigned long)
#else
int mp_set_long(mp_int *a, unsigned long b)
{
int x = 0;
int res = mp_grow(a, (CHAR_BIT * sizeof(unsigned long) + DIGIT_BIT - 1) / DIGIT_BIT);
if (res == MP_OKAY) {
mp_zero(a);
if (b) {
a->dp[x++] = (mp_digit)b;
}
a->used = x;
}
return res;
}

#endif
#endif

/* ref: $Format:%D$ */
Expand Down
2 changes: 1 addition & 1 deletion bn_mp_shrink.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ int mp_shrink(mp_int *a)
}

if (a->alloc != used) {
if ((tmp = (mp_digit *) XREALLOC(a->dp, sizeof(mp_digit) * (size_t)used)) == NULL) {
if ((tmp = (mp_digit *) XREALLOC(a->dp, (size_t)used * sizeof(mp_digit))) == NULL) {
return MP_MEM;
}
a->dp = tmp;
Expand Down
2 changes: 1 addition & 1 deletion bn_mp_sqr.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ int mp_sqr(const mp_int *a, mp_int *b)
/* can we use the fast comba multiplier? */
if ((((a->used * 2) + 1) < (int)MP_WARRAY) &&
(a->used <
(int)(1u << (((sizeof(mp_word) * (size_t)CHAR_BIT) - (2u * (size_t)DIGIT_BIT)) - 1u)))) {
(int)(1u << (((CHAR_BIT * sizeof(mp_word)) - (2u * (size_t)DIGIT_BIT)) - 1u)))) {
res = fast_s_mp_sqr(a, b);
} else
#endif
Expand Down
4 changes: 2 additions & 2 deletions bn_mp_sub_d.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,13 @@ int mp_sub_d(const mp_int *a, mp_digit b, mp_int *c)

/* subtract first digit */
*tmpc = *tmpa++ - b;
mu = *tmpc >> ((sizeof(mp_digit) * (size_t)CHAR_BIT) - 1u);
mu = *tmpc >> ((CHAR_BIT * sizeof(mp_digit)) - 1u);
*tmpc++ &= MP_MASK;

/* handle rest of the digits */
for (ix = 1; ix < a->used; ix++) {
*tmpc = *tmpa++ - mu;
mu = *tmpc >> ((sizeof(mp_digit) * (size_t)CHAR_BIT) - 1u);
mu = *tmpc >> ((CHAR_BIT * sizeof(mp_digit)) - 1u);
*tmpc++ &= MP_MASK;
}
}
Expand Down
2 changes: 1 addition & 1 deletion bn_s_mp_mul_digs.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ int s_mp_mul_digs(const mp_int *a, const mp_int *b, mp_int *c, int digs)
/* can we use the fast multiplier? */
if ((digs < (int)MP_WARRAY) &&
(MIN(a->used, b->used) <
(int)(1u << (((size_t)CHAR_BIT * sizeof(mp_word)) - (2u * (size_t)DIGIT_BIT))))) {
(int)(1u << ((CHAR_BIT * sizeof(mp_word)) - (2u * (size_t)DIGIT_BIT))))) {
return fast_s_mp_mul_digs(a, b, c, digs);
}

Expand Down
2 changes: 1 addition & 1 deletion bn_s_mp_mul_high_digs.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ int s_mp_mul_high_digs(const mp_int *a, const mp_int *b, mp_int *c, int digs)
/* can we use the fast multiplier? */
#ifdef BN_FAST_S_MP_MUL_HIGH_DIGS_C
if (((a->used + b->used + 1) < (int)MP_WARRAY)
&& (MIN(a->used, b->used) < (int)(1u << (((size_t)CHAR_BIT * sizeof(mp_word)) - (2u * (size_t)DIGIT_BIT))))) {
&& (MIN(a->used, b->used) < (int)(1u << ((CHAR_BIT * sizeof(mp_word)) - (2u * (size_t)DIGIT_BIT))))) {
return fast_s_mp_mul_high_digs(a, b, c, digs);
}
#endif
Expand Down
4 changes: 2 additions & 2 deletions bn_s_mp_sub.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ int s_mp_sub(const mp_int *a, const mp_int *b, mp_int *c)
* if a carry does occur it will propagate all the way to the
* MSB. As a result a single shift is enough to get the carry
*/
u = *tmpc >> (((size_t)CHAR_BIT * sizeof(mp_digit)) - 1u);
u = *tmpc >> ((CHAR_BIT * sizeof(mp_digit)) - 1u);

/* Clear carry from T[i] */
*tmpc++ &= MP_MASK;
Expand All @@ -62,7 +62,7 @@ int s_mp_sub(const mp_int *a, const mp_int *b, mp_int *c)
*tmpc = *tmpa++ - u;

/* U = carry bit of T[i] */
u = *tmpc >> (((size_t)CHAR_BIT * sizeof(mp_digit)) - 1u);
u = *tmpc >> ((CHAR_BIT * sizeof(mp_digit)) - 1u);

/* Clear carry from T[i] */
*tmpc++ &= MP_MASK;
Expand Down
4 changes: 3 additions & 1 deletion makefile_include.mk
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,10 @@ CFLAGS += -I./ -Wall -Wsign-compare -Wextra -Wshadow

ifndef NO_ADDTL_WARNINGS
# additional warnings
CFLAGS += -Wsystem-headers -Wdeclaration-after-statement -Wbad-function-cast -Wcast-align
CFLAGS += -Wsystem-headers
CFLAGS += -Wdeclaration-after-statement -Wbad-function-cast -Wcast-align
CFLAGS += -Wstrict-prototypes -Wpointer-arith
#CFLAGS += -Wconversion -Wsign-conversion
endif

ifdef COMPILE_DEBUG
Expand Down
2 changes: 1 addition & 1 deletion tommath.h
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ extern int KARATSUBA_MUL_CUTOFF,
#endif

/* size of comba arrays, should be at least 2 * 2**(BITS_PER_WORD - BITS_PER_DIGIT*2) */
#define MP_WARRAY (1u << (((sizeof(mp_word) * CHAR_BIT) - (2 * DIGIT_BIT)) + 1))
#define MP_WARRAY (1u << (((CHAR_BIT * sizeof(mp_word)) - (2 * DIGIT_BIT)) + 1))

/* the infamous mp_int structure */
typedef struct {
Expand Down
3 changes: 2 additions & 1 deletion tommath_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ int func_name (mp_int * a, type b) \
mp_zero(a); \
while (b != 0u) { \
a->dp[x++] = ((mp_digit)b & MP_MASK); \
b >>= DIGIT_BIT; \
if ((CHAR_BIT * sizeof (b)) <= DIGIT_BIT) { break; } \
b >>= ((CHAR_BIT * sizeof (b)) <= DIGIT_BIT ? 0 : DIGIT_BIT); \
} \
a->used = x; \
} \
Expand Down