blob: 68ac65b1dac887e2ff00655fdd6215b09b10a065 [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;
141 #if ( defined(_MSC_VER) && defined(_M_IX86) )
142 typedef uint64_t mbedtls_t_udbl;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200143 #define MBEDTLS_HAVE_UDBL
Manuel Pégourié-Gonnard7b538892015-04-09 17:00:17 +0200144 #else
145 #if defined( MBEDTLS_HAVE_LONGLONG )
146 typedef unsigned long long mbedtls_t_udbl;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200147 #define MBEDTLS_HAVE_UDBL
Paul Bakker62261d62012-10-02 12:19:31 +0000148 #endif
Manuel Pégourié-Gonnard7b538892015-04-09 17:00:17 +0200149 #endif
150 #endif /* !MBEDTLS_HAVE_INT32 && __GNUC__ && 64-bit platform */
151#endif /* !MBEDTLS_HAVE_INT32 && _MSC_VER && _M_AMD64 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000152
Paul Bakker407a0da2013-06-27 14:29:21 +0200153#ifdef __cplusplus
154extern "C" {
155#endif
156
Paul Bakker5121ce52009-01-03 21:22:43 +0000157/**
158 * \brief MPI structure
159 */
160typedef struct
161{
162 int s; /*!< integer sign */
Paul Bakker23986e52011-04-24 08:57:21 +0000163 size_t n; /*!< total # of limbs */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200164 mbedtls_mpi_uint *p; /*!< pointer to limbs */
Paul Bakker5121ce52009-01-03 21:22:43 +0000165}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200166mbedtls_mpi;
Paul Bakker5121ce52009-01-03 21:22:43 +0000167
Paul Bakker5121ce52009-01-03 21:22:43 +0000168/**
Paul Bakker6c591fa2011-05-05 11:49:20 +0000169 * \brief Initialize one MPI
170 *
171 * \param X One MPI to initialize.
Paul Bakker5121ce52009-01-03 21:22:43 +0000172 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200173void mbedtls_mpi_init( mbedtls_mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000174
175/**
Paul Bakker6c591fa2011-05-05 11:49:20 +0000176 * \brief Unallocate one MPI
177 *
178 * \param X One MPI to unallocate.
Paul Bakker5121ce52009-01-03 21:22:43 +0000179 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200180void mbedtls_mpi_free( mbedtls_mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000181
182/**
183 * \brief Enlarge to the specified number of limbs
184 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000185 * \param X MPI to grow
186 * \param nblimbs The target number of limbs
187 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000188 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200189 * MBEDTLS_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000190 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200191int mbedtls_mpi_grow( mbedtls_mpi *X, size_t nblimbs );
Paul Bakker5121ce52009-01-03 21:22:43 +0000192
193/**
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100194 * \brief Resize down, keeping at least the specified number of limbs
195 *
196 * \param X MPI to shrink
197 * \param nblimbs The minimum number of limbs to keep
198 *
199 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200200 * MBEDTLS_ERR_MPI_MALLOC_FAILED if memory allocation failed
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100201 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200202int mbedtls_mpi_shrink( mbedtls_mpi *X, size_t nblimbs );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100203
204/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000205 * \brief Copy the contents of Y into X
206 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000207 * \param X Destination MPI
208 * \param Y Source MPI
209 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000210 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200211 * MBEDTLS_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000212 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200213int mbedtls_mpi_copy( mbedtls_mpi *X, const mbedtls_mpi *Y );
Paul Bakker5121ce52009-01-03 21:22:43 +0000214
215/**
216 * \brief Swap the contents of X and Y
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000217 *
218 * \param X First MPI value
219 * \param Y Second MPI value
Paul Bakker5121ce52009-01-03 21:22:43 +0000220 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200221void mbedtls_mpi_swap( mbedtls_mpi *X, mbedtls_mpi *Y );
Paul Bakker5121ce52009-01-03 21:22:43 +0000222
223/**
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100224 * \brief Safe conditional assignement X = Y if assign is 1
225 *
226 * \param X MPI to conditionally assign to
227 * \param Y Value to be assigned
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100228 * \param assign 1: perform the assignment, 0: keep X's original value
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100229 *
230 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200231 * MBEDTLS_ERR_MPI_MALLOC_FAILED if memory allocation failed,
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100232 *
233 * \note This function is equivalent to
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200234 * if( assign ) mbedtls_mpi_copy( X, Y );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100235 * except that it avoids leaking any information about whether
236 * the assignment was done or not (the above code may leak
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +0100237 * information through branch prediction and/or memory access
238 * patterns analysis).
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100239 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200240int mbedtls_mpi_safe_cond_assign( mbedtls_mpi *X, const mbedtls_mpi *Y, unsigned char assign );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100241
242/**
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100243 * \brief Safe conditional swap X <-> Y if swap is 1
244 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200245 * \param X First mbedtls_mpi value
246 * \param Y Second mbedtls_mpi value
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100247 * \param assign 1: perform the swap, 0: keep X and Y's original values
248 *
249 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200250 * MBEDTLS_ERR_MPI_MALLOC_FAILED if memory allocation failed,
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100251 *
252 * \note This function is equivalent to
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200253 * if( assign ) mbedtls_mpi_swap( X, Y );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100254 * except that it avoids leaking any information about whether
255 * the assignment was done or not (the above code may leak
256 * information through branch prediction and/or memory access
257 * patterns analysis).
258 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200259int mbedtls_mpi_safe_cond_swap( mbedtls_mpi *X, mbedtls_mpi *Y, unsigned char assign );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100260
261/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000262 * \brief Set value from integer
263 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000264 * \param X MPI to set
265 * \param z Value to use
266 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000267 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200268 * MBEDTLS_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000269 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200270int mbedtls_mpi_lset( mbedtls_mpi *X, mbedtls_mpi_sint z );
Paul Bakker5121ce52009-01-03 21:22:43 +0000271
Paul Bakker9a736322012-11-14 12:39:52 +0000272/**
Paul Bakker2f5947e2011-05-18 15:47:11 +0000273 * \brief Get a specific bit from X
274 *
275 * \param X MPI to use
276 * \param pos Zero-based index of the bit in X
277 *
278 * \return Either a 0 or a 1
279 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200280int mbedtls_mpi_get_bit( const mbedtls_mpi *X, size_t pos );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000281
Paul Bakker9a736322012-11-14 12:39:52 +0000282/**
Paul Bakker2f5947e2011-05-18 15:47:11 +0000283 * \brief Set a bit of X to a specific value of 0 or 1
284 *
285 * \note Will grow X if necessary to set a bit to 1 in a not yet
286 * existing limb. Will not grow if bit should be set to 0
287 *
288 * \param X MPI to use
289 * \param pos Zero-based index of the bit in X
290 * \param val The value to set the bit to (0 or 1)
291 *
292 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200293 * MBEDTLS_ERR_MPI_MALLOC_FAILED if memory allocation failed,
294 * MBEDTLS_ERR_MPI_BAD_INPUT_DATA if val is not 0 or 1
Paul Bakker2f5947e2011-05-18 15:47:11 +0000295 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200296int mbedtls_mpi_set_bit( mbedtls_mpi *X, size_t pos, unsigned char val );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000297
Paul Bakker5121ce52009-01-03 21:22:43 +0000298/**
Paul Bakker6b906e52012-05-08 12:01:43 +0000299 * \brief Return the number of zero-bits before the least significant
300 * '1' bit
301 *
302 * Note: Thus also the zero-based index of the least significant '1' bit
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000303 *
304 * \param X MPI to use
Paul Bakker5121ce52009-01-03 21:22:43 +0000305 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200306size_t mbedtls_mpi_lsb( const mbedtls_mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000307
308/**
Paul Bakker6b906e52012-05-08 12:01:43 +0000309 * \brief Return the number of bits up to and including the most
310 * significant '1' bit'
311 *
312 * Note: Thus also the one-based index of the most significant '1' bit
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_msb( const mbedtls_mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000317
318/**
319 * \brief Return the total size in bytes
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000320 *
321 * \param X MPI to use
Paul Bakker5121ce52009-01-03 21:22:43 +0000322 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200323size_t mbedtls_mpi_size( const mbedtls_mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000324
325/**
326 * \brief Import from an ASCII string
327 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000328 * \param X Destination MPI
329 * \param radix Input numeric base
330 * \param s Null-terminated string buffer
Paul Bakker5121ce52009-01-03 21:22:43 +0000331 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200332 * \return 0 if successful, or a MBEDTLS_ERR_MPI_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000333 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200334int mbedtls_mpi_read_string( mbedtls_mpi *X, int radix, const char *s );
Paul Bakker5121ce52009-01-03 21:22:43 +0000335
336/**
337 * \brief Export into an ASCII string
338 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000339 * \param X Source MPI
340 * \param radix Output numeric base
341 * \param s String buffer
342 * \param slen String buffer size
Paul Bakker5121ce52009-01-03 21:22:43 +0000343 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200344 * \return 0 if successful, or a MBEDTLS_ERR_MPI_XXX error code.
Paul Bakkerff60ee62010-03-16 21:09:09 +0000345 * *slen is always updated to reflect the amount
346 * of data that has (or would have) been written.
Paul Bakker5121ce52009-01-03 21:22:43 +0000347 *
348 * \note Call this function with *slen = 0 to obtain the
349 * minimum required buffer size in *slen.
350 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200351int mbedtls_mpi_write_string( const mbedtls_mpi *X, int radix, char *s, size_t *slen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000352
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200353#if defined(MBEDTLS_FS_IO)
Paul Bakker5121ce52009-01-03 21:22:43 +0000354/**
355 * \brief Read X from an opened file
356 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000357 * \param X Destination MPI
358 * \param radix Input numeric base
359 * \param fin Input file handle
Paul Bakker5121ce52009-01-03 21:22:43 +0000360 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200361 * \return 0 if successful, MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if
Paul Bakkercb37aa52011-11-30 16:00:20 +0000362 * the file read buffer is too small or a
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200363 * MBEDTLS_ERR_MPI_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000364 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200365int mbedtls_mpi_read_file( mbedtls_mpi *X, int radix, FILE *fin );
Paul Bakker5121ce52009-01-03 21:22:43 +0000366
367/**
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000368 * \brief Write X into an opened file, or stdout if fout is NULL
Paul Bakker5121ce52009-01-03 21:22:43 +0000369 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000370 * \param p Prefix, can be NULL
371 * \param X Source MPI
372 * \param radix Output numeric base
373 * \param fout Output file handle (can be NULL)
Paul Bakker5121ce52009-01-03 21:22:43 +0000374 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200375 * \return 0 if successful, or a MBEDTLS_ERR_MPI_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000376 *
377 * \note Set fout == NULL to print X on the console.
378 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200379int mbedtls_mpi_write_file( const char *p, const mbedtls_mpi *X, int radix, FILE *fout );
380#endif /* MBEDTLS_FS_IO */
Paul Bakker5121ce52009-01-03 21:22:43 +0000381
382/**
383 * \brief Import X from unsigned binary data, big endian
384 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000385 * \param X Destination MPI
386 * \param buf Input buffer
387 * \param buflen Input 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_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000391 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200392int mbedtls_mpi_read_binary( mbedtls_mpi *X, const unsigned char *buf, size_t buflen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000393
394/**
Manuel Pégourié-Gonnard3926a2c2014-06-25 12:57:47 +0200395 * \brief Export X into unsigned binary data, big endian.
396 * Always fills the whole buffer, which will start with zeros
397 * if the number is smaller.
Paul Bakker5121ce52009-01-03 21:22:43 +0000398 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000399 * \param X Source MPI
400 * \param buf Output buffer
401 * \param buflen Output buffer size
Paul Bakker5121ce52009-01-03 21:22:43 +0000402 *
403 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200404 * MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if buf isn't large enough
Paul Bakker5121ce52009-01-03 21:22:43 +0000405 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200406int mbedtls_mpi_write_binary( const mbedtls_mpi *X, unsigned char *buf, size_t buflen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000407
408/**
409 * \brief Left-shift: X <<= count
410 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000411 * \param X MPI to shift
412 * \param count Amount to shift
413 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000414 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200415 * MBEDTLS_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000416 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200417int mbedtls_mpi_shift_l( mbedtls_mpi *X, size_t count );
Paul Bakker5121ce52009-01-03 21:22:43 +0000418
419/**
420 * \brief Right-shift: X >>= count
421 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000422 * \param X MPI to shift
423 * \param count Amount to shift
424 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000425 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200426 * MBEDTLS_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000427 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200428int mbedtls_mpi_shift_r( mbedtls_mpi *X, size_t count );
Paul Bakker5121ce52009-01-03 21:22:43 +0000429
430/**
431 * \brief Compare unsigned values
432 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000433 * \param X Left-hand MPI
434 * \param Y Right-hand MPI
435 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000436 * \return 1 if |X| is greater than |Y|,
437 * -1 if |X| is lesser than |Y| or
438 * 0 if |X| is equal to |Y|
439 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200440int mbedtls_mpi_cmp_abs( const mbedtls_mpi *X, const mbedtls_mpi *Y );
Paul Bakker5121ce52009-01-03 21:22:43 +0000441
442/**
443 * \brief Compare signed values
444 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000445 * \param X Left-hand MPI
446 * \param Y Right-hand MPI
447 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000448 * \return 1 if X is greater than Y,
449 * -1 if X is lesser than Y or
450 * 0 if X is equal to Y
451 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200452int mbedtls_mpi_cmp_mpi( const mbedtls_mpi *X, const mbedtls_mpi *Y );
Paul Bakker5121ce52009-01-03 21:22:43 +0000453
454/**
455 * \brief Compare signed values
456 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000457 * \param X Left-hand MPI
458 * \param z The integer value to compare to
459 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000460 * \return 1 if X is greater than z,
461 * -1 if X is lesser than z or
462 * 0 if X is equal to z
463 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200464int mbedtls_mpi_cmp_int( const mbedtls_mpi *X, mbedtls_mpi_sint z );
Paul Bakker5121ce52009-01-03 21:22:43 +0000465
466/**
467 * \brief Unsigned addition: X = |A| + |B|
468 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000469 * \param X Destination MPI
470 * \param A Left-hand MPI
471 * \param B Right-hand MPI
472 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000473 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200474 * MBEDTLS_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000475 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200476int mbedtls_mpi_add_abs( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000477
478/**
Paul Bakker60b1d102013-10-29 10:02:51 +0100479 * \brief Unsigned subtraction: X = |A| - |B|
Paul Bakker5121ce52009-01-03 21:22:43 +0000480 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000481 * \param X Destination MPI
482 * \param A Left-hand MPI
483 * \param B Right-hand MPI
484 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000485 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200486 * MBEDTLS_ERR_MPI_NEGATIVE_VALUE if B is greater than A
Paul Bakker5121ce52009-01-03 21:22:43 +0000487 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200488int mbedtls_mpi_sub_abs( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000489
490/**
491 * \brief Signed addition: X = A + B
492 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000493 * \param X Destination MPI
494 * \param A Left-hand MPI
495 * \param B Right-hand MPI
496 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000497 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200498 * MBEDTLS_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000499 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200500int mbedtls_mpi_add_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000501
502/**
Paul Bakker60b1d102013-10-29 10:02:51 +0100503 * \brief Signed subtraction: X = A - B
Paul Bakker5121ce52009-01-03 21:22:43 +0000504 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000505 * \param X Destination MPI
506 * \param A Left-hand MPI
507 * \param B Right-hand MPI
508 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000509 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200510 * MBEDTLS_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000511 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200512int mbedtls_mpi_sub_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000513
514/**
515 * \brief Signed addition: X = A + b
516 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000517 * \param X Destination MPI
518 * \param A Left-hand MPI
519 * \param b The integer value to add
520 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000521 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200522 * MBEDTLS_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000523 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200524int mbedtls_mpi_add_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000525
526/**
Paul Bakker60b1d102013-10-29 10:02:51 +0100527 * \brief Signed subtraction: X = A - b
Paul Bakker5121ce52009-01-03 21:22:43 +0000528 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000529 * \param X Destination MPI
530 * \param A Left-hand MPI
531 * \param b The integer value to subtract
532 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000533 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200534 * MBEDTLS_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000535 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200536int mbedtls_mpi_sub_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000537
538/**
539 * \brief Baseline multiplication: X = A * B
540 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000541 * \param X Destination MPI
542 * \param A Left-hand MPI
543 * \param B Right-hand MPI
544 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000545 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200546 * MBEDTLS_ERR_MPI_MALLOC_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_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000549
550/**
551 * \brief Baseline multiplication: X = A * b
552 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000553 * \param X Destination MPI
554 * \param A Left-hand MPI
Manuel Pégourié-Gonnard35f1d7f2015-03-19 12:42:40 +0000555 * \param b The unsigned integer value to multiply with
556 *
557 * \note b is unsigned
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000558 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000559 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200560 * MBEDTLS_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000561 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200562int mbedtls_mpi_mul_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_uint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000563
564/**
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200565 * \brief Division by mbedtls_mpi: A = Q * B + R
Paul Bakker5121ce52009-01-03 21:22:43 +0000566 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000567 * \param Q Destination MPI for the quotient
568 * \param R Destination MPI for the rest value
569 * \param A Left-hand MPI
570 * \param B Right-hand MPI
571 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000572 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200573 * MBEDTLS_ERR_MPI_MALLOC_FAILED if memory allocation failed,
574 * MBEDTLS_ERR_MPI_DIVISION_BY_ZERO if B == 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000575 *
576 * \note Either Q or R can be NULL.
577 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200578int 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 +0000579
580/**
581 * \brief Division by int: A = Q * b + R
582 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000583 * \param Q Destination MPI for the quotient
584 * \param R Destination MPI for the rest value
585 * \param A Left-hand MPI
586 * \param b Integer to divide by
587 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000588 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200589 * MBEDTLS_ERR_MPI_MALLOC_FAILED if memory allocation failed,
590 * MBEDTLS_ERR_MPI_DIVISION_BY_ZERO if b == 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000591 *
592 * \note Either Q or R can be NULL.
593 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200594int 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 +0000595
596/**
597 * \brief Modulo: R = A mod B
598 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000599 * \param R Destination MPI for the rest value
600 * \param A Left-hand MPI
601 * \param B Right-hand MPI
602 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000603 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200604 * MBEDTLS_ERR_MPI_MALLOC_FAILED if memory allocation failed,
605 * 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_mpi( mbedtls_mpi *R, const mbedtls_mpi *A, const mbedtls_mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000609
610/**
611 * \brief Modulo: r = A mod b
612 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200613 * \param r Destination mbedtls_mpi_uint
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000614 * \param A Left-hand MPI
615 * \param b Integer to divide by
616 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000617 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200618 * MBEDTLS_ERR_MPI_MALLOC_FAILED if memory allocation failed,
619 * MBEDTLS_ERR_MPI_DIVISION_BY_ZERO if b == 0,
620 * MBEDTLS_ERR_MPI_NEGATIVE_VALUE if b < 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000621 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200622int mbedtls_mpi_mod_int( mbedtls_mpi_uint *r, const mbedtls_mpi *A, mbedtls_mpi_sint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000623
624/**
625 * \brief Sliding-window exponentiation: X = A^E mod N
626 *
Paul Bakker9af723c2014-05-01 13:03:14 +0200627 * \param X Destination MPI
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000628 * \param A Left-hand MPI
629 * \param E Exponent MPI
630 * \param N Modular MPI
631 * \param _RR Speed-up MPI used for recalculations
632 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000633 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200634 * MBEDTLS_ERR_MPI_MALLOC_FAILED if memory allocation failed,
635 * MBEDTLS_ERR_MPI_BAD_INPUT_DATA if N is negative or even or
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200636 * if E is negative
Paul Bakker5121ce52009-01-03 21:22:43 +0000637 *
638 * \note _RR is used to avoid re-computing R*R mod N across
639 * multiple calls, which speeds up things a bit. It can
640 * be set to NULL if the extra performance is unneeded.
641 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200642int 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 +0000643
644/**
Paul Bakker287781a2011-03-26 13:18:49 +0000645 * \brief Fill an MPI X with size bytes of random
646 *
647 * \param X Destination MPI
648 * \param size Size in bytes
649 * \param f_rng RNG function
650 * \param p_rng RNG parameter
651 *
652 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200653 * MBEDTLS_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker287781a2011-03-26 13:18:49 +0000654 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200655int mbedtls_mpi_fill_random( mbedtls_mpi *X, size_t size,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000656 int (*f_rng)(void *, unsigned char *, size_t),
657 void *p_rng );
Paul Bakker287781a2011-03-26 13:18:49 +0000658
659/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000660 * \brief Greatest common divisor: G = gcd(A, B)
661 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000662 * \param G Destination MPI
663 * \param A Left-hand MPI
664 * \param B Right-hand MPI
665 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000666 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200667 * MBEDTLS_ERR_MPI_MALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000668 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200669int mbedtls_mpi_gcd( mbedtls_mpi *G, const mbedtls_mpi *A, const mbedtls_mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000670
671/**
672 * \brief Modular inverse: X = A^-1 mod N
673 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000674 * \param X Destination MPI
675 * \param A Left-hand MPI
676 * \param N Right-hand MPI
677 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000678 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200679 * MBEDTLS_ERR_MPI_MALLOC_FAILED if memory allocation failed,
680 * MBEDTLS_ERR_MPI_BAD_INPUT_DATA if N is negative or nil
681 MBEDTLS_ERR_MPI_NOT_ACCEPTABLE if A has no inverse mod N
Paul Bakker5121ce52009-01-03 21:22:43 +0000682 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200683int mbedtls_mpi_inv_mod( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *N );
Paul Bakker5121ce52009-01-03 21:22:43 +0000684
685/**
686 * \brief Miller-Rabin primality test
687 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000688 * \param X MPI to check
689 * \param f_rng RNG function
690 * \param p_rng RNG parameter
691 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000692 * \return 0 if successful (probably prime),
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200693 * MBEDTLS_ERR_MPI_MALLOC_FAILED if memory allocation failed,
694 * MBEDTLS_ERR_MPI_NOT_ACCEPTABLE if X is not prime
Paul Bakker5121ce52009-01-03 21:22:43 +0000695 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200696int mbedtls_mpi_is_prime( const mbedtls_mpi *X,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000697 int (*f_rng)(void *, unsigned char *, size_t),
698 void *p_rng );
Paul Bakker5121ce52009-01-03 21:22:43 +0000699
700/**
701 * \brief Prime number generation
702 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000703 * \param X Destination MPI
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200704 * \param nbits Required size of X in bits
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200705 * ( 3 <= nbits <= MBEDTLS_MPI_MAX_BITS )
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000706 * \param dh_flag If 1, then (X-1)/2 will be prime too
Paul Bakker5121ce52009-01-03 21:22:43 +0000707 * \param f_rng RNG function
708 * \param p_rng RNG parameter
709 *
710 * \return 0 if successful (probably prime),
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200711 * MBEDTLS_ERR_MPI_MALLOC_FAILED if memory allocation failed,
712 * MBEDTLS_ERR_MPI_BAD_INPUT_DATA if nbits is < 3
Paul Bakker5121ce52009-01-03 21:22:43 +0000713 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200714int mbedtls_mpi_gen_prime( mbedtls_mpi *X, size_t nbits, int dh_flag,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000715 int (*f_rng)(void *, unsigned char *, size_t),
716 void *p_rng );
Paul Bakker5121ce52009-01-03 21:22:43 +0000717
718/**
719 * \brief Checkup routine
720 *
721 * \return 0 if successful, or 1 if the test failed
722 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200723int mbedtls_mpi_self_test( int verbose );
Paul Bakker5121ce52009-01-03 21:22:43 +0000724
725#ifdef __cplusplus
726}
727#endif
728
729#endif /* bignum.h */