blob: e7318c2ac320bfc5d45481504e228833782fabb4 [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 Rodgman7d8c99a2024-01-19 14:02:58 +000018#if defined(__GNUC__) && !defined(__ARMCC_VERSION) && !defined(__clang__) \
19 && !defined(__llvm__) && !defined(__INTEL_COMPILER)
20/* Defined if the compiler really is gcc and not clang, etc */
21#define MBEDTLS_COMPILER_IS_GCC
22#define MBEDTLS_GCC_VERSION \
23 (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)
24#endif
25
Dave Rodgmanb9cd19b2022-12-30 21:32:03 +000026/*
27 * Define MBEDTLS_EFFICIENT_UNALIGNED_ACCESS for architectures where unaligned memory
Dave Rodgman7f376fa2023-01-05 12:25:15 +000028 * accesses are known to be efficient.
29 *
30 * All functions defined here will behave correctly regardless, but might be less
31 * efficient when this is not defined.
Dave Rodgmanb9cd19b2022-12-30 21:32:03 +000032 */
33#if defined(__ARM_FEATURE_UNALIGNED) \
Dave Rodgmanc5cc7272023-09-15 11:41:17 +010034 || defined(MBEDTLS_ARCH_IS_X86) || defined(MBEDTLS_ARCH_IS_X64) \
Dave Rodgman0a487172023-09-15 11:52:06 +010035 || defined(MBEDTLS_PLATFORM_IS_WINDOWS_ON_ARM64)
Dave Rodgmanb9cd19b2022-12-30 21:32:03 +000036/*
37 * __ARM_FEATURE_UNALIGNED is defined where appropriate by armcc, gcc 7, clang 9
Dave Rodgman7f376fa2023-01-05 12:25:15 +000038 * (and later versions) for Arm v7 and later; all x86 platforms should have
39 * efficient unaligned access.
Dave Rodgman78fc0bd2023-08-08 10:36:15 +010040 *
41 * https://learn.microsoft.com/en-us/cpp/build/arm64-windows-abi-conventions?view=msvc-170#alignment
42 * specifies that on Windows-on-Arm64, unaligned access is safe (except for uncached
43 * device memory).
Dave Rodgmanb9cd19b2022-12-30 21:32:03 +000044 */
45#define MBEDTLS_EFFICIENT_UNALIGNED_ACCESS
46#endif
47
Dave Rodgmanc5812642024-01-19 14:04:28 +000048#if defined(__IAR_SYSTEMS_ICC__) && \
49 (defined(MBEDTLS_ARCH_IS_ARM64) || defined(MBEDTLS_ARCH_IS_ARM32) \
50 || defined(__ICCRX__) || defined(__ICCRL78__) || defined(__ICCRISCV__))
51#pragma language=save
52#pragma language=extended
53#define MBEDTLS_POP_IAR_LANGUAGE_PRAGMA
54/* IAR recommend this technique for accessing unaligned data in
55 * https://www.iar.com/knowledge/support/technical-notes/compiler/accessing-unaligned-data
56 * This results in a single load / store instruction (if unaligned access is supported).
57 * According to that document, this is only supported on certain architectures.
58 */
59 #define UINT_UNALIGNED
60typedef uint16_t __packed mbedtls_uint16_unaligned_t;
61typedef uint32_t __packed mbedtls_uint32_unaligned_t;
62typedef uint64_t __packed mbedtls_uint64_unaligned_t;
63#elif defined(MBEDTLS_COMPILER_IS_GCC) && (MBEDTLS_GCC_VERSION >= 40504) && \
64 ((MBEDTLS_GCC_VERSION < 90300) || (!defined(MBEDTLS_EFFICIENT_UNALIGNED_ACCESS)))
65/*
66 * Old versions of gcc, depending on how the target is specified, may generate a branch to memcpy
67 * for calls like `memcpy(dest, src, 4)` rather than generating some LDR or LDRB instructions
68 * (similar for stores).
69 * Recent versions where unaligned access is not enabled also do this.
70 *
71 * For performance (and code size, in some cases), we want to avoid the branch and just generate
72 * some inline load/store instructions since the access is small and constant-size.
73 *
74 * The manual states:
75 * "The aligned attribute specifies a minimum alignment for the variable or structure field,
76 * measured in bytes."
77 * https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html
78 *
79 * Tested with several versions of GCC from 4.5.0 up to 9.3.0
80 * We don't enable for older than 4.5.0 as this has not been tested.
81 */
82 #define UINT_UNALIGNED
83typedef uint16_t __attribute__((__aligned__(1))) mbedtls_uint16_unaligned_t;
84typedef uint32_t __attribute__((__aligned__(1))) mbedtls_uint32_unaligned_t;
85typedef uint64_t __attribute__((__aligned__(1))) mbedtls_uint64_unaligned_t;
86 #endif
87
Dave Rodgman96d61d12022-11-24 19:33:22 +000088/**
Dave Rodgmana360e192022-11-28 14:44:05 +000089 * Read the unsigned 16 bits integer from the given address, which need not
90 * be aligned.
91 *
92 * \param p pointer to 2 bytes of data
93 * \return Data at the given address
94 */
Gilles Peskine449bd832023-01-11 14:50:10 +010095inline uint16_t mbedtls_get_unaligned_uint16(const void *p)
Dave Rodgmana360e192022-11-28 14:44:05 +000096{
97 uint16_t r;
Dave Rodgmanc5812642024-01-19 14:04:28 +000098#if defined(UINT_UNALIGNED)
99 mbedtls_uint16_unaligned_t *p16 = (mbedtls_uint16_unaligned_t *) p;
100 r = *p16;
101#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100102 memcpy(&r, p, sizeof(r));
Dave Rodgmanc5812642024-01-19 14:04:28 +0000103#endif
Dave Rodgmana360e192022-11-28 14:44:05 +0000104 return r;
105}
106
107/**
108 * Write the unsigned 16 bits integer to the given address, which need not
109 * be aligned.
110 *
111 * \param p pointer to 2 bytes of data
112 * \param x data to write
113 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100114inline void mbedtls_put_unaligned_uint16(void *p, uint16_t x)
Dave Rodgmana360e192022-11-28 14:44:05 +0000115{
Dave Rodgmanc5812642024-01-19 14:04:28 +0000116#if defined(UINT_UNALIGNED)
117 mbedtls_uint16_unaligned_t *p16 = (mbedtls_uint16_unaligned_t *) p;
118 *p16 = x;
119#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100120 memcpy(p, &x, sizeof(x));
Dave Rodgmanc5812642024-01-19 14:04:28 +0000121#endif
Dave Rodgmana360e192022-11-28 14:44:05 +0000122}
123
124/**
Dave Rodgman96d61d12022-11-24 19:33:22 +0000125 * Read the unsigned 32 bits integer from the given address, which need not
126 * be aligned.
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000127 *
Dave Rodgman96d61d12022-11-24 19:33:22 +0000128 * \param p pointer to 4 bytes of data
Dave Rodgman875d2382022-11-24 20:43:15 +0000129 * \return Data at the given address
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000130 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100131inline uint32_t mbedtls_get_unaligned_uint32(const void *p)
Dave Rodgman96d61d12022-11-24 19:33:22 +0000132{
133 uint32_t r;
Dave Rodgmanc5812642024-01-19 14:04:28 +0000134#if defined(UINT_UNALIGNED)
135 mbedtls_uint32_unaligned_t *p32 = (mbedtls_uint32_unaligned_t *) p;
136 r = *p32;
137#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100138 memcpy(&r, p, sizeof(r));
Dave Rodgmanc5812642024-01-19 14:04:28 +0000139#endif
Dave Rodgman96d61d12022-11-24 19:33:22 +0000140 return r;
141}
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000142
Dave Rodgman96d61d12022-11-24 19:33:22 +0000143/**
144 * Write the unsigned 32 bits integer to the given address, which need not
145 * be aligned.
146 *
147 * \param p pointer to 4 bytes of data
148 * \param x data to write
149 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100150inline void mbedtls_put_unaligned_uint32(void *p, uint32_t x)
Dave Rodgman96d61d12022-11-24 19:33:22 +0000151{
Dave Rodgmanc5812642024-01-19 14:04:28 +0000152#if defined(UINT_UNALIGNED)
153 mbedtls_uint32_unaligned_t *p32 = (mbedtls_uint32_unaligned_t *) p;
154 *p32 = x;
155#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100156 memcpy(p, &x, sizeof(x));
Dave Rodgmanc5812642024-01-19 14:04:28 +0000157#endif
Dave Rodgman96d61d12022-11-24 19:33:22 +0000158}
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000159
Dave Rodgmana360e192022-11-28 14:44:05 +0000160/**
161 * Read the unsigned 64 bits integer from the given address, which need not
162 * be aligned.
163 *
164 * \param p pointer to 8 bytes of data
165 * \return Data at the given address
166 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100167inline uint64_t mbedtls_get_unaligned_uint64(const void *p)
Dave Rodgmana360e192022-11-28 14:44:05 +0000168{
169 uint64_t r;
Dave Rodgmanc5812642024-01-19 14:04:28 +0000170#if defined(UINT_UNALIGNED)
171 mbedtls_uint64_unaligned_t *p64 = (mbedtls_uint64_unaligned_t *) p;
172 r = *p64;
173#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100174 memcpy(&r, p, sizeof(r));
Dave Rodgmanc5812642024-01-19 14:04:28 +0000175#endif
Dave Rodgmana360e192022-11-28 14:44:05 +0000176 return r;
177}
178
179/**
180 * Write the unsigned 64 bits integer to the given address, which need not
181 * be aligned.
182 *
183 * \param p pointer to 8 bytes of data
184 * \param x data to write
185 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100186inline void mbedtls_put_unaligned_uint64(void *p, uint64_t x)
Dave Rodgmana360e192022-11-28 14:44:05 +0000187{
Dave Rodgmanc5812642024-01-19 14:04:28 +0000188#if defined(UINT_UNALIGNED)
189 mbedtls_uint64_unaligned_t *p64 = (mbedtls_uint64_unaligned_t *) p;
190 *p64 = x;
191#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100192 memcpy(p, &x, sizeof(x));
Dave Rodgmanc5812642024-01-19 14:04:28 +0000193#endif
Dave Rodgmana360e192022-11-28 14:44:05 +0000194}
195
Dave Rodgmanc5812642024-01-19 14:04:28 +0000196#if defined(MBEDTLS_POP_IAR_LANGUAGE_PRAGMA)
197#pragma language=restore
198#endif
199
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000200/** Byte Reading Macros
201 *
202 * Given a multi-byte integer \p x, MBEDTLS_BYTE_n retrieves the n-th
203 * byte from x, where byte 0 is the least significant byte.
204 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100205#define MBEDTLS_BYTE_0(x) ((uint8_t) ((x) & 0xff))
Dave Rodgman914c6322023-03-01 09:30:14 +0000206#define MBEDTLS_BYTE_1(x) ((uint8_t) (((x) >> 8) & 0xff))
Gilles Peskine449bd832023-01-11 14:50:10 +0100207#define MBEDTLS_BYTE_2(x) ((uint8_t) (((x) >> 16) & 0xff))
208#define MBEDTLS_BYTE_3(x) ((uint8_t) (((x) >> 24) & 0xff))
209#define MBEDTLS_BYTE_4(x) ((uint8_t) (((x) >> 32) & 0xff))
210#define MBEDTLS_BYTE_5(x) ((uint8_t) (((x) >> 40) & 0xff))
211#define MBEDTLS_BYTE_6(x) ((uint8_t) (((x) >> 48) & 0xff))
212#define MBEDTLS_BYTE_7(x) ((uint8_t) (((x) >> 56) & 0xff))
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000213
Dave Rodgmanf7f1f742022-11-28 14:52:45 +0000214/*
215 * Detect GCC built-in byteswap routines
216 */
217#if defined(__GNUC__) && defined(__GNUC_PREREQ)
Gilles Peskine449bd832023-01-11 14:50:10 +0100218#if __GNUC_PREREQ(4, 8)
Dave Rodgmanf7f1f742022-11-28 14:52:45 +0000219#define MBEDTLS_BSWAP16 __builtin_bswap16
220#endif /* __GNUC_PREREQ(4,8) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100221#if __GNUC_PREREQ(4, 3)
Dave Rodgmanf7f1f742022-11-28 14:52:45 +0000222#define MBEDTLS_BSWAP32 __builtin_bswap32
223#define MBEDTLS_BSWAP64 __builtin_bswap64
224#endif /* __GNUC_PREREQ(4,3) */
225#endif /* defined(__GNUC__) && defined(__GNUC_PREREQ) */
226
227/*
228 * Detect Clang built-in byteswap routines
229 */
230#if defined(__clang__) && defined(__has_builtin)
Dave Rodgmane47899d2023-02-28 17:39:03 +0000231#if __has_builtin(__builtin_bswap16) && !defined(MBEDTLS_BSWAP16)
Dave Rodgmanf7f1f742022-11-28 14:52:45 +0000232#define MBEDTLS_BSWAP16 __builtin_bswap16
233#endif /* __has_builtin(__builtin_bswap16) */
Dave Rodgmane47899d2023-02-28 17:39:03 +0000234#if __has_builtin(__builtin_bswap32) && !defined(MBEDTLS_BSWAP32)
Dave Rodgmanf7f1f742022-11-28 14:52:45 +0000235#define MBEDTLS_BSWAP32 __builtin_bswap32
236#endif /* __has_builtin(__builtin_bswap32) */
Dave Rodgmane47899d2023-02-28 17:39:03 +0000237#if __has_builtin(__builtin_bswap64) && !defined(MBEDTLS_BSWAP64)
Dave Rodgmanf7f1f742022-11-28 14:52:45 +0000238#define MBEDTLS_BSWAP64 __builtin_bswap64
239#endif /* __has_builtin(__builtin_bswap64) */
240#endif /* defined(__clang__) && defined(__has_builtin) */
241
242/*
243 * Detect MSVC built-in byteswap routines
244 */
245#if defined(_MSC_VER)
Dave Rodgmane47899d2023-02-28 17:39:03 +0000246#if !defined(MBEDTLS_BSWAP16)
Dave Rodgmanf7f1f742022-11-28 14:52:45 +0000247#define MBEDTLS_BSWAP16 _byteswap_ushort
Dave Rodgmane47899d2023-02-28 17:39:03 +0000248#endif
249#if !defined(MBEDTLS_BSWAP32)
Dave Rodgmanf7f1f742022-11-28 14:52:45 +0000250#define MBEDTLS_BSWAP32 _byteswap_ulong
Dave Rodgmane47899d2023-02-28 17:39:03 +0000251#endif
252#if !defined(MBEDTLS_BSWAP64)
Dave Rodgmanf7f1f742022-11-28 14:52:45 +0000253#define MBEDTLS_BSWAP64 _byteswap_uint64
Dave Rodgmane47899d2023-02-28 17:39:03 +0000254#endif
Dave Rodgmanf7f1f742022-11-28 14:52:45 +0000255#endif /* defined(_MSC_VER) */
256
Dave Rodgman2dae4b32022-11-30 12:07:36 +0000257/* Detect armcc built-in byteswap routine */
Dave Rodgmane47899d2023-02-28 17:39:03 +0000258#if defined(__ARMCC_VERSION) && (__ARMCC_VERSION >= 410000) && !defined(MBEDTLS_BSWAP32)
Tom Cosgrovef2b5a132023-04-26 17:00:12 +0100259#if defined(__ARM_ACLE) /* ARM Compiler 6 - earlier versions don't need a header */
260#include <arm_acle.h>
261#endif
Dave Rodgman2dae4b32022-11-30 12:07:36 +0000262#define MBEDTLS_BSWAP32 __rev
263#endif
264
Dave Rodgman650674b2023-12-05 12:16:48 +0000265/* Detect IAR built-in byteswap routine */
266#if defined(__IAR_SYSTEMS_ICC__)
267#if defined(__ARM_ACLE)
268#include <arm_acle.h>
269#define MBEDTLS_BSWAP16(x) ((uint16_t) __rev16((uint32_t) (x)))
270#define MBEDTLS_BSWAP32 __rev
271#define MBEDTLS_BSWAP64 __revll
272#endif
273#endif
274
Dave Rodgmanf7f1f742022-11-28 14:52:45 +0000275/*
276 * Where compiler built-ins are not present, fall back to C code that the
277 * compiler may be able to detect and transform into the relevant bswap or
278 * similar instruction.
279 */
280#if !defined(MBEDTLS_BSWAP16)
Gilles Peskine449bd832023-01-11 14:50:10 +0100281static inline uint16_t mbedtls_bswap16(uint16_t x)
282{
Dave Rodgman6298b242022-11-28 14:51:49 +0000283 return
Gilles Peskine449bd832023-01-11 14:50:10 +0100284 (x & 0x00ff) << 8 |
285 (x & 0xff00) >> 8;
Dave Rodgman6298b242022-11-28 14:51:49 +0000286}
287#define MBEDTLS_BSWAP16 mbedtls_bswap16
Dave Rodgmanf7f1f742022-11-28 14:52:45 +0000288#endif /* !defined(MBEDTLS_BSWAP16) */
Dave Rodgman6298b242022-11-28 14:51:49 +0000289
Dave Rodgmanf7f1f742022-11-28 14:52:45 +0000290#if !defined(MBEDTLS_BSWAP32)
Gilles Peskine449bd832023-01-11 14:50:10 +0100291static inline uint32_t mbedtls_bswap32(uint32_t x)
292{
Dave Rodgman6298b242022-11-28 14:51:49 +0000293 return
Gilles Peskine449bd832023-01-11 14:50:10 +0100294 (x & 0x000000ff) << 24 |
295 (x & 0x0000ff00) << 8 |
296 (x & 0x00ff0000) >> 8 |
297 (x & 0xff000000) >> 24;
Dave Rodgman6298b242022-11-28 14:51:49 +0000298}
299#define MBEDTLS_BSWAP32 mbedtls_bswap32
Dave Rodgmanf7f1f742022-11-28 14:52:45 +0000300#endif /* !defined(MBEDTLS_BSWAP32) */
Dave Rodgman6298b242022-11-28 14:51:49 +0000301
Dave Rodgmanf7f1f742022-11-28 14:52:45 +0000302#if !defined(MBEDTLS_BSWAP64)
Gilles Peskine449bd832023-01-11 14:50:10 +0100303static inline uint64_t mbedtls_bswap64(uint64_t x)
304{
Dave Rodgman6298b242022-11-28 14:51:49 +0000305 return
Tom Cosgrovebbe166e2023-03-08 13:23:24 +0000306 (x & 0x00000000000000ffULL) << 56 |
307 (x & 0x000000000000ff00ULL) << 40 |
308 (x & 0x0000000000ff0000ULL) << 24 |
309 (x & 0x00000000ff000000ULL) << 8 |
310 (x & 0x000000ff00000000ULL) >> 8 |
311 (x & 0x0000ff0000000000ULL) >> 24 |
312 (x & 0x00ff000000000000ULL) >> 40 |
313 (x & 0xff00000000000000ULL) >> 56;
Dave Rodgman6298b242022-11-28 14:51:49 +0000314}
315#define MBEDTLS_BSWAP64 mbedtls_bswap64
Dave Rodgmanf7f1f742022-11-28 14:52:45 +0000316#endif /* !defined(MBEDTLS_BSWAP64) */
Dave Rodgman6298b242022-11-28 14:51:49 +0000317
Dave Rodgmane5c42592022-11-28 14:47:46 +0000318#if !defined(__BYTE_ORDER__)
Dave Rodgmanf3c04f32023-12-05 12:06:11 +0000319
320#if defined(__LITTLE_ENDIAN__)
321/* IAR defines __xxx_ENDIAN__, but not __BYTE_ORDER__ */
322#define MBEDTLS_IS_BIG_ENDIAN 0
323#elif defined(__BIG_ENDIAN__)
324#define MBEDTLS_IS_BIG_ENDIAN 1
325#else
Dave Rodgmane5c42592022-11-28 14:47:46 +0000326static const uint16_t mbedtls_byte_order_detector = { 0x100 };
327#define MBEDTLS_IS_BIG_ENDIAN (*((unsigned char *) (&mbedtls_byte_order_detector)) == 0x01)
Dave Rodgmanf3c04f32023-12-05 12:06:11 +0000328#endif
329
Dave Rodgmane5c42592022-11-28 14:47:46 +0000330#else
Dave Rodgmanf3c04f32023-12-05 12:06:11 +0000331
332#if (__BYTE_ORDER__) == (__ORDER_BIG_ENDIAN__)
333#define MBEDTLS_IS_BIG_ENDIAN 1
334#else
335#define MBEDTLS_IS_BIG_ENDIAN 0
336#endif
337
Dave Rodgmane5c42592022-11-28 14:47:46 +0000338#endif /* !defined(__BYTE_ORDER__) */
339
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000340/**
341 * Get the unsigned 32 bits integer corresponding to four bytes in
342 * big-endian order (MSB first).
343 *
344 * \param data Base address of the memory to get the four bytes from.
345 * \param offset Offset from \p data of the first and most significant
346 * byte of the four bytes to build the 32 bits unsigned
347 * integer from.
348 */
Dave Rodgman914c6322023-03-01 09:30:14 +0000349#define MBEDTLS_GET_UINT32_BE(data, offset) \
350 ((MBEDTLS_IS_BIG_ENDIAN) \
Dave Rodgmana5110b02022-11-28 14:48:45 +0000351 ? mbedtls_get_unaligned_uint32((data) + (offset)) \
352 : MBEDTLS_BSWAP32(mbedtls_get_unaligned_uint32((data) + (offset))) \
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000353 )
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000354
355/**
356 * Put in memory a 32 bits unsigned integer in big-endian order.
357 *
358 * \param n 32 bits unsigned integer to put in memory.
359 * \param data Base address of the memory where to put the 32
360 * bits unsigned integer in.
361 * \param offset Offset from \p data where to put the most significant
362 * byte of the 32 bits unsigned integer \p n.
363 */
Dave Rodgman914c6322023-03-01 09:30:14 +0000364#define MBEDTLS_PUT_UINT32_BE(n, data, offset) \
Gilles Peskine449bd832023-01-11 14:50:10 +0100365 { \
Dave Rodgman914c6322023-03-01 09:30:14 +0000366 if (MBEDTLS_IS_BIG_ENDIAN) \
Gilles Peskine449bd832023-01-11 14:50:10 +0100367 { \
Dave Rodgman914c6322023-03-01 09:30:14 +0000368 mbedtls_put_unaligned_uint32((data) + (offset), (uint32_t) (n)); \
Gilles Peskine449bd832023-01-11 14:50:10 +0100369 } \
370 else \
371 { \
372 mbedtls_put_unaligned_uint32((data) + (offset), MBEDTLS_BSWAP32((uint32_t) (n))); \
373 } \
374 }
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000375
376/**
377 * Get the unsigned 32 bits integer corresponding to four bytes in
378 * little-endian order (LSB first).
379 *
380 * \param data Base address of the memory to get the four bytes from.
381 * \param offset Offset from \p data of the first and least significant
382 * byte of the four bytes to build the 32 bits unsigned
383 * integer from.
384 */
Dave Rodgman914c6322023-03-01 09:30:14 +0000385#define MBEDTLS_GET_UINT32_LE(data, offset) \
386 ((MBEDTLS_IS_BIG_ENDIAN) \
Dave Rodgmana5110b02022-11-28 14:48:45 +0000387 ? MBEDTLS_BSWAP32(mbedtls_get_unaligned_uint32((data) + (offset))) \
388 : mbedtls_get_unaligned_uint32((data) + (offset)) \
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000389 )
Dave Rodgmana5110b02022-11-28 14:48:45 +0000390
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000391
392/**
393 * Put in memory a 32 bits unsigned integer in little-endian order.
394 *
395 * \param n 32 bits unsigned integer to put in memory.
396 * \param data Base address of the memory where to put the 32
397 * bits unsigned integer in.
398 * \param offset Offset from \p data where to put the least significant
399 * byte of the 32 bits unsigned integer \p n.
400 */
Dave Rodgman914c6322023-03-01 09:30:14 +0000401#define MBEDTLS_PUT_UINT32_LE(n, data, offset) \
Gilles Peskine449bd832023-01-11 14:50:10 +0100402 { \
Dave Rodgman914c6322023-03-01 09:30:14 +0000403 if (MBEDTLS_IS_BIG_ENDIAN) \
Gilles Peskine449bd832023-01-11 14:50:10 +0100404 { \
405 mbedtls_put_unaligned_uint32((data) + (offset), MBEDTLS_BSWAP32((uint32_t) (n))); \
406 } \
407 else \
408 { \
Dave Rodgman914c6322023-03-01 09:30:14 +0000409 mbedtls_put_unaligned_uint32((data) + (offset), ((uint32_t) (n))); \
Gilles Peskine449bd832023-01-11 14:50:10 +0100410 } \
411 }
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000412
413/**
414 * Get the unsigned 16 bits integer corresponding to two bytes in
415 * little-endian order (LSB first).
416 *
417 * \param data Base address of the memory to get the two bytes from.
418 * \param offset Offset from \p data of the first and least significant
419 * byte of the two bytes to build the 16 bits unsigned
420 * integer from.
421 */
Dave Rodgman914c6322023-03-01 09:30:14 +0000422#define MBEDTLS_GET_UINT16_LE(data, offset) \
423 ((MBEDTLS_IS_BIG_ENDIAN) \
Dave Rodgmana5110b02022-11-28 14:48:45 +0000424 ? MBEDTLS_BSWAP16(mbedtls_get_unaligned_uint16((data) + (offset))) \
425 : mbedtls_get_unaligned_uint16((data) + (offset)) \
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000426 )
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000427
428/**
429 * Put in memory a 16 bits unsigned integer in little-endian order.
430 *
431 * \param n 16 bits unsigned integer to put in memory.
432 * \param data Base address of the memory where to put the 16
433 * bits unsigned integer in.
434 * \param offset Offset from \p data where to put the least significant
435 * byte of the 16 bits unsigned integer \p n.
436 */
Dave Rodgman914c6322023-03-01 09:30:14 +0000437#define MBEDTLS_PUT_UINT16_LE(n, data, offset) \
Gilles Peskine449bd832023-01-11 14:50:10 +0100438 { \
Dave Rodgman914c6322023-03-01 09:30:14 +0000439 if (MBEDTLS_IS_BIG_ENDIAN) \
Gilles Peskine449bd832023-01-11 14:50:10 +0100440 { \
441 mbedtls_put_unaligned_uint16((data) + (offset), MBEDTLS_BSWAP16((uint16_t) (n))); \
442 } \
443 else \
444 { \
Dave Rodgman914c6322023-03-01 09:30:14 +0000445 mbedtls_put_unaligned_uint16((data) + (offset), (uint16_t) (n)); \
Gilles Peskine449bd832023-01-11 14:50:10 +0100446 } \
447 }
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000448
449/**
450 * Get the unsigned 16 bits integer corresponding to two bytes in
451 * big-endian order (MSB first).
452 *
453 * \param data Base address of the memory to get the two bytes from.
454 * \param offset Offset from \p data of the first and most significant
455 * byte of the two bytes to build the 16 bits unsigned
456 * integer from.
457 */
Dave Rodgman914c6322023-03-01 09:30:14 +0000458#define MBEDTLS_GET_UINT16_BE(data, offset) \
459 ((MBEDTLS_IS_BIG_ENDIAN) \
Dave Rodgmana5110b02022-11-28 14:48:45 +0000460 ? mbedtls_get_unaligned_uint16((data) + (offset)) \
461 : MBEDTLS_BSWAP16(mbedtls_get_unaligned_uint16((data) + (offset))) \
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000462 )
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000463
464/**
465 * Put in memory a 16 bits unsigned integer in big-endian order.
466 *
467 * \param n 16 bits unsigned integer to put in memory.
468 * \param data Base address of the memory where to put the 16
469 * bits unsigned integer in.
470 * \param offset Offset from \p data where to put the most significant
471 * byte of the 16 bits unsigned integer \p n.
472 */
Dave Rodgman914c6322023-03-01 09:30:14 +0000473#define MBEDTLS_PUT_UINT16_BE(n, data, offset) \
Gilles Peskine449bd832023-01-11 14:50:10 +0100474 { \
Dave Rodgman914c6322023-03-01 09:30:14 +0000475 if (MBEDTLS_IS_BIG_ENDIAN) \
Gilles Peskine449bd832023-01-11 14:50:10 +0100476 { \
Dave Rodgman914c6322023-03-01 09:30:14 +0000477 mbedtls_put_unaligned_uint16((data) + (offset), (uint16_t) (n)); \
Gilles Peskine449bd832023-01-11 14:50:10 +0100478 } \
479 else \
480 { \
481 mbedtls_put_unaligned_uint16((data) + (offset), MBEDTLS_BSWAP16((uint16_t) (n))); \
482 } \
483 }
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000484
485/**
486 * Get the unsigned 24 bits integer corresponding to three bytes in
487 * big-endian order (MSB first).
488 *
489 * \param data Base address of the memory to get the three bytes from.
490 * \param offset Offset from \p data of the first and most significant
491 * byte of the three bytes to build the 24 bits unsigned
492 * integer from.
493 */
Dave Rodgman914c6322023-03-01 09:30:14 +0000494#define MBEDTLS_GET_UINT24_BE(data, offset) \
495 ( \
496 ((uint32_t) (data)[(offset)] << 16) \
497 | ((uint32_t) (data)[(offset) + 1] << 8) \
498 | ((uint32_t) (data)[(offset) + 2]) \
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000499 )
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000500
501/**
502 * Put in memory a 24 bits unsigned integer in big-endian order.
503 *
504 * \param n 24 bits unsigned integer to put in memory.
505 * \param data Base address of the memory where to put the 24
506 * bits unsigned integer in.
507 * \param offset Offset from \p data where to put the most significant
508 * byte of the 24 bits unsigned integer \p n.
509 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100510#define MBEDTLS_PUT_UINT24_BE(n, data, offset) \
Dave Rodgman914c6322023-03-01 09:30:14 +0000511 { \
512 (data)[(offset)] = MBEDTLS_BYTE_2(n); \
Gilles Peskine449bd832023-01-11 14:50:10 +0100513 (data)[(offset) + 1] = MBEDTLS_BYTE_1(n); \
514 (data)[(offset) + 2] = MBEDTLS_BYTE_0(n); \
515 }
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000516
517/**
518 * Get the unsigned 24 bits integer corresponding to three bytes in
519 * little-endian order (LSB first).
520 *
521 * \param data Base address of the memory to get the three bytes from.
522 * \param offset Offset from \p data of the first and least significant
523 * byte of the three bytes to build the 24 bits unsigned
524 * integer from.
525 */
Dave Rodgman914c6322023-03-01 09:30:14 +0000526#define MBEDTLS_GET_UINT24_LE(data, offset) \
527 ( \
528 ((uint32_t) (data)[(offset)]) \
Gilles Peskine449bd832023-01-11 14:50:10 +0100529 | ((uint32_t) (data)[(offset) + 1] << 8) \
530 | ((uint32_t) (data)[(offset) + 2] << 16) \
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000531 )
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000532
533/**
534 * Put in memory a 24 bits unsigned integer in little-endian order.
535 *
536 * \param n 24 bits unsigned integer to put in memory.
537 * \param data Base address of the memory where to put the 24
538 * bits unsigned integer in.
539 * \param offset Offset from \p data where to put the least significant
540 * byte of the 24 bits unsigned integer \p n.
541 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100542#define MBEDTLS_PUT_UINT24_LE(n, data, offset) \
Dave Rodgman914c6322023-03-01 09:30:14 +0000543 { \
544 (data)[(offset)] = MBEDTLS_BYTE_0(n); \
Gilles Peskine449bd832023-01-11 14:50:10 +0100545 (data)[(offset) + 1] = MBEDTLS_BYTE_1(n); \
546 (data)[(offset) + 2] = MBEDTLS_BYTE_2(n); \
547 }
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000548
549/**
550 * Get the unsigned 64 bits integer corresponding to eight bytes in
551 * big-endian order (MSB first).
552 *
553 * \param data Base address of the memory to get the eight bytes from.
554 * \param offset Offset from \p data of the first and most significant
555 * byte of the eight bytes to build the 64 bits unsigned
556 * integer from.
557 */
Dave Rodgman914c6322023-03-01 09:30:14 +0000558#define MBEDTLS_GET_UINT64_BE(data, offset) \
559 ((MBEDTLS_IS_BIG_ENDIAN) \
Dave Rodgmana5110b02022-11-28 14:48:45 +0000560 ? mbedtls_get_unaligned_uint64((data) + (offset)) \
561 : MBEDTLS_BSWAP64(mbedtls_get_unaligned_uint64((data) + (offset))) \
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000562 )
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000563
564/**
565 * Put in memory a 64 bits unsigned integer in big-endian order.
566 *
567 * \param n 64 bits unsigned integer to put in memory.
568 * \param data Base address of the memory where to put the 64
569 * bits unsigned integer in.
570 * \param offset Offset from \p data where to put the most significant
571 * byte of the 64 bits unsigned integer \p n.
572 */
Dave Rodgman914c6322023-03-01 09:30:14 +0000573#define MBEDTLS_PUT_UINT64_BE(n, data, offset) \
Gilles Peskine449bd832023-01-11 14:50:10 +0100574 { \
Dave Rodgman914c6322023-03-01 09:30:14 +0000575 if (MBEDTLS_IS_BIG_ENDIAN) \
Gilles Peskine449bd832023-01-11 14:50:10 +0100576 { \
Dave Rodgman914c6322023-03-01 09:30:14 +0000577 mbedtls_put_unaligned_uint64((data) + (offset), (uint64_t) (n)); \
Gilles Peskine449bd832023-01-11 14:50:10 +0100578 } \
579 else \
580 { \
581 mbedtls_put_unaligned_uint64((data) + (offset), MBEDTLS_BSWAP64((uint64_t) (n))); \
582 } \
583 }
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000584
585/**
586 * Get the unsigned 64 bits integer corresponding to eight bytes in
587 * little-endian order (LSB first).
588 *
589 * \param data Base address of the memory to get the eight bytes from.
590 * \param offset Offset from \p data of the first and least significant
591 * byte of the eight bytes to build the 64 bits unsigned
592 * integer from.
593 */
Dave Rodgman914c6322023-03-01 09:30:14 +0000594#define MBEDTLS_GET_UINT64_LE(data, offset) \
595 ((MBEDTLS_IS_BIG_ENDIAN) \
Dave Rodgmana5110b02022-11-28 14:48:45 +0000596 ? MBEDTLS_BSWAP64(mbedtls_get_unaligned_uint64((data) + (offset))) \
597 : mbedtls_get_unaligned_uint64((data) + (offset)) \
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000598 )
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000599
600/**
601 * Put in memory a 64 bits unsigned integer in little-endian order.
602 *
603 * \param n 64 bits unsigned integer to put in memory.
604 * \param data Base address of the memory where to put the 64
605 * bits unsigned integer in.
606 * \param offset Offset from \p data where to put the least significant
607 * byte of the 64 bits unsigned integer \p n.
608 */
Dave Rodgman914c6322023-03-01 09:30:14 +0000609#define MBEDTLS_PUT_UINT64_LE(n, data, offset) \
Gilles Peskine449bd832023-01-11 14:50:10 +0100610 { \
Dave Rodgman914c6322023-03-01 09:30:14 +0000611 if (MBEDTLS_IS_BIG_ENDIAN) \
Gilles Peskine449bd832023-01-11 14:50:10 +0100612 { \
613 mbedtls_put_unaligned_uint64((data) + (offset), MBEDTLS_BSWAP64((uint64_t) (n))); \
614 } \
615 else \
616 { \
Dave Rodgman914c6322023-03-01 09:30:14 +0000617 mbedtls_put_unaligned_uint64((data) + (offset), (uint64_t) (n)); \
Gilles Peskine449bd832023-01-11 14:50:10 +0100618 } \
619 }
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000620
621#endif /* MBEDTLS_LIBRARY_ALIGNMENT_H */