Gabor Mezei | b903070 | 2022-07-18 23:09:45 +0200 | [diff] [blame] | 1 | /** |
Janos Follath | 6318468 | 2022-08-11 17:42:59 +0100 | [diff] [blame] | 2 | * Core bignum functions |
| 3 | * |
Janos Follath | af3f39c | 2022-08-22 09:06:32 +0100 | [diff] [blame] | 4 | * This interface should only be used by the legacy bignum module (bignum.h) |
Janos Follath | 6318468 | 2022-08-11 17:42:59 +0100 | [diff] [blame] | 5 | * and the modular bignum modules (bignum_mod.c, bignum_mod_raw.c). All other |
Janos Follath | af3f39c | 2022-08-22 09:06:32 +0100 | [diff] [blame] | 6 | * modules should use the high-level modular bignum interface (bignum_mod.h) |
Janos Follath | 6318468 | 2022-08-11 17:42:59 +0100 | [diff] [blame] | 7 | * or the legacy bignum interface (bignum.h). |
Gabor Mezei | b903070 | 2022-07-18 23:09:45 +0200 | [diff] [blame] | 8 | * |
| 9 | * Copyright The Mbed TLS Contributors |
| 10 | * SPDX-License-Identifier: Apache-2.0 |
| 11 | * |
| 12 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 13 | * not use this file except in compliance with the License. |
| 14 | * You may obtain a copy of the License at |
| 15 | * |
| 16 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 17 | * |
| 18 | * Unless required by applicable law or agreed to in writing, software |
| 19 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 20 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 21 | * See the License for the specific language governing permissions and |
| 22 | * limitations under the License. |
| 23 | */ |
| 24 | |
| 25 | #ifndef MBEDTLS_BIGNUM_CORE_H |
| 26 | #define MBEDTLS_BIGNUM_CORE_H |
| 27 | |
| 28 | #include "common.h" |
| 29 | |
| 30 | #if defined(MBEDTLS_BIGNUM_C) |
| 31 | #include "mbedtls/bignum.h" |
| 32 | #endif |
| 33 | |
Tom Cosgrove | 5eefc3d | 2022-08-31 17:16:50 +0100 | [diff] [blame] | 34 | #define ciL ( sizeof(mbedtls_mpi_uint) ) /* chars in limb */ |
| 35 | #define biL ( ciL << 3 ) /* bits in limb */ |
| 36 | #define biH ( ciL << 2 ) /* half limb size */ |
| 37 | |
| 38 | /* |
| 39 | * Convert between bits/chars and number of limbs |
| 40 | * Divide first in order to avoid potential overflows |
| 41 | */ |
| 42 | #define BITS_TO_LIMBS(i) ( (i) / biL + ( (i) % biL != 0 ) ) |
| 43 | #define CHARS_TO_LIMBS(i) ( (i) / ciL + ( (i) % ciL != 0 ) ) |
| 44 | /* Get a specific byte, without range checks. */ |
| 45 | #define GET_BYTE( X, i ) \ |
| 46 | ( ( (X)[(i) / ciL] >> ( ( (i) % ciL ) * 8 ) ) & 0xff ) |
| 47 | |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 48 | /** Count leading zero bits in a given integer. |
| 49 | * |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 50 | * \param a Integer to count leading zero bits. |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 51 | * |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 52 | * \return The number of leading zero bits in \p a. |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 53 | */ |
Janos Follath | 2e328c8 | 2022-08-22 11:19:10 +0100 | [diff] [blame] | 54 | size_t mbedtls_mpi_core_clz( mbedtls_mpi_uint a ); |
Janos Follath | 4670f88 | 2022-07-21 18:25:42 +0100 | [diff] [blame] | 55 | |
Janos Follath | a95f204 | 2022-08-19 12:09:17 +0100 | [diff] [blame] | 56 | /** Return the minimum number of bits required to represent the value held |
Janos Follath | 6318468 | 2022-08-11 17:42:59 +0100 | [diff] [blame] | 57 | * in the MPI. |
| 58 | * |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 59 | * \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] | 60 | * |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 61 | * \param[in] A The address of the MPI. |
Janos Follath | c459641 | 2022-08-22 10:01:27 +0100 | [diff] [blame] | 62 | * \param A_limbs The number of limbs of \p A. |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 63 | * |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 64 | * \return The number of bits in \p A. |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 65 | */ |
Janos Follath | af3f39c | 2022-08-22 09:06:32 +0100 | [diff] [blame] | 66 | 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] | 67 | |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 68 | /** Convert a big-endian byte array aligned to the size of mbedtls_mpi_uint |
| 69 | * into the storage form used by mbedtls_mpi. |
| 70 | * |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 71 | * \param[in,out] A The address of the MPI. |
Janos Follath | c459641 | 2022-08-22 10:01:27 +0100 | [diff] [blame] | 72 | * \param A_limbs The number of limbs of \p A. |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 73 | */ |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 74 | void mbedtls_mpi_core_bigendian_to_host( mbedtls_mpi_uint *A, |
Janos Follath | c459641 | 2022-08-22 10:01:27 +0100 | [diff] [blame] | 75 | size_t A_limbs ); |
Janos Follath | 4670f88 | 2022-07-21 18:25:42 +0100 | [diff] [blame] | 76 | |
Gabor Mezei | e1d31c4 | 2022-09-12 16:25:24 +0200 | [diff] [blame] | 77 | /** |
| 78 | * \brief Perform a safe conditional copy of MPI which doesn't reveal whether |
| 79 | * the condition was true or not. |
| 80 | * |
Gabor Mezei | 86dfe38 | 2022-09-30 14:03:04 +0200 | [diff] [blame] | 81 | * \param[out] X The address of the first MPI. This must be initialized. |
Gabor Mezei | dba2677 | 2022-10-03 17:01:02 +0200 | [diff] [blame] | 82 | * Must have enough limbs to store the full value of \p A. |
Gabor Mezei | 86dfe38 | 2022-09-30 14:03:04 +0200 | [diff] [blame] | 83 | * \param[in] A The address of the second MPI. This must be initialized. |
Gabor Mezei | 1c628d5 | 2022-09-27 12:13:51 +0200 | [diff] [blame] | 84 | * \param limbs The number of limbs of \p A. |
Gabor Mezei | e1d31c4 | 2022-09-12 16:25:24 +0200 | [diff] [blame] | 85 | * \param assign The condition deciding whether to perform the |
| 86 | * assignment or not. Must be either 0 or 1: |
Gabor Mezei | 1c628d5 | 2022-09-27 12:13:51 +0200 | [diff] [blame] | 87 | * * \c 1: Perform the assignment `X = A`. |
Gabor Mezei | e1d31c4 | 2022-09-12 16:25:24 +0200 | [diff] [blame] | 88 | * * \c 0: Keep the original value of \p X. |
| 89 | * |
| 90 | * \note This function avoids leaking any information about whether |
| 91 | * the assignment was done or not. |
| 92 | * |
| 93 | * \warning If \p assign is neither 0 nor 1, the result of this function |
| 94 | * is indeterminate, and the resulting value in \p X might be |
| 95 | * neither its original value nor the value in \p Y. |
Gabor Mezei | e1d31c4 | 2022-09-12 16:25:24 +0200 | [diff] [blame] | 96 | */ |
Gabor Mezei | 9f6615f | 2022-09-15 19:12:06 +0200 | [diff] [blame] | 97 | void mbedtls_mpi_core_cond_assign( mbedtls_mpi_uint *X, |
Gabor Mezei | 1c628d5 | 2022-09-27 12:13:51 +0200 | [diff] [blame] | 98 | const mbedtls_mpi_uint *A, |
Gabor Mezei | 3eff425 | 2022-09-26 17:26:42 +0200 | [diff] [blame] | 99 | size_t limbs, |
Gabor Mezei | 9f6615f | 2022-09-15 19:12:06 +0200 | [diff] [blame] | 100 | unsigned char assign ); |
Gabor Mezei | e1d31c4 | 2022-09-12 16:25:24 +0200 | [diff] [blame] | 101 | |
| 102 | /** |
Gabor Mezei | 2b5bf4c | 2022-09-26 17:09:58 +0200 | [diff] [blame] | 103 | * \brief Perform a safe conditional swap of MPI which doesn't reveal whether |
Gabor Mezei | e1d31c4 | 2022-09-12 16:25:24 +0200 | [diff] [blame] | 104 | * the condition was true or not. |
| 105 | * |
Gabor Mezei | 86dfe38 | 2022-09-30 14:03:04 +0200 | [diff] [blame] | 106 | * \param[in,out] X The address of the first MPI. |
Gabor Mezei | e1d31c4 | 2022-09-12 16:25:24 +0200 | [diff] [blame] | 107 | * This must be initialized. |
Gabor Mezei | 86dfe38 | 2022-09-30 14:03:04 +0200 | [diff] [blame] | 108 | * \param[in,out] Y The address of the second MPI. |
Gabor Mezei | e1d31c4 | 2022-09-12 16:25:24 +0200 | [diff] [blame] | 109 | * This must be initialized. |
Gabor Mezei | e5b8585 | 2022-09-30 13:54:02 +0200 | [diff] [blame] | 110 | * \param limbs The number of limbs of \p X and \p Y. |
Gabor Mezei | e1d31c4 | 2022-09-12 16:25:24 +0200 | [diff] [blame] | 111 | * \param swap The condition deciding whether to perform |
| 112 | * the swap or not. Must be either 0 or 1: |
Gabor Mezei | e5b8585 | 2022-09-30 13:54:02 +0200 | [diff] [blame] | 113 | * * \c 1: Swap the values of \p X and \p Y. |
| 114 | * * \c 0: Keep the original values of \p X and \p Y. |
Gabor Mezei | e1d31c4 | 2022-09-12 16:25:24 +0200 | [diff] [blame] | 115 | * |
| 116 | * \note This function avoids leaking any information about whether |
| 117 | * the swap was done or not. |
| 118 | * |
| 119 | * \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] | 120 | * 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] | 121 | * values different to either of the original ones. |
Gabor Mezei | e1d31c4 | 2022-09-12 16:25:24 +0200 | [diff] [blame] | 122 | */ |
Gabor Mezei | e5b8585 | 2022-09-30 13:54:02 +0200 | [diff] [blame] | 123 | void mbedtls_mpi_core_cond_swap( mbedtls_mpi_uint *X, |
| 124 | mbedtls_mpi_uint *Y, |
Gabor Mezei | cfc0eb8 | 2022-09-15 20:15:34 +0200 | [diff] [blame] | 125 | size_t limbs, |
Gabor Mezei | 9f6615f | 2022-09-15 19:12:06 +0200 | [diff] [blame] | 126 | unsigned char swap ); |
Gabor Mezei | e1d31c4 | 2022-09-12 16:25:24 +0200 | [diff] [blame] | 127 | |
Janos Follath | af3f39c | 2022-08-22 09:06:32 +0100 | [diff] [blame] | 128 | /** Import X from unsigned binary data, little-endian. |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 129 | * |
Janos Follath | 6318468 | 2022-08-11 17:42:59 +0100 | [diff] [blame] | 130 | * The MPI needs to have enough limbs to store the full value (including any |
| 131 | * most significant zero bytes in the input). |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 132 | * |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 133 | * \param[out] X The address of the MPI. |
| 134 | * \param X_limbs The number of limbs of \p X. |
| 135 | * \param[in] input The input buffer to import from. |
| 136 | * \param input_length The length bytes of \p input. |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 137 | * |
| 138 | * \return \c 0 if successful. |
| 139 | * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p X isn't |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 140 | * large enough to hold the value in \p input. |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 141 | */ |
Gabor Mezei | b903070 | 2022-07-18 23:09:45 +0200 | [diff] [blame] | 142 | int mbedtls_mpi_core_read_le( mbedtls_mpi_uint *X, |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 143 | size_t X_limbs, |
| 144 | const unsigned char *input, |
| 145 | size_t input_length ); |
Gabor Mezei | b903070 | 2022-07-18 23:09:45 +0200 | [diff] [blame] | 146 | |
Janos Follath | af3f39c | 2022-08-22 09:06:32 +0100 | [diff] [blame] | 147 | /** Import X from unsigned binary data, big-endian. |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 148 | * |
Janos Follath | 6318468 | 2022-08-11 17:42:59 +0100 | [diff] [blame] | 149 | * The MPI needs to have enough limbs to store the full value (including any |
| 150 | * most significant zero bytes in the input). |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 151 | * |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 152 | * \param[out] X The address of the MPI. |
| 153 | * May only be #NULL if \X_limbs is 0 and \p input_length |
| 154 | * is 0. |
| 155 | * \param X_limbs The number of limbs of \p X. |
| 156 | * \param[in] input The input buffer to import from. |
| 157 | * May only be #NULL if \p input_length is 0. |
| 158 | * \param input_length The length in bytes of \p input. |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 159 | * |
| 160 | * \return \c 0 if successful. |
| 161 | * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p X isn't |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 162 | * large enough to hold the value in \p input. |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 163 | */ |
Gabor Mezei | b903070 | 2022-07-18 23:09:45 +0200 | [diff] [blame] | 164 | int mbedtls_mpi_core_read_be( mbedtls_mpi_uint *X, |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 165 | size_t X_limbs, |
| 166 | const unsigned char *input, |
| 167 | size_t input_length ); |
Gabor Mezei | b903070 | 2022-07-18 23:09:45 +0200 | [diff] [blame] | 168 | |
Janos Follath | af3f39c | 2022-08-22 09:06:32 +0100 | [diff] [blame] | 169 | /** Export A into unsigned binary data, little-endian. |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 170 | * |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 171 | * \note If \p output is shorter than \p A the export is still successful if the |
| 172 | * value held in \p A fits in the buffer (that is, if enough of the most |
| 173 | * significant bytes of \p A are 0). |
Janos Follath | 6318468 | 2022-08-11 17:42:59 +0100 | [diff] [blame] | 174 | * |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 175 | * \param[in] A The address of the MPI. |
| 176 | * \param A_limbs The number of limbs of \p A. |
| 177 | * \param[out] output The output buffer to export to. |
| 178 | * \param output_length The length in bytes of \p output. |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 179 | * |
| 180 | * \return \c 0 if successful. |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 181 | * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p output isn't |
| 182 | * large enough to hold the value of \p A. |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 183 | */ |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 184 | int mbedtls_mpi_core_write_le( const mbedtls_mpi_uint *A, |
| 185 | size_t A_limbs, |
| 186 | unsigned char *output, |
| 187 | size_t output_length ); |
Gabor Mezei | b903070 | 2022-07-18 23:09:45 +0200 | [diff] [blame] | 188 | |
Janos Follath | af3f39c | 2022-08-22 09:06:32 +0100 | [diff] [blame] | 189 | /** Export A into unsigned binary data, big-endian. |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 190 | * |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 191 | * \note If \p output is shorter than \p A the export is still successful if the |
| 192 | * value held in \p A fits in the buffer (that is, if enough of the most |
| 193 | * significant bytes of \p A are 0). |
Janos Follath | 6318468 | 2022-08-11 17:42:59 +0100 | [diff] [blame] | 194 | * |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 195 | * \param[in] A The address of the MPI. |
| 196 | * \param A_limbs The number of limbs of \p A. |
| 197 | * \param[out] output The output buffer to export to. |
| 198 | * \param output_length The length in bytes of \p output. |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 199 | * |
| 200 | * \return \c 0 if successful. |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 201 | * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p output isn't |
| 202 | * large enough to hold the value of \p A. |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 203 | */ |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 204 | int mbedtls_mpi_core_write_be( const mbedtls_mpi_uint *A, |
| 205 | size_t A_limbs, |
| 206 | unsigned char *output, |
| 207 | size_t output_length ); |
Gabor Mezei | b903070 | 2022-07-18 23:09:45 +0200 | [diff] [blame] | 208 | |
Tom Cosgrove | 90c426b | 2022-08-23 16:15:19 +0100 | [diff] [blame] | 209 | /** |
Tom Cosgrove | 5c0e810 | 2022-09-15 15:46:10 +0100 | [diff] [blame] | 210 | * \brief Conditional addition of two fixed-size large unsigned integers, |
Tom Cosgrove | d932de8 | 2022-08-25 16:43:43 +0100 | [diff] [blame] | 211 | * returning the carry. |
Hanno Becker | 71f4b0d | 2022-08-23 12:09:35 +0100 | [diff] [blame] | 212 | * |
| 213 | * Functionally equivalent to |
| 214 | * |
| 215 | * ``` |
| 216 | * if( cond ) |
Tom Cosgrove | 3bd7bc3 | 2022-09-15 15:55:07 +0100 | [diff] [blame] | 217 | * X += A; |
Hanno Becker | 71f4b0d | 2022-08-23 12:09:35 +0100 | [diff] [blame] | 218 | * return carry; |
| 219 | * ``` |
| 220 | * |
Tom Cosgrove | 4782823 | 2022-09-20 13:51:50 +0100 | [diff] [blame] | 221 | * This function operates modulo `2^(biL*limbs)`. |
| 222 | * |
Tom Cosgrove | 3bd7bc3 | 2022-09-15 15:55:07 +0100 | [diff] [blame] | 223 | * \param[in,out] X The pointer to the (little-endian) array |
Tom Cosgrove | 7259463 | 2022-08-24 11:51:58 +0100 | [diff] [blame] | 224 | * representing the bignum to accumulate onto. |
Tom Cosgrove | 3bd7bc3 | 2022-09-15 15:55:07 +0100 | [diff] [blame] | 225 | * \param[in] A The pointer to the (little-endian) array |
Tom Cosgrove | 7259463 | 2022-08-24 11:51:58 +0100 | [diff] [blame] | 226 | * representing the bignum to conditionally add |
Tom Cosgrove | 3bd7bc3 | 2022-09-15 15:55:07 +0100 | [diff] [blame] | 227 | * 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] | 228 | * overlap otherwise. |
Tom Cosgrove | 3bd7bc3 | 2022-09-15 15:55:07 +0100 | [diff] [blame] | 229 | * \param limbs Number of limbs of \p X and \p A. |
Tom Cosgrove | 7259463 | 2022-08-24 11:51:58 +0100 | [diff] [blame] | 230 | * \param cond Condition bit dictating whether addition should |
| 231 | * happen or not. This must be \c 0 or \c 1. |
Hanno Becker | 71f4b0d | 2022-08-23 12:09:35 +0100 | [diff] [blame] | 232 | * |
Tom Cosgrove | ecbb124 | 2022-08-25 10:13:44 +0100 | [diff] [blame] | 233 | * \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] | 234 | * is unspecified, and the resulting value in \p X might be |
| 235 | * neither its original value nor \p X + \p A. |
Tom Cosgrove | 7259463 | 2022-08-24 11:51:58 +0100 | [diff] [blame] | 236 | * |
Tom Cosgrove | 3bd7bc3 | 2022-09-15 15:55:07 +0100 | [diff] [blame] | 237 | * \return 1 if `X + cond * A >= 2^(biL*limbs)`, 0 otherwise. |
Hanno Becker | 71f4b0d | 2022-08-23 12:09:35 +0100 | [diff] [blame] | 238 | */ |
Tom Cosgrove | 3bd7bc3 | 2022-09-15 15:55:07 +0100 | [diff] [blame] | 239 | mbedtls_mpi_uint mbedtls_mpi_core_add_if( mbedtls_mpi_uint *X, |
| 240 | const mbedtls_mpi_uint *A, |
Tom Cosgrove | 7259463 | 2022-08-24 11:51:58 +0100 | [diff] [blame] | 241 | size_t limbs, |
Tom Cosgrove | 90c426b | 2022-08-23 16:15:19 +0100 | [diff] [blame] | 242 | unsigned cond ); |
Hanno Becker | 71f4b0d | 2022-08-23 12:09:35 +0100 | [diff] [blame] | 243 | |
Tom Cosgrove | b496486 | 2022-08-30 11:57:22 +0100 | [diff] [blame] | 244 | /** |
Tom Cosgrove | 5c0e810 | 2022-09-15 15:46:10 +0100 | [diff] [blame] | 245 | * \brief Subtract two fixed-size large unsigned integers, returning the borrow. |
Tom Cosgrove | b496486 | 2022-08-30 11:57:22 +0100 | [diff] [blame] | 246 | * |
Tom Cosgrove | 5dd97e6 | 2022-08-30 14:31:49 +0100 | [diff] [blame] | 247 | * 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] | 248 | * This function operates modulo `2^(biL*limbs)` and returns the carry |
Tom Cosgrove | b496486 | 2022-08-30 11:57:22 +0100 | [diff] [blame] | 249 | * (1 if there was a wraparound, i.e. if `A < B`, and 0 otherwise). |
| 250 | * |
Tom Cosgrove | 5dd97e6 | 2022-08-30 14:31:49 +0100 | [diff] [blame] | 251 | * \p X may be aliased to \p A or \p B, or even both, but may not overlap |
| 252 | * either otherwise. |
Tom Cosgrove | b496486 | 2022-08-30 11:57:22 +0100 | [diff] [blame] | 253 | * |
| 254 | * \param[out] X The result of the subtraction. |
| 255 | * \param[in] A Little-endian presentation of left operand. |
| 256 | * \param[in] B Little-endian presentation of right operand. |
| 257 | * \param limbs Number of limbs of \p X, \p A and \p B. |
| 258 | * |
| 259 | * \return 1 if `A < B`. |
| 260 | * 0 if `A >= B`. |
| 261 | */ |
| 262 | mbedtls_mpi_uint mbedtls_mpi_core_sub( mbedtls_mpi_uint *X, |
| 263 | const mbedtls_mpi_uint *A, |
| 264 | const mbedtls_mpi_uint *B, |
| 265 | size_t limbs ); |
| 266 | |
| 267 | /** |
Tom Cosgrove | 3bd7bc3 | 2022-09-15 15:55:07 +0100 | [diff] [blame] | 268 | * \brief Perform a fixed-size multiply accumulate operation: X += b * A |
Tom Cosgrove | b496486 | 2022-08-30 11:57:22 +0100 | [diff] [blame] | 269 | * |
Tom Cosgrove | b0b77e1 | 2022-09-20 13:33:40 +0100 | [diff] [blame] | 270 | * \p X may be aliased to \p A (when \p X_limbs == \p A_limbs), but may not |
| 271 | * otherwise overlap. |
| 272 | * |
Tom Cosgrove | 4782823 | 2022-09-20 13:51:50 +0100 | [diff] [blame] | 273 | * This function operates modulo `2^(biL*X_limbs)`. |
| 274 | * |
Tom Cosgrove | 3bd7bc3 | 2022-09-15 15:55:07 +0100 | [diff] [blame] | 275 | * \param[in,out] X The pointer to the (little-endian) array |
Tom Cosgrove | b496486 | 2022-08-30 11:57:22 +0100 | [diff] [blame] | 276 | * representing the bignum to accumulate onto. |
Tom Cosgrove | 3bd7bc3 | 2022-09-15 15:55:07 +0100 | [diff] [blame] | 277 | * \param X_limbs The number of limbs of \p X. This must be |
| 278 | * at least \p A_limbs. |
| 279 | * \param[in] A The pointer to the (little-endian) array |
Tom Cosgrove | b496486 | 2022-08-30 11:57:22 +0100 | [diff] [blame] | 280 | * representing the bignum to multiply with. |
Tom Cosgrove | 3bd7bc3 | 2022-09-15 15:55:07 +0100 | [diff] [blame] | 281 | * This may be aliased to \p X but may not overlap |
Tom Cosgrove | ed43c6c | 2022-08-31 11:35:00 +0100 | [diff] [blame] | 282 | * otherwise. |
Tom Cosgrove | 3bd7bc3 | 2022-09-15 15:55:07 +0100 | [diff] [blame] | 283 | * \param A_limbs The number of limbs of \p A. |
| 284 | * \param b X scalar to multiply with. |
Tom Cosgrove | b496486 | 2022-08-30 11:57:22 +0100 | [diff] [blame] | 285 | * |
| 286 | * \return The carry at the end of the operation. |
| 287 | */ |
Tom Cosgrove | 3bd7bc3 | 2022-09-15 15:55:07 +0100 | [diff] [blame] | 288 | mbedtls_mpi_uint mbedtls_mpi_core_mla( mbedtls_mpi_uint *X, size_t X_limbs, |
| 289 | const mbedtls_mpi_uint *A, size_t A_limbs, |
| 290 | mbedtls_mpi_uint b ); |
Tom Cosgrove | b496486 | 2022-08-30 11:57:22 +0100 | [diff] [blame] | 291 | |
| 292 | /** |
| 293 | * \brief Calculate initialisation value for fast Montgomery modular |
| 294 | * multiplication |
| 295 | * |
| 296 | * \param[in] N Little-endian presentation of the modulus. This must have |
| 297 | * at least one limb. |
| 298 | * |
| 299 | * \return The initialisation value for fast Montgomery modular multiplication |
| 300 | */ |
Tom Cosgrove | b7438d1 | 2022-09-15 15:05:59 +0100 | [diff] [blame] | 301 | 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] | 302 | |
| 303 | /** |
Tom Cosgrove | 4386ead | 2022-09-29 14:40:21 +0100 | [diff] [blame] | 304 | * \brief Montgomery multiplication: X = A * B * R^-1 mod N (HAC 14.36) |
| 305 | * |
| 306 | * \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] | 307 | * |
Tom Cosgrove | ea45c1d | 2022-09-20 13:17:51 +0100 | [diff] [blame] | 308 | * \p X may be aliased to \p A or \p N, or even \p B (if \p AN_limbs == |
| 309 | * \p B_limbs) but may not overlap any parameters otherwise. |
| 310 | * |
Tom Cosgrove | 4386ead | 2022-09-29 14:40:21 +0100 | [diff] [blame] | 311 | * \p A and \p B may alias each other, if \p AN_limbs == \p B_limbs. They may |
| 312 | * 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] | 313 | * |
Tom Cosgrove | b496486 | 2022-08-30 11:57:22 +0100 | [diff] [blame] | 314 | * \param[out] X The destination MPI, as a little-endian array of |
| 315 | * length \p AN_limbs. |
| 316 | * On successful completion, X contains the result of |
Tom Cosgrove | 630110a | 2022-08-31 17:09:29 +0100 | [diff] [blame] | 317 | * the multiplication `A * B * R^-1` mod N where |
| 318 | * `R = 2^(biL*AN_limbs)`. |
Tom Cosgrove | b496486 | 2022-08-30 11:57:22 +0100 | [diff] [blame] | 319 | * \param[in] A Little-endian presentation of first operand. |
Tom Cosgrove | 5dd97e6 | 2022-08-30 14:31:49 +0100 | [diff] [blame] | 320 | * Must have the same number of limbs as \p N. |
Tom Cosgrove | b496486 | 2022-08-30 11:57:22 +0100 | [diff] [blame] | 321 | * \param[in] B Little-endian presentation of second operand. |
| 322 | * \param[in] B_limbs The number of limbs in \p B. |
Tom Cosgrove | 5dd97e6 | 2022-08-30 14:31:49 +0100 | [diff] [blame] | 323 | * Must be <= \p AN_limbs. |
Tom Cosgrove | b496486 | 2022-08-30 11:57:22 +0100 | [diff] [blame] | 324 | * \param[in] N Little-endian presentation of the modulus. |
Tom Cosgrove | 5dd97e6 | 2022-08-30 14:31:49 +0100 | [diff] [blame] | 325 | * This must be odd, and have exactly the same number |
| 326 | * of limbs as \p A. |
Tom Cosgrove | 6da3a3b | 2022-09-29 17:20:18 +0100 | [diff] [blame] | 327 | * It may alias \p X, but must not alias or otherwise |
| 328 | * overlap any of the other parameters. |
Tom Cosgrove | 5dd97e6 | 2022-08-30 14:31:49 +0100 | [diff] [blame] | 329 | * \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] | 330 | * \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] | 331 | * This can be calculated by `mbedtls_mpi_core_montmul_init()`. |
Tom Cosgrove | b496486 | 2022-08-30 11:57:22 +0100 | [diff] [blame] | 332 | * \param[in,out] T Temporary storage of size at least 2*AN_limbs+1 limbs. |
| 333 | * Its initial content is unused and |
| 334 | * its final content is indeterminate. |
Tom Cosgrove | 4386ead | 2022-09-29 14:40:21 +0100 | [diff] [blame] | 335 | * It must not alias or otherwise overlap any of the |
| 336 | * other parameters. |
Tom Cosgrove | b496486 | 2022-08-30 11:57:22 +0100 | [diff] [blame] | 337 | */ |
| 338 | void mbedtls_mpi_core_montmul( mbedtls_mpi_uint *X, |
| 339 | const mbedtls_mpi_uint *A, |
| 340 | const mbedtls_mpi_uint *B, size_t B_limbs, |
| 341 | const mbedtls_mpi_uint *N, size_t AN_limbs, |
| 342 | mbedtls_mpi_uint mm, mbedtls_mpi_uint *T ); |
| 343 | |
Gabor Mezei | b903070 | 2022-07-18 23:09:45 +0200 | [diff] [blame] | 344 | #endif /* MBEDTLS_BIGNUM_CORE_H */ |