blob: 808d13de2a108b21f6df61e56c5bf21b28129afa [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
Dave Rodgman468df312022-11-23 16:56:35 +000031#if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \
32 !defined(inline) && !defined(__cplusplus)
33#define inline __inline
34#endif
35
Gilles Peskinec4672fd2019-09-11 13:39:11 +020036/** Helper to define a function as static except when building invasive tests.
37 *
38 * If a function is only used inside its own source file and should be
39 * declared `static` to allow the compiler to optimize for code size,
40 * but that function has unit tests, define it with
41 * ```
42 * MBEDTLS_STATIC_TESTABLE int mbedtls_foo(...) { ... }
43 * ```
44 * and declare it in a header in the `library/` directory with
45 * ```
46 * #if defined(MBEDTLS_TEST_HOOKS)
47 * int mbedtls_foo(...);
48 * #endif
49 * ```
50 */
51#if defined(MBEDTLS_TEST_HOOKS)
52#define MBEDTLS_STATIC_TESTABLE
53#else
54#define MBEDTLS_STATIC_TESTABLE static
55#endif
56
TRodziewicz7871c2e2021-07-07 17:29:43 +020057#if defined(MBEDTLS_TEST_HOOKS)
58extern void (*mbedtls_test_hook_test_fail)( const char * test, int line, const char * file );
59#define MBEDTLS_TEST_HOOK_TEST_ASSERT( TEST ) \
60 do { \
61 if( ( ! ( TEST ) ) && ( ( *mbedtls_test_hook_test_fail ) != NULL ) ) \
62 { \
63 ( *mbedtls_test_hook_test_fail )( #TEST, __LINE__, __FILE__ ); \
64 } \
65 } while( 0 )
66#else
67#define MBEDTLS_TEST_HOOK_TEST_ASSERT( TEST )
68#endif /* defined(MBEDTLS_TEST_HOOKS) */
69
Mateusz Starzyk57d1d192021-05-27 14:39:53 +020070/** Allow library to access its structs' private members.
Mateusz Starzyk2c09c9b2021-05-14 22:20:10 +020071 *
72 * Although structs defined in header files are publicly available,
73 * their members are private and should not be accessed by the user.
74 */
75#define MBEDTLS_ALLOW_PRIVATE_ACCESS
76
Dave Rodgmanfdd967e2022-11-22 18:55:17 +000077/** Detect architectures where unaligned memory accesses are safe and performant.
78 *
79 * This list is incomplete.
80 */
Dave Rodgman1bab27f2022-11-23 16:51:59 +000081#if (defined(__i386__) || defined(__amd64__) || defined( __x86_64__) \
Dave Rodgmanfdd967e2022-11-22 18:55:17 +000082 || defined(__ARM_FEATURE_UNALIGNED) \
83 || defined(__aarch64__) \
84 || defined(__ARM_ARCH_8__) || defined(__ARM_ARCH_8A__) || defined(__ARM_ARCH_8M__) \
Dave Rodgman1bab27f2022-11-23 16:51:59 +000085 || defined(__ARM_ARCH_7A__)) \
86 && (!(defined(__has_feature) && __has_feature(undefined_behavior_sanitizer)))
Dave Rodgmanfdd967e2022-11-22 18:55:17 +000087#define MBEDTLS_ALLOW_UNALIGNED_ACCESS
88#endif
89
Joe Subbiani50dde562021-06-22 15:51:53 +010090/** Byte Reading Macros
Joe Subbiani6f2bb0c2021-06-24 09:06:23 +010091 *
Joe Subbiani9ab18662021-07-21 16:35:48 +010092 * Given a multi-byte integer \p x, MBEDTLS_BYTE_n retrieves the n-th
Joe Subbianid0687852021-07-21 15:22:47 +010093 * byte from x, where byte 0 is the least significant byte.
Joe Subbiani50dde562021-06-22 15:51:53 +010094 */
Joe Subbiani2194dc42021-07-14 12:31:31 +010095#define MBEDTLS_BYTE_0( x ) ( (uint8_t) ( ( x ) & 0xff ) )
Joe Subbiani5ecac212021-06-24 13:00:03 +010096#define MBEDTLS_BYTE_1( x ) ( (uint8_t) ( ( ( x ) >> 8 ) & 0xff ) )
97#define MBEDTLS_BYTE_2( x ) ( (uint8_t) ( ( ( x ) >> 16 ) & 0xff ) )
98#define MBEDTLS_BYTE_3( x ) ( (uint8_t) ( ( ( x ) >> 24 ) & 0xff ) )
Joe Subbiani2194dc42021-07-14 12:31:31 +010099#define MBEDTLS_BYTE_4( x ) ( (uint8_t) ( ( ( x ) >> 32 ) & 0xff ) )
100#define MBEDTLS_BYTE_5( x ) ( (uint8_t) ( ( ( x ) >> 40 ) & 0xff ) )
101#define MBEDTLS_BYTE_6( x ) ( (uint8_t) ( ( ( x ) >> 48 ) & 0xff ) )
102#define MBEDTLS_BYTE_7( x ) ( (uint8_t) ( ( ( x ) >> 56 ) & 0xff ) )
Joe Subbianicd84d762021-07-08 14:59:52 +0100103
Joe Subbiani394bdd62021-07-07 15:16:56 +0100104/**
Joe Subbianif5462d92021-07-13 12:13:19 +0100105 * Get the unsigned 32 bits integer corresponding to four bytes in
Joe Subbiani635231a2021-07-14 11:53:07 +0100106 * big-endian order (MSB first).
Joe Subbiani394bdd62021-07-07 15:16:56 +0100107 *
Joe Subbiani635231a2021-07-14 11:53:07 +0100108 * \param data Base address of the memory to get the four bytes from.
Jerry Yu29287a42021-10-28 10:26:13 +0800109 * \param offset Offset from \p data of the first and most significant
Joe Subbianif5462d92021-07-13 12:13:19 +0100110 * byte of the four bytes to build the 32 bits unsigned
Joe Subbiani635231a2021-07-14 11:53:07 +0100111 * integer from.
Joe Subbiani6a506312021-07-07 16:56:29 +0100112 */
113#ifndef MBEDTLS_GET_UINT32_BE
Joe Subbiani5241e342021-07-19 15:29:18 +0100114#define MBEDTLS_GET_UINT32_BE( data , offset ) \
115 ( \
116 ( (uint32_t) ( data )[( offset ) ] << 24 ) \
117 | ( (uint32_t) ( data )[( offset ) + 1] << 16 ) \
118 | ( (uint32_t) ( data )[( offset ) + 2] << 8 ) \
119 | ( (uint32_t) ( data )[( offset ) + 3] ) \
Joe Subbiani6a506312021-07-07 16:56:29 +0100120 )
121#endif
122
123/**
Joe Subbiani635231a2021-07-14 11:53:07 +0100124 * Put in memory a 32 bits unsigned integer in big-endian order.
Joe Subbiani6a506312021-07-07 16:56:29 +0100125 *
Joe Subbianif5462d92021-07-13 12:13:19 +0100126 * \param n 32 bits unsigned integer to put in memory.
127 * \param data Base address of the memory where to put the 32
Joe Subbiani635231a2021-07-14 11:53:07 +0100128 * bits unsigned integer in.
Jerry Yu29287a42021-10-28 10:26:13 +0800129 * \param offset Offset from \p data where to put the most significant
Joe Subbiani635231a2021-07-14 11:53:07 +0100130 * byte of the 32 bits unsigned integer \p n.
Joe Subbiani394bdd62021-07-07 15:16:56 +0100131 */
Joe Subbiani5ecac212021-06-24 13:00:03 +0100132#ifndef MBEDTLS_PUT_UINT32_BE
Joe Subbiani5241e342021-07-19 15:29:18 +0100133#define MBEDTLS_PUT_UINT32_BE( n, data, offset ) \
134{ \
135 ( data )[( offset ) ] = MBEDTLS_BYTE_3( n ); \
136 ( data )[( offset ) + 1] = MBEDTLS_BYTE_2( n ); \
137 ( data )[( offset ) + 2] = MBEDTLS_BYTE_1( n ); \
138 ( data )[( offset ) + 3] = MBEDTLS_BYTE_0( n ); \
139}
Joe Subbiani30d974c2021-06-23 11:49:03 +0100140#endif
141
Joe Subbiani394bdd62021-07-07 15:16:56 +0100142/**
Joe Subbianif5462d92021-07-13 12:13:19 +0100143 * Get the unsigned 32 bits integer corresponding to four bytes in
Joe Subbiani635231a2021-07-14 11:53:07 +0100144 * little-endian order (LSB first).
Joe Subbiani6a506312021-07-07 16:56:29 +0100145 *
Joe Subbiani635231a2021-07-14 11:53:07 +0100146 * \param data Base address of the memory to get the four bytes from.
Jerry Yu29287a42021-10-28 10:26:13 +0800147 * \param offset Offset from \p data of the first and least significant
Joe Subbianif5462d92021-07-13 12:13:19 +0100148 * byte of the four bytes to build the 32 bits unsigned
Joe Subbiani635231a2021-07-14 11:53:07 +0100149 * integer from.
Joe Subbiani54c61342021-06-23 12:16:47 +0100150 */
Joe Subbiani5ecac212021-06-24 13:00:03 +0100151#ifndef MBEDTLS_GET_UINT32_LE
Joe Subbiani5241e342021-07-19 15:29:18 +0100152#define MBEDTLS_GET_UINT32_LE( data, offset ) \
153 ( \
154 ( (uint32_t) ( data )[( offset ) ] ) \
155 | ( (uint32_t) ( data )[( offset ) + 1] << 8 ) \
156 | ( (uint32_t) ( data )[( offset ) + 2] << 16 ) \
157 | ( (uint32_t) ( data )[( offset ) + 3] << 24 ) \
Joe Subbiani6a506312021-07-07 16:56:29 +0100158 )
Joe Subbiani54c61342021-06-23 12:16:47 +0100159#endif
160
Joe Subbiani6a506312021-07-07 16:56:29 +0100161/**
Joe Subbiani635231a2021-07-14 11:53:07 +0100162 * Put in memory a 32 bits unsigned integer in little-endian order.
Joe Subbiani6a506312021-07-07 16:56:29 +0100163 *
Joe Subbianif5462d92021-07-13 12:13:19 +0100164 * \param n 32 bits unsigned integer to put in memory.
165 * \param data Base address of the memory where to put the 32
Joe Subbiani635231a2021-07-14 11:53:07 +0100166 * bits unsigned integer in.
Jerry Yu29287a42021-10-28 10:26:13 +0800167 * \param offset Offset from \p data where to put the least significant
Joe Subbiani635231a2021-07-14 11:53:07 +0100168 * byte of the 32 bits unsigned integer \p n.
Joe Subbiani6a506312021-07-07 16:56:29 +0100169 */
Joe Subbiani5ecac212021-06-24 13:00:03 +0100170#ifndef MBEDTLS_PUT_UINT32_LE
Joe Subbiani5241e342021-07-19 15:29:18 +0100171#define MBEDTLS_PUT_UINT32_LE( n, data, offset ) \
172{ \
173 ( data )[( offset ) ] = MBEDTLS_BYTE_0( n ); \
174 ( data )[( offset ) + 1] = MBEDTLS_BYTE_1( n ); \
175 ( data )[( offset ) + 2] = MBEDTLS_BYTE_2( n ); \
176 ( data )[( offset ) + 3] = MBEDTLS_BYTE_3( n ); \
177}
Joe Subbiani54c61342021-06-23 12:16:47 +0100178#endif
179
Joe Subbiani6f2bb0c2021-06-24 09:06:23 +0100180/**
Joe Subbianibf7ea842021-07-14 12:05:51 +0100181 * Get the unsigned 16 bits integer corresponding to two bytes in
Joe Subbiani635231a2021-07-14 11:53:07 +0100182 * little-endian order (LSB first).
Joe Subbiani6a506312021-07-07 16:56:29 +0100183 *
Joe Subbianibf7ea842021-07-14 12:05:51 +0100184 * \param data Base address of the memory to get the two bytes from.
Jerry Yu29287a42021-10-28 10:26:13 +0800185 * \param offset Offset from \p data of the first and least significant
Joe Subbianibf7ea842021-07-14 12:05:51 +0100186 * byte of the two bytes to build the 16 bits unsigned
Joe Subbiani635231a2021-07-14 11:53:07 +0100187 * integer from.
Joe Subbiani3b394502021-06-23 11:23:44 +0100188 */
Joe Subbiani6a506312021-07-07 16:56:29 +0100189#ifndef MBEDTLS_GET_UINT16_LE
Joe Subbiani5241e342021-07-19 15:29:18 +0100190#define MBEDTLS_GET_UINT16_LE( data, offset ) \
191 ( \
192 ( (uint16_t) ( data )[( offset ) ] ) \
193 | ( (uint16_t) ( data )[( offset ) + 1] << 8 ) \
Joe Subbiani3b394502021-06-23 11:23:44 +0100194 )
Joe Subbiani6a506312021-07-07 16:56:29 +0100195#endif
Joe Subbiani3b394502021-06-23 11:23:44 +0100196
Joe Subbiani394bdd62021-07-07 15:16:56 +0100197/**
Joe Subbiani635231a2021-07-14 11:53:07 +0100198 * Put in memory a 16 bits unsigned integer in little-endian order.
Joe Subbiani394bdd62021-07-07 15:16:56 +0100199 *
Joe Subbianif5462d92021-07-13 12:13:19 +0100200 * \param n 16 bits unsigned integer to put in memory.
201 * \param data Base address of the memory where to put the 16
Joe Subbiani635231a2021-07-14 11:53:07 +0100202 * bits unsigned integer in.
Jerry Yu29287a42021-10-28 10:26:13 +0800203 * \param offset Offset from \p data where to put the least significant
Joe Subbiani635231a2021-07-14 11:53:07 +0100204 * byte of the 16 bits unsigned integer \p n.
Joe Subbiani394bdd62021-07-07 15:16:56 +0100205 */
Joe Subbiani9fa9ac32021-07-05 15:37:39 +0100206#ifndef MBEDTLS_PUT_UINT16_LE
Joe Subbiani5241e342021-07-19 15:29:18 +0100207#define MBEDTLS_PUT_UINT16_LE( n, data, offset ) \
208{ \
209 ( data )[( offset ) ] = MBEDTLS_BYTE_0( n ); \
210 ( data )[( offset ) + 1] = MBEDTLS_BYTE_1( n ); \
Joe Subbiani9fa9ac32021-07-05 15:37:39 +0100211}
212#endif
213
Joe Subbiani99edd6c2021-07-16 12:29:49 +0100214/**
Joe Subbiani6dd73642021-07-19 11:56:54 +0100215 * Get the unsigned 16 bits integer corresponding to two bytes in
Joe Subbiani5241e342021-07-19 15:29:18 +0100216 * big-endian order (MSB first).
Joe Subbiani6dd73642021-07-19 11:56:54 +0100217 *
218 * \param data Base address of the memory to get the two bytes from.
Jerry Yu29287a42021-10-28 10:26:13 +0800219 * \param offset Offset from \p data of the first and most significant
Joe Subbiani6dd73642021-07-19 11:56:54 +0100220 * byte of the two bytes to build the 16 bits unsigned
221 * integer from.
222 */
223#ifndef MBEDTLS_GET_UINT16_BE
Joe Subbiani5241e342021-07-19 15:29:18 +0100224#define MBEDTLS_GET_UINT16_BE( data, offset ) \
225 ( \
226 ( (uint16_t) ( data )[( offset ) ] << 8 ) \
227 | ( (uint16_t) ( data )[( offset ) + 1] ) \
Joe Subbiani6dd73642021-07-19 11:56:54 +0100228 )
229#endif
230
231/**
232 * Put in memory a 16 bits unsigned integer in big-endian order.
233 *
234 * \param n 16 bits unsigned integer to put in memory.
235 * \param data Base address of the memory where to put the 16
236 * bits unsigned integer in.
Jerry Yu29287a42021-10-28 10:26:13 +0800237 * \param offset Offset from \p data where to put the most significant
Joe Subbiani6dd73642021-07-19 11:56:54 +0100238 * byte of the 16 bits unsigned integer \p n.
239 */
240#ifndef MBEDTLS_PUT_UINT16_BE
Joe Subbiani5241e342021-07-19 15:29:18 +0100241#define MBEDTLS_PUT_UINT16_BE( n, data, offset ) \
242{ \
243 ( data )[( offset ) ] = MBEDTLS_BYTE_1( n ); \
244 ( data )[( offset ) + 1] = MBEDTLS_BYTE_0( n ); \
Joe Subbiani6dd73642021-07-19 11:56:54 +0100245}
246#endif
247
248/**
Jerry Yuf3f5c212021-10-27 17:05:49 +0800249 * Get the unsigned 24 bits integer corresponding to three bytes in
Jerry Yu643d1162021-10-27 13:52:04 +0800250 * big-endian order (MSB first).
251 *
Jerry Yuf3f5c212021-10-27 17:05:49 +0800252 * \param data Base address of the memory to get the three bytes from.
253 * \param offset Offset from \p data of the first and most significant
254 * byte of the three bytes to build the 24 bits unsigned
Jerry Yu643d1162021-10-27 13:52:04 +0800255 * integer from.
256 */
257#ifndef MBEDTLS_GET_UINT24_BE
258#define MBEDTLS_GET_UINT24_BE( data , offset ) \
259 ( \
260 ( (uint32_t) ( data )[( offset ) ] << 16 ) \
261 | ( (uint32_t) ( data )[( offset ) + 1] << 8 ) \
262 | ( (uint32_t) ( data )[( offset ) + 2] ) \
263 )
264#endif
265
266/**
267 * Put in memory a 24 bits unsigned integer in big-endian order.
268 *
269 * \param n 24 bits unsigned integer to put in memory.
270 * \param data Base address of the memory where to put the 24
271 * bits unsigned integer in.
Jerry Yuf3f5c212021-10-27 17:05:49 +0800272 * \param offset Offset from \p data where to put the most significant
Jerry Yu643d1162021-10-27 13:52:04 +0800273 * byte of the 24 bits unsigned integer \p n.
274 */
275#ifndef MBEDTLS_PUT_UINT24_BE
276#define MBEDTLS_PUT_UINT24_BE( n, data, offset ) \
277{ \
278 ( data )[( offset ) ] = MBEDTLS_BYTE_2( n ); \
279 ( data )[( offset ) + 1] = MBEDTLS_BYTE_1( n ); \
280 ( data )[( offset ) + 2] = MBEDTLS_BYTE_0( n ); \
281}
282#endif
283
284/**
Jerry Yuf3f5c212021-10-27 17:05:49 +0800285 * Get the unsigned 24 bits integer corresponding to three bytes in
Jerry Yu643d1162021-10-27 13:52:04 +0800286 * little-endian order (LSB first).
287 *
Jerry Yuf3f5c212021-10-27 17:05:49 +0800288 * \param data Base address of the memory to get the three bytes from.
289 * \param offset Offset from \p data of the first and least significant
290 * byte of the three bytes to build the 24 bits unsigned
Jerry Yu643d1162021-10-27 13:52:04 +0800291 * integer from.
292 */
293#ifndef MBEDTLS_GET_UINT24_LE
294#define MBEDTLS_GET_UINT24_LE( data, offset ) \
295 ( \
296 ( (uint32_t) ( data )[( offset ) ] ) \
297 | ( (uint32_t) ( data )[( offset ) + 1] << 8 ) \
298 | ( (uint32_t) ( data )[( offset ) + 2] << 16 ) \
299 )
300#endif
301
302/**
303 * Put in memory a 24 bits unsigned integer in little-endian order.
304 *
305 * \param n 24 bits unsigned integer to put in memory.
306 * \param data Base address of the memory where to put the 24
307 * bits unsigned integer in.
Jerry Yuf3f5c212021-10-27 17:05:49 +0800308 * \param offset Offset from \p data where to put the least significant
Jerry Yu643d1162021-10-27 13:52:04 +0800309 * byte of the 24 bits unsigned integer \p n.
310 */
311#ifndef MBEDTLS_PUT_UINT24_LE
312#define MBEDTLS_PUT_UINT24_LE( n, data, offset ) \
313{ \
314 ( data )[( offset ) ] = MBEDTLS_BYTE_0( n ); \
315 ( data )[( offset ) + 1] = MBEDTLS_BYTE_1( n ); \
316 ( data )[( offset ) + 2] = MBEDTLS_BYTE_2( n ); \
317}
318#endif
319
320/**
Joe Subbiani99edd6c2021-07-16 12:29:49 +0100321 * Get the unsigned 64 bits integer corresponding to eight bytes in
322 * big-endian order (MSB first).
323 *
324 * \param data Base address of the memory to get the eight bytes from.
Jerry Yu29287a42021-10-28 10:26:13 +0800325 * \param offset Offset from \p data of the first and most significant
Joe Subbiani99edd6c2021-07-16 12:29:49 +0100326 * byte of the eight bytes to build the 64 bits unsigned
327 * integer from.
328 */
329#ifndef MBEDTLS_GET_UINT64_BE
Joe Subbiani5241e342021-07-19 15:29:18 +0100330#define MBEDTLS_GET_UINT64_BE( data, offset ) \
331 ( \
332 ( (uint64_t) ( data )[( offset ) ] << 56 ) \
333 | ( (uint64_t) ( data )[( offset ) + 1] << 48 ) \
334 | ( (uint64_t) ( data )[( offset ) + 2] << 40 ) \
335 | ( (uint64_t) ( data )[( offset ) + 3] << 32 ) \
336 | ( (uint64_t) ( data )[( offset ) + 4] << 24 ) \
337 | ( (uint64_t) ( data )[( offset ) + 5] << 16 ) \
338 | ( (uint64_t) ( data )[( offset ) + 6] << 8 ) \
339 | ( (uint64_t) ( data )[( offset ) + 7] ) \
Joe Subbiani99edd6c2021-07-16 12:29:49 +0100340 )
341#endif
342
343/**
344 * Put in memory a 64 bits unsigned integer in big-endian order.
345 *
346 * \param n 64 bits unsigned integer to put in memory.
347 * \param data Base address of the memory where to put the 64
348 * bits unsigned integer in.
Jerry Yu29287a42021-10-28 10:26:13 +0800349 * \param offset Offset from \p data where to put the most significant
Joe Subbiani99edd6c2021-07-16 12:29:49 +0100350 * byte of the 64 bits unsigned integer \p n.
351 */
352#ifndef MBEDTLS_PUT_UINT64_BE
Joe Subbiani5241e342021-07-19 15:29:18 +0100353#define MBEDTLS_PUT_UINT64_BE( n, data, offset ) \
354{ \
355 ( data )[( offset ) ] = MBEDTLS_BYTE_7( n ); \
356 ( data )[( offset ) + 1] = MBEDTLS_BYTE_6( n ); \
357 ( data )[( offset ) + 2] = MBEDTLS_BYTE_5( n ); \
358 ( data )[( offset ) + 3] = MBEDTLS_BYTE_4( n ); \
359 ( data )[( offset ) + 4] = MBEDTLS_BYTE_3( n ); \
360 ( data )[( offset ) + 5] = MBEDTLS_BYTE_2( n ); \
361 ( data )[( offset ) + 6] = MBEDTLS_BYTE_1( n ); \
362 ( data )[( offset ) + 7] = MBEDTLS_BYTE_0( n ); \
Joe Subbiani99edd6c2021-07-16 12:29:49 +0100363}
364#endif
365
366/**
367 * Get the unsigned 64 bits integer corresponding to eight bytes in
368 * little-endian order (LSB first).
369 *
370 * \param data Base address of the memory to get the eight bytes from.
Jerry Yu29287a42021-10-28 10:26:13 +0800371 * \param offset Offset from \p data of the first and least significant
Joe Subbiani99edd6c2021-07-16 12:29:49 +0100372 * byte of the eight bytes to build the 64 bits unsigned
373 * integer from.
374 */
375#ifndef MBEDTLS_GET_UINT64_LE
Joe Subbiani5241e342021-07-19 15:29:18 +0100376#define MBEDTLS_GET_UINT64_LE( data, offset ) \
377 ( \
378 ( (uint64_t) ( data )[( offset ) + 7] << 56 ) \
379 | ( (uint64_t) ( data )[( offset ) + 6] << 48 ) \
380 | ( (uint64_t) ( data )[( offset ) + 5] << 40 ) \
381 | ( (uint64_t) ( data )[( offset ) + 4] << 32 ) \
382 | ( (uint64_t) ( data )[( offset ) + 3] << 24 ) \
383 | ( (uint64_t) ( data )[( offset ) + 2] << 16 ) \
384 | ( (uint64_t) ( data )[( offset ) + 1] << 8 ) \
385 | ( (uint64_t) ( data )[( offset ) ] ) \
Joe Subbiani99edd6c2021-07-16 12:29:49 +0100386 )
387#endif
388
389/**
390 * Put in memory a 64 bits unsigned integer in little-endian order.
391 *
392 * \param n 64 bits unsigned integer to put in memory.
393 * \param data Base address of the memory where to put the 64
394 * bits unsigned integer in.
Jerry Yu29287a42021-10-28 10:26:13 +0800395 * \param offset Offset from \p data where to put the least significant
Joe Subbiani99edd6c2021-07-16 12:29:49 +0100396 * byte of the 64 bits unsigned integer \p n.
397 */
398#ifndef MBEDTLS_PUT_UINT64_LE
Joe Subbiani5241e342021-07-19 15:29:18 +0100399#define MBEDTLS_PUT_UINT64_LE( n, data, offset ) \
400{ \
401 ( data )[( offset ) ] = MBEDTLS_BYTE_0( n ); \
402 ( data )[( offset ) + 1] = MBEDTLS_BYTE_1( n ); \
403 ( data )[( offset ) + 2] = MBEDTLS_BYTE_2( n ); \
404 ( data )[( offset ) + 3] = MBEDTLS_BYTE_3( n ); \
405 ( data )[( offset ) + 4] = MBEDTLS_BYTE_4( n ); \
406 ( data )[( offset ) + 5] = MBEDTLS_BYTE_5( n ); \
407 ( data )[( offset ) + 6] = MBEDTLS_BYTE_6( n ); \
408 ( data )[( offset ) + 7] = MBEDTLS_BYTE_7( n ); \
Joe Subbiani99edd6c2021-07-16 12:29:49 +0100409}
410#endif
Joe Subbiani9fa9ac32021-07-05 15:37:39 +0100411
Dave Rodgmanc3d80412022-11-22 15:01:39 +0000412/**
413 * Perform a fast block XOR operation, such that
414 * r[i] = a[i] ^ b[i] where 0 <= i < n
415 *
416 * \param r Pointer to result (buffer of at least \p n bytes). \p r
417 * may be equal to either \p a or \p b, but behaviour when
418 * it overlaps in other ways is undefined.
419 * \param a Pointer to input (buffer of at least \p n bytes)
420 * \param b Pointer to input (buffer of at least \p n bytes)
421 * \param n Number of bytes to process.
422 */
Dave Rodgman3c8eb7e2022-11-23 14:50:03 +0000423inline void mbedtls_xor( unsigned char *r, unsigned char const *a, unsigned char const *b, size_t n )
Dave Rodgmanc3d80412022-11-22 15:01:39 +0000424{
Dave Rodgmanfdd967e2022-11-22 18:55:17 +0000425#if defined(MBEDTLS_ALLOW_UNALIGNED_ACCESS)
Dave Rodgmanf9a1c372022-11-23 14:02:00 +0000426 uint32_t *a32 = (uint32_t *)a;
427 uint32_t *b32 = (uint32_t *)b;
428 uint32_t *r32 = (uint32_t *)r;
429 for ( size_t i = 0; i < ( n >> 2 ); i++ )
Dave Rodgmanc3d80412022-11-22 15:01:39 +0000430 {
431 r32[i] = a32[i] ^ b32[i];
432 }
Dave Rodgmanf9a1c372022-11-23 14:02:00 +0000433 for ( size_t i = n - ( n % 4 ) ; i < n; i++ )
Dave Rodgmanc3d80412022-11-22 15:01:39 +0000434 {
435 r[i] = a[i] ^ b[i];
436 }
Dave Rodgmanfdd967e2022-11-22 18:55:17 +0000437#else
438 for ( size_t i = 0; i < n; i++ )
439 {
440 r[i] = a[i] ^ b[i];
441 }
442#endif
Dave Rodgmanc3d80412022-11-22 15:01:39 +0000443}
444
Jerry Yu6c983522021-09-24 12:45:36 +0800445/* Fix MSVC C99 compatible issue
446 * MSVC support __func__ from visual studio 2015( 1900 )
447 * Use MSVC predefine macro to avoid name check fail.
448 */
449#if (defined(_MSC_VER) && ( _MSC_VER <= 1900 ))
Jerry Yud52398d2021-09-28 16:13:44 +0800450#define /*no-check-names*/ __func__ __FUNCTION__
Jerry Yu6c983522021-09-24 12:45:36 +0800451#endif
452
Gilles Peskinec4672fd2019-09-11 13:39:11 +0200453#endif /* MBEDTLS_LIBRARY_COMMON_H */