blob: 898e37daf77b7cc7d1de6b2912f8133ba3ee9a28 [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 Bakkercf0360a2012-01-20 10:08:14 +000033#include "config.h"
34
Paul Bakker9d781402011-05-09 16:17:09 +000035#define POLARSSL_ERR_MPI_FILE_IO_ERROR -0x0002 /**< An error occurred while reading from or writing to a file. */
36#define POLARSSL_ERR_MPI_BAD_INPUT_DATA -0x0004 /**< Bad input parameters to function. */
37#define POLARSSL_ERR_MPI_INVALID_CHARACTER -0x0006 /**< There is an invalid character in the digit string. */
Paul Bakker69e095c2011-12-10 21:55:01 +000038#define POLARSSL_ERR_MPI_BUFFER_TOO_SMALL -0x0008 /**< The buffer is too small to write to. */
Paul Bakker9d781402011-05-09 16:17:09 +000039#define POLARSSL_ERR_MPI_NEGATIVE_VALUE -0x000A /**< The input arguments are negative or result in illegal output. */
40#define POLARSSL_ERR_MPI_DIVISION_BY_ZERO -0x000C /**< The input argument for division is zero, which is not allowed. */
41#define POLARSSL_ERR_MPI_NOT_ACCEPTABLE -0x000E /**< The input arguments are not acceptable. */
Paul Bakker69e095c2011-12-10 21:55:01 +000042#define POLARSSL_ERR_MPI_MALLOC_FAILED -0x0010 /**< Memory allocation failed. */
Paul Bakker5121ce52009-01-03 21:22:43 +000043
44#define MPI_CHK(f) if( ( ret = f ) != 0 ) goto cleanup
45
46/*
Paul Bakkerf9688572011-05-05 10:00:45 +000047 * Maximum size MPIs are allowed to grow to in number of limbs.
48 */
49#define POLARSSL_MPI_MAX_LIMBS 10000
50
51/*
Paul Bakkerb6d5f082011-11-25 11:52:11 +000052 * Maximum window size used for modular exponentiation. Default: 6
53 * Minimum value: 1. Maximum value: 6.
54 *
55 * Result is an array of ( 2 << POLARSSL_MPI_WINDOW_SIZE ) MPIs used
56 * for the sliding window calculation. (So 64 by default)
57 *
58 * Reduction in size, reduces speed.
59 */
60#define POLARSSL_MPI_WINDOW_SIZE 6 /**< Maximum windows size used. */
61
62/*
Paul Bakkerfe3256e2011-11-25 12:11:43 +000063 * Maximum size of MPIs allowed in bits and bytes for user-MPIs.
64 * ( Default: 512 bytes => 4096 bits )
65 *
66 * Note: Calculations can results temporarily in larger MPIs. So the number
67 * of limbs required (POLARSSL_MPI_MAX_LIMBS) is higher.
68 */
69#define POLARSSL_MPI_MAX_SIZE 512 /**< Maximum number of bytes for usable MPIs. */
70#define POLARSSL_MPI_MAX_BITS ( 8 * POLARSSL_MPI_MAX_SIZE ) /**< Maximum number of bits for usable MPIs. */
71
72/*
Paul Bakkercb37aa52011-11-30 16:00:20 +000073 * When reading from files with mpi_read_file() the buffer should have space
74 * for a (short) label, the MPI (in the provided radix), the newline
75 * characters and the '\0'.
76 *
77 * By default we assume at least a 10 char label, a minimum radix of 10
78 * (decimal) and a maximum of 4096 bit numbers (1234 decimal chars).
79 */
80#define POLARSSL_MPI_READ_BUFFER_SIZE 1250
81
82/*
Paul Bakker5121ce52009-01-03 21:22:43 +000083 * Define the base integer type, architecture-wise
84 */
Paul Bakker40e46942009-01-03 21:51:57 +000085#if defined(POLARSSL_HAVE_INT8)
Paul Bakkera755ca12011-04-24 09:11:17 +000086typedef signed char t_sint;
87typedef unsigned char t_uint;
88typedef unsigned short t_udbl;
Paul Bakker5121ce52009-01-03 21:22:43 +000089#else
Paul Bakker40e46942009-01-03 21:51:57 +000090#if defined(POLARSSL_HAVE_INT16)
Paul Bakkera755ca12011-04-24 09:11:17 +000091typedef signed short t_sint;
92typedef unsigned short t_uint;
93typedef unsigned long t_udbl;
Paul Bakker5121ce52009-01-03 21:22:43 +000094#else
Paul Bakkera755ca12011-04-24 09:11:17 +000095 typedef signed long t_sint;
96 typedef unsigned long t_uint;
Paul Bakker5121ce52009-01-03 21:22:43 +000097 #if defined(_MSC_VER) && defined(_M_IX86)
Paul Bakkera755ca12011-04-24 09:11:17 +000098 typedef unsigned __int64 t_udbl;
Paul Bakker5121ce52009-01-03 21:22:43 +000099 #else
Paul Bakkercf0360a2012-01-20 10:08:14 +0000100 #if defined(__GNUC__) && ( \
101 defined(__amd64__) || defined(__x86_64__) || \
Paul Bakker5121ce52009-01-03 21:22:43 +0000102 defined(__ppc64__) || defined(__powerpc64__) || \
Paul Bakker44637402011-11-26 09:23:07 +0000103 defined(__ia64__) || defined(__alpha__) || \
104 (defined(__sparc__) && defined(__arch64__)) || \
Paul Bakkercf0360a2012-01-20 10:08:14 +0000105 defined(__s390x__) )
Paul Bakkera755ca12011-04-24 09:11:17 +0000106 typedef unsigned int t_udbl __attribute__((mode(TI)));
Paul Bakker66219872012-01-22 20:38:13 +0000107 #define POLARSSL_HAVE_LONGLONG
Paul Bakker5121ce52009-01-03 21:22:43 +0000108 #else
Paul Bakker1a9382e2009-07-11 16:35:32 +0000109 #if defined(POLARSSL_HAVE_LONGLONG)
Paul Bakkera755ca12011-04-24 09:11:17 +0000110 typedef unsigned long long t_udbl;
Paul Bakker1a9382e2009-07-11 16:35:32 +0000111 #endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000112 #endif
113 #endif
114#endif
115#endif
116
117/**
118 * \brief MPI structure
119 */
120typedef struct
121{
122 int s; /*!< integer sign */
Paul Bakker23986e52011-04-24 08:57:21 +0000123 size_t n; /*!< total # of limbs */
Paul Bakkera755ca12011-04-24 09:11:17 +0000124 t_uint *p; /*!< pointer to limbs */
Paul Bakker5121ce52009-01-03 21:22:43 +0000125}
126mpi;
127
128#ifdef __cplusplus
129extern "C" {
130#endif
131
132/**
Paul Bakker6c591fa2011-05-05 11:49:20 +0000133 * \brief Initialize one MPI
134 *
135 * \param X One MPI to initialize.
Paul Bakker5121ce52009-01-03 21:22:43 +0000136 */
Paul Bakker6c591fa2011-05-05 11:49:20 +0000137void mpi_init( mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000138
139/**
Paul Bakker6c591fa2011-05-05 11:49:20 +0000140 * \brief Unallocate one MPI
141 *
142 * \param X One MPI to unallocate.
Paul Bakker5121ce52009-01-03 21:22:43 +0000143 */
Paul Bakker6c591fa2011-05-05 11:49:20 +0000144void mpi_free( mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000145
146/**
147 * \brief Enlarge to the specified number of limbs
148 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000149 * \param X MPI to grow
150 * \param nblimbs The target number of limbs
151 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000152 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000153 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000154 */
Paul Bakker23986e52011-04-24 08:57:21 +0000155int mpi_grow( mpi *X, size_t nblimbs );
Paul Bakker5121ce52009-01-03 21:22:43 +0000156
157/**
158 * \brief Copy the contents of Y into X
159 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000160 * \param X Destination MPI
161 * \param Y Source MPI
162 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000163 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000164 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000165 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000166int mpi_copy( mpi *X, const mpi *Y );
Paul Bakker5121ce52009-01-03 21:22:43 +0000167
168/**
169 * \brief Swap the contents of X and Y
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000170 *
171 * \param X First MPI value
172 * \param Y Second MPI value
Paul Bakker5121ce52009-01-03 21:22:43 +0000173 */
174void mpi_swap( mpi *X, mpi *Y );
175
176/**
177 * \brief Set value from integer
178 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000179 * \param X MPI to set
180 * \param z Value to use
181 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000182 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000183 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000184 */
Paul Bakkera755ca12011-04-24 09:11:17 +0000185int mpi_lset( mpi *X, t_sint z );
Paul Bakker5121ce52009-01-03 21:22:43 +0000186
Paul Bakker2f5947e2011-05-18 15:47:11 +0000187/*
188 * \brief Get a specific bit from X
189 *
190 * \param X MPI to use
191 * \param pos Zero-based index of the bit in X
192 *
193 * \return Either a 0 or a 1
194 */
Paul Bakker6b906e52012-05-08 12:01:43 +0000195int mpi_get_bit( const mpi *X, size_t pos );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000196
197/*
198 * \brief Set a bit of X to a specific value of 0 or 1
199 *
200 * \note Will grow X if necessary to set a bit to 1 in a not yet
201 * existing limb. Will not grow if bit should be set to 0
202 *
203 * \param X MPI to use
204 * \param pos Zero-based index of the bit in X
205 * \param val The value to set the bit to (0 or 1)
206 *
207 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000208 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
Paul Bakker2f5947e2011-05-18 15:47:11 +0000209 * POLARSSL_ERR_MPI_BAD_INPUT_DATA if val is not 0 or 1
210 */
211int mpi_set_bit( mpi *X, size_t pos, unsigned char val );
212
Paul Bakker5121ce52009-01-03 21:22:43 +0000213/**
Paul Bakker6b906e52012-05-08 12:01:43 +0000214 * \brief Return the number of zero-bits before the least significant
215 * '1' bit
216 *
217 * Note: Thus also the zero-based index of the least significant '1' bit
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000218 *
219 * \param X MPI to use
Paul Bakker5121ce52009-01-03 21:22:43 +0000220 */
Paul Bakker23986e52011-04-24 08:57:21 +0000221size_t mpi_lsb( const mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000222
223/**
Paul Bakker6b906e52012-05-08 12:01:43 +0000224 * \brief Return the number of bits up to and including the most
225 * significant '1' bit'
226 *
227 * Note: Thus also the one-based index of the most significant '1' bit
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000228 *
229 * \param X MPI to use
Paul Bakker5121ce52009-01-03 21:22:43 +0000230 */
Paul Bakker23986e52011-04-24 08:57:21 +0000231size_t mpi_msb( const mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000232
233/**
234 * \brief Return the total size in bytes
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000235 *
236 * \param X MPI to use
Paul Bakker5121ce52009-01-03 21:22:43 +0000237 */
Paul Bakker23986e52011-04-24 08:57:21 +0000238size_t mpi_size( const mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000239
240/**
241 * \brief Import from an ASCII string
242 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000243 * \param X Destination MPI
244 * \param radix Input numeric base
245 * \param s Null-terminated string buffer
Paul Bakker5121ce52009-01-03 21:22:43 +0000246 *
Paul Bakkercb37aa52011-11-30 16:00:20 +0000247 * \return 0 if successful, or a POLARSSL_ERR_MPI_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000248 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000249int mpi_read_string( mpi *X, int radix, const char *s );
Paul Bakker5121ce52009-01-03 21:22:43 +0000250
251/**
252 * \brief Export into an ASCII string
253 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000254 * \param X Source MPI
255 * \param radix Output numeric base
256 * \param s String buffer
257 * \param slen String buffer size
Paul Bakker5121ce52009-01-03 21:22:43 +0000258 *
Paul Bakkercb37aa52011-11-30 16:00:20 +0000259 * \return 0 if successful, or a POLARSSL_ERR_MPI_XXX error code.
Paul Bakkerff60ee62010-03-16 21:09:09 +0000260 * *slen is always updated to reflect the amount
261 * of data that has (or would have) been written.
Paul Bakker5121ce52009-01-03 21:22:43 +0000262 *
263 * \note Call this function with *slen = 0 to obtain the
264 * minimum required buffer size in *slen.
265 */
Paul Bakker23986e52011-04-24 08:57:21 +0000266int mpi_write_string( const mpi *X, int radix, char *s, size_t *slen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000267
268/**
269 * \brief Read X from an opened file
270 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000271 * \param X Destination MPI
272 * \param radix Input numeric base
273 * \param fin Input file handle
Paul Bakker5121ce52009-01-03 21:22:43 +0000274 *
Paul Bakkercb37aa52011-11-30 16:00:20 +0000275 * \return 0 if successful, POLARSSL_ERR_MPI_BUFFER_TOO_SMALL if
276 * the file read buffer is too small or a
277 * POLARSSL_ERR_MPI_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000278 */
279int mpi_read_file( mpi *X, int radix, FILE *fin );
280
281/**
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000282 * \brief Write X into an opened file, or stdout if fout is NULL
Paul Bakker5121ce52009-01-03 21:22:43 +0000283 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000284 * \param p Prefix, can be NULL
285 * \param X Source MPI
286 * \param radix Output numeric base
287 * \param fout Output file handle (can be NULL)
Paul Bakker5121ce52009-01-03 21:22:43 +0000288 *
Paul Bakkercb37aa52011-11-30 16:00:20 +0000289 * \return 0 if successful, or a POLARSSL_ERR_MPI_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000290 *
291 * \note Set fout == NULL to print X on the console.
292 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000293int mpi_write_file( const char *p, const mpi *X, int radix, FILE *fout );
Paul Bakker5121ce52009-01-03 21:22:43 +0000294
295/**
296 * \brief Import X from unsigned binary data, big endian
297 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000298 * \param X Destination MPI
299 * \param buf Input buffer
300 * \param buflen Input buffer size
Paul Bakker5121ce52009-01-03 21:22:43 +0000301 *
302 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000303 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000304 */
Paul Bakker23986e52011-04-24 08:57:21 +0000305int mpi_read_binary( mpi *X, const unsigned char *buf, size_t buflen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000306
307/**
308 * \brief Export X into unsigned binary data, big endian
309 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000310 * \param X Source MPI
311 * \param buf Output buffer
312 * \param buflen Output buffer size
Paul Bakker5121ce52009-01-03 21:22:43 +0000313 *
314 * \return 0 if successful,
Paul Bakker40e46942009-01-03 21:51:57 +0000315 * POLARSSL_ERR_MPI_BUFFER_TOO_SMALL if buf isn't large enough
Paul Bakker5121ce52009-01-03 21:22:43 +0000316 */
Paul Bakker23986e52011-04-24 08:57:21 +0000317int mpi_write_binary( const mpi *X, unsigned char *buf, size_t buflen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000318
319/**
320 * \brief Left-shift: X <<= count
321 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000322 * \param X MPI to shift
323 * \param count Amount to shift
324 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000325 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000326 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000327 */
Paul Bakker23986e52011-04-24 08:57:21 +0000328int mpi_shift_l( mpi *X, size_t count );
Paul Bakker5121ce52009-01-03 21:22:43 +0000329
330/**
331 * \brief Right-shift: X >>= count
332 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000333 * \param X MPI to shift
334 * \param count Amount to shift
335 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000336 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000337 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000338 */
Paul Bakker23986e52011-04-24 08:57:21 +0000339int mpi_shift_r( mpi *X, size_t count );
Paul Bakker5121ce52009-01-03 21:22:43 +0000340
341/**
342 * \brief Compare unsigned values
343 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000344 * \param X Left-hand MPI
345 * \param Y Right-hand MPI
346 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000347 * \return 1 if |X| is greater than |Y|,
348 * -1 if |X| is lesser than |Y| or
349 * 0 if |X| is equal to |Y|
350 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000351int mpi_cmp_abs( const mpi *X, const mpi *Y );
Paul Bakker5121ce52009-01-03 21:22:43 +0000352
353/**
354 * \brief Compare signed values
355 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000356 * \param X Left-hand MPI
357 * \param Y Right-hand MPI
358 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000359 * \return 1 if X is greater than Y,
360 * -1 if X is lesser than Y or
361 * 0 if X is equal to Y
362 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000363int mpi_cmp_mpi( const mpi *X, const mpi *Y );
Paul Bakker5121ce52009-01-03 21:22:43 +0000364
365/**
366 * \brief Compare signed values
367 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000368 * \param X Left-hand MPI
369 * \param z The integer value to compare to
370 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000371 * \return 1 if X is greater than z,
372 * -1 if X is lesser than z or
373 * 0 if X is equal to z
374 */
Paul Bakkera755ca12011-04-24 09:11:17 +0000375int mpi_cmp_int( const mpi *X, t_sint z );
Paul Bakker5121ce52009-01-03 21:22:43 +0000376
377/**
378 * \brief Unsigned addition: X = |A| + |B|
379 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000380 * \param X Destination MPI
381 * \param A Left-hand MPI
382 * \param B Right-hand MPI
383 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000384 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000385 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000386 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000387int mpi_add_abs( mpi *X, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000388
389/**
390 * \brief Unsigned substraction: X = |A| - |B|
391 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000392 * \param X Destination MPI
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,
Paul Bakker40e46942009-01-03 21:51:57 +0000397 * POLARSSL_ERR_MPI_NEGATIVE_VALUE if B is greater than A
Paul Bakker5121ce52009-01-03 21:22:43 +0000398 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000399int mpi_sub_abs( mpi *X, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000400
401/**
402 * \brief Signed addition: X = A + B
403 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000404 * \param X Destination MPI
405 * \param A Left-hand MPI
406 * \param B Right-hand MPI
407 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000408 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000409 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000410 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000411int mpi_add_mpi( mpi *X, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000412
413/**
414 * \brief Signed substraction: X = A - B
415 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000416 * \param X Destination MPI
417 * \param A Left-hand MPI
418 * \param B Right-hand MPI
419 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000420 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000421 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000422 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000423int mpi_sub_mpi( mpi *X, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000424
425/**
426 * \brief Signed addition: X = A + b
427 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000428 * \param X Destination MPI
429 * \param A Left-hand MPI
430 * \param b The integer value to add
431 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000432 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000433 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000434 */
Paul Bakkera755ca12011-04-24 09:11:17 +0000435int mpi_add_int( mpi *X, const mpi *A, t_sint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000436
437/**
438 * \brief Signed substraction: X = A - b
439 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000440 * \param X Destination MPI
441 * \param A Left-hand MPI
442 * \param b The integer value to subtract
443 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000444 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000445 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000446 */
Paul Bakkera755ca12011-04-24 09:11:17 +0000447int mpi_sub_int( mpi *X, const mpi *A, t_sint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000448
449/**
450 * \brief Baseline multiplication: X = A * B
451 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000452 * \param X Destination MPI
453 * \param A Left-hand MPI
454 * \param B Right-hand MPI
455 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000456 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000457 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000458 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000459int mpi_mul_mpi( mpi *X, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000460
461/**
462 * \brief Baseline multiplication: X = A * b
Paul Bakkerce40a6d2009-06-23 19:46:08 +0000463 * Note: b is an unsigned integer type, thus
464 * Negative values of b are ignored.
Paul Bakker5121ce52009-01-03 21:22:43 +0000465 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000466 * \param X Destination MPI
467 * \param A Left-hand MPI
468 * \param b The integer value to multiply with
469 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000470 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000471 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000472 */
Paul Bakkera755ca12011-04-24 09:11:17 +0000473int mpi_mul_int( mpi *X, const mpi *A, t_sint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000474
475/**
476 * \brief Division by mpi: A = Q * B + R
477 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000478 * \param Q Destination MPI for the quotient
479 * \param R Destination MPI for the rest value
480 * \param A Left-hand MPI
481 * \param B Right-hand MPI
482 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000483 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000484 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000485 * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if B == 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000486 *
487 * \note Either Q or R can be NULL.
488 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000489int mpi_div_mpi( mpi *Q, mpi *R, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000490
491/**
492 * \brief Division by int: A = Q * b + R
493 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000494 * \param Q Destination MPI for the quotient
495 * \param R Destination MPI for the rest value
496 * \param A Left-hand MPI
497 * \param b Integer to divide by
498 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000499 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000500 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000501 * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if b == 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000502 *
503 * \note Either Q or R can be NULL.
504 */
Paul Bakkera755ca12011-04-24 09:11:17 +0000505int mpi_div_int( mpi *Q, mpi *R, const mpi *A, t_sint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000506
507/**
508 * \brief Modulo: R = A mod B
509 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000510 * \param R Destination MPI for the rest value
511 * \param A Left-hand MPI
512 * \param B Right-hand MPI
513 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000514 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000515 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
Paul Bakkerce40a6d2009-06-23 19:46:08 +0000516 * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if B == 0,
517 * POLARSSL_ERR_MPI_NEGATIVE_VALUE if B < 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000518 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000519int mpi_mod_mpi( mpi *R, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000520
521/**
522 * \brief Modulo: r = A mod b
523 *
Paul Bakkera755ca12011-04-24 09:11:17 +0000524 * \param r Destination t_uint
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000525 * \param A Left-hand MPI
526 * \param b Integer to divide by
527 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000528 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000529 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
Paul Bakkerce40a6d2009-06-23 19:46:08 +0000530 * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if b == 0,
531 * POLARSSL_ERR_MPI_NEGATIVE_VALUE if b < 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000532 */
Paul Bakkera755ca12011-04-24 09:11:17 +0000533int mpi_mod_int( t_uint *r, const mpi *A, t_sint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000534
535/**
536 * \brief Sliding-window exponentiation: X = A^E mod N
537 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000538 * \param X Destination MPI
539 * \param A Left-hand MPI
540 * \param E Exponent MPI
541 * \param N Modular MPI
542 * \param _RR Speed-up MPI used for recalculations
543 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000544 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000545 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000546 * POLARSSL_ERR_MPI_BAD_INPUT_DATA if N is negative or even
Paul Bakker5121ce52009-01-03 21:22:43 +0000547 *
548 * \note _RR is used to avoid re-computing R*R mod N across
549 * multiple calls, which speeds up things a bit. It can
550 * be set to NULL if the extra performance is unneeded.
551 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000552int mpi_exp_mod( mpi *X, const mpi *A, const mpi *E, const mpi *N, mpi *_RR );
Paul Bakker5121ce52009-01-03 21:22:43 +0000553
554/**
Paul Bakker287781a2011-03-26 13:18:49 +0000555 * \brief Fill an MPI X with size bytes of random
556 *
557 * \param X Destination MPI
558 * \param size Size in bytes
559 * \param f_rng RNG function
560 * \param p_rng RNG parameter
561 *
562 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000563 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker287781a2011-03-26 13:18:49 +0000564 */
Paul Bakkera3d195c2011-11-27 21:07:34 +0000565int mpi_fill_random( mpi *X, size_t size,
566 int (*f_rng)(void *, unsigned char *, size_t),
567 void *p_rng );
Paul Bakker287781a2011-03-26 13:18:49 +0000568
569/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000570 * \brief Greatest common divisor: G = gcd(A, B)
571 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000572 * \param G Destination MPI
573 * \param A Left-hand MPI
574 * \param B Right-hand MPI
575 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000576 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000577 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000578 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000579int mpi_gcd( mpi *G, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000580
581/**
582 * \brief Modular inverse: X = A^-1 mod N
583 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000584 * \param X Destination MPI
585 * \param A Left-hand MPI
586 * \param N Right-hand MPI
587 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000588 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000589 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000590 * POLARSSL_ERR_MPI_BAD_INPUT_DATA if N is negative or nil
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000591 POLARSSL_ERR_MPI_NOT_ACCEPTABLE if A has no inverse mod N
Paul Bakker5121ce52009-01-03 21:22:43 +0000592 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000593int mpi_inv_mod( mpi *X, const mpi *A, const mpi *N );
Paul Bakker5121ce52009-01-03 21:22:43 +0000594
595/**
596 * \brief Miller-Rabin primality test
597 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000598 * \param X MPI to check
599 * \param f_rng RNG function
600 * \param p_rng RNG parameter
601 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000602 * \return 0 if successful (probably prime),
Paul Bakker69e095c2011-12-10 21:55:01 +0000603 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000604 * POLARSSL_ERR_MPI_NOT_ACCEPTABLE if X is not prime
Paul Bakker5121ce52009-01-03 21:22:43 +0000605 */
Paul Bakkera3d195c2011-11-27 21:07:34 +0000606int mpi_is_prime( mpi *X,
607 int (*f_rng)(void *, unsigned char *, size_t),
608 void *p_rng );
Paul Bakker5121ce52009-01-03 21:22:43 +0000609
610/**
611 * \brief Prime number generation
612 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000613 * \param X Destination MPI
Paul Bakkerfe3256e2011-11-25 12:11:43 +0000614 * \param nbits Required size of X in bits ( 3 <= nbits <= POLARSSL_MPI_MAX_BITS )
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000615 * \param dh_flag If 1, then (X-1)/2 will be prime too
Paul Bakker5121ce52009-01-03 21:22:43 +0000616 * \param f_rng RNG function
617 * \param p_rng RNG parameter
618 *
619 * \return 0 if successful (probably prime),
Paul Bakker69e095c2011-12-10 21:55:01 +0000620 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000621 * POLARSSL_ERR_MPI_BAD_INPUT_DATA if nbits is < 3
Paul Bakker5121ce52009-01-03 21:22:43 +0000622 */
Paul Bakker23986e52011-04-24 08:57:21 +0000623int mpi_gen_prime( mpi *X, size_t nbits, int dh_flag,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000624 int (*f_rng)(void *, unsigned char *, size_t),
625 void *p_rng );
Paul Bakker5121ce52009-01-03 21:22:43 +0000626
627/**
628 * \brief Checkup routine
629 *
630 * \return 0 if successful, or 1 if the test failed
631 */
632int mpi_self_test( int verbose );
633
634#ifdef __cplusplus
635}
636#endif
637
638#endif /* bignum.h */