blob: 180743945671c8ebf68e3058881c10ea7396bc25 [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 *
Manuel Pégourié-Gonnarda658a402015-01-23 09:45:19 +00006 * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
Paul Bakkerb96f1542010-07-18 20:36:00 +00007 *
Manuel Pégourié-Gonnard085ab042015-01-23 11:06:27 +00008 * This file is part of mbed TLS (https://www.polarssl.org)
Paul Bakkerb96f1542010-07-18 20:36:00 +00009 *
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000010 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Paul Bakker5121ce52009-01-03 21:22:43 +000023 */
Paul Bakker40e46942009-01-03 21:51:57 +000024#ifndef POLARSSL_BIGNUM_H
25#define POLARSSL_BIGNUM_H
Paul Bakker5121ce52009-01-03 21:22:43 +000026
27#include <stdio.h>
Paul Bakker23986e52011-04-24 08:57:21 +000028#include <string.h>
Paul Bakker5121ce52009-01-03 21:22:43 +000029
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020030#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakkercf0360a2012-01-20 10:08:14 +000031#include "config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020032#else
33#include POLARSSL_CONFIG_FILE
34#endif
Paul Bakkercf0360a2012-01-20 10:08:14 +000035
Paul Bakkerfa6a6202013-10-28 18:48:30 +010036#if defined(_MSC_VER) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker5c2364c2012-10-01 14:41:15 +000037#include <basetsd.h>
Paul Bakker4a2bd0d2012-11-02 11:06:08 +000038#if (_MSC_VER <= 1200)
39typedef signed short int16_t;
40typedef unsigned short uint16_t;
41#else
Paul Bakker5c2364c2012-10-01 14:41:15 +000042typedef INT16 int16_t;
43typedef UINT16 uint16_t;
Paul Bakker4a2bd0d2012-11-02 11:06:08 +000044#endif
Paul Bakker9ef6e2b2012-10-01 20:57:38 +000045typedef INT32 int32_t;
Paul Bakker8ea31ff2013-02-27 15:02:50 +010046typedef INT64 int64_t;
Paul Bakker5c2364c2012-10-01 14:41:15 +000047typedef UINT32 uint32_t;
48typedef UINT64 uint64_t;
49#else
50#include <inttypes.h>
Paul Bakker9af723c2014-05-01 13:03:14 +020051#endif /* _MSC_VER && !EFIX64 && !EFI32 */
Paul Bakker5c2364c2012-10-01 14:41:15 +000052
Paul Bakker9d781402011-05-09 16:17:09 +000053#define POLARSSL_ERR_MPI_FILE_IO_ERROR -0x0002 /**< An error occurred while reading from or writing to a file. */
54#define POLARSSL_ERR_MPI_BAD_INPUT_DATA -0x0004 /**< Bad input parameters to function. */
55#define POLARSSL_ERR_MPI_INVALID_CHARACTER -0x0006 /**< There is an invalid character in the digit string. */
Paul Bakker69e095c2011-12-10 21:55:01 +000056#define POLARSSL_ERR_MPI_BUFFER_TOO_SMALL -0x0008 /**< The buffer is too small to write to. */
Paul Bakker9d781402011-05-09 16:17:09 +000057#define POLARSSL_ERR_MPI_NEGATIVE_VALUE -0x000A /**< The input arguments are negative or result in illegal output. */
58#define POLARSSL_ERR_MPI_DIVISION_BY_ZERO -0x000C /**< The input argument for division is zero, which is not allowed. */
59#define POLARSSL_ERR_MPI_NOT_ACCEPTABLE -0x000E /**< The input arguments are not acceptable. */
Paul Bakker69e095c2011-12-10 21:55:01 +000060#define POLARSSL_ERR_MPI_MALLOC_FAILED -0x0010 /**< Memory allocation failed. */
Paul Bakker5121ce52009-01-03 21:22:43 +000061
Manuel Pégourié-Gonnard0160eac2013-11-22 17:54:59 +010062#define MPI_CHK(f) do { if( ( ret = f ) != 0 ) goto cleanup; } while( 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000063
64/*
Paul Bakkerf9688572011-05-05 10:00:45 +000065 * Maximum size MPIs are allowed to grow to in number of limbs.
66 */
67#define POLARSSL_MPI_MAX_LIMBS 10000
68
Paul Bakker770268f2014-05-05 11:40:14 +020069#if !defined(POLARSSL_MPI_WINDOW_SIZE)
Paul Bakkerf9688572011-05-05 10:00:45 +000070/*
Paul Bakkerb6d5f082011-11-25 11:52:11 +000071 * Maximum window size used for modular exponentiation. Default: 6
72 * Minimum value: 1. Maximum value: 6.
73 *
74 * Result is an array of ( 2 << POLARSSL_MPI_WINDOW_SIZE ) MPIs used
75 * for the sliding window calculation. (So 64 by default)
76 *
77 * Reduction in size, reduces speed.
78 */
79#define POLARSSL_MPI_WINDOW_SIZE 6 /**< Maximum windows size used. */
Paul Bakker770268f2014-05-05 11:40:14 +020080#endif /* !POLARSSL_MPI_WINDOW_SIZE */
Paul Bakkerb6d5f082011-11-25 11:52:11 +000081
Paul Bakker770268f2014-05-05 11:40:14 +020082#if !defined(POLARSSL_MPI_MAX_SIZE)
Paul Bakkerb6d5f082011-11-25 11:52:11 +000083/*
Paul Bakkerfe3256e2011-11-25 12:11:43 +000084 * Maximum size of MPIs allowed in bits and bytes for user-MPIs.
Paul Bakkerf626e1d2013-01-21 12:10:00 +010085 * ( Default: 512 bytes => 4096 bits, Maximum tested: 2048 bytes => 16384 bits )
Paul Bakkerfe3256e2011-11-25 12:11:43 +000086 *
87 * Note: Calculations can results temporarily in larger MPIs. So the number
88 * of limbs required (POLARSSL_MPI_MAX_LIMBS) is higher.
89 */
Manuel Pégourié-Gonnardda1b4de2014-09-08 17:03:50 +020090#define POLARSSL_MPI_MAX_SIZE 1024 /**< Maximum number of bytes for usable MPIs. */
Paul Bakker770268f2014-05-05 11:40:14 +020091#endif /* !POLARSSL_MPI_MAX_SIZE */
Paul Bakker9bcf16c2013-06-24 19:31:17 +020092
Paul Bakkerfe3256e2011-11-25 12:11:43 +000093#define POLARSSL_MPI_MAX_BITS ( 8 * POLARSSL_MPI_MAX_SIZE ) /**< Maximum number of bits for usable MPIs. */
94
95/*
Paul Bakker5531c6d2012-09-26 19:20:46 +000096 * When reading from files with mpi_read_file() and writing to files with
97 * mpi_write_file() the buffer should have space
Paul Bakkercb37aa52011-11-30 16:00:20 +000098 * for a (short) label, the MPI (in the provided radix), the newline
99 * characters and the '\0'.
100 *
101 * By default we assume at least a 10 char label, a minimum radix of 10
102 * (decimal) and a maximum of 4096 bit numbers (1234 decimal chars).
Paul Bakkerf9183102012-09-27 20:42:35 +0000103 * Autosized at compile time for at least a 10 char label, a minimum radix
104 * of 10 (decimal) for a number of POLARSSL_MPI_MAX_BITS size.
105 *
106 * This used to be statically sized to 1250 for a maximum of 4096 bit
107 * numbers (1234 decimal chars).
108 *
109 * Calculate using the formula:
110 * POLARSSL_MPI_RW_BUFFER_SIZE = ceil(POLARSSL_MPI_MAX_BITS / ln(10) * ln(2)) +
111 * LabelSize + 6
Paul Bakkercb37aa52011-11-30 16:00:20 +0000112 */
Paul Bakkerf9183102012-09-27 20:42:35 +0000113#define POLARSSL_MPI_MAX_BITS_SCALE100 ( 100 * POLARSSL_MPI_MAX_BITS )
114#define LN_2_DIV_LN_10_SCALE100 332
115#define POLARSSL_MPI_RW_BUFFER_SIZE ( ((POLARSSL_MPI_MAX_BITS_SCALE100 + LN_2_DIV_LN_10_SCALE100 - 1) / LN_2_DIV_LN_10_SCALE100) + 10 + 6 )
Paul Bakkercb37aa52011-11-30 16:00:20 +0000116
117/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000118 * Define the base integer type, architecture-wise
119 */
Paul Bakker40e46942009-01-03 21:51:57 +0000120#if defined(POLARSSL_HAVE_INT8)
Paul Bakkera755ca12011-04-24 09:11:17 +0000121typedef signed char t_sint;
122typedef unsigned char t_uint;
Paul Bakker5c2364c2012-10-01 14:41:15 +0000123typedef uint16_t t_udbl;
Paul Bakker62261d62012-10-02 12:19:31 +0000124#define POLARSSL_HAVE_UDBL
Paul Bakker5121ce52009-01-03 21:22:43 +0000125#else
Paul Bakker40e46942009-01-03 21:51:57 +0000126#if defined(POLARSSL_HAVE_INT16)
Paul Bakker5c2364c2012-10-01 14:41:15 +0000127typedef int16_t t_sint;
128typedef uint16_t t_uint;
129typedef uint32_t t_udbl;
Paul Bakker62261d62012-10-02 12:19:31 +0000130#define POLARSSL_HAVE_UDBL
Paul Bakker5121ce52009-01-03 21:22:43 +0000131#else
Manuel Pégourié-Gonnard96eed7b2013-12-12 15:49:10 +0100132 /*
133 * 32-bit integers can be forced on 64-bit arches (eg. for testing purposes)
Barry K. Nathan79e69f92014-05-05 18:08:57 -0700134 * by defining POLARSSL_HAVE_INT32 and undefining POLARSSL_HAVE_ASM
Manuel Pégourié-Gonnard96eed7b2013-12-12 15:49:10 +0100135 */
136 #if ( ! defined(POLARSSL_HAVE_INT32) && \
137 defined(_MSC_VER) && defined(_M_AMD64) )
Manuel Pégourié-Gonnard5779cbe2013-10-23 20:17:00 +0200138 #define POLARSSL_HAVE_INT64
Paul Bakker62261d62012-10-02 12:19:31 +0000139 typedef int64_t t_sint;
140 typedef uint64_t t_uint;
141 #else
Manuel Pégourié-Gonnard96eed7b2013-12-12 15:49:10 +0100142 #if ( ! defined(POLARSSL_HAVE_INT32) && \
143 defined(__GNUC__) && ( \
Paul Bakker62261d62012-10-02 12:19:31 +0000144 defined(__amd64__) || defined(__x86_64__) || \
145 defined(__ppc64__) || defined(__powerpc64__) || \
146 defined(__ia64__) || defined(__alpha__) || \
147 (defined(__sparc__) && defined(__arch64__)) || \
148 defined(__s390x__) ) )
Manuel Pégourié-Gonnard5779cbe2013-10-23 20:17:00 +0200149 #define POLARSSL_HAVE_INT64
Paul Bakker62261d62012-10-02 12:19:31 +0000150 typedef int64_t t_sint;
151 typedef uint64_t t_uint;
152 typedef unsigned int t_udbl __attribute__((mode(TI)));
153 #define POLARSSL_HAVE_UDBL
154 #else
Manuel Pégourié-Gonnarda47e7052013-10-21 17:51:45 +0200155 #define POLARSSL_HAVE_INT32
Paul Bakker62261d62012-10-02 12:19:31 +0000156 typedef int32_t t_sint;
157 typedef uint32_t t_uint;
158 #if ( defined(_MSC_VER) && defined(_M_IX86) )
159 typedef uint64_t t_udbl;
160 #define POLARSSL_HAVE_UDBL
161 #else
162 #if defined( POLARSSL_HAVE_LONGLONG )
163 typedef unsigned long long t_udbl;
164 #define POLARSSL_HAVE_UDBL
165 #endif
166 #endif
Paul Bakkerdb20c102014-06-17 14:34:44 +0200167 #endif /* !POLARSSL_HAVE_INT32 && __GNUC__ && 64-bit platform */
168 #endif /* !POLARSSL_HAVE_INT32 && _MSC_VER && _M_AMD64 */
Paul Bakker5c2364c2012-10-01 14:41:15 +0000169#endif /* POLARSSL_HAVE_INT16 */
170#endif /* POLARSSL_HAVE_INT8 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000171
Paul Bakker407a0da2013-06-27 14:29:21 +0200172#ifdef __cplusplus
173extern "C" {
174#endif
175
Paul Bakker5121ce52009-01-03 21:22:43 +0000176/**
177 * \brief MPI structure
178 */
179typedef struct
180{
181 int s; /*!< integer sign */
Paul Bakker23986e52011-04-24 08:57:21 +0000182 size_t n; /*!< total # of limbs */
Paul Bakkera755ca12011-04-24 09:11:17 +0000183 t_uint *p; /*!< pointer to limbs */
Paul Bakker5121ce52009-01-03 21:22:43 +0000184}
185mpi;
186
Paul Bakker5121ce52009-01-03 21:22:43 +0000187/**
Paul Bakker6c591fa2011-05-05 11:49:20 +0000188 * \brief Initialize one MPI
189 *
190 * \param X One MPI to initialize.
Paul Bakker5121ce52009-01-03 21:22:43 +0000191 */
Paul Bakker6c591fa2011-05-05 11:49:20 +0000192void mpi_init( mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000193
194/**
Paul Bakker6c591fa2011-05-05 11:49:20 +0000195 * \brief Unallocate one MPI
196 *
197 * \param X One MPI to unallocate.
Paul Bakker5121ce52009-01-03 21:22:43 +0000198 */
Paul Bakker6c591fa2011-05-05 11:49:20 +0000199void mpi_free( mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000200
201/**
202 * \brief Enlarge to the specified number of limbs
203 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000204 * \param X MPI to grow
205 * \param nblimbs The target number of limbs
206 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000207 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000208 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000209 */
Paul Bakker23986e52011-04-24 08:57:21 +0000210int mpi_grow( mpi *X, size_t nblimbs );
Paul Bakker5121ce52009-01-03 21:22:43 +0000211
212/**
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100213 * \brief Resize down, keeping at least the specified number of limbs
214 *
215 * \param X MPI to shrink
216 * \param nblimbs The minimum number of limbs to keep
217 *
218 * \return 0 if successful,
219 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
220 */
221int mpi_shrink( mpi *X, size_t nblimbs );
222
223/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000224 * \brief Copy the contents of Y into X
225 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000226 * \param X Destination MPI
227 * \param Y Source MPI
228 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000229 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000230 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000231 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000232int mpi_copy( mpi *X, const mpi *Y );
Paul Bakker5121ce52009-01-03 21:22:43 +0000233
234/**
235 * \brief Swap the contents of X and Y
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000236 *
237 * \param X First MPI value
238 * \param Y Second MPI value
Paul Bakker5121ce52009-01-03 21:22:43 +0000239 */
240void mpi_swap( mpi *X, mpi *Y );
241
242/**
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100243 * \brief Safe conditional assignement X = Y if assign is 1
244 *
245 * \param X MPI to conditionally assign to
246 * \param Y Value to be assigned
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100247 * \param assign 1: perform the assignment, 0: keep X's original value
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100248 *
249 * \return 0 if successful,
250 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100251 *
252 * \note This function is equivalent to
253 * if( assign ) mpi_copy( X, Y );
254 * except that it avoids leaking any information about whether
255 * the assignment was done or not (the above code may leak
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +0100256 * information through branch prediction and/or memory access
257 * patterns analysis).
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100258 */
Manuel Pégourié-Gonnard96c7a922013-11-25 18:28:53 +0100259int mpi_safe_cond_assign( mpi *X, const mpi *Y, unsigned char assign );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100260
261/**
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100262 * \brief Safe conditional swap X <-> Y if swap is 1
263 *
264 * \param X First mpi value
265 * \param Y Second mpi value
266 * \param assign 1: perform the swap, 0: keep X and Y's original values
267 *
268 * \return 0 if successful,
269 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
270 *
271 * \note This function is equivalent to
272 * if( assign ) mpi_swap( X, Y );
273 * except that it avoids leaking any information about whether
274 * the assignment was done or not (the above code may leak
275 * information through branch prediction and/or memory access
276 * patterns analysis).
277 */
278int mpi_safe_cond_swap( mpi *X, mpi *Y, unsigned char assign );
279
280/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000281 * \brief Set value from integer
282 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000283 * \param X MPI to set
284 * \param z Value to use
285 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000286 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000287 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000288 */
Paul Bakkera755ca12011-04-24 09:11:17 +0000289int mpi_lset( mpi *X, t_sint z );
Paul Bakker5121ce52009-01-03 21:22:43 +0000290
Paul Bakker9a736322012-11-14 12:39:52 +0000291/**
Paul Bakker2f5947e2011-05-18 15:47:11 +0000292 * \brief Get a specific bit from X
293 *
294 * \param X MPI to use
295 * \param pos Zero-based index of the bit in X
296 *
297 * \return Either a 0 or a 1
298 */
Paul Bakker6b906e52012-05-08 12:01:43 +0000299int mpi_get_bit( const mpi *X, size_t pos );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000300
Paul Bakker9a736322012-11-14 12:39:52 +0000301/**
Paul Bakker2f5947e2011-05-18 15:47:11 +0000302 * \brief Set a bit of X to a specific value of 0 or 1
303 *
304 * \note Will grow X if necessary to set a bit to 1 in a not yet
305 * existing limb. Will not grow if bit should be set to 0
306 *
307 * \param X MPI to use
308 * \param pos Zero-based index of the bit in X
309 * \param val The value to set the bit to (0 or 1)
310 *
311 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000312 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
Paul Bakker2f5947e2011-05-18 15:47:11 +0000313 * POLARSSL_ERR_MPI_BAD_INPUT_DATA if val is not 0 or 1
314 */
315int mpi_set_bit( mpi *X, size_t pos, unsigned char val );
316
Paul Bakker5121ce52009-01-03 21:22:43 +0000317/**
Paul Bakker6b906e52012-05-08 12:01:43 +0000318 * \brief Return the number of zero-bits before the least significant
319 * '1' bit
320 *
321 * Note: Thus also the zero-based index of the least significant '1' bit
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000322 *
323 * \param X MPI to use
Paul Bakker5121ce52009-01-03 21:22:43 +0000324 */
Paul Bakker23986e52011-04-24 08:57:21 +0000325size_t mpi_lsb( const mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000326
327/**
Paul Bakker6b906e52012-05-08 12:01:43 +0000328 * \brief Return the number of bits up to and including the most
329 * significant '1' bit'
330 *
331 * Note: Thus also the one-based index of the most significant '1' bit
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000332 *
333 * \param X MPI to use
Paul Bakker5121ce52009-01-03 21:22:43 +0000334 */
Paul Bakker23986e52011-04-24 08:57:21 +0000335size_t mpi_msb( const mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000336
337/**
338 * \brief Return the total size in bytes
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000339 *
340 * \param X MPI to use
Paul Bakker5121ce52009-01-03 21:22:43 +0000341 */
Paul Bakker23986e52011-04-24 08:57:21 +0000342size_t mpi_size( const mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000343
344/**
345 * \brief Import from an ASCII string
346 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000347 * \param X Destination MPI
348 * \param radix Input numeric base
349 * \param s Null-terminated string buffer
Paul Bakker5121ce52009-01-03 21:22:43 +0000350 *
Paul Bakkercb37aa52011-11-30 16:00:20 +0000351 * \return 0 if successful, or a POLARSSL_ERR_MPI_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000352 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000353int mpi_read_string( mpi *X, int radix, const char *s );
Paul Bakker5121ce52009-01-03 21:22:43 +0000354
355/**
356 * \brief Export into an ASCII string
357 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000358 * \param X Source MPI
359 * \param radix Output numeric base
360 * \param s String buffer
361 * \param slen String buffer size
Paul Bakker5121ce52009-01-03 21:22:43 +0000362 *
Paul Bakkercb37aa52011-11-30 16:00:20 +0000363 * \return 0 if successful, or a POLARSSL_ERR_MPI_XXX error code.
Paul Bakkerff60ee62010-03-16 21:09:09 +0000364 * *slen is always updated to reflect the amount
365 * of data that has (or would have) been written.
Paul Bakker5121ce52009-01-03 21:22:43 +0000366 *
367 * \note Call this function with *slen = 0 to obtain the
368 * minimum required buffer size in *slen.
369 */
Paul Bakker23986e52011-04-24 08:57:21 +0000370int mpi_write_string( const mpi *X, int radix, char *s, size_t *slen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000371
Paul Bakker2b6af2f2012-10-23 11:08:02 +0000372#if defined(POLARSSL_FS_IO)
Paul Bakker5121ce52009-01-03 21:22:43 +0000373/**
374 * \brief Read X from an opened file
375 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000376 * \param X Destination MPI
377 * \param radix Input numeric base
378 * \param fin Input file handle
Paul Bakker5121ce52009-01-03 21:22:43 +0000379 *
Paul Bakkercb37aa52011-11-30 16:00:20 +0000380 * \return 0 if successful, POLARSSL_ERR_MPI_BUFFER_TOO_SMALL if
381 * the file read buffer is too small or a
382 * POLARSSL_ERR_MPI_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000383 */
384int mpi_read_file( mpi *X, int radix, FILE *fin );
385
386/**
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000387 * \brief Write X into an opened file, or stdout if fout is NULL
Paul Bakker5121ce52009-01-03 21:22:43 +0000388 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000389 * \param p Prefix, can be NULL
390 * \param X Source MPI
391 * \param radix Output numeric base
392 * \param fout Output file handle (can be NULL)
Paul Bakker5121ce52009-01-03 21:22:43 +0000393 *
Paul Bakkercb37aa52011-11-30 16:00:20 +0000394 * \return 0 if successful, or a POLARSSL_ERR_MPI_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000395 *
396 * \note Set fout == NULL to print X on the console.
397 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000398int mpi_write_file( const char *p, const mpi *X, int radix, FILE *fout );
Paul Bakker2b6af2f2012-10-23 11:08:02 +0000399#endif /* POLARSSL_FS_IO */
Paul Bakker5121ce52009-01-03 21:22:43 +0000400
401/**
402 * \brief Import X from unsigned binary data, big endian
403 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000404 * \param X Destination MPI
405 * \param buf Input buffer
406 * \param buflen Input buffer size
Paul Bakker5121ce52009-01-03 21:22:43 +0000407 *
408 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000409 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000410 */
Paul Bakker23986e52011-04-24 08:57:21 +0000411int mpi_read_binary( mpi *X, const unsigned char *buf, size_t buflen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000412
413/**
Manuel Pégourié-Gonnard3926a2c2014-06-25 12:57:47 +0200414 * \brief Export X into unsigned binary data, big endian.
415 * Always fills the whole buffer, which will start with zeros
416 * if the number is smaller.
Paul Bakker5121ce52009-01-03 21:22:43 +0000417 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000418 * \param X Source MPI
419 * \param buf Output buffer
420 * \param buflen Output buffer size
Paul Bakker5121ce52009-01-03 21:22:43 +0000421 *
422 * \return 0 if successful,
Paul Bakker40e46942009-01-03 21:51:57 +0000423 * POLARSSL_ERR_MPI_BUFFER_TOO_SMALL if buf isn't large enough
Paul Bakker5121ce52009-01-03 21:22:43 +0000424 */
Paul Bakker23986e52011-04-24 08:57:21 +0000425int mpi_write_binary( const mpi *X, unsigned char *buf, size_t buflen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000426
427/**
428 * \brief Left-shift: X <<= count
429 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000430 * \param X MPI to shift
431 * \param count Amount to shift
432 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000433 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000434 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000435 */
Paul Bakker23986e52011-04-24 08:57:21 +0000436int mpi_shift_l( mpi *X, size_t count );
Paul Bakker5121ce52009-01-03 21:22:43 +0000437
438/**
439 * \brief Right-shift: X >>= count
440 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000441 * \param X MPI to shift
442 * \param count Amount to shift
443 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000444 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000445 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000446 */
Paul Bakker23986e52011-04-24 08:57:21 +0000447int mpi_shift_r( mpi *X, size_t count );
Paul Bakker5121ce52009-01-03 21:22:43 +0000448
449/**
450 * \brief Compare unsigned values
451 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000452 * \param X Left-hand MPI
453 * \param Y Right-hand MPI
454 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000455 * \return 1 if |X| is greater than |Y|,
456 * -1 if |X| is lesser than |Y| or
457 * 0 if |X| is equal to |Y|
458 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000459int mpi_cmp_abs( const mpi *X, const mpi *Y );
Paul Bakker5121ce52009-01-03 21:22:43 +0000460
461/**
462 * \brief Compare signed values
463 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000464 * \param X Left-hand MPI
465 * \param Y Right-hand MPI
466 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000467 * \return 1 if X is greater than Y,
468 * -1 if X is lesser than Y or
469 * 0 if X is equal to Y
470 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000471int mpi_cmp_mpi( const mpi *X, const mpi *Y );
Paul Bakker5121ce52009-01-03 21:22:43 +0000472
473/**
474 * \brief Compare signed values
475 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000476 * \param X Left-hand MPI
477 * \param z The integer value to compare to
478 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000479 * \return 1 if X is greater than z,
480 * -1 if X is lesser than z or
481 * 0 if X is equal to z
482 */
Paul Bakkera755ca12011-04-24 09:11:17 +0000483int mpi_cmp_int( const mpi *X, t_sint z );
Paul Bakker5121ce52009-01-03 21:22:43 +0000484
485/**
486 * \brief Unsigned addition: X = |A| + |B|
487 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000488 * \param X Destination MPI
489 * \param A Left-hand MPI
490 * \param B Right-hand MPI
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 Bakker5121ce52009-01-03 21:22:43 +0000494 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000495int mpi_add_abs( mpi *X, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000496
497/**
Paul Bakker60b1d102013-10-29 10:02:51 +0100498 * \brief Unsigned subtraction: X = |A| - |B|
Paul Bakker5121ce52009-01-03 21:22:43 +0000499 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000500 * \param X Destination MPI
501 * \param A Left-hand MPI
502 * \param B Right-hand MPI
503 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000504 * \return 0 if successful,
Paul Bakker40e46942009-01-03 21:51:57 +0000505 * POLARSSL_ERR_MPI_NEGATIVE_VALUE if B is greater than A
Paul Bakker5121ce52009-01-03 21:22:43 +0000506 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000507int mpi_sub_abs( mpi *X, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000508
509/**
510 * \brief Signed addition: X = A + B
511 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000512 * \param X Destination MPI
513 * \param A Left-hand MPI
514 * \param B Right-hand MPI
515 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000516 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000517 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000518 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000519int mpi_add_mpi( mpi *X, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000520
521/**
Paul Bakker60b1d102013-10-29 10:02:51 +0100522 * \brief Signed subtraction: X = A - B
Paul Bakker5121ce52009-01-03 21:22:43 +0000523 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000524 * \param X Destination MPI
525 * \param A Left-hand MPI
526 * \param B Right-hand MPI
527 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000528 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000529 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000530 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000531int mpi_sub_mpi( mpi *X, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000532
533/**
534 * \brief Signed addition: X = A + b
535 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000536 * \param X Destination MPI
537 * \param A Left-hand MPI
538 * \param b The integer value to add
539 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000540 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000541 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000542 */
Paul Bakkera755ca12011-04-24 09:11:17 +0000543int mpi_add_int( mpi *X, const mpi *A, t_sint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000544
545/**
Paul Bakker60b1d102013-10-29 10:02:51 +0100546 * \brief Signed subtraction: X = A - b
Paul Bakker5121ce52009-01-03 21:22:43 +0000547 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000548 * \param X Destination MPI
549 * \param A Left-hand MPI
550 * \param b The integer value to subtract
551 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000552 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000553 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000554 */
Paul Bakkera755ca12011-04-24 09:11:17 +0000555int mpi_sub_int( mpi *X, const mpi *A, t_sint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000556
557/**
558 * \brief Baseline multiplication: X = A * B
559 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000560 * \param X Destination MPI
561 * \param A Left-hand MPI
562 * \param B Right-hand MPI
563 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000564 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000565 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000566 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000567int mpi_mul_mpi( mpi *X, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000568
569/**
570 * \brief Baseline multiplication: X = A * b
Manuel Pégourié-Gonnard3eaa8e72013-11-25 16:16:33 +0100571 * Note: despite the functon signature, b is treated as a
572 * t_uint. Negative values of b are treated as large positive
573 * values.
Paul Bakker5121ce52009-01-03 21:22:43 +0000574 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000575 * \param X Destination MPI
576 * \param A Left-hand MPI
577 * \param b The integer value to multiply with
578 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000579 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000580 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000581 */
Paul Bakkera755ca12011-04-24 09:11:17 +0000582int mpi_mul_int( mpi *X, const mpi *A, t_sint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000583
584/**
585 * \brief Division by mpi: A = Q * B + R
586 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000587 * \param Q Destination MPI for the quotient
588 * \param R Destination MPI for the rest value
589 * \param A Left-hand MPI
590 * \param B Right-hand MPI
591 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000592 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000593 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000594 * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if B == 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000595 *
596 * \note Either Q or R can be NULL.
597 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000598int mpi_div_mpi( mpi *Q, mpi *R, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000599
600/**
601 * \brief Division by int: A = Q * b + R
602 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000603 * \param Q Destination MPI for the quotient
604 * \param R Destination MPI for the rest value
605 * \param A Left-hand MPI
606 * \param b Integer to divide by
607 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000608 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000609 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000610 * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if b == 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000611 *
612 * \note Either Q or R can be NULL.
613 */
Paul Bakkera755ca12011-04-24 09:11:17 +0000614int mpi_div_int( mpi *Q, mpi *R, const mpi *A, t_sint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000615
616/**
617 * \brief Modulo: R = A mod B
618 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000619 * \param R Destination MPI for the rest value
620 * \param A Left-hand MPI
621 * \param B Right-hand MPI
622 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000623 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000624 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
Paul Bakkerce40a6d2009-06-23 19:46:08 +0000625 * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if B == 0,
626 * POLARSSL_ERR_MPI_NEGATIVE_VALUE if B < 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000627 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000628int mpi_mod_mpi( mpi *R, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000629
630/**
631 * \brief Modulo: r = A mod b
632 *
Paul Bakkera755ca12011-04-24 09:11:17 +0000633 * \param r Destination t_uint
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000634 * \param A Left-hand MPI
635 * \param b Integer to divide by
636 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000637 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000638 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
Paul Bakkerce40a6d2009-06-23 19:46:08 +0000639 * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if b == 0,
640 * POLARSSL_ERR_MPI_NEGATIVE_VALUE if b < 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000641 */
Paul Bakkera755ca12011-04-24 09:11:17 +0000642int mpi_mod_int( t_uint *r, const mpi *A, t_sint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000643
644/**
645 * \brief Sliding-window exponentiation: X = A^E mod N
646 *
Paul Bakker9af723c2014-05-01 13:03:14 +0200647 * \param X Destination MPI
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000648 * \param A Left-hand MPI
649 * \param E Exponent MPI
650 * \param N Modular MPI
651 * \param _RR Speed-up MPI used for recalculations
652 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000653 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000654 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200655 * POLARSSL_ERR_MPI_BAD_INPUT_DATA if N is negative or even or
656 * if E is negative
Paul Bakker5121ce52009-01-03 21:22:43 +0000657 *
658 * \note _RR is used to avoid re-computing R*R mod N across
659 * multiple calls, which speeds up things a bit. It can
660 * be set to NULL if the extra performance is unneeded.
661 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000662int mpi_exp_mod( mpi *X, const mpi *A, const mpi *E, const mpi *N, mpi *_RR );
Paul Bakker5121ce52009-01-03 21:22:43 +0000663
664/**
Paul Bakker287781a2011-03-26 13:18:49 +0000665 * \brief Fill an MPI X with size bytes of random
666 *
667 * \param X Destination MPI
668 * \param size Size in bytes
669 * \param f_rng RNG function
670 * \param p_rng RNG parameter
671 *
672 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000673 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker287781a2011-03-26 13:18:49 +0000674 */
Paul Bakkera3d195c2011-11-27 21:07:34 +0000675int mpi_fill_random( mpi *X, size_t size,
676 int (*f_rng)(void *, unsigned char *, size_t),
677 void *p_rng );
Paul Bakker287781a2011-03-26 13:18:49 +0000678
679/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000680 * \brief Greatest common divisor: G = gcd(A, B)
681 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000682 * \param G Destination MPI
683 * \param A Left-hand MPI
684 * \param B Right-hand MPI
685 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000686 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000687 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000688 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000689int mpi_gcd( mpi *G, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000690
691/**
692 * \brief Modular inverse: X = A^-1 mod N
693 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000694 * \param X Destination MPI
695 * \param A Left-hand MPI
696 * \param N Right-hand MPI
697 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000698 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000699 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000700 * POLARSSL_ERR_MPI_BAD_INPUT_DATA if N is negative or nil
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000701 POLARSSL_ERR_MPI_NOT_ACCEPTABLE if A has no inverse mod N
Paul Bakker5121ce52009-01-03 21:22:43 +0000702 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000703int mpi_inv_mod( mpi *X, const mpi *A, const mpi *N );
Paul Bakker5121ce52009-01-03 21:22:43 +0000704
705/**
706 * \brief Miller-Rabin primality test
707 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000708 * \param X MPI to check
709 * \param f_rng RNG function
710 * \param p_rng RNG parameter
711 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000712 * \return 0 if successful (probably prime),
Paul Bakker69e095c2011-12-10 21:55:01 +0000713 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000714 * POLARSSL_ERR_MPI_NOT_ACCEPTABLE if X is not prime
Paul Bakker5121ce52009-01-03 21:22:43 +0000715 */
Paul Bakker45f457d2013-11-25 14:26:52 +0100716int mpi_is_prime( mpi *X,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000717 int (*f_rng)(void *, unsigned char *, size_t),
718 void *p_rng );
Paul Bakker5121ce52009-01-03 21:22:43 +0000719
720/**
721 * \brief Prime number generation
722 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000723 * \param X Destination MPI
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200724 * \param nbits Required size of X in bits
725 * ( 3 <= nbits <= POLARSSL_MPI_MAX_BITS )
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000726 * \param dh_flag If 1, then (X-1)/2 will be prime too
Paul Bakker5121ce52009-01-03 21:22:43 +0000727 * \param f_rng RNG function
728 * \param p_rng RNG parameter
729 *
730 * \return 0 if successful (probably prime),
Paul Bakker69e095c2011-12-10 21:55:01 +0000731 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000732 * POLARSSL_ERR_MPI_BAD_INPUT_DATA if nbits is < 3
Paul Bakker5121ce52009-01-03 21:22:43 +0000733 */
Paul Bakker23986e52011-04-24 08:57:21 +0000734int mpi_gen_prime( mpi *X, size_t nbits, int dh_flag,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000735 int (*f_rng)(void *, unsigned char *, size_t),
736 void *p_rng );
Paul Bakker5121ce52009-01-03 21:22:43 +0000737
738/**
739 * \brief Checkup routine
740 *
741 * \return 0 if successful, or 1 if the test failed
742 */
743int mpi_self_test( int verbose );
744
745#ifdef __cplusplus
746}
747#endif
748
749#endif /* bignum.h */