blob: 12435f72833ac6b3cdd441b1ec20a149e5378a3a [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 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 Bakker5121ce52009-01-03 21:22:43 +000070 * Define the base integer type, architecture-wise
71 */
Paul Bakker40e46942009-01-03 21:51:57 +000072#if defined(POLARSSL_HAVE_INT8)
Paul Bakkera755ca12011-04-24 09:11:17 +000073typedef signed char t_sint;
74typedef unsigned char t_uint;
75typedef unsigned short t_udbl;
Paul Bakker5121ce52009-01-03 21:22:43 +000076#else
Paul Bakker40e46942009-01-03 21:51:57 +000077#if defined(POLARSSL_HAVE_INT16)
Paul Bakkera755ca12011-04-24 09:11:17 +000078typedef signed short t_sint;
79typedef unsigned short t_uint;
80typedef unsigned long t_udbl;
Paul Bakker5121ce52009-01-03 21:22:43 +000081#else
Paul Bakkera755ca12011-04-24 09:11:17 +000082 typedef signed long t_sint;
83 typedef unsigned long t_uint;
Paul Bakker5121ce52009-01-03 21:22:43 +000084 #if defined(_MSC_VER) && defined(_M_IX86)
Paul Bakkera755ca12011-04-24 09:11:17 +000085 typedef unsigned __int64 t_udbl;
Paul Bakker5121ce52009-01-03 21:22:43 +000086 #else
87 #if defined(__amd64__) || defined(__x86_64__) || \
88 defined(__ppc64__) || defined(__powerpc64__) || \
Paul Bakker44637402011-11-26 09:23:07 +000089 defined(__ia64__) || defined(__alpha__) || \
90 (defined(__sparc__) && defined(__arch64__)) || \
91 defined(__s390x__)
Paul Bakkera755ca12011-04-24 09:11:17 +000092 typedef unsigned int t_udbl __attribute__((mode(TI)));
Paul Bakker5121ce52009-01-03 21:22:43 +000093 #else
Paul Bakker1a9382e2009-07-11 16:35:32 +000094 #if defined(POLARSSL_HAVE_LONGLONG)
Paul Bakkera755ca12011-04-24 09:11:17 +000095 typedef unsigned long long t_udbl;
Paul Bakker1a9382e2009-07-11 16:35:32 +000096 #endif
Paul Bakker5121ce52009-01-03 21:22:43 +000097 #endif
98 #endif
99#endif
100#endif
101
102/**
103 * \brief MPI structure
104 */
105typedef struct
106{
107 int s; /*!< integer sign */
Paul Bakker23986e52011-04-24 08:57:21 +0000108 size_t n; /*!< total # of limbs */
Paul Bakkera755ca12011-04-24 09:11:17 +0000109 t_uint *p; /*!< pointer to limbs */
Paul Bakker5121ce52009-01-03 21:22:43 +0000110}
111mpi;
112
113#ifdef __cplusplus
114extern "C" {
115#endif
116
117/**
Paul Bakker6c591fa2011-05-05 11:49:20 +0000118 * \brief Initialize one MPI
119 *
120 * \param X One MPI to initialize.
Paul Bakker5121ce52009-01-03 21:22:43 +0000121 */
Paul Bakker6c591fa2011-05-05 11:49:20 +0000122void mpi_init( mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000123
124/**
Paul Bakker6c591fa2011-05-05 11:49:20 +0000125 * \brief Unallocate one MPI
126 *
127 * \param X One MPI to unallocate.
Paul Bakker5121ce52009-01-03 21:22:43 +0000128 */
Paul Bakker6c591fa2011-05-05 11:49:20 +0000129void mpi_free( mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000130
131/**
132 * \brief Enlarge to the specified number of limbs
133 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000134 * \param X MPI to grow
135 * \param nblimbs The target number of limbs
136 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000137 * \return 0 if successful,
138 * 1 if memory allocation failed
139 */
Paul Bakker23986e52011-04-24 08:57:21 +0000140int mpi_grow( mpi *X, size_t nblimbs );
Paul Bakker5121ce52009-01-03 21:22:43 +0000141
142/**
143 * \brief Copy the contents of Y into X
144 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000145 * \param X Destination MPI
146 * \param Y Source MPI
147 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000148 * \return 0 if successful,
149 * 1 if memory allocation failed
150 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000151int mpi_copy( mpi *X, const mpi *Y );
Paul Bakker5121ce52009-01-03 21:22:43 +0000152
153/**
154 * \brief Swap the contents of X and Y
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000155 *
156 * \param X First MPI value
157 * \param Y Second MPI value
Paul Bakker5121ce52009-01-03 21:22:43 +0000158 */
159void mpi_swap( mpi *X, mpi *Y );
160
161/**
162 * \brief Set value from integer
163 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000164 * \param X MPI to set
165 * \param z Value to use
166 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000167 * \return 0 if successful,
168 * 1 if memory allocation failed
169 */
Paul Bakkera755ca12011-04-24 09:11:17 +0000170int mpi_lset( mpi *X, t_sint z );
Paul Bakker5121ce52009-01-03 21:22:43 +0000171
Paul Bakker2f5947e2011-05-18 15:47:11 +0000172/*
173 * \brief Get a specific bit from X
174 *
175 * \param X MPI to use
176 * \param pos Zero-based index of the bit in X
177 *
178 * \return Either a 0 or a 1
179 */
180int mpi_get_bit( mpi *X, size_t pos );
181
182/*
183 * \brief Set a bit of X to a specific value of 0 or 1
184 *
185 * \note Will grow X if necessary to set a bit to 1 in a not yet
186 * existing limb. Will not grow if bit should be set to 0
187 *
188 * \param X MPI to use
189 * \param pos Zero-based index of the bit in X
190 * \param val The value to set the bit to (0 or 1)
191 *
192 * \return 0 if successful,
193 * 1 if memory allocation failed,
194 * POLARSSL_ERR_MPI_BAD_INPUT_DATA if val is not 0 or 1
195 */
196int mpi_set_bit( mpi *X, size_t pos, unsigned char val );
197
Paul Bakker5121ce52009-01-03 21:22:43 +0000198/**
199 * \brief Return the number of least significant bits
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000200 *
201 * \param X MPI to use
Paul Bakker5121ce52009-01-03 21:22:43 +0000202 */
Paul Bakker23986e52011-04-24 08:57:21 +0000203size_t mpi_lsb( const mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000204
205/**
206 * \brief Return the number of most significant bits
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000207 *
208 * \param X MPI to use
Paul Bakker5121ce52009-01-03 21:22:43 +0000209 */
Paul Bakker23986e52011-04-24 08:57:21 +0000210size_t mpi_msb( const mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000211
212/**
213 * \brief Return the total size in bytes
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_size( const mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000218
219/**
220 * \brief Import from an ASCII string
221 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000222 * \param X Destination MPI
223 * \param radix Input numeric base
224 * \param s Null-terminated string buffer
Paul Bakker5121ce52009-01-03 21:22:43 +0000225 *
Paul Bakker40e46942009-01-03 21:51:57 +0000226 * \return 0 if successful, or an POLARSSL_ERR_MPI_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000227 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000228int mpi_read_string( mpi *X, int radix, const char *s );
Paul Bakker5121ce52009-01-03 21:22:43 +0000229
230/**
231 * \brief Export into an ASCII string
232 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000233 * \param X Source MPI
234 * \param radix Output numeric base
235 * \param s String buffer
236 * \param slen String buffer size
Paul Bakker5121ce52009-01-03 21:22:43 +0000237 *
Paul Bakkerff60ee62010-03-16 21:09:09 +0000238 * \return 0 if successful, or an POLARSSL_ERR_MPI_XXX error code.
239 * *slen is always updated to reflect the amount
240 * of data that has (or would have) been written.
Paul Bakker5121ce52009-01-03 21:22:43 +0000241 *
242 * \note Call this function with *slen = 0 to obtain the
243 * minimum required buffer size in *slen.
244 */
Paul Bakker23986e52011-04-24 08:57:21 +0000245int mpi_write_string( const mpi *X, int radix, char *s, size_t *slen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000246
247/**
248 * \brief Read X from an opened file
249 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000250 * \param X Destination MPI
251 * \param radix Input numeric base
252 * \param fin Input file handle
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 */
256int mpi_read_file( mpi *X, int radix, FILE *fin );
257
258/**
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000259 * \brief Write X into an opened file, or stdout if fout is NULL
Paul Bakker5121ce52009-01-03 21:22:43 +0000260 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000261 * \param p Prefix, can be NULL
262 * \param X Source MPI
263 * \param radix Output numeric base
264 * \param fout Output file handle (can be NULL)
Paul Bakker5121ce52009-01-03 21:22:43 +0000265 *
Paul Bakker40e46942009-01-03 21:51:57 +0000266 * \return 0 if successful, or an POLARSSL_ERR_MPI_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000267 *
268 * \note Set fout == NULL to print X on the console.
269 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000270int mpi_write_file( const char *p, const mpi *X, int radix, FILE *fout );
Paul Bakker5121ce52009-01-03 21:22:43 +0000271
272/**
273 * \brief Import X from unsigned binary data, big endian
274 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000275 * \param X Destination MPI
276 * \param buf Input buffer
277 * \param buflen Input buffer size
Paul Bakker5121ce52009-01-03 21:22:43 +0000278 *
279 * \return 0 if successful,
280 * 1 if memory allocation failed
281 */
Paul Bakker23986e52011-04-24 08:57:21 +0000282int mpi_read_binary( mpi *X, const unsigned char *buf, size_t buflen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000283
284/**
285 * \brief Export X into unsigned binary data, big endian
286 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000287 * \param X Source MPI
288 * \param buf Output buffer
289 * \param buflen Output buffer size
Paul Bakker5121ce52009-01-03 21:22:43 +0000290 *
291 * \return 0 if successful,
Paul Bakker40e46942009-01-03 21:51:57 +0000292 * POLARSSL_ERR_MPI_BUFFER_TOO_SMALL if buf isn't large enough
Paul Bakker5121ce52009-01-03 21:22:43 +0000293 */
Paul Bakker23986e52011-04-24 08:57:21 +0000294int mpi_write_binary( const mpi *X, unsigned char *buf, size_t buflen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000295
296/**
297 * \brief Left-shift: X <<= count
298 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000299 * \param X MPI to shift
300 * \param count Amount to shift
301 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000302 * \return 0 if successful,
303 * 1 if memory allocation failed
304 */
Paul Bakker23986e52011-04-24 08:57:21 +0000305int mpi_shift_l( mpi *X, size_t count );
Paul Bakker5121ce52009-01-03 21:22:43 +0000306
307/**
308 * \brief Right-shift: X >>= count
309 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000310 * \param X MPI to shift
311 * \param count Amount to shift
312 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000313 * \return 0 if successful,
314 * 1 if memory allocation failed
315 */
Paul Bakker23986e52011-04-24 08:57:21 +0000316int mpi_shift_r( mpi *X, size_t count );
Paul Bakker5121ce52009-01-03 21:22:43 +0000317
318/**
319 * \brief Compare unsigned 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_abs( 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 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_mpi( 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 z The integer value to compare to
347 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000348 * \return 1 if X is greater than z,
349 * -1 if X is lesser than z or
350 * 0 if X is equal to z
351 */
Paul Bakkera755ca12011-04-24 09:11:17 +0000352int mpi_cmp_int( const mpi *X, t_sint z );
Paul Bakker5121ce52009-01-03 21:22:43 +0000353
354/**
355 * \brief Unsigned addition: 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,
362 * 1 if memory allocation failed
363 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000364int mpi_add_abs( mpi *X, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000365
366/**
367 * \brief Unsigned substraction: 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,
Paul Bakker40e46942009-01-03 21:51:57 +0000374 * POLARSSL_ERR_MPI_NEGATIVE_VALUE if B is greater than A
Paul Bakker5121ce52009-01-03 21:22:43 +0000375 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000376int mpi_sub_abs( mpi *X, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000377
378/**
379 * \brief Signed addition: X = A + B
380 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000381 * \param X Destination MPI
382 * \param A Left-hand MPI
383 * \param B Right-hand MPI
384 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000385 * \return 0 if successful,
386 * 1 if memory allocation failed
387 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000388int mpi_add_mpi( mpi *X, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000389
390/**
391 * \brief Signed substraction: X = A - B
392 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000393 * \param X Destination MPI
394 * \param A Left-hand MPI
395 * \param B Right-hand MPI
396 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000397 * \return 0 if successful,
398 * 1 if memory allocation failed
399 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000400int mpi_sub_mpi( mpi *X, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000401
402/**
403 * \brief Signed addition: X = A + b
404 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000405 * \param X Destination MPI
406 * \param A Left-hand MPI
407 * \param b The integer value to add
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_add_int( mpi *X, const mpi *A, t_sint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000413
414/**
415 * \brief Signed substraction: X = A - b
416 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000417 * \param X Destination MPI
418 * \param A Left-hand MPI
419 * \param b The integer value to subtract
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_sub_int( mpi *X, const mpi *A, t_sint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000425
426/**
427 * \brief Baseline multiplication: 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 Right-hand MPI
432 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000433 * \return 0 if successful,
434 * 1 if memory allocation failed
435 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000436int mpi_mul_mpi( mpi *X, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000437
438/**
439 * \brief Baseline multiplication: X = A * b
Paul Bakkerce40a6d2009-06-23 19:46:08 +0000440 * Note: b is an unsigned integer type, thus
441 * Negative values of b are ignored.
Paul Bakker5121ce52009-01-03 21:22:43 +0000442 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000443 * \param X Destination MPI
444 * \param A Left-hand MPI
445 * \param b The integer value to multiply with
446 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000447 * \return 0 if successful,
448 * 1 if memory allocation failed
449 */
Paul Bakkera755ca12011-04-24 09:11:17 +0000450int mpi_mul_int( mpi *X, const mpi *A, t_sint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000451
452/**
453 * \brief Division by mpi: A = Q * B + R
454 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000455 * \param Q Destination MPI for the quotient
456 * \param R Destination MPI for the rest value
457 * \param A Left-hand MPI
458 * \param B Right-hand MPI
459 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000460 * \return 0 if successful,
461 * 1 if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000462 * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if B == 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000463 *
464 * \note Either Q or R can be NULL.
465 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000466int mpi_div_mpi( mpi *Q, mpi *R, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000467
468/**
469 * \brief Division by int: 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 Integer to divide by
475 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000476 * \return 0 if successful,
477 * 1 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 Bakkera755ca12011-04-24 09:11:17 +0000482int mpi_div_int( mpi *Q, mpi *R, const mpi *A, t_sint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000483
484/**
485 * \brief Modulo: R = A mod B
486 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000487 * \param R Destination MPI for the rest value
488 * \param A Left-hand MPI
489 * \param B Right-hand MPI
490 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000491 * \return 0 if successful,
492 * 1 if memory allocation failed,
Paul Bakkerce40a6d2009-06-23 19:46:08 +0000493 * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if B == 0,
494 * POLARSSL_ERR_MPI_NEGATIVE_VALUE if B < 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000495 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000496int mpi_mod_mpi( mpi *R, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000497
498/**
499 * \brief Modulo: r = A mod b
500 *
Paul Bakkera755ca12011-04-24 09:11:17 +0000501 * \param r Destination t_uint
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000502 * \param A Left-hand MPI
503 * \param b Integer to divide by
504 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000505 * \return 0 if successful,
506 * 1 if memory allocation failed,
Paul Bakkerce40a6d2009-06-23 19:46:08 +0000507 * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if b == 0,
508 * POLARSSL_ERR_MPI_NEGATIVE_VALUE if b < 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000509 */
Paul Bakkera755ca12011-04-24 09:11:17 +0000510int mpi_mod_int( t_uint *r, const mpi *A, t_sint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000511
512/**
513 * \brief Sliding-window exponentiation: X = A^E mod N
514 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000515 * \param X Destination MPI
516 * \param A Left-hand MPI
517 * \param E Exponent MPI
518 * \param N Modular MPI
519 * \param _RR Speed-up MPI used for recalculations
520 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000521 * \return 0 if successful,
522 * 1 if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000523 * POLARSSL_ERR_MPI_BAD_INPUT_DATA if N is negative or even
Paul Bakker5121ce52009-01-03 21:22:43 +0000524 *
525 * \note _RR is used to avoid re-computing R*R mod N across
526 * multiple calls, which speeds up things a bit. It can
527 * be set to NULL if the extra performance is unneeded.
528 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000529int mpi_exp_mod( mpi *X, const mpi *A, const mpi *E, const mpi *N, mpi *_RR );
Paul Bakker5121ce52009-01-03 21:22:43 +0000530
531/**
Paul Bakker287781a2011-03-26 13:18:49 +0000532 * \brief Fill an MPI X with size bytes of random
533 *
534 * \param X Destination MPI
535 * \param size Size in bytes
536 * \param f_rng RNG function
537 * \param p_rng RNG parameter
538 *
539 * \return 0 if successful,
540 * 1 if memory allocation failed
541 */
Paul Bakkera3d195c2011-11-27 21:07:34 +0000542int mpi_fill_random( mpi *X, size_t size,
543 int (*f_rng)(void *, unsigned char *, size_t),
544 void *p_rng );
Paul Bakker287781a2011-03-26 13:18:49 +0000545
546/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000547 * \brief Greatest common divisor: G = gcd(A, B)
548 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000549 * \param G Destination MPI
550 * \param A Left-hand MPI
551 * \param B Right-hand MPI
552 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000553 * \return 0 if successful,
554 * 1 if memory allocation failed
555 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000556int mpi_gcd( mpi *G, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000557
558/**
559 * \brief Modular inverse: X = A^-1 mod N
560 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000561 * \param X Destination MPI
562 * \param A Left-hand MPI
563 * \param N Right-hand MPI
564 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000565 * \return 0 if successful,
566 * 1 if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000567 * POLARSSL_ERR_MPI_BAD_INPUT_DATA if N is negative or nil
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000568 POLARSSL_ERR_MPI_NOT_ACCEPTABLE if A has no inverse mod N
Paul Bakker5121ce52009-01-03 21:22:43 +0000569 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000570int mpi_inv_mod( mpi *X, const mpi *A, const mpi *N );
Paul Bakker5121ce52009-01-03 21:22:43 +0000571
572/**
573 * \brief Miller-Rabin primality test
574 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000575 * \param X MPI to check
576 * \param f_rng RNG function
577 * \param p_rng RNG parameter
578 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000579 * \return 0 if successful (probably prime),
580 * 1 if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000581 * POLARSSL_ERR_MPI_NOT_ACCEPTABLE if X is not prime
Paul Bakker5121ce52009-01-03 21:22:43 +0000582 */
Paul Bakkera3d195c2011-11-27 21:07:34 +0000583int mpi_is_prime( mpi *X,
584 int (*f_rng)(void *, unsigned char *, size_t),
585 void *p_rng );
Paul Bakker5121ce52009-01-03 21:22:43 +0000586
587/**
588 * \brief Prime number generation
589 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000590 * \param X Destination MPI
Paul Bakkerfe3256e2011-11-25 12:11:43 +0000591 * \param nbits Required size of X in bits ( 3 <= nbits <= POLARSSL_MPI_MAX_BITS )
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000592 * \param dh_flag If 1, then (X-1)/2 will be prime too
Paul Bakker5121ce52009-01-03 21:22:43 +0000593 * \param f_rng RNG function
594 * \param p_rng RNG parameter
595 *
596 * \return 0 if successful (probably prime),
597 * 1 if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000598 * POLARSSL_ERR_MPI_BAD_INPUT_DATA if nbits is < 3
Paul Bakker5121ce52009-01-03 21:22:43 +0000599 */
Paul Bakker23986e52011-04-24 08:57:21 +0000600int mpi_gen_prime( mpi *X, size_t nbits, int dh_flag,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000601 int (*f_rng)(void *, unsigned char *, size_t),
602 void *p_rng );
Paul Bakker5121ce52009-01-03 21:22:43 +0000603
604/**
605 * \brief Checkup routine
606 *
607 * \return 0 if successful, or 1 if the test failed
608 */
609int mpi_self_test( int verbose );
610
611#ifdef __cplusplus
612}
613#endif
614
615#endif /* bignum.h */