1 #include "tommath_private.h"
2 #ifdef BN_DEPRECATED_C
3 /* LibTomMath, multiple-precision integer library -- Tom St Denis */
4 /* SPDX-License-Identifier: Unlicense */
5 
6 #ifdef BN_MP_GET_BIT_C
mp_get_bit(const mp_int * a,int b)7 int mp_get_bit(const mp_int *a, int b)
8 {
9    if (b < 0) {
10       return MP_VAL;
11    }
12    return (s_mp_get_bit(a, (unsigned int)b) == MP_YES) ? MP_YES : MP_NO;
13 }
14 #endif
15 #ifdef BN_MP_JACOBI_C
mp_jacobi(const mp_int * a,const mp_int * n,int * c)16 mp_err mp_jacobi(const mp_int *a, const mp_int *n, int *c)
17 {
18    if (a->sign == MP_NEG) {
19       return MP_VAL;
20    }
21    if (mp_cmp_d(n, 0uL) != MP_GT) {
22       return MP_VAL;
23    }
24    return mp_kronecker(a, n, c);
25 }
26 #endif
27 #ifdef BN_MP_PRIME_RANDOM_EX_C
mp_prime_random_ex(mp_int * a,int t,int size,int flags,private_mp_prime_callback cb,void * dat)28 mp_err mp_prime_random_ex(mp_int *a, int t, int size, int flags, private_mp_prime_callback cb, void *dat)
29 {
30    return s_mp_prime_random_ex(a, t, size, flags, cb, dat);
31 }
32 #endif
33 #ifdef BN_MP_RAND_DIGIT_C
mp_rand_digit(mp_digit * r)34 mp_err mp_rand_digit(mp_digit *r)
35 {
36    mp_err err = s_mp_rand_source(r, sizeof(mp_digit));
37    *r &= MP_MASK;
38    return err;
39 }
40 #endif
41 #ifdef BN_FAST_MP_INVMOD_C
fast_mp_invmod(const mp_int * a,const mp_int * b,mp_int * c)42 mp_err fast_mp_invmod(const mp_int *a, const mp_int *b, mp_int *c)
43 {
44    return s_mp_invmod_fast(a, b, c);
45 }
46 #endif
47 #ifdef BN_FAST_MP_MONTGOMERY_REDUCE_C
fast_mp_montgomery_reduce(mp_int * x,const mp_int * n,mp_digit rho)48 mp_err fast_mp_montgomery_reduce(mp_int *x, const mp_int *n, mp_digit rho)
49 {
50    return s_mp_montgomery_reduce_fast(x, n, rho);
51 }
52 #endif
53 #ifdef BN_FAST_S_MP_MUL_DIGS_C
fast_s_mp_mul_digs(const mp_int * a,const mp_int * b,mp_int * c,int digs)54 mp_err fast_s_mp_mul_digs(const mp_int *a, const mp_int *b, mp_int *c, int digs)
55 {
56    return s_mp_mul_digs_fast(a, b, c, digs);
57 }
58 #endif
59 #ifdef BN_FAST_S_MP_MUL_HIGH_DIGS_C
fast_s_mp_mul_high_digs(const mp_int * a,const mp_int * b,mp_int * c,int digs)60 mp_err fast_s_mp_mul_high_digs(const mp_int *a, const mp_int *b, mp_int *c, int digs)
61 {
62    return s_mp_mul_high_digs_fast(a, b, c, digs);
63 }
64 #endif
65 #ifdef BN_FAST_S_MP_SQR_C
fast_s_mp_sqr(const mp_int * a,mp_int * b)66 mp_err fast_s_mp_sqr(const mp_int *a, mp_int *b)
67 {
68    return s_mp_sqr_fast(a, b);
69 }
70 #endif
71 #ifdef BN_MP_BALANCE_MUL_C
mp_balance_mul(const mp_int * a,const mp_int * b,mp_int * c)72 mp_err mp_balance_mul(const mp_int *a, const mp_int *b, mp_int *c)
73 {
74    return s_mp_balance_mul(a, b, c);
75 }
76 #endif
77 #ifdef BN_MP_EXPTMOD_FAST_C
mp_exptmod_fast(const mp_int * G,const mp_int * X,const mp_int * P,mp_int * Y,int redmode)78 mp_err mp_exptmod_fast(const mp_int *G, const mp_int *X, const mp_int *P, mp_int *Y, int redmode)
79 {
80    return s_mp_exptmod_fast(G, X, P, Y, redmode);
81 }
82 #endif
83 #ifdef BN_MP_INVMOD_SLOW_C
mp_invmod_slow(const mp_int * a,const mp_int * b,mp_int * c)84 mp_err mp_invmod_slow(const mp_int *a, const mp_int *b, mp_int *c)
85 {
86    return s_mp_invmod_slow(a, b, c);
87 }
88 #endif
89 #ifdef BN_MP_KARATSUBA_MUL_C
mp_karatsuba_mul(const mp_int * a,const mp_int * b,mp_int * c)90 mp_err mp_karatsuba_mul(const mp_int *a, const mp_int *b, mp_int *c)
91 {
92    return s_mp_karatsuba_mul(a, b, c);
93 }
94 #endif
95 #ifdef BN_MP_KARATSUBA_SQR_C
mp_karatsuba_sqr(const mp_int * a,mp_int * b)96 mp_err mp_karatsuba_sqr(const mp_int *a, mp_int *b)
97 {
98    return s_mp_karatsuba_sqr(a, b);
99 }
100 #endif
101 #ifdef BN_MP_TOOM_MUL_C
mp_toom_mul(const mp_int * a,const mp_int * b,mp_int * c)102 mp_err mp_toom_mul(const mp_int *a, const mp_int *b, mp_int *c)
103 {
104    return s_mp_toom_mul(a, b, c);
105 }
106 #endif
107 #ifdef BN_MP_TOOM_SQR_C
mp_toom_sqr(const mp_int * a,mp_int * b)108 mp_err mp_toom_sqr(const mp_int *a, mp_int *b)
109 {
110    return s_mp_toom_sqr(a, b);
111 }
112 #endif
113 #ifdef S_MP_REVERSE_C
bn_reverse(unsigned char * s,int len)114 void bn_reverse(unsigned char *s, int len)
115 {
116    if (len > 0) {
117       s_mp_reverse(s, (size_t)len);
118    }
119 }
120 #endif
121 #ifdef BN_MP_TC_AND_C
mp_tc_and(const mp_int * a,const mp_int * b,mp_int * c)122 mp_err mp_tc_and(const mp_int *a, const mp_int *b, mp_int *c)
123 {
124    return mp_and(a, b, c);
125 }
126 #endif
127 #ifdef BN_MP_TC_OR_C
mp_tc_or(const mp_int * a,const mp_int * b,mp_int * c)128 mp_err mp_tc_or(const mp_int *a, const mp_int *b, mp_int *c)
129 {
130    return mp_or(a, b, c);
131 }
132 #endif
133 #ifdef BN_MP_TC_XOR_C
mp_tc_xor(const mp_int * a,const mp_int * b,mp_int * c)134 mp_err mp_tc_xor(const mp_int *a, const mp_int *b, mp_int *c)
135 {
136    return mp_xor(a, b, c);
137 }
138 #endif
139 #ifdef BN_MP_TC_DIV_2D_C
mp_tc_div_2d(const mp_int * a,int b,mp_int * c)140 mp_err mp_tc_div_2d(const mp_int *a, int b, mp_int *c)
141 {
142    return mp_signed_rsh(a, b, c);
143 }
144 #endif
145 #ifdef BN_MP_INIT_SET_INT_C
mp_init_set_int(mp_int * a,unsigned long b)146 mp_err mp_init_set_int(mp_int *a, unsigned long b)
147 {
148    return mp_init_u32(a, (uint32_t)b);
149 }
150 #endif
151 #ifdef BN_MP_SET_INT_C
mp_set_int(mp_int * a,unsigned long b)152 mp_err mp_set_int(mp_int *a, unsigned long b)
153 {
154    mp_set_u32(a, (uint32_t)b);
155    return MP_OKAY;
156 }
157 #endif
158 #ifdef BN_MP_SET_LONG_C
mp_set_long(mp_int * a,unsigned long b)159 mp_err mp_set_long(mp_int *a, unsigned long b)
160 {
161    mp_set_u64(a, b);
162    return MP_OKAY;
163 }
164 #endif
165 #ifdef BN_MP_SET_LONG_LONG_C
mp_set_long_long(mp_int * a,unsigned long long b)166 mp_err mp_set_long_long(mp_int *a, unsigned long long b)
167 {
168    mp_set_u64(a, b);
169    return MP_OKAY;
170 }
171 #endif
172 #ifdef BN_MP_GET_INT_C
mp_get_int(const mp_int * a)173 unsigned long mp_get_int(const mp_int *a)
174 {
175    return (unsigned long)mp_get_mag_u32(a);
176 }
177 #endif
178 #ifdef BN_MP_GET_LONG_C
mp_get_long(const mp_int * a)179 unsigned long mp_get_long(const mp_int *a)
180 {
181    return (unsigned long)mp_get_mag_ul(a);
182 }
183 #endif
184 #ifdef BN_MP_GET_LONG_LONG_C
mp_get_long_long(const mp_int * a)185 unsigned long long mp_get_long_long(const mp_int *a)
186 {
187    return mp_get_mag_ull(a);
188 }
189 #endif
190 #ifdef BN_MP_PRIME_IS_DIVISIBLE_C
mp_prime_is_divisible(const mp_int * a,mp_bool * result)191 mp_err mp_prime_is_divisible(const mp_int *a, mp_bool *result)
192 {
193    return s_mp_prime_is_divisible(a, result);
194 }
195 #endif
196 #ifdef BN_MP_EXPT_D_EX_C
mp_expt_d_ex(const mp_int * a,mp_digit b,mp_int * c,int fast)197 mp_err mp_expt_d_ex(const mp_int *a, mp_digit b, mp_int *c, int fast)
198 {
199    (void)fast;
200    if (b > MP_MIN(MP_DIGIT_MAX, UINT32_MAX)) {
201       return MP_VAL;
202    }
203    return mp_expt_u32(a, (uint32_t)b, c);
204 }
205 #endif
206 #ifdef BN_MP_EXPT_D_C
mp_expt_d(const mp_int * a,mp_digit b,mp_int * c)207 mp_err mp_expt_d(const mp_int *a, mp_digit b, mp_int *c)
208 {
209    if (b > MP_MIN(MP_DIGIT_MAX, UINT32_MAX)) {
210       return MP_VAL;
211    }
212    return mp_expt_u32(a, (uint32_t)b, c);
213 }
214 #endif
215 #ifdef BN_MP_N_ROOT_EX_C
mp_n_root_ex(const mp_int * a,mp_digit b,mp_int * c,int fast)216 mp_err mp_n_root_ex(const mp_int *a, mp_digit b, mp_int *c, int fast)
217 {
218    (void)fast;
219    if (b > MP_MIN(MP_DIGIT_MAX, UINT32_MAX)) {
220       return MP_VAL;
221    }
222    return mp_root_u32(a, (uint32_t)b, c);
223 }
224 #endif
225 #ifdef BN_MP_N_ROOT_C
mp_n_root(const mp_int * a,mp_digit b,mp_int * c)226 mp_err mp_n_root(const mp_int *a, mp_digit b, mp_int *c)
227 {
228    if (b > MP_MIN(MP_DIGIT_MAX, UINT32_MAX)) {
229       return MP_VAL;
230    }
231    return mp_root_u32(a, (uint32_t)b, c);
232 }
233 #endif
234 #ifdef BN_MP_UNSIGNED_BIN_SIZE_C
mp_unsigned_bin_size(const mp_int * a)235 int mp_unsigned_bin_size(const mp_int *a)
236 {
237    return (int)mp_ubin_size(a);
238 }
239 #endif
240 #ifdef BN_MP_READ_UNSIGNED_BIN_C
mp_read_unsigned_bin(mp_int * a,const unsigned char * b,int c)241 mp_err mp_read_unsigned_bin(mp_int *a, const unsigned char *b, int c)
242 {
243    return mp_from_ubin(a, b, (size_t) c);
244 }
245 #endif
246 #ifdef BN_MP_TO_UNSIGNED_BIN_C
mp_to_unsigned_bin(const mp_int * a,unsigned char * b)247 mp_err mp_to_unsigned_bin(const mp_int *a, unsigned char *b)
248 {
249    return mp_to_ubin(a, b, SIZE_MAX, NULL);
250 }
251 #endif
252 #ifdef BN_MP_TO_UNSIGNED_BIN_N_C
mp_to_unsigned_bin_n(const mp_int * a,unsigned char * b,unsigned long * outlen)253 mp_err mp_to_unsigned_bin_n(const mp_int *a, unsigned char *b, unsigned long *outlen)
254 {
255    size_t n = mp_ubin_size(a);
256    if (*outlen < (unsigned long)n) {
257       return MP_VAL;
258    }
259    *outlen = (unsigned long)n;
260    return mp_to_ubin(a, b, n, NULL);
261 }
262 #endif
263 #ifdef BN_MP_SIGNED_BIN_SIZE_C
mp_signed_bin_size(const mp_int * a)264 int mp_signed_bin_size(const mp_int *a)
265 {
266    return (int)mp_sbin_size(a);
267 }
268 #endif
269 #ifdef BN_MP_READ_SIGNED_BIN_C
mp_read_signed_bin(mp_int * a,const unsigned char * b,int c)270 mp_err mp_read_signed_bin(mp_int *a, const unsigned char *b, int c)
271 {
272    return mp_from_sbin(a, b, (size_t) c);
273 }
274 #endif
275 #ifdef BN_MP_TO_SIGNED_BIN_C
mp_to_signed_bin(const mp_int * a,unsigned char * b)276 mp_err mp_to_signed_bin(const mp_int *a, unsigned char *b)
277 {
278    return mp_to_sbin(a, b, SIZE_MAX, NULL);
279 }
280 #endif
281 #ifdef BN_MP_TO_SIGNED_BIN_N_C
mp_to_signed_bin_n(const mp_int * a,unsigned char * b,unsigned long * outlen)282 mp_err mp_to_signed_bin_n(const mp_int *a, unsigned char *b, unsigned long *outlen)
283 {
284    size_t n = mp_sbin_size(a);
285    if (*outlen < (unsigned long)n) {
286       return MP_VAL;
287    }
288    *outlen = (unsigned long)n;
289    return mp_to_sbin(a, b, n, NULL);
290 }
291 #endif
292 #ifdef BN_MP_TORADIX_N_C
mp_toradix_n(const mp_int * a,char * str,int radix,int maxlen)293 mp_err mp_toradix_n(const mp_int *a, char *str, int radix, int maxlen)
294 {
295    if (maxlen < 0) {
296       return MP_VAL;
297    }
298    return mp_to_radix(a, str, (size_t)maxlen, NULL, radix);
299 }
300 #endif
301 #ifdef BN_MP_TORADIX_C
mp_toradix(const mp_int * a,char * str,int radix)302 mp_err mp_toradix(const mp_int *a, char *str, int radix)
303 {
304    return mp_to_radix(a, str, SIZE_MAX, NULL, radix);
305 }
306 #endif
307 #ifdef BN_MP_IMPORT_C
mp_import(mp_int * rop,size_t count,int order,size_t size,int endian,size_t nails,const void * op)308 mp_err mp_import(mp_int *rop, size_t count, int order, size_t size, int endian, size_t nails,
309                  const void *op)
310 {
311    return mp_unpack(rop, count, order, size, endian, nails, op);
312 }
313 #endif
314 #ifdef BN_MP_EXPORT_C
mp_export(void * rop,size_t * countp,int order,size_t size,int endian,size_t nails,const mp_int * op)315 mp_err mp_export(void *rop, size_t *countp, int order, size_t size,
316                  int endian, size_t nails, const mp_int *op)
317 {
318    return mp_pack(rop, SIZE_MAX, countp, order, size, endian, nails, op);
319 }
320 #endif
321 #endif
322