blob: bcdeaad6d235719b83cbd18fbae865c58adddb41 [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.
30 * - **Bignum sizes**: bignum sizes are always expressed by the \p limbs
31 * member of the modulus argument. Any bignum parameters must have the same
32 * 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.
39 * Temporaries come last.
40 * - **Aliasing**: in general, output bignums may be aliased to one or more
41 * inputs. Modulus values may not be aliased to any other parameter. Outputs
42 * may not be aliased to one another. Temporaries may not be aliased to any
43 * other parameter.
44 * - **Overlap**: apart from aliasing of limb array pointers (where two
45 * arguments are equal pointers), overlap is not supported and may result
46 * in undefined behavior.
47 * - **Error handling**: This is a low-level module. Functions generally do not
48 * try to protect against invalid arguments such as nonsensical sizes or
49 * null pointers. Note that passing bignums with a different size than the
50 * modulus may lead to buffer overflows. Some functions which allocate
51 * memory or handle reading/writing of bignums will return an error if
52 * memory allocation fails or if buffer sizes are invalid.
53 * - **Modular representatives**: functions that operate modulo \p N expect
54 * all modular inputs to be in the range [0, \p N - 1] and guarantee outputs
55 * in the range [0, \p N - 1]. If an input is out of range, outputs are
56 * fully unspecified, though bignum values out of range should not cause
57 * buffer overflows (beware that this is not extensively tested).
Gilles Peskine7f887bd2022-09-27 13:12:30 +020058 */
59
60/*
Gabor Mezeic5328cf2022-07-18 23:13:13 +020061 * Copyright The Mbed TLS Contributors
62 * SPDX-License-Identifier: Apache-2.0
63 *
64 * Licensed under the Apache License, Version 2.0 (the "License"); you may
65 * not use this file except in compliance with the License.
66 * You may obtain a copy of the License at
67 *
68 * http://www.apache.org/licenses/LICENSE-2.0
69 *
70 * Unless required by applicable law or agreed to in writing, software
71 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
72 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
73 * See the License for the specific language governing permissions and
74 * limitations under the License.
75 */
76
Janos Follath5005edb2022-07-19 12:45:13 +010077#ifndef MBEDTLS_BIGNUM_MOD_RAW_H
78#define MBEDTLS_BIGNUM_MOD_RAW_H
Gabor Mezeic5328cf2022-07-18 23:13:13 +020079
80#include "common.h"
81
82#if defined(MBEDTLS_BIGNUM_C)
83#include "mbedtls/bignum.h"
84#endif
85
Janos Follath0ded6312022-08-09 13:34:54 +010086#include "bignum_mod.h"
87
Gabor Mezei12071d42022-09-12 16:35:58 +020088/**
Gabor Mezei4086de62022-10-14 16:29:42 +020089 * \brief Perform a safe conditional copy of an MPI which doesn't reveal
90 * whether the assignment was done or not.
Gabor Mezei12071d42022-09-12 16:35:58 +020091 *
Gabor Mezeidba26772022-10-03 17:01:02 +020092 * The size to copy is determined by \p N.
93 *
Gabor Mezei4086de62022-10-14 16:29:42 +020094 * \param[out] X The address of the destination MPI.
95 * This must be initialized. Must have enough limbs to
96 * store the full value of \p A.
97 * \param[in] A The address of the source MPI. This must be initialized.
Gabor Mezei86dfe382022-09-30 14:03:04 +020098 * \param[in] N The address of the modulus related to \p X and \p A.
Gabor Mezei12071d42022-09-12 16:35:58 +020099 * \param assign The condition deciding whether to perform the
100 * assignment or not. Must be either 0 or 1:
Gabor Mezei1c628d52022-09-27 12:13:51 +0200101 * * \c 1: Perform the assignment `X = A`.
Gabor Mezei12071d42022-09-12 16:35:58 +0200102 * * \c 0: Keep the original value of \p X.
103 *
104 * \note This function avoids leaking any information about whether
105 * the assignment was done or not.
106 *
107 * \warning If \p assign is neither 0 nor 1, the result of this function
108 * is indeterminate, and the resulting value in \p X might be
Gabor Mezei4086de62022-10-14 16:29:42 +0200109 * neither its original value nor the value in \p A.
Gabor Mezei12071d42022-09-12 16:35:58 +0200110 */
Gabor Mezei63c32822022-09-15 20:01:31 +0200111void mbedtls_mpi_mod_raw_cond_assign( mbedtls_mpi_uint *X,
Gabor Mezei1c628d52022-09-27 12:13:51 +0200112 const mbedtls_mpi_uint *A,
Gabor Mezeie5b85852022-09-30 13:54:02 +0200113 const mbedtls_mpi_mod_modulus *N,
Gabor Mezei63c32822022-09-15 20:01:31 +0200114 unsigned char assign );
Gabor Mezei12071d42022-09-12 16:35:58 +0200115
116/**
Gabor Mezei4086de62022-10-14 16:29:42 +0200117 * \brief Perform a safe conditional swap of two MPIs which doesn't reveal
118 * whether the swap was done or not.
Gabor Mezei12071d42022-09-12 16:35:58 +0200119 *
Gabor Mezeidba26772022-10-03 17:01:02 +0200120 * The size to swap is determined by \p N.
121 *
Gabor Mezei86dfe382022-09-30 14:03:04 +0200122 * \param[in,out] X The address of the first MPI. This must be initialized.
123 * \param[in,out] Y The address of the second MPI. This must be initialized.
124 * \param[in] N The address of the modulus related to \p X and \p Y.
Gabor Mezei12071d42022-09-12 16:35:58 +0200125 * \param swap The condition deciding whether to perform
126 * the swap or not. Must be either 0 or 1:
Gabor Mezeie5b85852022-09-30 13:54:02 +0200127 * * \c 1: Swap the values of \p X and \p Y.
128 * * \c 0: Keep the original values of \p X and \p Y.
Gabor Mezei12071d42022-09-12 16:35:58 +0200129 *
130 * \note This function avoids leaking any information about whether
131 * the swap was done or not.
132 *
133 * \warning If \p swap is neither 0 nor 1, the result of this function
Gabor Mezeie5b85852022-09-30 13:54:02 +0200134 * is indeterminate, and both \p X and \p Y might end up with
Gabor Mezei12071d42022-09-12 16:35:58 +0200135 * values different to either of the original ones.
Gabor Mezei63c32822022-09-15 20:01:31 +0200136 */
Gabor Mezeie5b85852022-09-30 13:54:02 +0200137void mbedtls_mpi_mod_raw_cond_swap( mbedtls_mpi_uint *X,
138 mbedtls_mpi_uint *Y,
139 const mbedtls_mpi_mod_modulus *N,
Gabor Mezei63c32822022-09-15 20:01:31 +0200140 unsigned char swap );
Gabor Mezei12071d42022-09-12 16:35:58 +0200141
Gabor Mezei37b06362022-08-02 17:22:18 +0200142/** Import X from unsigned binary data.
143 *
Janos Follath63184682022-08-11 17:42:59 +0100144 * The MPI needs to have enough limbs to store the full value (including any
145 * most significant zero bytes in the input).
Gabor Mezei37b06362022-08-02 17:22:18 +0200146 *
Janos Follathb7a88ec2022-08-19 12:24:40 +0100147 * \param[out] X The address of the MPI. The size is determined by \p m.
148 * (In particular, it must have at least as many limbs as
149 * the modulus \p m.)
150 * \param[in] m The address of the modulus related to \p X.
151 * \param[in] input The input buffer to import from.
152 * \param input_length The length in bytes of \p input.
Janos Follathd3eed332022-11-24 17:42:02 +0000153 * \param ext_rep The endianness of the number in the input buffer.
Gabor Mezei37b06362022-08-02 17:22:18 +0200154 *
155 * \return \c 0 if successful.
156 * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p X isn't
Janos Follathb7a88ec2022-08-19 12:24:40 +0100157 * large enough to hold the value in \p input.
Janos Follath8ff07292022-08-08 08:39:52 +0100158 * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if the external representation
Janos Follathdae11472022-08-08 11:50:02 +0100159 * of \p m is invalid or \p X is not less than \p m.
Gabor Mezei37b06362022-08-02 17:22:18 +0200160 */
Gabor Mezeic5328cf2022-07-18 23:13:13 +0200161int mbedtls_mpi_mod_raw_read( mbedtls_mpi_uint *X,
Janos Follath6b8a4ad2022-08-19 10:58:34 +0100162 const mbedtls_mpi_mod_modulus *m,
Janos Follathb7a88ec2022-08-19 12:24:40 +0100163 const unsigned char *input,
Janos Follathd3eed332022-11-24 17:42:02 +0000164 size_t input_length,
165 mbedtls_mpi_mod_ext_rep ext_rep );
Gabor Mezeic5328cf2022-07-18 23:13:13 +0200166
Janos Follathb7a88ec2022-08-19 12:24:40 +0100167/** Export A into unsigned binary data.
Gabor Mezei37b06362022-08-02 17:22:18 +0200168 *
Janos Follathb7a88ec2022-08-19 12:24:40 +0100169 * \param[in] A The address of the MPI. The size is determined by \p m.
170 * (In particular, it must have at least as many limbs as
171 * the modulus \p m.)
172 * \param[in] m The address of the modulus related to \p A.
173 * \param[out] output The output buffer to export to.
174 * \param output_length The length in bytes of \p output.
Janos Follathd3eed332022-11-24 17:42:02 +0000175 * \param ext_rep The endianness in which the number should be written into the output buffer.
Gabor Mezei37b06362022-08-02 17:22:18 +0200176 *
177 * \return \c 0 if successful.
Janos Follathb7a88ec2022-08-19 12:24:40 +0100178 * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p output isn't
179 * large enough to hold the value of \p A.
Janos Follath8ff07292022-08-08 08:39:52 +0100180 * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if the external representation
Gabor Mezei37b06362022-08-02 17:22:18 +0200181 * of \p m is invalid.
182 */
Janos Follathb7a88ec2022-08-19 12:24:40 +0100183int mbedtls_mpi_mod_raw_write( const mbedtls_mpi_uint *A,
Janos Follath6b8a4ad2022-08-19 10:58:34 +0100184 const mbedtls_mpi_mod_modulus *m,
Janos Follathb7a88ec2022-08-19 12:24:40 +0100185 unsigned char *output,
Janos Follathd3eed332022-11-24 17:42:02 +0000186 size_t output_length,
187 mbedtls_mpi_mod_ext_rep ext_rep );
Gabor Mezeic5328cf2022-07-18 23:13:13 +0200188
Janos Follath5933f692022-11-02 14:35:17 +0000189/* BEGIN MERGE SLOT 1 */
190
191/* END MERGE SLOT 1 */
192
193/* BEGIN MERGE SLOT 2 */
194
Gabor Mezei02d23132022-11-23 13:09:43 +0100195/** \brief Subtract two MPIs, returning the residue modulo the specified
196 * modulus.
Gabor Mezei4c7cf7d2022-11-09 14:07:43 +0100197 *
Gabor Mezei02d23132022-11-23 13:09:43 +0100198 * The size of the operation is determined by \p N. \p A and \p B must have
199 * the same number of limbs as \p N.
200 *
201 * \p X may be aliased to \p A or \p B, or even both, but may not overlap
202 * either otherwise.
Gabor Mezei4c7cf7d2022-11-09 14:07:43 +0100203 *
204 * \param[out] X The address of the result MPI.
205 * This must be initialized. Must have enough limbs to
206 * store the full value of the result.
207 * \param[in] A The address of the first MPI. This must be initialized.
208 * \param[in] B The address of the second MPI. This must be initialized.
Gabor Mezei02d23132022-11-23 13:09:43 +0100209 * \param[in] N The address of the modulus. Used to perform a modulo
Gabor Mezei4c7cf7d2022-11-09 14:07:43 +0100210 * operation on the result of the subtraction.
Gabor Mezei4c7cf7d2022-11-09 14:07:43 +0100211 */
212void mbedtls_mpi_mod_raw_sub( mbedtls_mpi_uint *X,
213 const mbedtls_mpi_uint *A,
214 const mbedtls_mpi_uint *B,
215 const mbedtls_mpi_mod_modulus *N );
216
Janos Follath5933f692022-11-02 14:35:17 +0000217/* END MERGE SLOT 2 */
218
219/* BEGIN MERGE SLOT 3 */
220
Tom Cosgrove61292682022-12-08 09:44:10 +0000221/**
222 * \brief Returns the number of limbs of working memory required for
223 * a call to `mbedtls_mpi_mod_raw_inv_prime()`.
224 *
225 * \param AN_limbs The number of limbs in the input `A` and the modulus `N`
226 * (they must be the same size) that will be given to
227 * `mbedtls_mpi_mod_raw_inv_prime()`.
228 *
229 * \return The number of limbs of working memory required by
230 * `mbedtls_mpi_mod_raw_inv_prime()`.
231 */
232size_t mbedtls_mpi_mod_raw_inv_prime_working_limbs( size_t AN_limbs );
233
234/**
235 * \brief Perform fixed-width modular inversion of a Montgomery-form MPI with
236 * respect to a modulus \p N that must be prime.
237 *
238 * \p X may be aliased to \p A, but not to \p N or \p RR.
239 *
240 * \param[out] X The modular inverse of \p A with respect to \p N.
241 * Will be in Montgomery form.
242 * \param[in] A The number to calculate the modular inverse of.
243 * Must be in Montgomery form. Must not be 0.
244 * \param[in] N The modulus, as a little-endian array of length \p AN_limbs.
245 * Must be prime.
246 * \param AN_limbs The number of limbs in \p A, \p N and \p RR.
247 * \param[in] RR The precomputed residue of 2^{2*biL} modulo N, as a little-
248 * endian array of length \p AN_limbs.
249 * \param[in,out] T Temporary storage of at least the number of limbs returned
250 * by `mbedtls_mpi_mod_raw_inv_prime_working_limbs()`.
251 * Its initial content is unused and its final content is
252 * indeterminate.
253 * It must not alias or otherwise overlap any of the other
254 * parameters.
255 * It is up to the caller to zeroize \p T when it is no
256 * longer needed, and before freeing it if it was dynamically
257 * allocated.
258 */
259void mbedtls_mpi_mod_raw_inv_prime( mbedtls_mpi_uint *X,
260 const mbedtls_mpi_uint *A,
261 const mbedtls_mpi_uint *N,
262 size_t AN_limbs,
263 const mbedtls_mpi_uint *RR,
264 mbedtls_mpi_uint *T );
265
Janos Follath5933f692022-11-02 14:35:17 +0000266/* END MERGE SLOT 3 */
267
268/* BEGIN MERGE SLOT 4 */
269
270/* END MERGE SLOT 4 */
271
272/* BEGIN MERGE SLOT 5 */
Hanno Beckera45b6fe2022-11-01 13:14:28 +0000273/**
274 * \brief Perform a known-size modular addition.
275 *
Tom Cosgroveabddad42022-11-24 15:54:16 +0000276 * Calculate `A + B modulo N`.
277 *
278 * The number of limbs in each operand, and the result, is given by the
279 * modulus \p N.
280 *
281 * \p X may be aliased to \p A or \p B, or even both, but may not overlap
282 * either otherwise.
Hanno Beckera45b6fe2022-11-01 13:14:28 +0000283 *
Werner Lewis0eea8272022-11-01 13:27:29 +0000284 * \param[out] X The result of the modular addition.
285 * \param[in] A Little-endian presentation of the left operand. This
Tom Cosgroveabddad42022-11-24 15:54:16 +0000286 * must be smaller than \p N.
Werner Lewis0eea8272022-11-01 13:27:29 +0000287 * \param[in] B Little-endian presentation of the right operand. This
Tom Cosgroveabddad42022-11-24 15:54:16 +0000288 * must be smaller than \p N.
Werner Lewis9fa91eb2022-11-01 13:36:51 +0000289 * \param[in] N The address of the modulus.
Hanno Beckera45b6fe2022-11-01 13:14:28 +0000290 */
Werner Lewis0eea8272022-11-01 13:27:29 +0000291void mbedtls_mpi_mod_raw_add( mbedtls_mpi_uint *X,
Werner Lewisd391b8c2022-11-08 15:53:47 +0000292 const mbedtls_mpi_uint *A,
293 const mbedtls_mpi_uint *B,
Werner Lewis9fa91eb2022-11-01 13:36:51 +0000294 const mbedtls_mpi_mod_modulus *N );
Janos Follath5933f692022-11-02 14:35:17 +0000295/* END MERGE SLOT 5 */
296
297/* BEGIN MERGE SLOT 6 */
298
299/* END MERGE SLOT 6 */
300
301/* BEGIN MERGE SLOT 7 */
Minos Galanakisd9299c32022-11-01 16:19:07 +0000302/** Convert an MPI into Montgomery form.
Hanno Becker5ad4a932022-08-09 14:45:53 +0100303 *
304 * \param X The address of the MPI.
Minos Galanakisd9299c32022-11-01 16:19:07 +0000305 * Must have the same number of limbs as \p m.
306 * \param m The address of the modulus, which gives the size of
307 * the base `R` = 2^(biL*m->limbs).
Hanno Becker5ad4a932022-08-09 14:45:53 +0100308 *
309 * \return \c 0 if successful.
310 */
Minos Galanakisd9299c32022-11-01 16:19:07 +0000311int mbedtls_mpi_mod_raw_to_mont_rep( mbedtls_mpi_uint *X,
312 const mbedtls_mpi_mod_modulus *m );
Janos Follath5933f692022-11-02 14:35:17 +0000313
Minos Galanakisd9299c32022-11-01 16:19:07 +0000314/** Convert an MPI back from Montgomery representation.
Hanno Becker5ad4a932022-08-09 14:45:53 +0100315 *
316 * \param X The address of the MPI.
Minos Galanakisd9299c32022-11-01 16:19:07 +0000317 * Must have the same number of limbs as \p m.
318 * \param m The address of the modulus, which gives the size of
319 * the base `R`= 2^(biL*m->limbs).
Hanno Becker5ad4a932022-08-09 14:45:53 +0100320 *
321 * \return \c 0 if successful.
322 */
Minos Galanakisd9299c32022-11-01 16:19:07 +0000323int mbedtls_mpi_mod_raw_from_mont_rep( mbedtls_mpi_uint *X,
324 const mbedtls_mpi_mod_modulus *m );
Janos Follath5933f692022-11-02 14:35:17 +0000325/* END MERGE SLOT 7 */
326
327/* BEGIN MERGE SLOT 8 */
328
329/* END MERGE SLOT 8 */
330
331/* BEGIN MERGE SLOT 9 */
332
333/* END MERGE SLOT 9 */
334
335/* BEGIN MERGE SLOT 10 */
336
337/* END MERGE SLOT 10 */
338
Janos Follath5005edb2022-07-19 12:45:13 +0100339#endif /* MBEDTLS_BIGNUM_MOD_RAW_H */