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