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
2 changes: 1 addition & 1 deletion bn_mp_dr_reduce.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ int mp_dr_reduce(mp_int *x, const mp_int *n, mp_digit k)
*tmpx1++ = mu;

/* zero words above m */
MP_ZERO_DIGITS(tmpx1, x->used - m - 1);
MP_ZERO_DIGITS(tmpx1, (x->used - m) - 1);

/* clamp, sub and return */
mp_clamp(x);
Expand Down
2 changes: 1 addition & 1 deletion bn_mp_ilogb.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ static mp_digit s_digit_ilogb(mp_digit base, mp_digit n)
as is the output of mp_bitcount.
With the same problem: max size is INT_MAX * MP_DIGIT not INT_MAX only!
*/
int mp_ilogb(mp_int *a, mp_digit base, mp_int *c)
int mp_ilogb(const mp_int *a, mp_digit base, mp_int *c)
{
int err, cmp;
unsigned int high, low, mid;
Expand Down
2 changes: 1 addition & 1 deletion bn_mp_rand.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/* LibTomMath, multiple-precision integer library -- Tom St Denis */
/* SPDX-License-Identifier: Unlicense */

int (*s_mp_rand_source)(void *, size_t) = s_mp_rand_platform;
int (*s_mp_rand_source)(void *out, size_t size) = s_mp_rand_platform;

void mp_rand_source(int (*source)(void *out, size_t size))
{
Expand Down
10 changes: 5 additions & 5 deletions bn_s_mp_rand_jenkins.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,21 @@ static uint64_t s_rand_jenkins_val(void)
void s_mp_rand_jenkins_init(uint64_t seed)
{
uint64_t i;
jenkins_x.a = 0xf1ea5eed;
jenkins_x.a = 0xf1ea5eedULL;
jenkins_x.b = jenkins_x.c = jenkins_x.d = seed;
for (i = 0; i < 20; ++i) {
for (i = 0uLL; i < 20uLL; ++i) {
(void)s_rand_jenkins_val();
}
}

int s_mp_rand_jenkins(void *p, size_t n)
{
char *q = (char *)p;
while (n > 0) {
while (n > 0u) {
int i;
uint64_t x = s_rand_jenkins_val();
for (i = 0; i < 8 && n > 0; ++i, --n) {
*q++ = (char)(x & 0xFF);
for (i = 0; (i < 8) && (n > 0u); ++i, --n) {
*q++ = (char)(x & 0xFFuLL);
x >>= 8;
}
}
Expand Down
4 changes: 2 additions & 2 deletions bn_s_mp_rand_platform.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ static int s_read_win_csp(void *p, size_t n)
static int s_read_getrandom(void *p, size_t n)
{
char *q = (char *)p;
while (n > 0) {
while (n > 0u) {
ssize_t ret = getrandom(q, n, 0);
if (ret < 0) {
if (errno == EINTR) {
Expand Down Expand Up @@ -89,7 +89,7 @@ static int s_read_dev_urandom(void *p, size_t n)
} while ((fd == -1) && (errno == EINTR));
if (fd == -1) return MP_ERR;

while (n > 0) {
while (n > 0u) {
ssize_t ret = read(fd, p, n);
if (ret < 0) {
if (errno == EINTR) {
Expand Down
4 changes: 2 additions & 2 deletions tommath.h
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ TOOM_SQR_CUTOFF;
#endif

/* size of comba arrays, should be at least 2 * 2**(BITS_PER_WORD - BITS_PER_DIGIT*2) */
#define PRIVATE_MP_WARRAY (1u << (((CHAR_BIT * sizeof(mp_word)) - (2 * MP_DIGIT_BIT)) + 1))
#define PRIVATE_MP_WARRAY (1uLL << (((CHAR_BIT * sizeof(mp_word)) - (2 * MP_DIGIT_BIT)) + 1))
#define MP_WARRAY (MP_DEPRECATED_PRAGMA("MP_WARRAY is an internal macro") PRIVATE_MP_WARRAY)

#if defined(__GNUC__) && __GNUC__ >= 4
Expand Down Expand Up @@ -630,7 +630,7 @@ MP_WUR MP_DEPRECATED(mp_prime_rand) int mp_prime_random_ex(mp_int *a, int t, int
MP_WUR int mp_prime_rand(mp_int *a, int t, int size, int flags);

/* Integer logarithm to integer base */
MP_WUR int mp_ilogb(mp_int *a, mp_digit base, mp_int *c);
MP_WUR int mp_ilogb(const mp_int *a, mp_digit base, mp_int *c);


/* ---> radix conversion <--- */
Expand Down
52 changes: 44 additions & 8 deletions tommath_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,53 @@ extern "C" {
# define MP_FREE_BUFFER(mem, size) MP_FREE((mem), (size))
# define MP_FREE_DIGITS(mem, digits) MP_FREE((mem), sizeof (mp_digit) * (digits))
#else
# define MP_FREE_BUFFER(mem, size) do { size_t fs_ = (size); void* fm_ = (mem); if (fm_) { MP_ZERO_BUFFER(fm_, fs_); MP_FREE(fm_, fs_); } } while (0)
# define MP_FREE_DIGITS(mem, digits) do { int fd_ = (digits); void* fm_ = (mem); if (fm_) { MP_ZERO_BUFFER(fm_, sizeof (mp_digit) * (size_t)fd_); MP_FREE(fm_, sizeof (mp_digit) * (size_t)fd_); } } while (0)
# define MP_FREE_BUFFER(mem, size) \
do { \
size_t fs_ = (size); \
void* fm_ = (mem); \
if (fm_ != NULL) { \
MP_ZERO_BUFFER(fm_, fs_); \
MP_FREE(fm_, fs_); \
} \
} while (0)
# define MP_FREE_DIGITS(mem, digits) \
do { \
int fd_ = (digits); \
void* fm_ = (mem); \
if (fm_ != NULL) { \
MP_ZERO_BUFFER(fm_, sizeof(mp_digit) * (size_t)fd_); \
MP_FREE(fm_, sizeof(mp_digit) * (size_t)fd_); \
} \
} while (0)
#endif

#ifdef MP_USE_MEMSET
# include <string.h>
# define MP_ZERO_BUFFER(mem, size) memset((mem), 0, (size))
# define MP_ZERO_DIGITS(mem, digits) do { int zd_ = (digits); if (zd_ > 0) { memset((mem), 0, sizeof (mp_digit) * (size_t)zd_); } } while (0)
# define MP_ZERO_DIGITS(mem, digits) \
do { \
int zd_ = (digits); \
if (zd_ > 0) { \
memset((mem), 0, sizeof(mp_digit) * (size_t)zd_); \
} \
} while (0)
#else
# define MP_ZERO_BUFFER(mem, size) do { size_t zs_ = (size); char* zm_ = (char*)(mem); while (zs_-- > 0) { *zm_++ = 0; } } while (0)
# define MP_ZERO_DIGITS(mem, digits) do { int zd_ = (digits); mp_digit* zm_ = (mem); while (zd_-- > 0) { *zm_++ = 0; } } while (0)
# define MP_ZERO_BUFFER(mem, size) \
do { \
size_t zs_ = (size); \
char* zm_ = (char*)(mem); \
while (zs_-- > 0) { \
*zm_++ = 0; \
} \
} while (0)
# define MP_ZERO_DIGITS(mem, digits) \
do { \
int zd_ = (digits); \
mp_digit* zm_ = (mem); \
while (zd_-- > 0) { \
*zm_++ = 0; \
} \
} while (0)
#endif

/* Tunable cutoffs
Expand Down Expand Up @@ -103,13 +139,13 @@ extern void MP_FREE(void *mem, size_t size);
#define MP_IS_ODD(a) (((a)->used > 0) && (((a)->dp[0] & 1u) == 1u))

#define MP_SIZEOF_BITS(type) (CHAR_BIT * sizeof(type))
#define MP_MAXFAST (int)(1u << (MP_SIZEOF_BITS(mp_word) - (2u * (size_t)MP_DIGIT_BIT)))
#define MP_MAXFAST (int)(1uL << (MP_SIZEOF_BITS(mp_word) - (2u * (size_t)MP_DIGIT_BIT)))

/* random number source */
extern int (*s_mp_rand_source)(void *out, size_t size);

/* Minimum number of available digits in mp_int, MP_PREC >= MP_MIN_PREC */
#define MP_MIN_PREC ((CHAR_BIT * (int)sizeof(long long) + MP_DIGIT_BIT - 1) / MP_DIGIT_BIT)
#define MP_MIN_PREC ((((CHAR_BIT * (int)sizeof(long long)) + MP_DIGIT_BIT) - 1) / MP_DIGIT_BIT)

/* lowlevel functions, do not call! */
MP_WUR int s_mp_add(const mp_int *a, const mp_int *b, mp_int *c);
Expand All @@ -135,7 +171,7 @@ void s_mp_reverse(unsigned char *s, int len);

/* TODO: jenkins prng is not thread safe as of now */
MP_WUR int s_mp_rand_jenkins(void *p, size_t n);
void s_mp_rand_jenkins_init(uint64_t);
void s_mp_rand_jenkins_init(uint64_t seed);

extern const char *const mp_s_rmap;
extern const uint8_t mp_s_rmap_reverse[];
Expand Down