blob: e5500f117aa6636034d70f981d5543a97237e374 [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
65 * SPDX-License-Identifier: Apache-2.0
66 *
67 * Licensed under the Apache License, Version 2.0 (the "License"); you may
68 * not use this file except in compliance with the License.
69 * You may obtain a copy of the License at
70 *
71 * http://www.apache.org/licenses/LICENSE-2.0
72 *
73 * Unless required by applicable law or agreed to in writing, software
74 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
75 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
76 * See the License for the specific language governing permissions and
77 * limitations under the License.
78 */
79
80#ifndef MBEDTLS_BIGNUM_CORE_H
81#define MBEDTLS_BIGNUM_CORE_H
82
83#include "common.h"
84
85#if defined(MBEDTLS_BIGNUM_C)
86#include "mbedtls/bignum.h"
87#endif
88
Dave Rodgmancd2e38b2023-05-17 13:31:55 +010089#include "constant_time_internal.h"
90
Gilles Peskine449bd832023-01-11 14:50:10 +010091#define ciL (sizeof(mbedtls_mpi_uint)) /** chars in limb */
92#define biL (ciL << 3) /** bits in limb */
93#define biH (ciL << 2) /** half limb size */
Tom Cosgrove5eefc3d2022-08-31 17:16:50 +010094
95/*
96 * Convert between bits/chars and number of limbs
97 * Divide first in order to avoid potential overflows
98 */
Gilles Peskine449bd832023-01-11 14:50:10 +010099#define BITS_TO_LIMBS(i) ((i) / biL + ((i) % biL != 0))
100#define CHARS_TO_LIMBS(i) ((i) / ciL + ((i) % ciL != 0))
Tom Cosgrove5eefc3d2022-08-31 17:16:50 +0100101/* Get a specific byte, without range checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100102#define GET_BYTE(X, i) \
103 (((X)[(i) / ciL] >> (((i) % ciL) * 8)) & 0xff)
Tom Cosgrove5eefc3d2022-08-31 17:16:50 +0100104
Gabor Mezei37b06362022-08-02 17:22:18 +0200105/** Count leading zero bits in a given integer.
106 *
Dave Rodgman0f16d562023-04-24 12:53:29 +0100107 * \warning The result is undefined if \p a == 0
108 *
Janos Follathb7a88ec2022-08-19 12:24:40 +0100109 * \param a Integer to count leading zero bits.
Gabor Mezei37b06362022-08-02 17:22:18 +0200110 *
Dave Rodgman0f16d562023-04-24 12:53:29 +0100111 * \return The number of leading zero bits in \p a, if \p a != 0.
112 * If \p a == 0, the result is undefined.
Gabor Mezei37b06362022-08-02 17:22:18 +0200113 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100114size_t mbedtls_mpi_core_clz(mbedtls_mpi_uint a);
Janos Follath4670f882022-07-21 18:25:42 +0100115
Janos Follatha95f2042022-08-19 12:09:17 +0100116/** Return the minimum number of bits required to represent the value held
Janos Follath63184682022-08-11 17:42:59 +0100117 * in the MPI.
118 *
Janos Follathb7a88ec2022-08-19 12:24:40 +0100119 * \note This function returns 0 if all the limbs of \p A are 0.
Gabor Mezei37b06362022-08-02 17:22:18 +0200120 *
Janos Follathb7a88ec2022-08-19 12:24:40 +0100121 * \param[in] A The address of the MPI.
Janos Follathc4596412022-08-22 10:01:27 +0100122 * \param A_limbs The number of limbs of \p A.
Gabor Mezei37b06362022-08-02 17:22:18 +0200123 *
Janos Follathb7a88ec2022-08-19 12:24:40 +0100124 * \return The number of bits in \p A.
Gabor Mezei37b06362022-08-02 17:22:18 +0200125 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100126size_t mbedtls_mpi_core_bitlen(const mbedtls_mpi_uint *A, size_t A_limbs);
Janos Follath4670f882022-07-21 18:25:42 +0100127
Gabor Mezei37b06362022-08-02 17:22:18 +0200128/** Convert a big-endian byte array aligned to the size of mbedtls_mpi_uint
129 * into the storage form used by mbedtls_mpi.
130 *
Janos Follathb7a88ec2022-08-19 12:24:40 +0100131 * \param[in,out] A The address of the MPI.
Janos Follathc4596412022-08-22 10:01:27 +0100132 * \param A_limbs The number of limbs of \p A.
Gabor Mezei37b06362022-08-02 17:22:18 +0200133 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100134void mbedtls_mpi_core_bigendian_to_host(mbedtls_mpi_uint *A,
135 size_t A_limbs);
Janos Follath4670f882022-07-21 18:25:42 +0100136
Gilles Peskine6f949ea2022-09-20 18:38:35 +0200137/** \brief Compare a machine integer with an MPI.
138 *
139 * This function operates in constant time with respect
140 * to the values of \p min and \p A.
141 *
142 * \param min A machine integer.
143 * \param[in] A An MPI.
144 * \param A_limbs The number of limbs of \p A.
145 * This must be at least 1.
146 *
Dave Rodgmanfd7fab42023-05-17 14:00:39 +0100147 * \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 +0200148 */
Dave Rodgmanfd7fab42023-05-17 14:00:39 +0100149mbedtls_ct_condition_t mbedtls_mpi_core_uint_le_mpi(mbedtls_mpi_uint min,
150 const mbedtls_mpi_uint *A,
151 size_t A_limbs);
Gilles Peskine6f949ea2022-09-20 18:38:35 +0200152
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200153/**
Dave Rodgman7d4f0192023-05-09 14:01:05 +0100154 * \brief Check if one unsigned MPI is less than another in constant
155 * time.
156 *
157 * \param A The left-hand MPI. This must point to an array of limbs
158 * with the same allocated length as \p B.
159 * \param B The right-hand MPI. This must point to an array of limbs
160 * with the same allocated length as \p A.
161 * \param limbs The number of limbs in \p A and \p B.
162 * This must not be 0.
163 *
Dave Rodgman8ac9a1d2023-05-17 15:16:22 +0100164 * \return MBEDTLS_CT_TRUE if \p A is less than \p B.
165 * MBEDTLS_CT_FALSE if \p A is greater than or equal to \p B.
Dave Rodgman7d4f0192023-05-09 14:01:05 +0100166 */
Dave Rodgman8ac9a1d2023-05-17 15:16:22 +0100167mbedtls_ct_condition_t mbedtls_mpi_core_lt_ct(const mbedtls_mpi_uint *A,
168 const mbedtls_mpi_uint *B,
169 size_t limbs);
170
Dave Rodgman7d4f0192023-05-09 14:01:05 +0100171/**
Gabor Mezei4086de62022-10-14 16:29:42 +0200172 * \brief Perform a safe conditional copy of an MPI which doesn't reveal
173 * whether assignment was done or not.
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200174 *
Gabor Mezei4086de62022-10-14 16:29:42 +0200175 * \param[out] X The address of the destination MPI.
176 * This must be initialized. Must have enough limbs to
177 * store the full value of \p A.
178 * \param[in] A The address of the source MPI. This must be initialized.
Gabor Mezei1c628d52022-09-27 12:13:51 +0200179 * \param limbs The number of limbs of \p A.
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200180 * \param assign The condition deciding whether to perform the
Dave Rodgmanfb1b8512023-07-31 12:27:05 +0100181 * assignment or not. Callers will need to use
182 * the constant time interface (e.g. `mbedtls_ct_bool()`)
183 * to construct this argument.
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200184 *
185 * \note This function avoids leaking any information about whether
186 * the assignment was done or not.
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200187 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100188void mbedtls_mpi_core_cond_assign(mbedtls_mpi_uint *X,
189 const mbedtls_mpi_uint *A,
190 size_t limbs,
Dave Rodgmancd2e38b2023-05-17 13:31:55 +0100191 mbedtls_ct_condition_t assign);
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200192
193/**
Gabor Mezei4086de62022-10-14 16:29:42 +0200194 * \brief Perform a safe conditional swap of two MPIs which doesn't reveal
195 * whether the swap was done or not.
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200196 *
Gabor Mezei86dfe382022-09-30 14:03:04 +0200197 * \param[in,out] X The address of the first MPI.
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200198 * This must be initialized.
Gabor Mezei86dfe382022-09-30 14:03:04 +0200199 * \param[in,out] Y The address of the second MPI.
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200200 * This must be initialized.
Gabor Mezeie5b85852022-09-30 13:54:02 +0200201 * \param limbs The number of limbs of \p X and \p Y.
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200202 * \param swap The condition deciding whether to perform
Dave Rodgmancd2e38b2023-05-17 13:31:55 +0100203 * the swap or not.
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200204 *
205 * \note This function avoids leaking any information about whether
206 * the swap was done or not.
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200207 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100208void mbedtls_mpi_core_cond_swap(mbedtls_mpi_uint *X,
209 mbedtls_mpi_uint *Y,
210 size_t limbs,
Dave Rodgmancd2e38b2023-05-17 13:31:55 +0100211 mbedtls_ct_condition_t swap);
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200212
Janos Follathaf3f39c2022-08-22 09:06:32 +0100213/** Import X from unsigned binary data, little-endian.
Gabor Mezei37b06362022-08-02 17:22:18 +0200214 *
Janos Follath63184682022-08-11 17:42:59 +0100215 * The MPI needs to have enough limbs to store the full value (including any
216 * most significant zero bytes in the input).
Gabor Mezei37b06362022-08-02 17:22:18 +0200217 *
Janos Follathb7a88ec2022-08-19 12:24:40 +0100218 * \param[out] X The address of the MPI.
219 * \param X_limbs The number of limbs of \p X.
220 * \param[in] input The input buffer to import from.
221 * \param input_length The length bytes of \p input.
Gabor Mezei37b06362022-08-02 17:22:18 +0200222 *
223 * \return \c 0 if successful.
224 * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p X isn't
Janos Follathb7a88ec2022-08-19 12:24:40 +0100225 * large enough to hold the value in \p input.
Gabor Mezei37b06362022-08-02 17:22:18 +0200226 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100227int mbedtls_mpi_core_read_le(mbedtls_mpi_uint *X,
228 size_t X_limbs,
229 const unsigned char *input,
230 size_t input_length);
Gabor Mezeib9030702022-07-18 23:09:45 +0200231
Janos Follathaf3f39c2022-08-22 09:06:32 +0100232/** Import X from unsigned binary data, big-endian.
Gabor Mezei37b06362022-08-02 17:22:18 +0200233 *
Janos Follath63184682022-08-11 17:42:59 +0100234 * The MPI needs to have enough limbs to store the full value (including any
235 * most significant zero bytes in the input).
Gabor Mezei37b06362022-08-02 17:22:18 +0200236 *
Janos Follathb7a88ec2022-08-19 12:24:40 +0100237 * \param[out] X The address of the MPI.
Tom Cosgrove8a1f7842023-02-01 08:43:54 +0000238 * May only be #NULL if \p X_limbs is 0 and \p input_length
Janos Follathb7a88ec2022-08-19 12:24:40 +0100239 * is 0.
240 * \param X_limbs The number of limbs of \p X.
241 * \param[in] input The input buffer to import from.
242 * May only be #NULL if \p input_length is 0.
243 * \param input_length The length in bytes of \p input.
Gabor Mezei37b06362022-08-02 17:22:18 +0200244 *
245 * \return \c 0 if successful.
246 * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p X isn't
Janos Follathb7a88ec2022-08-19 12:24:40 +0100247 * large enough to hold the value in \p input.
Gabor Mezei37b06362022-08-02 17:22:18 +0200248 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100249int mbedtls_mpi_core_read_be(mbedtls_mpi_uint *X,
250 size_t X_limbs,
251 const unsigned char *input,
252 size_t input_length);
Gabor Mezeib9030702022-07-18 23:09:45 +0200253
Janos Follathaf3f39c2022-08-22 09:06:32 +0100254/** Export A into unsigned binary data, little-endian.
Gabor Mezei37b06362022-08-02 17:22:18 +0200255 *
Janos Follathb7a88ec2022-08-19 12:24:40 +0100256 * \note If \p output is shorter than \p A the export is still successful if the
257 * value held in \p A fits in the buffer (that is, if enough of the most
258 * significant bytes of \p A are 0).
Janos Follath63184682022-08-11 17:42:59 +0100259 *
Janos Follathb7a88ec2022-08-19 12:24:40 +0100260 * \param[in] A The address of the MPI.
261 * \param A_limbs The number of limbs of \p A.
262 * \param[out] output The output buffer to export to.
263 * \param output_length The length in bytes of \p output.
Gabor Mezei37b06362022-08-02 17:22:18 +0200264 *
265 * \return \c 0 if successful.
Janos Follathb7a88ec2022-08-19 12:24:40 +0100266 * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p output isn't
267 * large enough to hold the value of \p A.
Gabor Mezei37b06362022-08-02 17:22:18 +0200268 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100269int mbedtls_mpi_core_write_le(const mbedtls_mpi_uint *A,
270 size_t A_limbs,
271 unsigned char *output,
272 size_t output_length);
Gabor Mezeib9030702022-07-18 23:09:45 +0200273
Janos Follathaf3f39c2022-08-22 09:06:32 +0100274/** Export A into unsigned binary data, big-endian.
Gabor Mezei37b06362022-08-02 17:22:18 +0200275 *
Janos Follathb7a88ec2022-08-19 12:24:40 +0100276 * \note If \p output is shorter than \p A the export is still successful if the
277 * value held in \p A fits in the buffer (that is, if enough of the most
278 * significant bytes of \p A are 0).
Janos Follath63184682022-08-11 17:42:59 +0100279 *
Janos Follathb7a88ec2022-08-19 12:24:40 +0100280 * \param[in] A The address of the MPI.
281 * \param A_limbs The number of limbs of \p A.
282 * \param[out] output The output buffer to export to.
283 * \param output_length The length in bytes of \p output.
Gabor Mezei37b06362022-08-02 17:22:18 +0200284 *
285 * \return \c 0 if successful.
Janos Follathb7a88ec2022-08-19 12:24:40 +0100286 * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p output isn't
287 * large enough to hold the value of \p A.
Gabor Mezei37b06362022-08-02 17:22:18 +0200288 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100289int mbedtls_mpi_core_write_be(const mbedtls_mpi_uint *A,
290 size_t A_limbs,
291 unsigned char *output,
292 size_t output_length);
Gabor Mezeib9030702022-07-18 23:09:45 +0200293
Minos Galanakisec09e252023-04-20 14:22:16 +0100294/** \brief Shift an MPI in-place right by a number of bits.
Gilles Peskine66414202022-09-21 15:36:16 +0200295 *
296 * Shifting by more bits than there are bit positions
297 * in \p X is valid and results in setting \p X to 0.
298 *
299 * This function's execution time depends on the value
300 * of \p count (and of course \p limbs).
301 *
302 * \param[in,out] X The number to shift.
303 * \param limbs The number of limbs of \p X. This must be at least 1.
304 * \param count The number of bits to shift by.
305 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100306void mbedtls_mpi_core_shift_r(mbedtls_mpi_uint *X, size_t limbs,
307 size_t count);
Gilles Peskine66414202022-09-21 15:36:16 +0200308
Tom Cosgrove90c426b2022-08-23 16:15:19 +0100309/**
Minos Galanakisec09e252023-04-20 14:22:16 +0100310 * \brief Shift an MPI in-place left by a number of bits.
Minos Galanakisad808dd2023-04-20 12:18:41 +0100311 *
Minos Galanakisec09e252023-04-20 14:22:16 +0100312 * Shifting by more bits than there are bit positions
Minos Galanakisb8944032023-04-28 14:09:44 +0100313 * in \p X will produce an unspecified result.
Minos Galanakisad808dd2023-04-20 12:18:41 +0100314 *
Minos Galanakisec09e252023-04-20 14:22:16 +0100315 * This function's execution time depends on the value
316 * of \p count (and of course \p limbs).
317 * \param[in,out] X The number to shift.
318 * \param limbs The number of limbs of \p X. This must be at least 1.
319 * \param count The number of bits to shift by.
Minos Galanakisad808dd2023-04-20 12:18:41 +0100320 */
Minos Galanakisec09e252023-04-20 14:22:16 +0100321void mbedtls_mpi_core_shift_l(mbedtls_mpi_uint *X, size_t limbs,
322 size_t count);
Minos Galanakisad808dd2023-04-20 12:18:41 +0100323
324/**
Tom Cosgroveaf7d44b2022-08-24 14:05:26 +0100325 * \brief Add two fixed-size large unsigned integers, returning the carry.
Hanno Beckerc9887132022-08-24 12:54:36 +0100326 *
Tom Cosgroveaf7d44b2022-08-24 14:05:26 +0100327 * Calculates `A + B` where `A` and `B` have the same size.
328 *
Tom Cosgrove82f13102022-10-25 12:46:03 +0100329 * This function operates modulo `2^(biL*limbs)` and returns the carry
Hanno Beckerc9887132022-08-24 12:54:36 +0100330 * (1 if there was a wraparound, and 0 otherwise).
331 *
Tom Cosgroveaf7d44b2022-08-24 14:05:26 +0100332 * \p X may be aliased to \p A or \p B.
Hanno Beckerc9887132022-08-24 12:54:36 +0100333 *
Tom Cosgroveaf7d44b2022-08-24 14:05:26 +0100334 * \param[out] X The result of the addition.
335 * \param[in] A Little-endian presentation of the left operand.
336 * \param[in] B Little-endian presentation of the right operand.
337 * \param limbs Number of limbs of \p X, \p A and \p B.
Hanno Beckerc9887132022-08-24 12:54:36 +0100338 *
Tom Cosgroveaf7d44b2022-08-24 14:05:26 +0100339 * \return 1 if `A + B >= 2^(biL*limbs)`, 0 otherwise.
Hanno Beckerc9887132022-08-24 12:54:36 +0100340 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100341mbedtls_mpi_uint mbedtls_mpi_core_add(mbedtls_mpi_uint *X,
342 const mbedtls_mpi_uint *A,
343 const mbedtls_mpi_uint *B,
344 size_t limbs);
Hanno Beckerc9887132022-08-24 12:54:36 +0100345
Tom Cosgrove90c426b2022-08-23 16:15:19 +0100346/**
Tom Cosgrove5c0e8102022-09-15 15:46:10 +0100347 * \brief Conditional addition of two fixed-size large unsigned integers,
Tom Cosgroved932de82022-08-25 16:43:43 +0100348 * returning the carry.
Hanno Becker71f4b0d2022-08-23 12:09:35 +0100349 *
350 * Functionally equivalent to
351 *
352 * ```
353 * if( cond )
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100354 * X += A;
Hanno Becker71f4b0d2022-08-23 12:09:35 +0100355 * return carry;
356 * ```
357 *
Tom Cosgrove47828232022-09-20 13:51:50 +0100358 * This function operates modulo `2^(biL*limbs)`.
359 *
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100360 * \param[in,out] X The pointer to the (little-endian) array
Tom Cosgrove72594632022-08-24 11:51:58 +0100361 * representing the bignum to accumulate onto.
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100362 * \param[in] A The pointer to the (little-endian) array
Tom Cosgrove72594632022-08-24 11:51:58 +0100363 * representing the bignum to conditionally add
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100364 * to \p X. This may be aliased to \p X but may not
Tom Cosgroveed43c6c2022-08-31 11:35:00 +0100365 * overlap otherwise.
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100366 * \param limbs Number of limbs of \p X and \p A.
Tom Cosgrove72594632022-08-24 11:51:58 +0100367 * \param cond Condition bit dictating whether addition should
368 * happen or not. This must be \c 0 or \c 1.
Hanno Becker71f4b0d2022-08-23 12:09:35 +0100369 *
Tom Cosgroveecbb1242022-08-25 10:13:44 +0100370 * \warning If \p cond is neither 0 nor 1, the result of this function
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100371 * is unspecified, and the resulting value in \p X might be
372 * neither its original value nor \p X + \p A.
Tom Cosgrove72594632022-08-24 11:51:58 +0100373 *
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100374 * \return 1 if `X + cond * A >= 2^(biL*limbs)`, 0 otherwise.
Hanno Becker71f4b0d2022-08-23 12:09:35 +0100375 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100376mbedtls_mpi_uint mbedtls_mpi_core_add_if(mbedtls_mpi_uint *X,
377 const mbedtls_mpi_uint *A,
378 size_t limbs,
379 unsigned cond);
Hanno Becker71f4b0d2022-08-23 12:09:35 +0100380
Tom Cosgroveb4964862022-08-30 11:57:22 +0100381/**
Tom Cosgrove5c0e8102022-09-15 15:46:10 +0100382 * \brief Subtract two fixed-size large unsigned integers, returning the borrow.
Tom Cosgroveb4964862022-08-30 11:57:22 +0100383 *
Tom Cosgrove5dd97e62022-08-30 14:31:49 +0100384 * Calculate `A - B` where \p A and \p B have the same size.
Tom Cosgrove630110a2022-08-31 17:09:29 +0100385 * This function operates modulo `2^(biL*limbs)` and returns the carry
Tom Cosgroveb4964862022-08-30 11:57:22 +0100386 * (1 if there was a wraparound, i.e. if `A < B`, and 0 otherwise).
387 *
Tom Cosgrove5dd97e62022-08-30 14:31:49 +0100388 * \p X may be aliased to \p A or \p B, or even both, but may not overlap
389 * either otherwise.
Tom Cosgroveb4964862022-08-30 11:57:22 +0100390 *
391 * \param[out] X The result of the subtraction.
392 * \param[in] A Little-endian presentation of left operand.
393 * \param[in] B Little-endian presentation of right operand.
394 * \param limbs Number of limbs of \p X, \p A and \p B.
395 *
396 * \return 1 if `A < B`.
397 * 0 if `A >= B`.
398 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100399mbedtls_mpi_uint mbedtls_mpi_core_sub(mbedtls_mpi_uint *X,
400 const mbedtls_mpi_uint *A,
401 const mbedtls_mpi_uint *B,
402 size_t limbs);
Tom Cosgroveb4964862022-08-30 11:57:22 +0100403
404/**
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100405 * \brief Perform a fixed-size multiply accumulate operation: X += b * A
Tom Cosgroveb4964862022-08-30 11:57:22 +0100406 *
Tom Cosgroveb0b77e12022-09-20 13:33:40 +0100407 * \p X may be aliased to \p A (when \p X_limbs == \p A_limbs), but may not
408 * otherwise overlap.
409 *
Tom Cosgrove47828232022-09-20 13:51:50 +0100410 * This function operates modulo `2^(biL*X_limbs)`.
411 *
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100412 * \param[in,out] X The pointer to the (little-endian) array
Tom Cosgroveb4964862022-08-30 11:57:22 +0100413 * representing the bignum to accumulate onto.
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100414 * \param X_limbs The number of limbs of \p X. This must be
415 * at least \p A_limbs.
416 * \param[in] A The pointer to the (little-endian) array
Tom Cosgroveb4964862022-08-30 11:57:22 +0100417 * representing the bignum to multiply with.
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100418 * This may be aliased to \p X but may not overlap
Tom Cosgroveed43c6c2022-08-31 11:35:00 +0100419 * otherwise.
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100420 * \param A_limbs The number of limbs of \p A.
421 * \param b X scalar to multiply with.
Tom Cosgroveb4964862022-08-30 11:57:22 +0100422 *
423 * \return The carry at the end of the operation.
424 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100425mbedtls_mpi_uint mbedtls_mpi_core_mla(mbedtls_mpi_uint *X, size_t X_limbs,
426 const mbedtls_mpi_uint *A, size_t A_limbs,
427 mbedtls_mpi_uint b);
Tom Cosgroveb4964862022-08-30 11:57:22 +0100428
Hanno Becker4ae890b2022-08-24 16:17:52 +0100429/**
430 * \brief Perform a known-size multiplication
431 *
Gabor Mezeid6260512023-04-03 17:32:55 +0200432 * \p X may not be aliased to any of the inputs for this function.
Gabor Mezei6f182c32023-03-31 16:09:28 +0200433 * \p A may be aliased to \p B.
434 *
Tom Cosgrove6af26f32022-08-24 16:40:55 +0100435 * \param[out] X The pointer to the (little-endian) array to receive
436 * the product of \p A_limbs and \p B_limbs.
437 * This must be of length \p A_limbs + \p B_limbs.
438 * \param[in] A The pointer to the (little-endian) array
439 * representing the first factor.
440 * \param A_limbs The number of limbs in \p A.
441 * \param[in] B The pointer to the (little-endian) array
442 * representing the second factor.
443 * \param B_limbs The number of limbs in \p B.
Hanno Becker4ae890b2022-08-24 16:17:52 +0100444 */
Tom Cosgrove6af26f32022-08-24 16:40:55 +0100445void mbedtls_mpi_core_mul(mbedtls_mpi_uint *X,
446 const mbedtls_mpi_uint *A, size_t A_limbs,
447 const mbedtls_mpi_uint *B, size_t B_limbs);
Hanno Becker4ae890b2022-08-24 16:17:52 +0100448
Tom Cosgroveb4964862022-08-30 11:57:22 +0100449/**
450 * \brief Calculate initialisation value for fast Montgomery modular
451 * multiplication
452 *
453 * \param[in] N Little-endian presentation of the modulus. This must have
454 * at least one limb.
455 *
456 * \return The initialisation value for fast Montgomery modular multiplication
457 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100458mbedtls_mpi_uint mbedtls_mpi_core_montmul_init(const mbedtls_mpi_uint *N);
Tom Cosgroveb4964862022-08-30 11:57:22 +0100459
460/**
Tom Cosgrove4386ead2022-09-29 14:40:21 +0100461 * \brief Montgomery multiplication: X = A * B * R^-1 mod N (HAC 14.36)
462 *
463 * \p A and \p B must be in canonical form. That is, < \p N.
Tom Cosgroveb4964862022-08-30 11:57:22 +0100464 *
Tom Cosgroveea45c1d2022-09-20 13:17:51 +0100465 * \p X may be aliased to \p A or \p N, or even \p B (if \p AN_limbs ==
466 * \p B_limbs) but may not overlap any parameters otherwise.
467 *
Tom Cosgrove4386ead2022-09-29 14:40:21 +0100468 * \p A and \p B may alias each other, if \p AN_limbs == \p B_limbs. They may
469 * not alias \p N (since they must be in canonical form, they cannot == \p N).
Tom Cosgroveea45c1d2022-09-20 13:17:51 +0100470 *
Tom Cosgroveb4964862022-08-30 11:57:22 +0100471 * \param[out] X The destination MPI, as a little-endian array of
472 * length \p AN_limbs.
473 * On successful completion, X contains the result of
Tom Cosgrove630110a2022-08-31 17:09:29 +0100474 * the multiplication `A * B * R^-1` mod N where
475 * `R = 2^(biL*AN_limbs)`.
Tom Cosgroveb4964862022-08-30 11:57:22 +0100476 * \param[in] A Little-endian presentation of first operand.
Tom Cosgrove5dd97e62022-08-30 14:31:49 +0100477 * Must have the same number of limbs as \p N.
Tom Cosgroveb4964862022-08-30 11:57:22 +0100478 * \param[in] B Little-endian presentation of second operand.
479 * \param[in] B_limbs The number of limbs in \p B.
Tom Cosgrove5dd97e62022-08-30 14:31:49 +0100480 * Must be <= \p AN_limbs.
Tom Cosgroveb4964862022-08-30 11:57:22 +0100481 * \param[in] N Little-endian presentation of the modulus.
Tom Cosgrove5dd97e62022-08-30 14:31:49 +0100482 * This must be odd, and have exactly the same number
483 * of limbs as \p A.
Tom Cosgrove6da3a3b2022-09-29 17:20:18 +0100484 * It may alias \p X, but must not alias or otherwise
485 * overlap any of the other parameters.
Tom Cosgrove5dd97e62022-08-30 14:31:49 +0100486 * \param[in] AN_limbs The number of limbs in \p X, \p A and \p N.
Tom Cosgrove630110a2022-08-31 17:09:29 +0100487 * \param mm The Montgomery constant for \p N: -N^-1 mod 2^biL.
Tom Cosgroveb7438d12022-09-15 15:05:59 +0100488 * This can be calculated by `mbedtls_mpi_core_montmul_init()`.
Tom Cosgroveb4964862022-08-30 11:57:22 +0100489 * \param[in,out] T Temporary storage of size at least 2*AN_limbs+1 limbs.
490 * Its initial content is unused and
491 * its final content is indeterminate.
Tom Cosgrove4386ead2022-09-29 14:40:21 +0100492 * It must not alias or otherwise overlap any of the
493 * other parameters.
Tom Cosgroveb4964862022-08-30 11:57:22 +0100494 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100495void mbedtls_mpi_core_montmul(mbedtls_mpi_uint *X,
496 const mbedtls_mpi_uint *A,
497 const mbedtls_mpi_uint *B, size_t B_limbs,
498 const mbedtls_mpi_uint *N, size_t AN_limbs,
499 mbedtls_mpi_uint mm, mbedtls_mpi_uint *T);
Tom Cosgroveb4964862022-08-30 11:57:22 +0100500
Hanno Beckerec440f22022-08-11 17:29:32 +0100501/**
Minos Galanakisb85506e2022-10-20 09:51:53 +0100502 * \brief Calculate the square of the Montgomery constant. (Needed
503 * for conversion and operations in Montgomery form.)
Hanno Beckerec440f22022-08-11 17:29:32 +0100504 *
505 * \param[out] X A pointer to the result of the calculation of
Minos Galanakisb85506e2022-10-20 09:51:53 +0100506 * the square of the Montgomery constant:
507 * 2^{2*n*biL} mod N.
Hanno Beckerec440f22022-08-11 17:29:32 +0100508 * \param[in] N Little-endian presentation of the modulus, which must be odd.
509 *
510 * \return 0 if successful.
511 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if there is not enough space
512 * to store the value of Montgomery constant squared.
513 * \return #MBEDTLS_ERR_MPI_DIVISION_BY_ZERO if \p N modulus is zero.
514 * \return #MBEDTLS_ERR_MPI_NEGATIVE_VALUE if \p N modulus is negative.
Hanno Beckerec440f22022-08-11 17:29:32 +0100515 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100516int mbedtls_mpi_core_get_mont_r2_unsafe(mbedtls_mpi *X,
517 const mbedtls_mpi *N);
Hanno Beckerec440f22022-08-11 17:29:32 +0100518
Janos Follath59cbd1b2022-10-28 18:13:43 +0100519#if defined(MBEDTLS_TEST_HOOKS)
Janos Follathe50f2f12022-10-26 15:14:33 +0100520/**
Janos Follath8904a2d2022-10-31 15:32:28 +0000521 * Copy an MPI from a table without leaking the index.
Janos Follathe50f2f12022-10-26 15:14:33 +0100522 *
523 * \param dest The destination buffer. This must point to a writable
524 * buffer of at least \p limbs limbs.
525 * \param table The address of the table. This must point to a readable
Janos Follath8904a2d2022-10-31 15:32:28 +0000526 * array of \p count elements of \p limbs limbs each.
527 * \param limbs The number of limbs in each table entry.
528 * \param count The number of entries in \p table.
529 * \param index The (secret) table index to look up. This must be in the
530 * range `0 .. count-1`.
Janos Follathe50f2f12022-10-26 15:14:33 +0100531 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100532void mbedtls_mpi_core_ct_uint_table_lookup(mbedtls_mpi_uint *dest,
533 const mbedtls_mpi_uint *table,
534 size_t limbs,
535 size_t count,
536 size_t index);
Janos Follath59cbd1b2022-10-28 18:13:43 +0100537#endif /* MBEDTLS_TEST_HOOKS */
Janos Follathe50f2f12022-10-26 15:14:33 +0100538
Gilles Peskine909e03c2022-10-18 18:14:33 +0200539/**
540 * \brief Fill an integer with a number of random bytes.
541 *
542 * \param X The destination MPI.
543 * \param X_limbs The number of limbs of \p X.
544 * \param bytes The number of random bytes to generate.
545 * \param f_rng The RNG function to use. This must not be \c NULL.
546 * \param p_rng The RNG parameter to be passed to \p f_rng. This may be
547 * \c NULL if \p f_rng doesn't need a context argument.
548 *
549 * \return \c 0 if successful.
550 * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if \p X does not have
551 * enough room for \p bytes bytes.
552 * \return A negative error code on RNG failure.
553 *
554 * \note The bytes obtained from the RNG are interpreted
555 * as a big-endian representation of an MPI; this can
556 * be relevant in applications like deterministic ECDSA.
557 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100558int mbedtls_mpi_core_fill_random(mbedtls_mpi_uint *X, size_t X_limbs,
559 size_t bytes,
560 int (*f_rng)(void *, unsigned char *, size_t),
561 void *p_rng);
Gilles Peskine909e03c2022-10-18 18:14:33 +0200562
Gilles Peskine4a8c5cd2022-10-18 18:15:01 +0200563/** Generate a random number uniformly in a range.
564 *
565 * This function generates a random number between \p min inclusive and
566 * \p N exclusive.
567 *
568 * The procedure complies with RFC 6979 §3.3 (deterministic ECDSA)
569 * when the RNG is a suitably parametrized instance of HMAC_DRBG
570 * and \p min is \c 1.
571 *
572 * \note There are `N - min` possible outputs. The lower bound
573 * \p min can be reached, but the upper bound \p N cannot.
574 *
575 * \param X The destination MPI, with \p limbs limbs.
576 * It must not be aliased with \p N or otherwise overlap it.
577 * \param min The minimum value to return.
578 * \param N The upper bound of the range, exclusive, with \p limbs limbs.
579 * In other words, this is one plus the maximum value to return.
580 * \p N must be strictly larger than \p min.
581 * \param limbs The number of limbs of \p N and \p X.
582 * This must not be 0.
583 * \param f_rng The RNG function to use. This must not be \c NULL.
584 * \param p_rng The RNG parameter to be passed to \p f_rng.
585 *
586 * \return \c 0 if successful.
587 * \return #MBEDTLS_ERR_MPI_NOT_ACCEPTABLE if the implementation was
588 * unable to find a suitable value within a limited number
589 * of attempts. This has a negligible probability if \p N
590 * is significantly larger than \p min, which is the case
591 * for all usual cryptographic applications.
592 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100593int mbedtls_mpi_core_random(mbedtls_mpi_uint *X,
594 mbedtls_mpi_uint min,
595 const mbedtls_mpi_uint *N,
596 size_t limbs,
597 int (*f_rng)(void *, unsigned char *, size_t),
598 void *p_rng);
Gilles Peskine4a8c5cd2022-10-18 18:15:01 +0200599
Janos Follathb6673f02022-09-30 14:13:14 +0100600/**
Tom Cosgrove0a0dded2022-12-06 14:37:18 +0000601 * \brief Returns the number of limbs of working memory required for
602 * a call to `mbedtls_mpi_core_exp_mod()`.
Janos Follathb6673f02022-09-30 14:13:14 +0100603 *
Tom Cosgrove28ff92c2022-12-12 17:06:27 +0000604 * \note This will always be at least
605 * `mbedtls_mpi_core_montmul_working_limbs(AN_limbs)`,
606 * i.e. sufficient for a call to `mbedtls_mpi_core_montmul()`.
607 *
Tom Cosgrove0a0dded2022-12-06 14:37:18 +0000608 * \param AN_limbs The number of limbs in the input `A` and the modulus `N`
609 * (they must be the same size) that will be given to
610 * `mbedtls_mpi_core_exp_mod()`.
611 * \param E_limbs The number of limbs in the exponent `E` that will be given
612 * to `mbedtls_mpi_core_exp_mod()`.
Janos Follathb6673f02022-09-30 14:13:14 +0100613 *
Tom Cosgrove0a0dded2022-12-06 14:37:18 +0000614 * \return The number of limbs of working memory required by
615 * `mbedtls_mpi_core_exp_mod()`.
Janos Follathb6673f02022-09-30 14:13:14 +0100616 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100617size_t mbedtls_mpi_core_exp_mod_working_limbs(size_t AN_limbs, size_t E_limbs);
Tom Cosgrove0a0dded2022-12-06 14:37:18 +0000618
619/**
620 * \brief Perform a modular exponentiation with secret exponent:
621 * X = A^E mod N, where \p A is already in Montgomery form.
622 *
Tom Cosgrovea7f0d7b2022-12-07 13:29:07 +0000623 * \p X may be aliased to \p A, but not to \p RR or \p E, even if \p E_limbs ==
624 * \p AN_limbs.
625 *
Tom Cosgrove0a0dded2022-12-06 14:37:18 +0000626 * \param[out] X The destination MPI, as a little endian array of length
627 * \p AN_limbs.
628 * \param[in] A The base MPI, as a little endian array of length \p AN_limbs.
629 * Must be in Montgomery form.
630 * \param[in] N The modulus, as a little endian array of length \p AN_limbs.
631 * \param AN_limbs The number of limbs in \p X, \p A, \p N, \p RR.
632 * \param[in] E The exponent, as a little endian array of length \p E_limbs.
633 * \param E_limbs The number of limbs in \p E.
634 * \param[in] RR The precomputed residue of 2^{2*biL} modulo N, as a little
635 * endian array of length \p AN_limbs.
636 * \param[in,out] T Temporary storage of at least the number of limbs returned
637 * by `mbedtls_mpi_core_exp_mod_working_limbs()`.
638 * Its initial content is unused and its final content is
639 * indeterminate.
640 * It must not alias or otherwise overlap any of the other
641 * parameters.
642 * It is up to the caller to zeroize \p T when it is no
643 * longer needed, and before freeing it if it was dynamically
644 * allocated.
645 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100646void mbedtls_mpi_core_exp_mod(mbedtls_mpi_uint *X,
647 const mbedtls_mpi_uint *A,
648 const mbedtls_mpi_uint *N, size_t AN_limbs,
649 const mbedtls_mpi_uint *E, size_t E_limbs,
650 const mbedtls_mpi_uint *RR,
651 mbedtls_mpi_uint *T);
Janos Follathb6673f02022-09-30 14:13:14 +0100652
Hanno Beckerd9b23482022-08-25 08:25:19 +0100653/**
654 * \brief Subtract unsigned integer from known-size large unsigned integers.
655 * Return the borrow.
656 *
Tom Cosgrovef7ff4c92022-08-25 08:39:07 +0100657 * \param[out] X The result of the subtraction.
658 * \param[in] A The left operand.
659 * \param b The unsigned scalar to subtract.
660 * \param limbs Number of limbs of \p X and \p A.
Hanno Beckerd9b23482022-08-25 08:25:19 +0100661 *
Tom Cosgrovef7ff4c92022-08-25 08:39:07 +0100662 * \return 1 if `A < b`.
663 * 0 if `A >= b`.
Hanno Beckerd9b23482022-08-25 08:25:19 +0100664 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100665mbedtls_mpi_uint mbedtls_mpi_core_sub_int(mbedtls_mpi_uint *X,
666 const mbedtls_mpi_uint *A,
667 mbedtls_mpi_uint b,
668 size_t limbs);
Hanno Beckerd9b23482022-08-25 08:25:19 +0100669
Tom Cosgrove30f3b4d2022-12-12 16:54:57 +0000670/**
671 * \brief Determine if a given MPI has the value \c 0 in constant time with
672 * respect to the value (but not with respect to the number of limbs).
673 *
674 * \param[in] A The MPI to test.
675 * \param limbs Number of limbs in \p A.
676 *
677 * \return 0 if `A == 0`
678 * non-0 (may be any value) if `A != 0`.
679 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100680mbedtls_mpi_uint mbedtls_mpi_core_check_zero_ct(const mbedtls_mpi_uint *A,
681 size_t limbs);
Tom Cosgrove30f3b4d2022-12-12 16:54:57 +0000682
Tom Cosgrove28ff92c2022-12-12 17:06:27 +0000683/**
684 * \brief Returns the number of limbs of working memory required for
685 * a call to `mbedtls_mpi_core_montmul()`.
686 *
687 * \param AN_limbs The number of limbs in the input `A` and the modulus `N`
688 * (they must be the same size) that will be given to
689 * `mbedtls_mpi_core_montmul()` or one of the other functions
690 * that specifies this as the amount of working memory needed.
691 *
692 * \return The number of limbs of working memory required by
693 * `mbedtls_mpi_core_montmul()` (or other similar function).
694 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100695static inline size_t mbedtls_mpi_core_montmul_working_limbs(size_t AN_limbs)
Tom Cosgrove28ff92c2022-12-12 17:06:27 +0000696{
Gilles Peskine449bd832023-01-11 14:50:10 +0100697 return 2 * AN_limbs + 1;
Tom Cosgrove28ff92c2022-12-12 17:06:27 +0000698}
699
Tom Cosgrove786848b2022-12-13 10:45:19 +0000700/** Convert an MPI into Montgomery form.
701 *
702 * \p X may be aliased to \p A, but may not otherwise overlap it.
703 *
Tom Cosgrove5c8505f2023-03-07 11:39:52 +0000704 * \p X may not alias \p N (it is in canonical form, so must be strictly less
Tom Cosgrove786848b2022-12-13 10:45:19 +0000705 * than \p N). Nor may it alias or overlap \p rr (this is unlikely to be
706 * required in practice.)
707 *
708 * This function is a thin wrapper around `mbedtls_mpi_core_montmul()` that is
709 * an alternative to calling `mbedtls_mpi_mod_raw_to_mont_rep()` when we
710 * don't want to allocate memory.
711 *
712 * \param[out] X The result of the conversion.
713 * Must have the same number of limbs as \p A.
714 * \param[in] A The MPI to convert into Montgomery form.
715 * Must have the same number of limbs as the modulus.
716 * \param[in] N The address of the modulus, which gives the size of
Tom Cosgrovef7237542022-12-16 16:10:36 +0000717 * the base `R` = 2^(biL*N->limbs).
Tom Cosgrove786848b2022-12-13 10:45:19 +0000718 * \param[in] AN_limbs The number of limbs in \p X, \p A, \p N and \p rr.
719 * \param mm The Montgomery constant for \p N: -N^-1 mod 2^biL.
Tom Cosgroveb38c2ed2022-12-14 13:11:46 +0000720 * This can be determined by calling
Tom Cosgrove786848b2022-12-13 10:45:19 +0000721 * `mbedtls_mpi_core_montmul_init()`.
722 * \param[in] rr The residue for `2^{2*n*biL} mod N`.
723 * \param[in,out] T Temporary storage of size at least
724 * `mbedtls_mpi_core_montmul_working_limbs(AN_limbs)`
725 * limbs.
726 * Its initial content is unused and
727 * its final content is indeterminate.
728 * It must not alias or otherwise overlap any of the
729 * other parameters.
730 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100731void mbedtls_mpi_core_to_mont_rep(mbedtls_mpi_uint *X,
732 const mbedtls_mpi_uint *A,
733 const mbedtls_mpi_uint *N,
734 size_t AN_limbs,
735 mbedtls_mpi_uint mm,
736 const mbedtls_mpi_uint *rr,
737 mbedtls_mpi_uint *T);
Tom Cosgrove786848b2022-12-13 10:45:19 +0000738
739/** Convert an MPI from Montgomery form.
740 *
741 * \p X may be aliased to \p A, but may not otherwise overlap it.
742 *
Tom Cosgrove5c8505f2023-03-07 11:39:52 +0000743 * \p X may not alias \p N (it is in canonical form, so must be strictly less
Tom Cosgrove786848b2022-12-13 10:45:19 +0000744 * than \p N).
745 *
746 * This function is a thin wrapper around `mbedtls_mpi_core_montmul()` that is
747 * an alternative to calling `mbedtls_mpi_mod_raw_from_mont_rep()` when we
748 * don't want to allocate memory.
749 *
750 * \param[out] X The result of the conversion.
751 * Must have the same number of limbs as \p A.
752 * \param[in] A The MPI to convert from Montgomery form.
753 * Must have the same number of limbs as the modulus.
754 * \param[in] N The address of the modulus, which gives the size of
Tom Cosgrovef7237542022-12-16 16:10:36 +0000755 * the base `R` = 2^(biL*N->limbs).
Tom Cosgrove786848b2022-12-13 10:45:19 +0000756 * \param[in] AN_limbs The number of limbs in \p X, \p A and \p N.
757 * \param mm The Montgomery constant for \p N: -N^-1 mod 2^biL.
Tom Cosgroveb38c2ed2022-12-14 13:11:46 +0000758 * This can be determined by calling
Tom Cosgrove786848b2022-12-13 10:45:19 +0000759 * `mbedtls_mpi_core_montmul_init()`.
760 * \param[in,out] T Temporary storage of size at least
761 * `mbedtls_mpi_core_montmul_working_limbs(AN_limbs)`
762 * limbs.
763 * Its initial content is unused and
764 * its final content is indeterminate.
765 * It must not alias or otherwise overlap any of the
766 * other parameters.
767 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100768void mbedtls_mpi_core_from_mont_rep(mbedtls_mpi_uint *X,
769 const mbedtls_mpi_uint *A,
770 const mbedtls_mpi_uint *N,
771 size_t AN_limbs,
772 mbedtls_mpi_uint mm,
773 mbedtls_mpi_uint *T);
Tom Cosgrove786848b2022-12-13 10:45:19 +0000774
Gabor Mezeib9030702022-07-18 23:09:45 +0200775#endif /* MBEDTLS_BIGNUM_CORE_H */