blob: 2b8e823144bc118c80fa5f5df815ab3624765b15 [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 Bakkerb6d5f082011-11-25 11:52:11 +000049 * Maximum window size used for modular exponentiation. Default: 6
50 * Minimum value: 1. Maximum value: 6.
51 *
52 * Result is an array of ( 2 << POLARSSL_MPI_WINDOW_SIZE ) MPIs used
53 * for the sliding window calculation. (So 64 by default)
54 *
55 * Reduction in size, reduces speed.
56 */
57#define POLARSSL_MPI_WINDOW_SIZE 6 /**< Maximum windows size used. */
58
59/*
Paul Bakkerfe3256e2011-11-25 12:11:43 +000060 * Maximum size of MPIs allowed in bits and bytes for user-MPIs.
61 * ( Default: 512 bytes => 4096 bits )
62 *
63 * Note: Calculations can results temporarily in larger MPIs. So the number
64 * of limbs required (POLARSSL_MPI_MAX_LIMBS) is higher.
65 */
66#define POLARSSL_MPI_MAX_SIZE 512 /**< Maximum number of bytes for usable MPIs. */
67#define POLARSSL_MPI_MAX_BITS ( 8 * POLARSSL_MPI_MAX_SIZE ) /**< Maximum number of bits for usable MPIs. */
68
69/*
Paul Bakker5121ce52009-01-03 21:22:43 +000070 * Define the base integer type, architecture-wise
71 */
Paul Bakker40e46942009-01-03 21:51:57 +000072#if defined(POLARSSL_HAVE_INT8)
Paul Bakkera755ca12011-04-24 09:11:17 +000073typedef signed char t_sint;
74typedef unsigned char t_uint;
75typedef unsigned short t_udbl;
Paul Bakker5121ce52009-01-03 21:22:43 +000076#else
Paul Bakker40e46942009-01-03 21:51:57 +000077#if defined(POLARSSL_HAVE_INT16)
Paul Bakkera755ca12011-04-24 09:11:17 +000078typedef signed short t_sint;
79typedef unsigned short t_uint;
80typedef unsigned long t_udbl;
Paul Bakker5121ce52009-01-03 21:22:43 +000081#else
Paul Bakkera755ca12011-04-24 09:11:17 +000082 typedef signed long t_sint;
83 typedef unsigned long t_uint;
Paul Bakker5121ce52009-01-03 21:22:43 +000084 #if defined(_MSC_VER) && defined(_M_IX86)
Paul Bakkera755ca12011-04-24 09:11:17 +000085 typedef unsigned __int64 t_udbl;
Paul Bakker5121ce52009-01-03 21:22:43 +000086 #else
87 #if defined(__amd64__) || defined(__x86_64__) || \
88 defined(__ppc64__) || defined(__powerpc64__) || \
89 defined(__ia64__) || defined(__alpha__)
Paul Bakkera755ca12011-04-24 09:11:17 +000090 typedef unsigned int t_udbl __attribute__((mode(TI)));
Paul Bakker5121ce52009-01-03 21:22:43 +000091 #else
Paul Bakker1a9382e2009-07-11 16:35:32 +000092 #if defined(POLARSSL_HAVE_LONGLONG)
Paul Bakkera755ca12011-04-24 09:11:17 +000093 typedef unsigned long long t_udbl;
Paul Bakker1a9382e2009-07-11 16:35:32 +000094 #endif
Paul Bakker5121ce52009-01-03 21:22:43 +000095 #endif
96 #endif
97#endif
98#endif
99
100/**
101 * \brief MPI structure
102 */
103typedef struct
104{
105 int s; /*!< integer sign */
Paul Bakker23986e52011-04-24 08:57:21 +0000106 size_t n; /*!< total # of limbs */
Paul Bakkera755ca12011-04-24 09:11:17 +0000107 t_uint *p; /*!< pointer to limbs */
Paul Bakker5121ce52009-01-03 21:22:43 +0000108}
109mpi;
110
111#ifdef __cplusplus
112extern "C" {
113#endif
114
115/**
Paul Bakker6c591fa2011-05-05 11:49:20 +0000116 * \brief Initialize one MPI
117 *
118 * \param X One MPI to initialize.
Paul Bakker5121ce52009-01-03 21:22:43 +0000119 */
Paul Bakker6c591fa2011-05-05 11:49:20 +0000120void mpi_init( mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000121
122/**
Paul Bakker6c591fa2011-05-05 11:49:20 +0000123 * \brief Unallocate one MPI
124 *
125 * \param X One MPI to unallocate.
Paul Bakker5121ce52009-01-03 21:22:43 +0000126 */
Paul Bakker6c591fa2011-05-05 11:49:20 +0000127void mpi_free( mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000128
129/**
130 * \brief Enlarge to the specified number of limbs
131 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000132 * \param X MPI to grow
133 * \param nblimbs The target number of limbs
134 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000135 * \return 0 if successful,
136 * 1 if memory allocation failed
137 */
Paul Bakker23986e52011-04-24 08:57:21 +0000138int mpi_grow( mpi *X, size_t nblimbs );
Paul Bakker5121ce52009-01-03 21:22:43 +0000139
140/**
141 * \brief Copy the contents of Y into X
142 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000143 * \param X Destination MPI
144 * \param Y Source MPI
145 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000146 * \return 0 if successful,
147 * 1 if memory allocation failed
148 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000149int mpi_copy( mpi *X, const mpi *Y );
Paul Bakker5121ce52009-01-03 21:22:43 +0000150
151/**
152 * \brief Swap the contents of X and Y
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000153 *
154 * \param X First MPI value
155 * \param Y Second MPI value
Paul Bakker5121ce52009-01-03 21:22:43 +0000156 */
157void mpi_swap( mpi *X, mpi *Y );
158
159/**
160 * \brief Set value from integer
161 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000162 * \param X MPI to set
163 * \param z Value to use
164 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000165 * \return 0 if successful,
166 * 1 if memory allocation failed
167 */
Paul Bakkera755ca12011-04-24 09:11:17 +0000168int mpi_lset( mpi *X, t_sint z );
Paul Bakker5121ce52009-01-03 21:22:43 +0000169
Paul Bakker2f5947e2011-05-18 15:47:11 +0000170/*
171 * \brief Get a specific bit from X
172 *
173 * \param X MPI to use
174 * \param pos Zero-based index of the bit in X
175 *
176 * \return Either a 0 or a 1
177 */
178int mpi_get_bit( mpi *X, size_t pos );
179
180/*
181 * \brief Set a bit of X to a specific value of 0 or 1
182 *
183 * \note Will grow X if necessary to set a bit to 1 in a not yet
184 * existing limb. Will not grow if bit should be set to 0
185 *
186 * \param X MPI to use
187 * \param pos Zero-based index of the bit in X
188 * \param val The value to set the bit to (0 or 1)
189 *
190 * \return 0 if successful,
191 * 1 if memory allocation failed,
192 * POLARSSL_ERR_MPI_BAD_INPUT_DATA if val is not 0 or 1
193 */
194int mpi_set_bit( mpi *X, size_t pos, unsigned char val );
195
Paul Bakker5121ce52009-01-03 21:22:43 +0000196/**
197 * \brief Return the number of least significant bits
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000198 *
199 * \param X MPI to use
Paul Bakker5121ce52009-01-03 21:22:43 +0000200 */
Paul Bakker23986e52011-04-24 08:57:21 +0000201size_t mpi_lsb( const mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000202
203/**
204 * \brief Return the number of most significant bits
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000205 *
206 * \param X MPI to use
Paul Bakker5121ce52009-01-03 21:22:43 +0000207 */
Paul Bakker23986e52011-04-24 08:57:21 +0000208size_t mpi_msb( const mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000209
210/**
211 * \brief Return the total size in bytes
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000212 *
213 * \param X MPI to use
Paul Bakker5121ce52009-01-03 21:22:43 +0000214 */
Paul Bakker23986e52011-04-24 08:57:21 +0000215size_t mpi_size( const mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000216
217/**
218 * \brief Import from an ASCII string
219 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000220 * \param X Destination MPI
221 * \param radix Input numeric base
222 * \param s Null-terminated string buffer
Paul Bakker5121ce52009-01-03 21:22:43 +0000223 *
Paul Bakker40e46942009-01-03 21:51:57 +0000224 * \return 0 if successful, or an POLARSSL_ERR_MPI_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000225 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000226int mpi_read_string( mpi *X, int radix, const char *s );
Paul Bakker5121ce52009-01-03 21:22:43 +0000227
228/**
229 * \brief Export into an ASCII string
230 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000231 * \param X Source MPI
232 * \param radix Output numeric base
233 * \param s String buffer
234 * \param slen String buffer size
Paul Bakker5121ce52009-01-03 21:22:43 +0000235 *
Paul Bakkerff60ee62010-03-16 21:09:09 +0000236 * \return 0 if successful, or an POLARSSL_ERR_MPI_XXX error code.
237 * *slen is always updated to reflect the amount
238 * of data that has (or would have) been written.
Paul Bakker5121ce52009-01-03 21:22:43 +0000239 *
240 * \note Call this function with *slen = 0 to obtain the
241 * minimum required buffer size in *slen.
242 */
Paul Bakker23986e52011-04-24 08:57:21 +0000243int mpi_write_string( const mpi *X, int radix, char *s, size_t *slen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000244
245/**
246 * \brief Read X from an opened file
247 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000248 * \param X Destination MPI
249 * \param radix Input numeric base
250 * \param fin Input file handle
Paul Bakker5121ce52009-01-03 21:22:43 +0000251 *
Paul Bakker40e46942009-01-03 21:51:57 +0000252 * \return 0 if successful, or an POLARSSL_ERR_MPI_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000253 */
254int mpi_read_file( mpi *X, int radix, FILE *fin );
255
256/**
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000257 * \brief Write X into an opened file, or stdout if fout is NULL
Paul Bakker5121ce52009-01-03 21:22:43 +0000258 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000259 * \param p Prefix, can be NULL
260 * \param X Source MPI
261 * \param radix Output numeric base
262 * \param fout Output file handle (can be NULL)
Paul Bakker5121ce52009-01-03 21:22:43 +0000263 *
Paul Bakker40e46942009-01-03 21:51:57 +0000264 * \return 0 if successful, or an POLARSSL_ERR_MPI_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000265 *
266 * \note Set fout == NULL to print X on the console.
267 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000268int mpi_write_file( const char *p, const mpi *X, int radix, FILE *fout );
Paul Bakker5121ce52009-01-03 21:22:43 +0000269
270/**
271 * \brief Import X from unsigned binary data, big endian
272 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000273 * \param X Destination MPI
274 * \param buf Input buffer
275 * \param buflen Input buffer size
Paul Bakker5121ce52009-01-03 21:22:43 +0000276 *
277 * \return 0 if successful,
278 * 1 if memory allocation failed
279 */
Paul Bakker23986e52011-04-24 08:57:21 +0000280int mpi_read_binary( mpi *X, const unsigned char *buf, size_t buflen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000281
282/**
283 * \brief Export X into unsigned binary data, big endian
284 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000285 * \param X Source MPI
286 * \param buf Output buffer
287 * \param buflen Output buffer size
Paul Bakker5121ce52009-01-03 21:22:43 +0000288 *
289 * \return 0 if successful,
Paul Bakker40e46942009-01-03 21:51:57 +0000290 * POLARSSL_ERR_MPI_BUFFER_TOO_SMALL if buf isn't large enough
Paul Bakker5121ce52009-01-03 21:22:43 +0000291 */
Paul Bakker23986e52011-04-24 08:57:21 +0000292int mpi_write_binary( const mpi *X, unsigned char *buf, size_t buflen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000293
294/**
295 * \brief Left-shift: X <<= count
296 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000297 * \param X MPI to shift
298 * \param count Amount to shift
299 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000300 * \return 0 if successful,
301 * 1 if memory allocation failed
302 */
Paul Bakker23986e52011-04-24 08:57:21 +0000303int mpi_shift_l( mpi *X, size_t count );
Paul Bakker5121ce52009-01-03 21:22:43 +0000304
305/**
306 * \brief Right-shift: X >>= count
307 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000308 * \param X MPI to shift
309 * \param count Amount to shift
310 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000311 * \return 0 if successful,
312 * 1 if memory allocation failed
313 */
Paul Bakker23986e52011-04-24 08:57:21 +0000314int mpi_shift_r( mpi *X, size_t count );
Paul Bakker5121ce52009-01-03 21:22:43 +0000315
316/**
317 * \brief Compare unsigned values
318 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000319 * \param X Left-hand MPI
320 * \param Y Right-hand MPI
321 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000322 * \return 1 if |X| is greater than |Y|,
323 * -1 if |X| is lesser than |Y| or
324 * 0 if |X| is equal to |Y|
325 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000326int mpi_cmp_abs( const mpi *X, const mpi *Y );
Paul Bakker5121ce52009-01-03 21:22:43 +0000327
328/**
329 * \brief Compare signed values
330 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000331 * \param X Left-hand MPI
332 * \param Y Right-hand MPI
333 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000334 * \return 1 if X is greater than Y,
335 * -1 if X is lesser than Y or
336 * 0 if X is equal to Y
337 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000338int mpi_cmp_mpi( const mpi *X, const mpi *Y );
Paul Bakker5121ce52009-01-03 21:22:43 +0000339
340/**
341 * \brief Compare signed values
342 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000343 * \param X Left-hand MPI
344 * \param z The integer value to compare to
345 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000346 * \return 1 if X is greater than z,
347 * -1 if X is lesser than z or
348 * 0 if X is equal to z
349 */
Paul Bakkera755ca12011-04-24 09:11:17 +0000350int mpi_cmp_int( const mpi *X, t_sint z );
Paul Bakker5121ce52009-01-03 21:22:43 +0000351
352/**
353 * \brief Unsigned addition: X = |A| + |B|
354 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000355 * \param X Destination MPI
356 * \param A Left-hand MPI
357 * \param B Right-hand MPI
358 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000359 * \return 0 if successful,
360 * 1 if memory allocation failed
361 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000362int mpi_add_abs( mpi *X, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000363
364/**
365 * \brief Unsigned substraction: X = |A| - |B|
366 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000367 * \param X Destination MPI
368 * \param A Left-hand MPI
369 * \param B Right-hand MPI
370 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000371 * \return 0 if successful,
Paul Bakker40e46942009-01-03 21:51:57 +0000372 * POLARSSL_ERR_MPI_NEGATIVE_VALUE if B is greater than A
Paul Bakker5121ce52009-01-03 21:22:43 +0000373 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000374int mpi_sub_abs( mpi *X, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000375
376/**
377 * \brief Signed addition: X = A + B
378 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000379 * \param X Destination MPI
380 * \param A Left-hand MPI
381 * \param B Right-hand MPI
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_add_mpi( mpi *X, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000387
388/**
389 * \brief Signed substraction: X = A - B
390 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000391 * \param X Destination MPI
392 * \param A Left-hand MPI
393 * \param B Right-hand MPI
394 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000395 * \return 0 if successful,
396 * 1 if memory allocation failed
397 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000398int mpi_sub_mpi( mpi *X, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000399
400/**
401 * \brief Signed addition: X = A + b
402 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000403 * \param X Destination MPI
404 * \param A Left-hand MPI
405 * \param b The integer value to add
406 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000407 * \return 0 if successful,
408 * 1 if memory allocation failed
409 */
Paul Bakkera755ca12011-04-24 09:11:17 +0000410int mpi_add_int( mpi *X, const mpi *A, t_sint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000411
412/**
413 * \brief Signed substraction: X = A - b
414 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000415 * \param X Destination MPI
416 * \param A Left-hand MPI
417 * \param b The integer value to subtract
418 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000419 * \return 0 if successful,
420 * 1 if memory allocation failed
421 */
Paul Bakkera755ca12011-04-24 09:11:17 +0000422int mpi_sub_int( mpi *X, const mpi *A, t_sint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000423
424/**
425 * \brief Baseline multiplication: X = A * B
426 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000427 * \param X Destination MPI
428 * \param A Left-hand MPI
429 * \param B Right-hand MPI
430 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000431 * \return 0 if successful,
432 * 1 if memory allocation failed
433 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000434int mpi_mul_mpi( mpi *X, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000435
436/**
437 * \brief Baseline multiplication: X = A * b
Paul Bakkerce40a6d2009-06-23 19:46:08 +0000438 * Note: b is an unsigned integer type, thus
439 * Negative values of b are ignored.
Paul Bakker5121ce52009-01-03 21:22:43 +0000440 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000441 * \param X Destination MPI
442 * \param A Left-hand MPI
443 * \param b The integer value to multiply with
444 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000445 * \return 0 if successful,
446 * 1 if memory allocation failed
447 */
Paul Bakkera755ca12011-04-24 09:11:17 +0000448int mpi_mul_int( mpi *X, const mpi *A, t_sint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000449
450/**
451 * \brief Division by mpi: A = Q * B + R
452 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000453 * \param Q Destination MPI for the quotient
454 * \param R Destination MPI for the rest value
455 * \param A Left-hand MPI
456 * \param B Right-hand MPI
457 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000458 * \return 0 if successful,
459 * 1 if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000460 * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if B == 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000461 *
462 * \note Either Q or R can be NULL.
463 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000464int mpi_div_mpi( mpi *Q, mpi *R, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000465
466/**
467 * \brief Division by int: A = Q * b + R
468 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000469 * \param Q Destination MPI for the quotient
470 * \param R Destination MPI for the rest value
471 * \param A Left-hand MPI
472 * \param b Integer to divide by
473 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000474 * \return 0 if successful,
475 * 1 if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000476 * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if b == 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000477 *
478 * \note Either Q or R can be NULL.
479 */
Paul Bakkera755ca12011-04-24 09:11:17 +0000480int mpi_div_int( mpi *Q, mpi *R, const mpi *A, t_sint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000481
482/**
483 * \brief Modulo: R = A mod B
484 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000485 * \param R Destination MPI for the rest value
486 * \param A Left-hand MPI
487 * \param B Right-hand MPI
488 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000489 * \return 0 if successful,
490 * 1 if memory allocation failed,
Paul Bakkerce40a6d2009-06-23 19:46:08 +0000491 * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if B == 0,
492 * POLARSSL_ERR_MPI_NEGATIVE_VALUE if B < 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000493 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000494int mpi_mod_mpi( mpi *R, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000495
496/**
497 * \brief Modulo: r = A mod b
498 *
Paul Bakkera755ca12011-04-24 09:11:17 +0000499 * \param r Destination t_uint
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000500 * \param A Left-hand MPI
501 * \param b Integer to divide by
502 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000503 * \return 0 if successful,
504 * 1 if memory allocation failed,
Paul Bakkerce40a6d2009-06-23 19:46:08 +0000505 * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if b == 0,
506 * POLARSSL_ERR_MPI_NEGATIVE_VALUE if b < 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000507 */
Paul Bakkera755ca12011-04-24 09:11:17 +0000508int mpi_mod_int( t_uint *r, const mpi *A, t_sint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000509
510/**
511 * \brief Sliding-window exponentiation: X = A^E mod N
512 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000513 * \param X Destination MPI
514 * \param A Left-hand MPI
515 * \param E Exponent MPI
516 * \param N Modular MPI
517 * \param _RR Speed-up MPI used for recalculations
518 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000519 * \return 0 if successful,
520 * 1 if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000521 * POLARSSL_ERR_MPI_BAD_INPUT_DATA if N is negative or even
Paul Bakker5121ce52009-01-03 21:22:43 +0000522 *
523 * \note _RR is used to avoid re-computing R*R mod N across
524 * multiple calls, which speeds up things a bit. It can
525 * be set to NULL if the extra performance is unneeded.
526 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000527int mpi_exp_mod( mpi *X, const mpi *A, const mpi *E, const mpi *N, mpi *_RR );
Paul Bakker5121ce52009-01-03 21:22:43 +0000528
529/**
Paul Bakker287781a2011-03-26 13:18:49 +0000530 * \brief Fill an MPI X with size bytes of random
531 *
532 * \param X Destination MPI
533 * \param size Size in bytes
534 * \param f_rng RNG function
535 * \param p_rng RNG parameter
536 *
537 * \return 0 if successful,
538 * 1 if memory allocation failed
539 */
Paul Bakker23986e52011-04-24 08:57:21 +0000540int mpi_fill_random( mpi *X, size_t size, int (*f_rng)(void *), void *p_rng );
Paul Bakker287781a2011-03-26 13:18:49 +0000541
542/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000543 * \brief Greatest common divisor: G = gcd(A, B)
544 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000545 * \param G Destination MPI
546 * \param A Left-hand MPI
547 * \param B Right-hand MPI
548 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000549 * \return 0 if successful,
550 * 1 if memory allocation failed
551 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000552int mpi_gcd( mpi *G, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000553
554/**
555 * \brief Modular inverse: X = A^-1 mod N
556 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000557 * \param X Destination MPI
558 * \param A Left-hand MPI
559 * \param N Right-hand MPI
560 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000561 * \return 0 if successful,
562 * 1 if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000563 * POLARSSL_ERR_MPI_BAD_INPUT_DATA if N is negative or nil
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000564 POLARSSL_ERR_MPI_NOT_ACCEPTABLE if A has no inverse mod N
Paul Bakker5121ce52009-01-03 21:22:43 +0000565 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000566int mpi_inv_mod( mpi *X, const mpi *A, const mpi *N );
Paul Bakker5121ce52009-01-03 21:22:43 +0000567
568/**
569 * \brief Miller-Rabin primality test
570 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000571 * \param X MPI to check
572 * \param f_rng RNG function
573 * \param p_rng RNG parameter
574 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000575 * \return 0 if successful (probably prime),
576 * 1 if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000577 * POLARSSL_ERR_MPI_NOT_ACCEPTABLE if X is not prime
Paul Bakker5121ce52009-01-03 21:22:43 +0000578 */
579int mpi_is_prime( mpi *X, int (*f_rng)(void *), void *p_rng );
580
581/**
582 * \brief Prime number generation
583 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000584 * \param X Destination MPI
Paul Bakkerfe3256e2011-11-25 12:11:43 +0000585 * \param nbits Required size of X in bits ( 3 <= nbits <= POLARSSL_MPI_MAX_BITS )
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000586 * \param dh_flag If 1, then (X-1)/2 will be prime too
Paul Bakker5121ce52009-01-03 21:22:43 +0000587 * \param f_rng RNG function
588 * \param p_rng RNG parameter
589 *
590 * \return 0 if successful (probably prime),
591 * 1 if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000592 * POLARSSL_ERR_MPI_BAD_INPUT_DATA if nbits is < 3
Paul Bakker5121ce52009-01-03 21:22:43 +0000593 */
Paul Bakker23986e52011-04-24 08:57:21 +0000594int mpi_gen_prime( mpi *X, size_t nbits, int dh_flag,
Paul Bakker5121ce52009-01-03 21:22:43 +0000595 int (*f_rng)(void *), void *p_rng );
596
597/**
598 * \brief Checkup routine
599 *
600 * \return 0 if successful, or 1 if the test failed
601 */
602int mpi_self_test( int verbose );
603
604#ifdef __cplusplus
605}
606#endif
607
608#endif /* bignum.h */