2019-04-07 13:29:11 +00:00
|
|
|
/* LibTomMath, multiple-precision integer library -- Tom St Denis */
|
|
|
|
/* SPDX-License-Identifier: Unlicense */
|
|
|
|
|
2015-11-12 00:49:07 +00:00
|
|
|
#ifndef TOMMATH_PRIV_H_
|
|
|
|
#define TOMMATH_PRIV_H_
|
|
|
|
|
2018-05-02 19:43:17 +00:00
|
|
|
#include "tommath.h"
|
2019-05-19 10:53:49 +00:00
|
|
|
#include "tommath_class.h"
|
2015-11-12 00:49:07 +00:00
|
|
|
|
2019-05-20 04:57:20 +00:00
|
|
|
/*
|
|
|
|
* Private symbols
|
|
|
|
* ---------------
|
|
|
|
*
|
|
|
|
* On Unix symbols can be marked as hidden if libtommath is compiled
|
|
|
|
* as a shared object. By default, symbols are visible.
|
|
|
|
* As of now, this feature is opt-in via the MP_PRIVATE_SYMBOLS define.
|
|
|
|
*
|
|
|
|
* On Win32 a .def file must be used to specify the exported symbols.
|
|
|
|
*/
|
|
|
|
#if defined (MP_PRIVATE_SYMBOLS) && __GNUC__ >= 4
|
|
|
|
# define MP_PRIVATE __attribute__ ((visibility ("hidden")))
|
|
|
|
#else
|
|
|
|
# define MP_PRIVATE
|
|
|
|
#endif
|
|
|
|
|
2019-05-11 08:22:20 +00:00
|
|
|
/* Hardening libtommath
|
|
|
|
* --------------------
|
|
|
|
*
|
|
|
|
* By default memory is zeroed before calling
|
|
|
|
* MP_FREE to avoid leaking data. This is good
|
|
|
|
* practice in cryptographical applications.
|
|
|
|
*
|
|
|
|
* Note however that memory allocators used
|
|
|
|
* in cryptographical applications can often
|
|
|
|
* be configured by itself to clear memory,
|
|
|
|
* rendering the clearing in tommath unnecessary.
|
|
|
|
* See for example https://github.com/GrapheneOS/hardened_malloc
|
|
|
|
* and the option CONFIG_ZERO_ON_FREE.
|
|
|
|
*
|
|
|
|
* Furthermore there are applications which
|
|
|
|
* value performance more and want this
|
|
|
|
* feature to be disabled. For such applications
|
|
|
|
* define MP_NO_ZERO_ON_FREE during compilation.
|
|
|
|
*/
|
|
|
|
#ifdef MP_NO_ZERO_ON_FREE
|
|
|
|
# define MP_FREE_BUFFER(mem, size) MP_FREE((mem), (size))
|
2019-05-13 13:04:30 +00:00
|
|
|
# define MP_FREE_DIGITS(mem, digits) MP_FREE((mem), sizeof (mp_digit) * (size_t)(digits))
|
2019-05-11 08:22:20 +00:00
|
|
|
#else
|
2019-05-13 01:47:24 +00:00
|
|
|
# define MP_FREE_BUFFER(mem, size) \
|
|
|
|
do { \
|
|
|
|
size_t fs_ = (size); \
|
|
|
|
void* fm_ = (mem); \
|
2019-05-13 01:48:20 +00:00
|
|
|
if (fm_ != NULL) { \
|
2019-05-13 01:47:24 +00:00
|
|
|
MP_ZERO_BUFFER(fm_, fs_); \
|
|
|
|
MP_FREE(fm_, fs_); \
|
|
|
|
} \
|
|
|
|
} while (0)
|
|
|
|
# define MP_FREE_DIGITS(mem, digits) \
|
|
|
|
do { \
|
|
|
|
int fd_ = (digits); \
|
|
|
|
void* fm_ = (mem); \
|
2019-05-13 01:48:20 +00:00
|
|
|
if (fm_ != NULL) { \
|
2019-05-13 17:18:09 +00:00
|
|
|
size_t fs_ = sizeof (mp_digit) * (size_t)fd_; \
|
|
|
|
MP_ZERO_BUFFER(fm_, fs_); \
|
|
|
|
MP_FREE(fm_, fs_); \
|
2019-05-13 01:47:24 +00:00
|
|
|
} \
|
|
|
|
} while (0)
|
2019-05-11 08:22:20 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef MP_USE_MEMSET
|
|
|
|
# include <string.h>
|
|
|
|
# define MP_ZERO_BUFFER(mem, size) memset((mem), 0, (size))
|
2019-05-13 01:47:24 +00:00
|
|
|
# define MP_ZERO_DIGITS(mem, digits) \
|
|
|
|
do { \
|
|
|
|
int zd_ = (digits); \
|
|
|
|
if (zd_ > 0) { \
|
|
|
|
memset((mem), 0, sizeof(mp_digit) * (size_t)zd_); \
|
|
|
|
} \
|
|
|
|
} while (0)
|
2019-05-11 08:22:20 +00:00
|
|
|
#else
|
2019-05-13 01:47:24 +00:00
|
|
|
# define MP_ZERO_BUFFER(mem, size) \
|
|
|
|
do { \
|
|
|
|
size_t zs_ = (size); \
|
|
|
|
char* zm_ = (char*)(mem); \
|
2019-05-18 10:37:54 +00:00
|
|
|
while (zs_-- > 0u) { \
|
2019-05-18 09:55:27 +00:00
|
|
|
*zm_++ = '\0'; \
|
2019-05-13 01:47:24 +00:00
|
|
|
} \
|
|
|
|
} while (0)
|
|
|
|
# define MP_ZERO_DIGITS(mem, digits) \
|
|
|
|
do { \
|
|
|
|
int zd_ = (digits); \
|
|
|
|
mp_digit* zm_ = (mem); \
|
|
|
|
while (zd_-- > 0) { \
|
|
|
|
*zm_++ = 0; \
|
|
|
|
} \
|
|
|
|
} while (0)
|
2019-05-11 08:22:20 +00:00
|
|
|
#endif
|
|
|
|
|
2019-05-03 15:39:10 +00:00
|
|
|
/* Tunable cutoffs
|
|
|
|
* ---------------
|
|
|
|
*
|
|
|
|
* - In the default settings, a cutoff X can be modified at runtime
|
|
|
|
* by adjusting the corresponding X_CUTOFF variable.
|
|
|
|
*
|
|
|
|
* - Tunability of the library can be disabled at compile time
|
|
|
|
* by defining the MP_FIXED_CUTOFFS macro.
|
|
|
|
*
|
|
|
|
* - There is an additional file tommath_cutoffs.h, which defines
|
|
|
|
* the default cutoffs. These can be adjusted manually or by the
|
|
|
|
* autotuner.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef MP_FIXED_CUTOFFS
|
|
|
|
# include "tommath_cutoffs.h"
|
|
|
|
# define MP_KARATSUBA_MUL_CUTOFF MP_DEFAULT_KARATSUBA_MUL_CUTOFF
|
|
|
|
# define MP_KARATSUBA_SQR_CUTOFF MP_DEFAULT_KARATSUBA_SQR_CUTOFF
|
|
|
|
# define MP_TOOM_MUL_CUTOFF MP_DEFAULT_TOOM_MUL_CUTOFF
|
|
|
|
# define MP_TOOM_SQR_CUTOFF MP_DEFAULT_TOOM_SQR_CUTOFF
|
|
|
|
#else
|
|
|
|
# define MP_KARATSUBA_MUL_CUTOFF KARATSUBA_MUL_CUTOFF
|
|
|
|
# define MP_KARATSUBA_SQR_CUTOFF KARATSUBA_SQR_CUTOFF
|
|
|
|
# define MP_TOOM_MUL_CUTOFF TOOM_MUL_CUTOFF
|
|
|
|
# define MP_TOOM_SQR_CUTOFF TOOM_SQR_CUTOFF
|
|
|
|
#endif
|
|
|
|
|
2015-11-19 20:53:32 +00:00
|
|
|
/* define heap macros */
|
2019-04-09 09:08:26 +00:00
|
|
|
#ifndef MP_MALLOC
|
2017-08-30 17:15:27 +00:00
|
|
|
/* default to libc stuff */
|
2019-05-07 07:32:59 +00:00
|
|
|
# include <stdlib.h>
|
2019-04-09 09:08:26 +00:00
|
|
|
# define MP_MALLOC(size) malloc(size)
|
2019-05-11 08:22:20 +00:00
|
|
|
# define MP_REALLOC(mem, oldsize, newsize) realloc((mem), (newsize))
|
|
|
|
# define MP_CALLOC(nmemb, size) calloc((nmemb), (size))
|
2019-04-09 09:08:26 +00:00
|
|
|
# define MP_FREE(mem, size) free(mem)
|
2015-11-19 20:53:32 +00:00
|
|
|
#else
|
2017-08-30 17:15:27 +00:00
|
|
|
/* prototypes for our heap functions */
|
2019-04-09 09:08:26 +00:00
|
|
|
extern void *MP_MALLOC(size_t size);
|
|
|
|
extern void *MP_REALLOC(void *mem, size_t oldsize, size_t newsize);
|
|
|
|
extern void *MP_CALLOC(size_t nmemb, size_t size);
|
|
|
|
extern void MP_FREE(void *mem, size_t size);
|
2015-11-12 00:49:07 +00:00
|
|
|
#endif
|
|
|
|
|
2019-04-25 13:36:35 +00:00
|
|
|
/* TODO: Remove PRIVATE_MP_WARRAY as soon as deprecated MP_WARRAY is removed from tommath.h */
|
2019-04-13 15:34:11 +00:00
|
|
|
#undef MP_WARRAY
|
2019-04-25 13:36:35 +00:00
|
|
|
#define MP_WARRAY PRIVATE_MP_WARRAY
|
2019-04-13 15:34:11 +00:00
|
|
|
|
2019-05-17 18:53:24 +00:00
|
|
|
/* TODO: Remove private_mp_word as soon as deprecated mp_word is removed from tommath. */
|
|
|
|
#undef mp_word
|
|
|
|
typedef private_mp_word mp_word;
|
|
|
|
|
2019-04-09 09:08:26 +00:00
|
|
|
#define MP_MIN(x, y) (((x) < (y)) ? (x) : (y))
|
|
|
|
#define MP_MAX(x, y) (((x) > (y)) ? (x) : (y))
|
|
|
|
|
2018-12-28 08:01:01 +00:00
|
|
|
/* ---> Basic Manipulations <--- */
|
2019-04-09 09:08:26 +00:00
|
|
|
#define MP_IS_ZERO(a) ((a)->used == 0)
|
|
|
|
#define MP_IS_EVEN(a) (((a)->used == 0) || (((a)->dp[0] & 1u) == 0u))
|
|
|
|
#define MP_IS_ODD(a) (((a)->used > 0) && (((a)->dp[0] & 1u) == 1u))
|
2018-12-28 08:01:01 +00:00
|
|
|
|
2019-05-18 10:36:45 +00:00
|
|
|
#define MP_SIZEOF_BITS(type) ((size_t)CHAR_BIT * sizeof(type))
|
2019-05-13 15:51:07 +00:00
|
|
|
#define MP_MAXFAST (int)(1uL << (MP_SIZEOF_BITS(mp_word) - (2u * (size_t)MP_DIGIT_BIT)))
|
2019-05-09 20:07:13 +00:00
|
|
|
|
2019-05-10 21:59:46 +00:00
|
|
|
/* Minimum number of available digits in mp_int, MP_PREC >= MP_MIN_PREC */
|
2019-05-13 01:27:02 +00:00
|
|
|
#define MP_MIN_PREC ((((CHAR_BIT * (int)sizeof(long long)) + MP_DIGIT_BIT) - 1) / MP_DIGIT_BIT)
|
2019-05-10 21:59:46 +00:00
|
|
|
|
2019-05-20 04:57:20 +00:00
|
|
|
/* random number source */
|
|
|
|
extern MP_PRIVATE mp_err(*s_mp_rand_source)(void *out, size_t size);
|
|
|
|
|
2015-11-12 00:49:07 +00:00
|
|
|
/* lowlevel functions, do not call! */
|
2019-05-20 04:57:20 +00:00
|
|
|
MP_PRIVATE mp_bool s_mp_get_bit(const mp_int *a, unsigned int b);
|
|
|
|
MP_PRIVATE mp_err s_mp_add(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR;
|
|
|
|
MP_PRIVATE mp_err s_mp_sub(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR;
|
|
|
|
MP_PRIVATE mp_err s_mp_mul_digs_fast(const mp_int *a, const mp_int *b, mp_int *c, int digs) MP_WUR;
|
|
|
|
MP_PRIVATE mp_err s_mp_mul_digs(const mp_int *a, const mp_int *b, mp_int *c, int digs) MP_WUR;
|
|
|
|
MP_PRIVATE mp_err s_mp_mul_high_digs_fast(const mp_int *a, const mp_int *b, mp_int *c, int digs) MP_WUR;
|
|
|
|
MP_PRIVATE mp_err s_mp_mul_high_digs(const mp_int *a, const mp_int *b, mp_int *c, int digs) MP_WUR;
|
|
|
|
MP_PRIVATE mp_err s_mp_sqr_fast(const mp_int *a, mp_int *b) MP_WUR;
|
|
|
|
MP_PRIVATE mp_err s_mp_sqr(const mp_int *a, mp_int *b) MP_WUR;
|
|
|
|
MP_PRIVATE mp_err s_mp_balance_mul(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR;
|
|
|
|
MP_PRIVATE mp_err s_mp_karatsuba_mul(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR;
|
|
|
|
MP_PRIVATE mp_err s_mp_toom_mul(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR;
|
|
|
|
MP_PRIVATE mp_err s_mp_karatsuba_sqr(const mp_int *a, mp_int *b) MP_WUR;
|
|
|
|
MP_PRIVATE mp_err s_mp_toom_sqr(const mp_int *a, mp_int *b) MP_WUR;
|
|
|
|
MP_PRIVATE mp_err s_mp_invmod_fast(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR;
|
|
|
|
MP_PRIVATE mp_err s_mp_invmod_slow(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR;
|
|
|
|
MP_PRIVATE mp_err s_mp_montgomery_reduce_fast(mp_int *x, const mp_int *n, mp_digit rho) MP_WUR;
|
|
|
|
MP_PRIVATE mp_err s_mp_exptmod_fast(const mp_int *G, const mp_int *X, const mp_int *P, mp_int *Y, int redmode) MP_WUR;
|
|
|
|
MP_PRIVATE mp_err s_mp_exptmod(const mp_int *G, const mp_int *X, const mp_int *P, mp_int *Y, int redmode) MP_WUR;
|
|
|
|
MP_PRIVATE mp_err s_mp_rand_platform(void *p, size_t n) MP_WUR;
|
|
|
|
MP_PRIVATE mp_err s_mp_prime_random_ex(mp_int *a, int t, int size, int flags, private_mp_prime_callback cb, void *dat);
|
|
|
|
MP_PRIVATE mp_err s_mp_jacobi(const mp_int *a, const mp_int *n, int *c);
|
|
|
|
MP_PRIVATE void s_mp_reverse(unsigned char *s, int len);
|
2015-11-12 00:49:07 +00:00
|
|
|
|
2019-05-12 09:33:17 +00:00
|
|
|
/* TODO: jenkins prng is not thread safe as of now */
|
2019-05-20 04:57:20 +00:00
|
|
|
MP_PRIVATE mp_err s_mp_rand_jenkins(void *p, size_t n) MP_WUR;
|
|
|
|
MP_PRIVATE void s_mp_rand_jenkins_init(uint64_t seed);
|
2019-05-12 09:33:17 +00:00
|
|
|
|
2019-05-20 04:57:20 +00:00
|
|
|
extern MP_PRIVATE const char *const mp_s_rmap;
|
|
|
|
extern MP_PRIVATE const uint8_t mp_s_rmap_reverse[];
|
|
|
|
extern MP_PRIVATE const size_t mp_s_rmap_reverse_sz;
|
2015-11-12 00:49:07 +00:00
|
|
|
|
|
|
|
/* Fancy macro to set an MPI from another type.
|
|
|
|
* There are several things assumed:
|
2019-03-26 10:30:21 +00:00
|
|
|
* x is the counter
|
2015-11-12 00:49:07 +00:00
|
|
|
* a is the pointer to the MPI
|
|
|
|
* b is the original value that should be set in the MPI.
|
|
|
|
*/
|
|
|
|
#define MP_SET_XLONG(func_name, type) \
|
2019-05-13 16:08:03 +00:00
|
|
|
mp_err func_name (mp_int * a, type b) \
|
2015-11-12 00:49:07 +00:00
|
|
|
{ \
|
2019-03-26 10:30:21 +00:00
|
|
|
int x = 0; \
|
2019-05-10 21:59:46 +00:00
|
|
|
mp_zero(a); \
|
|
|
|
while (b != 0u) { \
|
|
|
|
a->dp[x++] = ((mp_digit)b & MP_MASK); \
|
|
|
|
if (MP_SIZEOF_BITS(b) <= MP_DIGIT_BIT) { break; } \
|
|
|
|
b >>= ((MP_SIZEOF_BITS(b) <= MP_DIGIT_BIT) ? 0 : MP_DIGIT_BIT); \
|
2019-03-26 10:30:21 +00:00
|
|
|
} \
|
2019-05-10 21:59:46 +00:00
|
|
|
a->used = x; \
|
|
|
|
return MP_OKAY; \
|
2015-11-12 00:49:07 +00:00
|
|
|
}
|
|
|
|
|
2019-03-07 03:02:32 +00:00
|
|
|
/* deprecated functions */
|
2019-05-12 22:22:18 +00:00
|
|
|
MP_DEPRECATED(s_mp_invmod_fast) mp_err fast_mp_invmod(const mp_int *a, const mp_int *b, mp_int *c);
|
2019-05-20 04:57:20 +00:00
|
|
|
MP_DEPRECATED(s_mp_montgomery_reduce_fast) mp_err fast_mp_montgomery_reduce(mp_int *x, const mp_int *n,
|
|
|
|
mp_digit rho);
|
|
|
|
MP_DEPRECATED(s_mp_mul_digs_fast) mp_err fast_s_mp_mul_digs(const mp_int *a, const mp_int *b, mp_int *c,
|
|
|
|
int digs);
|
|
|
|
MP_DEPRECATED(s_mp_mul_high_digs_fast) mp_err fast_s_mp_mul_high_digs(const mp_int *a, const mp_int *b,
|
|
|
|
mp_int *c,
|
2019-04-12 12:56:29 +00:00
|
|
|
int digs);
|
2019-05-12 22:22:18 +00:00
|
|
|
MP_DEPRECATED(s_mp_sqr_fast) mp_err fast_s_mp_sqr(const mp_int *a, mp_int *b);
|
|
|
|
MP_DEPRECATED(s_mp_balance_mul) mp_err mp_balance_mul(const mp_int *a, const mp_int *b, mp_int *c);
|
2019-05-20 04:57:20 +00:00
|
|
|
MP_DEPRECATED(s_mp_exptmod_fast) mp_err mp_exptmod_fast(const mp_int *G, const mp_int *X, const mp_int *P,
|
|
|
|
mp_int *Y,
|
2019-04-12 12:56:29 +00:00
|
|
|
int redmode);
|
2019-05-12 22:22:18 +00:00
|
|
|
MP_DEPRECATED(s_mp_invmod_slow) mp_err mp_invmod_slow(const mp_int *a, const mp_int *b, mp_int *c);
|
|
|
|
MP_DEPRECATED(s_mp_karatsuba_mul) mp_err mp_karatsuba_mul(const mp_int *a, const mp_int *b, mp_int *c);
|
|
|
|
MP_DEPRECATED(s_mp_karatsuba_sqr) mp_err mp_karatsuba_sqr(const mp_int *a, mp_int *b);
|
|
|
|
MP_DEPRECATED(s_mp_toom_mul) mp_err mp_toom_mul(const mp_int *a, const mp_int *b, mp_int *c);
|
|
|
|
MP_DEPRECATED(s_mp_toom_sqr) mp_err mp_toom_sqr(const mp_int *a, mp_int *b);
|
2019-04-12 12:56:29 +00:00
|
|
|
MP_DEPRECATED(s_mp_reverse) void bn_reverse(unsigned char *s, int len);
|
2019-03-07 03:02:32 +00:00
|
|
|
|
2015-11-12 00:49:07 +00:00
|
|
|
#endif
|