blob: d02520d0b277530d7ef45e00ce09a353c211b2c9 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/**
2 * \file bignum.h
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00003 *
4 * Based on XySSL: Copyright (C) 2006-2008 Christophe Devine
5 *
Paul Bakker785a9ee2009-01-25 14:15:10 +00006 * Copyright (C) 2009 Paul Bakker <polarssl_maintainer at polarssl dot org>
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00007 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Paul Bakker5121ce52009-01-03 21:22:43 +000021 */
Paul Bakker40e46942009-01-03 21:51:57 +000022#ifndef POLARSSL_BIGNUM_H
23#define POLARSSL_BIGNUM_H
Paul Bakker5121ce52009-01-03 21:22:43 +000024
25#include <stdio.h>
26
Paul Bakkerb5bf1762009-07-19 20:28:35 +000027#define POLARSSL_ERR_MPI_FILE_IO_ERROR 0x0002
28#define POLARSSL_ERR_MPI_BAD_INPUT_DATA 0x0004
29#define POLARSSL_ERR_MPI_INVALID_CHARACTER 0x0006
30#define POLARSSL_ERR_MPI_BUFFER_TOO_SMALL 0x0008
31#define POLARSSL_ERR_MPI_NEGATIVE_VALUE 0x000A
32#define POLARSSL_ERR_MPI_DIVISION_BY_ZERO 0x000C
33#define POLARSSL_ERR_MPI_NOT_ACCEPTABLE 0x000E
Paul Bakker5121ce52009-01-03 21:22:43 +000034
35#define MPI_CHK(f) if( ( ret = f ) != 0 ) goto cleanup
36
37/*
38 * Define the base integer type, architecture-wise
39 */
Paul Bakker40e46942009-01-03 21:51:57 +000040#if defined(POLARSSL_HAVE_INT8)
Paul Bakker5121ce52009-01-03 21:22:43 +000041typedef unsigned char t_int;
42typedef unsigned short t_dbl;
43#else
Paul Bakker40e46942009-01-03 21:51:57 +000044#if defined(POLARSSL_HAVE_INT16)
Paul Bakker5121ce52009-01-03 21:22:43 +000045typedef unsigned short t_int;
46typedef unsigned long t_dbl;
47#else
48 typedef unsigned long t_int;
49 #if defined(_MSC_VER) && defined(_M_IX86)
50 typedef unsigned __int64 t_dbl;
51 #else
52 #if defined(__amd64__) || defined(__x86_64__) || \
53 defined(__ppc64__) || defined(__powerpc64__) || \
54 defined(__ia64__) || defined(__alpha__)
55 typedef unsigned int t_dbl __attribute__((mode(TI)));
56 #else
Paul Bakker1a9382e2009-07-11 16:35:32 +000057 #if defined(POLARSSL_HAVE_LONGLONG)
58 typedef unsigned long long t_dbl;
59 #endif
Paul Bakker5121ce52009-01-03 21:22:43 +000060 #endif
61 #endif
62#endif
63#endif
64
65/**
66 * \brief MPI structure
67 */
68typedef struct
69{
70 int s; /*!< integer sign */
71 int n; /*!< total # of limbs */
72 t_int *p; /*!< pointer to limbs */
73}
74mpi;
75
76#ifdef __cplusplus
77extern "C" {
78#endif
79
80/**
81 * \brief Initialize one or more mpi
82 */
83void mpi_init( mpi *X, ... );
84
85/**
86 * \brief Unallocate one or more mpi
87 */
88void mpi_free( mpi *X, ... );
89
90/**
91 * \brief Enlarge to the specified number of limbs
92 *
93 * \return 0 if successful,
94 * 1 if memory allocation failed
95 */
96int mpi_grow( mpi *X, int nblimbs );
97
98/**
99 * \brief Copy the contents of Y into X
100 *
101 * \return 0 if successful,
102 * 1 if memory allocation failed
103 */
104int mpi_copy( mpi *X, mpi *Y );
105
106/**
107 * \brief Swap the contents of X and Y
108 */
109void mpi_swap( mpi *X, mpi *Y );
110
111/**
112 * \brief Set value from integer
113 *
114 * \return 0 if successful,
115 * 1 if memory allocation failed
116 */
117int mpi_lset( mpi *X, int z );
118
119/**
120 * \brief Return the number of least significant bits
121 */
122int mpi_lsb( mpi *X );
123
124/**
125 * \brief Return the number of most significant bits
126 */
127int mpi_msb( mpi *X );
128
129/**
130 * \brief Return the total size in bytes
131 */
132int mpi_size( mpi *X );
133
134/**
135 * \brief Import from an ASCII string
136 *
137 * \param X destination mpi
138 * \param radix input numeric base
139 * \param s null-terminated string buffer
140 *
Paul Bakker40e46942009-01-03 21:51:57 +0000141 * \return 0 if successful, or an POLARSSL_ERR_MPI_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000142 */
143int mpi_read_string( mpi *X, int radix, char *s );
144
145/**
146 * \brief Export into an ASCII string
147 *
148 * \param X source mpi
149 * \param radix output numeric base
150 * \param s string buffer
151 * \param slen string buffer size
152 *
Paul Bakker40e46942009-01-03 21:51:57 +0000153 * \return 0 if successful, or an POLARSSL_ERR_MPI_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000154 *
155 * \note Call this function with *slen = 0 to obtain the
156 * minimum required buffer size in *slen.
157 */
158int mpi_write_string( mpi *X, int radix, char *s, int *slen );
159
160/**
161 * \brief Read X from an opened file
162 *
163 * \param X destination mpi
164 * \param radix input numeric base
165 * \param fin input file handle
166 *
Paul Bakker40e46942009-01-03 21:51:57 +0000167 * \return 0 if successful, or an POLARSSL_ERR_MPI_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000168 */
169int mpi_read_file( mpi *X, int radix, FILE *fin );
170
171/**
172 * \brief Write X into an opened file, or stdout
173 *
174 * \param p prefix, can be NULL
175 * \param X source mpi
176 * \param radix output numeric base
177 * \param fout output file handle
178 *
Paul Bakker40e46942009-01-03 21:51:57 +0000179 * \return 0 if successful, or an POLARSSL_ERR_MPI_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000180 *
181 * \note Set fout == NULL to print X on the console.
182 */
183int mpi_write_file( char *p, mpi *X, int radix, FILE *fout );
184
185/**
186 * \brief Import X from unsigned binary data, big endian
187 *
188 * \param X destination mpi
189 * \param buf input buffer
190 * \param buflen input buffer size
191 *
192 * \return 0 if successful,
193 * 1 if memory allocation failed
194 */
195int mpi_read_binary( mpi *X, unsigned char *buf, int buflen );
196
197/**
198 * \brief Export X into unsigned binary data, big endian
199 *
200 * \param X source mpi
201 * \param buf output buffer
202 * \param buflen output buffer size
203 *
204 * \return 0 if successful,
Paul Bakker40e46942009-01-03 21:51:57 +0000205 * POLARSSL_ERR_MPI_BUFFER_TOO_SMALL if buf isn't large enough
Paul Bakker5121ce52009-01-03 21:22:43 +0000206 *
207 * \note Call this function with *buflen = 0 to obtain the
208 * minimum required buffer size in *buflen.
209 */
210int mpi_write_binary( mpi *X, unsigned char *buf, int buflen );
211
212/**
213 * \brief Left-shift: X <<= count
214 *
215 * \return 0 if successful,
216 * 1 if memory allocation failed
217 */
218int mpi_shift_l( mpi *X, int count );
219
220/**
221 * \brief Right-shift: X >>= count
222 *
223 * \return 0 if successful,
224 * 1 if memory allocation failed
225 */
226int mpi_shift_r( mpi *X, int count );
227
228/**
229 * \brief Compare unsigned values
230 *
231 * \return 1 if |X| is greater than |Y|,
232 * -1 if |X| is lesser than |Y| or
233 * 0 if |X| is equal to |Y|
234 */
235int mpi_cmp_abs( mpi *X, mpi *Y );
236
237/**
238 * \brief Compare signed values
239 *
240 * \return 1 if X is greater than Y,
241 * -1 if X is lesser than Y or
242 * 0 if X is equal to Y
243 */
244int mpi_cmp_mpi( mpi *X, mpi *Y );
245
246/**
247 * \brief Compare signed values
248 *
249 * \return 1 if X is greater than z,
250 * -1 if X is lesser than z or
251 * 0 if X is equal to z
252 */
253int mpi_cmp_int( mpi *X, int z );
254
255/**
256 * \brief Unsigned addition: X = |A| + |B|
257 *
258 * \return 0 if successful,
259 * 1 if memory allocation failed
260 */
261int mpi_add_abs( mpi *X, mpi *A, mpi *B );
262
263/**
264 * \brief Unsigned substraction: X = |A| - |B|
265 *
266 * \return 0 if successful,
Paul Bakker40e46942009-01-03 21:51:57 +0000267 * POLARSSL_ERR_MPI_NEGATIVE_VALUE if B is greater than A
Paul Bakker5121ce52009-01-03 21:22:43 +0000268 */
269int mpi_sub_abs( mpi *X, mpi *A, mpi *B );
270
271/**
272 * \brief Signed addition: X = A + B
273 *
274 * \return 0 if successful,
275 * 1 if memory allocation failed
276 */
277int mpi_add_mpi( mpi *X, mpi *A, mpi *B );
278
279/**
280 * \brief Signed substraction: X = A - B
281 *
282 * \return 0 if successful,
283 * 1 if memory allocation failed
284 */
285int mpi_sub_mpi( mpi *X, mpi *A, mpi *B );
286
287/**
288 * \brief Signed addition: X = A + b
289 *
290 * \return 0 if successful,
291 * 1 if memory allocation failed
292 */
293int mpi_add_int( mpi *X, mpi *A, int b );
294
295/**
296 * \brief Signed substraction: X = A - b
297 *
298 * \return 0 if successful,
299 * 1 if memory allocation failed
300 */
301int mpi_sub_int( mpi *X, mpi *A, int b );
302
303/**
304 * \brief Baseline multiplication: X = A * B
305 *
306 * \return 0 if successful,
307 * 1 if memory allocation failed
308 */
309int mpi_mul_mpi( mpi *X, mpi *A, mpi *B );
310
311/**
312 * \brief Baseline multiplication: X = A * b
Paul Bakkerce40a6d2009-06-23 19:46:08 +0000313 * Note: b is an unsigned integer type, thus
314 * Negative values of b are ignored.
Paul Bakker5121ce52009-01-03 21:22:43 +0000315 *
316 * \return 0 if successful,
317 * 1 if memory allocation failed
318 */
319int mpi_mul_int( mpi *X, mpi *A, t_int b );
320
321/**
322 * \brief Division by mpi: A = Q * B + R
323 *
324 * \return 0 if successful,
325 * 1 if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000326 * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if B == 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000327 *
328 * \note Either Q or R can be NULL.
329 */
330int mpi_div_mpi( mpi *Q, mpi *R, mpi *A, mpi *B );
331
332/**
333 * \brief Division by int: A = Q * b + R
334 *
335 * \return 0 if successful,
336 * 1 if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000337 * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if b == 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000338 *
339 * \note Either Q or R can be NULL.
340 */
341int mpi_div_int( mpi *Q, mpi *R, mpi *A, int b );
342
343/**
344 * \brief Modulo: R = A mod B
345 *
346 * \return 0 if successful,
347 * 1 if memory allocation failed,
Paul Bakkerce40a6d2009-06-23 19:46:08 +0000348 * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if B == 0,
349 * POLARSSL_ERR_MPI_NEGATIVE_VALUE if B < 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000350 */
351int mpi_mod_mpi( mpi *R, mpi *A, mpi *B );
352
353/**
354 * \brief Modulo: r = A mod b
355 *
356 * \return 0 if successful,
357 * 1 if memory allocation failed,
Paul Bakkerce40a6d2009-06-23 19:46:08 +0000358 * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if b == 0,
359 * POLARSSL_ERR_MPI_NEGATIVE_VALUE if b < 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000360 */
361int mpi_mod_int( t_int *r, mpi *A, int b );
362
363/**
364 * \brief Sliding-window exponentiation: X = A^E mod N
365 *
366 * \return 0 if successful,
367 * 1 if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000368 * POLARSSL_ERR_MPI_BAD_INPUT_DATA if N is negative or even
Paul Bakker5121ce52009-01-03 21:22:43 +0000369 *
370 * \note _RR is used to avoid re-computing R*R mod N across
371 * multiple calls, which speeds up things a bit. It can
372 * be set to NULL if the extra performance is unneeded.
373 */
374int mpi_exp_mod( mpi *X, mpi *A, mpi *E, mpi *N, mpi *_RR );
375
376/**
377 * \brief Greatest common divisor: G = gcd(A, B)
378 *
379 * \return 0 if successful,
380 * 1 if memory allocation failed
381 */
382int mpi_gcd( mpi *G, mpi *A, mpi *B );
383
384/**
385 * \brief Modular inverse: X = A^-1 mod N
386 *
387 * \return 0 if successful,
388 * 1 if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000389 * POLARSSL_ERR_MPI_BAD_INPUT_DATA if N is negative or nil
390 * POLARSSL_ERR_MPI_NOT_ACCEPTABLE if A has no inverse mod N
Paul Bakker5121ce52009-01-03 21:22:43 +0000391 */
392int mpi_inv_mod( mpi *X, mpi *A, mpi *N );
393
394/**
395 * \brief Miller-Rabin primality test
396 *
397 * \return 0 if successful (probably prime),
398 * 1 if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000399 * POLARSSL_ERR_MPI_NOT_ACCEPTABLE if X is not prime
Paul Bakker5121ce52009-01-03 21:22:43 +0000400 */
401int mpi_is_prime( mpi *X, int (*f_rng)(void *), void *p_rng );
402
403/**
404 * \brief Prime number generation
405 *
406 * \param X destination mpi
407 * \param nbits required size of X in bits
408 * \param dh_flag if 1, then (X-1)/2 will be prime too
409 * \param f_rng RNG function
410 * \param p_rng RNG parameter
411 *
412 * \return 0 if successful (probably prime),
413 * 1 if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000414 * POLARSSL_ERR_MPI_BAD_INPUT_DATA if nbits is < 3
Paul Bakker5121ce52009-01-03 21:22:43 +0000415 */
416int mpi_gen_prime( mpi *X, int nbits, int dh_flag,
417 int (*f_rng)(void *), void *p_rng );
418
419/**
420 * \brief Checkup routine
421 *
422 * \return 0 if successful, or 1 if the test failed
423 */
424int mpi_self_test( int verbose );
425
426#ifdef __cplusplus
427}
428#endif
429
430#endif /* bignum.h */