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