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 | f5462d9 | 2021-07-13 12:13:19 +0100 | [diff] [blame^] | 90 | * Get the unsigned 32 bits integer corresponding to four bytes in |
Joe Subbiani | 635231a | 2021-07-14 11:53:07 +0100 | [diff] [blame] | 91 | * big-endian order (MSB first). |
Joe Subbiani | 394bdd6 | 2021-07-07 15:16:56 +0100 | [diff] [blame] | 92 | * |
Joe Subbiani | 635231a | 2021-07-14 11:53:07 +0100 | [diff] [blame] | 93 | * \param data Base address of the memory to get the four bytes from. |
Joe Subbiani | f5462d9 | 2021-07-13 12:13:19 +0100 | [diff] [blame^] | 94 | * \param offset Offset from \p base of the first and most significant |
| 95 | * byte of the four bytes to build the 32 bits unsigned |
Joe Subbiani | 635231a | 2021-07-14 11:53:07 +0100 | [diff] [blame] | 96 | * integer from. |
Joe Subbiani | 6a50631 | 2021-07-07 16:56:29 +0100 | [diff] [blame] | 97 | */ |
| 98 | #ifndef MBEDTLS_GET_UINT32_BE |
| 99 | #define MBEDTLS_GET_UINT32_BE( data , offset ) \ |
| 100 | ( \ |
| 101 | ( (uint32_t) ( data )[( offset ) ] << 24 ) \ |
| 102 | | ( (uint32_t) ( data )[( offset ) + 1] << 16 ) \ |
| 103 | | ( (uint32_t) ( data )[( offset ) + 2] << 8 ) \ |
| 104 | | ( (uint32_t) ( data )[( offset ) + 3] ) \ |
| 105 | ) |
| 106 | #endif |
| 107 | |
| 108 | /** |
Joe Subbiani | 635231a | 2021-07-14 11:53:07 +0100 | [diff] [blame] | 109 | * Put in memory a 32 bits unsigned integer in big-endian order. |
Joe Subbiani | 6a50631 | 2021-07-07 16:56:29 +0100 | [diff] [blame] | 110 | * |
Joe Subbiani | f5462d9 | 2021-07-13 12:13:19 +0100 | [diff] [blame^] | 111 | * \param n 32 bits unsigned integer to put in memory. |
| 112 | * \param data Base address of the memory where to put the 32 |
Joe Subbiani | 635231a | 2021-07-14 11:53:07 +0100 | [diff] [blame] | 113 | * bits unsigned integer in. |
Joe Subbiani | f5462d9 | 2021-07-13 12:13:19 +0100 | [diff] [blame^] | 114 | * \param offset Offset from \p base where to put the most significant |
Joe Subbiani | 635231a | 2021-07-14 11:53:07 +0100 | [diff] [blame] | 115 | * byte of the 32 bits unsigned integer \p n. |
Joe Subbiani | 394bdd6 | 2021-07-07 15:16:56 +0100 | [diff] [blame] | 116 | */ |
Joe Subbiani | 5ecac21 | 2021-06-24 13:00:03 +0100 | [diff] [blame] | 117 | #ifndef MBEDTLS_PUT_UINT32_BE |
Joe Subbiani | 635231a | 2021-07-14 11:53:07 +0100 | [diff] [blame] | 118 | #define MBEDTLS_PUT_UINT32_BE( n, data, offset ) \ |
| 119 | do { \ |
| 120 | ( data )[( offset ) ] = (unsigned char) ( (n) >> 24 ); \ |
| 121 | ( data )[( offset ) + 1] = (unsigned char) ( (n) >> 16 ); \ |
| 122 | ( data )[( offset ) + 2] = (unsigned char) ( (n) >> 8 ); \ |
| 123 | ( data )[( offset ) + 3] = (unsigned char) ( (n) ); \ |
Joe Subbiani | 5ecac21 | 2021-06-24 13:00:03 +0100 | [diff] [blame] | 124 | } while( 0 ) |
Joe Subbiani | 30d974c | 2021-06-23 11:49:03 +0100 | [diff] [blame] | 125 | #endif |
| 126 | |
Joe Subbiani | 394bdd6 | 2021-07-07 15:16:56 +0100 | [diff] [blame] | 127 | /** |
Joe Subbiani | f5462d9 | 2021-07-13 12:13:19 +0100 | [diff] [blame^] | 128 | * Get the unsigned 32 bits integer corresponding to four bytes in |
Joe Subbiani | 635231a | 2021-07-14 11:53:07 +0100 | [diff] [blame] | 129 | * little-endian order (LSB first). |
Joe Subbiani | 6a50631 | 2021-07-07 16:56:29 +0100 | [diff] [blame] | 130 | * |
Joe Subbiani | 635231a | 2021-07-14 11:53:07 +0100 | [diff] [blame] | 131 | * \param data Base address of the memory to get the four bytes from. |
Joe Subbiani | f5462d9 | 2021-07-13 12:13:19 +0100 | [diff] [blame^] | 132 | * \param offset Offset from \p base of the first and least significant |
| 133 | * byte of the four bytes to build the 32 bits unsigned |
Joe Subbiani | 635231a | 2021-07-14 11:53:07 +0100 | [diff] [blame] | 134 | * integer from. |
Joe Subbiani | 54c6134 | 2021-06-23 12:16:47 +0100 | [diff] [blame] | 135 | */ |
Joe Subbiani | 5ecac21 | 2021-06-24 13:00:03 +0100 | [diff] [blame] | 136 | #ifndef MBEDTLS_GET_UINT32_LE |
Joe Subbiani | 6a50631 | 2021-07-07 16:56:29 +0100 | [diff] [blame] | 137 | #define MBEDTLS_GET_UINT32_LE( data, offset ) \ |
| 138 | ( \ |
| 139 | ( (uint32_t) ( data )[( offset ) ] ) \ |
| 140 | | ( (uint32_t) ( data )[( offset ) + 1] << 8 ) \ |
| 141 | | ( (uint32_t) ( data )[( offset ) + 2] << 16 ) \ |
| 142 | | ( (uint32_t) ( data )[( offset ) + 3] << 24 ) \ |
| 143 | ) |
Joe Subbiani | 54c6134 | 2021-06-23 12:16:47 +0100 | [diff] [blame] | 144 | #endif |
| 145 | |
Joe Subbiani | 6a50631 | 2021-07-07 16:56:29 +0100 | [diff] [blame] | 146 | /** |
Joe Subbiani | 635231a | 2021-07-14 11:53:07 +0100 | [diff] [blame] | 147 | * Put in memory a 32 bits unsigned integer in little-endian order. |
Joe Subbiani | 6a50631 | 2021-07-07 16:56:29 +0100 | [diff] [blame] | 148 | * |
Joe Subbiani | f5462d9 | 2021-07-13 12:13:19 +0100 | [diff] [blame^] | 149 | * \param n 32 bits unsigned integer to put in memory. |
| 150 | * \param data Base address of the memory where to put the 32 |
Joe Subbiani | 635231a | 2021-07-14 11:53:07 +0100 | [diff] [blame] | 151 | * bits unsigned integer in. |
Joe Subbiani | f5462d9 | 2021-07-13 12:13:19 +0100 | [diff] [blame^] | 152 | * \param offset Offset from \p base where to put the least significant |
Joe Subbiani | 635231a | 2021-07-14 11:53:07 +0100 | [diff] [blame] | 153 | * byte of the 32 bits unsigned integer \p n. |
Joe Subbiani | 6a50631 | 2021-07-07 16:56:29 +0100 | [diff] [blame] | 154 | */ |
Joe Subbiani | 5ecac21 | 2021-06-24 13:00:03 +0100 | [diff] [blame] | 155 | #ifndef MBEDTLS_PUT_UINT32_LE |
Joe Subbiani | 635231a | 2021-07-14 11:53:07 +0100 | [diff] [blame] | 156 | #define MBEDTLS_PUT_UINT32_LE( n, data, offset ) \ |
| 157 | do { \ |
| 158 | ( data )[( offset ) ] = (unsigned char) ( ( (n) ) & 0xFF ); \ |
| 159 | ( data )[( offset ) + 1] = (unsigned char) ( ( (n) >> 8 ) & 0xFF ); \ |
| 160 | ( data )[( offset ) + 2] = (unsigned char) ( ( (n) >> 16 ) & 0xFF ); \ |
| 161 | ( data )[( offset ) + 3] = (unsigned char) ( ( (n) >> 24 ) & 0xFF ); \ |
Joe Subbiani | 5ecac21 | 2021-06-24 13:00:03 +0100 | [diff] [blame] | 162 | } while( 0 ) |
Joe Subbiani | 54c6134 | 2021-06-23 12:16:47 +0100 | [diff] [blame] | 163 | #endif |
| 164 | |
Joe Subbiani | 6f2bb0c | 2021-06-24 09:06:23 +0100 | [diff] [blame] | 165 | /** |
Joe Subbiani | f5462d9 | 2021-07-13 12:13:19 +0100 | [diff] [blame^] | 166 | * Get the unsigned 16 bits integer corresponding to four bytes in |
Joe Subbiani | 635231a | 2021-07-14 11:53:07 +0100 | [diff] [blame] | 167 | * little-endian order (LSB first). |
Joe Subbiani | 6a50631 | 2021-07-07 16:56:29 +0100 | [diff] [blame] | 168 | * |
Joe Subbiani | 635231a | 2021-07-14 11:53:07 +0100 | [diff] [blame] | 169 | * \param data Base address of the memory to get the four bytes from. |
Joe Subbiani | f5462d9 | 2021-07-13 12:13:19 +0100 | [diff] [blame^] | 170 | * \param offset Offset from \p base of the first and least significant |
| 171 | * byte of the four bytes to build the 16 bits unsigned |
Joe Subbiani | 635231a | 2021-07-14 11:53:07 +0100 | [diff] [blame] | 172 | * integer from. |
Joe Subbiani | 3b39450 | 2021-06-23 11:23:44 +0100 | [diff] [blame] | 173 | */ |
Joe Subbiani | 6a50631 | 2021-07-07 16:56:29 +0100 | [diff] [blame] | 174 | #ifndef MBEDTLS_GET_UINT16_LE |
| 175 | #define MBEDTLS_GET_UINT16_LE( data, offset ) \ |
| 176 | ( \ |
| 177 | ( (uint16_t) ( data )[( offset ) ] ) \ |
| 178 | | ( (uint16_t) ( data )[( offset ) + 1] << 8 ) \ |
Joe Subbiani | 3b39450 | 2021-06-23 11:23:44 +0100 | [diff] [blame] | 179 | ) |
Joe Subbiani | 6a50631 | 2021-07-07 16:56:29 +0100 | [diff] [blame] | 180 | #endif |
Joe Subbiani | 3b39450 | 2021-06-23 11:23:44 +0100 | [diff] [blame] | 181 | |
Joe Subbiani | 394bdd6 | 2021-07-07 15:16:56 +0100 | [diff] [blame] | 182 | /** |
Joe Subbiani | 635231a | 2021-07-14 11:53:07 +0100 | [diff] [blame] | 183 | * Put in memory a 16 bits unsigned integer in little-endian order. |
Joe Subbiani | 394bdd6 | 2021-07-07 15:16:56 +0100 | [diff] [blame] | 184 | * |
Joe Subbiani | f5462d9 | 2021-07-13 12:13:19 +0100 | [diff] [blame^] | 185 | * \param n 16 bits unsigned integer to put in memory. |
| 186 | * \param data Base address of the memory where to put the 16 |
Joe Subbiani | 635231a | 2021-07-14 11:53:07 +0100 | [diff] [blame] | 187 | * bits unsigned integer in. |
Joe Subbiani | f5462d9 | 2021-07-13 12:13:19 +0100 | [diff] [blame^] | 188 | * \param offset Offset from \p base where to put the least significant |
Joe Subbiani | 635231a | 2021-07-14 11:53:07 +0100 | [diff] [blame] | 189 | * byte of the 16 bits unsigned integer \p n. |
Joe Subbiani | 394bdd6 | 2021-07-07 15:16:56 +0100 | [diff] [blame] | 190 | */ |
Joe Subbiani | 9fa9ac3 | 2021-07-05 15:37:39 +0100 | [diff] [blame] | 191 | #ifndef MBEDTLS_PUT_UINT16_LE |
Joe Subbiani | 635231a | 2021-07-14 11:53:07 +0100 | [diff] [blame] | 192 | #define MBEDTLS_PUT_UINT16_LE( n, data, offset ) \ |
| 193 | { \ |
| 194 | ( data )[( offset ) ] = (unsigned char) ( ( (n) ) & 0xFF ); \ |
| 195 | ( data )[( offset ) + 1] = (unsigned char) ( ( (n) >> 8 ) & 0xFF ); \ |
Joe Subbiani | 9fa9ac3 | 2021-07-05 15:37:39 +0100 | [diff] [blame] | 196 | } |
| 197 | #endif |
| 198 | |
| 199 | |
Gilles Peskine | c4672fd | 2019-09-11 13:39:11 +0200 | [diff] [blame] | 200 | #endif /* MBEDTLS_LIBRARY_COMMON_H */ |