blob: ff811e34d523536e71ac7a4ed75a93112f3442be [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
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 Rodgman96d61d12022-11-24 19:33:22 +000027#include <string.h>
Dave Rodgmanf7f1f742022-11-28 14:52:45 +000028#include <stdlib.h>
Dave Rodgmanfbc23222022-11-24 18:07:37 +000029
Dave Rodgmanb9cd19b2022-12-30 21:32:03 +000030/*
31 * Define MBEDTLS_EFFICIENT_UNALIGNED_ACCESS for architectures where unaligned memory
Dave Rodgman7f376fa2023-01-05 12:25:15 +000032 * accesses are known to be efficient.
33 *
34 * All functions defined here will behave correctly regardless, but might be less
35 * efficient when this is not defined.
Dave Rodgmanb9cd19b2022-12-30 21:32:03 +000036 */
37#if defined(__ARM_FEATURE_UNALIGNED) \
Dave Rodgmanc5cc7272023-09-15 11:41:17 +010038 || defined(MBEDTLS_ARCH_IS_X86) || defined(MBEDTLS_ARCH_IS_X64) \
Dave Rodgman78fc0bd2023-08-08 10:36:15 +010039 || defined(_M_ARM64) || defined(_M_ARM64EC)
Dave Rodgmanb9cd19b2022-12-30 21:32:03 +000040/*
41 * __ARM_FEATURE_UNALIGNED is defined where appropriate by armcc, gcc 7, clang 9
Dave Rodgman7f376fa2023-01-05 12:25:15 +000042 * (and later versions) for Arm v7 and later; all x86 platforms should have
43 * efficient unaligned access.
Dave Rodgman78fc0bd2023-08-08 10:36:15 +010044 *
45 * https://learn.microsoft.com/en-us/cpp/build/arm64-windows-abi-conventions?view=msvc-170#alignment
46 * specifies that on Windows-on-Arm64, unaligned access is safe (except for uncached
47 * device memory).
Dave Rodgmanb9cd19b2022-12-30 21:32:03 +000048 */
49#define MBEDTLS_EFFICIENT_UNALIGNED_ACCESS
50#endif
51
Dave Rodgman96d61d12022-11-24 19:33:22 +000052/**
Dave Rodgmana360e192022-11-28 14:44:05 +000053 * Read the unsigned 16 bits integer from the given address, which need not
54 * be aligned.
55 *
56 * \param p pointer to 2 bytes of data
57 * \return Data at the given address
58 */
Gilles Peskine449bd832023-01-11 14:50:10 +010059inline uint16_t mbedtls_get_unaligned_uint16(const void *p)
Dave Rodgmana360e192022-11-28 14:44:05 +000060{
61 uint16_t r;
Gilles Peskine449bd832023-01-11 14:50:10 +010062 memcpy(&r, p, sizeof(r));
Dave Rodgmana360e192022-11-28 14:44:05 +000063 return r;
64}
65
66/**
67 * Write the unsigned 16 bits integer to the given address, which need not
68 * be aligned.
69 *
70 * \param p pointer to 2 bytes of data
71 * \param x data to write
72 */
Gilles Peskine449bd832023-01-11 14:50:10 +010073inline void mbedtls_put_unaligned_uint16(void *p, uint16_t x)
Dave Rodgmana360e192022-11-28 14:44:05 +000074{
Gilles Peskine449bd832023-01-11 14:50:10 +010075 memcpy(p, &x, sizeof(x));
Dave Rodgmana360e192022-11-28 14:44:05 +000076}
77
78/**
Dave Rodgman96d61d12022-11-24 19:33:22 +000079 * Read the unsigned 32 bits integer from the given address, which need not
80 * be aligned.
Dave Rodgmanfbc23222022-11-24 18:07:37 +000081 *
Dave Rodgman96d61d12022-11-24 19:33:22 +000082 * \param p pointer to 4 bytes of data
Dave Rodgman875d2382022-11-24 20:43:15 +000083 * \return Data at the given address
Dave Rodgmanfbc23222022-11-24 18:07:37 +000084 */
Gilles Peskine449bd832023-01-11 14:50:10 +010085inline uint32_t mbedtls_get_unaligned_uint32(const void *p)
Dave Rodgman96d61d12022-11-24 19:33:22 +000086{
87 uint32_t r;
Gilles Peskine449bd832023-01-11 14:50:10 +010088 memcpy(&r, p, sizeof(r));
Dave Rodgman96d61d12022-11-24 19:33:22 +000089 return r;
90}
Dave Rodgmanfbc23222022-11-24 18:07:37 +000091
Dave Rodgman96d61d12022-11-24 19:33:22 +000092/**
93 * Write the unsigned 32 bits integer to the given address, which need not
94 * be aligned.
95 *
96 * \param p pointer to 4 bytes of data
97 * \param x data to write
98 */
Gilles Peskine449bd832023-01-11 14:50:10 +010099inline void mbedtls_put_unaligned_uint32(void *p, uint32_t x)
Dave Rodgman96d61d12022-11-24 19:33:22 +0000100{
Gilles Peskine449bd832023-01-11 14:50:10 +0100101 memcpy(p, &x, sizeof(x));
Dave Rodgman96d61d12022-11-24 19:33:22 +0000102}
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000103
Dave Rodgmana360e192022-11-28 14:44:05 +0000104/**
105 * Read the unsigned 64 bits integer from the given address, which need not
106 * be aligned.
107 *
108 * \param p pointer to 8 bytes of data
109 * \return Data at the given address
110 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100111inline uint64_t mbedtls_get_unaligned_uint64(const void *p)
Dave Rodgmana360e192022-11-28 14:44:05 +0000112{
113 uint64_t r;
Gilles Peskine449bd832023-01-11 14:50:10 +0100114 memcpy(&r, p, sizeof(r));
Dave Rodgmana360e192022-11-28 14:44:05 +0000115 return r;
116}
117
118/**
119 * Write the unsigned 64 bits integer to the given address, which need not
120 * be aligned.
121 *
122 * \param p pointer to 8 bytes of data
123 * \param x data to write
124 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100125inline void mbedtls_put_unaligned_uint64(void *p, uint64_t x)
Dave Rodgmana360e192022-11-28 14:44:05 +0000126{
Gilles Peskine449bd832023-01-11 14:50:10 +0100127 memcpy(p, &x, sizeof(x));
Dave Rodgmana360e192022-11-28 14:44:05 +0000128}
129
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000130/** Byte Reading Macros
131 *
132 * Given a multi-byte integer \p x, MBEDTLS_BYTE_n retrieves the n-th
133 * byte from x, where byte 0 is the least significant byte.
134 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100135#define MBEDTLS_BYTE_0(x) ((uint8_t) ((x) & 0xff))
Dave Rodgman914c6322023-03-01 09:30:14 +0000136#define MBEDTLS_BYTE_1(x) ((uint8_t) (((x) >> 8) & 0xff))
Gilles Peskine449bd832023-01-11 14:50:10 +0100137#define MBEDTLS_BYTE_2(x) ((uint8_t) (((x) >> 16) & 0xff))
138#define MBEDTLS_BYTE_3(x) ((uint8_t) (((x) >> 24) & 0xff))
139#define MBEDTLS_BYTE_4(x) ((uint8_t) (((x) >> 32) & 0xff))
140#define MBEDTLS_BYTE_5(x) ((uint8_t) (((x) >> 40) & 0xff))
141#define MBEDTLS_BYTE_6(x) ((uint8_t) (((x) >> 48) & 0xff))
142#define MBEDTLS_BYTE_7(x) ((uint8_t) (((x) >> 56) & 0xff))
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000143
Dave Rodgmanf7f1f742022-11-28 14:52:45 +0000144/*
145 * Detect GCC built-in byteswap routines
146 */
147#if defined(__GNUC__) && defined(__GNUC_PREREQ)
Gilles Peskine449bd832023-01-11 14:50:10 +0100148#if __GNUC_PREREQ(4, 8)
Dave Rodgmanf7f1f742022-11-28 14:52:45 +0000149#define MBEDTLS_BSWAP16 __builtin_bswap16
150#endif /* __GNUC_PREREQ(4,8) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100151#if __GNUC_PREREQ(4, 3)
Dave Rodgmanf7f1f742022-11-28 14:52:45 +0000152#define MBEDTLS_BSWAP32 __builtin_bswap32
153#define MBEDTLS_BSWAP64 __builtin_bswap64
154#endif /* __GNUC_PREREQ(4,3) */
155#endif /* defined(__GNUC__) && defined(__GNUC_PREREQ) */
156
157/*
158 * Detect Clang built-in byteswap routines
159 */
160#if defined(__clang__) && defined(__has_builtin)
Dave Rodgmane47899d2023-02-28 17:39:03 +0000161#if __has_builtin(__builtin_bswap16) && !defined(MBEDTLS_BSWAP16)
Dave Rodgmanf7f1f742022-11-28 14:52:45 +0000162#define MBEDTLS_BSWAP16 __builtin_bswap16
163#endif /* __has_builtin(__builtin_bswap16) */
Dave Rodgmane47899d2023-02-28 17:39:03 +0000164#if __has_builtin(__builtin_bswap32) && !defined(MBEDTLS_BSWAP32)
Dave Rodgmanf7f1f742022-11-28 14:52:45 +0000165#define MBEDTLS_BSWAP32 __builtin_bswap32
166#endif /* __has_builtin(__builtin_bswap32) */
Dave Rodgmane47899d2023-02-28 17:39:03 +0000167#if __has_builtin(__builtin_bswap64) && !defined(MBEDTLS_BSWAP64)
Dave Rodgmanf7f1f742022-11-28 14:52:45 +0000168#define MBEDTLS_BSWAP64 __builtin_bswap64
169#endif /* __has_builtin(__builtin_bswap64) */
170#endif /* defined(__clang__) && defined(__has_builtin) */
171
172/*
173 * Detect MSVC built-in byteswap routines
174 */
175#if defined(_MSC_VER)
Dave Rodgmane47899d2023-02-28 17:39:03 +0000176#if !defined(MBEDTLS_BSWAP16)
Dave Rodgmanf7f1f742022-11-28 14:52:45 +0000177#define MBEDTLS_BSWAP16 _byteswap_ushort
Dave Rodgmane47899d2023-02-28 17:39:03 +0000178#endif
179#if !defined(MBEDTLS_BSWAP32)
Dave Rodgmanf7f1f742022-11-28 14:52:45 +0000180#define MBEDTLS_BSWAP32 _byteswap_ulong
Dave Rodgmane47899d2023-02-28 17:39:03 +0000181#endif
182#if !defined(MBEDTLS_BSWAP64)
Dave Rodgmanf7f1f742022-11-28 14:52:45 +0000183#define MBEDTLS_BSWAP64 _byteswap_uint64
Dave Rodgmane47899d2023-02-28 17:39:03 +0000184#endif
Dave Rodgmanf7f1f742022-11-28 14:52:45 +0000185#endif /* defined(_MSC_VER) */
186
Dave Rodgman2dae4b32022-11-30 12:07:36 +0000187/* Detect armcc built-in byteswap routine */
Dave Rodgmane47899d2023-02-28 17:39:03 +0000188#if defined(__ARMCC_VERSION) && (__ARMCC_VERSION >= 410000) && !defined(MBEDTLS_BSWAP32)
Tom Cosgrovef2b5a132023-04-26 17:00:12 +0100189#if defined(__ARM_ACLE) /* ARM Compiler 6 - earlier versions don't need a header */
190#include <arm_acle.h>
191#endif
Dave Rodgman2dae4b32022-11-30 12:07:36 +0000192#define MBEDTLS_BSWAP32 __rev
193#endif
194
Dave Rodgmanf7f1f742022-11-28 14:52:45 +0000195/*
196 * Where compiler built-ins are not present, fall back to C code that the
197 * compiler may be able to detect and transform into the relevant bswap or
198 * similar instruction.
199 */
200#if !defined(MBEDTLS_BSWAP16)
Gilles Peskine449bd832023-01-11 14:50:10 +0100201static inline uint16_t mbedtls_bswap16(uint16_t x)
202{
Dave Rodgman6298b242022-11-28 14:51:49 +0000203 return
Gilles Peskine449bd832023-01-11 14:50:10 +0100204 (x & 0x00ff) << 8 |
205 (x & 0xff00) >> 8;
Dave Rodgman6298b242022-11-28 14:51:49 +0000206}
207#define MBEDTLS_BSWAP16 mbedtls_bswap16
Dave Rodgmanf7f1f742022-11-28 14:52:45 +0000208#endif /* !defined(MBEDTLS_BSWAP16) */
Dave Rodgman6298b242022-11-28 14:51:49 +0000209
Dave Rodgmanf7f1f742022-11-28 14:52:45 +0000210#if !defined(MBEDTLS_BSWAP32)
Gilles Peskine449bd832023-01-11 14:50:10 +0100211static inline uint32_t mbedtls_bswap32(uint32_t x)
212{
Dave Rodgman6298b242022-11-28 14:51:49 +0000213 return
Gilles Peskine449bd832023-01-11 14:50:10 +0100214 (x & 0x000000ff) << 24 |
215 (x & 0x0000ff00) << 8 |
216 (x & 0x00ff0000) >> 8 |
217 (x & 0xff000000) >> 24;
Dave Rodgman6298b242022-11-28 14:51:49 +0000218}
219#define MBEDTLS_BSWAP32 mbedtls_bswap32
Dave Rodgmanf7f1f742022-11-28 14:52:45 +0000220#endif /* !defined(MBEDTLS_BSWAP32) */
Dave Rodgman6298b242022-11-28 14:51:49 +0000221
Dave Rodgmanf7f1f742022-11-28 14:52:45 +0000222#if !defined(MBEDTLS_BSWAP64)
Gilles Peskine449bd832023-01-11 14:50:10 +0100223static inline uint64_t mbedtls_bswap64(uint64_t x)
224{
Dave Rodgman6298b242022-11-28 14:51:49 +0000225 return
Tom Cosgrovebbe166e2023-03-08 13:23:24 +0000226 (x & 0x00000000000000ffULL) << 56 |
227 (x & 0x000000000000ff00ULL) << 40 |
228 (x & 0x0000000000ff0000ULL) << 24 |
229 (x & 0x00000000ff000000ULL) << 8 |
230 (x & 0x000000ff00000000ULL) >> 8 |
231 (x & 0x0000ff0000000000ULL) >> 24 |
232 (x & 0x00ff000000000000ULL) >> 40 |
233 (x & 0xff00000000000000ULL) >> 56;
Dave Rodgman6298b242022-11-28 14:51:49 +0000234}
235#define MBEDTLS_BSWAP64 mbedtls_bswap64
Dave Rodgmanf7f1f742022-11-28 14:52:45 +0000236#endif /* !defined(MBEDTLS_BSWAP64) */
Dave Rodgman6298b242022-11-28 14:51:49 +0000237
Dave Rodgmane5c42592022-11-28 14:47:46 +0000238#if !defined(__BYTE_ORDER__)
239static const uint16_t mbedtls_byte_order_detector = { 0x100 };
240#define MBEDTLS_IS_BIG_ENDIAN (*((unsigned char *) (&mbedtls_byte_order_detector)) == 0x01)
241#else
242#define MBEDTLS_IS_BIG_ENDIAN ((__BYTE_ORDER__) == (__ORDER_BIG_ENDIAN__))
243#endif /* !defined(__BYTE_ORDER__) */
244
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000245/**
246 * Get the unsigned 32 bits integer corresponding to four bytes in
247 * big-endian order (MSB first).
248 *
249 * \param data Base address of the memory to get the four bytes from.
250 * \param offset Offset from \p data of the first and most significant
251 * byte of the four bytes to build the 32 bits unsigned
252 * integer from.
253 */
Dave Rodgman914c6322023-03-01 09:30:14 +0000254#define MBEDTLS_GET_UINT32_BE(data, offset) \
255 ((MBEDTLS_IS_BIG_ENDIAN) \
Dave Rodgmana5110b02022-11-28 14:48:45 +0000256 ? mbedtls_get_unaligned_uint32((data) + (offset)) \
257 : MBEDTLS_BSWAP32(mbedtls_get_unaligned_uint32((data) + (offset))) \
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000258 )
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000259
260/**
261 * Put in memory a 32 bits unsigned integer in big-endian order.
262 *
263 * \param n 32 bits unsigned integer to put in memory.
264 * \param data Base address of the memory where to put the 32
265 * bits unsigned integer in.
266 * \param offset Offset from \p data where to put the most significant
267 * byte of the 32 bits unsigned integer \p n.
268 */
Dave Rodgman914c6322023-03-01 09:30:14 +0000269#define MBEDTLS_PUT_UINT32_BE(n, data, offset) \
Gilles Peskine449bd832023-01-11 14:50:10 +0100270 { \
Dave Rodgman914c6322023-03-01 09:30:14 +0000271 if (MBEDTLS_IS_BIG_ENDIAN) \
Gilles Peskine449bd832023-01-11 14:50:10 +0100272 { \
Dave Rodgman914c6322023-03-01 09:30:14 +0000273 mbedtls_put_unaligned_uint32((data) + (offset), (uint32_t) (n)); \
Gilles Peskine449bd832023-01-11 14:50:10 +0100274 } \
275 else \
276 { \
277 mbedtls_put_unaligned_uint32((data) + (offset), MBEDTLS_BSWAP32((uint32_t) (n))); \
278 } \
279 }
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000280
281/**
282 * Get the unsigned 32 bits integer corresponding to four bytes in
283 * little-endian order (LSB first).
284 *
285 * \param data Base address of the memory to get the four bytes from.
286 * \param offset Offset from \p data of the first and least significant
287 * byte of the four bytes to build the 32 bits unsigned
288 * integer from.
289 */
Dave Rodgman914c6322023-03-01 09:30:14 +0000290#define MBEDTLS_GET_UINT32_LE(data, offset) \
291 ((MBEDTLS_IS_BIG_ENDIAN) \
Dave Rodgmana5110b02022-11-28 14:48:45 +0000292 ? MBEDTLS_BSWAP32(mbedtls_get_unaligned_uint32((data) + (offset))) \
293 : mbedtls_get_unaligned_uint32((data) + (offset)) \
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000294 )
Dave Rodgmana5110b02022-11-28 14:48:45 +0000295
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000296
297/**
298 * Put in memory a 32 bits unsigned integer in little-endian order.
299 *
300 * \param n 32 bits unsigned integer to put in memory.
301 * \param data Base address of the memory where to put the 32
302 * bits unsigned integer in.
303 * \param offset Offset from \p data where to put the least significant
304 * byte of the 32 bits unsigned integer \p n.
305 */
Dave Rodgman914c6322023-03-01 09:30:14 +0000306#define MBEDTLS_PUT_UINT32_LE(n, data, offset) \
Gilles Peskine449bd832023-01-11 14:50:10 +0100307 { \
Dave Rodgman914c6322023-03-01 09:30:14 +0000308 if (MBEDTLS_IS_BIG_ENDIAN) \
Gilles Peskine449bd832023-01-11 14:50:10 +0100309 { \
310 mbedtls_put_unaligned_uint32((data) + (offset), MBEDTLS_BSWAP32((uint32_t) (n))); \
311 } \
312 else \
313 { \
Dave Rodgman914c6322023-03-01 09:30:14 +0000314 mbedtls_put_unaligned_uint32((data) + (offset), ((uint32_t) (n))); \
Gilles Peskine449bd832023-01-11 14:50:10 +0100315 } \
316 }
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000317
318/**
319 * Get the unsigned 16 bits integer corresponding to two bytes in
320 * little-endian order (LSB first).
321 *
322 * \param data Base address of the memory to get the two bytes from.
323 * \param offset Offset from \p data of the first and least significant
324 * byte of the two bytes to build the 16 bits unsigned
325 * integer from.
326 */
Dave Rodgman914c6322023-03-01 09:30:14 +0000327#define MBEDTLS_GET_UINT16_LE(data, offset) \
328 ((MBEDTLS_IS_BIG_ENDIAN) \
Dave Rodgmana5110b02022-11-28 14:48:45 +0000329 ? MBEDTLS_BSWAP16(mbedtls_get_unaligned_uint16((data) + (offset))) \
330 : mbedtls_get_unaligned_uint16((data) + (offset)) \
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000331 )
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000332
333/**
334 * Put in memory a 16 bits unsigned integer in little-endian order.
335 *
336 * \param n 16 bits unsigned integer to put in memory.
337 * \param data Base address of the memory where to put the 16
338 * bits unsigned integer in.
339 * \param offset Offset from \p data where to put the least significant
340 * byte of the 16 bits unsigned integer \p n.
341 */
Dave Rodgman914c6322023-03-01 09:30:14 +0000342#define MBEDTLS_PUT_UINT16_LE(n, data, offset) \
Gilles Peskine449bd832023-01-11 14:50:10 +0100343 { \
Dave Rodgman914c6322023-03-01 09:30:14 +0000344 if (MBEDTLS_IS_BIG_ENDIAN) \
Gilles Peskine449bd832023-01-11 14:50:10 +0100345 { \
346 mbedtls_put_unaligned_uint16((data) + (offset), MBEDTLS_BSWAP16((uint16_t) (n))); \
347 } \
348 else \
349 { \
Dave Rodgman914c6322023-03-01 09:30:14 +0000350 mbedtls_put_unaligned_uint16((data) + (offset), (uint16_t) (n)); \
Gilles Peskine449bd832023-01-11 14:50:10 +0100351 } \
352 }
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000353
354/**
355 * Get the unsigned 16 bits integer corresponding to two bytes in
356 * big-endian order (MSB first).
357 *
358 * \param data Base address of the memory to get the two bytes from.
359 * \param offset Offset from \p data of the first and most significant
360 * byte of the two bytes to build the 16 bits unsigned
361 * integer from.
362 */
Dave Rodgman914c6322023-03-01 09:30:14 +0000363#define MBEDTLS_GET_UINT16_BE(data, offset) \
364 ((MBEDTLS_IS_BIG_ENDIAN) \
Dave Rodgmana5110b02022-11-28 14:48:45 +0000365 ? mbedtls_get_unaligned_uint16((data) + (offset)) \
366 : MBEDTLS_BSWAP16(mbedtls_get_unaligned_uint16((data) + (offset))) \
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000367 )
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000368
369/**
370 * Put in memory a 16 bits unsigned integer in big-endian order.
371 *
372 * \param n 16 bits unsigned integer to put in memory.
373 * \param data Base address of the memory where to put the 16
374 * bits unsigned integer in.
375 * \param offset Offset from \p data where to put the most significant
376 * byte of the 16 bits unsigned integer \p n.
377 */
Dave Rodgman914c6322023-03-01 09:30:14 +0000378#define MBEDTLS_PUT_UINT16_BE(n, data, offset) \
Gilles Peskine449bd832023-01-11 14:50:10 +0100379 { \
Dave Rodgman914c6322023-03-01 09:30:14 +0000380 if (MBEDTLS_IS_BIG_ENDIAN) \
Gilles Peskine449bd832023-01-11 14:50:10 +0100381 { \
Dave Rodgman914c6322023-03-01 09:30:14 +0000382 mbedtls_put_unaligned_uint16((data) + (offset), (uint16_t) (n)); \
Gilles Peskine449bd832023-01-11 14:50:10 +0100383 } \
384 else \
385 { \
386 mbedtls_put_unaligned_uint16((data) + (offset), MBEDTLS_BSWAP16((uint16_t) (n))); \
387 } \
388 }
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000389
390/**
391 * Get the unsigned 24 bits integer corresponding to three bytes in
392 * big-endian order (MSB first).
393 *
394 * \param data Base address of the memory to get the three bytes from.
395 * \param offset Offset from \p data of the first and most significant
396 * byte of the three bytes to build the 24 bits unsigned
397 * integer from.
398 */
Dave Rodgman914c6322023-03-01 09:30:14 +0000399#define MBEDTLS_GET_UINT24_BE(data, offset) \
400 ( \
401 ((uint32_t) (data)[(offset)] << 16) \
402 | ((uint32_t) (data)[(offset) + 1] << 8) \
403 | ((uint32_t) (data)[(offset) + 2]) \
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000404 )
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000405
406/**
407 * Put in memory a 24 bits unsigned integer in big-endian order.
408 *
409 * \param n 24 bits unsigned integer to put in memory.
410 * \param data Base address of the memory where to put the 24
411 * bits unsigned integer in.
412 * \param offset Offset from \p data where to put the most significant
413 * byte of the 24 bits unsigned integer \p n.
414 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100415#define MBEDTLS_PUT_UINT24_BE(n, data, offset) \
Dave Rodgman914c6322023-03-01 09:30:14 +0000416 { \
417 (data)[(offset)] = MBEDTLS_BYTE_2(n); \
Gilles Peskine449bd832023-01-11 14:50:10 +0100418 (data)[(offset) + 1] = MBEDTLS_BYTE_1(n); \
419 (data)[(offset) + 2] = MBEDTLS_BYTE_0(n); \
420 }
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000421
422/**
423 * Get the unsigned 24 bits integer corresponding to three bytes in
424 * little-endian order (LSB first).
425 *
426 * \param data Base address of the memory to get the three bytes from.
427 * \param offset Offset from \p data of the first and least significant
428 * byte of the three bytes to build the 24 bits unsigned
429 * integer from.
430 */
Dave Rodgman914c6322023-03-01 09:30:14 +0000431#define MBEDTLS_GET_UINT24_LE(data, offset) \
432 ( \
433 ((uint32_t) (data)[(offset)]) \
Gilles Peskine449bd832023-01-11 14:50:10 +0100434 | ((uint32_t) (data)[(offset) + 1] << 8) \
435 | ((uint32_t) (data)[(offset) + 2] << 16) \
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000436 )
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000437
438/**
439 * Put in memory a 24 bits unsigned integer in little-endian order.
440 *
441 * \param n 24 bits unsigned integer to put in memory.
442 * \param data Base address of the memory where to put the 24
443 * bits unsigned integer in.
444 * \param offset Offset from \p data where to put the least significant
445 * byte of the 24 bits unsigned integer \p n.
446 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100447#define MBEDTLS_PUT_UINT24_LE(n, data, offset) \
Dave Rodgman914c6322023-03-01 09:30:14 +0000448 { \
449 (data)[(offset)] = MBEDTLS_BYTE_0(n); \
Gilles Peskine449bd832023-01-11 14:50:10 +0100450 (data)[(offset) + 1] = MBEDTLS_BYTE_1(n); \
451 (data)[(offset) + 2] = MBEDTLS_BYTE_2(n); \
452 }
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000453
454/**
455 * Get the unsigned 64 bits integer corresponding to eight bytes in
456 * big-endian order (MSB first).
457 *
458 * \param data Base address of the memory to get the eight bytes from.
459 * \param offset Offset from \p data of the first and most significant
460 * byte of the eight bytes to build the 64 bits unsigned
461 * integer from.
462 */
Dave Rodgman914c6322023-03-01 09:30:14 +0000463#define MBEDTLS_GET_UINT64_BE(data, offset) \
464 ((MBEDTLS_IS_BIG_ENDIAN) \
Dave Rodgmana5110b02022-11-28 14:48:45 +0000465 ? mbedtls_get_unaligned_uint64((data) + (offset)) \
466 : MBEDTLS_BSWAP64(mbedtls_get_unaligned_uint64((data) + (offset))) \
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000467 )
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000468
469/**
470 * Put in memory a 64 bits unsigned integer in big-endian order.
471 *
472 * \param n 64 bits unsigned integer to put in memory.
473 * \param data Base address of the memory where to put the 64
474 * bits unsigned integer in.
475 * \param offset Offset from \p data where to put the most significant
476 * byte of the 64 bits unsigned integer \p n.
477 */
Dave Rodgman914c6322023-03-01 09:30:14 +0000478#define MBEDTLS_PUT_UINT64_BE(n, data, offset) \
Gilles Peskine449bd832023-01-11 14:50:10 +0100479 { \
Dave Rodgman914c6322023-03-01 09:30:14 +0000480 if (MBEDTLS_IS_BIG_ENDIAN) \
Gilles Peskine449bd832023-01-11 14:50:10 +0100481 { \
Dave Rodgman914c6322023-03-01 09:30:14 +0000482 mbedtls_put_unaligned_uint64((data) + (offset), (uint64_t) (n)); \
Gilles Peskine449bd832023-01-11 14:50:10 +0100483 } \
484 else \
485 { \
486 mbedtls_put_unaligned_uint64((data) + (offset), MBEDTLS_BSWAP64((uint64_t) (n))); \
487 } \
488 }
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000489
490/**
491 * Get the unsigned 64 bits integer corresponding to eight bytes in
492 * little-endian order (LSB first).
493 *
494 * \param data Base address of the memory to get the eight bytes from.
495 * \param offset Offset from \p data of the first and least significant
496 * byte of the eight bytes to build the 64 bits unsigned
497 * integer from.
498 */
Dave Rodgman914c6322023-03-01 09:30:14 +0000499#define MBEDTLS_GET_UINT64_LE(data, offset) \
500 ((MBEDTLS_IS_BIG_ENDIAN) \
Dave Rodgmana5110b02022-11-28 14:48:45 +0000501 ? MBEDTLS_BSWAP64(mbedtls_get_unaligned_uint64((data) + (offset))) \
502 : mbedtls_get_unaligned_uint64((data) + (offset)) \
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000503 )
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000504
505/**
506 * Put in memory a 64 bits unsigned integer in little-endian order.
507 *
508 * \param n 64 bits unsigned integer to put in memory.
509 * \param data Base address of the memory where to put the 64
510 * bits unsigned integer in.
511 * \param offset Offset from \p data where to put the least significant
512 * byte of the 64 bits unsigned integer \p n.
513 */
Dave Rodgman914c6322023-03-01 09:30:14 +0000514#define MBEDTLS_PUT_UINT64_LE(n, data, offset) \
Gilles Peskine449bd832023-01-11 14:50:10 +0100515 { \
Dave Rodgman914c6322023-03-01 09:30:14 +0000516 if (MBEDTLS_IS_BIG_ENDIAN) \
Gilles Peskine449bd832023-01-11 14:50:10 +0100517 { \
518 mbedtls_put_unaligned_uint64((data) + (offset), MBEDTLS_BSWAP64((uint64_t) (n))); \
519 } \
520 else \
521 { \
Dave Rodgman914c6322023-03-01 09:30:14 +0000522 mbedtls_put_unaligned_uint64((data) + (offset), (uint64_t) (n)); \
Gilles Peskine449bd832023-01-11 14:50:10 +0100523 } \
524 }
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000525
526#endif /* MBEDTLS_LIBRARY_ALIGNMENT_H */