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 | |
Gabor Mezei | e1d31c4 | 2022-09-12 16:25:24 +0200 | [diff] [blame] | 132 | /** |
Gabor Mezei | 4086de6 | 2022-10-14 16:29:42 +0200 | [diff] [blame] | 133 | * \brief Perform a safe conditional copy of an MPI which doesn't reveal |
| 134 | * whether assignment was done or not. |
Gabor Mezei | e1d31c4 | 2022-09-12 16:25:24 +0200 | [diff] [blame] | 135 | * |
Gabor Mezei | 4086de6 | 2022-10-14 16:29:42 +0200 | [diff] [blame] | 136 | * \param[out] X The address of the destination MPI. |
| 137 | * This must be initialized. Must have enough limbs to |
| 138 | * store the full value of \p A. |
| 139 | * \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] | 140 | * \param limbs The number of limbs of \p A. |
Gabor Mezei | e1d31c4 | 2022-09-12 16:25:24 +0200 | [diff] [blame] | 141 | * \param assign The condition deciding whether to perform the |
| 142 | * assignment or not. Must be either 0 or 1: |
Gabor Mezei | 1c628d5 | 2022-09-27 12:13:51 +0200 | [diff] [blame] | 143 | * * \c 1: Perform the assignment `X = A`. |
Gabor Mezei | e1d31c4 | 2022-09-12 16:25:24 +0200 | [diff] [blame] | 144 | * * \c 0: Keep the original value of \p X. |
| 145 | * |
| 146 | * \note This function avoids leaking any information about whether |
| 147 | * the assignment was done or not. |
| 148 | * |
| 149 | * \warning If \p assign is neither 0 nor 1, the result of this function |
| 150 | * is indeterminate, and the resulting value in \p X might be |
Gabor Mezei | 4086de6 | 2022-10-14 16:29:42 +0200 | [diff] [blame] | 151 | * neither its original value nor the value in \p A. |
Gabor Mezei | e1d31c4 | 2022-09-12 16:25:24 +0200 | [diff] [blame] | 152 | */ |
Gabor Mezei | 9f6615f | 2022-09-15 19:12:06 +0200 | [diff] [blame] | 153 | void mbedtls_mpi_core_cond_assign( mbedtls_mpi_uint *X, |
Gabor Mezei | 1c628d5 | 2022-09-27 12:13:51 +0200 | [diff] [blame] | 154 | const mbedtls_mpi_uint *A, |
Gabor Mezei | 3eff425 | 2022-09-26 17:26:42 +0200 | [diff] [blame] | 155 | size_t limbs, |
Gabor Mezei | 9f6615f | 2022-09-15 19:12:06 +0200 | [diff] [blame] | 156 | unsigned char assign ); |
Gabor Mezei | e1d31c4 | 2022-09-12 16:25:24 +0200 | [diff] [blame] | 157 | |
| 158 | /** |
Gabor Mezei | 4086de6 | 2022-10-14 16:29:42 +0200 | [diff] [blame] | 159 | * \brief Perform a safe conditional swap of two MPIs which doesn't reveal |
| 160 | * whether the swap was done or not. |
Gabor Mezei | e1d31c4 | 2022-09-12 16:25:24 +0200 | [diff] [blame] | 161 | * |
Gabor Mezei | 86dfe38 | 2022-09-30 14:03:04 +0200 | [diff] [blame] | 162 | * \param[in,out] X The address of the first MPI. |
Gabor Mezei | e1d31c4 | 2022-09-12 16:25:24 +0200 | [diff] [blame] | 163 | * This must be initialized. |
Gabor Mezei | 86dfe38 | 2022-09-30 14:03:04 +0200 | [diff] [blame] | 164 | * \param[in,out] Y The address of the second MPI. |
Gabor Mezei | e1d31c4 | 2022-09-12 16:25:24 +0200 | [diff] [blame] | 165 | * This must be initialized. |
Gabor Mezei | e5b8585 | 2022-09-30 13:54:02 +0200 | [diff] [blame] | 166 | * \param limbs The number of limbs of \p X and \p Y. |
Gabor Mezei | e1d31c4 | 2022-09-12 16:25:24 +0200 | [diff] [blame] | 167 | * \param swap The condition deciding whether to perform |
| 168 | * the swap or not. Must be either 0 or 1: |
Gabor Mezei | e5b8585 | 2022-09-30 13:54:02 +0200 | [diff] [blame] | 169 | * * \c 1: Swap the values of \p X and \p Y. |
| 170 | * * \c 0: Keep the original values of \p X and \p Y. |
Gabor Mezei | e1d31c4 | 2022-09-12 16:25:24 +0200 | [diff] [blame] | 171 | * |
| 172 | * \note This function avoids leaking any information about whether |
| 173 | * the swap was done or not. |
| 174 | * |
| 175 | * \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] | 176 | * 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] | 177 | * values different to either of the original ones. |
Gabor Mezei | e1d31c4 | 2022-09-12 16:25:24 +0200 | [diff] [blame] | 178 | */ |
Gabor Mezei | e5b8585 | 2022-09-30 13:54:02 +0200 | [diff] [blame] | 179 | void mbedtls_mpi_core_cond_swap( mbedtls_mpi_uint *X, |
| 180 | mbedtls_mpi_uint *Y, |
Gabor Mezei | cfc0eb8 | 2022-09-15 20:15:34 +0200 | [diff] [blame] | 181 | size_t limbs, |
Gabor Mezei | 9f6615f | 2022-09-15 19:12:06 +0200 | [diff] [blame] | 182 | unsigned char swap ); |
Gabor Mezei | e1d31c4 | 2022-09-12 16:25:24 +0200 | [diff] [blame] | 183 | |
Janos Follath | af3f39c | 2022-08-22 09:06:32 +0100 | [diff] [blame] | 184 | /** Import X from unsigned binary data, little-endian. |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 185 | * |
Janos Follath | 6318468 | 2022-08-11 17:42:59 +0100 | [diff] [blame] | 186 | * The MPI needs to have enough limbs to store the full value (including any |
| 187 | * most significant zero bytes in the input). |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 188 | * |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 189 | * \param[out] X The address of the MPI. |
| 190 | * \param X_limbs The number of limbs of \p X. |
| 191 | * \param[in] input The input buffer to import from. |
| 192 | * \param input_length The length bytes of \p input. |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 193 | * |
| 194 | * \return \c 0 if successful. |
| 195 | * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p X isn't |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 196 | * large enough to hold the value in \p input. |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 197 | */ |
Gabor Mezei | b903070 | 2022-07-18 23:09:45 +0200 | [diff] [blame] | 198 | int mbedtls_mpi_core_read_le( mbedtls_mpi_uint *X, |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 199 | size_t X_limbs, |
| 200 | const unsigned char *input, |
| 201 | size_t input_length ); |
Gabor Mezei | b903070 | 2022-07-18 23:09:45 +0200 | [diff] [blame] | 202 | |
Janos Follath | af3f39c | 2022-08-22 09:06:32 +0100 | [diff] [blame] | 203 | /** Import X from unsigned binary data, big-endian. |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 204 | * |
Janos Follath | 6318468 | 2022-08-11 17:42:59 +0100 | [diff] [blame] | 205 | * The MPI needs to have enough limbs to store the full value (including any |
| 206 | * most significant zero bytes in the input). |
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 | * \param[out] X The address of the MPI. |
| 209 | * May only be #NULL if \X_limbs is 0 and \p input_length |
| 210 | * is 0. |
| 211 | * \param X_limbs The number of limbs of \p X. |
| 212 | * \param[in] input The input buffer to import from. |
| 213 | * May only be #NULL if \p input_length is 0. |
| 214 | * \param input_length The length in bytes of \p input. |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 215 | * |
| 216 | * \return \c 0 if successful. |
| 217 | * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p X isn't |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 218 | * large enough to hold the value in \p input. |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 219 | */ |
Gabor Mezei | b903070 | 2022-07-18 23:09:45 +0200 | [diff] [blame] | 220 | int mbedtls_mpi_core_read_be( mbedtls_mpi_uint *X, |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 221 | size_t X_limbs, |
| 222 | const unsigned char *input, |
| 223 | size_t input_length ); |
Gabor Mezei | b903070 | 2022-07-18 23:09:45 +0200 | [diff] [blame] | 224 | |
Janos Follath | af3f39c | 2022-08-22 09:06:32 +0100 | [diff] [blame] | 225 | /** Export A into unsigned binary data, little-endian. |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 226 | * |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 227 | * \note If \p output is shorter than \p A the export is still successful if the |
| 228 | * value held in \p A fits in the buffer (that is, if enough of the most |
| 229 | * significant bytes of \p A are 0). |
Janos Follath | 6318468 | 2022-08-11 17:42:59 +0100 | [diff] [blame] | 230 | * |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 231 | * \param[in] A The address of the MPI. |
| 232 | * \param A_limbs The number of limbs of \p A. |
| 233 | * \param[out] output The output buffer to export to. |
| 234 | * \param output_length The length in bytes of \p output. |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 235 | * |
| 236 | * \return \c 0 if successful. |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 237 | * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p output isn't |
| 238 | * large enough to hold the value of \p A. |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 239 | */ |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 240 | int mbedtls_mpi_core_write_le( const mbedtls_mpi_uint *A, |
| 241 | size_t A_limbs, |
| 242 | unsigned char *output, |
| 243 | size_t output_length ); |
Gabor Mezei | b903070 | 2022-07-18 23:09:45 +0200 | [diff] [blame] | 244 | |
Janos Follath | af3f39c | 2022-08-22 09:06:32 +0100 | [diff] [blame] | 245 | /** Export A into unsigned binary data, big-endian. |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 246 | * |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 247 | * \note If \p output is shorter than \p A the export is still successful if the |
| 248 | * value held in \p A fits in the buffer (that is, if enough of the most |
| 249 | * significant bytes of \p A are 0). |
Janos Follath | 6318468 | 2022-08-11 17:42:59 +0100 | [diff] [blame] | 250 | * |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 251 | * \param[in] A The address of the MPI. |
| 252 | * \param A_limbs The number of limbs of \p A. |
| 253 | * \param[out] output The output buffer to export to. |
| 254 | * \param output_length The length in bytes of \p output. |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 255 | * |
| 256 | * \return \c 0 if successful. |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 257 | * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p output isn't |
| 258 | * large enough to hold the value of \p A. |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 259 | */ |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 260 | int mbedtls_mpi_core_write_be( const mbedtls_mpi_uint *A, |
| 261 | size_t A_limbs, |
| 262 | unsigned char *output, |
| 263 | size_t output_length ); |
Gabor Mezei | b903070 | 2022-07-18 23:09:45 +0200 | [diff] [blame] | 264 | |
Gilles Peskine | abc6fbb | 2022-10-21 18:36:08 +0200 | [diff] [blame] | 265 | /** \brief Shift an MPI right in place by a number of bits. |
Gilles Peskine | 6641420 | 2022-09-21 15:36:16 +0200 | [diff] [blame] | 266 | * |
| 267 | * Shifting by more bits than there are bit positions |
| 268 | * in \p X is valid and results in setting \p X to 0. |
| 269 | * |
| 270 | * This function's execution time depends on the value |
| 271 | * of \p count (and of course \p limbs). |
| 272 | * |
| 273 | * \param[in,out] X The number to shift. |
| 274 | * \param limbs The number of limbs of \p X. This must be at least 1. |
| 275 | * \param count The number of bits to shift by. |
| 276 | */ |
| 277 | void mbedtls_mpi_core_shift_r( mbedtls_mpi_uint *X, size_t limbs, |
| 278 | size_t count ); |
| 279 | |
Tom Cosgrove | 90c426b | 2022-08-23 16:15:19 +0100 | [diff] [blame] | 280 | /** |
Tom Cosgrove | 5c0e810 | 2022-09-15 15:46:10 +0100 | [diff] [blame] | 281 | * \brief Conditional addition of two fixed-size large unsigned integers, |
Tom Cosgrove | d932de8 | 2022-08-25 16:43:43 +0100 | [diff] [blame] | 282 | * returning the carry. |
Hanno Becker | 71f4b0d | 2022-08-23 12:09:35 +0100 | [diff] [blame] | 283 | * |
| 284 | * Functionally equivalent to |
| 285 | * |
| 286 | * ``` |
| 287 | * if( cond ) |
Tom Cosgrove | 3bd7bc3 | 2022-09-15 15:55:07 +0100 | [diff] [blame] | 288 | * X += A; |
Hanno Becker | 71f4b0d | 2022-08-23 12:09:35 +0100 | [diff] [blame] | 289 | * return carry; |
| 290 | * ``` |
| 291 | * |
Tom Cosgrove | 4782823 | 2022-09-20 13:51:50 +0100 | [diff] [blame] | 292 | * This function operates modulo `2^(biL*limbs)`. |
| 293 | * |
Tom Cosgrove | 3bd7bc3 | 2022-09-15 15:55:07 +0100 | [diff] [blame] | 294 | * \param[in,out] X The pointer to the (little-endian) array |
Tom Cosgrove | 7259463 | 2022-08-24 11:51:58 +0100 | [diff] [blame] | 295 | * representing the bignum to accumulate onto. |
Tom Cosgrove | 3bd7bc3 | 2022-09-15 15:55:07 +0100 | [diff] [blame] | 296 | * \param[in] A The pointer to the (little-endian) array |
Tom Cosgrove | 7259463 | 2022-08-24 11:51:58 +0100 | [diff] [blame] | 297 | * representing the bignum to conditionally add |
Tom Cosgrove | 3bd7bc3 | 2022-09-15 15:55:07 +0100 | [diff] [blame] | 298 | * 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] | 299 | * overlap otherwise. |
Tom Cosgrove | 3bd7bc3 | 2022-09-15 15:55:07 +0100 | [diff] [blame] | 300 | * \param limbs Number of limbs of \p X and \p A. |
Tom Cosgrove | 7259463 | 2022-08-24 11:51:58 +0100 | [diff] [blame] | 301 | * \param cond Condition bit dictating whether addition should |
| 302 | * happen or not. This must be \c 0 or \c 1. |
Hanno Becker | 71f4b0d | 2022-08-23 12:09:35 +0100 | [diff] [blame] | 303 | * |
Tom Cosgrove | ecbb124 | 2022-08-25 10:13:44 +0100 | [diff] [blame] | 304 | * \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] | 305 | * is unspecified, and the resulting value in \p X might be |
| 306 | * neither its original value nor \p X + \p A. |
Tom Cosgrove | 7259463 | 2022-08-24 11:51:58 +0100 | [diff] [blame] | 307 | * |
Tom Cosgrove | 3bd7bc3 | 2022-09-15 15:55:07 +0100 | [diff] [blame] | 308 | * \return 1 if `X + cond * A >= 2^(biL*limbs)`, 0 otherwise. |
Hanno Becker | 71f4b0d | 2022-08-23 12:09:35 +0100 | [diff] [blame] | 309 | */ |
Tom Cosgrove | 3bd7bc3 | 2022-09-15 15:55:07 +0100 | [diff] [blame] | 310 | mbedtls_mpi_uint mbedtls_mpi_core_add_if( mbedtls_mpi_uint *X, |
| 311 | const mbedtls_mpi_uint *A, |
Tom Cosgrove | 7259463 | 2022-08-24 11:51:58 +0100 | [diff] [blame] | 312 | size_t limbs, |
Tom Cosgrove | 90c426b | 2022-08-23 16:15:19 +0100 | [diff] [blame] | 313 | unsigned cond ); |
Hanno Becker | 71f4b0d | 2022-08-23 12:09:35 +0100 | [diff] [blame] | 314 | |
Tom Cosgrove | b496486 | 2022-08-30 11:57:22 +0100 | [diff] [blame] | 315 | /** |
Tom Cosgrove | 5c0e810 | 2022-09-15 15:46:10 +0100 | [diff] [blame] | 316 | * \brief Subtract two fixed-size large unsigned integers, returning the borrow. |
Tom Cosgrove | b496486 | 2022-08-30 11:57:22 +0100 | [diff] [blame] | 317 | * |
Tom Cosgrove | 5dd97e6 | 2022-08-30 14:31:49 +0100 | [diff] [blame] | 318 | * 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] | 319 | * This function operates modulo `2^(biL*limbs)` and returns the carry |
Tom Cosgrove | b496486 | 2022-08-30 11:57:22 +0100 | [diff] [blame] | 320 | * (1 if there was a wraparound, i.e. if `A < B`, and 0 otherwise). |
| 321 | * |
Tom Cosgrove | 5dd97e6 | 2022-08-30 14:31:49 +0100 | [diff] [blame] | 322 | * \p X may be aliased to \p A or \p B, or even both, but may not overlap |
| 323 | * either otherwise. |
Tom Cosgrove | b496486 | 2022-08-30 11:57:22 +0100 | [diff] [blame] | 324 | * |
| 325 | * \param[out] X The result of the subtraction. |
| 326 | * \param[in] A Little-endian presentation of left operand. |
| 327 | * \param[in] B Little-endian presentation of right operand. |
| 328 | * \param limbs Number of limbs of \p X, \p A and \p B. |
| 329 | * |
| 330 | * \return 1 if `A < B`. |
| 331 | * 0 if `A >= B`. |
| 332 | */ |
| 333 | mbedtls_mpi_uint mbedtls_mpi_core_sub( mbedtls_mpi_uint *X, |
| 334 | const mbedtls_mpi_uint *A, |
| 335 | const mbedtls_mpi_uint *B, |
| 336 | size_t limbs ); |
| 337 | |
| 338 | /** |
Tom Cosgrove | 3bd7bc3 | 2022-09-15 15:55:07 +0100 | [diff] [blame] | 339 | * \brief Perform a fixed-size multiply accumulate operation: X += b * A |
Tom Cosgrove | b496486 | 2022-08-30 11:57:22 +0100 | [diff] [blame] | 340 | * |
Tom Cosgrove | b0b77e1 | 2022-09-20 13:33:40 +0100 | [diff] [blame] | 341 | * \p X may be aliased to \p A (when \p X_limbs == \p A_limbs), but may not |
| 342 | * otherwise overlap. |
| 343 | * |
Tom Cosgrove | 4782823 | 2022-09-20 13:51:50 +0100 | [diff] [blame] | 344 | * This function operates modulo `2^(biL*X_limbs)`. |
| 345 | * |
Tom Cosgrove | 3bd7bc3 | 2022-09-15 15:55:07 +0100 | [diff] [blame] | 346 | * \param[in,out] X The pointer to the (little-endian) array |
Tom Cosgrove | b496486 | 2022-08-30 11:57:22 +0100 | [diff] [blame] | 347 | * representing the bignum to accumulate onto. |
Tom Cosgrove | 3bd7bc3 | 2022-09-15 15:55:07 +0100 | [diff] [blame] | 348 | * \param X_limbs The number of limbs of \p X. This must be |
| 349 | * at least \p A_limbs. |
| 350 | * \param[in] A The pointer to the (little-endian) array |
Tom Cosgrove | b496486 | 2022-08-30 11:57:22 +0100 | [diff] [blame] | 351 | * representing the bignum to multiply with. |
Tom Cosgrove | 3bd7bc3 | 2022-09-15 15:55:07 +0100 | [diff] [blame] | 352 | * This may be aliased to \p X but may not overlap |
Tom Cosgrove | ed43c6c | 2022-08-31 11:35:00 +0100 | [diff] [blame] | 353 | * otherwise. |
Tom Cosgrove | 3bd7bc3 | 2022-09-15 15:55:07 +0100 | [diff] [blame] | 354 | * \param A_limbs The number of limbs of \p A. |
| 355 | * \param b X scalar to multiply with. |
Tom Cosgrove | b496486 | 2022-08-30 11:57:22 +0100 | [diff] [blame] | 356 | * |
| 357 | * \return The carry at the end of the operation. |
| 358 | */ |
Tom Cosgrove | 3bd7bc3 | 2022-09-15 15:55:07 +0100 | [diff] [blame] | 359 | mbedtls_mpi_uint mbedtls_mpi_core_mla( mbedtls_mpi_uint *X, size_t X_limbs, |
| 360 | const mbedtls_mpi_uint *A, size_t A_limbs, |
| 361 | mbedtls_mpi_uint b ); |
Tom Cosgrove | b496486 | 2022-08-30 11:57:22 +0100 | [diff] [blame] | 362 | |
| 363 | /** |
| 364 | * \brief Calculate initialisation value for fast Montgomery modular |
| 365 | * multiplication |
| 366 | * |
| 367 | * \param[in] N Little-endian presentation of the modulus. This must have |
| 368 | * at least one limb. |
| 369 | * |
| 370 | * \return The initialisation value for fast Montgomery modular multiplication |
| 371 | */ |
Tom Cosgrove | b7438d1 | 2022-09-15 15:05:59 +0100 | [diff] [blame] | 372 | 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] | 373 | |
| 374 | /** |
Tom Cosgrove | 4386ead | 2022-09-29 14:40:21 +0100 | [diff] [blame] | 375 | * \brief Montgomery multiplication: X = A * B * R^-1 mod N (HAC 14.36) |
| 376 | * |
| 377 | * \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] | 378 | * |
Tom Cosgrove | ea45c1d | 2022-09-20 13:17:51 +0100 | [diff] [blame] | 379 | * \p X may be aliased to \p A or \p N, or even \p B (if \p AN_limbs == |
| 380 | * \p B_limbs) but may not overlap any parameters otherwise. |
| 381 | * |
Tom Cosgrove | 4386ead | 2022-09-29 14:40:21 +0100 | [diff] [blame] | 382 | * \p A and \p B may alias each other, if \p AN_limbs == \p B_limbs. They may |
| 383 | * 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] | 384 | * |
Tom Cosgrove | b496486 | 2022-08-30 11:57:22 +0100 | [diff] [blame] | 385 | * \param[out] X The destination MPI, as a little-endian array of |
| 386 | * length \p AN_limbs. |
| 387 | * On successful completion, X contains the result of |
Tom Cosgrove | 630110a | 2022-08-31 17:09:29 +0100 | [diff] [blame] | 388 | * the multiplication `A * B * R^-1` mod N where |
| 389 | * `R = 2^(biL*AN_limbs)`. |
Tom Cosgrove | b496486 | 2022-08-30 11:57:22 +0100 | [diff] [blame] | 390 | * \param[in] A Little-endian presentation of first operand. |
Tom Cosgrove | 5dd97e6 | 2022-08-30 14:31:49 +0100 | [diff] [blame] | 391 | * Must have the same number of limbs as \p N. |
Tom Cosgrove | b496486 | 2022-08-30 11:57:22 +0100 | [diff] [blame] | 392 | * \param[in] B Little-endian presentation of second operand. |
| 393 | * \param[in] B_limbs The number of limbs in \p B. |
Tom Cosgrove | 5dd97e6 | 2022-08-30 14:31:49 +0100 | [diff] [blame] | 394 | * Must be <= \p AN_limbs. |
Tom Cosgrove | b496486 | 2022-08-30 11:57:22 +0100 | [diff] [blame] | 395 | * \param[in] N Little-endian presentation of the modulus. |
Tom Cosgrove | 5dd97e6 | 2022-08-30 14:31:49 +0100 | [diff] [blame] | 396 | * This must be odd, and have exactly the same number |
| 397 | * of limbs as \p A. |
Tom Cosgrove | 6da3a3b | 2022-09-29 17:20:18 +0100 | [diff] [blame] | 398 | * It may alias \p X, but must not alias or otherwise |
| 399 | * overlap any of the other parameters. |
Tom Cosgrove | 5dd97e6 | 2022-08-30 14:31:49 +0100 | [diff] [blame] | 400 | * \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] | 401 | * \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] | 402 | * This can be calculated by `mbedtls_mpi_core_montmul_init()`. |
Tom Cosgrove | b496486 | 2022-08-30 11:57:22 +0100 | [diff] [blame] | 403 | * \param[in,out] T Temporary storage of size at least 2*AN_limbs+1 limbs. |
| 404 | * Its initial content is unused and |
| 405 | * its final content is indeterminate. |
Tom Cosgrove | 4386ead | 2022-09-29 14:40:21 +0100 | [diff] [blame] | 406 | * It must not alias or otherwise overlap any of the |
| 407 | * other parameters. |
Tom Cosgrove | b496486 | 2022-08-30 11:57:22 +0100 | [diff] [blame] | 408 | */ |
| 409 | void mbedtls_mpi_core_montmul( mbedtls_mpi_uint *X, |
| 410 | const mbedtls_mpi_uint *A, |
| 411 | const mbedtls_mpi_uint *B, size_t B_limbs, |
| 412 | const mbedtls_mpi_uint *N, size_t AN_limbs, |
| 413 | mbedtls_mpi_uint mm, mbedtls_mpi_uint *T ); |
| 414 | |
Hanno Becker | ec440f2 | 2022-08-11 17:29:32 +0100 | [diff] [blame] | 415 | /** |
Minos Galanakis | b85506e | 2022-10-20 09:51:53 +0100 | [diff] [blame^] | 416 | * \brief Calculate the square of the Montgomery constant. (Needed |
| 417 | * for conversion and operations in Montgomery form.) |
Hanno Becker | ec440f2 | 2022-08-11 17:29:32 +0100 | [diff] [blame] | 418 | * |
| 419 | * \param[out] X A pointer to the result of the calculation of |
Minos Galanakis | b85506e | 2022-10-20 09:51:53 +0100 | [diff] [blame^] | 420 | * the square of the Montgomery constant: |
| 421 | * 2^{2*n*biL} mod N. |
Hanno Becker | ec440f2 | 2022-08-11 17:29:32 +0100 | [diff] [blame] | 422 | * \param[in] N Little-endian presentation of the modulus, which must be odd. |
| 423 | * |
| 424 | * \return 0 if successful. |
| 425 | * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if there is not enough space |
| 426 | * to store the value of Montgomery constant squared. |
| 427 | * \return #MBEDTLS_ERR_MPI_DIVISION_BY_ZERO if \p N modulus is zero. |
| 428 | * \return #MBEDTLS_ERR_MPI_NEGATIVE_VALUE if \p N modulus is negative. |
| 429 | * \return #MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED \p N, \p X are NULL |
| 430 | * or other operations fail. |
| 431 | */ |
Minos Galanakis | 4f43f61 | 2022-10-20 09:46:59 +0100 | [diff] [blame] | 432 | int mbedtls_mpi_core_get_mont_R2_unsafe( mbedtls_mpi *X, |
| 433 | mbedtls_mpi const *N ); |
Hanno Becker | ec440f2 | 2022-08-11 17:29:32 +0100 | [diff] [blame] | 434 | |
Gabor Mezei | b903070 | 2022-07-18 23:09:45 +0200 | [diff] [blame] | 435 | #endif /* MBEDTLS_BIGNUM_CORE_H */ |