Path: blob/master/thirdparty/mbedtls/library/bignum_core.h
9898 views
/**1* Core bignum functions2*3* This interface should only be used by the legacy bignum module (bignum.h)4* and the modular bignum modules (bignum_mod.c, bignum_mod_raw.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 module is about processing non-negative integers with a fixed upper9* bound that's of the form 2^n-1 where n is a multiple of #biL.10* These can be thought of integers written in base 2^#biL with a fixed11* number of digits. Digits in this base are called *limbs*.12* Many operations treat these numbers as the principal representation of13* a number modulo 2^n or a smaller bound.14*15* The functions in this module obey the following conventions unless16* explicitly indicated otherwise:17*18* - **Overflow**: some functions indicate overflow from the range19* [0, 2^n-1] by returning carry parameters, while others operate20* modulo and so cannot overflow. This should be clear from the function21* documentation.22* - **Bignum parameters**: Bignums are passed as pointers to an array of23* limbs. A limb has the type #mbedtls_mpi_uint. Unless otherwise specified:24* - Bignum parameters called \p A, \p B, ... are inputs, and are25* not modified by the function.26* - For operations modulo some number, the modulus is called \p N27* and is input-only.28* - Bignum parameters called \p X, \p Y are outputs or input-output.29* The initial content of output-only parameters is ignored.30* - Some functions use different names that reflect traditional31* naming of operands of certain operations (e.g.32* divisor/dividend/quotient/remainder).33* - \p T is a temporary storage area. The initial content of such34* parameter is ignored and the final content is unspecified.35* - **Bignum sizes**: bignum sizes are always expressed in limbs.36* Most functions work on bignums of a given size and take a single37* \p limbs parameter that applies to all parameters that are limb arrays.38* All bignum sizes must be at least 1 and must be significantly less than39* #SIZE_MAX. The behavior if a size is 0 is undefined. The behavior if the40* total size of all parameters overflows #SIZE_MAX is undefined.41* - **Parameter ordering**: for bignum parameters, outputs come before inputs.42* Temporaries come last.43* - **Aliasing**: in general, output bignums may be aliased to one or more44* inputs. As an exception, parameters that are documented as a modulus value45* may not be aliased to an output. Outputs may not be aliased to one another.46* Temporaries may not be aliased to any other parameter.47* - **Overlap**: apart from aliasing of limb array pointers (where two48* arguments are equal pointers), overlap is not supported and may result49* in undefined behavior.50* - **Error handling**: This is a low-level module. Functions generally do not51* try to protect against invalid arguments such as nonsensical sizes or52* null pointers. Note that some functions that operate on bignums of53* different sizes have constraints about their size, and violating those54* constraints may lead to buffer overflows.55* - **Modular representatives**: functions that operate modulo \p N expect56* all modular inputs to be in the range [0, \p N - 1] and guarantee outputs57* in the range [0, \p N - 1]. If an input is out of range, outputs are58* fully unspecified, though bignum values out of range should not cause59* buffer overflows (beware that this is not extensively tested).60*/6162/*63* Copyright The Mbed TLS Contributors64* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later65*/6667#ifndef MBEDTLS_BIGNUM_CORE_H68#define MBEDTLS_BIGNUM_CORE_H6970#include "common.h"7172#include "mbedtls/bignum.h"7374#include "constant_time_internal.h"7576#define ciL (sizeof(mbedtls_mpi_uint)) /** chars in limb */77#define biL (ciL << 3) /** bits in limb */78#define biH (ciL << 2) /** half limb size */7980/*81* Convert between bits/chars and number of limbs82* Divide first in order to avoid potential overflows83*/84#define BITS_TO_LIMBS(i) ((i) / biL + ((i) % biL != 0))85#define CHARS_TO_LIMBS(i) ((i) / ciL + ((i) % ciL != 0))86/* Get a specific byte, without range checks. */87#define GET_BYTE(X, i) \88(((X)[(i) / ciL] >> (((i) % ciL) * 8)) & 0xff)8990/* Constants to identify whether a value is public or secret. If a parameter is marked as secret by91* this constant, the function must be constant time with respect to the parameter.92*93* This is only needed for functions with the _optionally_safe postfix. All other functions have94* fixed behavior that can't be changed at runtime and are constant time with respect to their95* parameters as prescribed by their documentation or by conventions in their module's documentation.96*97* Parameters should be named X_public where X is the name of the98* corresponding input parameter.99*100* Implementation should always check using101* if (X_public == MBEDTLS_MPI_IS_PUBLIC) {102* // unsafe path103* } else {104* // safe path105* }106* not the other way round, in order to prevent misuse. (That is, if a value107* other than the two below is passed, default to the safe path.)108*109* The value of MBEDTLS_MPI_IS_PUBLIC is chosen in a way that is unlikely to happen by accident, but110* which can be used as an immediate value in a Thumb2 comparison (for code size). */111#define MBEDTLS_MPI_IS_PUBLIC 0x2a2a2a2a112#define MBEDTLS_MPI_IS_SECRET 0113#if defined(MBEDTLS_TEST_HOOKS) && !defined(MBEDTLS_THREADING_C)114// Default value for testing that is neither MBEDTLS_MPI_IS_PUBLIC nor MBEDTLS_MPI_IS_SECRET115#define MBEDTLS_MPI_IS_TEST 1116#endif117118/** Count leading zero bits in a given integer.119*120* \warning The result is undefined if \p a == 0121*122* \param a Integer to count leading zero bits.123*124* \return The number of leading zero bits in \p a, if \p a != 0.125* If \p a == 0, the result is undefined.126*/127size_t mbedtls_mpi_core_clz(mbedtls_mpi_uint a);128129/** Return the minimum number of bits required to represent the value held130* in the MPI.131*132* \note This function returns 0 if all the limbs of \p A are 0.133*134* \param[in] A The address of the MPI.135* \param A_limbs The number of limbs of \p A.136*137* \return The number of bits in \p A.138*/139size_t mbedtls_mpi_core_bitlen(const mbedtls_mpi_uint *A, size_t A_limbs);140141/** Convert a big-endian byte array aligned to the size of mbedtls_mpi_uint142* into the storage form used by mbedtls_mpi.143*144* \param[in,out] A The address of the MPI.145* \param A_limbs The number of limbs of \p A.146*/147void mbedtls_mpi_core_bigendian_to_host(mbedtls_mpi_uint *A,148size_t A_limbs);149150/** \brief Compare a machine integer with an MPI.151*152* This function operates in constant time with respect153* to the values of \p min and \p A.154*155* \param min A machine integer.156* \param[in] A An MPI.157* \param A_limbs The number of limbs of \p A.158* This must be at least 1.159*160* \return MBEDTLS_CT_TRUE if \p min is less than or equal to \p A, otherwise MBEDTLS_CT_FALSE.161*/162mbedtls_ct_condition_t mbedtls_mpi_core_uint_le_mpi(mbedtls_mpi_uint min,163const mbedtls_mpi_uint *A,164size_t A_limbs);165166/**167* \brief Check if one unsigned MPI is less than another in constant168* time.169*170* \param A The left-hand MPI. This must point to an array of limbs171* with the same allocated length as \p B.172* \param B The right-hand MPI. This must point to an array of limbs173* with the same allocated length as \p A.174* \param limbs The number of limbs in \p A and \p B.175* This must not be 0.176*177* \return MBEDTLS_CT_TRUE if \p A is less than \p B.178* MBEDTLS_CT_FALSE if \p A is greater than or equal to \p B.179*/180mbedtls_ct_condition_t mbedtls_mpi_core_lt_ct(const mbedtls_mpi_uint *A,181const mbedtls_mpi_uint *B,182size_t limbs);183184/**185* \brief Perform a safe conditional copy of an MPI which doesn't reveal186* whether assignment was done or not.187*188* \param[out] X The address of the destination MPI.189* This must be initialized. Must have enough limbs to190* store the full value of \p A.191* \param[in] A The address of the source MPI. This must be initialized.192* \param limbs The number of limbs of \p A.193* \param assign The condition deciding whether to perform the194* assignment or not. Callers will need to use195* the constant time interface (e.g. `mbedtls_ct_bool()`)196* to construct this argument.197*198* \note This function avoids leaking any information about whether199* the assignment was done or not.200*/201void mbedtls_mpi_core_cond_assign(mbedtls_mpi_uint *X,202const mbedtls_mpi_uint *A,203size_t limbs,204mbedtls_ct_condition_t assign);205206/**207* \brief Perform a safe conditional swap of two MPIs which doesn't reveal208* whether the swap was done or not.209*210* \param[in,out] X The address of the first MPI.211* This must be initialized.212* \param[in,out] Y The address of the second MPI.213* This must be initialized.214* \param limbs The number of limbs of \p X and \p Y.215* \param swap The condition deciding whether to perform216* the swap or not.217*218* \note This function avoids leaking any information about whether219* the swap was done or not.220*/221void mbedtls_mpi_core_cond_swap(mbedtls_mpi_uint *X,222mbedtls_mpi_uint *Y,223size_t limbs,224mbedtls_ct_condition_t swap);225226/** Import X from unsigned binary data, little-endian.227*228* The MPI needs to have enough limbs to store the full value (including any229* most significant zero bytes in the input).230*231* \param[out] X The address of the MPI.232* \param X_limbs The number of limbs of \p X.233* \param[in] input The input buffer to import from.234* \param input_length The length bytes of \p input.235*236* \return \c 0 if successful.237* \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p X isn't238* large enough to hold the value in \p input.239*/240int mbedtls_mpi_core_read_le(mbedtls_mpi_uint *X,241size_t X_limbs,242const unsigned char *input,243size_t input_length);244245/** Import X from unsigned binary data, big-endian.246*247* The MPI needs to have enough limbs to store the full value (including any248* most significant zero bytes in the input).249*250* \param[out] X The address of the MPI.251* May only be #NULL if \p X_limbs is 0 and \p input_length252* is 0.253* \param X_limbs The number of limbs of \p X.254* \param[in] input The input buffer to import from.255* May only be #NULL if \p input_length is 0.256* \param input_length The length in bytes of \p input.257*258* \return \c 0 if successful.259* \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p X isn't260* large enough to hold the value in \p input.261*/262int mbedtls_mpi_core_read_be(mbedtls_mpi_uint *X,263size_t X_limbs,264const unsigned char *input,265size_t input_length);266267/** Export A into unsigned binary data, little-endian.268*269* \note If \p output is shorter than \p A the export is still successful if the270* value held in \p A fits in the buffer (that is, if enough of the most271* significant bytes of \p A are 0).272*273* \param[in] A The address of the MPI.274* \param A_limbs The number of limbs of \p A.275* \param[out] output The output buffer to export to.276* \param output_length The length in bytes of \p output.277*278* \return \c 0 if successful.279* \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p output isn't280* large enough to hold the value of \p A.281*/282int mbedtls_mpi_core_write_le(const mbedtls_mpi_uint *A,283size_t A_limbs,284unsigned char *output,285size_t output_length);286287/** Export A into unsigned binary data, big-endian.288*289* \note If \p output is shorter than \p A the export is still successful if the290* value held in \p A fits in the buffer (that is, if enough of the most291* significant bytes of \p A are 0).292*293* \param[in] A The address of the MPI.294* \param A_limbs The number of limbs of \p A.295* \param[out] output The output buffer to export to.296* \param output_length The length in bytes of \p output.297*298* \return \c 0 if successful.299* \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p output isn't300* large enough to hold the value of \p A.301*/302int mbedtls_mpi_core_write_be(const mbedtls_mpi_uint *A,303size_t A_limbs,304unsigned char *output,305size_t output_length);306307/** \brief Shift an MPI in-place right by a number of bits.308*309* Shifting by more bits than there are bit positions310* in \p X is valid and results in setting \p X to 0.311*312* This function's execution time depends on the value313* of \p count (and of course \p limbs).314*315* \param[in,out] X The number to shift.316* \param limbs The number of limbs of \p X. This must be at least 1.317* \param count The number of bits to shift by.318*/319void mbedtls_mpi_core_shift_r(mbedtls_mpi_uint *X, size_t limbs,320size_t count);321322/**323* \brief Shift an MPI in-place left by a number of bits.324*325* Shifting by more bits than there are bit positions326* in \p X will produce an unspecified result.327*328* This function's execution time depends on the value329* of \p count (and of course \p limbs).330* \param[in,out] X The number to shift.331* \param limbs The number of limbs of \p X. This must be at least 1.332* \param count The number of bits to shift by.333*/334void mbedtls_mpi_core_shift_l(mbedtls_mpi_uint *X, size_t limbs,335size_t count);336337/**338* \brief Add two fixed-size large unsigned integers, returning the carry.339*340* Calculates `A + B` where `A` and `B` have the same size.341*342* This function operates modulo `2^(biL*limbs)` and returns the carry343* (1 if there was a wraparound, and 0 otherwise).344*345* \p X may be aliased to \p A or \p B.346*347* \param[out] X The result of the addition.348* \param[in] A Little-endian presentation of the left operand.349* \param[in] B Little-endian presentation of the right operand.350* \param limbs Number of limbs of \p X, \p A and \p B.351*352* \return 1 if `A + B >= 2^(biL*limbs)`, 0 otherwise.353*/354mbedtls_mpi_uint mbedtls_mpi_core_add(mbedtls_mpi_uint *X,355const mbedtls_mpi_uint *A,356const mbedtls_mpi_uint *B,357size_t limbs);358359/**360* \brief Conditional addition of two fixed-size large unsigned integers,361* returning the carry.362*363* Functionally equivalent to364*365* ```366* if( cond )367* X += A;368* return carry;369* ```370*371* This function operates modulo `2^(biL*limbs)`.372*373* \param[in,out] X The pointer to the (little-endian) array374* representing the bignum to accumulate onto.375* \param[in] A The pointer to the (little-endian) array376* representing the bignum to conditionally add377* to \p X. This may be aliased to \p X but may not378* overlap otherwise.379* \param limbs Number of limbs of \p X and \p A.380* \param cond Condition bit dictating whether addition should381* happen or not. This must be \c 0 or \c 1.382*383* \warning If \p cond is neither 0 nor 1, the result of this function384* is unspecified, and the resulting value in \p X might be385* neither its original value nor \p X + \p A.386*387* \return 1 if `X + cond * A >= 2^(biL*limbs)`, 0 otherwise.388*/389mbedtls_mpi_uint mbedtls_mpi_core_add_if(mbedtls_mpi_uint *X,390const mbedtls_mpi_uint *A,391size_t limbs,392unsigned cond);393394/**395* \brief Subtract two fixed-size large unsigned integers, returning the borrow.396*397* Calculate `A - B` where \p A and \p B have the same size.398* This function operates modulo `2^(biL*limbs)` and returns the carry399* (1 if there was a wraparound, i.e. if `A < B`, and 0 otherwise).400*401* \p X may be aliased to \p A or \p B, or even both, but may not overlap402* either otherwise.403*404* \param[out] X The result of the subtraction.405* \param[in] A Little-endian presentation of left operand.406* \param[in] B Little-endian presentation of right operand.407* \param limbs Number of limbs of \p X, \p A and \p B.408*409* \return 1 if `A < B`.410* 0 if `A >= B`.411*/412mbedtls_mpi_uint mbedtls_mpi_core_sub(mbedtls_mpi_uint *X,413const mbedtls_mpi_uint *A,414const mbedtls_mpi_uint *B,415size_t limbs);416417/**418* \brief Perform a fixed-size multiply accumulate operation: X += b * A419*420* \p X may be aliased to \p A (when \p X_limbs == \p A_limbs), but may not421* otherwise overlap.422*423* This function operates modulo `2^(biL*X_limbs)`.424*425* \param[in,out] X The pointer to the (little-endian) array426* representing the bignum to accumulate onto.427* \param X_limbs The number of limbs of \p X. This must be428* at least \p A_limbs.429* \param[in] A The pointer to the (little-endian) array430* representing the bignum to multiply with.431* This may be aliased to \p X but may not overlap432* otherwise.433* \param A_limbs The number of limbs of \p A.434* \param b X scalar to multiply with.435*436* \return The carry at the end of the operation.437*/438mbedtls_mpi_uint mbedtls_mpi_core_mla(mbedtls_mpi_uint *X, size_t X_limbs,439const mbedtls_mpi_uint *A, size_t A_limbs,440mbedtls_mpi_uint b);441442/**443* \brief Perform a known-size multiplication444*445* \p X may not be aliased to any of the inputs for this function.446* \p A may be aliased to \p B.447*448* \param[out] X The pointer to the (little-endian) array to receive449* the product of \p A_limbs and \p B_limbs.450* This must be of length \p A_limbs + \p B_limbs.451* \param[in] A The pointer to the (little-endian) array452* representing the first factor.453* \param A_limbs The number of limbs in \p A.454* \param[in] B The pointer to the (little-endian) array455* representing the second factor.456* \param B_limbs The number of limbs in \p B.457*/458void mbedtls_mpi_core_mul(mbedtls_mpi_uint *X,459const mbedtls_mpi_uint *A, size_t A_limbs,460const mbedtls_mpi_uint *B, size_t B_limbs);461462/**463* \brief Calculate initialisation value for fast Montgomery modular464* multiplication465*466* \param[in] N Little-endian presentation of the modulus. This must have467* at least one limb.468*469* \return The initialisation value for fast Montgomery modular multiplication470*/471mbedtls_mpi_uint mbedtls_mpi_core_montmul_init(const mbedtls_mpi_uint *N);472473/**474* \brief Montgomery multiplication: X = A * B * R^-1 mod N (HAC 14.36)475*476* \p A and \p B must be in canonical form. That is, < \p N.477*478* \p X may be aliased to \p A or \p N, or even \p B (if \p AN_limbs ==479* \p B_limbs) but may not overlap any parameters otherwise.480*481* \p A and \p B may alias each other, if \p AN_limbs == \p B_limbs. They may482* not alias \p N (since they must be in canonical form, they cannot == \p N).483*484* \param[out] X The destination MPI, as a little-endian array of485* length \p AN_limbs.486* On successful completion, X contains the result of487* the multiplication `A * B * R^-1` mod N where488* `R = 2^(biL*AN_limbs)`.489* \param[in] A Little-endian presentation of first operand.490* Must have the same number of limbs as \p N.491* \param[in] B Little-endian presentation of second operand.492* \param[in] B_limbs The number of limbs in \p B.493* Must be <= \p AN_limbs.494* \param[in] N Little-endian presentation of the modulus.495* This must be odd, and have exactly the same number496* of limbs as \p A.497* It may alias \p X, but must not alias or otherwise498* overlap any of the other parameters.499* \param[in] AN_limbs The number of limbs in \p X, \p A and \p N.500* \param mm The Montgomery constant for \p N: -N^-1 mod 2^biL.501* This can be calculated by `mbedtls_mpi_core_montmul_init()`.502* \param[in,out] T Temporary storage of size at least 2*AN_limbs+1 limbs.503* Its initial content is unused and504* its final content is indeterminate.505* It must not alias or otherwise overlap any of the506* other parameters.507*/508void mbedtls_mpi_core_montmul(mbedtls_mpi_uint *X,509const mbedtls_mpi_uint *A,510const mbedtls_mpi_uint *B, size_t B_limbs,511const mbedtls_mpi_uint *N, size_t AN_limbs,512mbedtls_mpi_uint mm, mbedtls_mpi_uint *T);513514/**515* \brief Calculate the square of the Montgomery constant. (Needed516* for conversion and operations in Montgomery form.)517*518* \param[out] X A pointer to the result of the calculation of519* the square of the Montgomery constant:520* 2^{2*n*biL} mod N.521* \param[in] N Little-endian presentation of the modulus, which must be odd.522*523* \return 0 if successful.524* \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if there is not enough space525* to store the value of Montgomery constant squared.526* \return #MBEDTLS_ERR_MPI_DIVISION_BY_ZERO if \p N modulus is zero.527* \return #MBEDTLS_ERR_MPI_NEGATIVE_VALUE if \p N modulus is negative.528*/529int mbedtls_mpi_core_get_mont_r2_unsafe(mbedtls_mpi *X,530const mbedtls_mpi *N);531532#if defined(MBEDTLS_TEST_HOOKS)533/**534* Copy an MPI from a table without leaking the index.535*536* \param dest The destination buffer. This must point to a writable537* buffer of at least \p limbs limbs.538* \param table The address of the table. This must point to a readable539* array of \p count elements of \p limbs limbs each.540* \param limbs The number of limbs in each table entry.541* \param count The number of entries in \p table.542* \param index The (secret) table index to look up. This must be in the543* range `0 .. count-1`.544*/545void mbedtls_mpi_core_ct_uint_table_lookup(mbedtls_mpi_uint *dest,546const mbedtls_mpi_uint *table,547size_t limbs,548size_t count,549size_t index);550#endif /* MBEDTLS_TEST_HOOKS */551552/**553* \brief Fill an integer with a number of random bytes.554*555* \param X The destination MPI.556* \param X_limbs The number of limbs of \p X.557* \param bytes The number of random bytes to generate.558* \param f_rng The RNG function to use. This must not be \c NULL.559* \param p_rng The RNG parameter to be passed to \p f_rng. This may be560* \c NULL if \p f_rng doesn't need a context argument.561*562* \return \c 0 if successful.563* \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if \p X does not have564* enough room for \p bytes bytes.565* \return A negative error code on RNG failure.566*567* \note The bytes obtained from the RNG are interpreted568* as a big-endian representation of an MPI; this can569* be relevant in applications like deterministic ECDSA.570*/571int mbedtls_mpi_core_fill_random(mbedtls_mpi_uint *X, size_t X_limbs,572size_t bytes,573int (*f_rng)(void *, unsigned char *, size_t),574void *p_rng);575576/** Generate a random number uniformly in a range.577*578* This function generates a random number between \p min inclusive and579* \p N exclusive.580*581* The procedure complies with RFC 6979 ยง3.3 (deterministic ECDSA)582* when the RNG is a suitably parametrized instance of HMAC_DRBG583* and \p min is \c 1.584*585* \note There are `N - min` possible outputs. The lower bound586* \p min can be reached, but the upper bound \p N cannot.587*588* \param X The destination MPI, with \p limbs limbs.589* It must not be aliased with \p N or otherwise overlap it.590* \param min The minimum value to return.591* \param N The upper bound of the range, exclusive, with \p limbs limbs.592* In other words, this is one plus the maximum value to return.593* \p N must be strictly larger than \p min.594* \param limbs The number of limbs of \p N and \p X.595* This must not be 0.596* \param f_rng The RNG function to use. This must not be \c NULL.597* \param p_rng The RNG parameter to be passed to \p f_rng.598*599* \return \c 0 if successful.600* \return #MBEDTLS_ERR_MPI_NOT_ACCEPTABLE if the implementation was601* unable to find a suitable value within a limited number602* of attempts. This has a negligible probability if \p N603* is significantly larger than \p min, which is the case604* for all usual cryptographic applications.605*/606int mbedtls_mpi_core_random(mbedtls_mpi_uint *X,607mbedtls_mpi_uint min,608const mbedtls_mpi_uint *N,609size_t limbs,610int (*f_rng)(void *, unsigned char *, size_t),611void *p_rng);612613/**614* \brief Returns the number of limbs of working memory required for615* a call to `mbedtls_mpi_core_exp_mod()`.616*617* \note This will always be at least618* `mbedtls_mpi_core_montmul_working_limbs(AN_limbs)`,619* i.e. sufficient for a call to `mbedtls_mpi_core_montmul()`.620*621* \param AN_limbs The number of limbs in the input `A` and the modulus `N`622* (they must be the same size) that will be given to623* `mbedtls_mpi_core_exp_mod()`.624* \param E_limbs The number of limbs in the exponent `E` that will be given625* to `mbedtls_mpi_core_exp_mod()`.626*627* \return The number of limbs of working memory required by628* `mbedtls_mpi_core_exp_mod()`.629*/630size_t mbedtls_mpi_core_exp_mod_working_limbs(size_t AN_limbs, size_t E_limbs);631632/**633* \brief Perform a modular exponentiation with public or secret exponent:634* X = A^E mod N, where \p A is already in Montgomery form.635*636* \warning This function is not constant time with respect to \p E (the exponent).637*638* \p X may be aliased to \p A, but not to \p RR or \p E, even if \p E_limbs ==639* \p AN_limbs.640*641* \param[out] X The destination MPI, as a little endian array of length642* \p AN_limbs.643* \param[in] A The base MPI, as a little endian array of length \p AN_limbs.644* Must be in Montgomery form.645* \param[in] N The modulus, as a little endian array of length \p AN_limbs.646* \param AN_limbs The number of limbs in \p X, \p A, \p N, \p RR.647* \param[in] E The exponent, as a little endian array of length \p E_limbs.648* \param E_limbs The number of limbs in \p E.649* \param[in] RR The precomputed residue of 2^{2*biL} modulo N, as a little650* endian array of length \p AN_limbs.651* \param[in,out] T Temporary storage of at least the number of limbs returned652* by `mbedtls_mpi_core_exp_mod_working_limbs()`.653* Its initial content is unused and its final content is654* indeterminate.655* It must not alias or otherwise overlap any of the other656* parameters.657* It is up to the caller to zeroize \p T when it is no658* longer needed, and before freeing it if it was dynamically659* allocated.660*/661void mbedtls_mpi_core_exp_mod_unsafe(mbedtls_mpi_uint *X,662const mbedtls_mpi_uint *A,663const mbedtls_mpi_uint *N, size_t AN_limbs,664const mbedtls_mpi_uint *E, size_t E_limbs,665const mbedtls_mpi_uint *RR,666mbedtls_mpi_uint *T);667668/**669* \brief Perform a modular exponentiation with secret exponent:670* X = A^E mod N, where \p A is already in Montgomery form.671*672* \p X may be aliased to \p A, but not to \p RR or \p E, even if \p E_limbs ==673* \p AN_limbs.674*675* \param[out] X The destination MPI, as a little endian array of length676* \p AN_limbs.677* \param[in] A The base MPI, as a little endian array of length \p AN_limbs.678* Must be in Montgomery form.679* \param[in] N The modulus, as a little endian array of length \p AN_limbs.680* \param AN_limbs The number of limbs in \p X, \p A, \p N, \p RR.681* \param[in] E The exponent, as a little endian array of length \p E_limbs.682* \param E_limbs The number of limbs in \p E.683* \param[in] RR The precomputed residue of 2^{2*biL} modulo N, as a little684* endian array of length \p AN_limbs.685* \param[in,out] T Temporary storage of at least the number of limbs returned686* by `mbedtls_mpi_core_exp_mod_working_limbs()`.687* Its initial content is unused and its final content is688* indeterminate.689* It must not alias or otherwise overlap any of the other690* parameters.691* It is up to the caller to zeroize \p T when it is no692* longer needed, and before freeing it if it was dynamically693* allocated.694*/695void mbedtls_mpi_core_exp_mod(mbedtls_mpi_uint *X,696const mbedtls_mpi_uint *A,697const mbedtls_mpi_uint *N, size_t AN_limbs,698const mbedtls_mpi_uint *E, size_t E_limbs,699const mbedtls_mpi_uint *RR,700mbedtls_mpi_uint *T);701702/**703* \brief Subtract unsigned integer from known-size large unsigned integers.704* Return the borrow.705*706* \param[out] X The result of the subtraction.707* \param[in] A The left operand.708* \param b The unsigned scalar to subtract.709* \param limbs Number of limbs of \p X and \p A.710*711* \return 1 if `A < b`.712* 0 if `A >= b`.713*/714mbedtls_mpi_uint mbedtls_mpi_core_sub_int(mbedtls_mpi_uint *X,715const mbedtls_mpi_uint *A,716mbedtls_mpi_uint b,717size_t limbs);718719/**720* \brief Determine if a given MPI has the value \c 0 in constant time with721* respect to the value (but not with respect to the number of limbs).722*723* \param[in] A The MPI to test.724* \param limbs Number of limbs in \p A.725*726* \return MBEDTLS_CT_FALSE if `A == 0`727* MBEDTLS_CT_TRUE if `A != 0`.728*/729mbedtls_ct_condition_t mbedtls_mpi_core_check_zero_ct(const mbedtls_mpi_uint *A,730size_t limbs);731732/**733* \brief Returns the number of limbs of working memory required for734* a call to `mbedtls_mpi_core_montmul()`.735*736* \param AN_limbs The number of limbs in the input `A` and the modulus `N`737* (they must be the same size) that will be given to738* `mbedtls_mpi_core_montmul()` or one of the other functions739* that specifies this as the amount of working memory needed.740*741* \return The number of limbs of working memory required by742* `mbedtls_mpi_core_montmul()` (or other similar function).743*/744static inline size_t mbedtls_mpi_core_montmul_working_limbs(size_t AN_limbs)745{746return 2 * AN_limbs + 1;747}748749/** Convert an MPI into Montgomery form.750*751* \p X may be aliased to \p A, but may not otherwise overlap it.752*753* \p X may not alias \p N (it is in canonical form, so must be strictly less754* than \p N). Nor may it alias or overlap \p rr (this is unlikely to be755* required in practice.)756*757* This function is a thin wrapper around `mbedtls_mpi_core_montmul()` that is758* an alternative to calling `mbedtls_mpi_mod_raw_to_mont_rep()` when we759* don't want to allocate memory.760*761* \param[out] X The result of the conversion.762* Must have the same number of limbs as \p A.763* \param[in] A The MPI to convert into Montgomery form.764* Must have the same number of limbs as the modulus.765* \param[in] N The address of the modulus, which gives the size of766* the base `R` = 2^(biL*N->limbs).767* \param[in] AN_limbs The number of limbs in \p X, \p A, \p N and \p rr.768* \param mm The Montgomery constant for \p N: -N^-1 mod 2^biL.769* This can be determined by calling770* `mbedtls_mpi_core_montmul_init()`.771* \param[in] rr The residue for `2^{2*n*biL} mod N`.772* \param[in,out] T Temporary storage of size at least773* `mbedtls_mpi_core_montmul_working_limbs(AN_limbs)`774* limbs.775* Its initial content is unused and776* its final content is indeterminate.777* It must not alias or otherwise overlap any of the778* other parameters.779*/780void mbedtls_mpi_core_to_mont_rep(mbedtls_mpi_uint *X,781const mbedtls_mpi_uint *A,782const mbedtls_mpi_uint *N,783size_t AN_limbs,784mbedtls_mpi_uint mm,785const mbedtls_mpi_uint *rr,786mbedtls_mpi_uint *T);787788/** Convert an MPI from Montgomery form.789*790* \p X may be aliased to \p A, but may not otherwise overlap it.791*792* \p X may not alias \p N (it is in canonical form, so must be strictly less793* than \p N).794*795* This function is a thin wrapper around `mbedtls_mpi_core_montmul()` that is796* an alternative to calling `mbedtls_mpi_mod_raw_from_mont_rep()` when we797* don't want to allocate memory.798*799* \param[out] X The result of the conversion.800* Must have the same number of limbs as \p A.801* \param[in] A The MPI to convert from Montgomery form.802* Must have the same number of limbs as the modulus.803* \param[in] N The address of the modulus, which gives the size of804* the base `R` = 2^(biL*N->limbs).805* \param[in] AN_limbs The number of limbs in \p X, \p A and \p N.806* \param mm The Montgomery constant for \p N: -N^-1 mod 2^biL.807* This can be determined by calling808* `mbedtls_mpi_core_montmul_init()`.809* \param[in,out] T Temporary storage of size at least810* `mbedtls_mpi_core_montmul_working_limbs(AN_limbs)`811* limbs.812* Its initial content is unused and813* its final content is indeterminate.814* It must not alias or otherwise overlap any of the815* other parameters.816*/817void mbedtls_mpi_core_from_mont_rep(mbedtls_mpi_uint *X,818const mbedtls_mpi_uint *A,819const mbedtls_mpi_uint *N,820size_t AN_limbs,821mbedtls_mpi_uint mm,822mbedtls_mpi_uint *T);823824#endif /* MBEDTLS_BIGNUM_CORE_H */825826827