Coverage Report

Created: 2023-03-20 06:28

/src/dropbear/libtommath/tommath_private.h
Line
Count
Source (jump to first uncovered line)
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
0
#  define MP_FREE_DIGITS(mem, digits)                   \
59
0
do {                                                    \
60
0
   int fd_ = (digits);                                  \
61
0
   void* fm_ = (mem);                                   \
62
0
   if (fm_ != NULL) {                                   \
63
0
      size_t fs_ = sizeof (mp_digit) * (size_t)fd_;     \
64
0
      MP_ZERO_BUFFER(fm_, fs_);                         \
65
0
      MP_FREE(fm_, fs_);                                \
66
0
   }                                                    \
67
0
} 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
0
#  define MP_ZERO_BUFFER(mem, size)                     \
82
0
do {                                                    \
83
0
   size_t zs_ = (size);                                 \
84
0
   char* zm_ = (char*)(mem);                            \
85
0
   while (zs_-- > 0u) {                                 \
86
0
      *zm_++ = '\0';                                    \
87
0
   }                                                    \
88
0
} while (0)
89
0
#  define MP_ZERO_DIGITS(mem, digits)                   \
90
0
do {                                                    \
91
0
   int zd_ = (digits);                                  \
92
0
   mp_digit* zm_ = (mem);                               \
93
0
   while (zd_-- > 0) {                                  \
94
0
      *zm_++ = 0;                                       \
95
0
   }                                                    \
96
0
} 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
0
#  define MP_KARATSUBA_MUL_CUTOFF KARATSUBA_MUL_CUTOFF
122
0
#  define MP_KARATSUBA_SQR_CUTOFF KARATSUBA_SQR_CUTOFF
123
0
#  define MP_TOOM_MUL_CUTOFF      TOOM_MUL_CUTOFF
124
0
#  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
#else
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
0
#define MP_STRINGIZE(x)  MP__STRINGIZE(x)
149
0
#define MP__STRINGIZE(x) ""#x""
150
0
#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
0
#define MP_MIN(x, y) (((x) < (y)) ? (x) : (y))
157
0
#define MP_MAX(x, y) (((x) > (y)) ? (x) : (y))
158
159
/* Static assertion */
160
#define MP_STATIC_ASSERT(msg, cond) typedef char mp_static_assert_##msg[(cond) ? 1 : -1];
161
162
/* ---> Basic Manipulations <--- */
163
0
#define MP_IS_ZERO(a) ((a)->used == 0)
164
0
#define MP_IS_EVEN(a) (((a)->used == 0) || (((a)->dp[0] & 1u) == 0u))
165
0
#define MP_IS_ODD(a)  (((a)->used > 0) && (((a)->dp[0] & 1u) == 1u))
166
167
0
#define MP_SIZEOF_BITS(type)    ((size_t)CHAR_BIT * sizeof(type))
168
0
#define MP_MAXFAST              (int)(1uL << (MP_SIZEOF_BITS(mp_word) - (2u * (size_t)MP_DIGIT_BIT)))
169
170
/* TODO: Remove PRIVATE_MP_WARRAY as soon as deprecated MP_WARRAY is removed from tommath.h */
171
#undef MP_WARRAY
172
0
#define MP_WARRAY PRIVATE_MP_WARRAY
173
174
/* TODO: Remove PRIVATE_MP_PREC as soon as deprecated MP_PREC is removed from tommath.h */
175
#ifdef PRIVATE_MP_PREC
176
#   undef MP_PREC
177
0
#   define MP_PREC PRIVATE_MP_PREC
178
#endif
179
180
/* Minimum number of available digits in mp_int, MP_PREC >= MP_MIN_PREC */
181
#define MP_MIN_PREC ((((int)MP_SIZEOF_BITS(long long) + MP_DIGIT_BIT) - 1) / MP_DIGIT_BIT)
182
183
MP_STATIC_ASSERT(prec_geq_min_prec, MP_PREC >= MP_MIN_PREC)
184
185
/* random number source */
186
extern MP_PRIVATE mp_err(*s_mp_rand_source)(void *out, size_t size);
187
188
/* lowlevel functions, do not call! */
189
MP_PRIVATE mp_bool s_mp_get_bit(const mp_int *a, unsigned int b);
190
MP_PRIVATE mp_err s_mp_add(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR;
191
MP_PRIVATE mp_err s_mp_sub(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR;
192
MP_PRIVATE mp_err s_mp_mul_digs_fast(const mp_int *a, const mp_int *b, mp_int *c, int digs) MP_WUR;
193
MP_PRIVATE mp_err s_mp_mul_digs(const mp_int *a, const mp_int *b, mp_int *c, int digs) MP_WUR;
194
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;
195
MP_PRIVATE mp_err s_mp_mul_high_digs(const mp_int *a, const mp_int *b, mp_int *c, int digs) MP_WUR;
196
MP_PRIVATE mp_err s_mp_sqr_fast(const mp_int *a, mp_int *b) MP_WUR;
197
MP_PRIVATE mp_err s_mp_sqr(const mp_int *a, mp_int *b) MP_WUR;
198
MP_PRIVATE mp_err s_mp_balance_mul(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR;
199
MP_PRIVATE mp_err s_mp_karatsuba_mul(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR;
200
MP_PRIVATE mp_err s_mp_toom_mul(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR;
201
MP_PRIVATE mp_err s_mp_karatsuba_sqr(const mp_int *a, mp_int *b) MP_WUR;
202
MP_PRIVATE mp_err s_mp_toom_sqr(const mp_int *a, mp_int *b) MP_WUR;
203
MP_PRIVATE mp_err s_mp_invmod_fast(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR;
204
MP_PRIVATE mp_err s_mp_invmod_slow(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR;
205
MP_PRIVATE mp_err s_mp_montgomery_reduce_fast(mp_int *x, const mp_int *n, mp_digit rho) MP_WUR;
206
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;
207
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;
208
MP_PRIVATE mp_err s_mp_rand_platform(void *p, size_t n) MP_WUR;
209
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);
210
MP_PRIVATE void s_mp_reverse(unsigned char *s, size_t len);
211
MP_PRIVATE mp_err s_mp_prime_is_divisible(const mp_int *a, mp_bool *result);
212
213
/* TODO: jenkins prng is not thread safe as of now */
214
MP_PRIVATE mp_err s_mp_rand_jenkins(void *p, size_t n) MP_WUR;
215
MP_PRIVATE void s_mp_rand_jenkins_init(uint64_t seed);
216
217
extern MP_PRIVATE const char *const mp_s_rmap;
218
extern MP_PRIVATE const uint8_t mp_s_rmap_reverse[];
219
extern MP_PRIVATE const size_t mp_s_rmap_reverse_sz;
220
extern MP_PRIVATE const mp_digit *s_mp_prime_tab;
221
222
/* deprecated functions */
223
MP_DEPRECATED(s_mp_invmod_fast) mp_err fast_mp_invmod(const mp_int *a, const mp_int *b, mp_int *c);
224
MP_DEPRECATED(s_mp_montgomery_reduce_fast) mp_err fast_mp_montgomery_reduce(mp_int *x, const mp_int *n,
225
      mp_digit rho);
226
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,
227
      int digs);
228
MP_DEPRECATED(s_mp_mul_high_digs_fast) mp_err fast_s_mp_mul_high_digs(const mp_int *a, const mp_int *b,
229
      mp_int *c,
230
      int digs);
231
MP_DEPRECATED(s_mp_sqr_fast) mp_err fast_s_mp_sqr(const mp_int *a, mp_int *b);
232
MP_DEPRECATED(s_mp_balance_mul) mp_err mp_balance_mul(const mp_int *a, const mp_int *b, mp_int *c);
233
MP_DEPRECATED(s_mp_exptmod_fast) mp_err mp_exptmod_fast(const mp_int *G, const mp_int *X, const mp_int *P,
234
      mp_int *Y,
235
      int redmode);
236
MP_DEPRECATED(s_mp_invmod_slow) mp_err mp_invmod_slow(const mp_int *a, const mp_int *b, mp_int *c);
237
MP_DEPRECATED(s_mp_karatsuba_mul) mp_err mp_karatsuba_mul(const mp_int *a, const mp_int *b, mp_int *c);
238
MP_DEPRECATED(s_mp_karatsuba_sqr) mp_err mp_karatsuba_sqr(const mp_int *a, mp_int *b);
239
MP_DEPRECATED(s_mp_toom_mul) mp_err mp_toom_mul(const mp_int *a, const mp_int *b, mp_int *c);
240
MP_DEPRECATED(s_mp_toom_sqr) mp_err mp_toom_sqr(const mp_int *a, mp_int *b);
241
MP_DEPRECATED(s_mp_reverse) void bn_reverse(unsigned char *s, int len);
242
243
#define MP_GET_ENDIANNESS(x) \
244
   do{\
245
      int16_t n = 0x1;                                          \
246
      char *p = (char *)&n;                                     \
247
      x = (p[0] == '\x01') ? MP_LITTLE_ENDIAN : MP_BIG_ENDIAN;  \
248
   } while (0)
249
250
/* code-generating macros */
251
#define MP_SET_UNSIGNED(name, type)                                                    \
252
    void name(mp_int * a, type b)                                                      \
253
0
    {                                                                                  \
254
0
        int i = 0;                                                                     \
255
0
        while (b != 0u) {                                                              \
256
0
            a->dp[i++] = ((mp_digit)b & MP_MASK);                                      \
257
0
            if (MP_SIZEOF_BITS(type) <= MP_DIGIT_BIT) { break; }                       \
258
0
            b >>= ((MP_SIZEOF_BITS(type) <= MP_DIGIT_BIT) ? 0 : MP_DIGIT_BIT);         \
259
0
        }                                                                              \
260
0
        a->used = i;                                                                   \
261
0
        a->sign = MP_ZPOS;                                                             \
262
0
        MP_ZERO_DIGITS(a->dp + a->used, a->alloc - a->used);                           \
263
0
    }
Unexecuted instantiation: mp_set_u32
Unexecuted instantiation: mp_set_ul
264
265
#define MP_SET_SIGNED(name, uname, type, utype)          \
266
    void name(mp_int * a, type b)                        \
267
    {                                                    \
268
        uname(a, (b < 0) ? -(utype)b : (utype)b);        \
269
        if (b < 0) { a->sign = MP_NEG; }                 \
270
    }
271
272
#define MP_INIT_INT(name , set, type)                    \
273
    mp_err name(mp_int * a, type b)                      \
274
0
    {                                                    \
275
0
        mp_err err;                                      \
276
0
        if ((err = mp_init(a)) != MP_OKAY) {             \
277
0
            return err;                                  \
278
0
        }                                                \
279
0
        set(a, b);                                       \
280
0
        return MP_OKAY;                                  \
281
0
    }
282
283
#define MP_GET_MAG(name, type)                                                         \
284
    type name(const mp_int* a)                                                         \
285
0
    {                                                                                  \
286
0
        unsigned i = MP_MIN((unsigned)a->used, (unsigned)((MP_SIZEOF_BITS(type) + MP_DIGIT_BIT - 1) / MP_DIGIT_BIT)); \
287
0
        type res = 0u;                                                                 \
288
0
        while (i --> 0u) {                                                             \
289
0
            res <<= ((MP_SIZEOF_BITS(type) <= MP_DIGIT_BIT) ? 0 : MP_DIGIT_BIT);       \
290
0
            res |= (type)a->dp[i];                                                     \
291
0
            if (MP_SIZEOF_BITS(type) <= MP_DIGIT_BIT) { break; }                       \
292
0
        }                                                                              \
293
0
        return res;                                                                    \
294
0
    }
Unexecuted instantiation: mp_get_mag_ul
Unexecuted instantiation: mp_get_mag_u32
295
296
#define MP_GET_SIGNED(name, mag, type, utype)                 \
297
    type name(const mp_int* a)                                \
298
0
    {                                                         \
299
0
        utype res = mag(a);                                   \
300
0
        return (a->sign == MP_NEG) ? (type)-res : (type)res;  \
301
0
    }
Unexecuted instantiation: mp_get_l
Unexecuted instantiation: mp_get_i32
302
303
#endif