blob: 22d5d848401663b9763d496f28dbbd94bc43eb80 [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
Dave Rodgman16799db2023-11-02 19:47:20 +00008 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Paul Bakker5121ce52009-01-03 21:22:43 +00009 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010#ifndef MBEDTLS_BIGNUM_H
11#define MBEDTLS_BIGNUM_H
Mateusz Starzyk846f0212021-05-19 19:44:07 +020012#include "mbedtls/private_access.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000013
Bence Szépkútic662b362021-05-27 11:25:03 +020014#include "mbedtls/build_info.h"
Paul Bakkercf0360a2012-01-20 10:08:14 +000015
Rich Evans00ab4702015-02-06 13:43:58 +000016#include <stddef.h>
Manuel Pégourié-Gonnardab229102015-04-15 11:53:16 +020017#include <stdint.h>
Rich Evans00ab4702015-02-06 13:43:58 +000018
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020019#if defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnarde94e6e52015-01-19 15:01:53 +000020#include <stdio.h>
21#endif
22
Gilles Peskined2971572021-07-26 18:48:10 +020023/** An error occurred while reading from or writing to a file. */
24#define MBEDTLS_ERR_MPI_FILE_IO_ERROR -0x0002
25/** Bad input parameters to function. */
26#define MBEDTLS_ERR_MPI_BAD_INPUT_DATA -0x0004
27/** There is an invalid character in the digit string. */
28#define MBEDTLS_ERR_MPI_INVALID_CHARACTER -0x0006
29/** The buffer is too small to write to. */
30#define MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL -0x0008
31/** The input arguments are negative or result in illegal output. */
32#define MBEDTLS_ERR_MPI_NEGATIVE_VALUE -0x000A
33/** The input argument for division is zero, which is not allowed. */
34#define MBEDTLS_ERR_MPI_DIVISION_BY_ZERO -0x000C
35/** The input arguments are not acceptable. */
36#define MBEDTLS_ERR_MPI_NOT_ACCEPTABLE -0x000E
37/** Memory allocation failed. */
38#define MBEDTLS_ERR_MPI_ALLOC_FAILED -0x0010
Paul Bakker5121ce52009-01-03 21:22:43 +000039
Hanno Becker1eeca412018-10-15 12:01:35 +010040#define MBEDTLS_MPI_CHK(f) \
41 do \
42 { \
Gilles Peskine449bd832023-01-11 14:50:10 +010043 if ((ret = (f)) != 0) \
44 goto cleanup; \
45 } while (0)
Paul Bakker5121ce52009-01-03 21:22:43 +000046
47/*
Paul Bakkerf9688572011-05-05 10:00:45 +000048 * Maximum size MPIs are allowed to grow to in number of limbs.
49 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020050#define MBEDTLS_MPI_MAX_LIMBS 10000
Paul Bakkerf9688572011-05-05 10:00:45 +000051
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052#if !defined(MBEDTLS_MPI_WINDOW_SIZE)
Paul Bakkerf9688572011-05-05 10:00:45 +000053/*
Janos Follath6bd5cae2024-02-21 11:27:31 +000054 * Maximum window size used for modular exponentiation. Default: 3
Paul Bakkerb6d5f082011-11-25 11:52:11 +000055 * Minimum value: 1. Maximum value: 6.
56 *
Daniel Otte60861512020-09-07 13:07:14 +020057 * Result is an array of ( 2 ** MBEDTLS_MPI_WINDOW_SIZE ) MPIs used
Janos Follath6bd5cae2024-02-21 11:27:31 +000058 * for the sliding window calculation. (So 8 by default)
Paul Bakkerb6d5f082011-11-25 11:52:11 +000059 *
60 * Reduction in size, reduces speed.
61 */
Janos Follath1609d572024-02-14 14:58:39 +000062#define MBEDTLS_MPI_WINDOW_SIZE 3 /**< Maximum window size used. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020063#endif /* !MBEDTLS_MPI_WINDOW_SIZE */
Paul Bakkerb6d5f082011-11-25 11:52:11 +000064
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020065#if !defined(MBEDTLS_MPI_MAX_SIZE)
Paul Bakkerb6d5f082011-11-25 11:52:11 +000066/*
Paul Bakkerfe3256e2011-11-25 12:11:43 +000067 * Maximum size of MPIs allowed in bits and bytes for user-MPIs.
Paul Bakkerf626e1d2013-01-21 12:10:00 +010068 * ( Default: 512 bytes => 4096 bits, Maximum tested: 2048 bytes => 16384 bits )
Paul Bakkerfe3256e2011-11-25 12:11:43 +000069 *
Hanno Beckerefeef6c2018-01-05 08:07:47 +000070 * Note: Calculations can temporarily result in larger MPIs. So the number
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020071 * of limbs required (MBEDTLS_MPI_MAX_LIMBS) is higher.
Paul Bakkerfe3256e2011-11-25 12:11:43 +000072 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020073#define MBEDTLS_MPI_MAX_SIZE 1024 /**< Maximum number of bytes for usable MPIs. */
74#endif /* !MBEDTLS_MPI_MAX_SIZE */
Paul Bakker9bcf16c2013-06-24 19:31:17 +020075
Gilles Peskine449bd832023-01-11 14:50:10 +010076#define MBEDTLS_MPI_MAX_BITS (8 * MBEDTLS_MPI_MAX_SIZE) /**< Maximum number of bits for usable MPIs. */
Paul Bakkerfe3256e2011-11-25 12:11:43 +000077
78/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020079 * When reading from files with mbedtls_mpi_read_file() and writing to files with
80 * mbedtls_mpi_write_file() the buffer should have space
Paul Bakkercb37aa52011-11-30 16:00:20 +000081 * for a (short) label, the MPI (in the provided radix), the newline
82 * characters and the '\0'.
83 *
84 * By default we assume at least a 10 char label, a minimum radix of 10
85 * (decimal) and a maximum of 4096 bit numbers (1234 decimal chars).
Paul Bakkerf9183102012-09-27 20:42:35 +000086 * Autosized at compile time for at least a 10 char label, a minimum radix
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020087 * of 10 (decimal) for a number of MBEDTLS_MPI_MAX_BITS size.
Paul Bakkerf9183102012-09-27 20:42:35 +000088 *
89 * This used to be statically sized to 1250 for a maximum of 4096 bit
90 * numbers (1234 decimal chars).
91 *
92 * Calculate using the formula:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020093 * MBEDTLS_MPI_RW_BUFFER_SIZE = ceil(MBEDTLS_MPI_MAX_BITS / ln(10) * ln(2)) +
Paul Bakkerf9183102012-09-27 20:42:35 +000094 * LabelSize + 6
Paul Bakkercb37aa52011-11-30 16:00:20 +000095 */
Gilles Peskine449bd832023-01-11 14:50:10 +010096#define MBEDTLS_MPI_MAX_BITS_SCALE100 (100 * MBEDTLS_MPI_MAX_BITS)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020097#define MBEDTLS_LN_2_DIV_LN_10_SCALE100 332
Gilles Peskine449bd832023-01-11 14:50:10 +010098#define MBEDTLS_MPI_RW_BUFFER_SIZE (((MBEDTLS_MPI_MAX_BITS_SCALE100 + \
99 MBEDTLS_LN_2_DIV_LN_10_SCALE100 - 1) / \
100 MBEDTLS_LN_2_DIV_LN_10_SCALE100) + 10 + 6)
Paul Bakkercb37aa52011-11-30 16:00:20 +0000101
102/*
Manuel Pégourié-Gonnard7b538892015-04-09 17:00:17 +0200103 * Define the base integer type, architecture-wise.
104 *
Andres Amaya Garciad7fce002017-07-20 11:49:32 +0100105 * 32 or 64-bit integer types can be forced regardless of the underlying
106 * architecture by defining MBEDTLS_HAVE_INT32 or MBEDTLS_HAVE_INT64
107 * respectively and undefining MBEDTLS_HAVE_ASM.
108 *
Andres Amaya Garcia93db11a2017-07-20 12:11:19 +0100109 * Double-width integers (e.g. 128-bit in 64-bit architectures) can be
Andres Amaya Garciad7fce002017-07-20 11:49:32 +0100110 * disabled by defining MBEDTLS_NO_UDBL_DIVISION.
Paul Bakker5121ce52009-01-03 21:22:43 +0000111 */
Andres Amaya Garciaaa27dfe2017-05-04 11:05:55 +0100112#if !defined(MBEDTLS_HAVE_INT32)
113 #if defined(_MSC_VER) && defined(_M_AMD64)
Gilles Peskine449bd832023-01-11 14:50:10 +0100114/* Always choose 64-bit when using MSC */
Andres Amaya Garciad7fce002017-07-20 11:49:32 +0100115 #if !defined(MBEDTLS_HAVE_INT64)
116 #define MBEDTLS_HAVE_INT64
117 #endif /* !MBEDTLS_HAVE_INT64 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100118typedef int64_t mbedtls_mpi_sint;
119typedef uint64_t mbedtls_mpi_uint;
Agathiyan Bragadeesheed55c62023-07-19 11:08:02 +0100120#define MBEDTLS_MPI_UINT_MAX UINT64_MAX
Andres Amaya Garciaaa27dfe2017-05-04 11:05:55 +0100121 #elif defined(__GNUC__) && ( \
Gilles Peskine449bd832023-01-11 14:50:10 +0100122 defined(__amd64__) || defined(__x86_64__) || \
123 defined(__ppc64__) || defined(__powerpc64__) || \
124 defined(__ia64__) || defined(__alpha__) || \
125 (defined(__sparc__) && defined(__arch64__)) || \
126 defined(__s390x__) || defined(__mips64) || \
127 defined(__aarch64__))
Andres Amaya Garciad7fce002017-07-20 11:49:32 +0100128 #if !defined(MBEDTLS_HAVE_INT64)
129 #define MBEDTLS_HAVE_INT64
130 #endif /* MBEDTLS_HAVE_INT64 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100131typedef int64_t mbedtls_mpi_sint;
132typedef uint64_t mbedtls_mpi_uint;
Agathiyan Bragadeesheed55c62023-07-19 11:08:02 +0100133#define MBEDTLS_MPI_UINT_MAX UINT64_MAX
Andres Amaya Garciad7fce002017-07-20 11:49:32 +0100134 #if !defined(MBEDTLS_NO_UDBL_DIVISION)
Gilles Peskine449bd832023-01-11 14:50:10 +0100135/* mbedtls_t_udbl defined as 128-bit unsigned int */
136typedef unsigned int mbedtls_t_udbl __attribute__((mode(TI)));
Andres Amaya Garciad7fce002017-07-20 11:49:32 +0100137 #define MBEDTLS_HAVE_UDBL
138 #endif /* !MBEDTLS_NO_UDBL_DIVISION */
Ko-05cff952018-08-20 12:59:57 +0100139 #elif defined(__ARMCC_VERSION) && defined(__aarch64__)
Gilles Peskine449bd832023-01-11 14:50:10 +0100140/*
141 * __ARMCC_VERSION is defined for both armcc and armclang and
142 * __aarch64__ is only defined by armclang when compiling 64-bit code
143 */
Andres Amaya Garciad7fce002017-07-20 11:49:32 +0100144 #if !defined(MBEDTLS_HAVE_INT64)
145 #define MBEDTLS_HAVE_INT64
146 #endif /* !MBEDTLS_HAVE_INT64 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100147typedef int64_t mbedtls_mpi_sint;
148typedef uint64_t mbedtls_mpi_uint;
Agathiyan Bragadeesheed55c62023-07-19 11:08:02 +0100149#define MBEDTLS_MPI_UINT_MAX UINT64_MAX
Andres Amaya Garciad7fce002017-07-20 11:49:32 +0100150 #if !defined(MBEDTLS_NO_UDBL_DIVISION)
Gilles Peskine449bd832023-01-11 14:50:10 +0100151/* mbedtls_t_udbl defined as 128-bit unsigned int */
152typedef __uint128_t mbedtls_t_udbl;
Andres Amaya Garciad7fce002017-07-20 11:49:32 +0100153 #define MBEDTLS_HAVE_UDBL
154 #endif /* !MBEDTLS_NO_UDBL_DIVISION */
155 #elif defined(MBEDTLS_HAVE_INT64)
Gilles Peskine449bd832023-01-11 14:50:10 +0100156/* Force 64-bit integers with unknown compiler */
157typedef int64_t mbedtls_mpi_sint;
158typedef uint64_t mbedtls_mpi_uint;
Agathiyan Bragadeesheed55c62023-07-19 11:08:02 +0100159#define MBEDTLS_MPI_UINT_MAX UINT64_MAX
Andres Amaya Garciaaa27dfe2017-05-04 11:05:55 +0100160 #endif
161#endif /* !MBEDTLS_HAVE_INT32 */
162
163#if !defined(MBEDTLS_HAVE_INT64)
Gilles Peskine449bd832023-01-11 14:50:10 +0100164/* Default to 32-bit compilation */
Andres Amaya Garciaaa27dfe2017-05-04 11:05:55 +0100165 #if !defined(MBEDTLS_HAVE_INT32)
166 #define MBEDTLS_HAVE_INT32
167 #endif /* !MBEDTLS_HAVE_INT32 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100168typedef int32_t mbedtls_mpi_sint;
169typedef uint32_t mbedtls_mpi_uint;
Agathiyan Bragadeesheed55c62023-07-19 11:08:02 +0100170#define MBEDTLS_MPI_UINT_MAX UINT32_MAX
Andres Amaya Garciad7fce002017-07-20 11:49:32 +0100171 #if !defined(MBEDTLS_NO_UDBL_DIVISION)
Gilles Peskine449bd832023-01-11 14:50:10 +0100172typedef uint64_t mbedtls_t_udbl;
Andres Amaya Garciadf1486a2017-07-20 17:33:09 +0100173 #define MBEDTLS_HAVE_UDBL
Andres Amaya Garciad7fce002017-07-20 11:49:32 +0100174 #endif /* !MBEDTLS_NO_UDBL_DIVISION */
Andres Amaya Garciaaa27dfe2017-05-04 11:05:55 +0100175#endif /* !MBEDTLS_HAVE_INT64 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000176
Dave Rodgman85061b92023-09-06 08:41:05 +0100177/*
178 * Sanity check that exactly one of MBEDTLS_HAVE_INT32 or MBEDTLS_HAVE_INT64 is defined,
179 * so that code elsewhere doesn't have to check.
180 */
181#if (!(defined(MBEDTLS_HAVE_INT32) || defined(MBEDTLS_HAVE_INT64))) || \
182 (defined(MBEDTLS_HAVE_INT32) && defined(MBEDTLS_HAVE_INT64))
183#error "Only 32-bit or 64-bit limbs are supported in bignum"
184#endif
185
Gilles Peskine6110a162022-11-15 21:22:27 +0100186/** \typedef mbedtls_mpi_uint
187 * \brief The type of machine digits in a bignum, called _limbs_.
188 *
189 * This is always an unsigned integer type with no padding bits. The size
190 * is platform-dependent.
191 */
192
193/** \typedef mbedtls_mpi_sint
194 * \brief The signed type corresponding to #mbedtls_mpi_uint.
195 *
196 * This is always an signed integer type with no padding bits. The size
197 * is platform-dependent.
198 */
199
Paul Bakker407a0da2013-06-27 14:29:21 +0200200#ifdef __cplusplus
201extern "C" {
202#endif
203
Paul Bakker5121ce52009-01-03 21:22:43 +0000204/**
205 * \brief MPI structure
206 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100207typedef struct mbedtls_mpi {
Dave Rodgman98e632f2023-07-11 15:59:14 +0100208 /** Pointer to limbs.
209 *
210 * This may be \c NULL if \c n is 0.
211 */
212 mbedtls_mpi_uint *MBEDTLS_PRIVATE(p);
213
Gilles Peskine12889572022-11-09 21:55:33 +0100214 /** Sign: -1 if the mpi is negative, 1 otherwise.
215 *
216 * The number 0 must be represented with `s = +1`. Although many library
217 * functions treat all-limbs-zero as equivalent to a valid representation
218 * of 0 regardless of the sign bit, there are exceptions, so bignum
219 * functions and external callers must always set \c s to +1 for the
220 * number zero.
221 *
222 * Note that this implies that calloc() or `... = {0}` does not create
223 * a valid MPI representation. You must call mbedtls_mpi_init().
224 */
Gilles Peskine053022f2023-06-29 19:26:48 +0200225 signed short MBEDTLS_PRIVATE(s);
Gilles Peskine12889572022-11-09 21:55:33 +0100226
227 /** Total number of limbs in \c p. */
Gilles Peskine053022f2023-06-29 19:26:48 +0200228 unsigned short MBEDTLS_PRIVATE(n);
Gilles Peskine24a305e2023-07-18 13:41:22 +0200229 /* Make sure that MBEDTLS_MPI_MAX_LIMBS fits in n.
230 * Use the same limit value on all platforms so that we don't have to
231 * think about different behavior on the rare platforms where
232 * unsigned short can store values larger than the minimum required by
233 * the C language, which is 65535.
234 */
Gilles Peskine053022f2023-06-29 19:26:48 +0200235#if MBEDTLS_MPI_MAX_LIMBS > 65535
236#error "MBEDTLS_MPI_MAX_LIMBS > 65535 is not supported"
237#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000238}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200239mbedtls_mpi;
Paul Bakker5121ce52009-01-03 21:22:43 +0000240
Waleed Elmelegycca4dbe2024-09-25 17:49:14 +0100241#define MBEDTLS_MPI_INIT { 0, 0, 0 }
242
Paul Bakker5121ce52009-01-03 21:22:43 +0000243/**
Hanno Beckerc23483e2018-12-10 17:21:19 +0000244 * \brief Initialize an MPI context.
245 *
246 * This makes the MPI ready to be set or freed,
Manuel Pégourié-Gonnardda61ed32015-04-30 10:28:51 +0200247 * but does not define a value for the MPI.
Paul Bakker6c591fa2011-05-05 11:49:20 +0000248 *
Hanno Becker8282c2f2018-12-12 13:36:46 +0000249 * \param X The MPI context to initialize. This must not be \c NULL.
Paul Bakker5121ce52009-01-03 21:22:43 +0000250 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100251void mbedtls_mpi_init(mbedtls_mpi *X);
Paul Bakker5121ce52009-01-03 21:22:43 +0000252
253/**
Hanno Becker8ce11a32018-12-19 16:18:52 +0000254 * \brief This function frees the components of an MPI context.
Paul Bakker6c591fa2011-05-05 11:49:20 +0000255 *
Hanno Beckere1185042018-12-13 14:31:46 +0000256 * \param X The MPI context to be cleared. This may be \c NULL,
Hanno Becker8282c2f2018-12-12 13:36:46 +0000257 * in which case this function is a no-op. If it is
258 * not \c NULL, it must point to an initialized MPI.
Paul Bakker5121ce52009-01-03 21:22:43 +0000259 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100260void mbedtls_mpi_free(mbedtls_mpi *X);
Paul Bakker5121ce52009-01-03 21:22:43 +0000261
262/**
Hanno Beckerc23483e2018-12-10 17:21:19 +0000263 * \brief Enlarge an MPI to the specified number of limbs.
Paul Bakker5121ce52009-01-03 21:22:43 +0000264 *
Hanno Beckerc23483e2018-12-10 17:21:19 +0000265 * \note This function does nothing if the MPI is
266 * already large enough.
Gilles Peskine70ad8392018-03-21 16:28:41 +0100267 *
Hanno Becker8282c2f2018-12-12 13:36:46 +0000268 * \param X The MPI to grow. It must be initialized.
Hanno Beckerc23483e2018-12-10 17:21:19 +0000269 * \param nblimbs The target number of limbs.
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000270 *
Hanno Beckerc23483e2018-12-10 17:21:19 +0000271 * \return \c 0 if successful.
272 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed.
273 * \return Another negative error code on other kinds of failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000274 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100275int mbedtls_mpi_grow(mbedtls_mpi *X, size_t nblimbs);
Paul Bakker5121ce52009-01-03 21:22:43 +0000276
277/**
Hanno Beckere1185042018-12-13 14:31:46 +0000278 * \brief This function resizes an MPI downwards, keeping at least the
279 * specified number of limbs.
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100280 *
Gilles Peskine70ad8392018-03-21 16:28:41 +0100281 * If \c X is smaller than \c nblimbs, it is resized up
282 * instead.
283 *
Hanno Becker8282c2f2018-12-12 13:36:46 +0000284 * \param X The MPI to shrink. This must point to an initialized MPI.
Hanno Beckerc23483e2018-12-10 17:21:19 +0000285 * \param nblimbs The minimum number of limbs to keep.
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100286 *
Hanno Beckerc23483e2018-12-10 17:21:19 +0000287 * \return \c 0 if successful.
288 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Gilles Peskine70ad8392018-03-21 16:28:41 +0100289 * (this can only happen when resizing up).
Hanno Beckerc23483e2018-12-10 17:21:19 +0000290 * \return Another negative error code on other kinds of failure.
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100291 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100292int mbedtls_mpi_shrink(mbedtls_mpi *X, size_t nblimbs);
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100293
294/**
Hanno Beckerc23483e2018-12-10 17:21:19 +0000295 * \brief Make a copy of an MPI.
Paul Bakker5121ce52009-01-03 21:22:43 +0000296 *
Hanno Becker8282c2f2018-12-12 13:36:46 +0000297 * \param X The destination MPI. This must point to an initialized MPI.
298 * \param Y The source MPI. This must point to an initialized MPI.
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000299 *
Hanno Beckerc23483e2018-12-10 17:21:19 +0000300 * \note The limb-buffer in the destination MPI is enlarged
301 * if necessary to hold the value in the source MPI.
302 *
303 * \return \c 0 if successful.
304 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed.
305 * \return Another negative error code on other kinds of failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000306 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100307int mbedtls_mpi_copy(mbedtls_mpi *X, const mbedtls_mpi *Y);
Paul Bakker5121ce52009-01-03 21:22:43 +0000308
309/**
Hanno Beckerc23483e2018-12-10 17:21:19 +0000310 * \brief Swap the contents of two MPIs.
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000311 *
Hanno Becker8282c2f2018-12-12 13:36:46 +0000312 * \param X The first MPI. It must be initialized.
313 * \param Y The second MPI. It must be initialized.
Paul Bakker5121ce52009-01-03 21:22:43 +0000314 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100315void mbedtls_mpi_swap(mbedtls_mpi *X, mbedtls_mpi *Y);
Paul Bakker5121ce52009-01-03 21:22:43 +0000316
317/**
Hanno Beckere1185042018-12-13 14:31:46 +0000318 * \brief Perform a safe conditional copy of MPI which doesn't
319 * reveal whether the condition was true or not.
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100320 *
Hanno Becker8282c2f2018-12-12 13:36:46 +0000321 * \param X The MPI to conditionally assign to. This must point
322 * to an initialized MPI.
323 * \param Y The MPI to be assigned from. This must point to an
324 * initialized MPI.
Hanno Beckere1185042018-12-13 14:31:46 +0000325 * \param assign The condition deciding whether to perform the
Tom Cosgrove583816c2022-08-18 14:09:18 +0100326 * assignment or not. Must be either 0 or 1:
Hanno Beckerc23483e2018-12-10 17:21:19 +0000327 * * \c 1: Perform the assignment `X = Y`.
328 * * \c 0: Keep the original value of \p X.
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100329 *
330 * \note This function is equivalent to
Hanno Beckerc23483e2018-12-10 17:21:19 +0000331 * `if( assign ) mbedtls_mpi_copy( X, Y );`
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100332 * except that it avoids leaking any information about whether
333 * the assignment was done or not (the above code may leak
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +0100334 * information through branch prediction and/or memory access
335 * patterns analysis).
Hanno Beckerc23483e2018-12-10 17:21:19 +0000336 *
Tom Cosgrove583816c2022-08-18 14:09:18 +0100337 * \warning If \p assign is neither 0 nor 1, the result of this function
338 * is indeterminate, and the resulting value in \p X might be
339 * neither its original value nor the value in \p Y.
340 *
Hanno Beckerc23483e2018-12-10 17:21:19 +0000341 * \return \c 0 if successful.
342 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed.
343 * \return Another negative error code on other kinds of failure.
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100344 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100345int mbedtls_mpi_safe_cond_assign(mbedtls_mpi *X, const mbedtls_mpi *Y, unsigned char assign);
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100346
347/**
Hanno Beckere1185042018-12-13 14:31:46 +0000348 * \brief Perform a safe conditional swap which doesn't
349 * reveal whether the condition was true or not.
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100350 *
Hanno Becker8282c2f2018-12-12 13:36:46 +0000351 * \param X The first MPI. This must be initialized.
352 * \param Y The second MPI. This must be initialized.
Tom Cosgrove583816c2022-08-18 14:09:18 +0100353 * \param swap The condition deciding whether to perform
354 * the swap or not. Must be either 0 or 1:
Hanno Beckerc23483e2018-12-10 17:21:19 +0000355 * * \c 1: Swap the values of \p X and \p Y.
356 * * \c 0: Keep the original values of \p X and \p Y.
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100357 *
358 * \note This function is equivalent to
Tom Cosgrove583816c2022-08-18 14:09:18 +0100359 * if( swap ) mbedtls_mpi_swap( X, Y );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100360 * except that it avoids leaking any information about whether
Tom Cosgrove583816c2022-08-18 14:09:18 +0100361 * the swap was done or not (the above code may leak
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100362 * information through branch prediction and/or memory access
363 * patterns analysis).
Hanno Beckerc23483e2018-12-10 17:21:19 +0000364 *
Tom Cosgrove583816c2022-08-18 14:09:18 +0100365 * \warning If \p swap is neither 0 nor 1, the result of this function
366 * is indeterminate, and both \p X and \p Y might end up with
367 * values different to either of the original ones.
368 *
Hanno Beckerc23483e2018-12-10 17:21:19 +0000369 * \return \c 0 if successful.
370 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed.
371 * \return Another negative error code on other kinds of failure.
372 *
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100373 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100374int mbedtls_mpi_safe_cond_swap(mbedtls_mpi *X, mbedtls_mpi *Y, unsigned char swap);
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100375
376/**
Hanno Beckerc23483e2018-12-10 17:21:19 +0000377 * \brief Store integer value in MPI.
Paul Bakker5121ce52009-01-03 21:22:43 +0000378 *
Hanno Becker8282c2f2018-12-12 13:36:46 +0000379 * \param X The MPI to set. This must be initialized.
Hanno Beckerc23483e2018-12-10 17:21:19 +0000380 * \param z The value to use.
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000381 *
Hanno Beckerc23483e2018-12-10 17:21:19 +0000382 * \return \c 0 if successful.
383 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed.
384 * \return Another negative error code on other kinds of failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000385 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100386int mbedtls_mpi_lset(mbedtls_mpi *X, mbedtls_mpi_sint z);
Paul Bakker5121ce52009-01-03 21:22:43 +0000387
Paul Bakker9a736322012-11-14 12:39:52 +0000388/**
Hanno Beckerc23483e2018-12-10 17:21:19 +0000389 * \brief Get a specific bit from an MPI.
Paul Bakker2f5947e2011-05-18 15:47:11 +0000390 *
Hanno Becker8282c2f2018-12-12 13:36:46 +0000391 * \param X The MPI to query. This must be initialized.
Hanno Beckerc23483e2018-12-10 17:21:19 +0000392 * \param pos Zero-based index of the bit to query.
Paul Bakker2f5947e2011-05-18 15:47:11 +0000393 *
Hanno Beckerc23483e2018-12-10 17:21:19 +0000394 * \return \c 0 or \c 1 on success, depending on whether bit \c pos
Hanno Becker8282c2f2018-12-12 13:36:46 +0000395 * of \c X is unset or set.
Hanno Beckerc23483e2018-12-10 17:21:19 +0000396 * \return A negative error code on failure.
Paul Bakker2f5947e2011-05-18 15:47:11 +0000397 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100398int mbedtls_mpi_get_bit(const mbedtls_mpi *X, size_t pos);
Paul Bakker2f5947e2011-05-18 15:47:11 +0000399
Paul Bakker9a736322012-11-14 12:39:52 +0000400/**
Hanno Beckerc23483e2018-12-10 17:21:19 +0000401 * \brief Modify a specific bit in an MPI.
Paul Bakker2f5947e2011-05-18 15:47:11 +0000402 *
Hanno Beckerc23483e2018-12-10 17:21:19 +0000403 * \note This function will grow the target MPI if necessary to set a
404 * bit to \c 1 in a not yet existing limb. It will not grow if
405 * the bit should be set to \c 0.
Paul Bakker2f5947e2011-05-18 15:47:11 +0000406 *
Hanno Becker8282c2f2018-12-12 13:36:46 +0000407 * \param X The MPI to modify. This must be initialized.
Hanno Beckerc23483e2018-12-10 17:21:19 +0000408 * \param pos Zero-based index of the bit to modify.
409 * \param val The desired value of bit \c pos: \c 0 or \c 1.
Paul Bakker2f5947e2011-05-18 15:47:11 +0000410 *
Hanno Beckerc23483e2018-12-10 17:21:19 +0000411 * \return \c 0 if successful.
412 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed.
413 * \return Another negative error code on other kinds of failure.
Paul Bakker2f5947e2011-05-18 15:47:11 +0000414 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100415int mbedtls_mpi_set_bit(mbedtls_mpi *X, size_t pos, unsigned char val);
Paul Bakker2f5947e2011-05-18 15:47:11 +0000416
Paul Bakker5121ce52009-01-03 21:22:43 +0000417/**
Hanno Beckere1185042018-12-13 14:31:46 +0000418 * \brief Return the number of bits of value \c 0 before the
419 * least significant bit of value \c 1.
Paul Bakker6b906e52012-05-08 12:01:43 +0000420 *
Hanno Beckerc23483e2018-12-10 17:21:19 +0000421 * \note This is the same as the zero-based index of
Hanno Beckere1185042018-12-13 14:31:46 +0000422 * the least significant bit of value \c 1.
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000423 *
Hanno Beckerc23483e2018-12-10 17:21:19 +0000424 * \param X The MPI to query.
425 *
Hanno Beckere1185042018-12-13 14:31:46 +0000426 * \return The number of bits of value \c 0 before the least significant
427 * bit of value \c 1 in \p X.
Paul Bakker5121ce52009-01-03 21:22:43 +0000428 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100429size_t mbedtls_mpi_lsb(const mbedtls_mpi *X);
Paul Bakker5121ce52009-01-03 21:22:43 +0000430
431/**
Paul Bakker6b906e52012-05-08 12:01:43 +0000432 * \brief Return the number of bits up to and including the most
Hanno Beckere1185042018-12-13 14:31:46 +0000433 * significant bit of value \c 1.
Paul Bakker6b906e52012-05-08 12:01:43 +0000434 *
Hanno Beckerc23483e2018-12-10 17:21:19 +0000435 * * \note This is same as the one-based index of the most
Hanno Beckere1185042018-12-13 14:31:46 +0000436 * significant bit of value \c 1.
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000437 *
Hanno Becker8282c2f2018-12-12 13:36:46 +0000438 * \param X The MPI to query. This must point to an initialized MPI.
Hanno Beckerc23483e2018-12-10 17:21:19 +0000439 *
440 * \return The number of bits up to and including the most
Hanno Beckere1185042018-12-13 14:31:46 +0000441 * significant bit of value \c 1.
Paul Bakker5121ce52009-01-03 21:22:43 +0000442 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100443size_t mbedtls_mpi_bitlen(const mbedtls_mpi *X);
Paul Bakker5121ce52009-01-03 21:22:43 +0000444
445/**
Hanno Beckere1185042018-12-13 14:31:46 +0000446 * \brief Return the total size of an MPI value in bytes.
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000447 *
Hanno Becker8282c2f2018-12-12 13:36:46 +0000448 * \param X The MPI to use. This must point to an initialized MPI.
Hanno Beckerc23483e2018-12-10 17:21:19 +0000449 *
450 * \note The value returned by this function may be less than
451 * the number of bytes used to store \p X internally.
Hanno Beckere1185042018-12-13 14:31:46 +0000452 * This happens if and only if there are trailing bytes
453 * of value zero.
Hanno Beckerc23483e2018-12-10 17:21:19 +0000454 *
455 * \return The least number of bytes capable of storing
456 * the absolute value of \p X.
Paul Bakker5121ce52009-01-03 21:22:43 +0000457 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100458size_t mbedtls_mpi_size(const mbedtls_mpi *X);
Paul Bakker5121ce52009-01-03 21:22:43 +0000459
460/**
Hanno Beckerc23483e2018-12-10 17:21:19 +0000461 * \brief Import an MPI from an ASCII string.
Paul Bakker5121ce52009-01-03 21:22:43 +0000462 *
Hanno Becker8282c2f2018-12-12 13:36:46 +0000463 * \param X The destination MPI. This must point to an initialized MPI.
Hanno Beckerc23483e2018-12-10 17:21:19 +0000464 * \param radix The numeric base of the input string.
465 * \param s Null-terminated string buffer.
Paul Bakker5121ce52009-01-03 21:22:43 +0000466 *
Hanno Beckerc23483e2018-12-10 17:21:19 +0000467 * \return \c 0 if successful.
468 * \return A negative error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000469 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100470int mbedtls_mpi_read_string(mbedtls_mpi *X, int radix, const char *s);
Paul Bakker5121ce52009-01-03 21:22:43 +0000471
472/**
Hanno Beckere1185042018-12-13 14:31:46 +0000473 * \brief Export an MPI to an ASCII string.
Paul Bakker5121ce52009-01-03 21:22:43 +0000474 *
Hanno Becker8282c2f2018-12-12 13:36:46 +0000475 * \param X The source MPI. This must point to an initialized MPI.
Hanno Beckerc23483e2018-12-10 17:21:19 +0000476 * \param radix The numeric base of the output string.
Hanno Beckere1185042018-12-13 14:31:46 +0000477 * \param buf The buffer to write the string to. This must be writable
Hanno Beckerd7310122018-12-18 18:11:42 +0000478 * buffer of length \p buflen Bytes.
Hanno Beckerc23483e2018-12-10 17:21:19 +0000479 * \param buflen The available size in Bytes of \p buf.
Hanno Beckere1185042018-12-13 14:31:46 +0000480 * \param olen The address at which to store the length of the string
481 * written, including the final \c NULL byte. This must
482 * not be \c NULL.
Paul Bakker5121ce52009-01-03 21:22:43 +0000483 *
Hanno Beckere1185042018-12-13 14:31:46 +0000484 * \note You can call this function with `buflen == 0` to obtain the
Hanno Beckerc23483e2018-12-10 17:21:19 +0000485 * minimum required buffer size in `*olen`.
Paul Bakker5121ce52009-01-03 21:22:43 +0000486 *
Hanno Beckerc23483e2018-12-10 17:21:19 +0000487 * \return \c 0 if successful.
488 * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if the target buffer \p buf
489 * is too small to hold the value of \p X in the desired base.
490 * In this case, `*olen` is nonetheless updated to contain the
491 * size of \p buf required for a successful call.
492 * \return Another negative error code on different kinds of failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000493 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100494int mbedtls_mpi_write_string(const mbedtls_mpi *X, int radix,
495 char *buf, size_t buflen, size_t *olen);
Paul Bakker5121ce52009-01-03 21:22:43 +0000496
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200497#if defined(MBEDTLS_FS_IO)
Paul Bakker5121ce52009-01-03 21:22:43 +0000498/**
Hanno Beckerc23483e2018-12-10 17:21:19 +0000499 * \brief Read an MPI from a line in an opened file.
Paul Bakker5121ce52009-01-03 21:22:43 +0000500 *
Hanno Becker8282c2f2018-12-12 13:36:46 +0000501 * \param X The destination MPI. This must point to an initialized MPI.
Hanno Beckerc23483e2018-12-10 17:21:19 +0000502 * \param radix The numeric base of the string representation used
503 * in the source line.
Hanno Becker8282c2f2018-12-12 13:36:46 +0000504 * \param fin The input file handle to use. This must not be \c NULL.
Hanno Beckerb2034b72017-04-26 11:46:46 +0100505 *
506 * \note On success, this function advances the file stream
507 * to the end of the current line or to EOF.
508 *
Hanno Beckerc23483e2018-12-10 17:21:19 +0000509 * The function returns \c 0 on an empty line.
Hanno Beckerb2034b72017-04-26 11:46:46 +0100510 *
511 * Leading whitespaces are ignored, as is a
Hanno Beckerc23483e2018-12-10 17:21:19 +0000512 * '0x' prefix for radix \c 16.
Hanno Beckerb2034b72017-04-26 11:46:46 +0100513 *
Hanno Beckerc23483e2018-12-10 17:21:19 +0000514 * \return \c 0 if successful.
515 * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if the file read buffer
516 * is too small.
517 * \return Another negative error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000518 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100519int mbedtls_mpi_read_file(mbedtls_mpi *X, int radix, FILE *fin);
Paul Bakker5121ce52009-01-03 21:22:43 +0000520
521/**
Hanno Beckerc23483e2018-12-10 17:21:19 +0000522 * \brief Export an MPI into an opened file.
Paul Bakker5121ce52009-01-03 21:22:43 +0000523 *
Hanno Beckerc23483e2018-12-10 17:21:19 +0000524 * \param p A string prefix to emit prior to the MPI data.
525 * For example, this might be a label, or "0x" when
Hanno Beckere1185042018-12-13 14:31:46 +0000526 * printing in base \c 16. This may be \c NULL if no prefix
Hanno Beckerc23483e2018-12-10 17:21:19 +0000527 * is needed.
Hanno Becker8282c2f2018-12-12 13:36:46 +0000528 * \param X The source MPI. This must point to an initialized MPI.
Hanno Beckerc23483e2018-12-10 17:21:19 +0000529 * \param radix The numeric base to be used in the emitted string.
Hanno Beckere1185042018-12-13 14:31:46 +0000530 * \param fout The output file handle. This may be \c NULL, in which case
531 * the output is written to \c stdout.
Paul Bakker5121ce52009-01-03 21:22:43 +0000532 *
Hanno Beckerc23483e2018-12-10 17:21:19 +0000533 * \return \c 0 if successful.
534 * \return A negative error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000535 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100536int mbedtls_mpi_write_file(const char *p, const mbedtls_mpi *X,
537 int radix, FILE *fout);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200538#endif /* MBEDTLS_FS_IO */
Paul Bakker5121ce52009-01-03 21:22:43 +0000539
540/**
Hanno Beckerc23483e2018-12-10 17:21:19 +0000541 * \brief Import an MPI from unsigned big endian binary data.
Paul Bakker5121ce52009-01-03 21:22:43 +0000542 *
Hanno Becker8282c2f2018-12-12 13:36:46 +0000543 * \param X The destination MPI. This must point to an initialized MPI.
Hanno Beckere1185042018-12-13 14:31:46 +0000544 * \param buf The input buffer. This must be a readable buffer of length
Hanno Beckerc23483e2018-12-10 17:21:19 +0000545 * \p buflen Bytes.
Andrzej Kurek69ed8c42022-02-17 10:06:44 -0500546 * \param buflen The length of the input buffer \p buf in Bytes.
Paul Bakker5121ce52009-01-03 21:22:43 +0000547 *
Hanno Beckerc23483e2018-12-10 17:21:19 +0000548 * \return \c 0 if successful.
549 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed.
550 * \return Another negative error code on different kinds of failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000551 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100552int mbedtls_mpi_read_binary(mbedtls_mpi *X, const unsigned char *buf,
553 size_t buflen);
Paul Bakker5121ce52009-01-03 21:22:43 +0000554
555/**
Janos Follatha778a942019-02-13 10:28:28 +0000556 * \brief Import X from unsigned binary data, little endian
557 *
558 * \param X The destination MPI. This must point to an initialized MPI.
559 * \param buf The input buffer. This must be a readable buffer of length
560 * \p buflen Bytes.
Andrzej Kurek69ed8c42022-02-17 10:06:44 -0500561 * \param buflen The length of the input buffer \p buf in Bytes.
Janos Follatha778a942019-02-13 10:28:28 +0000562 *
563 * \return \c 0 if successful.
564 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed.
565 * \return Another negative error code on different kinds of failure.
566 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100567int mbedtls_mpi_read_binary_le(mbedtls_mpi *X,
568 const unsigned char *buf, size_t buflen);
Janos Follatha778a942019-02-13 10:28:28 +0000569
570/**
571 * \brief Export X into unsigned binary data, big endian.
572 * Always fills the whole buffer, which will start with zeros
573 * if the number is smaller.
Paul Bakker5121ce52009-01-03 21:22:43 +0000574 *
Hanno Becker8282c2f2018-12-12 13:36:46 +0000575 * \param X The source MPI. This must point to an initialized MPI.
Hanno Beckere1185042018-12-13 14:31:46 +0000576 * \param buf The output buffer. This must be a writable buffer of length
Hanno Beckerc23483e2018-12-10 17:21:19 +0000577 * \p buflen Bytes.
578 * \param buflen The size of the output buffer \p buf in Bytes.
Paul Bakker5121ce52009-01-03 21:22:43 +0000579 *
Hanno Beckerc23483e2018-12-10 17:21:19 +0000580 * \return \c 0 if successful.
581 * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p buf isn't
Hanno Beckere1185042018-12-13 14:31:46 +0000582 * large enough to hold the value of \p X.
Hanno Beckerc23483e2018-12-10 17:21:19 +0000583 * \return Another negative error code on different kinds of failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000584 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100585int mbedtls_mpi_write_binary(const mbedtls_mpi *X, unsigned char *buf,
586 size_t buflen);
Paul Bakker5121ce52009-01-03 21:22:43 +0000587
588/**
Janos Follathe344d0f2019-02-19 16:17:40 +0000589 * \brief Export X into unsigned binary data, little endian.
590 * Always fills the whole buffer, which will end with zeros
591 * if the number is smaller.
592 *
593 * \param X The source MPI. This must point to an initialized MPI.
594 * \param buf The output buffer. This must be a writable buffer of length
595 * \p buflen Bytes.
596 * \param buflen The size of the output buffer \p buf in Bytes.
597 *
598 * \return \c 0 if successful.
599 * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p buf isn't
600 * large enough to hold the value of \p X.
601 * \return Another negative error code on different kinds of failure.
602 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100603int mbedtls_mpi_write_binary_le(const mbedtls_mpi *X,
604 unsigned char *buf, size_t buflen);
Janos Follathe344d0f2019-02-19 16:17:40 +0000605
606/**
Hanno Beckerc23483e2018-12-10 17:21:19 +0000607 * \brief Perform a left-shift on an MPI: X <<= count
Paul Bakker5121ce52009-01-03 21:22:43 +0000608 *
Hanno Becker8282c2f2018-12-12 13:36:46 +0000609 * \param X The MPI to shift. This must point to an initialized MPI.
Minos Galanakis2056d092023-05-02 14:53:58 +0100610 * The MPI pointed by \p X may be resized to fit
611 * the resulting number.
Hanno Beckere1185042018-12-13 14:31:46 +0000612 * \param count The number of bits to shift by.
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000613 *
Hanno Beckerc23483e2018-12-10 17:21:19 +0000614 * \return \c 0 if successful.
615 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
616 * \return Another negative error code on different kinds of failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000617 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100618int mbedtls_mpi_shift_l(mbedtls_mpi *X, size_t count);
Paul Bakker5121ce52009-01-03 21:22:43 +0000619
620/**
Hanno Beckerc23483e2018-12-10 17:21:19 +0000621 * \brief Perform a right-shift on an MPI: X >>= count
Paul Bakker5121ce52009-01-03 21:22:43 +0000622 *
Hanno Becker8282c2f2018-12-12 13:36:46 +0000623 * \param X The MPI to shift. This must point to an initialized MPI.
Hanno Beckere1185042018-12-13 14:31:46 +0000624 * \param count The number of bits to shift by.
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000625 *
Hanno Beckerc23483e2018-12-10 17:21:19 +0000626 * \return \c 0 if successful.
627 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
628 * \return Another negative error code on different kinds of failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000629 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100630int mbedtls_mpi_shift_r(mbedtls_mpi *X, size_t count);
Paul Bakker5121ce52009-01-03 21:22:43 +0000631
632/**
Hanno Beckerc23483e2018-12-10 17:21:19 +0000633 * \brief Compare the absolute values of two MPIs.
Paul Bakker5121ce52009-01-03 21:22:43 +0000634 *
Hanno Becker8282c2f2018-12-12 13:36:46 +0000635 * \param X The left-hand MPI. This must point to an initialized MPI.
636 * \param Y The right-hand MPI. This must point to an initialized MPI.
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000637 *
Hanno Beckerc23483e2018-12-10 17:21:19 +0000638 * \return \c 1 if `|X|` is greater than `|Y|`.
639 * \return \c -1 if `|X|` is lesser than `|Y|`.
Hanno Beckere1185042018-12-13 14:31:46 +0000640 * \return \c 0 if `|X|` is equal to `|Y|`.
Paul Bakker5121ce52009-01-03 21:22:43 +0000641 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100642int mbedtls_mpi_cmp_abs(const mbedtls_mpi *X, const mbedtls_mpi *Y);
Paul Bakker5121ce52009-01-03 21:22:43 +0000643
644/**
Hanno Beckerc23483e2018-12-10 17:21:19 +0000645 * \brief Compare two MPIs.
Paul Bakker5121ce52009-01-03 21:22:43 +0000646 *
Hanno Becker8282c2f2018-12-12 13:36:46 +0000647 * \param X The left-hand MPI. This must point to an initialized MPI.
648 * \param Y The right-hand MPI. This must point to an initialized MPI.
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000649 *
Hanno Beckere1185042018-12-13 14:31:46 +0000650 * \return \c 1 if \p X is greater than \p Y.
651 * \return \c -1 if \p X is lesser than \p Y.
652 * \return \c 0 if \p X is equal to \p Y.
Paul Bakker5121ce52009-01-03 21:22:43 +0000653 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100654int mbedtls_mpi_cmp_mpi(const mbedtls_mpi *X, const mbedtls_mpi *Y);
Paul Bakker5121ce52009-01-03 21:22:43 +0000655
656/**
Janos Follath0e5532d2019-10-11 14:21:53 +0100657 * \brief Check if an MPI is less than the other in constant time.
Janos Follathee6abce2019-09-05 14:47:19 +0100658 *
659 * \param X The left-hand MPI. This must point to an initialized MPI
660 * with the same allocated length as Y.
661 * \param Y The right-hand MPI. This must point to an initialized MPI
662 * with the same allocated length as X.
663 * \param ret The result of the comparison:
Janos Follath0e5532d2019-10-11 14:21:53 +0100664 * \c 1 if \p X is less than \p Y.
665 * \c 0 if \p X is greater than or equal to \p Y.
Janos Follathee6abce2019-09-05 14:47:19 +0100666 *
667 * \return 0 on success.
668 * \return MBEDTLS_ERR_MPI_BAD_INPUT_DATA if the allocated length of
669 * the two input MPIs is not the same.
670 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100671int mbedtls_mpi_lt_mpi_ct(const mbedtls_mpi *X, const mbedtls_mpi *Y,
672 unsigned *ret);
Janos Follathee6abce2019-09-05 14:47:19 +0100673
674/**
Hanno Beckerc23483e2018-12-10 17:21:19 +0000675 * \brief Compare an MPI with an integer.
Paul Bakker5121ce52009-01-03 21:22:43 +0000676 *
Hanno Becker8282c2f2018-12-12 13:36:46 +0000677 * \param X The left-hand MPI. This must point to an initialized MPI.
Hanno Beckerc23483e2018-12-10 17:21:19 +0000678 * \param z The integer value to compare \p X to.
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000679 *
Hanno Beckere1185042018-12-13 14:31:46 +0000680 * \return \c 1 if \p X is greater than \p z.
681 * \return \c -1 if \p X is lesser than \p z.
682 * \return \c 0 if \p X is equal to \p z.
Paul Bakker5121ce52009-01-03 21:22:43 +0000683 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100684int mbedtls_mpi_cmp_int(const mbedtls_mpi *X, mbedtls_mpi_sint z);
Paul Bakker5121ce52009-01-03 21:22:43 +0000685
686/**
Hanno Beckerc23483e2018-12-10 17:21:19 +0000687 * \brief Perform an unsigned addition of MPIs: X = |A| + |B|
Paul Bakker5121ce52009-01-03 21:22:43 +0000688 *
Hanno Becker8282c2f2018-12-12 13:36:46 +0000689 * \param X The destination MPI. This must point to an initialized MPI.
690 * \param A The first summand. This must point to an initialized MPI.
691 * \param B The second summand. This must point to an initialized MPI.
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000692 *
Hanno Beckerc23483e2018-12-10 17:21:19 +0000693 * \return \c 0 if successful.
694 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
695 * \return Another negative error code on different kinds of failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000696 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100697int mbedtls_mpi_add_abs(mbedtls_mpi *X, const mbedtls_mpi *A,
698 const mbedtls_mpi *B);
Paul Bakker5121ce52009-01-03 21:22:43 +0000699
700/**
Hanno Beckerc23483e2018-12-10 17:21:19 +0000701 * \brief Perform an unsigned subtraction of MPIs: X = |A| - |B|
Paul Bakker5121ce52009-01-03 21:22:43 +0000702 *
Hanno Becker8282c2f2018-12-12 13:36:46 +0000703 * \param X The destination MPI. This must point to an initialized MPI.
704 * \param A The minuend. This must point to an initialized MPI.
705 * \param B The subtrahend. This must point to an initialized MPI.
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000706 *
Hanno Beckerc23483e2018-12-10 17:21:19 +0000707 * \return \c 0 if successful.
708 * \return #MBEDTLS_ERR_MPI_NEGATIVE_VALUE if \p B is greater than \p A.
709 * \return Another negative error code on different kinds of failure.
710 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000711 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100712int mbedtls_mpi_sub_abs(mbedtls_mpi *X, const mbedtls_mpi *A,
713 const mbedtls_mpi *B);
Paul Bakker5121ce52009-01-03 21:22:43 +0000714
715/**
Hanno Beckerc23483e2018-12-10 17:21:19 +0000716 * \brief Perform a signed addition of MPIs: X = A + B
Paul Bakker5121ce52009-01-03 21:22:43 +0000717 *
Hanno Becker8282c2f2018-12-12 13:36:46 +0000718 * \param X The destination MPI. This must point to an initialized MPI.
719 * \param A The first summand. This must point to an initialized MPI.
720 * \param B The second summand. This must point to an initialized MPI.
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000721 *
Hanno Beckerc23483e2018-12-10 17:21:19 +0000722 * \return \c 0 if successful.
723 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
724 * \return Another negative error code on different kinds of failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000725 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100726int mbedtls_mpi_add_mpi(mbedtls_mpi *X, const mbedtls_mpi *A,
727 const mbedtls_mpi *B);
Paul Bakker5121ce52009-01-03 21:22:43 +0000728
729/**
Hanno Beckerc23483e2018-12-10 17:21:19 +0000730 * \brief Perform a signed subtraction of MPIs: X = A - B
Paul Bakker5121ce52009-01-03 21:22:43 +0000731 *
Hanno Becker8282c2f2018-12-12 13:36:46 +0000732 * \param X The destination MPI. This must point to an initialized MPI.
733 * \param A The minuend. This must point to an initialized MPI.
734 * \param B The subtrahend. This must point to an initialized MPI.
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000735 *
Hanno Beckerc23483e2018-12-10 17:21:19 +0000736 * \return \c 0 if successful.
737 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
738 * \return Another negative error code on different kinds of failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000739 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100740int mbedtls_mpi_sub_mpi(mbedtls_mpi *X, const mbedtls_mpi *A,
741 const mbedtls_mpi *B);
Paul Bakker5121ce52009-01-03 21:22:43 +0000742
743/**
Hanno Beckerc23483e2018-12-10 17:21:19 +0000744 * \brief Perform a signed addition of an MPI and an integer: X = A + b
Paul Bakker5121ce52009-01-03 21:22:43 +0000745 *
Hanno Becker8282c2f2018-12-12 13:36:46 +0000746 * \param X The destination MPI. This must point to an initialized MPI.
747 * \param A The first summand. This must point to an initialized MPI.
Hanno Beckerc23483e2018-12-10 17:21:19 +0000748 * \param b The second summand.
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000749 *
Hanno Beckerc23483e2018-12-10 17:21:19 +0000750 * \return \c 0 if successful.
751 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
752 * \return Another negative error code on different kinds of failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000753 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100754int mbedtls_mpi_add_int(mbedtls_mpi *X, const mbedtls_mpi *A,
755 mbedtls_mpi_sint b);
Paul Bakker5121ce52009-01-03 21:22:43 +0000756
757/**
Hanno Beckerc23483e2018-12-10 17:21:19 +0000758 * \brief Perform a signed subtraction of an MPI and an integer:
759 * X = A - b
Paul Bakker5121ce52009-01-03 21:22:43 +0000760 *
Hanno Becker8282c2f2018-12-12 13:36:46 +0000761 * \param X The destination MPI. This must point to an initialized MPI.
762 * \param A The minuend. This must point to an initialized MPI.
Hanno Becker01c3c102018-12-17 23:04:51 +0000763 * \param b The subtrahend.
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000764 *
Hanno Beckerc23483e2018-12-10 17:21:19 +0000765 * \return \c 0 if successful.
766 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
767 * \return Another negative error code on different kinds of failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000768 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100769int mbedtls_mpi_sub_int(mbedtls_mpi *X, const mbedtls_mpi *A,
770 mbedtls_mpi_sint b);
Paul Bakker5121ce52009-01-03 21:22:43 +0000771
772/**
Hanno Beckerc23483e2018-12-10 17:21:19 +0000773 * \brief Perform a multiplication of two MPIs: X = A * B
Paul Bakker5121ce52009-01-03 21:22:43 +0000774 *
Hanno Becker8282c2f2018-12-12 13:36:46 +0000775 * \param X The destination MPI. This must point to an initialized MPI.
776 * \param A The first factor. This must point to an initialized MPI.
777 * \param B The second factor. This must point to an initialized MPI.
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000778 *
Hanno Beckerc23483e2018-12-10 17:21:19 +0000779 * \return \c 0 if successful.
780 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
781 * \return Another negative error code on different kinds of failure.
782 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000783 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100784int mbedtls_mpi_mul_mpi(mbedtls_mpi *X, const mbedtls_mpi *A,
785 const mbedtls_mpi *B);
Paul Bakker5121ce52009-01-03 21:22:43 +0000786
787/**
Hanno Beckerc23483e2018-12-10 17:21:19 +0000788 * \brief Perform a multiplication of an MPI with an unsigned integer:
789 * X = A * b
Paul Bakker5121ce52009-01-03 21:22:43 +0000790 *
Hanno Becker8282c2f2018-12-12 13:36:46 +0000791 * \param X The destination MPI. This must point to an initialized MPI.
792 * \param A The first factor. This must point to an initialized MPI.
Hanno Beckerc23483e2018-12-10 17:21:19 +0000793 * \param b The second factor.
Manuel Pégourié-Gonnard35f1d7f2015-03-19 12:42:40 +0000794 *
Hanno Beckerc23483e2018-12-10 17:21:19 +0000795 * \return \c 0 if successful.
796 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
797 * \return Another negative error code on different kinds of failure.
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000798 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000799 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100800int mbedtls_mpi_mul_int(mbedtls_mpi *X, const mbedtls_mpi *A,
801 mbedtls_mpi_uint b);
Paul Bakker5121ce52009-01-03 21:22:43 +0000802
803/**
Hanno Beckerc23483e2018-12-10 17:21:19 +0000804 * \brief Perform a division with remainder of two MPIs:
805 * A = Q * B + R
Paul Bakker5121ce52009-01-03 21:22:43 +0000806 *
Hanno Beckerc23483e2018-12-10 17:21:19 +0000807 * \param Q The destination MPI for the quotient.
Hanno Beckerd01ff492018-12-18 23:10:28 +0000808 * This may be \c NULL if the value of the
Glenn Strauss0750d082022-11-08 02:25:01 -0500809 * quotient is not needed. This must not alias A or B.
Hanno Beckerc23483e2018-12-10 17:21:19 +0000810 * \param R The destination MPI for the remainder value.
Hanno Beckerd01ff492018-12-18 23:10:28 +0000811 * This may be \c NULL if the value of the
Glenn Strauss0750d082022-11-08 02:25:01 -0500812 * remainder is not needed. This must not alias A or B.
813 * \param A The dividend. This must point to an initialized MPI.
Hanno Becker8282c2f2018-12-12 13:36:46 +0000814 * \param B The divisor. This must point to an initialized MPI.
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000815 *
Hanno Beckerc23483e2018-12-10 17:21:19 +0000816 * \return \c 0 if successful.
817 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed.
Hanno Beckere1185042018-12-13 14:31:46 +0000818 * \return #MBEDTLS_ERR_MPI_DIVISION_BY_ZERO if \p B equals zero.
Hanno Beckerc23483e2018-12-10 17:21:19 +0000819 * \return Another negative error code on different kinds of failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000820 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100821int mbedtls_mpi_div_mpi(mbedtls_mpi *Q, mbedtls_mpi *R, const mbedtls_mpi *A,
822 const mbedtls_mpi *B);
Paul Bakker5121ce52009-01-03 21:22:43 +0000823
824/**
Hanno Beckerc23483e2018-12-10 17:21:19 +0000825 * \brief Perform a division with remainder of an MPI by an integer:
826 * A = Q * b + R
Paul Bakker5121ce52009-01-03 21:22:43 +0000827 *
Hanno Beckerc23483e2018-12-10 17:21:19 +0000828 * \param Q The destination MPI for the quotient.
Hanno Beckerd01ff492018-12-18 23:10:28 +0000829 * This may be \c NULL if the value of the
Glenn Strauss0750d082022-11-08 02:25:01 -0500830 * quotient is not needed. This must not alias A.
Hanno Beckerc23483e2018-12-10 17:21:19 +0000831 * \param R The destination MPI for the remainder value.
Hanno Beckerd01ff492018-12-18 23:10:28 +0000832 * This may be \c NULL if the value of the
Glenn Strauss0750d082022-11-08 02:25:01 -0500833 * remainder is not needed. This must not alias A.
Hanno Becker8ce11a32018-12-19 16:18:52 +0000834 * \param A The dividend. This must point to an initialized MPi.
Hanno Beckerc23483e2018-12-10 17:21:19 +0000835 * \param b The divisor.
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000836 *
Hanno Beckerc23483e2018-12-10 17:21:19 +0000837 * \return \c 0 if successful.
838 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed.
Hanno Beckere1185042018-12-13 14:31:46 +0000839 * \return #MBEDTLS_ERR_MPI_DIVISION_BY_ZERO if \p b equals zero.
Hanno Beckerc23483e2018-12-10 17:21:19 +0000840 * \return Another negative error code on different kinds of failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000841 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100842int mbedtls_mpi_div_int(mbedtls_mpi *Q, mbedtls_mpi *R, const mbedtls_mpi *A,
843 mbedtls_mpi_sint b);
Paul Bakker5121ce52009-01-03 21:22:43 +0000844
845/**
Hanno Beckerc23483e2018-12-10 17:21:19 +0000846 * \brief Perform a modular reduction. R = A mod B
Paul Bakker5121ce52009-01-03 21:22:43 +0000847 *
Hanno Beckerc23483e2018-12-10 17:21:19 +0000848 * \param R The destination MPI for the residue value.
Hanno Becker8282c2f2018-12-12 13:36:46 +0000849 * This must point to an initialized MPI.
Hanno Beckerc23483e2018-12-10 17:21:19 +0000850 * \param A The MPI to compute the residue of.
Hanno Becker8282c2f2018-12-12 13:36:46 +0000851 * This must point to an initialized MPI.
Hanno Beckerc23483e2018-12-10 17:21:19 +0000852 * \param B The base of the modular reduction.
Hanno Becker8282c2f2018-12-12 13:36:46 +0000853 * This must point to an initialized MPI.
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000854 *
Hanno Beckerc23483e2018-12-10 17:21:19 +0000855 * \return \c 0 if successful.
856 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
Hanno Beckere1185042018-12-13 14:31:46 +0000857 * \return #MBEDTLS_ERR_MPI_DIVISION_BY_ZERO if \p B equals zero.
858 * \return #MBEDTLS_ERR_MPI_NEGATIVE_VALUE if \p B is negative.
Hanno Beckerc23483e2018-12-10 17:21:19 +0000859 * \return Another negative error code on different kinds of failure.
860 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000861 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100862int mbedtls_mpi_mod_mpi(mbedtls_mpi *R, const mbedtls_mpi *A,
863 const mbedtls_mpi *B);
Paul Bakker5121ce52009-01-03 21:22:43 +0000864
865/**
Hanno Beckerc23483e2018-12-10 17:21:19 +0000866 * \brief Perform a modular reduction with respect to an integer.
867 * r = A mod b
Paul Bakker5121ce52009-01-03 21:22:43 +0000868 *
Hanno Beckerc23483e2018-12-10 17:21:19 +0000869 * \param r The address at which to store the residue.
Hanno Becker8282c2f2018-12-12 13:36:46 +0000870 * This must not be \c NULL.
Hanno Beckerc23483e2018-12-10 17:21:19 +0000871 * \param A The MPI to compute the residue of.
Hanno Becker8282c2f2018-12-12 13:36:46 +0000872 * This must point to an initialized MPi.
Hanno Beckerc23483e2018-12-10 17:21:19 +0000873 * \param b The integer base of the modular reduction.
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000874 *
Hanno Beckerc23483e2018-12-10 17:21:19 +0000875 * \return \c 0 if successful.
876 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
Hanno Beckere1185042018-12-13 14:31:46 +0000877 * \return #MBEDTLS_ERR_MPI_DIVISION_BY_ZERO if \p b equals zero.
878 * \return #MBEDTLS_ERR_MPI_NEGATIVE_VALUE if \p b is negative.
Hanno Beckerc23483e2018-12-10 17:21:19 +0000879 * \return Another negative error code on different kinds of failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000880 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100881int mbedtls_mpi_mod_int(mbedtls_mpi_uint *r, const mbedtls_mpi *A,
882 mbedtls_mpi_sint b);
Paul Bakker5121ce52009-01-03 21:22:43 +0000883
884/**
Manuel Pégourié-Gonnard73050022024-06-18 12:52:45 +0200885 * \brief Perform a modular exponentiation: X = A^E mod N
886 *
Hanno Becker8282c2f2018-12-12 13:36:46 +0000887 * \param X The destination MPI. This must point to an initialized MPI.
Glenn Strauss0750d082022-11-08 02:25:01 -0500888 * This must not alias E or N.
Hanno Beckerc23483e2018-12-10 17:21:19 +0000889 * \param A The base of the exponentiation.
Hanno Becker8282c2f2018-12-12 13:36:46 +0000890 * This must point to an initialized MPI.
891 * \param E The exponent MPI. This must point to an initialized MPI.
892 * \param N The base for the modular reduction. This must point to an
Hanno Beckerc23483e2018-12-10 17:21:19 +0000893 * initialized MPI.
Yuto Takano538a0cb2021-07-14 10:20:09 +0100894 * \param prec_RR A helper MPI depending solely on \p N which can be used to
Hanno Beckerc23483e2018-12-10 17:21:19 +0000895 * speed-up multiple modular exponentiations for the same value
Hanno Beckere1185042018-12-13 14:31:46 +0000896 * of \p N. This may be \c NULL. If it is not \c NULL, it must
Hanno Becker8ce11a32018-12-19 16:18:52 +0000897 * point to an initialized MPI. If it hasn't been used after
Hanno Beckere1185042018-12-13 14:31:46 +0000898 * the call to mbedtls_mpi_init(), this function will compute
Yuto Takano538a0cb2021-07-14 10:20:09 +0100899 * the helper value and store it in \p prec_RR for reuse on
Hanno Beckere1185042018-12-13 14:31:46 +0000900 * subsequent calls to this function. Otherwise, the function
Yuto Takano538a0cb2021-07-14 10:20:09 +0100901 * will assume that \p prec_RR holds the helper value set by a
Hanno Beckere1185042018-12-13 14:31:46 +0000902 * previous call to mbedtls_mpi_exp_mod(), and reuse it.
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000903 *
Hanno Beckerc23483e2018-12-10 17:21:19 +0000904 * \return \c 0 if successful.
905 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
906 * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if \c N is negative or
907 * even, or if \c E is negative.
908 * \return Another negative error code on different kinds of failures.
Paul Bakker5121ce52009-01-03 21:22:43 +0000909 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000910 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100911int mbedtls_mpi_exp_mod(mbedtls_mpi *X, const mbedtls_mpi *A,
912 const mbedtls_mpi *E, const mbedtls_mpi *N,
913 mbedtls_mpi *prec_RR);
Paul Bakker5121ce52009-01-03 21:22:43 +0000914
915/**
Hanno Beckerc23483e2018-12-10 17:21:19 +0000916 * \brief Fill an MPI with a number of random bytes.
Paul Bakker287781a2011-03-26 13:18:49 +0000917 *
Hanno Becker8282c2f2018-12-12 13:36:46 +0000918 * \param X The destination MPI. This must point to an initialized MPI.
Hanno Beckerc23483e2018-12-10 17:21:19 +0000919 * \param size The number of random bytes to generate.
Hanno Becker8282c2f2018-12-12 13:36:46 +0000920 * \param f_rng The RNG function to use. This must not be \c NULL.
Hanno Beckere1185042018-12-13 14:31:46 +0000921 * \param p_rng The RNG parameter to be passed to \p f_rng. This may be
922 * \c NULL if \p f_rng doesn't need a context argument.
Paul Bakker287781a2011-03-26 13:18:49 +0000923 *
Hanno Beckerc23483e2018-12-10 17:21:19 +0000924 * \return \c 0 if successful.
925 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
926 * \return Another negative error code on failure.
Hanno Becker15f2b3e2017-10-17 15:17:05 +0100927 *
Hanno Beckerc23483e2018-12-10 17:21:19 +0000928 * \note The bytes obtained from the RNG are interpreted
Hanno Becker15f2b3e2017-10-17 15:17:05 +0100929 * as a big-endian representation of an MPI; this can
930 * be relevant in applications like deterministic ECDSA.
Paul Bakker287781a2011-03-26 13:18:49 +0000931 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100932int mbedtls_mpi_fill_random(mbedtls_mpi *X, size_t size,
933 int (*f_rng)(void *, unsigned char *, size_t),
934 void *p_rng);
Paul Bakker287781a2011-03-26 13:18:49 +0000935
Gilles Peskine02ac93a2021-03-29 22:02:55 +0200936/** Generate a random number uniformly in a range.
937 *
938 * This function generates a random number between \p min inclusive and
939 * \p N exclusive.
940 *
941 * The procedure complies with RFC 6979 §3.3 (deterministic ECDSA)
942 * when the RNG is a suitably parametrized instance of HMAC_DRBG
943 * and \p min is \c 1.
944 *
945 * \note There are `N - min` possible outputs. The lower bound
946 * \p min can be reached, but the upper bound \p N cannot.
947 *
948 * \param X The destination MPI. This must point to an initialized MPI.
949 * \param min The minimum value to return.
950 * It must be nonnegative.
951 * \param N The upper bound of the range, exclusive.
952 * In other words, this is one plus the maximum value to return.
953 * \p N must be strictly larger than \p min.
954 * \param f_rng The RNG function to use. This must not be \c NULL.
955 * \param p_rng The RNG parameter to be passed to \p f_rng.
956 *
957 * \return \c 0 if successful.
958 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
Gilles Peskine1e918f42021-03-29 22:14:51 +0200959 * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if \p min or \p N is invalid
960 * or if they are incompatible.
Gilles Peskine7ed7c5a2021-04-13 21:28:38 +0200961 * \return #MBEDTLS_ERR_MPI_NOT_ACCEPTABLE if the implementation was
962 * unable to find a suitable value within a limited number
963 * of attempts. This has a negligible probability if \p N
964 * is significantly larger than \p min, which is the case
965 * for all usual cryptographic applications.
Gilles Peskine02ac93a2021-03-29 22:02:55 +0200966 * \return Another negative error code on failure.
967 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100968int mbedtls_mpi_random(mbedtls_mpi *X,
969 mbedtls_mpi_sint min,
970 const mbedtls_mpi *N,
971 int (*f_rng)(void *, unsigned char *, size_t),
972 void *p_rng);
Gilles Peskine02ac93a2021-03-29 22:02:55 +0200973
Paul Bakker287781a2011-03-26 13:18:49 +0000974/**
Hanno Beckerc23483e2018-12-10 17:21:19 +0000975 * \brief Compute the greatest common divisor: G = gcd(A, B)
Paul Bakker5121ce52009-01-03 21:22:43 +0000976 *
Hanno Becker8282c2f2018-12-12 13:36:46 +0000977 * \param G The destination MPI. This must point to an initialized MPI.
978 * \param A The first operand. This must point to an initialized MPI.
Hanno Becker01c3c102018-12-17 23:04:51 +0000979 * \param B The second operand. This must point to an initialized MPI.
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000980 *
Hanno Beckerc23483e2018-12-10 17:21:19 +0000981 * \return \c 0 if successful.
982 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
983 * \return Another negative error code on different kinds of failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000984 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100985int mbedtls_mpi_gcd(mbedtls_mpi *G, const mbedtls_mpi *A,
986 const mbedtls_mpi *B);
Paul Bakker5121ce52009-01-03 21:22:43 +0000987
988/**
Hanno Beckerc23483e2018-12-10 17:21:19 +0000989 * \brief Compute the modular inverse: X = A^-1 mod N
Paul Bakker5121ce52009-01-03 21:22:43 +0000990 *
Hanno Becker8282c2f2018-12-12 13:36:46 +0000991 * \param X The destination MPI. This must point to an initialized MPI.
992 * \param A The MPI to calculate the modular inverse of. This must point
Hanno Beckerc23483e2018-12-10 17:21:19 +0000993 * to an initialized MPI.
Hanno Becker8282c2f2018-12-12 13:36:46 +0000994 * \param N The base of the modular inversion. This must point to an
Hanno Beckerc23483e2018-12-10 17:21:19 +0000995 * initialized MPI.
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000996 *
Hanno Beckerc23483e2018-12-10 17:21:19 +0000997 * \return \c 0 if successful.
998 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
Hanno Beckere1185042018-12-13 14:31:46 +0000999 * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if \p N is less than
1000 * or equal to one.
Andrzej Kurek69ed8c42022-02-17 10:06:44 -05001001 * \return #MBEDTLS_ERR_MPI_NOT_ACCEPTABLE if \p A has no modular
1002 * inverse with respect to \p N.
Paul Bakker5121ce52009-01-03 21:22:43 +00001003 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001004int mbedtls_mpi_inv_mod(mbedtls_mpi *X, const mbedtls_mpi *A,
1005 const mbedtls_mpi *N);
Paul Bakker5121ce52009-01-03 21:22:43 +00001006
Paul Bakker5121ce52009-01-03 21:22:43 +00001007/**
Janos Follatha0b67c22018-09-18 14:48:23 +01001008 * \brief Miller-Rabin primality test.
1009 *
1010 * \warning If \p X is potentially generated by an adversary, for example
1011 * when validating cryptographic parameters that you didn't
1012 * generate yourself and that are supposed to be prime, then
1013 * \p rounds should be at least the half of the security
1014 * strength of the cryptographic algorithm. On the other hand,
Tom Cosgrove1e211442022-05-26 11:51:00 +01001015 * if \p X is chosen uniformly or non-adversarially (as is the
Janos Follatha0b67c22018-09-18 14:48:23 +01001016 * case when mbedtls_mpi_gen_prime calls this function), then
1017 * \p rounds can be much lower.
1018 *
Hanno Beckerc23483e2018-12-10 17:21:19 +00001019 * \param X The MPI to check for primality.
Hanno Becker8282c2f2018-12-12 13:36:46 +00001020 * This must point to an initialized MPI.
Hanno Beckerc23483e2018-12-10 17:21:19 +00001021 * \param rounds The number of bases to perform the Miller-Rabin primality
1022 * test for. The probability of returning 0 on a composite is
David Horstmannd855b462023-02-23 18:39:16 +00001023 * at most 2<sup>-2*\p rounds </sup>.
Hanno Becker8282c2f2018-12-12 13:36:46 +00001024 * \param f_rng The RNG function to use. This must not be \c NULL.
Hanno Beckerc23483e2018-12-10 17:21:19 +00001025 * \param p_rng The RNG parameter to be passed to \p f_rng.
Hanno Beckere1185042018-12-13 14:31:46 +00001026 * This may be \c NULL if \p f_rng doesn't use
1027 * a context parameter.
Janos Follatha0b67c22018-09-18 14:48:23 +01001028 *
Hanno Beckerc23483e2018-12-10 17:21:19 +00001029 * \return \c 0 if successful, i.e. \p X is probably prime.
1030 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
1031 * \return #MBEDTLS_ERR_MPI_NOT_ACCEPTABLE if \p X is not prime.
1032 * \return Another negative error code on other kinds of failure.
Janos Follatha0b67c22018-09-18 14:48:23 +01001033 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001034int mbedtls_mpi_is_prime_ext(const mbedtls_mpi *X, int rounds,
1035 int (*f_rng)(void *, unsigned char *, size_t),
1036 void *p_rng);
Janos Follatha0b67c22018-09-18 14:48:23 +01001037/**
Janos Follath7c025a92018-08-14 11:08:41 +01001038 * \brief Flags for mbedtls_mpi_gen_prime()
1039 *
1040 * Each of these flags is a constraint on the result X returned by
1041 * mbedtls_mpi_gen_prime().
1042 */
1043typedef enum {
Janos Follathf301d232018-08-14 13:34:01 +01001044 MBEDTLS_MPI_GEN_PRIME_FLAG_DH = 0x0001, /**< (X-1)/2 is prime too */
1045 MBEDTLS_MPI_GEN_PRIME_FLAG_LOW_ERR = 0x0002, /**< lower error rate from 2<sup>-80</sup> to 2<sup>-128</sup> */
Janos Follath7c025a92018-08-14 11:08:41 +01001046} mbedtls_mpi_gen_prime_flag_t;
1047
1048/**
Hanno Beckerc23483e2018-12-10 17:21:19 +00001049 * \brief Generate a prime number.
Paul Bakker5121ce52009-01-03 21:22:43 +00001050 *
Hanno Beckerc23483e2018-12-10 17:21:19 +00001051 * \param X The destination MPI to store the generated prime in.
Hanno Becker8282c2f2018-12-12 13:36:46 +00001052 * This must point to an initialized MPi.
Hanno Beckerc23483e2018-12-10 17:21:19 +00001053 * \param nbits The required size of the destination MPI in bits.
Hanno Beckerd01ff492018-12-18 23:10:28 +00001054 * This must be between \c 3 and #MBEDTLS_MPI_MAX_BITS.
Hanno Beckerc23483e2018-12-10 17:21:19 +00001055 * \param flags A mask of flags of type #mbedtls_mpi_gen_prime_flag_t.
Hanno Becker8282c2f2018-12-12 13:36:46 +00001056 * \param f_rng The RNG function to use. This must not be \c NULL.
Hanno Beckerc23483e2018-12-10 17:21:19 +00001057 * \param p_rng The RNG parameter to be passed to \p f_rng.
Hanno Beckere1185042018-12-13 14:31:46 +00001058 * This may be \c NULL if \p f_rng doesn't use
1059 * a context parameter.
Paul Bakker5121ce52009-01-03 21:22:43 +00001060 *
Hanno Beckere1185042018-12-13 14:31:46 +00001061 * \return \c 0 if successful, in which case \p X holds a
Hanno Beckerc23483e2018-12-10 17:21:19 +00001062 * probably prime number.
1063 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
1064 * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if `nbits` is not between
1065 * \c 3 and #MBEDTLS_MPI_MAX_BITS.
Paul Bakker5121ce52009-01-03 21:22:43 +00001066 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001067int mbedtls_mpi_gen_prime(mbedtls_mpi *X, size_t nbits, int flags,
1068 int (*f_rng)(void *, unsigned char *, size_t),
1069 void *p_rng);
Paul Bakker5121ce52009-01-03 21:22:43 +00001070
Ron Eldorfa8f6352017-06-20 15:48:46 +03001071#if defined(MBEDTLS_SELF_TEST)
1072
Paul Bakker5121ce52009-01-03 21:22:43 +00001073/**
1074 * \brief Checkup routine
1075 *
1076 * \return 0 if successful, or 1 if the test failed
1077 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001078int mbedtls_mpi_self_test(int verbose);
Paul Bakker5121ce52009-01-03 21:22:43 +00001079
Ron Eldorfa8f6352017-06-20 15:48:46 +03001080#endif /* MBEDTLS_SELF_TEST */
1081
Paul Bakker5121ce52009-01-03 21:22:43 +00001082#ifdef __cplusplus
1083}
1084#endif
1085
1086#endif /* bignum.h */