| Gabor Mezei | c5328cf | 2022-07-18 23:13:13 +0200 | [diff] [blame] | 1 | /** | 
| Janos Follath | a95f204 | 2022-08-19 12:09:17 +0100 | [diff] [blame] | 2 | *  Low-level modular bignum functions | 
| Janos Follath | 6318468 | 2022-08-11 17:42:59 +0100 | [diff] [blame] | 3 | * | 
| Janos Follath | af3f39c | 2022-08-22 09:06:32 +0100 | [diff] [blame] | 4 | *  This interface should only be used by the higher-level modular bignum | 
| Janos Follath | 6318468 | 2022-08-11 17:42:59 +0100 | [diff] [blame] | 5 | *  module (bignum_mod.c) and the ECP module (ecp.c, ecp_curves.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 | c5328cf | 2022-07-18 23:13:13 +0200 | [diff] [blame] | 8 | * | 
| Gilles Peskine | 7aab2fb | 2022-09-27 13:19:13 +0200 | [diff] [blame] | 9 | * This is a low-level interface to operations on integers modulo which | 
|  | 10 | * has no protection against passing invalid arguments such as arrays of | 
|  | 11 | * the wrong size. The functions in bignum_mod.h provide a higher-level | 
|  | 12 | * interface that includes protections against accidental misuse, at the | 
|  | 13 | * expense of code size and sometimes more cumbersome memory management. | 
| Werner Lewis | 5e9d2e9 | 2022-12-12 14:00:25 +0000 | [diff] [blame] | 14 | * | 
|  | 15 | * The functions in this module obey the following conventions unless | 
|  | 16 | * explicitly indicated otherwise: | 
|  | 17 | * - **Modulus parameters**: the modulus is passed as a pointer to a structure | 
| Werner Lewis | e1eb75d | 2022-12-14 13:45:49 +0000 | [diff] [blame] | 18 | *   of type #mbedtls_mpi_mod_modulus. The structure must be set up with an | 
|  | 19 | *   array of limbs storing the bignum value of the modulus. The modulus must | 
|  | 20 | *   be odd and is assumed to have no leading zeroes. The modulus is usually | 
| Werner Lewis | 214ae64 | 2022-12-15 10:57:59 +0000 | [diff] [blame] | 21 | *   named \c N and is usually input-only. | 
| Werner Lewis | 5e9d2e9 | 2022-12-12 14:00:25 +0000 | [diff] [blame] | 22 | * - **Bignum parameters**: Bignums are passed as pointers to an array of | 
|  | 23 | *   limbs. A limb has the type #mbedtls_mpi_uint. Unless otherwise specified: | 
| Werner Lewis | 214ae64 | 2022-12-15 10:57:59 +0000 | [diff] [blame] | 24 | *     - Bignum parameters called \c A, \c B, ... are inputs, and are not | 
| Werner Lewis | 5e9d2e9 | 2022-12-12 14:00:25 +0000 | [diff] [blame] | 25 | *       modified by the function. | 
| Werner Lewis | 214ae64 | 2022-12-15 10:57:59 +0000 | [diff] [blame] | 26 | *     - Bignum parameters called \c X, \c Y are outputs or input-output. | 
| Werner Lewis | 5e9d2e9 | 2022-12-12 14:00:25 +0000 | [diff] [blame] | 27 | *       The initial content of output-only parameters is ignored. | 
| Werner Lewis | 214ae64 | 2022-12-15 10:57:59 +0000 | [diff] [blame] | 28 | *     - \c T is a temporary storage area. The initial content of such a | 
| Werner Lewis | 5e9d2e9 | 2022-12-12 14:00:25 +0000 | [diff] [blame] | 29 | *       parameter is ignored and the final content is unspecified. | 
| Werner Lewis | 2e70b9a | 2022-12-14 15:48:31 +0000 | [diff] [blame] | 30 | * - **Bignum sizes**: bignum sizes are usually expressed by the \c limbs | 
|  | 31 | *   member of the modulus argument. All bignum parameters must have the same | 
| Werner Lewis | 5e9d2e9 | 2022-12-12 14:00:25 +0000 | [diff] [blame] | 32 | *   number of limbs as the modulus. All bignum sizes must be at least 1 and | 
|  | 33 | *   must be significantly less than #SIZE_MAX. The behavior if a size is 0 is | 
|  | 34 | *   undefined. | 
|  | 35 | * - **Bignum representation**: the representation of inputs and outputs is | 
| Werner Lewis | 214ae64 | 2022-12-15 10:57:59 +0000 | [diff] [blame] | 36 | *   specified by the \c int_rep field of the modulus for arithmetic | 
| Werner Lewis | 5e9d2e9 | 2022-12-12 14:00:25 +0000 | [diff] [blame] | 37 | *   functions. Utility functions may allow for different representation. | 
|  | 38 | * - **Parameter ordering**: for bignum parameters, outputs come before inputs. | 
| Werner Lewis | a306886 | 2022-12-14 15:57:12 +0000 | [diff] [blame] | 39 | *   The modulus is passed after other bignum input parameters. Temporaries | 
|  | 40 | *   come last. | 
| Werner Lewis | 5e9d2e9 | 2022-12-12 14:00:25 +0000 | [diff] [blame] | 41 | * - **Aliasing**: in general, output bignums may be aliased to one or more | 
|  | 42 | *   inputs. Modulus values may not be aliased to any other parameter. Outputs | 
|  | 43 | *   may not be aliased to one another. Temporaries may not be aliased to any | 
|  | 44 | *   other parameter. | 
|  | 45 | * - **Overlap**: apart from aliasing of limb array pointers (where two | 
|  | 46 | *   arguments are equal pointers), overlap is not supported and may result | 
|  | 47 | *   in undefined behavior. | 
|  | 48 | * - **Error handling**: This is a low-level module. Functions generally do not | 
|  | 49 | *   try to protect against invalid arguments such as nonsensical sizes or | 
|  | 50 | *   null pointers. Note that passing bignums with a different size than the | 
|  | 51 | *   modulus may lead to buffer overflows. Some functions which allocate | 
|  | 52 | *   memory or handle reading/writing of bignums will return an error if | 
|  | 53 | *   memory allocation fails or if buffer sizes are invalid. | 
| Werner Lewis | 1d89ebf | 2022-12-14 17:08:43 +0000 | [diff] [blame] | 54 | * - **Modular representatives**: all functions expect inputs to be in the | 
| Werner Lewis | 214ae64 | 2022-12-15 10:57:59 +0000 | [diff] [blame] | 55 | *   range [0, \c N - 1] and guarantee outputs in the range [0, \c N - 1]. If | 
| Werner Lewis | 1d89ebf | 2022-12-14 17:08:43 +0000 | [diff] [blame] | 56 | *   an input is out of range, outputs are fully unspecified, though bignum | 
|  | 57 | *   values out of range should not cause buffer overflows (beware that this is | 
|  | 58 | *   not extensively tested). | 
| Gilles Peskine | 7f887bd | 2022-09-27 13:12:30 +0200 | [diff] [blame] | 59 | */ | 
|  | 60 |  | 
|  | 61 | /* | 
| Gabor Mezei | c5328cf | 2022-07-18 23:13:13 +0200 | [diff] [blame] | 62 | *  Copyright The Mbed TLS Contributors | 
|  | 63 | *  SPDX-License-Identifier: Apache-2.0 | 
|  | 64 | * | 
|  | 65 | *  Licensed under the Apache License, Version 2.0 (the "License"); you may | 
|  | 66 | *  not use this file except in compliance with the License. | 
|  | 67 | *  You may obtain a copy of the License at | 
|  | 68 | * | 
|  | 69 | *  http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 70 | * | 
|  | 71 | *  Unless required by applicable law or agreed to in writing, software | 
|  | 72 | *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | 
|  | 73 | *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 74 | *  See the License for the specific language governing permissions and | 
|  | 75 | *  limitations under the License. | 
|  | 76 | */ | 
|  | 77 |  | 
| Janos Follath | 5005edb | 2022-07-19 12:45:13 +0100 | [diff] [blame] | 78 | #ifndef MBEDTLS_BIGNUM_MOD_RAW_H | 
|  | 79 | #define MBEDTLS_BIGNUM_MOD_RAW_H | 
| Gabor Mezei | c5328cf | 2022-07-18 23:13:13 +0200 | [diff] [blame] | 80 |  | 
|  | 81 | #include "common.h" | 
|  | 82 |  | 
|  | 83 | #if defined(MBEDTLS_BIGNUM_C) | 
|  | 84 | #include "mbedtls/bignum.h" | 
|  | 85 | #endif | 
|  | 86 |  | 
| Janos Follath | 0ded631 | 2022-08-09 13:34:54 +0100 | [diff] [blame] | 87 | #include "bignum_mod.h" | 
|  | 88 |  | 
| Gabor Mezei | 12071d4 | 2022-09-12 16:35:58 +0200 | [diff] [blame] | 89 | /** | 
| Gabor Mezei | 4086de6 | 2022-10-14 16:29:42 +0200 | [diff] [blame] | 90 | * \brief   Perform a safe conditional copy of an MPI which doesn't reveal | 
|  | 91 | *          whether the assignment was done or not. | 
| Gabor Mezei | 12071d4 | 2022-09-12 16:35:58 +0200 | [diff] [blame] | 92 | * | 
| Gabor Mezei | dba2677 | 2022-10-03 17:01:02 +0200 | [diff] [blame] | 93 | * The size to copy is determined by \p N. | 
|  | 94 | * | 
| Gabor Mezei | 4086de6 | 2022-10-14 16:29:42 +0200 | [diff] [blame] | 95 | * \param[out] X        The address of the destination MPI. | 
|  | 96 | *                      This must be initialized. Must have enough limbs to | 
|  | 97 | *                      store the full value of \p A. | 
|  | 98 | * \param[in]  A        The address of the source MPI. This must be initialized. | 
| Gabor Mezei | 86dfe38 | 2022-09-30 14:03:04 +0200 | [diff] [blame] | 99 | * \param[in]  N        The address of the modulus related to \p X and \p A. | 
| Gabor Mezei | 12071d4 | 2022-09-12 16:35:58 +0200 | [diff] [blame] | 100 | * \param      assign   The condition deciding whether to perform the | 
|  | 101 | *                      assignment or not. Must be either 0 or 1: | 
| Gabor Mezei | 1c628d5 | 2022-09-27 12:13:51 +0200 | [diff] [blame] | 102 | *                      * \c 1: Perform the assignment `X = A`. | 
| Gabor Mezei | 12071d4 | 2022-09-12 16:35:58 +0200 | [diff] [blame] | 103 | *                      * \c 0: Keep the original value of \p X. | 
|  | 104 | * | 
|  | 105 | * \note           This function avoids leaking any information about whether | 
|  | 106 | *                 the assignment was done or not. | 
|  | 107 | * | 
|  | 108 | * \warning        If \p assign is neither 0 nor 1, the result of this function | 
|  | 109 | *                 is indeterminate, and the resulting value in \p X might be | 
| Gabor Mezei | 4086de6 | 2022-10-14 16:29:42 +0200 | [diff] [blame] | 110 | *                 neither its original value nor the value in \p A. | 
| Gabor Mezei | 12071d4 | 2022-09-12 16:35:58 +0200 | [diff] [blame] | 111 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 112 | void mbedtls_mpi_mod_raw_cond_assign(mbedtls_mpi_uint *X, | 
|  | 113 | const mbedtls_mpi_uint *A, | 
|  | 114 | const mbedtls_mpi_mod_modulus *N, | 
|  | 115 | unsigned char assign); | 
| Gabor Mezei | 12071d4 | 2022-09-12 16:35:58 +0200 | [diff] [blame] | 116 |  | 
|  | 117 | /** | 
| Gabor Mezei | 4086de6 | 2022-10-14 16:29:42 +0200 | [diff] [blame] | 118 | * \brief   Perform a safe conditional swap of two MPIs which doesn't reveal | 
|  | 119 | *          whether the swap was done or not. | 
| Gabor Mezei | 12071d4 | 2022-09-12 16:35:58 +0200 | [diff] [blame] | 120 | * | 
| Gabor Mezei | dba2677 | 2022-10-03 17:01:02 +0200 | [diff] [blame] | 121 | * The size to swap is determined by \p N. | 
|  | 122 | * | 
| Gabor Mezei | 86dfe38 | 2022-09-30 14:03:04 +0200 | [diff] [blame] | 123 | * \param[in,out] X     The address of the first MPI. This must be initialized. | 
|  | 124 | * \param[in,out] Y     The address of the second MPI. This must be initialized. | 
|  | 125 | * \param[in]     N     The address of the modulus related to \p X and \p Y. | 
| Gabor Mezei | 12071d4 | 2022-09-12 16:35:58 +0200 | [diff] [blame] | 126 | * \param         swap  The condition deciding whether to perform | 
|  | 127 | *                      the swap or not. Must be either 0 or 1: | 
| Gabor Mezei | e5b8585 | 2022-09-30 13:54:02 +0200 | [diff] [blame] | 128 | *                      * \c 1: Swap the values of \p X and \p Y. | 
|  | 129 | *                      * \c 0: Keep the original values of \p X and \p Y. | 
| Gabor Mezei | 12071d4 | 2022-09-12 16:35:58 +0200 | [diff] [blame] | 130 | * | 
|  | 131 | * \note           This function avoids leaking any information about whether | 
|  | 132 | *                 the swap was done or not. | 
|  | 133 | * | 
|  | 134 | * \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] | 135 | *                 is indeterminate, and both \p X and \p Y might end up with | 
| Gabor Mezei | 12071d4 | 2022-09-12 16:35:58 +0200 | [diff] [blame] | 136 | *                 values different to either of the original ones. | 
| Gabor Mezei | 63c3282 | 2022-09-15 20:01:31 +0200 | [diff] [blame] | 137 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 138 | void mbedtls_mpi_mod_raw_cond_swap(mbedtls_mpi_uint *X, | 
|  | 139 | mbedtls_mpi_uint *Y, | 
|  | 140 | const mbedtls_mpi_mod_modulus *N, | 
|  | 141 | unsigned char swap); | 
| Gabor Mezei | 12071d4 | 2022-09-12 16:35:58 +0200 | [diff] [blame] | 142 |  | 
| Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 143 | /** Import X from unsigned binary data. | 
|  | 144 | * | 
| Janos Follath | 6318468 | 2022-08-11 17:42:59 +0100 | [diff] [blame] | 145 | * The MPI needs to have enough limbs to store the full value (including any | 
|  | 146 | * most significant zero bytes in the input). | 
| Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 147 | * | 
| Mihir Raj Singh | cd17ff0 | 2023-01-11 20:56:13 +0530 | [diff] [blame] | 148 | * \param[out] X        The address of the MPI. The size is determined by \p N. | 
| Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 149 | *                      (In particular, it must have at least as many limbs as | 
| Mihir Raj Singh | cd17ff0 | 2023-01-11 20:56:13 +0530 | [diff] [blame] | 150 | *                      the modulus \p N.) | 
|  | 151 | * \param[in] N         The address of the modulus related to \p X. | 
| Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 152 | * \param[in] input     The input buffer to import from. | 
|  | 153 | * \param input_length  The length in bytes of \p input. | 
| Janos Follath | d3eed33 | 2022-11-24 17:42:02 +0000 | [diff] [blame] | 154 | * \param ext_rep       The endianness of the number in the input buffer. | 
| Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 155 | * | 
|  | 156 | * \return       \c 0 if successful. | 
|  | 157 | * \return       #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p X isn't | 
| Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 158 | *               large enough to hold the value in \p input. | 
| Janos Follath | 8ff0729 | 2022-08-08 08:39:52 +0100 | [diff] [blame] | 159 | * \return       #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if the external representation | 
| Mihir Raj Singh | cd17ff0 | 2023-01-11 20:56:13 +0530 | [diff] [blame] | 160 | *               of \p N is invalid or \p X is not less than \p N. | 
| Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 161 | */ | 
| Mihir Raj Singh | 432cacf | 2023-01-11 21:12:46 +0530 | [diff] [blame] | 162 | int mbedtls_mpi_mod_raw_read(mbedtls_mpi_uint *X, | 
|  | 163 | const mbedtls_mpi_mod_modulus *N, | 
|  | 164 | const unsigned char *input, | 
|  | 165 | size_t input_length, | 
|  | 166 | mbedtls_mpi_mod_ext_rep ext_rep); | 
| Gabor Mezei | c5328cf | 2022-07-18 23:13:13 +0200 | [diff] [blame] | 167 |  | 
| Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 168 | /** Export A into unsigned binary data. | 
| Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 169 | * | 
| Mihir Raj Singh | 01e861f | 2023-01-11 21:00:42 +0530 | [diff] [blame] | 170 | * \param[in] A         The address of the MPI. The size is determined by \p N. | 
| Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 171 | *                      (In particular, it must have at least as many limbs as | 
| Mihir Raj Singh | 01e861f | 2023-01-11 21:00:42 +0530 | [diff] [blame] | 172 | *                      the modulus \p N.) | 
|  | 173 | * \param[in] N         The address of the modulus related to \p A. | 
| Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 174 | * \param[out] output   The output buffer to export to. | 
|  | 175 | * \param output_length The length in bytes of \p output. | 
| Janos Follath | d3eed33 | 2022-11-24 17:42:02 +0000 | [diff] [blame] | 176 | * \param ext_rep       The endianness in which the number should be written into the output buffer. | 
| Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 177 | * | 
|  | 178 | * \return       \c 0 if successful. | 
| Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 179 | * \return       #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p output isn't | 
|  | 180 | *               large enough to hold the value of \p A. | 
| Janos Follath | 8ff0729 | 2022-08-08 08:39:52 +0100 | [diff] [blame] | 181 | * \return       #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if the external representation | 
| Mihir Raj Singh | 01e861f | 2023-01-11 21:00:42 +0530 | [diff] [blame] | 182 | *               of \p N is invalid. | 
| Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 183 | */ | 
| Mihir Raj Singh | 432cacf | 2023-01-11 21:12:46 +0530 | [diff] [blame] | 184 | int mbedtls_mpi_mod_raw_write(const mbedtls_mpi_uint *A, | 
|  | 185 | const mbedtls_mpi_mod_modulus *N, | 
|  | 186 | unsigned char *output, | 
|  | 187 | size_t output_length, | 
|  | 188 | mbedtls_mpi_mod_ext_rep ext_rep); | 
| Gabor Mezei | c5328cf | 2022-07-18 23:13:13 +0200 | [diff] [blame] | 189 |  | 
| Gabor Mezei | 02d2313 | 2022-11-23 13:09:43 +0100 | [diff] [blame] | 190 | /** \brief  Subtract two MPIs, returning the residue modulo the specified | 
|  | 191 | *          modulus. | 
| Gabor Mezei | 4c7cf7d | 2022-11-09 14:07:43 +0100 | [diff] [blame] | 192 | * | 
| Gabor Mezei | 02d2313 | 2022-11-23 13:09:43 +0100 | [diff] [blame] | 193 | * The size of the operation is determined by \p N. \p A and \p B must have | 
|  | 194 | * the same number of limbs as \p N. | 
|  | 195 | * | 
|  | 196 | * \p X may be aliased to \p A or \p B, or even both, but may not overlap | 
|  | 197 | * either otherwise. | 
| Gabor Mezei | 4c7cf7d | 2022-11-09 14:07:43 +0100 | [diff] [blame] | 198 | * | 
|  | 199 | * \param[out] X        The address of the result MPI. | 
|  | 200 | *                      This must be initialized. Must have enough limbs to | 
|  | 201 | *                      store the full value of the result. | 
|  | 202 | * \param[in]  A        The address of the first MPI. This must be initialized. | 
|  | 203 | * \param[in]  B        The address of the second MPI. This must be initialized. | 
| Gabor Mezei | 02d2313 | 2022-11-23 13:09:43 +0100 | [diff] [blame] | 204 | * \param[in]  N        The address of the modulus. Used to perform a modulo | 
| Gabor Mezei | 4c7cf7d | 2022-11-09 14:07:43 +0100 | [diff] [blame] | 205 | *                      operation on the result of the subtraction. | 
| Gabor Mezei | 4c7cf7d | 2022-11-09 14:07:43 +0100 | [diff] [blame] | 206 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 207 | void mbedtls_mpi_mod_raw_sub(mbedtls_mpi_uint *X, | 
|  | 208 | const mbedtls_mpi_uint *A, | 
|  | 209 | const mbedtls_mpi_uint *B, | 
|  | 210 | const mbedtls_mpi_mod_modulus *N); | 
| Gabor Mezei | 4c7cf7d | 2022-11-09 14:07:43 +0100 | [diff] [blame] | 211 |  | 
| Gabor Mezei | 979d34c | 2022-12-07 16:02:33 +0100 | [diff] [blame] | 212 | /** \brief  Multiply two MPIs, returning the residue modulo the specified | 
|  | 213 | *          modulus. | 
|  | 214 | * | 
| Gabor Mezei | 210ea63 | 2022-12-16 16:35:24 +0100 | [diff] [blame] | 215 | * \note Currently handles the case when `N->int_rep` is | 
| Gabor Mezei | 95b754d | 2022-12-15 10:14:18 +0100 | [diff] [blame] | 216 | * MBEDTLS_MPI_MOD_REP_MONTGOMERY. | 
|  | 217 | * | 
|  | 218 | * The size of the operation is determined by \p N. \p A, \p B and \p X must | 
|  | 219 | * all be associated with the modulus \p N and must all have the same number | 
|  | 220 | * of limbs as \p N. | 
| Gabor Mezei | 979d34c | 2022-12-07 16:02:33 +0100 | [diff] [blame] | 221 | * | 
|  | 222 | * \p X may be aliased to \p A or \p B, or even both, but may not overlap | 
| Gabor Mezei | 95b754d | 2022-12-15 10:14:18 +0100 | [diff] [blame] | 223 | * either otherwise. They may not alias \p N (since they must be in canonical | 
|  | 224 | * form, they cannot == \p N). | 
| Gabor Mezei | 979d34c | 2022-12-07 16:02:33 +0100 | [diff] [blame] | 225 | * | 
| Gabor Mezei | 95b754d | 2022-12-15 10:14:18 +0100 | [diff] [blame] | 226 | * \param[out] X        The address of the result MPI. Must have the same | 
|  | 227 | *                      number of limbs as \p N. | 
|  | 228 | *                      On successful completion, \p X contains the result of | 
|  | 229 | *                      the multiplication `A * B * R^-1` mod N where | 
|  | 230 | *                      `R = 2^(biL * N->limbs)`. | 
|  | 231 | * \param[in]  A        The address of the first MPI. | 
|  | 232 | * \param[in]  B        The address of the second MPI. | 
| Gabor Mezei | 979d34c | 2022-12-07 16:02:33 +0100 | [diff] [blame] | 233 | * \param[in]  N        The address of the modulus. Used to perform a modulo | 
| Gabor Mezei | 95b754d | 2022-12-15 10:14:18 +0100 | [diff] [blame] | 234 | *                      operation on the result of the multiplication. | 
| Gabor Mezei | 210ea63 | 2022-12-16 16:35:24 +0100 | [diff] [blame] | 235 | * \param[in,out] T     Temporary storage of size at least 2 * N->limbs + 1 | 
| Gabor Mezei | 95b754d | 2022-12-15 10:14:18 +0100 | [diff] [blame] | 236 | *                      limbs. Its initial content is unused and | 
|  | 237 | *                      its final content is indeterminate. | 
|  | 238 | *                      It must not alias or otherwise overlap any of the | 
|  | 239 | *                      other parameters. | 
| Gabor Mezei | 979d34c | 2022-12-07 16:02:33 +0100 | [diff] [blame] | 240 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 241 | void mbedtls_mpi_mod_raw_mul(mbedtls_mpi_uint *X, | 
|  | 242 | const mbedtls_mpi_uint *A, | 
|  | 243 | const mbedtls_mpi_uint *B, | 
|  | 244 | const mbedtls_mpi_mod_modulus *N, | 
|  | 245 | mbedtls_mpi_uint *T); | 
| Gabor Mezei | 979d34c | 2022-12-07 16:02:33 +0100 | [diff] [blame] | 246 |  | 
| Tom Cosgrove | 6129268 | 2022-12-08 09:44:10 +0000 | [diff] [blame] | 247 | /** | 
|  | 248 | * \brief          Returns the number of limbs of working memory required for | 
|  | 249 | *                 a call to `mbedtls_mpi_mod_raw_inv_prime()`. | 
|  | 250 | * | 
| Tom Cosgrove | 28ff92c | 2022-12-12 17:06:27 +0000 | [diff] [blame] | 251 | * \note           This will always be at least | 
|  | 252 | *                 `mbedtls_mpi_core_montmul_working_limbs(AN_limbs)`, | 
|  | 253 | *                 i.e. sufficient for a call to `mbedtls_mpi_core_montmul()`. | 
|  | 254 | * | 
| Tom Cosgrove | 6129268 | 2022-12-08 09:44:10 +0000 | [diff] [blame] | 255 | * \param AN_limbs The number of limbs in the input `A` and the modulus `N` | 
|  | 256 | *                 (they must be the same size) that will be given to | 
|  | 257 | *                 `mbedtls_mpi_mod_raw_inv_prime()`. | 
|  | 258 | * | 
|  | 259 | * \return         The number of limbs of working memory required by | 
|  | 260 | *                 `mbedtls_mpi_mod_raw_inv_prime()`. | 
|  | 261 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 262 | size_t mbedtls_mpi_mod_raw_inv_prime_working_limbs(size_t AN_limbs); | 
| Tom Cosgrove | 6129268 | 2022-12-08 09:44:10 +0000 | [diff] [blame] | 263 |  | 
|  | 264 | /** | 
|  | 265 | * \brief Perform fixed-width modular inversion of a Montgomery-form MPI with | 
|  | 266 | *        respect to a modulus \p N that must be prime. | 
|  | 267 | * | 
|  | 268 | * \p X may be aliased to \p A, but not to \p N or \p RR. | 
|  | 269 | * | 
|  | 270 | * \param[out] X     The modular inverse of \p A with respect to \p N. | 
|  | 271 | *                   Will be in Montgomery form. | 
|  | 272 | * \param[in] A      The number to calculate the modular inverse of. | 
|  | 273 | *                   Must be in Montgomery form. Must not be 0. | 
|  | 274 | * \param[in] N      The modulus, as a little-endian array of length \p AN_limbs. | 
|  | 275 | *                   Must be prime. | 
|  | 276 | * \param AN_limbs   The number of limbs in \p A, \p N and \p RR. | 
|  | 277 | * \param[in] RR     The precomputed residue of 2^{2*biL} modulo N, as a little- | 
|  | 278 | *                   endian array of length \p AN_limbs. | 
|  | 279 | * \param[in,out] T  Temporary storage of at least the number of limbs returned | 
|  | 280 | *                   by `mbedtls_mpi_mod_raw_inv_prime_working_limbs()`. | 
|  | 281 | *                   Its initial content is unused and its final content is | 
|  | 282 | *                   indeterminate. | 
|  | 283 | *                   It must not alias or otherwise overlap any of the other | 
|  | 284 | *                   parameters. | 
|  | 285 | *                   It is up to the caller to zeroize \p T when it is no | 
|  | 286 | *                   longer needed, and before freeing it if it was dynamically | 
|  | 287 | *                   allocated. | 
|  | 288 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 289 | void mbedtls_mpi_mod_raw_inv_prime(mbedtls_mpi_uint *X, | 
|  | 290 | const mbedtls_mpi_uint *A, | 
|  | 291 | const mbedtls_mpi_uint *N, | 
|  | 292 | size_t AN_limbs, | 
|  | 293 | const mbedtls_mpi_uint *RR, | 
|  | 294 | mbedtls_mpi_uint *T); | 
| Tom Cosgrove | 6129268 | 2022-12-08 09:44:10 +0000 | [diff] [blame] | 295 |  | 
| Hanno Becker | a45b6fe | 2022-11-01 13:14:28 +0000 | [diff] [blame] | 296 | /** | 
|  | 297 | * \brief Perform a known-size modular addition. | 
|  | 298 | * | 
| Tom Cosgrove | abddad4 | 2022-11-24 15:54:16 +0000 | [diff] [blame] | 299 | * Calculate `A + B modulo N`. | 
|  | 300 | * | 
|  | 301 | * The number of limbs in each operand, and the result, is given by the | 
|  | 302 | * modulus \p N. | 
|  | 303 | * | 
|  | 304 | * \p X may be aliased to \p A or \p B, or even both, but may not overlap | 
|  | 305 | * either otherwise. | 
| Hanno Becker | a45b6fe | 2022-11-01 13:14:28 +0000 | [diff] [blame] | 306 | * | 
| Werner Lewis | 0eea827 | 2022-11-01 13:27:29 +0000 | [diff] [blame] | 307 | * \param[out] X    The result of the modular addition. | 
|  | 308 | * \param[in] A     Little-endian presentation of the left operand. This | 
| Tom Cosgrove | abddad4 | 2022-11-24 15:54:16 +0000 | [diff] [blame] | 309 | *                  must be smaller than \p N. | 
| Werner Lewis | 0eea827 | 2022-11-01 13:27:29 +0000 | [diff] [blame] | 310 | * \param[in] B     Little-endian presentation of the right operand. This | 
| Tom Cosgrove | abddad4 | 2022-11-24 15:54:16 +0000 | [diff] [blame] | 311 | *                  must be smaller than \p N. | 
| Werner Lewis | 9fa91eb | 2022-11-01 13:36:51 +0000 | [diff] [blame] | 312 | * \param[in] N     The address of the modulus. | 
| Hanno Becker | a45b6fe | 2022-11-01 13:14:28 +0000 | [diff] [blame] | 313 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 314 | void mbedtls_mpi_mod_raw_add(mbedtls_mpi_uint *X, | 
|  | 315 | const mbedtls_mpi_uint *A, | 
|  | 316 | const mbedtls_mpi_uint *B, | 
|  | 317 | const mbedtls_mpi_mod_modulus *N); | 
| Janos Follath | 5933f69 | 2022-11-02 14:35:17 +0000 | [diff] [blame] | 318 |  | 
| Gilles Peskine | 1e2a4d4 | 2022-12-20 19:21:17 +0100 | [diff] [blame] | 319 | /** Convert an MPI from canonical representation (little-endian limb array) | 
|  | 320 | * to the representation associated with the modulus. | 
|  | 321 | * | 
|  | 322 | * \param[in,out] X The limb array to convert. | 
|  | 323 | *                  It must have as many limbs as \p N. | 
|  | 324 | *                  It is converted in place. | 
|  | 325 | *                  If this function returns an error, the content of \p X | 
|  | 326 | *                  is unspecified. | 
|  | 327 | * \param[in] N     The modulus structure. | 
|  | 328 | * | 
| Tom Cosgrove | 37dabd5 | 2023-01-23 16:57:26 +0000 | [diff] [blame] | 329 | * \return          \c 0 if successful. | 
| Gilles Peskine | 1e2a4d4 | 2022-12-20 19:21:17 +0100 | [diff] [blame] | 330 | *                  Otherwise an \c MBEDTLS_ERR_MPI_xxx error code. | 
|  | 331 | */ | 
|  | 332 | int mbedtls_mpi_mod_raw_canonical_to_modulus_rep( | 
|  | 333 | mbedtls_mpi_uint *X, | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 334 | const mbedtls_mpi_mod_modulus *N); | 
| Gilles Peskine | 1e2a4d4 | 2022-12-20 19:21:17 +0100 | [diff] [blame] | 335 |  | 
|  | 336 | /** Convert an MPI from the representation associated with the modulus | 
|  | 337 | * to canonical representation (little-endian limb array). | 
|  | 338 | * | 
|  | 339 | * \param[in,out] X The limb array to convert. | 
|  | 340 | *                  It must have as many limbs as \p N. | 
|  | 341 | *                  It is converted in place. | 
|  | 342 | *                  If this function returns an error, the content of \p X | 
|  | 343 | *                  is unspecified. | 
|  | 344 | * \param[in] N     The modulus structure. | 
|  | 345 | * | 
| Tom Cosgrove | 37dabd5 | 2023-01-23 16:57:26 +0000 | [diff] [blame] | 346 | * \return          \c 0 if successful. | 
| Gilles Peskine | 1e2a4d4 | 2022-12-20 19:21:17 +0100 | [diff] [blame] | 347 | *                  Otherwise an \c MBEDTLS_ERR_MPI_xxx error code. | 
|  | 348 | */ | 
|  | 349 | int mbedtls_mpi_mod_raw_modulus_to_canonical_rep( | 
|  | 350 | mbedtls_mpi_uint *X, | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 351 | const mbedtls_mpi_mod_modulus *N); | 
| Gilles Peskine | 1e2a4d4 | 2022-12-20 19:21:17 +0100 | [diff] [blame] | 352 |  | 
| Gilles Peskine | a57cf98 | 2022-12-06 22:54:09 +0100 | [diff] [blame] | 353 | /** Generate a random number uniformly in a range. | 
|  | 354 | * | 
|  | 355 | * This function generates a random number between \p min inclusive and | 
|  | 356 | * \p N exclusive. | 
|  | 357 | * | 
|  | 358 | * The procedure complies with RFC 6979 ยง3.3 (deterministic ECDSA) | 
|  | 359 | * when the RNG is a suitably parametrized instance of HMAC_DRBG | 
|  | 360 | * and \p min is \c 1. | 
|  | 361 | * | 
|  | 362 | * \note           There are `N - min` possible outputs. The lower bound | 
|  | 363 | *                 \p min can be reached, but the upper bound \p N cannot. | 
|  | 364 | * | 
|  | 365 | * \param X        The destination MPI, in canonical representation modulo \p N. | 
|  | 366 | *                 It must not be aliased with \p N or otherwise overlap it. | 
|  | 367 | * \param min      The minimum value to return. It must be strictly smaller | 
|  | 368 | *                 than \b N. | 
|  | 369 | * \param N        The modulus. | 
|  | 370 | *                 This is the upper bound of the output range, exclusive. | 
|  | 371 | * \param f_rng    The RNG function to use. This must not be \c NULL. | 
|  | 372 | * \param p_rng    The RNG parameter to be passed to \p f_rng. | 
|  | 373 | * | 
|  | 374 | * \return         \c 0 if successful. | 
|  | 375 | * \return         #MBEDTLS_ERR_MPI_NOT_ACCEPTABLE if the implementation was | 
|  | 376 | *                 unable to find a suitable value within a limited number | 
|  | 377 | *                 of attempts. This has a negligible probability if \p N | 
|  | 378 | *                 is significantly larger than \p min, which is the case | 
|  | 379 | *                 for all usual cryptographic applications. | 
|  | 380 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 381 | int mbedtls_mpi_mod_raw_random(mbedtls_mpi_uint *X, | 
|  | 382 | mbedtls_mpi_uint min, | 
|  | 383 | const mbedtls_mpi_mod_modulus *N, | 
|  | 384 | int (*f_rng)(void *, unsigned char *, size_t), | 
|  | 385 | void *p_rng); | 
| Gilles Peskine | a57cf98 | 2022-12-06 22:54:09 +0100 | [diff] [blame] | 386 |  | 
| Minos Galanakis | d9299c3 | 2022-11-01 16:19:07 +0000 | [diff] [blame] | 387 | /** Convert an MPI into Montgomery form. | 
| Hanno Becker | 5ad4a93 | 2022-08-09 14:45:53 +0100 | [diff] [blame] | 388 | * | 
|  | 389 | * \param X      The address of the MPI. | 
| Mihir Raj Singh | 37ece72 | 2023-01-11 21:06:16 +0530 | [diff] [blame] | 390 | *               Must have the same number of limbs as \p N. | 
|  | 391 | * \param N      The address of the modulus, which gives the size of | 
|  | 392 | *               the base `R` = 2^(biL*N->limbs). | 
| Hanno Becker | 5ad4a93 | 2022-08-09 14:45:53 +0100 | [diff] [blame] | 393 | * | 
|  | 394 | * \return       \c 0 if successful. | 
|  | 395 | */ | 
| Mihir Raj Singh | 432cacf | 2023-01-11 21:12:46 +0530 | [diff] [blame] | 396 | int mbedtls_mpi_mod_raw_to_mont_rep(mbedtls_mpi_uint *X, | 
|  | 397 | const mbedtls_mpi_mod_modulus *N); | 
| Janos Follath | 5933f69 | 2022-11-02 14:35:17 +0000 | [diff] [blame] | 398 |  | 
| Minos Galanakis | d9299c3 | 2022-11-01 16:19:07 +0000 | [diff] [blame] | 399 | /** Convert an MPI back from Montgomery representation. | 
| Hanno Becker | 5ad4a93 | 2022-08-09 14:45:53 +0100 | [diff] [blame] | 400 | * | 
|  | 401 | * \param X      The address of the MPI. | 
| Mihir Raj Singh | b0354c5 | 2023-01-11 21:10:22 +0530 | [diff] [blame] | 402 | *               Must have the same number of limbs as \p N. | 
|  | 403 | * \param N      The address of the modulus, which gives the size of | 
|  | 404 | *               the base `R`= 2^(biL*N->limbs). | 
| Hanno Becker | 5ad4a93 | 2022-08-09 14:45:53 +0100 | [diff] [blame] | 405 | * | 
|  | 406 | * \return       \c 0 if successful. | 
|  | 407 | */ | 
| Mihir Raj Singh | 432cacf | 2023-01-11 21:12:46 +0530 | [diff] [blame] | 408 | int mbedtls_mpi_mod_raw_from_mont_rep(mbedtls_mpi_uint *X, | 
|  | 409 | const mbedtls_mpi_mod_modulus *N); | 
| Minos Galanakis | 21fe8bd | 2022-12-07 18:06:05 +0000 | [diff] [blame] | 410 |  | 
|  | 411 | /** \brief  Perform fixed width modular negation. | 
|  | 412 | * | 
| Mihir Raj Singh | 432cacf | 2023-01-11 21:12:46 +0530 | [diff] [blame] | 413 | * The size of the operation is determined by \p N. \p A must have | 
|  | 414 | * the same number of limbs as \p N. | 
| Minos Galanakis | 21fe8bd | 2022-12-07 18:06:05 +0000 | [diff] [blame] | 415 | * | 
|  | 416 | * \p X may be aliased to \p A. | 
|  | 417 | * | 
|  | 418 | * \param[out] X        The result of the modular negation. | 
| Minos Galanakis | 5e8443e6 | 2022-12-08 11:40:51 +0000 | [diff] [blame] | 419 | *                      This must be initialized. | 
| Minos Galanakis | 21fe8bd | 2022-12-07 18:06:05 +0000 | [diff] [blame] | 420 | * \param[in] A         Little-endian presentation of the input operand. This | 
| Mihir Raj Singh | 432cacf | 2023-01-11 21:12:46 +0530 | [diff] [blame] | 421 | *                      must be less than or equal to \p N. | 
|  | 422 | * \param[in] N         The modulus to use. | 
| Minos Galanakis | 21fe8bd | 2022-12-07 18:06:05 +0000 | [diff] [blame] | 423 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 424 | void mbedtls_mpi_mod_raw_neg(mbedtls_mpi_uint *X, | 
|  | 425 | const mbedtls_mpi_uint *A, | 
| Mihir Raj Singh | 432cacf | 2023-01-11 21:12:46 +0530 | [diff] [blame] | 426 | const mbedtls_mpi_mod_modulus *N); | 
| Janos Follath | 5933f69 | 2022-11-02 14:35:17 +0000 | [diff] [blame] | 427 |  | 
| Janos Follath | 5005edb | 2022-07-19 12:45:13 +0100 | [diff] [blame] | 428 | #endif /* MBEDTLS_BIGNUM_MOD_RAW_H */ |