blob: aef8a04e26d1b5125fd066fdfd78674e5a54c1ac [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).
Gilles Peskine7aab2fb2022-09-27 13:19:13 +02008 *
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.
Gilles Peskine7f887bd2022-09-27 13:12:30 +020014 */
15
16/*
Gabor Mezeic5328cf2022-07-18 23:13:13 +020017 * Copyright The Mbed TLS Contributors
18 * SPDX-License-Identifier: Apache-2.0
19 *
20 * Licensed under the Apache License, Version 2.0 (the "License"); you may
21 * not use this file except in compliance with the License.
22 * You may obtain a copy of the License at
23 *
24 * http://www.apache.org/licenses/LICENSE-2.0
25 *
26 * Unless required by applicable law or agreed to in writing, software
27 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
28 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
29 * See the License for the specific language governing permissions and
30 * limitations under the License.
31 */
32
Janos Follath5005edb2022-07-19 12:45:13 +010033#ifndef MBEDTLS_BIGNUM_MOD_RAW_H
34#define MBEDTLS_BIGNUM_MOD_RAW_H
Gabor Mezeic5328cf2022-07-18 23:13:13 +020035
36#include "common.h"
37
38#if defined(MBEDTLS_BIGNUM_C)
39#include "mbedtls/bignum.h"
40#endif
41
Janos Follath0ded6312022-08-09 13:34:54 +010042#include "bignum_mod.h"
43
Gabor Mezei37b06362022-08-02 17:22:18 +020044/** Import X from unsigned binary data.
45 *
Janos Follath63184682022-08-11 17:42:59 +010046 * The MPI needs to have enough limbs to store the full value (including any
47 * most significant zero bytes in the input).
Gabor Mezei37b06362022-08-02 17:22:18 +020048 *
Janos Follathb7a88ec2022-08-19 12:24:40 +010049 * \param[out] X The address of the MPI. The size is determined by \p m.
50 * (In particular, it must have at least as many limbs as
51 * the modulus \p m.)
52 * \param[in] m The address of the modulus related to \p X.
53 * \param[in] input The input buffer to import from.
54 * \param input_length The length in bytes of \p input.
Gabor Mezei37b06362022-08-02 17:22:18 +020055 *
56 * \return \c 0 if successful.
57 * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p X isn't
Janos Follathb7a88ec2022-08-19 12:24:40 +010058 * large enough to hold the value in \p input.
Janos Follath8ff07292022-08-08 08:39:52 +010059 * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if the external representation
Janos Follathdae11472022-08-08 11:50:02 +010060 * of \p m is invalid or \p X is not less than \p m.
Gabor Mezei37b06362022-08-02 17:22:18 +020061 */
Gabor Mezeic5328cf2022-07-18 23:13:13 +020062int mbedtls_mpi_mod_raw_read( mbedtls_mpi_uint *X,
Janos Follath6b8a4ad2022-08-19 10:58:34 +010063 const mbedtls_mpi_mod_modulus *m,
Janos Follathb7a88ec2022-08-19 12:24:40 +010064 const unsigned char *input,
65 size_t input_length );
Gabor Mezeic5328cf2022-07-18 23:13:13 +020066
Janos Follathb7a88ec2022-08-19 12:24:40 +010067/** Export A into unsigned binary data.
Gabor Mezei37b06362022-08-02 17:22:18 +020068 *
Janos Follathb7a88ec2022-08-19 12:24:40 +010069 * \param[in] A The address of the MPI. The size is determined by \p m.
70 * (In particular, it must have at least as many limbs as
71 * the modulus \p m.)
72 * \param[in] m The address of the modulus related to \p A.
73 * \param[out] output The output buffer to export to.
74 * \param output_length The length in bytes of \p output.
Gabor Mezei37b06362022-08-02 17:22:18 +020075 *
76 * \return \c 0 if successful.
Janos Follathb7a88ec2022-08-19 12:24:40 +010077 * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p output isn't
78 * large enough to hold the value of \p A.
Janos Follath8ff07292022-08-08 08:39:52 +010079 * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if the external representation
Gabor Mezei37b06362022-08-02 17:22:18 +020080 * of \p m is invalid.
81 */
Janos Follathb7a88ec2022-08-19 12:24:40 +010082int mbedtls_mpi_mod_raw_write( const mbedtls_mpi_uint *A,
Janos Follath6b8a4ad2022-08-19 10:58:34 +010083 const mbedtls_mpi_mod_modulus *m,
Janos Follathb7a88ec2022-08-19 12:24:40 +010084 unsigned char *output,
85 size_t output_length );
Gabor Mezeic5328cf2022-07-18 23:13:13 +020086
Janos Follath5005edb2022-07-19 12:45:13 +010087#endif /* MBEDTLS_BIGNUM_MOD_RAW_H */