blob: 9afc948d0493e6224002953ff1d0e1449e5a4617 [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 Bakker5121ce52009-01-03 21:22:43 +0000107 #else
Paul Bakker1a9382e2009-07-11 16:35:32 +0000108 #if defined(POLARSSL_HAVE_LONGLONG)
Paul Bakkera755ca12011-04-24 09:11:17 +0000109 typedef unsigned long long t_udbl;
Paul Bakker1a9382e2009-07-11 16:35:32 +0000110 #endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000111 #endif
112 #endif
113#endif
114#endif
115
116/**
117 * \brief MPI structure
118 */
119typedef struct
120{
121 int s; /*!< integer sign */
Paul Bakker23986e52011-04-24 08:57:21 +0000122 size_t n; /*!< total # of limbs */
Paul Bakkera755ca12011-04-24 09:11:17 +0000123 t_uint *p; /*!< pointer to limbs */
Paul Bakker5121ce52009-01-03 21:22:43 +0000124}
125mpi;
126
127#ifdef __cplusplus
128extern "C" {
129#endif
130
131/**
Paul Bakker6c591fa2011-05-05 11:49:20 +0000132 * \brief Initialize one MPI
133 *
134 * \param X One MPI to initialize.
Paul Bakker5121ce52009-01-03 21:22:43 +0000135 */
Paul Bakker6c591fa2011-05-05 11:49:20 +0000136void mpi_init( mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000137
138/**
Paul Bakker6c591fa2011-05-05 11:49:20 +0000139 * \brief Unallocate one MPI
140 *
141 * \param X One MPI to unallocate.
Paul Bakker5121ce52009-01-03 21:22:43 +0000142 */
Paul Bakker6c591fa2011-05-05 11:49:20 +0000143void mpi_free( mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000144
145/**
146 * \brief Enlarge to the specified number of limbs
147 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000148 * \param X MPI to grow
149 * \param nblimbs The target number of limbs
150 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000151 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000152 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000153 */
Paul Bakker23986e52011-04-24 08:57:21 +0000154int mpi_grow( mpi *X, size_t nblimbs );
Paul Bakker5121ce52009-01-03 21:22:43 +0000155
156/**
157 * \brief Copy the contents of Y into X
158 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000159 * \param X Destination MPI
160 * \param Y Source MPI
161 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000162 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000163 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000164 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000165int mpi_copy( mpi *X, const mpi *Y );
Paul Bakker5121ce52009-01-03 21:22:43 +0000166
167/**
168 * \brief Swap the contents of X and Y
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000169 *
170 * \param X First MPI value
171 * \param Y Second MPI value
Paul Bakker5121ce52009-01-03 21:22:43 +0000172 */
173void mpi_swap( mpi *X, mpi *Y );
174
175/**
176 * \brief Set value from integer
177 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000178 * \param X MPI to set
179 * \param z Value to use
180 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000181 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000182 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000183 */
Paul Bakkera755ca12011-04-24 09:11:17 +0000184int mpi_lset( mpi *X, t_sint z );
Paul Bakker5121ce52009-01-03 21:22:43 +0000185
Paul Bakker2f5947e2011-05-18 15:47:11 +0000186/*
187 * \brief Get a specific bit from X
188 *
189 * \param X MPI to use
190 * \param pos Zero-based index of the bit in X
191 *
192 * \return Either a 0 or a 1
193 */
194int mpi_get_bit( mpi *X, size_t pos );
195
196/*
197 * \brief Set a bit of X to a specific value of 0 or 1
198 *
199 * \note Will grow X if necessary to set a bit to 1 in a not yet
200 * existing limb. Will not grow if bit should be set to 0
201 *
202 * \param X MPI to use
203 * \param pos Zero-based index of the bit in X
204 * \param val The value to set the bit to (0 or 1)
205 *
206 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000207 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
Paul Bakker2f5947e2011-05-18 15:47:11 +0000208 * POLARSSL_ERR_MPI_BAD_INPUT_DATA if val is not 0 or 1
209 */
210int mpi_set_bit( mpi *X, size_t pos, unsigned char val );
211
Paul Bakker5121ce52009-01-03 21:22:43 +0000212/**
213 * \brief Return the number of least significant bits
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000214 *
215 * \param X MPI to use
Paul Bakker5121ce52009-01-03 21:22:43 +0000216 */
Paul Bakker23986e52011-04-24 08:57:21 +0000217size_t mpi_lsb( const mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000218
219/**
220 * \brief Return the number of most significant bits
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000221 *
222 * \param X MPI to use
Paul Bakker5121ce52009-01-03 21:22:43 +0000223 */
Paul Bakker23986e52011-04-24 08:57:21 +0000224size_t mpi_msb( const mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000225
226/**
227 * \brief Return the total size in bytes
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000228 *
229 * \param X MPI to use
Paul Bakker5121ce52009-01-03 21:22:43 +0000230 */
Paul Bakker23986e52011-04-24 08:57:21 +0000231size_t mpi_size( const mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000232
233/**
234 * \brief Import from an ASCII string
235 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000236 * \param X Destination MPI
237 * \param radix Input numeric base
238 * \param s Null-terminated string buffer
Paul Bakker5121ce52009-01-03 21:22:43 +0000239 *
Paul Bakkercb37aa52011-11-30 16:00:20 +0000240 * \return 0 if successful, or a POLARSSL_ERR_MPI_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000241 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000242int mpi_read_string( mpi *X, int radix, const char *s );
Paul Bakker5121ce52009-01-03 21:22:43 +0000243
244/**
245 * \brief Export into an ASCII string
246 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000247 * \param X Source MPI
248 * \param radix Output numeric base
249 * \param s String buffer
250 * \param slen String buffer size
Paul Bakker5121ce52009-01-03 21:22:43 +0000251 *
Paul Bakkercb37aa52011-11-30 16:00:20 +0000252 * \return 0 if successful, or a POLARSSL_ERR_MPI_XXX error code.
Paul Bakkerff60ee62010-03-16 21:09:09 +0000253 * *slen is always updated to reflect the amount
254 * of data that has (or would have) been written.
Paul Bakker5121ce52009-01-03 21:22:43 +0000255 *
256 * \note Call this function with *slen = 0 to obtain the
257 * minimum required buffer size in *slen.
258 */
Paul Bakker23986e52011-04-24 08:57:21 +0000259int mpi_write_string( const mpi *X, int radix, char *s, size_t *slen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000260
261/**
262 * \brief Read X from an opened file
263 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000264 * \param X Destination MPI
265 * \param radix Input numeric base
266 * \param fin Input file handle
Paul Bakker5121ce52009-01-03 21:22:43 +0000267 *
Paul Bakkercb37aa52011-11-30 16:00:20 +0000268 * \return 0 if successful, POLARSSL_ERR_MPI_BUFFER_TOO_SMALL if
269 * the file read buffer is too small or a
270 * POLARSSL_ERR_MPI_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000271 */
272int mpi_read_file( mpi *X, int radix, FILE *fin );
273
274/**
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000275 * \brief Write X into an opened file, or stdout if fout is NULL
Paul Bakker5121ce52009-01-03 21:22:43 +0000276 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000277 * \param p Prefix, can be NULL
278 * \param X Source MPI
279 * \param radix Output numeric base
280 * \param fout Output file handle (can be NULL)
Paul Bakker5121ce52009-01-03 21:22:43 +0000281 *
Paul Bakkercb37aa52011-11-30 16:00:20 +0000282 * \return 0 if successful, or a POLARSSL_ERR_MPI_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000283 *
284 * \note Set fout == NULL to print X on the console.
285 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000286int mpi_write_file( const char *p, const mpi *X, int radix, FILE *fout );
Paul Bakker5121ce52009-01-03 21:22:43 +0000287
288/**
289 * \brief Import X from unsigned binary data, big endian
290 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000291 * \param X Destination MPI
292 * \param buf Input buffer
293 * \param buflen Input buffer size
Paul Bakker5121ce52009-01-03 21:22:43 +0000294 *
295 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000296 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000297 */
Paul Bakker23986e52011-04-24 08:57:21 +0000298int mpi_read_binary( mpi *X, const unsigned char *buf, size_t buflen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000299
300/**
301 * \brief Export X into unsigned binary data, big endian
302 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000303 * \param X Source MPI
304 * \param buf Output buffer
305 * \param buflen Output buffer size
Paul Bakker5121ce52009-01-03 21:22:43 +0000306 *
307 * \return 0 if successful,
Paul Bakker40e46942009-01-03 21:51:57 +0000308 * POLARSSL_ERR_MPI_BUFFER_TOO_SMALL if buf isn't large enough
Paul Bakker5121ce52009-01-03 21:22:43 +0000309 */
Paul Bakker23986e52011-04-24 08:57:21 +0000310int mpi_write_binary( const mpi *X, unsigned char *buf, size_t buflen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000311
312/**
313 * \brief Left-shift: X <<= count
314 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000315 * \param X MPI to shift
316 * \param count Amount to shift
317 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000318 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000319 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000320 */
Paul Bakker23986e52011-04-24 08:57:21 +0000321int mpi_shift_l( mpi *X, size_t count );
Paul Bakker5121ce52009-01-03 21:22:43 +0000322
323/**
324 * \brief Right-shift: X >>= count
325 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000326 * \param X MPI to shift
327 * \param count Amount to shift
328 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000329 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000330 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000331 */
Paul Bakker23986e52011-04-24 08:57:21 +0000332int mpi_shift_r( mpi *X, size_t count );
Paul Bakker5121ce52009-01-03 21:22:43 +0000333
334/**
335 * \brief Compare unsigned values
336 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000337 * \param X Left-hand MPI
338 * \param Y Right-hand MPI
339 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000340 * \return 1 if |X| is greater than |Y|,
341 * -1 if |X| is lesser than |Y| or
342 * 0 if |X| is equal to |Y|
343 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000344int mpi_cmp_abs( const mpi *X, const mpi *Y );
Paul Bakker5121ce52009-01-03 21:22:43 +0000345
346/**
347 * \brief Compare signed values
348 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000349 * \param X Left-hand MPI
350 * \param Y Right-hand MPI
351 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000352 * \return 1 if X is greater than Y,
353 * -1 if X is lesser than Y or
354 * 0 if X is equal to Y
355 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000356int mpi_cmp_mpi( const mpi *X, const mpi *Y );
Paul Bakker5121ce52009-01-03 21:22:43 +0000357
358/**
359 * \brief Compare signed values
360 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000361 * \param X Left-hand MPI
362 * \param z The integer value to compare to
363 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000364 * \return 1 if X is greater than z,
365 * -1 if X is lesser than z or
366 * 0 if X is equal to z
367 */
Paul Bakkera755ca12011-04-24 09:11:17 +0000368int mpi_cmp_int( const mpi *X, t_sint z );
Paul Bakker5121ce52009-01-03 21:22:43 +0000369
370/**
371 * \brief Unsigned addition: X = |A| + |B|
372 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000373 * \param X Destination MPI
374 * \param A Left-hand MPI
375 * \param B Right-hand MPI
376 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000377 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000378 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000379 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000380int mpi_add_abs( mpi *X, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000381
382/**
383 * \brief Unsigned substraction: X = |A| - |B|
384 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000385 * \param X Destination MPI
386 * \param A Left-hand MPI
387 * \param B Right-hand MPI
388 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000389 * \return 0 if successful,
Paul Bakker40e46942009-01-03 21:51:57 +0000390 * POLARSSL_ERR_MPI_NEGATIVE_VALUE if B is greater than A
Paul Bakker5121ce52009-01-03 21:22:43 +0000391 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000392int mpi_sub_abs( mpi *X, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000393
394/**
395 * \brief Signed addition: X = A + B
396 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000397 * \param X Destination MPI
398 * \param A Left-hand MPI
399 * \param B Right-hand MPI
400 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000401 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000402 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000403 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000404int mpi_add_mpi( mpi *X, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000405
406/**
407 * \brief Signed substraction: X = A - B
408 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000409 * \param X Destination MPI
410 * \param A Left-hand MPI
411 * \param B Right-hand MPI
412 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000413 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000414 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000415 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000416int mpi_sub_mpi( mpi *X, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000417
418/**
419 * \brief Signed addition: X = A + b
420 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000421 * \param X Destination MPI
422 * \param A Left-hand MPI
423 * \param b The integer value to add
424 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000425 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000426 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000427 */
Paul Bakkera755ca12011-04-24 09:11:17 +0000428int mpi_add_int( mpi *X, const mpi *A, t_sint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000429
430/**
431 * \brief Signed substraction: X = A - b
432 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000433 * \param X Destination MPI
434 * \param A Left-hand MPI
435 * \param b The integer value to subtract
436 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000437 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000438 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000439 */
Paul Bakkera755ca12011-04-24 09:11:17 +0000440int mpi_sub_int( mpi *X, const mpi *A, t_sint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000441
442/**
443 * \brief Baseline multiplication: X = A * B
444 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000445 * \param X Destination MPI
446 * \param A Left-hand MPI
447 * \param B Right-hand MPI
448 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000449 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000450 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000451 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000452int mpi_mul_mpi( mpi *X, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000453
454/**
455 * \brief Baseline multiplication: X = A * b
Paul Bakkerce40a6d2009-06-23 19:46:08 +0000456 * Note: b is an unsigned integer type, thus
457 * Negative values of b are ignored.
Paul Bakker5121ce52009-01-03 21:22:43 +0000458 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000459 * \param X Destination MPI
460 * \param A Left-hand MPI
461 * \param b The integer value to multiply with
462 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000463 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000464 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000465 */
Paul Bakkera755ca12011-04-24 09:11:17 +0000466int mpi_mul_int( mpi *X, const mpi *A, t_sint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000467
468/**
469 * \brief Division by mpi: A = Q * B + R
470 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000471 * \param Q Destination MPI for the quotient
472 * \param R Destination MPI for the rest value
473 * \param A Left-hand MPI
474 * \param B Right-hand MPI
475 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000476 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000477 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000478 * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if B == 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000479 *
480 * \note Either Q or R can be NULL.
481 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000482int mpi_div_mpi( mpi *Q, mpi *R, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000483
484/**
485 * \brief Division by int: A = Q * b + R
486 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000487 * \param Q Destination MPI for the quotient
488 * \param R Destination MPI for the rest value
489 * \param A Left-hand MPI
490 * \param b Integer to divide by
491 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000492 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000493 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000494 * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if b == 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000495 *
496 * \note Either Q or R can be NULL.
497 */
Paul Bakkera755ca12011-04-24 09:11:17 +0000498int mpi_div_int( mpi *Q, mpi *R, const mpi *A, t_sint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000499
500/**
501 * \brief Modulo: R = A mod B
502 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000503 * \param R Destination MPI for the rest value
504 * \param A Left-hand MPI
505 * \param B Right-hand MPI
506 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000507 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000508 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
Paul Bakkerce40a6d2009-06-23 19:46:08 +0000509 * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if B == 0,
510 * POLARSSL_ERR_MPI_NEGATIVE_VALUE if B < 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000511 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000512int mpi_mod_mpi( mpi *R, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000513
514/**
515 * \brief Modulo: r = A mod b
516 *
Paul Bakkera755ca12011-04-24 09:11:17 +0000517 * \param r Destination t_uint
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000518 * \param A Left-hand MPI
519 * \param b Integer to divide by
520 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000521 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000522 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
Paul Bakkerce40a6d2009-06-23 19:46:08 +0000523 * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if b == 0,
524 * POLARSSL_ERR_MPI_NEGATIVE_VALUE if b < 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000525 */
Paul Bakkera755ca12011-04-24 09:11:17 +0000526int mpi_mod_int( t_uint *r, const mpi *A, t_sint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000527
528/**
529 * \brief Sliding-window exponentiation: X = A^E mod N
530 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000531 * \param X Destination MPI
532 * \param A Left-hand MPI
533 * \param E Exponent MPI
534 * \param N Modular MPI
535 * \param _RR Speed-up MPI used for recalculations
536 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000537 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000538 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000539 * POLARSSL_ERR_MPI_BAD_INPUT_DATA if N is negative or even
Paul Bakker5121ce52009-01-03 21:22:43 +0000540 *
541 * \note _RR is used to avoid re-computing R*R mod N across
542 * multiple calls, which speeds up things a bit. It can
543 * be set to NULL if the extra performance is unneeded.
544 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000545int mpi_exp_mod( mpi *X, const mpi *A, const mpi *E, const mpi *N, mpi *_RR );
Paul Bakker5121ce52009-01-03 21:22:43 +0000546
547/**
Paul Bakker287781a2011-03-26 13:18:49 +0000548 * \brief Fill an MPI X with size bytes of random
549 *
550 * \param X Destination MPI
551 * \param size Size in bytes
552 * \param f_rng RNG function
553 * \param p_rng RNG parameter
554 *
555 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000556 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker287781a2011-03-26 13:18:49 +0000557 */
Paul Bakkera3d195c2011-11-27 21:07:34 +0000558int mpi_fill_random( mpi *X, size_t size,
559 int (*f_rng)(void *, unsigned char *, size_t),
560 void *p_rng );
Paul Bakker287781a2011-03-26 13:18:49 +0000561
562/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000563 * \brief Greatest common divisor: G = gcd(A, B)
564 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000565 * \param G Destination MPI
566 * \param A Left-hand MPI
567 * \param B Right-hand MPI
568 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000569 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000570 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000571 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000572int mpi_gcd( mpi *G, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000573
574/**
575 * \brief Modular inverse: X = A^-1 mod N
576 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000577 * \param X Destination MPI
578 * \param A Left-hand MPI
579 * \param N Right-hand MPI
580 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000581 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000582 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000583 * POLARSSL_ERR_MPI_BAD_INPUT_DATA if N is negative or nil
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000584 POLARSSL_ERR_MPI_NOT_ACCEPTABLE if A has no inverse mod N
Paul Bakker5121ce52009-01-03 21:22:43 +0000585 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000586int mpi_inv_mod( mpi *X, const mpi *A, const mpi *N );
Paul Bakker5121ce52009-01-03 21:22:43 +0000587
588/**
589 * \brief Miller-Rabin primality test
590 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000591 * \param X MPI to check
592 * \param f_rng RNG function
593 * \param p_rng RNG parameter
594 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000595 * \return 0 if successful (probably prime),
Paul Bakker69e095c2011-12-10 21:55:01 +0000596 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000597 * POLARSSL_ERR_MPI_NOT_ACCEPTABLE if X is not prime
Paul Bakker5121ce52009-01-03 21:22:43 +0000598 */
Paul Bakkera3d195c2011-11-27 21:07:34 +0000599int mpi_is_prime( mpi *X,
600 int (*f_rng)(void *, unsigned char *, size_t),
601 void *p_rng );
Paul Bakker5121ce52009-01-03 21:22:43 +0000602
603/**
604 * \brief Prime number generation
605 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000606 * \param X Destination MPI
Paul Bakkerfe3256e2011-11-25 12:11:43 +0000607 * \param nbits Required size of X in bits ( 3 <= nbits <= POLARSSL_MPI_MAX_BITS )
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000608 * \param dh_flag If 1, then (X-1)/2 will be prime too
Paul Bakker5121ce52009-01-03 21:22:43 +0000609 * \param f_rng RNG function
610 * \param p_rng RNG parameter
611 *
612 * \return 0 if successful (probably prime),
Paul Bakker69e095c2011-12-10 21:55:01 +0000613 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000614 * POLARSSL_ERR_MPI_BAD_INPUT_DATA if nbits is < 3
Paul Bakker5121ce52009-01-03 21:22:43 +0000615 */
Paul Bakker23986e52011-04-24 08:57:21 +0000616int mpi_gen_prime( mpi *X, size_t nbits, int dh_flag,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000617 int (*f_rng)(void *, unsigned char *, size_t),
618 void *p_rng );
Paul Bakker5121ce52009-01-03 21:22:43 +0000619
620/**
621 * \brief Checkup routine
622 *
623 * \return 0 if successful, or 1 if the test failed
624 */
625int mpi_self_test( int verbose );
626
627#ifdef __cplusplus
628}
629#endif
630
631#endif /* bignum.h */