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