blob: 4b579b05064f70dbc5e95c9910d3a1eb9a4157dd [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/**
2 * \file bignum.h
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00003 *
Darryl Greena40a1012018-01-05 15:33:17 +00004 * \brief Multi-precision integer library
5 */
6/*
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02007 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02008 * SPDX-License-Identifier: Apache-2.0
9 *
10 * Licensed under the Apache License, Version 2.0 (the "License"); you may
11 * not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
13 *
14 * http://www.apache.org/licenses/LICENSE-2.0
15 *
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
18 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000021 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000022 * This file is part of mbed TLS (https://tls.mbed.org)
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 *
Hanno Beckerefeef6c2018-01-05 08:07:47 +000074 * Note: Calculations can temporarily result 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 *
Andres Amaya Garciad7fce002017-07-20 11:49:32 +0100107 * 32 or 64-bit integer types can be forced regardless of the underlying
108 * architecture by defining MBEDTLS_HAVE_INT32 or MBEDTLS_HAVE_INT64
109 * respectively and undefining MBEDTLS_HAVE_ASM.
110 *
Andres Amaya Garcia93db11a2017-07-20 12:11:19 +0100111 * Double-width integers (e.g. 128-bit in 64-bit architectures) can be
Andres Amaya Garciad7fce002017-07-20 11:49:32 +0100112 * disabled by defining MBEDTLS_NO_UDBL_DIVISION.
Paul Bakker5121ce52009-01-03 21:22:43 +0000113 */
Andres Amaya Garciaaa27dfe2017-05-04 11:05:55 +0100114#if !defined(MBEDTLS_HAVE_INT32)
115 #if defined(_MSC_VER) && defined(_M_AMD64)
116 /* Always choose 64-bit when using MSC */
Andres Amaya Garciad7fce002017-07-20 11:49:32 +0100117 #if !defined(MBEDTLS_HAVE_INT64)
118 #define MBEDTLS_HAVE_INT64
119 #endif /* !MBEDTLS_HAVE_INT64 */
Andres Amaya Garciaaa27dfe2017-05-04 11:05:55 +0100120 typedef int64_t mbedtls_mpi_sint;
121 typedef uint64_t mbedtls_mpi_uint;
122 #elif defined(__GNUC__) && ( \
123 defined(__amd64__) || defined(__x86_64__) || \
124 defined(__ppc64__) || defined(__powerpc64__) || \
125 defined(__ia64__) || defined(__alpha__) || \
126 ( defined(__sparc__) && defined(__arch64__) ) || \
127 defined(__s390x__) || defined(__mips64) )
Andres Amaya Garciad7fce002017-07-20 11:49:32 +0100128 #if !defined(MBEDTLS_HAVE_INT64)
129 #define MBEDTLS_HAVE_INT64
130 #endif /* MBEDTLS_HAVE_INT64 */
Andres Amaya Garciaaa27dfe2017-05-04 11:05:55 +0100131 typedef int64_t mbedtls_mpi_sint;
132 typedef uint64_t mbedtls_mpi_uint;
Andres Amaya Garciad7fce002017-07-20 11:49:32 +0100133 #if !defined(MBEDTLS_NO_UDBL_DIVISION)
134 /* mbedtls_t_udbl defined as 128-bit unsigned int */
135 typedef unsigned int mbedtls_t_udbl __attribute__((mode(TI)));
136 #define MBEDTLS_HAVE_UDBL
137 #endif /* !MBEDTLS_NO_UDBL_DIVISION */
Andres Amaya Garciaaa27dfe2017-05-04 11:05:55 +0100138 #elif defined(__ARMCC_VERSION) && defined(__aarch64__)
Andres Amaya Garciad7fce002017-07-20 11:49:32 +0100139 /*
140 * __ARMCC_VERSION is defined for both armcc and armclang and
Andres Amaya Garciaaa27dfe2017-05-04 11:05:55 +0100141 * __aarch64__ is only defined by armclang when compiling 64-bit code
142 */
Andres Amaya Garciad7fce002017-07-20 11:49:32 +0100143 #if !defined(MBEDTLS_HAVE_INT64)
144 #define MBEDTLS_HAVE_INT64
145 #endif /* !MBEDTLS_HAVE_INT64 */
Andres Amaya Garciaaa27dfe2017-05-04 11:05:55 +0100146 typedef int64_t mbedtls_mpi_sint;
147 typedef uint64_t mbedtls_mpi_uint;
Andres Amaya Garciad7fce002017-07-20 11:49:32 +0100148 #if !defined(MBEDTLS_NO_UDBL_DIVISION)
149 /* mbedtls_t_udbl defined as 128-bit unsigned int */
150 typedef __uint128_t mbedtls_t_udbl;
151 #define MBEDTLS_HAVE_UDBL
152 #endif /* !MBEDTLS_NO_UDBL_DIVISION */
153 #elif defined(MBEDTLS_HAVE_INT64)
154 /* Force 64-bit integers with unknown compiler */
155 typedef int64_t mbedtls_mpi_sint;
156 typedef uint64_t mbedtls_mpi_uint;
Andres Amaya Garciaaa27dfe2017-05-04 11:05:55 +0100157 #endif
158#endif /* !MBEDTLS_HAVE_INT32 */
159
160#if !defined(MBEDTLS_HAVE_INT64)
161 /* Default to 32-bit compilation */
162 #if !defined(MBEDTLS_HAVE_INT32)
163 #define MBEDTLS_HAVE_INT32
164 #endif /* !MBEDTLS_HAVE_INT32 */
165 typedef int32_t mbedtls_mpi_sint;
166 typedef uint32_t mbedtls_mpi_uint;
Andres Amaya Garciad7fce002017-07-20 11:49:32 +0100167 #if !defined(MBEDTLS_NO_UDBL_DIVISION)
168 typedef uint64_t mbedtls_t_udbl;
Andres Amaya Garciadf1486a2017-07-20 17:33:09 +0100169 #define MBEDTLS_HAVE_UDBL
Andres Amaya Garciad7fce002017-07-20 11:49:32 +0100170 #endif /* !MBEDTLS_NO_UDBL_DIVISION */
Andres Amaya Garciaaa27dfe2017-05-04 11:05:55 +0100171#endif /* !MBEDTLS_HAVE_INT64 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000172
Paul Bakker407a0da2013-06-27 14:29:21 +0200173#ifdef __cplusplus
174extern "C" {
175#endif
176
Paul Bakker5121ce52009-01-03 21:22:43 +0000177/**
178 * \brief MPI structure
179 */
180typedef struct
181{
182 int s; /*!< integer sign */
Paul Bakker23986e52011-04-24 08:57:21 +0000183 size_t n; /*!< total # of limbs */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200184 mbedtls_mpi_uint *p; /*!< pointer to limbs */
Paul Bakker5121ce52009-01-03 21:22:43 +0000185}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200186mbedtls_mpi;
Paul Bakker5121ce52009-01-03 21:22:43 +0000187
Paul Bakker5121ce52009-01-03 21:22:43 +0000188/**
Manuel Pégourié-Gonnardda61ed32015-04-30 10:28:51 +0200189 * \brief Initialize one MPI (make internal references valid)
190 * This just makes it ready to be set or freed,
191 * but does not define a value for the MPI.
Paul Bakker6c591fa2011-05-05 11:49:20 +0000192 *
193 * \param X One MPI to initialize.
Paul Bakker5121ce52009-01-03 21:22:43 +0000194 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200195void mbedtls_mpi_init( mbedtls_mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000196
197/**
Paul Bakker6c591fa2011-05-05 11:49:20 +0000198 * \brief Unallocate one MPI
199 *
200 * \param X One MPI to unallocate.
Paul Bakker5121ce52009-01-03 21:22:43 +0000201 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200202void mbedtls_mpi_free( mbedtls_mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000203
204/**
205 * \brief Enlarge to the specified number of limbs
206 *
Gilles Peskine70ad8392018-03-21 16:28:41 +0100207 * This function does nothing if the MPI is already large enough.
208 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000209 * \param X MPI to grow
210 * \param nblimbs The target number of limbs
211 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000212 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200213 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000214 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200215int mbedtls_mpi_grow( mbedtls_mpi *X, size_t nblimbs );
Paul Bakker5121ce52009-01-03 21:22:43 +0000216
217/**
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100218 * \brief Resize down, keeping at least the specified number of limbs
219 *
Gilles Peskine70ad8392018-03-21 16:28:41 +0100220 * If \c X is smaller than \c nblimbs, it is resized up
221 * instead.
222 *
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100223 * \param X MPI to shrink
224 * \param nblimbs The minimum number of limbs to keep
225 *
226 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200227 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Gilles Peskine70ad8392018-03-21 16:28:41 +0100228 * (this can only happen when resizing up).
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100229 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200230int mbedtls_mpi_shrink( mbedtls_mpi *X, size_t nblimbs );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100231
232/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000233 * \brief Copy the contents of Y into X
234 *
Gilles Peskine70ad8392018-03-21 16:28:41 +0100235 * \param X Destination MPI. It is enlarged if necessary.
236 * \param Y Source MPI.
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000237 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000238 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200239 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000240 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200241int mbedtls_mpi_copy( mbedtls_mpi *X, const mbedtls_mpi *Y );
Paul Bakker5121ce52009-01-03 21:22:43 +0000242
243/**
244 * \brief Swap the contents of X and Y
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000245 *
246 * \param X First MPI value
247 * \param Y Second MPI value
Paul Bakker5121ce52009-01-03 21:22:43 +0000248 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200249void mbedtls_mpi_swap( mbedtls_mpi *X, mbedtls_mpi *Y );
Paul Bakker5121ce52009-01-03 21:22:43 +0000250
251/**
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100252 * \brief Safe conditional assignement X = Y if assign is 1
253 *
254 * \param X MPI to conditionally assign to
255 * \param Y Value to be assigned
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100256 * \param assign 1: perform the assignment, 0: keep X's original value
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100257 *
258 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200259 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100260 *
261 * \note This function is equivalent to
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200262 * if( assign ) mbedtls_mpi_copy( X, Y );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100263 * except that it avoids leaking any information about whether
264 * the assignment was done or not (the above code may leak
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +0100265 * information through branch prediction and/or memory access
266 * patterns analysis).
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100267 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200268int mbedtls_mpi_safe_cond_assign( mbedtls_mpi *X, const mbedtls_mpi *Y, unsigned char assign );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100269
270/**
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100271 * \brief Safe conditional swap X <-> Y if swap is 1
272 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200273 * \param X First mbedtls_mpi value
274 * \param Y Second mbedtls_mpi value
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100275 * \param assign 1: perform the swap, 0: keep X and Y's original values
276 *
277 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200278 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100279 *
280 * \note This function is equivalent to
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200281 * if( assign ) mbedtls_mpi_swap( X, Y );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100282 * except that it avoids leaking any information about whether
283 * the assignment was done or not (the above code may leak
284 * information through branch prediction and/or memory access
285 * patterns analysis).
286 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200287int mbedtls_mpi_safe_cond_swap( mbedtls_mpi *X, mbedtls_mpi *Y, unsigned char assign );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100288
289/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000290 * \brief Set value from integer
291 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000292 * \param X MPI to set
293 * \param z Value to use
294 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000295 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200296 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000297 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200298int mbedtls_mpi_lset( mbedtls_mpi *X, mbedtls_mpi_sint z );
Paul Bakker5121ce52009-01-03 21:22:43 +0000299
Paul Bakker9a736322012-11-14 12:39:52 +0000300/**
Paul Bakker2f5947e2011-05-18 15:47:11 +0000301 * \brief Get a specific bit from X
302 *
303 * \param X MPI to use
304 * \param pos Zero-based index of the bit in X
305 *
306 * \return Either a 0 or a 1
307 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200308int mbedtls_mpi_get_bit( const mbedtls_mpi *X, size_t pos );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000309
Paul Bakker9a736322012-11-14 12:39:52 +0000310/**
Paul Bakker2f5947e2011-05-18 15:47:11 +0000311 * \brief Set a bit of X to a specific value of 0 or 1
312 *
313 * \note Will grow X if necessary to set a bit to 1 in a not yet
314 * existing limb. Will not grow if bit should be set to 0
315 *
316 * \param X MPI to use
317 * \param pos Zero-based index of the bit in X
318 * \param val The value to set the bit to (0 or 1)
319 *
320 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200321 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200322 * MBEDTLS_ERR_MPI_BAD_INPUT_DATA if val is not 0 or 1
Paul Bakker2f5947e2011-05-18 15:47:11 +0000323 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200324int mbedtls_mpi_set_bit( mbedtls_mpi *X, size_t pos, unsigned char val );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000325
Paul Bakker5121ce52009-01-03 21:22:43 +0000326/**
Paul Bakker6b906e52012-05-08 12:01:43 +0000327 * \brief Return the number of zero-bits before the least significant
328 * '1' bit
329 *
330 * Note: Thus also the zero-based index of the least significant '1' bit
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000331 *
332 * \param X MPI to use
Paul Bakker5121ce52009-01-03 21:22:43 +0000333 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200334size_t mbedtls_mpi_lsb( const mbedtls_mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000335
336/**
Paul Bakker6b906e52012-05-08 12:01:43 +0000337 * \brief Return the number of bits up to and including the most
338 * significant '1' bit'
339 *
340 * Note: Thus also the one-based index of the most significant '1' bit
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000341 *
342 * \param X MPI to use
Paul Bakker5121ce52009-01-03 21:22:43 +0000343 */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200344size_t mbedtls_mpi_bitlen( const mbedtls_mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000345
346/**
347 * \brief Return the total size in bytes
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000348 *
349 * \param X MPI to use
Paul Bakker5121ce52009-01-03 21:22:43 +0000350 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200351size_t mbedtls_mpi_size( const mbedtls_mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000352
353/**
354 * \brief Import from an ASCII string
355 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000356 * \param X Destination MPI
357 * \param radix Input numeric base
358 * \param s Null-terminated string buffer
Paul Bakker5121ce52009-01-03 21:22:43 +0000359 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200360 * \return 0 if successful, or a MBEDTLS_ERR_MPI_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000361 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200362int mbedtls_mpi_read_string( mbedtls_mpi *X, int radix, const char *s );
Paul Bakker5121ce52009-01-03 21:22:43 +0000363
364/**
365 * \brief Export into an ASCII string
366 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000367 * \param X Source MPI
368 * \param radix Output numeric base
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100369 * \param buf Buffer to write the string to
370 * \param buflen Length of buf
371 * \param olen Length of the string written, including final NUL byte
Paul Bakker5121ce52009-01-03 21:22:43 +0000372 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200373 * \return 0 if successful, or a MBEDTLS_ERR_MPI_XXX error code.
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100374 * *olen is always updated to reflect the amount
Paul Bakkerff60ee62010-03-16 21:09:09 +0000375 * of data that has (or would have) been written.
Paul Bakker5121ce52009-01-03 21:22:43 +0000376 *
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100377 * \note Call this function with buflen = 0 to obtain the
378 * minimum required buffer size in *olen.
Paul Bakker5121ce52009-01-03 21:22:43 +0000379 */
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100380int mbedtls_mpi_write_string( const mbedtls_mpi *X, int radix,
381 char *buf, size_t buflen, size_t *olen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000382
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200383#if defined(MBEDTLS_FS_IO)
Paul Bakker5121ce52009-01-03 21:22:43 +0000384/**
Hanno Beckerb2034b72017-04-26 11:46:46 +0100385 * \brief Read MPI from a line in an opened file
Paul Bakker5121ce52009-01-03 21:22:43 +0000386 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000387 * \param X Destination MPI
388 * \param radix Input numeric base
389 * \param fin Input file handle
Paul Bakker5121ce52009-01-03 21:22:43 +0000390 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200391 * \return 0 if successful, MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if
Paul Bakkercb37aa52011-11-30 16:00:20 +0000392 * the file read buffer is too small or a
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200393 * MBEDTLS_ERR_MPI_XXX error code
Hanno Beckerb2034b72017-04-26 11:46:46 +0100394 *
395 * \note On success, this function advances the file stream
396 * to the end of the current line or to EOF.
397 *
398 * The function returns 0 on an empty line.
399 *
400 * Leading whitespaces are ignored, as is a
401 * '0x' prefix for radix 16.
402 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000403 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200404int mbedtls_mpi_read_file( mbedtls_mpi *X, int radix, FILE *fin );
Paul Bakker5121ce52009-01-03 21:22:43 +0000405
406/**
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000407 * \brief Write X into an opened file, or stdout if fout is NULL
Paul Bakker5121ce52009-01-03 21:22:43 +0000408 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000409 * \param p Prefix, can be NULL
410 * \param X Source MPI
411 * \param radix Output numeric base
412 * \param fout Output file handle (can be NULL)
Paul Bakker5121ce52009-01-03 21:22:43 +0000413 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200414 * \return 0 if successful, or a MBEDTLS_ERR_MPI_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000415 *
416 * \note Set fout == NULL to print X on the console.
417 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200418int mbedtls_mpi_write_file( const char *p, const mbedtls_mpi *X, int radix, FILE *fout );
419#endif /* MBEDTLS_FS_IO */
Paul Bakker5121ce52009-01-03 21:22:43 +0000420
421/**
422 * \brief Import X from unsigned binary data, big endian
423 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000424 * \param X Destination MPI
425 * \param buf Input buffer
426 * \param buflen Input buffer size
Paul Bakker5121ce52009-01-03 21:22:43 +0000427 *
428 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200429 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000430 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200431int mbedtls_mpi_read_binary( mbedtls_mpi *X, const unsigned char *buf, size_t buflen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000432
433/**
Manuel Pégourié-Gonnard3926a2c2014-06-25 12:57:47 +0200434 * \brief Export X into unsigned binary data, big endian.
435 * Always fills the whole buffer, which will start with zeros
436 * if the number is smaller.
Paul Bakker5121ce52009-01-03 21:22:43 +0000437 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000438 * \param X Source MPI
439 * \param buf Output buffer
440 * \param buflen Output buffer size
Paul Bakker5121ce52009-01-03 21:22:43 +0000441 *
442 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200443 * MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if buf isn't large enough
Paul Bakker5121ce52009-01-03 21:22:43 +0000444 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200445int mbedtls_mpi_write_binary( const mbedtls_mpi *X, unsigned char *buf, size_t buflen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000446
447/**
448 * \brief Left-shift: X <<= count
449 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000450 * \param X MPI to shift
451 * \param count Amount to shift
452 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000453 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200454 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000455 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200456int mbedtls_mpi_shift_l( mbedtls_mpi *X, size_t count );
Paul Bakker5121ce52009-01-03 21:22:43 +0000457
458/**
459 * \brief Right-shift: X >>= count
460 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000461 * \param X MPI to shift
462 * \param count Amount to shift
463 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000464 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200465 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000466 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200467int mbedtls_mpi_shift_r( mbedtls_mpi *X, size_t count );
Paul Bakker5121ce52009-01-03 21:22:43 +0000468
469/**
470 * \brief Compare unsigned values
471 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000472 * \param X Left-hand MPI
473 * \param Y Right-hand MPI
474 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000475 * \return 1 if |X| is greater than |Y|,
476 * -1 if |X| is lesser than |Y| or
477 * 0 if |X| is equal to |Y|
478 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200479int mbedtls_mpi_cmp_abs( const mbedtls_mpi *X, const mbedtls_mpi *Y );
Paul Bakker5121ce52009-01-03 21:22:43 +0000480
481/**
482 * \brief Compare signed values
483 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000484 * \param X Left-hand MPI
485 * \param Y Right-hand MPI
486 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000487 * \return 1 if X is greater than Y,
488 * -1 if X is lesser than Y or
489 * 0 if X is equal to Y
490 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200491int mbedtls_mpi_cmp_mpi( const mbedtls_mpi *X, const mbedtls_mpi *Y );
Paul Bakker5121ce52009-01-03 21:22:43 +0000492
493/**
494 * \brief Compare signed values
495 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000496 * \param X Left-hand MPI
497 * \param z The integer value to compare to
498 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000499 * \return 1 if X is greater than z,
500 * -1 if X is lesser than z or
501 * 0 if X is equal to z
502 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200503int mbedtls_mpi_cmp_int( const mbedtls_mpi *X, mbedtls_mpi_sint z );
Paul Bakker5121ce52009-01-03 21:22:43 +0000504
505/**
506 * \brief Unsigned addition: X = |A| + |B|
507 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000508 * \param X Destination MPI
509 * \param A Left-hand MPI
510 * \param B Right-hand MPI
511 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000512 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200513 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000514 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200515int mbedtls_mpi_add_abs( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000516
517/**
Paul Bakker60b1d102013-10-29 10:02:51 +0100518 * \brief Unsigned subtraction: X = |A| - |B|
Paul Bakker5121ce52009-01-03 21:22:43 +0000519 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000520 * \param X Destination MPI
521 * \param A Left-hand MPI
522 * \param B Right-hand MPI
523 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000524 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200525 * MBEDTLS_ERR_MPI_NEGATIVE_VALUE if B is greater than A
Paul Bakker5121ce52009-01-03 21:22:43 +0000526 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200527int mbedtls_mpi_sub_abs( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000528
529/**
530 * \brief Signed addition: X = A + B
531 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000532 * \param X Destination MPI
533 * \param A Left-hand MPI
534 * \param B Right-hand MPI
535 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000536 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200537 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000538 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200539int mbedtls_mpi_add_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000540
541/**
Paul Bakker60b1d102013-10-29 10:02:51 +0100542 * \brief Signed subtraction: X = A - B
Paul Bakker5121ce52009-01-03 21:22:43 +0000543 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000544 * \param X Destination MPI
545 * \param A Left-hand MPI
546 * \param B Right-hand MPI
547 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000548 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200549 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000550 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200551int mbedtls_mpi_sub_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000552
553/**
554 * \brief Signed addition: X = A + b
555 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000556 * \param X Destination MPI
557 * \param A Left-hand MPI
558 * \param b The integer value to add
559 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000560 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200561 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000562 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200563int mbedtls_mpi_add_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000564
565/**
Paul Bakker60b1d102013-10-29 10:02:51 +0100566 * \brief Signed subtraction: X = A - b
Paul Bakker5121ce52009-01-03 21:22:43 +0000567 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000568 * \param X Destination MPI
569 * \param A Left-hand MPI
570 * \param b The integer value to subtract
571 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000572 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200573 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000574 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200575int mbedtls_mpi_sub_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000576
577/**
578 * \brief Baseline multiplication: X = A * B
579 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000580 * \param X Destination MPI
581 * \param A Left-hand MPI
582 * \param B Right-hand MPI
583 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000584 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200585 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000586 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200587int mbedtls_mpi_mul_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000588
589/**
590 * \brief Baseline multiplication: X = A * b
591 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000592 * \param X Destination MPI
593 * \param A Left-hand MPI
Manuel Pégourié-Gonnard35f1d7f2015-03-19 12:42:40 +0000594 * \param b The unsigned integer value to multiply with
595 *
596 * \note b is unsigned
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000597 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000598 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200599 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000600 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200601int mbedtls_mpi_mul_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_uint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000602
603/**
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200604 * \brief Division by mbedtls_mpi: A = Q * B + R
Paul Bakker5121ce52009-01-03 21:22:43 +0000605 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000606 * \param Q Destination MPI for the quotient
607 * \param R Destination MPI for the rest value
608 * \param A Left-hand MPI
609 * \param B Right-hand MPI
610 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000611 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200612 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200613 * MBEDTLS_ERR_MPI_DIVISION_BY_ZERO if B == 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000614 *
615 * \note Either Q or R can be NULL.
616 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200617int 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 +0000618
619/**
620 * \brief Division by int: A = Q * b + R
621 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000622 * \param Q Destination MPI for the quotient
623 * \param R Destination MPI for the rest value
624 * \param A Left-hand MPI
625 * \param b Integer to divide by
626 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000627 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200628 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200629 * MBEDTLS_ERR_MPI_DIVISION_BY_ZERO if b == 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000630 *
631 * \note Either Q or R can be NULL.
632 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200633int 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 +0000634
635/**
636 * \brief Modulo: R = A mod B
637 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000638 * \param R Destination MPI for the rest value
639 * \param A Left-hand MPI
640 * \param B Right-hand MPI
641 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000642 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200643 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200644 * MBEDTLS_ERR_MPI_DIVISION_BY_ZERO if B == 0,
645 * MBEDTLS_ERR_MPI_NEGATIVE_VALUE if B < 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000646 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200647int mbedtls_mpi_mod_mpi( mbedtls_mpi *R, const mbedtls_mpi *A, const mbedtls_mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000648
649/**
650 * \brief Modulo: r = A mod b
651 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200652 * \param r Destination mbedtls_mpi_uint
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000653 * \param A Left-hand MPI
654 * \param b Integer to divide by
655 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000656 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200657 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200658 * MBEDTLS_ERR_MPI_DIVISION_BY_ZERO if b == 0,
659 * MBEDTLS_ERR_MPI_NEGATIVE_VALUE if b < 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000660 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200661int mbedtls_mpi_mod_int( mbedtls_mpi_uint *r, const mbedtls_mpi *A, mbedtls_mpi_sint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000662
663/**
664 * \brief Sliding-window exponentiation: X = A^E mod N
665 *
Paul Bakker9af723c2014-05-01 13:03:14 +0200666 * \param X Destination MPI
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000667 * \param A Left-hand MPI
668 * \param E Exponent MPI
669 * \param N Modular MPI
670 * \param _RR Speed-up MPI used for recalculations
671 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000672 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200673 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200674 * MBEDTLS_ERR_MPI_BAD_INPUT_DATA if N is negative or even or
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200675 * if E is negative
Paul Bakker5121ce52009-01-03 21:22:43 +0000676 *
677 * \note _RR is used to avoid re-computing R*R mod N across
678 * multiple calls, which speeds up things a bit. It can
679 * be set to NULL if the extra performance is unneeded.
680 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200681int 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 +0000682
683/**
Paul Bakker287781a2011-03-26 13:18:49 +0000684 * \brief Fill an MPI X with size bytes of random
685 *
686 * \param X Destination MPI
687 * \param size Size in bytes
688 * \param f_rng RNG function
689 * \param p_rng RNG parameter
690 *
691 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200692 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Hanno Becker15f2b3e2017-10-17 15:17:05 +0100693 *
694 * \note The bytes obtained from the PRNG are interpreted
695 * as a big-endian representation of an MPI; this can
696 * be relevant in applications like deterministic ECDSA.
Paul Bakker287781a2011-03-26 13:18:49 +0000697 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200698int mbedtls_mpi_fill_random( mbedtls_mpi *X, size_t size,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000699 int (*f_rng)(void *, unsigned char *, size_t),
700 void *p_rng );
Paul Bakker287781a2011-03-26 13:18:49 +0000701
702/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000703 * \brief Greatest common divisor: G = gcd(A, B)
704 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000705 * \param G Destination MPI
706 * \param A Left-hand MPI
707 * \param B Right-hand MPI
708 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000709 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200710 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000711 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200712int mbedtls_mpi_gcd( mbedtls_mpi *G, const mbedtls_mpi *A, const mbedtls_mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000713
714/**
715 * \brief Modular inverse: X = A^-1 mod N
716 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000717 * \param X Destination MPI
718 * \param A Left-hand MPI
719 * \param N Right-hand MPI
720 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000721 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200722 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
Hanno Becker4bcb4912017-04-18 15:49:39 +0100723 * MBEDTLS_ERR_MPI_BAD_INPUT_DATA if N is <= 1,
724 MBEDTLS_ERR_MPI_NOT_ACCEPTABLE if A has no inverse mod N.
Paul Bakker5121ce52009-01-03 21:22:43 +0000725 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200726int mbedtls_mpi_inv_mod( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *N );
Paul Bakker5121ce52009-01-03 21:22:43 +0000727
728/**
729 * \brief Miller-Rabin primality test
730 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000731 * \param X MPI to check
732 * \param f_rng RNG function
733 * \param p_rng RNG parameter
734 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000735 * \return 0 if successful (probably prime),
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200736 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200737 * MBEDTLS_ERR_MPI_NOT_ACCEPTABLE if X is not prime
Paul Bakker5121ce52009-01-03 21:22:43 +0000738 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200739int mbedtls_mpi_is_prime( const mbedtls_mpi *X,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000740 int (*f_rng)(void *, unsigned char *, size_t),
741 void *p_rng );
Paul Bakker5121ce52009-01-03 21:22:43 +0000742
743/**
744 * \brief Prime number generation
745 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000746 * \param X Destination MPI
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200747 * \param nbits Required size of X in bits
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200748 * ( 3 <= nbits <= MBEDTLS_MPI_MAX_BITS )
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000749 * \param dh_flag If 1, then (X-1)/2 will be prime too
Paul Bakker5121ce52009-01-03 21:22:43 +0000750 * \param f_rng RNG function
751 * \param p_rng RNG parameter
752 *
753 * \return 0 if successful (probably prime),
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200754 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200755 * MBEDTLS_ERR_MPI_BAD_INPUT_DATA if nbits is < 3
Paul Bakker5121ce52009-01-03 21:22:43 +0000756 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200757int mbedtls_mpi_gen_prime( mbedtls_mpi *X, size_t nbits, int dh_flag,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000758 int (*f_rng)(void *, unsigned char *, size_t),
759 void *p_rng );
Paul Bakker5121ce52009-01-03 21:22:43 +0000760
Ron Eldorfa8f6352017-06-20 15:48:46 +0300761#if defined(MBEDTLS_SELF_TEST)
762
Paul Bakker5121ce52009-01-03 21:22:43 +0000763/**
764 * \brief Checkup routine
765 *
766 * \return 0 if successful, or 1 if the test failed
767 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200768int mbedtls_mpi_self_test( int verbose );
Paul Bakker5121ce52009-01-03 21:22:43 +0000769
Ron Eldorfa8f6352017-06-20 15:48:46 +0300770#endif /* MBEDTLS_SELF_TEST */
771
Paul Bakker5121ce52009-01-03 21:22:43 +0000772#ifdef __cplusplus
773}
774#endif
775
776#endif /* bignum.h */