Gilles Peskine | c4672fd | 2019-09-11 13:39:11 +0200 | [diff] [blame] | 1 | /** |
| 2 | * \file common.h |
| 3 | * |
| 4 | * \brief Utility macros for internal use in the library |
| 5 | */ |
| 6 | /* |
Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 7 | * Copyright The Mbed TLS Contributors |
Gilles Peskine | c4672fd | 2019-09-11 13:39:11 +0200 | [diff] [blame] | 8 | * SPDX-License-Identifier: Apache-2.0 |
| 9 | * |
| 10 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 11 | * not use this file except in compliance with the License. |
| 12 | * You may obtain a copy of the License at |
| 13 | * |
| 14 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 15 | * |
| 16 | * Unless required by applicable law or agreed to in writing, software |
| 17 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 18 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 19 | * See the License for the specific language governing permissions and |
| 20 | * limitations under the License. |
Gilles Peskine | c4672fd | 2019-09-11 13:39:11 +0200 | [diff] [blame] | 21 | */ |
| 22 | |
| 23 | #ifndef MBEDTLS_LIBRARY_COMMON_H |
| 24 | #define MBEDTLS_LIBRARY_COMMON_H |
| 25 | |
| 26 | #if defined(MBEDTLS_CONFIG_FILE) |
| 27 | #include MBEDTLS_CONFIG_FILE |
| 28 | #else |
| 29 | #include "mbedtls/config.h" |
| 30 | #endif |
| 31 | |
| 32 | /** Helper to define a function as static except when building invasive tests. |
| 33 | * |
| 34 | * If a function is only used inside its own source file and should be |
| 35 | * declared `static` to allow the compiler to optimize for code size, |
| 36 | * but that function has unit tests, define it with |
| 37 | * ``` |
| 38 | * MBEDTLS_STATIC_TESTABLE int mbedtls_foo(...) { ... } |
| 39 | * ``` |
| 40 | * and declare it in a header in the `library/` directory with |
| 41 | * ``` |
| 42 | * #if defined(MBEDTLS_TEST_HOOKS) |
| 43 | * int mbedtls_foo(...); |
| 44 | * #endif |
| 45 | * ``` |
| 46 | */ |
| 47 | #if defined(MBEDTLS_TEST_HOOKS) |
| 48 | #define MBEDTLS_STATIC_TESTABLE |
| 49 | #else |
| 50 | #define MBEDTLS_STATIC_TESTABLE static |
| 51 | #endif |
| 52 | |
Joe Subbiani | ba486b0 | 2021-06-22 15:51:53 +0100 | [diff] [blame] | 53 | /** Allow library to access its structs' private members. |
| 54 | * |
| 55 | * Although structs defined in header files are publicly available, |
| 56 | * their members are private and should not be accessed by the user. |
| 57 | */ |
| 58 | #define MBEDTLS_ALLOW_PRIVATE_ACCESS |
| 59 | |
| 60 | /** Byte Reading Macros |
Joe Subbiani | 61f7d73 | 2021-06-24 09:06:23 +0100 | [diff] [blame] | 61 | * |
Joe Subbiani | 266476d | 2021-07-07 15:16:56 +0100 | [diff] [blame] | 62 | * Obtain the most significant byte of x using 0xff |
| 63 | * Using MBEDTLS_BYTE_a will shift a*8 bits |
| 64 | * to retrieve the next byte of information |
Joe Subbiani | ba486b0 | 2021-06-22 15:51:53 +0100 | [diff] [blame] | 65 | */ |
Joe Subbiani | 2bbafda | 2021-06-24 13:00:03 +0100 | [diff] [blame] | 66 | #define MBEDTLS_BYTE_0( x ) ( (uint8_t) ( ( x ) & 0xff ) ) |
| 67 | #define MBEDTLS_BYTE_1( x ) ( (uint8_t) ( ( ( x ) >> 8 ) & 0xff ) ) |
| 68 | #define MBEDTLS_BYTE_2( x ) ( (uint8_t) ( ( ( x ) >> 16 ) & 0xff ) ) |
| 69 | #define MBEDTLS_BYTE_3( x ) ( (uint8_t) ( ( ( x ) >> 24 ) & 0xff ) ) |
Joe Subbiani | ba486b0 | 2021-06-22 15:51:53 +0100 | [diff] [blame] | 70 | |
Joe Subbiani | 6b897c9 | 2021-07-08 14:59:52 +0100 | [diff] [blame] | 71 | #define MBEDTLS_CHAR_0( x ) ( (unsigned char) ( ( x ) & 0xff ) ) |
| 72 | #define MBEDTLS_CHAR_1( x ) ( (unsigned char) ( ( ( x ) >> 8 ) & 0xff ) ) |
| 73 | #define MBEDTLS_CHAR_2( x ) ( (unsigned char) ( ( ( x ) >> 16 ) & 0xff ) ) |
| 74 | #define MBEDTLS_CHAR_3( x ) ( (unsigned char) ( ( ( x ) >> 24 ) & 0xff ) ) |
| 75 | #define MBEDTLS_CHAR_4( x ) ( (unsigned char) ( ( ( x ) >> 32 ) & 0xff ) ) |
| 76 | #define MBEDTLS_CHAR_5( x ) ( (unsigned char) ( ( ( x ) >> 40 ) & 0xff ) ) |
| 77 | #define MBEDTLS_CHAR_6( x ) ( (unsigned char) ( ( ( x ) >> 48 ) & 0xff ) ) |
| 78 | #define MBEDTLS_CHAR_7( x ) ( (unsigned char) ( ( ( x ) >> 56 ) & 0xff ) ) |
| 79 | |
Joe Subbiani | 266476d | 2021-07-07 15:16:56 +0100 | [diff] [blame] | 80 | /** |
Joe Subbiani | 0a65d53 | 2021-07-14 11:53:07 +0100 | [diff] [blame^] | 81 | * Get the unsigned 32 bits integer corresponding to four bytes in |
| 82 | * big-endian order (MSB first). |
Joe Subbiani | 266476d | 2021-07-07 15:16:56 +0100 | [diff] [blame] | 83 | * |
Joe Subbiani | 0a65d53 | 2021-07-14 11:53:07 +0100 | [diff] [blame^] | 84 | * \param data Base address of the memory to get the four bytes from. |
| 85 | * \param offset Offset from \p base of the first and most significant |
| 86 | * byte of the four bytes to build the 32 bits unsigned |
| 87 | * integer from. |
Joe Subbiani | 9231d5f | 2021-07-07 16:56:29 +0100 | [diff] [blame] | 88 | */ |
| 89 | #ifndef MBEDTLS_GET_UINT32_BE |
| 90 | #define MBEDTLS_GET_UINT32_BE( data , offset ) \ |
| 91 | ( \ |
| 92 | ( (uint32_t) ( data )[( offset ) ] << 24 ) \ |
| 93 | | ( (uint32_t) ( data )[( offset ) + 1] << 16 ) \ |
| 94 | | ( (uint32_t) ( data )[( offset ) + 2] << 8 ) \ |
| 95 | | ( (uint32_t) ( data )[( offset ) + 3] ) \ |
| 96 | ) |
| 97 | #endif |
| 98 | |
| 99 | /** |
Joe Subbiani | 0a65d53 | 2021-07-14 11:53:07 +0100 | [diff] [blame^] | 100 | * Put in memory a 32 bits unsigned integer in big-endian order. |
Joe Subbiani | 9231d5f | 2021-07-07 16:56:29 +0100 | [diff] [blame] | 101 | * |
Joe Subbiani | 0a65d53 | 2021-07-14 11:53:07 +0100 | [diff] [blame^] | 102 | * \param n 32 bits unsigned integer to put in memory |
| 103 | * \param data Base address of the memory where to put the 32 |
| 104 | * bits unsigned integer in. |
| 105 | * \param offset Offset from \p base where to put the most significant |
| 106 | * byte of the 32 bits unsigned integer \p n. |
Joe Subbiani | 266476d | 2021-07-07 15:16:56 +0100 | [diff] [blame] | 107 | */ |
Joe Subbiani | 2bbafda | 2021-06-24 13:00:03 +0100 | [diff] [blame] | 108 | #ifndef MBEDTLS_PUT_UINT32_BE |
Joe Subbiani | 0a65d53 | 2021-07-14 11:53:07 +0100 | [diff] [blame^] | 109 | #define MBEDTLS_PUT_UINT32_BE( n, data, offset ) \ |
| 110 | do { \ |
| 111 | ( data )[( offset ) ] = (unsigned char) ( (n) >> 24 ); \ |
| 112 | ( data )[( offset ) + 1] = (unsigned char) ( (n) >> 16 ); \ |
| 113 | ( data )[( offset ) + 2] = (unsigned char) ( (n) >> 8 ); \ |
| 114 | ( data )[( offset ) + 3] = (unsigned char) ( (n) ); \ |
Joe Subbiani | 2bbafda | 2021-06-24 13:00:03 +0100 | [diff] [blame] | 115 | } while( 0 ) |
Joe Subbiani | aa5f6a6 | 2021-06-23 11:49:03 +0100 | [diff] [blame] | 116 | #endif |
| 117 | |
Joe Subbiani | 266476d | 2021-07-07 15:16:56 +0100 | [diff] [blame] | 118 | /** |
Joe Subbiani | 0a65d53 | 2021-07-14 11:53:07 +0100 | [diff] [blame^] | 119 | * Get the unsigned 32 bits integer corresponding to four bytes in |
| 120 | * little-endian order (LSB first). |
Joe Subbiani | 9231d5f | 2021-07-07 16:56:29 +0100 | [diff] [blame] | 121 | * |
Joe Subbiani | 0a65d53 | 2021-07-14 11:53:07 +0100 | [diff] [blame^] | 122 | * \param data Base address of the memory to get the four bytes from. |
| 123 | * \param offset Offset from \p base of the first and least significant |
| 124 | * byte of the four bytes to build the 32 bits unsigned |
| 125 | * integer from. |
Joe Subbiani | 4fb7555 | 2021-06-23 12:16:47 +0100 | [diff] [blame] | 126 | */ |
Joe Subbiani | 2bbafda | 2021-06-24 13:00:03 +0100 | [diff] [blame] | 127 | #ifndef MBEDTLS_GET_UINT32_LE |
Joe Subbiani | 9231d5f | 2021-07-07 16:56:29 +0100 | [diff] [blame] | 128 | #define MBEDTLS_GET_UINT32_LE( data, offset ) \ |
| 129 | ( \ |
| 130 | ( (uint32_t) ( data )[( offset ) ] ) \ |
| 131 | | ( (uint32_t) ( data )[( offset ) + 1] << 8 ) \ |
| 132 | | ( (uint32_t) ( data )[( offset ) + 2] << 16 ) \ |
| 133 | | ( (uint32_t) ( data )[( offset ) + 3] << 24 ) \ |
| 134 | ) |
Joe Subbiani | 4fb7555 | 2021-06-23 12:16:47 +0100 | [diff] [blame] | 135 | #endif |
| 136 | |
Joe Subbiani | 9231d5f | 2021-07-07 16:56:29 +0100 | [diff] [blame] | 137 | /** |
Joe Subbiani | 0a65d53 | 2021-07-14 11:53:07 +0100 | [diff] [blame^] | 138 | * Put in memory a 32 bits unsigned integer in little-endian order. |
Joe Subbiani | 9231d5f | 2021-07-07 16:56:29 +0100 | [diff] [blame] | 139 | * |
Joe Subbiani | 0a65d53 | 2021-07-14 11:53:07 +0100 | [diff] [blame^] | 140 | * \param n 32 bits unsigned integer to put in memory |
| 141 | * \param data Base address of the memory where to put the 32 |
| 142 | * bits unsigned integer in. |
| 143 | * \param offset Offset from \p base where to put the least significant |
| 144 | * byte of the 32 bits unsigned integer \p n. |
Joe Subbiani | 9231d5f | 2021-07-07 16:56:29 +0100 | [diff] [blame] | 145 | */ |
Joe Subbiani | 2bbafda | 2021-06-24 13:00:03 +0100 | [diff] [blame] | 146 | #ifndef MBEDTLS_PUT_UINT32_LE |
Joe Subbiani | 0a65d53 | 2021-07-14 11:53:07 +0100 | [diff] [blame^] | 147 | #define MBEDTLS_PUT_UINT32_LE( n, data, offset ) \ |
| 148 | do { \ |
| 149 | ( data )[( offset ) ] = (unsigned char) ( ( (n) ) & 0xFF ); \ |
| 150 | ( data )[( offset ) + 1] = (unsigned char) ( ( (n) >> 8 ) & 0xFF ); \ |
| 151 | ( data )[( offset ) + 2] = (unsigned char) ( ( (n) >> 16 ) & 0xFF ); \ |
| 152 | ( data )[( offset ) + 3] = (unsigned char) ( ( (n) >> 24 ) & 0xFF ); \ |
Joe Subbiani | 2bbafda | 2021-06-24 13:00:03 +0100 | [diff] [blame] | 153 | } while( 0 ) |
Joe Subbiani | 4fb7555 | 2021-06-23 12:16:47 +0100 | [diff] [blame] | 154 | #endif |
| 155 | |
Joe Subbiani | 61f7d73 | 2021-06-24 09:06:23 +0100 | [diff] [blame] | 156 | /** |
Joe Subbiani | 0a65d53 | 2021-07-14 11:53:07 +0100 | [diff] [blame^] | 157 | * Get the unsigned 16 bits integer corresponding to four bytes in |
| 158 | * little-endian order (LSB first). |
Joe Subbiani | 9231d5f | 2021-07-07 16:56:29 +0100 | [diff] [blame] | 159 | * |
Joe Subbiani | 0a65d53 | 2021-07-14 11:53:07 +0100 | [diff] [blame^] | 160 | * \param data Base address of the memory to get the four bytes from. |
| 161 | * \param offset Offset from \p base of the first and least significant |
| 162 | * byte of the four bytes to build the 16 bits unsigned |
| 163 | * integer from. |
Joe Subbiani | 927488e | 2021-06-23 11:23:44 +0100 | [diff] [blame] | 164 | */ |
Joe Subbiani | 9231d5f | 2021-07-07 16:56:29 +0100 | [diff] [blame] | 165 | #ifndef MBEDTLS_GET_UINT16_LE |
| 166 | #define MBEDTLS_GET_UINT16_LE( data, offset ) \ |
| 167 | ( \ |
| 168 | ( (uint16_t) ( data )[( offset ) ] ) \ |
| 169 | | ( (uint16_t) ( data )[( offset ) + 1] << 8 ) \ |
Joe Subbiani | 927488e | 2021-06-23 11:23:44 +0100 | [diff] [blame] | 170 | ) |
Joe Subbiani | 9231d5f | 2021-07-07 16:56:29 +0100 | [diff] [blame] | 171 | #endif |
Joe Subbiani | 927488e | 2021-06-23 11:23:44 +0100 | [diff] [blame] | 172 | |
Joe Subbiani | 266476d | 2021-07-07 15:16:56 +0100 | [diff] [blame] | 173 | /** |
Joe Subbiani | 0a65d53 | 2021-07-14 11:53:07 +0100 | [diff] [blame^] | 174 | * Put in memory a 16 bits unsigned integer in little-endian order. |
Joe Subbiani | 266476d | 2021-07-07 15:16:56 +0100 | [diff] [blame] | 175 | * |
Joe Subbiani | 0a65d53 | 2021-07-14 11:53:07 +0100 | [diff] [blame^] | 176 | * \param n 16 bits unsigned integer to put in memory |
| 177 | * \param data Base address of the memory where to put the 16 |
| 178 | * bits unsigned integer in. |
| 179 | * \param offset Offset from \p base where to put the least significant |
| 180 | * byte of the 16 bits unsigned integer \p n. |
Joe Subbiani | 266476d | 2021-07-07 15:16:56 +0100 | [diff] [blame] | 181 | */ |
Joe Subbiani | 4530b27 | 2021-07-05 15:37:39 +0100 | [diff] [blame] | 182 | #ifndef MBEDTLS_PUT_UINT16_LE |
Joe Subbiani | 0a65d53 | 2021-07-14 11:53:07 +0100 | [diff] [blame^] | 183 | #define MBEDTLS_PUT_UINT16_LE( n, data, offset ) \ |
| 184 | { \ |
| 185 | ( data )[( offset ) ] = (unsigned char) ( ( (n) ) & 0xFF ); \ |
| 186 | ( data )[( offset ) + 1] = (unsigned char) ( ( (n) >> 8 ) & 0xFF ); \ |
Joe Subbiani | 4530b27 | 2021-07-05 15:37:39 +0100 | [diff] [blame] | 187 | } |
| 188 | #endif |
| 189 | |
| 190 | |
Gilles Peskine | c4672fd | 2019-09-11 13:39:11 +0200 | [diff] [blame] | 191 | #endif /* MBEDTLS_LIBRARY_COMMON_H */ |