blob: e2d93476d8869eecef2c6fd25353a0f15e20992d [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/**
2 * \file bignum.h
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00003 *
Paul Bakker37ca75d2011-01-06 12:28:03 +00004 * \brief Multi-precision integer library
5 *
Paul Bakker84f12b72010-07-18 10:13:04 +00006 * Copyright (C) 2006-2010, Brainspark B.V.
Paul Bakkerb96f1542010-07-18 20:36:00 +00007 *
8 * This file is part of PolarSSL (http://www.polarssl.org)
Paul Bakker84f12b72010-07-18 10:13:04 +00009 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
Paul Bakkerb96f1542010-07-18 20:36:00 +000010 *
Paul Bakker77b385e2009-07-28 17:23:11 +000011 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000012 *
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000013 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License along
24 * with this program; if not, write to the Free Software Foundation, Inc.,
25 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Paul Bakker5121ce52009-01-03 21:22:43 +000026 */
Paul Bakker40e46942009-01-03 21:51:57 +000027#ifndef POLARSSL_BIGNUM_H
28#define POLARSSL_BIGNUM_H
Paul Bakker5121ce52009-01-03 21:22:43 +000029
30#include <stdio.h>
Paul Bakker23986e52011-04-24 08:57:21 +000031#include <string.h>
Paul Bakker5121ce52009-01-03 21:22:43 +000032
Paul Bakkercf0360a2012-01-20 10:08:14 +000033#include "config.h"
34
Paul Bakker5c2364c2012-10-01 14:41:15 +000035#ifdef _MSC_VER
36#include <basetsd.h>
37typedef INT16 int16_t;
38typedef UINT16 uint16_t;
Paul Bakker9ef6e2b2012-10-01 20:57:38 +000039typedef INT32 int32_t;
Paul Bakker5c2364c2012-10-01 14:41:15 +000040typedef UINT32 uint32_t;
41typedef UINT64 uint64_t;
42#else
43#include <inttypes.h>
44#endif
45
Paul Bakker9d781402011-05-09 16:17:09 +000046#define POLARSSL_ERR_MPI_FILE_IO_ERROR -0x0002 /**< An error occurred while reading from or writing to a file. */
47#define POLARSSL_ERR_MPI_BAD_INPUT_DATA -0x0004 /**< Bad input parameters to function. */
48#define POLARSSL_ERR_MPI_INVALID_CHARACTER -0x0006 /**< There is an invalid character in the digit string. */
Paul Bakker69e095c2011-12-10 21:55:01 +000049#define POLARSSL_ERR_MPI_BUFFER_TOO_SMALL -0x0008 /**< The buffer is too small to write to. */
Paul Bakker9d781402011-05-09 16:17:09 +000050#define POLARSSL_ERR_MPI_NEGATIVE_VALUE -0x000A /**< The input arguments are negative or result in illegal output. */
51#define POLARSSL_ERR_MPI_DIVISION_BY_ZERO -0x000C /**< The input argument for division is zero, which is not allowed. */
52#define POLARSSL_ERR_MPI_NOT_ACCEPTABLE -0x000E /**< The input arguments are not acceptable. */
Paul Bakker69e095c2011-12-10 21:55:01 +000053#define POLARSSL_ERR_MPI_MALLOC_FAILED -0x0010 /**< Memory allocation failed. */
Paul Bakker5121ce52009-01-03 21:22:43 +000054
55#define MPI_CHK(f) if( ( ret = f ) != 0 ) goto cleanup
56
57/*
Paul Bakkerf9688572011-05-05 10:00:45 +000058 * Maximum size MPIs are allowed to grow to in number of limbs.
59 */
60#define POLARSSL_MPI_MAX_LIMBS 10000
61
62/*
Paul Bakkerb6d5f082011-11-25 11:52:11 +000063 * Maximum window size used for modular exponentiation. Default: 6
64 * Minimum value: 1. Maximum value: 6.
65 *
66 * Result is an array of ( 2 << POLARSSL_MPI_WINDOW_SIZE ) MPIs used
67 * for the sliding window calculation. (So 64 by default)
68 *
69 * Reduction in size, reduces speed.
70 */
71#define POLARSSL_MPI_WINDOW_SIZE 6 /**< Maximum windows size used. */
72
73/*
Paul Bakkerfe3256e2011-11-25 12:11:43 +000074 * Maximum size of MPIs allowed in bits and bytes for user-MPIs.
Paul Bakker5552c8c2012-07-05 13:31:54 +000075 * ( Default: 512 bytes => 4096 bits, Maximum: 1024 bytes => 8192 bits )
Paul Bakkerfe3256e2011-11-25 12:11:43 +000076 *
77 * Note: Calculations can results temporarily in larger MPIs. So the number
78 * of limbs required (POLARSSL_MPI_MAX_LIMBS) is higher.
79 */
80#define POLARSSL_MPI_MAX_SIZE 512 /**< Maximum number of bytes for usable MPIs. */
81#define POLARSSL_MPI_MAX_BITS ( 8 * POLARSSL_MPI_MAX_SIZE ) /**< Maximum number of bits for usable MPIs. */
82
83/*
Paul Bakker5531c6d2012-09-26 19:20:46 +000084 * When reading from files with mpi_read_file() and writing to files with
85 * mpi_write_file() the buffer should have space
Paul Bakkercb37aa52011-11-30 16:00:20 +000086 * for a (short) label, the MPI (in the provided radix), the newline
87 * characters and the '\0'.
88 *
89 * By default we assume at least a 10 char label, a minimum radix of 10
90 * (decimal) and a maximum of 4096 bit numbers (1234 decimal chars).
Paul Bakkerf9183102012-09-27 20:42:35 +000091 * Autosized at compile time for at least a 10 char label, a minimum radix
92 * of 10 (decimal) for a number of POLARSSL_MPI_MAX_BITS size.
93 *
94 * This used to be statically sized to 1250 for a maximum of 4096 bit
95 * numbers (1234 decimal chars).
96 *
97 * Calculate using the formula:
98 * POLARSSL_MPI_RW_BUFFER_SIZE = ceil(POLARSSL_MPI_MAX_BITS / ln(10) * ln(2)) +
99 * LabelSize + 6
Paul Bakkercb37aa52011-11-30 16:00:20 +0000100 */
Paul Bakkerf9183102012-09-27 20:42:35 +0000101#define POLARSSL_MPI_MAX_BITS_SCALE100 ( 100 * POLARSSL_MPI_MAX_BITS )
102#define LN_2_DIV_LN_10_SCALE100 332
103#define POLARSSL_MPI_RW_BUFFER_SIZE ( ((POLARSSL_MPI_MAX_BITS_SCALE100 + LN_2_DIV_LN_10_SCALE100 - 1) / LN_2_DIV_LN_10_SCALE100) + 10 + 6 )
Paul Bakkercb37aa52011-11-30 16:00:20 +0000104
105/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000106 * Define the base integer type, architecture-wise
107 */
Paul Bakker40e46942009-01-03 21:51:57 +0000108#if defined(POLARSSL_HAVE_INT8)
Paul Bakkera755ca12011-04-24 09:11:17 +0000109typedef signed char t_sint;
110typedef unsigned char t_uint;
Paul Bakker5c2364c2012-10-01 14:41:15 +0000111typedef uint16_t t_udbl;
Paul Bakker62261d62012-10-02 12:19:31 +0000112#define POLARSSL_HAVE_UDBL
Paul Bakker5121ce52009-01-03 21:22:43 +0000113#else
Paul Bakker40e46942009-01-03 21:51:57 +0000114#if defined(POLARSSL_HAVE_INT16)
Paul Bakker5c2364c2012-10-01 14:41:15 +0000115typedef int16_t t_sint;
116typedef uint16_t t_uint;
117typedef uint32_t t_udbl;
Paul Bakker62261d62012-10-02 12:19:31 +0000118#define POLARSSL_HAVE_UDBL
Paul Bakker5121ce52009-01-03 21:22:43 +0000119#else
Paul Bakker62261d62012-10-02 12:19:31 +0000120 #if ( defined(__MSC_VER) && defined(_M_AMD64) )
121 typedef int64_t t_sint;
122 typedef uint64_t t_uint;
123 #else
124 #if ( defined(__GNUC__) && ( \
125 defined(__amd64__) || defined(__x86_64__) || \
126 defined(__ppc64__) || defined(__powerpc64__) || \
127 defined(__ia64__) || defined(__alpha__) || \
128 (defined(__sparc__) && defined(__arch64__)) || \
129 defined(__s390x__) ) )
130 typedef int64_t t_sint;
131 typedef uint64_t t_uint;
132 typedef unsigned int t_udbl __attribute__((mode(TI)));
133 #define POLARSSL_HAVE_UDBL
134 #else
135 typedef int32_t t_sint;
136 typedef uint32_t t_uint;
137 #if ( defined(_MSC_VER) && defined(_M_IX86) )
138 typedef uint64_t t_udbl;
139 #define POLARSSL_HAVE_UDBL
140 #else
141 #if defined( POLARSSL_HAVE_LONGLONG )
142 typedef unsigned long long t_udbl;
143 #define POLARSSL_HAVE_UDBL
144 #endif
145 #endif
146 #endif
Paul Bakker5c2364c2012-10-01 14:41:15 +0000147 #endif
148#endif /* POLARSSL_HAVE_INT16 */
149#endif /* POLARSSL_HAVE_INT8 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000150
151/**
152 * \brief MPI structure
153 */
154typedef struct
155{
156 int s; /*!< integer sign */
Paul Bakker23986e52011-04-24 08:57:21 +0000157 size_t n; /*!< total # of limbs */
Paul Bakkera755ca12011-04-24 09:11:17 +0000158 t_uint *p; /*!< pointer to limbs */
Paul Bakker5121ce52009-01-03 21:22:43 +0000159}
160mpi;
161
162#ifdef __cplusplus
163extern "C" {
164#endif
165
166/**
Paul Bakker6c591fa2011-05-05 11:49:20 +0000167 * \brief Initialize one MPI
168 *
169 * \param X One MPI to initialize.
Paul Bakker5121ce52009-01-03 21:22:43 +0000170 */
Paul Bakker6c591fa2011-05-05 11:49:20 +0000171void mpi_init( mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000172
173/**
Paul Bakker6c591fa2011-05-05 11:49:20 +0000174 * \brief Unallocate one MPI
175 *
176 * \param X One MPI to unallocate.
Paul Bakker5121ce52009-01-03 21:22:43 +0000177 */
Paul Bakker6c591fa2011-05-05 11:49:20 +0000178void mpi_free( mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000179
180/**
181 * \brief Enlarge to the specified number of limbs
182 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000183 * \param X MPI to grow
184 * \param nblimbs The target number of limbs
185 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000186 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000187 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000188 */
Paul Bakker23986e52011-04-24 08:57:21 +0000189int mpi_grow( mpi *X, size_t nblimbs );
Paul Bakker5121ce52009-01-03 21:22:43 +0000190
191/**
192 * \brief Copy the contents of Y into X
193 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000194 * \param X Destination MPI
195 * \param Y Source MPI
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 Bakkerff60ee62010-03-16 21:09:09 +0000200int mpi_copy( mpi *X, const mpi *Y );
Paul Bakker5121ce52009-01-03 21:22:43 +0000201
202/**
203 * \brief Swap the contents of X and Y
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000204 *
205 * \param X First MPI value
206 * \param Y Second MPI value
Paul Bakker5121ce52009-01-03 21:22:43 +0000207 */
208void mpi_swap( mpi *X, mpi *Y );
209
210/**
211 * \brief Set value from integer
212 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000213 * \param X MPI to set
214 * \param z Value to use
215 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000216 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000217 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000218 */
Paul Bakkera755ca12011-04-24 09:11:17 +0000219int mpi_lset( mpi *X, t_sint z );
Paul Bakker5121ce52009-01-03 21:22:43 +0000220
Paul Bakker2f5947e2011-05-18 15:47:11 +0000221/*
222 * \brief Get a specific bit from X
223 *
224 * \param X MPI to use
225 * \param pos Zero-based index of the bit in X
226 *
227 * \return Either a 0 or a 1
228 */
Paul Bakker6b906e52012-05-08 12:01:43 +0000229int mpi_get_bit( const mpi *X, size_t pos );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000230
231/*
232 * \brief Set a bit of X to a specific value of 0 or 1
233 *
234 * \note Will grow X if necessary to set a bit to 1 in a not yet
235 * existing limb. Will not grow if bit should be set to 0
236 *
237 * \param X MPI to use
238 * \param pos Zero-based index of the bit in X
239 * \param val The value to set the bit to (0 or 1)
240 *
241 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000242 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
Paul Bakker2f5947e2011-05-18 15:47:11 +0000243 * POLARSSL_ERR_MPI_BAD_INPUT_DATA if val is not 0 or 1
244 */
245int mpi_set_bit( mpi *X, size_t pos, unsigned char val );
246
Paul Bakker5121ce52009-01-03 21:22:43 +0000247/**
Paul Bakker6b906e52012-05-08 12:01:43 +0000248 * \brief Return the number of zero-bits before the least significant
249 * '1' bit
250 *
251 * Note: Thus also the zero-based index of the least significant '1' bit
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000252 *
253 * \param X MPI to use
Paul Bakker5121ce52009-01-03 21:22:43 +0000254 */
Paul Bakker23986e52011-04-24 08:57:21 +0000255size_t mpi_lsb( const mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000256
257/**
Paul Bakker6b906e52012-05-08 12:01:43 +0000258 * \brief Return the number of bits up to and including the most
259 * significant '1' bit'
260 *
261 * Note: Thus also the one-based index of the most 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_msb( const mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000266
267/**
268 * \brief Return the total size in bytes
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000269 *
270 * \param X MPI to use
Paul Bakker5121ce52009-01-03 21:22:43 +0000271 */
Paul Bakker23986e52011-04-24 08:57:21 +0000272size_t mpi_size( const mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000273
274/**
275 * \brief Import from an ASCII string
276 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000277 * \param X Destination MPI
278 * \param radix Input numeric base
279 * \param s Null-terminated string buffer
Paul Bakker5121ce52009-01-03 21:22:43 +0000280 *
Paul Bakkercb37aa52011-11-30 16:00:20 +0000281 * \return 0 if successful, or a POLARSSL_ERR_MPI_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000282 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000283int mpi_read_string( mpi *X, int radix, const char *s );
Paul Bakker5121ce52009-01-03 21:22:43 +0000284
285/**
286 * \brief Export into an ASCII string
287 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000288 * \param X Source MPI
289 * \param radix Output numeric base
290 * \param s String buffer
291 * \param slen String buffer size
Paul Bakker5121ce52009-01-03 21:22:43 +0000292 *
Paul Bakkercb37aa52011-11-30 16:00:20 +0000293 * \return 0 if successful, or a POLARSSL_ERR_MPI_XXX error code.
Paul Bakkerff60ee62010-03-16 21:09:09 +0000294 * *slen is always updated to reflect the amount
295 * of data that has (or would have) been written.
Paul Bakker5121ce52009-01-03 21:22:43 +0000296 *
297 * \note Call this function with *slen = 0 to obtain the
298 * minimum required buffer size in *slen.
299 */
Paul Bakker23986e52011-04-24 08:57:21 +0000300int mpi_write_string( const mpi *X, int radix, char *s, size_t *slen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000301
Paul Bakker2b6af2f2012-10-23 11:08:02 +0000302#if defined(POLARSSL_FS_IO)
Paul Bakker5121ce52009-01-03 21:22:43 +0000303/**
304 * \brief Read X from an opened file
305 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000306 * \param X Destination MPI
307 * \param radix Input numeric base
308 * \param fin Input file handle
Paul Bakker5121ce52009-01-03 21:22:43 +0000309 *
Paul Bakkercb37aa52011-11-30 16:00:20 +0000310 * \return 0 if successful, POLARSSL_ERR_MPI_BUFFER_TOO_SMALL if
311 * the file read buffer is too small or a
312 * POLARSSL_ERR_MPI_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000313 */
314int mpi_read_file( mpi *X, int radix, FILE *fin );
315
316/**
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000317 * \brief Write X into an opened file, or stdout if fout is NULL
Paul Bakker5121ce52009-01-03 21:22:43 +0000318 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000319 * \param p Prefix, can be NULL
320 * \param X Source MPI
321 * \param radix Output numeric base
322 * \param fout Output file handle (can be NULL)
Paul Bakker5121ce52009-01-03 21:22:43 +0000323 *
Paul Bakkercb37aa52011-11-30 16:00:20 +0000324 * \return 0 if successful, or a POLARSSL_ERR_MPI_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000325 *
326 * \note Set fout == NULL to print X on the console.
327 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000328int mpi_write_file( const char *p, const mpi *X, int radix, FILE *fout );
Paul Bakker2b6af2f2012-10-23 11:08:02 +0000329#endif /* POLARSSL_FS_IO */
Paul Bakker5121ce52009-01-03 21:22:43 +0000330
331/**
332 * \brief Import X from unsigned binary data, big endian
333 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000334 * \param X Destination MPI
335 * \param buf Input buffer
336 * \param buflen Input buffer size
Paul Bakker5121ce52009-01-03 21:22:43 +0000337 *
338 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000339 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000340 */
Paul Bakker23986e52011-04-24 08:57:21 +0000341int mpi_read_binary( mpi *X, const unsigned char *buf, size_t buflen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000342
343/**
344 * \brief Export X into unsigned binary data, big endian
345 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000346 * \param X Source MPI
347 * \param buf Output buffer
348 * \param buflen Output buffer size
Paul Bakker5121ce52009-01-03 21:22:43 +0000349 *
350 * \return 0 if successful,
Paul Bakker40e46942009-01-03 21:51:57 +0000351 * POLARSSL_ERR_MPI_BUFFER_TOO_SMALL if buf isn't large enough
Paul Bakker5121ce52009-01-03 21:22:43 +0000352 */
Paul Bakker23986e52011-04-24 08:57:21 +0000353int mpi_write_binary( const mpi *X, unsigned char *buf, size_t buflen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000354
355/**
356 * \brief Left-shift: X <<= count
357 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000358 * \param X MPI to shift
359 * \param count Amount to shift
360 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000361 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000362 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000363 */
Paul Bakker23986e52011-04-24 08:57:21 +0000364int mpi_shift_l( mpi *X, size_t count );
Paul Bakker5121ce52009-01-03 21:22:43 +0000365
366/**
367 * \brief Right-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_r( mpi *X, size_t count );
Paul Bakker5121ce52009-01-03 21:22:43 +0000376
377/**
378 * \brief Compare unsigned values
379 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000380 * \param X Left-hand MPI
381 * \param Y Right-hand MPI
382 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000383 * \return 1 if |X| is greater than |Y|,
384 * -1 if |X| is lesser than |Y| or
385 * 0 if |X| is equal to |Y|
386 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000387int mpi_cmp_abs( const mpi *X, const mpi *Y );
Paul Bakker5121ce52009-01-03 21:22:43 +0000388
389/**
390 * \brief Compare signed 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_mpi( 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 z The integer value to compare to
406 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000407 * \return 1 if X is greater than z,
408 * -1 if X is lesser than z or
409 * 0 if X is equal to z
410 */
Paul Bakkera755ca12011-04-24 09:11:17 +0000411int mpi_cmp_int( const mpi *X, t_sint z );
Paul Bakker5121ce52009-01-03 21:22:43 +0000412
413/**
414 * \brief Unsigned addition: X = |A| + |B|
415 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000416 * \param X Destination MPI
417 * \param A Left-hand MPI
418 * \param B Right-hand MPI
419 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000420 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000421 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000422 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000423int mpi_add_abs( mpi *X, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000424
425/**
426 * \brief Unsigned substraction: X = |A| - |B|
427 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000428 * \param X Destination MPI
429 * \param A Left-hand MPI
430 * \param B Right-hand MPI
431 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000432 * \return 0 if successful,
Paul Bakker40e46942009-01-03 21:51:57 +0000433 * POLARSSL_ERR_MPI_NEGATIVE_VALUE if B is greater than A
Paul Bakker5121ce52009-01-03 21:22:43 +0000434 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000435int mpi_sub_abs( mpi *X, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000436
437/**
438 * \brief Signed addition: X = A + B
439 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000440 * \param X Destination MPI
441 * \param A Left-hand MPI
442 * \param B Right-hand MPI
443 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000444 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000445 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000446 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000447int mpi_add_mpi( mpi *X, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000448
449/**
450 * \brief Signed substraction: X = A - B
451 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000452 * \param X Destination MPI
453 * \param A Left-hand MPI
454 * \param B 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_sub_mpi( mpi *X, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000460
461/**
462 * \brief Signed addition: 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 The integer value to add
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 Bakkera755ca12011-04-24 09:11:17 +0000471int mpi_add_int( mpi *X, const mpi *A, t_sint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000472
473/**
474 * \brief Signed substraction: 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 subtract
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_sub_int( mpi *X, const mpi *A, t_sint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000484
485/**
486 * \brief Baseline multiplication: 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 Right-hand MPI
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 Bakkerff60ee62010-03-16 21:09:09 +0000495int mpi_mul_mpi( mpi *X, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000496
497/**
498 * \brief Baseline multiplication: X = A * b
Paul Bakkerce40a6d2009-06-23 19:46:08 +0000499 * Note: b is an unsigned integer type, thus
500 * Negative values of b are ignored.
Paul Bakker5121ce52009-01-03 21:22:43 +0000501 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000502 * \param X Destination MPI
503 * \param A Left-hand MPI
504 * \param b The integer value to multiply with
505 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000506 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000507 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000508 */
Paul Bakkera755ca12011-04-24 09:11:17 +0000509int mpi_mul_int( mpi *X, const mpi *A, t_sint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000510
511/**
512 * \brief Division by mpi: A = Q * B + R
513 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000514 * \param Q Destination MPI for the quotient
515 * \param R Destination MPI for the rest value
516 * \param A Left-hand MPI
517 * \param B Right-hand MPI
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 Bakker40e46942009-01-03 21:51:57 +0000521 * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if B == 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000522 *
523 * \note Either Q or R can be NULL.
524 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000525int mpi_div_mpi( mpi *Q, mpi *R, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000526
527/**
528 * \brief Division by int: A = Q * b + R
529 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000530 * \param Q Destination MPI for the quotient
531 * \param R Destination MPI for the rest value
532 * \param A Left-hand MPI
533 * \param b Integer to divide by
534 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000535 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000536 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000537 * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if b == 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000538 *
539 * \note Either Q or R can be NULL.
540 */
Paul Bakkera755ca12011-04-24 09:11:17 +0000541int mpi_div_int( mpi *Q, mpi *R, const mpi *A, t_sint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000542
543/**
544 * \brief Modulo: R = A mod B
545 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000546 * \param R Destination MPI for the rest value
547 * \param A Left-hand MPI
548 * \param B Right-hand MPI
549 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000550 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000551 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
Paul Bakkerce40a6d2009-06-23 19:46:08 +0000552 * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if B == 0,
553 * POLARSSL_ERR_MPI_NEGATIVE_VALUE if B < 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000554 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000555int mpi_mod_mpi( mpi *R, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000556
557/**
558 * \brief Modulo: r = A mod b
559 *
Paul Bakkera755ca12011-04-24 09:11:17 +0000560 * \param r Destination t_uint
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000561 * \param A Left-hand MPI
562 * \param b Integer to divide by
563 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000564 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000565 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
Paul Bakkerce40a6d2009-06-23 19:46:08 +0000566 * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if b == 0,
567 * POLARSSL_ERR_MPI_NEGATIVE_VALUE if b < 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000568 */
Paul Bakkera755ca12011-04-24 09:11:17 +0000569int mpi_mod_int( t_uint *r, const mpi *A, t_sint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000570
571/**
572 * \brief Sliding-window exponentiation: X = A^E mod N
573 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000574 * \param X Destination MPI
575 * \param A Left-hand MPI
576 * \param E Exponent MPI
577 * \param N Modular MPI
578 * \param _RR Speed-up MPI used for recalculations
579 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000580 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000581 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
Paul Bakkerf6198c12012-05-16 08:02:29 +0000582 * POLARSSL_ERR_MPI_BAD_INPUT_DATA if N is negative or even or if
583 * E is negative
Paul Bakker5121ce52009-01-03 21:22:43 +0000584 *
585 * \note _RR is used to avoid re-computing R*R mod N across
586 * multiple calls, which speeds up things a bit. It can
587 * be set to NULL if the extra performance is unneeded.
588 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000589int mpi_exp_mod( mpi *X, const mpi *A, const mpi *E, const mpi *N, mpi *_RR );
Paul Bakker5121ce52009-01-03 21:22:43 +0000590
591/**
Paul Bakker287781a2011-03-26 13:18:49 +0000592 * \brief Fill an MPI X with size bytes of random
593 *
594 * \param X Destination MPI
595 * \param size Size in bytes
596 * \param f_rng RNG function
597 * \param p_rng RNG parameter
598 *
599 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000600 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker287781a2011-03-26 13:18:49 +0000601 */
Paul Bakkera3d195c2011-11-27 21:07:34 +0000602int mpi_fill_random( mpi *X, size_t size,
603 int (*f_rng)(void *, unsigned char *, size_t),
604 void *p_rng );
Paul Bakker287781a2011-03-26 13:18:49 +0000605
606/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000607 * \brief Greatest common divisor: G = gcd(A, B)
608 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000609 * \param G Destination MPI
610 * \param A Left-hand MPI
611 * \param B Right-hand MPI
612 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000613 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000614 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000615 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000616int mpi_gcd( mpi *G, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000617
618/**
619 * \brief Modular inverse: X = A^-1 mod N
620 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000621 * \param X Destination MPI
622 * \param A Left-hand MPI
623 * \param N Right-hand MPI
624 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000625 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000626 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000627 * POLARSSL_ERR_MPI_BAD_INPUT_DATA if N is negative or nil
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000628 POLARSSL_ERR_MPI_NOT_ACCEPTABLE if A has no inverse mod N
Paul Bakker5121ce52009-01-03 21:22:43 +0000629 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000630int mpi_inv_mod( mpi *X, const mpi *A, const mpi *N );
Paul Bakker5121ce52009-01-03 21:22:43 +0000631
632/**
633 * \brief Miller-Rabin primality test
634 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000635 * \param X MPI to check
636 * \param f_rng RNG function
637 * \param p_rng RNG parameter
638 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000639 * \return 0 if successful (probably prime),
Paul Bakker69e095c2011-12-10 21:55:01 +0000640 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000641 * POLARSSL_ERR_MPI_NOT_ACCEPTABLE if X is not prime
Paul Bakker5121ce52009-01-03 21:22:43 +0000642 */
Paul Bakkera3d195c2011-11-27 21:07:34 +0000643int mpi_is_prime( mpi *X,
644 int (*f_rng)(void *, unsigned char *, size_t),
645 void *p_rng );
Paul Bakker5121ce52009-01-03 21:22:43 +0000646
647/**
648 * \brief Prime number generation
649 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000650 * \param X Destination MPI
Paul Bakkerfe3256e2011-11-25 12:11:43 +0000651 * \param nbits Required size of X in bits ( 3 <= nbits <= POLARSSL_MPI_MAX_BITS )
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000652 * \param dh_flag If 1, then (X-1)/2 will be prime too
Paul Bakker5121ce52009-01-03 21:22:43 +0000653 * \param f_rng RNG function
654 * \param p_rng RNG parameter
655 *
656 * \return 0 if successful (probably prime),
Paul Bakker69e095c2011-12-10 21:55:01 +0000657 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000658 * POLARSSL_ERR_MPI_BAD_INPUT_DATA if nbits is < 3
Paul Bakker5121ce52009-01-03 21:22:43 +0000659 */
Paul Bakker23986e52011-04-24 08:57:21 +0000660int mpi_gen_prime( mpi *X, size_t nbits, int dh_flag,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000661 int (*f_rng)(void *, unsigned char *, size_t),
662 void *p_rng );
Paul Bakker5121ce52009-01-03 21:22:43 +0000663
664/**
665 * \brief Checkup routine
666 *
667 * \return 0 if successful, or 1 if the test failed
668 */
669int mpi_self_test( int verbose );
670
671#ifdef __cplusplus
672}
673#endif
674
675#endif /* bignum.h */