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 | c581264 | 2024-01-19 14:04:28 +0000 | [diff] [blame] | 48 | #if defined(__IAR_SYSTEMS_ICC__) && \ |
| 49 | (defined(MBEDTLS_ARCH_IS_ARM64) || defined(MBEDTLS_ARCH_IS_ARM32) \ |
| 50 | || defined(__ICCRX__) || defined(__ICCRL78__) || defined(__ICCRISCV__)) |
| 51 | #pragma language=save |
| 52 | #pragma language=extended |
| 53 | #define MBEDTLS_POP_IAR_LANGUAGE_PRAGMA |
| 54 | /* IAR recommend this technique for accessing unaligned data in |
| 55 | * https://www.iar.com/knowledge/support/technical-notes/compiler/accessing-unaligned-data |
| 56 | * This results in a single load / store instruction (if unaligned access is supported). |
| 57 | * According to that document, this is only supported on certain architectures. |
| 58 | */ |
| 59 | #define UINT_UNALIGNED |
| 60 | typedef uint16_t __packed mbedtls_uint16_unaligned_t; |
| 61 | typedef uint32_t __packed mbedtls_uint32_unaligned_t; |
| 62 | typedef uint64_t __packed mbedtls_uint64_unaligned_t; |
| 63 | #elif defined(MBEDTLS_COMPILER_IS_GCC) && (MBEDTLS_GCC_VERSION >= 40504) && \ |
| 64 | ((MBEDTLS_GCC_VERSION < 90300) || (!defined(MBEDTLS_EFFICIENT_UNALIGNED_ACCESS))) |
| 65 | /* |
| 66 | * Old versions of gcc, depending on how the target is specified, may generate a branch to memcpy |
| 67 | * for calls like `memcpy(dest, src, 4)` rather than generating some LDR or LDRB instructions |
| 68 | * (similar for stores). |
| 69 | * Recent versions where unaligned access is not enabled also do this. |
| 70 | * |
| 71 | * For performance (and code size, in some cases), we want to avoid the branch and just generate |
| 72 | * some inline load/store instructions since the access is small and constant-size. |
| 73 | * |
| 74 | * The manual states: |
| 75 | * "The aligned attribute specifies a minimum alignment for the variable or structure field, |
| 76 | * measured in bytes." |
| 77 | * https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html |
| 78 | * |
| 79 | * Tested with several versions of GCC from 4.5.0 up to 9.3.0 |
| 80 | * We don't enable for older than 4.5.0 as this has not been tested. |
| 81 | */ |
| 82 | #define UINT_UNALIGNED |
| 83 | typedef uint16_t __attribute__((__aligned__(1))) mbedtls_uint16_unaligned_t; |
| 84 | typedef uint32_t __attribute__((__aligned__(1))) mbedtls_uint32_unaligned_t; |
| 85 | typedef uint64_t __attribute__((__aligned__(1))) mbedtls_uint64_unaligned_t; |
| 86 | #endif |
| 87 | |
Dave Rodgman | 55b5dd2 | 2024-01-19 14:06:52 +0000 | [diff] [blame^] | 88 | /* |
| 89 | * We try to force mbedtls_(get|put)_unaligned_uintXX to be always inline, because this results |
| 90 | * in code that is both smaller and faster. IAR and gcc both benefit from this when optimising |
| 91 | * for size. |
| 92 | */ |
| 93 | |
Dave Rodgman | 96d61d1 | 2022-11-24 19:33:22 +0000 | [diff] [blame] | 94 | /** |
Dave Rodgman | a360e19 | 2022-11-28 14:44:05 +0000 | [diff] [blame] | 95 | * Read the unsigned 16 bits integer from the given address, which need not |
| 96 | * be aligned. |
| 97 | * |
| 98 | * \param p pointer to 2 bytes of data |
| 99 | * \return Data at the given address |
| 100 | */ |
Dave Rodgman | 55b5dd2 | 2024-01-19 14:06:52 +0000 | [diff] [blame^] | 101 | #if defined(__IAR_SYSTEMS_ICC__) |
| 102 | #pragma inline = forced |
| 103 | #elif defined(__GNUC__) |
| 104 | __attribute__((always_inline)) |
| 105 | #endif |
| 106 | static inline uint16_t mbedtls_get_unaligned_uint16(const void *p) |
Dave Rodgman | a360e19 | 2022-11-28 14:44:05 +0000 | [diff] [blame] | 107 | { |
| 108 | uint16_t r; |
Dave Rodgman | c581264 | 2024-01-19 14:04:28 +0000 | [diff] [blame] | 109 | #if defined(UINT_UNALIGNED) |
| 110 | mbedtls_uint16_unaligned_t *p16 = (mbedtls_uint16_unaligned_t *) p; |
| 111 | r = *p16; |
| 112 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 113 | memcpy(&r, p, sizeof(r)); |
Dave Rodgman | c581264 | 2024-01-19 14:04:28 +0000 | [diff] [blame] | 114 | #endif |
Dave Rodgman | a360e19 | 2022-11-28 14:44:05 +0000 | [diff] [blame] | 115 | return r; |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Write the unsigned 16 bits integer to the given address, which need not |
| 120 | * be aligned. |
| 121 | * |
| 122 | * \param p pointer to 2 bytes of data |
| 123 | * \param x data to write |
| 124 | */ |
Dave Rodgman | 55b5dd2 | 2024-01-19 14:06:52 +0000 | [diff] [blame^] | 125 | #if defined(__IAR_SYSTEMS_ICC__) |
| 126 | #pragma inline = forced |
| 127 | #elif defined(__GNUC__) |
| 128 | __attribute__((always_inline)) |
| 129 | #endif |
| 130 | static inline void mbedtls_put_unaligned_uint16(void *p, uint16_t x) |
Dave Rodgman | a360e19 | 2022-11-28 14:44:05 +0000 | [diff] [blame] | 131 | { |
Dave Rodgman | c581264 | 2024-01-19 14:04:28 +0000 | [diff] [blame] | 132 | #if defined(UINT_UNALIGNED) |
| 133 | mbedtls_uint16_unaligned_t *p16 = (mbedtls_uint16_unaligned_t *) p; |
| 134 | *p16 = x; |
| 135 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 136 | memcpy(p, &x, sizeof(x)); |
Dave Rodgman | c581264 | 2024-01-19 14:04:28 +0000 | [diff] [blame] | 137 | #endif |
Dave Rodgman | a360e19 | 2022-11-28 14:44:05 +0000 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | /** |
Dave Rodgman | 96d61d1 | 2022-11-24 19:33:22 +0000 | [diff] [blame] | 141 | * Read the unsigned 32 bits integer from the given address, which need not |
| 142 | * be aligned. |
Dave Rodgman | fbc2322 | 2022-11-24 18:07:37 +0000 | [diff] [blame] | 143 | * |
Dave Rodgman | 96d61d1 | 2022-11-24 19:33:22 +0000 | [diff] [blame] | 144 | * \param p pointer to 4 bytes of data |
Dave Rodgman | 875d238 | 2022-11-24 20:43:15 +0000 | [diff] [blame] | 145 | * \return Data at the given address |
Dave Rodgman | fbc2322 | 2022-11-24 18:07:37 +0000 | [diff] [blame] | 146 | */ |
Dave Rodgman | 55b5dd2 | 2024-01-19 14:06:52 +0000 | [diff] [blame^] | 147 | #if defined(__IAR_SYSTEMS_ICC__) |
| 148 | #pragma inline = forced |
| 149 | #elif defined(__GNUC__) |
| 150 | __attribute__((always_inline)) |
| 151 | #endif |
| 152 | static inline uint32_t mbedtls_get_unaligned_uint32(const void *p) |
Dave Rodgman | 96d61d1 | 2022-11-24 19:33:22 +0000 | [diff] [blame] | 153 | { |
| 154 | uint32_t r; |
Dave Rodgman | c581264 | 2024-01-19 14:04:28 +0000 | [diff] [blame] | 155 | #if defined(UINT_UNALIGNED) |
| 156 | mbedtls_uint32_unaligned_t *p32 = (mbedtls_uint32_unaligned_t *) p; |
| 157 | r = *p32; |
| 158 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 159 | memcpy(&r, p, sizeof(r)); |
Dave Rodgman | c581264 | 2024-01-19 14:04:28 +0000 | [diff] [blame] | 160 | #endif |
Dave Rodgman | 96d61d1 | 2022-11-24 19:33:22 +0000 | [diff] [blame] | 161 | return r; |
| 162 | } |
Dave Rodgman | fbc2322 | 2022-11-24 18:07:37 +0000 | [diff] [blame] | 163 | |
Dave Rodgman | 96d61d1 | 2022-11-24 19:33:22 +0000 | [diff] [blame] | 164 | /** |
| 165 | * Write the unsigned 32 bits integer to the given address, which need not |
| 166 | * be aligned. |
| 167 | * |
| 168 | * \param p pointer to 4 bytes of data |
| 169 | * \param x data to write |
| 170 | */ |
Dave Rodgman | 55b5dd2 | 2024-01-19 14:06:52 +0000 | [diff] [blame^] | 171 | #if defined(__IAR_SYSTEMS_ICC__) |
| 172 | #pragma inline = forced |
| 173 | #elif defined(__GNUC__) |
| 174 | __attribute__((always_inline)) |
| 175 | #endif |
| 176 | static inline void mbedtls_put_unaligned_uint32(void *p, uint32_t x) |
Dave Rodgman | 96d61d1 | 2022-11-24 19:33:22 +0000 | [diff] [blame] | 177 | { |
Dave Rodgman | c581264 | 2024-01-19 14:04:28 +0000 | [diff] [blame] | 178 | #if defined(UINT_UNALIGNED) |
| 179 | mbedtls_uint32_unaligned_t *p32 = (mbedtls_uint32_unaligned_t *) p; |
| 180 | *p32 = x; |
| 181 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 182 | memcpy(p, &x, sizeof(x)); |
Dave Rodgman | c581264 | 2024-01-19 14:04:28 +0000 | [diff] [blame] | 183 | #endif |
Dave Rodgman | 96d61d1 | 2022-11-24 19:33:22 +0000 | [diff] [blame] | 184 | } |
Dave Rodgman | fbc2322 | 2022-11-24 18:07:37 +0000 | [diff] [blame] | 185 | |
Dave Rodgman | a360e19 | 2022-11-28 14:44:05 +0000 | [diff] [blame] | 186 | /** |
| 187 | * Read the unsigned 64 bits integer from the given address, which need not |
| 188 | * be aligned. |
| 189 | * |
| 190 | * \param p pointer to 8 bytes of data |
| 191 | * \return Data at the given address |
| 192 | */ |
Dave Rodgman | 55b5dd2 | 2024-01-19 14:06:52 +0000 | [diff] [blame^] | 193 | #if defined(__IAR_SYSTEMS_ICC__) |
| 194 | #pragma inline = forced |
| 195 | #elif defined(__GNUC__) |
| 196 | __attribute__((always_inline)) |
| 197 | #endif |
| 198 | static inline uint64_t mbedtls_get_unaligned_uint64(const void *p) |
Dave Rodgman | a360e19 | 2022-11-28 14:44:05 +0000 | [diff] [blame] | 199 | { |
| 200 | uint64_t r; |
Dave Rodgman | c581264 | 2024-01-19 14:04:28 +0000 | [diff] [blame] | 201 | #if defined(UINT_UNALIGNED) |
| 202 | mbedtls_uint64_unaligned_t *p64 = (mbedtls_uint64_unaligned_t *) p; |
| 203 | r = *p64; |
| 204 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 205 | memcpy(&r, p, sizeof(r)); |
Dave Rodgman | c581264 | 2024-01-19 14:04:28 +0000 | [diff] [blame] | 206 | #endif |
Dave Rodgman | a360e19 | 2022-11-28 14:44:05 +0000 | [diff] [blame] | 207 | return r; |
| 208 | } |
| 209 | |
| 210 | /** |
| 211 | * Write the unsigned 64 bits integer to the given address, which need not |
| 212 | * be aligned. |
| 213 | * |
| 214 | * \param p pointer to 8 bytes of data |
| 215 | * \param x data to write |
| 216 | */ |
Dave Rodgman | 55b5dd2 | 2024-01-19 14:06:52 +0000 | [diff] [blame^] | 217 | #if defined(__IAR_SYSTEMS_ICC__) |
| 218 | #pragma inline = forced |
| 219 | #elif defined(__GNUC__) |
| 220 | __attribute__((always_inline)) |
| 221 | #endif |
| 222 | static inline void mbedtls_put_unaligned_uint64(void *p, uint64_t x) |
Dave Rodgman | a360e19 | 2022-11-28 14:44:05 +0000 | [diff] [blame] | 223 | { |
Dave Rodgman | c581264 | 2024-01-19 14:04:28 +0000 | [diff] [blame] | 224 | #if defined(UINT_UNALIGNED) |
| 225 | mbedtls_uint64_unaligned_t *p64 = (mbedtls_uint64_unaligned_t *) p; |
| 226 | *p64 = x; |
| 227 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 228 | memcpy(p, &x, sizeof(x)); |
Dave Rodgman | c581264 | 2024-01-19 14:04:28 +0000 | [diff] [blame] | 229 | #endif |
Dave Rodgman | a360e19 | 2022-11-28 14:44:05 +0000 | [diff] [blame] | 230 | } |
| 231 | |
Dave Rodgman | c581264 | 2024-01-19 14:04:28 +0000 | [diff] [blame] | 232 | #if defined(MBEDTLS_POP_IAR_LANGUAGE_PRAGMA) |
| 233 | #pragma language=restore |
| 234 | #endif |
| 235 | |
Dave Rodgman | fbc2322 | 2022-11-24 18:07:37 +0000 | [diff] [blame] | 236 | /** Byte Reading Macros |
| 237 | * |
| 238 | * Given a multi-byte integer \p x, MBEDTLS_BYTE_n retrieves the n-th |
| 239 | * byte from x, where byte 0 is the least significant byte. |
| 240 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 241 | #define MBEDTLS_BYTE_0(x) ((uint8_t) ((x) & 0xff)) |
Dave Rodgman | 914c632 | 2023-03-01 09:30:14 +0000 | [diff] [blame] | 242 | #define MBEDTLS_BYTE_1(x) ((uint8_t) (((x) >> 8) & 0xff)) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 243 | #define MBEDTLS_BYTE_2(x) ((uint8_t) (((x) >> 16) & 0xff)) |
| 244 | #define MBEDTLS_BYTE_3(x) ((uint8_t) (((x) >> 24) & 0xff)) |
| 245 | #define MBEDTLS_BYTE_4(x) ((uint8_t) (((x) >> 32) & 0xff)) |
| 246 | #define MBEDTLS_BYTE_5(x) ((uint8_t) (((x) >> 40) & 0xff)) |
| 247 | #define MBEDTLS_BYTE_6(x) ((uint8_t) (((x) >> 48) & 0xff)) |
| 248 | #define MBEDTLS_BYTE_7(x) ((uint8_t) (((x) >> 56) & 0xff)) |
Dave Rodgman | fbc2322 | 2022-11-24 18:07:37 +0000 | [diff] [blame] | 249 | |
Dave Rodgman | f7f1f74 | 2022-11-28 14:52:45 +0000 | [diff] [blame] | 250 | /* |
| 251 | * Detect GCC built-in byteswap routines |
| 252 | */ |
| 253 | #if defined(__GNUC__) && defined(__GNUC_PREREQ) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 254 | #if __GNUC_PREREQ(4, 8) |
Dave Rodgman | f7f1f74 | 2022-11-28 14:52:45 +0000 | [diff] [blame] | 255 | #define MBEDTLS_BSWAP16 __builtin_bswap16 |
| 256 | #endif /* __GNUC_PREREQ(4,8) */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 257 | #if __GNUC_PREREQ(4, 3) |
Dave Rodgman | f7f1f74 | 2022-11-28 14:52:45 +0000 | [diff] [blame] | 258 | #define MBEDTLS_BSWAP32 __builtin_bswap32 |
| 259 | #define MBEDTLS_BSWAP64 __builtin_bswap64 |
| 260 | #endif /* __GNUC_PREREQ(4,3) */ |
| 261 | #endif /* defined(__GNUC__) && defined(__GNUC_PREREQ) */ |
| 262 | |
| 263 | /* |
| 264 | * Detect Clang built-in byteswap routines |
| 265 | */ |
| 266 | #if defined(__clang__) && defined(__has_builtin) |
Dave Rodgman | e47899d | 2023-02-28 17:39:03 +0000 | [diff] [blame] | 267 | #if __has_builtin(__builtin_bswap16) && !defined(MBEDTLS_BSWAP16) |
Dave Rodgman | f7f1f74 | 2022-11-28 14:52:45 +0000 | [diff] [blame] | 268 | #define MBEDTLS_BSWAP16 __builtin_bswap16 |
| 269 | #endif /* __has_builtin(__builtin_bswap16) */ |
Dave Rodgman | e47899d | 2023-02-28 17:39:03 +0000 | [diff] [blame] | 270 | #if __has_builtin(__builtin_bswap32) && !defined(MBEDTLS_BSWAP32) |
Dave Rodgman | f7f1f74 | 2022-11-28 14:52:45 +0000 | [diff] [blame] | 271 | #define MBEDTLS_BSWAP32 __builtin_bswap32 |
| 272 | #endif /* __has_builtin(__builtin_bswap32) */ |
Dave Rodgman | e47899d | 2023-02-28 17:39:03 +0000 | [diff] [blame] | 273 | #if __has_builtin(__builtin_bswap64) && !defined(MBEDTLS_BSWAP64) |
Dave Rodgman | f7f1f74 | 2022-11-28 14:52:45 +0000 | [diff] [blame] | 274 | #define MBEDTLS_BSWAP64 __builtin_bswap64 |
| 275 | #endif /* __has_builtin(__builtin_bswap64) */ |
| 276 | #endif /* defined(__clang__) && defined(__has_builtin) */ |
| 277 | |
| 278 | /* |
| 279 | * Detect MSVC built-in byteswap routines |
| 280 | */ |
| 281 | #if defined(_MSC_VER) |
Dave Rodgman | e47899d | 2023-02-28 17:39:03 +0000 | [diff] [blame] | 282 | #if !defined(MBEDTLS_BSWAP16) |
Dave Rodgman | f7f1f74 | 2022-11-28 14:52:45 +0000 | [diff] [blame] | 283 | #define MBEDTLS_BSWAP16 _byteswap_ushort |
Dave Rodgman | e47899d | 2023-02-28 17:39:03 +0000 | [diff] [blame] | 284 | #endif |
| 285 | #if !defined(MBEDTLS_BSWAP32) |
Dave Rodgman | f7f1f74 | 2022-11-28 14:52:45 +0000 | [diff] [blame] | 286 | #define MBEDTLS_BSWAP32 _byteswap_ulong |
Dave Rodgman | e47899d | 2023-02-28 17:39:03 +0000 | [diff] [blame] | 287 | #endif |
| 288 | #if !defined(MBEDTLS_BSWAP64) |
Dave Rodgman | f7f1f74 | 2022-11-28 14:52:45 +0000 | [diff] [blame] | 289 | #define MBEDTLS_BSWAP64 _byteswap_uint64 |
Dave Rodgman | e47899d | 2023-02-28 17:39:03 +0000 | [diff] [blame] | 290 | #endif |
Dave Rodgman | f7f1f74 | 2022-11-28 14:52:45 +0000 | [diff] [blame] | 291 | #endif /* defined(_MSC_VER) */ |
| 292 | |
Dave Rodgman | 2dae4b3 | 2022-11-30 12:07:36 +0000 | [diff] [blame] | 293 | /* Detect armcc built-in byteswap routine */ |
Dave Rodgman | e47899d | 2023-02-28 17:39:03 +0000 | [diff] [blame] | 294 | #if defined(__ARMCC_VERSION) && (__ARMCC_VERSION >= 410000) && !defined(MBEDTLS_BSWAP32) |
Tom Cosgrove | f2b5a13 | 2023-04-26 17:00:12 +0100 | [diff] [blame] | 295 | #if defined(__ARM_ACLE) /* ARM Compiler 6 - earlier versions don't need a header */ |
| 296 | #include <arm_acle.h> |
| 297 | #endif |
Dave Rodgman | 2dae4b3 | 2022-11-30 12:07:36 +0000 | [diff] [blame] | 298 | #define MBEDTLS_BSWAP32 __rev |
| 299 | #endif |
| 300 | |
Dave Rodgman | 650674b | 2023-12-05 12:16:48 +0000 | [diff] [blame] | 301 | /* Detect IAR built-in byteswap routine */ |
| 302 | #if defined(__IAR_SYSTEMS_ICC__) |
| 303 | #if defined(__ARM_ACLE) |
| 304 | #include <arm_acle.h> |
| 305 | #define MBEDTLS_BSWAP16(x) ((uint16_t) __rev16((uint32_t) (x))) |
| 306 | #define MBEDTLS_BSWAP32 __rev |
| 307 | #define MBEDTLS_BSWAP64 __revll |
| 308 | #endif |
| 309 | #endif |
| 310 | |
Dave Rodgman | f7f1f74 | 2022-11-28 14:52:45 +0000 | [diff] [blame] | 311 | /* |
| 312 | * Where compiler built-ins are not present, fall back to C code that the |
| 313 | * compiler may be able to detect and transform into the relevant bswap or |
| 314 | * similar instruction. |
| 315 | */ |
| 316 | #if !defined(MBEDTLS_BSWAP16) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 317 | static inline uint16_t mbedtls_bswap16(uint16_t x) |
| 318 | { |
Dave Rodgman | 6298b24 | 2022-11-28 14:51:49 +0000 | [diff] [blame] | 319 | return |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 320 | (x & 0x00ff) << 8 | |
| 321 | (x & 0xff00) >> 8; |
Dave Rodgman | 6298b24 | 2022-11-28 14:51:49 +0000 | [diff] [blame] | 322 | } |
| 323 | #define MBEDTLS_BSWAP16 mbedtls_bswap16 |
Dave Rodgman | f7f1f74 | 2022-11-28 14:52:45 +0000 | [diff] [blame] | 324 | #endif /* !defined(MBEDTLS_BSWAP16) */ |
Dave Rodgman | 6298b24 | 2022-11-28 14:51:49 +0000 | [diff] [blame] | 325 | |
Dave Rodgman | f7f1f74 | 2022-11-28 14:52:45 +0000 | [diff] [blame] | 326 | #if !defined(MBEDTLS_BSWAP32) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 327 | static inline uint32_t mbedtls_bswap32(uint32_t x) |
| 328 | { |
Dave Rodgman | 6298b24 | 2022-11-28 14:51:49 +0000 | [diff] [blame] | 329 | return |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 330 | (x & 0x000000ff) << 24 | |
| 331 | (x & 0x0000ff00) << 8 | |
| 332 | (x & 0x00ff0000) >> 8 | |
| 333 | (x & 0xff000000) >> 24; |
Dave Rodgman | 6298b24 | 2022-11-28 14:51:49 +0000 | [diff] [blame] | 334 | } |
| 335 | #define MBEDTLS_BSWAP32 mbedtls_bswap32 |
Dave Rodgman | f7f1f74 | 2022-11-28 14:52:45 +0000 | [diff] [blame] | 336 | #endif /* !defined(MBEDTLS_BSWAP32) */ |
Dave Rodgman | 6298b24 | 2022-11-28 14:51:49 +0000 | [diff] [blame] | 337 | |
Dave Rodgman | f7f1f74 | 2022-11-28 14:52:45 +0000 | [diff] [blame] | 338 | #if !defined(MBEDTLS_BSWAP64) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 339 | static inline uint64_t mbedtls_bswap64(uint64_t x) |
| 340 | { |
Dave Rodgman | 6298b24 | 2022-11-28 14:51:49 +0000 | [diff] [blame] | 341 | return |
Tom Cosgrove | bbe166e | 2023-03-08 13:23:24 +0000 | [diff] [blame] | 342 | (x & 0x00000000000000ffULL) << 56 | |
| 343 | (x & 0x000000000000ff00ULL) << 40 | |
| 344 | (x & 0x0000000000ff0000ULL) << 24 | |
| 345 | (x & 0x00000000ff000000ULL) << 8 | |
| 346 | (x & 0x000000ff00000000ULL) >> 8 | |
| 347 | (x & 0x0000ff0000000000ULL) >> 24 | |
| 348 | (x & 0x00ff000000000000ULL) >> 40 | |
| 349 | (x & 0xff00000000000000ULL) >> 56; |
Dave Rodgman | 6298b24 | 2022-11-28 14:51:49 +0000 | [diff] [blame] | 350 | } |
| 351 | #define MBEDTLS_BSWAP64 mbedtls_bswap64 |
Dave Rodgman | f7f1f74 | 2022-11-28 14:52:45 +0000 | [diff] [blame] | 352 | #endif /* !defined(MBEDTLS_BSWAP64) */ |
Dave Rodgman | 6298b24 | 2022-11-28 14:51:49 +0000 | [diff] [blame] | 353 | |
Dave Rodgman | e5c4259 | 2022-11-28 14:47:46 +0000 | [diff] [blame] | 354 | #if !defined(__BYTE_ORDER__) |
Dave Rodgman | f3c04f3 | 2023-12-05 12:06:11 +0000 | [diff] [blame] | 355 | |
| 356 | #if defined(__LITTLE_ENDIAN__) |
| 357 | /* IAR defines __xxx_ENDIAN__, but not __BYTE_ORDER__ */ |
| 358 | #define MBEDTLS_IS_BIG_ENDIAN 0 |
| 359 | #elif defined(__BIG_ENDIAN__) |
| 360 | #define MBEDTLS_IS_BIG_ENDIAN 1 |
| 361 | #else |
Dave Rodgman | e5c4259 | 2022-11-28 14:47:46 +0000 | [diff] [blame] | 362 | static const uint16_t mbedtls_byte_order_detector = { 0x100 }; |
| 363 | #define MBEDTLS_IS_BIG_ENDIAN (*((unsigned char *) (&mbedtls_byte_order_detector)) == 0x01) |
Dave Rodgman | f3c04f3 | 2023-12-05 12:06:11 +0000 | [diff] [blame] | 364 | #endif |
| 365 | |
Dave Rodgman | e5c4259 | 2022-11-28 14:47:46 +0000 | [diff] [blame] | 366 | #else |
Dave Rodgman | f3c04f3 | 2023-12-05 12:06:11 +0000 | [diff] [blame] | 367 | |
| 368 | #if (__BYTE_ORDER__) == (__ORDER_BIG_ENDIAN__) |
| 369 | #define MBEDTLS_IS_BIG_ENDIAN 1 |
| 370 | #else |
| 371 | #define MBEDTLS_IS_BIG_ENDIAN 0 |
| 372 | #endif |
| 373 | |
Dave Rodgman | e5c4259 | 2022-11-28 14:47:46 +0000 | [diff] [blame] | 374 | #endif /* !defined(__BYTE_ORDER__) */ |
| 375 | |
Dave Rodgman | fbc2322 | 2022-11-24 18:07:37 +0000 | [diff] [blame] | 376 | /** |
| 377 | * Get the unsigned 32 bits integer corresponding to four bytes in |
| 378 | * big-endian order (MSB first). |
| 379 | * |
| 380 | * \param data Base address of the memory to get the four bytes from. |
| 381 | * \param offset Offset from \p data of the first and most significant |
| 382 | * byte of the four bytes to build the 32 bits unsigned |
| 383 | * integer from. |
| 384 | */ |
Dave Rodgman | 914c632 | 2023-03-01 09:30:14 +0000 | [diff] [blame] | 385 | #define MBEDTLS_GET_UINT32_BE(data, offset) \ |
| 386 | ((MBEDTLS_IS_BIG_ENDIAN) \ |
Dave Rodgman | a5110b0 | 2022-11-28 14:48:45 +0000 | [diff] [blame] | 387 | ? mbedtls_get_unaligned_uint32((data) + (offset)) \ |
| 388 | : MBEDTLS_BSWAP32(mbedtls_get_unaligned_uint32((data) + (offset))) \ |
Dave Rodgman | fbc2322 | 2022-11-24 18:07:37 +0000 | [diff] [blame] | 389 | ) |
Dave Rodgman | fbc2322 | 2022-11-24 18:07:37 +0000 | [diff] [blame] | 390 | |
| 391 | /** |
| 392 | * Put in memory a 32 bits unsigned integer in big-endian order. |
| 393 | * |
| 394 | * \param n 32 bits unsigned integer to put in memory. |
| 395 | * \param data Base address of the memory where to put the 32 |
| 396 | * bits unsigned integer in. |
| 397 | * \param offset Offset from \p data where to put the most significant |
| 398 | * byte of the 32 bits unsigned integer \p n. |
| 399 | */ |
Dave Rodgman | 914c632 | 2023-03-01 09:30:14 +0000 | [diff] [blame] | 400 | #define MBEDTLS_PUT_UINT32_BE(n, data, offset) \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 401 | { \ |
Dave Rodgman | 914c632 | 2023-03-01 09:30:14 +0000 | [diff] [blame] | 402 | if (MBEDTLS_IS_BIG_ENDIAN) \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 403 | { \ |
Dave Rodgman | 914c632 | 2023-03-01 09:30:14 +0000 | [diff] [blame] | 404 | mbedtls_put_unaligned_uint32((data) + (offset), (uint32_t) (n)); \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 405 | } \ |
| 406 | else \ |
| 407 | { \ |
| 408 | mbedtls_put_unaligned_uint32((data) + (offset), MBEDTLS_BSWAP32((uint32_t) (n))); \ |
| 409 | } \ |
| 410 | } |
Dave Rodgman | fbc2322 | 2022-11-24 18:07:37 +0000 | [diff] [blame] | 411 | |
| 412 | /** |
| 413 | * Get the unsigned 32 bits integer corresponding to four bytes in |
| 414 | * little-endian order (LSB first). |
| 415 | * |
| 416 | * \param data Base address of the memory to get the four bytes from. |
| 417 | * \param offset Offset from \p data of the first and least significant |
| 418 | * byte of the four bytes to build the 32 bits unsigned |
| 419 | * integer from. |
| 420 | */ |
Dave Rodgman | 914c632 | 2023-03-01 09:30:14 +0000 | [diff] [blame] | 421 | #define MBEDTLS_GET_UINT32_LE(data, offset) \ |
| 422 | ((MBEDTLS_IS_BIG_ENDIAN) \ |
Dave Rodgman | a5110b0 | 2022-11-28 14:48:45 +0000 | [diff] [blame] | 423 | ? MBEDTLS_BSWAP32(mbedtls_get_unaligned_uint32((data) + (offset))) \ |
| 424 | : mbedtls_get_unaligned_uint32((data) + (offset)) \ |
Dave Rodgman | fbc2322 | 2022-11-24 18:07:37 +0000 | [diff] [blame] | 425 | ) |
Dave Rodgman | a5110b0 | 2022-11-28 14:48:45 +0000 | [diff] [blame] | 426 | |
Dave Rodgman | fbc2322 | 2022-11-24 18:07:37 +0000 | [diff] [blame] | 427 | |
| 428 | /** |
| 429 | * Put in memory a 32 bits unsigned integer in little-endian order. |
| 430 | * |
| 431 | * \param n 32 bits unsigned integer to put in memory. |
| 432 | * \param data Base address of the memory where to put the 32 |
| 433 | * bits unsigned integer in. |
| 434 | * \param offset Offset from \p data where to put the least significant |
| 435 | * byte of the 32 bits unsigned integer \p n. |
| 436 | */ |
Dave Rodgman | 914c632 | 2023-03-01 09:30:14 +0000 | [diff] [blame] | 437 | #define MBEDTLS_PUT_UINT32_LE(n, data, offset) \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 438 | { \ |
Dave Rodgman | 914c632 | 2023-03-01 09:30:14 +0000 | [diff] [blame] | 439 | if (MBEDTLS_IS_BIG_ENDIAN) \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 440 | { \ |
| 441 | mbedtls_put_unaligned_uint32((data) + (offset), MBEDTLS_BSWAP32((uint32_t) (n))); \ |
| 442 | } \ |
| 443 | else \ |
| 444 | { \ |
Dave Rodgman | 914c632 | 2023-03-01 09:30:14 +0000 | [diff] [blame] | 445 | mbedtls_put_unaligned_uint32((data) + (offset), ((uint32_t) (n))); \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 446 | } \ |
| 447 | } |
Dave Rodgman | fbc2322 | 2022-11-24 18:07:37 +0000 | [diff] [blame] | 448 | |
| 449 | /** |
| 450 | * Get the unsigned 16 bits integer corresponding to two bytes in |
| 451 | * little-endian order (LSB first). |
| 452 | * |
| 453 | * \param data Base address of the memory to get the two bytes from. |
| 454 | * \param offset Offset from \p data of the first and least significant |
| 455 | * byte of the two bytes to build the 16 bits unsigned |
| 456 | * integer from. |
| 457 | */ |
Dave Rodgman | 914c632 | 2023-03-01 09:30:14 +0000 | [diff] [blame] | 458 | #define MBEDTLS_GET_UINT16_LE(data, offset) \ |
| 459 | ((MBEDTLS_IS_BIG_ENDIAN) \ |
Dave Rodgman | a5110b0 | 2022-11-28 14:48:45 +0000 | [diff] [blame] | 460 | ? MBEDTLS_BSWAP16(mbedtls_get_unaligned_uint16((data) + (offset))) \ |
| 461 | : mbedtls_get_unaligned_uint16((data) + (offset)) \ |
Dave Rodgman | fbc2322 | 2022-11-24 18:07:37 +0000 | [diff] [blame] | 462 | ) |
Dave Rodgman | fbc2322 | 2022-11-24 18:07:37 +0000 | [diff] [blame] | 463 | |
| 464 | /** |
| 465 | * Put in memory a 16 bits unsigned integer in little-endian order. |
| 466 | * |
| 467 | * \param n 16 bits unsigned integer to put in memory. |
| 468 | * \param data Base address of the memory where to put the 16 |
| 469 | * bits unsigned integer in. |
| 470 | * \param offset Offset from \p data where to put the least significant |
| 471 | * byte of the 16 bits unsigned integer \p n. |
| 472 | */ |
Dave Rodgman | 914c632 | 2023-03-01 09:30:14 +0000 | [diff] [blame] | 473 | #define MBEDTLS_PUT_UINT16_LE(n, data, offset) \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 474 | { \ |
Dave Rodgman | 914c632 | 2023-03-01 09:30:14 +0000 | [diff] [blame] | 475 | if (MBEDTLS_IS_BIG_ENDIAN) \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 476 | { \ |
| 477 | mbedtls_put_unaligned_uint16((data) + (offset), MBEDTLS_BSWAP16((uint16_t) (n))); \ |
| 478 | } \ |
| 479 | else \ |
| 480 | { \ |
Dave Rodgman | 914c632 | 2023-03-01 09:30:14 +0000 | [diff] [blame] | 481 | mbedtls_put_unaligned_uint16((data) + (offset), (uint16_t) (n)); \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 482 | } \ |
| 483 | } |
Dave Rodgman | fbc2322 | 2022-11-24 18:07:37 +0000 | [diff] [blame] | 484 | |
| 485 | /** |
| 486 | * Get the unsigned 16 bits integer corresponding to two bytes in |
| 487 | * big-endian order (MSB first). |
| 488 | * |
| 489 | * \param data Base address of the memory to get the two bytes from. |
| 490 | * \param offset Offset from \p data of the first and most significant |
| 491 | * byte of the two bytes to build the 16 bits unsigned |
| 492 | * integer from. |
| 493 | */ |
Dave Rodgman | 914c632 | 2023-03-01 09:30:14 +0000 | [diff] [blame] | 494 | #define MBEDTLS_GET_UINT16_BE(data, offset) \ |
| 495 | ((MBEDTLS_IS_BIG_ENDIAN) \ |
Dave Rodgman | a5110b0 | 2022-11-28 14:48:45 +0000 | [diff] [blame] | 496 | ? mbedtls_get_unaligned_uint16((data) + (offset)) \ |
| 497 | : MBEDTLS_BSWAP16(mbedtls_get_unaligned_uint16((data) + (offset))) \ |
Dave Rodgman | fbc2322 | 2022-11-24 18:07:37 +0000 | [diff] [blame] | 498 | ) |
Dave Rodgman | fbc2322 | 2022-11-24 18:07:37 +0000 | [diff] [blame] | 499 | |
| 500 | /** |
| 501 | * Put in memory a 16 bits unsigned integer in big-endian order. |
| 502 | * |
| 503 | * \param n 16 bits unsigned integer to put in memory. |
| 504 | * \param data Base address of the memory where to put the 16 |
| 505 | * bits unsigned integer in. |
| 506 | * \param offset Offset from \p data where to put the most significant |
| 507 | * byte of the 16 bits unsigned integer \p n. |
| 508 | */ |
Dave Rodgman | 914c632 | 2023-03-01 09:30:14 +0000 | [diff] [blame] | 509 | #define MBEDTLS_PUT_UINT16_BE(n, data, offset) \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 510 | { \ |
Dave Rodgman | 914c632 | 2023-03-01 09:30:14 +0000 | [diff] [blame] | 511 | if (MBEDTLS_IS_BIG_ENDIAN) \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 512 | { \ |
Dave Rodgman | 914c632 | 2023-03-01 09:30:14 +0000 | [diff] [blame] | 513 | mbedtls_put_unaligned_uint16((data) + (offset), (uint16_t) (n)); \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 514 | } \ |
| 515 | else \ |
| 516 | { \ |
| 517 | mbedtls_put_unaligned_uint16((data) + (offset), MBEDTLS_BSWAP16((uint16_t) (n))); \ |
| 518 | } \ |
| 519 | } |
Dave Rodgman | fbc2322 | 2022-11-24 18:07:37 +0000 | [diff] [blame] | 520 | |
| 521 | /** |
| 522 | * Get the unsigned 24 bits integer corresponding to three bytes in |
| 523 | * big-endian order (MSB first). |
| 524 | * |
| 525 | * \param data Base address of the memory to get the three bytes from. |
| 526 | * \param offset Offset from \p data of the first and most significant |
| 527 | * byte of the three bytes to build the 24 bits unsigned |
| 528 | * integer from. |
| 529 | */ |
Dave Rodgman | 914c632 | 2023-03-01 09:30:14 +0000 | [diff] [blame] | 530 | #define MBEDTLS_GET_UINT24_BE(data, offset) \ |
| 531 | ( \ |
| 532 | ((uint32_t) (data)[(offset)] << 16) \ |
| 533 | | ((uint32_t) (data)[(offset) + 1] << 8) \ |
| 534 | | ((uint32_t) (data)[(offset) + 2]) \ |
Dave Rodgman | fbc2322 | 2022-11-24 18:07:37 +0000 | [diff] [blame] | 535 | ) |
Dave Rodgman | fbc2322 | 2022-11-24 18:07:37 +0000 | [diff] [blame] | 536 | |
| 537 | /** |
| 538 | * Put in memory a 24 bits unsigned integer in big-endian order. |
| 539 | * |
| 540 | * \param n 24 bits unsigned integer to put in memory. |
| 541 | * \param data Base address of the memory where to put the 24 |
| 542 | * bits unsigned integer in. |
| 543 | * \param offset Offset from \p data where to put the most significant |
| 544 | * byte of the 24 bits unsigned integer \p n. |
| 545 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 546 | #define MBEDTLS_PUT_UINT24_BE(n, data, offset) \ |
Dave Rodgman | 914c632 | 2023-03-01 09:30:14 +0000 | [diff] [blame] | 547 | { \ |
| 548 | (data)[(offset)] = MBEDTLS_BYTE_2(n); \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 549 | (data)[(offset) + 1] = MBEDTLS_BYTE_1(n); \ |
| 550 | (data)[(offset) + 2] = MBEDTLS_BYTE_0(n); \ |
| 551 | } |
Dave Rodgman | fbc2322 | 2022-11-24 18:07:37 +0000 | [diff] [blame] | 552 | |
| 553 | /** |
| 554 | * Get the unsigned 24 bits integer corresponding to three bytes in |
| 555 | * little-endian order (LSB first). |
| 556 | * |
| 557 | * \param data Base address of the memory to get the three bytes from. |
| 558 | * \param offset Offset from \p data of the first and least significant |
| 559 | * byte of the three bytes to build the 24 bits unsigned |
| 560 | * integer from. |
| 561 | */ |
Dave Rodgman | 914c632 | 2023-03-01 09:30:14 +0000 | [diff] [blame] | 562 | #define MBEDTLS_GET_UINT24_LE(data, offset) \ |
| 563 | ( \ |
| 564 | ((uint32_t) (data)[(offset)]) \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 565 | | ((uint32_t) (data)[(offset) + 1] << 8) \ |
| 566 | | ((uint32_t) (data)[(offset) + 2] << 16) \ |
Dave Rodgman | fbc2322 | 2022-11-24 18:07:37 +0000 | [diff] [blame] | 567 | ) |
Dave Rodgman | fbc2322 | 2022-11-24 18:07:37 +0000 | [diff] [blame] | 568 | |
| 569 | /** |
| 570 | * Put in memory a 24 bits unsigned integer in little-endian order. |
| 571 | * |
| 572 | * \param n 24 bits unsigned integer to put in memory. |
| 573 | * \param data Base address of the memory where to put the 24 |
| 574 | * bits unsigned integer in. |
| 575 | * \param offset Offset from \p data where to put the least significant |
| 576 | * byte of the 24 bits unsigned integer \p n. |
| 577 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 578 | #define MBEDTLS_PUT_UINT24_LE(n, data, offset) \ |
Dave Rodgman | 914c632 | 2023-03-01 09:30:14 +0000 | [diff] [blame] | 579 | { \ |
| 580 | (data)[(offset)] = MBEDTLS_BYTE_0(n); \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 581 | (data)[(offset) + 1] = MBEDTLS_BYTE_1(n); \ |
| 582 | (data)[(offset) + 2] = MBEDTLS_BYTE_2(n); \ |
| 583 | } |
Dave Rodgman | fbc2322 | 2022-11-24 18:07:37 +0000 | [diff] [blame] | 584 | |
| 585 | /** |
| 586 | * Get the unsigned 64 bits integer corresponding to eight bytes in |
| 587 | * big-endian order (MSB first). |
| 588 | * |
| 589 | * \param data Base address of the memory to get the eight bytes from. |
| 590 | * \param offset Offset from \p data of the first and most significant |
| 591 | * byte of the eight bytes to build the 64 bits unsigned |
| 592 | * integer from. |
| 593 | */ |
Dave Rodgman | 914c632 | 2023-03-01 09:30:14 +0000 | [diff] [blame] | 594 | #define MBEDTLS_GET_UINT64_BE(data, offset) \ |
| 595 | ((MBEDTLS_IS_BIG_ENDIAN) \ |
Dave Rodgman | a5110b0 | 2022-11-28 14:48:45 +0000 | [diff] [blame] | 596 | ? mbedtls_get_unaligned_uint64((data) + (offset)) \ |
| 597 | : MBEDTLS_BSWAP64(mbedtls_get_unaligned_uint64((data) + (offset))) \ |
Dave Rodgman | fbc2322 | 2022-11-24 18:07:37 +0000 | [diff] [blame] | 598 | ) |
Dave Rodgman | fbc2322 | 2022-11-24 18:07:37 +0000 | [diff] [blame] | 599 | |
| 600 | /** |
| 601 | * Put in memory a 64 bits unsigned integer in big-endian order. |
| 602 | * |
| 603 | * \param n 64 bits unsigned integer to put in memory. |
| 604 | * \param data Base address of the memory where to put the 64 |
| 605 | * bits unsigned integer in. |
| 606 | * \param offset Offset from \p data where to put the most significant |
| 607 | * byte of the 64 bits unsigned integer \p n. |
| 608 | */ |
Dave Rodgman | 914c632 | 2023-03-01 09:30:14 +0000 | [diff] [blame] | 609 | #define MBEDTLS_PUT_UINT64_BE(n, data, offset) \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 610 | { \ |
Dave Rodgman | 914c632 | 2023-03-01 09:30:14 +0000 | [diff] [blame] | 611 | if (MBEDTLS_IS_BIG_ENDIAN) \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 612 | { \ |
Dave Rodgman | 914c632 | 2023-03-01 09:30:14 +0000 | [diff] [blame] | 613 | mbedtls_put_unaligned_uint64((data) + (offset), (uint64_t) (n)); \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 614 | } \ |
| 615 | else \ |
| 616 | { \ |
| 617 | mbedtls_put_unaligned_uint64((data) + (offset), MBEDTLS_BSWAP64((uint64_t) (n))); \ |
| 618 | } \ |
| 619 | } |
Dave Rodgman | fbc2322 | 2022-11-24 18:07:37 +0000 | [diff] [blame] | 620 | |
| 621 | /** |
| 622 | * Get the unsigned 64 bits integer corresponding to eight bytes in |
| 623 | * little-endian order (LSB first). |
| 624 | * |
| 625 | * \param data Base address of the memory to get the eight bytes from. |
| 626 | * \param offset Offset from \p data of the first and least significant |
| 627 | * byte of the eight bytes to build the 64 bits unsigned |
| 628 | * integer from. |
| 629 | */ |
Dave Rodgman | 914c632 | 2023-03-01 09:30:14 +0000 | [diff] [blame] | 630 | #define MBEDTLS_GET_UINT64_LE(data, offset) \ |
| 631 | ((MBEDTLS_IS_BIG_ENDIAN) \ |
Dave Rodgman | a5110b0 | 2022-11-28 14:48:45 +0000 | [diff] [blame] | 632 | ? MBEDTLS_BSWAP64(mbedtls_get_unaligned_uint64((data) + (offset))) \ |
| 633 | : mbedtls_get_unaligned_uint64((data) + (offset)) \ |
Dave Rodgman | fbc2322 | 2022-11-24 18:07:37 +0000 | [diff] [blame] | 634 | ) |
Dave Rodgman | fbc2322 | 2022-11-24 18:07:37 +0000 | [diff] [blame] | 635 | |
| 636 | /** |
| 637 | * Put in memory a 64 bits unsigned integer in little-endian order. |
| 638 | * |
| 639 | * \param n 64 bits unsigned integer to put in memory. |
| 640 | * \param data Base address of the memory where to put the 64 |
| 641 | * bits unsigned integer in. |
| 642 | * \param offset Offset from \p data where to put the least significant |
| 643 | * byte of the 64 bits unsigned integer \p n. |
| 644 | */ |
Dave Rodgman | 914c632 | 2023-03-01 09:30:14 +0000 | [diff] [blame] | 645 | #define MBEDTLS_PUT_UINT64_LE(n, data, offset) \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 646 | { \ |
Dave Rodgman | 914c632 | 2023-03-01 09:30:14 +0000 | [diff] [blame] | 647 | if (MBEDTLS_IS_BIG_ENDIAN) \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 648 | { \ |
| 649 | mbedtls_put_unaligned_uint64((data) + (offset), MBEDTLS_BSWAP64((uint64_t) (n))); \ |
| 650 | } \ |
| 651 | else \ |
| 652 | { \ |
Dave Rodgman | 914c632 | 2023-03-01 09:30:14 +0000 | [diff] [blame] | 653 | mbedtls_put_unaligned_uint64((data) + (offset), (uint64_t) (n)); \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 654 | } \ |
| 655 | } |
Dave Rodgman | fbc2322 | 2022-11-24 18:07:37 +0000 | [diff] [blame] | 656 | |
| 657 | #endif /* MBEDTLS_LIBRARY_ALIGNMENT_H */ |