blob: a2039c71f00e0bc7a3a7353670ae337d13bb6ab0 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/**
2 * \file bignum.h
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00003 *
Paul Bakker37ca75d2011-01-06 12:28:03 +00004 * \brief Multi-precision integer library
5 *
Paul Bakker84f12b72010-07-18 10:13:04 +00006 * Copyright (C) 2006-2010, Brainspark B.V.
Paul Bakkerb96f1542010-07-18 20:36:00 +00007 *
8 * This file is part of PolarSSL (http://www.polarssl.org)
Paul Bakker84f12b72010-07-18 10:13:04 +00009 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
Paul Bakkerb96f1542010-07-18 20:36:00 +000010 *
Paul Bakker77b385e2009-07-28 17:23:11 +000011 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000012 *
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000013 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License along
24 * with this program; if not, write to the Free Software Foundation, Inc.,
25 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Paul Bakker5121ce52009-01-03 21:22:43 +000026 */
Paul Bakker40e46942009-01-03 21:51:57 +000027#ifndef POLARSSL_BIGNUM_H
28#define POLARSSL_BIGNUM_H
Paul Bakker5121ce52009-01-03 21:22:43 +000029
30#include <stdio.h>
Paul Bakker23986e52011-04-24 08:57:21 +000031#include <string.h>
Paul Bakker5121ce52009-01-03 21:22:43 +000032
Paul Bakkercf0360a2012-01-20 10:08:14 +000033#include "config.h"
34
Paul Bakker9d781402011-05-09 16:17:09 +000035#define POLARSSL_ERR_MPI_FILE_IO_ERROR -0x0002 /**< An error occurred while reading from or writing to a file. */
36#define POLARSSL_ERR_MPI_BAD_INPUT_DATA -0x0004 /**< Bad input parameters to function. */
37#define POLARSSL_ERR_MPI_INVALID_CHARACTER -0x0006 /**< There is an invalid character in the digit string. */
Paul Bakker69e095c2011-12-10 21:55:01 +000038#define POLARSSL_ERR_MPI_BUFFER_TOO_SMALL -0x0008 /**< The buffer is too small to write to. */
Paul Bakker9d781402011-05-09 16:17:09 +000039#define POLARSSL_ERR_MPI_NEGATIVE_VALUE -0x000A /**< The input arguments are negative or result in illegal output. */
40#define POLARSSL_ERR_MPI_DIVISION_BY_ZERO -0x000C /**< The input argument for division is zero, which is not allowed. */
41#define POLARSSL_ERR_MPI_NOT_ACCEPTABLE -0x000E /**< The input arguments are not acceptable. */
Paul Bakker69e095c2011-12-10 21:55:01 +000042#define POLARSSL_ERR_MPI_MALLOC_FAILED -0x0010 /**< Memory allocation failed. */
Paul Bakker5121ce52009-01-03 21:22:43 +000043
44#define MPI_CHK(f) if( ( ret = f ) != 0 ) goto cleanup
45
46/*
Paul Bakkerf9688572011-05-05 10:00:45 +000047 * Maximum size MPIs are allowed to grow to in number of limbs.
48 */
49#define POLARSSL_MPI_MAX_LIMBS 10000
50
51/*
Paul Bakkerb6d5f082011-11-25 11:52:11 +000052 * Maximum window size used for modular exponentiation. Default: 6
53 * Minimum value: 1. Maximum value: 6.
54 *
55 * Result is an array of ( 2 << POLARSSL_MPI_WINDOW_SIZE ) MPIs used
56 * for the sliding window calculation. (So 64 by default)
57 *
58 * Reduction in size, reduces speed.
59 */
60#define POLARSSL_MPI_WINDOW_SIZE 6 /**< Maximum windows size used. */
61
62/*
Paul Bakkerfe3256e2011-11-25 12:11:43 +000063 * Maximum size of MPIs allowed in bits and bytes for user-MPIs.
Paul Bakker5552c8c2012-07-05 13:31:54 +000064 * ( Default: 512 bytes => 4096 bits, Maximum: 1024 bytes => 8192 bits )
Paul Bakkerfe3256e2011-11-25 12:11:43 +000065 *
66 * Note: Calculations can results temporarily in larger MPIs. So the number
67 * of limbs required (POLARSSL_MPI_MAX_LIMBS) is higher.
68 */
69#define POLARSSL_MPI_MAX_SIZE 512 /**< Maximum number of bytes for usable MPIs. */
70#define POLARSSL_MPI_MAX_BITS ( 8 * POLARSSL_MPI_MAX_SIZE ) /**< Maximum number of bits for usable MPIs. */
71
72/*
Paul Bakker5531c6d2012-09-26 19:20:46 +000073 * When reading from files with mpi_read_file() and writing to files with
74 * mpi_write_file() the buffer should have space
Paul Bakkercb37aa52011-11-30 16:00:20 +000075 * for a (short) label, the MPI (in the provided radix), the newline
76 * characters and the '\0'.
77 *
78 * By default we assume at least a 10 char label, a minimum radix of 10
79 * (decimal) and a maximum of 4096 bit numbers (1234 decimal chars).
Paul Bakkerf9183102012-09-27 20:42:35 +000080 * Autosized at compile time for at least a 10 char label, a minimum radix
81 * of 10 (decimal) for a number of POLARSSL_MPI_MAX_BITS size.
82 *
83 * This used to be statically sized to 1250 for a maximum of 4096 bit
84 * numbers (1234 decimal chars).
85 *
86 * Calculate using the formula:
87 * POLARSSL_MPI_RW_BUFFER_SIZE = ceil(POLARSSL_MPI_MAX_BITS / ln(10) * ln(2)) +
88 * LabelSize + 6
Paul Bakkercb37aa52011-11-30 16:00:20 +000089 */
Paul Bakkerf9183102012-09-27 20:42:35 +000090#define POLARSSL_MPI_MAX_BITS_SCALE100 ( 100 * POLARSSL_MPI_MAX_BITS )
91#define LN_2_DIV_LN_10_SCALE100 332
92#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 +000093
94/*
Paul Bakker5121ce52009-01-03 21:22:43 +000095 * Define the base integer type, architecture-wise
96 */
Paul Bakker40e46942009-01-03 21:51:57 +000097#if defined(POLARSSL_HAVE_INT8)
Paul Bakkera755ca12011-04-24 09:11:17 +000098typedef signed char t_sint;
99typedef unsigned char t_uint;
100typedef unsigned short t_udbl;
Paul Bakker5121ce52009-01-03 21:22:43 +0000101#else
Paul Bakker40e46942009-01-03 21:51:57 +0000102#if defined(POLARSSL_HAVE_INT16)
Paul Bakkera755ca12011-04-24 09:11:17 +0000103typedef signed short t_sint;
104typedef unsigned short t_uint;
105typedef unsigned long t_udbl;
Paul Bakker5121ce52009-01-03 21:22:43 +0000106#else
Paul Bakkera755ca12011-04-24 09:11:17 +0000107 typedef signed long t_sint;
108 typedef unsigned long t_uint;
Paul Bakker5121ce52009-01-03 21:22:43 +0000109 #if defined(_MSC_VER) && defined(_M_IX86)
Paul Bakkera755ca12011-04-24 09:11:17 +0000110 typedef unsigned __int64 t_udbl;
Paul Bakker5121ce52009-01-03 21:22:43 +0000111 #else
Paul Bakkercf0360a2012-01-20 10:08:14 +0000112 #if defined(__GNUC__) && ( \
113 defined(__amd64__) || defined(__x86_64__) || \
Paul Bakker5121ce52009-01-03 21:22:43 +0000114 defined(__ppc64__) || defined(__powerpc64__) || \
Paul Bakker44637402011-11-26 09:23:07 +0000115 defined(__ia64__) || defined(__alpha__) || \
116 (defined(__sparc__) && defined(__arch64__)) || \
Paul Bakkercf0360a2012-01-20 10:08:14 +0000117 defined(__s390x__) )
Paul Bakkera755ca12011-04-24 09:11:17 +0000118 typedef unsigned int t_udbl __attribute__((mode(TI)));
Paul Bakker66219872012-01-22 20:38:13 +0000119 #define POLARSSL_HAVE_LONGLONG
Paul Bakker5121ce52009-01-03 21:22:43 +0000120 #else
Paul Bakker1a9382e2009-07-11 16:35:32 +0000121 #if defined(POLARSSL_HAVE_LONGLONG)
Paul Bakkera755ca12011-04-24 09:11:17 +0000122 typedef unsigned long long t_udbl;
Paul Bakker1a9382e2009-07-11 16:35:32 +0000123 #endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000124 #endif
125 #endif
126#endif
127#endif
128
129/**
130 * \brief MPI structure
131 */
132typedef struct
133{
134 int s; /*!< integer sign */
Paul Bakker23986e52011-04-24 08:57:21 +0000135 size_t n; /*!< total # of limbs */
Paul Bakkera755ca12011-04-24 09:11:17 +0000136 t_uint *p; /*!< pointer to limbs */
Paul Bakker5121ce52009-01-03 21:22:43 +0000137}
138mpi;
139
140#ifdef __cplusplus
141extern "C" {
142#endif
143
144/**
Paul Bakker6c591fa2011-05-05 11:49:20 +0000145 * \brief Initialize one MPI
146 *
147 * \param X One MPI to initialize.
Paul Bakker5121ce52009-01-03 21:22:43 +0000148 */
Paul Bakker6c591fa2011-05-05 11:49:20 +0000149void mpi_init( mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000150
151/**
Paul Bakker6c591fa2011-05-05 11:49:20 +0000152 * \brief Unallocate one MPI
153 *
154 * \param X One MPI to unallocate.
Paul Bakker5121ce52009-01-03 21:22:43 +0000155 */
Paul Bakker6c591fa2011-05-05 11:49:20 +0000156void mpi_free( mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000157
158/**
159 * \brief Enlarge to the specified number of limbs
160 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000161 * \param X MPI to grow
162 * \param nblimbs The target number of limbs
163 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000164 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000165 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000166 */
Paul Bakker23986e52011-04-24 08:57:21 +0000167int mpi_grow( mpi *X, size_t nblimbs );
Paul Bakker5121ce52009-01-03 21:22:43 +0000168
169/**
170 * \brief Copy the contents of Y into X
171 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000172 * \param X Destination MPI
173 * \param Y Source MPI
174 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000175 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000176 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000177 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000178int mpi_copy( mpi *X, const mpi *Y );
Paul Bakker5121ce52009-01-03 21:22:43 +0000179
180/**
181 * \brief Swap the contents of X and Y
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000182 *
183 * \param X First MPI value
184 * \param Y Second MPI value
Paul Bakker5121ce52009-01-03 21:22:43 +0000185 */
186void mpi_swap( mpi *X, mpi *Y );
187
188/**
189 * \brief Set value from integer
190 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000191 * \param X MPI to set
192 * \param z Value to use
193 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000194 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000195 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000196 */
Paul Bakkera755ca12011-04-24 09:11:17 +0000197int mpi_lset( mpi *X, t_sint z );
Paul Bakker5121ce52009-01-03 21:22:43 +0000198
Paul Bakker2f5947e2011-05-18 15:47:11 +0000199/*
200 * \brief Get a specific bit from X
201 *
202 * \param X MPI to use
203 * \param pos Zero-based index of the bit in X
204 *
205 * \return Either a 0 or a 1
206 */
Paul Bakker6b906e52012-05-08 12:01:43 +0000207int mpi_get_bit( const mpi *X, size_t pos );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000208
209/*
210 * \brief Set a bit of X to a specific value of 0 or 1
211 *
212 * \note Will grow X if necessary to set a bit to 1 in a not yet
213 * existing limb. Will not grow if bit should be set to 0
214 *
215 * \param X MPI to use
216 * \param pos Zero-based index of the bit in X
217 * \param val The value to set the bit to (0 or 1)
218 *
219 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000220 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
Paul Bakker2f5947e2011-05-18 15:47:11 +0000221 * POLARSSL_ERR_MPI_BAD_INPUT_DATA if val is not 0 or 1
222 */
223int mpi_set_bit( mpi *X, size_t pos, unsigned char val );
224
Paul Bakker5121ce52009-01-03 21:22:43 +0000225/**
Paul Bakker6b906e52012-05-08 12:01:43 +0000226 * \brief Return the number of zero-bits before the least significant
227 * '1' bit
228 *
229 * Note: Thus also the zero-based index of the least significant '1' bit
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000230 *
231 * \param X MPI to use
Paul Bakker5121ce52009-01-03 21:22:43 +0000232 */
Paul Bakker23986e52011-04-24 08:57:21 +0000233size_t mpi_lsb( const mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000234
235/**
Paul Bakker6b906e52012-05-08 12:01:43 +0000236 * \brief Return the number of bits up to and including the most
237 * significant '1' bit'
238 *
239 * Note: Thus also the one-based index of the most significant '1' bit
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000240 *
241 * \param X MPI to use
Paul Bakker5121ce52009-01-03 21:22:43 +0000242 */
Paul Bakker23986e52011-04-24 08:57:21 +0000243size_t mpi_msb( const mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000244
245/**
246 * \brief Return the total size in bytes
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000247 *
248 * \param X MPI to use
Paul Bakker5121ce52009-01-03 21:22:43 +0000249 */
Paul Bakker23986e52011-04-24 08:57:21 +0000250size_t mpi_size( const mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000251
252/**
253 * \brief Import from an ASCII string
254 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000255 * \param X Destination MPI
256 * \param radix Input numeric base
257 * \param s Null-terminated string buffer
Paul Bakker5121ce52009-01-03 21:22:43 +0000258 *
Paul Bakkercb37aa52011-11-30 16:00:20 +0000259 * \return 0 if successful, or a POLARSSL_ERR_MPI_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000260 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000261int mpi_read_string( mpi *X, int radix, const char *s );
Paul Bakker5121ce52009-01-03 21:22:43 +0000262
263/**
264 * \brief Export into an ASCII string
265 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000266 * \param X Source MPI
267 * \param radix Output numeric base
268 * \param s String buffer
269 * \param slen String buffer size
Paul Bakker5121ce52009-01-03 21:22:43 +0000270 *
Paul Bakkercb37aa52011-11-30 16:00:20 +0000271 * \return 0 if successful, or a POLARSSL_ERR_MPI_XXX error code.
Paul Bakkerff60ee62010-03-16 21:09:09 +0000272 * *slen is always updated to reflect the amount
273 * of data that has (or would have) been written.
Paul Bakker5121ce52009-01-03 21:22:43 +0000274 *
275 * \note Call this function with *slen = 0 to obtain the
276 * minimum required buffer size in *slen.
277 */
Paul Bakker23986e52011-04-24 08:57:21 +0000278int mpi_write_string( const mpi *X, int radix, char *s, size_t *slen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000279
280/**
281 * \brief Read X from an opened file
282 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000283 * \param X Destination MPI
284 * \param radix Input numeric base
285 * \param fin Input file handle
Paul Bakker5121ce52009-01-03 21:22:43 +0000286 *
Paul Bakkercb37aa52011-11-30 16:00:20 +0000287 * \return 0 if successful, POLARSSL_ERR_MPI_BUFFER_TOO_SMALL if
288 * the file read buffer is too small or a
289 * POLARSSL_ERR_MPI_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000290 */
291int mpi_read_file( mpi *X, int radix, FILE *fin );
292
293/**
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000294 * \brief Write X into an opened file, or stdout if fout is NULL
Paul Bakker5121ce52009-01-03 21:22:43 +0000295 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000296 * \param p Prefix, can be NULL
297 * \param X Source MPI
298 * \param radix Output numeric base
299 * \param fout Output file handle (can be NULL)
Paul Bakker5121ce52009-01-03 21:22:43 +0000300 *
Paul Bakkercb37aa52011-11-30 16:00:20 +0000301 * \return 0 if successful, or a POLARSSL_ERR_MPI_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000302 *
303 * \note Set fout == NULL to print X on the console.
304 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000305int mpi_write_file( const char *p, const mpi *X, int radix, FILE *fout );
Paul Bakker5121ce52009-01-03 21:22:43 +0000306
307/**
308 * \brief Import X from unsigned binary data, big endian
309 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000310 * \param X Destination MPI
311 * \param buf Input buffer
312 * \param buflen Input buffer size
Paul Bakker5121ce52009-01-03 21:22:43 +0000313 *
314 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000315 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000316 */
Paul Bakker23986e52011-04-24 08:57:21 +0000317int mpi_read_binary( mpi *X, const unsigned char *buf, size_t buflen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000318
319/**
320 * \brief Export X into unsigned binary data, big endian
321 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000322 * \param X Source MPI
323 * \param buf Output buffer
324 * \param buflen Output buffer size
Paul Bakker5121ce52009-01-03 21:22:43 +0000325 *
326 * \return 0 if successful,
Paul Bakker40e46942009-01-03 21:51:57 +0000327 * POLARSSL_ERR_MPI_BUFFER_TOO_SMALL if buf isn't large enough
Paul Bakker5121ce52009-01-03 21:22:43 +0000328 */
Paul Bakker23986e52011-04-24 08:57:21 +0000329int mpi_write_binary( const mpi *X, unsigned char *buf, size_t buflen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000330
331/**
332 * \brief Left-shift: X <<= count
333 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000334 * \param X MPI to shift
335 * \param count Amount to shift
336 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000337 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000338 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000339 */
Paul Bakker23986e52011-04-24 08:57:21 +0000340int mpi_shift_l( mpi *X, size_t count );
Paul Bakker5121ce52009-01-03 21:22:43 +0000341
342/**
343 * \brief Right-shift: X >>= count
344 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000345 * \param X MPI to shift
346 * \param count Amount to shift
347 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000348 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000349 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000350 */
Paul Bakker23986e52011-04-24 08:57:21 +0000351int mpi_shift_r( mpi *X, size_t count );
Paul Bakker5121ce52009-01-03 21:22:43 +0000352
353/**
354 * \brief Compare unsigned values
355 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000356 * \param X Left-hand MPI
357 * \param Y Right-hand MPI
358 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000359 * \return 1 if |X| is greater than |Y|,
360 * -1 if |X| is lesser than |Y| or
361 * 0 if |X| is equal to |Y|
362 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000363int mpi_cmp_abs( const mpi *X, const mpi *Y );
Paul Bakker5121ce52009-01-03 21:22:43 +0000364
365/**
366 * \brief Compare signed values
367 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000368 * \param X Left-hand MPI
369 * \param Y Right-hand MPI
370 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000371 * \return 1 if X is greater than Y,
372 * -1 if X is lesser than Y or
373 * 0 if X is equal to Y
374 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000375int mpi_cmp_mpi( const mpi *X, const mpi *Y );
Paul Bakker5121ce52009-01-03 21:22:43 +0000376
377/**
378 * \brief Compare signed values
379 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000380 * \param X Left-hand MPI
381 * \param z The integer value to compare to
382 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000383 * \return 1 if X is greater than z,
384 * -1 if X is lesser than z or
385 * 0 if X is equal to z
386 */
Paul Bakkera755ca12011-04-24 09:11:17 +0000387int mpi_cmp_int( const mpi *X, t_sint z );
Paul Bakker5121ce52009-01-03 21:22:43 +0000388
389/**
390 * \brief Unsigned addition: X = |A| + |B|
391 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000392 * \param X Destination MPI
393 * \param A Left-hand MPI
394 * \param B Right-hand MPI
395 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000396 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000397 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000398 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000399int mpi_add_abs( mpi *X, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000400
401/**
402 * \brief Unsigned substraction: X = |A| - |B|
403 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000404 * \param X Destination MPI
405 * \param A Left-hand MPI
406 * \param B Right-hand MPI
407 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000408 * \return 0 if successful,
Paul Bakker40e46942009-01-03 21:51:57 +0000409 * POLARSSL_ERR_MPI_NEGATIVE_VALUE if B is greater than A
Paul Bakker5121ce52009-01-03 21:22:43 +0000410 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000411int mpi_sub_abs( mpi *X, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000412
413/**
414 * \brief Signed addition: X = A + B
415 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000416 * \param X Destination MPI
417 * \param A Left-hand MPI
418 * \param B Right-hand MPI
419 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000420 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000421 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000422 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000423int mpi_add_mpi( mpi *X, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000424
425/**
426 * \brief Signed substraction: X = A - B
427 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000428 * \param X Destination MPI
429 * \param A Left-hand MPI
430 * \param B Right-hand MPI
431 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000432 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000433 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000434 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000435int mpi_sub_mpi( mpi *X, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000436
437/**
438 * \brief Signed addition: X = A + b
439 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000440 * \param X Destination MPI
441 * \param A Left-hand MPI
442 * \param b The integer value to add
443 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000444 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000445 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000446 */
Paul Bakkera755ca12011-04-24 09:11:17 +0000447int mpi_add_int( mpi *X, const mpi *A, t_sint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000448
449/**
450 * \brief Signed substraction: X = A - b
451 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000452 * \param X Destination MPI
453 * \param A Left-hand MPI
454 * \param b The integer value to subtract
455 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000456 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000457 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000458 */
Paul Bakkera755ca12011-04-24 09:11:17 +0000459int mpi_sub_int( mpi *X, const mpi *A, t_sint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000460
461/**
462 * \brief Baseline multiplication: X = A * B
463 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000464 * \param X Destination MPI
465 * \param A Left-hand MPI
466 * \param B Right-hand MPI
467 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000468 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000469 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000470 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000471int mpi_mul_mpi( mpi *X, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000472
473/**
474 * \brief Baseline multiplication: X = A * b
Paul Bakkerce40a6d2009-06-23 19:46:08 +0000475 * Note: b is an unsigned integer type, thus
476 * Negative values of b are ignored.
Paul Bakker5121ce52009-01-03 21:22:43 +0000477 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000478 * \param X Destination MPI
479 * \param A Left-hand MPI
480 * \param b The integer value to multiply with
481 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000482 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000483 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000484 */
Paul Bakkera755ca12011-04-24 09:11:17 +0000485int mpi_mul_int( mpi *X, const mpi *A, t_sint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000486
487/**
488 * \brief Division by mpi: A = Q * B + R
489 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000490 * \param Q Destination MPI for the quotient
491 * \param R Destination MPI for the rest value
492 * \param A Left-hand MPI
493 * \param B Right-hand MPI
494 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000495 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000496 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000497 * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if B == 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000498 *
499 * \note Either Q or R can be NULL.
500 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000501int mpi_div_mpi( mpi *Q, mpi *R, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000502
503/**
504 * \brief Division by int: A = Q * b + R
505 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000506 * \param Q Destination MPI for the quotient
507 * \param R Destination MPI for the rest value
508 * \param A Left-hand MPI
509 * \param b Integer to divide by
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 Bakker40e46942009-01-03 21:51:57 +0000513 * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if b == 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000514 *
515 * \note Either Q or R can be NULL.
516 */
Paul Bakkera755ca12011-04-24 09:11:17 +0000517int mpi_div_int( mpi *Q, mpi *R, const mpi *A, t_sint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000518
519/**
520 * \brief Modulo: R = A mod B
521 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000522 * \param R Destination MPI for the rest value
523 * \param A Left-hand MPI
524 * \param B Right-hand MPI
525 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000526 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000527 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
Paul Bakkerce40a6d2009-06-23 19:46:08 +0000528 * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if B == 0,
529 * POLARSSL_ERR_MPI_NEGATIVE_VALUE if B < 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000530 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000531int mpi_mod_mpi( mpi *R, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000532
533/**
534 * \brief Modulo: r = A mod b
535 *
Paul Bakkera755ca12011-04-24 09:11:17 +0000536 * \param r Destination t_uint
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000537 * \param A Left-hand MPI
538 * \param b Integer to divide by
539 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000540 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000541 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
Paul Bakkerce40a6d2009-06-23 19:46:08 +0000542 * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if b == 0,
543 * POLARSSL_ERR_MPI_NEGATIVE_VALUE if b < 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000544 */
Paul Bakkera755ca12011-04-24 09:11:17 +0000545int mpi_mod_int( t_uint *r, const mpi *A, t_sint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000546
547/**
548 * \brief Sliding-window exponentiation: X = A^E mod N
549 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000550 * \param X Destination MPI
551 * \param A Left-hand MPI
552 * \param E Exponent MPI
553 * \param N Modular MPI
554 * \param _RR Speed-up MPI used for recalculations
555 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000556 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000557 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
Paul Bakkerf6198c12012-05-16 08:02:29 +0000558 * POLARSSL_ERR_MPI_BAD_INPUT_DATA if N is negative or even or if
559 * E is negative
Paul Bakker5121ce52009-01-03 21:22:43 +0000560 *
561 * \note _RR is used to avoid re-computing R*R mod N across
562 * multiple calls, which speeds up things a bit. It can
563 * be set to NULL if the extra performance is unneeded.
564 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000565int mpi_exp_mod( mpi *X, const mpi *A, const mpi *E, const mpi *N, mpi *_RR );
Paul Bakker5121ce52009-01-03 21:22:43 +0000566
567/**
Paul Bakker287781a2011-03-26 13:18:49 +0000568 * \brief Fill an MPI X with size bytes of random
569 *
570 * \param X Destination MPI
571 * \param size Size in bytes
572 * \param f_rng RNG function
573 * \param p_rng RNG parameter
574 *
575 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000576 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker287781a2011-03-26 13:18:49 +0000577 */
Paul Bakkera3d195c2011-11-27 21:07:34 +0000578int mpi_fill_random( mpi *X, size_t size,
579 int (*f_rng)(void *, unsigned char *, size_t),
580 void *p_rng );
Paul Bakker287781a2011-03-26 13:18:49 +0000581
582/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000583 * \brief Greatest common divisor: G = gcd(A, B)
584 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000585 * \param G Destination MPI
586 * \param A Left-hand MPI
587 * \param B Right-hand MPI
588 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000589 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000590 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000591 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000592int mpi_gcd( mpi *G, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000593
594/**
595 * \brief Modular inverse: X = A^-1 mod N
596 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000597 * \param X Destination MPI
598 * \param A Left-hand MPI
599 * \param N Right-hand MPI
600 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000601 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000602 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000603 * POLARSSL_ERR_MPI_BAD_INPUT_DATA if N is negative or nil
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000604 POLARSSL_ERR_MPI_NOT_ACCEPTABLE if A has no inverse mod N
Paul Bakker5121ce52009-01-03 21:22:43 +0000605 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000606int mpi_inv_mod( mpi *X, const mpi *A, const mpi *N );
Paul Bakker5121ce52009-01-03 21:22:43 +0000607
608/**
609 * \brief Miller-Rabin primality test
610 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000611 * \param X MPI to check
612 * \param f_rng RNG function
613 * \param p_rng RNG parameter
614 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000615 * \return 0 if successful (probably prime),
Paul Bakker69e095c2011-12-10 21:55:01 +0000616 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000617 * POLARSSL_ERR_MPI_NOT_ACCEPTABLE if X is not prime
Paul Bakker5121ce52009-01-03 21:22:43 +0000618 */
Paul Bakkera3d195c2011-11-27 21:07:34 +0000619int mpi_is_prime( mpi *X,
620 int (*f_rng)(void *, unsigned char *, size_t),
621 void *p_rng );
Paul Bakker5121ce52009-01-03 21:22:43 +0000622
623/**
624 * \brief Prime number generation
625 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000626 * \param X Destination MPI
Paul Bakkerfe3256e2011-11-25 12:11:43 +0000627 * \param nbits Required size of X in bits ( 3 <= nbits <= POLARSSL_MPI_MAX_BITS )
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000628 * \param dh_flag If 1, then (X-1)/2 will be prime too
Paul Bakker5121ce52009-01-03 21:22:43 +0000629 * \param f_rng RNG function
630 * \param p_rng RNG parameter
631 *
632 * \return 0 if successful (probably prime),
Paul Bakker69e095c2011-12-10 21:55:01 +0000633 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000634 * POLARSSL_ERR_MPI_BAD_INPUT_DATA if nbits is < 3
Paul Bakker5121ce52009-01-03 21:22:43 +0000635 */
Paul Bakker23986e52011-04-24 08:57:21 +0000636int mpi_gen_prime( mpi *X, size_t nbits, int dh_flag,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000637 int (*f_rng)(void *, unsigned char *, size_t),
638 void *p_rng );
Paul Bakker5121ce52009-01-03 21:22:43 +0000639
640/**
641 * \brief Checkup routine
642 *
643 * \return 0 if successful, or 1 if the test failed
644 */
645int mpi_self_test( int verbose );
646
647#ifdef __cplusplus
648}
649#endif
650
651#endif /* bignum.h */