blob: 00164e03f06b7d207dc17662b070febf16c591cc [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>
Paul Bakker23986e52011-04-24 08:57:21 +000031#include <string.h>
Paul Bakker5121ce52009-01-03 21:22:43 +000032
Paul Bakkerb5bf1762009-07-19 20:28:35 +000033#define POLARSSL_ERR_MPI_FILE_IO_ERROR 0x0002
34#define POLARSSL_ERR_MPI_BAD_INPUT_DATA 0x0004
35#define POLARSSL_ERR_MPI_INVALID_CHARACTER 0x0006
36#define POLARSSL_ERR_MPI_BUFFER_TOO_SMALL 0x0008
37#define POLARSSL_ERR_MPI_NEGATIVE_VALUE 0x000A
38#define POLARSSL_ERR_MPI_DIVISION_BY_ZERO 0x000C
39#define POLARSSL_ERR_MPI_NOT_ACCEPTABLE 0x000E
Paul Bakker5121ce52009-01-03 21:22:43 +000040
41#define MPI_CHK(f) if( ( ret = f ) != 0 ) goto cleanup
42
43/*
44 * Define the base integer type, architecture-wise
45 */
Paul Bakker40e46942009-01-03 21:51:57 +000046#if defined(POLARSSL_HAVE_INT8)
Paul Bakkera755ca12011-04-24 09:11:17 +000047typedef signed char t_sint;
48typedef unsigned char t_uint;
49typedef unsigned short t_udbl;
Paul Bakker5121ce52009-01-03 21:22:43 +000050#else
Paul Bakker40e46942009-01-03 21:51:57 +000051#if defined(POLARSSL_HAVE_INT16)
Paul Bakkera755ca12011-04-24 09:11:17 +000052typedef signed short t_sint;
53typedef unsigned short t_uint;
54typedef unsigned long t_udbl;
Paul Bakker5121ce52009-01-03 21:22:43 +000055#else
Paul Bakkera755ca12011-04-24 09:11:17 +000056 typedef signed long t_sint;
57 typedef unsigned long t_uint;
Paul Bakker5121ce52009-01-03 21:22:43 +000058 #if defined(_MSC_VER) && defined(_M_IX86)
Paul Bakkera755ca12011-04-24 09:11:17 +000059 typedef unsigned __int64 t_udbl;
Paul Bakker5121ce52009-01-03 21:22:43 +000060 #else
61 #if defined(__amd64__) || defined(__x86_64__) || \
62 defined(__ppc64__) || defined(__powerpc64__) || \
63 defined(__ia64__) || defined(__alpha__)
Paul Bakkera755ca12011-04-24 09:11:17 +000064 typedef unsigned int t_udbl __attribute__((mode(TI)));
Paul Bakker5121ce52009-01-03 21:22:43 +000065 #else
Paul Bakker1a9382e2009-07-11 16:35:32 +000066 #if defined(POLARSSL_HAVE_LONGLONG)
Paul Bakkera755ca12011-04-24 09:11:17 +000067 typedef unsigned long long t_udbl;
Paul Bakker1a9382e2009-07-11 16:35:32 +000068 #endif
Paul Bakker5121ce52009-01-03 21:22:43 +000069 #endif
70 #endif
71#endif
72#endif
73
74/**
75 * \brief MPI structure
76 */
77typedef struct
78{
79 int s; /*!< integer sign */
Paul Bakker23986e52011-04-24 08:57:21 +000080 size_t n; /*!< total # of limbs */
Paul Bakkera755ca12011-04-24 09:11:17 +000081 t_uint *p; /*!< pointer to limbs */
Paul Bakker5121ce52009-01-03 21:22:43 +000082}
83mpi;
84
85#ifdef __cplusplus
86extern "C" {
87#endif
88
89/**
90 * \brief Initialize one or more mpi
91 */
92void mpi_init( mpi *X, ... );
93
94/**
95 * \brief Unallocate one or more mpi
96 */
97void mpi_free( mpi *X, ... );
98
99/**
100 * \brief Enlarge to the specified number of limbs
101 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000102 * \param X MPI to grow
103 * \param nblimbs The target number of limbs
104 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000105 * \return 0 if successful,
106 * 1 if memory allocation failed
107 */
Paul Bakker23986e52011-04-24 08:57:21 +0000108int mpi_grow( mpi *X, size_t nblimbs );
Paul Bakker5121ce52009-01-03 21:22:43 +0000109
110/**
111 * \brief Copy the contents of Y into X
112 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000113 * \param X Destination MPI
114 * \param Y Source MPI
115 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000116 * \return 0 if successful,
117 * 1 if memory allocation failed
118 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000119int mpi_copy( mpi *X, const mpi *Y );
Paul Bakker5121ce52009-01-03 21:22:43 +0000120
121/**
122 * \brief Swap the contents of X and Y
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000123 *
124 * \param X First MPI value
125 * \param Y Second MPI value
Paul Bakker5121ce52009-01-03 21:22:43 +0000126 */
127void mpi_swap( mpi *X, mpi *Y );
128
129/**
130 * \brief Set value from integer
131 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000132 * \param X MPI to set
133 * \param z Value to use
134 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000135 * \return 0 if successful,
136 * 1 if memory allocation failed
137 */
Paul Bakkera755ca12011-04-24 09:11:17 +0000138int mpi_lset( mpi *X, t_sint z );
Paul Bakker5121ce52009-01-03 21:22:43 +0000139
140/**
141 * \brief Return the number of least significant bits
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000142 *
143 * \param X MPI to use
Paul Bakker5121ce52009-01-03 21:22:43 +0000144 */
Paul Bakker23986e52011-04-24 08:57:21 +0000145size_t mpi_lsb( const mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000146
147/**
148 * \brief Return the number of most significant bits
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000149 *
150 * \param X MPI to use
Paul Bakker5121ce52009-01-03 21:22:43 +0000151 */
Paul Bakker23986e52011-04-24 08:57:21 +0000152size_t mpi_msb( const mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000153
154/**
155 * \brief Return the total size in bytes
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000156 *
157 * \param X MPI to use
Paul Bakker5121ce52009-01-03 21:22:43 +0000158 */
Paul Bakker23986e52011-04-24 08:57:21 +0000159size_t mpi_size( const mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000160
161/**
162 * \brief Import from an ASCII string
163 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000164 * \param X Destination MPI
165 * \param radix Input numeric base
166 * \param s Null-terminated string buffer
Paul Bakker5121ce52009-01-03 21:22:43 +0000167 *
Paul Bakker40e46942009-01-03 21:51:57 +0000168 * \return 0 if successful, or an POLARSSL_ERR_MPI_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000169 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000170int mpi_read_string( mpi *X, int radix, const char *s );
Paul Bakker5121ce52009-01-03 21:22:43 +0000171
172/**
173 * \brief Export into an ASCII string
174 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000175 * \param X Source MPI
176 * \param radix Output numeric base
177 * \param s String buffer
178 * \param slen String buffer size
Paul Bakker5121ce52009-01-03 21:22:43 +0000179 *
Paul Bakkerff60ee62010-03-16 21:09:09 +0000180 * \return 0 if successful, or an POLARSSL_ERR_MPI_XXX error code.
181 * *slen is always updated to reflect the amount
182 * of data that has (or would have) been written.
Paul Bakker5121ce52009-01-03 21:22:43 +0000183 *
184 * \note Call this function with *slen = 0 to obtain the
185 * minimum required buffer size in *slen.
186 */
Paul Bakker23986e52011-04-24 08:57:21 +0000187int mpi_write_string( const mpi *X, int radix, char *s, size_t *slen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000188
189/**
190 * \brief Read X from an opened file
191 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000192 * \param X Destination MPI
193 * \param radix Input numeric base
194 * \param fin Input file handle
Paul Bakker5121ce52009-01-03 21:22:43 +0000195 *
Paul Bakker40e46942009-01-03 21:51:57 +0000196 * \return 0 if successful, or an POLARSSL_ERR_MPI_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000197 */
198int mpi_read_file( mpi *X, int radix, FILE *fin );
199
200/**
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000201 * \brief Write X into an opened file, or stdout if fout is NULL
Paul Bakker5121ce52009-01-03 21:22:43 +0000202 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000203 * \param p Prefix, can be NULL
204 * \param X Source MPI
205 * \param radix Output numeric base
206 * \param fout Output file handle (can be NULL)
Paul Bakker5121ce52009-01-03 21:22:43 +0000207 *
Paul Bakker40e46942009-01-03 21:51:57 +0000208 * \return 0 if successful, or an POLARSSL_ERR_MPI_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000209 *
210 * \note Set fout == NULL to print X on the console.
211 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000212int mpi_write_file( const char *p, const mpi *X, int radix, FILE *fout );
Paul Bakker5121ce52009-01-03 21:22:43 +0000213
214/**
215 * \brief Import X from unsigned binary data, big endian
216 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000217 * \param X Destination MPI
218 * \param buf Input buffer
219 * \param buflen Input buffer size
Paul Bakker5121ce52009-01-03 21:22:43 +0000220 *
221 * \return 0 if successful,
222 * 1 if memory allocation failed
223 */
Paul Bakker23986e52011-04-24 08:57:21 +0000224int mpi_read_binary( mpi *X, const unsigned char *buf, size_t buflen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000225
226/**
227 * \brief Export X into unsigned binary data, big endian
228 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000229 * \param X Source MPI
230 * \param buf Output buffer
231 * \param buflen Output buffer size
Paul Bakker5121ce52009-01-03 21:22:43 +0000232 *
233 * \return 0 if successful,
Paul Bakker40e46942009-01-03 21:51:57 +0000234 * POLARSSL_ERR_MPI_BUFFER_TOO_SMALL if buf isn't large enough
Paul Bakker5121ce52009-01-03 21:22:43 +0000235 */
Paul Bakker23986e52011-04-24 08:57:21 +0000236int mpi_write_binary( const mpi *X, unsigned char *buf, size_t buflen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000237
238/**
239 * \brief Left-shift: X <<= count
240 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000241 * \param X MPI to shift
242 * \param count Amount to shift
243 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000244 * \return 0 if successful,
245 * 1 if memory allocation failed
246 */
Paul Bakker23986e52011-04-24 08:57:21 +0000247int mpi_shift_l( mpi *X, size_t count );
Paul Bakker5121ce52009-01-03 21:22:43 +0000248
249/**
250 * \brief Right-shift: X >>= count
251 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000252 * \param X MPI to shift
253 * \param count Amount to shift
254 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000255 * \return 0 if successful,
256 * 1 if memory allocation failed
257 */
Paul Bakker23986e52011-04-24 08:57:21 +0000258int mpi_shift_r( mpi *X, size_t count );
Paul Bakker5121ce52009-01-03 21:22:43 +0000259
260/**
261 * \brief Compare unsigned values
262 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000263 * \param X Left-hand MPI
264 * \param Y Right-hand MPI
265 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000266 * \return 1 if |X| is greater than |Y|,
267 * -1 if |X| is lesser than |Y| or
268 * 0 if |X| is equal to |Y|
269 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000270int mpi_cmp_abs( const mpi *X, const mpi *Y );
Paul Bakker5121ce52009-01-03 21:22:43 +0000271
272/**
273 * \brief Compare signed values
274 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000275 * \param X Left-hand MPI
276 * \param Y Right-hand MPI
277 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000278 * \return 1 if X is greater than Y,
279 * -1 if X is lesser than Y or
280 * 0 if X is equal to Y
281 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000282int mpi_cmp_mpi( const mpi *X, const mpi *Y );
Paul Bakker5121ce52009-01-03 21:22:43 +0000283
284/**
285 * \brief Compare signed values
286 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000287 * \param X Left-hand MPI
288 * \param z The integer value to compare to
289 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000290 * \return 1 if X is greater than z,
291 * -1 if X is lesser than z or
292 * 0 if X is equal to z
293 */
Paul Bakkera755ca12011-04-24 09:11:17 +0000294int mpi_cmp_int( const mpi *X, t_sint z );
Paul Bakker5121ce52009-01-03 21:22:43 +0000295
296/**
297 * \brief Unsigned addition: X = |A| + |B|
298 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000299 * \param X Destination MPI
300 * \param A Left-hand MPI
301 * \param B Right-hand MPI
302 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000303 * \return 0 if successful,
304 * 1 if memory allocation failed
305 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000306int mpi_add_abs( mpi *X, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000307
308/**
309 * \brief Unsigned substraction: X = |A| - |B|
310 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000311 * \param X Destination MPI
312 * \param A Left-hand MPI
313 * \param B Right-hand MPI
314 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000315 * \return 0 if successful,
Paul Bakker40e46942009-01-03 21:51:57 +0000316 * POLARSSL_ERR_MPI_NEGATIVE_VALUE if B is greater than A
Paul Bakker5121ce52009-01-03 21:22:43 +0000317 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000318int mpi_sub_abs( mpi *X, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000319
320/**
321 * \brief Signed addition: X = A + B
322 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000323 * \param X Destination MPI
324 * \param A Left-hand MPI
325 * \param B Right-hand MPI
326 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000327 * \return 0 if successful,
328 * 1 if memory allocation failed
329 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000330int mpi_add_mpi( mpi *X, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000331
332/**
333 * \brief Signed substraction: X = A - B
334 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000335 * \param X Destination MPI
336 * \param A Left-hand MPI
337 * \param B Right-hand MPI
338 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000339 * \return 0 if successful,
340 * 1 if memory allocation failed
341 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000342int mpi_sub_mpi( mpi *X, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000343
344/**
345 * \brief Signed addition: X = A + b
346 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000347 * \param X Destination MPI
348 * \param A Left-hand MPI
349 * \param b The integer value to add
350 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000351 * \return 0 if successful,
352 * 1 if memory allocation failed
353 */
Paul Bakkera755ca12011-04-24 09:11:17 +0000354int mpi_add_int( mpi *X, const mpi *A, t_sint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000355
356/**
357 * \brief Signed substraction: X = A - b
358 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000359 * \param X Destination MPI
360 * \param A Left-hand MPI
361 * \param b The integer value to subtract
362 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000363 * \return 0 if successful,
364 * 1 if memory allocation failed
365 */
Paul Bakkera755ca12011-04-24 09:11:17 +0000366int mpi_sub_int( mpi *X, const mpi *A, t_sint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000367
368/**
369 * \brief Baseline multiplication: X = A * B
370 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000371 * \param X Destination MPI
372 * \param A Left-hand MPI
373 * \param B Right-hand MPI
374 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000375 * \return 0 if successful,
376 * 1 if memory allocation failed
377 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000378int mpi_mul_mpi( mpi *X, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000379
380/**
381 * \brief Baseline multiplication: X = A * b
Paul Bakkerce40a6d2009-06-23 19:46:08 +0000382 * Note: b is an unsigned integer type, thus
383 * Negative values of b are ignored.
Paul Bakker5121ce52009-01-03 21:22:43 +0000384 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000385 * \param X Destination MPI
386 * \param A Left-hand MPI
387 * \param b The integer value to multiply with
388 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000389 * \return 0 if successful,
390 * 1 if memory allocation failed
391 */
Paul Bakkera755ca12011-04-24 09:11:17 +0000392int mpi_mul_int( mpi *X, const mpi *A, t_sint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000393
394/**
395 * \brief Division by mpi: A = Q * B + R
396 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000397 * \param Q Destination MPI for the quotient
398 * \param R Destination MPI for the rest value
399 * \param A Left-hand MPI
400 * \param B Right-hand MPI
401 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000402 * \return 0 if successful,
403 * 1 if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000404 * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if B == 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000405 *
406 * \note Either Q or R can be NULL.
407 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000408int mpi_div_mpi( mpi *Q, mpi *R, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000409
410/**
411 * \brief Division by int: A = Q * b + R
412 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000413 * \param Q Destination MPI for the quotient
414 * \param R Destination MPI for the rest value
415 * \param A Left-hand MPI
416 * \param b Integer to divide by
417 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000418 * \return 0 if successful,
419 * 1 if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000420 * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if b == 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000421 *
422 * \note Either Q or R can be NULL.
423 */
Paul Bakkera755ca12011-04-24 09:11:17 +0000424int mpi_div_int( mpi *Q, mpi *R, const mpi *A, t_sint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000425
426/**
427 * \brief Modulo: R = A mod B
428 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000429 * \param R Destination MPI for the rest value
430 * \param A Left-hand MPI
431 * \param B Right-hand MPI
432 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000433 * \return 0 if successful,
434 * 1 if memory allocation failed,
Paul Bakkerce40a6d2009-06-23 19:46:08 +0000435 * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if B == 0,
436 * POLARSSL_ERR_MPI_NEGATIVE_VALUE if B < 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000437 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000438int mpi_mod_mpi( mpi *R, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000439
440/**
441 * \brief Modulo: r = A mod b
442 *
Paul Bakkera755ca12011-04-24 09:11:17 +0000443 * \param r Destination t_uint
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000444 * \param A Left-hand MPI
445 * \param b Integer to divide by
446 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000447 * \return 0 if successful,
448 * 1 if memory allocation failed,
Paul Bakkerce40a6d2009-06-23 19:46:08 +0000449 * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if b == 0,
450 * POLARSSL_ERR_MPI_NEGATIVE_VALUE if b < 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000451 */
Paul Bakkera755ca12011-04-24 09:11:17 +0000452int mpi_mod_int( t_uint *r, const mpi *A, t_sint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000453
454/**
455 * \brief Sliding-window exponentiation: X = A^E mod N
456 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000457 * \param X Destination MPI
458 * \param A Left-hand MPI
459 * \param E Exponent MPI
460 * \param N Modular MPI
461 * \param _RR Speed-up MPI used for recalculations
462 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000463 * \return 0 if successful,
464 * 1 if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000465 * POLARSSL_ERR_MPI_BAD_INPUT_DATA if N is negative or even
Paul Bakker5121ce52009-01-03 21:22:43 +0000466 *
467 * \note _RR is used to avoid re-computing R*R mod N across
468 * multiple calls, which speeds up things a bit. It can
469 * be set to NULL if the extra performance is unneeded.
470 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000471int mpi_exp_mod( mpi *X, const mpi *A, const mpi *E, const mpi *N, mpi *_RR );
Paul Bakker5121ce52009-01-03 21:22:43 +0000472
473/**
Paul Bakker287781a2011-03-26 13:18:49 +0000474 * \brief Fill an MPI X with size bytes of random
475 *
476 * \param X Destination MPI
477 * \param size Size in bytes
478 * \param f_rng RNG function
479 * \param p_rng RNG parameter
480 *
481 * \return 0 if successful,
482 * 1 if memory allocation failed
483 */
Paul Bakker23986e52011-04-24 08:57:21 +0000484int mpi_fill_random( mpi *X, size_t size, int (*f_rng)(void *), void *p_rng );
Paul Bakker287781a2011-03-26 13:18:49 +0000485
486/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000487 * \brief Greatest common divisor: G = gcd(A, B)
488 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000489 * \param G Destination MPI
490 * \param A Left-hand MPI
491 * \param B Right-hand MPI
492 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000493 * \return 0 if successful,
494 * 1 if memory allocation failed
495 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000496int mpi_gcd( mpi *G, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000497
498/**
499 * \brief Modular inverse: X = A^-1 mod N
500 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000501 * \param X Destination MPI
502 * \param A Left-hand MPI
503 * \param N Right-hand MPI
504 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000505 * \return 0 if successful,
506 * 1 if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000507 * POLARSSL_ERR_MPI_BAD_INPUT_DATA if N is negative or nil
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000508 POLARSSL_ERR_MPI_NOT_ACCEPTABLE if A has no inverse mod N
Paul Bakker5121ce52009-01-03 21:22:43 +0000509 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000510int mpi_inv_mod( mpi *X, const mpi *A, const mpi *N );
Paul Bakker5121ce52009-01-03 21:22:43 +0000511
512/**
513 * \brief Miller-Rabin primality test
514 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000515 * \param X MPI to check
516 * \param f_rng RNG function
517 * \param p_rng RNG parameter
518 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000519 * \return 0 if successful (probably prime),
520 * 1 if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000521 * POLARSSL_ERR_MPI_NOT_ACCEPTABLE if X is not prime
Paul Bakker5121ce52009-01-03 21:22:43 +0000522 */
523int mpi_is_prime( mpi *X, int (*f_rng)(void *), void *p_rng );
524
525/**
526 * \brief Prime number generation
527 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000528 * \param X Destination MPI
529 * \param nbits Required size of X in bits
530 * \param dh_flag If 1, then (X-1)/2 will be prime too
Paul Bakker5121ce52009-01-03 21:22:43 +0000531 * \param f_rng RNG function
532 * \param p_rng RNG parameter
533 *
534 * \return 0 if successful (probably prime),
535 * 1 if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000536 * POLARSSL_ERR_MPI_BAD_INPUT_DATA if nbits is < 3
Paul Bakker5121ce52009-01-03 21:22:43 +0000537 */
Paul Bakker23986e52011-04-24 08:57:21 +0000538int mpi_gen_prime( mpi *X, size_t nbits, int dh_flag,
Paul Bakker5121ce52009-01-03 21:22:43 +0000539 int (*f_rng)(void *), void *p_rng );
540
541/**
542 * \brief Checkup routine
543 *
544 * \return 0 if successful, or 1 if the test failed
545 */
546int mpi_self_test( int verbose );
547
548#ifdef __cplusplus
549}
550#endif
551
552#endif /* bignum.h */