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 | fc6fbb4 | 2022-11-25 15:43:17 +0000 | [diff] [blame] | 77 | * 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] | 78 | * |
Janos Follath | fc6fbb4 | 2022-11-25 15:43:17 +0000 | [diff] [blame] | 79 | * The memory pointed to by \p p will be used by the resulting residue structure. |
| 80 | * The value at the pointed-to 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-is |
Janos Follath | 41427de | 2022-11-24 19:04:54 +0000 | [diff] [blame] | 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 |
Janos Follath | 6eb92c0 | 2022-11-26 17:34:37 +0000 | [diff] [blame] | 85 | * should only be used in operations where the modulus is \p m. |
Janos Follath | 41427de | 2022-11-24 19:04:54 +0000 | [diff] [blame] | 86 | * |
Janos Follath | ee530cc | 2022-11-25 15:54:40 +0000 | [diff] [blame] | 87 | * \param[out] r The address of the residue to setup. |
Janos Follath | 6b8a4ad | 2022-08-19 10:58:34 +0100 | [diff] [blame] | 88 | * \param[in] m The address of the modulus related to \p r. |
Janos Follath | fc6fbb4 | 2022-11-25 15:43:17 +0000 | [diff] [blame] | 89 | * \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] | 90 | * The memory pointed to by \p p will be used by \p r and must |
| 91 | * not be modified in any way until after |
Minos Galanakis | aed832a | 2022-11-24 09:09:47 +0000 | [diff] [blame] | 92 | * mbedtls_mpi_mod_residue_release() is called. The data |
Janos Follath | fc6fbb4 | 2022-11-25 15:43:17 +0000 | [diff] [blame] | 93 | * pointed to by \p p must be less than the modulus (the value |
| 94 | * pointed to by `m->p`) and already in the representation |
Janos Follath | 41427de | 2022-11-24 19:04:54 +0000 | [diff] [blame] | 95 | * indicated by `m->int_rep`. |
Janos Follath | ee530cc | 2022-11-25 15:54:40 +0000 | [diff] [blame] | 96 | * \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] | 97 | * of limbs in the modulus \p m. |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 98 | * |
| 99 | * \return \c 0 if successful. |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 100 | * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if \p p_limbs is less than the |
| 101 | * 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] | 102 | */ |
Gabor Mezei | f049dbf | 2022-07-18 23:02:33 +0200 | [diff] [blame] | 103 | int mbedtls_mpi_mod_residue_setup( mbedtls_mpi_mod_residue *r, |
Janos Follath | 6b8a4ad | 2022-08-19 10:58:34 +0100 | [diff] [blame] | 104 | const mbedtls_mpi_mod_modulus *m, |
Janos Follath | 8b718b5 | 2022-07-25 11:31:02 +0100 | [diff] [blame] | 105 | mbedtls_mpi_uint *p, |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 106 | size_t p_limbs ); |
Gabor Mezei | f049dbf | 2022-07-18 23:02:33 +0200 | [diff] [blame] | 107 | |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 108 | /** Unbind elements of a residue structure. |
| 109 | * |
Janos Follath | dae1147 | 2022-08-08 11:50:02 +0100 | [diff] [blame] | 110 | * This function removes the reference to the limb array that was passed to |
| 111 | * mbedtls_mpi_mod_residue_setup() to make it safe to free or use again. |
| 112 | * |
| 113 | * This function invalidates \p r and it must not be used until after |
| 114 | * mbedtls_mpi_mod_residue_setup() is called on it again. |
| 115 | * |
Janos Follath | 6b8a4ad | 2022-08-19 10:58:34 +0100 | [diff] [blame] | 116 | * \param[out] r The address of residue to release. |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 117 | */ |
| 118 | void mbedtls_mpi_mod_residue_release( mbedtls_mpi_mod_residue *r ); |
| 119 | |
| 120 | /** Initialize a modulus structure. |
| 121 | * |
Janos Follath | a95f204 | 2022-08-19 12:09:17 +0100 | [diff] [blame] | 122 | * \param[out] m The address of the modulus structure to initialize. |
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 | void mbedtls_mpi_mod_modulus_init( mbedtls_mpi_mod_modulus *m ); |
| 125 | |
Janos Follath | 6318468 | 2022-08-11 17:42:59 +0100 | [diff] [blame] | 126 | /** Setup a modulus structure. |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 127 | * |
Janos Follath | 6b8a4ad | 2022-08-19 10:58:34 +0100 | [diff] [blame] | 128 | * \param[out] m The address of the modulus structure to populate. |
| 129 | * \param[in] p The address of the limb array storing the value of \p m. |
| 130 | * The memory pointed to by \p p will be used by \p m and must |
| 131 | * not be modified in any way until after |
Janos Follath | dae1147 | 2022-08-08 11:50:02 +0100 | [diff] [blame] | 132 | * mbedtls_mpi_mod_modulus_free() is called. |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 133 | * \param p_limbs The number of limbs of \p p. |
Janos Follath | dae1147 | 2022-08-08 11:50:02 +0100 | [diff] [blame] | 134 | * \param int_rep The internal representation to be used for residues |
| 135 | * associated with \p m (see #mbedtls_mpi_mod_rep_selector). |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 136 | * |
| 137 | * \return \c 0 if successful. |
Janos Follath | ee530cc | 2022-11-25 15:54:40 +0000 | [diff] [blame] | 138 | * \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] | 139 | */ |
Gabor Mezei | f049dbf | 2022-07-18 23:02:33 +0200 | [diff] [blame] | 140 | int mbedtls_mpi_mod_modulus_setup( mbedtls_mpi_mod_modulus *m, |
Janos Follath | ed5c8d3 | 2022-08-15 11:50:22 +0100 | [diff] [blame] | 141 | const mbedtls_mpi_uint *p, |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 142 | size_t p_limbs, |
Janos Follath | 296ea66 | 2022-08-11 14:58:29 +0100 | [diff] [blame] | 143 | mbedtls_mpi_mod_rep_selector int_rep ); |
Gabor Mezei | f049dbf | 2022-07-18 23:02:33 +0200 | [diff] [blame] | 144 | |
Janos Follath | dae1147 | 2022-08-08 11:50:02 +0100 | [diff] [blame] | 145 | /** Free elements of a modulus structure. |
| 146 | * |
| 147 | * This function frees any memory allocated by mbedtls_mpi_mod_modulus_setup(). |
| 148 | * |
| 149 | * \warning This function does not free the limb array passed to |
| 150 | * mbedtls_mpi_mod_modulus_setup() only removes the reference to it, |
| 151 | * making it safe to free or to use it again. |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 152 | * |
Janos Follath | a95f204 | 2022-08-19 12:09:17 +0100 | [diff] [blame] | 153 | * \param[in,out] m The address of the modulus structure to free. |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 154 | */ |
| 155 | void mbedtls_mpi_mod_modulus_free( mbedtls_mpi_mod_modulus *m ); |
| 156 | |
Janos Follath | 5933f69 | 2022-11-02 14:35:17 +0000 | [diff] [blame] | 157 | /* BEGIN MERGE SLOT 1 */ |
| 158 | |
| 159 | /* END MERGE SLOT 1 */ |
| 160 | |
| 161 | /* BEGIN MERGE SLOT 2 */ |
| 162 | |
| 163 | /* END MERGE SLOT 2 */ |
| 164 | |
| 165 | /* BEGIN MERGE SLOT 3 */ |
Tom Cosgrove | 62b2048 | 2022-12-01 14:27:37 +0000 | [diff] [blame] | 166 | /** |
| 167 | * \brief Perform a fixed-size modular subtraction. |
| 168 | * |
| 169 | * Calculate `A - B modulo N`. |
| 170 | * |
| 171 | * \p A, \p B and \p X must all have the same number of limbs as \p N. |
| 172 | * |
| 173 | * \p X may be aliased to \p A or \p B, or even both, but may not overlap |
| 174 | * either otherwise. |
| 175 | * |
| 176 | * \note This function does not check that \p A or \p B are in canonical |
| 177 | * form (that is, are < \p N) - that will have been done by |
| 178 | * mbedtls_mpi_mod_residue_setup(). |
| 179 | * |
| 180 | * \param[out] X The address of the result MPI. Must be initialized. |
| 181 | * Must have the same number of limbs as the modulus \p N. |
| 182 | * \param[in] A The address of the first MPI. |
| 183 | * \param[in] B The address of the second MPI. |
| 184 | * \param[in] N The address of the modulus. Used to perform a modulo |
| 185 | * operation on the result of the subtraction. |
| 186 | * |
| 187 | * \return \c 0 if successful. |
| 188 | * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if the given MPIs do not |
| 189 | * have the correct number of limbs. |
| 190 | */ |
| 191 | int mbedtls_mpi_mod_sub( mbedtls_mpi_mod_residue *X, |
| 192 | const mbedtls_mpi_mod_residue *A, |
| 193 | const mbedtls_mpi_mod_residue *B, |
| 194 | const mbedtls_mpi_mod_modulus *N ); |
Janos Follath | 5933f69 | 2022-11-02 14:35:17 +0000 | [diff] [blame] | 195 | /* END MERGE SLOT 3 */ |
| 196 | |
| 197 | /* BEGIN MERGE SLOT 4 */ |
| 198 | |
| 199 | /* END MERGE SLOT 4 */ |
| 200 | |
| 201 | /* BEGIN MERGE SLOT 5 */ |
Werner Lewis | e1b6b7c | 2022-11-29 12:25:05 +0000 | [diff] [blame] | 202 | /** |
| 203 | * \brief Perform a fixed-size modular addition. |
| 204 | * |
| 205 | * Calculate `A + B modulo N`. |
| 206 | * |
Werner Lewis | eed01aa | 2022-12-13 17:18:17 +0000 | [diff] [blame^] | 207 | * \p A, \p B and \p X must all be associated with the modulus \p N and must |
| 208 | * all have the same number of limbs as \p N. |
Werner Lewis | e1b6b7c | 2022-11-29 12:25:05 +0000 | [diff] [blame] | 209 | * |
| 210 | * \p X may be aliased to \p A or \p B, or even both, but may not overlap |
| 211 | * either otherwise. |
| 212 | * |
| 213 | * \note This function does not check that \p A or \p B are in canonical |
| 214 | * form (that is, are < \p N) - that will have been done by |
| 215 | * mbedtls_mpi_mod_residue_setup(). |
| 216 | * |
Werner Lewis | eed01aa | 2022-12-13 17:18:17 +0000 | [diff] [blame^] | 217 | * \param[out] X The address of the result residue. Must be initialized. |
Werner Lewis | e1b6b7c | 2022-11-29 12:25:05 +0000 | [diff] [blame] | 218 | * Must have the same number of limbs as the modulus \p N. |
Werner Lewis | eed01aa | 2022-12-13 17:18:17 +0000 | [diff] [blame^] | 219 | * \param[in] A The address of the first input residue. |
| 220 | * \param[in] B The address of the second input residue. |
Werner Lewis | e1b6b7c | 2022-11-29 12:25:05 +0000 | [diff] [blame] | 221 | * \param[in] N The address of the modulus. Used to perform a modulo |
| 222 | * operation on the result of the addition. |
| 223 | * |
| 224 | * \return \c 0 if successful. |
| 225 | * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if the given MPIs do not |
| 226 | * have the correct number of limbs. |
| 227 | */ |
| 228 | int mbedtls_mpi_mod_add( mbedtls_mpi_mod_residue *X, |
| 229 | const mbedtls_mpi_mod_residue *A, |
| 230 | const mbedtls_mpi_mod_residue *B, |
| 231 | const mbedtls_mpi_mod_modulus *N ); |
Janos Follath | 5933f69 | 2022-11-02 14:35:17 +0000 | [diff] [blame] | 232 | /* END MERGE SLOT 5 */ |
| 233 | |
| 234 | /* BEGIN MERGE SLOT 6 */ |
| 235 | |
| 236 | /* END MERGE SLOT 6 */ |
| 237 | |
| 238 | /* BEGIN MERGE SLOT 7 */ |
Janos Follath | 41427de | 2022-11-24 19:04:54 +0000 | [diff] [blame] | 239 | /** Read a residue from a byte buffer. |
Minos Galanakis | 81f4b11 | 2022-11-10 14:40:38 +0000 | [diff] [blame] | 240 | * |
Janos Follath | 41427de | 2022-11-24 19:04:54 +0000 | [diff] [blame] | 241 | * The residue will be automatically converted to the internal representation |
Janos Follath | fc6fbb4 | 2022-11-25 15:43:17 +0000 | [diff] [blame] | 242 | * based on the value of the `m->int_rep` field. |
Minos Galanakis | 81f4b11 | 2022-11-10 14:40:38 +0000 | [diff] [blame] | 243 | * |
Janos Follath | 41427de | 2022-11-24 19:04:54 +0000 | [diff] [blame] | 244 | * The modulus \p m will be the modulus associated with \p r. The residue \p r |
| 245 | * should only be used in operations where the modulus is \p m or a modulus |
| 246 | * equivalent to \p m (in the sense that all their fields or memory pointed by |
| 247 | * their fields hold the same value). |
| 248 | * |
Janos Follath | 1f8afa2 | 2022-11-28 14:32:33 +0000 | [diff] [blame] | 249 | * \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] | 250 | * number of limbs as the modulus \p m. |
Janos Follath | 1f8afa2 | 2022-11-28 14:32:33 +0000 | [diff] [blame] | 251 | * \param[in] m The address of the modulus. |
| 252 | * \param[in] buf The input buffer to import from. |
Janos Follath | 3e3fc91 | 2022-11-24 18:02:46 +0000 | [diff] [blame] | 253 | * \param buflen The length in bytes of \p buf. |
| 254 | * \param ext_rep The endianness of the number in the input buffer. |
Minos Galanakis | 81f4b11 | 2022-11-10 14:40:38 +0000 | [diff] [blame] | 255 | * |
| 256 | * \return \c 0 if successful. |
Janos Follath | 41427de | 2022-11-24 19:04:54 +0000 | [diff] [blame] | 257 | * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p r isn't |
Minos Galanakis | 81f4b11 | 2022-11-10 14:40:38 +0000 | [diff] [blame] | 258 | * large enough to hold the value in \p buf. |
Janos Follath | 41427de | 2022-11-24 19:04:54 +0000 | [diff] [blame] | 259 | * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if \p ext_rep |
| 260 | * 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] | 261 | */ |
| 262 | int mbedtls_mpi_mod_read( mbedtls_mpi_mod_residue *r, |
Minos Galanakis | 8b37545 | 2022-11-24 11:04:11 +0000 | [diff] [blame] | 263 | const mbedtls_mpi_mod_modulus *m, |
| 264 | const unsigned char *buf, |
Janos Follath | 3e3fc91 | 2022-11-24 18:02:46 +0000 | [diff] [blame] | 265 | size_t buflen, |
| 266 | mbedtls_mpi_mod_ext_rep ext_rep ); |
Janos Follath | 5933f69 | 2022-11-02 14:35:17 +0000 | [diff] [blame] | 267 | |
Janos Follath | 41427de | 2022-11-24 19:04:54 +0000 | [diff] [blame] | 268 | /** Write a residue into a byte buffer. |
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 must be the modulus associated with \p r (see |
| 271 | * mbedtls_mpi_mod_residue_setup() and mbedtls_mpi_mod_read()). |
Minos Galanakis | 81f4b11 | 2022-11-10 14:40:38 +0000 | [diff] [blame] | 272 | * |
Janos Follath | 41427de | 2022-11-24 19:04:54 +0000 | [diff] [blame] | 273 | * The residue will be automatically converted from the internal representation |
| 274 | * based on the value of `m->int_rep` field. |
| 275 | * |
| 276 | * \warning If the buffer is smaller than `m->bits`, the number of |
Janos Follath | 6eb92c0 | 2022-11-26 17:34:37 +0000 | [diff] [blame] | 277 | * leading zeroes is leaked through timing. If \p r is |
Janos Follath | 41427de | 2022-11-24 19:04:54 +0000 | [diff] [blame] | 278 | * secret, the caller must ensure that \p buflen is at least |
| 279 | * (`m->bits`+7)/8. |
| 280 | * |
Janos Follath | 1f8afa2 | 2022-11-28 14:32:33 +0000 | [diff] [blame] | 281 | * \param[in] r The address of the residue. It must have the same number of |
| 282 | * limbs as the modulus \p m. (\p r is an input parameter, but |
| 283 | * its value will be modified during execution and restored |
| 284 | * before the function returns.) |
| 285 | * \param[in] m The address of the modulus associated with \r. |
| 286 | * \param[out] buf The output buffer to export to. |
Janos Follath | 3e3fc91 | 2022-11-24 18:02:46 +0000 | [diff] [blame] | 287 | * \param buflen The length in bytes of \p buf. |
Janos Follath | 41427de | 2022-11-24 19:04:54 +0000 | [diff] [blame] | 288 | * \param ext_rep The endianness in which the number should be written into |
| 289 | * the output buffer. |
Minos Galanakis | 81f4b11 | 2022-11-10 14:40:38 +0000 | [diff] [blame] | 290 | * |
| 291 | * \return \c 0 if successful. |
| 292 | * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p buf isn't |
Janos Follath | 41427de | 2022-11-24 19:04:54 +0000 | [diff] [blame] | 293 | * large enough to hold the value of \p r (without leading |
| 294 | * zeroes). |
Janos Follath | fc6fbb4 | 2022-11-25 15:43:17 +0000 | [diff] [blame] | 295 | * \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] | 296 | * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if couldn't allocate enough |
| 297 | * memory for conversion. Can occur only for moduli with |
| 298 | * MBEDTLS_MPI_MOD_REP_MONTGOMERY. |
Minos Galanakis | 81f4b11 | 2022-11-10 14:40:38 +0000 | [diff] [blame] | 299 | */ |
Minos Galanakis | 8b37545 | 2022-11-24 11:04:11 +0000 | [diff] [blame] | 300 | int mbedtls_mpi_mod_write( const mbedtls_mpi_mod_residue *r, |
| 301 | const mbedtls_mpi_mod_modulus *m, |
Minos Galanakis | 81f4b11 | 2022-11-10 14:40:38 +0000 | [diff] [blame] | 302 | unsigned char *buf, |
Janos Follath | 3e3fc91 | 2022-11-24 18:02:46 +0000 | [diff] [blame] | 303 | size_t buflen, |
| 304 | mbedtls_mpi_mod_ext_rep ext_rep ); |
Janos Follath | 5933f69 | 2022-11-02 14:35:17 +0000 | [diff] [blame] | 305 | /* END MERGE SLOT 7 */ |
| 306 | |
| 307 | /* BEGIN MERGE SLOT 8 */ |
| 308 | |
| 309 | /* END MERGE SLOT 8 */ |
| 310 | |
| 311 | /* BEGIN MERGE SLOT 9 */ |
| 312 | |
| 313 | /* END MERGE SLOT 9 */ |
| 314 | |
| 315 | /* BEGIN MERGE SLOT 10 */ |
| 316 | |
| 317 | /* END MERGE SLOT 10 */ |
| 318 | |
Gabor Mezei | f049dbf | 2022-07-18 23:02:33 +0200 | [diff] [blame] | 319 | #endif /* MBEDTLS_BIGNUM_MOD_H */ |