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 | 449bd83 | 2023-01-11 14:50:10 +0100 | [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 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 97 | #define BITS_TO_LIMBS(i) ((i) / biL + ((i) % biL != 0)) |
| 98 | #define CHARS_TO_LIMBS(i) ((i) / ciL + ((i) % ciL != 0)) |
Tom Cosgrove | 5eefc3d | 2022-08-31 17:16:50 +0100 | [diff] [blame] | 99 | /* Get a specific byte, without range checks. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 100 | #define GET_BYTE(X, i) \ |
| 101 | (((X)[(i) / ciL] >> (((i) % ciL) * 8)) & 0xff) |
Tom Cosgrove | 5eefc3d | 2022-08-31 17:16:50 +0100 | [diff] [blame] | 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 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50: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 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +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 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 129 | void mbedtls_mpi_core_bigendian_to_host(mbedtls_mpi_uint *A, |
| 130 | size_t A_limbs); |
Janos Follath | 4670f88 | 2022-07-21 18:25:42 +0100 | [diff] [blame] | 131 | |
Gilles Peskine | 6f949ea | 2022-09-20 18:38:35 +0200 | [diff] [blame] | 132 | /** \brief Compare a machine integer with an MPI. |
| 133 | * |
| 134 | * This function operates in constant time with respect |
| 135 | * to the values of \p min and \p A. |
| 136 | * |
| 137 | * \param min A machine integer. |
| 138 | * \param[in] A An MPI. |
| 139 | * \param A_limbs The number of limbs of \p A. |
| 140 | * This must be at least 1. |
| 141 | * |
| 142 | * \return 1 if \p min is less than or equal to \p A, otherwise 0. |
| 143 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 144 | unsigned mbedtls_mpi_core_uint_le_mpi(mbedtls_mpi_uint min, |
| 145 | const mbedtls_mpi_uint *A, |
| 146 | size_t A_limbs); |
Gilles Peskine | 6f949ea | 2022-09-20 18:38:35 +0200 | [diff] [blame] | 147 | |
Gabor Mezei | e1d31c4 | 2022-09-12 16:25:24 +0200 | [diff] [blame] | 148 | /** |
Gabor Mezei | 4086de6 | 2022-10-14 16:29:42 +0200 | [diff] [blame] | 149 | * \brief Perform a safe conditional copy of an MPI which doesn't reveal |
| 150 | * whether assignment was done or not. |
Gabor Mezei | e1d31c4 | 2022-09-12 16:25:24 +0200 | [diff] [blame] | 151 | * |
Gabor Mezei | 4086de6 | 2022-10-14 16:29:42 +0200 | [diff] [blame] | 152 | * \param[out] X The address of the destination MPI. |
| 153 | * This must be initialized. Must have enough limbs to |
| 154 | * store the full value of \p A. |
| 155 | * \param[in] A The address of the source MPI. This must be initialized. |
Gabor Mezei | 1c628d5 | 2022-09-27 12:13:51 +0200 | [diff] [blame] | 156 | * \param limbs The number of limbs of \p A. |
Gabor Mezei | e1d31c4 | 2022-09-12 16:25:24 +0200 | [diff] [blame] | 157 | * \param assign The condition deciding whether to perform the |
| 158 | * assignment or not. Must be either 0 or 1: |
Gabor Mezei | 1c628d5 | 2022-09-27 12:13:51 +0200 | [diff] [blame] | 159 | * * \c 1: Perform the assignment `X = A`. |
Gabor Mezei | e1d31c4 | 2022-09-12 16:25:24 +0200 | [diff] [blame] | 160 | * * \c 0: Keep the original value of \p X. |
| 161 | * |
| 162 | * \note This function avoids leaking any information about whether |
| 163 | * the assignment was done or not. |
| 164 | * |
| 165 | * \warning If \p assign is neither 0 nor 1, the result of this function |
| 166 | * is indeterminate, and the resulting value in \p X might be |
Gabor Mezei | 4086de6 | 2022-10-14 16:29:42 +0200 | [diff] [blame] | 167 | * neither its original value nor the value in \p A. |
Gabor Mezei | e1d31c4 | 2022-09-12 16:25:24 +0200 | [diff] [blame] | 168 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 169 | void mbedtls_mpi_core_cond_assign(mbedtls_mpi_uint *X, |
| 170 | const mbedtls_mpi_uint *A, |
| 171 | size_t limbs, |
| 172 | unsigned char assign); |
Gabor Mezei | e1d31c4 | 2022-09-12 16:25:24 +0200 | [diff] [blame] | 173 | |
| 174 | /** |
Gabor Mezei | 4086de6 | 2022-10-14 16:29:42 +0200 | [diff] [blame] | 175 | * \brief Perform a safe conditional swap of two MPIs which doesn't reveal |
| 176 | * whether the swap was done or not. |
Gabor Mezei | e1d31c4 | 2022-09-12 16:25:24 +0200 | [diff] [blame] | 177 | * |
Gabor Mezei | 86dfe38 | 2022-09-30 14:03:04 +0200 | [diff] [blame] | 178 | * \param[in,out] X The address of the first MPI. |
Gabor Mezei | e1d31c4 | 2022-09-12 16:25:24 +0200 | [diff] [blame] | 179 | * This must be initialized. |
Gabor Mezei | 86dfe38 | 2022-09-30 14:03:04 +0200 | [diff] [blame] | 180 | * \param[in,out] Y The address of the second MPI. |
Gabor Mezei | e1d31c4 | 2022-09-12 16:25:24 +0200 | [diff] [blame] | 181 | * This must be initialized. |
Gabor Mezei | e5b8585 | 2022-09-30 13:54:02 +0200 | [diff] [blame] | 182 | * \param limbs The number of limbs of \p X and \p Y. |
Gabor Mezei | e1d31c4 | 2022-09-12 16:25:24 +0200 | [diff] [blame] | 183 | * \param swap The condition deciding whether to perform |
| 184 | * the swap or not. Must be either 0 or 1: |
Gabor Mezei | e5b8585 | 2022-09-30 13:54:02 +0200 | [diff] [blame] | 185 | * * \c 1: Swap the values of \p X and \p Y. |
| 186 | * * \c 0: Keep the original values of \p X and \p Y. |
Gabor Mezei | e1d31c4 | 2022-09-12 16:25:24 +0200 | [diff] [blame] | 187 | * |
| 188 | * \note This function avoids leaking any information about whether |
| 189 | * the swap was done or not. |
| 190 | * |
| 191 | * \warning If \p swap is neither 0 nor 1, the result of this function |
Gabor Mezei | e5b8585 | 2022-09-30 13:54:02 +0200 | [diff] [blame] | 192 | * is indeterminate, and both \p X and \p Y might end up with |
Gabor Mezei | e1d31c4 | 2022-09-12 16:25:24 +0200 | [diff] [blame] | 193 | * values different to either of the original ones. |
Gabor Mezei | e1d31c4 | 2022-09-12 16:25:24 +0200 | [diff] [blame] | 194 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 195 | void mbedtls_mpi_core_cond_swap(mbedtls_mpi_uint *X, |
| 196 | mbedtls_mpi_uint *Y, |
| 197 | size_t limbs, |
| 198 | unsigned char swap); |
Gabor Mezei | e1d31c4 | 2022-09-12 16:25:24 +0200 | [diff] [blame] | 199 | |
Janos Follath | af3f39c | 2022-08-22 09:06:32 +0100 | [diff] [blame] | 200 | /** Import X from unsigned binary data, little-endian. |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 201 | * |
Janos Follath | 6318468 | 2022-08-11 17:42:59 +0100 | [diff] [blame] | 202 | * The MPI needs to have enough limbs to store the full value (including any |
| 203 | * most significant zero bytes in the input). |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 204 | * |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 205 | * \param[out] X The address of the MPI. |
| 206 | * \param X_limbs The number of limbs of \p X. |
| 207 | * \param[in] input The input buffer to import from. |
| 208 | * \param input_length The length bytes of \p input. |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 209 | * |
| 210 | * \return \c 0 if successful. |
| 211 | * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p X isn't |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 212 | * large enough to hold the value in \p input. |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 213 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 214 | int mbedtls_mpi_core_read_le(mbedtls_mpi_uint *X, |
| 215 | size_t X_limbs, |
| 216 | const unsigned char *input, |
| 217 | size_t input_length); |
Gabor Mezei | b903070 | 2022-07-18 23:09:45 +0200 | [diff] [blame] | 218 | |
Janos Follath | af3f39c | 2022-08-22 09:06:32 +0100 | [diff] [blame] | 219 | /** Import X from unsigned binary data, big-endian. |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 220 | * |
Janos Follath | 6318468 | 2022-08-11 17:42:59 +0100 | [diff] [blame] | 221 | * The MPI needs to have enough limbs to store the full value (including any |
| 222 | * most significant zero bytes in the input). |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 223 | * |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 224 | * \param[out] X The address of the MPI. |
Tom Cosgrove | 8a1f784 | 2023-02-01 08:43:54 +0000 | [diff] [blame] | 225 | * May only be #NULL if \p X_limbs is 0 and \p input_length |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 226 | * is 0. |
| 227 | * \param X_limbs The number of limbs of \p X. |
| 228 | * \param[in] input The input buffer to import from. |
| 229 | * May only be #NULL if \p input_length is 0. |
| 230 | * \param input_length The length in bytes of \p input. |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 231 | * |
| 232 | * \return \c 0 if successful. |
| 233 | * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p X isn't |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 234 | * large enough to hold the value in \p input. |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 235 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 236 | int mbedtls_mpi_core_read_be(mbedtls_mpi_uint *X, |
| 237 | size_t X_limbs, |
| 238 | const unsigned char *input, |
| 239 | size_t input_length); |
Gabor Mezei | b903070 | 2022-07-18 23:09:45 +0200 | [diff] [blame] | 240 | |
Janos Follath | af3f39c | 2022-08-22 09:06:32 +0100 | [diff] [blame] | 241 | /** Export A into unsigned binary data, little-endian. |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 242 | * |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 243 | * \note If \p output is shorter than \p A the export is still successful if the |
| 244 | * value held in \p A fits in the buffer (that is, if enough of the most |
| 245 | * significant bytes of \p A are 0). |
Janos Follath | 6318468 | 2022-08-11 17:42:59 +0100 | [diff] [blame] | 246 | * |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 247 | * \param[in] A The address of the MPI. |
| 248 | * \param A_limbs The number of limbs of \p A. |
| 249 | * \param[out] output The output buffer to export to. |
| 250 | * \param output_length The length in bytes of \p output. |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 251 | * |
| 252 | * \return \c 0 if successful. |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 253 | * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p output isn't |
| 254 | * large enough to hold the value of \p A. |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 255 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 256 | int mbedtls_mpi_core_write_le(const mbedtls_mpi_uint *A, |
| 257 | size_t A_limbs, |
| 258 | unsigned char *output, |
| 259 | size_t output_length); |
Gabor Mezei | b903070 | 2022-07-18 23:09:45 +0200 | [diff] [blame] | 260 | |
Janos Follath | af3f39c | 2022-08-22 09:06:32 +0100 | [diff] [blame] | 261 | /** Export A into unsigned binary data, big-endian. |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 262 | * |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 263 | * \note If \p output is shorter than \p A the export is still successful if the |
| 264 | * value held in \p A fits in the buffer (that is, if enough of the most |
| 265 | * significant bytes of \p A are 0). |
Janos Follath | 6318468 | 2022-08-11 17:42:59 +0100 | [diff] [blame] | 266 | * |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 267 | * \param[in] A The address of the MPI. |
| 268 | * \param A_limbs The number of limbs of \p A. |
| 269 | * \param[out] output The output buffer to export to. |
| 270 | * \param output_length The length in bytes of \p output. |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 271 | * |
| 272 | * \return \c 0 if successful. |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 273 | * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p output isn't |
| 274 | * large enough to hold the value of \p A. |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 275 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 276 | int mbedtls_mpi_core_write_be(const mbedtls_mpi_uint *A, |
| 277 | size_t A_limbs, |
| 278 | unsigned char *output, |
| 279 | size_t output_length); |
Gabor Mezei | b903070 | 2022-07-18 23:09:45 +0200 | [diff] [blame] | 280 | |
Minos Galanakis | ec09e25 | 2023-04-20 14:22:16 +0100 | [diff] [blame^] | 281 | /** \brief Shift an MPI in-place right by a number of bits. |
Gilles Peskine | 6641420 | 2022-09-21 15:36:16 +0200 | [diff] [blame] | 282 | * |
| 283 | * Shifting by more bits than there are bit positions |
| 284 | * in \p X is valid and results in setting \p X to 0. |
| 285 | * |
| 286 | * This function's execution time depends on the value |
| 287 | * of \p count (and of course \p limbs). |
| 288 | * |
| 289 | * \param[in,out] X The number to shift. |
| 290 | * \param limbs The number of limbs of \p X. This must be at least 1. |
| 291 | * \param count The number of bits to shift by. |
| 292 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 293 | void mbedtls_mpi_core_shift_r(mbedtls_mpi_uint *X, size_t limbs, |
| 294 | size_t count); |
Gilles Peskine | 6641420 | 2022-09-21 15:36:16 +0200 | [diff] [blame] | 295 | |
Tom Cosgrove | 90c426b | 2022-08-23 16:15:19 +0100 | [diff] [blame] | 296 | /** |
Minos Galanakis | ec09e25 | 2023-04-20 14:22:16 +0100 | [diff] [blame^] | 297 | * \brief Shift an MPI in-place left by a number of bits. |
Minos Galanakis | ad808dd | 2023-04-20 12:18:41 +0100 | [diff] [blame] | 298 | * |
Minos Galanakis | ec09e25 | 2023-04-20 14:22:16 +0100 | [diff] [blame^] | 299 | * Shifting by more bits than there are bit positions |
| 300 | * in \p X is valid and results in setting \p X to 0. |
Minos Galanakis | ad808dd | 2023-04-20 12:18:41 +0100 | [diff] [blame] | 301 | * |
Minos Galanakis | ec09e25 | 2023-04-20 14:22:16 +0100 | [diff] [blame^] | 302 | * This function's execution time depends on the value |
| 303 | * of \p count (and of course \p limbs). |
| 304 | * \param[in,out] X The number to shift. |
| 305 | * \param limbs The number of limbs of \p X. This must be at least 1. |
| 306 | * \param count The number of bits to shift by. |
Minos Galanakis | ad808dd | 2023-04-20 12:18:41 +0100 | [diff] [blame] | 307 | */ |
Minos Galanakis | ec09e25 | 2023-04-20 14:22:16 +0100 | [diff] [blame^] | 308 | void mbedtls_mpi_core_shift_l(mbedtls_mpi_uint *X, size_t limbs, |
| 309 | size_t count); |
Minos Galanakis | ad808dd | 2023-04-20 12:18:41 +0100 | [diff] [blame] | 310 | |
| 311 | /** |
Tom Cosgrove | af7d44b | 2022-08-24 14:05:26 +0100 | [diff] [blame] | 312 | * \brief Add two fixed-size large unsigned integers, returning the carry. |
Hanno Becker | c988713 | 2022-08-24 12:54:36 +0100 | [diff] [blame] | 313 | * |
Tom Cosgrove | af7d44b | 2022-08-24 14:05:26 +0100 | [diff] [blame] | 314 | * Calculates `A + B` where `A` and `B` have the same size. |
| 315 | * |
Tom Cosgrove | 82f1310 | 2022-10-25 12:46:03 +0100 | [diff] [blame] | 316 | * This function operates modulo `2^(biL*limbs)` and returns the carry |
Hanno Becker | c988713 | 2022-08-24 12:54:36 +0100 | [diff] [blame] | 317 | * (1 if there was a wraparound, and 0 otherwise). |
| 318 | * |
Tom Cosgrove | af7d44b | 2022-08-24 14:05:26 +0100 | [diff] [blame] | 319 | * \p X may be aliased to \p A or \p B. |
Hanno Becker | c988713 | 2022-08-24 12:54:36 +0100 | [diff] [blame] | 320 | * |
Tom Cosgrove | af7d44b | 2022-08-24 14:05:26 +0100 | [diff] [blame] | 321 | * \param[out] X The result of the addition. |
| 322 | * \param[in] A Little-endian presentation of the left operand. |
| 323 | * \param[in] B Little-endian presentation of the right operand. |
| 324 | * \param limbs Number of limbs of \p X, \p A and \p B. |
Hanno Becker | c988713 | 2022-08-24 12:54:36 +0100 | [diff] [blame] | 325 | * |
Tom Cosgrove | af7d44b | 2022-08-24 14:05:26 +0100 | [diff] [blame] | 326 | * \return 1 if `A + B >= 2^(biL*limbs)`, 0 otherwise. |
Hanno Becker | c988713 | 2022-08-24 12:54:36 +0100 | [diff] [blame] | 327 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 328 | mbedtls_mpi_uint mbedtls_mpi_core_add(mbedtls_mpi_uint *X, |
| 329 | const mbedtls_mpi_uint *A, |
| 330 | const mbedtls_mpi_uint *B, |
| 331 | size_t limbs); |
Hanno Becker | c988713 | 2022-08-24 12:54:36 +0100 | [diff] [blame] | 332 | |
Tom Cosgrove | 90c426b | 2022-08-23 16:15:19 +0100 | [diff] [blame] | 333 | /** |
Tom Cosgrove | 5c0e810 | 2022-09-15 15:46:10 +0100 | [diff] [blame] | 334 | * \brief Conditional addition of two fixed-size large unsigned integers, |
Tom Cosgrove | d932de8 | 2022-08-25 16:43:43 +0100 | [diff] [blame] | 335 | * returning the carry. |
Hanno Becker | 71f4b0d | 2022-08-23 12:09:35 +0100 | [diff] [blame] | 336 | * |
| 337 | * Functionally equivalent to |
| 338 | * |
| 339 | * ``` |
| 340 | * if( cond ) |
Tom Cosgrove | 3bd7bc3 | 2022-09-15 15:55:07 +0100 | [diff] [blame] | 341 | * X += A; |
Hanno Becker | 71f4b0d | 2022-08-23 12:09:35 +0100 | [diff] [blame] | 342 | * return carry; |
| 343 | * ``` |
| 344 | * |
Tom Cosgrove | 4782823 | 2022-09-20 13:51:50 +0100 | [diff] [blame] | 345 | * This function operates modulo `2^(biL*limbs)`. |
| 346 | * |
Tom Cosgrove | 3bd7bc3 | 2022-09-15 15:55:07 +0100 | [diff] [blame] | 347 | * \param[in,out] X The pointer to the (little-endian) array |
Tom Cosgrove | 7259463 | 2022-08-24 11:51:58 +0100 | [diff] [blame] | 348 | * representing the bignum to accumulate onto. |
Tom Cosgrove | 3bd7bc3 | 2022-09-15 15:55:07 +0100 | [diff] [blame] | 349 | * \param[in] A The pointer to the (little-endian) array |
Tom Cosgrove | 7259463 | 2022-08-24 11:51:58 +0100 | [diff] [blame] | 350 | * representing the bignum to conditionally add |
Tom Cosgrove | 3bd7bc3 | 2022-09-15 15:55:07 +0100 | [diff] [blame] | 351 | * 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] | 352 | * overlap otherwise. |
Tom Cosgrove | 3bd7bc3 | 2022-09-15 15:55:07 +0100 | [diff] [blame] | 353 | * \param limbs Number of limbs of \p X and \p A. |
Tom Cosgrove | 7259463 | 2022-08-24 11:51:58 +0100 | [diff] [blame] | 354 | * \param cond Condition bit dictating whether addition should |
| 355 | * happen or not. This must be \c 0 or \c 1. |
Hanno Becker | 71f4b0d | 2022-08-23 12:09:35 +0100 | [diff] [blame] | 356 | * |
Tom Cosgrove | ecbb124 | 2022-08-25 10:13:44 +0100 | [diff] [blame] | 357 | * \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] | 358 | * is unspecified, and the resulting value in \p X might be |
| 359 | * neither its original value nor \p X + \p A. |
Tom Cosgrove | 7259463 | 2022-08-24 11:51:58 +0100 | [diff] [blame] | 360 | * |
Tom Cosgrove | 3bd7bc3 | 2022-09-15 15:55:07 +0100 | [diff] [blame] | 361 | * \return 1 if `X + cond * A >= 2^(biL*limbs)`, 0 otherwise. |
Hanno Becker | 71f4b0d | 2022-08-23 12:09:35 +0100 | [diff] [blame] | 362 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 363 | mbedtls_mpi_uint mbedtls_mpi_core_add_if(mbedtls_mpi_uint *X, |
| 364 | const mbedtls_mpi_uint *A, |
| 365 | size_t limbs, |
| 366 | unsigned cond); |
Hanno Becker | 71f4b0d | 2022-08-23 12:09:35 +0100 | [diff] [blame] | 367 | |
Tom Cosgrove | b496486 | 2022-08-30 11:57:22 +0100 | [diff] [blame] | 368 | /** |
Tom Cosgrove | 5c0e810 | 2022-09-15 15:46:10 +0100 | [diff] [blame] | 369 | * \brief Subtract two fixed-size large unsigned integers, returning the borrow. |
Tom Cosgrove | b496486 | 2022-08-30 11:57:22 +0100 | [diff] [blame] | 370 | * |
Tom Cosgrove | 5dd97e6 | 2022-08-30 14:31:49 +0100 | [diff] [blame] | 371 | * 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] | 372 | * This function operates modulo `2^(biL*limbs)` and returns the carry |
Tom Cosgrove | b496486 | 2022-08-30 11:57:22 +0100 | [diff] [blame] | 373 | * (1 if there was a wraparound, i.e. if `A < B`, and 0 otherwise). |
| 374 | * |
Tom Cosgrove | 5dd97e6 | 2022-08-30 14:31:49 +0100 | [diff] [blame] | 375 | * \p X may be aliased to \p A or \p B, or even both, but may not overlap |
| 376 | * either otherwise. |
Tom Cosgrove | b496486 | 2022-08-30 11:57:22 +0100 | [diff] [blame] | 377 | * |
| 378 | * \param[out] X The result of the subtraction. |
| 379 | * \param[in] A Little-endian presentation of left operand. |
| 380 | * \param[in] B Little-endian presentation of right operand. |
| 381 | * \param limbs Number of limbs of \p X, \p A and \p B. |
| 382 | * |
| 383 | * \return 1 if `A < B`. |
| 384 | * 0 if `A >= B`. |
| 385 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 386 | mbedtls_mpi_uint mbedtls_mpi_core_sub(mbedtls_mpi_uint *X, |
| 387 | const mbedtls_mpi_uint *A, |
| 388 | const mbedtls_mpi_uint *B, |
| 389 | size_t limbs); |
Tom Cosgrove | b496486 | 2022-08-30 11:57:22 +0100 | [diff] [blame] | 390 | |
| 391 | /** |
Tom Cosgrove | 3bd7bc3 | 2022-09-15 15:55:07 +0100 | [diff] [blame] | 392 | * \brief Perform a fixed-size multiply accumulate operation: X += b * A |
Tom Cosgrove | b496486 | 2022-08-30 11:57:22 +0100 | [diff] [blame] | 393 | * |
Tom Cosgrove | b0b77e1 | 2022-09-20 13:33:40 +0100 | [diff] [blame] | 394 | * \p X may be aliased to \p A (when \p X_limbs == \p A_limbs), but may not |
| 395 | * otherwise overlap. |
| 396 | * |
Tom Cosgrove | 4782823 | 2022-09-20 13:51:50 +0100 | [diff] [blame] | 397 | * This function operates modulo `2^(biL*X_limbs)`. |
| 398 | * |
Tom Cosgrove | 3bd7bc3 | 2022-09-15 15:55:07 +0100 | [diff] [blame] | 399 | * \param[in,out] X The pointer to the (little-endian) array |
Tom Cosgrove | b496486 | 2022-08-30 11:57:22 +0100 | [diff] [blame] | 400 | * representing the bignum to accumulate onto. |
Tom Cosgrove | 3bd7bc3 | 2022-09-15 15:55:07 +0100 | [diff] [blame] | 401 | * \param X_limbs The number of limbs of \p X. This must be |
| 402 | * at least \p A_limbs. |
| 403 | * \param[in] A The pointer to the (little-endian) array |
Tom Cosgrove | b496486 | 2022-08-30 11:57:22 +0100 | [diff] [blame] | 404 | * representing the bignum to multiply with. |
Tom Cosgrove | 3bd7bc3 | 2022-09-15 15:55:07 +0100 | [diff] [blame] | 405 | * This may be aliased to \p X but may not overlap |
Tom Cosgrove | ed43c6c | 2022-08-31 11:35:00 +0100 | [diff] [blame] | 406 | * otherwise. |
Tom Cosgrove | 3bd7bc3 | 2022-09-15 15:55:07 +0100 | [diff] [blame] | 407 | * \param A_limbs The number of limbs of \p A. |
| 408 | * \param b X scalar to multiply with. |
Tom Cosgrove | b496486 | 2022-08-30 11:57:22 +0100 | [diff] [blame] | 409 | * |
| 410 | * \return The carry at the end of the operation. |
| 411 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 412 | mbedtls_mpi_uint mbedtls_mpi_core_mla(mbedtls_mpi_uint *X, size_t X_limbs, |
| 413 | const mbedtls_mpi_uint *A, size_t A_limbs, |
| 414 | mbedtls_mpi_uint b); |
Tom Cosgrove | b496486 | 2022-08-30 11:57:22 +0100 | [diff] [blame] | 415 | |
Hanno Becker | 4ae890b | 2022-08-24 16:17:52 +0100 | [diff] [blame] | 416 | /** |
| 417 | * \brief Perform a known-size multiplication |
| 418 | * |
Gabor Mezei | d626051 | 2023-04-03 17:32:55 +0200 | [diff] [blame] | 419 | * \p X may not be aliased to any of the inputs for this function. |
Gabor Mezei | 6f182c3 | 2023-03-31 16:09:28 +0200 | [diff] [blame] | 420 | * \p A may be aliased to \p B. |
| 421 | * |
Tom Cosgrove | 6af26f3 | 2022-08-24 16:40:55 +0100 | [diff] [blame] | 422 | * \param[out] X The pointer to the (little-endian) array to receive |
| 423 | * the product of \p A_limbs and \p B_limbs. |
| 424 | * This must be of length \p A_limbs + \p B_limbs. |
| 425 | * \param[in] A The pointer to the (little-endian) array |
| 426 | * representing the first factor. |
| 427 | * \param A_limbs The number of limbs in \p A. |
| 428 | * \param[in] B The pointer to the (little-endian) array |
| 429 | * representing the second factor. |
| 430 | * \param B_limbs The number of limbs in \p B. |
Hanno Becker | 4ae890b | 2022-08-24 16:17:52 +0100 | [diff] [blame] | 431 | */ |
Tom Cosgrove | 6af26f3 | 2022-08-24 16:40:55 +0100 | [diff] [blame] | 432 | void mbedtls_mpi_core_mul(mbedtls_mpi_uint *X, |
| 433 | const mbedtls_mpi_uint *A, size_t A_limbs, |
| 434 | const mbedtls_mpi_uint *B, size_t B_limbs); |
Hanno Becker | 4ae890b | 2022-08-24 16:17:52 +0100 | [diff] [blame] | 435 | |
Tom Cosgrove | b496486 | 2022-08-30 11:57:22 +0100 | [diff] [blame] | 436 | /** |
| 437 | * \brief Calculate initialisation value for fast Montgomery modular |
| 438 | * multiplication |
| 439 | * |
| 440 | * \param[in] N Little-endian presentation of the modulus. This must have |
| 441 | * at least one limb. |
| 442 | * |
| 443 | * \return The initialisation value for fast Montgomery modular multiplication |
| 444 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 445 | 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] | 446 | |
| 447 | /** |
Tom Cosgrove | 4386ead | 2022-09-29 14:40:21 +0100 | [diff] [blame] | 448 | * \brief Montgomery multiplication: X = A * B * R^-1 mod N (HAC 14.36) |
| 449 | * |
| 450 | * \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] | 451 | * |
Tom Cosgrove | ea45c1d | 2022-09-20 13:17:51 +0100 | [diff] [blame] | 452 | * \p X may be aliased to \p A or \p N, or even \p B (if \p AN_limbs == |
| 453 | * \p B_limbs) but may not overlap any parameters otherwise. |
| 454 | * |
Tom Cosgrove | 4386ead | 2022-09-29 14:40:21 +0100 | [diff] [blame] | 455 | * \p A and \p B may alias each other, if \p AN_limbs == \p B_limbs. They may |
| 456 | * 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] | 457 | * |
Tom Cosgrove | b496486 | 2022-08-30 11:57:22 +0100 | [diff] [blame] | 458 | * \param[out] X The destination MPI, as a little-endian array of |
| 459 | * length \p AN_limbs. |
| 460 | * On successful completion, X contains the result of |
Tom Cosgrove | 630110a | 2022-08-31 17:09:29 +0100 | [diff] [blame] | 461 | * the multiplication `A * B * R^-1` mod N where |
| 462 | * `R = 2^(biL*AN_limbs)`. |
Tom Cosgrove | b496486 | 2022-08-30 11:57:22 +0100 | [diff] [blame] | 463 | * \param[in] A Little-endian presentation of first operand. |
Tom Cosgrove | 5dd97e6 | 2022-08-30 14:31:49 +0100 | [diff] [blame] | 464 | * Must have the same number of limbs as \p N. |
Tom Cosgrove | b496486 | 2022-08-30 11:57:22 +0100 | [diff] [blame] | 465 | * \param[in] B Little-endian presentation of second operand. |
| 466 | * \param[in] B_limbs The number of limbs in \p B. |
Tom Cosgrove | 5dd97e6 | 2022-08-30 14:31:49 +0100 | [diff] [blame] | 467 | * Must be <= \p AN_limbs. |
Tom Cosgrove | b496486 | 2022-08-30 11:57:22 +0100 | [diff] [blame] | 468 | * \param[in] N Little-endian presentation of the modulus. |
Tom Cosgrove | 5dd97e6 | 2022-08-30 14:31:49 +0100 | [diff] [blame] | 469 | * This must be odd, and have exactly the same number |
| 470 | * of limbs as \p A. |
Tom Cosgrove | 6da3a3b | 2022-09-29 17:20:18 +0100 | [diff] [blame] | 471 | * It may alias \p X, but must not alias or otherwise |
| 472 | * overlap any of the other parameters. |
Tom Cosgrove | 5dd97e6 | 2022-08-30 14:31:49 +0100 | [diff] [blame] | 473 | * \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] | 474 | * \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] | 475 | * This can be calculated by `mbedtls_mpi_core_montmul_init()`. |
Tom Cosgrove | b496486 | 2022-08-30 11:57:22 +0100 | [diff] [blame] | 476 | * \param[in,out] T Temporary storage of size at least 2*AN_limbs+1 limbs. |
| 477 | * Its initial content is unused and |
| 478 | * its final content is indeterminate. |
Tom Cosgrove | 4386ead | 2022-09-29 14:40:21 +0100 | [diff] [blame] | 479 | * It must not alias or otherwise overlap any of the |
| 480 | * other parameters. |
Tom Cosgrove | b496486 | 2022-08-30 11:57:22 +0100 | [diff] [blame] | 481 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 482 | void mbedtls_mpi_core_montmul(mbedtls_mpi_uint *X, |
| 483 | const mbedtls_mpi_uint *A, |
| 484 | const mbedtls_mpi_uint *B, size_t B_limbs, |
| 485 | const mbedtls_mpi_uint *N, size_t AN_limbs, |
| 486 | mbedtls_mpi_uint mm, mbedtls_mpi_uint *T); |
Tom Cosgrove | b496486 | 2022-08-30 11:57:22 +0100 | [diff] [blame] | 487 | |
Hanno Becker | ec440f2 | 2022-08-11 17:29:32 +0100 | [diff] [blame] | 488 | /** |
Minos Galanakis | b85506e | 2022-10-20 09:51:53 +0100 | [diff] [blame] | 489 | * \brief Calculate the square of the Montgomery constant. (Needed |
| 490 | * for conversion and operations in Montgomery form.) |
Hanno Becker | ec440f2 | 2022-08-11 17:29:32 +0100 | [diff] [blame] | 491 | * |
| 492 | * \param[out] X A pointer to the result of the calculation of |
Minos Galanakis | b85506e | 2022-10-20 09:51:53 +0100 | [diff] [blame] | 493 | * the square of the Montgomery constant: |
| 494 | * 2^{2*n*biL} mod N. |
Hanno Becker | ec440f2 | 2022-08-11 17:29:32 +0100 | [diff] [blame] | 495 | * \param[in] N Little-endian presentation of the modulus, which must be odd. |
| 496 | * |
| 497 | * \return 0 if successful. |
| 498 | * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if there is not enough space |
| 499 | * to store the value of Montgomery constant squared. |
| 500 | * \return #MBEDTLS_ERR_MPI_DIVISION_BY_ZERO if \p N modulus is zero. |
| 501 | * \return #MBEDTLS_ERR_MPI_NEGATIVE_VALUE if \p N modulus is negative. |
Hanno Becker | ec440f2 | 2022-08-11 17:29:32 +0100 | [diff] [blame] | 502 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 503 | int mbedtls_mpi_core_get_mont_r2_unsafe(mbedtls_mpi *X, |
| 504 | const mbedtls_mpi *N); |
Hanno Becker | ec440f2 | 2022-08-11 17:29:32 +0100 | [diff] [blame] | 505 | |
Janos Follath | 59cbd1b | 2022-10-28 18:13:43 +0100 | [diff] [blame] | 506 | #if defined(MBEDTLS_TEST_HOOKS) |
Janos Follath | e50f2f1 | 2022-10-26 15:14:33 +0100 | [diff] [blame] | 507 | /** |
Janos Follath | 8904a2d | 2022-10-31 15:32:28 +0000 | [diff] [blame] | 508 | * Copy an MPI from a table without leaking the index. |
Janos Follath | e50f2f1 | 2022-10-26 15:14:33 +0100 | [diff] [blame] | 509 | * |
| 510 | * \param dest The destination buffer. This must point to a writable |
| 511 | * buffer of at least \p limbs limbs. |
| 512 | * \param table The address of the table. This must point to a readable |
Janos Follath | 8904a2d | 2022-10-31 15:32:28 +0000 | [diff] [blame] | 513 | * array of \p count elements of \p limbs limbs each. |
| 514 | * \param limbs The number of limbs in each table entry. |
| 515 | * \param count The number of entries in \p table. |
| 516 | * \param index The (secret) table index to look up. This must be in the |
| 517 | * range `0 .. count-1`. |
Janos Follath | e50f2f1 | 2022-10-26 15:14:33 +0100 | [diff] [blame] | 518 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 519 | void mbedtls_mpi_core_ct_uint_table_lookup(mbedtls_mpi_uint *dest, |
| 520 | const mbedtls_mpi_uint *table, |
| 521 | size_t limbs, |
| 522 | size_t count, |
| 523 | size_t index); |
Janos Follath | 59cbd1b | 2022-10-28 18:13:43 +0100 | [diff] [blame] | 524 | #endif /* MBEDTLS_TEST_HOOKS */ |
Janos Follath | e50f2f1 | 2022-10-26 15:14:33 +0100 | [diff] [blame] | 525 | |
Gilles Peskine | 909e03c | 2022-10-18 18:14:33 +0200 | [diff] [blame] | 526 | /** |
| 527 | * \brief Fill an integer with a number of random bytes. |
| 528 | * |
| 529 | * \param X The destination MPI. |
| 530 | * \param X_limbs The number of limbs of \p X. |
| 531 | * \param bytes The number of random bytes to generate. |
| 532 | * \param f_rng The RNG function to use. This must not be \c NULL. |
| 533 | * \param p_rng The RNG parameter to be passed to \p f_rng. This may be |
| 534 | * \c NULL if \p f_rng doesn't need a context argument. |
| 535 | * |
| 536 | * \return \c 0 if successful. |
| 537 | * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if \p X does not have |
| 538 | * enough room for \p bytes bytes. |
| 539 | * \return A negative error code on RNG failure. |
| 540 | * |
| 541 | * \note The bytes obtained from the RNG are interpreted |
| 542 | * as a big-endian representation of an MPI; this can |
| 543 | * be relevant in applications like deterministic ECDSA. |
| 544 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 545 | int mbedtls_mpi_core_fill_random(mbedtls_mpi_uint *X, size_t X_limbs, |
| 546 | size_t bytes, |
| 547 | int (*f_rng)(void *, unsigned char *, size_t), |
| 548 | void *p_rng); |
Gilles Peskine | 909e03c | 2022-10-18 18:14:33 +0200 | [diff] [blame] | 549 | |
Gilles Peskine | 4a8c5cd | 2022-10-18 18:15:01 +0200 | [diff] [blame] | 550 | /** Generate a random number uniformly in a range. |
| 551 | * |
| 552 | * This function generates a random number between \p min inclusive and |
| 553 | * \p N exclusive. |
| 554 | * |
| 555 | * The procedure complies with RFC 6979 §3.3 (deterministic ECDSA) |
| 556 | * when the RNG is a suitably parametrized instance of HMAC_DRBG |
| 557 | * and \p min is \c 1. |
| 558 | * |
| 559 | * \note There are `N - min` possible outputs. The lower bound |
| 560 | * \p min can be reached, but the upper bound \p N cannot. |
| 561 | * |
| 562 | * \param X The destination MPI, with \p limbs limbs. |
| 563 | * It must not be aliased with \p N or otherwise overlap it. |
| 564 | * \param min The minimum value to return. |
| 565 | * \param N The upper bound of the range, exclusive, with \p limbs limbs. |
| 566 | * In other words, this is one plus the maximum value to return. |
| 567 | * \p N must be strictly larger than \p min. |
| 568 | * \param limbs The number of limbs of \p N and \p X. |
| 569 | * This must not be 0. |
| 570 | * \param f_rng The RNG function to use. This must not be \c NULL. |
| 571 | * \param p_rng The RNG parameter to be passed to \p f_rng. |
| 572 | * |
| 573 | * \return \c 0 if successful. |
| 574 | * \return #MBEDTLS_ERR_MPI_NOT_ACCEPTABLE if the implementation was |
| 575 | * unable to find a suitable value within a limited number |
| 576 | * of attempts. This has a negligible probability if \p N |
| 577 | * is significantly larger than \p min, which is the case |
| 578 | * for all usual cryptographic applications. |
| 579 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 580 | int mbedtls_mpi_core_random(mbedtls_mpi_uint *X, |
| 581 | mbedtls_mpi_uint min, |
| 582 | const mbedtls_mpi_uint *N, |
| 583 | size_t limbs, |
| 584 | int (*f_rng)(void *, unsigned char *, size_t), |
| 585 | void *p_rng); |
Gilles Peskine | 4a8c5cd | 2022-10-18 18:15:01 +0200 | [diff] [blame] | 586 | |
Janos Follath | b6673f0 | 2022-09-30 14:13:14 +0100 | [diff] [blame] | 587 | /** |
Tom Cosgrove | 0a0dded | 2022-12-06 14:37:18 +0000 | [diff] [blame] | 588 | * \brief Returns the number of limbs of working memory required for |
| 589 | * a call to `mbedtls_mpi_core_exp_mod()`. |
Janos Follath | b6673f0 | 2022-09-30 14:13:14 +0100 | [diff] [blame] | 590 | * |
Tom Cosgrove | 28ff92c | 2022-12-12 17:06:27 +0000 | [diff] [blame] | 591 | * \note This will always be at least |
| 592 | * `mbedtls_mpi_core_montmul_working_limbs(AN_limbs)`, |
| 593 | * i.e. sufficient for a call to `mbedtls_mpi_core_montmul()`. |
| 594 | * |
Tom Cosgrove | 0a0dded | 2022-12-06 14:37:18 +0000 | [diff] [blame] | 595 | * \param AN_limbs The number of limbs in the input `A` and the modulus `N` |
| 596 | * (they must be the same size) that will be given to |
| 597 | * `mbedtls_mpi_core_exp_mod()`. |
| 598 | * \param E_limbs The number of limbs in the exponent `E` that will be given |
| 599 | * to `mbedtls_mpi_core_exp_mod()`. |
Janos Follath | b6673f0 | 2022-09-30 14:13:14 +0100 | [diff] [blame] | 600 | * |
Tom Cosgrove | 0a0dded | 2022-12-06 14:37:18 +0000 | [diff] [blame] | 601 | * \return The number of limbs of working memory required by |
| 602 | * `mbedtls_mpi_core_exp_mod()`. |
Janos Follath | b6673f0 | 2022-09-30 14:13:14 +0100 | [diff] [blame] | 603 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 604 | size_t mbedtls_mpi_core_exp_mod_working_limbs(size_t AN_limbs, size_t E_limbs); |
Tom Cosgrove | 0a0dded | 2022-12-06 14:37:18 +0000 | [diff] [blame] | 605 | |
| 606 | /** |
| 607 | * \brief Perform a modular exponentiation with secret exponent: |
| 608 | * X = A^E mod N, where \p A is already in Montgomery form. |
| 609 | * |
Tom Cosgrove | a7f0d7b | 2022-12-07 13:29:07 +0000 | [diff] [blame] | 610 | * \p X may be aliased to \p A, but not to \p RR or \p E, even if \p E_limbs == |
| 611 | * \p AN_limbs. |
| 612 | * |
Tom Cosgrove | 0a0dded | 2022-12-06 14:37:18 +0000 | [diff] [blame] | 613 | * \param[out] X The destination MPI, as a little endian array of length |
| 614 | * \p AN_limbs. |
| 615 | * \param[in] A The base MPI, as a little endian array of length \p AN_limbs. |
| 616 | * Must be in Montgomery form. |
| 617 | * \param[in] N The modulus, as a little endian array of length \p AN_limbs. |
| 618 | * \param AN_limbs The number of limbs in \p X, \p A, \p N, \p RR. |
| 619 | * \param[in] E The exponent, as a little endian array of length \p E_limbs. |
| 620 | * \param E_limbs The number of limbs in \p E. |
| 621 | * \param[in] RR The precomputed residue of 2^{2*biL} modulo N, as a little |
| 622 | * endian array of length \p AN_limbs. |
| 623 | * \param[in,out] T Temporary storage of at least the number of limbs returned |
| 624 | * by `mbedtls_mpi_core_exp_mod_working_limbs()`. |
| 625 | * Its initial content is unused and its final content is |
| 626 | * indeterminate. |
| 627 | * It must not alias or otherwise overlap any of the other |
| 628 | * parameters. |
| 629 | * It is up to the caller to zeroize \p T when it is no |
| 630 | * longer needed, and before freeing it if it was dynamically |
| 631 | * allocated. |
| 632 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 633 | void mbedtls_mpi_core_exp_mod(mbedtls_mpi_uint *X, |
| 634 | const mbedtls_mpi_uint *A, |
| 635 | const mbedtls_mpi_uint *N, size_t AN_limbs, |
| 636 | const mbedtls_mpi_uint *E, size_t E_limbs, |
| 637 | const mbedtls_mpi_uint *RR, |
| 638 | mbedtls_mpi_uint *T); |
Janos Follath | b6673f0 | 2022-09-30 14:13:14 +0100 | [diff] [blame] | 639 | |
Hanno Becker | d9b2348 | 2022-08-25 08:25:19 +0100 | [diff] [blame] | 640 | /** |
| 641 | * \brief Subtract unsigned integer from known-size large unsigned integers. |
| 642 | * Return the borrow. |
| 643 | * |
Tom Cosgrove | f7ff4c9 | 2022-08-25 08:39:07 +0100 | [diff] [blame] | 644 | * \param[out] X The result of the subtraction. |
| 645 | * \param[in] A The left operand. |
| 646 | * \param b The unsigned scalar to subtract. |
| 647 | * \param limbs Number of limbs of \p X and \p A. |
Hanno Becker | d9b2348 | 2022-08-25 08:25:19 +0100 | [diff] [blame] | 648 | * |
Tom Cosgrove | f7ff4c9 | 2022-08-25 08:39:07 +0100 | [diff] [blame] | 649 | * \return 1 if `A < b`. |
| 650 | * 0 if `A >= b`. |
Hanno Becker | d9b2348 | 2022-08-25 08:25:19 +0100 | [diff] [blame] | 651 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 652 | mbedtls_mpi_uint mbedtls_mpi_core_sub_int(mbedtls_mpi_uint *X, |
| 653 | const mbedtls_mpi_uint *A, |
| 654 | mbedtls_mpi_uint b, |
| 655 | size_t limbs); |
Hanno Becker | d9b2348 | 2022-08-25 08:25:19 +0100 | [diff] [blame] | 656 | |
Tom Cosgrove | 30f3b4d | 2022-12-12 16:54:57 +0000 | [diff] [blame] | 657 | /** |
| 658 | * \brief Determine if a given MPI has the value \c 0 in constant time with |
| 659 | * respect to the value (but not with respect to the number of limbs). |
| 660 | * |
| 661 | * \param[in] A The MPI to test. |
| 662 | * \param limbs Number of limbs in \p A. |
| 663 | * |
| 664 | * \return 0 if `A == 0` |
| 665 | * non-0 (may be any value) if `A != 0`. |
| 666 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 667 | mbedtls_mpi_uint mbedtls_mpi_core_check_zero_ct(const mbedtls_mpi_uint *A, |
| 668 | size_t limbs); |
Tom Cosgrove | 30f3b4d | 2022-12-12 16:54:57 +0000 | [diff] [blame] | 669 | |
Tom Cosgrove | 28ff92c | 2022-12-12 17:06:27 +0000 | [diff] [blame] | 670 | /** |
| 671 | * \brief Returns the number of limbs of working memory required for |
| 672 | * a call to `mbedtls_mpi_core_montmul()`. |
| 673 | * |
| 674 | * \param AN_limbs The number of limbs in the input `A` and the modulus `N` |
| 675 | * (they must be the same size) that will be given to |
| 676 | * `mbedtls_mpi_core_montmul()` or one of the other functions |
| 677 | * that specifies this as the amount of working memory needed. |
| 678 | * |
| 679 | * \return The number of limbs of working memory required by |
| 680 | * `mbedtls_mpi_core_montmul()` (or other similar function). |
| 681 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 682 | static inline size_t mbedtls_mpi_core_montmul_working_limbs(size_t AN_limbs) |
Tom Cosgrove | 28ff92c | 2022-12-12 17:06:27 +0000 | [diff] [blame] | 683 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 684 | return 2 * AN_limbs + 1; |
Tom Cosgrove | 28ff92c | 2022-12-12 17:06:27 +0000 | [diff] [blame] | 685 | } |
| 686 | |
Tom Cosgrove | 786848b | 2022-12-13 10:45:19 +0000 | [diff] [blame] | 687 | /** Convert an MPI into Montgomery form. |
| 688 | * |
| 689 | * \p X may be aliased to \p A, but may not otherwise overlap it. |
| 690 | * |
Tom Cosgrove | 5c8505f | 2023-03-07 11:39:52 +0000 | [diff] [blame] | 691 | * \p X may not alias \p N (it is in canonical form, so must be strictly less |
Tom Cosgrove | 786848b | 2022-12-13 10:45:19 +0000 | [diff] [blame] | 692 | * than \p N). Nor may it alias or overlap \p rr (this is unlikely to be |
| 693 | * required in practice.) |
| 694 | * |
| 695 | * This function is a thin wrapper around `mbedtls_mpi_core_montmul()` that is |
| 696 | * an alternative to calling `mbedtls_mpi_mod_raw_to_mont_rep()` when we |
| 697 | * don't want to allocate memory. |
| 698 | * |
| 699 | * \param[out] X The result of the conversion. |
| 700 | * Must have the same number of limbs as \p A. |
| 701 | * \param[in] A The MPI to convert into Montgomery form. |
| 702 | * Must have the same number of limbs as the modulus. |
| 703 | * \param[in] N The address of the modulus, which gives the size of |
Tom Cosgrove | f723754 | 2022-12-16 16:10:36 +0000 | [diff] [blame] | 704 | * the base `R` = 2^(biL*N->limbs). |
Tom Cosgrove | 786848b | 2022-12-13 10:45:19 +0000 | [diff] [blame] | 705 | * \param[in] AN_limbs The number of limbs in \p X, \p A, \p N and \p rr. |
| 706 | * \param mm The Montgomery constant for \p N: -N^-1 mod 2^biL. |
Tom Cosgrove | b38c2ed | 2022-12-14 13:11:46 +0000 | [diff] [blame] | 707 | * This can be determined by calling |
Tom Cosgrove | 786848b | 2022-12-13 10:45:19 +0000 | [diff] [blame] | 708 | * `mbedtls_mpi_core_montmul_init()`. |
| 709 | * \param[in] rr The residue for `2^{2*n*biL} mod N`. |
| 710 | * \param[in,out] T Temporary storage of size at least |
| 711 | * `mbedtls_mpi_core_montmul_working_limbs(AN_limbs)` |
| 712 | * limbs. |
| 713 | * Its initial content is unused and |
| 714 | * its final content is indeterminate. |
| 715 | * It must not alias or otherwise overlap any of the |
| 716 | * other parameters. |
| 717 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 718 | void mbedtls_mpi_core_to_mont_rep(mbedtls_mpi_uint *X, |
| 719 | const mbedtls_mpi_uint *A, |
| 720 | const mbedtls_mpi_uint *N, |
| 721 | size_t AN_limbs, |
| 722 | mbedtls_mpi_uint mm, |
| 723 | const mbedtls_mpi_uint *rr, |
| 724 | mbedtls_mpi_uint *T); |
Tom Cosgrove | 786848b | 2022-12-13 10:45:19 +0000 | [diff] [blame] | 725 | |
| 726 | /** Convert an MPI from Montgomery form. |
| 727 | * |
| 728 | * \p X may be aliased to \p A, but may not otherwise overlap it. |
| 729 | * |
Tom Cosgrove | 5c8505f | 2023-03-07 11:39:52 +0000 | [diff] [blame] | 730 | * \p X may not alias \p N (it is in canonical form, so must be strictly less |
Tom Cosgrove | 786848b | 2022-12-13 10:45:19 +0000 | [diff] [blame] | 731 | * than \p N). |
| 732 | * |
| 733 | * This function is a thin wrapper around `mbedtls_mpi_core_montmul()` that is |
| 734 | * an alternative to calling `mbedtls_mpi_mod_raw_from_mont_rep()` when we |
| 735 | * don't want to allocate memory. |
| 736 | * |
| 737 | * \param[out] X The result of the conversion. |
| 738 | * Must have the same number of limbs as \p A. |
| 739 | * \param[in] A The MPI to convert from Montgomery form. |
| 740 | * Must have the same number of limbs as the modulus. |
| 741 | * \param[in] N The address of the modulus, which gives the size of |
Tom Cosgrove | f723754 | 2022-12-16 16:10:36 +0000 | [diff] [blame] | 742 | * the base `R` = 2^(biL*N->limbs). |
Tom Cosgrove | 786848b | 2022-12-13 10:45:19 +0000 | [diff] [blame] | 743 | * \param[in] AN_limbs The number of limbs in \p X, \p A and \p N. |
| 744 | * \param mm The Montgomery constant for \p N: -N^-1 mod 2^biL. |
Tom Cosgrove | b38c2ed | 2022-12-14 13:11:46 +0000 | [diff] [blame] | 745 | * This can be determined by calling |
Tom Cosgrove | 786848b | 2022-12-13 10:45:19 +0000 | [diff] [blame] | 746 | * `mbedtls_mpi_core_montmul_init()`. |
| 747 | * \param[in,out] T Temporary storage of size at least |
| 748 | * `mbedtls_mpi_core_montmul_working_limbs(AN_limbs)` |
| 749 | * limbs. |
| 750 | * Its initial content is unused and |
| 751 | * its final content is indeterminate. |
| 752 | * It must not alias or otherwise overlap any of the |
| 753 | * other parameters. |
| 754 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 755 | void mbedtls_mpi_core_from_mont_rep(mbedtls_mpi_uint *X, |
| 756 | const mbedtls_mpi_uint *A, |
| 757 | const mbedtls_mpi_uint *N, |
| 758 | size_t AN_limbs, |
| 759 | mbedtls_mpi_uint mm, |
| 760 | mbedtls_mpi_uint *T); |
Tom Cosgrove | 786848b | 2022-12-13 10:45:19 +0000 | [diff] [blame] | 761 | |
Gabor Mezei | b903070 | 2022-07-18 23:09:45 +0200 | [diff] [blame] | 762 | #endif /* MBEDTLS_BIGNUM_CORE_H */ |