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 | 9231d5f | 2021-07-07 16:56:29 +0100 | [diff] [blame] | 81 | * 32-bit integer manipulation GET macros (big endian) |
Joe Subbiani | 266476d | 2021-07-07 15:16:56 +0100 | [diff] [blame] | 82 | * |
Joe Subbiani | 9231d5f | 2021-07-07 16:56:29 +0100 | [diff] [blame] | 83 | * \brief Use this to assign an unsigned 32 bit integer |
| 84 | * by taking data stored adjacent in memory that |
| 85 | * can be accessed via on offset |
| 86 | * Big Endian is used when wanting to |
| 87 | * transmit the most signifcant bits first |
Joe Subbiani | 266476d | 2021-07-07 15:16:56 +0100 | [diff] [blame] | 88 | * |
Joe Subbiani | 9231d5f | 2021-07-07 16:56:29 +0100 | [diff] [blame] | 89 | * \param data The data used to translate to a 32 bit |
| 90 | * integer |
| 91 | * \param offset the shift in bytes to access the next byte |
| 92 | * of data |
| 93 | */ |
| 94 | #ifndef MBEDTLS_GET_UINT32_BE |
| 95 | #define MBEDTLS_GET_UINT32_BE( data , offset ) \ |
| 96 | ( \ |
| 97 | ( (uint32_t) ( data )[( offset ) ] << 24 ) \ |
| 98 | | ( (uint32_t) ( data )[( offset ) + 1] << 16 ) \ |
| 99 | | ( (uint32_t) ( data )[( offset ) + 2] << 8 ) \ |
| 100 | | ( (uint32_t) ( data )[( offset ) + 3] ) \ |
| 101 | ) |
| 102 | #endif |
| 103 | |
| 104 | /** |
| 105 | * 32-bit integer manipulation PUT macros (big endian) |
| 106 | * |
| 107 | * \brief Read from a 32 bit integer and store each byte |
| 108 | * in memory, offset by a specified amount, resulting |
| 109 | * in each byte being adjacent in memory. |
| 110 | * Big Endian is used when wanting to |
| 111 | * transmit the most signifcant bits first |
| 112 | * |
| 113 | * \param n 32 bit integer where data is accessed |
Joe Subbiani | 266476d | 2021-07-07 15:16:56 +0100 | [diff] [blame] | 114 | * \param b const unsigned char array of data to be |
| 115 | * manipulated |
| 116 | * \param i offset in bytes, In the case of UINT32, i |
| 117 | * would increment by 4 every use assuming |
| 118 | * the data is being stored in the same location |
| 119 | */ |
Joe Subbiani | 2bbafda | 2021-06-24 13:00:03 +0100 | [diff] [blame] | 120 | #ifndef MBEDTLS_PUT_UINT32_BE |
Joe Subbiani | 4530b27 | 2021-07-05 15:37:39 +0100 | [diff] [blame] | 121 | #define MBEDTLS_PUT_UINT32_BE(n,b,i) \ |
Joe Subbiani | 2bbafda | 2021-06-24 13:00:03 +0100 | [diff] [blame] | 122 | do { \ |
| 123 | (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \ |
| 124 | (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \ |
| 125 | (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \ |
| 126 | (b)[(i) + 3] = (unsigned char) ( (n) ); \ |
| 127 | } while( 0 ) |
Joe Subbiani | aa5f6a6 | 2021-06-23 11:49:03 +0100 | [diff] [blame] | 128 | #endif |
| 129 | |
Joe Subbiani | 266476d | 2021-07-07 15:16:56 +0100 | [diff] [blame] | 130 | /** |
Joe Subbiani | 9231d5f | 2021-07-07 16:56:29 +0100 | [diff] [blame] | 131 | * 32-bit integer manipulation GET macros (little endian) |
| 132 | * |
| 133 | * \brief Use this to assign an unsigned 32 bit integer |
| 134 | * by taking data stored adjacent in memory that |
| 135 | * can be accessed via on offset |
| 136 | * Little Endian is used when wanting to |
| 137 | * transmit the least signifcant bits first |
| 138 | * |
| 139 | * \param data The data used to translate to a 32 bit |
| 140 | * integer |
| 141 | * \param offset the shift in bytes to access the next byte |
| 142 | * of data |
Joe Subbiani | 4fb7555 | 2021-06-23 12:16:47 +0100 | [diff] [blame] | 143 | */ |
Joe Subbiani | 2bbafda | 2021-06-24 13:00:03 +0100 | [diff] [blame] | 144 | #ifndef MBEDTLS_GET_UINT32_LE |
Joe Subbiani | 9231d5f | 2021-07-07 16:56:29 +0100 | [diff] [blame] | 145 | #define MBEDTLS_GET_UINT32_LE( data, offset ) \ |
| 146 | ( \ |
| 147 | ( (uint32_t) ( data )[( offset ) ] ) \ |
| 148 | | ( (uint32_t) ( data )[( offset ) + 1] << 8 ) \ |
| 149 | | ( (uint32_t) ( data )[( offset ) + 2] << 16 ) \ |
| 150 | | ( (uint32_t) ( data )[( offset ) + 3] << 24 ) \ |
| 151 | ) |
Joe Subbiani | 4fb7555 | 2021-06-23 12:16:47 +0100 | [diff] [blame] | 152 | #endif |
| 153 | |
Joe Subbiani | 9231d5f | 2021-07-07 16:56:29 +0100 | [diff] [blame] | 154 | /** |
| 155 | * 32-bit integer manipulation PUT macros (little endian) |
| 156 | * |
| 157 | * \brief Read from a 32 bit integer and store each byte |
| 158 | * in memory, offset by a specified amount, resulting |
| 159 | * in each byte being adjacent in memory. |
| 160 | * Little Endian is used when wanting to |
| 161 | * transmit the least signifcant bits first |
| 162 | * |
| 163 | * \param n 32 bit integer where data is accessed |
| 164 | * \param b const unsigned char array of data to be |
| 165 | * manipulated |
| 166 | * \param i offset in bytes, In the case of UINT32, i |
| 167 | * would increment by 4 every use assuming |
| 168 | * the data is being stored in the same location |
| 169 | */ |
Joe Subbiani | 2bbafda | 2021-06-24 13:00:03 +0100 | [diff] [blame] | 170 | #ifndef MBEDTLS_PUT_UINT32_LE |
Joe Subbiani | 4530b27 | 2021-07-05 15:37:39 +0100 | [diff] [blame] | 171 | #define MBEDTLS_PUT_UINT32_LE(n,b,i) \ |
Joe Subbiani | 2bbafda | 2021-06-24 13:00:03 +0100 | [diff] [blame] | 172 | do { \ |
| 173 | (b)[(i) ] = (unsigned char) ( ( (n) ) & 0xFF ); \ |
| 174 | (b)[(i) + 1] = (unsigned char) ( ( (n) >> 8 ) & 0xFF ); \ |
| 175 | (b)[(i) + 2] = (unsigned char) ( ( (n) >> 16 ) & 0xFF ); \ |
| 176 | (b)[(i) + 3] = (unsigned char) ( ( (n) >> 24 ) & 0xFF ); \ |
| 177 | } while( 0 ) |
Joe Subbiani | 4fb7555 | 2021-06-23 12:16:47 +0100 | [diff] [blame] | 178 | #endif |
| 179 | |
Joe Subbiani | 61f7d73 | 2021-06-24 09:06:23 +0100 | [diff] [blame] | 180 | /** |
Joe Subbiani | 9231d5f | 2021-07-07 16:56:29 +0100 | [diff] [blame] | 181 | * 16-bit integer manipulation GET macros (little endian) |
| 182 | * |
| 183 | * \brief Use this to assign an unsigned 16 bit integer |
| 184 | * by taking data stored adjacent in memory that |
| 185 | * can be accessed via on offset |
| 186 | * Little Endian is used when wanting to |
| 187 | * transmit the least signifcant bits first |
| 188 | * |
| 189 | * \param data The data used to translate to a 16 bit |
| 190 | * integer |
| 191 | * \param offset the shit in bytes to access the next byte |
| 192 | * of data |
Joe Subbiani | 927488e | 2021-06-23 11:23:44 +0100 | [diff] [blame] | 193 | */ |
Joe Subbiani | 9231d5f | 2021-07-07 16:56:29 +0100 | [diff] [blame] | 194 | #ifndef MBEDTLS_GET_UINT16_LE |
| 195 | #define MBEDTLS_GET_UINT16_LE( data, offset ) \ |
| 196 | ( \ |
| 197 | ( (uint16_t) ( data )[( offset ) ] ) \ |
| 198 | | ( (uint16_t) ( data )[( offset ) + 1] << 8 ) \ |
Joe Subbiani | 927488e | 2021-06-23 11:23:44 +0100 | [diff] [blame] | 199 | ) |
Joe Subbiani | 9231d5f | 2021-07-07 16:56:29 +0100 | [diff] [blame] | 200 | #endif |
Joe Subbiani | 927488e | 2021-06-23 11:23:44 +0100 | [diff] [blame] | 201 | |
Joe Subbiani | 266476d | 2021-07-07 15:16:56 +0100 | [diff] [blame] | 202 | /** |
Joe Subbiani | 9231d5f | 2021-07-07 16:56:29 +0100 | [diff] [blame] | 203 | * 16-bit integer manipulation PUT macros (little endian) |
Joe Subbiani | 266476d | 2021-07-07 15:16:56 +0100 | [diff] [blame] | 204 | * |
Joe Subbiani | 9231d5f | 2021-07-07 16:56:29 +0100 | [diff] [blame] | 205 | * \brief Read from a 16 bit integer and store each byte |
| 206 | * in memory, offset by a specified amount, resulting |
| 207 | * in each byte being adjacent in memory. |
| 208 | * Little Endian is used when wanting to |
| 209 | * transmit the least signifcant bits first |
Joe Subbiani | 266476d | 2021-07-07 15:16:56 +0100 | [diff] [blame] | 210 | * |
Joe Subbiani | 9231d5f | 2021-07-07 16:56:29 +0100 | [diff] [blame] | 211 | * \param n 16 bit integer where data is accessed |
Joe Subbiani | 266476d | 2021-07-07 15:16:56 +0100 | [diff] [blame] | 212 | * \param b const unsigned char array of data to be |
| 213 | * manipulated |
| 214 | * \param i offset in bytes, In the case of UINT16, i |
| 215 | * would increment by 2 every use assuming |
| 216 | * the data is being stored in the same location |
| 217 | */ |
Joe Subbiani | 4530b27 | 2021-07-05 15:37:39 +0100 | [diff] [blame] | 218 | #ifndef MBEDTLS_PUT_UINT16_LE |
| 219 | #define MBEDTLS_PUT_UINT16_LE( n, b, i ) \ |
| 220 | { \ |
| 221 | (b)[(i) ] = (unsigned char) ( ( (n) ) & 0xFF ); \ |
| 222 | (b)[(i) + 1] = (unsigned char) ( ( (n) >> 8 ) & 0xFF ); \ |
| 223 | } |
| 224 | #endif |
| 225 | |
| 226 | |
Gilles Peskine | c4672fd | 2019-09-11 13:39:11 +0200 | [diff] [blame] | 227 | #endif /* MBEDTLS_LIBRARY_COMMON_H */ |