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