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 | /** |
Tom Cosgrove | d932de8 | 2022-08-25 16:43:43 +0100 | [diff] [blame] | 159 | * \brief Conditional addition of two known-size large unsigned integers, |
| 160 | * returning the carry. |
Hanno Becker | 71f4b0d | 2022-08-23 12:09:35 +0100 | [diff] [blame] | 161 | * |
| 162 | * Functionally equivalent to |
| 163 | * |
| 164 | * ``` |
| 165 | * if( cond ) |
Tom Cosgrove | 7259463 | 2022-08-24 11:51:58 +0100 | [diff] [blame] | 166 | * A += B; |
Hanno Becker | 71f4b0d | 2022-08-23 12:09:35 +0100 | [diff] [blame] | 167 | * return carry; |
| 168 | * ``` |
| 169 | * |
Tom Cosgrove | 7259463 | 2022-08-24 11:51:58 +0100 | [diff] [blame] | 170 | * \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 |
| 174 | * to \p A. This must be disjoint from \p A. |
| 175 | * \param limbs Number of limbs of \p A and \p B. |
| 176 | * \param cond Condition bit dictating whether addition should |
| 177 | * happen or not. This must be \c 0 or \c 1. |
Hanno Becker | 71f4b0d | 2022-08-23 12:09:35 +0100 | [diff] [blame] | 178 | * |
Tom Cosgrove | ecbb124 | 2022-08-25 10:13:44 +0100 | [diff] [blame] | 179 | * \warning If \p cond is neither 0 nor 1, the result of this function |
Tom Cosgrove | 7259463 | 2022-08-24 11:51:58 +0100 | [diff] [blame] | 180 | * is unspecified, and the resulting value in \p A might be |
| 181 | * neither its original value nor \p A + \p B. |
| 182 | * |
| 183 | * \return 1 if `A + cond * B >= (2^{ciL})^limbs`, 0 otherwise. |
Hanno Becker | 71f4b0d | 2022-08-23 12:09:35 +0100 | [diff] [blame] | 184 | */ |
Tom Cosgrove | 7259463 | 2022-08-24 11:51:58 +0100 | [diff] [blame] | 185 | mbedtls_mpi_uint mbedtls_mpi_core_add_if( mbedtls_mpi_uint *A, |
| 186 | const mbedtls_mpi_uint *B, |
| 187 | size_t limbs, |
Tom Cosgrove | 90c426b | 2022-08-23 16:15:19 +0100 | [diff] [blame] | 188 | unsigned cond ); |
Hanno Becker | 71f4b0d | 2022-08-23 12:09:35 +0100 | [diff] [blame] | 189 | |
Tom Cosgrove | b496486 | 2022-08-30 11:57:22 +0100 | [diff] [blame^] | 190 | /** |
| 191 | * \brief Subtract two known-size large unsigned integers, returning the borrow. |
| 192 | * |
| 193 | * Calculate A - B where A and B have the same size. |
| 194 | * This function operates modulo (2^ciL)^limbs and returns the carry |
| 195 | * (1 if there was a wraparound, i.e. if `A < B`, and 0 otherwise). |
| 196 | * |
| 197 | * X may be aliased to A or B. |
| 198 | * |
| 199 | * \param[out] X The result of the subtraction. |
| 200 | * \param[in] A Little-endian presentation of left operand. |
| 201 | * \param[in] B Little-endian presentation of right operand. |
| 202 | * \param limbs Number of limbs of \p X, \p A and \p B. |
| 203 | * |
| 204 | * \return 1 if `A < B`. |
| 205 | * 0 if `A >= B`. |
| 206 | */ |
| 207 | mbedtls_mpi_uint mbedtls_mpi_core_sub( mbedtls_mpi_uint *X, |
| 208 | const mbedtls_mpi_uint *A, |
| 209 | const mbedtls_mpi_uint *B, |
| 210 | size_t limbs ); |
| 211 | |
| 212 | /** |
| 213 | * \brief Perform a known-size multiply accumulate operation: A += c * B |
| 214 | * |
| 215 | * \param[in,out] A The pointer to the (little-endian) array |
| 216 | * representing the bignum to accumulate onto. |
| 217 | * \param A_limbs The number of limbs of \p A. This must be |
| 218 | * at least \p B_limbs. |
| 219 | * \param[in] B The pointer to the (little-endian) array |
| 220 | * representing the bignum to multiply with. |
| 221 | * This may be the same as \p A. Otherwise, |
| 222 | * it must be disjoint from \p A. |
| 223 | * \param B_limbs The number of limbs of \p B. |
| 224 | * \param c A scalar to multiply with. |
| 225 | * |
| 226 | * \return The carry at the end of the operation. |
| 227 | */ |
| 228 | mbedtls_mpi_uint mbedtls_mpi_core_mla( mbedtls_mpi_uint *A, size_t A_limbs, |
| 229 | const mbedtls_mpi_uint *B, size_t B_limbs, |
| 230 | mbedtls_mpi_uint c ); |
| 231 | |
| 232 | /** |
| 233 | * \brief Calculate initialisation value for fast Montgomery modular |
| 234 | * multiplication |
| 235 | * |
| 236 | * \param[in] N Little-endian presentation of the modulus. This must have |
| 237 | * at least one limb. |
| 238 | * |
| 239 | * \return The initialisation value for fast Montgomery modular multiplication |
| 240 | */ |
| 241 | mbedtls_mpi_uint mbedtls_mpi_montg_init( const mbedtls_mpi_uint *N ); |
| 242 | |
| 243 | /** |
| 244 | * \brief Montgomery multiplication: X = A * B * R^-1 mod N (HAC 14.36) |
| 245 | * |
| 246 | * \param[out] X The destination MPI, as a little-endian array of |
| 247 | * length \p AN_limbs. |
| 248 | * On successful completion, X contains the result of |
| 249 | * the multiplication A * B * R^-1 mod N where |
| 250 | * R = (2^ciL)^AN_limbs. |
| 251 | * \param[in] A Little-endian presentation of first operand. |
| 252 | * Must have exactly \p AN_limbs limbs. |
| 253 | * \param[in] B Little-endian presentation of second operand. |
| 254 | * \param[in] B_limbs The number of limbs in \p B. |
| 255 | * \param[in] N Little-endian presentation of the modulus. |
| 256 | * This must be odd and have exactly \p AN_limbs limbs. |
| 257 | * \param[in] AN_limbs The number of limbs in \p X, \p A, \p N. |
| 258 | * \param mm The Montgomery constant for \p N: -N^-1 mod 2^ciL. |
| 259 | * This can be calculated by `mbedtls_mpi_montg_init()`. |
| 260 | * \param[in,out] T Temporary storage of size at least 2*AN_limbs+1 limbs. |
| 261 | * Its initial content is unused and |
| 262 | * its final content is indeterminate. |
| 263 | */ |
| 264 | void mbedtls_mpi_core_montmul( mbedtls_mpi_uint *X, |
| 265 | const mbedtls_mpi_uint *A, |
| 266 | const mbedtls_mpi_uint *B, size_t B_limbs, |
| 267 | const mbedtls_mpi_uint *N, size_t AN_limbs, |
| 268 | mbedtls_mpi_uint mm, mbedtls_mpi_uint *T ); |
| 269 | |
Gabor Mezei | b903070 | 2022-07-18 23:09:45 +0200 | [diff] [blame] | 270 | #endif /* MBEDTLS_BIGNUM_CORE_H */ |