blob: 3a111600b8044eb366ddf4cbfe0f48cc5eb95325 [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
Gilles Peskine449bd832023-01-11 14:50:10 +010089#define ciL (sizeof(mbedtls_mpi_uint)) /** chars in limb */
90#define biL (ciL << 3) /** bits in limb */
91#define biH (ciL << 2) /** half limb size */
Tom Cosgrove5eefc3d2022-08-31 17:16:50 +010092
93/*
94 * Convert between bits/chars and number of limbs
95 * Divide first in order to avoid potential overflows
96 */
Gilles Peskine449bd832023-01-11 14:50:10 +010097#define BITS_TO_LIMBS(i) ((i) / biL + ((i) % biL != 0))
98#define CHARS_TO_LIMBS(i) ((i) / ciL + ((i) % ciL != 0))
Tom Cosgrove5eefc3d2022-08-31 17:16:50 +010099/* Get a specific byte, without range checks. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100100#define GET_BYTE(X, i) \
101 (((X)[(i) / ciL] >> (((i) % ciL) * 8)) & 0xff)
Tom Cosgrove5eefc3d2022-08-31 17:16:50 +0100102
Gabor Mezei37b06362022-08-02 17:22:18 +0200103/** Count leading zero bits in a given integer.
104 *
Janos Follathb7a88ec2022-08-19 12:24:40 +0100105 * \param a Integer to count leading zero bits.
Gabor Mezei37b06362022-08-02 17:22:18 +0200106 *
Janos Follathb7a88ec2022-08-19 12:24:40 +0100107 * \return The number of leading zero bits in \p a.
Gabor Mezei37b06362022-08-02 17:22:18 +0200108 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100109size_t mbedtls_mpi_core_clz(mbedtls_mpi_uint a);
Janos Follath4670f882022-07-21 18:25:42 +0100110
Janos Follatha95f2042022-08-19 12:09:17 +0100111/** Return the minimum number of bits required to represent the value held
Janos Follath63184682022-08-11 17:42:59 +0100112 * in the MPI.
113 *
Janos Follathb7a88ec2022-08-19 12:24:40 +0100114 * \note This function returns 0 if all the limbs of \p A are 0.
Gabor Mezei37b06362022-08-02 17:22:18 +0200115 *
Janos Follathb7a88ec2022-08-19 12:24:40 +0100116 * \param[in] A The address of the MPI.
Janos Follathc4596412022-08-22 10:01:27 +0100117 * \param A_limbs The number of limbs of \p A.
Gabor Mezei37b06362022-08-02 17:22:18 +0200118 *
Janos Follathb7a88ec2022-08-19 12:24:40 +0100119 * \return The number of bits in \p A.
Gabor Mezei37b06362022-08-02 17:22:18 +0200120 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100121size_t mbedtls_mpi_core_bitlen(const mbedtls_mpi_uint *A, size_t A_limbs);
Janos Follath4670f882022-07-21 18:25:42 +0100122
Gabor Mezei37b06362022-08-02 17:22:18 +0200123/** Convert a big-endian byte array aligned to the size of mbedtls_mpi_uint
124 * into the storage form used by mbedtls_mpi.
125 *
Janos Follathb7a88ec2022-08-19 12:24:40 +0100126 * \param[in,out] A The address of the MPI.
Janos Follathc4596412022-08-22 10:01:27 +0100127 * \param A_limbs The number of limbs of \p A.
Gabor Mezei37b06362022-08-02 17:22:18 +0200128 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100129void mbedtls_mpi_core_bigendian_to_host(mbedtls_mpi_uint *A,
130 size_t A_limbs);
Janos Follath4670f882022-07-21 18:25:42 +0100131
Gilles Peskine6f949ea2022-09-20 18:38:35 +0200132/** \brief Compare a machine integer with an MPI.
133 *
134 * This function operates in constant time with respect
135 * to the values of \p min and \p A.
136 *
137 * \param min A machine integer.
138 * \param[in] A An MPI.
139 * \param A_limbs The number of limbs of \p A.
140 * This must be at least 1.
141 *
142 * \return 1 if \p min is less than or equal to \p A, otherwise 0.
143 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100144unsigned mbedtls_mpi_core_uint_le_mpi(mbedtls_mpi_uint min,
145 const mbedtls_mpi_uint *A,
146 size_t A_limbs);
Gilles Peskine6f949ea2022-09-20 18:38:35 +0200147
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200148/**
Gabor Mezei4086de62022-10-14 16:29:42 +0200149 * \brief Perform a safe conditional copy of an MPI which doesn't reveal
150 * whether assignment was done or not.
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200151 *
Gabor Mezei4086de62022-10-14 16:29:42 +0200152 * \param[out] X The address of the destination MPI.
153 * This must be initialized. Must have enough limbs to
154 * store the full value of \p A.
155 * \param[in] A The address of the source MPI. This must be initialized.
Gabor Mezei1c628d52022-09-27 12:13:51 +0200156 * \param limbs The number of limbs of \p A.
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200157 * \param assign The condition deciding whether to perform the
158 * assignment or not. Must be either 0 or 1:
Gabor Mezei1c628d52022-09-27 12:13:51 +0200159 * * \c 1: Perform the assignment `X = A`.
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200160 * * \c 0: Keep the original value of \p X.
161 *
162 * \note This function avoids leaking any information about whether
163 * the assignment was done or not.
164 *
165 * \warning If \p assign is neither 0 nor 1, the result of this function
166 * is indeterminate, and the resulting value in \p X might be
Gabor Mezei4086de62022-10-14 16:29:42 +0200167 * neither its original value nor the value in \p A.
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200168 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100169void mbedtls_mpi_core_cond_assign(mbedtls_mpi_uint *X,
170 const mbedtls_mpi_uint *A,
171 size_t limbs,
172 unsigned char assign);
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200173
174/**
Gabor Mezei4086de62022-10-14 16:29:42 +0200175 * \brief Perform a safe conditional swap of two MPIs which doesn't reveal
176 * whether the swap was done or not.
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200177 *
Gabor Mezei86dfe382022-09-30 14:03:04 +0200178 * \param[in,out] X The address of the first MPI.
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200179 * This must be initialized.
Gabor Mezei86dfe382022-09-30 14:03:04 +0200180 * \param[in,out] Y The address of the second MPI.
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200181 * This must be initialized.
Gabor Mezeie5b85852022-09-30 13:54:02 +0200182 * \param limbs The number of limbs of \p X and \p Y.
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200183 * \param swap The condition deciding whether to perform
184 * the swap or not. Must be either 0 or 1:
Gabor Mezeie5b85852022-09-30 13:54:02 +0200185 * * \c 1: Swap the values of \p X and \p Y.
186 * * \c 0: Keep the original values of \p X and \p Y.
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200187 *
188 * \note This function avoids leaking any information about whether
189 * the swap was done or not.
190 *
191 * \warning If \p swap is neither 0 nor 1, the result of this function
Gabor Mezeie5b85852022-09-30 13:54:02 +0200192 * is indeterminate, and both \p X and \p Y might end up with
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200193 * values different to either of the original ones.
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200194 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100195void mbedtls_mpi_core_cond_swap(mbedtls_mpi_uint *X,
196 mbedtls_mpi_uint *Y,
197 size_t limbs,
198 unsigned char swap);
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200199
Janos Follathaf3f39c2022-08-22 09:06:32 +0100200/** Import X from unsigned binary data, little-endian.
Gabor Mezei37b06362022-08-02 17:22:18 +0200201 *
Janos Follath63184682022-08-11 17:42:59 +0100202 * The MPI needs to have enough limbs to store the full value (including any
203 * most significant zero bytes in the input).
Gabor Mezei37b06362022-08-02 17:22:18 +0200204 *
Janos Follathb7a88ec2022-08-19 12:24:40 +0100205 * \param[out] X The address of the MPI.
206 * \param X_limbs The number of limbs of \p X.
207 * \param[in] input The input buffer to import from.
208 * \param input_length The length bytes of \p input.
Gabor Mezei37b06362022-08-02 17:22:18 +0200209 *
210 * \return \c 0 if successful.
211 * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p X isn't
Janos Follathb7a88ec2022-08-19 12:24:40 +0100212 * large enough to hold the value in \p input.
Gabor Mezei37b06362022-08-02 17:22:18 +0200213 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100214int mbedtls_mpi_core_read_le(mbedtls_mpi_uint *X,
215 size_t X_limbs,
216 const unsigned char *input,
217 size_t input_length);
Gabor Mezeib9030702022-07-18 23:09:45 +0200218
Janos Follathaf3f39c2022-08-22 09:06:32 +0100219/** Import X from unsigned binary data, big-endian.
Gabor Mezei37b06362022-08-02 17:22:18 +0200220 *
Janos Follath63184682022-08-11 17:42:59 +0100221 * The MPI needs to have enough limbs to store the full value (including any
222 * most significant zero bytes in the input).
Gabor Mezei37b06362022-08-02 17:22:18 +0200223 *
Janos Follathb7a88ec2022-08-19 12:24:40 +0100224 * \param[out] X The address of the MPI.
Tom Cosgrove8a1f7842023-02-01 08:43:54 +0000225 * May only be #NULL if \p X_limbs is 0 and \p input_length
Janos Follathb7a88ec2022-08-19 12:24:40 +0100226 * is 0.
227 * \param X_limbs The number of limbs of \p X.
228 * \param[in] input The input buffer to import from.
229 * May only be #NULL if \p input_length is 0.
230 * \param input_length The length in bytes of \p input.
Gabor Mezei37b06362022-08-02 17:22:18 +0200231 *
232 * \return \c 0 if successful.
233 * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p X isn't
Janos Follathb7a88ec2022-08-19 12:24:40 +0100234 * large enough to hold the value in \p input.
Gabor Mezei37b06362022-08-02 17:22:18 +0200235 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100236int mbedtls_mpi_core_read_be(mbedtls_mpi_uint *X,
237 size_t X_limbs,
238 const unsigned char *input,
239 size_t input_length);
Gabor Mezeib9030702022-07-18 23:09:45 +0200240
Janos Follathaf3f39c2022-08-22 09:06:32 +0100241/** Export A into unsigned binary data, little-endian.
Gabor Mezei37b06362022-08-02 17:22:18 +0200242 *
Janos Follathb7a88ec2022-08-19 12:24:40 +0100243 * \note If \p output is shorter than \p A the export is still successful if the
244 * value held in \p A fits in the buffer (that is, if enough of the most
245 * significant bytes of \p A are 0).
Janos Follath63184682022-08-11 17:42:59 +0100246 *
Janos Follathb7a88ec2022-08-19 12:24:40 +0100247 * \param[in] A The address of the MPI.
248 * \param A_limbs The number of limbs of \p A.
249 * \param[out] output The output buffer to export to.
250 * \param output_length The length in bytes of \p output.
Gabor Mezei37b06362022-08-02 17:22:18 +0200251 *
252 * \return \c 0 if successful.
Janos Follathb7a88ec2022-08-19 12:24:40 +0100253 * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p output isn't
254 * large enough to hold the value of \p A.
Gabor Mezei37b06362022-08-02 17:22:18 +0200255 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100256int mbedtls_mpi_core_write_le(const mbedtls_mpi_uint *A,
257 size_t A_limbs,
258 unsigned char *output,
259 size_t output_length);
Gabor Mezeib9030702022-07-18 23:09:45 +0200260
Janos Follathaf3f39c2022-08-22 09:06:32 +0100261/** Export A into unsigned binary data, big-endian.
Gabor Mezei37b06362022-08-02 17:22:18 +0200262 *
Janos Follathb7a88ec2022-08-19 12:24:40 +0100263 * \note If \p output is shorter than \p A the export is still successful if the
264 * value held in \p A fits in the buffer (that is, if enough of the most
265 * significant bytes of \p A are 0).
Janos Follath63184682022-08-11 17:42:59 +0100266 *
Janos Follathb7a88ec2022-08-19 12:24:40 +0100267 * \param[in] A The address of the MPI.
268 * \param A_limbs The number of limbs of \p A.
269 * \param[out] output The output buffer to export to.
270 * \param output_length The length in bytes of \p output.
Gabor Mezei37b06362022-08-02 17:22:18 +0200271 *
272 * \return \c 0 if successful.
Janos Follathb7a88ec2022-08-19 12:24:40 +0100273 * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p output isn't
274 * large enough to hold the value of \p A.
Gabor Mezei37b06362022-08-02 17:22:18 +0200275 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100276int mbedtls_mpi_core_write_be(const mbedtls_mpi_uint *A,
277 size_t A_limbs,
278 unsigned char *output,
279 size_t output_length);
Gabor Mezeib9030702022-07-18 23:09:45 +0200280
Gilles Peskineabc6fbb2022-10-21 18:36:08 +0200281/** \brief Shift an MPI right in place by a number of bits.
Gilles Peskine66414202022-09-21 15:36:16 +0200282 *
283 * Shifting by more bits than there are bit positions
284 * in \p X is valid and results in setting \p X to 0.
285 *
286 * This function's execution time depends on the value
287 * of \p count (and of course \p limbs).
288 *
289 * \param[in,out] X The number to shift.
290 * \param limbs The number of limbs of \p X. This must be at least 1.
291 * \param count The number of bits to shift by.
292 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100293void mbedtls_mpi_core_shift_r(mbedtls_mpi_uint *X, size_t limbs,
294 size_t count);
Gilles Peskine66414202022-09-21 15:36:16 +0200295
Tom Cosgrove90c426b2022-08-23 16:15:19 +0100296/**
Tom Cosgroveaf7d44b2022-08-24 14:05:26 +0100297 * \brief Add two fixed-size large unsigned integers, returning the carry.
Hanno Beckerc9887132022-08-24 12:54:36 +0100298 *
Tom Cosgroveaf7d44b2022-08-24 14:05:26 +0100299 * Calculates `A + B` where `A` and `B` have the same size.
300 *
Tom Cosgrove82f13102022-10-25 12:46:03 +0100301 * This function operates modulo `2^(biL*limbs)` and returns the carry
Hanno Beckerc9887132022-08-24 12:54:36 +0100302 * (1 if there was a wraparound, and 0 otherwise).
303 *
Tom Cosgroveaf7d44b2022-08-24 14:05:26 +0100304 * \p X may be aliased to \p A or \p B.
Hanno Beckerc9887132022-08-24 12:54:36 +0100305 *
Tom Cosgroveaf7d44b2022-08-24 14:05:26 +0100306 * \param[out] X The result of the addition.
307 * \param[in] A Little-endian presentation of the left operand.
308 * \param[in] B Little-endian presentation of the right operand.
309 * \param limbs Number of limbs of \p X, \p A and \p B.
Hanno Beckerc9887132022-08-24 12:54:36 +0100310 *
Tom Cosgroveaf7d44b2022-08-24 14:05:26 +0100311 * \return 1 if `A + B >= 2^(biL*limbs)`, 0 otherwise.
Hanno Beckerc9887132022-08-24 12:54:36 +0100312 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100313mbedtls_mpi_uint mbedtls_mpi_core_add(mbedtls_mpi_uint *X,
314 const mbedtls_mpi_uint *A,
315 const mbedtls_mpi_uint *B,
316 size_t limbs);
Hanno Beckerc9887132022-08-24 12:54:36 +0100317
Tom Cosgrove90c426b2022-08-23 16:15:19 +0100318/**
Tom Cosgrove5c0e8102022-09-15 15:46:10 +0100319 * \brief Conditional addition of two fixed-size large unsigned integers,
Tom Cosgroved932de82022-08-25 16:43:43 +0100320 * returning the carry.
Hanno Becker71f4b0d2022-08-23 12:09:35 +0100321 *
322 * Functionally equivalent to
323 *
324 * ```
325 * if( cond )
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100326 * X += A;
Hanno Becker71f4b0d2022-08-23 12:09:35 +0100327 * return carry;
328 * ```
329 *
Tom Cosgrove47828232022-09-20 13:51:50 +0100330 * This function operates modulo `2^(biL*limbs)`.
331 *
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100332 * \param[in,out] X The pointer to the (little-endian) array
Tom Cosgrove72594632022-08-24 11:51:58 +0100333 * representing the bignum to accumulate onto.
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100334 * \param[in] A The pointer to the (little-endian) array
Tom Cosgrove72594632022-08-24 11:51:58 +0100335 * representing the bignum to conditionally add
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100336 * to \p X. This may be aliased to \p X but may not
Tom Cosgroveed43c6c2022-08-31 11:35:00 +0100337 * overlap otherwise.
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100338 * \param limbs Number of limbs of \p X and \p A.
Tom Cosgrove72594632022-08-24 11:51:58 +0100339 * \param cond Condition bit dictating whether addition should
340 * happen or not. This must be \c 0 or \c 1.
Hanno Becker71f4b0d2022-08-23 12:09:35 +0100341 *
Tom Cosgroveecbb1242022-08-25 10:13:44 +0100342 * \warning If \p cond is neither 0 nor 1, the result of this function
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100343 * is unspecified, and the resulting value in \p X might be
344 * neither its original value nor \p X + \p A.
Tom Cosgrove72594632022-08-24 11:51:58 +0100345 *
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100346 * \return 1 if `X + cond * A >= 2^(biL*limbs)`, 0 otherwise.
Hanno Becker71f4b0d2022-08-23 12:09:35 +0100347 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100348mbedtls_mpi_uint mbedtls_mpi_core_add_if(mbedtls_mpi_uint *X,
349 const mbedtls_mpi_uint *A,
350 size_t limbs,
351 unsigned cond);
Hanno Becker71f4b0d2022-08-23 12:09:35 +0100352
Tom Cosgroveb4964862022-08-30 11:57:22 +0100353/**
Tom Cosgrove5c0e8102022-09-15 15:46:10 +0100354 * \brief Subtract two fixed-size large unsigned integers, returning the borrow.
Tom Cosgroveb4964862022-08-30 11:57:22 +0100355 *
Tom Cosgrove5dd97e62022-08-30 14:31:49 +0100356 * Calculate `A - B` where \p A and \p B have the same size.
Tom Cosgrove630110a2022-08-31 17:09:29 +0100357 * This function operates modulo `2^(biL*limbs)` and returns the carry
Tom Cosgroveb4964862022-08-30 11:57:22 +0100358 * (1 if there was a wraparound, i.e. if `A < B`, and 0 otherwise).
359 *
Tom Cosgrove5dd97e62022-08-30 14:31:49 +0100360 * \p X may be aliased to \p A or \p B, or even both, but may not overlap
361 * either otherwise.
Tom Cosgroveb4964862022-08-30 11:57:22 +0100362 *
363 * \param[out] X The result of the subtraction.
364 * \param[in] A Little-endian presentation of left operand.
365 * \param[in] B Little-endian presentation of right operand.
366 * \param limbs Number of limbs of \p X, \p A and \p B.
367 *
368 * \return 1 if `A < B`.
369 * 0 if `A >= B`.
370 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100371mbedtls_mpi_uint mbedtls_mpi_core_sub(mbedtls_mpi_uint *X,
372 const mbedtls_mpi_uint *A,
373 const mbedtls_mpi_uint *B,
374 size_t limbs);
Tom Cosgroveb4964862022-08-30 11:57:22 +0100375
376/**
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100377 * \brief Perform a fixed-size multiply accumulate operation: X += b * A
Tom Cosgroveb4964862022-08-30 11:57:22 +0100378 *
Tom Cosgroveb0b77e12022-09-20 13:33:40 +0100379 * \p X may be aliased to \p A (when \p X_limbs == \p A_limbs), but may not
380 * otherwise overlap.
381 *
Tom Cosgrove47828232022-09-20 13:51:50 +0100382 * This function operates modulo `2^(biL*X_limbs)`.
383 *
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100384 * \param[in,out] X The pointer to the (little-endian) array
Tom Cosgroveb4964862022-08-30 11:57:22 +0100385 * representing the bignum to accumulate onto.
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100386 * \param X_limbs The number of limbs of \p X. This must be
387 * at least \p A_limbs.
388 * \param[in] A The pointer to the (little-endian) array
Tom Cosgroveb4964862022-08-30 11:57:22 +0100389 * representing the bignum to multiply with.
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100390 * This may be aliased to \p X but may not overlap
Tom Cosgroveed43c6c2022-08-31 11:35:00 +0100391 * otherwise.
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100392 * \param A_limbs The number of limbs of \p A.
393 * \param b X scalar to multiply with.
Tom Cosgroveb4964862022-08-30 11:57:22 +0100394 *
395 * \return The carry at the end of the operation.
396 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100397mbedtls_mpi_uint mbedtls_mpi_core_mla(mbedtls_mpi_uint *X, size_t X_limbs,
398 const mbedtls_mpi_uint *A, size_t A_limbs,
399 mbedtls_mpi_uint b);
Tom Cosgroveb4964862022-08-30 11:57:22 +0100400
Hanno Becker4ae890b2022-08-24 16:17:52 +0100401/**
402 * \brief Perform a known-size multiplication
403 *
Tom Cosgrove6af26f32022-08-24 16:40:55 +0100404 * \param[out] X The pointer to the (little-endian) array to receive
405 * the product of \p A_limbs and \p B_limbs.
406 * This must be of length \p A_limbs + \p B_limbs.
407 * \param[in] A The pointer to the (little-endian) array
408 * representing the first factor.
409 * \param A_limbs The number of limbs in \p A.
410 * \param[in] B The pointer to the (little-endian) array
411 * representing the second factor.
412 * \param B_limbs The number of limbs in \p B.
Hanno Becker4ae890b2022-08-24 16:17:52 +0100413 */
Tom Cosgrove6af26f32022-08-24 16:40:55 +0100414void mbedtls_mpi_core_mul(mbedtls_mpi_uint *X,
415 const mbedtls_mpi_uint *A, size_t A_limbs,
416 const mbedtls_mpi_uint *B, size_t B_limbs);
Hanno Becker4ae890b2022-08-24 16:17:52 +0100417
Tom Cosgroveb4964862022-08-30 11:57:22 +0100418/**
419 * \brief Calculate initialisation value for fast Montgomery modular
420 * multiplication
421 *
422 * \param[in] N Little-endian presentation of the modulus. This must have
423 * at least one limb.
424 *
425 * \return The initialisation value for fast Montgomery modular multiplication
426 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100427mbedtls_mpi_uint mbedtls_mpi_core_montmul_init(const mbedtls_mpi_uint *N);
Tom Cosgroveb4964862022-08-30 11:57:22 +0100428
429/**
Tom Cosgrove4386ead2022-09-29 14:40:21 +0100430 * \brief Montgomery multiplication: X = A * B * R^-1 mod N (HAC 14.36)
431 *
432 * \p A and \p B must be in canonical form. That is, < \p N.
Tom Cosgroveb4964862022-08-30 11:57:22 +0100433 *
Tom Cosgroveea45c1d2022-09-20 13:17:51 +0100434 * \p X may be aliased to \p A or \p N, or even \p B (if \p AN_limbs ==
435 * \p B_limbs) but may not overlap any parameters otherwise.
436 *
Tom Cosgrove4386ead2022-09-29 14:40:21 +0100437 * \p A and \p B may alias each other, if \p AN_limbs == \p B_limbs. They may
438 * not alias \p N (since they must be in canonical form, they cannot == \p N).
Tom Cosgroveea45c1d2022-09-20 13:17:51 +0100439 *
Tom Cosgroveb4964862022-08-30 11:57:22 +0100440 * \param[out] X The destination MPI, as a little-endian array of
441 * length \p AN_limbs.
442 * On successful completion, X contains the result of
Tom Cosgrove630110a2022-08-31 17:09:29 +0100443 * the multiplication `A * B * R^-1` mod N where
444 * `R = 2^(biL*AN_limbs)`.
Tom Cosgroveb4964862022-08-30 11:57:22 +0100445 * \param[in] A Little-endian presentation of first operand.
Tom Cosgrove5dd97e62022-08-30 14:31:49 +0100446 * Must have the same number of limbs as \p N.
Tom Cosgroveb4964862022-08-30 11:57:22 +0100447 * \param[in] B Little-endian presentation of second operand.
448 * \param[in] B_limbs The number of limbs in \p B.
Tom Cosgrove5dd97e62022-08-30 14:31:49 +0100449 * Must be <= \p AN_limbs.
Tom Cosgroveb4964862022-08-30 11:57:22 +0100450 * \param[in] N Little-endian presentation of the modulus.
Tom Cosgrove5dd97e62022-08-30 14:31:49 +0100451 * This must be odd, and have exactly the same number
452 * of limbs as \p A.
Tom Cosgrove6da3a3b2022-09-29 17:20:18 +0100453 * It may alias \p X, but must not alias or otherwise
454 * overlap any of the other parameters.
Tom Cosgrove5dd97e62022-08-30 14:31:49 +0100455 * \param[in] AN_limbs The number of limbs in \p X, \p A and \p N.
Tom Cosgrove630110a2022-08-31 17:09:29 +0100456 * \param mm The Montgomery constant for \p N: -N^-1 mod 2^biL.
Tom Cosgroveb7438d12022-09-15 15:05:59 +0100457 * This can be calculated by `mbedtls_mpi_core_montmul_init()`.
Tom Cosgroveb4964862022-08-30 11:57:22 +0100458 * \param[in,out] T Temporary storage of size at least 2*AN_limbs+1 limbs.
459 * Its initial content is unused and
460 * its final content is indeterminate.
Tom Cosgrove4386ead2022-09-29 14:40:21 +0100461 * It must not alias or otherwise overlap any of the
462 * other parameters.
Tom Cosgroveb4964862022-08-30 11:57:22 +0100463 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100464void mbedtls_mpi_core_montmul(mbedtls_mpi_uint *X,
465 const mbedtls_mpi_uint *A,
466 const mbedtls_mpi_uint *B, size_t B_limbs,
467 const mbedtls_mpi_uint *N, size_t AN_limbs,
468 mbedtls_mpi_uint mm, mbedtls_mpi_uint *T);
Tom Cosgroveb4964862022-08-30 11:57:22 +0100469
Hanno Beckerec440f22022-08-11 17:29:32 +0100470/**
Minos Galanakisb85506e2022-10-20 09:51:53 +0100471 * \brief Calculate the square of the Montgomery constant. (Needed
472 * for conversion and operations in Montgomery form.)
Hanno Beckerec440f22022-08-11 17:29:32 +0100473 *
474 * \param[out] X A pointer to the result of the calculation of
Minos Galanakisb85506e2022-10-20 09:51:53 +0100475 * the square of the Montgomery constant:
476 * 2^{2*n*biL} mod N.
Hanno Beckerec440f22022-08-11 17:29:32 +0100477 * \param[in] N Little-endian presentation of the modulus, which must be odd.
478 *
479 * \return 0 if successful.
480 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if there is not enough space
481 * to store the value of Montgomery constant squared.
482 * \return #MBEDTLS_ERR_MPI_DIVISION_BY_ZERO if \p N modulus is zero.
483 * \return #MBEDTLS_ERR_MPI_NEGATIVE_VALUE if \p N modulus is negative.
Hanno Beckerec440f22022-08-11 17:29:32 +0100484 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100485int mbedtls_mpi_core_get_mont_r2_unsafe(mbedtls_mpi *X,
486 const mbedtls_mpi *N);
Hanno Beckerec440f22022-08-11 17:29:32 +0100487
Janos Follath59cbd1b2022-10-28 18:13:43 +0100488#if defined(MBEDTLS_TEST_HOOKS)
Janos Follathe50f2f12022-10-26 15:14:33 +0100489/**
Janos Follath8904a2d2022-10-31 15:32:28 +0000490 * Copy an MPI from a table without leaking the index.
Janos Follathe50f2f12022-10-26 15:14:33 +0100491 *
492 * \param dest The destination buffer. This must point to a writable
493 * buffer of at least \p limbs limbs.
494 * \param table The address of the table. This must point to a readable
Janos Follath8904a2d2022-10-31 15:32:28 +0000495 * array of \p count elements of \p limbs limbs each.
496 * \param limbs The number of limbs in each table entry.
497 * \param count The number of entries in \p table.
498 * \param index The (secret) table index to look up. This must be in the
499 * range `0 .. count-1`.
Janos Follathe50f2f12022-10-26 15:14:33 +0100500 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100501void mbedtls_mpi_core_ct_uint_table_lookup(mbedtls_mpi_uint *dest,
502 const mbedtls_mpi_uint *table,
503 size_t limbs,
504 size_t count,
505 size_t index);
Janos Follath59cbd1b2022-10-28 18:13:43 +0100506#endif /* MBEDTLS_TEST_HOOKS */
Janos Follathe50f2f12022-10-26 15:14:33 +0100507
Gilles Peskine909e03c2022-10-18 18:14:33 +0200508/**
509 * \brief Fill an integer with a number of random bytes.
510 *
511 * \param X The destination MPI.
512 * \param X_limbs The number of limbs of \p X.
513 * \param bytes The number of random bytes to generate.
514 * \param f_rng The RNG function to use. This must not be \c NULL.
515 * \param p_rng The RNG parameter to be passed to \p f_rng. This may be
516 * \c NULL if \p f_rng doesn't need a context argument.
517 *
518 * \return \c 0 if successful.
519 * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if \p X does not have
520 * enough room for \p bytes bytes.
521 * \return A negative error code on RNG failure.
522 *
523 * \note The bytes obtained from the RNG are interpreted
524 * as a big-endian representation of an MPI; this can
525 * be relevant in applications like deterministic ECDSA.
526 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100527int mbedtls_mpi_core_fill_random(mbedtls_mpi_uint *X, size_t X_limbs,
528 size_t bytes,
529 int (*f_rng)(void *, unsigned char *, size_t),
530 void *p_rng);
Gilles Peskine909e03c2022-10-18 18:14:33 +0200531
Gilles Peskine4a8c5cd2022-10-18 18:15:01 +0200532/** Generate a random number uniformly in a range.
533 *
534 * This function generates a random number between \p min inclusive and
535 * \p N exclusive.
536 *
537 * The procedure complies with RFC 6979 §3.3 (deterministic ECDSA)
538 * when the RNG is a suitably parametrized instance of HMAC_DRBG
539 * and \p min is \c 1.
540 *
541 * \note There are `N - min` possible outputs. The lower bound
542 * \p min can be reached, but the upper bound \p N cannot.
543 *
544 * \param X The destination MPI, with \p limbs limbs.
545 * It must not be aliased with \p N or otherwise overlap it.
546 * \param min The minimum value to return.
547 * \param N The upper bound of the range, exclusive, with \p limbs limbs.
548 * In other words, this is one plus the maximum value to return.
549 * \p N must be strictly larger than \p min.
550 * \param limbs The number of limbs of \p N and \p X.
551 * This must not be 0.
552 * \param f_rng The RNG function to use. This must not be \c NULL.
553 * \param p_rng The RNG parameter to be passed to \p f_rng.
554 *
555 * \return \c 0 if successful.
556 * \return #MBEDTLS_ERR_MPI_NOT_ACCEPTABLE if the implementation was
557 * unable to find a suitable value within a limited number
558 * of attempts. This has a negligible probability if \p N
559 * is significantly larger than \p min, which is the case
560 * for all usual cryptographic applications.
561 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100562int mbedtls_mpi_core_random(mbedtls_mpi_uint *X,
563 mbedtls_mpi_uint min,
564 const mbedtls_mpi_uint *N,
565 size_t limbs,
566 int (*f_rng)(void *, unsigned char *, size_t),
567 void *p_rng);
Gilles Peskine4a8c5cd2022-10-18 18:15:01 +0200568
Janos Follath5933f692022-11-02 14:35:17 +0000569/* BEGIN MERGE SLOT 1 */
570
Janos Follathb6673f02022-09-30 14:13:14 +0100571/**
Tom Cosgrove0a0dded2022-12-06 14:37:18 +0000572 * \brief Returns the number of limbs of working memory required for
573 * a call to `mbedtls_mpi_core_exp_mod()`.
Janos Follathb6673f02022-09-30 14:13:14 +0100574 *
Tom Cosgrove28ff92c2022-12-12 17:06:27 +0000575 * \note This will always be at least
576 * `mbedtls_mpi_core_montmul_working_limbs(AN_limbs)`,
577 * i.e. sufficient for a call to `mbedtls_mpi_core_montmul()`.
578 *
Tom Cosgrove0a0dded2022-12-06 14:37:18 +0000579 * \param AN_limbs The number of limbs in the input `A` and the modulus `N`
580 * (they must be the same size) that will be given to
581 * `mbedtls_mpi_core_exp_mod()`.
582 * \param E_limbs The number of limbs in the exponent `E` that will be given
583 * to `mbedtls_mpi_core_exp_mod()`.
Janos Follathb6673f02022-09-30 14:13:14 +0100584 *
Tom Cosgrove0a0dded2022-12-06 14:37:18 +0000585 * \return The number of limbs of working memory required by
586 * `mbedtls_mpi_core_exp_mod()`.
Janos Follathb6673f02022-09-30 14:13:14 +0100587 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100588size_t mbedtls_mpi_core_exp_mod_working_limbs(size_t AN_limbs, size_t E_limbs);
Tom Cosgrove0a0dded2022-12-06 14:37:18 +0000589
590/**
591 * \brief Perform a modular exponentiation with secret exponent:
592 * X = A^E mod N, where \p A is already in Montgomery form.
593 *
Tom Cosgrovea7f0d7b2022-12-07 13:29:07 +0000594 * \p X may be aliased to \p A, but not to \p RR or \p E, even if \p E_limbs ==
595 * \p AN_limbs.
596 *
Tom Cosgrove0a0dded2022-12-06 14:37:18 +0000597 * \param[out] X The destination MPI, as a little endian array of length
598 * \p AN_limbs.
599 * \param[in] A The base MPI, as a little endian array of length \p AN_limbs.
600 * Must be in Montgomery form.
601 * \param[in] N The modulus, as a little endian array of length \p AN_limbs.
602 * \param AN_limbs The number of limbs in \p X, \p A, \p N, \p RR.
603 * \param[in] E The exponent, as a little endian array of length \p E_limbs.
604 * \param E_limbs The number of limbs in \p E.
605 * \param[in] RR The precomputed residue of 2^{2*biL} modulo N, as a little
606 * endian array of length \p AN_limbs.
607 * \param[in,out] T Temporary storage of at least the number of limbs returned
608 * by `mbedtls_mpi_core_exp_mod_working_limbs()`.
609 * Its initial content is unused and its final content is
610 * indeterminate.
611 * It must not alias or otherwise overlap any of the other
612 * parameters.
613 * It is up to the caller to zeroize \p T when it is no
614 * longer needed, and before freeing it if it was dynamically
615 * allocated.
616 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100617void mbedtls_mpi_core_exp_mod(mbedtls_mpi_uint *X,
618 const mbedtls_mpi_uint *A,
619 const mbedtls_mpi_uint *N, size_t AN_limbs,
620 const mbedtls_mpi_uint *E, size_t E_limbs,
621 const mbedtls_mpi_uint *RR,
622 mbedtls_mpi_uint *T);
Janos Follathb6673f02022-09-30 14:13:14 +0100623
Janos Follath5933f692022-11-02 14:35:17 +0000624/* END MERGE SLOT 1 */
625
626/* BEGIN MERGE SLOT 2 */
627
628/* END MERGE SLOT 2 */
629
630/* BEGIN MERGE SLOT 3 */
631
Hanno Beckerd9b23482022-08-25 08:25:19 +0100632/**
633 * \brief Subtract unsigned integer from known-size large unsigned integers.
634 * Return the borrow.
635 *
Tom Cosgrovef7ff4c92022-08-25 08:39:07 +0100636 * \param[out] X The result of the subtraction.
637 * \param[in] A The left operand.
638 * \param b The unsigned scalar to subtract.
639 * \param limbs Number of limbs of \p X and \p A.
Hanno Beckerd9b23482022-08-25 08:25:19 +0100640 *
Tom Cosgrovef7ff4c92022-08-25 08:39:07 +0100641 * \return 1 if `A < b`.
642 * 0 if `A >= b`.
Hanno Beckerd9b23482022-08-25 08:25:19 +0100643 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100644mbedtls_mpi_uint mbedtls_mpi_core_sub_int(mbedtls_mpi_uint *X,
645 const mbedtls_mpi_uint *A,
646 mbedtls_mpi_uint b,
647 size_t limbs);
Hanno Beckerd9b23482022-08-25 08:25:19 +0100648
Tom Cosgrove30f3b4d2022-12-12 16:54:57 +0000649/**
650 * \brief Determine if a given MPI has the value \c 0 in constant time with
651 * respect to the value (but not with respect to the number of limbs).
652 *
653 * \param[in] A The MPI to test.
654 * \param limbs Number of limbs in \p A.
655 *
656 * \return 0 if `A == 0`
657 * non-0 (may be any value) if `A != 0`.
658 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100659mbedtls_mpi_uint mbedtls_mpi_core_check_zero_ct(const mbedtls_mpi_uint *A,
660 size_t limbs);
Tom Cosgrove30f3b4d2022-12-12 16:54:57 +0000661
Tom Cosgrove28ff92c2022-12-12 17:06:27 +0000662/**
663 * \brief Returns the number of limbs of working memory required for
664 * a call to `mbedtls_mpi_core_montmul()`.
665 *
666 * \param AN_limbs The number of limbs in the input `A` and the modulus `N`
667 * (they must be the same size) that will be given to
668 * `mbedtls_mpi_core_montmul()` or one of the other functions
669 * that specifies this as the amount of working memory needed.
670 *
671 * \return The number of limbs of working memory required by
672 * `mbedtls_mpi_core_montmul()` (or other similar function).
673 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100674static inline size_t mbedtls_mpi_core_montmul_working_limbs(size_t AN_limbs)
Tom Cosgrove28ff92c2022-12-12 17:06:27 +0000675{
Gilles Peskine449bd832023-01-11 14:50:10 +0100676 return 2 * AN_limbs + 1;
Tom Cosgrove28ff92c2022-12-12 17:06:27 +0000677}
678
Tom Cosgrove786848b2022-12-13 10:45:19 +0000679/** Convert an MPI into Montgomery form.
680 *
681 * \p X may be aliased to \p A, but may not otherwise overlap it.
682 *
Tom Cosgrove5c8505f2023-03-07 11:39:52 +0000683 * \p X may not alias \p N (it is in canonical form, so must be strictly less
Tom Cosgrove786848b2022-12-13 10:45:19 +0000684 * than \p N). Nor may it alias or overlap \p rr (this is unlikely to be
685 * required in practice.)
686 *
687 * This function is a thin wrapper around `mbedtls_mpi_core_montmul()` that is
688 * an alternative to calling `mbedtls_mpi_mod_raw_to_mont_rep()` when we
689 * don't want to allocate memory.
690 *
691 * \param[out] X The result of the conversion.
692 * Must have the same number of limbs as \p A.
693 * \param[in] A The MPI to convert into Montgomery form.
694 * Must have the same number of limbs as the modulus.
695 * \param[in] N The address of the modulus, which gives the size of
Tom Cosgrovef7237542022-12-16 16:10:36 +0000696 * the base `R` = 2^(biL*N->limbs).
Tom Cosgrove786848b2022-12-13 10:45:19 +0000697 * \param[in] AN_limbs The number of limbs in \p X, \p A, \p N and \p rr.
698 * \param mm The Montgomery constant for \p N: -N^-1 mod 2^biL.
Tom Cosgroveb38c2ed2022-12-14 13:11:46 +0000699 * This can be determined by calling
Tom Cosgrove786848b2022-12-13 10:45:19 +0000700 * `mbedtls_mpi_core_montmul_init()`.
701 * \param[in] rr The residue for `2^{2*n*biL} mod N`.
702 * \param[in,out] T Temporary storage of size at least
703 * `mbedtls_mpi_core_montmul_working_limbs(AN_limbs)`
704 * limbs.
705 * Its initial content is unused and
706 * its final content is indeterminate.
707 * It must not alias or otherwise overlap any of the
708 * other parameters.
709 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100710void mbedtls_mpi_core_to_mont_rep(mbedtls_mpi_uint *X,
711 const mbedtls_mpi_uint *A,
712 const mbedtls_mpi_uint *N,
713 size_t AN_limbs,
714 mbedtls_mpi_uint mm,
715 const mbedtls_mpi_uint *rr,
716 mbedtls_mpi_uint *T);
Tom Cosgrove786848b2022-12-13 10:45:19 +0000717
718/** Convert an MPI from Montgomery form.
719 *
720 * \p X may be aliased to \p A, but may not otherwise overlap it.
721 *
Tom Cosgrove5c8505f2023-03-07 11:39:52 +0000722 * \p X may not alias \p N (it is in canonical form, so must be strictly less
Tom Cosgrove786848b2022-12-13 10:45:19 +0000723 * than \p N).
724 *
725 * This function is a thin wrapper around `mbedtls_mpi_core_montmul()` that is
726 * an alternative to calling `mbedtls_mpi_mod_raw_from_mont_rep()` when we
727 * don't want to allocate memory.
728 *
729 * \param[out] X The result of the conversion.
730 * Must have the same number of limbs as \p A.
731 * \param[in] A The MPI to convert from Montgomery form.
732 * Must have the same number of limbs as the modulus.
733 * \param[in] N The address of the modulus, which gives the size of
Tom Cosgrovef7237542022-12-16 16:10:36 +0000734 * the base `R` = 2^(biL*N->limbs).
Tom Cosgrove786848b2022-12-13 10:45:19 +0000735 * \param[in] AN_limbs The number of limbs in \p X, \p A and \p N.
736 * \param mm The Montgomery constant for \p N: -N^-1 mod 2^biL.
Tom Cosgroveb38c2ed2022-12-14 13:11:46 +0000737 * This can be determined by calling
Tom Cosgrove786848b2022-12-13 10:45:19 +0000738 * `mbedtls_mpi_core_montmul_init()`.
739 * \param[in,out] T Temporary storage of size at least
740 * `mbedtls_mpi_core_montmul_working_limbs(AN_limbs)`
741 * limbs.
742 * Its initial content is unused and
743 * its final content is indeterminate.
744 * It must not alias or otherwise overlap any of the
745 * other parameters.
746 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100747void mbedtls_mpi_core_from_mont_rep(mbedtls_mpi_uint *X,
748 const mbedtls_mpi_uint *A,
749 const mbedtls_mpi_uint *N,
750 size_t AN_limbs,
751 mbedtls_mpi_uint mm,
752 mbedtls_mpi_uint *T);
Tom Cosgrove786848b2022-12-13 10:45:19 +0000753
Janos Follath5933f692022-11-02 14:35:17 +0000754/* END MERGE SLOT 3 */
755
756/* BEGIN MERGE SLOT 4 */
757
758/* END MERGE SLOT 4 */
759
760/* BEGIN MERGE SLOT 5 */
761
762/* END MERGE SLOT 5 */
763
764/* BEGIN MERGE SLOT 6 */
765
766/* END MERGE SLOT 6 */
767
768/* BEGIN MERGE SLOT 7 */
769
770/* END MERGE SLOT 7 */
771
772/* BEGIN MERGE SLOT 8 */
773
774/* END MERGE SLOT 8 */
775
776/* BEGIN MERGE SLOT 9 */
777
778/* END MERGE SLOT 9 */
779
780/* BEGIN MERGE SLOT 10 */
781
782/* END MERGE SLOT 10 */
783
Gabor Mezeib9030702022-07-18 23:09:45 +0200784#endif /* MBEDTLS_BIGNUM_CORE_H */