blob: e6abd59e53a4c1e4e4a716d88bba4275fff994d5 [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
311 *
312 * \return 0 if successful,
313 * 1 if memory allocation failed
314 */
315int mpi_mul_int( mpi *X, mpi *A, t_int b );
316
317/**
318 * \brief Division by mpi: A = Q * B + R
319 *
320 * \return 0 if successful,
321 * 1 if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000322 * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if B == 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000323 *
324 * \note Either Q or R can be NULL.
325 */
326int mpi_div_mpi( mpi *Q, mpi *R, mpi *A, mpi *B );
327
328/**
329 * \brief Division by int: A = Q * b + R
330 *
331 * \return 0 if successful,
332 * 1 if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000333 * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if b == 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000334 *
335 * \note Either Q or R can be NULL.
336 */
337int mpi_div_int( mpi *Q, mpi *R, mpi *A, int b );
338
339/**
340 * \brief Modulo: R = A mod B
341 *
342 * \return 0 if successful,
343 * 1 if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000344 * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if B == 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000345 */
346int mpi_mod_mpi( mpi *R, mpi *A, mpi *B );
347
348/**
349 * \brief Modulo: r = A mod b
350 *
351 * \return 0 if successful,
352 * 1 if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000353 * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if b == 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000354 */
355int mpi_mod_int( t_int *r, mpi *A, int b );
356
357/**
358 * \brief Sliding-window exponentiation: X = A^E mod N
359 *
360 * \return 0 if successful,
361 * 1 if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000362 * POLARSSL_ERR_MPI_BAD_INPUT_DATA if N is negative or even
Paul Bakker5121ce52009-01-03 21:22:43 +0000363 *
364 * \note _RR is used to avoid re-computing R*R mod N across
365 * multiple calls, which speeds up things a bit. It can
366 * be set to NULL if the extra performance is unneeded.
367 */
368int mpi_exp_mod( mpi *X, mpi *A, mpi *E, mpi *N, mpi *_RR );
369
370/**
371 * \brief Greatest common divisor: G = gcd(A, B)
372 *
373 * \return 0 if successful,
374 * 1 if memory allocation failed
375 */
376int mpi_gcd( mpi *G, mpi *A, mpi *B );
377
378/**
379 * \brief Modular inverse: X = A^-1 mod N
380 *
381 * \return 0 if successful,
382 * 1 if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000383 * POLARSSL_ERR_MPI_BAD_INPUT_DATA if N is negative or nil
384 * POLARSSL_ERR_MPI_NOT_ACCEPTABLE if A has no inverse mod N
Paul Bakker5121ce52009-01-03 21:22:43 +0000385 */
386int mpi_inv_mod( mpi *X, mpi *A, mpi *N );
387
388/**
389 * \brief Miller-Rabin primality test
390 *
391 * \return 0 if successful (probably prime),
392 * 1 if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000393 * POLARSSL_ERR_MPI_NOT_ACCEPTABLE if X is not prime
Paul Bakker5121ce52009-01-03 21:22:43 +0000394 */
395int mpi_is_prime( mpi *X, int (*f_rng)(void *), void *p_rng );
396
397/**
398 * \brief Prime number generation
399 *
400 * \param X destination mpi
401 * \param nbits required size of X in bits
402 * \param dh_flag if 1, then (X-1)/2 will be prime too
403 * \param f_rng RNG function
404 * \param p_rng RNG parameter
405 *
406 * \return 0 if successful (probably prime),
407 * 1 if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000408 * POLARSSL_ERR_MPI_BAD_INPUT_DATA if nbits is < 3
Paul Bakker5121ce52009-01-03 21:22:43 +0000409 */
410int mpi_gen_prime( mpi *X, int nbits, int dh_flag,
411 int (*f_rng)(void *), void *p_rng );
412
413/**
414 * \brief Checkup routine
415 *
416 * \return 0 if successful, or 1 if the test failed
417 */
418int mpi_self_test( int verbose );
419
420#ifdef __cplusplus
421}
422#endif
423
424#endif /* bignum.h */