blob: f2fd7e2457673d01fa67ad344c25d478826d823f [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
13 * named \p 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
17 * associated with the modulus \p N. Unless otherwise specified:
18 * - Bignum parameters called \p A, \p B, ... are inputs and are not
19 * modified by the function. These will have the type
20 * #mbedtls_mpi_mod_residue.
21 * - Bignum parameters called \p X, \p Y, ... are outputs or input-output.
22 * The initial content of output-only parameters is ignored. These will
23 * have the type #mbedtls_mpi_mod_residue.
24 * - Bignum parameters called \p P are inputs used to setup a modulus or
25 * residue. These must be pointers to an array of limbs.
26 * - \p T is a temporary storage area. The initial content of such
27 * parameter is ignored and the final content is unspecified.
28 * - Some functions use different names, such as \p R for the residue.
29 * - **Bignum sizes**: bignum sizes are always expressed in limbs. Both
30 * #mbedtls_mpi_mod_modulus and #mbedtls_mpi_mod_residue have a \p limbs
31 * member storing its size. Functions which take a limb array parameter
32 * must also take an associated \p limbs parameter specifying its size.
33 * All bignum sizes must be at least 1 and be significantly less than
34 * #SIZE_MAX. The behavior if a size is 0 may be undefined or an error
35 * may be returned. All bignum parameters must have the same size unless
36 * otherwise specified.
37 * - **Bignum representation**: the representation of inputs and outputs is
38 * specified by the \p int_rep field of the modulus.
39 * - **Parameter ordering**: for bignum parameters, outputs come before inputs.
40 * Temporaries come last.
41 * - **Aliasing**: in general, output bignums may be aliased to one or more
42 * inputs. Modulus values may not be aliased to any other parameter. Outputs
43 * may not be aliased to one another. Temporaries may not be aliased to any
44 * other parameter.
45 * - **Overlap**: apart from aliasing of residue pointers (where two residue
46 * arguments are equal pointers), overlap is not supported and may result
47 * in undefined behavior.
48 * - **Error handling**: functions generally check compatability of input
49 * sizes. Most functions will not check that input values are in canonical
50 * form (i.e. that \p A < \p N), this is only checked during setup of a
51 * residue structure.
52 * - **Modular representatives**: functions that operate modulo \p N expect
53 * all modular inputs to be in the range [0, \p N - 1] and guarantee outputs
54 * in the range [0, \p N - 1]. Residues are setup with an associated modulus,
55 * and operations are only guaranteed to work if the modulus is associated
56 * with all residue parameters. If a residue is passed with a modulus other
57 * than the one it is associated with, then it may be out of range. If an
58 * input is out of range, outputs are fully unspecified, though bignum values
59 * out of range should not cause buffer overflows (beware that this is not
60 * extensively tested).
Gilles Peskine7f887bd2022-09-27 13:12:30 +020061 */
62
63/*
Gabor Mezeif049dbf2022-07-18 23:02:33 +020064 * Copyright The Mbed TLS Contributors
65 * SPDX-License-Identifier: Apache-2.0
66 *
67 * Licensed under the Apache License, Version 2.0 (the "License"); you may
68 * not use this file except in compliance with the License.
69 * You may obtain a copy of the License at
70 *
71 * http://www.apache.org/licenses/LICENSE-2.0
72 *
73 * Unless required by applicable law or agreed to in writing, software
74 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
75 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
76 * See the License for the specific language governing permissions and
77 * limitations under the License.
78 */
79
80#ifndef MBEDTLS_BIGNUM_MOD_H
81#define MBEDTLS_BIGNUM_MOD_H
82
83#include "common.h"
84
85#if defined(MBEDTLS_BIGNUM_C)
86#include "mbedtls/bignum.h"
87#endif
88
Janos Follath296ea662022-08-11 14:58:29 +010089/* Skip 1 as it is slightly easier to accidentally pass to functions. */
90typedef enum
91{
92 MBEDTLS_MPI_MOD_REP_INVALID = 0,
93 MBEDTLS_MPI_MOD_REP_MONTGOMERY = 2,
94 MBEDTLS_MPI_MOD_REP_OPT_RED
95} mbedtls_mpi_mod_rep_selector;
96
97/* Make mbedtls_mpi_mod_rep_selector and mbedtls_mpi_mod_ext_rep disjoint to
98 * make it easier to catch when they are accidentally swapped. */
99typedef enum
100{
101 MBEDTLS_MPI_MOD_EXT_REP_INVALID = 0,
102 MBEDTLS_MPI_MOD_EXT_REP_LE = 8,
103 MBEDTLS_MPI_MOD_EXT_REP_BE
104} mbedtls_mpi_mod_ext_rep;
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200105
106typedef struct
107{
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200108 mbedtls_mpi_uint *p;
Gabor Mezeifd65e822022-08-12 18:09:12 +0200109 size_t limbs;
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200110} mbedtls_mpi_mod_residue;
111
Hanno Beckercd860df2022-08-18 16:23:05 +0100112typedef struct {
113 mbedtls_mpi_uint const *rr; /* The residue for 2^{2*n*biL} mod N */
114 mbedtls_mpi_uint mm; /* Montgomery const for -N^{-1} mod 2^{ciL} */
115} mbedtls_mpi_mont_struct;
116
Gabor Mezei89e31462022-08-12 15:36:56 +0200117typedef void *mbedtls_mpi_opt_red_struct;
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200118
119typedef struct {
Janos Follathed5c8d32022-08-15 11:50:22 +0100120 const mbedtls_mpi_uint *p;
Gabor Mezeifd65e822022-08-12 18:09:12 +0200121 size_t limbs; // number of limbs
122 size_t bits; // bitlen of p
Janos Follath296ea662022-08-11 14:58:29 +0100123 mbedtls_mpi_mod_rep_selector int_rep; // selector to signal the active member of the union
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200124 union rep
125 {
126 mbedtls_mpi_mont_struct mont;
127 mbedtls_mpi_opt_red_struct ored;
128 } rep;
129} mbedtls_mpi_mod_modulus;
130
Gabor Mezei37b06362022-08-02 17:22:18 +0200131/** Setup a residue structure.
132 *
Janos Follathfc6fbb42022-11-25 15:43:17 +0000133 * The residue will be set up with the buffer \p p and modulus \p m.
Janos Follath41427de2022-11-24 19:04:54 +0000134 *
Janos Follathfc6fbb42022-11-25 15:43:17 +0000135 * The memory pointed to by \p p will be used by the resulting residue structure.
136 * The value at the pointed-to memory will be the initial value of \p r and must
137 * hold a value that is less than the modulus. This value will be used as-is
Janos Follath41427de2022-11-24 19:04:54 +0000138 * and interpreted according to the value of the `m->int_rep` field.
139 *
140 * The modulus \p m will be the modulus associated with \p r. The residue \p r
Janos Follath6eb92c02022-11-26 17:34:37 +0000141 * should only be used in operations where the modulus is \p m.
Janos Follath41427de2022-11-24 19:04:54 +0000142 *
Janos Follathee530cc2022-11-25 15:54:40 +0000143 * \param[out] r The address of the residue to setup.
Janos Follath6b8a4ad2022-08-19 10:58:34 +0100144 * \param[in] m The address of the modulus related to \p r.
Janos Follathfc6fbb42022-11-25 15:43:17 +0000145 * \param[in] p The address of the limb array containing the value of \p r.
Janos Follath6b8a4ad2022-08-19 10:58:34 +0100146 * The memory pointed to by \p p will be used by \p r and must
147 * not be modified in any way until after
Minos Galanakisaed832a2022-11-24 09:09:47 +0000148 * mbedtls_mpi_mod_residue_release() is called. The data
Janos Follathfc6fbb42022-11-25 15:43:17 +0000149 * pointed to by \p p must be less than the modulus (the value
150 * pointed to by `m->p`) and already in the representation
Janos Follath41427de2022-11-24 19:04:54 +0000151 * indicated by `m->int_rep`.
Janos Follathee530cc2022-11-25 15:54:40 +0000152 * \param p_limbs The number of limbs of \p p. Must be the same as the number
Janos Follath6eb92c02022-11-26 17:34:37 +0000153 * of limbs in the modulus \p m.
Gabor Mezei37b06362022-08-02 17:22:18 +0200154 *
155 * \return \c 0 if successful.
Janos Follathb7a88ec2022-08-19 12:24:40 +0100156 * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if \p p_limbs is less than the
157 * limbs in \p m or if \p p is not less than \p m.
Gabor Mezei37b06362022-08-02 17:22:18 +0200158 */
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200159int mbedtls_mpi_mod_residue_setup( mbedtls_mpi_mod_residue *r,
Janos Follath6b8a4ad2022-08-19 10:58:34 +0100160 const mbedtls_mpi_mod_modulus *m,
Janos Follath8b718b52022-07-25 11:31:02 +0100161 mbedtls_mpi_uint *p,
Janos Follathb7a88ec2022-08-19 12:24:40 +0100162 size_t p_limbs );
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200163
Gabor Mezei37b06362022-08-02 17:22:18 +0200164/** Unbind elements of a residue structure.
165 *
Janos Follathdae11472022-08-08 11:50:02 +0100166 * This function removes the reference to the limb array that was passed to
167 * mbedtls_mpi_mod_residue_setup() to make it safe to free or use again.
168 *
169 * This function invalidates \p r and it must not be used until after
170 * mbedtls_mpi_mod_residue_setup() is called on it again.
171 *
Janos Follath6b8a4ad2022-08-19 10:58:34 +0100172 * \param[out] r The address of residue to release.
Gabor Mezei37b06362022-08-02 17:22:18 +0200173 */
174void mbedtls_mpi_mod_residue_release( mbedtls_mpi_mod_residue *r );
175
176/** Initialize a modulus structure.
177 *
Janos Follatha95f2042022-08-19 12:09:17 +0100178 * \param[out] m The address of the modulus structure to initialize.
Gabor Mezei37b06362022-08-02 17:22:18 +0200179 */
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200180void mbedtls_mpi_mod_modulus_init( mbedtls_mpi_mod_modulus *m );
181
Janos Follath63184682022-08-11 17:42:59 +0100182/** Setup a modulus structure.
Gabor Mezei37b06362022-08-02 17:22:18 +0200183 *
Janos Follath6b8a4ad2022-08-19 10:58:34 +0100184 * \param[out] m The address of the modulus structure to populate.
185 * \param[in] p The address of the limb array storing the value of \p m.
186 * The memory pointed to by \p p will be used by \p m and must
187 * not be modified in any way until after
Janos Follathdae11472022-08-08 11:50:02 +0100188 * mbedtls_mpi_mod_modulus_free() is called.
Janos Follathb7a88ec2022-08-19 12:24:40 +0100189 * \param p_limbs The number of limbs of \p p.
Janos Follathdae11472022-08-08 11:50:02 +0100190 * \param int_rep The internal representation to be used for residues
191 * associated with \p m (see #mbedtls_mpi_mod_rep_selector).
Gabor Mezei37b06362022-08-02 17:22:18 +0200192 *
193 * \return \c 0 if successful.
Janos Follathee530cc2022-11-25 15:54:40 +0000194 * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if \p int_rep is invalid.
Gabor Mezei37b06362022-08-02 17:22:18 +0200195 */
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200196int mbedtls_mpi_mod_modulus_setup( mbedtls_mpi_mod_modulus *m,
Janos Follathed5c8d32022-08-15 11:50:22 +0100197 const mbedtls_mpi_uint *p,
Janos Follathb7a88ec2022-08-19 12:24:40 +0100198 size_t p_limbs,
Janos Follath296ea662022-08-11 14:58:29 +0100199 mbedtls_mpi_mod_rep_selector int_rep );
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200200
Janos Follathdae11472022-08-08 11:50:02 +0100201/** Free elements of a modulus structure.
202 *
203 * This function frees any memory allocated by mbedtls_mpi_mod_modulus_setup().
204 *
205 * \warning This function does not free the limb array passed to
206 * mbedtls_mpi_mod_modulus_setup() only removes the reference to it,
207 * making it safe to free or to use it again.
Gabor Mezei37b06362022-08-02 17:22:18 +0200208 *
Janos Follatha95f2042022-08-19 12:09:17 +0100209 * \param[in,out] m The address of the modulus structure to free.
Gabor Mezei37b06362022-08-02 17:22:18 +0200210 */
211void mbedtls_mpi_mod_modulus_free( mbedtls_mpi_mod_modulus *m );
212
Janos Follath5933f692022-11-02 14:35:17 +0000213/* BEGIN MERGE SLOT 1 */
214
215/* END MERGE SLOT 1 */
216
217/* BEGIN MERGE SLOT 2 */
218
219/* END MERGE SLOT 2 */
220
221/* BEGIN MERGE SLOT 3 */
Tom Cosgrove62b20482022-12-01 14:27:37 +0000222/**
223 * \brief Perform a fixed-size modular subtraction.
224 *
225 * Calculate `A - B modulo N`.
226 *
227 * \p A, \p B and \p X must all have the same number of limbs as \p N.
228 *
229 * \p X may be aliased to \p A or \p B, or even both, but may not overlap
230 * either otherwise.
231 *
232 * \note This function does not check that \p A or \p B are in canonical
233 * form (that is, are < \p N) - that will have been done by
234 * mbedtls_mpi_mod_residue_setup().
235 *
236 * \param[out] X The address of the result MPI. Must be initialized.
237 * Must have the same number of limbs as the modulus \p N.
238 * \param[in] A The address of the first MPI.
239 * \param[in] B The address of the second MPI.
240 * \param[in] N The address of the modulus. Used to perform a modulo
241 * operation on the result of the subtraction.
242 *
243 * \return \c 0 if successful.
244 * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if the given MPIs do not
245 * have the correct number of limbs.
246 */
247int mbedtls_mpi_mod_sub( mbedtls_mpi_mod_residue *X,
248 const mbedtls_mpi_mod_residue *A,
249 const mbedtls_mpi_mod_residue *B,
250 const mbedtls_mpi_mod_modulus *N );
Janos Follath5933f692022-11-02 14:35:17 +0000251/* END MERGE SLOT 3 */
252
253/* BEGIN MERGE SLOT 4 */
254
255/* END MERGE SLOT 4 */
256
257/* BEGIN MERGE SLOT 5 */
258
259/* END MERGE SLOT 5 */
260
261/* BEGIN MERGE SLOT 6 */
262
263/* END MERGE SLOT 6 */
264
265/* BEGIN MERGE SLOT 7 */
Janos Follath41427de2022-11-24 19:04:54 +0000266/** Read a residue from a byte buffer.
Minos Galanakis81f4b112022-11-10 14:40:38 +0000267 *
Janos Follath41427de2022-11-24 19:04:54 +0000268 * The residue will be automatically converted to the internal representation
Janos Follathfc6fbb42022-11-25 15:43:17 +0000269 * based on the value of the `m->int_rep` field.
Minos Galanakis81f4b112022-11-10 14:40:38 +0000270 *
Janos Follath41427de2022-11-24 19:04:54 +0000271 * The modulus \p m will be the modulus associated with \p r. The residue \p r
272 * should only be used in operations where the modulus is \p m or a modulus
273 * equivalent to \p m (in the sense that all their fields or memory pointed by
274 * their fields hold the same value).
275 *
Janos Follath1f8afa22022-11-28 14:32:33 +0000276 * \param[out] r The address of the residue. It must have exactly the same
Janos Follathfc6fbb42022-11-25 15:43:17 +0000277 * number of limbs as the modulus \p m.
Janos Follath1f8afa22022-11-28 14:32:33 +0000278 * \param[in] m The address of the modulus.
279 * \param[in] buf The input buffer to import from.
Janos Follath3e3fc912022-11-24 18:02:46 +0000280 * \param buflen The length in bytes of \p buf.
281 * \param ext_rep The endianness of the number in the input buffer.
Minos Galanakis81f4b112022-11-10 14:40:38 +0000282 *
283 * \return \c 0 if successful.
Janos Follath41427de2022-11-24 19:04:54 +0000284 * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p r isn't
Minos Galanakis81f4b112022-11-10 14:40:38 +0000285 * large enough to hold the value in \p buf.
Janos Follath41427de2022-11-24 19:04:54 +0000286 * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if \p ext_rep
287 * is invalid or the value in the buffer is not less than \p m.
Minos Galanakis81f4b112022-11-10 14:40:38 +0000288 */
289int mbedtls_mpi_mod_read( mbedtls_mpi_mod_residue *r,
Minos Galanakis8b375452022-11-24 11:04:11 +0000290 const mbedtls_mpi_mod_modulus *m,
291 const unsigned char *buf,
Janos Follath3e3fc912022-11-24 18:02:46 +0000292 size_t buflen,
293 mbedtls_mpi_mod_ext_rep ext_rep );
Janos Follath5933f692022-11-02 14:35:17 +0000294
Janos Follath41427de2022-11-24 19:04:54 +0000295/** Write a residue into a byte buffer.
Minos Galanakis81f4b112022-11-10 14:40:38 +0000296 *
Janos Follath41427de2022-11-24 19:04:54 +0000297 * The modulus \p m must be the modulus associated with \p r (see
298 * mbedtls_mpi_mod_residue_setup() and mbedtls_mpi_mod_read()).
Minos Galanakis81f4b112022-11-10 14:40:38 +0000299 *
Janos Follath41427de2022-11-24 19:04:54 +0000300 * The residue will be automatically converted from the internal representation
301 * based on the value of `m->int_rep` field.
302 *
303 * \warning If the buffer is smaller than `m->bits`, the number of
Janos Follath6eb92c02022-11-26 17:34:37 +0000304 * leading zeroes is leaked through timing. If \p r is
Janos Follath41427de2022-11-24 19:04:54 +0000305 * secret, the caller must ensure that \p buflen is at least
306 * (`m->bits`+7)/8.
307 *
Janos Follath1f8afa22022-11-28 14:32:33 +0000308 * \param[in] r The address of the residue. It must have the same number of
309 * limbs as the modulus \p m. (\p r is an input parameter, but
310 * its value will be modified during execution and restored
311 * before the function returns.)
312 * \param[in] m The address of the modulus associated with \r.
313 * \param[out] buf The output buffer to export to.
Janos Follath3e3fc912022-11-24 18:02:46 +0000314 * \param buflen The length in bytes of \p buf.
Janos Follath41427de2022-11-24 19:04:54 +0000315 * \param ext_rep The endianness in which the number should be written into
316 * the output buffer.
Minos Galanakis81f4b112022-11-10 14:40:38 +0000317 *
318 * \return \c 0 if successful.
319 * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p buf isn't
Janos Follath41427de2022-11-24 19:04:54 +0000320 * large enough to hold the value of \p r (without leading
321 * zeroes).
Janos Follathfc6fbb42022-11-25 15:43:17 +0000322 * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if \p ext_rep is invalid.
Janos Follath1f8afa22022-11-28 14:32:33 +0000323 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if couldn't allocate enough
324 * memory for conversion. Can occur only for moduli with
325 * MBEDTLS_MPI_MOD_REP_MONTGOMERY.
Minos Galanakis81f4b112022-11-10 14:40:38 +0000326 */
Minos Galanakis8b375452022-11-24 11:04:11 +0000327int mbedtls_mpi_mod_write( const mbedtls_mpi_mod_residue *r,
328 const mbedtls_mpi_mod_modulus *m,
Minos Galanakis81f4b112022-11-10 14:40:38 +0000329 unsigned char *buf,
Janos Follath3e3fc912022-11-24 18:02:46 +0000330 size_t buflen,
331 mbedtls_mpi_mod_ext_rep ext_rep );
Janos Follath5933f692022-11-02 14:35:17 +0000332/* END MERGE SLOT 7 */
333
334/* BEGIN MERGE SLOT 8 */
335
336/* END MERGE SLOT 8 */
337
338/* BEGIN MERGE SLOT 9 */
339
340/* END MERGE SLOT 9 */
341
342/* BEGIN MERGE SLOT 10 */
343
344/* END MERGE SLOT 10 */
345
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200346#endif /* MBEDTLS_BIGNUM_MOD_H */