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