blob: ab00c930337b397724580c5b97d69fc0b7ad9616 [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
10 * of type #mbedtls_mpi_mod_modulus. The structure must be setup with an
11 * array of limbs storing the bignum value of the modulus. Unless otherwise
12 * specified, the modulus is called \p N and is input-only.
13 * - **Bignum parameters**: Bignums are passed as pointers to an array of
14 * limbs or to a #mbedtls_mpi_mod_residue structure. A limb has the type
15 * #mbedtls_mpi_uint. Residues must be initialized before use, and must be
16 * associated with the modulus \p N. Unless otherwise specified:
17 * - Bignum parameters called \p A, \p B, ... are inputs and are not
18 * modified by the function. These will have the type
19 * #mbedtls_mpi_mod_residue.
20 * - Bignum parameters called \p X, \p Y, ... are outputs or input-output.
21 * The initial content of output-only parameters is ignored. These will
22 * have the type #mbedtls_mpi_mod_residue.
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 Peskine7f887bd2022-09-27 13:12:30 +020060 */
61
62/*
Gabor Mezeif049dbf2022-07-18 23:02:33 +020063 * 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 Follath296ea662022-08-11 14:58:29 +010088/* Skip 1 as it is slightly easier to accidentally pass to functions. */
89typedef 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. */
98typedef 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 Mezeif049dbf2022-07-18 23:02:33 +0200104
105typedef struct
106{
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200107 mbedtls_mpi_uint *p;
Gabor Mezeifd65e822022-08-12 18:09:12 +0200108 size_t limbs;
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200109} mbedtls_mpi_mod_residue;
110
Hanno Beckercd860df2022-08-18 16:23:05 +0100111typedef 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 Mezei89e31462022-08-12 15:36:56 +0200116typedef void *mbedtls_mpi_opt_red_struct;
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200117
118typedef struct {
Janos Follathed5c8d32022-08-15 11:50:22 +0100119 const mbedtls_mpi_uint *p;
Gabor Mezeifd65e822022-08-12 18:09:12 +0200120 size_t limbs; // number of limbs
121 size_t bits; // bitlen of p
Janos Follath296ea662022-08-11 14:58:29 +0100122 mbedtls_mpi_mod_rep_selector int_rep; // selector to signal the active member of the union
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200123 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 Mezei37b06362022-08-02 17:22:18 +0200130/** Setup a residue structure.
131 *
Janos Follathfc6fbb42022-11-25 15:43:17 +0000132 * The residue will be set up with the buffer \p p and modulus \p m.
Janos Follath41427de2022-11-24 19:04:54 +0000133 *
Janos Follathfc6fbb42022-11-25 15:43:17 +0000134 * 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 Follath41427de2022-11-24 19:04:54 +0000137 * 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 Follath6eb92c02022-11-26 17:34:37 +0000140 * should only be used in operations where the modulus is \p m.
Janos Follath41427de2022-11-24 19:04:54 +0000141 *
Janos Follathee530cc2022-11-25 15:54:40 +0000142 * \param[out] r The address of the residue to setup.
Janos Follath6b8a4ad2022-08-19 10:58:34 +0100143 * \param[in] m The address of the modulus related to \p r.
Janos Follathfc6fbb42022-11-25 15:43:17 +0000144 * \param[in] p The address of the limb array containing the value of \p r.
Janos Follath6b8a4ad2022-08-19 10:58:34 +0100145 * 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 Galanakisaed832a2022-11-24 09:09:47 +0000147 * mbedtls_mpi_mod_residue_release() is called. The data
Janos Follathfc6fbb42022-11-25 15:43:17 +0000148 * 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 Follath41427de2022-11-24 19:04:54 +0000150 * indicated by `m->int_rep`.
Janos Follathee530cc2022-11-25 15:54:40 +0000151 * \param p_limbs The number of limbs of \p p. Must be the same as the number
Janos Follath6eb92c02022-11-26 17:34:37 +0000152 * of limbs in the modulus \p m.
Gabor Mezei37b06362022-08-02 17:22:18 +0200153 *
154 * \return \c 0 if successful.
Janos Follathb7a88ec2022-08-19 12:24:40 +0100155 * \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 Mezei37b06362022-08-02 17:22:18 +0200157 */
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200158int mbedtls_mpi_mod_residue_setup( mbedtls_mpi_mod_residue *r,
Janos Follath6b8a4ad2022-08-19 10:58:34 +0100159 const mbedtls_mpi_mod_modulus *m,
Janos Follath8b718b52022-07-25 11:31:02 +0100160 mbedtls_mpi_uint *p,
Janos Follathb7a88ec2022-08-19 12:24:40 +0100161 size_t p_limbs );
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200162
Gabor Mezei37b06362022-08-02 17:22:18 +0200163/** Unbind elements of a residue structure.
164 *
Janos Follathdae11472022-08-08 11:50:02 +0100165 * 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 Follath6b8a4ad2022-08-19 10:58:34 +0100171 * \param[out] r The address of residue to release.
Gabor Mezei37b06362022-08-02 17:22:18 +0200172 */
173void mbedtls_mpi_mod_residue_release( mbedtls_mpi_mod_residue *r );
174
175/** Initialize a modulus structure.
176 *
Janos Follatha95f2042022-08-19 12:09:17 +0100177 * \param[out] m The address of the modulus structure to initialize.
Gabor Mezei37b06362022-08-02 17:22:18 +0200178 */
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200179void mbedtls_mpi_mod_modulus_init( mbedtls_mpi_mod_modulus *m );
180
Janos Follath63184682022-08-11 17:42:59 +0100181/** Setup a modulus structure.
Gabor Mezei37b06362022-08-02 17:22:18 +0200182 *
Janos Follath6b8a4ad2022-08-19 10:58:34 +0100183 * \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 Follathdae11472022-08-08 11:50:02 +0100187 * mbedtls_mpi_mod_modulus_free() is called.
Janos Follathb7a88ec2022-08-19 12:24:40 +0100188 * \param p_limbs The number of limbs of \p p.
Janos Follathdae11472022-08-08 11:50:02 +0100189 * \param int_rep The internal representation to be used for residues
190 * associated with \p m (see #mbedtls_mpi_mod_rep_selector).
Gabor Mezei37b06362022-08-02 17:22:18 +0200191 *
192 * \return \c 0 if successful.
Janos Follathee530cc2022-11-25 15:54:40 +0000193 * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if \p int_rep is invalid.
Gabor Mezei37b06362022-08-02 17:22:18 +0200194 */
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200195int mbedtls_mpi_mod_modulus_setup( mbedtls_mpi_mod_modulus *m,
Janos Follathed5c8d32022-08-15 11:50:22 +0100196 const mbedtls_mpi_uint *p,
Janos Follathb7a88ec2022-08-19 12:24:40 +0100197 size_t p_limbs,
Janos Follath296ea662022-08-11 14:58:29 +0100198 mbedtls_mpi_mod_rep_selector int_rep );
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200199
Janos Follathdae11472022-08-08 11:50:02 +0100200/** 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 Mezei37b06362022-08-02 17:22:18 +0200207 *
Janos Follatha95f2042022-08-19 12:09:17 +0100208 * \param[in,out] m The address of the modulus structure to free.
Gabor Mezei37b06362022-08-02 17:22:18 +0200209 */
210void mbedtls_mpi_mod_modulus_free( mbedtls_mpi_mod_modulus *m );
211
Janos Follath5933f692022-11-02 14:35:17 +0000212/* 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 Cosgrove62b20482022-12-01 14:27:37 +0000221/**
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 */
246int 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 Follath5933f692022-11-02 14:35:17 +0000250/* 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 Follath41427de2022-11-24 19:04:54 +0000265/** Read a residue from a byte buffer.
Minos Galanakis81f4b112022-11-10 14:40:38 +0000266 *
Janos Follath41427de2022-11-24 19:04:54 +0000267 * The residue will be automatically converted to the internal representation
Janos Follathfc6fbb42022-11-25 15:43:17 +0000268 * based on the value of the `m->int_rep` field.
Minos Galanakis81f4b112022-11-10 14:40:38 +0000269 *
Janos Follath41427de2022-11-24 19:04:54 +0000270 * 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 Follath1f8afa22022-11-28 14:32:33 +0000275 * \param[out] r The address of the residue. It must have exactly the same
Janos Follathfc6fbb42022-11-25 15:43:17 +0000276 * number of limbs as the modulus \p m.
Janos Follath1f8afa22022-11-28 14:32:33 +0000277 * \param[in] m The address of the modulus.
278 * \param[in] buf The input buffer to import from.
Janos Follath3e3fc912022-11-24 18:02:46 +0000279 * \param buflen The length in bytes of \p buf.
280 * \param ext_rep The endianness of the number in the input buffer.
Minos Galanakis81f4b112022-11-10 14:40:38 +0000281 *
282 * \return \c 0 if successful.
Janos Follath41427de2022-11-24 19:04:54 +0000283 * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p r isn't
Minos Galanakis81f4b112022-11-10 14:40:38 +0000284 * large enough to hold the value in \p buf.
Janos Follath41427de2022-11-24 19:04:54 +0000285 * \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 Galanakis81f4b112022-11-10 14:40:38 +0000287 */
288int mbedtls_mpi_mod_read( mbedtls_mpi_mod_residue *r,
Minos Galanakis8b375452022-11-24 11:04:11 +0000289 const mbedtls_mpi_mod_modulus *m,
290 const unsigned char *buf,
Janos Follath3e3fc912022-11-24 18:02:46 +0000291 size_t buflen,
292 mbedtls_mpi_mod_ext_rep ext_rep );
Janos Follath5933f692022-11-02 14:35:17 +0000293
Janos Follath41427de2022-11-24 19:04:54 +0000294/** Write a residue into a byte buffer.
Minos Galanakis81f4b112022-11-10 14:40:38 +0000295 *
Janos Follath41427de2022-11-24 19:04:54 +0000296 * 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 Galanakis81f4b112022-11-10 14:40:38 +0000298 *
Janos Follath41427de2022-11-24 19:04:54 +0000299 * 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 Follath6eb92c02022-11-26 17:34:37 +0000303 * leading zeroes is leaked through timing. If \p r is
Janos Follath41427de2022-11-24 19:04:54 +0000304 * secret, the caller must ensure that \p buflen is at least
305 * (`m->bits`+7)/8.
306 *
Janos Follath1f8afa22022-11-28 14:32:33 +0000307 * \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 Follath3e3fc912022-11-24 18:02:46 +0000313 * \param buflen The length in bytes of \p buf.
Janos Follath41427de2022-11-24 19:04:54 +0000314 * \param ext_rep The endianness in which the number should be written into
315 * the output buffer.
Minos Galanakis81f4b112022-11-10 14:40:38 +0000316 *
317 * \return \c 0 if successful.
318 * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p buf isn't
Janos Follath41427de2022-11-24 19:04:54 +0000319 * large enough to hold the value of \p r (without leading
320 * zeroes).
Janos Follathfc6fbb42022-11-25 15:43:17 +0000321 * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if \p ext_rep is invalid.
Janos Follath1f8afa22022-11-28 14:32:33 +0000322 * \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 Galanakis81f4b112022-11-10 14:40:38 +0000325 */
Minos Galanakis8b375452022-11-24 11:04:11 +0000326int mbedtls_mpi_mod_write( const mbedtls_mpi_mod_residue *r,
327 const mbedtls_mpi_mod_modulus *m,
Minos Galanakis81f4b112022-11-10 14:40:38 +0000328 unsigned char *buf,
Janos Follath3e3fc912022-11-24 18:02:46 +0000329 size_t buflen,
330 mbedtls_mpi_mod_ext_rep ext_rep );
Janos Follath5933f692022-11-02 14:35:17 +0000331/* 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 Mezeif049dbf2022-07-18 23:02:33 +0200345#endif /* MBEDTLS_BIGNUM_MOD_H */