1a8e1175bSopenharmony_ci/** 2a8e1175bSopenharmony_ci * Modular bignum functions 3a8e1175bSopenharmony_ci * 4a8e1175bSopenharmony_ci * This module implements operations on integers modulo some fixed modulus. 5a8e1175bSopenharmony_ci * 6a8e1175bSopenharmony_ci * The functions in this module obey the following conventions unless 7a8e1175bSopenharmony_ci * explicitly indicated otherwise: 8a8e1175bSopenharmony_ci * 9a8e1175bSopenharmony_ci * - **Modulus parameters**: the modulus is passed as a pointer to a structure 10a8e1175bSopenharmony_ci * of type #mbedtls_mpi_mod_modulus. The structure must be set up with an 11a8e1175bSopenharmony_ci * array of limbs storing the bignum value of the modulus. The modulus must 12a8e1175bSopenharmony_ci * be odd and is assumed to have no leading zeroes. The modulus is usually 13a8e1175bSopenharmony_ci * named \c N and is usually input-only. Functions which take a parameter 14a8e1175bSopenharmony_ci * of type \c const #mbedtls_mpi_mod_modulus* must not modify its value. 15a8e1175bSopenharmony_ci * - **Bignum parameters**: Bignums are passed as pointers to an array of 16a8e1175bSopenharmony_ci * limbs or to a #mbedtls_mpi_mod_residue structure. A limb has the type 17a8e1175bSopenharmony_ci * #mbedtls_mpi_uint. Residues must be initialized before use, and must be 18a8e1175bSopenharmony_ci * associated with the modulus \c N. Unless otherwise specified: 19a8e1175bSopenharmony_ci * - Bignum parameters called \c A, \c B, ... are inputs and are not 20a8e1175bSopenharmony_ci * modified by the function. Functions which take a parameter of 21a8e1175bSopenharmony_ci * type \c const #mbedtls_mpi_mod_residue* must not modify its value. 22a8e1175bSopenharmony_ci * - Bignum parameters called \c X, \c Y, ... are outputs or input-output. 23a8e1175bSopenharmony_ci * The initial bignum value of output-only parameters is ignored, but 24a8e1175bSopenharmony_ci * they must be set up and associated with the modulus \c N. Some 25a8e1175bSopenharmony_ci * functions (typically constant-flow) require that the limbs in an 26a8e1175bSopenharmony_ci * output residue are initialized. 27a8e1175bSopenharmony_ci * - Bignum parameters called \c p are inputs used to set up a modulus or 28a8e1175bSopenharmony_ci * residue. These must be pointers to an array of limbs. 29a8e1175bSopenharmony_ci * - \c T is a temporary storage area. The initial content of such a 30a8e1175bSopenharmony_ci * parameter is ignored and the final content is unspecified. 31a8e1175bSopenharmony_ci * - Some functions use different names, such as \c r for the residue. 32a8e1175bSopenharmony_ci * - **Bignum sizes**: bignum sizes are always expressed in limbs. Both 33a8e1175bSopenharmony_ci * #mbedtls_mpi_mod_modulus and #mbedtls_mpi_mod_residue have a \c limbs 34a8e1175bSopenharmony_ci * member storing its size. All bignum parameters must have the same 35a8e1175bSopenharmony_ci * number of limbs as the modulus. All bignum sizes must be at least 1 and 36a8e1175bSopenharmony_ci * must be significantly less than #SIZE_MAX. The behavior if a size is 0 is 37a8e1175bSopenharmony_ci * undefined. 38a8e1175bSopenharmony_ci * - **Bignum representation**: the representation of inputs and outputs is 39a8e1175bSopenharmony_ci * specified by the \c int_rep field of the modulus. 40a8e1175bSopenharmony_ci * - **Parameter ordering**: for bignum parameters, outputs come before inputs. 41a8e1175bSopenharmony_ci * The modulus is passed after residues. Temporaries come last. 42a8e1175bSopenharmony_ci * - **Aliasing**: in general, output bignums may be aliased to one or more 43a8e1175bSopenharmony_ci * inputs. Modulus values may not be aliased to any other parameter. Outputs 44a8e1175bSopenharmony_ci * may not be aliased to one another. Temporaries may not be aliased to any 45a8e1175bSopenharmony_ci * other parameter. 46a8e1175bSopenharmony_ci * - **Overlap**: apart from aliasing of residue pointers (where two residue 47a8e1175bSopenharmony_ci * arguments are equal pointers), overlap is not supported and may result 48a8e1175bSopenharmony_ci * in undefined behavior. 49a8e1175bSopenharmony_ci * - **Error handling**: functions generally check compatibility of input 50a8e1175bSopenharmony_ci * sizes. Most functions will not check that input values are in canonical 51a8e1175bSopenharmony_ci * form (i.e. that \c A < \c N), this is only checked during setup of a 52a8e1175bSopenharmony_ci * residue structure. 53a8e1175bSopenharmony_ci * - **Modular representatives**: all functions expect inputs to be in the 54a8e1175bSopenharmony_ci * range [0, \c N - 1] and guarantee outputs in the range [0, \c N - 1]. 55a8e1175bSopenharmony_ci * Residues are set up with an associated modulus, and operations are only 56a8e1175bSopenharmony_ci * guaranteed to work if the modulus is associated with all residue 57a8e1175bSopenharmony_ci * parameters. If a residue is passed with a modulus other than the one it 58a8e1175bSopenharmony_ci * is associated with, then it may be out of range. If an input is out of 59a8e1175bSopenharmony_ci * range, outputs are fully unspecified, though bignum values out of range 60a8e1175bSopenharmony_ci * should not cause buffer overflows (beware that this is not extensively 61a8e1175bSopenharmony_ci * tested). 62a8e1175bSopenharmony_ci */ 63a8e1175bSopenharmony_ci 64a8e1175bSopenharmony_ci/* 65a8e1175bSopenharmony_ci * Copyright The Mbed TLS Contributors 66a8e1175bSopenharmony_ci * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later 67a8e1175bSopenharmony_ci */ 68a8e1175bSopenharmony_ci 69a8e1175bSopenharmony_ci#ifndef MBEDTLS_BIGNUM_MOD_H 70a8e1175bSopenharmony_ci#define MBEDTLS_BIGNUM_MOD_H 71a8e1175bSopenharmony_ci 72a8e1175bSopenharmony_ci#include "common.h" 73a8e1175bSopenharmony_ci 74a8e1175bSopenharmony_ci#if defined(MBEDTLS_BIGNUM_C) 75a8e1175bSopenharmony_ci#include "mbedtls/bignum.h" 76a8e1175bSopenharmony_ci#endif 77a8e1175bSopenharmony_ci 78a8e1175bSopenharmony_ci/** How residues associated with a modulus are represented. 79a8e1175bSopenharmony_ci * 80a8e1175bSopenharmony_ci * This also determines which fields of the modulus structure are valid and 81a8e1175bSopenharmony_ci * what their contents are (see #mbedtls_mpi_mod_modulus). 82a8e1175bSopenharmony_ci */ 83a8e1175bSopenharmony_citypedef enum { 84a8e1175bSopenharmony_ci /** Representation not chosen (makes the modulus structure invalid). */ 85a8e1175bSopenharmony_ci MBEDTLS_MPI_MOD_REP_INVALID = 0, 86a8e1175bSopenharmony_ci /* Skip 1 as it is slightly easier to accidentally pass to functions. */ 87a8e1175bSopenharmony_ci /** Montgomery representation. */ 88a8e1175bSopenharmony_ci MBEDTLS_MPI_MOD_REP_MONTGOMERY = 2, 89a8e1175bSopenharmony_ci /* Optimised reduction available. This indicates a coordinate modulus (P) 90a8e1175bSopenharmony_ci * and one or more of the following have been configured: 91a8e1175bSopenharmony_ci * - A nist curve (MBEDTLS_ECP_DP_SECPXXXR1_ENABLED) & MBEDTLS_ECP_NIST_OPTIM. 92a8e1175bSopenharmony_ci * - A Kobliz Curve. 93a8e1175bSopenharmony_ci * - A Fast Reduction Curve CURVE25519 or CURVE448. */ 94a8e1175bSopenharmony_ci MBEDTLS_MPI_MOD_REP_OPT_RED, 95a8e1175bSopenharmony_ci} mbedtls_mpi_mod_rep_selector; 96a8e1175bSopenharmony_ci 97a8e1175bSopenharmony_ci/* Make mbedtls_mpi_mod_rep_selector and mbedtls_mpi_mod_ext_rep disjoint to 98a8e1175bSopenharmony_ci * make it easier to catch when they are accidentally swapped. */ 99a8e1175bSopenharmony_citypedef enum { 100a8e1175bSopenharmony_ci MBEDTLS_MPI_MOD_EXT_REP_INVALID = 0, 101a8e1175bSopenharmony_ci MBEDTLS_MPI_MOD_EXT_REP_LE = 8, 102a8e1175bSopenharmony_ci MBEDTLS_MPI_MOD_EXT_REP_BE 103a8e1175bSopenharmony_ci} mbedtls_mpi_mod_ext_rep; 104a8e1175bSopenharmony_ci 105a8e1175bSopenharmony_citypedef struct { 106a8e1175bSopenharmony_ci mbedtls_mpi_uint *p; 107a8e1175bSopenharmony_ci size_t limbs; 108a8e1175bSopenharmony_ci} mbedtls_mpi_mod_residue; 109a8e1175bSopenharmony_ci 110a8e1175bSopenharmony_citypedef struct { 111a8e1175bSopenharmony_ci mbedtls_mpi_uint const *rr; /* The residue for 2^{2*n*biL} mod N */ 112a8e1175bSopenharmony_ci mbedtls_mpi_uint mm; /* Montgomery const for -N^{-1} mod 2^{ciL} */ 113a8e1175bSopenharmony_ci} mbedtls_mpi_mont_struct; 114a8e1175bSopenharmony_ci 115a8e1175bSopenharmony_citypedef int (*mbedtls_mpi_modp_fn)(mbedtls_mpi_uint *X, size_t X_limbs); 116a8e1175bSopenharmony_ci 117a8e1175bSopenharmony_citypedef struct { 118a8e1175bSopenharmony_ci mbedtls_mpi_modp_fn modp; /* The optimised reduction function pointer */ 119a8e1175bSopenharmony_ci} mbedtls_mpi_opt_red_struct; 120a8e1175bSopenharmony_ci 121a8e1175bSopenharmony_citypedef struct { 122a8e1175bSopenharmony_ci const mbedtls_mpi_uint *p; 123a8e1175bSopenharmony_ci size_t limbs; // number of limbs 124a8e1175bSopenharmony_ci size_t bits; // bitlen of p 125a8e1175bSopenharmony_ci mbedtls_mpi_mod_rep_selector int_rep; // selector to signal the active member of the union 126a8e1175bSopenharmony_ci union rep { 127a8e1175bSopenharmony_ci /* if int_rep == #MBEDTLS_MPI_MOD_REP_MONTGOMERY */ 128a8e1175bSopenharmony_ci mbedtls_mpi_mont_struct mont; 129a8e1175bSopenharmony_ci /* if int_rep == #MBEDTLS_MPI_MOD_REP_OPT_RED */ 130a8e1175bSopenharmony_ci mbedtls_mpi_opt_red_struct ored; 131a8e1175bSopenharmony_ci } rep; 132a8e1175bSopenharmony_ci} mbedtls_mpi_mod_modulus; 133a8e1175bSopenharmony_ci 134a8e1175bSopenharmony_ci/** Setup a residue structure. 135a8e1175bSopenharmony_ci * 136a8e1175bSopenharmony_ci * The residue will be set up with the buffer \p p and modulus \p N. 137a8e1175bSopenharmony_ci * 138a8e1175bSopenharmony_ci * The memory pointed to by \p p will be used by the resulting residue structure. 139a8e1175bSopenharmony_ci * The value at the pointed-to memory will be the initial value of \p r and must 140a8e1175bSopenharmony_ci * hold a value that is less than the modulus. This value will be used as-is 141a8e1175bSopenharmony_ci * and interpreted according to the value of the `N->int_rep` field. 142a8e1175bSopenharmony_ci * 143a8e1175bSopenharmony_ci * The modulus \p N will be the modulus associated with \p r. The residue \p r 144a8e1175bSopenharmony_ci * should only be used in operations where the modulus is \p N. 145a8e1175bSopenharmony_ci * 146a8e1175bSopenharmony_ci * \param[out] r The address of the residue to setup. 147a8e1175bSopenharmony_ci * \param[in] N The address of the modulus related to \p r. 148a8e1175bSopenharmony_ci * \param[in] p The address of the limb array containing the value of \p r. 149a8e1175bSopenharmony_ci * The memory pointed to by \p p will be used by \p r and must 150a8e1175bSopenharmony_ci * not be modified in any way until after 151a8e1175bSopenharmony_ci * mbedtls_mpi_mod_residue_release() is called. The data 152a8e1175bSopenharmony_ci * pointed to by \p p must be less than the modulus (the value 153a8e1175bSopenharmony_ci * pointed to by `N->p`) and already in the representation 154a8e1175bSopenharmony_ci * indicated by `N->int_rep`. 155a8e1175bSopenharmony_ci * \param p_limbs The number of limbs of \p p. Must be the same as the number 156a8e1175bSopenharmony_ci * of limbs in the modulus \p N. 157a8e1175bSopenharmony_ci * 158a8e1175bSopenharmony_ci * \return \c 0 if successful. 159a8e1175bSopenharmony_ci * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if \p p_limbs is less than the 160a8e1175bSopenharmony_ci * limbs in \p N or if \p p is not less than \p N. 161a8e1175bSopenharmony_ci */ 162a8e1175bSopenharmony_ciint mbedtls_mpi_mod_residue_setup(mbedtls_mpi_mod_residue *r, 163a8e1175bSopenharmony_ci const mbedtls_mpi_mod_modulus *N, 164a8e1175bSopenharmony_ci mbedtls_mpi_uint *p, 165a8e1175bSopenharmony_ci size_t p_limbs); 166a8e1175bSopenharmony_ci 167a8e1175bSopenharmony_ci/** Unbind elements of a residue structure. 168a8e1175bSopenharmony_ci * 169a8e1175bSopenharmony_ci * This function removes the reference to the limb array that was passed to 170a8e1175bSopenharmony_ci * mbedtls_mpi_mod_residue_setup() to make it safe to free or use again. 171a8e1175bSopenharmony_ci * 172a8e1175bSopenharmony_ci * This function invalidates \p r and it must not be used until after 173a8e1175bSopenharmony_ci * mbedtls_mpi_mod_residue_setup() is called on it again. 174a8e1175bSopenharmony_ci * 175a8e1175bSopenharmony_ci * \param[out] r The address of residue to release. 176a8e1175bSopenharmony_ci */ 177a8e1175bSopenharmony_civoid mbedtls_mpi_mod_residue_release(mbedtls_mpi_mod_residue *r); 178a8e1175bSopenharmony_ci 179a8e1175bSopenharmony_ci/** Initialize a modulus structure. 180a8e1175bSopenharmony_ci * 181a8e1175bSopenharmony_ci * \param[out] N The address of the modulus structure to initialize. 182a8e1175bSopenharmony_ci */ 183a8e1175bSopenharmony_civoid mbedtls_mpi_mod_modulus_init(mbedtls_mpi_mod_modulus *N); 184a8e1175bSopenharmony_ci 185a8e1175bSopenharmony_ci/** Setup a modulus structure. 186a8e1175bSopenharmony_ci * 187a8e1175bSopenharmony_ci * \param[out] N The address of the modulus structure to populate. 188a8e1175bSopenharmony_ci * \param[in] p The address of the limb array storing the value of \p N. 189a8e1175bSopenharmony_ci * The memory pointed to by \p p will be used by \p N and must 190a8e1175bSopenharmony_ci * not be modified in any way until after 191a8e1175bSopenharmony_ci * mbedtls_mpi_mod_modulus_free() is called. 192a8e1175bSopenharmony_ci * \param p_limbs The number of limbs of \p p. 193a8e1175bSopenharmony_ci * 194a8e1175bSopenharmony_ci * \return \c 0 if successful. 195a8e1175bSopenharmony_ci */ 196a8e1175bSopenharmony_ciint mbedtls_mpi_mod_modulus_setup(mbedtls_mpi_mod_modulus *N, 197a8e1175bSopenharmony_ci const mbedtls_mpi_uint *p, 198a8e1175bSopenharmony_ci size_t p_limbs); 199a8e1175bSopenharmony_ci 200a8e1175bSopenharmony_ci/** Setup an optimised-reduction compatible modulus structure. 201a8e1175bSopenharmony_ci * 202a8e1175bSopenharmony_ci * \param[out] N The address of the modulus structure to populate. 203a8e1175bSopenharmony_ci * \param[in] p The address of the limb array storing the value of \p N. 204a8e1175bSopenharmony_ci * The memory pointed to by \p p will be used by \p N and must 205a8e1175bSopenharmony_ci * not be modified in any way until after 206a8e1175bSopenharmony_ci * mbedtls_mpi_mod_modulus_free() is called. 207a8e1175bSopenharmony_ci * \param p_limbs The number of limbs of \p p. 208a8e1175bSopenharmony_ci * \param modp A pointer to the optimised reduction function to use. \p p. 209a8e1175bSopenharmony_ci * 210a8e1175bSopenharmony_ci * \return \c 0 if successful. 211a8e1175bSopenharmony_ci */ 212a8e1175bSopenharmony_ciint mbedtls_mpi_mod_optred_modulus_setup(mbedtls_mpi_mod_modulus *N, 213a8e1175bSopenharmony_ci const mbedtls_mpi_uint *p, 214a8e1175bSopenharmony_ci size_t p_limbs, 215a8e1175bSopenharmony_ci mbedtls_mpi_modp_fn modp); 216a8e1175bSopenharmony_ci 217a8e1175bSopenharmony_ci/** Free elements of a modulus structure. 218a8e1175bSopenharmony_ci * 219a8e1175bSopenharmony_ci * This function frees any memory allocated by mbedtls_mpi_mod_modulus_setup(). 220a8e1175bSopenharmony_ci * 221a8e1175bSopenharmony_ci * \warning This function does not free the limb array passed to 222a8e1175bSopenharmony_ci * mbedtls_mpi_mod_modulus_setup() only removes the reference to it, 223a8e1175bSopenharmony_ci * making it safe to free or to use it again. 224a8e1175bSopenharmony_ci * 225a8e1175bSopenharmony_ci * \param[in,out] N The address of the modulus structure to free. 226a8e1175bSopenharmony_ci */ 227a8e1175bSopenharmony_civoid mbedtls_mpi_mod_modulus_free(mbedtls_mpi_mod_modulus *N); 228a8e1175bSopenharmony_ci 229a8e1175bSopenharmony_ci/** \brief Multiply two residues, returning the residue modulo the specified 230a8e1175bSopenharmony_ci * modulus. 231a8e1175bSopenharmony_ci * 232a8e1175bSopenharmony_ci * \note Currently handles the case when `N->int_rep` is 233a8e1175bSopenharmony_ci * MBEDTLS_MPI_MOD_REP_MONTGOMERY. 234a8e1175bSopenharmony_ci * 235a8e1175bSopenharmony_ci * The size of the operation is determined by \p N. \p A, \p B and \p X must 236a8e1175bSopenharmony_ci * all be associated with the modulus \p N and must all have the same number 237a8e1175bSopenharmony_ci * of limbs as \p N. 238a8e1175bSopenharmony_ci * 239a8e1175bSopenharmony_ci * \p X may be aliased to \p A or \p B, or even both, but may not overlap 240a8e1175bSopenharmony_ci * either otherwise. They may not alias \p N (since they must be in canonical 241a8e1175bSopenharmony_ci * form, they cannot == \p N). 242a8e1175bSopenharmony_ci * 243a8e1175bSopenharmony_ci * \param[out] X The address of the result MPI. Must have the same 244a8e1175bSopenharmony_ci * number of limbs as \p N. 245a8e1175bSopenharmony_ci * On successful completion, \p X contains the result of 246a8e1175bSopenharmony_ci * the multiplication `A * B * R^-1` mod N where 247a8e1175bSopenharmony_ci * `R = 2^(biL * N->limbs)`. 248a8e1175bSopenharmony_ci * \param[in] A The address of the first MPI. 249a8e1175bSopenharmony_ci * \param[in] B The address of the second MPI. 250a8e1175bSopenharmony_ci * \param[in] N The address of the modulus. Used to perform a modulo 251a8e1175bSopenharmony_ci * operation on the result of the multiplication. 252a8e1175bSopenharmony_ci * 253a8e1175bSopenharmony_ci * \return \c 0 if successful. 254a8e1175bSopenharmony_ci * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if all the parameters do not 255a8e1175bSopenharmony_ci * have the same number of limbs or \p N is invalid. 256a8e1175bSopenharmony_ci * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory-allocation failure. 257a8e1175bSopenharmony_ci */ 258a8e1175bSopenharmony_ciint mbedtls_mpi_mod_mul(mbedtls_mpi_mod_residue *X, 259a8e1175bSopenharmony_ci const mbedtls_mpi_mod_residue *A, 260a8e1175bSopenharmony_ci const mbedtls_mpi_mod_residue *B, 261a8e1175bSopenharmony_ci const mbedtls_mpi_mod_modulus *N); 262a8e1175bSopenharmony_ci 263a8e1175bSopenharmony_ci/** 264a8e1175bSopenharmony_ci * \brief Perform a fixed-size modular subtraction. 265a8e1175bSopenharmony_ci * 266a8e1175bSopenharmony_ci * Calculate `A - B modulo N`. 267a8e1175bSopenharmony_ci * 268a8e1175bSopenharmony_ci * \p A, \p B and \p X must all have the same number of limbs as \p N. 269a8e1175bSopenharmony_ci * 270a8e1175bSopenharmony_ci * \p X may be aliased to \p A or \p B, or even both, but may not overlap 271a8e1175bSopenharmony_ci * either otherwise. 272a8e1175bSopenharmony_ci * 273a8e1175bSopenharmony_ci * \note This function does not check that \p A or \p B are in canonical 274a8e1175bSopenharmony_ci * form (that is, are < \p N) - that will have been done by 275a8e1175bSopenharmony_ci * mbedtls_mpi_mod_residue_setup(). 276a8e1175bSopenharmony_ci * 277a8e1175bSopenharmony_ci * \param[out] X The address of the result MPI. Must be initialized. 278a8e1175bSopenharmony_ci * Must have the same number of limbs as the modulus \p N. 279a8e1175bSopenharmony_ci * \param[in] A The address of the first MPI. 280a8e1175bSopenharmony_ci * \param[in] B The address of the second MPI. 281a8e1175bSopenharmony_ci * \param[in] N The address of the modulus. Used to perform a modulo 282a8e1175bSopenharmony_ci * operation on the result of the subtraction. 283a8e1175bSopenharmony_ci * 284a8e1175bSopenharmony_ci * \return \c 0 if successful. 285a8e1175bSopenharmony_ci * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if the given MPIs do not 286a8e1175bSopenharmony_ci * have the correct number of limbs. 287a8e1175bSopenharmony_ci */ 288a8e1175bSopenharmony_ciint mbedtls_mpi_mod_sub(mbedtls_mpi_mod_residue *X, 289a8e1175bSopenharmony_ci const mbedtls_mpi_mod_residue *A, 290a8e1175bSopenharmony_ci const mbedtls_mpi_mod_residue *B, 291a8e1175bSopenharmony_ci const mbedtls_mpi_mod_modulus *N); 292a8e1175bSopenharmony_ci 293a8e1175bSopenharmony_ci/** 294a8e1175bSopenharmony_ci * \brief Perform modular inversion of an MPI with respect to a modulus \p N. 295a8e1175bSopenharmony_ci * 296a8e1175bSopenharmony_ci * \p A and \p X must be associated with the modulus \p N and will therefore 297a8e1175bSopenharmony_ci * have the same number of limbs as \p N. 298a8e1175bSopenharmony_ci * 299a8e1175bSopenharmony_ci * \p X may be aliased to \p A. 300a8e1175bSopenharmony_ci * 301a8e1175bSopenharmony_ci * \warning Currently only supports prime moduli, but does not check for them. 302a8e1175bSopenharmony_ci * 303a8e1175bSopenharmony_ci * \param[out] X The modular inverse of \p A with respect to \p N. 304a8e1175bSopenharmony_ci * \param[in] A The number to calculate the modular inverse of. 305a8e1175bSopenharmony_ci * Must not be 0. 306a8e1175bSopenharmony_ci * \param[in] N The modulus to use. 307a8e1175bSopenharmony_ci * 308a8e1175bSopenharmony_ci * \return \c 0 if successful. 309a8e1175bSopenharmony_ci * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if \p A and \p N do not 310a8e1175bSopenharmony_ci * have the same number of limbs. 311a8e1175bSopenharmony_ci * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if \p A is zero. 312a8e1175bSopenharmony_ci * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if couldn't allocate enough 313a8e1175bSopenharmony_ci * memory (needed for conversion to and from Mongtomery form 314a8e1175bSopenharmony_ci * when not in Montgomery form already, and for temporary use 315a8e1175bSopenharmony_ci * by the inversion calculation itself). 316a8e1175bSopenharmony_ci */ 317a8e1175bSopenharmony_ci 318a8e1175bSopenharmony_ciint mbedtls_mpi_mod_inv(mbedtls_mpi_mod_residue *X, 319a8e1175bSopenharmony_ci const mbedtls_mpi_mod_residue *A, 320a8e1175bSopenharmony_ci const mbedtls_mpi_mod_modulus *N); 321a8e1175bSopenharmony_ci/** 322a8e1175bSopenharmony_ci * \brief Perform a fixed-size modular addition. 323a8e1175bSopenharmony_ci * 324a8e1175bSopenharmony_ci * Calculate `A + B modulo N`. 325a8e1175bSopenharmony_ci * 326a8e1175bSopenharmony_ci * \p A, \p B and \p X must all be associated with the modulus \p N and must 327a8e1175bSopenharmony_ci * all have the same number of limbs as \p N. 328a8e1175bSopenharmony_ci * 329a8e1175bSopenharmony_ci * \p X may be aliased to \p A or \p B, or even both, but may not overlap 330a8e1175bSopenharmony_ci * either otherwise. 331a8e1175bSopenharmony_ci * 332a8e1175bSopenharmony_ci * \note This function does not check that \p A or \p B are in canonical 333a8e1175bSopenharmony_ci * form (that is, are < \p N) - that will have been done by 334a8e1175bSopenharmony_ci * mbedtls_mpi_mod_residue_setup(). 335a8e1175bSopenharmony_ci * 336a8e1175bSopenharmony_ci * \param[out] X The address of the result residue. Must be initialized. 337a8e1175bSopenharmony_ci * Must have the same number of limbs as the modulus \p N. 338a8e1175bSopenharmony_ci * \param[in] A The address of the first input residue. 339a8e1175bSopenharmony_ci * \param[in] B The address of the second input residue. 340a8e1175bSopenharmony_ci * \param[in] N The address of the modulus. Used to perform a modulo 341a8e1175bSopenharmony_ci * operation on the result of the addition. 342a8e1175bSopenharmony_ci * 343a8e1175bSopenharmony_ci * \return \c 0 if successful. 344a8e1175bSopenharmony_ci * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if the given MPIs do not 345a8e1175bSopenharmony_ci * have the correct number of limbs. 346a8e1175bSopenharmony_ci */ 347a8e1175bSopenharmony_ciint mbedtls_mpi_mod_add(mbedtls_mpi_mod_residue *X, 348a8e1175bSopenharmony_ci const mbedtls_mpi_mod_residue *A, 349a8e1175bSopenharmony_ci const mbedtls_mpi_mod_residue *B, 350a8e1175bSopenharmony_ci const mbedtls_mpi_mod_modulus *N); 351a8e1175bSopenharmony_ci 352a8e1175bSopenharmony_ci/** Generate a random number uniformly in a range. 353a8e1175bSopenharmony_ci * 354a8e1175bSopenharmony_ci * This function generates a random number between \p min inclusive and 355a8e1175bSopenharmony_ci * \p N exclusive. 356a8e1175bSopenharmony_ci * 357a8e1175bSopenharmony_ci * The procedure complies with RFC 6979 §3.3 (deterministic ECDSA) 358a8e1175bSopenharmony_ci * when the RNG is a suitably parametrized instance of HMAC_DRBG 359a8e1175bSopenharmony_ci * and \p min is \c 1. 360a8e1175bSopenharmony_ci * 361a8e1175bSopenharmony_ci * \note There are `N - min` possible outputs. The lower bound 362a8e1175bSopenharmony_ci * \p min can be reached, but the upper bound \p N cannot. 363a8e1175bSopenharmony_ci * 364a8e1175bSopenharmony_ci * \param X The destination residue. 365a8e1175bSopenharmony_ci * \param min The minimum value to return. It must be strictly smaller 366a8e1175bSopenharmony_ci * than \b N. 367a8e1175bSopenharmony_ci * \param N The modulus. 368a8e1175bSopenharmony_ci * This is the upper bound of the output range, exclusive. 369a8e1175bSopenharmony_ci * \param f_rng The RNG function to use. This must not be \c NULL. 370a8e1175bSopenharmony_ci * \param p_rng The RNG parameter to be passed to \p f_rng. 371a8e1175bSopenharmony_ci * 372a8e1175bSopenharmony_ci * \return \c 0 if successful. 373a8e1175bSopenharmony_ci * \return #MBEDTLS_ERR_MPI_NOT_ACCEPTABLE if the implementation was 374a8e1175bSopenharmony_ci * unable to find a suitable value within a limited number 375a8e1175bSopenharmony_ci * of attempts. This has a negligible probability if \p N 376a8e1175bSopenharmony_ci * is significantly larger than \p min, which is the case 377a8e1175bSopenharmony_ci * for all usual cryptographic applications. 378a8e1175bSopenharmony_ci */ 379a8e1175bSopenharmony_ciint mbedtls_mpi_mod_random(mbedtls_mpi_mod_residue *X, 380a8e1175bSopenharmony_ci mbedtls_mpi_uint min, 381a8e1175bSopenharmony_ci const mbedtls_mpi_mod_modulus *N, 382a8e1175bSopenharmony_ci int (*f_rng)(void *, unsigned char *, size_t), 383a8e1175bSopenharmony_ci void *p_rng); 384a8e1175bSopenharmony_ci 385a8e1175bSopenharmony_ci/** Read a residue from a byte buffer. 386a8e1175bSopenharmony_ci * 387a8e1175bSopenharmony_ci * The residue will be automatically converted to the internal representation 388a8e1175bSopenharmony_ci * based on the value of the `N->int_rep` field. 389a8e1175bSopenharmony_ci * 390a8e1175bSopenharmony_ci * The modulus \p N will be the modulus associated with \p r. The residue \p r 391a8e1175bSopenharmony_ci * should only be used in operations where the modulus is \p N or a modulus 392a8e1175bSopenharmony_ci * equivalent to \p N (in the sense that all their fields or memory pointed by 393a8e1175bSopenharmony_ci * their fields hold the same value). 394a8e1175bSopenharmony_ci * 395a8e1175bSopenharmony_ci * \param[out] r The address of the residue. It must have exactly the same 396a8e1175bSopenharmony_ci * number of limbs as the modulus \p N. 397a8e1175bSopenharmony_ci * \param[in] N The address of the modulus. 398a8e1175bSopenharmony_ci * \param[in] buf The input buffer to import from. 399a8e1175bSopenharmony_ci * \param buflen The length in bytes of \p buf. 400a8e1175bSopenharmony_ci * \param ext_rep The endianness of the number in the input buffer. 401a8e1175bSopenharmony_ci * 402a8e1175bSopenharmony_ci * \return \c 0 if successful. 403a8e1175bSopenharmony_ci * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p r isn't 404a8e1175bSopenharmony_ci * large enough to hold the value in \p buf. 405a8e1175bSopenharmony_ci * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if \p ext_rep 406a8e1175bSopenharmony_ci * is invalid or the value in the buffer is not less than \p N. 407a8e1175bSopenharmony_ci */ 408a8e1175bSopenharmony_ciint mbedtls_mpi_mod_read(mbedtls_mpi_mod_residue *r, 409a8e1175bSopenharmony_ci const mbedtls_mpi_mod_modulus *N, 410a8e1175bSopenharmony_ci const unsigned char *buf, 411a8e1175bSopenharmony_ci size_t buflen, 412a8e1175bSopenharmony_ci mbedtls_mpi_mod_ext_rep ext_rep); 413a8e1175bSopenharmony_ci 414a8e1175bSopenharmony_ci/** Write a residue into a byte buffer. 415a8e1175bSopenharmony_ci * 416a8e1175bSopenharmony_ci * The modulus \p N must be the modulus associated with \p r (see 417a8e1175bSopenharmony_ci * mbedtls_mpi_mod_residue_setup() and mbedtls_mpi_mod_read()). 418a8e1175bSopenharmony_ci * 419a8e1175bSopenharmony_ci * The residue will be automatically converted from the internal representation 420a8e1175bSopenharmony_ci * based on the value of `N->int_rep` field. 421a8e1175bSopenharmony_ci * 422a8e1175bSopenharmony_ci * \warning If the buffer is smaller than `N->bits`, the number of 423a8e1175bSopenharmony_ci * leading zeroes is leaked through timing. If \p r is 424a8e1175bSopenharmony_ci * secret, the caller must ensure that \p buflen is at least 425a8e1175bSopenharmony_ci * (`N->bits`+7)/8. 426a8e1175bSopenharmony_ci * 427a8e1175bSopenharmony_ci * \param[in] r The address of the residue. It must have the same number of 428a8e1175bSopenharmony_ci * limbs as the modulus \p N. (\p r is an input parameter, but 429a8e1175bSopenharmony_ci * its value will be modified during execution and restored 430a8e1175bSopenharmony_ci * before the function returns.) 431a8e1175bSopenharmony_ci * \param[in] N The address of the modulus associated with \p r. 432a8e1175bSopenharmony_ci * \param[out] buf The output buffer to export to. 433a8e1175bSopenharmony_ci * \param buflen The length in bytes of \p buf. 434a8e1175bSopenharmony_ci * \param ext_rep The endianness in which the number should be written into 435a8e1175bSopenharmony_ci * the output buffer. 436a8e1175bSopenharmony_ci * 437a8e1175bSopenharmony_ci * \return \c 0 if successful. 438a8e1175bSopenharmony_ci * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p buf isn't 439a8e1175bSopenharmony_ci * large enough to hold the value of \p r (without leading 440a8e1175bSopenharmony_ci * zeroes). 441a8e1175bSopenharmony_ci * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if \p ext_rep is invalid. 442a8e1175bSopenharmony_ci * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if couldn't allocate enough 443a8e1175bSopenharmony_ci * memory for conversion. Can occur only for moduli with 444a8e1175bSopenharmony_ci * MBEDTLS_MPI_MOD_REP_MONTGOMERY. 445a8e1175bSopenharmony_ci */ 446a8e1175bSopenharmony_ciint mbedtls_mpi_mod_write(const mbedtls_mpi_mod_residue *r, 447a8e1175bSopenharmony_ci const mbedtls_mpi_mod_modulus *N, 448a8e1175bSopenharmony_ci unsigned char *buf, 449a8e1175bSopenharmony_ci size_t buflen, 450a8e1175bSopenharmony_ci mbedtls_mpi_mod_ext_rep ext_rep); 451a8e1175bSopenharmony_ci 452a8e1175bSopenharmony_ci#endif /* MBEDTLS_BIGNUM_MOD_H */ 453