blob: 88fc86ec2b042eeef7859642e2cfaf2fd8e794b6 [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>
34
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020035#if defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnarde94e6e52015-01-19 15:01:53 +000036#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 Bakker9ef6e2b2012-10-01 20:57:38 +000041typedef INT32 int32_t;
Paul Bakker8ea31ff2013-02-27 15:02:50 +010042typedef INT64 int64_t;
Paul Bakker5c2364c2012-10-01 14:41:15 +000043typedef UINT32 uint32_t;
44typedef UINT64 uint64_t;
45#else
46#include <inttypes.h>
Paul Bakker9af723c2014-05-01 13:03:14 +020047#endif /* _MSC_VER && !EFIX64 && !EFI32 */
Paul Bakker5c2364c2012-10-01 14:41:15 +000048
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020049#define MBEDTLS_ERR_MPI_FILE_IO_ERROR -0x0002 /**< An error occurred while reading from or writing to a file. */
50#define MBEDTLS_ERR_MPI_BAD_INPUT_DATA -0x0004 /**< Bad input parameters to function. */
51#define MBEDTLS_ERR_MPI_INVALID_CHARACTER -0x0006 /**< There is an invalid character in the digit string. */
52#define MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL -0x0008 /**< The buffer is too small to write to. */
53#define MBEDTLS_ERR_MPI_NEGATIVE_VALUE -0x000A /**< The input arguments are negative or result in illegal output. */
54#define MBEDTLS_ERR_MPI_DIVISION_BY_ZERO -0x000C /**< The input argument for division is zero, which is not allowed. */
55#define MBEDTLS_ERR_MPI_NOT_ACCEPTABLE -0x000E /**< The input arguments are not acceptable. */
56#define MBEDTLS_ERR_MPI_MALLOC_FAILED -0x0010 /**< Memory allocation failed. */
Paul Bakker5121ce52009-01-03 21:22:43 +000057
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020058#define MBEDTLS_MPI_CHK(f) do { if( ( ret = f ) != 0 ) goto cleanup; } while( 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000059
60/*
Paul Bakkerf9688572011-05-05 10:00:45 +000061 * Maximum size MPIs are allowed to grow to in number of limbs.
62 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020063#define MBEDTLS_MPI_MAX_LIMBS 10000
Paul Bakkerf9688572011-05-05 10:00:45 +000064
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020065#if !defined(MBEDTLS_MPI_WINDOW_SIZE)
Paul Bakkerf9688572011-05-05 10:00:45 +000066/*
Paul Bakkerb6d5f082011-11-25 11:52:11 +000067 * Maximum window size used for modular exponentiation. Default: 6
68 * Minimum value: 1. Maximum value: 6.
69 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020070 * Result is an array of ( 2 << MBEDTLS_MPI_WINDOW_SIZE ) MPIs used
Paul Bakkerb6d5f082011-11-25 11:52:11 +000071 * for the sliding window calculation. (So 64 by default)
72 *
73 * Reduction in size, reduces speed.
74 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020075#define MBEDTLS_MPI_WINDOW_SIZE 6 /**< Maximum windows size used. */
76#endif /* !MBEDTLS_MPI_WINDOW_SIZE */
Paul Bakkerb6d5f082011-11-25 11:52:11 +000077
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020078#if !defined(MBEDTLS_MPI_MAX_SIZE)
Paul Bakkerb6d5f082011-11-25 11:52:11 +000079/*
Paul Bakkerfe3256e2011-11-25 12:11:43 +000080 * Maximum size of MPIs allowed in bits and bytes for user-MPIs.
Paul Bakkerf626e1d2013-01-21 12:10:00 +010081 * ( Default: 512 bytes => 4096 bits, Maximum tested: 2048 bytes => 16384 bits )
Paul Bakkerfe3256e2011-11-25 12:11:43 +000082 *
83 * Note: Calculations can results temporarily in larger MPIs. So the number
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020084 * of limbs required (MBEDTLS_MPI_MAX_LIMBS) is higher.
Paul Bakkerfe3256e2011-11-25 12:11:43 +000085 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020086#define MBEDTLS_MPI_MAX_SIZE 1024 /**< Maximum number of bytes for usable MPIs. */
87#endif /* !MBEDTLS_MPI_MAX_SIZE */
Paul Bakker9bcf16c2013-06-24 19:31:17 +020088
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020089#define MBEDTLS_MPI_MAX_BITS ( 8 * MBEDTLS_MPI_MAX_SIZE ) /**< Maximum number of bits for usable MPIs. */
Paul Bakkerfe3256e2011-11-25 12:11:43 +000090
91/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020092 * When reading from files with mbedtls_mpi_read_file() and writing to files with
93 * mbedtls_mpi_write_file() the buffer should have space
Paul Bakkercb37aa52011-11-30 16:00:20 +000094 * for a (short) label, the MPI (in the provided radix), the newline
95 * characters and the '\0'.
96 *
97 * By default we assume at least a 10 char label, a minimum radix of 10
98 * (decimal) and a maximum of 4096 bit numbers (1234 decimal chars).
Paul Bakkerf9183102012-09-27 20:42:35 +000099 * Autosized at compile time for at least a 10 char label, a minimum radix
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200100 * of 10 (decimal) for a number of MBEDTLS_MPI_MAX_BITS size.
Paul Bakkerf9183102012-09-27 20:42:35 +0000101 *
102 * This used to be statically sized to 1250 for a maximum of 4096 bit
103 * numbers (1234 decimal chars).
104 *
105 * Calculate using the formula:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200106 * MBEDTLS_MPI_RW_BUFFER_SIZE = ceil(MBEDTLS_MPI_MAX_BITS / ln(10) * ln(2)) +
Paul Bakkerf9183102012-09-27 20:42:35 +0000107 * LabelSize + 6
Paul Bakkercb37aa52011-11-30 16:00:20 +0000108 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200109#define MBEDTLS_MPI_MAX_BITS_SCALE100 ( 100 * MBEDTLS_MPI_MAX_BITS )
110#define MBEDTLS_LN_2_DIV_LN_10_SCALE100 332
111#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 +0000112
113/*
Manuel Pégourié-Gonnard7b538892015-04-09 17:00:17 +0200114 * Define the base integer type, architecture-wise.
115 *
116 * 32-bit integers can be forced on 64-bit arches (eg. for testing purposes)
117 * by defining MBEDTLS_HAVE_INT32 and undefining MBEDTLS_HAVE_ASM
Paul Bakker5121ce52009-01-03 21:22:43 +0000118 */
Manuel Pégourié-Gonnard7b538892015-04-09 17:00:17 +0200119#if ( ! defined(MBEDTLS_HAVE_INT32) && \
120 defined(_MSC_VER) && defined(_M_AMD64) )
121 #define MBEDTLS_HAVE_INT64
122 typedef int64_t mbedtls_mpi_sint;
123 typedef uint64_t mbedtls_mpi_uint;
Paul Bakker5121ce52009-01-03 21:22:43 +0000124#else
Manuel Pégourié-Gonnard7b538892015-04-09 17:00:17 +0200125 #if ( ! defined(MBEDTLS_HAVE_INT32) && \
126 defined(__GNUC__) && ( \
127 defined(__amd64__) || defined(__x86_64__) || \
128 defined(__ppc64__) || defined(__powerpc64__) || \
129 defined(__ia64__) || defined(__alpha__) || \
130 (defined(__sparc__) && defined(__arch64__)) || \
131 defined(__s390x__) || defined(__mips64) ) )
132 #define MBEDTLS_HAVE_INT64
133 typedef int64_t mbedtls_mpi_sint;
134 typedef uint64_t mbedtls_mpi_uint;
135 typedef unsigned int mbedtls_t_udbl __attribute__((mode(TI)));
136 #define MBEDTLS_HAVE_UDBL
Paul Bakker62261d62012-10-02 12:19:31 +0000137 #else
Manuel Pégourié-Gonnard7b538892015-04-09 17:00:17 +0200138 #define MBEDTLS_HAVE_INT32
139 typedef int32_t mbedtls_mpi_sint;
140 typedef uint32_t mbedtls_mpi_uint;
Manuel Pégourié-Gonnard975d5fa2015-04-09 17:13:45 +0200141 typedef uint64_t mbedtls_t_udbl;
142 #define MBEDTLS_HAVE_UDBL
Manuel Pégourié-Gonnard7b538892015-04-09 17:00:17 +0200143 #endif /* !MBEDTLS_HAVE_INT32 && __GNUC__ && 64-bit platform */
144#endif /* !MBEDTLS_HAVE_INT32 && _MSC_VER && _M_AMD64 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000145
Paul Bakker407a0da2013-06-27 14:29:21 +0200146#ifdef __cplusplus
147extern "C" {
148#endif
149
Paul Bakker5121ce52009-01-03 21:22:43 +0000150/**
151 * \brief MPI structure
152 */
153typedef struct
154{
155 int s; /*!< integer sign */
Paul Bakker23986e52011-04-24 08:57:21 +0000156 size_t n; /*!< total # of limbs */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157 mbedtls_mpi_uint *p; /*!< pointer to limbs */
Paul Bakker5121ce52009-01-03 21:22:43 +0000158}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200159mbedtls_mpi;
Paul Bakker5121ce52009-01-03 21:22:43 +0000160
Paul Bakker5121ce52009-01-03 21:22:43 +0000161/**
Paul Bakker6c591fa2011-05-05 11:49:20 +0000162 * \brief Initialize one MPI
163 *
164 * \param X One MPI to initialize.
Paul Bakker5121ce52009-01-03 21:22:43 +0000165 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200166void mbedtls_mpi_init( mbedtls_mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000167
168/**
Paul Bakker6c591fa2011-05-05 11:49:20 +0000169 * \brief Unallocate one MPI
170 *
171 * \param X One MPI to unallocate.
Paul Bakker5121ce52009-01-03 21:22:43 +0000172 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200173void mbedtls_mpi_free( mbedtls_mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000174
175/**
176 * \brief Enlarge to the specified number of limbs
177 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000178 * \param X MPI to grow
179 * \param nblimbs The target number of limbs
180 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000181 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200182 * MBEDTLS_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000183 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200184int mbedtls_mpi_grow( mbedtls_mpi *X, size_t nblimbs );
Paul Bakker5121ce52009-01-03 21:22:43 +0000185
186/**
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100187 * \brief Resize down, keeping at least the specified number of limbs
188 *
189 * \param X MPI to shrink
190 * \param nblimbs The minimum number of limbs to keep
191 *
192 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200193 * MBEDTLS_ERR_MPI_MALLOC_FAILED if memory allocation failed
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100194 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200195int mbedtls_mpi_shrink( mbedtls_mpi *X, size_t nblimbs );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100196
197/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000198 * \brief Copy the contents of Y into X
199 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000200 * \param X Destination MPI
201 * \param Y Source MPI
202 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000203 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200204 * MBEDTLS_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000205 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200206int mbedtls_mpi_copy( mbedtls_mpi *X, const mbedtls_mpi *Y );
Paul Bakker5121ce52009-01-03 21:22:43 +0000207
208/**
209 * \brief Swap the contents of X and Y
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000210 *
211 * \param X First MPI value
212 * \param Y Second MPI value
Paul Bakker5121ce52009-01-03 21:22:43 +0000213 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200214void mbedtls_mpi_swap( mbedtls_mpi *X, mbedtls_mpi *Y );
Paul Bakker5121ce52009-01-03 21:22:43 +0000215
216/**
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100217 * \brief Safe conditional assignement X = Y if assign is 1
218 *
219 * \param X MPI to conditionally assign to
220 * \param Y Value to be assigned
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100221 * \param assign 1: perform the assignment, 0: keep X's original value
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100222 *
223 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200224 * MBEDTLS_ERR_MPI_MALLOC_FAILED if memory allocation failed,
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100225 *
226 * \note This function is equivalent to
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200227 * if( assign ) mbedtls_mpi_copy( X, Y );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100228 * except that it avoids leaking any information about whether
229 * the assignment was done or not (the above code may leak
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +0100230 * information through branch prediction and/or memory access
231 * patterns analysis).
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100232 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200233int mbedtls_mpi_safe_cond_assign( mbedtls_mpi *X, const mbedtls_mpi *Y, unsigned char assign );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100234
235/**
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100236 * \brief Safe conditional swap X <-> Y if swap is 1
237 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200238 * \param X First mbedtls_mpi value
239 * \param Y Second mbedtls_mpi value
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100240 * \param assign 1: perform the swap, 0: keep X and Y's original values
241 *
242 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200243 * MBEDTLS_ERR_MPI_MALLOC_FAILED if memory allocation failed,
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100244 *
245 * \note This function is equivalent to
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200246 * if( assign ) mbedtls_mpi_swap( X, Y );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100247 * except that it avoids leaking any information about whether
248 * the assignment was done or not (the above code may leak
249 * information through branch prediction and/or memory access
250 * patterns analysis).
251 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200252int mbedtls_mpi_safe_cond_swap( mbedtls_mpi *X, mbedtls_mpi *Y, unsigned char assign );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100253
254/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000255 * \brief Set value from integer
256 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000257 * \param X MPI to set
258 * \param z Value to use
259 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000260 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200261 * MBEDTLS_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000262 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200263int mbedtls_mpi_lset( mbedtls_mpi *X, mbedtls_mpi_sint z );
Paul Bakker5121ce52009-01-03 21:22:43 +0000264
Paul Bakker9a736322012-11-14 12:39:52 +0000265/**
Paul Bakker2f5947e2011-05-18 15:47:11 +0000266 * \brief Get a specific bit from X
267 *
268 * \param X MPI to use
269 * \param pos Zero-based index of the bit in X
270 *
271 * \return Either a 0 or a 1
272 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200273int mbedtls_mpi_get_bit( const mbedtls_mpi *X, size_t pos );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000274
Paul Bakker9a736322012-11-14 12:39:52 +0000275/**
Paul Bakker2f5947e2011-05-18 15:47:11 +0000276 * \brief Set a bit of X to a specific value of 0 or 1
277 *
278 * \note Will grow X if necessary to set a bit to 1 in a not yet
279 * existing limb. Will not grow if bit should be set to 0
280 *
281 * \param X MPI to use
282 * \param pos Zero-based index of the bit in X
283 * \param val The value to set the bit to (0 or 1)
284 *
285 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200286 * MBEDTLS_ERR_MPI_MALLOC_FAILED if memory allocation failed,
287 * MBEDTLS_ERR_MPI_BAD_INPUT_DATA if val is not 0 or 1
Paul Bakker2f5947e2011-05-18 15:47:11 +0000288 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200289int mbedtls_mpi_set_bit( mbedtls_mpi *X, size_t pos, unsigned char val );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000290
Paul Bakker5121ce52009-01-03 21:22:43 +0000291/**
Paul Bakker6b906e52012-05-08 12:01:43 +0000292 * \brief Return the number of zero-bits before the least significant
293 * '1' bit
294 *
295 * Note: Thus also the zero-based index of the least significant '1' bit
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000296 *
297 * \param X MPI to use
Paul Bakker5121ce52009-01-03 21:22:43 +0000298 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200299size_t mbedtls_mpi_lsb( const mbedtls_mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000300
301/**
Paul Bakker6b906e52012-05-08 12:01:43 +0000302 * \brief Return the number of bits up to and including the most
303 * significant '1' bit'
304 *
305 * Note: Thus also the one-based index of the most significant '1' bit
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_msb( const mbedtls_mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000310
311/**
312 * \brief Return the total size in bytes
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000313 *
314 * \param X MPI to use
Paul Bakker5121ce52009-01-03 21:22:43 +0000315 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200316size_t mbedtls_mpi_size( const mbedtls_mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000317
318/**
319 * \brief Import from an ASCII string
320 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000321 * \param X Destination MPI
322 * \param radix Input numeric base
323 * \param s Null-terminated string buffer
Paul Bakker5121ce52009-01-03 21:22:43 +0000324 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200325 * \return 0 if successful, or a MBEDTLS_ERR_MPI_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000326 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200327int mbedtls_mpi_read_string( mbedtls_mpi *X, int radix, const char *s );
Paul Bakker5121ce52009-01-03 21:22:43 +0000328
329/**
330 * \brief Export into an ASCII string
331 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000332 * \param X Source MPI
333 * \param radix Output numeric base
334 * \param s String buffer
335 * \param slen String buffer size
Paul Bakker5121ce52009-01-03 21:22:43 +0000336 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200337 * \return 0 if successful, or a MBEDTLS_ERR_MPI_XXX error code.
Paul Bakkerff60ee62010-03-16 21:09:09 +0000338 * *slen is always updated to reflect the amount
339 * of data that has (or would have) been written.
Paul Bakker5121ce52009-01-03 21:22:43 +0000340 *
341 * \note Call this function with *slen = 0 to obtain the
342 * minimum required buffer size in *slen.
343 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200344int mbedtls_mpi_write_string( const mbedtls_mpi *X, int radix, char *s, size_t *slen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000345
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200346#if defined(MBEDTLS_FS_IO)
Paul Bakker5121ce52009-01-03 21:22:43 +0000347/**
348 * \brief Read X from an opened file
349 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000350 * \param X Destination MPI
351 * \param radix Input numeric base
352 * \param fin Input file handle
Paul Bakker5121ce52009-01-03 21:22:43 +0000353 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200354 * \return 0 if successful, MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if
Paul Bakkercb37aa52011-11-30 16:00:20 +0000355 * the file read buffer is too small or a
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200356 * MBEDTLS_ERR_MPI_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000357 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200358int mbedtls_mpi_read_file( mbedtls_mpi *X, int radix, FILE *fin );
Paul Bakker5121ce52009-01-03 21:22:43 +0000359
360/**
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000361 * \brief Write X into an opened file, or stdout if fout is NULL
Paul Bakker5121ce52009-01-03 21:22:43 +0000362 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000363 * \param p Prefix, can be NULL
364 * \param X Source MPI
365 * \param radix Output numeric base
366 * \param fout Output file handle (can be NULL)
Paul Bakker5121ce52009-01-03 21:22:43 +0000367 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200368 * \return 0 if successful, or a MBEDTLS_ERR_MPI_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000369 *
370 * \note Set fout == NULL to print X on the console.
371 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200372int mbedtls_mpi_write_file( const char *p, const mbedtls_mpi *X, int radix, FILE *fout );
373#endif /* MBEDTLS_FS_IO */
Paul Bakker5121ce52009-01-03 21:22:43 +0000374
375/**
376 * \brief Import X from unsigned binary data, big endian
377 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000378 * \param X Destination MPI
379 * \param buf Input buffer
380 * \param buflen Input buffer size
Paul Bakker5121ce52009-01-03 21:22:43 +0000381 *
382 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200383 * MBEDTLS_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000384 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200385int mbedtls_mpi_read_binary( mbedtls_mpi *X, const unsigned char *buf, size_t buflen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000386
387/**
Manuel Pégourié-Gonnard3926a2c2014-06-25 12:57:47 +0200388 * \brief Export X into unsigned binary data, big endian.
389 * Always fills the whole buffer, which will start with zeros
390 * if the number is smaller.
Paul Bakker5121ce52009-01-03 21:22:43 +0000391 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000392 * \param X Source MPI
393 * \param buf Output buffer
394 * \param buflen Output buffer size
Paul Bakker5121ce52009-01-03 21:22:43 +0000395 *
396 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200397 * MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if buf isn't large enough
Paul Bakker5121ce52009-01-03 21:22:43 +0000398 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200399int mbedtls_mpi_write_binary( const mbedtls_mpi *X, unsigned char *buf, size_t buflen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000400
401/**
402 * \brief Left-shift: X <<= count
403 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000404 * \param X MPI to shift
405 * \param count Amount to shift
406 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000407 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200408 * MBEDTLS_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000409 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200410int mbedtls_mpi_shift_l( mbedtls_mpi *X, size_t count );
Paul Bakker5121ce52009-01-03 21:22:43 +0000411
412/**
413 * \brief Right-shift: X >>= count
414 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000415 * \param X MPI to shift
416 * \param count Amount to shift
417 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000418 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200419 * MBEDTLS_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000420 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200421int mbedtls_mpi_shift_r( mbedtls_mpi *X, size_t count );
Paul Bakker5121ce52009-01-03 21:22:43 +0000422
423/**
424 * \brief Compare unsigned values
425 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000426 * \param X Left-hand MPI
427 * \param Y Right-hand MPI
428 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000429 * \return 1 if |X| is greater than |Y|,
430 * -1 if |X| is lesser than |Y| or
431 * 0 if |X| is equal to |Y|
432 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200433int mbedtls_mpi_cmp_abs( const mbedtls_mpi *X, const mbedtls_mpi *Y );
Paul Bakker5121ce52009-01-03 21:22:43 +0000434
435/**
436 * \brief Compare signed values
437 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000438 * \param X Left-hand MPI
439 * \param Y Right-hand MPI
440 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000441 * \return 1 if X is greater than Y,
442 * -1 if X is lesser than Y or
443 * 0 if X is equal to Y
444 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200445int mbedtls_mpi_cmp_mpi( const mbedtls_mpi *X, const mbedtls_mpi *Y );
Paul Bakker5121ce52009-01-03 21:22:43 +0000446
447/**
448 * \brief Compare signed values
449 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000450 * \param X Left-hand MPI
451 * \param z The integer value to compare to
452 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000453 * \return 1 if X is greater than z,
454 * -1 if X is lesser than z or
455 * 0 if X is equal to z
456 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200457int mbedtls_mpi_cmp_int( const mbedtls_mpi *X, mbedtls_mpi_sint z );
Paul Bakker5121ce52009-01-03 21:22:43 +0000458
459/**
460 * \brief Unsigned addition: X = |A| + |B|
461 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000462 * \param X Destination MPI
463 * \param A Left-hand MPI
464 * \param B Right-hand MPI
465 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000466 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200467 * MBEDTLS_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000468 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200469int mbedtls_mpi_add_abs( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000470
471/**
Paul Bakker60b1d102013-10-29 10:02:51 +0100472 * \brief Unsigned subtraction: X = |A| - |B|
Paul Bakker5121ce52009-01-03 21:22:43 +0000473 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000474 * \param X Destination MPI
475 * \param A Left-hand MPI
476 * \param B Right-hand MPI
477 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000478 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200479 * MBEDTLS_ERR_MPI_NEGATIVE_VALUE if B is greater than A
Paul Bakker5121ce52009-01-03 21:22:43 +0000480 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200481int mbedtls_mpi_sub_abs( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000482
483/**
484 * \brief Signed addition: X = A + B
485 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000486 * \param X Destination MPI
487 * \param A Left-hand MPI
488 * \param B Right-hand MPI
489 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000490 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200491 * MBEDTLS_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000492 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200493int mbedtls_mpi_add_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000494
495/**
Paul Bakker60b1d102013-10-29 10:02:51 +0100496 * \brief Signed subtraction: X = A - B
Paul Bakker5121ce52009-01-03 21:22:43 +0000497 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000498 * \param X Destination MPI
499 * \param A Left-hand MPI
500 * \param B Right-hand MPI
501 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000502 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200503 * MBEDTLS_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000504 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200505int mbedtls_mpi_sub_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000506
507/**
508 * \brief Signed addition: X = A + b
509 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000510 * \param X Destination MPI
511 * \param A Left-hand MPI
512 * \param b The integer value to add
513 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000514 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200515 * MBEDTLS_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000516 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200517int mbedtls_mpi_add_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000518
519/**
Paul Bakker60b1d102013-10-29 10:02:51 +0100520 * \brief Signed subtraction: X = A - b
Paul Bakker5121ce52009-01-03 21:22:43 +0000521 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000522 * \param X Destination MPI
523 * \param A Left-hand MPI
524 * \param b The integer value to subtract
525 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000526 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200527 * MBEDTLS_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000528 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200529int mbedtls_mpi_sub_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000530
531/**
532 * \brief Baseline multiplication: X = A * B
533 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000534 * \param X Destination MPI
535 * \param A Left-hand MPI
536 * \param B Right-hand MPI
537 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000538 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200539 * MBEDTLS_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000540 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200541int mbedtls_mpi_mul_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000542
543/**
544 * \brief Baseline multiplication: X = A * b
545 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000546 * \param X Destination MPI
547 * \param A Left-hand MPI
Manuel Pégourié-Gonnard35f1d7f2015-03-19 12:42:40 +0000548 * \param b The unsigned integer value to multiply with
549 *
550 * \note b is unsigned
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000551 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000552 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200553 * MBEDTLS_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000554 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200555int mbedtls_mpi_mul_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_uint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000556
557/**
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200558 * \brief Division by mbedtls_mpi: A = Q * B + R
Paul Bakker5121ce52009-01-03 21:22:43 +0000559 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000560 * \param Q Destination MPI for the quotient
561 * \param R Destination MPI for the rest value
562 * \param A Left-hand MPI
563 * \param B Right-hand MPI
564 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000565 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200566 * MBEDTLS_ERR_MPI_MALLOC_FAILED if memory allocation failed,
567 * MBEDTLS_ERR_MPI_DIVISION_BY_ZERO if B == 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000568 *
569 * \note Either Q or R can be NULL.
570 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200571int 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 +0000572
573/**
574 * \brief Division by int: A = Q * b + R
575 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000576 * \param Q Destination MPI for the quotient
577 * \param R Destination MPI for the rest value
578 * \param A Left-hand MPI
579 * \param b Integer to divide by
580 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000581 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200582 * MBEDTLS_ERR_MPI_MALLOC_FAILED if memory allocation failed,
583 * MBEDTLS_ERR_MPI_DIVISION_BY_ZERO if b == 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000584 *
585 * \note Either Q or R can be NULL.
586 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200587int 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 +0000588
589/**
590 * \brief Modulo: R = A mod B
591 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000592 * \param R Destination MPI for the rest value
593 * \param A Left-hand MPI
594 * \param B Right-hand MPI
595 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000596 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200597 * MBEDTLS_ERR_MPI_MALLOC_FAILED if memory allocation failed,
598 * MBEDTLS_ERR_MPI_DIVISION_BY_ZERO if B == 0,
599 * MBEDTLS_ERR_MPI_NEGATIVE_VALUE if B < 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000600 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200601int mbedtls_mpi_mod_mpi( mbedtls_mpi *R, const mbedtls_mpi *A, const mbedtls_mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000602
603/**
604 * \brief Modulo: r = A mod b
605 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200606 * \param r Destination mbedtls_mpi_uint
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000607 * \param A Left-hand MPI
608 * \param b Integer to divide by
609 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000610 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200611 * MBEDTLS_ERR_MPI_MALLOC_FAILED if memory allocation failed,
612 * MBEDTLS_ERR_MPI_DIVISION_BY_ZERO if b == 0,
613 * MBEDTLS_ERR_MPI_NEGATIVE_VALUE if b < 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000614 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200615int mbedtls_mpi_mod_int( mbedtls_mpi_uint *r, const mbedtls_mpi *A, mbedtls_mpi_sint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000616
617/**
618 * \brief Sliding-window exponentiation: X = A^E mod N
619 *
Paul Bakker9af723c2014-05-01 13:03:14 +0200620 * \param X Destination MPI
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000621 * \param A Left-hand MPI
622 * \param E Exponent MPI
623 * \param N Modular MPI
624 * \param _RR Speed-up MPI used for recalculations
625 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000626 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200627 * MBEDTLS_ERR_MPI_MALLOC_FAILED if memory allocation failed,
628 * MBEDTLS_ERR_MPI_BAD_INPUT_DATA if N is negative or even or
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200629 * if E is negative
Paul Bakker5121ce52009-01-03 21:22:43 +0000630 *
631 * \note _RR is used to avoid re-computing R*R mod N across
632 * multiple calls, which speeds up things a bit. It can
633 * be set to NULL if the extra performance is unneeded.
634 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200635int 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 +0000636
637/**
Paul Bakker287781a2011-03-26 13:18:49 +0000638 * \brief Fill an MPI X with size bytes of random
639 *
640 * \param X Destination MPI
641 * \param size Size in bytes
642 * \param f_rng RNG function
643 * \param p_rng RNG parameter
644 *
645 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200646 * MBEDTLS_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker287781a2011-03-26 13:18:49 +0000647 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200648int mbedtls_mpi_fill_random( mbedtls_mpi *X, size_t size,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000649 int (*f_rng)(void *, unsigned char *, size_t),
650 void *p_rng );
Paul Bakker287781a2011-03-26 13:18:49 +0000651
652/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000653 * \brief Greatest common divisor: G = gcd(A, B)
654 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000655 * \param G Destination MPI
656 * \param A Left-hand MPI
657 * \param B Right-hand MPI
658 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000659 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200660 * MBEDTLS_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000661 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200662int mbedtls_mpi_gcd( mbedtls_mpi *G, const mbedtls_mpi *A, const mbedtls_mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000663
664/**
665 * \brief Modular inverse: X = A^-1 mod N
666 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000667 * \param X Destination MPI
668 * \param A Left-hand MPI
669 * \param N Right-hand MPI
670 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000671 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200672 * MBEDTLS_ERR_MPI_MALLOC_FAILED if memory allocation failed,
673 * MBEDTLS_ERR_MPI_BAD_INPUT_DATA if N is negative or nil
674 MBEDTLS_ERR_MPI_NOT_ACCEPTABLE if A has no inverse mod N
Paul Bakker5121ce52009-01-03 21:22:43 +0000675 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200676int mbedtls_mpi_inv_mod( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *N );
Paul Bakker5121ce52009-01-03 21:22:43 +0000677
678/**
679 * \brief Miller-Rabin primality test
680 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000681 * \param X MPI to check
682 * \param f_rng RNG function
683 * \param p_rng RNG parameter
684 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000685 * \return 0 if successful (probably prime),
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200686 * MBEDTLS_ERR_MPI_MALLOC_FAILED if memory allocation failed,
687 * MBEDTLS_ERR_MPI_NOT_ACCEPTABLE if X is not prime
Paul Bakker5121ce52009-01-03 21:22:43 +0000688 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200689int mbedtls_mpi_is_prime( const mbedtls_mpi *X,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000690 int (*f_rng)(void *, unsigned char *, size_t),
691 void *p_rng );
Paul Bakker5121ce52009-01-03 21:22:43 +0000692
693/**
694 * \brief Prime number generation
695 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000696 * \param X Destination MPI
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200697 * \param nbits Required size of X in bits
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200698 * ( 3 <= nbits <= MBEDTLS_MPI_MAX_BITS )
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000699 * \param dh_flag If 1, then (X-1)/2 will be prime too
Paul Bakker5121ce52009-01-03 21:22:43 +0000700 * \param f_rng RNG function
701 * \param p_rng RNG parameter
702 *
703 * \return 0 if successful (probably prime),
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200704 * MBEDTLS_ERR_MPI_MALLOC_FAILED if memory allocation failed,
705 * MBEDTLS_ERR_MPI_BAD_INPUT_DATA if nbits is < 3
Paul Bakker5121ce52009-01-03 21:22:43 +0000706 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200707int mbedtls_mpi_gen_prime( mbedtls_mpi *X, size_t nbits, int dh_flag,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000708 int (*f_rng)(void *, unsigned char *, size_t),
709 void *p_rng );
Paul Bakker5121ce52009-01-03 21:22:43 +0000710
711/**
712 * \brief Checkup routine
713 *
714 * \return 0 if successful, or 1 if the test failed
715 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200716int mbedtls_mpi_self_test( int verbose );
Paul Bakker5121ce52009-01-03 21:22:43 +0000717
718#ifdef __cplusplus
719}
720#endif
721
722#endif /* bignum.h */