blob: 3925a79ce590b7bf3740111645e664bcaa62c967 [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
Dave Rodgmanfdd967e2022-11-22 18:55:17 +000072/** Detect architectures where unaligned memory accesses are safe and performant.
73 *
74 * This list is incomplete.
75 */
Dave Rodgman1bab27f2022-11-23 16:51:59 +000076#if (defined(__i386__) || defined(__amd64__) || defined( __x86_64__) \
Dave Rodgmanfdd967e2022-11-22 18:55:17 +000077 || defined(__ARM_FEATURE_UNALIGNED) \
78 || defined(__aarch64__) \
79 || defined(__ARM_ARCH_8__) || defined(__ARM_ARCH_8A__) || defined(__ARM_ARCH_8M__) \
Dave Rodgman1bab27f2022-11-23 16:51:59 +000080 || defined(__ARM_ARCH_7A__)) \
81 && (!(defined(__has_feature) && __has_feature(undefined_behavior_sanitizer)))
Dave Rodgmanfdd967e2022-11-22 18:55:17 +000082#define MBEDTLS_ALLOW_UNALIGNED_ACCESS
83#endif
84
Joe Subbiani50dde562021-06-22 15:51:53 +010085/** Byte Reading Macros
Joe Subbiani6f2bb0c2021-06-24 09:06:23 +010086 *
Joe Subbiani9ab18662021-07-21 16:35:48 +010087 * Given a multi-byte integer \p x, MBEDTLS_BYTE_n retrieves the n-th
Joe Subbianid0687852021-07-21 15:22:47 +010088 * byte from x, where byte 0 is the least significant byte.
Joe Subbiani50dde562021-06-22 15:51:53 +010089 */
Joe Subbiani2194dc42021-07-14 12:31:31 +010090#define MBEDTLS_BYTE_0( x ) ( (uint8_t) ( ( x ) & 0xff ) )
Joe Subbiani5ecac212021-06-24 13:00:03 +010091#define MBEDTLS_BYTE_1( x ) ( (uint8_t) ( ( ( x ) >> 8 ) & 0xff ) )
92#define MBEDTLS_BYTE_2( x ) ( (uint8_t) ( ( ( x ) >> 16 ) & 0xff ) )
93#define MBEDTLS_BYTE_3( x ) ( (uint8_t) ( ( ( x ) >> 24 ) & 0xff ) )
Joe Subbiani2194dc42021-07-14 12:31:31 +010094#define MBEDTLS_BYTE_4( x ) ( (uint8_t) ( ( ( x ) >> 32 ) & 0xff ) )
95#define MBEDTLS_BYTE_5( x ) ( (uint8_t) ( ( ( x ) >> 40 ) & 0xff ) )
96#define MBEDTLS_BYTE_6( x ) ( (uint8_t) ( ( ( x ) >> 48 ) & 0xff ) )
97#define MBEDTLS_BYTE_7( x ) ( (uint8_t) ( ( ( x ) >> 56 ) & 0xff ) )
Joe Subbianicd84d762021-07-08 14:59:52 +010098
Joe Subbiani394bdd62021-07-07 15:16:56 +010099/**
Joe Subbianif5462d92021-07-13 12:13:19 +0100100 * Get the unsigned 32 bits integer corresponding to four bytes in
Joe Subbiani635231a2021-07-14 11:53:07 +0100101 * big-endian order (MSB first).
Joe Subbiani394bdd62021-07-07 15:16:56 +0100102 *
Joe Subbiani635231a2021-07-14 11:53:07 +0100103 * \param data Base address of the memory to get the four bytes from.
Jerry Yu29287a42021-10-28 10:26:13 +0800104 * \param offset Offset from \p data of the first and most significant
Joe Subbianif5462d92021-07-13 12:13:19 +0100105 * byte of the four bytes to build the 32 bits unsigned
Joe Subbiani635231a2021-07-14 11:53:07 +0100106 * integer from.
Joe Subbiani6a506312021-07-07 16:56:29 +0100107 */
108#ifndef MBEDTLS_GET_UINT32_BE
Joe Subbiani5241e342021-07-19 15:29:18 +0100109#define MBEDTLS_GET_UINT32_BE( data , offset ) \
110 ( \
111 ( (uint32_t) ( data )[( offset ) ] << 24 ) \
112 | ( (uint32_t) ( data )[( offset ) + 1] << 16 ) \
113 | ( (uint32_t) ( data )[( offset ) + 2] << 8 ) \
114 | ( (uint32_t) ( data )[( offset ) + 3] ) \
Joe Subbiani6a506312021-07-07 16:56:29 +0100115 )
116#endif
117
118/**
Joe Subbiani635231a2021-07-14 11:53:07 +0100119 * Put in memory a 32 bits unsigned integer in big-endian order.
Joe Subbiani6a506312021-07-07 16:56:29 +0100120 *
Joe Subbianif5462d92021-07-13 12:13:19 +0100121 * \param n 32 bits unsigned integer to put in memory.
122 * \param data Base address of the memory where to put the 32
Joe Subbiani635231a2021-07-14 11:53:07 +0100123 * bits unsigned integer in.
Jerry Yu29287a42021-10-28 10:26:13 +0800124 * \param offset Offset from \p data where to put the most significant
Joe Subbiani635231a2021-07-14 11:53:07 +0100125 * byte of the 32 bits unsigned integer \p n.
Joe Subbiani394bdd62021-07-07 15:16:56 +0100126 */
Joe Subbiani5ecac212021-06-24 13:00:03 +0100127#ifndef MBEDTLS_PUT_UINT32_BE
Joe Subbiani5241e342021-07-19 15:29:18 +0100128#define MBEDTLS_PUT_UINT32_BE( n, data, offset ) \
129{ \
130 ( data )[( offset ) ] = MBEDTLS_BYTE_3( n ); \
131 ( data )[( offset ) + 1] = MBEDTLS_BYTE_2( n ); \
132 ( data )[( offset ) + 2] = MBEDTLS_BYTE_1( n ); \
133 ( data )[( offset ) + 3] = MBEDTLS_BYTE_0( n ); \
134}
Joe Subbiani30d974c2021-06-23 11:49:03 +0100135#endif
136
Joe Subbiani394bdd62021-07-07 15:16:56 +0100137/**
Joe Subbianif5462d92021-07-13 12:13:19 +0100138 * Get the unsigned 32 bits integer corresponding to four bytes in
Joe Subbiani635231a2021-07-14 11:53:07 +0100139 * little-endian order (LSB first).
Joe Subbiani6a506312021-07-07 16:56:29 +0100140 *
Joe Subbiani635231a2021-07-14 11:53:07 +0100141 * \param data Base address of the memory to get the four bytes from.
Jerry Yu29287a42021-10-28 10:26:13 +0800142 * \param offset Offset from \p data of the first and least significant
Joe Subbianif5462d92021-07-13 12:13:19 +0100143 * byte of the four bytes to build the 32 bits unsigned
Joe Subbiani635231a2021-07-14 11:53:07 +0100144 * integer from.
Joe Subbiani54c61342021-06-23 12:16:47 +0100145 */
Joe Subbiani5ecac212021-06-24 13:00:03 +0100146#ifndef MBEDTLS_GET_UINT32_LE
Joe Subbiani5241e342021-07-19 15:29:18 +0100147#define MBEDTLS_GET_UINT32_LE( data, offset ) \
148 ( \
149 ( (uint32_t) ( data )[( offset ) ] ) \
150 | ( (uint32_t) ( data )[( offset ) + 1] << 8 ) \
151 | ( (uint32_t) ( data )[( offset ) + 2] << 16 ) \
152 | ( (uint32_t) ( data )[( offset ) + 3] << 24 ) \
Joe Subbiani6a506312021-07-07 16:56:29 +0100153 )
Joe Subbiani54c61342021-06-23 12:16:47 +0100154#endif
155
Joe Subbiani6a506312021-07-07 16:56:29 +0100156/**
Joe Subbiani635231a2021-07-14 11:53:07 +0100157 * Put in memory a 32 bits unsigned integer in little-endian order.
Joe Subbiani6a506312021-07-07 16:56:29 +0100158 *
Joe Subbianif5462d92021-07-13 12:13:19 +0100159 * \param n 32 bits unsigned integer to put in memory.
160 * \param data Base address of the memory where to put the 32
Joe Subbiani635231a2021-07-14 11:53:07 +0100161 * bits unsigned integer in.
Jerry Yu29287a42021-10-28 10:26:13 +0800162 * \param offset Offset from \p data where to put the least significant
Joe Subbiani635231a2021-07-14 11:53:07 +0100163 * byte of the 32 bits unsigned integer \p n.
Joe Subbiani6a506312021-07-07 16:56:29 +0100164 */
Joe Subbiani5ecac212021-06-24 13:00:03 +0100165#ifndef MBEDTLS_PUT_UINT32_LE
Joe Subbiani5241e342021-07-19 15:29:18 +0100166#define MBEDTLS_PUT_UINT32_LE( n, data, offset ) \
167{ \
168 ( data )[( offset ) ] = MBEDTLS_BYTE_0( n ); \
169 ( data )[( offset ) + 1] = MBEDTLS_BYTE_1( n ); \
170 ( data )[( offset ) + 2] = MBEDTLS_BYTE_2( n ); \
171 ( data )[( offset ) + 3] = MBEDTLS_BYTE_3( n ); \
172}
Joe Subbiani54c61342021-06-23 12:16:47 +0100173#endif
174
Joe Subbiani6f2bb0c2021-06-24 09:06:23 +0100175/**
Joe Subbianibf7ea842021-07-14 12:05:51 +0100176 * Get the unsigned 16 bits integer corresponding to two bytes in
Joe Subbiani635231a2021-07-14 11:53:07 +0100177 * little-endian order (LSB first).
Joe Subbiani6a506312021-07-07 16:56:29 +0100178 *
Joe Subbianibf7ea842021-07-14 12:05:51 +0100179 * \param data Base address of the memory to get the two bytes from.
Jerry Yu29287a42021-10-28 10:26:13 +0800180 * \param offset Offset from \p data of the first and least significant
Joe Subbianibf7ea842021-07-14 12:05:51 +0100181 * byte of the two bytes to build the 16 bits unsigned
Joe Subbiani635231a2021-07-14 11:53:07 +0100182 * integer from.
Joe Subbiani3b394502021-06-23 11:23:44 +0100183 */
Joe Subbiani6a506312021-07-07 16:56:29 +0100184#ifndef MBEDTLS_GET_UINT16_LE
Joe Subbiani5241e342021-07-19 15:29:18 +0100185#define MBEDTLS_GET_UINT16_LE( data, offset ) \
186 ( \
187 ( (uint16_t) ( data )[( offset ) ] ) \
188 | ( (uint16_t) ( data )[( offset ) + 1] << 8 ) \
Joe Subbiani3b394502021-06-23 11:23:44 +0100189 )
Joe Subbiani6a506312021-07-07 16:56:29 +0100190#endif
Joe Subbiani3b394502021-06-23 11:23:44 +0100191
Joe Subbiani394bdd62021-07-07 15:16:56 +0100192/**
Joe Subbiani635231a2021-07-14 11:53:07 +0100193 * Put in memory a 16 bits unsigned integer in little-endian order.
Joe Subbiani394bdd62021-07-07 15:16:56 +0100194 *
Joe Subbianif5462d92021-07-13 12:13:19 +0100195 * \param n 16 bits unsigned integer to put in memory.
196 * \param data Base address of the memory where to put the 16
Joe Subbiani635231a2021-07-14 11:53:07 +0100197 * bits unsigned integer in.
Jerry Yu29287a42021-10-28 10:26:13 +0800198 * \param offset Offset from \p data where to put the least significant
Joe Subbiani635231a2021-07-14 11:53:07 +0100199 * byte of the 16 bits unsigned integer \p n.
Joe Subbiani394bdd62021-07-07 15:16:56 +0100200 */
Joe Subbiani9fa9ac32021-07-05 15:37:39 +0100201#ifndef MBEDTLS_PUT_UINT16_LE
Joe Subbiani5241e342021-07-19 15:29:18 +0100202#define MBEDTLS_PUT_UINT16_LE( n, data, offset ) \
203{ \
204 ( data )[( offset ) ] = MBEDTLS_BYTE_0( n ); \
205 ( data )[( offset ) + 1] = MBEDTLS_BYTE_1( n ); \
Joe Subbiani9fa9ac32021-07-05 15:37:39 +0100206}
207#endif
208
Joe Subbiani99edd6c2021-07-16 12:29:49 +0100209/**
Joe Subbiani6dd73642021-07-19 11:56:54 +0100210 * Get the unsigned 16 bits integer corresponding to two bytes in
Joe Subbiani5241e342021-07-19 15:29:18 +0100211 * big-endian order (MSB first).
Joe Subbiani6dd73642021-07-19 11:56:54 +0100212 *
213 * \param data Base address of the memory to get the two bytes from.
Jerry Yu29287a42021-10-28 10:26:13 +0800214 * \param offset Offset from \p data of the first and most significant
Joe Subbiani6dd73642021-07-19 11:56:54 +0100215 * byte of the two bytes to build the 16 bits unsigned
216 * integer from.
217 */
218#ifndef MBEDTLS_GET_UINT16_BE
Joe Subbiani5241e342021-07-19 15:29:18 +0100219#define MBEDTLS_GET_UINT16_BE( data, offset ) \
220 ( \
221 ( (uint16_t) ( data )[( offset ) ] << 8 ) \
222 | ( (uint16_t) ( data )[( offset ) + 1] ) \
Joe Subbiani6dd73642021-07-19 11:56:54 +0100223 )
224#endif
225
226/**
227 * Put in memory a 16 bits unsigned integer in big-endian order.
228 *
229 * \param n 16 bits unsigned integer to put in memory.
230 * \param data Base address of the memory where to put the 16
231 * bits unsigned integer in.
Jerry Yu29287a42021-10-28 10:26:13 +0800232 * \param offset Offset from \p data where to put the most significant
Joe Subbiani6dd73642021-07-19 11:56:54 +0100233 * byte of the 16 bits unsigned integer \p n.
234 */
235#ifndef MBEDTLS_PUT_UINT16_BE
Joe Subbiani5241e342021-07-19 15:29:18 +0100236#define MBEDTLS_PUT_UINT16_BE( n, data, offset ) \
237{ \
238 ( data )[( offset ) ] = MBEDTLS_BYTE_1( n ); \
239 ( data )[( offset ) + 1] = MBEDTLS_BYTE_0( n ); \
Joe Subbiani6dd73642021-07-19 11:56:54 +0100240}
241#endif
242
243/**
Jerry Yuf3f5c212021-10-27 17:05:49 +0800244 * Get the unsigned 24 bits integer corresponding to three bytes in
Jerry Yu643d1162021-10-27 13:52:04 +0800245 * big-endian order (MSB first).
246 *
Jerry Yuf3f5c212021-10-27 17:05:49 +0800247 * \param data Base address of the memory to get the three bytes from.
248 * \param offset Offset from \p data of the first and most significant
249 * byte of the three bytes to build the 24 bits unsigned
Jerry Yu643d1162021-10-27 13:52:04 +0800250 * integer from.
251 */
252#ifndef MBEDTLS_GET_UINT24_BE
253#define MBEDTLS_GET_UINT24_BE( data , offset ) \
254 ( \
255 ( (uint32_t) ( data )[( offset ) ] << 16 ) \
256 | ( (uint32_t) ( data )[( offset ) + 1] << 8 ) \
257 | ( (uint32_t) ( data )[( offset ) + 2] ) \
258 )
259#endif
260
261/**
262 * Put in memory a 24 bits unsigned integer in big-endian order.
263 *
264 * \param n 24 bits unsigned integer to put in memory.
265 * \param data Base address of the memory where to put the 24
266 * bits unsigned integer in.
Jerry Yuf3f5c212021-10-27 17:05:49 +0800267 * \param offset Offset from \p data where to put the most significant
Jerry Yu643d1162021-10-27 13:52:04 +0800268 * byte of the 24 bits unsigned integer \p n.
269 */
270#ifndef MBEDTLS_PUT_UINT24_BE
271#define MBEDTLS_PUT_UINT24_BE( n, data, offset ) \
272{ \
273 ( data )[( offset ) ] = MBEDTLS_BYTE_2( n ); \
274 ( data )[( offset ) + 1] = MBEDTLS_BYTE_1( n ); \
275 ( data )[( offset ) + 2] = MBEDTLS_BYTE_0( n ); \
276}
277#endif
278
279/**
Jerry Yuf3f5c212021-10-27 17:05:49 +0800280 * Get the unsigned 24 bits integer corresponding to three bytes in
Jerry Yu643d1162021-10-27 13:52:04 +0800281 * little-endian order (LSB first).
282 *
Jerry Yuf3f5c212021-10-27 17:05:49 +0800283 * \param data Base address of the memory to get the three bytes from.
284 * \param offset Offset from \p data of the first and least significant
285 * byte of the three bytes to build the 24 bits unsigned
Jerry Yu643d1162021-10-27 13:52:04 +0800286 * integer from.
287 */
288#ifndef MBEDTLS_GET_UINT24_LE
289#define MBEDTLS_GET_UINT24_LE( data, offset ) \
290 ( \
291 ( (uint32_t) ( data )[( offset ) ] ) \
292 | ( (uint32_t) ( data )[( offset ) + 1] << 8 ) \
293 | ( (uint32_t) ( data )[( offset ) + 2] << 16 ) \
294 )
295#endif
296
297/**
298 * Put in memory a 24 bits unsigned integer in little-endian order.
299 *
300 * \param n 24 bits unsigned integer to put in memory.
301 * \param data Base address of the memory where to put the 24
302 * bits unsigned integer in.
Jerry Yuf3f5c212021-10-27 17:05:49 +0800303 * \param offset Offset from \p data where to put the least significant
Jerry Yu643d1162021-10-27 13:52:04 +0800304 * byte of the 24 bits unsigned integer \p n.
305 */
306#ifndef MBEDTLS_PUT_UINT24_LE
307#define MBEDTLS_PUT_UINT24_LE( n, data, offset ) \
308{ \
309 ( data )[( offset ) ] = MBEDTLS_BYTE_0( n ); \
310 ( data )[( offset ) + 1] = MBEDTLS_BYTE_1( n ); \
311 ( data )[( offset ) + 2] = MBEDTLS_BYTE_2( n ); \
312}
313#endif
314
315/**
Joe Subbiani99edd6c2021-07-16 12:29:49 +0100316 * Get the unsigned 64 bits integer corresponding to eight bytes in
317 * big-endian order (MSB first).
318 *
319 * \param data Base address of the memory to get the eight bytes from.
Jerry Yu29287a42021-10-28 10:26:13 +0800320 * \param offset Offset from \p data of the first and most significant
Joe Subbiani99edd6c2021-07-16 12:29:49 +0100321 * byte of the eight bytes to build the 64 bits unsigned
322 * integer from.
323 */
324#ifndef MBEDTLS_GET_UINT64_BE
Joe Subbiani5241e342021-07-19 15:29:18 +0100325#define MBEDTLS_GET_UINT64_BE( data, offset ) \
326 ( \
327 ( (uint64_t) ( data )[( offset ) ] << 56 ) \
328 | ( (uint64_t) ( data )[( offset ) + 1] << 48 ) \
329 | ( (uint64_t) ( data )[( offset ) + 2] << 40 ) \
330 | ( (uint64_t) ( data )[( offset ) + 3] << 32 ) \
331 | ( (uint64_t) ( data )[( offset ) + 4] << 24 ) \
332 | ( (uint64_t) ( data )[( offset ) + 5] << 16 ) \
333 | ( (uint64_t) ( data )[( offset ) + 6] << 8 ) \
334 | ( (uint64_t) ( data )[( offset ) + 7] ) \
Joe Subbiani99edd6c2021-07-16 12:29:49 +0100335 )
336#endif
337
338/**
339 * Put in memory a 64 bits unsigned integer in big-endian order.
340 *
341 * \param n 64 bits unsigned integer to put in memory.
342 * \param data Base address of the memory where to put the 64
343 * bits unsigned integer in.
Jerry Yu29287a42021-10-28 10:26:13 +0800344 * \param offset Offset from \p data where to put the most significant
Joe Subbiani99edd6c2021-07-16 12:29:49 +0100345 * byte of the 64 bits unsigned integer \p n.
346 */
347#ifndef MBEDTLS_PUT_UINT64_BE
Joe Subbiani5241e342021-07-19 15:29:18 +0100348#define MBEDTLS_PUT_UINT64_BE( n, data, offset ) \
349{ \
350 ( data )[( offset ) ] = MBEDTLS_BYTE_7( n ); \
351 ( data )[( offset ) + 1] = MBEDTLS_BYTE_6( n ); \
352 ( data )[( offset ) + 2] = MBEDTLS_BYTE_5( n ); \
353 ( data )[( offset ) + 3] = MBEDTLS_BYTE_4( n ); \
354 ( data )[( offset ) + 4] = MBEDTLS_BYTE_3( n ); \
355 ( data )[( offset ) + 5] = MBEDTLS_BYTE_2( n ); \
356 ( data )[( offset ) + 6] = MBEDTLS_BYTE_1( n ); \
357 ( data )[( offset ) + 7] = MBEDTLS_BYTE_0( n ); \
Joe Subbiani99edd6c2021-07-16 12:29:49 +0100358}
359#endif
360
361/**
362 * Get the unsigned 64 bits integer corresponding to eight bytes in
363 * little-endian order (LSB first).
364 *
365 * \param data Base address of the memory to get the eight bytes from.
Jerry Yu29287a42021-10-28 10:26:13 +0800366 * \param offset Offset from \p data of the first and least significant
Joe Subbiani99edd6c2021-07-16 12:29:49 +0100367 * byte of the eight bytes to build the 64 bits unsigned
368 * integer from.
369 */
370#ifndef MBEDTLS_GET_UINT64_LE
Joe Subbiani5241e342021-07-19 15:29:18 +0100371#define MBEDTLS_GET_UINT64_LE( data, offset ) \
372 ( \
373 ( (uint64_t) ( data )[( offset ) + 7] << 56 ) \
374 | ( (uint64_t) ( data )[( offset ) + 6] << 48 ) \
375 | ( (uint64_t) ( data )[( offset ) + 5] << 40 ) \
376 | ( (uint64_t) ( data )[( offset ) + 4] << 32 ) \
377 | ( (uint64_t) ( data )[( offset ) + 3] << 24 ) \
378 | ( (uint64_t) ( data )[( offset ) + 2] << 16 ) \
379 | ( (uint64_t) ( data )[( offset ) + 1] << 8 ) \
380 | ( (uint64_t) ( data )[( offset ) ] ) \
Joe Subbiani99edd6c2021-07-16 12:29:49 +0100381 )
382#endif
383
384/**
385 * Put in memory a 64 bits unsigned integer in little-endian order.
386 *
387 * \param n 64 bits unsigned integer to put in memory.
388 * \param data Base address of the memory where to put the 64
389 * bits unsigned integer in.
Jerry Yu29287a42021-10-28 10:26:13 +0800390 * \param offset Offset from \p data where to put the least significant
Joe Subbiani99edd6c2021-07-16 12:29:49 +0100391 * byte of the 64 bits unsigned integer \p n.
392 */
393#ifndef MBEDTLS_PUT_UINT64_LE
Joe Subbiani5241e342021-07-19 15:29:18 +0100394#define MBEDTLS_PUT_UINT64_LE( n, data, offset ) \
395{ \
396 ( data )[( offset ) ] = MBEDTLS_BYTE_0( n ); \
397 ( data )[( offset ) + 1] = MBEDTLS_BYTE_1( n ); \
398 ( data )[( offset ) + 2] = MBEDTLS_BYTE_2( n ); \
399 ( data )[( offset ) + 3] = MBEDTLS_BYTE_3( n ); \
400 ( data )[( offset ) + 4] = MBEDTLS_BYTE_4( n ); \
401 ( data )[( offset ) + 5] = MBEDTLS_BYTE_5( n ); \
402 ( data )[( offset ) + 6] = MBEDTLS_BYTE_6( n ); \
403 ( data )[( offset ) + 7] = MBEDTLS_BYTE_7( n ); \
Joe Subbiani99edd6c2021-07-16 12:29:49 +0100404}
405#endif
Joe Subbiani9fa9ac32021-07-05 15:37:39 +0100406
Dave Rodgmanc3d80412022-11-22 15:01:39 +0000407/**
408 * Perform a fast block XOR operation, such that
409 * r[i] = a[i] ^ b[i] where 0 <= i < n
410 *
411 * \param r Pointer to result (buffer of at least \p n bytes). \p r
412 * may be equal to either \p a or \p b, but behaviour when
413 * it overlaps in other ways is undefined.
414 * \param a Pointer to input (buffer of at least \p n bytes)
415 * \param b Pointer to input (buffer of at least \p n bytes)
416 * \param n Number of bytes to process.
417 */
Dave Rodgman3c8eb7e2022-11-23 14:50:03 +0000418inline void mbedtls_xor( unsigned char *r, unsigned char const *a, unsigned char const *b, size_t n )
Dave Rodgmanc3d80412022-11-22 15:01:39 +0000419{
Dave Rodgmanfdd967e2022-11-22 18:55:17 +0000420#if defined(MBEDTLS_ALLOW_UNALIGNED_ACCESS)
Dave Rodgmanf9a1c372022-11-23 14:02:00 +0000421 uint32_t *a32 = (uint32_t *)a;
422 uint32_t *b32 = (uint32_t *)b;
423 uint32_t *r32 = (uint32_t *)r;
424 for ( size_t i = 0; i < ( n >> 2 ); i++ )
Dave Rodgmanc3d80412022-11-22 15:01:39 +0000425 {
426 r32[i] = a32[i] ^ b32[i];
427 }
Dave Rodgmanf9a1c372022-11-23 14:02:00 +0000428 for ( size_t i = n - ( n % 4 ) ; i < n; i++ )
Dave Rodgmanc3d80412022-11-22 15:01:39 +0000429 {
430 r[i] = a[i] ^ b[i];
431 }
Dave Rodgmanfdd967e2022-11-22 18:55:17 +0000432#else
433 for ( size_t i = 0; i < n; i++ )
434 {
435 r[i] = a[i] ^ b[i];
436 }
437#endif
Dave Rodgmanc3d80412022-11-22 15:01:39 +0000438}
439
Jerry Yu6c983522021-09-24 12:45:36 +0800440/* Fix MSVC C99 compatible issue
441 * MSVC support __func__ from visual studio 2015( 1900 )
442 * Use MSVC predefine macro to avoid name check fail.
443 */
444#if (defined(_MSC_VER) && ( _MSC_VER <= 1900 ))
Jerry Yud52398d2021-09-28 16:13:44 +0800445#define /*no-check-names*/ __func__ __FUNCTION__
Jerry Yu6c983522021-09-24 12:45:36 +0800446#endif
447
Gilles Peskinec4672fd2019-09-11 13:39:11 +0200448#endif /* MBEDTLS_LIBRARY_COMMON_H */