scorpiox-bot-plugin-webhook
←alignment.hc
704 lines · 26 KBHEADBlameDownload
1/**
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 OR GPL-2.0-or-later
9 */
10
11#ifndef MBEDTLS_LIBRARY_ALIGNMENT_H
12#define MBEDTLS_LIBRARY_ALIGNMENT_H
13
14#include <stdint.h>
15#include <string.h>
16#include <stdlib.h>
17
18#if !defined(MBEDTLS_ALIGNMENT_DISABLE_EFFICENT_UNALIGNED_ACCESS) //no-check-names
19/*
20 * Define MBEDTLS_EFFICIENT_UNALIGNED_ACCESS for architectures where unaligned memory
21 * accesses are known to be efficient.
22 *
23 * All functions defined here will behave correctly regardless, but might be less
24 * efficient when this is not defined.
25 */
26#if defined(__ARM_FEATURE_UNALIGNED) \
27 || defined(MBEDTLS_ARCH_IS_X86) || defined(MBEDTLS_ARCH_IS_X64) \
28 || defined(MBEDTLS_PLATFORM_IS_WINDOWS_ON_ARM64)
29/*
30 * __ARM_FEATURE_UNALIGNED is defined where appropriate by armcc, gcc 7, clang 9
31 * (and later versions) for Arm v7 and later; all x86 platforms should have
32 * efficient unaligned access.
33 *
34 * https://learn.microsoft.com/en-us/cpp/build/arm64-windows-abi-conventions?view=msvc-170#alignment
35 * specifies that on Windows-on-Arm64, unaligned access is safe (except for uncached
36 * device memory).
37 */
38#define MBEDTLS_EFFICIENT_UNALIGNED_ACCESS
39#endif /* __ARM_FEATURE_UNALIGNED || MBEDTLS_ARCH_IS_X86 || MBEDTLS_ARCH_IS_X64 ||
40 * MBEDTLS_PLATFORM_IS_WINDOWS_ON_ARM64 */
41#endif /* MBEDTLS_ALIGNMENT_DISABLE_EFFICENT_UNALIGNED_ACCESS */ //no-check-names
42
43#if defined(__IAR_SYSTEMS_ICC__) && \
44 (defined(MBEDTLS_ARCH_IS_ARM64) || defined(MBEDTLS_ARCH_IS_ARM32) \
45 || defined(__ICCRX__) || defined(__ICCRL78__) || defined(__ICCRISCV__))
46#pragma language=save
47#pragma language=extended
48#define MBEDTLS_POP_IAR_LANGUAGE_PRAGMA
49/* IAR recommend this technique for accessing unaligned data in
50 * https://mypages.iar.com/s/article/Accessing-Unaligned-Data
51 * This results in a single load / store instruction (if unaligned access is supported).
52 * According to that document, this is only supported on certain architectures.
53 */
54#define UINT_UNALIGNED
55
56/* Some products, like Zephyr, defines __packed as a macro for attribute(packed) and
57 * that does not work with typedefs, so if __packed is defined, undef it for the
58 * typedefs and restore it afterwards.
59 */
60#ifdef __packed
61#pragma push_macro("__packed")
62#undef __packed
63#define MBEDTLS_IAR_PACKED_MACRO_USED
64#endif
65
66typedef uint16_t __packed mbedtls_uint16_unaligned_t;
67typedef uint32_t __packed mbedtls_uint32_unaligned_t;
68typedef uint64_t __packed mbedtls_uint64_unaligned_t;
69
70#ifdef MBEDTLS_IAR_PACKED_MACRO_USED
71#undef MBEDTLS_IAR_PACKED_MACRO_USED
72#pragma pop_macro("__packed")
73#endif
74
75#elif defined(MBEDTLS_COMPILER_IS_GCC) && (MBEDTLS_GCC_VERSION >= 40504) && \
76 ((MBEDTLS_GCC_VERSION < 60300) || (!defined(MBEDTLS_EFFICIENT_UNALIGNED_ACCESS)))
77/*
78 * gcc may generate a branch to memcpy for calls like `memcpy(dest, src, 4)` rather than
79 * generating some LDR or LDRB instructions (similar for stores).
80 *
81 * This is architecture dependent: x86-64 seems fine even with old gcc; 32-bit Arm
82 * is affected. To keep it simple, we enable for all architectures.
83 *
84 * For versions of gcc < 5.4.0 this issue always happens.
85 * For gcc < 6.3.0, this issue happens at -O0
86 * For all versions, this issue happens iff unaligned access is not supported.
87 *
88 * For gcc 4.x, this implementation will generate byte-by-byte loads even if unaligned access is
89 * supported, which is correct but not optimal.
90 *
91 * For performance (and code size, in some cases), we want to avoid the branch and just generate
92 * some inline load/store instructions since the access is small and constant-size.
93 *
94 * The manual states:
95 * "The packed attribute specifies that a variable or structure field should have the smallest
96 * possible alignment—one byte for a variable"
97 * https://gcc.gnu.org/onlinedocs/gcc-4.5.4/gcc/Variable-Attributes.html
98 *
99 * Previous implementations used __attribute__((__aligned__(1)), but had issues with a gcc bug:
100 * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94662
101 *
102 * Tested with several versions of GCC from 4.5.0 up to 13.2.0
103 * We don't enable for older than 4.5.0 as this has not been tested.
104 */
105 #define UINT_UNALIGNED_STRUCT
106typedef struct {
107 uint16_t x;
108} __attribute__((packed, may_alias)) mbedtls_uint16_unaligned_t;
109typedef struct {
110 uint32_t x;
111} __attribute__((packed, may_alias)) mbedtls_uint32_unaligned_t;
112typedef struct {
113 uint64_t x;
114} __attribute__((packed, may_alias)) mbedtls_uint64_unaligned_t;
115 #endif
116
117/*
118 * We try to force mbedtls_(get|put)_unaligned_uintXX to be always inline, because this results
119 * in code that is both smaller and faster. IAR and gcc both benefit from this when optimising
120 * for size.
121 */
122
123/**
124 * Read the unsigned 16 bits integer from the given address, which need not
125 * be aligned.
126 *
127 * \param p pointer to 2 bytes of data
128 * \return Data at the given address
129 */
130#if defined(__IAR_SYSTEMS_ICC__)
131#pragma inline = forced
132#elif defined(__GNUC__)
133__attribute__((always_inline))
134#endif
135static inline uint16_t mbedtls_get_unaligned_uint16(const void *p)
136{
137 uint16_t r;
138#if defined(UINT_UNALIGNED)
139 mbedtls_uint16_unaligned_t *p16 = (mbedtls_uint16_unaligned_t *) p;
140 r = *p16;
141#elif defined(UINT_UNALIGNED_STRUCT)
142 mbedtls_uint16_unaligned_t *p16 = (mbedtls_uint16_unaligned_t *) p;
143 r = p16->x;
144#else
145 memcpy(&r, p, sizeof(r));
146#endif
147 return r;
148}
149
150/**
151 * Write the unsigned 16 bits integer to the given address, which need not
152 * be aligned.
153 *
154 * \param p pointer to 2 bytes of data
155 * \param x data to write
156 */
157#if defined(__IAR_SYSTEMS_ICC__)
158#pragma inline = forced
159#elif defined(__GNUC__)
160__attribute__((always_inline))
161#endif
162static inline void mbedtls_put_unaligned_uint16(void *p, uint16_t x)
163{
164#if defined(UINT_UNALIGNED)
165 mbedtls_uint16_unaligned_t *p16 = (mbedtls_uint16_unaligned_t *) p;
166 *p16 = x;
167#elif defined(UINT_UNALIGNED_STRUCT)
168 mbedtls_uint16_unaligned_t *p16 = (mbedtls_uint16_unaligned_t *) p;
169 p16->x = x;
170#else
171 memcpy(p, &x, sizeof(x));
172#endif
173}
174
175/**
176 * Read the unsigned 32 bits integer from the given address, which need not
177 * be aligned.
178 *
179 * \param p pointer to 4 bytes of data
180 * \return Data at the given address
181 */
182#if defined(__IAR_SYSTEMS_ICC__)
183#pragma inline = forced
184#elif defined(__GNUC__)
185__attribute__((always_inline))
186#endif
187static inline uint32_t mbedtls_get_unaligned_uint32(const void *p)
188{
189 uint32_t r;
190#if defined(UINT_UNALIGNED)
191 mbedtls_uint32_unaligned_t *p32 = (mbedtls_uint32_unaligned_t *) p;
192 r = *p32;
193#elif defined(UINT_UNALIGNED_STRUCT)
194 mbedtls_uint32_unaligned_t *p32 = (mbedtls_uint32_unaligned_t *) p;
195 r = p32->x;
196#else
197 memcpy(&r, p, sizeof(r));
198#endif
199 return r;
200}
201
202/**
203 * Write the unsigned 32 bits integer to the given address, which need not
204 * be aligned.
205 *
206 * \param p pointer to 4 bytes of data
207 * \param x data to write
208 */
209#if defined(__IAR_SYSTEMS_ICC__)
210#pragma inline = forced
211#elif defined(__GNUC__)
212__attribute__((always_inline))
213#endif
214static inline void mbedtls_put_unaligned_uint32(void *p, uint32_t x)
215{
216#if defined(UINT_UNALIGNED)
217 mbedtls_uint32_unaligned_t *p32 = (mbedtls_uint32_unaligned_t *) p;
218 *p32 = x;
219#elif defined(UINT_UNALIGNED_STRUCT)
220 mbedtls_uint32_unaligned_t *p32 = (mbedtls_uint32_unaligned_t *) p;
221 p32->x = x;
222#else
223 memcpy(p, &x, sizeof(x));
224#endif
225}
226
227/**
228 * Read the unsigned 64 bits integer from the given address, which need not
229 * be aligned.
230 *
231 * \param p pointer to 8 bytes of data
232 * \return Data at the given address
233 */
234#if defined(__IAR_SYSTEMS_ICC__)
235#pragma inline = forced
236#elif defined(__GNUC__)
237__attribute__((always_inline))
238#endif
239static inline uint64_t mbedtls_get_unaligned_uint64(const void *p)
240{
241 uint64_t r;
242#if defined(UINT_UNALIGNED)
243 mbedtls_uint64_unaligned_t *p64 = (mbedtls_uint64_unaligned_t *) p;
244 r = *p64;
245#elif defined(UINT_UNALIGNED_STRUCT)
246 mbedtls_uint64_unaligned_t *p64 = (mbedtls_uint64_unaligned_t *) p;
247 r = p64->x;
248#else
249 memcpy(&r, p, sizeof(r));
250#endif
251 return r;
252}
253
254/**
255 * Write the unsigned 64 bits integer to the given address, which need not
256 * be aligned.
257 *
258 * \param p pointer to 8 bytes of data
259 * \param x data to write
260 */
261#if defined(__IAR_SYSTEMS_ICC__)
262#pragma inline = forced
263#elif defined(__GNUC__)
264__attribute__((always_inline))
265#endif
266static inline void mbedtls_put_unaligned_uint64(void *p, uint64_t x)
267{
268#if defined(UINT_UNALIGNED)
269 mbedtls_uint64_unaligned_t *p64 = (mbedtls_uint64_unaligned_t *) p;
270 *p64 = x;
271#elif defined(UINT_UNALIGNED_STRUCT)
272 mbedtls_uint64_unaligned_t *p64 = (mbedtls_uint64_unaligned_t *) p;
273 p64->x = x;
274#else
275 memcpy(p, &x, sizeof(x));
276#endif
277}
278
279#if defined(MBEDTLS_POP_IAR_LANGUAGE_PRAGMA)
280#pragma language=restore
281#endif
282
283/** Byte Reading Macros
284 *
285 * Given a multi-byte integer \p x, MBEDTLS_BYTE_n retrieves the n-th
286 * byte from x, where byte 0 is the least significant byte.
287 */
288#define MBEDTLS_BYTE_0(x) ((uint8_t) ((x) & 0xff))
289#define MBEDTLS_BYTE_1(x) ((uint8_t) (((x) >> 8) & 0xff))
290#define MBEDTLS_BYTE_2(x) ((uint8_t) (((x) >> 16) & 0xff))
291#define MBEDTLS_BYTE_3(x) ((uint8_t) (((x) >> 24) & 0xff))
292#define MBEDTLS_BYTE_4(x) ((uint8_t) (((x) >> 32) & 0xff))
293#define MBEDTLS_BYTE_5(x) ((uint8_t) (((x) >> 40) & 0xff))
294#define MBEDTLS_BYTE_6(x) ((uint8_t) (((x) >> 48) & 0xff))
295#define MBEDTLS_BYTE_7(x) ((uint8_t) (((x) >> 56) & 0xff))
296
297/*
298 * Detect GCC built-in byteswap routines
299 */
300#if defined(__GNUC__)
301#if MBEDTLS_GCC_VERSION >= 40800
302#define MBEDTLS_BSWAP16 __builtin_bswap16
303#endif
304#if MBEDTLS_GCC_VERSION >= 40300
305#define MBEDTLS_BSWAP32 __builtin_bswap32
306#define MBEDTLS_BSWAP64 __builtin_bswap64
307#endif
308#endif /* defined(__GNUC__) */
309
310/*
311 * Detect Clang built-in byteswap routines
312 */
313#if defined(__clang__) && defined(__has_builtin)
314#if __has_builtin(__builtin_bswap16) && !defined(MBEDTLS_BSWAP16)
315#define MBEDTLS_BSWAP16 __builtin_bswap16
316#endif /* __has_builtin(__builtin_bswap16) */
317#if __has_builtin(__builtin_bswap32) && !defined(MBEDTLS_BSWAP32)
318#define MBEDTLS_BSWAP32 __builtin_bswap32
319#endif /* __has_builtin(__builtin_bswap32) */
320#if __has_builtin(__builtin_bswap64) && !defined(MBEDTLS_BSWAP64)
321#define MBEDTLS_BSWAP64 __builtin_bswap64
322#endif /* __has_builtin(__builtin_bswap64) */
323#endif /* defined(__clang__) && defined(__has_builtin) */
324
325/*
326 * Detect MSVC built-in byteswap routines
327 */
328#if defined(_MSC_VER)
329#if !defined(MBEDTLS_BSWAP16)
330#define MBEDTLS_BSWAP16 _byteswap_ushort
331#endif
332#if !defined(MBEDTLS_BSWAP32)
333#define MBEDTLS_BSWAP32 _byteswap_ulong
334#endif
335#if !defined(MBEDTLS_BSWAP64)
336#define MBEDTLS_BSWAP64 _byteswap_uint64
337#endif
338#endif /* defined(_MSC_VER) */
339
340/* Detect armcc built-in byteswap routine */
341#if defined(__ARMCC_VERSION) && (__ARMCC_VERSION >= 410000) && !defined(MBEDTLS_BSWAP32)
342#if defined(__ARM_ACLE) /* ARM Compiler 6 - earlier versions don't need a header */
343#include <arm_acle.h>
344#endif
345#define MBEDTLS_BSWAP32 __rev
346#endif
347
348/* Detect IAR built-in byteswap routine */
349#if defined(__IAR_SYSTEMS_ICC__)
350#if defined(__ARM_ACLE)
351#include <arm_acle.h>
352#define MBEDTLS_BSWAP16(x) ((uint16_t) __rev16((uint32_t) (x)))
353#define MBEDTLS_BSWAP32 __rev
354#define MBEDTLS_BSWAP64 __revll
355#endif
356#endif
357
358/*
359 * Where compiler built-ins are not present, fall back to C code that the
360 * compiler may be able to detect and transform into the relevant bswap or
361 * similar instruction.
362 */
363#if !defined(MBEDTLS_BSWAP16)
364static inline uint16_t mbedtls_bswap16(uint16_t x)
365{
366 return
367 (x & 0x00ff) << 8 |
368 (x & 0xff00) >> 8;
369}
370#define MBEDTLS_BSWAP16 mbedtls_bswap16
371#endif /* !defined(MBEDTLS_BSWAP16) */
372
373#if !defined(MBEDTLS_BSWAP32)
374static inline uint32_t mbedtls_bswap32(uint32_t x)
375{
376 return
377 (x & 0x000000ff) << 24 |
378 (x & 0x0000ff00) << 8 |
379 (x & 0x00ff0000) >> 8 |
380 (x & 0xff000000) >> 24;
381}
382#define MBEDTLS_BSWAP32 mbedtls_bswap32
383#endif /* !defined(MBEDTLS_BSWAP32) */
384
385#if !defined(MBEDTLS_BSWAP64)
386static inline uint64_t mbedtls_bswap64(uint64_t x)
387{
388 return
389 (x & 0x00000000000000ffULL) << 56 |
390 (x & 0x000000000000ff00ULL) << 40 |
391 (x & 0x0000000000ff0000ULL) << 24 |
392 (x & 0x00000000ff000000ULL) << 8 |
393 (x & 0x000000ff00000000ULL) >> 8 |
394 (x & 0x0000ff0000000000ULL) >> 24 |
395 (x & 0x00ff000000000000ULL) >> 40 |
396 (x & 0xff00000000000000ULL) >> 56;
397}
398#define MBEDTLS_BSWAP64 mbedtls_bswap64
399#endif /* !defined(MBEDTLS_BSWAP64) */
400
401#if !defined(__BYTE_ORDER__)
402
403#if defined(__LITTLE_ENDIAN__)
404/* IAR defines __xxx_ENDIAN__, but not __BYTE_ORDER__ */
405#define MBEDTLS_IS_BIG_ENDIAN 0
406#elif defined(__BIG_ENDIAN__)
407#define MBEDTLS_IS_BIG_ENDIAN 1
408#else
409static const uint16_t mbedtls_byte_order_detector = { 0x100 };
410#define MBEDTLS_IS_BIG_ENDIAN (*((unsigned char *) (&mbedtls_byte_order_detector)) == 0x01)
411#endif
412
413#else
414
415#if (__BYTE_ORDER__) == (__ORDER_BIG_ENDIAN__)
416#define MBEDTLS_IS_BIG_ENDIAN 1
417#else
418#define MBEDTLS_IS_BIG_ENDIAN 0
419#endif
420
421#endif /* !defined(__BYTE_ORDER__) */
422
423/**
424 * Get the unsigned 32 bits integer corresponding to four bytes in
425 * big-endian order (MSB first).
426 *
427 * \param data Base address of the memory to get the four bytes from.
428 * \param offset Offset from \p data of the first and most significant
429 * byte of the four bytes to build the 32 bits unsigned
430 * integer from.
431 */
432#define MBEDTLS_GET_UINT32_BE(data, offset) \
433 ((MBEDTLS_IS_BIG_ENDIAN) \
434 ? mbedtls_get_unaligned_uint32((data) + (offset)) \
435 : MBEDTLS_BSWAP32(mbedtls_get_unaligned_uint32((data) + (offset))) \
436 )
437
438/**
439 * Put in memory a 32 bits unsigned integer in big-endian order.
440 *
441 * \param n 32 bits unsigned integer to put in memory.
442 * \param data Base address of the memory where to put the 32
443 * bits unsigned integer in.
444 * \param offset Offset from \p data where to put the most significant
445 * byte of the 32 bits unsigned integer \p n.
446 */
447#define MBEDTLS_PUT_UINT32_BE(n, data, offset) \
448 { \
449 if (MBEDTLS_IS_BIG_ENDIAN) \
450 { \
451 mbedtls_put_unaligned_uint32((data) + (offset), (uint32_t) (n)); \
452 } \
453 else \
454 { \
455 mbedtls_put_unaligned_uint32((data) + (offset), MBEDTLS_BSWAP32((uint32_t) (n))); \
456 } \
457 }
458
459/**
460 * Get the unsigned 32 bits integer corresponding to four bytes in
461 * little-endian order (LSB first).
462 *
463 * \param data Base address of the memory to get the four bytes from.
464 * \param offset Offset from \p data of the first and least significant
465 * byte of the four bytes to build the 32 bits unsigned
466 * integer from.
467 */
468#define MBEDTLS_GET_UINT32_LE(data, offset) \
469 ((MBEDTLS_IS_BIG_ENDIAN) \
470 ? MBEDTLS_BSWAP32(mbedtls_get_unaligned_uint32((data) + (offset))) \
471 : mbedtls_get_unaligned_uint32((data) + (offset)) \
472 )
473
474
475/**
476 * Put in memory a 32 bits unsigned integer in little-endian order.
477 *
478 * \param n 32 bits unsigned integer to put in memory.
479 * \param data Base address of the memory where to put the 32
480 * bits unsigned integer in.
481 * \param offset Offset from \p data where to put the least significant
482 * byte of the 32 bits unsigned integer \p n.
483 */
484#define MBEDTLS_PUT_UINT32_LE(n, data, offset) \
485 { \
486 if (MBEDTLS_IS_BIG_ENDIAN) \
487 { \
488 mbedtls_put_unaligned_uint32((data) + (offset), MBEDTLS_BSWAP32((uint32_t) (n))); \
489 } \
490 else \
491 { \
492 mbedtls_put_unaligned_uint32((data) + (offset), ((uint32_t) (n))); \
493 } \
494 }
495
496/**
497 * Get the unsigned 16 bits integer corresponding to two bytes in
498 * little-endian order (LSB first).
499 *
500 * \param data Base address of the memory to get the two bytes from.
501 * \param offset Offset from \p data of the first and least significant
502 * byte of the two bytes to build the 16 bits unsigned
503 * integer from.
504 */
505#define MBEDTLS_GET_UINT16_LE(data, offset) \
506 ((MBEDTLS_IS_BIG_ENDIAN) \
507 ? MBEDTLS_BSWAP16(mbedtls_get_unaligned_uint16((data) + (offset))) \
508 : mbedtls_get_unaligned_uint16((data) + (offset)) \
509 )
510
511/**
512 * Put in memory a 16 bits unsigned integer in little-endian order.
513 *
514 * \param n 16 bits unsigned integer to put in memory.
515 * \param data Base address of the memory where to put the 16
516 * bits unsigned integer in.
517 * \param offset Offset from \p data where to put the least significant
518 * byte of the 16 bits unsigned integer \p n.
519 */
520#define MBEDTLS_PUT_UINT16_LE(n, data, offset) \
521 { \
522 if (MBEDTLS_IS_BIG_ENDIAN) \
523 { \
524 mbedtls_put_unaligned_uint16((data) + (offset), MBEDTLS_BSWAP16((uint16_t) (n))); \
525 } \
526 else \
527 { \
528 mbedtls_put_unaligned_uint16((data) + (offset), (uint16_t) (n)); \
529 } \
530 }
531
532/**
533 * Get the unsigned 16 bits integer corresponding to two bytes in
534 * big-endian order (MSB first).
535 *
536 * \param data Base address of the memory to get the two bytes from.
537 * \param offset Offset from \p data of the first and most significant
538 * byte of the two bytes to build the 16 bits unsigned
539 * integer from.
540 */
541#define MBEDTLS_GET_UINT16_BE(data, offset) \
542 ((MBEDTLS_IS_BIG_ENDIAN) \
543 ? mbedtls_get_unaligned_uint16((data) + (offset)) \
544 : MBEDTLS_BSWAP16(mbedtls_get_unaligned_uint16((data) + (offset))) \
545 )
546
547/**
548 * Put in memory a 16 bits unsigned integer in big-endian order.
549 *
550 * \param n 16 bits unsigned integer to put in memory.
551 * \param data Base address of the memory where to put the 16
552 * bits unsigned integer in.
553 * \param offset Offset from \p data where to put the most significant
554 * byte of the 16 bits unsigned integer \p n.
555 */
556#define MBEDTLS_PUT_UINT16_BE(n, data, offset) \
557 { \
558 if (MBEDTLS_IS_BIG_ENDIAN) \
559 { \
560 mbedtls_put_unaligned_uint16((data) + (offset), (uint16_t) (n)); \
561 } \
562 else \
563 { \
564 mbedtls_put_unaligned_uint16((data) + (offset), MBEDTLS_BSWAP16((uint16_t) (n))); \
565 } \
566 }
567
568/**
569 * Get the unsigned 24 bits integer corresponding to three bytes in
570 * big-endian order (MSB 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 most significant
574 * byte of the three bytes to build the 24 bits unsigned
575 * integer from.
576 */
577#define MBEDTLS_GET_UINT24_BE(data, offset) \
578 ( \
579 ((uint32_t) (data)[(offset)] << 16) \
580 | ((uint32_t) (data)[(offset) + 1] << 8) \
581 | ((uint32_t) (data)[(offset) + 2]) \
582 )
583
584/**
585 * Put in memory a 24 bits unsigned integer in big-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 most significant
591 * byte of the 24 bits unsigned integer \p n.
592 */
593#define MBEDTLS_PUT_UINT24_BE(n, data, offset) \
594 { \
595 (data)[(offset)] = MBEDTLS_BYTE_2(n); \
596 (data)[(offset) + 1] = MBEDTLS_BYTE_1(n); \
597 (data)[(offset) + 2] = MBEDTLS_BYTE_0(n); \
598 }
599
600/**
601 * Get the unsigned 24 bits integer corresponding to three bytes in
602 * little-endian order (LSB first).
603 *
604 * \param data Base address of the memory to get the three bytes from.
605 * \param offset Offset from \p data of the first and least significant
606 * byte of the three bytes to build the 24 bits unsigned
607 * integer from.
608 */
609#define MBEDTLS_GET_UINT24_LE(data, offset) \
610 ( \
611 ((uint32_t) (data)[(offset)]) \
612 | ((uint32_t) (data)[(offset) + 1] << 8) \
613 | ((uint32_t) (data)[(offset) + 2] << 16) \
614 )
615
616/**
617 * Put in memory a 24 bits unsigned integer in little-endian order.
618 *
619 * \param n 24 bits unsigned integer to put in memory.
620 * \param data Base address of the memory where to put the 24
621 * bits unsigned integer in.
622 * \param offset Offset from \p data where to put the least significant
623 * byte of the 24 bits unsigned integer \p n.
624 */
625#define MBEDTLS_PUT_UINT24_LE(n, data, offset) \
626 { \
627 (data)[(offset)] = MBEDTLS_BYTE_0(n); \
628 (data)[(offset) + 1] = MBEDTLS_BYTE_1(n); \
629 (data)[(offset) + 2] = MBEDTLS_BYTE_2(n); \
630 }
631
632/**
633 * Get the unsigned 64 bits integer corresponding to eight bytes in
634 * big-endian order (MSB first).
635 *
636 * \param data Base address of the memory to get the eight bytes from.
637 * \param offset Offset from \p data of the first and most significant
638 * byte of the eight bytes to build the 64 bits unsigned
639 * integer from.
640 */
641#define MBEDTLS_GET_UINT64_BE(data, offset) \
642 ((MBEDTLS_IS_BIG_ENDIAN) \
643 ? mbedtls_get_unaligned_uint64((data) + (offset)) \
644 : MBEDTLS_BSWAP64(mbedtls_get_unaligned_uint64((data) + (offset))) \
645 )
646
647/**
648 * Put in memory a 64 bits unsigned integer in big-endian order.
649 *
650 * \param n 64 bits unsigned integer to put in memory.
651 * \param data Base address of the memory where to put the 64
652 * bits unsigned integer in.
653 * \param offset Offset from \p data where to put the most significant
654 * byte of the 64 bits unsigned integer \p n.
655 */
656#define MBEDTLS_PUT_UINT64_BE(n, data, offset) \
657 { \
658 if (MBEDTLS_IS_BIG_ENDIAN) \
659 { \
660 mbedtls_put_unaligned_uint64((data) + (offset), (uint64_t) (n)); \
661 } \
662 else \
663 { \
664 mbedtls_put_unaligned_uint64((data) + (offset), MBEDTLS_BSWAP64((uint64_t) (n))); \
665 } \
666 }
667
668/**
669 * Get the unsigned 64 bits integer corresponding to eight bytes in
670 * little-endian order (LSB first).
671 *
672 * \param data Base address of the memory to get the eight bytes from.
673 * \param offset Offset from \p data of the first and least significant
674 * byte of the eight bytes to build the 64 bits unsigned
675 * integer from.
676 */
677#define MBEDTLS_GET_UINT64_LE(data, offset) \
678 ((MBEDTLS_IS_BIG_ENDIAN) \
679 ? MBEDTLS_BSWAP64(mbedtls_get_unaligned_uint64((data) + (offset))) \
680 : mbedtls_get_unaligned_uint64((data) + (offset)) \
681 )
682
683/**
684 * Put in memory a 64 bits unsigned integer in little-endian order.
685 *
686 * \param n 64 bits unsigned integer to put in memory.
687 * \param data Base address of the memory where to put the 64
688 * bits unsigned integer in.
689 * \param offset Offset from \p data where to put the least significant
690 * byte of the 64 bits unsigned integer \p n.
691 */
692#define MBEDTLS_PUT_UINT64_LE(n, data, offset) \
693 { \
694 if (MBEDTLS_IS_BIG_ENDIAN) \
695 { \
696 mbedtls_put_unaligned_uint64((data) + (offset), MBEDTLS_BSWAP64((uint64_t) (n))); \
697 } \
698 else \
699 { \
700 mbedtls_put_unaligned_uint64((data) + (offset), (uint64_t) (n)); \
701 } \
702 }
703
704#endif /* MBEDTLS_LIBRARY_ALIGNMENT_H */