blob: 7638f9438401690b68fdfa3bfc7657e8a2b6f503 [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 Rodgmanfbc23222022-11-24 18:07:37 +000028
Dave Rodgmana616afe2022-11-25 17:11:45 +000029#include "mbedtls/build_info.h"
Dave Rodgman8f6583d2022-11-25 09:16:41 +000030
Dave Rodgman96d61d12022-11-24 19:33:22 +000031/**
Dave Rodgmana360e192022-11-28 14:44:05 +000032 * Read the unsigned 16 bits integer from the given address, which need not
33 * be aligned.
34 *
35 * \param p pointer to 2 bytes of data
36 * \return Data at the given address
37 */
38inline uint16_t mbedtls_get_unaligned_uint16( const void *p )
39{
40 uint16_t r;
41 memcpy( &r, p, sizeof( r ) );
42 return r;
43}
44
45/**
46 * Write the unsigned 16 bits integer to the given address, which need not
47 * be aligned.
48 *
49 * \param p pointer to 2 bytes of data
50 * \param x data to write
51 */
52inline void mbedtls_put_unaligned_uint16( void *p, uint16_t x )
53{
54 memcpy( p, &x, sizeof( x ) );
55}
56
57/**
Dave Rodgman96d61d12022-11-24 19:33:22 +000058 * Read the unsigned 32 bits integer from the given address, which need not
59 * be aligned.
Dave Rodgmanfbc23222022-11-24 18:07:37 +000060 *
Dave Rodgman96d61d12022-11-24 19:33:22 +000061 * \param p pointer to 4 bytes of data
Dave Rodgman875d2382022-11-24 20:43:15 +000062 * \return Data at the given address
Dave Rodgmanfbc23222022-11-24 18:07:37 +000063 */
Dave Rodgman7a910a82022-11-24 21:17:40 +000064inline uint32_t mbedtls_get_unaligned_uint32( const void *p )
Dave Rodgman96d61d12022-11-24 19:33:22 +000065{
66 uint32_t r;
Dave Rodgman7a910a82022-11-24 21:17:40 +000067 memcpy( &r, p, sizeof( r ) );
Dave Rodgman96d61d12022-11-24 19:33:22 +000068 return r;
69}
Dave Rodgmanfbc23222022-11-24 18:07:37 +000070
Dave Rodgman96d61d12022-11-24 19:33:22 +000071/**
72 * Write the unsigned 32 bits integer to the given address, which need not
73 * be aligned.
74 *
75 * \param p pointer to 4 bytes of data
76 * \param x data to write
77 */
Dave Rodgman66433442022-11-24 20:07:39 +000078inline void mbedtls_put_unaligned_uint32( void *p, uint32_t x )
Dave Rodgman96d61d12022-11-24 19:33:22 +000079{
Dave Rodgman7a910a82022-11-24 21:17:40 +000080 memcpy( p, &x, sizeof( x ) );
Dave Rodgman96d61d12022-11-24 19:33:22 +000081}
Dave Rodgmanfbc23222022-11-24 18:07:37 +000082
Dave Rodgmana360e192022-11-28 14:44:05 +000083/**
84 * Read the unsigned 64 bits integer from the given address, which need not
85 * be aligned.
86 *
87 * \param p pointer to 8 bytes of data
88 * \return Data at the given address
89 */
90inline uint64_t mbedtls_get_unaligned_uint64( const void *p )
91{
92 uint64_t r;
93 memcpy( &r, p, sizeof( r ) );
94 return r;
95}
96
97/**
98 * Write the unsigned 64 bits integer to the given address, which need not
99 * be aligned.
100 *
101 * \param p pointer to 8 bytes of data
102 * \param x data to write
103 */
104inline void mbedtls_put_unaligned_uint64( void *p, uint64_t x )
105{
106 memcpy( p, &x, sizeof( x ) );
107}
108
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000109/** Byte Reading Macros
110 *
111 * Given a multi-byte integer \p x, MBEDTLS_BYTE_n retrieves the n-th
112 * byte from x, where byte 0 is the least significant byte.
113 */
114#define MBEDTLS_BYTE_0( x ) ( (uint8_t) ( ( x ) & 0xff ) )
115#define MBEDTLS_BYTE_1( x ) ( (uint8_t) ( ( ( x ) >> 8 ) & 0xff ) )
116#define MBEDTLS_BYTE_2( x ) ( (uint8_t) ( ( ( x ) >> 16 ) & 0xff ) )
117#define MBEDTLS_BYTE_3( x ) ( (uint8_t) ( ( ( x ) >> 24 ) & 0xff ) )
118#define MBEDTLS_BYTE_4( x ) ( (uint8_t) ( ( ( x ) >> 32 ) & 0xff ) )
119#define MBEDTLS_BYTE_5( x ) ( (uint8_t) ( ( ( x ) >> 40 ) & 0xff ) )
120#define MBEDTLS_BYTE_6( x ) ( (uint8_t) ( ( ( x ) >> 48 ) & 0xff ) )
121#define MBEDTLS_BYTE_7( x ) ( (uint8_t) ( ( ( x ) >> 56 ) & 0xff ) )
122
Dave Rodgman6298b242022-11-28 14:51:49 +0000123static inline uint16_t mbedtls_bswap16( uint16_t x ) {
124 return
125 ( x & 0x00ff ) << 8 |
126 ( x & 0xff00 ) >> 8;
127}
128#define MBEDTLS_BSWAP16 mbedtls_bswap16
129
130static inline uint32_t mbedtls_bswap32( uint32_t x ) {
131 return
132 ( x & 0x000000ff ) << 24 |
133 ( x & 0x0000ff00 ) << 8 |
134 ( x & 0x00ff0000 ) >> 8 |
135 ( x & 0xff000000 ) >> 24;
136}
137#define MBEDTLS_BSWAP32 mbedtls_bswap32
138
139static inline uint64_t mbedtls_bswap64( uint64_t x ) {
140 return
141 ( x & 0x00000000000000ff ) << 56 |
142 ( x & 0x000000000000ff00 ) << 40 |
143 ( x & 0x0000000000ff0000 ) << 24 |
144 ( x & 0x00000000ff000000 ) << 8 |
145 ( x & 0x000000ff00000000 ) >> 8 |
146 ( x & 0x0000ff0000000000 ) >> 24 |
147 ( x & 0x00ff000000000000 ) >> 40 |
148 ( x & 0xff00000000000000 ) >> 56;
149}
150#define MBEDTLS_BSWAP64 mbedtls_bswap64
151
Dave Rodgmane5c42592022-11-28 14:47:46 +0000152#if !defined(__BYTE_ORDER__)
153static const uint16_t mbedtls_byte_order_detector = { 0x100 };
154#define MBEDTLS_IS_BIG_ENDIAN (*((unsigned char *) (&mbedtls_byte_order_detector)) == 0x01)
155#else
156#define MBEDTLS_IS_BIG_ENDIAN ((__BYTE_ORDER__) == (__ORDER_BIG_ENDIAN__))
157#endif /* !defined(__BYTE_ORDER__) */
158
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000159/**
160 * Get the unsigned 32 bits integer corresponding to four bytes in
161 * big-endian order (MSB first).
162 *
163 * \param data Base address of the memory to get the four bytes from.
164 * \param offset Offset from \p data of the first and most significant
165 * byte of the four bytes to build the 32 bits unsigned
166 * integer from.
167 */
Dave Rodgmana5110b02022-11-28 14:48:45 +0000168#define MBEDTLS_GET_UINT32_BE( data, offset ) \
169 ( ( MBEDTLS_IS_BIG_ENDIAN ) \
170 ? mbedtls_get_unaligned_uint32((data) + (offset)) \
171 : MBEDTLS_BSWAP32(mbedtls_get_unaligned_uint32((data) + (offset))) \
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000172 )
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000173
174/**
175 * Put in memory a 32 bits unsigned integer in big-endian order.
176 *
177 * \param n 32 bits unsigned integer to put in memory.
178 * \param data Base address of the memory where to put the 32
179 * bits unsigned integer in.
180 * \param offset Offset from \p data where to put the most significant
181 * byte of the 32 bits unsigned integer \p n.
182 */
Dave Rodgmana5110b02022-11-28 14:48:45 +0000183#define MBEDTLS_PUT_UINT32_BE( n, data, offset ) \
184{ \
185 if ( MBEDTLS_IS_BIG_ENDIAN ) \
186 { \
187 mbedtls_put_unaligned_uint32((data) + (offset), (uint32_t)(n)); \
188 } \
189 else \
190 { \
191 mbedtls_put_unaligned_uint32((data) + (offset), MBEDTLS_BSWAP32((uint32_t)(n))); \
192 } \
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000193}
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000194
195/**
196 * Get the unsigned 32 bits integer corresponding to four bytes in
197 * little-endian order (LSB first).
198 *
199 * \param data Base address of the memory to get the four bytes from.
200 * \param offset Offset from \p data of the first and least significant
201 * byte of the four bytes to build the 32 bits unsigned
202 * integer from.
203 */
Dave Rodgmana5110b02022-11-28 14:48:45 +0000204#define MBEDTLS_GET_UINT32_LE( data, offset ) \
205 ( ( MBEDTLS_IS_BIG_ENDIAN ) \
206 ? MBEDTLS_BSWAP32(mbedtls_get_unaligned_uint32((data) + (offset))) \
207 : mbedtls_get_unaligned_uint32((data) + (offset)) \
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000208 )
Dave Rodgmana5110b02022-11-28 14:48:45 +0000209
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000210
211/**
212 * Put in memory a 32 bits unsigned integer in little-endian order.
213 *
214 * \param n 32 bits unsigned integer to put in memory.
215 * \param data Base address of the memory where to put the 32
216 * bits unsigned integer in.
217 * \param offset Offset from \p data where to put the least significant
218 * byte of the 32 bits unsigned integer \p n.
219 */
Dave Rodgmana5110b02022-11-28 14:48:45 +0000220#define MBEDTLS_PUT_UINT32_LE( n, data, offset ) \
221{ \
222 if ( MBEDTLS_IS_BIG_ENDIAN ) \
223 { \
224 mbedtls_put_unaligned_uint32((data) + (offset), MBEDTLS_BSWAP32((uint32_t)(n))); \
225 } \
226 else \
227 { \
228 mbedtls_put_unaligned_uint32((data) + (offset), ((uint32_t)(n))); \
229 } \
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000230}
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000231
232/**
233 * Get the unsigned 16 bits integer corresponding to two bytes in
234 * little-endian order (LSB first).
235 *
236 * \param data Base address of the memory to get the two bytes from.
237 * \param offset Offset from \p data of the first and least significant
238 * byte of the two bytes to build the 16 bits unsigned
239 * integer from.
240 */
Dave Rodgmana5110b02022-11-28 14:48:45 +0000241#define MBEDTLS_GET_UINT16_LE( data, offset ) \
242 ( ( MBEDTLS_IS_BIG_ENDIAN ) \
243 ? MBEDTLS_BSWAP16(mbedtls_get_unaligned_uint16((data) + (offset))) \
244 : mbedtls_get_unaligned_uint16((data) + (offset)) \
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000245 )
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000246
247/**
248 * Put in memory a 16 bits unsigned integer in little-endian order.
249 *
250 * \param n 16 bits unsigned integer to put in memory.
251 * \param data Base address of the memory where to put the 16
252 * bits unsigned integer in.
253 * \param offset Offset from \p data where to put the least significant
254 * byte of the 16 bits unsigned integer \p n.
255 */
Dave Rodgmana5110b02022-11-28 14:48:45 +0000256#define MBEDTLS_PUT_UINT16_LE( n, data, offset ) \
257{ \
258 if ( MBEDTLS_IS_BIG_ENDIAN ) \
259 { \
260 mbedtls_put_unaligned_uint16((data) + (offset), MBEDTLS_BSWAP16((uint16_t)(n))); \
261 } \
262 else \
263 { \
264 mbedtls_put_unaligned_uint16((data) + (offset), (uint16_t)(n)); \
265 } \
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000266}
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000267
268/**
269 * Get the unsigned 16 bits integer corresponding to two bytes in
270 * big-endian order (MSB first).
271 *
272 * \param data Base address of the memory to get the two bytes from.
273 * \param offset Offset from \p data of the first and most significant
274 * byte of the two bytes to build the 16 bits unsigned
275 * integer from.
276 */
Dave Rodgmana5110b02022-11-28 14:48:45 +0000277#define MBEDTLS_GET_UINT16_BE( data, offset ) \
278 ( ( MBEDTLS_IS_BIG_ENDIAN ) \
279 ? mbedtls_get_unaligned_uint16((data) + (offset)) \
280 : MBEDTLS_BSWAP16(mbedtls_get_unaligned_uint16((data) + (offset))) \
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000281 )
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000282
283/**
284 * Put in memory a 16 bits unsigned integer in big-endian order.
285 *
286 * \param n 16 bits unsigned integer to put in memory.
287 * \param data Base address of the memory where to put the 16
288 * bits unsigned integer in.
289 * \param offset Offset from \p data where to put the most significant
290 * byte of the 16 bits unsigned integer \p n.
291 */
Dave Rodgmana5110b02022-11-28 14:48:45 +0000292#define MBEDTLS_PUT_UINT16_BE( n, data, offset ) \
293{ \
294 if ( MBEDTLS_IS_BIG_ENDIAN ) \
295 { \
296 mbedtls_put_unaligned_uint16((data) + (offset), (uint16_t)(n)); \
297 } \
298 else \
299 { \
300 mbedtls_put_unaligned_uint16((data) + (offset), MBEDTLS_BSWAP16((uint16_t)(n))); \
301 } \
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000302}
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000303
304/**
305 * Get the unsigned 24 bits integer corresponding to three bytes in
306 * big-endian order (MSB first).
307 *
308 * \param data Base address of the memory to get the three bytes from.
309 * \param offset Offset from \p data of the first and most significant
310 * byte of the three bytes to build the 24 bits unsigned
311 * integer from.
312 */
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000313#define MBEDTLS_GET_UINT24_BE( data , offset ) \
314 ( \
315 ( (uint32_t) ( data )[( offset ) ] << 16 ) \
316 | ( (uint32_t) ( data )[( offset ) + 1] << 8 ) \
317 | ( (uint32_t) ( data )[( offset ) + 2] ) \
318 )
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000319
320/**
321 * Put in memory a 24 bits unsigned integer in big-endian order.
322 *
323 * \param n 24 bits unsigned integer to put in memory.
324 * \param data Base address of the memory where to put the 24
325 * bits unsigned integer in.
326 * \param offset Offset from \p data where to put the most significant
327 * byte of the 24 bits unsigned integer \p n.
328 */
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000329#define MBEDTLS_PUT_UINT24_BE( n, data, offset ) \
330{ \
331 ( data )[( offset ) ] = MBEDTLS_BYTE_2( n ); \
332 ( data )[( offset ) + 1] = MBEDTLS_BYTE_1( n ); \
333 ( data )[( offset ) + 2] = MBEDTLS_BYTE_0( n ); \
334}
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000335
336/**
337 * Get the unsigned 24 bits integer corresponding to three bytes in
338 * little-endian order (LSB first).
339 *
340 * \param data Base address of the memory to get the three bytes from.
341 * \param offset Offset from \p data of the first and least significant
342 * byte of the three bytes to build the 24 bits unsigned
343 * integer from.
344 */
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000345#define MBEDTLS_GET_UINT24_LE( data, offset ) \
346 ( \
347 ( (uint32_t) ( data )[( offset ) ] ) \
348 | ( (uint32_t) ( data )[( offset ) + 1] << 8 ) \
349 | ( (uint32_t) ( data )[( offset ) + 2] << 16 ) \
350 )
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000351
352/**
353 * Put in memory a 24 bits unsigned integer in little-endian order.
354 *
355 * \param n 24 bits unsigned integer to put in memory.
356 * \param data Base address of the memory where to put the 24
357 * bits unsigned integer in.
358 * \param offset Offset from \p data where to put the least significant
359 * byte of the 24 bits unsigned integer \p n.
360 */
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000361#define MBEDTLS_PUT_UINT24_LE( n, data, offset ) \
362{ \
363 ( data )[( offset ) ] = MBEDTLS_BYTE_0( n ); \
364 ( data )[( offset ) + 1] = MBEDTLS_BYTE_1( n ); \
365 ( data )[( offset ) + 2] = MBEDTLS_BYTE_2( n ); \
366}
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000367
368/**
369 * Get the unsigned 64 bits integer corresponding to eight bytes in
370 * big-endian order (MSB first).
371 *
372 * \param data Base address of the memory to get the eight bytes from.
373 * \param offset Offset from \p data of the first and most significant
374 * byte of the eight bytes to build the 64 bits unsigned
375 * integer from.
376 */
Dave Rodgmana5110b02022-11-28 14:48:45 +0000377#define MBEDTLS_GET_UINT64_BE( data, offset ) \
378 ( ( MBEDTLS_IS_BIG_ENDIAN ) \
379 ? mbedtls_get_unaligned_uint64((data) + (offset)) \
380 : MBEDTLS_BSWAP64(mbedtls_get_unaligned_uint64((data) + (offset))) \
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000381 )
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000382
383/**
384 * Put in memory a 64 bits unsigned integer in big-endian order.
385 *
386 * \param n 64 bits unsigned integer to put in memory.
387 * \param data Base address of the memory where to put the 64
388 * bits unsigned integer in.
389 * \param offset Offset from \p data where to put the most significant
390 * byte of the 64 bits unsigned integer \p n.
391 */
Dave Rodgmana5110b02022-11-28 14:48:45 +0000392#define MBEDTLS_PUT_UINT64_BE( n, data, offset ) \
393{ \
394 if ( MBEDTLS_IS_BIG_ENDIAN ) \
395 { \
396 mbedtls_put_unaligned_uint64((data) + (offset), (uint64_t)(n)); \
397 } \
398 else \
399 { \
400 mbedtls_put_unaligned_uint64((data) + (offset), MBEDTLS_BSWAP64((uint64_t)(n))); \
401 } \
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000402}
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000403
404/**
405 * Get the unsigned 64 bits integer corresponding to eight bytes in
406 * little-endian order (LSB first).
407 *
408 * \param data Base address of the memory to get the eight bytes from.
409 * \param offset Offset from \p data of the first and least significant
410 * byte of the eight bytes to build the 64 bits unsigned
411 * integer from.
412 */
Dave Rodgmana5110b02022-11-28 14:48:45 +0000413#define MBEDTLS_GET_UINT64_LE( data, offset ) \
414 ( ( MBEDTLS_IS_BIG_ENDIAN ) \
415 ? MBEDTLS_BSWAP64(mbedtls_get_unaligned_uint64((data) + (offset))) \
416 : mbedtls_get_unaligned_uint64((data) + (offset)) \
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000417 )
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000418
419/**
420 * Put in memory a 64 bits unsigned integer in little-endian order.
421 *
422 * \param n 64 bits unsigned integer to put in memory.
423 * \param data Base address of the memory where to put the 64
424 * bits unsigned integer in.
425 * \param offset Offset from \p data where to put the least significant
426 * byte of the 64 bits unsigned integer \p n.
427 */
Dave Rodgmana5110b02022-11-28 14:48:45 +0000428#define MBEDTLS_PUT_UINT64_LE( n, data, offset ) \
429{ \
430 if ( MBEDTLS_IS_BIG_ENDIAN ) \
431 { \
432 mbedtls_put_unaligned_uint64((data) + (offset), MBEDTLS_BSWAP64((uint64_t)(n))); \
433 } \
434 else \
435 { \
436 mbedtls_put_unaligned_uint64((data) + (offset), (uint64_t)(n)); \
437 } \
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000438}
Dave Rodgmanfbc23222022-11-24 18:07:37 +0000439
440#endif /* MBEDTLS_LIBRARY_ALIGNMENT_H */