blob: f9968ba74882cb00a5883194807c9818cf4d01b3 [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.
Gilles Peskine7f887bd2022-09-27 13:12:30 +020014 */
15
16/*
Gabor Mezeic5328cf2022-07-18 23:13:13 +020017 * Copyright The Mbed TLS Contributors
18 * SPDX-License-Identifier: Apache-2.0
19 *
20 * Licensed under the Apache License, Version 2.0 (the "License"); you may
21 * not use this file except in compliance with the License.
22 * You may obtain a copy of the License at
23 *
24 * http://www.apache.org/licenses/LICENSE-2.0
25 *
26 * Unless required by applicable law or agreed to in writing, software
27 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
28 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
29 * See the License for the specific language governing permissions and
30 * limitations under the License.
31 */
32
Janos Follath5005edb2022-07-19 12:45:13 +010033#ifndef MBEDTLS_BIGNUM_MOD_RAW_H
34#define MBEDTLS_BIGNUM_MOD_RAW_H
Gabor Mezeic5328cf2022-07-18 23:13:13 +020035
36#include "common.h"
37
38#if defined(MBEDTLS_BIGNUM_C)
39#include "mbedtls/bignum.h"
40#endif
41
Janos Follath0ded6312022-08-09 13:34:54 +010042#include "bignum_mod.h"
43
Gabor Mezei12071d42022-09-12 16:35:58 +020044/**
Gabor Mezei4086de62022-10-14 16:29:42 +020045 * \brief Perform a safe conditional copy of an MPI which doesn't reveal
46 * whether the assignment was done or not.
Gabor Mezei12071d42022-09-12 16:35:58 +020047 *
Gabor Mezeidba26772022-10-03 17:01:02 +020048 * The size to copy is determined by \p N.
49 *
Gabor Mezei4086de62022-10-14 16:29:42 +020050 * \param[out] X The address of the destination MPI.
51 * This must be initialized. Must have enough limbs to
52 * store the full value of \p A.
53 * \param[in] A The address of the source MPI. This must be initialized.
Gabor Mezei86dfe382022-09-30 14:03:04 +020054 * \param[in] N The address of the modulus related to \p X and \p A.
Gabor Mezei12071d42022-09-12 16:35:58 +020055 * \param assign The condition deciding whether to perform the
56 * assignment or not. Must be either 0 or 1:
Gabor Mezei1c628d52022-09-27 12:13:51 +020057 * * \c 1: Perform the assignment `X = A`.
Gabor Mezei12071d42022-09-12 16:35:58 +020058 * * \c 0: Keep the original value of \p X.
59 *
60 * \note This function avoids leaking any information about whether
61 * the assignment was done or not.
62 *
63 * \warning If \p assign is neither 0 nor 1, the result of this function
64 * is indeterminate, and the resulting value in \p X might be
Gabor Mezei4086de62022-10-14 16:29:42 +020065 * neither its original value nor the value in \p A.
Gabor Mezei12071d42022-09-12 16:35:58 +020066 */
Gabor Mezei63c32822022-09-15 20:01:31 +020067void mbedtls_mpi_mod_raw_cond_assign( mbedtls_mpi_uint *X,
Gabor Mezei1c628d52022-09-27 12:13:51 +020068 const mbedtls_mpi_uint *A,
Gabor Mezeie5b85852022-09-30 13:54:02 +020069 const mbedtls_mpi_mod_modulus *N,
Gabor Mezei63c32822022-09-15 20:01:31 +020070 unsigned char assign );
Gabor Mezei12071d42022-09-12 16:35:58 +020071
72/**
Gabor Mezei4086de62022-10-14 16:29:42 +020073 * \brief Perform a safe conditional swap of two MPIs which doesn't reveal
74 * whether the swap was done or not.
Gabor Mezei12071d42022-09-12 16:35:58 +020075 *
Gabor Mezeidba26772022-10-03 17:01:02 +020076 * The size to swap is determined by \p N.
77 *
Gabor Mezei86dfe382022-09-30 14:03:04 +020078 * \param[in,out] X The address of the first MPI. This must be initialized.
79 * \param[in,out] Y The address of the second MPI. This must be initialized.
80 * \param[in] N The address of the modulus related to \p X and \p Y.
Gabor Mezei12071d42022-09-12 16:35:58 +020081 * \param swap The condition deciding whether to perform
82 * the swap or not. Must be either 0 or 1:
Gabor Mezeie5b85852022-09-30 13:54:02 +020083 * * \c 1: Swap the values of \p X and \p Y.
84 * * \c 0: Keep the original values of \p X and \p Y.
Gabor Mezei12071d42022-09-12 16:35:58 +020085 *
86 * \note This function avoids leaking any information about whether
87 * the swap was done or not.
88 *
89 * \warning If \p swap is neither 0 nor 1, the result of this function
Gabor Mezeie5b85852022-09-30 13:54:02 +020090 * is indeterminate, and both \p X and \p Y might end up with
Gabor Mezei12071d42022-09-12 16:35:58 +020091 * values different to either of the original ones.
Gabor Mezei63c32822022-09-15 20:01:31 +020092 */
Gabor Mezeie5b85852022-09-30 13:54:02 +020093void mbedtls_mpi_mod_raw_cond_swap( mbedtls_mpi_uint *X,
94 mbedtls_mpi_uint *Y,
95 const mbedtls_mpi_mod_modulus *N,
Gabor Mezei63c32822022-09-15 20:01:31 +020096 unsigned char swap );
Gabor Mezei12071d42022-09-12 16:35:58 +020097
Gabor Mezei37b06362022-08-02 17:22:18 +020098/** Import X from unsigned binary data.
99 *
Janos Follath63184682022-08-11 17:42:59 +0100100 * The MPI needs to have enough limbs to store the full value (including any
101 * most significant zero bytes in the input).
Gabor Mezei37b06362022-08-02 17:22:18 +0200102 *
Janos Follathb7a88ec2022-08-19 12:24:40 +0100103 * \param[out] X The address of the MPI. The size is determined by \p m.
104 * (In particular, it must have at least as many limbs as
105 * the modulus \p m.)
106 * \param[in] m The address of the modulus related to \p X.
107 * \param[in] input The input buffer to import from.
108 * \param input_length The length in bytes of \p input.
Janos Follathd3eed332022-11-24 17:42:02 +0000109 * \param ext_rep The endianness of the number in the input buffer.
Gabor Mezei37b06362022-08-02 17:22:18 +0200110 *
111 * \return \c 0 if successful.
112 * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p X isn't
Janos Follathb7a88ec2022-08-19 12:24:40 +0100113 * large enough to hold the value in \p input.
Janos Follath8ff07292022-08-08 08:39:52 +0100114 * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if the external representation
Janos Follathdae11472022-08-08 11:50:02 +0100115 * of \p m is invalid or \p X is not less than \p m.
Gabor Mezei37b06362022-08-02 17:22:18 +0200116 */
Gabor Mezeic5328cf2022-07-18 23:13:13 +0200117int mbedtls_mpi_mod_raw_read( mbedtls_mpi_uint *X,
Janos Follath6b8a4ad2022-08-19 10:58:34 +0100118 const mbedtls_mpi_mod_modulus *m,
Janos Follathb7a88ec2022-08-19 12:24:40 +0100119 const unsigned char *input,
Janos Follathd3eed332022-11-24 17:42:02 +0000120 size_t input_length,
121 mbedtls_mpi_mod_ext_rep ext_rep );
Gabor Mezeic5328cf2022-07-18 23:13:13 +0200122
Janos Follathb7a88ec2022-08-19 12:24:40 +0100123/** Export A into unsigned binary data.
Gabor Mezei37b06362022-08-02 17:22:18 +0200124 *
Janos Follathb7a88ec2022-08-19 12:24:40 +0100125 * \param[in] A The address of the MPI. The size is determined by \p m.
126 * (In particular, it must have at least as many limbs as
127 * the modulus \p m.)
128 * \param[in] m The address of the modulus related to \p A.
129 * \param[out] output The output buffer to export to.
130 * \param output_length The length in bytes of \p output.
Janos Follathd3eed332022-11-24 17:42:02 +0000131 * \param ext_rep The endianness in which the number should be written into the output buffer.
Gabor Mezei37b06362022-08-02 17:22:18 +0200132 *
133 * \return \c 0 if successful.
Janos Follathb7a88ec2022-08-19 12:24:40 +0100134 * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p output isn't
135 * large enough to hold the value of \p A.
Janos Follath8ff07292022-08-08 08:39:52 +0100136 * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if the external representation
Gabor Mezei37b06362022-08-02 17:22:18 +0200137 * of \p m is invalid.
138 */
Janos Follathb7a88ec2022-08-19 12:24:40 +0100139int mbedtls_mpi_mod_raw_write( const mbedtls_mpi_uint *A,
Janos Follath6b8a4ad2022-08-19 10:58:34 +0100140 const mbedtls_mpi_mod_modulus *m,
Janos Follathb7a88ec2022-08-19 12:24:40 +0100141 unsigned char *output,
Janos Follathd3eed332022-11-24 17:42:02 +0000142 size_t output_length,
143 mbedtls_mpi_mod_ext_rep ext_rep );
Gabor Mezeic5328cf2022-07-18 23:13:13 +0200144
Janos Follath5933f692022-11-02 14:35:17 +0000145/* BEGIN MERGE SLOT 1 */
146
147/* END MERGE SLOT 1 */
148
149/* BEGIN MERGE SLOT 2 */
150
Gabor Mezei02d23132022-11-23 13:09:43 +0100151/** \brief Subtract two MPIs, returning the residue modulo the specified
152 * modulus.
Gabor Mezei4c7cf7d2022-11-09 14:07:43 +0100153 *
Gabor Mezei02d23132022-11-23 13:09:43 +0100154 * The size of the operation is determined by \p N. \p A and \p B must have
155 * the same number of limbs as \p N.
156 *
157 * \p X may be aliased to \p A or \p B, or even both, but may not overlap
158 * either otherwise.
Gabor Mezei4c7cf7d2022-11-09 14:07:43 +0100159 *
160 * \param[out] X The address of the result MPI.
161 * This must be initialized. Must have enough limbs to
162 * store the full value of the result.
163 * \param[in] A The address of the first MPI. This must be initialized.
164 * \param[in] B The address of the second MPI. This must be initialized.
Gabor Mezei02d23132022-11-23 13:09:43 +0100165 * \param[in] N The address of the modulus. Used to perform a modulo
Gabor Mezei4c7cf7d2022-11-09 14:07:43 +0100166 * operation on the result of the subtraction.
Gabor Mezei4c7cf7d2022-11-09 14:07:43 +0100167 */
168void mbedtls_mpi_mod_raw_sub( mbedtls_mpi_uint *X,
169 const mbedtls_mpi_uint *A,
170 const mbedtls_mpi_uint *B,
171 const mbedtls_mpi_mod_modulus *N );
172
Janos Follath5933f692022-11-02 14:35:17 +0000173/* END MERGE SLOT 2 */
174
175/* BEGIN MERGE SLOT 3 */
176
Tom Cosgrove61292682022-12-08 09:44:10 +0000177/**
178 * \brief Returns the number of limbs of working memory required for
179 * a call to `mbedtls_mpi_mod_raw_inv_prime()`.
180 *
181 * \param AN_limbs The number of limbs in the input `A` and the modulus `N`
182 * (they must be the same size) that will be given to
183 * `mbedtls_mpi_mod_raw_inv_prime()`.
184 *
185 * \return The number of limbs of working memory required by
186 * `mbedtls_mpi_mod_raw_inv_prime()`.
187 */
188size_t mbedtls_mpi_mod_raw_inv_prime_working_limbs( size_t AN_limbs );
189
190/**
191 * \brief Perform fixed-width modular inversion of a Montgomery-form MPI with
192 * respect to a modulus \p N that must be prime.
193 *
194 * \p X may be aliased to \p A, but not to \p N or \p RR.
195 *
196 * \param[out] X The modular inverse of \p A with respect to \p N.
197 * Will be in Montgomery form.
198 * \param[in] A The number to calculate the modular inverse of.
199 * Must be in Montgomery form. Must not be 0.
200 * \param[in] N The modulus, as a little-endian array of length \p AN_limbs.
201 * Must be prime.
202 * \param AN_limbs The number of limbs in \p A, \p N and \p RR.
203 * \param[in] RR The precomputed residue of 2^{2*biL} modulo N, as a little-
204 * endian array of length \p AN_limbs.
205 * \param[in,out] T Temporary storage of at least the number of limbs returned
206 * by `mbedtls_mpi_mod_raw_inv_prime_working_limbs()`.
207 * Its initial content is unused and its final content is
208 * indeterminate.
209 * It must not alias or otherwise overlap any of the other
210 * parameters.
211 * It is up to the caller to zeroize \p T when it is no
212 * longer needed, and before freeing it if it was dynamically
213 * allocated.
214 */
215void mbedtls_mpi_mod_raw_inv_prime( mbedtls_mpi_uint *X,
216 const mbedtls_mpi_uint *A,
217 const mbedtls_mpi_uint *N,
218 size_t AN_limbs,
219 const mbedtls_mpi_uint *RR,
220 mbedtls_mpi_uint *T );
221
Janos Follath5933f692022-11-02 14:35:17 +0000222/* END MERGE SLOT 3 */
223
224/* BEGIN MERGE SLOT 4 */
225
226/* END MERGE SLOT 4 */
227
228/* BEGIN MERGE SLOT 5 */
Hanno Beckera45b6fe2022-11-01 13:14:28 +0000229/**
230 * \brief Perform a known-size modular addition.
231 *
Tom Cosgroveabddad42022-11-24 15:54:16 +0000232 * Calculate `A + B modulo N`.
233 *
234 * The number of limbs in each operand, and the result, is given by the
235 * modulus \p N.
236 *
237 * \p X may be aliased to \p A or \p B, or even both, but may not overlap
238 * either otherwise.
Hanno Beckera45b6fe2022-11-01 13:14:28 +0000239 *
Werner Lewis0eea8272022-11-01 13:27:29 +0000240 * \param[out] X The result of the modular addition.
241 * \param[in] A Little-endian presentation of the left operand. This
Tom Cosgroveabddad42022-11-24 15:54:16 +0000242 * must be smaller than \p N.
Werner Lewis0eea8272022-11-01 13:27:29 +0000243 * \param[in] B Little-endian presentation of the right operand. This
Tom Cosgroveabddad42022-11-24 15:54:16 +0000244 * must be smaller than \p N.
Werner Lewis9fa91eb2022-11-01 13:36:51 +0000245 * \param[in] N The address of the modulus.
Hanno Beckera45b6fe2022-11-01 13:14:28 +0000246 */
Werner Lewis0eea8272022-11-01 13:27:29 +0000247void mbedtls_mpi_mod_raw_add( mbedtls_mpi_uint *X,
Werner Lewisd391b8c2022-11-08 15:53:47 +0000248 const mbedtls_mpi_uint *A,
249 const mbedtls_mpi_uint *B,
Werner Lewis9fa91eb2022-11-01 13:36:51 +0000250 const mbedtls_mpi_mod_modulus *N );
Janos Follath5933f692022-11-02 14:35:17 +0000251/* END MERGE SLOT 5 */
252
253/* BEGIN MERGE SLOT 6 */
254
255/* END MERGE SLOT 6 */
256
257/* BEGIN MERGE SLOT 7 */
Minos Galanakisd9299c32022-11-01 16:19:07 +0000258/** Convert an MPI into Montgomery form.
Hanno Becker5ad4a932022-08-09 14:45:53 +0100259 *
260 * \param X The address of the MPI.
Minos Galanakisd9299c32022-11-01 16:19:07 +0000261 * Must have the same number of limbs as \p m.
262 * \param m The address of the modulus, which gives the size of
263 * the base `R` = 2^(biL*m->limbs).
Hanno Becker5ad4a932022-08-09 14:45:53 +0100264 *
265 * \return \c 0 if successful.
266 */
Minos Galanakisd9299c32022-11-01 16:19:07 +0000267int mbedtls_mpi_mod_raw_to_mont_rep( mbedtls_mpi_uint *X,
268 const mbedtls_mpi_mod_modulus *m );
Janos Follath5933f692022-11-02 14:35:17 +0000269
Minos Galanakisd9299c32022-11-01 16:19:07 +0000270/** Convert an MPI back from Montgomery representation.
Hanno Becker5ad4a932022-08-09 14:45:53 +0100271 *
272 * \param X The address of the MPI.
Minos Galanakisd9299c32022-11-01 16:19:07 +0000273 * Must have the same number of limbs as \p m.
274 * \param m The address of the modulus, which gives the size of
275 * the base `R`= 2^(biL*m->limbs).
Hanno Becker5ad4a932022-08-09 14:45:53 +0100276 *
277 * \return \c 0 if successful.
278 */
Minos Galanakisd9299c32022-11-01 16:19:07 +0000279int mbedtls_mpi_mod_raw_from_mont_rep( mbedtls_mpi_uint *X,
280 const mbedtls_mpi_mod_modulus *m );
Minos Galanakis21fe8bd2022-12-07 18:06:05 +0000281
282/** \brief Perform fixed width modular negation.
283 *
Minos Galanakis5e8443e62022-12-08 11:40:51 +0000284 * The size of the operation is determined by \p m. \p A must have
285 * the same number of limbs as \p m.
Minos Galanakis21fe8bd2022-12-07 18:06:05 +0000286 *
287 * \p X may be aliased to \p A.
288 *
289 * \param[out] X The result of the modular negation.
Minos Galanakis5e8443e62022-12-08 11:40:51 +0000290 * This must be initialized.
Minos Galanakis21fe8bd2022-12-07 18:06:05 +0000291 * \param[in] A Little-endian presentation of the input operand. This
Minos Galanakis5e8443e62022-12-08 11:40:51 +0000292 * must be less than or equal to \p m.
293 * \param[in] m The modulus to use.
Minos Galanakis21fe8bd2022-12-07 18:06:05 +0000294 */
295void mbedtls_mpi_mod_raw_neg( mbedtls_mpi_uint *X,
296 const mbedtls_mpi_uint *A,
297 const mbedtls_mpi_mod_modulus *m);
Janos Follath5933f692022-11-02 14:35:17 +0000298/* END MERGE SLOT 7 */
299
300/* BEGIN MERGE SLOT 8 */
301
302/* END MERGE SLOT 8 */
303
304/* BEGIN MERGE SLOT 9 */
305
306/* END MERGE SLOT 9 */
307
308/* BEGIN MERGE SLOT 10 */
309
310/* END MERGE SLOT 10 */
311
Janos Follath5005edb2022-07-19 12:45:13 +0100312#endif /* MBEDTLS_BIGNUM_MOD_RAW_H */