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