blob: 80399a22807785c36a5f938d93485432979bb61e [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.
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
Paul Bakker84f12b72010-07-18 10:13:04 +00007 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
Paul Bakkerb96f1542010-07-18 20:36:00 +00008 *
Paul Bakker77b385e2009-07-28 17:23:11 +00009 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000010 *
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000011 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Paul Bakker5121ce52009-01-03 21:22:43 +000024 */
Paul Bakker40e46942009-01-03 21:51:57 +000025#ifndef POLARSSL_BIGNUM_H
26#define POLARSSL_BIGNUM_H
Paul Bakker5121ce52009-01-03 21:22:43 +000027
28#include <stdio.h>
29
Paul Bakkerb5bf1762009-07-19 20:28:35 +000030#define POLARSSL_ERR_MPI_FILE_IO_ERROR 0x0002
31#define POLARSSL_ERR_MPI_BAD_INPUT_DATA 0x0004
32#define POLARSSL_ERR_MPI_INVALID_CHARACTER 0x0006
33#define POLARSSL_ERR_MPI_BUFFER_TOO_SMALL 0x0008
34#define POLARSSL_ERR_MPI_NEGATIVE_VALUE 0x000A
35#define POLARSSL_ERR_MPI_DIVISION_BY_ZERO 0x000C
36#define POLARSSL_ERR_MPI_NOT_ACCEPTABLE 0x000E
Paul Bakker5121ce52009-01-03 21:22:43 +000037
38#define MPI_CHK(f) if( ( ret = f ) != 0 ) goto cleanup
39
40/*
41 * Define the base integer type, architecture-wise
42 */
Paul Bakker40e46942009-01-03 21:51:57 +000043#if defined(POLARSSL_HAVE_INT8)
Paul Bakker5121ce52009-01-03 21:22:43 +000044typedef unsigned char t_int;
45typedef unsigned short t_dbl;
46#else
Paul Bakker40e46942009-01-03 21:51:57 +000047#if defined(POLARSSL_HAVE_INT16)
Paul Bakker5121ce52009-01-03 21:22:43 +000048typedef unsigned short t_int;
49typedef unsigned long t_dbl;
50#else
51 typedef unsigned long t_int;
52 #if defined(_MSC_VER) && defined(_M_IX86)
53 typedef unsigned __int64 t_dbl;
54 #else
55 #if defined(__amd64__) || defined(__x86_64__) || \
56 defined(__ppc64__) || defined(__powerpc64__) || \
57 defined(__ia64__) || defined(__alpha__)
58 typedef unsigned int t_dbl __attribute__((mode(TI)));
59 #else
Paul Bakker1a9382e2009-07-11 16:35:32 +000060 #if defined(POLARSSL_HAVE_LONGLONG)
61 typedef unsigned long long t_dbl;
62 #endif
Paul Bakker5121ce52009-01-03 21:22:43 +000063 #endif
64 #endif
65#endif
66#endif
67
68/**
69 * \brief MPI structure
70 */
71typedef struct
72{
73 int s; /*!< integer sign */
74 int n; /*!< total # of limbs */
75 t_int *p; /*!< pointer to limbs */
76}
77mpi;
78
79#ifdef __cplusplus
80extern "C" {
81#endif
82
83/**
84 * \brief Initialize one or more mpi
85 */
86void mpi_init( mpi *X, ... );
87
88/**
89 * \brief Unallocate one or more mpi
90 */
91void mpi_free( mpi *X, ... );
92
93/**
94 * \brief Enlarge to the specified number of limbs
95 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +000096 * \param X MPI to grow
97 * \param nblimbs The target number of limbs
98 *
Paul Bakker5121ce52009-01-03 21:22:43 +000099 * \return 0 if successful,
100 * 1 if memory allocation failed
101 */
102int mpi_grow( mpi *X, int nblimbs );
103
104/**
105 * \brief Copy the contents of Y into X
106 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000107 * \param X Destination MPI
108 * \param Y Source MPI
109 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000110 * \return 0 if successful,
111 * 1 if memory allocation failed
112 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000113int mpi_copy( mpi *X, const mpi *Y );
Paul Bakker5121ce52009-01-03 21:22:43 +0000114
115/**
116 * \brief Swap the contents of X and Y
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000117 *
118 * \param X First MPI value
119 * \param Y Second MPI value
Paul Bakker5121ce52009-01-03 21:22:43 +0000120 */
121void mpi_swap( mpi *X, mpi *Y );
122
123/**
124 * \brief Set value from integer
125 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000126 * \param X MPI to set
127 * \param z Value to use
128 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000129 * \return 0 if successful,
130 * 1 if memory allocation failed
131 */
132int mpi_lset( mpi *X, int z );
133
134/**
135 * \brief Return the number of least significant bits
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000136 *
137 * \param X MPI to use
Paul Bakker5121ce52009-01-03 21:22:43 +0000138 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000139int mpi_lsb( const mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000140
141/**
142 * \brief Return the number of most significant bits
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000143 *
144 * \param X MPI to use
Paul Bakker5121ce52009-01-03 21:22:43 +0000145 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000146int mpi_msb( const mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000147
148/**
149 * \brief Return the total size in bytes
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000150 *
151 * \param X MPI to use
Paul Bakker5121ce52009-01-03 21:22:43 +0000152 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000153int mpi_size( const mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000154
155/**
156 * \brief Import from an ASCII string
157 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000158 * \param X Destination MPI
159 * \param radix Input numeric base
160 * \param s Null-terminated string buffer
Paul Bakker5121ce52009-01-03 21:22:43 +0000161 *
Paul Bakker40e46942009-01-03 21:51:57 +0000162 * \return 0 if successful, or an POLARSSL_ERR_MPI_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000163 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000164int mpi_read_string( mpi *X, int radix, const char *s );
Paul Bakker5121ce52009-01-03 21:22:43 +0000165
166/**
167 * \brief Export into an ASCII string
168 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000169 * \param X Source MPI
170 * \param radix Output numeric base
171 * \param s String buffer
172 * \param slen String buffer size
Paul Bakker5121ce52009-01-03 21:22:43 +0000173 *
Paul Bakkerff60ee62010-03-16 21:09:09 +0000174 * \return 0 if successful, or an POLARSSL_ERR_MPI_XXX error code.
175 * *slen is always updated to reflect the amount
176 * of data that has (or would have) been written.
Paul Bakker5121ce52009-01-03 21:22:43 +0000177 *
178 * \note Call this function with *slen = 0 to obtain the
179 * minimum required buffer size in *slen.
180 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000181int mpi_write_string( const mpi *X, int radix, char *s, int *slen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000182
183/**
184 * \brief Read X from an opened file
185 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000186 * \param X Destination MPI
187 * \param radix Input numeric base
188 * \param fin Input file handle
Paul Bakker5121ce52009-01-03 21:22:43 +0000189 *
Paul Bakker40e46942009-01-03 21:51:57 +0000190 * \return 0 if successful, or an POLARSSL_ERR_MPI_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000191 */
192int mpi_read_file( mpi *X, int radix, FILE *fin );
193
194/**
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000195 * \brief Write X into an opened file, or stdout if fout is NULL
Paul Bakker5121ce52009-01-03 21:22:43 +0000196 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000197 * \param p Prefix, can be NULL
198 * \param X Source MPI
199 * \param radix Output numeric base
200 * \param fout Output file handle (can be NULL)
Paul Bakker5121ce52009-01-03 21:22:43 +0000201 *
Paul Bakker40e46942009-01-03 21:51:57 +0000202 * \return 0 if successful, or an POLARSSL_ERR_MPI_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000203 *
204 * \note Set fout == NULL to print X on the console.
205 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000206int mpi_write_file( const char *p, const mpi *X, int radix, FILE *fout );
Paul Bakker5121ce52009-01-03 21:22:43 +0000207
208/**
209 * \brief Import X from unsigned binary data, big endian
210 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000211 * \param X Destination MPI
212 * \param buf Input buffer
213 * \param buflen Input buffer size
Paul Bakker5121ce52009-01-03 21:22:43 +0000214 *
215 * \return 0 if successful,
216 * 1 if memory allocation failed
217 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000218int mpi_read_binary( mpi *X, const unsigned char *buf, int buflen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000219
220/**
221 * \brief Export X into unsigned binary data, big endian
222 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000223 * \param X Source MPI
224 * \param buf Output buffer
225 * \param buflen Output buffer size
Paul Bakker5121ce52009-01-03 21:22:43 +0000226 *
227 * \return 0 if successful,
Paul Bakker40e46942009-01-03 21:51:57 +0000228 * POLARSSL_ERR_MPI_BUFFER_TOO_SMALL if buf isn't large enough
Paul Bakker5121ce52009-01-03 21:22:43 +0000229 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000230int mpi_write_binary( const mpi *X, unsigned char *buf, int buflen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000231
232/**
233 * \brief Left-shift: X <<= count
234 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000235 * \param X MPI to shift
236 * \param count Amount to shift
237 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000238 * \return 0 if successful,
239 * 1 if memory allocation failed
240 */
241int mpi_shift_l( mpi *X, int count );
242
243/**
244 * \brief Right-shift: X >>= count
245 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000246 * \param X MPI to shift
247 * \param count Amount to shift
248 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000249 * \return 0 if successful,
250 * 1 if memory allocation failed
251 */
252int mpi_shift_r( mpi *X, int count );
253
254/**
255 * \brief Compare unsigned values
256 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000257 * \param X Left-hand MPI
258 * \param Y Right-hand MPI
259 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000260 * \return 1 if |X| is greater than |Y|,
261 * -1 if |X| is lesser than |Y| or
262 * 0 if |X| is equal to |Y|
263 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000264int mpi_cmp_abs( const mpi *X, const mpi *Y );
Paul Bakker5121ce52009-01-03 21:22:43 +0000265
266/**
267 * \brief Compare signed values
268 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000269 * \param X Left-hand MPI
270 * \param Y Right-hand MPI
271 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000272 * \return 1 if X is greater than Y,
273 * -1 if X is lesser than Y or
274 * 0 if X is equal to Y
275 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000276int mpi_cmp_mpi( const mpi *X, const mpi *Y );
Paul Bakker5121ce52009-01-03 21:22:43 +0000277
278/**
279 * \brief Compare signed values
280 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000281 * \param X Left-hand MPI
282 * \param z The integer value to compare to
283 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000284 * \return 1 if X is greater than z,
285 * -1 if X is lesser than z or
286 * 0 if X is equal to z
287 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000288int mpi_cmp_int( const mpi *X, int z );
Paul Bakker5121ce52009-01-03 21:22:43 +0000289
290/**
291 * \brief Unsigned addition: X = |A| + |B|
292 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000293 * \param X Destination MPI
294 * \param A Left-hand MPI
295 * \param B Right-hand MPI
296 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000297 * \return 0 if successful,
298 * 1 if memory allocation failed
299 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000300int mpi_add_abs( mpi *X, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000301
302/**
303 * \brief Unsigned substraction: X = |A| - |B|
304 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000305 * \param X Destination MPI
306 * \param A Left-hand MPI
307 * \param B Right-hand MPI
308 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000309 * \return 0 if successful,
Paul Bakker40e46942009-01-03 21:51:57 +0000310 * POLARSSL_ERR_MPI_NEGATIVE_VALUE if B is greater than A
Paul Bakker5121ce52009-01-03 21:22:43 +0000311 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000312int mpi_sub_abs( mpi *X, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000313
314/**
315 * \brief Signed addition: X = A + B
316 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000317 * \param X Destination MPI
318 * \param A Left-hand MPI
319 * \param B Right-hand MPI
320 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000321 * \return 0 if successful,
322 * 1 if memory allocation failed
323 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000324int mpi_add_mpi( mpi *X, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000325
326/**
327 * \brief Signed substraction: X = A - B
328 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000329 * \param X Destination MPI
330 * \param A Left-hand MPI
331 * \param B Right-hand MPI
332 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000333 * \return 0 if successful,
334 * 1 if memory allocation failed
335 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000336int mpi_sub_mpi( mpi *X, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000337
338/**
339 * \brief Signed addition: X = A + b
340 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000341 * \param X Destination MPI
342 * \param A Left-hand MPI
343 * \param b The integer value to add
344 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000345 * \return 0 if successful,
346 * 1 if memory allocation failed
347 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000348int mpi_add_int( mpi *X, const mpi *A, int b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000349
350/**
351 * \brief Signed substraction: X = A - b
352 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000353 * \param X Destination MPI
354 * \param A Left-hand MPI
355 * \param b The integer value to subtract
356 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000357 * \return 0 if successful,
358 * 1 if memory allocation failed
359 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000360int mpi_sub_int( mpi *X, const mpi *A, int b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000361
362/**
363 * \brief Baseline multiplication: X = A * B
364 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000365 * \param X Destination MPI
366 * \param A Left-hand MPI
367 * \param B Right-hand MPI
368 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000369 * \return 0 if successful,
370 * 1 if memory allocation failed
371 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000372int mpi_mul_mpi( mpi *X, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000373
374/**
375 * \brief Baseline multiplication: X = A * b
Paul Bakkerce40a6d2009-06-23 19:46:08 +0000376 * Note: b is an unsigned integer type, thus
377 * Negative values of b are ignored.
Paul Bakker5121ce52009-01-03 21:22:43 +0000378 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000379 * \param X Destination MPI
380 * \param A Left-hand MPI
381 * \param b The integer value to multiply with
382 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000383 * \return 0 if successful,
384 * 1 if memory allocation failed
385 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000386int mpi_mul_int( mpi *X, const mpi *A, t_int b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000387
388/**
389 * \brief Division by mpi: A = Q * B + R
390 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000391 * \param Q Destination MPI for the quotient
392 * \param R Destination MPI for the rest value
393 * \param A Left-hand MPI
394 * \param B Right-hand MPI
395 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000396 * \return 0 if successful,
397 * 1 if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000398 * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if B == 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000399 *
400 * \note Either Q or R can be NULL.
401 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000402int mpi_div_mpi( mpi *Q, mpi *R, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000403
404/**
405 * \brief Division by int: A = Q * b + R
406 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000407 * \param Q Destination MPI for the quotient
408 * \param R Destination MPI for the rest value
409 * \param A Left-hand MPI
410 * \param b Integer to divide by
411 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000412 * \return 0 if successful,
413 * 1 if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000414 * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if b == 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000415 *
416 * \note Either Q or R can be NULL.
417 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000418int mpi_div_int( mpi *Q, mpi *R, const mpi *A, int b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000419
420/**
421 * \brief Modulo: R = A mod B
422 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000423 * \param R Destination MPI for the rest value
424 * \param A Left-hand MPI
425 * \param B Right-hand MPI
426 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000427 * \return 0 if successful,
428 * 1 if memory allocation failed,
Paul Bakkerce40a6d2009-06-23 19:46:08 +0000429 * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if B == 0,
430 * POLARSSL_ERR_MPI_NEGATIVE_VALUE if B < 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000431 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000432int mpi_mod_mpi( mpi *R, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000433
434/**
435 * \brief Modulo: r = A mod b
436 *
Paul Bakkerff60ee62010-03-16 21:09:09 +0000437 * \param r Destination t_int
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000438 * \param A Left-hand MPI
439 * \param b Integer to divide by
440 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000441 * \return 0 if successful,
442 * 1 if memory allocation failed,
Paul Bakkerce40a6d2009-06-23 19:46:08 +0000443 * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if b == 0,
444 * POLARSSL_ERR_MPI_NEGATIVE_VALUE if b < 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000445 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000446int mpi_mod_int( t_int *r, const mpi *A, int b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000447
448/**
449 * \brief Sliding-window exponentiation: X = A^E mod N
450 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000451 * \param X Destination MPI
452 * \param A Left-hand MPI
453 * \param E Exponent MPI
454 * \param N Modular MPI
455 * \param _RR Speed-up MPI used for recalculations
456 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000457 * \return 0 if successful,
458 * 1 if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000459 * POLARSSL_ERR_MPI_BAD_INPUT_DATA if N is negative or even
Paul Bakker5121ce52009-01-03 21:22:43 +0000460 *
461 * \note _RR is used to avoid re-computing R*R mod N across
462 * multiple calls, which speeds up things a bit. It can
463 * be set to NULL if the extra performance is unneeded.
464 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000465int mpi_exp_mod( mpi *X, const mpi *A, const mpi *E, const mpi *N, mpi *_RR );
Paul Bakker5121ce52009-01-03 21:22:43 +0000466
467/**
468 * \brief Greatest common divisor: G = gcd(A, B)
469 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000470 * \param G Destination MPI
471 * \param A Left-hand MPI
472 * \param B Right-hand MPI
473 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000474 * \return 0 if successful,
475 * 1 if memory allocation failed
476 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000477int mpi_gcd( mpi *G, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000478
479/**
480 * \brief Modular inverse: X = A^-1 mod N
481 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000482 * \param X Destination MPI
483 * \param A Left-hand MPI
484 * \param N Right-hand MPI
485 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000486 * \return 0 if successful,
487 * 1 if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000488 * POLARSSL_ERR_MPI_BAD_INPUT_DATA if N is negative or nil
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000489 POLARSSL_ERR_MPI_NOT_ACCEPTABLE if A has no inverse mod N
Paul Bakker5121ce52009-01-03 21:22:43 +0000490 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000491int mpi_inv_mod( mpi *X, const mpi *A, const mpi *N );
Paul Bakker5121ce52009-01-03 21:22:43 +0000492
493/**
494 * \brief Miller-Rabin primality test
495 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000496 * \param X MPI to check
497 * \param f_rng RNG function
498 * \param p_rng RNG parameter
499 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000500 * \return 0 if successful (probably prime),
501 * 1 if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000502 * POLARSSL_ERR_MPI_NOT_ACCEPTABLE if X is not prime
Paul Bakker5121ce52009-01-03 21:22:43 +0000503 */
504int mpi_is_prime( mpi *X, int (*f_rng)(void *), void *p_rng );
505
506/**
507 * \brief Prime number generation
508 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000509 * \param X Destination MPI
510 * \param nbits Required size of X in bits
511 * \param dh_flag If 1, then (X-1)/2 will be prime too
Paul Bakker5121ce52009-01-03 21:22:43 +0000512 * \param f_rng RNG function
513 * \param p_rng RNG parameter
514 *
515 * \return 0 if successful (probably prime),
516 * 1 if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000517 * POLARSSL_ERR_MPI_BAD_INPUT_DATA if nbits is < 3
Paul Bakker5121ce52009-01-03 21:22:43 +0000518 */
519int mpi_gen_prime( mpi *X, int nbits, int dh_flag,
520 int (*f_rng)(void *), void *p_rng );
521
522/**
523 * \brief Checkup routine
524 *
525 * \return 0 if successful, or 1 if the test failed
526 */
527int mpi_self_test( int verbose );
528
529#ifdef __cplusplus
530}
531#endif
532
533#endif /* bignum.h */