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