blob: ba2978eb33b42fcedf0366b0afe43349a85b1da5 [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
Janos Follath2f8ad592024-08-22 17:13:25 +0100110 * other than the two below is passed, default to the safe path.)
111 *
112 * The value of MBEDTLS_MPI_IS_PUBLIC is chosen in a way that is unlikely to happen by accident, but
113 * which can be used as an immediate value in a Thumb2 comparison (for code size). */
Janos Follath0c292b22024-08-12 19:55:02 +0100114#define MBEDTLS_MPI_IS_PUBLIC 0x2a2a2a2a
Janos Follath90b42712024-08-12 19:32:45 +0100115#define MBEDTLS_MPI_IS_SECRET 0
Janos Follath42f72b32024-08-22 08:25:33 +0100116#if defined(MBEDTLS_TEST_HOOKS) && !defined(MBEDTLS_THREADING_C)
117// Default value for testing that is neither MBEDTLS_MPI_IS_PUBLIC nor MBEDTLS_MPI_IS_SECRET
118#define MBEDTLS_MPI_IS_TEST 1
119#endif
Janos Follath90b42712024-08-12 19:32:45 +0100120
Gabor Mezei37b06362022-08-02 17:22:18 +0200121/** Count leading zero bits in a given integer.
122 *
Dave Rodgman0f16d562023-04-24 12:53:29 +0100123 * \warning The result is undefined if \p a == 0
124 *
Janos Follathb7a88ec2022-08-19 12:24:40 +0100125 * \param a Integer to count leading zero bits.
Gabor Mezei37b06362022-08-02 17:22:18 +0200126 *
Dave Rodgman0f16d562023-04-24 12:53:29 +0100127 * \return The number of leading zero bits in \p a, if \p a != 0.
128 * If \p a == 0, the result is undefined.
Gabor Mezei37b06362022-08-02 17:22:18 +0200129 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100130size_t mbedtls_mpi_core_clz(mbedtls_mpi_uint a);
Janos Follath4670f882022-07-21 18:25:42 +0100131
Janos Follatha95f2042022-08-19 12:09:17 +0100132/** Return the minimum number of bits required to represent the value held
Janos Follath63184682022-08-11 17:42:59 +0100133 * in the MPI.
134 *
Janos Follathb7a88ec2022-08-19 12:24:40 +0100135 * \note This function returns 0 if all the limbs of \p A are 0.
Gabor Mezei37b06362022-08-02 17:22:18 +0200136 *
Janos Follathb7a88ec2022-08-19 12:24:40 +0100137 * \param[in] A The address of the MPI.
Janos Follathc4596412022-08-22 10:01:27 +0100138 * \param A_limbs The number of limbs of \p A.
Gabor Mezei37b06362022-08-02 17:22:18 +0200139 *
Janos Follathb7a88ec2022-08-19 12:24:40 +0100140 * \return The number of bits in \p A.
Gabor Mezei37b06362022-08-02 17:22:18 +0200141 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100142size_t mbedtls_mpi_core_bitlen(const mbedtls_mpi_uint *A, size_t A_limbs);
Janos Follath4670f882022-07-21 18:25:42 +0100143
Gabor Mezei37b06362022-08-02 17:22:18 +0200144/** Convert a big-endian byte array aligned to the size of mbedtls_mpi_uint
145 * into the storage form used by mbedtls_mpi.
146 *
Janos Follathb7a88ec2022-08-19 12:24:40 +0100147 * \param[in,out] A The address of the MPI.
Janos Follathc4596412022-08-22 10:01:27 +0100148 * \param A_limbs The number of limbs of \p A.
Gabor Mezei37b06362022-08-02 17:22:18 +0200149 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100150void mbedtls_mpi_core_bigendian_to_host(mbedtls_mpi_uint *A,
151 size_t A_limbs);
Janos Follath4670f882022-07-21 18:25:42 +0100152
Gilles Peskine6f949ea2022-09-20 18:38:35 +0200153/** \brief Compare a machine integer with an MPI.
154 *
155 * This function operates in constant time with respect
156 * to the values of \p min and \p A.
157 *
158 * \param min A machine integer.
159 * \param[in] A An MPI.
160 * \param A_limbs The number of limbs of \p A.
161 * This must be at least 1.
162 *
Dave Rodgmanfd7fab42023-05-17 14:00:39 +0100163 * \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 +0200164 */
Dave Rodgmanfd7fab42023-05-17 14:00:39 +0100165mbedtls_ct_condition_t mbedtls_mpi_core_uint_le_mpi(mbedtls_mpi_uint min,
166 const mbedtls_mpi_uint *A,
167 size_t A_limbs);
Gilles Peskine6f949ea2022-09-20 18:38:35 +0200168
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200169/**
Dave Rodgman7d4f0192023-05-09 14:01:05 +0100170 * \brief Check if one unsigned MPI is less than another in constant
171 * time.
172 *
173 * \param A The left-hand MPI. This must point to an array of limbs
174 * with the same allocated length as \p B.
175 * \param B The right-hand MPI. This must point to an array of limbs
176 * with the same allocated length as \p A.
177 * \param limbs The number of limbs in \p A and \p B.
178 * This must not be 0.
179 *
Dave Rodgman8ac9a1d2023-05-17 15:16:22 +0100180 * \return MBEDTLS_CT_TRUE if \p A is less than \p B.
181 * MBEDTLS_CT_FALSE if \p A is greater than or equal to \p B.
Dave Rodgman7d4f0192023-05-09 14:01:05 +0100182 */
Dave Rodgman8ac9a1d2023-05-17 15:16:22 +0100183mbedtls_ct_condition_t mbedtls_mpi_core_lt_ct(const mbedtls_mpi_uint *A,
184 const mbedtls_mpi_uint *B,
185 size_t limbs);
186
Dave Rodgman7d4f0192023-05-09 14:01:05 +0100187/**
Gabor Mezei4086de62022-10-14 16:29:42 +0200188 * \brief Perform a safe conditional copy of an MPI which doesn't reveal
189 * whether assignment was done or not.
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200190 *
Gabor Mezei4086de62022-10-14 16:29:42 +0200191 * \param[out] X The address of the destination MPI.
192 * This must be initialized. Must have enough limbs to
193 * store the full value of \p A.
194 * \param[in] A The address of the source MPI. This must be initialized.
Gabor Mezei1c628d52022-09-27 12:13:51 +0200195 * \param limbs The number of limbs of \p A.
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200196 * \param assign The condition deciding whether to perform the
Dave Rodgmanfb1b8512023-07-31 12:27:05 +0100197 * assignment or not. Callers will need to use
198 * the constant time interface (e.g. `mbedtls_ct_bool()`)
199 * to construct this argument.
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200200 *
201 * \note This function avoids leaking any information about whether
202 * the assignment was done or not.
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200203 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100204void mbedtls_mpi_core_cond_assign(mbedtls_mpi_uint *X,
205 const mbedtls_mpi_uint *A,
206 size_t limbs,
Dave Rodgmancd2e38b2023-05-17 13:31:55 +0100207 mbedtls_ct_condition_t assign);
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200208
209/**
Gabor Mezei4086de62022-10-14 16:29:42 +0200210 * \brief Perform a safe conditional swap of two MPIs which doesn't reveal
211 * whether the swap was done or not.
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200212 *
Gabor Mezei86dfe382022-09-30 14:03:04 +0200213 * \param[in,out] X The address of the first MPI.
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200214 * This must be initialized.
Gabor Mezei86dfe382022-09-30 14:03:04 +0200215 * \param[in,out] Y The address of the second MPI.
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200216 * This must be initialized.
Gabor Mezeie5b85852022-09-30 13:54:02 +0200217 * \param limbs The number of limbs of \p X and \p Y.
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200218 * \param swap The condition deciding whether to perform
Dave Rodgmancd2e38b2023-05-17 13:31:55 +0100219 * the swap or not.
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200220 *
221 * \note This function avoids leaking any information about whether
222 * the swap was done or not.
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200223 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100224void mbedtls_mpi_core_cond_swap(mbedtls_mpi_uint *X,
225 mbedtls_mpi_uint *Y,
226 size_t limbs,
Dave Rodgmancd2e38b2023-05-17 13:31:55 +0100227 mbedtls_ct_condition_t swap);
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200228
Janos Follathaf3f39c2022-08-22 09:06:32 +0100229/** Import X from unsigned binary data, little-endian.
Gabor Mezei37b06362022-08-02 17:22:18 +0200230 *
Janos Follath63184682022-08-11 17:42:59 +0100231 * The MPI needs to have enough limbs to store the full value (including any
232 * most significant zero bytes in the input).
Gabor Mezei37b06362022-08-02 17:22:18 +0200233 *
Janos Follathb7a88ec2022-08-19 12:24:40 +0100234 * \param[out] X The address of the MPI.
235 * \param X_limbs The number of limbs of \p X.
236 * \param[in] input The input buffer to import from.
237 * \param input_length The length bytes of \p input.
Gabor Mezei37b06362022-08-02 17:22:18 +0200238 *
239 * \return \c 0 if successful.
240 * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p X isn't
Janos Follathb7a88ec2022-08-19 12:24:40 +0100241 * large enough to hold the value in \p input.
Gabor Mezei37b06362022-08-02 17:22:18 +0200242 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100243int mbedtls_mpi_core_read_le(mbedtls_mpi_uint *X,
244 size_t X_limbs,
245 const unsigned char *input,
246 size_t input_length);
Gabor Mezeib9030702022-07-18 23:09:45 +0200247
Janos Follathaf3f39c2022-08-22 09:06:32 +0100248/** Import X from unsigned binary data, big-endian.
Gabor Mezei37b06362022-08-02 17:22:18 +0200249 *
Janos Follath63184682022-08-11 17:42:59 +0100250 * The MPI needs to have enough limbs to store the full value (including any
251 * most significant zero bytes in the input).
Gabor Mezei37b06362022-08-02 17:22:18 +0200252 *
Janos Follathb7a88ec2022-08-19 12:24:40 +0100253 * \param[out] X The address of the MPI.
Tom Cosgrove8a1f7842023-02-01 08:43:54 +0000254 * May only be #NULL if \p X_limbs is 0 and \p input_length
Janos Follathb7a88ec2022-08-19 12:24:40 +0100255 * is 0.
256 * \param X_limbs The number of limbs of \p X.
257 * \param[in] input The input buffer to import from.
258 * May only be #NULL if \p input_length is 0.
259 * \param input_length The length in bytes of \p input.
Gabor Mezei37b06362022-08-02 17:22:18 +0200260 *
261 * \return \c 0 if successful.
262 * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p X isn't
Janos Follathb7a88ec2022-08-19 12:24:40 +0100263 * large enough to hold the value in \p input.
Gabor Mezei37b06362022-08-02 17:22:18 +0200264 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100265int mbedtls_mpi_core_read_be(mbedtls_mpi_uint *X,
266 size_t X_limbs,
267 const unsigned char *input,
268 size_t input_length);
Gabor Mezeib9030702022-07-18 23:09:45 +0200269
Janos Follathaf3f39c2022-08-22 09:06:32 +0100270/** Export A into unsigned binary data, little-endian.
Gabor Mezei37b06362022-08-02 17:22:18 +0200271 *
Janos Follathb7a88ec2022-08-19 12:24:40 +0100272 * \note If \p output is shorter than \p A the export is still successful if the
273 * value held in \p A fits in the buffer (that is, if enough of the most
274 * significant bytes of \p A are 0).
Janos Follath63184682022-08-11 17:42:59 +0100275 *
Janos Follathb7a88ec2022-08-19 12:24:40 +0100276 * \param[in] A The address of the MPI.
277 * \param A_limbs The number of limbs of \p A.
278 * \param[out] output The output buffer to export to.
279 * \param output_length The length in bytes of \p output.
Gabor Mezei37b06362022-08-02 17:22:18 +0200280 *
281 * \return \c 0 if successful.
Janos Follathb7a88ec2022-08-19 12:24:40 +0100282 * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p output isn't
283 * large enough to hold the value of \p A.
Gabor Mezei37b06362022-08-02 17:22:18 +0200284 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100285int mbedtls_mpi_core_write_le(const mbedtls_mpi_uint *A,
286 size_t A_limbs,
287 unsigned char *output,
288 size_t output_length);
Gabor Mezeib9030702022-07-18 23:09:45 +0200289
Janos Follathaf3f39c2022-08-22 09:06:32 +0100290/** Export A into unsigned binary data, big-endian.
Gabor Mezei37b06362022-08-02 17:22:18 +0200291 *
Janos Follathb7a88ec2022-08-19 12:24:40 +0100292 * \note If \p output is shorter than \p A the export is still successful if the
293 * value held in \p A fits in the buffer (that is, if enough of the most
294 * significant bytes of \p A are 0).
Janos Follath63184682022-08-11 17:42:59 +0100295 *
Janos Follathb7a88ec2022-08-19 12:24:40 +0100296 * \param[in] A The address of the MPI.
297 * \param A_limbs The number of limbs of \p A.
298 * \param[out] output The output buffer to export to.
299 * \param output_length The length in bytes of \p output.
Gabor Mezei37b06362022-08-02 17:22:18 +0200300 *
301 * \return \c 0 if successful.
Janos Follathb7a88ec2022-08-19 12:24:40 +0100302 * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p output isn't
303 * large enough to hold the value of \p A.
Gabor Mezei37b06362022-08-02 17:22:18 +0200304 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100305int mbedtls_mpi_core_write_be(const mbedtls_mpi_uint *A,
306 size_t A_limbs,
307 unsigned char *output,
308 size_t output_length);
Gabor Mezeib9030702022-07-18 23:09:45 +0200309
Minos Galanakisec09e252023-04-20 14:22:16 +0100310/** \brief Shift an MPI in-place right by a number of bits.
Gilles Peskine66414202022-09-21 15:36:16 +0200311 *
312 * Shifting by more bits than there are bit positions
313 * in \p X is valid and results in setting \p X to 0.
314 *
315 * This function's execution time depends on the value
316 * of \p count (and of course \p limbs).
317 *
318 * \param[in,out] X The number to shift.
319 * \param limbs The number of limbs of \p X. This must be at least 1.
320 * \param count The number of bits to shift by.
321 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100322void mbedtls_mpi_core_shift_r(mbedtls_mpi_uint *X, size_t limbs,
323 size_t count);
Gilles Peskine66414202022-09-21 15:36:16 +0200324
Tom Cosgrove90c426b2022-08-23 16:15:19 +0100325/**
Minos Galanakisec09e252023-04-20 14:22:16 +0100326 * \brief Shift an MPI in-place left by a number of bits.
Minos Galanakisad808dd2023-04-20 12:18:41 +0100327 *
Minos Galanakisec09e252023-04-20 14:22:16 +0100328 * Shifting by more bits than there are bit positions
Minos Galanakisb8944032023-04-28 14:09:44 +0100329 * in \p X will produce an unspecified result.
Minos Galanakisad808dd2023-04-20 12:18:41 +0100330 *
Minos Galanakisec09e252023-04-20 14:22:16 +0100331 * This function's execution time depends on the value
332 * of \p count (and of course \p limbs).
333 * \param[in,out] X The number to shift.
334 * \param limbs The number of limbs of \p X. This must be at least 1.
335 * \param count The number of bits to shift by.
Minos Galanakisad808dd2023-04-20 12:18:41 +0100336 */
Minos Galanakisec09e252023-04-20 14:22:16 +0100337void mbedtls_mpi_core_shift_l(mbedtls_mpi_uint *X, size_t limbs,
338 size_t count);
Minos Galanakisad808dd2023-04-20 12:18:41 +0100339
340/**
Tom Cosgroveaf7d44b2022-08-24 14:05:26 +0100341 * \brief Add two fixed-size large unsigned integers, returning the carry.
Hanno Beckerc9887132022-08-24 12:54:36 +0100342 *
Tom Cosgroveaf7d44b2022-08-24 14:05:26 +0100343 * Calculates `A + B` where `A` and `B` have the same size.
344 *
Tom Cosgrove82f13102022-10-25 12:46:03 +0100345 * This function operates modulo `2^(biL*limbs)` and returns the carry
Hanno Beckerc9887132022-08-24 12:54:36 +0100346 * (1 if there was a wraparound, and 0 otherwise).
347 *
Tom Cosgroveaf7d44b2022-08-24 14:05:26 +0100348 * \p X may be aliased to \p A or \p B.
Hanno Beckerc9887132022-08-24 12:54:36 +0100349 *
Tom Cosgroveaf7d44b2022-08-24 14:05:26 +0100350 * \param[out] X The result of the addition.
351 * \param[in] A Little-endian presentation of the left operand.
352 * \param[in] B Little-endian presentation of the right operand.
353 * \param limbs Number of limbs of \p X, \p A and \p B.
Hanno Beckerc9887132022-08-24 12:54:36 +0100354 *
Tom Cosgroveaf7d44b2022-08-24 14:05:26 +0100355 * \return 1 if `A + B >= 2^(biL*limbs)`, 0 otherwise.
Hanno Beckerc9887132022-08-24 12:54:36 +0100356 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100357mbedtls_mpi_uint mbedtls_mpi_core_add(mbedtls_mpi_uint *X,
358 const mbedtls_mpi_uint *A,
359 const mbedtls_mpi_uint *B,
360 size_t limbs);
Hanno Beckerc9887132022-08-24 12:54:36 +0100361
Tom Cosgrove90c426b2022-08-23 16:15:19 +0100362/**
Tom Cosgrove5c0e8102022-09-15 15:46:10 +0100363 * \brief Conditional addition of two fixed-size large unsigned integers,
Tom Cosgroved932de82022-08-25 16:43:43 +0100364 * returning the carry.
Hanno Becker71f4b0d2022-08-23 12:09:35 +0100365 *
366 * Functionally equivalent to
367 *
368 * ```
369 * if( cond )
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100370 * X += A;
Hanno Becker71f4b0d2022-08-23 12:09:35 +0100371 * return carry;
372 * ```
373 *
Tom Cosgrove47828232022-09-20 13:51:50 +0100374 * This function operates modulo `2^(biL*limbs)`.
375 *
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100376 * \param[in,out] X The pointer to the (little-endian) array
Tom Cosgrove72594632022-08-24 11:51:58 +0100377 * representing the bignum to accumulate onto.
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100378 * \param[in] A The pointer to the (little-endian) array
Tom Cosgrove72594632022-08-24 11:51:58 +0100379 * representing the bignum to conditionally add
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100380 * to \p X. This may be aliased to \p X but may not
Tom Cosgroveed43c6c2022-08-31 11:35:00 +0100381 * overlap otherwise.
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100382 * \param limbs Number of limbs of \p X and \p A.
Tom Cosgrove72594632022-08-24 11:51:58 +0100383 * \param cond Condition bit dictating whether addition should
384 * happen or not. This must be \c 0 or \c 1.
Hanno Becker71f4b0d2022-08-23 12:09:35 +0100385 *
Tom Cosgroveecbb1242022-08-25 10:13:44 +0100386 * \warning If \p cond is neither 0 nor 1, the result of this function
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100387 * is unspecified, and the resulting value in \p X might be
388 * neither its original value nor \p X + \p A.
Tom Cosgrove72594632022-08-24 11:51:58 +0100389 *
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100390 * \return 1 if `X + cond * A >= 2^(biL*limbs)`, 0 otherwise.
Hanno Becker71f4b0d2022-08-23 12:09:35 +0100391 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100392mbedtls_mpi_uint mbedtls_mpi_core_add_if(mbedtls_mpi_uint *X,
393 const mbedtls_mpi_uint *A,
394 size_t limbs,
395 unsigned cond);
Hanno Becker71f4b0d2022-08-23 12:09:35 +0100396
Tom Cosgroveb4964862022-08-30 11:57:22 +0100397/**
Tom Cosgrove5c0e8102022-09-15 15:46:10 +0100398 * \brief Subtract two fixed-size large unsigned integers, returning the borrow.
Tom Cosgroveb4964862022-08-30 11:57:22 +0100399 *
Tom Cosgrove5dd97e62022-08-30 14:31:49 +0100400 * Calculate `A - B` where \p A and \p B have the same size.
Tom Cosgrove630110a2022-08-31 17:09:29 +0100401 * This function operates modulo `2^(biL*limbs)` and returns the carry
Tom Cosgroveb4964862022-08-30 11:57:22 +0100402 * (1 if there was a wraparound, i.e. if `A < B`, and 0 otherwise).
403 *
Tom Cosgrove5dd97e62022-08-30 14:31:49 +0100404 * \p X may be aliased to \p A or \p B, or even both, but may not overlap
405 * either otherwise.
Tom Cosgroveb4964862022-08-30 11:57:22 +0100406 *
407 * \param[out] X The result of the subtraction.
408 * \param[in] A Little-endian presentation of left operand.
409 * \param[in] B Little-endian presentation of right operand.
410 * \param limbs Number of limbs of \p X, \p A and \p B.
411 *
412 * \return 1 if `A < B`.
413 * 0 if `A >= B`.
414 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100415mbedtls_mpi_uint mbedtls_mpi_core_sub(mbedtls_mpi_uint *X,
416 const mbedtls_mpi_uint *A,
417 const mbedtls_mpi_uint *B,
418 size_t limbs);
Tom Cosgroveb4964862022-08-30 11:57:22 +0100419
420/**
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100421 * \brief Perform a fixed-size multiply accumulate operation: X += b * A
Tom Cosgroveb4964862022-08-30 11:57:22 +0100422 *
Tom Cosgroveb0b77e12022-09-20 13:33:40 +0100423 * \p X may be aliased to \p A (when \p X_limbs == \p A_limbs), but may not
424 * otherwise overlap.
425 *
Tom Cosgrove47828232022-09-20 13:51:50 +0100426 * This function operates modulo `2^(biL*X_limbs)`.
427 *
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100428 * \param[in,out] X The pointer to the (little-endian) array
Tom Cosgroveb4964862022-08-30 11:57:22 +0100429 * representing the bignum to accumulate onto.
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100430 * \param X_limbs The number of limbs of \p X. This must be
431 * at least \p A_limbs.
432 * \param[in] A The pointer to the (little-endian) array
Tom Cosgroveb4964862022-08-30 11:57:22 +0100433 * representing the bignum to multiply with.
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100434 * This may be aliased to \p X but may not overlap
Tom Cosgroveed43c6c2022-08-31 11:35:00 +0100435 * otherwise.
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100436 * \param A_limbs The number of limbs of \p A.
437 * \param b X scalar to multiply with.
Tom Cosgroveb4964862022-08-30 11:57:22 +0100438 *
439 * \return The carry at the end of the operation.
440 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100441mbedtls_mpi_uint mbedtls_mpi_core_mla(mbedtls_mpi_uint *X, size_t X_limbs,
442 const mbedtls_mpi_uint *A, size_t A_limbs,
443 mbedtls_mpi_uint b);
Tom Cosgroveb4964862022-08-30 11:57:22 +0100444
Hanno Becker4ae890b2022-08-24 16:17:52 +0100445/**
446 * \brief Perform a known-size multiplication
447 *
Gabor Mezeid6260512023-04-03 17:32:55 +0200448 * \p X may not be aliased to any of the inputs for this function.
Gabor Mezei6f182c32023-03-31 16:09:28 +0200449 * \p A may be aliased to \p B.
450 *
Tom Cosgrove6af26f32022-08-24 16:40:55 +0100451 * \param[out] X The pointer to the (little-endian) array to receive
452 * the product of \p A_limbs and \p B_limbs.
453 * This must be of length \p A_limbs + \p B_limbs.
454 * \param[in] A The pointer to the (little-endian) array
455 * representing the first factor.
456 * \param A_limbs The number of limbs in \p A.
457 * \param[in] B The pointer to the (little-endian) array
458 * representing the second factor.
459 * \param B_limbs The number of limbs in \p B.
Hanno Becker4ae890b2022-08-24 16:17:52 +0100460 */
Tom Cosgrove6af26f32022-08-24 16:40:55 +0100461void mbedtls_mpi_core_mul(mbedtls_mpi_uint *X,
462 const mbedtls_mpi_uint *A, size_t A_limbs,
463 const mbedtls_mpi_uint *B, size_t B_limbs);
Hanno Becker4ae890b2022-08-24 16:17:52 +0100464
Tom Cosgroveb4964862022-08-30 11:57:22 +0100465/**
466 * \brief Calculate initialisation value for fast Montgomery modular
467 * multiplication
468 *
469 * \param[in] N Little-endian presentation of the modulus. This must have
470 * at least one limb.
471 *
472 * \return The initialisation value for fast Montgomery modular multiplication
473 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100474mbedtls_mpi_uint mbedtls_mpi_core_montmul_init(const mbedtls_mpi_uint *N);
Tom Cosgroveb4964862022-08-30 11:57:22 +0100475
476/**
Tom Cosgrove4386ead2022-09-29 14:40:21 +0100477 * \brief Montgomery multiplication: X = A * B * R^-1 mod N (HAC 14.36)
478 *
479 * \p A and \p B must be in canonical form. That is, < \p N.
Tom Cosgroveb4964862022-08-30 11:57:22 +0100480 *
Tom Cosgroveea45c1d2022-09-20 13:17:51 +0100481 * \p X may be aliased to \p A or \p N, or even \p B (if \p AN_limbs ==
482 * \p B_limbs) but may not overlap any parameters otherwise.
483 *
Tom Cosgrove4386ead2022-09-29 14:40:21 +0100484 * \p A and \p B may alias each other, if \p AN_limbs == \p B_limbs. They may
485 * not alias \p N (since they must be in canonical form, they cannot == \p N).
Tom Cosgroveea45c1d2022-09-20 13:17:51 +0100486 *
Tom Cosgroveb4964862022-08-30 11:57:22 +0100487 * \param[out] X The destination MPI, as a little-endian array of
488 * length \p AN_limbs.
489 * On successful completion, X contains the result of
Tom Cosgrove630110a2022-08-31 17:09:29 +0100490 * the multiplication `A * B * R^-1` mod N where
491 * `R = 2^(biL*AN_limbs)`.
Tom Cosgroveb4964862022-08-30 11:57:22 +0100492 * \param[in] A Little-endian presentation of first operand.
Tom Cosgrove5dd97e62022-08-30 14:31:49 +0100493 * Must have the same number of limbs as \p N.
Tom Cosgroveb4964862022-08-30 11:57:22 +0100494 * \param[in] B Little-endian presentation of second operand.
495 * \param[in] B_limbs The number of limbs in \p B.
Tom Cosgrove5dd97e62022-08-30 14:31:49 +0100496 * Must be <= \p AN_limbs.
Tom Cosgroveb4964862022-08-30 11:57:22 +0100497 * \param[in] N Little-endian presentation of the modulus.
Tom Cosgrove5dd97e62022-08-30 14:31:49 +0100498 * This must be odd, and have exactly the same number
499 * of limbs as \p A.
Tom Cosgrove6da3a3b2022-09-29 17:20:18 +0100500 * It may alias \p X, but must not alias or otherwise
501 * overlap any of the other parameters.
Tom Cosgrove5dd97e62022-08-30 14:31:49 +0100502 * \param[in] AN_limbs The number of limbs in \p X, \p A and \p N.
Tom Cosgrove630110a2022-08-31 17:09:29 +0100503 * \param mm The Montgomery constant for \p N: -N^-1 mod 2^biL.
Tom Cosgroveb7438d12022-09-15 15:05:59 +0100504 * This can be calculated by `mbedtls_mpi_core_montmul_init()`.
Tom Cosgroveb4964862022-08-30 11:57:22 +0100505 * \param[in,out] T Temporary storage of size at least 2*AN_limbs+1 limbs.
506 * Its initial content is unused and
507 * its final content is indeterminate.
Tom Cosgrove4386ead2022-09-29 14:40:21 +0100508 * It must not alias or otherwise overlap any of the
509 * other parameters.
Tom Cosgroveb4964862022-08-30 11:57:22 +0100510 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100511void mbedtls_mpi_core_montmul(mbedtls_mpi_uint *X,
512 const mbedtls_mpi_uint *A,
513 const mbedtls_mpi_uint *B, size_t B_limbs,
514 const mbedtls_mpi_uint *N, size_t AN_limbs,
515 mbedtls_mpi_uint mm, mbedtls_mpi_uint *T);
Tom Cosgroveb4964862022-08-30 11:57:22 +0100516
Hanno Beckerec440f22022-08-11 17:29:32 +0100517/**
Minos Galanakisb85506e2022-10-20 09:51:53 +0100518 * \brief Calculate the square of the Montgomery constant. (Needed
519 * for conversion and operations in Montgomery form.)
Hanno Beckerec440f22022-08-11 17:29:32 +0100520 *
521 * \param[out] X A pointer to the result of the calculation of
Minos Galanakisb85506e2022-10-20 09:51:53 +0100522 * the square of the Montgomery constant:
523 * 2^{2*n*biL} mod N.
Hanno Beckerec440f22022-08-11 17:29:32 +0100524 * \param[in] N Little-endian presentation of the modulus, which must be odd.
525 *
526 * \return 0 if successful.
527 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if there is not enough space
528 * to store the value of Montgomery constant squared.
529 * \return #MBEDTLS_ERR_MPI_DIVISION_BY_ZERO if \p N modulus is zero.
530 * \return #MBEDTLS_ERR_MPI_NEGATIVE_VALUE if \p N modulus is negative.
Hanno Beckerec440f22022-08-11 17:29:32 +0100531 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100532int mbedtls_mpi_core_get_mont_r2_unsafe(mbedtls_mpi *X,
533 const mbedtls_mpi *N);
Hanno Beckerec440f22022-08-11 17:29:32 +0100534
Janos Follath59cbd1b2022-10-28 18:13:43 +0100535#if defined(MBEDTLS_TEST_HOOKS)
Janos Follathe50f2f12022-10-26 15:14:33 +0100536/**
Janos Follath8904a2d2022-10-31 15:32:28 +0000537 * Copy an MPI from a table without leaking the index.
Janos Follathe50f2f12022-10-26 15:14:33 +0100538 *
539 * \param dest The destination buffer. This must point to a writable
540 * buffer of at least \p limbs limbs.
541 * \param table The address of the table. This must point to a readable
Janos Follath8904a2d2022-10-31 15:32:28 +0000542 * array of \p count elements of \p limbs limbs each.
543 * \param limbs The number of limbs in each table entry.
544 * \param count The number of entries in \p table.
545 * \param index The (secret) table index to look up. This must be in the
546 * range `0 .. count-1`.
Janos Follathe50f2f12022-10-26 15:14:33 +0100547 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100548void mbedtls_mpi_core_ct_uint_table_lookup(mbedtls_mpi_uint *dest,
549 const mbedtls_mpi_uint *table,
550 size_t limbs,
551 size_t count,
552 size_t index);
Janos Follath59cbd1b2022-10-28 18:13:43 +0100553#endif /* MBEDTLS_TEST_HOOKS */
Janos Follathe50f2f12022-10-26 15:14:33 +0100554
Gilles Peskine909e03c2022-10-18 18:14:33 +0200555/**
556 * \brief Fill an integer with a number of random bytes.
557 *
558 * \param X The destination MPI.
559 * \param X_limbs The number of limbs of \p X.
560 * \param bytes The number of random bytes to generate.
561 * \param f_rng The RNG function to use. This must not be \c NULL.
562 * \param p_rng The RNG parameter to be passed to \p f_rng. This may be
563 * \c NULL if \p f_rng doesn't need a context argument.
564 *
565 * \return \c 0 if successful.
566 * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if \p X does not have
567 * enough room for \p bytes bytes.
568 * \return A negative error code on RNG failure.
569 *
570 * \note The bytes obtained from the RNG are interpreted
571 * as a big-endian representation of an MPI; this can
572 * be relevant in applications like deterministic ECDSA.
573 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100574int mbedtls_mpi_core_fill_random(mbedtls_mpi_uint *X, size_t X_limbs,
575 size_t bytes,
576 int (*f_rng)(void *, unsigned char *, size_t),
577 void *p_rng);
Gilles Peskine909e03c2022-10-18 18:14:33 +0200578
Gilles Peskine4a8c5cd2022-10-18 18:15:01 +0200579/** Generate a random number uniformly in a range.
580 *
581 * This function generates a random number between \p min inclusive and
582 * \p N exclusive.
583 *
584 * The procedure complies with RFC 6979 §3.3 (deterministic ECDSA)
585 * when the RNG is a suitably parametrized instance of HMAC_DRBG
586 * and \p min is \c 1.
587 *
588 * \note There are `N - min` possible outputs. The lower bound
589 * \p min can be reached, but the upper bound \p N cannot.
590 *
591 * \param X The destination MPI, with \p limbs limbs.
592 * It must not be aliased with \p N or otherwise overlap it.
593 * \param min The minimum value to return.
594 * \param N The upper bound of the range, exclusive, with \p limbs limbs.
595 * In other words, this is one plus the maximum value to return.
596 * \p N must be strictly larger than \p min.
597 * \param limbs The number of limbs of \p N and \p X.
598 * This must not be 0.
599 * \param f_rng The RNG function to use. This must not be \c NULL.
600 * \param p_rng The RNG parameter to be passed to \p f_rng.
601 *
602 * \return \c 0 if successful.
603 * \return #MBEDTLS_ERR_MPI_NOT_ACCEPTABLE if the implementation was
604 * unable to find a suitable value within a limited number
605 * of attempts. This has a negligible probability if \p N
606 * is significantly larger than \p min, which is the case
607 * for all usual cryptographic applications.
608 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100609int mbedtls_mpi_core_random(mbedtls_mpi_uint *X,
610 mbedtls_mpi_uint min,
611 const mbedtls_mpi_uint *N,
612 size_t limbs,
613 int (*f_rng)(void *, unsigned char *, size_t),
614 void *p_rng);
Gilles Peskine4a8c5cd2022-10-18 18:15:01 +0200615
Janos Follathb6673f02022-09-30 14:13:14 +0100616/**
Tom Cosgrove0a0dded2022-12-06 14:37:18 +0000617 * \brief Returns the number of limbs of working memory required for
618 * a call to `mbedtls_mpi_core_exp_mod()`.
Janos Follathb6673f02022-09-30 14:13:14 +0100619 *
Tom Cosgrove28ff92c2022-12-12 17:06:27 +0000620 * \note This will always be at least
621 * `mbedtls_mpi_core_montmul_working_limbs(AN_limbs)`,
622 * i.e. sufficient for a call to `mbedtls_mpi_core_montmul()`.
623 *
Tom Cosgrove0a0dded2022-12-06 14:37:18 +0000624 * \param AN_limbs The number of limbs in the input `A` and the modulus `N`
625 * (they must be the same size) that will be given to
626 * `mbedtls_mpi_core_exp_mod()`.
627 * \param E_limbs The number of limbs in the exponent `E` that will be given
628 * to `mbedtls_mpi_core_exp_mod()`.
Janos Follathb6673f02022-09-30 14:13:14 +0100629 *
Tom Cosgrove0a0dded2022-12-06 14:37:18 +0000630 * \return The number of limbs of working memory required by
631 * `mbedtls_mpi_core_exp_mod()`.
Janos Follathb6673f02022-09-30 14:13:14 +0100632 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100633size_t mbedtls_mpi_core_exp_mod_working_limbs(size_t AN_limbs, size_t E_limbs);
Tom Cosgrove0a0dded2022-12-06 14:37:18 +0000634
635/**
Manuel Pégourié-Gonnard75ed5872024-06-18 12:52:45 +0200636 * \brief Perform a modular exponentiation with public or secret exponent:
637 * X = A^E mod N, where \p A is already in Montgomery form.
638 *
Janos Follath38ff70e2024-08-12 18:20:59 +0100639 * \warning This function is not constant time with respect to \p E (the exponent).
640 *
Manuel Pégourié-Gonnard75ed5872024-06-18 12:52:45 +0200641 * \p X may be aliased to \p A, but not to \p RR or \p E, even if \p E_limbs ==
642 * \p AN_limbs.
643 *
644 * \param[out] X The destination MPI, as a little endian array of length
645 * \p AN_limbs.
646 * \param[in] A The base MPI, as a little endian array of length \p AN_limbs.
647 * Must be in Montgomery form.
648 * \param[in] N The modulus, as a little endian array of length \p AN_limbs.
649 * \param AN_limbs The number of limbs in \p X, \p A, \p N, \p RR.
650 * \param[in] E The exponent, as a little endian array of length \p E_limbs.
651 * \param E_limbs The number of limbs in \p E.
652 * \param[in] RR The precomputed residue of 2^{2*biL} modulo N, as a little
653 * endian array of length \p AN_limbs.
654 * \param[in,out] T Temporary storage of at least the number of limbs returned
655 * by `mbedtls_mpi_core_exp_mod_working_limbs()`.
656 * Its initial content is unused and its final content is
657 * indeterminate.
658 * It must not alias or otherwise overlap any of the other
659 * parameters.
660 * It is up to the caller to zeroize \p T when it is no
661 * longer needed, and before freeing it if it was dynamically
662 * allocated.
Manuel Pégourié-Gonnard75ed5872024-06-18 12:52:45 +0200663 */
Janos Follath38ff70e2024-08-12 18:20:59 +0100664void mbedtls_mpi_core_exp_mod_unsafe(mbedtls_mpi_uint *X,
665 const mbedtls_mpi_uint *A,
666 const mbedtls_mpi_uint *N, size_t AN_limbs,
667 const mbedtls_mpi_uint *E, size_t E_limbs,
668 const mbedtls_mpi_uint *RR,
669 mbedtls_mpi_uint *T);
Manuel Pégourié-Gonnard75ed5872024-06-18 12:52:45 +0200670
671/**
Tom Cosgrove0a0dded2022-12-06 14:37:18 +0000672 * \brief Perform a modular exponentiation with secret exponent:
673 * X = A^E mod N, where \p A is already in Montgomery form.
674 *
Tom Cosgrovea7f0d7b2022-12-07 13:29:07 +0000675 * \p X may be aliased to \p A, but not to \p RR or \p E, even if \p E_limbs ==
676 * \p AN_limbs.
677 *
Tom Cosgrove0a0dded2022-12-06 14:37:18 +0000678 * \param[out] X The destination MPI, as a little endian array of length
679 * \p AN_limbs.
680 * \param[in] A The base MPI, as a little endian array of length \p AN_limbs.
681 * Must be in Montgomery form.
682 * \param[in] N The modulus, as a little endian array of length \p AN_limbs.
683 * \param AN_limbs The number of limbs in \p X, \p A, \p N, \p RR.
684 * \param[in] E The exponent, as a little endian array of length \p E_limbs.
685 * \param E_limbs The number of limbs in \p E.
686 * \param[in] RR The precomputed residue of 2^{2*biL} modulo N, as a little
687 * endian array of length \p AN_limbs.
688 * \param[in,out] T Temporary storage of at least the number of limbs returned
689 * by `mbedtls_mpi_core_exp_mod_working_limbs()`.
690 * Its initial content is unused and its final content is
691 * indeterminate.
692 * It must not alias or otherwise overlap any of the other
693 * parameters.
694 * It is up to the caller to zeroize \p T when it is no
695 * longer needed, and before freeing it if it was dynamically
696 * allocated.
697 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100698void mbedtls_mpi_core_exp_mod(mbedtls_mpi_uint *X,
699 const mbedtls_mpi_uint *A,
700 const mbedtls_mpi_uint *N, size_t AN_limbs,
701 const mbedtls_mpi_uint *E, size_t E_limbs,
702 const mbedtls_mpi_uint *RR,
703 mbedtls_mpi_uint *T);
Janos Follathb6673f02022-09-30 14:13:14 +0100704
Hanno Beckerd9b23482022-08-25 08:25:19 +0100705/**
706 * \brief Subtract unsigned integer from known-size large unsigned integers.
707 * Return the borrow.
708 *
Tom Cosgrovef7ff4c92022-08-25 08:39:07 +0100709 * \param[out] X The result of the subtraction.
710 * \param[in] A The left operand.
711 * \param b The unsigned scalar to subtract.
712 * \param limbs Number of limbs of \p X and \p A.
Hanno Beckerd9b23482022-08-25 08:25:19 +0100713 *
Tom Cosgrovef7ff4c92022-08-25 08:39:07 +0100714 * \return 1 if `A < b`.
715 * 0 if `A >= b`.
Hanno Beckerd9b23482022-08-25 08:25:19 +0100716 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100717mbedtls_mpi_uint mbedtls_mpi_core_sub_int(mbedtls_mpi_uint *X,
718 const mbedtls_mpi_uint *A,
719 mbedtls_mpi_uint b,
720 size_t limbs);
Hanno Beckerd9b23482022-08-25 08:25:19 +0100721
Tom Cosgrove30f3b4d2022-12-12 16:54:57 +0000722/**
723 * \brief Determine if a given MPI has the value \c 0 in constant time with
724 * respect to the value (but not with respect to the number of limbs).
725 *
726 * \param[in] A The MPI to test.
727 * \param limbs Number of limbs in \p A.
728 *
Janos Follathaec1a862024-02-21 11:24:20 +0000729 * \return MBEDTLS_CT_FALSE if `A == 0`
730 * MBEDTLS_CT_TRUE if `A != 0`.
Tom Cosgrove30f3b4d2022-12-12 16:54:57 +0000731 */
Janos Follathadb9d2d2024-03-11 10:03:05 +0000732mbedtls_ct_condition_t mbedtls_mpi_core_check_zero_ct(const mbedtls_mpi_uint *A,
Janos Follathaec1a862024-02-21 11:24:20 +0000733 size_t limbs);
Tom Cosgrove30f3b4d2022-12-12 16:54:57 +0000734
Tom Cosgrove28ff92c2022-12-12 17:06:27 +0000735/**
736 * \brief Returns the number of limbs of working memory required for
737 * a call to `mbedtls_mpi_core_montmul()`.
738 *
739 * \param AN_limbs The number of limbs in the input `A` and the modulus `N`
740 * (they must be the same size) that will be given to
741 * `mbedtls_mpi_core_montmul()` or one of the other functions
742 * that specifies this as the amount of working memory needed.
743 *
744 * \return The number of limbs of working memory required by
745 * `mbedtls_mpi_core_montmul()` (or other similar function).
746 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100747static inline size_t mbedtls_mpi_core_montmul_working_limbs(size_t AN_limbs)
Tom Cosgrove28ff92c2022-12-12 17:06:27 +0000748{
Gilles Peskine449bd832023-01-11 14:50:10 +0100749 return 2 * AN_limbs + 1;
Tom Cosgrove28ff92c2022-12-12 17:06:27 +0000750}
751
Tom Cosgrove786848b2022-12-13 10:45:19 +0000752/** Convert an MPI into Montgomery form.
753 *
754 * \p X may be aliased to \p A, but may not otherwise overlap it.
755 *
Tom Cosgrove5c8505f2023-03-07 11:39:52 +0000756 * \p X may not alias \p N (it is in canonical form, so must be strictly less
Tom Cosgrove786848b2022-12-13 10:45:19 +0000757 * than \p N). Nor may it alias or overlap \p rr (this is unlikely to be
758 * required in practice.)
759 *
760 * This function is a thin wrapper around `mbedtls_mpi_core_montmul()` that is
761 * an alternative to calling `mbedtls_mpi_mod_raw_to_mont_rep()` when we
762 * don't want to allocate memory.
763 *
764 * \param[out] X The result of the conversion.
765 * Must have the same number of limbs as \p A.
766 * \param[in] A The MPI to convert into Montgomery form.
767 * Must have the same number of limbs as the modulus.
768 * \param[in] N The address of the modulus, which gives the size of
Tom Cosgrovef7237542022-12-16 16:10:36 +0000769 * the base `R` = 2^(biL*N->limbs).
Tom Cosgrove786848b2022-12-13 10:45:19 +0000770 * \param[in] AN_limbs The number of limbs in \p X, \p A, \p N and \p rr.
771 * \param mm The Montgomery constant for \p N: -N^-1 mod 2^biL.
Tom Cosgroveb38c2ed2022-12-14 13:11:46 +0000772 * This can be determined by calling
Tom Cosgrove786848b2022-12-13 10:45:19 +0000773 * `mbedtls_mpi_core_montmul_init()`.
774 * \param[in] rr The residue for `2^{2*n*biL} mod N`.
775 * \param[in,out] T Temporary storage of size at least
776 * `mbedtls_mpi_core_montmul_working_limbs(AN_limbs)`
777 * limbs.
778 * Its initial content is unused and
779 * its final content is indeterminate.
780 * It must not alias or otherwise overlap any of the
781 * other parameters.
782 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100783void mbedtls_mpi_core_to_mont_rep(mbedtls_mpi_uint *X,
784 const mbedtls_mpi_uint *A,
785 const mbedtls_mpi_uint *N,
786 size_t AN_limbs,
787 mbedtls_mpi_uint mm,
788 const mbedtls_mpi_uint *rr,
789 mbedtls_mpi_uint *T);
Tom Cosgrove786848b2022-12-13 10:45:19 +0000790
791/** Convert an MPI from Montgomery form.
792 *
793 * \p X may be aliased to \p A, but may not otherwise overlap it.
794 *
Tom Cosgrove5c8505f2023-03-07 11:39:52 +0000795 * \p X may not alias \p N (it is in canonical form, so must be strictly less
Tom Cosgrove786848b2022-12-13 10:45:19 +0000796 * than \p N).
797 *
798 * This function is a thin wrapper around `mbedtls_mpi_core_montmul()` that is
799 * an alternative to calling `mbedtls_mpi_mod_raw_from_mont_rep()` when we
800 * don't want to allocate memory.
801 *
802 * \param[out] X The result of the conversion.
803 * Must have the same number of limbs as \p A.
804 * \param[in] A The MPI to convert from Montgomery form.
805 * Must have the same number of limbs as the modulus.
806 * \param[in] N The address of the modulus, which gives the size of
Tom Cosgrovef7237542022-12-16 16:10:36 +0000807 * the base `R` = 2^(biL*N->limbs).
Tom Cosgrove786848b2022-12-13 10:45:19 +0000808 * \param[in] AN_limbs The number of limbs in \p X, \p A and \p N.
809 * \param mm The Montgomery constant for \p N: -N^-1 mod 2^biL.
Tom Cosgroveb38c2ed2022-12-14 13:11:46 +0000810 * This can be determined by calling
Tom Cosgrove786848b2022-12-13 10:45:19 +0000811 * `mbedtls_mpi_core_montmul_init()`.
812 * \param[in,out] T Temporary storage of size at least
813 * `mbedtls_mpi_core_montmul_working_limbs(AN_limbs)`
814 * limbs.
815 * Its initial content is unused and
816 * its final content is indeterminate.
817 * It must not alias or otherwise overlap any of the
818 * other parameters.
819 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100820void mbedtls_mpi_core_from_mont_rep(mbedtls_mpi_uint *X,
821 const mbedtls_mpi_uint *A,
822 const mbedtls_mpi_uint *N,
823 size_t AN_limbs,
824 mbedtls_mpi_uint mm,
825 mbedtls_mpi_uint *T);
Tom Cosgrove786848b2022-12-13 10:45:19 +0000826
Janos Follath8786dd72024-08-20 10:21:54 +0100827/*
828 * Can't define thread local variables with our abstraction layer: do nothing if threading is on.
829 */
830#if defined(MBEDTLS_TEST_HOOKS) && !defined(MBEDTLS_THREADING_C)
Janos Follatha1126912024-08-20 09:56:16 +0100831extern int mbedtls_mpi_optionally_safe_codepath;
Janos Follathe0842aa2024-08-13 08:40:31 +0100832
Janos Follatha1126912024-08-20 09:56:16 +0100833static inline void mbedtls_mpi_optionally_safe_codepath_reset(void)
Janos Follathe0842aa2024-08-13 08:40:31 +0100834{
Janos Follath42f72b32024-08-22 08:25:33 +0100835 mbedtls_mpi_optionally_safe_codepath = MBEDTLS_MPI_IS_TEST;
Janos Follathe0842aa2024-08-13 08:40:31 +0100836}
837#endif
838
Gabor Mezeib9030702022-07-18 23:09:45 +0200839#endif /* MBEDTLS_BIGNUM_CORE_H */