blob: 417f5603a82b91cafe8a0d2d63bfad6eed9754aa [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/**
2 * \file bignum.h
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00003 *
Paul Bakker84f12b72010-07-18 10:13:04 +00004 * Copyright (C) 2006-2010, Brainspark B.V.
5 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
Paul Bakker77b385e2009-07-28 17:23:11 +00006 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00007 *
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00008 * 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 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +000093 * \param X MPI to grow
94 * \param nblimbs The target number of limbs
95 *
Paul Bakker5121ce52009-01-03 21:22:43 +000096 * \return 0 if successful,
97 * 1 if memory allocation failed
98 */
99int mpi_grow( mpi *X, int nblimbs );
100
101/**
102 * \brief Copy the contents of Y into X
103 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000104 * \param X Destination MPI
105 * \param Y Source MPI
106 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000107 * \return 0 if successful,
108 * 1 if memory allocation failed
109 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000110int mpi_copy( mpi *X, const mpi *Y );
Paul Bakker5121ce52009-01-03 21:22:43 +0000111
112/**
113 * \brief Swap the contents of X and Y
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000114 *
115 * \param X First MPI value
116 * \param Y Second MPI value
Paul Bakker5121ce52009-01-03 21:22:43 +0000117 */
118void mpi_swap( mpi *X, mpi *Y );
119
120/**
121 * \brief Set value from integer
122 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000123 * \param X MPI to set
124 * \param z Value to use
125 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000126 * \return 0 if successful,
127 * 1 if memory allocation failed
128 */
129int mpi_lset( mpi *X, int z );
130
131/**
132 * \brief Return the number of least significant bits
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000133 *
134 * \param X MPI to use
Paul Bakker5121ce52009-01-03 21:22:43 +0000135 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000136int mpi_lsb( const mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000137
138/**
139 * \brief Return the number of most significant bits
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000140 *
141 * \param X MPI to use
Paul Bakker5121ce52009-01-03 21:22:43 +0000142 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000143int mpi_msb( const mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000144
145/**
146 * \brief Return the total size in bytes
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000147 *
148 * \param X MPI to use
Paul Bakker5121ce52009-01-03 21:22:43 +0000149 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000150int mpi_size( const mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000151
152/**
153 * \brief Import from an ASCII string
154 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000155 * \param X Destination MPI
156 * \param radix Input numeric base
157 * \param s Null-terminated string buffer
Paul Bakker5121ce52009-01-03 21:22:43 +0000158 *
Paul Bakker40e46942009-01-03 21:51:57 +0000159 * \return 0 if successful, or an POLARSSL_ERR_MPI_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000160 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000161int mpi_read_string( mpi *X, int radix, const char *s );
Paul Bakker5121ce52009-01-03 21:22:43 +0000162
163/**
164 * \brief Export into an ASCII string
165 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000166 * \param X Source MPI
167 * \param radix Output numeric base
168 * \param s String buffer
169 * \param slen String buffer size
Paul Bakker5121ce52009-01-03 21:22:43 +0000170 *
Paul Bakkerff60ee62010-03-16 21:09:09 +0000171 * \return 0 if successful, or an POLARSSL_ERR_MPI_XXX error code.
172 * *slen is always updated to reflect the amount
173 * of data that has (or would have) been written.
Paul Bakker5121ce52009-01-03 21:22:43 +0000174 *
175 * \note Call this function with *slen = 0 to obtain the
176 * minimum required buffer size in *slen.
177 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000178int mpi_write_string( const mpi *X, int radix, char *s, int *slen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000179
180/**
181 * \brief Read X from an opened file
182 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000183 * \param X Destination MPI
184 * \param radix Input numeric base
185 * \param fin Input file handle
Paul Bakker5121ce52009-01-03 21:22:43 +0000186 *
Paul Bakker40e46942009-01-03 21:51:57 +0000187 * \return 0 if successful, or an POLARSSL_ERR_MPI_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000188 */
189int mpi_read_file( mpi *X, int radix, FILE *fin );
190
191/**
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000192 * \brief Write X into an opened file, or stdout if fout is NULL
Paul Bakker5121ce52009-01-03 21:22:43 +0000193 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000194 * \param p Prefix, can be NULL
195 * \param X Source MPI
196 * \param radix Output numeric base
197 * \param fout Output file handle (can be NULL)
Paul Bakker5121ce52009-01-03 21:22:43 +0000198 *
Paul Bakker40e46942009-01-03 21:51:57 +0000199 * \return 0 if successful, or an POLARSSL_ERR_MPI_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000200 *
201 * \note Set fout == NULL to print X on the console.
202 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000203int mpi_write_file( const char *p, const mpi *X, int radix, FILE *fout );
Paul Bakker5121ce52009-01-03 21:22:43 +0000204
205/**
206 * \brief Import X from unsigned binary data, big endian
207 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000208 * \param X Destination MPI
209 * \param buf Input buffer
210 * \param buflen Input buffer size
Paul Bakker5121ce52009-01-03 21:22:43 +0000211 *
212 * \return 0 if successful,
213 * 1 if memory allocation failed
214 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000215int mpi_read_binary( mpi *X, const unsigned char *buf, int buflen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000216
217/**
218 * \brief Export X into unsigned binary data, big endian
219 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000220 * \param X Source MPI
221 * \param buf Output buffer
222 * \param buflen Output buffer size
Paul Bakker5121ce52009-01-03 21:22:43 +0000223 *
224 * \return 0 if successful,
Paul Bakker40e46942009-01-03 21:51:57 +0000225 * POLARSSL_ERR_MPI_BUFFER_TOO_SMALL if buf isn't large enough
Paul Bakker5121ce52009-01-03 21:22:43 +0000226 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000227int mpi_write_binary( const mpi *X, unsigned char *buf, int buflen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000228
229/**
230 * \brief Left-shift: X <<= count
231 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000232 * \param X MPI to shift
233 * \param count Amount to shift
234 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000235 * \return 0 if successful,
236 * 1 if memory allocation failed
237 */
238int mpi_shift_l( mpi *X, int count );
239
240/**
241 * \brief Right-shift: X >>= count
242 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000243 * \param X MPI to shift
244 * \param count Amount to shift
245 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000246 * \return 0 if successful,
247 * 1 if memory allocation failed
248 */
249int mpi_shift_r( mpi *X, int count );
250
251/**
252 * \brief Compare unsigned values
253 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000254 * \param X Left-hand MPI
255 * \param Y Right-hand MPI
256 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000257 * \return 1 if |X| is greater than |Y|,
258 * -1 if |X| is lesser than |Y| or
259 * 0 if |X| is equal to |Y|
260 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000261int mpi_cmp_abs( const mpi *X, const mpi *Y );
Paul Bakker5121ce52009-01-03 21:22:43 +0000262
263/**
264 * \brief Compare signed values
265 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000266 * \param X Left-hand MPI
267 * \param Y Right-hand MPI
268 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000269 * \return 1 if X is greater than Y,
270 * -1 if X is lesser than Y or
271 * 0 if X is equal to Y
272 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000273int mpi_cmp_mpi( const mpi *X, const mpi *Y );
Paul Bakker5121ce52009-01-03 21:22:43 +0000274
275/**
276 * \brief Compare signed values
277 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000278 * \param X Left-hand MPI
279 * \param z The integer value to compare to
280 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000281 * \return 1 if X is greater than z,
282 * -1 if X is lesser than z or
283 * 0 if X is equal to z
284 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000285int mpi_cmp_int( const mpi *X, int z );
Paul Bakker5121ce52009-01-03 21:22:43 +0000286
287/**
288 * \brief Unsigned addition: X = |A| + |B|
289 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000290 * \param X Destination MPI
291 * \param A Left-hand MPI
292 * \param B Right-hand MPI
293 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000294 * \return 0 if successful,
295 * 1 if memory allocation failed
296 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000297int mpi_add_abs( mpi *X, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000298
299/**
300 * \brief Unsigned substraction: X = |A| - |B|
301 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000302 * \param X Destination MPI
303 * \param A Left-hand MPI
304 * \param B Right-hand MPI
305 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000306 * \return 0 if successful,
Paul Bakker40e46942009-01-03 21:51:57 +0000307 * POLARSSL_ERR_MPI_NEGATIVE_VALUE if B is greater than A
Paul Bakker5121ce52009-01-03 21:22:43 +0000308 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000309int mpi_sub_abs( mpi *X, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000310
311/**
312 * \brief Signed addition: X = A + B
313 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000314 * \param X Destination MPI
315 * \param A Left-hand MPI
316 * \param B Right-hand MPI
317 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000318 * \return 0 if successful,
319 * 1 if memory allocation failed
320 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000321int mpi_add_mpi( mpi *X, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000322
323/**
324 * \brief Signed substraction: X = A - B
325 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000326 * \param X Destination MPI
327 * \param A Left-hand MPI
328 * \param B Right-hand MPI
329 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000330 * \return 0 if successful,
331 * 1 if memory allocation failed
332 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000333int mpi_sub_mpi( mpi *X, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000334
335/**
336 * \brief Signed addition: X = A + b
337 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000338 * \param X Destination MPI
339 * \param A Left-hand MPI
340 * \param b The integer value to add
341 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000342 * \return 0 if successful,
343 * 1 if memory allocation failed
344 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000345int mpi_add_int( mpi *X, const mpi *A, int b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000346
347/**
348 * \brief Signed substraction: X = A - b
349 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000350 * \param X Destination MPI
351 * \param A Left-hand MPI
352 * \param b The integer value to subtract
353 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000354 * \return 0 if successful,
355 * 1 if memory allocation failed
356 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000357int mpi_sub_int( mpi *X, const mpi *A, int b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000358
359/**
360 * \brief Baseline multiplication: X = A * B
361 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000362 * \param X Destination MPI
363 * \param A Left-hand MPI
364 * \param B Right-hand MPI
365 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000366 * \return 0 if successful,
367 * 1 if memory allocation failed
368 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000369int mpi_mul_mpi( mpi *X, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000370
371/**
372 * \brief Baseline multiplication: X = A * b
Paul Bakkerce40a6d2009-06-23 19:46:08 +0000373 * Note: b is an unsigned integer type, thus
374 * Negative values of b are ignored.
Paul Bakker5121ce52009-01-03 21:22:43 +0000375 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000376 * \param X Destination MPI
377 * \param A Left-hand MPI
378 * \param b The integer value to multiply with
379 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000380 * \return 0 if successful,
381 * 1 if memory allocation failed
382 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000383int mpi_mul_int( mpi *X, const mpi *A, t_int b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000384
385/**
386 * \brief Division by mpi: A = Q * B + R
387 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000388 * \param Q Destination MPI for the quotient
389 * \param R Destination MPI for the rest value
390 * \param A Left-hand MPI
391 * \param B Right-hand MPI
392 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000393 * \return 0 if successful,
394 * 1 if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000395 * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if B == 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000396 *
397 * \note Either Q or R can be NULL.
398 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000399int mpi_div_mpi( mpi *Q, mpi *R, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000400
401/**
402 * \brief Division by int: A = Q * b + R
403 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000404 * \param Q Destination MPI for the quotient
405 * \param R Destination MPI for the rest value
406 * \param A Left-hand MPI
407 * \param b Integer to divide by
408 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000409 * \return 0 if successful,
410 * 1 if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000411 * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if b == 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000412 *
413 * \note Either Q or R can be NULL.
414 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000415int mpi_div_int( mpi *Q, mpi *R, const mpi *A, int b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000416
417/**
418 * \brief Modulo: R = A mod B
419 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000420 * \param R Destination MPI for the rest value
421 * \param A Left-hand MPI
422 * \param B Right-hand MPI
423 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000424 * \return 0 if successful,
425 * 1 if memory allocation failed,
Paul Bakkerce40a6d2009-06-23 19:46:08 +0000426 * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if B == 0,
427 * POLARSSL_ERR_MPI_NEGATIVE_VALUE if B < 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000428 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000429int mpi_mod_mpi( mpi *R, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000430
431/**
432 * \brief Modulo: r = A mod b
433 *
Paul Bakkerff60ee62010-03-16 21:09:09 +0000434 * \param r Destination t_int
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000435 * \param A Left-hand MPI
436 * \param b Integer to divide by
437 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000438 * \return 0 if successful,
439 * 1 if memory allocation failed,
Paul Bakkerce40a6d2009-06-23 19:46:08 +0000440 * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if b == 0,
441 * POLARSSL_ERR_MPI_NEGATIVE_VALUE if b < 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000442 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000443int mpi_mod_int( t_int *r, const mpi *A, int b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000444
445/**
446 * \brief Sliding-window exponentiation: X = A^E mod N
447 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000448 * \param X Destination MPI
449 * \param A Left-hand MPI
450 * \param E Exponent MPI
451 * \param N Modular MPI
452 * \param _RR Speed-up MPI used for recalculations
453 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000454 * \return 0 if successful,
455 * 1 if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000456 * POLARSSL_ERR_MPI_BAD_INPUT_DATA if N is negative or even
Paul Bakker5121ce52009-01-03 21:22:43 +0000457 *
458 * \note _RR is used to avoid re-computing R*R mod N across
459 * multiple calls, which speeds up things a bit. It can
460 * be set to NULL if the extra performance is unneeded.
461 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000462int mpi_exp_mod( mpi *X, const mpi *A, const mpi *E, const mpi *N, mpi *_RR );
Paul Bakker5121ce52009-01-03 21:22:43 +0000463
464/**
465 * \brief Greatest common divisor: G = gcd(A, B)
466 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000467 * \param G Destination MPI
468 * \param A Left-hand MPI
469 * \param B Right-hand MPI
470 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000471 * \return 0 if successful,
472 * 1 if memory allocation failed
473 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000474int mpi_gcd( mpi *G, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000475
476/**
477 * \brief Modular inverse: X = A^-1 mod N
478 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000479 * \param X Destination MPI
480 * \param A Left-hand MPI
481 * \param N Right-hand MPI
482 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000483 * \return 0 if successful,
484 * 1 if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000485 * POLARSSL_ERR_MPI_BAD_INPUT_DATA if N is negative or nil
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000486 POLARSSL_ERR_MPI_NOT_ACCEPTABLE if A has no inverse mod N
Paul Bakker5121ce52009-01-03 21:22:43 +0000487 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000488int mpi_inv_mod( mpi *X, const mpi *A, const mpi *N );
Paul Bakker5121ce52009-01-03 21:22:43 +0000489
490/**
491 * \brief Miller-Rabin primality test
492 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000493 * \param X MPI to check
494 * \param f_rng RNG function
495 * \param p_rng RNG parameter
496 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000497 * \return 0 if successful (probably prime),
498 * 1 if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000499 * POLARSSL_ERR_MPI_NOT_ACCEPTABLE if X is not prime
Paul Bakker5121ce52009-01-03 21:22:43 +0000500 */
501int mpi_is_prime( mpi *X, int (*f_rng)(void *), void *p_rng );
502
503/**
504 * \brief Prime number generation
505 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000506 * \param X Destination MPI
507 * \param nbits Required size of X in bits
508 * \param dh_flag If 1, then (X-1)/2 will be prime too
Paul Bakker5121ce52009-01-03 21:22:43 +0000509 * \param f_rng RNG function
510 * \param p_rng RNG parameter
511 *
512 * \return 0 if successful (probably prime),
513 * 1 if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000514 * POLARSSL_ERR_MPI_BAD_INPUT_DATA if nbits is < 3
Paul Bakker5121ce52009-01-03 21:22:43 +0000515 */
516int mpi_gen_prime( mpi *X, int nbits, int dh_flag,
517 int (*f_rng)(void *), void *p_rng );
518
519/**
520 * \brief Checkup routine
521 *
522 * \return 0 if successful, or 1 if the test failed
523 */
524int mpi_self_test( int verbose );
525
526#ifdef __cplusplus
527}
528#endif
529
530#endif /* bignum.h */