blob: 845ebaa5a702ae78bb568492159108a1589af161 [file] [log] [blame]
Gabor Mezeic5328cf2022-07-18 23:13:13 +02001/**
Janos Follatha95f2042022-08-19 12:09:17 +01002 * Low-level modular bignum functions
Janos Follath63184682022-08-11 17:42:59 +01003 *
Janos Follathaf3f39c2022-08-22 09:06:32 +01004 * This interface should only be used by the higher-level modular bignum
Janos Follath63184682022-08-11 17:42:59 +01005 * module (bignum_mod.c) and the ECP module (ecp.c, ecp_curves.c). All other
Janos Follathaf3f39c2022-08-22 09:06:32 +01006 * modules should use the high-level modular bignum interface (bignum_mod.h)
Janos Follath63184682022-08-11 17:42:59 +01007 * or the legacy bignum interface (bignum.h).
Gabor Mezeic5328cf2022-07-18 23:13:13 +02008 *
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 Follath5005edb2022-07-19 12:45:13 +010025#ifndef MBEDTLS_BIGNUM_MOD_RAW_H
26#define MBEDTLS_BIGNUM_MOD_RAW_H
Gabor Mezeic5328cf2022-07-18 23:13:13 +020027
28#include "common.h"
29
30#if defined(MBEDTLS_BIGNUM_C)
31#include "mbedtls/bignum.h"
32#endif
33
Janos Follath0ded6312022-08-09 13:34:54 +010034#include "bignum_mod.h"
35
Gabor Mezei12071d42022-09-12 16:35:58 +020036/**
37 * \brief Perform a safe conditional copy of MPI which doesn't reveal whether
38 * the condition was true or not.
39 *
Gabor Mezeidba26772022-10-03 17:01:02 +020040 * The size to copy is determined by \p N.
41 *
Gabor Mezei86dfe382022-09-30 14:03:04 +020042 * \param[out] X The address of the first MPI. This must be initialized.
Gabor Mezeidba26772022-10-03 17:01:02 +020043 * Must have enough limbs to store the full value of \p A.
Gabor Mezei86dfe382022-09-30 14:03:04 +020044 * \param[in] A The address of the second MPI. This must be initialized.
45 * \param[in] N The address of the modulus related to \p X and \p A.
Gabor Mezei12071d42022-09-12 16:35:58 +020046 * \param assign The condition deciding whether to perform the
47 * assignment or not. Must be either 0 or 1:
Gabor Mezei1c628d52022-09-27 12:13:51 +020048 * * \c 1: Perform the assignment `X = A`.
Gabor Mezei12071d42022-09-12 16:35:58 +020049 * * \c 0: Keep the original value of \p X.
50 *
51 * \note This function avoids leaking any information about whether
52 * the assignment was done or not.
53 *
54 * \warning If \p assign is neither 0 nor 1, the result of this function
55 * is indeterminate, and the resulting value in \p X might be
Gabor Mezei1c628d52022-09-27 12:13:51 +020056 * neither its original value nor the value in \p B.
Gabor Mezei12071d42022-09-12 16:35:58 +020057 */
Gabor Mezei63c32822022-09-15 20:01:31 +020058void mbedtls_mpi_mod_raw_cond_assign( mbedtls_mpi_uint *X,
Gabor Mezei1c628d52022-09-27 12:13:51 +020059 const mbedtls_mpi_uint *A,
Gabor Mezeie5b85852022-09-30 13:54:02 +020060 const mbedtls_mpi_mod_modulus *N,
Gabor Mezei63c32822022-09-15 20:01:31 +020061 unsigned char assign );
Gabor Mezei12071d42022-09-12 16:35:58 +020062
63/**
Gabor Mezei2b5bf4c2022-09-26 17:09:58 +020064 * \brief Perform a safe conditional swap of MPI which doesn't reveal whether
Gabor Mezei12071d42022-09-12 16:35:58 +020065 * the condition was true or not.
66 *
Gabor Mezeidba26772022-10-03 17:01:02 +020067 * The size to swap is determined by \p N.
68 *
Gabor Mezei86dfe382022-09-30 14:03:04 +020069 * \param[in,out] X The address of the first MPI. This must be initialized.
70 * \param[in,out] Y The address of the second MPI. This must be initialized.
71 * \param[in] N The address of the modulus related to \p X and \p Y.
Gabor Mezei12071d42022-09-12 16:35:58 +020072 * \param swap The condition deciding whether to perform
73 * the swap or not. Must be either 0 or 1:
Gabor Mezeie5b85852022-09-30 13:54:02 +020074 * * \c 1: Swap the values of \p X and \p Y.
75 * * \c 0: Keep the original values of \p X and \p Y.
Gabor Mezei12071d42022-09-12 16:35:58 +020076 *
77 * \note This function avoids leaking any information about whether
78 * the swap was done or not.
79 *
80 * \warning If \p swap is neither 0 nor 1, the result of this function
Gabor Mezeie5b85852022-09-30 13:54:02 +020081 * is indeterminate, and both \p X and \p Y might end up with
Gabor Mezei12071d42022-09-12 16:35:58 +020082 * values different to either of the original ones.
Gabor Mezei63c32822022-09-15 20:01:31 +020083 */
Gabor Mezeie5b85852022-09-30 13:54:02 +020084void mbedtls_mpi_mod_raw_cond_swap( mbedtls_mpi_uint *X,
85 mbedtls_mpi_uint *Y,
86 const mbedtls_mpi_mod_modulus *N,
Gabor Mezei63c32822022-09-15 20:01:31 +020087 unsigned char swap );
Gabor Mezei12071d42022-09-12 16:35:58 +020088
Gabor Mezei37b06362022-08-02 17:22:18 +020089/** Import X from unsigned binary data.
90 *
Janos Follath63184682022-08-11 17:42:59 +010091 * The MPI needs to have enough limbs to store the full value (including any
92 * most significant zero bytes in the input).
Gabor Mezei37b06362022-08-02 17:22:18 +020093 *
Janos Follathb7a88ec2022-08-19 12:24:40 +010094 * \param[out] X The address of the MPI. The size is determined by \p m.
95 * (In particular, it must have at least as many limbs as
96 * the modulus \p m.)
97 * \param[in] m The address of the modulus related to \p X.
98 * \param[in] input The input buffer to import from.
99 * \param input_length The length in bytes of \p input.
Gabor Mezei37b06362022-08-02 17:22:18 +0200100 *
101 * \return \c 0 if successful.
102 * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p X isn't
Janos Follathb7a88ec2022-08-19 12:24:40 +0100103 * large enough to hold the value in \p input.
Janos Follath8ff07292022-08-08 08:39:52 +0100104 * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if the external representation
Janos Follathdae11472022-08-08 11:50:02 +0100105 * of \p m is invalid or \p X is not less than \p m.
Gabor Mezei37b06362022-08-02 17:22:18 +0200106 */
Gabor Mezeic5328cf2022-07-18 23:13:13 +0200107int mbedtls_mpi_mod_raw_read( mbedtls_mpi_uint *X,
Janos Follath6b8a4ad2022-08-19 10:58:34 +0100108 const mbedtls_mpi_mod_modulus *m,
Janos Follathb7a88ec2022-08-19 12:24:40 +0100109 const unsigned char *input,
110 size_t input_length );
Gabor Mezeic5328cf2022-07-18 23:13:13 +0200111
Janos Follathb7a88ec2022-08-19 12:24:40 +0100112/** Export A into unsigned binary data.
Gabor Mezei37b06362022-08-02 17:22:18 +0200113 *
Janos Follathb7a88ec2022-08-19 12:24:40 +0100114 * \param[in] A The address of the MPI. The size is determined by \p m.
115 * (In particular, it must have at least as many limbs as
116 * the modulus \p m.)
117 * \param[in] m The address of the modulus related to \p A.
118 * \param[out] output The output buffer to export to.
119 * \param output_length The length in bytes of \p output.
Gabor Mezei37b06362022-08-02 17:22:18 +0200120 *
121 * \return \c 0 if successful.
Janos Follathb7a88ec2022-08-19 12:24:40 +0100122 * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p output isn't
123 * large enough to hold the value of \p A.
Janos Follath8ff07292022-08-08 08:39:52 +0100124 * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if the external representation
Gabor Mezei37b06362022-08-02 17:22:18 +0200125 * of \p m is invalid.
126 */
Janos Follathb7a88ec2022-08-19 12:24:40 +0100127int mbedtls_mpi_mod_raw_write( const mbedtls_mpi_uint *A,
Janos Follath6b8a4ad2022-08-19 10:58:34 +0100128 const mbedtls_mpi_mod_modulus *m,
Janos Follathb7a88ec2022-08-19 12:24:40 +0100129 unsigned char *output,
130 size_t output_length );
Gabor Mezeic5328cf2022-07-18 23:13:13 +0200131
Janos Follath5005edb2022-07-19 12:45:13 +0100132#endif /* MBEDTLS_BIGNUM_MOD_RAW_H */