blob: ad033084c1a665905d3691425fce386405409f4a [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.
64 * ( Default: 512 bytes => 4096 bits )
65 *
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 Bakkercb37aa52011-11-30 16:00:20 +000073 * When reading from files with mpi_read_file() the buffer should have space
74 * for a (short) label, the MPI (in the provided radix), the newline
75 * characters and the '\0'.
76 *
77 * By default we assume at least a 10 char label, a minimum radix of 10
78 * (decimal) and a maximum of 4096 bit numbers (1234 decimal chars).
79 */
80#define POLARSSL_MPI_READ_BUFFER_SIZE 1250
81
82/*
Paul Bakker5121ce52009-01-03 21:22:43 +000083 * Define the base integer type, architecture-wise
84 */
Paul Bakker40e46942009-01-03 21:51:57 +000085#if defined(POLARSSL_HAVE_INT8)
Paul Bakkera755ca12011-04-24 09:11:17 +000086typedef signed char t_sint;
87typedef unsigned char t_uint;
88typedef unsigned short t_udbl;
Paul Bakker5121ce52009-01-03 21:22:43 +000089#else
Paul Bakker40e46942009-01-03 21:51:57 +000090#if defined(POLARSSL_HAVE_INT16)
Paul Bakkera755ca12011-04-24 09:11:17 +000091typedef signed short t_sint;
92typedef unsigned short t_uint;
93typedef unsigned long t_udbl;
Paul Bakker5121ce52009-01-03 21:22:43 +000094#else
Paul Bakkera755ca12011-04-24 09:11:17 +000095 typedef signed long t_sint;
96 typedef unsigned long t_uint;
Paul Bakker5121ce52009-01-03 21:22:43 +000097 #if defined(_MSC_VER) && defined(_M_IX86)
Paul Bakkera755ca12011-04-24 09:11:17 +000098 typedef unsigned __int64 t_udbl;
Paul Bakker5121ce52009-01-03 21:22:43 +000099 #else
Paul Bakkercf0360a2012-01-20 10:08:14 +0000100 #if defined(__GNUC__) && ( \
101 defined(__amd64__) || defined(__x86_64__) || \
Paul Bakker5121ce52009-01-03 21:22:43 +0000102 defined(__ppc64__) || defined(__powerpc64__) || \
Paul Bakker44637402011-11-26 09:23:07 +0000103 defined(__ia64__) || defined(__alpha__) || \
104 (defined(__sparc__) && defined(__arch64__)) || \
Paul Bakkercf0360a2012-01-20 10:08:14 +0000105 defined(__s390x__) )
Paul Bakkera755ca12011-04-24 09:11:17 +0000106 typedef unsigned int t_udbl __attribute__((mode(TI)));
Paul Bakker66219872012-01-22 20:38:13 +0000107 #define POLARSSL_HAVE_LONGLONG
Paul Bakker5121ce52009-01-03 21:22:43 +0000108 #else
Paul Bakker1a9382e2009-07-11 16:35:32 +0000109 #if defined(POLARSSL_HAVE_LONGLONG)
Paul Bakkera755ca12011-04-24 09:11:17 +0000110 typedef unsigned long long t_udbl;
Paul Bakker1a9382e2009-07-11 16:35:32 +0000111 #endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000112 #endif
113 #endif
114#endif
115#endif
116
117/**
118 * \brief MPI structure
119 */
120typedef struct
121{
122 int s; /*!< integer sign */
Paul Bakker23986e52011-04-24 08:57:21 +0000123 size_t n; /*!< total # of limbs */
Paul Bakkera755ca12011-04-24 09:11:17 +0000124 t_uint *p; /*!< pointer to limbs */
Paul Bakker5121ce52009-01-03 21:22:43 +0000125}
126mpi;
127
128#ifdef __cplusplus
129extern "C" {
130#endif
131
132/**
Paul Bakker6c591fa2011-05-05 11:49:20 +0000133 * \brief Initialize one MPI
134 *
135 * \param X One MPI to initialize.
Paul Bakker5121ce52009-01-03 21:22:43 +0000136 */
Paul Bakker6c591fa2011-05-05 11:49:20 +0000137void mpi_init( mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000138
139/**
Paul Bakker6c591fa2011-05-05 11:49:20 +0000140 * \brief Unallocate one MPI
141 *
142 * \param X One MPI to unallocate.
Paul Bakker5121ce52009-01-03 21:22:43 +0000143 */
Paul Bakker6c591fa2011-05-05 11:49:20 +0000144void mpi_free( mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000145
146/**
147 * \brief Enlarge to the specified number of limbs
148 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000149 * \param X MPI to grow
150 * \param nblimbs The target number of limbs
151 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000152 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000153 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000154 */
Paul Bakker23986e52011-04-24 08:57:21 +0000155int mpi_grow( mpi *X, size_t nblimbs );
Paul Bakker5121ce52009-01-03 21:22:43 +0000156
157/**
158 * \brief Copy the contents of Y into X
159 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000160 * \param X Destination MPI
161 * \param Y Source MPI
162 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000163 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000164 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000165 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000166int mpi_copy( mpi *X, const mpi *Y );
Paul Bakker5121ce52009-01-03 21:22:43 +0000167
168/**
169 * \brief Swap the contents of X and Y
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000170 *
171 * \param X First MPI value
172 * \param Y Second MPI value
Paul Bakker5121ce52009-01-03 21:22:43 +0000173 */
174void mpi_swap( mpi *X, mpi *Y );
175
176/**
177 * \brief Set value from integer
178 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000179 * \param X MPI to set
180 * \param z Value to use
181 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000182 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000183 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000184 */
Paul Bakkera755ca12011-04-24 09:11:17 +0000185int mpi_lset( mpi *X, t_sint z );
Paul Bakker5121ce52009-01-03 21:22:43 +0000186
Paul Bakker2f5947e2011-05-18 15:47:11 +0000187/*
188 * \brief Get a specific bit from X
189 *
190 * \param X MPI to use
191 * \param pos Zero-based index of the bit in X
192 *
193 * \return Either a 0 or a 1
194 */
195int mpi_get_bit( mpi *X, size_t pos );
196
197/*
198 * \brief Set a bit of X to a specific value of 0 or 1
199 *
200 * \note Will grow X if necessary to set a bit to 1 in a not yet
201 * existing limb. Will not grow if bit should be set to 0
202 *
203 * \param X MPI to use
204 * \param pos Zero-based index of the bit in X
205 * \param val The value to set the bit to (0 or 1)
206 *
207 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000208 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
Paul Bakker2f5947e2011-05-18 15:47:11 +0000209 * POLARSSL_ERR_MPI_BAD_INPUT_DATA if val is not 0 or 1
210 */
211int mpi_set_bit( mpi *X, size_t pos, unsigned char val );
212
Paul Bakker5121ce52009-01-03 21:22:43 +0000213/**
214 * \brief Return the number of least significant bits
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000215 *
216 * \param X MPI to use
Paul Bakker5121ce52009-01-03 21:22:43 +0000217 */
Paul Bakker23986e52011-04-24 08:57:21 +0000218size_t mpi_lsb( const mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000219
220/**
221 * \brief Return the number of most significant bits
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000222 *
223 * \param X MPI to use
Paul Bakker5121ce52009-01-03 21:22:43 +0000224 */
Paul Bakker23986e52011-04-24 08:57:21 +0000225size_t mpi_msb( const mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000226
227/**
228 * \brief Return the total size in bytes
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_size( const mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000233
234/**
235 * \brief Import from an ASCII string
236 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000237 * \param X Destination MPI
238 * \param radix Input numeric base
239 * \param s Null-terminated string buffer
Paul Bakker5121ce52009-01-03 21:22:43 +0000240 *
Paul Bakkercb37aa52011-11-30 16:00:20 +0000241 * \return 0 if successful, or a POLARSSL_ERR_MPI_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000242 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000243int mpi_read_string( mpi *X, int radix, const char *s );
Paul Bakker5121ce52009-01-03 21:22:43 +0000244
245/**
246 * \brief Export into an ASCII string
247 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000248 * \param X Source MPI
249 * \param radix Output numeric base
250 * \param s String buffer
251 * \param slen String buffer size
Paul Bakker5121ce52009-01-03 21:22:43 +0000252 *
Paul Bakkercb37aa52011-11-30 16:00:20 +0000253 * \return 0 if successful, or a POLARSSL_ERR_MPI_XXX error code.
Paul Bakkerff60ee62010-03-16 21:09:09 +0000254 * *slen is always updated to reflect the amount
255 * of data that has (or would have) been written.
Paul Bakker5121ce52009-01-03 21:22:43 +0000256 *
257 * \note Call this function with *slen = 0 to obtain the
258 * minimum required buffer size in *slen.
259 */
Paul Bakker23986e52011-04-24 08:57:21 +0000260int mpi_write_string( const mpi *X, int radix, char *s, size_t *slen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000261
262/**
263 * \brief Read X from an opened file
264 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000265 * \param X Destination MPI
266 * \param radix Input numeric base
267 * \param fin Input file handle
Paul Bakker5121ce52009-01-03 21:22:43 +0000268 *
Paul Bakkercb37aa52011-11-30 16:00:20 +0000269 * \return 0 if successful, POLARSSL_ERR_MPI_BUFFER_TOO_SMALL if
270 * the file read buffer is too small or a
271 * POLARSSL_ERR_MPI_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000272 */
273int mpi_read_file( mpi *X, int radix, FILE *fin );
274
275/**
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000276 * \brief Write X into an opened file, or stdout if fout is NULL
Paul Bakker5121ce52009-01-03 21:22:43 +0000277 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000278 * \param p Prefix, can be NULL
279 * \param X Source MPI
280 * \param radix Output numeric base
281 * \param fout Output file handle (can be NULL)
Paul Bakker5121ce52009-01-03 21:22:43 +0000282 *
Paul Bakkercb37aa52011-11-30 16:00:20 +0000283 * \return 0 if successful, or a POLARSSL_ERR_MPI_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000284 *
285 * \note Set fout == NULL to print X on the console.
286 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000287int mpi_write_file( const char *p, const mpi *X, int radix, FILE *fout );
Paul Bakker5121ce52009-01-03 21:22:43 +0000288
289/**
290 * \brief Import X from unsigned binary data, big endian
291 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000292 * \param X Destination MPI
293 * \param buf Input buffer
294 * \param buflen Input buffer size
Paul Bakker5121ce52009-01-03 21:22:43 +0000295 *
296 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000297 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000298 */
Paul Bakker23986e52011-04-24 08:57:21 +0000299int mpi_read_binary( mpi *X, const unsigned char *buf, size_t buflen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000300
301/**
302 * \brief Export X into unsigned binary data, big endian
303 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000304 * \param X Source MPI
305 * \param buf Output buffer
306 * \param buflen Output buffer size
Paul Bakker5121ce52009-01-03 21:22:43 +0000307 *
308 * \return 0 if successful,
Paul Bakker40e46942009-01-03 21:51:57 +0000309 * POLARSSL_ERR_MPI_BUFFER_TOO_SMALL if buf isn't large enough
Paul Bakker5121ce52009-01-03 21:22:43 +0000310 */
Paul Bakker23986e52011-04-24 08:57:21 +0000311int mpi_write_binary( const mpi *X, unsigned char *buf, size_t buflen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000312
313/**
314 * \brief Left-shift: X <<= count
315 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000316 * \param X MPI to shift
317 * \param count Amount to shift
318 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000319 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000320 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000321 */
Paul Bakker23986e52011-04-24 08:57:21 +0000322int mpi_shift_l( mpi *X, size_t count );
Paul Bakker5121ce52009-01-03 21:22:43 +0000323
324/**
325 * \brief Right-shift: X >>= count
326 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000327 * \param X MPI to shift
328 * \param count Amount to shift
329 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000330 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000331 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000332 */
Paul Bakker23986e52011-04-24 08:57:21 +0000333int mpi_shift_r( mpi *X, size_t count );
Paul Bakker5121ce52009-01-03 21:22:43 +0000334
335/**
336 * \brief Compare unsigned values
337 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000338 * \param X Left-hand MPI
339 * \param Y Right-hand MPI
340 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000341 * \return 1 if |X| is greater than |Y|,
342 * -1 if |X| is lesser than |Y| or
343 * 0 if |X| is equal to |Y|
344 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000345int mpi_cmp_abs( const mpi *X, const mpi *Y );
Paul Bakker5121ce52009-01-03 21:22:43 +0000346
347/**
348 * \brief Compare signed values
349 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000350 * \param X Left-hand MPI
351 * \param Y Right-hand MPI
352 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000353 * \return 1 if X is greater than Y,
354 * -1 if X is lesser than Y or
355 * 0 if X is equal to Y
356 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000357int mpi_cmp_mpi( const mpi *X, const mpi *Y );
Paul Bakker5121ce52009-01-03 21:22:43 +0000358
359/**
360 * \brief Compare signed values
361 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000362 * \param X Left-hand MPI
363 * \param z The integer value to compare to
364 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000365 * \return 1 if X is greater than z,
366 * -1 if X is lesser than z or
367 * 0 if X is equal to z
368 */
Paul Bakkera755ca12011-04-24 09:11:17 +0000369int mpi_cmp_int( const mpi *X, t_sint z );
Paul Bakker5121ce52009-01-03 21:22:43 +0000370
371/**
372 * \brief Unsigned addition: X = |A| + |B|
373 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000374 * \param X Destination MPI
375 * \param A Left-hand MPI
376 * \param B Right-hand MPI
377 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000378 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000379 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000380 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000381int mpi_add_abs( mpi *X, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000382
383/**
384 * \brief Unsigned substraction: X = |A| - |B|
385 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000386 * \param X Destination MPI
387 * \param A Left-hand MPI
388 * \param B Right-hand MPI
389 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000390 * \return 0 if successful,
Paul Bakker40e46942009-01-03 21:51:57 +0000391 * POLARSSL_ERR_MPI_NEGATIVE_VALUE if B is greater than A
Paul Bakker5121ce52009-01-03 21:22:43 +0000392 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000393int mpi_sub_abs( mpi *X, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000394
395/**
396 * \brief Signed addition: X = A + B
397 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000398 * \param X Destination MPI
399 * \param A Left-hand MPI
400 * \param B Right-hand MPI
401 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000402 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000403 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000404 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000405int mpi_add_mpi( mpi *X, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000406
407/**
408 * \brief Signed substraction: X = A - B
409 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000410 * \param X Destination MPI
411 * \param A Left-hand MPI
412 * \param B Right-hand MPI
413 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000414 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000415 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000416 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000417int mpi_sub_mpi( mpi *X, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000418
419/**
420 * \brief Signed addition: X = A + b
421 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000422 * \param X Destination MPI
423 * \param A Left-hand MPI
424 * \param b The integer value to add
425 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000426 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000427 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000428 */
Paul Bakkera755ca12011-04-24 09:11:17 +0000429int mpi_add_int( mpi *X, const mpi *A, t_sint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000430
431/**
432 * \brief Signed substraction: X = A - b
433 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000434 * \param X Destination MPI
435 * \param A Left-hand MPI
436 * \param b The integer value to subtract
437 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000438 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000439 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000440 */
Paul Bakkera755ca12011-04-24 09:11:17 +0000441int mpi_sub_int( mpi *X, const mpi *A, t_sint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000442
443/**
444 * \brief Baseline multiplication: X = A * B
445 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000446 * \param X Destination MPI
447 * \param A Left-hand MPI
448 * \param B Right-hand MPI
449 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000450 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000451 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000452 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000453int mpi_mul_mpi( mpi *X, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000454
455/**
456 * \brief Baseline multiplication: X = A * b
Paul Bakkerce40a6d2009-06-23 19:46:08 +0000457 * Note: b is an unsigned integer type, thus
458 * Negative values of b are ignored.
Paul Bakker5121ce52009-01-03 21:22:43 +0000459 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000460 * \param X Destination MPI
461 * \param A Left-hand MPI
462 * \param b The integer value to multiply with
463 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000464 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000465 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000466 */
Paul Bakkera755ca12011-04-24 09:11:17 +0000467int mpi_mul_int( mpi *X, const mpi *A, t_sint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000468
469/**
470 * \brief Division by mpi: A = Q * B + R
471 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000472 * \param Q Destination MPI for the quotient
473 * \param R Destination MPI for the rest value
474 * \param A Left-hand MPI
475 * \param B Right-hand MPI
476 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000477 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000478 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000479 * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if B == 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000480 *
481 * \note Either Q or R can be NULL.
482 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000483int mpi_div_mpi( mpi *Q, mpi *R, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000484
485/**
486 * \brief Division by int: A = Q * b + R
487 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000488 * \param Q Destination MPI for the quotient
489 * \param R Destination MPI for the rest value
490 * \param A Left-hand MPI
491 * \param b Integer to divide by
492 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000493 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000494 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000495 * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if b == 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000496 *
497 * \note Either Q or R can be NULL.
498 */
Paul Bakkera755ca12011-04-24 09:11:17 +0000499int mpi_div_int( mpi *Q, mpi *R, const mpi *A, t_sint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000500
501/**
502 * \brief Modulo: R = A mod B
503 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000504 * \param R Destination MPI for the rest value
505 * \param A Left-hand MPI
506 * \param B Right-hand MPI
507 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000508 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000509 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
Paul Bakkerce40a6d2009-06-23 19:46:08 +0000510 * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if B == 0,
511 * POLARSSL_ERR_MPI_NEGATIVE_VALUE if B < 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000512 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000513int mpi_mod_mpi( mpi *R, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000514
515/**
516 * \brief Modulo: r = A mod b
517 *
Paul Bakkera755ca12011-04-24 09:11:17 +0000518 * \param r Destination t_uint
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000519 * \param A Left-hand MPI
520 * \param b Integer to divide by
521 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000522 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000523 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
Paul Bakkerce40a6d2009-06-23 19:46:08 +0000524 * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if b == 0,
525 * POLARSSL_ERR_MPI_NEGATIVE_VALUE if b < 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000526 */
Paul Bakkera755ca12011-04-24 09:11:17 +0000527int mpi_mod_int( t_uint *r, const mpi *A, t_sint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000528
529/**
530 * \brief Sliding-window exponentiation: X = A^E mod N
531 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000532 * \param X Destination MPI
533 * \param A Left-hand MPI
534 * \param E Exponent MPI
535 * \param N Modular MPI
536 * \param _RR Speed-up MPI used for recalculations
537 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000538 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000539 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000540 * POLARSSL_ERR_MPI_BAD_INPUT_DATA if N is negative or even
Paul Bakker5121ce52009-01-03 21:22:43 +0000541 *
542 * \note _RR is used to avoid re-computing R*R mod N across
543 * multiple calls, which speeds up things a bit. It can
544 * be set to NULL if the extra performance is unneeded.
545 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000546int mpi_exp_mod( mpi *X, const mpi *A, const mpi *E, const mpi *N, mpi *_RR );
Paul Bakker5121ce52009-01-03 21:22:43 +0000547
548/**
Paul Bakker287781a2011-03-26 13:18:49 +0000549 * \brief Fill an MPI X with size bytes of random
550 *
551 * \param X Destination MPI
552 * \param size Size in bytes
553 * \param f_rng RNG function
554 * \param p_rng RNG parameter
555 *
556 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000557 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker287781a2011-03-26 13:18:49 +0000558 */
Paul Bakkera3d195c2011-11-27 21:07:34 +0000559int mpi_fill_random( mpi *X, size_t size,
560 int (*f_rng)(void *, unsigned char *, size_t),
561 void *p_rng );
Paul Bakker287781a2011-03-26 13:18:49 +0000562
563/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000564 * \brief Greatest common divisor: G = gcd(A, B)
565 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000566 * \param G Destination MPI
567 * \param A Left-hand MPI
568 * \param B Right-hand MPI
569 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000570 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000571 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000572 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000573int mpi_gcd( mpi *G, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000574
575/**
576 * \brief Modular inverse: X = A^-1 mod N
577 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000578 * \param X Destination MPI
579 * \param A Left-hand MPI
580 * \param N Right-hand MPI
581 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000582 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000583 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000584 * POLARSSL_ERR_MPI_BAD_INPUT_DATA if N is negative or nil
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000585 POLARSSL_ERR_MPI_NOT_ACCEPTABLE if A has no inverse mod N
Paul Bakker5121ce52009-01-03 21:22:43 +0000586 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000587int mpi_inv_mod( mpi *X, const mpi *A, const mpi *N );
Paul Bakker5121ce52009-01-03 21:22:43 +0000588
589/**
590 * \brief Miller-Rabin primality test
591 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000592 * \param X MPI to check
593 * \param f_rng RNG function
594 * \param p_rng RNG parameter
595 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000596 * \return 0 if successful (probably prime),
Paul Bakker69e095c2011-12-10 21:55:01 +0000597 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000598 * POLARSSL_ERR_MPI_NOT_ACCEPTABLE if X is not prime
Paul Bakker5121ce52009-01-03 21:22:43 +0000599 */
Paul Bakkera3d195c2011-11-27 21:07:34 +0000600int mpi_is_prime( mpi *X,
601 int (*f_rng)(void *, unsigned char *, size_t),
602 void *p_rng );
Paul Bakker5121ce52009-01-03 21:22:43 +0000603
604/**
605 * \brief Prime number generation
606 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000607 * \param X Destination MPI
Paul Bakkerfe3256e2011-11-25 12:11:43 +0000608 * \param nbits Required size of X in bits ( 3 <= nbits <= POLARSSL_MPI_MAX_BITS )
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000609 * \param dh_flag If 1, then (X-1)/2 will be prime too
Paul Bakker5121ce52009-01-03 21:22:43 +0000610 * \param f_rng RNG function
611 * \param p_rng RNG parameter
612 *
613 * \return 0 if successful (probably prime),
Paul Bakker69e095c2011-12-10 21:55:01 +0000614 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000615 * POLARSSL_ERR_MPI_BAD_INPUT_DATA if nbits is < 3
Paul Bakker5121ce52009-01-03 21:22:43 +0000616 */
Paul Bakker23986e52011-04-24 08:57:21 +0000617int mpi_gen_prime( mpi *X, size_t nbits, int dh_flag,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000618 int (*f_rng)(void *, unsigned char *, size_t),
619 void *p_rng );
Paul Bakker5121ce52009-01-03 21:22:43 +0000620
621/**
622 * \brief Checkup routine
623 *
624 * \return 0 if successful, or 1 if the test failed
625 */
626int mpi_self_test( int verbose );
627
628#ifdef __cplusplus
629}
630#endif
631
632#endif /* bignum.h */