blob: 769e546d5108b30c9f755bb7077ff2b6d29c1746 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/**
2 * \file bignum.h
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00003 *
Paul Bakker37ca75d2011-01-06 12:28:03 +00004 * \brief Multi-precision integer library
5 *
Paul Bakker9bcf16c2013-06-24 19:31:17 +02006 * Copyright (C) 2006-2013, Brainspark B.V.
Paul Bakkerb96f1542010-07-18 20:36:00 +00007 *
8 * This file is part of PolarSSL (http://www.polarssl.org)
Paul Bakker84f12b72010-07-18 10:13:04 +00009 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
Paul Bakkerb96f1542010-07-18 20:36:00 +000010 *
Paul Bakker77b385e2009-07-28 17:23:11 +000011 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000012 *
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000013 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License along
24 * with this program; if not, write to the Free Software Foundation, Inc.,
25 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Paul Bakker5121ce52009-01-03 21:22:43 +000026 */
Paul Bakker40e46942009-01-03 21:51:57 +000027#ifndef POLARSSL_BIGNUM_H
28#define POLARSSL_BIGNUM_H
Paul Bakker5121ce52009-01-03 21:22:43 +000029
30#include <stdio.h>
Paul Bakker23986e52011-04-24 08:57:21 +000031#include <string.h>
Paul Bakker5121ce52009-01-03 21:22:43 +000032
Paul Bakkercf0360a2012-01-20 10:08:14 +000033#include "config.h"
34
Paul 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 Bakker9bcf16c2013-06-24 19:31:17 +020068#if !defined(POLARSSL_CONFIG_OPTIONS)
Paul Bakkerf9688572011-05-05 10:00:45 +000069/*
Paul Bakkerb6d5f082011-11-25 11:52:11 +000070 * Maximum window size used for modular exponentiation. Default: 6
71 * Minimum value: 1. Maximum value: 6.
72 *
73 * Result is an array of ( 2 << POLARSSL_MPI_WINDOW_SIZE ) MPIs used
74 * for the sliding window calculation. (So 64 by default)
75 *
76 * Reduction in size, reduces speed.
77 */
78#define POLARSSL_MPI_WINDOW_SIZE 6 /**< Maximum windows size used. */
79
80/*
Paul Bakkerfe3256e2011-11-25 12:11:43 +000081 * Maximum size of MPIs allowed in bits and bytes for user-MPIs.
Paul Bakkerf626e1d2013-01-21 12:10:00 +010082 * ( Default: 512 bytes => 4096 bits, Maximum tested: 2048 bytes => 16384 bits )
Paul Bakkerfe3256e2011-11-25 12:11:43 +000083 *
84 * Note: Calculations can results temporarily in larger MPIs. So the number
85 * of limbs required (POLARSSL_MPI_MAX_LIMBS) is higher.
86 */
87#define POLARSSL_MPI_MAX_SIZE 512 /**< Maximum number of bytes for usable MPIs. */
Paul Bakker9bcf16c2013-06-24 19:31:17 +020088
89#endif /* !POLARSSL_CONFIG_OPTIONS */
90
Paul Bakkerfe3256e2011-11-25 12:11:43 +000091#define POLARSSL_MPI_MAX_BITS ( 8 * POLARSSL_MPI_MAX_SIZE ) /**< Maximum number of bits for usable MPIs. */
92
93/*
Paul Bakker5531c6d2012-09-26 19:20:46 +000094 * When reading from files with mpi_read_file() and writing to files with
95 * mpi_write_file() the buffer should have space
Paul Bakkercb37aa52011-11-30 16:00:20 +000096 * for a (short) label, the MPI (in the provided radix), the newline
97 * characters and the '\0'.
98 *
99 * By default we assume at least a 10 char label, a minimum radix of 10
100 * (decimal) and a maximum of 4096 bit numbers (1234 decimal chars).
Paul Bakkerf9183102012-09-27 20:42:35 +0000101 * Autosized at compile time for at least a 10 char label, a minimum radix
102 * of 10 (decimal) for a number of POLARSSL_MPI_MAX_BITS size.
103 *
104 * This used to be statically sized to 1250 for a maximum of 4096 bit
105 * numbers (1234 decimal chars).
106 *
107 * Calculate using the formula:
108 * POLARSSL_MPI_RW_BUFFER_SIZE = ceil(POLARSSL_MPI_MAX_BITS / ln(10) * ln(2)) +
109 * LabelSize + 6
Paul Bakkercb37aa52011-11-30 16:00:20 +0000110 */
Paul Bakkerf9183102012-09-27 20:42:35 +0000111#define POLARSSL_MPI_MAX_BITS_SCALE100 ( 100 * POLARSSL_MPI_MAX_BITS )
112#define LN_2_DIV_LN_10_SCALE100 332
113#define POLARSSL_MPI_RW_BUFFER_SIZE ( ((POLARSSL_MPI_MAX_BITS_SCALE100 + LN_2_DIV_LN_10_SCALE100 - 1) / LN_2_DIV_LN_10_SCALE100) + 10 + 6 )
Paul Bakkercb37aa52011-11-30 16:00:20 +0000114
115/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000116 * Define the base integer type, architecture-wise
117 */
Paul Bakker40e46942009-01-03 21:51:57 +0000118#if defined(POLARSSL_HAVE_INT8)
Paul Bakkera755ca12011-04-24 09:11:17 +0000119typedef signed char t_sint;
120typedef unsigned char t_uint;
Paul Bakker5c2364c2012-10-01 14:41:15 +0000121typedef uint16_t t_udbl;
Paul Bakker62261d62012-10-02 12:19:31 +0000122#define POLARSSL_HAVE_UDBL
Paul Bakker5121ce52009-01-03 21:22:43 +0000123#else
Paul Bakker40e46942009-01-03 21:51:57 +0000124#if defined(POLARSSL_HAVE_INT16)
Paul Bakker5c2364c2012-10-01 14:41:15 +0000125typedef int16_t t_sint;
126typedef uint16_t t_uint;
127typedef uint32_t t_udbl;
Paul Bakker62261d62012-10-02 12:19:31 +0000128#define POLARSSL_HAVE_UDBL
Paul Bakker5121ce52009-01-03 21:22:43 +0000129#else
Paul Bakker9f2018e2013-02-27 15:01:34 +0100130 #if ( defined(_MSC_VER) && defined(_M_AMD64) )
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
Manuel Pégourié-Gonnarda47e7052013-10-21 17:51:45 +0200145 #define POLARSSL_HAVE_INT32
Paul Bakker62261d62012-10-02 12:19:31 +0000146 typedef int32_t t_sint;
147 typedef uint32_t t_uint;
148 #if ( defined(_MSC_VER) && defined(_M_IX86) )
149 typedef uint64_t t_udbl;
150 #define POLARSSL_HAVE_UDBL
151 #else
152 #if defined( POLARSSL_HAVE_LONGLONG )
153 typedef unsigned long long t_udbl;
154 #define POLARSSL_HAVE_UDBL
155 #endif
156 #endif
157 #endif
Paul Bakker5c2364c2012-10-01 14:41:15 +0000158 #endif
159#endif /* POLARSSL_HAVE_INT16 */
160#endif /* POLARSSL_HAVE_INT8 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000161
Paul Bakker407a0da2013-06-27 14:29:21 +0200162#ifdef __cplusplus
163extern "C" {
164#endif
165
Paul Bakker5121ce52009-01-03 21:22:43 +0000166/**
167 * \brief MPI structure
168 */
169typedef struct
170{
171 int s; /*!< integer sign */
Paul Bakker23986e52011-04-24 08:57:21 +0000172 size_t n; /*!< total # of limbs */
Paul Bakkera755ca12011-04-24 09:11:17 +0000173 t_uint *p; /*!< pointer to limbs */
Paul Bakker5121ce52009-01-03 21:22:43 +0000174}
175mpi;
176
Paul Bakker5121ce52009-01-03 21:22:43 +0000177/**
Paul Bakker6c591fa2011-05-05 11:49:20 +0000178 * \brief Initialize one MPI
179 *
180 * \param X One MPI to initialize.
Paul Bakker5121ce52009-01-03 21:22:43 +0000181 */
Paul Bakker6c591fa2011-05-05 11:49:20 +0000182void mpi_init( mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000183
184/**
Paul Bakker6c591fa2011-05-05 11:49:20 +0000185 * \brief Unallocate one MPI
186 *
187 * \param X One MPI to unallocate.
Paul Bakker5121ce52009-01-03 21:22:43 +0000188 */
Paul Bakker6c591fa2011-05-05 11:49:20 +0000189void mpi_free( mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000190
191/**
192 * \brief Enlarge to the specified number of limbs
193 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000194 * \param X MPI to grow
195 * \param nblimbs The target number of limbs
196 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000197 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000198 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000199 */
Paul Bakker23986e52011-04-24 08:57:21 +0000200int mpi_grow( mpi *X, size_t nblimbs );
Paul Bakker5121ce52009-01-03 21:22:43 +0000201
202/**
203 * \brief Copy the contents of Y into X
204 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000205 * \param X Destination MPI
206 * \param Y Source MPI
207 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000208 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000209 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000210 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000211int mpi_copy( mpi *X, const mpi *Y );
Paul Bakker5121ce52009-01-03 21:22:43 +0000212
213/**
214 * \brief Swap the contents of X and Y
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000215 *
216 * \param X First MPI value
217 * \param Y Second MPI value
Paul Bakker5121ce52009-01-03 21:22:43 +0000218 */
219void mpi_swap( mpi *X, mpi *Y );
220
221/**
222 * \brief Set value from integer
223 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000224 * \param X MPI to set
225 * \param z Value to use
226 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000227 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000228 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000229 */
Paul Bakkera755ca12011-04-24 09:11:17 +0000230int mpi_lset( mpi *X, t_sint z );
Paul Bakker5121ce52009-01-03 21:22:43 +0000231
Paul Bakker9a736322012-11-14 12:39:52 +0000232/**
Paul Bakker2f5947e2011-05-18 15:47:11 +0000233 * \brief Get a specific bit from X
234 *
235 * \param X MPI to use
236 * \param pos Zero-based index of the bit in X
237 *
238 * \return Either a 0 or a 1
239 */
Paul Bakker6b906e52012-05-08 12:01:43 +0000240int mpi_get_bit( const mpi *X, size_t pos );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000241
Paul Bakker9a736322012-11-14 12:39:52 +0000242/**
Paul Bakker2f5947e2011-05-18 15:47:11 +0000243 * \brief Set a bit of X to a specific value of 0 or 1
244 *
245 * \note Will grow X if necessary to set a bit to 1 in a not yet
246 * existing limb. Will not grow if bit should be set to 0
247 *
248 * \param X MPI to use
249 * \param pos Zero-based index of the bit in X
250 * \param val The value to set the bit to (0 or 1)
251 *
252 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000253 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
Paul Bakker2f5947e2011-05-18 15:47:11 +0000254 * POLARSSL_ERR_MPI_BAD_INPUT_DATA if val is not 0 or 1
255 */
256int mpi_set_bit( mpi *X, size_t pos, unsigned char val );
257
Paul Bakker5121ce52009-01-03 21:22:43 +0000258/**
Paul Bakker6b906e52012-05-08 12:01:43 +0000259 * \brief Return the number of zero-bits before the least significant
260 * '1' bit
261 *
262 * Note: Thus also the zero-based index of the least significant '1' bit
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000263 *
264 * \param X MPI to use
Paul Bakker5121ce52009-01-03 21:22:43 +0000265 */
Paul Bakker23986e52011-04-24 08:57:21 +0000266size_t mpi_lsb( const mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000267
268/**
Paul Bakker6b906e52012-05-08 12:01:43 +0000269 * \brief Return the number of bits up to and including the most
270 * significant '1' bit'
271 *
272 * Note: Thus also the one-based index of the most significant '1' bit
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000273 *
274 * \param X MPI to use
Paul Bakker5121ce52009-01-03 21:22:43 +0000275 */
Paul Bakker23986e52011-04-24 08:57:21 +0000276size_t mpi_msb( const mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000277
278/**
279 * \brief Return the total size in bytes
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000280 *
281 * \param X MPI to use
Paul Bakker5121ce52009-01-03 21:22:43 +0000282 */
Paul Bakker23986e52011-04-24 08:57:21 +0000283size_t mpi_size( const mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000284
285/**
286 * \brief Import from an ASCII string
287 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000288 * \param X Destination MPI
289 * \param radix Input numeric base
290 * \param s Null-terminated string buffer
Paul Bakker5121ce52009-01-03 21:22:43 +0000291 *
Paul Bakkercb37aa52011-11-30 16:00:20 +0000292 * \return 0 if successful, or a POLARSSL_ERR_MPI_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000293 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000294int mpi_read_string( mpi *X, int radix, const char *s );
Paul Bakker5121ce52009-01-03 21:22:43 +0000295
296/**
297 * \brief Export into an ASCII string
298 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000299 * \param X Source MPI
300 * \param radix Output numeric base
301 * \param s String buffer
302 * \param slen String buffer size
Paul Bakker5121ce52009-01-03 21:22:43 +0000303 *
Paul Bakkercb37aa52011-11-30 16:00:20 +0000304 * \return 0 if successful, or a POLARSSL_ERR_MPI_XXX error code.
Paul Bakkerff60ee62010-03-16 21:09:09 +0000305 * *slen is always updated to reflect the amount
306 * of data that has (or would have) been written.
Paul Bakker5121ce52009-01-03 21:22:43 +0000307 *
308 * \note Call this function with *slen = 0 to obtain the
309 * minimum required buffer size in *slen.
310 */
Paul Bakker23986e52011-04-24 08:57:21 +0000311int mpi_write_string( const mpi *X, int radix, char *s, size_t *slen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000312
Paul Bakker2b6af2f2012-10-23 11:08:02 +0000313#if defined(POLARSSL_FS_IO)
Paul Bakker5121ce52009-01-03 21:22:43 +0000314/**
315 * \brief Read X from an opened file
316 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000317 * \param X Destination MPI
318 * \param radix Input numeric base
319 * \param fin Input file handle
Paul Bakker5121ce52009-01-03 21:22:43 +0000320 *
Paul Bakkercb37aa52011-11-30 16:00:20 +0000321 * \return 0 if successful, POLARSSL_ERR_MPI_BUFFER_TOO_SMALL if
322 * the file read buffer is too small or a
323 * POLARSSL_ERR_MPI_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000324 */
325int mpi_read_file( mpi *X, int radix, FILE *fin );
326
327/**
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000328 * \brief Write X into an opened file, or stdout if fout is NULL
Paul Bakker5121ce52009-01-03 21:22:43 +0000329 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000330 * \param p Prefix, can be NULL
331 * \param X Source MPI
332 * \param radix Output numeric base
333 * \param fout Output file handle (can be NULL)
Paul Bakker5121ce52009-01-03 21:22:43 +0000334 *
Paul Bakkercb37aa52011-11-30 16:00:20 +0000335 * \return 0 if successful, or a POLARSSL_ERR_MPI_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000336 *
337 * \note Set fout == NULL to print X on the console.
338 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000339int mpi_write_file( const char *p, const mpi *X, int radix, FILE *fout );
Paul Bakker2b6af2f2012-10-23 11:08:02 +0000340#endif /* POLARSSL_FS_IO */
Paul Bakker5121ce52009-01-03 21:22:43 +0000341
342/**
343 * \brief Import X from unsigned binary data, big endian
344 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000345 * \param X Destination MPI
346 * \param buf Input buffer
347 * \param buflen Input buffer size
Paul Bakker5121ce52009-01-03 21:22:43 +0000348 *
349 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000350 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000351 */
Paul Bakker23986e52011-04-24 08:57:21 +0000352int mpi_read_binary( mpi *X, const unsigned char *buf, size_t buflen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000353
354/**
355 * \brief Export X into unsigned binary data, big endian
356 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000357 * \param X Source MPI
358 * \param buf Output buffer
359 * \param buflen Output buffer size
Paul Bakker5121ce52009-01-03 21:22:43 +0000360 *
361 * \return 0 if successful,
Paul Bakker40e46942009-01-03 21:51:57 +0000362 * POLARSSL_ERR_MPI_BUFFER_TOO_SMALL if buf isn't large enough
Paul Bakker5121ce52009-01-03 21:22:43 +0000363 */
Paul Bakker23986e52011-04-24 08:57:21 +0000364int mpi_write_binary( const mpi *X, unsigned char *buf, size_t buflen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000365
366/**
367 * \brief Left-shift: X <<= count
368 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000369 * \param X MPI to shift
370 * \param count Amount to shift
371 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000372 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000373 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000374 */
Paul Bakker23986e52011-04-24 08:57:21 +0000375int mpi_shift_l( mpi *X, size_t count );
Paul Bakker5121ce52009-01-03 21:22:43 +0000376
377/**
378 * \brief Right-shift: X >>= count
379 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000380 * \param X MPI to shift
381 * \param count Amount to shift
382 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000383 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000384 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000385 */
Paul Bakker23986e52011-04-24 08:57:21 +0000386int mpi_shift_r( mpi *X, size_t count );
Paul Bakker5121ce52009-01-03 21:22:43 +0000387
388/**
389 * \brief Compare unsigned values
390 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000391 * \param X Left-hand MPI
392 * \param Y Right-hand MPI
393 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000394 * \return 1 if |X| is greater than |Y|,
395 * -1 if |X| is lesser than |Y| or
396 * 0 if |X| is equal to |Y|
397 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000398int mpi_cmp_abs( const mpi *X, const mpi *Y );
Paul Bakker5121ce52009-01-03 21:22:43 +0000399
400/**
401 * \brief Compare signed values
402 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000403 * \param X Left-hand MPI
404 * \param Y Right-hand MPI
405 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000406 * \return 1 if X is greater than Y,
407 * -1 if X is lesser than Y or
408 * 0 if X is equal to Y
409 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000410int mpi_cmp_mpi( const mpi *X, const mpi *Y );
Paul Bakker5121ce52009-01-03 21:22:43 +0000411
412/**
413 * \brief Compare signed values
414 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000415 * \param X Left-hand MPI
416 * \param z The integer value to compare to
417 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000418 * \return 1 if X is greater than z,
419 * -1 if X is lesser than z or
420 * 0 if X is equal to z
421 */
Paul Bakkera755ca12011-04-24 09:11:17 +0000422int mpi_cmp_int( const mpi *X, t_sint z );
Paul Bakker5121ce52009-01-03 21:22:43 +0000423
424/**
425 * \brief Unsigned addition: X = |A| + |B|
426 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000427 * \param X Destination MPI
428 * \param A Left-hand MPI
429 * \param B Right-hand MPI
430 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000431 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000432 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000433 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000434int mpi_add_abs( mpi *X, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000435
436/**
437 * \brief Unsigned substraction: X = |A| - |B|
438 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000439 * \param X Destination MPI
440 * \param A Left-hand MPI
441 * \param B Right-hand MPI
442 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000443 * \return 0 if successful,
Paul Bakker40e46942009-01-03 21:51:57 +0000444 * POLARSSL_ERR_MPI_NEGATIVE_VALUE if B is greater than A
Paul Bakker5121ce52009-01-03 21:22:43 +0000445 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000446int mpi_sub_abs( mpi *X, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000447
448/**
449 * \brief Signed addition: X = A + B
450 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000451 * \param X Destination MPI
452 * \param A Left-hand MPI
453 * \param B Right-hand MPI
454 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000455 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000456 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000457 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000458int mpi_add_mpi( mpi *X, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000459
460/**
461 * \brief Signed substraction: X = A - B
462 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000463 * \param X Destination MPI
464 * \param A Left-hand MPI
465 * \param B Right-hand MPI
466 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000467 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000468 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000469 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000470int mpi_sub_mpi( mpi *X, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000471
472/**
473 * \brief Signed addition: X = A + b
474 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000475 * \param X Destination MPI
476 * \param A Left-hand MPI
477 * \param b The integer value to add
478 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000479 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000480 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000481 */
Paul Bakkera755ca12011-04-24 09:11:17 +0000482int mpi_add_int( mpi *X, const mpi *A, t_sint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000483
484/**
485 * \brief Signed substraction: X = A - b
486 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000487 * \param X Destination MPI
488 * \param A Left-hand MPI
489 * \param b The integer value to subtract
490 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000491 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000492 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000493 */
Paul Bakkera755ca12011-04-24 09:11:17 +0000494int mpi_sub_int( mpi *X, const mpi *A, t_sint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000495
496/**
497 * \brief Baseline multiplication: X = A * B
498 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000499 * \param X Destination MPI
500 * \param A Left-hand MPI
501 * \param B Right-hand MPI
502 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000503 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000504 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000505 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000506int mpi_mul_mpi( mpi *X, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000507
508/**
509 * \brief Baseline multiplication: X = A * b
Paul Bakkerce40a6d2009-06-23 19:46:08 +0000510 * Note: b is an unsigned integer type, thus
511 * Negative values of b are ignored.
Paul Bakker5121ce52009-01-03 21:22:43 +0000512 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000513 * \param X Destination MPI
514 * \param A Left-hand MPI
515 * \param b The integer value to multiply with
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 Bakker5121ce52009-01-03 21:22:43 +0000519 */
Paul Bakkera755ca12011-04-24 09:11:17 +0000520int mpi_mul_int( mpi *X, const mpi *A, t_sint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000521
522/**
523 * \brief Division by mpi: A = Q * B + R
524 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000525 * \param Q Destination MPI for the quotient
526 * \param R Destination MPI for the rest value
527 * \param A Left-hand MPI
528 * \param B Right-hand MPI
529 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000530 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000531 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000532 * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if B == 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000533 *
534 * \note Either Q or R can be NULL.
535 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000536int mpi_div_mpi( mpi *Q, mpi *R, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000537
538/**
539 * \brief Division by int: A = Q * b + R
540 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000541 * \param Q Destination MPI for the quotient
542 * \param R Destination MPI for the rest value
543 * \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 Bakker40e46942009-01-03 21:51:57 +0000548 * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if b == 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000549 *
550 * \note Either Q or R can be NULL.
551 */
Paul Bakkera755ca12011-04-24 09:11:17 +0000552int mpi_div_int( mpi *Q, mpi *R, const mpi *A, t_sint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000553
554/**
555 * \brief Modulo: R = A mod B
556 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000557 * \param R Destination MPI for the rest value
558 * \param A Left-hand MPI
559 * \param B Right-hand MPI
560 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000561 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000562 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
Paul Bakkerce40a6d2009-06-23 19:46:08 +0000563 * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if B == 0,
564 * POLARSSL_ERR_MPI_NEGATIVE_VALUE if B < 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000565 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000566int mpi_mod_mpi( mpi *R, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000567
568/**
569 * \brief Modulo: r = A mod b
570 *
Paul Bakkera755ca12011-04-24 09:11:17 +0000571 * \param r Destination t_uint
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000572 * \param A Left-hand MPI
573 * \param b Integer to divide by
574 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000575 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000576 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
Paul Bakkerce40a6d2009-06-23 19:46:08 +0000577 * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if b == 0,
578 * POLARSSL_ERR_MPI_NEGATIVE_VALUE if b < 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000579 */
Paul Bakkera755ca12011-04-24 09:11:17 +0000580int mpi_mod_int( t_uint *r, const mpi *A, t_sint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000581
582/**
583 * \brief Sliding-window exponentiation: X = A^E mod N
584 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000585 * \param X Destination MPI
586 * \param A Left-hand MPI
587 * \param E Exponent MPI
588 * \param N Modular MPI
589 * \param _RR Speed-up MPI used for recalculations
590 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000591 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000592 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
Paul Bakkerf6198c12012-05-16 08:02:29 +0000593 * POLARSSL_ERR_MPI_BAD_INPUT_DATA if N is negative or even or if
594 * E is negative
Paul Bakker5121ce52009-01-03 21:22:43 +0000595 *
596 * \note _RR is used to avoid re-computing R*R mod N across
597 * multiple calls, which speeds up things a bit. It can
598 * be set to NULL if the extra performance is unneeded.
599 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000600int mpi_exp_mod( mpi *X, const mpi *A, const mpi *E, const mpi *N, mpi *_RR );
Paul Bakker5121ce52009-01-03 21:22:43 +0000601
602/**
Paul Bakker287781a2011-03-26 13:18:49 +0000603 * \brief Fill an MPI X with size bytes of random
604 *
605 * \param X Destination MPI
606 * \param size Size in bytes
607 * \param f_rng RNG function
608 * \param p_rng RNG parameter
609 *
610 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000611 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker287781a2011-03-26 13:18:49 +0000612 */
Paul Bakkera3d195c2011-11-27 21:07:34 +0000613int mpi_fill_random( mpi *X, size_t size,
614 int (*f_rng)(void *, unsigned char *, size_t),
615 void *p_rng );
Paul Bakker287781a2011-03-26 13:18:49 +0000616
617/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000618 * \brief Greatest common divisor: G = gcd(A, B)
619 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000620 * \param G Destination MPI
621 * \param A Left-hand MPI
622 * \param B Right-hand MPI
623 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000624 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000625 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000626 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000627int mpi_gcd( mpi *G, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000628
629/**
630 * \brief Modular inverse: X = A^-1 mod N
631 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000632 * \param X Destination MPI
633 * \param A Left-hand MPI
634 * \param N Right-hand MPI
635 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000636 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000637 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000638 * POLARSSL_ERR_MPI_BAD_INPUT_DATA if N is negative or nil
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000639 POLARSSL_ERR_MPI_NOT_ACCEPTABLE if A has no inverse mod N
Paul Bakker5121ce52009-01-03 21:22:43 +0000640 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000641int mpi_inv_mod( mpi *X, const mpi *A, const mpi *N );
Paul Bakker5121ce52009-01-03 21:22:43 +0000642
643/**
644 * \brief Miller-Rabin primality test
645 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000646 * \param X MPI to check
647 * \param f_rng RNG function
648 * \param p_rng RNG parameter
649 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000650 * \return 0 if successful (probably prime),
Paul Bakker69e095c2011-12-10 21:55:01 +0000651 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000652 * POLARSSL_ERR_MPI_NOT_ACCEPTABLE if X is not prime
Paul Bakker5121ce52009-01-03 21:22:43 +0000653 */
Paul Bakkera3d195c2011-11-27 21:07:34 +0000654int mpi_is_prime( mpi *X,
655 int (*f_rng)(void *, unsigned char *, size_t),
656 void *p_rng );
Paul Bakker5121ce52009-01-03 21:22:43 +0000657
658/**
659 * \brief Prime number generation
660 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000661 * \param X Destination MPI
Paul Bakkerfe3256e2011-11-25 12:11:43 +0000662 * \param nbits Required size of X in bits ( 3 <= nbits <= POLARSSL_MPI_MAX_BITS )
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000663 * \param dh_flag If 1, then (X-1)/2 will be prime too
Paul Bakker5121ce52009-01-03 21:22:43 +0000664 * \param f_rng RNG function
665 * \param p_rng RNG parameter
666 *
667 * \return 0 if successful (probably prime),
Paul Bakker69e095c2011-12-10 21:55:01 +0000668 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000669 * POLARSSL_ERR_MPI_BAD_INPUT_DATA if nbits is < 3
Paul Bakker5121ce52009-01-03 21:22:43 +0000670 */
Paul Bakker23986e52011-04-24 08:57:21 +0000671int mpi_gen_prime( mpi *X, size_t nbits, int dh_flag,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000672 int (*f_rng)(void *, unsigned char *, size_t),
673 void *p_rng );
Paul Bakker5121ce52009-01-03 21:22:43 +0000674
675/**
676 * \brief Checkup routine
677 *
678 * \return 0 if successful, or 1 if the test failed
679 */
680int mpi_self_test( int verbose );
681
682#ifdef __cplusplus
683}
684#endif
685
686#endif /* bignum.h */