1a8e1175bSopenharmony_ci/**
2a8e1175bSopenharmony_ci *  Low-level modular bignum functions
3a8e1175bSopenharmony_ci *
4a8e1175bSopenharmony_ci *  This interface should only be used by the higher-level modular bignum
5a8e1175bSopenharmony_ci *  module (bignum_mod.c) and the ECP module (ecp.c, ecp_curves.c). All other
6a8e1175bSopenharmony_ci *  modules should use the high-level modular bignum interface (bignum_mod.h)
7a8e1175bSopenharmony_ci *  or the legacy bignum interface (bignum.h).
8a8e1175bSopenharmony_ci *
9a8e1175bSopenharmony_ci * This is a low-level interface to operations on integers modulo which
10a8e1175bSopenharmony_ci * has no protection against passing invalid arguments such as arrays of
11a8e1175bSopenharmony_ci * the wrong size. The functions in bignum_mod.h provide a higher-level
12a8e1175bSopenharmony_ci * interface that includes protections against accidental misuse, at the
13a8e1175bSopenharmony_ci * expense of code size and sometimes more cumbersome memory management.
14a8e1175bSopenharmony_ci *
15a8e1175bSopenharmony_ci * The functions in this module obey the following conventions unless
16a8e1175bSopenharmony_ci * explicitly indicated otherwise:
17a8e1175bSopenharmony_ci * - **Modulus parameters**: the modulus is passed as a pointer to a structure
18a8e1175bSopenharmony_ci *   of type #mbedtls_mpi_mod_modulus. The structure must be set up with an
19a8e1175bSopenharmony_ci *   array of limbs storing the bignum value of the modulus. The modulus must
20a8e1175bSopenharmony_ci *   be odd and is assumed to have no leading zeroes. The modulus is usually
21a8e1175bSopenharmony_ci *   named \c N and is usually input-only.
22a8e1175bSopenharmony_ci * - **Bignum parameters**: Bignums are passed as pointers to an array of
23a8e1175bSopenharmony_ci *   limbs. A limb has the type #mbedtls_mpi_uint. Unless otherwise specified:
24a8e1175bSopenharmony_ci *     - Bignum parameters called \c A, \c B, ... are inputs, and are not
25a8e1175bSopenharmony_ci *       modified by the function.
26a8e1175bSopenharmony_ci *     - Bignum parameters called \c X, \c Y are outputs or input-output.
27a8e1175bSopenharmony_ci *       The initial content of output-only parameters is ignored.
28a8e1175bSopenharmony_ci *     - \c T is a temporary storage area. The initial content of such a
29a8e1175bSopenharmony_ci *       parameter is ignored and the final content is unspecified.
30a8e1175bSopenharmony_ci * - **Bignum sizes**: bignum sizes are usually expressed by the \c limbs
31a8e1175bSopenharmony_ci *   member of the modulus argument. All bignum parameters must have the same
32a8e1175bSopenharmony_ci *   number of limbs as the modulus. All bignum sizes must be at least 1 and
33a8e1175bSopenharmony_ci *   must be significantly less than #SIZE_MAX. The behavior if a size is 0 is
34a8e1175bSopenharmony_ci *   undefined.
35a8e1175bSopenharmony_ci * - **Bignum representation**: the representation of inputs and outputs is
36a8e1175bSopenharmony_ci *   specified by the \c int_rep field of the modulus for arithmetic
37a8e1175bSopenharmony_ci *   functions. Utility functions may allow for different representation.
38a8e1175bSopenharmony_ci * - **Parameter ordering**: for bignum parameters, outputs come before inputs.
39a8e1175bSopenharmony_ci *   The modulus is passed after other bignum input parameters. Temporaries
40a8e1175bSopenharmony_ci *   come last.
41a8e1175bSopenharmony_ci * - **Aliasing**: in general, output bignums may be aliased to one or more
42a8e1175bSopenharmony_ci *   inputs. Modulus values may not be aliased to any other parameter. Outputs
43a8e1175bSopenharmony_ci *   may not be aliased to one another. Temporaries may not be aliased to any
44a8e1175bSopenharmony_ci *   other parameter.
45a8e1175bSopenharmony_ci * - **Overlap**: apart from aliasing of limb array pointers (where two
46a8e1175bSopenharmony_ci *   arguments are equal pointers), overlap is not supported and may result
47a8e1175bSopenharmony_ci *   in undefined behavior.
48a8e1175bSopenharmony_ci * - **Error handling**: This is a low-level module. Functions generally do not
49a8e1175bSopenharmony_ci *   try to protect against invalid arguments such as nonsensical sizes or
50a8e1175bSopenharmony_ci *   null pointers. Note that passing bignums with a different size than the
51a8e1175bSopenharmony_ci *   modulus may lead to buffer overflows. Some functions which allocate
52a8e1175bSopenharmony_ci *   memory or handle reading/writing of bignums will return an error if
53a8e1175bSopenharmony_ci *   memory allocation fails or if buffer sizes are invalid.
54a8e1175bSopenharmony_ci * - **Modular representatives**: all functions expect inputs to be in the
55a8e1175bSopenharmony_ci *   range [0, \c N - 1] and guarantee outputs in the range [0, \c N - 1]. If
56a8e1175bSopenharmony_ci *   an input is out of range, outputs are fully unspecified, though bignum
57a8e1175bSopenharmony_ci *   values out of range should not cause buffer overflows (beware that this is
58a8e1175bSopenharmony_ci *   not extensively tested).
59a8e1175bSopenharmony_ci */
60a8e1175bSopenharmony_ci
61a8e1175bSopenharmony_ci/*
62a8e1175bSopenharmony_ci *  Copyright The Mbed TLS Contributors
63a8e1175bSopenharmony_ci *  SPDX-License-Identifier: Apache-2.0
64a8e1175bSopenharmony_ci *
65a8e1175bSopenharmony_ci *  Licensed under the Apache License, Version 2.0 (the "License"); you may
66a8e1175bSopenharmony_ci *  not use this file except in compliance with the License.
67a8e1175bSopenharmony_ci *  You may obtain a copy of the License at
68a8e1175bSopenharmony_ci *
69a8e1175bSopenharmony_ci *  http://www.apache.org/licenses/LICENSE-2.0
70a8e1175bSopenharmony_ci *
71a8e1175bSopenharmony_ci *  Unless required by applicable law or agreed to in writing, software
72a8e1175bSopenharmony_ci *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
73a8e1175bSopenharmony_ci *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
74a8e1175bSopenharmony_ci *  See the License for the specific language governing permissions and
75a8e1175bSopenharmony_ci *  limitations under the License.
76a8e1175bSopenharmony_ci */
77a8e1175bSopenharmony_ci
78a8e1175bSopenharmony_ci#ifndef MBEDTLS_BIGNUM_MOD_RAW_H
79a8e1175bSopenharmony_ci#define MBEDTLS_BIGNUM_MOD_RAW_H
80a8e1175bSopenharmony_ci
81a8e1175bSopenharmony_ci#include "common.h"
82a8e1175bSopenharmony_ci
83a8e1175bSopenharmony_ci#if defined(MBEDTLS_BIGNUM_C)
84a8e1175bSopenharmony_ci#include "mbedtls/bignum.h"
85a8e1175bSopenharmony_ci#endif
86a8e1175bSopenharmony_ci
87a8e1175bSopenharmony_ci#include "bignum_mod.h"
88a8e1175bSopenharmony_ci
89a8e1175bSopenharmony_ci/**
90a8e1175bSopenharmony_ci * \brief   Perform a safe conditional copy of an MPI which doesn't reveal
91a8e1175bSopenharmony_ci *          whether the assignment was done or not.
92a8e1175bSopenharmony_ci *
93a8e1175bSopenharmony_ci * The size to copy is determined by \p N.
94a8e1175bSopenharmony_ci *
95a8e1175bSopenharmony_ci * \param[out] X        The address of the destination MPI.
96a8e1175bSopenharmony_ci *                      This must be initialized. Must have enough limbs to
97a8e1175bSopenharmony_ci *                      store the full value of \p A.
98a8e1175bSopenharmony_ci * \param[in]  A        The address of the source MPI. This must be initialized.
99a8e1175bSopenharmony_ci * \param[in]  N        The address of the modulus related to \p X and \p A.
100a8e1175bSopenharmony_ci * \param      assign   The condition deciding whether to perform the
101a8e1175bSopenharmony_ci *                      assignment or not. Must be either 0 or 1:
102a8e1175bSopenharmony_ci *                      * \c 1: Perform the assignment `X = A`.
103a8e1175bSopenharmony_ci *                      * \c 0: Keep the original value of \p X.
104a8e1175bSopenharmony_ci *
105a8e1175bSopenharmony_ci * \note           This function avoids leaking any information about whether
106a8e1175bSopenharmony_ci *                 the assignment was done or not.
107a8e1175bSopenharmony_ci *
108a8e1175bSopenharmony_ci * \warning        If \p assign is neither 0 nor 1, the result of this function
109a8e1175bSopenharmony_ci *                 is indeterminate, and the resulting value in \p X might be
110a8e1175bSopenharmony_ci *                 neither its original value nor the value in \p A.
111a8e1175bSopenharmony_ci */
112a8e1175bSopenharmony_civoid mbedtls_mpi_mod_raw_cond_assign(mbedtls_mpi_uint *X,
113a8e1175bSopenharmony_ci                                     const mbedtls_mpi_uint *A,
114a8e1175bSopenharmony_ci                                     const mbedtls_mpi_mod_modulus *N,
115a8e1175bSopenharmony_ci                                     unsigned char assign);
116a8e1175bSopenharmony_ci
117a8e1175bSopenharmony_ci/**
118a8e1175bSopenharmony_ci * \brief   Perform a safe conditional swap of two MPIs which doesn't reveal
119a8e1175bSopenharmony_ci *          whether the swap was done or not.
120a8e1175bSopenharmony_ci *
121a8e1175bSopenharmony_ci * The size to swap is determined by \p N.
122a8e1175bSopenharmony_ci *
123a8e1175bSopenharmony_ci * \param[in,out] X     The address of the first MPI. This must be initialized.
124a8e1175bSopenharmony_ci * \param[in,out] Y     The address of the second MPI. This must be initialized.
125a8e1175bSopenharmony_ci * \param[in]     N     The address of the modulus related to \p X and \p Y.
126a8e1175bSopenharmony_ci * \param         swap  The condition deciding whether to perform
127a8e1175bSopenharmony_ci *                      the swap or not. Must be either 0 or 1:
128a8e1175bSopenharmony_ci *                      * \c 1: Swap the values of \p X and \p Y.
129a8e1175bSopenharmony_ci *                      * \c 0: Keep the original values of \p X and \p Y.
130a8e1175bSopenharmony_ci *
131a8e1175bSopenharmony_ci * \note           This function avoids leaking any information about whether
132a8e1175bSopenharmony_ci *                 the swap was done or not.
133a8e1175bSopenharmony_ci *
134a8e1175bSopenharmony_ci * \warning        If \p swap is neither 0 nor 1, the result of this function
135a8e1175bSopenharmony_ci *                 is indeterminate, and both \p X and \p Y might end up with
136a8e1175bSopenharmony_ci *                 values different to either of the original ones.
137a8e1175bSopenharmony_ci */
138a8e1175bSopenharmony_civoid mbedtls_mpi_mod_raw_cond_swap(mbedtls_mpi_uint *X,
139a8e1175bSopenharmony_ci                                   mbedtls_mpi_uint *Y,
140a8e1175bSopenharmony_ci                                   const mbedtls_mpi_mod_modulus *N,
141a8e1175bSopenharmony_ci                                   unsigned char swap);
142a8e1175bSopenharmony_ci
143a8e1175bSopenharmony_ci/** Import X from unsigned binary data.
144a8e1175bSopenharmony_ci *
145a8e1175bSopenharmony_ci * The MPI needs to have enough limbs to store the full value (including any
146a8e1175bSopenharmony_ci * most significant zero bytes in the input).
147a8e1175bSopenharmony_ci *
148a8e1175bSopenharmony_ci * \param[out] X        The address of the MPI. The size is determined by \p N.
149a8e1175bSopenharmony_ci *                      (In particular, it must have at least as many limbs as
150a8e1175bSopenharmony_ci *                      the modulus \p N.)
151a8e1175bSopenharmony_ci * \param[in] N         The address of the modulus related to \p X.
152a8e1175bSopenharmony_ci * \param[in] input     The input buffer to import from.
153a8e1175bSopenharmony_ci * \param input_length  The length in bytes of \p input.
154a8e1175bSopenharmony_ci * \param ext_rep       The endianness of the number in the input buffer.
155a8e1175bSopenharmony_ci *
156a8e1175bSopenharmony_ci * \return       \c 0 if successful.
157a8e1175bSopenharmony_ci * \return       #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p X isn't
158a8e1175bSopenharmony_ci *               large enough to hold the value in \p input.
159a8e1175bSopenharmony_ci * \return       #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if the external representation
160a8e1175bSopenharmony_ci *               of \p N is invalid or \p X is not less than \p N.
161a8e1175bSopenharmony_ci */
162a8e1175bSopenharmony_ciint mbedtls_mpi_mod_raw_read(mbedtls_mpi_uint *X,
163a8e1175bSopenharmony_ci                             const mbedtls_mpi_mod_modulus *N,
164a8e1175bSopenharmony_ci                             const unsigned char *input,
165a8e1175bSopenharmony_ci                             size_t input_length,
166a8e1175bSopenharmony_ci                             mbedtls_mpi_mod_ext_rep ext_rep);
167a8e1175bSopenharmony_ci
168a8e1175bSopenharmony_ci/** Export A into unsigned binary data.
169a8e1175bSopenharmony_ci *
170a8e1175bSopenharmony_ci * \param[in] A         The address of the MPI. The size is determined by \p N.
171a8e1175bSopenharmony_ci *                      (In particular, it must have at least as many limbs as
172a8e1175bSopenharmony_ci *                      the modulus \p N.)
173a8e1175bSopenharmony_ci * \param[in] N         The address of the modulus related to \p A.
174a8e1175bSopenharmony_ci * \param[out] output   The output buffer to export to.
175a8e1175bSopenharmony_ci * \param output_length The length in bytes of \p output.
176a8e1175bSopenharmony_ci * \param ext_rep       The endianness in which the number should be written into the output buffer.
177a8e1175bSopenharmony_ci *
178a8e1175bSopenharmony_ci * \return       \c 0 if successful.
179a8e1175bSopenharmony_ci * \return       #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p output isn't
180a8e1175bSopenharmony_ci *               large enough to hold the value of \p A.
181a8e1175bSopenharmony_ci * \return       #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if the external representation
182a8e1175bSopenharmony_ci *               of \p N is invalid.
183a8e1175bSopenharmony_ci */
184a8e1175bSopenharmony_ciint mbedtls_mpi_mod_raw_write(const mbedtls_mpi_uint *A,
185a8e1175bSopenharmony_ci                              const mbedtls_mpi_mod_modulus *N,
186a8e1175bSopenharmony_ci                              unsigned char *output,
187a8e1175bSopenharmony_ci                              size_t output_length,
188a8e1175bSopenharmony_ci                              mbedtls_mpi_mod_ext_rep ext_rep);
189a8e1175bSopenharmony_ci
190a8e1175bSopenharmony_ci/* BEGIN MERGE SLOT 1 */
191a8e1175bSopenharmony_ci
192a8e1175bSopenharmony_ci/* END MERGE SLOT 1 */
193a8e1175bSopenharmony_ci
194a8e1175bSopenharmony_ci/* BEGIN MERGE SLOT 2 */
195a8e1175bSopenharmony_ci
196a8e1175bSopenharmony_ci/** \brief  Subtract two MPIs, returning the residue modulo the specified
197a8e1175bSopenharmony_ci *          modulus.
198a8e1175bSopenharmony_ci *
199a8e1175bSopenharmony_ci * The size of the operation is determined by \p N. \p A and \p B must have
200a8e1175bSopenharmony_ci * the same number of limbs as \p N.
201a8e1175bSopenharmony_ci *
202a8e1175bSopenharmony_ci * \p X may be aliased to \p A or \p B, or even both, but may not overlap
203a8e1175bSopenharmony_ci * either otherwise.
204a8e1175bSopenharmony_ci *
205a8e1175bSopenharmony_ci * \param[out] X        The address of the result MPI.
206a8e1175bSopenharmony_ci *                      This must be initialized. Must have enough limbs to
207a8e1175bSopenharmony_ci *                      store the full value of the result.
208a8e1175bSopenharmony_ci * \param[in]  A        The address of the first MPI. This must be initialized.
209a8e1175bSopenharmony_ci * \param[in]  B        The address of the second MPI. This must be initialized.
210a8e1175bSopenharmony_ci * \param[in]  N        The address of the modulus. Used to perform a modulo
211a8e1175bSopenharmony_ci *                      operation on the result of the subtraction.
212a8e1175bSopenharmony_ci */
213a8e1175bSopenharmony_civoid mbedtls_mpi_mod_raw_sub(mbedtls_mpi_uint *X,
214a8e1175bSopenharmony_ci                             const mbedtls_mpi_uint *A,
215a8e1175bSopenharmony_ci                             const mbedtls_mpi_uint *B,
216a8e1175bSopenharmony_ci                             const mbedtls_mpi_mod_modulus *N);
217a8e1175bSopenharmony_ci
218a8e1175bSopenharmony_ci/** \brief  Multiply two MPIs, returning the residue modulo the specified
219a8e1175bSopenharmony_ci *          modulus.
220a8e1175bSopenharmony_ci *
221a8e1175bSopenharmony_ci * \note Currently handles the case when `N->int_rep` is
222a8e1175bSopenharmony_ci * MBEDTLS_MPI_MOD_REP_MONTGOMERY.
223a8e1175bSopenharmony_ci *
224a8e1175bSopenharmony_ci * The size of the operation is determined by \p N. \p A, \p B and \p X must
225a8e1175bSopenharmony_ci * all be associated with the modulus \p N and must all have the same number
226a8e1175bSopenharmony_ci * of limbs as \p N.
227a8e1175bSopenharmony_ci *
228a8e1175bSopenharmony_ci * \p X may be aliased to \p A or \p B, or even both, but may not overlap
229a8e1175bSopenharmony_ci * either otherwise. They may not alias \p N (since they must be in canonical
230a8e1175bSopenharmony_ci * form, they cannot == \p N).
231a8e1175bSopenharmony_ci *
232a8e1175bSopenharmony_ci * \param[out] X        The address of the result MPI. Must have the same
233a8e1175bSopenharmony_ci *                      number of limbs as \p N.
234a8e1175bSopenharmony_ci *                      On successful completion, \p X contains the result of
235a8e1175bSopenharmony_ci *                      the multiplication `A * B * R^-1` mod N where
236a8e1175bSopenharmony_ci *                      `R = 2^(biL * N->limbs)`.
237a8e1175bSopenharmony_ci * \param[in]  A        The address of the first MPI.
238a8e1175bSopenharmony_ci * \param[in]  B        The address of the second MPI.
239a8e1175bSopenharmony_ci * \param[in]  N        The address of the modulus. Used to perform a modulo
240a8e1175bSopenharmony_ci *                      operation on the result of the multiplication.
241a8e1175bSopenharmony_ci * \param[in,out] T     Temporary storage of size at least 2 * N->limbs + 1
242a8e1175bSopenharmony_ci *                      limbs. Its initial content is unused and
243a8e1175bSopenharmony_ci *                      its final content is indeterminate.
244a8e1175bSopenharmony_ci *                      It must not alias or otherwise overlap any of the
245a8e1175bSopenharmony_ci *                      other parameters.
246a8e1175bSopenharmony_ci */
247a8e1175bSopenharmony_civoid mbedtls_mpi_mod_raw_mul(mbedtls_mpi_uint *X,
248a8e1175bSopenharmony_ci                             const mbedtls_mpi_uint *A,
249a8e1175bSopenharmony_ci                             const mbedtls_mpi_uint *B,
250a8e1175bSopenharmony_ci                             const mbedtls_mpi_mod_modulus *N,
251a8e1175bSopenharmony_ci                             mbedtls_mpi_uint *T);
252a8e1175bSopenharmony_ci
253a8e1175bSopenharmony_ci/* END MERGE SLOT 2 */
254a8e1175bSopenharmony_ci
255a8e1175bSopenharmony_ci/* BEGIN MERGE SLOT 3 */
256a8e1175bSopenharmony_ci
257a8e1175bSopenharmony_ci/**
258a8e1175bSopenharmony_ci * \brief          Returns the number of limbs of working memory required for
259a8e1175bSopenharmony_ci *                 a call to `mbedtls_mpi_mod_raw_inv_prime()`.
260a8e1175bSopenharmony_ci *
261a8e1175bSopenharmony_ci * \note           This will always be at least
262a8e1175bSopenharmony_ci *                 `mbedtls_mpi_core_montmul_working_limbs(AN_limbs)`,
263a8e1175bSopenharmony_ci *                 i.e. sufficient for a call to `mbedtls_mpi_core_montmul()`.
264a8e1175bSopenharmony_ci *
265a8e1175bSopenharmony_ci * \param AN_limbs The number of limbs in the input `A` and the modulus `N`
266a8e1175bSopenharmony_ci *                 (they must be the same size) that will be given to
267a8e1175bSopenharmony_ci *                 `mbedtls_mpi_mod_raw_inv_prime()`.
268a8e1175bSopenharmony_ci *
269a8e1175bSopenharmony_ci * \return         The number of limbs of working memory required by
270a8e1175bSopenharmony_ci *                 `mbedtls_mpi_mod_raw_inv_prime()`.
271a8e1175bSopenharmony_ci */
272a8e1175bSopenharmony_cisize_t mbedtls_mpi_mod_raw_inv_prime_working_limbs(size_t AN_limbs);
273a8e1175bSopenharmony_ci
274a8e1175bSopenharmony_ci/**
275a8e1175bSopenharmony_ci * \brief Perform fixed-width modular inversion of a Montgomery-form MPI with
276a8e1175bSopenharmony_ci *        respect to a modulus \p N that must be prime.
277a8e1175bSopenharmony_ci *
278a8e1175bSopenharmony_ci * \p X may be aliased to \p A, but not to \p N or \p RR.
279a8e1175bSopenharmony_ci *
280a8e1175bSopenharmony_ci * \param[out] X     The modular inverse of \p A with respect to \p N.
281a8e1175bSopenharmony_ci *                   Will be in Montgomery form.
282a8e1175bSopenharmony_ci * \param[in] A      The number to calculate the modular inverse of.
283a8e1175bSopenharmony_ci *                   Must be in Montgomery form. Must not be 0.
284a8e1175bSopenharmony_ci * \param[in] N      The modulus, as a little-endian array of length \p AN_limbs.
285a8e1175bSopenharmony_ci *                   Must be prime.
286a8e1175bSopenharmony_ci * \param AN_limbs   The number of limbs in \p A, \p N and \p RR.
287a8e1175bSopenharmony_ci * \param[in] RR     The precomputed residue of 2^{2*biL} modulo N, as a little-
288a8e1175bSopenharmony_ci *                   endian array of length \p AN_limbs.
289a8e1175bSopenharmony_ci * \param[in,out] T  Temporary storage of at least the number of limbs returned
290a8e1175bSopenharmony_ci *                   by `mbedtls_mpi_mod_raw_inv_prime_working_limbs()`.
291a8e1175bSopenharmony_ci *                   Its initial content is unused and its final content is
292a8e1175bSopenharmony_ci *                   indeterminate.
293a8e1175bSopenharmony_ci *                   It must not alias or otherwise overlap any of the other
294a8e1175bSopenharmony_ci *                   parameters.
295a8e1175bSopenharmony_ci *                   It is up to the caller to zeroize \p T when it is no
296a8e1175bSopenharmony_ci *                   longer needed, and before freeing it if it was dynamically
297a8e1175bSopenharmony_ci *                   allocated.
298a8e1175bSopenharmony_ci */
299a8e1175bSopenharmony_civoid mbedtls_mpi_mod_raw_inv_prime(mbedtls_mpi_uint *X,
300a8e1175bSopenharmony_ci                                   const mbedtls_mpi_uint *A,
301a8e1175bSopenharmony_ci                                   const mbedtls_mpi_uint *N,
302a8e1175bSopenharmony_ci                                   size_t AN_limbs,
303a8e1175bSopenharmony_ci                                   const mbedtls_mpi_uint *RR,
304a8e1175bSopenharmony_ci                                   mbedtls_mpi_uint *T);
305a8e1175bSopenharmony_ci
306a8e1175bSopenharmony_ci/* END MERGE SLOT 3 */
307a8e1175bSopenharmony_ci
308a8e1175bSopenharmony_ci/* BEGIN MERGE SLOT 4 */
309a8e1175bSopenharmony_ci
310a8e1175bSopenharmony_ci/* END MERGE SLOT 4 */
311a8e1175bSopenharmony_ci
312a8e1175bSopenharmony_ci/* BEGIN MERGE SLOT 5 */
313a8e1175bSopenharmony_ci/**
314a8e1175bSopenharmony_ci * \brief Perform a known-size modular addition.
315a8e1175bSopenharmony_ci *
316a8e1175bSopenharmony_ci * Calculate `A + B modulo N`.
317a8e1175bSopenharmony_ci *
318a8e1175bSopenharmony_ci * The number of limbs in each operand, and the result, is given by the
319a8e1175bSopenharmony_ci * modulus \p N.
320a8e1175bSopenharmony_ci *
321a8e1175bSopenharmony_ci * \p X may be aliased to \p A or \p B, or even both, but may not overlap
322a8e1175bSopenharmony_ci * either otherwise.
323a8e1175bSopenharmony_ci *
324a8e1175bSopenharmony_ci * \param[out] X    The result of the modular addition.
325a8e1175bSopenharmony_ci * \param[in] A     Little-endian presentation of the left operand. This
326a8e1175bSopenharmony_ci *                  must be smaller than \p N.
327a8e1175bSopenharmony_ci * \param[in] B     Little-endian presentation of the right operand. This
328a8e1175bSopenharmony_ci *                  must be smaller than \p N.
329a8e1175bSopenharmony_ci * \param[in] N     The address of the modulus.
330a8e1175bSopenharmony_ci */
331a8e1175bSopenharmony_civoid mbedtls_mpi_mod_raw_add(mbedtls_mpi_uint *X,
332a8e1175bSopenharmony_ci                             const mbedtls_mpi_uint *A,
333a8e1175bSopenharmony_ci                             const mbedtls_mpi_uint *B,
334a8e1175bSopenharmony_ci                             const mbedtls_mpi_mod_modulus *N);
335a8e1175bSopenharmony_ci/* END MERGE SLOT 5 */
336a8e1175bSopenharmony_ci
337a8e1175bSopenharmony_ci/* BEGIN MERGE SLOT 6 */
338a8e1175bSopenharmony_ci
339a8e1175bSopenharmony_ci/** Convert an MPI from canonical representation (little-endian limb array)
340a8e1175bSopenharmony_ci * to the representation associated with the modulus.
341a8e1175bSopenharmony_ci *
342a8e1175bSopenharmony_ci * \param[in,out] X The limb array to convert.
343a8e1175bSopenharmony_ci *                  It must have as many limbs as \p N.
344a8e1175bSopenharmony_ci *                  It is converted in place.
345a8e1175bSopenharmony_ci *                  If this function returns an error, the content of \p X
346a8e1175bSopenharmony_ci *                  is unspecified.
347a8e1175bSopenharmony_ci * \param[in] N     The modulus structure.
348a8e1175bSopenharmony_ci *
349a8e1175bSopenharmony_ci * \return          \c 0 if successful.
350a8e1175bSopenharmony_ci *                  Otherwise an \c MBEDTLS_ERR_MPI_xxx error code.
351a8e1175bSopenharmony_ci */
352a8e1175bSopenharmony_ciint mbedtls_mpi_mod_raw_canonical_to_modulus_rep(
353a8e1175bSopenharmony_ci    mbedtls_mpi_uint *X,
354a8e1175bSopenharmony_ci    const mbedtls_mpi_mod_modulus *N);
355a8e1175bSopenharmony_ci
356a8e1175bSopenharmony_ci/** Convert an MPI from the representation associated with the modulus
357a8e1175bSopenharmony_ci * to canonical representation (little-endian limb array).
358a8e1175bSopenharmony_ci *
359a8e1175bSopenharmony_ci * \param[in,out] X The limb array to convert.
360a8e1175bSopenharmony_ci *                  It must have as many limbs as \p N.
361a8e1175bSopenharmony_ci *                  It is converted in place.
362a8e1175bSopenharmony_ci *                  If this function returns an error, the content of \p X
363a8e1175bSopenharmony_ci *                  is unspecified.
364a8e1175bSopenharmony_ci * \param[in] N     The modulus structure.
365a8e1175bSopenharmony_ci *
366a8e1175bSopenharmony_ci * \return          \c 0 if successful.
367a8e1175bSopenharmony_ci *                  Otherwise an \c MBEDTLS_ERR_MPI_xxx error code.
368a8e1175bSopenharmony_ci */
369a8e1175bSopenharmony_ciint mbedtls_mpi_mod_raw_modulus_to_canonical_rep(
370a8e1175bSopenharmony_ci    mbedtls_mpi_uint *X,
371a8e1175bSopenharmony_ci    const mbedtls_mpi_mod_modulus *N);
372a8e1175bSopenharmony_ci
373a8e1175bSopenharmony_ci/** Generate a random number uniformly in a range.
374a8e1175bSopenharmony_ci *
375a8e1175bSopenharmony_ci * This function generates a random number between \p min inclusive and
376a8e1175bSopenharmony_ci * \p N exclusive.
377a8e1175bSopenharmony_ci *
378a8e1175bSopenharmony_ci * The procedure complies with RFC 6979 §3.3 (deterministic ECDSA)
379a8e1175bSopenharmony_ci * when the RNG is a suitably parametrized instance of HMAC_DRBG
380a8e1175bSopenharmony_ci * and \p min is \c 1.
381a8e1175bSopenharmony_ci *
382a8e1175bSopenharmony_ci * \note           There are `N - min` possible outputs. The lower bound
383a8e1175bSopenharmony_ci *                 \p min can be reached, but the upper bound \p N cannot.
384a8e1175bSopenharmony_ci *
385a8e1175bSopenharmony_ci * \param X        The destination MPI, in canonical representation modulo \p N.
386a8e1175bSopenharmony_ci *                 It must not be aliased with \p N or otherwise overlap it.
387a8e1175bSopenharmony_ci * \param min      The minimum value to return. It must be strictly smaller
388a8e1175bSopenharmony_ci *                 than \b N.
389a8e1175bSopenharmony_ci * \param N        The modulus.
390a8e1175bSopenharmony_ci *                 This is the upper bound of the output range, exclusive.
391a8e1175bSopenharmony_ci * \param f_rng    The RNG function to use. This must not be \c NULL.
392a8e1175bSopenharmony_ci * \param p_rng    The RNG parameter to be passed to \p f_rng.
393a8e1175bSopenharmony_ci *
394a8e1175bSopenharmony_ci * \return         \c 0 if successful.
395a8e1175bSopenharmony_ci * \return         #MBEDTLS_ERR_MPI_NOT_ACCEPTABLE if the implementation was
396a8e1175bSopenharmony_ci *                 unable to find a suitable value within a limited number
397a8e1175bSopenharmony_ci *                 of attempts. This has a negligible probability if \p N
398a8e1175bSopenharmony_ci *                 is significantly larger than \p min, which is the case
399a8e1175bSopenharmony_ci *                 for all usual cryptographic applications.
400a8e1175bSopenharmony_ci */
401a8e1175bSopenharmony_ciint mbedtls_mpi_mod_raw_random(mbedtls_mpi_uint *X,
402a8e1175bSopenharmony_ci                               mbedtls_mpi_uint min,
403a8e1175bSopenharmony_ci                               const mbedtls_mpi_mod_modulus *N,
404a8e1175bSopenharmony_ci                               int (*f_rng)(void *, unsigned char *, size_t),
405a8e1175bSopenharmony_ci                               void *p_rng);
406a8e1175bSopenharmony_ci
407a8e1175bSopenharmony_ci/* END MERGE SLOT 6 */
408a8e1175bSopenharmony_ci
409a8e1175bSopenharmony_ci/* BEGIN MERGE SLOT 7 */
410a8e1175bSopenharmony_ci/** Convert an MPI into Montgomery form.
411a8e1175bSopenharmony_ci *
412a8e1175bSopenharmony_ci * \param X      The address of the MPI.
413a8e1175bSopenharmony_ci *               Must have the same number of limbs as \p N.
414a8e1175bSopenharmony_ci * \param N      The address of the modulus, which gives the size of
415a8e1175bSopenharmony_ci *               the base `R` = 2^(biL*N->limbs).
416a8e1175bSopenharmony_ci *
417a8e1175bSopenharmony_ci * \return       \c 0 if successful.
418a8e1175bSopenharmony_ci */
419a8e1175bSopenharmony_ciint mbedtls_mpi_mod_raw_to_mont_rep(mbedtls_mpi_uint *X,
420a8e1175bSopenharmony_ci                                    const mbedtls_mpi_mod_modulus *N);
421a8e1175bSopenharmony_ci
422a8e1175bSopenharmony_ci/** Convert an MPI back from Montgomery representation.
423a8e1175bSopenharmony_ci *
424a8e1175bSopenharmony_ci * \param X      The address of the MPI.
425a8e1175bSopenharmony_ci *               Must have the same number of limbs as \p N.
426a8e1175bSopenharmony_ci * \param N      The address of the modulus, which gives the size of
427a8e1175bSopenharmony_ci *               the base `R`= 2^(biL*N->limbs).
428a8e1175bSopenharmony_ci *
429a8e1175bSopenharmony_ci * \return       \c 0 if successful.
430a8e1175bSopenharmony_ci */
431a8e1175bSopenharmony_ciint mbedtls_mpi_mod_raw_from_mont_rep(mbedtls_mpi_uint *X,
432a8e1175bSopenharmony_ci                                      const mbedtls_mpi_mod_modulus *N);
433a8e1175bSopenharmony_ci
434a8e1175bSopenharmony_ci/** \brief  Perform fixed width modular negation.
435a8e1175bSopenharmony_ci *
436a8e1175bSopenharmony_ci * The size of the operation is determined by \p N. \p A must have
437a8e1175bSopenharmony_ci * the same number of limbs as \p N.
438a8e1175bSopenharmony_ci *
439a8e1175bSopenharmony_ci * \p X may be aliased to \p A.
440a8e1175bSopenharmony_ci *
441a8e1175bSopenharmony_ci * \param[out] X        The result of the modular negation.
442a8e1175bSopenharmony_ci *                      This must be initialized.
443a8e1175bSopenharmony_ci * \param[in] A         Little-endian presentation of the input operand. This
444a8e1175bSopenharmony_ci *                      must be less than or equal to \p N.
445a8e1175bSopenharmony_ci * \param[in] N         The modulus to use.
446a8e1175bSopenharmony_ci */
447a8e1175bSopenharmony_civoid mbedtls_mpi_mod_raw_neg(mbedtls_mpi_uint *X,
448a8e1175bSopenharmony_ci                             const mbedtls_mpi_uint *A,
449a8e1175bSopenharmony_ci                             const mbedtls_mpi_mod_modulus *N);
450a8e1175bSopenharmony_ci/* END MERGE SLOT 7 */
451a8e1175bSopenharmony_ci
452a8e1175bSopenharmony_ci/* BEGIN MERGE SLOT 8 */
453a8e1175bSopenharmony_ci
454a8e1175bSopenharmony_ci/* END MERGE SLOT 8 */
455a8e1175bSopenharmony_ci
456a8e1175bSopenharmony_ci/* BEGIN MERGE SLOT 9 */
457a8e1175bSopenharmony_ci
458a8e1175bSopenharmony_ci/* END MERGE SLOT 9 */
459a8e1175bSopenharmony_ci
460a8e1175bSopenharmony_ci/* BEGIN MERGE SLOT 10 */
461a8e1175bSopenharmony_ci
462a8e1175bSopenharmony_ci/* END MERGE SLOT 10 */
463a8e1175bSopenharmony_ci
464a8e1175bSopenharmony_ci#endif /* MBEDTLS_BIGNUM_MOD_RAW_H */
465