xref: /optee_os/lib/libmbedtls/mbedtls/library/bignum_mod.h (revision 32b3180828fa15a49ccc86ecb4be9d274c140c89)
1*32b31808SJens Wiklander /**
2*32b31808SJens Wiklander  *  Modular bignum functions
3*32b31808SJens Wiklander  *
4*32b31808SJens Wiklander  * This module implements operations on integers modulo some fixed modulus.
5*32b31808SJens Wiklander  *
6*32b31808SJens Wiklander  * The functions in this module obey the following conventions unless
7*32b31808SJens Wiklander  * explicitly indicated otherwise:
8*32b31808SJens Wiklander  *
9*32b31808SJens Wiklander  * - **Modulus parameters**: the modulus is passed as a pointer to a structure
10*32b31808SJens Wiklander  *   of type #mbedtls_mpi_mod_modulus. The structure must be set up with an
11*32b31808SJens Wiklander  *   array of limbs storing the bignum value of the modulus. The modulus must
12*32b31808SJens Wiklander  *   be odd and is assumed to have no leading zeroes. The modulus is usually
13*32b31808SJens Wiklander  *   named \c N and is usually input-only. Functions which take a parameter
14*32b31808SJens Wiklander  *   of type \c const #mbedtls_mpi_mod_modulus* must not modify its value.
15*32b31808SJens Wiklander  * - **Bignum parameters**: Bignums are passed as pointers to an array of
16*32b31808SJens Wiklander  *   limbs or to a #mbedtls_mpi_mod_residue structure. A limb has the type
17*32b31808SJens Wiklander  *   #mbedtls_mpi_uint. Residues must be initialized before use, and must be
18*32b31808SJens Wiklander  *   associated with the modulus \c N. Unless otherwise specified:
19*32b31808SJens Wiklander  *     - Bignum parameters called \c A, \c B, ... are inputs and are not
20*32b31808SJens Wiklander  *       modified by the function. Functions which take a parameter of
21*32b31808SJens Wiklander  *       type \c const #mbedtls_mpi_mod_residue* must not modify its value.
22*32b31808SJens Wiklander  *     - Bignum parameters called \c X, \c Y, ... are outputs or input-output.
23*32b31808SJens Wiklander  *       The initial bignum value of output-only parameters is ignored, but
24*32b31808SJens Wiklander  *       they must be set up and associated with the modulus \c N. Some
25*32b31808SJens Wiklander  *       functions (typically constant-flow) require that the limbs in an
26*32b31808SJens Wiklander  *       output residue are initialized.
27*32b31808SJens Wiklander  *     - Bignum parameters called \c p are inputs used to set up a modulus or
28*32b31808SJens Wiklander  *       residue. These must be pointers to an array of limbs.
29*32b31808SJens Wiklander  *     - \c T is a temporary storage area. The initial content of such a
30*32b31808SJens Wiklander  *       parameter is ignored and the final content is unspecified.
31*32b31808SJens Wiklander  *     - Some functions use different names, such as \c r for the residue.
32*32b31808SJens Wiklander  * - **Bignum sizes**: bignum sizes are always expressed in limbs. Both
33*32b31808SJens Wiklander  *   #mbedtls_mpi_mod_modulus and #mbedtls_mpi_mod_residue have a \c limbs
34*32b31808SJens Wiklander  *   member storing its size. All bignum parameters must have the same
35*32b31808SJens Wiklander  *   number of limbs as the modulus. All bignum sizes must be at least 1 and
36*32b31808SJens Wiklander  *   must be significantly less than #SIZE_MAX. The behavior if a size is 0 is
37*32b31808SJens Wiklander  *   undefined.
38*32b31808SJens Wiklander  * - **Bignum representation**: the representation of inputs and outputs is
39*32b31808SJens Wiklander  *   specified by the \c int_rep field of the modulus.
40*32b31808SJens Wiklander  * - **Parameter ordering**: for bignum parameters, outputs come before inputs.
41*32b31808SJens Wiklander  *   The modulus is passed after residues. Temporaries come last.
42*32b31808SJens Wiklander  * - **Aliasing**: in general, output bignums may be aliased to one or more
43*32b31808SJens Wiklander  *   inputs. Modulus values may not be aliased to any other parameter. Outputs
44*32b31808SJens Wiklander  *   may not be aliased to one another. Temporaries may not be aliased to any
45*32b31808SJens Wiklander  *   other parameter.
46*32b31808SJens Wiklander  * - **Overlap**: apart from aliasing of residue pointers (where two residue
47*32b31808SJens Wiklander  *   arguments are equal pointers), overlap is not supported and may result
48*32b31808SJens Wiklander  *   in undefined behavior.
49*32b31808SJens Wiklander  * - **Error handling**: functions generally check compatibility of input
50*32b31808SJens Wiklander  *   sizes. Most functions will not check that input values are in canonical
51*32b31808SJens Wiklander  *   form (i.e. that \c A < \c N), this is only checked during setup of a
52*32b31808SJens Wiklander  *   residue structure.
53*32b31808SJens Wiklander  * - **Modular representatives**: all functions expect inputs to be in the
54*32b31808SJens Wiklander  *   range [0, \c N - 1] and guarantee outputs in the range [0, \c N - 1].
55*32b31808SJens Wiklander  *   Residues are set up with an associated modulus, and operations are only
56*32b31808SJens Wiklander  *   guaranteed to work if the modulus is associated with all residue
57*32b31808SJens Wiklander  *   parameters. If a residue is passed with a modulus other than the one it
58*32b31808SJens Wiklander  *   is associated with, then it may be out of range. If an input is out of
59*32b31808SJens Wiklander  *   range, outputs are fully unspecified, though bignum values out of range
60*32b31808SJens Wiklander  *   should not cause buffer overflows (beware that this is not extensively
61*32b31808SJens Wiklander  *   tested).
62*32b31808SJens Wiklander  */
63*32b31808SJens Wiklander 
64*32b31808SJens Wiklander /*
65*32b31808SJens Wiklander  *  Copyright The Mbed TLS Contributors
66*32b31808SJens Wiklander  *  SPDX-License-Identifier: Apache-2.0
67*32b31808SJens Wiklander  *
68*32b31808SJens Wiklander  *  Licensed under the Apache License, Version 2.0 (the "License"); you may
69*32b31808SJens Wiklander  *  not use this file except in compliance with the License.
70*32b31808SJens Wiklander  *  You may obtain a copy of the License at
71*32b31808SJens Wiklander  *
72*32b31808SJens Wiklander  *  http://www.apache.org/licenses/LICENSE-2.0
73*32b31808SJens Wiklander  *
74*32b31808SJens Wiklander  *  Unless required by applicable law or agreed to in writing, software
75*32b31808SJens Wiklander  *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
76*32b31808SJens Wiklander  *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
77*32b31808SJens Wiklander  *  See the License for the specific language governing permissions and
78*32b31808SJens Wiklander  *  limitations under the License.
79*32b31808SJens Wiklander  */
80*32b31808SJens Wiklander 
81*32b31808SJens Wiklander #ifndef MBEDTLS_BIGNUM_MOD_H
82*32b31808SJens Wiklander #define MBEDTLS_BIGNUM_MOD_H
83*32b31808SJens Wiklander 
84*32b31808SJens Wiklander #include "common.h"
85*32b31808SJens Wiklander 
86*32b31808SJens Wiklander #if defined(MBEDTLS_BIGNUM_C)
87*32b31808SJens Wiklander #include "mbedtls/bignum.h"
88*32b31808SJens Wiklander #endif
89*32b31808SJens Wiklander 
90*32b31808SJens Wiklander /** How residues associated with a modulus are represented.
91*32b31808SJens Wiklander  *
92*32b31808SJens Wiklander  * This also determines which fields of the modulus structure are valid and
93*32b31808SJens Wiklander  * what their contents are (see #mbedtls_mpi_mod_modulus).
94*32b31808SJens Wiklander  */
95*32b31808SJens Wiklander typedef enum {
96*32b31808SJens Wiklander     /** Representation not chosen (makes the modulus structure invalid). */
97*32b31808SJens Wiklander     MBEDTLS_MPI_MOD_REP_INVALID    = 0,
98*32b31808SJens Wiklander     /* Skip 1 as it is slightly easier to accidentally pass to functions. */
99*32b31808SJens Wiklander     /** Montgomery representation. */
100*32b31808SJens Wiklander     MBEDTLS_MPI_MOD_REP_MONTGOMERY = 2,
101*32b31808SJens Wiklander     /** TODO: document this.
102*32b31808SJens Wiklander      *
103*32b31808SJens Wiklander      * Residues are in canonical representation.
104*32b31808SJens Wiklander      */
105*32b31808SJens Wiklander     MBEDTLS_MPI_MOD_REP_OPT_RED,
106*32b31808SJens Wiklander } mbedtls_mpi_mod_rep_selector;
107*32b31808SJens Wiklander 
108*32b31808SJens Wiklander /* Make mbedtls_mpi_mod_rep_selector and mbedtls_mpi_mod_ext_rep disjoint to
109*32b31808SJens Wiklander  * make it easier to catch when they are accidentally swapped. */
110*32b31808SJens Wiklander typedef enum {
111*32b31808SJens Wiklander     MBEDTLS_MPI_MOD_EXT_REP_INVALID = 0,
112*32b31808SJens Wiklander     MBEDTLS_MPI_MOD_EXT_REP_LE      = 8,
113*32b31808SJens Wiklander     MBEDTLS_MPI_MOD_EXT_REP_BE
114*32b31808SJens Wiklander } mbedtls_mpi_mod_ext_rep;
115*32b31808SJens Wiklander 
116*32b31808SJens Wiklander typedef struct {
117*32b31808SJens Wiklander     mbedtls_mpi_uint *p;
118*32b31808SJens Wiklander     size_t limbs;
119*32b31808SJens Wiklander } mbedtls_mpi_mod_residue;
120*32b31808SJens Wiklander 
121*32b31808SJens Wiklander typedef struct {
122*32b31808SJens Wiklander     mbedtls_mpi_uint const *rr;  /* The residue for 2^{2*n*biL} mod N */
123*32b31808SJens Wiklander     mbedtls_mpi_uint mm;         /* Montgomery const for -N^{-1} mod 2^{ciL} */
124*32b31808SJens Wiklander } mbedtls_mpi_mont_struct;
125*32b31808SJens Wiklander 
126*32b31808SJens Wiklander typedef void *mbedtls_mpi_opt_red_struct;
127*32b31808SJens Wiklander 
128*32b31808SJens Wiklander typedef struct {
129*32b31808SJens Wiklander     const mbedtls_mpi_uint *p;
130*32b31808SJens Wiklander     size_t limbs;                            // number of limbs
131*32b31808SJens Wiklander     size_t bits;                             // bitlen of p
132*32b31808SJens Wiklander     mbedtls_mpi_mod_rep_selector int_rep;    // selector to signal the active member of the union
133*32b31808SJens Wiklander     union rep {
134*32b31808SJens Wiklander         /* if int_rep == #MBEDTLS_MPI_MOD_REP_MONTGOMERY */
135*32b31808SJens Wiklander         mbedtls_mpi_mont_struct mont;
136*32b31808SJens Wiklander         /* if int_rep == #MBEDTLS_MPI_MOD_REP_OPT_RED */
137*32b31808SJens Wiklander         mbedtls_mpi_opt_red_struct ored;
138*32b31808SJens Wiklander     } rep;
139*32b31808SJens Wiklander } mbedtls_mpi_mod_modulus;
140*32b31808SJens Wiklander 
141*32b31808SJens Wiklander /** Setup a residue structure.
142*32b31808SJens Wiklander  *
143*32b31808SJens Wiklander  * The residue will be set up with the buffer \p p and modulus \p N.
144*32b31808SJens Wiklander  *
145*32b31808SJens Wiklander  * The memory pointed to by \p p will be used by the resulting residue structure.
146*32b31808SJens Wiklander  * The value at the pointed-to memory will be the initial value of \p r and must
147*32b31808SJens Wiklander  * hold a value that is less than the modulus. This value will be used as-is
148*32b31808SJens Wiklander  * and interpreted according to the value of the `N->int_rep` field.
149*32b31808SJens Wiklander  *
150*32b31808SJens Wiklander  * The modulus \p N will be the modulus associated with \p r. The residue \p r
151*32b31808SJens Wiklander  * should only be used in operations where the modulus is \p N.
152*32b31808SJens Wiklander  *
153*32b31808SJens Wiklander  * \param[out] r    The address of the residue to setup.
154*32b31808SJens Wiklander  * \param[in] N     The address of the modulus related to \p r.
155*32b31808SJens Wiklander  * \param[in] p     The address of the limb array containing the value of \p r.
156*32b31808SJens Wiklander  *                  The memory pointed to by \p p will be used by \p r and must
157*32b31808SJens Wiklander  *                  not be modified in any way until after
158*32b31808SJens Wiklander  *                  mbedtls_mpi_mod_residue_release() is called. The data
159*32b31808SJens Wiklander  *                  pointed to by \p p must be less than the modulus (the value
160*32b31808SJens Wiklander  *                  pointed to by `N->p`) and already in the representation
161*32b31808SJens Wiklander  *                  indicated by `N->int_rep`.
162*32b31808SJens Wiklander  * \param p_limbs   The number of limbs of \p p. Must be the same as the number
163*32b31808SJens Wiklander  *                  of limbs in the modulus \p N.
164*32b31808SJens Wiklander  *
165*32b31808SJens Wiklander  * \return      \c 0 if successful.
166*32b31808SJens Wiklander  * \return      #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if \p p_limbs is less than the
167*32b31808SJens Wiklander  *              limbs in \p N or if \p p is not less than \p N.
168*32b31808SJens Wiklander  */
169*32b31808SJens Wiklander int mbedtls_mpi_mod_residue_setup(mbedtls_mpi_mod_residue *r,
170*32b31808SJens Wiklander                                   const mbedtls_mpi_mod_modulus *N,
171*32b31808SJens Wiklander                                   mbedtls_mpi_uint *p,
172*32b31808SJens Wiklander                                   size_t p_limbs);
173*32b31808SJens Wiklander 
174*32b31808SJens Wiklander /** Unbind elements of a residue structure.
175*32b31808SJens Wiklander  *
176*32b31808SJens Wiklander  * This function removes the reference to the limb array that was passed to
177*32b31808SJens Wiklander  * mbedtls_mpi_mod_residue_setup() to make it safe to free or use again.
178*32b31808SJens Wiklander  *
179*32b31808SJens Wiklander  * This function invalidates \p r and it must not be used until after
180*32b31808SJens Wiklander  * mbedtls_mpi_mod_residue_setup() is called on it again.
181*32b31808SJens Wiklander  *
182*32b31808SJens Wiklander  * \param[out] r     The address of residue to release.
183*32b31808SJens Wiklander  */
184*32b31808SJens Wiklander void mbedtls_mpi_mod_residue_release(mbedtls_mpi_mod_residue *r);
185*32b31808SJens Wiklander 
186*32b31808SJens Wiklander /** Initialize a modulus structure.
187*32b31808SJens Wiklander  *
188*32b31808SJens Wiklander  * \param[out] N     The address of the modulus structure to initialize.
189*32b31808SJens Wiklander  */
190*32b31808SJens Wiklander void mbedtls_mpi_mod_modulus_init(mbedtls_mpi_mod_modulus *N);
191*32b31808SJens Wiklander 
192*32b31808SJens Wiklander /** Setup a modulus structure.
193*32b31808SJens Wiklander  *
194*32b31808SJens Wiklander  * \param[out] N    The address of the modulus structure to populate.
195*32b31808SJens Wiklander  * \param[in] p     The address of the limb array storing the value of \p N.
196*32b31808SJens Wiklander  *                  The memory pointed to by \p p will be used by \p N and must
197*32b31808SJens Wiklander  *                  not be modified in any way until after
198*32b31808SJens Wiklander  *                  mbedtls_mpi_mod_modulus_free() is called.
199*32b31808SJens Wiklander  * \param p_limbs   The number of limbs of \p p.
200*32b31808SJens Wiklander  * \param int_rep   The internal representation to be used for residues
201*32b31808SJens Wiklander  *                  associated with \p N (see #mbedtls_mpi_mod_rep_selector).
202*32b31808SJens Wiklander  *
203*32b31808SJens Wiklander  * \return      \c 0 if successful.
204*32b31808SJens Wiklander  * \return      #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if \p int_rep is invalid.
205*32b31808SJens Wiklander  */
206*32b31808SJens Wiklander int mbedtls_mpi_mod_modulus_setup(mbedtls_mpi_mod_modulus *N,
207*32b31808SJens Wiklander                                   const mbedtls_mpi_uint *p,
208*32b31808SJens Wiklander                                   size_t p_limbs,
209*32b31808SJens Wiklander                                   mbedtls_mpi_mod_rep_selector int_rep);
210*32b31808SJens Wiklander 
211*32b31808SJens Wiklander /** Free elements of a modulus structure.
212*32b31808SJens Wiklander  *
213*32b31808SJens Wiklander  * This function frees any memory allocated by mbedtls_mpi_mod_modulus_setup().
214*32b31808SJens Wiklander  *
215*32b31808SJens Wiklander  * \warning This function does not free the limb array passed to
216*32b31808SJens Wiklander  *          mbedtls_mpi_mod_modulus_setup() only removes the reference to it,
217*32b31808SJens Wiklander  *          making it safe to free or to use it again.
218*32b31808SJens Wiklander  *
219*32b31808SJens Wiklander  * \param[in,out] N     The address of the modulus structure to free.
220*32b31808SJens Wiklander  */
221*32b31808SJens Wiklander void mbedtls_mpi_mod_modulus_free(mbedtls_mpi_mod_modulus *N);
222*32b31808SJens Wiklander 
223*32b31808SJens Wiklander /* BEGIN MERGE SLOT 1 */
224*32b31808SJens Wiklander 
225*32b31808SJens Wiklander /* END MERGE SLOT 1 */
226*32b31808SJens Wiklander 
227*32b31808SJens Wiklander /* BEGIN MERGE SLOT 2 */
228*32b31808SJens Wiklander 
229*32b31808SJens Wiklander /** \brief  Multiply two residues, returning the residue modulo the specified
230*32b31808SJens Wiklander  *          modulus.
231*32b31808SJens Wiklander  *
232*32b31808SJens Wiklander  * \note Currently handles the case when `N->int_rep` is
233*32b31808SJens Wiklander  * MBEDTLS_MPI_MOD_REP_MONTGOMERY.
234*32b31808SJens Wiklander  *
235*32b31808SJens Wiklander  * The size of the operation is determined by \p N. \p A, \p B and \p X must
236*32b31808SJens Wiklander  * all be associated with the modulus \p N and must all have the same number
237*32b31808SJens Wiklander  * of limbs as \p N.
238*32b31808SJens Wiklander  *
239*32b31808SJens Wiklander  * \p X may be aliased to \p A or \p B, or even both, but may not overlap
240*32b31808SJens Wiklander  * either otherwise. They may not alias \p N (since they must be in canonical
241*32b31808SJens Wiklander  * form, they cannot == \p N).
242*32b31808SJens Wiklander  *
243*32b31808SJens Wiklander  * \param[out] X        The address of the result MPI. Must have the same
244*32b31808SJens Wiklander  *                      number of limbs as \p N.
245*32b31808SJens Wiklander  *                      On successful completion, \p X contains the result of
246*32b31808SJens Wiklander  *                      the multiplication `A * B * R^-1` mod N where
247*32b31808SJens Wiklander  *                      `R = 2^(biL * N->limbs)`.
248*32b31808SJens Wiklander  * \param[in]  A        The address of the first MPI.
249*32b31808SJens Wiklander  * \param[in]  B        The address of the second MPI.
250*32b31808SJens Wiklander  * \param[in]  N        The address of the modulus. Used to perform a modulo
251*32b31808SJens Wiklander  *                      operation on the result of the multiplication.
252*32b31808SJens Wiklander  *
253*32b31808SJens Wiklander  * \return      \c 0 if successful.
254*32b31808SJens Wiklander  * \return      #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if all the parameters do not
255*32b31808SJens Wiklander  *              have the same number of limbs or \p N is invalid.
256*32b31808SJens Wiklander  * \return      #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory-allocation failure.
257*32b31808SJens Wiklander  */
258*32b31808SJens Wiklander int mbedtls_mpi_mod_mul(mbedtls_mpi_mod_residue *X,
259*32b31808SJens Wiklander                         const mbedtls_mpi_mod_residue *A,
260*32b31808SJens Wiklander                         const mbedtls_mpi_mod_residue *B,
261*32b31808SJens Wiklander                         const mbedtls_mpi_mod_modulus *N);
262*32b31808SJens Wiklander 
263*32b31808SJens Wiklander /* END MERGE SLOT 2 */
264*32b31808SJens Wiklander 
265*32b31808SJens Wiklander /* BEGIN MERGE SLOT 3 */
266*32b31808SJens Wiklander /**
267*32b31808SJens Wiklander  * \brief Perform a fixed-size modular subtraction.
268*32b31808SJens Wiklander  *
269*32b31808SJens Wiklander  * Calculate `A - B modulo N`.
270*32b31808SJens Wiklander  *
271*32b31808SJens Wiklander  * \p A, \p B and \p X must all have the same number of limbs as \p N.
272*32b31808SJens Wiklander  *
273*32b31808SJens Wiklander  * \p X may be aliased to \p A or \p B, or even both, but may not overlap
274*32b31808SJens Wiklander  * either otherwise.
275*32b31808SJens Wiklander  *
276*32b31808SJens Wiklander  * \note This function does not check that \p A or \p B are in canonical
277*32b31808SJens Wiklander  *       form (that is, are < \p N) - that will have been done by
278*32b31808SJens Wiklander  *       mbedtls_mpi_mod_residue_setup().
279*32b31808SJens Wiklander  *
280*32b31808SJens Wiklander  * \param[out] X    The address of the result MPI. Must be initialized.
281*32b31808SJens Wiklander  *                  Must have the same number of limbs as the modulus \p N.
282*32b31808SJens Wiklander  * \param[in]  A    The address of the first MPI.
283*32b31808SJens Wiklander  * \param[in]  B    The address of the second MPI.
284*32b31808SJens Wiklander  * \param[in]  N    The address of the modulus. Used to perform a modulo
285*32b31808SJens Wiklander  *                  operation on the result of the subtraction.
286*32b31808SJens Wiklander  *
287*32b31808SJens Wiklander  * \return          \c 0 if successful.
288*32b31808SJens Wiklander  * \return          #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if the given MPIs do not
289*32b31808SJens Wiklander  *                  have the correct number of limbs.
290*32b31808SJens Wiklander  */
291*32b31808SJens Wiklander int mbedtls_mpi_mod_sub(mbedtls_mpi_mod_residue *X,
292*32b31808SJens Wiklander                         const mbedtls_mpi_mod_residue *A,
293*32b31808SJens Wiklander                         const mbedtls_mpi_mod_residue *B,
294*32b31808SJens Wiklander                         const mbedtls_mpi_mod_modulus *N);
295*32b31808SJens Wiklander 
296*32b31808SJens Wiklander /**
297*32b31808SJens Wiklander  * \brief Perform modular inversion of an MPI with respect to a modulus \p N.
298*32b31808SJens Wiklander  *
299*32b31808SJens Wiklander  * \p A and \p X must be associated with the modulus \p N and will therefore
300*32b31808SJens Wiklander  * have the same number of limbs as \p N.
301*32b31808SJens Wiklander  *
302*32b31808SJens Wiklander  * \p X may be aliased to \p A.
303*32b31808SJens Wiklander  *
304*32b31808SJens Wiklander  * \warning  Currently only supports prime moduli, but does not check for them.
305*32b31808SJens Wiklander  *
306*32b31808SJens Wiklander  * \param[out] X   The modular inverse of \p A with respect to \p N.
307*32b31808SJens Wiklander  * \param[in] A    The number to calculate the modular inverse of.
308*32b31808SJens Wiklander  *                 Must not be 0.
309*32b31808SJens Wiklander  * \param[in] N    The modulus to use.
310*32b31808SJens Wiklander  *
311*32b31808SJens Wiklander  * \return         \c 0 if successful.
312*32b31808SJens Wiklander  * \return         #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if \p A and \p N do not
313*32b31808SJens Wiklander  *                 have the same number of limbs.
314*32b31808SJens Wiklander  * \return         #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if \p A is zero.
315*32b31808SJens Wiklander  * \return         #MBEDTLS_ERR_MPI_ALLOC_FAILED if couldn't allocate enough
316*32b31808SJens Wiklander  *                 memory (needed for conversion to and from Mongtomery form
317*32b31808SJens Wiklander  *                 when not in Montgomery form already, and for temporary use
318*32b31808SJens Wiklander  *                 by the inversion calculation itself).
319*32b31808SJens Wiklander  */
320*32b31808SJens Wiklander 
321*32b31808SJens Wiklander int mbedtls_mpi_mod_inv(mbedtls_mpi_mod_residue *X,
322*32b31808SJens Wiklander                         const mbedtls_mpi_mod_residue *A,
323*32b31808SJens Wiklander                         const mbedtls_mpi_mod_modulus *N);
324*32b31808SJens Wiklander /* END MERGE SLOT 3 */
325*32b31808SJens Wiklander 
326*32b31808SJens Wiklander /* BEGIN MERGE SLOT 4 */
327*32b31808SJens Wiklander 
328*32b31808SJens Wiklander /* END MERGE SLOT 4 */
329*32b31808SJens Wiklander 
330*32b31808SJens Wiklander /* BEGIN MERGE SLOT 5 */
331*32b31808SJens Wiklander /**
332*32b31808SJens Wiklander  * \brief Perform a fixed-size modular addition.
333*32b31808SJens Wiklander  *
334*32b31808SJens Wiklander  * Calculate `A + B modulo N`.
335*32b31808SJens Wiklander  *
336*32b31808SJens Wiklander  * \p A, \p B and \p X must all be associated with the modulus \p N and must
337*32b31808SJens Wiklander  * all have the same number of limbs as \p N.
338*32b31808SJens Wiklander  *
339*32b31808SJens Wiklander  * \p X may be aliased to \p A or \p B, or even both, but may not overlap
340*32b31808SJens Wiklander  * either otherwise.
341*32b31808SJens Wiklander  *
342*32b31808SJens Wiklander  * \note This function does not check that \p A or \p B are in canonical
343*32b31808SJens Wiklander  *       form (that is, are < \p N) - that will have been done by
344*32b31808SJens Wiklander  *       mbedtls_mpi_mod_residue_setup().
345*32b31808SJens Wiklander  *
346*32b31808SJens Wiklander  * \param[out] X    The address of the result residue. Must be initialized.
347*32b31808SJens Wiklander  *                  Must have the same number of limbs as the modulus \p N.
348*32b31808SJens Wiklander  * \param[in]  A    The address of the first input residue.
349*32b31808SJens Wiklander  * \param[in]  B    The address of the second input residue.
350*32b31808SJens Wiklander  * \param[in]  N    The address of the modulus. Used to perform a modulo
351*32b31808SJens Wiklander  *                  operation on the result of the addition.
352*32b31808SJens Wiklander  *
353*32b31808SJens Wiklander  * \return          \c 0 if successful.
354*32b31808SJens Wiklander  * \return          #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if the given MPIs do not
355*32b31808SJens Wiklander  *                  have the correct number of limbs.
356*32b31808SJens Wiklander  */
357*32b31808SJens Wiklander int mbedtls_mpi_mod_add(mbedtls_mpi_mod_residue *X,
358*32b31808SJens Wiklander                         const mbedtls_mpi_mod_residue *A,
359*32b31808SJens Wiklander                         const mbedtls_mpi_mod_residue *B,
360*32b31808SJens Wiklander                         const mbedtls_mpi_mod_modulus *N);
361*32b31808SJens Wiklander /* END MERGE SLOT 5 */
362*32b31808SJens Wiklander 
363*32b31808SJens Wiklander /* BEGIN MERGE SLOT 6 */
364*32b31808SJens Wiklander 
365*32b31808SJens Wiklander /** Generate a random number uniformly in a range.
366*32b31808SJens Wiklander  *
367*32b31808SJens Wiklander  * This function generates a random number between \p min inclusive and
368*32b31808SJens Wiklander  * \p N exclusive.
369*32b31808SJens Wiklander  *
370*32b31808SJens Wiklander  * The procedure complies with RFC 6979 §3.3 (deterministic ECDSA)
371*32b31808SJens Wiklander  * when the RNG is a suitably parametrized instance of HMAC_DRBG
372*32b31808SJens Wiklander  * and \p min is \c 1.
373*32b31808SJens Wiklander  *
374*32b31808SJens Wiklander  * \note           There are `N - min` possible outputs. The lower bound
375*32b31808SJens Wiklander  *                 \p min can be reached, but the upper bound \p N cannot.
376*32b31808SJens Wiklander  *
377*32b31808SJens Wiklander  * \param X        The destination residue.
378*32b31808SJens Wiklander  * \param min      The minimum value to return. It must be strictly smaller
379*32b31808SJens Wiklander  *                 than \b N.
380*32b31808SJens Wiklander  * \param N        The modulus.
381*32b31808SJens Wiklander  *                 This is the upper bound of the output range, exclusive.
382*32b31808SJens Wiklander  * \param f_rng    The RNG function to use. This must not be \c NULL.
383*32b31808SJens Wiklander  * \param p_rng    The RNG parameter to be passed to \p f_rng.
384*32b31808SJens Wiklander  *
385*32b31808SJens Wiklander  * \return         \c 0 if successful.
386*32b31808SJens Wiklander  * \return         #MBEDTLS_ERR_MPI_NOT_ACCEPTABLE if the implementation was
387*32b31808SJens Wiklander  *                 unable to find a suitable value within a limited number
388*32b31808SJens Wiklander  *                 of attempts. This has a negligible probability if \p N
389*32b31808SJens Wiklander  *                 is significantly larger than \p min, which is the case
390*32b31808SJens Wiklander  *                 for all usual cryptographic applications.
391*32b31808SJens Wiklander  */
392*32b31808SJens Wiklander int mbedtls_mpi_mod_random(mbedtls_mpi_mod_residue *X,
393*32b31808SJens Wiklander                            mbedtls_mpi_uint min,
394*32b31808SJens Wiklander                            const mbedtls_mpi_mod_modulus *N,
395*32b31808SJens Wiklander                            int (*f_rng)(void *, unsigned char *, size_t),
396*32b31808SJens Wiklander                            void *p_rng);
397*32b31808SJens Wiklander 
398*32b31808SJens Wiklander /* END MERGE SLOT 6 */
399*32b31808SJens Wiklander 
400*32b31808SJens Wiklander /* BEGIN MERGE SLOT 7 */
401*32b31808SJens Wiklander /** Read a residue from a byte buffer.
402*32b31808SJens Wiklander  *
403*32b31808SJens Wiklander  * The residue will be automatically converted to the internal representation
404*32b31808SJens Wiklander  * based on the value of the `N->int_rep` field.
405*32b31808SJens Wiklander  *
406*32b31808SJens Wiklander  * The modulus \p N will be the modulus associated with \p r. The residue \p r
407*32b31808SJens Wiklander  * should only be used in operations where the modulus is \p N or a modulus
408*32b31808SJens Wiklander  * equivalent to \p N (in the sense that all their fields or memory pointed by
409*32b31808SJens Wiklander  * their fields hold the same value).
410*32b31808SJens Wiklander  *
411*32b31808SJens Wiklander  * \param[out] r    The address of the residue. It must have exactly the same
412*32b31808SJens Wiklander  *                  number of limbs as the modulus \p N.
413*32b31808SJens Wiklander  * \param[in] N     The address of the modulus.
414*32b31808SJens Wiklander  * \param[in] buf   The input buffer to import from.
415*32b31808SJens Wiklander  * \param buflen    The length in bytes of \p buf.
416*32b31808SJens Wiklander  * \param ext_rep   The endianness of the number in the input buffer.
417*32b31808SJens Wiklander  *
418*32b31808SJens Wiklander  * \return       \c 0 if successful.
419*32b31808SJens Wiklander  * \return       #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p r isn't
420*32b31808SJens Wiklander  *               large enough to hold the value in \p buf.
421*32b31808SJens Wiklander  * \return       #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if \p ext_rep
422*32b31808SJens Wiklander  *               is invalid or the value in the buffer is not less than \p N.
423*32b31808SJens Wiklander  */
424*32b31808SJens Wiklander int mbedtls_mpi_mod_read(mbedtls_mpi_mod_residue *r,
425*32b31808SJens Wiklander                          const mbedtls_mpi_mod_modulus *N,
426*32b31808SJens Wiklander                          const unsigned char *buf,
427*32b31808SJens Wiklander                          size_t buflen,
428*32b31808SJens Wiklander                          mbedtls_mpi_mod_ext_rep ext_rep);
429*32b31808SJens Wiklander 
430*32b31808SJens Wiklander /** Write a residue into a byte buffer.
431*32b31808SJens Wiklander  *
432*32b31808SJens Wiklander  * The modulus \p N must be the modulus associated with \p r (see
433*32b31808SJens Wiklander  * mbedtls_mpi_mod_residue_setup() and mbedtls_mpi_mod_read()).
434*32b31808SJens Wiklander  *
435*32b31808SJens Wiklander  * The residue will be automatically converted from the internal representation
436*32b31808SJens Wiklander  * based on the value of `N->int_rep` field.
437*32b31808SJens Wiklander  *
438*32b31808SJens Wiklander  * \warning     If the buffer is smaller than `N->bits`, the number of
439*32b31808SJens Wiklander  *              leading zeroes is leaked through timing. If \p r is
440*32b31808SJens Wiklander  *              secret, the caller must ensure that \p buflen is at least
441*32b31808SJens Wiklander  *              (`N->bits`+7)/8.
442*32b31808SJens Wiklander  *
443*32b31808SJens Wiklander  * \param[in] r     The address of the residue. It must have the same number of
444*32b31808SJens Wiklander  *                  limbs as the modulus \p N. (\p r is an input parameter, but
445*32b31808SJens Wiklander  *                  its value will be modified during execution and restored
446*32b31808SJens Wiklander  *                  before the function returns.)
447*32b31808SJens Wiklander  * \param[in] N     The address of the modulus associated with \p r.
448*32b31808SJens Wiklander  * \param[out] buf  The output buffer to export to.
449*32b31808SJens Wiklander  * \param buflen    The length in bytes of \p buf.
450*32b31808SJens Wiklander  * \param ext_rep   The endianness in which the number should be written into
451*32b31808SJens Wiklander  *                  the output buffer.
452*32b31808SJens Wiklander  *
453*32b31808SJens Wiklander  * \return       \c 0 if successful.
454*32b31808SJens Wiklander  * \return       #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p buf isn't
455*32b31808SJens Wiklander  *               large enough to hold the value of \p r (without leading
456*32b31808SJens Wiklander  *               zeroes).
457*32b31808SJens Wiklander  * \return       #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if \p ext_rep is invalid.
458*32b31808SJens Wiklander  * \return       #MBEDTLS_ERR_MPI_ALLOC_FAILED if couldn't allocate enough
459*32b31808SJens Wiklander  *               memory for conversion. Can occur only for moduli with
460*32b31808SJens Wiklander  *               MBEDTLS_MPI_MOD_REP_MONTGOMERY.
461*32b31808SJens Wiklander  */
462*32b31808SJens Wiklander int mbedtls_mpi_mod_write(const mbedtls_mpi_mod_residue *r,
463*32b31808SJens Wiklander                           const mbedtls_mpi_mod_modulus *N,
464*32b31808SJens Wiklander                           unsigned char *buf,
465*32b31808SJens Wiklander                           size_t buflen,
466*32b31808SJens Wiklander                           mbedtls_mpi_mod_ext_rep ext_rep);
467*32b31808SJens Wiklander /* END MERGE SLOT 7 */
468*32b31808SJens Wiklander 
469*32b31808SJens Wiklander /* BEGIN MERGE SLOT 8 */
470*32b31808SJens Wiklander 
471*32b31808SJens Wiklander /* END MERGE SLOT 8 */
472*32b31808SJens Wiklander 
473*32b31808SJens Wiklander /* BEGIN MERGE SLOT 9 */
474*32b31808SJens Wiklander 
475*32b31808SJens Wiklander /* END MERGE SLOT 9 */
476*32b31808SJens Wiklander 
477*32b31808SJens Wiklander /* BEGIN MERGE SLOT 10 */
478*32b31808SJens Wiklander 
479*32b31808SJens Wiklander /* END MERGE SLOT 10 */
480*32b31808SJens Wiklander 
481*32b31808SJens Wiklander #endif /* MBEDTLS_BIGNUM_MOD_H */
482