blob: 3b55151a0590b6574d35b25c78284bef50041d8b [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é-Gonnardfe446432015-03-06 13:17:10 +00008 * This file is part of mbed TLS (https://tls.mbed.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
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020027#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakkercf0360a2012-01-20 10:08:14 +000028#include "config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020029#else
30#include POLARSSL_CONFIG_FILE
31#endif
Paul Bakkercf0360a2012-01-20 10:08:14 +000032
Rich Evans00ab4702015-02-06 13:43:58 +000033#include <stddef.h>
34
Manuel Pégourié-Gonnarde94e6e52015-01-19 15:01:53 +000035#if defined(POLARSSL_FS_IO)
36#include <stdio.h>
37#endif
38
Paul Bakkerfa6a6202013-10-28 18:48:30 +010039#if defined(_MSC_VER) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker5c2364c2012-10-01 14:41:15 +000040#include <basetsd.h>
Paul Bakker4a2bd0d2012-11-02 11:06:08 +000041#if (_MSC_VER <= 1200)
42typedef signed short int16_t;
43typedef unsigned short uint16_t;
44#else
Paul Bakker5c2364c2012-10-01 14:41:15 +000045typedef INT16 int16_t;
46typedef UINT16 uint16_t;
Paul Bakker4a2bd0d2012-11-02 11:06:08 +000047#endif
Paul Bakker9ef6e2b2012-10-01 20:57:38 +000048typedef INT32 int32_t;
Paul Bakker8ea31ff2013-02-27 15:02:50 +010049typedef INT64 int64_t;
Paul Bakker5c2364c2012-10-01 14:41:15 +000050typedef UINT32 uint32_t;
51typedef UINT64 uint64_t;
52#else
53#include <inttypes.h>
Paul Bakker9af723c2014-05-01 13:03:14 +020054#endif /* _MSC_VER && !EFIX64 && !EFI32 */
Paul Bakker5c2364c2012-10-01 14:41:15 +000055
Paul Bakker9d781402011-05-09 16:17:09 +000056#define POLARSSL_ERR_MPI_FILE_IO_ERROR -0x0002 /**< An error occurred while reading from or writing to a file. */
57#define POLARSSL_ERR_MPI_BAD_INPUT_DATA -0x0004 /**< Bad input parameters to function. */
58#define POLARSSL_ERR_MPI_INVALID_CHARACTER -0x0006 /**< There is an invalid character in the digit string. */
Paul Bakker69e095c2011-12-10 21:55:01 +000059#define POLARSSL_ERR_MPI_BUFFER_TOO_SMALL -0x0008 /**< The buffer is too small to write to. */
Paul Bakker9d781402011-05-09 16:17:09 +000060#define POLARSSL_ERR_MPI_NEGATIVE_VALUE -0x000A /**< The input arguments are negative or result in illegal output. */
61#define POLARSSL_ERR_MPI_DIVISION_BY_ZERO -0x000C /**< The input argument for division is zero, which is not allowed. */
62#define POLARSSL_ERR_MPI_NOT_ACCEPTABLE -0x000E /**< The input arguments are not acceptable. */
Paul Bakker69e095c2011-12-10 21:55:01 +000063#define POLARSSL_ERR_MPI_MALLOC_FAILED -0x0010 /**< Memory allocation failed. */
Paul Bakker5121ce52009-01-03 21:22:43 +000064
Manuel Pégourié-Gonnard0160eac2013-11-22 17:54:59 +010065#define MPI_CHK(f) do { if( ( ret = f ) != 0 ) goto cleanup; } while( 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000066
67/*
Paul Bakkerf9688572011-05-05 10:00:45 +000068 * Maximum size MPIs are allowed to grow to in number of limbs.
69 */
70#define POLARSSL_MPI_MAX_LIMBS 10000
71
Paul Bakker770268f2014-05-05 11:40:14 +020072#if !defined(POLARSSL_MPI_WINDOW_SIZE)
Paul Bakkerf9688572011-05-05 10:00:45 +000073/*
Paul Bakkerb6d5f082011-11-25 11:52:11 +000074 * Maximum window size used for modular exponentiation. Default: 6
75 * Minimum value: 1. Maximum value: 6.
76 *
77 * Result is an array of ( 2 << POLARSSL_MPI_WINDOW_SIZE ) MPIs used
78 * for the sliding window calculation. (So 64 by default)
79 *
80 * Reduction in size, reduces speed.
81 */
82#define POLARSSL_MPI_WINDOW_SIZE 6 /**< Maximum windows size used. */
Paul Bakker770268f2014-05-05 11:40:14 +020083#endif /* !POLARSSL_MPI_WINDOW_SIZE */
Paul Bakkerb6d5f082011-11-25 11:52:11 +000084
Paul Bakker770268f2014-05-05 11:40:14 +020085#if !defined(POLARSSL_MPI_MAX_SIZE)
Paul Bakkerb6d5f082011-11-25 11:52:11 +000086/*
Paul Bakkerfe3256e2011-11-25 12:11:43 +000087 * Maximum size of MPIs allowed in bits and bytes for user-MPIs.
Paul Bakkerf626e1d2013-01-21 12:10:00 +010088 * ( Default: 512 bytes => 4096 bits, Maximum tested: 2048 bytes => 16384 bits )
Paul Bakkerfe3256e2011-11-25 12:11:43 +000089 *
90 * Note: Calculations can results temporarily in larger MPIs. So the number
91 * of limbs required (POLARSSL_MPI_MAX_LIMBS) is higher.
92 */
Manuel Pégourié-Gonnardda1b4de2014-09-08 17:03:50 +020093#define POLARSSL_MPI_MAX_SIZE 1024 /**< Maximum number of bytes for usable MPIs. */
Paul Bakker770268f2014-05-05 11:40:14 +020094#endif /* !POLARSSL_MPI_MAX_SIZE */
Paul Bakker9bcf16c2013-06-24 19:31:17 +020095
Paul Bakkerfe3256e2011-11-25 12:11:43 +000096#define POLARSSL_MPI_MAX_BITS ( 8 * POLARSSL_MPI_MAX_SIZE ) /**< Maximum number of bits for usable MPIs. */
97
98/*
Paul Bakker5531c6d2012-09-26 19:20:46 +000099 * When reading from files with mpi_read_file() and writing to files with
100 * mpi_write_file() the buffer should have space
Paul Bakkercb37aa52011-11-30 16:00:20 +0000101 * for a (short) label, the MPI (in the provided radix), the newline
102 * characters and the '\0'.
103 *
104 * By default we assume at least a 10 char label, a minimum radix of 10
105 * (decimal) and a maximum of 4096 bit numbers (1234 decimal chars).
Paul Bakkerf9183102012-09-27 20:42:35 +0000106 * Autosized at compile time for at least a 10 char label, a minimum radix
107 * of 10 (decimal) for a number of POLARSSL_MPI_MAX_BITS size.
108 *
109 * This used to be statically sized to 1250 for a maximum of 4096 bit
110 * numbers (1234 decimal chars).
111 *
112 * Calculate using the formula:
113 * POLARSSL_MPI_RW_BUFFER_SIZE = ceil(POLARSSL_MPI_MAX_BITS / ln(10) * ln(2)) +
114 * LabelSize + 6
Paul Bakkercb37aa52011-11-30 16:00:20 +0000115 */
Paul Bakkerf9183102012-09-27 20:42:35 +0000116#define POLARSSL_MPI_MAX_BITS_SCALE100 ( 100 * POLARSSL_MPI_MAX_BITS )
117#define LN_2_DIV_LN_10_SCALE100 332
118#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 +0000119
120/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000121 * Define the base integer type, architecture-wise
122 */
Paul Bakker40e46942009-01-03 21:51:57 +0000123#if defined(POLARSSL_HAVE_INT8)
Paul Bakkera755ca12011-04-24 09:11:17 +0000124typedef signed char t_sint;
125typedef unsigned char t_uint;
Paul Bakker5c2364c2012-10-01 14:41:15 +0000126typedef uint16_t t_udbl;
Paul Bakker62261d62012-10-02 12:19:31 +0000127#define POLARSSL_HAVE_UDBL
Paul Bakker5121ce52009-01-03 21:22:43 +0000128#else
Paul Bakker40e46942009-01-03 21:51:57 +0000129#if defined(POLARSSL_HAVE_INT16)
Paul Bakker5c2364c2012-10-01 14:41:15 +0000130typedef int16_t t_sint;
131typedef uint16_t t_uint;
132typedef uint32_t t_udbl;
Paul Bakker62261d62012-10-02 12:19:31 +0000133#define POLARSSL_HAVE_UDBL
Paul Bakker5121ce52009-01-03 21:22:43 +0000134#else
Manuel Pégourié-Gonnard96eed7b2013-12-12 15:49:10 +0100135 /*
136 * 32-bit integers can be forced on 64-bit arches (eg. for testing purposes)
Barry K. Nathan79e69f92014-05-05 18:08:57 -0700137 * by defining POLARSSL_HAVE_INT32 and undefining POLARSSL_HAVE_ASM
Manuel Pégourié-Gonnard96eed7b2013-12-12 15:49:10 +0100138 */
139 #if ( ! defined(POLARSSL_HAVE_INT32) && \
140 defined(_MSC_VER) && defined(_M_AMD64) )
Manuel Pégourié-Gonnard5779cbe2013-10-23 20:17:00 +0200141 #define POLARSSL_HAVE_INT64
Paul Bakker62261d62012-10-02 12:19:31 +0000142 typedef int64_t t_sint;
143 typedef uint64_t t_uint;
144 #else
Manuel Pégourié-Gonnard96eed7b2013-12-12 15:49:10 +0100145 #if ( ! defined(POLARSSL_HAVE_INT32) && \
146 defined(__GNUC__) && ( \
Paul Bakker62261d62012-10-02 12:19:31 +0000147 defined(__amd64__) || defined(__x86_64__) || \
148 defined(__ppc64__) || defined(__powerpc64__) || \
149 defined(__ia64__) || defined(__alpha__) || \
150 (defined(__sparc__) && defined(__arch64__)) || \
James Cowgilld1e7e8b2014-12-16 15:24:06 +0000151 defined(__s390x__) || defined(__mips64) ) )
Manuel Pégourié-Gonnard5779cbe2013-10-23 20:17:00 +0200152 #define POLARSSL_HAVE_INT64
Paul Bakker62261d62012-10-02 12:19:31 +0000153 typedef int64_t t_sint;
154 typedef uint64_t t_uint;
Simon Butcherd7fe6fb2016-01-03 22:39:18 +0000155 /* mbedtls_t_udbl defined as 128-bit unsigned int */
Paul Bakker62261d62012-10-02 12:19:31 +0000156 typedef unsigned int t_udbl __attribute__((mode(TI)));
157 #define POLARSSL_HAVE_UDBL
158 #else
Manuel Pégourié-Gonnarda47e7052013-10-21 17:51:45 +0200159 #define POLARSSL_HAVE_INT32
Paul Bakker62261d62012-10-02 12:19:31 +0000160 typedef int32_t t_sint;
161 typedef uint32_t t_uint;
162 #if ( defined(_MSC_VER) && defined(_M_IX86) )
163 typedef uint64_t t_udbl;
164 #define POLARSSL_HAVE_UDBL
165 #else
166 #if defined( POLARSSL_HAVE_LONGLONG )
167 typedef unsigned long long t_udbl;
168 #define POLARSSL_HAVE_UDBL
169 #endif
170 #endif
Paul Bakkerdb20c102014-06-17 14:34:44 +0200171 #endif /* !POLARSSL_HAVE_INT32 && __GNUC__ && 64-bit platform */
172 #endif /* !POLARSSL_HAVE_INT32 && _MSC_VER && _M_AMD64 */
Paul Bakker5c2364c2012-10-01 14:41:15 +0000173#endif /* POLARSSL_HAVE_INT16 */
174#endif /* POLARSSL_HAVE_INT8 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000175
Paul Bakker407a0da2013-06-27 14:29:21 +0200176#ifdef __cplusplus
177extern "C" {
178#endif
179
Paul Bakker5121ce52009-01-03 21:22:43 +0000180/**
181 * \brief MPI structure
182 */
183typedef struct
184{
185 int s; /*!< integer sign */
Paul Bakker23986e52011-04-24 08:57:21 +0000186 size_t n; /*!< total # of limbs */
Paul Bakkera755ca12011-04-24 09:11:17 +0000187 t_uint *p; /*!< pointer to limbs */
Paul Bakker5121ce52009-01-03 21:22:43 +0000188}
189mpi;
190
Paul Bakker5121ce52009-01-03 21:22:43 +0000191/**
Manuel Pégourié-Gonnard770b5e12015-04-29 17:02:01 +0200192 * \brief Initialize one MPI (make internal references valid)
193 * This just makes it ready to be set or freed,
194 * but does not define a value for the MPI.
Paul Bakker6c591fa2011-05-05 11:49:20 +0000195 *
196 * \param X One MPI to initialize.
Paul Bakker5121ce52009-01-03 21:22:43 +0000197 */
Paul Bakker6c591fa2011-05-05 11:49:20 +0000198void mpi_init( mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000199
200/**
Paul Bakker6c591fa2011-05-05 11:49:20 +0000201 * \brief Unallocate one MPI
202 *
203 * \param X One MPI to unallocate.
Paul Bakker5121ce52009-01-03 21:22:43 +0000204 */
Paul Bakker6c591fa2011-05-05 11:49:20 +0000205void mpi_free( mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000206
207/**
208 * \brief Enlarge to the specified number of limbs
209 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000210 * \param X MPI to grow
211 * \param nblimbs The target number of limbs
212 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000213 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000214 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000215 */
Paul Bakker23986e52011-04-24 08:57:21 +0000216int mpi_grow( mpi *X, size_t nblimbs );
Paul Bakker5121ce52009-01-03 21:22:43 +0000217
218/**
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100219 * \brief Resize down, keeping at least the specified number of limbs
220 *
221 * \param X MPI to shrink
222 * \param nblimbs The minimum number of limbs to keep
223 *
224 * \return 0 if successful,
225 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
226 */
227int mpi_shrink( mpi *X, size_t nblimbs );
228
229/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000230 * \brief Copy the contents of Y into X
231 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000232 * \param X Destination MPI
233 * \param Y Source MPI
234 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000235 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000236 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000237 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000238int mpi_copy( mpi *X, const mpi *Y );
Paul Bakker5121ce52009-01-03 21:22:43 +0000239
240/**
241 * \brief Swap the contents of X and Y
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000242 *
243 * \param X First MPI value
244 * \param Y Second MPI value
Paul Bakker5121ce52009-01-03 21:22:43 +0000245 */
246void mpi_swap( mpi *X, mpi *Y );
247
248/**
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100249 * \brief Safe conditional assignement X = Y if assign is 1
250 *
251 * \param X MPI to conditionally assign to
252 * \param Y Value to be assigned
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100253 * \param assign 1: perform the assignment, 0: keep X's original value
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100254 *
255 * \return 0 if successful,
256 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100257 *
258 * \note This function is equivalent to
259 * if( assign ) mpi_copy( X, Y );
260 * except that it avoids leaking any information about whether
261 * the assignment was done or not (the above code may leak
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +0100262 * information through branch prediction and/or memory access
263 * patterns analysis).
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100264 */
Manuel Pégourié-Gonnard96c7a922013-11-25 18:28:53 +0100265int mpi_safe_cond_assign( mpi *X, const mpi *Y, unsigned char assign );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100266
267/**
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100268 * \brief Safe conditional swap X <-> Y if swap is 1
269 *
270 * \param X First mpi value
271 * \param Y Second mpi value
272 * \param assign 1: perform the swap, 0: keep X and Y's original values
273 *
274 * \return 0 if successful,
275 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
276 *
277 * \note This function is equivalent to
278 * if( assign ) mpi_swap( X, Y );
279 * except that it avoids leaking any information about whether
280 * the assignment was done or not (the above code may leak
281 * information through branch prediction and/or memory access
282 * patterns analysis).
283 */
284int mpi_safe_cond_swap( mpi *X, mpi *Y, unsigned char assign );
285
286/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000287 * \brief Set value from integer
288 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000289 * \param X MPI to set
290 * \param z Value to use
291 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000292 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000293 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000294 */
Paul Bakkera755ca12011-04-24 09:11:17 +0000295int mpi_lset( mpi *X, t_sint z );
Paul Bakker5121ce52009-01-03 21:22:43 +0000296
Paul Bakker9a736322012-11-14 12:39:52 +0000297/**
Paul Bakker2f5947e2011-05-18 15:47:11 +0000298 * \brief Get a specific bit from X
299 *
300 * \param X MPI to use
301 * \param pos Zero-based index of the bit in X
302 *
303 * \return Either a 0 or a 1
304 */
Paul Bakker6b906e52012-05-08 12:01:43 +0000305int mpi_get_bit( const mpi *X, size_t pos );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000306
Paul Bakker9a736322012-11-14 12:39:52 +0000307/**
Paul Bakker2f5947e2011-05-18 15:47:11 +0000308 * \brief Set a bit of X to a specific value of 0 or 1
309 *
310 * \note Will grow X if necessary to set a bit to 1 in a not yet
311 * existing limb. Will not grow if bit should be set to 0
312 *
313 * \param X MPI to use
314 * \param pos Zero-based index of the bit in X
315 * \param val The value to set the bit to (0 or 1)
316 *
317 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000318 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
Paul Bakker2f5947e2011-05-18 15:47:11 +0000319 * POLARSSL_ERR_MPI_BAD_INPUT_DATA if val is not 0 or 1
320 */
321int mpi_set_bit( mpi *X, size_t pos, unsigned char val );
322
Paul Bakker5121ce52009-01-03 21:22:43 +0000323/**
Paul Bakker6b906e52012-05-08 12:01:43 +0000324 * \brief Return the number of zero-bits before the least significant
325 * '1' bit
326 *
327 * Note: Thus also the zero-based index of the least significant '1' bit
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000328 *
329 * \param X MPI to use
Paul Bakker5121ce52009-01-03 21:22:43 +0000330 */
Paul Bakker23986e52011-04-24 08:57:21 +0000331size_t mpi_lsb( const mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000332
333/**
Paul Bakker6b906e52012-05-08 12:01:43 +0000334 * \brief Return the number of bits up to and including the most
335 * significant '1' bit'
336 *
337 * Note: Thus also the one-based index of the most significant '1' bit
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000338 *
339 * \param X MPI to use
Paul Bakker5121ce52009-01-03 21:22:43 +0000340 */
Paul Bakker23986e52011-04-24 08:57:21 +0000341size_t mpi_msb( const mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000342
343/**
344 * \brief Return the total size in bytes
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000345 *
346 * \param X MPI to use
Paul Bakker5121ce52009-01-03 21:22:43 +0000347 */
Paul Bakker23986e52011-04-24 08:57:21 +0000348size_t mpi_size( const mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000349
350/**
351 * \brief Import from an ASCII string
352 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000353 * \param X Destination MPI
354 * \param radix Input numeric base
355 * \param s Null-terminated string buffer
Paul Bakker5121ce52009-01-03 21:22:43 +0000356 *
Paul Bakkercb37aa52011-11-30 16:00:20 +0000357 * \return 0 if successful, or a POLARSSL_ERR_MPI_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000358 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000359int mpi_read_string( mpi *X, int radix, const char *s );
Paul Bakker5121ce52009-01-03 21:22:43 +0000360
361/**
362 * \brief Export into an ASCII string
363 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000364 * \param X Source MPI
365 * \param radix Output numeric base
366 * \param s String buffer
367 * \param slen String buffer size
Paul Bakker5121ce52009-01-03 21:22:43 +0000368 *
Paul Bakkercb37aa52011-11-30 16:00:20 +0000369 * \return 0 if successful, or a POLARSSL_ERR_MPI_XXX error code.
Paul Bakkerff60ee62010-03-16 21:09:09 +0000370 * *slen is always updated to reflect the amount
371 * of data that has (or would have) been written.
Paul Bakker5121ce52009-01-03 21:22:43 +0000372 *
373 * \note Call this function with *slen = 0 to obtain the
374 * minimum required buffer size in *slen.
375 */
Paul Bakker23986e52011-04-24 08:57:21 +0000376int mpi_write_string( const mpi *X, int radix, char *s, size_t *slen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000377
Paul Bakker2b6af2f2012-10-23 11:08:02 +0000378#if defined(POLARSSL_FS_IO)
Paul Bakker5121ce52009-01-03 21:22:43 +0000379/**
Hanno Becker8c7698b2017-05-12 07:26:01 +0100380 * \brief Read MPI from a line in an opened file
Paul Bakker5121ce52009-01-03 21:22:43 +0000381 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000382 * \param X Destination MPI
383 * \param radix Input numeric base
384 * \param fin Input file handle
Paul Bakker5121ce52009-01-03 21:22:43 +0000385 *
Paul Bakkercb37aa52011-11-30 16:00:20 +0000386 * \return 0 if successful, POLARSSL_ERR_MPI_BUFFER_TOO_SMALL if
387 * the file read buffer is too small or a
388 * POLARSSL_ERR_MPI_XXX error code
Hanno Becker8c7698b2017-05-12 07:26:01 +0100389 *
390 * \note On success, this function advances the file stream
391 * to the end of the current line or to EOF.
392 *
393 * The function returns 0 on an empty line.
394 *
395 * Leading whitespaces are ignored, as is a
396 * '0x' prefix for radix 16.
Paul Bakker5121ce52009-01-03 21:22:43 +0000397 */
398int mpi_read_file( mpi *X, int radix, FILE *fin );
399
400/**
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000401 * \brief Write X into an opened file, or stdout if fout is NULL
Paul Bakker5121ce52009-01-03 21:22:43 +0000402 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000403 * \param p Prefix, can be NULL
404 * \param X Source MPI
405 * \param radix Output numeric base
406 * \param fout Output file handle (can be NULL)
Paul Bakker5121ce52009-01-03 21:22:43 +0000407 *
Paul Bakkercb37aa52011-11-30 16:00:20 +0000408 * \return 0 if successful, or a POLARSSL_ERR_MPI_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000409 *
410 * \note Set fout == NULL to print X on the console.
411 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000412int mpi_write_file( const char *p, const mpi *X, int radix, FILE *fout );
Paul Bakker2b6af2f2012-10-23 11:08:02 +0000413#endif /* POLARSSL_FS_IO */
Paul Bakker5121ce52009-01-03 21:22:43 +0000414
415/**
416 * \brief Import X from unsigned binary data, big endian
417 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000418 * \param X Destination MPI
419 * \param buf Input buffer
420 * \param buflen Input buffer size
Paul Bakker5121ce52009-01-03 21:22:43 +0000421 *
422 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000423 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000424 */
Paul Bakker23986e52011-04-24 08:57:21 +0000425int mpi_read_binary( mpi *X, const unsigned char *buf, size_t buflen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000426
427/**
Manuel Pégourié-Gonnard3926a2c2014-06-25 12:57:47 +0200428 * \brief Export X into unsigned binary data, big endian.
429 * Always fills the whole buffer, which will start with zeros
430 * if the number is smaller.
Paul Bakker5121ce52009-01-03 21:22:43 +0000431 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000432 * \param X Source MPI
433 * \param buf Output buffer
434 * \param buflen Output buffer size
Paul Bakker5121ce52009-01-03 21:22:43 +0000435 *
436 * \return 0 if successful,
Paul Bakker40e46942009-01-03 21:51:57 +0000437 * POLARSSL_ERR_MPI_BUFFER_TOO_SMALL if buf isn't large enough
Paul Bakker5121ce52009-01-03 21:22:43 +0000438 */
Paul Bakker23986e52011-04-24 08:57:21 +0000439int mpi_write_binary( const mpi *X, unsigned char *buf, size_t buflen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000440
441/**
442 * \brief Left-shift: X <<= count
443 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000444 * \param X MPI to shift
445 * \param count Amount to shift
446 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000447 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000448 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000449 */
Paul Bakker23986e52011-04-24 08:57:21 +0000450int mpi_shift_l( mpi *X, size_t count );
Paul Bakker5121ce52009-01-03 21:22:43 +0000451
452/**
453 * \brief Right-shift: X >>= count
454 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000455 * \param X MPI to shift
456 * \param count Amount to shift
457 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000458 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000459 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000460 */
Paul Bakker23986e52011-04-24 08:57:21 +0000461int mpi_shift_r( mpi *X, size_t count );
Paul Bakker5121ce52009-01-03 21:22:43 +0000462
463/**
464 * \brief Compare unsigned values
465 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000466 * \param X Left-hand MPI
467 * \param Y Right-hand MPI
468 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000469 * \return 1 if |X| is greater than |Y|,
470 * -1 if |X| is lesser than |Y| or
471 * 0 if |X| is equal to |Y|
472 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000473int mpi_cmp_abs( const mpi *X, const mpi *Y );
Paul Bakker5121ce52009-01-03 21:22:43 +0000474
475/**
476 * \brief Compare signed values
477 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000478 * \param X Left-hand MPI
479 * \param Y Right-hand MPI
480 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000481 * \return 1 if X is greater than Y,
482 * -1 if X is lesser than Y or
483 * 0 if X is equal to Y
484 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000485int mpi_cmp_mpi( const mpi *X, const mpi *Y );
Paul Bakker5121ce52009-01-03 21:22:43 +0000486
487/**
488 * \brief Compare signed values
489 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000490 * \param X Left-hand MPI
491 * \param z The integer value to compare to
492 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000493 * \return 1 if X is greater than z,
494 * -1 if X is lesser than z or
495 * 0 if X is equal to z
496 */
Paul Bakkera755ca12011-04-24 09:11:17 +0000497int mpi_cmp_int( const mpi *X, t_sint z );
Paul Bakker5121ce52009-01-03 21:22:43 +0000498
499/**
500 * \brief Unsigned addition: X = |A| + |B|
501 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000502 * \param X Destination MPI
503 * \param A Left-hand MPI
504 * \param B Right-hand MPI
505 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000506 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000507 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000508 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000509int mpi_add_abs( mpi *X, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000510
511/**
Paul Bakker60b1d102013-10-29 10:02:51 +0100512 * \brief Unsigned subtraction: X = |A| - |B|
Paul Bakker5121ce52009-01-03 21:22:43 +0000513 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000514 * \param X Destination MPI
515 * \param A Left-hand MPI
516 * \param B Right-hand MPI
517 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000518 * \return 0 if successful,
Paul Bakker40e46942009-01-03 21:51:57 +0000519 * POLARSSL_ERR_MPI_NEGATIVE_VALUE if B is greater than A
Paul Bakker5121ce52009-01-03 21:22:43 +0000520 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000521int mpi_sub_abs( mpi *X, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000522
523/**
524 * \brief Signed addition: X = A + B
525 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000526 * \param X Destination MPI
527 * \param A Left-hand MPI
528 * \param B Right-hand MPI
529 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000530 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000531 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000532 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000533int mpi_add_mpi( mpi *X, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000534
535/**
Paul Bakker60b1d102013-10-29 10:02:51 +0100536 * \brief Signed subtraction: X = A - B
Paul Bakker5121ce52009-01-03 21:22:43 +0000537 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000538 * \param X Destination MPI
539 * \param A Left-hand MPI
540 * \param B Right-hand MPI
541 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000542 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000543 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000544 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000545int mpi_sub_mpi( mpi *X, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000546
547/**
548 * \brief Signed addition: X = A + b
549 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000550 * \param X Destination MPI
551 * \param A Left-hand MPI
552 * \param b The integer value to add
553 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000554 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000555 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000556 */
Paul Bakkera755ca12011-04-24 09:11:17 +0000557int mpi_add_int( mpi *X, const mpi *A, t_sint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000558
559/**
Paul Bakker60b1d102013-10-29 10:02:51 +0100560 * \brief Signed subtraction: X = A - b
Paul Bakker5121ce52009-01-03 21:22:43 +0000561 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000562 * \param X Destination MPI
563 * \param A Left-hand MPI
564 * \param b The integer value to subtract
565 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000566 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000567 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000568 */
Paul Bakkera755ca12011-04-24 09:11:17 +0000569int mpi_sub_int( mpi *X, const mpi *A, t_sint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000570
571/**
572 * \brief Baseline multiplication: X = A * B
573 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000574 * \param X Destination MPI
575 * \param A Left-hand MPI
576 * \param B Right-hand MPI
577 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000578 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000579 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000580 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000581int mpi_mul_mpi( mpi *X, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000582
583/**
584 * \brief Baseline multiplication: X = A * b
Manuel Pégourié-Gonnard3eaa8e72013-11-25 16:16:33 +0100585 * Note: despite the functon signature, b is treated as a
586 * t_uint. Negative values of b are treated as large positive
587 * values.
Paul Bakker5121ce52009-01-03 21:22:43 +0000588 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000589 * \param X Destination MPI
590 * \param A Left-hand MPI
591 * \param b The integer value to multiply with
592 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000593 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000594 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000595 */
Paul Bakkera755ca12011-04-24 09:11:17 +0000596int mpi_mul_int( mpi *X, const mpi *A, t_sint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000597
598/**
599 * \brief Division by mpi: A = Q * B + R
600 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000601 * \param Q Destination MPI for the quotient
602 * \param R Destination MPI for the rest value
603 * \param A Left-hand MPI
604 * \param B Right-hand MPI
605 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000606 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000607 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000608 * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if B == 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000609 *
610 * \note Either Q or R can be NULL.
611 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000612int mpi_div_mpi( mpi *Q, mpi *R, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000613
614/**
615 * \brief Division by int: A = Q * b + R
616 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000617 * \param Q Destination MPI for the quotient
618 * \param R Destination MPI for the rest value
619 * \param A Left-hand MPI
620 * \param b Integer to divide by
621 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000622 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000623 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000624 * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if b == 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000625 *
626 * \note Either Q or R can be NULL.
627 */
Paul Bakkera755ca12011-04-24 09:11:17 +0000628int mpi_div_int( mpi *Q, mpi *R, const mpi *A, t_sint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000629
630/**
631 * \brief Modulo: R = A mod B
632 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000633 * \param R Destination MPI for the rest value
634 * \param A Left-hand MPI
635 * \param B Right-hand MPI
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 Bakkerff60ee62010-03-16 21:09:09 +0000642int mpi_mod_mpi( mpi *R, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000643
644/**
645 * \brief Modulo: r = A mod b
646 *
Paul Bakkera755ca12011-04-24 09:11:17 +0000647 * \param r Destination t_uint
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000648 * \param A Left-hand MPI
649 * \param b Integer to divide by
650 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000651 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000652 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
Paul Bakkerce40a6d2009-06-23 19:46:08 +0000653 * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if b == 0,
654 * POLARSSL_ERR_MPI_NEGATIVE_VALUE if b < 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000655 */
Paul Bakkera755ca12011-04-24 09:11:17 +0000656int mpi_mod_int( t_uint *r, const mpi *A, t_sint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000657
658/**
659 * \brief Sliding-window exponentiation: X = A^E mod N
660 *
Paul Bakker9af723c2014-05-01 13:03:14 +0200661 * \param X Destination MPI
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000662 * \param A Left-hand MPI
663 * \param E Exponent MPI
664 * \param N Modular MPI
665 * \param _RR Speed-up MPI used for recalculations
666 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000667 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000668 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200669 * POLARSSL_ERR_MPI_BAD_INPUT_DATA if N is negative or even or
670 * if E is negative
Paul Bakker5121ce52009-01-03 21:22:43 +0000671 *
672 * \note _RR is used to avoid re-computing R*R mod N across
673 * multiple calls, which speeds up things a bit. It can
674 * be set to NULL if the extra performance is unneeded.
675 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000676int mpi_exp_mod( mpi *X, const mpi *A, const mpi *E, const mpi *N, mpi *_RR );
Paul Bakker5121ce52009-01-03 21:22:43 +0000677
678/**
Paul Bakker287781a2011-03-26 13:18:49 +0000679 * \brief Fill an MPI X with size bytes of random
680 *
681 * \param X Destination MPI
682 * \param size Size in bytes
683 * \param f_rng RNG function
684 * \param p_rng RNG parameter
685 *
686 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000687 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker287781a2011-03-26 13:18:49 +0000688 */
Paul Bakkera3d195c2011-11-27 21:07:34 +0000689int mpi_fill_random( mpi *X, size_t size,
690 int (*f_rng)(void *, unsigned char *, size_t),
691 void *p_rng );
Paul Bakker287781a2011-03-26 13:18:49 +0000692
693/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000694 * \brief Greatest common divisor: G = gcd(A, B)
695 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000696 * \param G Destination MPI
697 * \param A Left-hand MPI
698 * \param B Right-hand MPI
699 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000700 * \return 0 if successful,
Paul Bakker69e095c2011-12-10 21:55:01 +0000701 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000702 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000703int mpi_gcd( mpi *G, const mpi *A, const mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000704
705/**
706 * \brief Modular inverse: X = A^-1 mod N
707 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000708 * \param X Destination MPI
709 * \param A Left-hand MPI
710 * \param N Right-hand MPI
711 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000712 * \return 0 if successful,
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_BAD_INPUT_DATA if N is negative or nil
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000715 POLARSSL_ERR_MPI_NOT_ACCEPTABLE if A has no inverse mod N
Paul Bakker5121ce52009-01-03 21:22:43 +0000716 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000717int mpi_inv_mod( mpi *X, const mpi *A, const mpi *N );
Paul Bakker5121ce52009-01-03 21:22:43 +0000718
719/**
720 * \brief Miller-Rabin primality test
721 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000722 * \param X MPI to check
723 * \param f_rng RNG function
724 * \param p_rng RNG parameter
725 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000726 * \return 0 if successful (probably prime),
Paul Bakker69e095c2011-12-10 21:55:01 +0000727 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000728 * POLARSSL_ERR_MPI_NOT_ACCEPTABLE if X is not prime
Paul Bakker5121ce52009-01-03 21:22:43 +0000729 */
Paul Bakker45f457d2013-11-25 14:26:52 +0100730int mpi_is_prime( mpi *X,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000731 int (*f_rng)(void *, unsigned char *, size_t),
732 void *p_rng );
Paul Bakker5121ce52009-01-03 21:22:43 +0000733
734/**
735 * \brief Prime number generation
736 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000737 * \param X Destination MPI
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200738 * \param nbits Required size of X in bits
739 * ( 3 <= nbits <= POLARSSL_MPI_MAX_BITS )
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000740 * \param dh_flag If 1, then (X-1)/2 will be prime too
Paul Bakker5121ce52009-01-03 21:22:43 +0000741 * \param f_rng RNG function
742 * \param p_rng RNG parameter
743 *
744 * \return 0 if successful (probably prime),
Paul Bakker69e095c2011-12-10 21:55:01 +0000745 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
Paul Bakker40e46942009-01-03 21:51:57 +0000746 * POLARSSL_ERR_MPI_BAD_INPUT_DATA if nbits is < 3
Paul Bakker5121ce52009-01-03 21:22:43 +0000747 */
Paul Bakker23986e52011-04-24 08:57:21 +0000748int mpi_gen_prime( mpi *X, size_t nbits, int dh_flag,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000749 int (*f_rng)(void *, unsigned char *, size_t),
750 void *p_rng );
Paul Bakker5121ce52009-01-03 21:22:43 +0000751
752/**
753 * \brief Checkup routine
754 *
755 * \return 0 if successful, or 1 if the test failed
756 */
757int mpi_self_test( int verbose );
758
759#ifdef __cplusplus
760}
761#endif
762
763#endif /* bignum.h */