blob: 376a267dede374c2325b284cbc9ea5ef58119dc7 [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
Janos Follath4670f882022-07-21 18:25:42 +010045size_t mbedtls_mpi_core_clz( const mbedtls_mpi_uint x );
46
47size_t mbedtls_mpi_core_bitlen( const mbedtls_mpi_uint *X, size_t nx );
48
49void mbedtls_mpi_core_bigendian_to_host( mbedtls_mpi_uint * const X,
50 size_t limbs );
51
Gabor Mezeib9030702022-07-18 23:09:45 +020052int mbedtls_mpi_core_read_le( mbedtls_mpi_uint *X,
53 size_t nx,
54 const unsigned char *buf,
55 size_t buflen );
56
57int mbedtls_mpi_core_read_be( mbedtls_mpi_uint *X,
58 size_t nx,
59 const unsigned char *buf,
60 size_t buflen );
61
62int mbedtls_mpi_core_write_le( const mbedtls_mpi_uint *X,
63 size_t nx,
64 unsigned char *buf,
65 size_t buflen );
66
67int mbedtls_mpi_core_write_be( const mbedtls_mpi_uint *X,
68 size_t nx,
69 unsigned char *buf,
70 size_t buflen );
71
72#endif /* MBEDTLS_BIGNUM_CORE_H */