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