Debian Codegraph @

libtommath/tommath_private.h

raw
1 /* LibTomMath, multiple-precision integer library -- Tom St Denis */
2 /* SPDX-License-Identifier: Unlicense */
3
4 #ifndef TOMMATH_PRIV_H_
5 #define TOMMATH_PRIV_H_
6
7 #include <tommath.h>
8 #include "tommath_class.h"
9
10 /*
11 * Private symbols
12 * ---------------
13 *
14 * On Unix symbols can be marked as hidden if libtommath is compiled
15 * as a shared object. By default, symbols are visible.
16 * As of now, this feature is opt-in via the MP_PRIVATE_SYMBOLS define.
17 *
18 * On Win32 a .def file must be used to specify the exported symbols.
19 */
20 #if defined (MP_PRIVATE_SYMBOLS) && defined(__GNUC__) && __GNUC__ >= 4
21 # define MP_PRIVATE __attribute__ ((visibility ("hidden")))
22 #else
23 # define MP_PRIVATE
24 #endif
25
26 /* Hardening libtommath
27 * --------------------
28 *
29 * By default memory is zeroed before calling
30 * MP_FREE to avoid leaking data. This is good
31 * practice in cryptographical applications.
32 *
33 * Note however that memory allocators used
34 * in cryptographical applications can often
35 * be configured by itself to clear memory,
36 * rendering the clearing in tommath unnecessary.
37 * See for example https://github.com/GrapheneOS/hardened_malloc
38 * and the option CONFIG_ZERO_ON_FREE.
39 *
40 * Furthermore there are applications which
41 * value performance more and want this
42 * feature to be disabled. For such applications
43 * define MP_NO_ZERO_ON_FREE during compilation.
44 */
45 #ifdef MP_NO_ZERO_ON_FREE
46 # define MP_FREE_BUFFER(mem, size) MP_FREE((mem), (size))
47 # define MP_FREE_DIGITS(mem, digits) MP_FREE((mem), sizeof (mp_digit) * (size_t)(digits))
48 #else
49 # define MP_FREE_BUFFER(mem, size) \
50 do { \
51 size_t fs_ = (size); \
52 void* fm_ = (mem); \
53 if (fm_ != NULL) { \
54 MP_ZERO_BUFFER(fm_, fs_); \
55 MP_FREE(fm_, fs_); \
56 } \
57 } while (0)
58 # define MP_FREE_DIGITS(mem, digits) \
59 do { \
60 int fd_ = (digits); \
61 void* fm_ = (mem); \
62 if (fm_ != NULL) { \
63 size_t fs_ = sizeof (mp_digit) * (size_t)fd_; \
64 MP_ZERO_BUFFER(fm_, fs_); \
65 MP_FREE(fm_, fs_); \
66 } \
67 } while (0)
68 #endif
69
70 #ifdef MP_USE_MEMSET
71 # include <string.h>
72 # define MP_ZERO_BUFFER(mem, size) memset((mem), 0, (size))
73 # define MP_ZERO_DIGITS(mem, digits) \
74 do { \
75 int zd_ = (digits); \
76 if (zd_ > 0) { \
77 memset((mem), 0, sizeof(mp_digit) * (size_t)zd_); \
78 } \
79 } while (0)
80 #else
81 # define MP_ZERO_BUFFER(mem, size) \
82 do { \
83 size_t zs_ = (size); \
84 char* zm_ = (char*)(mem); \
85 while (zs_-- > 0u) { \
86 *zm_++ = '\0'; \
87 } \
88 } while (0)
89 # define MP_ZERO_DIGITS(mem, digits) \
90 do { \
91 int zd_ = (digits); \
92 mp_digit* zm_ = (mem); \
93 while (zd_-- > 0) { \
94 *zm_++ = 0; \
95 } \
96 } while (0)
97 #endif
98
99 /* Tunable cutoffs
100 * ---------------
101 *
102 * - In the default settings, a cutoff X can be modified at runtime
103 * by adjusting the corresponding X_CUTOFF variable.
104 *
105 * - Tunability of the library can be disabled at compile time
106 * by defining the MP_FIXED_CUTOFFS macro.
107 *
108 * - There is an additional file tommath_cutoffs.h, which defines
109 * the default cutoffs. These can be adjusted manually or by the
110 * autotuner.
111 *
112 */
113
114 #ifdef MP_FIXED_CUTOFFS
115 # include "tommath_cutoffs.h"
116 # define MP_KARATSUBA_MUL_CUTOFF MP_DEFAULT_KARATSUBA_MUL_CUTOFF
117 # define MP_KARATSUBA_SQR_CUTOFF MP_DEFAULT_KARATSUBA_SQR_CUTOFF
118 # define MP_TOOM_MUL_CUTOFF MP_DEFAULT_TOOM_MUL_CUTOFF
119 # define MP_TOOM_SQR_CUTOFF MP_DEFAULT_TOOM_SQR_CUTOFF
120 #else
121 # define MP_KARATSUBA_MUL_CUTOFF KARATSUBA_MUL_CUTOFF
122 # define MP_KARATSUBA_SQR_CUTOFF KARATSUBA_SQR_CUTOFF
123 # define MP_TOOM_MUL_CUTOFF TOOM_MUL_CUTOFF
124 # define MP_TOOM_SQR_CUTOFF TOOM_SQR_CUTOFF
125 #endif
126
127 /* define heap macros */
128 #ifndef MP_MALLOC
129 /* default to libc stuff */
130 # include <stdlib.h>
131 # define MP_MALLOC(size) malloc(size)
132 # define MP_REALLOC(mem, oldsize, newsize) realloc((mem), (newsize))
133 # define MP_CALLOC(nmemb, size) calloc((nmemb), (size))
134 # define MP_FREE(mem, size) free(mem)
135 #elif 0
136 /* prototypes for our heap functions */
137 extern void *MP_MALLOC(size_t size);
138 extern void *MP_REALLOC(void *mem, size_t oldsize, size_t newsize);
139 extern void *MP_CALLOC(size_t nmemb, size_t size);
140 extern void MP_FREE(void *mem, size_t size);
141 #endif
142
143 /* feature detection macro */
144 #ifdef _MSC_VER
145 /* Prevent false positive: not enough arguments for function-like macro invocation */
146 #pragma warning(disable: 4003)
147 #endif
148 #define MP_STRINGIZE(x) MP__STRINGIZE(x)
149 #define MP__STRINGIZE(x) ""#x""
150 #define MP_HAS(x) (sizeof(MP_STRINGIZE(BN_##x##_C)) == 1u)
151
152 /* TODO: Remove private_mp_word as soon as deprecated mp_word is removed from tommath. */
153 #undef mp_word
154 typedef private_mp_word mp_word;
155
156 #define MP_MIN(x, y) (((x) < (y)) ? (x) : (y))
157 #define MP_MAX(x, y) (((x) > (y)) ? (x) : (y))
158
159 #define MP_IS_2EXPT(x) (((x) != 0u) && (((x) & ((x) - 1u)) == 0u))
160
161 /* Static assertion */
162 #define MP_STATIC_ASSERT(msg, cond) typedef char mp_static_assert_##msg[(cond) ? 1 : -1];
163
164 /* ---> Basic Manipulations <--- */
165 #define MP_IS_ZERO(a) ((a)->used == 0)
166 #define MP_IS_EVEN(a) (((a)->used == 0) || (((a)->dp[0] & 1u) == 0u))
167 #define MP_IS_ODD(a) (((a)->used > 0) && (((a)->dp[0] & 1u) == 1u))
168
169 #define MP_SIZEOF_BITS(type) ((size_t)CHAR_BIT * sizeof(type))
170 #define MP_MAXFAST (int)(1uL << (MP_SIZEOF_BITS(mp_word) - (2u * (size_t)MP_DIGIT_BIT)))
171
172 /* TODO: Remove PRIVATE_MP_WARRAY as soon as deprecated MP_WARRAY is removed from tommath.h */
173 #undef MP_WARRAY
174 #define MP_WARRAY PRIVATE_MP_WARRAY
175
176 /* TODO: Remove PRIVATE_MP_PREC as soon as deprecated MP_PREC is removed from tommath.h */
177 #ifdef PRIVATE_MP_PREC
178 # undef MP_PREC
179 # define MP_PREC PRIVATE_MP_PREC
180 #endif
181
182 /* Minimum number of available digits in mp_int, MP_PREC >= MP_MIN_PREC */
183 #define MP_MIN_PREC ((((int)MP_SIZEOF_BITS(Tcl_WideInt) + MP_DIGIT_BIT) - 1) / MP_DIGIT_BIT)
184
185 MP_STATIC_ASSERT(prec_geq_min_prec, MP_PREC >= MP_MIN_PREC)
186
187 /* random number source */
188 extern MP_PRIVATE mp_err(*s_mp_rand_source)(void *out, size_t size);
189
190 #ifdef __cplusplus
191 extern "C" {
192 #endif
193 /* lowlevel functions, do not call! */
194 MP_PRIVATE mp_bool s_mp_get_bit(const mp_int *a, unsigned int b);
195 MP_PRIVATE int s_mp_log_2expt(const mp_int *a, mp_digit base) MP_WUR;
196 MP_PRIVATE int s_mp_log_d(mp_digit base, mp_digit n) MP_WUR;
197 MP_PRIVATE mp_err s_mp_add(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR;
198 MP_PRIVATE mp_err s_mp_sub(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR;
199 MP_PRIVATE mp_err s_mp_div_3(const mp_int *a, mp_int *c, mp_digit *d) MP_WUR;
200 MP_PRIVATE mp_err s_mp_mul_digs_fast(const mp_int *a, const mp_int *b, mp_int *c, int digs) MP_WUR;
201 MP_PRIVATE mp_err s_mp_mul_digs(const mp_int *a, const mp_int *b, mp_int *c, int digs) MP_WUR;
202 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;
203 MP_PRIVATE mp_err s_mp_mul_high_digs(const mp_int *a, const mp_int *b, mp_int *c, int digs) MP_WUR;
204 MP_PRIVATE mp_err s_mp_sqr_fast(const mp_int *a, mp_int *b) MP_WUR;
205 MP_PRIVATE mp_err s_mp_sqr(const mp_int *a, mp_int *b) MP_WUR;
206 MP_PRIVATE mp_err s_mp_balance_mul(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR;
207 MP_PRIVATE mp_err s_mp_karatsuba_mul(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR;
208 MP_PRIVATE mp_err s_mp_toom_mul(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR;
209 MP_PRIVATE mp_err s_mp_karatsuba_sqr(const mp_int *a, mp_int *b) MP_WUR;
210 MP_PRIVATE mp_err s_mp_toom_sqr(const mp_int *a, mp_int *b) MP_WUR;
211 MP_PRIVATE mp_err s_mp_invmod_fast(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR;
212 MP_PRIVATE mp_err s_mp_invmod_slow(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR;
213 MP_PRIVATE mp_err s_mp_montgomery_reduce_fast(mp_int *x, const mp_int *n, mp_digit rho) MP_WUR;
214 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;
215 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;
216 MP_PRIVATE mp_err s_mp_log(const mp_int *a, mp_digit base, int *c) MP_WUR;
217 MP_PRIVATE mp_err s_mp_rand_platform(void *p, size_t n) MP_WUR;
218 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);
219 MP_PRIVATE void s_mp_reverse(unsigned char *s, size_t len);
220 MP_PRIVATE mp_err s_mp_prime_is_divisible(const mp_int *a, mp_bool *result);
221
222 /* TODO: jenkins prng is not thread safe as of now */
223 MP_PRIVATE mp_err s_mp_rand_jenkins(void *p, size_t n) MP_WUR;
224 #ifndef MP_NO_STDINT
225 MP_PRIVATE void s_mp_rand_jenkins_init(uint64_t seed);
226 #endif
227
228 extern MP_PRIVATE const char *const mp_s_rmap;
229 extern MP_PRIVATE const unsigned char mp_s_rmap_reverse[];
230 extern MP_PRIVATE const size_t mp_s_rmap_reverse_sz;
231 extern MP_PRIVATE const mp_digit *s_mp_prime_tab;
232
233 /* deprecated functions */
234 #if 0
235 MP_DEPRECATED(s_mp_invmod_fast) mp_err fast_mp_invmod(const mp_int *a, const mp_int *b, mp_int *c);
236 MP_DEPRECATED(s_mp_montgomery_reduce_fast) mp_err fast_mp_montgomery_reduce(mp_int *x, const mp_int *n,
237 mp_digit rho);
238 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,
239 int digs);
240 MP_DEPRECATED(s_mp_mul_high_digs_fast) mp_err fast_s_mp_mul_high_digs(const mp_int *a, const mp_int *b,
241 mp_int *c,
242 int digs);
243 MP_DEPRECATED(s_mp_sqr_fast) mp_err fast_s_mp_sqr(const mp_int *a, mp_int *b);
244 MP_DEPRECATED(s_mp_balance_mul) mp_err mp_balance_mul(const mp_int *a, const mp_int *b, mp_int *c);
245 MP_DEPRECATED(s_mp_exptmod_fast) mp_err mp_exptmod_fast(const mp_int *G, const mp_int *X, const mp_int *P,
246 mp_int *Y,
247 int redmode);
248 MP_DEPRECATED(s_mp_invmod_slow) mp_err mp_invmod_slow(const mp_int *a, const mp_int *b, mp_int *c);
249 MP_DEPRECATED(s_mp_karatsuba_mul) mp_err mp_karatsuba_mul(const mp_int *a, const mp_int *b, mp_int *c);
250 MP_DEPRECATED(s_mp_karatsuba_sqr) mp_err mp_karatsuba_sqr(const mp_int *a, mp_int *b);
251 MP_DEPRECATED(s_mp_toom_mul) mp_err mp_toom_mul(const mp_int *a, const mp_int *b, mp_int *c);
252 MP_DEPRECATED(s_mp_toom_sqr) mp_err mp_toom_sqr(const mp_int *a, mp_int *b);
253 MP_DEPRECATED(s_mp_reverse) void bn_reverse(unsigned char *s, int len);
254
255 #ifdef __cplusplus
256 }
257 #endif
258 #endif
259
260 #define MP_GET_ENDIANNESS(x) \
261 do{\
262 short n = 0x1; \
263 char *p = (char *)&n; \
264 x = (p[0] == '\x01') ? MP_LITTLE_ENDIAN : MP_BIG_ENDIAN; \
265 } while (0)
266
267 /* code-generating macros */
268 #define MP_SET_UNSIGNED(name, type) \
269 void name(mp_int * a, type b) \
270 { \
271 int i = 0; \
272 while (b != 0u) { \
273 a->dp[i++] = ((mp_digit)b & MP_MASK); \
274 if (MP_SIZEOF_BITS(type) <= MP_DIGIT_BIT) { break; } \
275 b >>= ((MP_SIZEOF_BITS(type) <= MP_DIGIT_BIT) ? 0 : MP_DIGIT_BIT); \
276 } \
277 a->used = i; \
278 a->sign = MP_ZPOS; \
279 MP_ZERO_DIGITS(a->dp + a->used, a->alloc - a->used); \
280 }
281
282 #define MP_SET_SIGNED(name, uname, type, utype) \
283 void name(mp_int * a, type b) \
284 { \
285 uname(a, (b < 0) ? -(utype)b : (utype)b); \
286 if (b < 0) { a->sign = MP_NEG; } \
287 }
288
289 #define MP_INIT_INT(name , set, type) \
290 mp_err name(mp_int * a, type b) \
291 { \
292 mp_err err; \
293 if ((err = mp_init(a)) != MP_OKAY) { \
294 return err; \
295 } \
296 set(a, b); \
297 return MP_OKAY; \
298 }
299
300 #define MP_GET_MAG(name, type) \
301 type name(const mp_int* a) \
302 { \
303 unsigned i = MP_MIN((unsigned)a->used, (unsigned)((MP_SIZEOF_BITS(type) + MP_DIGIT_BIT - 1) / MP_DIGIT_BIT)); \
304 type res = 0u; \
305 while (i --> 0u) { \
306 res <<= ((MP_SIZEOF_BITS(type) <= MP_DIGIT_BIT) ? 0 : MP_DIGIT_BIT); \
307 res |= (type)a->dp[i]; \
308 if (MP_SIZEOF_BITS(type) <= MP_DIGIT_BIT) { break; } \
309 } \
310 return res; \
311 }
312
313 #define MP_GET_SIGNED(name, mag, type, utype) \
314 type name(const mp_int* a) \
315 { \
316 utype res = mag(a); \
317 return (a->sign == MP_NEG) ? (type)-res : (type)res; \
318 }
319
320 #undef mp_isodd
321 #define mp_isodd TclBN_mp_isodd
322
323 #endif