blob: 4aab8e02751f5c2050ea438a2d9dd4f3dc925580 [file] [log] [blame]
Dave Rodgmanfbc23222022-11-24 18:07:37 +00001/**
2 * \file alignment.h
3 *
4 * \brief Utility code for dealing with unaligned memory accesses
5 */
6/*
7 * Copyright The Mbed TLS Contributors
Dave Rodgman16799db2023-11-02 19:47:20 +00008 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Dave Rodgmanfbc23222022-11-24 18:07:37 +00009 */
10
11#ifndef MBEDTLS_LIBRARY_ALIGNMENT_H
12#define MBEDTLS_LIBRARY_ALIGNMENT_H
13
14#include <stdint.h>
Dave Rodgman96d61d12022-11-24 19:33:22 +000015#include <string.h>
Dave Rodgmanf7f1f742022-11-28 14:52:45 +000016#include <stdlib.h>
Dave Rodgmanfbc23222022-11-24 18:07:37 +000017
Dave Rodgmanb9cd19b2022-12-30 21:32:03 +000018/*
19 * Define MBEDTLS_EFFICIENT_UNALIGNED_ACCESS for architectures where unaligned memory
Dave Rodgman7f376fa2023-01-05 12:25:15 +000020 * accesses are known to be efficient.
21 *
22 * All functions defined here will behave correctly regardless, but might be less
23 * efficient when this is not defined.
Dave Rodgmanb9cd19b2022-12-30 21:32:03 +000024 */
25#if defined(__ARM_FEATURE_UNALIGNED) \
Dave Rodgmanc5cc7272023-09-15 11:41:17 +010026 || defined(MBEDTLS_ARCH_IS_X86) || defined(MBEDTLS_ARCH_IS_X64) \
Dave Rodgman0a487172023-09-15 11:52:06 +010027 || defined(MBEDTLS_PLATFORM_IS_WINDOWS_ON_ARM64)
Dave Rodgmanb9cd19b2022-12-30 21:32:03 +000028/*
29 * __ARM_FEATURE_UNALIGNED is defined where appropriate by armcc, gcc 7, clang 9
Dave Rodgman7f376fa2023-01-05 12:25:15 +000030 * (and later versions) for Arm v7 and later; all x86 platforms should have
31 * efficient unaligned access.
Dave Rodgman78fc0bd2023-08-08 10:36:15 +010032 *
33 * https://learn.microsoft.com/en-us/cpp/build/arm64-windows-abi-conventions?view=msvc-170#alignment
34 * specifies that on Windows-on-Arm64, unaligned access is safe (except for uncached
35 * device memory).
Dave Rodgmanb9cd19b2022-12-30 21:32:03 +000036 */
37#define MBEDTLS_EFFICIENT_UNALIGNED_ACCESS
38#endif
39
Dave Rodgman96d61d12022-11-24 19:33:22 +000040/**
Dave Rodgmana360e192022-11-28 14:44:05 +000041 * Read the unsigned 16 bits integer from the given address, which need not
42 * be aligned.
43 *
44 * \param p pointer to 2 bytes of data
45 * \return Data at the given address
46 */
Gilles Peskine449bd832023-01-11 14:50:10 +010047inline uint16_t mbedtls_get_unaligned_uint16(const void *p)
Dave Rodgmana360e192022-11-28 14:44:05 +000048{
49 uint16_t r;
Gilles Peskine449bd832023-01-11 14:50:10 +010050 memcpy(&r, p, sizeof(r));
Dave Rodgmana360e192022-11-28 14:44:05 +000051 return r;
52}
53
54/**
55 * Write the unsigned 16 bits integer to the given address, which need not
56 * be aligned.
57 *
58 * \param p pointer to 2 bytes of data
59 * \param x data to write
60 */
Gilles Peskine449bd832023-01-11 14:50:10 +010061inline void mbedtls_put_unaligned_uint16(void *p, uint16_t x)
Dave Rodgmana360e192022-11-28 14:44:05 +000062{
Gilles Peskine449bd832023-01-11 14:50:10 +010063 memcpy(p, &x, sizeof(x));
Dave Rodgmana360e192022-11-28 14:44:05 +000064}
65
66/**
Dave Rodgman96d61d12022-11-24 19:33:22 +000067 * Read the unsigned 32 bits integer from the given address, which need not
68 * be aligned.
Dave Rodgmanfbc23222022-11-24 18:07:37 +000069 *
Dave Rodgman96d61d12022-11-24 19:33:22 +000070 * \param p pointer to 4 bytes of data
Dave Rodgman875d2382022-11-24 20:43:15 +000071 * \return Data at the given address
Dave Rodgmanfbc23222022-11-24 18:07:37 +000072 */
Gilles Peskine449bd832023-01-11 14:50:10 +010073inline uint32_t mbedtls_get_unaligned_uint32(const void *p)
Dave Rodgman96d61d12022-11-24 19:33:22 +000074{
75 uint32_t r;
Gilles Peskine449bd832023-01-11 14:50:10 +010076 memcpy(&r, p, sizeof(r));
Dave Rodgman96d61d12022-11-24 19:33:22 +000077 return r;
78}
Dave Rodgmanfbc23222022-11-24 18:07:37 +000079
Dave Rodgman96d61d12022-11-24 19:33:22 +000080/**
81 * Write the unsigned 32 bits integer to the given address, which need not
82 * be aligned.
83 *
84 * \param p pointer to 4 bytes of data
85 * \param x data to write
86 */
Gilles Peskine449bd832023-01-11 14:50:10 +010087inline void mbedtls_put_unaligned_uint32(void *p, uint32_t x)
Dave Rodgman96d61d12022-11-24 19:33:22 +000088{
Gilles Peskine449bd832023-01-11 14:50:10 +010089 memcpy(p, &x, sizeof(x));
Dave Rodgman96d61d12022-11-24 19:33:22 +000090}
Dave Rodgmanfbc23222022-11-24 18:07:37 +000091
Dave Rodgmana360e192022-11-28 14:44:05 +000092/**
93 * Read the unsigned 64 bits integer from the given address, which need not
94 * be aligned.
95 *
96 * \param p pointer to 8 bytes of data
97 * \return Data at the given address
98 */
Gilles Peskine449bd832023-01-11 14:50:10 +010099inline uint64_t mbedtls_get_unaligned_uint64(const void *p)
Dave Rodgmana360e192022-11-28 14:44:05 +0000100{
101 uint64_t r;
Gilles Peskine449bd832023-01-11 14:50:10 +0100102 memcpy(&r, p, sizeof(r));
Dave Rodgmana360e192022-11-28 14:44:05 +0000103 return r;
104}
105
106/**
107 * Write the unsigned 64 bits integer to the given address, which need not
108 * be aligned.
109 *
110 * \param p pointer to 8 bytes of data
111 * \param x data to write
112 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100113inline void mbedtls_put_unaligned_uint64(void *p, uint64_t x)
Dave Rodgmana360e192022-11-28 14:44:05 +0000114{
Gilles Peskine449bd832023-01-11 14:50:10 +0100115 memcpy(p, &x, sizeof(x));
Dave Rodgmana360e192022-11-28 14:44:05 +0000116}
117
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000118/** Byte Reading Macros
119 *
120 * Given a multi-byte integer \p x, MBEDTLS_BYTE_n retrieves the n-th
121 * byte from x, where byte 0 is the least significant byte.
122 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100123#define MBEDTLS_BYTE_0(x) ((uint8_t) ((x) & 0xff))
Dave Rodgman914c6322023-03-01 09:30:14 +0000124#define MBEDTLS_BYTE_1(x) ((uint8_t) (((x) >> 8) & 0xff))
Gilles Peskine449bd832023-01-11 14:50:10 +0100125#define MBEDTLS_BYTE_2(x) ((uint8_t) (((x) >> 16) & 0xff))
126#define MBEDTLS_BYTE_3(x) ((uint8_t) (((x) >> 24) & 0xff))
127#define MBEDTLS_BYTE_4(x) ((uint8_t) (((x) >> 32) & 0xff))
128#define MBEDTLS_BYTE_5(x) ((uint8_t) (((x) >> 40) & 0xff))
129#define MBEDTLS_BYTE_6(x) ((uint8_t) (((x) >> 48) & 0xff))
130#define MBEDTLS_BYTE_7(x) ((uint8_t) (((x) >> 56) & 0xff))
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000131
Dave Rodgmanf7f1f742022-11-28 14:52:45 +0000132/*
133 * Detect GCC built-in byteswap routines
134 */
135#if defined(__GNUC__) && defined(__GNUC_PREREQ)
Gilles Peskine449bd832023-01-11 14:50:10 +0100136#if __GNUC_PREREQ(4, 8)
Dave Rodgmanf7f1f742022-11-28 14:52:45 +0000137#define MBEDTLS_BSWAP16 __builtin_bswap16
138#endif /* __GNUC_PREREQ(4,8) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100139#if __GNUC_PREREQ(4, 3)
Dave Rodgmanf7f1f742022-11-28 14:52:45 +0000140#define MBEDTLS_BSWAP32 __builtin_bswap32
141#define MBEDTLS_BSWAP64 __builtin_bswap64
142#endif /* __GNUC_PREREQ(4,3) */
143#endif /* defined(__GNUC__) && defined(__GNUC_PREREQ) */
144
145/*
146 * Detect Clang built-in byteswap routines
147 */
148#if defined(__clang__) && defined(__has_builtin)
Dave Rodgmane47899d2023-02-28 17:39:03 +0000149#if __has_builtin(__builtin_bswap16) && !defined(MBEDTLS_BSWAP16)
Dave Rodgmanf7f1f742022-11-28 14:52:45 +0000150#define MBEDTLS_BSWAP16 __builtin_bswap16
151#endif /* __has_builtin(__builtin_bswap16) */
Dave Rodgmane47899d2023-02-28 17:39:03 +0000152#if __has_builtin(__builtin_bswap32) && !defined(MBEDTLS_BSWAP32)
Dave Rodgmanf7f1f742022-11-28 14:52:45 +0000153#define MBEDTLS_BSWAP32 __builtin_bswap32
154#endif /* __has_builtin(__builtin_bswap32) */
Dave Rodgmane47899d2023-02-28 17:39:03 +0000155#if __has_builtin(__builtin_bswap64) && !defined(MBEDTLS_BSWAP64)
Dave Rodgmanf7f1f742022-11-28 14:52:45 +0000156#define MBEDTLS_BSWAP64 __builtin_bswap64
157#endif /* __has_builtin(__builtin_bswap64) */
158#endif /* defined(__clang__) && defined(__has_builtin) */
159
160/*
161 * Detect MSVC built-in byteswap routines
162 */
163#if defined(_MSC_VER)
Dave Rodgmane47899d2023-02-28 17:39:03 +0000164#if !defined(MBEDTLS_BSWAP16)
Dave Rodgmanf7f1f742022-11-28 14:52:45 +0000165#define MBEDTLS_BSWAP16 _byteswap_ushort
Dave Rodgmane47899d2023-02-28 17:39:03 +0000166#endif
167#if !defined(MBEDTLS_BSWAP32)
Dave Rodgmanf7f1f742022-11-28 14:52:45 +0000168#define MBEDTLS_BSWAP32 _byteswap_ulong
Dave Rodgmane47899d2023-02-28 17:39:03 +0000169#endif
170#if !defined(MBEDTLS_BSWAP64)
Dave Rodgmanf7f1f742022-11-28 14:52:45 +0000171#define MBEDTLS_BSWAP64 _byteswap_uint64
Dave Rodgmane47899d2023-02-28 17:39:03 +0000172#endif
Dave Rodgmanf7f1f742022-11-28 14:52:45 +0000173#endif /* defined(_MSC_VER) */
174
Dave Rodgman2dae4b32022-11-30 12:07:36 +0000175/* Detect armcc built-in byteswap routine */
Dave Rodgmane47899d2023-02-28 17:39:03 +0000176#if defined(__ARMCC_VERSION) && (__ARMCC_VERSION >= 410000) && !defined(MBEDTLS_BSWAP32)
Tom Cosgrovef2b5a132023-04-26 17:00:12 +0100177#if defined(__ARM_ACLE) /* ARM Compiler 6 - earlier versions don't need a header */
178#include <arm_acle.h>
179#endif
Dave Rodgman2dae4b32022-11-30 12:07:36 +0000180#define MBEDTLS_BSWAP32 __rev
181#endif
182
Dave Rodgmanf7f1f742022-11-28 14:52:45 +0000183/*
184 * Where compiler built-ins are not present, fall back to C code that the
185 * compiler may be able to detect and transform into the relevant bswap or
186 * similar instruction.
187 */
188#if !defined(MBEDTLS_BSWAP16)
Gilles Peskine449bd832023-01-11 14:50:10 +0100189static inline uint16_t mbedtls_bswap16(uint16_t x)
190{
Dave Rodgman6298b242022-11-28 14:51:49 +0000191 return
Gilles Peskine449bd832023-01-11 14:50:10 +0100192 (x & 0x00ff) << 8 |
193 (x & 0xff00) >> 8;
Dave Rodgman6298b242022-11-28 14:51:49 +0000194}
195#define MBEDTLS_BSWAP16 mbedtls_bswap16
Dave Rodgmanf7f1f742022-11-28 14:52:45 +0000196#endif /* !defined(MBEDTLS_BSWAP16) */
Dave Rodgman6298b242022-11-28 14:51:49 +0000197
Dave Rodgmanf7f1f742022-11-28 14:52:45 +0000198#if !defined(MBEDTLS_BSWAP32)
Gilles Peskine449bd832023-01-11 14:50:10 +0100199static inline uint32_t mbedtls_bswap32(uint32_t x)
200{
Dave Rodgman6298b242022-11-28 14:51:49 +0000201 return
Gilles Peskine449bd832023-01-11 14:50:10 +0100202 (x & 0x000000ff) << 24 |
203 (x & 0x0000ff00) << 8 |
204 (x & 0x00ff0000) >> 8 |
205 (x & 0xff000000) >> 24;
Dave Rodgman6298b242022-11-28 14:51:49 +0000206}
207#define MBEDTLS_BSWAP32 mbedtls_bswap32
Dave Rodgmanf7f1f742022-11-28 14:52:45 +0000208#endif /* !defined(MBEDTLS_BSWAP32) */
Dave Rodgman6298b242022-11-28 14:51:49 +0000209
Dave Rodgmanf7f1f742022-11-28 14:52:45 +0000210#if !defined(MBEDTLS_BSWAP64)
Gilles Peskine449bd832023-01-11 14:50:10 +0100211static inline uint64_t mbedtls_bswap64(uint64_t x)
212{
Dave Rodgman6298b242022-11-28 14:51:49 +0000213 return
Tom Cosgrovebbe166e2023-03-08 13:23:24 +0000214 (x & 0x00000000000000ffULL) << 56 |
215 (x & 0x000000000000ff00ULL) << 40 |
216 (x & 0x0000000000ff0000ULL) << 24 |
217 (x & 0x00000000ff000000ULL) << 8 |
218 (x & 0x000000ff00000000ULL) >> 8 |
219 (x & 0x0000ff0000000000ULL) >> 24 |
220 (x & 0x00ff000000000000ULL) >> 40 |
221 (x & 0xff00000000000000ULL) >> 56;
Dave Rodgman6298b242022-11-28 14:51:49 +0000222}
223#define MBEDTLS_BSWAP64 mbedtls_bswap64
Dave Rodgmanf7f1f742022-11-28 14:52:45 +0000224#endif /* !defined(MBEDTLS_BSWAP64) */
Dave Rodgman6298b242022-11-28 14:51:49 +0000225
Dave Rodgmane5c42592022-11-28 14:47:46 +0000226#if !defined(__BYTE_ORDER__)
227static const uint16_t mbedtls_byte_order_detector = { 0x100 };
228#define MBEDTLS_IS_BIG_ENDIAN (*((unsigned char *) (&mbedtls_byte_order_detector)) == 0x01)
229#else
230#define MBEDTLS_IS_BIG_ENDIAN ((__BYTE_ORDER__) == (__ORDER_BIG_ENDIAN__))
231#endif /* !defined(__BYTE_ORDER__) */
232
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000233/**
234 * Get the unsigned 32 bits integer corresponding to four bytes in
235 * big-endian order (MSB first).
236 *
237 * \param data Base address of the memory to get the four bytes from.
238 * \param offset Offset from \p data of the first and most significant
239 * byte of the four bytes to build the 32 bits unsigned
240 * integer from.
241 */
Dave Rodgman914c6322023-03-01 09:30:14 +0000242#define MBEDTLS_GET_UINT32_BE(data, offset) \
243 ((MBEDTLS_IS_BIG_ENDIAN) \
Dave Rodgmana5110b02022-11-28 14:48:45 +0000244 ? mbedtls_get_unaligned_uint32((data) + (offset)) \
245 : MBEDTLS_BSWAP32(mbedtls_get_unaligned_uint32((data) + (offset))) \
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000246 )
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000247
248/**
249 * Put in memory a 32 bits unsigned integer in big-endian order.
250 *
251 * \param n 32 bits unsigned integer to put in memory.
252 * \param data Base address of the memory where to put the 32
253 * bits unsigned integer in.
254 * \param offset Offset from \p data where to put the most significant
255 * byte of the 32 bits unsigned integer \p n.
256 */
Dave Rodgman914c6322023-03-01 09:30:14 +0000257#define MBEDTLS_PUT_UINT32_BE(n, data, offset) \
Gilles Peskine449bd832023-01-11 14:50:10 +0100258 { \
Dave Rodgman914c6322023-03-01 09:30:14 +0000259 if (MBEDTLS_IS_BIG_ENDIAN) \
Gilles Peskine449bd832023-01-11 14:50:10 +0100260 { \
Dave Rodgman914c6322023-03-01 09:30:14 +0000261 mbedtls_put_unaligned_uint32((data) + (offset), (uint32_t) (n)); \
Gilles Peskine449bd832023-01-11 14:50:10 +0100262 } \
263 else \
264 { \
265 mbedtls_put_unaligned_uint32((data) + (offset), MBEDTLS_BSWAP32((uint32_t) (n))); \
266 } \
267 }
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000268
269/**
270 * Get the unsigned 32 bits integer corresponding to four bytes in
271 * little-endian order (LSB first).
272 *
273 * \param data Base address of the memory to get the four bytes from.
274 * \param offset Offset from \p data of the first and least significant
275 * byte of the four bytes to build the 32 bits unsigned
276 * integer from.
277 */
Dave Rodgman914c6322023-03-01 09:30:14 +0000278#define MBEDTLS_GET_UINT32_LE(data, offset) \
279 ((MBEDTLS_IS_BIG_ENDIAN) \
Dave Rodgmana5110b02022-11-28 14:48:45 +0000280 ? MBEDTLS_BSWAP32(mbedtls_get_unaligned_uint32((data) + (offset))) \
281 : mbedtls_get_unaligned_uint32((data) + (offset)) \
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000282 )
Dave Rodgmana5110b02022-11-28 14:48:45 +0000283
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000284
285/**
286 * Put in memory a 32 bits unsigned integer in little-endian order.
287 *
288 * \param n 32 bits unsigned integer to put in memory.
289 * \param data Base address of the memory where to put the 32
290 * bits unsigned integer in.
291 * \param offset Offset from \p data where to put the least significant
292 * byte of the 32 bits unsigned integer \p n.
293 */
Dave Rodgman914c6322023-03-01 09:30:14 +0000294#define MBEDTLS_PUT_UINT32_LE(n, data, offset) \
Gilles Peskine449bd832023-01-11 14:50:10 +0100295 { \
Dave Rodgman914c6322023-03-01 09:30:14 +0000296 if (MBEDTLS_IS_BIG_ENDIAN) \
Gilles Peskine449bd832023-01-11 14:50:10 +0100297 { \
298 mbedtls_put_unaligned_uint32((data) + (offset), MBEDTLS_BSWAP32((uint32_t) (n))); \
299 } \
300 else \
301 { \
Dave Rodgman914c6322023-03-01 09:30:14 +0000302 mbedtls_put_unaligned_uint32((data) + (offset), ((uint32_t) (n))); \
Gilles Peskine449bd832023-01-11 14:50:10 +0100303 } \
304 }
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000305
306/**
307 * Get the unsigned 16 bits integer corresponding to two bytes in
308 * little-endian order (LSB first).
309 *
310 * \param data Base address of the memory to get the two bytes from.
311 * \param offset Offset from \p data of the first and least significant
312 * byte of the two bytes to build the 16 bits unsigned
313 * integer from.
314 */
Dave Rodgman914c6322023-03-01 09:30:14 +0000315#define MBEDTLS_GET_UINT16_LE(data, offset) \
316 ((MBEDTLS_IS_BIG_ENDIAN) \
Dave Rodgmana5110b02022-11-28 14:48:45 +0000317 ? MBEDTLS_BSWAP16(mbedtls_get_unaligned_uint16((data) + (offset))) \
318 : mbedtls_get_unaligned_uint16((data) + (offset)) \
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000319 )
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000320
321/**
322 * Put in memory a 16 bits unsigned integer in little-endian order.
323 *
324 * \param n 16 bits unsigned integer to put in memory.
325 * \param data Base address of the memory where to put the 16
326 * bits unsigned integer in.
327 * \param offset Offset from \p data where to put the least significant
328 * byte of the 16 bits unsigned integer \p n.
329 */
Dave Rodgman914c6322023-03-01 09:30:14 +0000330#define MBEDTLS_PUT_UINT16_LE(n, data, offset) \
Gilles Peskine449bd832023-01-11 14:50:10 +0100331 { \
Dave Rodgman914c6322023-03-01 09:30:14 +0000332 if (MBEDTLS_IS_BIG_ENDIAN) \
Gilles Peskine449bd832023-01-11 14:50:10 +0100333 { \
334 mbedtls_put_unaligned_uint16((data) + (offset), MBEDTLS_BSWAP16((uint16_t) (n))); \
335 } \
336 else \
337 { \
Dave Rodgman914c6322023-03-01 09:30:14 +0000338 mbedtls_put_unaligned_uint16((data) + (offset), (uint16_t) (n)); \
Gilles Peskine449bd832023-01-11 14:50:10 +0100339 } \
340 }
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000341
342/**
343 * Get the unsigned 16 bits integer corresponding to two bytes in
344 * big-endian order (MSB first).
345 *
346 * \param data Base address of the memory to get the two bytes from.
347 * \param offset Offset from \p data of the first and most significant
348 * byte of the two bytes to build the 16 bits unsigned
349 * integer from.
350 */
Dave Rodgman914c6322023-03-01 09:30:14 +0000351#define MBEDTLS_GET_UINT16_BE(data, offset) \
352 ((MBEDTLS_IS_BIG_ENDIAN) \
Dave Rodgmana5110b02022-11-28 14:48:45 +0000353 ? mbedtls_get_unaligned_uint16((data) + (offset)) \
354 : MBEDTLS_BSWAP16(mbedtls_get_unaligned_uint16((data) + (offset))) \
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000355 )
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000356
357/**
358 * Put in memory a 16 bits unsigned integer in big-endian order.
359 *
360 * \param n 16 bits unsigned integer to put in memory.
361 * \param data Base address of the memory where to put the 16
362 * bits unsigned integer in.
363 * \param offset Offset from \p data where to put the most significant
364 * byte of the 16 bits unsigned integer \p n.
365 */
Dave Rodgman914c6322023-03-01 09:30:14 +0000366#define MBEDTLS_PUT_UINT16_BE(n, data, offset) \
Gilles Peskine449bd832023-01-11 14:50:10 +0100367 { \
Dave Rodgman914c6322023-03-01 09:30:14 +0000368 if (MBEDTLS_IS_BIG_ENDIAN) \
Gilles Peskine449bd832023-01-11 14:50:10 +0100369 { \
Dave Rodgman914c6322023-03-01 09:30:14 +0000370 mbedtls_put_unaligned_uint16((data) + (offset), (uint16_t) (n)); \
Gilles Peskine449bd832023-01-11 14:50:10 +0100371 } \
372 else \
373 { \
374 mbedtls_put_unaligned_uint16((data) + (offset), MBEDTLS_BSWAP16((uint16_t) (n))); \
375 } \
376 }
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000377
378/**
379 * Get the unsigned 24 bits integer corresponding to three bytes in
380 * big-endian order (MSB first).
381 *
382 * \param data Base address of the memory to get the three bytes from.
383 * \param offset Offset from \p data of the first and most significant
384 * byte of the three bytes to build the 24 bits unsigned
385 * integer from.
386 */
Dave Rodgman914c6322023-03-01 09:30:14 +0000387#define MBEDTLS_GET_UINT24_BE(data, offset) \
388 ( \
389 ((uint32_t) (data)[(offset)] << 16) \
390 | ((uint32_t) (data)[(offset) + 1] << 8) \
391 | ((uint32_t) (data)[(offset) + 2]) \
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000392 )
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000393
394/**
395 * Put in memory a 24 bits unsigned integer in big-endian order.
396 *
397 * \param n 24 bits unsigned integer to put in memory.
398 * \param data Base address of the memory where to put the 24
399 * bits unsigned integer in.
400 * \param offset Offset from \p data where to put the most significant
401 * byte of the 24 bits unsigned integer \p n.
402 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100403#define MBEDTLS_PUT_UINT24_BE(n, data, offset) \
Dave Rodgman914c6322023-03-01 09:30:14 +0000404 { \
405 (data)[(offset)] = MBEDTLS_BYTE_2(n); \
Gilles Peskine449bd832023-01-11 14:50:10 +0100406 (data)[(offset) + 1] = MBEDTLS_BYTE_1(n); \
407 (data)[(offset) + 2] = MBEDTLS_BYTE_0(n); \
408 }
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000409
410/**
411 * Get the unsigned 24 bits integer corresponding to three bytes in
412 * little-endian order (LSB first).
413 *
414 * \param data Base address of the memory to get the three bytes from.
415 * \param offset Offset from \p data of the first and least significant
416 * byte of the three bytes to build the 24 bits unsigned
417 * integer from.
418 */
Dave Rodgman914c6322023-03-01 09:30:14 +0000419#define MBEDTLS_GET_UINT24_LE(data, offset) \
420 ( \
421 ((uint32_t) (data)[(offset)]) \
Gilles Peskine449bd832023-01-11 14:50:10 +0100422 | ((uint32_t) (data)[(offset) + 1] << 8) \
423 | ((uint32_t) (data)[(offset) + 2] << 16) \
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000424 )
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000425
426/**
427 * Put in memory a 24 bits unsigned integer in little-endian order.
428 *
429 * \param n 24 bits unsigned integer to put in memory.
430 * \param data Base address of the memory where to put the 24
431 * bits unsigned integer in.
432 * \param offset Offset from \p data where to put the least significant
433 * byte of the 24 bits unsigned integer \p n.
434 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100435#define MBEDTLS_PUT_UINT24_LE(n, data, offset) \
Dave Rodgman914c6322023-03-01 09:30:14 +0000436 { \
437 (data)[(offset)] = MBEDTLS_BYTE_0(n); \
Gilles Peskine449bd832023-01-11 14:50:10 +0100438 (data)[(offset) + 1] = MBEDTLS_BYTE_1(n); \
439 (data)[(offset) + 2] = MBEDTLS_BYTE_2(n); \
440 }
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000441
442/**
443 * Get the unsigned 64 bits integer corresponding to eight bytes in
444 * big-endian order (MSB first).
445 *
446 * \param data Base address of the memory to get the eight bytes from.
447 * \param offset Offset from \p data of the first and most significant
448 * byte of the eight bytes to build the 64 bits unsigned
449 * integer from.
450 */
Dave Rodgman914c6322023-03-01 09:30:14 +0000451#define MBEDTLS_GET_UINT64_BE(data, offset) \
452 ((MBEDTLS_IS_BIG_ENDIAN) \
Dave Rodgmana5110b02022-11-28 14:48:45 +0000453 ? mbedtls_get_unaligned_uint64((data) + (offset)) \
454 : MBEDTLS_BSWAP64(mbedtls_get_unaligned_uint64((data) + (offset))) \
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000455 )
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000456
457/**
458 * Put in memory a 64 bits unsigned integer in big-endian order.
459 *
460 * \param n 64 bits unsigned integer to put in memory.
461 * \param data Base address of the memory where to put the 64
462 * bits unsigned integer in.
463 * \param offset Offset from \p data where to put the most significant
464 * byte of the 64 bits unsigned integer \p n.
465 */
Dave Rodgman914c6322023-03-01 09:30:14 +0000466#define MBEDTLS_PUT_UINT64_BE(n, data, offset) \
Gilles Peskine449bd832023-01-11 14:50:10 +0100467 { \
Dave Rodgman914c6322023-03-01 09:30:14 +0000468 if (MBEDTLS_IS_BIG_ENDIAN) \
Gilles Peskine449bd832023-01-11 14:50:10 +0100469 { \
Dave Rodgman914c6322023-03-01 09:30:14 +0000470 mbedtls_put_unaligned_uint64((data) + (offset), (uint64_t) (n)); \
Gilles Peskine449bd832023-01-11 14:50:10 +0100471 } \
472 else \
473 { \
474 mbedtls_put_unaligned_uint64((data) + (offset), MBEDTLS_BSWAP64((uint64_t) (n))); \
475 } \
476 }
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000477
478/**
479 * Get the unsigned 64 bits integer corresponding to eight bytes in
480 * little-endian order (LSB first).
481 *
482 * \param data Base address of the memory to get the eight bytes from.
483 * \param offset Offset from \p data of the first and least significant
484 * byte of the eight bytes to build the 64 bits unsigned
485 * integer from.
486 */
Dave Rodgman914c6322023-03-01 09:30:14 +0000487#define MBEDTLS_GET_UINT64_LE(data, offset) \
488 ((MBEDTLS_IS_BIG_ENDIAN) \
Dave Rodgmana5110b02022-11-28 14:48:45 +0000489 ? MBEDTLS_BSWAP64(mbedtls_get_unaligned_uint64((data) + (offset))) \
490 : mbedtls_get_unaligned_uint64((data) + (offset)) \
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000491 )
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000492
493/**
494 * Put in memory a 64 bits unsigned integer in little-endian order.
495 *
496 * \param n 64 bits unsigned integer to put in memory.
497 * \param data Base address of the memory where to put the 64
498 * bits unsigned integer in.
499 * \param offset Offset from \p data where to put the least significant
500 * byte of the 64 bits unsigned integer \p n.
501 */
Dave Rodgman914c6322023-03-01 09:30:14 +0000502#define MBEDTLS_PUT_UINT64_LE(n, data, offset) \
Gilles Peskine449bd832023-01-11 14:50:10 +0100503 { \
Dave Rodgman914c6322023-03-01 09:30:14 +0000504 if (MBEDTLS_IS_BIG_ENDIAN) \
Gilles Peskine449bd832023-01-11 14:50:10 +0100505 { \
506 mbedtls_put_unaligned_uint64((data) + (offset), MBEDTLS_BSWAP64((uint64_t) (n))); \
507 } \
508 else \
509 { \
Dave Rodgman914c6322023-03-01 09:30:14 +0000510 mbedtls_put_unaligned_uint64((data) + (offset), (uint64_t) (n)); \
Gilles Peskine449bd832023-01-11 14:50:10 +0100511 } \
512 }
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000513
514#endif /* MBEDTLS_LIBRARY_ALIGNMENT_H */