blob: e64128f160ec630ad432891a4d65339a7a0aaefd [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
Janos Follath90b42712024-08-12 19:32:45 +010093/* Constants to identify whether a value is public or secret. If a parameter is marked as secret by
94 * this constant, the function must be constant time with respect to the parameter.
95 *
96 * This is only needed for functions with the _optionally_safe postfix. All other functions have
97 * fixed behavior that can't be changed at runtime and are constant time with respect to their
98 * parameters as prescribed by their documentation or by conventions in their module's documentation.
99 *
100 * Parameters should be named X_public where X is the name of the
101 * corresponding input parameter.
102 *
103 * Implementation should always check using
104 * if (X_public == MBEDTLS_MPI_IS_PUBLIC) {
105 * // unsafe path
106 * } else {
107 * // safe path
108 * }
109 * not the other way round, in order to prevent misuse. (This is, if a value
110 * other than the two below is passed, default to the safe path.) */
Janos Follath0c292b22024-08-12 19:55:02 +0100111#define MBEDTLS_MPI_IS_PUBLIC 0x2a2a2a2a
Janos Follath90b42712024-08-12 19:32:45 +0100112#define MBEDTLS_MPI_IS_SECRET 0
Janos Follath42f72b32024-08-22 08:25:33 +0100113#if defined(MBEDTLS_TEST_HOOKS) && !defined(MBEDTLS_THREADING_C)
114// Default value for testing that is neither MBEDTLS_MPI_IS_PUBLIC nor MBEDTLS_MPI_IS_SECRET
115#define MBEDTLS_MPI_IS_TEST 1
116#endif
Janos Follath90b42712024-08-12 19:32:45 +0100117
Gabor Mezei37b06362022-08-02 17:22:18 +0200118/** Count leading zero bits in a given integer.
119 *
Dave Rodgman0f16d562023-04-24 12:53:29 +0100120 * \warning The result is undefined if \p a == 0
121 *
Janos Follathb7a88ec2022-08-19 12:24:40 +0100122 * \param a Integer to count leading zero bits.
Gabor Mezei37b06362022-08-02 17:22:18 +0200123 *
Dave Rodgman0f16d562023-04-24 12:53:29 +0100124 * \return The number of leading zero bits in \p a, if \p a != 0.
125 * If \p a == 0, the result is undefined.
Gabor Mezei37b06362022-08-02 17:22:18 +0200126 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100127size_t mbedtls_mpi_core_clz(mbedtls_mpi_uint a);
Janos Follath4670f882022-07-21 18:25:42 +0100128
Janos Follatha95f2042022-08-19 12:09:17 +0100129/** Return the minimum number of bits required to represent the value held
Janos Follath63184682022-08-11 17:42:59 +0100130 * in the MPI.
131 *
Janos Follathb7a88ec2022-08-19 12:24:40 +0100132 * \note This function returns 0 if all the limbs of \p A are 0.
Gabor Mezei37b06362022-08-02 17:22:18 +0200133 *
Janos Follathb7a88ec2022-08-19 12:24:40 +0100134 * \param[in] A The address of the MPI.
Janos Follathc4596412022-08-22 10:01:27 +0100135 * \param A_limbs The number of limbs of \p A.
Gabor Mezei37b06362022-08-02 17:22:18 +0200136 *
Janos Follathb7a88ec2022-08-19 12:24:40 +0100137 * \return The number of bits in \p A.
Gabor Mezei37b06362022-08-02 17:22:18 +0200138 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100139size_t mbedtls_mpi_core_bitlen(const mbedtls_mpi_uint *A, size_t A_limbs);
Janos Follath4670f882022-07-21 18:25:42 +0100140
Gabor Mezei37b06362022-08-02 17:22:18 +0200141/** Convert a big-endian byte array aligned to the size of mbedtls_mpi_uint
142 * into the storage form used by mbedtls_mpi.
143 *
Janos Follathb7a88ec2022-08-19 12:24:40 +0100144 * \param[in,out] A The address of the MPI.
Janos Follathc4596412022-08-22 10:01:27 +0100145 * \param A_limbs The number of limbs of \p A.
Gabor Mezei37b06362022-08-02 17:22:18 +0200146 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100147void mbedtls_mpi_core_bigendian_to_host(mbedtls_mpi_uint *A,
148 size_t A_limbs);
Janos Follath4670f882022-07-21 18:25:42 +0100149
Gilles Peskine6f949ea2022-09-20 18:38:35 +0200150/** \brief Compare a machine integer with an MPI.
151 *
152 * This function operates in constant time with respect
153 * to the values of \p min and \p A.
154 *
155 * \param min A machine integer.
156 * \param[in] A An MPI.
157 * \param A_limbs The number of limbs of \p A.
158 * This must be at least 1.
159 *
Dave Rodgmanfd7fab42023-05-17 14:00:39 +0100160 * \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 +0200161 */
Dave Rodgmanfd7fab42023-05-17 14:00:39 +0100162mbedtls_ct_condition_t mbedtls_mpi_core_uint_le_mpi(mbedtls_mpi_uint min,
163 const mbedtls_mpi_uint *A,
164 size_t A_limbs);
Gilles Peskine6f949ea2022-09-20 18:38:35 +0200165
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200166/**
Dave Rodgman7d4f0192023-05-09 14:01:05 +0100167 * \brief Check if one unsigned MPI is less than another in constant
168 * time.
169 *
170 * \param A The left-hand MPI. This must point to an array of limbs
171 * with the same allocated length as \p B.
172 * \param B The right-hand MPI. This must point to an array of limbs
173 * with the same allocated length as \p A.
174 * \param limbs The number of limbs in \p A and \p B.
175 * This must not be 0.
176 *
Dave Rodgman8ac9a1d2023-05-17 15:16:22 +0100177 * \return MBEDTLS_CT_TRUE if \p A is less than \p B.
178 * MBEDTLS_CT_FALSE if \p A is greater than or equal to \p B.
Dave Rodgman7d4f0192023-05-09 14:01:05 +0100179 */
Dave Rodgman8ac9a1d2023-05-17 15:16:22 +0100180mbedtls_ct_condition_t mbedtls_mpi_core_lt_ct(const mbedtls_mpi_uint *A,
181 const mbedtls_mpi_uint *B,
182 size_t limbs);
183
Dave Rodgman7d4f0192023-05-09 14:01:05 +0100184/**
Gabor Mezei4086de62022-10-14 16:29:42 +0200185 * \brief Perform a safe conditional copy of an MPI which doesn't reveal
186 * whether assignment was done or not.
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200187 *
Gabor Mezei4086de62022-10-14 16:29:42 +0200188 * \param[out] X The address of the destination MPI.
189 * This must be initialized. Must have enough limbs to
190 * store the full value of \p A.
191 * \param[in] A The address of the source MPI. This must be initialized.
Gabor Mezei1c628d52022-09-27 12:13:51 +0200192 * \param limbs The number of limbs of \p A.
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200193 * \param assign The condition deciding whether to perform the
Dave Rodgmanfb1b8512023-07-31 12:27:05 +0100194 * assignment or not. Callers will need to use
195 * the constant time interface (e.g. `mbedtls_ct_bool()`)
196 * to construct this argument.
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200197 *
198 * \note This function avoids leaking any information about whether
199 * the assignment was done or not.
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200200 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100201void mbedtls_mpi_core_cond_assign(mbedtls_mpi_uint *X,
202 const mbedtls_mpi_uint *A,
203 size_t limbs,
Dave Rodgmancd2e38b2023-05-17 13:31:55 +0100204 mbedtls_ct_condition_t assign);
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200205
206/**
Gabor Mezei4086de62022-10-14 16:29:42 +0200207 * \brief Perform a safe conditional swap of two MPIs which doesn't reveal
208 * whether the swap was done or not.
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200209 *
Gabor Mezei86dfe382022-09-30 14:03:04 +0200210 * \param[in,out] X The address of the first MPI.
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200211 * This must be initialized.
Gabor Mezei86dfe382022-09-30 14:03:04 +0200212 * \param[in,out] Y The address of the second MPI.
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200213 * This must be initialized.
Gabor Mezeie5b85852022-09-30 13:54:02 +0200214 * \param limbs The number of limbs of \p X and \p Y.
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200215 * \param swap The condition deciding whether to perform
Dave Rodgmancd2e38b2023-05-17 13:31:55 +0100216 * the swap or not.
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200217 *
218 * \note This function avoids leaking any information about whether
219 * the swap was done or not.
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200220 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100221void mbedtls_mpi_core_cond_swap(mbedtls_mpi_uint *X,
222 mbedtls_mpi_uint *Y,
223 size_t limbs,
Dave Rodgmancd2e38b2023-05-17 13:31:55 +0100224 mbedtls_ct_condition_t swap);
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200225
Janos Follathaf3f39c2022-08-22 09:06:32 +0100226/** Import X from unsigned binary data, little-endian.
Gabor Mezei37b06362022-08-02 17:22:18 +0200227 *
Janos Follath63184682022-08-11 17:42:59 +0100228 * The MPI needs to have enough limbs to store the full value (including any
229 * most significant zero bytes in the input).
Gabor Mezei37b06362022-08-02 17:22:18 +0200230 *
Janos Follathb7a88ec2022-08-19 12:24:40 +0100231 * \param[out] X The address of the MPI.
232 * \param X_limbs The number of limbs of \p X.
233 * \param[in] input The input buffer to import from.
234 * \param input_length The length bytes of \p input.
Gabor Mezei37b06362022-08-02 17:22:18 +0200235 *
236 * \return \c 0 if successful.
237 * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p X isn't
Janos Follathb7a88ec2022-08-19 12:24:40 +0100238 * large enough to hold the value in \p input.
Gabor Mezei37b06362022-08-02 17:22:18 +0200239 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100240int mbedtls_mpi_core_read_le(mbedtls_mpi_uint *X,
241 size_t X_limbs,
242 const unsigned char *input,
243 size_t input_length);
Gabor Mezeib9030702022-07-18 23:09:45 +0200244
Janos Follathaf3f39c2022-08-22 09:06:32 +0100245/** Import X from unsigned binary data, big-endian.
Gabor Mezei37b06362022-08-02 17:22:18 +0200246 *
Janos Follath63184682022-08-11 17:42:59 +0100247 * The MPI needs to have enough limbs to store the full value (including any
248 * most significant zero bytes in the input).
Gabor Mezei37b06362022-08-02 17:22:18 +0200249 *
Janos Follathb7a88ec2022-08-19 12:24:40 +0100250 * \param[out] X The address of the MPI.
Tom Cosgrove8a1f7842023-02-01 08:43:54 +0000251 * May only be #NULL if \p X_limbs is 0 and \p input_length
Janos Follathb7a88ec2022-08-19 12:24:40 +0100252 * is 0.
253 * \param X_limbs The number of limbs of \p X.
254 * \param[in] input The input buffer to import from.
255 * May only be #NULL if \p input_length is 0.
256 * \param input_length The length in bytes of \p input.
Gabor Mezei37b06362022-08-02 17:22:18 +0200257 *
258 * \return \c 0 if successful.
259 * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p X isn't
Janos Follathb7a88ec2022-08-19 12:24:40 +0100260 * large enough to hold the value in \p input.
Gabor Mezei37b06362022-08-02 17:22:18 +0200261 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100262int mbedtls_mpi_core_read_be(mbedtls_mpi_uint *X,
263 size_t X_limbs,
264 const unsigned char *input,
265 size_t input_length);
Gabor Mezeib9030702022-07-18 23:09:45 +0200266
Janos Follathaf3f39c2022-08-22 09:06:32 +0100267/** Export A into unsigned binary data, little-endian.
Gabor Mezei37b06362022-08-02 17:22:18 +0200268 *
Janos Follathb7a88ec2022-08-19 12:24:40 +0100269 * \note If \p output is shorter than \p A the export is still successful if the
270 * value held in \p A fits in the buffer (that is, if enough of the most
271 * significant bytes of \p A are 0).
Janos Follath63184682022-08-11 17:42:59 +0100272 *
Janos Follathb7a88ec2022-08-19 12:24:40 +0100273 * \param[in] A The address of the MPI.
274 * \param A_limbs The number of limbs of \p A.
275 * \param[out] output The output buffer to export to.
276 * \param output_length The length in bytes of \p output.
Gabor Mezei37b06362022-08-02 17:22:18 +0200277 *
278 * \return \c 0 if successful.
Janos Follathb7a88ec2022-08-19 12:24:40 +0100279 * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p output isn't
280 * large enough to hold the value of \p A.
Gabor Mezei37b06362022-08-02 17:22:18 +0200281 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100282int mbedtls_mpi_core_write_le(const mbedtls_mpi_uint *A,
283 size_t A_limbs,
284 unsigned char *output,
285 size_t output_length);
Gabor Mezeib9030702022-07-18 23:09:45 +0200286
Janos Follathaf3f39c2022-08-22 09:06:32 +0100287/** Export A into unsigned binary data, big-endian.
Gabor Mezei37b06362022-08-02 17:22:18 +0200288 *
Janos Follathb7a88ec2022-08-19 12:24:40 +0100289 * \note If \p output is shorter than \p A the export is still successful if the
290 * value held in \p A fits in the buffer (that is, if enough of the most
291 * significant bytes of \p A are 0).
Janos Follath63184682022-08-11 17:42:59 +0100292 *
Janos Follathb7a88ec2022-08-19 12:24:40 +0100293 * \param[in] A The address of the MPI.
294 * \param A_limbs The number of limbs of \p A.
295 * \param[out] output The output buffer to export to.
296 * \param output_length The length in bytes of \p output.
Gabor Mezei37b06362022-08-02 17:22:18 +0200297 *
298 * \return \c 0 if successful.
Janos Follathb7a88ec2022-08-19 12:24:40 +0100299 * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p output isn't
300 * large enough to hold the value of \p A.
Gabor Mezei37b06362022-08-02 17:22:18 +0200301 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100302int mbedtls_mpi_core_write_be(const mbedtls_mpi_uint *A,
303 size_t A_limbs,
304 unsigned char *output,
305 size_t output_length);
Gabor Mezeib9030702022-07-18 23:09:45 +0200306
Minos Galanakisec09e252023-04-20 14:22:16 +0100307/** \brief Shift an MPI in-place right by a number of bits.
Gilles Peskine66414202022-09-21 15:36:16 +0200308 *
309 * Shifting by more bits than there are bit positions
310 * in \p X is valid and results in setting \p X to 0.
311 *
312 * This function's execution time depends on the value
313 * of \p count (and of course \p limbs).
314 *
315 * \param[in,out] X The number to shift.
316 * \param limbs The number of limbs of \p X. This must be at least 1.
317 * \param count The number of bits to shift by.
318 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100319void mbedtls_mpi_core_shift_r(mbedtls_mpi_uint *X, size_t limbs,
320 size_t count);
Gilles Peskine66414202022-09-21 15:36:16 +0200321
Tom Cosgrove90c426b2022-08-23 16:15:19 +0100322/**
Minos Galanakisec09e252023-04-20 14:22:16 +0100323 * \brief Shift an MPI in-place left by a number of bits.
Minos Galanakisad808dd2023-04-20 12:18:41 +0100324 *
Minos Galanakisec09e252023-04-20 14:22:16 +0100325 * Shifting by more bits than there are bit positions
Minos Galanakisb8944032023-04-28 14:09:44 +0100326 * in \p X will produce an unspecified result.
Minos Galanakisad808dd2023-04-20 12:18:41 +0100327 *
Minos Galanakisec09e252023-04-20 14:22:16 +0100328 * This function's execution time depends on the value
329 * of \p count (and of course \p limbs).
330 * \param[in,out] X The number to shift.
331 * \param limbs The number of limbs of \p X. This must be at least 1.
332 * \param count The number of bits to shift by.
Minos Galanakisad808dd2023-04-20 12:18:41 +0100333 */
Minos Galanakisec09e252023-04-20 14:22:16 +0100334void mbedtls_mpi_core_shift_l(mbedtls_mpi_uint *X, size_t limbs,
335 size_t count);
Minos Galanakisad808dd2023-04-20 12:18:41 +0100336
337/**
Tom Cosgroveaf7d44b2022-08-24 14:05:26 +0100338 * \brief Add two fixed-size large unsigned integers, returning the carry.
Hanno Beckerc9887132022-08-24 12:54:36 +0100339 *
Tom Cosgroveaf7d44b2022-08-24 14:05:26 +0100340 * Calculates `A + B` where `A` and `B` have the same size.
341 *
Tom Cosgrove82f13102022-10-25 12:46:03 +0100342 * This function operates modulo `2^(biL*limbs)` and returns the carry
Hanno Beckerc9887132022-08-24 12:54:36 +0100343 * (1 if there was a wraparound, and 0 otherwise).
344 *
Tom Cosgroveaf7d44b2022-08-24 14:05:26 +0100345 * \p X may be aliased to \p A or \p B.
Hanno Beckerc9887132022-08-24 12:54:36 +0100346 *
Tom Cosgroveaf7d44b2022-08-24 14:05:26 +0100347 * \param[out] X The result of the addition.
348 * \param[in] A Little-endian presentation of the left operand.
349 * \param[in] B Little-endian presentation of the right operand.
350 * \param limbs Number of limbs of \p X, \p A and \p B.
Hanno Beckerc9887132022-08-24 12:54:36 +0100351 *
Tom Cosgroveaf7d44b2022-08-24 14:05:26 +0100352 * \return 1 if `A + B >= 2^(biL*limbs)`, 0 otherwise.
Hanno Beckerc9887132022-08-24 12:54:36 +0100353 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100354mbedtls_mpi_uint mbedtls_mpi_core_add(mbedtls_mpi_uint *X,
355 const mbedtls_mpi_uint *A,
356 const mbedtls_mpi_uint *B,
357 size_t limbs);
Hanno Beckerc9887132022-08-24 12:54:36 +0100358
Tom Cosgrove90c426b2022-08-23 16:15:19 +0100359/**
Tom Cosgrove5c0e8102022-09-15 15:46:10 +0100360 * \brief Conditional addition of two fixed-size large unsigned integers,
Tom Cosgroved932de82022-08-25 16:43:43 +0100361 * returning the carry.
Hanno Becker71f4b0d2022-08-23 12:09:35 +0100362 *
363 * Functionally equivalent to
364 *
365 * ```
366 * if( cond )
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100367 * X += A;
Hanno Becker71f4b0d2022-08-23 12:09:35 +0100368 * return carry;
369 * ```
370 *
Tom Cosgrove47828232022-09-20 13:51:50 +0100371 * This function operates modulo `2^(biL*limbs)`.
372 *
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100373 * \param[in,out] X The pointer to the (little-endian) array
Tom Cosgrove72594632022-08-24 11:51:58 +0100374 * representing the bignum to accumulate onto.
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100375 * \param[in] A The pointer to the (little-endian) array
Tom Cosgrove72594632022-08-24 11:51:58 +0100376 * representing the bignum to conditionally add
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100377 * to \p X. This may be aliased to \p X but may not
Tom Cosgroveed43c6c2022-08-31 11:35:00 +0100378 * overlap otherwise.
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100379 * \param limbs Number of limbs of \p X and \p A.
Tom Cosgrove72594632022-08-24 11:51:58 +0100380 * \param cond Condition bit dictating whether addition should
381 * happen or not. This must be \c 0 or \c 1.
Hanno Becker71f4b0d2022-08-23 12:09:35 +0100382 *
Tom Cosgroveecbb1242022-08-25 10:13:44 +0100383 * \warning If \p cond is neither 0 nor 1, the result of this function
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100384 * is unspecified, and the resulting value in \p X might be
385 * neither its original value nor \p X + \p A.
Tom Cosgrove72594632022-08-24 11:51:58 +0100386 *
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100387 * \return 1 if `X + cond * A >= 2^(biL*limbs)`, 0 otherwise.
Hanno Becker71f4b0d2022-08-23 12:09:35 +0100388 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100389mbedtls_mpi_uint mbedtls_mpi_core_add_if(mbedtls_mpi_uint *X,
390 const mbedtls_mpi_uint *A,
391 size_t limbs,
392 unsigned cond);
Hanno Becker71f4b0d2022-08-23 12:09:35 +0100393
Tom Cosgroveb4964862022-08-30 11:57:22 +0100394/**
Tom Cosgrove5c0e8102022-09-15 15:46:10 +0100395 * \brief Subtract two fixed-size large unsigned integers, returning the borrow.
Tom Cosgroveb4964862022-08-30 11:57:22 +0100396 *
Tom Cosgrove5dd97e62022-08-30 14:31:49 +0100397 * Calculate `A - B` where \p A and \p B have the same size.
Tom Cosgrove630110a2022-08-31 17:09:29 +0100398 * This function operates modulo `2^(biL*limbs)` and returns the carry
Tom Cosgroveb4964862022-08-30 11:57:22 +0100399 * (1 if there was a wraparound, i.e. if `A < B`, and 0 otherwise).
400 *
Tom Cosgrove5dd97e62022-08-30 14:31:49 +0100401 * \p X may be aliased to \p A or \p B, or even both, but may not overlap
402 * either otherwise.
Tom Cosgroveb4964862022-08-30 11:57:22 +0100403 *
404 * \param[out] X The result of the subtraction.
405 * \param[in] A Little-endian presentation of left operand.
406 * \param[in] B Little-endian presentation of right operand.
407 * \param limbs Number of limbs of \p X, \p A and \p B.
408 *
409 * \return 1 if `A < B`.
410 * 0 if `A >= B`.
411 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100412mbedtls_mpi_uint mbedtls_mpi_core_sub(mbedtls_mpi_uint *X,
413 const mbedtls_mpi_uint *A,
414 const mbedtls_mpi_uint *B,
415 size_t limbs);
Tom Cosgroveb4964862022-08-30 11:57:22 +0100416
417/**
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100418 * \brief Perform a fixed-size multiply accumulate operation: X += b * A
Tom Cosgroveb4964862022-08-30 11:57:22 +0100419 *
Tom Cosgroveb0b77e12022-09-20 13:33:40 +0100420 * \p X may be aliased to \p A (when \p X_limbs == \p A_limbs), but may not
421 * otherwise overlap.
422 *
Tom Cosgrove47828232022-09-20 13:51:50 +0100423 * This function operates modulo `2^(biL*X_limbs)`.
424 *
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100425 * \param[in,out] X The pointer to the (little-endian) array
Tom Cosgroveb4964862022-08-30 11:57:22 +0100426 * representing the bignum to accumulate onto.
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100427 * \param X_limbs The number of limbs of \p X. This must be
428 * at least \p A_limbs.
429 * \param[in] A The pointer to the (little-endian) array
Tom Cosgroveb4964862022-08-30 11:57:22 +0100430 * representing the bignum to multiply with.
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100431 * This may be aliased to \p X but may not overlap
Tom Cosgroveed43c6c2022-08-31 11:35:00 +0100432 * otherwise.
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100433 * \param A_limbs The number of limbs of \p A.
434 * \param b X scalar to multiply with.
Tom Cosgroveb4964862022-08-30 11:57:22 +0100435 *
436 * \return The carry at the end of the operation.
437 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100438mbedtls_mpi_uint mbedtls_mpi_core_mla(mbedtls_mpi_uint *X, size_t X_limbs,
439 const mbedtls_mpi_uint *A, size_t A_limbs,
440 mbedtls_mpi_uint b);
Tom Cosgroveb4964862022-08-30 11:57:22 +0100441
Hanno Becker4ae890b2022-08-24 16:17:52 +0100442/**
443 * \brief Perform a known-size multiplication
444 *
Gabor Mezeid6260512023-04-03 17:32:55 +0200445 * \p X may not be aliased to any of the inputs for this function.
Gabor Mezei6f182c32023-03-31 16:09:28 +0200446 * \p A may be aliased to \p B.
447 *
Tom Cosgrove6af26f32022-08-24 16:40:55 +0100448 * \param[out] X The pointer to the (little-endian) array to receive
449 * the product of \p A_limbs and \p B_limbs.
450 * This must be of length \p A_limbs + \p B_limbs.
451 * \param[in] A The pointer to the (little-endian) array
452 * representing the first factor.
453 * \param A_limbs The number of limbs in \p A.
454 * \param[in] B The pointer to the (little-endian) array
455 * representing the second factor.
456 * \param B_limbs The number of limbs in \p B.
Hanno Becker4ae890b2022-08-24 16:17:52 +0100457 */
Tom Cosgrove6af26f32022-08-24 16:40:55 +0100458void mbedtls_mpi_core_mul(mbedtls_mpi_uint *X,
459 const mbedtls_mpi_uint *A, size_t A_limbs,
460 const mbedtls_mpi_uint *B, size_t B_limbs);
Hanno Becker4ae890b2022-08-24 16:17:52 +0100461
Tom Cosgroveb4964862022-08-30 11:57:22 +0100462/**
463 * \brief Calculate initialisation value for fast Montgomery modular
464 * multiplication
465 *
466 * \param[in] N Little-endian presentation of the modulus. This must have
467 * at least one limb.
468 *
469 * \return The initialisation value for fast Montgomery modular multiplication
470 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100471mbedtls_mpi_uint mbedtls_mpi_core_montmul_init(const mbedtls_mpi_uint *N);
Tom Cosgroveb4964862022-08-30 11:57:22 +0100472
473/**
Tom Cosgrove4386ead2022-09-29 14:40:21 +0100474 * \brief Montgomery multiplication: X = A * B * R^-1 mod N (HAC 14.36)
475 *
476 * \p A and \p B must be in canonical form. That is, < \p N.
Tom Cosgroveb4964862022-08-30 11:57:22 +0100477 *
Tom Cosgroveea45c1d2022-09-20 13:17:51 +0100478 * \p X may be aliased to \p A or \p N, or even \p B (if \p AN_limbs ==
479 * \p B_limbs) but may not overlap any parameters otherwise.
480 *
Tom Cosgrove4386ead2022-09-29 14:40:21 +0100481 * \p A and \p B may alias each other, if \p AN_limbs == \p B_limbs. They may
482 * not alias \p N (since they must be in canonical form, they cannot == \p N).
Tom Cosgroveea45c1d2022-09-20 13:17:51 +0100483 *
Tom Cosgroveb4964862022-08-30 11:57:22 +0100484 * \param[out] X The destination MPI, as a little-endian array of
485 * length \p AN_limbs.
486 * On successful completion, X contains the result of
Tom Cosgrove630110a2022-08-31 17:09:29 +0100487 * the multiplication `A * B * R^-1` mod N where
488 * `R = 2^(biL*AN_limbs)`.
Tom Cosgroveb4964862022-08-30 11:57:22 +0100489 * \param[in] A Little-endian presentation of first operand.
Tom Cosgrove5dd97e62022-08-30 14:31:49 +0100490 * Must have the same number of limbs as \p N.
Tom Cosgroveb4964862022-08-30 11:57:22 +0100491 * \param[in] B Little-endian presentation of second operand.
492 * \param[in] B_limbs The number of limbs in \p B.
Tom Cosgrove5dd97e62022-08-30 14:31:49 +0100493 * Must be <= \p AN_limbs.
Tom Cosgroveb4964862022-08-30 11:57:22 +0100494 * \param[in] N Little-endian presentation of the modulus.
Tom Cosgrove5dd97e62022-08-30 14:31:49 +0100495 * This must be odd, and have exactly the same number
496 * of limbs as \p A.
Tom Cosgrove6da3a3b2022-09-29 17:20:18 +0100497 * It may alias \p X, but must not alias or otherwise
498 * overlap any of the other parameters.
Tom Cosgrove5dd97e62022-08-30 14:31:49 +0100499 * \param[in] AN_limbs The number of limbs in \p X, \p A and \p N.
Tom Cosgrove630110a2022-08-31 17:09:29 +0100500 * \param mm The Montgomery constant for \p N: -N^-1 mod 2^biL.
Tom Cosgroveb7438d12022-09-15 15:05:59 +0100501 * This can be calculated by `mbedtls_mpi_core_montmul_init()`.
Tom Cosgroveb4964862022-08-30 11:57:22 +0100502 * \param[in,out] T Temporary storage of size at least 2*AN_limbs+1 limbs.
503 * Its initial content is unused and
504 * its final content is indeterminate.
Tom Cosgrove4386ead2022-09-29 14:40:21 +0100505 * It must not alias or otherwise overlap any of the
506 * other parameters.
Tom Cosgroveb4964862022-08-30 11:57:22 +0100507 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100508void mbedtls_mpi_core_montmul(mbedtls_mpi_uint *X,
509 const mbedtls_mpi_uint *A,
510 const mbedtls_mpi_uint *B, size_t B_limbs,
511 const mbedtls_mpi_uint *N, size_t AN_limbs,
512 mbedtls_mpi_uint mm, mbedtls_mpi_uint *T);
Tom Cosgroveb4964862022-08-30 11:57:22 +0100513
Hanno Beckerec440f22022-08-11 17:29:32 +0100514/**
Minos Galanakisb85506e2022-10-20 09:51:53 +0100515 * \brief Calculate the square of the Montgomery constant. (Needed
516 * for conversion and operations in Montgomery form.)
Hanno Beckerec440f22022-08-11 17:29:32 +0100517 *
518 * \param[out] X A pointer to the result of the calculation of
Minos Galanakisb85506e2022-10-20 09:51:53 +0100519 * the square of the Montgomery constant:
520 * 2^{2*n*biL} mod N.
Hanno Beckerec440f22022-08-11 17:29:32 +0100521 * \param[in] N Little-endian presentation of the modulus, which must be odd.
522 *
523 * \return 0 if successful.
524 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if there is not enough space
525 * to store the value of Montgomery constant squared.
526 * \return #MBEDTLS_ERR_MPI_DIVISION_BY_ZERO if \p N modulus is zero.
527 * \return #MBEDTLS_ERR_MPI_NEGATIVE_VALUE if \p N modulus is negative.
Hanno Beckerec440f22022-08-11 17:29:32 +0100528 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100529int mbedtls_mpi_core_get_mont_r2_unsafe(mbedtls_mpi *X,
530 const mbedtls_mpi *N);
Hanno Beckerec440f22022-08-11 17:29:32 +0100531
Janos Follath59cbd1b2022-10-28 18:13:43 +0100532#if defined(MBEDTLS_TEST_HOOKS)
Janos Follathe50f2f12022-10-26 15:14:33 +0100533/**
Janos Follath8904a2d2022-10-31 15:32:28 +0000534 * Copy an MPI from a table without leaking the index.
Janos Follathe50f2f12022-10-26 15:14:33 +0100535 *
536 * \param dest The destination buffer. This must point to a writable
537 * buffer of at least \p limbs limbs.
538 * \param table The address of the table. This must point to a readable
Janos Follath8904a2d2022-10-31 15:32:28 +0000539 * array of \p count elements of \p limbs limbs each.
540 * \param limbs The number of limbs in each table entry.
541 * \param count The number of entries in \p table.
542 * \param index The (secret) table index to look up. This must be in the
543 * range `0 .. count-1`.
Janos Follathe50f2f12022-10-26 15:14:33 +0100544 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100545void mbedtls_mpi_core_ct_uint_table_lookup(mbedtls_mpi_uint *dest,
546 const mbedtls_mpi_uint *table,
547 size_t limbs,
548 size_t count,
549 size_t index);
Janos Follath59cbd1b2022-10-28 18:13:43 +0100550#endif /* MBEDTLS_TEST_HOOKS */
Janos Follathe50f2f12022-10-26 15:14:33 +0100551
Gilles Peskine909e03c2022-10-18 18:14:33 +0200552/**
553 * \brief Fill an integer with a number of random bytes.
554 *
555 * \param X The destination MPI.
556 * \param X_limbs The number of limbs of \p X.
557 * \param bytes The number of random bytes to generate.
558 * \param f_rng The RNG function to use. This must not be \c NULL.
559 * \param p_rng The RNG parameter to be passed to \p f_rng. This may be
560 * \c NULL if \p f_rng doesn't need a context argument.
561 *
562 * \return \c 0 if successful.
563 * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if \p X does not have
564 * enough room for \p bytes bytes.
565 * \return A negative error code on RNG failure.
566 *
567 * \note The bytes obtained from the RNG are interpreted
568 * as a big-endian representation of an MPI; this can
569 * be relevant in applications like deterministic ECDSA.
570 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100571int mbedtls_mpi_core_fill_random(mbedtls_mpi_uint *X, size_t X_limbs,
572 size_t bytes,
573 int (*f_rng)(void *, unsigned char *, size_t),
574 void *p_rng);
Gilles Peskine909e03c2022-10-18 18:14:33 +0200575
Gilles Peskine4a8c5cd2022-10-18 18:15:01 +0200576/** Generate a random number uniformly in a range.
577 *
578 * This function generates a random number between \p min inclusive and
579 * \p N exclusive.
580 *
581 * The procedure complies with RFC 6979 §3.3 (deterministic ECDSA)
582 * when the RNG is a suitably parametrized instance of HMAC_DRBG
583 * and \p min is \c 1.
584 *
585 * \note There are `N - min` possible outputs. The lower bound
586 * \p min can be reached, but the upper bound \p N cannot.
587 *
588 * \param X The destination MPI, with \p limbs limbs.
589 * It must not be aliased with \p N or otherwise overlap it.
590 * \param min The minimum value to return.
591 * \param N The upper bound of the range, exclusive, with \p limbs limbs.
592 * In other words, this is one plus the maximum value to return.
593 * \p N must be strictly larger than \p min.
594 * \param limbs The number of limbs of \p N and \p X.
595 * This must not be 0.
596 * \param f_rng The RNG function to use. This must not be \c NULL.
597 * \param p_rng The RNG parameter to be passed to \p f_rng.
598 *
599 * \return \c 0 if successful.
600 * \return #MBEDTLS_ERR_MPI_NOT_ACCEPTABLE if the implementation was
601 * unable to find a suitable value within a limited number
602 * of attempts. This has a negligible probability if \p N
603 * is significantly larger than \p min, which is the case
604 * for all usual cryptographic applications.
605 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100606int mbedtls_mpi_core_random(mbedtls_mpi_uint *X,
607 mbedtls_mpi_uint min,
608 const mbedtls_mpi_uint *N,
609 size_t limbs,
610 int (*f_rng)(void *, unsigned char *, size_t),
611 void *p_rng);
Gilles Peskine4a8c5cd2022-10-18 18:15:01 +0200612
Janos Follathb6673f02022-09-30 14:13:14 +0100613/**
Tom Cosgrove0a0dded2022-12-06 14:37:18 +0000614 * \brief Returns the number of limbs of working memory required for
615 * a call to `mbedtls_mpi_core_exp_mod()`.
Janos Follathb6673f02022-09-30 14:13:14 +0100616 *
Tom Cosgrove28ff92c2022-12-12 17:06:27 +0000617 * \note This will always be at least
618 * `mbedtls_mpi_core_montmul_working_limbs(AN_limbs)`,
619 * i.e. sufficient for a call to `mbedtls_mpi_core_montmul()`.
620 *
Tom Cosgrove0a0dded2022-12-06 14:37:18 +0000621 * \param AN_limbs The number of limbs in the input `A` and the modulus `N`
622 * (they must be the same size) that will be given to
623 * `mbedtls_mpi_core_exp_mod()`.
624 * \param E_limbs The number of limbs in the exponent `E` that will be given
625 * to `mbedtls_mpi_core_exp_mod()`.
Janos Follathb6673f02022-09-30 14:13:14 +0100626 *
Tom Cosgrove0a0dded2022-12-06 14:37:18 +0000627 * \return The number of limbs of working memory required by
628 * `mbedtls_mpi_core_exp_mod()`.
Janos Follathb6673f02022-09-30 14:13:14 +0100629 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100630size_t mbedtls_mpi_core_exp_mod_working_limbs(size_t AN_limbs, size_t E_limbs);
Tom Cosgrove0a0dded2022-12-06 14:37:18 +0000631
632/**
Manuel Pégourié-Gonnard75ed5872024-06-18 12:52:45 +0200633 * \brief Perform a modular exponentiation with public or secret exponent:
634 * X = A^E mod N, where \p A is already in Montgomery form.
635 *
Janos Follath38ff70e2024-08-12 18:20:59 +0100636 * \warning This function is not constant time with respect to \p E (the exponent).
637 *
Manuel Pégourié-Gonnard75ed5872024-06-18 12:52:45 +0200638 * \p X may be aliased to \p A, but not to \p RR or \p E, even if \p E_limbs ==
639 * \p AN_limbs.
640 *
641 * \param[out] X The destination MPI, as a little endian array of length
642 * \p AN_limbs.
643 * \param[in] A The base MPI, as a little endian array of length \p AN_limbs.
644 * Must be in Montgomery form.
645 * \param[in] N The modulus, as a little endian array of length \p AN_limbs.
646 * \param AN_limbs The number of limbs in \p X, \p A, \p N, \p RR.
647 * \param[in] E The exponent, as a little endian array of length \p E_limbs.
648 * \param E_limbs The number of limbs in \p E.
649 * \param[in] RR The precomputed residue of 2^{2*biL} modulo N, as a little
650 * endian array of length \p AN_limbs.
651 * \param[in,out] T Temporary storage of at least the number of limbs returned
652 * by `mbedtls_mpi_core_exp_mod_working_limbs()`.
653 * Its initial content is unused and its final content is
654 * indeterminate.
655 * It must not alias or otherwise overlap any of the other
656 * parameters.
657 * It is up to the caller to zeroize \p T when it is no
658 * longer needed, and before freeing it if it was dynamically
659 * allocated.
Manuel Pégourié-Gonnard75ed5872024-06-18 12:52:45 +0200660 */
Janos Follath38ff70e2024-08-12 18:20:59 +0100661void mbedtls_mpi_core_exp_mod_unsafe(mbedtls_mpi_uint *X,
662 const mbedtls_mpi_uint *A,
663 const mbedtls_mpi_uint *N, size_t AN_limbs,
664 const mbedtls_mpi_uint *E, size_t E_limbs,
665 const mbedtls_mpi_uint *RR,
666 mbedtls_mpi_uint *T);
Manuel Pégourié-Gonnard75ed5872024-06-18 12:52:45 +0200667
668/**
Tom Cosgrove0a0dded2022-12-06 14:37:18 +0000669 * \brief Perform a modular exponentiation with secret exponent:
670 * X = A^E mod N, where \p A is already in Montgomery form.
671 *
Tom Cosgrovea7f0d7b2022-12-07 13:29:07 +0000672 * \p X may be aliased to \p A, but not to \p RR or \p E, even if \p E_limbs ==
673 * \p AN_limbs.
674 *
Tom Cosgrove0a0dded2022-12-06 14:37:18 +0000675 * \param[out] X The destination MPI, as a little endian array of length
676 * \p AN_limbs.
677 * \param[in] A The base MPI, as a little endian array of length \p AN_limbs.
678 * Must be in Montgomery form.
679 * \param[in] N The modulus, as a little endian array of length \p AN_limbs.
680 * \param AN_limbs The number of limbs in \p X, \p A, \p N, \p RR.
681 * \param[in] E The exponent, as a little endian array of length \p E_limbs.
682 * \param E_limbs The number of limbs in \p E.
683 * \param[in] RR The precomputed residue of 2^{2*biL} modulo N, as a little
684 * endian array of length \p AN_limbs.
685 * \param[in,out] T Temporary storage of at least the number of limbs returned
686 * by `mbedtls_mpi_core_exp_mod_working_limbs()`.
687 * Its initial content is unused and its final content is
688 * indeterminate.
689 * It must not alias or otherwise overlap any of the other
690 * parameters.
691 * It is up to the caller to zeroize \p T when it is no
692 * longer needed, and before freeing it if it was dynamically
693 * allocated.
694 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100695void mbedtls_mpi_core_exp_mod(mbedtls_mpi_uint *X,
696 const mbedtls_mpi_uint *A,
697 const mbedtls_mpi_uint *N, size_t AN_limbs,
698 const mbedtls_mpi_uint *E, size_t E_limbs,
699 const mbedtls_mpi_uint *RR,
700 mbedtls_mpi_uint *T);
Janos Follathb6673f02022-09-30 14:13:14 +0100701
Hanno Beckerd9b23482022-08-25 08:25:19 +0100702/**
703 * \brief Subtract unsigned integer from known-size large unsigned integers.
704 * Return the borrow.
705 *
Tom Cosgrovef7ff4c92022-08-25 08:39:07 +0100706 * \param[out] X The result of the subtraction.
707 * \param[in] A The left operand.
708 * \param b The unsigned scalar to subtract.
709 * \param limbs Number of limbs of \p X and \p A.
Hanno Beckerd9b23482022-08-25 08:25:19 +0100710 *
Tom Cosgrovef7ff4c92022-08-25 08:39:07 +0100711 * \return 1 if `A < b`.
712 * 0 if `A >= b`.
Hanno Beckerd9b23482022-08-25 08:25:19 +0100713 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100714mbedtls_mpi_uint mbedtls_mpi_core_sub_int(mbedtls_mpi_uint *X,
715 const mbedtls_mpi_uint *A,
716 mbedtls_mpi_uint b,
717 size_t limbs);
Hanno Beckerd9b23482022-08-25 08:25:19 +0100718
Tom Cosgrove30f3b4d2022-12-12 16:54:57 +0000719/**
720 * \brief Determine if a given MPI has the value \c 0 in constant time with
721 * respect to the value (but not with respect to the number of limbs).
722 *
723 * \param[in] A The MPI to test.
724 * \param limbs Number of limbs in \p A.
725 *
Janos Follathaec1a862024-02-21 11:24:20 +0000726 * \return MBEDTLS_CT_FALSE if `A == 0`
727 * MBEDTLS_CT_TRUE if `A != 0`.
Tom Cosgrove30f3b4d2022-12-12 16:54:57 +0000728 */
Janos Follathadb9d2d2024-03-11 10:03:05 +0000729mbedtls_ct_condition_t mbedtls_mpi_core_check_zero_ct(const mbedtls_mpi_uint *A,
Janos Follathaec1a862024-02-21 11:24:20 +0000730 size_t limbs);
Tom Cosgrove30f3b4d2022-12-12 16:54:57 +0000731
Tom Cosgrove28ff92c2022-12-12 17:06:27 +0000732/**
733 * \brief Returns the number of limbs of working memory required for
734 * a call to `mbedtls_mpi_core_montmul()`.
735 *
736 * \param AN_limbs The number of limbs in the input `A` and the modulus `N`
737 * (they must be the same size) that will be given to
738 * `mbedtls_mpi_core_montmul()` or one of the other functions
739 * that specifies this as the amount of working memory needed.
740 *
741 * \return The number of limbs of working memory required by
742 * `mbedtls_mpi_core_montmul()` (or other similar function).
743 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100744static inline size_t mbedtls_mpi_core_montmul_working_limbs(size_t AN_limbs)
Tom Cosgrove28ff92c2022-12-12 17:06:27 +0000745{
Gilles Peskine449bd832023-01-11 14:50:10 +0100746 return 2 * AN_limbs + 1;
Tom Cosgrove28ff92c2022-12-12 17:06:27 +0000747}
748
Tom Cosgrove786848b2022-12-13 10:45:19 +0000749/** Convert an MPI into Montgomery form.
750 *
751 * \p X may be aliased to \p A, but may not otherwise overlap it.
752 *
Tom Cosgrove5c8505f2023-03-07 11:39:52 +0000753 * \p X may not alias \p N (it is in canonical form, so must be strictly less
Tom Cosgrove786848b2022-12-13 10:45:19 +0000754 * than \p N). Nor may it alias or overlap \p rr (this is unlikely to be
755 * required in practice.)
756 *
757 * This function is a thin wrapper around `mbedtls_mpi_core_montmul()` that is
758 * an alternative to calling `mbedtls_mpi_mod_raw_to_mont_rep()` when we
759 * don't want to allocate memory.
760 *
761 * \param[out] X The result of the conversion.
762 * Must have the same number of limbs as \p A.
763 * \param[in] A The MPI to convert into Montgomery form.
764 * Must have the same number of limbs as the modulus.
765 * \param[in] N The address of the modulus, which gives the size of
Tom Cosgrovef7237542022-12-16 16:10:36 +0000766 * the base `R` = 2^(biL*N->limbs).
Tom Cosgrove786848b2022-12-13 10:45:19 +0000767 * \param[in] AN_limbs The number of limbs in \p X, \p A, \p N and \p rr.
768 * \param mm The Montgomery constant for \p N: -N^-1 mod 2^biL.
Tom Cosgroveb38c2ed2022-12-14 13:11:46 +0000769 * This can be determined by calling
Tom Cosgrove786848b2022-12-13 10:45:19 +0000770 * `mbedtls_mpi_core_montmul_init()`.
771 * \param[in] rr The residue for `2^{2*n*biL} mod N`.
772 * \param[in,out] T Temporary storage of size at least
773 * `mbedtls_mpi_core_montmul_working_limbs(AN_limbs)`
774 * limbs.
775 * Its initial content is unused and
776 * its final content is indeterminate.
777 * It must not alias or otherwise overlap any of the
778 * other parameters.
779 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100780void mbedtls_mpi_core_to_mont_rep(mbedtls_mpi_uint *X,
781 const mbedtls_mpi_uint *A,
782 const mbedtls_mpi_uint *N,
783 size_t AN_limbs,
784 mbedtls_mpi_uint mm,
785 const mbedtls_mpi_uint *rr,
786 mbedtls_mpi_uint *T);
Tom Cosgrove786848b2022-12-13 10:45:19 +0000787
788/** Convert an MPI from Montgomery form.
789 *
790 * \p X may be aliased to \p A, but may not otherwise overlap it.
791 *
Tom Cosgrove5c8505f2023-03-07 11:39:52 +0000792 * \p X may not alias \p N (it is in canonical form, so must be strictly less
Tom Cosgrove786848b2022-12-13 10:45:19 +0000793 * than \p N).
794 *
795 * This function is a thin wrapper around `mbedtls_mpi_core_montmul()` that is
796 * an alternative to calling `mbedtls_mpi_mod_raw_from_mont_rep()` when we
797 * don't want to allocate memory.
798 *
799 * \param[out] X The result of the conversion.
800 * Must have the same number of limbs as \p A.
801 * \param[in] A The MPI to convert from Montgomery form.
802 * Must have the same number of limbs as the modulus.
803 * \param[in] N The address of the modulus, which gives the size of
Tom Cosgrovef7237542022-12-16 16:10:36 +0000804 * the base `R` = 2^(biL*N->limbs).
Tom Cosgrove786848b2022-12-13 10:45:19 +0000805 * \param[in] AN_limbs The number of limbs in \p X, \p A and \p N.
806 * \param mm The Montgomery constant for \p N: -N^-1 mod 2^biL.
Tom Cosgroveb38c2ed2022-12-14 13:11:46 +0000807 * This can be determined by calling
Tom Cosgrove786848b2022-12-13 10:45:19 +0000808 * `mbedtls_mpi_core_montmul_init()`.
809 * \param[in,out] T Temporary storage of size at least
810 * `mbedtls_mpi_core_montmul_working_limbs(AN_limbs)`
811 * limbs.
812 * Its initial content is unused and
813 * its final content is indeterminate.
814 * It must not alias or otherwise overlap any of the
815 * other parameters.
816 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100817void mbedtls_mpi_core_from_mont_rep(mbedtls_mpi_uint *X,
818 const mbedtls_mpi_uint *A,
819 const mbedtls_mpi_uint *N,
820 size_t AN_limbs,
821 mbedtls_mpi_uint mm,
822 mbedtls_mpi_uint *T);
Tom Cosgrove786848b2022-12-13 10:45:19 +0000823
Janos Follath8786dd72024-08-20 10:21:54 +0100824/*
825 * Can't define thread local variables with our abstraction layer: do nothing if threading is on.
826 */
827#if defined(MBEDTLS_TEST_HOOKS) && !defined(MBEDTLS_THREADING_C)
Janos Follatha1126912024-08-20 09:56:16 +0100828extern int mbedtls_mpi_optionally_safe_codepath;
Janos Follathe0842aa2024-08-13 08:40:31 +0100829
Janos Follatha1126912024-08-20 09:56:16 +0100830static inline void mbedtls_mpi_optionally_safe_codepath_reset(void)
Janos Follathe0842aa2024-08-13 08:40:31 +0100831{
Janos Follath42f72b32024-08-22 08:25:33 +0100832 mbedtls_mpi_optionally_safe_codepath = MBEDTLS_MPI_IS_TEST;
Janos Follathe0842aa2024-08-13 08:40:31 +0100833}
834#endif
835
Gabor Mezeib9030702022-07-18 23:09:45 +0200836#endif /* MBEDTLS_BIGNUM_CORE_H */