blob: 635c5d78173788fa6aff11fce7732db015d4be20 [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 Bakker530927b2015-02-13 14:24:10 +01006 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Paul Bakkerb96f1542010-07-18 20:36:00 +00007 *
Manuel Pégourié-Gonnarde12abf92015-01-28 17:13:45 +00008 * This file is part of mbed TLS (https://polarssl.org)
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00009 *
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000010 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Paul Bakker5121ce52009-01-03 21:22:43 +000023 */
Paul Bakker40e46942009-01-03 21:51:57 +000024#ifndef POLARSSL_BIGNUM_H
25#define POLARSSL_BIGNUM_H
Paul Bakker5121ce52009-01-03 21:22:43 +000026
27#include <stdio.h>
Paul Bakker23986e52011-04-24 08:57:21 +000028#include <string.h>
Paul Bakker5121ce52009-01-03 21:22:43 +000029
Paul Bakkercf0360a2012-01-20 10:08:14 +000030#include "config.h"
31
Paul Bakker5c2364c2012-10-01 14:41:15 +000032#ifdef _MSC_VER
33#include <basetsd.h>
Paul Bakker4a2bd0d2012-11-02 11:06:08 +000034#if (_MSC_VER <= 1200)
35typedef signed short int16_t;
36typedef unsigned short uint16_t;
37#else
Paul Bakker5c2364c2012-10-01 14:41:15 +000038typedef INT16 int16_t;
39typedef UINT16 uint16_t;
Paul Bakker4a2bd0d2012-11-02 11:06:08 +000040#endif
Paul Bakker9ef6e2b2012-10-01 20:57:38 +000041typedef INT32 int32_t;
Paul Bakker8ea31ff2013-02-27 15:02:50 +010042typedef INT64 int64_t;
Paul Bakker5c2364c2012-10-01 14:41:15 +000043typedef UINT32 uint32_t;
44typedef UINT64 uint64_t;
45#else
46#include <inttypes.h>
47#endif
48
Paul Bakker9d781402011-05-09 16:17:09 +000049#define POLARSSL_ERR_MPI_FILE_IO_ERROR -0x0002 /**< An error occurred while reading from or writing to a file. */
50#define POLARSSL_ERR_MPI_BAD_INPUT_DATA -0x0004 /**< Bad input parameters to function. */
51#define POLARSSL_ERR_MPI_INVALID_CHARACTER -0x0006 /**< There is an invalid character in the digit string. */
Paul Bakker69e095c2011-12-10 21:55:01 +000052#define POLARSSL_ERR_MPI_BUFFER_TOO_SMALL -0x0008 /**< The buffer is too small to write to. */
Paul Bakker9d781402011-05-09 16:17:09 +000053#define POLARSSL_ERR_MPI_NEGATIVE_VALUE -0x000A /**< The input arguments are negative or result in illegal output. */
54#define POLARSSL_ERR_MPI_DIVISION_BY_ZERO -0x000C /**< The input argument for division is zero, which is not allowed. */
55#define POLARSSL_ERR_MPI_NOT_ACCEPTABLE -0x000E /**< The input arguments are not acceptable. */
Paul Bakker69e095c2011-12-10 21:55:01 +000056#define POLARSSL_ERR_MPI_MALLOC_FAILED -0x0010 /**< Memory allocation failed. */
Paul Bakker5121ce52009-01-03 21:22:43 +000057
58#define MPI_CHK(f) if( ( ret = f ) != 0 ) goto cleanup
59
60/*
Paul Bakkerf9688572011-05-05 10:00:45 +000061 * Maximum size MPIs are allowed to grow to in number of limbs.
62 */
63#define POLARSSL_MPI_MAX_LIMBS 10000
64
Paul Bakker6fa54882013-06-17 15:44:03 +020065#if !defined(POLARSSL_CONFIG_OPTIONS)
Paul Bakkerf9688572011-05-05 10:00:45 +000066/*
Paul Bakkerb6d5f082011-11-25 11:52:11 +000067 * Maximum window size used for modular exponentiation. Default: 6
68 * Minimum value: 1. Maximum value: 6.
69 *
70 * Result is an array of ( 2 << POLARSSL_MPI_WINDOW_SIZE ) MPIs used
71 * for the sliding window calculation. (So 64 by default)
72 *
73 * Reduction in size, reduces speed.
74 */
75#define POLARSSL_MPI_WINDOW_SIZE 6 /**< Maximum windows size used. */
76
77/*
Paul Bakkerfe3256e2011-11-25 12:11:43 +000078 * Maximum size of MPIs allowed in bits and bytes for user-MPIs.
Paul Bakkerf626e1d2013-01-21 12:10:00 +010079 * ( Default: 512 bytes => 4096 bits, Maximum tested: 2048 bytes => 16384 bits )
Paul Bakkerfe3256e2011-11-25 12:11:43 +000080 *
81 * Note: Calculations can results temporarily in larger MPIs. So the number
82 * of limbs required (POLARSSL_MPI_MAX_LIMBS) is higher.
83 */
84#define POLARSSL_MPI_MAX_SIZE 512 /**< Maximum number of bytes for usable MPIs. */
Paul Bakker6fa54882013-06-17 15:44:03 +020085
86#endif /* !POLARSSL_CONFIG_OPTIONS */
87
Paul Bakkerfe3256e2011-11-25 12:11:43 +000088#define POLARSSL_MPI_MAX_BITS ( 8 * POLARSSL_MPI_MAX_SIZE ) /**< Maximum number of bits for usable MPIs. */
89
90/*
Paul Bakker5531c6d2012-09-26 19:20:46 +000091 * When reading from files with mpi_read_file() and writing to files with
92 * mpi_write_file() the buffer should have space
Paul Bakkercb37aa52011-11-30 16:00:20 +000093 * for a (short) label, the MPI (in the provided radix), the newline
94 * characters and the '\0'.
95 *
96 * By default we assume at least a 10 char label, a minimum radix of 10
97 * (decimal) and a maximum of 4096 bit numbers (1234 decimal chars).
Paul Bakkerf9183102012-09-27 20:42:35 +000098 * Autosized at compile time for at least a 10 char label, a minimum radix
99 * of 10 (decimal) for a number of POLARSSL_MPI_MAX_BITS size.
100 *
101 * This used to be statically sized to 1250 for a maximum of 4096 bit
102 * numbers (1234 decimal chars).
103 *
104 * Calculate using the formula:
105 * POLARSSL_MPI_RW_BUFFER_SIZE = ceil(POLARSSL_MPI_MAX_BITS / ln(10) * ln(2)) +
106 * LabelSize + 6
Paul Bakkercb37aa52011-11-30 16:00:20 +0000107 */
Paul Bakkerf9183102012-09-27 20:42:35 +0000108#define POLARSSL_MPI_MAX_BITS_SCALE100 ( 100 * POLARSSL_MPI_MAX_BITS )
109#define LN_2_DIV_LN_10_SCALE100 332
110#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 +0000111
112/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000113 * Define the base integer type, architecture-wise
114 */
Paul Bakker40e46942009-01-03 21:51:57 +0000115#if defined(POLARSSL_HAVE_INT8)
Paul Bakkera755ca12011-04-24 09:11:17 +0000116typedef signed char t_sint;
117typedef unsigned char t_uint;
Paul Bakker5c2364c2012-10-01 14:41:15 +0000118typedef uint16_t t_udbl;
Paul Bakker62261d62012-10-02 12:19:31 +0000119#define POLARSSL_HAVE_UDBL
Paul Bakker5121ce52009-01-03 21:22:43 +0000120#else
Paul Bakker40e46942009-01-03 21:51:57 +0000121#if defined(POLARSSL_HAVE_INT16)
Paul Bakker5c2364c2012-10-01 14:41:15 +0000122typedef int16_t t_sint;
123typedef uint16_t t_uint;
124typedef uint32_t t_udbl;
Paul Bakker62261d62012-10-02 12:19:31 +0000125#define POLARSSL_HAVE_UDBL
Paul Bakker5121ce52009-01-03 21:22:43 +0000126#else
Paul Bakker9f2018e2013-02-27 15:01:34 +0100127 #if ( defined(_MSC_VER) && defined(_M_AMD64) )
Paul Bakker62261d62012-10-02 12:19:31 +0000128 typedef int64_t t_sint;
129 typedef uint64_t t_uint;
130 #else
131 #if ( defined(__GNUC__) && ( \
132 defined(__amd64__) || defined(__x86_64__) || \
133 defined(__ppc64__) || defined(__powerpc64__) || \
134 defined(__ia64__) || defined(__alpha__) || \
135 (defined(__sparc__) && defined(__arch64__)) || \
James Cowgillb82f5912014-12-16 15:24:06 +0000136 defined(__s390x__) || defined(__mips64) ) )
Paul Bakker62261d62012-10-02 12:19:31 +0000137 typedef int64_t t_sint;
138 typedef uint64_t t_uint;
Simon Butcher6901e502016-01-03 22:39:18 +0000139 /* mbedtls_t_udbl defined as 128-bit unsigned int */
Paul Bakker62261d62012-10-02 12:19:31 +0000140 typedef unsigned int t_udbl __attribute__((mode(TI)));
141 #define POLARSSL_HAVE_UDBL
142 #else
143 typedef int32_t t_sint;
144 typedef uint32_t t_uint;
145 #if ( defined(_MSC_VER) && defined(_M_IX86) )
146 typedef uint64_t t_udbl;
147 #define POLARSSL_HAVE_UDBL
148 #else
149 #if defined( POLARSSL_HAVE_LONGLONG )
150 typedef unsigned long long t_udbl;
151 #define POLARSSL_HAVE_UDBL
152 #endif
153 #endif
154 #endif
Paul Bakker5c2364c2012-10-01 14:41:15 +0000155 #endif
156#endif /* POLARSSL_HAVE_INT16 */
157#endif /* POLARSSL_HAVE_INT8 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000158
159/**
160 * \brief MPI structure
161 */
162typedef struct
163{
164 int s; /*!< integer sign */
Paul Bakker23986e52011-04-24 08:57:21 +0000165 size_t n; /*!< total # of limbs */
Paul Bakkera755ca12011-04-24 09:11:17 +0000166 t_uint *p; /*!< pointer to limbs */
Paul Bakker5121ce52009-01-03 21:22:43 +0000167}
168mpi;
169
170#ifdef __cplusplus
171extern "C" {
172#endif
173
174/**
Paul Bakker6c591fa2011-05-05 11:49:20 +0000175 * \brief Initialize one MPI
176 *
177 * \param X One MPI to initialize.
Paul Bakker5121ce52009-01-03 21:22:43 +0000178 */
Paul Bakker6c591fa2011-05-05 11:49:20 +0000179void mpi_init( mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000180
181/**
Paul Bakker6c591fa2011-05-05 11:49:20 +0000182 * \brief Unallocate one MPI
183 *
184 * \param X One MPI to unallocate.
Paul Bakker5121ce52009-01-03 21:22:43 +0000185 */
Paul Bakker6c591fa2011-05-05 11:49:20 +0000186void mpi_free( mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000187
188/**
189 * \brief Enlarge to the specified number of limbs
190 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000191 * \param X MPI to grow
192 * \param nblimbs The target number of limbs
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 Bakker23986e52011-04-24 08:57:21 +0000197int mpi_grow( mpi *X, size_t nblimbs );
Paul Bakker5121ce52009-01-03 21:22:43 +0000198
199/**
200 * \brief Copy the contents of Y into X
201 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000202 * \param X Destination MPI
203 * \param Y Source MPI
204 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000205 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000206 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000207 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000208int mpi_copy( mpi *X, const mpi *Y );
Paul Bakker5121ce52009-01-03 21:22:43 +0000209
210/**
211 * \brief Swap the contents of X and Y
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000212 *
213 * \param X First MPI value
214 * \param Y Second MPI value
Paul Bakker5121ce52009-01-03 21:22:43 +0000215 */
216void mpi_swap( mpi *X, mpi *Y );
217
218/**
219 * \brief Set value from integer
220 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000221 * \param X MPI to set
222 * \param z Value to use
223 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000224 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000225 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000226 */
Paul Bakkera755ca12011-04-24 09:11:17 +0000227int mpi_lset( mpi *X, t_sint z );
Paul Bakker5121ce52009-01-03 21:22:43 +0000228
Paul Bakker9a736322012-11-14 12:39:52 +0000229/**
Paul Bakker2f5947e2011-05-18 15:47:11 +0000230 * \brief Get a specific bit from X
231 *
232 * \param X MPI to use
233 * \param pos Zero-based index of the bit in X
234 *
235 * \return Either a 0 or a 1
236 */
Paul Bakker6b906e52012-05-08 12:01:43 +0000237int mpi_get_bit( const mpi *X, size_t pos );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000238
Paul Bakker9a736322012-11-14 12:39:52 +0000239/**
Paul Bakker2f5947e2011-05-18 15:47:11 +0000240 * \brief Set a bit of X to a specific value of 0 or 1
241 *
242 * \note Will grow X if necessary to set a bit to 1 in a not yet
243 * existing limb. Will not grow if bit should be set to 0
244 *
245 * \param X MPI to use
246 * \param pos Zero-based index of the bit in X
247 * \param val The value to set the bit to (0 or 1)
248 *
249 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000250 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
Paul Bakker2f5947e2011-05-18 15:47:11 +0000251 * POLARSSL_ERR_MPI_BAD_INPUT_DATA if val is not 0 or 1
252 */
253int mpi_set_bit( mpi *X, size_t pos, unsigned char val );
254
Paul Bakker5121ce52009-01-03 21:22:43 +0000255/**
Paul Bakker6b906e52012-05-08 12:01:43 +0000256 * \brief Return the number of zero-bits before the least significant
257 * '1' bit
258 *
259 * Note: Thus also the zero-based index of the least significant '1' bit
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000260 *
261 * \param X MPI to use
Paul Bakker5121ce52009-01-03 21:22:43 +0000262 */
Paul Bakker23986e52011-04-24 08:57:21 +0000263size_t mpi_lsb( const mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000264
265/**
Paul Bakker6b906e52012-05-08 12:01:43 +0000266 * \brief Return the number of bits up to and including the most
267 * significant '1' bit'
268 *
269 * Note: Thus also the one-based index of the most significant '1' bit
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000270 *
271 * \param X MPI to use
Paul Bakker5121ce52009-01-03 21:22:43 +0000272 */
Paul Bakker23986e52011-04-24 08:57:21 +0000273size_t mpi_msb( const mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000274
275/**
276 * \brief Return the total size in bytes
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000277 *
278 * \param X MPI to use
Paul Bakker5121ce52009-01-03 21:22:43 +0000279 */
Paul Bakker23986e52011-04-24 08:57:21 +0000280size_t mpi_size( const mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000281
282/**
283 * \brief Import from an ASCII string
284 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000285 * \param X Destination MPI
286 * \param radix Input numeric base
287 * \param s Null-terminated string buffer
Paul Bakker5121ce52009-01-03 21:22:43 +0000288 *
Paul Bakkercb37aa52011-11-30 16:00:20 +0000289 * \return 0 if successful, or a POLARSSL_ERR_MPI_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000290 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000291int mpi_read_string( mpi *X, int radix, const char *s );
Paul Bakker5121ce52009-01-03 21:22:43 +0000292
293/**
294 * \brief Export into an ASCII string
295 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000296 * \param X Source MPI
297 * \param radix Output numeric base
298 * \param s String buffer
299 * \param slen String buffer size
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 Bakkerff60ee62010-03-16 21:09:09 +0000302 * *slen is always updated to reflect the amount
303 * of data that has (or would have) been written.
Paul Bakker5121ce52009-01-03 21:22:43 +0000304 *
305 * \note Call this function with *slen = 0 to obtain the
306 * minimum required buffer size in *slen.
307 */
Paul Bakker23986e52011-04-24 08:57:21 +0000308int mpi_write_string( const mpi *X, int radix, char *s, size_t *slen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000309
Paul Bakker2b6af2f2012-10-23 11:08:02 +0000310#if defined(POLARSSL_FS_IO)
Paul Bakker5121ce52009-01-03 21:22:43 +0000311/**
312 * \brief Read X from an opened file
313 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000314 * \param X Destination MPI
315 * \param radix Input numeric base
316 * \param fin Input file handle
Paul Bakker5121ce52009-01-03 21:22:43 +0000317 *
Paul Bakkercb37aa52011-11-30 16:00:20 +0000318 * \return 0 if successful, POLARSSL_ERR_MPI_BUFFER_TOO_SMALL if
319 * the file read buffer is too small or a
320 * POLARSSL_ERR_MPI_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000321 */
322int mpi_read_file( mpi *X, int radix, FILE *fin );
323
324/**
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000325 * \brief Write X into an opened file, or stdout if fout is NULL
Paul Bakker5121ce52009-01-03 21:22:43 +0000326 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000327 * \param p Prefix, can be NULL
328 * \param X Source MPI
329 * \param radix Output numeric base
330 * \param fout Output file handle (can be NULL)
Paul Bakker5121ce52009-01-03 21:22:43 +0000331 *
Paul Bakkercb37aa52011-11-30 16:00:20 +0000332 * \return 0 if successful, or a POLARSSL_ERR_MPI_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000333 *
334 * \note Set fout == NULL to print X on the console.
335 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000336int mpi_write_file( const char *p, const mpi *X, int radix, FILE *fout );
Paul Bakker2b6af2f2012-10-23 11:08:02 +0000337#endif /* POLARSSL_FS_IO */
Paul Bakker5121ce52009-01-03 21:22:43 +0000338
339/**
340 * \brief Import X from unsigned binary data, big endian
341 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000342 * \param X Destination MPI
343 * \param buf Input buffer
344 * \param buflen Input buffer size
Paul Bakker5121ce52009-01-03 21:22:43 +0000345 *
346 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000347 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000348 */
Paul Bakker23986e52011-04-24 08:57:21 +0000349int mpi_read_binary( mpi *X, const unsigned char *buf, size_t buflen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000350
351/**
Manuel Pégourié-Gonnard002bc862014-06-25 12:57:47 +0200352 * \brief Export X into unsigned binary data, big endian.
353 * Always fills the whole buffer, which will start with zeros
354 * if the number is smaller.
Paul Bakker5121ce52009-01-03 21:22:43 +0000355 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000356 * \param X Source MPI
357 * \param buf Output buffer
358 * \param buflen Output buffer size
Paul Bakker5121ce52009-01-03 21:22:43 +0000359 *
360 * \return 0 if successful,
Paul Bakker40e46942009-01-03 21:51:57 +0000361 * POLARSSL_ERR_MPI_BUFFER_TOO_SMALL if buf isn't large enough
Paul Bakker5121ce52009-01-03 21:22:43 +0000362 */
Paul Bakker23986e52011-04-24 08:57:21 +0000363int mpi_write_binary( const mpi *X, unsigned char *buf, size_t buflen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000364
365/**
366 * \brief Left-shift: X <<= count
367 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000368 * \param X MPI to shift
369 * \param count Amount to shift
370 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000371 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000372 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000373 */
Paul Bakker23986e52011-04-24 08:57:21 +0000374int mpi_shift_l( mpi *X, size_t count );
Paul Bakker5121ce52009-01-03 21:22:43 +0000375
376/**
377 * \brief Right-shift: X >>= count
378 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000379 * \param X MPI to shift
380 * \param count Amount to shift
381 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000382 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000383 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000384 */
Paul Bakker23986e52011-04-24 08:57:21 +0000385int mpi_shift_r( mpi *X, size_t count );
Paul Bakker5121ce52009-01-03 21:22:43 +0000386
387/**
388 * \brief Compare unsigned values
389 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000390 * \param X Left-hand MPI
391 * \param Y Right-hand MPI
392 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000393 * \return 1 if |X| is greater than |Y|,
394 * -1 if |X| is lesser than |Y| or
395 * 0 if |X| is equal to |Y|
396 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000397int mpi_cmp_abs( const mpi *X, const mpi *Y );
Paul Bakker5121ce52009-01-03 21:22:43 +0000398
399/**
400 * \brief Compare signed values
401 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000402 * \param X Left-hand MPI
403 * \param Y Right-hand MPI
404 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000405 * \return 1 if X is greater than Y,
406 * -1 if X is lesser than Y or
407 * 0 if X is equal to Y
408 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000409int mpi_cmp_mpi( const mpi *X, const mpi *Y );
Paul Bakker5121ce52009-01-03 21:22:43 +0000410
411/**
412 * \brief Compare signed values
413 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000414 * \param X Left-hand MPI
415 * \param z The integer value to compare to
416 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000417 * \return 1 if X is greater than z,
418 * -1 if X is lesser than z or
419 * 0 if X is equal to z
420 */
Paul Bakkera755ca12011-04-24 09:11:17 +0000421int mpi_cmp_int( const mpi *X, t_sint z );
Paul Bakker5121ce52009-01-03 21:22:43 +0000422
423/**
424 * \brief Unsigned addition: X = |A| + |B|
425 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000426 * \param X Destination MPI
427 * \param A Left-hand MPI
428 * \param B Right-hand MPI
429 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000430 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000431 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000432 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000433int mpi_add_abs( mpi *X, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000434
435/**
436 * \brief Unsigned substraction: X = |A| - |B|
437 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000438 * \param X Destination MPI
439 * \param A Left-hand MPI
440 * \param B Right-hand MPI
441 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000442 * \return 0 if successful,
Paul Bakker40e46942009-01-03 21:51:57 +0000443 * POLARSSL_ERR_MPI_NEGATIVE_VALUE if B is greater than A
Paul Bakker5121ce52009-01-03 21:22:43 +0000444 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000445int mpi_sub_abs( mpi *X, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000446
447/**
448 * \brief Signed addition: X = A + B
449 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000450 * \param X Destination MPI
451 * \param A Left-hand MPI
452 * \param B Right-hand MPI
453 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000454 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000455 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000456 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000457int mpi_add_mpi( mpi *X, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000458
459/**
460 * \brief Signed substraction: X = A - B
461 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000462 * \param X Destination MPI
463 * \param A Left-hand MPI
464 * \param B Right-hand MPI
465 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000466 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000467 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000468 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000469int mpi_sub_mpi( mpi *X, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000470
471/**
472 * \brief Signed addition: X = A + b
473 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000474 * \param X Destination MPI
475 * \param A Left-hand MPI
476 * \param b The integer value to add
477 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000478 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000479 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000480 */
Paul Bakkera755ca12011-04-24 09:11:17 +0000481int mpi_add_int( mpi *X, const mpi *A, t_sint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000482
483/**
484 * \brief Signed substraction: X = A - b
485 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000486 * \param X Destination MPI
487 * \param A Left-hand MPI
488 * \param b The integer value to subtract
489 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000490 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000491 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000492 */
Paul Bakkera755ca12011-04-24 09:11:17 +0000493int mpi_sub_int( mpi *X, const mpi *A, t_sint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000494
495/**
496 * \brief Baseline multiplication: X = A * B
497 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000498 * \param X Destination MPI
499 * \param A Left-hand MPI
500 * \param B Right-hand MPI
501 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000502 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000503 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000504 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000505int mpi_mul_mpi( mpi *X, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000506
507/**
508 * \brief Baseline multiplication: X = A * b
Manuel Pégourié-Gonnard7ab2d5d2013-11-25 16:16:33 +0100509 * Note: despite the functon signature, b is treated as a
510 * t_uint. Negative values of b are treated as large positive
511 * values.
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 */