blob: 14a86cf377e32fe4b2e8c1ceb1c3d6a49cee74db [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 Rodgmanc5812642024-01-19 14:04:28 +000040#if defined(__IAR_SYSTEMS_ICC__) && \
41 (defined(MBEDTLS_ARCH_IS_ARM64) || defined(MBEDTLS_ARCH_IS_ARM32) \
42 || defined(__ICCRX__) || defined(__ICCRL78__) || defined(__ICCRISCV__))
43#pragma language=save
44#pragma language=extended
45#define MBEDTLS_POP_IAR_LANGUAGE_PRAGMA
46/* IAR recommend this technique for accessing unaligned data in
47 * https://www.iar.com/knowledge/support/technical-notes/compiler/accessing-unaligned-data
48 * This results in a single load / store instruction (if unaligned access is supported).
49 * According to that document, this is only supported on certain architectures.
50 */
51 #define UINT_UNALIGNED
52typedef uint16_t __packed mbedtls_uint16_unaligned_t;
53typedef uint32_t __packed mbedtls_uint32_unaligned_t;
54typedef uint64_t __packed mbedtls_uint64_unaligned_t;
55#elif defined(MBEDTLS_COMPILER_IS_GCC) && (MBEDTLS_GCC_VERSION >= 40504) && \
Dave Rodgmanec9936d2024-02-06 12:56:45 +000056 ((MBEDTLS_GCC_VERSION < 60300) || (!defined(MBEDTLS_EFFICIENT_UNALIGNED_ACCESS)))
Dave Rodgmanc5812642024-01-19 14:04:28 +000057/*
Dave Rodgmanf4e82342024-02-06 12:57:03 +000058 * gcc may generate a branch to memcpy for calls like `memcpy(dest, src, 4)` rather than
59 * generating some LDR or LDRB instructions (similar for stores).
60 *
61 * For versions of gcc < 5.4.0 this always happens.
62 * For gcc < 6.3.0, this happens at -O0
63 * For all versions, this happens iff unaligned access is not supported.
64 *
65 * For gcc 4.x, this will generate byte-by-byte loads even if unaligned access is supported, which
66 * is correct but not optimal.
Dave Rodgmanc5812642024-01-19 14:04:28 +000067 *
68 * For performance (and code size, in some cases), we want to avoid the branch and just generate
69 * some inline load/store instructions since the access is small and constant-size.
70 *
71 * The manual states:
Dave Rodgmanf4e82342024-02-06 12:57:03 +000072 * "The packed attribute specifies that a variable or structure field should have the smallest
73 * possible alignment—one byte for a variable"
74 * https://gcc.gnu.org/onlinedocs/gcc-4.5.4/gcc/Variable-Attributes.html
Dave Rodgmanc5812642024-01-19 14:04:28 +000075 *
Dave Rodgmanf4e82342024-02-06 12:57:03 +000076 * Tested with several versions of GCC from 4.5.0 up to 13.2.0
Dave Rodgmanc5812642024-01-19 14:04:28 +000077 * We don't enable for older than 4.5.0 as this has not been tested.
78 */
Dave Rodgmanb327a1e2024-02-06 11:21:26 +000079 #define UINT_UNALIGNED_UNION
80typedef union { uint16_t x; } __attribute__((packed)) mbedtls_uint16_unaligned_t;
81typedef union { uint32_t x; } __attribute__((packed)) mbedtls_uint32_unaligned_t;
82typedef union { uint64_t x; } __attribute__((packed)) mbedtls_uint64_unaligned_t;
Dave Rodgmanc5812642024-01-19 14:04:28 +000083 #endif
84
Dave Rodgman55b5dd22024-01-19 14:06:52 +000085/*
86 * We try to force mbedtls_(get|put)_unaligned_uintXX to be always inline, because this results
87 * in code that is both smaller and faster. IAR and gcc both benefit from this when optimising
88 * for size.
89 */
90
Dave Rodgman96d61d12022-11-24 19:33:22 +000091/**
Dave Rodgmana360e192022-11-28 14:44:05 +000092 * Read the unsigned 16 bits integer from the given address, which need not
93 * be aligned.
94 *
95 * \param p pointer to 2 bytes of data
96 * \return Data at the given address
97 */
Dave Rodgman55b5dd22024-01-19 14:06:52 +000098#if defined(__IAR_SYSTEMS_ICC__)
99#pragma inline = forced
100#elif defined(__GNUC__)
101__attribute__((always_inline))
102#endif
103static inline uint16_t mbedtls_get_unaligned_uint16(const void *p)
Dave Rodgmana360e192022-11-28 14:44:05 +0000104{
105 uint16_t r;
Dave Rodgmanc5812642024-01-19 14:04:28 +0000106#if defined(UINT_UNALIGNED)
107 mbedtls_uint16_unaligned_t *p16 = (mbedtls_uint16_unaligned_t *) p;
108 r = *p16;
Dave Rodgmanb327a1e2024-02-06 11:21:26 +0000109#elif defined(UINT_UNALIGNED_UNION)
110 mbedtls_uint16_unaligned_t *p16 = (mbedtls_uint16_unaligned_t *) p;
111 r = p16->x;
Dave Rodgmanc5812642024-01-19 14:04:28 +0000112#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100113 memcpy(&r, p, sizeof(r));
Dave Rodgmanc5812642024-01-19 14:04:28 +0000114#endif
Dave Rodgmana360e192022-11-28 14:44:05 +0000115 return r;
116}
117
118/**
119 * Write the unsigned 16 bits integer to the given address, which need not
120 * be aligned.
121 *
122 * \param p pointer to 2 bytes of data
123 * \param x data to write
124 */
Dave Rodgman55b5dd22024-01-19 14:06:52 +0000125#if defined(__IAR_SYSTEMS_ICC__)
126#pragma inline = forced
127#elif defined(__GNUC__)
128__attribute__((always_inline))
129#endif
130static inline void mbedtls_put_unaligned_uint16(void *p, uint16_t x)
Dave Rodgmana360e192022-11-28 14:44:05 +0000131{
Dave Rodgmanc5812642024-01-19 14:04:28 +0000132#if defined(UINT_UNALIGNED)
133 mbedtls_uint16_unaligned_t *p16 = (mbedtls_uint16_unaligned_t *) p;
134 *p16 = x;
Dave Rodgmanb327a1e2024-02-06 11:21:26 +0000135#elif defined(UINT_UNALIGNED_UNION)
136 mbedtls_uint16_unaligned_t *p16 = (mbedtls_uint16_unaligned_t *) p;
137 p16->x = x;
Dave Rodgmanc5812642024-01-19 14:04:28 +0000138#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100139 memcpy(p, &x, sizeof(x));
Dave Rodgmanc5812642024-01-19 14:04:28 +0000140#endif
Dave Rodgmana360e192022-11-28 14:44:05 +0000141}
142
143/**
Dave Rodgman96d61d12022-11-24 19:33:22 +0000144 * Read the unsigned 32 bits integer from the given address, which need not
145 * be aligned.
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000146 *
Dave Rodgman96d61d12022-11-24 19:33:22 +0000147 * \param p pointer to 4 bytes of data
Dave Rodgman875d2382022-11-24 20:43:15 +0000148 * \return Data at the given address
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000149 */
Dave Rodgman55b5dd22024-01-19 14:06:52 +0000150#if defined(__IAR_SYSTEMS_ICC__)
151#pragma inline = forced
152#elif defined(__GNUC__)
153__attribute__((always_inline))
154#endif
155static inline uint32_t mbedtls_get_unaligned_uint32(const void *p)
Dave Rodgman96d61d12022-11-24 19:33:22 +0000156{
157 uint32_t r;
Dave Rodgmanc5812642024-01-19 14:04:28 +0000158#if defined(UINT_UNALIGNED)
159 mbedtls_uint32_unaligned_t *p32 = (mbedtls_uint32_unaligned_t *) p;
160 r = *p32;
Dave Rodgmanb327a1e2024-02-06 11:21:26 +0000161#elif defined(UINT_UNALIGNED_UNION)
162 mbedtls_uint32_unaligned_t *p32 = (mbedtls_uint32_unaligned_t *) p;
163 r = p32->x;
Dave Rodgmanc5812642024-01-19 14:04:28 +0000164#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100165 memcpy(&r, p, sizeof(r));
Dave Rodgmanc5812642024-01-19 14:04:28 +0000166#endif
Dave Rodgman96d61d12022-11-24 19:33:22 +0000167 return r;
168}
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000169
Dave Rodgman96d61d12022-11-24 19:33:22 +0000170/**
171 * Write the unsigned 32 bits integer to the given address, which need not
172 * be aligned.
173 *
174 * \param p pointer to 4 bytes of data
175 * \param x data to write
176 */
Dave Rodgman55b5dd22024-01-19 14:06:52 +0000177#if defined(__IAR_SYSTEMS_ICC__)
178#pragma inline = forced
179#elif defined(__GNUC__)
180__attribute__((always_inline))
181#endif
182static inline void mbedtls_put_unaligned_uint32(void *p, uint32_t x)
Dave Rodgman96d61d12022-11-24 19:33:22 +0000183{
Dave Rodgmanc5812642024-01-19 14:04:28 +0000184#if defined(UINT_UNALIGNED)
185 mbedtls_uint32_unaligned_t *p32 = (mbedtls_uint32_unaligned_t *) p;
186 *p32 = x;
Dave Rodgmanb327a1e2024-02-06 11:21:26 +0000187#elif defined(UINT_UNALIGNED_UNION)
188 mbedtls_uint32_unaligned_t *p32 = (mbedtls_uint32_unaligned_t *) p;
189 p32->x = x;
Dave Rodgmanc5812642024-01-19 14:04:28 +0000190#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100191 memcpy(p, &x, sizeof(x));
Dave Rodgmanc5812642024-01-19 14:04:28 +0000192#endif
Dave Rodgman96d61d12022-11-24 19:33:22 +0000193}
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000194
Dave Rodgmana360e192022-11-28 14:44:05 +0000195/**
196 * Read the unsigned 64 bits integer from the given address, which need not
197 * be aligned.
198 *
199 * \param p pointer to 8 bytes of data
200 * \return Data at the given address
201 */
Dave Rodgman55b5dd22024-01-19 14:06:52 +0000202#if defined(__IAR_SYSTEMS_ICC__)
203#pragma inline = forced
204#elif defined(__GNUC__)
205__attribute__((always_inline))
206#endif
207static inline uint64_t mbedtls_get_unaligned_uint64(const void *p)
Dave Rodgmana360e192022-11-28 14:44:05 +0000208{
209 uint64_t r;
Dave Rodgmanc5812642024-01-19 14:04:28 +0000210#if defined(UINT_UNALIGNED)
211 mbedtls_uint64_unaligned_t *p64 = (mbedtls_uint64_unaligned_t *) p;
212 r = *p64;
Dave Rodgmanb327a1e2024-02-06 11:21:26 +0000213#elif defined(UINT_UNALIGNED_UNION)
214 mbedtls_uint64_unaligned_t *p64 = (mbedtls_uint64_unaligned_t *) p;
215 r = p64->x;
Dave Rodgmanc5812642024-01-19 14:04:28 +0000216#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100217 memcpy(&r, p, sizeof(r));
Dave Rodgmanc5812642024-01-19 14:04:28 +0000218#endif
Dave Rodgmana360e192022-11-28 14:44:05 +0000219 return r;
220}
221
222/**
223 * Write the unsigned 64 bits integer to the given address, which need not
224 * be aligned.
225 *
226 * \param p pointer to 8 bytes of data
227 * \param x data to write
228 */
Dave Rodgman55b5dd22024-01-19 14:06:52 +0000229#if defined(__IAR_SYSTEMS_ICC__)
230#pragma inline = forced
231#elif defined(__GNUC__)
232__attribute__((always_inline))
233#endif
234static inline void mbedtls_put_unaligned_uint64(void *p, uint64_t x)
Dave Rodgmana360e192022-11-28 14:44:05 +0000235{
Dave Rodgmanc5812642024-01-19 14:04:28 +0000236#if defined(UINT_UNALIGNED)
237 mbedtls_uint64_unaligned_t *p64 = (mbedtls_uint64_unaligned_t *) p;
238 *p64 = x;
Dave Rodgmanb327a1e2024-02-06 11:21:26 +0000239#elif defined(UINT_UNALIGNED_UNION)
240 mbedtls_uint64_unaligned_t *p64 = (mbedtls_uint64_unaligned_t *) p;
241 p64->x = x;
Dave Rodgmanc5812642024-01-19 14:04:28 +0000242#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100243 memcpy(p, &x, sizeof(x));
Dave Rodgmanc5812642024-01-19 14:04:28 +0000244#endif
Dave Rodgmana360e192022-11-28 14:44:05 +0000245}
246
Dave Rodgmanc5812642024-01-19 14:04:28 +0000247#if defined(MBEDTLS_POP_IAR_LANGUAGE_PRAGMA)
248#pragma language=restore
249#endif
250
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000251/** Byte Reading Macros
252 *
253 * Given a multi-byte integer \p x, MBEDTLS_BYTE_n retrieves the n-th
254 * byte from x, where byte 0 is the least significant byte.
255 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100256#define MBEDTLS_BYTE_0(x) ((uint8_t) ((x) & 0xff))
Dave Rodgman914c6322023-03-01 09:30:14 +0000257#define MBEDTLS_BYTE_1(x) ((uint8_t) (((x) >> 8) & 0xff))
Gilles Peskine449bd832023-01-11 14:50:10 +0100258#define MBEDTLS_BYTE_2(x) ((uint8_t) (((x) >> 16) & 0xff))
259#define MBEDTLS_BYTE_3(x) ((uint8_t) (((x) >> 24) & 0xff))
260#define MBEDTLS_BYTE_4(x) ((uint8_t) (((x) >> 32) & 0xff))
261#define MBEDTLS_BYTE_5(x) ((uint8_t) (((x) >> 40) & 0xff))
262#define MBEDTLS_BYTE_6(x) ((uint8_t) (((x) >> 48) & 0xff))
263#define MBEDTLS_BYTE_7(x) ((uint8_t) (((x) >> 56) & 0xff))
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000264
Dave Rodgmanf7f1f742022-11-28 14:52:45 +0000265/*
266 * Detect GCC built-in byteswap routines
267 */
268#if defined(__GNUC__) && defined(__GNUC_PREREQ)
Gilles Peskine449bd832023-01-11 14:50:10 +0100269#if __GNUC_PREREQ(4, 8)
Dave Rodgmanf7f1f742022-11-28 14:52:45 +0000270#define MBEDTLS_BSWAP16 __builtin_bswap16
271#endif /* __GNUC_PREREQ(4,8) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100272#if __GNUC_PREREQ(4, 3)
Dave Rodgmanf7f1f742022-11-28 14:52:45 +0000273#define MBEDTLS_BSWAP32 __builtin_bswap32
274#define MBEDTLS_BSWAP64 __builtin_bswap64
275#endif /* __GNUC_PREREQ(4,3) */
276#endif /* defined(__GNUC__) && defined(__GNUC_PREREQ) */
277
278/*
279 * Detect Clang built-in byteswap routines
280 */
281#if defined(__clang__) && defined(__has_builtin)
Dave Rodgmane47899d2023-02-28 17:39:03 +0000282#if __has_builtin(__builtin_bswap16) && !defined(MBEDTLS_BSWAP16)
Dave Rodgmanf7f1f742022-11-28 14:52:45 +0000283#define MBEDTLS_BSWAP16 __builtin_bswap16
284#endif /* __has_builtin(__builtin_bswap16) */
Dave Rodgmane47899d2023-02-28 17:39:03 +0000285#if __has_builtin(__builtin_bswap32) && !defined(MBEDTLS_BSWAP32)
Dave Rodgmanf7f1f742022-11-28 14:52:45 +0000286#define MBEDTLS_BSWAP32 __builtin_bswap32
287#endif /* __has_builtin(__builtin_bswap32) */
Dave Rodgmane47899d2023-02-28 17:39:03 +0000288#if __has_builtin(__builtin_bswap64) && !defined(MBEDTLS_BSWAP64)
Dave Rodgmanf7f1f742022-11-28 14:52:45 +0000289#define MBEDTLS_BSWAP64 __builtin_bswap64
290#endif /* __has_builtin(__builtin_bswap64) */
291#endif /* defined(__clang__) && defined(__has_builtin) */
292
293/*
294 * Detect MSVC built-in byteswap routines
295 */
296#if defined(_MSC_VER)
Dave Rodgmane47899d2023-02-28 17:39:03 +0000297#if !defined(MBEDTLS_BSWAP16)
Dave Rodgmanf7f1f742022-11-28 14:52:45 +0000298#define MBEDTLS_BSWAP16 _byteswap_ushort
Dave Rodgmane47899d2023-02-28 17:39:03 +0000299#endif
300#if !defined(MBEDTLS_BSWAP32)
Dave Rodgmanf7f1f742022-11-28 14:52:45 +0000301#define MBEDTLS_BSWAP32 _byteswap_ulong
Dave Rodgmane47899d2023-02-28 17:39:03 +0000302#endif
303#if !defined(MBEDTLS_BSWAP64)
Dave Rodgmanf7f1f742022-11-28 14:52:45 +0000304#define MBEDTLS_BSWAP64 _byteswap_uint64
Dave Rodgmane47899d2023-02-28 17:39:03 +0000305#endif
Dave Rodgmanf7f1f742022-11-28 14:52:45 +0000306#endif /* defined(_MSC_VER) */
307
Dave Rodgman2dae4b32022-11-30 12:07:36 +0000308/* Detect armcc built-in byteswap routine */
Dave Rodgmane47899d2023-02-28 17:39:03 +0000309#if defined(__ARMCC_VERSION) && (__ARMCC_VERSION >= 410000) && !defined(MBEDTLS_BSWAP32)
Tom Cosgrovef2b5a132023-04-26 17:00:12 +0100310#if defined(__ARM_ACLE) /* ARM Compiler 6 - earlier versions don't need a header */
311#include <arm_acle.h>
312#endif
Dave Rodgman2dae4b32022-11-30 12:07:36 +0000313#define MBEDTLS_BSWAP32 __rev
314#endif
315
Dave Rodgman650674b2023-12-05 12:16:48 +0000316/* Detect IAR built-in byteswap routine */
317#if defined(__IAR_SYSTEMS_ICC__)
318#if defined(__ARM_ACLE)
319#include <arm_acle.h>
320#define MBEDTLS_BSWAP16(x) ((uint16_t) __rev16((uint32_t) (x)))
321#define MBEDTLS_BSWAP32 __rev
322#define MBEDTLS_BSWAP64 __revll
323#endif
324#endif
325
Dave Rodgmanf7f1f742022-11-28 14:52:45 +0000326/*
327 * Where compiler built-ins are not present, fall back to C code that the
328 * compiler may be able to detect and transform into the relevant bswap or
329 * similar instruction.
330 */
331#if !defined(MBEDTLS_BSWAP16)
Gilles Peskine449bd832023-01-11 14:50:10 +0100332static inline uint16_t mbedtls_bswap16(uint16_t x)
333{
Dave Rodgman6298b242022-11-28 14:51:49 +0000334 return
Gilles Peskine449bd832023-01-11 14:50:10 +0100335 (x & 0x00ff) << 8 |
336 (x & 0xff00) >> 8;
Dave Rodgman6298b242022-11-28 14:51:49 +0000337}
338#define MBEDTLS_BSWAP16 mbedtls_bswap16
Dave Rodgmanf7f1f742022-11-28 14:52:45 +0000339#endif /* !defined(MBEDTLS_BSWAP16) */
Dave Rodgman6298b242022-11-28 14:51:49 +0000340
Dave Rodgmanf7f1f742022-11-28 14:52:45 +0000341#if !defined(MBEDTLS_BSWAP32)
Gilles Peskine449bd832023-01-11 14:50:10 +0100342static inline uint32_t mbedtls_bswap32(uint32_t x)
343{
Dave Rodgman6298b242022-11-28 14:51:49 +0000344 return
Gilles Peskine449bd832023-01-11 14:50:10 +0100345 (x & 0x000000ff) << 24 |
346 (x & 0x0000ff00) << 8 |
347 (x & 0x00ff0000) >> 8 |
348 (x & 0xff000000) >> 24;
Dave Rodgman6298b242022-11-28 14:51:49 +0000349}
350#define MBEDTLS_BSWAP32 mbedtls_bswap32
Dave Rodgmanf7f1f742022-11-28 14:52:45 +0000351#endif /* !defined(MBEDTLS_BSWAP32) */
Dave Rodgman6298b242022-11-28 14:51:49 +0000352
Dave Rodgmanf7f1f742022-11-28 14:52:45 +0000353#if !defined(MBEDTLS_BSWAP64)
Gilles Peskine449bd832023-01-11 14:50:10 +0100354static inline uint64_t mbedtls_bswap64(uint64_t x)
355{
Dave Rodgman6298b242022-11-28 14:51:49 +0000356 return
Tom Cosgrovebbe166e2023-03-08 13:23:24 +0000357 (x & 0x00000000000000ffULL) << 56 |
358 (x & 0x000000000000ff00ULL) << 40 |
359 (x & 0x0000000000ff0000ULL) << 24 |
360 (x & 0x00000000ff000000ULL) << 8 |
361 (x & 0x000000ff00000000ULL) >> 8 |
362 (x & 0x0000ff0000000000ULL) >> 24 |
363 (x & 0x00ff000000000000ULL) >> 40 |
364 (x & 0xff00000000000000ULL) >> 56;
Dave Rodgman6298b242022-11-28 14:51:49 +0000365}
366#define MBEDTLS_BSWAP64 mbedtls_bswap64
Dave Rodgmanf7f1f742022-11-28 14:52:45 +0000367#endif /* !defined(MBEDTLS_BSWAP64) */
Dave Rodgman6298b242022-11-28 14:51:49 +0000368
Dave Rodgmane5c42592022-11-28 14:47:46 +0000369#if !defined(__BYTE_ORDER__)
Dave Rodgmanf3c04f32023-12-05 12:06:11 +0000370
371#if defined(__LITTLE_ENDIAN__)
372/* IAR defines __xxx_ENDIAN__, but not __BYTE_ORDER__ */
373#define MBEDTLS_IS_BIG_ENDIAN 0
374#elif defined(__BIG_ENDIAN__)
375#define MBEDTLS_IS_BIG_ENDIAN 1
376#else
Dave Rodgmane5c42592022-11-28 14:47:46 +0000377static const uint16_t mbedtls_byte_order_detector = { 0x100 };
378#define MBEDTLS_IS_BIG_ENDIAN (*((unsigned char *) (&mbedtls_byte_order_detector)) == 0x01)
Dave Rodgmanf3c04f32023-12-05 12:06:11 +0000379#endif
380
Dave Rodgmane5c42592022-11-28 14:47:46 +0000381#else
Dave Rodgmanf3c04f32023-12-05 12:06:11 +0000382
383#if (__BYTE_ORDER__) == (__ORDER_BIG_ENDIAN__)
384#define MBEDTLS_IS_BIG_ENDIAN 1
385#else
386#define MBEDTLS_IS_BIG_ENDIAN 0
387#endif
388
Dave Rodgmane5c42592022-11-28 14:47:46 +0000389#endif /* !defined(__BYTE_ORDER__) */
390
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000391/**
392 * Get the unsigned 32 bits integer corresponding to four bytes in
393 * big-endian order (MSB first).
394 *
395 * \param data Base address of the memory to get the four bytes from.
396 * \param offset Offset from \p data of the first and most significant
397 * byte of the four bytes to build the 32 bits unsigned
398 * integer from.
399 */
Dave Rodgman914c6322023-03-01 09:30:14 +0000400#define MBEDTLS_GET_UINT32_BE(data, offset) \
401 ((MBEDTLS_IS_BIG_ENDIAN) \
Dave Rodgmana5110b02022-11-28 14:48:45 +0000402 ? mbedtls_get_unaligned_uint32((data) + (offset)) \
403 : MBEDTLS_BSWAP32(mbedtls_get_unaligned_uint32((data) + (offset))) \
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000404 )
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000405
406/**
407 * Put in memory a 32 bits unsigned integer in big-endian order.
408 *
409 * \param n 32 bits unsigned integer to put in memory.
410 * \param data Base address of the memory where to put the 32
411 * bits unsigned integer in.
412 * \param offset Offset from \p data where to put the most significant
413 * byte of the 32 bits unsigned integer \p n.
414 */
Dave Rodgman914c6322023-03-01 09:30:14 +0000415#define MBEDTLS_PUT_UINT32_BE(n, data, offset) \
Gilles Peskine449bd832023-01-11 14:50:10 +0100416 { \
Dave Rodgman914c6322023-03-01 09:30:14 +0000417 if (MBEDTLS_IS_BIG_ENDIAN) \
Gilles Peskine449bd832023-01-11 14:50:10 +0100418 { \
Dave Rodgman914c6322023-03-01 09:30:14 +0000419 mbedtls_put_unaligned_uint32((data) + (offset), (uint32_t) (n)); \
Gilles Peskine449bd832023-01-11 14:50:10 +0100420 } \
421 else \
422 { \
423 mbedtls_put_unaligned_uint32((data) + (offset), MBEDTLS_BSWAP32((uint32_t) (n))); \
424 } \
425 }
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000426
427/**
428 * Get the unsigned 32 bits integer corresponding to four bytes in
429 * little-endian order (LSB first).
430 *
431 * \param data Base address of the memory to get the four bytes from.
432 * \param offset Offset from \p data of the first and least significant
433 * byte of the four bytes to build the 32 bits unsigned
434 * integer from.
435 */
Dave Rodgman914c6322023-03-01 09:30:14 +0000436#define MBEDTLS_GET_UINT32_LE(data, offset) \
437 ((MBEDTLS_IS_BIG_ENDIAN) \
Dave Rodgmana5110b02022-11-28 14:48:45 +0000438 ? MBEDTLS_BSWAP32(mbedtls_get_unaligned_uint32((data) + (offset))) \
439 : mbedtls_get_unaligned_uint32((data) + (offset)) \
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000440 )
Dave Rodgmana5110b02022-11-28 14:48:45 +0000441
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000442
443/**
444 * Put in memory a 32 bits unsigned integer in little-endian order.
445 *
446 * \param n 32 bits unsigned integer to put in memory.
447 * \param data Base address of the memory where to put the 32
448 * bits unsigned integer in.
449 * \param offset Offset from \p data where to put the least significant
450 * byte of the 32 bits unsigned integer \p n.
451 */
Dave Rodgman914c6322023-03-01 09:30:14 +0000452#define MBEDTLS_PUT_UINT32_LE(n, data, offset) \
Gilles Peskine449bd832023-01-11 14:50:10 +0100453 { \
Dave Rodgman914c6322023-03-01 09:30:14 +0000454 if (MBEDTLS_IS_BIG_ENDIAN) \
Gilles Peskine449bd832023-01-11 14:50:10 +0100455 { \
456 mbedtls_put_unaligned_uint32((data) + (offset), MBEDTLS_BSWAP32((uint32_t) (n))); \
457 } \
458 else \
459 { \
Dave Rodgman914c6322023-03-01 09:30:14 +0000460 mbedtls_put_unaligned_uint32((data) + (offset), ((uint32_t) (n))); \
Gilles Peskine449bd832023-01-11 14:50:10 +0100461 } \
462 }
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000463
464/**
465 * Get the unsigned 16 bits integer corresponding to two bytes in
466 * little-endian order (LSB first).
467 *
468 * \param data Base address of the memory to get the two bytes from.
469 * \param offset Offset from \p data of the first and least significant
470 * byte of the two bytes to build the 16 bits unsigned
471 * integer from.
472 */
Dave Rodgman914c6322023-03-01 09:30:14 +0000473#define MBEDTLS_GET_UINT16_LE(data, offset) \
474 ((MBEDTLS_IS_BIG_ENDIAN) \
Dave Rodgmana5110b02022-11-28 14:48:45 +0000475 ? MBEDTLS_BSWAP16(mbedtls_get_unaligned_uint16((data) + (offset))) \
476 : mbedtls_get_unaligned_uint16((data) + (offset)) \
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000477 )
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000478
479/**
480 * Put in memory a 16 bits unsigned integer in little-endian order.
481 *
482 * \param n 16 bits unsigned integer to put in memory.
483 * \param data Base address of the memory where to put the 16
484 * bits unsigned integer in.
485 * \param offset Offset from \p data where to put the least significant
486 * byte of the 16 bits unsigned integer \p n.
487 */
Dave Rodgman914c6322023-03-01 09:30:14 +0000488#define MBEDTLS_PUT_UINT16_LE(n, data, offset) \
Gilles Peskine449bd832023-01-11 14:50:10 +0100489 { \
Dave Rodgman914c6322023-03-01 09:30:14 +0000490 if (MBEDTLS_IS_BIG_ENDIAN) \
Gilles Peskine449bd832023-01-11 14:50:10 +0100491 { \
492 mbedtls_put_unaligned_uint16((data) + (offset), MBEDTLS_BSWAP16((uint16_t) (n))); \
493 } \
494 else \
495 { \
Dave Rodgman914c6322023-03-01 09:30:14 +0000496 mbedtls_put_unaligned_uint16((data) + (offset), (uint16_t) (n)); \
Gilles Peskine449bd832023-01-11 14:50:10 +0100497 } \
498 }
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000499
500/**
501 * Get the unsigned 16 bits integer corresponding to two bytes in
502 * big-endian order (MSB first).
503 *
504 * \param data Base address of the memory to get the two bytes from.
505 * \param offset Offset from \p data of the first and most significant
506 * byte of the two bytes to build the 16 bits unsigned
507 * integer from.
508 */
Dave Rodgman914c6322023-03-01 09:30:14 +0000509#define MBEDTLS_GET_UINT16_BE(data, offset) \
510 ((MBEDTLS_IS_BIG_ENDIAN) \
Dave Rodgmana5110b02022-11-28 14:48:45 +0000511 ? mbedtls_get_unaligned_uint16((data) + (offset)) \
512 : MBEDTLS_BSWAP16(mbedtls_get_unaligned_uint16((data) + (offset))) \
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000513 )
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000514
515/**
516 * Put in memory a 16 bits unsigned integer in big-endian order.
517 *
518 * \param n 16 bits unsigned integer to put in memory.
519 * \param data Base address of the memory where to put the 16
520 * bits unsigned integer in.
521 * \param offset Offset from \p data where to put the most significant
522 * byte of the 16 bits unsigned integer \p n.
523 */
Dave Rodgman914c6322023-03-01 09:30:14 +0000524#define MBEDTLS_PUT_UINT16_BE(n, data, offset) \
Gilles Peskine449bd832023-01-11 14:50:10 +0100525 { \
Dave Rodgman914c6322023-03-01 09:30:14 +0000526 if (MBEDTLS_IS_BIG_ENDIAN) \
Gilles Peskine449bd832023-01-11 14:50:10 +0100527 { \
Dave Rodgman914c6322023-03-01 09:30:14 +0000528 mbedtls_put_unaligned_uint16((data) + (offset), (uint16_t) (n)); \
Gilles Peskine449bd832023-01-11 14:50:10 +0100529 } \
530 else \
531 { \
532 mbedtls_put_unaligned_uint16((data) + (offset), MBEDTLS_BSWAP16((uint16_t) (n))); \
533 } \
534 }
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000535
536/**
537 * Get the unsigned 24 bits integer corresponding to three bytes in
538 * big-endian order (MSB first).
539 *
540 * \param data Base address of the memory to get the three bytes from.
541 * \param offset Offset from \p data of the first and most significant
542 * byte of the three bytes to build the 24 bits unsigned
543 * integer from.
544 */
Dave Rodgman914c6322023-03-01 09:30:14 +0000545#define MBEDTLS_GET_UINT24_BE(data, offset) \
546 ( \
547 ((uint32_t) (data)[(offset)] << 16) \
548 | ((uint32_t) (data)[(offset) + 1] << 8) \
549 | ((uint32_t) (data)[(offset) + 2]) \
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000550 )
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000551
552/**
553 * Put in memory a 24 bits unsigned integer in big-endian order.
554 *
555 * \param n 24 bits unsigned integer to put in memory.
556 * \param data Base address of the memory where to put the 24
557 * bits unsigned integer in.
558 * \param offset Offset from \p data where to put the most significant
559 * byte of the 24 bits unsigned integer \p n.
560 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100561#define MBEDTLS_PUT_UINT24_BE(n, data, offset) \
Dave Rodgman914c6322023-03-01 09:30:14 +0000562 { \
563 (data)[(offset)] = MBEDTLS_BYTE_2(n); \
Gilles Peskine449bd832023-01-11 14:50:10 +0100564 (data)[(offset) + 1] = MBEDTLS_BYTE_1(n); \
565 (data)[(offset) + 2] = MBEDTLS_BYTE_0(n); \
566 }
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000567
568/**
569 * Get the unsigned 24 bits integer corresponding to three bytes in
570 * little-endian order (LSB first).
571 *
572 * \param data Base address of the memory to get the three bytes from.
573 * \param offset Offset from \p data of the first and least significant
574 * byte of the three bytes to build the 24 bits unsigned
575 * integer from.
576 */
Dave Rodgman914c6322023-03-01 09:30:14 +0000577#define MBEDTLS_GET_UINT24_LE(data, offset) \
578 ( \
579 ((uint32_t) (data)[(offset)]) \
Gilles Peskine449bd832023-01-11 14:50:10 +0100580 | ((uint32_t) (data)[(offset) + 1] << 8) \
581 | ((uint32_t) (data)[(offset) + 2] << 16) \
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000582 )
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000583
584/**
585 * Put in memory a 24 bits unsigned integer in little-endian order.
586 *
587 * \param n 24 bits unsigned integer to put in memory.
588 * \param data Base address of the memory where to put the 24
589 * bits unsigned integer in.
590 * \param offset Offset from \p data where to put the least significant
591 * byte of the 24 bits unsigned integer \p n.
592 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100593#define MBEDTLS_PUT_UINT24_LE(n, data, offset) \
Dave Rodgman914c6322023-03-01 09:30:14 +0000594 { \
595 (data)[(offset)] = MBEDTLS_BYTE_0(n); \
Gilles Peskine449bd832023-01-11 14:50:10 +0100596 (data)[(offset) + 1] = MBEDTLS_BYTE_1(n); \
597 (data)[(offset) + 2] = MBEDTLS_BYTE_2(n); \
598 }
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000599
600/**
601 * Get the unsigned 64 bits integer corresponding to eight bytes in
602 * big-endian order (MSB first).
603 *
604 * \param data Base address of the memory to get the eight bytes from.
605 * \param offset Offset from \p data of the first and most significant
606 * byte of the eight bytes to build the 64 bits unsigned
607 * integer from.
608 */
Dave Rodgman914c6322023-03-01 09:30:14 +0000609#define MBEDTLS_GET_UINT64_BE(data, offset) \
610 ((MBEDTLS_IS_BIG_ENDIAN) \
Dave Rodgmana5110b02022-11-28 14:48:45 +0000611 ? mbedtls_get_unaligned_uint64((data) + (offset)) \
612 : MBEDTLS_BSWAP64(mbedtls_get_unaligned_uint64((data) + (offset))) \
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000613 )
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000614
615/**
616 * Put in memory a 64 bits unsigned integer in big-endian order.
617 *
618 * \param n 64 bits unsigned integer to put in memory.
619 * \param data Base address of the memory where to put the 64
620 * bits unsigned integer in.
621 * \param offset Offset from \p data where to put the most significant
622 * byte of the 64 bits unsigned integer \p n.
623 */
Dave Rodgman914c6322023-03-01 09:30:14 +0000624#define MBEDTLS_PUT_UINT64_BE(n, data, offset) \
Gilles Peskine449bd832023-01-11 14:50:10 +0100625 { \
Dave Rodgman914c6322023-03-01 09:30:14 +0000626 if (MBEDTLS_IS_BIG_ENDIAN) \
Gilles Peskine449bd832023-01-11 14:50:10 +0100627 { \
Dave Rodgman914c6322023-03-01 09:30:14 +0000628 mbedtls_put_unaligned_uint64((data) + (offset), (uint64_t) (n)); \
Gilles Peskine449bd832023-01-11 14:50:10 +0100629 } \
630 else \
631 { \
632 mbedtls_put_unaligned_uint64((data) + (offset), MBEDTLS_BSWAP64((uint64_t) (n))); \
633 } \
634 }
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000635
636/**
637 * Get the unsigned 64 bits integer corresponding to eight bytes in
638 * little-endian order (LSB first).
639 *
640 * \param data Base address of the memory to get the eight bytes from.
641 * \param offset Offset from \p data of the first and least significant
642 * byte of the eight bytes to build the 64 bits unsigned
643 * integer from.
644 */
Dave Rodgman914c6322023-03-01 09:30:14 +0000645#define MBEDTLS_GET_UINT64_LE(data, offset) \
646 ((MBEDTLS_IS_BIG_ENDIAN) \
Dave Rodgmana5110b02022-11-28 14:48:45 +0000647 ? MBEDTLS_BSWAP64(mbedtls_get_unaligned_uint64((data) + (offset))) \
648 : mbedtls_get_unaligned_uint64((data) + (offset)) \
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000649 )
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000650
651/**
652 * Put in memory a 64 bits unsigned integer in little-endian order.
653 *
654 * \param n 64 bits unsigned integer to put in memory.
655 * \param data Base address of the memory where to put the 64
656 * bits unsigned integer in.
657 * \param offset Offset from \p data where to put the least significant
658 * byte of the 64 bits unsigned integer \p n.
659 */
Dave Rodgman914c6322023-03-01 09:30:14 +0000660#define MBEDTLS_PUT_UINT64_LE(n, data, offset) \
Gilles Peskine449bd832023-01-11 14:50:10 +0100661 { \
Dave Rodgman914c6322023-03-01 09:30:14 +0000662 if (MBEDTLS_IS_BIG_ENDIAN) \
Gilles Peskine449bd832023-01-11 14:50:10 +0100663 { \
664 mbedtls_put_unaligned_uint64((data) + (offset), MBEDTLS_BSWAP64((uint64_t) (n))); \
665 } \
666 else \
667 { \
Dave Rodgman914c6322023-03-01 09:30:14 +0000668 mbedtls_put_unaligned_uint64((data) + (offset), (uint64_t) (n)); \
Gilles Peskine449bd832023-01-11 14:50:10 +0100669 } \
670 }
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000671
672#endif /* MBEDTLS_LIBRARY_ALIGNMENT_H */