blob: d1af48eb28885279e2199005adaa1dae89585b20 [file] [log] [blame]
Gilles Peskinec4672fd2019-09-11 13:39:11 +02001/**
2 * \file common.h
3 *
4 * \brief Utility macros for internal use in the library
5 */
6/*
Bence Szépkúti1e148272020-08-07 13:07:28 +02007 * Copyright The Mbed TLS Contributors
Gilles Peskinec4672fd2019-09-11 13:39:11 +02008 * 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.
Gilles Peskinec4672fd2019-09-11 13:39:11 +020021 */
22
23#ifndef MBEDTLS_LIBRARY_COMMON_H
24#define MBEDTLS_LIBRARY_COMMON_H
25
Bence Szépkútic662b362021-05-27 11:25:03 +020026#include "mbedtls/build_info.h"
Gilles Peskinec4672fd2019-09-11 13:39:11 +020027
Joe Subbiani2194dc42021-07-14 12:31:31 +010028#include <stdint.h>
Dave Rodgmanc3d80412022-11-22 15:01:39 +000029#include <stddef.h>
Joe Subbiani2194dc42021-07-14 12:31:31 +010030
Gilles Peskinec4672fd2019-09-11 13:39:11 +020031/** Helper to define a function as static except when building invasive tests.
32 *
33 * If a function is only used inside its own source file and should be
34 * declared `static` to allow the compiler to optimize for code size,
35 * but that function has unit tests, define it with
36 * ```
37 * MBEDTLS_STATIC_TESTABLE int mbedtls_foo(...) { ... }
38 * ```
39 * and declare it in a header in the `library/` directory with
40 * ```
41 * #if defined(MBEDTLS_TEST_HOOKS)
42 * int mbedtls_foo(...);
43 * #endif
44 * ```
45 */
46#if defined(MBEDTLS_TEST_HOOKS)
47#define MBEDTLS_STATIC_TESTABLE
48#else
49#define MBEDTLS_STATIC_TESTABLE static
50#endif
51
TRodziewicz7871c2e2021-07-07 17:29:43 +020052#if defined(MBEDTLS_TEST_HOOKS)
53extern void (*mbedtls_test_hook_test_fail)( const char * test, int line, const char * file );
54#define MBEDTLS_TEST_HOOK_TEST_ASSERT( TEST ) \
55 do { \
56 if( ( ! ( TEST ) ) && ( ( *mbedtls_test_hook_test_fail ) != NULL ) ) \
57 { \
58 ( *mbedtls_test_hook_test_fail )( #TEST, __LINE__, __FILE__ ); \
59 } \
60 } while( 0 )
61#else
62#define MBEDTLS_TEST_HOOK_TEST_ASSERT( TEST )
63#endif /* defined(MBEDTLS_TEST_HOOKS) */
64
Mateusz Starzyk57d1d192021-05-27 14:39:53 +020065/** Allow library to access its structs' private members.
Mateusz Starzyk2c09c9b2021-05-14 22:20:10 +020066 *
67 * Although structs defined in header files are publicly available,
68 * their members are private and should not be accessed by the user.
69 */
70#define MBEDTLS_ALLOW_PRIVATE_ACCESS
71
Joe Subbiani50dde562021-06-22 15:51:53 +010072/** Byte Reading Macros
Joe Subbiani6f2bb0c2021-06-24 09:06:23 +010073 *
Joe Subbiani9ab18662021-07-21 16:35:48 +010074 * Given a multi-byte integer \p x, MBEDTLS_BYTE_n retrieves the n-th
Joe Subbianid0687852021-07-21 15:22:47 +010075 * byte from x, where byte 0 is the least significant byte.
Joe Subbiani50dde562021-06-22 15:51:53 +010076 */
Joe Subbiani2194dc42021-07-14 12:31:31 +010077#define MBEDTLS_BYTE_0( x ) ( (uint8_t) ( ( x ) & 0xff ) )
Joe Subbiani5ecac212021-06-24 13:00:03 +010078#define MBEDTLS_BYTE_1( x ) ( (uint8_t) ( ( ( x ) >> 8 ) & 0xff ) )
79#define MBEDTLS_BYTE_2( x ) ( (uint8_t) ( ( ( x ) >> 16 ) & 0xff ) )
80#define MBEDTLS_BYTE_3( x ) ( (uint8_t) ( ( ( x ) >> 24 ) & 0xff ) )
Joe Subbiani2194dc42021-07-14 12:31:31 +010081#define MBEDTLS_BYTE_4( x ) ( (uint8_t) ( ( ( x ) >> 32 ) & 0xff ) )
82#define MBEDTLS_BYTE_5( x ) ( (uint8_t) ( ( ( x ) >> 40 ) & 0xff ) )
83#define MBEDTLS_BYTE_6( x ) ( (uint8_t) ( ( ( x ) >> 48 ) & 0xff ) )
84#define MBEDTLS_BYTE_7( x ) ( (uint8_t) ( ( ( x ) >> 56 ) & 0xff ) )
Joe Subbianicd84d762021-07-08 14:59:52 +010085
Joe Subbiani394bdd62021-07-07 15:16:56 +010086/**
Joe Subbianif5462d92021-07-13 12:13:19 +010087 * Get the unsigned 32 bits integer corresponding to four bytes in
Joe Subbiani635231a2021-07-14 11:53:07 +010088 * big-endian order (MSB first).
Joe Subbiani394bdd62021-07-07 15:16:56 +010089 *
Joe Subbiani635231a2021-07-14 11:53:07 +010090 * \param data Base address of the memory to get the four bytes from.
Jerry Yu29287a42021-10-28 10:26:13 +080091 * \param offset Offset from \p data of the first and most significant
Joe Subbianif5462d92021-07-13 12:13:19 +010092 * byte of the four bytes to build the 32 bits unsigned
Joe Subbiani635231a2021-07-14 11:53:07 +010093 * integer from.
Joe Subbiani6a506312021-07-07 16:56:29 +010094 */
95#ifndef MBEDTLS_GET_UINT32_BE
Joe Subbiani5241e342021-07-19 15:29:18 +010096#define MBEDTLS_GET_UINT32_BE( data , offset ) \
97 ( \
98 ( (uint32_t) ( data )[( offset ) ] << 24 ) \
99 | ( (uint32_t) ( data )[( offset ) + 1] << 16 ) \
100 | ( (uint32_t) ( data )[( offset ) + 2] << 8 ) \
101 | ( (uint32_t) ( data )[( offset ) + 3] ) \
Joe Subbiani6a506312021-07-07 16:56:29 +0100102 )
103#endif
104
105/**
Joe Subbiani635231a2021-07-14 11:53:07 +0100106 * Put in memory a 32 bits unsigned integer in big-endian order.
Joe Subbiani6a506312021-07-07 16:56:29 +0100107 *
Joe Subbianif5462d92021-07-13 12:13:19 +0100108 * \param n 32 bits unsigned integer to put in memory.
109 * \param data Base address of the memory where to put the 32
Joe Subbiani635231a2021-07-14 11:53:07 +0100110 * bits unsigned integer in.
Jerry Yu29287a42021-10-28 10:26:13 +0800111 * \param offset Offset from \p data where to put the most significant
Joe Subbiani635231a2021-07-14 11:53:07 +0100112 * byte of the 32 bits unsigned integer \p n.
Joe Subbiani394bdd62021-07-07 15:16:56 +0100113 */
Joe Subbiani5ecac212021-06-24 13:00:03 +0100114#ifndef MBEDTLS_PUT_UINT32_BE
Joe Subbiani5241e342021-07-19 15:29:18 +0100115#define MBEDTLS_PUT_UINT32_BE( n, data, offset ) \
116{ \
117 ( data )[( offset ) ] = MBEDTLS_BYTE_3( n ); \
118 ( data )[( offset ) + 1] = MBEDTLS_BYTE_2( n ); \
119 ( data )[( offset ) + 2] = MBEDTLS_BYTE_1( n ); \
120 ( data )[( offset ) + 3] = MBEDTLS_BYTE_0( n ); \
121}
Joe Subbiani30d974c2021-06-23 11:49:03 +0100122#endif
123
Joe Subbiani394bdd62021-07-07 15:16:56 +0100124/**
Joe Subbianif5462d92021-07-13 12:13:19 +0100125 * Get the unsigned 32 bits integer corresponding to four bytes in
Joe Subbiani635231a2021-07-14 11:53:07 +0100126 * little-endian order (LSB first).
Joe Subbiani6a506312021-07-07 16:56:29 +0100127 *
Joe Subbiani635231a2021-07-14 11:53:07 +0100128 * \param data Base address of the memory to get the four bytes from.
Jerry Yu29287a42021-10-28 10:26:13 +0800129 * \param offset Offset from \p data of the first and least significant
Joe Subbianif5462d92021-07-13 12:13:19 +0100130 * byte of the four bytes to build the 32 bits unsigned
Joe Subbiani635231a2021-07-14 11:53:07 +0100131 * integer from.
Joe Subbiani54c61342021-06-23 12:16:47 +0100132 */
Joe Subbiani5ecac212021-06-24 13:00:03 +0100133#ifndef MBEDTLS_GET_UINT32_LE
Joe Subbiani5241e342021-07-19 15:29:18 +0100134#define MBEDTLS_GET_UINT32_LE( data, offset ) \
135 ( \
136 ( (uint32_t) ( data )[( offset ) ] ) \
137 | ( (uint32_t) ( data )[( offset ) + 1] << 8 ) \
138 | ( (uint32_t) ( data )[( offset ) + 2] << 16 ) \
139 | ( (uint32_t) ( data )[( offset ) + 3] << 24 ) \
Joe Subbiani6a506312021-07-07 16:56:29 +0100140 )
Joe Subbiani54c61342021-06-23 12:16:47 +0100141#endif
142
Joe Subbiani6a506312021-07-07 16:56:29 +0100143/**
Joe Subbiani635231a2021-07-14 11:53:07 +0100144 * Put in memory a 32 bits unsigned integer in little-endian order.
Joe Subbiani6a506312021-07-07 16:56:29 +0100145 *
Joe Subbianif5462d92021-07-13 12:13:19 +0100146 * \param n 32 bits unsigned integer to put in memory.
147 * \param data Base address of the memory where to put the 32
Joe Subbiani635231a2021-07-14 11:53:07 +0100148 * bits unsigned integer in.
Jerry Yu29287a42021-10-28 10:26:13 +0800149 * \param offset Offset from \p data where to put the least significant
Joe Subbiani635231a2021-07-14 11:53:07 +0100150 * byte of the 32 bits unsigned integer \p n.
Joe Subbiani6a506312021-07-07 16:56:29 +0100151 */
Joe Subbiani5ecac212021-06-24 13:00:03 +0100152#ifndef MBEDTLS_PUT_UINT32_LE
Joe Subbiani5241e342021-07-19 15:29:18 +0100153#define MBEDTLS_PUT_UINT32_LE( n, data, offset ) \
154{ \
155 ( data )[( offset ) ] = MBEDTLS_BYTE_0( n ); \
156 ( data )[( offset ) + 1] = MBEDTLS_BYTE_1( n ); \
157 ( data )[( offset ) + 2] = MBEDTLS_BYTE_2( n ); \
158 ( data )[( offset ) + 3] = MBEDTLS_BYTE_3( n ); \
159}
Joe Subbiani54c61342021-06-23 12:16:47 +0100160#endif
161
Joe Subbiani6f2bb0c2021-06-24 09:06:23 +0100162/**
Joe Subbianibf7ea842021-07-14 12:05:51 +0100163 * Get the unsigned 16 bits integer corresponding to two bytes in
Joe Subbiani635231a2021-07-14 11:53:07 +0100164 * little-endian order (LSB first).
Joe Subbiani6a506312021-07-07 16:56:29 +0100165 *
Joe Subbianibf7ea842021-07-14 12:05:51 +0100166 * \param data Base address of the memory to get the two bytes from.
Jerry Yu29287a42021-10-28 10:26:13 +0800167 * \param offset Offset from \p data of the first and least significant
Joe Subbianibf7ea842021-07-14 12:05:51 +0100168 * byte of the two bytes to build the 16 bits unsigned
Joe Subbiani635231a2021-07-14 11:53:07 +0100169 * integer from.
Joe Subbiani3b394502021-06-23 11:23:44 +0100170 */
Joe Subbiani6a506312021-07-07 16:56:29 +0100171#ifndef MBEDTLS_GET_UINT16_LE
Joe Subbiani5241e342021-07-19 15:29:18 +0100172#define MBEDTLS_GET_UINT16_LE( data, offset ) \
173 ( \
174 ( (uint16_t) ( data )[( offset ) ] ) \
175 | ( (uint16_t) ( data )[( offset ) + 1] << 8 ) \
Joe Subbiani3b394502021-06-23 11:23:44 +0100176 )
Joe Subbiani6a506312021-07-07 16:56:29 +0100177#endif
Joe Subbiani3b394502021-06-23 11:23:44 +0100178
Joe Subbiani394bdd62021-07-07 15:16:56 +0100179/**
Joe Subbiani635231a2021-07-14 11:53:07 +0100180 * Put in memory a 16 bits unsigned integer in little-endian order.
Joe Subbiani394bdd62021-07-07 15:16:56 +0100181 *
Joe Subbianif5462d92021-07-13 12:13:19 +0100182 * \param n 16 bits unsigned integer to put in memory.
183 * \param data Base address of the memory where to put the 16
Joe Subbiani635231a2021-07-14 11:53:07 +0100184 * bits unsigned integer in.
Jerry Yu29287a42021-10-28 10:26:13 +0800185 * \param offset Offset from \p data where to put the least significant
Joe Subbiani635231a2021-07-14 11:53:07 +0100186 * byte of the 16 bits unsigned integer \p n.
Joe Subbiani394bdd62021-07-07 15:16:56 +0100187 */
Joe Subbiani9fa9ac32021-07-05 15:37:39 +0100188#ifndef MBEDTLS_PUT_UINT16_LE
Joe Subbiani5241e342021-07-19 15:29:18 +0100189#define MBEDTLS_PUT_UINT16_LE( n, data, offset ) \
190{ \
191 ( data )[( offset ) ] = MBEDTLS_BYTE_0( n ); \
192 ( data )[( offset ) + 1] = MBEDTLS_BYTE_1( n ); \
Joe Subbiani9fa9ac32021-07-05 15:37:39 +0100193}
194#endif
195
Joe Subbiani99edd6c2021-07-16 12:29:49 +0100196/**
Joe Subbiani6dd73642021-07-19 11:56:54 +0100197 * Get the unsigned 16 bits integer corresponding to two bytes in
Joe Subbiani5241e342021-07-19 15:29:18 +0100198 * big-endian order (MSB first).
Joe Subbiani6dd73642021-07-19 11:56:54 +0100199 *
200 * \param data Base address of the memory to get the two bytes from.
Jerry Yu29287a42021-10-28 10:26:13 +0800201 * \param offset Offset from \p data of the first and most significant
Joe Subbiani6dd73642021-07-19 11:56:54 +0100202 * byte of the two bytes to build the 16 bits unsigned
203 * integer from.
204 */
205#ifndef MBEDTLS_GET_UINT16_BE
Joe Subbiani5241e342021-07-19 15:29:18 +0100206#define MBEDTLS_GET_UINT16_BE( data, offset ) \
207 ( \
208 ( (uint16_t) ( data )[( offset ) ] << 8 ) \
209 | ( (uint16_t) ( data )[( offset ) + 1] ) \
Joe Subbiani6dd73642021-07-19 11:56:54 +0100210 )
211#endif
212
213/**
214 * Put in memory a 16 bits unsigned integer in big-endian order.
215 *
216 * \param n 16 bits unsigned integer to put in memory.
217 * \param data Base address of the memory where to put the 16
218 * bits unsigned integer in.
Jerry Yu29287a42021-10-28 10:26:13 +0800219 * \param offset Offset from \p data where to put the most significant
Joe Subbiani6dd73642021-07-19 11:56:54 +0100220 * byte of the 16 bits unsigned integer \p n.
221 */
222#ifndef MBEDTLS_PUT_UINT16_BE
Joe Subbiani5241e342021-07-19 15:29:18 +0100223#define MBEDTLS_PUT_UINT16_BE( n, data, offset ) \
224{ \
225 ( data )[( offset ) ] = MBEDTLS_BYTE_1( n ); \
226 ( data )[( offset ) + 1] = MBEDTLS_BYTE_0( n ); \
Joe Subbiani6dd73642021-07-19 11:56:54 +0100227}
228#endif
229
230/**
Jerry Yuf3f5c212021-10-27 17:05:49 +0800231 * Get the unsigned 24 bits integer corresponding to three bytes in
Jerry Yu643d1162021-10-27 13:52:04 +0800232 * big-endian order (MSB first).
233 *
Jerry Yuf3f5c212021-10-27 17:05:49 +0800234 * \param data Base address of the memory to get the three bytes from.
235 * \param offset Offset from \p data of the first and most significant
236 * byte of the three bytes to build the 24 bits unsigned
Jerry Yu643d1162021-10-27 13:52:04 +0800237 * integer from.
238 */
239#ifndef MBEDTLS_GET_UINT24_BE
240#define MBEDTLS_GET_UINT24_BE( data , offset ) \
241 ( \
242 ( (uint32_t) ( data )[( offset ) ] << 16 ) \
243 | ( (uint32_t) ( data )[( offset ) + 1] << 8 ) \
244 | ( (uint32_t) ( data )[( offset ) + 2] ) \
245 )
246#endif
247
248/**
249 * Put in memory a 24 bits unsigned integer in big-endian order.
250 *
251 * \param n 24 bits unsigned integer to put in memory.
252 * \param data Base address of the memory where to put the 24
253 * bits unsigned integer in.
Jerry Yuf3f5c212021-10-27 17:05:49 +0800254 * \param offset Offset from \p data where to put the most significant
Jerry Yu643d1162021-10-27 13:52:04 +0800255 * byte of the 24 bits unsigned integer \p n.
256 */
257#ifndef MBEDTLS_PUT_UINT24_BE
258#define MBEDTLS_PUT_UINT24_BE( n, data, offset ) \
259{ \
260 ( data )[( offset ) ] = MBEDTLS_BYTE_2( n ); \
261 ( data )[( offset ) + 1] = MBEDTLS_BYTE_1( n ); \
262 ( data )[( offset ) + 2] = MBEDTLS_BYTE_0( n ); \
263}
264#endif
265
266/**
Jerry Yuf3f5c212021-10-27 17:05:49 +0800267 * Get the unsigned 24 bits integer corresponding to three bytes in
Jerry Yu643d1162021-10-27 13:52:04 +0800268 * little-endian order (LSB first).
269 *
Jerry Yuf3f5c212021-10-27 17:05:49 +0800270 * \param data Base address of the memory to get the three bytes from.
271 * \param offset Offset from \p data of the first and least significant
272 * byte of the three bytes to build the 24 bits unsigned
Jerry Yu643d1162021-10-27 13:52:04 +0800273 * integer from.
274 */
275#ifndef MBEDTLS_GET_UINT24_LE
276#define MBEDTLS_GET_UINT24_LE( data, offset ) \
277 ( \
278 ( (uint32_t) ( data )[( offset ) ] ) \
279 | ( (uint32_t) ( data )[( offset ) + 1] << 8 ) \
280 | ( (uint32_t) ( data )[( offset ) + 2] << 16 ) \
281 )
282#endif
283
284/**
285 * Put in memory a 24 bits unsigned integer in little-endian order.
286 *
287 * \param n 24 bits unsigned integer to put in memory.
288 * \param data Base address of the memory where to put the 24
289 * bits unsigned integer in.
Jerry Yuf3f5c212021-10-27 17:05:49 +0800290 * \param offset Offset from \p data where to put the least significant
Jerry Yu643d1162021-10-27 13:52:04 +0800291 * byte of the 24 bits unsigned integer \p n.
292 */
293#ifndef MBEDTLS_PUT_UINT24_LE
294#define MBEDTLS_PUT_UINT24_LE( n, data, offset ) \
295{ \
296 ( data )[( offset ) ] = MBEDTLS_BYTE_0( n ); \
297 ( data )[( offset ) + 1] = MBEDTLS_BYTE_1( n ); \
298 ( data )[( offset ) + 2] = MBEDTLS_BYTE_2( n ); \
299}
300#endif
301
302/**
Joe Subbiani99edd6c2021-07-16 12:29:49 +0100303 * Get the unsigned 64 bits integer corresponding to eight bytes in
304 * big-endian order (MSB first).
305 *
306 * \param data Base address of the memory to get the eight bytes from.
Jerry Yu29287a42021-10-28 10:26:13 +0800307 * \param offset Offset from \p data of the first and most significant
Joe Subbiani99edd6c2021-07-16 12:29:49 +0100308 * byte of the eight bytes to build the 64 bits unsigned
309 * integer from.
310 */
311#ifndef MBEDTLS_GET_UINT64_BE
Joe Subbiani5241e342021-07-19 15:29:18 +0100312#define MBEDTLS_GET_UINT64_BE( data, offset ) \
313 ( \
314 ( (uint64_t) ( data )[( offset ) ] << 56 ) \
315 | ( (uint64_t) ( data )[( offset ) + 1] << 48 ) \
316 | ( (uint64_t) ( data )[( offset ) + 2] << 40 ) \
317 | ( (uint64_t) ( data )[( offset ) + 3] << 32 ) \
318 | ( (uint64_t) ( data )[( offset ) + 4] << 24 ) \
319 | ( (uint64_t) ( data )[( offset ) + 5] << 16 ) \
320 | ( (uint64_t) ( data )[( offset ) + 6] << 8 ) \
321 | ( (uint64_t) ( data )[( offset ) + 7] ) \
Joe Subbiani99edd6c2021-07-16 12:29:49 +0100322 )
323#endif
324
325/**
326 * Put in memory a 64 bits unsigned integer in big-endian order.
327 *
328 * \param n 64 bits unsigned integer to put in memory.
329 * \param data Base address of the memory where to put the 64
330 * bits unsigned integer in.
Jerry Yu29287a42021-10-28 10:26:13 +0800331 * \param offset Offset from \p data where to put the most significant
Joe Subbiani99edd6c2021-07-16 12:29:49 +0100332 * byte of the 64 bits unsigned integer \p n.
333 */
334#ifndef MBEDTLS_PUT_UINT64_BE
Joe Subbiani5241e342021-07-19 15:29:18 +0100335#define MBEDTLS_PUT_UINT64_BE( n, data, offset ) \
336{ \
337 ( data )[( offset ) ] = MBEDTLS_BYTE_7( n ); \
338 ( data )[( offset ) + 1] = MBEDTLS_BYTE_6( n ); \
339 ( data )[( offset ) + 2] = MBEDTLS_BYTE_5( n ); \
340 ( data )[( offset ) + 3] = MBEDTLS_BYTE_4( n ); \
341 ( data )[( offset ) + 4] = MBEDTLS_BYTE_3( n ); \
342 ( data )[( offset ) + 5] = MBEDTLS_BYTE_2( n ); \
343 ( data )[( offset ) + 6] = MBEDTLS_BYTE_1( n ); \
344 ( data )[( offset ) + 7] = MBEDTLS_BYTE_0( n ); \
Joe Subbiani99edd6c2021-07-16 12:29:49 +0100345}
346#endif
347
348/**
349 * Get the unsigned 64 bits integer corresponding to eight bytes in
350 * little-endian order (LSB first).
351 *
352 * \param data Base address of the memory to get the eight bytes from.
Jerry Yu29287a42021-10-28 10:26:13 +0800353 * \param offset Offset from \p data of the first and least significant
Joe Subbiani99edd6c2021-07-16 12:29:49 +0100354 * byte of the eight bytes to build the 64 bits unsigned
355 * integer from.
356 */
357#ifndef MBEDTLS_GET_UINT64_LE
Joe Subbiani5241e342021-07-19 15:29:18 +0100358#define MBEDTLS_GET_UINT64_LE( data, offset ) \
359 ( \
360 ( (uint64_t) ( data )[( offset ) + 7] << 56 ) \
361 | ( (uint64_t) ( data )[( offset ) + 6] << 48 ) \
362 | ( (uint64_t) ( data )[( offset ) + 5] << 40 ) \
363 | ( (uint64_t) ( data )[( offset ) + 4] << 32 ) \
364 | ( (uint64_t) ( data )[( offset ) + 3] << 24 ) \
365 | ( (uint64_t) ( data )[( offset ) + 2] << 16 ) \
366 | ( (uint64_t) ( data )[( offset ) + 1] << 8 ) \
367 | ( (uint64_t) ( data )[( offset ) ] ) \
Joe Subbiani99edd6c2021-07-16 12:29:49 +0100368 )
369#endif
370
371/**
372 * Put in memory a 64 bits unsigned integer in little-endian order.
373 *
374 * \param n 64 bits unsigned integer to put in memory.
375 * \param data Base address of the memory where to put the 64
376 * bits unsigned integer in.
Jerry Yu29287a42021-10-28 10:26:13 +0800377 * \param offset Offset from \p data where to put the least significant
Joe Subbiani99edd6c2021-07-16 12:29:49 +0100378 * byte of the 64 bits unsigned integer \p n.
379 */
380#ifndef MBEDTLS_PUT_UINT64_LE
Joe Subbiani5241e342021-07-19 15:29:18 +0100381#define MBEDTLS_PUT_UINT64_LE( n, data, offset ) \
382{ \
383 ( data )[( offset ) ] = MBEDTLS_BYTE_0( n ); \
384 ( data )[( offset ) + 1] = MBEDTLS_BYTE_1( n ); \
385 ( data )[( offset ) + 2] = MBEDTLS_BYTE_2( n ); \
386 ( data )[( offset ) + 3] = MBEDTLS_BYTE_3( n ); \
387 ( data )[( offset ) + 4] = MBEDTLS_BYTE_4( n ); \
388 ( data )[( offset ) + 5] = MBEDTLS_BYTE_5( n ); \
389 ( data )[( offset ) + 6] = MBEDTLS_BYTE_6( n ); \
390 ( data )[( offset ) + 7] = MBEDTLS_BYTE_7( n ); \
Joe Subbiani99edd6c2021-07-16 12:29:49 +0100391}
392#endif
Joe Subbiani9fa9ac32021-07-05 15:37:39 +0100393
Dave Rodgmanc3d80412022-11-22 15:01:39 +0000394/**
395 * Perform a fast block XOR operation, such that
396 * r[i] = a[i] ^ b[i] where 0 <= i < n
397 *
398 * \param r Pointer to result (buffer of at least \p n bytes). \p r
399 * may be equal to either \p a or \p b, but behaviour when
400 * it overlaps in other ways is undefined.
401 * \param a Pointer to input (buffer of at least \p n bytes)
402 * \param b Pointer to input (buffer of at least \p n bytes)
403 * \param n Number of bytes to process.
404 */
405static inline void mbedtls_xor( unsigned char* r, unsigned char const *a, unsigned char const *b, size_t n )
406{
407 uint32_t *a32 = (uint32_t*)a;
408 uint32_t *b32 = (uint32_t*)b;
409 uint32_t *r32 = (uint32_t*)r;
410 for ( size_t i = 0; i < (n >> 2); i++ )
411 {
412 r32[i] = a32[i] ^ b32[i];
413 }
414 for ( size_t i = n - (n % 4) ; i < n; i++ )
415 {
416 r[i] = a[i] ^ b[i];
417 }
418}
419
Jerry Yu6c983522021-09-24 12:45:36 +0800420/* Fix MSVC C99 compatible issue
421 * MSVC support __func__ from visual studio 2015( 1900 )
422 * Use MSVC predefine macro to avoid name check fail.
423 */
424#if (defined(_MSC_VER) && ( _MSC_VER <= 1900 ))
Jerry Yud52398d2021-09-28 16:13:44 +0800425#define /*no-check-names*/ __func__ __FUNCTION__
Jerry Yu6c983522021-09-24 12:45:36 +0800426#endif
427
Gilles Peskinec4672fd2019-09-11 13:39:11 +0200428#endif /* MBEDTLS_LIBRARY_COMMON_H */