Gabor Mezei | b903070 | 2022-07-18 23:09:45 +0200 | [diff] [blame] | 1 | /** |
Janos Follath | 6318468 | 2022-08-11 17:42:59 +0100 | [diff] [blame] | 2 | * Core bignum functions |
| 3 | * |
| 4 | * This interface only should be used by the legacy bignum module (bignum.h) |
| 5 | * and the modular bignum modules (bignum_mod.c, bignum_mod_raw.c). All other |
| 6 | * modules should use the high level modular bignum interface (bignum_mod.h) |
| 7 | * or the legacy bignum interface (bignum.h). |
Gabor Mezei | b903070 | 2022-07-18 23:09:45 +0200 | [diff] [blame] | 8 | * |
| 9 | * Copyright The Mbed TLS Contributors |
| 10 | * SPDX-License-Identifier: Apache-2.0 |
| 11 | * |
| 12 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 13 | * not use this file except in compliance with the License. |
| 14 | * You may obtain a copy of the License at |
| 15 | * |
| 16 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 17 | * |
| 18 | * Unless required by applicable law or agreed to in writing, software |
| 19 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 20 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 21 | * See the License for the specific language governing permissions and |
| 22 | * limitations under the License. |
| 23 | */ |
| 24 | |
| 25 | #ifndef MBEDTLS_BIGNUM_CORE_H |
| 26 | #define MBEDTLS_BIGNUM_CORE_H |
| 27 | |
| 28 | #include "common.h" |
| 29 | |
| 30 | #if defined(MBEDTLS_BIGNUM_C) |
| 31 | #include "mbedtls/bignum.h" |
| 32 | #endif |
| 33 | |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 34 | /** Count leading zero bits in a given integer. |
| 35 | * |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 36 | * \param a Integer to count leading zero bits. |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 37 | * |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 38 | * \return The number of leading zero bits in \p a. |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 39 | */ |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 40 | size_t mbedtls_mpi_core_clz( const mbedtls_mpi_uint a ); |
Janos Follath | 4670f88 | 2022-07-21 18:25:42 +0100 | [diff] [blame] | 41 | |
Janos Follath | a95f204 | 2022-08-19 12:09:17 +0100 | [diff] [blame] | 42 | /** Return the minimum number of bits required to represent the value held |
Janos Follath | 6318468 | 2022-08-11 17:42:59 +0100 | [diff] [blame] | 43 | * in the MPI. |
| 44 | * |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 45 | * \note This function returns 0 if all the limbs of \p A are 0. |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 46 | * |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 47 | * \param[in] A The address of the MPI. |
| 48 | * \param a_limbs The number of limbs of \p A. |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 49 | * |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 50 | * \return The number of bits in \p A. |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 51 | */ |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 52 | size_t mbedtls_mpi_core_bitlen( const mbedtls_mpi_uint *A, size_t a_limbs ); |
Janos Follath | 4670f88 | 2022-07-21 18:25:42 +0100 | [diff] [blame] | 53 | |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 54 | /** Convert a big-endian byte array aligned to the size of mbedtls_mpi_uint |
| 55 | * into the storage form used by mbedtls_mpi. |
| 56 | * |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 57 | * \param[in,out] A The address of the MPI. |
| 58 | * \param limbs The number of limbs of \p A. |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 59 | */ |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 60 | void mbedtls_mpi_core_bigendian_to_host( mbedtls_mpi_uint *A, |
Janos Follath | 4670f88 | 2022-07-21 18:25:42 +0100 | [diff] [blame] | 61 | size_t limbs ); |
| 62 | |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 63 | /** Import X from unsigned binary data, little endian. |
| 64 | * |
Janos Follath | 6318468 | 2022-08-11 17:42:59 +0100 | [diff] [blame] | 65 | * The MPI needs to have enough limbs to store the full value (including any |
| 66 | * most significant zero bytes in the input). |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 67 | * |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 68 | * \param[out] X The address of the MPI. |
| 69 | * \param X_limbs The number of limbs of \p X. |
| 70 | * \param[in] input The input buffer to import from. |
| 71 | * \param input_length The length bytes of \p input. |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 72 | * |
| 73 | * \return \c 0 if successful. |
| 74 | * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p X isn't |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 75 | * large enough to hold the value in \p input. |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 76 | */ |
Gabor Mezei | b903070 | 2022-07-18 23:09:45 +0200 | [diff] [blame] | 77 | int mbedtls_mpi_core_read_le( mbedtls_mpi_uint *X, |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 78 | size_t X_limbs, |
| 79 | const unsigned char *input, |
| 80 | size_t input_length ); |
Gabor Mezei | b903070 | 2022-07-18 23:09:45 +0200 | [diff] [blame] | 81 | |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 82 | /** Import X from unsigned binary data, big endian. |
| 83 | * |
Janos Follath | 6318468 | 2022-08-11 17:42:59 +0100 | [diff] [blame] | 84 | * The MPI needs to have enough limbs to store the full value (including any |
| 85 | * most significant zero bytes in the input). |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 86 | * |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 87 | * \param[out] X The address of the MPI. |
| 88 | * May only be #NULL if \X_limbs is 0 and \p input_length |
| 89 | * is 0. |
| 90 | * \param X_limbs The number of limbs of \p X. |
| 91 | * \param[in] input The input buffer to import from. |
| 92 | * May only be #NULL if \p input_length is 0. |
| 93 | * \param input_length The length in bytes of \p input. |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 94 | * |
| 95 | * \return \c 0 if successful. |
| 96 | * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p X isn't |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 97 | * large enough to hold the value in \p input. |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 98 | */ |
Gabor Mezei | b903070 | 2022-07-18 23:09:45 +0200 | [diff] [blame] | 99 | int mbedtls_mpi_core_read_be( mbedtls_mpi_uint *X, |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 100 | size_t X_limbs, |
| 101 | const unsigned char *input, |
| 102 | size_t input_length ); |
Gabor Mezei | b903070 | 2022-07-18 23:09:45 +0200 | [diff] [blame] | 103 | |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 104 | /** Export A into unsigned binary data, little endian. |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 105 | * |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 106 | * \note If \p output is shorter than \p A the export is still successful if the |
| 107 | * value held in \p A fits in the buffer (that is, if enough of the most |
| 108 | * significant bytes of \p A are 0). |
Janos Follath | 6318468 | 2022-08-11 17:42:59 +0100 | [diff] [blame] | 109 | * |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 110 | * \param[in] A The address of the MPI. |
| 111 | * \param A_limbs The number of limbs of \p A. |
| 112 | * \param[out] output The output buffer to export to. |
| 113 | * \param output_length The length in bytes of \p output. |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 114 | * |
| 115 | * \return \c 0 if successful. |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 116 | * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p output isn't |
| 117 | * large enough to hold the value of \p A. |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 118 | */ |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 119 | int mbedtls_mpi_core_write_le( const mbedtls_mpi_uint *A, |
| 120 | size_t A_limbs, |
| 121 | unsigned char *output, |
| 122 | size_t output_length ); |
Gabor Mezei | b903070 | 2022-07-18 23:09:45 +0200 | [diff] [blame] | 123 | |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 124 | /** Export A into unsigned binary data, big endian. |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 125 | * |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 126 | * \note If \p output is shorter than \p A the export is still successful if the |
| 127 | * value held in \p A fits in the buffer (that is, if enough of the most |
| 128 | * significant bytes of \p A are 0). |
Janos Follath | 6318468 | 2022-08-11 17:42:59 +0100 | [diff] [blame] | 129 | * |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 130 | * \param[in] A The address of the MPI. |
| 131 | * \param A_limbs The number of limbs of \p A. |
| 132 | * \param[out] output The output buffer to export to. |
| 133 | * \param output_length The length in bytes of \p output. |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 134 | * |
| 135 | * \return \c 0 if successful. |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 136 | * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p output isn't |
| 137 | * large enough to hold the value of \p A. |
Gabor Mezei | 37b0636 | 2022-08-02 17:22:18 +0200 | [diff] [blame] | 138 | */ |
Janos Follath | b7a88ec | 2022-08-19 12:24:40 +0100 | [diff] [blame] | 139 | int mbedtls_mpi_core_write_be( const mbedtls_mpi_uint *A, |
| 140 | size_t A_limbs, |
| 141 | unsigned char *output, |
| 142 | size_t output_length ); |
Gabor Mezei | b903070 | 2022-07-18 23:09:45 +0200 | [diff] [blame] | 143 | |
Janos Follath | ca5688e | 2022-08-19 12:05:28 +0100 | [diff] [blame] | 144 | #define ciL ( sizeof(mbedtls_mpi_uint) ) /* chars in limb */ |
| 145 | #define biL ( ciL << 3 ) /* bits in limb */ |
| 146 | #define biH ( ciL << 2 ) /* half limb size */ |
Janos Follath | d089570 | 2022-08-10 13:32:16 +0100 | [diff] [blame] | 147 | |
| 148 | /* |
| 149 | * Convert between bits/chars and number of limbs |
| 150 | * Divide first in order to avoid potential overflows |
| 151 | */ |
| 152 | #define BITS_TO_LIMBS(i) ( (i) / biL + ( (i) % biL != 0 ) ) |
| 153 | #define CHARS_TO_LIMBS(i) ( (i) / ciL + ( (i) % ciL != 0 ) ) |
| 154 | /* Get a specific byte, without range checks. */ |
| 155 | #define GET_BYTE( X, i ) \ |
Janos Follath | ca5688e | 2022-08-19 12:05:28 +0100 | [diff] [blame] | 156 | ( ( (X)[(i) / ciL] >> ( ( (i) % ciL ) * 8 ) ) & 0xff ) |
Janos Follath | d089570 | 2022-08-10 13:32:16 +0100 | [diff] [blame] | 157 | |
Gabor Mezei | b903070 | 2022-07-18 23:09:45 +0200 | [diff] [blame] | 158 | #endif /* MBEDTLS_BIGNUM_CORE_H */ |