blob: 9cf61a3fc3a4ea56babd7596bbfde7319783f96e [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;
Paul Bakker9ef6e2b2012-10-01 20:57:38 +000039typedef INT32 int32_t;
Paul Bakker5c2364c2012-10-01 14:41:15 +000040typedef UINT32 uint32_t;
41typedef UINT64 uint64_t;
42#else
43#include <inttypes.h>
44#endif
45
Paul Bakker9d781402011-05-09 16:17:09 +000046#define POLARSSL_ERR_MPI_FILE_IO_ERROR -0x0002 /**< An error occurred while reading from or writing to a file. */
47#define POLARSSL_ERR_MPI_BAD_INPUT_DATA -0x0004 /**< Bad input parameters to function. */
48#define POLARSSL_ERR_MPI_INVALID_CHARACTER -0x0006 /**< There is an invalid character in the digit string. */
Paul Bakker69e095c2011-12-10 21:55:01 +000049#define POLARSSL_ERR_MPI_BUFFER_TOO_SMALL -0x0008 /**< The buffer is too small to write to. */
Paul Bakker9d781402011-05-09 16:17:09 +000050#define POLARSSL_ERR_MPI_NEGATIVE_VALUE -0x000A /**< The input arguments are negative or result in illegal output. */
51#define POLARSSL_ERR_MPI_DIVISION_BY_ZERO -0x000C /**< The input argument for division is zero, which is not allowed. */
52#define POLARSSL_ERR_MPI_NOT_ACCEPTABLE -0x000E /**< The input arguments are not acceptable. */
Paul Bakker69e095c2011-12-10 21:55:01 +000053#define POLARSSL_ERR_MPI_MALLOC_FAILED -0x0010 /**< Memory allocation failed. */
Paul Bakker5121ce52009-01-03 21:22:43 +000054
55#define MPI_CHK(f) if( ( ret = f ) != 0 ) goto cleanup
56
57/*
Paul Bakkerf9688572011-05-05 10:00:45 +000058 * Maximum size MPIs are allowed to grow to in number of limbs.
59 */
60#define POLARSSL_MPI_MAX_LIMBS 10000
61
62/*
Paul Bakkerb6d5f082011-11-25 11:52:11 +000063 * Maximum window size used for modular exponentiation. Default: 6
64 * Minimum value: 1. Maximum value: 6.
65 *
66 * Result is an array of ( 2 << POLARSSL_MPI_WINDOW_SIZE ) MPIs used
67 * for the sliding window calculation. (So 64 by default)
68 *
69 * Reduction in size, reduces speed.
70 */
71#define POLARSSL_MPI_WINDOW_SIZE 6 /**< Maximum windows size used. */
72
73/*
Paul Bakkerfe3256e2011-11-25 12:11:43 +000074 * Maximum size of MPIs allowed in bits and bytes for user-MPIs.
Paul Bakker5552c8c2012-07-05 13:31:54 +000075 * ( Default: 512 bytes => 4096 bits, Maximum: 1024 bytes => 8192 bits )
Paul Bakkerfe3256e2011-11-25 12:11:43 +000076 *
77 * Note: Calculations can results temporarily in larger MPIs. So the number
78 * of limbs required (POLARSSL_MPI_MAX_LIMBS) is higher.
79 */
80#define POLARSSL_MPI_MAX_SIZE 512 /**< Maximum number of bytes for usable MPIs. */
81#define POLARSSL_MPI_MAX_BITS ( 8 * POLARSSL_MPI_MAX_SIZE ) /**< Maximum number of bits for usable MPIs. */
82
83/*
Paul Bakker5531c6d2012-09-26 19:20:46 +000084 * When reading from files with mpi_read_file() and writing to files with
85 * mpi_write_file() the buffer should have space
Paul Bakkercb37aa52011-11-30 16:00:20 +000086 * for a (short) label, the MPI (in the provided radix), the newline
87 * characters and the '\0'.
88 *
89 * By default we assume at least a 10 char label, a minimum radix of 10
90 * (decimal) and a maximum of 4096 bit numbers (1234 decimal chars).
Paul Bakkerf9183102012-09-27 20:42:35 +000091 * Autosized at compile time for at least a 10 char label, a minimum radix
92 * of 10 (decimal) for a number of POLARSSL_MPI_MAX_BITS size.
93 *
94 * This used to be statically sized to 1250 for a maximum of 4096 bit
95 * numbers (1234 decimal chars).
96 *
97 * Calculate using the formula:
98 * POLARSSL_MPI_RW_BUFFER_SIZE = ceil(POLARSSL_MPI_MAX_BITS / ln(10) * ln(2)) +
99 * LabelSize + 6
Paul Bakkercb37aa52011-11-30 16:00:20 +0000100 */
Paul Bakkerf9183102012-09-27 20:42:35 +0000101#define POLARSSL_MPI_MAX_BITS_SCALE100 ( 100 * POLARSSL_MPI_MAX_BITS )
102#define LN_2_DIV_LN_10_SCALE100 332
103#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 +0000104
105/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000106 * Define the base integer type, architecture-wise
107 */
Paul Bakker40e46942009-01-03 21:51:57 +0000108#if defined(POLARSSL_HAVE_INT8)
Paul Bakkera755ca12011-04-24 09:11:17 +0000109typedef signed char t_sint;
110typedef unsigned char t_uint;
Paul Bakker5c2364c2012-10-01 14:41:15 +0000111typedef uint16_t t_udbl;
Paul Bakker5121ce52009-01-03 21:22:43 +0000112#else
Paul Bakker40e46942009-01-03 21:51:57 +0000113#if defined(POLARSSL_HAVE_INT16)
Paul Bakker5c2364c2012-10-01 14:41:15 +0000114typedef int16_t t_sint;
115typedef uint16_t t_uint;
116typedef uint32_t t_udbl;
Paul Bakker5121ce52009-01-03 21:22:43 +0000117#else
Paul Bakker5c2364c2012-10-01 14:41:15 +0000118 typedef int32_t t_sint;
119 typedef uint32_t t_uint;
120 #if ( defined(_MSC_VER) && defined(_M_IX86) ) || \
121 ( defined(__GNUC__) && ( \
Paul Bakkercf0360a2012-01-20 10:08:14 +0000122 defined(__amd64__) || defined(__x86_64__) || \
Paul Bakker5121ce52009-01-03 21:22:43 +0000123 defined(__ppc64__) || defined(__powerpc64__) || \
Paul Bakker44637402011-11-26 09:23:07 +0000124 defined(__ia64__) || defined(__alpha__) || \
125 (defined(__sparc__) && defined(__arch64__)) || \
Paul Bakker5c2364c2012-10-01 14:41:15 +0000126 defined(__s390x__) ) )
127 #define POLARSSL_HAVE_INT64
Paul Bakker5121ce52009-01-03 21:22:43 +0000128 #endif
Paul Bakker5c2364c2012-10-01 14:41:15 +0000129 #if defined(POLARSSL_HAVE_INT64)
130 typedef uint64_t t_udbl;
131 #endif
132#endif /* POLARSSL_HAVE_INT16 */
133#endif /* POLARSSL_HAVE_INT8 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000134
135/**
136 * \brief MPI structure
137 */
138typedef struct
139{
140 int s; /*!< integer sign */
Paul Bakker23986e52011-04-24 08:57:21 +0000141 size_t n; /*!< total # of limbs */
Paul Bakkera755ca12011-04-24 09:11:17 +0000142 t_uint *p; /*!< pointer to limbs */
Paul Bakker5121ce52009-01-03 21:22:43 +0000143}
144mpi;
145
146#ifdef __cplusplus
147extern "C" {
148#endif
149
150/**
Paul Bakker6c591fa2011-05-05 11:49:20 +0000151 * \brief Initialize one MPI
152 *
153 * \param X One MPI to initialize.
Paul Bakker5121ce52009-01-03 21:22:43 +0000154 */
Paul Bakker6c591fa2011-05-05 11:49:20 +0000155void mpi_init( mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000156
157/**
Paul Bakker6c591fa2011-05-05 11:49:20 +0000158 * \brief Unallocate one MPI
159 *
160 * \param X One MPI to unallocate.
Paul Bakker5121ce52009-01-03 21:22:43 +0000161 */
Paul Bakker6c591fa2011-05-05 11:49:20 +0000162void mpi_free( mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000163
164/**
165 * \brief Enlarge to the specified number of limbs
166 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000167 * \param X MPI to grow
168 * \param nblimbs The target number of limbs
169 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000170 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000171 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000172 */
Paul Bakker23986e52011-04-24 08:57:21 +0000173int mpi_grow( mpi *X, size_t nblimbs );
Paul Bakker5121ce52009-01-03 21:22:43 +0000174
175/**
176 * \brief Copy the contents of Y into X
177 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000178 * \param X Destination MPI
179 * \param Y Source MPI
180 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000181 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000182 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000183 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000184int mpi_copy( mpi *X, const mpi *Y );
Paul Bakker5121ce52009-01-03 21:22:43 +0000185
186/**
187 * \brief Swap the contents of X and Y
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000188 *
189 * \param X First MPI value
190 * \param Y Second MPI value
Paul Bakker5121ce52009-01-03 21:22:43 +0000191 */
192void mpi_swap( mpi *X, mpi *Y );
193
194/**
195 * \brief Set value from integer
196 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000197 * \param X MPI to set
198 * \param z Value to use
199 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000200 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000201 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000202 */
Paul Bakkera755ca12011-04-24 09:11:17 +0000203int mpi_lset( mpi *X, t_sint z );
Paul Bakker5121ce52009-01-03 21:22:43 +0000204
Paul Bakker2f5947e2011-05-18 15:47:11 +0000205/*
206 * \brief Get a specific bit from X
207 *
208 * \param X MPI to use
209 * \param pos Zero-based index of the bit in X
210 *
211 * \return Either a 0 or a 1
212 */
Paul Bakker6b906e52012-05-08 12:01:43 +0000213int mpi_get_bit( const mpi *X, size_t pos );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000214
215/*
216 * \brief Set a bit of X to a specific value of 0 or 1
217 *
218 * \note Will grow X if necessary to set a bit to 1 in a not yet
219 * existing limb. Will not grow if bit should be set to 0
220 *
221 * \param X MPI to use
222 * \param pos Zero-based index of the bit in X
223 * \param val The value to set the bit to (0 or 1)
224 *
225 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000226 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
Paul Bakker2f5947e2011-05-18 15:47:11 +0000227 * POLARSSL_ERR_MPI_BAD_INPUT_DATA if val is not 0 or 1
228 */
229int mpi_set_bit( mpi *X, size_t pos, unsigned char val );
230
Paul Bakker5121ce52009-01-03 21:22:43 +0000231/**
Paul Bakker6b906e52012-05-08 12:01:43 +0000232 * \brief Return the number of zero-bits before the least significant
233 * '1' bit
234 *
235 * Note: Thus also the zero-based index of the least significant '1' bit
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000236 *
237 * \param X MPI to use
Paul Bakker5121ce52009-01-03 21:22:43 +0000238 */
Paul Bakker23986e52011-04-24 08:57:21 +0000239size_t mpi_lsb( const mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000240
241/**
Paul Bakker6b906e52012-05-08 12:01:43 +0000242 * \brief Return the number of bits up to and including the most
243 * significant '1' bit'
244 *
245 * Note: Thus also the one-based index of the most significant '1' bit
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000246 *
247 * \param X MPI to use
Paul Bakker5121ce52009-01-03 21:22:43 +0000248 */
Paul Bakker23986e52011-04-24 08:57:21 +0000249size_t mpi_msb( const mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000250
251/**
252 * \brief Return the total size in bytes
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000253 *
254 * \param X MPI to use
Paul Bakker5121ce52009-01-03 21:22:43 +0000255 */
Paul Bakker23986e52011-04-24 08:57:21 +0000256size_t mpi_size( const mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000257
258/**
259 * \brief Import from an ASCII string
260 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000261 * \param X Destination MPI
262 * \param radix Input numeric base
263 * \param s Null-terminated string buffer
Paul Bakker5121ce52009-01-03 21:22:43 +0000264 *
Paul Bakkercb37aa52011-11-30 16:00:20 +0000265 * \return 0 if successful, or a POLARSSL_ERR_MPI_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000266 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000267int mpi_read_string( mpi *X, int radix, const char *s );
Paul Bakker5121ce52009-01-03 21:22:43 +0000268
269/**
270 * \brief Export into an ASCII string
271 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000272 * \param X Source MPI
273 * \param radix Output numeric base
274 * \param s String buffer
275 * \param slen String buffer size
Paul Bakker5121ce52009-01-03 21:22:43 +0000276 *
Paul Bakkercb37aa52011-11-30 16:00:20 +0000277 * \return 0 if successful, or a POLARSSL_ERR_MPI_XXX error code.
Paul Bakkerff60ee62010-03-16 21:09:09 +0000278 * *slen is always updated to reflect the amount
279 * of data that has (or would have) been written.
Paul Bakker5121ce52009-01-03 21:22:43 +0000280 *
281 * \note Call this function with *slen = 0 to obtain the
282 * minimum required buffer size in *slen.
283 */
Paul Bakker23986e52011-04-24 08:57:21 +0000284int mpi_write_string( const mpi *X, int radix, char *s, size_t *slen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000285
286/**
287 * \brief Read X from an opened file
288 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000289 * \param X Destination MPI
290 * \param radix Input numeric base
291 * \param fin Input file handle
Paul Bakker5121ce52009-01-03 21:22:43 +0000292 *
Paul Bakkercb37aa52011-11-30 16:00:20 +0000293 * \return 0 if successful, POLARSSL_ERR_MPI_BUFFER_TOO_SMALL if
294 * the file read buffer is too small or a
295 * POLARSSL_ERR_MPI_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000296 */
297int mpi_read_file( mpi *X, int radix, FILE *fin );
298
299/**
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000300 * \brief Write X into an opened file, or stdout if fout is NULL
Paul Bakker5121ce52009-01-03 21:22:43 +0000301 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000302 * \param p Prefix, can be NULL
303 * \param X Source MPI
304 * \param radix Output numeric base
305 * \param fout Output file handle (can be NULL)
Paul Bakker5121ce52009-01-03 21:22:43 +0000306 *
Paul Bakkercb37aa52011-11-30 16:00:20 +0000307 * \return 0 if successful, or a POLARSSL_ERR_MPI_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000308 *
309 * \note Set fout == NULL to print X on the console.
310 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000311int mpi_write_file( const char *p, const mpi *X, int radix, FILE *fout );
Paul Bakker5121ce52009-01-03 21:22:43 +0000312
313/**
314 * \brief Import X from unsigned binary data, big endian
315 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000316 * \param X Destination MPI
317 * \param buf Input buffer
318 * \param buflen Input buffer size
Paul Bakker5121ce52009-01-03 21:22:43 +0000319 *
320 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000321 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000322 */
Paul Bakker23986e52011-04-24 08:57:21 +0000323int mpi_read_binary( mpi *X, const unsigned char *buf, size_t buflen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000324
325/**
326 * \brief Export X into unsigned binary data, big endian
327 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000328 * \param X Source MPI
329 * \param buf Output buffer
330 * \param buflen Output buffer size
Paul Bakker5121ce52009-01-03 21:22:43 +0000331 *
332 * \return 0 if successful,
Paul Bakker40e46942009-01-03 21:51:57 +0000333 * POLARSSL_ERR_MPI_BUFFER_TOO_SMALL if buf isn't large enough
Paul Bakker5121ce52009-01-03 21:22:43 +0000334 */
Paul Bakker23986e52011-04-24 08:57:21 +0000335int mpi_write_binary( const mpi *X, unsigned char *buf, size_t buflen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000336
337/**
338 * \brief Left-shift: X <<= count
339 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000340 * \param X MPI to shift
341 * \param count Amount to shift
342 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000343 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000344 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000345 */
Paul Bakker23986e52011-04-24 08:57:21 +0000346int mpi_shift_l( mpi *X, size_t count );
Paul Bakker5121ce52009-01-03 21:22:43 +0000347
348/**
349 * \brief Right-shift: X >>= count
350 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000351 * \param X MPI to shift
352 * \param count Amount to shift
353 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000354 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000355 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000356 */
Paul Bakker23986e52011-04-24 08:57:21 +0000357int mpi_shift_r( mpi *X, size_t count );
Paul Bakker5121ce52009-01-03 21:22:43 +0000358
359/**
360 * \brief Compare unsigned values
361 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000362 * \param X Left-hand MPI
363 * \param Y Right-hand MPI
364 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000365 * \return 1 if |X| is greater than |Y|,
366 * -1 if |X| is lesser than |Y| or
367 * 0 if |X| is equal to |Y|
368 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000369int mpi_cmp_abs( const mpi *X, const mpi *Y );
Paul Bakker5121ce52009-01-03 21:22:43 +0000370
371/**
372 * \brief Compare signed values
373 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000374 * \param X Left-hand MPI
375 * \param Y Right-hand MPI
376 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000377 * \return 1 if X is greater than Y,
378 * -1 if X is lesser than Y or
379 * 0 if X is equal to Y
380 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000381int mpi_cmp_mpi( const mpi *X, const mpi *Y );
Paul Bakker5121ce52009-01-03 21:22:43 +0000382
383/**
384 * \brief Compare signed values
385 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000386 * \param X Left-hand MPI
387 * \param z The integer value to compare to
388 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000389 * \return 1 if X is greater than z,
390 * -1 if X is lesser than z or
391 * 0 if X is equal to z
392 */
Paul Bakkera755ca12011-04-24 09:11:17 +0000393int mpi_cmp_int( const mpi *X, t_sint z );
Paul Bakker5121ce52009-01-03 21:22:43 +0000394
395/**
396 * \brief Unsigned addition: X = |A| + |B|
397 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000398 * \param X Destination MPI
399 * \param A Left-hand MPI
400 * \param B Right-hand MPI
401 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000402 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000403 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000404 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000405int mpi_add_abs( mpi *X, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000406
407/**
408 * \brief Unsigned substraction: X = |A| - |B|
409 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000410 * \param X Destination MPI
411 * \param A Left-hand MPI
412 * \param B Right-hand MPI
413 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000414 * \return 0 if successful,
Paul Bakker40e46942009-01-03 21:51:57 +0000415 * POLARSSL_ERR_MPI_NEGATIVE_VALUE if B is greater than A
Paul Bakker5121ce52009-01-03 21:22:43 +0000416 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000417int mpi_sub_abs( mpi *X, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000418
419/**
420 * \brief Signed addition: X = A + B
421 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000422 * \param X Destination MPI
423 * \param A Left-hand MPI
424 * \param B Right-hand MPI
425 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000426 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000427 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000428 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000429int mpi_add_mpi( mpi *X, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000430
431/**
432 * \brief Signed substraction: X = A - B
433 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000434 * \param X Destination MPI
435 * \param A Left-hand MPI
436 * \param B Right-hand MPI
437 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000438 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000439 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000440 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000441int mpi_sub_mpi( mpi *X, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000442
443/**
444 * \brief Signed addition: X = A + b
445 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000446 * \param X Destination MPI
447 * \param A Left-hand MPI
448 * \param b The integer value to add
449 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000450 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000451 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000452 */
Paul Bakkera755ca12011-04-24 09:11:17 +0000453int mpi_add_int( mpi *X, const mpi *A, t_sint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000454
455/**
456 * \brief Signed substraction: X = A - b
457 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000458 * \param X Destination MPI
459 * \param A Left-hand MPI
460 * \param b The integer value to subtract
461 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000462 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000463 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000464 */
Paul Bakkera755ca12011-04-24 09:11:17 +0000465int mpi_sub_int( mpi *X, const mpi *A, t_sint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000466
467/**
468 * \brief Baseline multiplication: X = A * B
469 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000470 * \param X Destination MPI
471 * \param A Left-hand MPI
472 * \param B Right-hand MPI
473 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000474 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000475 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000476 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000477int mpi_mul_mpi( mpi *X, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000478
479/**
480 * \brief Baseline multiplication: X = A * b
Paul Bakkerce40a6d2009-06-23 19:46:08 +0000481 * Note: b is an unsigned integer type, thus
482 * Negative values of b are ignored.
Paul Bakker5121ce52009-01-03 21:22:43 +0000483 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000484 * \param X Destination MPI
485 * \param A Left-hand MPI
486 * \param b The integer value to multiply with
487 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000488 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000489 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000490 */
Paul Bakkera755ca12011-04-24 09:11:17 +0000491int mpi_mul_int( mpi *X, const mpi *A, t_sint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000492
493/**
494 * \brief Division by mpi: A = Q * B + R
495 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000496 * \param Q Destination MPI for the quotient
497 * \param R Destination MPI for the rest value
498 * \param A Left-hand MPI
499 * \param B Right-hand MPI
500 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000501 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000502 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000503 * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if B == 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000504 *
505 * \note Either Q or R can be NULL.
506 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000507int mpi_div_mpi( mpi *Q, mpi *R, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000508
509/**
510 * \brief Division by int: A = Q * b + R
511 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000512 * \param Q Destination MPI for the quotient
513 * \param R Destination MPI for the rest value
514 * \param A Left-hand MPI
515 * \param b Integer to divide by
516 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000517 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000518 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000519 * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if b == 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000520 *
521 * \note Either Q or R can be NULL.
522 */
Paul Bakkera755ca12011-04-24 09:11:17 +0000523int mpi_div_int( mpi *Q, mpi *R, const mpi *A, t_sint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000524
525/**
526 * \brief Modulo: R = A mod B
527 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000528 * \param R Destination MPI for the rest value
529 * \param A Left-hand MPI
530 * \param B Right-hand MPI
531 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000532 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000533 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
Paul Bakkerce40a6d2009-06-23 19:46:08 +0000534 * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if B == 0,
535 * POLARSSL_ERR_MPI_NEGATIVE_VALUE if B < 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000536 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000537int mpi_mod_mpi( mpi *R, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000538
539/**
540 * \brief Modulo: r = A mod b
541 *
Paul Bakkera755ca12011-04-24 09:11:17 +0000542 * \param r Destination t_uint
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000543 * \param A Left-hand MPI
544 * \param b Integer to divide by
545 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000546 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000547 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
Paul Bakkerce40a6d2009-06-23 19:46:08 +0000548 * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if b == 0,
549 * POLARSSL_ERR_MPI_NEGATIVE_VALUE if b < 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000550 */
Paul Bakkera755ca12011-04-24 09:11:17 +0000551int mpi_mod_int( t_uint *r, const mpi *A, t_sint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000552
553/**
554 * \brief Sliding-window exponentiation: X = A^E mod N
555 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000556 * \param X Destination MPI
557 * \param A Left-hand MPI
558 * \param E Exponent MPI
559 * \param N Modular MPI
560 * \param _RR Speed-up MPI used for recalculations
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 Bakkerf6198c12012-05-16 08:02:29 +0000564 * POLARSSL_ERR_MPI_BAD_INPUT_DATA if N is negative or even or if
565 * E is negative
Paul Bakker5121ce52009-01-03 21:22:43 +0000566 *
567 * \note _RR is used to avoid re-computing R*R mod N across
568 * multiple calls, which speeds up things a bit. It can
569 * be set to NULL if the extra performance is unneeded.
570 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000571int mpi_exp_mod( mpi *X, const mpi *A, const mpi *E, const mpi *N, mpi *_RR );
Paul Bakker5121ce52009-01-03 21:22:43 +0000572
573/**
Paul Bakker287781a2011-03-26 13:18:49 +0000574 * \brief Fill an MPI X with size bytes of random
575 *
576 * \param X Destination MPI
577 * \param size Size in bytes
578 * \param f_rng RNG function
579 * \param p_rng RNG parameter
580 *
581 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000582 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker287781a2011-03-26 13:18:49 +0000583 */
Paul Bakkera3d195c2011-11-27 21:07:34 +0000584int mpi_fill_random( mpi *X, size_t size,
585 int (*f_rng)(void *, unsigned char *, size_t),
586 void *p_rng );
Paul Bakker287781a2011-03-26 13:18:49 +0000587
588/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000589 * \brief Greatest common divisor: G = gcd(A, B)
590 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000591 * \param G Destination MPI
592 * \param A Left-hand MPI
593 * \param B Right-hand MPI
594 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000595 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000596 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000597 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000598int mpi_gcd( mpi *G, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000599
600/**
601 * \brief Modular inverse: X = A^-1 mod N
602 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000603 * \param X Destination MPI
604 * \param A Left-hand MPI
605 * \param N Right-hand MPI
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 Bakker40e46942009-01-03 21:51:57 +0000609 * POLARSSL_ERR_MPI_BAD_INPUT_DATA if N is negative or nil
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000610 POLARSSL_ERR_MPI_NOT_ACCEPTABLE if A has no inverse mod N
Paul Bakker5121ce52009-01-03 21:22:43 +0000611 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000612int mpi_inv_mod( mpi *X, const mpi *A, const mpi *N );
Paul Bakker5121ce52009-01-03 21:22:43 +0000613
614/**
615 * \brief Miller-Rabin primality test
616 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000617 * \param X MPI to check
618 * \param f_rng RNG function
619 * \param p_rng RNG parameter
620 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000621 * \return 0 if successful (probably prime),
Paul Bakker69e095c2011-12-10 21:55:01 +0000622 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000623 * POLARSSL_ERR_MPI_NOT_ACCEPTABLE if X is not prime
Paul Bakker5121ce52009-01-03 21:22:43 +0000624 */
Paul Bakkera3d195c2011-11-27 21:07:34 +0000625int mpi_is_prime( mpi *X,
626 int (*f_rng)(void *, unsigned char *, size_t),
627 void *p_rng );
Paul Bakker5121ce52009-01-03 21:22:43 +0000628
629/**
630 * \brief Prime number generation
631 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000632 * \param X Destination MPI
Paul Bakkerfe3256e2011-11-25 12:11:43 +0000633 * \param nbits Required size of X in bits ( 3 <= nbits <= POLARSSL_MPI_MAX_BITS )
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000634 * \param dh_flag If 1, then (X-1)/2 will be prime too
Paul Bakker5121ce52009-01-03 21:22:43 +0000635 * \param f_rng RNG function
636 * \param p_rng RNG parameter
637 *
638 * \return 0 if successful (probably prime),
Paul Bakker69e095c2011-12-10 21:55:01 +0000639 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000640 * POLARSSL_ERR_MPI_BAD_INPUT_DATA if nbits is < 3
Paul Bakker5121ce52009-01-03 21:22:43 +0000641 */
Paul Bakker23986e52011-04-24 08:57:21 +0000642int mpi_gen_prime( mpi *X, size_t nbits, int dh_flag,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000643 int (*f_rng)(void *, unsigned char *, size_t),
644 void *p_rng );
Paul Bakker5121ce52009-01-03 21:22:43 +0000645
646/**
647 * \brief Checkup routine
648 *
649 * \return 0 if successful, or 1 if the test failed
650 */
651int mpi_self_test( int verbose );
652
653#ifdef __cplusplus
654}
655#endif
656
657#endif /* bignum.h */