blob: a538ece0f5abd2ee8de0af4b4fc183e390b0d1b0 [file] [log] [blame]
Gabor Mezeib9030702022-07-18 23:09:45 +02001/**
Janos Follath63184682022-08-11 17:42:59 +01002 * Core bignum functions
3 *
Janos Follathaf3f39c2022-08-22 09:06:32 +01004 * This interface should only be used by the legacy bignum module (bignum.h)
Janos Follath63184682022-08-11 17:42:59 +01005 * and the modular bignum modules (bignum_mod.c, bignum_mod_raw.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 Mezeib9030702022-07-18 23:09:45 +02008 *
9 * Copyright The Mbed TLS Contributors
10 * SPDX-License-Identifier: Apache-2.0
11 *
12 * Licensed under the Apache License, Version 2.0 (the "License"); you may
13 * not use this file except in compliance with the License.
14 * You may obtain a copy of the License at
15 *
16 * http://www.apache.org/licenses/LICENSE-2.0
17 *
18 * Unless required by applicable law or agreed to in writing, software
19 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
20 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21 * See the License for the specific language governing permissions and
22 * limitations under the License.
23 */
24
25#ifndef MBEDTLS_BIGNUM_CORE_H
26#define MBEDTLS_BIGNUM_CORE_H
27
28#include "common.h"
29
30#if defined(MBEDTLS_BIGNUM_C)
31#include "mbedtls/bignum.h"
32#endif
33
Tom Cosgrove5eefc3d2022-08-31 17:16:50 +010034#define ciL ( sizeof(mbedtls_mpi_uint) ) /* chars in limb */
35#define biL ( ciL << 3 ) /* bits in limb */
36#define biH ( ciL << 2 ) /* half limb size */
37
38/*
39 * Convert between bits/chars and number of limbs
40 * Divide first in order to avoid potential overflows
41 */
42#define BITS_TO_LIMBS(i) ( (i) / biL + ( (i) % biL != 0 ) )
43#define CHARS_TO_LIMBS(i) ( (i) / ciL + ( (i) % ciL != 0 ) )
44/* Get a specific byte, without range checks. */
45#define GET_BYTE( X, i ) \
46 ( ( (X)[(i) / ciL] >> ( ( (i) % ciL ) * 8 ) ) & 0xff )
47
Gabor Mezei37b06362022-08-02 17:22:18 +020048/** Count leading zero bits in a given integer.
49 *
Janos Follathb7a88ec2022-08-19 12:24:40 +010050 * \param a Integer to count leading zero bits.
Gabor Mezei37b06362022-08-02 17:22:18 +020051 *
Janos Follathb7a88ec2022-08-19 12:24:40 +010052 * \return The number of leading zero bits in \p a.
Gabor Mezei37b06362022-08-02 17:22:18 +020053 */
Janos Follath2e328c82022-08-22 11:19:10 +010054size_t mbedtls_mpi_core_clz( mbedtls_mpi_uint a );
Janos Follath4670f882022-07-21 18:25:42 +010055
Janos Follatha95f2042022-08-19 12:09:17 +010056/** Return the minimum number of bits required to represent the value held
Janos Follath63184682022-08-11 17:42:59 +010057 * in the MPI.
58 *
Janos Follathb7a88ec2022-08-19 12:24:40 +010059 * \note This function returns 0 if all the limbs of \p A are 0.
Gabor Mezei37b06362022-08-02 17:22:18 +020060 *
Janos Follathb7a88ec2022-08-19 12:24:40 +010061 * \param[in] A The address of the MPI.
Janos Follathc4596412022-08-22 10:01:27 +010062 * \param A_limbs The number of limbs of \p A.
Gabor Mezei37b06362022-08-02 17:22:18 +020063 *
Janos Follathb7a88ec2022-08-19 12:24:40 +010064 * \return The number of bits in \p A.
Gabor Mezei37b06362022-08-02 17:22:18 +020065 */
Janos Follathaf3f39c2022-08-22 09:06:32 +010066size_t mbedtls_mpi_core_bitlen( const mbedtls_mpi_uint *A, size_t A_limbs );
Janos Follath4670f882022-07-21 18:25:42 +010067
Gabor Mezei37b06362022-08-02 17:22:18 +020068/** Convert a big-endian byte array aligned to the size of mbedtls_mpi_uint
69 * into the storage form used by mbedtls_mpi.
70 *
Janos Follathb7a88ec2022-08-19 12:24:40 +010071 * \param[in,out] A The address of the MPI.
Janos Follathc4596412022-08-22 10:01:27 +010072 * \param A_limbs The number of limbs of \p A.
Gabor Mezei37b06362022-08-02 17:22:18 +020073 */
Janos Follathb7a88ec2022-08-19 12:24:40 +010074void mbedtls_mpi_core_bigendian_to_host( mbedtls_mpi_uint *A,
Janos Follathc4596412022-08-22 10:01:27 +010075 size_t A_limbs );
Janos Follath4670f882022-07-21 18:25:42 +010076
Gabor Mezeie1d31c42022-09-12 16:25:24 +020077/**
78 * \brief Perform a safe conditional copy of MPI which doesn't reveal whether
79 * the condition was true or not.
80 *
81 * \param[OUT] X The address of the first MPI. This must be initialized.
82 * \param X_limbs The number of limbs of \p X.
83 * \param[IN] Y The address of the second MPI. This must be initialized.
84 * \param Y_limbs The number of limbs of \p Y.
85 * \param assign The condition deciding whether to perform the
86 * assignment or not. Must be either 0 or 1:
87 * * \c 1: Perform the assignment `X = Y`.
88 * * \c 0: Keep the original value of \p X.
89 *
90 * \note This function avoids leaking any information about whether
91 * the assignment was done or not.
92 *
93 * \warning If \p assign is neither 0 nor 1, the result of this function
94 * is indeterminate, and the resulting value in \p X might be
95 * neither its original value nor the value in \p Y.
Gabor Mezeie1d31c42022-09-12 16:25:24 +020096 */
Gabor Mezei9f6615f2022-09-15 19:12:06 +020097void mbedtls_mpi_core_cond_assign( mbedtls_mpi_uint *X,
98 size_t X_limbs,
99 const mbedtls_mpi_uint *Y,
100 size_t Y_limbs,
101 unsigned char assign );
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200102
103/**
104 * \brief Perform a safe conditional copy of MPI which doesn't reveal whether
105 * the condition was true or not.
106 *
107 * \param[IN,OUT] X The address of the first MPI.
108 * This must be initialized.
109 * \param X_limbs The number of limbs of \p X.
110 * \param[IN,OUT] Y The address of the second MPI.
111 * This must be initialized.
112 * \param Y_limbs The number of limbs of \p Y.
113 * \param swap The condition deciding whether to perform
114 * the swap or not. Must be either 0 or 1:
115 * * \c 1: Swap the values of \p X and \p Y.
116 * * \c 0: Keep the original values of \p X and \p Y.
117 *
118 * \note This function avoids leaking any information about whether
119 * the swap was done or not.
120 *
121 * \warning If \p swap is neither 0 nor 1, the result of this function
122 * is indeterminate, and both \p X and \p Y might end up with
123 * values different to either of the original ones.
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200124 */
Gabor Mezei9f6615f2022-09-15 19:12:06 +0200125void mbedtls_mpi_core_cond_swap( mbedtls_mpi_uint *X,
126 size_t X_limbs,
127 mbedtls_mpi_uint *Y,
128 size_t Y_limbs,
129 unsigned char swap );
Gabor Mezeie1d31c42022-09-12 16:25:24 +0200130
Janos Follathaf3f39c2022-08-22 09:06:32 +0100131/** Import X from unsigned binary data, little-endian.
Gabor Mezei37b06362022-08-02 17:22:18 +0200132 *
Janos Follath63184682022-08-11 17:42:59 +0100133 * The MPI needs to have enough limbs to store the full value (including any
134 * most significant zero bytes in the input).
Gabor Mezei37b06362022-08-02 17:22:18 +0200135 *
Janos Follathb7a88ec2022-08-19 12:24:40 +0100136 * \param[out] X The address of the MPI.
137 * \param X_limbs The number of limbs of \p X.
138 * \param[in] input The input buffer to import from.
139 * \param input_length The length bytes of \p input.
Gabor Mezei37b06362022-08-02 17:22:18 +0200140 *
141 * \return \c 0 if successful.
142 * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p X isn't
Janos Follathb7a88ec2022-08-19 12:24:40 +0100143 * large enough to hold the value in \p input.
Gabor Mezei37b06362022-08-02 17:22:18 +0200144 */
Gabor Mezeib9030702022-07-18 23:09:45 +0200145int mbedtls_mpi_core_read_le( mbedtls_mpi_uint *X,
Janos Follathb7a88ec2022-08-19 12:24:40 +0100146 size_t X_limbs,
147 const unsigned char *input,
148 size_t input_length );
Gabor Mezeib9030702022-07-18 23:09:45 +0200149
Janos Follathaf3f39c2022-08-22 09:06:32 +0100150/** Import X from unsigned binary data, big-endian.
Gabor Mezei37b06362022-08-02 17:22:18 +0200151 *
Janos Follath63184682022-08-11 17:42:59 +0100152 * The MPI needs to have enough limbs to store the full value (including any
153 * most significant zero bytes in the input).
Gabor Mezei37b06362022-08-02 17:22:18 +0200154 *
Janos Follathb7a88ec2022-08-19 12:24:40 +0100155 * \param[out] X The address of the MPI.
156 * May only be #NULL if \X_limbs is 0 and \p input_length
157 * is 0.
158 * \param X_limbs The number of limbs of \p X.
159 * \param[in] input The input buffer to import from.
160 * May only be #NULL if \p input_length is 0.
161 * \param input_length The length in bytes of \p input.
Gabor Mezei37b06362022-08-02 17:22:18 +0200162 *
163 * \return \c 0 if successful.
164 * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p X isn't
Janos Follathb7a88ec2022-08-19 12:24:40 +0100165 * large enough to hold the value in \p input.
Gabor Mezei37b06362022-08-02 17:22:18 +0200166 */
Gabor Mezeib9030702022-07-18 23:09:45 +0200167int mbedtls_mpi_core_read_be( mbedtls_mpi_uint *X,
Janos Follathb7a88ec2022-08-19 12:24:40 +0100168 size_t X_limbs,
169 const unsigned char *input,
170 size_t input_length );
Gabor Mezeib9030702022-07-18 23:09:45 +0200171
Janos Follathaf3f39c2022-08-22 09:06:32 +0100172/** Export A into unsigned binary data, little-endian.
Gabor Mezei37b06362022-08-02 17:22:18 +0200173 *
Janos Follathb7a88ec2022-08-19 12:24:40 +0100174 * \note If \p output is shorter than \p A the export is still successful if the
175 * value held in \p A fits in the buffer (that is, if enough of the most
176 * significant bytes of \p A are 0).
Janos Follath63184682022-08-11 17:42:59 +0100177 *
Janos Follathb7a88ec2022-08-19 12:24:40 +0100178 * \param[in] A The address of the MPI.
179 * \param A_limbs The number of limbs of \p A.
180 * \param[out] output The output buffer to export to.
181 * \param output_length The length in bytes of \p output.
Gabor Mezei37b06362022-08-02 17:22:18 +0200182 *
183 * \return \c 0 if successful.
Janos Follathb7a88ec2022-08-19 12:24:40 +0100184 * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p output isn't
185 * large enough to hold the value of \p A.
Gabor Mezei37b06362022-08-02 17:22:18 +0200186 */
Janos Follathb7a88ec2022-08-19 12:24:40 +0100187int mbedtls_mpi_core_write_le( const mbedtls_mpi_uint *A,
188 size_t A_limbs,
189 unsigned char *output,
190 size_t output_length );
Gabor Mezeib9030702022-07-18 23:09:45 +0200191
Janos Follathaf3f39c2022-08-22 09:06:32 +0100192/** Export A into unsigned binary data, big-endian.
Gabor Mezei37b06362022-08-02 17:22:18 +0200193 *
Janos Follathb7a88ec2022-08-19 12:24:40 +0100194 * \note If \p output is shorter than \p A the export is still successful if the
195 * value held in \p A fits in the buffer (that is, if enough of the most
196 * significant bytes of \p A are 0).
Janos Follath63184682022-08-11 17:42:59 +0100197 *
Janos Follathb7a88ec2022-08-19 12:24:40 +0100198 * \param[in] A The address of the MPI.
199 * \param A_limbs The number of limbs of \p A.
200 * \param[out] output The output buffer to export to.
201 * \param output_length The length in bytes of \p output.
Gabor Mezei37b06362022-08-02 17:22:18 +0200202 *
203 * \return \c 0 if successful.
Janos Follathb7a88ec2022-08-19 12:24:40 +0100204 * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p output isn't
205 * large enough to hold the value of \p A.
Gabor Mezei37b06362022-08-02 17:22:18 +0200206 */
Janos Follathb7a88ec2022-08-19 12:24:40 +0100207int mbedtls_mpi_core_write_be( const mbedtls_mpi_uint *A,
208 size_t A_limbs,
209 unsigned char *output,
210 size_t output_length );
Gabor Mezeib9030702022-07-18 23:09:45 +0200211
Tom Cosgrove90c426b2022-08-23 16:15:19 +0100212/**
Tom Cosgrove5c0e8102022-09-15 15:46:10 +0100213 * \brief Conditional addition of two fixed-size large unsigned integers,
Tom Cosgroved932de82022-08-25 16:43:43 +0100214 * returning the carry.
Hanno Becker71f4b0d2022-08-23 12:09:35 +0100215 *
216 * Functionally equivalent to
217 *
218 * ```
219 * if( cond )
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100220 * X += A;
Hanno Becker71f4b0d2022-08-23 12:09:35 +0100221 * return carry;
222 * ```
223 *
Tom Cosgrove47828232022-09-20 13:51:50 +0100224 * This function operates modulo `2^(biL*limbs)`.
225 *
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100226 * \param[in,out] X The pointer to the (little-endian) array
Tom Cosgrove72594632022-08-24 11:51:58 +0100227 * representing the bignum to accumulate onto.
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100228 * \param[in] A The pointer to the (little-endian) array
Tom Cosgrove72594632022-08-24 11:51:58 +0100229 * representing the bignum to conditionally add
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100230 * to \p X. This may be aliased to \p X but may not
Tom Cosgroveed43c6c2022-08-31 11:35:00 +0100231 * overlap otherwise.
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100232 * \param limbs Number of limbs of \p X and \p A.
Tom Cosgrove72594632022-08-24 11:51:58 +0100233 * \param cond Condition bit dictating whether addition should
234 * happen or not. This must be \c 0 or \c 1.
Hanno Becker71f4b0d2022-08-23 12:09:35 +0100235 *
Tom Cosgroveecbb1242022-08-25 10:13:44 +0100236 * \warning If \p cond is neither 0 nor 1, the result of this function
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100237 * is unspecified, and the resulting value in \p X might be
238 * neither its original value nor \p X + \p A.
Tom Cosgrove72594632022-08-24 11:51:58 +0100239 *
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100240 * \return 1 if `X + cond * A >= 2^(biL*limbs)`, 0 otherwise.
Hanno Becker71f4b0d2022-08-23 12:09:35 +0100241 */
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100242mbedtls_mpi_uint mbedtls_mpi_core_add_if( mbedtls_mpi_uint *X,
243 const mbedtls_mpi_uint *A,
Tom Cosgrove72594632022-08-24 11:51:58 +0100244 size_t limbs,
Tom Cosgrove90c426b2022-08-23 16:15:19 +0100245 unsigned cond );
Hanno Becker71f4b0d2022-08-23 12:09:35 +0100246
Tom Cosgroveb4964862022-08-30 11:57:22 +0100247/**
Tom Cosgrove5c0e8102022-09-15 15:46:10 +0100248 * \brief Subtract two fixed-size large unsigned integers, returning the borrow.
Tom Cosgroveb4964862022-08-30 11:57:22 +0100249 *
Tom Cosgrove5dd97e62022-08-30 14:31:49 +0100250 * Calculate `A - B` where \p A and \p B have the same size.
Tom Cosgrove630110a2022-08-31 17:09:29 +0100251 * This function operates modulo `2^(biL*limbs)` and returns the carry
Tom Cosgroveb4964862022-08-30 11:57:22 +0100252 * (1 if there was a wraparound, i.e. if `A < B`, and 0 otherwise).
253 *
Tom Cosgrove5dd97e62022-08-30 14:31:49 +0100254 * \p X may be aliased to \p A or \p B, or even both, but may not overlap
255 * either otherwise.
Tom Cosgroveb4964862022-08-30 11:57:22 +0100256 *
257 * \param[out] X The result of the subtraction.
258 * \param[in] A Little-endian presentation of left operand.
259 * \param[in] B Little-endian presentation of right operand.
260 * \param limbs Number of limbs of \p X, \p A and \p B.
261 *
262 * \return 1 if `A < B`.
263 * 0 if `A >= B`.
264 */
265mbedtls_mpi_uint mbedtls_mpi_core_sub( mbedtls_mpi_uint *X,
266 const mbedtls_mpi_uint *A,
267 const mbedtls_mpi_uint *B,
268 size_t limbs );
269
270/**
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100271 * \brief Perform a fixed-size multiply accumulate operation: X += b * A
Tom Cosgroveb4964862022-08-30 11:57:22 +0100272 *
Tom Cosgroveb0b77e12022-09-20 13:33:40 +0100273 * \p X may be aliased to \p A (when \p X_limbs == \p A_limbs), but may not
274 * otherwise overlap.
275 *
Tom Cosgrove47828232022-09-20 13:51:50 +0100276 * This function operates modulo `2^(biL*X_limbs)`.
277 *
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100278 * \param[in,out] X The pointer to the (little-endian) array
Tom Cosgroveb4964862022-08-30 11:57:22 +0100279 * representing the bignum to accumulate onto.
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100280 * \param X_limbs The number of limbs of \p X. This must be
281 * at least \p A_limbs.
282 * \param[in] A The pointer to the (little-endian) array
Tom Cosgroveb4964862022-08-30 11:57:22 +0100283 * representing the bignum to multiply with.
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100284 * This may be aliased to \p X but may not overlap
Tom Cosgroveed43c6c2022-08-31 11:35:00 +0100285 * otherwise.
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100286 * \param A_limbs The number of limbs of \p A.
287 * \param b X scalar to multiply with.
Tom Cosgroveb4964862022-08-30 11:57:22 +0100288 *
289 * \return The carry at the end of the operation.
290 */
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100291mbedtls_mpi_uint mbedtls_mpi_core_mla( mbedtls_mpi_uint *X, size_t X_limbs,
292 const mbedtls_mpi_uint *A, size_t A_limbs,
293 mbedtls_mpi_uint b );
Tom Cosgroveb4964862022-08-30 11:57:22 +0100294
295/**
296 * \brief Calculate initialisation value for fast Montgomery modular
297 * multiplication
298 *
299 * \param[in] N Little-endian presentation of the modulus. This must have
300 * at least one limb.
301 *
302 * \return The initialisation value for fast Montgomery modular multiplication
303 */
Tom Cosgroveb7438d12022-09-15 15:05:59 +0100304mbedtls_mpi_uint mbedtls_mpi_core_montmul_init( const mbedtls_mpi_uint *N );
Tom Cosgroveb4964862022-08-30 11:57:22 +0100305
306/**
Tom Cosgrove4386ead2022-09-29 14:40:21 +0100307 * \brief Montgomery multiplication: X = A * B * R^-1 mod N (HAC 14.36)
308 *
309 * \p A and \p B must be in canonical form. That is, < \p N.
Tom Cosgroveb4964862022-08-30 11:57:22 +0100310 *
Tom Cosgroveea45c1d2022-09-20 13:17:51 +0100311 * \p X may be aliased to \p A or \p N, or even \p B (if \p AN_limbs ==
312 * \p B_limbs) but may not overlap any parameters otherwise.
313 *
Tom Cosgrove4386ead2022-09-29 14:40:21 +0100314 * \p A and \p B may alias each other, if \p AN_limbs == \p B_limbs. They may
315 * not alias \p N (since they must be in canonical form, they cannot == \p N).
Tom Cosgroveea45c1d2022-09-20 13:17:51 +0100316 *
Tom Cosgroveb4964862022-08-30 11:57:22 +0100317 * \param[out] X The destination MPI, as a little-endian array of
318 * length \p AN_limbs.
319 * On successful completion, X contains the result of
Tom Cosgrove630110a2022-08-31 17:09:29 +0100320 * the multiplication `A * B * R^-1` mod N where
321 * `R = 2^(biL*AN_limbs)`.
Tom Cosgroveb4964862022-08-30 11:57:22 +0100322 * \param[in] A Little-endian presentation of first operand.
Tom Cosgrove5dd97e62022-08-30 14:31:49 +0100323 * Must have the same number of limbs as \p N.
Tom Cosgroveb4964862022-08-30 11:57:22 +0100324 * \param[in] B Little-endian presentation of second operand.
325 * \param[in] B_limbs The number of limbs in \p B.
Tom Cosgrove5dd97e62022-08-30 14:31:49 +0100326 * Must be <= \p AN_limbs.
Tom Cosgroveb4964862022-08-30 11:57:22 +0100327 * \param[in] N Little-endian presentation of the modulus.
Tom Cosgrove5dd97e62022-08-30 14:31:49 +0100328 * This must be odd, and have exactly the same number
329 * of limbs as \p A.
Tom Cosgrove6da3a3b2022-09-29 17:20:18 +0100330 * It may alias \p X, but must not alias or otherwise
331 * overlap any of the other parameters.
Tom Cosgrove5dd97e62022-08-30 14:31:49 +0100332 * \param[in] AN_limbs The number of limbs in \p X, \p A and \p N.
Tom Cosgrove630110a2022-08-31 17:09:29 +0100333 * \param mm The Montgomery constant for \p N: -N^-1 mod 2^biL.
Tom Cosgroveb7438d12022-09-15 15:05:59 +0100334 * This can be calculated by `mbedtls_mpi_core_montmul_init()`.
Tom Cosgroveb4964862022-08-30 11:57:22 +0100335 * \param[in,out] T Temporary storage of size at least 2*AN_limbs+1 limbs.
336 * Its initial content is unused and
337 * its final content is indeterminate.
Tom Cosgrove4386ead2022-09-29 14:40:21 +0100338 * It must not alias or otherwise overlap any of the
339 * other parameters.
Tom Cosgroveb4964862022-08-30 11:57:22 +0100340 */
341void mbedtls_mpi_core_montmul( mbedtls_mpi_uint *X,
342 const mbedtls_mpi_uint *A,
343 const mbedtls_mpi_uint *B, size_t B_limbs,
344 const mbedtls_mpi_uint *N, size_t AN_limbs,
345 mbedtls_mpi_uint mm, mbedtls_mpi_uint *T );
346
Gabor Mezeib9030702022-07-18 23:09:45 +0200347#endif /* MBEDTLS_BIGNUM_CORE_H */