blob: 98a8c97c234e78372bdc606109927ae7279a3d7c [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
Janos Follath4614b9a2022-07-21 15:34:47 +010029#define MPI_VALIDATE_RET( cond ) \
30 MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_MPI_BAD_INPUT_DATA )
31#define MPI_VALIDATE( cond ) \
32 MBEDTLS_INTERNAL_VALIDATE( cond )
33
34#define ciL (sizeof(mbedtls_mpi_uint)) /* chars in limb */
35#define biL (ciL << 3) /* bits in limb */
36#define biH (ciL << 2) /* half limb size */
37
38/*
39 * Convert between bits/chars and number of limbs
40 * Divide first in order to avoid potential overflows
41 */
42#define BITS_TO_LIMBS(i) ( (i) / biL + ( (i) % biL != 0 ) )
43#define CHARS_TO_LIMBS(i) ( (i) / ciL + ( (i) % ciL != 0 ) )
44
Gabor Mezei37b06362022-08-02 17:22:18 +020045/** Count leading zero bits in a given integer.
46 *
47 * \param x Integer to count leading zero bits.
48 *
49 * \return Tne munber of leading zero bits in \p x.
50 */
Janos Follath4670f882022-07-21 18:25:42 +010051size_t mbedtls_mpi_core_clz( const mbedtls_mpi_uint x );
52
Gabor Mezei37b06362022-08-02 17:22:18 +020053/** Return the number of bits of an MPI.
54 *
55 * \param X The address of the MPI.
56 * \param nx The number of limbs of \p X.
57 *
58 * \return Tne number of bits in \p X.
59 */
Janos Follath4670f882022-07-21 18:25:42 +010060size_t mbedtls_mpi_core_bitlen( const mbedtls_mpi_uint *X, size_t nx );
61
Gabor Mezei37b06362022-08-02 17:22:18 +020062/** Convert a big-endian byte array aligned to the size of mbedtls_mpi_uint
63 * into the storage form used by mbedtls_mpi.
64 *
65 * \param X The address of the MPI.
66 * \param limbs The number of limbs of \p X.
67 */
Janos Follath4670f882022-07-21 18:25:42 +010068void mbedtls_mpi_core_bigendian_to_host( mbedtls_mpi_uint * const X,
69 size_t limbs );
70
Gabor Mezei37b06362022-08-02 17:22:18 +020071/** Import X from unsigned binary data, little endian.
72 *
73 * This function is guaranteed to return an MPI with at least the necessary
74 * number of limbs (in particular, it does not skip 0s in the input).
75 *
76 * \param X The address of the MPI.
77 * \param nx The number of limbs of \p X.
78 * \param buf The input buffer to import from.
79 * \param buflen Tne length in bytes of \p buf.
80 *
81 * \return \c 0 if successful.
82 * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p X isn't
83 * large enough to hold the value in \p buf.
84 */
Gabor Mezeib9030702022-07-18 23:09:45 +020085int mbedtls_mpi_core_read_le( mbedtls_mpi_uint *X,
86 size_t nx,
87 const unsigned char *buf,
88 size_t buflen );
89
Gabor Mezei37b06362022-08-02 17:22:18 +020090/** Import X from unsigned binary data, big endian.
91 *
92 * This function is guaranteed to return an MPI with exactly the necessary
93 * number of limbs (in particular, it does not skip 0s in the input).
94 *
95 * \param X The address of the MPI.
96 * \param nx The number of limbs of \p X.
97 * \param buf The input buffer to import from.
98 * \param buflen Tne length in bytes of \p buf.
99 *
100 * \return \c 0 if successful.
101 * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p X isn't
102 * large enough to hold the value in \p buf.
103 */
Gabor Mezeib9030702022-07-18 23:09:45 +0200104int mbedtls_mpi_core_read_be( mbedtls_mpi_uint *X,
105 size_t nx,
106 const unsigned char *buf,
107 size_t buflen );
108
Gabor Mezei37b06362022-08-02 17:22:18 +0200109/** Export X into unsigned binary data, little endian.
110 *
111 * \param X The address of the MPI.
112 * \param nx The number of limbs of \p X.
113 * \param buf The output buffer to import.
114 * \param buflen Tne length in bytes of \p buf.
115 *
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_le( const mbedtls_mpi_uint *X,
121 size_t nx,
122 unsigned char *buf,
123 size_t buflen );
124
Gabor Mezei37b06362022-08-02 17:22:18 +0200125/** Export X into unsigned binary data, big endian.
126 *
127 * \param X The address of the MPI.
128 * \param nx The number of limbs of \p X.
129 * \param buf The output buffer to import.
130 * \param buflen Tne length in bytes of \p buf.
131 *
132 * \return \c 0 if successful.
133 * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p buf isn't
134 * large enough to hold the value of \p X.
135 */
Gabor Mezeib9030702022-07-18 23:09:45 +0200136int mbedtls_mpi_core_write_be( const mbedtls_mpi_uint *X,
137 size_t nx,
138 unsigned char *buf,
139 size_t buflen );
140
141#endif /* MBEDTLS_BIGNUM_CORE_H */