blob: d2f6cee5e07d62eab2a4cc256c74044990d9fda7 [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
Janos Follath296ea662022-08-11 14:58:29 +010090/* Skip 1 as it is slightly easier to accidentally pass to functions. */
91typedef enum
92{
93 MBEDTLS_MPI_MOD_REP_INVALID = 0,
94 MBEDTLS_MPI_MOD_REP_MONTGOMERY = 2,
95 MBEDTLS_MPI_MOD_REP_OPT_RED
96} mbedtls_mpi_mod_rep_selector;
97
98/* Make mbedtls_mpi_mod_rep_selector and mbedtls_mpi_mod_ext_rep disjoint to
99 * make it easier to catch when they are accidentally swapped. */
100typedef enum
101{
102 MBEDTLS_MPI_MOD_EXT_REP_INVALID = 0,
103 MBEDTLS_MPI_MOD_EXT_REP_LE = 8,
104 MBEDTLS_MPI_MOD_EXT_REP_BE
105} mbedtls_mpi_mod_ext_rep;
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200106
107typedef struct
108{
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200109 mbedtls_mpi_uint *p;
Gabor Mezeifd65e822022-08-12 18:09:12 +0200110 size_t limbs;
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200111} mbedtls_mpi_mod_residue;
112
Hanno Beckercd860df2022-08-18 16:23:05 +0100113typedef struct {
114 mbedtls_mpi_uint const *rr; /* The residue for 2^{2*n*biL} mod N */
115 mbedtls_mpi_uint mm; /* Montgomery const for -N^{-1} mod 2^{ciL} */
116} mbedtls_mpi_mont_struct;
117
Gabor Mezei89e31462022-08-12 15:36:56 +0200118typedef void *mbedtls_mpi_opt_red_struct;
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200119
120typedef struct {
Janos Follathed5c8d32022-08-15 11:50:22 +0100121 const mbedtls_mpi_uint *p;
Gabor Mezeifd65e822022-08-12 18:09:12 +0200122 size_t limbs; // number of limbs
123 size_t bits; // bitlen of p
Janos Follath296ea662022-08-11 14:58:29 +0100124 mbedtls_mpi_mod_rep_selector int_rep; // selector to signal the active member of the union
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200125 union rep
126 {
127 mbedtls_mpi_mont_struct mont;
128 mbedtls_mpi_opt_red_struct ored;
129 } rep;
130} mbedtls_mpi_mod_modulus;
131
Gabor Mezei37b06362022-08-02 17:22:18 +0200132/** Setup a residue structure.
133 *
Janos Follathfc6fbb42022-11-25 15:43:17 +0000134 * The residue will be set up with the buffer \p p and modulus \p m.
Janos Follath41427de2022-11-24 19:04:54 +0000135 *
Janos Follathfc6fbb42022-11-25 15:43:17 +0000136 * The memory pointed to by \p p will be used by the resulting residue structure.
137 * The value at the pointed-to memory will be the initial value of \p r and must
138 * hold a value that is less than the modulus. This value will be used as-is
Janos Follath41427de2022-11-24 19:04:54 +0000139 * and interpreted according to the value of the `m->int_rep` field.
140 *
141 * The modulus \p m will be the modulus associated with \p r. The residue \p r
Janos Follath6eb92c02022-11-26 17:34:37 +0000142 * should only be used in operations where the modulus is \p m.
Janos Follath41427de2022-11-24 19:04:54 +0000143 *
Janos Follathee530cc2022-11-25 15:54:40 +0000144 * \param[out] r The address of the residue to setup.
Janos Follath6b8a4ad2022-08-19 10:58:34 +0100145 * \param[in] m The address of the modulus related to \p r.
Janos Follathfc6fbb42022-11-25 15:43:17 +0000146 * \param[in] p The address of the limb array containing the value of \p r.
Janos Follath6b8a4ad2022-08-19 10:58:34 +0100147 * The memory pointed to by \p p will be used by \p r and must
148 * not be modified in any way until after
Minos Galanakisaed832a2022-11-24 09:09:47 +0000149 * mbedtls_mpi_mod_residue_release() is called. The data
Janos Follathfc6fbb42022-11-25 15:43:17 +0000150 * pointed to by \p p must be less than the modulus (the value
151 * pointed to by `m->p`) and already in the representation
Janos Follath41427de2022-11-24 19:04:54 +0000152 * indicated by `m->int_rep`.
Janos Follathee530cc2022-11-25 15:54:40 +0000153 * \param p_limbs The number of limbs of \p p. Must be the same as the number
Janos Follath6eb92c02022-11-26 17:34:37 +0000154 * of limbs in the modulus \p m.
Gabor Mezei37b06362022-08-02 17:22:18 +0200155 *
156 * \return \c 0 if successful.
Janos Follathb7a88ec2022-08-19 12:24:40 +0100157 * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if \p p_limbs is less than the
158 * limbs in \p m or if \p p is not less than \p m.
Gabor Mezei37b06362022-08-02 17:22:18 +0200159 */
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200160int mbedtls_mpi_mod_residue_setup( mbedtls_mpi_mod_residue *r,
Janos Follath6b8a4ad2022-08-19 10:58:34 +0100161 const mbedtls_mpi_mod_modulus *m,
Janos Follath8b718b52022-07-25 11:31:02 +0100162 mbedtls_mpi_uint *p,
Janos Follathb7a88ec2022-08-19 12:24:40 +0100163 size_t p_limbs );
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200164
Gabor Mezei37b06362022-08-02 17:22:18 +0200165/** Unbind elements of a residue structure.
166 *
Janos Follathdae11472022-08-08 11:50:02 +0100167 * This function removes the reference to the limb array that was passed to
168 * mbedtls_mpi_mod_residue_setup() to make it safe to free or use again.
169 *
170 * This function invalidates \p r and it must not be used until after
171 * mbedtls_mpi_mod_residue_setup() is called on it again.
172 *
Janos Follath6b8a4ad2022-08-19 10:58:34 +0100173 * \param[out] r The address of residue to release.
Gabor Mezei37b06362022-08-02 17:22:18 +0200174 */
175void mbedtls_mpi_mod_residue_release( mbedtls_mpi_mod_residue *r );
176
177/** Initialize a modulus structure.
178 *
Janos Follatha95f2042022-08-19 12:09:17 +0100179 * \param[out] m The address of the modulus structure to initialize.
Gabor Mezei37b06362022-08-02 17:22:18 +0200180 */
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200181void mbedtls_mpi_mod_modulus_init( mbedtls_mpi_mod_modulus *m );
182
Janos Follath63184682022-08-11 17:42:59 +0100183/** Setup a modulus structure.
Gabor Mezei37b06362022-08-02 17:22:18 +0200184 *
Janos Follath6b8a4ad2022-08-19 10:58:34 +0100185 * \param[out] m The address of the modulus structure to populate.
186 * \param[in] p The address of the limb array storing the value of \p m.
187 * The memory pointed to by \p p will be used by \p m and must
188 * not be modified in any way until after
Janos Follathdae11472022-08-08 11:50:02 +0100189 * mbedtls_mpi_mod_modulus_free() is called.
Janos Follathb7a88ec2022-08-19 12:24:40 +0100190 * \param p_limbs The number of limbs of \p p.
Janos Follathdae11472022-08-08 11:50:02 +0100191 * \param int_rep The internal representation to be used for residues
192 * associated with \p m (see #mbedtls_mpi_mod_rep_selector).
Gabor Mezei37b06362022-08-02 17:22:18 +0200193 *
194 * \return \c 0 if successful.
Janos Follathee530cc2022-11-25 15:54:40 +0000195 * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if \p int_rep is invalid.
Gabor Mezei37b06362022-08-02 17:22:18 +0200196 */
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200197int mbedtls_mpi_mod_modulus_setup( mbedtls_mpi_mod_modulus *m,
Janos Follathed5c8d32022-08-15 11:50:22 +0100198 const mbedtls_mpi_uint *p,
Janos Follathb7a88ec2022-08-19 12:24:40 +0100199 size_t p_limbs,
Janos Follath296ea662022-08-11 14:58:29 +0100200 mbedtls_mpi_mod_rep_selector int_rep );
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200201
Janos Follathdae11472022-08-08 11:50:02 +0100202/** Free elements of a modulus structure.
203 *
204 * This function frees any memory allocated by mbedtls_mpi_mod_modulus_setup().
205 *
206 * \warning This function does not free the limb array passed to
207 * mbedtls_mpi_mod_modulus_setup() only removes the reference to it,
208 * making it safe to free or to use it again.
Gabor Mezei37b06362022-08-02 17:22:18 +0200209 *
Janos Follatha95f2042022-08-19 12:09:17 +0100210 * \param[in,out] m The address of the modulus structure to free.
Gabor Mezei37b06362022-08-02 17:22:18 +0200211 */
212void mbedtls_mpi_mod_modulus_free( mbedtls_mpi_mod_modulus *m );
213
Janos Follath5933f692022-11-02 14:35:17 +0000214/* BEGIN MERGE SLOT 1 */
215
216/* END MERGE SLOT 1 */
217
218/* BEGIN MERGE SLOT 2 */
219
Gabor Mezei9db81e92022-12-13 10:51:37 +0100220/** \brief Multiply two residues, returning the residue modulo the specified
221 * modulus.
222 *
Gabor Mezei6a31b722022-12-16 15:24:03 +0100223 * \note Currently handles the case when `N->int_rep` is
Gabor Mezei9db81e92022-12-13 10:51:37 +0100224 * MBEDTLS_MPI_MOD_REP_MONTGOMERY.
225 *
Gabor Mezei6a31b722022-12-16 15:24:03 +0100226 * The size of the operation is determined by \p N. \p A, \p B and \p X must
227 * all be associated with the modulus \p N and must all have the same number
228 * of limbs as \p N.
Gabor Mezei9db81e92022-12-13 10:51:37 +0100229 *
230 * \p X may be aliased to \p A or \p B, or even both, but may not overlap
231 * either otherwise. They may not alias \p N (since they must be in canonical
232 * form, they cannot == \p N).
233 *
Gabor Mezei6a31b722022-12-16 15:24:03 +0100234 * \param[out] X The address of the result MPI. Must have the same
235 * number of limbs as \p N.
Gabor Mezei9db81e92022-12-13 10:51:37 +0100236 * On successful completion, \p X contains the result of
237 * the multiplication `A * B * R^-1` mod N where
Gabor Mezei6a31b722022-12-16 15:24:03 +0100238 * `R = 2^(biL * N->limbs)`.
239 * \param[in] A The address of the first MPI.
240 * \param[in] B The address of the second MPI.
Gabor Mezei9db81e92022-12-13 10:51:37 +0100241 * \param[in] N The address of the modulus. Used to perform a modulo
242 * operation on the result of the multiplication.
243 *
244 * \return \c 0 if successful.
Gabor Mezei6a31b722022-12-16 15:24:03 +0100245 * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if all the parameters do not
Gabor Mezei9db81e92022-12-13 10:51:37 +0100246 * have the same number of limbs or \p N is invalid.
247 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory-allocation failure.
248 */
249int mbedtls_mpi_mod_mul( mbedtls_mpi_mod_residue *X,
250 const mbedtls_mpi_mod_residue *A,
251 const mbedtls_mpi_mod_residue *B,
252 const mbedtls_mpi_mod_modulus *N );
253
Janos Follath5933f692022-11-02 14:35:17 +0000254/* END MERGE SLOT 2 */
255
256/* BEGIN MERGE SLOT 3 */
Tom Cosgrove62b20482022-12-01 14:27:37 +0000257/**
258 * \brief Perform a fixed-size modular subtraction.
259 *
260 * Calculate `A - B modulo N`.
261 *
262 * \p A, \p B and \p X must all have the same number of limbs as \p N.
263 *
264 * \p X may be aliased to \p A or \p B, or even both, but may not overlap
265 * either otherwise.
266 *
267 * \note This function does not check that \p A or \p B are in canonical
268 * form (that is, are < \p N) - that will have been done by
269 * mbedtls_mpi_mod_residue_setup().
270 *
271 * \param[out] X The address of the result MPI. Must be initialized.
272 * Must have the same number of limbs as the modulus \p N.
273 * \param[in] A The address of the first MPI.
274 * \param[in] B The address of the second MPI.
275 * \param[in] N The address of the modulus. Used to perform a modulo
276 * operation on the result of the subtraction.
277 *
278 * \return \c 0 if successful.
279 * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if the given MPIs do not
280 * have the correct number of limbs.
281 */
282int mbedtls_mpi_mod_sub( mbedtls_mpi_mod_residue *X,
283 const mbedtls_mpi_mod_residue *A,
284 const mbedtls_mpi_mod_residue *B,
285 const mbedtls_mpi_mod_modulus *N );
Tom Cosgrove4302d022022-12-13 10:46:39 +0000286
287/**
288 * \brief Perform modular inversion of an MPI with respect to a modulus \p N.
289 *
Tom Cosgroved692ba42022-12-14 09:53:45 +0000290 * \p A and \p X must be associated with the modulus \p N and will therefore
291 * have the same number of limbs as \p N.
292 *
Tom Cosgrove4302d022022-12-13 10:46:39 +0000293 * \p X may be aliased to \p A.
294 *
295 * \warning Currently only supports prime moduli, but does not check for them.
296 *
297 * \param[out] X The modular inverse of \p A with respect to \p N.
298 * \param[in] A The number to calculate the modular inverse of.
299 * Must not be 0.
300 * \param[in] N The modulus to use.
301 *
302 * \return \c 0 if successful.
303 * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if \p A and \p N do not
304 * have the same number of limbs.
305 * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if \p A is zero.
306 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if couldn't allocate enough
307 * memory (needed for conversion to and from Mongtomery form
308 * when not in Montgomery form already, and for temporary use
309 * by the inversion calculation itself).
310 */
311
312int mbedtls_mpi_mod_inv( mbedtls_mpi_mod_residue *X,
313 const mbedtls_mpi_mod_residue *A,
314 const mbedtls_mpi_mod_modulus *N );
Janos Follath5933f692022-11-02 14:35:17 +0000315/* END MERGE SLOT 3 */
316
317/* BEGIN MERGE SLOT 4 */
318
319/* END MERGE SLOT 4 */
320
321/* BEGIN MERGE SLOT 5 */
Werner Lewise1b6b7c2022-11-29 12:25:05 +0000322/**
323 * \brief Perform a fixed-size modular addition.
324 *
325 * Calculate `A + B modulo N`.
326 *
Werner Lewiseed01aa2022-12-13 17:18:17 +0000327 * \p A, \p B and \p X must all be associated with the modulus \p N and must
328 * all have the same number of limbs as \p N.
Werner Lewise1b6b7c2022-11-29 12:25:05 +0000329 *
330 * \p X may be aliased to \p A or \p B, or even both, but may not overlap
331 * either otherwise.
332 *
333 * \note This function does not check that \p A or \p B are in canonical
334 * form (that is, are < \p N) - that will have been done by
335 * mbedtls_mpi_mod_residue_setup().
336 *
Werner Lewiseed01aa2022-12-13 17:18:17 +0000337 * \param[out] X The address of the result residue. Must be initialized.
Werner Lewise1b6b7c2022-11-29 12:25:05 +0000338 * Must have the same number of limbs as the modulus \p N.
Werner Lewiseed01aa2022-12-13 17:18:17 +0000339 * \param[in] A The address of the first input residue.
340 * \param[in] B The address of the second input residue.
Werner Lewise1b6b7c2022-11-29 12:25:05 +0000341 * \param[in] N The address of the modulus. Used to perform a modulo
342 * operation on the result of the addition.
343 *
344 * \return \c 0 if successful.
345 * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if the given MPIs do not
346 * have the correct number of limbs.
347 */
348int mbedtls_mpi_mod_add( mbedtls_mpi_mod_residue *X,
349 const mbedtls_mpi_mod_residue *A,
350 const mbedtls_mpi_mod_residue *B,
351 const mbedtls_mpi_mod_modulus *N );
Janos Follath5933f692022-11-02 14:35:17 +0000352/* END MERGE SLOT 5 */
353
354/* BEGIN MERGE SLOT 6 */
355
356/* END MERGE SLOT 6 */
357
358/* BEGIN MERGE SLOT 7 */
Janos Follath41427de2022-11-24 19:04:54 +0000359/** Read a residue from a byte buffer.
Minos Galanakis81f4b112022-11-10 14:40:38 +0000360 *
Janos Follath41427de2022-11-24 19:04:54 +0000361 * The residue will be automatically converted to the internal representation
Janos Follathfc6fbb42022-11-25 15:43:17 +0000362 * based on the value of the `m->int_rep` field.
Minos Galanakis81f4b112022-11-10 14:40:38 +0000363 *
Janos Follath41427de2022-11-24 19:04:54 +0000364 * The modulus \p m will be the modulus associated with \p r. The residue \p r
365 * should only be used in operations where the modulus is \p m or a modulus
366 * equivalent to \p m (in the sense that all their fields or memory pointed by
367 * their fields hold the same value).
368 *
Janos Follath1f8afa22022-11-28 14:32:33 +0000369 * \param[out] r The address of the residue. It must have exactly the same
Janos Follathfc6fbb42022-11-25 15:43:17 +0000370 * number of limbs as the modulus \p m.
Janos Follath1f8afa22022-11-28 14:32:33 +0000371 * \param[in] m The address of the modulus.
372 * \param[in] buf The input buffer to import from.
Janos Follath3e3fc912022-11-24 18:02:46 +0000373 * \param buflen The length in bytes of \p buf.
374 * \param ext_rep The endianness of the number in the input buffer.
Minos Galanakis81f4b112022-11-10 14:40:38 +0000375 *
376 * \return \c 0 if successful.
Janos Follath41427de2022-11-24 19:04:54 +0000377 * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p r isn't
Minos Galanakis81f4b112022-11-10 14:40:38 +0000378 * large enough to hold the value in \p buf.
Janos Follath41427de2022-11-24 19:04:54 +0000379 * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if \p ext_rep
380 * is invalid or the value in the buffer is not less than \p m.
Minos Galanakis81f4b112022-11-10 14:40:38 +0000381 */
382int mbedtls_mpi_mod_read( mbedtls_mpi_mod_residue *r,
Minos Galanakis8b375452022-11-24 11:04:11 +0000383 const mbedtls_mpi_mod_modulus *m,
384 const unsigned char *buf,
Janos Follath3e3fc912022-11-24 18:02:46 +0000385 size_t buflen,
386 mbedtls_mpi_mod_ext_rep ext_rep );
Janos Follath5933f692022-11-02 14:35:17 +0000387
Janos Follath41427de2022-11-24 19:04:54 +0000388/** Write a residue into a byte buffer.
Minos Galanakis81f4b112022-11-10 14:40:38 +0000389 *
Janos Follath41427de2022-11-24 19:04:54 +0000390 * The modulus \p m must be the modulus associated with \p r (see
391 * mbedtls_mpi_mod_residue_setup() and mbedtls_mpi_mod_read()).
Minos Galanakis81f4b112022-11-10 14:40:38 +0000392 *
Janos Follath41427de2022-11-24 19:04:54 +0000393 * The residue will be automatically converted from the internal representation
394 * based on the value of `m->int_rep` field.
395 *
396 * \warning If the buffer is smaller than `m->bits`, the number of
Janos Follath6eb92c02022-11-26 17:34:37 +0000397 * leading zeroes is leaked through timing. If \p r is
Janos Follath41427de2022-11-24 19:04:54 +0000398 * secret, the caller must ensure that \p buflen is at least
399 * (`m->bits`+7)/8.
400 *
Janos Follath1f8afa22022-11-28 14:32:33 +0000401 * \param[in] r The address of the residue. It must have the same number of
402 * limbs as the modulus \p m. (\p r is an input parameter, but
403 * its value will be modified during execution and restored
404 * before the function returns.)
405 * \param[in] m The address of the modulus associated with \r.
406 * \param[out] buf The output buffer to export to.
Janos Follath3e3fc912022-11-24 18:02:46 +0000407 * \param buflen The length in bytes of \p buf.
Janos Follath41427de2022-11-24 19:04:54 +0000408 * \param ext_rep The endianness in which the number should be written into
409 * the output buffer.
Minos Galanakis81f4b112022-11-10 14:40:38 +0000410 *
411 * \return \c 0 if successful.
412 * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p buf isn't
Janos Follath41427de2022-11-24 19:04:54 +0000413 * large enough to hold the value of \p r (without leading
414 * zeroes).
Janos Follathfc6fbb42022-11-25 15:43:17 +0000415 * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if \p ext_rep is invalid.
Janos Follath1f8afa22022-11-28 14:32:33 +0000416 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if couldn't allocate enough
417 * memory for conversion. Can occur only for moduli with
418 * MBEDTLS_MPI_MOD_REP_MONTGOMERY.
Minos Galanakis81f4b112022-11-10 14:40:38 +0000419 */
Minos Galanakis8b375452022-11-24 11:04:11 +0000420int mbedtls_mpi_mod_write( const mbedtls_mpi_mod_residue *r,
421 const mbedtls_mpi_mod_modulus *m,
Minos Galanakis81f4b112022-11-10 14:40:38 +0000422 unsigned char *buf,
Janos Follath3e3fc912022-11-24 18:02:46 +0000423 size_t buflen,
424 mbedtls_mpi_mod_ext_rep ext_rep );
Janos Follath5933f692022-11-02 14:35:17 +0000425/* END MERGE SLOT 7 */
426
427/* BEGIN MERGE SLOT 8 */
428
429/* END MERGE SLOT 8 */
430
431/* BEGIN MERGE SLOT 9 */
432
433/* END MERGE SLOT 9 */
434
435/* BEGIN MERGE SLOT 10 */
436
437/* END MERGE SLOT 10 */
438
Gabor Mezeif049dbf2022-07-18 23:02:33 +0200439#endif /* MBEDTLS_BIGNUM_MOD_H */