Path: blob/master/thirdparty/mbedtls/library/bignum_mod_raw.h
9898 views
/**1* Low-level modular bignum functions2*3* This interface should only be used by the higher-level modular bignum4* module (bignum_mod.c) and the ECP module (ecp.c, ecp_curves.c). All other5* modules should use the high-level modular bignum interface (bignum_mod.h)6* or the legacy bignum interface (bignum.h).7*8* This is a low-level interface to operations on integers modulo which9* has no protection against passing invalid arguments such as arrays of10* the wrong size. The functions in bignum_mod.h provide a higher-level11* interface that includes protections against accidental misuse, at the12* expense of code size and sometimes more cumbersome memory management.13*14* The functions in this module obey the following conventions unless15* explicitly indicated otherwise:16* - **Modulus parameters**: the modulus is passed as a pointer to a structure17* of type #mbedtls_mpi_mod_modulus. The structure must be set up with an18* array of limbs storing the bignum value of the modulus. The modulus must19* be odd and is assumed to have no leading zeroes. The modulus is usually20* named \c N and is usually input-only.21* - **Bignum parameters**: Bignums are passed as pointers to an array of22* limbs. A limb has the type #mbedtls_mpi_uint. Unless otherwise specified:23* - Bignum parameters called \c A, \c B, ... are inputs, and are not24* modified by the function.25* - Bignum parameters called \c X, \c Y are outputs or input-output.26* The initial content of output-only parameters is ignored.27* - \c T is a temporary storage area. The initial content of such a28* parameter is ignored and the final content is unspecified.29* - **Bignum sizes**: bignum sizes are usually expressed by the \c limbs30* member of the modulus argument. All bignum parameters must have the same31* number of limbs as the modulus. All bignum sizes must be at least 1 and32* must be significantly less than #SIZE_MAX. The behavior if a size is 0 is33* undefined.34* - **Bignum representation**: the representation of inputs and outputs is35* specified by the \c int_rep field of the modulus for arithmetic36* functions. Utility functions may allow for different representation.37* - **Parameter ordering**: for bignum parameters, outputs come before inputs.38* The modulus is passed after other bignum input parameters. Temporaries39* come last.40* - **Aliasing**: in general, output bignums may be aliased to one or more41* inputs. Modulus values may not be aliased to any other parameter. Outputs42* may not be aliased to one another. Temporaries may not be aliased to any43* other parameter.44* - **Overlap**: apart from aliasing of limb array pointers (where two45* arguments are equal pointers), overlap is not supported and may result46* in undefined behavior.47* - **Error handling**: This is a low-level module. Functions generally do not48* try to protect against invalid arguments such as nonsensical sizes or49* null pointers. Note that passing bignums with a different size than the50* modulus may lead to buffer overflows. Some functions which allocate51* memory or handle reading/writing of bignums will return an error if52* memory allocation fails or if buffer sizes are invalid.53* - **Modular representatives**: all functions expect inputs to be in the54* range [0, \c N - 1] and guarantee outputs in the range [0, \c N - 1]. If55* an input is out of range, outputs are fully unspecified, though bignum56* values out of range should not cause buffer overflows (beware that this is57* not extensively tested).58*/5960/*61* Copyright The Mbed TLS Contributors62* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later63*/6465#ifndef MBEDTLS_BIGNUM_MOD_RAW_H66#define MBEDTLS_BIGNUM_MOD_RAW_H6768#include "common.h"6970#if defined(MBEDTLS_BIGNUM_C)71#include "mbedtls/bignum.h"72#endif7374#include "bignum_mod.h"7576/**77* \brief Perform a safe conditional copy of an MPI which doesn't reveal78* whether the assignment was done or not.79*80* The size to copy is determined by \p N.81*82* \param[out] X The address of the destination MPI.83* This must be initialized. Must have enough limbs to84* store the full value of \p A.85* \param[in] A The address of the source MPI. This must be initialized.86* \param[in] N The address of the modulus related to \p X and \p A.87* \param assign The condition deciding whether to perform the88* assignment or not. Must be either 0 or 1:89* * \c 1: Perform the assignment `X = A`.90* * \c 0: Keep the original value of \p X.91*92* \note This function avoids leaking any information about whether93* the assignment was done or not.94*95* \warning If \p assign is neither 0 nor 1, the result of this function96* is indeterminate, and the resulting value in \p X might be97* neither its original value nor the value in \p A.98*/99void mbedtls_mpi_mod_raw_cond_assign(mbedtls_mpi_uint *X,100const mbedtls_mpi_uint *A,101const mbedtls_mpi_mod_modulus *N,102unsigned char assign);103104/**105* \brief Perform a safe conditional swap of two MPIs which doesn't reveal106* whether the swap was done or not.107*108* The size to swap is determined by \p N.109*110* \param[in,out] X The address of the first MPI. This must be initialized.111* \param[in,out] Y The address of the second MPI. This must be initialized.112* \param[in] N The address of the modulus related to \p X and \p Y.113* \param swap The condition deciding whether to perform114* the swap or not. Must be either 0 or 1:115* * \c 1: Swap the values of \p X and \p Y.116* * \c 0: Keep the original values of \p X and \p Y.117*118* \note This function avoids leaking any information about whether119* the swap was done or not.120*121* \warning If \p swap is neither 0 nor 1, the result of this function122* is indeterminate, and both \p X and \p Y might end up with123* values different to either of the original ones.124*/125void mbedtls_mpi_mod_raw_cond_swap(mbedtls_mpi_uint *X,126mbedtls_mpi_uint *Y,127const mbedtls_mpi_mod_modulus *N,128unsigned char swap);129130/** Import X from unsigned binary data.131*132* The MPI needs to have enough limbs to store the full value (including any133* most significant zero bytes in the input).134*135* \param[out] X The address of the MPI. The size is determined by \p N.136* (In particular, it must have at least as many limbs as137* the modulus \p N.)138* \param[in] N The address of the modulus related to \p X.139* \param[in] input The input buffer to import from.140* \param input_length The length in bytes of \p input.141* \param ext_rep The endianness of the number in the input buffer.142*143* \return \c 0 if successful.144* \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p X isn't145* large enough to hold the value in \p input.146* \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if the external representation147* of \p N is invalid or \p X is not less than \p N.148*/149int mbedtls_mpi_mod_raw_read(mbedtls_mpi_uint *X,150const mbedtls_mpi_mod_modulus *N,151const unsigned char *input,152size_t input_length,153mbedtls_mpi_mod_ext_rep ext_rep);154155/** Export A into unsigned binary data.156*157* \param[in] A The address of the MPI. The size is determined by \p N.158* (In particular, it must have at least as many limbs as159* the modulus \p N.)160* \param[in] N The address of the modulus related to \p A.161* \param[out] output The output buffer to export to.162* \param output_length The length in bytes of \p output.163* \param ext_rep The endianness in which the number should be written into the output buffer.164*165* \return \c 0 if successful.166* \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p output isn't167* large enough to hold the value of \p A.168* \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if the external representation169* of \p N is invalid.170*/171int mbedtls_mpi_mod_raw_write(const mbedtls_mpi_uint *A,172const mbedtls_mpi_mod_modulus *N,173unsigned char *output,174size_t output_length,175mbedtls_mpi_mod_ext_rep ext_rep);176177/** \brief Subtract two MPIs, returning the residue modulo the specified178* modulus.179*180* The size of the operation is determined by \p N. \p A and \p B must have181* the same number of limbs as \p N.182*183* \p X may be aliased to \p A or \p B, or even both, but may not overlap184* either otherwise.185*186* \param[out] X The address of the result MPI.187* This must be initialized. Must have enough limbs to188* store the full value of the result.189* \param[in] A The address of the first MPI. This must be initialized.190* \param[in] B The address of the second MPI. This must be initialized.191* \param[in] N The address of the modulus. Used to perform a modulo192* operation on the result of the subtraction.193*/194void mbedtls_mpi_mod_raw_sub(mbedtls_mpi_uint *X,195const mbedtls_mpi_uint *A,196const mbedtls_mpi_uint *B,197const mbedtls_mpi_mod_modulus *N);198199/** \brief Multiply two MPIs, returning the residue modulo the specified200* modulus.201*202* \note Currently handles the case when `N->int_rep` is203* MBEDTLS_MPI_MOD_REP_MONTGOMERY.204*205* The size of the operation is determined by \p N. \p A, \p B and \p X must206* all be associated with the modulus \p N and must all have the same number207* of limbs as \p N.208*209* \p X may be aliased to \p A or \p B, or even both, but may not overlap210* either otherwise. They may not alias \p N (since they must be in canonical211* form, they cannot == \p N).212*213* \param[out] X The address of the result MPI. Must have the same214* number of limbs as \p N.215* On successful completion, \p X contains the result of216* the multiplication `A * B * R^-1` mod N where217* `R = 2^(biL * N->limbs)`.218* \param[in] A The address of the first MPI.219* \param[in] B The address of the second MPI.220* \param[in] N The address of the modulus. Used to perform a modulo221* operation on the result of the multiplication.222* \param[in,out] T Temporary storage of size at least 2 * N->limbs + 1223* limbs. Its initial content is unused and224* its final content is indeterminate.225* It must not alias or otherwise overlap any of the226* other parameters.227*/228void mbedtls_mpi_mod_raw_mul(mbedtls_mpi_uint *X,229const mbedtls_mpi_uint *A,230const mbedtls_mpi_uint *B,231const mbedtls_mpi_mod_modulus *N,232mbedtls_mpi_uint *T);233234/**235* \brief Returns the number of limbs of working memory required for236* a call to `mbedtls_mpi_mod_raw_inv_prime()`.237*238* \note This will always be at least239* `mbedtls_mpi_core_montmul_working_limbs(AN_limbs)`,240* i.e. sufficient for a call to `mbedtls_mpi_core_montmul()`.241*242* \param AN_limbs The number of limbs in the input `A` and the modulus `N`243* (they must be the same size) that will be given to244* `mbedtls_mpi_mod_raw_inv_prime()`.245*246* \return The number of limbs of working memory required by247* `mbedtls_mpi_mod_raw_inv_prime()`.248*/249size_t mbedtls_mpi_mod_raw_inv_prime_working_limbs(size_t AN_limbs);250251/**252* \brief Perform fixed-width modular inversion of a Montgomery-form MPI with253* respect to a modulus \p N that must be prime.254*255* \p X may be aliased to \p A, but not to \p N or \p RR.256*257* \param[out] X The modular inverse of \p A with respect to \p N.258* Will be in Montgomery form.259* \param[in] A The number to calculate the modular inverse of.260* Must be in Montgomery form. Must not be 0.261* \param[in] N The modulus, as a little-endian array of length \p AN_limbs.262* Must be prime.263* \param AN_limbs The number of limbs in \p A, \p N and \p RR.264* \param[in] RR The precomputed residue of 2^{2*biL} modulo N, as a little-265* endian array of length \p AN_limbs.266* \param[in,out] T Temporary storage of at least the number of limbs returned267* by `mbedtls_mpi_mod_raw_inv_prime_working_limbs()`.268* Its initial content is unused and its final content is269* indeterminate.270* It must not alias or otherwise overlap any of the other271* parameters.272* It is up to the caller to zeroize \p T when it is no273* longer needed, and before freeing it if it was dynamically274* allocated.275*/276void mbedtls_mpi_mod_raw_inv_prime(mbedtls_mpi_uint *X,277const mbedtls_mpi_uint *A,278const mbedtls_mpi_uint *N,279size_t AN_limbs,280const mbedtls_mpi_uint *RR,281mbedtls_mpi_uint *T);282283/**284* \brief Perform a known-size modular addition.285*286* Calculate `A + B modulo N`.287*288* The number of limbs in each operand, and the result, is given by the289* modulus \p N.290*291* \p X may be aliased to \p A or \p B, or even both, but may not overlap292* either otherwise.293*294* \param[out] X The result of the modular addition.295* \param[in] A Little-endian presentation of the left operand. This296* must be smaller than \p N.297* \param[in] B Little-endian presentation of the right operand. This298* must be smaller than \p N.299* \param[in] N The address of the modulus.300*/301void mbedtls_mpi_mod_raw_add(mbedtls_mpi_uint *X,302const mbedtls_mpi_uint *A,303const mbedtls_mpi_uint *B,304const mbedtls_mpi_mod_modulus *N);305306/** Convert an MPI from canonical representation (little-endian limb array)307* to the representation associated with the modulus.308*309* \param[in,out] X The limb array to convert.310* It must have as many limbs as \p N.311* It is converted in place.312* If this function returns an error, the content of \p X313* is unspecified.314* \param[in] N The modulus structure.315*316* \return \c 0 if successful.317* Otherwise an \c MBEDTLS_ERR_MPI_xxx error code.318*/319int mbedtls_mpi_mod_raw_canonical_to_modulus_rep(320mbedtls_mpi_uint *X,321const mbedtls_mpi_mod_modulus *N);322323/** Convert an MPI from the representation associated with the modulus324* to canonical representation (little-endian limb array).325*326* \param[in,out] X The limb array to convert.327* It must have as many limbs as \p N.328* It is converted in place.329* If this function returns an error, the content of \p X330* is unspecified.331* \param[in] N The modulus structure.332*333* \return \c 0 if successful.334* Otherwise an \c MBEDTLS_ERR_MPI_xxx error code.335*/336int mbedtls_mpi_mod_raw_modulus_to_canonical_rep(337mbedtls_mpi_uint *X,338const mbedtls_mpi_mod_modulus *N);339340/** Generate a random number uniformly in a range.341*342* This function generates a random number between \p min inclusive and343* \p N exclusive.344*345* The procedure complies with RFC 6979 §3.3 (deterministic ECDSA)346* when the RNG is a suitably parametrized instance of HMAC_DRBG347* and \p min is \c 1.348*349* \note There are `N - min` possible outputs. The lower bound350* \p min can be reached, but the upper bound \p N cannot.351*352* \param X The destination MPI, in canonical representation modulo \p N.353* It must not be aliased with \p N or otherwise overlap it.354* \param min The minimum value to return. It must be strictly smaller355* than \b N.356* \param N The modulus.357* This is the upper bound of the output range, exclusive.358* \param f_rng The RNG function to use. This must not be \c NULL.359* \param p_rng The RNG parameter to be passed to \p f_rng.360*361* \return \c 0 if successful.362* \return #MBEDTLS_ERR_MPI_NOT_ACCEPTABLE if the implementation was363* unable to find a suitable value within a limited number364* of attempts. This has a negligible probability if \p N365* is significantly larger than \p min, which is the case366* for all usual cryptographic applications.367*/368int mbedtls_mpi_mod_raw_random(mbedtls_mpi_uint *X,369mbedtls_mpi_uint min,370const mbedtls_mpi_mod_modulus *N,371int (*f_rng)(void *, unsigned char *, size_t),372void *p_rng);373374/** Convert an MPI into Montgomery form.375*376* \param X The address of the MPI.377* Must have the same number of limbs as \p N.378* \param N The address of the modulus, which gives the size of379* the base `R` = 2^(biL*N->limbs).380*381* \return \c 0 if successful.382*/383int mbedtls_mpi_mod_raw_to_mont_rep(mbedtls_mpi_uint *X,384const mbedtls_mpi_mod_modulus *N);385386/** Convert an MPI back from Montgomery representation.387*388* \param X The address of the MPI.389* Must have the same number of limbs as \p N.390* \param N The address of the modulus, which gives the size of391* the base `R`= 2^(biL*N->limbs).392*393* \return \c 0 if successful.394*/395int mbedtls_mpi_mod_raw_from_mont_rep(mbedtls_mpi_uint *X,396const mbedtls_mpi_mod_modulus *N);397398/** \brief Perform fixed width modular negation.399*400* The size of the operation is determined by \p N. \p A must have401* the same number of limbs as \p N.402*403* \p X may be aliased to \p A.404*405* \param[out] X The result of the modular negation.406* This must be initialized.407* \param[in] A Little-endian presentation of the input operand. This408* must be less than or equal to \p N.409* \param[in] N The modulus to use.410*/411void mbedtls_mpi_mod_raw_neg(mbedtls_mpi_uint *X,412const mbedtls_mpi_uint *A,413const mbedtls_mpi_mod_modulus *N);414415#endif /* MBEDTLS_BIGNUM_MOD_RAW_H */416417418