blob: 6e2afac7299b69e50c9afdf7ce24ceb7b08c6875 [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 Bakker9bcf16c2013-06-24 19:31:17 +02006 * Copyright (C) 2006-2013, 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 Bakkerfa6a6202013-10-28 18:48:30 +010035#if defined(_MSC_VER) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker5c2364c2012-10-01 14:41:15 +000036#include <basetsd.h>
Paul Bakker4a2bd0d2012-11-02 11:06:08 +000037#if (_MSC_VER <= 1200)
38typedef signed short int16_t;
39typedef unsigned short uint16_t;
40#else
Paul Bakker5c2364c2012-10-01 14:41:15 +000041typedef INT16 int16_t;
42typedef UINT16 uint16_t;
Paul Bakker4a2bd0d2012-11-02 11:06:08 +000043#endif
Paul Bakker9ef6e2b2012-10-01 20:57:38 +000044typedef INT32 int32_t;
Paul Bakker8ea31ff2013-02-27 15:02:50 +010045typedef INT64 int64_t;
Paul Bakker5c2364c2012-10-01 14:41:15 +000046typedef UINT32 uint32_t;
47typedef UINT64 uint64_t;
48#else
49#include <inttypes.h>
50#endif
51
Paul Bakker9d781402011-05-09 16:17:09 +000052#define POLARSSL_ERR_MPI_FILE_IO_ERROR -0x0002 /**< An error occurred while reading from or writing to a file. */
53#define POLARSSL_ERR_MPI_BAD_INPUT_DATA -0x0004 /**< Bad input parameters to function. */
54#define POLARSSL_ERR_MPI_INVALID_CHARACTER -0x0006 /**< There is an invalid character in the digit string. */
Paul Bakker69e095c2011-12-10 21:55:01 +000055#define POLARSSL_ERR_MPI_BUFFER_TOO_SMALL -0x0008 /**< The buffer is too small to write to. */
Paul Bakker9d781402011-05-09 16:17:09 +000056#define POLARSSL_ERR_MPI_NEGATIVE_VALUE -0x000A /**< The input arguments are negative or result in illegal output. */
57#define POLARSSL_ERR_MPI_DIVISION_BY_ZERO -0x000C /**< The input argument for division is zero, which is not allowed. */
58#define POLARSSL_ERR_MPI_NOT_ACCEPTABLE -0x000E /**< The input arguments are not acceptable. */
Paul Bakker69e095c2011-12-10 21:55:01 +000059#define POLARSSL_ERR_MPI_MALLOC_FAILED -0x0010 /**< Memory allocation failed. */
Paul Bakker5121ce52009-01-03 21:22:43 +000060
61#define MPI_CHK(f) if( ( ret = f ) != 0 ) goto cleanup
62
63/*
Paul Bakkerf9688572011-05-05 10:00:45 +000064 * Maximum size MPIs are allowed to grow to in number of limbs.
65 */
66#define POLARSSL_MPI_MAX_LIMBS 10000
67
Paul Bakker9bcf16c2013-06-24 19:31:17 +020068#if !defined(POLARSSL_CONFIG_OPTIONS)
Paul Bakkerf9688572011-05-05 10:00:45 +000069/*
Paul Bakkerb6d5f082011-11-25 11:52:11 +000070 * Maximum window size used for modular exponentiation. Default: 6
71 * Minimum value: 1. Maximum value: 6.
72 *
73 * Result is an array of ( 2 << POLARSSL_MPI_WINDOW_SIZE ) MPIs used
74 * for the sliding window calculation. (So 64 by default)
75 *
76 * Reduction in size, reduces speed.
77 */
78#define POLARSSL_MPI_WINDOW_SIZE 6 /**< Maximum windows size used. */
79
80/*
Paul Bakkerfe3256e2011-11-25 12:11:43 +000081 * Maximum size of MPIs allowed in bits and bytes for user-MPIs.
Paul Bakkerf626e1d2013-01-21 12:10:00 +010082 * ( Default: 512 bytes => 4096 bits, Maximum tested: 2048 bytes => 16384 bits )
Paul Bakkerfe3256e2011-11-25 12:11:43 +000083 *
84 * Note: Calculations can results temporarily in larger MPIs. So the number
85 * of limbs required (POLARSSL_MPI_MAX_LIMBS) is higher.
86 */
87#define POLARSSL_MPI_MAX_SIZE 512 /**< Maximum number of bytes for usable MPIs. */
Paul Bakker9bcf16c2013-06-24 19:31:17 +020088
89#endif /* !POLARSSL_CONFIG_OPTIONS */
90
Paul Bakkerfe3256e2011-11-25 12:11:43 +000091#define POLARSSL_MPI_MAX_BITS ( 8 * POLARSSL_MPI_MAX_SIZE ) /**< Maximum number of bits for usable MPIs. */
92
93/*
Paul Bakker5531c6d2012-09-26 19:20:46 +000094 * When reading from files with mpi_read_file() and writing to files with
95 * mpi_write_file() the buffer should have space
Paul Bakkercb37aa52011-11-30 16:00:20 +000096 * for a (short) label, the MPI (in the provided radix), the newline
97 * characters and the '\0'.
98 *
99 * By default we assume at least a 10 char label, a minimum radix of 10
100 * (decimal) and a maximum of 4096 bit numbers (1234 decimal chars).
Paul Bakkerf9183102012-09-27 20:42:35 +0000101 * Autosized at compile time for at least a 10 char label, a minimum radix
102 * of 10 (decimal) for a number of POLARSSL_MPI_MAX_BITS size.
103 *
104 * This used to be statically sized to 1250 for a maximum of 4096 bit
105 * numbers (1234 decimal chars).
106 *
107 * Calculate using the formula:
108 * POLARSSL_MPI_RW_BUFFER_SIZE = ceil(POLARSSL_MPI_MAX_BITS / ln(10) * ln(2)) +
109 * LabelSize + 6
Paul Bakkercb37aa52011-11-30 16:00:20 +0000110 */
Paul Bakkerf9183102012-09-27 20:42:35 +0000111#define POLARSSL_MPI_MAX_BITS_SCALE100 ( 100 * POLARSSL_MPI_MAX_BITS )
112#define LN_2_DIV_LN_10_SCALE100 332
113#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 +0000114
115/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000116 * Define the base integer type, architecture-wise
117 */
Paul Bakker40e46942009-01-03 21:51:57 +0000118#if defined(POLARSSL_HAVE_INT8)
Paul Bakkera755ca12011-04-24 09:11:17 +0000119typedef signed char t_sint;
120typedef unsigned char t_uint;
Paul Bakker5c2364c2012-10-01 14:41:15 +0000121typedef uint16_t t_udbl;
Paul Bakker62261d62012-10-02 12:19:31 +0000122#define POLARSSL_HAVE_UDBL
Paul Bakker5121ce52009-01-03 21:22:43 +0000123#else
Paul Bakker40e46942009-01-03 21:51:57 +0000124#if defined(POLARSSL_HAVE_INT16)
Paul Bakker5c2364c2012-10-01 14:41:15 +0000125typedef int16_t t_sint;
126typedef uint16_t t_uint;
127typedef uint32_t t_udbl;
Paul Bakker62261d62012-10-02 12:19:31 +0000128#define POLARSSL_HAVE_UDBL
Paul Bakker5121ce52009-01-03 21:22:43 +0000129#else
Paul Bakker9f2018e2013-02-27 15:01:34 +0100130 #if ( defined(_MSC_VER) && defined(_M_AMD64) )
Manuel Pégourié-Gonnard5779cbe2013-10-23 20:17:00 +0200131 #define POLARSSL_HAVE_INT64
Paul Bakker62261d62012-10-02 12:19:31 +0000132 typedef int64_t t_sint;
133 typedef uint64_t t_uint;
134 #else
135 #if ( defined(__GNUC__) && ( \
136 defined(__amd64__) || defined(__x86_64__) || \
137 defined(__ppc64__) || defined(__powerpc64__) || \
138 defined(__ia64__) || defined(__alpha__) || \
139 (defined(__sparc__) && defined(__arch64__)) || \
140 defined(__s390x__) ) )
Manuel Pégourié-Gonnard5779cbe2013-10-23 20:17:00 +0200141 #define POLARSSL_HAVE_INT64
Paul Bakker62261d62012-10-02 12:19:31 +0000142 typedef int64_t t_sint;
143 typedef uint64_t t_uint;
144 typedef unsigned int t_udbl __attribute__((mode(TI)));
145 #define POLARSSL_HAVE_UDBL
146 #else
Manuel Pégourié-Gonnarda47e7052013-10-21 17:51:45 +0200147 #define POLARSSL_HAVE_INT32
Paul Bakker62261d62012-10-02 12:19:31 +0000148 typedef int32_t t_sint;
149 typedef uint32_t t_uint;
150 #if ( defined(_MSC_VER) && defined(_M_IX86) )
151 typedef uint64_t t_udbl;
152 #define POLARSSL_HAVE_UDBL
153 #else
154 #if defined( POLARSSL_HAVE_LONGLONG )
155 typedef unsigned long long t_udbl;
156 #define POLARSSL_HAVE_UDBL
157 #endif
158 #endif
159 #endif
Paul Bakker5c2364c2012-10-01 14:41:15 +0000160 #endif
161#endif /* POLARSSL_HAVE_INT16 */
162#endif /* POLARSSL_HAVE_INT8 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000163
Paul Bakker407a0da2013-06-27 14:29:21 +0200164#ifdef __cplusplus
165extern "C" {
166#endif
167
Paul Bakker5121ce52009-01-03 21:22:43 +0000168/**
169 * \brief MPI structure
170 */
171typedef struct
172{
173 int s; /*!< integer sign */
Paul Bakker23986e52011-04-24 08:57:21 +0000174 size_t n; /*!< total # of limbs */
Paul Bakkera755ca12011-04-24 09:11:17 +0000175 t_uint *p; /*!< pointer to limbs */
Paul Bakker5121ce52009-01-03 21:22:43 +0000176}
177mpi;
178
Paul Bakker5121ce52009-01-03 21:22:43 +0000179/**
Paul Bakker6c591fa2011-05-05 11:49:20 +0000180 * \brief Initialize one MPI
181 *
182 * \param X One MPI to initialize.
Paul Bakker5121ce52009-01-03 21:22:43 +0000183 */
Paul Bakker6c591fa2011-05-05 11:49:20 +0000184void mpi_init( mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000185
186/**
Paul Bakker6c591fa2011-05-05 11:49:20 +0000187 * \brief Unallocate one MPI
188 *
189 * \param X One MPI to unallocate.
Paul Bakker5121ce52009-01-03 21:22:43 +0000190 */
Paul Bakker6c591fa2011-05-05 11:49:20 +0000191void mpi_free( mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000192
193/**
194 * \brief Enlarge to the specified number of limbs
195 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000196 * \param X MPI to grow
197 * \param nblimbs The target number of limbs
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 Bakker23986e52011-04-24 08:57:21 +0000202int mpi_grow( mpi *X, size_t nblimbs );
Paul Bakker5121ce52009-01-03 21:22:43 +0000203
204/**
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100205 * \brief Resize down, keeping at least the specified number of limbs
206 *
207 * \param X MPI to shrink
208 * \param nblimbs The minimum number of limbs to keep
209 *
210 * \return 0 if successful,
211 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
212 */
213int mpi_shrink( mpi *X, size_t nblimbs );
214
215/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000216 * \brief Copy the contents of Y into X
217 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000218 * \param X Destination MPI
219 * \param Y Source MPI
220 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000221 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000222 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000223 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000224int mpi_copy( mpi *X, const mpi *Y );
Paul Bakker5121ce52009-01-03 21:22:43 +0000225
226/**
227 * \brief Swap the contents of X and Y
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000228 *
229 * \param X First MPI value
230 * \param Y Second MPI value
Paul Bakker5121ce52009-01-03 21:22:43 +0000231 */
232void mpi_swap( mpi *X, mpi *Y );
233
234/**
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100235 * \brief Safe conditional assignement X = Y if assign is 1
236 *
237 * \param X MPI to conditionally assign to
238 * \param Y Value to be assigned
239 * \param assign 1: perform the assignment, 0: leave X untouched
240 *
241 * \return 0 if successful,
242 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
243 * POLARSSL_ERR_MPI_BAD_INPUT_DATA if assing is not 0 or 1
244 *
245 * \note This function is equivalent to
246 * if( assign ) mpi_copy( X, Y );
247 * except that it avoids leaking any information about whether
248 * the assignment was done or not (the above code may leak
249 * information through branch prediction analysis).
250 */
251int mpi_safe_cond_assign( mpi *X, mpi *Y, unsigned char assign );
252
253/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000254 * \brief Set value from integer
255 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000256 * \param X MPI to set
257 * \param z Value to use
258 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000259 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000260 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000261 */
Paul Bakkera755ca12011-04-24 09:11:17 +0000262int mpi_lset( mpi *X, t_sint z );
Paul Bakker5121ce52009-01-03 21:22:43 +0000263
Paul Bakker9a736322012-11-14 12:39:52 +0000264/**
Paul Bakker2f5947e2011-05-18 15:47:11 +0000265 * \brief Get a specific bit from X
266 *
267 * \param X MPI to use
268 * \param pos Zero-based index of the bit in X
269 *
270 * \return Either a 0 or a 1
271 */
Paul Bakker6b906e52012-05-08 12:01:43 +0000272int mpi_get_bit( const mpi *X, size_t pos );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000273
Paul Bakker9a736322012-11-14 12:39:52 +0000274/**
Paul Bakker2f5947e2011-05-18 15:47:11 +0000275 * \brief Set a bit of X to a specific value of 0 or 1
276 *
277 * \note Will grow X if necessary to set a bit to 1 in a not yet
278 * existing limb. Will not grow if bit should be set to 0
279 *
280 * \param X MPI to use
281 * \param pos Zero-based index of the bit in X
282 * \param val The value to set the bit to (0 or 1)
283 *
284 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000285 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
Paul Bakker2f5947e2011-05-18 15:47:11 +0000286 * POLARSSL_ERR_MPI_BAD_INPUT_DATA if val is not 0 or 1
287 */
288int mpi_set_bit( mpi *X, size_t pos, unsigned char val );
289
Paul Bakker5121ce52009-01-03 21:22:43 +0000290/**
Paul Bakker6b906e52012-05-08 12:01:43 +0000291 * \brief Return the number of zero-bits before the least significant
292 * '1' bit
293 *
294 * Note: Thus also the zero-based index of the least significant '1' bit
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000295 *
296 * \param X MPI to use
Paul Bakker5121ce52009-01-03 21:22:43 +0000297 */
Paul Bakker23986e52011-04-24 08:57:21 +0000298size_t mpi_lsb( const mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000299
300/**
Paul Bakker6b906e52012-05-08 12:01:43 +0000301 * \brief Return the number of bits up to and including the most
302 * significant '1' bit'
303 *
304 * Note: Thus also the one-based index of the most significant '1' bit
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000305 *
306 * \param X MPI to use
Paul Bakker5121ce52009-01-03 21:22:43 +0000307 */
Paul Bakker23986e52011-04-24 08:57:21 +0000308size_t mpi_msb( const mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000309
310/**
311 * \brief Return the total size in bytes
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000312 *
313 * \param X MPI to use
Paul Bakker5121ce52009-01-03 21:22:43 +0000314 */
Paul Bakker23986e52011-04-24 08:57:21 +0000315size_t mpi_size( const mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000316
317/**
318 * \brief Import from an ASCII string
319 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000320 * \param X Destination MPI
321 * \param radix Input numeric base
322 * \param s Null-terminated string buffer
Paul Bakker5121ce52009-01-03 21:22:43 +0000323 *
Paul Bakkercb37aa52011-11-30 16:00:20 +0000324 * \return 0 if successful, or a POLARSSL_ERR_MPI_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000325 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000326int mpi_read_string( mpi *X, int radix, const char *s );
Paul Bakker5121ce52009-01-03 21:22:43 +0000327
328/**
329 * \brief Export into an ASCII string
330 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000331 * \param X Source MPI
332 * \param radix Output numeric base
333 * \param s String buffer
334 * \param slen String buffer size
Paul Bakker5121ce52009-01-03 21:22:43 +0000335 *
Paul Bakkercb37aa52011-11-30 16:00:20 +0000336 * \return 0 if successful, or a POLARSSL_ERR_MPI_XXX error code.
Paul Bakkerff60ee62010-03-16 21:09:09 +0000337 * *slen is always updated to reflect the amount
338 * of data that has (or would have) been written.
Paul Bakker5121ce52009-01-03 21:22:43 +0000339 *
340 * \note Call this function with *slen = 0 to obtain the
341 * minimum required buffer size in *slen.
342 */
Paul Bakker23986e52011-04-24 08:57:21 +0000343int mpi_write_string( const mpi *X, int radix, char *s, size_t *slen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000344
Paul Bakker2b6af2f2012-10-23 11:08:02 +0000345#if defined(POLARSSL_FS_IO)
Paul Bakker5121ce52009-01-03 21:22:43 +0000346/**
347 * \brief Read X from an opened file
348 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000349 * \param X Destination MPI
350 * \param radix Input numeric base
351 * \param fin Input file handle
Paul Bakker5121ce52009-01-03 21:22:43 +0000352 *
Paul Bakkercb37aa52011-11-30 16:00:20 +0000353 * \return 0 if successful, POLARSSL_ERR_MPI_BUFFER_TOO_SMALL if
354 * the file read buffer is too small or a
355 * POLARSSL_ERR_MPI_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000356 */
357int mpi_read_file( mpi *X, int radix, FILE *fin );
358
359/**
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000360 * \brief Write X into an opened file, or stdout if fout is NULL
Paul Bakker5121ce52009-01-03 21:22:43 +0000361 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000362 * \param p Prefix, can be NULL
363 * \param X Source MPI
364 * \param radix Output numeric base
365 * \param fout Output file handle (can be NULL)
Paul Bakker5121ce52009-01-03 21:22:43 +0000366 *
Paul Bakkercb37aa52011-11-30 16:00:20 +0000367 * \return 0 if successful, or a POLARSSL_ERR_MPI_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000368 *
369 * \note Set fout == NULL to print X on the console.
370 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000371int mpi_write_file( const char *p, const mpi *X, int radix, FILE *fout );
Paul Bakker2b6af2f2012-10-23 11:08:02 +0000372#endif /* POLARSSL_FS_IO */
Paul Bakker5121ce52009-01-03 21:22:43 +0000373
374/**
375 * \brief Import X from unsigned binary data, big endian
376 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000377 * \param X Destination MPI
378 * \param buf Input buffer
379 * \param buflen Input buffer size
Paul Bakker5121ce52009-01-03 21:22:43 +0000380 *
381 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000382 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000383 */
Paul Bakker23986e52011-04-24 08:57:21 +0000384int mpi_read_binary( mpi *X, const unsigned char *buf, size_t buflen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000385
386/**
387 * \brief Export X into unsigned binary data, big endian
388 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000389 * \param X Source MPI
390 * \param buf Output buffer
391 * \param buflen Output buffer size
Paul Bakker5121ce52009-01-03 21:22:43 +0000392 *
393 * \return 0 if successful,
Paul Bakker40e46942009-01-03 21:51:57 +0000394 * POLARSSL_ERR_MPI_BUFFER_TOO_SMALL if buf isn't large enough
Paul Bakker5121ce52009-01-03 21:22:43 +0000395 */
Paul Bakker23986e52011-04-24 08:57:21 +0000396int mpi_write_binary( const mpi *X, unsigned char *buf, size_t buflen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000397
398/**
399 * \brief Left-shift: X <<= count
400 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000401 * \param X MPI to shift
402 * \param count Amount to shift
403 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000404 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000405 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000406 */
Paul Bakker23986e52011-04-24 08:57:21 +0000407int mpi_shift_l( mpi *X, size_t count );
Paul Bakker5121ce52009-01-03 21:22:43 +0000408
409/**
410 * \brief Right-shift: X >>= count
411 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000412 * \param X MPI to shift
413 * \param count Amount to shift
414 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000415 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000416 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000417 */
Paul Bakker23986e52011-04-24 08:57:21 +0000418int mpi_shift_r( mpi *X, size_t count );
Paul Bakker5121ce52009-01-03 21:22:43 +0000419
420/**
421 * \brief Compare unsigned values
422 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000423 * \param X Left-hand MPI
424 * \param Y Right-hand MPI
425 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000426 * \return 1 if |X| is greater than |Y|,
427 * -1 if |X| is lesser than |Y| or
428 * 0 if |X| is equal to |Y|
429 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000430int mpi_cmp_abs( const mpi *X, const mpi *Y );
Paul Bakker5121ce52009-01-03 21:22:43 +0000431
432/**
433 * \brief Compare signed values
434 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000435 * \param X Left-hand MPI
436 * \param Y Right-hand MPI
437 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000438 * \return 1 if X is greater than Y,
439 * -1 if X is lesser than Y or
440 * 0 if X is equal to Y
441 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000442int mpi_cmp_mpi( const mpi *X, const mpi *Y );
Paul Bakker5121ce52009-01-03 21:22:43 +0000443
444/**
445 * \brief Compare signed values
446 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000447 * \param X Left-hand MPI
448 * \param z The integer value to compare to
449 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000450 * \return 1 if X is greater than z,
451 * -1 if X is lesser than z or
452 * 0 if X is equal to z
453 */
Paul Bakkera755ca12011-04-24 09:11:17 +0000454int mpi_cmp_int( const mpi *X, t_sint z );
Paul Bakker5121ce52009-01-03 21:22:43 +0000455
456/**
457 * \brief Unsigned addition: X = |A| + |B|
458 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000459 * \param X Destination MPI
460 * \param A Left-hand MPI
461 * \param B Right-hand MPI
462 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000463 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000464 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000465 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000466int mpi_add_abs( mpi *X, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000467
468/**
Paul Bakker60b1d102013-10-29 10:02:51 +0100469 * \brief Unsigned subtraction: X = |A| - |B|
Paul Bakker5121ce52009-01-03 21:22:43 +0000470 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000471 * \param X Destination MPI
472 * \param A Left-hand MPI
473 * \param B Right-hand MPI
474 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000475 * \return 0 if successful,
Paul Bakker40e46942009-01-03 21:51:57 +0000476 * POLARSSL_ERR_MPI_NEGATIVE_VALUE if B is greater than A
Paul Bakker5121ce52009-01-03 21:22:43 +0000477 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000478int mpi_sub_abs( mpi *X, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000479
480/**
481 * \brief Signed addition: X = A + B
482 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000483 * \param X Destination MPI
484 * \param A Left-hand MPI
485 * \param B Right-hand MPI
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 Bakkerff60ee62010-03-16 21:09:09 +0000490int mpi_add_mpi( mpi *X, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000491
492/**
Paul Bakker60b1d102013-10-29 10:02:51 +0100493 * \brief Signed subtraction: X = A - B
Paul Bakker5121ce52009-01-03 21:22:43 +0000494 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000495 * \param X Destination MPI
496 * \param A Left-hand MPI
497 * \param B Right-hand MPI
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 Bakker5121ce52009-01-03 21:22:43 +0000501 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000502int mpi_sub_mpi( mpi *X, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000503
504/**
505 * \brief Signed addition: X = A + b
506 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000507 * \param X Destination MPI
508 * \param A Left-hand MPI
509 * \param b The integer value to add
510 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000511 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000512 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000513 */
Paul Bakkera755ca12011-04-24 09:11:17 +0000514int mpi_add_int( mpi *X, const mpi *A, t_sint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000515
516/**
Paul Bakker60b1d102013-10-29 10:02:51 +0100517 * \brief Signed subtraction: X = A - b
Paul Bakker5121ce52009-01-03 21:22:43 +0000518 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000519 * \param X Destination MPI
520 * \param A Left-hand MPI
521 * \param b The integer value to subtract
522 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000523 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000524 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000525 */
Paul Bakkera755ca12011-04-24 09:11:17 +0000526int mpi_sub_int( mpi *X, const mpi *A, t_sint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000527
528/**
529 * \brief Baseline multiplication: X = A * B
530 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000531 * \param X Destination MPI
532 * \param A Left-hand MPI
533 * \param B Right-hand MPI
534 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000535 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000536 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000537 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000538int mpi_mul_mpi( mpi *X, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000539
540/**
541 * \brief Baseline multiplication: X = A * b
Paul Bakkerce40a6d2009-06-23 19:46:08 +0000542 * Note: b is an unsigned integer type, thus
543 * Negative values of b are ignored.
Paul Bakker5121ce52009-01-03 21:22:43 +0000544 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000545 * \param X Destination MPI
546 * \param A Left-hand MPI
547 * \param b The integer value to multiply with
548 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000549 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000550 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000551 */
Paul Bakkera755ca12011-04-24 09:11:17 +0000552int mpi_mul_int( mpi *X, const mpi *A, t_sint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000553
554/**
555 * \brief Division by mpi: A = Q * B + R
556 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000557 * \param Q Destination MPI for the quotient
558 * \param R Destination MPI for the rest value
559 * \param A Left-hand MPI
560 * \param B Right-hand MPI
561 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000562 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000563 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000564 * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if B == 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000565 *
566 * \note Either Q or R can be NULL.
567 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000568int mpi_div_mpi( mpi *Q, mpi *R, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000569
570/**
571 * \brief Division by int: A = Q * b + R
572 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000573 * \param Q Destination MPI for the quotient
574 * \param R Destination MPI for the rest value
575 * \param A Left-hand MPI
576 * \param b Integer to divide by
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_DIVISION_BY_ZERO if b == 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000581 *
582 * \note Either Q or R can be NULL.
583 */
Paul Bakkera755ca12011-04-24 09:11:17 +0000584int mpi_div_int( mpi *Q, mpi *R, const mpi *A, t_sint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000585
586/**
587 * \brief Modulo: R = A mod B
588 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000589 * \param R Destination MPI for the rest value
590 * \param A Left-hand MPI
591 * \param B Right-hand MPI
592 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000593 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000594 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
Paul Bakkerce40a6d2009-06-23 19:46:08 +0000595 * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if B == 0,
596 * POLARSSL_ERR_MPI_NEGATIVE_VALUE if B < 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000597 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000598int mpi_mod_mpi( mpi *R, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000599
600/**
601 * \brief Modulo: r = A mod b
602 *
Paul Bakkera755ca12011-04-24 09:11:17 +0000603 * \param r Destination t_uint
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000604 * \param A Left-hand MPI
605 * \param b Integer to divide by
606 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000607 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000608 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
Paul Bakkerce40a6d2009-06-23 19:46:08 +0000609 * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if b == 0,
610 * POLARSSL_ERR_MPI_NEGATIVE_VALUE if b < 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000611 */
Paul Bakkera755ca12011-04-24 09:11:17 +0000612int mpi_mod_int( t_uint *r, const mpi *A, t_sint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000613
614/**
615 * \brief Sliding-window exponentiation: X = A^E mod N
616 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000617 * \param X Destination MPI
618 * \param A Left-hand MPI
619 * \param E Exponent MPI
620 * \param N Modular MPI
621 * \param _RR Speed-up MPI used for recalculations
622 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000623 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000624 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
Paul Bakkerf6198c12012-05-16 08:02:29 +0000625 * POLARSSL_ERR_MPI_BAD_INPUT_DATA if N is negative or even or if
626 * E is negative
Paul Bakker5121ce52009-01-03 21:22:43 +0000627 *
628 * \note _RR is used to avoid re-computing R*R mod N across
629 * multiple calls, which speeds up things a bit. It can
630 * be set to NULL if the extra performance is unneeded.
631 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000632int mpi_exp_mod( mpi *X, const mpi *A, const mpi *E, const mpi *N, mpi *_RR );
Paul Bakker5121ce52009-01-03 21:22:43 +0000633
634/**
Paul Bakker287781a2011-03-26 13:18:49 +0000635 * \brief Fill an MPI X with size bytes of random
636 *
637 * \param X Destination MPI
638 * \param size Size in bytes
639 * \param f_rng RNG function
640 * \param p_rng RNG parameter
641 *
642 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000643 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker287781a2011-03-26 13:18:49 +0000644 */
Paul Bakkera3d195c2011-11-27 21:07:34 +0000645int mpi_fill_random( mpi *X, size_t size,
646 int (*f_rng)(void *, unsigned char *, size_t),
647 void *p_rng );
Paul Bakker287781a2011-03-26 13:18:49 +0000648
649/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000650 * \brief Greatest common divisor: G = gcd(A, B)
651 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000652 * \param G Destination MPI
653 * \param A Left-hand MPI
654 * \param B Right-hand MPI
655 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000656 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000657 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000658 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000659int mpi_gcd( mpi *G, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000660
661/**
662 * \brief Modular inverse: X = A^-1 mod N
663 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000664 * \param X Destination MPI
665 * \param A Left-hand MPI
666 * \param N Right-hand MPI
667 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000668 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000669 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000670 * POLARSSL_ERR_MPI_BAD_INPUT_DATA if N is negative or nil
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000671 POLARSSL_ERR_MPI_NOT_ACCEPTABLE if A has no inverse mod N
Paul Bakker5121ce52009-01-03 21:22:43 +0000672 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000673int mpi_inv_mod( mpi *X, const mpi *A, const mpi *N );
Paul Bakker5121ce52009-01-03 21:22:43 +0000674
675/**
676 * \brief Miller-Rabin primality test
677 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000678 * \param X MPI to check
679 * \param f_rng RNG function
680 * \param p_rng RNG parameter
681 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000682 * \return 0 if successful (probably prime),
Paul Bakker69e095c2011-12-10 21:55:01 +0000683 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000684 * POLARSSL_ERR_MPI_NOT_ACCEPTABLE if X is not prime
Paul Bakker5121ce52009-01-03 21:22:43 +0000685 */
Paul Bakkera3d195c2011-11-27 21:07:34 +0000686int mpi_is_prime( mpi *X,
687 int (*f_rng)(void *, unsigned char *, size_t),
688 void *p_rng );
Paul Bakker5121ce52009-01-03 21:22:43 +0000689
690/**
691 * \brief Prime number generation
692 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000693 * \param X Destination MPI
Paul Bakkerfe3256e2011-11-25 12:11:43 +0000694 * \param nbits Required size of X in bits ( 3 <= nbits <= POLARSSL_MPI_MAX_BITS )
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000695 * \param dh_flag If 1, then (X-1)/2 will be prime too
Paul Bakker5121ce52009-01-03 21:22:43 +0000696 * \param f_rng RNG function
697 * \param p_rng RNG parameter
698 *
699 * \return 0 if successful (probably prime),
Paul Bakker69e095c2011-12-10 21:55:01 +0000700 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000701 * POLARSSL_ERR_MPI_BAD_INPUT_DATA if nbits is < 3
Paul Bakker5121ce52009-01-03 21:22:43 +0000702 */
Paul Bakker23986e52011-04-24 08:57:21 +0000703int mpi_gen_prime( mpi *X, size_t nbits, int dh_flag,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000704 int (*f_rng)(void *, unsigned char *, size_t),
705 void *p_rng );
Paul Bakker5121ce52009-01-03 21:22:43 +0000706
707/**
708 * \brief Checkup routine
709 *
710 * \return 0 if successful, or 1 if the test failed
711 */
712int mpi_self_test( int verbose );
713
714#ifdef __cplusplus
715}
716#endif
717
718#endif /* bignum.h */