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 | * |
| 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 | |
Janos Follath | 5005edb | 2022-07-19 12:45:13 +0100 | [diff] [blame] | 25 | #ifndef MBEDTLS_BIGNUM_MOD_RAW_H |
| 26 | #define MBEDTLS_BIGNUM_MOD_RAW_H |
Gabor Mezei | c5328cf | 2022-07-18 23:13:13 +0200 | [diff] [blame] | 27 | |
| 28 | #include "common.h" |
| 29 | |
| 30 | #if defined(MBEDTLS_BIGNUM_C) |
| 31 | #include "mbedtls/bignum.h" |
| 32 | #endif |
| 33 | |
Janos Follath | 0ded631 | 2022-08-09 13:34:54 +0100 | [diff] [blame] | 34 | #include "bignum_mod.h" |
| 35 | |
Gabor Mezei | 12071d4 | 2022-09-12 16:35:58 +0200 | [diff] [blame] | 36 | /** |
| 37 | * \brief Perform a safe conditional copy of MPI which doesn't reveal whether |
| 38 | * the condition was true or not. |
| 39 | * |
| 40 | * \param[OUT] X The address of the first MPI. This must be initialized. |
Gabor Mezei | 1c628d5 | 2022-09-27 12:13:51 +0200 | [diff] [blame^] | 41 | * \param[IN] A The address of the second MPI. This must be initialized. |
| 42 | * \param[IN] m The address of the modulus related to \p X and \p A. |
Gabor Mezei | 12071d4 | 2022-09-12 16:35:58 +0200 | [diff] [blame] | 43 | * \param assign The condition deciding whether to perform the |
| 44 | * assignment or not. Must be either 0 or 1: |
Gabor Mezei | 1c628d5 | 2022-09-27 12:13:51 +0200 | [diff] [blame^] | 45 | * * \c 1: Perform the assignment `X = A`. |
Gabor Mezei | 12071d4 | 2022-09-12 16:35:58 +0200 | [diff] [blame] | 46 | * * \c 0: Keep the original value of \p X. |
| 47 | * |
| 48 | * \note This function avoids leaking any information about whether |
| 49 | * the assignment was done or not. |
| 50 | * |
| 51 | * \warning If \p assign is neither 0 nor 1, the result of this function |
| 52 | * is indeterminate, and the resulting value in \p X might be |
Gabor Mezei | 1c628d5 | 2022-09-27 12:13:51 +0200 | [diff] [blame^] | 53 | * neither its original value nor the value in \p B. |
Gabor Mezei | 12071d4 | 2022-09-12 16:35:58 +0200 | [diff] [blame] | 54 | */ |
Gabor Mezei | 63c3282 | 2022-09-15 20:01:31 +0200 | [diff] [blame] | 55 | void mbedtls_mpi_mod_raw_cond_assign( mbedtls_mpi_uint *X, |
Gabor Mezei | 1c628d5 | 2022-09-27 12:13:51 +0200 | [diff] [blame^] | 56 | const mbedtls_mpi_uint *A, |
Gabor Mezei | 63c3282 | 2022-09-15 20:01:31 +0200 | [diff] [blame] | 57 | const mbedtls_mpi_mod_modulus *m, |
| 58 | unsigned char assign ); |
Gabor Mezei | 12071d4 | 2022-09-12 16:35:58 +0200 | [diff] [blame] | 59 | |
| 60 | /** |
Gabor Mezei | 2b5bf4c | 2022-09-26 17:09:58 +0200 | [diff] [blame] | 61 | * \brief Perform a safe conditional swap of MPI which doesn't reveal whether |
Gabor Mezei | 12071d4 | 2022-09-12 16:35:58 +0200 | [diff] [blame] | 62 | * the condition was true or not. |
| 63 | * |
Gabor Mezei | 1c628d5 | 2022-09-27 12:13:51 +0200 | [diff] [blame^] | 64 | * \param[IN,OUT] A The address of the first MPI. This must be initialized. |
| 65 | * \param[IN,OUT] B The address of the second MPI. This must be initialized. |
| 66 | * \param[IN] m The address of the modulus related to \p A and \p B. |
Gabor Mezei | 12071d4 | 2022-09-12 16:35:58 +0200 | [diff] [blame] | 67 | * \param swap The condition deciding whether to perform |
| 68 | * the swap or not. Must be either 0 or 1: |
Gabor Mezei | 1c628d5 | 2022-09-27 12:13:51 +0200 | [diff] [blame^] | 69 | * * \c 1: Swap the values of \p A and \p B. |
| 70 | * * \c 0: Keep the original values of \p A and \p B. |
Gabor Mezei | 12071d4 | 2022-09-12 16:35:58 +0200 | [diff] [blame] | 71 | * |
| 72 | * \note This function avoids leaking any information about whether |
| 73 | * the swap was done or not. |
| 74 | * |
| 75 | * \warning If \p swap is neither 0 nor 1, the result of this function |
Gabor Mezei | 1c628d5 | 2022-09-27 12:13:51 +0200 | [diff] [blame^] | 76 | * is indeterminate, and both \p A and \p B might end up with |
Gabor Mezei | 12071d4 | 2022-09-12 16:35:58 +0200 | [diff] [blame] | 77 | * values different to either of the original ones. |
Gabor Mezei | 63c3282 | 2022-09-15 20:01:31 +0200 | [diff] [blame] | 78 | */ |
Gabor Mezei | 1c628d5 | 2022-09-27 12:13:51 +0200 | [diff] [blame^] | 79 | void mbedtls_mpi_mod_raw_cond_swap( mbedtls_mpi_uint *A, |
| 80 | mbedtls_mpi_uint *B, |
Gabor Mezei | 63c3282 | 2022-09-15 20:01:31 +0200 | [diff] [blame] | 81 | const mbedtls_mpi_mod_modulus *m, |
| 82 | unsigned char swap ); |
Gabor Mezei | 12071d4 | 2022-09-12 16:35:58 +0200 | [diff] [blame] | 83 | |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 84 | /** Import X from unsigned binary data. |
| 85 | * |
Janos Follath | 6318468 | 2022-08-11 17:42:59 +0100 | [diff] [blame] | 86 | * The MPI needs to have enough limbs to store the full value (including any |
| 87 | * most significant zero bytes in the input). |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 88 | * |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 89 | * \param[out] X The address of the MPI. The size is determined by \p m. |
| 90 | * (In particular, it must have at least as many limbs as |
| 91 | * the modulus \p m.) |
| 92 | * \param[in] m The address of the modulus related to \p X. |
| 93 | * \param[in] input The input buffer to import from. |
| 94 | * \param input_length The length in bytes of \p input. |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 95 | * |
| 96 | * \return \c 0 if successful. |
| 97 | * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p X isn't |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 98 | * large enough to hold the value in \p input. |
Janos Follath | 8ff0729 | 2022-08-08 08:39:52 +0100 | [diff] [blame] | 99 | * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if the external representation |
Janos Follath | dae1147 | 2022-08-08 11:50:02 +0100 | [diff] [blame] | 100 | * of \p m is invalid or \p X is not less than \p m. |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 101 | */ |
Gabor Mezei | c5328cf | 2022-07-18 23:13:13 +0200 | [diff] [blame] | 102 | int mbedtls_mpi_mod_raw_read( mbedtls_mpi_uint *X, |
Janos Follath | 6b8a4ad | 2022-08-19 10:58:34 +0100 | [diff] [blame] | 103 | const mbedtls_mpi_mod_modulus *m, |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 104 | const unsigned char *input, |
| 105 | size_t input_length ); |
Gabor Mezei | c5328cf | 2022-07-18 23:13:13 +0200 | [diff] [blame] | 106 | |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 107 | /** Export A into unsigned binary data. |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 108 | * |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 109 | * \param[in] A The address of the MPI. The size is determined by \p m. |
| 110 | * (In particular, it must have at least as many limbs as |
| 111 | * the modulus \p m.) |
| 112 | * \param[in] m The address of the modulus related to \p A. |
| 113 | * \param[out] output The output buffer to export to. |
| 114 | * \param output_length The length in bytes of \p output. |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 115 | * |
| 116 | * \return \c 0 if successful. |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 117 | * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p output isn't |
| 118 | * large enough to hold the value of \p A. |
Janos Follath | 8ff0729 | 2022-08-08 08:39:52 +0100 | [diff] [blame] | 119 | * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if the external representation |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 120 | * of \p m is invalid. |
| 121 | */ |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 122 | int mbedtls_mpi_mod_raw_write( const mbedtls_mpi_uint *A, |
Janos Follath | 6b8a4ad | 2022-08-19 10:58:34 +0100 | [diff] [blame] | 123 | const mbedtls_mpi_mod_modulus *m, |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 124 | unsigned char *output, |
| 125 | size_t output_length ); |
Gabor Mezei | c5328cf | 2022-07-18 23:13:13 +0200 | [diff] [blame] | 126 | |
Janos Follath | 5005edb | 2022-07-19 12:45:13 +0100 | [diff] [blame] | 127 | #endif /* MBEDTLS_BIGNUM_MOD_RAW_H */ |