blob: bfc965eae1fc5c92bfb110da920934ad14daff77 [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 Rodgmana616afe2022-11-25 17:11:45 +000030#include "mbedtls/build_info.h"
Dave Rodgman8f6583d2022-11-25 09:16:41 +000031
Dave Rodgman96d61d12022-11-24 19:33:22 +000032/**
Dave Rodgmana360e192022-11-28 14:44:05 +000033 * Read the unsigned 16 bits integer from the given address, which need not
34 * be aligned.
35 *
36 * \param p pointer to 2 bytes of data
37 * \return Data at the given address
38 */
David Horstmann8b6068b2023-01-05 15:42:32 +000039inline uint16_t mbedtls_get_unaligned_uint16(const void *p)
Dave Rodgmana360e192022-11-28 14:44:05 +000040{
41 uint16_t r;
David Horstmann8b6068b2023-01-05 15:42:32 +000042 memcpy(&r, p, sizeof(r));
Dave Rodgmana360e192022-11-28 14:44:05 +000043 return r;
44}
45
46/**
47 * Write the unsigned 16 bits integer to the given address, which need not
48 * be aligned.
49 *
50 * \param p pointer to 2 bytes of data
51 * \param x data to write
52 */
David Horstmann8b6068b2023-01-05 15:42:32 +000053inline void mbedtls_put_unaligned_uint16(void *p, uint16_t x)
Dave Rodgmana360e192022-11-28 14:44:05 +000054{
David Horstmann8b6068b2023-01-05 15:42:32 +000055 memcpy(p, &x, sizeof(x));
Dave Rodgmana360e192022-11-28 14:44:05 +000056}
57
58/**
Dave Rodgman96d61d12022-11-24 19:33:22 +000059 * Read the unsigned 32 bits integer from the given address, which need not
60 * be aligned.
Dave Rodgmanfbc23222022-11-24 18:07:37 +000061 *
Dave Rodgman96d61d12022-11-24 19:33:22 +000062 * \param p pointer to 4 bytes of data
Dave Rodgman875d2382022-11-24 20:43:15 +000063 * \return Data at the given address
Dave Rodgmanfbc23222022-11-24 18:07:37 +000064 */
David Horstmann8b6068b2023-01-05 15:42:32 +000065inline uint32_t mbedtls_get_unaligned_uint32(const void *p)
Dave Rodgman96d61d12022-11-24 19:33:22 +000066{
67 uint32_t r;
David Horstmann8b6068b2023-01-05 15:42:32 +000068 memcpy(&r, p, sizeof(r));
Dave Rodgman96d61d12022-11-24 19:33:22 +000069 return r;
70}
Dave Rodgmanfbc23222022-11-24 18:07:37 +000071
Dave Rodgman96d61d12022-11-24 19:33:22 +000072/**
73 * Write the unsigned 32 bits integer to the given address, which need not
74 * be aligned.
75 *
76 * \param p pointer to 4 bytes of data
77 * \param x data to write
78 */
David Horstmann8b6068b2023-01-05 15:42:32 +000079inline void mbedtls_put_unaligned_uint32(void *p, uint32_t x)
Dave Rodgman96d61d12022-11-24 19:33:22 +000080{
David Horstmann8b6068b2023-01-05 15:42:32 +000081 memcpy(p, &x, sizeof(x));
Dave Rodgman96d61d12022-11-24 19:33:22 +000082}
Dave Rodgmanfbc23222022-11-24 18:07:37 +000083
Dave Rodgmana360e192022-11-28 14:44:05 +000084/**
85 * Read the unsigned 64 bits integer from the given address, which need not
86 * be aligned.
87 *
88 * \param p pointer to 8 bytes of data
89 * \return Data at the given address
90 */
David Horstmann8b6068b2023-01-05 15:42:32 +000091inline uint64_t mbedtls_get_unaligned_uint64(const void *p)
Dave Rodgmana360e192022-11-28 14:44:05 +000092{
93 uint64_t r;
David Horstmann8b6068b2023-01-05 15:42:32 +000094 memcpy(&r, p, sizeof(r));
Dave Rodgmana360e192022-11-28 14:44:05 +000095 return r;
96}
97
98/**
99 * Write the unsigned 64 bits integer to the given address, which need not
100 * be aligned.
101 *
102 * \param p pointer to 8 bytes of data
103 * \param x data to write
104 */
David Horstmann8b6068b2023-01-05 15:42:32 +0000105inline void mbedtls_put_unaligned_uint64(void *p, uint64_t x)
Dave Rodgmana360e192022-11-28 14:44:05 +0000106{
David Horstmann8b6068b2023-01-05 15:42:32 +0000107 memcpy(p, &x, sizeof(x));
Dave Rodgmana360e192022-11-28 14:44:05 +0000108}
109
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000110/** Byte Reading Macros
111 *
112 * Given a multi-byte integer \p x, MBEDTLS_BYTE_n retrieves the n-th
113 * byte from x, where byte 0 is the least significant byte.
114 */
David Horstmann8b6068b2023-01-05 15:42:32 +0000115#define MBEDTLS_BYTE_0(x) ((uint8_t) ((x) & 0xff))
116#define MBEDTLS_BYTE_1(x) ((uint8_t) (((x) >> 8) & 0xff))
117#define MBEDTLS_BYTE_2(x) ((uint8_t) (((x) >> 16) & 0xff))
118#define MBEDTLS_BYTE_3(x) ((uint8_t) (((x) >> 24) & 0xff))
119#define MBEDTLS_BYTE_4(x) ((uint8_t) (((x) >> 32) & 0xff))
120#define MBEDTLS_BYTE_5(x) ((uint8_t) (((x) >> 40) & 0xff))
121#define MBEDTLS_BYTE_6(x) ((uint8_t) (((x) >> 48) & 0xff))
122#define MBEDTLS_BYTE_7(x) ((uint8_t) (((x) >> 56) & 0xff))
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000123
Dave Rodgmanf7f1f742022-11-28 14:52:45 +0000124/*
125 * Detect GCC built-in byteswap routines
126 */
127#if defined(__GNUC__) && defined(__GNUC_PREREQ)
David Horstmann8b6068b2023-01-05 15:42:32 +0000128#if __GNUC_PREREQ(4, 8)
Dave Rodgmanf7f1f742022-11-28 14:52:45 +0000129#define MBEDTLS_BSWAP16 __builtin_bswap16
130#endif /* __GNUC_PREREQ(4,8) */
David Horstmann8b6068b2023-01-05 15:42:32 +0000131#if __GNUC_PREREQ(4, 3)
Dave Rodgmanf7f1f742022-11-28 14:52:45 +0000132#define MBEDTLS_BSWAP32 __builtin_bswap32
133#define MBEDTLS_BSWAP64 __builtin_bswap64
134#endif /* __GNUC_PREREQ(4,3) */
135#endif /* defined(__GNUC__) && defined(__GNUC_PREREQ) */
136
137/*
138 * Detect Clang built-in byteswap routines
139 */
140#if defined(__clang__) && defined(__has_builtin)
141#if __has_builtin(__builtin_bswap16)
142#define MBEDTLS_BSWAP16 __builtin_bswap16
143#endif /* __has_builtin(__builtin_bswap16) */
144#if __has_builtin(__builtin_bswap32)
145#define MBEDTLS_BSWAP32 __builtin_bswap32
146#endif /* __has_builtin(__builtin_bswap32) */
147#if __has_builtin(__builtin_bswap64)
148#define MBEDTLS_BSWAP64 __builtin_bswap64
149#endif /* __has_builtin(__builtin_bswap64) */
150#endif /* defined(__clang__) && defined(__has_builtin) */
151
152/*
153 * Detect MSVC built-in byteswap routines
154 */
155#if defined(_MSC_VER)
156#define MBEDTLS_BSWAP16 _byteswap_ushort
157#define MBEDTLS_BSWAP32 _byteswap_ulong
158#define MBEDTLS_BSWAP64 _byteswap_uint64
159#endif /* defined(_MSC_VER) */
160
Dave Rodgman2dae4b32022-11-30 12:07:36 +0000161/* Detect armcc built-in byteswap routine */
162#if defined(__ARMCC_VERSION) && (__ARMCC_VERSION >= 410000)
163#define MBEDTLS_BSWAP32 __rev
164#endif
165
Dave Rodgmanf7f1f742022-11-28 14:52:45 +0000166/*
167 * Where compiler built-ins are not present, fall back to C code that the
168 * compiler may be able to detect and transform into the relevant bswap or
169 * similar instruction.
170 */
171#if !defined(MBEDTLS_BSWAP16)
David Horstmann8b6068b2023-01-05 15:42:32 +0000172static inline uint16_t mbedtls_bswap16(uint16_t x)
173{
Dave Rodgman6298b242022-11-28 14:51:49 +0000174 return
David Horstmann8b6068b2023-01-05 15:42:32 +0000175 (x & 0x00ff) << 8 |
176 (x & 0xff00) >> 8;
Dave Rodgman6298b242022-11-28 14:51:49 +0000177}
178#define MBEDTLS_BSWAP16 mbedtls_bswap16
Dave Rodgmanf7f1f742022-11-28 14:52:45 +0000179#endif /* !defined(MBEDTLS_BSWAP16) */
Dave Rodgman6298b242022-11-28 14:51:49 +0000180
Dave Rodgmanf7f1f742022-11-28 14:52:45 +0000181#if !defined(MBEDTLS_BSWAP32)
David Horstmann8b6068b2023-01-05 15:42:32 +0000182static inline uint32_t mbedtls_bswap32(uint32_t x)
183{
Dave Rodgman6298b242022-11-28 14:51:49 +0000184 return
David Horstmann8b6068b2023-01-05 15:42:32 +0000185 (x & 0x000000ff) << 24 |
186 (x & 0x0000ff00) << 8 |
187 (x & 0x00ff0000) >> 8 |
188 (x & 0xff000000) >> 24;
Dave Rodgman6298b242022-11-28 14:51:49 +0000189}
190#define MBEDTLS_BSWAP32 mbedtls_bswap32
Dave Rodgmanf7f1f742022-11-28 14:52:45 +0000191#endif /* !defined(MBEDTLS_BSWAP32) */
Dave Rodgman6298b242022-11-28 14:51:49 +0000192
Dave Rodgmanf7f1f742022-11-28 14:52:45 +0000193#if !defined(MBEDTLS_BSWAP64)
David Horstmann8b6068b2023-01-05 15:42:32 +0000194static inline uint64_t mbedtls_bswap64(uint64_t x)
195{
Dave Rodgman6298b242022-11-28 14:51:49 +0000196 return
David Horstmann8b6068b2023-01-05 15:42:32 +0000197 (x & 0x00000000000000ff) << 56 |
198 (x & 0x000000000000ff00) << 40 |
199 (x & 0x0000000000ff0000) << 24 |
200 (x & 0x00000000ff000000) << 8 |
201 (x & 0x000000ff00000000) >> 8 |
202 (x & 0x0000ff0000000000) >> 24 |
203 (x & 0x00ff000000000000) >> 40 |
204 (x & 0xff00000000000000) >> 56;
Dave Rodgman6298b242022-11-28 14:51:49 +0000205}
206#define MBEDTLS_BSWAP64 mbedtls_bswap64
Dave Rodgmanf7f1f742022-11-28 14:52:45 +0000207#endif /* !defined(MBEDTLS_BSWAP64) */
Dave Rodgman6298b242022-11-28 14:51:49 +0000208
Dave Rodgmane5c42592022-11-28 14:47:46 +0000209#if !defined(__BYTE_ORDER__)
210static const uint16_t mbedtls_byte_order_detector = { 0x100 };
211#define MBEDTLS_IS_BIG_ENDIAN (*((unsigned char *) (&mbedtls_byte_order_detector)) == 0x01)
212#else
213#define MBEDTLS_IS_BIG_ENDIAN ((__BYTE_ORDER__) == (__ORDER_BIG_ENDIAN__))
214#endif /* !defined(__BYTE_ORDER__) */
215
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000216/**
217 * Get the unsigned 32 bits integer corresponding to four bytes in
218 * big-endian order (MSB first).
219 *
220 * \param data Base address of the memory to get the four bytes from.
221 * \param offset Offset from \p data of the first and most significant
222 * byte of the four bytes to build the 32 bits unsigned
223 * integer from.
224 */
David Horstmann8b6068b2023-01-05 15:42:32 +0000225#define MBEDTLS_GET_UINT32_BE(data, offset) \
226 ((MBEDTLS_IS_BIG_ENDIAN) \
Dave Rodgmana5110b02022-11-28 14:48:45 +0000227 ? mbedtls_get_unaligned_uint32((data) + (offset)) \
228 : MBEDTLS_BSWAP32(mbedtls_get_unaligned_uint32((data) + (offset))) \
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000229 )
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000230
231/**
232 * Put in memory a 32 bits unsigned integer in big-endian order.
233 *
234 * \param n 32 bits unsigned integer to put in memory.
235 * \param data Base address of the memory where to put the 32
236 * bits unsigned integer in.
237 * \param offset Offset from \p data where to put the most significant
238 * byte of the 32 bits unsigned integer \p n.
239 */
David Horstmann8b6068b2023-01-05 15:42:32 +0000240#define MBEDTLS_PUT_UINT32_BE(n, data, offset) \
241 { \
242 if (MBEDTLS_IS_BIG_ENDIAN) \
243 { \
244 mbedtls_put_unaligned_uint32((data) + (offset), (uint32_t) (n)); \
245 } \
246 else \
247 { \
248 mbedtls_put_unaligned_uint32((data) + (offset), MBEDTLS_BSWAP32((uint32_t) (n))); \
249 } \
250 }
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000251
252/**
253 * Get the unsigned 32 bits integer corresponding to four bytes in
254 * little-endian order (LSB first).
255 *
256 * \param data Base address of the memory to get the four bytes from.
257 * \param offset Offset from \p data of the first and least significant
258 * byte of the four bytes to build the 32 bits unsigned
259 * integer from.
260 */
David Horstmann8b6068b2023-01-05 15:42:32 +0000261#define MBEDTLS_GET_UINT32_LE(data, offset) \
262 ((MBEDTLS_IS_BIG_ENDIAN) \
Dave Rodgmana5110b02022-11-28 14:48:45 +0000263 ? MBEDTLS_BSWAP32(mbedtls_get_unaligned_uint32((data) + (offset))) \
264 : mbedtls_get_unaligned_uint32((data) + (offset)) \
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000265 )
Dave Rodgmana5110b02022-11-28 14:48:45 +0000266
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000267
268/**
269 * Put in memory a 32 bits unsigned integer in little-endian order.
270 *
271 * \param n 32 bits unsigned integer to put in memory.
272 * \param data Base address of the memory where to put the 32
273 * bits unsigned integer in.
274 * \param offset Offset from \p data where to put the least significant
275 * byte of the 32 bits unsigned integer \p n.
276 */
David Horstmann8b6068b2023-01-05 15:42:32 +0000277#define MBEDTLS_PUT_UINT32_LE(n, data, offset) \
278 { \
279 if (MBEDTLS_IS_BIG_ENDIAN) \
280 { \
281 mbedtls_put_unaligned_uint32((data) + (offset), MBEDTLS_BSWAP32((uint32_t) (n))); \
282 } \
283 else \
284 { \
285 mbedtls_put_unaligned_uint32((data) + (offset), ((uint32_t) (n))); \
286 } \
287 }
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000288
289/**
290 * Get the unsigned 16 bits integer corresponding to two bytes in
291 * little-endian order (LSB first).
292 *
293 * \param data Base address of the memory to get the two bytes from.
294 * \param offset Offset from \p data of the first and least significant
295 * byte of the two bytes to build the 16 bits unsigned
296 * integer from.
297 */
David Horstmann8b6068b2023-01-05 15:42:32 +0000298#define MBEDTLS_GET_UINT16_LE(data, offset) \
299 ((MBEDTLS_IS_BIG_ENDIAN) \
Dave Rodgmana5110b02022-11-28 14:48:45 +0000300 ? MBEDTLS_BSWAP16(mbedtls_get_unaligned_uint16((data) + (offset))) \
301 : mbedtls_get_unaligned_uint16((data) + (offset)) \
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000302 )
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000303
304/**
305 * Put in memory a 16 bits unsigned integer in little-endian order.
306 *
307 * \param n 16 bits unsigned integer to put in memory.
308 * \param data Base address of the memory where to put the 16
309 * bits unsigned integer in.
310 * \param offset Offset from \p data where to put the least significant
311 * byte of the 16 bits unsigned integer \p n.
312 */
David Horstmann8b6068b2023-01-05 15:42:32 +0000313#define MBEDTLS_PUT_UINT16_LE(n, data, offset) \
314 { \
315 if (MBEDTLS_IS_BIG_ENDIAN) \
316 { \
317 mbedtls_put_unaligned_uint16((data) + (offset), MBEDTLS_BSWAP16((uint16_t) (n))); \
318 } \
319 else \
320 { \
321 mbedtls_put_unaligned_uint16((data) + (offset), (uint16_t) (n)); \
322 } \
323 }
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000324
325/**
326 * Get the unsigned 16 bits integer corresponding to two bytes in
327 * big-endian order (MSB first).
328 *
329 * \param data Base address of the memory to get the two bytes from.
330 * \param offset Offset from \p data of the first and most significant
331 * byte of the two bytes to build the 16 bits unsigned
332 * integer from.
333 */
David Horstmann8b6068b2023-01-05 15:42:32 +0000334#define MBEDTLS_GET_UINT16_BE(data, offset) \
335 ((MBEDTLS_IS_BIG_ENDIAN) \
Dave Rodgmana5110b02022-11-28 14:48:45 +0000336 ? mbedtls_get_unaligned_uint16((data) + (offset)) \
337 : MBEDTLS_BSWAP16(mbedtls_get_unaligned_uint16((data) + (offset))) \
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000338 )
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000339
340/**
341 * Put in memory a 16 bits unsigned integer in big-endian order.
342 *
343 * \param n 16 bits unsigned integer to put in memory.
344 * \param data Base address of the memory where to put the 16
345 * bits unsigned integer in.
346 * \param offset Offset from \p data where to put the most significant
347 * byte of the 16 bits unsigned integer \p n.
348 */
David Horstmann8b6068b2023-01-05 15:42:32 +0000349#define MBEDTLS_PUT_UINT16_BE(n, data, offset) \
350 { \
351 if (MBEDTLS_IS_BIG_ENDIAN) \
352 { \
353 mbedtls_put_unaligned_uint16((data) + (offset), (uint16_t) (n)); \
354 } \
355 else \
356 { \
357 mbedtls_put_unaligned_uint16((data) + (offset), MBEDTLS_BSWAP16((uint16_t) (n))); \
358 } \
359 }
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000360
361/**
362 * Get the unsigned 24 bits integer corresponding to three bytes in
363 * big-endian order (MSB first).
364 *
365 * \param data Base address of the memory to get the three bytes from.
366 * \param offset Offset from \p data of the first and most significant
367 * byte of the three bytes to build the 24 bits unsigned
368 * integer from.
369 */
David Horstmann8b6068b2023-01-05 15:42:32 +0000370#define MBEDTLS_GET_UINT24_BE(data, offset) \
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000371 ( \
David Horstmann8b6068b2023-01-05 15:42:32 +0000372 ((uint32_t) (data)[(offset)] << 16) \
373 | ((uint32_t) (data)[(offset) + 1] << 8) \
374 | ((uint32_t) (data)[(offset) + 2]) \
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000375 )
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000376
377/**
378 * Put in memory a 24 bits unsigned integer in big-endian order.
379 *
380 * \param n 24 bits unsigned integer to put in memory.
381 * \param data Base address of the memory where to put the 24
382 * bits unsigned integer in.
383 * \param offset Offset from \p data where to put the most significant
384 * byte of the 24 bits unsigned integer \p n.
385 */
David Horstmann8b6068b2023-01-05 15:42:32 +0000386#define MBEDTLS_PUT_UINT24_BE(n, data, offset) \
387 { \
388 (data)[(offset)] = MBEDTLS_BYTE_2(n); \
389 (data)[(offset) + 1] = MBEDTLS_BYTE_1(n); \
390 (data)[(offset) + 2] = MBEDTLS_BYTE_0(n); \
391 }
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000392
393/**
394 * Get the unsigned 24 bits integer corresponding to three bytes in
395 * little-endian order (LSB first).
396 *
397 * \param data Base address of the memory to get the three bytes from.
398 * \param offset Offset from \p data of the first and least significant
399 * byte of the three bytes to build the 24 bits unsigned
400 * integer from.
401 */
David Horstmann8b6068b2023-01-05 15:42:32 +0000402#define MBEDTLS_GET_UINT24_LE(data, offset) \
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000403 ( \
David Horstmann8b6068b2023-01-05 15:42:32 +0000404 ((uint32_t) (data)[(offset)]) \
405 | ((uint32_t) (data)[(offset) + 1] << 8) \
406 | ((uint32_t) (data)[(offset) + 2] << 16) \
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000407 )
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000408
409/**
410 * Put in memory a 24 bits unsigned integer in little-endian order.
411 *
412 * \param n 24 bits unsigned integer to put in memory.
413 * \param data Base address of the memory where to put the 24
414 * bits unsigned integer in.
415 * \param offset Offset from \p data where to put the least significant
416 * byte of the 24 bits unsigned integer \p n.
417 */
David Horstmann8b6068b2023-01-05 15:42:32 +0000418#define MBEDTLS_PUT_UINT24_LE(n, data, offset) \
419 { \
420 (data)[(offset)] = MBEDTLS_BYTE_0(n); \
421 (data)[(offset) + 1] = MBEDTLS_BYTE_1(n); \
422 (data)[(offset) + 2] = MBEDTLS_BYTE_2(n); \
423 }
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000424
425/**
426 * Get the unsigned 64 bits integer corresponding to eight bytes in
427 * big-endian order (MSB first).
428 *
429 * \param data Base address of the memory to get the eight bytes from.
430 * \param offset Offset from \p data of the first and most significant
431 * byte of the eight bytes to build the 64 bits unsigned
432 * integer from.
433 */
David Horstmann8b6068b2023-01-05 15:42:32 +0000434#define MBEDTLS_GET_UINT64_BE(data, offset) \
435 ((MBEDTLS_IS_BIG_ENDIAN) \
Dave Rodgmana5110b02022-11-28 14:48:45 +0000436 ? mbedtls_get_unaligned_uint64((data) + (offset)) \
437 : MBEDTLS_BSWAP64(mbedtls_get_unaligned_uint64((data) + (offset))) \
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000438 )
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000439
440/**
441 * Put in memory a 64 bits unsigned integer in big-endian order.
442 *
443 * \param n 64 bits unsigned integer to put in memory.
444 * \param data Base address of the memory where to put the 64
445 * bits unsigned integer in.
446 * \param offset Offset from \p data where to put the most significant
447 * byte of the 64 bits unsigned integer \p n.
448 */
David Horstmann8b6068b2023-01-05 15:42:32 +0000449#define MBEDTLS_PUT_UINT64_BE(n, data, offset) \
450 { \
451 if (MBEDTLS_IS_BIG_ENDIAN) \
452 { \
453 mbedtls_put_unaligned_uint64((data) + (offset), (uint64_t) (n)); \
454 } \
455 else \
456 { \
457 mbedtls_put_unaligned_uint64((data) + (offset), MBEDTLS_BSWAP64((uint64_t) (n))); \
458 } \
459 }
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000460
461/**
462 * Get the unsigned 64 bits integer corresponding to eight bytes in
463 * little-endian order (LSB first).
464 *
465 * \param data Base address of the memory to get the eight bytes from.
466 * \param offset Offset from \p data of the first and least significant
467 * byte of the eight bytes to build the 64 bits unsigned
468 * integer from.
469 */
David Horstmann8b6068b2023-01-05 15:42:32 +0000470#define MBEDTLS_GET_UINT64_LE(data, offset) \
471 ((MBEDTLS_IS_BIG_ENDIAN) \
Dave Rodgmana5110b02022-11-28 14:48:45 +0000472 ? MBEDTLS_BSWAP64(mbedtls_get_unaligned_uint64((data) + (offset))) \
473 : mbedtls_get_unaligned_uint64((data) + (offset)) \
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000474 )
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000475
476/**
477 * Put in memory a 64 bits unsigned integer in little-endian order.
478 *
479 * \param n 64 bits unsigned integer to put in memory.
480 * \param data Base address of the memory where to put the 64
481 * bits unsigned integer in.
482 * \param offset Offset from \p data where to put the least significant
483 * byte of the 64 bits unsigned integer \p n.
484 */
David Horstmann8b6068b2023-01-05 15:42:32 +0000485#define MBEDTLS_PUT_UINT64_LE(n, data, offset) \
486 { \
487 if (MBEDTLS_IS_BIG_ENDIAN) \
488 { \
489 mbedtls_put_unaligned_uint64((data) + (offset), MBEDTLS_BSWAP64((uint64_t) (n))); \
490 } \
491 else \
492 { \
493 mbedtls_put_unaligned_uint64((data) + (offset), (uint64_t) (n)); \
494 } \
495 }
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000496
497#endif /* MBEDTLS_LIBRARY_ALIGNMENT_H */