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 | |
Bence Szépkúti | c662b36 | 2021-05-27 11:25:03 +0200 | [diff] [blame] | 26 | #include "mbedtls/build_info.h" |
Gilles Peskine | c4672fd | 2019-09-11 13:39:11 +0200 | [diff] [blame] | 27 | |
| 28 | /** Helper to define a function as static except when building invasive tests. |
| 29 | * |
| 30 | * If a function is only used inside its own source file and should be |
| 31 | * declared `static` to allow the compiler to optimize for code size, |
| 32 | * but that function has unit tests, define it with |
| 33 | * ``` |
| 34 | * MBEDTLS_STATIC_TESTABLE int mbedtls_foo(...) { ... } |
| 35 | * ``` |
| 36 | * and declare it in a header in the `library/` directory with |
| 37 | * ``` |
| 38 | * #if defined(MBEDTLS_TEST_HOOKS) |
| 39 | * int mbedtls_foo(...); |
| 40 | * #endif |
| 41 | * ``` |
| 42 | */ |
| 43 | #if defined(MBEDTLS_TEST_HOOKS) |
| 44 | #define MBEDTLS_STATIC_TESTABLE |
| 45 | #else |
| 46 | #define MBEDTLS_STATIC_TESTABLE static |
| 47 | #endif |
| 48 | |
TRodziewicz | 7871c2e | 2021-07-07 17:29:43 +0200 | [diff] [blame] | 49 | #if defined(MBEDTLS_TEST_HOOKS) |
| 50 | extern void (*mbedtls_test_hook_test_fail)( const char * test, int line, const char * file ); |
| 51 | #define MBEDTLS_TEST_HOOK_TEST_ASSERT( TEST ) \ |
| 52 | do { \ |
| 53 | if( ( ! ( TEST ) ) && ( ( *mbedtls_test_hook_test_fail ) != NULL ) ) \ |
| 54 | { \ |
| 55 | ( *mbedtls_test_hook_test_fail )( #TEST, __LINE__, __FILE__ ); \ |
| 56 | } \ |
| 57 | } while( 0 ) |
| 58 | #else |
| 59 | #define MBEDTLS_TEST_HOOK_TEST_ASSERT( TEST ) |
| 60 | #endif /* defined(MBEDTLS_TEST_HOOKS) */ |
| 61 | |
Mateusz Starzyk | 57d1d19 | 2021-05-27 14:39:53 +0200 | [diff] [blame] | 62 | /** Allow library to access its structs' private members. |
Mateusz Starzyk | 2c09c9b | 2021-05-14 22:20:10 +0200 | [diff] [blame] | 63 | * |
| 64 | * Although structs defined in header files are publicly available, |
| 65 | * their members are private and should not be accessed by the user. |
| 66 | */ |
| 67 | #define MBEDTLS_ALLOW_PRIVATE_ACCESS |
| 68 | |
Joe Subbiani | 50dde56 | 2021-06-22 15:51:53 +0100 | [diff] [blame] | 69 | /** Byte Reading Macros |
Joe Subbiani | 6f2bb0c | 2021-06-24 09:06:23 +0100 | [diff] [blame] | 70 | * |
Joe Subbiani | 394bdd6 | 2021-07-07 15:16:56 +0100 | [diff] [blame] | 71 | * Obtain the most significant byte of x using 0xff |
| 72 | * Using MBEDTLS_BYTE_a will shift a*8 bits |
| 73 | * to retrieve the next byte of information |
Joe Subbiani | 50dde56 | 2021-06-22 15:51:53 +0100 | [diff] [blame] | 74 | */ |
Joe Subbiani | 5ecac21 | 2021-06-24 13:00:03 +0100 | [diff] [blame] | 75 | #define MBEDTLS_BYTE_0( x ) ( (uint8_t) ( ( x ) & 0xff ) ) |
| 76 | #define MBEDTLS_BYTE_1( x ) ( (uint8_t) ( ( ( x ) >> 8 ) & 0xff ) ) |
| 77 | #define MBEDTLS_BYTE_2( x ) ( (uint8_t) ( ( ( x ) >> 16 ) & 0xff ) ) |
| 78 | #define MBEDTLS_BYTE_3( x ) ( (uint8_t) ( ( ( x ) >> 24 ) & 0xff ) ) |
Joe Subbiani | 50dde56 | 2021-06-22 15:51:53 +0100 | [diff] [blame] | 79 | |
Joe Subbiani | cd84d76 | 2021-07-08 14:59:52 +0100 | [diff] [blame^] | 80 | #define MBEDTLS_CHAR_0( x ) ( (unsigned char) ( ( x ) & 0xff ) ) |
| 81 | #define MBEDTLS_CHAR_1( x ) ( (unsigned char) ( ( ( x ) >> 8 ) & 0xff ) ) |
| 82 | #define MBEDTLS_CHAR_2( x ) ( (unsigned char) ( ( ( x ) >> 16 ) & 0xff ) ) |
| 83 | #define MBEDTLS_CHAR_3( x ) ( (unsigned char) ( ( ( x ) >> 24 ) & 0xff ) ) |
| 84 | #define MBEDTLS_CHAR_4( x ) ( (unsigned char) ( ( ( x ) >> 32 ) & 0xff ) ) |
| 85 | #define MBEDTLS_CHAR_5( x ) ( (unsigned char) ( ( ( x ) >> 40 ) & 0xff ) ) |
| 86 | #define MBEDTLS_CHAR_6( x ) ( (unsigned char) ( ( ( x ) >> 48 ) & 0xff ) ) |
| 87 | #define MBEDTLS_CHAR_7( x ) ( (unsigned char) ( ( ( x ) >> 56 ) & 0xff ) ) |
| 88 | |
Joe Subbiani | 394bdd6 | 2021-07-07 15:16:56 +0100 | [diff] [blame] | 89 | /** |
Joe Subbiani | 6a50631 | 2021-07-07 16:56:29 +0100 | [diff] [blame] | 90 | * 32-bit integer manipulation GET macros (big endian) |
Joe Subbiani | 394bdd6 | 2021-07-07 15:16:56 +0100 | [diff] [blame] | 91 | * |
Joe Subbiani | 6a50631 | 2021-07-07 16:56:29 +0100 | [diff] [blame] | 92 | * \brief Use this to assign an unsigned 32 bit integer |
| 93 | * by taking data stored adjacent in memory that |
| 94 | * can be accessed via on offset |
| 95 | * Big Endian is used when wanting to |
| 96 | * transmit the most signifcant bits first |
Joe Subbiani | 394bdd6 | 2021-07-07 15:16:56 +0100 | [diff] [blame] | 97 | * |
Joe Subbiani | 6a50631 | 2021-07-07 16:56:29 +0100 | [diff] [blame] | 98 | * \param data The data used to translate to a 32 bit |
| 99 | * integer |
| 100 | * \param offset the shift in bytes to access the next byte |
| 101 | * of data |
| 102 | */ |
| 103 | #ifndef MBEDTLS_GET_UINT32_BE |
| 104 | #define MBEDTLS_GET_UINT32_BE( data , offset ) \ |
| 105 | ( \ |
| 106 | ( (uint32_t) ( data )[( offset ) ] << 24 ) \ |
| 107 | | ( (uint32_t) ( data )[( offset ) + 1] << 16 ) \ |
| 108 | | ( (uint32_t) ( data )[( offset ) + 2] << 8 ) \ |
| 109 | | ( (uint32_t) ( data )[( offset ) + 3] ) \ |
| 110 | ) |
| 111 | #endif |
| 112 | |
| 113 | /** |
| 114 | * 32-bit integer manipulation PUT macros (big endian) |
| 115 | * |
| 116 | * \brief Read from a 32 bit integer and store each byte |
| 117 | * in memory, offset by a specified amount, resulting |
| 118 | * in each byte being adjacent in memory. |
| 119 | * Big Endian is used when wanting to |
| 120 | * transmit the most signifcant bits first |
| 121 | * |
| 122 | * \param n 32 bit integer where data is accessed |
Joe Subbiani | 394bdd6 | 2021-07-07 15:16:56 +0100 | [diff] [blame] | 123 | * \param b const unsigned char array of data to be |
| 124 | * manipulated |
| 125 | * \param i offset in bytes, In the case of UINT32, i |
| 126 | * would increment by 4 every use assuming |
| 127 | * the data is being stored in the same location |
| 128 | */ |
Joe Subbiani | 5ecac21 | 2021-06-24 13:00:03 +0100 | [diff] [blame] | 129 | #ifndef MBEDTLS_PUT_UINT32_BE |
Joe Subbiani | 9fa9ac3 | 2021-07-05 15:37:39 +0100 | [diff] [blame] | 130 | #define MBEDTLS_PUT_UINT32_BE(n,b,i) \ |
Joe Subbiani | 5ecac21 | 2021-06-24 13:00:03 +0100 | [diff] [blame] | 131 | do { \ |
| 132 | (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \ |
| 133 | (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \ |
| 134 | (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \ |
| 135 | (b)[(i) + 3] = (unsigned char) ( (n) ); \ |
| 136 | } while( 0 ) |
Joe Subbiani | 30d974c | 2021-06-23 11:49:03 +0100 | [diff] [blame] | 137 | #endif |
| 138 | |
Joe Subbiani | 394bdd6 | 2021-07-07 15:16:56 +0100 | [diff] [blame] | 139 | /** |
Joe Subbiani | 6a50631 | 2021-07-07 16:56:29 +0100 | [diff] [blame] | 140 | * 32-bit integer manipulation GET macros (little endian) |
| 141 | * |
| 142 | * \brief Use this to assign an unsigned 32 bit integer |
| 143 | * by taking data stored adjacent in memory that |
| 144 | * can be accessed via on offset |
| 145 | * Little Endian is used when wanting to |
| 146 | * transmit the least signifcant bits first |
| 147 | * |
| 148 | * \param data The data used to translate to a 32 bit |
| 149 | * integer |
| 150 | * \param offset the shift in bytes to access the next byte |
| 151 | * of data |
Joe Subbiani | 54c6134 | 2021-06-23 12:16:47 +0100 | [diff] [blame] | 152 | */ |
Joe Subbiani | 5ecac21 | 2021-06-24 13:00:03 +0100 | [diff] [blame] | 153 | #ifndef MBEDTLS_GET_UINT32_LE |
Joe Subbiani | 6a50631 | 2021-07-07 16:56:29 +0100 | [diff] [blame] | 154 | #define MBEDTLS_GET_UINT32_LE( data, offset ) \ |
| 155 | ( \ |
| 156 | ( (uint32_t) ( data )[( offset ) ] ) \ |
| 157 | | ( (uint32_t) ( data )[( offset ) + 1] << 8 ) \ |
| 158 | | ( (uint32_t) ( data )[( offset ) + 2] << 16 ) \ |
| 159 | | ( (uint32_t) ( data )[( offset ) + 3] << 24 ) \ |
| 160 | ) |
Joe Subbiani | 54c6134 | 2021-06-23 12:16:47 +0100 | [diff] [blame] | 161 | #endif |
| 162 | |
Joe Subbiani | 6a50631 | 2021-07-07 16:56:29 +0100 | [diff] [blame] | 163 | /** |
| 164 | * 32-bit integer manipulation PUT macros (little endian) |
| 165 | * |
| 166 | * \brief Read from a 32 bit integer and store each byte |
| 167 | * in memory, offset by a specified amount, resulting |
| 168 | * in each byte being adjacent in memory. |
| 169 | * Little Endian is used when wanting to |
| 170 | * transmit the least signifcant bits first |
| 171 | * |
| 172 | * \param n 32 bit integer where data is accessed |
| 173 | * \param b const unsigned char array of data to be |
| 174 | * manipulated |
| 175 | * \param i offset in bytes, In the case of UINT32, i |
| 176 | * would increment by 4 every use assuming |
| 177 | * the data is being stored in the same location |
| 178 | */ |
Joe Subbiani | 5ecac21 | 2021-06-24 13:00:03 +0100 | [diff] [blame] | 179 | #ifndef MBEDTLS_PUT_UINT32_LE |
Joe Subbiani | 9fa9ac3 | 2021-07-05 15:37:39 +0100 | [diff] [blame] | 180 | #define MBEDTLS_PUT_UINT32_LE(n,b,i) \ |
Joe Subbiani | 5ecac21 | 2021-06-24 13:00:03 +0100 | [diff] [blame] | 181 | do { \ |
| 182 | (b)[(i) ] = (unsigned char) ( ( (n) ) & 0xFF ); \ |
| 183 | (b)[(i) + 1] = (unsigned char) ( ( (n) >> 8 ) & 0xFF ); \ |
| 184 | (b)[(i) + 2] = (unsigned char) ( ( (n) >> 16 ) & 0xFF ); \ |
| 185 | (b)[(i) + 3] = (unsigned char) ( ( (n) >> 24 ) & 0xFF ); \ |
| 186 | } while( 0 ) |
Joe Subbiani | 54c6134 | 2021-06-23 12:16:47 +0100 | [diff] [blame] | 187 | #endif |
| 188 | |
Joe Subbiani | 6f2bb0c | 2021-06-24 09:06:23 +0100 | [diff] [blame] | 189 | /** |
Joe Subbiani | 6a50631 | 2021-07-07 16:56:29 +0100 | [diff] [blame] | 190 | * 16-bit integer manipulation GET macros (little endian) |
| 191 | * |
| 192 | * \brief Use this to assign an unsigned 16 bit integer |
| 193 | * by taking data stored adjacent in memory that |
| 194 | * can be accessed via on offset |
| 195 | * Little Endian is used when wanting to |
| 196 | * transmit the least signifcant bits first |
| 197 | * |
| 198 | * \param data The data used to translate to a 16 bit |
| 199 | * integer |
| 200 | * \param offset the shit in bytes to access the next byte |
| 201 | * of data |
Joe Subbiani | 3b39450 | 2021-06-23 11:23:44 +0100 | [diff] [blame] | 202 | */ |
Joe Subbiani | 6a50631 | 2021-07-07 16:56:29 +0100 | [diff] [blame] | 203 | #ifndef MBEDTLS_GET_UINT16_LE |
| 204 | #define MBEDTLS_GET_UINT16_LE( data, offset ) \ |
| 205 | ( \ |
| 206 | ( (uint16_t) ( data )[( offset ) ] ) \ |
| 207 | | ( (uint16_t) ( data )[( offset ) + 1] << 8 ) \ |
Joe Subbiani | 3b39450 | 2021-06-23 11:23:44 +0100 | [diff] [blame] | 208 | ) |
Joe Subbiani | 6a50631 | 2021-07-07 16:56:29 +0100 | [diff] [blame] | 209 | #endif |
Joe Subbiani | 3b39450 | 2021-06-23 11:23:44 +0100 | [diff] [blame] | 210 | |
Joe Subbiani | 394bdd6 | 2021-07-07 15:16:56 +0100 | [diff] [blame] | 211 | /** |
Joe Subbiani | 6a50631 | 2021-07-07 16:56:29 +0100 | [diff] [blame] | 212 | * 16-bit integer manipulation PUT macros (little endian) |
Joe Subbiani | 394bdd6 | 2021-07-07 15:16:56 +0100 | [diff] [blame] | 213 | * |
Joe Subbiani | 6a50631 | 2021-07-07 16:56:29 +0100 | [diff] [blame] | 214 | * \brief Read from a 16 bit integer and store each byte |
| 215 | * in memory, offset by a specified amount, resulting |
| 216 | * in each byte being adjacent in memory. |
| 217 | * Little Endian is used when wanting to |
| 218 | * transmit the least signifcant bits first |
Joe Subbiani | 394bdd6 | 2021-07-07 15:16:56 +0100 | [diff] [blame] | 219 | * |
Joe Subbiani | 6a50631 | 2021-07-07 16:56:29 +0100 | [diff] [blame] | 220 | * \param n 16 bit integer where data is accessed |
Joe Subbiani | 394bdd6 | 2021-07-07 15:16:56 +0100 | [diff] [blame] | 221 | * \param b const unsigned char array of data to be |
| 222 | * manipulated |
| 223 | * \param i offset in bytes, In the case of UINT16, i |
| 224 | * would increment by 2 every use assuming |
| 225 | * the data is being stored in the same location |
| 226 | */ |
Joe Subbiani | 9fa9ac3 | 2021-07-05 15:37:39 +0100 | [diff] [blame] | 227 | #ifndef MBEDTLS_PUT_UINT16_LE |
| 228 | #define MBEDTLS_PUT_UINT16_LE( n, b, i ) \ |
| 229 | { \ |
| 230 | (b)[(i) ] = (unsigned char) ( ( (n) ) & 0xFF ); \ |
| 231 | (b)[(i) + 1] = (unsigned char) ( ( (n) >> 8 ) & 0xFF ); \ |
| 232 | } |
| 233 | #endif |
| 234 | |
| 235 | |
Gilles Peskine | c4672fd | 2019-09-11 13:39:11 +0200 | [diff] [blame] | 236 | #endif /* MBEDTLS_LIBRARY_COMMON_H */ |