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 |
Gilles Peskine | 7aab2fb | 2022-09-27 13:19:13 +0200 | [diff] [blame^] | 3 | * |
| 4 | * This module implements operations on integers modulo some fixed modulus. |
Gilles Peskine | 7f887bd | 2022-09-27 13:12:30 +0200 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | /* |
Gabor Mezei | f049dbf | 2022-07-18 23:02:33 +0200 | [diff] [blame] | 8 | * Copyright The Mbed TLS Contributors |
| 9 | * SPDX-License-Identifier: Apache-2.0 |
| 10 | * |
| 11 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 12 | * not use this file except in compliance with the License. |
| 13 | * You may obtain a copy of the License at |
| 14 | * |
| 15 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 16 | * |
| 17 | * Unless required by applicable law or agreed to in writing, software |
| 18 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 19 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 20 | * See the License for the specific language governing permissions and |
| 21 | * limitations under the License. |
| 22 | */ |
| 23 | |
| 24 | #ifndef MBEDTLS_BIGNUM_MOD_H |
| 25 | #define MBEDTLS_BIGNUM_MOD_H |
| 26 | |
| 27 | #include "common.h" |
| 28 | |
| 29 | #if defined(MBEDTLS_BIGNUM_C) |
| 30 | #include "mbedtls/bignum.h" |
| 31 | #endif |
| 32 | |
Janos Follath | 296ea66 | 2022-08-11 14:58:29 +0100 | [diff] [blame] | 33 | /* Skip 1 as it is slightly easier to accidentally pass to functions. */ |
| 34 | typedef enum |
| 35 | { |
| 36 | MBEDTLS_MPI_MOD_REP_INVALID = 0, |
| 37 | MBEDTLS_MPI_MOD_REP_MONTGOMERY = 2, |
| 38 | MBEDTLS_MPI_MOD_REP_OPT_RED |
| 39 | } mbedtls_mpi_mod_rep_selector; |
| 40 | |
| 41 | /* Make mbedtls_mpi_mod_rep_selector and mbedtls_mpi_mod_ext_rep disjoint to |
| 42 | * make it easier to catch when they are accidentally swapped. */ |
| 43 | typedef enum |
| 44 | { |
| 45 | MBEDTLS_MPI_MOD_EXT_REP_INVALID = 0, |
| 46 | MBEDTLS_MPI_MOD_EXT_REP_LE = 8, |
| 47 | MBEDTLS_MPI_MOD_EXT_REP_BE |
| 48 | } mbedtls_mpi_mod_ext_rep; |
Gabor Mezei | f049dbf | 2022-07-18 23:02:33 +0200 | [diff] [blame] | 49 | |
| 50 | typedef struct |
| 51 | { |
Gabor Mezei | f049dbf | 2022-07-18 23:02:33 +0200 | [diff] [blame] | 52 | mbedtls_mpi_uint *p; |
Gabor Mezei | fd65e82 | 2022-08-12 18:09:12 +0200 | [diff] [blame] | 53 | size_t limbs; |
Gabor Mezei | f049dbf | 2022-07-18 23:02:33 +0200 | [diff] [blame] | 54 | } mbedtls_mpi_mod_residue; |
| 55 | |
Gabor Mezei | 89e3146 | 2022-08-12 15:36:56 +0200 | [diff] [blame] | 56 | typedef void *mbedtls_mpi_mont_struct; |
| 57 | typedef void *mbedtls_mpi_opt_red_struct; |
Gabor Mezei | f049dbf | 2022-07-18 23:02:33 +0200 | [diff] [blame] | 58 | |
| 59 | typedef struct { |
Janos Follath | ed5c8d3 | 2022-08-15 11:50:22 +0100 | [diff] [blame] | 60 | const mbedtls_mpi_uint *p; |
Gabor Mezei | fd65e82 | 2022-08-12 18:09:12 +0200 | [diff] [blame] | 61 | size_t limbs; // number of limbs |
| 62 | size_t bits; // bitlen of p |
Janos Follath | 296ea66 | 2022-08-11 14:58:29 +0100 | [diff] [blame] | 63 | mbedtls_mpi_mod_ext_rep ext_rep; // signals external representation (eg. byte order) |
| 64 | 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] | 65 | union rep |
| 66 | { |
| 67 | mbedtls_mpi_mont_struct mont; |
| 68 | mbedtls_mpi_opt_red_struct ored; |
| 69 | } rep; |
| 70 | } mbedtls_mpi_mod_modulus; |
| 71 | |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 72 | /** Setup a residue structure. |
| 73 | * |
Janos Follath | 6b8a4ad | 2022-08-19 10:58:34 +0100 | [diff] [blame] | 74 | * \param[out] r The address of residue to setup. The size is determined by |
| 75 | * \p m. |
| 76 | * (In particular, it must have at least as many limbs as the |
| 77 | * modulus \p m.) |
| 78 | * \param[in] m The address of the modulus related to \p r. |
| 79 | * \param[in] p The address of the limb array storing the value of \p r. |
| 80 | * The memory pointed to by \p p will be used by \p r and must |
| 81 | * not be modified in any way until after |
| 82 | * mbedtls_mpi_mod_residue_release() is called. |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 83 | * \param p_limbs The number of limbs of \p p. |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 84 | * |
| 85 | * \return \c 0 if successful. |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 86 | * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if \p p_limbs is less than the |
| 87 | * limbs 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] | 88 | */ |
Gabor Mezei | f049dbf | 2022-07-18 23:02:33 +0200 | [diff] [blame] | 89 | int mbedtls_mpi_mod_residue_setup( mbedtls_mpi_mod_residue *r, |
Janos Follath | 6b8a4ad | 2022-08-19 10:58:34 +0100 | [diff] [blame] | 90 | const mbedtls_mpi_mod_modulus *m, |
Janos Follath | 8b718b5 | 2022-07-25 11:31:02 +0100 | [diff] [blame] | 91 | mbedtls_mpi_uint *p, |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 92 | size_t p_limbs ); |
Gabor Mezei | f049dbf | 2022-07-18 23:02:33 +0200 | [diff] [blame] | 93 | |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 94 | /** Unbind elements of a residue structure. |
| 95 | * |
Janos Follath | dae1147 | 2022-08-08 11:50:02 +0100 | [diff] [blame] | 96 | * This function removes the reference to the limb array that was passed to |
| 97 | * mbedtls_mpi_mod_residue_setup() to make it safe to free or use again. |
| 98 | * |
| 99 | * This function invalidates \p r and it must not be used until after |
| 100 | * mbedtls_mpi_mod_residue_setup() is called on it again. |
| 101 | * |
Janos Follath | 6b8a4ad | 2022-08-19 10:58:34 +0100 | [diff] [blame] | 102 | * \param[out] r The address of residue to release. |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 103 | */ |
| 104 | void mbedtls_mpi_mod_residue_release( mbedtls_mpi_mod_residue *r ); |
| 105 | |
| 106 | /** Initialize a modulus structure. |
| 107 | * |
Janos Follath | a95f204 | 2022-08-19 12:09:17 +0100 | [diff] [blame] | 108 | * \param[out] m The address of the modulus structure to initialize. |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 109 | */ |
Gabor Mezei | f049dbf | 2022-07-18 23:02:33 +0200 | [diff] [blame] | 110 | void mbedtls_mpi_mod_modulus_init( mbedtls_mpi_mod_modulus *m ); |
| 111 | |
Janos Follath | 6318468 | 2022-08-11 17:42:59 +0100 | [diff] [blame] | 112 | /** Setup a modulus structure. |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 113 | * |
Janos Follath | 6b8a4ad | 2022-08-19 10:58:34 +0100 | [diff] [blame] | 114 | * \param[out] m The address of the modulus structure to populate. |
| 115 | * \param[in] p The address of the limb array storing the value of \p m. |
| 116 | * The memory pointed to by \p p will be used by \p m and must |
| 117 | * not be modified in any way until after |
Janos Follath | dae1147 | 2022-08-08 11:50:02 +0100 | [diff] [blame] | 118 | * mbedtls_mpi_mod_modulus_free() is called. |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 119 | * \param p_limbs The number of limbs of \p p. |
Janos Follath | dae1147 | 2022-08-08 11:50:02 +0100 | [diff] [blame] | 120 | * \param ext_rep The external representation to be used for residues |
| 121 | * associated with \p m (see #mbedtls_mpi_mod_ext_rep). |
| 122 | * \param int_rep The internal representation to be used for residues |
| 123 | * associated with \p m (see #mbedtls_mpi_mod_rep_selector). |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 124 | * |
| 125 | * \return \c 0 if successful. |
Janos Follath | 56a10f9 | 2022-08-11 15:19:00 +0100 | [diff] [blame] | 126 | * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if \p ext_rep or \p int_rep is |
| 127 | * invalid. |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 128 | */ |
Gabor Mezei | f049dbf | 2022-07-18 23:02:33 +0200 | [diff] [blame] | 129 | int mbedtls_mpi_mod_modulus_setup( mbedtls_mpi_mod_modulus *m, |
Janos Follath | ed5c8d3 | 2022-08-15 11:50:22 +0100 | [diff] [blame] | 130 | const mbedtls_mpi_uint *p, |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 131 | size_t p_limbs, |
Janos Follath | 296ea66 | 2022-08-11 14:58:29 +0100 | [diff] [blame] | 132 | mbedtls_mpi_mod_ext_rep ext_rep, |
| 133 | mbedtls_mpi_mod_rep_selector int_rep ); |
Gabor Mezei | f049dbf | 2022-07-18 23:02:33 +0200 | [diff] [blame] | 134 | |
Janos Follath | dae1147 | 2022-08-08 11:50:02 +0100 | [diff] [blame] | 135 | /** Free elements of a modulus structure. |
| 136 | * |
| 137 | * This function frees any memory allocated by mbedtls_mpi_mod_modulus_setup(). |
| 138 | * |
| 139 | * \warning This function does not free the limb array passed to |
| 140 | * mbedtls_mpi_mod_modulus_setup() only removes the reference to it, |
| 141 | * making it safe to free or to use it again. |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 142 | * |
Janos Follath | a95f204 | 2022-08-19 12:09:17 +0100 | [diff] [blame] | 143 | * \param[in,out] m The address of the modulus structure to free. |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 144 | */ |
| 145 | void mbedtls_mpi_mod_modulus_free( mbedtls_mpi_mod_modulus *m ); |
| 146 | |
Gabor Mezei | f049dbf | 2022-07-18 23:02:33 +0200 | [diff] [blame] | 147 | #endif /* MBEDTLS_BIGNUM_MOD_H */ |