blob: c75be260c113ccc7a367a3d344ec09519e82348f [file] [log] [blame]
Gabor Mezeib9030702022-07-18 23:09:45 +02001/**
Janos Follath63184682022-08-11 17:42:59 +01002 * Core bignum functions
3 *
4 * This interface only should be used by the legacy bignum module (bignum.h)
5 * and the modular bignum modules (bignum_mod.c, bignum_mod_raw.c). All other
6 * modules should use the high level modular bignum interface (bignum_mod.h)
7 * or the legacy bignum interface (bignum.h).
Gabor Mezeib9030702022-07-18 23:09:45 +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
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
Gabor Mezei37b06362022-08-02 17:22:18 +020034/** Count leading zero bits in a given integer.
35 *
Janos Follathb7a88ec2022-08-19 12:24:40 +010036 * \param a Integer to count leading zero bits.
Gabor Mezei37b06362022-08-02 17:22:18 +020037 *
Janos Follathb7a88ec2022-08-19 12:24:40 +010038 * \return The number of leading zero bits in \p a.
Gabor Mezei37b06362022-08-02 17:22:18 +020039 */
Janos Follathb7a88ec2022-08-19 12:24:40 +010040size_t mbedtls_mpi_core_clz( const mbedtls_mpi_uint a );
Janos Follath4670f882022-07-21 18:25:42 +010041
Janos Follatha95f2042022-08-19 12:09:17 +010042/** Return the minimum number of bits required to represent the value held
Janos Follath63184682022-08-11 17:42:59 +010043 * in the MPI.
44 *
Janos Follathb7a88ec2022-08-19 12:24:40 +010045 * \note This function returns 0 if all the limbs of \p A are 0.
Gabor Mezei37b06362022-08-02 17:22:18 +020046 *
Janos Follathb7a88ec2022-08-19 12:24:40 +010047 * \param[in] A The address of the MPI.
48 * \param a_limbs The number of limbs of \p A.
Gabor Mezei37b06362022-08-02 17:22:18 +020049 *
Janos Follathb7a88ec2022-08-19 12:24:40 +010050 * \return The number of bits in \p A.
Gabor Mezei37b06362022-08-02 17:22:18 +020051 */
Janos Follathb7a88ec2022-08-19 12:24:40 +010052size_t mbedtls_mpi_core_bitlen( const mbedtls_mpi_uint *A, size_t a_limbs );
Janos Follath4670f882022-07-21 18:25:42 +010053
Gabor Mezei37b06362022-08-02 17:22:18 +020054/** Convert a big-endian byte array aligned to the size of mbedtls_mpi_uint
55 * into the storage form used by mbedtls_mpi.
56 *
Janos Follathb7a88ec2022-08-19 12:24:40 +010057 * \param[in,out] A The address of the MPI.
58 * \param limbs The number of limbs of \p A.
Gabor Mezei37b06362022-08-02 17:22:18 +020059 */
Janos Follathb7a88ec2022-08-19 12:24:40 +010060void mbedtls_mpi_core_bigendian_to_host( mbedtls_mpi_uint *A,
Janos Follath4670f882022-07-21 18:25:42 +010061 size_t limbs );
62
Gabor Mezei37b06362022-08-02 17:22:18 +020063/** Import X from unsigned binary data, little endian.
64 *
Janos Follath63184682022-08-11 17:42:59 +010065 * The MPI needs to have enough limbs to store the full value (including any
66 * most significant zero bytes in the input).
Gabor Mezei37b06362022-08-02 17:22:18 +020067 *
Janos Follathb7a88ec2022-08-19 12:24:40 +010068 * \param[out] X The address of the MPI.
69 * \param X_limbs The number of limbs of \p X.
70 * \param[in] input The input buffer to import from.
71 * \param input_length The length bytes of \p input.
Gabor Mezei37b06362022-08-02 17:22:18 +020072 *
73 * \return \c 0 if successful.
74 * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p X isn't
Janos Follathb7a88ec2022-08-19 12:24:40 +010075 * large enough to hold the value in \p input.
Gabor Mezei37b06362022-08-02 17:22:18 +020076 */
Gabor Mezeib9030702022-07-18 23:09:45 +020077int mbedtls_mpi_core_read_le( mbedtls_mpi_uint *X,
Janos Follathb7a88ec2022-08-19 12:24:40 +010078 size_t X_limbs,
79 const unsigned char *input,
80 size_t input_length );
Gabor Mezeib9030702022-07-18 23:09:45 +020081
Gabor Mezei37b06362022-08-02 17:22:18 +020082/** Import X from unsigned binary data, big endian.
83 *
Janos Follath63184682022-08-11 17:42:59 +010084 * The MPI needs to have enough limbs to store the full value (including any
85 * most significant zero bytes in the input).
Gabor Mezei37b06362022-08-02 17:22:18 +020086 *
Janos Follathb7a88ec2022-08-19 12:24:40 +010087 * \param[out] X The address of the MPI.
88 * May only be #NULL if \X_limbs is 0 and \p input_length
89 * is 0.
90 * \param X_limbs The number of limbs of \p X.
91 * \param[in] input The input buffer to import from.
92 * May only be #NULL if \p input_length is 0.
93 * \param input_length The length in bytes of \p input.
Gabor Mezei37b06362022-08-02 17:22:18 +020094 *
95 * \return \c 0 if successful.
96 * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p X isn't
Janos Follathb7a88ec2022-08-19 12:24:40 +010097 * large enough to hold the value in \p input.
Gabor Mezei37b06362022-08-02 17:22:18 +020098 */
Gabor Mezeib9030702022-07-18 23:09:45 +020099int mbedtls_mpi_core_read_be( mbedtls_mpi_uint *X,
Janos Follathb7a88ec2022-08-19 12:24:40 +0100100 size_t X_limbs,
101 const unsigned char *input,
102 size_t input_length );
Gabor Mezeib9030702022-07-18 23:09:45 +0200103
Janos Follathb7a88ec2022-08-19 12:24:40 +0100104/** Export A into unsigned binary data, little endian.
Gabor Mezei37b06362022-08-02 17:22:18 +0200105 *
Janos Follathb7a88ec2022-08-19 12:24:40 +0100106 * \note If \p output is shorter than \p A the export is still successful if the
107 * value held in \p A fits in the buffer (that is, if enough of the most
108 * significant bytes of \p A are 0).
Janos Follath63184682022-08-11 17:42:59 +0100109 *
Janos Follathb7a88ec2022-08-19 12:24:40 +0100110 * \param[in] A The address of the MPI.
111 * \param A_limbs The number of limbs of \p A.
112 * \param[out] output The output buffer to export to.
113 * \param output_length The length in bytes of \p output.
Gabor Mezei37b06362022-08-02 17:22:18 +0200114 *
115 * \return \c 0 if successful.
Janos Follathb7a88ec2022-08-19 12:24:40 +0100116 * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p output isn't
117 * large enough to hold the value of \p A.
Gabor Mezei37b06362022-08-02 17:22:18 +0200118 */
Janos Follathb7a88ec2022-08-19 12:24:40 +0100119int mbedtls_mpi_core_write_le( const mbedtls_mpi_uint *A,
120 size_t A_limbs,
121 unsigned char *output,
122 size_t output_length );
Gabor Mezeib9030702022-07-18 23:09:45 +0200123
Janos Follathb7a88ec2022-08-19 12:24:40 +0100124/** Export A into unsigned binary data, big endian.
Gabor Mezei37b06362022-08-02 17:22:18 +0200125 *
Janos Follathb7a88ec2022-08-19 12:24:40 +0100126 * \note If \p output is shorter than \p A the export is still successful if the
127 * value held in \p A fits in the buffer (that is, if enough of the most
128 * significant bytes of \p A are 0).
Janos Follath63184682022-08-11 17:42:59 +0100129 *
Janos Follathb7a88ec2022-08-19 12:24:40 +0100130 * \param[in] A The address of the MPI.
131 * \param A_limbs The number of limbs of \p A.
132 * \param[out] output The output buffer to export to.
133 * \param output_length The length in bytes of \p output.
Gabor Mezei37b06362022-08-02 17:22:18 +0200134 *
135 * \return \c 0 if successful.
Janos Follathb7a88ec2022-08-19 12:24:40 +0100136 * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p output isn't
137 * large enough to hold the value of \p A.
Gabor Mezei37b06362022-08-02 17:22:18 +0200138 */
Janos Follathb7a88ec2022-08-19 12:24:40 +0100139int mbedtls_mpi_core_write_be( const mbedtls_mpi_uint *A,
140 size_t A_limbs,
141 unsigned char *output,
142 size_t output_length );
Gabor Mezeib9030702022-07-18 23:09:45 +0200143
Janos Follathca5688e2022-08-19 12:05:28 +0100144#define ciL ( sizeof(mbedtls_mpi_uint) ) /* chars in limb */
145#define biL ( ciL << 3 ) /* bits in limb */
146#define biH ( ciL << 2 ) /* half limb size */
Janos Follathd0895702022-08-10 13:32:16 +0100147
148/*
149 * Convert between bits/chars and number of limbs
150 * Divide first in order to avoid potential overflows
151 */
152#define BITS_TO_LIMBS(i) ( (i) / biL + ( (i) % biL != 0 ) )
153#define CHARS_TO_LIMBS(i) ( (i) / ciL + ( (i) % ciL != 0 ) )
154/* Get a specific byte, without range checks. */
155#define GET_BYTE( X, i ) \
Janos Follathca5688e2022-08-19 12:05:28 +0100156 ( ( (X)[(i) / ciL] >> ( ( (i) % ciL ) * 8 ) ) & 0xff )
Janos Follathd0895702022-08-10 13:32:16 +0100157
Gabor Mezeib9030702022-07-18 23:09:45 +0200158#endif /* MBEDTLS_BIGNUM_CORE_H */