blob: 174cc6e56f38ce549dac2e6e0dca7e6b897b33bb [file] [log] [blame]
Gabor Mezeic5328cf2022-07-18 23:13:13 +02001/**
Janos Follatha95f2042022-08-19 12:09:17 +01002 * Low-level modular bignum functions
Janos Follath63184682022-08-11 17:42:59 +01003 *
Janos Follathaf3f39c2022-08-22 09:06:32 +01004 * This interface should only be used by the higher-level modular bignum
Janos Follath63184682022-08-11 17:42:59 +01005 * module (bignum_mod.c) and the ECP module (ecp.c, ecp_curves.c). All other
Janos Follathaf3f39c2022-08-22 09:06:32 +01006 * modules should use the high-level modular bignum interface (bignum_mod.h)
Janos Follath63184682022-08-11 17:42:59 +01007 * or the legacy bignum interface (bignum.h).
Gabor Mezeic5328cf2022-07-18 23:13:13 +02008 *
Gilles Peskine7aab2fb2022-09-27 13:19:13 +02009 * This is a low-level interface to operations on integers modulo which
10 * has no protection against passing invalid arguments such as arrays of
11 * the wrong size. The functions in bignum_mod.h provide a higher-level
12 * interface that includes protections against accidental misuse, at the
13 * expense of code size and sometimes more cumbersome memory management.
Werner Lewis5e9d2e92022-12-12 14:00:25 +000014 *
15 * The functions in this module obey the following conventions unless
16 * explicitly indicated otherwise:
17 * - **Modulus parameters**: the modulus is passed as a pointer to a structure
Werner Lewise1eb75d2022-12-14 13:45:49 +000018 * of type #mbedtls_mpi_mod_modulus. The structure must be set up with an
19 * array of limbs storing the bignum value of the modulus. The modulus must
20 * be odd and is assumed to have no leading zeroes. The modulus is usually
21 * named \p N and is usually input-only.
Werner Lewis5e9d2e92022-12-12 14:00:25 +000022 * - **Bignum parameters**: Bignums are passed as pointers to an array of
23 * limbs. A limb has the type #mbedtls_mpi_uint. Unless otherwise specified:
24 * - Bignum parameters called \p A, \p B, ... are inputs, and are not
25 * modified by the function.
26 * - Bignum parameters called \p X, \p Y are outputs or input-output.
27 * The initial content of output-only parameters is ignored.
Werner Lewis2bd263d2022-12-14 15:32:31 +000028 * - \p T is a temporary storage area. The initial content of such a
Werner Lewis5e9d2e92022-12-12 14:00:25 +000029 * parameter is ignored and the final content is unspecified.
Werner Lewis2e70b9a2022-12-14 15:48:31 +000030 * - **Bignum sizes**: bignum sizes are usually expressed by the \c limbs
31 * member of the modulus argument. All bignum parameters must have the same
Werner Lewis5e9d2e92022-12-12 14:00:25 +000032 * number of limbs as the modulus. All bignum sizes must be at least 1 and
33 * must be significantly less than #SIZE_MAX. The behavior if a size is 0 is
34 * undefined.
35 * - **Bignum representation**: the representation of inputs and outputs is
36 * specified by the \p int_rep field of the modulus for arithmetic
37 * functions. Utility functions may allow for different representation.
38 * - **Parameter ordering**: for bignum parameters, outputs come before inputs.
Werner Lewisa3068862022-12-14 15:57:12 +000039 * The modulus is passed after other bignum input parameters. Temporaries
40 * come last.
Werner Lewis5e9d2e92022-12-12 14:00:25 +000041 * - **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 limb array pointers (where two
46 * arguments are equal pointers), overlap is not supported and may result
47 * in undefined behavior.
48 * - **Error handling**: This is a low-level module. Functions generally do not
49 * try to protect against invalid arguments such as nonsensical sizes or
50 * null pointers. Note that passing bignums with a different size than the
51 * modulus may lead to buffer overflows. Some functions which allocate
52 * memory or handle reading/writing of bignums will return an error if
53 * memory allocation fails or if buffer sizes are invalid.
Werner Lewis1d89ebf2022-12-14 17:08:43 +000054 * - **Modular representatives**: all functions expect inputs to be in the
55 * range [0, \p N - 1] and guarantee outputs in the range [0, \p N - 1]. If
56 * an input is out of range, outputs are fully unspecified, though bignum
57 * values out of range should not cause buffer overflows (beware that this is
58 * not extensively tested).
Gilles Peskine7f887bd2022-09-27 13:12:30 +020059 */
60
61/*
Gabor Mezeic5328cf2022-07-18 23:13:13 +020062 * Copyright The Mbed TLS Contributors
63 * SPDX-License-Identifier: Apache-2.0
64 *
65 * Licensed under the Apache License, Version 2.0 (the "License"); you may
66 * not use this file except in compliance with the License.
67 * You may obtain a copy of the License at
68 *
69 * http://www.apache.org/licenses/LICENSE-2.0
70 *
71 * Unless required by applicable law or agreed to in writing, software
72 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
73 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
74 * See the License for the specific language governing permissions and
75 * limitations under the License.
76 */
77
Janos Follath5005edb2022-07-19 12:45:13 +010078#ifndef MBEDTLS_BIGNUM_MOD_RAW_H
79#define MBEDTLS_BIGNUM_MOD_RAW_H
Gabor Mezeic5328cf2022-07-18 23:13:13 +020080
81#include "common.h"
82
83#if defined(MBEDTLS_BIGNUM_C)
84#include "mbedtls/bignum.h"
85#endif
86
Janos Follath0ded6312022-08-09 13:34:54 +010087#include "bignum_mod.h"
88
Gabor Mezei12071d42022-09-12 16:35:58 +020089/**
Gabor Mezei4086de62022-10-14 16:29:42 +020090 * \brief Perform a safe conditional copy of an MPI which doesn't reveal
91 * whether the assignment was done or not.
Gabor Mezei12071d42022-09-12 16:35:58 +020092 *
Gabor Mezeidba26772022-10-03 17:01:02 +020093 * The size to copy is determined by \p N.
94 *
Gabor Mezei4086de62022-10-14 16:29:42 +020095 * \param[out] X The address of the destination MPI.
96 * This must be initialized. Must have enough limbs to
97 * store the full value of \p A.
98 * \param[in] A The address of the source MPI. This must be initialized.
Gabor Mezei86dfe382022-09-30 14:03:04 +020099 * \param[in] N The address of the modulus related to \p X and \p A.
Gabor Mezei12071d42022-09-12 16:35:58 +0200100 * \param assign The condition deciding whether to perform the
101 * assignment or not. Must be either 0 or 1:
Gabor Mezei1c628d52022-09-27 12:13:51 +0200102 * * \c 1: Perform the assignment `X = A`.
Gabor Mezei12071d42022-09-12 16:35:58 +0200103 * * \c 0: Keep the original value of \p X.
104 *
105 * \note This function avoids leaking any information about whether
106 * the assignment was done or not.
107 *
108 * \warning If \p assign is neither 0 nor 1, the result of this function
109 * is indeterminate, and the resulting value in \p X might be
Gabor Mezei4086de62022-10-14 16:29:42 +0200110 * neither its original value nor the value in \p A.
Gabor Mezei12071d42022-09-12 16:35:58 +0200111 */
Gabor Mezei63c32822022-09-15 20:01:31 +0200112void mbedtls_mpi_mod_raw_cond_assign( mbedtls_mpi_uint *X,
Gabor Mezei1c628d52022-09-27 12:13:51 +0200113 const mbedtls_mpi_uint *A,
Gabor Mezeie5b85852022-09-30 13:54:02 +0200114 const mbedtls_mpi_mod_modulus *N,
Gabor Mezei63c32822022-09-15 20:01:31 +0200115 unsigned char assign );
Gabor Mezei12071d42022-09-12 16:35:58 +0200116
117/**
Gabor Mezei4086de62022-10-14 16:29:42 +0200118 * \brief Perform a safe conditional swap of two MPIs which doesn't reveal
119 * whether the swap was done or not.
Gabor Mezei12071d42022-09-12 16:35:58 +0200120 *
Gabor Mezeidba26772022-10-03 17:01:02 +0200121 * The size to swap is determined by \p N.
122 *
Gabor Mezei86dfe382022-09-30 14:03:04 +0200123 * \param[in,out] X The address of the first MPI. This must be initialized.
124 * \param[in,out] Y The address of the second MPI. This must be initialized.
125 * \param[in] N The address of the modulus related to \p X and \p Y.
Gabor Mezei12071d42022-09-12 16:35:58 +0200126 * \param swap The condition deciding whether to perform
127 * the swap or not. Must be either 0 or 1:
Gabor Mezeie5b85852022-09-30 13:54:02 +0200128 * * \c 1: Swap the values of \p X and \p Y.
129 * * \c 0: Keep the original values of \p X and \p Y.
Gabor Mezei12071d42022-09-12 16:35:58 +0200130 *
131 * \note This function avoids leaking any information about whether
132 * the swap was done or not.
133 *
134 * \warning If \p swap is neither 0 nor 1, the result of this function
Gabor Mezeie5b85852022-09-30 13:54:02 +0200135 * is indeterminate, and both \p X and \p Y might end up with
Gabor Mezei12071d42022-09-12 16:35:58 +0200136 * values different to either of the original ones.
Gabor Mezei63c32822022-09-15 20:01:31 +0200137 */
Gabor Mezeie5b85852022-09-30 13:54:02 +0200138void mbedtls_mpi_mod_raw_cond_swap( mbedtls_mpi_uint *X,
139 mbedtls_mpi_uint *Y,
140 const mbedtls_mpi_mod_modulus *N,
Gabor Mezei63c32822022-09-15 20:01:31 +0200141 unsigned char swap );
Gabor Mezei12071d42022-09-12 16:35:58 +0200142
Gabor Mezei37b06362022-08-02 17:22:18 +0200143/** Import X from unsigned binary data.
144 *
Janos Follath63184682022-08-11 17:42:59 +0100145 * The MPI needs to have enough limbs to store the full value (including any
146 * most significant zero bytes in the input).
Gabor Mezei37b06362022-08-02 17:22:18 +0200147 *
Janos Follathb7a88ec2022-08-19 12:24:40 +0100148 * \param[out] X The address of the MPI. The size is determined by \p m.
149 * (In particular, it must have at least as many limbs as
150 * the modulus \p m.)
151 * \param[in] m The address of the modulus related to \p X.
152 * \param[in] input The input buffer to import from.
153 * \param input_length The length in bytes of \p input.
Janos Follathd3eed332022-11-24 17:42:02 +0000154 * \param ext_rep The endianness of the number in the input buffer.
Gabor Mezei37b06362022-08-02 17:22:18 +0200155 *
156 * \return \c 0 if successful.
157 * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p X isn't
Janos Follathb7a88ec2022-08-19 12:24:40 +0100158 * large enough to hold the value in \p input.
Janos Follath8ff07292022-08-08 08:39:52 +0100159 * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if the external representation
Janos Follathdae11472022-08-08 11:50:02 +0100160 * of \p m is invalid or \p X is not less than \p m.
Gabor Mezei37b06362022-08-02 17:22:18 +0200161 */
Gabor Mezeic5328cf2022-07-18 23:13:13 +0200162int mbedtls_mpi_mod_raw_read( mbedtls_mpi_uint *X,
Janos Follath6b8a4ad2022-08-19 10:58:34 +0100163 const mbedtls_mpi_mod_modulus *m,
Janos Follathb7a88ec2022-08-19 12:24:40 +0100164 const unsigned char *input,
Janos Follathd3eed332022-11-24 17:42:02 +0000165 size_t input_length,
166 mbedtls_mpi_mod_ext_rep ext_rep );
Gabor Mezeic5328cf2022-07-18 23:13:13 +0200167
Janos Follathb7a88ec2022-08-19 12:24:40 +0100168/** Export A into unsigned binary data.
Gabor Mezei37b06362022-08-02 17:22:18 +0200169 *
Janos Follathb7a88ec2022-08-19 12:24:40 +0100170 * \param[in] A The address of the MPI. The size is determined by \p m.
171 * (In particular, it must have at least as many limbs as
172 * the modulus \p m.)
173 * \param[in] m The address of the modulus related to \p A.
174 * \param[out] output The output buffer to export to.
175 * \param output_length The length in bytes of \p output.
Janos Follathd3eed332022-11-24 17:42:02 +0000176 * \param ext_rep The endianness in which the number should be written into the output buffer.
Gabor Mezei37b06362022-08-02 17:22:18 +0200177 *
178 * \return \c 0 if successful.
Janos Follathb7a88ec2022-08-19 12:24:40 +0100179 * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p output isn't
180 * large enough to hold the value of \p A.
Janos Follath8ff07292022-08-08 08:39:52 +0100181 * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if the external representation
Gabor Mezei37b06362022-08-02 17:22:18 +0200182 * of \p m is invalid.
183 */
Janos Follathb7a88ec2022-08-19 12:24:40 +0100184int mbedtls_mpi_mod_raw_write( const mbedtls_mpi_uint *A,
Janos Follath6b8a4ad2022-08-19 10:58:34 +0100185 const mbedtls_mpi_mod_modulus *m,
Janos Follathb7a88ec2022-08-19 12:24:40 +0100186 unsigned char *output,
Janos Follathd3eed332022-11-24 17:42:02 +0000187 size_t output_length,
188 mbedtls_mpi_mod_ext_rep ext_rep );
Gabor Mezeic5328cf2022-07-18 23:13:13 +0200189
Janos Follath5933f692022-11-02 14:35:17 +0000190/* BEGIN MERGE SLOT 1 */
191
192/* END MERGE SLOT 1 */
193
194/* BEGIN MERGE SLOT 2 */
195
Gabor Mezei02d23132022-11-23 13:09:43 +0100196/** \brief Subtract two MPIs, returning the residue modulo the specified
197 * modulus.
Gabor Mezei4c7cf7d2022-11-09 14:07:43 +0100198 *
Gabor Mezei02d23132022-11-23 13:09:43 +0100199 * The size of the operation is determined by \p N. \p A and \p B must have
200 * the same number of limbs as \p N.
201 *
202 * \p X may be aliased to \p A or \p B, or even both, but may not overlap
203 * either otherwise.
Gabor Mezei4c7cf7d2022-11-09 14:07:43 +0100204 *
205 * \param[out] X The address of the result MPI.
206 * This must be initialized. Must have enough limbs to
207 * store the full value of the result.
208 * \param[in] A The address of the first MPI. This must be initialized.
209 * \param[in] B The address of the second MPI. This must be initialized.
Gabor Mezei02d23132022-11-23 13:09:43 +0100210 * \param[in] N The address of the modulus. Used to perform a modulo
Gabor Mezei4c7cf7d2022-11-09 14:07:43 +0100211 * operation on the result of the subtraction.
Gabor Mezei4c7cf7d2022-11-09 14:07:43 +0100212 */
213void mbedtls_mpi_mod_raw_sub( mbedtls_mpi_uint *X,
214 const mbedtls_mpi_uint *A,
215 const mbedtls_mpi_uint *B,
216 const mbedtls_mpi_mod_modulus *N );
217
Janos Follath5933f692022-11-02 14:35:17 +0000218/* END MERGE SLOT 2 */
219
220/* BEGIN MERGE SLOT 3 */
221
Tom Cosgrove61292682022-12-08 09:44:10 +0000222/**
223 * \brief Returns the number of limbs of working memory required for
224 * a call to `mbedtls_mpi_mod_raw_inv_prime()`.
225 *
226 * \param AN_limbs The number of limbs in the input `A` and the modulus `N`
227 * (they must be the same size) that will be given to
228 * `mbedtls_mpi_mod_raw_inv_prime()`.
229 *
230 * \return The number of limbs of working memory required by
231 * `mbedtls_mpi_mod_raw_inv_prime()`.
232 */
233size_t mbedtls_mpi_mod_raw_inv_prime_working_limbs( size_t AN_limbs );
234
235/**
236 * \brief Perform fixed-width modular inversion of a Montgomery-form MPI with
237 * respect to a modulus \p N that must be prime.
238 *
239 * \p X may be aliased to \p A, but not to \p N or \p RR.
240 *
241 * \param[out] X The modular inverse of \p A with respect to \p N.
242 * Will be in Montgomery form.
243 * \param[in] A The number to calculate the modular inverse of.
244 * Must be in Montgomery form. Must not be 0.
245 * \param[in] N The modulus, as a little-endian array of length \p AN_limbs.
246 * Must be prime.
247 * \param AN_limbs The number of limbs in \p A, \p N and \p RR.
248 * \param[in] RR The precomputed residue of 2^{2*biL} modulo N, as a little-
249 * endian array of length \p AN_limbs.
250 * \param[in,out] T Temporary storage of at least the number of limbs returned
251 * by `mbedtls_mpi_mod_raw_inv_prime_working_limbs()`.
252 * Its initial content is unused and its final content is
253 * indeterminate.
254 * It must not alias or otherwise overlap any of the other
255 * parameters.
256 * It is up to the caller to zeroize \p T when it is no
257 * longer needed, and before freeing it if it was dynamically
258 * allocated.
259 */
260void mbedtls_mpi_mod_raw_inv_prime( mbedtls_mpi_uint *X,
261 const mbedtls_mpi_uint *A,
262 const mbedtls_mpi_uint *N,
263 size_t AN_limbs,
264 const mbedtls_mpi_uint *RR,
265 mbedtls_mpi_uint *T );
266
Janos Follath5933f692022-11-02 14:35:17 +0000267/* END MERGE SLOT 3 */
268
269/* BEGIN MERGE SLOT 4 */
270
271/* END MERGE SLOT 4 */
272
273/* BEGIN MERGE SLOT 5 */
Hanno Beckera45b6fe2022-11-01 13:14:28 +0000274/**
275 * \brief Perform a known-size modular addition.
276 *
Tom Cosgroveabddad42022-11-24 15:54:16 +0000277 * Calculate `A + B modulo N`.
278 *
279 * The number of limbs in each operand, and the result, is given by the
280 * modulus \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.
Hanno Beckera45b6fe2022-11-01 13:14:28 +0000284 *
Werner Lewis0eea8272022-11-01 13:27:29 +0000285 * \param[out] X The result of the modular addition.
286 * \param[in] A Little-endian presentation of the left operand. This
Tom Cosgroveabddad42022-11-24 15:54:16 +0000287 * must be smaller than \p N.
Werner Lewis0eea8272022-11-01 13:27:29 +0000288 * \param[in] B Little-endian presentation of the right operand. This
Tom Cosgroveabddad42022-11-24 15:54:16 +0000289 * must be smaller than \p N.
Werner Lewis9fa91eb2022-11-01 13:36:51 +0000290 * \param[in] N The address of the modulus.
Hanno Beckera45b6fe2022-11-01 13:14:28 +0000291 */
Werner Lewis0eea8272022-11-01 13:27:29 +0000292void mbedtls_mpi_mod_raw_add( mbedtls_mpi_uint *X,
Werner Lewisd391b8c2022-11-08 15:53:47 +0000293 const mbedtls_mpi_uint *A,
294 const mbedtls_mpi_uint *B,
Werner Lewis9fa91eb2022-11-01 13:36:51 +0000295 const mbedtls_mpi_mod_modulus *N );
Janos Follath5933f692022-11-02 14:35:17 +0000296/* END MERGE SLOT 5 */
297
298/* BEGIN MERGE SLOT 6 */
299
300/* END MERGE SLOT 6 */
301
302/* BEGIN MERGE SLOT 7 */
Minos Galanakisd9299c32022-11-01 16:19:07 +0000303/** Convert an MPI into Montgomery form.
Hanno Becker5ad4a932022-08-09 14:45:53 +0100304 *
305 * \param X The address of the MPI.
Minos Galanakisd9299c32022-11-01 16:19:07 +0000306 * Must have the same number of limbs as \p m.
307 * \param m The address of the modulus, which gives the size of
308 * the base `R` = 2^(biL*m->limbs).
Hanno Becker5ad4a932022-08-09 14:45:53 +0100309 *
310 * \return \c 0 if successful.
311 */
Minos Galanakisd9299c32022-11-01 16:19:07 +0000312int mbedtls_mpi_mod_raw_to_mont_rep( mbedtls_mpi_uint *X,
313 const mbedtls_mpi_mod_modulus *m );
Janos Follath5933f692022-11-02 14:35:17 +0000314
Minos Galanakisd9299c32022-11-01 16:19:07 +0000315/** Convert an MPI back from Montgomery representation.
Hanno Becker5ad4a932022-08-09 14:45:53 +0100316 *
317 * \param X The address of the MPI.
Minos Galanakisd9299c32022-11-01 16:19:07 +0000318 * Must have the same number of limbs as \p m.
319 * \param m The address of the modulus, which gives the size of
320 * the base `R`= 2^(biL*m->limbs).
Hanno Becker5ad4a932022-08-09 14:45:53 +0100321 *
322 * \return \c 0 if successful.
323 */
Minos Galanakisd9299c32022-11-01 16:19:07 +0000324int mbedtls_mpi_mod_raw_from_mont_rep( mbedtls_mpi_uint *X,
325 const mbedtls_mpi_mod_modulus *m );
Janos Follath5933f692022-11-02 14:35:17 +0000326/* END MERGE SLOT 7 */
327
328/* BEGIN MERGE SLOT 8 */
329
330/* END MERGE SLOT 8 */
331
332/* BEGIN MERGE SLOT 9 */
333
334/* END MERGE SLOT 9 */
335
336/* BEGIN MERGE SLOT 10 */
337
338/* END MERGE SLOT 10 */
339
Janos Follath5005edb2022-07-19 12:45:13 +0100340#endif /* MBEDTLS_BIGNUM_MOD_RAW_H */