Path: blob/master/thirdparty/mbedtls/library/bignum_internal.h
21827 views
/**1* \file bignum_internal.h2*3* \brief Internal-only bignum public-key cryptosystem API.4*5* This file declares bignum-related functions that are to be used6* only from within the Mbed TLS library itself.7*8*/9/*10* Copyright The Mbed TLS Contributors11* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later12*/13#ifndef MBEDTLS_BIGNUM_INTERNAL_H14#define MBEDTLS_BIGNUM_INTERNAL_H1516/**17* \brief Perform a modular exponentiation: X = A^E mod N18*19* \warning This function is not constant time with respect to \p E (the exponent).20*21* \param X The destination MPI. This must point to an initialized MPI.22* This must not alias E or N.23* \param A The base of the exponentiation.24* This must point to an initialized MPI.25* \param E The exponent MPI. This must point to an initialized MPI.26* \param N The base for the modular reduction. This must point to an27* initialized MPI.28* \param prec_RR A helper MPI depending solely on \p N which can be used to29* speed-up multiple modular exponentiations for the same value30* of \p N. This may be \c NULL. If it is not \c NULL, it must31* point to an initialized MPI. If it hasn't been used after32* the call to mbedtls_mpi_init(), this function will compute33* the helper value and store it in \p prec_RR for reuse on34* subsequent calls to this function. Otherwise, the function35* will assume that \p prec_RR holds the helper value set by a36* previous call to mbedtls_mpi_exp_mod(), and reuse it.37*38* \return \c 0 if successful.39* \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.40* \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if \c N is negative or41* even, or if \c E is negative.42* \return Another negative error code on different kinds of failures.43*44*/45int mbedtls_mpi_exp_mod_unsafe(mbedtls_mpi *X, const mbedtls_mpi *A,46const mbedtls_mpi *E, const mbedtls_mpi *N,47mbedtls_mpi *prec_RR);4849/**50* \brief A wrapper around a constant time function to compute51* GCD(A, N) and/or A^-1 mod N if it exists.52*53* \warning Requires N to be odd, and 0 <= A <= N. Additionally, if54* I != NULL, requires N > 1.55* The wrapper part of this function is not constant time.56*57* \note A and N must not alias each other.58* When I == NULL (computing only the GCD), G can alias A or N.59* When I != NULL (computing the modular inverse), G or I can60* alias A, but neither of them can alias N (the modulus).61*62* \param[out] G The GCD of \p A and \p N.63* This may be NULL, to only compute I.64* \param[out] I The inverse of \p A modulo \p N if it exists (that is,65* if \p G above is 1 on exit), in the range [1, \p N);66* indeterminate otherwise.67* This may be NULL, to only compute G.68* \param[in] A The 1st operand of GCD and number to invert.69* This value must be less than or equal to \p N.70* \param[in] N The 2nd operand of GCD and modulus for inversion.71* Must be odd or the results are indeterminate.72*73* \return \c 0 if successful.74* \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.75* \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if preconditions were not76* met.77*/78int mbedtls_mpi_gcd_modinv_odd(mbedtls_mpi *G,79mbedtls_mpi *I,80const mbedtls_mpi *A,81const mbedtls_mpi *N);8283/**84* \brief Modular inverse: X = A^-1 mod N with N odd85*86* \param[out] X The inverse of \p A modulo \p N in the range [1, \p N)87* on success; indeterminate otherwise.88* \param[in] A The number to invert.89* \param[in] N The modulus. Must be odd and greater than 1.90*91* \return \c 0 if successful.92* \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.93* \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if preconditions were not94* met.95* \return #MBEDTLS_ERR_MPI_NOT_ACCEPTABLE if A is not invertible mod N.96*/97int mbedtls_mpi_inv_mod_odd(mbedtls_mpi *X,98const mbedtls_mpi *A,99const mbedtls_mpi *N);100101/**102* \brief Modular inverse: X = A^-1 mod N with N even,103* A odd and 1 < A < N.104*105* \param[out] X The inverse of \p A modulo \p N in the range [1, \p N)106* on success; indeterminate otherwise.107* \param[in] A The number to invert. Must be odd, greated than 1108* and less than \p N.109* \param[in] N The modulus. Must be even and greater than 1.110*111* \return \c 0 if successful.112* \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.113* \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if preconditions were not114* met.115* \return #MBEDTLS_ERR_MPI_NOT_ACCEPTABLE if A is not invertible mod N.116*/117int mbedtls_mpi_inv_mod_even_in_range(mbedtls_mpi *X,118mbedtls_mpi const *A,119mbedtls_mpi const *N);120121#endif /* bignum_internal.h */122123124