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