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 | |
Gilles Peskine | 42649d9 | 2022-11-23 14:15:57 +0100 | [diff] [blame^] | 28 | #include <stddef.h> |
Joe Subbiani | 2194dc4 | 2021-07-14 12:31:31 +0100 | [diff] [blame] | 29 | #include <stdint.h> |
| 30 | |
Gilles Peskine | c4672fd | 2019-09-11 13:39:11 +0200 | [diff] [blame] | 31 | /** Helper to define a function as static except when building invasive tests. |
| 32 | * |
| 33 | * If a function is only used inside its own source file and should be |
| 34 | * declared `static` to allow the compiler to optimize for code size, |
| 35 | * but that function has unit tests, define it with |
| 36 | * ``` |
| 37 | * MBEDTLS_STATIC_TESTABLE int mbedtls_foo(...) { ... } |
| 38 | * ``` |
| 39 | * and declare it in a header in the `library/` directory with |
| 40 | * ``` |
| 41 | * #if defined(MBEDTLS_TEST_HOOKS) |
| 42 | * int mbedtls_foo(...); |
| 43 | * #endif |
| 44 | * ``` |
| 45 | */ |
| 46 | #if defined(MBEDTLS_TEST_HOOKS) |
| 47 | #define MBEDTLS_STATIC_TESTABLE |
| 48 | #else |
| 49 | #define MBEDTLS_STATIC_TESTABLE static |
| 50 | #endif |
| 51 | |
TRodziewicz | 7871c2e | 2021-07-07 17:29:43 +0200 | [diff] [blame] | 52 | #if defined(MBEDTLS_TEST_HOOKS) |
| 53 | extern void (*mbedtls_test_hook_test_fail)( const char * test, int line, const char * file ); |
| 54 | #define MBEDTLS_TEST_HOOK_TEST_ASSERT( TEST ) \ |
| 55 | do { \ |
| 56 | if( ( ! ( TEST ) ) && ( ( *mbedtls_test_hook_test_fail ) != NULL ) ) \ |
| 57 | { \ |
| 58 | ( *mbedtls_test_hook_test_fail )( #TEST, __LINE__, __FILE__ ); \ |
| 59 | } \ |
| 60 | } while( 0 ) |
| 61 | #else |
| 62 | #define MBEDTLS_TEST_HOOK_TEST_ASSERT( TEST ) |
| 63 | #endif /* defined(MBEDTLS_TEST_HOOKS) */ |
| 64 | |
Mateusz Starzyk | 57d1d19 | 2021-05-27 14:39:53 +0200 | [diff] [blame] | 65 | /** Allow library to access its structs' private members. |
Mateusz Starzyk | 2c09c9b | 2021-05-14 22:20:10 +0200 | [diff] [blame] | 66 | * |
| 67 | * Although structs defined in header files are publicly available, |
| 68 | * their members are private and should not be accessed by the user. |
| 69 | */ |
| 70 | #define MBEDTLS_ALLOW_PRIVATE_ACCESS |
| 71 | |
Gilles Peskine | 42649d9 | 2022-11-23 14:15:57 +0100 | [diff] [blame^] | 72 | /** Return an offset into a buffer. |
| 73 | * |
| 74 | * This is just the addition of an offset to a pointer, except that this |
| 75 | * function also accepts an offset of 0 into a buffer whose pointer is null. |
| 76 | * |
| 77 | * \param p Pointer to a buffer of at least n bytes. |
| 78 | * This may be \p NULL if \p n is zero. |
| 79 | * \param n An offset in bytes. |
| 80 | * \return Pointer to offset \p n in the buffer \p p. |
| 81 | * Note that this is only a valid pointer if the size of the |
| 82 | * buffer is at least \p n + 1. |
| 83 | */ |
| 84 | static inline unsigned char *mbedtls_buffer_offset( |
| 85 | unsigned char *p, size_t n ) |
| 86 | { |
| 87 | return( p == NULL ? NULL : p + n ); |
| 88 | } |
| 89 | |
| 90 | /** Return an offset into a read-only buffer. |
| 91 | * |
| 92 | * This is just the addition of an offset to a pointer, except that this |
| 93 | * function also accepts an offset of 0 into a buffer whose pointer is null. |
| 94 | * |
| 95 | * \param p Pointer to a buffer of at least n bytes. |
| 96 | * This may be \p NULL if \p n is zero. |
| 97 | * \param n An offset in bytes. |
| 98 | * \return Pointer to offset \p n in the buffer \p p. |
| 99 | * Note that this is only a valid pointer if the size of the |
| 100 | * buffer is at least \p n + 1. |
| 101 | */ |
| 102 | static inline const unsigned char *mbedtls_buffer_offset_const( |
| 103 | const unsigned char *p, size_t n ) |
| 104 | { |
| 105 | return( p == NULL ? NULL : p + n ); |
| 106 | } |
| 107 | |
Joe Subbiani | 50dde56 | 2021-06-22 15:51:53 +0100 | [diff] [blame] | 108 | /** Byte Reading Macros |
Joe Subbiani | 6f2bb0c | 2021-06-24 09:06:23 +0100 | [diff] [blame] | 109 | * |
Joe Subbiani | 9ab1866 | 2021-07-21 16:35:48 +0100 | [diff] [blame] | 110 | * Given a multi-byte integer \p x, MBEDTLS_BYTE_n retrieves the n-th |
Joe Subbiani | d068785 | 2021-07-21 15:22:47 +0100 | [diff] [blame] | 111 | * byte from x, where byte 0 is the least significant byte. |
Joe Subbiani | 50dde56 | 2021-06-22 15:51:53 +0100 | [diff] [blame] | 112 | */ |
Joe Subbiani | 2194dc4 | 2021-07-14 12:31:31 +0100 | [diff] [blame] | 113 | #define MBEDTLS_BYTE_0( x ) ( (uint8_t) ( ( x ) & 0xff ) ) |
Joe Subbiani | 5ecac21 | 2021-06-24 13:00:03 +0100 | [diff] [blame] | 114 | #define MBEDTLS_BYTE_1( x ) ( (uint8_t) ( ( ( x ) >> 8 ) & 0xff ) ) |
| 115 | #define MBEDTLS_BYTE_2( x ) ( (uint8_t) ( ( ( x ) >> 16 ) & 0xff ) ) |
| 116 | #define MBEDTLS_BYTE_3( x ) ( (uint8_t) ( ( ( x ) >> 24 ) & 0xff ) ) |
Joe Subbiani | 2194dc4 | 2021-07-14 12:31:31 +0100 | [diff] [blame] | 117 | #define MBEDTLS_BYTE_4( x ) ( (uint8_t) ( ( ( x ) >> 32 ) & 0xff ) ) |
| 118 | #define MBEDTLS_BYTE_5( x ) ( (uint8_t) ( ( ( x ) >> 40 ) & 0xff ) ) |
| 119 | #define MBEDTLS_BYTE_6( x ) ( (uint8_t) ( ( ( x ) >> 48 ) & 0xff ) ) |
| 120 | #define MBEDTLS_BYTE_7( x ) ( (uint8_t) ( ( ( x ) >> 56 ) & 0xff ) ) |
Joe Subbiani | cd84d76 | 2021-07-08 14:59:52 +0100 | [diff] [blame] | 121 | |
Joe Subbiani | 394bdd6 | 2021-07-07 15:16:56 +0100 | [diff] [blame] | 122 | /** |
Joe Subbiani | f5462d9 | 2021-07-13 12:13:19 +0100 | [diff] [blame] | 123 | * Get the unsigned 32 bits integer corresponding to four bytes in |
Joe Subbiani | 635231a | 2021-07-14 11:53:07 +0100 | [diff] [blame] | 124 | * big-endian order (MSB first). |
Joe Subbiani | 394bdd6 | 2021-07-07 15:16:56 +0100 | [diff] [blame] | 125 | * |
Joe Subbiani | 635231a | 2021-07-14 11:53:07 +0100 | [diff] [blame] | 126 | * \param data Base address of the memory to get the four bytes from. |
Jerry Yu | 29287a4 | 2021-10-28 10:26:13 +0800 | [diff] [blame] | 127 | * \param offset Offset from \p data of the first and most significant |
Joe Subbiani | f5462d9 | 2021-07-13 12:13:19 +0100 | [diff] [blame] | 128 | * byte of the four bytes to build the 32 bits unsigned |
Joe Subbiani | 635231a | 2021-07-14 11:53:07 +0100 | [diff] [blame] | 129 | * integer from. |
Joe Subbiani | 6a50631 | 2021-07-07 16:56:29 +0100 | [diff] [blame] | 130 | */ |
| 131 | #ifndef MBEDTLS_GET_UINT32_BE |
Joe Subbiani | 5241e34 | 2021-07-19 15:29:18 +0100 | [diff] [blame] | 132 | #define MBEDTLS_GET_UINT32_BE( data , offset ) \ |
| 133 | ( \ |
| 134 | ( (uint32_t) ( data )[( offset ) ] << 24 ) \ |
| 135 | | ( (uint32_t) ( data )[( offset ) + 1] << 16 ) \ |
| 136 | | ( (uint32_t) ( data )[( offset ) + 2] << 8 ) \ |
| 137 | | ( (uint32_t) ( data )[( offset ) + 3] ) \ |
Joe Subbiani | 6a50631 | 2021-07-07 16:56:29 +0100 | [diff] [blame] | 138 | ) |
| 139 | #endif |
| 140 | |
| 141 | /** |
Joe Subbiani | 635231a | 2021-07-14 11:53:07 +0100 | [diff] [blame] | 142 | * Put in memory a 32 bits unsigned integer in big-endian order. |
Joe Subbiani | 6a50631 | 2021-07-07 16:56:29 +0100 | [diff] [blame] | 143 | * |
Joe Subbiani | f5462d9 | 2021-07-13 12:13:19 +0100 | [diff] [blame] | 144 | * \param n 32 bits unsigned integer to put in memory. |
| 145 | * \param data Base address of the memory where to put the 32 |
Joe Subbiani | 635231a | 2021-07-14 11:53:07 +0100 | [diff] [blame] | 146 | * bits unsigned integer in. |
Jerry Yu | 29287a4 | 2021-10-28 10:26:13 +0800 | [diff] [blame] | 147 | * \param offset Offset from \p data where to put the most significant |
Joe Subbiani | 635231a | 2021-07-14 11:53:07 +0100 | [diff] [blame] | 148 | * byte of the 32 bits unsigned integer \p n. |
Joe Subbiani | 394bdd6 | 2021-07-07 15:16:56 +0100 | [diff] [blame] | 149 | */ |
Joe Subbiani | 5ecac21 | 2021-06-24 13:00:03 +0100 | [diff] [blame] | 150 | #ifndef MBEDTLS_PUT_UINT32_BE |
Joe Subbiani | 5241e34 | 2021-07-19 15:29:18 +0100 | [diff] [blame] | 151 | #define MBEDTLS_PUT_UINT32_BE( n, data, offset ) \ |
| 152 | { \ |
| 153 | ( data )[( offset ) ] = MBEDTLS_BYTE_3( n ); \ |
| 154 | ( data )[( offset ) + 1] = MBEDTLS_BYTE_2( n ); \ |
| 155 | ( data )[( offset ) + 2] = MBEDTLS_BYTE_1( n ); \ |
| 156 | ( data )[( offset ) + 3] = MBEDTLS_BYTE_0( n ); \ |
| 157 | } |
Joe Subbiani | 30d974c | 2021-06-23 11:49:03 +0100 | [diff] [blame] | 158 | #endif |
| 159 | |
Joe Subbiani | 394bdd6 | 2021-07-07 15:16:56 +0100 | [diff] [blame] | 160 | /** |
Joe Subbiani | f5462d9 | 2021-07-13 12:13:19 +0100 | [diff] [blame] | 161 | * Get the unsigned 32 bits integer corresponding to four bytes in |
Joe Subbiani | 635231a | 2021-07-14 11:53:07 +0100 | [diff] [blame] | 162 | * little-endian order (LSB first). |
Joe Subbiani | 6a50631 | 2021-07-07 16:56:29 +0100 | [diff] [blame] | 163 | * |
Joe Subbiani | 635231a | 2021-07-14 11:53:07 +0100 | [diff] [blame] | 164 | * \param data Base address of the memory to get the four bytes from. |
Jerry Yu | 29287a4 | 2021-10-28 10:26:13 +0800 | [diff] [blame] | 165 | * \param offset Offset from \p data of the first and least significant |
Joe Subbiani | f5462d9 | 2021-07-13 12:13:19 +0100 | [diff] [blame] | 166 | * byte of the four bytes to build the 32 bits unsigned |
Joe Subbiani | 635231a | 2021-07-14 11:53:07 +0100 | [diff] [blame] | 167 | * integer from. |
Joe Subbiani | 54c6134 | 2021-06-23 12:16:47 +0100 | [diff] [blame] | 168 | */ |
Joe Subbiani | 5ecac21 | 2021-06-24 13:00:03 +0100 | [diff] [blame] | 169 | #ifndef MBEDTLS_GET_UINT32_LE |
Joe Subbiani | 5241e34 | 2021-07-19 15:29:18 +0100 | [diff] [blame] | 170 | #define MBEDTLS_GET_UINT32_LE( data, offset ) \ |
| 171 | ( \ |
| 172 | ( (uint32_t) ( data )[( offset ) ] ) \ |
| 173 | | ( (uint32_t) ( data )[( offset ) + 1] << 8 ) \ |
| 174 | | ( (uint32_t) ( data )[( offset ) + 2] << 16 ) \ |
| 175 | | ( (uint32_t) ( data )[( offset ) + 3] << 24 ) \ |
Joe Subbiani | 6a50631 | 2021-07-07 16:56:29 +0100 | [diff] [blame] | 176 | ) |
Joe Subbiani | 54c6134 | 2021-06-23 12:16:47 +0100 | [diff] [blame] | 177 | #endif |
| 178 | |
Joe Subbiani | 6a50631 | 2021-07-07 16:56:29 +0100 | [diff] [blame] | 179 | /** |
Joe Subbiani | 635231a | 2021-07-14 11:53:07 +0100 | [diff] [blame] | 180 | * Put in memory a 32 bits unsigned integer in little-endian order. |
Joe Subbiani | 6a50631 | 2021-07-07 16:56:29 +0100 | [diff] [blame] | 181 | * |
Joe Subbiani | f5462d9 | 2021-07-13 12:13:19 +0100 | [diff] [blame] | 182 | * \param n 32 bits unsigned integer to put in memory. |
| 183 | * \param data Base address of the memory where to put the 32 |
Joe Subbiani | 635231a | 2021-07-14 11:53:07 +0100 | [diff] [blame] | 184 | * bits unsigned integer in. |
Jerry Yu | 29287a4 | 2021-10-28 10:26:13 +0800 | [diff] [blame] | 185 | * \param offset Offset from \p data where to put the least significant |
Joe Subbiani | 635231a | 2021-07-14 11:53:07 +0100 | [diff] [blame] | 186 | * byte of the 32 bits unsigned integer \p n. |
Joe Subbiani | 6a50631 | 2021-07-07 16:56:29 +0100 | [diff] [blame] | 187 | */ |
Joe Subbiani | 5ecac21 | 2021-06-24 13:00:03 +0100 | [diff] [blame] | 188 | #ifndef MBEDTLS_PUT_UINT32_LE |
Joe Subbiani | 5241e34 | 2021-07-19 15:29:18 +0100 | [diff] [blame] | 189 | #define MBEDTLS_PUT_UINT32_LE( n, data, offset ) \ |
| 190 | { \ |
| 191 | ( data )[( offset ) ] = MBEDTLS_BYTE_0( n ); \ |
| 192 | ( data )[( offset ) + 1] = MBEDTLS_BYTE_1( n ); \ |
| 193 | ( data )[( offset ) + 2] = MBEDTLS_BYTE_2( n ); \ |
| 194 | ( data )[( offset ) + 3] = MBEDTLS_BYTE_3( n ); \ |
| 195 | } |
Joe Subbiani | 54c6134 | 2021-06-23 12:16:47 +0100 | [diff] [blame] | 196 | #endif |
| 197 | |
Joe Subbiani | 6f2bb0c | 2021-06-24 09:06:23 +0100 | [diff] [blame] | 198 | /** |
Joe Subbiani | bf7ea84 | 2021-07-14 12:05:51 +0100 | [diff] [blame] | 199 | * Get the unsigned 16 bits integer corresponding to two bytes in |
Joe Subbiani | 635231a | 2021-07-14 11:53:07 +0100 | [diff] [blame] | 200 | * little-endian order (LSB first). |
Joe Subbiani | 6a50631 | 2021-07-07 16:56:29 +0100 | [diff] [blame] | 201 | * |
Joe Subbiani | bf7ea84 | 2021-07-14 12:05:51 +0100 | [diff] [blame] | 202 | * \param data Base address of the memory to get the two bytes from. |
Jerry Yu | 29287a4 | 2021-10-28 10:26:13 +0800 | [diff] [blame] | 203 | * \param offset Offset from \p data of the first and least significant |
Joe Subbiani | bf7ea84 | 2021-07-14 12:05:51 +0100 | [diff] [blame] | 204 | * byte of the two bytes to build the 16 bits unsigned |
Joe Subbiani | 635231a | 2021-07-14 11:53:07 +0100 | [diff] [blame] | 205 | * integer from. |
Joe Subbiani | 3b39450 | 2021-06-23 11:23:44 +0100 | [diff] [blame] | 206 | */ |
Joe Subbiani | 6a50631 | 2021-07-07 16:56:29 +0100 | [diff] [blame] | 207 | #ifndef MBEDTLS_GET_UINT16_LE |
Joe Subbiani | 5241e34 | 2021-07-19 15:29:18 +0100 | [diff] [blame] | 208 | #define MBEDTLS_GET_UINT16_LE( data, offset ) \ |
| 209 | ( \ |
| 210 | ( (uint16_t) ( data )[( offset ) ] ) \ |
| 211 | | ( (uint16_t) ( data )[( offset ) + 1] << 8 ) \ |
Joe Subbiani | 3b39450 | 2021-06-23 11:23:44 +0100 | [diff] [blame] | 212 | ) |
Joe Subbiani | 6a50631 | 2021-07-07 16:56:29 +0100 | [diff] [blame] | 213 | #endif |
Joe Subbiani | 3b39450 | 2021-06-23 11:23:44 +0100 | [diff] [blame] | 214 | |
Joe Subbiani | 394bdd6 | 2021-07-07 15:16:56 +0100 | [diff] [blame] | 215 | /** |
Joe Subbiani | 635231a | 2021-07-14 11:53:07 +0100 | [diff] [blame] | 216 | * Put in memory a 16 bits unsigned integer in little-endian order. |
Joe Subbiani | 394bdd6 | 2021-07-07 15:16:56 +0100 | [diff] [blame] | 217 | * |
Joe Subbiani | f5462d9 | 2021-07-13 12:13:19 +0100 | [diff] [blame] | 218 | * \param n 16 bits unsigned integer to put in memory. |
| 219 | * \param data Base address of the memory where to put the 16 |
Joe Subbiani | 635231a | 2021-07-14 11:53:07 +0100 | [diff] [blame] | 220 | * bits unsigned integer in. |
Jerry Yu | 29287a4 | 2021-10-28 10:26:13 +0800 | [diff] [blame] | 221 | * \param offset Offset from \p data where to put the least significant |
Joe Subbiani | 635231a | 2021-07-14 11:53:07 +0100 | [diff] [blame] | 222 | * byte of the 16 bits unsigned integer \p n. |
Joe Subbiani | 394bdd6 | 2021-07-07 15:16:56 +0100 | [diff] [blame] | 223 | */ |
Joe Subbiani | 9fa9ac3 | 2021-07-05 15:37:39 +0100 | [diff] [blame] | 224 | #ifndef MBEDTLS_PUT_UINT16_LE |
Joe Subbiani | 5241e34 | 2021-07-19 15:29:18 +0100 | [diff] [blame] | 225 | #define MBEDTLS_PUT_UINT16_LE( n, data, offset ) \ |
| 226 | { \ |
| 227 | ( data )[( offset ) ] = MBEDTLS_BYTE_0( n ); \ |
| 228 | ( data )[( offset ) + 1] = MBEDTLS_BYTE_1( n ); \ |
Joe Subbiani | 9fa9ac3 | 2021-07-05 15:37:39 +0100 | [diff] [blame] | 229 | } |
| 230 | #endif |
| 231 | |
Joe Subbiani | 99edd6c | 2021-07-16 12:29:49 +0100 | [diff] [blame] | 232 | /** |
Joe Subbiani | 6dd7364 | 2021-07-19 11:56:54 +0100 | [diff] [blame] | 233 | * Get the unsigned 16 bits integer corresponding to two bytes in |
Joe Subbiani | 5241e34 | 2021-07-19 15:29:18 +0100 | [diff] [blame] | 234 | * big-endian order (MSB first). |
Joe Subbiani | 6dd7364 | 2021-07-19 11:56:54 +0100 | [diff] [blame] | 235 | * |
| 236 | * \param data Base address of the memory to get the two bytes from. |
Jerry Yu | 29287a4 | 2021-10-28 10:26:13 +0800 | [diff] [blame] | 237 | * \param offset Offset from \p data of the first and most significant |
Joe Subbiani | 6dd7364 | 2021-07-19 11:56:54 +0100 | [diff] [blame] | 238 | * byte of the two bytes to build the 16 bits unsigned |
| 239 | * integer from. |
| 240 | */ |
| 241 | #ifndef MBEDTLS_GET_UINT16_BE |
Joe Subbiani | 5241e34 | 2021-07-19 15:29:18 +0100 | [diff] [blame] | 242 | #define MBEDTLS_GET_UINT16_BE( data, offset ) \ |
| 243 | ( \ |
| 244 | ( (uint16_t) ( data )[( offset ) ] << 8 ) \ |
| 245 | | ( (uint16_t) ( data )[( offset ) + 1] ) \ |
Joe Subbiani | 6dd7364 | 2021-07-19 11:56:54 +0100 | [diff] [blame] | 246 | ) |
| 247 | #endif |
| 248 | |
| 249 | /** |
| 250 | * Put in memory a 16 bits unsigned integer in big-endian order. |
| 251 | * |
| 252 | * \param n 16 bits unsigned integer to put in memory. |
| 253 | * \param data Base address of the memory where to put the 16 |
| 254 | * bits unsigned integer in. |
Jerry Yu | 29287a4 | 2021-10-28 10:26:13 +0800 | [diff] [blame] | 255 | * \param offset Offset from \p data where to put the most significant |
Joe Subbiani | 6dd7364 | 2021-07-19 11:56:54 +0100 | [diff] [blame] | 256 | * byte of the 16 bits unsigned integer \p n. |
| 257 | */ |
| 258 | #ifndef MBEDTLS_PUT_UINT16_BE |
Joe Subbiani | 5241e34 | 2021-07-19 15:29:18 +0100 | [diff] [blame] | 259 | #define MBEDTLS_PUT_UINT16_BE( n, data, offset ) \ |
| 260 | { \ |
| 261 | ( data )[( offset ) ] = MBEDTLS_BYTE_1( n ); \ |
| 262 | ( data )[( offset ) + 1] = MBEDTLS_BYTE_0( n ); \ |
Joe Subbiani | 6dd7364 | 2021-07-19 11:56:54 +0100 | [diff] [blame] | 263 | } |
| 264 | #endif |
| 265 | |
| 266 | /** |
Jerry Yu | f3f5c21 | 2021-10-27 17:05:49 +0800 | [diff] [blame] | 267 | * Get the unsigned 24 bits integer corresponding to three bytes in |
Jerry Yu | 643d116 | 2021-10-27 13:52:04 +0800 | [diff] [blame] | 268 | * big-endian order (MSB first). |
| 269 | * |
Jerry Yu | f3f5c21 | 2021-10-27 17:05:49 +0800 | [diff] [blame] | 270 | * \param data Base address of the memory to get the three bytes from. |
| 271 | * \param offset Offset from \p data of the first and most significant |
| 272 | * byte of the three bytes to build the 24 bits unsigned |
Jerry Yu | 643d116 | 2021-10-27 13:52:04 +0800 | [diff] [blame] | 273 | * integer from. |
| 274 | */ |
| 275 | #ifndef MBEDTLS_GET_UINT24_BE |
| 276 | #define MBEDTLS_GET_UINT24_BE( data , offset ) \ |
| 277 | ( \ |
| 278 | ( (uint32_t) ( data )[( offset ) ] << 16 ) \ |
| 279 | | ( (uint32_t) ( data )[( offset ) + 1] << 8 ) \ |
| 280 | | ( (uint32_t) ( data )[( offset ) + 2] ) \ |
| 281 | ) |
| 282 | #endif |
| 283 | |
| 284 | /** |
| 285 | * Put in memory a 24 bits unsigned integer in big-endian order. |
| 286 | * |
| 287 | * \param n 24 bits unsigned integer to put in memory. |
| 288 | * \param data Base address of the memory where to put the 24 |
| 289 | * bits unsigned integer in. |
Jerry Yu | f3f5c21 | 2021-10-27 17:05:49 +0800 | [diff] [blame] | 290 | * \param offset Offset from \p data where to put the most significant |
Jerry Yu | 643d116 | 2021-10-27 13:52:04 +0800 | [diff] [blame] | 291 | * byte of the 24 bits unsigned integer \p n. |
| 292 | */ |
| 293 | #ifndef MBEDTLS_PUT_UINT24_BE |
| 294 | #define MBEDTLS_PUT_UINT24_BE( n, data, offset ) \ |
| 295 | { \ |
| 296 | ( data )[( offset ) ] = MBEDTLS_BYTE_2( n ); \ |
| 297 | ( data )[( offset ) + 1] = MBEDTLS_BYTE_1( n ); \ |
| 298 | ( data )[( offset ) + 2] = MBEDTLS_BYTE_0( n ); \ |
| 299 | } |
| 300 | #endif |
| 301 | |
| 302 | /** |
Jerry Yu | f3f5c21 | 2021-10-27 17:05:49 +0800 | [diff] [blame] | 303 | * Get the unsigned 24 bits integer corresponding to three bytes in |
Jerry Yu | 643d116 | 2021-10-27 13:52:04 +0800 | [diff] [blame] | 304 | * little-endian order (LSB first). |
| 305 | * |
Jerry Yu | f3f5c21 | 2021-10-27 17:05:49 +0800 | [diff] [blame] | 306 | * \param data Base address of the memory to get the three bytes from. |
| 307 | * \param offset Offset from \p data of the first and least significant |
| 308 | * byte of the three bytes to build the 24 bits unsigned |
Jerry Yu | 643d116 | 2021-10-27 13:52:04 +0800 | [diff] [blame] | 309 | * integer from. |
| 310 | */ |
| 311 | #ifndef MBEDTLS_GET_UINT24_LE |
| 312 | #define MBEDTLS_GET_UINT24_LE( data, offset ) \ |
| 313 | ( \ |
| 314 | ( (uint32_t) ( data )[( offset ) ] ) \ |
| 315 | | ( (uint32_t) ( data )[( offset ) + 1] << 8 ) \ |
| 316 | | ( (uint32_t) ( data )[( offset ) + 2] << 16 ) \ |
| 317 | ) |
| 318 | #endif |
| 319 | |
| 320 | /** |
| 321 | * Put in memory a 24 bits unsigned integer in little-endian order. |
| 322 | * |
| 323 | * \param n 24 bits unsigned integer to put in memory. |
| 324 | * \param data Base address of the memory where to put the 24 |
| 325 | * bits unsigned integer in. |
Jerry Yu | f3f5c21 | 2021-10-27 17:05:49 +0800 | [diff] [blame] | 326 | * \param offset Offset from \p data where to put the least significant |
Jerry Yu | 643d116 | 2021-10-27 13:52:04 +0800 | [diff] [blame] | 327 | * byte of the 24 bits unsigned integer \p n. |
| 328 | */ |
| 329 | #ifndef MBEDTLS_PUT_UINT24_LE |
| 330 | #define MBEDTLS_PUT_UINT24_LE( n, data, offset ) \ |
| 331 | { \ |
| 332 | ( data )[( offset ) ] = MBEDTLS_BYTE_0( n ); \ |
| 333 | ( data )[( offset ) + 1] = MBEDTLS_BYTE_1( n ); \ |
| 334 | ( data )[( offset ) + 2] = MBEDTLS_BYTE_2( n ); \ |
| 335 | } |
| 336 | #endif |
| 337 | |
| 338 | /** |
Joe Subbiani | 99edd6c | 2021-07-16 12:29:49 +0100 | [diff] [blame] | 339 | * Get the unsigned 64 bits integer corresponding to eight bytes in |
| 340 | * big-endian order (MSB first). |
| 341 | * |
| 342 | * \param data Base address of the memory to get the eight bytes from. |
Jerry Yu | 29287a4 | 2021-10-28 10:26:13 +0800 | [diff] [blame] | 343 | * \param offset Offset from \p data of the first and most significant |
Joe Subbiani | 99edd6c | 2021-07-16 12:29:49 +0100 | [diff] [blame] | 344 | * byte of the eight bytes to build the 64 bits unsigned |
| 345 | * integer from. |
| 346 | */ |
| 347 | #ifndef MBEDTLS_GET_UINT64_BE |
Joe Subbiani | 5241e34 | 2021-07-19 15:29:18 +0100 | [diff] [blame] | 348 | #define MBEDTLS_GET_UINT64_BE( data, offset ) \ |
| 349 | ( \ |
| 350 | ( (uint64_t) ( data )[( offset ) ] << 56 ) \ |
| 351 | | ( (uint64_t) ( data )[( offset ) + 1] << 48 ) \ |
| 352 | | ( (uint64_t) ( data )[( offset ) + 2] << 40 ) \ |
| 353 | | ( (uint64_t) ( data )[( offset ) + 3] << 32 ) \ |
| 354 | | ( (uint64_t) ( data )[( offset ) + 4] << 24 ) \ |
| 355 | | ( (uint64_t) ( data )[( offset ) + 5] << 16 ) \ |
| 356 | | ( (uint64_t) ( data )[( offset ) + 6] << 8 ) \ |
| 357 | | ( (uint64_t) ( data )[( offset ) + 7] ) \ |
Joe Subbiani | 99edd6c | 2021-07-16 12:29:49 +0100 | [diff] [blame] | 358 | ) |
| 359 | #endif |
| 360 | |
| 361 | /** |
| 362 | * Put in memory a 64 bits unsigned integer in big-endian order. |
| 363 | * |
| 364 | * \param n 64 bits unsigned integer to put in memory. |
| 365 | * \param data Base address of the memory where to put the 64 |
| 366 | * bits unsigned integer in. |
Jerry Yu | 29287a4 | 2021-10-28 10:26:13 +0800 | [diff] [blame] | 367 | * \param offset Offset from \p data where to put the most significant |
Joe Subbiani | 99edd6c | 2021-07-16 12:29:49 +0100 | [diff] [blame] | 368 | * byte of the 64 bits unsigned integer \p n. |
| 369 | */ |
| 370 | #ifndef MBEDTLS_PUT_UINT64_BE |
Joe Subbiani | 5241e34 | 2021-07-19 15:29:18 +0100 | [diff] [blame] | 371 | #define MBEDTLS_PUT_UINT64_BE( n, data, offset ) \ |
| 372 | { \ |
| 373 | ( data )[( offset ) ] = MBEDTLS_BYTE_7( n ); \ |
| 374 | ( data )[( offset ) + 1] = MBEDTLS_BYTE_6( n ); \ |
| 375 | ( data )[( offset ) + 2] = MBEDTLS_BYTE_5( n ); \ |
| 376 | ( data )[( offset ) + 3] = MBEDTLS_BYTE_4( n ); \ |
| 377 | ( data )[( offset ) + 4] = MBEDTLS_BYTE_3( n ); \ |
| 378 | ( data )[( offset ) + 5] = MBEDTLS_BYTE_2( n ); \ |
| 379 | ( data )[( offset ) + 6] = MBEDTLS_BYTE_1( n ); \ |
| 380 | ( data )[( offset ) + 7] = MBEDTLS_BYTE_0( n ); \ |
Joe Subbiani | 99edd6c | 2021-07-16 12:29:49 +0100 | [diff] [blame] | 381 | } |
| 382 | #endif |
| 383 | |
| 384 | /** |
| 385 | * Get the unsigned 64 bits integer corresponding to eight bytes in |
| 386 | * little-endian order (LSB first). |
| 387 | * |
| 388 | * \param data Base address of the memory to get the eight bytes from. |
Jerry Yu | 29287a4 | 2021-10-28 10:26:13 +0800 | [diff] [blame] | 389 | * \param offset Offset from \p data of the first and least significant |
Joe Subbiani | 99edd6c | 2021-07-16 12:29:49 +0100 | [diff] [blame] | 390 | * byte of the eight bytes to build the 64 bits unsigned |
| 391 | * integer from. |
| 392 | */ |
| 393 | #ifndef MBEDTLS_GET_UINT64_LE |
Joe Subbiani | 5241e34 | 2021-07-19 15:29:18 +0100 | [diff] [blame] | 394 | #define MBEDTLS_GET_UINT64_LE( data, offset ) \ |
| 395 | ( \ |
| 396 | ( (uint64_t) ( data )[( offset ) + 7] << 56 ) \ |
| 397 | | ( (uint64_t) ( data )[( offset ) + 6] << 48 ) \ |
| 398 | | ( (uint64_t) ( data )[( offset ) + 5] << 40 ) \ |
| 399 | | ( (uint64_t) ( data )[( offset ) + 4] << 32 ) \ |
| 400 | | ( (uint64_t) ( data )[( offset ) + 3] << 24 ) \ |
| 401 | | ( (uint64_t) ( data )[( offset ) + 2] << 16 ) \ |
| 402 | | ( (uint64_t) ( data )[( offset ) + 1] << 8 ) \ |
| 403 | | ( (uint64_t) ( data )[( offset ) ] ) \ |
Joe Subbiani | 99edd6c | 2021-07-16 12:29:49 +0100 | [diff] [blame] | 404 | ) |
| 405 | #endif |
| 406 | |
| 407 | /** |
| 408 | * Put in memory a 64 bits unsigned integer in little-endian order. |
| 409 | * |
| 410 | * \param n 64 bits unsigned integer to put in memory. |
| 411 | * \param data Base address of the memory where to put the 64 |
| 412 | * bits unsigned integer in. |
Jerry Yu | 29287a4 | 2021-10-28 10:26:13 +0800 | [diff] [blame] | 413 | * \param offset Offset from \p data where to put the least significant |
Joe Subbiani | 99edd6c | 2021-07-16 12:29:49 +0100 | [diff] [blame] | 414 | * byte of the 64 bits unsigned integer \p n. |
| 415 | */ |
| 416 | #ifndef MBEDTLS_PUT_UINT64_LE |
Joe Subbiani | 5241e34 | 2021-07-19 15:29:18 +0100 | [diff] [blame] | 417 | #define MBEDTLS_PUT_UINT64_LE( n, data, offset ) \ |
| 418 | { \ |
| 419 | ( data )[( offset ) ] = MBEDTLS_BYTE_0( n ); \ |
| 420 | ( data )[( offset ) + 1] = MBEDTLS_BYTE_1( n ); \ |
| 421 | ( data )[( offset ) + 2] = MBEDTLS_BYTE_2( n ); \ |
| 422 | ( data )[( offset ) + 3] = MBEDTLS_BYTE_3( n ); \ |
| 423 | ( data )[( offset ) + 4] = MBEDTLS_BYTE_4( n ); \ |
| 424 | ( data )[( offset ) + 5] = MBEDTLS_BYTE_5( n ); \ |
| 425 | ( data )[( offset ) + 6] = MBEDTLS_BYTE_6( n ); \ |
| 426 | ( data )[( offset ) + 7] = MBEDTLS_BYTE_7( n ); \ |
Joe Subbiani | 99edd6c | 2021-07-16 12:29:49 +0100 | [diff] [blame] | 427 | } |
| 428 | #endif |
Joe Subbiani | 9fa9ac3 | 2021-07-05 15:37:39 +0100 | [diff] [blame] | 429 | |
Jerry Yu | 6c98352 | 2021-09-24 12:45:36 +0800 | [diff] [blame] | 430 | /* Fix MSVC C99 compatible issue |
| 431 | * MSVC support __func__ from visual studio 2015( 1900 ) |
| 432 | * Use MSVC predefine macro to avoid name check fail. |
| 433 | */ |
| 434 | #if (defined(_MSC_VER) && ( _MSC_VER <= 1900 )) |
Jerry Yu | d52398d | 2021-09-28 16:13:44 +0800 | [diff] [blame] | 435 | #define /*no-check-names*/ __func__ __FUNCTION__ |
Jerry Yu | 6c98352 | 2021-09-24 12:45:36 +0800 | [diff] [blame] | 436 | #endif |
| 437 | |
Gilles Peskine | c4672fd | 2019-09-11 13:39:11 +0200 | [diff] [blame] | 438 | #endif /* MBEDTLS_LIBRARY_COMMON_H */ |