blob: 0dfe22e89e0e908632cbf77bede634ec1adef3b4 [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 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020024#ifndef MBEDTLS_BIGNUM_H
25#define MBEDTLS_BIGNUM_H
Paul Bakker5121ce52009-01-03 21:22:43 +000026
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020027#if !defined(MBEDTLS_CONFIG_FILE)
Paul Bakkercf0360a2012-01-20 10:08:14 +000028#include "config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020029#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020030#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020031#endif
Paul Bakkercf0360a2012-01-20 10:08:14 +000032
Rich Evans00ab4702015-02-06 13:43:58 +000033#include <stddef.h>
Manuel Pégourié-Gonnardab229102015-04-15 11:53:16 +020034#include <stdint.h>
Rich Evans00ab4702015-02-06 13:43:58 +000035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020036#if defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnarde94e6e52015-01-19 15:01:53 +000037#include <stdio.h>
38#endif
39
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020040#define MBEDTLS_ERR_MPI_FILE_IO_ERROR -0x0002 /**< An error occurred while reading from or writing to a file. */
41#define MBEDTLS_ERR_MPI_BAD_INPUT_DATA -0x0004 /**< Bad input parameters to function. */
42#define MBEDTLS_ERR_MPI_INVALID_CHARACTER -0x0006 /**< There is an invalid character in the digit string. */
43#define MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL -0x0008 /**< The buffer is too small to write to. */
44#define MBEDTLS_ERR_MPI_NEGATIVE_VALUE -0x000A /**< The input arguments are negative or result in illegal output. */
45#define MBEDTLS_ERR_MPI_DIVISION_BY_ZERO -0x000C /**< The input argument for division is zero, which is not allowed. */
46#define MBEDTLS_ERR_MPI_NOT_ACCEPTABLE -0x000E /**< The input arguments are not acceptable. */
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +020047#define MBEDTLS_ERR_MPI_ALLOC_FAILED -0x0010 /**< Memory allocation failed. */
Paul Bakker5121ce52009-01-03 21:22:43 +000048
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020049#define MBEDTLS_MPI_CHK(f) do { if( ( ret = f ) != 0 ) goto cleanup; } while( 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000050
51/*
Paul Bakkerf9688572011-05-05 10:00:45 +000052 * Maximum size MPIs are allowed to grow to in number of limbs.
53 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020054#define MBEDTLS_MPI_MAX_LIMBS 10000
Paul Bakkerf9688572011-05-05 10:00:45 +000055
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020056#if !defined(MBEDTLS_MPI_WINDOW_SIZE)
Paul Bakkerf9688572011-05-05 10:00:45 +000057/*
Paul Bakkerb6d5f082011-11-25 11:52:11 +000058 * Maximum window size used for modular exponentiation. Default: 6
59 * Minimum value: 1. Maximum value: 6.
60 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020061 * Result is an array of ( 2 << MBEDTLS_MPI_WINDOW_SIZE ) MPIs used
Paul Bakkerb6d5f082011-11-25 11:52:11 +000062 * for the sliding window calculation. (So 64 by default)
63 *
64 * Reduction in size, reduces speed.
65 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020066#define MBEDTLS_MPI_WINDOW_SIZE 6 /**< Maximum windows size used. */
67#endif /* !MBEDTLS_MPI_WINDOW_SIZE */
Paul Bakkerb6d5f082011-11-25 11:52:11 +000068
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020069#if !defined(MBEDTLS_MPI_MAX_SIZE)
Paul Bakkerb6d5f082011-11-25 11:52:11 +000070/*
Paul Bakkerfe3256e2011-11-25 12:11:43 +000071 * Maximum size of MPIs allowed in bits and bytes for user-MPIs.
Paul Bakkerf626e1d2013-01-21 12:10:00 +010072 * ( Default: 512 bytes => 4096 bits, Maximum tested: 2048 bytes => 16384 bits )
Paul Bakkerfe3256e2011-11-25 12:11:43 +000073 *
74 * Note: Calculations can results temporarily in larger MPIs. So the number
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020075 * of limbs required (MBEDTLS_MPI_MAX_LIMBS) is higher.
Paul Bakkerfe3256e2011-11-25 12:11:43 +000076 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020077#define MBEDTLS_MPI_MAX_SIZE 1024 /**< Maximum number of bytes for usable MPIs. */
78#endif /* !MBEDTLS_MPI_MAX_SIZE */
Paul Bakker9bcf16c2013-06-24 19:31:17 +020079
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020080#define MBEDTLS_MPI_MAX_BITS ( 8 * MBEDTLS_MPI_MAX_SIZE ) /**< Maximum number of bits for usable MPIs. */
Paul Bakkerfe3256e2011-11-25 12:11:43 +000081
82/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020083 * When reading from files with mbedtls_mpi_read_file() and writing to files with
84 * mbedtls_mpi_write_file() the buffer should have space
Paul Bakkercb37aa52011-11-30 16:00:20 +000085 * for a (short) label, the MPI (in the provided radix), the newline
86 * characters and the '\0'.
87 *
88 * By default we assume at least a 10 char label, a minimum radix of 10
89 * (decimal) and a maximum of 4096 bit numbers (1234 decimal chars).
Paul Bakkerf9183102012-09-27 20:42:35 +000090 * Autosized at compile time for at least a 10 char label, a minimum radix
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020091 * of 10 (decimal) for a number of MBEDTLS_MPI_MAX_BITS size.
Paul Bakkerf9183102012-09-27 20:42:35 +000092 *
93 * This used to be statically sized to 1250 for a maximum of 4096 bit
94 * numbers (1234 decimal chars).
95 *
96 * Calculate using the formula:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020097 * MBEDTLS_MPI_RW_BUFFER_SIZE = ceil(MBEDTLS_MPI_MAX_BITS / ln(10) * ln(2)) +
Paul Bakkerf9183102012-09-27 20:42:35 +000098 * LabelSize + 6
Paul Bakkercb37aa52011-11-30 16:00:20 +000099 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200100#define MBEDTLS_MPI_MAX_BITS_SCALE100 ( 100 * MBEDTLS_MPI_MAX_BITS )
101#define MBEDTLS_LN_2_DIV_LN_10_SCALE100 332
102#define MBEDTLS_MPI_RW_BUFFER_SIZE ( ((MBEDTLS_MPI_MAX_BITS_SCALE100 + MBEDTLS_LN_2_DIV_LN_10_SCALE100 - 1) / MBEDTLS_LN_2_DIV_LN_10_SCALE100) + 10 + 6 )
Paul Bakkercb37aa52011-11-30 16:00:20 +0000103
104/*
Manuel Pégourié-Gonnard7b538892015-04-09 17:00:17 +0200105 * Define the base integer type, architecture-wise.
106 *
107 * 32-bit integers can be forced on 64-bit arches (eg. for testing purposes)
108 * by defining MBEDTLS_HAVE_INT32 and undefining MBEDTLS_HAVE_ASM
Paul Bakker5121ce52009-01-03 21:22:43 +0000109 */
Manuel Pégourié-Gonnard7b538892015-04-09 17:00:17 +0200110#if ( ! defined(MBEDTLS_HAVE_INT32) && \
111 defined(_MSC_VER) && defined(_M_AMD64) )
112 #define MBEDTLS_HAVE_INT64
113 typedef int64_t mbedtls_mpi_sint;
114 typedef uint64_t mbedtls_mpi_uint;
Paul Bakker5121ce52009-01-03 21:22:43 +0000115#else
Manuel Pégourié-Gonnard7b538892015-04-09 17:00:17 +0200116 #if ( ! defined(MBEDTLS_HAVE_INT32) && \
117 defined(__GNUC__) && ( \
118 defined(__amd64__) || defined(__x86_64__) || \
119 defined(__ppc64__) || defined(__powerpc64__) || \
120 defined(__ia64__) || defined(__alpha__) || \
121 (defined(__sparc__) && defined(__arch64__)) || \
122 defined(__s390x__) || defined(__mips64) ) )
123 #define MBEDTLS_HAVE_INT64
124 typedef int64_t mbedtls_mpi_sint;
125 typedef uint64_t mbedtls_mpi_uint;
126 typedef unsigned int mbedtls_t_udbl __attribute__((mode(TI)));
127 #define MBEDTLS_HAVE_UDBL
Paul Bakker62261d62012-10-02 12:19:31 +0000128 #else
Manuel Pégourié-Gonnard7b538892015-04-09 17:00:17 +0200129 #define MBEDTLS_HAVE_INT32
130 typedef int32_t mbedtls_mpi_sint;
131 typedef uint32_t mbedtls_mpi_uint;
Manuel Pégourié-Gonnard975d5fa2015-04-09 17:13:45 +0200132 typedef uint64_t mbedtls_t_udbl;
133 #define MBEDTLS_HAVE_UDBL
Manuel Pégourié-Gonnard7b538892015-04-09 17:00:17 +0200134 #endif /* !MBEDTLS_HAVE_INT32 && __GNUC__ && 64-bit platform */
135#endif /* !MBEDTLS_HAVE_INT32 && _MSC_VER && _M_AMD64 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000136
Paul Bakker407a0da2013-06-27 14:29:21 +0200137#ifdef __cplusplus
138extern "C" {
139#endif
140
Paul Bakker5121ce52009-01-03 21:22:43 +0000141/**
142 * \brief MPI structure
143 */
144typedef struct
145{
146 int s; /*!< integer sign */
Paul Bakker23986e52011-04-24 08:57:21 +0000147 size_t n; /*!< total # of limbs */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200148 mbedtls_mpi_uint *p; /*!< pointer to limbs */
Paul Bakker5121ce52009-01-03 21:22:43 +0000149}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200150mbedtls_mpi;
Paul Bakker5121ce52009-01-03 21:22:43 +0000151
Paul Bakker5121ce52009-01-03 21:22:43 +0000152/**
Manuel Pégourié-Gonnardda61ed32015-04-30 10:28:51 +0200153 * \brief Initialize one MPI (make internal references valid)
154 * This just makes it ready to be set or freed,
155 * but does not define a value for the MPI.
Paul Bakker6c591fa2011-05-05 11:49:20 +0000156 *
157 * \param X One MPI to initialize.
Paul Bakker5121ce52009-01-03 21:22:43 +0000158 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200159void mbedtls_mpi_init( mbedtls_mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000160
161/**
Paul Bakker6c591fa2011-05-05 11:49:20 +0000162 * \brief Unallocate one MPI
163 *
164 * \param X One MPI to unallocate.
Paul Bakker5121ce52009-01-03 21:22:43 +0000165 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200166void mbedtls_mpi_free( mbedtls_mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000167
168/**
169 * \brief Enlarge to the specified number of limbs
170 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000171 * \param X MPI to grow
172 * \param nblimbs The target number of limbs
173 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000174 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200175 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000176 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200177int mbedtls_mpi_grow( mbedtls_mpi *X, size_t nblimbs );
Paul Bakker5121ce52009-01-03 21:22:43 +0000178
179/**
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100180 * \brief Resize down, keeping at least the specified number of limbs
181 *
182 * \param X MPI to shrink
183 * \param nblimbs The minimum number of limbs to keep
184 *
185 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200186 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100187 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200188int mbedtls_mpi_shrink( mbedtls_mpi *X, size_t nblimbs );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100189
190/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000191 * \brief Copy the contents of Y into X
192 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000193 * \param X Destination MPI
194 * \param Y Source MPI
195 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000196 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200197 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000198 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200199int mbedtls_mpi_copy( mbedtls_mpi *X, const mbedtls_mpi *Y );
Paul Bakker5121ce52009-01-03 21:22:43 +0000200
201/**
202 * \brief Swap the contents of X and Y
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000203 *
204 * \param X First MPI value
205 * \param Y Second MPI value
Paul Bakker5121ce52009-01-03 21:22:43 +0000206 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200207void mbedtls_mpi_swap( mbedtls_mpi *X, mbedtls_mpi *Y );
Paul Bakker5121ce52009-01-03 21:22:43 +0000208
209/**
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100210 * \brief Safe conditional assignement X = Y if assign is 1
211 *
212 * \param X MPI to conditionally assign to
213 * \param Y Value to be assigned
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100214 * \param assign 1: perform the assignment, 0: keep X's original value
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100215 *
216 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200217 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100218 *
219 * \note This function is equivalent to
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200220 * if( assign ) mbedtls_mpi_copy( X, Y );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100221 * except that it avoids leaking any information about whether
222 * the assignment was done or not (the above code may leak
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +0100223 * information through branch prediction and/or memory access
224 * patterns analysis).
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100225 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200226int mbedtls_mpi_safe_cond_assign( mbedtls_mpi *X, const mbedtls_mpi *Y, unsigned char assign );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100227
228/**
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100229 * \brief Safe conditional swap X <-> Y if swap is 1
230 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200231 * \param X First mbedtls_mpi value
232 * \param Y Second mbedtls_mpi value
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100233 * \param assign 1: perform the swap, 0: keep X and Y's original values
234 *
235 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200236 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100237 *
238 * \note This function is equivalent to
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200239 * if( assign ) mbedtls_mpi_swap( X, Y );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100240 * except that it avoids leaking any information about whether
241 * the assignment was done or not (the above code may leak
242 * information through branch prediction and/or memory access
243 * patterns analysis).
244 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200245int mbedtls_mpi_safe_cond_swap( mbedtls_mpi *X, mbedtls_mpi *Y, unsigned char assign );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100246
247/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000248 * \brief Set value from integer
249 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000250 * \param X MPI to set
251 * \param z Value to use
252 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000253 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200254 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000255 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200256int mbedtls_mpi_lset( mbedtls_mpi *X, mbedtls_mpi_sint z );
Paul Bakker5121ce52009-01-03 21:22:43 +0000257
Paul Bakker9a736322012-11-14 12:39:52 +0000258/**
Paul Bakker2f5947e2011-05-18 15:47:11 +0000259 * \brief Get a specific bit from X
260 *
261 * \param X MPI to use
262 * \param pos Zero-based index of the bit in X
263 *
264 * \return Either a 0 or a 1
265 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200266int mbedtls_mpi_get_bit( const mbedtls_mpi *X, size_t pos );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000267
Paul Bakker9a736322012-11-14 12:39:52 +0000268/**
Paul Bakker2f5947e2011-05-18 15:47:11 +0000269 * \brief Set a bit of X to a specific value of 0 or 1
270 *
271 * \note Will grow X if necessary to set a bit to 1 in a not yet
272 * existing limb. Will not grow if bit should be set to 0
273 *
274 * \param X MPI to use
275 * \param pos Zero-based index of the bit in X
276 * \param val The value to set the bit to (0 or 1)
277 *
278 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200279 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200280 * MBEDTLS_ERR_MPI_BAD_INPUT_DATA if val is not 0 or 1
Paul Bakker2f5947e2011-05-18 15:47:11 +0000281 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200282int mbedtls_mpi_set_bit( mbedtls_mpi *X, size_t pos, unsigned char val );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000283
Paul Bakker5121ce52009-01-03 21:22:43 +0000284/**
Paul Bakker6b906e52012-05-08 12:01:43 +0000285 * \brief Return the number of zero-bits before the least significant
286 * '1' bit
287 *
288 * Note: Thus also the zero-based index of the least significant '1' bit
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000289 *
290 * \param X MPI to use
Paul Bakker5121ce52009-01-03 21:22:43 +0000291 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200292size_t mbedtls_mpi_lsb( const mbedtls_mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000293
294/**
Paul Bakker6b906e52012-05-08 12:01:43 +0000295 * \brief Return the number of bits up to and including the most
296 * significant '1' bit'
297 *
298 * Note: Thus also the one-based index of the most significant '1' bit
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000299 *
300 * \param X MPI to use
Paul Bakker5121ce52009-01-03 21:22:43 +0000301 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200302size_t mbedtls_mpi_msb( const mbedtls_mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000303
304/**
305 * \brief Return the total size in bytes
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000306 *
307 * \param X MPI to use
Paul Bakker5121ce52009-01-03 21:22:43 +0000308 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200309size_t mbedtls_mpi_size( const mbedtls_mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000310
311/**
312 * \brief Import from an ASCII string
313 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000314 * \param X Destination MPI
315 * \param radix Input numeric base
316 * \param s Null-terminated string buffer
Paul Bakker5121ce52009-01-03 21:22:43 +0000317 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200318 * \return 0 if successful, or a MBEDTLS_ERR_MPI_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000319 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200320int mbedtls_mpi_read_string( mbedtls_mpi *X, int radix, const char *s );
Paul Bakker5121ce52009-01-03 21:22:43 +0000321
322/**
323 * \brief Export into an ASCII string
324 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000325 * \param X Source MPI
326 * \param radix Output numeric base
327 * \param s String buffer
328 * \param slen String buffer size
Paul Bakker5121ce52009-01-03 21:22:43 +0000329 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200330 * \return 0 if successful, or a MBEDTLS_ERR_MPI_XXX error code.
Paul Bakkerff60ee62010-03-16 21:09:09 +0000331 * *slen is always updated to reflect the amount
332 * of data that has (or would have) been written.
Paul Bakker5121ce52009-01-03 21:22:43 +0000333 *
334 * \note Call this function with *slen = 0 to obtain the
335 * minimum required buffer size in *slen.
336 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200337int mbedtls_mpi_write_string( const mbedtls_mpi *X, int radix, char *s, size_t *slen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000338
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200339#if defined(MBEDTLS_FS_IO)
Paul Bakker5121ce52009-01-03 21:22:43 +0000340/**
341 * \brief Read X from an opened file
342 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000343 * \param X Destination MPI
344 * \param radix Input numeric base
345 * \param fin Input file handle
Paul Bakker5121ce52009-01-03 21:22:43 +0000346 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200347 * \return 0 if successful, MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if
Paul Bakkercb37aa52011-11-30 16:00:20 +0000348 * the file read buffer is too small or a
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200349 * MBEDTLS_ERR_MPI_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000350 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200351int mbedtls_mpi_read_file( mbedtls_mpi *X, int radix, FILE *fin );
Paul Bakker5121ce52009-01-03 21:22:43 +0000352
353/**
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000354 * \brief Write X into an opened file, or stdout if fout is NULL
Paul Bakker5121ce52009-01-03 21:22:43 +0000355 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000356 * \param p Prefix, can be NULL
357 * \param X Source MPI
358 * \param radix Output numeric base
359 * \param fout Output file handle (can be NULL)
Paul Bakker5121ce52009-01-03 21:22:43 +0000360 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200361 * \return 0 if successful, or a MBEDTLS_ERR_MPI_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000362 *
363 * \note Set fout == NULL to print X on the console.
364 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200365int mbedtls_mpi_write_file( const char *p, const mbedtls_mpi *X, int radix, FILE *fout );
366#endif /* MBEDTLS_FS_IO */
Paul Bakker5121ce52009-01-03 21:22:43 +0000367
368/**
369 * \brief Import X from unsigned binary data, big endian
370 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000371 * \param X Destination MPI
372 * \param buf Input buffer
373 * \param buflen Input buffer size
Paul Bakker5121ce52009-01-03 21:22:43 +0000374 *
375 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200376 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000377 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200378int mbedtls_mpi_read_binary( mbedtls_mpi *X, const unsigned char *buf, size_t buflen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000379
380/**
Manuel Pégourié-Gonnard3926a2c2014-06-25 12:57:47 +0200381 * \brief Export X into unsigned binary data, big endian.
382 * Always fills the whole buffer, which will start with zeros
383 * if the number is smaller.
Paul Bakker5121ce52009-01-03 21:22:43 +0000384 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000385 * \param X Source MPI
386 * \param buf Output buffer
387 * \param buflen Output buffer size
Paul Bakker5121ce52009-01-03 21:22:43 +0000388 *
389 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200390 * MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if buf isn't large enough
Paul Bakker5121ce52009-01-03 21:22:43 +0000391 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200392int mbedtls_mpi_write_binary( const mbedtls_mpi *X, unsigned char *buf, size_t buflen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000393
394/**
395 * \brief Left-shift: X <<= count
396 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000397 * \param X MPI to shift
398 * \param count Amount to shift
399 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000400 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200401 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000402 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200403int mbedtls_mpi_shift_l( mbedtls_mpi *X, size_t count );
Paul Bakker5121ce52009-01-03 21:22:43 +0000404
405/**
406 * \brief Right-shift: X >>= count
407 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000408 * \param X MPI to shift
409 * \param count Amount to shift
410 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000411 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200412 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000413 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200414int mbedtls_mpi_shift_r( mbedtls_mpi *X, size_t count );
Paul Bakker5121ce52009-01-03 21:22:43 +0000415
416/**
417 * \brief Compare unsigned values
418 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000419 * \param X Left-hand MPI
420 * \param Y Right-hand MPI
421 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000422 * \return 1 if |X| is greater than |Y|,
423 * -1 if |X| is lesser than |Y| or
424 * 0 if |X| is equal to |Y|
425 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200426int mbedtls_mpi_cmp_abs( const mbedtls_mpi *X, const mbedtls_mpi *Y );
Paul Bakker5121ce52009-01-03 21:22:43 +0000427
428/**
429 * \brief Compare signed values
430 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000431 * \param X Left-hand MPI
432 * \param Y Right-hand MPI
433 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000434 * \return 1 if X is greater than Y,
435 * -1 if X is lesser than Y or
436 * 0 if X is equal to Y
437 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200438int mbedtls_mpi_cmp_mpi( const mbedtls_mpi *X, const mbedtls_mpi *Y );
Paul Bakker5121ce52009-01-03 21:22:43 +0000439
440/**
441 * \brief Compare signed values
442 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000443 * \param X Left-hand MPI
444 * \param z The integer value to compare to
445 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000446 * \return 1 if X is greater than z,
447 * -1 if X is lesser than z or
448 * 0 if X is equal to z
449 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200450int mbedtls_mpi_cmp_int( const mbedtls_mpi *X, mbedtls_mpi_sint z );
Paul Bakker5121ce52009-01-03 21:22:43 +0000451
452/**
453 * \brief Unsigned addition: X = |A| + |B|
454 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000455 * \param X Destination MPI
456 * \param A Left-hand MPI
457 * \param B Right-hand MPI
458 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000459 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200460 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000461 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200462int mbedtls_mpi_add_abs( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000463
464/**
Paul Bakker60b1d102013-10-29 10:02:51 +0100465 * \brief Unsigned subtraction: X = |A| - |B|
Paul Bakker5121ce52009-01-03 21:22:43 +0000466 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000467 * \param X Destination MPI
468 * \param A Left-hand MPI
469 * \param B Right-hand MPI
470 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000471 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200472 * MBEDTLS_ERR_MPI_NEGATIVE_VALUE if B is greater than A
Paul Bakker5121ce52009-01-03 21:22:43 +0000473 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200474int mbedtls_mpi_sub_abs( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000475
476/**
477 * \brief Signed addition: X = A + B
478 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000479 * \param X Destination MPI
480 * \param A Left-hand MPI
481 * \param B Right-hand MPI
482 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000483 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200484 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000485 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200486int mbedtls_mpi_add_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000487
488/**
Paul Bakker60b1d102013-10-29 10:02:51 +0100489 * \brief Signed subtraction: X = A - B
Paul Bakker5121ce52009-01-03 21:22:43 +0000490 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000491 * \param X Destination MPI
492 * \param A Left-hand MPI
493 * \param B Right-hand MPI
494 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000495 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200496 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000497 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200498int mbedtls_mpi_sub_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000499
500/**
501 * \brief Signed addition: X = A + b
502 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000503 * \param X Destination MPI
504 * \param A Left-hand MPI
505 * \param b The integer value to add
506 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000507 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200508 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000509 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200510int mbedtls_mpi_add_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000511
512/**
Paul Bakker60b1d102013-10-29 10:02:51 +0100513 * \brief Signed subtraction: X = A - b
Paul Bakker5121ce52009-01-03 21:22:43 +0000514 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000515 * \param X Destination MPI
516 * \param A Left-hand MPI
517 * \param b The integer value to subtract
518 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000519 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200520 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000521 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200522int mbedtls_mpi_sub_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000523
524/**
525 * \brief Baseline multiplication: X = A * B
526 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000527 * \param X Destination MPI
528 * \param A Left-hand MPI
529 * \param B Right-hand MPI
530 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000531 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200532 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000533 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200534int mbedtls_mpi_mul_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000535
536/**
537 * \brief Baseline multiplication: X = A * b
538 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000539 * \param X Destination MPI
540 * \param A Left-hand MPI
Manuel Pégourié-Gonnard35f1d7f2015-03-19 12:42:40 +0000541 * \param b The unsigned integer value to multiply with
542 *
543 * \note b is unsigned
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000544 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000545 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200546 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000547 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200548int mbedtls_mpi_mul_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_uint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000549
550/**
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200551 * \brief Division by mbedtls_mpi: A = Q * B + R
Paul Bakker5121ce52009-01-03 21:22:43 +0000552 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000553 * \param Q Destination MPI for the quotient
554 * \param R Destination MPI for the rest value
555 * \param A Left-hand MPI
556 * \param B Right-hand MPI
557 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000558 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200559 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200560 * MBEDTLS_ERR_MPI_DIVISION_BY_ZERO if B == 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000561 *
562 * \note Either Q or R can be NULL.
563 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200564int mbedtls_mpi_div_mpi( mbedtls_mpi *Q, mbedtls_mpi *R, const mbedtls_mpi *A, const mbedtls_mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000565
566/**
567 * \brief Division by int: A = Q * b + R
568 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000569 * \param Q Destination MPI for the quotient
570 * \param R Destination MPI for the rest value
571 * \param A Left-hand MPI
572 * \param b Integer to divide by
573 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000574 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200575 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200576 * MBEDTLS_ERR_MPI_DIVISION_BY_ZERO if b == 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000577 *
578 * \note Either Q or R can be NULL.
579 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200580int mbedtls_mpi_div_int( mbedtls_mpi *Q, mbedtls_mpi *R, const mbedtls_mpi *A, mbedtls_mpi_sint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000581
582/**
583 * \brief Modulo: R = A mod B
584 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000585 * \param R Destination MPI for the rest value
586 * \param A Left-hand MPI
587 * \param B Right-hand MPI
588 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000589 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200590 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200591 * MBEDTLS_ERR_MPI_DIVISION_BY_ZERO if B == 0,
592 * MBEDTLS_ERR_MPI_NEGATIVE_VALUE if B < 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000593 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200594int mbedtls_mpi_mod_mpi( mbedtls_mpi *R, const mbedtls_mpi *A, const mbedtls_mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000595
596/**
597 * \brief Modulo: r = A mod b
598 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200599 * \param r Destination mbedtls_mpi_uint
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000600 * \param A Left-hand MPI
601 * \param b Integer to divide by
602 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000603 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200604 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200605 * MBEDTLS_ERR_MPI_DIVISION_BY_ZERO if b == 0,
606 * MBEDTLS_ERR_MPI_NEGATIVE_VALUE if b < 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000607 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200608int mbedtls_mpi_mod_int( mbedtls_mpi_uint *r, const mbedtls_mpi *A, mbedtls_mpi_sint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000609
610/**
611 * \brief Sliding-window exponentiation: X = A^E mod N
612 *
Paul Bakker9af723c2014-05-01 13:03:14 +0200613 * \param X Destination MPI
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000614 * \param A Left-hand MPI
615 * \param E Exponent MPI
616 * \param N Modular MPI
617 * \param _RR Speed-up MPI used for recalculations
618 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000619 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200620 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200621 * MBEDTLS_ERR_MPI_BAD_INPUT_DATA if N is negative or even or
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200622 * if E is negative
Paul Bakker5121ce52009-01-03 21:22:43 +0000623 *
624 * \note _RR is used to avoid re-computing R*R mod N across
625 * multiple calls, which speeds up things a bit. It can
626 * be set to NULL if the extra performance is unneeded.
627 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200628int mbedtls_mpi_exp_mod( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *E, const mbedtls_mpi *N, mbedtls_mpi *_RR );
Paul Bakker5121ce52009-01-03 21:22:43 +0000629
630/**
Paul Bakker287781a2011-03-26 13:18:49 +0000631 * \brief Fill an MPI X with size bytes of random
632 *
633 * \param X Destination MPI
634 * \param size Size in bytes
635 * \param f_rng RNG function
636 * \param p_rng RNG parameter
637 *
638 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200639 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Paul Bakker287781a2011-03-26 13:18:49 +0000640 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200641int mbedtls_mpi_fill_random( mbedtls_mpi *X, size_t size,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000642 int (*f_rng)(void *, unsigned char *, size_t),
643 void *p_rng );
Paul Bakker287781a2011-03-26 13:18:49 +0000644
645/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000646 * \brief Greatest common divisor: G = gcd(A, B)
647 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000648 * \param G Destination MPI
649 * \param A Left-hand MPI
650 * \param B Right-hand MPI
651 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000652 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200653 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000654 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200655int mbedtls_mpi_gcd( mbedtls_mpi *G, const mbedtls_mpi *A, const mbedtls_mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000656
657/**
658 * \brief Modular inverse: X = A^-1 mod N
659 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000660 * \param X Destination MPI
661 * \param A Left-hand MPI
662 * \param N Right-hand MPI
663 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000664 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200665 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200666 * MBEDTLS_ERR_MPI_BAD_INPUT_DATA if N is negative or nil
667 MBEDTLS_ERR_MPI_NOT_ACCEPTABLE if A has no inverse mod N
Paul Bakker5121ce52009-01-03 21:22:43 +0000668 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200669int mbedtls_mpi_inv_mod( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *N );
Paul Bakker5121ce52009-01-03 21:22:43 +0000670
671/**
672 * \brief Miller-Rabin primality test
673 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000674 * \param X MPI to check
675 * \param f_rng RNG function
676 * \param p_rng RNG parameter
677 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000678 * \return 0 if successful (probably prime),
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200679 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200680 * MBEDTLS_ERR_MPI_NOT_ACCEPTABLE if X is not prime
Paul Bakker5121ce52009-01-03 21:22:43 +0000681 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200682int mbedtls_mpi_is_prime( const mbedtls_mpi *X,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000683 int (*f_rng)(void *, unsigned char *, size_t),
684 void *p_rng );
Paul Bakker5121ce52009-01-03 21:22:43 +0000685
686/**
687 * \brief Prime number generation
688 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000689 * \param X Destination MPI
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200690 * \param nbits Required size of X in bits
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200691 * ( 3 <= nbits <= MBEDTLS_MPI_MAX_BITS )
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000692 * \param dh_flag If 1, then (X-1)/2 will be prime too
Paul Bakker5121ce52009-01-03 21:22:43 +0000693 * \param f_rng RNG function
694 * \param p_rng RNG parameter
695 *
696 * \return 0 if successful (probably prime),
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200697 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200698 * MBEDTLS_ERR_MPI_BAD_INPUT_DATA if nbits is < 3
Paul Bakker5121ce52009-01-03 21:22:43 +0000699 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200700int mbedtls_mpi_gen_prime( mbedtls_mpi *X, size_t nbits, int dh_flag,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000701 int (*f_rng)(void *, unsigned char *, size_t),
702 void *p_rng );
Paul Bakker5121ce52009-01-03 21:22:43 +0000703
704/**
705 * \brief Checkup routine
706 *
707 * \return 0 if successful, or 1 if the test failed
708 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200709int mbedtls_mpi_self_test( int verbose );
Paul Bakker5121ce52009-01-03 21:22:43 +0000710
711#ifdef __cplusplus
712}
713#endif
714
715#endif /* bignum.h */