blob: b4496b84752e65805e29297aabecf153f98d6564 [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 Bakker9d781402011-05-09 16:17:09 +000033#define POLARSSL_ERR_MPI_FILE_IO_ERROR -0x0002 /**< An error occurred while reading from or writing to a file. */
34#define POLARSSL_ERR_MPI_BAD_INPUT_DATA -0x0004 /**< Bad input parameters to function. */
35#define POLARSSL_ERR_MPI_INVALID_CHARACTER -0x0006 /**< There is an invalid character in the digit string. */
Paul Bakkercb37aa52011-11-30 16:00:20 +000036#define POLARSSL_ERR_MPI_BUFFER_TOO_SMALL -0x0008 /**< The buffer is too small to write too. */
Paul Bakker9d781402011-05-09 16:17:09 +000037#define POLARSSL_ERR_MPI_NEGATIVE_VALUE -0x000A /**< The input arguments are negative or result in illegal output. */
38#define POLARSSL_ERR_MPI_DIVISION_BY_ZERO -0x000C /**< The input argument for division is zero, which is not allowed. */
39#define POLARSSL_ERR_MPI_NOT_ACCEPTABLE -0x000E /**< The input arguments are not acceptable. */
Paul Bakker5121ce52009-01-03 21:22:43 +000040
41#define MPI_CHK(f) if( ( ret = f ) != 0 ) goto cleanup
42
43/*
Paul Bakkerf9688572011-05-05 10:00:45 +000044 * Maximum size MPIs are allowed to grow to in number of limbs.
45 */
46#define POLARSSL_MPI_MAX_LIMBS 10000
47
48/*
Paul Bakkerb6d5f082011-11-25 11:52:11 +000049 * Maximum window size used for modular exponentiation. Default: 6
50 * Minimum value: 1. Maximum value: 6.
51 *
52 * Result is an array of ( 2 << POLARSSL_MPI_WINDOW_SIZE ) MPIs used
53 * for the sliding window calculation. (So 64 by default)
54 *
55 * Reduction in size, reduces speed.
56 */
57#define POLARSSL_MPI_WINDOW_SIZE 6 /**< Maximum windows size used. */
58
59/*
Paul Bakkerfe3256e2011-11-25 12:11:43 +000060 * Maximum size of MPIs allowed in bits and bytes for user-MPIs.
61 * ( Default: 512 bytes => 4096 bits )
62 *
63 * Note: Calculations can results temporarily in larger MPIs. So the number
64 * of limbs required (POLARSSL_MPI_MAX_LIMBS) is higher.
65 */
66#define POLARSSL_MPI_MAX_SIZE 512 /**< Maximum number of bytes for usable MPIs. */
67#define POLARSSL_MPI_MAX_BITS ( 8 * POLARSSL_MPI_MAX_SIZE ) /**< Maximum number of bits for usable MPIs. */
68
69/*
Paul Bakkercb37aa52011-11-30 16:00:20 +000070 * When reading from files with mpi_read_file() the buffer should have space
71 * for a (short) label, the MPI (in the provided radix), the newline
72 * characters and the '\0'.
73 *
74 * By default we assume at least a 10 char label, a minimum radix of 10
75 * (decimal) and a maximum of 4096 bit numbers (1234 decimal chars).
76 */
77#define POLARSSL_MPI_READ_BUFFER_SIZE 1250
78
79/*
Paul Bakker5121ce52009-01-03 21:22:43 +000080 * Define the base integer type, architecture-wise
81 */
Paul Bakker40e46942009-01-03 21:51:57 +000082#if defined(POLARSSL_HAVE_INT8)
Paul Bakkera755ca12011-04-24 09:11:17 +000083typedef signed char t_sint;
84typedef unsigned char t_uint;
85typedef unsigned short t_udbl;
Paul Bakker5121ce52009-01-03 21:22:43 +000086#else
Paul Bakker40e46942009-01-03 21:51:57 +000087#if defined(POLARSSL_HAVE_INT16)
Paul Bakkera755ca12011-04-24 09:11:17 +000088typedef signed short t_sint;
89typedef unsigned short t_uint;
90typedef unsigned long t_udbl;
Paul Bakker5121ce52009-01-03 21:22:43 +000091#else
Paul Bakkera755ca12011-04-24 09:11:17 +000092 typedef signed long t_sint;
93 typedef unsigned long t_uint;
Paul Bakker5121ce52009-01-03 21:22:43 +000094 #if defined(_MSC_VER) && defined(_M_IX86)
Paul Bakkera755ca12011-04-24 09:11:17 +000095 typedef unsigned __int64 t_udbl;
Paul Bakker5121ce52009-01-03 21:22:43 +000096 #else
97 #if defined(__amd64__) || defined(__x86_64__) || \
98 defined(__ppc64__) || defined(__powerpc64__) || \
Paul Bakker44637402011-11-26 09:23:07 +000099 defined(__ia64__) || defined(__alpha__) || \
100 (defined(__sparc__) && defined(__arch64__)) || \
101 defined(__s390x__)
Paul Bakkera755ca12011-04-24 09:11:17 +0000102 typedef unsigned int t_udbl __attribute__((mode(TI)));
Paul Bakker5121ce52009-01-03 21:22:43 +0000103 #else
Paul Bakker1a9382e2009-07-11 16:35:32 +0000104 #if defined(POLARSSL_HAVE_LONGLONG)
Paul Bakkera755ca12011-04-24 09:11:17 +0000105 typedef unsigned long long t_udbl;
Paul Bakker1a9382e2009-07-11 16:35:32 +0000106 #endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000107 #endif
108 #endif
109#endif
110#endif
111
112/**
113 * \brief MPI structure
114 */
115typedef struct
116{
117 int s; /*!< integer sign */
Paul Bakker23986e52011-04-24 08:57:21 +0000118 size_t n; /*!< total # of limbs */
Paul Bakkera755ca12011-04-24 09:11:17 +0000119 t_uint *p; /*!< pointer to limbs */
Paul Bakker5121ce52009-01-03 21:22:43 +0000120}
121mpi;
122
123#ifdef __cplusplus
124extern "C" {
125#endif
126
127/**
Paul Bakker6c591fa2011-05-05 11:49:20 +0000128 * \brief Initialize one MPI
129 *
130 * \param X One MPI to initialize.
Paul Bakker5121ce52009-01-03 21:22:43 +0000131 */
Paul Bakker6c591fa2011-05-05 11:49:20 +0000132void mpi_init( mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000133
134/**
Paul Bakker6c591fa2011-05-05 11:49:20 +0000135 * \brief Unallocate one MPI
136 *
137 * \param X One MPI to unallocate.
Paul Bakker5121ce52009-01-03 21:22:43 +0000138 */
Paul Bakker6c591fa2011-05-05 11:49:20 +0000139void mpi_free( mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000140
141/**
142 * \brief Enlarge to the specified number of limbs
143 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000144 * \param X MPI to grow
145 * \param nblimbs The target number of limbs
146 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000147 * \return 0 if successful,
148 * 1 if memory allocation failed
149 */
Paul Bakker23986e52011-04-24 08:57:21 +0000150int mpi_grow( mpi *X, size_t nblimbs );
Paul Bakker5121ce52009-01-03 21:22:43 +0000151
152/**
153 * \brief Copy the contents of Y into X
154 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000155 * \param X Destination MPI
156 * \param Y Source MPI
157 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000158 * \return 0 if successful,
159 * 1 if memory allocation failed
160 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000161int mpi_copy( mpi *X, const mpi *Y );
Paul Bakker5121ce52009-01-03 21:22:43 +0000162
163/**
164 * \brief Swap the contents of X and Y
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000165 *
166 * \param X First MPI value
167 * \param Y Second MPI value
Paul Bakker5121ce52009-01-03 21:22:43 +0000168 */
169void mpi_swap( mpi *X, mpi *Y );
170
171/**
172 * \brief Set value from integer
173 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000174 * \param X MPI to set
175 * \param z Value to use
176 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000177 * \return 0 if successful,
178 * 1 if memory allocation failed
179 */
Paul Bakkera755ca12011-04-24 09:11:17 +0000180int mpi_lset( mpi *X, t_sint z );
Paul Bakker5121ce52009-01-03 21:22:43 +0000181
Paul Bakker2f5947e2011-05-18 15:47:11 +0000182/*
183 * \brief Get a specific bit from X
184 *
185 * \param X MPI to use
186 * \param pos Zero-based index of the bit in X
187 *
188 * \return Either a 0 or a 1
189 */
190int mpi_get_bit( mpi *X, size_t pos );
191
192/*
193 * \brief Set a bit of X to a specific value of 0 or 1
194 *
195 * \note Will grow X if necessary to set a bit to 1 in a not yet
196 * existing limb. Will not grow if bit should be set to 0
197 *
198 * \param X MPI to use
199 * \param pos Zero-based index of the bit in X
200 * \param val The value to set the bit to (0 or 1)
201 *
202 * \return 0 if successful,
203 * 1 if memory allocation failed,
204 * POLARSSL_ERR_MPI_BAD_INPUT_DATA if val is not 0 or 1
205 */
206int mpi_set_bit( mpi *X, size_t pos, unsigned char val );
207
Paul Bakker5121ce52009-01-03 21:22:43 +0000208/**
209 * \brief Return the number of least significant bits
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000210 *
211 * \param X MPI to use
Paul Bakker5121ce52009-01-03 21:22:43 +0000212 */
Paul Bakker23986e52011-04-24 08:57:21 +0000213size_t mpi_lsb( const mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000214
215/**
216 * \brief Return the number of most significant bits
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000217 *
218 * \param X MPI to use
Paul Bakker5121ce52009-01-03 21:22:43 +0000219 */
Paul Bakker23986e52011-04-24 08:57:21 +0000220size_t mpi_msb( const mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000221
222/**
223 * \brief Return the total size in bytes
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000224 *
225 * \param X MPI to use
Paul Bakker5121ce52009-01-03 21:22:43 +0000226 */
Paul Bakker23986e52011-04-24 08:57:21 +0000227size_t mpi_size( const mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000228
229/**
230 * \brief Import from an ASCII string
231 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000232 * \param X Destination MPI
233 * \param radix Input numeric base
234 * \param s Null-terminated string buffer
Paul Bakker5121ce52009-01-03 21:22:43 +0000235 *
Paul Bakkercb37aa52011-11-30 16:00:20 +0000236 * \return 0 if successful, or a POLARSSL_ERR_MPI_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000237 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000238int mpi_read_string( mpi *X, int radix, const char *s );
Paul Bakker5121ce52009-01-03 21:22:43 +0000239
240/**
241 * \brief Export into an ASCII string
242 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000243 * \param X Source MPI
244 * \param radix Output numeric base
245 * \param s String buffer
246 * \param slen String buffer size
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 Bakkerff60ee62010-03-16 21:09:09 +0000249 * *slen is always updated to reflect the amount
250 * of data that has (or would have) been written.
Paul Bakker5121ce52009-01-03 21:22:43 +0000251 *
252 * \note Call this function with *slen = 0 to obtain the
253 * minimum required buffer size in *slen.
254 */
Paul Bakker23986e52011-04-24 08:57:21 +0000255int mpi_write_string( const mpi *X, int radix, char *s, size_t *slen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000256
257/**
258 * \brief Read X from an opened file
259 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000260 * \param X Destination MPI
261 * \param radix Input numeric base
262 * \param fin Input file handle
Paul Bakker5121ce52009-01-03 21:22:43 +0000263 *
Paul Bakkercb37aa52011-11-30 16:00:20 +0000264 * \return 0 if successful, POLARSSL_ERR_MPI_BUFFER_TOO_SMALL if
265 * the file read buffer is too small or a
266 * POLARSSL_ERR_MPI_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000267 */
268int mpi_read_file( mpi *X, int radix, FILE *fin );
269
270/**
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000271 * \brief Write X into an opened file, or stdout if fout is NULL
Paul Bakker5121ce52009-01-03 21:22:43 +0000272 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000273 * \param p Prefix, can be NULL
274 * \param X Source MPI
275 * \param radix Output numeric base
276 * \param fout Output file handle (can be NULL)
Paul Bakker5121ce52009-01-03 21:22:43 +0000277 *
Paul Bakkercb37aa52011-11-30 16:00:20 +0000278 * \return 0 if successful, or a POLARSSL_ERR_MPI_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000279 *
280 * \note Set fout == NULL to print X on the console.
281 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000282int mpi_write_file( const char *p, const mpi *X, int radix, FILE *fout );
Paul Bakker5121ce52009-01-03 21:22:43 +0000283
284/**
285 * \brief Import X from unsigned binary data, big endian
286 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000287 * \param X Destination MPI
288 * \param buf Input buffer
289 * \param buflen Input buffer size
Paul Bakker5121ce52009-01-03 21:22:43 +0000290 *
291 * \return 0 if successful,
292 * 1 if memory allocation failed
293 */
Paul Bakker23986e52011-04-24 08:57:21 +0000294int mpi_read_binary( mpi *X, const unsigned char *buf, size_t buflen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000295
296/**
297 * \brief Export X into unsigned binary data, big endian
298 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000299 * \param X Source MPI
300 * \param buf Output buffer
301 * \param buflen Output buffer size
Paul Bakker5121ce52009-01-03 21:22:43 +0000302 *
303 * \return 0 if successful,
Paul Bakker40e46942009-01-03 21:51:57 +0000304 * POLARSSL_ERR_MPI_BUFFER_TOO_SMALL if buf isn't large enough
Paul Bakker5121ce52009-01-03 21:22:43 +0000305 */
Paul Bakker23986e52011-04-24 08:57:21 +0000306int mpi_write_binary( const mpi *X, unsigned char *buf, size_t buflen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000307
308/**
309 * \brief Left-shift: X <<= count
310 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000311 * \param X MPI to shift
312 * \param count Amount to shift
313 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000314 * \return 0 if successful,
315 * 1 if memory allocation failed
316 */
Paul Bakker23986e52011-04-24 08:57:21 +0000317int mpi_shift_l( mpi *X, size_t count );
Paul Bakker5121ce52009-01-03 21:22:43 +0000318
319/**
320 * \brief Right-shift: X >>= count
321 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000322 * \param X MPI to shift
323 * \param count Amount to shift
324 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000325 * \return 0 if successful,
326 * 1 if memory allocation failed
327 */
Paul Bakker23986e52011-04-24 08:57:21 +0000328int mpi_shift_r( mpi *X, size_t count );
Paul Bakker5121ce52009-01-03 21:22:43 +0000329
330/**
331 * \brief Compare unsigned values
332 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000333 * \param X Left-hand MPI
334 * \param Y Right-hand MPI
335 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000336 * \return 1 if |X| is greater than |Y|,
337 * -1 if |X| is lesser than |Y| or
338 * 0 if |X| is equal to |Y|
339 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000340int mpi_cmp_abs( const mpi *X, const mpi *Y );
Paul Bakker5121ce52009-01-03 21:22:43 +0000341
342/**
343 * \brief Compare signed 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_mpi( 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 z The integer value to compare to
359 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000360 * \return 1 if X is greater than z,
361 * -1 if X is lesser than z or
362 * 0 if X is equal to z
363 */
Paul Bakkera755ca12011-04-24 09:11:17 +0000364int mpi_cmp_int( const mpi *X, t_sint z );
Paul Bakker5121ce52009-01-03 21:22:43 +0000365
366/**
367 * \brief Unsigned addition: X = |A| + |B|
368 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000369 * \param X Destination MPI
370 * \param A Left-hand MPI
371 * \param B Right-hand MPI
372 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000373 * \return 0 if successful,
374 * 1 if memory allocation failed
375 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000376int mpi_add_abs( mpi *X, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000377
378/**
379 * \brief Unsigned substraction: 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 Bakker40e46942009-01-03 21:51:57 +0000386 * POLARSSL_ERR_MPI_NEGATIVE_VALUE if B is greater than A
Paul Bakker5121ce52009-01-03 21:22:43 +0000387 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000388int mpi_sub_abs( mpi *X, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000389
390/**
391 * \brief Signed addition: 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,
398 * 1 if memory allocation failed
399 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000400int mpi_add_mpi( mpi *X, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000401
402/**
403 * \brief Signed substraction: 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,
410 * 1 if memory allocation failed
411 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000412int mpi_sub_mpi( mpi *X, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000413
414/**
415 * \brief Signed addition: 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 The integer value to add
420 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000421 * \return 0 if successful,
422 * 1 if memory allocation failed
423 */
Paul Bakkera755ca12011-04-24 09:11:17 +0000424int mpi_add_int( mpi *X, const mpi *A, t_sint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000425
426/**
427 * \brief Signed substraction: 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 subtract
432 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000433 * \return 0 if successful,
434 * 1 if memory allocation failed
435 */
Paul Bakkera755ca12011-04-24 09:11:17 +0000436int mpi_sub_int( mpi *X, const mpi *A, t_sint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000437
438/**
439 * \brief Baseline multiplication: 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 Right-hand MPI
444 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000445 * \return 0 if successful,
446 * 1 if memory allocation failed
447 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000448int mpi_mul_mpi( mpi *X, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000449
450/**
451 * \brief Baseline multiplication: X = A * b
Paul Bakkerce40a6d2009-06-23 19:46:08 +0000452 * Note: b is an unsigned integer type, thus
453 * Negative values of b are ignored.
Paul Bakker5121ce52009-01-03 21:22:43 +0000454 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000455 * \param X Destination MPI
456 * \param A Left-hand MPI
457 * \param b The integer value to multiply with
458 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000459 * \return 0 if successful,
460 * 1 if memory allocation failed
461 */
Paul Bakkera755ca12011-04-24 09:11:17 +0000462int mpi_mul_int( mpi *X, const mpi *A, t_sint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000463
464/**
465 * \brief Division by mpi: A = Q * B + R
466 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000467 * \param Q Destination MPI for the quotient
468 * \param R Destination MPI for the rest value
469 * \param A Left-hand MPI
470 * \param B Right-hand MPI
471 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000472 * \return 0 if successful,
473 * 1 if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000474 * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if B == 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000475 *
476 * \note Either Q or R can be NULL.
477 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000478int mpi_div_mpi( mpi *Q, mpi *R, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000479
480/**
481 * \brief Division by int: A = Q * b + R
482 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000483 * \param Q Destination MPI for the quotient
484 * \param R Destination MPI for the rest value
485 * \param A Left-hand MPI
486 * \param b Integer to divide by
487 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000488 * \return 0 if successful,
489 * 1 if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000490 * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if b == 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000491 *
492 * \note Either Q or R can be NULL.
493 */
Paul Bakkera755ca12011-04-24 09:11:17 +0000494int mpi_div_int( mpi *Q, mpi *R, const mpi *A, t_sint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000495
496/**
497 * \brief Modulo: R = A mod B
498 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000499 * \param R Destination MPI for the rest value
500 * \param A Left-hand MPI
501 * \param B Right-hand MPI
502 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000503 * \return 0 if successful,
504 * 1 if memory allocation failed,
Paul Bakkerce40a6d2009-06-23 19:46:08 +0000505 * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if B == 0,
506 * POLARSSL_ERR_MPI_NEGATIVE_VALUE if B < 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000507 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000508int mpi_mod_mpi( mpi *R, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000509
510/**
511 * \brief Modulo: r = A mod b
512 *
Paul Bakkera755ca12011-04-24 09:11:17 +0000513 * \param r Destination t_uint
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000514 * \param A Left-hand MPI
515 * \param b Integer to divide by
516 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000517 * \return 0 if successful,
518 * 1 if memory allocation failed,
Paul Bakkerce40a6d2009-06-23 19:46:08 +0000519 * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if b == 0,
520 * POLARSSL_ERR_MPI_NEGATIVE_VALUE if b < 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000521 */
Paul Bakkera755ca12011-04-24 09:11:17 +0000522int mpi_mod_int( t_uint *r, const mpi *A, t_sint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000523
524/**
525 * \brief Sliding-window exponentiation: X = A^E mod N
526 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000527 * \param X Destination MPI
528 * \param A Left-hand MPI
529 * \param E Exponent MPI
530 * \param N Modular MPI
531 * \param _RR Speed-up MPI used for recalculations
532 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000533 * \return 0 if successful,
534 * 1 if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000535 * POLARSSL_ERR_MPI_BAD_INPUT_DATA if N is negative or even
Paul Bakker5121ce52009-01-03 21:22:43 +0000536 *
537 * \note _RR is used to avoid re-computing R*R mod N across
538 * multiple calls, which speeds up things a bit. It can
539 * be set to NULL if the extra performance is unneeded.
540 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000541int mpi_exp_mod( mpi *X, const mpi *A, const mpi *E, const mpi *N, mpi *_RR );
Paul Bakker5121ce52009-01-03 21:22:43 +0000542
543/**
Paul Bakker287781a2011-03-26 13:18:49 +0000544 * \brief Fill an MPI X with size bytes of random
545 *
546 * \param X Destination MPI
547 * \param size Size in bytes
548 * \param f_rng RNG function
549 * \param p_rng RNG parameter
550 *
551 * \return 0 if successful,
552 * 1 if memory allocation failed
553 */
Paul Bakkera3d195c2011-11-27 21:07:34 +0000554int mpi_fill_random( mpi *X, size_t size,
555 int (*f_rng)(void *, unsigned char *, size_t),
556 void *p_rng );
Paul Bakker287781a2011-03-26 13:18:49 +0000557
558/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000559 * \brief Greatest common divisor: G = gcd(A, B)
560 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000561 * \param G Destination MPI
562 * \param A Left-hand MPI
563 * \param B Right-hand MPI
564 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000565 * \return 0 if successful,
566 * 1 if memory allocation failed
567 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000568int mpi_gcd( mpi *G, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000569
570/**
571 * \brief Modular inverse: X = A^-1 mod N
572 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000573 * \param X Destination MPI
574 * \param A Left-hand MPI
575 * \param N Right-hand MPI
576 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000577 * \return 0 if successful,
578 * 1 if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000579 * POLARSSL_ERR_MPI_BAD_INPUT_DATA if N is negative or nil
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000580 POLARSSL_ERR_MPI_NOT_ACCEPTABLE if A has no inverse mod N
Paul Bakker5121ce52009-01-03 21:22:43 +0000581 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000582int mpi_inv_mod( mpi *X, const mpi *A, const mpi *N );
Paul Bakker5121ce52009-01-03 21:22:43 +0000583
584/**
585 * \brief Miller-Rabin primality test
586 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000587 * \param X MPI to check
588 * \param f_rng RNG function
589 * \param p_rng RNG parameter
590 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000591 * \return 0 if successful (probably prime),
592 * 1 if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000593 * POLARSSL_ERR_MPI_NOT_ACCEPTABLE if X is not prime
Paul Bakker5121ce52009-01-03 21:22:43 +0000594 */
Paul Bakkera3d195c2011-11-27 21:07:34 +0000595int mpi_is_prime( mpi *X,
596 int (*f_rng)(void *, unsigned char *, size_t),
597 void *p_rng );
Paul Bakker5121ce52009-01-03 21:22:43 +0000598
599/**
600 * \brief Prime number generation
601 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000602 * \param X Destination MPI
Paul Bakkerfe3256e2011-11-25 12:11:43 +0000603 * \param nbits Required size of X in bits ( 3 <= nbits <= POLARSSL_MPI_MAX_BITS )
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000604 * \param dh_flag If 1, then (X-1)/2 will be prime too
Paul Bakker5121ce52009-01-03 21:22:43 +0000605 * \param f_rng RNG function
606 * \param p_rng RNG parameter
607 *
608 * \return 0 if successful (probably prime),
609 * 1 if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000610 * POLARSSL_ERR_MPI_BAD_INPUT_DATA if nbits is < 3
Paul Bakker5121ce52009-01-03 21:22:43 +0000611 */
Paul Bakker23986e52011-04-24 08:57:21 +0000612int mpi_gen_prime( mpi *X, size_t nbits, int dh_flag,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000613 int (*f_rng)(void *, unsigned char *, size_t),
614 void *p_rng );
Paul Bakker5121ce52009-01-03 21:22:43 +0000615
616/**
617 * \brief Checkup routine
618 *
619 * \return 0 if successful, or 1 if the test failed
620 */
621int mpi_self_test( int verbose );
622
623#ifdef __cplusplus
624}
625#endif
626
627#endif /* bignum.h */