blob: 986fdaa95c89527028238cfce9880763e70b7d8c [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 Rodgman8f6583d2022-11-25 09:16:41 +000029#if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \
30 !defined(inline) && !defined(__cplusplus)
31#define inline __inline
32#endif
33
Dave Rodgman96d61d12022-11-24 19:33:22 +000034/**
35 * Read the unsigned 32 bits integer from the given address, which need not
36 * be aligned.
Dave Rodgmanfbc23222022-11-24 18:07:37 +000037 *
Dave Rodgman96d61d12022-11-24 19:33:22 +000038 * \param p pointer to 4 bytes of data
Dave Rodgman875d2382022-11-24 20:43:15 +000039 * \return Data at the given address
Dave Rodgmanfbc23222022-11-24 18:07:37 +000040 */
Dave Rodgman7a910a82022-11-24 21:17:40 +000041inline uint32_t mbedtls_get_unaligned_uint32( const void *p )
Dave Rodgman96d61d12022-11-24 19:33:22 +000042{
43 uint32_t r;
Dave Rodgman7a910a82022-11-24 21:17:40 +000044 memcpy( &r, p, sizeof( r ) );
Dave Rodgman96d61d12022-11-24 19:33:22 +000045 return r;
46}
Dave Rodgmanfbc23222022-11-24 18:07:37 +000047
Dave Rodgman96d61d12022-11-24 19:33:22 +000048/**
49 * Write the unsigned 32 bits integer to the given address, which need not
50 * be aligned.
51 *
52 * \param p pointer to 4 bytes of data
53 * \param x data to write
54 */
Dave Rodgman66433442022-11-24 20:07:39 +000055inline void mbedtls_put_unaligned_uint32( void *p, uint32_t x )
Dave Rodgman96d61d12022-11-24 19:33:22 +000056{
Dave Rodgman7a910a82022-11-24 21:17:40 +000057 memcpy( p, &x, sizeof( x ) );
Dave Rodgman96d61d12022-11-24 19:33:22 +000058}
Dave Rodgmanfbc23222022-11-24 18:07:37 +000059
60/** Byte Reading Macros
61 *
62 * Given a multi-byte integer \p x, MBEDTLS_BYTE_n retrieves the n-th
63 * byte from x, where byte 0 is the least significant byte.
64 */
65#define MBEDTLS_BYTE_0( x ) ( (uint8_t) ( ( x ) & 0xff ) )
66#define MBEDTLS_BYTE_1( x ) ( (uint8_t) ( ( ( x ) >> 8 ) & 0xff ) )
67#define MBEDTLS_BYTE_2( x ) ( (uint8_t) ( ( ( x ) >> 16 ) & 0xff ) )
68#define MBEDTLS_BYTE_3( x ) ( (uint8_t) ( ( ( x ) >> 24 ) & 0xff ) )
69#define MBEDTLS_BYTE_4( x ) ( (uint8_t) ( ( ( x ) >> 32 ) & 0xff ) )
70#define MBEDTLS_BYTE_5( x ) ( (uint8_t) ( ( ( x ) >> 40 ) & 0xff ) )
71#define MBEDTLS_BYTE_6( x ) ( (uint8_t) ( ( ( x ) >> 48 ) & 0xff ) )
72#define MBEDTLS_BYTE_7( x ) ( (uint8_t) ( ( ( x ) >> 56 ) & 0xff ) )
73
74/**
75 * Get the unsigned 32 bits integer corresponding to four bytes in
76 * big-endian order (MSB first).
77 *
78 * \param data Base address of the memory to get the four bytes from.
79 * \param offset Offset from \p data of the first and most significant
80 * byte of the four bytes to build the 32 bits unsigned
81 * integer from.
82 */
83#ifndef MBEDTLS_GET_UINT32_BE
84#define MBEDTLS_GET_UINT32_BE( data , offset ) \
85 ( \
86 ( (uint32_t) ( data )[( offset ) ] << 24 ) \
87 | ( (uint32_t) ( data )[( offset ) + 1] << 16 ) \
88 | ( (uint32_t) ( data )[( offset ) + 2] << 8 ) \
89 | ( (uint32_t) ( data )[( offset ) + 3] ) \
90 )
91#endif
92
93/**
94 * Put in memory a 32 bits unsigned integer in big-endian order.
95 *
96 * \param n 32 bits unsigned integer to put in memory.
97 * \param data Base address of the memory where to put the 32
98 * bits unsigned integer in.
99 * \param offset Offset from \p data where to put the most significant
100 * byte of the 32 bits unsigned integer \p n.
101 */
102#ifndef MBEDTLS_PUT_UINT32_BE
103#define MBEDTLS_PUT_UINT32_BE( n, data, offset ) \
104{ \
105 ( data )[( offset ) ] = MBEDTLS_BYTE_3( n ); \
106 ( data )[( offset ) + 1] = MBEDTLS_BYTE_2( n ); \
107 ( data )[( offset ) + 2] = MBEDTLS_BYTE_1( n ); \
108 ( data )[( offset ) + 3] = MBEDTLS_BYTE_0( n ); \
109}
110#endif
111
112/**
113 * Get the unsigned 32 bits integer corresponding to four bytes in
114 * little-endian order (LSB first).
115 *
116 * \param data Base address of the memory to get the four bytes from.
117 * \param offset Offset from \p data of the first and least significant
118 * byte of the four bytes to build the 32 bits unsigned
119 * integer from.
120 */
121#ifndef MBEDTLS_GET_UINT32_LE
122#define MBEDTLS_GET_UINT32_LE( data, offset ) \
123 ( \
124 ( (uint32_t) ( data )[( offset ) ] ) \
125 | ( (uint32_t) ( data )[( offset ) + 1] << 8 ) \
126 | ( (uint32_t) ( data )[( offset ) + 2] << 16 ) \
127 | ( (uint32_t) ( data )[( offset ) + 3] << 24 ) \
128 )
129#endif
130
131/**
132 * Put in memory a 32 bits unsigned integer in little-endian order.
133 *
134 * \param n 32 bits unsigned integer to put in memory.
135 * \param data Base address of the memory where to put the 32
136 * bits unsigned integer in.
137 * \param offset Offset from \p data where to put the least significant
138 * byte of the 32 bits unsigned integer \p n.
139 */
140#ifndef MBEDTLS_PUT_UINT32_LE
141#define MBEDTLS_PUT_UINT32_LE( n, data, offset ) \
142{ \
143 ( data )[( offset ) ] = MBEDTLS_BYTE_0( n ); \
144 ( data )[( offset ) + 1] = MBEDTLS_BYTE_1( n ); \
145 ( data )[( offset ) + 2] = MBEDTLS_BYTE_2( n ); \
146 ( data )[( offset ) + 3] = MBEDTLS_BYTE_3( n ); \
147}
148#endif
149
150/**
151 * Get the unsigned 16 bits integer corresponding to two bytes in
152 * little-endian order (LSB first).
153 *
154 * \param data Base address of the memory to get the two bytes from.
155 * \param offset Offset from \p data of the first and least significant
156 * byte of the two bytes to build the 16 bits unsigned
157 * integer from.
158 */
159#ifndef MBEDTLS_GET_UINT16_LE
160#define MBEDTLS_GET_UINT16_LE( data, offset ) \
161 ( \
162 ( (uint16_t) ( data )[( offset ) ] ) \
163 | ( (uint16_t) ( data )[( offset ) + 1] << 8 ) \
164 )
165#endif
166
167/**
168 * Put in memory a 16 bits unsigned integer in little-endian order.
169 *
170 * \param n 16 bits unsigned integer to put in memory.
171 * \param data Base address of the memory where to put the 16
172 * bits unsigned integer in.
173 * \param offset Offset from \p data where to put the least significant
174 * byte of the 16 bits unsigned integer \p n.
175 */
176#ifndef MBEDTLS_PUT_UINT16_LE
177#define MBEDTLS_PUT_UINT16_LE( n, data, offset ) \
178{ \
179 ( data )[( offset ) ] = MBEDTLS_BYTE_0( n ); \
180 ( data )[( offset ) + 1] = MBEDTLS_BYTE_1( n ); \
181}
182#endif
183
184/**
185 * Get the unsigned 16 bits integer corresponding to two bytes in
186 * big-endian order (MSB first).
187 *
188 * \param data Base address of the memory to get the two bytes from.
189 * \param offset Offset from \p data of the first and most significant
190 * byte of the two bytes to build the 16 bits unsigned
191 * integer from.
192 */
193#ifndef MBEDTLS_GET_UINT16_BE
194#define MBEDTLS_GET_UINT16_BE( data, offset ) \
195 ( \
196 ( (uint16_t) ( data )[( offset ) ] << 8 ) \
197 | ( (uint16_t) ( data )[( offset ) + 1] ) \
198 )
199#endif
200
201/**
202 * Put in memory a 16 bits unsigned integer in big-endian order.
203 *
204 * \param n 16 bits unsigned integer to put in memory.
205 * \param data Base address of the memory where to put the 16
206 * bits unsigned integer in.
207 * \param offset Offset from \p data where to put the most significant
208 * byte of the 16 bits unsigned integer \p n.
209 */
210#ifndef MBEDTLS_PUT_UINT16_BE
211#define MBEDTLS_PUT_UINT16_BE( n, data, offset ) \
212{ \
213 ( data )[( offset ) ] = MBEDTLS_BYTE_1( n ); \
214 ( data )[( offset ) + 1] = MBEDTLS_BYTE_0( n ); \
215}
216#endif
217
218/**
219 * Get the unsigned 24 bits integer corresponding to three bytes in
220 * big-endian order (MSB first).
221 *
222 * \param data Base address of the memory to get the three bytes from.
223 * \param offset Offset from \p data of the first and most significant
224 * byte of the three bytes to build the 24 bits unsigned
225 * integer from.
226 */
227#ifndef MBEDTLS_GET_UINT24_BE
228#define MBEDTLS_GET_UINT24_BE( data , offset ) \
229 ( \
230 ( (uint32_t) ( data )[( offset ) ] << 16 ) \
231 | ( (uint32_t) ( data )[( offset ) + 1] << 8 ) \
232 | ( (uint32_t) ( data )[( offset ) + 2] ) \
233 )
234#endif
235
236/**
237 * Put in memory a 24 bits unsigned integer in big-endian order.
238 *
239 * \param n 24 bits unsigned integer to put in memory.
240 * \param data Base address of the memory where to put the 24
241 * bits unsigned integer in.
242 * \param offset Offset from \p data where to put the most significant
243 * byte of the 24 bits unsigned integer \p n.
244 */
245#ifndef MBEDTLS_PUT_UINT24_BE
246#define MBEDTLS_PUT_UINT24_BE( n, data, offset ) \
247{ \
248 ( data )[( offset ) ] = MBEDTLS_BYTE_2( n ); \
249 ( data )[( offset ) + 1] = MBEDTLS_BYTE_1( n ); \
250 ( data )[( offset ) + 2] = MBEDTLS_BYTE_0( n ); \
251}
252#endif
253
254/**
255 * Get the unsigned 24 bits integer corresponding to three bytes in
256 * little-endian order (LSB first).
257 *
258 * \param data Base address of the memory to get the three bytes from.
259 * \param offset Offset from \p data of the first and least significant
260 * byte of the three bytes to build the 24 bits unsigned
261 * integer from.
262 */
263#ifndef MBEDTLS_GET_UINT24_LE
264#define MBEDTLS_GET_UINT24_LE( data, offset ) \
265 ( \
266 ( (uint32_t) ( data )[( offset ) ] ) \
267 | ( (uint32_t) ( data )[( offset ) + 1] << 8 ) \
268 | ( (uint32_t) ( data )[( offset ) + 2] << 16 ) \
269 )
270#endif
271
272/**
273 * Put in memory a 24 bits unsigned integer in little-endian order.
274 *
275 * \param n 24 bits unsigned integer to put in memory.
276 * \param data Base address of the memory where to put the 24
277 * bits unsigned integer in.
278 * \param offset Offset from \p data where to put the least significant
279 * byte of the 24 bits unsigned integer \p n.
280 */
281#ifndef MBEDTLS_PUT_UINT24_LE
282#define MBEDTLS_PUT_UINT24_LE( n, data, offset ) \
283{ \
284 ( data )[( offset ) ] = MBEDTLS_BYTE_0( n ); \
285 ( data )[( offset ) + 1] = MBEDTLS_BYTE_1( n ); \
286 ( data )[( offset ) + 2] = MBEDTLS_BYTE_2( n ); \
287}
288#endif
289
290/**
291 * Get the unsigned 64 bits integer corresponding to eight bytes in
292 * big-endian order (MSB first).
293 *
294 * \param data Base address of the memory to get the eight bytes from.
295 * \param offset Offset from \p data of the first and most significant
296 * byte of the eight bytes to build the 64 bits unsigned
297 * integer from.
298 */
299#ifndef MBEDTLS_GET_UINT64_BE
300#define MBEDTLS_GET_UINT64_BE( data, offset ) \
301 ( \
302 ( (uint64_t) ( data )[( offset ) ] << 56 ) \
303 | ( (uint64_t) ( data )[( offset ) + 1] << 48 ) \
304 | ( (uint64_t) ( data )[( offset ) + 2] << 40 ) \
305 | ( (uint64_t) ( data )[( offset ) + 3] << 32 ) \
306 | ( (uint64_t) ( data )[( offset ) + 4] << 24 ) \
307 | ( (uint64_t) ( data )[( offset ) + 5] << 16 ) \
308 | ( (uint64_t) ( data )[( offset ) + 6] << 8 ) \
309 | ( (uint64_t) ( data )[( offset ) + 7] ) \
310 )
311#endif
312
313/**
314 * Put in memory a 64 bits unsigned integer in big-endian order.
315 *
316 * \param n 64 bits unsigned integer to put in memory.
317 * \param data Base address of the memory where to put the 64
318 * bits unsigned integer in.
319 * \param offset Offset from \p data where to put the most significant
320 * byte of the 64 bits unsigned integer \p n.
321 */
322#ifndef MBEDTLS_PUT_UINT64_BE
323#define MBEDTLS_PUT_UINT64_BE( n, data, offset ) \
324{ \
325 ( data )[( offset ) ] = MBEDTLS_BYTE_7( n ); \
326 ( data )[( offset ) + 1] = MBEDTLS_BYTE_6( n ); \
327 ( data )[( offset ) + 2] = MBEDTLS_BYTE_5( n ); \
328 ( data )[( offset ) + 3] = MBEDTLS_BYTE_4( n ); \
329 ( data )[( offset ) + 4] = MBEDTLS_BYTE_3( n ); \
330 ( data )[( offset ) + 5] = MBEDTLS_BYTE_2( n ); \
331 ( data )[( offset ) + 6] = MBEDTLS_BYTE_1( n ); \
332 ( data )[( offset ) + 7] = MBEDTLS_BYTE_0( n ); \
333}
334#endif
335
336/**
337 * Get the unsigned 64 bits integer corresponding to eight bytes in
338 * little-endian order (LSB first).
339 *
340 * \param data Base address of the memory to get the eight bytes from.
341 * \param offset Offset from \p data of the first and least significant
342 * byte of the eight bytes to build the 64 bits unsigned
343 * integer from.
344 */
345#ifndef MBEDTLS_GET_UINT64_LE
346#define MBEDTLS_GET_UINT64_LE( data, offset ) \
347 ( \
348 ( (uint64_t) ( data )[( offset ) + 7] << 56 ) \
349 | ( (uint64_t) ( data )[( offset ) + 6] << 48 ) \
350 | ( (uint64_t) ( data )[( offset ) + 5] << 40 ) \
351 | ( (uint64_t) ( data )[( offset ) + 4] << 32 ) \
352 | ( (uint64_t) ( data )[( offset ) + 3] << 24 ) \
353 | ( (uint64_t) ( data )[( offset ) + 2] << 16 ) \
354 | ( (uint64_t) ( data )[( offset ) + 1] << 8 ) \
355 | ( (uint64_t) ( data )[( offset ) ] ) \
356 )
357#endif
358
359/**
360 * Put in memory a 64 bits unsigned integer in little-endian order.
361 *
362 * \param n 64 bits unsigned integer to put in memory.
363 * \param data Base address of the memory where to put the 64
364 * bits unsigned integer in.
365 * \param offset Offset from \p data where to put the least significant
366 * byte of the 64 bits unsigned integer \p n.
367 */
368#ifndef MBEDTLS_PUT_UINT64_LE
369#define MBEDTLS_PUT_UINT64_LE( n, data, offset ) \
370{ \
371 ( data )[( offset ) ] = MBEDTLS_BYTE_0( n ); \
372 ( data )[( offset ) + 1] = MBEDTLS_BYTE_1( n ); \
373 ( data )[( offset ) + 2] = MBEDTLS_BYTE_2( n ); \
374 ( data )[( offset ) + 3] = MBEDTLS_BYTE_3( n ); \
375 ( data )[( offset ) + 4] = MBEDTLS_BYTE_4( n ); \
376 ( data )[( offset ) + 5] = MBEDTLS_BYTE_5( n ); \
377 ( data )[( offset ) + 6] = MBEDTLS_BYTE_6( n ); \
378 ( data )[( offset ) + 7] = MBEDTLS_BYTE_7( n ); \
379}
380#endif
381
382#endif /* MBEDTLS_LIBRARY_ALIGNMENT_H */