blob: 51ecca5ff727a22d2c36fd86a7d3ecaae4fd3afd [file] [log] [blame]
Gabor Mezeib9030702022-07-18 23:09:45 +02001/**
Janos Follath63184682022-08-11 17:42:59 +01002 * Core bignum functions
3 *
Janos Follathaf3f39c2022-08-22 09:06:32 +01004 * This interface should only be used by the legacy bignum module (bignum.h)
Janos Follath63184682022-08-11 17:42:59 +01005 * and the modular bignum modules (bignum_mod.c, bignum_mod_raw.c). All other
Janos Follathaf3f39c2022-08-22 09:06:32 +01006 * modules should use the high-level modular bignum interface (bignum_mod.h)
Janos Follath63184682022-08-11 17:42:59 +01007 * or the legacy bignum interface (bignum.h).
Gabor Mezeib9030702022-07-18 23:09:45 +02008 *
Gilles Peskine7aab2fb2022-09-27 13:19:13 +02009 * This module is about processing non-negative integers with a fixed upper
Gilles Peskine01af3dd2022-10-04 16:23:29 +020010 * bound that's of the form 2^n-1 where n is a multiple of #biL.
11 * These can be thought of integers written in base 2^#biL with a fixed
12 * number of digits. Digits in this base are called *limbs*.
13 * Many operations treat these numbers as the principal representation of
14 * a number modulo 2^n or a smaller bound.
Gilles Peskine7aab2fb2022-09-27 13:19:13 +020015 *
Gilles Peskine29264842022-09-27 13:19:50 +020016 * The functions in this module obey the following conventions unless
17 * explicitly indicated otherwise:
18 *
19 * - **Overflow**: some functions indicate overflow from the range
Gilles Peskine01af3dd2022-10-04 16:23:29 +020020 * [0, 2^n-1] by returning carry parameters, while others operate
Gilles Peskine29264842022-09-27 13:19:50 +020021 * modulo and so cannot overflow. This should be clear from the function
22 * documentation.
23 * - **Bignum parameters**: Bignums are passed as pointers to an array of
24 * limbs. A limb has the type #mbedtls_mpi_uint. Unless otherwise specified:
25 * - Bignum parameters called \p A, \p B, ... are inputs, and are
26 * not modified by the function.
27 * - For operations modulo some number, the modulus is called \p N
28 * and is input-only.
29 * - Bignum parameters called \p X, \p Y are outputs or input-output.
30 * The initial content of output-only parameters is ignored.
31 * - Some functions use different names that reflect traditional
32 * naming of operands of certain operations (e.g.
33 * divisor/dividend/quotient/remainder).
34 * - \p T is a temporary storage area. The initial content of such
35 * parameter is ignored and the final content is unspecified.
36 * - **Bignum sizes**: bignum sizes are always expressed in limbs.
37 * Most functions work on bignums of a given size and take a single
38 * \p limbs parameter that applies to all parameters that are limb arrays.
39 * All bignum sizes must be at least 1 and must be significantly less than
40 * #SIZE_MAX. The behavior if a size is 0 is undefined. The behavior if the
41 * total size of all parameters overflows #SIZE_MAX is undefined.
42 * - **Parameter ordering**: for bignum parameters, outputs come before inputs.
43 * Temporaries come last.
44 * - **Aliasing**: in general, output bignums may be aliased to one or more
45 * inputs. As an exception, parameters that are documented as a modulus value
Gilles Peskinedcd17172022-10-14 17:14:20 +020046 * may not be aliased to an output. Outputs may not be aliased to one another.
47 * Temporaries may not be aliased to any other parameter.
Gilles Peskine29264842022-09-27 13:19:50 +020048 * - **Overlap**: apart from aliasing of limb array pointers (where two
49 * arguments are equal pointers), overlap is not supported and may result
50 * in undefined behavior.
51 * - **Error handling**: This is a low-level module. Functions generally do not
52 * try to protect against invalid arguments such as nonsensical sizes or
53 * null pointers. Note that some functions that operate on bignums of
54 * different sizes have constraints about their size, and violating those
55 * constraints may lead to buffer overflows.
56 * - **Modular representatives**: functions that operate modulo \p N expect
57 * all modular inputs to be in the range [0, \p N - 1] and guarantee outputs
58 * in the range [0, \p N - 1]. If an input is out of range, outputs are
59 * fully unspecified, though bignum values out of range should not cause
60 * buffer overflows (beware that this is not extensively tested).
Gilles Peskine7f887bd2022-09-27 13:12:30 +020061 */
62
63/*
Gabor Mezeib9030702022-07-18 23:09:45 +020064 * Copyright The Mbed TLS Contributors
Dave Rodgman16799db2023-11-02 19:47:20 +000065 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Gabor Mezeib9030702022-07-18 23:09:45 +020066 */
67
68#ifndef MBEDTLS_BIGNUM_CORE_H
69#define MBEDTLS_BIGNUM_CORE_H
70
71#include "common.h"
72
73#if defined(MBEDTLS_BIGNUM_C)
74#include "mbedtls/bignum.h"
75#endif
76
Dave Rodgmancd2e38b2023-05-17 13:31:55 +010077#include "constant_time_internal.h"
78
Gilles Peskine449bd832023-01-11 14:50:10 +010079#define ciL (sizeof(mbedtls_mpi_uint)) /** chars in limb */
80#define biL (ciL << 3) /** bits in limb */
81#define biH (ciL << 2) /** half limb size */
Tom Cosgrove5eefc3d2022-08-31 17:16:50 +010082
83/*
84 * Convert between bits/chars and number of limbs
85 * Divide first in order to avoid potential overflows
86 */
Gilles Peskine449bd832023-01-11 14:50:10 +010087#define BITS_TO_LIMBS(i) ((i) / biL + ((i) % biL != 0))
88#define CHARS_TO_LIMBS(i) ((i) / ciL + ((i) % ciL != 0))
Tom Cosgrove5eefc3d2022-08-31 17:16:50 +010089/* Get a specific byte, without range checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +010090#define GET_BYTE(X, i) \
91 (((X)[(i) / ciL] >> (((i) % ciL) * 8)) & 0xff)
Tom Cosgrove5eefc3d2022-08-31 17:16:50 +010092
Gabor Mezei37b06362022-08-02 17:22:18 +020093/** Count leading zero bits in a given integer.
94 *
Dave Rodgman0f16d562023-04-24 12:53:29 +010095 * \warning The result is undefined if \p a == 0
96 *
Janos Follathb7a88ec2022-08-19 12:24:40 +010097 * \param a Integer to count leading zero bits.
Gabor Mezei37b06362022-08-02 17:22:18 +020098 *
Dave Rodgman0f16d562023-04-24 12:53:29 +010099 * \return The number of leading zero bits in \p a, if \p a != 0.
100 * If \p a == 0, the result is undefined.
Gabor Mezei37b06362022-08-02 17:22:18 +0200101 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100102size_t mbedtls_mpi_core_clz(mbedtls_mpi_uint a);
Janos Follath4670f882022-07-21 18:25:42 +0100103
Janos Follatha95f2042022-08-19 12:09:17 +0100104/** Return the minimum number of bits required to represent the value held
Janos Follath63184682022-08-11 17:42:59 +0100105 * in the MPI.
106 *
Janos Follathb7a88ec2022-08-19 12:24:40 +0100107 * \note This function returns 0 if all the limbs of \p A are 0.
Gabor Mezei37b06362022-08-02 17:22:18 +0200108 *
Janos Follathb7a88ec2022-08-19 12:24:40 +0100109 * \param[in] A The address of the MPI.
Janos Follathc4596412022-08-22 10:01:27 +0100110 * \param A_limbs The number of limbs of \p A.
Gabor Mezei37b06362022-08-02 17:22:18 +0200111 *
Janos Follathb7a88ec2022-08-19 12:24:40 +0100112 * \return The number of bits in \p A.
Gabor Mezei37b06362022-08-02 17:22:18 +0200113 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100114size_t mbedtls_mpi_core_bitlen(const mbedtls_mpi_uint *A, size_t A_limbs);
Janos Follath4670f882022-07-21 18:25:42 +0100115
Gabor Mezei37b06362022-08-02 17:22:18 +0200116/** Convert a big-endian byte array aligned to the size of mbedtls_mpi_uint
117 * into the storage form used by mbedtls_mpi.
118 *
Janos Follathb7a88ec2022-08-19 12:24:40 +0100119 * \param[in,out] A The address of the MPI.
Janos Follathc4596412022-08-22 10:01:27 +0100120 * \param A_limbs The number of limbs of \p A.
Gabor Mezei37b06362022-08-02 17:22:18 +0200121 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100122void mbedtls_mpi_core_bigendian_to_host(mbedtls_mpi_uint *A,
123 size_t A_limbs);
Janos Follath4670f882022-07-21 18:25:42 +0100124
Gilles Peskine6f949ea2022-09-20 18:38:35 +0200125/** \brief Compare a machine integer with an MPI.
126 *
127 * This function operates in constant time with respect
128 * to the values of \p min and \p A.
129 *
130 * \param min A machine integer.
131 * \param[in] A An MPI.
132 * \param A_limbs The number of limbs of \p A.
133 * This must be at least 1.
134 *
Dave Rodgmanfd7fab42023-05-17 14:00:39 +0100135 * \return MBEDTLS_CT_TRUE if \p min is less than or equal to \p A, otherwise MBEDTLS_CT_FALSE.
Gilles Peskine6f949ea2022-09-20 18:38:35 +0200136 */
Dave Rodgmanfd7fab42023-05-17 14:00:39 +0100137mbedtls_ct_condition_t mbedtls_mpi_core_uint_le_mpi(mbedtls_mpi_uint min,
138 const mbedtls_mpi_uint *A,
139 size_t A_limbs);
Gilles Peskine6f949ea2022-09-20 18:38:35 +0200140
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200141/**
Dave Rodgman7d4f0192023-05-09 14:01:05 +0100142 * \brief Check if one unsigned MPI is less than another in constant
143 * time.
144 *
145 * \param A The left-hand MPI. This must point to an array of limbs
146 * with the same allocated length as \p B.
147 * \param B The right-hand MPI. This must point to an array of limbs
148 * with the same allocated length as \p A.
149 * \param limbs The number of limbs in \p A and \p B.
150 * This must not be 0.
151 *
Dave Rodgman8ac9a1d2023-05-17 15:16:22 +0100152 * \return MBEDTLS_CT_TRUE if \p A is less than \p B.
153 * MBEDTLS_CT_FALSE if \p A is greater than or equal to \p B.
Dave Rodgman7d4f0192023-05-09 14:01:05 +0100154 */
Dave Rodgman8ac9a1d2023-05-17 15:16:22 +0100155mbedtls_ct_condition_t mbedtls_mpi_core_lt_ct(const mbedtls_mpi_uint *A,
156 const mbedtls_mpi_uint *B,
157 size_t limbs);
158
Dave Rodgman7d4f0192023-05-09 14:01:05 +0100159/**
Gabor Mezei4086de62022-10-14 16:29:42 +0200160 * \brief Perform a safe conditional copy of an MPI which doesn't reveal
161 * whether assignment was done or not.
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200162 *
Gabor Mezei4086de62022-10-14 16:29:42 +0200163 * \param[out] X The address of the destination MPI.
164 * This must be initialized. Must have enough limbs to
165 * store the full value of \p A.
166 * \param[in] A The address of the source MPI. This must be initialized.
Gabor Mezei1c628d52022-09-27 12:13:51 +0200167 * \param limbs The number of limbs of \p A.
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200168 * \param assign The condition deciding whether to perform the
Dave Rodgmanfb1b8512023-07-31 12:27:05 +0100169 * assignment or not. Callers will need to use
170 * the constant time interface (e.g. `mbedtls_ct_bool()`)
171 * to construct this argument.
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200172 *
173 * \note This function avoids leaking any information about whether
174 * the assignment was done or not.
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200175 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100176void mbedtls_mpi_core_cond_assign(mbedtls_mpi_uint *X,
177 const mbedtls_mpi_uint *A,
178 size_t limbs,
Dave Rodgmancd2e38b2023-05-17 13:31:55 +0100179 mbedtls_ct_condition_t assign);
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200180
181/**
Gabor Mezei4086de62022-10-14 16:29:42 +0200182 * \brief Perform a safe conditional swap of two MPIs which doesn't reveal
183 * whether the swap was done or not.
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200184 *
Gabor Mezei86dfe382022-09-30 14:03:04 +0200185 * \param[in,out] X The address of the first MPI.
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200186 * This must be initialized.
Gabor Mezei86dfe382022-09-30 14:03:04 +0200187 * \param[in,out] Y The address of the second MPI.
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200188 * This must be initialized.
Gabor Mezeie5b85852022-09-30 13:54:02 +0200189 * \param limbs The number of limbs of \p X and \p Y.
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200190 * \param swap The condition deciding whether to perform
Dave Rodgmancd2e38b2023-05-17 13:31:55 +0100191 * the swap or not.
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200192 *
193 * \note This function avoids leaking any information about whether
194 * the swap was done or not.
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200195 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100196void mbedtls_mpi_core_cond_swap(mbedtls_mpi_uint *X,
197 mbedtls_mpi_uint *Y,
198 size_t limbs,
Dave Rodgmancd2e38b2023-05-17 13:31:55 +0100199 mbedtls_ct_condition_t swap);
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200200
Janos Follathaf3f39c2022-08-22 09:06:32 +0100201/** Import X from unsigned binary data, little-endian.
Gabor Mezei37b06362022-08-02 17:22:18 +0200202 *
Janos Follath63184682022-08-11 17:42:59 +0100203 * The MPI needs to have enough limbs to store the full value (including any
204 * most significant zero bytes in the input).
Gabor Mezei37b06362022-08-02 17:22:18 +0200205 *
Janos Follathb7a88ec2022-08-19 12:24:40 +0100206 * \param[out] X The address of the MPI.
207 * \param X_limbs The number of limbs of \p X.
208 * \param[in] input The input buffer to import from.
209 * \param input_length The length bytes of \p input.
Gabor Mezei37b06362022-08-02 17:22:18 +0200210 *
211 * \return \c 0 if successful.
212 * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p X isn't
Janos Follathb7a88ec2022-08-19 12:24:40 +0100213 * large enough to hold the value in \p input.
Gabor Mezei37b06362022-08-02 17:22:18 +0200214 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100215int mbedtls_mpi_core_read_le(mbedtls_mpi_uint *X,
216 size_t X_limbs,
217 const unsigned char *input,
218 size_t input_length);
Gabor Mezeib9030702022-07-18 23:09:45 +0200219
Janos Follathaf3f39c2022-08-22 09:06:32 +0100220/** Import X from unsigned binary data, big-endian.
Gabor Mezei37b06362022-08-02 17:22:18 +0200221 *
Janos Follath63184682022-08-11 17:42:59 +0100222 * The MPI needs to have enough limbs to store the full value (including any
223 * most significant zero bytes in the input).
Gabor Mezei37b06362022-08-02 17:22:18 +0200224 *
Janos Follathb7a88ec2022-08-19 12:24:40 +0100225 * \param[out] X The address of the MPI.
Tom Cosgrove8a1f7842023-02-01 08:43:54 +0000226 * May only be #NULL if \p X_limbs is 0 and \p input_length
Janos Follathb7a88ec2022-08-19 12:24:40 +0100227 * is 0.
228 * \param X_limbs The number of limbs of \p X.
229 * \param[in] input The input buffer to import from.
230 * May only be #NULL if \p input_length is 0.
231 * \param input_length The length in bytes of \p input.
Gabor Mezei37b06362022-08-02 17:22:18 +0200232 *
233 * \return \c 0 if successful.
234 * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p X isn't
Janos Follathb7a88ec2022-08-19 12:24:40 +0100235 * large enough to hold the value in \p input.
Gabor Mezei37b06362022-08-02 17:22:18 +0200236 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100237int mbedtls_mpi_core_read_be(mbedtls_mpi_uint *X,
238 size_t X_limbs,
239 const unsigned char *input,
240 size_t input_length);
Gabor Mezeib9030702022-07-18 23:09:45 +0200241
Janos Follathaf3f39c2022-08-22 09:06:32 +0100242/** Export A into unsigned binary data, little-endian.
Gabor Mezei37b06362022-08-02 17:22:18 +0200243 *
Janos Follathb7a88ec2022-08-19 12:24:40 +0100244 * \note If \p output is shorter than \p A the export is still successful if the
245 * value held in \p A fits in the buffer (that is, if enough of the most
246 * significant bytes of \p A are 0).
Janos Follath63184682022-08-11 17:42:59 +0100247 *
Janos Follathb7a88ec2022-08-19 12:24:40 +0100248 * \param[in] A The address of the MPI.
249 * \param A_limbs The number of limbs of \p A.
250 * \param[out] output The output buffer to export to.
251 * \param output_length The length in bytes of \p output.
Gabor Mezei37b06362022-08-02 17:22:18 +0200252 *
253 * \return \c 0 if successful.
Janos Follathb7a88ec2022-08-19 12:24:40 +0100254 * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p output isn't
255 * large enough to hold the value of \p A.
Gabor Mezei37b06362022-08-02 17:22:18 +0200256 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100257int mbedtls_mpi_core_write_le(const mbedtls_mpi_uint *A,
258 size_t A_limbs,
259 unsigned char *output,
260 size_t output_length);
Gabor Mezeib9030702022-07-18 23:09:45 +0200261
Janos Follathaf3f39c2022-08-22 09:06:32 +0100262/** Export A into unsigned binary data, big-endian.
Gabor Mezei37b06362022-08-02 17:22:18 +0200263 *
Janos Follathb7a88ec2022-08-19 12:24:40 +0100264 * \note If \p output is shorter than \p A the export is still successful if the
265 * value held in \p A fits in the buffer (that is, if enough of the most
266 * significant bytes of \p A are 0).
Janos Follath63184682022-08-11 17:42:59 +0100267 *
Janos Follathb7a88ec2022-08-19 12:24:40 +0100268 * \param[in] A The address of the MPI.
269 * \param A_limbs The number of limbs of \p A.
270 * \param[out] output The output buffer to export to.
271 * \param output_length The length in bytes of \p output.
Gabor Mezei37b06362022-08-02 17:22:18 +0200272 *
273 * \return \c 0 if successful.
Janos Follathb7a88ec2022-08-19 12:24:40 +0100274 * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p output isn't
275 * large enough to hold the value of \p A.
Gabor Mezei37b06362022-08-02 17:22:18 +0200276 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100277int mbedtls_mpi_core_write_be(const mbedtls_mpi_uint *A,
278 size_t A_limbs,
279 unsigned char *output,
280 size_t output_length);
Gabor Mezeib9030702022-07-18 23:09:45 +0200281
Minos Galanakisec09e252023-04-20 14:22:16 +0100282/** \brief Shift an MPI in-place right by a number of bits.
Gilles Peskine66414202022-09-21 15:36:16 +0200283 *
284 * Shifting by more bits than there are bit positions
285 * in \p X is valid and results in setting \p X to 0.
286 *
287 * This function's execution time depends on the value
288 * of \p count (and of course \p limbs).
289 *
290 * \param[in,out] X The number to shift.
291 * \param limbs The number of limbs of \p X. This must be at least 1.
292 * \param count The number of bits to shift by.
293 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100294void mbedtls_mpi_core_shift_r(mbedtls_mpi_uint *X, size_t limbs,
295 size_t count);
Gilles Peskine66414202022-09-21 15:36:16 +0200296
Tom Cosgrove90c426b2022-08-23 16:15:19 +0100297/**
Minos Galanakisec09e252023-04-20 14:22:16 +0100298 * \brief Shift an MPI in-place left by a number of bits.
Minos Galanakisad808dd2023-04-20 12:18:41 +0100299 *
Minos Galanakisec09e252023-04-20 14:22:16 +0100300 * Shifting by more bits than there are bit positions
Minos Galanakisb8944032023-04-28 14:09:44 +0100301 * in \p X will produce an unspecified result.
Minos Galanakisad808dd2023-04-20 12:18:41 +0100302 *
Minos Galanakisec09e252023-04-20 14:22:16 +0100303 * This function's execution time depends on the value
304 * of \p count (and of course \p limbs).
305 * \param[in,out] X The number to shift.
306 * \param limbs The number of limbs of \p X. This must be at least 1.
307 * \param count The number of bits to shift by.
Minos Galanakisad808dd2023-04-20 12:18:41 +0100308 */
Minos Galanakisec09e252023-04-20 14:22:16 +0100309void mbedtls_mpi_core_shift_l(mbedtls_mpi_uint *X, size_t limbs,
310 size_t count);
Minos Galanakisad808dd2023-04-20 12:18:41 +0100311
312/**
Tom Cosgroveaf7d44b2022-08-24 14:05:26 +0100313 * \brief Add two fixed-size large unsigned integers, returning the carry.
Hanno Beckerc9887132022-08-24 12:54:36 +0100314 *
Tom Cosgroveaf7d44b2022-08-24 14:05:26 +0100315 * Calculates `A + B` where `A` and `B` have the same size.
316 *
Tom Cosgrove82f13102022-10-25 12:46:03 +0100317 * This function operates modulo `2^(biL*limbs)` and returns the carry
Hanno Beckerc9887132022-08-24 12:54:36 +0100318 * (1 if there was a wraparound, and 0 otherwise).
319 *
Tom Cosgroveaf7d44b2022-08-24 14:05:26 +0100320 * \p X may be aliased to \p A or \p B.
Hanno Beckerc9887132022-08-24 12:54:36 +0100321 *
Tom Cosgroveaf7d44b2022-08-24 14:05:26 +0100322 * \param[out] X The result of the addition.
323 * \param[in] A Little-endian presentation of the left operand.
324 * \param[in] B Little-endian presentation of the right operand.
325 * \param limbs Number of limbs of \p X, \p A and \p B.
Hanno Beckerc9887132022-08-24 12:54:36 +0100326 *
Tom Cosgroveaf7d44b2022-08-24 14:05:26 +0100327 * \return 1 if `A + B >= 2^(biL*limbs)`, 0 otherwise.
Hanno Beckerc9887132022-08-24 12:54:36 +0100328 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100329mbedtls_mpi_uint mbedtls_mpi_core_add(mbedtls_mpi_uint *X,
330 const mbedtls_mpi_uint *A,
331 const mbedtls_mpi_uint *B,
332 size_t limbs);
Hanno Beckerc9887132022-08-24 12:54:36 +0100333
Tom Cosgrove90c426b2022-08-23 16:15:19 +0100334/**
Tom Cosgrove5c0e8102022-09-15 15:46:10 +0100335 * \brief Conditional addition of two fixed-size large unsigned integers,
Tom Cosgroved932de82022-08-25 16:43:43 +0100336 * returning the carry.
Hanno Becker71f4b0d2022-08-23 12:09:35 +0100337 *
338 * Functionally equivalent to
339 *
340 * ```
341 * if( cond )
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100342 * X += A;
Hanno Becker71f4b0d2022-08-23 12:09:35 +0100343 * return carry;
344 * ```
345 *
Tom Cosgrove47828232022-09-20 13:51:50 +0100346 * This function operates modulo `2^(biL*limbs)`.
347 *
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100348 * \param[in,out] X The pointer to the (little-endian) array
Tom Cosgrove72594632022-08-24 11:51:58 +0100349 * representing the bignum to accumulate onto.
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100350 * \param[in] A The pointer to the (little-endian) array
Tom Cosgrove72594632022-08-24 11:51:58 +0100351 * representing the bignum to conditionally add
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100352 * to \p X. This may be aliased to \p X but may not
Tom Cosgroveed43c6c2022-08-31 11:35:00 +0100353 * overlap otherwise.
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100354 * \param limbs Number of limbs of \p X and \p A.
Tom Cosgrove72594632022-08-24 11:51:58 +0100355 * \param cond Condition bit dictating whether addition should
356 * happen or not. This must be \c 0 or \c 1.
Hanno Becker71f4b0d2022-08-23 12:09:35 +0100357 *
Tom Cosgroveecbb1242022-08-25 10:13:44 +0100358 * \warning If \p cond is neither 0 nor 1, the result of this function
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100359 * is unspecified, and the resulting value in \p X might be
360 * neither its original value nor \p X + \p A.
Tom Cosgrove72594632022-08-24 11:51:58 +0100361 *
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100362 * \return 1 if `X + cond * A >= 2^(biL*limbs)`, 0 otherwise.
Hanno Becker71f4b0d2022-08-23 12:09:35 +0100363 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100364mbedtls_mpi_uint mbedtls_mpi_core_add_if(mbedtls_mpi_uint *X,
365 const mbedtls_mpi_uint *A,
366 size_t limbs,
367 unsigned cond);
Hanno Becker71f4b0d2022-08-23 12:09:35 +0100368
Tom Cosgroveb4964862022-08-30 11:57:22 +0100369/**
Tom Cosgrove5c0e8102022-09-15 15:46:10 +0100370 * \brief Subtract two fixed-size large unsigned integers, returning the borrow.
Tom Cosgroveb4964862022-08-30 11:57:22 +0100371 *
Tom Cosgrove5dd97e62022-08-30 14:31:49 +0100372 * Calculate `A - B` where \p A and \p B have the same size.
Tom Cosgrove630110a2022-08-31 17:09:29 +0100373 * This function operates modulo `2^(biL*limbs)` and returns the carry
Tom Cosgroveb4964862022-08-30 11:57:22 +0100374 * (1 if there was a wraparound, i.e. if `A < B`, and 0 otherwise).
375 *
Tom Cosgrove5dd97e62022-08-30 14:31:49 +0100376 * \p X may be aliased to \p A or \p B, or even both, but may not overlap
377 * either otherwise.
Tom Cosgroveb4964862022-08-30 11:57:22 +0100378 *
Waleed Elmelegya9fe03e2024-05-21 16:17:06 +0000379 * This function operates in constant time with respect to the values
Waleed Elmelegy473dea22024-05-28 11:15:21 +0000380 * of \p A and \p B.
Waleed Elmelegya9fe03e2024-05-21 16:17:06 +0000381 *
Tom Cosgroveb4964862022-08-30 11:57:22 +0100382 * \param[out] X The result of the subtraction.
383 * \param[in] A Little-endian presentation of left operand.
384 * \param[in] B Little-endian presentation of right operand.
385 * \param limbs Number of limbs of \p X, \p A and \p B.
386 *
387 * \return 1 if `A < B`.
388 * 0 if `A >= B`.
389 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100390mbedtls_mpi_uint mbedtls_mpi_core_sub(mbedtls_mpi_uint *X,
391 const mbedtls_mpi_uint *A,
392 const mbedtls_mpi_uint *B,
393 size_t limbs);
Tom Cosgroveb4964862022-08-30 11:57:22 +0100394
395/**
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100396 * \brief Perform a fixed-size multiply accumulate operation: X += b * A
Tom Cosgroveb4964862022-08-30 11:57:22 +0100397 *
Tom Cosgroveb0b77e12022-09-20 13:33:40 +0100398 * \p X may be aliased to \p A (when \p X_limbs == \p A_limbs), but may not
399 * otherwise overlap.
400 *
Tom Cosgrove47828232022-09-20 13:51:50 +0100401 * This function operates modulo `2^(biL*X_limbs)`.
402 *
Waleed Elmelegy11a81cd2024-05-23 08:01:58 +0000403 * This function operates in constant time with respect to the values
404 * of \p X and \p A and \p b.
405 *
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100406 * \param[in,out] X The pointer to the (little-endian) array
Tom Cosgroveb4964862022-08-30 11:57:22 +0100407 * representing the bignum to accumulate onto.
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100408 * \param X_limbs The number of limbs of \p X. This must be
409 * at least \p A_limbs.
410 * \param[in] A The pointer to the (little-endian) array
Tom Cosgroveb4964862022-08-30 11:57:22 +0100411 * representing the bignum to multiply with.
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100412 * This may be aliased to \p X but may not overlap
Tom Cosgroveed43c6c2022-08-31 11:35:00 +0100413 * otherwise.
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100414 * \param A_limbs The number of limbs of \p A.
415 * \param b X scalar to multiply with.
Tom Cosgroveb4964862022-08-30 11:57:22 +0100416 *
417 * \return The carry at the end of the operation.
418 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100419mbedtls_mpi_uint mbedtls_mpi_core_mla(mbedtls_mpi_uint *X, size_t X_limbs,
420 const mbedtls_mpi_uint *A, size_t A_limbs,
421 mbedtls_mpi_uint b);
Tom Cosgroveb4964862022-08-30 11:57:22 +0100422
Hanno Becker4ae890b2022-08-24 16:17:52 +0100423/**
424 * \brief Perform a known-size multiplication
425 *
Gabor Mezeid6260512023-04-03 17:32:55 +0200426 * \p X may not be aliased to any of the inputs for this function.
Gabor Mezei6f182c32023-03-31 16:09:28 +0200427 * \p A may be aliased to \p B.
428 *
Tom Cosgrove6af26f32022-08-24 16:40:55 +0100429 * \param[out] X The pointer to the (little-endian) array to receive
430 * the product of \p A_limbs and \p B_limbs.
431 * This must be of length \p A_limbs + \p B_limbs.
432 * \param[in] A The pointer to the (little-endian) array
433 * representing the first factor.
434 * \param A_limbs The number of limbs in \p A.
435 * \param[in] B The pointer to the (little-endian) array
436 * representing the second factor.
437 * \param B_limbs The number of limbs in \p B.
Hanno Becker4ae890b2022-08-24 16:17:52 +0100438 */
Tom Cosgrove6af26f32022-08-24 16:40:55 +0100439void mbedtls_mpi_core_mul(mbedtls_mpi_uint *X,
440 const mbedtls_mpi_uint *A, size_t A_limbs,
441 const mbedtls_mpi_uint *B, size_t B_limbs);
Hanno Becker4ae890b2022-08-24 16:17:52 +0100442
Tom Cosgroveb4964862022-08-30 11:57:22 +0100443/**
444 * \brief Calculate initialisation value for fast Montgomery modular
445 * multiplication
446 *
447 * \param[in] N Little-endian presentation of the modulus. This must have
448 * at least one limb.
449 *
450 * \return The initialisation value for fast Montgomery modular multiplication
451 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100452mbedtls_mpi_uint mbedtls_mpi_core_montmul_init(const mbedtls_mpi_uint *N);
Tom Cosgroveb4964862022-08-30 11:57:22 +0100453
454/**
Tom Cosgrove4386ead2022-09-29 14:40:21 +0100455 * \brief Montgomery multiplication: X = A * B * R^-1 mod N (HAC 14.36)
456 *
457 * \p A and \p B must be in canonical form. That is, < \p N.
Tom Cosgroveb4964862022-08-30 11:57:22 +0100458 *
Tom Cosgroveea45c1d2022-09-20 13:17:51 +0100459 * \p X may be aliased to \p A or \p N, or even \p B (if \p AN_limbs ==
460 * \p B_limbs) but may not overlap any parameters otherwise.
461 *
Tom Cosgrove4386ead2022-09-29 14:40:21 +0100462 * \p A and \p B may alias each other, if \p AN_limbs == \p B_limbs. They may
463 * not alias \p N (since they must be in canonical form, they cannot == \p N).
Tom Cosgroveea45c1d2022-09-20 13:17:51 +0100464 *
Waleed Elmelegy122ae062024-05-29 17:11:28 +0000465 * This function operates in constant time with respect
466 * to the values of \p A, \p B and \p N.
467 *
468 *
Tom Cosgroveb4964862022-08-30 11:57:22 +0100469 * \param[out] X The destination MPI, as a little-endian array of
470 * length \p AN_limbs.
471 * On successful completion, X contains the result of
Tom Cosgrove630110a2022-08-31 17:09:29 +0100472 * the multiplication `A * B * R^-1` mod N where
473 * `R = 2^(biL*AN_limbs)`.
Tom Cosgroveb4964862022-08-30 11:57:22 +0100474 * \param[in] A Little-endian presentation of first operand.
Tom Cosgrove5dd97e62022-08-30 14:31:49 +0100475 * Must have the same number of limbs as \p N.
Tom Cosgroveb4964862022-08-30 11:57:22 +0100476 * \param[in] B Little-endian presentation of second operand.
477 * \param[in] B_limbs The number of limbs in \p B.
Tom Cosgrove5dd97e62022-08-30 14:31:49 +0100478 * Must be <= \p AN_limbs.
Tom Cosgroveb4964862022-08-30 11:57:22 +0100479 * \param[in] N Little-endian presentation of the modulus.
Tom Cosgrove5dd97e62022-08-30 14:31:49 +0100480 * This must be odd, and have exactly the same number
481 * of limbs as \p A.
Tom Cosgrove6da3a3b2022-09-29 17:20:18 +0100482 * It may alias \p X, but must not alias or otherwise
483 * overlap any of the other parameters.
Tom Cosgrove5dd97e62022-08-30 14:31:49 +0100484 * \param[in] AN_limbs The number of limbs in \p X, \p A and \p N.
Tom Cosgrove630110a2022-08-31 17:09:29 +0100485 * \param mm The Montgomery constant for \p N: -N^-1 mod 2^biL.
Tom Cosgroveb7438d12022-09-15 15:05:59 +0100486 * This can be calculated by `mbedtls_mpi_core_montmul_init()`.
Tom Cosgroveb4964862022-08-30 11:57:22 +0100487 * \param[in,out] T Temporary storage of size at least 2*AN_limbs+1 limbs.
488 * Its initial content is unused and
489 * its final content is indeterminate.
Tom Cosgrove4386ead2022-09-29 14:40:21 +0100490 * It must not alias or otherwise overlap any of the
491 * other parameters.
Tom Cosgroveb4964862022-08-30 11:57:22 +0100492 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100493void mbedtls_mpi_core_montmul(mbedtls_mpi_uint *X,
494 const mbedtls_mpi_uint *A,
495 const mbedtls_mpi_uint *B, size_t B_limbs,
496 const mbedtls_mpi_uint *N, size_t AN_limbs,
497 mbedtls_mpi_uint mm, mbedtls_mpi_uint *T);
Tom Cosgroveb4964862022-08-30 11:57:22 +0100498
Hanno Beckerec440f22022-08-11 17:29:32 +0100499/**
Minos Galanakisb85506e2022-10-20 09:51:53 +0100500 * \brief Calculate the square of the Montgomery constant. (Needed
501 * for conversion and operations in Montgomery form.)
Hanno Beckerec440f22022-08-11 17:29:32 +0100502 *
503 * \param[out] X A pointer to the result of the calculation of
Minos Galanakisb85506e2022-10-20 09:51:53 +0100504 * the square of the Montgomery constant:
505 * 2^{2*n*biL} mod N.
Hanno Beckerec440f22022-08-11 17:29:32 +0100506 * \param[in] N Little-endian presentation of the modulus, which must be odd.
507 *
508 * \return 0 if successful.
509 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if there is not enough space
510 * to store the value of Montgomery constant squared.
511 * \return #MBEDTLS_ERR_MPI_DIVISION_BY_ZERO if \p N modulus is zero.
512 * \return #MBEDTLS_ERR_MPI_NEGATIVE_VALUE if \p N modulus is negative.
Hanno Beckerec440f22022-08-11 17:29:32 +0100513 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100514int mbedtls_mpi_core_get_mont_r2_unsafe(mbedtls_mpi *X,
515 const mbedtls_mpi *N);
Hanno Beckerec440f22022-08-11 17:29:32 +0100516
Janos Follath59cbd1b2022-10-28 18:13:43 +0100517#if defined(MBEDTLS_TEST_HOOKS)
Janos Follathe50f2f12022-10-26 15:14:33 +0100518/**
Janos Follath8904a2d2022-10-31 15:32:28 +0000519 * Copy an MPI from a table without leaking the index.
Janos Follathe50f2f12022-10-26 15:14:33 +0100520 *
521 * \param dest The destination buffer. This must point to a writable
522 * buffer of at least \p limbs limbs.
523 * \param table The address of the table. This must point to a readable
Janos Follath8904a2d2022-10-31 15:32:28 +0000524 * array of \p count elements of \p limbs limbs each.
525 * \param limbs The number of limbs in each table entry.
526 * \param count The number of entries in \p table.
527 * \param index The (secret) table index to look up. This must be in the
528 * range `0 .. count-1`.
Janos Follathe50f2f12022-10-26 15:14:33 +0100529 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100530void mbedtls_mpi_core_ct_uint_table_lookup(mbedtls_mpi_uint *dest,
531 const mbedtls_mpi_uint *table,
532 size_t limbs,
533 size_t count,
534 size_t index);
Janos Follath59cbd1b2022-10-28 18:13:43 +0100535#endif /* MBEDTLS_TEST_HOOKS */
Janos Follathe50f2f12022-10-26 15:14:33 +0100536
Gilles Peskine909e03c2022-10-18 18:14:33 +0200537/**
538 * \brief Fill an integer with a number of random bytes.
539 *
540 * \param X The destination MPI.
541 * \param X_limbs The number of limbs of \p X.
542 * \param bytes The number of random bytes to generate.
543 * \param f_rng The RNG function to use. This must not be \c NULL.
544 * \param p_rng The RNG parameter to be passed to \p f_rng. This may be
545 * \c NULL if \p f_rng doesn't need a context argument.
546 *
547 * \return \c 0 if successful.
548 * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if \p X does not have
549 * enough room for \p bytes bytes.
550 * \return A negative error code on RNG failure.
551 *
552 * \note The bytes obtained from the RNG are interpreted
553 * as a big-endian representation of an MPI; this can
554 * be relevant in applications like deterministic ECDSA.
555 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100556int mbedtls_mpi_core_fill_random(mbedtls_mpi_uint *X, size_t X_limbs,
557 size_t bytes,
558 int (*f_rng)(void *, unsigned char *, size_t),
559 void *p_rng);
Gilles Peskine909e03c2022-10-18 18:14:33 +0200560
Gilles Peskine4a8c5cd2022-10-18 18:15:01 +0200561/** Generate a random number uniformly in a range.
562 *
563 * This function generates a random number between \p min inclusive and
564 * \p N exclusive.
565 *
566 * The procedure complies with RFC 6979 §3.3 (deterministic ECDSA)
567 * when the RNG is a suitably parametrized instance of HMAC_DRBG
568 * and \p min is \c 1.
569 *
570 * \note There are `N - min` possible outputs. The lower bound
571 * \p min can be reached, but the upper bound \p N cannot.
572 *
573 * \param X The destination MPI, with \p limbs limbs.
574 * It must not be aliased with \p N or otherwise overlap it.
575 * \param min The minimum value to return.
576 * \param N The upper bound of the range, exclusive, with \p limbs limbs.
577 * In other words, this is one plus the maximum value to return.
578 * \p N must be strictly larger than \p min.
579 * \param limbs The number of limbs of \p N and \p X.
580 * This must not be 0.
581 * \param f_rng The RNG function to use. This must not be \c NULL.
582 * \param p_rng The RNG parameter to be passed to \p f_rng.
583 *
584 * \return \c 0 if successful.
585 * \return #MBEDTLS_ERR_MPI_NOT_ACCEPTABLE if the implementation was
586 * unable to find a suitable value within a limited number
587 * of attempts. This has a negligible probability if \p N
588 * is significantly larger than \p min, which is the case
589 * for all usual cryptographic applications.
590 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100591int mbedtls_mpi_core_random(mbedtls_mpi_uint *X,
592 mbedtls_mpi_uint min,
593 const mbedtls_mpi_uint *N,
594 size_t limbs,
595 int (*f_rng)(void *, unsigned char *, size_t),
596 void *p_rng);
Gilles Peskine4a8c5cd2022-10-18 18:15:01 +0200597
Janos Follathb6673f02022-09-30 14:13:14 +0100598/**
Tom Cosgrove0a0dded2022-12-06 14:37:18 +0000599 * \brief Returns the number of limbs of working memory required for
600 * a call to `mbedtls_mpi_core_exp_mod()`.
Janos Follathb6673f02022-09-30 14:13:14 +0100601 *
Tom Cosgrove28ff92c2022-12-12 17:06:27 +0000602 * \note This will always be at least
603 * `mbedtls_mpi_core_montmul_working_limbs(AN_limbs)`,
604 * i.e. sufficient for a call to `mbedtls_mpi_core_montmul()`.
605 *
Tom Cosgrove0a0dded2022-12-06 14:37:18 +0000606 * \param AN_limbs The number of limbs in the input `A` and the modulus `N`
607 * (they must be the same size) that will be given to
608 * `mbedtls_mpi_core_exp_mod()`.
609 * \param E_limbs The number of limbs in the exponent `E` that will be given
610 * to `mbedtls_mpi_core_exp_mod()`.
Janos Follathb6673f02022-09-30 14:13:14 +0100611 *
Tom Cosgrove0a0dded2022-12-06 14:37:18 +0000612 * \return The number of limbs of working memory required by
613 * `mbedtls_mpi_core_exp_mod()`.
Janos Follathb6673f02022-09-30 14:13:14 +0100614 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100615size_t mbedtls_mpi_core_exp_mod_working_limbs(size_t AN_limbs, size_t E_limbs);
Tom Cosgrove0a0dded2022-12-06 14:37:18 +0000616
617/**
618 * \brief Perform a modular exponentiation with secret exponent:
619 * X = A^E mod N, where \p A is already in Montgomery form.
620 *
Tom Cosgrovea7f0d7b2022-12-07 13:29:07 +0000621 * \p X may be aliased to \p A, but not to \p RR or \p E, even if \p E_limbs ==
622 * \p AN_limbs.
623 *
Waleed Elmelegy7b3024e2024-05-30 16:41:47 +0000624 * This function operates in constant time with respect
625 * to the values of \p A, \p N and \p E.
626 *
Tom Cosgrove0a0dded2022-12-06 14:37:18 +0000627 * \param[out] X The destination MPI, as a little endian array of length
628 * \p AN_limbs.
629 * \param[in] A The base MPI, as a little endian array of length \p AN_limbs.
630 * Must be in Montgomery form.
631 * \param[in] N The modulus, as a little endian array of length \p AN_limbs.
632 * \param AN_limbs The number of limbs in \p X, \p A, \p N, \p RR.
633 * \param[in] E The exponent, as a little endian array of length \p E_limbs.
634 * \param E_limbs The number of limbs in \p E.
635 * \param[in] RR The precomputed residue of 2^{2*biL} modulo N, as a little
636 * endian array of length \p AN_limbs.
637 * \param[in,out] T Temporary storage of at least the number of limbs returned
638 * by `mbedtls_mpi_core_exp_mod_working_limbs()`.
639 * Its initial content is unused and its final content is
640 * indeterminate.
641 * It must not alias or otherwise overlap any of the other
642 * parameters.
643 * It is up to the caller to zeroize \p T when it is no
644 * longer needed, and before freeing it if it was dynamically
645 * allocated.
646 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100647void mbedtls_mpi_core_exp_mod(mbedtls_mpi_uint *X,
648 const mbedtls_mpi_uint *A,
649 const mbedtls_mpi_uint *N, size_t AN_limbs,
650 const mbedtls_mpi_uint *E, size_t E_limbs,
651 const mbedtls_mpi_uint *RR,
652 mbedtls_mpi_uint *T);
Janos Follathb6673f02022-09-30 14:13:14 +0100653
Hanno Beckerd9b23482022-08-25 08:25:19 +0100654/**
655 * \brief Subtract unsigned integer from known-size large unsigned integers.
656 * Return the borrow.
657 *
Tom Cosgrovef7ff4c92022-08-25 08:39:07 +0100658 * \param[out] X The result of the subtraction.
659 * \param[in] A The left operand.
660 * \param b The unsigned scalar to subtract.
661 * \param limbs Number of limbs of \p X and \p A.
Hanno Beckerd9b23482022-08-25 08:25:19 +0100662 *
Tom Cosgrovef7ff4c92022-08-25 08:39:07 +0100663 * \return 1 if `A < b`.
664 * 0 if `A >= b`.
Hanno Beckerd9b23482022-08-25 08:25:19 +0100665 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100666mbedtls_mpi_uint mbedtls_mpi_core_sub_int(mbedtls_mpi_uint *X,
667 const mbedtls_mpi_uint *A,
668 mbedtls_mpi_uint b,
669 size_t limbs);
Hanno Beckerd9b23482022-08-25 08:25:19 +0100670
Tom Cosgrove30f3b4d2022-12-12 16:54:57 +0000671/**
672 * \brief Determine if a given MPI has the value \c 0 in constant time with
673 * respect to the value (but not with respect to the number of limbs).
674 *
675 * \param[in] A The MPI to test.
676 * \param limbs Number of limbs in \p A.
677 *
Janos Follathaec1a862024-02-21 11:24:20 +0000678 * \return MBEDTLS_CT_FALSE if `A == 0`
679 * MBEDTLS_CT_TRUE if `A != 0`.
Tom Cosgrove30f3b4d2022-12-12 16:54:57 +0000680 */
Janos Follathadb9d2d2024-03-11 10:03:05 +0000681mbedtls_ct_condition_t mbedtls_mpi_core_check_zero_ct(const mbedtls_mpi_uint *A,
Janos Follathaec1a862024-02-21 11:24:20 +0000682 size_t limbs);
Tom Cosgrove30f3b4d2022-12-12 16:54:57 +0000683
Tom Cosgrove28ff92c2022-12-12 17:06:27 +0000684/**
685 * \brief Returns the number of limbs of working memory required for
686 * a call to `mbedtls_mpi_core_montmul()`.
687 *
688 * \param AN_limbs The number of limbs in the input `A` and the modulus `N`
689 * (they must be the same size) that will be given to
690 * `mbedtls_mpi_core_montmul()` or one of the other functions
691 * that specifies this as the amount of working memory needed.
692 *
693 * \return The number of limbs of working memory required by
694 * `mbedtls_mpi_core_montmul()` (or other similar function).
695 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100696static inline size_t mbedtls_mpi_core_montmul_working_limbs(size_t AN_limbs)
Tom Cosgrove28ff92c2022-12-12 17:06:27 +0000697{
Gilles Peskine449bd832023-01-11 14:50:10 +0100698 return 2 * AN_limbs + 1;
Tom Cosgrove28ff92c2022-12-12 17:06:27 +0000699}
700
Tom Cosgrove786848b2022-12-13 10:45:19 +0000701/** Convert an MPI into Montgomery form.
702 *
703 * \p X may be aliased to \p A, but may not otherwise overlap it.
704 *
Tom Cosgrove5c8505f2023-03-07 11:39:52 +0000705 * \p X may not alias \p N (it is in canonical form, so must be strictly less
Tom Cosgrove786848b2022-12-13 10:45:19 +0000706 * than \p N). Nor may it alias or overlap \p rr (this is unlikely to be
707 * required in practice.)
708 *
709 * This function is a thin wrapper around `mbedtls_mpi_core_montmul()` that is
710 * an alternative to calling `mbedtls_mpi_mod_raw_to_mont_rep()` when we
711 * don't want to allocate memory.
712 *
713 * \param[out] X The result of the conversion.
714 * Must have the same number of limbs as \p A.
715 * \param[in] A The MPI to convert into Montgomery form.
716 * Must have the same number of limbs as the modulus.
717 * \param[in] N The address of the modulus, which gives the size of
Tom Cosgrovef7237542022-12-16 16:10:36 +0000718 * the base `R` = 2^(biL*N->limbs).
Tom Cosgrove786848b2022-12-13 10:45:19 +0000719 * \param[in] AN_limbs The number of limbs in \p X, \p A, \p N and \p rr.
720 * \param mm The Montgomery constant for \p N: -N^-1 mod 2^biL.
Tom Cosgroveb38c2ed2022-12-14 13:11:46 +0000721 * This can be determined by calling
Tom Cosgrove786848b2022-12-13 10:45:19 +0000722 * `mbedtls_mpi_core_montmul_init()`.
723 * \param[in] rr The residue for `2^{2*n*biL} mod N`.
724 * \param[in,out] T Temporary storage of size at least
725 * `mbedtls_mpi_core_montmul_working_limbs(AN_limbs)`
726 * limbs.
727 * Its initial content is unused and
728 * its final content is indeterminate.
729 * It must not alias or otherwise overlap any of the
730 * other parameters.
731 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100732void mbedtls_mpi_core_to_mont_rep(mbedtls_mpi_uint *X,
733 const mbedtls_mpi_uint *A,
734 const mbedtls_mpi_uint *N,
735 size_t AN_limbs,
736 mbedtls_mpi_uint mm,
737 const mbedtls_mpi_uint *rr,
738 mbedtls_mpi_uint *T);
Tom Cosgrove786848b2022-12-13 10:45:19 +0000739
740/** Convert an MPI from Montgomery form.
741 *
742 * \p X may be aliased to \p A, but may not otherwise overlap it.
743 *
Tom Cosgrove5c8505f2023-03-07 11:39:52 +0000744 * \p X may not alias \p N (it is in canonical form, so must be strictly less
Tom Cosgrove786848b2022-12-13 10:45:19 +0000745 * than \p N).
746 *
747 * This function is a thin wrapper around `mbedtls_mpi_core_montmul()` that is
748 * an alternative to calling `mbedtls_mpi_mod_raw_from_mont_rep()` when we
749 * don't want to allocate memory.
750 *
751 * \param[out] X The result of the conversion.
752 * Must have the same number of limbs as \p A.
753 * \param[in] A The MPI to convert from Montgomery form.
754 * Must have the same number of limbs as the modulus.
755 * \param[in] N The address of the modulus, which gives the size of
Tom Cosgrovef7237542022-12-16 16:10:36 +0000756 * the base `R` = 2^(biL*N->limbs).
Tom Cosgrove786848b2022-12-13 10:45:19 +0000757 * \param[in] AN_limbs The number of limbs in \p X, \p A and \p N.
758 * \param mm The Montgomery constant for \p N: -N^-1 mod 2^biL.
Tom Cosgroveb38c2ed2022-12-14 13:11:46 +0000759 * This can be determined by calling
Tom Cosgrove786848b2022-12-13 10:45:19 +0000760 * `mbedtls_mpi_core_montmul_init()`.
761 * \param[in,out] T Temporary storage of size at least
762 * `mbedtls_mpi_core_montmul_working_limbs(AN_limbs)`
763 * limbs.
764 * Its initial content is unused and
765 * its final content is indeterminate.
766 * It must not alias or otherwise overlap any of the
767 * other parameters.
768 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100769void mbedtls_mpi_core_from_mont_rep(mbedtls_mpi_uint *X,
770 const mbedtls_mpi_uint *A,
771 const mbedtls_mpi_uint *N,
772 size_t AN_limbs,
773 mbedtls_mpi_uint mm,
774 mbedtls_mpi_uint *T);
Tom Cosgrove786848b2022-12-13 10:45:19 +0000775
Gabor Mezeib9030702022-07-18 23:09:45 +0200776#endif /* MBEDTLS_BIGNUM_CORE_H */