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. |
Werner Lewis | 5e9d2e9 | 2022-12-12 14:00:25 +0000 | [diff] [blame] | 5 | * |
| 6 | * The functions in this module obey the following conventions unless |
| 7 | * explicitly indicated otherwise: |
| 8 | * |
| 9 | * - **Modulus parameters**: the modulus is passed as a pointer to a structure |
Werner Lewis | e1eb75d | 2022-12-14 13:45:49 +0000 | [diff] [blame] | 10 | * of type #mbedtls_mpi_mod_modulus. The structure must be set up with an |
| 11 | * array of limbs storing the bignum value of the modulus. The modulus must |
| 12 | * be odd and is assumed to have no leading zeroes. The modulus is usually |
| 13 | * named \p N and is usually input-only. |
Werner Lewis | 5e9d2e9 | 2022-12-12 14:00:25 +0000 | [diff] [blame] | 14 | * - **Bignum parameters**: Bignums are passed as pointers to an array of |
| 15 | * limbs or to a #mbedtls_mpi_mod_residue structure. A limb has the type |
| 16 | * #mbedtls_mpi_uint. Residues must be initialized before use, and must be |
| 17 | * associated with the modulus \p N. Unless otherwise specified: |
| 18 | * - Bignum parameters called \p A, \p B, ... are inputs and are not |
Werner Lewis | eac8be7 | 2022-12-14 13:49:12 +0000 | [diff] [blame] | 19 | * modified by the function. |
Werner Lewis | 5e9d2e9 | 2022-12-12 14:00:25 +0000 | [diff] [blame] | 20 | * - Bignum parameters called \p X, \p Y, ... are outputs or input-output. |
Werner Lewis | 945a165 | 2022-12-14 15:24:46 +0000 | [diff] [blame] | 21 | * The initial bignum value of output-only parameters is ignored, but |
| 22 | * they must be set up and associated with the modulus \p N. |
Werner Lewis | 2bd263d | 2022-12-14 15:32:31 +0000 | [diff] [blame] | 23 | * - Bignum parameters called \p P are inputs used to set up a modulus or |
Werner Lewis | 5e9d2e9 | 2022-12-12 14:00:25 +0000 | [diff] [blame] | 24 | * residue. These must be pointers to an array of limbs. |
Werner Lewis | 2bd263d | 2022-12-14 15:32:31 +0000 | [diff] [blame] | 25 | * - \p T is a temporary storage area. The initial content of such a |
Werner Lewis | 5e9d2e9 | 2022-12-12 14:00:25 +0000 | [diff] [blame] | 26 | * parameter is ignored and the final content is unspecified. |
| 27 | * - Some functions use different names, such as \p R for the residue. |
| 28 | * - **Bignum sizes**: bignum sizes are always expressed in limbs. Both |
Werner Lewis | 2e70b9a | 2022-12-14 15:48:31 +0000 | [diff] [blame] | 29 | * #mbedtls_mpi_mod_modulus and #mbedtls_mpi_mod_residue have a \c limbs |
| 30 | * member storing its size. All bignum parameters must have the same |
| 31 | * number of limbs as the modulus. All bignum sizes must be at least 1 and |
| 32 | * must be significantly less than #SIZE_MAX. The behavior if a size is 0 is |
| 33 | * undefined. |
Werner Lewis | 5e9d2e9 | 2022-12-12 14:00:25 +0000 | [diff] [blame] | 34 | * - **Bignum representation**: the representation of inputs and outputs is |
| 35 | * specified by the \p int_rep field of the modulus. |
| 36 | * - **Parameter ordering**: for bignum parameters, outputs come before inputs. |
Werner Lewis | a306886 | 2022-12-14 15:57:12 +0000 | [diff] [blame] | 37 | * The modulus is passed after residues. Temporaries come last. |
Werner Lewis | 5e9d2e9 | 2022-12-12 14:00:25 +0000 | [diff] [blame] | 38 | * - **Aliasing**: in general, output bignums may be aliased to one or more |
| 39 | * inputs. Modulus values may not be aliased to any other parameter. Outputs |
| 40 | * may not be aliased to one another. Temporaries may not be aliased to any |
| 41 | * other parameter. |
| 42 | * - **Overlap**: apart from aliasing of residue pointers (where two residue |
| 43 | * arguments are equal pointers), overlap is not supported and may result |
| 44 | * in undefined behavior. |
Werner Lewis | 2bd263d | 2022-12-14 15:32:31 +0000 | [diff] [blame] | 45 | * - **Error handling**: functions generally check compatibility of input |
Werner Lewis | 5e9d2e9 | 2022-12-12 14:00:25 +0000 | [diff] [blame] | 46 | * sizes. Most functions will not check that input values are in canonical |
| 47 | * form (i.e. that \p A < \p N), this is only checked during setup of a |
| 48 | * residue structure. |
Werner Lewis | 1d89ebf | 2022-12-14 17:08:43 +0000 | [diff] [blame^] | 49 | * - **Modular representatives**: all functions expect inputs to be in the |
| 50 | * range [0, \p N - 1] and guarantee outputs in the range [0, \p N - 1]. |
| 51 | * Residues are set up with an associated modulus, and operations are only |
| 52 | * guaranteed to work if the modulus is associated with all residue |
| 53 | * parameters. If a residue is passed with a modulus other than the one it |
| 54 | * is associated with, then it may be out of range. If an input is out of |
| 55 | * range, outputs are fully unspecified, though bignum values out of range |
| 56 | * should not cause buffer overflows (beware that this is not extensively |
| 57 | * tested). |
Gilles Peskine | 7f887bd | 2022-09-27 13:12:30 +0200 | [diff] [blame] | 58 | */ |
| 59 | |
| 60 | /* |
Gabor Mezei | f049dbf | 2022-07-18 23:02:33 +0200 | [diff] [blame] | 61 | * Copyright The Mbed TLS Contributors |
| 62 | * SPDX-License-Identifier: Apache-2.0 |
| 63 | * |
| 64 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 65 | * not use this file except in compliance with the License. |
| 66 | * You may obtain a copy of the License at |
| 67 | * |
| 68 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 69 | * |
| 70 | * Unless required by applicable law or agreed to in writing, software |
| 71 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 72 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 73 | * See the License for the specific language governing permissions and |
| 74 | * limitations under the License. |
| 75 | */ |
| 76 | |
| 77 | #ifndef MBEDTLS_BIGNUM_MOD_H |
| 78 | #define MBEDTLS_BIGNUM_MOD_H |
| 79 | |
| 80 | #include "common.h" |
| 81 | |
| 82 | #if defined(MBEDTLS_BIGNUM_C) |
| 83 | #include "mbedtls/bignum.h" |
| 84 | #endif |
| 85 | |
Janos Follath | 296ea66 | 2022-08-11 14:58:29 +0100 | [diff] [blame] | 86 | /* Skip 1 as it is slightly easier to accidentally pass to functions. */ |
| 87 | typedef enum |
| 88 | { |
| 89 | MBEDTLS_MPI_MOD_REP_INVALID = 0, |
| 90 | MBEDTLS_MPI_MOD_REP_MONTGOMERY = 2, |
| 91 | MBEDTLS_MPI_MOD_REP_OPT_RED |
| 92 | } mbedtls_mpi_mod_rep_selector; |
| 93 | |
| 94 | /* Make mbedtls_mpi_mod_rep_selector and mbedtls_mpi_mod_ext_rep disjoint to |
| 95 | * make it easier to catch when they are accidentally swapped. */ |
| 96 | typedef enum |
| 97 | { |
| 98 | MBEDTLS_MPI_MOD_EXT_REP_INVALID = 0, |
| 99 | MBEDTLS_MPI_MOD_EXT_REP_LE = 8, |
| 100 | MBEDTLS_MPI_MOD_EXT_REP_BE |
| 101 | } mbedtls_mpi_mod_ext_rep; |
Gabor Mezei | f049dbf | 2022-07-18 23:02:33 +0200 | [diff] [blame] | 102 | |
| 103 | typedef struct |
| 104 | { |
Gabor Mezei | f049dbf | 2022-07-18 23:02:33 +0200 | [diff] [blame] | 105 | mbedtls_mpi_uint *p; |
Gabor Mezei | fd65e82 | 2022-08-12 18:09:12 +0200 | [diff] [blame] | 106 | size_t limbs; |
Gabor Mezei | f049dbf | 2022-07-18 23:02:33 +0200 | [diff] [blame] | 107 | } mbedtls_mpi_mod_residue; |
| 108 | |
Hanno Becker | cd860df | 2022-08-18 16:23:05 +0100 | [diff] [blame] | 109 | typedef struct { |
| 110 | mbedtls_mpi_uint const *rr; /* The residue for 2^{2*n*biL} mod N */ |
| 111 | mbedtls_mpi_uint mm; /* Montgomery const for -N^{-1} mod 2^{ciL} */ |
| 112 | } mbedtls_mpi_mont_struct; |
| 113 | |
Gabor Mezei | 89e3146 | 2022-08-12 15:36:56 +0200 | [diff] [blame] | 114 | typedef void *mbedtls_mpi_opt_red_struct; |
Gabor Mezei | f049dbf | 2022-07-18 23:02:33 +0200 | [diff] [blame] | 115 | |
| 116 | typedef struct { |
Janos Follath | ed5c8d3 | 2022-08-15 11:50:22 +0100 | [diff] [blame] | 117 | const mbedtls_mpi_uint *p; |
Gabor Mezei | fd65e82 | 2022-08-12 18:09:12 +0200 | [diff] [blame] | 118 | size_t limbs; // number of limbs |
| 119 | size_t bits; // bitlen of p |
Janos Follath | 296ea66 | 2022-08-11 14:58:29 +0100 | [diff] [blame] | 120 | 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] | 121 | union rep |
| 122 | { |
| 123 | mbedtls_mpi_mont_struct mont; |
| 124 | mbedtls_mpi_opt_red_struct ored; |
| 125 | } rep; |
| 126 | } mbedtls_mpi_mod_modulus; |
| 127 | |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 128 | /** Setup a residue structure. |
| 129 | * |
Janos Follath | fc6fbb4 | 2022-11-25 15:43:17 +0000 | [diff] [blame] | 130 | * The residue will be set up with the buffer \p p and modulus \p m. |
Janos Follath | 41427de | 2022-11-24 19:04:54 +0000 | [diff] [blame] | 131 | * |
Janos Follath | fc6fbb4 | 2022-11-25 15:43:17 +0000 | [diff] [blame] | 132 | * The memory pointed to by \p p will be used by the resulting residue structure. |
| 133 | * The value at the pointed-to memory will be the initial value of \p r and must |
| 134 | * hold a value that is less than the modulus. This value will be used as-is |
Janos Follath | 41427de | 2022-11-24 19:04:54 +0000 | [diff] [blame] | 135 | * and interpreted according to the value of the `m->int_rep` field. |
| 136 | * |
| 137 | * The modulus \p m will be the modulus associated with \p r. The residue \p r |
Janos Follath | 6eb92c0 | 2022-11-26 17:34:37 +0000 | [diff] [blame] | 138 | * should only be used in operations where the modulus is \p m. |
Janos Follath | 41427de | 2022-11-24 19:04:54 +0000 | [diff] [blame] | 139 | * |
Janos Follath | ee530cc | 2022-11-25 15:54:40 +0000 | [diff] [blame] | 140 | * \param[out] r The address of the residue to setup. |
Janos Follath | 6b8a4ad | 2022-08-19 10:58:34 +0100 | [diff] [blame] | 141 | * \param[in] m The address of the modulus related to \p r. |
Janos Follath | fc6fbb4 | 2022-11-25 15:43:17 +0000 | [diff] [blame] | 142 | * \param[in] p The address of the limb array containing the value of \p r. |
Janos Follath | 6b8a4ad | 2022-08-19 10:58:34 +0100 | [diff] [blame] | 143 | * The memory pointed to by \p p will be used by \p r and must |
| 144 | * not be modified in any way until after |
Minos Galanakis | aed832a | 2022-11-24 09:09:47 +0000 | [diff] [blame] | 145 | * mbedtls_mpi_mod_residue_release() is called. The data |
Janos Follath | fc6fbb4 | 2022-11-25 15:43:17 +0000 | [diff] [blame] | 146 | * pointed to by \p p must be less than the modulus (the value |
| 147 | * pointed to by `m->p`) and already in the representation |
Janos Follath | 41427de | 2022-11-24 19:04:54 +0000 | [diff] [blame] | 148 | * indicated by `m->int_rep`. |
Janos Follath | ee530cc | 2022-11-25 15:54:40 +0000 | [diff] [blame] | 149 | * \param p_limbs The number of limbs of \p p. Must be the same as the number |
Janos Follath | 6eb92c0 | 2022-11-26 17:34:37 +0000 | [diff] [blame] | 150 | * of limbs in the modulus \p m. |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 151 | * |
| 152 | * \return \c 0 if successful. |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 153 | * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if \p p_limbs is less than the |
| 154 | * 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] | 155 | */ |
Gabor Mezei | f049dbf | 2022-07-18 23:02:33 +0200 | [diff] [blame] | 156 | int mbedtls_mpi_mod_residue_setup( mbedtls_mpi_mod_residue *r, |
Janos Follath | 6b8a4ad | 2022-08-19 10:58:34 +0100 | [diff] [blame] | 157 | const mbedtls_mpi_mod_modulus *m, |
Janos Follath | 8b718b5 | 2022-07-25 11:31:02 +0100 | [diff] [blame] | 158 | mbedtls_mpi_uint *p, |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 159 | size_t p_limbs ); |
Gabor Mezei | f049dbf | 2022-07-18 23:02:33 +0200 | [diff] [blame] | 160 | |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 161 | /** Unbind elements of a residue structure. |
| 162 | * |
Janos Follath | dae1147 | 2022-08-08 11:50:02 +0100 | [diff] [blame] | 163 | * This function removes the reference to the limb array that was passed to |
| 164 | * mbedtls_mpi_mod_residue_setup() to make it safe to free or use again. |
| 165 | * |
| 166 | * This function invalidates \p r and it must not be used until after |
| 167 | * mbedtls_mpi_mod_residue_setup() is called on it again. |
| 168 | * |
Janos Follath | 6b8a4ad | 2022-08-19 10:58:34 +0100 | [diff] [blame] | 169 | * \param[out] r The address of residue to release. |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 170 | */ |
| 171 | void mbedtls_mpi_mod_residue_release( mbedtls_mpi_mod_residue *r ); |
| 172 | |
| 173 | /** Initialize a modulus structure. |
| 174 | * |
Janos Follath | a95f204 | 2022-08-19 12:09:17 +0100 | [diff] [blame] | 175 | * \param[out] m The address of the modulus structure to initialize. |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 176 | */ |
Gabor Mezei | f049dbf | 2022-07-18 23:02:33 +0200 | [diff] [blame] | 177 | void mbedtls_mpi_mod_modulus_init( mbedtls_mpi_mod_modulus *m ); |
| 178 | |
Janos Follath | 6318468 | 2022-08-11 17:42:59 +0100 | [diff] [blame] | 179 | /** Setup a modulus structure. |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 180 | * |
Janos Follath | 6b8a4ad | 2022-08-19 10:58:34 +0100 | [diff] [blame] | 181 | * \param[out] m The address of the modulus structure to populate. |
| 182 | * \param[in] p The address of the limb array storing the value of \p m. |
| 183 | * The memory pointed to by \p p will be used by \p m and must |
| 184 | * not be modified in any way until after |
Janos Follath | dae1147 | 2022-08-08 11:50:02 +0100 | [diff] [blame] | 185 | * mbedtls_mpi_mod_modulus_free() is called. |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 186 | * \param p_limbs The number of limbs of \p p. |
Janos Follath | dae1147 | 2022-08-08 11:50:02 +0100 | [diff] [blame] | 187 | * \param int_rep The internal representation to be used for residues |
| 188 | * associated with \p m (see #mbedtls_mpi_mod_rep_selector). |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 189 | * |
| 190 | * \return \c 0 if successful. |
Janos Follath | ee530cc | 2022-11-25 15:54:40 +0000 | [diff] [blame] | 191 | * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if \p int_rep is invalid. |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 192 | */ |
Gabor Mezei | f049dbf | 2022-07-18 23:02:33 +0200 | [diff] [blame] | 193 | int mbedtls_mpi_mod_modulus_setup( mbedtls_mpi_mod_modulus *m, |
Janos Follath | ed5c8d3 | 2022-08-15 11:50:22 +0100 | [diff] [blame] | 194 | const mbedtls_mpi_uint *p, |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 195 | size_t p_limbs, |
Janos Follath | 296ea66 | 2022-08-11 14:58:29 +0100 | [diff] [blame] | 196 | mbedtls_mpi_mod_rep_selector int_rep ); |
Gabor Mezei | f049dbf | 2022-07-18 23:02:33 +0200 | [diff] [blame] | 197 | |
Janos Follath | dae1147 | 2022-08-08 11:50:02 +0100 | [diff] [blame] | 198 | /** Free elements of a modulus structure. |
| 199 | * |
| 200 | * This function frees any memory allocated by mbedtls_mpi_mod_modulus_setup(). |
| 201 | * |
| 202 | * \warning This function does not free the limb array passed to |
| 203 | * mbedtls_mpi_mod_modulus_setup() only removes the reference to it, |
| 204 | * making it safe to free or to use it again. |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 205 | * |
Janos Follath | a95f204 | 2022-08-19 12:09:17 +0100 | [diff] [blame] | 206 | * \param[in,out] m The address of the modulus structure to free. |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 207 | */ |
| 208 | void mbedtls_mpi_mod_modulus_free( mbedtls_mpi_mod_modulus *m ); |
| 209 | |
Janos Follath | 5933f69 | 2022-11-02 14:35:17 +0000 | [diff] [blame] | 210 | /* BEGIN MERGE SLOT 1 */ |
| 211 | |
| 212 | /* END MERGE SLOT 1 */ |
| 213 | |
| 214 | /* BEGIN MERGE SLOT 2 */ |
| 215 | |
| 216 | /* END MERGE SLOT 2 */ |
| 217 | |
| 218 | /* BEGIN MERGE SLOT 3 */ |
Tom Cosgrove | 62b2048 | 2022-12-01 14:27:37 +0000 | [diff] [blame] | 219 | /** |
| 220 | * \brief Perform a fixed-size modular subtraction. |
| 221 | * |
| 222 | * Calculate `A - B modulo N`. |
| 223 | * |
| 224 | * \p A, \p B and \p X must all have the same number of limbs as \p N. |
| 225 | * |
| 226 | * \p X may be aliased to \p A or \p B, or even both, but may not overlap |
| 227 | * either otherwise. |
| 228 | * |
| 229 | * \note This function does not check that \p A or \p B are in canonical |
| 230 | * form (that is, are < \p N) - that will have been done by |
| 231 | * mbedtls_mpi_mod_residue_setup(). |
| 232 | * |
| 233 | * \param[out] X The address of the result MPI. Must be initialized. |
| 234 | * Must have the same number of limbs as the modulus \p N. |
| 235 | * \param[in] A The address of the first MPI. |
| 236 | * \param[in] B The address of the second MPI. |
| 237 | * \param[in] N The address of the modulus. Used to perform a modulo |
| 238 | * operation on the result of the subtraction. |
| 239 | * |
| 240 | * \return \c 0 if successful. |
| 241 | * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if the given MPIs do not |
| 242 | * have the correct number of limbs. |
| 243 | */ |
| 244 | int mbedtls_mpi_mod_sub( mbedtls_mpi_mod_residue *X, |
| 245 | const mbedtls_mpi_mod_residue *A, |
| 246 | const mbedtls_mpi_mod_residue *B, |
| 247 | const mbedtls_mpi_mod_modulus *N ); |
Janos Follath | 5933f69 | 2022-11-02 14:35:17 +0000 | [diff] [blame] | 248 | /* END MERGE SLOT 3 */ |
| 249 | |
| 250 | /* BEGIN MERGE SLOT 4 */ |
| 251 | |
| 252 | /* END MERGE SLOT 4 */ |
| 253 | |
| 254 | /* BEGIN MERGE SLOT 5 */ |
| 255 | |
| 256 | /* END MERGE SLOT 5 */ |
| 257 | |
| 258 | /* BEGIN MERGE SLOT 6 */ |
| 259 | |
| 260 | /* END MERGE SLOT 6 */ |
| 261 | |
| 262 | /* BEGIN MERGE SLOT 7 */ |
Janos Follath | 41427de | 2022-11-24 19:04:54 +0000 | [diff] [blame] | 263 | /** Read a residue from a byte buffer. |
Minos Galanakis | 81f4b11 | 2022-11-10 14:40:38 +0000 | [diff] [blame] | 264 | * |
Janos Follath | 41427de | 2022-11-24 19:04:54 +0000 | [diff] [blame] | 265 | * The residue will be automatically converted to the internal representation |
Janos Follath | fc6fbb4 | 2022-11-25 15:43:17 +0000 | [diff] [blame] | 266 | * based on the value of the `m->int_rep` field. |
Minos Galanakis | 81f4b11 | 2022-11-10 14:40:38 +0000 | [diff] [blame] | 267 | * |
Janos Follath | 41427de | 2022-11-24 19:04:54 +0000 | [diff] [blame] | 268 | * The modulus \p m will be the modulus associated with \p r. The residue \p r |
| 269 | * should only be used in operations where the modulus is \p m or a modulus |
| 270 | * equivalent to \p m (in the sense that all their fields or memory pointed by |
| 271 | * their fields hold the same value). |
| 272 | * |
Janos Follath | 1f8afa2 | 2022-11-28 14:32:33 +0000 | [diff] [blame] | 273 | * \param[out] r The address of the residue. It must have exactly the same |
Janos Follath | fc6fbb4 | 2022-11-25 15:43:17 +0000 | [diff] [blame] | 274 | * number of limbs as the modulus \p m. |
Janos Follath | 1f8afa2 | 2022-11-28 14:32:33 +0000 | [diff] [blame] | 275 | * \param[in] m The address of the modulus. |
| 276 | * \param[in] buf The input buffer to import from. |
Janos Follath | 3e3fc91 | 2022-11-24 18:02:46 +0000 | [diff] [blame] | 277 | * \param buflen The length in bytes of \p buf. |
| 278 | * \param ext_rep The endianness of the number in the input buffer. |
Minos Galanakis | 81f4b11 | 2022-11-10 14:40:38 +0000 | [diff] [blame] | 279 | * |
| 280 | * \return \c 0 if successful. |
Janos Follath | 41427de | 2022-11-24 19:04:54 +0000 | [diff] [blame] | 281 | * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p r isn't |
Minos Galanakis | 81f4b11 | 2022-11-10 14:40:38 +0000 | [diff] [blame] | 282 | * large enough to hold the value in \p buf. |
Janos Follath | 41427de | 2022-11-24 19:04:54 +0000 | [diff] [blame] | 283 | * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if \p ext_rep |
| 284 | * 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] | 285 | */ |
| 286 | int mbedtls_mpi_mod_read( mbedtls_mpi_mod_residue *r, |
Minos Galanakis | 8b37545 | 2022-11-24 11:04:11 +0000 | [diff] [blame] | 287 | const mbedtls_mpi_mod_modulus *m, |
| 288 | const unsigned char *buf, |
Janos Follath | 3e3fc91 | 2022-11-24 18:02:46 +0000 | [diff] [blame] | 289 | size_t buflen, |
| 290 | mbedtls_mpi_mod_ext_rep ext_rep ); |
Janos Follath | 5933f69 | 2022-11-02 14:35:17 +0000 | [diff] [blame] | 291 | |
Janos Follath | 41427de | 2022-11-24 19:04:54 +0000 | [diff] [blame] | 292 | /** Write a residue into a byte buffer. |
Minos Galanakis | 81f4b11 | 2022-11-10 14:40:38 +0000 | [diff] [blame] | 293 | * |
Janos Follath | 41427de | 2022-11-24 19:04:54 +0000 | [diff] [blame] | 294 | * The modulus \p m must be the modulus associated with \p r (see |
| 295 | * mbedtls_mpi_mod_residue_setup() and mbedtls_mpi_mod_read()). |
Minos Galanakis | 81f4b11 | 2022-11-10 14:40:38 +0000 | [diff] [blame] | 296 | * |
Janos Follath | 41427de | 2022-11-24 19:04:54 +0000 | [diff] [blame] | 297 | * The residue will be automatically converted from the internal representation |
| 298 | * based on the value of `m->int_rep` field. |
| 299 | * |
| 300 | * \warning If the buffer is smaller than `m->bits`, the number of |
Janos Follath | 6eb92c0 | 2022-11-26 17:34:37 +0000 | [diff] [blame] | 301 | * leading zeroes is leaked through timing. If \p r is |
Janos Follath | 41427de | 2022-11-24 19:04:54 +0000 | [diff] [blame] | 302 | * secret, the caller must ensure that \p buflen is at least |
| 303 | * (`m->bits`+7)/8. |
| 304 | * |
Janos Follath | 1f8afa2 | 2022-11-28 14:32:33 +0000 | [diff] [blame] | 305 | * \param[in] r The address of the residue. It must have the same number of |
| 306 | * limbs as the modulus \p m. (\p r is an input parameter, but |
| 307 | * its value will be modified during execution and restored |
| 308 | * before the function returns.) |
| 309 | * \param[in] m The address of the modulus associated with \r. |
| 310 | * \param[out] buf The output buffer to export to. |
Janos Follath | 3e3fc91 | 2022-11-24 18:02:46 +0000 | [diff] [blame] | 311 | * \param buflen The length in bytes of \p buf. |
Janos Follath | 41427de | 2022-11-24 19:04:54 +0000 | [diff] [blame] | 312 | * \param ext_rep The endianness in which the number should be written into |
| 313 | * the output buffer. |
Minos Galanakis | 81f4b11 | 2022-11-10 14:40:38 +0000 | [diff] [blame] | 314 | * |
| 315 | * \return \c 0 if successful. |
| 316 | * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p buf isn't |
Janos Follath | 41427de | 2022-11-24 19:04:54 +0000 | [diff] [blame] | 317 | * large enough to hold the value of \p r (without leading |
| 318 | * zeroes). |
Janos Follath | fc6fbb4 | 2022-11-25 15:43:17 +0000 | [diff] [blame] | 319 | * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if \p ext_rep is invalid. |
Janos Follath | 1f8afa2 | 2022-11-28 14:32:33 +0000 | [diff] [blame] | 320 | * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if couldn't allocate enough |
| 321 | * memory for conversion. Can occur only for moduli with |
| 322 | * MBEDTLS_MPI_MOD_REP_MONTGOMERY. |
Minos Galanakis | 81f4b11 | 2022-11-10 14:40:38 +0000 | [diff] [blame] | 323 | */ |
Minos Galanakis | 8b37545 | 2022-11-24 11:04:11 +0000 | [diff] [blame] | 324 | int mbedtls_mpi_mod_write( const mbedtls_mpi_mod_residue *r, |
| 325 | const mbedtls_mpi_mod_modulus *m, |
Minos Galanakis | 81f4b11 | 2022-11-10 14:40:38 +0000 | [diff] [blame] | 326 | unsigned char *buf, |
Janos Follath | 3e3fc91 | 2022-11-24 18:02:46 +0000 | [diff] [blame] | 327 | size_t buflen, |
| 328 | mbedtls_mpi_mod_ext_rep ext_rep ); |
Janos Follath | 5933f69 | 2022-11-02 14:35:17 +0000 | [diff] [blame] | 329 | /* END MERGE SLOT 7 */ |
| 330 | |
| 331 | /* BEGIN MERGE SLOT 8 */ |
| 332 | |
| 333 | /* END MERGE SLOT 8 */ |
| 334 | |
| 335 | /* BEGIN MERGE SLOT 9 */ |
| 336 | |
| 337 | /* END MERGE SLOT 9 */ |
| 338 | |
| 339 | /* BEGIN MERGE SLOT 10 */ |
| 340 | |
| 341 | /* END MERGE SLOT 10 */ |
| 342 | |
Gabor Mezei | f049dbf | 2022-07-18 23:02:33 +0200 | [diff] [blame] | 343 | #endif /* MBEDTLS_BIGNUM_MOD_H */ |