blob: 746c1d9505a3d59e66982a6b600b5bbd0bb4a0bb [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
Janos Follathaf3f39c2022-08-22 09:06:32 +010077/** Import X from unsigned binary data, little-endian.
Gabor Mezei37b06362022-08-02 17:22:18 +020078 *
Janos Follath63184682022-08-11 17:42:59 +010079 * The MPI needs to have enough limbs to store the full value (including any
80 * most significant zero bytes in the input).
Gabor Mezei37b06362022-08-02 17:22:18 +020081 *
Janos Follathb7a88ec2022-08-19 12:24:40 +010082 * \param[out] X The address of the MPI.
83 * \param X_limbs The number of limbs of \p X.
84 * \param[in] input The input buffer to import from.
85 * \param input_length The length bytes of \p input.
Gabor Mezei37b06362022-08-02 17:22:18 +020086 *
87 * \return \c 0 if successful.
88 * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p X isn't
Janos Follathb7a88ec2022-08-19 12:24:40 +010089 * large enough to hold the value in \p input.
Gabor Mezei37b06362022-08-02 17:22:18 +020090 */
Gabor Mezeib9030702022-07-18 23:09:45 +020091int mbedtls_mpi_core_read_le( mbedtls_mpi_uint *X,
Janos Follathb7a88ec2022-08-19 12:24:40 +010092 size_t X_limbs,
93 const unsigned char *input,
94 size_t input_length );
Gabor Mezeib9030702022-07-18 23:09:45 +020095
Janos Follathaf3f39c2022-08-22 09:06:32 +010096/** Import X from unsigned binary data, big-endian.
Gabor Mezei37b06362022-08-02 17:22:18 +020097 *
Janos Follath63184682022-08-11 17:42:59 +010098 * The MPI needs to have enough limbs to store the full value (including any
99 * most significant zero bytes in the input).
Gabor Mezei37b06362022-08-02 17:22:18 +0200100 *
Janos Follathb7a88ec2022-08-19 12:24:40 +0100101 * \param[out] X The address of the MPI.
102 * May only be #NULL if \X_limbs is 0 and \p input_length
103 * is 0.
104 * \param X_limbs The number of limbs of \p X.
105 * \param[in] input The input buffer to import from.
106 * May only be #NULL if \p input_length is 0.
107 * \param input_length The length in bytes of \p input.
Gabor Mezei37b06362022-08-02 17:22:18 +0200108 *
109 * \return \c 0 if successful.
110 * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p X isn't
Janos Follathb7a88ec2022-08-19 12:24:40 +0100111 * large enough to hold the value in \p input.
Gabor Mezei37b06362022-08-02 17:22:18 +0200112 */
Gabor Mezeib9030702022-07-18 23:09:45 +0200113int mbedtls_mpi_core_read_be( mbedtls_mpi_uint *X,
Janos Follathb7a88ec2022-08-19 12:24:40 +0100114 size_t X_limbs,
115 const unsigned char *input,
116 size_t input_length );
Gabor Mezeib9030702022-07-18 23:09:45 +0200117
Janos Follathaf3f39c2022-08-22 09:06:32 +0100118/** Export A into unsigned binary data, little-endian.
Gabor Mezei37b06362022-08-02 17:22:18 +0200119 *
Janos Follathb7a88ec2022-08-19 12:24:40 +0100120 * \note If \p output is shorter than \p A the export is still successful if the
121 * value held in \p A fits in the buffer (that is, if enough of the most
122 * significant bytes of \p A are 0).
Janos Follath63184682022-08-11 17:42:59 +0100123 *
Janos Follathb7a88ec2022-08-19 12:24:40 +0100124 * \param[in] A The address of the MPI.
125 * \param A_limbs The number of limbs of \p A.
126 * \param[out] output The output buffer to export to.
127 * \param output_length The length in bytes of \p output.
Gabor Mezei37b06362022-08-02 17:22:18 +0200128 *
129 * \return \c 0 if successful.
Janos Follathb7a88ec2022-08-19 12:24:40 +0100130 * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p output isn't
131 * large enough to hold the value of \p A.
Gabor Mezei37b06362022-08-02 17:22:18 +0200132 */
Janos Follathb7a88ec2022-08-19 12:24:40 +0100133int mbedtls_mpi_core_write_le( const mbedtls_mpi_uint *A,
134 size_t A_limbs,
135 unsigned char *output,
136 size_t output_length );
Gabor Mezeib9030702022-07-18 23:09:45 +0200137
Janos Follathaf3f39c2022-08-22 09:06:32 +0100138/** Export A into unsigned binary data, big-endian.
Gabor Mezei37b06362022-08-02 17:22:18 +0200139 *
Janos Follathb7a88ec2022-08-19 12:24:40 +0100140 * \note If \p output is shorter than \p A the export is still successful if the
141 * value held in \p A fits in the buffer (that is, if enough of the most
142 * significant bytes of \p A are 0).
Janos Follath63184682022-08-11 17:42:59 +0100143 *
Janos Follathb7a88ec2022-08-19 12:24:40 +0100144 * \param[in] A The address of the MPI.
145 * \param A_limbs The number of limbs of \p A.
146 * \param[out] output The output buffer to export to.
147 * \param output_length The length in bytes of \p output.
Gabor Mezei37b06362022-08-02 17:22:18 +0200148 *
149 * \return \c 0 if successful.
Janos Follathb7a88ec2022-08-19 12:24:40 +0100150 * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p output isn't
151 * large enough to hold the value of \p A.
Gabor Mezei37b06362022-08-02 17:22:18 +0200152 */
Janos Follathb7a88ec2022-08-19 12:24:40 +0100153int mbedtls_mpi_core_write_be( const mbedtls_mpi_uint *A,
154 size_t A_limbs,
155 unsigned char *output,
156 size_t output_length );
Gabor Mezeib9030702022-07-18 23:09:45 +0200157
Tom Cosgrove90c426b2022-08-23 16:15:19 +0100158/**
Tom Cosgroved932de82022-08-25 16:43:43 +0100159 * \brief Conditional addition of two known-size large unsigned integers,
160 * returning the carry.
Hanno Becker71f4b0d2022-08-23 12:09:35 +0100161 *
162 * Functionally equivalent to
163 *
164 * ```
165 * if( cond )
Tom Cosgrove72594632022-08-24 11:51:58 +0100166 * A += B;
Hanno Becker71f4b0d2022-08-23 12:09:35 +0100167 * return carry;
168 * ```
169 *
Tom Cosgrove72594632022-08-24 11:51:58 +0100170 * \param[in,out] A The pointer to the (little-endian) array
171 * representing the bignum to accumulate onto.
172 * \param[in] B The pointer to the (little-endian) array
173 * representing the bignum to conditionally add
Tom Cosgroveed43c6c2022-08-31 11:35:00 +0100174 * to \p A. This may be aliased to \p A but may not
175 * overlap otherwise.
Tom Cosgrove72594632022-08-24 11:51:58 +0100176 * \param limbs Number of limbs of \p A and \p B.
177 * \param cond Condition bit dictating whether addition should
178 * happen or not. This must be \c 0 or \c 1.
Hanno Becker71f4b0d2022-08-23 12:09:35 +0100179 *
Tom Cosgroveecbb1242022-08-25 10:13:44 +0100180 * \warning If \p cond is neither 0 nor 1, the result of this function
Tom Cosgrove72594632022-08-24 11:51:58 +0100181 * is unspecified, and the resulting value in \p A might be
182 * neither its original value nor \p A + \p B.
183 *
Tom Cosgrove630110a2022-08-31 17:09:29 +0100184 * \return 1 if `A + cond * B >= 2^(biL*limbs)`, 0 otherwise.
Hanno Becker71f4b0d2022-08-23 12:09:35 +0100185 */
Tom Cosgrove72594632022-08-24 11:51:58 +0100186mbedtls_mpi_uint mbedtls_mpi_core_add_if( mbedtls_mpi_uint *A,
187 const mbedtls_mpi_uint *B,
188 size_t limbs,
Tom Cosgrove90c426b2022-08-23 16:15:19 +0100189 unsigned cond );
Hanno Becker71f4b0d2022-08-23 12:09:35 +0100190
Tom Cosgroveb4964862022-08-30 11:57:22 +0100191/**
192 * \brief Subtract two known-size large unsigned integers, returning the borrow.
193 *
Tom Cosgrove5dd97e62022-08-30 14:31:49 +0100194 * Calculate `A - B` where \p A and \p B have the same size.
Tom Cosgrove630110a2022-08-31 17:09:29 +0100195 * This function operates modulo `2^(biL*limbs)` and returns the carry
Tom Cosgroveb4964862022-08-30 11:57:22 +0100196 * (1 if there was a wraparound, i.e. if `A < B`, and 0 otherwise).
197 *
Tom Cosgrove5dd97e62022-08-30 14:31:49 +0100198 * \p X may be aliased to \p A or \p B, or even both, but may not overlap
199 * either otherwise.
Tom Cosgroveb4964862022-08-30 11:57:22 +0100200 *
201 * \param[out] X The result of the subtraction.
202 * \param[in] A Little-endian presentation of left operand.
203 * \param[in] B Little-endian presentation of right operand.
204 * \param limbs Number of limbs of \p X, \p A and \p B.
205 *
206 * \return 1 if `A < B`.
207 * 0 if `A >= B`.
208 */
209mbedtls_mpi_uint mbedtls_mpi_core_sub( mbedtls_mpi_uint *X,
210 const mbedtls_mpi_uint *A,
211 const mbedtls_mpi_uint *B,
212 size_t limbs );
213
214/**
215 * \brief Perform a known-size multiply accumulate operation: A += c * B
216 *
217 * \param[in,out] A The pointer to the (little-endian) array
218 * representing the bignum to accumulate onto.
219 * \param A_limbs The number of limbs of \p A. This must be
220 * at least \p B_limbs.
221 * \param[in] B The pointer to the (little-endian) array
222 * representing the bignum to multiply with.
Tom Cosgroveed43c6c2022-08-31 11:35:00 +0100223 * This may be aliased to \p A but may not overlap
224 * otherwise.
Tom Cosgroveb4964862022-08-30 11:57:22 +0100225 * \param B_limbs The number of limbs of \p B.
226 * \param c A scalar to multiply with.
227 *
228 * \return The carry at the end of the operation.
229 */
230mbedtls_mpi_uint mbedtls_mpi_core_mla( mbedtls_mpi_uint *A, size_t A_limbs,
231 const mbedtls_mpi_uint *B, size_t B_limbs,
232 mbedtls_mpi_uint c );
233
234/**
235 * \brief Calculate initialisation value for fast Montgomery modular
236 * multiplication
237 *
238 * \param[in] N Little-endian presentation of the modulus. This must have
239 * at least one limb.
240 *
241 * \return The initialisation value for fast Montgomery modular multiplication
242 */
Tom Cosgroveb7438d12022-09-15 15:05:59 +0100243mbedtls_mpi_uint mbedtls_mpi_core_montmul_init( const mbedtls_mpi_uint *N );
Tom Cosgroveb4964862022-08-30 11:57:22 +0100244
245/**
246 * \brief Montgomery multiplication: X = A * B * R^-1 mod N (HAC 14.36)
247 *
248 * \param[out] X The destination MPI, as a little-endian array of
249 * length \p AN_limbs.
250 * On successful completion, X contains the result of
Tom Cosgrove630110a2022-08-31 17:09:29 +0100251 * the multiplication `A * B * R^-1` mod N where
252 * `R = 2^(biL*AN_limbs)`.
Tom Cosgroveb4964862022-08-30 11:57:22 +0100253 * \param[in] A Little-endian presentation of first operand.
Tom Cosgrove5dd97e62022-08-30 14:31:49 +0100254 * Must have the same number of limbs as \p N.
Tom Cosgroveb4964862022-08-30 11:57:22 +0100255 * \param[in] B Little-endian presentation of second operand.
256 * \param[in] B_limbs The number of limbs in \p B.
Tom Cosgrove5dd97e62022-08-30 14:31:49 +0100257 * Must be <= \p AN_limbs.
Tom Cosgroveb4964862022-08-30 11:57:22 +0100258 * \param[in] N Little-endian presentation of the modulus.
Tom Cosgrove5dd97e62022-08-30 14:31:49 +0100259 * This must be odd, and have exactly the same number
260 * of limbs as \p A.
261 * \param[in] AN_limbs The number of limbs in \p X, \p A and \p N.
Tom Cosgrove630110a2022-08-31 17:09:29 +0100262 * \param mm The Montgomery constant for \p N: -N^-1 mod 2^biL.
Tom Cosgroveb7438d12022-09-15 15:05:59 +0100263 * This can be calculated by `mbedtls_mpi_core_montmul_init()`.
Tom Cosgroveb4964862022-08-30 11:57:22 +0100264 * \param[in,out] T Temporary storage of size at least 2*AN_limbs+1 limbs.
265 * Its initial content is unused and
266 * its final content is indeterminate.
Tom Cosgrove818d9922022-09-15 14:58:10 +0100267 * It must not overlap any of the other parameters.
Tom Cosgroveb4964862022-08-30 11:57:22 +0100268 */
269void mbedtls_mpi_core_montmul( mbedtls_mpi_uint *X,
270 const mbedtls_mpi_uint *A,
271 const mbedtls_mpi_uint *B, size_t B_limbs,
272 const mbedtls_mpi_uint *N, size_t AN_limbs,
273 mbedtls_mpi_uint mm, mbedtls_mpi_uint *T );
274
Gabor Mezeib9030702022-07-18 23:09:45 +0200275#endif /* MBEDTLS_BIGNUM_CORE_H */