blob: ebc1f8f2f00a5d6125f510b5db5f22e249aec52f [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/**
2 * \file bignum.h
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00003 *
Paul Bakker37ca75d2011-01-06 12:28:03 +00004 * \brief Multi-precision integer library
5 *
Paul Bakker84f12b72010-07-18 10:13:04 +00006 * Copyright (C) 2006-2010, Brainspark B.V.
Paul Bakkerb96f1542010-07-18 20:36:00 +00007 *
8 * This file is part of PolarSSL (http://www.polarssl.org)
Paul Bakker84f12b72010-07-18 10:13:04 +00009 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
Paul Bakkerb96f1542010-07-18 20:36:00 +000010 *
Paul Bakker77b385e2009-07-28 17:23:11 +000011 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000012 *
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000013 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License along
24 * with this program; if not, write to the Free Software Foundation, Inc.,
25 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Paul Bakker5121ce52009-01-03 21:22:43 +000026 */
Paul Bakker40e46942009-01-03 21:51:57 +000027#ifndef POLARSSL_BIGNUM_H
28#define POLARSSL_BIGNUM_H
Paul Bakker5121ce52009-01-03 21:22:43 +000029
30#include <stdio.h>
31
Paul Bakkerb5bf1762009-07-19 20:28:35 +000032#define POLARSSL_ERR_MPI_FILE_IO_ERROR 0x0002
33#define POLARSSL_ERR_MPI_BAD_INPUT_DATA 0x0004
34#define POLARSSL_ERR_MPI_INVALID_CHARACTER 0x0006
35#define POLARSSL_ERR_MPI_BUFFER_TOO_SMALL 0x0008
36#define POLARSSL_ERR_MPI_NEGATIVE_VALUE 0x000A
37#define POLARSSL_ERR_MPI_DIVISION_BY_ZERO 0x000C
38#define POLARSSL_ERR_MPI_NOT_ACCEPTABLE 0x000E
Paul Bakker5121ce52009-01-03 21:22:43 +000039
40#define MPI_CHK(f) if( ( ret = f ) != 0 ) goto cleanup
41
42/*
43 * Define the base integer type, architecture-wise
44 */
Paul Bakker40e46942009-01-03 21:51:57 +000045#if defined(POLARSSL_HAVE_INT8)
Paul Bakker5121ce52009-01-03 21:22:43 +000046typedef unsigned char t_int;
47typedef unsigned short t_dbl;
48#else
Paul Bakker40e46942009-01-03 21:51:57 +000049#if defined(POLARSSL_HAVE_INT16)
Paul Bakker5121ce52009-01-03 21:22:43 +000050typedef unsigned short t_int;
51typedef unsigned long t_dbl;
52#else
53 typedef unsigned long t_int;
54 #if defined(_MSC_VER) && defined(_M_IX86)
55 typedef unsigned __int64 t_dbl;
56 #else
57 #if defined(__amd64__) || defined(__x86_64__) || \
58 defined(__ppc64__) || defined(__powerpc64__) || \
59 defined(__ia64__) || defined(__alpha__)
60 typedef unsigned int t_dbl __attribute__((mode(TI)));
61 #else
Paul Bakker1a9382e2009-07-11 16:35:32 +000062 #if defined(POLARSSL_HAVE_LONGLONG)
63 typedef unsigned long long t_dbl;
64 #endif
Paul Bakker5121ce52009-01-03 21:22:43 +000065 #endif
66 #endif
67#endif
68#endif
69
70/**
71 * \brief MPI structure
72 */
73typedef struct
74{
75 int s; /*!< integer sign */
76 int n; /*!< total # of limbs */
77 t_int *p; /*!< pointer to limbs */
78}
79mpi;
80
81#ifdef __cplusplus
82extern "C" {
83#endif
84
85/**
86 * \brief Initialize one or more mpi
87 */
88void mpi_init( mpi *X, ... );
89
90/**
91 * \brief Unallocate one or more mpi
92 */
93void mpi_free( mpi *X, ... );
94
95/**
96 * \brief Enlarge to the specified number of limbs
97 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +000098 * \param X MPI to grow
99 * \param nblimbs The target number of limbs
100 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000101 * \return 0 if successful,
102 * 1 if memory allocation failed
103 */
104int mpi_grow( mpi *X, int nblimbs );
105
106/**
107 * \brief Copy the contents of Y into X
108 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000109 * \param X Destination MPI
110 * \param Y Source MPI
111 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000112 * \return 0 if successful,
113 * 1 if memory allocation failed
114 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000115int mpi_copy( mpi *X, const mpi *Y );
Paul Bakker5121ce52009-01-03 21:22:43 +0000116
117/**
118 * \brief Swap the contents of X and Y
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000119 *
120 * \param X First MPI value
121 * \param Y Second MPI value
Paul Bakker5121ce52009-01-03 21:22:43 +0000122 */
123void mpi_swap( mpi *X, mpi *Y );
124
125/**
126 * \brief Set value from integer
127 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000128 * \param X MPI to set
129 * \param z Value to use
130 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000131 * \return 0 if successful,
132 * 1 if memory allocation failed
133 */
134int mpi_lset( mpi *X, int z );
135
136/**
137 * \brief Return the number of least significant bits
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000138 *
139 * \param X MPI to use
Paul Bakker5121ce52009-01-03 21:22:43 +0000140 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000141int mpi_lsb( const mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000142
143/**
144 * \brief Return the number of most significant bits
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000145 *
146 * \param X MPI to use
Paul Bakker5121ce52009-01-03 21:22:43 +0000147 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000148int mpi_msb( const mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000149
150/**
151 * \brief Return the total size in bytes
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000152 *
153 * \param X MPI to use
Paul Bakker5121ce52009-01-03 21:22:43 +0000154 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000155int mpi_size( const mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000156
157/**
158 * \brief Import from an ASCII string
159 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000160 * \param X Destination MPI
161 * \param radix Input numeric base
162 * \param s Null-terminated string buffer
Paul Bakker5121ce52009-01-03 21:22:43 +0000163 *
Paul Bakker40e46942009-01-03 21:51:57 +0000164 * \return 0 if successful, or an POLARSSL_ERR_MPI_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000165 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000166int mpi_read_string( mpi *X, int radix, const char *s );
Paul Bakker5121ce52009-01-03 21:22:43 +0000167
168/**
169 * \brief Export into an ASCII string
170 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000171 * \param X Source MPI
172 * \param radix Output numeric base
173 * \param s String buffer
174 * \param slen String buffer size
Paul Bakker5121ce52009-01-03 21:22:43 +0000175 *
Paul Bakkerff60ee62010-03-16 21:09:09 +0000176 * \return 0 if successful, or an POLARSSL_ERR_MPI_XXX error code.
177 * *slen is always updated to reflect the amount
178 * of data that has (or would have) been written.
Paul Bakker5121ce52009-01-03 21:22:43 +0000179 *
180 * \note Call this function with *slen = 0 to obtain the
181 * minimum required buffer size in *slen.
182 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000183int mpi_write_string( const mpi *X, int radix, char *s, int *slen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000184
185/**
186 * \brief Read X from an opened file
187 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000188 * \param X Destination MPI
189 * \param radix Input numeric base
190 * \param fin Input file handle
Paul Bakker5121ce52009-01-03 21:22:43 +0000191 *
Paul Bakker40e46942009-01-03 21:51:57 +0000192 * \return 0 if successful, or an POLARSSL_ERR_MPI_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000193 */
194int mpi_read_file( mpi *X, int radix, FILE *fin );
195
196/**
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000197 * \brief Write X into an opened file, or stdout if fout is NULL
Paul Bakker5121ce52009-01-03 21:22:43 +0000198 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000199 * \param p Prefix, can be NULL
200 * \param X Source MPI
201 * \param radix Output numeric base
202 * \param fout Output file handle (can be NULL)
Paul Bakker5121ce52009-01-03 21:22:43 +0000203 *
Paul Bakker40e46942009-01-03 21:51:57 +0000204 * \return 0 if successful, or an POLARSSL_ERR_MPI_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000205 *
206 * \note Set fout == NULL to print X on the console.
207 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000208int mpi_write_file( const char *p, const mpi *X, int radix, FILE *fout );
Paul Bakker5121ce52009-01-03 21:22:43 +0000209
210/**
211 * \brief Import X from unsigned binary data, big endian
212 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000213 * \param X Destination MPI
214 * \param buf Input buffer
215 * \param buflen Input buffer size
Paul Bakker5121ce52009-01-03 21:22:43 +0000216 *
217 * \return 0 if successful,
218 * 1 if memory allocation failed
219 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000220int mpi_read_binary( mpi *X, const unsigned char *buf, int buflen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000221
222/**
223 * \brief Export X into unsigned binary data, big endian
224 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000225 * \param X Source MPI
226 * \param buf Output buffer
227 * \param buflen Output buffer size
Paul Bakker5121ce52009-01-03 21:22:43 +0000228 *
229 * \return 0 if successful,
Paul Bakker40e46942009-01-03 21:51:57 +0000230 * POLARSSL_ERR_MPI_BUFFER_TOO_SMALL if buf isn't large enough
Paul Bakker5121ce52009-01-03 21:22:43 +0000231 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000232int mpi_write_binary( const mpi *X, unsigned char *buf, int buflen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000233
234/**
235 * \brief Left-shift: X <<= count
236 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000237 * \param X MPI to shift
238 * \param count Amount to shift
239 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000240 * \return 0 if successful,
241 * 1 if memory allocation failed
242 */
243int mpi_shift_l( mpi *X, int count );
244
245/**
246 * \brief Right-shift: X >>= count
247 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000248 * \param X MPI to shift
249 * \param count Amount to shift
250 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000251 * \return 0 if successful,
252 * 1 if memory allocation failed
253 */
254int mpi_shift_r( mpi *X, int count );
255
256/**
257 * \brief Compare unsigned values
258 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000259 * \param X Left-hand MPI
260 * \param Y Right-hand MPI
261 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000262 * \return 1 if |X| is greater than |Y|,
263 * -1 if |X| is lesser than |Y| or
264 * 0 if |X| is equal to |Y|
265 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000266int mpi_cmp_abs( const mpi *X, const mpi *Y );
Paul Bakker5121ce52009-01-03 21:22:43 +0000267
268/**
269 * \brief Compare signed values
270 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000271 * \param X Left-hand MPI
272 * \param Y Right-hand MPI
273 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000274 * \return 1 if X is greater than Y,
275 * -1 if X is lesser than Y or
276 * 0 if X is equal to Y
277 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000278int mpi_cmp_mpi( const mpi *X, const mpi *Y );
Paul Bakker5121ce52009-01-03 21:22:43 +0000279
280/**
281 * \brief Compare signed values
282 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000283 * \param X Left-hand MPI
284 * \param z The integer value to compare to
285 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000286 * \return 1 if X is greater than z,
287 * -1 if X is lesser than z or
288 * 0 if X is equal to z
289 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000290int mpi_cmp_int( const mpi *X, int z );
Paul Bakker5121ce52009-01-03 21:22:43 +0000291
292/**
293 * \brief Unsigned addition: X = |A| + |B|
294 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000295 * \param X Destination MPI
296 * \param A Left-hand MPI
297 * \param B Right-hand MPI
298 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000299 * \return 0 if successful,
300 * 1 if memory allocation failed
301 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000302int mpi_add_abs( mpi *X, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000303
304/**
305 * \brief Unsigned substraction: X = |A| - |B|
306 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000307 * \param X Destination MPI
308 * \param A Left-hand MPI
309 * \param B Right-hand MPI
310 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000311 * \return 0 if successful,
Paul Bakker40e46942009-01-03 21:51:57 +0000312 * POLARSSL_ERR_MPI_NEGATIVE_VALUE if B is greater than A
Paul Bakker5121ce52009-01-03 21:22:43 +0000313 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000314int mpi_sub_abs( mpi *X, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000315
316/**
317 * \brief Signed addition: X = A + B
318 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000319 * \param X Destination MPI
320 * \param A Left-hand MPI
321 * \param B Right-hand MPI
322 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000323 * \return 0 if successful,
324 * 1 if memory allocation failed
325 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000326int mpi_add_mpi( mpi *X, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000327
328/**
329 * \brief Signed substraction: X = A - B
330 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000331 * \param X Destination MPI
332 * \param A Left-hand MPI
333 * \param B Right-hand MPI
334 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000335 * \return 0 if successful,
336 * 1 if memory allocation failed
337 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000338int mpi_sub_mpi( mpi *X, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000339
340/**
341 * \brief Signed addition: X = A + b
342 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000343 * \param X Destination MPI
344 * \param A Left-hand MPI
345 * \param b The integer value to add
346 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000347 * \return 0 if successful,
348 * 1 if memory allocation failed
349 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000350int mpi_add_int( mpi *X, const mpi *A, int b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000351
352/**
353 * \brief Signed substraction: X = A - b
354 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000355 * \param X Destination MPI
356 * \param A Left-hand MPI
357 * \param b The integer value to subtract
358 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000359 * \return 0 if successful,
360 * 1 if memory allocation failed
361 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000362int mpi_sub_int( mpi *X, const mpi *A, int b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000363
364/**
365 * \brief Baseline multiplication: X = A * B
366 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000367 * \param X Destination MPI
368 * \param A Left-hand MPI
369 * \param B Right-hand MPI
370 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000371 * \return 0 if successful,
372 * 1 if memory allocation failed
373 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000374int mpi_mul_mpi( mpi *X, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000375
376/**
377 * \brief Baseline multiplication: X = A * b
Paul Bakkerce40a6d2009-06-23 19:46:08 +0000378 * Note: b is an unsigned integer type, thus
379 * Negative values of b are ignored.
Paul Bakker5121ce52009-01-03 21:22:43 +0000380 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000381 * \param X Destination MPI
382 * \param A Left-hand MPI
383 * \param b The integer value to multiply with
384 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000385 * \return 0 if successful,
386 * 1 if memory allocation failed
387 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000388int mpi_mul_int( mpi *X, const mpi *A, t_int b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000389
390/**
391 * \brief Division by mpi: A = Q * B + R
392 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000393 * \param Q Destination MPI for the quotient
394 * \param R Destination MPI for the rest value
395 * \param A Left-hand MPI
396 * \param B Right-hand MPI
397 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000398 * \return 0 if successful,
399 * 1 if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000400 * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if B == 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000401 *
402 * \note Either Q or R can be NULL.
403 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000404int mpi_div_mpi( mpi *Q, mpi *R, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000405
406/**
407 * \brief Division by int: A = Q * b + R
408 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000409 * \param Q Destination MPI for the quotient
410 * \param R Destination MPI for the rest value
411 * \param A Left-hand MPI
412 * \param b Integer to divide by
413 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000414 * \return 0 if successful,
415 * 1 if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000416 * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if b == 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000417 *
418 * \note Either Q or R can be NULL.
419 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000420int mpi_div_int( mpi *Q, mpi *R, const mpi *A, int b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000421
422/**
423 * \brief Modulo: R = A mod B
424 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000425 * \param R Destination MPI for the rest value
426 * \param A Left-hand MPI
427 * \param B Right-hand MPI
428 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000429 * \return 0 if successful,
430 * 1 if memory allocation failed,
Paul Bakkerce40a6d2009-06-23 19:46:08 +0000431 * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if B == 0,
432 * POLARSSL_ERR_MPI_NEGATIVE_VALUE if B < 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000433 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000434int mpi_mod_mpi( mpi *R, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000435
436/**
437 * \brief Modulo: r = A mod b
438 *
Paul Bakkerff60ee62010-03-16 21:09:09 +0000439 * \param r Destination t_int
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000440 * \param A Left-hand MPI
441 * \param b Integer to divide by
442 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000443 * \return 0 if successful,
444 * 1 if memory allocation failed,
Paul Bakkerce40a6d2009-06-23 19:46:08 +0000445 * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if b == 0,
446 * POLARSSL_ERR_MPI_NEGATIVE_VALUE if b < 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000447 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000448int mpi_mod_int( t_int *r, const mpi *A, int b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000449
450/**
451 * \brief Sliding-window exponentiation: X = A^E mod N
452 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000453 * \param X Destination MPI
454 * \param A Left-hand MPI
455 * \param E Exponent MPI
456 * \param N Modular MPI
457 * \param _RR Speed-up MPI used for recalculations
458 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000459 * \return 0 if successful,
460 * 1 if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000461 * POLARSSL_ERR_MPI_BAD_INPUT_DATA if N is negative or even
Paul Bakker5121ce52009-01-03 21:22:43 +0000462 *
463 * \note _RR is used to avoid re-computing R*R mod N across
464 * multiple calls, which speeds up things a bit. It can
465 * be set to NULL if the extra performance is unneeded.
466 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000467int mpi_exp_mod( mpi *X, const mpi *A, const mpi *E, const mpi *N, mpi *_RR );
Paul Bakker5121ce52009-01-03 21:22:43 +0000468
469/**
Paul Bakker287781a2011-03-26 13:18:49 +0000470 * \brief Fill an MPI X with size bytes of random
471 *
472 * \param X Destination MPI
473 * \param size Size in bytes
474 * \param f_rng RNG function
475 * \param p_rng RNG parameter
476 *
477 * \return 0 if successful,
478 * 1 if memory allocation failed
479 */
480int mpi_fill_random( mpi *X, int size, int (*f_rng)(void *), void *p_rng );
481
482/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000483 * \brief Greatest common divisor: G = gcd(A, B)
484 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000485 * \param G Destination MPI
486 * \param A Left-hand MPI
487 * \param B Right-hand MPI
488 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000489 * \return 0 if successful,
490 * 1 if memory allocation failed
491 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000492int mpi_gcd( mpi *G, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000493
494/**
495 * \brief Modular inverse: X = A^-1 mod N
496 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000497 * \param X Destination MPI
498 * \param A Left-hand MPI
499 * \param N Right-hand MPI
500 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000501 * \return 0 if successful,
502 * 1 if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000503 * POLARSSL_ERR_MPI_BAD_INPUT_DATA if N is negative or nil
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000504 POLARSSL_ERR_MPI_NOT_ACCEPTABLE if A has no inverse mod N
Paul Bakker5121ce52009-01-03 21:22:43 +0000505 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000506int mpi_inv_mod( mpi *X, const mpi *A, const mpi *N );
Paul Bakker5121ce52009-01-03 21:22:43 +0000507
508/**
509 * \brief Miller-Rabin primality test
510 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000511 * \param X MPI to check
512 * \param f_rng RNG function
513 * \param p_rng RNG parameter
514 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000515 * \return 0 if successful (probably prime),
516 * 1 if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000517 * POLARSSL_ERR_MPI_NOT_ACCEPTABLE if X is not prime
Paul Bakker5121ce52009-01-03 21:22:43 +0000518 */
519int mpi_is_prime( mpi *X, int (*f_rng)(void *), void *p_rng );
520
521/**
522 * \brief Prime number generation
523 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000524 * \param X Destination MPI
525 * \param nbits Required size of X in bits
526 * \param dh_flag If 1, then (X-1)/2 will be prime too
Paul Bakker5121ce52009-01-03 21:22:43 +0000527 * \param f_rng RNG function
528 * \param p_rng RNG parameter
529 *
530 * \return 0 if successful (probably prime),
531 * 1 if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000532 * POLARSSL_ERR_MPI_BAD_INPUT_DATA if nbits is < 3
Paul Bakker5121ce52009-01-03 21:22:43 +0000533 */
534int mpi_gen_prime( mpi *X, int nbits, int dh_flag,
535 int (*f_rng)(void *), void *p_rng );
536
537/**
538 * \brief Checkup routine
539 *
540 * \return 0 if successful, or 1 if the test failed
541 */
542int mpi_self_test( int verbose );
543
544#ifdef __cplusplus
545}
546#endif
547
548#endif /* bignum.h */