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 | |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 29 | /** Count leading zero bits in a given integer. |
| 30 | * |
| 31 | * \param x Integer to count leading zero bits. |
| 32 | * |
Janos Follath | 8ff0729 | 2022-08-08 08:39:52 +0100 | [diff] [blame] | 33 | * \return The number of leading zero bits in \p x. |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 34 | */ |
Janos Follath | 4670f88 | 2022-07-21 18:25:42 +0100 | [diff] [blame] | 35 | size_t mbedtls_mpi_core_clz( const mbedtls_mpi_uint x ); |
| 36 | |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 37 | /** 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 Follath | 8ff0729 | 2022-08-08 08:39:52 +0100 | [diff] [blame] | 42 | * \return The number of bits in \p X. |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 43 | */ |
Janos Follath | 4670f88 | 2022-07-21 18:25:42 +0100 | [diff] [blame] | 44 | size_t mbedtls_mpi_core_bitlen( const mbedtls_mpi_uint *X, size_t nx ); |
| 45 | |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 46 | /** 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 Follath | 4670f88 | 2022-07-21 18:25:42 +0100 | [diff] [blame] | 52 | void mbedtls_mpi_core_bigendian_to_host( mbedtls_mpi_uint * const X, |
| 53 | size_t limbs ); |
| 54 | |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 55 | /** Import X from unsigned binary data, little endian. |
| 56 | * |
Janos Follath | dae1147 | 2022-08-08 11:50:02 +0100 | [diff] [blame] | 57 | * 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 Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 59 | * |
| 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 Follath | 8ff0729 | 2022-08-08 08:39:52 +0100 | [diff] [blame] | 63 | * \param buflen The length in bytes of \p buf. |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 64 | * |
| 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 Mezei | b903070 | 2022-07-18 23:09:45 +0200 | [diff] [blame] | 69 | int mbedtls_mpi_core_read_le( mbedtls_mpi_uint *X, |
| 70 | size_t nx, |
| 71 | const unsigned char *buf, |
| 72 | size_t buflen ); |
| 73 | |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 74 | /** Import X from unsigned binary data, big endian. |
| 75 | * |
Janos Follath | dae1147 | 2022-08-08 11:50:02 +0100 | [diff] [blame] | 76 | * 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 Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 78 | * |
| 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 Follath | 8ff0729 | 2022-08-08 08:39:52 +0100 | [diff] [blame] | 82 | * \param buflen The length in bytes of \p buf. |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 83 | * |
| 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 Mezei | b903070 | 2022-07-18 23:09:45 +0200 | [diff] [blame] | 88 | int mbedtls_mpi_core_read_be( mbedtls_mpi_uint *X, |
| 89 | size_t nx, |
| 90 | const unsigned char *buf, |
| 91 | size_t buflen ); |
| 92 | |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 93 | /** 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 Follath | dae1147 | 2022-08-08 11:50:02 +0100 | [diff] [blame] | 97 | * \param buf The output buffer to export to. |
Janos Follath | 8ff0729 | 2022-08-08 08:39:52 +0100 | [diff] [blame] | 98 | * \param buflen The length in bytes of \p buf. |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 99 | * |
| 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 Mezei | b903070 | 2022-07-18 23:09:45 +0200 | [diff] [blame] | 104 | int mbedtls_mpi_core_write_le( const mbedtls_mpi_uint *X, |
| 105 | size_t nx, |
| 106 | unsigned char *buf, |
| 107 | size_t buflen ); |
| 108 | |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 109 | /** 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 Follath | dae1147 | 2022-08-08 11:50:02 +0100 | [diff] [blame] | 113 | * \param buf The output buffer to export to. |
Janos Follath | 8ff0729 | 2022-08-08 08:39:52 +0100 | [diff] [blame] | 114 | * \param buflen The length in bytes of \p buf. |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 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 Mezei | b903070 | 2022-07-18 23:09:45 +0200 | [diff] [blame] | 120 | int mbedtls_mpi_core_write_be( const mbedtls_mpi_uint *X, |
| 121 | size_t nx, |
| 122 | unsigned char *buf, |
| 123 | size_t buflen ); |
| 124 | |
Janos Follath | d089570 | 2022-08-10 13:32:16 +0100 | [diff] [blame^] | 125 | #define ciL (sizeof(mbedtls_mpi_uint)) /* chars in limb */ |
| 126 | #define biL (ciL << 3) /* bits in limb */ |
| 127 | #define biH (ciL << 2) /* half limb size */ |
| 128 | |
| 129 | /* |
| 130 | * Convert between bits/chars and number of limbs |
| 131 | * Divide first in order to avoid potential overflows |
| 132 | */ |
| 133 | #define BITS_TO_LIMBS(i) ( (i) / biL + ( (i) % biL != 0 ) ) |
| 134 | #define CHARS_TO_LIMBS(i) ( (i) / ciL + ( (i) % ciL != 0 ) ) |
| 135 | /* Get a specific byte, without range checks. */ |
| 136 | #define GET_BYTE( X, i ) \ |
| 137 | ( ( ( X )[( i ) / ciL] >> ( ( ( i ) % ciL ) * 8 ) ) & 0xff ) |
| 138 | |
Gabor Mezei | b903070 | 2022-07-18 23:09:45 +0200 | [diff] [blame] | 139 | #endif /* MBEDTLS_BIGNUM_CORE_H */ |