blob: 3539c9175efe2890cfed51277b11a980a04c8546 [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
123/**
124 * Get the unsigned 32 bits integer corresponding to four bytes in
125 * big-endian order (MSB first).
126 *
127 * \param data Base address of the memory to get the four bytes from.
128 * \param offset Offset from \p data of the first and most significant
129 * byte of the four bytes to build the 32 bits unsigned
130 * integer from.
131 */
132#ifndef MBEDTLS_GET_UINT32_BE
133#define MBEDTLS_GET_UINT32_BE( data , offset ) \
134 ( \
135 ( (uint32_t) ( data )[( offset ) ] << 24 ) \
136 | ( (uint32_t) ( data )[( offset ) + 1] << 16 ) \
137 | ( (uint32_t) ( data )[( offset ) + 2] << 8 ) \
138 | ( (uint32_t) ( data )[( offset ) + 3] ) \
139 )
140#endif
141
142/**
143 * Put in memory a 32 bits unsigned integer in big-endian order.
144 *
145 * \param n 32 bits unsigned integer to put in memory.
146 * \param data Base address of the memory where to put the 32
147 * bits unsigned integer in.
148 * \param offset Offset from \p data where to put the most significant
149 * byte of the 32 bits unsigned integer \p n.
150 */
151#ifndef MBEDTLS_PUT_UINT32_BE
152#define MBEDTLS_PUT_UINT32_BE( n, data, offset ) \
153{ \
154 ( data )[( offset ) ] = MBEDTLS_BYTE_3( n ); \
155 ( data )[( offset ) + 1] = MBEDTLS_BYTE_2( n ); \
156 ( data )[( offset ) + 2] = MBEDTLS_BYTE_1( n ); \
157 ( data )[( offset ) + 3] = MBEDTLS_BYTE_0( n ); \
158}
159#endif
160
161/**
162 * Get the unsigned 32 bits integer corresponding to four bytes in
163 * little-endian order (LSB first).
164 *
165 * \param data Base address of the memory to get the four bytes from.
166 * \param offset Offset from \p data of the first and least significant
167 * byte of the four bytes to build the 32 bits unsigned
168 * integer from.
169 */
170#ifndef MBEDTLS_GET_UINT32_LE
171#define MBEDTLS_GET_UINT32_LE( data, offset ) \
172 ( \
173 ( (uint32_t) ( data )[( offset ) ] ) \
174 | ( (uint32_t) ( data )[( offset ) + 1] << 8 ) \
175 | ( (uint32_t) ( data )[( offset ) + 2] << 16 ) \
176 | ( (uint32_t) ( data )[( offset ) + 3] << 24 ) \
177 )
178#endif
179
180/**
181 * Put in memory a 32 bits unsigned integer in little-endian order.
182 *
183 * \param n 32 bits unsigned integer to put in memory.
184 * \param data Base address of the memory where to put the 32
185 * bits unsigned integer in.
186 * \param offset Offset from \p data where to put the least significant
187 * byte of the 32 bits unsigned integer \p n.
188 */
189#ifndef MBEDTLS_PUT_UINT32_LE
190#define MBEDTLS_PUT_UINT32_LE( n, data, offset ) \
191{ \
192 ( data )[( offset ) ] = MBEDTLS_BYTE_0( n ); \
193 ( data )[( offset ) + 1] = MBEDTLS_BYTE_1( n ); \
194 ( data )[( offset ) + 2] = MBEDTLS_BYTE_2( n ); \
195 ( data )[( offset ) + 3] = MBEDTLS_BYTE_3( n ); \
196}
197#endif
198
199/**
200 * Get the unsigned 16 bits integer corresponding to two bytes in
201 * little-endian order (LSB first).
202 *
203 * \param data Base address of the memory to get the two bytes from.
204 * \param offset Offset from \p data of the first and least significant
205 * byte of the two bytes to build the 16 bits unsigned
206 * integer from.
207 */
208#ifndef MBEDTLS_GET_UINT16_LE
209#define MBEDTLS_GET_UINT16_LE( data, offset ) \
210 ( \
211 ( (uint16_t) ( data )[( offset ) ] ) \
212 | ( (uint16_t) ( data )[( offset ) + 1] << 8 ) \
213 )
214#endif
215
216/**
217 * Put in memory a 16 bits unsigned integer in little-endian order.
218 *
219 * \param n 16 bits unsigned integer to put in memory.
220 * \param data Base address of the memory where to put the 16
221 * bits unsigned integer in.
222 * \param offset Offset from \p data where to put the least significant
223 * byte of the 16 bits unsigned integer \p n.
224 */
225#ifndef MBEDTLS_PUT_UINT16_LE
226#define MBEDTLS_PUT_UINT16_LE( n, data, offset ) \
227{ \
228 ( data )[( offset ) ] = MBEDTLS_BYTE_0( n ); \
229 ( data )[( offset ) + 1] = MBEDTLS_BYTE_1( n ); \
230}
231#endif
232
233/**
234 * Get the unsigned 16 bits integer corresponding to two bytes in
235 * big-endian order (MSB first).
236 *
237 * \param data Base address of the memory to get the two bytes from.
238 * \param offset Offset from \p data of the first and most significant
239 * byte of the two bytes to build the 16 bits unsigned
240 * integer from.
241 */
242#ifndef MBEDTLS_GET_UINT16_BE
243#define MBEDTLS_GET_UINT16_BE( data, offset ) \
244 ( \
245 ( (uint16_t) ( data )[( offset ) ] << 8 ) \
246 | ( (uint16_t) ( data )[( offset ) + 1] ) \
247 )
248#endif
249
250/**
251 * Put in memory a 16 bits unsigned integer in big-endian order.
252 *
253 * \param n 16 bits unsigned integer to put in memory.
254 * \param data Base address of the memory where to put the 16
255 * bits unsigned integer in.
256 * \param offset Offset from \p data where to put the most significant
257 * byte of the 16 bits unsigned integer \p n.
258 */
259#ifndef MBEDTLS_PUT_UINT16_BE
260#define MBEDTLS_PUT_UINT16_BE( n, data, offset ) \
261{ \
262 ( data )[( offset ) ] = MBEDTLS_BYTE_1( n ); \
263 ( data )[( offset ) + 1] = MBEDTLS_BYTE_0( n ); \
264}
265#endif
266
267/**
268 * Get the unsigned 24 bits integer corresponding to three bytes in
269 * big-endian order (MSB first).
270 *
271 * \param data Base address of the memory to get the three bytes from.
272 * \param offset Offset from \p data of the first and most significant
273 * byte of the three bytes to build the 24 bits unsigned
274 * integer from.
275 */
276#ifndef MBEDTLS_GET_UINT24_BE
277#define MBEDTLS_GET_UINT24_BE( data , offset ) \
278 ( \
279 ( (uint32_t) ( data )[( offset ) ] << 16 ) \
280 | ( (uint32_t) ( data )[( offset ) + 1] << 8 ) \
281 | ( (uint32_t) ( data )[( offset ) + 2] ) \
282 )
283#endif
284
285/**
286 * Put in memory a 24 bits unsigned integer in big-endian order.
287 *
288 * \param n 24 bits unsigned integer to put in memory.
289 * \param data Base address of the memory where to put the 24
290 * bits unsigned integer in.
291 * \param offset Offset from \p data where to put the most significant
292 * byte of the 24 bits unsigned integer \p n.
293 */
294#ifndef MBEDTLS_PUT_UINT24_BE
295#define MBEDTLS_PUT_UINT24_BE( n, data, offset ) \
296{ \
297 ( data )[( offset ) ] = MBEDTLS_BYTE_2( n ); \
298 ( data )[( offset ) + 1] = MBEDTLS_BYTE_1( n ); \
299 ( data )[( offset ) + 2] = MBEDTLS_BYTE_0( n ); \
300}
301#endif
302
303/**
304 * Get the unsigned 24 bits integer corresponding to three bytes in
305 * little-endian order (LSB first).
306 *
307 * \param data Base address of the memory to get the three bytes from.
308 * \param offset Offset from \p data of the first and least significant
309 * byte of the three bytes to build the 24 bits unsigned
310 * integer from.
311 */
312#ifndef MBEDTLS_GET_UINT24_LE
313#define MBEDTLS_GET_UINT24_LE( data, offset ) \
314 ( \
315 ( (uint32_t) ( data )[( offset ) ] ) \
316 | ( (uint32_t) ( data )[( offset ) + 1] << 8 ) \
317 | ( (uint32_t) ( data )[( offset ) + 2] << 16 ) \
318 )
319#endif
320
321/**
322 * Put in memory a 24 bits unsigned integer in little-endian order.
323 *
324 * \param n 24 bits unsigned integer to put in memory.
325 * \param data Base address of the memory where to put the 24
326 * bits unsigned integer in.
327 * \param offset Offset from \p data where to put the least significant
328 * byte of the 24 bits unsigned integer \p n.
329 */
330#ifndef MBEDTLS_PUT_UINT24_LE
331#define MBEDTLS_PUT_UINT24_LE( n, data, offset ) \
332{ \
333 ( data )[( offset ) ] = MBEDTLS_BYTE_0( n ); \
334 ( data )[( offset ) + 1] = MBEDTLS_BYTE_1( n ); \
335 ( data )[( offset ) + 2] = MBEDTLS_BYTE_2( n ); \
336}
337#endif
338
339/**
340 * Get the unsigned 64 bits integer corresponding to eight bytes in
341 * big-endian order (MSB first).
342 *
343 * \param data Base address of the memory to get the eight bytes from.
344 * \param offset Offset from \p data of the first and most significant
345 * byte of the eight bytes to build the 64 bits unsigned
346 * integer from.
347 */
348#ifndef MBEDTLS_GET_UINT64_BE
349#define MBEDTLS_GET_UINT64_BE( data, offset ) \
350 ( \
351 ( (uint64_t) ( data )[( offset ) ] << 56 ) \
352 | ( (uint64_t) ( data )[( offset ) + 1] << 48 ) \
353 | ( (uint64_t) ( data )[( offset ) + 2] << 40 ) \
354 | ( (uint64_t) ( data )[( offset ) + 3] << 32 ) \
355 | ( (uint64_t) ( data )[( offset ) + 4] << 24 ) \
356 | ( (uint64_t) ( data )[( offset ) + 5] << 16 ) \
357 | ( (uint64_t) ( data )[( offset ) + 6] << 8 ) \
358 | ( (uint64_t) ( data )[( offset ) + 7] ) \
359 )
360#endif
361
362/**
363 * Put in memory a 64 bits unsigned integer in big-endian order.
364 *
365 * \param n 64 bits unsigned integer to put in memory.
366 * \param data Base address of the memory where to put the 64
367 * bits unsigned integer in.
368 * \param offset Offset from \p data where to put the most significant
369 * byte of the 64 bits unsigned integer \p n.
370 */
371#ifndef MBEDTLS_PUT_UINT64_BE
372#define MBEDTLS_PUT_UINT64_BE( n, data, offset ) \
373{ \
374 ( data )[( offset ) ] = MBEDTLS_BYTE_7( n ); \
375 ( data )[( offset ) + 1] = MBEDTLS_BYTE_6( n ); \
376 ( data )[( offset ) + 2] = MBEDTLS_BYTE_5( n ); \
377 ( data )[( offset ) + 3] = MBEDTLS_BYTE_4( n ); \
378 ( data )[( offset ) + 4] = MBEDTLS_BYTE_3( n ); \
379 ( data )[( offset ) + 5] = MBEDTLS_BYTE_2( n ); \
380 ( data )[( offset ) + 6] = MBEDTLS_BYTE_1( n ); \
381 ( data )[( offset ) + 7] = MBEDTLS_BYTE_0( n ); \
382}
383#endif
384
385/**
386 * Get the unsigned 64 bits integer corresponding to eight bytes in
387 * little-endian order (LSB first).
388 *
389 * \param data Base address of the memory to get the eight bytes from.
390 * \param offset Offset from \p data of the first and least significant
391 * byte of the eight bytes to build the 64 bits unsigned
392 * integer from.
393 */
394#ifndef MBEDTLS_GET_UINT64_LE
395#define MBEDTLS_GET_UINT64_LE( data, offset ) \
396 ( \
397 ( (uint64_t) ( data )[( offset ) + 7] << 56 ) \
398 | ( (uint64_t) ( data )[( offset ) + 6] << 48 ) \
399 | ( (uint64_t) ( data )[( offset ) + 5] << 40 ) \
400 | ( (uint64_t) ( data )[( offset ) + 4] << 32 ) \
401 | ( (uint64_t) ( data )[( offset ) + 3] << 24 ) \
402 | ( (uint64_t) ( data )[( offset ) + 2] << 16 ) \
403 | ( (uint64_t) ( data )[( offset ) + 1] << 8 ) \
404 | ( (uint64_t) ( data )[( offset ) ] ) \
405 )
406#endif
407
408/**
409 * Put in memory a 64 bits unsigned integer in little-endian order.
410 *
411 * \param n 64 bits unsigned integer to put in memory.
412 * \param data Base address of the memory where to put the 64
413 * bits unsigned integer in.
414 * \param offset Offset from \p data where to put the least significant
415 * byte of the 64 bits unsigned integer \p n.
416 */
417#ifndef MBEDTLS_PUT_UINT64_LE
418#define MBEDTLS_PUT_UINT64_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 ( data )[( offset ) + 3] = MBEDTLS_BYTE_3( n ); \
424 ( data )[( offset ) + 4] = MBEDTLS_BYTE_4( n ); \
425 ( data )[( offset ) + 5] = MBEDTLS_BYTE_5( n ); \
426 ( data )[( offset ) + 6] = MBEDTLS_BYTE_6( n ); \
427 ( data )[( offset ) + 7] = MBEDTLS_BYTE_7( n ); \
428}
429#endif
430
431#endif /* MBEDTLS_LIBRARY_ALIGNMENT_H */