blob: dc802ab4ef6602edbfb1bfff47088d8e744c3120 [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 *
Gilles Peskine7f887bd2022-09-27 13:12:30 +02009 */
10
11/*
Gabor Mezeib9030702022-07-18 23:09:45 +020012 * Copyright The Mbed TLS Contributors
13 * SPDX-License-Identifier: Apache-2.0
14 *
15 * Licensed under the Apache License, Version 2.0 (the "License"); you may
16 * not use this file except in compliance with the License.
17 * You may obtain a copy of the License at
18 *
19 * http://www.apache.org/licenses/LICENSE-2.0
20 *
21 * Unless required by applicable law or agreed to in writing, software
22 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
23 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24 * See the License for the specific language governing permissions and
25 * limitations under the License.
26 */
27
28#ifndef MBEDTLS_BIGNUM_CORE_H
29#define MBEDTLS_BIGNUM_CORE_H
30
31#include "common.h"
32
33#if defined(MBEDTLS_BIGNUM_C)
34#include "mbedtls/bignum.h"
35#endif
36
Tom Cosgrove5eefc3d2022-08-31 17:16:50 +010037#define ciL ( sizeof(mbedtls_mpi_uint) ) /* chars in limb */
38#define biL ( ciL << 3 ) /* bits in limb */
39#define biH ( ciL << 2 ) /* half limb size */
40
41/*
42 * Convert between bits/chars and number of limbs
43 * Divide first in order to avoid potential overflows
44 */
45#define BITS_TO_LIMBS(i) ( (i) / biL + ( (i) % biL != 0 ) )
46#define CHARS_TO_LIMBS(i) ( (i) / ciL + ( (i) % ciL != 0 ) )
47/* Get a specific byte, without range checks. */
48#define GET_BYTE( X, i ) \
49 ( ( (X)[(i) / ciL] >> ( ( (i) % ciL ) * 8 ) ) & 0xff )
50
Gabor Mezei37b06362022-08-02 17:22:18 +020051/** Count leading zero bits in a given integer.
52 *
Janos Follathb7a88ec2022-08-19 12:24:40 +010053 * \param a Integer to count leading zero bits.
Gabor Mezei37b06362022-08-02 17:22:18 +020054 *
Janos Follathb7a88ec2022-08-19 12:24:40 +010055 * \return The number of leading zero bits in \p a.
Gabor Mezei37b06362022-08-02 17:22:18 +020056 */
Janos Follath2e328c82022-08-22 11:19:10 +010057size_t mbedtls_mpi_core_clz( mbedtls_mpi_uint a );
Janos Follath4670f882022-07-21 18:25:42 +010058
Janos Follatha95f2042022-08-19 12:09:17 +010059/** Return the minimum number of bits required to represent the value held
Janos Follath63184682022-08-11 17:42:59 +010060 * in the MPI.
61 *
Janos Follathb7a88ec2022-08-19 12:24:40 +010062 * \note This function returns 0 if all the limbs of \p A are 0.
Gabor Mezei37b06362022-08-02 17:22:18 +020063 *
Janos Follathb7a88ec2022-08-19 12:24:40 +010064 * \param[in] A The address of the MPI.
Janos Follathc4596412022-08-22 10:01:27 +010065 * \param A_limbs The number of limbs of \p A.
Gabor Mezei37b06362022-08-02 17:22:18 +020066 *
Janos Follathb7a88ec2022-08-19 12:24:40 +010067 * \return The number of bits in \p A.
Gabor Mezei37b06362022-08-02 17:22:18 +020068 */
Janos Follathaf3f39c2022-08-22 09:06:32 +010069size_t mbedtls_mpi_core_bitlen( const mbedtls_mpi_uint *A, size_t A_limbs );
Janos Follath4670f882022-07-21 18:25:42 +010070
Gabor Mezei37b06362022-08-02 17:22:18 +020071/** Convert a big-endian byte array aligned to the size of mbedtls_mpi_uint
72 * into the storage form used by mbedtls_mpi.
73 *
Janos Follathb7a88ec2022-08-19 12:24:40 +010074 * \param[in,out] A The address of the MPI.
Janos Follathc4596412022-08-22 10:01:27 +010075 * \param A_limbs The number of limbs of \p A.
Gabor Mezei37b06362022-08-02 17:22:18 +020076 */
Janos Follathb7a88ec2022-08-19 12:24:40 +010077void mbedtls_mpi_core_bigendian_to_host( mbedtls_mpi_uint *A,
Janos Follathc4596412022-08-22 10:01:27 +010078 size_t A_limbs );
Janos Follath4670f882022-07-21 18:25:42 +010079
Janos Follathaf3f39c2022-08-22 09:06:32 +010080/** Import X from unsigned binary data, little-endian.
Gabor Mezei37b06362022-08-02 17:22:18 +020081 *
Janos Follath63184682022-08-11 17:42:59 +010082 * The MPI needs to have enough limbs to store the full value (including any
83 * most significant zero bytes in the input).
Gabor Mezei37b06362022-08-02 17:22:18 +020084 *
Janos Follathb7a88ec2022-08-19 12:24:40 +010085 * \param[out] X The address of the MPI.
86 * \param X_limbs The number of limbs of \p X.
87 * \param[in] input The input buffer to import from.
88 * \param input_length The length bytes of \p input.
Gabor Mezei37b06362022-08-02 17:22:18 +020089 *
90 * \return \c 0 if successful.
91 * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p X isn't
Janos Follathb7a88ec2022-08-19 12:24:40 +010092 * large enough to hold the value in \p input.
Gabor Mezei37b06362022-08-02 17:22:18 +020093 */
Gabor Mezeib9030702022-07-18 23:09:45 +020094int mbedtls_mpi_core_read_le( mbedtls_mpi_uint *X,
Janos Follathb7a88ec2022-08-19 12:24:40 +010095 size_t X_limbs,
96 const unsigned char *input,
97 size_t input_length );
Gabor Mezeib9030702022-07-18 23:09:45 +020098
Janos Follathaf3f39c2022-08-22 09:06:32 +010099/** Import X from unsigned binary data, big-endian.
Gabor Mezei37b06362022-08-02 17:22:18 +0200100 *
Janos Follath63184682022-08-11 17:42:59 +0100101 * The MPI needs to have enough limbs to store the full value (including any
102 * most significant zero bytes in the input).
Gabor Mezei37b06362022-08-02 17:22:18 +0200103 *
Janos Follathb7a88ec2022-08-19 12:24:40 +0100104 * \param[out] X The address of the MPI.
105 * May only be #NULL if \X_limbs is 0 and \p input_length
106 * is 0.
107 * \param X_limbs The number of limbs of \p X.
108 * \param[in] input The input buffer to import from.
109 * May only be #NULL if \p input_length is 0.
110 * \param input_length The length in bytes of \p input.
Gabor Mezei37b06362022-08-02 17:22:18 +0200111 *
112 * \return \c 0 if successful.
113 * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p X isn't
Janos Follathb7a88ec2022-08-19 12:24:40 +0100114 * large enough to hold the value in \p input.
Gabor Mezei37b06362022-08-02 17:22:18 +0200115 */
Gabor Mezeib9030702022-07-18 23:09:45 +0200116int mbedtls_mpi_core_read_be( mbedtls_mpi_uint *X,
Janos Follathb7a88ec2022-08-19 12:24:40 +0100117 size_t X_limbs,
118 const unsigned char *input,
119 size_t input_length );
Gabor Mezeib9030702022-07-18 23:09:45 +0200120
Janos Follathaf3f39c2022-08-22 09:06:32 +0100121/** Export A into unsigned binary data, little-endian.
Gabor Mezei37b06362022-08-02 17:22:18 +0200122 *
Janos Follathb7a88ec2022-08-19 12:24:40 +0100123 * \note If \p output is shorter than \p A the export is still successful if the
124 * value held in \p A fits in the buffer (that is, if enough of the most
125 * significant bytes of \p A are 0).
Janos Follath63184682022-08-11 17:42:59 +0100126 *
Janos Follathb7a88ec2022-08-19 12:24:40 +0100127 * \param[in] A The address of the MPI.
128 * \param A_limbs The number of limbs of \p A.
129 * \param[out] output The output buffer to export to.
130 * \param output_length The length in bytes of \p output.
Gabor Mezei37b06362022-08-02 17:22:18 +0200131 *
132 * \return \c 0 if successful.
Janos Follathb7a88ec2022-08-19 12:24:40 +0100133 * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p output isn't
134 * large enough to hold the value of \p A.
Gabor Mezei37b06362022-08-02 17:22:18 +0200135 */
Janos Follathb7a88ec2022-08-19 12:24:40 +0100136int mbedtls_mpi_core_write_le( const mbedtls_mpi_uint *A,
137 size_t A_limbs,
138 unsigned char *output,
139 size_t output_length );
Gabor Mezeib9030702022-07-18 23:09:45 +0200140
Janos Follathaf3f39c2022-08-22 09:06:32 +0100141/** Export A into unsigned binary data, big-endian.
Gabor Mezei37b06362022-08-02 17:22:18 +0200142 *
Janos Follathb7a88ec2022-08-19 12:24:40 +0100143 * \note If \p output is shorter than \p A the export is still successful if the
144 * value held in \p A fits in the buffer (that is, if enough of the most
145 * significant bytes of \p A are 0).
Janos Follath63184682022-08-11 17:42:59 +0100146 *
Janos Follathb7a88ec2022-08-19 12:24:40 +0100147 * \param[in] A The address of the MPI.
148 * \param A_limbs The number of limbs of \p A.
149 * \param[out] output The output buffer to export to.
150 * \param output_length The length in bytes of \p output.
Gabor Mezei37b06362022-08-02 17:22:18 +0200151 *
152 * \return \c 0 if successful.
Janos Follathb7a88ec2022-08-19 12:24:40 +0100153 * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p output isn't
154 * large enough to hold the value of \p A.
Gabor Mezei37b06362022-08-02 17:22:18 +0200155 */
Janos Follathb7a88ec2022-08-19 12:24:40 +0100156int mbedtls_mpi_core_write_be( const mbedtls_mpi_uint *A,
157 size_t A_limbs,
158 unsigned char *output,
159 size_t output_length );
Gabor Mezeib9030702022-07-18 23:09:45 +0200160
Tom Cosgrove90c426b2022-08-23 16:15:19 +0100161/**
Tom Cosgrove5c0e8102022-09-15 15:46:10 +0100162 * \brief Conditional addition of two fixed-size large unsigned integers,
Tom Cosgroved932de82022-08-25 16:43:43 +0100163 * returning the carry.
Hanno Becker71f4b0d2022-08-23 12:09:35 +0100164 *
165 * Functionally equivalent to
166 *
167 * ```
168 * if( cond )
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100169 * X += A;
Hanno Becker71f4b0d2022-08-23 12:09:35 +0100170 * return carry;
171 * ```
172 *
Tom Cosgrove47828232022-09-20 13:51:50 +0100173 * This function operates modulo `2^(biL*limbs)`.
174 *
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100175 * \param[in,out] X The pointer to the (little-endian) array
Tom Cosgrove72594632022-08-24 11:51:58 +0100176 * representing the bignum to accumulate onto.
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100177 * \param[in] A The pointer to the (little-endian) array
Tom Cosgrove72594632022-08-24 11:51:58 +0100178 * representing the bignum to conditionally add
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100179 * to \p X. This may be aliased to \p X but may not
Tom Cosgroveed43c6c2022-08-31 11:35:00 +0100180 * overlap otherwise.
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100181 * \param limbs Number of limbs of \p X and \p A.
Tom Cosgrove72594632022-08-24 11:51:58 +0100182 * \param cond Condition bit dictating whether addition should
183 * happen or not. This must be \c 0 or \c 1.
Hanno Becker71f4b0d2022-08-23 12:09:35 +0100184 *
Tom Cosgroveecbb1242022-08-25 10:13:44 +0100185 * \warning If \p cond is neither 0 nor 1, the result of this function
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100186 * is unspecified, and the resulting value in \p X might be
187 * neither its original value nor \p X + \p A.
Tom Cosgrove72594632022-08-24 11:51:58 +0100188 *
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100189 * \return 1 if `X + cond * A >= 2^(biL*limbs)`, 0 otherwise.
Hanno Becker71f4b0d2022-08-23 12:09:35 +0100190 */
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100191mbedtls_mpi_uint mbedtls_mpi_core_add_if( mbedtls_mpi_uint *X,
192 const mbedtls_mpi_uint *A,
Tom Cosgrove72594632022-08-24 11:51:58 +0100193 size_t limbs,
Tom Cosgrove90c426b2022-08-23 16:15:19 +0100194 unsigned cond );
Hanno Becker71f4b0d2022-08-23 12:09:35 +0100195
Tom Cosgroveb4964862022-08-30 11:57:22 +0100196/**
Tom Cosgrove5c0e8102022-09-15 15:46:10 +0100197 * \brief Subtract two fixed-size large unsigned integers, returning the borrow.
Tom Cosgroveb4964862022-08-30 11:57:22 +0100198 *
Tom Cosgrove5dd97e62022-08-30 14:31:49 +0100199 * Calculate `A - B` where \p A and \p B have the same size.
Tom Cosgrove630110a2022-08-31 17:09:29 +0100200 * This function operates modulo `2^(biL*limbs)` and returns the carry
Tom Cosgroveb4964862022-08-30 11:57:22 +0100201 * (1 if there was a wraparound, i.e. if `A < B`, and 0 otherwise).
202 *
Tom Cosgrove5dd97e62022-08-30 14:31:49 +0100203 * \p X may be aliased to \p A or \p B, or even both, but may not overlap
204 * either otherwise.
Tom Cosgroveb4964862022-08-30 11:57:22 +0100205 *
206 * \param[out] X The result of the subtraction.
207 * \param[in] A Little-endian presentation of left operand.
208 * \param[in] B Little-endian presentation of right operand.
209 * \param limbs Number of limbs of \p X, \p A and \p B.
210 *
211 * \return 1 if `A < B`.
212 * 0 if `A >= B`.
213 */
214mbedtls_mpi_uint mbedtls_mpi_core_sub( mbedtls_mpi_uint *X,
215 const mbedtls_mpi_uint *A,
216 const mbedtls_mpi_uint *B,
217 size_t limbs );
218
219/**
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100220 * \brief Perform a fixed-size multiply accumulate operation: X += b * A
Tom Cosgroveb4964862022-08-30 11:57:22 +0100221 *
Tom Cosgroveb0b77e12022-09-20 13:33:40 +0100222 * \p X may be aliased to \p A (when \p X_limbs == \p A_limbs), but may not
223 * otherwise overlap.
224 *
Tom Cosgrove47828232022-09-20 13:51:50 +0100225 * This function operates modulo `2^(biL*X_limbs)`.
226 *
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100227 * \param[in,out] X The pointer to the (little-endian) array
Tom Cosgroveb4964862022-08-30 11:57:22 +0100228 * representing the bignum to accumulate onto.
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100229 * \param X_limbs The number of limbs of \p X. This must be
230 * at least \p A_limbs.
231 * \param[in] A The pointer to the (little-endian) array
Tom Cosgroveb4964862022-08-30 11:57:22 +0100232 * representing the bignum to multiply with.
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100233 * This may be aliased to \p X but may not overlap
Tom Cosgroveed43c6c2022-08-31 11:35:00 +0100234 * otherwise.
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100235 * \param A_limbs The number of limbs of \p A.
236 * \param b X scalar to multiply with.
Tom Cosgroveb4964862022-08-30 11:57:22 +0100237 *
238 * \return The carry at the end of the operation.
239 */
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100240mbedtls_mpi_uint mbedtls_mpi_core_mla( mbedtls_mpi_uint *X, size_t X_limbs,
241 const mbedtls_mpi_uint *A, size_t A_limbs,
242 mbedtls_mpi_uint b );
Tom Cosgroveb4964862022-08-30 11:57:22 +0100243
244/**
245 * \brief Calculate initialisation value for fast Montgomery modular
246 * multiplication
247 *
248 * \param[in] N Little-endian presentation of the modulus. This must have
249 * at least one limb.
250 *
251 * \return The initialisation value for fast Montgomery modular multiplication
252 */
Tom Cosgroveb7438d12022-09-15 15:05:59 +0100253mbedtls_mpi_uint mbedtls_mpi_core_montmul_init( const mbedtls_mpi_uint *N );
Tom Cosgroveb4964862022-08-30 11:57:22 +0100254
255/**
Tom Cosgrove4386ead2022-09-29 14:40:21 +0100256 * \brief Montgomery multiplication: X = A * B * R^-1 mod N (HAC 14.36)
257 *
258 * \p A and \p B must be in canonical form. That is, < \p N.
Tom Cosgroveb4964862022-08-30 11:57:22 +0100259 *
Tom Cosgroveea45c1d2022-09-20 13:17:51 +0100260 * \p X may be aliased to \p A or \p N, or even \p B (if \p AN_limbs ==
261 * \p B_limbs) but may not overlap any parameters otherwise.
262 *
Tom Cosgrove4386ead2022-09-29 14:40:21 +0100263 * \p A and \p B may alias each other, if \p AN_limbs == \p B_limbs. They may
264 * not alias \p N (since they must be in canonical form, they cannot == \p N).
Tom Cosgroveea45c1d2022-09-20 13:17:51 +0100265 *
Tom Cosgroveb4964862022-08-30 11:57:22 +0100266 * \param[out] X The destination MPI, as a little-endian array of
267 * length \p AN_limbs.
268 * On successful completion, X contains the result of
Tom Cosgrove630110a2022-08-31 17:09:29 +0100269 * the multiplication `A * B * R^-1` mod N where
270 * `R = 2^(biL*AN_limbs)`.
Tom Cosgroveb4964862022-08-30 11:57:22 +0100271 * \param[in] A Little-endian presentation of first operand.
Tom Cosgrove5dd97e62022-08-30 14:31:49 +0100272 * Must have the same number of limbs as \p N.
Tom Cosgroveb4964862022-08-30 11:57:22 +0100273 * \param[in] B Little-endian presentation of second operand.
274 * \param[in] B_limbs The number of limbs in \p B.
Tom Cosgrove5dd97e62022-08-30 14:31:49 +0100275 * Must be <= \p AN_limbs.
Tom Cosgroveb4964862022-08-30 11:57:22 +0100276 * \param[in] N Little-endian presentation of the modulus.
Tom Cosgrove5dd97e62022-08-30 14:31:49 +0100277 * This must be odd, and have exactly the same number
278 * of limbs as \p A.
Tom Cosgrove6da3a3b2022-09-29 17:20:18 +0100279 * It may alias \p X, but must not alias or otherwise
280 * overlap any of the other parameters.
Tom Cosgrove5dd97e62022-08-30 14:31:49 +0100281 * \param[in] AN_limbs The number of limbs in \p X, \p A and \p N.
Tom Cosgrove630110a2022-08-31 17:09:29 +0100282 * \param mm The Montgomery constant for \p N: -N^-1 mod 2^biL.
Tom Cosgroveb7438d12022-09-15 15:05:59 +0100283 * This can be calculated by `mbedtls_mpi_core_montmul_init()`.
Tom Cosgroveb4964862022-08-30 11:57:22 +0100284 * \param[in,out] T Temporary storage of size at least 2*AN_limbs+1 limbs.
285 * Its initial content is unused and
286 * its final content is indeterminate.
Tom Cosgrove4386ead2022-09-29 14:40:21 +0100287 * It must not alias or otherwise overlap any of the
288 * other parameters.
Tom Cosgroveb4964862022-08-30 11:57:22 +0100289 */
290void mbedtls_mpi_core_montmul( mbedtls_mpi_uint *X,
291 const mbedtls_mpi_uint *A,
292 const mbedtls_mpi_uint *B, size_t B_limbs,
293 const mbedtls_mpi_uint *N, size_t AN_limbs,
294 mbedtls_mpi_uint mm, mbedtls_mpi_uint *T );
295
Gabor Mezeib9030702022-07-18 23:09:45 +0200296#endif /* MBEDTLS_BIGNUM_CORE_H */