Dave Rodgman | fbc2322 | 2022-11-24 18:07:37 +0000 | [diff] [blame] | 1 | /** |
| 2 | * \file alignment.h |
| 3 | * |
| 4 | * \brief Utility code for dealing with unaligned memory accesses |
| 5 | */ |
| 6 | /* |
| 7 | * Copyright The Mbed TLS Contributors |
Dave Rodgman | 16799db | 2023-11-02 19:47:20 +0000 | [diff] [blame] | 8 | * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
Dave Rodgman | fbc2322 | 2022-11-24 18:07:37 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | #ifndef MBEDTLS_LIBRARY_ALIGNMENT_H |
| 12 | #define MBEDTLS_LIBRARY_ALIGNMENT_H |
| 13 | |
| 14 | #include <stdint.h> |
Dave Rodgman | 96d61d1 | 2022-11-24 19:33:22 +0000 | [diff] [blame] | 15 | #include <string.h> |
Dave Rodgman | f7f1f74 | 2022-11-28 14:52:45 +0000 | [diff] [blame] | 16 | #include <stdlib.h> |
Dave Rodgman | fbc2322 | 2022-11-24 18:07:37 +0000 | [diff] [blame] | 17 | |
Dave Rodgman | 7d8c99a | 2024-01-19 14:02:58 +0000 | [diff] [blame^] | 18 | #if defined(__GNUC__) && !defined(__ARMCC_VERSION) && !defined(__clang__) \ |
| 19 | && !defined(__llvm__) && !defined(__INTEL_COMPILER) |
| 20 | /* Defined if the compiler really is gcc and not clang, etc */ |
| 21 | #define MBEDTLS_COMPILER_IS_GCC |
| 22 | #define MBEDTLS_GCC_VERSION \ |
| 23 | (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) |
| 24 | #endif |
| 25 | |
Dave Rodgman | b9cd19b | 2022-12-30 21:32:03 +0000 | [diff] [blame] | 26 | /* |
| 27 | * Define MBEDTLS_EFFICIENT_UNALIGNED_ACCESS for architectures where unaligned memory |
Dave Rodgman | 7f376fa | 2023-01-05 12:25:15 +0000 | [diff] [blame] | 28 | * accesses are known to be efficient. |
| 29 | * |
| 30 | * All functions defined here will behave correctly regardless, but might be less |
| 31 | * efficient when this is not defined. |
Dave Rodgman | b9cd19b | 2022-12-30 21:32:03 +0000 | [diff] [blame] | 32 | */ |
| 33 | #if defined(__ARM_FEATURE_UNALIGNED) \ |
Dave Rodgman | c5cc727 | 2023-09-15 11:41:17 +0100 | [diff] [blame] | 34 | || defined(MBEDTLS_ARCH_IS_X86) || defined(MBEDTLS_ARCH_IS_X64) \ |
Dave Rodgman | 0a48717 | 2023-09-15 11:52:06 +0100 | [diff] [blame] | 35 | || defined(MBEDTLS_PLATFORM_IS_WINDOWS_ON_ARM64) |
Dave Rodgman | b9cd19b | 2022-12-30 21:32:03 +0000 | [diff] [blame] | 36 | /* |
| 37 | * __ARM_FEATURE_UNALIGNED is defined where appropriate by armcc, gcc 7, clang 9 |
Dave Rodgman | 7f376fa | 2023-01-05 12:25:15 +0000 | [diff] [blame] | 38 | * (and later versions) for Arm v7 and later; all x86 platforms should have |
| 39 | * efficient unaligned access. |
Dave Rodgman | 78fc0bd | 2023-08-08 10:36:15 +0100 | [diff] [blame] | 40 | * |
| 41 | * https://learn.microsoft.com/en-us/cpp/build/arm64-windows-abi-conventions?view=msvc-170#alignment |
| 42 | * specifies that on Windows-on-Arm64, unaligned access is safe (except for uncached |
| 43 | * device memory). |
Dave Rodgman | b9cd19b | 2022-12-30 21:32:03 +0000 | [diff] [blame] | 44 | */ |
| 45 | #define MBEDTLS_EFFICIENT_UNALIGNED_ACCESS |
| 46 | #endif |
| 47 | |
Dave Rodgman | 96d61d1 | 2022-11-24 19:33:22 +0000 | [diff] [blame] | 48 | /** |
Dave Rodgman | a360e19 | 2022-11-28 14:44:05 +0000 | [diff] [blame] | 49 | * Read the unsigned 16 bits integer from the given address, which need not |
| 50 | * be aligned. |
| 51 | * |
| 52 | * \param p pointer to 2 bytes of data |
| 53 | * \return Data at the given address |
| 54 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 55 | inline uint16_t mbedtls_get_unaligned_uint16(const void *p) |
Dave Rodgman | a360e19 | 2022-11-28 14:44:05 +0000 | [diff] [blame] | 56 | { |
| 57 | uint16_t r; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 58 | memcpy(&r, p, sizeof(r)); |
Dave Rodgman | a360e19 | 2022-11-28 14:44:05 +0000 | [diff] [blame] | 59 | return r; |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Write the unsigned 16 bits integer to the given address, which need not |
| 64 | * be aligned. |
| 65 | * |
| 66 | * \param p pointer to 2 bytes of data |
| 67 | * \param x data to write |
| 68 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 69 | inline void mbedtls_put_unaligned_uint16(void *p, uint16_t x) |
Dave Rodgman | a360e19 | 2022-11-28 14:44:05 +0000 | [diff] [blame] | 70 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 71 | memcpy(p, &x, sizeof(x)); |
Dave Rodgman | a360e19 | 2022-11-28 14:44:05 +0000 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | /** |
Dave Rodgman | 96d61d1 | 2022-11-24 19:33:22 +0000 | [diff] [blame] | 75 | * Read the unsigned 32 bits integer from the given address, which need not |
| 76 | * be aligned. |
Dave Rodgman | fbc2322 | 2022-11-24 18:07:37 +0000 | [diff] [blame] | 77 | * |
Dave Rodgman | 96d61d1 | 2022-11-24 19:33:22 +0000 | [diff] [blame] | 78 | * \param p pointer to 4 bytes of data |
Dave Rodgman | 875d238 | 2022-11-24 20:43:15 +0000 | [diff] [blame] | 79 | * \return Data at the given address |
Dave Rodgman | fbc2322 | 2022-11-24 18:07:37 +0000 | [diff] [blame] | 80 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 81 | inline uint32_t mbedtls_get_unaligned_uint32(const void *p) |
Dave Rodgman | 96d61d1 | 2022-11-24 19:33:22 +0000 | [diff] [blame] | 82 | { |
| 83 | uint32_t r; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 84 | memcpy(&r, p, sizeof(r)); |
Dave Rodgman | 96d61d1 | 2022-11-24 19:33:22 +0000 | [diff] [blame] | 85 | return r; |
| 86 | } |
Dave Rodgman | fbc2322 | 2022-11-24 18:07:37 +0000 | [diff] [blame] | 87 | |
Dave Rodgman | 96d61d1 | 2022-11-24 19:33:22 +0000 | [diff] [blame] | 88 | /** |
| 89 | * Write the unsigned 32 bits integer to the given address, which need not |
| 90 | * be aligned. |
| 91 | * |
| 92 | * \param p pointer to 4 bytes of data |
| 93 | * \param x data to write |
| 94 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 95 | inline void mbedtls_put_unaligned_uint32(void *p, uint32_t x) |
Dave Rodgman | 96d61d1 | 2022-11-24 19:33:22 +0000 | [diff] [blame] | 96 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 97 | memcpy(p, &x, sizeof(x)); |
Dave Rodgman | 96d61d1 | 2022-11-24 19:33:22 +0000 | [diff] [blame] | 98 | } |
Dave Rodgman | fbc2322 | 2022-11-24 18:07:37 +0000 | [diff] [blame] | 99 | |
Dave Rodgman | a360e19 | 2022-11-28 14:44:05 +0000 | [diff] [blame] | 100 | /** |
| 101 | * Read the unsigned 64 bits integer from the given address, which need not |
| 102 | * be aligned. |
| 103 | * |
| 104 | * \param p pointer to 8 bytes of data |
| 105 | * \return Data at the given address |
| 106 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 107 | inline uint64_t mbedtls_get_unaligned_uint64(const void *p) |
Dave Rodgman | a360e19 | 2022-11-28 14:44:05 +0000 | [diff] [blame] | 108 | { |
| 109 | uint64_t r; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 110 | memcpy(&r, p, sizeof(r)); |
Dave Rodgman | a360e19 | 2022-11-28 14:44:05 +0000 | [diff] [blame] | 111 | return r; |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Write the unsigned 64 bits integer to the given address, which need not |
| 116 | * be aligned. |
| 117 | * |
| 118 | * \param p pointer to 8 bytes of data |
| 119 | * \param x data to write |
| 120 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 121 | inline void mbedtls_put_unaligned_uint64(void *p, uint64_t x) |
Dave Rodgman | a360e19 | 2022-11-28 14:44:05 +0000 | [diff] [blame] | 122 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 123 | memcpy(p, &x, sizeof(x)); |
Dave Rodgman | a360e19 | 2022-11-28 14:44:05 +0000 | [diff] [blame] | 124 | } |
| 125 | |
Dave Rodgman | fbc2322 | 2022-11-24 18:07:37 +0000 | [diff] [blame] | 126 | /** Byte Reading Macros |
| 127 | * |
| 128 | * Given a multi-byte integer \p x, MBEDTLS_BYTE_n retrieves the n-th |
| 129 | * byte from x, where byte 0 is the least significant byte. |
| 130 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 131 | #define MBEDTLS_BYTE_0(x) ((uint8_t) ((x) & 0xff)) |
Dave Rodgman | 914c632 | 2023-03-01 09:30:14 +0000 | [diff] [blame] | 132 | #define MBEDTLS_BYTE_1(x) ((uint8_t) (((x) >> 8) & 0xff)) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 133 | #define MBEDTLS_BYTE_2(x) ((uint8_t) (((x) >> 16) & 0xff)) |
| 134 | #define MBEDTLS_BYTE_3(x) ((uint8_t) (((x) >> 24) & 0xff)) |
| 135 | #define MBEDTLS_BYTE_4(x) ((uint8_t) (((x) >> 32) & 0xff)) |
| 136 | #define MBEDTLS_BYTE_5(x) ((uint8_t) (((x) >> 40) & 0xff)) |
| 137 | #define MBEDTLS_BYTE_6(x) ((uint8_t) (((x) >> 48) & 0xff)) |
| 138 | #define MBEDTLS_BYTE_7(x) ((uint8_t) (((x) >> 56) & 0xff)) |
Dave Rodgman | fbc2322 | 2022-11-24 18:07:37 +0000 | [diff] [blame] | 139 | |
Dave Rodgman | f7f1f74 | 2022-11-28 14:52:45 +0000 | [diff] [blame] | 140 | /* |
| 141 | * Detect GCC built-in byteswap routines |
| 142 | */ |
| 143 | #if defined(__GNUC__) && defined(__GNUC_PREREQ) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 144 | #if __GNUC_PREREQ(4, 8) |
Dave Rodgman | f7f1f74 | 2022-11-28 14:52:45 +0000 | [diff] [blame] | 145 | #define MBEDTLS_BSWAP16 __builtin_bswap16 |
| 146 | #endif /* __GNUC_PREREQ(4,8) */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 147 | #if __GNUC_PREREQ(4, 3) |
Dave Rodgman | f7f1f74 | 2022-11-28 14:52:45 +0000 | [diff] [blame] | 148 | #define MBEDTLS_BSWAP32 __builtin_bswap32 |
| 149 | #define MBEDTLS_BSWAP64 __builtin_bswap64 |
| 150 | #endif /* __GNUC_PREREQ(4,3) */ |
| 151 | #endif /* defined(__GNUC__) && defined(__GNUC_PREREQ) */ |
| 152 | |
| 153 | /* |
| 154 | * Detect Clang built-in byteswap routines |
| 155 | */ |
| 156 | #if defined(__clang__) && defined(__has_builtin) |
Dave Rodgman | e47899d | 2023-02-28 17:39:03 +0000 | [diff] [blame] | 157 | #if __has_builtin(__builtin_bswap16) && !defined(MBEDTLS_BSWAP16) |
Dave Rodgman | f7f1f74 | 2022-11-28 14:52:45 +0000 | [diff] [blame] | 158 | #define MBEDTLS_BSWAP16 __builtin_bswap16 |
| 159 | #endif /* __has_builtin(__builtin_bswap16) */ |
Dave Rodgman | e47899d | 2023-02-28 17:39:03 +0000 | [diff] [blame] | 160 | #if __has_builtin(__builtin_bswap32) && !defined(MBEDTLS_BSWAP32) |
Dave Rodgman | f7f1f74 | 2022-11-28 14:52:45 +0000 | [diff] [blame] | 161 | #define MBEDTLS_BSWAP32 __builtin_bswap32 |
| 162 | #endif /* __has_builtin(__builtin_bswap32) */ |
Dave Rodgman | e47899d | 2023-02-28 17:39:03 +0000 | [diff] [blame] | 163 | #if __has_builtin(__builtin_bswap64) && !defined(MBEDTLS_BSWAP64) |
Dave Rodgman | f7f1f74 | 2022-11-28 14:52:45 +0000 | [diff] [blame] | 164 | #define MBEDTLS_BSWAP64 __builtin_bswap64 |
| 165 | #endif /* __has_builtin(__builtin_bswap64) */ |
| 166 | #endif /* defined(__clang__) && defined(__has_builtin) */ |
| 167 | |
| 168 | /* |
| 169 | * Detect MSVC built-in byteswap routines |
| 170 | */ |
| 171 | #if defined(_MSC_VER) |
Dave Rodgman | e47899d | 2023-02-28 17:39:03 +0000 | [diff] [blame] | 172 | #if !defined(MBEDTLS_BSWAP16) |
Dave Rodgman | f7f1f74 | 2022-11-28 14:52:45 +0000 | [diff] [blame] | 173 | #define MBEDTLS_BSWAP16 _byteswap_ushort |
Dave Rodgman | e47899d | 2023-02-28 17:39:03 +0000 | [diff] [blame] | 174 | #endif |
| 175 | #if !defined(MBEDTLS_BSWAP32) |
Dave Rodgman | f7f1f74 | 2022-11-28 14:52:45 +0000 | [diff] [blame] | 176 | #define MBEDTLS_BSWAP32 _byteswap_ulong |
Dave Rodgman | e47899d | 2023-02-28 17:39:03 +0000 | [diff] [blame] | 177 | #endif |
| 178 | #if !defined(MBEDTLS_BSWAP64) |
Dave Rodgman | f7f1f74 | 2022-11-28 14:52:45 +0000 | [diff] [blame] | 179 | #define MBEDTLS_BSWAP64 _byteswap_uint64 |
Dave Rodgman | e47899d | 2023-02-28 17:39:03 +0000 | [diff] [blame] | 180 | #endif |
Dave Rodgman | f7f1f74 | 2022-11-28 14:52:45 +0000 | [diff] [blame] | 181 | #endif /* defined(_MSC_VER) */ |
| 182 | |
Dave Rodgman | 2dae4b3 | 2022-11-30 12:07:36 +0000 | [diff] [blame] | 183 | /* Detect armcc built-in byteswap routine */ |
Dave Rodgman | e47899d | 2023-02-28 17:39:03 +0000 | [diff] [blame] | 184 | #if defined(__ARMCC_VERSION) && (__ARMCC_VERSION >= 410000) && !defined(MBEDTLS_BSWAP32) |
Tom Cosgrove | f2b5a13 | 2023-04-26 17:00:12 +0100 | [diff] [blame] | 185 | #if defined(__ARM_ACLE) /* ARM Compiler 6 - earlier versions don't need a header */ |
| 186 | #include <arm_acle.h> |
| 187 | #endif |
Dave Rodgman | 2dae4b3 | 2022-11-30 12:07:36 +0000 | [diff] [blame] | 188 | #define MBEDTLS_BSWAP32 __rev |
| 189 | #endif |
| 190 | |
Dave Rodgman | 650674b | 2023-12-05 12:16:48 +0000 | [diff] [blame] | 191 | /* Detect IAR built-in byteswap routine */ |
| 192 | #if defined(__IAR_SYSTEMS_ICC__) |
| 193 | #if defined(__ARM_ACLE) |
| 194 | #include <arm_acle.h> |
| 195 | #define MBEDTLS_BSWAP16(x) ((uint16_t) __rev16((uint32_t) (x))) |
| 196 | #define MBEDTLS_BSWAP32 __rev |
| 197 | #define MBEDTLS_BSWAP64 __revll |
| 198 | #endif |
| 199 | #endif |
| 200 | |
Dave Rodgman | f7f1f74 | 2022-11-28 14:52:45 +0000 | [diff] [blame] | 201 | /* |
| 202 | * Where compiler built-ins are not present, fall back to C code that the |
| 203 | * compiler may be able to detect and transform into the relevant bswap or |
| 204 | * similar instruction. |
| 205 | */ |
| 206 | #if !defined(MBEDTLS_BSWAP16) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 207 | static inline uint16_t mbedtls_bswap16(uint16_t x) |
| 208 | { |
Dave Rodgman | 6298b24 | 2022-11-28 14:51:49 +0000 | [diff] [blame] | 209 | return |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 210 | (x & 0x00ff) << 8 | |
| 211 | (x & 0xff00) >> 8; |
Dave Rodgman | 6298b24 | 2022-11-28 14:51:49 +0000 | [diff] [blame] | 212 | } |
| 213 | #define MBEDTLS_BSWAP16 mbedtls_bswap16 |
Dave Rodgman | f7f1f74 | 2022-11-28 14:52:45 +0000 | [diff] [blame] | 214 | #endif /* !defined(MBEDTLS_BSWAP16) */ |
Dave Rodgman | 6298b24 | 2022-11-28 14:51:49 +0000 | [diff] [blame] | 215 | |
Dave Rodgman | f7f1f74 | 2022-11-28 14:52:45 +0000 | [diff] [blame] | 216 | #if !defined(MBEDTLS_BSWAP32) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 217 | static inline uint32_t mbedtls_bswap32(uint32_t x) |
| 218 | { |
Dave Rodgman | 6298b24 | 2022-11-28 14:51:49 +0000 | [diff] [blame] | 219 | return |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 220 | (x & 0x000000ff) << 24 | |
| 221 | (x & 0x0000ff00) << 8 | |
| 222 | (x & 0x00ff0000) >> 8 | |
| 223 | (x & 0xff000000) >> 24; |
Dave Rodgman | 6298b24 | 2022-11-28 14:51:49 +0000 | [diff] [blame] | 224 | } |
| 225 | #define MBEDTLS_BSWAP32 mbedtls_bswap32 |
Dave Rodgman | f7f1f74 | 2022-11-28 14:52:45 +0000 | [diff] [blame] | 226 | #endif /* !defined(MBEDTLS_BSWAP32) */ |
Dave Rodgman | 6298b24 | 2022-11-28 14:51:49 +0000 | [diff] [blame] | 227 | |
Dave Rodgman | f7f1f74 | 2022-11-28 14:52:45 +0000 | [diff] [blame] | 228 | #if !defined(MBEDTLS_BSWAP64) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 229 | static inline uint64_t mbedtls_bswap64(uint64_t x) |
| 230 | { |
Dave Rodgman | 6298b24 | 2022-11-28 14:51:49 +0000 | [diff] [blame] | 231 | return |
Tom Cosgrove | bbe166e | 2023-03-08 13:23:24 +0000 | [diff] [blame] | 232 | (x & 0x00000000000000ffULL) << 56 | |
| 233 | (x & 0x000000000000ff00ULL) << 40 | |
| 234 | (x & 0x0000000000ff0000ULL) << 24 | |
| 235 | (x & 0x00000000ff000000ULL) << 8 | |
| 236 | (x & 0x000000ff00000000ULL) >> 8 | |
| 237 | (x & 0x0000ff0000000000ULL) >> 24 | |
| 238 | (x & 0x00ff000000000000ULL) >> 40 | |
| 239 | (x & 0xff00000000000000ULL) >> 56; |
Dave Rodgman | 6298b24 | 2022-11-28 14:51:49 +0000 | [diff] [blame] | 240 | } |
| 241 | #define MBEDTLS_BSWAP64 mbedtls_bswap64 |
Dave Rodgman | f7f1f74 | 2022-11-28 14:52:45 +0000 | [diff] [blame] | 242 | #endif /* !defined(MBEDTLS_BSWAP64) */ |
Dave Rodgman | 6298b24 | 2022-11-28 14:51:49 +0000 | [diff] [blame] | 243 | |
Dave Rodgman | e5c4259 | 2022-11-28 14:47:46 +0000 | [diff] [blame] | 244 | #if !defined(__BYTE_ORDER__) |
Dave Rodgman | f3c04f3 | 2023-12-05 12:06:11 +0000 | [diff] [blame] | 245 | |
| 246 | #if defined(__LITTLE_ENDIAN__) |
| 247 | /* IAR defines __xxx_ENDIAN__, but not __BYTE_ORDER__ */ |
| 248 | #define MBEDTLS_IS_BIG_ENDIAN 0 |
| 249 | #elif defined(__BIG_ENDIAN__) |
| 250 | #define MBEDTLS_IS_BIG_ENDIAN 1 |
| 251 | #else |
Dave Rodgman | e5c4259 | 2022-11-28 14:47:46 +0000 | [diff] [blame] | 252 | static const uint16_t mbedtls_byte_order_detector = { 0x100 }; |
| 253 | #define MBEDTLS_IS_BIG_ENDIAN (*((unsigned char *) (&mbedtls_byte_order_detector)) == 0x01) |
Dave Rodgman | f3c04f3 | 2023-12-05 12:06:11 +0000 | [diff] [blame] | 254 | #endif |
| 255 | |
Dave Rodgman | e5c4259 | 2022-11-28 14:47:46 +0000 | [diff] [blame] | 256 | #else |
Dave Rodgman | f3c04f3 | 2023-12-05 12:06:11 +0000 | [diff] [blame] | 257 | |
| 258 | #if (__BYTE_ORDER__) == (__ORDER_BIG_ENDIAN__) |
| 259 | #define MBEDTLS_IS_BIG_ENDIAN 1 |
| 260 | #else |
| 261 | #define MBEDTLS_IS_BIG_ENDIAN 0 |
| 262 | #endif |
| 263 | |
Dave Rodgman | e5c4259 | 2022-11-28 14:47:46 +0000 | [diff] [blame] | 264 | #endif /* !defined(__BYTE_ORDER__) */ |
| 265 | |
Dave Rodgman | fbc2322 | 2022-11-24 18:07:37 +0000 | [diff] [blame] | 266 | /** |
| 267 | * Get the unsigned 32 bits integer corresponding to four bytes in |
| 268 | * big-endian order (MSB first). |
| 269 | * |
| 270 | * \param data Base address of the memory to get the four bytes from. |
| 271 | * \param offset Offset from \p data of the first and most significant |
| 272 | * byte of the four bytes to build the 32 bits unsigned |
| 273 | * integer from. |
| 274 | */ |
Dave Rodgman | 914c632 | 2023-03-01 09:30:14 +0000 | [diff] [blame] | 275 | #define MBEDTLS_GET_UINT32_BE(data, offset) \ |
| 276 | ((MBEDTLS_IS_BIG_ENDIAN) \ |
Dave Rodgman | a5110b0 | 2022-11-28 14:48:45 +0000 | [diff] [blame] | 277 | ? mbedtls_get_unaligned_uint32((data) + (offset)) \ |
| 278 | : MBEDTLS_BSWAP32(mbedtls_get_unaligned_uint32((data) + (offset))) \ |
Dave Rodgman | fbc2322 | 2022-11-24 18:07:37 +0000 | [diff] [blame] | 279 | ) |
Dave Rodgman | fbc2322 | 2022-11-24 18:07:37 +0000 | [diff] [blame] | 280 | |
| 281 | /** |
| 282 | * Put in memory a 32 bits unsigned integer in big-endian order. |
| 283 | * |
| 284 | * \param n 32 bits unsigned integer to put in memory. |
| 285 | * \param data Base address of the memory where to put the 32 |
| 286 | * bits unsigned integer in. |
| 287 | * \param offset Offset from \p data where to put the most significant |
| 288 | * byte of the 32 bits unsigned integer \p n. |
| 289 | */ |
Dave Rodgman | 914c632 | 2023-03-01 09:30:14 +0000 | [diff] [blame] | 290 | #define MBEDTLS_PUT_UINT32_BE(n, data, offset) \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 291 | { \ |
Dave Rodgman | 914c632 | 2023-03-01 09:30:14 +0000 | [diff] [blame] | 292 | if (MBEDTLS_IS_BIG_ENDIAN) \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 293 | { \ |
Dave Rodgman | 914c632 | 2023-03-01 09:30:14 +0000 | [diff] [blame] | 294 | mbedtls_put_unaligned_uint32((data) + (offset), (uint32_t) (n)); \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 295 | } \ |
| 296 | else \ |
| 297 | { \ |
| 298 | mbedtls_put_unaligned_uint32((data) + (offset), MBEDTLS_BSWAP32((uint32_t) (n))); \ |
| 299 | } \ |
| 300 | } |
Dave Rodgman | fbc2322 | 2022-11-24 18:07:37 +0000 | [diff] [blame] | 301 | |
| 302 | /** |
| 303 | * Get the unsigned 32 bits integer corresponding to four bytes in |
| 304 | * little-endian order (LSB first). |
| 305 | * |
| 306 | * \param data Base address of the memory to get the four bytes from. |
| 307 | * \param offset Offset from \p data of the first and least significant |
| 308 | * byte of the four bytes to build the 32 bits unsigned |
| 309 | * integer from. |
| 310 | */ |
Dave Rodgman | 914c632 | 2023-03-01 09:30:14 +0000 | [diff] [blame] | 311 | #define MBEDTLS_GET_UINT32_LE(data, offset) \ |
| 312 | ((MBEDTLS_IS_BIG_ENDIAN) \ |
Dave Rodgman | a5110b0 | 2022-11-28 14:48:45 +0000 | [diff] [blame] | 313 | ? MBEDTLS_BSWAP32(mbedtls_get_unaligned_uint32((data) + (offset))) \ |
| 314 | : mbedtls_get_unaligned_uint32((data) + (offset)) \ |
Dave Rodgman | fbc2322 | 2022-11-24 18:07:37 +0000 | [diff] [blame] | 315 | ) |
Dave Rodgman | a5110b0 | 2022-11-28 14:48:45 +0000 | [diff] [blame] | 316 | |
Dave Rodgman | fbc2322 | 2022-11-24 18:07:37 +0000 | [diff] [blame] | 317 | |
| 318 | /** |
| 319 | * Put in memory a 32 bits unsigned integer in little-endian order. |
| 320 | * |
| 321 | * \param n 32 bits unsigned integer to put in memory. |
| 322 | * \param data Base address of the memory where to put the 32 |
| 323 | * bits unsigned integer in. |
| 324 | * \param offset Offset from \p data where to put the least significant |
| 325 | * byte of the 32 bits unsigned integer \p n. |
| 326 | */ |
Dave Rodgman | 914c632 | 2023-03-01 09:30:14 +0000 | [diff] [blame] | 327 | #define MBEDTLS_PUT_UINT32_LE(n, data, offset) \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 328 | { \ |
Dave Rodgman | 914c632 | 2023-03-01 09:30:14 +0000 | [diff] [blame] | 329 | if (MBEDTLS_IS_BIG_ENDIAN) \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 330 | { \ |
| 331 | mbedtls_put_unaligned_uint32((data) + (offset), MBEDTLS_BSWAP32((uint32_t) (n))); \ |
| 332 | } \ |
| 333 | else \ |
| 334 | { \ |
Dave Rodgman | 914c632 | 2023-03-01 09:30:14 +0000 | [diff] [blame] | 335 | mbedtls_put_unaligned_uint32((data) + (offset), ((uint32_t) (n))); \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 336 | } \ |
| 337 | } |
Dave Rodgman | fbc2322 | 2022-11-24 18:07:37 +0000 | [diff] [blame] | 338 | |
| 339 | /** |
| 340 | * Get the unsigned 16 bits integer corresponding to two bytes in |
| 341 | * little-endian order (LSB first). |
| 342 | * |
| 343 | * \param data Base address of the memory to get the two bytes from. |
| 344 | * \param offset Offset from \p data of the first and least significant |
| 345 | * byte of the two bytes to build the 16 bits unsigned |
| 346 | * integer from. |
| 347 | */ |
Dave Rodgman | 914c632 | 2023-03-01 09:30:14 +0000 | [diff] [blame] | 348 | #define MBEDTLS_GET_UINT16_LE(data, offset) \ |
| 349 | ((MBEDTLS_IS_BIG_ENDIAN) \ |
Dave Rodgman | a5110b0 | 2022-11-28 14:48:45 +0000 | [diff] [blame] | 350 | ? MBEDTLS_BSWAP16(mbedtls_get_unaligned_uint16((data) + (offset))) \ |
| 351 | : mbedtls_get_unaligned_uint16((data) + (offset)) \ |
Dave Rodgman | fbc2322 | 2022-11-24 18:07:37 +0000 | [diff] [blame] | 352 | ) |
Dave Rodgman | fbc2322 | 2022-11-24 18:07:37 +0000 | [diff] [blame] | 353 | |
| 354 | /** |
| 355 | * Put in memory a 16 bits unsigned integer in little-endian order. |
| 356 | * |
| 357 | * \param n 16 bits unsigned integer to put in memory. |
| 358 | * \param data Base address of the memory where to put the 16 |
| 359 | * bits unsigned integer in. |
| 360 | * \param offset Offset from \p data where to put the least significant |
| 361 | * byte of the 16 bits unsigned integer \p n. |
| 362 | */ |
Dave Rodgman | 914c632 | 2023-03-01 09:30:14 +0000 | [diff] [blame] | 363 | #define MBEDTLS_PUT_UINT16_LE(n, data, offset) \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 364 | { \ |
Dave Rodgman | 914c632 | 2023-03-01 09:30:14 +0000 | [diff] [blame] | 365 | if (MBEDTLS_IS_BIG_ENDIAN) \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 366 | { \ |
| 367 | mbedtls_put_unaligned_uint16((data) + (offset), MBEDTLS_BSWAP16((uint16_t) (n))); \ |
| 368 | } \ |
| 369 | else \ |
| 370 | { \ |
Dave Rodgman | 914c632 | 2023-03-01 09:30:14 +0000 | [diff] [blame] | 371 | mbedtls_put_unaligned_uint16((data) + (offset), (uint16_t) (n)); \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 372 | } \ |
| 373 | } |
Dave Rodgman | fbc2322 | 2022-11-24 18:07:37 +0000 | [diff] [blame] | 374 | |
| 375 | /** |
| 376 | * Get the unsigned 16 bits integer corresponding to two bytes in |
| 377 | * big-endian order (MSB first). |
| 378 | * |
| 379 | * \param data Base address of the memory to get the two bytes from. |
| 380 | * \param offset Offset from \p data of the first and most significant |
| 381 | * byte of the two bytes to build the 16 bits unsigned |
| 382 | * integer from. |
| 383 | */ |
Dave Rodgman | 914c632 | 2023-03-01 09:30:14 +0000 | [diff] [blame] | 384 | #define MBEDTLS_GET_UINT16_BE(data, offset) \ |
| 385 | ((MBEDTLS_IS_BIG_ENDIAN) \ |
Dave Rodgman | a5110b0 | 2022-11-28 14:48:45 +0000 | [diff] [blame] | 386 | ? mbedtls_get_unaligned_uint16((data) + (offset)) \ |
| 387 | : MBEDTLS_BSWAP16(mbedtls_get_unaligned_uint16((data) + (offset))) \ |
Dave Rodgman | fbc2322 | 2022-11-24 18:07:37 +0000 | [diff] [blame] | 388 | ) |
Dave Rodgman | fbc2322 | 2022-11-24 18:07:37 +0000 | [diff] [blame] | 389 | |
| 390 | /** |
| 391 | * Put in memory a 16 bits unsigned integer in big-endian order. |
| 392 | * |
| 393 | * \param n 16 bits unsigned integer to put in memory. |
| 394 | * \param data Base address of the memory where to put the 16 |
| 395 | * bits unsigned integer in. |
| 396 | * \param offset Offset from \p data where to put the most significant |
| 397 | * byte of the 16 bits unsigned integer \p n. |
| 398 | */ |
Dave Rodgman | 914c632 | 2023-03-01 09:30:14 +0000 | [diff] [blame] | 399 | #define MBEDTLS_PUT_UINT16_BE(n, data, offset) \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 400 | { \ |
Dave Rodgman | 914c632 | 2023-03-01 09:30:14 +0000 | [diff] [blame] | 401 | if (MBEDTLS_IS_BIG_ENDIAN) \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 402 | { \ |
Dave Rodgman | 914c632 | 2023-03-01 09:30:14 +0000 | [diff] [blame] | 403 | mbedtls_put_unaligned_uint16((data) + (offset), (uint16_t) (n)); \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 404 | } \ |
| 405 | else \ |
| 406 | { \ |
| 407 | mbedtls_put_unaligned_uint16((data) + (offset), MBEDTLS_BSWAP16((uint16_t) (n))); \ |
| 408 | } \ |
| 409 | } |
Dave Rodgman | fbc2322 | 2022-11-24 18:07:37 +0000 | [diff] [blame] | 410 | |
| 411 | /** |
| 412 | * Get the unsigned 24 bits integer corresponding to three bytes in |
| 413 | * big-endian order (MSB first). |
| 414 | * |
| 415 | * \param data Base address of the memory to get the three bytes from. |
| 416 | * \param offset Offset from \p data of the first and most significant |
| 417 | * byte of the three bytes to build the 24 bits unsigned |
| 418 | * integer from. |
| 419 | */ |
Dave Rodgman | 914c632 | 2023-03-01 09:30:14 +0000 | [diff] [blame] | 420 | #define MBEDTLS_GET_UINT24_BE(data, offset) \ |
| 421 | ( \ |
| 422 | ((uint32_t) (data)[(offset)] << 16) \ |
| 423 | | ((uint32_t) (data)[(offset) + 1] << 8) \ |
| 424 | | ((uint32_t) (data)[(offset) + 2]) \ |
Dave Rodgman | fbc2322 | 2022-11-24 18:07:37 +0000 | [diff] [blame] | 425 | ) |
Dave Rodgman | fbc2322 | 2022-11-24 18:07:37 +0000 | [diff] [blame] | 426 | |
| 427 | /** |
| 428 | * Put in memory a 24 bits unsigned integer in big-endian order. |
| 429 | * |
| 430 | * \param n 24 bits unsigned integer to put in memory. |
| 431 | * \param data Base address of the memory where to put the 24 |
| 432 | * bits unsigned integer in. |
| 433 | * \param offset Offset from \p data where to put the most significant |
| 434 | * byte of the 24 bits unsigned integer \p n. |
| 435 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 436 | #define MBEDTLS_PUT_UINT24_BE(n, data, offset) \ |
Dave Rodgman | 914c632 | 2023-03-01 09:30:14 +0000 | [diff] [blame] | 437 | { \ |
| 438 | (data)[(offset)] = MBEDTLS_BYTE_2(n); \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 439 | (data)[(offset) + 1] = MBEDTLS_BYTE_1(n); \ |
| 440 | (data)[(offset) + 2] = MBEDTLS_BYTE_0(n); \ |
| 441 | } |
Dave Rodgman | fbc2322 | 2022-11-24 18:07:37 +0000 | [diff] [blame] | 442 | |
| 443 | /** |
| 444 | * Get the unsigned 24 bits integer corresponding to three bytes in |
| 445 | * little-endian order (LSB first). |
| 446 | * |
| 447 | * \param data Base address of the memory to get the three bytes from. |
| 448 | * \param offset Offset from \p data of the first and least significant |
| 449 | * byte of the three bytes to build the 24 bits unsigned |
| 450 | * integer from. |
| 451 | */ |
Dave Rodgman | 914c632 | 2023-03-01 09:30:14 +0000 | [diff] [blame] | 452 | #define MBEDTLS_GET_UINT24_LE(data, offset) \ |
| 453 | ( \ |
| 454 | ((uint32_t) (data)[(offset)]) \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 455 | | ((uint32_t) (data)[(offset) + 1] << 8) \ |
| 456 | | ((uint32_t) (data)[(offset) + 2] << 16) \ |
Dave Rodgman | fbc2322 | 2022-11-24 18:07:37 +0000 | [diff] [blame] | 457 | ) |
Dave Rodgman | fbc2322 | 2022-11-24 18:07:37 +0000 | [diff] [blame] | 458 | |
| 459 | /** |
| 460 | * Put in memory a 24 bits unsigned integer in little-endian order. |
| 461 | * |
| 462 | * \param n 24 bits unsigned integer to put in memory. |
| 463 | * \param data Base address of the memory where to put the 24 |
| 464 | * bits unsigned integer in. |
| 465 | * \param offset Offset from \p data where to put the least significant |
| 466 | * byte of the 24 bits unsigned integer \p n. |
| 467 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 468 | #define MBEDTLS_PUT_UINT24_LE(n, data, offset) \ |
Dave Rodgman | 914c632 | 2023-03-01 09:30:14 +0000 | [diff] [blame] | 469 | { \ |
| 470 | (data)[(offset)] = MBEDTLS_BYTE_0(n); \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 471 | (data)[(offset) + 1] = MBEDTLS_BYTE_1(n); \ |
| 472 | (data)[(offset) + 2] = MBEDTLS_BYTE_2(n); \ |
| 473 | } |
Dave Rodgman | fbc2322 | 2022-11-24 18:07:37 +0000 | [diff] [blame] | 474 | |
| 475 | /** |
| 476 | * Get the unsigned 64 bits integer corresponding to eight bytes in |
| 477 | * big-endian order (MSB first). |
| 478 | * |
| 479 | * \param data Base address of the memory to get the eight bytes from. |
| 480 | * \param offset Offset from \p data of the first and most significant |
| 481 | * byte of the eight bytes to build the 64 bits unsigned |
| 482 | * integer from. |
| 483 | */ |
Dave Rodgman | 914c632 | 2023-03-01 09:30:14 +0000 | [diff] [blame] | 484 | #define MBEDTLS_GET_UINT64_BE(data, offset) \ |
| 485 | ((MBEDTLS_IS_BIG_ENDIAN) \ |
Dave Rodgman | a5110b0 | 2022-11-28 14:48:45 +0000 | [diff] [blame] | 486 | ? mbedtls_get_unaligned_uint64((data) + (offset)) \ |
| 487 | : MBEDTLS_BSWAP64(mbedtls_get_unaligned_uint64((data) + (offset))) \ |
Dave Rodgman | fbc2322 | 2022-11-24 18:07:37 +0000 | [diff] [blame] | 488 | ) |
Dave Rodgman | fbc2322 | 2022-11-24 18:07:37 +0000 | [diff] [blame] | 489 | |
| 490 | /** |
| 491 | * Put in memory a 64 bits unsigned integer in big-endian order. |
| 492 | * |
| 493 | * \param n 64 bits unsigned integer to put in memory. |
| 494 | * \param data Base address of the memory where to put the 64 |
| 495 | * bits unsigned integer in. |
| 496 | * \param offset Offset from \p data where to put the most significant |
| 497 | * byte of the 64 bits unsigned integer \p n. |
| 498 | */ |
Dave Rodgman | 914c632 | 2023-03-01 09:30:14 +0000 | [diff] [blame] | 499 | #define MBEDTLS_PUT_UINT64_BE(n, data, offset) \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 500 | { \ |
Dave Rodgman | 914c632 | 2023-03-01 09:30:14 +0000 | [diff] [blame] | 501 | if (MBEDTLS_IS_BIG_ENDIAN) \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 502 | { \ |
Dave Rodgman | 914c632 | 2023-03-01 09:30:14 +0000 | [diff] [blame] | 503 | mbedtls_put_unaligned_uint64((data) + (offset), (uint64_t) (n)); \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 504 | } \ |
| 505 | else \ |
| 506 | { \ |
| 507 | mbedtls_put_unaligned_uint64((data) + (offset), MBEDTLS_BSWAP64((uint64_t) (n))); \ |
| 508 | } \ |
| 509 | } |
Dave Rodgman | fbc2322 | 2022-11-24 18:07:37 +0000 | [diff] [blame] | 510 | |
| 511 | /** |
| 512 | * Get the unsigned 64 bits integer corresponding to eight bytes in |
| 513 | * little-endian order (LSB first). |
| 514 | * |
| 515 | * \param data Base address of the memory to get the eight bytes from. |
| 516 | * \param offset Offset from \p data of the first and least significant |
| 517 | * byte of the eight bytes to build the 64 bits unsigned |
| 518 | * integer from. |
| 519 | */ |
Dave Rodgman | 914c632 | 2023-03-01 09:30:14 +0000 | [diff] [blame] | 520 | #define MBEDTLS_GET_UINT64_LE(data, offset) \ |
| 521 | ((MBEDTLS_IS_BIG_ENDIAN) \ |
Dave Rodgman | a5110b0 | 2022-11-28 14:48:45 +0000 | [diff] [blame] | 522 | ? MBEDTLS_BSWAP64(mbedtls_get_unaligned_uint64((data) + (offset))) \ |
| 523 | : mbedtls_get_unaligned_uint64((data) + (offset)) \ |
Dave Rodgman | fbc2322 | 2022-11-24 18:07:37 +0000 | [diff] [blame] | 524 | ) |
Dave Rodgman | fbc2322 | 2022-11-24 18:07:37 +0000 | [diff] [blame] | 525 | |
| 526 | /** |
| 527 | * Put in memory a 64 bits unsigned integer in little-endian order. |
| 528 | * |
| 529 | * \param n 64 bits unsigned integer to put in memory. |
| 530 | * \param data Base address of the memory where to put the 64 |
| 531 | * bits unsigned integer in. |
| 532 | * \param offset Offset from \p data where to put the least significant |
| 533 | * byte of the 64 bits unsigned integer \p n. |
| 534 | */ |
Dave Rodgman | 914c632 | 2023-03-01 09:30:14 +0000 | [diff] [blame] | 535 | #define MBEDTLS_PUT_UINT64_LE(n, data, offset) \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 536 | { \ |
Dave Rodgman | 914c632 | 2023-03-01 09:30:14 +0000 | [diff] [blame] | 537 | if (MBEDTLS_IS_BIG_ENDIAN) \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 538 | { \ |
| 539 | mbedtls_put_unaligned_uint64((data) + (offset), MBEDTLS_BSWAP64((uint64_t) (n))); \ |
| 540 | } \ |
| 541 | else \ |
| 542 | { \ |
Dave Rodgman | 914c632 | 2023-03-01 09:30:14 +0000 | [diff] [blame] | 543 | mbedtls_put_unaligned_uint64((data) + (offset), (uint64_t) (n)); \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 544 | } \ |
| 545 | } |
Dave Rodgman | fbc2322 | 2022-11-24 18:07:37 +0000 | [diff] [blame] | 546 | |
| 547 | #endif /* MBEDTLS_LIBRARY_ALIGNMENT_H */ |