blob: 56e5cbc2ea9320e8f3cf99cf886b80e4ad2f630c [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
18 * of type #mbedtls_mpi_mod_modulus. The structure must be setup with an
19 * array of limbs storing the bignum value of the modulus. Unless otherwise
20 * specified, the modulus is called \p N and is input-only.
21 * - **Bignum parameters**: Bignums are passed as pointers to an array of
22 * limbs. A limb has the type #mbedtls_mpi_uint. Unless otherwise specified:
23 * - Bignum parameters called \p A, \p B, ... are inputs, and are not
24 * modified by the function.
25 * - Bignum parameters called \p X, \p Y are outputs or input-output.
26 * The initial content of output-only parameters is ignored.
27 * - \p T is a temporary storage area. The initial content of such
28 * parameter is ignored and the final content is unspecified.
29 * - **Bignum sizes**: bignum sizes are always expressed by the \p limbs
30 * member of the modulus argument. Any bignum parameters must have the same
31 * number of limbs as the modulus. All bignum sizes must be at least 1 and
32 * must be significantly less than #SIZE_MAX. The behavior if a size is 0 is
33 * undefined.
34 * - **Bignum representation**: the representation of inputs and outputs is
35 * specified by the \p int_rep field of the modulus for arithmetic
36 * functions. Utility functions may allow for different representation.
37 * - **Parameter ordering**: for bignum parameters, outputs come before inputs.
38 * Temporaries come last.
39 * - **Aliasing**: in general, output bignums may be aliased to one or more
40 * inputs. Modulus values may not be aliased to any other parameter. Outputs
41 * may not be aliased to one another. Temporaries may not be aliased to any
42 * other parameter.
43 * - **Overlap**: apart from aliasing of limb array pointers (where two
44 * arguments are equal pointers), overlap is not supported and may result
45 * in undefined behavior.
46 * - **Error handling**: This is a low-level module. Functions generally do not
47 * try to protect against invalid arguments such as nonsensical sizes or
48 * null pointers. Note that passing bignums with a different size than the
49 * modulus may lead to buffer overflows. Some functions which allocate
50 * memory or handle reading/writing of bignums will return an error if
51 * memory allocation fails or if buffer sizes are invalid.
52 * - **Modular representatives**: functions that operate modulo \p N expect
53 * all modular inputs to be in the range [0, \p N - 1] and guarantee outputs
54 * in the range [0, \p N - 1]. If an input is out of range, outputs are
55 * fully unspecified, though bignum values out of range should not cause
56 * buffer overflows (beware that this is not extensively tested).
Gilles Peskine7f887bd2022-09-27 13:12:30 +020057 */
58
59/*
Gabor Mezeic5328cf2022-07-18 23:13:13 +020060 * Copyright The Mbed TLS Contributors
61 * SPDX-License-Identifier: Apache-2.0
62 *
63 * Licensed under the Apache License, Version 2.0 (the "License"); you may
64 * not use this file except in compliance with the License.
65 * You may obtain a copy of the License at
66 *
67 * http://www.apache.org/licenses/LICENSE-2.0
68 *
69 * Unless required by applicable law or agreed to in writing, software
70 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
71 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
72 * See the License for the specific language governing permissions and
73 * limitations under the License.
74 */
75
Janos Follath5005edb2022-07-19 12:45:13 +010076#ifndef MBEDTLS_BIGNUM_MOD_RAW_H
77#define MBEDTLS_BIGNUM_MOD_RAW_H
Gabor Mezeic5328cf2022-07-18 23:13:13 +020078
79#include "common.h"
80
81#if defined(MBEDTLS_BIGNUM_C)
82#include "mbedtls/bignum.h"
83#endif
84
Janos Follath0ded6312022-08-09 13:34:54 +010085#include "bignum_mod.h"
86
Gabor Mezei12071d42022-09-12 16:35:58 +020087/**
Gabor Mezei4086de62022-10-14 16:29:42 +020088 * \brief Perform a safe conditional copy of an MPI which doesn't reveal
89 * whether the assignment was done or not.
Gabor Mezei12071d42022-09-12 16:35:58 +020090 *
Gabor Mezeidba26772022-10-03 17:01:02 +020091 * The size to copy is determined by \p N.
92 *
Gabor Mezei4086de62022-10-14 16:29:42 +020093 * \param[out] X The address of the destination MPI.
94 * This must be initialized. Must have enough limbs to
95 * store the full value of \p A.
96 * \param[in] A The address of the source MPI. This must be initialized.
Gabor Mezei86dfe382022-09-30 14:03:04 +020097 * \param[in] N The address of the modulus related to \p X and \p A.
Gabor Mezei12071d42022-09-12 16:35:58 +020098 * \param assign The condition deciding whether to perform the
99 * assignment or not. Must be either 0 or 1:
Gabor Mezei1c628d52022-09-27 12:13:51 +0200100 * * \c 1: Perform the assignment `X = A`.
Gabor Mezei12071d42022-09-12 16:35:58 +0200101 * * \c 0: Keep the original value of \p X.
102 *
103 * \note This function avoids leaking any information about whether
104 * the assignment was done or not.
105 *
106 * \warning If \p assign is neither 0 nor 1, the result of this function
107 * is indeterminate, and the resulting value in \p X might be
Gabor Mezei4086de62022-10-14 16:29:42 +0200108 * neither its original value nor the value in \p A.
Gabor Mezei12071d42022-09-12 16:35:58 +0200109 */
Gabor Mezei63c32822022-09-15 20:01:31 +0200110void mbedtls_mpi_mod_raw_cond_assign( mbedtls_mpi_uint *X,
Gabor Mezei1c628d52022-09-27 12:13:51 +0200111 const mbedtls_mpi_uint *A,
Gabor Mezeie5b85852022-09-30 13:54:02 +0200112 const mbedtls_mpi_mod_modulus *N,
Gabor Mezei63c32822022-09-15 20:01:31 +0200113 unsigned char assign );
Gabor Mezei12071d42022-09-12 16:35:58 +0200114
115/**
Gabor Mezei4086de62022-10-14 16:29:42 +0200116 * \brief Perform a safe conditional swap of two MPIs which doesn't reveal
117 * whether the swap was done or not.
Gabor Mezei12071d42022-09-12 16:35:58 +0200118 *
Gabor Mezeidba26772022-10-03 17:01:02 +0200119 * The size to swap is determined by \p N.
120 *
Gabor Mezei86dfe382022-09-30 14:03:04 +0200121 * \param[in,out] X The address of the first MPI. This must be initialized.
122 * \param[in,out] Y The address of the second MPI. This must be initialized.
123 * \param[in] N The address of the modulus related to \p X and \p Y.
Gabor Mezei12071d42022-09-12 16:35:58 +0200124 * \param swap The condition deciding whether to perform
125 * the swap or not. Must be either 0 or 1:
Gabor Mezeie5b85852022-09-30 13:54:02 +0200126 * * \c 1: Swap the values of \p X and \p Y.
127 * * \c 0: Keep the original values of \p X and \p Y.
Gabor Mezei12071d42022-09-12 16:35:58 +0200128 *
129 * \note This function avoids leaking any information about whether
130 * the swap was done or not.
131 *
132 * \warning If \p swap is neither 0 nor 1, the result of this function
Gabor Mezeie5b85852022-09-30 13:54:02 +0200133 * is indeterminate, and both \p X and \p Y might end up with
Gabor Mezei12071d42022-09-12 16:35:58 +0200134 * values different to either of the original ones.
Gabor Mezei63c32822022-09-15 20:01:31 +0200135 */
Gabor Mezeie5b85852022-09-30 13:54:02 +0200136void mbedtls_mpi_mod_raw_cond_swap( mbedtls_mpi_uint *X,
137 mbedtls_mpi_uint *Y,
138 const mbedtls_mpi_mod_modulus *N,
Gabor Mezei63c32822022-09-15 20:01:31 +0200139 unsigned char swap );
Gabor Mezei12071d42022-09-12 16:35:58 +0200140
Gabor Mezei37b06362022-08-02 17:22:18 +0200141/** Import X from unsigned binary data.
142 *
Janos Follath63184682022-08-11 17:42:59 +0100143 * The MPI needs to have enough limbs to store the full value (including any
144 * most significant zero bytes in the input).
Gabor Mezei37b06362022-08-02 17:22:18 +0200145 *
Janos Follathb7a88ec2022-08-19 12:24:40 +0100146 * \param[out] X The address of the MPI. The size is determined by \p m.
147 * (In particular, it must have at least as many limbs as
148 * the modulus \p m.)
149 * \param[in] m The address of the modulus related to \p X.
150 * \param[in] input The input buffer to import from.
151 * \param input_length The length in bytes of \p input.
Janos Follathd3eed332022-11-24 17:42:02 +0000152 * \param ext_rep The endianness of the number in the input buffer.
Gabor Mezei37b06362022-08-02 17:22:18 +0200153 *
154 * \return \c 0 if successful.
155 * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p X isn't
Janos Follathb7a88ec2022-08-19 12:24:40 +0100156 * large enough to hold the value in \p input.
Janos Follath8ff07292022-08-08 08:39:52 +0100157 * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if the external representation
Janos Follathdae11472022-08-08 11:50:02 +0100158 * of \p m is invalid or \p X is not less than \p m.
Gabor Mezei37b06362022-08-02 17:22:18 +0200159 */
Gabor Mezeic5328cf2022-07-18 23:13:13 +0200160int mbedtls_mpi_mod_raw_read( mbedtls_mpi_uint *X,
Janos Follath6b8a4ad2022-08-19 10:58:34 +0100161 const mbedtls_mpi_mod_modulus *m,
Janos Follathb7a88ec2022-08-19 12:24:40 +0100162 const unsigned char *input,
Janos Follathd3eed332022-11-24 17:42:02 +0000163 size_t input_length,
164 mbedtls_mpi_mod_ext_rep ext_rep );
Gabor Mezeic5328cf2022-07-18 23:13:13 +0200165
Janos Follathb7a88ec2022-08-19 12:24:40 +0100166/** Export A into unsigned binary data.
Gabor Mezei37b06362022-08-02 17:22:18 +0200167 *
Janos Follathb7a88ec2022-08-19 12:24:40 +0100168 * \param[in] A The address of the MPI. The size is determined by \p m.
169 * (In particular, it must have at least as many limbs as
170 * the modulus \p m.)
171 * \param[in] m The address of the modulus related to \p A.
172 * \param[out] output The output buffer to export to.
173 * \param output_length The length in bytes of \p output.
Janos Follathd3eed332022-11-24 17:42:02 +0000174 * \param ext_rep The endianness in which the number should be written into the output buffer.
Gabor Mezei37b06362022-08-02 17:22:18 +0200175 *
176 * \return \c 0 if successful.
Janos Follathb7a88ec2022-08-19 12:24:40 +0100177 * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p output isn't
178 * large enough to hold the value of \p A.
Janos Follath8ff07292022-08-08 08:39:52 +0100179 * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if the external representation
Gabor Mezei37b06362022-08-02 17:22:18 +0200180 * of \p m is invalid.
181 */
Janos Follathb7a88ec2022-08-19 12:24:40 +0100182int mbedtls_mpi_mod_raw_write( const mbedtls_mpi_uint *A,
Janos Follath6b8a4ad2022-08-19 10:58:34 +0100183 const mbedtls_mpi_mod_modulus *m,
Janos Follathb7a88ec2022-08-19 12:24:40 +0100184 unsigned char *output,
Janos Follathd3eed332022-11-24 17:42:02 +0000185 size_t output_length,
186 mbedtls_mpi_mod_ext_rep ext_rep );
Gabor Mezeic5328cf2022-07-18 23:13:13 +0200187
Janos Follath5933f692022-11-02 14:35:17 +0000188/* BEGIN MERGE SLOT 1 */
189
190/* END MERGE SLOT 1 */
191
192/* BEGIN MERGE SLOT 2 */
193
Gabor Mezei02d23132022-11-23 13:09:43 +0100194/** \brief Subtract two MPIs, returning the residue modulo the specified
195 * modulus.
Gabor Mezei4c7cf7d2022-11-09 14:07:43 +0100196 *
Gabor Mezei02d23132022-11-23 13:09:43 +0100197 * The size of the operation is determined by \p N. \p A and \p B must have
198 * the same number of limbs as \p N.
199 *
200 * \p X may be aliased to \p A or \p B, or even both, but may not overlap
201 * either otherwise.
Gabor Mezei4c7cf7d2022-11-09 14:07:43 +0100202 *
203 * \param[out] X The address of the result MPI.
204 * This must be initialized. Must have enough limbs to
205 * store the full value of the result.
206 * \param[in] A The address of the first MPI. This must be initialized.
207 * \param[in] B The address of the second MPI. This must be initialized.
Gabor Mezei02d23132022-11-23 13:09:43 +0100208 * \param[in] N The address of the modulus. Used to perform a modulo
Gabor Mezei4c7cf7d2022-11-09 14:07:43 +0100209 * operation on the result of the subtraction.
Gabor Mezei4c7cf7d2022-11-09 14:07:43 +0100210 */
211void mbedtls_mpi_mod_raw_sub( mbedtls_mpi_uint *X,
212 const mbedtls_mpi_uint *A,
213 const mbedtls_mpi_uint *B,
214 const mbedtls_mpi_mod_modulus *N );
215
Janos Follath5933f692022-11-02 14:35:17 +0000216/* END MERGE SLOT 2 */
217
218/* BEGIN MERGE SLOT 3 */
219
Tom Cosgrove61292682022-12-08 09:44:10 +0000220/**
221 * \brief Returns the number of limbs of working memory required for
222 * a call to `mbedtls_mpi_mod_raw_inv_prime()`.
223 *
224 * \param AN_limbs The number of limbs in the input `A` and the modulus `N`
225 * (they must be the same size) that will be given to
226 * `mbedtls_mpi_mod_raw_inv_prime()`.
227 *
228 * \return The number of limbs of working memory required by
229 * `mbedtls_mpi_mod_raw_inv_prime()`.
230 */
231size_t mbedtls_mpi_mod_raw_inv_prime_working_limbs( size_t AN_limbs );
232
233/**
234 * \brief Perform fixed-width modular inversion of a Montgomery-form MPI with
235 * respect to a modulus \p N that must be prime.
236 *
237 * \p X may be aliased to \p A, but not to \p N or \p RR.
238 *
239 * \param[out] X The modular inverse of \p A with respect to \p N.
240 * Will be in Montgomery form.
241 * \param[in] A The number to calculate the modular inverse of.
242 * Must be in Montgomery form. Must not be 0.
243 * \param[in] N The modulus, as a little-endian array of length \p AN_limbs.
244 * Must be prime.
245 * \param AN_limbs The number of limbs in \p A, \p N and \p RR.
246 * \param[in] RR The precomputed residue of 2^{2*biL} modulo N, as a little-
247 * endian array of length \p AN_limbs.
248 * \param[in,out] T Temporary storage of at least the number of limbs returned
249 * by `mbedtls_mpi_mod_raw_inv_prime_working_limbs()`.
250 * Its initial content is unused and its final content is
251 * indeterminate.
252 * It must not alias or otherwise overlap any of the other
253 * parameters.
254 * It is up to the caller to zeroize \p T when it is no
255 * longer needed, and before freeing it if it was dynamically
256 * allocated.
257 */
258void mbedtls_mpi_mod_raw_inv_prime( mbedtls_mpi_uint *X,
259 const mbedtls_mpi_uint *A,
260 const mbedtls_mpi_uint *N,
261 size_t AN_limbs,
262 const mbedtls_mpi_uint *RR,
263 mbedtls_mpi_uint *T );
264
Janos Follath5933f692022-11-02 14:35:17 +0000265/* END MERGE SLOT 3 */
266
267/* BEGIN MERGE SLOT 4 */
268
269/* END MERGE SLOT 4 */
270
271/* BEGIN MERGE SLOT 5 */
Hanno Beckera45b6fe2022-11-01 13:14:28 +0000272/**
273 * \brief Perform a known-size modular addition.
274 *
Tom Cosgroveabddad42022-11-24 15:54:16 +0000275 * Calculate `A + B modulo N`.
276 *
277 * The number of limbs in each operand, and the result, is given by the
278 * modulus \p N.
279 *
280 * \p X may be aliased to \p A or \p B, or even both, but may not overlap
281 * either otherwise.
Hanno Beckera45b6fe2022-11-01 13:14:28 +0000282 *
Werner Lewis0eea8272022-11-01 13:27:29 +0000283 * \param[out] X The result of the modular addition.
284 * \param[in] A Little-endian presentation of the left operand. This
Tom Cosgroveabddad42022-11-24 15:54:16 +0000285 * must be smaller than \p N.
Werner Lewis0eea8272022-11-01 13:27:29 +0000286 * \param[in] B Little-endian presentation of the right operand. This
Tom Cosgroveabddad42022-11-24 15:54:16 +0000287 * must be smaller than \p N.
Werner Lewis9fa91eb2022-11-01 13:36:51 +0000288 * \param[in] N The address of the modulus.
Hanno Beckera45b6fe2022-11-01 13:14:28 +0000289 */
Werner Lewis0eea8272022-11-01 13:27:29 +0000290void mbedtls_mpi_mod_raw_add( mbedtls_mpi_uint *X,
Werner Lewisd391b8c2022-11-08 15:53:47 +0000291 const mbedtls_mpi_uint *A,
292 const mbedtls_mpi_uint *B,
Werner Lewis9fa91eb2022-11-01 13:36:51 +0000293 const mbedtls_mpi_mod_modulus *N );
Janos Follath5933f692022-11-02 14:35:17 +0000294/* END MERGE SLOT 5 */
295
296/* BEGIN MERGE SLOT 6 */
297
298/* END MERGE SLOT 6 */
299
300/* BEGIN MERGE SLOT 7 */
Minos Galanakisd9299c32022-11-01 16:19:07 +0000301/** Convert an MPI into Montgomery form.
Hanno Becker5ad4a932022-08-09 14:45:53 +0100302 *
303 * \param X The address of the MPI.
Minos Galanakisd9299c32022-11-01 16:19:07 +0000304 * Must have the same number of limbs as \p m.
305 * \param m The address of the modulus, which gives the size of
306 * the base `R` = 2^(biL*m->limbs).
Hanno Becker5ad4a932022-08-09 14:45:53 +0100307 *
308 * \return \c 0 if successful.
309 */
Minos Galanakisd9299c32022-11-01 16:19:07 +0000310int mbedtls_mpi_mod_raw_to_mont_rep( mbedtls_mpi_uint *X,
311 const mbedtls_mpi_mod_modulus *m );
Janos Follath5933f692022-11-02 14:35:17 +0000312
Minos Galanakisd9299c32022-11-01 16:19:07 +0000313/** Convert an MPI back from Montgomery representation.
Hanno Becker5ad4a932022-08-09 14:45:53 +0100314 *
315 * \param X The address of the MPI.
Minos Galanakisd9299c32022-11-01 16:19:07 +0000316 * Must have the same number of limbs as \p m.
317 * \param m The address of the modulus, which gives the size of
318 * the base `R`= 2^(biL*m->limbs).
Hanno Becker5ad4a932022-08-09 14:45:53 +0100319 *
320 * \return \c 0 if successful.
321 */
Minos Galanakisd9299c32022-11-01 16:19:07 +0000322int mbedtls_mpi_mod_raw_from_mont_rep( mbedtls_mpi_uint *X,
323 const mbedtls_mpi_mod_modulus *m );
Janos Follath5933f692022-11-02 14:35:17 +0000324/* END MERGE SLOT 7 */
325
326/* BEGIN MERGE SLOT 8 */
327
328/* END MERGE SLOT 8 */
329
330/* BEGIN MERGE SLOT 9 */
331
332/* END MERGE SLOT 9 */
333
334/* BEGIN MERGE SLOT 10 */
335
336/* END MERGE SLOT 10 */
337
Janos Follath5005edb2022-07-19 12:45:13 +0100338#endif /* MBEDTLS_BIGNUM_MOD_RAW_H */