blob: 62d0151e5fa945fd9a5852d20f5f4cae4bfc64cc [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
Gabor Mezei37b06362022-08-02 17:22:18 +020034/** Count leading zero bits in a given integer.
35 *
Janos Follathb7a88ec2022-08-19 12:24:40 +010036 * \param a Integer to count leading zero bits.
Gabor Mezei37b06362022-08-02 17:22:18 +020037 *
Janos Follathb7a88ec2022-08-19 12:24:40 +010038 * \return The number of leading zero bits in \p a.
Gabor Mezei37b06362022-08-02 17:22:18 +020039 */
Janos Follath2e328c82022-08-22 11:19:10 +010040size_t mbedtls_mpi_core_clz( mbedtls_mpi_uint a );
Janos Follath4670f882022-07-21 18:25:42 +010041
Janos Follatha95f2042022-08-19 12:09:17 +010042/** Return the minimum number of bits required to represent the value held
Janos Follath63184682022-08-11 17:42:59 +010043 * in the MPI.
44 *
Janos Follathb7a88ec2022-08-19 12:24:40 +010045 * \note This function returns 0 if all the limbs of \p A are 0.
Gabor Mezei37b06362022-08-02 17:22:18 +020046 *
Janos Follathb7a88ec2022-08-19 12:24:40 +010047 * \param[in] A The address of the MPI.
Janos Follathc4596412022-08-22 10:01:27 +010048 * \param A_limbs The number of limbs of \p A.
Gabor Mezei37b06362022-08-02 17:22:18 +020049 *
Janos Follathb7a88ec2022-08-19 12:24:40 +010050 * \return The number of bits in \p A.
Gabor Mezei37b06362022-08-02 17:22:18 +020051 */
Janos Follathaf3f39c2022-08-22 09:06:32 +010052size_t mbedtls_mpi_core_bitlen( const mbedtls_mpi_uint *A, size_t A_limbs );
Janos Follath4670f882022-07-21 18:25:42 +010053
Gabor Mezei37b06362022-08-02 17:22:18 +020054/** Convert a big-endian byte array aligned to the size of mbedtls_mpi_uint
55 * into the storage form used by mbedtls_mpi.
56 *
Janos Follathb7a88ec2022-08-19 12:24:40 +010057 * \param[in,out] A The address of the MPI.
Janos Follathc4596412022-08-22 10:01:27 +010058 * \param A_limbs The number of limbs of \p A.
Gabor Mezei37b06362022-08-02 17:22:18 +020059 */
Janos Follathb7a88ec2022-08-19 12:24:40 +010060void mbedtls_mpi_core_bigendian_to_host( mbedtls_mpi_uint *A,
Janos Follathc4596412022-08-22 10:01:27 +010061 size_t A_limbs );
Janos Follath4670f882022-07-21 18:25:42 +010062
Janos Follathaf3f39c2022-08-22 09:06:32 +010063/** Import X from unsigned binary data, little-endian.
Gabor Mezei37b06362022-08-02 17:22:18 +020064 *
Janos Follath63184682022-08-11 17:42:59 +010065 * The MPI needs to have enough limbs to store the full value (including any
66 * most significant zero bytes in the input).
Gabor Mezei37b06362022-08-02 17:22:18 +020067 *
Janos Follathb7a88ec2022-08-19 12:24:40 +010068 * \param[out] X The address of the MPI.
69 * \param X_limbs The number of limbs of \p X.
70 * \param[in] input The input buffer to import from.
71 * \param input_length The length bytes of \p input.
Gabor Mezei37b06362022-08-02 17:22:18 +020072 *
73 * \return \c 0 if successful.
74 * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p X isn't
Janos Follathb7a88ec2022-08-19 12:24:40 +010075 * large enough to hold the value in \p input.
Gabor Mezei37b06362022-08-02 17:22:18 +020076 */
Gabor Mezeib9030702022-07-18 23:09:45 +020077int mbedtls_mpi_core_read_le( mbedtls_mpi_uint *X,
Janos Follathb7a88ec2022-08-19 12:24:40 +010078 size_t X_limbs,
79 const unsigned char *input,
80 size_t input_length );
Gabor Mezeib9030702022-07-18 23:09:45 +020081
Janos Follathaf3f39c2022-08-22 09:06:32 +010082/** Import X from unsigned binary data, big-endian.
Gabor Mezei37b06362022-08-02 17:22:18 +020083 *
Janos Follath63184682022-08-11 17:42:59 +010084 * The MPI needs to have enough limbs to store the full value (including any
85 * most significant zero bytes in the input).
Gabor Mezei37b06362022-08-02 17:22:18 +020086 *
Janos Follathb7a88ec2022-08-19 12:24:40 +010087 * \param[out] X The address of the MPI.
88 * May only be #NULL if \X_limbs is 0 and \p input_length
89 * is 0.
90 * \param X_limbs The number of limbs of \p X.
91 * \param[in] input The input buffer to import from.
92 * May only be #NULL if \p input_length is 0.
93 * \param input_length The length in 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_be( 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/** Export A into unsigned binary data, little-endian.
Gabor Mezei37b06362022-08-02 17:22:18 +0200105 *
Janos Follathb7a88ec2022-08-19 12:24:40 +0100106 * \note If \p output is shorter than \p A the export is still successful if the
107 * value held in \p A fits in the buffer (that is, if enough of the most
108 * significant bytes of \p A are 0).
Janos Follath63184682022-08-11 17:42:59 +0100109 *
Janos Follathb7a88ec2022-08-19 12:24:40 +0100110 * \param[in] A The address of the MPI.
111 * \param A_limbs The number of limbs of \p A.
112 * \param[out] output The output buffer to export to.
113 * \param output_length The length in bytes of \p output.
Gabor Mezei37b06362022-08-02 17:22:18 +0200114 *
115 * \return \c 0 if successful.
Janos Follathb7a88ec2022-08-19 12:24:40 +0100116 * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p output isn't
117 * large enough to hold the value of \p A.
Gabor Mezei37b06362022-08-02 17:22:18 +0200118 */
Janos Follathb7a88ec2022-08-19 12:24:40 +0100119int mbedtls_mpi_core_write_le( const mbedtls_mpi_uint *A,
120 size_t A_limbs,
121 unsigned char *output,
122 size_t output_length );
Gabor Mezeib9030702022-07-18 23:09:45 +0200123
Janos Follathaf3f39c2022-08-22 09:06:32 +0100124/** Export A into unsigned binary data, big-endian.
Gabor Mezei37b06362022-08-02 17:22:18 +0200125 *
Janos Follathb7a88ec2022-08-19 12:24:40 +0100126 * \note If \p output is shorter than \p A the export is still successful if the
127 * value held in \p A fits in the buffer (that is, if enough of the most
128 * significant bytes of \p A are 0).
Janos Follath63184682022-08-11 17:42:59 +0100129 *
Janos Follathb7a88ec2022-08-19 12:24:40 +0100130 * \param[in] A The address of the MPI.
131 * \param A_limbs The number of limbs of \p A.
132 * \param[out] output The output buffer to export to.
133 * \param output_length The length in bytes of \p output.
Gabor Mezei37b06362022-08-02 17:22:18 +0200134 *
135 * \return \c 0 if successful.
Janos Follathb7a88ec2022-08-19 12:24:40 +0100136 * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p output isn't
137 * large enough to hold the value of \p A.
Gabor Mezei37b06362022-08-02 17:22:18 +0200138 */
Janos Follathb7a88ec2022-08-19 12:24:40 +0100139int mbedtls_mpi_core_write_be( const mbedtls_mpi_uint *A,
140 size_t A_limbs,
141 unsigned char *output,
142 size_t output_length );
Gabor Mezeib9030702022-07-18 23:09:45 +0200143
Janos Follathca5688e2022-08-19 12:05:28 +0100144#define ciL ( sizeof(mbedtls_mpi_uint) ) /* chars in limb */
145#define biL ( ciL << 3 ) /* bits in limb */
146#define biH ( ciL << 2 ) /* half limb size */
Janos Follathd0895702022-08-10 13:32:16 +0100147
148/*
149 * Convert between bits/chars and number of limbs
150 * Divide first in order to avoid potential overflows
151 */
152#define BITS_TO_LIMBS(i) ( (i) / biL + ( (i) % biL != 0 ) )
153#define CHARS_TO_LIMBS(i) ( (i) / ciL + ( (i) % ciL != 0 ) )
154/* Get a specific byte, without range checks. */
155#define GET_BYTE( X, i ) \
Janos Follathca5688e2022-08-19 12:05:28 +0100156 ( ( (X)[(i) / ciL] >> ( ( (i) % ciL ) * 8 ) ) & 0xff )
Janos Follathd0895702022-08-10 13:32:16 +0100157
Tom Cosgrove82d3f1e2022-08-23 12:01:39 +0100158/** Perform a known-size multiply accumulate operation
159 *
160 * Add \p b * \p s to \p d.
161 *
162 * \param[in,out] d The pointer to the (little-endian) array
163 * representing the bignum to accumulate onto.
164 * \param d_len The number of limbs of \p d. This must be
165 * at least \p s_len.
166 * \param[in] s The pointer to the (little-endian) array
167 * representing the bignum to multiply with.
168 * This may be the same as \p d. Otherwise,
169 * it must be disjoint from \p d.
170 * \param s_len The number of limbs of \p s.
171 * \param b A scalar to multiply with.
172 *
173 * \return c The carry at the end of the operation.
174 */
Hanno Becker71f4b0d2022-08-23 12:09:35 +0100175mbedtls_mpi_uint mbedtls_mpi_core_mla( mbedtls_mpi_uint *d, size_t d_len,
Tom Cosgrove82d3f1e2022-08-23 12:01:39 +0100176 const mbedtls_mpi_uint *s, size_t s_len,
177 mbedtls_mpi_uint b );
178
Hanno Becker71f4b0d2022-08-23 12:09:35 +0100179#define MPI_CORE(func) mbedtls_mpi_core_ ## func ## _minimal
180
181/** Montgomery multiplication: X = A * B * R^-1 mod N (HAC 14.36)
182 *
183 * \param[out] X The destination MPI, as a big endian array of length \p n.
184 * On successful completion, X contains the result of
185 * the multiplication A * B * R^-1 mod N where
186 * R = (2^ciL)^n.
187 * \param[in] A Big endian presentation of first operand.
188 * Must have exactly \p n limbs.
189 * \param[in] B Big endian presentation of second operand.
190 * \param[in] B_len The number of limbs in \p B.
191 * \param[in] N Big endian presentation of the modulus.
192 * This must be odd and have exactly \p n limbs.
193 * \param[in] n The number of limbs in \p X, \p A, \p N.
194 * \param mm The Montgomery constant for \p N: -N^-1 mod 2^ciL.
195 * This can be calculated by `mpi_montg_init()`.
196 * \param[in,out] T Temporary storage of size at least 2*n+1 limbs.
197 * Its initial content is unused and
198 * its final content is indeterminate.
199 */
200void MPI_CORE(montmul)( mbedtls_mpi_uint *X,
201 const mbedtls_mpi_uint *A,
202 const mbedtls_mpi_uint *B, size_t B_len,
203 const mbedtls_mpi_uint *N, size_t n,
204 mbedtls_mpi_uint mm, mbedtls_mpi_uint *T );
205
206/**
207 * \brief Perform a known-size multiply accumulate operation
208 *
209 * Add \p b * \p s to \p d.
210 *
211 * \param[in,out] d The pointer to the (little-endian) array
212 * representing the bignum to accumulate onto.
213 * \param d_len The number of limbs of \p d. This must be
214 * at least \p s_len.
215 * \param[in] s The pointer to the (little-endian) array
216 * representing the bignum to multiply with.
217 * This may be the same as \p d. Otherwise,
218 * it must be disjoint from \p d.
219 * \param s_len The number of limbs of \p s.
220 * \param b A scalar to multiply with.
221 *
222 * \return c The carry at the end of the operation.
223 */
224mbedtls_mpi_uint MPI_CORE(mla)( mbedtls_mpi_uint *d, size_t d_len ,
225 const mbedtls_mpi_uint *s, size_t s_len,
226 mbedtls_mpi_uint b );
227
228/**
229 * \brief Subtract two known-size large unsigned integers, returning the borrow.
230 *
231 * Calculate l - r where l and r have the same size.
232 * This function operates modulo (2^ciL)^n and returns the carry
233 * (1 if there was a wraparound, i.e. if `l < r`, and 0 otherwise).
234 *
235 * d may be aliased to l or r.
236 *
237 * \param[out] d The result of the subtraction.
238 * \param[in] l The left operand.
239 * \param[in] r The right operand.
240 * \param n Number of limbs of \p d, \p l and \p r.
241 *
242 * \return 1 if `l < r`.
243 * 0 if `l >= r`.
244 */
245mbedtls_mpi_uint MPI_CORE(sub)( mbedtls_mpi_uint *d,
246 const mbedtls_mpi_uint *l,
247 const mbedtls_mpi_uint *r,
248 size_t n );
249
250/**
251 * \brief Constant-time conditional addition of two known-size large unsigned
252 * integers, returning the carry.
253 *
254 * Functionally equivalent to
255 *
256 * ```
257 * if( cond )
258 * d += r;
259 * return carry;
260 * ```
261 *
262 * \param[in,out] d The pointer to the (little-endian) array
263 * representing the bignum to accumulate onto.
264 * \param[in] r The pointer to the (little-endian) array
265 * representing the bignum to conditionally add
266 * to \p d. This must be disjoint from \p d.
267 * \param n Number of limbs of \p d and \p r.
268 * \param cond Condition bit dictating whether addition should
269 * happen or not. This must be \c 0 or \c 1.
270 *
271 * \return 1 if `d + cond*r >= (2^{ciL})^n`, 0 otherwise.
272 */
273mbedtls_mpi_uint MPI_CORE(add_if)( mbedtls_mpi_uint *d,
274 const mbedtls_mpi_uint *r,
275 size_t n,
276 unsigned cond );
277
Gabor Mezeib9030702022-07-18 23:09:45 +0200278#endif /* MBEDTLS_BIGNUM_CORE_H */