blob: b4a6c063790c2e9461f532c73e87a77f11c1b78e [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
302/**
303 * \brief Read X from an opened file
304 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000305 * \param X Destination MPI
306 * \param radix Input numeric base
307 * \param fin Input file handle
Paul Bakker5121ce52009-01-03 21:22:43 +0000308 *
Paul Bakkercb37aa52011-11-30 16:00:20 +0000309 * \return 0 if successful, POLARSSL_ERR_MPI_BUFFER_TOO_SMALL if
310 * the file read buffer is too small or a
311 * POLARSSL_ERR_MPI_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000312 */
313int mpi_read_file( mpi *X, int radix, FILE *fin );
314
315/**
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000316 * \brief Write X into an opened file, or stdout if fout is NULL
Paul Bakker5121ce52009-01-03 21:22:43 +0000317 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000318 * \param p Prefix, can be NULL
319 * \param X Source MPI
320 * \param radix Output numeric base
321 * \param fout Output file handle (can be NULL)
Paul Bakker5121ce52009-01-03 21:22:43 +0000322 *
Paul Bakkercb37aa52011-11-30 16:00:20 +0000323 * \return 0 if successful, or a POLARSSL_ERR_MPI_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000324 *
325 * \note Set fout == NULL to print X on the console.
326 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000327int mpi_write_file( const char *p, const mpi *X, int radix, FILE *fout );
Paul Bakker5121ce52009-01-03 21:22:43 +0000328
329/**
330 * \brief Import X from unsigned binary data, big endian
331 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000332 * \param X Destination MPI
333 * \param buf Input buffer
334 * \param buflen Input buffer size
Paul Bakker5121ce52009-01-03 21:22:43 +0000335 *
336 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000337 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000338 */
Paul Bakker23986e52011-04-24 08:57:21 +0000339int mpi_read_binary( mpi *X, const unsigned char *buf, size_t buflen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000340
341/**
342 * \brief Export X into unsigned binary data, big endian
343 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000344 * \param X Source MPI
345 * \param buf Output buffer
346 * \param buflen Output buffer size
Paul Bakker5121ce52009-01-03 21:22:43 +0000347 *
348 * \return 0 if successful,
Paul Bakker40e46942009-01-03 21:51:57 +0000349 * POLARSSL_ERR_MPI_BUFFER_TOO_SMALL if buf isn't large enough
Paul Bakker5121ce52009-01-03 21:22:43 +0000350 */
Paul Bakker23986e52011-04-24 08:57:21 +0000351int mpi_write_binary( const mpi *X, unsigned char *buf, size_t buflen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000352
353/**
354 * \brief Left-shift: X <<= count
355 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000356 * \param X MPI to shift
357 * \param count Amount to shift
358 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000359 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000360 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000361 */
Paul Bakker23986e52011-04-24 08:57:21 +0000362int mpi_shift_l( mpi *X, size_t count );
Paul Bakker5121ce52009-01-03 21:22:43 +0000363
364/**
365 * \brief Right-shift: X >>= count
366 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000367 * \param X MPI to shift
368 * \param count Amount to shift
369 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000370 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000371 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000372 */
Paul Bakker23986e52011-04-24 08:57:21 +0000373int mpi_shift_r( mpi *X, size_t count );
Paul Bakker5121ce52009-01-03 21:22:43 +0000374
375/**
376 * \brief Compare unsigned values
377 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000378 * \param X Left-hand MPI
379 * \param Y Right-hand MPI
380 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000381 * \return 1 if |X| is greater than |Y|,
382 * -1 if |X| is lesser than |Y| or
383 * 0 if |X| is equal to |Y|
384 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000385int mpi_cmp_abs( const mpi *X, const mpi *Y );
Paul Bakker5121ce52009-01-03 21:22:43 +0000386
387/**
388 * \brief Compare signed 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_mpi( 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 z The integer value to compare to
404 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000405 * \return 1 if X is greater than z,
406 * -1 if X is lesser than z or
407 * 0 if X is equal to z
408 */
Paul Bakkera755ca12011-04-24 09:11:17 +0000409int mpi_cmp_int( const mpi *X, t_sint z );
Paul Bakker5121ce52009-01-03 21:22:43 +0000410
411/**
412 * \brief Unsigned addition: X = |A| + |B|
413 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000414 * \param X Destination MPI
415 * \param A Left-hand MPI
416 * \param B Right-hand MPI
417 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000418 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000419 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000420 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000421int mpi_add_abs( mpi *X, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000422
423/**
424 * \brief Unsigned substraction: 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 Bakker40e46942009-01-03 21:51:57 +0000431 * POLARSSL_ERR_MPI_NEGATIVE_VALUE if B is greater than A
Paul Bakker5121ce52009-01-03 21:22:43 +0000432 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000433int mpi_sub_abs( mpi *X, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000434
435/**
436 * \brief Signed addition: 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 Bakker69e095c2011-12-10 21:55:01 +0000443 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000444 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000445int mpi_add_mpi( mpi *X, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000446
447/**
448 * \brief Signed substraction: 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_sub_mpi( mpi *X, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000458
459/**
460 * \brief Signed addition: 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 The integer value to add
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 Bakkera755ca12011-04-24 09:11:17 +0000469int mpi_add_int( mpi *X, const mpi *A, t_sint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000470
471/**
472 * \brief Signed substraction: 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 subtract
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_sub_int( mpi *X, const mpi *A, t_sint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000482
483/**
484 * \brief Baseline multiplication: 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 Right-hand MPI
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 Bakkerff60ee62010-03-16 21:09:09 +0000493int mpi_mul_mpi( mpi *X, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000494
495/**
496 * \brief Baseline multiplication: X = A * b
Paul Bakkerce40a6d2009-06-23 19:46:08 +0000497 * Note: b is an unsigned integer type, thus
498 * Negative values of b are ignored.
Paul Bakker5121ce52009-01-03 21:22:43 +0000499 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000500 * \param X Destination MPI
501 * \param A Left-hand MPI
502 * \param b The integer value to multiply with
503 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000504 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000505 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000506 */
Paul Bakkera755ca12011-04-24 09:11:17 +0000507int mpi_mul_int( mpi *X, const mpi *A, t_sint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000508
509/**
510 * \brief Division by mpi: A = Q * B + R
511 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000512 * \param Q Destination MPI for the quotient
513 * \param R Destination MPI for the rest value
514 * \param A Left-hand MPI
515 * \param B Right-hand MPI
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 Bakker40e46942009-01-03 21:51:57 +0000519 * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if B == 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000520 *
521 * \note Either Q or R can be NULL.
522 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000523int mpi_div_mpi( mpi *Q, mpi *R, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000524
525/**
526 * \brief Division by int: A = Q * b + R
527 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000528 * \param Q Destination MPI for the quotient
529 * \param R Destination MPI for the rest value
530 * \param A Left-hand MPI
531 * \param b Integer to divide by
532 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000533 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000534 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000535 * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if b == 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000536 *
537 * \note Either Q or R can be NULL.
538 */
Paul Bakkera755ca12011-04-24 09:11:17 +0000539int mpi_div_int( mpi *Q, mpi *R, const mpi *A, t_sint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000540
541/**
542 * \brief Modulo: R = A mod B
543 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000544 * \param R Destination MPI for the rest value
545 * \param A Left-hand MPI
546 * \param B Right-hand MPI
547 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000548 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000549 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
Paul Bakkerce40a6d2009-06-23 19:46:08 +0000550 * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if B == 0,
551 * POLARSSL_ERR_MPI_NEGATIVE_VALUE if B < 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000552 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000553int mpi_mod_mpi( mpi *R, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000554
555/**
556 * \brief Modulo: r = A mod b
557 *
Paul Bakkera755ca12011-04-24 09:11:17 +0000558 * \param r Destination t_uint
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000559 * \param A Left-hand MPI
560 * \param b Integer to divide by
561 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000562 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000563 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
Paul Bakkerce40a6d2009-06-23 19:46:08 +0000564 * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if b == 0,
565 * POLARSSL_ERR_MPI_NEGATIVE_VALUE if b < 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000566 */
Paul Bakkera755ca12011-04-24 09:11:17 +0000567int mpi_mod_int( t_uint *r, const mpi *A, t_sint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000568
569/**
570 * \brief Sliding-window exponentiation: X = A^E mod N
571 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000572 * \param X Destination MPI
573 * \param A Left-hand MPI
574 * \param E Exponent MPI
575 * \param N Modular MPI
576 * \param _RR Speed-up MPI used for recalculations
577 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000578 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000579 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
Paul Bakkerf6198c12012-05-16 08:02:29 +0000580 * POLARSSL_ERR_MPI_BAD_INPUT_DATA if N is negative or even or if
581 * E is negative
Paul Bakker5121ce52009-01-03 21:22:43 +0000582 *
583 * \note _RR is used to avoid re-computing R*R mod N across
584 * multiple calls, which speeds up things a bit. It can
585 * be set to NULL if the extra performance is unneeded.
586 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000587int mpi_exp_mod( mpi *X, const mpi *A, const mpi *E, const mpi *N, mpi *_RR );
Paul Bakker5121ce52009-01-03 21:22:43 +0000588
589/**
Paul Bakker287781a2011-03-26 13:18:49 +0000590 * \brief Fill an MPI X with size bytes of random
591 *
592 * \param X Destination MPI
593 * \param size Size in bytes
594 * \param f_rng RNG function
595 * \param p_rng RNG parameter
596 *
597 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000598 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker287781a2011-03-26 13:18:49 +0000599 */
Paul Bakkera3d195c2011-11-27 21:07:34 +0000600int mpi_fill_random( mpi *X, size_t size,
601 int (*f_rng)(void *, unsigned char *, size_t),
602 void *p_rng );
Paul Bakker287781a2011-03-26 13:18:49 +0000603
604/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000605 * \brief Greatest common divisor: G = gcd(A, B)
606 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000607 * \param G Destination MPI
608 * \param A Left-hand MPI
609 * \param B Right-hand MPI
610 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000611 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000612 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000613 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000614int mpi_gcd( mpi *G, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000615
616/**
617 * \brief Modular inverse: X = A^-1 mod N
618 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000619 * \param X Destination MPI
620 * \param A Left-hand MPI
621 * \param N Right-hand MPI
622 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000623 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000624 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000625 * POLARSSL_ERR_MPI_BAD_INPUT_DATA if N is negative or nil
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000626 POLARSSL_ERR_MPI_NOT_ACCEPTABLE if A has no inverse mod N
Paul Bakker5121ce52009-01-03 21:22:43 +0000627 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000628int mpi_inv_mod( mpi *X, const mpi *A, const mpi *N );
Paul Bakker5121ce52009-01-03 21:22:43 +0000629
630/**
631 * \brief Miller-Rabin primality test
632 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000633 * \param X MPI to check
634 * \param f_rng RNG function
635 * \param p_rng RNG parameter
636 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000637 * \return 0 if successful (probably prime),
Paul Bakker69e095c2011-12-10 21:55:01 +0000638 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000639 * POLARSSL_ERR_MPI_NOT_ACCEPTABLE if X is not prime
Paul Bakker5121ce52009-01-03 21:22:43 +0000640 */
Paul Bakkera3d195c2011-11-27 21:07:34 +0000641int mpi_is_prime( mpi *X,
642 int (*f_rng)(void *, unsigned char *, size_t),
643 void *p_rng );
Paul Bakker5121ce52009-01-03 21:22:43 +0000644
645/**
646 * \brief Prime number generation
647 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000648 * \param X Destination MPI
Paul Bakkerfe3256e2011-11-25 12:11:43 +0000649 * \param nbits Required size of X in bits ( 3 <= nbits <= POLARSSL_MPI_MAX_BITS )
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000650 * \param dh_flag If 1, then (X-1)/2 will be prime too
Paul Bakker5121ce52009-01-03 21:22:43 +0000651 * \param f_rng RNG function
652 * \param p_rng RNG parameter
653 *
654 * \return 0 if successful (probably prime),
Paul Bakker69e095c2011-12-10 21:55:01 +0000655 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000656 * POLARSSL_ERR_MPI_BAD_INPUT_DATA if nbits is < 3
Paul Bakker5121ce52009-01-03 21:22:43 +0000657 */
Paul Bakker23986e52011-04-24 08:57:21 +0000658int mpi_gen_prime( mpi *X, size_t nbits, int dh_flag,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000659 int (*f_rng)(void *, unsigned char *, size_t),
660 void *p_rng );
Paul Bakker5121ce52009-01-03 21:22:43 +0000661
662/**
663 * \brief Checkup routine
664 *
665 * \return 0 if successful, or 1 if the test failed
666 */
667int mpi_self_test( int verbose );
668
669#ifdef __cplusplus
670}
671#endif
672
673#endif /* bignum.h */