blob: 092e185d52cdf457c4514b03b669a4647147b75c [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 Bakker5c2364c2012-10-01 14:41:15 +000035#ifdef _MSC_VER
36#include <basetsd.h>
37typedef INT16 int16_t;
38typedef UINT16 uint16_t;
39typedef UINT32 uint32_t;
40typedef UINT64 uint64_t;
41#else
42#include <inttypes.h>
43#endif
44
Paul Bakker9d781402011-05-09 16:17:09 +000045#define POLARSSL_ERR_MPI_FILE_IO_ERROR -0x0002 /**< An error occurred while reading from or writing to a file. */
46#define POLARSSL_ERR_MPI_BAD_INPUT_DATA -0x0004 /**< Bad input parameters to function. */
47#define POLARSSL_ERR_MPI_INVALID_CHARACTER -0x0006 /**< There is an invalid character in the digit string. */
Paul Bakker69e095c2011-12-10 21:55:01 +000048#define POLARSSL_ERR_MPI_BUFFER_TOO_SMALL -0x0008 /**< The buffer is too small to write to. */
Paul Bakker9d781402011-05-09 16:17:09 +000049#define POLARSSL_ERR_MPI_NEGATIVE_VALUE -0x000A /**< The input arguments are negative or result in illegal output. */
50#define POLARSSL_ERR_MPI_DIVISION_BY_ZERO -0x000C /**< The input argument for division is zero, which is not allowed. */
51#define POLARSSL_ERR_MPI_NOT_ACCEPTABLE -0x000E /**< The input arguments are not acceptable. */
Paul Bakker69e095c2011-12-10 21:55:01 +000052#define POLARSSL_ERR_MPI_MALLOC_FAILED -0x0010 /**< Memory allocation failed. */
Paul Bakker5121ce52009-01-03 21:22:43 +000053
54#define MPI_CHK(f) if( ( ret = f ) != 0 ) goto cleanup
55
56/*
Paul Bakkerf9688572011-05-05 10:00:45 +000057 * Maximum size MPIs are allowed to grow to in number of limbs.
58 */
59#define POLARSSL_MPI_MAX_LIMBS 10000
60
61/*
Paul Bakkerb6d5f082011-11-25 11:52:11 +000062 * Maximum window size used for modular exponentiation. Default: 6
63 * Minimum value: 1. Maximum value: 6.
64 *
65 * Result is an array of ( 2 << POLARSSL_MPI_WINDOW_SIZE ) MPIs used
66 * for the sliding window calculation. (So 64 by default)
67 *
68 * Reduction in size, reduces speed.
69 */
70#define POLARSSL_MPI_WINDOW_SIZE 6 /**< Maximum windows size used. */
71
72/*
Paul Bakkerfe3256e2011-11-25 12:11:43 +000073 * Maximum size of MPIs allowed in bits and bytes for user-MPIs.
Paul Bakker5552c8c2012-07-05 13:31:54 +000074 * ( Default: 512 bytes => 4096 bits, Maximum: 1024 bytes => 8192 bits )
Paul Bakkerfe3256e2011-11-25 12:11:43 +000075 *
76 * Note: Calculations can results temporarily in larger MPIs. So the number
77 * of limbs required (POLARSSL_MPI_MAX_LIMBS) is higher.
78 */
79#define POLARSSL_MPI_MAX_SIZE 512 /**< Maximum number of bytes for usable MPIs. */
80#define POLARSSL_MPI_MAX_BITS ( 8 * POLARSSL_MPI_MAX_SIZE ) /**< Maximum number of bits for usable MPIs. */
81
82/*
Paul Bakker5531c6d2012-09-26 19:20:46 +000083 * When reading from files with mpi_read_file() and writing to files with
84 * mpi_write_file() the buffer should have space
Paul Bakkercb37aa52011-11-30 16:00:20 +000085 * for a (short) label, the MPI (in the provided radix), the newline
86 * characters and the '\0'.
87 *
88 * By default we assume at least a 10 char label, a minimum radix of 10
89 * (decimal) and a maximum of 4096 bit numbers (1234 decimal chars).
Paul Bakkerf9183102012-09-27 20:42:35 +000090 * Autosized at compile time for at least a 10 char label, a minimum radix
91 * of 10 (decimal) for a number of POLARSSL_MPI_MAX_BITS size.
92 *
93 * This used to be statically sized to 1250 for a maximum of 4096 bit
94 * numbers (1234 decimal chars).
95 *
96 * Calculate using the formula:
97 * POLARSSL_MPI_RW_BUFFER_SIZE = ceil(POLARSSL_MPI_MAX_BITS / ln(10) * ln(2)) +
98 * LabelSize + 6
Paul Bakkercb37aa52011-11-30 16:00:20 +000099 */
Paul Bakkerf9183102012-09-27 20:42:35 +0000100#define POLARSSL_MPI_MAX_BITS_SCALE100 ( 100 * POLARSSL_MPI_MAX_BITS )
101#define LN_2_DIV_LN_10_SCALE100 332
102#define POLARSSL_MPI_RW_BUFFER_SIZE ( ((POLARSSL_MPI_MAX_BITS_SCALE100 + LN_2_DIV_LN_10_SCALE100 - 1) / LN_2_DIV_LN_10_SCALE100) + 10 + 6 )
Paul Bakkercb37aa52011-11-30 16:00:20 +0000103
104/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000105 * Define the base integer type, architecture-wise
106 */
Paul Bakker40e46942009-01-03 21:51:57 +0000107#if defined(POLARSSL_HAVE_INT8)
Paul Bakkera755ca12011-04-24 09:11:17 +0000108typedef signed char t_sint;
109typedef unsigned char t_uint;
Paul Bakker5c2364c2012-10-01 14:41:15 +0000110typedef uint16_t t_udbl;
Paul Bakker5121ce52009-01-03 21:22:43 +0000111#else
Paul Bakker40e46942009-01-03 21:51:57 +0000112#if defined(POLARSSL_HAVE_INT16)
Paul Bakker5c2364c2012-10-01 14:41:15 +0000113typedef int16_t t_sint;
114typedef uint16_t t_uint;
115typedef uint32_t t_udbl;
Paul Bakker5121ce52009-01-03 21:22:43 +0000116#else
Paul Bakker5c2364c2012-10-01 14:41:15 +0000117 typedef int32_t t_sint;
118 typedef uint32_t t_uint;
119 #if ( defined(_MSC_VER) && defined(_M_IX86) ) || \
120 ( defined(__GNUC__) && ( \
Paul Bakkercf0360a2012-01-20 10:08:14 +0000121 defined(__amd64__) || defined(__x86_64__) || \
Paul Bakker5121ce52009-01-03 21:22:43 +0000122 defined(__ppc64__) || defined(__powerpc64__) || \
Paul Bakker44637402011-11-26 09:23:07 +0000123 defined(__ia64__) || defined(__alpha__) || \
124 (defined(__sparc__) && defined(__arch64__)) || \
Paul Bakker5c2364c2012-10-01 14:41:15 +0000125 defined(__s390x__) ) )
126 #define POLARSSL_HAVE_INT64
Paul Bakker5121ce52009-01-03 21:22:43 +0000127 #endif
Paul Bakker5c2364c2012-10-01 14:41:15 +0000128 #if defined(POLARSSL_HAVE_INT64)
129 typedef uint64_t t_udbl;
130 #endif
131#endif /* POLARSSL_HAVE_INT16 */
132#endif /* POLARSSL_HAVE_INT8 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000133
134/**
135 * \brief MPI structure
136 */
137typedef struct
138{
139 int s; /*!< integer sign */
Paul Bakker23986e52011-04-24 08:57:21 +0000140 size_t n; /*!< total # of limbs */
Paul Bakkera755ca12011-04-24 09:11:17 +0000141 t_uint *p; /*!< pointer to limbs */
Paul Bakker5121ce52009-01-03 21:22:43 +0000142}
143mpi;
144
145#ifdef __cplusplus
146extern "C" {
147#endif
148
149/**
Paul Bakker6c591fa2011-05-05 11:49:20 +0000150 * \brief Initialize one MPI
151 *
152 * \param X One MPI to initialize.
Paul Bakker5121ce52009-01-03 21:22:43 +0000153 */
Paul Bakker6c591fa2011-05-05 11:49:20 +0000154void mpi_init( mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000155
156/**
Paul Bakker6c591fa2011-05-05 11:49:20 +0000157 * \brief Unallocate one MPI
158 *
159 * \param X One MPI to unallocate.
Paul Bakker5121ce52009-01-03 21:22:43 +0000160 */
Paul Bakker6c591fa2011-05-05 11:49:20 +0000161void mpi_free( mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000162
163/**
164 * \brief Enlarge to the specified number of limbs
165 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000166 * \param X MPI to grow
167 * \param nblimbs The target number of limbs
168 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000169 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000170 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000171 */
Paul Bakker23986e52011-04-24 08:57:21 +0000172int mpi_grow( mpi *X, size_t nblimbs );
Paul Bakker5121ce52009-01-03 21:22:43 +0000173
174/**
175 * \brief Copy the contents of Y into X
176 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000177 * \param X Destination MPI
178 * \param Y Source MPI
179 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000180 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000181 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000182 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000183int mpi_copy( mpi *X, const mpi *Y );
Paul Bakker5121ce52009-01-03 21:22:43 +0000184
185/**
186 * \brief Swap the contents of X and Y
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000187 *
188 * \param X First MPI value
189 * \param Y Second MPI value
Paul Bakker5121ce52009-01-03 21:22:43 +0000190 */
191void mpi_swap( mpi *X, mpi *Y );
192
193/**
194 * \brief Set value from integer
195 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000196 * \param X MPI to set
197 * \param z Value to use
198 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000199 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000200 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000201 */
Paul Bakkera755ca12011-04-24 09:11:17 +0000202int mpi_lset( mpi *X, t_sint z );
Paul Bakker5121ce52009-01-03 21:22:43 +0000203
Paul Bakker2f5947e2011-05-18 15:47:11 +0000204/*
205 * \brief Get a specific bit from X
206 *
207 * \param X MPI to use
208 * \param pos Zero-based index of the bit in X
209 *
210 * \return Either a 0 or a 1
211 */
Paul Bakker6b906e52012-05-08 12:01:43 +0000212int mpi_get_bit( const mpi *X, size_t pos );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000213
214/*
215 * \brief Set a bit of X to a specific value of 0 or 1
216 *
217 * \note Will grow X if necessary to set a bit to 1 in a not yet
218 * existing limb. Will not grow if bit should be set to 0
219 *
220 * \param X MPI to use
221 * \param pos Zero-based index of the bit in X
222 * \param val The value to set the bit to (0 or 1)
223 *
224 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000225 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
Paul Bakker2f5947e2011-05-18 15:47:11 +0000226 * POLARSSL_ERR_MPI_BAD_INPUT_DATA if val is not 0 or 1
227 */
228int mpi_set_bit( mpi *X, size_t pos, unsigned char val );
229
Paul Bakker5121ce52009-01-03 21:22:43 +0000230/**
Paul Bakker6b906e52012-05-08 12:01:43 +0000231 * \brief Return the number of zero-bits before the least significant
232 * '1' bit
233 *
234 * Note: Thus also the zero-based index of the least significant '1' bit
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_lsb( const mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000239
240/**
Paul Bakker6b906e52012-05-08 12:01:43 +0000241 * \brief Return the number of bits up to and including the most
242 * significant '1' bit'
243 *
244 * Note: Thus also the one-based index of the most significant '1' bit
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000245 *
246 * \param X MPI to use
Paul Bakker5121ce52009-01-03 21:22:43 +0000247 */
Paul Bakker23986e52011-04-24 08:57:21 +0000248size_t mpi_msb( const mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000249
250/**
251 * \brief Return the total size in bytes
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000252 *
253 * \param X MPI to use
Paul Bakker5121ce52009-01-03 21:22:43 +0000254 */
Paul Bakker23986e52011-04-24 08:57:21 +0000255size_t mpi_size( const mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000256
257/**
258 * \brief Import from an ASCII string
259 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000260 * \param X Destination MPI
261 * \param radix Input numeric base
262 * \param s Null-terminated string buffer
Paul Bakker5121ce52009-01-03 21:22:43 +0000263 *
Paul Bakkercb37aa52011-11-30 16:00:20 +0000264 * \return 0 if successful, or a POLARSSL_ERR_MPI_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000265 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000266int mpi_read_string( mpi *X, int radix, const char *s );
Paul Bakker5121ce52009-01-03 21:22:43 +0000267
268/**
269 * \brief Export into an ASCII string
270 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000271 * \param X Source MPI
272 * \param radix Output numeric base
273 * \param s String buffer
274 * \param slen String buffer size
Paul Bakker5121ce52009-01-03 21:22:43 +0000275 *
Paul Bakkercb37aa52011-11-30 16:00:20 +0000276 * \return 0 if successful, or a POLARSSL_ERR_MPI_XXX error code.
Paul Bakkerff60ee62010-03-16 21:09:09 +0000277 * *slen is always updated to reflect the amount
278 * of data that has (or would have) been written.
Paul Bakker5121ce52009-01-03 21:22:43 +0000279 *
280 * \note Call this function with *slen = 0 to obtain the
281 * minimum required buffer size in *slen.
282 */
Paul Bakker23986e52011-04-24 08:57:21 +0000283int mpi_write_string( const mpi *X, int radix, char *s, size_t *slen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000284
285/**
286 * \brief Read X from an opened file
287 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000288 * \param X Destination MPI
289 * \param radix Input numeric base
290 * \param fin Input file handle
Paul Bakker5121ce52009-01-03 21:22:43 +0000291 *
Paul Bakkercb37aa52011-11-30 16:00:20 +0000292 * \return 0 if successful, POLARSSL_ERR_MPI_BUFFER_TOO_SMALL if
293 * the file read buffer is too small or a
294 * POLARSSL_ERR_MPI_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000295 */
296int mpi_read_file( mpi *X, int radix, FILE *fin );
297
298/**
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000299 * \brief Write X into an opened file, or stdout if fout is NULL
Paul Bakker5121ce52009-01-03 21:22:43 +0000300 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000301 * \param p Prefix, can be NULL
302 * \param X Source MPI
303 * \param radix Output numeric base
304 * \param fout Output file handle (can be NULL)
Paul Bakker5121ce52009-01-03 21:22:43 +0000305 *
Paul Bakkercb37aa52011-11-30 16:00:20 +0000306 * \return 0 if successful, or a POLARSSL_ERR_MPI_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000307 *
308 * \note Set fout == NULL to print X on the console.
309 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000310int mpi_write_file( const char *p, const mpi *X, int radix, FILE *fout );
Paul Bakker5121ce52009-01-03 21:22:43 +0000311
312/**
313 * \brief Import X from unsigned binary data, big endian
314 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000315 * \param X Destination MPI
316 * \param buf Input buffer
317 * \param buflen Input buffer size
Paul Bakker5121ce52009-01-03 21:22:43 +0000318 *
319 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000320 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000321 */
Paul Bakker23986e52011-04-24 08:57:21 +0000322int mpi_read_binary( mpi *X, const unsigned char *buf, size_t buflen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000323
324/**
325 * \brief Export X into unsigned binary data, big endian
326 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000327 * \param X Source MPI
328 * \param buf Output buffer
329 * \param buflen Output buffer size
Paul Bakker5121ce52009-01-03 21:22:43 +0000330 *
331 * \return 0 if successful,
Paul Bakker40e46942009-01-03 21:51:57 +0000332 * POLARSSL_ERR_MPI_BUFFER_TOO_SMALL if buf isn't large enough
Paul Bakker5121ce52009-01-03 21:22:43 +0000333 */
Paul Bakker23986e52011-04-24 08:57:21 +0000334int mpi_write_binary( const mpi *X, unsigned char *buf, size_t buflen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000335
336/**
337 * \brief Left-shift: X <<= count
338 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000339 * \param X MPI to shift
340 * \param count Amount to shift
341 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000342 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000343 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000344 */
Paul Bakker23986e52011-04-24 08:57:21 +0000345int mpi_shift_l( mpi *X, size_t count );
Paul Bakker5121ce52009-01-03 21:22:43 +0000346
347/**
348 * \brief Right-shift: X >>= count
349 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000350 * \param X MPI to shift
351 * \param count Amount to shift
352 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000353 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000354 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000355 */
Paul Bakker23986e52011-04-24 08:57:21 +0000356int mpi_shift_r( mpi *X, size_t count );
Paul Bakker5121ce52009-01-03 21:22:43 +0000357
358/**
359 * \brief Compare unsigned values
360 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000361 * \param X Left-hand MPI
362 * \param Y Right-hand MPI
363 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000364 * \return 1 if |X| is greater than |Y|,
365 * -1 if |X| is lesser than |Y| or
366 * 0 if |X| is equal to |Y|
367 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000368int mpi_cmp_abs( const mpi *X, const mpi *Y );
Paul Bakker5121ce52009-01-03 21:22:43 +0000369
370/**
371 * \brief Compare signed values
372 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000373 * \param X Left-hand MPI
374 * \param Y Right-hand MPI
375 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000376 * \return 1 if X is greater than Y,
377 * -1 if X is lesser than Y or
378 * 0 if X is equal to Y
379 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000380int mpi_cmp_mpi( const mpi *X, const mpi *Y );
Paul Bakker5121ce52009-01-03 21:22:43 +0000381
382/**
383 * \brief Compare signed values
384 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000385 * \param X Left-hand MPI
386 * \param z The integer value to compare to
387 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000388 * \return 1 if X is greater than z,
389 * -1 if X is lesser than z or
390 * 0 if X is equal to z
391 */
Paul Bakkera755ca12011-04-24 09:11:17 +0000392int mpi_cmp_int( const mpi *X, t_sint z );
Paul Bakker5121ce52009-01-03 21:22:43 +0000393
394/**
395 * \brief Unsigned addition: X = |A| + |B|
396 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000397 * \param X Destination MPI
398 * \param A Left-hand MPI
399 * \param B Right-hand MPI
400 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000401 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000402 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000403 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000404int mpi_add_abs( mpi *X, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000405
406/**
407 * \brief Unsigned substraction: X = |A| - |B|
408 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000409 * \param X Destination MPI
410 * \param A Left-hand MPI
411 * \param B Right-hand MPI
412 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000413 * \return 0 if successful,
Paul Bakker40e46942009-01-03 21:51:57 +0000414 * POLARSSL_ERR_MPI_NEGATIVE_VALUE if B is greater than A
Paul Bakker5121ce52009-01-03 21:22:43 +0000415 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000416int mpi_sub_abs( mpi *X, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000417
418/**
419 * \brief Signed addition: X = A + B
420 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000421 * \param X Destination MPI
422 * \param A Left-hand MPI
423 * \param B Right-hand MPI
424 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000425 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000426 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000427 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000428int mpi_add_mpi( mpi *X, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000429
430/**
431 * \brief Signed substraction: X = A - B
432 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000433 * \param X Destination MPI
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,
Paul Bakker69e095c2011-12-10 21:55:01 +0000438 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000439 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000440int mpi_sub_mpi( mpi *X, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000441
442/**
443 * \brief Signed addition: X = A + b
444 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000445 * \param X Destination MPI
446 * \param A Left-hand MPI
447 * \param b The integer value to add
448 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000449 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000450 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000451 */
Paul Bakkera755ca12011-04-24 09:11:17 +0000452int mpi_add_int( mpi *X, const mpi *A, t_sint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000453
454/**
455 * \brief Signed substraction: X = A - b
456 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000457 * \param X Destination MPI
458 * \param A Left-hand MPI
459 * \param b The integer value to subtract
460 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000461 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000462 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000463 */
Paul Bakkera755ca12011-04-24 09:11:17 +0000464int mpi_sub_int( mpi *X, const mpi *A, t_sint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000465
466/**
467 * \brief Baseline multiplication: X = A * B
468 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000469 * \param X Destination MPI
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 Bakker5121ce52009-01-03 21:22:43 +0000475 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000476int mpi_mul_mpi( mpi *X, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000477
478/**
479 * \brief Baseline multiplication: X = A * b
Paul Bakkerce40a6d2009-06-23 19:46:08 +0000480 * Note: b is an unsigned integer type, thus
481 * Negative values of b are ignored.
Paul Bakker5121ce52009-01-03 21:22:43 +0000482 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000483 * \param X Destination MPI
484 * \param A Left-hand MPI
485 * \param b The integer value to multiply with
486 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000487 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000488 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000489 */
Paul Bakkera755ca12011-04-24 09:11:17 +0000490int mpi_mul_int( mpi *X, const mpi *A, t_sint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000491
492/**
493 * \brief Division by mpi: A = Q * B + R
494 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000495 * \param Q Destination MPI for the quotient
496 * \param R Destination MPI for the rest value
497 * \param A Left-hand MPI
498 * \param B Right-hand MPI
499 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000500 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000501 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000502 * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if B == 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000503 *
504 * \note Either Q or R can be NULL.
505 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000506int mpi_div_mpi( mpi *Q, mpi *R, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000507
508/**
509 * \brief Division by int: A = Q * b + R
510 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000511 * \param Q Destination MPI for the quotient
512 * \param R Destination MPI for the rest value
513 * \param A Left-hand MPI
514 * \param b Integer to divide by
515 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000516 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000517 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000518 * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if b == 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000519 *
520 * \note Either Q or R can be NULL.
521 */
Paul Bakkera755ca12011-04-24 09:11:17 +0000522int mpi_div_int( mpi *Q, mpi *R, const mpi *A, t_sint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000523
524/**
525 * \brief Modulo: R = A mod B
526 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000527 * \param R Destination MPI for the rest value
528 * \param A Left-hand MPI
529 * \param B Right-hand MPI
530 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000531 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000532 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
Paul Bakkerce40a6d2009-06-23 19:46:08 +0000533 * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if B == 0,
534 * POLARSSL_ERR_MPI_NEGATIVE_VALUE if B < 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000535 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000536int mpi_mod_mpi( mpi *R, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000537
538/**
539 * \brief Modulo: r = A mod b
540 *
Paul Bakkera755ca12011-04-24 09:11:17 +0000541 * \param r Destination t_uint
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000542 * \param A Left-hand MPI
543 * \param b Integer to divide by
544 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000545 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000546 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
Paul Bakkerce40a6d2009-06-23 19:46:08 +0000547 * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if b == 0,
548 * POLARSSL_ERR_MPI_NEGATIVE_VALUE if b < 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000549 */
Paul Bakkera755ca12011-04-24 09:11:17 +0000550int mpi_mod_int( t_uint *r, const mpi *A, t_sint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000551
552/**
553 * \brief Sliding-window exponentiation: X = A^E mod N
554 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000555 * \param X Destination MPI
556 * \param A Left-hand MPI
557 * \param E Exponent MPI
558 * \param N Modular MPI
559 * \param _RR Speed-up MPI used for recalculations
560 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000561 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000562 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
Paul Bakkerf6198c12012-05-16 08:02:29 +0000563 * POLARSSL_ERR_MPI_BAD_INPUT_DATA if N is negative or even or if
564 * E is negative
Paul Bakker5121ce52009-01-03 21:22:43 +0000565 *
566 * \note _RR is used to avoid re-computing R*R mod N across
567 * multiple calls, which speeds up things a bit. It can
568 * be set to NULL if the extra performance is unneeded.
569 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000570int mpi_exp_mod( mpi *X, const mpi *A, const mpi *E, const mpi *N, mpi *_RR );
Paul Bakker5121ce52009-01-03 21:22:43 +0000571
572/**
Paul Bakker287781a2011-03-26 13:18:49 +0000573 * \brief Fill an MPI X with size bytes of random
574 *
575 * \param X Destination MPI
576 * \param size Size in bytes
577 * \param f_rng RNG function
578 * \param p_rng RNG parameter
579 *
580 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000581 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker287781a2011-03-26 13:18:49 +0000582 */
Paul Bakkera3d195c2011-11-27 21:07:34 +0000583int mpi_fill_random( mpi *X, size_t size,
584 int (*f_rng)(void *, unsigned char *, size_t),
585 void *p_rng );
Paul Bakker287781a2011-03-26 13:18:49 +0000586
587/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000588 * \brief Greatest common divisor: G = gcd(A, B)
589 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000590 * \param G Destination MPI
591 * \param A Left-hand MPI
592 * \param B Right-hand MPI
593 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000594 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000595 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000596 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000597int mpi_gcd( mpi *G, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000598
599/**
600 * \brief Modular inverse: X = A^-1 mod N
601 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000602 * \param X Destination MPI
603 * \param A Left-hand MPI
604 * \param N Right-hand MPI
605 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000606 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000607 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000608 * POLARSSL_ERR_MPI_BAD_INPUT_DATA if N is negative or nil
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000609 POLARSSL_ERR_MPI_NOT_ACCEPTABLE if A has no inverse mod N
Paul Bakker5121ce52009-01-03 21:22:43 +0000610 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000611int mpi_inv_mod( mpi *X, const mpi *A, const mpi *N );
Paul Bakker5121ce52009-01-03 21:22:43 +0000612
613/**
614 * \brief Miller-Rabin primality test
615 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000616 * \param X MPI to check
617 * \param f_rng RNG function
618 * \param p_rng RNG parameter
619 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000620 * \return 0 if successful (probably prime),
Paul Bakker69e095c2011-12-10 21:55:01 +0000621 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000622 * POLARSSL_ERR_MPI_NOT_ACCEPTABLE if X is not prime
Paul Bakker5121ce52009-01-03 21:22:43 +0000623 */
Paul Bakkera3d195c2011-11-27 21:07:34 +0000624int mpi_is_prime( mpi *X,
625 int (*f_rng)(void *, unsigned char *, size_t),
626 void *p_rng );
Paul Bakker5121ce52009-01-03 21:22:43 +0000627
628/**
629 * \brief Prime number generation
630 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000631 * \param X Destination MPI
Paul Bakkerfe3256e2011-11-25 12:11:43 +0000632 * \param nbits Required size of X in bits ( 3 <= nbits <= POLARSSL_MPI_MAX_BITS )
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000633 * \param dh_flag If 1, then (X-1)/2 will be prime too
Paul Bakker5121ce52009-01-03 21:22:43 +0000634 * \param f_rng RNG function
635 * \param p_rng RNG parameter
636 *
637 * \return 0 if successful (probably prime),
Paul Bakker69e095c2011-12-10 21:55:01 +0000638 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000639 * POLARSSL_ERR_MPI_BAD_INPUT_DATA if nbits is < 3
Paul Bakker5121ce52009-01-03 21:22:43 +0000640 */
Paul Bakker23986e52011-04-24 08:57:21 +0000641int mpi_gen_prime( mpi *X, size_t nbits, int dh_flag,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000642 int (*f_rng)(void *, unsigned char *, size_t),
643 void *p_rng );
Paul Bakker5121ce52009-01-03 21:22:43 +0000644
645/**
646 * \brief Checkup routine
647 *
648 * \return 0 if successful, or 1 if the test failed
649 */
650int mpi_self_test( int verbose );
651
652#ifdef __cplusplus
653}
654#endif
655
656#endif /* bignum.h */