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 | |
Hanno Becker | cd860df | 2022-08-18 16:23:05 +0100 | [diff] [blame] | 56 | typedef struct { |
| 57 | mbedtls_mpi_uint const *rr; /* The residue for 2^{2*n*biL} mod N */ |
| 58 | mbedtls_mpi_uint mm; /* Montgomery const for -N^{-1} mod 2^{ciL} */ |
| 59 | } mbedtls_mpi_mont_struct; |
| 60 | |
Gabor Mezei | 89e3146 | 2022-08-12 15:36:56 +0200 | [diff] [blame] | 61 | typedef void *mbedtls_mpi_opt_red_struct; |
Gabor Mezei | f049dbf | 2022-07-18 23:02:33 +0200 | [diff] [blame] | 62 | |
| 63 | typedef struct { |
Janos Follath | ed5c8d3 | 2022-08-15 11:50:22 +0100 | [diff] [blame] | 64 | const mbedtls_mpi_uint *p; |
Gabor Mezei | fd65e82 | 2022-08-12 18:09:12 +0200 | [diff] [blame] | 65 | size_t limbs; // number of limbs |
| 66 | size_t bits; // bitlen of p |
Janos Follath | 296ea66 | 2022-08-11 14:58:29 +0100 | [diff] [blame] | 67 | 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] | 68 | union rep |
| 69 | { |
| 70 | mbedtls_mpi_mont_struct mont; |
| 71 | mbedtls_mpi_opt_red_struct ored; |
| 72 | } rep; |
| 73 | } mbedtls_mpi_mod_modulus; |
| 74 | |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 75 | /** Setup a residue structure. |
| 76 | * |
Janos Follath | 41427de | 2022-11-24 19:04:54 +0000 | [diff] [blame^] | 77 | * The residue will be set up with the \p p buffer \p m modulus. |
| 78 | * |
| 79 | * The memory pointed by \p p will be used by the resulting residue structure. |
| 80 | * The value at the pointed memory will be the initial value of \p r and must |
| 81 | * hold a value that is less than the modulus. This value will be used as it is |
| 82 | * and interpreted according to the value of the `m->int_rep` field. |
| 83 | * |
| 84 | * The modulus \p m will be the modulus associated with \p r. The residue \p r |
| 85 | * should only be used in operations where the modulus is \p m or a modulus |
| 86 | * equivalent to \p m (in the sense that all their fields or memory pointed by |
| 87 | * their fields hold the same value). |
| 88 | * |
Minos Galanakis | aed832a | 2022-11-24 09:09:47 +0000 | [diff] [blame] | 89 | * \param[out] r The address of residue to setup. The resulting structure's |
| 90 | * size is determined by \p m. |
Janos Follath | 6b8a4ad | 2022-08-19 10:58:34 +0100 | [diff] [blame] | 91 | * \param[in] m The address of the modulus related to \p r. |
| 92 | * \param[in] p The address of the limb array storing the value of \p r. |
| 93 | * The memory pointed to by \p p will be used by \p r and must |
| 94 | * not be modified in any way until after |
Minos Galanakis | aed832a | 2022-11-24 09:09:47 +0000 | [diff] [blame] | 95 | * mbedtls_mpi_mod_residue_release() is called. The data |
Janos Follath | 41427de | 2022-11-24 19:04:54 +0000 | [diff] [blame^] | 96 | * pointed by \p p should be less than the modulus (the value |
| 97 | * pointed by `m->p`) and already in the representation |
| 98 | * indicated by `m->int_rep`. |
Minos Galanakis | aed832a | 2022-11-24 09:09:47 +0000 | [diff] [blame] | 99 | * \param p_limbs The number of limbs of \p p. It must have at most as |
| 100 | * many limbs as the modulus \p m.) |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 101 | * |
| 102 | * \return \c 0 if successful. |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 103 | * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if \p p_limbs is less than the |
| 104 | * 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] | 105 | */ |
Gabor Mezei | f049dbf | 2022-07-18 23:02:33 +0200 | [diff] [blame] | 106 | int mbedtls_mpi_mod_residue_setup( mbedtls_mpi_mod_residue *r, |
Janos Follath | 6b8a4ad | 2022-08-19 10:58:34 +0100 | [diff] [blame] | 107 | const mbedtls_mpi_mod_modulus *m, |
Janos Follath | 8b718b5 | 2022-07-25 11:31:02 +0100 | [diff] [blame] | 108 | mbedtls_mpi_uint *p, |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 109 | size_t p_limbs ); |
Gabor Mezei | f049dbf | 2022-07-18 23:02:33 +0200 | [diff] [blame] | 110 | |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 111 | /** Unbind elements of a residue structure. |
| 112 | * |
Janos Follath | dae1147 | 2022-08-08 11:50:02 +0100 | [diff] [blame] | 113 | * This function removes the reference to the limb array that was passed to |
| 114 | * mbedtls_mpi_mod_residue_setup() to make it safe to free or use again. |
| 115 | * |
| 116 | * This function invalidates \p r and it must not be used until after |
| 117 | * mbedtls_mpi_mod_residue_setup() is called on it again. |
| 118 | * |
Janos Follath | 6b8a4ad | 2022-08-19 10:58:34 +0100 | [diff] [blame] | 119 | * \param[out] r The address of residue to release. |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 120 | */ |
| 121 | void mbedtls_mpi_mod_residue_release( mbedtls_mpi_mod_residue *r ); |
| 122 | |
| 123 | /** Initialize a modulus structure. |
| 124 | * |
Janos Follath | a95f204 | 2022-08-19 12:09:17 +0100 | [diff] [blame] | 125 | * \param[out] m The address of the modulus structure to initialize. |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 126 | */ |
Gabor Mezei | f049dbf | 2022-07-18 23:02:33 +0200 | [diff] [blame] | 127 | void mbedtls_mpi_mod_modulus_init( mbedtls_mpi_mod_modulus *m ); |
| 128 | |
Janos Follath | 6318468 | 2022-08-11 17:42:59 +0100 | [diff] [blame] | 129 | /** Setup a modulus structure. |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 130 | * |
Janos Follath | 6b8a4ad | 2022-08-19 10:58:34 +0100 | [diff] [blame] | 131 | * \param[out] m The address of the modulus structure to populate. |
| 132 | * \param[in] p The address of the limb array storing the value of \p m. |
| 133 | * The memory pointed to by \p p will be used by \p m and must |
| 134 | * not be modified in any way until after |
Janos Follath | dae1147 | 2022-08-08 11:50:02 +0100 | [diff] [blame] | 135 | * mbedtls_mpi_mod_modulus_free() is called. |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 136 | * \param p_limbs The number of limbs of \p p. |
Janos Follath | dae1147 | 2022-08-08 11:50:02 +0100 | [diff] [blame] | 137 | * \param int_rep The internal representation to be used for residues |
| 138 | * associated with \p m (see #mbedtls_mpi_mod_rep_selector). |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 139 | * |
| 140 | * \return \c 0 if successful. |
Janos Follath | 56a10f9 | 2022-08-11 15:19:00 +0100 | [diff] [blame] | 141 | * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if \p ext_rep or \p int_rep is |
| 142 | * invalid. |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 143 | */ |
Gabor Mezei | f049dbf | 2022-07-18 23:02:33 +0200 | [diff] [blame] | 144 | int mbedtls_mpi_mod_modulus_setup( mbedtls_mpi_mod_modulus *m, |
Janos Follath | ed5c8d3 | 2022-08-15 11:50:22 +0100 | [diff] [blame] | 145 | const mbedtls_mpi_uint *p, |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 146 | size_t p_limbs, |
Janos Follath | 296ea66 | 2022-08-11 14:58:29 +0100 | [diff] [blame] | 147 | mbedtls_mpi_mod_rep_selector int_rep ); |
Gabor Mezei | f049dbf | 2022-07-18 23:02:33 +0200 | [diff] [blame] | 148 | |
Janos Follath | dae1147 | 2022-08-08 11:50:02 +0100 | [diff] [blame] | 149 | /** Free elements of a modulus structure. |
| 150 | * |
| 151 | * This function frees any memory allocated by mbedtls_mpi_mod_modulus_setup(). |
| 152 | * |
| 153 | * \warning This function does not free the limb array passed to |
| 154 | * mbedtls_mpi_mod_modulus_setup() only removes the reference to it, |
| 155 | * making it safe to free or to use it again. |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 156 | * |
Janos Follath | a95f204 | 2022-08-19 12:09:17 +0100 | [diff] [blame] | 157 | * \param[in,out] m The address of the modulus structure to free. |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 158 | */ |
| 159 | void mbedtls_mpi_mod_modulus_free( mbedtls_mpi_mod_modulus *m ); |
| 160 | |
Janos Follath | 5933f69 | 2022-11-02 14:35:17 +0000 | [diff] [blame] | 161 | /* BEGIN MERGE SLOT 1 */ |
| 162 | |
| 163 | /* END MERGE SLOT 1 */ |
| 164 | |
| 165 | /* BEGIN MERGE SLOT 2 */ |
| 166 | |
| 167 | /* END MERGE SLOT 2 */ |
| 168 | |
| 169 | /* BEGIN MERGE SLOT 3 */ |
| 170 | |
| 171 | /* END MERGE SLOT 3 */ |
| 172 | |
| 173 | /* BEGIN MERGE SLOT 4 */ |
| 174 | |
| 175 | /* END MERGE SLOT 4 */ |
| 176 | |
| 177 | /* BEGIN MERGE SLOT 5 */ |
| 178 | |
| 179 | /* END MERGE SLOT 5 */ |
| 180 | |
| 181 | /* BEGIN MERGE SLOT 6 */ |
| 182 | |
| 183 | /* END MERGE SLOT 6 */ |
| 184 | |
| 185 | /* BEGIN MERGE SLOT 7 */ |
Janos Follath | 41427de | 2022-11-24 19:04:54 +0000 | [diff] [blame^] | 186 | /** Read a residue from a byte buffer. |
Minos Galanakis | 81f4b11 | 2022-11-10 14:40:38 +0000 | [diff] [blame] | 187 | * |
Janos Follath | 41427de | 2022-11-24 19:04:54 +0000 | [diff] [blame^] | 188 | * The residue will be automatically converted to the internal representation |
| 189 | * based on the value of `m->int_rep` field. |
Minos Galanakis | 81f4b11 | 2022-11-10 14:40:38 +0000 | [diff] [blame] | 190 | * |
Janos Follath | 41427de | 2022-11-24 19:04:54 +0000 | [diff] [blame^] | 191 | * The modulus \p m will be the modulus associated with \p r. The residue \p r |
| 192 | * should only be used in operations where the modulus is \p m or a modulus |
| 193 | * equivalent to \p m (in the sense that all their fields or memory pointed by |
| 194 | * their fields hold the same value). |
| 195 | * |
| 196 | * \param r The address of the residue. It must have as many limbs as |
| 197 | * the modulus \p m. |
Janos Follath | 3e3fc91 | 2022-11-24 18:02:46 +0000 | [diff] [blame] | 198 | * \param m The address of the modulus. |
| 199 | * \param buf The input buffer to import from. |
| 200 | * \param buflen The length in bytes of \p buf. |
| 201 | * \param ext_rep The endianness of the number in the input buffer. |
Minos Galanakis | 81f4b11 | 2022-11-10 14:40:38 +0000 | [diff] [blame] | 202 | * |
| 203 | * \return \c 0 if successful. |
Janos Follath | 41427de | 2022-11-24 19:04:54 +0000 | [diff] [blame^] | 204 | * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p r isn't |
Minos Galanakis | 81f4b11 | 2022-11-10 14:40:38 +0000 | [diff] [blame] | 205 | * large enough to hold the value in \p buf. |
Janos Follath | 41427de | 2022-11-24 19:04:54 +0000 | [diff] [blame^] | 206 | * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if \p ext_rep |
| 207 | * is invalid or the value in the buffer is not less than \p m. |
Minos Galanakis | 81f4b11 | 2022-11-10 14:40:38 +0000 | [diff] [blame] | 208 | */ |
| 209 | int mbedtls_mpi_mod_read( mbedtls_mpi_mod_residue *r, |
Minos Galanakis | 8b37545 | 2022-11-24 11:04:11 +0000 | [diff] [blame] | 210 | const mbedtls_mpi_mod_modulus *m, |
| 211 | const unsigned char *buf, |
Janos Follath | 3e3fc91 | 2022-11-24 18:02:46 +0000 | [diff] [blame] | 212 | size_t buflen, |
| 213 | mbedtls_mpi_mod_ext_rep ext_rep ); |
Janos Follath | 5933f69 | 2022-11-02 14:35:17 +0000 | [diff] [blame] | 214 | |
Janos Follath | 41427de | 2022-11-24 19:04:54 +0000 | [diff] [blame^] | 215 | /** Write a residue into a byte buffer. |
Minos Galanakis | 81f4b11 | 2022-11-10 14:40:38 +0000 | [diff] [blame] | 216 | * |
Janos Follath | 41427de | 2022-11-24 19:04:54 +0000 | [diff] [blame^] | 217 | * The modulus \p m must be the modulus associated with \p r (see |
| 218 | * mbedtls_mpi_mod_residue_setup() and mbedtls_mpi_mod_read()). |
Minos Galanakis | 81f4b11 | 2022-11-10 14:40:38 +0000 | [diff] [blame] | 219 | * |
Janos Follath | 41427de | 2022-11-24 19:04:54 +0000 | [diff] [blame^] | 220 | * The residue will be automatically converted from the internal representation |
| 221 | * based on the value of `m->int_rep` field. |
| 222 | * |
| 223 | * \warning If the buffer is smaller than `m->bits`, the number of |
| 224 | * leading zeroes is leaked through side channels. If \p r is |
| 225 | * secret, the caller must ensure that \p buflen is at least |
| 226 | * (`m->bits`+7)/8. |
| 227 | * |
| 228 | * \param r The address of the residue. It must have as many limbs as |
| 229 | * the modulus \p m. |
| 230 | * \param m The address of the modulus associated with \r. |
Janos Follath | 3e3fc91 | 2022-11-24 18:02:46 +0000 | [diff] [blame] | 231 | * \param buf The output buffer to export to. |
| 232 | * \param buflen The length in bytes of \p buf. |
Janos Follath | 41427de | 2022-11-24 19:04:54 +0000 | [diff] [blame^] | 233 | * \param ext_rep The endianness in which the number should be written into |
| 234 | * the output buffer. |
Minos Galanakis | 81f4b11 | 2022-11-10 14:40:38 +0000 | [diff] [blame] | 235 | * |
| 236 | * \return \c 0 if successful. |
| 237 | * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p buf isn't |
Janos Follath | 41427de | 2022-11-24 19:04:54 +0000 | [diff] [blame^] | 238 | * large enough to hold the value of \p r (without leading |
| 239 | * zeroes). |
| 240 | * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if of \p ext_rep is invalid. |
Minos Galanakis | 81f4b11 | 2022-11-10 14:40:38 +0000 | [diff] [blame] | 241 | */ |
Minos Galanakis | 8b37545 | 2022-11-24 11:04:11 +0000 | [diff] [blame] | 242 | int mbedtls_mpi_mod_write( const mbedtls_mpi_mod_residue *r, |
| 243 | const mbedtls_mpi_mod_modulus *m, |
Minos Galanakis | 81f4b11 | 2022-11-10 14:40:38 +0000 | [diff] [blame] | 244 | unsigned char *buf, |
Janos Follath | 3e3fc91 | 2022-11-24 18:02:46 +0000 | [diff] [blame] | 245 | size_t buflen, |
| 246 | mbedtls_mpi_mod_ext_rep ext_rep ); |
Janos Follath | 5933f69 | 2022-11-02 14:35:17 +0000 | [diff] [blame] | 247 | /* END MERGE SLOT 7 */ |
| 248 | |
| 249 | /* BEGIN MERGE SLOT 8 */ |
| 250 | |
| 251 | /* END MERGE SLOT 8 */ |
| 252 | |
| 253 | /* BEGIN MERGE SLOT 9 */ |
| 254 | |
| 255 | /* END MERGE SLOT 9 */ |
| 256 | |
| 257 | /* BEGIN MERGE SLOT 10 */ |
| 258 | |
| 259 | /* END MERGE SLOT 10 */ |
| 260 | |
Gabor Mezei | f049dbf | 2022-07-18 23:02:33 +0200 | [diff] [blame] | 261 | #endif /* MBEDTLS_BIGNUM_MOD_H */ |