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