blob: beecd898edcaceb0ce8ddca923d9bb86a4aa2bd8 [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 Bakker9d781402011-05-09 16:17:09 +000035#define POLARSSL_ERR_MPI_FILE_IO_ERROR -0x0002 /**< An error occurred while reading from or writing to a file. */
36#define POLARSSL_ERR_MPI_BAD_INPUT_DATA -0x0004 /**< Bad input parameters to function. */
37#define POLARSSL_ERR_MPI_INVALID_CHARACTER -0x0006 /**< There is an invalid character in the digit string. */
Paul Bakker69e095c2011-12-10 21:55:01 +000038#define POLARSSL_ERR_MPI_BUFFER_TOO_SMALL -0x0008 /**< The buffer is too small to write to. */
Paul Bakker9d781402011-05-09 16:17:09 +000039#define POLARSSL_ERR_MPI_NEGATIVE_VALUE -0x000A /**< The input arguments are negative or result in illegal output. */
40#define POLARSSL_ERR_MPI_DIVISION_BY_ZERO -0x000C /**< The input argument for division is zero, which is not allowed. */
41#define POLARSSL_ERR_MPI_NOT_ACCEPTABLE -0x000E /**< The input arguments are not acceptable. */
Paul Bakker69e095c2011-12-10 21:55:01 +000042#define POLARSSL_ERR_MPI_MALLOC_FAILED -0x0010 /**< Memory allocation failed. */
Paul Bakker5121ce52009-01-03 21:22:43 +000043
44#define MPI_CHK(f) if( ( ret = f ) != 0 ) goto cleanup
45
46/*
Paul Bakkerf9688572011-05-05 10:00:45 +000047 * Maximum size MPIs are allowed to grow to in number of limbs.
48 */
49#define POLARSSL_MPI_MAX_LIMBS 10000
50
51/*
Paul Bakkerb6d5f082011-11-25 11:52:11 +000052 * Maximum window size used for modular exponentiation. Default: 6
53 * Minimum value: 1. Maximum value: 6.
54 *
55 * Result is an array of ( 2 << POLARSSL_MPI_WINDOW_SIZE ) MPIs used
56 * for the sliding window calculation. (So 64 by default)
57 *
58 * Reduction in size, reduces speed.
59 */
60#define POLARSSL_MPI_WINDOW_SIZE 6 /**< Maximum windows size used. */
61
62/*
Paul Bakkerfe3256e2011-11-25 12:11:43 +000063 * Maximum size of MPIs allowed in bits and bytes for user-MPIs.
Paul Bakker5552c8c2012-07-05 13:31:54 +000064 * ( Default: 512 bytes => 4096 bits, Maximum: 1024 bytes => 8192 bits )
Paul Bakkerfe3256e2011-11-25 12:11:43 +000065 *
66 * Note: Calculations can results temporarily in larger MPIs. So the number
67 * of limbs required (POLARSSL_MPI_MAX_LIMBS) is higher.
68 */
69#define POLARSSL_MPI_MAX_SIZE 512 /**< Maximum number of bytes for usable MPIs. */
70#define POLARSSL_MPI_MAX_BITS ( 8 * POLARSSL_MPI_MAX_SIZE ) /**< Maximum number of bits for usable MPIs. */
71
72/*
Paul Bakker5531c6d2012-09-26 19:20:46 +000073 * When reading from files with mpi_read_file() and writing to files with
74 * mpi_write_file() the buffer should have space
Paul Bakkercb37aa52011-11-30 16:00:20 +000075 * for a (short) label, the MPI (in the provided radix), the newline
76 * characters and the '\0'.
77 *
78 * By default we assume at least a 10 char label, a minimum radix of 10
79 * (decimal) and a maximum of 4096 bit numbers (1234 decimal chars).
80 */
Paul Bakker5531c6d2012-09-26 19:20:46 +000081#define POLARSSL_MPI_RW_BUFFER_SIZE 1250
Paul Bakkercb37aa52011-11-30 16:00:20 +000082
83/*
Paul Bakker5121ce52009-01-03 21:22:43 +000084 * Define the base integer type, architecture-wise
85 */
Paul Bakker40e46942009-01-03 21:51:57 +000086#if defined(POLARSSL_HAVE_INT8)
Paul Bakkera755ca12011-04-24 09:11:17 +000087typedef signed char t_sint;
88typedef unsigned char t_uint;
89typedef unsigned short t_udbl;
Paul Bakker5121ce52009-01-03 21:22:43 +000090#else
Paul Bakker40e46942009-01-03 21:51:57 +000091#if defined(POLARSSL_HAVE_INT16)
Paul Bakkera755ca12011-04-24 09:11:17 +000092typedef signed short t_sint;
93typedef unsigned short t_uint;
94typedef unsigned long t_udbl;
Paul Bakker5121ce52009-01-03 21:22:43 +000095#else
Paul Bakkera755ca12011-04-24 09:11:17 +000096 typedef signed long t_sint;
97 typedef unsigned long t_uint;
Paul Bakker5121ce52009-01-03 21:22:43 +000098 #if defined(_MSC_VER) && defined(_M_IX86)
Paul Bakkera755ca12011-04-24 09:11:17 +000099 typedef unsigned __int64 t_udbl;
Paul Bakker5121ce52009-01-03 21:22:43 +0000100 #else
Paul Bakkercf0360a2012-01-20 10:08:14 +0000101 #if defined(__GNUC__) && ( \
102 defined(__amd64__) || defined(__x86_64__) || \
Paul Bakker5121ce52009-01-03 21:22:43 +0000103 defined(__ppc64__) || defined(__powerpc64__) || \
Paul Bakker44637402011-11-26 09:23:07 +0000104 defined(__ia64__) || defined(__alpha__) || \
105 (defined(__sparc__) && defined(__arch64__)) || \
Paul Bakkercf0360a2012-01-20 10:08:14 +0000106 defined(__s390x__) )
Paul Bakkera755ca12011-04-24 09:11:17 +0000107 typedef unsigned int t_udbl __attribute__((mode(TI)));
Paul Bakker66219872012-01-22 20:38:13 +0000108 #define POLARSSL_HAVE_LONGLONG
Paul Bakker5121ce52009-01-03 21:22:43 +0000109 #else
Paul Bakker1a9382e2009-07-11 16:35:32 +0000110 #if defined(POLARSSL_HAVE_LONGLONG)
Paul Bakkera755ca12011-04-24 09:11:17 +0000111 typedef unsigned long long t_udbl;
Paul Bakker1a9382e2009-07-11 16:35:32 +0000112 #endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000113 #endif
114 #endif
115#endif
116#endif
117
118/**
119 * \brief MPI structure
120 */
121typedef struct
122{
123 int s; /*!< integer sign */
Paul Bakker23986e52011-04-24 08:57:21 +0000124 size_t n; /*!< total # of limbs */
Paul Bakkera755ca12011-04-24 09:11:17 +0000125 t_uint *p; /*!< pointer to limbs */
Paul Bakker5121ce52009-01-03 21:22:43 +0000126}
127mpi;
128
129#ifdef __cplusplus
130extern "C" {
131#endif
132
133/**
Paul Bakker6c591fa2011-05-05 11:49:20 +0000134 * \brief Initialize one MPI
135 *
136 * \param X One MPI to initialize.
Paul Bakker5121ce52009-01-03 21:22:43 +0000137 */
Paul Bakker6c591fa2011-05-05 11:49:20 +0000138void mpi_init( mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000139
140/**
Paul Bakker6c591fa2011-05-05 11:49:20 +0000141 * \brief Unallocate one MPI
142 *
143 * \param X One MPI to unallocate.
Paul Bakker5121ce52009-01-03 21:22:43 +0000144 */
Paul Bakker6c591fa2011-05-05 11:49:20 +0000145void mpi_free( mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000146
147/**
148 * \brief Enlarge to the specified number of limbs
149 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000150 * \param X MPI to grow
151 * \param nblimbs The target number of limbs
152 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000153 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000154 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000155 */
Paul Bakker23986e52011-04-24 08:57:21 +0000156int mpi_grow( mpi *X, size_t nblimbs );
Paul Bakker5121ce52009-01-03 21:22:43 +0000157
158/**
159 * \brief Copy the contents of Y into X
160 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000161 * \param X Destination MPI
162 * \param Y Source MPI
163 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000164 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000165 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000166 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000167int mpi_copy( mpi *X, const mpi *Y );
Paul Bakker5121ce52009-01-03 21:22:43 +0000168
169/**
170 * \brief Swap the contents of X and Y
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000171 *
172 * \param X First MPI value
173 * \param Y Second MPI value
Paul Bakker5121ce52009-01-03 21:22:43 +0000174 */
175void mpi_swap( mpi *X, mpi *Y );
176
177/**
178 * \brief Set value from integer
179 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000180 * \param X MPI to set
181 * \param z Value to use
182 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000183 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000184 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000185 */
Paul Bakkera755ca12011-04-24 09:11:17 +0000186int mpi_lset( mpi *X, t_sint z );
Paul Bakker5121ce52009-01-03 21:22:43 +0000187
Paul Bakker2f5947e2011-05-18 15:47:11 +0000188/*
189 * \brief Get a specific bit from X
190 *
191 * \param X MPI to use
192 * \param pos Zero-based index of the bit in X
193 *
194 * \return Either a 0 or a 1
195 */
Paul Bakker6b906e52012-05-08 12:01:43 +0000196int mpi_get_bit( const mpi *X, size_t pos );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000197
198/*
199 * \brief Set a bit of X to a specific value of 0 or 1
200 *
201 * \note Will grow X if necessary to set a bit to 1 in a not yet
202 * existing limb. Will not grow if bit should be set to 0
203 *
204 * \param X MPI to use
205 * \param pos Zero-based index of the bit in X
206 * \param val The value to set the bit to (0 or 1)
207 *
208 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000209 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
Paul Bakker2f5947e2011-05-18 15:47:11 +0000210 * POLARSSL_ERR_MPI_BAD_INPUT_DATA if val is not 0 or 1
211 */
212int mpi_set_bit( mpi *X, size_t pos, unsigned char val );
213
Paul Bakker5121ce52009-01-03 21:22:43 +0000214/**
Paul Bakker6b906e52012-05-08 12:01:43 +0000215 * \brief Return the number of zero-bits before the least significant
216 * '1' bit
217 *
218 * Note: Thus also the zero-based index of the least significant '1' bit
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000219 *
220 * \param X MPI to use
Paul Bakker5121ce52009-01-03 21:22:43 +0000221 */
Paul Bakker23986e52011-04-24 08:57:21 +0000222size_t mpi_lsb( const mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000223
224/**
Paul Bakker6b906e52012-05-08 12:01:43 +0000225 * \brief Return the number of bits up to and including the most
226 * significant '1' bit'
227 *
228 * Note: Thus also the one-based index of the most significant '1' bit
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000229 *
230 * \param X MPI to use
Paul Bakker5121ce52009-01-03 21:22:43 +0000231 */
Paul Bakker23986e52011-04-24 08:57:21 +0000232size_t mpi_msb( const mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000233
234/**
235 * \brief Return the total size in bytes
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000236 *
237 * \param X MPI to use
Paul Bakker5121ce52009-01-03 21:22:43 +0000238 */
Paul Bakker23986e52011-04-24 08:57:21 +0000239size_t mpi_size( const mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000240
241/**
242 * \brief Import from an ASCII string
243 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000244 * \param X Destination MPI
245 * \param radix Input numeric base
246 * \param s Null-terminated string buffer
Paul Bakker5121ce52009-01-03 21:22:43 +0000247 *
Paul Bakkercb37aa52011-11-30 16:00:20 +0000248 * \return 0 if successful, or a POLARSSL_ERR_MPI_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000249 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000250int mpi_read_string( mpi *X, int radix, const char *s );
Paul Bakker5121ce52009-01-03 21:22:43 +0000251
252/**
253 * \brief Export into an ASCII string
254 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000255 * \param X Source MPI
256 * \param radix Output numeric base
257 * \param s String buffer
258 * \param slen String buffer size
Paul Bakker5121ce52009-01-03 21:22:43 +0000259 *
Paul Bakkercb37aa52011-11-30 16:00:20 +0000260 * \return 0 if successful, or a POLARSSL_ERR_MPI_XXX error code.
Paul Bakkerff60ee62010-03-16 21:09:09 +0000261 * *slen is always updated to reflect the amount
262 * of data that has (or would have) been written.
Paul Bakker5121ce52009-01-03 21:22:43 +0000263 *
264 * \note Call this function with *slen = 0 to obtain the
265 * minimum required buffer size in *slen.
266 */
Paul Bakker23986e52011-04-24 08:57:21 +0000267int mpi_write_string( const mpi *X, int radix, char *s, size_t *slen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000268
269/**
270 * \brief Read X from an opened file
271 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000272 * \param X Destination MPI
273 * \param radix Input numeric base
274 * \param fin Input file handle
Paul Bakker5121ce52009-01-03 21:22:43 +0000275 *
Paul Bakkercb37aa52011-11-30 16:00:20 +0000276 * \return 0 if successful, POLARSSL_ERR_MPI_BUFFER_TOO_SMALL if
277 * the file read buffer is too small or a
278 * POLARSSL_ERR_MPI_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000279 */
280int mpi_read_file( mpi *X, int radix, FILE *fin );
281
282/**
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000283 * \brief Write X into an opened file, or stdout if fout is NULL
Paul Bakker5121ce52009-01-03 21:22:43 +0000284 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000285 * \param p Prefix, can be NULL
286 * \param X Source MPI
287 * \param radix Output numeric base
288 * \param fout Output file handle (can be NULL)
Paul Bakker5121ce52009-01-03 21:22:43 +0000289 *
Paul Bakkercb37aa52011-11-30 16:00:20 +0000290 * \return 0 if successful, or a POLARSSL_ERR_MPI_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000291 *
292 * \note Set fout == NULL to print X on the console.
293 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000294int mpi_write_file( const char *p, const mpi *X, int radix, FILE *fout );
Paul Bakker5121ce52009-01-03 21:22:43 +0000295
296/**
297 * \brief Import X from unsigned binary data, big endian
298 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000299 * \param X Destination MPI
300 * \param buf Input buffer
301 * \param buflen Input buffer size
Paul Bakker5121ce52009-01-03 21:22:43 +0000302 *
303 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000304 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000305 */
Paul Bakker23986e52011-04-24 08:57:21 +0000306int mpi_read_binary( mpi *X, const unsigned char *buf, size_t buflen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000307
308/**
309 * \brief Export X into unsigned binary data, big endian
310 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000311 * \param X Source MPI
312 * \param buf Output buffer
313 * \param buflen Output buffer size
Paul Bakker5121ce52009-01-03 21:22:43 +0000314 *
315 * \return 0 if successful,
Paul Bakker40e46942009-01-03 21:51:57 +0000316 * POLARSSL_ERR_MPI_BUFFER_TOO_SMALL if buf isn't large enough
Paul Bakker5121ce52009-01-03 21:22:43 +0000317 */
Paul Bakker23986e52011-04-24 08:57:21 +0000318int mpi_write_binary( const mpi *X, unsigned char *buf, size_t buflen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000319
320/**
321 * \brief Left-shift: X <<= count
322 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000323 * \param X MPI to shift
324 * \param count Amount to shift
325 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000326 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000327 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000328 */
Paul Bakker23986e52011-04-24 08:57:21 +0000329int mpi_shift_l( mpi *X, size_t count );
Paul Bakker5121ce52009-01-03 21:22:43 +0000330
331/**
332 * \brief Right-shift: X >>= count
333 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000334 * \param X MPI to shift
335 * \param count Amount to shift
336 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000337 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000338 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000339 */
Paul Bakker23986e52011-04-24 08:57:21 +0000340int mpi_shift_r( mpi *X, size_t count );
Paul Bakker5121ce52009-01-03 21:22:43 +0000341
342/**
343 * \brief Compare unsigned values
344 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000345 * \param X Left-hand MPI
346 * \param Y Right-hand MPI
347 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000348 * \return 1 if |X| is greater than |Y|,
349 * -1 if |X| is lesser than |Y| or
350 * 0 if |X| is equal to |Y|
351 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000352int mpi_cmp_abs( const mpi *X, const mpi *Y );
Paul Bakker5121ce52009-01-03 21:22:43 +0000353
354/**
355 * \brief Compare signed values
356 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000357 * \param X Left-hand MPI
358 * \param Y Right-hand MPI
359 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000360 * \return 1 if X is greater than Y,
361 * -1 if X is lesser than Y or
362 * 0 if X is equal to Y
363 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000364int mpi_cmp_mpi( const mpi *X, const mpi *Y );
Paul Bakker5121ce52009-01-03 21:22:43 +0000365
366/**
367 * \brief Compare signed values
368 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000369 * \param X Left-hand MPI
370 * \param z The integer value to compare to
371 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000372 * \return 1 if X is greater than z,
373 * -1 if X is lesser than z or
374 * 0 if X is equal to z
375 */
Paul Bakkera755ca12011-04-24 09:11:17 +0000376int mpi_cmp_int( const mpi *X, t_sint z );
Paul Bakker5121ce52009-01-03 21:22:43 +0000377
378/**
379 * \brief Unsigned addition: X = |A| + |B|
380 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000381 * \param X Destination MPI
382 * \param A Left-hand MPI
383 * \param B Right-hand MPI
384 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000385 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000386 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000387 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000388int mpi_add_abs( mpi *X, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000389
390/**
391 * \brief Unsigned substraction: X = |A| - |B|
392 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000393 * \param X Destination MPI
394 * \param A Left-hand MPI
395 * \param B Right-hand MPI
396 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000397 * \return 0 if successful,
Paul Bakker40e46942009-01-03 21:51:57 +0000398 * POLARSSL_ERR_MPI_NEGATIVE_VALUE if B is greater than A
Paul Bakker5121ce52009-01-03 21:22:43 +0000399 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000400int mpi_sub_abs( mpi *X, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000401
402/**
403 * \brief Signed addition: X = A + B
404 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000405 * \param X Destination MPI
406 * \param A Left-hand MPI
407 * \param B Right-hand MPI
408 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000409 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000410 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000411 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000412int mpi_add_mpi( mpi *X, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000413
414/**
415 * \brief Signed substraction: X = A - B
416 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000417 * \param X Destination MPI
418 * \param A Left-hand MPI
419 * \param B Right-hand MPI
420 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000421 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000422 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000423 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000424int mpi_sub_mpi( mpi *X, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000425
426/**
427 * \brief Signed addition: X = A + b
428 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000429 * \param X Destination MPI
430 * \param A Left-hand MPI
431 * \param b The integer value to add
432 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000433 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000434 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000435 */
Paul Bakkera755ca12011-04-24 09:11:17 +0000436int mpi_add_int( mpi *X, const mpi *A, t_sint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000437
438/**
439 * \brief Signed substraction: X = A - b
440 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000441 * \param X Destination MPI
442 * \param A Left-hand MPI
443 * \param b The integer value to subtract
444 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000445 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000446 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000447 */
Paul Bakkera755ca12011-04-24 09:11:17 +0000448int mpi_sub_int( mpi *X, const mpi *A, t_sint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000449
450/**
451 * \brief Baseline multiplication: X = A * B
452 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000453 * \param X Destination MPI
454 * \param A Left-hand MPI
455 * \param B Right-hand MPI
456 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000457 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000458 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000459 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000460int mpi_mul_mpi( mpi *X, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000461
462/**
463 * \brief Baseline multiplication: X = A * b
Paul Bakkerce40a6d2009-06-23 19:46:08 +0000464 * Note: b is an unsigned integer type, thus
465 * Negative values of b are ignored.
Paul Bakker5121ce52009-01-03 21:22:43 +0000466 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000467 * \param X Destination MPI
468 * \param A Left-hand MPI
469 * \param b The integer value to multiply with
470 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000471 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000472 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000473 */
Paul Bakkera755ca12011-04-24 09:11:17 +0000474int mpi_mul_int( mpi *X, const mpi *A, t_sint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000475
476/**
477 * \brief Division by mpi: A = Q * B + R
478 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000479 * \param Q Destination MPI for the quotient
480 * \param R Destination MPI for the rest value
481 * \param A Left-hand MPI
482 * \param B Right-hand MPI
483 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000484 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000485 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000486 * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if B == 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000487 *
488 * \note Either Q or R can be NULL.
489 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000490int mpi_div_mpi( mpi *Q, mpi *R, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000491
492/**
493 * \brief Division by int: A = Q * b + R
494 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000495 * \param Q Destination MPI for the quotient
496 * \param R Destination MPI for the rest value
497 * \param A Left-hand MPI
498 * \param b Integer to divide by
499 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000500 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000501 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000502 * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if b == 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000503 *
504 * \note Either Q or R can be NULL.
505 */
Paul Bakkera755ca12011-04-24 09:11:17 +0000506int mpi_div_int( mpi *Q, mpi *R, const mpi *A, t_sint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000507
508/**
509 * \brief Modulo: R = A mod B
510 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000511 * \param R Destination MPI for the rest value
512 * \param A Left-hand MPI
513 * \param B Right-hand MPI
514 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000515 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000516 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
Paul Bakkerce40a6d2009-06-23 19:46:08 +0000517 * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if B == 0,
518 * POLARSSL_ERR_MPI_NEGATIVE_VALUE if B < 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000519 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000520int mpi_mod_mpi( mpi *R, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000521
522/**
523 * \brief Modulo: r = A mod b
524 *
Paul Bakkera755ca12011-04-24 09:11:17 +0000525 * \param r Destination t_uint
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000526 * \param A Left-hand MPI
527 * \param b Integer to divide by
528 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000529 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000530 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
Paul Bakkerce40a6d2009-06-23 19:46:08 +0000531 * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if b == 0,
532 * POLARSSL_ERR_MPI_NEGATIVE_VALUE if b < 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000533 */
Paul Bakkera755ca12011-04-24 09:11:17 +0000534int mpi_mod_int( t_uint *r, const mpi *A, t_sint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000535
536/**
537 * \brief Sliding-window exponentiation: X = A^E mod N
538 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000539 * \param X Destination MPI
540 * \param A Left-hand MPI
541 * \param E Exponent MPI
542 * \param N Modular MPI
543 * \param _RR Speed-up MPI used for recalculations
544 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000545 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000546 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
Paul Bakkerf6198c12012-05-16 08:02:29 +0000547 * POLARSSL_ERR_MPI_BAD_INPUT_DATA if N is negative or even or if
548 * E is negative
Paul Bakker5121ce52009-01-03 21:22:43 +0000549 *
550 * \note _RR is used to avoid re-computing R*R mod N across
551 * multiple calls, which speeds up things a bit. It can
552 * be set to NULL if the extra performance is unneeded.
553 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000554int mpi_exp_mod( mpi *X, const mpi *A, const mpi *E, const mpi *N, mpi *_RR );
Paul Bakker5121ce52009-01-03 21:22:43 +0000555
556/**
Paul Bakker287781a2011-03-26 13:18:49 +0000557 * \brief Fill an MPI X with size bytes of random
558 *
559 * \param X Destination MPI
560 * \param size Size in bytes
561 * \param f_rng RNG function
562 * \param p_rng RNG parameter
563 *
564 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000565 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker287781a2011-03-26 13:18:49 +0000566 */
Paul Bakkera3d195c2011-11-27 21:07:34 +0000567int mpi_fill_random( mpi *X, size_t size,
568 int (*f_rng)(void *, unsigned char *, size_t),
569 void *p_rng );
Paul Bakker287781a2011-03-26 13:18:49 +0000570
571/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000572 * \brief Greatest common divisor: G = gcd(A, B)
573 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000574 * \param G Destination MPI
575 * \param A Left-hand MPI
576 * \param B Right-hand MPI
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 Bakker5121ce52009-01-03 21:22:43 +0000580 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000581int mpi_gcd( mpi *G, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000582
583/**
584 * \brief Modular inverse: X = A^-1 mod N
585 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000586 * \param X Destination MPI
587 * \param A Left-hand MPI
588 * \param N Right-hand MPI
589 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000590 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000591 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000592 * POLARSSL_ERR_MPI_BAD_INPUT_DATA if N is negative or nil
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000593 POLARSSL_ERR_MPI_NOT_ACCEPTABLE if A has no inverse mod N
Paul Bakker5121ce52009-01-03 21:22:43 +0000594 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000595int mpi_inv_mod( mpi *X, const mpi *A, const mpi *N );
Paul Bakker5121ce52009-01-03 21:22:43 +0000596
597/**
598 * \brief Miller-Rabin primality test
599 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000600 * \param X MPI to check
601 * \param f_rng RNG function
602 * \param p_rng RNG parameter
603 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000604 * \return 0 if successful (probably prime),
Paul Bakker69e095c2011-12-10 21:55:01 +0000605 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000606 * POLARSSL_ERR_MPI_NOT_ACCEPTABLE if X is not prime
Paul Bakker5121ce52009-01-03 21:22:43 +0000607 */
Paul Bakkera3d195c2011-11-27 21:07:34 +0000608int mpi_is_prime( mpi *X,
609 int (*f_rng)(void *, unsigned char *, size_t),
610 void *p_rng );
Paul Bakker5121ce52009-01-03 21:22:43 +0000611
612/**
613 * \brief Prime number generation
614 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000615 * \param X Destination MPI
Paul Bakkerfe3256e2011-11-25 12:11:43 +0000616 * \param nbits Required size of X in bits ( 3 <= nbits <= POLARSSL_MPI_MAX_BITS )
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000617 * \param dh_flag If 1, then (X-1)/2 will be prime too
Paul Bakker5121ce52009-01-03 21:22:43 +0000618 * \param f_rng RNG function
619 * \param p_rng RNG parameter
620 *
621 * \return 0 if successful (probably prime),
Paul Bakker69e095c2011-12-10 21:55:01 +0000622 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000623 * POLARSSL_ERR_MPI_BAD_INPUT_DATA if nbits is < 3
Paul Bakker5121ce52009-01-03 21:22:43 +0000624 */
Paul Bakker23986e52011-04-24 08:57:21 +0000625int mpi_gen_prime( mpi *X, size_t nbits, int dh_flag,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000626 int (*f_rng)(void *, unsigned char *, size_t),
627 void *p_rng );
Paul Bakker5121ce52009-01-03 21:22:43 +0000628
629/**
630 * \brief Checkup routine
631 *
632 * \return 0 if successful, or 1 if the test failed
633 */
634int mpi_self_test( int verbose );
635
636#ifdef __cplusplus
637}
638#endif
639
640#endif /* bignum.h */