blob: 2141640cfd24f17329004d025ca6395fcb7def92 [file] [log] [blame]
Gabor Mezeib9030702022-07-18 23:09:45 +02001/**
2 * Internal bignum functions
3 *
4 * Copyright The Mbed TLS Contributors
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19
20#ifndef MBEDTLS_BIGNUM_CORE_H
21#define MBEDTLS_BIGNUM_CORE_H
22
23#include "common.h"
24
25#if defined(MBEDTLS_BIGNUM_C)
26#include "mbedtls/bignum.h"
27#endif
28
Gabor Mezei37b06362022-08-02 17:22:18 +020029/** Count leading zero bits in a given integer.
30 *
31 * \param x Integer to count leading zero bits.
32 *
Janos Follath8ff07292022-08-08 08:39:52 +010033 * \return The number of leading zero bits in \p x.
Gabor Mezei37b06362022-08-02 17:22:18 +020034 */
Janos Follath4670f882022-07-21 18:25:42 +010035size_t mbedtls_mpi_core_clz( const mbedtls_mpi_uint x );
36
Gabor Mezei37b06362022-08-02 17:22:18 +020037/** Return the number of bits of an MPI.
38 *
39 * \param X The address of the MPI.
40 * \param nx The number of limbs of \p X.
41 *
Janos Follath8ff07292022-08-08 08:39:52 +010042 * \return The number of bits in \p X.
Gabor Mezei37b06362022-08-02 17:22:18 +020043 */
Janos Follath4670f882022-07-21 18:25:42 +010044size_t mbedtls_mpi_core_bitlen( const mbedtls_mpi_uint *X, size_t nx );
45
Gabor Mezei37b06362022-08-02 17:22:18 +020046/** Convert a big-endian byte array aligned to the size of mbedtls_mpi_uint
47 * into the storage form used by mbedtls_mpi.
48 *
49 * \param X The address of the MPI.
50 * \param limbs The number of limbs of \p X.
51 */
Janos Follath4670f882022-07-21 18:25:42 +010052void mbedtls_mpi_core_bigendian_to_host( mbedtls_mpi_uint * const X,
53 size_t limbs );
54
Gabor Mezei37b06362022-08-02 17:22:18 +020055/** Import X from unsigned binary data, little endian.
56 *
Janos Follathdae11472022-08-08 11:50:02 +010057 * The MPI needs to have enough limbs to store the full value (in particular,
58 * this function does not skip 0s in the input).
Gabor Mezei37b06362022-08-02 17:22:18 +020059 *
60 * \param X The address of the MPI.
61 * \param nx The number of limbs of \p X.
62 * \param buf The input buffer to import from.
Janos Follath8ff07292022-08-08 08:39:52 +010063 * \param buflen The length in bytes of \p buf.
Gabor Mezei37b06362022-08-02 17:22:18 +020064 *
65 * \return \c 0 if successful.
66 * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p X isn't
67 * large enough to hold the value in \p buf.
68 */
Gabor Mezeib9030702022-07-18 23:09:45 +020069int mbedtls_mpi_core_read_le( mbedtls_mpi_uint *X,
70 size_t nx,
71 const unsigned char *buf,
72 size_t buflen );
73
Gabor Mezei37b06362022-08-02 17:22:18 +020074/** Import X from unsigned binary data, big endian.
75 *
Janos Follathdae11472022-08-08 11:50:02 +010076 * The MPI needs to have enough limbs to store the full value (in particular,
77 * this function does not skip 0s in the input).
Gabor Mezei37b06362022-08-02 17:22:18 +020078 *
79 * \param X The address of the MPI.
80 * \param nx The number of limbs of \p X.
81 * \param buf The input buffer to import from.
Janos Follath8ff07292022-08-08 08:39:52 +010082 * \param buflen The length in bytes of \p buf.
Gabor Mezei37b06362022-08-02 17:22:18 +020083 *
84 * \return \c 0 if successful.
85 * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p X isn't
86 * large enough to hold the value in \p buf.
87 */
Gabor Mezeib9030702022-07-18 23:09:45 +020088int mbedtls_mpi_core_read_be( mbedtls_mpi_uint *X,
89 size_t nx,
90 const unsigned char *buf,
91 size_t buflen );
92
Gabor Mezei37b06362022-08-02 17:22:18 +020093/** Export X into unsigned binary data, little endian.
94 *
95 * \param X The address of the MPI.
96 * \param nx The number of limbs of \p X.
Janos Follathdae11472022-08-08 11:50:02 +010097 * \param buf The output buffer to export to.
Janos Follath8ff07292022-08-08 08:39:52 +010098 * \param buflen The length in bytes of \p buf.
Gabor Mezei37b06362022-08-02 17:22:18 +020099 *
100 * \return \c 0 if successful.
101 * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p buf isn't
102 * large enough to hold the value of \p X.
103 */
Gabor Mezeib9030702022-07-18 23:09:45 +0200104int mbedtls_mpi_core_write_le( const mbedtls_mpi_uint *X,
105 size_t nx,
106 unsigned char *buf,
107 size_t buflen );
108
Gabor Mezei37b06362022-08-02 17:22:18 +0200109/** Export X into unsigned binary data, big endian.
110 *
111 * \param X The address of the MPI.
112 * \param nx The number of limbs of \p X.
Janos Follathdae11472022-08-08 11:50:02 +0100113 * \param buf The output buffer to export to.
Janos Follath8ff07292022-08-08 08:39:52 +0100114 * \param buflen The length in bytes of \p buf.
Gabor Mezei37b06362022-08-02 17:22:18 +0200115 *
116 * \return \c 0 if successful.
117 * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p buf isn't
118 * large enough to hold the value of \p X.
119 */
Gabor Mezeib9030702022-07-18 23:09:45 +0200120int mbedtls_mpi_core_write_be( const mbedtls_mpi_uint *X,
121 size_t nx,
122 unsigned char *buf,
123 size_t buflen );
124
125#endif /* MBEDTLS_BIGNUM_CORE_H */