blob: 1e46d126b1656fb9678d86ccc70ce4771b02d612 [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. */
36#define POLARSSL_ERR_MPI_BUFFER_TOO_SMALL -0x0008 /**< The output buffer is too small to write too. */
37#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 Bakker5121ce52009-01-03 21:22:43 +000060 * Define the base integer type, architecture-wise
61 */
Paul Bakker40e46942009-01-03 21:51:57 +000062#if defined(POLARSSL_HAVE_INT8)
Paul Bakkera755ca12011-04-24 09:11:17 +000063typedef signed char t_sint;
64typedef unsigned char t_uint;
65typedef unsigned short t_udbl;
Paul Bakker5121ce52009-01-03 21:22:43 +000066#else
Paul Bakker40e46942009-01-03 21:51:57 +000067#if defined(POLARSSL_HAVE_INT16)
Paul Bakkera755ca12011-04-24 09:11:17 +000068typedef signed short t_sint;
69typedef unsigned short t_uint;
70typedef unsigned long t_udbl;
Paul Bakker5121ce52009-01-03 21:22:43 +000071#else
Paul Bakkera755ca12011-04-24 09:11:17 +000072 typedef signed long t_sint;
73 typedef unsigned long t_uint;
Paul Bakker5121ce52009-01-03 21:22:43 +000074 #if defined(_MSC_VER) && defined(_M_IX86)
Paul Bakkera755ca12011-04-24 09:11:17 +000075 typedef unsigned __int64 t_udbl;
Paul Bakker5121ce52009-01-03 21:22:43 +000076 #else
77 #if defined(__amd64__) || defined(__x86_64__) || \
78 defined(__ppc64__) || defined(__powerpc64__) || \
79 defined(__ia64__) || defined(__alpha__)
Paul Bakkera755ca12011-04-24 09:11:17 +000080 typedef unsigned int t_udbl __attribute__((mode(TI)));
Paul Bakker5121ce52009-01-03 21:22:43 +000081 #else
Paul Bakker1a9382e2009-07-11 16:35:32 +000082 #if defined(POLARSSL_HAVE_LONGLONG)
Paul Bakkera755ca12011-04-24 09:11:17 +000083 typedef unsigned long long t_udbl;
Paul Bakker1a9382e2009-07-11 16:35:32 +000084 #endif
Paul Bakker5121ce52009-01-03 21:22:43 +000085 #endif
86 #endif
87#endif
88#endif
89
90/**
91 * \brief MPI structure
92 */
93typedef struct
94{
95 int s; /*!< integer sign */
Paul Bakker23986e52011-04-24 08:57:21 +000096 size_t n; /*!< total # of limbs */
Paul Bakkera755ca12011-04-24 09:11:17 +000097 t_uint *p; /*!< pointer to limbs */
Paul Bakker5121ce52009-01-03 21:22:43 +000098}
99mpi;
100
101#ifdef __cplusplus
102extern "C" {
103#endif
104
105/**
Paul Bakker6c591fa2011-05-05 11:49:20 +0000106 * \brief Initialize one MPI
107 *
108 * \param X One MPI to initialize.
Paul Bakker5121ce52009-01-03 21:22:43 +0000109 */
Paul Bakker6c591fa2011-05-05 11:49:20 +0000110void mpi_init( mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000111
112/**
Paul Bakker6c591fa2011-05-05 11:49:20 +0000113 * \brief Unallocate one MPI
114 *
115 * \param X One MPI to unallocate.
Paul Bakker5121ce52009-01-03 21:22:43 +0000116 */
Paul Bakker6c591fa2011-05-05 11:49:20 +0000117void mpi_free( mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000118
119/**
120 * \brief Enlarge to the specified number of limbs
121 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000122 * \param X MPI to grow
123 * \param nblimbs The target number of limbs
124 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000125 * \return 0 if successful,
126 * 1 if memory allocation failed
127 */
Paul Bakker23986e52011-04-24 08:57:21 +0000128int mpi_grow( mpi *X, size_t nblimbs );
Paul Bakker5121ce52009-01-03 21:22:43 +0000129
130/**
131 * \brief Copy the contents of Y into X
132 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000133 * \param X Destination MPI
134 * \param Y Source MPI
135 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000136 * \return 0 if successful,
137 * 1 if memory allocation failed
138 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000139int mpi_copy( mpi *X, const mpi *Y );
Paul Bakker5121ce52009-01-03 21:22:43 +0000140
141/**
142 * \brief Swap the contents of X and Y
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000143 *
144 * \param X First MPI value
145 * \param Y Second MPI value
Paul Bakker5121ce52009-01-03 21:22:43 +0000146 */
147void mpi_swap( mpi *X, mpi *Y );
148
149/**
150 * \brief Set value from integer
151 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000152 * \param X MPI to set
153 * \param z Value to use
154 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000155 * \return 0 if successful,
156 * 1 if memory allocation failed
157 */
Paul Bakkera755ca12011-04-24 09:11:17 +0000158int mpi_lset( mpi *X, t_sint z );
Paul Bakker5121ce52009-01-03 21:22:43 +0000159
Paul Bakker2f5947e2011-05-18 15:47:11 +0000160/*
161 * \brief Get a specific bit from X
162 *
163 * \param X MPI to use
164 * \param pos Zero-based index of the bit in X
165 *
166 * \return Either a 0 or a 1
167 */
168int mpi_get_bit( mpi *X, size_t pos );
169
170/*
171 * \brief Set a bit of X to a specific value of 0 or 1
172 *
173 * \note Will grow X if necessary to set a bit to 1 in a not yet
174 * existing limb. Will not grow if bit should be set to 0
175 *
176 * \param X MPI to use
177 * \param pos Zero-based index of the bit in X
178 * \param val The value to set the bit to (0 or 1)
179 *
180 * \return 0 if successful,
181 * 1 if memory allocation failed,
182 * POLARSSL_ERR_MPI_BAD_INPUT_DATA if val is not 0 or 1
183 */
184int mpi_set_bit( mpi *X, size_t pos, unsigned char val );
185
Paul Bakker5121ce52009-01-03 21:22:43 +0000186/**
187 * \brief Return the number of least significant bits
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000188 *
189 * \param X MPI to use
Paul Bakker5121ce52009-01-03 21:22:43 +0000190 */
Paul Bakker23986e52011-04-24 08:57:21 +0000191size_t mpi_lsb( const mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000192
193/**
194 * \brief Return the number of most significant bits
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000195 *
196 * \param X MPI to use
Paul Bakker5121ce52009-01-03 21:22:43 +0000197 */
Paul Bakker23986e52011-04-24 08:57:21 +0000198size_t mpi_msb( const mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000199
200/**
201 * \brief Return the total size in bytes
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000202 *
203 * \param X MPI to use
Paul Bakker5121ce52009-01-03 21:22:43 +0000204 */
Paul Bakker23986e52011-04-24 08:57:21 +0000205size_t mpi_size( const mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000206
207/**
208 * \brief Import from an ASCII string
209 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000210 * \param X Destination MPI
211 * \param radix Input numeric base
212 * \param s Null-terminated string buffer
Paul Bakker5121ce52009-01-03 21:22:43 +0000213 *
Paul Bakker40e46942009-01-03 21:51:57 +0000214 * \return 0 if successful, or an POLARSSL_ERR_MPI_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000215 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000216int mpi_read_string( mpi *X, int radix, const char *s );
Paul Bakker5121ce52009-01-03 21:22:43 +0000217
218/**
219 * \brief Export into an ASCII string
220 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000221 * \param X Source MPI
222 * \param radix Output numeric base
223 * \param s String buffer
224 * \param slen String buffer size
Paul Bakker5121ce52009-01-03 21:22:43 +0000225 *
Paul Bakkerff60ee62010-03-16 21:09:09 +0000226 * \return 0 if successful, or an POLARSSL_ERR_MPI_XXX error code.
227 * *slen is always updated to reflect the amount
228 * of data that has (or would have) been written.
Paul Bakker5121ce52009-01-03 21:22:43 +0000229 *
230 * \note Call this function with *slen = 0 to obtain the
231 * minimum required buffer size in *slen.
232 */
Paul Bakker23986e52011-04-24 08:57:21 +0000233int mpi_write_string( const mpi *X, int radix, char *s, size_t *slen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000234
235/**
236 * \brief Read X from an opened file
237 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000238 * \param X Destination MPI
239 * \param radix Input numeric base
240 * \param fin Input file handle
Paul Bakker5121ce52009-01-03 21:22:43 +0000241 *
Paul Bakker40e46942009-01-03 21:51:57 +0000242 * \return 0 if successful, or an POLARSSL_ERR_MPI_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000243 */
244int mpi_read_file( mpi *X, int radix, FILE *fin );
245
246/**
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000247 * \brief Write X into an opened file, or stdout if fout is NULL
Paul Bakker5121ce52009-01-03 21:22:43 +0000248 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000249 * \param p Prefix, can be NULL
250 * \param X Source MPI
251 * \param radix Output numeric base
252 * \param fout Output file handle (can be NULL)
Paul Bakker5121ce52009-01-03 21:22:43 +0000253 *
Paul Bakker40e46942009-01-03 21:51:57 +0000254 * \return 0 if successful, or an POLARSSL_ERR_MPI_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000255 *
256 * \note Set fout == NULL to print X on the console.
257 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000258int mpi_write_file( const char *p, const mpi *X, int radix, FILE *fout );
Paul Bakker5121ce52009-01-03 21:22:43 +0000259
260/**
261 * \brief Import X from unsigned binary data, big endian
262 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000263 * \param X Destination MPI
264 * \param buf Input buffer
265 * \param buflen Input buffer size
Paul Bakker5121ce52009-01-03 21:22:43 +0000266 *
267 * \return 0 if successful,
268 * 1 if memory allocation failed
269 */
Paul Bakker23986e52011-04-24 08:57:21 +0000270int mpi_read_binary( mpi *X, const unsigned char *buf, size_t buflen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000271
272/**
273 * \brief Export X into unsigned binary data, big endian
274 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000275 * \param X Source MPI
276 * \param buf Output buffer
277 * \param buflen Output buffer size
Paul Bakker5121ce52009-01-03 21:22:43 +0000278 *
279 * \return 0 if successful,
Paul Bakker40e46942009-01-03 21:51:57 +0000280 * POLARSSL_ERR_MPI_BUFFER_TOO_SMALL if buf isn't large enough
Paul Bakker5121ce52009-01-03 21:22:43 +0000281 */
Paul Bakker23986e52011-04-24 08:57:21 +0000282int mpi_write_binary( const mpi *X, unsigned char *buf, size_t buflen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000283
284/**
285 * \brief Left-shift: X <<= count
286 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000287 * \param X MPI to shift
288 * \param count Amount to shift
289 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000290 * \return 0 if successful,
291 * 1 if memory allocation failed
292 */
Paul Bakker23986e52011-04-24 08:57:21 +0000293int mpi_shift_l( mpi *X, size_t count );
Paul Bakker5121ce52009-01-03 21:22:43 +0000294
295/**
296 * \brief Right-shift: X >>= count
297 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000298 * \param X MPI to shift
299 * \param count Amount to shift
300 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000301 * \return 0 if successful,
302 * 1 if memory allocation failed
303 */
Paul Bakker23986e52011-04-24 08:57:21 +0000304int mpi_shift_r( mpi *X, size_t count );
Paul Bakker5121ce52009-01-03 21:22:43 +0000305
306/**
307 * \brief Compare unsigned values
308 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000309 * \param X Left-hand MPI
310 * \param Y Right-hand MPI
311 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000312 * \return 1 if |X| is greater than |Y|,
313 * -1 if |X| is lesser than |Y| or
314 * 0 if |X| is equal to |Y|
315 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000316int mpi_cmp_abs( const mpi *X, const mpi *Y );
Paul Bakker5121ce52009-01-03 21:22:43 +0000317
318/**
319 * \brief Compare signed values
320 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000321 * \param X Left-hand MPI
322 * \param Y Right-hand MPI
323 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000324 * \return 1 if X is greater than Y,
325 * -1 if X is lesser than Y or
326 * 0 if X is equal to Y
327 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000328int mpi_cmp_mpi( const mpi *X, const mpi *Y );
Paul Bakker5121ce52009-01-03 21:22:43 +0000329
330/**
331 * \brief Compare signed values
332 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000333 * \param X Left-hand MPI
334 * \param z The integer value to compare to
335 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000336 * \return 1 if X is greater than z,
337 * -1 if X is lesser than z or
338 * 0 if X is equal to z
339 */
Paul Bakkera755ca12011-04-24 09:11:17 +0000340int mpi_cmp_int( const mpi *X, t_sint z );
Paul Bakker5121ce52009-01-03 21:22:43 +0000341
342/**
343 * \brief Unsigned addition: X = |A| + |B|
344 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000345 * \param X Destination MPI
346 * \param A Left-hand MPI
347 * \param B Right-hand MPI
348 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000349 * \return 0 if successful,
350 * 1 if memory allocation failed
351 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000352int mpi_add_abs( mpi *X, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000353
354/**
355 * \brief Unsigned substraction: X = |A| - |B|
356 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000357 * \param X Destination MPI
358 * \param A Left-hand MPI
359 * \param B Right-hand MPI
360 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000361 * \return 0 if successful,
Paul Bakker40e46942009-01-03 21:51:57 +0000362 * POLARSSL_ERR_MPI_NEGATIVE_VALUE if B is greater than A
Paul Bakker5121ce52009-01-03 21:22:43 +0000363 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000364int mpi_sub_abs( mpi *X, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000365
366/**
367 * \brief Signed 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_mpi( mpi *X, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000377
378/**
379 * \brief Signed 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,
386 * 1 if memory allocation failed
387 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000388int mpi_sub_mpi( 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 The integer value to add
396 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000397 * \return 0 if successful,
398 * 1 if memory allocation failed
399 */
Paul Bakkera755ca12011-04-24 09:11:17 +0000400int mpi_add_int( mpi *X, const mpi *A, t_sint 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 The integer value to subtract
408 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000409 * \return 0 if successful,
410 * 1 if memory allocation failed
411 */
Paul Bakkera755ca12011-04-24 09:11:17 +0000412int mpi_sub_int( mpi *X, const mpi *A, t_sint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000413
414/**
415 * \brief Baseline multiplication: 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,
422 * 1 if memory allocation failed
423 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000424int mpi_mul_mpi( mpi *X, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000425
426/**
427 * \brief Baseline multiplication: X = A * b
Paul Bakkerce40a6d2009-06-23 19:46:08 +0000428 * Note: b is an unsigned integer type, thus
429 * Negative values of b are ignored.
Paul Bakker5121ce52009-01-03 21:22:43 +0000430 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000431 * \param X Destination MPI
432 * \param A Left-hand MPI
433 * \param b The integer value to multiply with
434 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000435 * \return 0 if successful,
436 * 1 if memory allocation failed
437 */
Paul Bakkera755ca12011-04-24 09:11:17 +0000438int mpi_mul_int( mpi *X, const mpi *A, t_sint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000439
440/**
441 * \brief Division by mpi: A = Q * B + R
442 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000443 * \param Q Destination MPI for the quotient
444 * \param R Destination MPI for the rest value
445 * \param A Left-hand MPI
446 * \param B Right-hand MPI
447 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000448 * \return 0 if successful,
449 * 1 if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000450 * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if B == 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000451 *
452 * \note Either Q or R can be NULL.
453 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000454int mpi_div_mpi( mpi *Q, mpi *R, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000455
456/**
457 * \brief Division by int: A = Q * b + R
458 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000459 * \param Q Destination MPI for the quotient
460 * \param R Destination MPI for the rest value
461 * \param A Left-hand MPI
462 * \param b Integer to divide by
463 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000464 * \return 0 if successful,
465 * 1 if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000466 * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if b == 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000467 *
468 * \note Either Q or R can be NULL.
469 */
Paul Bakkera755ca12011-04-24 09:11:17 +0000470int mpi_div_int( mpi *Q, mpi *R, const mpi *A, t_sint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000471
472/**
473 * \brief Modulo: R = A mod B
474 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000475 * \param R Destination MPI for the rest value
476 * \param A Left-hand MPI
477 * \param B Right-hand MPI
478 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000479 * \return 0 if successful,
480 * 1 if memory allocation failed,
Paul Bakkerce40a6d2009-06-23 19:46:08 +0000481 * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if B == 0,
482 * POLARSSL_ERR_MPI_NEGATIVE_VALUE if B < 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000483 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000484int mpi_mod_mpi( mpi *R, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000485
486/**
487 * \brief Modulo: r = A mod b
488 *
Paul Bakkera755ca12011-04-24 09:11:17 +0000489 * \param r Destination t_uint
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000490 * \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,
494 * 1 if memory allocation failed,
Paul Bakkerce40a6d2009-06-23 19:46:08 +0000495 * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if b == 0,
496 * POLARSSL_ERR_MPI_NEGATIVE_VALUE if b < 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000497 */
Paul Bakkera755ca12011-04-24 09:11:17 +0000498int mpi_mod_int( t_uint *r, const mpi *A, t_sint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000499
500/**
501 * \brief Sliding-window exponentiation: X = A^E mod N
502 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000503 * \param X Destination MPI
504 * \param A Left-hand MPI
505 * \param E Exponent MPI
506 * \param N Modular MPI
507 * \param _RR Speed-up MPI used for recalculations
508 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000509 * \return 0 if successful,
510 * 1 if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000511 * POLARSSL_ERR_MPI_BAD_INPUT_DATA if N is negative or even
Paul Bakker5121ce52009-01-03 21:22:43 +0000512 *
513 * \note _RR is used to avoid re-computing R*R mod N across
514 * multiple calls, which speeds up things a bit. It can
515 * be set to NULL if the extra performance is unneeded.
516 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000517int mpi_exp_mod( mpi *X, const mpi *A, const mpi *E, const mpi *N, mpi *_RR );
Paul Bakker5121ce52009-01-03 21:22:43 +0000518
519/**
Paul Bakker287781a2011-03-26 13:18:49 +0000520 * \brief Fill an MPI X with size bytes of random
521 *
522 * \param X Destination MPI
523 * \param size Size in bytes
524 * \param f_rng RNG function
525 * \param p_rng RNG parameter
526 *
527 * \return 0 if successful,
528 * 1 if memory allocation failed
529 */
Paul Bakker23986e52011-04-24 08:57:21 +0000530int mpi_fill_random( mpi *X, size_t size, int (*f_rng)(void *), void *p_rng );
Paul Bakker287781a2011-03-26 13:18:49 +0000531
532/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000533 * \brief Greatest common divisor: G = gcd(A, B)
534 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000535 * \param G Destination MPI
536 * \param A Left-hand MPI
537 * \param B Right-hand MPI
538 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000539 * \return 0 if successful,
540 * 1 if memory allocation failed
541 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000542int mpi_gcd( mpi *G, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000543
544/**
545 * \brief Modular inverse: X = A^-1 mod N
546 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000547 * \param X Destination MPI
548 * \param A Left-hand MPI
549 * \param N Right-hand MPI
550 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000551 * \return 0 if successful,
552 * 1 if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000553 * POLARSSL_ERR_MPI_BAD_INPUT_DATA if N is negative or nil
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000554 POLARSSL_ERR_MPI_NOT_ACCEPTABLE if A has no inverse mod N
Paul Bakker5121ce52009-01-03 21:22:43 +0000555 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000556int mpi_inv_mod( mpi *X, const mpi *A, const mpi *N );
Paul Bakker5121ce52009-01-03 21:22:43 +0000557
558/**
559 * \brief Miller-Rabin primality test
560 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000561 * \param X MPI to check
562 * \param f_rng RNG function
563 * \param p_rng RNG parameter
564 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000565 * \return 0 if successful (probably prime),
566 * 1 if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000567 * POLARSSL_ERR_MPI_NOT_ACCEPTABLE if X is not prime
Paul Bakker5121ce52009-01-03 21:22:43 +0000568 */
569int mpi_is_prime( mpi *X, int (*f_rng)(void *), void *p_rng );
570
571/**
572 * \brief Prime number generation
573 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000574 * \param X Destination MPI
Paul Bakkerf9688572011-05-05 10:00:45 +0000575 * \param nbits Required size of X in bits ( 3 <= nbits <= 4096 )
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000576 * \param dh_flag If 1, then (X-1)/2 will be prime too
Paul Bakker5121ce52009-01-03 21:22:43 +0000577 * \param f_rng RNG function
578 * \param p_rng RNG parameter
579 *
580 * \return 0 if successful (probably prime),
581 * 1 if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000582 * POLARSSL_ERR_MPI_BAD_INPUT_DATA if nbits is < 3
Paul Bakker5121ce52009-01-03 21:22:43 +0000583 */
Paul Bakker23986e52011-04-24 08:57:21 +0000584int mpi_gen_prime( mpi *X, size_t nbits, int dh_flag,
Paul Bakker5121ce52009-01-03 21:22:43 +0000585 int (*f_rng)(void *), void *p_rng );
586
587/**
588 * \brief Checkup routine
589 *
590 * \return 0 if successful, or 1 if the test failed
591 */
592int mpi_self_test( int verbose );
593
594#ifdef __cplusplus
595}
596#endif
597
598#endif /* bignum.h */