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