blob: 2853cc283aea890d6797c8d2b4093cdbad09a032 [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 Bakker6fa54882013-06-17 15:44:03 +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 Bakker5c2364c2012-10-01 14:41:15 +000035#ifdef _MSC_VER
36#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 Bakker6fa54882013-06-17 15:44:03 +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 Bakker6fa54882013-06-17 15:44:03 +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) )
Paul Bakker62261d62012-10-02 12:19:31 +0000131 typedef int64_t t_sint;
132 typedef uint64_t t_uint;
133 #else
134 #if ( defined(__GNUC__) && ( \
135 defined(__amd64__) || defined(__x86_64__) || \
136 defined(__ppc64__) || defined(__powerpc64__) || \
137 defined(__ia64__) || defined(__alpha__) || \
138 (defined(__sparc__) && defined(__arch64__)) || \
139 defined(__s390x__) ) )
140 typedef int64_t t_sint;
141 typedef uint64_t t_uint;
142 typedef unsigned int t_udbl __attribute__((mode(TI)));
143 #define POLARSSL_HAVE_UDBL
144 #else
145 typedef int32_t t_sint;
146 typedef uint32_t t_uint;
147 #if ( defined(_MSC_VER) && defined(_M_IX86) )
148 typedef uint64_t t_udbl;
149 #define POLARSSL_HAVE_UDBL
150 #else
151 #if defined( POLARSSL_HAVE_LONGLONG )
152 typedef unsigned long long t_udbl;
153 #define POLARSSL_HAVE_UDBL
154 #endif
155 #endif
156 #endif
Paul Bakker5c2364c2012-10-01 14:41:15 +0000157 #endif
158#endif /* POLARSSL_HAVE_INT16 */
159#endif /* POLARSSL_HAVE_INT8 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000160
161/**
162 * \brief MPI structure
163 */
164typedef struct
165{
166 int s; /*!< integer sign */
Paul Bakker23986e52011-04-24 08:57:21 +0000167 size_t n; /*!< total # of limbs */
Paul Bakkera755ca12011-04-24 09:11:17 +0000168 t_uint *p; /*!< pointer to limbs */
Paul Bakker5121ce52009-01-03 21:22:43 +0000169}
170mpi;
171
172#ifdef __cplusplus
173extern "C" {
174#endif
175
176/**
Paul Bakker6c591fa2011-05-05 11:49:20 +0000177 * \brief Initialize one MPI
178 *
179 * \param X One MPI to initialize.
Paul Bakker5121ce52009-01-03 21:22:43 +0000180 */
Paul Bakker6c591fa2011-05-05 11:49:20 +0000181void mpi_init( mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000182
183/**
Paul Bakker6c591fa2011-05-05 11:49:20 +0000184 * \brief Unallocate one MPI
185 *
186 * \param X One MPI to unallocate.
Paul Bakker5121ce52009-01-03 21:22:43 +0000187 */
Paul Bakker6c591fa2011-05-05 11:49:20 +0000188void mpi_free( mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000189
190/**
191 * \brief Enlarge to the specified number of limbs
192 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000193 * \param X MPI to grow
194 * \param nblimbs The target number of limbs
195 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000196 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000197 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000198 */
Paul Bakker23986e52011-04-24 08:57:21 +0000199int mpi_grow( mpi *X, size_t nblimbs );
Paul Bakker5121ce52009-01-03 21:22:43 +0000200
201/**
202 * \brief Copy the contents of Y into X
203 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000204 * \param X Destination MPI
205 * \param Y Source MPI
206 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000207 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000208 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000209 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000210int mpi_copy( mpi *X, const mpi *Y );
Paul Bakker5121ce52009-01-03 21:22:43 +0000211
212/**
213 * \brief Swap the contents of X and Y
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000214 *
215 * \param X First MPI value
216 * \param Y Second MPI value
Paul Bakker5121ce52009-01-03 21:22:43 +0000217 */
218void mpi_swap( mpi *X, mpi *Y );
219
220/**
221 * \brief Set value from integer
222 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000223 * \param X MPI to set
224 * \param z Value to use
225 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000226 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000227 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000228 */
Paul Bakkera755ca12011-04-24 09:11:17 +0000229int mpi_lset( mpi *X, t_sint z );
Paul Bakker5121ce52009-01-03 21:22:43 +0000230
Paul Bakker9a736322012-11-14 12:39:52 +0000231/**
Paul Bakker2f5947e2011-05-18 15:47:11 +0000232 * \brief Get a specific bit from X
233 *
234 * \param X MPI to use
235 * \param pos Zero-based index of the bit in X
236 *
237 * \return Either a 0 or a 1
238 */
Paul Bakker6b906e52012-05-08 12:01:43 +0000239int mpi_get_bit( const mpi *X, size_t pos );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000240
Paul Bakker9a736322012-11-14 12:39:52 +0000241/**
Paul Bakker2f5947e2011-05-18 15:47:11 +0000242 * \brief Set a bit of X to a specific value of 0 or 1
243 *
244 * \note Will grow X if necessary to set a bit to 1 in a not yet
245 * existing limb. Will not grow if bit should be set to 0
246 *
247 * \param X MPI to use
248 * \param pos Zero-based index of the bit in X
249 * \param val The value to set the bit to (0 or 1)
250 *
251 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000252 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
Paul Bakker2f5947e2011-05-18 15:47:11 +0000253 * POLARSSL_ERR_MPI_BAD_INPUT_DATA if val is not 0 or 1
254 */
255int mpi_set_bit( mpi *X, size_t pos, unsigned char val );
256
Paul Bakker5121ce52009-01-03 21:22:43 +0000257/**
Paul Bakker6b906e52012-05-08 12:01:43 +0000258 * \brief Return the number of zero-bits before the least significant
259 * '1' bit
260 *
261 * Note: Thus also the zero-based index of the least significant '1' bit
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000262 *
263 * \param X MPI to use
Paul Bakker5121ce52009-01-03 21:22:43 +0000264 */
Paul Bakker23986e52011-04-24 08:57:21 +0000265size_t mpi_lsb( const mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000266
267/**
Paul Bakker6b906e52012-05-08 12:01:43 +0000268 * \brief Return the number of bits up to and including the most
269 * significant '1' bit'
270 *
271 * Note: Thus also the one-based index of the most significant '1' bit
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000272 *
273 * \param X MPI to use
Paul Bakker5121ce52009-01-03 21:22:43 +0000274 */
Paul Bakker23986e52011-04-24 08:57:21 +0000275size_t mpi_msb( const mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000276
277/**
278 * \brief Return the total size in bytes
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000279 *
280 * \param X MPI to use
Paul Bakker5121ce52009-01-03 21:22:43 +0000281 */
Paul Bakker23986e52011-04-24 08:57:21 +0000282size_t mpi_size( const mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000283
284/**
285 * \brief Import from an ASCII string
286 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000287 * \param X Destination MPI
288 * \param radix Input numeric base
289 * \param s Null-terminated string buffer
Paul Bakker5121ce52009-01-03 21:22:43 +0000290 *
Paul Bakkercb37aa52011-11-30 16:00:20 +0000291 * \return 0 if successful, or a POLARSSL_ERR_MPI_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000292 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000293int mpi_read_string( mpi *X, int radix, const char *s );
Paul Bakker5121ce52009-01-03 21:22:43 +0000294
295/**
296 * \brief Export into an ASCII string
297 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000298 * \param X Source MPI
299 * \param radix Output numeric base
300 * \param s String buffer
301 * \param slen String buffer size
Paul Bakker5121ce52009-01-03 21:22:43 +0000302 *
Paul Bakkercb37aa52011-11-30 16:00:20 +0000303 * \return 0 if successful, or a POLARSSL_ERR_MPI_XXX error code.
Paul Bakkerff60ee62010-03-16 21:09:09 +0000304 * *slen is always updated to reflect the amount
305 * of data that has (or would have) been written.
Paul Bakker5121ce52009-01-03 21:22:43 +0000306 *
307 * \note Call this function with *slen = 0 to obtain the
308 * minimum required buffer size in *slen.
309 */
Paul Bakker23986e52011-04-24 08:57:21 +0000310int mpi_write_string( const mpi *X, int radix, char *s, size_t *slen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000311
Paul Bakker2b6af2f2012-10-23 11:08:02 +0000312#if defined(POLARSSL_FS_IO)
Paul Bakker5121ce52009-01-03 21:22:43 +0000313/**
314 * \brief Read X from an opened file
315 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000316 * \param X Destination MPI
317 * \param radix Input numeric base
318 * \param fin Input file handle
Paul Bakker5121ce52009-01-03 21:22:43 +0000319 *
Paul Bakkercb37aa52011-11-30 16:00:20 +0000320 * \return 0 if successful, POLARSSL_ERR_MPI_BUFFER_TOO_SMALL if
321 * the file read buffer is too small or a
322 * POLARSSL_ERR_MPI_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000323 */
324int mpi_read_file( mpi *X, int radix, FILE *fin );
325
326/**
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000327 * \brief Write X into an opened file, or stdout if fout is NULL
Paul Bakker5121ce52009-01-03 21:22:43 +0000328 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000329 * \param p Prefix, can be NULL
330 * \param X Source MPI
331 * \param radix Output numeric base
332 * \param fout Output file handle (can be NULL)
Paul Bakker5121ce52009-01-03 21:22:43 +0000333 *
Paul Bakkercb37aa52011-11-30 16:00:20 +0000334 * \return 0 if successful, or a POLARSSL_ERR_MPI_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000335 *
336 * \note Set fout == NULL to print X on the console.
337 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000338int mpi_write_file( const char *p, const mpi *X, int radix, FILE *fout );
Paul Bakker2b6af2f2012-10-23 11:08:02 +0000339#endif /* POLARSSL_FS_IO */
Paul Bakker5121ce52009-01-03 21:22:43 +0000340
341/**
342 * \brief Import X from unsigned binary data, big endian
343 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000344 * \param X Destination MPI
345 * \param buf Input buffer
346 * \param buflen Input buffer size
Paul Bakker5121ce52009-01-03 21:22:43 +0000347 *
348 * \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_read_binary( mpi *X, const unsigned char *buf, size_t buflen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000352
353/**
Manuel Pégourié-Gonnard002bc862014-06-25 12:57:47 +0200354 * \brief Export X into unsigned binary data, big endian.
355 * Always fills the whole buffer, which will start with zeros
356 * if the number is smaller.
Paul Bakker5121ce52009-01-03 21:22:43 +0000357 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000358 * \param X Source MPI
359 * \param buf Output buffer
360 * \param buflen Output buffer size
Paul Bakker5121ce52009-01-03 21:22:43 +0000361 *
362 * \return 0 if successful,
Paul Bakker40e46942009-01-03 21:51:57 +0000363 * POLARSSL_ERR_MPI_BUFFER_TOO_SMALL if buf isn't large enough
Paul Bakker5121ce52009-01-03 21:22:43 +0000364 */
Paul Bakker23986e52011-04-24 08:57:21 +0000365int mpi_write_binary( const mpi *X, unsigned char *buf, size_t buflen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000366
367/**
368 * \brief Left-shift: X <<= count
369 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000370 * \param X MPI to shift
371 * \param count Amount to shift
372 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000373 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000374 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000375 */
Paul Bakker23986e52011-04-24 08:57:21 +0000376int mpi_shift_l( mpi *X, size_t count );
Paul Bakker5121ce52009-01-03 21:22:43 +0000377
378/**
379 * \brief Right-shift: X >>= count
380 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000381 * \param X MPI to shift
382 * \param count Amount to shift
383 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000384 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000385 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000386 */
Paul Bakker23986e52011-04-24 08:57:21 +0000387int mpi_shift_r( mpi *X, size_t count );
Paul Bakker5121ce52009-01-03 21:22:43 +0000388
389/**
390 * \brief Compare unsigned values
391 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000392 * \param X Left-hand MPI
393 * \param Y Right-hand MPI
394 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000395 * \return 1 if |X| is greater than |Y|,
396 * -1 if |X| is lesser than |Y| or
397 * 0 if |X| is equal to |Y|
398 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000399int mpi_cmp_abs( const mpi *X, const mpi *Y );
Paul Bakker5121ce52009-01-03 21:22:43 +0000400
401/**
402 * \brief Compare signed values
403 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000404 * \param X Left-hand MPI
405 * \param Y Right-hand MPI
406 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000407 * \return 1 if X is greater than Y,
408 * -1 if X is lesser than Y or
409 * 0 if X is equal to Y
410 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000411int mpi_cmp_mpi( const mpi *X, const mpi *Y );
Paul Bakker5121ce52009-01-03 21:22:43 +0000412
413/**
414 * \brief Compare signed values
415 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000416 * \param X Left-hand MPI
417 * \param z The integer value to compare to
418 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000419 * \return 1 if X is greater than z,
420 * -1 if X is lesser than z or
421 * 0 if X is equal to z
422 */
Paul Bakkera755ca12011-04-24 09:11:17 +0000423int mpi_cmp_int( const mpi *X, t_sint z );
Paul Bakker5121ce52009-01-03 21:22:43 +0000424
425/**
426 * \brief Unsigned addition: 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_add_abs( mpi *X, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000436
437/**
438 * \brief Unsigned substraction: 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 Right-hand MPI
443 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000444 * \return 0 if successful,
Paul Bakker40e46942009-01-03 21:51:57 +0000445 * POLARSSL_ERR_MPI_NEGATIVE_VALUE if B is greater than A
Paul Bakker5121ce52009-01-03 21:22:43 +0000446 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000447int mpi_sub_abs( mpi *X, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000448
449/**
450 * \brief Signed addition: 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 Right-hand MPI
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 Bakkerff60ee62010-03-16 21:09:09 +0000459int mpi_add_mpi( mpi *X, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000460
461/**
462 * \brief Signed substraction: 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_sub_mpi( mpi *X, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000472
473/**
474 * \brief Signed addition: X = A + b
475 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000476 * \param X Destination MPI
477 * \param A Left-hand MPI
478 * \param b The integer value to add
479 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000480 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000481 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000482 */
Paul Bakkera755ca12011-04-24 09:11:17 +0000483int mpi_add_int( mpi *X, const mpi *A, t_sint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000484
485/**
486 * \brief Signed substraction: X = A - b
487 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000488 * \param X Destination MPI
489 * \param A Left-hand MPI
490 * \param b The integer value to subtract
491 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000492 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000493 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000494 */
Paul Bakkera755ca12011-04-24 09:11:17 +0000495int mpi_sub_int( mpi *X, const mpi *A, t_sint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000496
497/**
498 * \brief Baseline multiplication: X = A * B
499 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000500 * \param X Destination MPI
501 * \param A Left-hand MPI
502 * \param B Right-hand MPI
503 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000504 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000505 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000506 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000507int mpi_mul_mpi( mpi *X, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000508
509/**
510 * \brief Baseline multiplication: X = A * b
Manuel Pégourié-Gonnard7ab2d5d2013-11-25 16:16:33 +0100511 * Note: despite the functon signature, b is treated as a
512 * t_uint. Negative values of b are treated as large positive
513 * values.
Paul Bakker5121ce52009-01-03 21:22:43 +0000514 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000515 * \param X Destination MPI
516 * \param A Left-hand MPI
517 * \param b The integer value to multiply with
518 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000519 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000520 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000521 */
Paul Bakkera755ca12011-04-24 09:11:17 +0000522int mpi_mul_int( mpi *X, const mpi *A, t_sint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000523
524/**
525 * \brief Division by mpi: A = Q * B + R
526 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000527 * \param Q Destination MPI for the quotient
528 * \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 Bakker40e46942009-01-03 21:51:57 +0000534 * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if B == 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000535 *
536 * \note Either Q or R can be NULL.
537 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000538int mpi_div_mpi( mpi *Q, mpi *R, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000539
540/**
541 * \brief Division by int: A = Q * b + R
542 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000543 * \param Q Destination MPI for the quotient
544 * \param R Destination MPI for the rest value
545 * \param A Left-hand MPI
546 * \param b Integer to divide by
547 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000548 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000549 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000550 * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if b == 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000551 *
552 * \note Either Q or R can be NULL.
553 */
Paul Bakkera755ca12011-04-24 09:11:17 +0000554int mpi_div_int( mpi *Q, mpi *R, const mpi *A, t_sint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000555
556/**
557 * \brief Modulo: R = A mod B
558 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000559 * \param R Destination MPI for the rest value
560 * \param A Left-hand MPI
561 * \param B Right-hand MPI
562 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000563 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000564 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
Paul Bakkerce40a6d2009-06-23 19:46:08 +0000565 * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if B == 0,
566 * POLARSSL_ERR_MPI_NEGATIVE_VALUE if B < 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000567 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000568int mpi_mod_mpi( mpi *R, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000569
570/**
571 * \brief Modulo: r = A mod b
572 *
Paul Bakkera755ca12011-04-24 09:11:17 +0000573 * \param r Destination t_uint
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000574 * \param A Left-hand MPI
575 * \param b Integer to divide by
576 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000577 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000578 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
Paul Bakkerce40a6d2009-06-23 19:46:08 +0000579 * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if b == 0,
580 * POLARSSL_ERR_MPI_NEGATIVE_VALUE if b < 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000581 */
Paul Bakkera755ca12011-04-24 09:11:17 +0000582int mpi_mod_int( t_uint *r, const mpi *A, t_sint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000583
584/**
585 * \brief Sliding-window exponentiation: X = A^E mod N
586 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000587 * \param X Destination MPI
588 * \param A Left-hand MPI
589 * \param E Exponent MPI
590 * \param N Modular MPI
591 * \param _RR Speed-up MPI used for recalculations
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 Bakkerf6198c12012-05-16 08:02:29 +0000595 * POLARSSL_ERR_MPI_BAD_INPUT_DATA if N is negative or even or if
596 * E is negative
Paul Bakker5121ce52009-01-03 21:22:43 +0000597 *
598 * \note _RR is used to avoid re-computing R*R mod N across
599 * multiple calls, which speeds up things a bit. It can
600 * be set to NULL if the extra performance is unneeded.
601 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000602int mpi_exp_mod( mpi *X, const mpi *A, const mpi *E, const mpi *N, mpi *_RR );
Paul Bakker5121ce52009-01-03 21:22:43 +0000603
604/**
Paul Bakker287781a2011-03-26 13:18:49 +0000605 * \brief Fill an MPI X with size bytes of random
606 *
607 * \param X Destination MPI
608 * \param size Size in bytes
609 * \param f_rng RNG function
610 * \param p_rng RNG parameter
611 *
612 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000613 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker287781a2011-03-26 13:18:49 +0000614 */
Paul Bakkera3d195c2011-11-27 21:07:34 +0000615int mpi_fill_random( mpi *X, size_t size,
616 int (*f_rng)(void *, unsigned char *, size_t),
617 void *p_rng );
Paul Bakker287781a2011-03-26 13:18:49 +0000618
619/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000620 * \brief Greatest common divisor: G = gcd(A, B)
621 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000622 * \param G Destination MPI
623 * \param A Left-hand MPI
624 * \param B Right-hand MPI
625 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000626 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000627 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000628 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000629int mpi_gcd( mpi *G, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000630
631/**
632 * \brief Modular inverse: X = A^-1 mod N
633 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000634 * \param X Destination MPI
635 * \param A Left-hand MPI
636 * \param N Right-hand MPI
637 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000638 * \return 0 if successful,
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 N is negative or nil
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000641 POLARSSL_ERR_MPI_NOT_ACCEPTABLE if A has no inverse mod N
Paul Bakker5121ce52009-01-03 21:22:43 +0000642 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000643int mpi_inv_mod( mpi *X, const mpi *A, const mpi *N );
Paul Bakker5121ce52009-01-03 21:22:43 +0000644
645/**
646 * \brief Miller-Rabin primality test
647 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000648 * \param X MPI to check
649 * \param f_rng RNG function
650 * \param p_rng RNG parameter
651 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000652 * \return 0 if successful (probably prime),
Paul Bakker69e095c2011-12-10 21:55:01 +0000653 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000654 * POLARSSL_ERR_MPI_NOT_ACCEPTABLE if X is not prime
Paul Bakker5121ce52009-01-03 21:22:43 +0000655 */
Paul Bakkera3d195c2011-11-27 21:07:34 +0000656int mpi_is_prime( mpi *X,
657 int (*f_rng)(void *, unsigned char *, size_t),
658 void *p_rng );
Paul Bakker5121ce52009-01-03 21:22:43 +0000659
660/**
661 * \brief Prime number generation
662 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000663 * \param X Destination MPI
Paul Bakkerfe3256e2011-11-25 12:11:43 +0000664 * \param nbits Required size of X in bits ( 3 <= nbits <= POLARSSL_MPI_MAX_BITS )
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000665 * \param dh_flag If 1, then (X-1)/2 will be prime too
Paul Bakker5121ce52009-01-03 21:22:43 +0000666 * \param f_rng RNG function
667 * \param p_rng RNG parameter
668 *
669 * \return 0 if successful (probably prime),
Paul Bakker69e095c2011-12-10 21:55:01 +0000670 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000671 * POLARSSL_ERR_MPI_BAD_INPUT_DATA if nbits is < 3
Paul Bakker5121ce52009-01-03 21:22:43 +0000672 */
Paul Bakker23986e52011-04-24 08:57:21 +0000673int mpi_gen_prime( mpi *X, size_t nbits, int dh_flag,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000674 int (*f_rng)(void *, unsigned char *, size_t),
675 void *p_rng );
Paul Bakker5121ce52009-01-03 21:22:43 +0000676
677/**
678 * \brief Checkup routine
679 *
680 * \return 0 if successful, or 1 if the test failed
681 */
682int mpi_self_test( int verbose );
683
684#ifdef __cplusplus
685}
686#endif
687
688#endif /* bignum.h */