blob: d64111b18e1088da533693062ecf6556145d4fd4 [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 Peskine7aab2fb2022-09-27 13:19:13 +02009 * This module is about processing non-negative integers with a fixed upper
10 * bound that's of the form 2^#biL-1. Many operations treat these numbers
11 * as the principal representation of a number modulo 2^#biL or a smaller
12 * bound.
13 *
Gilles Peskine7f887bd2022-09-27 13:12:30 +020014 */
15
16/*
Gabor Mezeib9030702022-07-18 23:09:45 +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
33#ifndef MBEDTLS_BIGNUM_CORE_H
34#define MBEDTLS_BIGNUM_CORE_H
35
36#include "common.h"
37
38#if defined(MBEDTLS_BIGNUM_C)
39#include "mbedtls/bignum.h"
40#endif
41
Tom Cosgrove5eefc3d2022-08-31 17:16:50 +010042#define ciL ( sizeof(mbedtls_mpi_uint) ) /* chars in limb */
43#define biL ( ciL << 3 ) /* bits in limb */
44#define biH ( ciL << 2 ) /* half limb size */
45
46/*
47 * Convert between bits/chars and number of limbs
48 * Divide first in order to avoid potential overflows
49 */
50#define BITS_TO_LIMBS(i) ( (i) / biL + ( (i) % biL != 0 ) )
51#define CHARS_TO_LIMBS(i) ( (i) / ciL + ( (i) % ciL != 0 ) )
52/* Get a specific byte, without range checks. */
53#define GET_BYTE( X, i ) \
54 ( ( (X)[(i) / ciL] >> ( ( (i) % ciL ) * 8 ) ) & 0xff )
55
Gabor Mezei37b06362022-08-02 17:22:18 +020056/** Count leading zero bits in a given integer.
57 *
Janos Follathb7a88ec2022-08-19 12:24:40 +010058 * \param a Integer to count leading zero bits.
Gabor Mezei37b06362022-08-02 17:22:18 +020059 *
Janos Follathb7a88ec2022-08-19 12:24:40 +010060 * \return The number of leading zero bits in \p a.
Gabor Mezei37b06362022-08-02 17:22:18 +020061 */
Janos Follath2e328c82022-08-22 11:19:10 +010062size_t mbedtls_mpi_core_clz( mbedtls_mpi_uint a );
Janos Follath4670f882022-07-21 18:25:42 +010063
Janos Follatha95f2042022-08-19 12:09:17 +010064/** Return the minimum number of bits required to represent the value held
Janos Follath63184682022-08-11 17:42:59 +010065 * in the MPI.
66 *
Janos Follathb7a88ec2022-08-19 12:24:40 +010067 * \note This function returns 0 if all the limbs of \p A are 0.
Gabor Mezei37b06362022-08-02 17:22:18 +020068 *
Janos Follathb7a88ec2022-08-19 12:24:40 +010069 * \param[in] A The address of the MPI.
Janos Follathc4596412022-08-22 10:01:27 +010070 * \param A_limbs The number of limbs of \p A.
Gabor Mezei37b06362022-08-02 17:22:18 +020071 *
Janos Follathb7a88ec2022-08-19 12:24:40 +010072 * \return The number of bits in \p A.
Gabor Mezei37b06362022-08-02 17:22:18 +020073 */
Janos Follathaf3f39c2022-08-22 09:06:32 +010074size_t mbedtls_mpi_core_bitlen( const mbedtls_mpi_uint *A, size_t A_limbs );
Janos Follath4670f882022-07-21 18:25:42 +010075
Gabor Mezei37b06362022-08-02 17:22:18 +020076/** Convert a big-endian byte array aligned to the size of mbedtls_mpi_uint
77 * into the storage form used by mbedtls_mpi.
78 *
Janos Follathb7a88ec2022-08-19 12:24:40 +010079 * \param[in,out] A The address of the MPI.
Janos Follathc4596412022-08-22 10:01:27 +010080 * \param A_limbs The number of limbs of \p A.
Gabor Mezei37b06362022-08-02 17:22:18 +020081 */
Janos Follathb7a88ec2022-08-19 12:24:40 +010082void mbedtls_mpi_core_bigendian_to_host( mbedtls_mpi_uint *A,
Janos Follathc4596412022-08-22 10:01:27 +010083 size_t A_limbs );
Janos Follath4670f882022-07-21 18:25:42 +010084
Janos Follathaf3f39c2022-08-22 09:06:32 +010085/** Import X from unsigned binary data, little-endian.
Gabor Mezei37b06362022-08-02 17:22:18 +020086 *
Janos Follath63184682022-08-11 17:42:59 +010087 * The MPI needs to have enough limbs to store the full value (including any
88 * most significant zero bytes in the input).
Gabor Mezei37b06362022-08-02 17:22:18 +020089 *
Janos Follathb7a88ec2022-08-19 12:24:40 +010090 * \param[out] X The address of the MPI.
91 * \param X_limbs The number of limbs of \p X.
92 * \param[in] input The input buffer to import from.
93 * \param input_length The length bytes of \p input.
Gabor Mezei37b06362022-08-02 17:22:18 +020094 *
95 * \return \c 0 if successful.
96 * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p X isn't
Janos Follathb7a88ec2022-08-19 12:24:40 +010097 * large enough to hold the value in \p input.
Gabor Mezei37b06362022-08-02 17:22:18 +020098 */
Gabor Mezeib9030702022-07-18 23:09:45 +020099int mbedtls_mpi_core_read_le( mbedtls_mpi_uint *X,
Janos Follathb7a88ec2022-08-19 12:24:40 +0100100 size_t X_limbs,
101 const unsigned char *input,
102 size_t input_length );
Gabor Mezeib9030702022-07-18 23:09:45 +0200103
Janos Follathaf3f39c2022-08-22 09:06:32 +0100104/** Import X from unsigned binary data, big-endian.
Gabor Mezei37b06362022-08-02 17:22:18 +0200105 *
Janos Follath63184682022-08-11 17:42:59 +0100106 * The MPI needs to have enough limbs to store the full value (including any
107 * most significant zero bytes in the input).
Gabor Mezei37b06362022-08-02 17:22:18 +0200108 *
Janos Follathb7a88ec2022-08-19 12:24:40 +0100109 * \param[out] X The address of the MPI.
110 * May only be #NULL if \X_limbs is 0 and \p input_length
111 * is 0.
112 * \param X_limbs The number of limbs of \p X.
113 * \param[in] input The input buffer to import from.
114 * May only be #NULL if \p input_length is 0.
115 * \param input_length The length in bytes of \p input.
Gabor Mezei37b06362022-08-02 17:22:18 +0200116 *
117 * \return \c 0 if successful.
118 * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p X isn't
Janos Follathb7a88ec2022-08-19 12:24:40 +0100119 * large enough to hold the value in \p input.
Gabor Mezei37b06362022-08-02 17:22:18 +0200120 */
Gabor Mezeib9030702022-07-18 23:09:45 +0200121int mbedtls_mpi_core_read_be( mbedtls_mpi_uint *X,
Janos Follathb7a88ec2022-08-19 12:24:40 +0100122 size_t X_limbs,
123 const unsigned char *input,
124 size_t input_length );
Gabor Mezeib9030702022-07-18 23:09:45 +0200125
Janos Follathaf3f39c2022-08-22 09:06:32 +0100126/** Export A into unsigned binary data, little-endian.
Gabor Mezei37b06362022-08-02 17:22:18 +0200127 *
Janos Follathb7a88ec2022-08-19 12:24:40 +0100128 * \note If \p output is shorter than \p A the export is still successful if the
129 * value held in \p A fits in the buffer (that is, if enough of the most
130 * significant bytes of \p A are 0).
Janos Follath63184682022-08-11 17:42:59 +0100131 *
Janos Follathb7a88ec2022-08-19 12:24:40 +0100132 * \param[in] A The address of the MPI.
133 * \param A_limbs The number of limbs of \p A.
134 * \param[out] output The output buffer to export to.
135 * \param output_length The length in bytes of \p output.
Gabor Mezei37b06362022-08-02 17:22:18 +0200136 *
137 * \return \c 0 if successful.
Janos Follathb7a88ec2022-08-19 12:24:40 +0100138 * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p output isn't
139 * large enough to hold the value of \p A.
Gabor Mezei37b06362022-08-02 17:22:18 +0200140 */
Janos Follathb7a88ec2022-08-19 12:24:40 +0100141int mbedtls_mpi_core_write_le( const mbedtls_mpi_uint *A,
142 size_t A_limbs,
143 unsigned char *output,
144 size_t output_length );
Gabor Mezeib9030702022-07-18 23:09:45 +0200145
Janos Follathaf3f39c2022-08-22 09:06:32 +0100146/** Export A into unsigned binary data, big-endian.
Gabor Mezei37b06362022-08-02 17:22:18 +0200147 *
Janos Follathb7a88ec2022-08-19 12:24:40 +0100148 * \note If \p output is shorter than \p A the export is still successful if the
149 * value held in \p A fits in the buffer (that is, if enough of the most
150 * significant bytes of \p A are 0).
Janos Follath63184682022-08-11 17:42:59 +0100151 *
Janos Follathb7a88ec2022-08-19 12:24:40 +0100152 * \param[in] A The address of the MPI.
153 * \param A_limbs The number of limbs of \p A.
154 * \param[out] output The output buffer to export to.
155 * \param output_length The length in bytes of \p output.
Gabor Mezei37b06362022-08-02 17:22:18 +0200156 *
157 * \return \c 0 if successful.
Janos Follathb7a88ec2022-08-19 12:24:40 +0100158 * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p output isn't
159 * large enough to hold the value of \p A.
Gabor Mezei37b06362022-08-02 17:22:18 +0200160 */
Janos Follathb7a88ec2022-08-19 12:24:40 +0100161int mbedtls_mpi_core_write_be( const mbedtls_mpi_uint *A,
162 size_t A_limbs,
163 unsigned char *output,
164 size_t output_length );
Gabor Mezeib9030702022-07-18 23:09:45 +0200165
Tom Cosgrove90c426b2022-08-23 16:15:19 +0100166/**
Tom Cosgrove5c0e8102022-09-15 15:46:10 +0100167 * \brief Conditional addition of two fixed-size large unsigned integers,
Tom Cosgroved932de82022-08-25 16:43:43 +0100168 * returning the carry.
Hanno Becker71f4b0d2022-08-23 12:09:35 +0100169 *
170 * Functionally equivalent to
171 *
172 * ```
173 * if( cond )
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100174 * X += A;
Hanno Becker71f4b0d2022-08-23 12:09:35 +0100175 * return carry;
176 * ```
177 *
Tom Cosgrove47828232022-09-20 13:51:50 +0100178 * This function operates modulo `2^(biL*limbs)`.
179 *
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100180 * \param[in,out] X The pointer to the (little-endian) array
Tom Cosgrove72594632022-08-24 11:51:58 +0100181 * representing the bignum to accumulate onto.
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100182 * \param[in] A The pointer to the (little-endian) array
Tom Cosgrove72594632022-08-24 11:51:58 +0100183 * representing the bignum to conditionally add
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100184 * to \p X. This may be aliased to \p X but may not
Tom Cosgroveed43c6c2022-08-31 11:35:00 +0100185 * overlap otherwise.
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100186 * \param limbs Number of limbs of \p X and \p A.
Tom Cosgrove72594632022-08-24 11:51:58 +0100187 * \param cond Condition bit dictating whether addition should
188 * happen or not. This must be \c 0 or \c 1.
Hanno Becker71f4b0d2022-08-23 12:09:35 +0100189 *
Tom Cosgroveecbb1242022-08-25 10:13:44 +0100190 * \warning If \p cond is neither 0 nor 1, the result of this function
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100191 * is unspecified, and the resulting value in \p X might be
192 * neither its original value nor \p X + \p A.
Tom Cosgrove72594632022-08-24 11:51:58 +0100193 *
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100194 * \return 1 if `X + cond * A >= 2^(biL*limbs)`, 0 otherwise.
Hanno Becker71f4b0d2022-08-23 12:09:35 +0100195 */
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100196mbedtls_mpi_uint mbedtls_mpi_core_add_if( mbedtls_mpi_uint *X,
197 const mbedtls_mpi_uint *A,
Tom Cosgrove72594632022-08-24 11:51:58 +0100198 size_t limbs,
Tom Cosgrove90c426b2022-08-23 16:15:19 +0100199 unsigned cond );
Hanno Becker71f4b0d2022-08-23 12:09:35 +0100200
Tom Cosgroveb4964862022-08-30 11:57:22 +0100201/**
Tom Cosgrove5c0e8102022-09-15 15:46:10 +0100202 * \brief Subtract two fixed-size large unsigned integers, returning the borrow.
Tom Cosgroveb4964862022-08-30 11:57:22 +0100203 *
Tom Cosgrove5dd97e62022-08-30 14:31:49 +0100204 * Calculate `A - B` where \p A and \p B have the same size.
Tom Cosgrove630110a2022-08-31 17:09:29 +0100205 * This function operates modulo `2^(biL*limbs)` and returns the carry
Tom Cosgroveb4964862022-08-30 11:57:22 +0100206 * (1 if there was a wraparound, i.e. if `A < B`, and 0 otherwise).
207 *
Tom Cosgrove5dd97e62022-08-30 14:31:49 +0100208 * \p X may be aliased to \p A or \p B, or even both, but may not overlap
209 * either otherwise.
Tom Cosgroveb4964862022-08-30 11:57:22 +0100210 *
211 * \param[out] X The result of the subtraction.
212 * \param[in] A Little-endian presentation of left operand.
213 * \param[in] B Little-endian presentation of right operand.
214 * \param limbs Number of limbs of \p X, \p A and \p B.
215 *
216 * \return 1 if `A < B`.
217 * 0 if `A >= B`.
218 */
219mbedtls_mpi_uint mbedtls_mpi_core_sub( mbedtls_mpi_uint *X,
220 const mbedtls_mpi_uint *A,
221 const mbedtls_mpi_uint *B,
222 size_t limbs );
223
224/**
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100225 * \brief Perform a fixed-size multiply accumulate operation: X += b * A
Tom Cosgroveb4964862022-08-30 11:57:22 +0100226 *
Tom Cosgroveb0b77e12022-09-20 13:33:40 +0100227 * \p X may be aliased to \p A (when \p X_limbs == \p A_limbs), but may not
228 * otherwise overlap.
229 *
Tom Cosgrove47828232022-09-20 13:51:50 +0100230 * This function operates modulo `2^(biL*X_limbs)`.
231 *
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100232 * \param[in,out] X The pointer to the (little-endian) array
Tom Cosgroveb4964862022-08-30 11:57:22 +0100233 * representing the bignum to accumulate onto.
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100234 * \param X_limbs The number of limbs of \p X. This must be
235 * at least \p A_limbs.
236 * \param[in] A The pointer to the (little-endian) array
Tom Cosgroveb4964862022-08-30 11:57:22 +0100237 * representing the bignum to multiply with.
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100238 * This may be aliased to \p X but may not overlap
Tom Cosgroveed43c6c2022-08-31 11:35:00 +0100239 * otherwise.
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100240 * \param A_limbs The number of limbs of \p A.
241 * \param b X scalar to multiply with.
Tom Cosgroveb4964862022-08-30 11:57:22 +0100242 *
243 * \return The carry at the end of the operation.
244 */
Tom Cosgrove3bd7bc32022-09-15 15:55:07 +0100245mbedtls_mpi_uint mbedtls_mpi_core_mla( mbedtls_mpi_uint *X, size_t X_limbs,
246 const mbedtls_mpi_uint *A, size_t A_limbs,
247 mbedtls_mpi_uint b );
Tom Cosgroveb4964862022-08-30 11:57:22 +0100248
249/**
250 * \brief Calculate initialisation value for fast Montgomery modular
251 * multiplication
252 *
253 * \param[in] N Little-endian presentation of the modulus. This must have
254 * at least one limb.
255 *
256 * \return The initialisation value for fast Montgomery modular multiplication
257 */
Tom Cosgroveb7438d12022-09-15 15:05:59 +0100258mbedtls_mpi_uint mbedtls_mpi_core_montmul_init( const mbedtls_mpi_uint *N );
Tom Cosgroveb4964862022-08-30 11:57:22 +0100259
260/**
Tom Cosgrove4386ead2022-09-29 14:40:21 +0100261 * \brief Montgomery multiplication: X = A * B * R^-1 mod N (HAC 14.36)
262 *
263 * \p A and \p B must be in canonical form. That is, < \p N.
Tom Cosgroveb4964862022-08-30 11:57:22 +0100264 *
Tom Cosgroveea45c1d2022-09-20 13:17:51 +0100265 * \p X may be aliased to \p A or \p N, or even \p B (if \p AN_limbs ==
266 * \p B_limbs) but may not overlap any parameters otherwise.
267 *
Tom Cosgrove4386ead2022-09-29 14:40:21 +0100268 * \p A and \p B may alias each other, if \p AN_limbs == \p B_limbs. They may
269 * not alias \p N (since they must be in canonical form, they cannot == \p N).
Tom Cosgroveea45c1d2022-09-20 13:17:51 +0100270 *
Tom Cosgroveb4964862022-08-30 11:57:22 +0100271 * \param[out] X The destination MPI, as a little-endian array of
272 * length \p AN_limbs.
273 * On successful completion, X contains the result of
Tom Cosgrove630110a2022-08-31 17:09:29 +0100274 * the multiplication `A * B * R^-1` mod N where
275 * `R = 2^(biL*AN_limbs)`.
Tom Cosgroveb4964862022-08-30 11:57:22 +0100276 * \param[in] A Little-endian presentation of first operand.
Tom Cosgrove5dd97e62022-08-30 14:31:49 +0100277 * Must have the same number of limbs as \p N.
Tom Cosgroveb4964862022-08-30 11:57:22 +0100278 * \param[in] B Little-endian presentation of second operand.
279 * \param[in] B_limbs The number of limbs in \p B.
Tom Cosgrove5dd97e62022-08-30 14:31:49 +0100280 * Must be <= \p AN_limbs.
Tom Cosgroveb4964862022-08-30 11:57:22 +0100281 * \param[in] N Little-endian presentation of the modulus.
Tom Cosgrove5dd97e62022-08-30 14:31:49 +0100282 * This must be odd, and have exactly the same number
283 * of limbs as \p A.
Tom Cosgrove6da3a3b2022-09-29 17:20:18 +0100284 * It may alias \p X, but must not alias or otherwise
285 * overlap any of the other parameters.
Tom Cosgrove5dd97e62022-08-30 14:31:49 +0100286 * \param[in] AN_limbs The number of limbs in \p X, \p A and \p N.
Tom Cosgrove630110a2022-08-31 17:09:29 +0100287 * \param mm The Montgomery constant for \p N: -N^-1 mod 2^biL.
Tom Cosgroveb7438d12022-09-15 15:05:59 +0100288 * This can be calculated by `mbedtls_mpi_core_montmul_init()`.
Tom Cosgroveb4964862022-08-30 11:57:22 +0100289 * \param[in,out] T Temporary storage of size at least 2*AN_limbs+1 limbs.
290 * Its initial content is unused and
291 * its final content is indeterminate.
Tom Cosgrove4386ead2022-09-29 14:40:21 +0100292 * It must not alias or otherwise overlap any of the
293 * other parameters.
Tom Cosgroveb4964862022-08-30 11:57:22 +0100294 */
295void mbedtls_mpi_core_montmul( mbedtls_mpi_uint *X,
296 const mbedtls_mpi_uint *A,
297 const mbedtls_mpi_uint *B, size_t B_limbs,
298 const mbedtls_mpi_uint *N, size_t AN_limbs,
299 mbedtls_mpi_uint mm, mbedtls_mpi_uint *T );
300
Gabor Mezeib9030702022-07-18 23:09:45 +0200301#endif /* MBEDTLS_BIGNUM_CORE_H */