Gabor Mezei | b903070 | 2022-07-18 23:09:45 +0200 | [diff] [blame] | 1 | /** |
Janos Follath | 6318468 | 2022-08-11 17:42:59 +0100 | [diff] [blame] | 2 | * Core bignum functions |
| 3 | * |
Janos Follath | af3f39c | 2022-08-22 09:06:32 +0100 | [diff] [blame] | 4 | * This interface should only be used by the legacy bignum module (bignum.h) |
Janos Follath | 6318468 | 2022-08-11 17:42:59 +0100 | [diff] [blame] | 5 | * and the modular bignum modules (bignum_mod.c, bignum_mod_raw.c). All other |
Janos Follath | af3f39c | 2022-08-22 09:06:32 +0100 | [diff] [blame] | 6 | * modules should use the high-level modular bignum interface (bignum_mod.h) |
Janos Follath | 6318468 | 2022-08-11 17:42:59 +0100 | [diff] [blame] | 7 | * or the legacy bignum interface (bignum.h). |
Gabor Mezei | b903070 | 2022-07-18 23:09:45 +0200 | [diff] [blame] | 8 | * |
| 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 Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 34 | /** Count leading zero bits in a given integer. |
| 35 | * |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 36 | * \param a Integer to count leading zero bits. |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 37 | * |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 38 | * \return The number of leading zero bits in \p a. |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 39 | */ |
Janos Follath | 2e328c8 | 2022-08-22 11:19:10 +0100 | [diff] [blame] | 40 | size_t mbedtls_mpi_core_clz( mbedtls_mpi_uint a ); |
Janos Follath | 4670f88 | 2022-07-21 18:25:42 +0100 | [diff] [blame] | 41 | |
Janos Follath | a95f204 | 2022-08-19 12:09:17 +0100 | [diff] [blame] | 42 | /** Return the minimum number of bits required to represent the value held |
Janos Follath | 6318468 | 2022-08-11 17:42:59 +0100 | [diff] [blame] | 43 | * in the MPI. |
| 44 | * |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 45 | * \note This function returns 0 if all the limbs of \p A are 0. |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 46 | * |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 47 | * \param[in] A The address of the MPI. |
Janos Follath | c459641 | 2022-08-22 10:01:27 +0100 | [diff] [blame] | 48 | * \param A_limbs The number of limbs of \p A. |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 49 | * |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 50 | * \return The number of bits in \p A. |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 51 | */ |
Janos Follath | af3f39c | 2022-08-22 09:06:32 +0100 | [diff] [blame] | 52 | size_t mbedtls_mpi_core_bitlen( const mbedtls_mpi_uint *A, size_t A_limbs ); |
Janos Follath | 4670f88 | 2022-07-21 18:25:42 +0100 | [diff] [blame] | 53 | |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 54 | /** 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 Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 57 | * \param[in,out] A The address of the MPI. |
Janos Follath | c459641 | 2022-08-22 10:01:27 +0100 | [diff] [blame] | 58 | * \param A_limbs The number of limbs of \p A. |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 59 | */ |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 60 | void mbedtls_mpi_core_bigendian_to_host( mbedtls_mpi_uint *A, |
Janos Follath | c459641 | 2022-08-22 10:01:27 +0100 | [diff] [blame] | 61 | size_t A_limbs ); |
Janos Follath | 4670f88 | 2022-07-21 18:25:42 +0100 | [diff] [blame] | 62 | |
Janos Follath | af3f39c | 2022-08-22 09:06:32 +0100 | [diff] [blame] | 63 | /** Import X from unsigned binary data, little-endian. |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 64 | * |
Janos Follath | 6318468 | 2022-08-11 17:42:59 +0100 | [diff] [blame] | 65 | * The MPI needs to have enough limbs to store the full value (including any |
| 66 | * most significant zero bytes in the input). |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 67 | * |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 68 | * \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 Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 72 | * |
| 73 | * \return \c 0 if successful. |
| 74 | * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p X isn't |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 75 | * large enough to hold the value in \p input. |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 76 | */ |
Gabor Mezei | b903070 | 2022-07-18 23:09:45 +0200 | [diff] [blame] | 77 | int mbedtls_mpi_core_read_le( mbedtls_mpi_uint *X, |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 78 | size_t X_limbs, |
| 79 | const unsigned char *input, |
| 80 | size_t input_length ); |
Gabor Mezei | b903070 | 2022-07-18 23:09:45 +0200 | [diff] [blame] | 81 | |
Janos Follath | af3f39c | 2022-08-22 09:06:32 +0100 | [diff] [blame] | 82 | /** Import X from unsigned binary data, big-endian. |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 83 | * |
Janos Follath | 6318468 | 2022-08-11 17:42:59 +0100 | [diff] [blame] | 84 | * The MPI needs to have enough limbs to store the full value (including any |
| 85 | * most significant zero bytes in the input). |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 86 | * |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 87 | * \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 Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 94 | * |
| 95 | * \return \c 0 if successful. |
| 96 | * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p X isn't |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 97 | * large enough to hold the value in \p input. |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 98 | */ |
Gabor Mezei | b903070 | 2022-07-18 23:09:45 +0200 | [diff] [blame] | 99 | int mbedtls_mpi_core_read_be( mbedtls_mpi_uint *X, |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 100 | size_t X_limbs, |
| 101 | const unsigned char *input, |
| 102 | size_t input_length ); |
Gabor Mezei | b903070 | 2022-07-18 23:09:45 +0200 | [diff] [blame] | 103 | |
Janos Follath | af3f39c | 2022-08-22 09:06:32 +0100 | [diff] [blame] | 104 | /** Export A into unsigned binary data, little-endian. |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 105 | * |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 106 | * \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 Follath | 6318468 | 2022-08-11 17:42:59 +0100 | [diff] [blame] | 109 | * |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 110 | * \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 Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 114 | * |
| 115 | * \return \c 0 if successful. |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 116 | * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p output isn't |
| 117 | * large enough to hold the value of \p A. |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 118 | */ |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 119 | int 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 Mezei | b903070 | 2022-07-18 23:09:45 +0200 | [diff] [blame] | 123 | |
Janos Follath | af3f39c | 2022-08-22 09:06:32 +0100 | [diff] [blame] | 124 | /** Export A into unsigned binary data, big-endian. |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 125 | * |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 126 | * \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 Follath | 6318468 | 2022-08-11 17:42:59 +0100 | [diff] [blame] | 129 | * |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 130 | * \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 Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 134 | * |
| 135 | * \return \c 0 if successful. |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 136 | * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p output isn't |
| 137 | * large enough to hold the value of \p A. |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 138 | */ |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 139 | int 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 Mezei | b903070 | 2022-07-18 23:09:45 +0200 | [diff] [blame] | 143 | |
Janos Follath | ca5688e | 2022-08-19 12:05:28 +0100 | [diff] [blame] | 144 | #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 Follath | d089570 | 2022-08-10 13:32:16 +0100 | [diff] [blame] | 147 | |
| 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 Follath | ca5688e | 2022-08-19 12:05:28 +0100 | [diff] [blame] | 156 | ( ( (X)[(i) / ciL] >> ( ( (i) % ciL ) * 8 ) ) & 0xff ) |
Janos Follath | d089570 | 2022-08-10 13:32:16 +0100 | [diff] [blame] | 157 | |
Tom Cosgrove | 90c426b | 2022-08-23 16:15:19 +0100 | [diff] [blame] | 158 | /** |
| 159 | * \brief Montgomery multiplication: X = A * B * R^-1 mod N (HAC 14.36) |
Tom Cosgrove | 82d3f1e | 2022-08-23 12:01:39 +0100 | [diff] [blame] | 160 | * |
Tom Cosgrove | 90c426b | 2022-08-23 16:15:19 +0100 | [diff] [blame] | 161 | * \param[out] X The destination MPI, as a little-endian array of |
| 162 | * length \p n. |
| 163 | * On successful completion, X contains the result of |
| 164 | * the multiplication A * B * R^-1 mod N where |
| 165 | * R = (2^ciL)^n. |
| 166 | * \param[in] A Little-endian presentation of first operand. |
| 167 | * Must have exactly \p n limbs. |
| 168 | * \param[in] B Little-endian presentation of second operand. |
| 169 | * \param[in] B_len The number of limbs in \p B. |
| 170 | * \param[in] N Little-endian presentation of the modulus. |
| 171 | * This must be odd and have exactly \p n limbs. |
| 172 | * \param[in] n The number of limbs in \p X, \p A, \p N. |
| 173 | * \param mm The Montgomery constant for \p N: -N^-1 mod 2^ciL. |
Tom Cosgrove | 79b70f6 | 2022-08-17 06:17:00 +0100 | [diff] [blame^] | 174 | * This can be calculated by `mbedtls_mpi_montg_init()`. |
Tom Cosgrove | 90c426b | 2022-08-23 16:15:19 +0100 | [diff] [blame] | 175 | * \param[in,out] T Temporary storage of size at least 2*n+1 limbs. |
| 176 | * Its initial content is unused and |
| 177 | * its final content is indeterminate. |
| 178 | */ |
| 179 | void mbedtls_mpi_core_montmul( mbedtls_mpi_uint *X, |
| 180 | const mbedtls_mpi_uint *A, |
| 181 | const mbedtls_mpi_uint *B, size_t B_len, |
| 182 | const mbedtls_mpi_uint *N, size_t n, |
| 183 | mbedtls_mpi_uint mm, mbedtls_mpi_uint *T ); |
| 184 | |
| 185 | /** |
Tom Cosgrove | 79b70f6 | 2022-08-17 06:17:00 +0100 | [diff] [blame^] | 186 | * \brief Calculate initialisation value for fast Montgomery modular |
| 187 | * multiplication |
| 188 | * |
| 189 | * \param m0 The least-significant mbedtls_mpi_uint from the modulus, which |
| 190 | * must be odd |
| 191 | * |
| 192 | * \return The initialisation value for fast Montgomery modular multiplication |
| 193 | */ |
| 194 | mbedtls_mpi_uint mbedtls_mpi_montg_init( mbedtls_mpi_uint m0 ); |
| 195 | |
| 196 | /** |
Tom Cosgrove | 90c426b | 2022-08-23 16:15:19 +0100 | [diff] [blame] | 197 | * \brief Perform a known-size multiply accumulate operation: d += b * s |
Tom Cosgrove | 82d3f1e | 2022-08-23 12:01:39 +0100 | [diff] [blame] | 198 | * |
| 199 | * \param[in,out] d The pointer to the (little-endian) array |
| 200 | * representing the bignum to accumulate onto. |
| 201 | * \param d_len The number of limbs of \p d. This must be |
| 202 | * at least \p s_len. |
| 203 | * \param[in] s The pointer to the (little-endian) array |
| 204 | * representing the bignum to multiply with. |
| 205 | * This may be the same as \p d. Otherwise, |
| 206 | * it must be disjoint from \p d. |
| 207 | * \param s_len The number of limbs of \p s. |
| 208 | * \param b A scalar to multiply with. |
| 209 | * |
| 210 | * \return c The carry at the end of the operation. |
| 211 | */ |
Hanno Becker | 71f4b0d | 2022-08-23 12:09:35 +0100 | [diff] [blame] | 212 | mbedtls_mpi_uint mbedtls_mpi_core_mla( mbedtls_mpi_uint *d, size_t d_len, |
Tom Cosgrove | 82d3f1e | 2022-08-23 12:01:39 +0100 | [diff] [blame] | 213 | const mbedtls_mpi_uint *s, size_t s_len, |
| 214 | mbedtls_mpi_uint b ); |
| 215 | |
Hanno Becker | 71f4b0d | 2022-08-23 12:09:35 +0100 | [diff] [blame] | 216 | /** |
| 217 | * \brief Subtract two known-size large unsigned integers, returning the borrow. |
| 218 | * |
| 219 | * Calculate l - r where l and r have the same size. |
| 220 | * This function operates modulo (2^ciL)^n and returns the carry |
| 221 | * (1 if there was a wraparound, i.e. if `l < r`, and 0 otherwise). |
| 222 | * |
| 223 | * d may be aliased to l or r. |
| 224 | * |
| 225 | * \param[out] d The result of the subtraction. |
Tom Cosgrove | 90c426b | 2022-08-23 16:15:19 +0100 | [diff] [blame] | 226 | * \param[in] l Little-endian presentation of left operand. |
| 227 | * \param[in] r Little-endian presentation of right operand. |
Hanno Becker | 71f4b0d | 2022-08-23 12:09:35 +0100 | [diff] [blame] | 228 | * \param n Number of limbs of \p d, \p l and \p r. |
| 229 | * |
| 230 | * \return 1 if `l < r`. |
| 231 | * 0 if `l >= r`. |
| 232 | */ |
Tom Cosgrove | 90c426b | 2022-08-23 16:15:19 +0100 | [diff] [blame] | 233 | mbedtls_mpi_uint mbedtls_mpi_core_sub( mbedtls_mpi_uint *d, |
| 234 | const mbedtls_mpi_uint *l, |
| 235 | const mbedtls_mpi_uint *r, |
| 236 | size_t n ); |
Hanno Becker | 71f4b0d | 2022-08-23 12:09:35 +0100 | [diff] [blame] | 237 | |
| 238 | /** |
| 239 | * \brief Constant-time conditional addition of two known-size large unsigned |
| 240 | * integers, returning the carry. |
| 241 | * |
| 242 | * Functionally equivalent to |
| 243 | * |
| 244 | * ``` |
| 245 | * if( cond ) |
| 246 | * d += r; |
| 247 | * return carry; |
| 248 | * ``` |
| 249 | * |
| 250 | * \param[in,out] d The pointer to the (little-endian) array |
| 251 | * representing the bignum to accumulate onto. |
| 252 | * \param[in] r The pointer to the (little-endian) array |
| 253 | * representing the bignum to conditionally add |
| 254 | * to \p d. This must be disjoint from \p d. |
| 255 | * \param n Number of limbs of \p d and \p r. |
| 256 | * \param cond Condition bit dictating whether addition should |
| 257 | * happen or not. This must be \c 0 or \c 1. |
| 258 | * |
| 259 | * \return 1 if `d + cond*r >= (2^{ciL})^n`, 0 otherwise. |
| 260 | */ |
Tom Cosgrove | 90c426b | 2022-08-23 16:15:19 +0100 | [diff] [blame] | 261 | mbedtls_mpi_uint mbedtls_mpi_core_add_if( mbedtls_mpi_uint *d, |
| 262 | const mbedtls_mpi_uint *r, |
| 263 | size_t n, |
| 264 | unsigned cond ); |
Hanno Becker | 71f4b0d | 2022-08-23 12:09:35 +0100 | [diff] [blame] | 265 | |
Gabor Mezei | b903070 | 2022-07-18 23:09:45 +0200 | [diff] [blame] | 266 | #endif /* MBEDTLS_BIGNUM_CORE_H */ |