blob: aa4c430b9652008a5af4cc8e6835ebc9184ba8db [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 Rodgmanb9cd19b2022-12-30 21:32:03 +000032/*
33 * Define MBEDTLS_EFFICIENT_UNALIGNED_ACCESS for architectures where unaligned memory
34 * accesses are known to be safe and efficient.
35 */
36#if defined(__ARM_FEATURE_UNALIGNED) \
37 || defined(__i386__) || defined(__amd64__) || defined(__x86_64__)
38/*
39 * __ARM_FEATURE_UNALIGNED is defined where appropriate by armcc, gcc 7, clang 9
40 * (and later versions); all x86 platforms should have efficient unaligned access.
41 */
42#define MBEDTLS_EFFICIENT_UNALIGNED_ACCESS
43#endif
44
Dave Rodgman96d61d12022-11-24 19:33:22 +000045/**
Dave Rodgmana360e192022-11-28 14:44:05 +000046 * Read the unsigned 16 bits integer from the given address, which need not
47 * be aligned.
48 *
49 * \param p pointer to 2 bytes of data
50 * \return Data at the given address
51 */
Gilles Peskine449bd832023-01-11 14:50:10 +010052inline uint16_t mbedtls_get_unaligned_uint16(const void *p)
Dave Rodgmana360e192022-11-28 14:44:05 +000053{
54 uint16_t r;
Gilles Peskine449bd832023-01-11 14:50:10 +010055 memcpy(&r, p, sizeof(r));
Dave Rodgmana360e192022-11-28 14:44:05 +000056 return r;
57}
58
59/**
60 * Write the unsigned 16 bits integer to the given address, which need not
61 * be aligned.
62 *
63 * \param p pointer to 2 bytes of data
64 * \param x data to write
65 */
Gilles Peskine449bd832023-01-11 14:50:10 +010066inline void mbedtls_put_unaligned_uint16(void *p, uint16_t x)
Dave Rodgmana360e192022-11-28 14:44:05 +000067{
Gilles Peskine449bd832023-01-11 14:50:10 +010068 memcpy(p, &x, sizeof(x));
Dave Rodgmana360e192022-11-28 14:44:05 +000069}
70
71/**
Dave Rodgman96d61d12022-11-24 19:33:22 +000072 * Read the unsigned 32 bits integer from the given address, which need not
73 * be aligned.
Dave Rodgmanfbc23222022-11-24 18:07:37 +000074 *
Dave Rodgman96d61d12022-11-24 19:33:22 +000075 * \param p pointer to 4 bytes of data
Dave Rodgman875d2382022-11-24 20:43:15 +000076 * \return Data at the given address
Dave Rodgmanfbc23222022-11-24 18:07:37 +000077 */
Gilles Peskine449bd832023-01-11 14:50:10 +010078inline uint32_t mbedtls_get_unaligned_uint32(const void *p)
Dave Rodgman96d61d12022-11-24 19:33:22 +000079{
80 uint32_t r;
Gilles Peskine449bd832023-01-11 14:50:10 +010081 memcpy(&r, p, sizeof(r));
Dave Rodgman96d61d12022-11-24 19:33:22 +000082 return r;
83}
Dave Rodgmanfbc23222022-11-24 18:07:37 +000084
Dave Rodgman96d61d12022-11-24 19:33:22 +000085/**
86 * Write the unsigned 32 bits integer to the given address, which need not
87 * be aligned.
88 *
89 * \param p pointer to 4 bytes of data
90 * \param x data to write
91 */
Gilles Peskine449bd832023-01-11 14:50:10 +010092inline void mbedtls_put_unaligned_uint32(void *p, uint32_t x)
Dave Rodgman96d61d12022-11-24 19:33:22 +000093{
Gilles Peskine449bd832023-01-11 14:50:10 +010094 memcpy(p, &x, sizeof(x));
Dave Rodgman96d61d12022-11-24 19:33:22 +000095}
Dave Rodgmanfbc23222022-11-24 18:07:37 +000096
Dave Rodgmana360e192022-11-28 14:44:05 +000097/**
98 * Read the unsigned 64 bits integer from the given address, which need not
99 * be aligned.
100 *
101 * \param p pointer to 8 bytes of data
102 * \return Data at the given address
103 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100104inline uint64_t mbedtls_get_unaligned_uint64(const void *p)
Dave Rodgmana360e192022-11-28 14:44:05 +0000105{
106 uint64_t r;
Gilles Peskine449bd832023-01-11 14:50:10 +0100107 memcpy(&r, p, sizeof(r));
Dave Rodgmana360e192022-11-28 14:44:05 +0000108 return r;
109}
110
111/**
112 * Write the unsigned 64 bits integer to the given address, which need not
113 * be aligned.
114 *
115 * \param p pointer to 8 bytes of data
116 * \param x data to write
117 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100118inline void mbedtls_put_unaligned_uint64(void *p, uint64_t x)
Dave Rodgmana360e192022-11-28 14:44:05 +0000119{
Gilles Peskine449bd832023-01-11 14:50:10 +0100120 memcpy(p, &x, sizeof(x));
Dave Rodgmana360e192022-11-28 14:44:05 +0000121}
122
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000123/** Byte Reading Macros
124 *
125 * Given a multi-byte integer \p x, MBEDTLS_BYTE_n retrieves the n-th
126 * byte from x, where byte 0 is the least significant byte.
127 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100128#define MBEDTLS_BYTE_0(x) ((uint8_t) ((x) & 0xff))
129#define MBEDTLS_BYTE_1(x) ((uint8_t) (((x) >> 8) & 0xff))
130#define MBEDTLS_BYTE_2(x) ((uint8_t) (((x) >> 16) & 0xff))
131#define MBEDTLS_BYTE_3(x) ((uint8_t) (((x) >> 24) & 0xff))
132#define MBEDTLS_BYTE_4(x) ((uint8_t) (((x) >> 32) & 0xff))
133#define MBEDTLS_BYTE_5(x) ((uint8_t) (((x) >> 40) & 0xff))
134#define MBEDTLS_BYTE_6(x) ((uint8_t) (((x) >> 48) & 0xff))
135#define MBEDTLS_BYTE_7(x) ((uint8_t) (((x) >> 56) & 0xff))
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000136
Dave Rodgmanf7f1f742022-11-28 14:52:45 +0000137/*
138 * Detect GCC built-in byteswap routines
139 */
140#if defined(__GNUC__) && defined(__GNUC_PREREQ)
Gilles Peskine449bd832023-01-11 14:50:10 +0100141#if __GNUC_PREREQ(4, 8)
Dave Rodgmanf7f1f742022-11-28 14:52:45 +0000142#define MBEDTLS_BSWAP16 __builtin_bswap16
143#endif /* __GNUC_PREREQ(4,8) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100144#if __GNUC_PREREQ(4, 3)
Dave Rodgmanf7f1f742022-11-28 14:52:45 +0000145#define MBEDTLS_BSWAP32 __builtin_bswap32
146#define MBEDTLS_BSWAP64 __builtin_bswap64
147#endif /* __GNUC_PREREQ(4,3) */
148#endif /* defined(__GNUC__) && defined(__GNUC_PREREQ) */
149
150/*
151 * Detect Clang built-in byteswap routines
152 */
153#if defined(__clang__) && defined(__has_builtin)
154#if __has_builtin(__builtin_bswap16)
155#define MBEDTLS_BSWAP16 __builtin_bswap16
156#endif /* __has_builtin(__builtin_bswap16) */
157#if __has_builtin(__builtin_bswap32)
158#define MBEDTLS_BSWAP32 __builtin_bswap32
159#endif /* __has_builtin(__builtin_bswap32) */
160#if __has_builtin(__builtin_bswap64)
161#define MBEDTLS_BSWAP64 __builtin_bswap64
162#endif /* __has_builtin(__builtin_bswap64) */
163#endif /* defined(__clang__) && defined(__has_builtin) */
164
165/*
166 * Detect MSVC built-in byteswap routines
167 */
168#if defined(_MSC_VER)
169#define MBEDTLS_BSWAP16 _byteswap_ushort
170#define MBEDTLS_BSWAP32 _byteswap_ulong
171#define MBEDTLS_BSWAP64 _byteswap_uint64
172#endif /* defined(_MSC_VER) */
173
Dave Rodgman2dae4b32022-11-30 12:07:36 +0000174/* Detect armcc built-in byteswap routine */
175#if defined(__ARMCC_VERSION) && (__ARMCC_VERSION >= 410000)
176#define MBEDTLS_BSWAP32 __rev
177#endif
178
Dave Rodgmanf7f1f742022-11-28 14:52:45 +0000179/*
180 * Where compiler built-ins are not present, fall back to C code that the
181 * compiler may be able to detect and transform into the relevant bswap or
182 * similar instruction.
183 */
184#if !defined(MBEDTLS_BSWAP16)
Gilles Peskine449bd832023-01-11 14:50:10 +0100185static inline uint16_t mbedtls_bswap16(uint16_t x)
186{
Dave Rodgman6298b242022-11-28 14:51:49 +0000187 return
Gilles Peskine449bd832023-01-11 14:50:10 +0100188 (x & 0x00ff) << 8 |
189 (x & 0xff00) >> 8;
Dave Rodgman6298b242022-11-28 14:51:49 +0000190}
191#define MBEDTLS_BSWAP16 mbedtls_bswap16
Dave Rodgmanf7f1f742022-11-28 14:52:45 +0000192#endif /* !defined(MBEDTLS_BSWAP16) */
Dave Rodgman6298b242022-11-28 14:51:49 +0000193
Dave Rodgmanf7f1f742022-11-28 14:52:45 +0000194#if !defined(MBEDTLS_BSWAP32)
Gilles Peskine449bd832023-01-11 14:50:10 +0100195static inline uint32_t mbedtls_bswap32(uint32_t x)
196{
Dave Rodgman6298b242022-11-28 14:51:49 +0000197 return
Gilles Peskine449bd832023-01-11 14:50:10 +0100198 (x & 0x000000ff) << 24 |
199 (x & 0x0000ff00) << 8 |
200 (x & 0x00ff0000) >> 8 |
201 (x & 0xff000000) >> 24;
Dave Rodgman6298b242022-11-28 14:51:49 +0000202}
203#define MBEDTLS_BSWAP32 mbedtls_bswap32
Dave Rodgmanf7f1f742022-11-28 14:52:45 +0000204#endif /* !defined(MBEDTLS_BSWAP32) */
Dave Rodgman6298b242022-11-28 14:51:49 +0000205
Dave Rodgmanf7f1f742022-11-28 14:52:45 +0000206#if !defined(MBEDTLS_BSWAP64)
Gilles Peskine449bd832023-01-11 14:50:10 +0100207static inline uint64_t mbedtls_bswap64(uint64_t x)
208{
Dave Rodgman6298b242022-11-28 14:51:49 +0000209 return
Gilles Peskine449bd832023-01-11 14:50:10 +0100210 (x & 0x00000000000000ff) << 56 |
211 (x & 0x000000000000ff00) << 40 |
212 (x & 0x0000000000ff0000) << 24 |
213 (x & 0x00000000ff000000) << 8 |
214 (x & 0x000000ff00000000) >> 8 |
215 (x & 0x0000ff0000000000) >> 24 |
216 (x & 0x00ff000000000000) >> 40 |
217 (x & 0xff00000000000000) >> 56;
Dave Rodgman6298b242022-11-28 14:51:49 +0000218}
219#define MBEDTLS_BSWAP64 mbedtls_bswap64
Dave Rodgmanf7f1f742022-11-28 14:52:45 +0000220#endif /* !defined(MBEDTLS_BSWAP64) */
Dave Rodgman6298b242022-11-28 14:51:49 +0000221
Dave Rodgmane5c42592022-11-28 14:47:46 +0000222#if !defined(__BYTE_ORDER__)
223static const uint16_t mbedtls_byte_order_detector = { 0x100 };
224#define MBEDTLS_IS_BIG_ENDIAN (*((unsigned char *) (&mbedtls_byte_order_detector)) == 0x01)
225#else
226#define MBEDTLS_IS_BIG_ENDIAN ((__BYTE_ORDER__) == (__ORDER_BIG_ENDIAN__))
227#endif /* !defined(__BYTE_ORDER__) */
228
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000229/**
230 * Get the unsigned 32 bits integer corresponding to four bytes in
231 * big-endian order (MSB first).
232 *
233 * \param data Base address of the memory to get the four bytes from.
234 * \param offset Offset from \p data of the first and most significant
235 * byte of the four bytes to build the 32 bits unsigned
236 * integer from.
237 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100238#define MBEDTLS_GET_UINT32_BE(data, offset) \
239 ((MBEDTLS_IS_BIG_ENDIAN) \
Dave Rodgmana5110b02022-11-28 14:48:45 +0000240 ? mbedtls_get_unaligned_uint32((data) + (offset)) \
241 : MBEDTLS_BSWAP32(mbedtls_get_unaligned_uint32((data) + (offset))) \
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000242 )
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000243
244/**
245 * Put in memory a 32 bits unsigned integer in big-endian order.
246 *
247 * \param n 32 bits unsigned integer to put in memory.
248 * \param data Base address of the memory where to put the 32
249 * bits unsigned integer in.
250 * \param offset Offset from \p data where to put the most significant
251 * byte of the 32 bits unsigned integer \p n.
252 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100253#define MBEDTLS_PUT_UINT32_BE(n, data, offset) \
254 { \
255 if (MBEDTLS_IS_BIG_ENDIAN) \
256 { \
257 mbedtls_put_unaligned_uint32((data) + (offset), (uint32_t) (n)); \
258 } \
259 else \
260 { \
261 mbedtls_put_unaligned_uint32((data) + (offset), MBEDTLS_BSWAP32((uint32_t) (n))); \
262 } \
263 }
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000264
265/**
266 * Get the unsigned 32 bits integer corresponding to four bytes in
267 * little-endian order (LSB first).
268 *
269 * \param data Base address of the memory to get the four bytes from.
270 * \param offset Offset from \p data of the first and least significant
271 * byte of the four bytes to build the 32 bits unsigned
272 * integer from.
273 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100274#define MBEDTLS_GET_UINT32_LE(data, offset) \
275 ((MBEDTLS_IS_BIG_ENDIAN) \
Dave Rodgmana5110b02022-11-28 14:48:45 +0000276 ? MBEDTLS_BSWAP32(mbedtls_get_unaligned_uint32((data) + (offset))) \
277 : mbedtls_get_unaligned_uint32((data) + (offset)) \
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000278 )
Dave Rodgmana5110b02022-11-28 14:48:45 +0000279
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000280
281/**
282 * Put in memory a 32 bits unsigned integer in little-endian order.
283 *
284 * \param n 32 bits unsigned integer to put in memory.
285 * \param data Base address of the memory where to put the 32
286 * bits unsigned integer in.
287 * \param offset Offset from \p data where to put the least significant
288 * byte of the 32 bits unsigned integer \p n.
289 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100290#define MBEDTLS_PUT_UINT32_LE(n, data, offset) \
291 { \
292 if (MBEDTLS_IS_BIG_ENDIAN) \
293 { \
294 mbedtls_put_unaligned_uint32((data) + (offset), MBEDTLS_BSWAP32((uint32_t) (n))); \
295 } \
296 else \
297 { \
298 mbedtls_put_unaligned_uint32((data) + (offset), ((uint32_t) (n))); \
299 } \
300 }
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000301
302/**
303 * Get the unsigned 16 bits integer corresponding to two bytes in
304 * little-endian order (LSB first).
305 *
306 * \param data Base address of the memory to get the two bytes from.
307 * \param offset Offset from \p data of the first and least significant
308 * byte of the two bytes to build the 16 bits unsigned
309 * integer from.
310 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100311#define MBEDTLS_GET_UINT16_LE(data, offset) \
312 ((MBEDTLS_IS_BIG_ENDIAN) \
Dave Rodgmana5110b02022-11-28 14:48:45 +0000313 ? MBEDTLS_BSWAP16(mbedtls_get_unaligned_uint16((data) + (offset))) \
314 : mbedtls_get_unaligned_uint16((data) + (offset)) \
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000315 )
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000316
317/**
318 * Put in memory a 16 bits unsigned integer in little-endian order.
319 *
320 * \param n 16 bits unsigned integer to put in memory.
321 * \param data Base address of the memory where to put the 16
322 * bits unsigned integer in.
323 * \param offset Offset from \p data where to put the least significant
324 * byte of the 16 bits unsigned integer \p n.
325 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100326#define MBEDTLS_PUT_UINT16_LE(n, data, offset) \
327 { \
328 if (MBEDTLS_IS_BIG_ENDIAN) \
329 { \
330 mbedtls_put_unaligned_uint16((data) + (offset), MBEDTLS_BSWAP16((uint16_t) (n))); \
331 } \
332 else \
333 { \
334 mbedtls_put_unaligned_uint16((data) + (offset), (uint16_t) (n)); \
335 } \
336 }
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000337
338/**
339 * Get the unsigned 16 bits integer corresponding to two bytes in
340 * big-endian order (MSB first).
341 *
342 * \param data Base address of the memory to get the two bytes from.
343 * \param offset Offset from \p data of the first and most significant
344 * byte of the two bytes to build the 16 bits unsigned
345 * integer from.
346 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100347#define MBEDTLS_GET_UINT16_BE(data, offset) \
348 ((MBEDTLS_IS_BIG_ENDIAN) \
Dave Rodgmana5110b02022-11-28 14:48:45 +0000349 ? mbedtls_get_unaligned_uint16((data) + (offset)) \
350 : MBEDTLS_BSWAP16(mbedtls_get_unaligned_uint16((data) + (offset))) \
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000351 )
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000352
353/**
354 * Put in memory a 16 bits unsigned integer in big-endian order.
355 *
356 * \param n 16 bits unsigned integer to put in memory.
357 * \param data Base address of the memory where to put the 16
358 * bits unsigned integer in.
359 * \param offset Offset from \p data where to put the most significant
360 * byte of the 16 bits unsigned integer \p n.
361 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100362#define MBEDTLS_PUT_UINT16_BE(n, data, offset) \
363 { \
364 if (MBEDTLS_IS_BIG_ENDIAN) \
365 { \
366 mbedtls_put_unaligned_uint16((data) + (offset), (uint16_t) (n)); \
367 } \
368 else \
369 { \
370 mbedtls_put_unaligned_uint16((data) + (offset), MBEDTLS_BSWAP16((uint16_t) (n))); \
371 } \
372 }
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000373
374/**
375 * Get the unsigned 24 bits integer corresponding to three bytes in
376 * big-endian order (MSB first).
377 *
378 * \param data Base address of the memory to get the three bytes from.
379 * \param offset Offset from \p data of the first and most significant
380 * byte of the three bytes to build the 24 bits unsigned
381 * integer from.
382 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100383#define MBEDTLS_GET_UINT24_BE(data, offset) \
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000384 ( \
Gilles Peskine449bd832023-01-11 14:50:10 +0100385 ((uint32_t) (data)[(offset)] << 16) \
386 | ((uint32_t) (data)[(offset) + 1] << 8) \
387 | ((uint32_t) (data)[(offset) + 2]) \
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000388 )
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000389
390/**
391 * Put in memory a 24 bits unsigned integer in big-endian order.
392 *
393 * \param n 24 bits unsigned integer to put in memory.
394 * \param data Base address of the memory where to put the 24
395 * bits unsigned integer in.
396 * \param offset Offset from \p data where to put the most significant
397 * byte of the 24 bits unsigned integer \p n.
398 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100399#define MBEDTLS_PUT_UINT24_BE(n, data, offset) \
400 { \
401 (data)[(offset)] = MBEDTLS_BYTE_2(n); \
402 (data)[(offset) + 1] = MBEDTLS_BYTE_1(n); \
403 (data)[(offset) + 2] = MBEDTLS_BYTE_0(n); \
404 }
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000405
406/**
407 * Get the unsigned 24 bits integer corresponding to three bytes in
408 * little-endian order (LSB first).
409 *
410 * \param data Base address of the memory to get the three bytes from.
411 * \param offset Offset from \p data of the first and least significant
412 * byte of the three bytes to build the 24 bits unsigned
413 * integer from.
414 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100415#define MBEDTLS_GET_UINT24_LE(data, offset) \
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000416 ( \
Gilles Peskine449bd832023-01-11 14:50:10 +0100417 ((uint32_t) (data)[(offset)]) \
418 | ((uint32_t) (data)[(offset) + 1] << 8) \
419 | ((uint32_t) (data)[(offset) + 2] << 16) \
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000420 )
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000421
422/**
423 * Put in memory a 24 bits unsigned integer in little-endian order.
424 *
425 * \param n 24 bits unsigned integer to put in memory.
426 * \param data Base address of the memory where to put the 24
427 * bits unsigned integer in.
428 * \param offset Offset from \p data where to put the least significant
429 * byte of the 24 bits unsigned integer \p n.
430 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100431#define MBEDTLS_PUT_UINT24_LE(n, data, offset) \
432 { \
433 (data)[(offset)] = MBEDTLS_BYTE_0(n); \
434 (data)[(offset) + 1] = MBEDTLS_BYTE_1(n); \
435 (data)[(offset) + 2] = MBEDTLS_BYTE_2(n); \
436 }
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000437
438/**
439 * Get the unsigned 64 bits integer corresponding to eight bytes in
440 * big-endian order (MSB first).
441 *
442 * \param data Base address of the memory to get the eight bytes from.
443 * \param offset Offset from \p data of the first and most significant
444 * byte of the eight bytes to build the 64 bits unsigned
445 * integer from.
446 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100447#define MBEDTLS_GET_UINT64_BE(data, offset) \
448 ((MBEDTLS_IS_BIG_ENDIAN) \
Dave Rodgmana5110b02022-11-28 14:48:45 +0000449 ? mbedtls_get_unaligned_uint64((data) + (offset)) \
450 : MBEDTLS_BSWAP64(mbedtls_get_unaligned_uint64((data) + (offset))) \
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000451 )
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000452
453/**
454 * Put in memory a 64 bits unsigned integer in big-endian order.
455 *
456 * \param n 64 bits unsigned integer to put in memory.
457 * \param data Base address of the memory where to put the 64
458 * bits unsigned integer in.
459 * \param offset Offset from \p data where to put the most significant
460 * byte of the 64 bits unsigned integer \p n.
461 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100462#define MBEDTLS_PUT_UINT64_BE(n, data, offset) \
463 { \
464 if (MBEDTLS_IS_BIG_ENDIAN) \
465 { \
466 mbedtls_put_unaligned_uint64((data) + (offset), (uint64_t) (n)); \
467 } \
468 else \
469 { \
470 mbedtls_put_unaligned_uint64((data) + (offset), MBEDTLS_BSWAP64((uint64_t) (n))); \
471 } \
472 }
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000473
474/**
475 * Get the unsigned 64 bits integer corresponding to eight bytes in
476 * little-endian order (LSB first).
477 *
478 * \param data Base address of the memory to get the eight bytes from.
479 * \param offset Offset from \p data of the first and least significant
480 * byte of the eight bytes to build the 64 bits unsigned
481 * integer from.
482 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100483#define MBEDTLS_GET_UINT64_LE(data, offset) \
484 ((MBEDTLS_IS_BIG_ENDIAN) \
Dave Rodgmana5110b02022-11-28 14:48:45 +0000485 ? MBEDTLS_BSWAP64(mbedtls_get_unaligned_uint64((data) + (offset))) \
486 : mbedtls_get_unaligned_uint64((data) + (offset)) \
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000487 )
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000488
489/**
490 * Put in memory a 64 bits unsigned integer in little-endian order.
491 *
492 * \param n 64 bits unsigned integer to put in memory.
493 * \param data Base address of the memory where to put the 64
494 * bits unsigned integer in.
495 * \param offset Offset from \p data where to put the least significant
496 * byte of the 64 bits unsigned integer \p n.
497 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100498#define MBEDTLS_PUT_UINT64_LE(n, data, offset) \
499 { \
500 if (MBEDTLS_IS_BIG_ENDIAN) \
501 { \
502 mbedtls_put_unaligned_uint64((data) + (offset), MBEDTLS_BSWAP64((uint64_t) (n))); \
503 } \
504 else \
505 { \
506 mbedtls_put_unaligned_uint64((data) + (offset), (uint64_t) (n)); \
507 } \
508 }
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000509
510#endif /* MBEDTLS_LIBRARY_ALIGNMENT_H */