blob: 8e64923005430eac8b45dc4f6f5fd4aa038e5b6b [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/*
Bence Szépkúti1e148272020-08-07 13:07:28 +02007 * Copyright The Mbed TLS Contributors
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 Bakker5121ce52009-01-03 21:22:43 +000021 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#ifndef MBEDTLS_BIGNUM_H
23#define MBEDTLS_BIGNUM_H
Mateusz Starzyk846f0212021-05-19 19:44:07 +020024#include "mbedtls/private_access.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000025
Bence Szépkútic662b362021-05-27 11:25:03 +020026#include "mbedtls/build_info.h"
Paul Bakkercf0360a2012-01-20 10:08:14 +000027
Rich Evans00ab4702015-02-06 13:43:58 +000028#include <stddef.h>
Manuel Pégourié-Gonnardab229102015-04-15 11:53:16 +020029#include <stdint.h>
Rich Evans00ab4702015-02-06 13:43:58 +000030
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020031#if defined(MBEDTLS_FS_IO)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020032# include <stdio.h>
Manuel Pégourié-Gonnarde94e6e52015-01-19 15:01:53 +000033#endif
34
Gilles Peskined2971572021-07-26 18:48:10 +020035/** An error occurred while reading from or writing to a file. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020036#define MBEDTLS_ERR_MPI_FILE_IO_ERROR -0x0002
Gilles Peskined2971572021-07-26 18:48:10 +020037/** Bad input parameters to function. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020038#define MBEDTLS_ERR_MPI_BAD_INPUT_DATA -0x0004
Gilles Peskined2971572021-07-26 18:48:10 +020039/** There is an invalid character in the digit string. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020040#define MBEDTLS_ERR_MPI_INVALID_CHARACTER -0x0006
Gilles Peskined2971572021-07-26 18:48:10 +020041/** The buffer is too small to write to. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020042#define MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL -0x0008
Gilles Peskined2971572021-07-26 18:48:10 +020043/** The input arguments are negative or result in illegal output. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020044#define MBEDTLS_ERR_MPI_NEGATIVE_VALUE -0x000A
Gilles Peskined2971572021-07-26 18:48:10 +020045/** The input argument for division is zero, which is not allowed. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020046#define MBEDTLS_ERR_MPI_DIVISION_BY_ZERO -0x000C
Gilles Peskined2971572021-07-26 18:48:10 +020047/** The input arguments are not acceptable. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020048#define MBEDTLS_ERR_MPI_NOT_ACCEPTABLE -0x000E
Gilles Peskined2971572021-07-26 18:48:10 +020049/** Memory allocation failed. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020050#define MBEDTLS_ERR_MPI_ALLOC_FAILED -0x0010
Paul Bakker5121ce52009-01-03 21:22:43 +000051
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020052#define MBEDTLS_MPI_CHK(f) \
53 do { \
54 if ((ret = (f)) != 0) \
55 goto cleanup; \
56 } while (0)
Paul Bakker5121ce52009-01-03 21:22:43 +000057
58/*
Paul Bakkerf9688572011-05-05 10:00:45 +000059 * Maximum size MPIs are allowed to grow to in number of limbs.
60 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020061#define MBEDTLS_MPI_MAX_LIMBS 10000
Paul Bakkerf9688572011-05-05 10:00:45 +000062
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020063#if !defined(MBEDTLS_MPI_WINDOW_SIZE)
Paul Bakkerf9688572011-05-05 10:00:45 +000064/*
Paul Bakkerb6d5f082011-11-25 11:52:11 +000065 * Maximum window size used for modular exponentiation. Default: 6
66 * Minimum value: 1. Maximum value: 6.
67 *
Daniel Otte60861512020-09-07 13:07:14 +020068 * Result is an array of ( 2 ** MBEDTLS_MPI_WINDOW_SIZE ) MPIs used
Paul Bakkerb6d5f082011-11-25 11:52:11 +000069 * for the sliding window calculation. (So 64 by default)
70 *
71 * Reduction in size, reduces speed.
72 */
Mateusz Starzyk16fec332021-07-22 16:43:35 +020073/** Maximum window size used. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020074# define MBEDTLS_MPI_WINDOW_SIZE 6
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020075#endif /* !MBEDTLS_MPI_WINDOW_SIZE */
Paul Bakkerb6d5f082011-11-25 11:52:11 +000076
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020077#if !defined(MBEDTLS_MPI_MAX_SIZE)
Paul Bakkerb6d5f082011-11-25 11:52:11 +000078/*
Paul Bakkerfe3256e2011-11-25 12:11:43 +000079 * Maximum size of MPIs allowed in bits and bytes for user-MPIs.
Paul Bakkerf626e1d2013-01-21 12:10:00 +010080 * ( Default: 512 bytes => 4096 bits, Maximum tested: 2048 bytes => 16384 bits )
Paul Bakkerfe3256e2011-11-25 12:11:43 +000081 *
Hanno Beckerefeef6c2018-01-05 08:07:47 +000082 * Note: Calculations can temporarily result in larger MPIs. So the number
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020083 * of limbs required (MBEDTLS_MPI_MAX_LIMBS) is higher.
Paul Bakkerfe3256e2011-11-25 12:11:43 +000084 */
Mateusz Starzyk16fec332021-07-22 16:43:35 +020085/** Maximum number of bytes for usable MPIs. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020086# define MBEDTLS_MPI_MAX_SIZE 1024
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020087#endif /* !MBEDTLS_MPI_MAX_SIZE */
Paul Bakker9bcf16c2013-06-24 19:31:17 +020088
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020089#define MBEDTLS_MPI_MAX_BITS \
90 (8 * MBEDTLS_MPI_MAX_SIZE) /**< Maximum number of bits for usable MPIs. */
Paul Bakkerfe3256e2011-11-25 12:11:43 +000091
92/*
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020093 * When reading from files with mbedtls_mpi_read_file() and writing to files
94 * with mbedtls_mpi_write_file() the buffer should have space for a (short)
95 * label, the MPI (in the provided radix), the newline characters and the '\0'.
Paul Bakkercb37aa52011-11-30 16:00:20 +000096 *
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 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +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 \
112 (((MBEDTLS_MPI_MAX_BITS_SCALE100 + MBEDTLS_LN_2_DIV_LN_10_SCALE100 - 1) / \
113 MBEDTLS_LN_2_DIV_LN_10_SCALE100) + \
114 10 + 6)
Paul Bakkercb37aa52011-11-30 16:00:20 +0000115
116/*
Manuel Pégourié-Gonnard7b538892015-04-09 17:00:17 +0200117 * Define the base integer type, architecture-wise.
118 *
Andres Amaya Garciad7fce002017-07-20 11:49:32 +0100119 * 32 or 64-bit integer types can be forced regardless of the underlying
120 * architecture by defining MBEDTLS_HAVE_INT32 or MBEDTLS_HAVE_INT64
121 * respectively and undefining MBEDTLS_HAVE_ASM.
122 *
Andres Amaya Garcia93db11a2017-07-20 12:11:19 +0100123 * Double-width integers (e.g. 128-bit in 64-bit architectures) can be
Andres Amaya Garciad7fce002017-07-20 11:49:32 +0100124 * disabled by defining MBEDTLS_NO_UDBL_DIVISION.
Paul Bakker5121ce52009-01-03 21:22:43 +0000125 */
Andres Amaya Garciaaa27dfe2017-05-04 11:05:55 +0100126#if !defined(MBEDTLS_HAVE_INT32)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200127# if defined(_MSC_VER) && defined(_M_AMD64)
128/* Always choose 64-bit when using MSC */
129# if !defined(MBEDTLS_HAVE_INT64)
130# define MBEDTLS_HAVE_INT64
131# endif /* !MBEDTLS_HAVE_INT64 */
132typedef int64_t mbedtls_mpi_sint;
133typedef uint64_t mbedtls_mpi_uint;
134# elif defined(__GNUC__) && \
135 (defined(__amd64__) || defined(__x86_64__) || defined(__ppc64__) || \
136 defined(__powerpc64__) || defined(__ia64__) || defined(__alpha__) || \
137 (defined(__sparc__) && defined(__arch64__)) || defined(__s390x__) || \
138 defined(__mips64) || defined(__aarch64__))
139# if !defined(MBEDTLS_HAVE_INT64)
140# define MBEDTLS_HAVE_INT64
141# endif /* MBEDTLS_HAVE_INT64 */
142typedef int64_t mbedtls_mpi_sint;
143typedef uint64_t mbedtls_mpi_uint;
144# if !defined(MBEDTLS_NO_UDBL_DIVISION)
145/* mbedtls_t_udbl defined as 128-bit unsigned int */
146typedef unsigned int mbedtls_t_udbl __attribute__((mode(TI)));
147# define MBEDTLS_HAVE_UDBL
148# endif /* !MBEDTLS_NO_UDBL_DIVISION */
149# elif defined(__ARMCC_VERSION) && defined(__aarch64__)
150/*
151 * __ARMCC_VERSION is defined for both armcc and armclang and
152 * __aarch64__ is only defined by armclang when compiling 64-bit code
153 */
154# if !defined(MBEDTLS_HAVE_INT64)
155# define MBEDTLS_HAVE_INT64
156# endif /* !MBEDTLS_HAVE_INT64 */
157typedef int64_t mbedtls_mpi_sint;
158typedef uint64_t mbedtls_mpi_uint;
159# if !defined(MBEDTLS_NO_UDBL_DIVISION)
160/* mbedtls_t_udbl defined as 128-bit unsigned int */
161typedef __uint128_t mbedtls_t_udbl;
162# define MBEDTLS_HAVE_UDBL
163# endif /* !MBEDTLS_NO_UDBL_DIVISION */
164# elif defined(MBEDTLS_HAVE_INT64)
165/* Force 64-bit integers with unknown compiler */
166typedef int64_t mbedtls_mpi_sint;
167typedef uint64_t mbedtls_mpi_uint;
168# endif
Andres Amaya Garciaaa27dfe2017-05-04 11:05:55 +0100169#endif /* !MBEDTLS_HAVE_INT32 */
170
171#if !defined(MBEDTLS_HAVE_INT64)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200172/* Default to 32-bit compilation */
173# if !defined(MBEDTLS_HAVE_INT32)
174# define MBEDTLS_HAVE_INT32
175# endif /* !MBEDTLS_HAVE_INT32 */
176typedef int32_t mbedtls_mpi_sint;
177typedef uint32_t mbedtls_mpi_uint;
178# if !defined(MBEDTLS_NO_UDBL_DIVISION)
179typedef uint64_t mbedtls_t_udbl;
180# define MBEDTLS_HAVE_UDBL
181# endif /* !MBEDTLS_NO_UDBL_DIVISION */
Andres Amaya Garciaaa27dfe2017-05-04 11:05:55 +0100182#endif /* !MBEDTLS_HAVE_INT64 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000183
Paul Bakker407a0da2013-06-27 14:29:21 +0200184#ifdef __cplusplus
185extern "C" {
186#endif
187
Paul Bakker5121ce52009-01-03 21:22:43 +0000188/**
189 * \brief MPI structure
190 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200191typedef struct mbedtls_mpi {
192 int MBEDTLS_PRIVATE(s); /*!< Sign: -1 if the mpi is negative, 1 otherwise
193 */
194 size_t MBEDTLS_PRIVATE(n); /*!< total # of limbs */
195 mbedtls_mpi_uint *MBEDTLS_PRIVATE(p); /*!< pointer to limbs */
196} mbedtls_mpi;
Paul Bakker5121ce52009-01-03 21:22:43 +0000197
Paul Bakker5121ce52009-01-03 21:22:43 +0000198/**
Hanno Beckerc23483e2018-12-10 17:21:19 +0000199 * \brief Initialize an MPI context.
200 *
201 * This makes the MPI ready to be set or freed,
Manuel Pégourié-Gonnardda61ed32015-04-30 10:28:51 +0200202 * but does not define a value for the MPI.
Paul Bakker6c591fa2011-05-05 11:49:20 +0000203 *
Hanno Becker8282c2f2018-12-12 13:36:46 +0000204 * \param X The MPI context to initialize. This must not be \c NULL.
Paul Bakker5121ce52009-01-03 21:22:43 +0000205 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200206void mbedtls_mpi_init(mbedtls_mpi *X);
Paul Bakker5121ce52009-01-03 21:22:43 +0000207
208/**
Hanno Becker8ce11a32018-12-19 16:18:52 +0000209 * \brief This function frees the components of an MPI context.
Paul Bakker6c591fa2011-05-05 11:49:20 +0000210 *
Hanno Beckere1185042018-12-13 14:31:46 +0000211 * \param X The MPI context to be cleared. This may be \c NULL,
Hanno Becker8282c2f2018-12-12 13:36:46 +0000212 * in which case this function is a no-op. If it is
213 * not \c NULL, it must point to an initialized MPI.
Paul Bakker5121ce52009-01-03 21:22:43 +0000214 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200215void mbedtls_mpi_free(mbedtls_mpi *X);
Paul Bakker5121ce52009-01-03 21:22:43 +0000216
217/**
Hanno Beckerc23483e2018-12-10 17:21:19 +0000218 * \brief Enlarge an MPI to the specified number of limbs.
Paul Bakker5121ce52009-01-03 21:22:43 +0000219 *
Hanno Beckerc23483e2018-12-10 17:21:19 +0000220 * \note This function does nothing if the MPI is
221 * already large enough.
Gilles Peskine70ad8392018-03-21 16:28:41 +0100222 *
Hanno Becker8282c2f2018-12-12 13:36:46 +0000223 * \param X The MPI to grow. It must be initialized.
Hanno Beckerc23483e2018-12-10 17:21:19 +0000224 * \param nblimbs The target number of limbs.
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000225 *
Hanno Beckerc23483e2018-12-10 17:21:19 +0000226 * \return \c 0 if successful.
227 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed.
228 * \return Another negative error code on other kinds of failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000229 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200230int mbedtls_mpi_grow(mbedtls_mpi *X, size_t nblimbs);
Paul Bakker5121ce52009-01-03 21:22:43 +0000231
232/**
Hanno Beckere1185042018-12-13 14:31:46 +0000233 * \brief This function resizes an MPI downwards, keeping at least the
234 * specified number of limbs.
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100235 *
Gilles Peskine70ad8392018-03-21 16:28:41 +0100236 * If \c X is smaller than \c nblimbs, it is resized up
237 * instead.
238 *
Hanno Becker8282c2f2018-12-12 13:36:46 +0000239 * \param X The MPI to shrink. This must point to an initialized MPI.
Hanno Beckerc23483e2018-12-10 17:21:19 +0000240 * \param nblimbs The minimum number of limbs to keep.
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100241 *
Hanno Beckerc23483e2018-12-10 17:21:19 +0000242 * \return \c 0 if successful.
243 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Gilles Peskine70ad8392018-03-21 16:28:41 +0100244 * (this can only happen when resizing up).
Hanno Beckerc23483e2018-12-10 17:21:19 +0000245 * \return Another negative error code on other kinds of failure.
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100246 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200247int mbedtls_mpi_shrink(mbedtls_mpi *X, size_t nblimbs);
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100248
249/**
Hanno Beckerc23483e2018-12-10 17:21:19 +0000250 * \brief Make a copy of an MPI.
Paul Bakker5121ce52009-01-03 21:22:43 +0000251 *
Hanno Becker8282c2f2018-12-12 13:36:46 +0000252 * \param X The destination MPI. This must point to an initialized MPI.
253 * \param Y The source MPI. This must point to an initialized MPI.
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000254 *
Hanno Beckerc23483e2018-12-10 17:21:19 +0000255 * \note The limb-buffer in the destination MPI is enlarged
256 * if necessary to hold the value in the source MPI.
257 *
258 * \return \c 0 if successful.
259 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed.
260 * \return Another negative error code on other kinds of failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000261 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200262int mbedtls_mpi_copy(mbedtls_mpi *X, const mbedtls_mpi *Y);
Paul Bakker5121ce52009-01-03 21:22:43 +0000263
264/**
Hanno Beckerc23483e2018-12-10 17:21:19 +0000265 * \brief Swap the contents of two MPIs.
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000266 *
Hanno Becker8282c2f2018-12-12 13:36:46 +0000267 * \param X The first MPI. It must be initialized.
268 * \param Y The second MPI. It must be initialized.
Paul Bakker5121ce52009-01-03 21:22:43 +0000269 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200270void mbedtls_mpi_swap(mbedtls_mpi *X, mbedtls_mpi *Y);
Paul Bakker5121ce52009-01-03 21:22:43 +0000271
272/**
Hanno Beckere1185042018-12-13 14:31:46 +0000273 * \brief Perform a safe conditional copy of MPI which doesn't
274 * reveal whether the condition was true or not.
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100275 *
Hanno Becker8282c2f2018-12-12 13:36:46 +0000276 * \param X The MPI to conditionally assign to. This must point
277 * to an initialized MPI.
278 * \param Y The MPI to be assigned from. This must point to an
279 * initialized MPI.
Hanno Beckere1185042018-12-13 14:31:46 +0000280 * \param assign The condition deciding whether to perform the
Hanno Beckerc23483e2018-12-10 17:21:19 +0000281 * assignment or not. Possible values:
282 * * \c 1: Perform the assignment `X = Y`.
283 * * \c 0: Keep the original value of \p X.
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100284 *
285 * \note This function is equivalent to
Hanno Beckerc23483e2018-12-10 17:21:19 +0000286 * `if( assign ) mbedtls_mpi_copy( X, Y );`
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100287 * except that it avoids leaking any information about whether
288 * the assignment was done or not (the above code may leak
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +0100289 * information through branch prediction and/or memory access
290 * patterns analysis).
Hanno Beckerc23483e2018-12-10 17:21:19 +0000291 *
292 * \return \c 0 if successful.
293 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed.
294 * \return Another negative error code on other kinds of failure.
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100295 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200296int mbedtls_mpi_safe_cond_assign(mbedtls_mpi *X,
297 const mbedtls_mpi *Y,
298 unsigned char assign);
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100299
300/**
Hanno Beckere1185042018-12-13 14:31:46 +0000301 * \brief Perform a safe conditional swap which doesn't
302 * reveal whether the condition was true or not.
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100303 *
Hanno Becker8282c2f2018-12-12 13:36:46 +0000304 * \param X The first MPI. This must be initialized.
305 * \param Y The second MPI. This must be initialized.
Hanno Beckere1185042018-12-13 14:31:46 +0000306 * \param assign The condition deciding whether to perform
Hanno Becker8282c2f2018-12-12 13:36:46 +0000307 * the swap or not. Possible values:
Hanno Beckerc23483e2018-12-10 17:21:19 +0000308 * * \c 1: Swap the values of \p X and \p Y.
309 * * \c 0: Keep the original values of \p X and \p Y.
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100310 *
311 * \note This function is equivalent to
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200312 * if( assign ) mbedtls_mpi_swap( X, Y );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100313 * except that it avoids leaking any information about whether
314 * the assignment was done or not (the above code may leak
315 * information through branch prediction and/or memory access
316 * patterns analysis).
Hanno Beckerc23483e2018-12-10 17:21:19 +0000317 *
318 * \return \c 0 if successful.
319 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed.
320 * \return Another negative error code on other kinds of failure.
321 *
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100322 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200323int mbedtls_mpi_safe_cond_swap(mbedtls_mpi *X,
324 mbedtls_mpi *Y,
325 unsigned char assign);
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100326
327/**
Hanno Beckerc23483e2018-12-10 17:21:19 +0000328 * \brief Store integer value in MPI.
Paul Bakker5121ce52009-01-03 21:22:43 +0000329 *
Hanno Becker8282c2f2018-12-12 13:36:46 +0000330 * \param X The MPI to set. This must be initialized.
Hanno Beckerc23483e2018-12-10 17:21:19 +0000331 * \param z The value to use.
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000332 *
Hanno Beckerc23483e2018-12-10 17:21:19 +0000333 * \return \c 0 if successful.
334 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed.
335 * \return Another negative error code on other kinds of failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000336 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200337int mbedtls_mpi_lset(mbedtls_mpi *X, mbedtls_mpi_sint z);
Paul Bakker5121ce52009-01-03 21:22:43 +0000338
Paul Bakker9a736322012-11-14 12:39:52 +0000339/**
Hanno Beckerc23483e2018-12-10 17:21:19 +0000340 * \brief Get a specific bit from an MPI.
Paul Bakker2f5947e2011-05-18 15:47:11 +0000341 *
Hanno Becker8282c2f2018-12-12 13:36:46 +0000342 * \param X The MPI to query. This must be initialized.
Hanno Beckerc23483e2018-12-10 17:21:19 +0000343 * \param pos Zero-based index of the bit to query.
Paul Bakker2f5947e2011-05-18 15:47:11 +0000344 *
Hanno Beckerc23483e2018-12-10 17:21:19 +0000345 * \return \c 0 or \c 1 on success, depending on whether bit \c pos
Hanno Becker8282c2f2018-12-12 13:36:46 +0000346 * of \c X is unset or set.
Hanno Beckerc23483e2018-12-10 17:21:19 +0000347 * \return A negative error code on failure.
Paul Bakker2f5947e2011-05-18 15:47:11 +0000348 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200349int mbedtls_mpi_get_bit(const mbedtls_mpi *X, size_t pos);
Paul Bakker2f5947e2011-05-18 15:47:11 +0000350
Paul Bakker9a736322012-11-14 12:39:52 +0000351/**
Hanno Beckerc23483e2018-12-10 17:21:19 +0000352 * \brief Modify a specific bit in an MPI.
Paul Bakker2f5947e2011-05-18 15:47:11 +0000353 *
Hanno Beckerc23483e2018-12-10 17:21:19 +0000354 * \note This function will grow the target MPI if necessary to set a
355 * bit to \c 1 in a not yet existing limb. It will not grow if
356 * the bit should be set to \c 0.
Paul Bakker2f5947e2011-05-18 15:47:11 +0000357 *
Hanno Becker8282c2f2018-12-12 13:36:46 +0000358 * \param X The MPI to modify. This must be initialized.
Hanno Beckerc23483e2018-12-10 17:21:19 +0000359 * \param pos Zero-based index of the bit to modify.
360 * \param val The desired value of bit \c pos: \c 0 or \c 1.
Paul Bakker2f5947e2011-05-18 15:47:11 +0000361 *
Hanno Beckerc23483e2018-12-10 17:21:19 +0000362 * \return \c 0 if successful.
363 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed.
364 * \return Another negative error code on other kinds of failure.
Paul Bakker2f5947e2011-05-18 15:47:11 +0000365 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200366int mbedtls_mpi_set_bit(mbedtls_mpi *X, size_t pos, unsigned char val);
Paul Bakker2f5947e2011-05-18 15:47:11 +0000367
Paul Bakker5121ce52009-01-03 21:22:43 +0000368/**
Hanno Beckere1185042018-12-13 14:31:46 +0000369 * \brief Return the number of bits of value \c 0 before the
370 * least significant bit of value \c 1.
Paul Bakker6b906e52012-05-08 12:01:43 +0000371 *
Hanno Beckerc23483e2018-12-10 17:21:19 +0000372 * \note This is the same as the zero-based index of
Hanno Beckere1185042018-12-13 14:31:46 +0000373 * the least significant bit of value \c 1.
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000374 *
Hanno Beckerc23483e2018-12-10 17:21:19 +0000375 * \param X The MPI to query.
376 *
Hanno Beckere1185042018-12-13 14:31:46 +0000377 * \return The number of bits of value \c 0 before the least significant
378 * bit of value \c 1 in \p X.
Paul Bakker5121ce52009-01-03 21:22:43 +0000379 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200380size_t mbedtls_mpi_lsb(const mbedtls_mpi *X);
Paul Bakker5121ce52009-01-03 21:22:43 +0000381
382/**
Paul Bakker6b906e52012-05-08 12:01:43 +0000383 * \brief Return the number of bits up to and including the most
Hanno Beckere1185042018-12-13 14:31:46 +0000384 * significant bit of value \c 1.
Paul Bakker6b906e52012-05-08 12:01:43 +0000385 *
Hanno Beckerc23483e2018-12-10 17:21:19 +0000386 * * \note This is same as the one-based index of the most
Hanno Beckere1185042018-12-13 14:31:46 +0000387 * significant bit of value \c 1.
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000388 *
Hanno Becker8282c2f2018-12-12 13:36:46 +0000389 * \param X The MPI to query. This must point to an initialized MPI.
Hanno Beckerc23483e2018-12-10 17:21:19 +0000390 *
391 * \return The number of bits up to and including the most
Hanno Beckere1185042018-12-13 14:31:46 +0000392 * significant bit of value \c 1.
Paul Bakker5121ce52009-01-03 21:22:43 +0000393 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200394size_t mbedtls_mpi_bitlen(const mbedtls_mpi *X);
Paul Bakker5121ce52009-01-03 21:22:43 +0000395
396/**
Hanno Beckere1185042018-12-13 14:31:46 +0000397 * \brief Return the total size of an MPI value in bytes.
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000398 *
Hanno Becker8282c2f2018-12-12 13:36:46 +0000399 * \param X The MPI to use. This must point to an initialized MPI.
Hanno Beckerc23483e2018-12-10 17:21:19 +0000400 *
401 * \note The value returned by this function may be less than
402 * the number of bytes used to store \p X internally.
Hanno Beckere1185042018-12-13 14:31:46 +0000403 * This happens if and only if there are trailing bytes
404 * of value zero.
Hanno Beckerc23483e2018-12-10 17:21:19 +0000405 *
406 * \return The least number of bytes capable of storing
407 * the absolute value of \p X.
Paul Bakker5121ce52009-01-03 21:22:43 +0000408 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200409size_t mbedtls_mpi_size(const mbedtls_mpi *X);
Paul Bakker5121ce52009-01-03 21:22:43 +0000410
411/**
Hanno Beckerc23483e2018-12-10 17:21:19 +0000412 * \brief Import an MPI from an ASCII string.
Paul Bakker5121ce52009-01-03 21:22:43 +0000413 *
Hanno Becker8282c2f2018-12-12 13:36:46 +0000414 * \param X The destination MPI. This must point to an initialized MPI.
Hanno Beckerc23483e2018-12-10 17:21:19 +0000415 * \param radix The numeric base of the input string.
416 * \param s Null-terminated string buffer.
Paul Bakker5121ce52009-01-03 21:22:43 +0000417 *
Hanno Beckerc23483e2018-12-10 17:21:19 +0000418 * \return \c 0 if successful.
419 * \return A negative error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000420 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200421int mbedtls_mpi_read_string(mbedtls_mpi *X, int radix, const char *s);
Paul Bakker5121ce52009-01-03 21:22:43 +0000422
423/**
Hanno Beckere1185042018-12-13 14:31:46 +0000424 * \brief Export an MPI to an ASCII string.
Paul Bakker5121ce52009-01-03 21:22:43 +0000425 *
Hanno Becker8282c2f2018-12-12 13:36:46 +0000426 * \param X The source MPI. This must point to an initialized MPI.
Hanno Beckerc23483e2018-12-10 17:21:19 +0000427 * \param radix The numeric base of the output string.
Hanno Beckere1185042018-12-13 14:31:46 +0000428 * \param buf The buffer to write the string to. This must be writable
Hanno Beckerd7310122018-12-18 18:11:42 +0000429 * buffer of length \p buflen Bytes.
Hanno Beckerc23483e2018-12-10 17:21:19 +0000430 * \param buflen The available size in Bytes of \p buf.
Hanno Beckere1185042018-12-13 14:31:46 +0000431 * \param olen The address at which to store the length of the string
432 * written, including the final \c NULL byte. This must
433 * not be \c NULL.
Paul Bakker5121ce52009-01-03 21:22:43 +0000434 *
Hanno Beckere1185042018-12-13 14:31:46 +0000435 * \note You can call this function with `buflen == 0` to obtain the
Hanno Beckerc23483e2018-12-10 17:21:19 +0000436 * minimum required buffer size in `*olen`.
Paul Bakker5121ce52009-01-03 21:22:43 +0000437 *
Hanno Beckerc23483e2018-12-10 17:21:19 +0000438 * \return \c 0 if successful.
439 * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if the target buffer \p buf
440 * is too small to hold the value of \p X in the desired base.
441 * In this case, `*olen` is nonetheless updated to contain the
442 * size of \p buf required for a successful call.
443 * \return Another negative error code on different kinds of failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000444 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200445int mbedtls_mpi_write_string(const mbedtls_mpi *X,
446 int radix,
447 char *buf,
448 size_t buflen,
449 size_t *olen);
Paul Bakker5121ce52009-01-03 21:22:43 +0000450
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200451#if defined(MBEDTLS_FS_IO)
Paul Bakker5121ce52009-01-03 21:22:43 +0000452/**
Hanno Beckerc23483e2018-12-10 17:21:19 +0000453 * \brief Read an MPI from a line in an opened file.
Paul Bakker5121ce52009-01-03 21:22:43 +0000454 *
Hanno Becker8282c2f2018-12-12 13:36:46 +0000455 * \param X The destination MPI. This must point to an initialized MPI.
Hanno Beckerc23483e2018-12-10 17:21:19 +0000456 * \param radix The numeric base of the string representation used
457 * in the source line.
Hanno Becker8282c2f2018-12-12 13:36:46 +0000458 * \param fin The input file handle to use. This must not be \c NULL.
Hanno Beckerb2034b72017-04-26 11:46:46 +0100459 *
460 * \note On success, this function advances the file stream
461 * to the end of the current line or to EOF.
462 *
Hanno Beckerc23483e2018-12-10 17:21:19 +0000463 * The function returns \c 0 on an empty line.
Hanno Beckerb2034b72017-04-26 11:46:46 +0100464 *
465 * Leading whitespaces are ignored, as is a
Hanno Beckerc23483e2018-12-10 17:21:19 +0000466 * '0x' prefix for radix \c 16.
Hanno Beckerb2034b72017-04-26 11:46:46 +0100467 *
Hanno Beckerc23483e2018-12-10 17:21:19 +0000468 * \return \c 0 if successful.
469 * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if the file read buffer
470 * is too small.
471 * \return Another negative error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000472 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200473int mbedtls_mpi_read_file(mbedtls_mpi *X, int radix, FILE *fin);
Paul Bakker5121ce52009-01-03 21:22:43 +0000474
475/**
Hanno Beckerc23483e2018-12-10 17:21:19 +0000476 * \brief Export an MPI into an opened file.
Paul Bakker5121ce52009-01-03 21:22:43 +0000477 *
Hanno Beckerc23483e2018-12-10 17:21:19 +0000478 * \param p A string prefix to emit prior to the MPI data.
479 * For example, this might be a label, or "0x" when
Hanno Beckere1185042018-12-13 14:31:46 +0000480 * printing in base \c 16. This may be \c NULL if no prefix
Hanno Beckerc23483e2018-12-10 17:21:19 +0000481 * is needed.
Hanno Becker8282c2f2018-12-12 13:36:46 +0000482 * \param X The source MPI. This must point to an initialized MPI.
Hanno Beckerc23483e2018-12-10 17:21:19 +0000483 * \param radix The numeric base to be used in the emitted string.
Hanno Beckere1185042018-12-13 14:31:46 +0000484 * \param fout The output file handle. This may be \c NULL, in which case
485 * the output is written to \c stdout.
Paul Bakker5121ce52009-01-03 21:22:43 +0000486 *
Hanno Beckerc23483e2018-12-10 17:21:19 +0000487 * \return \c 0 if successful.
488 * \return A negative error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000489 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200490int mbedtls_mpi_write_file(const char *p,
491 const mbedtls_mpi *X,
492 int radix,
493 FILE *fout);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200494#endif /* MBEDTLS_FS_IO */
Paul Bakker5121ce52009-01-03 21:22:43 +0000495
496/**
Hanno Beckerc23483e2018-12-10 17:21:19 +0000497 * \brief Import an MPI from unsigned big endian binary data.
Paul Bakker5121ce52009-01-03 21:22:43 +0000498 *
Hanno Becker8282c2f2018-12-12 13:36:46 +0000499 * \param X The destination MPI. This must point to an initialized MPI.
Hanno Beckere1185042018-12-13 14:31:46 +0000500 * \param buf The input buffer. This must be a readable buffer of length
Hanno Beckerc23483e2018-12-10 17:21:19 +0000501 * \p buflen Bytes.
502 * \param buflen The length of the input buffer \p p in Bytes.
Paul Bakker5121ce52009-01-03 21:22:43 +0000503 *
Hanno Beckerc23483e2018-12-10 17:21:19 +0000504 * \return \c 0 if successful.
505 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed.
506 * \return Another negative error code on different kinds of failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000507 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200508int mbedtls_mpi_read_binary(mbedtls_mpi *X,
509 const unsigned char *buf,
510 size_t buflen);
Paul Bakker5121ce52009-01-03 21:22:43 +0000511
512/**
Janos Follatha778a942019-02-13 10:28:28 +0000513 * \brief Import X from unsigned binary data, little endian
514 *
515 * \param X The destination MPI. This must point to an initialized MPI.
516 * \param buf The input buffer. This must be a readable buffer of length
517 * \p buflen Bytes.
518 * \param buflen The length of the input buffer \p p in Bytes.
519 *
520 * \return \c 0 if successful.
521 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed.
522 * \return Another negative error code on different kinds of failure.
523 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200524int mbedtls_mpi_read_binary_le(mbedtls_mpi *X,
525 const unsigned char *buf,
526 size_t buflen);
Janos Follatha778a942019-02-13 10:28:28 +0000527
528/**
529 * \brief Export X into unsigned binary data, big endian.
530 * Always fills the whole buffer, which will start with zeros
531 * if the number is smaller.
Paul Bakker5121ce52009-01-03 21:22:43 +0000532 *
Hanno Becker8282c2f2018-12-12 13:36:46 +0000533 * \param X The source MPI. This must point to an initialized MPI.
Hanno Beckere1185042018-12-13 14:31:46 +0000534 * \param buf The output buffer. This must be a writable buffer of length
Hanno Beckerc23483e2018-12-10 17:21:19 +0000535 * \p buflen Bytes.
536 * \param buflen The size of the output buffer \p buf in Bytes.
Paul Bakker5121ce52009-01-03 21:22:43 +0000537 *
Hanno Beckerc23483e2018-12-10 17:21:19 +0000538 * \return \c 0 if successful.
539 * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p buf isn't
Hanno Beckere1185042018-12-13 14:31:46 +0000540 * large enough to hold the value of \p X.
Hanno Beckerc23483e2018-12-10 17:21:19 +0000541 * \return Another negative error code on different kinds of failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000542 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200543int mbedtls_mpi_write_binary(const mbedtls_mpi *X,
544 unsigned char *buf,
545 size_t buflen);
Paul Bakker5121ce52009-01-03 21:22:43 +0000546
547/**
Janos Follathe344d0f2019-02-19 16:17:40 +0000548 * \brief Export X into unsigned binary data, little endian.
549 * Always fills the whole buffer, which will end with zeros
550 * if the number is smaller.
551 *
552 * \param X The source MPI. This must point to an initialized MPI.
553 * \param buf The output buffer. This must be a writable buffer of length
554 * \p buflen Bytes.
555 * \param buflen The size of the output buffer \p buf in Bytes.
556 *
557 * \return \c 0 if successful.
558 * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p buf isn't
559 * large enough to hold the value of \p X.
560 * \return Another negative error code on different kinds of failure.
561 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200562int mbedtls_mpi_write_binary_le(const mbedtls_mpi *X,
563 unsigned char *buf,
564 size_t buflen);
Janos Follathe344d0f2019-02-19 16:17:40 +0000565
566/**
Hanno Beckerc23483e2018-12-10 17:21:19 +0000567 * \brief Perform a left-shift on an MPI: X <<= count
Paul Bakker5121ce52009-01-03 21:22:43 +0000568 *
Hanno Becker8282c2f2018-12-12 13:36:46 +0000569 * \param X The MPI to shift. This must point to an initialized MPI.
Hanno Beckere1185042018-12-13 14:31:46 +0000570 * \param count The number of bits to shift by.
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000571 *
Hanno Beckerc23483e2018-12-10 17:21:19 +0000572 * \return \c 0 if successful.
573 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
574 * \return Another negative error code on different kinds of failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000575 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200576int mbedtls_mpi_shift_l(mbedtls_mpi *X, size_t count);
Paul Bakker5121ce52009-01-03 21:22:43 +0000577
578/**
Hanno Beckerc23483e2018-12-10 17:21:19 +0000579 * \brief Perform a right-shift on an MPI: X >>= count
Paul Bakker5121ce52009-01-03 21:22:43 +0000580 *
Hanno Becker8282c2f2018-12-12 13:36:46 +0000581 * \param X The MPI to shift. This must point to an initialized MPI.
Hanno Beckere1185042018-12-13 14:31:46 +0000582 * \param count The number of bits to shift by.
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000583 *
Hanno Beckerc23483e2018-12-10 17:21:19 +0000584 * \return \c 0 if successful.
585 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
586 * \return Another negative error code on different kinds of failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000587 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200588int mbedtls_mpi_shift_r(mbedtls_mpi *X, size_t count);
Paul Bakker5121ce52009-01-03 21:22:43 +0000589
590/**
Hanno Beckerc23483e2018-12-10 17:21:19 +0000591 * \brief Compare the absolute values of two MPIs.
Paul Bakker5121ce52009-01-03 21:22:43 +0000592 *
Hanno Becker8282c2f2018-12-12 13:36:46 +0000593 * \param X The left-hand MPI. This must point to an initialized MPI.
594 * \param Y The right-hand MPI. This must point to an initialized MPI.
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000595 *
Hanno Beckerc23483e2018-12-10 17:21:19 +0000596 * \return \c 1 if `|X|` is greater than `|Y|`.
597 * \return \c -1 if `|X|` is lesser than `|Y|`.
Hanno Beckere1185042018-12-13 14:31:46 +0000598 * \return \c 0 if `|X|` is equal to `|Y|`.
Paul Bakker5121ce52009-01-03 21:22:43 +0000599 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200600int mbedtls_mpi_cmp_abs(const mbedtls_mpi *X, const mbedtls_mpi *Y);
Paul Bakker5121ce52009-01-03 21:22:43 +0000601
602/**
Hanno Beckerc23483e2018-12-10 17:21:19 +0000603 * \brief Compare two MPIs.
Paul Bakker5121ce52009-01-03 21:22:43 +0000604 *
Hanno Becker8282c2f2018-12-12 13:36:46 +0000605 * \param X The left-hand MPI. This must point to an initialized MPI.
606 * \param Y The right-hand MPI. This must point to an initialized MPI.
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000607 *
Hanno Beckere1185042018-12-13 14:31:46 +0000608 * \return \c 1 if \p X is greater than \p Y.
609 * \return \c -1 if \p X is lesser than \p Y.
610 * \return \c 0 if \p X is equal to \p Y.
Paul Bakker5121ce52009-01-03 21:22:43 +0000611 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200612int mbedtls_mpi_cmp_mpi(const mbedtls_mpi *X, const mbedtls_mpi *Y);
Paul Bakker5121ce52009-01-03 21:22:43 +0000613
614/**
Janos Follath0e5532d2019-10-11 14:21:53 +0100615 * \brief Check if an MPI is less than the other in constant time.
Janos Follathee6abce2019-09-05 14:47:19 +0100616 *
617 * \param X The left-hand MPI. This must point to an initialized MPI
618 * with the same allocated length as Y.
619 * \param Y The right-hand MPI. This must point to an initialized MPI
620 * with the same allocated length as X.
621 * \param ret The result of the comparison:
Janos Follath0e5532d2019-10-11 14:21:53 +0100622 * \c 1 if \p X is less than \p Y.
623 * \c 0 if \p X is greater than or equal to \p Y.
Janos Follathee6abce2019-09-05 14:47:19 +0100624 *
625 * \return 0 on success.
626 * \return MBEDTLS_ERR_MPI_BAD_INPUT_DATA if the allocated length of
627 * the two input MPIs is not the same.
628 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200629int mbedtls_mpi_lt_mpi_ct(const mbedtls_mpi *X,
630 const mbedtls_mpi *Y,
631 unsigned *ret);
Janos Follathee6abce2019-09-05 14:47:19 +0100632
633/**
Hanno Beckerc23483e2018-12-10 17:21:19 +0000634 * \brief Compare an MPI with an integer.
Paul Bakker5121ce52009-01-03 21:22:43 +0000635 *
Hanno Becker8282c2f2018-12-12 13:36:46 +0000636 * \param X The left-hand MPI. This must point to an initialized MPI.
Hanno Beckerc23483e2018-12-10 17:21:19 +0000637 * \param z The integer value to compare \p X to.
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000638 *
Hanno Beckere1185042018-12-13 14:31:46 +0000639 * \return \c 1 if \p X is greater than \p z.
640 * \return \c -1 if \p X is lesser than \p z.
641 * \return \c 0 if \p X is equal to \p z.
Paul Bakker5121ce52009-01-03 21:22:43 +0000642 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200643int mbedtls_mpi_cmp_int(const mbedtls_mpi *X, mbedtls_mpi_sint z);
Paul Bakker5121ce52009-01-03 21:22:43 +0000644
645/**
Hanno Beckerc23483e2018-12-10 17:21:19 +0000646 * \brief Perform an unsigned addition of MPIs: X = |A| + |B|
Paul Bakker5121ce52009-01-03 21:22:43 +0000647 *
Hanno Becker8282c2f2018-12-12 13:36:46 +0000648 * \param X The destination MPI. This must point to an initialized MPI.
649 * \param A The first summand. This must point to an initialized MPI.
650 * \param B The second summand. This must point to an initialized MPI.
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000651 *
Hanno Beckerc23483e2018-12-10 17:21:19 +0000652 * \return \c 0 if successful.
653 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
654 * \return Another negative error code on different kinds of failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000655 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200656int mbedtls_mpi_add_abs(mbedtls_mpi *X,
657 const mbedtls_mpi *A,
658 const mbedtls_mpi *B);
Paul Bakker5121ce52009-01-03 21:22:43 +0000659
660/**
Hanno Beckerc23483e2018-12-10 17:21:19 +0000661 * \brief Perform an unsigned subtraction of MPIs: X = |A| - |B|
Paul Bakker5121ce52009-01-03 21:22:43 +0000662 *
Hanno Becker8282c2f2018-12-12 13:36:46 +0000663 * \param X The destination MPI. This must point to an initialized MPI.
664 * \param A The minuend. This must point to an initialized MPI.
665 * \param B The subtrahend. This must point to an initialized MPI.
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000666 *
Hanno Beckerc23483e2018-12-10 17:21:19 +0000667 * \return \c 0 if successful.
668 * \return #MBEDTLS_ERR_MPI_NEGATIVE_VALUE if \p B is greater than \p A.
669 * \return Another negative error code on different kinds of failure.
670 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000671 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200672int mbedtls_mpi_sub_abs(mbedtls_mpi *X,
673 const mbedtls_mpi *A,
674 const mbedtls_mpi *B);
Paul Bakker5121ce52009-01-03 21:22:43 +0000675
676/**
Hanno Beckerc23483e2018-12-10 17:21:19 +0000677 * \brief Perform a signed addition of MPIs: X = A + B
Paul Bakker5121ce52009-01-03 21:22:43 +0000678 *
Hanno Becker8282c2f2018-12-12 13:36:46 +0000679 * \param X The destination MPI. This must point to an initialized MPI.
680 * \param A The first summand. This must point to an initialized MPI.
681 * \param B The second summand. This must point to an initialized MPI.
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000682 *
Hanno Beckerc23483e2018-12-10 17:21:19 +0000683 * \return \c 0 if successful.
684 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
685 * \return Another negative error code on different kinds of failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000686 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200687int mbedtls_mpi_add_mpi(mbedtls_mpi *X,
688 const mbedtls_mpi *A,
689 const mbedtls_mpi *B);
Paul Bakker5121ce52009-01-03 21:22:43 +0000690
691/**
Hanno Beckerc23483e2018-12-10 17:21:19 +0000692 * \brief Perform a signed subtraction of MPIs: X = A - B
Paul Bakker5121ce52009-01-03 21:22:43 +0000693 *
Hanno Becker8282c2f2018-12-12 13:36:46 +0000694 * \param X The destination MPI. This must point to an initialized MPI.
695 * \param A The minuend. This must point to an initialized MPI.
696 * \param B The subtrahend. This must point to an initialized MPI.
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000697 *
Hanno Beckerc23483e2018-12-10 17:21:19 +0000698 * \return \c 0 if successful.
699 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
700 * \return Another negative error code on different kinds of failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000701 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200702int mbedtls_mpi_sub_mpi(mbedtls_mpi *X,
703 const mbedtls_mpi *A,
704 const mbedtls_mpi *B);
Paul Bakker5121ce52009-01-03 21:22:43 +0000705
706/**
Hanno Beckerc23483e2018-12-10 17:21:19 +0000707 * \brief Perform a signed addition of an MPI and an integer: X = A + b
Paul Bakker5121ce52009-01-03 21:22:43 +0000708 *
Hanno Becker8282c2f2018-12-12 13:36:46 +0000709 * \param X The destination MPI. This must point to an initialized MPI.
710 * \param A The first summand. This must point to an initialized MPI.
Hanno Beckerc23483e2018-12-10 17:21:19 +0000711 * \param b The second summand.
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000712 *
Hanno Beckerc23483e2018-12-10 17:21:19 +0000713 * \return \c 0 if successful.
714 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
715 * \return Another negative error code on different kinds of failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000716 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200717int mbedtls_mpi_add_int(mbedtls_mpi *X,
718 const mbedtls_mpi *A,
719 mbedtls_mpi_sint b);
Paul Bakker5121ce52009-01-03 21:22:43 +0000720
721/**
Hanno Beckerc23483e2018-12-10 17:21:19 +0000722 * \brief Perform a signed subtraction of an MPI and an integer:
723 * X = A - b
Paul Bakker5121ce52009-01-03 21:22:43 +0000724 *
Hanno Becker8282c2f2018-12-12 13:36:46 +0000725 * \param X The destination MPI. This must point to an initialized MPI.
726 * \param A The minuend. This must point to an initialized MPI.
Hanno Becker01c3c102018-12-17 23:04:51 +0000727 * \param b The subtrahend.
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000728 *
Hanno Beckerc23483e2018-12-10 17:21:19 +0000729 * \return \c 0 if successful.
730 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
731 * \return Another negative error code on different kinds of failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000732 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200733int mbedtls_mpi_sub_int(mbedtls_mpi *X,
734 const mbedtls_mpi *A,
735 mbedtls_mpi_sint b);
Paul Bakker5121ce52009-01-03 21:22:43 +0000736
737/**
Hanno Beckerc23483e2018-12-10 17:21:19 +0000738 * \brief Perform a multiplication of two MPIs: X = A * B
Paul Bakker5121ce52009-01-03 21:22:43 +0000739 *
Hanno Becker8282c2f2018-12-12 13:36:46 +0000740 * \param X The destination MPI. This must point to an initialized MPI.
741 * \param A The first factor. This must point to an initialized MPI.
742 * \param B The second factor. This must point to an initialized MPI.
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000743 *
Hanno Beckerc23483e2018-12-10 17:21:19 +0000744 * \return \c 0 if successful.
745 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
746 * \return Another negative error code on different kinds of failure.
747 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000748 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200749int mbedtls_mpi_mul_mpi(mbedtls_mpi *X,
750 const mbedtls_mpi *A,
751 const mbedtls_mpi *B);
Paul Bakker5121ce52009-01-03 21:22:43 +0000752
753/**
Hanno Beckerc23483e2018-12-10 17:21:19 +0000754 * \brief Perform a multiplication of an MPI with an unsigned integer:
755 * X = A * b
Paul Bakker5121ce52009-01-03 21:22:43 +0000756 *
Hanno Becker8282c2f2018-12-12 13:36:46 +0000757 * \param X The destination MPI. This must point to an initialized MPI.
758 * \param A The first factor. This must point to an initialized MPI.
Hanno Beckerc23483e2018-12-10 17:21:19 +0000759 * \param b The second factor.
Manuel Pégourié-Gonnard35f1d7f2015-03-19 12:42:40 +0000760 *
Hanno Beckerc23483e2018-12-10 17:21:19 +0000761 * \return \c 0 if successful.
762 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
763 * \return Another negative error code on different kinds of failure.
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000764 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000765 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200766int mbedtls_mpi_mul_int(mbedtls_mpi *X,
767 const mbedtls_mpi *A,
768 mbedtls_mpi_uint b);
Paul Bakker5121ce52009-01-03 21:22:43 +0000769
770/**
Hanno Beckerc23483e2018-12-10 17:21:19 +0000771 * \brief Perform a division with remainder of two MPIs:
772 * A = Q * B + R
Paul Bakker5121ce52009-01-03 21:22:43 +0000773 *
Hanno Beckerc23483e2018-12-10 17:21:19 +0000774 * \param Q The destination MPI for the quotient.
Hanno Beckerd01ff492018-12-18 23:10:28 +0000775 * This may be \c NULL if the value of the
776 * quotient is not needed.
Hanno Beckerc23483e2018-12-10 17:21:19 +0000777 * \param R The destination MPI for the remainder value.
Hanno Beckerd01ff492018-12-18 23:10:28 +0000778 * This may be \c NULL if the value of the
779 * remainder is not needed.
Hanno Becker8ce11a32018-12-19 16:18:52 +0000780 * \param A The dividend. This must point to an initialized MPi.
Hanno Becker8282c2f2018-12-12 13:36:46 +0000781 * \param B The divisor. This must point to an initialized MPI.
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000782 *
Hanno Beckerc23483e2018-12-10 17:21:19 +0000783 * \return \c 0 if successful.
784 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed.
Hanno Beckere1185042018-12-13 14:31:46 +0000785 * \return #MBEDTLS_ERR_MPI_DIVISION_BY_ZERO if \p B equals zero.
Hanno Beckerc23483e2018-12-10 17:21:19 +0000786 * \return Another negative error code on different kinds of failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000787 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200788int mbedtls_mpi_div_mpi(mbedtls_mpi *Q,
789 mbedtls_mpi *R,
790 const mbedtls_mpi *A,
791 const mbedtls_mpi *B);
Paul Bakker5121ce52009-01-03 21:22:43 +0000792
793/**
Hanno Beckerc23483e2018-12-10 17:21:19 +0000794 * \brief Perform a division with remainder of an MPI by an integer:
795 * A = Q * b + R
Paul Bakker5121ce52009-01-03 21:22:43 +0000796 *
Hanno Beckerc23483e2018-12-10 17:21:19 +0000797 * \param Q The destination MPI for the quotient.
Hanno Beckerd01ff492018-12-18 23:10:28 +0000798 * This may be \c NULL if the value of the
799 * quotient is not needed.
Hanno Beckerc23483e2018-12-10 17:21:19 +0000800 * \param R The destination MPI for the remainder value.
Hanno Beckerd01ff492018-12-18 23:10:28 +0000801 * This may be \c NULL if the value of the
802 * remainder is not needed.
Hanno Becker8ce11a32018-12-19 16:18:52 +0000803 * \param A The dividend. This must point to an initialized MPi.
Hanno Beckerc23483e2018-12-10 17:21:19 +0000804 * \param b The divisor.
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000805 *
Hanno Beckerc23483e2018-12-10 17:21:19 +0000806 * \return \c 0 if successful.
807 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed.
Hanno Beckere1185042018-12-13 14:31:46 +0000808 * \return #MBEDTLS_ERR_MPI_DIVISION_BY_ZERO if \p b equals zero.
Hanno Beckerc23483e2018-12-10 17:21:19 +0000809 * \return Another negative error code on different kinds of failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000810 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200811int mbedtls_mpi_div_int(mbedtls_mpi *Q,
812 mbedtls_mpi *R,
813 const mbedtls_mpi *A,
814 mbedtls_mpi_sint b);
Paul Bakker5121ce52009-01-03 21:22:43 +0000815
816/**
Hanno Beckerc23483e2018-12-10 17:21:19 +0000817 * \brief Perform a modular reduction. R = A mod B
Paul Bakker5121ce52009-01-03 21:22:43 +0000818 *
Hanno Beckerc23483e2018-12-10 17:21:19 +0000819 * \param R The destination MPI for the residue value.
Hanno Becker8282c2f2018-12-12 13:36:46 +0000820 * This must point to an initialized MPI.
Hanno Beckerc23483e2018-12-10 17:21:19 +0000821 * \param A The MPI to compute the residue of.
Hanno Becker8282c2f2018-12-12 13:36:46 +0000822 * This must point to an initialized MPI.
Hanno Beckerc23483e2018-12-10 17:21:19 +0000823 * \param B The base of the modular reduction.
Hanno Becker8282c2f2018-12-12 13:36:46 +0000824 * This must point to an initialized MPI.
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000825 *
Hanno Beckerc23483e2018-12-10 17:21:19 +0000826 * \return \c 0 if successful.
827 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
Hanno Beckere1185042018-12-13 14:31:46 +0000828 * \return #MBEDTLS_ERR_MPI_DIVISION_BY_ZERO if \p B equals zero.
829 * \return #MBEDTLS_ERR_MPI_NEGATIVE_VALUE if \p B is negative.
Hanno Beckerc23483e2018-12-10 17:21:19 +0000830 * \return Another negative error code on different kinds of failure.
831 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000832 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200833int mbedtls_mpi_mod_mpi(mbedtls_mpi *R,
834 const mbedtls_mpi *A,
835 const mbedtls_mpi *B);
Paul Bakker5121ce52009-01-03 21:22:43 +0000836
837/**
Hanno Beckerc23483e2018-12-10 17:21:19 +0000838 * \brief Perform a modular reduction with respect to an integer.
839 * r = A mod b
Paul Bakker5121ce52009-01-03 21:22:43 +0000840 *
Hanno Beckerc23483e2018-12-10 17:21:19 +0000841 * \param r The address at which to store the residue.
Hanno Becker8282c2f2018-12-12 13:36:46 +0000842 * This must not be \c NULL.
Hanno Beckerc23483e2018-12-10 17:21:19 +0000843 * \param A The MPI to compute the residue of.
Hanno Becker8282c2f2018-12-12 13:36:46 +0000844 * This must point to an initialized MPi.
Hanno Beckerc23483e2018-12-10 17:21:19 +0000845 * \param b The integer base of the modular reduction.
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000846 *
Hanno Beckerc23483e2018-12-10 17:21:19 +0000847 * \return \c 0 if successful.
848 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
Hanno Beckere1185042018-12-13 14:31:46 +0000849 * \return #MBEDTLS_ERR_MPI_DIVISION_BY_ZERO if \p b equals zero.
850 * \return #MBEDTLS_ERR_MPI_NEGATIVE_VALUE if \p b is negative.
Hanno Beckerc23483e2018-12-10 17:21:19 +0000851 * \return Another negative error code on different kinds of failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000852 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200853int mbedtls_mpi_mod_int(mbedtls_mpi_uint *r,
854 const mbedtls_mpi *A,
855 mbedtls_mpi_sint b);
Paul Bakker5121ce52009-01-03 21:22:43 +0000856
857/**
Hanno Beckerc23483e2018-12-10 17:21:19 +0000858 * \brief Perform a sliding-window exponentiation: X = A^E mod N
Paul Bakker5121ce52009-01-03 21:22:43 +0000859 *
Hanno Becker8282c2f2018-12-12 13:36:46 +0000860 * \param X The destination MPI. This must point to an initialized MPI.
Hanno Beckerc23483e2018-12-10 17:21:19 +0000861 * \param A The base of the exponentiation.
Hanno Becker8282c2f2018-12-12 13:36:46 +0000862 * This must point to an initialized MPI.
863 * \param E The exponent MPI. This must point to an initialized MPI.
864 * \param N The base for the modular reduction. This must point to an
Hanno Beckerc23483e2018-12-10 17:21:19 +0000865 * initialized MPI.
Yuto Takano538a0cb2021-07-14 10:20:09 +0100866 * \param prec_RR A helper MPI depending solely on \p N which can be used to
Hanno Beckerc23483e2018-12-10 17:21:19 +0000867 * speed-up multiple modular exponentiations for the same value
Hanno Beckere1185042018-12-13 14:31:46 +0000868 * of \p N. This may be \c NULL. If it is not \c NULL, it must
Hanno Becker8ce11a32018-12-19 16:18:52 +0000869 * point to an initialized MPI. If it hasn't been used after
Hanno Beckere1185042018-12-13 14:31:46 +0000870 * the call to mbedtls_mpi_init(), this function will compute
Yuto Takano538a0cb2021-07-14 10:20:09 +0100871 * the helper value and store it in \p prec_RR for reuse on
Hanno Beckere1185042018-12-13 14:31:46 +0000872 * subsequent calls to this function. Otherwise, the function
Yuto Takano538a0cb2021-07-14 10:20:09 +0100873 * will assume that \p prec_RR holds the helper value set by a
Hanno Beckere1185042018-12-13 14:31:46 +0000874 * previous call to mbedtls_mpi_exp_mod(), and reuse it.
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000875 *
Hanno Beckerc23483e2018-12-10 17:21:19 +0000876 * \return \c 0 if successful.
877 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
878 * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if \c N is negative or
879 * even, or if \c E is negative.
880 * \return Another negative error code on different kinds of failures.
Paul Bakker5121ce52009-01-03 21:22:43 +0000881 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000882 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200883int mbedtls_mpi_exp_mod(mbedtls_mpi *X,
884 const mbedtls_mpi *A,
885 const mbedtls_mpi *E,
886 const mbedtls_mpi *N,
887 mbedtls_mpi *prec_RR);
Paul Bakker5121ce52009-01-03 21:22:43 +0000888
889/**
Hanno Beckerc23483e2018-12-10 17:21:19 +0000890 * \brief Fill an MPI with a number of random bytes.
Paul Bakker287781a2011-03-26 13:18:49 +0000891 *
Hanno Becker8282c2f2018-12-12 13:36:46 +0000892 * \param X The destination MPI. This must point to an initialized MPI.
Hanno Beckerc23483e2018-12-10 17:21:19 +0000893 * \param size The number of random bytes to generate.
Hanno Becker8282c2f2018-12-12 13:36:46 +0000894 * \param f_rng The RNG function to use. This must not be \c NULL.
Hanno Beckere1185042018-12-13 14:31:46 +0000895 * \param p_rng The RNG parameter to be passed to \p f_rng. This may be
896 * \c NULL if \p f_rng doesn't need a context argument.
Paul Bakker287781a2011-03-26 13:18:49 +0000897 *
Hanno Beckerc23483e2018-12-10 17:21:19 +0000898 * \return \c 0 if successful.
899 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
900 * \return Another negative error code on failure.
Hanno Becker15f2b3e2017-10-17 15:17:05 +0100901 *
Hanno Beckerc23483e2018-12-10 17:21:19 +0000902 * \note The bytes obtained from the RNG are interpreted
Hanno Becker15f2b3e2017-10-17 15:17:05 +0100903 * as a big-endian representation of an MPI; this can
904 * be relevant in applications like deterministic ECDSA.
Paul Bakker287781a2011-03-26 13:18:49 +0000905 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200906int mbedtls_mpi_fill_random(mbedtls_mpi *X,
907 size_t size,
908 int (*f_rng)(void *, unsigned char *, size_t),
909 void *p_rng);
Paul Bakker287781a2011-03-26 13:18:49 +0000910
Gilles Peskine02ac93a2021-03-29 22:02:55 +0200911/** Generate a random number uniformly in a range.
912 *
913 * This function generates a random number between \p min inclusive and
914 * \p N exclusive.
915 *
916 * The procedure complies with RFC 6979 §3.3 (deterministic ECDSA)
917 * when the RNG is a suitably parametrized instance of HMAC_DRBG
918 * and \p min is \c 1.
919 *
920 * \note There are `N - min` possible outputs. The lower bound
921 * \p min can be reached, but the upper bound \p N cannot.
922 *
923 * \param X The destination MPI. This must point to an initialized MPI.
924 * \param min The minimum value to return.
925 * It must be nonnegative.
926 * \param N The upper bound of the range, exclusive.
927 * In other words, this is one plus the maximum value to return.
928 * \p N must be strictly larger than \p min.
929 * \param f_rng The RNG function to use. This must not be \c NULL.
930 * \param p_rng The RNG parameter to be passed to \p f_rng.
931 *
932 * \return \c 0 if successful.
933 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
Gilles Peskine1e918f42021-03-29 22:14:51 +0200934 * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if \p min or \p N is invalid
935 * or if they are incompatible.
Gilles Peskine7ed7c5a2021-04-13 21:28:38 +0200936 * \return #MBEDTLS_ERR_MPI_NOT_ACCEPTABLE if the implementation was
937 * unable to find a suitable value within a limited number
938 * of attempts. This has a negligible probability if \p N
939 * is significantly larger than \p min, which is the case
940 * for all usual cryptographic applications.
Gilles Peskine02ac93a2021-03-29 22:02:55 +0200941 * \return Another negative error code on failure.
942 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200943int mbedtls_mpi_random(mbedtls_mpi *X,
944 mbedtls_mpi_sint min,
945 const mbedtls_mpi *N,
946 int (*f_rng)(void *, unsigned char *, size_t),
947 void *p_rng);
Gilles Peskine02ac93a2021-03-29 22:02:55 +0200948
Paul Bakker287781a2011-03-26 13:18:49 +0000949/**
Hanno Beckerc23483e2018-12-10 17:21:19 +0000950 * \brief Compute the greatest common divisor: G = gcd(A, B)
Paul Bakker5121ce52009-01-03 21:22:43 +0000951 *
Hanno Becker8282c2f2018-12-12 13:36:46 +0000952 * \param G The destination MPI. This must point to an initialized MPI.
953 * \param A The first operand. This must point to an initialized MPI.
Hanno Becker01c3c102018-12-17 23:04:51 +0000954 * \param B The second operand. This must point to an initialized MPI.
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000955 *
Hanno Beckerc23483e2018-12-10 17:21:19 +0000956 * \return \c 0 if successful.
957 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
958 * \return Another negative error code on different kinds of failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000959 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200960int mbedtls_mpi_gcd(mbedtls_mpi *G, const mbedtls_mpi *A, const mbedtls_mpi *B);
Paul Bakker5121ce52009-01-03 21:22:43 +0000961
962/**
Hanno Beckerc23483e2018-12-10 17:21:19 +0000963 * \brief Compute the modular inverse: X = A^-1 mod N
Paul Bakker5121ce52009-01-03 21:22:43 +0000964 *
Hanno Becker8282c2f2018-12-12 13:36:46 +0000965 * \param X The destination MPI. This must point to an initialized MPI.
966 * \param A The MPI to calculate the modular inverse of. This must point
Hanno Beckerc23483e2018-12-10 17:21:19 +0000967 * to an initialized MPI.
Hanno Becker8282c2f2018-12-12 13:36:46 +0000968 * \param N The base of the modular inversion. This must point to an
Hanno Beckerc23483e2018-12-10 17:21:19 +0000969 * initialized MPI.
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000970 *
Hanno Beckerc23483e2018-12-10 17:21:19 +0000971 * \return \c 0 if successful.
972 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
Hanno Beckere1185042018-12-13 14:31:46 +0000973 * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if \p N is less than
974 * or equal to one.
Hanno Beckerc23483e2018-12-10 17:21:19 +0000975 * \return #MBEDTLS_ERR_MPI_NOT_ACCEPTABLE if \p has no modular inverse
976 * with respect to \p N.
Paul Bakker5121ce52009-01-03 21:22:43 +0000977 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200978int mbedtls_mpi_inv_mod(mbedtls_mpi *X,
979 const mbedtls_mpi *A,
980 const mbedtls_mpi *N);
Paul Bakker5121ce52009-01-03 21:22:43 +0000981
Paul Bakker5121ce52009-01-03 21:22:43 +0000982/**
Janos Follatha0b67c22018-09-18 14:48:23 +0100983 * \brief Miller-Rabin primality test.
984 *
985 * \warning If \p X is potentially generated by an adversary, for example
986 * when validating cryptographic parameters that you didn't
987 * generate yourself and that are supposed to be prime, then
988 * \p rounds should be at least the half of the security
989 * strength of the cryptographic algorithm. On the other hand,
990 * if \p X is chosen uniformly or non-adversially (as is the
991 * case when mbedtls_mpi_gen_prime calls this function), then
992 * \p rounds can be much lower.
993 *
Hanno Beckerc23483e2018-12-10 17:21:19 +0000994 * \param X The MPI to check for primality.
Hanno Becker8282c2f2018-12-12 13:36:46 +0000995 * This must point to an initialized MPI.
Hanno Beckerc23483e2018-12-10 17:21:19 +0000996 * \param rounds The number of bases to perform the Miller-Rabin primality
997 * test for. The probability of returning 0 on a composite is
998 * at most 2<sup>-2*\p rounds</sup>.
Hanno Becker8282c2f2018-12-12 13:36:46 +0000999 * \param f_rng The RNG function to use. This must not be \c NULL.
Hanno Beckerc23483e2018-12-10 17:21:19 +00001000 * \param p_rng The RNG parameter to be passed to \p f_rng.
Hanno Beckere1185042018-12-13 14:31:46 +00001001 * This may be \c NULL if \p f_rng doesn't use
1002 * a context parameter.
Janos Follatha0b67c22018-09-18 14:48:23 +01001003 *
Hanno Beckerc23483e2018-12-10 17:21:19 +00001004 * \return \c 0 if successful, i.e. \p X is probably prime.
1005 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
1006 * \return #MBEDTLS_ERR_MPI_NOT_ACCEPTABLE if \p X is not prime.
1007 * \return Another negative error code on other kinds of failure.
Janos Follatha0b67c22018-09-18 14:48:23 +01001008 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001009int mbedtls_mpi_is_prime_ext(const mbedtls_mpi *X,
1010 int rounds,
1011 int (*f_rng)(void *, unsigned char *, size_t),
1012 void *p_rng);
Janos Follatha0b67c22018-09-18 14:48:23 +01001013/**
Janos Follath7c025a92018-08-14 11:08:41 +01001014 * \brief Flags for mbedtls_mpi_gen_prime()
1015 *
1016 * Each of these flags is a constraint on the result X returned by
1017 * mbedtls_mpi_gen_prime().
1018 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001019typedef enum
1020{
1021 MBEDTLS_MPI_GEN_PRIME_FLAG_DH = 0x0001, /**< (X-1)/2 is prime too */
1022 MBEDTLS_MPI_GEN_PRIME_FLAG_LOW_ERR = 0x0002, /**< lower error rate from
1023 2<sup>-80</sup> to
1024 2<sup>-128</sup> */
Janos Follath7c025a92018-08-14 11:08:41 +01001025} mbedtls_mpi_gen_prime_flag_t;
1026
1027/**
Hanno Beckerc23483e2018-12-10 17:21:19 +00001028 * \brief Generate a prime number.
Paul Bakker5121ce52009-01-03 21:22:43 +00001029 *
Hanno Beckerc23483e2018-12-10 17:21:19 +00001030 * \param X The destination MPI to store the generated prime in.
Hanno Becker8282c2f2018-12-12 13:36:46 +00001031 * This must point to an initialized MPi.
Hanno Beckerc23483e2018-12-10 17:21:19 +00001032 * \param nbits The required size of the destination MPI in bits.
Hanno Beckerd01ff492018-12-18 23:10:28 +00001033 * This must be between \c 3 and #MBEDTLS_MPI_MAX_BITS.
Hanno Beckerc23483e2018-12-10 17:21:19 +00001034 * \param flags A mask of flags of type #mbedtls_mpi_gen_prime_flag_t.
Hanno Becker8282c2f2018-12-12 13:36:46 +00001035 * \param f_rng The RNG function to use. This must not be \c NULL.
Hanno Beckerc23483e2018-12-10 17:21:19 +00001036 * \param p_rng The RNG parameter to be passed to \p f_rng.
Hanno Beckere1185042018-12-13 14:31:46 +00001037 * This may be \c NULL if \p f_rng doesn't use
1038 * a context parameter.
Paul Bakker5121ce52009-01-03 21:22:43 +00001039 *
Hanno Beckere1185042018-12-13 14:31:46 +00001040 * \return \c 0 if successful, in which case \p X holds a
Hanno Beckerc23483e2018-12-10 17:21:19 +00001041 * probably prime number.
1042 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
1043 * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if `nbits` is not between
1044 * \c 3 and #MBEDTLS_MPI_MAX_BITS.
Paul Bakker5121ce52009-01-03 21:22:43 +00001045 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001046int mbedtls_mpi_gen_prime(mbedtls_mpi *X,
1047 size_t nbits,
1048 int flags,
1049 int (*f_rng)(void *, unsigned char *, size_t),
1050 void *p_rng);
Paul Bakker5121ce52009-01-03 21:22:43 +00001051
Ron Eldorfa8f6352017-06-20 15:48:46 +03001052#if defined(MBEDTLS_SELF_TEST)
1053
Paul Bakker5121ce52009-01-03 21:22:43 +00001054/**
1055 * \brief Checkup routine
1056 *
1057 * \return 0 if successful, or 1 if the test failed
1058 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +02001059int mbedtls_mpi_self_test(int verbose);
Paul Bakker5121ce52009-01-03 21:22:43 +00001060
Ron Eldorfa8f6352017-06-20 15:48:46 +03001061#endif /* MBEDTLS_SELF_TEST */
1062
Paul Bakker5121ce52009-01-03 21:22:43 +00001063#ifdef __cplusplus
1064}
1065#endif
1066
1067#endif /* bignum.h */