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