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