blob: 7938406708a75dd3938bf1b6757160f1074cda83 [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 Bakker9d781402011-05-09 16:17:09 +000033#define POLARSSL_ERR_MPI_FILE_IO_ERROR -0x0002 /**< An error occurred while reading from or writing to a file. */
34#define POLARSSL_ERR_MPI_BAD_INPUT_DATA -0x0004 /**< Bad input parameters to function. */
35#define POLARSSL_ERR_MPI_INVALID_CHARACTER -0x0006 /**< There is an invalid character in the digit string. */
36#define POLARSSL_ERR_MPI_BUFFER_TOO_SMALL -0x0008 /**< The output buffer is too small to write too. */
37#define POLARSSL_ERR_MPI_NEGATIVE_VALUE -0x000A /**< The input arguments are negative or result in illegal output. */
38#define POLARSSL_ERR_MPI_DIVISION_BY_ZERO -0x000C /**< The input argument for division is zero, which is not allowed. */
39#define POLARSSL_ERR_MPI_NOT_ACCEPTABLE -0x000E /**< The input arguments are not acceptable. */
Paul Bakker5121ce52009-01-03 21:22:43 +000040
41#define MPI_CHK(f) if( ( ret = f ) != 0 ) goto cleanup
42
43/*
Paul Bakkerf9688572011-05-05 10:00:45 +000044 * Maximum size MPIs are allowed to grow to in number of limbs.
45 */
46#define POLARSSL_MPI_MAX_LIMBS 10000
47
48/*
Paul Bakker5121ce52009-01-03 21:22:43 +000049 * Define the base integer type, architecture-wise
50 */
Paul Bakker40e46942009-01-03 21:51:57 +000051#if defined(POLARSSL_HAVE_INT8)
Paul Bakkera755ca12011-04-24 09:11:17 +000052typedef signed char t_sint;
53typedef unsigned char t_uint;
54typedef unsigned short t_udbl;
Paul Bakker5121ce52009-01-03 21:22:43 +000055#else
Paul Bakker40e46942009-01-03 21:51:57 +000056#if defined(POLARSSL_HAVE_INT16)
Paul Bakkera755ca12011-04-24 09:11:17 +000057typedef signed short t_sint;
58typedef unsigned short t_uint;
59typedef unsigned long t_udbl;
Paul Bakker5121ce52009-01-03 21:22:43 +000060#else
Paul Bakkera755ca12011-04-24 09:11:17 +000061 typedef signed long t_sint;
62 typedef unsigned long t_uint;
Paul Bakker5121ce52009-01-03 21:22:43 +000063 #if defined(_MSC_VER) && defined(_M_IX86)
Paul Bakkera755ca12011-04-24 09:11:17 +000064 typedef unsigned __int64 t_udbl;
Paul Bakker5121ce52009-01-03 21:22:43 +000065 #else
66 #if defined(__amd64__) || defined(__x86_64__) || \
67 defined(__ppc64__) || defined(__powerpc64__) || \
68 defined(__ia64__) || defined(__alpha__)
Paul Bakkera755ca12011-04-24 09:11:17 +000069 typedef unsigned int t_udbl __attribute__((mode(TI)));
Paul Bakker5121ce52009-01-03 21:22:43 +000070 #else
Paul Bakker1a9382e2009-07-11 16:35:32 +000071 #if defined(POLARSSL_HAVE_LONGLONG)
Paul Bakkera755ca12011-04-24 09:11:17 +000072 typedef unsigned long long t_udbl;
Paul Bakker1a9382e2009-07-11 16:35:32 +000073 #endif
Paul Bakker5121ce52009-01-03 21:22:43 +000074 #endif
75 #endif
76#endif
77#endif
78
79/**
80 * \brief MPI structure
81 */
82typedef struct
83{
84 int s; /*!< integer sign */
Paul Bakker23986e52011-04-24 08:57:21 +000085 size_t n; /*!< total # of limbs */
Paul Bakkera755ca12011-04-24 09:11:17 +000086 t_uint *p; /*!< pointer to limbs */
Paul Bakker5121ce52009-01-03 21:22:43 +000087}
88mpi;
89
90#ifdef __cplusplus
91extern "C" {
92#endif
93
94/**
Paul Bakker6c591fa2011-05-05 11:49:20 +000095 * \brief Initialize one MPI
96 *
97 * \param X One MPI to initialize.
Paul Bakker5121ce52009-01-03 21:22:43 +000098 */
Paul Bakker6c591fa2011-05-05 11:49:20 +000099void mpi_init( mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000100
101/**
Paul Bakker6c591fa2011-05-05 11:49:20 +0000102 * \brief Unallocate one MPI
103 *
104 * \param X One MPI to unallocate.
Paul Bakker5121ce52009-01-03 21:22:43 +0000105 */
Paul Bakker6c591fa2011-05-05 11:49:20 +0000106void mpi_free( mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000107
108/**
109 * \brief Enlarge to the specified number of limbs
110 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000111 * \param X MPI to grow
112 * \param nblimbs The target number of limbs
113 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000114 * \return 0 if successful,
115 * 1 if memory allocation failed
116 */
Paul Bakker23986e52011-04-24 08:57:21 +0000117int mpi_grow( mpi *X, size_t nblimbs );
Paul Bakker5121ce52009-01-03 21:22:43 +0000118
119/**
120 * \brief Copy the contents of Y into X
121 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000122 * \param X Destination MPI
123 * \param Y Source MPI
124 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000125 * \return 0 if successful,
126 * 1 if memory allocation failed
127 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000128int mpi_copy( mpi *X, const mpi *Y );
Paul Bakker5121ce52009-01-03 21:22:43 +0000129
130/**
131 * \brief Swap the contents of X and Y
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000132 *
133 * \param X First MPI value
134 * \param Y Second MPI value
Paul Bakker5121ce52009-01-03 21:22:43 +0000135 */
136void mpi_swap( mpi *X, mpi *Y );
137
138/**
139 * \brief Set value from integer
140 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000141 * \param X MPI to set
142 * \param z Value to use
143 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000144 * \return 0 if successful,
145 * 1 if memory allocation failed
146 */
Paul Bakkera755ca12011-04-24 09:11:17 +0000147int mpi_lset( mpi *X, t_sint z );
Paul Bakker5121ce52009-01-03 21:22:43 +0000148
Paul Bakker2f5947e2011-05-18 15:47:11 +0000149/*
150 * \brief Get a specific bit from X
151 *
152 * \param X MPI to use
153 * \param pos Zero-based index of the bit in X
154 *
155 * \return Either a 0 or a 1
156 */
157int mpi_get_bit( mpi *X, size_t pos );
158
159/*
160 * \brief Set a bit of X to a specific value of 0 or 1
161 *
162 * \note Will grow X if necessary to set a bit to 1 in a not yet
163 * existing limb. Will not grow if bit should be set to 0
164 *
165 * \param X MPI to use
166 * \param pos Zero-based index of the bit in X
167 * \param val The value to set the bit to (0 or 1)
168 *
169 * \return 0 if successful,
170 * 1 if memory allocation failed,
171 * POLARSSL_ERR_MPI_BAD_INPUT_DATA if val is not 0 or 1
172 */
173int mpi_set_bit( mpi *X, size_t pos, unsigned char val );
174
Paul Bakker5121ce52009-01-03 21:22:43 +0000175/**
176 * \brief Return the number of least significant bits
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000177 *
178 * \param X MPI to use
Paul Bakker5121ce52009-01-03 21:22:43 +0000179 */
Paul Bakker23986e52011-04-24 08:57:21 +0000180size_t mpi_lsb( const mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000181
182/**
183 * \brief Return the number of most significant bits
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000184 *
185 * \param X MPI to use
Paul Bakker5121ce52009-01-03 21:22:43 +0000186 */
Paul Bakker23986e52011-04-24 08:57:21 +0000187size_t mpi_msb( const mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000188
189/**
190 * \brief Return the total size in bytes
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000191 *
192 * \param X MPI to use
Paul Bakker5121ce52009-01-03 21:22:43 +0000193 */
Paul Bakker23986e52011-04-24 08:57:21 +0000194size_t mpi_size( const mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000195
196/**
197 * \brief Import from an ASCII string
198 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000199 * \param X Destination MPI
200 * \param radix Input numeric base
201 * \param s Null-terminated string buffer
Paul Bakker5121ce52009-01-03 21:22:43 +0000202 *
Paul Bakker40e46942009-01-03 21:51:57 +0000203 * \return 0 if successful, or an POLARSSL_ERR_MPI_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000204 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000205int mpi_read_string( mpi *X, int radix, const char *s );
Paul Bakker5121ce52009-01-03 21:22:43 +0000206
207/**
208 * \brief Export into an ASCII string
209 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000210 * \param X Source MPI
211 * \param radix Output numeric base
212 * \param s String buffer
213 * \param slen String buffer size
Paul Bakker5121ce52009-01-03 21:22:43 +0000214 *
Paul Bakkerff60ee62010-03-16 21:09:09 +0000215 * \return 0 if successful, or an POLARSSL_ERR_MPI_XXX error code.
216 * *slen is always updated to reflect the amount
217 * of data that has (or would have) been written.
Paul Bakker5121ce52009-01-03 21:22:43 +0000218 *
219 * \note Call this function with *slen = 0 to obtain the
220 * minimum required buffer size in *slen.
221 */
Paul Bakker23986e52011-04-24 08:57:21 +0000222int mpi_write_string( const mpi *X, int radix, char *s, size_t *slen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000223
224/**
225 * \brief Read X from an opened file
226 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000227 * \param X Destination MPI
228 * \param radix Input numeric base
229 * \param fin Input file handle
Paul Bakker5121ce52009-01-03 21:22:43 +0000230 *
Paul Bakker40e46942009-01-03 21:51:57 +0000231 * \return 0 if successful, or an POLARSSL_ERR_MPI_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000232 */
233int mpi_read_file( mpi *X, int radix, FILE *fin );
234
235/**
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000236 * \brief Write X into an opened file, or stdout if fout is NULL
Paul Bakker5121ce52009-01-03 21:22:43 +0000237 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000238 * \param p Prefix, can be NULL
239 * \param X Source MPI
240 * \param radix Output numeric base
241 * \param fout Output file handle (can be NULL)
Paul Bakker5121ce52009-01-03 21:22:43 +0000242 *
Paul Bakker40e46942009-01-03 21:51:57 +0000243 * \return 0 if successful, or an POLARSSL_ERR_MPI_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000244 *
245 * \note Set fout == NULL to print X on the console.
246 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000247int mpi_write_file( const char *p, const mpi *X, int radix, FILE *fout );
Paul Bakker5121ce52009-01-03 21:22:43 +0000248
249/**
250 * \brief Import X from unsigned binary data, big endian
251 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000252 * \param X Destination MPI
253 * \param buf Input buffer
254 * \param buflen Input buffer size
Paul Bakker5121ce52009-01-03 21:22:43 +0000255 *
256 * \return 0 if successful,
257 * 1 if memory allocation failed
258 */
Paul Bakker23986e52011-04-24 08:57:21 +0000259int mpi_read_binary( mpi *X, const unsigned char *buf, size_t buflen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000260
261/**
262 * \brief Export X into unsigned binary data, big endian
263 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000264 * \param X Source MPI
265 * \param buf Output buffer
266 * \param buflen Output buffer size
Paul Bakker5121ce52009-01-03 21:22:43 +0000267 *
268 * \return 0 if successful,
Paul Bakker40e46942009-01-03 21:51:57 +0000269 * POLARSSL_ERR_MPI_BUFFER_TOO_SMALL if buf isn't large enough
Paul Bakker5121ce52009-01-03 21:22:43 +0000270 */
Paul Bakker23986e52011-04-24 08:57:21 +0000271int mpi_write_binary( const mpi *X, unsigned char *buf, size_t buflen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000272
273/**
274 * \brief Left-shift: X <<= count
275 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000276 * \param X MPI to shift
277 * \param count Amount to shift
278 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000279 * \return 0 if successful,
280 * 1 if memory allocation failed
281 */
Paul Bakker23986e52011-04-24 08:57:21 +0000282int mpi_shift_l( mpi *X, size_t count );
Paul Bakker5121ce52009-01-03 21:22:43 +0000283
284/**
285 * \brief Right-shift: X >>= count
286 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000287 * \param X MPI to shift
288 * \param count Amount to shift
289 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000290 * \return 0 if successful,
291 * 1 if memory allocation failed
292 */
Paul Bakker23986e52011-04-24 08:57:21 +0000293int mpi_shift_r( mpi *X, size_t count );
Paul Bakker5121ce52009-01-03 21:22:43 +0000294
295/**
296 * \brief Compare unsigned values
297 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000298 * \param X Left-hand MPI
299 * \param Y Right-hand MPI
300 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000301 * \return 1 if |X| is greater than |Y|,
302 * -1 if |X| is lesser than |Y| or
303 * 0 if |X| is equal to |Y|
304 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000305int mpi_cmp_abs( const mpi *X, const mpi *Y );
Paul Bakker5121ce52009-01-03 21:22:43 +0000306
307/**
308 * \brief Compare signed values
309 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000310 * \param X Left-hand MPI
311 * \param Y Right-hand MPI
312 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000313 * \return 1 if X is greater than Y,
314 * -1 if X is lesser than Y or
315 * 0 if X is equal to Y
316 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000317int mpi_cmp_mpi( const mpi *X, const mpi *Y );
Paul Bakker5121ce52009-01-03 21:22:43 +0000318
319/**
320 * \brief Compare signed values
321 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000322 * \param X Left-hand MPI
323 * \param z The integer value to compare to
324 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000325 * \return 1 if X is greater than z,
326 * -1 if X is lesser than z or
327 * 0 if X is equal to z
328 */
Paul Bakkera755ca12011-04-24 09:11:17 +0000329int mpi_cmp_int( const mpi *X, t_sint z );
Paul Bakker5121ce52009-01-03 21:22:43 +0000330
331/**
332 * \brief Unsigned addition: X = |A| + |B|
333 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000334 * \param X Destination MPI
335 * \param A Left-hand MPI
336 * \param B Right-hand MPI
337 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000338 * \return 0 if successful,
339 * 1 if memory allocation failed
340 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000341int mpi_add_abs( mpi *X, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000342
343/**
344 * \brief Unsigned substraction: X = |A| - |B|
345 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000346 * \param X Destination MPI
347 * \param A Left-hand MPI
348 * \param B Right-hand MPI
349 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000350 * \return 0 if successful,
Paul Bakker40e46942009-01-03 21:51:57 +0000351 * POLARSSL_ERR_MPI_NEGATIVE_VALUE if B is greater than A
Paul Bakker5121ce52009-01-03 21:22:43 +0000352 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000353int mpi_sub_abs( mpi *X, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000354
355/**
356 * \brief Signed addition: X = A + B
357 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000358 * \param X Destination MPI
359 * \param A Left-hand MPI
360 * \param B Right-hand MPI
361 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000362 * \return 0 if successful,
363 * 1 if memory allocation failed
364 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000365int mpi_add_mpi( mpi *X, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000366
367/**
368 * \brief Signed substraction: X = A - B
369 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000370 * \param X Destination MPI
371 * \param A Left-hand MPI
372 * \param B Right-hand MPI
373 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000374 * \return 0 if successful,
375 * 1 if memory allocation failed
376 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000377int mpi_sub_mpi( mpi *X, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000378
379/**
380 * \brief Signed addition: X = A + b
381 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000382 * \param X Destination MPI
383 * \param A Left-hand MPI
384 * \param b The integer value to add
385 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000386 * \return 0 if successful,
387 * 1 if memory allocation failed
388 */
Paul Bakkera755ca12011-04-24 09:11:17 +0000389int mpi_add_int( mpi *X, const mpi *A, t_sint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000390
391/**
392 * \brief Signed substraction: X = A - b
393 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000394 * \param X Destination MPI
395 * \param A Left-hand MPI
396 * \param b The integer value to subtract
397 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000398 * \return 0 if successful,
399 * 1 if memory allocation failed
400 */
Paul Bakkera755ca12011-04-24 09:11:17 +0000401int mpi_sub_int( mpi *X, const mpi *A, t_sint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000402
403/**
404 * \brief Baseline multiplication: X = A * B
405 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000406 * \param X Destination MPI
407 * \param A Left-hand MPI
408 * \param B Right-hand MPI
409 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000410 * \return 0 if successful,
411 * 1 if memory allocation failed
412 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000413int mpi_mul_mpi( mpi *X, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000414
415/**
416 * \brief Baseline multiplication: X = A * b
Paul Bakkerce40a6d2009-06-23 19:46:08 +0000417 * Note: b is an unsigned integer type, thus
418 * Negative values of b are ignored.
Paul Bakker5121ce52009-01-03 21:22:43 +0000419 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000420 * \param X Destination MPI
421 * \param A Left-hand MPI
422 * \param b The integer value to multiply with
423 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000424 * \return 0 if successful,
425 * 1 if memory allocation failed
426 */
Paul Bakkera755ca12011-04-24 09:11:17 +0000427int mpi_mul_int( mpi *X, const mpi *A, t_sint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000428
429/**
430 * \brief Division by mpi: A = Q * B + R
431 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000432 * \param Q Destination MPI for the quotient
433 * \param R Destination MPI for the rest value
434 * \param A Left-hand MPI
435 * \param B Right-hand MPI
436 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000437 * \return 0 if successful,
438 * 1 if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000439 * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if B == 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000440 *
441 * \note Either Q or R can be NULL.
442 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000443int mpi_div_mpi( mpi *Q, mpi *R, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000444
445/**
446 * \brief Division by int: A = Q * b + R
447 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000448 * \param Q Destination MPI for the quotient
449 * \param R Destination MPI for the rest value
450 * \param A Left-hand MPI
451 * \param b Integer to divide by
452 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000453 * \return 0 if successful,
454 * 1 if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000455 * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if b == 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000456 *
457 * \note Either Q or R can be NULL.
458 */
Paul Bakkera755ca12011-04-24 09:11:17 +0000459int mpi_div_int( mpi *Q, mpi *R, const mpi *A, t_sint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000460
461/**
462 * \brief Modulo: R = A mod B
463 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000464 * \param R Destination MPI for the rest value
465 * \param A Left-hand MPI
466 * \param B Right-hand MPI
467 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000468 * \return 0 if successful,
469 * 1 if memory allocation failed,
Paul Bakkerce40a6d2009-06-23 19:46:08 +0000470 * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if B == 0,
471 * POLARSSL_ERR_MPI_NEGATIVE_VALUE if B < 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000472 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000473int mpi_mod_mpi( mpi *R, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000474
475/**
476 * \brief Modulo: r = A mod b
477 *
Paul Bakkera755ca12011-04-24 09:11:17 +0000478 * \param r Destination t_uint
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000479 * \param A Left-hand MPI
480 * \param b Integer to divide by
481 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000482 * \return 0 if successful,
483 * 1 if memory allocation failed,
Paul Bakkerce40a6d2009-06-23 19:46:08 +0000484 * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if b == 0,
485 * POLARSSL_ERR_MPI_NEGATIVE_VALUE if b < 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000486 */
Paul Bakkera755ca12011-04-24 09:11:17 +0000487int mpi_mod_int( t_uint *r, const mpi *A, t_sint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000488
489/**
490 * \brief Sliding-window exponentiation: X = A^E mod N
491 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000492 * \param X Destination MPI
493 * \param A Left-hand MPI
494 * \param E Exponent MPI
495 * \param N Modular MPI
496 * \param _RR Speed-up MPI used for recalculations
497 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000498 * \return 0 if successful,
499 * 1 if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000500 * POLARSSL_ERR_MPI_BAD_INPUT_DATA if N is negative or even
Paul Bakker5121ce52009-01-03 21:22:43 +0000501 *
502 * \note _RR is used to avoid re-computing R*R mod N across
503 * multiple calls, which speeds up things a bit. It can
504 * be set to NULL if the extra performance is unneeded.
505 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000506int mpi_exp_mod( mpi *X, const mpi *A, const mpi *E, const mpi *N, mpi *_RR );
Paul Bakker5121ce52009-01-03 21:22:43 +0000507
508/**
Paul Bakker287781a2011-03-26 13:18:49 +0000509 * \brief Fill an MPI X with size bytes of random
510 *
511 * \param X Destination MPI
512 * \param size Size in bytes
513 * \param f_rng RNG function
514 * \param p_rng RNG parameter
515 *
516 * \return 0 if successful,
517 * 1 if memory allocation failed
518 */
Paul Bakker23986e52011-04-24 08:57:21 +0000519int mpi_fill_random( mpi *X, size_t size, int (*f_rng)(void *), void *p_rng );
Paul Bakker287781a2011-03-26 13:18:49 +0000520
521/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000522 * \brief Greatest common divisor: G = gcd(A, B)
523 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000524 * \param G Destination MPI
525 * \param A Left-hand MPI
526 * \param B Right-hand MPI
527 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000528 * \return 0 if successful,
529 * 1 if memory allocation failed
530 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000531int mpi_gcd( mpi *G, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000532
533/**
534 * \brief Modular inverse: X = A^-1 mod N
535 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000536 * \param X Destination MPI
537 * \param A Left-hand MPI
538 * \param N Right-hand MPI
539 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000540 * \return 0 if successful,
541 * 1 if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000542 * POLARSSL_ERR_MPI_BAD_INPUT_DATA if N is negative or nil
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000543 POLARSSL_ERR_MPI_NOT_ACCEPTABLE if A has no inverse mod N
Paul Bakker5121ce52009-01-03 21:22:43 +0000544 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000545int mpi_inv_mod( mpi *X, const mpi *A, const mpi *N );
Paul Bakker5121ce52009-01-03 21:22:43 +0000546
547/**
548 * \brief Miller-Rabin primality test
549 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000550 * \param X MPI to check
551 * \param f_rng RNG function
552 * \param p_rng RNG parameter
553 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000554 * \return 0 if successful (probably prime),
555 * 1 if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000556 * POLARSSL_ERR_MPI_NOT_ACCEPTABLE if X is not prime
Paul Bakker5121ce52009-01-03 21:22:43 +0000557 */
558int mpi_is_prime( mpi *X, int (*f_rng)(void *), void *p_rng );
559
560/**
561 * \brief Prime number generation
562 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000563 * \param X Destination MPI
Paul Bakkerf9688572011-05-05 10:00:45 +0000564 * \param nbits Required size of X in bits ( 3 <= nbits <= 4096 )
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000565 * \param dh_flag If 1, then (X-1)/2 will be prime too
Paul Bakker5121ce52009-01-03 21:22:43 +0000566 * \param f_rng RNG function
567 * \param p_rng RNG parameter
568 *
569 * \return 0 if successful (probably prime),
570 * 1 if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000571 * POLARSSL_ERR_MPI_BAD_INPUT_DATA if nbits is < 3
Paul Bakker5121ce52009-01-03 21:22:43 +0000572 */
Paul Bakker23986e52011-04-24 08:57:21 +0000573int mpi_gen_prime( mpi *X, size_t nbits, int dh_flag,
Paul Bakker5121ce52009-01-03 21:22:43 +0000574 int (*f_rng)(void *), void *p_rng );
575
576/**
577 * \brief Checkup routine
578 *
579 * \return 0 if successful, or 1 if the test failed
580 */
581int mpi_self_test( int verbose );
582
583#ifdef __cplusplus
584}
585#endif
586
587#endif /* bignum.h */