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 | 96d61d1 | 2022-11-24 19:33:22 +0000 | [diff] [blame] | 88 | /** |
Dave Rodgman | a360e19 | 2022-11-28 14:44:05 +0000 | [diff] [blame] | 89 | * Read the unsigned 16 bits integer from the given address, which need not |
| 90 | * be aligned. |
| 91 | * |
| 92 | * \param p pointer to 2 bytes of data |
| 93 | * \return Data at the given address |
| 94 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 95 | inline uint16_t mbedtls_get_unaligned_uint16(const void *p) |
Dave Rodgman | a360e19 | 2022-11-28 14:44:05 +0000 | [diff] [blame] | 96 | { |
| 97 | uint16_t r; |
Dave Rodgman | c581264 | 2024-01-19 14:04:28 +0000 | [diff] [blame^] | 98 | #if defined(UINT_UNALIGNED) |
| 99 | mbedtls_uint16_unaligned_t *p16 = (mbedtls_uint16_unaligned_t *) p; |
| 100 | r = *p16; |
| 101 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 102 | memcpy(&r, p, sizeof(r)); |
Dave Rodgman | c581264 | 2024-01-19 14:04:28 +0000 | [diff] [blame^] | 103 | #endif |
Dave Rodgman | a360e19 | 2022-11-28 14:44:05 +0000 | [diff] [blame] | 104 | return r; |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Write the unsigned 16 bits integer to the given address, which need not |
| 109 | * be aligned. |
| 110 | * |
| 111 | * \param p pointer to 2 bytes of data |
| 112 | * \param x data to write |
| 113 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 114 | inline void mbedtls_put_unaligned_uint16(void *p, uint16_t x) |
Dave Rodgman | a360e19 | 2022-11-28 14:44:05 +0000 | [diff] [blame] | 115 | { |
Dave Rodgman | c581264 | 2024-01-19 14:04:28 +0000 | [diff] [blame^] | 116 | #if defined(UINT_UNALIGNED) |
| 117 | mbedtls_uint16_unaligned_t *p16 = (mbedtls_uint16_unaligned_t *) p; |
| 118 | *p16 = x; |
| 119 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 120 | memcpy(p, &x, sizeof(x)); |
Dave Rodgman | c581264 | 2024-01-19 14:04:28 +0000 | [diff] [blame^] | 121 | #endif |
Dave Rodgman | a360e19 | 2022-11-28 14:44:05 +0000 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | /** |
Dave Rodgman | 96d61d1 | 2022-11-24 19:33:22 +0000 | [diff] [blame] | 125 | * Read the unsigned 32 bits integer from the given address, which need not |
| 126 | * be aligned. |
Dave Rodgman | fbc2322 | 2022-11-24 18:07:37 +0000 | [diff] [blame] | 127 | * |
Dave Rodgman | 96d61d1 | 2022-11-24 19:33:22 +0000 | [diff] [blame] | 128 | * \param p pointer to 4 bytes of data |
Dave Rodgman | 875d238 | 2022-11-24 20:43:15 +0000 | [diff] [blame] | 129 | * \return Data at the given address |
Dave Rodgman | fbc2322 | 2022-11-24 18:07:37 +0000 | [diff] [blame] | 130 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 131 | inline uint32_t mbedtls_get_unaligned_uint32(const void *p) |
Dave Rodgman | 96d61d1 | 2022-11-24 19:33:22 +0000 | [diff] [blame] | 132 | { |
| 133 | uint32_t r; |
Dave Rodgman | c581264 | 2024-01-19 14:04:28 +0000 | [diff] [blame^] | 134 | #if defined(UINT_UNALIGNED) |
| 135 | mbedtls_uint32_unaligned_t *p32 = (mbedtls_uint32_unaligned_t *) p; |
| 136 | r = *p32; |
| 137 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 138 | memcpy(&r, p, sizeof(r)); |
Dave Rodgman | c581264 | 2024-01-19 14:04:28 +0000 | [diff] [blame^] | 139 | #endif |
Dave Rodgman | 96d61d1 | 2022-11-24 19:33:22 +0000 | [diff] [blame] | 140 | return r; |
| 141 | } |
Dave Rodgman | fbc2322 | 2022-11-24 18:07:37 +0000 | [diff] [blame] | 142 | |
Dave Rodgman | 96d61d1 | 2022-11-24 19:33:22 +0000 | [diff] [blame] | 143 | /** |
| 144 | * Write the unsigned 32 bits integer to the given address, which need not |
| 145 | * be aligned. |
| 146 | * |
| 147 | * \param p pointer to 4 bytes of data |
| 148 | * \param x data to write |
| 149 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 150 | inline void mbedtls_put_unaligned_uint32(void *p, uint32_t x) |
Dave Rodgman | 96d61d1 | 2022-11-24 19:33:22 +0000 | [diff] [blame] | 151 | { |
Dave Rodgman | c581264 | 2024-01-19 14:04:28 +0000 | [diff] [blame^] | 152 | #if defined(UINT_UNALIGNED) |
| 153 | mbedtls_uint32_unaligned_t *p32 = (mbedtls_uint32_unaligned_t *) p; |
| 154 | *p32 = x; |
| 155 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 156 | memcpy(p, &x, sizeof(x)); |
Dave Rodgman | c581264 | 2024-01-19 14:04:28 +0000 | [diff] [blame^] | 157 | #endif |
Dave Rodgman | 96d61d1 | 2022-11-24 19:33:22 +0000 | [diff] [blame] | 158 | } |
Dave Rodgman | fbc2322 | 2022-11-24 18:07:37 +0000 | [diff] [blame] | 159 | |
Dave Rodgman | a360e19 | 2022-11-28 14:44:05 +0000 | [diff] [blame] | 160 | /** |
| 161 | * Read the unsigned 64 bits integer from the given address, which need not |
| 162 | * be aligned. |
| 163 | * |
| 164 | * \param p pointer to 8 bytes of data |
| 165 | * \return Data at the given address |
| 166 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 167 | inline uint64_t mbedtls_get_unaligned_uint64(const void *p) |
Dave Rodgman | a360e19 | 2022-11-28 14:44:05 +0000 | [diff] [blame] | 168 | { |
| 169 | uint64_t r; |
Dave Rodgman | c581264 | 2024-01-19 14:04:28 +0000 | [diff] [blame^] | 170 | #if defined(UINT_UNALIGNED) |
| 171 | mbedtls_uint64_unaligned_t *p64 = (mbedtls_uint64_unaligned_t *) p; |
| 172 | r = *p64; |
| 173 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 174 | memcpy(&r, p, sizeof(r)); |
Dave Rodgman | c581264 | 2024-01-19 14:04:28 +0000 | [diff] [blame^] | 175 | #endif |
Dave Rodgman | a360e19 | 2022-11-28 14:44:05 +0000 | [diff] [blame] | 176 | return r; |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * Write the unsigned 64 bits integer to the given address, which need not |
| 181 | * be aligned. |
| 182 | * |
| 183 | * \param p pointer to 8 bytes of data |
| 184 | * \param x data to write |
| 185 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 186 | inline void mbedtls_put_unaligned_uint64(void *p, uint64_t x) |
Dave Rodgman | a360e19 | 2022-11-28 14:44:05 +0000 | [diff] [blame] | 187 | { |
Dave Rodgman | c581264 | 2024-01-19 14:04:28 +0000 | [diff] [blame^] | 188 | #if defined(UINT_UNALIGNED) |
| 189 | mbedtls_uint64_unaligned_t *p64 = (mbedtls_uint64_unaligned_t *) p; |
| 190 | *p64 = x; |
| 191 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 192 | memcpy(p, &x, sizeof(x)); |
Dave Rodgman | c581264 | 2024-01-19 14:04:28 +0000 | [diff] [blame^] | 193 | #endif |
Dave Rodgman | a360e19 | 2022-11-28 14:44:05 +0000 | [diff] [blame] | 194 | } |
| 195 | |
Dave Rodgman | c581264 | 2024-01-19 14:04:28 +0000 | [diff] [blame^] | 196 | #if defined(MBEDTLS_POP_IAR_LANGUAGE_PRAGMA) |
| 197 | #pragma language=restore |
| 198 | #endif |
| 199 | |
Dave Rodgman | fbc2322 | 2022-11-24 18:07:37 +0000 | [diff] [blame] | 200 | /** Byte Reading Macros |
| 201 | * |
| 202 | * Given a multi-byte integer \p x, MBEDTLS_BYTE_n retrieves the n-th |
| 203 | * byte from x, where byte 0 is the least significant byte. |
| 204 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 205 | #define MBEDTLS_BYTE_0(x) ((uint8_t) ((x) & 0xff)) |
Dave Rodgman | 914c632 | 2023-03-01 09:30:14 +0000 | [diff] [blame] | 206 | #define MBEDTLS_BYTE_1(x) ((uint8_t) (((x) >> 8) & 0xff)) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 207 | #define MBEDTLS_BYTE_2(x) ((uint8_t) (((x) >> 16) & 0xff)) |
| 208 | #define MBEDTLS_BYTE_3(x) ((uint8_t) (((x) >> 24) & 0xff)) |
| 209 | #define MBEDTLS_BYTE_4(x) ((uint8_t) (((x) >> 32) & 0xff)) |
| 210 | #define MBEDTLS_BYTE_5(x) ((uint8_t) (((x) >> 40) & 0xff)) |
| 211 | #define MBEDTLS_BYTE_6(x) ((uint8_t) (((x) >> 48) & 0xff)) |
| 212 | #define MBEDTLS_BYTE_7(x) ((uint8_t) (((x) >> 56) & 0xff)) |
Dave Rodgman | fbc2322 | 2022-11-24 18:07:37 +0000 | [diff] [blame] | 213 | |
Dave Rodgman | f7f1f74 | 2022-11-28 14:52:45 +0000 | [diff] [blame] | 214 | /* |
| 215 | * Detect GCC built-in byteswap routines |
| 216 | */ |
| 217 | #if defined(__GNUC__) && defined(__GNUC_PREREQ) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 218 | #if __GNUC_PREREQ(4, 8) |
Dave Rodgman | f7f1f74 | 2022-11-28 14:52:45 +0000 | [diff] [blame] | 219 | #define MBEDTLS_BSWAP16 __builtin_bswap16 |
| 220 | #endif /* __GNUC_PREREQ(4,8) */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 221 | #if __GNUC_PREREQ(4, 3) |
Dave Rodgman | f7f1f74 | 2022-11-28 14:52:45 +0000 | [diff] [blame] | 222 | #define MBEDTLS_BSWAP32 __builtin_bswap32 |
| 223 | #define MBEDTLS_BSWAP64 __builtin_bswap64 |
| 224 | #endif /* __GNUC_PREREQ(4,3) */ |
| 225 | #endif /* defined(__GNUC__) && defined(__GNUC_PREREQ) */ |
| 226 | |
| 227 | /* |
| 228 | * Detect Clang built-in byteswap routines |
| 229 | */ |
| 230 | #if defined(__clang__) && defined(__has_builtin) |
Dave Rodgman | e47899d | 2023-02-28 17:39:03 +0000 | [diff] [blame] | 231 | #if __has_builtin(__builtin_bswap16) && !defined(MBEDTLS_BSWAP16) |
Dave Rodgman | f7f1f74 | 2022-11-28 14:52:45 +0000 | [diff] [blame] | 232 | #define MBEDTLS_BSWAP16 __builtin_bswap16 |
| 233 | #endif /* __has_builtin(__builtin_bswap16) */ |
Dave Rodgman | e47899d | 2023-02-28 17:39:03 +0000 | [diff] [blame] | 234 | #if __has_builtin(__builtin_bswap32) && !defined(MBEDTLS_BSWAP32) |
Dave Rodgman | f7f1f74 | 2022-11-28 14:52:45 +0000 | [diff] [blame] | 235 | #define MBEDTLS_BSWAP32 __builtin_bswap32 |
| 236 | #endif /* __has_builtin(__builtin_bswap32) */ |
Dave Rodgman | e47899d | 2023-02-28 17:39:03 +0000 | [diff] [blame] | 237 | #if __has_builtin(__builtin_bswap64) && !defined(MBEDTLS_BSWAP64) |
Dave Rodgman | f7f1f74 | 2022-11-28 14:52:45 +0000 | [diff] [blame] | 238 | #define MBEDTLS_BSWAP64 __builtin_bswap64 |
| 239 | #endif /* __has_builtin(__builtin_bswap64) */ |
| 240 | #endif /* defined(__clang__) && defined(__has_builtin) */ |
| 241 | |
| 242 | /* |
| 243 | * Detect MSVC built-in byteswap routines |
| 244 | */ |
| 245 | #if defined(_MSC_VER) |
Dave Rodgman | e47899d | 2023-02-28 17:39:03 +0000 | [diff] [blame] | 246 | #if !defined(MBEDTLS_BSWAP16) |
Dave Rodgman | f7f1f74 | 2022-11-28 14:52:45 +0000 | [diff] [blame] | 247 | #define MBEDTLS_BSWAP16 _byteswap_ushort |
Dave Rodgman | e47899d | 2023-02-28 17:39:03 +0000 | [diff] [blame] | 248 | #endif |
| 249 | #if !defined(MBEDTLS_BSWAP32) |
Dave Rodgman | f7f1f74 | 2022-11-28 14:52:45 +0000 | [diff] [blame] | 250 | #define MBEDTLS_BSWAP32 _byteswap_ulong |
Dave Rodgman | e47899d | 2023-02-28 17:39:03 +0000 | [diff] [blame] | 251 | #endif |
| 252 | #if !defined(MBEDTLS_BSWAP64) |
Dave Rodgman | f7f1f74 | 2022-11-28 14:52:45 +0000 | [diff] [blame] | 253 | #define MBEDTLS_BSWAP64 _byteswap_uint64 |
Dave Rodgman | e47899d | 2023-02-28 17:39:03 +0000 | [diff] [blame] | 254 | #endif |
Dave Rodgman | f7f1f74 | 2022-11-28 14:52:45 +0000 | [diff] [blame] | 255 | #endif /* defined(_MSC_VER) */ |
| 256 | |
Dave Rodgman | 2dae4b3 | 2022-11-30 12:07:36 +0000 | [diff] [blame] | 257 | /* Detect armcc built-in byteswap routine */ |
Dave Rodgman | e47899d | 2023-02-28 17:39:03 +0000 | [diff] [blame] | 258 | #if defined(__ARMCC_VERSION) && (__ARMCC_VERSION >= 410000) && !defined(MBEDTLS_BSWAP32) |
Tom Cosgrove | f2b5a13 | 2023-04-26 17:00:12 +0100 | [diff] [blame] | 259 | #if defined(__ARM_ACLE) /* ARM Compiler 6 - earlier versions don't need a header */ |
| 260 | #include <arm_acle.h> |
| 261 | #endif |
Dave Rodgman | 2dae4b3 | 2022-11-30 12:07:36 +0000 | [diff] [blame] | 262 | #define MBEDTLS_BSWAP32 __rev |
| 263 | #endif |
| 264 | |
Dave Rodgman | 650674b | 2023-12-05 12:16:48 +0000 | [diff] [blame] | 265 | /* Detect IAR built-in byteswap routine */ |
| 266 | #if defined(__IAR_SYSTEMS_ICC__) |
| 267 | #if defined(__ARM_ACLE) |
| 268 | #include <arm_acle.h> |
| 269 | #define MBEDTLS_BSWAP16(x) ((uint16_t) __rev16((uint32_t) (x))) |
| 270 | #define MBEDTLS_BSWAP32 __rev |
| 271 | #define MBEDTLS_BSWAP64 __revll |
| 272 | #endif |
| 273 | #endif |
| 274 | |
Dave Rodgman | f7f1f74 | 2022-11-28 14:52:45 +0000 | [diff] [blame] | 275 | /* |
| 276 | * Where compiler built-ins are not present, fall back to C code that the |
| 277 | * compiler may be able to detect and transform into the relevant bswap or |
| 278 | * similar instruction. |
| 279 | */ |
| 280 | #if !defined(MBEDTLS_BSWAP16) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 281 | static inline uint16_t mbedtls_bswap16(uint16_t x) |
| 282 | { |
Dave Rodgman | 6298b24 | 2022-11-28 14:51:49 +0000 | [diff] [blame] | 283 | return |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 284 | (x & 0x00ff) << 8 | |
| 285 | (x & 0xff00) >> 8; |
Dave Rodgman | 6298b24 | 2022-11-28 14:51:49 +0000 | [diff] [blame] | 286 | } |
| 287 | #define MBEDTLS_BSWAP16 mbedtls_bswap16 |
Dave Rodgman | f7f1f74 | 2022-11-28 14:52:45 +0000 | [diff] [blame] | 288 | #endif /* !defined(MBEDTLS_BSWAP16) */ |
Dave Rodgman | 6298b24 | 2022-11-28 14:51:49 +0000 | [diff] [blame] | 289 | |
Dave Rodgman | f7f1f74 | 2022-11-28 14:52:45 +0000 | [diff] [blame] | 290 | #if !defined(MBEDTLS_BSWAP32) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 291 | static inline uint32_t mbedtls_bswap32(uint32_t x) |
| 292 | { |
Dave Rodgman | 6298b24 | 2022-11-28 14:51:49 +0000 | [diff] [blame] | 293 | return |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 294 | (x & 0x000000ff) << 24 | |
| 295 | (x & 0x0000ff00) << 8 | |
| 296 | (x & 0x00ff0000) >> 8 | |
| 297 | (x & 0xff000000) >> 24; |
Dave Rodgman | 6298b24 | 2022-11-28 14:51:49 +0000 | [diff] [blame] | 298 | } |
| 299 | #define MBEDTLS_BSWAP32 mbedtls_bswap32 |
Dave Rodgman | f7f1f74 | 2022-11-28 14:52:45 +0000 | [diff] [blame] | 300 | #endif /* !defined(MBEDTLS_BSWAP32) */ |
Dave Rodgman | 6298b24 | 2022-11-28 14:51:49 +0000 | [diff] [blame] | 301 | |
Dave Rodgman | f7f1f74 | 2022-11-28 14:52:45 +0000 | [diff] [blame] | 302 | #if !defined(MBEDTLS_BSWAP64) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 303 | static inline uint64_t mbedtls_bswap64(uint64_t x) |
| 304 | { |
Dave Rodgman | 6298b24 | 2022-11-28 14:51:49 +0000 | [diff] [blame] | 305 | return |
Tom Cosgrove | bbe166e | 2023-03-08 13:23:24 +0000 | [diff] [blame] | 306 | (x & 0x00000000000000ffULL) << 56 | |
| 307 | (x & 0x000000000000ff00ULL) << 40 | |
| 308 | (x & 0x0000000000ff0000ULL) << 24 | |
| 309 | (x & 0x00000000ff000000ULL) << 8 | |
| 310 | (x & 0x000000ff00000000ULL) >> 8 | |
| 311 | (x & 0x0000ff0000000000ULL) >> 24 | |
| 312 | (x & 0x00ff000000000000ULL) >> 40 | |
| 313 | (x & 0xff00000000000000ULL) >> 56; |
Dave Rodgman | 6298b24 | 2022-11-28 14:51:49 +0000 | [diff] [blame] | 314 | } |
| 315 | #define MBEDTLS_BSWAP64 mbedtls_bswap64 |
Dave Rodgman | f7f1f74 | 2022-11-28 14:52:45 +0000 | [diff] [blame] | 316 | #endif /* !defined(MBEDTLS_BSWAP64) */ |
Dave Rodgman | 6298b24 | 2022-11-28 14:51:49 +0000 | [diff] [blame] | 317 | |
Dave Rodgman | e5c4259 | 2022-11-28 14:47:46 +0000 | [diff] [blame] | 318 | #if !defined(__BYTE_ORDER__) |
Dave Rodgman | f3c04f3 | 2023-12-05 12:06:11 +0000 | [diff] [blame] | 319 | |
| 320 | #if defined(__LITTLE_ENDIAN__) |
| 321 | /* IAR defines __xxx_ENDIAN__, but not __BYTE_ORDER__ */ |
| 322 | #define MBEDTLS_IS_BIG_ENDIAN 0 |
| 323 | #elif defined(__BIG_ENDIAN__) |
| 324 | #define MBEDTLS_IS_BIG_ENDIAN 1 |
| 325 | #else |
Dave Rodgman | e5c4259 | 2022-11-28 14:47:46 +0000 | [diff] [blame] | 326 | static const uint16_t mbedtls_byte_order_detector = { 0x100 }; |
| 327 | #define MBEDTLS_IS_BIG_ENDIAN (*((unsigned char *) (&mbedtls_byte_order_detector)) == 0x01) |
Dave Rodgman | f3c04f3 | 2023-12-05 12:06:11 +0000 | [diff] [blame] | 328 | #endif |
| 329 | |
Dave Rodgman | e5c4259 | 2022-11-28 14:47:46 +0000 | [diff] [blame] | 330 | #else |
Dave Rodgman | f3c04f3 | 2023-12-05 12:06:11 +0000 | [diff] [blame] | 331 | |
| 332 | #if (__BYTE_ORDER__) == (__ORDER_BIG_ENDIAN__) |
| 333 | #define MBEDTLS_IS_BIG_ENDIAN 1 |
| 334 | #else |
| 335 | #define MBEDTLS_IS_BIG_ENDIAN 0 |
| 336 | #endif |
| 337 | |
Dave Rodgman | e5c4259 | 2022-11-28 14:47:46 +0000 | [diff] [blame] | 338 | #endif /* !defined(__BYTE_ORDER__) */ |
| 339 | |
Dave Rodgman | fbc2322 | 2022-11-24 18:07:37 +0000 | [diff] [blame] | 340 | /** |
| 341 | * Get the unsigned 32 bits integer corresponding to four bytes in |
| 342 | * big-endian order (MSB first). |
| 343 | * |
| 344 | * \param data Base address of the memory to get the four bytes from. |
| 345 | * \param offset Offset from \p data of the first and most significant |
| 346 | * byte of the four bytes to build the 32 bits unsigned |
| 347 | * integer from. |
| 348 | */ |
Dave Rodgman | 914c632 | 2023-03-01 09:30:14 +0000 | [diff] [blame] | 349 | #define MBEDTLS_GET_UINT32_BE(data, offset) \ |
| 350 | ((MBEDTLS_IS_BIG_ENDIAN) \ |
Dave Rodgman | a5110b0 | 2022-11-28 14:48:45 +0000 | [diff] [blame] | 351 | ? mbedtls_get_unaligned_uint32((data) + (offset)) \ |
| 352 | : MBEDTLS_BSWAP32(mbedtls_get_unaligned_uint32((data) + (offset))) \ |
Dave Rodgman | fbc2322 | 2022-11-24 18:07:37 +0000 | [diff] [blame] | 353 | ) |
Dave Rodgman | fbc2322 | 2022-11-24 18:07:37 +0000 | [diff] [blame] | 354 | |
| 355 | /** |
| 356 | * Put in memory a 32 bits unsigned integer in big-endian order. |
| 357 | * |
| 358 | * \param n 32 bits unsigned integer to put in memory. |
| 359 | * \param data Base address of the memory where to put the 32 |
| 360 | * bits unsigned integer in. |
| 361 | * \param offset Offset from \p data where to put the most significant |
| 362 | * byte of the 32 bits unsigned integer \p n. |
| 363 | */ |
Dave Rodgman | 914c632 | 2023-03-01 09:30:14 +0000 | [diff] [blame] | 364 | #define MBEDTLS_PUT_UINT32_BE(n, data, offset) \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 365 | { \ |
Dave Rodgman | 914c632 | 2023-03-01 09:30:14 +0000 | [diff] [blame] | 366 | if (MBEDTLS_IS_BIG_ENDIAN) \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 367 | { \ |
Dave Rodgman | 914c632 | 2023-03-01 09:30:14 +0000 | [diff] [blame] | 368 | mbedtls_put_unaligned_uint32((data) + (offset), (uint32_t) (n)); \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 369 | } \ |
| 370 | else \ |
| 371 | { \ |
| 372 | mbedtls_put_unaligned_uint32((data) + (offset), MBEDTLS_BSWAP32((uint32_t) (n))); \ |
| 373 | } \ |
| 374 | } |
Dave Rodgman | fbc2322 | 2022-11-24 18:07:37 +0000 | [diff] [blame] | 375 | |
| 376 | /** |
| 377 | * Get the unsigned 32 bits integer corresponding to four bytes in |
| 378 | * little-endian order (LSB 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 least 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_LE(data, offset) \ |
| 386 | ((MBEDTLS_IS_BIG_ENDIAN) \ |
Dave Rodgman | a5110b0 | 2022-11-28 14:48:45 +0000 | [diff] [blame] | 387 | ? MBEDTLS_BSWAP32(mbedtls_get_unaligned_uint32((data) + (offset))) \ |
| 388 | : mbedtls_get_unaligned_uint32((data) + (offset)) \ |
Dave Rodgman | fbc2322 | 2022-11-24 18:07:37 +0000 | [diff] [blame] | 389 | ) |
Dave Rodgman | a5110b0 | 2022-11-28 14:48:45 +0000 | [diff] [blame] | 390 | |
Dave Rodgman | fbc2322 | 2022-11-24 18:07:37 +0000 | [diff] [blame] | 391 | |
| 392 | /** |
| 393 | * Put in memory a 32 bits unsigned integer in little-endian order. |
| 394 | * |
| 395 | * \param n 32 bits unsigned integer to put in memory. |
| 396 | * \param data Base address of the memory where to put the 32 |
| 397 | * bits unsigned integer in. |
| 398 | * \param offset Offset from \p data where to put the least significant |
| 399 | * byte of the 32 bits unsigned integer \p n. |
| 400 | */ |
Dave Rodgman | 914c632 | 2023-03-01 09:30:14 +0000 | [diff] [blame] | 401 | #define MBEDTLS_PUT_UINT32_LE(n, data, offset) \ |
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 | if (MBEDTLS_IS_BIG_ENDIAN) \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 404 | { \ |
| 405 | mbedtls_put_unaligned_uint32((data) + (offset), MBEDTLS_BSWAP32((uint32_t) (n))); \ |
| 406 | } \ |
| 407 | else \ |
| 408 | { \ |
Dave Rodgman | 914c632 | 2023-03-01 09:30:14 +0000 | [diff] [blame] | 409 | mbedtls_put_unaligned_uint32((data) + (offset), ((uint32_t) (n))); \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 410 | } \ |
| 411 | } |
Dave Rodgman | fbc2322 | 2022-11-24 18:07:37 +0000 | [diff] [blame] | 412 | |
| 413 | /** |
| 414 | * Get the unsigned 16 bits integer corresponding to two bytes in |
| 415 | * little-endian order (LSB first). |
| 416 | * |
| 417 | * \param data Base address of the memory to get the two bytes from. |
| 418 | * \param offset Offset from \p data of the first and least significant |
| 419 | * byte of the two bytes to build the 16 bits unsigned |
| 420 | * integer from. |
| 421 | */ |
Dave Rodgman | 914c632 | 2023-03-01 09:30:14 +0000 | [diff] [blame] | 422 | #define MBEDTLS_GET_UINT16_LE(data, offset) \ |
| 423 | ((MBEDTLS_IS_BIG_ENDIAN) \ |
Dave Rodgman | a5110b0 | 2022-11-28 14:48:45 +0000 | [diff] [blame] | 424 | ? MBEDTLS_BSWAP16(mbedtls_get_unaligned_uint16((data) + (offset))) \ |
| 425 | : mbedtls_get_unaligned_uint16((data) + (offset)) \ |
Dave Rodgman | fbc2322 | 2022-11-24 18:07:37 +0000 | [diff] [blame] | 426 | ) |
Dave Rodgman | fbc2322 | 2022-11-24 18:07:37 +0000 | [diff] [blame] | 427 | |
| 428 | /** |
| 429 | * Put in memory a 16 bits unsigned integer in little-endian order. |
| 430 | * |
| 431 | * \param n 16 bits unsigned integer to put in memory. |
| 432 | * \param data Base address of the memory where to put the 16 |
| 433 | * bits unsigned integer in. |
| 434 | * \param offset Offset from \p data where to put the least significant |
| 435 | * byte of the 16 bits unsigned integer \p n. |
| 436 | */ |
Dave Rodgman | 914c632 | 2023-03-01 09:30:14 +0000 | [diff] [blame] | 437 | #define MBEDTLS_PUT_UINT16_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_uint16((data) + (offset), MBEDTLS_BSWAP16((uint16_t) (n))); \ |
| 442 | } \ |
| 443 | else \ |
| 444 | { \ |
Dave Rodgman | 914c632 | 2023-03-01 09:30:14 +0000 | [diff] [blame] | 445 | mbedtls_put_unaligned_uint16((data) + (offset), (uint16_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 | * big-endian order (MSB 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 most 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_BE(data, offset) \ |
| 459 | ((MBEDTLS_IS_BIG_ENDIAN) \ |
Dave Rodgman | a5110b0 | 2022-11-28 14:48:45 +0000 | [diff] [blame] | 460 | ? mbedtls_get_unaligned_uint16((data) + (offset)) \ |
| 461 | : MBEDTLS_BSWAP16(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 big-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 most 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_BE(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 | { \ |
Dave Rodgman | 914c632 | 2023-03-01 09:30:14 +0000 | [diff] [blame] | 477 | mbedtls_put_unaligned_uint16((data) + (offset), (uint16_t) (n)); \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 478 | } \ |
| 479 | else \ |
| 480 | { \ |
| 481 | mbedtls_put_unaligned_uint16((data) + (offset), MBEDTLS_BSWAP16((uint16_t) (n))); \ |
| 482 | } \ |
| 483 | } |
Dave Rodgman | fbc2322 | 2022-11-24 18:07:37 +0000 | [diff] [blame] | 484 | |
| 485 | /** |
| 486 | * Get the unsigned 24 bits integer corresponding to three bytes in |
| 487 | * big-endian order (MSB first). |
| 488 | * |
| 489 | * \param data Base address of the memory to get the three bytes from. |
| 490 | * \param offset Offset from \p data of the first and most significant |
| 491 | * byte of the three bytes to build the 24 bits unsigned |
| 492 | * integer from. |
| 493 | */ |
Dave Rodgman | 914c632 | 2023-03-01 09:30:14 +0000 | [diff] [blame] | 494 | #define MBEDTLS_GET_UINT24_BE(data, offset) \ |
| 495 | ( \ |
| 496 | ((uint32_t) (data)[(offset)] << 16) \ |
| 497 | | ((uint32_t) (data)[(offset) + 1] << 8) \ |
| 498 | | ((uint32_t) (data)[(offset) + 2]) \ |
Dave Rodgman | fbc2322 | 2022-11-24 18:07:37 +0000 | [diff] [blame] | 499 | ) |
Dave Rodgman | fbc2322 | 2022-11-24 18:07:37 +0000 | [diff] [blame] | 500 | |
| 501 | /** |
| 502 | * Put in memory a 24 bits unsigned integer in big-endian order. |
| 503 | * |
| 504 | * \param n 24 bits unsigned integer to put in memory. |
| 505 | * \param data Base address of the memory where to put the 24 |
| 506 | * bits unsigned integer in. |
| 507 | * \param offset Offset from \p data where to put the most significant |
| 508 | * byte of the 24 bits unsigned integer \p n. |
| 509 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 510 | #define MBEDTLS_PUT_UINT24_BE(n, data, offset) \ |
Dave Rodgman | 914c632 | 2023-03-01 09:30:14 +0000 | [diff] [blame] | 511 | { \ |
| 512 | (data)[(offset)] = MBEDTLS_BYTE_2(n); \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 513 | (data)[(offset) + 1] = MBEDTLS_BYTE_1(n); \ |
| 514 | (data)[(offset) + 2] = MBEDTLS_BYTE_0(n); \ |
| 515 | } |
Dave Rodgman | fbc2322 | 2022-11-24 18:07:37 +0000 | [diff] [blame] | 516 | |
| 517 | /** |
| 518 | * Get the unsigned 24 bits integer corresponding to three bytes in |
| 519 | * little-endian order (LSB first). |
| 520 | * |
| 521 | * \param data Base address of the memory to get the three bytes from. |
| 522 | * \param offset Offset from \p data of the first and least significant |
| 523 | * byte of the three bytes to build the 24 bits unsigned |
| 524 | * integer from. |
| 525 | */ |
Dave Rodgman | 914c632 | 2023-03-01 09:30:14 +0000 | [diff] [blame] | 526 | #define MBEDTLS_GET_UINT24_LE(data, offset) \ |
| 527 | ( \ |
| 528 | ((uint32_t) (data)[(offset)]) \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 529 | | ((uint32_t) (data)[(offset) + 1] << 8) \ |
| 530 | | ((uint32_t) (data)[(offset) + 2] << 16) \ |
Dave Rodgman | fbc2322 | 2022-11-24 18:07:37 +0000 | [diff] [blame] | 531 | ) |
Dave Rodgman | fbc2322 | 2022-11-24 18:07:37 +0000 | [diff] [blame] | 532 | |
| 533 | /** |
| 534 | * Put in memory a 24 bits unsigned integer in little-endian order. |
| 535 | * |
| 536 | * \param n 24 bits unsigned integer to put in memory. |
| 537 | * \param data Base address of the memory where to put the 24 |
| 538 | * bits unsigned integer in. |
| 539 | * \param offset Offset from \p data where to put the least significant |
| 540 | * byte of the 24 bits unsigned integer \p n. |
| 541 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 542 | #define MBEDTLS_PUT_UINT24_LE(n, data, offset) \ |
Dave Rodgman | 914c632 | 2023-03-01 09:30:14 +0000 | [diff] [blame] | 543 | { \ |
| 544 | (data)[(offset)] = MBEDTLS_BYTE_0(n); \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 545 | (data)[(offset) + 1] = MBEDTLS_BYTE_1(n); \ |
| 546 | (data)[(offset) + 2] = MBEDTLS_BYTE_2(n); \ |
| 547 | } |
Dave Rodgman | fbc2322 | 2022-11-24 18:07:37 +0000 | [diff] [blame] | 548 | |
| 549 | /** |
| 550 | * Get the unsigned 64 bits integer corresponding to eight bytes in |
| 551 | * big-endian order (MSB first). |
| 552 | * |
| 553 | * \param data Base address of the memory to get the eight bytes from. |
| 554 | * \param offset Offset from \p data of the first and most significant |
| 555 | * byte of the eight bytes to build the 64 bits unsigned |
| 556 | * integer from. |
| 557 | */ |
Dave Rodgman | 914c632 | 2023-03-01 09:30:14 +0000 | [diff] [blame] | 558 | #define MBEDTLS_GET_UINT64_BE(data, offset) \ |
| 559 | ((MBEDTLS_IS_BIG_ENDIAN) \ |
Dave Rodgman | a5110b0 | 2022-11-28 14:48:45 +0000 | [diff] [blame] | 560 | ? mbedtls_get_unaligned_uint64((data) + (offset)) \ |
| 561 | : MBEDTLS_BSWAP64(mbedtls_get_unaligned_uint64((data) + (offset))) \ |
Dave Rodgman | fbc2322 | 2022-11-24 18:07:37 +0000 | [diff] [blame] | 562 | ) |
Dave Rodgman | fbc2322 | 2022-11-24 18:07:37 +0000 | [diff] [blame] | 563 | |
| 564 | /** |
| 565 | * Put in memory a 64 bits unsigned integer in big-endian order. |
| 566 | * |
| 567 | * \param n 64 bits unsigned integer to put in memory. |
| 568 | * \param data Base address of the memory where to put the 64 |
| 569 | * bits unsigned integer in. |
| 570 | * \param offset Offset from \p data where to put the most significant |
| 571 | * byte of the 64 bits unsigned integer \p n. |
| 572 | */ |
Dave Rodgman | 914c632 | 2023-03-01 09:30:14 +0000 | [diff] [blame] | 573 | #define MBEDTLS_PUT_UINT64_BE(n, data, offset) \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 574 | { \ |
Dave Rodgman | 914c632 | 2023-03-01 09:30:14 +0000 | [diff] [blame] | 575 | if (MBEDTLS_IS_BIG_ENDIAN) \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 576 | { \ |
Dave Rodgman | 914c632 | 2023-03-01 09:30:14 +0000 | [diff] [blame] | 577 | mbedtls_put_unaligned_uint64((data) + (offset), (uint64_t) (n)); \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 578 | } \ |
| 579 | else \ |
| 580 | { \ |
| 581 | mbedtls_put_unaligned_uint64((data) + (offset), MBEDTLS_BSWAP64((uint64_t) (n))); \ |
| 582 | } \ |
| 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 | * little-endian order (LSB 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 least 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_LE(data, offset) \ |
| 595 | ((MBEDTLS_IS_BIG_ENDIAN) \ |
Dave Rodgman | a5110b0 | 2022-11-28 14:48:45 +0000 | [diff] [blame] | 596 | ? MBEDTLS_BSWAP64(mbedtls_get_unaligned_uint64((data) + (offset))) \ |
| 597 | : 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 little-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 least 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_LE(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 | { \ |
| 613 | mbedtls_put_unaligned_uint64((data) + (offset), MBEDTLS_BSWAP64((uint64_t) (n))); \ |
| 614 | } \ |
| 615 | else \ |
| 616 | { \ |
Dave Rodgman | 914c632 | 2023-03-01 09:30:14 +0000 | [diff] [blame] | 617 | mbedtls_put_unaligned_uint64((data) + (offset), (uint64_t) (n)); \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 618 | } \ |
| 619 | } |
Dave Rodgman | fbc2322 | 2022-11-24 18:07:37 +0000 | [diff] [blame] | 620 | |
| 621 | #endif /* MBEDTLS_LIBRARY_ALIGNMENT_H */ |