2022-07-18 23:02:33 +02:00
|
|
|
/**
|
2022-08-11 18:42:59 +02:00
|
|
|
* Modular bignum functions
|
2022-09-27 13:19:13 +02:00
|
|
|
*
|
|
|
|
* This module implements operations on integers modulo some fixed modulus.
|
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 17:58:44 +01:00
|
|
|
* named \c N and is usually input-only. Functions which take a parameter
|
|
|
|
* of type \c const #mbedtls_mpi_mod_modulus* must not modify its value.
|
2022-12-12 15:00:25 +01:00
|
|
|
* - **Bignum parameters**: Bignums are passed as pointers to an array of
|
|
|
|
* limbs or to a #mbedtls_mpi_mod_residue structure. A limb has the type
|
|
|
|
* #mbedtls_mpi_uint. Residues must be initialized before use, and must be
|
2022-12-15 11:57:59 +01:00
|
|
|
* associated with the modulus \c N. Unless otherwise specified:
|
|
|
|
* - Bignum parameters called \c A, \c B, ... are inputs and are not
|
2022-12-15 17:58:44 +01:00
|
|
|
* modified by the function. Functions which take a parameter of
|
|
|
|
* type \c const #mbedtls_mpi_mod_residue* must not modify its value.
|
2022-12-15 11:57:59 +01:00
|
|
|
* - Bignum parameters called \c X, \c Y, ... are outputs or input-output.
|
2022-12-14 16:24:46 +01:00
|
|
|
* The initial bignum value of output-only parameters is ignored, but
|
2022-12-15 15:13:32 +01:00
|
|
|
* they must be set up and associated with the modulus \c N. Some
|
|
|
|
* functions (typically constant-flow) require that the limbs in an
|
|
|
|
* output residue are initialized.
|
2022-12-15 15:53:43 +01:00
|
|
|
* - Bignum parameters called \c p are inputs used to set up a modulus or
|
2022-12-12 15:00:25 +01:00
|
|
|
* residue. These must be pointers to an array of limbs.
|
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-15 15:53:43 +01:00
|
|
|
* - Some functions use different names, such as \c r for the residue.
|
2022-12-12 15:00:25 +01:00
|
|
|
* - **Bignum sizes**: bignum sizes are always expressed in limbs. Both
|
2022-12-14 16:48:31 +01:00
|
|
|
* #mbedtls_mpi_mod_modulus and #mbedtls_mpi_mod_residue have a \c limbs
|
|
|
|
* member storing its size. All bignum parameters must have the same
|
|
|
|
* 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.
|
2022-12-12 15:00:25 +01:00
|
|
|
* - **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.
|
2022-12-12 15:00:25 +01:00
|
|
|
* - **Parameter ordering**: for bignum parameters, outputs come before inputs.
|
2022-12-14 16:57:12 +01:00
|
|
|
* The modulus is passed after residues. 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 residue pointers (where two residue
|
|
|
|
* arguments are equal pointers), overlap is not supported and may result
|
|
|
|
* in undefined behavior.
|
2022-12-14 16:32:31 +01:00
|
|
|
* - **Error handling**: functions generally check compatibility of input
|
2022-12-12 15:00:25 +01:00
|
|
|
* sizes. Most functions will not check that input values are in canonical
|
2022-12-15 11:57:59 +01:00
|
|
|
* form (i.e. that \c A < \c N), this is only checked during setup of a
|
2022-12-12 15:00:25 +01:00
|
|
|
* residue structure.
|
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].
|
2022-12-14 18:08:43 +01:00
|
|
|
* Residues are set up with an associated modulus, and operations are only
|
|
|
|
* guaranteed to work if the modulus is associated with all residue
|
|
|
|
* parameters. If a residue is passed with a modulus other than the one it
|
|
|
|
* is associated with, then it may be out of range. If 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:02:33 +02:00
|
|
|
* Copyright The Mbed TLS Contributors
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
|
|
* not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef MBEDTLS_BIGNUM_MOD_H
|
|
|
|
#define MBEDTLS_BIGNUM_MOD_H
|
|
|
|
|
|
|
|
#include "common.h"
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_BIGNUM_C)
|
|
|
|
#include "mbedtls/bignum.h"
|
|
|
|
#endif
|
|
|
|
|
2022-12-20 19:22:44 +01:00
|
|
|
/** 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).
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
typedef enum {
|
2022-12-20 19:22:44 +01:00
|
|
|
/** Representation not chosen (makes the modulus structure invalid). */
|
2022-08-11 15:58:29 +02:00
|
|
|
MBEDTLS_MPI_MOD_REP_INVALID = 0,
|
2022-12-20 19:22:44 +01:00
|
|
|
/* Skip 1 as it is slightly easier to accidentally pass to functions. */
|
|
|
|
/** Montgomery representation. */
|
2022-08-11 15:58:29 +02:00
|
|
|
MBEDTLS_MPI_MOD_REP_MONTGOMERY = 2,
|
2022-12-20 19:22:44 +01:00
|
|
|
/** TODO: document this.
|
|
|
|
*
|
|
|
|
* Residues are in canonical representation.
|
|
|
|
*/
|
|
|
|
MBEDTLS_MPI_MOD_REP_OPT_RED,
|
2022-08-11 15:58:29 +02:00
|
|
|
} mbedtls_mpi_mod_rep_selector;
|
|
|
|
|
|
|
|
/* Make mbedtls_mpi_mod_rep_selector and mbedtls_mpi_mod_ext_rep disjoint to
|
|
|
|
* make it easier to catch when they are accidentally swapped. */
|
2023-01-11 14:50:10 +01:00
|
|
|
typedef enum {
|
2022-08-11 15:58:29 +02:00
|
|
|
MBEDTLS_MPI_MOD_EXT_REP_INVALID = 0,
|
|
|
|
MBEDTLS_MPI_MOD_EXT_REP_LE = 8,
|
|
|
|
MBEDTLS_MPI_MOD_EXT_REP_BE
|
|
|
|
} mbedtls_mpi_mod_ext_rep;
|
2022-07-18 23:02:33 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
typedef struct {
|
2022-07-18 23:02:33 +02:00
|
|
|
mbedtls_mpi_uint *p;
|
2022-08-12 18:09:12 +02:00
|
|
|
size_t limbs;
|
2022-07-18 23:02:33 +02:00
|
|
|
} mbedtls_mpi_mod_residue;
|
|
|
|
|
2022-08-18 17:23:05 +02:00
|
|
|
typedef struct {
|
|
|
|
mbedtls_mpi_uint const *rr; /* The residue for 2^{2*n*biL} mod N */
|
|
|
|
mbedtls_mpi_uint mm; /* Montgomery const for -N^{-1} mod 2^{ciL} */
|
|
|
|
} mbedtls_mpi_mont_struct;
|
|
|
|
|
2022-08-12 15:36:56 +02:00
|
|
|
typedef void *mbedtls_mpi_opt_red_struct;
|
2022-07-18 23:02:33 +02:00
|
|
|
|
|
|
|
typedef struct {
|
2022-08-15 12:50:22 +02:00
|
|
|
const mbedtls_mpi_uint *p;
|
2022-08-12 18:09:12 +02:00
|
|
|
size_t limbs; // number of limbs
|
|
|
|
size_t bits; // bitlen of p
|
2022-08-11 15:58:29 +02:00
|
|
|
mbedtls_mpi_mod_rep_selector int_rep; // selector to signal the active member of the union
|
2023-01-11 14:50:10 +01:00
|
|
|
union rep {
|
2022-12-20 19:22:44 +01:00
|
|
|
/* if int_rep == #MBEDTLS_MPI_MOD_REP_MONTGOMERY */
|
2022-07-18 23:02:33 +02:00
|
|
|
mbedtls_mpi_mont_struct mont;
|
2022-12-20 19:22:44 +01:00
|
|
|
/* if int_rep == #MBEDTLS_MPI_MOD_REP_OPT_RED */
|
2022-07-18 23:02:33 +02:00
|
|
|
mbedtls_mpi_opt_red_struct ored;
|
|
|
|
} rep;
|
|
|
|
} mbedtls_mpi_mod_modulus;
|
|
|
|
|
2022-08-02 17:22:18 +02:00
|
|
|
/** Setup a residue structure.
|
2022-11-24 20:04:54 +01:00
|
|
|
*
|
2022-11-25 16:43:17 +01:00
|
|
|
* The residue will be set up with the buffer \p p and modulus \p m.
|
2022-11-24 20:04:54 +01:00
|
|
|
*
|
2022-11-25 16:43:17 +01:00
|
|
|
* The memory pointed to by \p p will be used by the resulting residue structure.
|
|
|
|
* The value at the pointed-to memory will be the initial value of \p r and must
|
|
|
|
* hold a value that is less than the modulus. This value will be used as-is
|
2022-11-24 20:04:54 +01:00
|
|
|
* and interpreted according to the value of the `m->int_rep` field.
|
|
|
|
*
|
|
|
|
* The modulus \p m will be the modulus associated with \p r. The residue \p r
|
2022-11-26 18:34:37 +01:00
|
|
|
* should only be used in operations where the modulus is \p m.
|
2022-08-02 17:22:18 +02:00
|
|
|
*
|
2022-11-25 16:54:40 +01:00
|
|
|
* \param[out] r The address of the residue to setup.
|
2022-08-19 11:58:34 +02:00
|
|
|
* \param[in] m The address of the modulus related to \p r.
|
2022-11-25 16:43:17 +01:00
|
|
|
* \param[in] p The address of the limb array containing the value of \p r.
|
2022-08-19 11:58:34 +02:00
|
|
|
* The memory pointed to by \p p will be used by \p r and must
|
|
|
|
* not be modified in any way until after
|
2022-11-24 10:09:47 +01:00
|
|
|
* mbedtls_mpi_mod_residue_release() is called. The data
|
2022-11-25 16:43:17 +01:00
|
|
|
* pointed to by \p p must be less than the modulus (the value
|
|
|
|
* pointed to by `m->p`) and already in the representation
|
2022-11-24 20:04:54 +01:00
|
|
|
* indicated by `m->int_rep`.
|
2022-11-25 16:54:40 +01:00
|
|
|
* \param p_limbs The number of limbs of \p p. Must be the same as the number
|
2022-11-26 18:34:37 +01:00
|
|
|
* of limbs in the modulus \p m.
|
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_BAD_INPUT_DATA if \p p_limbs is less than the
|
|
|
|
* limbs in \p m or if \p p is not less than \p m.
|
2022-08-02 17:22:18 +02:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_mpi_mod_residue_setup(mbedtls_mpi_mod_residue *r,
|
|
|
|
const mbedtls_mpi_mod_modulus *m,
|
|
|
|
mbedtls_mpi_uint *p,
|
|
|
|
size_t p_limbs);
|
2022-07-18 23:02:33 +02:00
|
|
|
|
2022-08-02 17:22:18 +02:00
|
|
|
/** Unbind elements of a residue structure.
|
2022-08-08 12:50:02 +02:00
|
|
|
*
|
|
|
|
* This function removes the reference to the limb array that was passed to
|
|
|
|
* mbedtls_mpi_mod_residue_setup() to make it safe to free or use again.
|
|
|
|
*
|
|
|
|
* This function invalidates \p r and it must not be used until after
|
|
|
|
* mbedtls_mpi_mod_residue_setup() is called on it again.
|
2022-08-02 17:22:18 +02:00
|
|
|
*
|
2022-08-19 11:58:34 +02:00
|
|
|
* \param[out] r The address of residue to release.
|
2022-08-02 17:22:18 +02:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_mpi_mod_residue_release(mbedtls_mpi_mod_residue *r);
|
2022-07-18 23:02:33 +02:00
|
|
|
|
2022-08-02 17:22:18 +02:00
|
|
|
/** Initialize a modulus structure.
|
|
|
|
*
|
2022-08-19 13:09:17 +02:00
|
|
|
* \param[out] m The address of the modulus structure to initialize.
|
2022-08-02 17:22:18 +02:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_mpi_mod_modulus_init(mbedtls_mpi_mod_modulus *m);
|
2022-07-18 23:02:33 +02:00
|
|
|
|
2022-08-11 18:42:59 +02:00
|
|
|
/** Setup a modulus structure.
|
2022-08-02 17:22:18 +02:00
|
|
|
*
|
2022-08-19 11:58:34 +02:00
|
|
|
* \param[out] m The address of the modulus structure to populate.
|
|
|
|
* \param[in] p The address of the limb array storing the value of \p m.
|
|
|
|
* The memory pointed to by \p p will be used by \p m and must
|
|
|
|
* not be modified in any way until after
|
2022-08-08 12:50:02 +02:00
|
|
|
* mbedtls_mpi_mod_modulus_free() is called.
|
2022-08-19 13:24:40 +02:00
|
|
|
* \param p_limbs The number of limbs of \p p.
|
2022-08-08 12:50:02 +02:00
|
|
|
* \param int_rep The internal representation to be used for residues
|
|
|
|
* associated with \p m (see #mbedtls_mpi_mod_rep_selector).
|
2022-08-02 17:22:18 +02:00
|
|
|
*
|
|
|
|
* \return \c 0 if successful.
|
2022-11-25 16:54:40 +01:00
|
|
|
* \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if \p int_rep is invalid.
|
2022-08-02 17:22:18 +02:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_mpi_mod_modulus_setup(mbedtls_mpi_mod_modulus *m,
|
|
|
|
const mbedtls_mpi_uint *p,
|
|
|
|
size_t p_limbs,
|
|
|
|
mbedtls_mpi_mod_rep_selector int_rep);
|
2022-07-18 23:02:33 +02:00
|
|
|
|
2022-08-08 12:50:02 +02:00
|
|
|
/** Free elements of a modulus structure.
|
|
|
|
*
|
|
|
|
* This function frees any memory allocated by mbedtls_mpi_mod_modulus_setup().
|
|
|
|
*
|
|
|
|
* \warning This function does not free the limb array passed to
|
|
|
|
* mbedtls_mpi_mod_modulus_setup() only removes the reference to it,
|
|
|
|
* making it safe to free or to use it again.
|
2022-08-02 17:22:18 +02:00
|
|
|
*
|
2022-08-19 13:09:17 +02:00
|
|
|
* \param[in,out] m The address of the modulus structure to free.
|
2022-08-02 17:22:18 +02:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_mpi_mod_modulus_free(mbedtls_mpi_mod_modulus *m);
|
2022-08-02 17:22:18 +02:00
|
|
|
|
2022-11-02 15:35:17 +01:00
|
|
|
/* BEGIN MERGE SLOT 1 */
|
|
|
|
|
|
|
|
/* END MERGE SLOT 1 */
|
|
|
|
|
|
|
|
/* BEGIN MERGE SLOT 2 */
|
|
|
|
|
2022-12-13 10:51:37 +01:00
|
|
|
/** \brief Multiply two residues, returning the residue modulo the specified
|
|
|
|
* modulus.
|
|
|
|
*
|
2022-12-16 15:24:03 +01:00
|
|
|
* \note Currently handles the case when `N->int_rep` is
|
2022-12-13 10:51:37 +01:00
|
|
|
* MBEDTLS_MPI_MOD_REP_MONTGOMERY.
|
|
|
|
*
|
2022-12-16 15:24:03 +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-13 10:51:37 +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).
|
|
|
|
*
|
2022-12-16 15:24:03 +01:00
|
|
|
* \param[out] X The address of the result MPI. Must have the same
|
|
|
|
* number of limbs as \p N.
|
2022-12-13 10:51:37 +01:00
|
|
|
* On successful completion, \p X contains the result of
|
|
|
|
* the multiplication `A * B * R^-1` mod N where
|
2022-12-16 15:24:03 +01:00
|
|
|
* `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-13 10:51:37 +01:00
|
|
|
* \param[in] N The address of the modulus. Used to perform a modulo
|
|
|
|
* operation on the result of the multiplication.
|
|
|
|
*
|
|
|
|
* \return \c 0 if successful.
|
2022-12-16 15:24:03 +01:00
|
|
|
* \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if all the parameters do not
|
2022-12-13 10:51:37 +01:00
|
|
|
* have the same number of limbs or \p N is invalid.
|
|
|
|
* \return #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory-allocation failure.
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_mpi_mod_mul(mbedtls_mpi_mod_residue *X,
|
|
|
|
const mbedtls_mpi_mod_residue *A,
|
|
|
|
const mbedtls_mpi_mod_residue *B,
|
|
|
|
const mbedtls_mpi_mod_modulus *N);
|
2022-12-13 10:51:37 +01:00
|
|
|
|
2022-11-02 15:35:17 +01:00
|
|
|
/* END MERGE SLOT 2 */
|
|
|
|
|
|
|
|
/* BEGIN MERGE SLOT 3 */
|
2022-12-01 15:27:37 +01:00
|
|
|
/**
|
|
|
|
* \brief Perform a fixed-size modular subtraction.
|
|
|
|
*
|
|
|
|
* Calculate `A - B modulo N`.
|
|
|
|
*
|
|
|
|
* \p A, \p B and \p X must all 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.
|
|
|
|
*
|
|
|
|
* \note This function does not check that \p A or \p B are in canonical
|
|
|
|
* form (that is, are < \p N) - that will have been done by
|
|
|
|
* mbedtls_mpi_mod_residue_setup().
|
|
|
|
*
|
|
|
|
* \param[out] X The address of the result MPI. Must be initialized.
|
|
|
|
* Must have the same number of limbs as the modulus \p N.
|
|
|
|
* \param[in] A The address of the first MPI.
|
|
|
|
* \param[in] B The address of the second MPI.
|
|
|
|
* \param[in] N The address of the modulus. Used to perform a modulo
|
|
|
|
* operation on the result of the subtraction.
|
|
|
|
*
|
|
|
|
* \return \c 0 if successful.
|
|
|
|
* \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if the given MPIs do not
|
|
|
|
* have the correct number of limbs.
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_mpi_mod_sub(mbedtls_mpi_mod_residue *X,
|
|
|
|
const mbedtls_mpi_mod_residue *A,
|
|
|
|
const mbedtls_mpi_mod_residue *B,
|
|
|
|
const mbedtls_mpi_mod_modulus *N);
|
2022-12-13 11:46:39 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief Perform modular inversion of an MPI with respect to a modulus \p N.
|
|
|
|
*
|
2022-12-14 10:53:45 +01:00
|
|
|
* \p A and \p X must be associated with the modulus \p N and will therefore
|
|
|
|
* have the same number of limbs as \p N.
|
|
|
|
*
|
2022-12-13 11:46:39 +01:00
|
|
|
* \p X may be aliased to \p A.
|
|
|
|
*
|
|
|
|
* \warning Currently only supports prime moduli, but does not check for them.
|
|
|
|
*
|
|
|
|
* \param[out] X The modular inverse of \p A with respect to \p N.
|
|
|
|
* \param[in] A The number to calculate the modular inverse of.
|
|
|
|
* Must not be 0.
|
|
|
|
* \param[in] N The modulus to use.
|
|
|
|
*
|
|
|
|
* \return \c 0 if successful.
|
|
|
|
* \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if \p A and \p N do not
|
|
|
|
* have the same number of limbs.
|
|
|
|
* \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if \p A is zero.
|
|
|
|
* \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if couldn't allocate enough
|
|
|
|
* memory (needed for conversion to and from Mongtomery form
|
|
|
|
* when not in Montgomery form already, and for temporary use
|
|
|
|
* by the inversion calculation itself).
|
|
|
|
*/
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_mpi_mod_inv(mbedtls_mpi_mod_residue *X,
|
|
|
|
const mbedtls_mpi_mod_residue *A,
|
|
|
|
const mbedtls_mpi_mod_modulus *N);
|
2022-11-02 15:35:17 +01:00
|
|
|
/* END MERGE SLOT 3 */
|
|
|
|
|
|
|
|
/* BEGIN MERGE SLOT 4 */
|
|
|
|
|
|
|
|
/* END MERGE SLOT 4 */
|
|
|
|
|
|
|
|
/* BEGIN MERGE SLOT 5 */
|
2022-11-29 13:25:05 +01:00
|
|
|
/**
|
|
|
|
* \brief Perform a fixed-size modular addition.
|
|
|
|
*
|
|
|
|
* Calculate `A + B modulo N`.
|
|
|
|
*
|
2022-12-13 18:18:17 +01:00
|
|
|
* \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-11-29 13:25:05 +01:00
|
|
|
*
|
|
|
|
* \p X may be aliased to \p A or \p B, or even both, but may not overlap
|
|
|
|
* either otherwise.
|
|
|
|
*
|
|
|
|
* \note This function does not check that \p A or \p B are in canonical
|
|
|
|
* form (that is, are < \p N) - that will have been done by
|
|
|
|
* mbedtls_mpi_mod_residue_setup().
|
|
|
|
*
|
2022-12-13 18:18:17 +01:00
|
|
|
* \param[out] X The address of the result residue. Must be initialized.
|
2022-11-29 13:25:05 +01:00
|
|
|
* Must have the same number of limbs as the modulus \p N.
|
2022-12-13 18:18:17 +01:00
|
|
|
* \param[in] A The address of the first input residue.
|
|
|
|
* \param[in] B The address of the second input residue.
|
2022-11-29 13:25:05 +01:00
|
|
|
* \param[in] N The address of the modulus. Used to perform a modulo
|
|
|
|
* operation on the result of the addition.
|
|
|
|
*
|
|
|
|
* \return \c 0 if successful.
|
|
|
|
* \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if the given MPIs do not
|
|
|
|
* have the correct number of limbs.
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_mpi_mod_add(mbedtls_mpi_mod_residue *X,
|
|
|
|
const mbedtls_mpi_mod_residue *A,
|
|
|
|
const mbedtls_mpi_mod_residue *B,
|
|
|
|
const mbedtls_mpi_mod_modulus *N);
|
2022-11-02 15:35:17 +01:00
|
|
|
/* END MERGE SLOT 5 */
|
|
|
|
|
|
|
|
/* BEGIN MERGE SLOT 6 */
|
|
|
|
|
2022-12-07 22:59:27 +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 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.
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
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);
|
2022-12-07 22:59:27 +01:00
|
|
|
|
2022-11-02 15:35:17 +01:00
|
|
|
/* END MERGE SLOT 6 */
|
|
|
|
|
|
|
|
/* BEGIN MERGE SLOT 7 */
|
2022-11-24 20:04:54 +01:00
|
|
|
/** Read a residue from a byte buffer.
|
|
|
|
*
|
|
|
|
* The residue will be automatically converted to the internal representation
|
2022-11-25 16:43:17 +01:00
|
|
|
* based on the value of the `m->int_rep` field.
|
2022-11-10 15:40:38 +01:00
|
|
|
*
|
2022-11-24 20:04:54 +01:00
|
|
|
* The modulus \p m will be the modulus associated with \p r. The residue \p r
|
|
|
|
* should only be used in operations where the modulus is \p m or a modulus
|
|
|
|
* equivalent to \p m (in the sense that all their fields or memory pointed by
|
|
|
|
* their fields hold the same value).
|
2022-11-10 15:40:38 +01:00
|
|
|
*
|
2022-11-28 15:32:33 +01:00
|
|
|
* \param[out] r The address of the residue. It must have exactly the same
|
2022-11-25 16:43:17 +01:00
|
|
|
* number of limbs as the modulus \p m.
|
2022-11-28 15:32:33 +01:00
|
|
|
* \param[in] m The address of the modulus.
|
|
|
|
* \param[in] buf The input buffer to import from.
|
2022-11-24 19:02:46 +01:00
|
|
|
* \param buflen The length in bytes of \p buf.
|
|
|
|
* \param ext_rep The endianness of the number in the input buffer.
|
2022-11-10 15:40:38 +01:00
|
|
|
*
|
|
|
|
* \return \c 0 if successful.
|
2022-11-24 20:04:54 +01:00
|
|
|
* \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p r isn't
|
2022-11-10 15:40:38 +01:00
|
|
|
* large enough to hold the value in \p buf.
|
2022-11-24 20:04:54 +01:00
|
|
|
* \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if \p ext_rep
|
|
|
|
* is invalid or the value in the buffer is not less than \p m.
|
2022-11-10 15:40:38 +01:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_mpi_mod_read(mbedtls_mpi_mod_residue *r,
|
|
|
|
const mbedtls_mpi_mod_modulus *m,
|
|
|
|
const unsigned char *buf,
|
|
|
|
size_t buflen,
|
|
|
|
mbedtls_mpi_mod_ext_rep ext_rep);
|
2022-11-02 15:35:17 +01:00
|
|
|
|
2022-11-24 20:04:54 +01:00
|
|
|
/** Write a residue into a byte buffer.
|
2022-11-10 15:40:38 +01:00
|
|
|
*
|
2022-11-24 20:04:54 +01:00
|
|
|
* The modulus \p m must be the modulus associated with \p r (see
|
|
|
|
* mbedtls_mpi_mod_residue_setup() and mbedtls_mpi_mod_read()).
|
2022-11-10 15:40:38 +01:00
|
|
|
*
|
2022-11-24 20:04:54 +01:00
|
|
|
* The residue will be automatically converted from the internal representation
|
|
|
|
* based on the value of `m->int_rep` field.
|
|
|
|
*
|
|
|
|
* \warning If the buffer is smaller than `m->bits`, the number of
|
2022-11-26 18:34:37 +01:00
|
|
|
* leading zeroes is leaked through timing. If \p r is
|
2022-11-24 20:04:54 +01:00
|
|
|
* secret, the caller must ensure that \p buflen is at least
|
|
|
|
* (`m->bits`+7)/8.
|
|
|
|
*
|
2022-11-28 15:32:33 +01:00
|
|
|
* \param[in] r The address of the residue. It must have the same number of
|
|
|
|
* limbs as the modulus \p m. (\p r is an input parameter, but
|
|
|
|
* its value will be modified during execution and restored
|
|
|
|
* before the function returns.)
|
|
|
|
* \param[in] m The address of the modulus associated with \r.
|
|
|
|
* \param[out] buf The output buffer to export to.
|
2022-11-24 19:02:46 +01:00
|
|
|
* \param buflen The length in bytes of \p buf.
|
2022-11-24 20:04:54 +01:00
|
|
|
* \param ext_rep The endianness in which the number should be written into
|
|
|
|
* the output buffer.
|
2022-11-10 15:40:38 +01:00
|
|
|
*
|
|
|
|
* \return \c 0 if successful.
|
|
|
|
* \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p buf isn't
|
2022-11-24 20:04:54 +01:00
|
|
|
* large enough to hold the value of \p r (without leading
|
|
|
|
* zeroes).
|
2022-11-25 16:43:17 +01:00
|
|
|
* \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if \p ext_rep is invalid.
|
2022-11-28 15:32:33 +01:00
|
|
|
* \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if couldn't allocate enough
|
|
|
|
* memory for conversion. Can occur only for moduli with
|
|
|
|
* MBEDTLS_MPI_MOD_REP_MONTGOMERY.
|
2022-11-10 15:40:38 +01:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_mpi_mod_write(const mbedtls_mpi_mod_residue *r,
|
|
|
|
const mbedtls_mpi_mod_modulus *m,
|
|
|
|
unsigned char *buf,
|
|
|
|
size_t buflen,
|
|
|
|
mbedtls_mpi_mod_ext_rep ext_rep);
|
2022-11-02 15:35:17 +01:00
|
|
|
/* END MERGE SLOT 7 */
|
|
|
|
|
|
|
|
/* BEGIN MERGE SLOT 8 */
|
|
|
|
|
|
|
|
/* END MERGE SLOT 8 */
|
|
|
|
|
|
|
|
/* BEGIN MERGE SLOT 9 */
|
|
|
|
|
|
|
|
/* END MERGE SLOT 9 */
|
|
|
|
|
|
|
|
/* BEGIN MERGE SLOT 10 */
|
|
|
|
|
|
|
|
/* END MERGE SLOT 10 */
|
|
|
|
|
2022-07-18 23:02:33 +02:00
|
|
|
#endif /* MBEDTLS_BIGNUM_MOD_H */
|