blob: f4135123b81c80f435303b483859a9debddd736c [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/**
2 * \file bignum.h
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00003 *
Paul Bakker77b385e2009-07-28 17:23:11 +00004 * Copyright (C) 2006-2009, Paul Bakker <polarssl_maintainer at polarssl.org>
5 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00006 *
Paul Bakker77b385e2009-07-28 17:23:11 +00007 * Joined copyright on original XySSL code with: Christophe Devine
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00008 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Paul Bakker5121ce52009-01-03 21:22:43 +000022 */
Paul Bakker40e46942009-01-03 21:51:57 +000023#ifndef POLARSSL_BIGNUM_H
24#define POLARSSL_BIGNUM_H
Paul Bakker5121ce52009-01-03 21:22:43 +000025
26#include <stdio.h>
27
Paul Bakkerb5bf1762009-07-19 20:28:35 +000028#define POLARSSL_ERR_MPI_FILE_IO_ERROR 0x0002
29#define POLARSSL_ERR_MPI_BAD_INPUT_DATA 0x0004
30#define POLARSSL_ERR_MPI_INVALID_CHARACTER 0x0006
31#define POLARSSL_ERR_MPI_BUFFER_TOO_SMALL 0x0008
32#define POLARSSL_ERR_MPI_NEGATIVE_VALUE 0x000A
33#define POLARSSL_ERR_MPI_DIVISION_BY_ZERO 0x000C
34#define POLARSSL_ERR_MPI_NOT_ACCEPTABLE 0x000E
Paul Bakker5121ce52009-01-03 21:22:43 +000035
36#define MPI_CHK(f) if( ( ret = f ) != 0 ) goto cleanup
37
38/*
39 * Define the base integer type, architecture-wise
40 */
Paul Bakker40e46942009-01-03 21:51:57 +000041#if defined(POLARSSL_HAVE_INT8)
Paul Bakker5121ce52009-01-03 21:22:43 +000042typedef unsigned char t_int;
43typedef unsigned short t_dbl;
44#else
Paul Bakker40e46942009-01-03 21:51:57 +000045#if defined(POLARSSL_HAVE_INT16)
Paul Bakker5121ce52009-01-03 21:22:43 +000046typedef unsigned short t_int;
47typedef unsigned long t_dbl;
48#else
49 typedef unsigned long t_int;
50 #if defined(_MSC_VER) && defined(_M_IX86)
51 typedef unsigned __int64 t_dbl;
52 #else
53 #if defined(__amd64__) || defined(__x86_64__) || \
54 defined(__ppc64__) || defined(__powerpc64__) || \
55 defined(__ia64__) || defined(__alpha__)
56 typedef unsigned int t_dbl __attribute__((mode(TI)));
57 #else
Paul Bakker1a9382e2009-07-11 16:35:32 +000058 #if defined(POLARSSL_HAVE_LONGLONG)
59 typedef unsigned long long t_dbl;
60 #endif
Paul Bakker5121ce52009-01-03 21:22:43 +000061 #endif
62 #endif
63#endif
64#endif
65
66/**
67 * \brief MPI structure
68 */
69typedef struct
70{
71 int s; /*!< integer sign */
72 int n; /*!< total # of limbs */
73 t_int *p; /*!< pointer to limbs */
74}
75mpi;
76
77#ifdef __cplusplus
78extern "C" {
79#endif
80
81/**
82 * \brief Initialize one or more mpi
83 */
84void mpi_init( mpi *X, ... );
85
86/**
87 * \brief Unallocate one or more mpi
88 */
89void mpi_free( mpi *X, ... );
90
91/**
92 * \brief Enlarge to the specified number of limbs
93 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +000094 * \param X MPI to grow
95 * \param nblimbs The target number of limbs
96 *
Paul Bakker5121ce52009-01-03 21:22:43 +000097 * \return 0 if successful,
98 * 1 if memory allocation failed
99 */
100int mpi_grow( mpi *X, int nblimbs );
101
102/**
103 * \brief Copy the contents of Y into X
104 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000105 * \param X Destination MPI
106 * \param Y Source MPI
107 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000108 * \return 0 if successful,
109 * 1 if memory allocation failed
110 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000111int mpi_copy( mpi *X, const mpi *Y );
Paul Bakker5121ce52009-01-03 21:22:43 +0000112
113/**
114 * \brief Swap the contents of X and Y
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000115 *
116 * \param X First MPI value
117 * \param Y Second MPI value
Paul Bakker5121ce52009-01-03 21:22:43 +0000118 */
119void mpi_swap( mpi *X, mpi *Y );
120
121/**
122 * \brief Set value from integer
123 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000124 * \param X MPI to set
125 * \param z Value to use
126 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000127 * \return 0 if successful,
128 * 1 if memory allocation failed
129 */
130int mpi_lset( mpi *X, int z );
131
132/**
133 * \brief Return the number of least significant bits
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000134 *
135 * \param X MPI to use
Paul Bakker5121ce52009-01-03 21:22:43 +0000136 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000137int mpi_lsb( const mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000138
139/**
140 * \brief Return the number of most significant bits
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000141 *
142 * \param X MPI to use
Paul Bakker5121ce52009-01-03 21:22:43 +0000143 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000144int mpi_msb( const mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000145
146/**
147 * \brief Return the total size in bytes
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000148 *
149 * \param X MPI to use
Paul Bakker5121ce52009-01-03 21:22:43 +0000150 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000151int mpi_size( const mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000152
153/**
154 * \brief Import from an ASCII string
155 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000156 * \param X Destination MPI
157 * \param radix Input numeric base
158 * \param s Null-terminated string buffer
Paul Bakker5121ce52009-01-03 21:22:43 +0000159 *
Paul Bakker40e46942009-01-03 21:51:57 +0000160 * \return 0 if successful, or an POLARSSL_ERR_MPI_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000161 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000162int mpi_read_string( mpi *X, int radix, const char *s );
Paul Bakker5121ce52009-01-03 21:22:43 +0000163
164/**
165 * \brief Export into an ASCII string
166 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000167 * \param X Source MPI
168 * \param radix Output numeric base
169 * \param s String buffer
170 * \param slen String buffer size
Paul Bakker5121ce52009-01-03 21:22:43 +0000171 *
Paul Bakkerff60ee62010-03-16 21:09:09 +0000172 * \return 0 if successful, or an POLARSSL_ERR_MPI_XXX error code.
173 * *slen is always updated to reflect the amount
174 * of data that has (or would have) been written.
Paul Bakker5121ce52009-01-03 21:22:43 +0000175 *
176 * \note Call this function with *slen = 0 to obtain the
177 * minimum required buffer size in *slen.
178 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000179int mpi_write_string( const mpi *X, int radix, char *s, int *slen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000180
181/**
182 * \brief Read X from an opened file
183 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000184 * \param X Destination MPI
185 * \param radix Input numeric base
186 * \param fin Input file handle
Paul Bakker5121ce52009-01-03 21:22:43 +0000187 *
Paul Bakker40e46942009-01-03 21:51:57 +0000188 * \return 0 if successful, or an POLARSSL_ERR_MPI_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000189 */
190int mpi_read_file( mpi *X, int radix, FILE *fin );
191
192/**
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000193 * \brief Write X into an opened file, or stdout if fout is NULL
Paul Bakker5121ce52009-01-03 21:22:43 +0000194 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000195 * \param p Prefix, can be NULL
196 * \param X Source MPI
197 * \param radix Output numeric base
198 * \param fout Output file handle (can be NULL)
Paul Bakker5121ce52009-01-03 21:22:43 +0000199 *
Paul Bakker40e46942009-01-03 21:51:57 +0000200 * \return 0 if successful, or an POLARSSL_ERR_MPI_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000201 *
202 * \note Set fout == NULL to print X on the console.
203 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000204int mpi_write_file( const char *p, const mpi *X, int radix, FILE *fout );
Paul Bakker5121ce52009-01-03 21:22:43 +0000205
206/**
207 * \brief Import X from unsigned binary data, big endian
208 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000209 * \param X Destination MPI
210 * \param buf Input buffer
211 * \param buflen Input buffer size
Paul Bakker5121ce52009-01-03 21:22:43 +0000212 *
213 * \return 0 if successful,
214 * 1 if memory allocation failed
215 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000216int mpi_read_binary( mpi *X, const unsigned char *buf, int buflen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000217
218/**
219 * \brief Export X into unsigned binary data, big endian
220 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000221 * \param X Source MPI
222 * \param buf Output buffer
223 * \param buflen Output buffer size
Paul Bakker5121ce52009-01-03 21:22:43 +0000224 *
225 * \return 0 if successful,
Paul Bakker40e46942009-01-03 21:51:57 +0000226 * POLARSSL_ERR_MPI_BUFFER_TOO_SMALL if buf isn't large enough
Paul Bakker5121ce52009-01-03 21:22:43 +0000227 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000228int mpi_write_binary( const mpi *X, unsigned char *buf, int buflen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000229
230/**
231 * \brief Left-shift: X <<= count
232 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000233 * \param X MPI to shift
234 * \param count Amount to shift
235 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000236 * \return 0 if successful,
237 * 1 if memory allocation failed
238 */
239int mpi_shift_l( mpi *X, int count );
240
241/**
242 * \brief Right-shift: X >>= count
243 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000244 * \param X MPI to shift
245 * \param count Amount to shift
246 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000247 * \return 0 if successful,
248 * 1 if memory allocation failed
249 */
250int mpi_shift_r( mpi *X, int count );
251
252/**
253 * \brief Compare unsigned values
254 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000255 * \param X Left-hand MPI
256 * \param Y Right-hand MPI
257 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000258 * \return 1 if |X| is greater than |Y|,
259 * -1 if |X| is lesser than |Y| or
260 * 0 if |X| is equal to |Y|
261 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000262int mpi_cmp_abs( const mpi *X, const mpi *Y );
Paul Bakker5121ce52009-01-03 21:22:43 +0000263
264/**
265 * \brief Compare signed values
266 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000267 * \param X Left-hand MPI
268 * \param Y Right-hand MPI
269 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000270 * \return 1 if X is greater than Y,
271 * -1 if X is lesser than Y or
272 * 0 if X is equal to Y
273 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000274int mpi_cmp_mpi( const mpi *X, const mpi *Y );
Paul Bakker5121ce52009-01-03 21:22:43 +0000275
276/**
277 * \brief Compare signed values
278 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000279 * \param X Left-hand MPI
280 * \param z The integer value to compare to
281 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000282 * \return 1 if X is greater than z,
283 * -1 if X is lesser than z or
284 * 0 if X is equal to z
285 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000286int mpi_cmp_int( const mpi *X, int z );
Paul Bakker5121ce52009-01-03 21:22:43 +0000287
288/**
289 * \brief Unsigned addition: X = |A| + |B|
290 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000291 * \param X Destination MPI
292 * \param A Left-hand MPI
293 * \param B Right-hand MPI
294 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000295 * \return 0 if successful,
296 * 1 if memory allocation failed
297 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000298int mpi_add_abs( mpi *X, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000299
300/**
301 * \brief Unsigned substraction: X = |A| - |B|
302 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000303 * \param X Destination MPI
304 * \param A Left-hand MPI
305 * \param B Right-hand MPI
306 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000307 * \return 0 if successful,
Paul Bakker40e46942009-01-03 21:51:57 +0000308 * POLARSSL_ERR_MPI_NEGATIVE_VALUE if B is greater than A
Paul Bakker5121ce52009-01-03 21:22:43 +0000309 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000310int mpi_sub_abs( mpi *X, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000311
312/**
313 * \brief Signed addition: X = A + B
314 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000315 * \param X Destination MPI
316 * \param A Left-hand MPI
317 * \param B Right-hand MPI
318 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000319 * \return 0 if successful,
320 * 1 if memory allocation failed
321 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000322int mpi_add_mpi( mpi *X, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000323
324/**
325 * \brief Signed substraction: X = A - B
326 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000327 * \param X Destination MPI
328 * \param A Left-hand MPI
329 * \param B Right-hand MPI
330 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000331 * \return 0 if successful,
332 * 1 if memory allocation failed
333 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000334int mpi_sub_mpi( mpi *X, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000335
336/**
337 * \brief Signed addition: X = A + b
338 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000339 * \param X Destination MPI
340 * \param A Left-hand MPI
341 * \param b The integer value to add
342 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000343 * \return 0 if successful,
344 * 1 if memory allocation failed
345 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000346int mpi_add_int( mpi *X, const mpi *A, int b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000347
348/**
349 * \brief Signed substraction: X = A - b
350 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000351 * \param X Destination MPI
352 * \param A Left-hand MPI
353 * \param b The integer value to subtract
354 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000355 * \return 0 if successful,
356 * 1 if memory allocation failed
357 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000358int mpi_sub_int( mpi *X, const mpi *A, int b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000359
360/**
361 * \brief Baseline multiplication: X = A * B
362 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000363 * \param X Destination MPI
364 * \param A Left-hand MPI
365 * \param B Right-hand MPI
366 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000367 * \return 0 if successful,
368 * 1 if memory allocation failed
369 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000370int mpi_mul_mpi( mpi *X, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000371
372/**
373 * \brief Baseline multiplication: X = A * b
Paul Bakkerce40a6d2009-06-23 19:46:08 +0000374 * Note: b is an unsigned integer type, thus
375 * Negative values of b are ignored.
Paul Bakker5121ce52009-01-03 21:22:43 +0000376 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000377 * \param X Destination MPI
378 * \param A Left-hand MPI
379 * \param b The integer value to multiply with
380 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000381 * \return 0 if successful,
382 * 1 if memory allocation failed
383 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000384int mpi_mul_int( mpi *X, const mpi *A, t_int b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000385
386/**
387 * \brief Division by mpi: A = Q * B + R
388 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000389 * \param Q Destination MPI for the quotient
390 * \param R Destination MPI for the rest value
391 * \param A Left-hand MPI
392 * \param B Right-hand MPI
393 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000394 * \return 0 if successful,
395 * 1 if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000396 * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if B == 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000397 *
398 * \note Either Q or R can be NULL.
399 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000400int mpi_div_mpi( mpi *Q, mpi *R, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000401
402/**
403 * \brief Division by int: A = Q * b + R
404 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000405 * \param Q Destination MPI for the quotient
406 * \param R Destination MPI for the rest value
407 * \param A Left-hand MPI
408 * \param b Integer to divide by
409 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000410 * \return 0 if successful,
411 * 1 if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000412 * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if b == 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000413 *
414 * \note Either Q or R can be NULL.
415 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000416int mpi_div_int( mpi *Q, mpi *R, const mpi *A, int b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000417
418/**
419 * \brief Modulo: R = A mod B
420 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000421 * \param R Destination MPI for the rest value
422 * \param A Left-hand MPI
423 * \param B Right-hand MPI
424 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000425 * \return 0 if successful,
426 * 1 if memory allocation failed,
Paul Bakkerce40a6d2009-06-23 19:46:08 +0000427 * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if B == 0,
428 * POLARSSL_ERR_MPI_NEGATIVE_VALUE if B < 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000429 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000430int mpi_mod_mpi( mpi *R, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000431
432/**
433 * \brief Modulo: r = A mod b
434 *
Paul Bakkerff60ee62010-03-16 21:09:09 +0000435 * \param r Destination t_int
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000436 * \param A Left-hand MPI
437 * \param b Integer to divide by
438 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000439 * \return 0 if successful,
440 * 1 if memory allocation failed,
Paul Bakkerce40a6d2009-06-23 19:46:08 +0000441 * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if b == 0,
442 * POLARSSL_ERR_MPI_NEGATIVE_VALUE if b < 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000443 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000444int mpi_mod_int( t_int *r, const mpi *A, int b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000445
446/**
447 * \brief Sliding-window exponentiation: X = A^E mod N
448 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000449 * \param X Destination MPI
450 * \param A Left-hand MPI
451 * \param E Exponent MPI
452 * \param N Modular MPI
453 * \param _RR Speed-up MPI used for recalculations
454 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000455 * \return 0 if successful,
456 * 1 if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000457 * POLARSSL_ERR_MPI_BAD_INPUT_DATA if N is negative or even
Paul Bakker5121ce52009-01-03 21:22:43 +0000458 *
459 * \note _RR is used to avoid re-computing R*R mod N across
460 * multiple calls, which speeds up things a bit. It can
461 * be set to NULL if the extra performance is unneeded.
462 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000463int mpi_exp_mod( mpi *X, const mpi *A, const mpi *E, const mpi *N, mpi *_RR );
Paul Bakker5121ce52009-01-03 21:22:43 +0000464
465/**
466 * \brief Greatest common divisor: G = gcd(A, B)
467 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000468 * \param G Destination MPI
469 * \param A Left-hand MPI
470 * \param B Right-hand MPI
471 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000472 * \return 0 if successful,
473 * 1 if memory allocation failed
474 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000475int mpi_gcd( mpi *G, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000476
477/**
478 * \brief Modular inverse: X = A^-1 mod N
479 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000480 * \param X Destination MPI
481 * \param A Left-hand MPI
482 * \param N Right-hand MPI
483 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000484 * \return 0 if successful,
485 * 1 if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000486 * POLARSSL_ERR_MPI_BAD_INPUT_DATA if N is negative or nil
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000487 POLARSSL_ERR_MPI_NOT_ACCEPTABLE if A has no inverse mod N
Paul Bakker5121ce52009-01-03 21:22:43 +0000488 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000489int mpi_inv_mod( mpi *X, const mpi *A, const mpi *N );
Paul Bakker5121ce52009-01-03 21:22:43 +0000490
491/**
492 * \brief Miller-Rabin primality test
493 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000494 * \param X MPI to check
495 * \param f_rng RNG function
496 * \param p_rng RNG parameter
497 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000498 * \return 0 if successful (probably prime),
499 * 1 if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000500 * POLARSSL_ERR_MPI_NOT_ACCEPTABLE if X is not prime
Paul Bakker5121ce52009-01-03 21:22:43 +0000501 */
502int mpi_is_prime( mpi *X, int (*f_rng)(void *), void *p_rng );
503
504/**
505 * \brief Prime number generation
506 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000507 * \param X Destination MPI
508 * \param nbits Required size of X in bits
509 * \param dh_flag If 1, then (X-1)/2 will be prime too
Paul Bakker5121ce52009-01-03 21:22:43 +0000510 * \param f_rng RNG function
511 * \param p_rng RNG parameter
512 *
513 * \return 0 if successful (probably prime),
514 * 1 if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000515 * POLARSSL_ERR_MPI_BAD_INPUT_DATA if nbits is < 3
Paul Bakker5121ce52009-01-03 21:22:43 +0000516 */
517int mpi_gen_prime( mpi *X, int nbits, int dh_flag,
518 int (*f_rng)(void *), void *p_rng );
519
520/**
521 * \brief Checkup routine
522 *
523 * \return 0 if successful, or 1 if the test failed
524 */
525int mpi_self_test( int verbose );
526
527#ifdef __cplusplus
528}
529#endif
530
531#endif /* bignum.h */