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 | * |
Gilles Peskine | 7aab2fb | 2022-09-27 13:19:13 +0200 | [diff] [blame] | 9 | * This module is about processing non-negative integers with a fixed upper |
Gilles Peskine | 01af3dd | 2022-10-04 16:23:29 +0200 | [diff] [blame] | 10 | * bound that's of the form 2^n-1 where n is a multiple of #biL. |
| 11 | * These can be thought of integers written in base 2^#biL with a fixed |
| 12 | * number of digits. Digits in this base are called *limbs*. |
| 13 | * Many operations treat these numbers as the principal representation of |
| 14 | * a number modulo 2^n or a smaller bound. |
Gilles Peskine | 7aab2fb | 2022-09-27 13:19:13 +0200 | [diff] [blame] | 15 | * |
Gilles Peskine | 2926484 | 2022-09-27 13:19:50 +0200 | [diff] [blame] | 16 | * The functions in this module obey the following conventions unless |
| 17 | * explicitly indicated otherwise: |
| 18 | * |
| 19 | * - **Overflow**: some functions indicate overflow from the range |
Gilles Peskine | 01af3dd | 2022-10-04 16:23:29 +0200 | [diff] [blame] | 20 | * [0, 2^n-1] by returning carry parameters, while others operate |
Gilles Peskine | 2926484 | 2022-09-27 13:19:50 +0200 | [diff] [blame] | 21 | * modulo and so cannot overflow. This should be clear from the function |
| 22 | * documentation. |
| 23 | * - **Bignum parameters**: Bignums are passed as pointers to an array of |
| 24 | * limbs. A limb has the type #mbedtls_mpi_uint. Unless otherwise specified: |
| 25 | * - Bignum parameters called \p A, \p B, ... are inputs, and are |
| 26 | * not modified by the function. |
| 27 | * - For operations modulo some number, the modulus is called \p N |
| 28 | * and is input-only. |
| 29 | * - Bignum parameters called \p X, \p Y are outputs or input-output. |
| 30 | * The initial content of output-only parameters is ignored. |
| 31 | * - Some functions use different names that reflect traditional |
| 32 | * naming of operands of certain operations (e.g. |
| 33 | * divisor/dividend/quotient/remainder). |
| 34 | * - \p T is a temporary storage area. The initial content of such |
| 35 | * parameter is ignored and the final content is unspecified. |
| 36 | * - **Bignum sizes**: bignum sizes are always expressed in limbs. |
| 37 | * Most functions work on bignums of a given size and take a single |
| 38 | * \p limbs parameter that applies to all parameters that are limb arrays. |
| 39 | * All bignum sizes must be at least 1 and must be significantly less than |
| 40 | * #SIZE_MAX. The behavior if a size is 0 is undefined. The behavior if the |
| 41 | * total size of all parameters overflows #SIZE_MAX is undefined. |
| 42 | * - **Parameter ordering**: for bignum parameters, outputs come before inputs. |
| 43 | * Temporaries come last. |
| 44 | * - **Aliasing**: in general, output bignums may be aliased to one or more |
| 45 | * inputs. As an exception, parameters that are documented as a modulus value |
Gilles Peskine | dcd1717 | 2022-10-14 17:14:20 +0200 | [diff] [blame^] | 46 | * may not be aliased to an output. Outputs may not be aliased to one another. |
| 47 | * Temporaries may not be aliased to any other parameter. |
Gilles Peskine | 2926484 | 2022-09-27 13:19:50 +0200 | [diff] [blame] | 48 | * - **Overlap**: apart from aliasing of limb array pointers (where two |
| 49 | * arguments are equal pointers), overlap is not supported and may result |
| 50 | * in undefined behavior. |
| 51 | * - **Error handling**: This is a low-level module. Functions generally do not |
| 52 | * try to protect against invalid arguments such as nonsensical sizes or |
| 53 | * null pointers. Note that some functions that operate on bignums of |
| 54 | * different sizes have constraints about their size, and violating those |
| 55 | * constraints may lead to buffer overflows. |
| 56 | * - **Modular representatives**: functions that operate modulo \p N expect |
| 57 | * all modular inputs to be in the range [0, \p N - 1] and guarantee outputs |
| 58 | * in the range [0, \p N - 1]. If an input is out of range, outputs are |
| 59 | * fully unspecified, though bignum values out of range should not cause |
| 60 | * buffer overflows (beware that this is not extensively tested). |
Gilles Peskine | 7f887bd | 2022-09-27 13:12:30 +0200 | [diff] [blame] | 61 | */ |
| 62 | |
| 63 | /* |
Gabor Mezei | b903070 | 2022-07-18 23:09:45 +0200 | [diff] [blame] | 64 | * Copyright The Mbed TLS Contributors |
| 65 | * SPDX-License-Identifier: Apache-2.0 |
| 66 | * |
| 67 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 68 | * not use this file except in compliance with the License. |
| 69 | * You may obtain a copy of the License at |
| 70 | * |
| 71 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 72 | * |
| 73 | * Unless required by applicable law or agreed to in writing, software |
| 74 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 75 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 76 | * See the License for the specific language governing permissions and |
| 77 | * limitations under the License. |
| 78 | */ |
| 79 | |
| 80 | #ifndef MBEDTLS_BIGNUM_CORE_H |
| 81 | #define MBEDTLS_BIGNUM_CORE_H |
| 82 | |
| 83 | #include "common.h" |
| 84 | |
| 85 | #if defined(MBEDTLS_BIGNUM_C) |
| 86 | #include "mbedtls/bignum.h" |
| 87 | #endif |
| 88 | |
Gilles Peskine | 2926484 | 2022-09-27 13:19:50 +0200 | [diff] [blame] | 89 | #define ciL ( sizeof(mbedtls_mpi_uint) ) /** chars in limb */ |
| 90 | #define biL ( ciL << 3 ) /** bits in limb */ |
| 91 | #define biH ( ciL << 2 ) /** half limb size */ |
Tom Cosgrove | 5eefc3d | 2022-08-31 17:16:50 +0100 | [diff] [blame] | 92 | |
| 93 | /* |
| 94 | * Convert between bits/chars and number of limbs |
| 95 | * Divide first in order to avoid potential overflows |
| 96 | */ |
| 97 | #define BITS_TO_LIMBS(i) ( (i) / biL + ( (i) % biL != 0 ) ) |
| 98 | #define CHARS_TO_LIMBS(i) ( (i) / ciL + ( (i) % ciL != 0 ) ) |
| 99 | /* Get a specific byte, without range checks. */ |
| 100 | #define GET_BYTE( X, i ) \ |
| 101 | ( ( (X)[(i) / ciL] >> ( ( (i) % ciL ) * 8 ) ) & 0xff ) |
| 102 | |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 103 | /** Count leading zero bits in a given integer. |
| 104 | * |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 105 | * \param a Integer to count leading zero bits. |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 106 | * |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 107 | * \return The number of leading zero bits in \p a. |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 108 | */ |
Janos Follath | 2e328c8 | 2022-08-22 11:19:10 +0100 | [diff] [blame] | 109 | size_t mbedtls_mpi_core_clz( mbedtls_mpi_uint a ); |
Janos Follath | 4670f88 | 2022-07-21 18:25:42 +0100 | [diff] [blame] | 110 | |
Janos Follath | a95f204 | 2022-08-19 12:09:17 +0100 | [diff] [blame] | 111 | /** Return the minimum number of bits required to represent the value held |
Janos Follath | 6318468 | 2022-08-11 17:42:59 +0100 | [diff] [blame] | 112 | * in the MPI. |
| 113 | * |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 114 | * \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] | 115 | * |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 116 | * \param[in] A The address of the MPI. |
Janos Follath | c459641 | 2022-08-22 10:01:27 +0100 | [diff] [blame] | 117 | * \param A_limbs The number of limbs 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 | * \return The number of bits in \p A. |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 120 | */ |
Janos Follath | af3f39c | 2022-08-22 09:06:32 +0100 | [diff] [blame] | 121 | 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] | 122 | |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 123 | /** Convert a big-endian byte array aligned to the size of mbedtls_mpi_uint |
| 124 | * into the storage form used by mbedtls_mpi. |
| 125 | * |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 126 | * \param[in,out] A The address of the MPI. |
Janos Follath | c459641 | 2022-08-22 10:01:27 +0100 | [diff] [blame] | 127 | * \param A_limbs The number of limbs of \p A. |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 128 | */ |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 129 | void mbedtls_mpi_core_bigendian_to_host( mbedtls_mpi_uint *A, |
Janos Follath | c459641 | 2022-08-22 10:01:27 +0100 | [diff] [blame] | 130 | size_t A_limbs ); |
Janos Follath | 4670f88 | 2022-07-21 18:25:42 +0100 | [diff] [blame] | 131 | |
Janos Follath | af3f39c | 2022-08-22 09:06:32 +0100 | [diff] [blame] | 132 | /** Import X from unsigned binary data, little-endian. |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 133 | * |
Janos Follath | 6318468 | 2022-08-11 17:42:59 +0100 | [diff] [blame] | 134 | * The MPI needs to have enough limbs to store the full value (including any |
| 135 | * most significant zero bytes in the input). |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 136 | * |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 137 | * \param[out] X The address of the MPI. |
| 138 | * \param X_limbs The number of limbs of \p X. |
| 139 | * \param[in] input The input buffer to import from. |
| 140 | * \param input_length The length bytes of \p input. |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 141 | * |
| 142 | * \return \c 0 if successful. |
| 143 | * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p X isn't |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 144 | * large enough to hold the value in \p input. |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 145 | */ |
Gabor Mezei | b903070 | 2022-07-18 23:09:45 +0200 | [diff] [blame] | 146 | int mbedtls_mpi_core_read_le( mbedtls_mpi_uint *X, |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 147 | size_t X_limbs, |
| 148 | const unsigned char *input, |
| 149 | size_t input_length ); |
Gabor Mezei | b903070 | 2022-07-18 23:09:45 +0200 | [diff] [blame] | 150 | |
Janos Follath | af3f39c | 2022-08-22 09:06:32 +0100 | [diff] [blame] | 151 | /** Import X from unsigned binary data, big-endian. |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 152 | * |
Janos Follath | 6318468 | 2022-08-11 17:42:59 +0100 | [diff] [blame] | 153 | * The MPI needs to have enough limbs to store the full value (including any |
| 154 | * most significant zero bytes in the input). |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 155 | * |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 156 | * \param[out] X The address of the MPI. |
| 157 | * May only be #NULL if \X_limbs is 0 and \p input_length |
| 158 | * is 0. |
| 159 | * \param X_limbs The number of limbs of \p X. |
| 160 | * \param[in] input The input buffer to import from. |
| 161 | * May only be #NULL if \p input_length is 0. |
| 162 | * \param input_length The length in bytes of \p input. |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 163 | * |
| 164 | * \return \c 0 if successful. |
| 165 | * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p X isn't |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 166 | * large enough to hold the value in \p input. |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 167 | */ |
Gabor Mezei | b903070 | 2022-07-18 23:09:45 +0200 | [diff] [blame] | 168 | int mbedtls_mpi_core_read_be( mbedtls_mpi_uint *X, |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 169 | size_t X_limbs, |
| 170 | const unsigned char *input, |
| 171 | size_t input_length ); |
Gabor Mezei | b903070 | 2022-07-18 23:09:45 +0200 | [diff] [blame] | 172 | |
Janos Follath | af3f39c | 2022-08-22 09:06:32 +0100 | [diff] [blame] | 173 | /** Export A into unsigned binary data, little-endian. |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 174 | * |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 175 | * \note If \p output is shorter than \p A the export is still successful if the |
| 176 | * value held in \p A fits in the buffer (that is, if enough of the most |
| 177 | * significant bytes of \p A are 0). |
Janos Follath | 6318468 | 2022-08-11 17:42:59 +0100 | [diff] [blame] | 178 | * |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 179 | * \param[in] A The address of the MPI. |
| 180 | * \param A_limbs The number of limbs of \p A. |
| 181 | * \param[out] output The output buffer to export to. |
| 182 | * \param output_length The length in bytes of \p output. |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 183 | * |
| 184 | * \return \c 0 if successful. |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 185 | * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p output isn't |
| 186 | * large enough to hold the value of \p A. |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 187 | */ |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 188 | int mbedtls_mpi_core_write_le( const mbedtls_mpi_uint *A, |
| 189 | size_t A_limbs, |
| 190 | unsigned char *output, |
| 191 | size_t output_length ); |
Gabor Mezei | b903070 | 2022-07-18 23:09:45 +0200 | [diff] [blame] | 192 | |
Janos Follath | af3f39c | 2022-08-22 09:06:32 +0100 | [diff] [blame] | 193 | /** Export A into unsigned binary data, big-endian. |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 194 | * |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 195 | * \note If \p output is shorter than \p A the export is still successful if the |
| 196 | * value held in \p A fits in the buffer (that is, if enough of the most |
| 197 | * significant bytes of \p A are 0). |
Janos Follath | 6318468 | 2022-08-11 17:42:59 +0100 | [diff] [blame] | 198 | * |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 199 | * \param[in] A The address of the MPI. |
| 200 | * \param A_limbs The number of limbs of \p A. |
| 201 | * \param[out] output The output buffer to export to. |
| 202 | * \param output_length The length in bytes of \p output. |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 203 | * |
| 204 | * \return \c 0 if successful. |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 205 | * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p output isn't |
| 206 | * large enough to hold the value of \p A. |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 207 | */ |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 208 | int mbedtls_mpi_core_write_be( const mbedtls_mpi_uint *A, |
| 209 | size_t A_limbs, |
| 210 | unsigned char *output, |
| 211 | size_t output_length ); |
Gabor Mezei | b903070 | 2022-07-18 23:09:45 +0200 | [diff] [blame] | 212 | |
Tom Cosgrove | 90c426b | 2022-08-23 16:15:19 +0100 | [diff] [blame] | 213 | /** |
Tom Cosgrove | 5c0e810 | 2022-09-15 15:46:10 +0100 | [diff] [blame] | 214 | * \brief Conditional addition of two fixed-size large unsigned integers, |
Tom Cosgrove | d932de8 | 2022-08-25 16:43:43 +0100 | [diff] [blame] | 215 | * returning the carry. |
Hanno Becker | 71f4b0d | 2022-08-23 12:09:35 +0100 | [diff] [blame] | 216 | * |
| 217 | * Functionally equivalent to |
| 218 | * |
| 219 | * ``` |
| 220 | * if( cond ) |
Tom Cosgrove | 3bd7bc3 | 2022-09-15 15:55:07 +0100 | [diff] [blame] | 221 | * X += A; |
Hanno Becker | 71f4b0d | 2022-08-23 12:09:35 +0100 | [diff] [blame] | 222 | * return carry; |
| 223 | * ``` |
| 224 | * |
Tom Cosgrove | 4782823 | 2022-09-20 13:51:50 +0100 | [diff] [blame] | 225 | * This function operates modulo `2^(biL*limbs)`. |
| 226 | * |
Tom Cosgrove | 3bd7bc3 | 2022-09-15 15:55:07 +0100 | [diff] [blame] | 227 | * \param[in,out] X The pointer to the (little-endian) array |
Tom Cosgrove | 7259463 | 2022-08-24 11:51:58 +0100 | [diff] [blame] | 228 | * representing the bignum to accumulate onto. |
Tom Cosgrove | 3bd7bc3 | 2022-09-15 15:55:07 +0100 | [diff] [blame] | 229 | * \param[in] A The pointer to the (little-endian) array |
Tom Cosgrove | 7259463 | 2022-08-24 11:51:58 +0100 | [diff] [blame] | 230 | * representing the bignum to conditionally add |
Tom Cosgrove | 3bd7bc3 | 2022-09-15 15:55:07 +0100 | [diff] [blame] | 231 | * to \p X. This may be aliased to \p X but may not |
Tom Cosgrove | ed43c6c | 2022-08-31 11:35:00 +0100 | [diff] [blame] | 232 | * overlap otherwise. |
Tom Cosgrove | 3bd7bc3 | 2022-09-15 15:55:07 +0100 | [diff] [blame] | 233 | * \param limbs Number of limbs of \p X and \p A. |
Tom Cosgrove | 7259463 | 2022-08-24 11:51:58 +0100 | [diff] [blame] | 234 | * \param cond Condition bit dictating whether addition should |
| 235 | * happen or not. This must be \c 0 or \c 1. |
Hanno Becker | 71f4b0d | 2022-08-23 12:09:35 +0100 | [diff] [blame] | 236 | * |
Tom Cosgrove | ecbb124 | 2022-08-25 10:13:44 +0100 | [diff] [blame] | 237 | * \warning If \p cond is neither 0 nor 1, the result of this function |
Tom Cosgrove | 3bd7bc3 | 2022-09-15 15:55:07 +0100 | [diff] [blame] | 238 | * is unspecified, and the resulting value in \p X might be |
| 239 | * neither its original value nor \p X + \p A. |
Tom Cosgrove | 7259463 | 2022-08-24 11:51:58 +0100 | [diff] [blame] | 240 | * |
Tom Cosgrove | 3bd7bc3 | 2022-09-15 15:55:07 +0100 | [diff] [blame] | 241 | * \return 1 if `X + cond * A >= 2^(biL*limbs)`, 0 otherwise. |
Hanno Becker | 71f4b0d | 2022-08-23 12:09:35 +0100 | [diff] [blame] | 242 | */ |
Tom Cosgrove | 3bd7bc3 | 2022-09-15 15:55:07 +0100 | [diff] [blame] | 243 | mbedtls_mpi_uint mbedtls_mpi_core_add_if( mbedtls_mpi_uint *X, |
| 244 | const mbedtls_mpi_uint *A, |
Tom Cosgrove | 7259463 | 2022-08-24 11:51:58 +0100 | [diff] [blame] | 245 | size_t limbs, |
Tom Cosgrove | 90c426b | 2022-08-23 16:15:19 +0100 | [diff] [blame] | 246 | unsigned cond ); |
Hanno Becker | 71f4b0d | 2022-08-23 12:09:35 +0100 | [diff] [blame] | 247 | |
Tom Cosgrove | b496486 | 2022-08-30 11:57:22 +0100 | [diff] [blame] | 248 | /** |
Tom Cosgrove | 5c0e810 | 2022-09-15 15:46:10 +0100 | [diff] [blame] | 249 | * \brief Subtract two fixed-size large unsigned integers, returning the borrow. |
Tom Cosgrove | b496486 | 2022-08-30 11:57:22 +0100 | [diff] [blame] | 250 | * |
Tom Cosgrove | 5dd97e6 | 2022-08-30 14:31:49 +0100 | [diff] [blame] | 251 | * Calculate `A - B` where \p A and \p B have the same size. |
Tom Cosgrove | 630110a | 2022-08-31 17:09:29 +0100 | [diff] [blame] | 252 | * This function operates modulo `2^(biL*limbs)` and returns the carry |
Tom Cosgrove | b496486 | 2022-08-30 11:57:22 +0100 | [diff] [blame] | 253 | * (1 if there was a wraparound, i.e. if `A < B`, and 0 otherwise). |
| 254 | * |
Tom Cosgrove | 5dd97e6 | 2022-08-30 14:31:49 +0100 | [diff] [blame] | 255 | * \p X may be aliased to \p A or \p B, or even both, but may not overlap |
| 256 | * either otherwise. |
Tom Cosgrove | b496486 | 2022-08-30 11:57:22 +0100 | [diff] [blame] | 257 | * |
| 258 | * \param[out] X The result of the subtraction. |
| 259 | * \param[in] A Little-endian presentation of left operand. |
| 260 | * \param[in] B Little-endian presentation of right operand. |
| 261 | * \param limbs Number of limbs of \p X, \p A and \p B. |
| 262 | * |
| 263 | * \return 1 if `A < B`. |
| 264 | * 0 if `A >= B`. |
| 265 | */ |
| 266 | mbedtls_mpi_uint mbedtls_mpi_core_sub( mbedtls_mpi_uint *X, |
| 267 | const mbedtls_mpi_uint *A, |
| 268 | const mbedtls_mpi_uint *B, |
| 269 | size_t limbs ); |
| 270 | |
| 271 | /** |
Tom Cosgrove | 3bd7bc3 | 2022-09-15 15:55:07 +0100 | [diff] [blame] | 272 | * \brief Perform a fixed-size multiply accumulate operation: X += b * A |
Tom Cosgrove | b496486 | 2022-08-30 11:57:22 +0100 | [diff] [blame] | 273 | * |
Tom Cosgrove | b0b77e1 | 2022-09-20 13:33:40 +0100 | [diff] [blame] | 274 | * \p X may be aliased to \p A (when \p X_limbs == \p A_limbs), but may not |
| 275 | * otherwise overlap. |
| 276 | * |
Tom Cosgrove | 4782823 | 2022-09-20 13:51:50 +0100 | [diff] [blame] | 277 | * This function operates modulo `2^(biL*X_limbs)`. |
| 278 | * |
Tom Cosgrove | 3bd7bc3 | 2022-09-15 15:55:07 +0100 | [diff] [blame] | 279 | * \param[in,out] X The pointer to the (little-endian) array |
Tom Cosgrove | b496486 | 2022-08-30 11:57:22 +0100 | [diff] [blame] | 280 | * representing the bignum to accumulate onto. |
Tom Cosgrove | 3bd7bc3 | 2022-09-15 15:55:07 +0100 | [diff] [blame] | 281 | * \param X_limbs The number of limbs of \p X. This must be |
| 282 | * at least \p A_limbs. |
| 283 | * \param[in] A The pointer to the (little-endian) array |
Tom Cosgrove | b496486 | 2022-08-30 11:57:22 +0100 | [diff] [blame] | 284 | * representing the bignum to multiply with. |
Tom Cosgrove | 3bd7bc3 | 2022-09-15 15:55:07 +0100 | [diff] [blame] | 285 | * This may be aliased to \p X but may not overlap |
Tom Cosgrove | ed43c6c | 2022-08-31 11:35:00 +0100 | [diff] [blame] | 286 | * otherwise. |
Tom Cosgrove | 3bd7bc3 | 2022-09-15 15:55:07 +0100 | [diff] [blame] | 287 | * \param A_limbs The number of limbs of \p A. |
| 288 | * \param b X scalar to multiply with. |
Tom Cosgrove | b496486 | 2022-08-30 11:57:22 +0100 | [diff] [blame] | 289 | * |
| 290 | * \return The carry at the end of the operation. |
| 291 | */ |
Tom Cosgrove | 3bd7bc3 | 2022-09-15 15:55:07 +0100 | [diff] [blame] | 292 | mbedtls_mpi_uint mbedtls_mpi_core_mla( mbedtls_mpi_uint *X, size_t X_limbs, |
| 293 | const mbedtls_mpi_uint *A, size_t A_limbs, |
| 294 | mbedtls_mpi_uint b ); |
Tom Cosgrove | b496486 | 2022-08-30 11:57:22 +0100 | [diff] [blame] | 295 | |
| 296 | /** |
| 297 | * \brief Calculate initialisation value for fast Montgomery modular |
| 298 | * multiplication |
| 299 | * |
| 300 | * \param[in] N Little-endian presentation of the modulus. This must have |
| 301 | * at least one limb. |
| 302 | * |
| 303 | * \return The initialisation value for fast Montgomery modular multiplication |
| 304 | */ |
Tom Cosgrove | b7438d1 | 2022-09-15 15:05:59 +0100 | [diff] [blame] | 305 | mbedtls_mpi_uint mbedtls_mpi_core_montmul_init( const mbedtls_mpi_uint *N ); |
Tom Cosgrove | b496486 | 2022-08-30 11:57:22 +0100 | [diff] [blame] | 306 | |
| 307 | /** |
Tom Cosgrove | 4386ead | 2022-09-29 14:40:21 +0100 | [diff] [blame] | 308 | * \brief Montgomery multiplication: X = A * B * R^-1 mod N (HAC 14.36) |
| 309 | * |
| 310 | * \p A and \p B must be in canonical form. That is, < \p N. |
Tom Cosgrove | b496486 | 2022-08-30 11:57:22 +0100 | [diff] [blame] | 311 | * |
Tom Cosgrove | ea45c1d | 2022-09-20 13:17:51 +0100 | [diff] [blame] | 312 | * \p X may be aliased to \p A or \p N, or even \p B (if \p AN_limbs == |
| 313 | * \p B_limbs) but may not overlap any parameters otherwise. |
| 314 | * |
Tom Cosgrove | 4386ead | 2022-09-29 14:40:21 +0100 | [diff] [blame] | 315 | * \p A and \p B may alias each other, if \p AN_limbs == \p B_limbs. They may |
| 316 | * not alias \p N (since they must be in canonical form, they cannot == \p N). |
Tom Cosgrove | ea45c1d | 2022-09-20 13:17:51 +0100 | [diff] [blame] | 317 | * |
Tom Cosgrove | b496486 | 2022-08-30 11:57:22 +0100 | [diff] [blame] | 318 | * \param[out] X The destination MPI, as a little-endian array of |
| 319 | * length \p AN_limbs. |
| 320 | * On successful completion, X contains the result of |
Tom Cosgrove | 630110a | 2022-08-31 17:09:29 +0100 | [diff] [blame] | 321 | * the multiplication `A * B * R^-1` mod N where |
| 322 | * `R = 2^(biL*AN_limbs)`. |
Tom Cosgrove | b496486 | 2022-08-30 11:57:22 +0100 | [diff] [blame] | 323 | * \param[in] A Little-endian presentation of first operand. |
Tom Cosgrove | 5dd97e6 | 2022-08-30 14:31:49 +0100 | [diff] [blame] | 324 | * Must have the same number of limbs as \p N. |
Tom Cosgrove | b496486 | 2022-08-30 11:57:22 +0100 | [diff] [blame] | 325 | * \param[in] B Little-endian presentation of second operand. |
| 326 | * \param[in] B_limbs The number of limbs in \p B. |
Tom Cosgrove | 5dd97e6 | 2022-08-30 14:31:49 +0100 | [diff] [blame] | 327 | * Must be <= \p AN_limbs. |
Tom Cosgrove | b496486 | 2022-08-30 11:57:22 +0100 | [diff] [blame] | 328 | * \param[in] N Little-endian presentation of the modulus. |
Tom Cosgrove | 5dd97e6 | 2022-08-30 14:31:49 +0100 | [diff] [blame] | 329 | * This must be odd, and have exactly the same number |
| 330 | * of limbs as \p A. |
Tom Cosgrove | 6da3a3b | 2022-09-29 17:20:18 +0100 | [diff] [blame] | 331 | * It may alias \p X, but must not alias or otherwise |
| 332 | * overlap any of the other parameters. |
Tom Cosgrove | 5dd97e6 | 2022-08-30 14:31:49 +0100 | [diff] [blame] | 333 | * \param[in] AN_limbs The number of limbs in \p X, \p A and \p N. |
Tom Cosgrove | 630110a | 2022-08-31 17:09:29 +0100 | [diff] [blame] | 334 | * \param mm The Montgomery constant for \p N: -N^-1 mod 2^biL. |
Tom Cosgrove | b7438d1 | 2022-09-15 15:05:59 +0100 | [diff] [blame] | 335 | * This can be calculated by `mbedtls_mpi_core_montmul_init()`. |
Tom Cosgrove | b496486 | 2022-08-30 11:57:22 +0100 | [diff] [blame] | 336 | * \param[in,out] T Temporary storage of size at least 2*AN_limbs+1 limbs. |
| 337 | * Its initial content is unused and |
| 338 | * its final content is indeterminate. |
Tom Cosgrove | 4386ead | 2022-09-29 14:40:21 +0100 | [diff] [blame] | 339 | * It must not alias or otherwise overlap any of the |
| 340 | * other parameters. |
Tom Cosgrove | b496486 | 2022-08-30 11:57:22 +0100 | [diff] [blame] | 341 | */ |
| 342 | void mbedtls_mpi_core_montmul( mbedtls_mpi_uint *X, |
| 343 | const mbedtls_mpi_uint *A, |
| 344 | const mbedtls_mpi_uint *B, size_t B_limbs, |
| 345 | const mbedtls_mpi_uint *N, size_t AN_limbs, |
| 346 | mbedtls_mpi_uint mm, mbedtls_mpi_uint *T ); |
| 347 | |
Gabor Mezei | b903070 | 2022-07-18 23:09:45 +0200 | [diff] [blame] | 348 | #endif /* MBEDTLS_BIGNUM_CORE_H */ |