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 |
Werner Lewis | 6bb49ba | 2022-12-15 16:58:44 +0000 | [diff] [blame] | 13 | * named \c N and is usually input-only. Functions which take a parameter |
| 14 | * of type \c const #mbedtls_mpi_mod_modulus* must not modify its value. |
Werner Lewis | 5e9d2e9 | 2022-12-12 14:00:25 +0000 | [diff] [blame] | 15 | * - **Bignum parameters**: Bignums are passed as pointers to an array of |
| 16 | * limbs or to a #mbedtls_mpi_mod_residue structure. A limb has the type |
| 17 | * #mbedtls_mpi_uint. Residues must be initialized before use, and must be |
Werner Lewis | 214ae64 | 2022-12-15 10:57:59 +0000 | [diff] [blame] | 18 | * associated with the modulus \c N. Unless otherwise specified: |
| 19 | * - Bignum parameters called \c A, \c B, ... are inputs and are not |
Werner Lewis | 6bb49ba | 2022-12-15 16:58:44 +0000 | [diff] [blame] | 20 | * modified by the function. Functions which take a parameter of |
| 21 | * type \c const #mbedtls_mpi_mod_residue* must not modify its value. |
Werner Lewis | 214ae64 | 2022-12-15 10:57:59 +0000 | [diff] [blame] | 22 | * - Bignum parameters called \c X, \c Y, ... are outputs or input-output. |
Werner Lewis | 945a165 | 2022-12-14 15:24:46 +0000 | [diff] [blame] | 23 | * The initial bignum value of output-only parameters is ignored, but |
Werner Lewis | 0f644f4 | 2022-12-15 14:13:32 +0000 | [diff] [blame] | 24 | * they must be set up and associated with the modulus \c N. Some |
| 25 | * functions (typically constant-flow) require that the limbs in an |
| 26 | * output residue are initialized. |
Werner Lewis | 756a34a | 2022-12-15 14:53:43 +0000 | [diff] [blame] | 27 | * - Bignum parameters called \c p are inputs used to set up a modulus or |
Werner Lewis | 5e9d2e9 | 2022-12-12 14:00:25 +0000 | [diff] [blame] | 28 | * residue. These must be pointers to an array of limbs. |
Werner Lewis | 214ae64 | 2022-12-15 10:57:59 +0000 | [diff] [blame] | 29 | * - \c T is a temporary storage area. The initial content of such a |
Werner Lewis | 5e9d2e9 | 2022-12-12 14:00:25 +0000 | [diff] [blame] | 30 | * parameter is ignored and the final content is unspecified. |
Werner Lewis | 756a34a | 2022-12-15 14:53:43 +0000 | [diff] [blame] | 31 | * - Some functions use different names, such as \c r for the residue. |
Werner Lewis | 5e9d2e9 | 2022-12-12 14:00:25 +0000 | [diff] [blame] | 32 | * - **Bignum sizes**: bignum sizes are always expressed in limbs. Both |
Werner Lewis | 2e70b9a | 2022-12-14 15:48:31 +0000 | [diff] [blame] | 33 | * #mbedtls_mpi_mod_modulus and #mbedtls_mpi_mod_residue have a \c limbs |
| 34 | * member storing its size. All bignum parameters must have the same |
| 35 | * number of limbs as the modulus. All bignum sizes must be at least 1 and |
| 36 | * must be significantly less than #SIZE_MAX. The behavior if a size is 0 is |
| 37 | * undefined. |
Werner Lewis | 5e9d2e9 | 2022-12-12 14:00:25 +0000 | [diff] [blame] | 38 | * - **Bignum representation**: the representation of inputs and outputs is |
Werner Lewis | 214ae64 | 2022-12-15 10:57:59 +0000 | [diff] [blame] | 39 | * specified by the \c int_rep field of the modulus. |
Werner Lewis | 5e9d2e9 | 2022-12-12 14:00:25 +0000 | [diff] [blame] | 40 | * - **Parameter ordering**: for bignum parameters, outputs come before inputs. |
Werner Lewis | a306886 | 2022-12-14 15:57:12 +0000 | [diff] [blame] | 41 | * The modulus is passed after residues. Temporaries come last. |
Werner Lewis | 5e9d2e9 | 2022-12-12 14:00:25 +0000 | [diff] [blame] | 42 | * - **Aliasing**: in general, output bignums may be aliased to one or more |
| 43 | * inputs. Modulus values may not be aliased to any other parameter. Outputs |
| 44 | * may not be aliased to one another. Temporaries may not be aliased to any |
| 45 | * other parameter. |
| 46 | * - **Overlap**: apart from aliasing of residue pointers (where two residue |
| 47 | * arguments are equal pointers), overlap is not supported and may result |
| 48 | * in undefined behavior. |
Werner Lewis | 2bd263d | 2022-12-14 15:32:31 +0000 | [diff] [blame] | 49 | * - **Error handling**: functions generally check compatibility of input |
Werner Lewis | 5e9d2e9 | 2022-12-12 14:00:25 +0000 | [diff] [blame] | 50 | * sizes. Most functions will not check that input values are in canonical |
Werner Lewis | 214ae64 | 2022-12-15 10:57:59 +0000 | [diff] [blame] | 51 | * form (i.e. that \c A < \c N), this is only checked during setup of a |
Werner Lewis | 5e9d2e9 | 2022-12-12 14:00:25 +0000 | [diff] [blame] | 52 | * residue structure. |
Werner Lewis | 1d89ebf | 2022-12-14 17:08:43 +0000 | [diff] [blame] | 53 | * - **Modular representatives**: all functions expect inputs to be in the |
Werner Lewis | 214ae64 | 2022-12-15 10:57:59 +0000 | [diff] [blame] | 54 | * range [0, \c N - 1] and guarantee outputs in the range [0, \c N - 1]. |
Werner Lewis | 1d89ebf | 2022-12-14 17:08:43 +0000 | [diff] [blame] | 55 | * Residues are set up with an associated modulus, and operations are only |
| 56 | * guaranteed to work if the modulus is associated with all residue |
| 57 | * parameters. If a residue is passed with a modulus other than the one it |
| 58 | * is associated with, then it may be out of range. If an input is out of |
| 59 | * range, outputs are fully unspecified, though bignum values out of range |
| 60 | * should not cause buffer overflows (beware that this is not extensively |
| 61 | * tested). |
Gilles Peskine | 7f887bd | 2022-09-27 13:12:30 +0200 | [diff] [blame] | 62 | */ |
| 63 | |
| 64 | /* |
Gabor Mezei | f049dbf | 2022-07-18 23:02:33 +0200 | [diff] [blame] | 65 | * Copyright The Mbed TLS Contributors |
| 66 | * SPDX-License-Identifier: Apache-2.0 |
| 67 | * |
| 68 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 69 | * not use this file except in compliance with the License. |
| 70 | * You may obtain a copy of the License at |
| 71 | * |
| 72 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 73 | * |
| 74 | * Unless required by applicable law or agreed to in writing, software |
| 75 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 76 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 77 | * See the License for the specific language governing permissions and |
| 78 | * limitations under the License. |
| 79 | */ |
| 80 | |
| 81 | #ifndef MBEDTLS_BIGNUM_MOD_H |
| 82 | #define MBEDTLS_BIGNUM_MOD_H |
| 83 | |
| 84 | #include "common.h" |
| 85 | |
| 86 | #if defined(MBEDTLS_BIGNUM_C) |
| 87 | #include "mbedtls/bignum.h" |
| 88 | #endif |
| 89 | |
Gilles Peskine | eb2e77f | 2022-12-20 19:22:44 +0100 | [diff] [blame] | 90 | /** How residues associated with a modulus are represented. |
| 91 | * |
| 92 | * This also determines which fields of the modulus structure are valid and |
| 93 | * what their contents are (see #mbedtls_mpi_mod_modulus). |
| 94 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 95 | typedef enum { |
Gilles Peskine | eb2e77f | 2022-12-20 19:22:44 +0100 | [diff] [blame] | 96 | /** Representation not chosen (makes the modulus structure invalid). */ |
Janos Follath | 296ea66 | 2022-08-11 14:58:29 +0100 | [diff] [blame] | 97 | MBEDTLS_MPI_MOD_REP_INVALID = 0, |
Gilles Peskine | eb2e77f | 2022-12-20 19:22:44 +0100 | [diff] [blame] | 98 | /* Skip 1 as it is slightly easier to accidentally pass to functions. */ |
| 99 | /** Montgomery representation. */ |
Janos Follath | 296ea66 | 2022-08-11 14:58:29 +0100 | [diff] [blame] | 100 | MBEDTLS_MPI_MOD_REP_MONTGOMERY = 2, |
Gilles Peskine | eb2e77f | 2022-12-20 19:22:44 +0100 | [diff] [blame] | 101 | /** TODO: document this. |
| 102 | * |
| 103 | * Residues are in canonical representation. |
| 104 | */ |
| 105 | MBEDTLS_MPI_MOD_REP_OPT_RED, |
Janos Follath | 296ea66 | 2022-08-11 14:58:29 +0100 | [diff] [blame] | 106 | } mbedtls_mpi_mod_rep_selector; |
| 107 | |
| 108 | /* Make mbedtls_mpi_mod_rep_selector and mbedtls_mpi_mod_ext_rep disjoint to |
| 109 | * make it easier to catch when they are accidentally swapped. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 110 | typedef enum { |
Janos Follath | 296ea66 | 2022-08-11 14:58:29 +0100 | [diff] [blame] | 111 | MBEDTLS_MPI_MOD_EXT_REP_INVALID = 0, |
| 112 | MBEDTLS_MPI_MOD_EXT_REP_LE = 8, |
| 113 | MBEDTLS_MPI_MOD_EXT_REP_BE |
| 114 | } mbedtls_mpi_mod_ext_rep; |
Gabor Mezei | f049dbf | 2022-07-18 23:02:33 +0200 | [diff] [blame] | 115 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 116 | typedef struct { |
Gabor Mezei | f049dbf | 2022-07-18 23:02:33 +0200 | [diff] [blame] | 117 | mbedtls_mpi_uint *p; |
Gabor Mezei | fd65e82 | 2022-08-12 18:09:12 +0200 | [diff] [blame] | 118 | size_t limbs; |
Gabor Mezei | f049dbf | 2022-07-18 23:02:33 +0200 | [diff] [blame] | 119 | } mbedtls_mpi_mod_residue; |
| 120 | |
Hanno Becker | cd860df | 2022-08-18 16:23:05 +0100 | [diff] [blame] | 121 | typedef struct { |
| 122 | mbedtls_mpi_uint const *rr; /* The residue for 2^{2*n*biL} mod N */ |
| 123 | mbedtls_mpi_uint mm; /* Montgomery const for -N^{-1} mod 2^{ciL} */ |
| 124 | } mbedtls_mpi_mont_struct; |
| 125 | |
Gabor Mezei | 89e3146 | 2022-08-12 15:36:56 +0200 | [diff] [blame] | 126 | typedef void *mbedtls_mpi_opt_red_struct; |
Gabor Mezei | f049dbf | 2022-07-18 23:02:33 +0200 | [diff] [blame] | 127 | |
| 128 | typedef struct { |
Janos Follath | ed5c8d3 | 2022-08-15 11:50:22 +0100 | [diff] [blame] | 129 | const mbedtls_mpi_uint *p; |
Gabor Mezei | fd65e82 | 2022-08-12 18:09:12 +0200 | [diff] [blame] | 130 | size_t limbs; // number of limbs |
| 131 | size_t bits; // bitlen of p |
Janos Follath | 296ea66 | 2022-08-11 14:58:29 +0100 | [diff] [blame] | 132 | mbedtls_mpi_mod_rep_selector int_rep; // selector to signal the active member of the union |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 133 | union rep { |
Gilles Peskine | eb2e77f | 2022-12-20 19:22:44 +0100 | [diff] [blame] | 134 | /* if int_rep == #MBEDTLS_MPI_MOD_REP_MONTGOMERY */ |
Gabor Mezei | f049dbf | 2022-07-18 23:02:33 +0200 | [diff] [blame] | 135 | mbedtls_mpi_mont_struct mont; |
Gilles Peskine | eb2e77f | 2022-12-20 19:22:44 +0100 | [diff] [blame] | 136 | /* if int_rep == #MBEDTLS_MPI_MOD_REP_OPT_RED */ |
Gabor Mezei | f049dbf | 2022-07-18 23:02:33 +0200 | [diff] [blame] | 137 | mbedtls_mpi_opt_red_struct ored; |
| 138 | } rep; |
| 139 | } mbedtls_mpi_mod_modulus; |
| 140 | |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 141 | /** Setup a residue structure. |
| 142 | * |
Mihir Raj Singh | b13a589 | 2023-01-11 19:49:00 +0530 | [diff] [blame] | 143 | * The residue will be set up with the buffer \p p and modulus \p N. |
Janos Follath | 41427de | 2022-11-24 19:04:54 +0000 | [diff] [blame] | 144 | * |
Janos Follath | fc6fbb4 | 2022-11-25 15:43:17 +0000 | [diff] [blame] | 145 | * The memory pointed to by \p p will be used by the resulting residue structure. |
| 146 | * The value at the pointed-to memory will be the initial value of \p r and must |
| 147 | * hold a value that is less than the modulus. This value will be used as-is |
Mihir Raj Singh | b13a589 | 2023-01-11 19:49:00 +0530 | [diff] [blame] | 148 | * and interpreted according to the value of the `N->int_rep` field. |
Janos Follath | 41427de | 2022-11-24 19:04:54 +0000 | [diff] [blame] | 149 | * |
Mihir Raj Singh | b13a589 | 2023-01-11 19:49:00 +0530 | [diff] [blame] | 150 | * The modulus \p N will be the modulus associated with \p r. The residue \p r |
| 151 | * should only be used in operations where the modulus is \p N. |
Janos Follath | 41427de | 2022-11-24 19:04:54 +0000 | [diff] [blame] | 152 | * |
Janos Follath | ee530cc | 2022-11-25 15:54:40 +0000 | [diff] [blame] | 153 | * \param[out] r The address of the residue to setup. |
Mihir Raj Singh | b13a589 | 2023-01-11 19:49:00 +0530 | [diff] [blame] | 154 | * \param[in] N The address of the modulus related to \p r. |
Janos Follath | fc6fbb4 | 2022-11-25 15:43:17 +0000 | [diff] [blame] | 155 | * \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] | 156 | * The memory pointed to by \p p will be used by \p r and must |
| 157 | * not be modified in any way until after |
Minos Galanakis | aed832a | 2022-11-24 09:09:47 +0000 | [diff] [blame] | 158 | * mbedtls_mpi_mod_residue_release() is called. The data |
Janos Follath | fc6fbb4 | 2022-11-25 15:43:17 +0000 | [diff] [blame] | 159 | * pointed to by \p p must be less than the modulus (the value |
Mihir Raj Singh | b13a589 | 2023-01-11 19:49:00 +0530 | [diff] [blame] | 160 | * pointed to by `N->p`) and already in the representation |
| 161 | * indicated by `N->int_rep`. |
Janos Follath | ee530cc | 2022-11-25 15:54:40 +0000 | [diff] [blame] | 162 | * \param p_limbs The number of limbs of \p p. Must be the same as the number |
Mihir Raj Singh | b13a589 | 2023-01-11 19:49:00 +0530 | [diff] [blame] | 163 | * of limbs in the modulus \p N. |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 164 | * |
| 165 | * \return \c 0 if successful. |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 166 | * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if \p p_limbs is less than the |
Mihir Raj Singh | b13a589 | 2023-01-11 19:49:00 +0530 | [diff] [blame] | 167 | * limbs in \p N or if \p p is not less than \p N. |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 168 | */ |
Mihir Raj Singh | 432cacf | 2023-01-11 21:12:46 +0530 | [diff] [blame] | 169 | int mbedtls_mpi_mod_residue_setup(mbedtls_mpi_mod_residue *r, |
| 170 | const mbedtls_mpi_mod_modulus *N, |
| 171 | mbedtls_mpi_uint *p, |
| 172 | size_t p_limbs); |
Gabor Mezei | f049dbf | 2022-07-18 23:02:33 +0200 | [diff] [blame] | 173 | |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 174 | /** Unbind elements of a residue structure. |
| 175 | * |
Janos Follath | dae1147 | 2022-08-08 11:50:02 +0100 | [diff] [blame] | 176 | * This function removes the reference to the limb array that was passed to |
| 177 | * mbedtls_mpi_mod_residue_setup() to make it safe to free or use again. |
| 178 | * |
| 179 | * This function invalidates \p r and it must not be used until after |
| 180 | * mbedtls_mpi_mod_residue_setup() is called on it again. |
| 181 | * |
Janos Follath | 6b8a4ad | 2022-08-19 10:58:34 +0100 | [diff] [blame] | 182 | * \param[out] r The address of residue to release. |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 183 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 184 | void mbedtls_mpi_mod_residue_release(mbedtls_mpi_mod_residue *r); |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 185 | |
| 186 | /** Initialize a modulus structure. |
| 187 | * |
Mihir Raj Singh | b6fa940 | 2023-01-11 19:55:14 +0530 | [diff] [blame] | 188 | * \param[out] N The address of the modulus structure to initialize. |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 189 | */ |
Mihir Raj Singh | 432cacf | 2023-01-11 21:12:46 +0530 | [diff] [blame] | 190 | void mbedtls_mpi_mod_modulus_init(mbedtls_mpi_mod_modulus *N); |
Gabor Mezei | f049dbf | 2022-07-18 23:02:33 +0200 | [diff] [blame] | 191 | |
Janos Follath | 6318468 | 2022-08-11 17:42:59 +0100 | [diff] [blame] | 192 | /** Setup a modulus structure. |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 193 | * |
Mihir Raj Singh | b6fa940 | 2023-01-11 19:55:14 +0530 | [diff] [blame] | 194 | * \param[out] N The address of the modulus structure to populate. |
| 195 | * \param[in] p The address of the limb array storing the value of \p N. |
| 196 | * The memory pointed to by \p p will be used by \p N and must |
Janos Follath | 6b8a4ad | 2022-08-19 10:58:34 +0100 | [diff] [blame] | 197 | * not be modified in any way until after |
Janos Follath | dae1147 | 2022-08-08 11:50:02 +0100 | [diff] [blame] | 198 | * mbedtls_mpi_mod_modulus_free() is called. |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 199 | * \param p_limbs The number of limbs of \p p. |
Janos Follath | dae1147 | 2022-08-08 11:50:02 +0100 | [diff] [blame] | 200 | * \param int_rep The internal representation to be used for residues |
Mihir Raj Singh | b6fa940 | 2023-01-11 19:55:14 +0530 | [diff] [blame] | 201 | * associated with \p N (see #mbedtls_mpi_mod_rep_selector). |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 202 | * |
| 203 | * \return \c 0 if successful. |
Janos Follath | ee530cc | 2022-11-25 15:54:40 +0000 | [diff] [blame] | 204 | * \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] | 205 | */ |
Mihir Raj Singh | 432cacf | 2023-01-11 21:12:46 +0530 | [diff] [blame] | 206 | int mbedtls_mpi_mod_modulus_setup(mbedtls_mpi_mod_modulus *N, |
| 207 | const mbedtls_mpi_uint *p, |
| 208 | size_t p_limbs, |
| 209 | mbedtls_mpi_mod_rep_selector int_rep); |
Gabor Mezei | f049dbf | 2022-07-18 23:02:33 +0200 | [diff] [blame] | 210 | |
Minos Galanakis | bbe9db4 | 2023-05-09 10:37:21 +0100 | [diff] [blame] | 211 | /** Setup an optimised-reduction compatible modulus structure. |
| 212 | * |
| 213 | * \param[out] N The address of the modulus structure to populate. |
| 214 | * \param[in] p The address of the limb array storing the value of \p N. |
| 215 | * The memory pointed to by \p p will be used by \p N and must |
| 216 | * not be modified in any way until after |
| 217 | * mbedtls_mpi_mod_modulus_free() is called. |
| 218 | * \param p_limbs The number of limbs of \p p. |
| 219 | * \param ored The optimized reduction structure to use. \p p. |
| 220 | * |
| 221 | * \return \c 0 if successful. |
| 222 | */ |
| 223 | int mbedtls_mpi_mod_optred_modulus_setup(mbedtls_mpi_mod_modulus *N, |
| 224 | const mbedtls_mpi_uint *p, |
| 225 | size_t p_limbs, |
| 226 | mbedtls_mpi_opt_red_struct *ored); |
| 227 | |
Janos Follath | dae1147 | 2022-08-08 11:50:02 +0100 | [diff] [blame] | 228 | /** Free elements of a modulus structure. |
| 229 | * |
| 230 | * This function frees any memory allocated by mbedtls_mpi_mod_modulus_setup(). |
| 231 | * |
| 232 | * \warning This function does not free the limb array passed to |
| 233 | * mbedtls_mpi_mod_modulus_setup() only removes the reference to it, |
| 234 | * making it safe to free or to use it again. |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 235 | * |
Mihir Raj Singh | 928a07b | 2023-01-11 20:08:34 +0530 | [diff] [blame] | 236 | * \param[in,out] N The address of the modulus structure to free. |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 237 | */ |
Mihir Raj Singh | 432cacf | 2023-01-11 21:12:46 +0530 | [diff] [blame] | 238 | void mbedtls_mpi_mod_modulus_free(mbedtls_mpi_mod_modulus *N); |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 239 | |
Gabor Mezei | 9db81e9 | 2022-12-13 10:51:37 +0100 | [diff] [blame] | 240 | /** \brief Multiply two residues, returning the residue modulo the specified |
| 241 | * modulus. |
| 242 | * |
Gabor Mezei | 6a31b72 | 2022-12-16 15:24:03 +0100 | [diff] [blame] | 243 | * \note Currently handles the case when `N->int_rep` is |
Gabor Mezei | 9db81e9 | 2022-12-13 10:51:37 +0100 | [diff] [blame] | 244 | * MBEDTLS_MPI_MOD_REP_MONTGOMERY. |
| 245 | * |
Gabor Mezei | 6a31b72 | 2022-12-16 15:24:03 +0100 | [diff] [blame] | 246 | * The size of the operation is determined by \p N. \p A, \p B and \p X must |
| 247 | * all be associated with the modulus \p N and must all have the same number |
| 248 | * of limbs as \p N. |
Gabor Mezei | 9db81e9 | 2022-12-13 10:51:37 +0100 | [diff] [blame] | 249 | * |
| 250 | * \p X may be aliased to \p A or \p B, or even both, but may not overlap |
| 251 | * either otherwise. They may not alias \p N (since they must be in canonical |
| 252 | * form, they cannot == \p N). |
| 253 | * |
Gabor Mezei | 6a31b72 | 2022-12-16 15:24:03 +0100 | [diff] [blame] | 254 | * \param[out] X The address of the result MPI. Must have the same |
| 255 | * number of limbs as \p N. |
Gabor Mezei | 9db81e9 | 2022-12-13 10:51:37 +0100 | [diff] [blame] | 256 | * On successful completion, \p X contains the result of |
| 257 | * the multiplication `A * B * R^-1` mod N where |
Gabor Mezei | 6a31b72 | 2022-12-16 15:24:03 +0100 | [diff] [blame] | 258 | * `R = 2^(biL * N->limbs)`. |
| 259 | * \param[in] A The address of the first MPI. |
| 260 | * \param[in] B The address of the second MPI. |
Gabor Mezei | 9db81e9 | 2022-12-13 10:51:37 +0100 | [diff] [blame] | 261 | * \param[in] N The address of the modulus. Used to perform a modulo |
| 262 | * operation on the result of the multiplication. |
| 263 | * |
| 264 | * \return \c 0 if successful. |
Gabor Mezei | 6a31b72 | 2022-12-16 15:24:03 +0100 | [diff] [blame] | 265 | * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if all the parameters do not |
Gabor Mezei | 9db81e9 | 2022-12-13 10:51:37 +0100 | [diff] [blame] | 266 | * have the same number of limbs or \p N is invalid. |
| 267 | * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory-allocation failure. |
| 268 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 269 | int mbedtls_mpi_mod_mul(mbedtls_mpi_mod_residue *X, |
| 270 | const mbedtls_mpi_mod_residue *A, |
| 271 | const mbedtls_mpi_mod_residue *B, |
| 272 | const mbedtls_mpi_mod_modulus *N); |
Gabor Mezei | 9db81e9 | 2022-12-13 10:51:37 +0100 | [diff] [blame] | 273 | |
Tom Cosgrove | 62b2048 | 2022-12-01 14:27:37 +0000 | [diff] [blame] | 274 | /** |
| 275 | * \brief Perform a fixed-size modular subtraction. |
| 276 | * |
| 277 | * Calculate `A - B modulo N`. |
| 278 | * |
| 279 | * \p A, \p B and \p X must all have the same number of limbs as \p N. |
| 280 | * |
| 281 | * \p X may be aliased to \p A or \p B, or even both, but may not overlap |
| 282 | * either otherwise. |
| 283 | * |
| 284 | * \note This function does not check that \p A or \p B are in canonical |
| 285 | * form (that is, are < \p N) - that will have been done by |
| 286 | * mbedtls_mpi_mod_residue_setup(). |
| 287 | * |
| 288 | * \param[out] X The address of the result MPI. Must be initialized. |
| 289 | * Must have the same number of limbs as the modulus \p N. |
| 290 | * \param[in] A The address of the first MPI. |
| 291 | * \param[in] B The address of the second MPI. |
| 292 | * \param[in] N The address of the modulus. Used to perform a modulo |
| 293 | * operation on the result of the subtraction. |
| 294 | * |
| 295 | * \return \c 0 if successful. |
| 296 | * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if the given MPIs do not |
| 297 | * have the correct number of limbs. |
| 298 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 299 | int mbedtls_mpi_mod_sub(mbedtls_mpi_mod_residue *X, |
| 300 | const mbedtls_mpi_mod_residue *A, |
| 301 | const mbedtls_mpi_mod_residue *B, |
| 302 | const mbedtls_mpi_mod_modulus *N); |
Tom Cosgrove | 4302d02 | 2022-12-13 10:46:39 +0000 | [diff] [blame] | 303 | |
| 304 | /** |
| 305 | * \brief Perform modular inversion of an MPI with respect to a modulus \p N. |
| 306 | * |
Tom Cosgrove | d692ba4 | 2022-12-14 09:53:45 +0000 | [diff] [blame] | 307 | * \p A and \p X must be associated with the modulus \p N and will therefore |
| 308 | * have the same number of limbs as \p N. |
| 309 | * |
Tom Cosgrove | 4302d02 | 2022-12-13 10:46:39 +0000 | [diff] [blame] | 310 | * \p X may be aliased to \p A. |
| 311 | * |
| 312 | * \warning Currently only supports prime moduli, but does not check for them. |
| 313 | * |
| 314 | * \param[out] X The modular inverse of \p A with respect to \p N. |
| 315 | * \param[in] A The number to calculate the modular inverse of. |
| 316 | * Must not be 0. |
| 317 | * \param[in] N The modulus to use. |
| 318 | * |
| 319 | * \return \c 0 if successful. |
| 320 | * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if \p A and \p N do not |
| 321 | * have the same number of limbs. |
| 322 | * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if \p A is zero. |
| 323 | * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if couldn't allocate enough |
| 324 | * memory (needed for conversion to and from Mongtomery form |
| 325 | * when not in Montgomery form already, and for temporary use |
| 326 | * by the inversion calculation itself). |
| 327 | */ |
| 328 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 329 | int mbedtls_mpi_mod_inv(mbedtls_mpi_mod_residue *X, |
| 330 | const mbedtls_mpi_mod_residue *A, |
| 331 | const mbedtls_mpi_mod_modulus *N); |
Werner Lewis | e1b6b7c | 2022-11-29 12:25:05 +0000 | [diff] [blame] | 332 | /** |
| 333 | * \brief Perform a fixed-size modular addition. |
| 334 | * |
| 335 | * Calculate `A + B modulo N`. |
| 336 | * |
Werner Lewis | eed01aa | 2022-12-13 17:18:17 +0000 | [diff] [blame] | 337 | * \p A, \p B and \p X must all be associated with the modulus \p N and must |
| 338 | * all have the same number of limbs as \p N. |
Werner Lewis | e1b6b7c | 2022-11-29 12:25:05 +0000 | [diff] [blame] | 339 | * |
| 340 | * \p X may be aliased to \p A or \p B, or even both, but may not overlap |
| 341 | * either otherwise. |
| 342 | * |
| 343 | * \note This function does not check that \p A or \p B are in canonical |
| 344 | * form (that is, are < \p N) - that will have been done by |
| 345 | * mbedtls_mpi_mod_residue_setup(). |
| 346 | * |
Werner Lewis | eed01aa | 2022-12-13 17:18:17 +0000 | [diff] [blame] | 347 | * \param[out] X The address of the result residue. Must be initialized. |
Werner Lewis | e1b6b7c | 2022-11-29 12:25:05 +0000 | [diff] [blame] | 348 | * Must have the same number of limbs as the modulus \p N. |
Werner Lewis | eed01aa | 2022-12-13 17:18:17 +0000 | [diff] [blame] | 349 | * \param[in] A The address of the first input residue. |
| 350 | * \param[in] B The address of the second input residue. |
Werner Lewis | e1b6b7c | 2022-11-29 12:25:05 +0000 | [diff] [blame] | 351 | * \param[in] N The address of the modulus. Used to perform a modulo |
| 352 | * operation on the result of the addition. |
| 353 | * |
| 354 | * \return \c 0 if successful. |
| 355 | * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if the given MPIs do not |
| 356 | * have the correct number of limbs. |
| 357 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 358 | int mbedtls_mpi_mod_add(mbedtls_mpi_mod_residue *X, |
| 359 | const mbedtls_mpi_mod_residue *A, |
| 360 | const mbedtls_mpi_mod_residue *B, |
| 361 | const mbedtls_mpi_mod_modulus *N); |
Janos Follath | 5933f69 | 2022-11-02 14:35:17 +0000 | [diff] [blame] | 362 | |
Gilles Peskine | b1eea02 | 2022-12-07 22:59:27 +0100 | [diff] [blame] | 363 | /** Generate a random number uniformly in a range. |
| 364 | * |
| 365 | * This function generates a random number between \p min inclusive and |
| 366 | * \p N exclusive. |
| 367 | * |
| 368 | * The procedure complies with RFC 6979 ยง3.3 (deterministic ECDSA) |
| 369 | * when the RNG is a suitably parametrized instance of HMAC_DRBG |
| 370 | * and \p min is \c 1. |
| 371 | * |
| 372 | * \note There are `N - min` possible outputs. The lower bound |
| 373 | * \p min can be reached, but the upper bound \p N cannot. |
| 374 | * |
| 375 | * \param X The destination residue. |
| 376 | * \param min The minimum value to return. It must be strictly smaller |
| 377 | * than \b N. |
| 378 | * \param N The modulus. |
| 379 | * This is the upper bound of the output range, exclusive. |
| 380 | * \param f_rng The RNG function to use. This must not be \c NULL. |
| 381 | * \param p_rng The RNG parameter to be passed to \p f_rng. |
| 382 | * |
| 383 | * \return \c 0 if successful. |
| 384 | * \return #MBEDTLS_ERR_MPI_NOT_ACCEPTABLE if the implementation was |
| 385 | * unable to find a suitable value within a limited number |
| 386 | * of attempts. This has a negligible probability if \p N |
| 387 | * is significantly larger than \p min, which is the case |
| 388 | * for all usual cryptographic applications. |
| 389 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 390 | int mbedtls_mpi_mod_random(mbedtls_mpi_mod_residue *X, |
| 391 | mbedtls_mpi_uint min, |
| 392 | const mbedtls_mpi_mod_modulus *N, |
| 393 | int (*f_rng)(void *, unsigned char *, size_t), |
| 394 | void *p_rng); |
Gilles Peskine | b1eea02 | 2022-12-07 22:59:27 +0100 | [diff] [blame] | 395 | |
Janos Follath | 41427de | 2022-11-24 19:04:54 +0000 | [diff] [blame] | 396 | /** Read a residue from a byte buffer. |
Minos Galanakis | 81f4b11 | 2022-11-10 14:40:38 +0000 | [diff] [blame] | 397 | * |
Janos Follath | 41427de | 2022-11-24 19:04:54 +0000 | [diff] [blame] | 398 | * The residue will be automatically converted to the internal representation |
Mihir Raj Singh | fdc314b | 2023-01-11 20:32:59 +0530 | [diff] [blame] | 399 | * based on the value of the `N->int_rep` field. |
Minos Galanakis | 81f4b11 | 2022-11-10 14:40:38 +0000 | [diff] [blame] | 400 | * |
Mihir Raj Singh | fdc314b | 2023-01-11 20:32:59 +0530 | [diff] [blame] | 401 | * The modulus \p N will be the modulus associated with \p r. The residue \p r |
| 402 | * should only be used in operations where the modulus is \p N or a modulus |
| 403 | * equivalent to \p N (in the sense that all their fields or memory pointed by |
Janos Follath | 41427de | 2022-11-24 19:04:54 +0000 | [diff] [blame] | 404 | * their fields hold the same value). |
| 405 | * |
Janos Follath | 1f8afa2 | 2022-11-28 14:32:33 +0000 | [diff] [blame] | 406 | * \param[out] r The address of the residue. It must have exactly the same |
Mihir Raj Singh | fdc314b | 2023-01-11 20:32:59 +0530 | [diff] [blame] | 407 | * number of limbs as the modulus \p N. |
| 408 | * \param[in] N The address of the modulus. |
Janos Follath | 1f8afa2 | 2022-11-28 14:32:33 +0000 | [diff] [blame] | 409 | * \param[in] buf The input buffer to import from. |
Janos Follath | 3e3fc91 | 2022-11-24 18:02:46 +0000 | [diff] [blame] | 410 | * \param buflen The length in bytes of \p buf. |
| 411 | * \param ext_rep The endianness of the number in the input buffer. |
Minos Galanakis | 81f4b11 | 2022-11-10 14:40:38 +0000 | [diff] [blame] | 412 | * |
| 413 | * \return \c 0 if successful. |
Janos Follath | 41427de | 2022-11-24 19:04:54 +0000 | [diff] [blame] | 414 | * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p r isn't |
Minos Galanakis | 81f4b11 | 2022-11-10 14:40:38 +0000 | [diff] [blame] | 415 | * large enough to hold the value in \p buf. |
Janos Follath | 41427de | 2022-11-24 19:04:54 +0000 | [diff] [blame] | 416 | * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if \p ext_rep |
Mihir Raj Singh | fdc314b | 2023-01-11 20:32:59 +0530 | [diff] [blame] | 417 | * is invalid or the value in the buffer is not less than \p N. |
Minos Galanakis | 81f4b11 | 2022-11-10 14:40:38 +0000 | [diff] [blame] | 418 | */ |
Mihir Raj Singh | 432cacf | 2023-01-11 21:12:46 +0530 | [diff] [blame] | 419 | int mbedtls_mpi_mod_read(mbedtls_mpi_mod_residue *r, |
| 420 | const mbedtls_mpi_mod_modulus *N, |
| 421 | const unsigned char *buf, |
| 422 | size_t buflen, |
| 423 | mbedtls_mpi_mod_ext_rep ext_rep); |
Janos Follath | 5933f69 | 2022-11-02 14:35:17 +0000 | [diff] [blame] | 424 | |
Janos Follath | 41427de | 2022-11-24 19:04:54 +0000 | [diff] [blame] | 425 | /** Write a residue into a byte buffer. |
Minos Galanakis | 81f4b11 | 2022-11-10 14:40:38 +0000 | [diff] [blame] | 426 | * |
Mihir Raj Singh | a43290d | 2023-01-11 20:46:18 +0530 | [diff] [blame] | 427 | * The modulus \p N must be the modulus associated with \p r (see |
Janos Follath | 41427de | 2022-11-24 19:04:54 +0000 | [diff] [blame] | 428 | * mbedtls_mpi_mod_residue_setup() and mbedtls_mpi_mod_read()). |
Minos Galanakis | 81f4b11 | 2022-11-10 14:40:38 +0000 | [diff] [blame] | 429 | * |
Janos Follath | 41427de | 2022-11-24 19:04:54 +0000 | [diff] [blame] | 430 | * The residue will be automatically converted from the internal representation |
Mihir Raj Singh | a43290d | 2023-01-11 20:46:18 +0530 | [diff] [blame] | 431 | * based on the value of `N->int_rep` field. |
Janos Follath | 41427de | 2022-11-24 19:04:54 +0000 | [diff] [blame] | 432 | * |
Mihir Raj Singh | a43290d | 2023-01-11 20:46:18 +0530 | [diff] [blame] | 433 | * \warning If the buffer is smaller than `N->bits`, the number of |
Janos Follath | 6eb92c0 | 2022-11-26 17:34:37 +0000 | [diff] [blame] | 434 | * leading zeroes is leaked through timing. If \p r is |
Janos Follath | 41427de | 2022-11-24 19:04:54 +0000 | [diff] [blame] | 435 | * secret, the caller must ensure that \p buflen is at least |
Mihir Raj Singh | a43290d | 2023-01-11 20:46:18 +0530 | [diff] [blame] | 436 | * (`N->bits`+7)/8. |
Janos Follath | 41427de | 2022-11-24 19:04:54 +0000 | [diff] [blame] | 437 | * |
Janos Follath | 1f8afa2 | 2022-11-28 14:32:33 +0000 | [diff] [blame] | 438 | * \param[in] r The address of the residue. It must have the same number of |
Mihir Raj Singh | a43290d | 2023-01-11 20:46:18 +0530 | [diff] [blame] | 439 | * limbs as the modulus \p N. (\p r is an input parameter, but |
Janos Follath | 1f8afa2 | 2022-11-28 14:32:33 +0000 | [diff] [blame] | 440 | * its value will be modified during execution and restored |
| 441 | * before the function returns.) |
Tom Cosgrove | 8a1f784 | 2023-02-01 08:43:54 +0000 | [diff] [blame] | 442 | * \param[in] N The address of the modulus associated with \p r. |
Janos Follath | 1f8afa2 | 2022-11-28 14:32:33 +0000 | [diff] [blame] | 443 | * \param[out] buf The output buffer to export to. |
Janos Follath | 3e3fc91 | 2022-11-24 18:02:46 +0000 | [diff] [blame] | 444 | * \param buflen The length in bytes of \p buf. |
Janos Follath | 41427de | 2022-11-24 19:04:54 +0000 | [diff] [blame] | 445 | * \param ext_rep The endianness in which the number should be written into |
| 446 | * the output buffer. |
Minos Galanakis | 81f4b11 | 2022-11-10 14:40:38 +0000 | [diff] [blame] | 447 | * |
| 448 | * \return \c 0 if successful. |
| 449 | * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p buf isn't |
Janos Follath | 41427de | 2022-11-24 19:04:54 +0000 | [diff] [blame] | 450 | * large enough to hold the value of \p r (without leading |
| 451 | * zeroes). |
Janos Follath | fc6fbb4 | 2022-11-25 15:43:17 +0000 | [diff] [blame] | 452 | * \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] | 453 | * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if couldn't allocate enough |
| 454 | * memory for conversion. Can occur only for moduli with |
| 455 | * MBEDTLS_MPI_MOD_REP_MONTGOMERY. |
Minos Galanakis | 81f4b11 | 2022-11-10 14:40:38 +0000 | [diff] [blame] | 456 | */ |
Mihir Raj Singh | 432cacf | 2023-01-11 21:12:46 +0530 | [diff] [blame] | 457 | int mbedtls_mpi_mod_write(const mbedtls_mpi_mod_residue *r, |
| 458 | const mbedtls_mpi_mod_modulus *N, |
| 459 | unsigned char *buf, |
| 460 | size_t buflen, |
| 461 | mbedtls_mpi_mod_ext_rep ext_rep); |
Janos Follath | 5933f69 | 2022-11-02 14:35:17 +0000 | [diff] [blame] | 462 | |
Gabor Mezei | f049dbf | 2022-07-18 23:02:33 +0200 | [diff] [blame] | 463 | #endif /* MBEDTLS_BIGNUM_MOD_H */ |