Gabor Mezei | f049dbf | 2022-07-18 23:02:33 +0200 | [diff] [blame] | 1 | /** |
| 2 | * Internal bignum functions |
| 3 | * |
| 4 | * Copyright The Mbed TLS Contributors |
| 5 | * SPDX-License-Identifier: Apache-2.0 |
| 6 | * |
| 7 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 8 | * not use this file except in compliance with the License. |
| 9 | * You may obtain a copy of the License at |
| 10 | * |
| 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | * |
| 13 | * Unless required by applicable law or agreed to in writing, software |
| 14 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 15 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | * See the License for the specific language governing permissions and |
| 17 | * limitations under the License. |
| 18 | */ |
| 19 | |
| 20 | #ifndef MBEDTLS_BIGNUM_MOD_H |
| 21 | #define MBEDTLS_BIGNUM_MOD_H |
| 22 | |
| 23 | #include "common.h" |
| 24 | |
| 25 | #if defined(MBEDTLS_BIGNUM_C) |
| 26 | #include "mbedtls/bignum.h" |
| 27 | #endif |
| 28 | |
| 29 | |
| 30 | typedef struct |
| 31 | { |
| 32 | size_t n; |
| 33 | mbedtls_mpi_uint *p; |
| 34 | } mbedtls_mpi_mod_residue; |
| 35 | |
| 36 | typedef void* mbedtls_mpi_mont_struct; |
| 37 | typedef void* mbedtls_mpi_opt_red_struct; |
| 38 | |
| 39 | typedef struct { |
| 40 | mbedtls_mpi_uint *p; |
| 41 | size_t n; // number of limbs |
| 42 | size_t plen; // bitlen of p |
| 43 | int ext_rep; // signals external representation (eg. byte order) |
| 44 | int int_rep; // selector to signal the active member of the union |
| 45 | union rep |
| 46 | { |
| 47 | mbedtls_mpi_mont_struct mont; |
| 48 | mbedtls_mpi_opt_red_struct ored; |
| 49 | } rep; |
| 50 | } mbedtls_mpi_mod_modulus; |
| 51 | |
Gabor Mezei | c5328cf | 2022-07-18 23:13:13 +0200 | [diff] [blame] | 52 | typedef enum |
| 53 | { |
| 54 | MBEDTLS_MPI_MOD_REP_INVALID = 0, |
| 55 | MBEDTLS_MPI_MOD_REP_MONTGOMERY, |
| 56 | MBEDTLS_MPI_MOD_REP_OPT_RED |
| 57 | } mbedtls_mpi_mod_rep_selector; |
| 58 | |
Janos Follath | 281ccda | 2022-07-19 13:14:36 +0100 | [diff] [blame] | 59 | typedef enum |
| 60 | { |
| 61 | MBEDTLS_MPI_MOD_EXT_REP_INVALID = 0, |
| 62 | MBEDTLS_MPI_MOD_EXT_REP_LE, |
| 63 | MBEDTLS_MPI_MOD_EXT_REP_BE |
| 64 | } mbedtls_mpi_mod_ext_rep; |
Gabor Mezei | c5328cf | 2022-07-18 23:13:13 +0200 | [diff] [blame] | 65 | |
Gabor Mezei | f049dbf | 2022-07-18 23:02:33 +0200 | [diff] [blame] | 66 | |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame^] | 67 | /** Setup a residue structure. |
| 68 | * |
| 69 | * \param r The address of residue to setup. The size is determined by \p m. |
| 70 | * \param m The address of a modulus related to \p r. |
| 71 | * \param p The address of the MPI used for \p r. |
| 72 | * \param pn The number of limbs of \p p. |
| 73 | * |
| 74 | * \return \c 0 if successful. |
| 75 | * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if \p r, \p m or \p p is |
| 76 | * #NULL pointer or if \p p is less then \p m. |
| 77 | */ |
Gabor Mezei | f049dbf | 2022-07-18 23:02:33 +0200 | [diff] [blame] | 78 | int mbedtls_mpi_mod_residue_setup( mbedtls_mpi_mod_residue *r, |
| 79 | mbedtls_mpi_mod_modulus *m, |
Janos Follath | 8b718b5 | 2022-07-25 11:31:02 +0100 | [diff] [blame] | 80 | mbedtls_mpi_uint *p, |
| 81 | size_t pn ); |
Gabor Mezei | f049dbf | 2022-07-18 23:02:33 +0200 | [diff] [blame] | 82 | |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame^] | 83 | /** Unbind elements of a residue structure. |
| 84 | * |
| 85 | * \param r The address of residue to release. |
| 86 | */ |
| 87 | void mbedtls_mpi_mod_residue_release( mbedtls_mpi_mod_residue *r ); |
| 88 | |
| 89 | /** Initialize a modulus structure. |
| 90 | * |
| 91 | * \param m The address of a modulus. |
| 92 | */ |
Gabor Mezei | f049dbf | 2022-07-18 23:02:33 +0200 | [diff] [blame] | 93 | void mbedtls_mpi_mod_modulus_init( mbedtls_mpi_mod_modulus *m ); |
| 94 | |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame^] | 95 | /** Setup a residue structure. |
| 96 | * |
| 97 | * \param m The address of a modulus. |
| 98 | * \param p The address of the MPI used for \p m. |
| 99 | * \param pn The number of limbs of \p p. |
| 100 | * \param ext_rep The external representation of \p m (eg. byte order). |
| 101 | * \param int_rep The selector which representation is used. |
| 102 | * |
| 103 | * \return \c 0 if successful. |
| 104 | * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if \p m or \p p is |
| 105 | * #NULL pointer or if \p ext_rep or \p int_rep is invalid. |
| 106 | */ |
Gabor Mezei | f049dbf | 2022-07-18 23:02:33 +0200 | [diff] [blame] | 107 | int mbedtls_mpi_mod_modulus_setup( mbedtls_mpi_mod_modulus *m, |
| 108 | mbedtls_mpi_uint *p, |
Gabor Mezei | 535f36d | 2022-08-02 11:50:44 +0200 | [diff] [blame] | 109 | size_t pn, |
Gabor Mezei | f049dbf | 2022-07-18 23:02:33 +0200 | [diff] [blame] | 110 | int ext_rep, |
| 111 | int int_rep ); |
| 112 | |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame^] | 113 | /** Unbind elements of a modulus structure. |
| 114 | * |
| 115 | * \param m The address of a modulus. |
| 116 | */ |
| 117 | void mbedtls_mpi_mod_modulus_free( mbedtls_mpi_mod_modulus *m ); |
| 118 | |
Gabor Mezei | f049dbf | 2022-07-18 23:02:33 +0200 | [diff] [blame] | 119 | #endif /* MBEDTLS_BIGNUM_MOD_H */ |