132b31808SJens Wiklander /** 232b31808SJens Wiklander * Modular bignum functions 332b31808SJens Wiklander * 432b31808SJens Wiklander * This module implements operations on integers modulo some fixed modulus. 532b31808SJens Wiklander * 632b31808SJens Wiklander * The functions in this module obey the following conventions unless 732b31808SJens Wiklander * explicitly indicated otherwise: 832b31808SJens Wiklander * 932b31808SJens Wiklander * - **Modulus parameters**: the modulus is passed as a pointer to a structure 1032b31808SJens Wiklander * of type #mbedtls_mpi_mod_modulus. The structure must be set up with an 1132b31808SJens Wiklander * array of limbs storing the bignum value of the modulus. The modulus must 1232b31808SJens Wiklander * be odd and is assumed to have no leading zeroes. The modulus is usually 1332b31808SJens Wiklander * named \c N and is usually input-only. Functions which take a parameter 1432b31808SJens Wiklander * of type \c const #mbedtls_mpi_mod_modulus* must not modify its value. 1532b31808SJens Wiklander * - **Bignum parameters**: Bignums are passed as pointers to an array of 1632b31808SJens Wiklander * limbs or to a #mbedtls_mpi_mod_residue structure. A limb has the type 1732b31808SJens Wiklander * #mbedtls_mpi_uint. Residues must be initialized before use, and must be 1832b31808SJens Wiklander * associated with the modulus \c N. Unless otherwise specified: 1932b31808SJens Wiklander * - Bignum parameters called \c A, \c B, ... are inputs and are not 2032b31808SJens Wiklander * modified by the function. Functions which take a parameter of 2132b31808SJens Wiklander * type \c const #mbedtls_mpi_mod_residue* must not modify its value. 2232b31808SJens Wiklander * - Bignum parameters called \c X, \c Y, ... are outputs or input-output. 2332b31808SJens Wiklander * The initial bignum value of output-only parameters is ignored, but 2432b31808SJens Wiklander * they must be set up and associated with the modulus \c N. Some 2532b31808SJens Wiklander * functions (typically constant-flow) require that the limbs in an 2632b31808SJens Wiklander * output residue are initialized. 2732b31808SJens Wiklander * - Bignum parameters called \c p are inputs used to set up a modulus or 2832b31808SJens Wiklander * residue. These must be pointers to an array of limbs. 2932b31808SJens Wiklander * - \c T is a temporary storage area. The initial content of such a 3032b31808SJens Wiklander * parameter is ignored and the final content is unspecified. 3132b31808SJens Wiklander * - Some functions use different names, such as \c r for the residue. 3232b31808SJens Wiklander * - **Bignum sizes**: bignum sizes are always expressed in limbs. Both 3332b31808SJens Wiklander * #mbedtls_mpi_mod_modulus and #mbedtls_mpi_mod_residue have a \c limbs 3432b31808SJens Wiklander * member storing its size. All bignum parameters must have the same 3532b31808SJens Wiklander * number of limbs as the modulus. All bignum sizes must be at least 1 and 3632b31808SJens Wiklander * must be significantly less than #SIZE_MAX. The behavior if a size is 0 is 3732b31808SJens Wiklander * undefined. 3832b31808SJens Wiklander * - **Bignum representation**: the representation of inputs and outputs is 3932b31808SJens Wiklander * specified by the \c int_rep field of the modulus. 4032b31808SJens Wiklander * - **Parameter ordering**: for bignum parameters, outputs come before inputs. 4132b31808SJens Wiklander * The modulus is passed after residues. Temporaries come last. 4232b31808SJens Wiklander * - **Aliasing**: in general, output bignums may be aliased to one or more 4332b31808SJens Wiklander * inputs. Modulus values may not be aliased to any other parameter. Outputs 4432b31808SJens Wiklander * may not be aliased to one another. Temporaries may not be aliased to any 4532b31808SJens Wiklander * other parameter. 4632b31808SJens Wiklander * - **Overlap**: apart from aliasing of residue pointers (where two residue 4732b31808SJens Wiklander * arguments are equal pointers), overlap is not supported and may result 4832b31808SJens Wiklander * in undefined behavior. 4932b31808SJens Wiklander * - **Error handling**: functions generally check compatibility of input 5032b31808SJens Wiklander * sizes. Most functions will not check that input values are in canonical 5132b31808SJens Wiklander * form (i.e. that \c A < \c N), this is only checked during setup of a 5232b31808SJens Wiklander * residue structure. 5332b31808SJens Wiklander * - **Modular representatives**: all functions expect inputs to be in the 5432b31808SJens Wiklander * range [0, \c N - 1] and guarantee outputs in the range [0, \c N - 1]. 5532b31808SJens Wiklander * Residues are set up with an associated modulus, and operations are only 5632b31808SJens Wiklander * guaranteed to work if the modulus is associated with all residue 5732b31808SJens Wiklander * parameters. If a residue is passed with a modulus other than the one it 5832b31808SJens Wiklander * is associated with, then it may be out of range. If an input is out of 5932b31808SJens Wiklander * range, outputs are fully unspecified, though bignum values out of range 6032b31808SJens Wiklander * should not cause buffer overflows (beware that this is not extensively 6132b31808SJens Wiklander * tested). 6232b31808SJens Wiklander */ 6332b31808SJens Wiklander 6432b31808SJens Wiklander /* 6532b31808SJens Wiklander * Copyright The Mbed TLS Contributors 66*b0563631STom Van Eyck * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later 6732b31808SJens Wiklander */ 6832b31808SJens Wiklander 6932b31808SJens Wiklander #ifndef MBEDTLS_BIGNUM_MOD_H 7032b31808SJens Wiklander #define MBEDTLS_BIGNUM_MOD_H 7132b31808SJens Wiklander 7232b31808SJens Wiklander #include "common.h" 7332b31808SJens Wiklander 7432b31808SJens Wiklander #if defined(MBEDTLS_BIGNUM_C) 7532b31808SJens Wiklander #include "mbedtls/bignum.h" 7632b31808SJens Wiklander #endif 7732b31808SJens Wiklander 7832b31808SJens Wiklander /** How residues associated with a modulus are represented. 7932b31808SJens Wiklander * 8032b31808SJens Wiklander * This also determines which fields of the modulus structure are valid and 8132b31808SJens Wiklander * what their contents are (see #mbedtls_mpi_mod_modulus). 8232b31808SJens Wiklander */ 8332b31808SJens Wiklander typedef enum { 8432b31808SJens Wiklander /** Representation not chosen (makes the modulus structure invalid). */ 8532b31808SJens Wiklander MBEDTLS_MPI_MOD_REP_INVALID = 0, 8632b31808SJens Wiklander /* Skip 1 as it is slightly easier to accidentally pass to functions. */ 8732b31808SJens Wiklander /** Montgomery representation. */ 8832b31808SJens Wiklander MBEDTLS_MPI_MOD_REP_MONTGOMERY = 2, 89*b0563631STom Van Eyck /* Optimised reduction available. This indicates a coordinate modulus (P) 90*b0563631STom Van Eyck * and one or more of the following have been configured: 91*b0563631STom Van Eyck * - A nist curve (MBEDTLS_ECP_DP_SECPXXXR1_ENABLED) & MBEDTLS_ECP_NIST_OPTIM. 92*b0563631STom Van Eyck * - A Kobliz Curve. 93*b0563631STom Van Eyck * - A Fast Reduction Curve CURVE25519 or CURVE448. */ 9432b31808SJens Wiklander MBEDTLS_MPI_MOD_REP_OPT_RED, 9532b31808SJens Wiklander } mbedtls_mpi_mod_rep_selector; 9632b31808SJens Wiklander 9732b31808SJens Wiklander /* Make mbedtls_mpi_mod_rep_selector and mbedtls_mpi_mod_ext_rep disjoint to 9832b31808SJens Wiklander * make it easier to catch when they are accidentally swapped. */ 9932b31808SJens Wiklander typedef enum { 10032b31808SJens Wiklander MBEDTLS_MPI_MOD_EXT_REP_INVALID = 0, 10132b31808SJens Wiklander MBEDTLS_MPI_MOD_EXT_REP_LE = 8, 10232b31808SJens Wiklander MBEDTLS_MPI_MOD_EXT_REP_BE 10332b31808SJens Wiklander } mbedtls_mpi_mod_ext_rep; 10432b31808SJens Wiklander 10532b31808SJens Wiklander typedef struct { 10632b31808SJens Wiklander mbedtls_mpi_uint *p; 10732b31808SJens Wiklander size_t limbs; 10832b31808SJens Wiklander } mbedtls_mpi_mod_residue; 10932b31808SJens Wiklander 11032b31808SJens Wiklander typedef struct { 11132b31808SJens Wiklander mbedtls_mpi_uint const *rr; /* The residue for 2^{2*n*biL} mod N */ 11232b31808SJens Wiklander mbedtls_mpi_uint mm; /* Montgomery const for -N^{-1} mod 2^{ciL} */ 11332b31808SJens Wiklander } mbedtls_mpi_mont_struct; 11432b31808SJens Wiklander 115*b0563631STom Van Eyck typedef int (*mbedtls_mpi_modp_fn)(mbedtls_mpi_uint *X, size_t X_limbs); 116*b0563631STom Van Eyck 117*b0563631STom Van Eyck typedef struct { 118*b0563631STom Van Eyck mbedtls_mpi_modp_fn modp; /* The optimised reduction function pointer */ 119*b0563631STom Van Eyck } mbedtls_mpi_opt_red_struct; 12032b31808SJens Wiklander 12132b31808SJens Wiklander typedef struct { 12232b31808SJens Wiklander const mbedtls_mpi_uint *p; 12332b31808SJens Wiklander size_t limbs; // number of limbs 12432b31808SJens Wiklander size_t bits; // bitlen of p 12532b31808SJens Wiklander mbedtls_mpi_mod_rep_selector int_rep; // selector to signal the active member of the union 12632b31808SJens Wiklander union rep { 12732b31808SJens Wiklander /* if int_rep == #MBEDTLS_MPI_MOD_REP_MONTGOMERY */ 12832b31808SJens Wiklander mbedtls_mpi_mont_struct mont; 12932b31808SJens Wiklander /* if int_rep == #MBEDTLS_MPI_MOD_REP_OPT_RED */ 13032b31808SJens Wiklander mbedtls_mpi_opt_red_struct ored; 13132b31808SJens Wiklander } rep; 13232b31808SJens Wiklander } mbedtls_mpi_mod_modulus; 13332b31808SJens Wiklander 13432b31808SJens Wiklander /** Setup a residue structure. 13532b31808SJens Wiklander * 13632b31808SJens Wiklander * The residue will be set up with the buffer \p p and modulus \p N. 13732b31808SJens Wiklander * 13832b31808SJens Wiklander * The memory pointed to by \p p will be used by the resulting residue structure. 13932b31808SJens Wiklander * The value at the pointed-to memory will be the initial value of \p r and must 14032b31808SJens Wiklander * hold a value that is less than the modulus. This value will be used as-is 14132b31808SJens Wiklander * and interpreted according to the value of the `N->int_rep` field. 14232b31808SJens Wiklander * 14332b31808SJens Wiklander * The modulus \p N will be the modulus associated with \p r. The residue \p r 14432b31808SJens Wiklander * should only be used in operations where the modulus is \p N. 14532b31808SJens Wiklander * 14632b31808SJens Wiklander * \param[out] r The address of the residue to setup. 14732b31808SJens Wiklander * \param[in] N The address of the modulus related to \p r. 14832b31808SJens Wiklander * \param[in] p The address of the limb array containing the value of \p r. 14932b31808SJens Wiklander * The memory pointed to by \p p will be used by \p r and must 15032b31808SJens Wiklander * not be modified in any way until after 15132b31808SJens Wiklander * mbedtls_mpi_mod_residue_release() is called. The data 15232b31808SJens Wiklander * pointed to by \p p must be less than the modulus (the value 15332b31808SJens Wiklander * pointed to by `N->p`) and already in the representation 15432b31808SJens Wiklander * indicated by `N->int_rep`. 15532b31808SJens Wiklander * \param p_limbs The number of limbs of \p p. Must be the same as the number 15632b31808SJens Wiklander * of limbs in the modulus \p N. 15732b31808SJens Wiklander * 15832b31808SJens Wiklander * \return \c 0 if successful. 15932b31808SJens Wiklander * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if \p p_limbs is less than the 16032b31808SJens Wiklander * limbs in \p N or if \p p is not less than \p N. 16132b31808SJens Wiklander */ 16232b31808SJens Wiklander int mbedtls_mpi_mod_residue_setup(mbedtls_mpi_mod_residue *r, 16332b31808SJens Wiklander const mbedtls_mpi_mod_modulus *N, 16432b31808SJens Wiklander mbedtls_mpi_uint *p, 16532b31808SJens Wiklander size_t p_limbs); 16632b31808SJens Wiklander 16732b31808SJens Wiklander /** Unbind elements of a residue structure. 16832b31808SJens Wiklander * 16932b31808SJens Wiklander * This function removes the reference to the limb array that was passed to 17032b31808SJens Wiklander * mbedtls_mpi_mod_residue_setup() to make it safe to free or use again. 17132b31808SJens Wiklander * 17232b31808SJens Wiklander * This function invalidates \p r and it must not be used until after 17332b31808SJens Wiklander * mbedtls_mpi_mod_residue_setup() is called on it again. 17432b31808SJens Wiklander * 17532b31808SJens Wiklander * \param[out] r The address of residue to release. 17632b31808SJens Wiklander */ 17732b31808SJens Wiklander void mbedtls_mpi_mod_residue_release(mbedtls_mpi_mod_residue *r); 17832b31808SJens Wiklander 17932b31808SJens Wiklander /** Initialize a modulus structure. 18032b31808SJens Wiklander * 18132b31808SJens Wiklander * \param[out] N The address of the modulus structure to initialize. 18232b31808SJens Wiklander */ 18332b31808SJens Wiklander void mbedtls_mpi_mod_modulus_init(mbedtls_mpi_mod_modulus *N); 18432b31808SJens Wiklander 18532b31808SJens Wiklander /** Setup a modulus structure. 18632b31808SJens Wiklander * 18732b31808SJens Wiklander * \param[out] N The address of the modulus structure to populate. 18832b31808SJens Wiklander * \param[in] p The address of the limb array storing the value of \p N. 18932b31808SJens Wiklander * The memory pointed to by \p p will be used by \p N and must 19032b31808SJens Wiklander * not be modified in any way until after 19132b31808SJens Wiklander * mbedtls_mpi_mod_modulus_free() is called. 19232b31808SJens Wiklander * \param p_limbs The number of limbs of \p p. 19332b31808SJens Wiklander * 19432b31808SJens Wiklander * \return \c 0 if successful. 19532b31808SJens Wiklander */ 19632b31808SJens Wiklander int mbedtls_mpi_mod_modulus_setup(mbedtls_mpi_mod_modulus *N, 19732b31808SJens Wiklander const mbedtls_mpi_uint *p, 198*b0563631STom Van Eyck size_t p_limbs); 199*b0563631STom Van Eyck 200*b0563631STom Van Eyck /** Setup an optimised-reduction compatible modulus structure. 201*b0563631STom Van Eyck * 202*b0563631STom Van Eyck * \param[out] N The address of the modulus structure to populate. 203*b0563631STom Van Eyck * \param[in] p The address of the limb array storing the value of \p N. 204*b0563631STom Van Eyck * The memory pointed to by \p p will be used by \p N and must 205*b0563631STom Van Eyck * not be modified in any way until after 206*b0563631STom Van Eyck * mbedtls_mpi_mod_modulus_free() is called. 207*b0563631STom Van Eyck * \param p_limbs The number of limbs of \p p. 208*b0563631STom Van Eyck * \param modp A pointer to the optimised reduction function to use. \p p. 209*b0563631STom Van Eyck * 210*b0563631STom Van Eyck * \return \c 0 if successful. 211*b0563631STom Van Eyck */ 212*b0563631STom Van Eyck int mbedtls_mpi_mod_optred_modulus_setup(mbedtls_mpi_mod_modulus *N, 213*b0563631STom Van Eyck const mbedtls_mpi_uint *p, 21432b31808SJens Wiklander size_t p_limbs, 215*b0563631STom Van Eyck mbedtls_mpi_modp_fn modp); 21632b31808SJens Wiklander 21732b31808SJens Wiklander /** Free elements of a modulus structure. 21832b31808SJens Wiklander * 21932b31808SJens Wiklander * This function frees any memory allocated by mbedtls_mpi_mod_modulus_setup(). 22032b31808SJens Wiklander * 22132b31808SJens Wiklander * \warning This function does not free the limb array passed to 22232b31808SJens Wiklander * mbedtls_mpi_mod_modulus_setup() only removes the reference to it, 22332b31808SJens Wiklander * making it safe to free or to use it again. 22432b31808SJens Wiklander * 22532b31808SJens Wiklander * \param[in,out] N The address of the modulus structure to free. 22632b31808SJens Wiklander */ 22732b31808SJens Wiklander void mbedtls_mpi_mod_modulus_free(mbedtls_mpi_mod_modulus *N); 22832b31808SJens Wiklander 22932b31808SJens Wiklander /** \brief Multiply two residues, returning the residue modulo the specified 23032b31808SJens Wiklander * modulus. 23132b31808SJens Wiklander * 23232b31808SJens Wiklander * \note Currently handles the case when `N->int_rep` is 23332b31808SJens Wiklander * MBEDTLS_MPI_MOD_REP_MONTGOMERY. 23432b31808SJens Wiklander * 23532b31808SJens Wiklander * The size of the operation is determined by \p N. \p A, \p B and \p X must 23632b31808SJens Wiklander * all be associated with the modulus \p N and must all have the same number 23732b31808SJens Wiklander * of limbs as \p N. 23832b31808SJens Wiklander * 23932b31808SJens Wiklander * \p X may be aliased to \p A or \p B, or even both, but may not overlap 24032b31808SJens Wiklander * either otherwise. They may not alias \p N (since they must be in canonical 24132b31808SJens Wiklander * form, they cannot == \p N). 24232b31808SJens Wiklander * 24332b31808SJens Wiklander * \param[out] X The address of the result MPI. Must have the same 24432b31808SJens Wiklander * number of limbs as \p N. 24532b31808SJens Wiklander * On successful completion, \p X contains the result of 24632b31808SJens Wiklander * the multiplication `A * B * R^-1` mod N where 24732b31808SJens Wiklander * `R = 2^(biL * N->limbs)`. 24832b31808SJens Wiklander * \param[in] A The address of the first MPI. 24932b31808SJens Wiklander * \param[in] B The address of the second MPI. 25032b31808SJens Wiklander * \param[in] N The address of the modulus. Used to perform a modulo 25132b31808SJens Wiklander * operation on the result of the multiplication. 25232b31808SJens Wiklander * 25332b31808SJens Wiklander * \return \c 0 if successful. 25432b31808SJens Wiklander * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if all the parameters do not 25532b31808SJens Wiklander * have the same number of limbs or \p N is invalid. 25632b31808SJens Wiklander * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory-allocation failure. 25732b31808SJens Wiklander */ 25832b31808SJens Wiklander int mbedtls_mpi_mod_mul(mbedtls_mpi_mod_residue *X, 25932b31808SJens Wiklander const mbedtls_mpi_mod_residue *A, 26032b31808SJens Wiklander const mbedtls_mpi_mod_residue *B, 26132b31808SJens Wiklander const mbedtls_mpi_mod_modulus *N); 26232b31808SJens Wiklander 26332b31808SJens Wiklander /** 26432b31808SJens Wiklander * \brief Perform a fixed-size modular subtraction. 26532b31808SJens Wiklander * 26632b31808SJens Wiklander * Calculate `A - B modulo N`. 26732b31808SJens Wiklander * 26832b31808SJens Wiklander * \p A, \p B and \p X must all have the same number of limbs as \p N. 26932b31808SJens Wiklander * 27032b31808SJens Wiklander * \p X may be aliased to \p A or \p B, or even both, but may not overlap 27132b31808SJens Wiklander * either otherwise. 27232b31808SJens Wiklander * 27332b31808SJens Wiklander * \note This function does not check that \p A or \p B are in canonical 27432b31808SJens Wiklander * form (that is, are < \p N) - that will have been done by 27532b31808SJens Wiklander * mbedtls_mpi_mod_residue_setup(). 27632b31808SJens Wiklander * 27732b31808SJens Wiklander * \param[out] X The address of the result MPI. Must be initialized. 27832b31808SJens Wiklander * Must have the same number of limbs as the modulus \p N. 27932b31808SJens Wiklander * \param[in] A The address of the first MPI. 28032b31808SJens Wiklander * \param[in] B The address of the second MPI. 28132b31808SJens Wiklander * \param[in] N The address of the modulus. Used to perform a modulo 28232b31808SJens Wiklander * operation on the result of the subtraction. 28332b31808SJens Wiklander * 28432b31808SJens Wiklander * \return \c 0 if successful. 28532b31808SJens Wiklander * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if the given MPIs do not 28632b31808SJens Wiklander * have the correct number of limbs. 28732b31808SJens Wiklander */ 28832b31808SJens Wiklander int mbedtls_mpi_mod_sub(mbedtls_mpi_mod_residue *X, 28932b31808SJens Wiklander const mbedtls_mpi_mod_residue *A, 29032b31808SJens Wiklander const mbedtls_mpi_mod_residue *B, 29132b31808SJens Wiklander const mbedtls_mpi_mod_modulus *N); 29232b31808SJens Wiklander 29332b31808SJens Wiklander /** 29432b31808SJens Wiklander * \brief Perform modular inversion of an MPI with respect to a modulus \p N. 29532b31808SJens Wiklander * 29632b31808SJens Wiklander * \p A and \p X must be associated with the modulus \p N and will therefore 29732b31808SJens Wiklander * have the same number of limbs as \p N. 29832b31808SJens Wiklander * 29932b31808SJens Wiklander * \p X may be aliased to \p A. 30032b31808SJens Wiklander * 30132b31808SJens Wiklander * \warning Currently only supports prime moduli, but does not check for them. 30232b31808SJens Wiklander * 30332b31808SJens Wiklander * \param[out] X The modular inverse of \p A with respect to \p N. 30432b31808SJens Wiklander * \param[in] A The number to calculate the modular inverse of. 30532b31808SJens Wiklander * Must not be 0. 30632b31808SJens Wiklander * \param[in] N The modulus to use. 30732b31808SJens Wiklander * 30832b31808SJens Wiklander * \return \c 0 if successful. 30932b31808SJens Wiklander * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if \p A and \p N do not 31032b31808SJens Wiklander * have the same number of limbs. 31132b31808SJens Wiklander * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if \p A is zero. 31232b31808SJens Wiklander * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if couldn't allocate enough 31332b31808SJens Wiklander * memory (needed for conversion to and from Mongtomery form 31432b31808SJens Wiklander * when not in Montgomery form already, and for temporary use 31532b31808SJens Wiklander * by the inversion calculation itself). 31632b31808SJens Wiklander */ 31732b31808SJens Wiklander 31832b31808SJens Wiklander int mbedtls_mpi_mod_inv(mbedtls_mpi_mod_residue *X, 31932b31808SJens Wiklander const mbedtls_mpi_mod_residue *A, 32032b31808SJens Wiklander const mbedtls_mpi_mod_modulus *N); 32132b31808SJens Wiklander /** 32232b31808SJens Wiklander * \brief Perform a fixed-size modular addition. 32332b31808SJens Wiklander * 32432b31808SJens Wiklander * Calculate `A + B modulo N`. 32532b31808SJens Wiklander * 32632b31808SJens Wiklander * \p A, \p B and \p X must all be associated with the modulus \p N and must 32732b31808SJens Wiklander * all have the same number of limbs as \p N. 32832b31808SJens Wiklander * 32932b31808SJens Wiklander * \p X may be aliased to \p A or \p B, or even both, but may not overlap 33032b31808SJens Wiklander * either otherwise. 33132b31808SJens Wiklander * 33232b31808SJens Wiklander * \note This function does not check that \p A or \p B are in canonical 33332b31808SJens Wiklander * form (that is, are < \p N) - that will have been done by 33432b31808SJens Wiklander * mbedtls_mpi_mod_residue_setup(). 33532b31808SJens Wiklander * 33632b31808SJens Wiklander * \param[out] X The address of the result residue. Must be initialized. 33732b31808SJens Wiklander * Must have the same number of limbs as the modulus \p N. 33832b31808SJens Wiklander * \param[in] A The address of the first input residue. 33932b31808SJens Wiklander * \param[in] B The address of the second input residue. 34032b31808SJens Wiklander * \param[in] N The address of the modulus. Used to perform a modulo 34132b31808SJens Wiklander * operation on the result of the addition. 34232b31808SJens Wiklander * 34332b31808SJens Wiklander * \return \c 0 if successful. 34432b31808SJens Wiklander * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if the given MPIs do not 34532b31808SJens Wiklander * have the correct number of limbs. 34632b31808SJens Wiklander */ 34732b31808SJens Wiklander int mbedtls_mpi_mod_add(mbedtls_mpi_mod_residue *X, 34832b31808SJens Wiklander const mbedtls_mpi_mod_residue *A, 34932b31808SJens Wiklander const mbedtls_mpi_mod_residue *B, 35032b31808SJens Wiklander const mbedtls_mpi_mod_modulus *N); 35132b31808SJens Wiklander 35232b31808SJens Wiklander /** Generate a random number uniformly in a range. 35332b31808SJens Wiklander * 35432b31808SJens Wiklander * This function generates a random number between \p min inclusive and 35532b31808SJens Wiklander * \p N exclusive. 35632b31808SJens Wiklander * 35732b31808SJens Wiklander * The procedure complies with RFC 6979 §3.3 (deterministic ECDSA) 35832b31808SJens Wiklander * when the RNG is a suitably parametrized instance of HMAC_DRBG 35932b31808SJens Wiklander * and \p min is \c 1. 36032b31808SJens Wiklander * 36132b31808SJens Wiklander * \note There are `N - min` possible outputs. The lower bound 36232b31808SJens Wiklander * \p min can be reached, but the upper bound \p N cannot. 36332b31808SJens Wiklander * 36432b31808SJens Wiklander * \param X The destination residue. 36532b31808SJens Wiklander * \param min The minimum value to return. It must be strictly smaller 36632b31808SJens Wiklander * than \b N. 36732b31808SJens Wiklander * \param N The modulus. 36832b31808SJens Wiklander * This is the upper bound of the output range, exclusive. 36932b31808SJens Wiklander * \param f_rng The RNG function to use. This must not be \c NULL. 37032b31808SJens Wiklander * \param p_rng The RNG parameter to be passed to \p f_rng. 37132b31808SJens Wiklander * 37232b31808SJens Wiklander * \return \c 0 if successful. 37332b31808SJens Wiklander * \return #MBEDTLS_ERR_MPI_NOT_ACCEPTABLE if the implementation was 37432b31808SJens Wiklander * unable to find a suitable value within a limited number 37532b31808SJens Wiklander * of attempts. This has a negligible probability if \p N 37632b31808SJens Wiklander * is significantly larger than \p min, which is the case 37732b31808SJens Wiklander * for all usual cryptographic applications. 37832b31808SJens Wiklander */ 37932b31808SJens Wiklander int mbedtls_mpi_mod_random(mbedtls_mpi_mod_residue *X, 38032b31808SJens Wiklander mbedtls_mpi_uint min, 38132b31808SJens Wiklander const mbedtls_mpi_mod_modulus *N, 38232b31808SJens Wiklander int (*f_rng)(void *, unsigned char *, size_t), 38332b31808SJens Wiklander void *p_rng); 38432b31808SJens Wiklander 38532b31808SJens Wiklander /** Read a residue from a byte buffer. 38632b31808SJens Wiklander * 38732b31808SJens Wiklander * The residue will be automatically converted to the internal representation 38832b31808SJens Wiklander * based on the value of the `N->int_rep` field. 38932b31808SJens Wiklander * 39032b31808SJens Wiklander * The modulus \p N will be the modulus associated with \p r. The residue \p r 39132b31808SJens Wiklander * should only be used in operations where the modulus is \p N or a modulus 39232b31808SJens Wiklander * equivalent to \p N (in the sense that all their fields or memory pointed by 39332b31808SJens Wiklander * their fields hold the same value). 39432b31808SJens Wiklander * 39532b31808SJens Wiklander * \param[out] r The address of the residue. It must have exactly the same 39632b31808SJens Wiklander * number of limbs as the modulus \p N. 39732b31808SJens Wiklander * \param[in] N The address of the modulus. 39832b31808SJens Wiklander * \param[in] buf The input buffer to import from. 39932b31808SJens Wiklander * \param buflen The length in bytes of \p buf. 40032b31808SJens Wiklander * \param ext_rep The endianness of the number in the input buffer. 40132b31808SJens Wiklander * 40232b31808SJens Wiklander * \return \c 0 if successful. 40332b31808SJens Wiklander * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p r isn't 40432b31808SJens Wiklander * large enough to hold the value in \p buf. 40532b31808SJens Wiklander * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if \p ext_rep 40632b31808SJens Wiklander * is invalid or the value in the buffer is not less than \p N. 40732b31808SJens Wiklander */ 40832b31808SJens Wiklander int mbedtls_mpi_mod_read(mbedtls_mpi_mod_residue *r, 40932b31808SJens Wiklander const mbedtls_mpi_mod_modulus *N, 41032b31808SJens Wiklander const unsigned char *buf, 41132b31808SJens Wiklander size_t buflen, 41232b31808SJens Wiklander mbedtls_mpi_mod_ext_rep ext_rep); 41332b31808SJens Wiklander 41432b31808SJens Wiklander /** Write a residue into a byte buffer. 41532b31808SJens Wiklander * 41632b31808SJens Wiklander * The modulus \p N must be the modulus associated with \p r (see 41732b31808SJens Wiklander * mbedtls_mpi_mod_residue_setup() and mbedtls_mpi_mod_read()). 41832b31808SJens Wiklander * 41932b31808SJens Wiklander * The residue will be automatically converted from the internal representation 42032b31808SJens Wiklander * based on the value of `N->int_rep` field. 42132b31808SJens Wiklander * 42232b31808SJens Wiklander * \warning If the buffer is smaller than `N->bits`, the number of 42332b31808SJens Wiklander * leading zeroes is leaked through timing. If \p r is 42432b31808SJens Wiklander * secret, the caller must ensure that \p buflen is at least 42532b31808SJens Wiklander * (`N->bits`+7)/8. 42632b31808SJens Wiklander * 42732b31808SJens Wiklander * \param[in] r The address of the residue. It must have the same number of 42832b31808SJens Wiklander * limbs as the modulus \p N. (\p r is an input parameter, but 42932b31808SJens Wiklander * its value will be modified during execution and restored 43032b31808SJens Wiklander * before the function returns.) 43132b31808SJens Wiklander * \param[in] N The address of the modulus associated with \p r. 43232b31808SJens Wiklander * \param[out] buf The output buffer to export to. 43332b31808SJens Wiklander * \param buflen The length in bytes of \p buf. 43432b31808SJens Wiklander * \param ext_rep The endianness in which the number should be written into 43532b31808SJens Wiklander * the output buffer. 43632b31808SJens Wiklander * 43732b31808SJens Wiklander * \return \c 0 if successful. 43832b31808SJens Wiklander * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p buf isn't 43932b31808SJens Wiklander * large enough to hold the value of \p r (without leading 44032b31808SJens Wiklander * zeroes). 44132b31808SJens Wiklander * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if \p ext_rep is invalid. 44232b31808SJens Wiklander * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if couldn't allocate enough 44332b31808SJens Wiklander * memory for conversion. Can occur only for moduli with 44432b31808SJens Wiklander * MBEDTLS_MPI_MOD_REP_MONTGOMERY. 44532b31808SJens Wiklander */ 44632b31808SJens Wiklander int mbedtls_mpi_mod_write(const mbedtls_mpi_mod_residue *r, 44732b31808SJens Wiklander const mbedtls_mpi_mod_modulus *N, 44832b31808SJens Wiklander unsigned char *buf, 44932b31808SJens Wiklander size_t buflen, 45032b31808SJens Wiklander mbedtls_mpi_mod_ext_rep ext_rep); 45132b31808SJens Wiklander 45232b31808SJens Wiklander #endif /* MBEDTLS_BIGNUM_MOD_H */ 453