blob: 49c5826aa15f6d7db95886da9255f854f0a998ce [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 Rodgmana6778012022-11-23 17:16:57 +000081#if defined(__has_feature)
82#if __has_feature(undefined_behavior_sanitizer)
83#define MBEDTLS_HAVE_UBSAN
84#endif
85#endif
86
Dave Rodgman1bab27f2022-11-23 16:51:59 +000087#if (defined(__i386__) || defined(__amd64__) || defined( __x86_64__) \
Dave Rodgmanfdd967e2022-11-22 18:55:17 +000088 || defined(__ARM_FEATURE_UNALIGNED) \
89 || defined(__aarch64__) \
90 || defined(__ARM_ARCH_8__) || defined(__ARM_ARCH_8A__) || defined(__ARM_ARCH_8M__) \
Dave Rodgmana6778012022-11-23 17:16:57 +000091 || defined(__ARM_ARCH_7A__)) && !defined(MBEDTLS_HAVE_UBSAN)
Dave Rodgmanfdd967e2022-11-22 18:55:17 +000092#define MBEDTLS_ALLOW_UNALIGNED_ACCESS
93#endif
94
Joe Subbiani50dde562021-06-22 15:51:53 +010095/** Byte Reading Macros
Joe Subbiani6f2bb0c2021-06-24 09:06:23 +010096 *
Joe Subbiani9ab18662021-07-21 16:35:48 +010097 * Given a multi-byte integer \p x, MBEDTLS_BYTE_n retrieves the n-th
Joe Subbianid0687852021-07-21 15:22:47 +010098 * byte from x, where byte 0 is the least significant byte.
Joe Subbiani50dde562021-06-22 15:51:53 +010099 */
Joe Subbiani2194dc42021-07-14 12:31:31 +0100100#define MBEDTLS_BYTE_0( x ) ( (uint8_t) ( ( x ) & 0xff ) )
Joe Subbiani5ecac212021-06-24 13:00:03 +0100101#define MBEDTLS_BYTE_1( x ) ( (uint8_t) ( ( ( x ) >> 8 ) & 0xff ) )
102#define MBEDTLS_BYTE_2( x ) ( (uint8_t) ( ( ( x ) >> 16 ) & 0xff ) )
103#define MBEDTLS_BYTE_3( x ) ( (uint8_t) ( ( ( x ) >> 24 ) & 0xff ) )
Joe Subbiani2194dc42021-07-14 12:31:31 +0100104#define MBEDTLS_BYTE_4( x ) ( (uint8_t) ( ( ( x ) >> 32 ) & 0xff ) )
105#define MBEDTLS_BYTE_5( x ) ( (uint8_t) ( ( ( x ) >> 40 ) & 0xff ) )
106#define MBEDTLS_BYTE_6( x ) ( (uint8_t) ( ( ( x ) >> 48 ) & 0xff ) )
107#define MBEDTLS_BYTE_7( x ) ( (uint8_t) ( ( ( x ) >> 56 ) & 0xff ) )
Joe Subbianicd84d762021-07-08 14:59:52 +0100108
Joe Subbiani394bdd62021-07-07 15:16:56 +0100109/**
Joe Subbianif5462d92021-07-13 12:13:19 +0100110 * Get the unsigned 32 bits integer corresponding to four bytes in
Joe Subbiani635231a2021-07-14 11:53:07 +0100111 * big-endian order (MSB first).
Joe Subbiani394bdd62021-07-07 15:16:56 +0100112 *
Joe Subbiani635231a2021-07-14 11:53:07 +0100113 * \param data Base address of the memory to get the four bytes from.
Jerry Yu29287a42021-10-28 10:26:13 +0800114 * \param offset Offset from \p data of the first and most significant
Joe Subbianif5462d92021-07-13 12:13:19 +0100115 * byte of the four bytes to build the 32 bits unsigned
Joe Subbiani635231a2021-07-14 11:53:07 +0100116 * integer from.
Joe Subbiani6a506312021-07-07 16:56:29 +0100117 */
118#ifndef MBEDTLS_GET_UINT32_BE
Joe Subbiani5241e342021-07-19 15:29:18 +0100119#define MBEDTLS_GET_UINT32_BE( data , offset ) \
120 ( \
121 ( (uint32_t) ( data )[( offset ) ] << 24 ) \
122 | ( (uint32_t) ( data )[( offset ) + 1] << 16 ) \
123 | ( (uint32_t) ( data )[( offset ) + 2] << 8 ) \
124 | ( (uint32_t) ( data )[( offset ) + 3] ) \
Joe Subbiani6a506312021-07-07 16:56:29 +0100125 )
126#endif
127
128/**
Joe Subbiani635231a2021-07-14 11:53:07 +0100129 * Put in memory a 32 bits unsigned integer in big-endian order.
Joe Subbiani6a506312021-07-07 16:56:29 +0100130 *
Joe Subbianif5462d92021-07-13 12:13:19 +0100131 * \param n 32 bits unsigned integer to put in memory.
132 * \param data Base address of the memory where to put the 32
Joe Subbiani635231a2021-07-14 11:53:07 +0100133 * bits unsigned integer in.
Jerry Yu29287a42021-10-28 10:26:13 +0800134 * \param offset Offset from \p data where to put the most significant
Joe Subbiani635231a2021-07-14 11:53:07 +0100135 * byte of the 32 bits unsigned integer \p n.
Joe Subbiani394bdd62021-07-07 15:16:56 +0100136 */
Joe Subbiani5ecac212021-06-24 13:00:03 +0100137#ifndef MBEDTLS_PUT_UINT32_BE
Joe Subbiani5241e342021-07-19 15:29:18 +0100138#define MBEDTLS_PUT_UINT32_BE( n, data, offset ) \
139{ \
140 ( data )[( offset ) ] = MBEDTLS_BYTE_3( n ); \
141 ( data )[( offset ) + 1] = MBEDTLS_BYTE_2( n ); \
142 ( data )[( offset ) + 2] = MBEDTLS_BYTE_1( n ); \
143 ( data )[( offset ) + 3] = MBEDTLS_BYTE_0( n ); \
144}
Joe Subbiani30d974c2021-06-23 11:49:03 +0100145#endif
146
Joe Subbiani394bdd62021-07-07 15:16:56 +0100147/**
Joe Subbianif5462d92021-07-13 12:13:19 +0100148 * Get the unsigned 32 bits integer corresponding to four bytes in
Joe Subbiani635231a2021-07-14 11:53:07 +0100149 * little-endian order (LSB first).
Joe Subbiani6a506312021-07-07 16:56:29 +0100150 *
Joe Subbiani635231a2021-07-14 11:53:07 +0100151 * \param data Base address of the memory to get the four bytes from.
Jerry Yu29287a42021-10-28 10:26:13 +0800152 * \param offset Offset from \p data of the first and least significant
Joe Subbianif5462d92021-07-13 12:13:19 +0100153 * byte of the four bytes to build the 32 bits unsigned
Joe Subbiani635231a2021-07-14 11:53:07 +0100154 * integer from.
Joe Subbiani54c61342021-06-23 12:16:47 +0100155 */
Joe Subbiani5ecac212021-06-24 13:00:03 +0100156#ifndef MBEDTLS_GET_UINT32_LE
Joe Subbiani5241e342021-07-19 15:29:18 +0100157#define MBEDTLS_GET_UINT32_LE( data, offset ) \
158 ( \
159 ( (uint32_t) ( data )[( offset ) ] ) \
160 | ( (uint32_t) ( data )[( offset ) + 1] << 8 ) \
161 | ( (uint32_t) ( data )[( offset ) + 2] << 16 ) \
162 | ( (uint32_t) ( data )[( offset ) + 3] << 24 ) \
Joe Subbiani6a506312021-07-07 16:56:29 +0100163 )
Joe Subbiani54c61342021-06-23 12:16:47 +0100164#endif
165
Joe Subbiani6a506312021-07-07 16:56:29 +0100166/**
Joe Subbiani635231a2021-07-14 11:53:07 +0100167 * Put in memory a 32 bits unsigned integer in little-endian order.
Joe Subbiani6a506312021-07-07 16:56:29 +0100168 *
Joe Subbianif5462d92021-07-13 12:13:19 +0100169 * \param n 32 bits unsigned integer to put in memory.
170 * \param data Base address of the memory where to put the 32
Joe Subbiani635231a2021-07-14 11:53:07 +0100171 * bits unsigned integer in.
Jerry Yu29287a42021-10-28 10:26:13 +0800172 * \param offset Offset from \p data where to put the least significant
Joe Subbiani635231a2021-07-14 11:53:07 +0100173 * byte of the 32 bits unsigned integer \p n.
Joe Subbiani6a506312021-07-07 16:56:29 +0100174 */
Joe Subbiani5ecac212021-06-24 13:00:03 +0100175#ifndef MBEDTLS_PUT_UINT32_LE
Joe Subbiani5241e342021-07-19 15:29:18 +0100176#define MBEDTLS_PUT_UINT32_LE( n, data, offset ) \
177{ \
178 ( data )[( offset ) ] = MBEDTLS_BYTE_0( n ); \
179 ( data )[( offset ) + 1] = MBEDTLS_BYTE_1( n ); \
180 ( data )[( offset ) + 2] = MBEDTLS_BYTE_2( n ); \
181 ( data )[( offset ) + 3] = MBEDTLS_BYTE_3( n ); \
182}
Joe Subbiani54c61342021-06-23 12:16:47 +0100183#endif
184
Joe Subbiani6f2bb0c2021-06-24 09:06:23 +0100185/**
Joe Subbianibf7ea842021-07-14 12:05:51 +0100186 * Get the unsigned 16 bits integer corresponding to two bytes in
Joe Subbiani635231a2021-07-14 11:53:07 +0100187 * little-endian order (LSB first).
Joe Subbiani6a506312021-07-07 16:56:29 +0100188 *
Joe Subbianibf7ea842021-07-14 12:05:51 +0100189 * \param data Base address of the memory to get the two bytes from.
Jerry Yu29287a42021-10-28 10:26:13 +0800190 * \param offset Offset from \p data of the first and least significant
Joe Subbianibf7ea842021-07-14 12:05:51 +0100191 * byte of the two bytes to build the 16 bits unsigned
Joe Subbiani635231a2021-07-14 11:53:07 +0100192 * integer from.
Joe Subbiani3b394502021-06-23 11:23:44 +0100193 */
Joe Subbiani6a506312021-07-07 16:56:29 +0100194#ifndef MBEDTLS_GET_UINT16_LE
Joe Subbiani5241e342021-07-19 15:29:18 +0100195#define MBEDTLS_GET_UINT16_LE( data, offset ) \
196 ( \
197 ( (uint16_t) ( data )[( offset ) ] ) \
198 | ( (uint16_t) ( data )[( offset ) + 1] << 8 ) \
Joe Subbiani3b394502021-06-23 11:23:44 +0100199 )
Joe Subbiani6a506312021-07-07 16:56:29 +0100200#endif
Joe Subbiani3b394502021-06-23 11:23:44 +0100201
Joe Subbiani394bdd62021-07-07 15:16:56 +0100202/**
Joe Subbiani635231a2021-07-14 11:53:07 +0100203 * Put in memory a 16 bits unsigned integer in little-endian order.
Joe Subbiani394bdd62021-07-07 15:16:56 +0100204 *
Joe Subbianif5462d92021-07-13 12:13:19 +0100205 * \param n 16 bits unsigned integer to put in memory.
206 * \param data Base address of the memory where to put the 16
Joe Subbiani635231a2021-07-14 11:53:07 +0100207 * bits unsigned integer in.
Jerry Yu29287a42021-10-28 10:26:13 +0800208 * \param offset Offset from \p data where to put the least significant
Joe Subbiani635231a2021-07-14 11:53:07 +0100209 * byte of the 16 bits unsigned integer \p n.
Joe Subbiani394bdd62021-07-07 15:16:56 +0100210 */
Joe Subbiani9fa9ac32021-07-05 15:37:39 +0100211#ifndef MBEDTLS_PUT_UINT16_LE
Joe Subbiani5241e342021-07-19 15:29:18 +0100212#define MBEDTLS_PUT_UINT16_LE( n, data, offset ) \
213{ \
214 ( data )[( offset ) ] = MBEDTLS_BYTE_0( n ); \
215 ( data )[( offset ) + 1] = MBEDTLS_BYTE_1( n ); \
Joe Subbiani9fa9ac32021-07-05 15:37:39 +0100216}
217#endif
218
Joe Subbiani99edd6c2021-07-16 12:29:49 +0100219/**
Joe Subbiani6dd73642021-07-19 11:56:54 +0100220 * Get the unsigned 16 bits integer corresponding to two bytes in
Joe Subbiani5241e342021-07-19 15:29:18 +0100221 * big-endian order (MSB first).
Joe Subbiani6dd73642021-07-19 11:56:54 +0100222 *
223 * \param data Base address of the memory to get the two bytes from.
Jerry Yu29287a42021-10-28 10:26:13 +0800224 * \param offset Offset from \p data of the first and most significant
Joe Subbiani6dd73642021-07-19 11:56:54 +0100225 * byte of the two bytes to build the 16 bits unsigned
226 * integer from.
227 */
228#ifndef MBEDTLS_GET_UINT16_BE
Joe Subbiani5241e342021-07-19 15:29:18 +0100229#define MBEDTLS_GET_UINT16_BE( data, offset ) \
230 ( \
231 ( (uint16_t) ( data )[( offset ) ] << 8 ) \
232 | ( (uint16_t) ( data )[( offset ) + 1] ) \
Joe Subbiani6dd73642021-07-19 11:56:54 +0100233 )
234#endif
235
236/**
237 * Put in memory a 16 bits unsigned integer in big-endian order.
238 *
239 * \param n 16 bits unsigned integer to put in memory.
240 * \param data Base address of the memory where to put the 16
241 * bits unsigned integer in.
Jerry Yu29287a42021-10-28 10:26:13 +0800242 * \param offset Offset from \p data where to put the most significant
Joe Subbiani6dd73642021-07-19 11:56:54 +0100243 * byte of the 16 bits unsigned integer \p n.
244 */
245#ifndef MBEDTLS_PUT_UINT16_BE
Joe Subbiani5241e342021-07-19 15:29:18 +0100246#define MBEDTLS_PUT_UINT16_BE( n, data, offset ) \
247{ \
248 ( data )[( offset ) ] = MBEDTLS_BYTE_1( n ); \
249 ( data )[( offset ) + 1] = MBEDTLS_BYTE_0( n ); \
Joe Subbiani6dd73642021-07-19 11:56:54 +0100250}
251#endif
252
253/**
Jerry Yuf3f5c212021-10-27 17:05:49 +0800254 * Get the unsigned 24 bits integer corresponding to three bytes in
Jerry Yu643d1162021-10-27 13:52:04 +0800255 * big-endian order (MSB first).
256 *
Jerry Yuf3f5c212021-10-27 17:05:49 +0800257 * \param data Base address of the memory to get the three bytes from.
258 * \param offset Offset from \p data of the first and most significant
259 * byte of the three bytes to build the 24 bits unsigned
Jerry Yu643d1162021-10-27 13:52:04 +0800260 * integer from.
261 */
262#ifndef MBEDTLS_GET_UINT24_BE
263#define MBEDTLS_GET_UINT24_BE( data , offset ) \
264 ( \
265 ( (uint32_t) ( data )[( offset ) ] << 16 ) \
266 | ( (uint32_t) ( data )[( offset ) + 1] << 8 ) \
267 | ( (uint32_t) ( data )[( offset ) + 2] ) \
268 )
269#endif
270
271/**
272 * Put in memory a 24 bits unsigned integer in big-endian order.
273 *
274 * \param n 24 bits unsigned integer to put in memory.
275 * \param data Base address of the memory where to put the 24
276 * bits unsigned integer in.
Jerry Yuf3f5c212021-10-27 17:05:49 +0800277 * \param offset Offset from \p data where to put the most significant
Jerry Yu643d1162021-10-27 13:52:04 +0800278 * byte of the 24 bits unsigned integer \p n.
279 */
280#ifndef MBEDTLS_PUT_UINT24_BE
281#define MBEDTLS_PUT_UINT24_BE( n, data, offset ) \
282{ \
283 ( data )[( offset ) ] = MBEDTLS_BYTE_2( n ); \
284 ( data )[( offset ) + 1] = MBEDTLS_BYTE_1( n ); \
285 ( data )[( offset ) + 2] = MBEDTLS_BYTE_0( n ); \
286}
287#endif
288
289/**
Jerry Yuf3f5c212021-10-27 17:05:49 +0800290 * Get the unsigned 24 bits integer corresponding to three bytes in
Jerry Yu643d1162021-10-27 13:52:04 +0800291 * little-endian order (LSB first).
292 *
Jerry Yuf3f5c212021-10-27 17:05:49 +0800293 * \param data Base address of the memory to get the three bytes from.
294 * \param offset Offset from \p data of the first and least significant
295 * byte of the three bytes to build the 24 bits unsigned
Jerry Yu643d1162021-10-27 13:52:04 +0800296 * integer from.
297 */
298#ifndef MBEDTLS_GET_UINT24_LE
299#define MBEDTLS_GET_UINT24_LE( data, offset ) \
300 ( \
301 ( (uint32_t) ( data )[( offset ) ] ) \
302 | ( (uint32_t) ( data )[( offset ) + 1] << 8 ) \
303 | ( (uint32_t) ( data )[( offset ) + 2] << 16 ) \
304 )
305#endif
306
307/**
308 * Put in memory a 24 bits unsigned integer in little-endian order.
309 *
310 * \param n 24 bits unsigned integer to put in memory.
311 * \param data Base address of the memory where to put the 24
312 * bits unsigned integer in.
Jerry Yuf3f5c212021-10-27 17:05:49 +0800313 * \param offset Offset from \p data where to put the least significant
Jerry Yu643d1162021-10-27 13:52:04 +0800314 * byte of the 24 bits unsigned integer \p n.
315 */
316#ifndef MBEDTLS_PUT_UINT24_LE
317#define MBEDTLS_PUT_UINT24_LE( n, data, offset ) \
318{ \
319 ( data )[( offset ) ] = MBEDTLS_BYTE_0( n ); \
320 ( data )[( offset ) + 1] = MBEDTLS_BYTE_1( n ); \
321 ( data )[( offset ) + 2] = MBEDTLS_BYTE_2( n ); \
322}
323#endif
324
325/**
Joe Subbiani99edd6c2021-07-16 12:29:49 +0100326 * Get the unsigned 64 bits integer corresponding to eight bytes in
327 * big-endian order (MSB first).
328 *
329 * \param data Base address of the memory to get the eight bytes from.
Jerry Yu29287a42021-10-28 10:26:13 +0800330 * \param offset Offset from \p data of the first and most significant
Joe Subbiani99edd6c2021-07-16 12:29:49 +0100331 * byte of the eight bytes to build the 64 bits unsigned
332 * integer from.
333 */
334#ifndef MBEDTLS_GET_UINT64_BE
Joe Subbiani5241e342021-07-19 15:29:18 +0100335#define MBEDTLS_GET_UINT64_BE( data, offset ) \
336 ( \
337 ( (uint64_t) ( data )[( offset ) ] << 56 ) \
338 | ( (uint64_t) ( data )[( offset ) + 1] << 48 ) \
339 | ( (uint64_t) ( data )[( offset ) + 2] << 40 ) \
340 | ( (uint64_t) ( data )[( offset ) + 3] << 32 ) \
341 | ( (uint64_t) ( data )[( offset ) + 4] << 24 ) \
342 | ( (uint64_t) ( data )[( offset ) + 5] << 16 ) \
343 | ( (uint64_t) ( data )[( offset ) + 6] << 8 ) \
344 | ( (uint64_t) ( data )[( offset ) + 7] ) \
Joe Subbiani99edd6c2021-07-16 12:29:49 +0100345 )
346#endif
347
348/**
349 * Put in memory a 64 bits unsigned integer in big-endian order.
350 *
351 * \param n 64 bits unsigned integer to put in memory.
352 * \param data Base address of the memory where to put the 64
353 * bits unsigned integer in.
Jerry Yu29287a42021-10-28 10:26:13 +0800354 * \param offset Offset from \p data where to put the most significant
Joe Subbiani99edd6c2021-07-16 12:29:49 +0100355 * byte of the 64 bits unsigned integer \p n.
356 */
357#ifndef MBEDTLS_PUT_UINT64_BE
Joe Subbiani5241e342021-07-19 15:29:18 +0100358#define MBEDTLS_PUT_UINT64_BE( n, data, offset ) \
359{ \
360 ( data )[( offset ) ] = MBEDTLS_BYTE_7( n ); \
361 ( data )[( offset ) + 1] = MBEDTLS_BYTE_6( n ); \
362 ( data )[( offset ) + 2] = MBEDTLS_BYTE_5( n ); \
363 ( data )[( offset ) + 3] = MBEDTLS_BYTE_4( n ); \
364 ( data )[( offset ) + 4] = MBEDTLS_BYTE_3( n ); \
365 ( data )[( offset ) + 5] = MBEDTLS_BYTE_2( n ); \
366 ( data )[( offset ) + 6] = MBEDTLS_BYTE_1( n ); \
367 ( data )[( offset ) + 7] = MBEDTLS_BYTE_0( n ); \
Joe Subbiani99edd6c2021-07-16 12:29:49 +0100368}
369#endif
370
371/**
372 * Get the unsigned 64 bits integer corresponding to eight bytes in
373 * little-endian order (LSB first).
374 *
375 * \param data Base address of the memory to get the eight bytes from.
Jerry Yu29287a42021-10-28 10:26:13 +0800376 * \param offset Offset from \p data of the first and least significant
Joe Subbiani99edd6c2021-07-16 12:29:49 +0100377 * byte of the eight bytes to build the 64 bits unsigned
378 * integer from.
379 */
380#ifndef MBEDTLS_GET_UINT64_LE
Joe Subbiani5241e342021-07-19 15:29:18 +0100381#define MBEDTLS_GET_UINT64_LE( data, offset ) \
382 ( \
383 ( (uint64_t) ( data )[( offset ) + 7] << 56 ) \
384 | ( (uint64_t) ( data )[( offset ) + 6] << 48 ) \
385 | ( (uint64_t) ( data )[( offset ) + 5] << 40 ) \
386 | ( (uint64_t) ( data )[( offset ) + 4] << 32 ) \
387 | ( (uint64_t) ( data )[( offset ) + 3] << 24 ) \
388 | ( (uint64_t) ( data )[( offset ) + 2] << 16 ) \
389 | ( (uint64_t) ( data )[( offset ) + 1] << 8 ) \
390 | ( (uint64_t) ( data )[( offset ) ] ) \
Joe Subbiani99edd6c2021-07-16 12:29:49 +0100391 )
392#endif
393
394/**
395 * Put in memory a 64 bits unsigned integer in little-endian order.
396 *
397 * \param n 64 bits unsigned integer to put in memory.
398 * \param data Base address of the memory where to put the 64
399 * bits unsigned integer in.
Jerry Yu29287a42021-10-28 10:26:13 +0800400 * \param offset Offset from \p data where to put the least significant
Joe Subbiani99edd6c2021-07-16 12:29:49 +0100401 * byte of the 64 bits unsigned integer \p n.
402 */
403#ifndef MBEDTLS_PUT_UINT64_LE
Joe Subbiani5241e342021-07-19 15:29:18 +0100404#define MBEDTLS_PUT_UINT64_LE( n, data, offset ) \
405{ \
406 ( data )[( offset ) ] = MBEDTLS_BYTE_0( n ); \
407 ( data )[( offset ) + 1] = MBEDTLS_BYTE_1( n ); \
408 ( data )[( offset ) + 2] = MBEDTLS_BYTE_2( n ); \
409 ( data )[( offset ) + 3] = MBEDTLS_BYTE_3( n ); \
410 ( data )[( offset ) + 4] = MBEDTLS_BYTE_4( n ); \
411 ( data )[( offset ) + 5] = MBEDTLS_BYTE_5( n ); \
412 ( data )[( offset ) + 6] = MBEDTLS_BYTE_6( n ); \
413 ( data )[( offset ) + 7] = MBEDTLS_BYTE_7( n ); \
Joe Subbiani99edd6c2021-07-16 12:29:49 +0100414}
415#endif
Joe Subbiani9fa9ac32021-07-05 15:37:39 +0100416
Dave Rodgmanc3d80412022-11-22 15:01:39 +0000417/**
418 * Perform a fast block XOR operation, such that
419 * r[i] = a[i] ^ b[i] where 0 <= i < n
420 *
421 * \param r Pointer to result (buffer of at least \p n bytes). \p r
422 * may be equal to either \p a or \p b, but behaviour when
423 * it overlaps in other ways is undefined.
424 * \param a Pointer to input (buffer of at least \p n bytes)
425 * \param b Pointer to input (buffer of at least \p n bytes)
426 * \param n Number of bytes to process.
427 */
Dave Rodgman3c8eb7e2022-11-23 14:50:03 +0000428inline void mbedtls_xor( unsigned char *r, unsigned char const *a, unsigned char const *b, size_t n )
Dave Rodgmanc3d80412022-11-22 15:01:39 +0000429{
Dave Rodgmanfdd967e2022-11-22 18:55:17 +0000430#if defined(MBEDTLS_ALLOW_UNALIGNED_ACCESS)
Dave Rodgmanf9a1c372022-11-23 14:02:00 +0000431 uint32_t *a32 = (uint32_t *)a;
432 uint32_t *b32 = (uint32_t *)b;
433 uint32_t *r32 = (uint32_t *)r;
434 for ( size_t i = 0; i < ( n >> 2 ); i++ )
Dave Rodgmanc3d80412022-11-22 15:01:39 +0000435 {
436 r32[i] = a32[i] ^ b32[i];
437 }
Dave Rodgmanf9a1c372022-11-23 14:02:00 +0000438 for ( size_t i = n - ( n % 4 ) ; i < n; i++ )
Dave Rodgmanc3d80412022-11-22 15:01:39 +0000439 {
440 r[i] = a[i] ^ b[i];
441 }
Dave Rodgmanfdd967e2022-11-22 18:55:17 +0000442#else
443 for ( size_t i = 0; i < n; i++ )
444 {
445 r[i] = a[i] ^ b[i];
446 }
447#endif
Dave Rodgmanc3d80412022-11-22 15:01:39 +0000448}
449
Jerry Yu6c983522021-09-24 12:45:36 +0800450/* Fix MSVC C99 compatible issue
451 * MSVC support __func__ from visual studio 2015( 1900 )
452 * Use MSVC predefine macro to avoid name check fail.
453 */
454#if (defined(_MSC_VER) && ( _MSC_VER <= 1900 ))
Jerry Yud52398d2021-09-28 16:13:44 +0800455#define /*no-check-names*/ __func__ __FUNCTION__
Jerry Yu6c983522021-09-24 12:45:36 +0800456#endif
457
Gilles Peskinec4672fd2019-09-11 13:39:11 +0200458#endif /* MBEDTLS_LIBRARY_COMMON_H */