Gabor Mezei | b903070 | 2022-07-18 23:09:45 +0200 | [diff] [blame] | 1 | /** |
| 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 Follath | 4614b9a | 2022-07-21 15:34:47 +0100 | [diff] [blame^] | 29 | #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 Mezei | b903070 | 2022-07-18 23:09:45 +0200 | [diff] [blame] | 45 | int mbedtls_mpi_core_read_le( mbedtls_mpi_uint *X, |
| 46 | size_t nx, |
| 47 | const unsigned char *buf, |
| 48 | size_t buflen ); |
| 49 | |
| 50 | int mbedtls_mpi_core_read_be( mbedtls_mpi_uint *X, |
| 51 | size_t nx, |
| 52 | const unsigned char *buf, |
| 53 | size_t buflen ); |
| 54 | |
| 55 | int mbedtls_mpi_core_write_le( const mbedtls_mpi_uint *X, |
| 56 | size_t nx, |
| 57 | unsigned char *buf, |
| 58 | size_t buflen ); |
| 59 | |
| 60 | int mbedtls_mpi_core_write_be( const mbedtls_mpi_uint *X, |
| 61 | size_t nx, |
| 62 | unsigned char *buf, |
| 63 | size_t buflen ); |
| 64 | |
| 65 | #endif /* MBEDTLS_BIGNUM_CORE_H */ |