Gabor Mezei | f049dbf | 2022-07-18 23:02:33 +0200 | [diff] [blame] | 1 | /** |
Janos Follath | 6318468 | 2022-08-11 17:42:59 +0100 | [diff] [blame] | 2 | * Modular bignum functions |
Gabor Mezei | f049dbf | 2022-07-18 23:02:33 +0200 | [diff] [blame] | 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 | |
Janos Follath | 296ea66 | 2022-08-11 14:58:29 +0100 | [diff] [blame] | 29 | /* Skip 1 as it is slightly easier to accidentally pass to functions. */ |
| 30 | typedef enum |
| 31 | { |
| 32 | MBEDTLS_MPI_MOD_REP_INVALID = 0, |
| 33 | MBEDTLS_MPI_MOD_REP_MONTGOMERY = 2, |
| 34 | MBEDTLS_MPI_MOD_REP_OPT_RED |
| 35 | } mbedtls_mpi_mod_rep_selector; |
| 36 | |
| 37 | /* Make mbedtls_mpi_mod_rep_selector and mbedtls_mpi_mod_ext_rep disjoint to |
| 38 | * make it easier to catch when they are accidentally swapped. */ |
| 39 | typedef enum |
| 40 | { |
| 41 | MBEDTLS_MPI_MOD_EXT_REP_INVALID = 0, |
| 42 | MBEDTLS_MPI_MOD_EXT_REP_LE = 8, |
| 43 | MBEDTLS_MPI_MOD_EXT_REP_BE |
| 44 | } mbedtls_mpi_mod_ext_rep; |
Gabor Mezei | f049dbf | 2022-07-18 23:02:33 +0200 | [diff] [blame] | 45 | |
| 46 | typedef struct |
| 47 | { |
Gabor Mezei | f049dbf | 2022-07-18 23:02:33 +0200 | [diff] [blame] | 48 | mbedtls_mpi_uint *p; |
Gabor Mezei | fd65e82 | 2022-08-12 18:09:12 +0200 | [diff] [blame] | 49 | size_t limbs; |
Gabor Mezei | f049dbf | 2022-07-18 23:02:33 +0200 | [diff] [blame] | 50 | } mbedtls_mpi_mod_residue; |
| 51 | |
Gabor Mezei | 89e3146 | 2022-08-12 15:36:56 +0200 | [diff] [blame] | 52 | typedef void *mbedtls_mpi_mont_struct; |
| 53 | typedef void *mbedtls_mpi_opt_red_struct; |
Gabor Mezei | f049dbf | 2022-07-18 23:02:33 +0200 | [diff] [blame] | 54 | |
| 55 | typedef struct { |
Janos Follath | ed5c8d3 | 2022-08-15 11:50:22 +0100 | [diff] [blame] | 56 | const mbedtls_mpi_uint *p; |
Gabor Mezei | fd65e82 | 2022-08-12 18:09:12 +0200 | [diff] [blame] | 57 | size_t limbs; // number of limbs |
| 58 | size_t bits; // bitlen of p |
Janos Follath | 296ea66 | 2022-08-11 14:58:29 +0100 | [diff] [blame] | 59 | mbedtls_mpi_mod_ext_rep ext_rep; // signals external representation (eg. byte order) |
| 60 | mbedtls_mpi_mod_rep_selector int_rep; // selector to signal the active member of the union |
Gabor Mezei | f049dbf | 2022-07-18 23:02:33 +0200 | [diff] [blame] | 61 | union rep |
| 62 | { |
| 63 | mbedtls_mpi_mont_struct mont; |
| 64 | mbedtls_mpi_opt_red_struct ored; |
| 65 | } rep; |
| 66 | } mbedtls_mpi_mod_modulus; |
| 67 | |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 68 | /** Setup a residue structure. |
| 69 | * |
| 70 | * \param r The address of residue to setup. The size is determined by \p m. |
Janos Follath | dae1147 | 2022-08-08 11:50:02 +0100 | [diff] [blame] | 71 | * (In particular, it must have at least as many limbs as the |
| 72 | * modulus \p m.) |
| 73 | * \param m The address of the modulus related to \p r. |
| 74 | * \param p The address of the limb array storing the value of \p r. The |
Janos Follath | 6318468 | 2022-08-11 17:42:59 +0100 | [diff] [blame] | 75 | * memory pointed to by \p p will be used by \p r and must not be |
| 76 | * modified in any way until after mbedtls_mpi_mod_residue_release() |
Janos Follath | dae1147 | 2022-08-08 11:50:02 +0100 | [diff] [blame] | 77 | * is called. |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 78 | * \param pn The number of limbs of \p p. |
| 79 | * |
| 80 | * \return \c 0 if successful. |
Janos Follath | 56a10f9 | 2022-08-11 15:19:00 +0100 | [diff] [blame] | 81 | * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if \p pn is less than the limbs |
| 82 | * in \p m or if \p p is not less than \p m. |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 83 | */ |
Gabor Mezei | f049dbf | 2022-07-18 23:02:33 +0200 | [diff] [blame] | 84 | int mbedtls_mpi_mod_residue_setup( mbedtls_mpi_mod_residue *r, |
| 85 | mbedtls_mpi_mod_modulus *m, |
Janos Follath | 8b718b5 | 2022-07-25 11:31:02 +0100 | [diff] [blame] | 86 | mbedtls_mpi_uint *p, |
| 87 | size_t pn ); |
Gabor Mezei | f049dbf | 2022-07-18 23:02:33 +0200 | [diff] [blame] | 88 | |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 89 | /** Unbind elements of a residue structure. |
| 90 | * |
Janos Follath | dae1147 | 2022-08-08 11:50:02 +0100 | [diff] [blame] | 91 | * This function removes the reference to the limb array that was passed to |
| 92 | * mbedtls_mpi_mod_residue_setup() to make it safe to free or use again. |
| 93 | * |
| 94 | * This function invalidates \p r and it must not be used until after |
| 95 | * mbedtls_mpi_mod_residue_setup() is called on it again. |
| 96 | * |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 97 | * \param r The address of residue to release. |
| 98 | */ |
| 99 | void mbedtls_mpi_mod_residue_release( mbedtls_mpi_mod_residue *r ); |
| 100 | |
| 101 | /** Initialize a modulus structure. |
| 102 | * |
| 103 | * \param m The address of a modulus. |
| 104 | */ |
Gabor Mezei | f049dbf | 2022-07-18 23:02:33 +0200 | [diff] [blame] | 105 | void mbedtls_mpi_mod_modulus_init( mbedtls_mpi_mod_modulus *m ); |
| 106 | |
Janos Follath | 6318468 | 2022-08-11 17:42:59 +0100 | [diff] [blame] | 107 | /** Setup a modulus structure. |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 108 | * |
Janos Follath | 6318468 | 2022-08-11 17:42:59 +0100 | [diff] [blame] | 109 | * \param m The address of the modulus structure to populate. |
Janos Follath | dae1147 | 2022-08-08 11:50:02 +0100 | [diff] [blame] | 110 | * \param p The address of the limb array storing the value of \p m. The |
Janos Follath | 6318468 | 2022-08-11 17:42:59 +0100 | [diff] [blame] | 111 | * memory pointed to by \p p will be used by \p m and must not |
| 112 | * be modified in any way until after |
Janos Follath | dae1147 | 2022-08-08 11:50:02 +0100 | [diff] [blame] | 113 | * mbedtls_mpi_mod_modulus_free() is called. |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 114 | * \param pn The number of limbs of \p p. |
Janos Follath | dae1147 | 2022-08-08 11:50:02 +0100 | [diff] [blame] | 115 | * \param ext_rep The external representation to be used for residues |
| 116 | * associated with \p m (see #mbedtls_mpi_mod_ext_rep). |
| 117 | * \param int_rep The internal representation to be used for residues |
| 118 | * associated with \p m (see #mbedtls_mpi_mod_rep_selector). |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 119 | * |
| 120 | * \return \c 0 if successful. |
Janos Follath | 56a10f9 | 2022-08-11 15:19:00 +0100 | [diff] [blame] | 121 | * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if \p ext_rep or \p int_rep is |
| 122 | * invalid. |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 123 | */ |
Gabor Mezei | f049dbf | 2022-07-18 23:02:33 +0200 | [diff] [blame] | 124 | int mbedtls_mpi_mod_modulus_setup( mbedtls_mpi_mod_modulus *m, |
Janos Follath | ed5c8d3 | 2022-08-15 11:50:22 +0100 | [diff] [blame] | 125 | const mbedtls_mpi_uint *p, |
Gabor Mezei | 535f36d | 2022-08-02 11:50:44 +0200 | [diff] [blame] | 126 | size_t pn, |
Janos Follath | 296ea66 | 2022-08-11 14:58:29 +0100 | [diff] [blame] | 127 | mbedtls_mpi_mod_ext_rep ext_rep, |
| 128 | mbedtls_mpi_mod_rep_selector int_rep ); |
Gabor Mezei | f049dbf | 2022-07-18 23:02:33 +0200 | [diff] [blame] | 129 | |
Janos Follath | dae1147 | 2022-08-08 11:50:02 +0100 | [diff] [blame] | 130 | /** Free elements of a modulus structure. |
| 131 | * |
| 132 | * This function frees any memory allocated by mbedtls_mpi_mod_modulus_setup(). |
| 133 | * |
| 134 | * \warning This function does not free the limb array passed to |
| 135 | * mbedtls_mpi_mod_modulus_setup() only removes the reference to it, |
| 136 | * making it safe to free or to use it again. |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 137 | * |
| 138 | * \param m The address of a modulus. |
| 139 | */ |
| 140 | void mbedtls_mpi_mod_modulus_free( mbedtls_mpi_mod_modulus *m ); |
| 141 | |
Gabor Mezei | f049dbf | 2022-07-18 23:02:33 +0200 | [diff] [blame] | 142 | #endif /* MBEDTLS_BIGNUM_MOD_H */ |