blob: 87ee015693ac7d01b2ab1f0022aeb5e851c729ef [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 */
Gilles Peskine449bd832023-01-11 14:50:10 +010095typedef enum {
Gilles Peskineeb2e77f2022-12-20 19:22:44 +010096 /** Representation not chosen (makes the modulus structure invalid). */
Janos Follath296ea662022-08-11 14:58:29 +010097 MBEDTLS_MPI_MOD_REP_INVALID = 0,
Gilles Peskineeb2e77f2022-12-20 19:22:44 +010098 /* Skip 1 as it is slightly easier to accidentally pass to functions. */
99 /** Montgomery representation. */
Janos Follath296ea662022-08-11 14:58:29 +0100100 MBEDTLS_MPI_MOD_REP_MONTGOMERY = 2,
Minos Galanakis5c238d82023-06-09 15:37:53 +0100101 /* Optimised reduction available. This indicates a coordinate modulus (P)
102 * and one of the following available:
103 * - MBEDTLS_ECP_NIST_OPTIM
104 * - Kobliz Curve.
105 * - Fast Reduction Curve CURVE25519 or CURVE448. */
Gilles Peskineeb2e77f2022-12-20 19:22:44 +0100106 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. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100111typedef enum {
Janos Follath296ea662022-08-11 14:58:29 +0100112 MBEDTLS_MPI_MOD_EXT_REP_INVALID = 0,
113 MBEDTLS_MPI_MOD_EXT_REP_LE = 8,
114 MBEDTLS_MPI_MOD_EXT_REP_BE
115} mbedtls_mpi_mod_ext_rep;
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200116
Gilles Peskine449bd832023-01-11 14:50:10 +0100117typedef struct {
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200118 mbedtls_mpi_uint *p;
Gabor Mezeifd65e822022-08-12 18:09:12 +0200119 size_t limbs;
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200120} mbedtls_mpi_mod_residue;
121
Hanno Beckercd860df2022-08-18 16:23:05 +0100122typedef struct {
123 mbedtls_mpi_uint const *rr; /* The residue for 2^{2*n*biL} mod N */
124 mbedtls_mpi_uint mm; /* Montgomery const for -N^{-1} mod 2^{ciL} */
125} mbedtls_mpi_mont_struct;
126
Minos Galanakisc6e68ed2023-06-09 14:43:55 +0100127typedef struct {
Minos Galanakisde874612023-06-13 16:59:26 +0100128 int (*modp)(mbedtls_mpi_uint *X,
129 size_t X_limbs); /* The optimised reduction function pointer */
Minos Galanakisc6e68ed2023-06-09 14:43:55 +0100130} mbedtls_mpi_opt_red_struct;
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200131
132typedef struct {
Janos Follathed5c8d32022-08-15 11:50:22 +0100133 const mbedtls_mpi_uint *p;
Gabor Mezeifd65e822022-08-12 18:09:12 +0200134 size_t limbs; // number of limbs
135 size_t bits; // bitlen of p
Janos Follath296ea662022-08-11 14:58:29 +0100136 mbedtls_mpi_mod_rep_selector int_rep; // selector to signal the active member of the union
Gilles Peskine449bd832023-01-11 14:50:10 +0100137 union rep {
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 *
Mihir Raj Singhb13a5892023-01-11 19:49:00 +0530147 * The residue will be set up with the buffer \p p and modulus \p N.
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
Mihir Raj Singhb13a5892023-01-11 19:49:00 +0530152 * and interpreted according to the value of the `N->int_rep` field.
Janos Follath41427de2022-11-24 19:04:54 +0000153 *
Mihir Raj Singhb13a5892023-01-11 19:49:00 +0530154 * The modulus \p N will be the modulus associated with \p r. The residue \p r
155 * should only be used in operations where the modulus is \p N.
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.
Mihir Raj Singhb13a5892023-01-11 19:49:00 +0530158 * \param[in] N 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
Mihir Raj Singhb13a5892023-01-11 19:49:00 +0530164 * pointed to by `N->p`) and already in the representation
165 * indicated by `N->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
Mihir Raj Singhb13a5892023-01-11 19:49:00 +0530167 * of limbs in the modulus \p N.
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
Mihir Raj Singhb13a5892023-01-11 19:49:00 +0530171 * limbs in \p N or if \p p is not less than \p N.
Gabor Mezei37b06362022-08-02 17:22:18 +0200172 */
Mihir Raj Singh432cacf2023-01-11 21:12:46 +0530173int mbedtls_mpi_mod_residue_setup(mbedtls_mpi_mod_residue *r,
174 const mbedtls_mpi_mod_modulus *N,
175 mbedtls_mpi_uint *p,
176 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 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100188void mbedtls_mpi_mod_residue_release(mbedtls_mpi_mod_residue *r);
Gabor Mezei37b06362022-08-02 17:22:18 +0200189
190/** Initialize a modulus structure.
191 *
Mihir Raj Singhb6fa9402023-01-11 19:55:14 +0530192 * \param[out] N The address of the modulus structure to initialize.
Gabor Mezei37b06362022-08-02 17:22:18 +0200193 */
Mihir Raj Singh432cacf2023-01-11 21:12:46 +0530194void mbedtls_mpi_mod_modulus_init(mbedtls_mpi_mod_modulus *N);
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200195
Janos Follath63184682022-08-11 17:42:59 +0100196/** Setup a modulus structure.
Gabor Mezei37b06362022-08-02 17:22:18 +0200197 *
Mihir Raj Singhb6fa9402023-01-11 19:55:14 +0530198 * \param[out] N The address of the modulus structure to populate.
199 * \param[in] p The address of the limb array storing the value of \p N.
200 * The memory pointed to by \p p will be used by \p N and must
Janos Follath6b8a4ad2022-08-19 10:58:34 +0100201 * 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.
Gabor Mezei37b06362022-08-02 17:22:18 +0200204 *
205 * \return \c 0 if successful.
Gabor Mezei37b06362022-08-02 17:22:18 +0200206 */
Mihir Raj Singh432cacf2023-01-11 21:12:46 +0530207int mbedtls_mpi_mod_modulus_setup(mbedtls_mpi_mod_modulus *N,
208 const mbedtls_mpi_uint *p,
Minos Galanakis88e16df2023-05-09 14:11:43 +0100209 size_t p_limbs);
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200210
Minos Galanakisbbe9db42023-05-09 10:37:21 +0100211/** 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.
Minos Galanakisbe1bf152023-06-09 14:47:55 +0100219 * \param modp A pointer to the optimised reduction function to use. \p p.
Minos Galanakisbbe9db42023-05-09 10:37:21 +0100220 *
221 * \return \c 0 if successful.
222 */
223int mbedtls_mpi_mod_optred_modulus_setup(mbedtls_mpi_mod_modulus *N,
224 const mbedtls_mpi_uint *p,
225 size_t p_limbs,
Minos Galanakisde874612023-06-13 16:59:26 +0100226 int (*modp)(mbedtls_mpi_uint *X,
227 size_t X_limbs));
Minos Galanakisbbe9db42023-05-09 10:37:21 +0100228
Janos Follathdae11472022-08-08 11:50:02 +0100229/** Free elements of a modulus structure.
230 *
231 * This function frees any memory allocated by mbedtls_mpi_mod_modulus_setup().
232 *
233 * \warning This function does not free the limb array passed to
234 * mbedtls_mpi_mod_modulus_setup() only removes the reference to it,
235 * making it safe to free or to use it again.
Gabor Mezei37b06362022-08-02 17:22:18 +0200236 *
Mihir Raj Singh928a07b2023-01-11 20:08:34 +0530237 * \param[in,out] N The address of the modulus structure to free.
Gabor Mezei37b06362022-08-02 17:22:18 +0200238 */
Mihir Raj Singh432cacf2023-01-11 21:12:46 +0530239void mbedtls_mpi_mod_modulus_free(mbedtls_mpi_mod_modulus *N);
Gabor Mezei37b06362022-08-02 17:22:18 +0200240
Gabor Mezei9db81e92022-12-13 10:51:37 +0100241/** \brief Multiply two residues, returning the residue modulo the specified
242 * modulus.
243 *
Gabor Mezei6a31b722022-12-16 15:24:03 +0100244 * \note Currently handles the case when `N->int_rep` is
Gabor Mezei9db81e92022-12-13 10:51:37 +0100245 * MBEDTLS_MPI_MOD_REP_MONTGOMERY.
246 *
Gabor Mezei6a31b722022-12-16 15:24:03 +0100247 * The size of the operation is determined by \p N. \p A, \p B and \p X must
248 * all be associated with the modulus \p N and must all have the same number
249 * of limbs as \p N.
Gabor Mezei9db81e92022-12-13 10:51:37 +0100250 *
251 * \p X may be aliased to \p A or \p B, or even both, but may not overlap
252 * either otherwise. They may not alias \p N (since they must be in canonical
253 * form, they cannot == \p N).
254 *
Gabor Mezei6a31b722022-12-16 15:24:03 +0100255 * \param[out] X The address of the result MPI. Must have the same
256 * number of limbs as \p N.
Gabor Mezei9db81e92022-12-13 10:51:37 +0100257 * On successful completion, \p X contains the result of
258 * the multiplication `A * B * R^-1` mod N where
Gabor Mezei6a31b722022-12-16 15:24:03 +0100259 * `R = 2^(biL * N->limbs)`.
260 * \param[in] A The address of the first MPI.
261 * \param[in] B The address of the second MPI.
Gabor Mezei9db81e92022-12-13 10:51:37 +0100262 * \param[in] N The address of the modulus. Used to perform a modulo
263 * operation on the result of the multiplication.
264 *
265 * \return \c 0 if successful.
Gabor Mezei6a31b722022-12-16 15:24:03 +0100266 * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if all the parameters do not
Gabor Mezei9db81e92022-12-13 10:51:37 +0100267 * have the same number of limbs or \p N is invalid.
268 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory-allocation failure.
269 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100270int mbedtls_mpi_mod_mul(mbedtls_mpi_mod_residue *X,
271 const mbedtls_mpi_mod_residue *A,
272 const mbedtls_mpi_mod_residue *B,
273 const mbedtls_mpi_mod_modulus *N);
Gabor Mezei9db81e92022-12-13 10:51:37 +0100274
Tom Cosgrove62b20482022-12-01 14:27:37 +0000275/**
276 * \brief Perform a fixed-size modular subtraction.
277 *
278 * Calculate `A - B modulo N`.
279 *
280 * \p A, \p B and \p X must all have the same number of limbs as \p N.
281 *
282 * \p X may be aliased to \p A or \p B, or even both, but may not overlap
283 * either otherwise.
284 *
285 * \note This function does not check that \p A or \p B are in canonical
286 * form (that is, are < \p N) - that will have been done by
287 * mbedtls_mpi_mod_residue_setup().
288 *
289 * \param[out] X The address of the result MPI. Must be initialized.
290 * Must have the same number of limbs as the modulus \p N.
291 * \param[in] A The address of the first MPI.
292 * \param[in] B The address of the second MPI.
293 * \param[in] N The address of the modulus. Used to perform a modulo
294 * operation on the result of the subtraction.
295 *
296 * \return \c 0 if successful.
297 * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if the given MPIs do not
298 * have the correct number of limbs.
299 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100300int mbedtls_mpi_mod_sub(mbedtls_mpi_mod_residue *X,
301 const mbedtls_mpi_mod_residue *A,
302 const mbedtls_mpi_mod_residue *B,
303 const mbedtls_mpi_mod_modulus *N);
Tom Cosgrove4302d022022-12-13 10:46:39 +0000304
305/**
306 * \brief Perform modular inversion of an MPI with respect to a modulus \p N.
307 *
Tom Cosgroved692ba42022-12-14 09:53:45 +0000308 * \p A and \p X must be associated with the modulus \p N and will therefore
309 * have the same number of limbs as \p N.
310 *
Tom Cosgrove4302d022022-12-13 10:46:39 +0000311 * \p X may be aliased to \p A.
312 *
313 * \warning Currently only supports prime moduli, but does not check for them.
314 *
315 * \param[out] X The modular inverse of \p A with respect to \p N.
316 * \param[in] A The number to calculate the modular inverse of.
317 * Must not be 0.
318 * \param[in] N The modulus to use.
319 *
320 * \return \c 0 if successful.
321 * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if \p A and \p N do not
322 * have the same number of limbs.
323 * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if \p A is zero.
324 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if couldn't allocate enough
325 * memory (needed for conversion to and from Mongtomery form
326 * when not in Montgomery form already, and for temporary use
327 * by the inversion calculation itself).
328 */
329
Gilles Peskine449bd832023-01-11 14:50:10 +0100330int mbedtls_mpi_mod_inv(mbedtls_mpi_mod_residue *X,
331 const mbedtls_mpi_mod_residue *A,
332 const mbedtls_mpi_mod_modulus *N);
Werner Lewise1b6b7c2022-11-29 12:25:05 +0000333/**
334 * \brief Perform a fixed-size modular addition.
335 *
336 * Calculate `A + B modulo N`.
337 *
Werner Lewiseed01aa2022-12-13 17:18:17 +0000338 * \p A, \p B and \p X must all be associated with the modulus \p N and must
339 * all have the same number of limbs as \p N.
Werner Lewise1b6b7c2022-11-29 12:25:05 +0000340 *
341 * \p X may be aliased to \p A or \p B, or even both, but may not overlap
342 * either otherwise.
343 *
344 * \note This function does not check that \p A or \p B are in canonical
345 * form (that is, are < \p N) - that will have been done by
346 * mbedtls_mpi_mod_residue_setup().
347 *
Werner Lewiseed01aa2022-12-13 17:18:17 +0000348 * \param[out] X The address of the result residue. Must be initialized.
Werner Lewise1b6b7c2022-11-29 12:25:05 +0000349 * Must have the same number of limbs as the modulus \p N.
Werner Lewiseed01aa2022-12-13 17:18:17 +0000350 * \param[in] A The address of the first input residue.
351 * \param[in] B The address of the second input residue.
Werner Lewise1b6b7c2022-11-29 12:25:05 +0000352 * \param[in] N The address of the modulus. Used to perform a modulo
353 * operation on the result of the addition.
354 *
355 * \return \c 0 if successful.
356 * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if the given MPIs do not
357 * have the correct number of limbs.
358 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100359int mbedtls_mpi_mod_add(mbedtls_mpi_mod_residue *X,
360 const mbedtls_mpi_mod_residue *A,
361 const mbedtls_mpi_mod_residue *B,
362 const mbedtls_mpi_mod_modulus *N);
Janos Follath5933f692022-11-02 14:35:17 +0000363
Gilles Peskineb1eea022022-12-07 22:59:27 +0100364/** Generate a random number uniformly in a range.
365 *
366 * This function generates a random number between \p min inclusive and
367 * \p N exclusive.
368 *
369 * The procedure complies with RFC 6979 ยง3.3 (deterministic ECDSA)
370 * when the RNG is a suitably parametrized instance of HMAC_DRBG
371 * and \p min is \c 1.
372 *
373 * \note There are `N - min` possible outputs. The lower bound
374 * \p min can be reached, but the upper bound \p N cannot.
375 *
376 * \param X The destination residue.
377 * \param min The minimum value to return. It must be strictly smaller
378 * than \b N.
379 * \param N The modulus.
380 * This is the upper bound of the output range, exclusive.
381 * \param f_rng The RNG function to use. This must not be \c NULL.
382 * \param p_rng The RNG parameter to be passed to \p f_rng.
383 *
384 * \return \c 0 if successful.
385 * \return #MBEDTLS_ERR_MPI_NOT_ACCEPTABLE if the implementation was
386 * unable to find a suitable value within a limited number
387 * of attempts. This has a negligible probability if \p N
388 * is significantly larger than \p min, which is the case
389 * for all usual cryptographic applications.
390 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100391int mbedtls_mpi_mod_random(mbedtls_mpi_mod_residue *X,
392 mbedtls_mpi_uint min,
393 const mbedtls_mpi_mod_modulus *N,
394 int (*f_rng)(void *, unsigned char *, size_t),
395 void *p_rng);
Gilles Peskineb1eea022022-12-07 22:59:27 +0100396
Janos Follath41427de2022-11-24 19:04:54 +0000397/** Read a residue from a byte buffer.
Minos Galanakis81f4b112022-11-10 14:40:38 +0000398 *
Janos Follath41427de2022-11-24 19:04:54 +0000399 * The residue will be automatically converted to the internal representation
Mihir Raj Singhfdc314b2023-01-11 20:32:59 +0530400 * based on the value of the `N->int_rep` field.
Minos Galanakis81f4b112022-11-10 14:40:38 +0000401 *
Mihir Raj Singhfdc314b2023-01-11 20:32:59 +0530402 * The modulus \p N will be the modulus associated with \p r. The residue \p r
403 * should only be used in operations where the modulus is \p N or a modulus
404 * equivalent to \p N (in the sense that all their fields or memory pointed by
Janos Follath41427de2022-11-24 19:04:54 +0000405 * their fields hold the same value).
406 *
Janos Follath1f8afa22022-11-28 14:32:33 +0000407 * \param[out] r The address of the residue. It must have exactly the same
Mihir Raj Singhfdc314b2023-01-11 20:32:59 +0530408 * number of limbs as the modulus \p N.
409 * \param[in] N The address of the modulus.
Janos Follath1f8afa22022-11-28 14:32:33 +0000410 * \param[in] buf The input buffer to import from.
Janos Follath3e3fc912022-11-24 18:02:46 +0000411 * \param buflen The length in bytes of \p buf.
412 * \param ext_rep The endianness of the number in the input buffer.
Minos Galanakis81f4b112022-11-10 14:40:38 +0000413 *
414 * \return \c 0 if successful.
Janos Follath41427de2022-11-24 19:04:54 +0000415 * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p r isn't
Minos Galanakis81f4b112022-11-10 14:40:38 +0000416 * large enough to hold the value in \p buf.
Janos Follath41427de2022-11-24 19:04:54 +0000417 * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if \p ext_rep
Mihir Raj Singhfdc314b2023-01-11 20:32:59 +0530418 * is invalid or the value in the buffer is not less than \p N.
Minos Galanakis81f4b112022-11-10 14:40:38 +0000419 */
Mihir Raj Singh432cacf2023-01-11 21:12:46 +0530420int mbedtls_mpi_mod_read(mbedtls_mpi_mod_residue *r,
421 const mbedtls_mpi_mod_modulus *N,
422 const unsigned char *buf,
423 size_t buflen,
424 mbedtls_mpi_mod_ext_rep ext_rep);
Janos Follath5933f692022-11-02 14:35:17 +0000425
Janos Follath41427de2022-11-24 19:04:54 +0000426/** Write a residue into a byte buffer.
Minos Galanakis81f4b112022-11-10 14:40:38 +0000427 *
Mihir Raj Singha43290d2023-01-11 20:46:18 +0530428 * The modulus \p N must be the modulus associated with \p r (see
Janos Follath41427de2022-11-24 19:04:54 +0000429 * mbedtls_mpi_mod_residue_setup() and mbedtls_mpi_mod_read()).
Minos Galanakis81f4b112022-11-10 14:40:38 +0000430 *
Janos Follath41427de2022-11-24 19:04:54 +0000431 * The residue will be automatically converted from the internal representation
Mihir Raj Singha43290d2023-01-11 20:46:18 +0530432 * based on the value of `N->int_rep` field.
Janos Follath41427de2022-11-24 19:04:54 +0000433 *
Mihir Raj Singha43290d2023-01-11 20:46:18 +0530434 * \warning If the buffer is smaller than `N->bits`, the number of
Janos Follath6eb92c02022-11-26 17:34:37 +0000435 * leading zeroes is leaked through timing. If \p r is
Janos Follath41427de2022-11-24 19:04:54 +0000436 * secret, the caller must ensure that \p buflen is at least
Mihir Raj Singha43290d2023-01-11 20:46:18 +0530437 * (`N->bits`+7)/8.
Janos Follath41427de2022-11-24 19:04:54 +0000438 *
Janos Follath1f8afa22022-11-28 14:32:33 +0000439 * \param[in] r The address of the residue. It must have the same number of
Mihir Raj Singha43290d2023-01-11 20:46:18 +0530440 * limbs as the modulus \p N. (\p r is an input parameter, but
Janos Follath1f8afa22022-11-28 14:32:33 +0000441 * its value will be modified during execution and restored
442 * before the function returns.)
Tom Cosgrove8a1f7842023-02-01 08:43:54 +0000443 * \param[in] N The address of the modulus associated with \p r.
Janos Follath1f8afa22022-11-28 14:32:33 +0000444 * \param[out] buf The output buffer to export to.
Janos Follath3e3fc912022-11-24 18:02:46 +0000445 * \param buflen The length in bytes of \p buf.
Janos Follath41427de2022-11-24 19:04:54 +0000446 * \param ext_rep The endianness in which the number should be written into
447 * the output buffer.
Minos Galanakis81f4b112022-11-10 14:40:38 +0000448 *
449 * \return \c 0 if successful.
450 * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p buf isn't
Janos Follath41427de2022-11-24 19:04:54 +0000451 * large enough to hold the value of \p r (without leading
452 * zeroes).
Janos Follathfc6fbb42022-11-25 15:43:17 +0000453 * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if \p ext_rep is invalid.
Janos Follath1f8afa22022-11-28 14:32:33 +0000454 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if couldn't allocate enough
455 * memory for conversion. Can occur only for moduli with
456 * MBEDTLS_MPI_MOD_REP_MONTGOMERY.
Minos Galanakis81f4b112022-11-10 14:40:38 +0000457 */
Mihir Raj Singh432cacf2023-01-11 21:12:46 +0530458int mbedtls_mpi_mod_write(const mbedtls_mpi_mod_residue *r,
459 const mbedtls_mpi_mod_modulus *N,
460 unsigned char *buf,
461 size_t buflen,
462 mbedtls_mpi_mod_ext_rep ext_rep);
Janos Follath5933f692022-11-02 14:35:17 +0000463
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200464#endif /* MBEDTLS_BIGNUM_MOD_H */