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-09-27 13:12:30 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2022-07-18 23:13:13 +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.
|
|
|
|
*/
|
|
|
|
|
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
|
|
|
*/
|
2022-09-15 20:01:31 +02:00
|
|
|
void mbedtls_mpi_mod_raw_cond_assign( mbedtls_mpi_uint *X,
|
2022-09-27 12:13:51 +02:00
|
|
|
const mbedtls_mpi_uint *A,
|
2022-09-30 13:54:02 +02:00
|
|
|
const mbedtls_mpi_mod_modulus *N,
|
2022-09-15 20:01:31 +02:00
|
|
|
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
|
|
|
*/
|
2022-09-30 13:54:02 +02:00
|
|
|
void mbedtls_mpi_mod_raw_cond_swap( mbedtls_mpi_uint *X,
|
|
|
|
mbedtls_mpi_uint *Y,
|
|
|
|
const mbedtls_mpi_mod_modulus *N,
|
2022-09-15 20:01:31 +02:00
|
|
|
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
|
|
|
*
|
2022-08-19 13:24:40 +02:00
|
|
|
* \param[out] X The address of the MPI. The size is determined by \p m.
|
|
|
|
* (In particular, it must have at least as many limbs as
|
|
|
|
* the modulus \p m.)
|
|
|
|
* \param[in] m The address of the modulus related to \p X.
|
|
|
|
* \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
|
2022-08-08 12:50:02 +02:00
|
|
|
* of \p m is invalid or \p X is not less than \p m.
|
2022-08-02 17:22:18 +02:00
|
|
|
*/
|
2022-07-18 23:13:13 +02:00
|
|
|
int mbedtls_mpi_mod_raw_read( mbedtls_mpi_uint *X,
|
2022-08-19 11:58:34 +02:00
|
|
|
const mbedtls_mpi_mod_modulus *m,
|
2022-08-19 13:24:40 +02:00
|
|
|
const unsigned char *input,
|
2022-11-24 18:42:02 +01:00
|
|
|
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
|
|
|
*
|
2022-08-19 13:24:40 +02:00
|
|
|
* \param[in] A The address of the MPI. The size is determined by \p m.
|
|
|
|
* (In particular, it must have at least as many limbs as
|
|
|
|
* the modulus \p m.)
|
|
|
|
* \param[in] m The address of the modulus related to \p A.
|
|
|
|
* \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
|
2022-08-02 17:22:18 +02:00
|
|
|
* of \p m is invalid.
|
|
|
|
*/
|
2022-08-19 13:24:40 +02:00
|
|
|
int mbedtls_mpi_mod_raw_write( const mbedtls_mpi_uint *A,
|
2022-08-19 11:58:34 +02:00
|
|
|
const mbedtls_mpi_mod_modulus *m,
|
2022-08-19 13:24:40 +02:00
|
|
|
unsigned char *output,
|
2022-11-24 18:42:02 +01:00
|
|
|
size_t output_length,
|
|
|
|
mbedtls_mpi_mod_ext_rep ext_rep );
|
2022-07-18 23:13:13 +02:00
|
|
|
|
2022-11-02 15:35:17 +01:00
|
|
|
/* BEGIN MERGE SLOT 1 */
|
|
|
|
|
|
|
|
/* END MERGE SLOT 1 */
|
|
|
|
|
|
|
|
/* BEGIN MERGE SLOT 2 */
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
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-02 15:35:17 +01:00
|
|
|
/* END MERGE SLOT 2 */
|
|
|
|
|
|
|
|
/* BEGIN MERGE SLOT 3 */
|
|
|
|
|
|
|
|
/* END MERGE SLOT 3 */
|
|
|
|
|
|
|
|
/* BEGIN MERGE SLOT 4 */
|
|
|
|
|
|
|
|
/* END MERGE SLOT 4 */
|
|
|
|
|
|
|
|
/* BEGIN MERGE SLOT 5 */
|
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
|
|
|
*/
|
2022-11-01 14:27:29 +01:00
|
|
|
void mbedtls_mpi_mod_raw_add( mbedtls_mpi_uint *X,
|
2022-11-08 16:53:47 +01:00
|
|
|
const mbedtls_mpi_uint *A,
|
|
|
|
const mbedtls_mpi_uint *B,
|
2022-11-01 14:36:51 +01:00
|
|
|
const mbedtls_mpi_mod_modulus *N );
|
2022-11-02 15:35:17 +01:00
|
|
|
/* END MERGE SLOT 5 */
|
|
|
|
|
|
|
|
/* BEGIN MERGE SLOT 6 */
|
|
|
|
|
|
|
|
/* END MERGE SLOT 6 */
|
|
|
|
|
|
|
|
/* BEGIN MERGE SLOT 7 */
|
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.
|
2022-11-01 17:19:07 +01:00
|
|
|
* Must have the same number of limbs as \p m.
|
|
|
|
* \param m The address of the modulus, which gives the size of
|
|
|
|
* the base `R` = 2^(biL*m->limbs).
|
2022-08-09 15:45:53 +02:00
|
|
|
*
|
|
|
|
* \return \c 0 if successful.
|
|
|
|
*/
|
2022-11-01 17:19:07 +01:00
|
|
|
int mbedtls_mpi_mod_raw_to_mont_rep( mbedtls_mpi_uint *X,
|
|
|
|
const mbedtls_mpi_mod_modulus *m );
|
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.
|
2022-11-01 17:19:07 +01:00
|
|
|
* Must have the same number of limbs as \p m.
|
|
|
|
* \param m The address of the modulus, which gives the size of
|
|
|
|
* the base `R`= 2^(biL*m->limbs).
|
2022-08-09 15:45:53 +02:00
|
|
|
*
|
|
|
|
* \return \c 0 if successful.
|
|
|
|
*/
|
2022-11-01 17:19:07 +01:00
|
|
|
int mbedtls_mpi_mod_raw_from_mont_rep( mbedtls_mpi_uint *X,
|
|
|
|
const mbedtls_mpi_mod_modulus *m );
|
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-19 13:45:13 +02:00
|
|
|
#endif /* MBEDTLS_BIGNUM_MOD_RAW_H */
|