blob: 9c06a3f55b1af8ecc5cf0f017e2a70b7c1b523e1 [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 Bakker40e46942009-01-03 21:51:57 +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
57 typedef unsigned long long t_dbl;
58 #endif
59 #endif
60#endif
61#endif
62
63/**
64 * \brief MPI structure
65 */
66typedef struct
67{
68 int s; /*!< integer sign */
69 int n; /*!< total # of limbs */
70 t_int *p; /*!< pointer to limbs */
71}
72mpi;
73
74#ifdef __cplusplus
75extern "C" {
76#endif
77
78/**
79 * \brief Initialize one or more mpi
80 */
81void mpi_init( mpi *X, ... );
82
83/**
84 * \brief Unallocate one or more mpi
85 */
86void mpi_free( mpi *X, ... );
87
88/**
89 * \brief Enlarge to the specified number of limbs
90 *
91 * \return 0 if successful,
92 * 1 if memory allocation failed
93 */
94int mpi_grow( mpi *X, int nblimbs );
95
96/**
97 * \brief Copy the contents of Y into X
98 *
99 * \return 0 if successful,
100 * 1 if memory allocation failed
101 */
102int mpi_copy( mpi *X, mpi *Y );
103
104/**
105 * \brief Swap the contents of X and Y
106 */
107void mpi_swap( mpi *X, mpi *Y );
108
109/**
110 * \brief Set value from integer
111 *
112 * \return 0 if successful,
113 * 1 if memory allocation failed
114 */
115int mpi_lset( mpi *X, int z );
116
117/**
118 * \brief Return the number of least significant bits
119 */
120int mpi_lsb( mpi *X );
121
122/**
123 * \brief Return the number of most significant bits
124 */
125int mpi_msb( mpi *X );
126
127/**
128 * \brief Return the total size in bytes
129 */
130int mpi_size( mpi *X );
131
132/**
133 * \brief Import from an ASCII string
134 *
135 * \param X destination mpi
136 * \param radix input numeric base
137 * \param s null-terminated string buffer
138 *
Paul Bakker40e46942009-01-03 21:51:57 +0000139 * \return 0 if successful, or an POLARSSL_ERR_MPI_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000140 */
141int mpi_read_string( mpi *X, int radix, char *s );
142
143/**
144 * \brief Export into an ASCII string
145 *
146 * \param X source mpi
147 * \param radix output numeric base
148 * \param s string buffer
149 * \param slen string buffer size
150 *
Paul Bakker40e46942009-01-03 21:51:57 +0000151 * \return 0 if successful, or an POLARSSL_ERR_MPI_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000152 *
153 * \note Call this function with *slen = 0 to obtain the
154 * minimum required buffer size in *slen.
155 */
156int mpi_write_string( mpi *X, int radix, char *s, int *slen );
157
158/**
159 * \brief Read X from an opened file
160 *
161 * \param X destination mpi
162 * \param radix input numeric base
163 * \param fin input file handle
164 *
Paul Bakker40e46942009-01-03 21:51:57 +0000165 * \return 0 if successful, or an POLARSSL_ERR_MPI_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000166 */
167int mpi_read_file( mpi *X, int radix, FILE *fin );
168
169/**
170 * \brief Write X into an opened file, or stdout
171 *
172 * \param p prefix, can be NULL
173 * \param X source mpi
174 * \param radix output numeric base
175 * \param fout output file handle
176 *
Paul Bakker40e46942009-01-03 21:51:57 +0000177 * \return 0 if successful, or an POLARSSL_ERR_MPI_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000178 *
179 * \note Set fout == NULL to print X on the console.
180 */
181int mpi_write_file( char *p, mpi *X, int radix, FILE *fout );
182
183/**
184 * \brief Import X from unsigned binary data, big endian
185 *
186 * \param X destination mpi
187 * \param buf input buffer
188 * \param buflen input buffer size
189 *
190 * \return 0 if successful,
191 * 1 if memory allocation failed
192 */
193int mpi_read_binary( mpi *X, unsigned char *buf, int buflen );
194
195/**
196 * \brief Export X into unsigned binary data, big endian
197 *
198 * \param X source mpi
199 * \param buf output buffer
200 * \param buflen output buffer size
201 *
202 * \return 0 if successful,
Paul Bakker40e46942009-01-03 21:51:57 +0000203 * POLARSSL_ERR_MPI_BUFFER_TOO_SMALL if buf isn't large enough
Paul Bakker5121ce52009-01-03 21:22:43 +0000204 *
205 * \note Call this function with *buflen = 0 to obtain the
206 * minimum required buffer size in *buflen.
207 */
208int mpi_write_binary( mpi *X, unsigned char *buf, int buflen );
209
210/**
211 * \brief Left-shift: X <<= count
212 *
213 * \return 0 if successful,
214 * 1 if memory allocation failed
215 */
216int mpi_shift_l( mpi *X, int count );
217
218/**
219 * \brief Right-shift: X >>= count
220 *
221 * \return 0 if successful,
222 * 1 if memory allocation failed
223 */
224int mpi_shift_r( mpi *X, int count );
225
226/**
227 * \brief Compare unsigned values
228 *
229 * \return 1 if |X| is greater than |Y|,
230 * -1 if |X| is lesser than |Y| or
231 * 0 if |X| is equal to |Y|
232 */
233int mpi_cmp_abs( mpi *X, mpi *Y );
234
235/**
236 * \brief Compare signed values
237 *
238 * \return 1 if X is greater than Y,
239 * -1 if X is lesser than Y or
240 * 0 if X is equal to Y
241 */
242int mpi_cmp_mpi( mpi *X, mpi *Y );
243
244/**
245 * \brief Compare signed values
246 *
247 * \return 1 if X is greater than z,
248 * -1 if X is lesser than z or
249 * 0 if X is equal to z
250 */
251int mpi_cmp_int( mpi *X, int z );
252
253/**
254 * \brief Unsigned addition: X = |A| + |B|
255 *
256 * \return 0 if successful,
257 * 1 if memory allocation failed
258 */
259int mpi_add_abs( mpi *X, mpi *A, mpi *B );
260
261/**
262 * \brief Unsigned substraction: X = |A| - |B|
263 *
264 * \return 0 if successful,
Paul Bakker40e46942009-01-03 21:51:57 +0000265 * POLARSSL_ERR_MPI_NEGATIVE_VALUE if B is greater than A
Paul Bakker5121ce52009-01-03 21:22:43 +0000266 */
267int mpi_sub_abs( mpi *X, mpi *A, mpi *B );
268
269/**
270 * \brief Signed addition: X = A + B
271 *
272 * \return 0 if successful,
273 * 1 if memory allocation failed
274 */
275int mpi_add_mpi( mpi *X, mpi *A, mpi *B );
276
277/**
278 * \brief Signed substraction: X = A - B
279 *
280 * \return 0 if successful,
281 * 1 if memory allocation failed
282 */
283int mpi_sub_mpi( mpi *X, mpi *A, mpi *B );
284
285/**
286 * \brief Signed addition: X = A + b
287 *
288 * \return 0 if successful,
289 * 1 if memory allocation failed
290 */
291int mpi_add_int( mpi *X, mpi *A, int b );
292
293/**
294 * \brief Signed substraction: X = A - b
295 *
296 * \return 0 if successful,
297 * 1 if memory allocation failed
298 */
299int mpi_sub_int( mpi *X, mpi *A, int b );
300
301/**
302 * \brief Baseline multiplication: X = A * B
303 *
304 * \return 0 if successful,
305 * 1 if memory allocation failed
306 */
307int mpi_mul_mpi( mpi *X, mpi *A, mpi *B );
308
309/**
310 * \brief Baseline multiplication: X = A * b
Paul Bakkerce40a6d2009-06-23 19:46:08 +0000311 * Note: b is an unsigned integer type, thus
312 * Negative values of b are ignored.
Paul Bakker5121ce52009-01-03 21:22:43 +0000313 *
314 * \return 0 if successful,
315 * 1 if memory allocation failed
316 */
317int mpi_mul_int( mpi *X, mpi *A, t_int b );
318
319/**
320 * \brief Division by mpi: A = Q * B + R
321 *
322 * \return 0 if successful,
323 * 1 if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000324 * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if B == 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000325 *
326 * \note Either Q or R can be NULL.
327 */
328int mpi_div_mpi( mpi *Q, mpi *R, mpi *A, mpi *B );
329
330/**
331 * \brief Division by int: A = Q * b + R
332 *
333 * \return 0 if successful,
334 * 1 if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000335 * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if b == 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000336 *
337 * \note Either Q or R can be NULL.
338 */
339int mpi_div_int( mpi *Q, mpi *R, mpi *A, int b );
340
341/**
342 * \brief Modulo: R = A mod B
343 *
344 * \return 0 if successful,
345 * 1 if memory allocation failed,
Paul Bakkerce40a6d2009-06-23 19:46:08 +0000346 * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if B == 0,
347 * POLARSSL_ERR_MPI_NEGATIVE_VALUE if B < 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000348 */
349int mpi_mod_mpi( mpi *R, mpi *A, mpi *B );
350
351/**
352 * \brief Modulo: r = A mod b
353 *
354 * \return 0 if successful,
355 * 1 if memory allocation failed,
Paul Bakkerce40a6d2009-06-23 19:46:08 +0000356 * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if b == 0,
357 * POLARSSL_ERR_MPI_NEGATIVE_VALUE if b < 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000358 */
359int mpi_mod_int( t_int *r, mpi *A, int b );
360
361/**
362 * \brief Sliding-window exponentiation: X = A^E mod N
363 *
364 * \return 0 if successful,
365 * 1 if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000366 * POLARSSL_ERR_MPI_BAD_INPUT_DATA if N is negative or even
Paul Bakker5121ce52009-01-03 21:22:43 +0000367 *
368 * \note _RR is used to avoid re-computing R*R mod N across
369 * multiple calls, which speeds up things a bit. It can
370 * be set to NULL if the extra performance is unneeded.
371 */
372int mpi_exp_mod( mpi *X, mpi *A, mpi *E, mpi *N, mpi *_RR );
373
374/**
375 * \brief Greatest common divisor: G = gcd(A, B)
376 *
377 * \return 0 if successful,
378 * 1 if memory allocation failed
379 */
380int mpi_gcd( mpi *G, mpi *A, mpi *B );
381
382/**
383 * \brief Modular inverse: X = A^-1 mod N
384 *
385 * \return 0 if successful,
386 * 1 if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000387 * POLARSSL_ERR_MPI_BAD_INPUT_DATA if N is negative or nil
388 * POLARSSL_ERR_MPI_NOT_ACCEPTABLE if A has no inverse mod N
Paul Bakker5121ce52009-01-03 21:22:43 +0000389 */
390int mpi_inv_mod( mpi *X, mpi *A, mpi *N );
391
392/**
393 * \brief Miller-Rabin primality test
394 *
395 * \return 0 if successful (probably prime),
396 * 1 if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000397 * POLARSSL_ERR_MPI_NOT_ACCEPTABLE if X is not prime
Paul Bakker5121ce52009-01-03 21:22:43 +0000398 */
399int mpi_is_prime( mpi *X, int (*f_rng)(void *), void *p_rng );
400
401/**
402 * \brief Prime number generation
403 *
404 * \param X destination mpi
405 * \param nbits required size of X in bits
406 * \param dh_flag if 1, then (X-1)/2 will be prime too
407 * \param f_rng RNG function
408 * \param p_rng RNG parameter
409 *
410 * \return 0 if successful (probably prime),
411 * 1 if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000412 * POLARSSL_ERR_MPI_BAD_INPUT_DATA if nbits is < 3
Paul Bakker5121ce52009-01-03 21:22:43 +0000413 */
414int mpi_gen_prime( mpi *X, int nbits, int dh_flag,
415 int (*f_rng)(void *), void *p_rng );
416
417/**
418 * \brief Checkup routine
419 *
420 * \return 0 if successful, or 1 if the test failed
421 */
422int mpi_self_test( int verbose );
423
424#ifdef __cplusplus
425}
426#endif
427
428#endif /* bignum.h */