mirror of
https://github.com/Mbed-TLS/mbedtls.git
synced 2025-07-29 11:41:15 +03:00
Merge pull request #6747 from gilles-peskine-arm/bignum-mod-random
Bignum mod random
This commit is contained in:
@ -87,12 +87,23 @@
|
||||
#include "mbedtls/bignum.h"
|
||||
#endif
|
||||
|
||||
/* Skip 1 as it is slightly easier to accidentally pass to functions. */
|
||||
/** How residues associated with a modulus are represented.
|
||||
*
|
||||
* This also determines which fields of the modulus structure are valid and
|
||||
* what their contents are (see #mbedtls_mpi_mod_modulus).
|
||||
*/
|
||||
typedef enum
|
||||
{
|
||||
/** Representation not chosen (makes the modulus structure invalid). */
|
||||
MBEDTLS_MPI_MOD_REP_INVALID = 0,
|
||||
/* Skip 1 as it is slightly easier to accidentally pass to functions. */
|
||||
/** Montgomery representation. */
|
||||
MBEDTLS_MPI_MOD_REP_MONTGOMERY = 2,
|
||||
MBEDTLS_MPI_MOD_REP_OPT_RED
|
||||
/** TODO: document this.
|
||||
*
|
||||
* Residues are in canonical representation.
|
||||
*/
|
||||
MBEDTLS_MPI_MOD_REP_OPT_RED,
|
||||
} mbedtls_mpi_mod_rep_selector;
|
||||
|
||||
/* Make mbedtls_mpi_mod_rep_selector and mbedtls_mpi_mod_ext_rep disjoint to
|
||||
@ -124,7 +135,9 @@ typedef struct {
|
||||
mbedtls_mpi_mod_rep_selector int_rep; // selector to signal the active member of the union
|
||||
union rep
|
||||
{
|
||||
/* if int_rep == #MBEDTLS_MPI_MOD_REP_MONTGOMERY */
|
||||
mbedtls_mpi_mont_struct mont;
|
||||
/* if int_rep == #MBEDTLS_MPI_MOD_REP_OPT_RED */
|
||||
mbedtls_mpi_opt_red_struct ored;
|
||||
} rep;
|
||||
} mbedtls_mpi_mod_modulus;
|
||||
@ -319,6 +332,39 @@ int mbedtls_mpi_mod_add( mbedtls_mpi_mod_residue *X,
|
||||
|
||||
/* BEGIN MERGE SLOT 6 */
|
||||
|
||||
/** 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 residue.
|
||||
* \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.
|
||||
*/
|
||||
int mbedtls_mpi_mod_random( mbedtls_mpi_mod_residue *X,
|
||||
mbedtls_mpi_uint min,
|
||||
const mbedtls_mpi_mod_modulus *N,
|
||||
int (*f_rng)(void *, unsigned char *, size_t),
|
||||
void *p_rng );
|
||||
|
||||
/* END MERGE SLOT 6 */
|
||||
|
||||
/* BEGIN MERGE SLOT 7 */
|
||||
|
Reference in New Issue
Block a user