blob: ee5e0bfb5361191bdf8546761bd630ecce064436 [file] [log] [blame]
Gabor Mezeif049dbf2022-07-18 23:02:33 +02001/**
Janos Follath63184682022-08-11 17:42:59 +01002 * Modular bignum functions
Gilles Peskine7aab2fb2022-09-27 13:19:13 +02003 *
4 * This module implements operations on integers modulo some fixed modulus.
Werner Lewis5e9d2e92022-12-12 14:00:25 +00005 *
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 Lewise1eb75d2022-12-14 13:45:49 +000010 * 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 Lewis214ae642022-12-15 10:57:59 +000013 * named \c N and is usually input-only.
Werner Lewis5e9d2e92022-12-12 14:00:25 +000014 * - **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
Werner Lewis214ae642022-12-15 10:57:59 +000017 * associated with the modulus \c N. Unless otherwise specified:
18 * - Bignum parameters called \c A, \c B, ... are inputs and are not
Werner Lewiseac8be72022-12-14 13:49:12 +000019 * modified by the function.
Werner Lewis214ae642022-12-15 10:57:59 +000020 * - Bignum parameters called \c X, \c Y, ... are outputs or input-output.
Werner Lewis945a1652022-12-14 15:24:46 +000021 * The initial bignum value of output-only parameters is ignored, but
Werner Lewis214ae642022-12-15 10:57:59 +000022 * they must be set up and associated with the modulus \c N.
23 * - Bignum parameters called \c P are inputs used to set up a modulus or
Werner Lewis5e9d2e92022-12-12 14:00:25 +000024 * residue. These must be pointers to an array of limbs.
Werner Lewis214ae642022-12-15 10:57:59 +000025 * - \c T is a temporary storage area. The initial content of such a
Werner Lewis5e9d2e92022-12-12 14:00:25 +000026 * parameter is ignored and the final content is unspecified.
Werner Lewis214ae642022-12-15 10:57:59 +000027 * - Some functions use different names, such as \c R for the residue.
Werner Lewis5e9d2e92022-12-12 14:00:25 +000028 * - **Bignum sizes**: bignum sizes are always expressed in limbs. Both
Werner Lewis2e70b9a2022-12-14 15:48:31 +000029 * #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 Lewis5e9d2e92022-12-12 14:00:25 +000034 * - **Bignum representation**: the representation of inputs and outputs is
Werner Lewis214ae642022-12-15 10:57:59 +000035 * specified by the \c int_rep field of the modulus.
Werner Lewis5e9d2e92022-12-12 14:00:25 +000036 * - **Parameter ordering**: for bignum parameters, outputs come before inputs.
Werner Lewisa3068862022-12-14 15:57:12 +000037 * The modulus is passed after residues. Temporaries come last.
Werner Lewis5e9d2e92022-12-12 14:00:25 +000038 * - **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 Lewis2bd263d2022-12-14 15:32:31 +000045 * - **Error handling**: functions generally check compatibility of input
Werner Lewis5e9d2e92022-12-12 14:00:25 +000046 * sizes. Most functions will not check that input values are in canonical
Werner Lewis214ae642022-12-15 10:57:59 +000047 * form (i.e. that \c A < \c N), this is only checked during setup of a
Werner Lewis5e9d2e92022-12-12 14:00:25 +000048 * residue structure.
Werner Lewis1d89ebf2022-12-14 17:08:43 +000049 * - **Modular representatives**: all functions expect inputs to be in the
Werner Lewis214ae642022-12-15 10:57:59 +000050 * range [0, \c N - 1] and guarantee outputs in the range [0, \c N - 1].
Werner Lewis1d89ebf2022-12-14 17:08:43 +000051 * 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 Peskine7f887bd2022-09-27 13:12:30 +020058 */
59
60/*
Gabor Mezeif049dbf2022-07-18 23:02:33 +020061 * 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 Follath296ea662022-08-11 14:58:29 +010086/* Skip 1 as it is slightly easier to accidentally pass to functions. */
87typedef 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. */
96typedef 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 Mezeif049dbf2022-07-18 23:02:33 +0200102
103typedef struct
104{
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200105 mbedtls_mpi_uint *p;
Gabor Mezeifd65e822022-08-12 18:09:12 +0200106 size_t limbs;
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200107} mbedtls_mpi_mod_residue;
108
Hanno Beckercd860df2022-08-18 16:23:05 +0100109typedef 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 Mezei89e31462022-08-12 15:36:56 +0200114typedef void *mbedtls_mpi_opt_red_struct;
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200115
116typedef struct {
Janos Follathed5c8d32022-08-15 11:50:22 +0100117 const mbedtls_mpi_uint *p;
Gabor Mezeifd65e822022-08-12 18:09:12 +0200118 size_t limbs; // number of limbs
119 size_t bits; // bitlen of p
Janos Follath296ea662022-08-11 14:58:29 +0100120 mbedtls_mpi_mod_rep_selector int_rep; // selector to signal the active member of the union
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200121 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 Mezei37b06362022-08-02 17:22:18 +0200128/** Setup a residue structure.
129 *
Janos Follathfc6fbb42022-11-25 15:43:17 +0000130 * The residue will be set up with the buffer \p p and modulus \p m.
Janos Follath41427de2022-11-24 19:04:54 +0000131 *
Janos Follathfc6fbb42022-11-25 15:43:17 +0000132 * 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 Follath41427de2022-11-24 19:04:54 +0000135 * 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 Follath6eb92c02022-11-26 17:34:37 +0000138 * should only be used in operations where the modulus is \p m.
Janos Follath41427de2022-11-24 19:04:54 +0000139 *
Janos Follathee530cc2022-11-25 15:54:40 +0000140 * \param[out] r The address of the residue to setup.
Janos Follath6b8a4ad2022-08-19 10:58:34 +0100141 * \param[in] m The address of the modulus related to \p r.
Janos Follathfc6fbb42022-11-25 15:43:17 +0000142 * \param[in] p The address of the limb array containing the value of \p r.
Janos Follath6b8a4ad2022-08-19 10:58:34 +0100143 * 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 Galanakisaed832a2022-11-24 09:09:47 +0000145 * mbedtls_mpi_mod_residue_release() is called. The data
Janos Follathfc6fbb42022-11-25 15:43:17 +0000146 * 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 Follath41427de2022-11-24 19:04:54 +0000148 * indicated by `m->int_rep`.
Janos Follathee530cc2022-11-25 15:54:40 +0000149 * \param p_limbs The number of limbs of \p p. Must be the same as the number
Janos Follath6eb92c02022-11-26 17:34:37 +0000150 * of limbs in the modulus \p m.
Gabor Mezei37b06362022-08-02 17:22:18 +0200151 *
152 * \return \c 0 if successful.
Janos Follathb7a88ec2022-08-19 12:24:40 +0100153 * \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 Mezei37b06362022-08-02 17:22:18 +0200155 */
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200156int mbedtls_mpi_mod_residue_setup( mbedtls_mpi_mod_residue *r,
Janos Follath6b8a4ad2022-08-19 10:58:34 +0100157 const mbedtls_mpi_mod_modulus *m,
Janos Follath8b718b52022-07-25 11:31:02 +0100158 mbedtls_mpi_uint *p,
Janos Follathb7a88ec2022-08-19 12:24:40 +0100159 size_t p_limbs );
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200160
Gabor Mezei37b06362022-08-02 17:22:18 +0200161/** Unbind elements of a residue structure.
162 *
Janos Follathdae11472022-08-08 11:50:02 +0100163 * 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 Follath6b8a4ad2022-08-19 10:58:34 +0100169 * \param[out] r The address of residue to release.
Gabor Mezei37b06362022-08-02 17:22:18 +0200170 */
171void mbedtls_mpi_mod_residue_release( mbedtls_mpi_mod_residue *r );
172
173/** Initialize a modulus structure.
174 *
Janos Follatha95f2042022-08-19 12:09:17 +0100175 * \param[out] m The address of the modulus structure to initialize.
Gabor Mezei37b06362022-08-02 17:22:18 +0200176 */
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200177void mbedtls_mpi_mod_modulus_init( mbedtls_mpi_mod_modulus *m );
178
Janos Follath63184682022-08-11 17:42:59 +0100179/** Setup a modulus structure.
Gabor Mezei37b06362022-08-02 17:22:18 +0200180 *
Janos Follath6b8a4ad2022-08-19 10:58:34 +0100181 * \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 Follathdae11472022-08-08 11:50:02 +0100185 * mbedtls_mpi_mod_modulus_free() is called.
Janos Follathb7a88ec2022-08-19 12:24:40 +0100186 * \param p_limbs The number of limbs of \p p.
Janos Follathdae11472022-08-08 11:50:02 +0100187 * \param int_rep The internal representation to be used for residues
188 * associated with \p m (see #mbedtls_mpi_mod_rep_selector).
Gabor Mezei37b06362022-08-02 17:22:18 +0200189 *
190 * \return \c 0 if successful.
Janos Follathee530cc2022-11-25 15:54:40 +0000191 * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if \p int_rep is invalid.
Gabor Mezei37b06362022-08-02 17:22:18 +0200192 */
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200193int mbedtls_mpi_mod_modulus_setup( mbedtls_mpi_mod_modulus *m,
Janos Follathed5c8d32022-08-15 11:50:22 +0100194 const mbedtls_mpi_uint *p,
Janos Follathb7a88ec2022-08-19 12:24:40 +0100195 size_t p_limbs,
Janos Follath296ea662022-08-11 14:58:29 +0100196 mbedtls_mpi_mod_rep_selector int_rep );
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200197
Janos Follathdae11472022-08-08 11:50:02 +0100198/** 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 Mezei37b06362022-08-02 17:22:18 +0200205 *
Janos Follatha95f2042022-08-19 12:09:17 +0100206 * \param[in,out] m The address of the modulus structure to free.
Gabor Mezei37b06362022-08-02 17:22:18 +0200207 */
208void mbedtls_mpi_mod_modulus_free( mbedtls_mpi_mod_modulus *m );
209
Janos Follath5933f692022-11-02 14:35:17 +0000210/* 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 Cosgrove62b20482022-12-01 14:27:37 +0000219/**
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 */
244int 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 Follath5933f692022-11-02 14:35:17 +0000248/* 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 Follath41427de2022-11-24 19:04:54 +0000263/** Read a residue from a byte buffer.
Minos Galanakis81f4b112022-11-10 14:40:38 +0000264 *
Janos Follath41427de2022-11-24 19:04:54 +0000265 * The residue will be automatically converted to the internal representation
Janos Follathfc6fbb42022-11-25 15:43:17 +0000266 * based on the value of the `m->int_rep` field.
Minos Galanakis81f4b112022-11-10 14:40:38 +0000267 *
Janos Follath41427de2022-11-24 19:04:54 +0000268 * 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 Follath1f8afa22022-11-28 14:32:33 +0000273 * \param[out] r The address of the residue. It must have exactly the same
Janos Follathfc6fbb42022-11-25 15:43:17 +0000274 * number of limbs as the modulus \p m.
Janos Follath1f8afa22022-11-28 14:32:33 +0000275 * \param[in] m The address of the modulus.
276 * \param[in] buf The input buffer to import from.
Janos Follath3e3fc912022-11-24 18:02:46 +0000277 * \param buflen The length in bytes of \p buf.
278 * \param ext_rep The endianness of the number in the input buffer.
Minos Galanakis81f4b112022-11-10 14:40:38 +0000279 *
280 * \return \c 0 if successful.
Janos Follath41427de2022-11-24 19:04:54 +0000281 * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p r isn't
Minos Galanakis81f4b112022-11-10 14:40:38 +0000282 * large enough to hold the value in \p buf.
Janos Follath41427de2022-11-24 19:04:54 +0000283 * \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 Galanakis81f4b112022-11-10 14:40:38 +0000285 */
286int mbedtls_mpi_mod_read( mbedtls_mpi_mod_residue *r,
Minos Galanakis8b375452022-11-24 11:04:11 +0000287 const mbedtls_mpi_mod_modulus *m,
288 const unsigned char *buf,
Janos Follath3e3fc912022-11-24 18:02:46 +0000289 size_t buflen,
290 mbedtls_mpi_mod_ext_rep ext_rep );
Janos Follath5933f692022-11-02 14:35:17 +0000291
Janos Follath41427de2022-11-24 19:04:54 +0000292/** Write a residue into a byte buffer.
Minos Galanakis81f4b112022-11-10 14:40:38 +0000293 *
Janos Follath41427de2022-11-24 19:04:54 +0000294 * 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 Galanakis81f4b112022-11-10 14:40:38 +0000296 *
Janos Follath41427de2022-11-24 19:04:54 +0000297 * 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 Follath6eb92c02022-11-26 17:34:37 +0000301 * leading zeroes is leaked through timing. If \p r is
Janos Follath41427de2022-11-24 19:04:54 +0000302 * secret, the caller must ensure that \p buflen is at least
303 * (`m->bits`+7)/8.
304 *
Janos Follath1f8afa22022-11-28 14:32:33 +0000305 * \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 Follath3e3fc912022-11-24 18:02:46 +0000311 * \param buflen The length in bytes of \p buf.
Janos Follath41427de2022-11-24 19:04:54 +0000312 * \param ext_rep The endianness in which the number should be written into
313 * the output buffer.
Minos Galanakis81f4b112022-11-10 14:40:38 +0000314 *
315 * \return \c 0 if successful.
316 * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p buf isn't
Janos Follath41427de2022-11-24 19:04:54 +0000317 * large enough to hold the value of \p r (without leading
318 * zeroes).
Janos Follathfc6fbb42022-11-25 15:43:17 +0000319 * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if \p ext_rep is invalid.
Janos Follath1f8afa22022-11-28 14:32:33 +0000320 * \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 Galanakis81f4b112022-11-10 14:40:38 +0000323 */
Minos Galanakis8b375452022-11-24 11:04:11 +0000324int mbedtls_mpi_mod_write( const mbedtls_mpi_mod_residue *r,
325 const mbedtls_mpi_mod_modulus *m,
Minos Galanakis81f4b112022-11-10 14:40:38 +0000326 unsigned char *buf,
Janos Follath3e3fc912022-11-24 18:02:46 +0000327 size_t buflen,
328 mbedtls_mpi_mod_ext_rep ext_rep );
Janos Follath5933f692022-11-02 14:35:17 +0000329/* 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 Mezeif049dbf2022-07-18 23:02:33 +0200343#endif /* MBEDTLS_BIGNUM_MOD_H */