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-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-08-11 15:58:29 +02:00
|
|
|
/* Skip 1 as it is slightly easier to accidentally pass to functions. */
|
|
|
|
typedef enum
|
|
|
|
{
|
|
|
|
MBEDTLS_MPI_MOD_REP_INVALID = 0,
|
|
|
|
MBEDTLS_MPI_MOD_REP_MONTGOMERY = 2,
|
|
|
|
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
|
|
|
|
* make it easier to catch when they are accidentally swapped. */
|
|
|
|
typedef enum
|
|
|
|
{
|
|
|
|
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
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
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-12 15:36:56 +02:00
|
|
|
typedef void *mbedtls_mpi_mont_struct;
|
|
|
|
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_ext_rep ext_rep; // signals external representation (eg. byte order)
|
|
|
|
mbedtls_mpi_mod_rep_selector int_rep; // selector to signal the active member of the union
|
2022-07-18 23:02:33 +02:00
|
|
|
union rep
|
|
|
|
{
|
|
|
|
mbedtls_mpi_mont_struct mont;
|
|
|
|
mbedtls_mpi_opt_red_struct ored;
|
|
|
|
} rep;
|
|
|
|
} mbedtls_mpi_mod_modulus;
|
|
|
|
|
2022-08-02 17:22:18 +02:00
|
|
|
/** Setup a residue structure.
|
|
|
|
*
|
2022-08-19 11:58:34 +02:00
|
|
|
* \param[out] r The address of residue to setup. 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 r.
|
|
|
|
* \param[in] p The address of the limb array storing the value of \p r.
|
|
|
|
* The memory pointed to by \p p will be used by \p r and must
|
|
|
|
* not be modified in any way until after
|
|
|
|
* mbedtls_mpi_mod_residue_release() is called.
|
2022-08-19 13:24:40 +02:00
|
|
|
* \param p_limbs The number of limbs of \p p.
|
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
|
|
|
*/
|
2022-07-18 23:02:33 +02:00
|
|
|
int mbedtls_mpi_mod_residue_setup( mbedtls_mpi_mod_residue *r,
|
2022-08-19 11:58:34 +02:00
|
|
|
const mbedtls_mpi_mod_modulus *m,
|
2022-07-25 12:31:02 +02:00
|
|
|
mbedtls_mpi_uint *p,
|
2022-08-19 13:24:40 +02:00
|
|
|
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
|
|
|
*/
|
|
|
|
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
|
|
|
*/
|
|
|
|
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 ext_rep The external representation to be used for residues
|
|
|
|
* associated with \p m (see #mbedtls_mpi_mod_ext_rep).
|
|
|
|
* \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-08-11 16:19:00 +02:00
|
|
|
* \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if \p ext_rep or \p int_rep is
|
|
|
|
* invalid.
|
2022-08-02 17:22:18 +02:00
|
|
|
*/
|
2022-07-18 23:02:33 +02:00
|
|
|
int mbedtls_mpi_mod_modulus_setup( mbedtls_mpi_mod_modulus *m,
|
2022-08-15 12:50:22 +02:00
|
|
|
const mbedtls_mpi_uint *p,
|
2022-08-19 13:24:40 +02:00
|
|
|
size_t p_limbs,
|
2022-08-11 15:58:29 +02:00
|
|
|
mbedtls_mpi_mod_ext_rep ext_rep,
|
|
|
|
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
|
|
|
*/
|
|
|
|
void mbedtls_mpi_mod_modulus_free( mbedtls_mpi_mod_modulus *m );
|
|
|
|
|
2022-07-18 23:02:33 +02:00
|
|
|
#endif /* MBEDTLS_BIGNUM_MOD_H */
|