blob: f830c087f0120033fea1891bc0052d8792eb7dfd [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. */
Paul Bakker69e095c2011-12-10 21:55:01 +000036#define POLARSSL_ERR_MPI_BUFFER_TOO_SMALL -0x0008 /**< The buffer is too small to write to. */
Paul Bakker9d781402011-05-09 16:17:09 +000037#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 Bakker69e095c2011-12-10 21:55:01 +000040#define POLARSSL_ERR_MPI_MALLOC_FAILED -0x0010 /**< Memory allocation failed. */
Paul Bakker5121ce52009-01-03 21:22:43 +000041
42#define MPI_CHK(f) if( ( ret = f ) != 0 ) goto cleanup
43
44/*
Paul Bakkerf9688572011-05-05 10:00:45 +000045 * Maximum size MPIs are allowed to grow to in number of limbs.
46 */
47#define POLARSSL_MPI_MAX_LIMBS 10000
48
49/*
Paul Bakkerb6d5f082011-11-25 11:52:11 +000050 * Maximum window size used for modular exponentiation. Default: 6
51 * Minimum value: 1. Maximum value: 6.
52 *
53 * Result is an array of ( 2 << POLARSSL_MPI_WINDOW_SIZE ) MPIs used
54 * for the sliding window calculation. (So 64 by default)
55 *
56 * Reduction in size, reduces speed.
57 */
58#define POLARSSL_MPI_WINDOW_SIZE 6 /**< Maximum windows size used. */
59
60/*
Paul Bakkerfe3256e2011-11-25 12:11:43 +000061 * Maximum size of MPIs allowed in bits and bytes for user-MPIs.
62 * ( Default: 512 bytes => 4096 bits )
63 *
64 * Note: Calculations can results temporarily in larger MPIs. So the number
65 * of limbs required (POLARSSL_MPI_MAX_LIMBS) is higher.
66 */
67#define POLARSSL_MPI_MAX_SIZE 512 /**< Maximum number of bytes for usable MPIs. */
68#define POLARSSL_MPI_MAX_BITS ( 8 * POLARSSL_MPI_MAX_SIZE ) /**< Maximum number of bits for usable MPIs. */
69
70/*
Paul Bakkercb37aa52011-11-30 16:00:20 +000071 * When reading from files with mpi_read_file() the buffer should have space
72 * for a (short) label, the MPI (in the provided radix), the newline
73 * characters and the '\0'.
74 *
75 * By default we assume at least a 10 char label, a minimum radix of 10
76 * (decimal) and a maximum of 4096 bit numbers (1234 decimal chars).
77 */
78#define POLARSSL_MPI_READ_BUFFER_SIZE 1250
79
80/*
Paul Bakker5121ce52009-01-03 21:22:43 +000081 * Define the base integer type, architecture-wise
82 */
Paul Bakker40e46942009-01-03 21:51:57 +000083#if defined(POLARSSL_HAVE_INT8)
Paul Bakkera755ca12011-04-24 09:11:17 +000084typedef signed char t_sint;
85typedef unsigned char t_uint;
86typedef unsigned short t_udbl;
Paul Bakker5121ce52009-01-03 21:22:43 +000087#else
Paul Bakker40e46942009-01-03 21:51:57 +000088#if defined(POLARSSL_HAVE_INT16)
Paul Bakkera755ca12011-04-24 09:11:17 +000089typedef signed short t_sint;
90typedef unsigned short t_uint;
91typedef unsigned long t_udbl;
Paul Bakker5121ce52009-01-03 21:22:43 +000092#else
Paul Bakkera755ca12011-04-24 09:11:17 +000093 typedef signed long t_sint;
94 typedef unsigned long t_uint;
Paul Bakker5121ce52009-01-03 21:22:43 +000095 #if defined(_MSC_VER) && defined(_M_IX86)
Paul Bakkera755ca12011-04-24 09:11:17 +000096 typedef unsigned __int64 t_udbl;
Paul Bakker5121ce52009-01-03 21:22:43 +000097 #else
98 #if defined(__amd64__) || defined(__x86_64__) || \
99 defined(__ppc64__) || defined(__powerpc64__) || \
Paul Bakker44637402011-11-26 09:23:07 +0000100 defined(__ia64__) || defined(__alpha__) || \
101 (defined(__sparc__) && defined(__arch64__)) || \
102 defined(__s390x__)
Paul Bakkera755ca12011-04-24 09:11:17 +0000103 typedef unsigned int t_udbl __attribute__((mode(TI)));
Paul Bakker5121ce52009-01-03 21:22:43 +0000104 #else
Paul Bakker1a9382e2009-07-11 16:35:32 +0000105 #if defined(POLARSSL_HAVE_LONGLONG)
Paul Bakkera755ca12011-04-24 09:11:17 +0000106 typedef unsigned long long t_udbl;
Paul Bakker1a9382e2009-07-11 16:35:32 +0000107 #endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000108 #endif
109 #endif
110#endif
111#endif
112
113/**
114 * \brief MPI structure
115 */
116typedef struct
117{
118 int s; /*!< integer sign */
Paul Bakker23986e52011-04-24 08:57:21 +0000119 size_t n; /*!< total # of limbs */
Paul Bakkera755ca12011-04-24 09:11:17 +0000120 t_uint *p; /*!< pointer to limbs */
Paul Bakker5121ce52009-01-03 21:22:43 +0000121}
122mpi;
123
124#ifdef __cplusplus
125extern "C" {
126#endif
127
128/**
Paul Bakker6c591fa2011-05-05 11:49:20 +0000129 * \brief Initialize one MPI
130 *
131 * \param X One MPI to initialize.
Paul Bakker5121ce52009-01-03 21:22:43 +0000132 */
Paul Bakker6c591fa2011-05-05 11:49:20 +0000133void mpi_init( mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000134
135/**
Paul Bakker6c591fa2011-05-05 11:49:20 +0000136 * \brief Unallocate one MPI
137 *
138 * \param X One MPI to unallocate.
Paul Bakker5121ce52009-01-03 21:22:43 +0000139 */
Paul Bakker6c591fa2011-05-05 11:49:20 +0000140void mpi_free( mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000141
142/**
143 * \brief Enlarge to the specified number of limbs
144 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000145 * \param X MPI to grow
146 * \param nblimbs The target number of limbs
147 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000148 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000149 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000150 */
Paul Bakker23986e52011-04-24 08:57:21 +0000151int mpi_grow( mpi *X, size_t nblimbs );
Paul Bakker5121ce52009-01-03 21:22:43 +0000152
153/**
154 * \brief Copy the contents of Y into X
155 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000156 * \param X Destination MPI
157 * \param Y Source MPI
158 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000159 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000160 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000161 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000162int mpi_copy( mpi *X, const mpi *Y );
Paul Bakker5121ce52009-01-03 21:22:43 +0000163
164/**
165 * \brief Swap the contents of X and Y
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000166 *
167 * \param X First MPI value
168 * \param Y Second MPI value
Paul Bakker5121ce52009-01-03 21:22:43 +0000169 */
170void mpi_swap( mpi *X, mpi *Y );
171
172/**
173 * \brief Set value from integer
174 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000175 * \param X MPI to set
176 * \param z Value to use
177 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000178 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000179 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000180 */
Paul Bakkera755ca12011-04-24 09:11:17 +0000181int mpi_lset( mpi *X, t_sint z );
Paul Bakker5121ce52009-01-03 21:22:43 +0000182
Paul Bakker2f5947e2011-05-18 15:47:11 +0000183/*
184 * \brief Get a specific bit from X
185 *
186 * \param X MPI to use
187 * \param pos Zero-based index of the bit in X
188 *
189 * \return Either a 0 or a 1
190 */
191int mpi_get_bit( mpi *X, size_t pos );
192
193/*
194 * \brief Set a bit of X to a specific value of 0 or 1
195 *
196 * \note Will grow X if necessary to set a bit to 1 in a not yet
197 * existing limb. Will not grow if bit should be set to 0
198 *
199 * \param X MPI to use
200 * \param pos Zero-based index of the bit in X
201 * \param val The value to set the bit to (0 or 1)
202 *
203 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000204 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
Paul Bakker2f5947e2011-05-18 15:47:11 +0000205 * POLARSSL_ERR_MPI_BAD_INPUT_DATA if val is not 0 or 1
206 */
207int mpi_set_bit( mpi *X, size_t pos, unsigned char val );
208
Paul Bakker5121ce52009-01-03 21:22:43 +0000209/**
210 * \brief Return the number of least significant bits
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000211 *
212 * \param X MPI to use
Paul Bakker5121ce52009-01-03 21:22:43 +0000213 */
Paul Bakker23986e52011-04-24 08:57:21 +0000214size_t mpi_lsb( const mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000215
216/**
217 * \brief Return the number of most significant bits
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_msb( const mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000222
223/**
224 * \brief Return the total size in bytes
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000225 *
226 * \param X MPI to use
Paul Bakker5121ce52009-01-03 21:22:43 +0000227 */
Paul Bakker23986e52011-04-24 08:57:21 +0000228size_t mpi_size( const mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000229
230/**
231 * \brief Import from an ASCII string
232 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000233 * \param X Destination MPI
234 * \param radix Input numeric base
235 * \param s Null-terminated string buffer
Paul Bakker5121ce52009-01-03 21:22:43 +0000236 *
Paul Bakkercb37aa52011-11-30 16:00:20 +0000237 * \return 0 if successful, or a POLARSSL_ERR_MPI_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000238 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000239int mpi_read_string( mpi *X, int radix, const char *s );
Paul Bakker5121ce52009-01-03 21:22:43 +0000240
241/**
242 * \brief Export into an ASCII string
243 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000244 * \param X Source MPI
245 * \param radix Output numeric base
246 * \param s String buffer
247 * \param slen String buffer size
Paul Bakker5121ce52009-01-03 21:22:43 +0000248 *
Paul Bakkercb37aa52011-11-30 16:00:20 +0000249 * \return 0 if successful, or a POLARSSL_ERR_MPI_XXX error code.
Paul Bakkerff60ee62010-03-16 21:09:09 +0000250 * *slen is always updated to reflect the amount
251 * of data that has (or would have) been written.
Paul Bakker5121ce52009-01-03 21:22:43 +0000252 *
253 * \note Call this function with *slen = 0 to obtain the
254 * minimum required buffer size in *slen.
255 */
Paul Bakker23986e52011-04-24 08:57:21 +0000256int mpi_write_string( const mpi *X, int radix, char *s, size_t *slen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000257
258/**
259 * \brief Read X from an opened file
260 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000261 * \param X Destination MPI
262 * \param radix Input numeric base
263 * \param fin Input file handle
Paul Bakker5121ce52009-01-03 21:22:43 +0000264 *
Paul Bakkercb37aa52011-11-30 16:00:20 +0000265 * \return 0 if successful, POLARSSL_ERR_MPI_BUFFER_TOO_SMALL if
266 * the file read buffer is too small or a
267 * POLARSSL_ERR_MPI_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000268 */
269int mpi_read_file( mpi *X, int radix, FILE *fin );
270
271/**
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000272 * \brief Write X into an opened file, or stdout if fout is NULL
Paul Bakker5121ce52009-01-03 21:22:43 +0000273 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000274 * \param p Prefix, can be NULL
275 * \param X Source MPI
276 * \param radix Output numeric base
277 * \param fout Output file handle (can be NULL)
Paul Bakker5121ce52009-01-03 21:22:43 +0000278 *
Paul Bakkercb37aa52011-11-30 16:00:20 +0000279 * \return 0 if successful, or a POLARSSL_ERR_MPI_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000280 *
281 * \note Set fout == NULL to print X on the console.
282 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000283int mpi_write_file( const char *p, const mpi *X, int radix, FILE *fout );
Paul Bakker5121ce52009-01-03 21:22:43 +0000284
285/**
286 * \brief Import X from unsigned binary data, big endian
287 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000288 * \param X Destination MPI
289 * \param buf Input buffer
290 * \param buflen Input buffer size
Paul Bakker5121ce52009-01-03 21:22:43 +0000291 *
292 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000293 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000294 */
Paul Bakker23986e52011-04-24 08:57:21 +0000295int mpi_read_binary( mpi *X, const unsigned char *buf, size_t buflen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000296
297/**
298 * \brief Export X into unsigned binary data, big endian
299 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000300 * \param X Source MPI
301 * \param buf Output buffer
302 * \param buflen Output buffer size
Paul Bakker5121ce52009-01-03 21:22:43 +0000303 *
304 * \return 0 if successful,
Paul Bakker40e46942009-01-03 21:51:57 +0000305 * POLARSSL_ERR_MPI_BUFFER_TOO_SMALL if buf isn't large enough
Paul Bakker5121ce52009-01-03 21:22:43 +0000306 */
Paul Bakker23986e52011-04-24 08:57:21 +0000307int mpi_write_binary( const mpi *X, unsigned char *buf, size_t buflen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000308
309/**
310 * \brief Left-shift: X <<= count
311 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000312 * \param X MPI to shift
313 * \param count Amount to shift
314 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000315 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000316 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000317 */
Paul Bakker23986e52011-04-24 08:57:21 +0000318int mpi_shift_l( mpi *X, size_t count );
Paul Bakker5121ce52009-01-03 21:22:43 +0000319
320/**
321 * \brief Right-shift: X >>= count
322 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000323 * \param X MPI to shift
324 * \param count Amount to shift
325 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000326 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000327 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000328 */
Paul Bakker23986e52011-04-24 08:57:21 +0000329int mpi_shift_r( mpi *X, size_t count );
Paul Bakker5121ce52009-01-03 21:22:43 +0000330
331/**
332 * \brief Compare unsigned values
333 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000334 * \param X Left-hand MPI
335 * \param Y Right-hand MPI
336 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000337 * \return 1 if |X| is greater than |Y|,
338 * -1 if |X| is lesser than |Y| or
339 * 0 if |X| is equal to |Y|
340 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000341int mpi_cmp_abs( const mpi *X, const mpi *Y );
Paul Bakker5121ce52009-01-03 21:22:43 +0000342
343/**
344 * \brief Compare signed values
345 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000346 * \param X Left-hand MPI
347 * \param Y Right-hand MPI
348 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000349 * \return 1 if X is greater than Y,
350 * -1 if X is lesser than Y or
351 * 0 if X is equal to Y
352 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000353int mpi_cmp_mpi( const mpi *X, const mpi *Y );
Paul Bakker5121ce52009-01-03 21:22:43 +0000354
355/**
356 * \brief Compare signed values
357 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000358 * \param X Left-hand MPI
359 * \param z The integer value to compare to
360 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000361 * \return 1 if X is greater than z,
362 * -1 if X is lesser than z or
363 * 0 if X is equal to z
364 */
Paul Bakkera755ca12011-04-24 09:11:17 +0000365int mpi_cmp_int( const mpi *X, t_sint z );
Paul Bakker5121ce52009-01-03 21:22:43 +0000366
367/**
368 * \brief Unsigned addition: 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,
Paul Bakker69e095c2011-12-10 21:55:01 +0000375 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000376 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000377int mpi_add_abs( mpi *X, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000378
379/**
380 * \brief Unsigned substraction: 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 Right-hand MPI
385 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000386 * \return 0 if successful,
Paul Bakker40e46942009-01-03 21:51:57 +0000387 * POLARSSL_ERR_MPI_NEGATIVE_VALUE if B is greater than A
Paul Bakker5121ce52009-01-03 21:22:43 +0000388 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000389int mpi_sub_abs( mpi *X, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000390
391/**
392 * \brief Signed addition: 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 Right-hand MPI
397 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000398 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000399 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000400 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000401int mpi_add_mpi( mpi *X, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000402
403/**
404 * \brief Signed substraction: 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,
Paul Bakker69e095c2011-12-10 21:55:01 +0000411 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000412 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000413int mpi_sub_mpi( mpi *X, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000414
415/**
416 * \brief Signed addition: X = A + b
417 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000418 * \param X Destination MPI
419 * \param A Left-hand MPI
420 * \param b The integer value to add
421 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000422 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000423 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000424 */
Paul Bakkera755ca12011-04-24 09:11:17 +0000425int mpi_add_int( mpi *X, const mpi *A, t_sint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000426
427/**
428 * \brief Signed substraction: X = A - b
429 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000430 * \param X Destination MPI
431 * \param A Left-hand MPI
432 * \param b The integer value to subtract
433 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000434 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000435 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000436 */
Paul Bakkera755ca12011-04-24 09:11:17 +0000437int mpi_sub_int( mpi *X, const mpi *A, t_sint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000438
439/**
440 * \brief Baseline multiplication: X = A * B
441 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000442 * \param X Destination MPI
443 * \param A Left-hand MPI
444 * \param B Right-hand MPI
445 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000446 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000447 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000448 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000449int mpi_mul_mpi( mpi *X, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000450
451/**
452 * \brief Baseline multiplication: X = A * b
Paul Bakkerce40a6d2009-06-23 19:46:08 +0000453 * Note: b is an unsigned integer type, thus
454 * Negative values of b are ignored.
Paul Bakker5121ce52009-01-03 21:22:43 +0000455 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000456 * \param X Destination MPI
457 * \param A Left-hand MPI
458 * \param b The integer value to multiply with
459 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000460 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000461 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000462 */
Paul Bakkera755ca12011-04-24 09:11:17 +0000463int mpi_mul_int( mpi *X, const mpi *A, t_sint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000464
465/**
466 * \brief Division by mpi: A = Q * B + R
467 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000468 * \param Q Destination MPI for the quotient
469 * \param R Destination MPI for the rest value
470 * \param A Left-hand MPI
471 * \param B Right-hand MPI
472 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000473 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000474 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000475 * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if B == 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000476 *
477 * \note Either Q or R can be NULL.
478 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000479int mpi_div_mpi( mpi *Q, mpi *R, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000480
481/**
482 * \brief Division by int: A = Q * b + R
483 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000484 * \param Q Destination MPI for the quotient
485 * \param R Destination MPI for the rest value
486 * \param A Left-hand MPI
487 * \param b Integer to divide by
488 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000489 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000490 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000491 * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if b == 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000492 *
493 * \note Either Q or R can be NULL.
494 */
Paul Bakkera755ca12011-04-24 09:11:17 +0000495int mpi_div_int( mpi *Q, mpi *R, const mpi *A, t_sint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000496
497/**
498 * \brief Modulo: R = A mod B
499 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000500 * \param R Destination MPI for the rest value
501 * \param A Left-hand MPI
502 * \param B Right-hand MPI
503 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000504 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000505 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
Paul Bakkerce40a6d2009-06-23 19:46:08 +0000506 * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if B == 0,
507 * POLARSSL_ERR_MPI_NEGATIVE_VALUE if B < 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000508 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000509int mpi_mod_mpi( mpi *R, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000510
511/**
512 * \brief Modulo: r = A mod b
513 *
Paul Bakkera755ca12011-04-24 09:11:17 +0000514 * \param r Destination t_uint
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000515 * \param A Left-hand MPI
516 * \param b Integer to divide by
517 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000518 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000519 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
Paul Bakkerce40a6d2009-06-23 19:46:08 +0000520 * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if b == 0,
521 * POLARSSL_ERR_MPI_NEGATIVE_VALUE if b < 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000522 */
Paul Bakkera755ca12011-04-24 09:11:17 +0000523int mpi_mod_int( t_uint *r, const mpi *A, t_sint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000524
525/**
526 * \brief Sliding-window exponentiation: X = A^E mod N
527 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000528 * \param X Destination MPI
529 * \param A Left-hand MPI
530 * \param E Exponent MPI
531 * \param N Modular MPI
532 * \param _RR Speed-up MPI used for recalculations
533 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000534 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000535 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000536 * POLARSSL_ERR_MPI_BAD_INPUT_DATA if N is negative or even
Paul Bakker5121ce52009-01-03 21:22:43 +0000537 *
538 * \note _RR is used to avoid re-computing R*R mod N across
539 * multiple calls, which speeds up things a bit. It can
540 * be set to NULL if the extra performance is unneeded.
541 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000542int mpi_exp_mod( mpi *X, const mpi *A, const mpi *E, const mpi *N, mpi *_RR );
Paul Bakker5121ce52009-01-03 21:22:43 +0000543
544/**
Paul Bakker287781a2011-03-26 13:18:49 +0000545 * \brief Fill an MPI X with size bytes of random
546 *
547 * \param X Destination MPI
548 * \param size Size in bytes
549 * \param f_rng RNG function
550 * \param p_rng RNG parameter
551 *
552 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000553 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker287781a2011-03-26 13:18:49 +0000554 */
Paul Bakkera3d195c2011-11-27 21:07:34 +0000555int mpi_fill_random( mpi *X, size_t size,
556 int (*f_rng)(void *, unsigned char *, size_t),
557 void *p_rng );
Paul Bakker287781a2011-03-26 13:18:49 +0000558
559/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000560 * \brief Greatest common divisor: G = gcd(A, B)
561 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000562 * \param G Destination MPI
563 * \param A Left-hand MPI
564 * \param B Right-hand MPI
565 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000566 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000567 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000568 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000569int mpi_gcd( mpi *G, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000570
571/**
572 * \brief Modular inverse: X = A^-1 mod N
573 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000574 * \param X Destination MPI
575 * \param A Left-hand MPI
576 * \param N Right-hand MPI
577 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000578 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000579 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000580 * POLARSSL_ERR_MPI_BAD_INPUT_DATA if N is negative or nil
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000581 POLARSSL_ERR_MPI_NOT_ACCEPTABLE if A has no inverse mod N
Paul Bakker5121ce52009-01-03 21:22:43 +0000582 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000583int mpi_inv_mod( mpi *X, const mpi *A, const mpi *N );
Paul Bakker5121ce52009-01-03 21:22:43 +0000584
585/**
586 * \brief Miller-Rabin primality test
587 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000588 * \param X MPI to check
589 * \param f_rng RNG function
590 * \param p_rng RNG parameter
591 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000592 * \return 0 if successful (probably prime),
Paul Bakker69e095c2011-12-10 21:55:01 +0000593 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000594 * POLARSSL_ERR_MPI_NOT_ACCEPTABLE if X is not prime
Paul Bakker5121ce52009-01-03 21:22:43 +0000595 */
Paul Bakkera3d195c2011-11-27 21:07:34 +0000596int mpi_is_prime( mpi *X,
597 int (*f_rng)(void *, unsigned char *, size_t),
598 void *p_rng );
Paul Bakker5121ce52009-01-03 21:22:43 +0000599
600/**
601 * \brief Prime number generation
602 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000603 * \param X Destination MPI
Paul Bakkerfe3256e2011-11-25 12:11:43 +0000604 * \param nbits Required size of X in bits ( 3 <= nbits <= POLARSSL_MPI_MAX_BITS )
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000605 * \param dh_flag If 1, then (X-1)/2 will be prime too
Paul Bakker5121ce52009-01-03 21:22:43 +0000606 * \param f_rng RNG function
607 * \param p_rng RNG parameter
608 *
609 * \return 0 if successful (probably prime),
Paul Bakker69e095c2011-12-10 21:55:01 +0000610 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000611 * POLARSSL_ERR_MPI_BAD_INPUT_DATA if nbits is < 3
Paul Bakker5121ce52009-01-03 21:22:43 +0000612 */
Paul Bakker23986e52011-04-24 08:57:21 +0000613int mpi_gen_prime( mpi *X, size_t nbits, int dh_flag,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000614 int (*f_rng)(void *, unsigned char *, size_t),
615 void *p_rng );
Paul Bakker5121ce52009-01-03 21:22:43 +0000616
617/**
618 * \brief Checkup routine
619 *
620 * \return 0 if successful, or 1 if the test failed
621 */
622int mpi_self_test( int verbose );
623
624#ifdef __cplusplus
625}
626#endif
627
628#endif /* bignum.h */