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