2022-07-18 23:09:45 +02:00
|
|
|
/**
|
2022-08-11 18:42:59 +02:00
|
|
|
* Core bignum functions
|
|
|
|
*
|
2022-08-22 10:06:32 +02:00
|
|
|
* This interface should only be used by the legacy bignum module (bignum.h)
|
2022-08-11 18:42:59 +02:00
|
|
|
* and the modular bignum modules (bignum_mod.c, bignum_mod_raw.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:09:45 +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_CORE_H
|
|
|
|
#define MBEDTLS_BIGNUM_CORE_H
|
|
|
|
|
|
|
|
#include "common.h"
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_BIGNUM_C)
|
|
|
|
#include "mbedtls/bignum.h"
|
|
|
|
#endif
|
|
|
|
|
2022-08-31 18:16:50 +02:00
|
|
|
#define ciL ( sizeof(mbedtls_mpi_uint) ) /* chars in limb */
|
|
|
|
#define biL ( ciL << 3 ) /* bits in limb */
|
|
|
|
#define biH ( ciL << 2 ) /* half limb size */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Convert between bits/chars and number of limbs
|
|
|
|
* Divide first in order to avoid potential overflows
|
|
|
|
*/
|
|
|
|
#define BITS_TO_LIMBS(i) ( (i) / biL + ( (i) % biL != 0 ) )
|
|
|
|
#define CHARS_TO_LIMBS(i) ( (i) / ciL + ( (i) % ciL != 0 ) )
|
|
|
|
/* Get a specific byte, without range checks. */
|
|
|
|
#define GET_BYTE( X, i ) \
|
|
|
|
( ( (X)[(i) / ciL] >> ( ( (i) % ciL ) * 8 ) ) & 0xff )
|
|
|
|
|
2022-08-02 17:22:18 +02:00
|
|
|
/** Count leading zero bits in a given integer.
|
|
|
|
*
|
2022-08-19 13:24:40 +02:00
|
|
|
* \param a Integer to count leading zero bits.
|
2022-08-02 17:22:18 +02:00
|
|
|
*
|
2022-08-19 13:24:40 +02:00
|
|
|
* \return The number of leading zero bits in \p a.
|
2022-08-02 17:22:18 +02:00
|
|
|
*/
|
2022-08-22 12:19:10 +02:00
|
|
|
size_t mbedtls_mpi_core_clz( mbedtls_mpi_uint a );
|
2022-07-21 19:25:42 +02:00
|
|
|
|
2022-08-19 13:09:17 +02:00
|
|
|
/** Return the minimum number of bits required to represent the value held
|
2022-08-11 18:42:59 +02:00
|
|
|
* in the MPI.
|
|
|
|
*
|
2022-08-19 13:24:40 +02:00
|
|
|
* \note This function returns 0 if all the limbs of \p A are 0.
|
2022-08-02 17:22:18 +02:00
|
|
|
*
|
2022-08-19 13:24:40 +02:00
|
|
|
* \param[in] A The address of the MPI.
|
2022-08-22 11:01:27 +02:00
|
|
|
* \param A_limbs The number of limbs of \p A.
|
2022-08-02 17:22:18 +02:00
|
|
|
*
|
2022-08-19 13:24:40 +02:00
|
|
|
* \return The number of bits in \p A.
|
2022-08-02 17:22:18 +02:00
|
|
|
*/
|
2022-08-22 10:06:32 +02:00
|
|
|
size_t mbedtls_mpi_core_bitlen( const mbedtls_mpi_uint *A, size_t A_limbs );
|
2022-07-21 19:25:42 +02:00
|
|
|
|
2022-08-02 17:22:18 +02:00
|
|
|
/** Convert a big-endian byte array aligned to the size of mbedtls_mpi_uint
|
|
|
|
* into the storage form used by mbedtls_mpi.
|
|
|
|
*
|
2022-08-19 13:24:40 +02:00
|
|
|
* \param[in,out] A The address of the MPI.
|
2022-08-22 11:01:27 +02:00
|
|
|
* \param A_limbs The number of limbs of \p A.
|
2022-08-02 17:22:18 +02:00
|
|
|
*/
|
2022-08-19 13:24:40 +02:00
|
|
|
void mbedtls_mpi_core_bigendian_to_host( mbedtls_mpi_uint *A,
|
2022-08-22 11:01:27 +02:00
|
|
|
size_t A_limbs );
|
2022-07-21 19:25:42 +02:00
|
|
|
|
2022-08-22 10:06:32 +02:00
|
|
|
/** Import X from unsigned binary data, little-endian.
|
2022-08-02 17:22:18 +02:00
|
|
|
*
|
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.
|
|
|
|
* \param X_limbs The number of limbs of \p X.
|
|
|
|
* \param[in] input The input buffer to import from.
|
|
|
|
* \param input_length The length bytes of \p input.
|
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-02 17:22:18 +02:00
|
|
|
*/
|
2022-07-18 23:09:45 +02:00
|
|
|
int mbedtls_mpi_core_read_le( mbedtls_mpi_uint *X,
|
2022-08-19 13:24:40 +02:00
|
|
|
size_t X_limbs,
|
|
|
|
const unsigned char *input,
|
|
|
|
size_t input_length );
|
2022-07-18 23:09:45 +02:00
|
|
|
|
2022-08-22 10:06:32 +02:00
|
|
|
/** Import X from unsigned binary data, big-endian.
|
2022-08-02 17:22:18 +02:00
|
|
|
*
|
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.
|
|
|
|
* May only be #NULL if \X_limbs is 0 and \p input_length
|
|
|
|
* is 0.
|
|
|
|
* \param X_limbs The number of limbs of \p X.
|
|
|
|
* \param[in] input The input buffer to import from.
|
|
|
|
* May only be #NULL if \p input_length is 0.
|
|
|
|
* \param input_length The length in bytes of \p input.
|
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-02 17:22:18 +02:00
|
|
|
*/
|
2022-07-18 23:09:45 +02:00
|
|
|
int mbedtls_mpi_core_read_be( mbedtls_mpi_uint *X,
|
2022-08-19 13:24:40 +02:00
|
|
|
size_t X_limbs,
|
|
|
|
const unsigned char *input,
|
|
|
|
size_t input_length );
|
2022-07-18 23:09:45 +02:00
|
|
|
|
2022-08-22 10:06:32 +02:00
|
|
|
/** Export A into unsigned binary data, little-endian.
|
2022-08-11 18:42:59 +02:00
|
|
|
*
|
2022-08-19 13:24:40 +02:00
|
|
|
* \note If \p output is shorter than \p A the export is still successful if the
|
|
|
|
* value held in \p A fits in the buffer (that is, if enough of the most
|
|
|
|
* significant bytes of \p A are 0).
|
2022-08-02 17:22:18 +02:00
|
|
|
*
|
2022-08-19 13:24:40 +02:00
|
|
|
* \param[in] A The address of the MPI.
|
|
|
|
* \param A_limbs The number of limbs of \p A.
|
|
|
|
* \param[out] output The output buffer to export to.
|
|
|
|
* \param output_length The length in bytes of \p output.
|
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-02 17:22:18 +02:00
|
|
|
*/
|
2022-08-19 13:24:40 +02:00
|
|
|
int mbedtls_mpi_core_write_le( const mbedtls_mpi_uint *A,
|
|
|
|
size_t A_limbs,
|
|
|
|
unsigned char *output,
|
|
|
|
size_t output_length );
|
2022-07-18 23:09:45 +02:00
|
|
|
|
2022-08-22 10:06:32 +02:00
|
|
|
/** Export A into unsigned binary data, big-endian.
|
2022-08-11 18:42:59 +02:00
|
|
|
*
|
2022-08-19 13:24:40 +02:00
|
|
|
* \note If \p output is shorter than \p A the export is still successful if the
|
|
|
|
* value held in \p A fits in the buffer (that is, if enough of the most
|
|
|
|
* significant bytes of \p A are 0).
|
2022-08-02 17:22:18 +02:00
|
|
|
*
|
2022-08-19 13:24:40 +02:00
|
|
|
* \param[in] A The address of the MPI.
|
|
|
|
* \param A_limbs The number of limbs of \p A.
|
|
|
|
* \param[out] output The output buffer to export to.
|
|
|
|
* \param output_length The length in bytes of \p output.
|
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-02 17:22:18 +02:00
|
|
|
*/
|
2022-08-19 13:24:40 +02:00
|
|
|
int mbedtls_mpi_core_write_be( const mbedtls_mpi_uint *A,
|
|
|
|
size_t A_limbs,
|
|
|
|
unsigned char *output,
|
|
|
|
size_t output_length );
|
2022-07-18 23:09:45 +02:00
|
|
|
|
2022-08-23 17:15:19 +02:00
|
|
|
/**
|
2022-09-15 16:46:10 +02:00
|
|
|
* \brief Conditional addition of two fixed-size large unsigned integers,
|
2022-08-30 12:57:22 +02:00
|
|
|
* returning the carry.
|
2022-08-17 07:17:00 +02:00
|
|
|
*
|
2022-08-30 12:57:22 +02:00
|
|
|
* Functionally equivalent to
|
2022-08-17 07:17:00 +02:00
|
|
|
*
|
2022-08-30 12:57:22 +02:00
|
|
|
* ```
|
|
|
|
* if( cond )
|
2022-09-15 16:55:07 +02:00
|
|
|
* X += A;
|
2022-08-30 12:57:22 +02:00
|
|
|
* return carry;
|
|
|
|
* ```
|
2022-08-24 12:51:58 +02:00
|
|
|
*
|
2022-09-20 14:51:50 +02:00
|
|
|
* This function operates modulo `2^(biL*limbs)`.
|
|
|
|
*
|
2022-09-15 16:55:07 +02:00
|
|
|
* \param[in,out] X The pointer to the (little-endian) array
|
2022-08-24 12:51:58 +02:00
|
|
|
* representing the bignum to accumulate onto.
|
2022-09-15 16:55:07 +02:00
|
|
|
* \param[in] A The pointer to the (little-endian) array
|
2022-08-30 12:57:22 +02:00
|
|
|
* representing the bignum to conditionally add
|
2022-09-15 16:55:07 +02:00
|
|
|
* to \p X. This may be aliased to \p X but may not
|
2022-08-31 12:35:00 +02:00
|
|
|
* overlap otherwise.
|
2022-09-15 16:55:07 +02:00
|
|
|
* \param limbs Number of limbs of \p X and \p A.
|
2022-08-30 12:57:22 +02:00
|
|
|
* \param cond Condition bit dictating whether addition should
|
|
|
|
* happen or not. This must be \c 0 or \c 1.
|
2022-08-24 12:51:58 +02:00
|
|
|
*
|
2022-08-30 12:57:22 +02:00
|
|
|
* \warning If \p cond is neither 0 nor 1, the result of this function
|
2022-09-15 16:55:07 +02:00
|
|
|
* is unspecified, and the resulting value in \p X might be
|
|
|
|
* neither its original value nor \p X + \p A.
|
2022-08-30 12:57:22 +02:00
|
|
|
*
|
2022-09-15 16:55:07 +02:00
|
|
|
* \return 1 if `X + cond * A >= 2^(biL*limbs)`, 0 otherwise.
|
2022-08-23 13:09:35 +02:00
|
|
|
*/
|
2022-09-15 16:55:07 +02:00
|
|
|
mbedtls_mpi_uint mbedtls_mpi_core_add_if( mbedtls_mpi_uint *X,
|
|
|
|
const mbedtls_mpi_uint *A,
|
2022-08-30 12:57:22 +02:00
|
|
|
size_t limbs,
|
|
|
|
unsigned cond );
|
2022-08-23 13:09:35 +02:00
|
|
|
|
|
|
|
/**
|
2022-09-15 16:46:10 +02:00
|
|
|
* \brief Subtract two fixed-size large unsigned integers, returning the borrow.
|
2022-08-23 13:09:35 +02:00
|
|
|
*
|
2022-08-30 15:31:49 +02:00
|
|
|
* Calculate `A - B` where \p A and \p B have the same size.
|
2022-08-31 18:09:29 +02:00
|
|
|
* This function operates modulo `2^(biL*limbs)` and returns the carry
|
2022-08-24 12:51:58 +02:00
|
|
|
* (1 if there was a wraparound, i.e. if `A < B`, and 0 otherwise).
|
2022-08-23 13:09:35 +02:00
|
|
|
*
|
2022-08-30 15:31:49 +02:00
|
|
|
* \p X may be aliased to \p A or \p B, or even both, but may not overlap
|
|
|
|
* either otherwise.
|
2022-08-23 13:09:35 +02:00
|
|
|
*
|
2022-08-24 12:51:58 +02:00
|
|
|
* \param[out] X The result of the subtraction.
|
|
|
|
* \param[in] A Little-endian presentation of left operand.
|
|
|
|
* \param[in] B Little-endian presentation of right operand.
|
|
|
|
* \param limbs Number of limbs of \p X, \p A and \p B.
|
2022-08-23 13:09:35 +02:00
|
|
|
*
|
2022-08-24 12:51:58 +02:00
|
|
|
* \return 1 if `A < B`.
|
|
|
|
* 0 if `A >= B`.
|
2022-08-23 13:09:35 +02:00
|
|
|
*/
|
2022-08-24 12:51:58 +02:00
|
|
|
mbedtls_mpi_uint mbedtls_mpi_core_sub( mbedtls_mpi_uint *X,
|
|
|
|
const mbedtls_mpi_uint *A,
|
|
|
|
const mbedtls_mpi_uint *B,
|
|
|
|
size_t limbs );
|
2022-08-23 13:09:35 +02:00
|
|
|
|
|
|
|
/**
|
2022-09-15 16:55:07 +02:00
|
|
|
* \brief Perform a fixed-size multiply accumulate operation: X += b * A
|
2022-08-23 13:09:35 +02:00
|
|
|
*
|
2022-09-20 14:33:40 +02:00
|
|
|
* \p X may be aliased to \p A (when \p X_limbs == \p A_limbs), but may not
|
|
|
|
* otherwise overlap.
|
|
|
|
*
|
2022-09-20 14:51:50 +02:00
|
|
|
* This function operates modulo `2^(biL*X_limbs)`.
|
|
|
|
*
|
2022-09-15 16:55:07 +02:00
|
|
|
* \param[in,out] X The pointer to the (little-endian) array
|
2022-08-24 12:51:58 +02:00
|
|
|
* representing the bignum to accumulate onto.
|
2022-09-15 16:55:07 +02:00
|
|
|
* \param X_limbs The number of limbs of \p X. This must be
|
|
|
|
* at least \p A_limbs.
|
|
|
|
* \param[in] A The pointer to the (little-endian) array
|
2022-08-30 12:57:22 +02:00
|
|
|
* representing the bignum to multiply with.
|
2022-09-15 16:55:07 +02:00
|
|
|
* This may be aliased to \p X but may not overlap
|
2022-08-31 12:35:00 +02:00
|
|
|
* otherwise.
|
2022-09-15 16:55:07 +02:00
|
|
|
* \param A_limbs The number of limbs of \p A.
|
|
|
|
* \param b X scalar to multiply with.
|
2022-08-23 13:09:35 +02:00
|
|
|
*
|
2022-08-30 12:57:22 +02:00
|
|
|
* \return The carry at the end of the operation.
|
|
|
|
*/
|
2022-09-15 16:55:07 +02:00
|
|
|
mbedtls_mpi_uint mbedtls_mpi_core_mla( mbedtls_mpi_uint *X, size_t X_limbs,
|
|
|
|
const mbedtls_mpi_uint *A, size_t A_limbs,
|
|
|
|
mbedtls_mpi_uint b );
|
2022-08-30 12:57:22 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief Calculate initialisation value for fast Montgomery modular
|
|
|
|
* multiplication
|
2022-08-24 12:51:58 +02:00
|
|
|
*
|
2022-08-30 12:57:22 +02:00
|
|
|
* \param[in] N Little-endian presentation of the modulus. This must have
|
|
|
|
* at least one limb.
|
|
|
|
*
|
|
|
|
* \return The initialisation value for fast Montgomery modular multiplication
|
2022-08-23 13:09:35 +02:00
|
|
|
*/
|
2022-09-15 16:05:59 +02:00
|
|
|
mbedtls_mpi_uint mbedtls_mpi_core_montmul_init( const mbedtls_mpi_uint *N );
|
2022-08-30 12:57:22 +02:00
|
|
|
|
|
|
|
/**
|
2022-09-29 15:40:21 +02:00
|
|
|
* \brief Montgomery multiplication: X = A * B * R^-1 mod N (HAC 14.36)
|
|
|
|
*
|
|
|
|
* \p A and \p B must be in canonical form. That is, < \p N.
|
2022-08-30 12:57:22 +02:00
|
|
|
*
|
2022-09-20 14:17:51 +02:00
|
|
|
* \p X may be aliased to \p A or \p N, or even \p B (if \p AN_limbs ==
|
|
|
|
* \p B_limbs) but may not overlap any parameters otherwise.
|
|
|
|
*
|
2022-09-29 15:40:21 +02:00
|
|
|
* \p A and \p B may alias each other, if \p AN_limbs == \p B_limbs. They may
|
|
|
|
* not alias \p N (since they must be in canonical form, they cannot == \p N).
|
2022-09-20 14:17:51 +02:00
|
|
|
*
|
2022-08-30 12:57:22 +02:00
|
|
|
* \param[out] X The destination MPI, as a little-endian array of
|
|
|
|
* length \p AN_limbs.
|
|
|
|
* On successful completion, X contains the result of
|
2022-08-31 18:09:29 +02:00
|
|
|
* the multiplication `A * B * R^-1` mod N where
|
|
|
|
* `R = 2^(biL*AN_limbs)`.
|
2022-08-30 12:57:22 +02:00
|
|
|
* \param[in] A Little-endian presentation of first operand.
|
2022-08-30 15:31:49 +02:00
|
|
|
* Must have the same number of limbs as \p N.
|
2022-08-30 12:57:22 +02:00
|
|
|
* \param[in] B Little-endian presentation of second operand.
|
|
|
|
* \param[in] B_limbs The number of limbs in \p B.
|
2022-08-30 15:31:49 +02:00
|
|
|
* Must be <= \p AN_limbs.
|
2022-08-30 12:57:22 +02:00
|
|
|
* \param[in] N Little-endian presentation of the modulus.
|
2022-08-30 15:31:49 +02:00
|
|
|
* This must be odd, and have exactly the same number
|
|
|
|
* of limbs as \p A.
|
2022-09-29 18:20:18 +02:00
|
|
|
* It may alias \p X, but must not alias or otherwise
|
|
|
|
* overlap any of the other parameters.
|
2022-08-30 15:31:49 +02:00
|
|
|
* \param[in] AN_limbs The number of limbs in \p X, \p A and \p N.
|
2022-08-31 18:09:29 +02:00
|
|
|
* \param mm The Montgomery constant for \p N: -N^-1 mod 2^biL.
|
2022-09-15 16:05:59 +02:00
|
|
|
* This can be calculated by `mbedtls_mpi_core_montmul_init()`.
|
2022-08-30 12:57:22 +02:00
|
|
|
* \param[in,out] T Temporary storage of size at least 2*AN_limbs+1 limbs.
|
|
|
|
* Its initial content is unused and
|
|
|
|
* its final content is indeterminate.
|
2022-09-29 15:40:21 +02:00
|
|
|
* It must not alias or otherwise overlap any of the
|
|
|
|
* other parameters.
|
2022-08-30 12:57:22 +02:00
|
|
|
*/
|
|
|
|
void mbedtls_mpi_core_montmul( mbedtls_mpi_uint *X,
|
|
|
|
const mbedtls_mpi_uint *A,
|
|
|
|
const mbedtls_mpi_uint *B, size_t B_limbs,
|
|
|
|
const mbedtls_mpi_uint *N, size_t AN_limbs,
|
|
|
|
mbedtls_mpi_uint mm, mbedtls_mpi_uint *T );
|
2022-08-23 13:09:35 +02:00
|
|
|
|
2022-07-18 23:09:45 +02:00
|
|
|
#endif /* MBEDTLS_BIGNUM_CORE_H */
|