blob: 1b1e920a941bcfa2b1fafcc0dd0510e41abeb92b [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 Lewis6bb49ba2022-12-15 16:58:44 +000013 * 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 Lewis5e9d2e92022-12-12 14:00:25 +000015 * - **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 Lewis214ae642022-12-15 10:57:59 +000018 * associated with the modulus \c N. Unless otherwise specified:
19 * - Bignum parameters called \c A, \c B, ... are inputs and are not
Werner Lewis6bb49ba2022-12-15 16:58:44 +000020 * modified by the function. Functions which take a parameter of
21 * type \c const #mbedtls_mpi_mod_residue* must not modify its value.
Werner Lewis214ae642022-12-15 10:57:59 +000022 * - Bignum parameters called \c X, \c Y, ... are outputs or input-output.
Werner Lewis945a1652022-12-14 15:24:46 +000023 * The initial bignum value of output-only parameters is ignored, but
Werner Lewis0f644f42022-12-15 14:13:32 +000024 * 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 Lewis756a34a2022-12-15 14:53:43 +000027 * - Bignum parameters called \c p are inputs used to set up a modulus or
Werner Lewis5e9d2e92022-12-12 14:00:25 +000028 * residue. These must be pointers to an array of limbs.
Werner Lewis214ae642022-12-15 10:57:59 +000029 * - \c T is a temporary storage area. The initial content of such a
Werner Lewis5e9d2e92022-12-12 14:00:25 +000030 * parameter is ignored and the final content is unspecified.
Werner Lewis756a34a2022-12-15 14:53:43 +000031 * - Some functions use different names, such as \c r for the residue.
Werner Lewis5e9d2e92022-12-12 14:00:25 +000032 * - **Bignum sizes**: bignum sizes are always expressed in limbs. Both
Werner Lewis2e70b9a2022-12-14 15:48:31 +000033 * #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 Lewis5e9d2e92022-12-12 14:00:25 +000038 * - **Bignum representation**: the representation of inputs and outputs is
Werner Lewis214ae642022-12-15 10:57:59 +000039 * specified by the \c int_rep field of the modulus.
Werner Lewis5e9d2e92022-12-12 14:00:25 +000040 * - **Parameter ordering**: for bignum parameters, outputs come before inputs.
Werner Lewisa3068862022-12-14 15:57:12 +000041 * The modulus is passed after residues. Temporaries come last.
Werner Lewis5e9d2e92022-12-12 14:00:25 +000042 * - **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 Lewis2bd263d2022-12-14 15:32:31 +000049 * - **Error handling**: functions generally check compatibility of input
Werner Lewis5e9d2e92022-12-12 14:00:25 +000050 * sizes. Most functions will not check that input values are in canonical
Werner Lewis214ae642022-12-15 10:57:59 +000051 * form (i.e. that \c A < \c N), this is only checked during setup of a
Werner Lewis5e9d2e92022-12-12 14:00:25 +000052 * residue structure.
Werner Lewis1d89ebf2022-12-14 17:08:43 +000053 * - **Modular representatives**: all functions expect inputs to be in the
Werner Lewis214ae642022-12-15 10:57:59 +000054 * range [0, \c N - 1] and guarantee outputs in the range [0, \c N - 1].
Werner Lewis1d89ebf2022-12-14 17:08:43 +000055 * 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 Peskine7f887bd2022-09-27 13:12:30 +020062 */
63
64/*
Gabor Mezeif049dbf2022-07-18 23:02:33 +020065 * 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 Peskineeb2e77f2022-12-20 19:22:44 +010090/** 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 */
Janos Follath296ea662022-08-11 14:58:29 +010095typedef enum
96{
Gilles Peskineeb2e77f2022-12-20 19:22:44 +010097 /** Representation not chosen (makes the modulus structure invalid). */
Janos Follath296ea662022-08-11 14:58:29 +010098 MBEDTLS_MPI_MOD_REP_INVALID = 0,
Gilles Peskineeb2e77f2022-12-20 19:22:44 +010099 /* Skip 1 as it is slightly easier to accidentally pass to functions. */
100 /** Montgomery representation. */
Janos Follath296ea662022-08-11 14:58:29 +0100101 MBEDTLS_MPI_MOD_REP_MONTGOMERY = 2,
Gilles Peskineeb2e77f2022-12-20 19:22:44 +0100102 /** TODO: document this.
103 *
104 * Residues are in canonical representation.
105 */
106 MBEDTLS_MPI_MOD_REP_OPT_RED,
Janos Follath296ea662022-08-11 14:58:29 +0100107} mbedtls_mpi_mod_rep_selector;
108
109/* Make mbedtls_mpi_mod_rep_selector and mbedtls_mpi_mod_ext_rep disjoint to
110 * make it easier to catch when they are accidentally swapped. */
111typedef enum
112{
113 MBEDTLS_MPI_MOD_EXT_REP_INVALID = 0,
114 MBEDTLS_MPI_MOD_EXT_REP_LE = 8,
115 MBEDTLS_MPI_MOD_EXT_REP_BE
116} mbedtls_mpi_mod_ext_rep;
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200117
118typedef struct
119{
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200120 mbedtls_mpi_uint *p;
Gabor Mezeifd65e822022-08-12 18:09:12 +0200121 size_t limbs;
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200122} mbedtls_mpi_mod_residue;
123
Hanno Beckercd860df2022-08-18 16:23:05 +0100124typedef struct {
125 mbedtls_mpi_uint const *rr; /* The residue for 2^{2*n*biL} mod N */
126 mbedtls_mpi_uint mm; /* Montgomery const for -N^{-1} mod 2^{ciL} */
127} mbedtls_mpi_mont_struct;
128
Gabor Mezei89e31462022-08-12 15:36:56 +0200129typedef void *mbedtls_mpi_opt_red_struct;
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200130
131typedef struct {
Janos Follathed5c8d32022-08-15 11:50:22 +0100132 const mbedtls_mpi_uint *p;
Gabor Mezeifd65e822022-08-12 18:09:12 +0200133 size_t limbs; // number of limbs
134 size_t bits; // bitlen of p
Janos Follath296ea662022-08-11 14:58:29 +0100135 mbedtls_mpi_mod_rep_selector int_rep; // selector to signal the active member of the union
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200136 union rep
137 {
Gilles Peskineeb2e77f2022-12-20 19:22:44 +0100138 /* if int_rep == #MBEDTLS_MPI_MOD_REP_MONTGOMERY */
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200139 mbedtls_mpi_mont_struct mont;
Gilles Peskineeb2e77f2022-12-20 19:22:44 +0100140 /* if int_rep == #MBEDTLS_MPI_MOD_REP_OPT_RED */
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200141 mbedtls_mpi_opt_red_struct ored;
142 } rep;
143} mbedtls_mpi_mod_modulus;
144
Gabor Mezei37b06362022-08-02 17:22:18 +0200145/** Setup a residue structure.
146 *
Janos Follathfc6fbb42022-11-25 15:43:17 +0000147 * The residue will be set up with the buffer \p p and modulus \p m.
Janos Follath41427de2022-11-24 19:04:54 +0000148 *
Janos Follathfc6fbb42022-11-25 15:43:17 +0000149 * The memory pointed to by \p p will be used by the resulting residue structure.
150 * The value at the pointed-to memory will be the initial value of \p r and must
151 * hold a value that is less than the modulus. This value will be used as-is
Janos Follath41427de2022-11-24 19:04:54 +0000152 * and interpreted according to the value of the `m->int_rep` field.
153 *
154 * The modulus \p m will be the modulus associated with \p r. The residue \p r
Janos Follath6eb92c02022-11-26 17:34:37 +0000155 * should only be used in operations where the modulus is \p m.
Janos Follath41427de2022-11-24 19:04:54 +0000156 *
Janos Follathee530cc2022-11-25 15:54:40 +0000157 * \param[out] r The address of the residue to setup.
Janos Follath6b8a4ad2022-08-19 10:58:34 +0100158 * \param[in] m The address of the modulus related to \p r.
Janos Follathfc6fbb42022-11-25 15:43:17 +0000159 * \param[in] p The address of the limb array containing the value of \p r.
Janos Follath6b8a4ad2022-08-19 10:58:34 +0100160 * The memory pointed to by \p p will be used by \p r and must
161 * not be modified in any way until after
Minos Galanakisaed832a2022-11-24 09:09:47 +0000162 * mbedtls_mpi_mod_residue_release() is called. The data
Janos Follathfc6fbb42022-11-25 15:43:17 +0000163 * pointed to by \p p must be less than the modulus (the value
164 * pointed to by `m->p`) and already in the representation
Janos Follath41427de2022-11-24 19:04:54 +0000165 * indicated by `m->int_rep`.
Janos Follathee530cc2022-11-25 15:54:40 +0000166 * \param p_limbs The number of limbs of \p p. Must be the same as the number
Janos Follath6eb92c02022-11-26 17:34:37 +0000167 * of limbs in the modulus \p m.
Gabor Mezei37b06362022-08-02 17:22:18 +0200168 *
169 * \return \c 0 if successful.
Janos Follathb7a88ec2022-08-19 12:24:40 +0100170 * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if \p p_limbs is less than the
171 * limbs in \p m or if \p p is not less than \p m.
Gabor Mezei37b06362022-08-02 17:22:18 +0200172 */
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200173int mbedtls_mpi_mod_residue_setup( mbedtls_mpi_mod_residue *r,
Janos Follath6b8a4ad2022-08-19 10:58:34 +0100174 const mbedtls_mpi_mod_modulus *m,
Janos Follath8b718b52022-07-25 11:31:02 +0100175 mbedtls_mpi_uint *p,
Janos Follathb7a88ec2022-08-19 12:24:40 +0100176 size_t p_limbs );
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200177
Gabor Mezei37b06362022-08-02 17:22:18 +0200178/** Unbind elements of a residue structure.
179 *
Janos Follathdae11472022-08-08 11:50:02 +0100180 * This function removes the reference to the limb array that was passed to
181 * mbedtls_mpi_mod_residue_setup() to make it safe to free or use again.
182 *
183 * This function invalidates \p r and it must not be used until after
184 * mbedtls_mpi_mod_residue_setup() is called on it again.
185 *
Janos Follath6b8a4ad2022-08-19 10:58:34 +0100186 * \param[out] r The address of residue to release.
Gabor Mezei37b06362022-08-02 17:22:18 +0200187 */
188void mbedtls_mpi_mod_residue_release( mbedtls_mpi_mod_residue *r );
189
190/** Initialize a modulus structure.
191 *
Janos Follatha95f2042022-08-19 12:09:17 +0100192 * \param[out] m The address of the modulus structure to initialize.
Gabor Mezei37b06362022-08-02 17:22:18 +0200193 */
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200194void mbedtls_mpi_mod_modulus_init( mbedtls_mpi_mod_modulus *m );
195
Janos Follath63184682022-08-11 17:42:59 +0100196/** Setup a modulus structure.
Gabor Mezei37b06362022-08-02 17:22:18 +0200197 *
Janos Follath6b8a4ad2022-08-19 10:58:34 +0100198 * \param[out] m The address of the modulus structure to populate.
199 * \param[in] p The address of the limb array storing the value of \p m.
200 * The memory pointed to by \p p will be used by \p m and must
201 * not be modified in any way until after
Janos Follathdae11472022-08-08 11:50:02 +0100202 * mbedtls_mpi_mod_modulus_free() is called.
Janos Follathb7a88ec2022-08-19 12:24:40 +0100203 * \param p_limbs The number of limbs of \p p.
Janos Follathdae11472022-08-08 11:50:02 +0100204 * \param int_rep The internal representation to be used for residues
205 * associated with \p m (see #mbedtls_mpi_mod_rep_selector).
Gabor Mezei37b06362022-08-02 17:22:18 +0200206 *
207 * \return \c 0 if successful.
Janos Follathee530cc2022-11-25 15:54:40 +0000208 * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if \p int_rep is invalid.
Gabor Mezei37b06362022-08-02 17:22:18 +0200209 */
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200210int mbedtls_mpi_mod_modulus_setup( mbedtls_mpi_mod_modulus *m,
Janos Follathed5c8d32022-08-15 11:50:22 +0100211 const mbedtls_mpi_uint *p,
Janos Follathb7a88ec2022-08-19 12:24:40 +0100212 size_t p_limbs,
Janos Follath296ea662022-08-11 14:58:29 +0100213 mbedtls_mpi_mod_rep_selector int_rep );
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200214
Janos Follathdae11472022-08-08 11:50:02 +0100215/** Free elements of a modulus structure.
216 *
217 * This function frees any memory allocated by mbedtls_mpi_mod_modulus_setup().
218 *
219 * \warning This function does not free the limb array passed to
220 * mbedtls_mpi_mod_modulus_setup() only removes the reference to it,
221 * making it safe to free or to use it again.
Gabor Mezei37b06362022-08-02 17:22:18 +0200222 *
Janos Follatha95f2042022-08-19 12:09:17 +0100223 * \param[in,out] m The address of the modulus structure to free.
Gabor Mezei37b06362022-08-02 17:22:18 +0200224 */
225void mbedtls_mpi_mod_modulus_free( mbedtls_mpi_mod_modulus *m );
226
Janos Follath5933f692022-11-02 14:35:17 +0000227/* BEGIN MERGE SLOT 1 */
228
229/* END MERGE SLOT 1 */
230
231/* BEGIN MERGE SLOT 2 */
232
Gabor Mezei9db81e92022-12-13 10:51:37 +0100233/** \brief Multiply two residues, returning the residue modulo the specified
234 * modulus.
235 *
Gabor Mezei6a31b722022-12-16 15:24:03 +0100236 * \note Currently handles the case when `N->int_rep` is
Gabor Mezei9db81e92022-12-13 10:51:37 +0100237 * MBEDTLS_MPI_MOD_REP_MONTGOMERY.
238 *
Gabor Mezei6a31b722022-12-16 15:24:03 +0100239 * The size of the operation is determined by \p N. \p A, \p B and \p X must
240 * all be associated with the modulus \p N and must all have the same number
241 * of limbs as \p N.
Gabor Mezei9db81e92022-12-13 10:51:37 +0100242 *
243 * \p X may be aliased to \p A or \p B, or even both, but may not overlap
244 * either otherwise. They may not alias \p N (since they must be in canonical
245 * form, they cannot == \p N).
246 *
Gabor Mezei6a31b722022-12-16 15:24:03 +0100247 * \param[out] X The address of the result MPI. Must have the same
248 * number of limbs as \p N.
Gabor Mezei9db81e92022-12-13 10:51:37 +0100249 * On successful completion, \p X contains the result of
250 * the multiplication `A * B * R^-1` mod N where
Gabor Mezei6a31b722022-12-16 15:24:03 +0100251 * `R = 2^(biL * N->limbs)`.
252 * \param[in] A The address of the first MPI.
253 * \param[in] B The address of the second MPI.
Gabor Mezei9db81e92022-12-13 10:51:37 +0100254 * \param[in] N The address of the modulus. Used to perform a modulo
255 * operation on the result of the multiplication.
256 *
257 * \return \c 0 if successful.
Gabor Mezei6a31b722022-12-16 15:24:03 +0100258 * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if all the parameters do not
Gabor Mezei9db81e92022-12-13 10:51:37 +0100259 * have the same number of limbs or \p N is invalid.
260 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory-allocation failure.
261 */
262int mbedtls_mpi_mod_mul( mbedtls_mpi_mod_residue *X,
263 const mbedtls_mpi_mod_residue *A,
264 const mbedtls_mpi_mod_residue *B,
265 const mbedtls_mpi_mod_modulus *N );
266
Janos Follath5933f692022-11-02 14:35:17 +0000267/* END MERGE SLOT 2 */
268
269/* BEGIN MERGE SLOT 3 */
Tom Cosgrove62b20482022-12-01 14:27:37 +0000270/**
271 * \brief Perform a fixed-size modular subtraction.
272 *
273 * Calculate `A - B modulo N`.
274 *
275 * \p A, \p B and \p X must all have the same number of limbs as \p N.
276 *
277 * \p X may be aliased to \p A or \p B, or even both, but may not overlap
278 * either otherwise.
279 *
280 * \note This function does not check that \p A or \p B are in canonical
281 * form (that is, are < \p N) - that will have been done by
282 * mbedtls_mpi_mod_residue_setup().
283 *
284 * \param[out] X The address of the result MPI. Must be initialized.
285 * Must have the same number of limbs as the modulus \p N.
286 * \param[in] A The address of the first MPI.
287 * \param[in] B The address of the second MPI.
288 * \param[in] N The address of the modulus. Used to perform a modulo
289 * operation on the result of the subtraction.
290 *
291 * \return \c 0 if successful.
292 * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if the given MPIs do not
293 * have the correct number of limbs.
294 */
295int mbedtls_mpi_mod_sub( mbedtls_mpi_mod_residue *X,
296 const mbedtls_mpi_mod_residue *A,
297 const mbedtls_mpi_mod_residue *B,
298 const mbedtls_mpi_mod_modulus *N );
Tom Cosgrove4302d022022-12-13 10:46:39 +0000299
300/**
301 * \brief Perform modular inversion of an MPI with respect to a modulus \p N.
302 *
Tom Cosgroved692ba42022-12-14 09:53:45 +0000303 * \p A and \p X must be associated with the modulus \p N and will therefore
304 * have the same number of limbs as \p N.
305 *
Tom Cosgrove4302d022022-12-13 10:46:39 +0000306 * \p X may be aliased to \p A.
307 *
308 * \warning Currently only supports prime moduli, but does not check for them.
309 *
310 * \param[out] X The modular inverse of \p A with respect to \p N.
311 * \param[in] A The number to calculate the modular inverse of.
312 * Must not be 0.
313 * \param[in] N The modulus to use.
314 *
315 * \return \c 0 if successful.
316 * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if \p A and \p N do not
317 * have the same number of limbs.
318 * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if \p A is zero.
319 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if couldn't allocate enough
320 * memory (needed for conversion to and from Mongtomery form
321 * when not in Montgomery form already, and for temporary use
322 * by the inversion calculation itself).
323 */
324
325int mbedtls_mpi_mod_inv( mbedtls_mpi_mod_residue *X,
326 const mbedtls_mpi_mod_residue *A,
327 const mbedtls_mpi_mod_modulus *N );
Janos Follath5933f692022-11-02 14:35:17 +0000328/* END MERGE SLOT 3 */
329
330/* BEGIN MERGE SLOT 4 */
331
332/* END MERGE SLOT 4 */
333
334/* BEGIN MERGE SLOT 5 */
Werner Lewise1b6b7c2022-11-29 12:25:05 +0000335/**
336 * \brief Perform a fixed-size modular addition.
337 *
338 * Calculate `A + B modulo N`.
339 *
Werner Lewiseed01aa2022-12-13 17:18:17 +0000340 * \p A, \p B and \p X must all be associated with the modulus \p N and must
341 * all have the same number of limbs as \p N.
Werner Lewise1b6b7c2022-11-29 12:25:05 +0000342 *
343 * \p X may be aliased to \p A or \p B, or even both, but may not overlap
344 * either otherwise.
345 *
346 * \note This function does not check that \p A or \p B are in canonical
347 * form (that is, are < \p N) - that will have been done by
348 * mbedtls_mpi_mod_residue_setup().
349 *
Werner Lewiseed01aa2022-12-13 17:18:17 +0000350 * \param[out] X The address of the result residue. Must be initialized.
Werner Lewise1b6b7c2022-11-29 12:25:05 +0000351 * Must have the same number of limbs as the modulus \p N.
Werner Lewiseed01aa2022-12-13 17:18:17 +0000352 * \param[in] A The address of the first input residue.
353 * \param[in] B The address of the second input residue.
Werner Lewise1b6b7c2022-11-29 12:25:05 +0000354 * \param[in] N The address of the modulus. Used to perform a modulo
355 * operation on the result of the addition.
356 *
357 * \return \c 0 if successful.
358 * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if the given MPIs do not
359 * have the correct number of limbs.
360 */
361int mbedtls_mpi_mod_add( mbedtls_mpi_mod_residue *X,
362 const mbedtls_mpi_mod_residue *A,
363 const mbedtls_mpi_mod_residue *B,
364 const mbedtls_mpi_mod_modulus *N );
Janos Follath5933f692022-11-02 14:35:17 +0000365/* END MERGE SLOT 5 */
366
367/* BEGIN MERGE SLOT 6 */
368
Gilles Peskineb1eea022022-12-07 22:59:27 +0100369/** Generate a random number uniformly in a range.
370 *
371 * This function generates a random number between \p min inclusive and
372 * \p N exclusive.
373 *
374 * The procedure complies with RFC 6979 ยง3.3 (deterministic ECDSA)
375 * when the RNG is a suitably parametrized instance of HMAC_DRBG
376 * and \p min is \c 1.
377 *
378 * \note There are `N - min` possible outputs. The lower bound
379 * \p min can be reached, but the upper bound \p N cannot.
380 *
381 * \param X The destination residue.
382 * \param min The minimum value to return. It must be strictly smaller
383 * than \b N.
384 * \param N The modulus.
385 * This is the upper bound of the output range, exclusive.
386 * \param f_rng The RNG function to use. This must not be \c NULL.
387 * \param p_rng The RNG parameter to be passed to \p f_rng.
388 *
389 * \return \c 0 if successful.
390 * \return #MBEDTLS_ERR_MPI_NOT_ACCEPTABLE if the implementation was
391 * unable to find a suitable value within a limited number
392 * of attempts. This has a negligible probability if \p N
393 * is significantly larger than \p min, which is the case
394 * for all usual cryptographic applications.
395 */
396int mbedtls_mpi_mod_random( mbedtls_mpi_mod_residue *X,
397 mbedtls_mpi_uint min,
398 const mbedtls_mpi_mod_modulus *N,
399 int (*f_rng)(void *, unsigned char *, size_t),
400 void *p_rng );
401
Janos Follath5933f692022-11-02 14:35:17 +0000402/* END MERGE SLOT 6 */
403
404/* BEGIN MERGE SLOT 7 */
Janos Follath41427de2022-11-24 19:04:54 +0000405/** Read a residue from a byte buffer.
Minos Galanakis81f4b112022-11-10 14:40:38 +0000406 *
Janos Follath41427de2022-11-24 19:04:54 +0000407 * The residue will be automatically converted to the internal representation
Janos Follathfc6fbb42022-11-25 15:43:17 +0000408 * based on the value of the `m->int_rep` field.
Minos Galanakis81f4b112022-11-10 14:40:38 +0000409 *
Janos Follath41427de2022-11-24 19:04:54 +0000410 * The modulus \p m will be the modulus associated with \p r. The residue \p r
411 * should only be used in operations where the modulus is \p m or a modulus
412 * equivalent to \p m (in the sense that all their fields or memory pointed by
413 * their fields hold the same value).
414 *
Janos Follath1f8afa22022-11-28 14:32:33 +0000415 * \param[out] r The address of the residue. It must have exactly the same
Janos Follathfc6fbb42022-11-25 15:43:17 +0000416 * number of limbs as the modulus \p m.
Janos Follath1f8afa22022-11-28 14:32:33 +0000417 * \param[in] m The address of the modulus.
418 * \param[in] buf The input buffer to import from.
Janos Follath3e3fc912022-11-24 18:02:46 +0000419 * \param buflen The length in bytes of \p buf.
420 * \param ext_rep The endianness of the number in the input buffer.
Minos Galanakis81f4b112022-11-10 14:40:38 +0000421 *
422 * \return \c 0 if successful.
Janos Follath41427de2022-11-24 19:04:54 +0000423 * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p r isn't
Minos Galanakis81f4b112022-11-10 14:40:38 +0000424 * large enough to hold the value in \p buf.
Janos Follath41427de2022-11-24 19:04:54 +0000425 * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if \p ext_rep
426 * is invalid or the value in the buffer is not less than \p m.
Minos Galanakis81f4b112022-11-10 14:40:38 +0000427 */
428int mbedtls_mpi_mod_read( mbedtls_mpi_mod_residue *r,
Minos Galanakis8b375452022-11-24 11:04:11 +0000429 const mbedtls_mpi_mod_modulus *m,
430 const unsigned char *buf,
Janos Follath3e3fc912022-11-24 18:02:46 +0000431 size_t buflen,
432 mbedtls_mpi_mod_ext_rep ext_rep );
Janos Follath5933f692022-11-02 14:35:17 +0000433
Janos Follath41427de2022-11-24 19:04:54 +0000434/** Write a residue into a byte buffer.
Minos Galanakis81f4b112022-11-10 14:40:38 +0000435 *
Janos Follath41427de2022-11-24 19:04:54 +0000436 * The modulus \p m must be the modulus associated with \p r (see
437 * mbedtls_mpi_mod_residue_setup() and mbedtls_mpi_mod_read()).
Minos Galanakis81f4b112022-11-10 14:40:38 +0000438 *
Janos Follath41427de2022-11-24 19:04:54 +0000439 * The residue will be automatically converted from the internal representation
440 * based on the value of `m->int_rep` field.
441 *
442 * \warning If the buffer is smaller than `m->bits`, the number of
Janos Follath6eb92c02022-11-26 17:34:37 +0000443 * leading zeroes is leaked through timing. If \p r is
Janos Follath41427de2022-11-24 19:04:54 +0000444 * secret, the caller must ensure that \p buflen is at least
445 * (`m->bits`+7)/8.
446 *
Janos Follath1f8afa22022-11-28 14:32:33 +0000447 * \param[in] r The address of the residue. It must have the same number of
448 * limbs as the modulus \p m. (\p r is an input parameter, but
449 * its value will be modified during execution and restored
450 * before the function returns.)
451 * \param[in] m The address of the modulus associated with \r.
452 * \param[out] buf The output buffer to export to.
Janos Follath3e3fc912022-11-24 18:02:46 +0000453 * \param buflen The length in bytes of \p buf.
Janos Follath41427de2022-11-24 19:04:54 +0000454 * \param ext_rep The endianness in which the number should be written into
455 * the output buffer.
Minos Galanakis81f4b112022-11-10 14:40:38 +0000456 *
457 * \return \c 0 if successful.
458 * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p buf isn't
Janos Follath41427de2022-11-24 19:04:54 +0000459 * large enough to hold the value of \p r (without leading
460 * zeroes).
Janos Follathfc6fbb42022-11-25 15:43:17 +0000461 * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if \p ext_rep is invalid.
Janos Follath1f8afa22022-11-28 14:32:33 +0000462 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if couldn't allocate enough
463 * memory for conversion. Can occur only for moduli with
464 * MBEDTLS_MPI_MOD_REP_MONTGOMERY.
Minos Galanakis81f4b112022-11-10 14:40:38 +0000465 */
Minos Galanakis8b375452022-11-24 11:04:11 +0000466int mbedtls_mpi_mod_write( const mbedtls_mpi_mod_residue *r,
467 const mbedtls_mpi_mod_modulus *m,
Minos Galanakis81f4b112022-11-10 14:40:38 +0000468 unsigned char *buf,
Janos Follath3e3fc912022-11-24 18:02:46 +0000469 size_t buflen,
470 mbedtls_mpi_mod_ext_rep ext_rep );
Janos Follath5933f692022-11-02 14:35:17 +0000471/* END MERGE SLOT 7 */
472
473/* BEGIN MERGE SLOT 8 */
474
475/* END MERGE SLOT 8 */
476
477/* BEGIN MERGE SLOT 9 */
478
479/* END MERGE SLOT 9 */
480
481/* BEGIN MERGE SLOT 10 */
482
483/* END MERGE SLOT 10 */
484
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200485#endif /* MBEDTLS_BIGNUM_MOD_H */