xref: /optee_os/lib/libmbedtls/mbedtls/library/bignum_mod_raw.h (revision b0563631928755fe864b97785160fb3088e9efdc)
132b31808SJens Wiklander /**
232b31808SJens Wiklander  *  Low-level modular bignum functions
332b31808SJens Wiklander  *
432b31808SJens Wiklander  *  This interface should only be used by the higher-level modular bignum
532b31808SJens Wiklander  *  module (bignum_mod.c) and the ECP module (ecp.c, ecp_curves.c). All other
632b31808SJens Wiklander  *  modules should use the high-level modular bignum interface (bignum_mod.h)
732b31808SJens Wiklander  *  or the legacy bignum interface (bignum.h).
832b31808SJens Wiklander  *
932b31808SJens Wiklander  * This is a low-level interface to operations on integers modulo which
1032b31808SJens Wiklander  * has no protection against passing invalid arguments such as arrays of
1132b31808SJens Wiklander  * the wrong size. The functions in bignum_mod.h provide a higher-level
1232b31808SJens Wiklander  * interface that includes protections against accidental misuse, at the
1332b31808SJens Wiklander  * expense of code size and sometimes more cumbersome memory management.
1432b31808SJens Wiklander  *
1532b31808SJens Wiklander  * The functions in this module obey the following conventions unless
1632b31808SJens Wiklander  * explicitly indicated otherwise:
1732b31808SJens Wiklander  * - **Modulus parameters**: the modulus is passed as a pointer to a structure
1832b31808SJens Wiklander  *   of type #mbedtls_mpi_mod_modulus. The structure must be set up with an
1932b31808SJens Wiklander  *   array of limbs storing the bignum value of the modulus. The modulus must
2032b31808SJens Wiklander  *   be odd and is assumed to have no leading zeroes. The modulus is usually
2132b31808SJens Wiklander  *   named \c N and is usually input-only.
2232b31808SJens Wiklander  * - **Bignum parameters**: Bignums are passed as pointers to an array of
2332b31808SJens Wiklander  *   limbs. A limb has the type #mbedtls_mpi_uint. Unless otherwise specified:
2432b31808SJens Wiklander  *     - Bignum parameters called \c A, \c B, ... are inputs, and are not
2532b31808SJens Wiklander  *       modified by the function.
2632b31808SJens Wiklander  *     - Bignum parameters called \c X, \c Y are outputs or input-output.
2732b31808SJens Wiklander  *       The initial content of output-only parameters is ignored.
2832b31808SJens Wiklander  *     - \c T is a temporary storage area. The initial content of such a
2932b31808SJens Wiklander  *       parameter is ignored and the final content is unspecified.
3032b31808SJens Wiklander  * - **Bignum sizes**: bignum sizes are usually expressed by the \c limbs
3132b31808SJens Wiklander  *   member of the modulus argument. All bignum parameters must have the same
3232b31808SJens Wiklander  *   number of limbs as the modulus. All bignum sizes must be at least 1 and
3332b31808SJens Wiklander  *   must be significantly less than #SIZE_MAX. The behavior if a size is 0 is
3432b31808SJens Wiklander  *   undefined.
3532b31808SJens Wiklander  * - **Bignum representation**: the representation of inputs and outputs is
3632b31808SJens Wiklander  *   specified by the \c int_rep field of the modulus for arithmetic
3732b31808SJens Wiklander  *   functions. Utility functions may allow for different representation.
3832b31808SJens Wiklander  * - **Parameter ordering**: for bignum parameters, outputs come before inputs.
3932b31808SJens Wiklander  *   The modulus is passed after other bignum input parameters. Temporaries
4032b31808SJens Wiklander  *   come last.
4132b31808SJens Wiklander  * - **Aliasing**: in general, output bignums may be aliased to one or more
4232b31808SJens Wiklander  *   inputs. Modulus values may not be aliased to any other parameter. Outputs
4332b31808SJens Wiklander  *   may not be aliased to one another. Temporaries may not be aliased to any
4432b31808SJens Wiklander  *   other parameter.
4532b31808SJens Wiklander  * - **Overlap**: apart from aliasing of limb array pointers (where two
4632b31808SJens Wiklander  *   arguments are equal pointers), overlap is not supported and may result
4732b31808SJens Wiklander  *   in undefined behavior.
4832b31808SJens Wiklander  * - **Error handling**: This is a low-level module. Functions generally do not
4932b31808SJens Wiklander  *   try to protect against invalid arguments such as nonsensical sizes or
5032b31808SJens Wiklander  *   null pointers. Note that passing bignums with a different size than the
5132b31808SJens Wiklander  *   modulus may lead to buffer overflows. Some functions which allocate
5232b31808SJens Wiklander  *   memory or handle reading/writing of bignums will return an error if
5332b31808SJens Wiklander  *   memory allocation fails or if buffer sizes are invalid.
5432b31808SJens Wiklander  * - **Modular representatives**: all functions expect inputs to be in the
5532b31808SJens Wiklander  *   range [0, \c N - 1] and guarantee outputs in the range [0, \c N - 1]. If
5632b31808SJens Wiklander  *   an input is out of range, outputs are fully unspecified, though bignum
5732b31808SJens Wiklander  *   values out of range should not cause buffer overflows (beware that this is
5832b31808SJens Wiklander  *   not extensively tested).
5932b31808SJens Wiklander  */
6032b31808SJens Wiklander 
6132b31808SJens Wiklander /*
6232b31808SJens Wiklander  *  Copyright The Mbed TLS Contributors
63*b0563631STom Van Eyck  *  SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6432b31808SJens Wiklander  */
6532b31808SJens Wiklander 
6632b31808SJens Wiklander #ifndef MBEDTLS_BIGNUM_MOD_RAW_H
6732b31808SJens Wiklander #define MBEDTLS_BIGNUM_MOD_RAW_H
6832b31808SJens Wiklander 
6932b31808SJens Wiklander #include "common.h"
7032b31808SJens Wiklander 
7132b31808SJens Wiklander #if defined(MBEDTLS_BIGNUM_C)
7232b31808SJens Wiklander #include "mbedtls/bignum.h"
7332b31808SJens Wiklander #endif
7432b31808SJens Wiklander 
7532b31808SJens Wiklander #include "bignum_mod.h"
7632b31808SJens Wiklander 
7732b31808SJens Wiklander /**
7832b31808SJens Wiklander  * \brief   Perform a safe conditional copy of an MPI which doesn't reveal
7932b31808SJens Wiklander  *          whether the assignment was done or not.
8032b31808SJens Wiklander  *
8132b31808SJens Wiklander  * The size to copy is determined by \p N.
8232b31808SJens Wiklander  *
8332b31808SJens Wiklander  * \param[out] X        The address of the destination MPI.
8432b31808SJens Wiklander  *                      This must be initialized. Must have enough limbs to
8532b31808SJens Wiklander  *                      store the full value of \p A.
8632b31808SJens Wiklander  * \param[in]  A        The address of the source MPI. This must be initialized.
8732b31808SJens Wiklander  * \param[in]  N        The address of the modulus related to \p X and \p A.
8832b31808SJens Wiklander  * \param      assign   The condition deciding whether to perform the
8932b31808SJens Wiklander  *                      assignment or not. Must be either 0 or 1:
9032b31808SJens Wiklander  *                      * \c 1: Perform the assignment `X = A`.
9132b31808SJens Wiklander  *                      * \c 0: Keep the original value of \p X.
9232b31808SJens Wiklander  *
9332b31808SJens Wiklander  * \note           This function avoids leaking any information about whether
9432b31808SJens Wiklander  *                 the assignment was done or not.
9532b31808SJens Wiklander  *
9632b31808SJens Wiklander  * \warning        If \p assign is neither 0 nor 1, the result of this function
9732b31808SJens Wiklander  *                 is indeterminate, and the resulting value in \p X might be
9832b31808SJens Wiklander  *                 neither its original value nor the value in \p A.
9932b31808SJens Wiklander  */
10032b31808SJens Wiklander void mbedtls_mpi_mod_raw_cond_assign(mbedtls_mpi_uint *X,
10132b31808SJens Wiklander                                      const mbedtls_mpi_uint *A,
10232b31808SJens Wiklander                                      const mbedtls_mpi_mod_modulus *N,
10332b31808SJens Wiklander                                      unsigned char assign);
10432b31808SJens Wiklander 
10532b31808SJens Wiklander /**
10632b31808SJens Wiklander  * \brief   Perform a safe conditional swap of two MPIs which doesn't reveal
10732b31808SJens Wiklander  *          whether the swap was done or not.
10832b31808SJens Wiklander  *
10932b31808SJens Wiklander  * The size to swap is determined by \p N.
11032b31808SJens Wiklander  *
11132b31808SJens Wiklander  * \param[in,out] X     The address of the first MPI. This must be initialized.
11232b31808SJens Wiklander  * \param[in,out] Y     The address of the second MPI. This must be initialized.
11332b31808SJens Wiklander  * \param[in]     N     The address of the modulus related to \p X and \p Y.
11432b31808SJens Wiklander  * \param         swap  The condition deciding whether to perform
11532b31808SJens Wiklander  *                      the swap or not. Must be either 0 or 1:
11632b31808SJens Wiklander  *                      * \c 1: Swap the values of \p X and \p Y.
11732b31808SJens Wiklander  *                      * \c 0: Keep the original values of \p X and \p Y.
11832b31808SJens Wiklander  *
11932b31808SJens Wiklander  * \note           This function avoids leaking any information about whether
12032b31808SJens Wiklander  *                 the swap was done or not.
12132b31808SJens Wiklander  *
12232b31808SJens Wiklander  * \warning        If \p swap is neither 0 nor 1, the result of this function
12332b31808SJens Wiklander  *                 is indeterminate, and both \p X and \p Y might end up with
12432b31808SJens Wiklander  *                 values different to either of the original ones.
12532b31808SJens Wiklander  */
12632b31808SJens Wiklander void mbedtls_mpi_mod_raw_cond_swap(mbedtls_mpi_uint *X,
12732b31808SJens Wiklander                                    mbedtls_mpi_uint *Y,
12832b31808SJens Wiklander                                    const mbedtls_mpi_mod_modulus *N,
12932b31808SJens Wiklander                                    unsigned char swap);
13032b31808SJens Wiklander 
13132b31808SJens Wiklander /** Import X from unsigned binary data.
13232b31808SJens Wiklander  *
13332b31808SJens Wiklander  * The MPI needs to have enough limbs to store the full value (including any
13432b31808SJens Wiklander  * most significant zero bytes in the input).
13532b31808SJens Wiklander  *
13632b31808SJens Wiklander  * \param[out] X        The address of the MPI. The size is determined by \p N.
13732b31808SJens Wiklander  *                      (In particular, it must have at least as many limbs as
13832b31808SJens Wiklander  *                      the modulus \p N.)
13932b31808SJens Wiklander  * \param[in] N         The address of the modulus related to \p X.
14032b31808SJens Wiklander  * \param[in] input     The input buffer to import from.
14132b31808SJens Wiklander  * \param input_length  The length in bytes of \p input.
14232b31808SJens Wiklander  * \param ext_rep       The endianness of the number in the input buffer.
14332b31808SJens Wiklander  *
14432b31808SJens Wiklander  * \return       \c 0 if successful.
14532b31808SJens Wiklander  * \return       #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p X isn't
14632b31808SJens Wiklander  *               large enough to hold the value in \p input.
14732b31808SJens Wiklander  * \return       #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if the external representation
14832b31808SJens Wiklander  *               of \p N is invalid or \p X is not less than \p N.
14932b31808SJens Wiklander  */
15032b31808SJens Wiklander int mbedtls_mpi_mod_raw_read(mbedtls_mpi_uint *X,
15132b31808SJens Wiklander                              const mbedtls_mpi_mod_modulus *N,
15232b31808SJens Wiklander                              const unsigned char *input,
15332b31808SJens Wiklander                              size_t input_length,
15432b31808SJens Wiklander                              mbedtls_mpi_mod_ext_rep ext_rep);
15532b31808SJens Wiklander 
15632b31808SJens Wiklander /** Export A into unsigned binary data.
15732b31808SJens Wiklander  *
15832b31808SJens Wiklander  * \param[in] A         The address of the MPI. The size is determined by \p N.
15932b31808SJens Wiklander  *                      (In particular, it must have at least as many limbs as
16032b31808SJens Wiklander  *                      the modulus \p N.)
16132b31808SJens Wiklander  * \param[in] N         The address of the modulus related to \p A.
16232b31808SJens Wiklander  * \param[out] output   The output buffer to export to.
16332b31808SJens Wiklander  * \param output_length The length in bytes of \p output.
16432b31808SJens Wiklander  * \param ext_rep       The endianness in which the number should be written into the output buffer.
16532b31808SJens Wiklander  *
16632b31808SJens Wiklander  * \return       \c 0 if successful.
16732b31808SJens Wiklander  * \return       #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p output isn't
16832b31808SJens Wiklander  *               large enough to hold the value of \p A.
16932b31808SJens Wiklander  * \return       #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if the external representation
17032b31808SJens Wiklander  *               of \p N is invalid.
17132b31808SJens Wiklander  */
17232b31808SJens Wiklander int mbedtls_mpi_mod_raw_write(const mbedtls_mpi_uint *A,
17332b31808SJens Wiklander                               const mbedtls_mpi_mod_modulus *N,
17432b31808SJens Wiklander                               unsigned char *output,
17532b31808SJens Wiklander                               size_t output_length,
17632b31808SJens Wiklander                               mbedtls_mpi_mod_ext_rep ext_rep);
17732b31808SJens Wiklander 
17832b31808SJens Wiklander /** \brief  Subtract two MPIs, returning the residue modulo the specified
17932b31808SJens Wiklander  *          modulus.
18032b31808SJens Wiklander  *
18132b31808SJens Wiklander  * The size of the operation is determined by \p N. \p A and \p B must have
18232b31808SJens Wiklander  * the same number of limbs as \p N.
18332b31808SJens Wiklander  *
18432b31808SJens Wiklander  * \p X may be aliased to \p A or \p B, or even both, but may not overlap
18532b31808SJens Wiklander  * either otherwise.
18632b31808SJens Wiklander  *
18732b31808SJens Wiklander  * \param[out] X        The address of the result MPI.
18832b31808SJens Wiklander  *                      This must be initialized. Must have enough limbs to
18932b31808SJens Wiklander  *                      store the full value of the result.
19032b31808SJens Wiklander  * \param[in]  A        The address of the first MPI. This must be initialized.
19132b31808SJens Wiklander  * \param[in]  B        The address of the second MPI. This must be initialized.
19232b31808SJens Wiklander  * \param[in]  N        The address of the modulus. Used to perform a modulo
19332b31808SJens Wiklander  *                      operation on the result of the subtraction.
19432b31808SJens Wiklander  */
19532b31808SJens Wiklander void mbedtls_mpi_mod_raw_sub(mbedtls_mpi_uint *X,
19632b31808SJens Wiklander                              const mbedtls_mpi_uint *A,
19732b31808SJens Wiklander                              const mbedtls_mpi_uint *B,
19832b31808SJens Wiklander                              const mbedtls_mpi_mod_modulus *N);
19932b31808SJens Wiklander 
20032b31808SJens Wiklander /** \brief  Multiply two MPIs, returning the residue modulo the specified
20132b31808SJens Wiklander  *          modulus.
20232b31808SJens Wiklander  *
20332b31808SJens Wiklander  * \note Currently handles the case when `N->int_rep` is
20432b31808SJens Wiklander  * MBEDTLS_MPI_MOD_REP_MONTGOMERY.
20532b31808SJens Wiklander  *
20632b31808SJens Wiklander  * The size of the operation is determined by \p N. \p A, \p B and \p X must
20732b31808SJens Wiklander  * all be associated with the modulus \p N and must all have the same number
20832b31808SJens Wiklander  * of limbs as \p N.
20932b31808SJens Wiklander  *
21032b31808SJens Wiklander  * \p X may be aliased to \p A or \p B, or even both, but may not overlap
21132b31808SJens Wiklander  * either otherwise. They may not alias \p N (since they must be in canonical
21232b31808SJens Wiklander  * form, they cannot == \p N).
21332b31808SJens Wiklander  *
21432b31808SJens Wiklander  * \param[out] X        The address of the result MPI. Must have the same
21532b31808SJens Wiklander  *                      number of limbs as \p N.
21632b31808SJens Wiklander  *                      On successful completion, \p X contains the result of
21732b31808SJens Wiklander  *                      the multiplication `A * B * R^-1` mod N where
21832b31808SJens Wiklander  *                      `R = 2^(biL * N->limbs)`.
21932b31808SJens Wiklander  * \param[in]  A        The address of the first MPI.
22032b31808SJens Wiklander  * \param[in]  B        The address of the second MPI.
22132b31808SJens Wiklander  * \param[in]  N        The address of the modulus. Used to perform a modulo
22232b31808SJens Wiklander  *                      operation on the result of the multiplication.
22332b31808SJens Wiklander  * \param[in,out] T     Temporary storage of size at least 2 * N->limbs + 1
22432b31808SJens Wiklander  *                      limbs. Its initial content is unused and
22532b31808SJens Wiklander  *                      its final content is indeterminate.
22632b31808SJens Wiklander  *                      It must not alias or otherwise overlap any of the
22732b31808SJens Wiklander  *                      other parameters.
22832b31808SJens Wiklander  */
22932b31808SJens Wiklander void mbedtls_mpi_mod_raw_mul(mbedtls_mpi_uint *X,
23032b31808SJens Wiklander                              const mbedtls_mpi_uint *A,
23132b31808SJens Wiklander                              const mbedtls_mpi_uint *B,
23232b31808SJens Wiklander                              const mbedtls_mpi_mod_modulus *N,
23332b31808SJens Wiklander                              mbedtls_mpi_uint *T);
23432b31808SJens Wiklander 
23532b31808SJens Wiklander /**
23632b31808SJens Wiklander  * \brief          Returns the number of limbs of working memory required for
23732b31808SJens Wiklander  *                 a call to `mbedtls_mpi_mod_raw_inv_prime()`.
23832b31808SJens Wiklander  *
23932b31808SJens Wiklander  * \note           This will always be at least
24032b31808SJens Wiklander  *                 `mbedtls_mpi_core_montmul_working_limbs(AN_limbs)`,
24132b31808SJens Wiklander  *                 i.e. sufficient for a call to `mbedtls_mpi_core_montmul()`.
24232b31808SJens Wiklander  *
24332b31808SJens Wiklander  * \param AN_limbs The number of limbs in the input `A` and the modulus `N`
24432b31808SJens Wiklander  *                 (they must be the same size) that will be given to
24532b31808SJens Wiklander  *                 `mbedtls_mpi_mod_raw_inv_prime()`.
24632b31808SJens Wiklander  *
24732b31808SJens Wiklander  * \return         The number of limbs of working memory required by
24832b31808SJens Wiklander  *                 `mbedtls_mpi_mod_raw_inv_prime()`.
24932b31808SJens Wiklander  */
25032b31808SJens Wiklander size_t mbedtls_mpi_mod_raw_inv_prime_working_limbs(size_t AN_limbs);
25132b31808SJens Wiklander 
25232b31808SJens Wiklander /**
25332b31808SJens Wiklander  * \brief Perform fixed-width modular inversion of a Montgomery-form MPI with
25432b31808SJens Wiklander  *        respect to a modulus \p N that must be prime.
25532b31808SJens Wiklander  *
25632b31808SJens Wiklander  * \p X may be aliased to \p A, but not to \p N or \p RR.
25732b31808SJens Wiklander  *
25832b31808SJens Wiklander  * \param[out] X     The modular inverse of \p A with respect to \p N.
25932b31808SJens Wiklander  *                   Will be in Montgomery form.
26032b31808SJens Wiklander  * \param[in] A      The number to calculate the modular inverse of.
26132b31808SJens Wiklander  *                   Must be in Montgomery form. Must not be 0.
26232b31808SJens Wiklander  * \param[in] N      The modulus, as a little-endian array of length \p AN_limbs.
26332b31808SJens Wiklander  *                   Must be prime.
26432b31808SJens Wiklander  * \param AN_limbs   The number of limbs in \p A, \p N and \p RR.
26532b31808SJens Wiklander  * \param[in] RR     The precomputed residue of 2^{2*biL} modulo N, as a little-
26632b31808SJens Wiklander  *                   endian array of length \p AN_limbs.
26732b31808SJens Wiklander  * \param[in,out] T  Temporary storage of at least the number of limbs returned
26832b31808SJens Wiklander  *                   by `mbedtls_mpi_mod_raw_inv_prime_working_limbs()`.
26932b31808SJens Wiklander  *                   Its initial content is unused and its final content is
27032b31808SJens Wiklander  *                   indeterminate.
27132b31808SJens Wiklander  *                   It must not alias or otherwise overlap any of the other
27232b31808SJens Wiklander  *                   parameters.
27332b31808SJens Wiklander  *                   It is up to the caller to zeroize \p T when it is no
27432b31808SJens Wiklander  *                   longer needed, and before freeing it if it was dynamically
27532b31808SJens Wiklander  *                   allocated.
27632b31808SJens Wiklander  */
27732b31808SJens Wiklander void mbedtls_mpi_mod_raw_inv_prime(mbedtls_mpi_uint *X,
27832b31808SJens Wiklander                                    const mbedtls_mpi_uint *A,
27932b31808SJens Wiklander                                    const mbedtls_mpi_uint *N,
28032b31808SJens Wiklander                                    size_t AN_limbs,
28132b31808SJens Wiklander                                    const mbedtls_mpi_uint *RR,
28232b31808SJens Wiklander                                    mbedtls_mpi_uint *T);
28332b31808SJens Wiklander 
28432b31808SJens Wiklander /**
28532b31808SJens Wiklander  * \brief Perform a known-size modular addition.
28632b31808SJens Wiklander  *
28732b31808SJens Wiklander  * Calculate `A + B modulo N`.
28832b31808SJens Wiklander  *
28932b31808SJens Wiklander  * The number of limbs in each operand, and the result, is given by the
29032b31808SJens Wiklander  * modulus \p N.
29132b31808SJens Wiklander  *
29232b31808SJens Wiklander  * \p X may be aliased to \p A or \p B, or even both, but may not overlap
29332b31808SJens Wiklander  * either otherwise.
29432b31808SJens Wiklander  *
29532b31808SJens Wiklander  * \param[out] X    The result of the modular addition.
29632b31808SJens Wiklander  * \param[in] A     Little-endian presentation of the left operand. This
29732b31808SJens Wiklander  *                  must be smaller than \p N.
29832b31808SJens Wiklander  * \param[in] B     Little-endian presentation of the right operand. This
29932b31808SJens Wiklander  *                  must be smaller than \p N.
30032b31808SJens Wiklander  * \param[in] N     The address of the modulus.
30132b31808SJens Wiklander  */
30232b31808SJens Wiklander void mbedtls_mpi_mod_raw_add(mbedtls_mpi_uint *X,
30332b31808SJens Wiklander                              const mbedtls_mpi_uint *A,
30432b31808SJens Wiklander                              const mbedtls_mpi_uint *B,
30532b31808SJens Wiklander                              const mbedtls_mpi_mod_modulus *N);
30632b31808SJens Wiklander 
30732b31808SJens Wiklander /** Convert an MPI from canonical representation (little-endian limb array)
30832b31808SJens Wiklander  * to the representation associated with the modulus.
30932b31808SJens Wiklander  *
31032b31808SJens Wiklander  * \param[in,out] X The limb array to convert.
31132b31808SJens Wiklander  *                  It must have as many limbs as \p N.
31232b31808SJens Wiklander  *                  It is converted in place.
31332b31808SJens Wiklander  *                  If this function returns an error, the content of \p X
31432b31808SJens Wiklander  *                  is unspecified.
31532b31808SJens Wiklander  * \param[in] N     The modulus structure.
31632b31808SJens Wiklander  *
31732b31808SJens Wiklander  * \return          \c 0 if successful.
31832b31808SJens Wiklander  *                  Otherwise an \c MBEDTLS_ERR_MPI_xxx error code.
31932b31808SJens Wiklander  */
32032b31808SJens Wiklander int mbedtls_mpi_mod_raw_canonical_to_modulus_rep(
32132b31808SJens Wiklander     mbedtls_mpi_uint *X,
32232b31808SJens Wiklander     const mbedtls_mpi_mod_modulus *N);
32332b31808SJens Wiklander 
32432b31808SJens Wiklander /** Convert an MPI from the representation associated with the modulus
32532b31808SJens Wiklander  * to canonical representation (little-endian limb array).
32632b31808SJens Wiklander  *
32732b31808SJens Wiklander  * \param[in,out] X The limb array to convert.
32832b31808SJens Wiklander  *                  It must have as many limbs as \p N.
32932b31808SJens Wiklander  *                  It is converted in place.
33032b31808SJens Wiklander  *                  If this function returns an error, the content of \p X
33132b31808SJens Wiklander  *                  is unspecified.
33232b31808SJens Wiklander  * \param[in] N     The modulus structure.
33332b31808SJens Wiklander  *
33432b31808SJens Wiklander  * \return          \c 0 if successful.
33532b31808SJens Wiklander  *                  Otherwise an \c MBEDTLS_ERR_MPI_xxx error code.
33632b31808SJens Wiklander  */
33732b31808SJens Wiklander int mbedtls_mpi_mod_raw_modulus_to_canonical_rep(
33832b31808SJens Wiklander     mbedtls_mpi_uint *X,
33932b31808SJens Wiklander     const mbedtls_mpi_mod_modulus *N);
34032b31808SJens Wiklander 
34132b31808SJens Wiklander /** Generate a random number uniformly in a range.
34232b31808SJens Wiklander  *
34332b31808SJens Wiklander  * This function generates a random number between \p min inclusive and
34432b31808SJens Wiklander  * \p N exclusive.
34532b31808SJens Wiklander  *
34632b31808SJens Wiklander  * The procedure complies with RFC 6979 §3.3 (deterministic ECDSA)
34732b31808SJens Wiklander  * when the RNG is a suitably parametrized instance of HMAC_DRBG
34832b31808SJens Wiklander  * and \p min is \c 1.
34932b31808SJens Wiklander  *
35032b31808SJens Wiklander  * \note           There are `N - min` possible outputs. The lower bound
35132b31808SJens Wiklander  *                 \p min can be reached, but the upper bound \p N cannot.
35232b31808SJens Wiklander  *
35332b31808SJens Wiklander  * \param X        The destination MPI, in canonical representation modulo \p N.
35432b31808SJens Wiklander  *                 It must not be aliased with \p N or otherwise overlap it.
35532b31808SJens Wiklander  * \param min      The minimum value to return. It must be strictly smaller
35632b31808SJens Wiklander  *                 than \b N.
35732b31808SJens Wiklander  * \param N        The modulus.
35832b31808SJens Wiklander  *                 This is the upper bound of the output range, exclusive.
35932b31808SJens Wiklander  * \param f_rng    The RNG function to use. This must not be \c NULL.
36032b31808SJens Wiklander  * \param p_rng    The RNG parameter to be passed to \p f_rng.
36132b31808SJens Wiklander  *
36232b31808SJens Wiklander  * \return         \c 0 if successful.
36332b31808SJens Wiklander  * \return         #MBEDTLS_ERR_MPI_NOT_ACCEPTABLE if the implementation was
36432b31808SJens Wiklander  *                 unable to find a suitable value within a limited number
36532b31808SJens Wiklander  *                 of attempts. This has a negligible probability if \p N
36632b31808SJens Wiklander  *                 is significantly larger than \p min, which is the case
36732b31808SJens Wiklander  *                 for all usual cryptographic applications.
36832b31808SJens Wiklander  */
36932b31808SJens Wiklander int mbedtls_mpi_mod_raw_random(mbedtls_mpi_uint *X,
37032b31808SJens Wiklander                                mbedtls_mpi_uint min,
37132b31808SJens Wiklander                                const mbedtls_mpi_mod_modulus *N,
37232b31808SJens Wiklander                                int (*f_rng)(void *, unsigned char *, size_t),
37332b31808SJens Wiklander                                void *p_rng);
37432b31808SJens Wiklander 
37532b31808SJens Wiklander /** Convert an MPI into Montgomery form.
37632b31808SJens Wiklander  *
37732b31808SJens Wiklander  * \param X      The address of the MPI.
37832b31808SJens Wiklander  *               Must have the same number of limbs as \p N.
37932b31808SJens Wiklander  * \param N      The address of the modulus, which gives the size of
38032b31808SJens Wiklander  *               the base `R` = 2^(biL*N->limbs).
38132b31808SJens Wiklander  *
38232b31808SJens Wiklander  * \return       \c 0 if successful.
38332b31808SJens Wiklander  */
38432b31808SJens Wiklander int mbedtls_mpi_mod_raw_to_mont_rep(mbedtls_mpi_uint *X,
38532b31808SJens Wiklander                                     const mbedtls_mpi_mod_modulus *N);
38632b31808SJens Wiklander 
38732b31808SJens Wiklander /** Convert an MPI back from Montgomery representation.
38832b31808SJens Wiklander  *
38932b31808SJens Wiklander  * \param X      The address of the MPI.
39032b31808SJens Wiklander  *               Must have the same number of limbs as \p N.
39132b31808SJens Wiklander  * \param N      The address of the modulus, which gives the size of
39232b31808SJens Wiklander  *               the base `R`= 2^(biL*N->limbs).
39332b31808SJens Wiklander  *
39432b31808SJens Wiklander  * \return       \c 0 if successful.
39532b31808SJens Wiklander  */
39632b31808SJens Wiklander int mbedtls_mpi_mod_raw_from_mont_rep(mbedtls_mpi_uint *X,
39732b31808SJens Wiklander                                       const mbedtls_mpi_mod_modulus *N);
39832b31808SJens Wiklander 
39932b31808SJens Wiklander /** \brief  Perform fixed width modular negation.
40032b31808SJens Wiklander  *
40132b31808SJens Wiklander  * The size of the operation is determined by \p N. \p A must have
40232b31808SJens Wiklander  * the same number of limbs as \p N.
40332b31808SJens Wiklander  *
40432b31808SJens Wiklander  * \p X may be aliased to \p A.
40532b31808SJens Wiklander  *
40632b31808SJens Wiklander  * \param[out] X        The result of the modular negation.
40732b31808SJens Wiklander  *                      This must be initialized.
40832b31808SJens Wiklander  * \param[in] A         Little-endian presentation of the input operand. This
40932b31808SJens Wiklander  *                      must be less than or equal to \p N.
41032b31808SJens Wiklander  * \param[in] N         The modulus to use.
41132b31808SJens Wiklander  */
41232b31808SJens Wiklander void mbedtls_mpi_mod_raw_neg(mbedtls_mpi_uint *X,
41332b31808SJens Wiklander                              const mbedtls_mpi_uint *A,
41432b31808SJens Wiklander                              const mbedtls_mpi_mod_modulus *N);
41532b31808SJens Wiklander 
41632b31808SJens Wiklander #endif /* MBEDTLS_BIGNUM_MOD_RAW_H */
417