2022-07-18 23:13:13 +02:00
|
|
|
/**
|
2022-08-19 13:09:17 +02:00
|
|
|
* Low-level modular bignum functions
|
2022-08-11 18:42:59 +02:00
|
|
|
*
|
2022-08-22 10:06:32 +02:00
|
|
|
* This interface should only be used by the higher-level modular bignum
|
2022-08-11 18:42:59 +02:00
|
|
|
* module (bignum_mod.c) and the ECP module (ecp.c, ecp_curves.c). All other
|
2022-08-22 10:06:32 +02:00
|
|
|
* modules should use the high-level modular bignum interface (bignum_mod.h)
|
2022-08-11 18:42:59 +02:00
|
|
|
* or the legacy bignum interface (bignum.h).
|
2022-07-18 23:13:13 +02:00
|
|
|
*
|
2022-09-27 13:19:13 +02:00
|
|
|
* This is a low-level interface to operations on integers modulo which
|
|
|
|
* has no protection against passing invalid arguments such as arrays of
|
|
|
|
* the wrong size. The functions in bignum_mod.h provide a higher-level
|
|
|
|
* interface that includes protections against accidental misuse, at the
|
|
|
|
* expense of code size and sometimes more cumbersome memory management.
|
2022-12-12 15:00:25 +01:00
|
|
|
*
|
|
|
|
* The functions in this module obey the following conventions unless
|
|
|
|
* explicitly indicated otherwise:
|
|
|
|
* - **Modulus parameters**: the modulus is passed as a pointer to a structure
|
2022-12-14 14:45:49 +01:00
|
|
|
* of type #mbedtls_mpi_mod_modulus. The structure must be set up with an
|
|
|
|
* array of limbs storing the bignum value of the modulus. The modulus must
|
|
|
|
* be odd and is assumed to have no leading zeroes. The modulus is usually
|
2022-12-15 11:57:59 +01:00
|
|
|
* named \c N and is usually input-only.
|
2022-12-12 15:00:25 +01:00
|
|
|
* - **Bignum parameters**: Bignums are passed as pointers to an array of
|
|
|
|
* limbs. A limb has the type #mbedtls_mpi_uint. Unless otherwise specified:
|
2022-12-15 11:57:59 +01:00
|
|
|
* - Bignum parameters called \c A, \c B, ... are inputs, and are not
|
2022-12-12 15:00:25 +01:00
|
|
|
* modified by the function.
|
2022-12-15 11:57:59 +01:00
|
|
|
* - Bignum parameters called \c X, \c Y are outputs or input-output.
|
2022-12-12 15:00:25 +01:00
|
|
|
* The initial content of output-only parameters is ignored.
|
2022-12-15 11:57:59 +01:00
|
|
|
* - \c T is a temporary storage area. The initial content of such a
|
2022-12-12 15:00:25 +01:00
|
|
|
* parameter is ignored and the final content is unspecified.
|
2022-12-14 16:48:31 +01:00
|
|
|
* - **Bignum sizes**: bignum sizes are usually expressed by the \c limbs
|
|
|
|
* member of the modulus argument. All bignum parameters must have the same
|
2022-12-12 15:00:25 +01:00
|
|
|
* number of limbs as the modulus. All bignum sizes must be at least 1 and
|
|
|
|
* must be significantly less than #SIZE_MAX. The behavior if a size is 0 is
|
|
|
|
* undefined.
|
|
|
|
* - **Bignum representation**: the representation of inputs and outputs is
|
2022-12-15 11:57:59 +01:00
|
|
|
* specified by the \c int_rep field of the modulus for arithmetic
|
2022-12-12 15:00:25 +01:00
|
|
|
* functions. Utility functions may allow for different representation.
|
|
|
|
* - **Parameter ordering**: for bignum parameters, outputs come before inputs.
|
2022-12-14 16:57:12 +01:00
|
|
|
* The modulus is passed after other bignum input parameters. Temporaries
|
|
|
|
* come last.
|
2022-12-12 15:00:25 +01:00
|
|
|
* - **Aliasing**: in general, output bignums may be aliased to one or more
|
|
|
|
* inputs. Modulus values may not be aliased to any other parameter. Outputs
|
|
|
|
* may not be aliased to one another. Temporaries may not be aliased to any
|
|
|
|
* other parameter.
|
|
|
|
* - **Overlap**: apart from aliasing of limb array pointers (where two
|
|
|
|
* arguments are equal pointers), overlap is not supported and may result
|
|
|
|
* in undefined behavior.
|
|
|
|
* - **Error handling**: This is a low-level module. Functions generally do not
|
|
|
|
* try to protect against invalid arguments such as nonsensical sizes or
|
|
|
|
* null pointers. Note that passing bignums with a different size than the
|
|
|
|
* modulus may lead to buffer overflows. Some functions which allocate
|
|
|
|
* memory or handle reading/writing of bignums will return an error if
|
|
|
|
* memory allocation fails or if buffer sizes are invalid.
|
2022-12-14 18:08:43 +01:00
|
|
|
* - **Modular representatives**: all functions expect inputs to be in the
|
2022-12-15 11:57:59 +01:00
|
|
|
* range [0, \c N - 1] and guarantee outputs in the range [0, \c N - 1]. If
|
2022-12-14 18:08:43 +01:00
|
|
|
* an input is out of range, outputs are fully unspecified, though bignum
|
|
|
|
* values out of range should not cause buffer overflows (beware that this is
|
|
|
|
* not extensively tested).
|
2022-09-27 13:12:30 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2022-07-18 23:13:13 +02:00
|
|
|
* Copyright The Mbed TLS Contributors
|
2023-11-02 20:47:20 +01:00
|
|
|
* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
|
2022-07-18 23:13:13 +02:00
|
|
|
*/
|
|
|
|
|
2022-07-19 13:45:13 +02:00
|
|
|
#ifndef MBEDTLS_BIGNUM_MOD_RAW_H
|
|
|
|
#define MBEDTLS_BIGNUM_MOD_RAW_H
|
2022-07-18 23:13:13 +02:00
|
|
|
|
|
|
|
#include "common.h"
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_BIGNUM_C)
|
|
|
|
#include "mbedtls/bignum.h"
|
|
|
|
#endif
|
|
|
|
|
2022-08-09 14:34:54 +02:00
|
|
|
#include "bignum_mod.h"
|
|
|
|
|
2022-09-12 16:35:58 +02:00
|
|
|
/**
|
2022-10-14 16:29:42 +02:00
|
|
|
* \brief Perform a safe conditional copy of an MPI which doesn't reveal
|
|
|
|
* whether the assignment was done or not.
|
2022-09-12 16:35:58 +02:00
|
|
|
*
|
2022-10-03 17:01:02 +02:00
|
|
|
* The size to copy is determined by \p N.
|
|
|
|
*
|
2022-10-14 16:29:42 +02:00
|
|
|
* \param[out] X The address of the destination MPI.
|
|
|
|
* This must be initialized. Must have enough limbs to
|
|
|
|
* store the full value of \p A.
|
|
|
|
* \param[in] A The address of the source MPI. This must be initialized.
|
2022-09-30 14:03:04 +02:00
|
|
|
* \param[in] N The address of the modulus related to \p X and \p A.
|
2022-09-12 16:35:58 +02:00
|
|
|
* \param assign The condition deciding whether to perform the
|
|
|
|
* assignment or not. Must be either 0 or 1:
|
2022-09-27 12:13:51 +02:00
|
|
|
* * \c 1: Perform the assignment `X = A`.
|
2022-09-12 16:35:58 +02:00
|
|
|
* * \c 0: Keep the original value of \p X.
|
|
|
|
*
|
|
|
|
* \note This function avoids leaking any information about whether
|
|
|
|
* the assignment was done or not.
|
|
|
|
*
|
|
|
|
* \warning If \p assign is neither 0 nor 1, the result of this function
|
|
|
|
* is indeterminate, and the resulting value in \p X might be
|
2022-10-14 16:29:42 +02:00
|
|
|
* neither its original value nor the value in \p A.
|
2022-09-12 16:35:58 +02:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_mpi_mod_raw_cond_assign(mbedtls_mpi_uint *X,
|
|
|
|
const mbedtls_mpi_uint *A,
|
|
|
|
const mbedtls_mpi_mod_modulus *N,
|
|
|
|
unsigned char assign);
|
2022-09-12 16:35:58 +02:00
|
|
|
|
|
|
|
/**
|
2022-10-14 16:29:42 +02:00
|
|
|
* \brief Perform a safe conditional swap of two MPIs which doesn't reveal
|
|
|
|
* whether the swap was done or not.
|
2022-09-12 16:35:58 +02:00
|
|
|
*
|
2022-10-03 17:01:02 +02:00
|
|
|
* The size to swap is determined by \p N.
|
|
|
|
*
|
2022-09-30 14:03:04 +02:00
|
|
|
* \param[in,out] X The address of the first MPI. This must be initialized.
|
|
|
|
* \param[in,out] Y The address of the second MPI. This must be initialized.
|
|
|
|
* \param[in] N The address of the modulus related to \p X and \p Y.
|
2022-09-12 16:35:58 +02:00
|
|
|
* \param swap The condition deciding whether to perform
|
|
|
|
* the swap or not. Must be either 0 or 1:
|
2022-09-30 13:54:02 +02:00
|
|
|
* * \c 1: Swap the values of \p X and \p Y.
|
|
|
|
* * \c 0: Keep the original values of \p X and \p Y.
|
2022-09-12 16:35:58 +02:00
|
|
|
*
|
|
|
|
* \note This function avoids leaking any information about whether
|
|
|
|
* the swap was done or not.
|
|
|
|
*
|
|
|
|
* \warning If \p swap is neither 0 nor 1, the result of this function
|
2022-09-30 13:54:02 +02:00
|
|
|
* is indeterminate, and both \p X and \p Y might end up with
|
2022-09-12 16:35:58 +02:00
|
|
|
* values different to either of the original ones.
|
2022-09-15 20:01:31 +02:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_mpi_mod_raw_cond_swap(mbedtls_mpi_uint *X,
|
|
|
|
mbedtls_mpi_uint *Y,
|
|
|
|
const mbedtls_mpi_mod_modulus *N,
|
|
|
|
unsigned char swap);
|
2022-09-12 16:35:58 +02:00
|
|
|
|
2022-08-02 17:22:18 +02:00
|
|
|
/** Import X from unsigned binary data.
|
|
|
|
*
|
2022-08-11 18:42:59 +02:00
|
|
|
* The MPI needs to have enough limbs to store the full value (including any
|
|
|
|
* most significant zero bytes in the input).
|
2022-08-02 17:22:18 +02:00
|
|
|
*
|
2023-01-11 16:26:13 +01:00
|
|
|
* \param[out] X The address of the MPI. The size is determined by \p N.
|
2022-08-19 13:24:40 +02:00
|
|
|
* (In particular, it must have at least as many limbs as
|
2023-01-11 16:26:13 +01:00
|
|
|
* the modulus \p N.)
|
|
|
|
* \param[in] N The address of the modulus related to \p X.
|
2022-08-19 13:24:40 +02:00
|
|
|
* \param[in] input The input buffer to import from.
|
|
|
|
* \param input_length The length in bytes of \p input.
|
2022-11-24 18:42:02 +01:00
|
|
|
* \param ext_rep The endianness of the number in the input buffer.
|
2022-08-02 17:22:18 +02:00
|
|
|
*
|
|
|
|
* \return \c 0 if successful.
|
|
|
|
* \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p X isn't
|
2022-08-19 13:24:40 +02:00
|
|
|
* large enough to hold the value in \p input.
|
2022-08-08 09:39:52 +02:00
|
|
|
* \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if the external representation
|
2023-01-11 16:26:13 +01:00
|
|
|
* of \p N is invalid or \p X is not less than \p N.
|
2022-08-02 17:22:18 +02:00
|
|
|
*/
|
2023-01-11 16:42:46 +01:00
|
|
|
int mbedtls_mpi_mod_raw_read(mbedtls_mpi_uint *X,
|
|
|
|
const mbedtls_mpi_mod_modulus *N,
|
|
|
|
const unsigned char *input,
|
|
|
|
size_t input_length,
|
|
|
|
mbedtls_mpi_mod_ext_rep ext_rep);
|
2022-07-18 23:13:13 +02:00
|
|
|
|
2022-08-19 13:24:40 +02:00
|
|
|
/** Export A into unsigned binary data.
|
2022-08-02 17:22:18 +02:00
|
|
|
*
|
2023-01-11 16:30:42 +01:00
|
|
|
* \param[in] A The address of the MPI. The size is determined by \p N.
|
2022-08-19 13:24:40 +02:00
|
|
|
* (In particular, it must have at least as many limbs as
|
2023-01-11 16:30:42 +01:00
|
|
|
* the modulus \p N.)
|
|
|
|
* \param[in] N The address of the modulus related to \p A.
|
2022-08-19 13:24:40 +02:00
|
|
|
* \param[out] output The output buffer to export to.
|
|
|
|
* \param output_length The length in bytes of \p output.
|
2022-11-24 18:42:02 +01:00
|
|
|
* \param ext_rep The endianness in which the number should be written into the output buffer.
|
2022-08-02 17:22:18 +02:00
|
|
|
*
|
|
|
|
* \return \c 0 if successful.
|
2022-08-19 13:24:40 +02:00
|
|
|
* \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p output isn't
|
|
|
|
* large enough to hold the value of \p A.
|
2022-08-08 09:39:52 +02:00
|
|
|
* \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if the external representation
|
2023-01-11 16:30:42 +01:00
|
|
|
* of \p N is invalid.
|
2022-08-02 17:22:18 +02:00
|
|
|
*/
|
2023-01-11 16:42:46 +01:00
|
|
|
int mbedtls_mpi_mod_raw_write(const mbedtls_mpi_uint *A,
|
|
|
|
const mbedtls_mpi_mod_modulus *N,
|
|
|
|
unsigned char *output,
|
|
|
|
size_t output_length,
|
|
|
|
mbedtls_mpi_mod_ext_rep ext_rep);
|
2022-07-18 23:13:13 +02:00
|
|
|
|
2022-11-23 13:09:43 +01:00
|
|
|
/** \brief Subtract two MPIs, returning the residue modulo the specified
|
|
|
|
* modulus.
|
2022-11-09 14:07:43 +01:00
|
|
|
*
|
2022-11-23 13:09:43 +01:00
|
|
|
* The size of the operation is determined by \p N. \p A and \p B must have
|
|
|
|
* the same number of limbs as \p N.
|
|
|
|
*
|
|
|
|
* \p X may be aliased to \p A or \p B, or even both, but may not overlap
|
|
|
|
* either otherwise.
|
2022-11-09 14:07:43 +01:00
|
|
|
*
|
|
|
|
* \param[out] X The address of the result MPI.
|
|
|
|
* This must be initialized. Must have enough limbs to
|
|
|
|
* store the full value of the result.
|
|
|
|
* \param[in] A The address of the first MPI. This must be initialized.
|
|
|
|
* \param[in] B The address of the second MPI. This must be initialized.
|
2022-11-23 13:09:43 +01:00
|
|
|
* \param[in] N The address of the modulus. Used to perform a modulo
|
2022-11-09 14:07:43 +01:00
|
|
|
* operation on the result of the subtraction.
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_mpi_mod_raw_sub(mbedtls_mpi_uint *X,
|
|
|
|
const mbedtls_mpi_uint *A,
|
|
|
|
const mbedtls_mpi_uint *B,
|
|
|
|
const mbedtls_mpi_mod_modulus *N);
|
2022-11-09 14:07:43 +01:00
|
|
|
|
2022-12-07 16:02:33 +01:00
|
|
|
/** \brief Multiply two MPIs, returning the residue modulo the specified
|
|
|
|
* modulus.
|
|
|
|
*
|
2022-12-16 16:35:24 +01:00
|
|
|
* \note Currently handles the case when `N->int_rep` is
|
2022-12-15 10:14:18 +01:00
|
|
|
* MBEDTLS_MPI_MOD_REP_MONTGOMERY.
|
2022-12-07 16:02:33 +01:00
|
|
|
*
|
2022-12-15 10:14:18 +01:00
|
|
|
* The size of the operation is determined by \p N. \p A, \p B and \p X must
|
|
|
|
* all be associated with the modulus \p N and must all have the same number
|
|
|
|
* of limbs as \p N.
|
2022-12-07 16:02:33 +01:00
|
|
|
*
|
2022-12-15 10:14:18 +01:00
|
|
|
* \p X may be aliased to \p A or \p B, or even both, but may not overlap
|
|
|
|
* either otherwise. They may not alias \p N (since they must be in canonical
|
|
|
|
* form, they cannot == \p N).
|
|
|
|
*
|
|
|
|
* \param[out] X The address of the result MPI. Must have the same
|
|
|
|
* number of limbs as \p N.
|
|
|
|
* On successful completion, \p X contains the result of
|
|
|
|
* the multiplication `A * B * R^-1` mod N where
|
|
|
|
* `R = 2^(biL * N->limbs)`.
|
|
|
|
* \param[in] A The address of the first MPI.
|
|
|
|
* \param[in] B The address of the second MPI.
|
2022-12-07 16:02:33 +01:00
|
|
|
* \param[in] N The address of the modulus. Used to perform a modulo
|
2022-12-15 10:14:18 +01:00
|
|
|
* operation on the result of the multiplication.
|
2022-12-16 16:35:24 +01:00
|
|
|
* \param[in,out] T Temporary storage of size at least 2 * N->limbs + 1
|
2022-12-15 10:14:18 +01:00
|
|
|
* limbs. Its initial content is unused and
|
|
|
|
* its final content is indeterminate.
|
|
|
|
* It must not alias or otherwise overlap any of the
|
|
|
|
* other parameters.
|
2022-12-07 16:02:33 +01:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_mpi_mod_raw_mul(mbedtls_mpi_uint *X,
|
|
|
|
const mbedtls_mpi_uint *A,
|
|
|
|
const mbedtls_mpi_uint *B,
|
|
|
|
const mbedtls_mpi_mod_modulus *N,
|
|
|
|
mbedtls_mpi_uint *T);
|
2022-12-07 16:02:33 +01:00
|
|
|
|
2022-12-08 10:44:10 +01:00
|
|
|
/**
|
|
|
|
* \brief Returns the number of limbs of working memory required for
|
|
|
|
* a call to `mbedtls_mpi_mod_raw_inv_prime()`.
|
|
|
|
*
|
2022-12-12 18:06:27 +01:00
|
|
|
* \note This will always be at least
|
|
|
|
* `mbedtls_mpi_core_montmul_working_limbs(AN_limbs)`,
|
|
|
|
* i.e. sufficient for a call to `mbedtls_mpi_core_montmul()`.
|
|
|
|
*
|
2022-12-08 10:44:10 +01:00
|
|
|
* \param AN_limbs The number of limbs in the input `A` and the modulus `N`
|
|
|
|
* (they must be the same size) that will be given to
|
|
|
|
* `mbedtls_mpi_mod_raw_inv_prime()`.
|
|
|
|
*
|
|
|
|
* \return The number of limbs of working memory required by
|
|
|
|
* `mbedtls_mpi_mod_raw_inv_prime()`.
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
size_t mbedtls_mpi_mod_raw_inv_prime_working_limbs(size_t AN_limbs);
|
2022-12-08 10:44:10 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief Perform fixed-width modular inversion of a Montgomery-form MPI with
|
|
|
|
* respect to a modulus \p N that must be prime.
|
|
|
|
*
|
|
|
|
* \p X may be aliased to \p A, but not to \p N or \p RR.
|
|
|
|
*
|
|
|
|
* \param[out] X The modular inverse of \p A with respect to \p N.
|
|
|
|
* Will be in Montgomery form.
|
|
|
|
* \param[in] A The number to calculate the modular inverse of.
|
|
|
|
* Must be in Montgomery form. Must not be 0.
|
|
|
|
* \param[in] N The modulus, as a little-endian array of length \p AN_limbs.
|
|
|
|
* Must be prime.
|
|
|
|
* \param AN_limbs The number of limbs in \p A, \p N and \p RR.
|
|
|
|
* \param[in] RR The precomputed residue of 2^{2*biL} modulo N, as a little-
|
|
|
|
* endian array of length \p AN_limbs.
|
|
|
|
* \param[in,out] T Temporary storage of at least the number of limbs returned
|
|
|
|
* by `mbedtls_mpi_mod_raw_inv_prime_working_limbs()`.
|
|
|
|
* Its initial content is unused and its final content is
|
|
|
|
* indeterminate.
|
|
|
|
* It must not alias or otherwise overlap any of the other
|
|
|
|
* parameters.
|
|
|
|
* It is up to the caller to zeroize \p T when it is no
|
|
|
|
* longer needed, and before freeing it if it was dynamically
|
|
|
|
* allocated.
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_mpi_mod_raw_inv_prime(mbedtls_mpi_uint *X,
|
|
|
|
const mbedtls_mpi_uint *A,
|
|
|
|
const mbedtls_mpi_uint *N,
|
|
|
|
size_t AN_limbs,
|
|
|
|
const mbedtls_mpi_uint *RR,
|
|
|
|
mbedtls_mpi_uint *T);
|
2022-12-08 10:44:10 +01:00
|
|
|
|
2022-11-01 14:14:28 +01:00
|
|
|
/**
|
|
|
|
* \brief Perform a known-size modular addition.
|
|
|
|
*
|
2022-11-24 16:54:16 +01:00
|
|
|
* Calculate `A + B modulo N`.
|
|
|
|
*
|
|
|
|
* The number of limbs in each operand, and the result, is given by the
|
|
|
|
* modulus \p N.
|
|
|
|
*
|
|
|
|
* \p X may be aliased to \p A or \p B, or even both, but may not overlap
|
|
|
|
* either otherwise.
|
2022-11-01 14:27:29 +01:00
|
|
|
*
|
|
|
|
* \param[out] X The result of the modular addition.
|
|
|
|
* \param[in] A Little-endian presentation of the left operand. This
|
2022-11-24 16:54:16 +01:00
|
|
|
* must be smaller than \p N.
|
2022-11-01 14:27:29 +01:00
|
|
|
* \param[in] B Little-endian presentation of the right operand. This
|
2022-11-24 16:54:16 +01:00
|
|
|
* must be smaller than \p N.
|
2022-11-01 14:36:51 +01:00
|
|
|
* \param[in] N The address of the modulus.
|
2022-11-01 14:14:28 +01:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_mpi_mod_raw_add(mbedtls_mpi_uint *X,
|
|
|
|
const mbedtls_mpi_uint *A,
|
|
|
|
const mbedtls_mpi_uint *B,
|
|
|
|
const mbedtls_mpi_mod_modulus *N);
|
2022-11-02 15:35:17 +01:00
|
|
|
|
2022-12-20 19:21:17 +01:00
|
|
|
/** Convert an MPI from canonical representation (little-endian limb array)
|
|
|
|
* to the representation associated with the modulus.
|
|
|
|
*
|
|
|
|
* \param[in,out] X The limb array to convert.
|
|
|
|
* It must have as many limbs as \p N.
|
|
|
|
* It is converted in place.
|
|
|
|
* If this function returns an error, the content of \p X
|
|
|
|
* is unspecified.
|
|
|
|
* \param[in] N The modulus structure.
|
|
|
|
*
|
2023-01-23 17:57:26 +01:00
|
|
|
* \return \c 0 if successful.
|
2022-12-20 19:21:17 +01:00
|
|
|
* Otherwise an \c MBEDTLS_ERR_MPI_xxx error code.
|
|
|
|
*/
|
|
|
|
int mbedtls_mpi_mod_raw_canonical_to_modulus_rep(
|
|
|
|
mbedtls_mpi_uint *X,
|
2023-01-11 14:50:10 +01:00
|
|
|
const mbedtls_mpi_mod_modulus *N);
|
2022-12-20 19:21:17 +01:00
|
|
|
|
|
|
|
/** Convert an MPI from the representation associated with the modulus
|
|
|
|
* to canonical representation (little-endian limb array).
|
|
|
|
*
|
|
|
|
* \param[in,out] X The limb array to convert.
|
|
|
|
* It must have as many limbs as \p N.
|
|
|
|
* It is converted in place.
|
|
|
|
* If this function returns an error, the content of \p X
|
|
|
|
* is unspecified.
|
|
|
|
* \param[in] N The modulus structure.
|
|
|
|
*
|
2023-01-23 17:57:26 +01:00
|
|
|
* \return \c 0 if successful.
|
2022-12-20 19:21:17 +01:00
|
|
|
* Otherwise an \c MBEDTLS_ERR_MPI_xxx error code.
|
|
|
|
*/
|
|
|
|
int mbedtls_mpi_mod_raw_modulus_to_canonical_rep(
|
|
|
|
mbedtls_mpi_uint *X,
|
2023-01-11 14:50:10 +01:00
|
|
|
const mbedtls_mpi_mod_modulus *N);
|
2022-12-20 19:21:17 +01:00
|
|
|
|
2022-12-06 22:54:09 +01:00
|
|
|
/** Generate a random number uniformly in a range.
|
|
|
|
*
|
|
|
|
* This function generates a random number between \p min inclusive and
|
|
|
|
* \p N exclusive.
|
|
|
|
*
|
|
|
|
* The procedure complies with RFC 6979 §3.3 (deterministic ECDSA)
|
|
|
|
* when the RNG is a suitably parametrized instance of HMAC_DRBG
|
|
|
|
* and \p min is \c 1.
|
|
|
|
*
|
|
|
|
* \note There are `N - min` possible outputs. The lower bound
|
|
|
|
* \p min can be reached, but the upper bound \p N cannot.
|
|
|
|
*
|
|
|
|
* \param X The destination MPI, in canonical representation modulo \p N.
|
|
|
|
* It must not be aliased with \p N or otherwise overlap it.
|
|
|
|
* \param min The minimum value to return. It must be strictly smaller
|
|
|
|
* than \b N.
|
|
|
|
* \param N The modulus.
|
|
|
|
* This is the upper bound of the output range, exclusive.
|
|
|
|
* \param f_rng The RNG function to use. This must not be \c NULL.
|
|
|
|
* \param p_rng The RNG parameter to be passed to \p f_rng.
|
|
|
|
*
|
|
|
|
* \return \c 0 if successful.
|
|
|
|
* \return #MBEDTLS_ERR_MPI_NOT_ACCEPTABLE if the implementation was
|
|
|
|
* unable to find a suitable value within a limited number
|
|
|
|
* of attempts. This has a negligible probability if \p N
|
|
|
|
* is significantly larger than \p min, which is the case
|
|
|
|
* for all usual cryptographic applications.
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_mpi_mod_raw_random(mbedtls_mpi_uint *X,
|
|
|
|
mbedtls_mpi_uint min,
|
|
|
|
const mbedtls_mpi_mod_modulus *N,
|
|
|
|
int (*f_rng)(void *, unsigned char *, size_t),
|
|
|
|
void *p_rng);
|
2022-12-06 22:54:09 +01:00
|
|
|
|
2022-11-01 17:19:07 +01:00
|
|
|
/** Convert an MPI into Montgomery form.
|
2022-08-09 15:45:53 +02:00
|
|
|
*
|
|
|
|
* \param X The address of the MPI.
|
2023-01-11 16:36:16 +01:00
|
|
|
* Must have the same number of limbs as \p N.
|
|
|
|
* \param N The address of the modulus, which gives the size of
|
|
|
|
* the base `R` = 2^(biL*N->limbs).
|
2022-08-09 15:45:53 +02:00
|
|
|
*
|
|
|
|
* \return \c 0 if successful.
|
|
|
|
*/
|
2023-01-11 16:42:46 +01:00
|
|
|
int mbedtls_mpi_mod_raw_to_mont_rep(mbedtls_mpi_uint *X,
|
|
|
|
const mbedtls_mpi_mod_modulus *N);
|
2022-11-02 15:35:17 +01:00
|
|
|
|
2022-11-01 17:19:07 +01:00
|
|
|
/** Convert an MPI back from Montgomery representation.
|
2022-08-09 15:45:53 +02:00
|
|
|
*
|
|
|
|
* \param X The address of the MPI.
|
2023-01-11 16:40:22 +01:00
|
|
|
* Must have the same number of limbs as \p N.
|
|
|
|
* \param N The address of the modulus, which gives the size of
|
|
|
|
* the base `R`= 2^(biL*N->limbs).
|
2022-08-09 15:45:53 +02:00
|
|
|
*
|
|
|
|
* \return \c 0 if successful.
|
|
|
|
*/
|
2023-01-11 16:42:46 +01:00
|
|
|
int mbedtls_mpi_mod_raw_from_mont_rep(mbedtls_mpi_uint *X,
|
|
|
|
const mbedtls_mpi_mod_modulus *N);
|
2022-12-07 19:06:05 +01:00
|
|
|
|
|
|
|
/** \brief Perform fixed width modular negation.
|
|
|
|
*
|
2023-01-11 16:42:46 +01:00
|
|
|
* The size of the operation is determined by \p N. \p A must have
|
|
|
|
* the same number of limbs as \p N.
|
2022-12-07 19:06:05 +01:00
|
|
|
*
|
|
|
|
* \p X may be aliased to \p A.
|
|
|
|
*
|
|
|
|
* \param[out] X The result of the modular negation.
|
2022-12-08 12:40:51 +01:00
|
|
|
* This must be initialized.
|
2022-12-07 19:06:05 +01:00
|
|
|
* \param[in] A Little-endian presentation of the input operand. This
|
2023-01-11 16:42:46 +01:00
|
|
|
* must be less than or equal to \p N.
|
|
|
|
* \param[in] N The modulus to use.
|
2022-12-07 19:06:05 +01:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_mpi_mod_raw_neg(mbedtls_mpi_uint *X,
|
|
|
|
const mbedtls_mpi_uint *A,
|
2023-01-11 16:42:46 +01:00
|
|
|
const mbedtls_mpi_mod_modulus *N);
|
2022-11-02 15:35:17 +01:00
|
|
|
|
2022-07-19 13:45:13 +02:00
|
|
|
#endif /* MBEDTLS_BIGNUM_MOD_RAW_H */
|