blob: 8266cece5388dc02efc61992a4ac8b5e14a432f0 [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 *
36 * \param x Integer to count leading zero bits.
37 *
Janos Follath8ff07292022-08-08 08:39:52 +010038 * \return The number of leading zero bits in \p x.
Gabor Mezei37b06362022-08-02 17:22:18 +020039 */
Janos Follath4670f882022-07-21 18:25:42 +010040size_t mbedtls_mpi_core_clz( const mbedtls_mpi_uint x );
41
Janos Follath63184682022-08-11 17:42:59 +010042/** Return the the minimum number of bits required to represent the value held
43 * in the MPI.
44 *
45 * \note This function returns 0 if all the limbs of \p X are 0.
Gabor Mezei37b06362022-08-02 17:22:18 +020046 *
Janos Follath6b8a4ad2022-08-19 10:58:34 +010047 * \param[in] X The address of the MPI.
48 * \param nx The number of limbs of \p X.
Gabor Mezei37b06362022-08-02 17:22:18 +020049 *
Janos Follath8ff07292022-08-08 08:39:52 +010050 * \return The number of bits in \p X.
Gabor Mezei37b06362022-08-02 17:22:18 +020051 */
Janos Follath4670f882022-07-21 18:25:42 +010052size_t mbedtls_mpi_core_bitlen( const mbedtls_mpi_uint *X, size_t nx );
53
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 Follath6b8a4ad2022-08-19 10:58:34 +010057 * \param[in,out] X The address of the MPI.
58 * \param limbs The number of limbs of \p X.
Gabor Mezei37b06362022-08-02 17:22:18 +020059 */
Janos Follath6b8a4ad2022-08-19 10:58:34 +010060void mbedtls_mpi_core_bigendian_to_host( mbedtls_mpi_uint *X,
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 Follath6b8a4ad2022-08-19 10:58:34 +010068 * \param[out] X The address of the MPI.
69 * \param nx The number of limbs of \p X.
70 * \param[in] buf The input buffer to import from.
71 * \param buflen The length in bytes of \p buf.
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
75 * large enough to hold the value in \p buf.
76 */
Gabor Mezeib9030702022-07-18 23:09:45 +020077int mbedtls_mpi_core_read_le( mbedtls_mpi_uint *X,
78 size_t nx,
79 const unsigned char *buf,
80 size_t buflen );
81
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 Follath6b8a4ad2022-08-19 10:58:34 +010087 * \param[out] X The address of the MPI.
88 * May only be #NULL if \nx is 0 and \p buflen is 0.
89 * \param nx The number of limbs of \p X.
90 * \param[in] buf The input buffer to import from.
91 * May only be #NULL if \p buflen is 0.
92 * \param buflen The length in bytes of \p buf.
Gabor Mezei37b06362022-08-02 17:22:18 +020093 *
94 * \return \c 0 if successful.
95 * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p X isn't
96 * large enough to hold the value in \p buf.
97 */
Gabor Mezeib9030702022-07-18 23:09:45 +020098int mbedtls_mpi_core_read_be( mbedtls_mpi_uint *X,
99 size_t nx,
100 const unsigned char *buf,
101 size_t buflen );
102
Gabor Mezei37b06362022-08-02 17:22:18 +0200103/** Export X into unsigned binary data, little endian.
104 *
Janos Follath63184682022-08-11 17:42:59 +0100105 * \note If \p buf is shorter than \p X the export is still successful if the
106 * value held in \p X fits in the buffer (that is, if enough of the most
107 * significant bytes of \p X are 0).
108 *
Janos Follath6b8a4ad2022-08-19 10:58:34 +0100109 * \param[in] X The address of the MPI.
110 * \param nx The number of limbs of \p X.
111 * \param[out] buf The output buffer to export to.
112 * \param buflen The length in bytes of \p buf.
Gabor Mezei37b06362022-08-02 17:22:18 +0200113 *
114 * \return \c 0 if successful.
115 * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p buf isn't
116 * large enough to hold the value of \p X.
117 */
Gabor Mezeib9030702022-07-18 23:09:45 +0200118int mbedtls_mpi_core_write_le( const mbedtls_mpi_uint *X,
119 size_t nx,
120 unsigned char *buf,
121 size_t buflen );
122
Gabor Mezei37b06362022-08-02 17:22:18 +0200123/** Export X into unsigned binary data, big endian.
124 *
Janos Follath63184682022-08-11 17:42:59 +0100125 * \note If \p buf is shorter than \p X the export is still successful if the
126 * value held in \p X fits in the buffer (that is, if enough of the most
127 * significant bytes of \p X are 0).
128 *
Janos Follath6b8a4ad2022-08-19 10:58:34 +0100129 * \param[in] X The address of the MPI.
130 * \param nx The number of limbs of \p X.
131 * \param[out] buf The output buffer to export to.
132 * \param buflen The length in bytes of \p buf.
Gabor Mezei37b06362022-08-02 17:22:18 +0200133 *
134 * \return \c 0 if successful.
135 * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p buf isn't
136 * large enough to hold the value of \p X.
137 */
Gabor Mezeib9030702022-07-18 23:09:45 +0200138int mbedtls_mpi_core_write_be( const mbedtls_mpi_uint *X,
139 size_t nx,
140 unsigned char *buf,
141 size_t buflen );
142
Janos Follathd0895702022-08-10 13:32:16 +0100143#define ciL (sizeof(mbedtls_mpi_uint)) /* chars in limb */
144#define biL (ciL << 3) /* bits in limb */
145#define biH (ciL << 2) /* half limb size */
146
147/*
148 * Convert between bits/chars and number of limbs
149 * Divide first in order to avoid potential overflows
150 */
151#define BITS_TO_LIMBS(i) ( (i) / biL + ( (i) % biL != 0 ) )
152#define CHARS_TO_LIMBS(i) ( (i) / ciL + ( (i) % ciL != 0 ) )
153/* Get a specific byte, without range checks. */
154#define GET_BYTE( X, i ) \
155 ( ( ( X )[( i ) / ciL] >> ( ( ( i ) % ciL ) * 8 ) ) & 0xff )
156
Gabor Mezeib9030702022-07-18 23:09:45 +0200157#endif /* MBEDTLS_BIGNUM_CORE_H */