blob: fc761347effe9fe2d74e3ab6dce3cc7b1a7d65f4 [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
Gabor Mezeib9030702022-07-18 23:09:45 +020073#include "mbedtls/bignum.h"
Gabor Mezeib9030702022-07-18 23:09:45 +020074
Dave Rodgmancd2e38b2023-05-17 13:31:55 +010075#include "constant_time_internal.h"
76
Gilles Peskine449bd832023-01-11 14:50:10 +010077#define ciL (sizeof(mbedtls_mpi_uint)) /** chars in limb */
78#define biL (ciL << 3) /** bits in limb */
79#define biH (ciL << 2) /** half limb size */
Tom Cosgrove5eefc3d2022-08-31 17:16:50 +010080
81/*
82 * Convert between bits/chars and number of limbs
83 * Divide first in order to avoid potential overflows
84 */
Gilles Peskine449bd832023-01-11 14:50:10 +010085#define BITS_TO_LIMBS(i) ((i) / biL + ((i) % biL != 0))
86#define CHARS_TO_LIMBS(i) ((i) / ciL + ((i) % ciL != 0))
Tom Cosgrove5eefc3d2022-08-31 17:16:50 +010087/* Get a specific byte, without range checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +010088#define GET_BYTE(X, i) \
89 (((X)[(i) / ciL] >> (((i) % ciL) * 8)) & 0xff)
Tom Cosgrove5eefc3d2022-08-31 17:16:50 +010090
Janos Follath90b42712024-08-12 19:32:45 +010091/* Constants to identify whether a value is public or secret. If a parameter is marked as secret by
92 * this constant, the function must be constant time with respect to the parameter.
93 *
94 * This is only needed for functions with the _optionally_safe postfix. All other functions have
95 * fixed behavior that can't be changed at runtime and are constant time with respect to their
96 * parameters as prescribed by their documentation or by conventions in their module's documentation.
97 *
98 * Parameters should be named X_public where X is the name of the
99 * corresponding input parameter.
100 *
101 * Implementation should always check using
102 * if (X_public == MBEDTLS_MPI_IS_PUBLIC) {
103 * // unsafe path
104 * } else {
105 * // safe path
106 * }
107 * not the other way round, in order to prevent misuse. (This is, if a value
Janos Follath2f8ad592024-08-22 17:13:25 +0100108 * other than the two below is passed, default to the safe path.)
109 *
110 * The value of MBEDTLS_MPI_IS_PUBLIC is chosen in a way that is unlikely to happen by accident, but
111 * which can be used as an immediate value in a Thumb2 comparison (for code size). */
Janos Follath0c292b22024-08-12 19:55:02 +0100112#define MBEDTLS_MPI_IS_PUBLIC 0x2a2a2a2a
Janos Follath90b42712024-08-12 19:32:45 +0100113#define MBEDTLS_MPI_IS_SECRET 0
Janos Follath42f72b32024-08-22 08:25:33 +0100114#if defined(MBEDTLS_TEST_HOOKS) && !defined(MBEDTLS_THREADING_C)
115// Default value for testing that is neither MBEDTLS_MPI_IS_PUBLIC nor MBEDTLS_MPI_IS_SECRET
116#define MBEDTLS_MPI_IS_TEST 1
117#endif
Janos Follath90b42712024-08-12 19:32:45 +0100118
Gabor Mezei37b06362022-08-02 17:22:18 +0200119/** Count leading zero bits in a given integer.
120 *
Dave Rodgman0f16d562023-04-24 12:53:29 +0100121 * \warning The result is undefined if \p a == 0
122 *
Janos Follathb7a88ec2022-08-19 12:24:40 +0100123 * \param a Integer to count leading zero bits.
Gabor Mezei37b06362022-08-02 17:22:18 +0200124 *
Dave Rodgman0f16d562023-04-24 12:53:29 +0100125 * \return The number of leading zero bits in \p a, if \p a != 0.
126 * If \p a == 0, the result is undefined.
Gabor Mezei37b06362022-08-02 17:22:18 +0200127 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100128size_t mbedtls_mpi_core_clz(mbedtls_mpi_uint a);
Janos Follath4670f882022-07-21 18:25:42 +0100129
Janos Follatha95f2042022-08-19 12:09:17 +0100130/** Return the minimum number of bits required to represent the value held
Janos Follath63184682022-08-11 17:42:59 +0100131 * in the MPI.
132 *
Janos Follathb7a88ec2022-08-19 12:24:40 +0100133 * \note This function returns 0 if all the limbs of \p A are 0.
Gabor Mezei37b06362022-08-02 17:22:18 +0200134 *
Janos Follathb7a88ec2022-08-19 12:24:40 +0100135 * \param[in] A The address of the MPI.
Janos Follathc4596412022-08-22 10:01:27 +0100136 * \param A_limbs The number of limbs of \p A.
Gabor Mezei37b06362022-08-02 17:22:18 +0200137 *
Janos Follathb7a88ec2022-08-19 12:24:40 +0100138 * \return The number of bits in \p A.
Gabor Mezei37b06362022-08-02 17:22:18 +0200139 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100140size_t mbedtls_mpi_core_bitlen(const mbedtls_mpi_uint *A, size_t A_limbs);
Janos Follath4670f882022-07-21 18:25:42 +0100141
Gabor Mezei37b06362022-08-02 17:22:18 +0200142/** Convert a big-endian byte array aligned to the size of mbedtls_mpi_uint
143 * into the storage form used by mbedtls_mpi.
144 *
Janos Follathb7a88ec2022-08-19 12:24:40 +0100145 * \param[in,out] A The address of the MPI.
Janos Follathc4596412022-08-22 10:01:27 +0100146 * \param A_limbs The number of limbs of \p A.
Gabor Mezei37b06362022-08-02 17:22:18 +0200147 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100148void mbedtls_mpi_core_bigendian_to_host(mbedtls_mpi_uint *A,
149 size_t A_limbs);
Janos Follath4670f882022-07-21 18:25:42 +0100150
Gilles Peskine6f949ea2022-09-20 18:38:35 +0200151/** \brief Compare a machine integer with an MPI.
152 *
153 * This function operates in constant time with respect
154 * to the values of \p min and \p A.
155 *
156 * \param min A machine integer.
157 * \param[in] A An MPI.
158 * \param A_limbs The number of limbs of \p A.
159 * This must be at least 1.
160 *
Dave Rodgmanfd7fab42023-05-17 14:00:39 +0100161 * \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 +0200162 */
Dave Rodgmanfd7fab42023-05-17 14:00:39 +0100163mbedtls_ct_condition_t mbedtls_mpi_core_uint_le_mpi(mbedtls_mpi_uint min,
164 const mbedtls_mpi_uint *A,
165 size_t A_limbs);
Gilles Peskine6f949ea2022-09-20 18:38:35 +0200166
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200167/**
Dave Rodgman7d4f0192023-05-09 14:01:05 +0100168 * \brief Check if one unsigned MPI is less than another in constant
169 * time.
170 *
171 * \param A The left-hand MPI. This must point to an array of limbs
172 * with the same allocated length as \p B.
173 * \param B The right-hand MPI. This must point to an array of limbs
174 * with the same allocated length as \p A.
175 * \param limbs The number of limbs in \p A and \p B.
176 * This must not be 0.
177 *
Dave Rodgman8ac9a1d2023-05-17 15:16:22 +0100178 * \return MBEDTLS_CT_TRUE if \p A is less than \p B.
179 * MBEDTLS_CT_FALSE if \p A is greater than or equal to \p B.
Dave Rodgman7d4f0192023-05-09 14:01:05 +0100180 */
Dave Rodgman8ac9a1d2023-05-17 15:16:22 +0100181mbedtls_ct_condition_t mbedtls_mpi_core_lt_ct(const mbedtls_mpi_uint *A,
182 const mbedtls_mpi_uint *B,
183 size_t limbs);
184
Dave Rodgman7d4f0192023-05-09 14:01:05 +0100185/**
Gabor Mezei4086de62022-10-14 16:29:42 +0200186 * \brief Perform a safe conditional copy of an MPI which doesn't reveal
187 * whether assignment was done or not.
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200188 *
Gabor Mezei4086de62022-10-14 16:29:42 +0200189 * \param[out] X The address of the destination MPI.
190 * This must be initialized. Must have enough limbs to
191 * store the full value of \p A.
192 * \param[in] A The address of the source MPI. This must be initialized.
Gabor Mezei1c628d52022-09-27 12:13:51 +0200193 * \param limbs The number of limbs of \p A.
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200194 * \param assign The condition deciding whether to perform the
Dave Rodgmanfb1b8512023-07-31 12:27:05 +0100195 * assignment or not. Callers will need to use
196 * the constant time interface (e.g. `mbedtls_ct_bool()`)
197 * to construct this argument.
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200198 *
199 * \note This function avoids leaking any information about whether
200 * the assignment was done or not.
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200201 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100202void mbedtls_mpi_core_cond_assign(mbedtls_mpi_uint *X,
203 const mbedtls_mpi_uint *A,
204 size_t limbs,
Dave Rodgmancd2e38b2023-05-17 13:31:55 +0100205 mbedtls_ct_condition_t assign);
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200206
207/**
Gabor Mezei4086de62022-10-14 16:29:42 +0200208 * \brief Perform a safe conditional swap of two MPIs which doesn't reveal
209 * whether the swap was done or not.
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200210 *
Gabor Mezei86dfe382022-09-30 14:03:04 +0200211 * \param[in,out] X The address of the first MPI.
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200212 * This must be initialized.
Gabor Mezei86dfe382022-09-30 14:03:04 +0200213 * \param[in,out] Y The address of the second MPI.
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200214 * This must be initialized.
Gabor Mezeie5b85852022-09-30 13:54:02 +0200215 * \param limbs The number of limbs of \p X and \p Y.
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200216 * \param swap The condition deciding whether to perform
Dave Rodgmancd2e38b2023-05-17 13:31:55 +0100217 * the swap or not.
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200218 *
219 * \note This function avoids leaking any information about whether
220 * the swap was done or not.
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200221 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100222void mbedtls_mpi_core_cond_swap(mbedtls_mpi_uint *X,
223 mbedtls_mpi_uint *Y,
224 size_t limbs,
Dave Rodgmancd2e38b2023-05-17 13:31:55 +0100225 mbedtls_ct_condition_t swap);
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200226
Janos Follathaf3f39c2022-08-22 09:06:32 +0100227/** Import X from unsigned binary data, little-endian.
Gabor Mezei37b06362022-08-02 17:22:18 +0200228 *
Janos Follath63184682022-08-11 17:42:59 +0100229 * The MPI needs to have enough limbs to store the full value (including any
230 * most significant zero bytes in the input).
Gabor Mezei37b06362022-08-02 17:22:18 +0200231 *
Janos Follathb7a88ec2022-08-19 12:24:40 +0100232 * \param[out] X The address of the MPI.
233 * \param X_limbs The number of limbs of \p X.
234 * \param[in] input The input buffer to import from.
235 * \param input_length The length bytes of \p input.
Gabor Mezei37b06362022-08-02 17:22:18 +0200236 *
237 * \return \c 0 if successful.
238 * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p X isn't
Janos Follathb7a88ec2022-08-19 12:24:40 +0100239 * large enough to hold the value in \p input.
Gabor Mezei37b06362022-08-02 17:22:18 +0200240 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100241int mbedtls_mpi_core_read_le(mbedtls_mpi_uint *X,
242 size_t X_limbs,
243 const unsigned char *input,
244 size_t input_length);
Gabor Mezeib9030702022-07-18 23:09:45 +0200245
Janos Follathaf3f39c2022-08-22 09:06:32 +0100246/** Import X from unsigned binary data, big-endian.
Gabor Mezei37b06362022-08-02 17:22:18 +0200247 *
Janos Follath63184682022-08-11 17:42:59 +0100248 * The MPI needs to have enough limbs to store the full value (including any
249 * most significant zero bytes in the input).
Gabor Mezei37b06362022-08-02 17:22:18 +0200250 *
Janos Follathb7a88ec2022-08-19 12:24:40 +0100251 * \param[out] X The address of the MPI.
Tom Cosgrove8a1f7842023-02-01 08:43:54 +0000252 * May only be #NULL if \p X_limbs is 0 and \p input_length
Janos Follathb7a88ec2022-08-19 12:24:40 +0100253 * is 0.
254 * \param X_limbs The number of limbs of \p X.
255 * \param[in] input The input buffer to import from.
256 * May only be #NULL if \p input_length is 0.
257 * \param input_length The length in bytes of \p input.
Gabor Mezei37b06362022-08-02 17:22:18 +0200258 *
259 * \return \c 0 if successful.
260 * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p X isn't
Janos Follathb7a88ec2022-08-19 12:24:40 +0100261 * large enough to hold the value in \p input.
Gabor Mezei37b06362022-08-02 17:22:18 +0200262 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100263int mbedtls_mpi_core_read_be(mbedtls_mpi_uint *X,
264 size_t X_limbs,
265 const unsigned char *input,
266 size_t input_length);
Gabor Mezeib9030702022-07-18 23:09:45 +0200267
Janos Follathaf3f39c2022-08-22 09:06:32 +0100268/** Export A into unsigned binary data, little-endian.
Gabor Mezei37b06362022-08-02 17:22:18 +0200269 *
Janos Follathb7a88ec2022-08-19 12:24:40 +0100270 * \note If \p output is shorter than \p A the export is still successful if the
271 * value held in \p A fits in the buffer (that is, if enough of the most
272 * significant bytes of \p A are 0).
Janos Follath63184682022-08-11 17:42:59 +0100273 *
Janos Follathb7a88ec2022-08-19 12:24:40 +0100274 * \param[in] A The address of the MPI.
275 * \param A_limbs The number of limbs of \p A.
276 * \param[out] output The output buffer to export to.
277 * \param output_length The length in bytes of \p output.
Gabor Mezei37b06362022-08-02 17:22:18 +0200278 *
279 * \return \c 0 if successful.
Janos Follathb7a88ec2022-08-19 12:24:40 +0100280 * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p output isn't
281 * large enough to hold the value of \p A.
Gabor Mezei37b06362022-08-02 17:22:18 +0200282 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100283int mbedtls_mpi_core_write_le(const mbedtls_mpi_uint *A,
284 size_t A_limbs,
285 unsigned char *output,
286 size_t output_length);
Gabor Mezeib9030702022-07-18 23:09:45 +0200287
Janos Follathaf3f39c2022-08-22 09:06:32 +0100288/** Export A into unsigned binary data, big-endian.
Gabor Mezei37b06362022-08-02 17:22:18 +0200289 *
Janos Follathb7a88ec2022-08-19 12:24:40 +0100290 * \note If \p output is shorter than \p A the export is still successful if the
291 * value held in \p A fits in the buffer (that is, if enough of the most
292 * significant bytes of \p A are 0).
Janos Follath63184682022-08-11 17:42:59 +0100293 *
Janos Follathb7a88ec2022-08-19 12:24:40 +0100294 * \param[in] A The address of the MPI.
295 * \param A_limbs The number of limbs of \p A.
296 * \param[out] output The output buffer to export to.
297 * \param output_length The length in bytes of \p output.
Gabor Mezei37b06362022-08-02 17:22:18 +0200298 *
299 * \return \c 0 if successful.
Janos Follathb7a88ec2022-08-19 12:24:40 +0100300 * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p output isn't
301 * large enough to hold the value of \p A.
Gabor Mezei37b06362022-08-02 17:22:18 +0200302 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100303int mbedtls_mpi_core_write_be(const mbedtls_mpi_uint *A,
304 size_t A_limbs,
305 unsigned char *output,
306 size_t output_length);
Gabor Mezeib9030702022-07-18 23:09:45 +0200307
Minos Galanakisec09e252023-04-20 14:22:16 +0100308/** \brief Shift an MPI in-place right by a number of bits.
Gilles Peskine66414202022-09-21 15:36:16 +0200309 *
310 * Shifting by more bits than there are bit positions
311 * in \p X is valid and results in setting \p X to 0.
312 *
313 * This function's execution time depends on the value
314 * of \p count (and of course \p limbs).
315 *
316 * \param[in,out] X The number to shift.
317 * \param limbs The number of limbs of \p X. This must be at least 1.
318 * \param count The number of bits to shift by.
319 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100320void mbedtls_mpi_core_shift_r(mbedtls_mpi_uint *X, size_t limbs,
321 size_t count);
Gilles Peskine66414202022-09-21 15:36:16 +0200322
Tom Cosgrove90c426b2022-08-23 16:15:19 +0100323/**
Minos Galanakisec09e252023-04-20 14:22:16 +0100324 * \brief Shift an MPI in-place left by a number of bits.
Minos Galanakisad808dd2023-04-20 12:18:41 +0100325 *
Minos Galanakisec09e252023-04-20 14:22:16 +0100326 * Shifting by more bits than there are bit positions
Minos Galanakisb8944032023-04-28 14:09:44 +0100327 * in \p X will produce an unspecified result.
Minos Galanakisad808dd2023-04-20 12:18:41 +0100328 *
Minos Galanakisec09e252023-04-20 14:22:16 +0100329 * This function's execution time depends on the value
330 * of \p count (and of course \p limbs).
331 * \param[in,out] X The number to shift.
332 * \param limbs The number of limbs of \p X. This must be at least 1.
333 * \param count The number of bits to shift by.
Minos Galanakisad808dd2023-04-20 12:18:41 +0100334 */
Minos Galanakisec09e252023-04-20 14:22:16 +0100335void mbedtls_mpi_core_shift_l(mbedtls_mpi_uint *X, size_t limbs,
336 size_t count);
Minos Galanakisad808dd2023-04-20 12:18:41 +0100337
338/**
Tom Cosgroveaf7d44b2022-08-24 14:05:26 +0100339 * \brief Add two fixed-size large unsigned integers, returning the carry.
Hanno Beckerc9887132022-08-24 12:54:36 +0100340 *
Tom Cosgroveaf7d44b2022-08-24 14:05:26 +0100341 * Calculates `A + B` where `A` and `B` have the same size.
342 *
Tom Cosgrove82f13102022-10-25 12:46:03 +0100343 * This function operates modulo `2^(biL*limbs)` and returns the carry
Hanno Beckerc9887132022-08-24 12:54:36 +0100344 * (1 if there was a wraparound, and 0 otherwise).
345 *
Tom Cosgroveaf7d44b2022-08-24 14:05:26 +0100346 * \p X may be aliased to \p A or \p B.
Hanno Beckerc9887132022-08-24 12:54:36 +0100347 *
Tom Cosgroveaf7d44b2022-08-24 14:05:26 +0100348 * \param[out] X The result of the addition.
349 * \param[in] A Little-endian presentation of the left operand.
350 * \param[in] B Little-endian presentation of the right operand.
351 * \param limbs Number of limbs of \p X, \p A and \p B.
Hanno Beckerc9887132022-08-24 12:54:36 +0100352 *
Tom Cosgroveaf7d44b2022-08-24 14:05:26 +0100353 * \return 1 if `A + B >= 2^(biL*limbs)`, 0 otherwise.
Hanno Beckerc9887132022-08-24 12:54:36 +0100354 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100355mbedtls_mpi_uint mbedtls_mpi_core_add(mbedtls_mpi_uint *X,
356 const mbedtls_mpi_uint *A,
357 const mbedtls_mpi_uint *B,
358 size_t limbs);
Hanno Beckerc9887132022-08-24 12:54:36 +0100359
Tom Cosgrove90c426b2022-08-23 16:15:19 +0100360/**
Tom Cosgrove5c0e8102022-09-15 15:46:10 +0100361 * \brief Conditional addition of two fixed-size large unsigned integers,
Tom Cosgroved932de82022-08-25 16:43:43 +0100362 * returning the carry.
Hanno Becker71f4b0d2022-08-23 12:09:35 +0100363 *
364 * Functionally equivalent to
365 *
366 * ```
367 * if( cond )
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100368 * X += A;
Hanno Becker71f4b0d2022-08-23 12:09:35 +0100369 * return carry;
370 * ```
371 *
Tom Cosgrove47828232022-09-20 13:51:50 +0100372 * This function operates modulo `2^(biL*limbs)`.
373 *
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100374 * \param[in,out] X The pointer to the (little-endian) array
Tom Cosgrove72594632022-08-24 11:51:58 +0100375 * representing the bignum to accumulate onto.
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100376 * \param[in] A The pointer to the (little-endian) array
Tom Cosgrove72594632022-08-24 11:51:58 +0100377 * representing the bignum to conditionally add
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100378 * to \p X. This may be aliased to \p X but may not
Tom Cosgroveed43c6c2022-08-31 11:35:00 +0100379 * overlap otherwise.
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100380 * \param limbs Number of limbs of \p X and \p A.
Tom Cosgrove72594632022-08-24 11:51:58 +0100381 * \param cond Condition bit dictating whether addition should
382 * happen or not. This must be \c 0 or \c 1.
Hanno Becker71f4b0d2022-08-23 12:09:35 +0100383 *
Tom Cosgroveecbb1242022-08-25 10:13:44 +0100384 * \warning If \p cond is neither 0 nor 1, the result of this function
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100385 * is unspecified, and the resulting value in \p X might be
386 * neither its original value nor \p X + \p A.
Tom Cosgrove72594632022-08-24 11:51:58 +0100387 *
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100388 * \return 1 if `X + cond * A >= 2^(biL*limbs)`, 0 otherwise.
Hanno Becker71f4b0d2022-08-23 12:09:35 +0100389 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100390mbedtls_mpi_uint mbedtls_mpi_core_add_if(mbedtls_mpi_uint *X,
391 const mbedtls_mpi_uint *A,
392 size_t limbs,
393 unsigned cond);
Hanno Becker71f4b0d2022-08-23 12:09:35 +0100394
Tom Cosgroveb4964862022-08-30 11:57:22 +0100395/**
Tom Cosgrove5c0e8102022-09-15 15:46:10 +0100396 * \brief Subtract two fixed-size large unsigned integers, returning the borrow.
Tom Cosgroveb4964862022-08-30 11:57:22 +0100397 *
Tom Cosgrove5dd97e62022-08-30 14:31:49 +0100398 * Calculate `A - B` where \p A and \p B have the same size.
Tom Cosgrove630110a2022-08-31 17:09:29 +0100399 * This function operates modulo `2^(biL*limbs)` and returns the carry
Tom Cosgroveb4964862022-08-30 11:57:22 +0100400 * (1 if there was a wraparound, i.e. if `A < B`, and 0 otherwise).
401 *
Tom Cosgrove5dd97e62022-08-30 14:31:49 +0100402 * \p X may be aliased to \p A or \p B, or even both, but may not overlap
403 * either otherwise.
Tom Cosgroveb4964862022-08-30 11:57:22 +0100404 *
405 * \param[out] X The result of the subtraction.
406 * \param[in] A Little-endian presentation of left operand.
407 * \param[in] B Little-endian presentation of right operand.
408 * \param limbs Number of limbs of \p X, \p A and \p B.
409 *
410 * \return 1 if `A < B`.
411 * 0 if `A >= B`.
412 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100413mbedtls_mpi_uint mbedtls_mpi_core_sub(mbedtls_mpi_uint *X,
414 const mbedtls_mpi_uint *A,
415 const mbedtls_mpi_uint *B,
416 size_t limbs);
Tom Cosgroveb4964862022-08-30 11:57:22 +0100417
418/**
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100419 * \brief Perform a fixed-size multiply accumulate operation: X += b * A
Tom Cosgroveb4964862022-08-30 11:57:22 +0100420 *
Tom Cosgroveb0b77e12022-09-20 13:33:40 +0100421 * \p X may be aliased to \p A (when \p X_limbs == \p A_limbs), but may not
422 * otherwise overlap.
423 *
Tom Cosgrove47828232022-09-20 13:51:50 +0100424 * This function operates modulo `2^(biL*X_limbs)`.
425 *
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100426 * \param[in,out] X The pointer to the (little-endian) array
Tom Cosgroveb4964862022-08-30 11:57:22 +0100427 * representing the bignum to accumulate onto.
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100428 * \param X_limbs The number of limbs of \p X. This must be
429 * at least \p A_limbs.
430 * \param[in] A The pointer to the (little-endian) array
Tom Cosgroveb4964862022-08-30 11:57:22 +0100431 * representing the bignum to multiply with.
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100432 * This may be aliased to \p X but may not overlap
Tom Cosgroveed43c6c2022-08-31 11:35:00 +0100433 * otherwise.
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100434 * \param A_limbs The number of limbs of \p A.
435 * \param b X scalar to multiply with.
Tom Cosgroveb4964862022-08-30 11:57:22 +0100436 *
437 * \return The carry at the end of the operation.
438 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100439mbedtls_mpi_uint mbedtls_mpi_core_mla(mbedtls_mpi_uint *X, size_t X_limbs,
440 const mbedtls_mpi_uint *A, size_t A_limbs,
441 mbedtls_mpi_uint b);
Tom Cosgroveb4964862022-08-30 11:57:22 +0100442
Hanno Becker4ae890b2022-08-24 16:17:52 +0100443/**
444 * \brief Perform a known-size multiplication
445 *
Gabor Mezeid6260512023-04-03 17:32:55 +0200446 * \p X may not be aliased to any of the inputs for this function.
Gabor Mezei6f182c32023-03-31 16:09:28 +0200447 * \p A may be aliased to \p B.
448 *
Tom Cosgrove6af26f32022-08-24 16:40:55 +0100449 * \param[out] X The pointer to the (little-endian) array to receive
450 * the product of \p A_limbs and \p B_limbs.
451 * This must be of length \p A_limbs + \p B_limbs.
452 * \param[in] A The pointer to the (little-endian) array
453 * representing the first factor.
454 * \param A_limbs The number of limbs in \p A.
455 * \param[in] B The pointer to the (little-endian) array
456 * representing the second factor.
457 * \param B_limbs The number of limbs in \p B.
Hanno Becker4ae890b2022-08-24 16:17:52 +0100458 */
Tom Cosgrove6af26f32022-08-24 16:40:55 +0100459void mbedtls_mpi_core_mul(mbedtls_mpi_uint *X,
460 const mbedtls_mpi_uint *A, size_t A_limbs,
461 const mbedtls_mpi_uint *B, size_t B_limbs);
Hanno Becker4ae890b2022-08-24 16:17:52 +0100462
Tom Cosgroveb4964862022-08-30 11:57:22 +0100463/**
464 * \brief Calculate initialisation value for fast Montgomery modular
465 * multiplication
466 *
467 * \param[in] N Little-endian presentation of the modulus. This must have
468 * at least one limb.
469 *
470 * \return The initialisation value for fast Montgomery modular multiplication
471 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100472mbedtls_mpi_uint mbedtls_mpi_core_montmul_init(const mbedtls_mpi_uint *N);
Tom Cosgroveb4964862022-08-30 11:57:22 +0100473
474/**
Tom Cosgrove4386ead2022-09-29 14:40:21 +0100475 * \brief Montgomery multiplication: X = A * B * R^-1 mod N (HAC 14.36)
476 *
477 * \p A and \p B must be in canonical form. That is, < \p N.
Tom Cosgroveb4964862022-08-30 11:57:22 +0100478 *
Tom Cosgroveea45c1d2022-09-20 13:17:51 +0100479 * \p X may be aliased to \p A or \p N, or even \p B (if \p AN_limbs ==
480 * \p B_limbs) but may not overlap any parameters otherwise.
481 *
Tom Cosgrove4386ead2022-09-29 14:40:21 +0100482 * \p A and \p B may alias each other, if \p AN_limbs == \p B_limbs. They may
483 * not alias \p N (since they must be in canonical form, they cannot == \p N).
Tom Cosgroveea45c1d2022-09-20 13:17:51 +0100484 *
Tom Cosgroveb4964862022-08-30 11:57:22 +0100485 * \param[out] X The destination MPI, as a little-endian array of
486 * length \p AN_limbs.
487 * On successful completion, X contains the result of
Tom Cosgrove630110a2022-08-31 17:09:29 +0100488 * the multiplication `A * B * R^-1` mod N where
489 * `R = 2^(biL*AN_limbs)`.
Tom Cosgroveb4964862022-08-30 11:57:22 +0100490 * \param[in] A Little-endian presentation of first operand.
Tom Cosgrove5dd97e62022-08-30 14:31:49 +0100491 * Must have the same number of limbs as \p N.
Tom Cosgroveb4964862022-08-30 11:57:22 +0100492 * \param[in] B Little-endian presentation of second operand.
493 * \param[in] B_limbs The number of limbs in \p B.
Tom Cosgrove5dd97e62022-08-30 14:31:49 +0100494 * Must be <= \p AN_limbs.
Tom Cosgroveb4964862022-08-30 11:57:22 +0100495 * \param[in] N Little-endian presentation of the modulus.
Tom Cosgrove5dd97e62022-08-30 14:31:49 +0100496 * This must be odd, and have exactly the same number
497 * of limbs as \p A.
Tom Cosgrove6da3a3b2022-09-29 17:20:18 +0100498 * It may alias \p X, but must not alias or otherwise
499 * overlap any of the other parameters.
Tom Cosgrove5dd97e62022-08-30 14:31:49 +0100500 * \param[in] AN_limbs The number of limbs in \p X, \p A and \p N.
Tom Cosgrove630110a2022-08-31 17:09:29 +0100501 * \param mm The Montgomery constant for \p N: -N^-1 mod 2^biL.
Tom Cosgroveb7438d12022-09-15 15:05:59 +0100502 * This can be calculated by `mbedtls_mpi_core_montmul_init()`.
Tom Cosgroveb4964862022-08-30 11:57:22 +0100503 * \param[in,out] T Temporary storage of size at least 2*AN_limbs+1 limbs.
504 * Its initial content is unused and
505 * its final content is indeterminate.
Tom Cosgrove4386ead2022-09-29 14:40:21 +0100506 * It must not alias or otherwise overlap any of the
507 * other parameters.
Tom Cosgroveb4964862022-08-30 11:57:22 +0100508 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100509void mbedtls_mpi_core_montmul(mbedtls_mpi_uint *X,
510 const mbedtls_mpi_uint *A,
511 const mbedtls_mpi_uint *B, size_t B_limbs,
512 const mbedtls_mpi_uint *N, size_t AN_limbs,
513 mbedtls_mpi_uint mm, mbedtls_mpi_uint *T);
Tom Cosgroveb4964862022-08-30 11:57:22 +0100514
Hanno Beckerec440f22022-08-11 17:29:32 +0100515/**
Minos Galanakisb85506e2022-10-20 09:51:53 +0100516 * \brief Calculate the square of the Montgomery constant. (Needed
517 * for conversion and operations in Montgomery form.)
Hanno Beckerec440f22022-08-11 17:29:32 +0100518 *
519 * \param[out] X A pointer to the result of the calculation of
Minos Galanakisb85506e2022-10-20 09:51:53 +0100520 * the square of the Montgomery constant:
521 * 2^{2*n*biL} mod N.
Hanno Beckerec440f22022-08-11 17:29:32 +0100522 * \param[in] N Little-endian presentation of the modulus, which must be odd.
523 *
524 * \return 0 if successful.
525 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if there is not enough space
526 * to store the value of Montgomery constant squared.
527 * \return #MBEDTLS_ERR_MPI_DIVISION_BY_ZERO if \p N modulus is zero.
528 * \return #MBEDTLS_ERR_MPI_NEGATIVE_VALUE if \p N modulus is negative.
Hanno Beckerec440f22022-08-11 17:29:32 +0100529 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100530int mbedtls_mpi_core_get_mont_r2_unsafe(mbedtls_mpi *X,
531 const mbedtls_mpi *N);
Hanno Beckerec440f22022-08-11 17:29:32 +0100532
Janos Follath59cbd1b2022-10-28 18:13:43 +0100533#if defined(MBEDTLS_TEST_HOOKS)
Janos Follathe50f2f12022-10-26 15:14:33 +0100534/**
Janos Follath8904a2d2022-10-31 15:32:28 +0000535 * Copy an MPI from a table without leaking the index.
Janos Follathe50f2f12022-10-26 15:14:33 +0100536 *
537 * \param dest The destination buffer. This must point to a writable
538 * buffer of at least \p limbs limbs.
539 * \param table The address of the table. This must point to a readable
Janos Follath8904a2d2022-10-31 15:32:28 +0000540 * array of \p count elements of \p limbs limbs each.
541 * \param limbs The number of limbs in each table entry.
542 * \param count The number of entries in \p table.
543 * \param index The (secret) table index to look up. This must be in the
544 * range `0 .. count-1`.
Janos Follathe50f2f12022-10-26 15:14:33 +0100545 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100546void mbedtls_mpi_core_ct_uint_table_lookup(mbedtls_mpi_uint *dest,
547 const mbedtls_mpi_uint *table,
548 size_t limbs,
549 size_t count,
550 size_t index);
Janos Follath59cbd1b2022-10-28 18:13:43 +0100551#endif /* MBEDTLS_TEST_HOOKS */
Janos Follathe50f2f12022-10-26 15:14:33 +0100552
Gilles Peskine909e03c2022-10-18 18:14:33 +0200553/**
554 * \brief Fill an integer with a number of random bytes.
555 *
556 * \param X The destination MPI.
557 * \param X_limbs The number of limbs of \p X.
558 * \param bytes The number of random bytes to generate.
559 * \param f_rng The RNG function to use. This must not be \c NULL.
560 * \param p_rng The RNG parameter to be passed to \p f_rng. This may be
561 * \c NULL if \p f_rng doesn't need a context argument.
562 *
563 * \return \c 0 if successful.
564 * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if \p X does not have
565 * enough room for \p bytes bytes.
566 * \return A negative error code on RNG failure.
567 *
568 * \note The bytes obtained from the RNG are interpreted
569 * as a big-endian representation of an MPI; this can
570 * be relevant in applications like deterministic ECDSA.
571 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100572int mbedtls_mpi_core_fill_random(mbedtls_mpi_uint *X, size_t X_limbs,
573 size_t bytes,
574 int (*f_rng)(void *, unsigned char *, size_t),
575 void *p_rng);
Gilles Peskine909e03c2022-10-18 18:14:33 +0200576
Gilles Peskine4a8c5cd2022-10-18 18:15:01 +0200577/** Generate a random number uniformly in a range.
578 *
579 * This function generates a random number between \p min inclusive and
580 * \p N exclusive.
581 *
582 * The procedure complies with RFC 6979 §3.3 (deterministic ECDSA)
583 * when the RNG is a suitably parametrized instance of HMAC_DRBG
584 * and \p min is \c 1.
585 *
586 * \note There are `N - min` possible outputs. The lower bound
587 * \p min can be reached, but the upper bound \p N cannot.
588 *
589 * \param X The destination MPI, with \p limbs limbs.
590 * It must not be aliased with \p N or otherwise overlap it.
591 * \param min The minimum value to return.
592 * \param N The upper bound of the range, exclusive, with \p limbs limbs.
593 * In other words, this is one plus the maximum value to return.
594 * \p N must be strictly larger than \p min.
595 * \param limbs The number of limbs of \p N and \p X.
596 * This must not be 0.
597 * \param f_rng The RNG function to use. This must not be \c NULL.
598 * \param p_rng The RNG parameter to be passed to \p f_rng.
599 *
600 * \return \c 0 if successful.
601 * \return #MBEDTLS_ERR_MPI_NOT_ACCEPTABLE if the implementation was
602 * unable to find a suitable value within a limited number
603 * of attempts. This has a negligible probability if \p N
604 * is significantly larger than \p min, which is the case
605 * for all usual cryptographic applications.
606 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100607int mbedtls_mpi_core_random(mbedtls_mpi_uint *X,
608 mbedtls_mpi_uint min,
609 const mbedtls_mpi_uint *N,
610 size_t limbs,
611 int (*f_rng)(void *, unsigned char *, size_t),
612 void *p_rng);
Gilles Peskine4a8c5cd2022-10-18 18:15:01 +0200613
Janos Follathb6673f02022-09-30 14:13:14 +0100614/**
Tom Cosgrove0a0dded2022-12-06 14:37:18 +0000615 * \brief Returns the number of limbs of working memory required for
616 * a call to `mbedtls_mpi_core_exp_mod()`.
Janos Follathb6673f02022-09-30 14:13:14 +0100617 *
Tom Cosgrove28ff92c2022-12-12 17:06:27 +0000618 * \note This will always be at least
619 * `mbedtls_mpi_core_montmul_working_limbs(AN_limbs)`,
620 * i.e. sufficient for a call to `mbedtls_mpi_core_montmul()`.
621 *
Tom Cosgrove0a0dded2022-12-06 14:37:18 +0000622 * \param AN_limbs The number of limbs in the input `A` and the modulus `N`
623 * (they must be the same size) that will be given to
624 * `mbedtls_mpi_core_exp_mod()`.
625 * \param E_limbs The number of limbs in the exponent `E` that will be given
626 * to `mbedtls_mpi_core_exp_mod()`.
Janos Follathb6673f02022-09-30 14:13:14 +0100627 *
Tom Cosgrove0a0dded2022-12-06 14:37:18 +0000628 * \return The number of limbs of working memory required by
629 * `mbedtls_mpi_core_exp_mod()`.
Janos Follathb6673f02022-09-30 14:13:14 +0100630 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100631size_t mbedtls_mpi_core_exp_mod_working_limbs(size_t AN_limbs, size_t E_limbs);
Tom Cosgrove0a0dded2022-12-06 14:37:18 +0000632
633/**
Manuel Pégourié-Gonnard75ed5872024-06-18 12:52:45 +0200634 * \brief Perform a modular exponentiation with public or secret exponent:
635 * X = A^E mod N, where \p A is already in Montgomery form.
636 *
Janos Follath38ff70e2024-08-12 18:20:59 +0100637 * \warning This function is not constant time with respect to \p E (the exponent).
638 *
Manuel Pégourié-Gonnard75ed5872024-06-18 12:52:45 +0200639 * \p X may be aliased to \p A, but not to \p RR or \p E, even if \p E_limbs ==
640 * \p AN_limbs.
641 *
642 * \param[out] X The destination MPI, as a little endian array of length
643 * \p AN_limbs.
644 * \param[in] A The base MPI, as a little endian array of length \p AN_limbs.
645 * Must be in Montgomery form.
646 * \param[in] N The modulus, as a little endian array of length \p AN_limbs.
647 * \param AN_limbs The number of limbs in \p X, \p A, \p N, \p RR.
648 * \param[in] E The exponent, as a little endian array of length \p E_limbs.
649 * \param E_limbs The number of limbs in \p E.
650 * \param[in] RR The precomputed residue of 2^{2*biL} modulo N, as a little
651 * endian array of length \p AN_limbs.
652 * \param[in,out] T Temporary storage of at least the number of limbs returned
653 * by `mbedtls_mpi_core_exp_mod_working_limbs()`.
654 * Its initial content is unused and its final content is
655 * indeterminate.
656 * It must not alias or otherwise overlap any of the other
657 * parameters.
658 * It is up to the caller to zeroize \p T when it is no
659 * longer needed, and before freeing it if it was dynamically
660 * allocated.
Manuel Pégourié-Gonnard75ed5872024-06-18 12:52:45 +0200661 */
Janos Follath38ff70e2024-08-12 18:20:59 +0100662void mbedtls_mpi_core_exp_mod_unsafe(mbedtls_mpi_uint *X,
663 const mbedtls_mpi_uint *A,
664 const mbedtls_mpi_uint *N, size_t AN_limbs,
665 const mbedtls_mpi_uint *E, size_t E_limbs,
666 const mbedtls_mpi_uint *RR,
667 mbedtls_mpi_uint *T);
Manuel Pégourié-Gonnard75ed5872024-06-18 12:52:45 +0200668
669/**
Tom Cosgrove0a0dded2022-12-06 14:37:18 +0000670 * \brief Perform a modular exponentiation with secret exponent:
671 * X = A^E mod N, where \p A is already in Montgomery form.
672 *
Tom Cosgrovea7f0d7b2022-12-07 13:29:07 +0000673 * \p X may be aliased to \p A, but not to \p RR or \p E, even if \p E_limbs ==
674 * \p AN_limbs.
675 *
Tom Cosgrove0a0dded2022-12-06 14:37:18 +0000676 * \param[out] X The destination MPI, as a little endian array of length
677 * \p AN_limbs.
678 * \param[in] A The base MPI, as a little endian array of length \p AN_limbs.
679 * Must be in Montgomery form.
680 * \param[in] N The modulus, as a little endian array of length \p AN_limbs.
681 * \param AN_limbs The number of limbs in \p X, \p A, \p N, \p RR.
682 * \param[in] E The exponent, as a little endian array of length \p E_limbs.
683 * \param E_limbs The number of limbs in \p E.
684 * \param[in] RR The precomputed residue of 2^{2*biL} modulo N, as a little
685 * endian array of length \p AN_limbs.
686 * \param[in,out] T Temporary storage of at least the number of limbs returned
687 * by `mbedtls_mpi_core_exp_mod_working_limbs()`.
688 * Its initial content is unused and its final content is
689 * indeterminate.
690 * It must not alias or otherwise overlap any of the other
691 * parameters.
692 * It is up to the caller to zeroize \p T when it is no
693 * longer needed, and before freeing it if it was dynamically
694 * allocated.
695 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100696void mbedtls_mpi_core_exp_mod(mbedtls_mpi_uint *X,
697 const mbedtls_mpi_uint *A,
698 const mbedtls_mpi_uint *N, size_t AN_limbs,
699 const mbedtls_mpi_uint *E, size_t E_limbs,
700 const mbedtls_mpi_uint *RR,
701 mbedtls_mpi_uint *T);
Janos Follathb6673f02022-09-30 14:13:14 +0100702
Hanno Beckerd9b23482022-08-25 08:25:19 +0100703/**
704 * \brief Subtract unsigned integer from known-size large unsigned integers.
705 * Return the borrow.
706 *
Tom Cosgrovef7ff4c92022-08-25 08:39:07 +0100707 * \param[out] X The result of the subtraction.
708 * \param[in] A The left operand.
709 * \param b The unsigned scalar to subtract.
710 * \param limbs Number of limbs of \p X and \p A.
Hanno Beckerd9b23482022-08-25 08:25:19 +0100711 *
Tom Cosgrovef7ff4c92022-08-25 08:39:07 +0100712 * \return 1 if `A < b`.
713 * 0 if `A >= b`.
Hanno Beckerd9b23482022-08-25 08:25:19 +0100714 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100715mbedtls_mpi_uint mbedtls_mpi_core_sub_int(mbedtls_mpi_uint *X,
716 const mbedtls_mpi_uint *A,
717 mbedtls_mpi_uint b,
718 size_t limbs);
Hanno Beckerd9b23482022-08-25 08:25:19 +0100719
Tom Cosgrove30f3b4d2022-12-12 16:54:57 +0000720/**
721 * \brief Determine if a given MPI has the value \c 0 in constant time with
722 * respect to the value (but not with respect to the number of limbs).
723 *
724 * \param[in] A The MPI to test.
725 * \param limbs Number of limbs in \p A.
726 *
Janos Follathaec1a862024-02-21 11:24:20 +0000727 * \return MBEDTLS_CT_FALSE if `A == 0`
728 * MBEDTLS_CT_TRUE if `A != 0`.
Tom Cosgrove30f3b4d2022-12-12 16:54:57 +0000729 */
Janos Follathadb9d2d2024-03-11 10:03:05 +0000730mbedtls_ct_condition_t mbedtls_mpi_core_check_zero_ct(const mbedtls_mpi_uint *A,
Janos Follathaec1a862024-02-21 11:24:20 +0000731 size_t limbs);
Tom Cosgrove30f3b4d2022-12-12 16:54:57 +0000732
Tom Cosgrove28ff92c2022-12-12 17:06:27 +0000733/**
734 * \brief Returns the number of limbs of working memory required for
735 * a call to `mbedtls_mpi_core_montmul()`.
736 *
737 * \param AN_limbs The number of limbs in the input `A` and the modulus `N`
738 * (they must be the same size) that will be given to
739 * `mbedtls_mpi_core_montmul()` or one of the other functions
740 * that specifies this as the amount of working memory needed.
741 *
742 * \return The number of limbs of working memory required by
743 * `mbedtls_mpi_core_montmul()` (or other similar function).
744 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100745static inline size_t mbedtls_mpi_core_montmul_working_limbs(size_t AN_limbs)
Tom Cosgrove28ff92c2022-12-12 17:06:27 +0000746{
Gilles Peskine449bd832023-01-11 14:50:10 +0100747 return 2 * AN_limbs + 1;
Tom Cosgrove28ff92c2022-12-12 17:06:27 +0000748}
749
Tom Cosgrove786848b2022-12-13 10:45:19 +0000750/** Convert an MPI into Montgomery form.
751 *
752 * \p X may be aliased to \p A, but may not otherwise overlap it.
753 *
Tom Cosgrove5c8505f2023-03-07 11:39:52 +0000754 * \p X may not alias \p N (it is in canonical form, so must be strictly less
Tom Cosgrove786848b2022-12-13 10:45:19 +0000755 * than \p N). Nor may it alias or overlap \p rr (this is unlikely to be
756 * required in practice.)
757 *
758 * This function is a thin wrapper around `mbedtls_mpi_core_montmul()` that is
759 * an alternative to calling `mbedtls_mpi_mod_raw_to_mont_rep()` when we
760 * don't want to allocate memory.
761 *
762 * \param[out] X The result of the conversion.
763 * Must have the same number of limbs as \p A.
764 * \param[in] A The MPI to convert into Montgomery form.
765 * Must have the same number of limbs as the modulus.
766 * \param[in] N The address of the modulus, which gives the size of
Tom Cosgrovef7237542022-12-16 16:10:36 +0000767 * the base `R` = 2^(biL*N->limbs).
Tom Cosgrove786848b2022-12-13 10:45:19 +0000768 * \param[in] AN_limbs The number of limbs in \p X, \p A, \p N and \p rr.
769 * \param mm The Montgomery constant for \p N: -N^-1 mod 2^biL.
Tom Cosgroveb38c2ed2022-12-14 13:11:46 +0000770 * This can be determined by calling
Tom Cosgrove786848b2022-12-13 10:45:19 +0000771 * `mbedtls_mpi_core_montmul_init()`.
772 * \param[in] rr The residue for `2^{2*n*biL} mod N`.
773 * \param[in,out] T Temporary storage of size at least
774 * `mbedtls_mpi_core_montmul_working_limbs(AN_limbs)`
775 * limbs.
776 * Its initial content is unused and
777 * its final content is indeterminate.
778 * It must not alias or otherwise overlap any of the
779 * other parameters.
780 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100781void mbedtls_mpi_core_to_mont_rep(mbedtls_mpi_uint *X,
782 const mbedtls_mpi_uint *A,
783 const mbedtls_mpi_uint *N,
784 size_t AN_limbs,
785 mbedtls_mpi_uint mm,
786 const mbedtls_mpi_uint *rr,
787 mbedtls_mpi_uint *T);
Tom Cosgrove786848b2022-12-13 10:45:19 +0000788
789/** Convert an MPI from Montgomery form.
790 *
791 * \p X may be aliased to \p A, but may not otherwise overlap it.
792 *
Tom Cosgrove5c8505f2023-03-07 11:39:52 +0000793 * \p X may not alias \p N (it is in canonical form, so must be strictly less
Tom Cosgrove786848b2022-12-13 10:45:19 +0000794 * than \p N).
795 *
796 * This function is a thin wrapper around `mbedtls_mpi_core_montmul()` that is
797 * an alternative to calling `mbedtls_mpi_mod_raw_from_mont_rep()` when we
798 * don't want to allocate memory.
799 *
800 * \param[out] X The result of the conversion.
801 * Must have the same number of limbs as \p A.
802 * \param[in] A The MPI to convert from Montgomery form.
803 * Must have the same number of limbs as the modulus.
804 * \param[in] N The address of the modulus, which gives the size of
Tom Cosgrovef7237542022-12-16 16:10:36 +0000805 * the base `R` = 2^(biL*N->limbs).
Tom Cosgrove786848b2022-12-13 10:45:19 +0000806 * \param[in] AN_limbs The number of limbs in \p X, \p A and \p N.
807 * \param mm The Montgomery constant for \p N: -N^-1 mod 2^biL.
Tom Cosgroveb38c2ed2022-12-14 13:11:46 +0000808 * This can be determined by calling
Tom Cosgrove786848b2022-12-13 10:45:19 +0000809 * `mbedtls_mpi_core_montmul_init()`.
810 * \param[in,out] T Temporary storage of size at least
811 * `mbedtls_mpi_core_montmul_working_limbs(AN_limbs)`
812 * limbs.
813 * Its initial content is unused and
814 * its final content is indeterminate.
815 * It must not alias or otherwise overlap any of the
816 * other parameters.
817 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100818void mbedtls_mpi_core_from_mont_rep(mbedtls_mpi_uint *X,
819 const mbedtls_mpi_uint *A,
820 const mbedtls_mpi_uint *N,
821 size_t AN_limbs,
822 mbedtls_mpi_uint mm,
823 mbedtls_mpi_uint *T);
Tom Cosgrove786848b2022-12-13 10:45:19 +0000824
Gabor Mezeib9030702022-07-18 23:09:45 +0200825#endif /* MBEDTLS_BIGNUM_CORE_H */