blob: 4b2af6d2a65c46422cc76798d66c6fd702008b5a [file] [log] [blame]
rjw6c1fd8f2022-11-30 14:33:01 +08001/**
2 * \file bignum.h
3 */
4#ifndef XYSSL_BIGNUM_H
5#define XYSSL_BIGNUM_H
6
7#include <stdio.h>
8
9#define XYSSL_ERR_MPI_FILE_IO_ERROR -0x0002
10#define XYSSL_ERR_MPI_BAD_INPUT_DATA -0x0004
11#define XYSSL_ERR_MPI_INVALID_CHARACTER -0x0006
12#define XYSSL_ERR_MPI_BUFFER_TOO_SMALL -0x0008
13#define XYSSL_ERR_MPI_NEGATIVE_VALUE -0x000A
14#define XYSSL_ERR_MPI_DIVISION_BY_ZERO -0x000C
15#define XYSSL_ERR_MPI_NOT_ACCEPTABLE -0x000E
16
17#define MPI_CHK(f) if( ( ret = f ) != 0 ) goto cleanup
18
19/*
20 * Define the base integer type, architecture-wise
21 */
22#if defined(XYSSL_HAVE_INT8)
23typedef unsigned char t_int;
24typedef unsigned short t_dbl;
25#else
26#if defined(XYSSL_HAVE_INT16)
27typedef unsigned short t_int;
28typedef unsigned long t_dbl;
29#else
30 typedef unsigned long t_int;
31 #if defined(_MSC_VER) && defined(_M_IX86)
32 typedef unsigned __int64 t_dbl;
33 #else
34 #if defined(__amd64__) || defined(__x86_64__) || \
35 defined(__ppc64__) || defined(__powerpc64__) || \
36 defined(__ia64__) || defined(__alpha__)
37 typedef unsigned int t_dbl __attribute__((mode(TI)));
38 #else
39 typedef unsigned long long t_dbl;
40 #endif
41 #endif
42#endif
43#endif
44
45/**
46 * \brief MPI structure
47 */
48typedef struct
49{
50 int s; /*!< integer sign */
51 int n; /*!< total # of limbs */
52 t_int *p; /*!< pointer to limbs */
53}
54mpi;
55
56#ifdef __cplusplus
57extern "C" {
58#endif
59
60/**
61 * \brief Initialize one or more mpi
62 */
63void che_mpi_init( mpi *X, ... );
64
65/**
66 * \brief Unallocate one or more mpi
67 */
68void che_mpi_free( mpi *X, ... );
69
70/**
71 * \brief Enlarge to the specified number of limbs
72 *
73 * \return 0 if successful,
74 * 1 if memory allocation failed
75 */
76int che_mpi_grow( mpi *X, int nblimbs );
77
78/**
79 * \brief Copy the contents of Y into X
80 *
81 * \return 0 if successful,
82 * 1 if memory allocation failed
83 */
84int che_mpi_copy( mpi *X, mpi *Y );
85
86/**
87 * \brief Swap the contents of X and Y
88 */
89void che_mpi_swap( mpi *X, mpi *Y );
90
91/**
92 * \brief Set value from integer
93 *
94 * \return 0 if successful,
95 * 1 if memory allocation failed
96 */
97int che_mpi_lset( mpi *X, int z );
98
99/**
100 * \brief Return the number of least significant bits
101 */
102int che_mpi_lsb( mpi *X );
103
104/**
105 * \brief Return the number of most significant bits
106 */
107int che_mpi_msb( mpi *X );
108
109/**
110 * \brief Return the total size in bytes
111 */
112int che_mpi_size( mpi *X );
113
114/**
115 * \brief Import from an ASCII string
116 *
117 * \param X destination mpi
118 * \param radix input numeric base
119 * \param s null-terminated string buffer
120 *
121 * \return 0 if successful, or an XYSSL_ERR_MPI_XXX error code
122 */
123int che_mpi_read_string( mpi *X, int radix, char *s );
124
125int che_mpi_read_string_ex( mpi *X, char *s, int radix, int s_len );
126int che_mpi_read( mpi *X, unsigned char *s_tmp, int radix, int s_len );
127
128/**
129 * \brief Export into an ASCII string
130 *
131 * \param X source mpi
132 * \param radix output numeric base
133 * \param s string buffer
134 * \param slen string buffer size
135 *
136 * \return 0 if successful, or an XYSSL_ERR_MPI_XXX error code
137 *
138 * \note Call this function with *slen = 0 to obtain the
139 * minimum required buffer size in *slen.
140 */
141int che_mpi_write_string( mpi *X, int radix, char *s, int *slen );
142
143/**
144 * \brief Read X from an opened file
145 *
146 * \param X destination mpi
147 * \param radix input numeric base
148 * \param fin input file handle
149 *
150 * \return 0 if successful, or an XYSSL_ERR_MPI_XXX error code
151 */
152int che_mpi_read_file( mpi *X, int radix, FILE *fin );
153
154/**
155 * \brief Write X into an opened file, or stdout
156 *
157 * \param p prefix, can be NULL
158 * \param X source mpi
159 * \param radix output numeric base
160 * \param fout output file handle
161 *
162 * \return 0 if successful, or an XYSSL_ERR_MPI_XXX error code
163 *
164 * \note Set fout == NULL to print X on the console.
165 */
166int che_mpi_write_file( char *p, mpi *X, int radix, FILE *fout );
167
168/**
169 * \brief Import X from unsigned binary data, big endian
170 *
171 * \param X destination mpi
172 * \param buf input buffer
173 * \param buflen input buffer size
174 *
175 * \return 0 if successful,
176 * 1 if memory allocation failed
177 */
178int che_mpi_read_binary( mpi *X, unsigned char *buf, int buflen );
179
180/**
181 * \brief Export X into unsigned binary data, big endian
182 *
183 * \param X source mpi
184 * \param buf output buffer
185 * \param buflen output buffer size
186 *
187 * \return 0 if successful,
188 * XYSSL_ERR_MPI_BUFFER_TOO_SMALL if buf isn't large enough
189 *
190 * \note Call this function with *buflen = 0 to obtain the
191 * minimum required buffer size in *buflen.
192 */
193int che_mpi_write_binary( mpi *X, unsigned char *buf, int buflen );
194
195/**
196 * \brief Left-shift: X <<= count
197 *
198 * \return 0 if successful,
199 * 1 if memory allocation failed
200 */
201int che_mpi_shift_l( mpi *X, int count );
202
203/**
204 * \brief Right-shift: X >>= count
205 *
206 * \return 0 if successful,
207 * 1 if memory allocation failed
208 */
209int che_mpi_shift_r( mpi *X, int count );
210
211/**
212 * \brief Compare unsigned values
213 *
214 * \return 1 if |X| is greater than |Y|,
215 * -1 if |X| is lesser than |Y| or
216 * 0 if |X| is equal to |Y|
217 */
218int che_mpi_cmp_abs( mpi *X, mpi *Y );
219
220/**
221 * \brief Compare signed values
222 *
223 * \return 1 if X is greater than Y,
224 * -1 if X is lesser than Y or
225 * 0 if X is equal to Y
226 */
227int che_mpi_cmp_mpi( mpi *X, mpi *Y );
228
229/**
230 * \brief Compare signed values
231 *
232 * \return 1 if X is greater than z,
233 * -1 if X is lesser than z or
234 * 0 if X is equal to z
235 */
236int che_mpi_cmp_int( mpi *X, int z );
237
238/**
239 * \brief Unsigned addition: X = |A| + |B|
240 *
241 * \return 0 if successful,
242 * 1 if memory allocation failed
243 */
244int che_mpi_add_abs( mpi *X, mpi *A, mpi *B );
245
246/**
247 * \brief Unsigned substraction: X = |A| - |B|
248 *
249 * \return 0 if successful,
250 * XYSSL_ERR_MPI_NEGATIVE_VALUE if B is greater than A
251 */
252int che_mpi_sub_abs( mpi *X, mpi *A, mpi *B );
253
254/**
255 * \brief Signed addition: X = A + B
256 *
257 * \return 0 if successful,
258 * 1 if memory allocation failed
259 */
260int che_mpi_add_mpi( mpi *X, mpi *A, mpi *B );
261
262/**
263 * \brief Signed substraction: X = A - B
264 *
265 * \return 0 if successful,
266 * 1 if memory allocation failed
267 */
268int che_mpi_sub_mpi( mpi *X, mpi *A, mpi *B );
269
270/**
271 * \brief Signed addition: X = A + b
272 *
273 * \return 0 if successful,
274 * 1 if memory allocation failed
275 */
276int che_mpi_add_int( mpi *X, mpi *A, int b );
277
278/**
279 * \brief Signed substraction: X = A - b
280 *
281 * \return 0 if successful,
282 * 1 if memory allocation failed
283 */
284int che_mpi_sub_int( mpi *X, mpi *A, int b );
285
286/**
287 * \brief Baseline multiplication: X = A * B
288 *
289 * \return 0 if successful,
290 * 1 if memory allocation failed
291 */
292int che_mpi_mul_mpi( mpi *X, mpi *A, mpi *B );
293
294/**
295 * \brief Baseline multiplication: X = A * b
296 *
297 * \return 0 if successful,
298 * 1 if memory allocation failed
299 */
300int che_mpi_mul_int( mpi *X, mpi *A, t_int b );
301
302/**
303 * \brief Division by mpi: A = Q * B + R
304 *
305 * \return 0 if successful,
306 * 1 if memory allocation failed,
307 * XYSSL_ERR_MPI_DIVISION_BY_ZERO if B == 0
308 *
309 * \note Either Q or R can be NULL.
310 */
311int che_mpi_div_mpi( mpi *Q, mpi *R, mpi *A, mpi *B );
312
313/**
314 * \brief Division by int: A = Q * b + R
315 *
316 * \return 0 if successful,
317 * 1 if memory allocation failed,
318 * XYSSL_ERR_MPI_DIVISION_BY_ZERO if b == 0
319 *
320 * \note Either Q or R can be NULL.
321 */
322int che_mpi_div_int( mpi *Q, mpi *R, mpi *A, int b );
323
324/**
325 * \brief Modulo: R = A mod B
326 *
327 * \return 0 if successful,
328 * 1 if memory allocation failed,
329 * XYSSL_ERR_MPI_DIVISION_BY_ZERO if B == 0
330 */
331int che_mpi_mod_mpi( mpi *R, mpi *A, mpi *B );
332
333/**
334 * \brief Modulo: r = A mod b
335 *
336 * \return 0 if successful,
337 * 1 if memory allocation failed,
338 * XYSSL_ERR_MPI_DIVISION_BY_ZERO if b == 0
339 */
340int che_mpi_mod_int( t_int *r, mpi *A, int b );
341
342/**
343 * \brief Sliding-window exponentiation: X = A^E mod N
344 *
345 * \return 0 if successful,
346 * 1 if memory allocation failed,
347 * XYSSL_ERR_MPI_BAD_INPUT_DATA if N is negative or even
348 *
349 * \note _RR is used to avoid re-computing R*R mod N across
350 * multiple calls, which speeds up things a bit. It can
351 * be set to NULL if the extra performance is unneeded.
352 */
353int che_mpi_exp_mod( mpi *X, mpi *A, mpi *E, mpi *N, mpi *_RR );
354
355/**
356 * \brief Greatest common divisor: G = gcd(A, B)
357 *
358 * \return 0 if successful,
359 * 1 if memory allocation failed
360 */
361int che_mpi_gcd( mpi *G, mpi *A, mpi *B );
362
363/**
364 * \brief Modular inverse: X = A^-1 mod N
365 *
366 * \return 0 if successful,
367 * 1 if memory allocation failed,
368 * XYSSL_ERR_MPI_BAD_INPUT_DATA if N is negative or nil
369 * XYSSL_ERR_MPI_NOT_ACCEPTABLE if A has no inverse mod N
370 */
371int che_mpi_inv_mod( mpi *X, mpi *A, mpi *N );
372
373/**
374 * \brief Miller-Rabin primality test
375 *
376 * \return 0 if successful (probably prime),
377 * 1 if memory allocation failed,
378 * XYSSL_ERR_MPI_NOT_ACCEPTABLE if X is not prime
379 */
380int che_mpi_is_prime( mpi *X, int (*f_rng)(void *), void *p_rng );
381
382/**
383 * \brief Prime number generation
384 *
385 * \param X destination mpi
386 * \param nbits required size of X in bits
387 * \param dh_flag if 1, then (X-1)/2 will be prime too
388 * \param f_rng RNG function
389 * \param p_rng RNG parameter
390 *
391 * \return 0 if successful (probably prime),
392 * 1 if memory allocation failed,
393 * XYSSL_ERR_MPI_BAD_INPUT_DATA if nbits is < 3
394 */
395int che_mpi_gen_prime( mpi *X, int nbits, int dh_flag,
396 int (*f_rng)(void *), void *p_rng );
397
398/**
399 * \brief Checkup routine
400 *
401 * \return 0 if successful, or 1 if the test failed
402 */
403int che_mpi_self_test( int verbose );
404
405#ifdef __cplusplus
406}
407#endif
408
409#endif /* bignum.h */