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