blob: c3847cdbf682e6b596316a794f3f53f13ff78880 [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 */
76#if defined(__i386__) || defined(__amd64__) || defined( __x86_64__) \
77 || defined(__ARM_FEATURE_UNALIGNED) \
78 || defined(__aarch64__) \
79 || defined(__ARM_ARCH_8__) || defined(__ARM_ARCH_8A__) || defined(__ARM_ARCH_8M__) \
80 || defined(__ARM_ARCH_7A__)
81#define MBEDTLS_ALLOW_UNALIGNED_ACCESS
82#endif
83
Joe Subbiani50dde562021-06-22 15:51:53 +010084/** Byte Reading Macros
Joe Subbiani6f2bb0c2021-06-24 09:06:23 +010085 *
Joe Subbiani9ab18662021-07-21 16:35:48 +010086 * Given a multi-byte integer \p x, MBEDTLS_BYTE_n retrieves the n-th
Joe Subbianid0687852021-07-21 15:22:47 +010087 * byte from x, where byte 0 is the least significant byte.
Joe Subbiani50dde562021-06-22 15:51:53 +010088 */
Joe Subbiani2194dc42021-07-14 12:31:31 +010089#define MBEDTLS_BYTE_0( x ) ( (uint8_t) ( ( x ) & 0xff ) )
Joe Subbiani5ecac212021-06-24 13:00:03 +010090#define MBEDTLS_BYTE_1( x ) ( (uint8_t) ( ( ( x ) >> 8 ) & 0xff ) )
91#define MBEDTLS_BYTE_2( x ) ( (uint8_t) ( ( ( x ) >> 16 ) & 0xff ) )
92#define MBEDTLS_BYTE_3( x ) ( (uint8_t) ( ( ( x ) >> 24 ) & 0xff ) )
Joe Subbiani2194dc42021-07-14 12:31:31 +010093#define MBEDTLS_BYTE_4( x ) ( (uint8_t) ( ( ( x ) >> 32 ) & 0xff ) )
94#define MBEDTLS_BYTE_5( x ) ( (uint8_t) ( ( ( x ) >> 40 ) & 0xff ) )
95#define MBEDTLS_BYTE_6( x ) ( (uint8_t) ( ( ( x ) >> 48 ) & 0xff ) )
96#define MBEDTLS_BYTE_7( x ) ( (uint8_t) ( ( ( x ) >> 56 ) & 0xff ) )
Joe Subbianicd84d762021-07-08 14:59:52 +010097
Joe Subbiani394bdd62021-07-07 15:16:56 +010098/**
Joe Subbianif5462d92021-07-13 12:13:19 +010099 * Get the unsigned 32 bits integer corresponding to four bytes in
Joe Subbiani635231a2021-07-14 11:53:07 +0100100 * big-endian order (MSB first).
Joe Subbiani394bdd62021-07-07 15:16:56 +0100101 *
Joe Subbiani635231a2021-07-14 11:53:07 +0100102 * \param data Base address of the memory to get the four bytes from.
Jerry Yu29287a42021-10-28 10:26:13 +0800103 * \param offset Offset from \p data of the first and most significant
Joe Subbianif5462d92021-07-13 12:13:19 +0100104 * byte of the four bytes to build the 32 bits unsigned
Joe Subbiani635231a2021-07-14 11:53:07 +0100105 * integer from.
Joe Subbiani6a506312021-07-07 16:56:29 +0100106 */
107#ifndef MBEDTLS_GET_UINT32_BE
Joe Subbiani5241e342021-07-19 15:29:18 +0100108#define MBEDTLS_GET_UINT32_BE( data , offset ) \
109 ( \
110 ( (uint32_t) ( data )[( offset ) ] << 24 ) \
111 | ( (uint32_t) ( data )[( offset ) + 1] << 16 ) \
112 | ( (uint32_t) ( data )[( offset ) + 2] << 8 ) \
113 | ( (uint32_t) ( data )[( offset ) + 3] ) \
Joe Subbiani6a506312021-07-07 16:56:29 +0100114 )
115#endif
116
117/**
Joe Subbiani635231a2021-07-14 11:53:07 +0100118 * Put in memory a 32 bits unsigned integer in big-endian order.
Joe Subbiani6a506312021-07-07 16:56:29 +0100119 *
Joe Subbianif5462d92021-07-13 12:13:19 +0100120 * \param n 32 bits unsigned integer to put in memory.
121 * \param data Base address of the memory where to put the 32
Joe Subbiani635231a2021-07-14 11:53:07 +0100122 * bits unsigned integer in.
Jerry Yu29287a42021-10-28 10:26:13 +0800123 * \param offset Offset from \p data where to put the most significant
Joe Subbiani635231a2021-07-14 11:53:07 +0100124 * byte of the 32 bits unsigned integer \p n.
Joe Subbiani394bdd62021-07-07 15:16:56 +0100125 */
Joe Subbiani5ecac212021-06-24 13:00:03 +0100126#ifndef MBEDTLS_PUT_UINT32_BE
Joe Subbiani5241e342021-07-19 15:29:18 +0100127#define MBEDTLS_PUT_UINT32_BE( n, data, offset ) \
128{ \
129 ( data )[( offset ) ] = MBEDTLS_BYTE_3( n ); \
130 ( data )[( offset ) + 1] = MBEDTLS_BYTE_2( n ); \
131 ( data )[( offset ) + 2] = MBEDTLS_BYTE_1( n ); \
132 ( data )[( offset ) + 3] = MBEDTLS_BYTE_0( n ); \
133}
Joe Subbiani30d974c2021-06-23 11:49:03 +0100134#endif
135
Joe Subbiani394bdd62021-07-07 15:16:56 +0100136/**
Joe Subbianif5462d92021-07-13 12:13:19 +0100137 * Get the unsigned 32 bits integer corresponding to four bytes in
Joe Subbiani635231a2021-07-14 11:53:07 +0100138 * little-endian order (LSB first).
Joe Subbiani6a506312021-07-07 16:56:29 +0100139 *
Joe Subbiani635231a2021-07-14 11:53:07 +0100140 * \param data Base address of the memory to get the four bytes from.
Jerry Yu29287a42021-10-28 10:26:13 +0800141 * \param offset Offset from \p data of the first and least significant
Joe Subbianif5462d92021-07-13 12:13:19 +0100142 * byte of the four bytes to build the 32 bits unsigned
Joe Subbiani635231a2021-07-14 11:53:07 +0100143 * integer from.
Joe Subbiani54c61342021-06-23 12:16:47 +0100144 */
Joe Subbiani5ecac212021-06-24 13:00:03 +0100145#ifndef MBEDTLS_GET_UINT32_LE
Joe Subbiani5241e342021-07-19 15:29:18 +0100146#define MBEDTLS_GET_UINT32_LE( data, offset ) \
147 ( \
148 ( (uint32_t) ( data )[( offset ) ] ) \
149 | ( (uint32_t) ( data )[( offset ) + 1] << 8 ) \
150 | ( (uint32_t) ( data )[( offset ) + 2] << 16 ) \
151 | ( (uint32_t) ( data )[( offset ) + 3] << 24 ) \
Joe Subbiani6a506312021-07-07 16:56:29 +0100152 )
Joe Subbiani54c61342021-06-23 12:16:47 +0100153#endif
154
Joe Subbiani6a506312021-07-07 16:56:29 +0100155/**
Joe Subbiani635231a2021-07-14 11:53:07 +0100156 * Put in memory a 32 bits unsigned integer in little-endian order.
Joe Subbiani6a506312021-07-07 16:56:29 +0100157 *
Joe Subbianif5462d92021-07-13 12:13:19 +0100158 * \param n 32 bits unsigned integer to put in memory.
159 * \param data Base address of the memory where to put the 32
Joe Subbiani635231a2021-07-14 11:53:07 +0100160 * bits unsigned integer in.
Jerry Yu29287a42021-10-28 10:26:13 +0800161 * \param offset Offset from \p data where to put the least significant
Joe Subbiani635231a2021-07-14 11:53:07 +0100162 * byte of the 32 bits unsigned integer \p n.
Joe Subbiani6a506312021-07-07 16:56:29 +0100163 */
Joe Subbiani5ecac212021-06-24 13:00:03 +0100164#ifndef MBEDTLS_PUT_UINT32_LE
Joe Subbiani5241e342021-07-19 15:29:18 +0100165#define MBEDTLS_PUT_UINT32_LE( n, data, offset ) \
166{ \
167 ( data )[( offset ) ] = MBEDTLS_BYTE_0( n ); \
168 ( data )[( offset ) + 1] = MBEDTLS_BYTE_1( n ); \
169 ( data )[( offset ) + 2] = MBEDTLS_BYTE_2( n ); \
170 ( data )[( offset ) + 3] = MBEDTLS_BYTE_3( n ); \
171}
Joe Subbiani54c61342021-06-23 12:16:47 +0100172#endif
173
Joe Subbiani6f2bb0c2021-06-24 09:06:23 +0100174/**
Joe Subbianibf7ea842021-07-14 12:05:51 +0100175 * Get the unsigned 16 bits integer corresponding to two bytes in
Joe Subbiani635231a2021-07-14 11:53:07 +0100176 * little-endian order (LSB first).
Joe Subbiani6a506312021-07-07 16:56:29 +0100177 *
Joe Subbianibf7ea842021-07-14 12:05:51 +0100178 * \param data Base address of the memory to get the two bytes from.
Jerry Yu29287a42021-10-28 10:26:13 +0800179 * \param offset Offset from \p data of the first and least significant
Joe Subbianibf7ea842021-07-14 12:05:51 +0100180 * byte of the two bytes to build the 16 bits unsigned
Joe Subbiani635231a2021-07-14 11:53:07 +0100181 * integer from.
Joe Subbiani3b394502021-06-23 11:23:44 +0100182 */
Joe Subbiani6a506312021-07-07 16:56:29 +0100183#ifndef MBEDTLS_GET_UINT16_LE
Joe Subbiani5241e342021-07-19 15:29:18 +0100184#define MBEDTLS_GET_UINT16_LE( data, offset ) \
185 ( \
186 ( (uint16_t) ( data )[( offset ) ] ) \
187 | ( (uint16_t) ( data )[( offset ) + 1] << 8 ) \
Joe Subbiani3b394502021-06-23 11:23:44 +0100188 )
Joe Subbiani6a506312021-07-07 16:56:29 +0100189#endif
Joe Subbiani3b394502021-06-23 11:23:44 +0100190
Joe Subbiani394bdd62021-07-07 15:16:56 +0100191/**
Joe Subbiani635231a2021-07-14 11:53:07 +0100192 * Put in memory a 16 bits unsigned integer in little-endian order.
Joe Subbiani394bdd62021-07-07 15:16:56 +0100193 *
Joe Subbianif5462d92021-07-13 12:13:19 +0100194 * \param n 16 bits unsigned integer to put in memory.
195 * \param data Base address of the memory where to put the 16
Joe Subbiani635231a2021-07-14 11:53:07 +0100196 * bits unsigned integer in.
Jerry Yu29287a42021-10-28 10:26:13 +0800197 * \param offset Offset from \p data where to put the least significant
Joe Subbiani635231a2021-07-14 11:53:07 +0100198 * byte of the 16 bits unsigned integer \p n.
Joe Subbiani394bdd62021-07-07 15:16:56 +0100199 */
Joe Subbiani9fa9ac32021-07-05 15:37:39 +0100200#ifndef MBEDTLS_PUT_UINT16_LE
Joe Subbiani5241e342021-07-19 15:29:18 +0100201#define MBEDTLS_PUT_UINT16_LE( n, data, offset ) \
202{ \
203 ( data )[( offset ) ] = MBEDTLS_BYTE_0( n ); \
204 ( data )[( offset ) + 1] = MBEDTLS_BYTE_1( n ); \
Joe Subbiani9fa9ac32021-07-05 15:37:39 +0100205}
206#endif
207
Joe Subbiani99edd6c2021-07-16 12:29:49 +0100208/**
Joe Subbiani6dd73642021-07-19 11:56:54 +0100209 * Get the unsigned 16 bits integer corresponding to two bytes in
Joe Subbiani5241e342021-07-19 15:29:18 +0100210 * big-endian order (MSB first).
Joe Subbiani6dd73642021-07-19 11:56:54 +0100211 *
212 * \param data Base address of the memory to get the two bytes from.
Jerry Yu29287a42021-10-28 10:26:13 +0800213 * \param offset Offset from \p data of the first and most significant
Joe Subbiani6dd73642021-07-19 11:56:54 +0100214 * byte of the two bytes to build the 16 bits unsigned
215 * integer from.
216 */
217#ifndef MBEDTLS_GET_UINT16_BE
Joe Subbiani5241e342021-07-19 15:29:18 +0100218#define MBEDTLS_GET_UINT16_BE( data, offset ) \
219 ( \
220 ( (uint16_t) ( data )[( offset ) ] << 8 ) \
221 | ( (uint16_t) ( data )[( offset ) + 1] ) \
Joe Subbiani6dd73642021-07-19 11:56:54 +0100222 )
223#endif
224
225/**
226 * Put in memory a 16 bits unsigned integer in big-endian order.
227 *
228 * \param n 16 bits unsigned integer to put in memory.
229 * \param data Base address of the memory where to put the 16
230 * bits unsigned integer in.
Jerry Yu29287a42021-10-28 10:26:13 +0800231 * \param offset Offset from \p data where to put the most significant
Joe Subbiani6dd73642021-07-19 11:56:54 +0100232 * byte of the 16 bits unsigned integer \p n.
233 */
234#ifndef MBEDTLS_PUT_UINT16_BE
Joe Subbiani5241e342021-07-19 15:29:18 +0100235#define MBEDTLS_PUT_UINT16_BE( n, data, offset ) \
236{ \
237 ( data )[( offset ) ] = MBEDTLS_BYTE_1( n ); \
238 ( data )[( offset ) + 1] = MBEDTLS_BYTE_0( n ); \
Joe Subbiani6dd73642021-07-19 11:56:54 +0100239}
240#endif
241
242/**
Jerry Yuf3f5c212021-10-27 17:05:49 +0800243 * Get the unsigned 24 bits integer corresponding to three bytes in
Jerry Yu643d1162021-10-27 13:52:04 +0800244 * big-endian order (MSB first).
245 *
Jerry Yuf3f5c212021-10-27 17:05:49 +0800246 * \param data Base address of the memory to get the three bytes from.
247 * \param offset Offset from \p data of the first and most significant
248 * byte of the three bytes to build the 24 bits unsigned
Jerry Yu643d1162021-10-27 13:52:04 +0800249 * integer from.
250 */
251#ifndef MBEDTLS_GET_UINT24_BE
252#define MBEDTLS_GET_UINT24_BE( data , offset ) \
253 ( \
254 ( (uint32_t) ( data )[( offset ) ] << 16 ) \
255 | ( (uint32_t) ( data )[( offset ) + 1] << 8 ) \
256 | ( (uint32_t) ( data )[( offset ) + 2] ) \
257 )
258#endif
259
260/**
261 * Put in memory a 24 bits unsigned integer in big-endian order.
262 *
263 * \param n 24 bits unsigned integer to put in memory.
264 * \param data Base address of the memory where to put the 24
265 * bits unsigned integer in.
Jerry Yuf3f5c212021-10-27 17:05:49 +0800266 * \param offset Offset from \p data where to put the most significant
Jerry Yu643d1162021-10-27 13:52:04 +0800267 * byte of the 24 bits unsigned integer \p n.
268 */
269#ifndef MBEDTLS_PUT_UINT24_BE
270#define MBEDTLS_PUT_UINT24_BE( n, data, offset ) \
271{ \
272 ( data )[( offset ) ] = MBEDTLS_BYTE_2( n ); \
273 ( data )[( offset ) + 1] = MBEDTLS_BYTE_1( n ); \
274 ( data )[( offset ) + 2] = MBEDTLS_BYTE_0( n ); \
275}
276#endif
277
278/**
Jerry Yuf3f5c212021-10-27 17:05:49 +0800279 * Get the unsigned 24 bits integer corresponding to three bytes in
Jerry Yu643d1162021-10-27 13:52:04 +0800280 * little-endian order (LSB first).
281 *
Jerry Yuf3f5c212021-10-27 17:05:49 +0800282 * \param data Base address of the memory to get the three bytes from.
283 * \param offset Offset from \p data of the first and least significant
284 * byte of the three bytes to build the 24 bits unsigned
Jerry Yu643d1162021-10-27 13:52:04 +0800285 * integer from.
286 */
287#ifndef MBEDTLS_GET_UINT24_LE
288#define MBEDTLS_GET_UINT24_LE( data, offset ) \
289 ( \
290 ( (uint32_t) ( data )[( offset ) ] ) \
291 | ( (uint32_t) ( data )[( offset ) + 1] << 8 ) \
292 | ( (uint32_t) ( data )[( offset ) + 2] << 16 ) \
293 )
294#endif
295
296/**
297 * Put in memory a 24 bits unsigned integer in little-endian order.
298 *
299 * \param n 24 bits unsigned integer to put in memory.
300 * \param data Base address of the memory where to put the 24
301 * bits unsigned integer in.
Jerry Yuf3f5c212021-10-27 17:05:49 +0800302 * \param offset Offset from \p data where to put the least significant
Jerry Yu643d1162021-10-27 13:52:04 +0800303 * byte of the 24 bits unsigned integer \p n.
304 */
305#ifndef MBEDTLS_PUT_UINT24_LE
306#define MBEDTLS_PUT_UINT24_LE( n, data, offset ) \
307{ \
308 ( data )[( offset ) ] = MBEDTLS_BYTE_0( n ); \
309 ( data )[( offset ) + 1] = MBEDTLS_BYTE_1( n ); \
310 ( data )[( offset ) + 2] = MBEDTLS_BYTE_2( n ); \
311}
312#endif
313
314/**
Joe Subbiani99edd6c2021-07-16 12:29:49 +0100315 * Get the unsigned 64 bits integer corresponding to eight bytes in
316 * big-endian order (MSB first).
317 *
318 * \param data Base address of the memory to get the eight bytes from.
Jerry Yu29287a42021-10-28 10:26:13 +0800319 * \param offset Offset from \p data of the first and most significant
Joe Subbiani99edd6c2021-07-16 12:29:49 +0100320 * byte of the eight bytes to build the 64 bits unsigned
321 * integer from.
322 */
323#ifndef MBEDTLS_GET_UINT64_BE
Joe Subbiani5241e342021-07-19 15:29:18 +0100324#define MBEDTLS_GET_UINT64_BE( data, offset ) \
325 ( \
326 ( (uint64_t) ( data )[( offset ) ] << 56 ) \
327 | ( (uint64_t) ( data )[( offset ) + 1] << 48 ) \
328 | ( (uint64_t) ( data )[( offset ) + 2] << 40 ) \
329 | ( (uint64_t) ( data )[( offset ) + 3] << 32 ) \
330 | ( (uint64_t) ( data )[( offset ) + 4] << 24 ) \
331 | ( (uint64_t) ( data )[( offset ) + 5] << 16 ) \
332 | ( (uint64_t) ( data )[( offset ) + 6] << 8 ) \
333 | ( (uint64_t) ( data )[( offset ) + 7] ) \
Joe Subbiani99edd6c2021-07-16 12:29:49 +0100334 )
335#endif
336
337/**
338 * Put in memory a 64 bits unsigned integer in big-endian order.
339 *
340 * \param n 64 bits unsigned integer to put in memory.
341 * \param data Base address of the memory where to put the 64
342 * bits unsigned integer in.
Jerry Yu29287a42021-10-28 10:26:13 +0800343 * \param offset Offset from \p data where to put the most significant
Joe Subbiani99edd6c2021-07-16 12:29:49 +0100344 * byte of the 64 bits unsigned integer \p n.
345 */
346#ifndef MBEDTLS_PUT_UINT64_BE
Joe Subbiani5241e342021-07-19 15:29:18 +0100347#define MBEDTLS_PUT_UINT64_BE( n, data, offset ) \
348{ \
349 ( data )[( offset ) ] = MBEDTLS_BYTE_7( n ); \
350 ( data )[( offset ) + 1] = MBEDTLS_BYTE_6( n ); \
351 ( data )[( offset ) + 2] = MBEDTLS_BYTE_5( n ); \
352 ( data )[( offset ) + 3] = MBEDTLS_BYTE_4( n ); \
353 ( data )[( offset ) + 4] = MBEDTLS_BYTE_3( n ); \
354 ( data )[( offset ) + 5] = MBEDTLS_BYTE_2( n ); \
355 ( data )[( offset ) + 6] = MBEDTLS_BYTE_1( n ); \
356 ( data )[( offset ) + 7] = MBEDTLS_BYTE_0( n ); \
Joe Subbiani99edd6c2021-07-16 12:29:49 +0100357}
358#endif
359
360/**
361 * Get the unsigned 64 bits integer corresponding to eight bytes in
362 * little-endian order (LSB first).
363 *
364 * \param data Base address of the memory to get the eight bytes from.
Jerry Yu29287a42021-10-28 10:26:13 +0800365 * \param offset Offset from \p data of the first and least significant
Joe Subbiani99edd6c2021-07-16 12:29:49 +0100366 * byte of the eight bytes to build the 64 bits unsigned
367 * integer from.
368 */
369#ifndef MBEDTLS_GET_UINT64_LE
Joe Subbiani5241e342021-07-19 15:29:18 +0100370#define MBEDTLS_GET_UINT64_LE( data, offset ) \
371 ( \
372 ( (uint64_t) ( data )[( offset ) + 7] << 56 ) \
373 | ( (uint64_t) ( data )[( offset ) + 6] << 48 ) \
374 | ( (uint64_t) ( data )[( offset ) + 5] << 40 ) \
375 | ( (uint64_t) ( data )[( offset ) + 4] << 32 ) \
376 | ( (uint64_t) ( data )[( offset ) + 3] << 24 ) \
377 | ( (uint64_t) ( data )[( offset ) + 2] << 16 ) \
378 | ( (uint64_t) ( data )[( offset ) + 1] << 8 ) \
379 | ( (uint64_t) ( data )[( offset ) ] ) \
Joe Subbiani99edd6c2021-07-16 12:29:49 +0100380 )
381#endif
382
383/**
384 * Put in memory a 64 bits unsigned integer in little-endian order.
385 *
386 * \param n 64 bits unsigned integer to put in memory.
387 * \param data Base address of the memory where to put the 64
388 * bits unsigned integer in.
Jerry Yu29287a42021-10-28 10:26:13 +0800389 * \param offset Offset from \p data where to put the least significant
Joe Subbiani99edd6c2021-07-16 12:29:49 +0100390 * byte of the 64 bits unsigned integer \p n.
391 */
392#ifndef MBEDTLS_PUT_UINT64_LE
Joe Subbiani5241e342021-07-19 15:29:18 +0100393#define MBEDTLS_PUT_UINT64_LE( n, data, offset ) \
394{ \
395 ( data )[( offset ) ] = MBEDTLS_BYTE_0( n ); \
396 ( data )[( offset ) + 1] = MBEDTLS_BYTE_1( n ); \
397 ( data )[( offset ) + 2] = MBEDTLS_BYTE_2( n ); \
398 ( data )[( offset ) + 3] = MBEDTLS_BYTE_3( n ); \
399 ( data )[( offset ) + 4] = MBEDTLS_BYTE_4( n ); \
400 ( data )[( offset ) + 5] = MBEDTLS_BYTE_5( n ); \
401 ( data )[( offset ) + 6] = MBEDTLS_BYTE_6( n ); \
402 ( data )[( offset ) + 7] = MBEDTLS_BYTE_7( n ); \
Joe Subbiani99edd6c2021-07-16 12:29:49 +0100403}
404#endif
Joe Subbiani9fa9ac32021-07-05 15:37:39 +0100405
Dave Rodgmanc3d80412022-11-22 15:01:39 +0000406/**
407 * Perform a fast block XOR operation, such that
408 * r[i] = a[i] ^ b[i] where 0 <= i < n
409 *
410 * \param r Pointer to result (buffer of at least \p n bytes). \p r
411 * may be equal to either \p a or \p b, but behaviour when
412 * it overlaps in other ways is undefined.
413 * \param a Pointer to input (buffer of at least \p n bytes)
414 * \param b Pointer to input (buffer of at least \p n bytes)
415 * \param n Number of bytes to process.
416 */
Dave Rodgman63d11432022-11-23 14:03:30 +0000417static inline void mbedtls_xor( unsigned char *r, unsigned char const *a, unsigned char const *b, size_t n )
Dave Rodgmanc3d80412022-11-22 15:01:39 +0000418{
Dave Rodgmanfdd967e2022-11-22 18:55:17 +0000419#if defined(MBEDTLS_ALLOW_UNALIGNED_ACCESS)
Dave Rodgmanf9a1c372022-11-23 14:02:00 +0000420 uint32_t *a32 = (uint32_t *)a;
421 uint32_t *b32 = (uint32_t *)b;
422 uint32_t *r32 = (uint32_t *)r;
423 for ( size_t i = 0; i < ( n >> 2 ); i++ )
Dave Rodgmanc3d80412022-11-22 15:01:39 +0000424 {
425 r32[i] = a32[i] ^ b32[i];
426 }
Dave Rodgmanf9a1c372022-11-23 14:02:00 +0000427 for ( size_t i = n - ( n % 4 ) ; i < n; i++ )
Dave Rodgmanc3d80412022-11-22 15:01:39 +0000428 {
429 r[i] = a[i] ^ b[i];
430 }
Dave Rodgmanfdd967e2022-11-22 18:55:17 +0000431#else
432 for ( size_t i = 0; i < n; i++ )
433 {
434 r[i] = a[i] ^ b[i];
435 }
436#endif
Dave Rodgmanc3d80412022-11-22 15:01:39 +0000437}
438
Jerry Yu6c983522021-09-24 12:45:36 +0800439/* Fix MSVC C99 compatible issue
440 * MSVC support __func__ from visual studio 2015( 1900 )
441 * Use MSVC predefine macro to avoid name check fail.
442 */
443#if (defined(_MSC_VER) && ( _MSC_VER <= 1900 ))
Jerry Yud52398d2021-09-28 16:13:44 +0800444#define /*no-check-names*/ __func__ __FUNCTION__
Jerry Yu6c983522021-09-24 12:45:36 +0800445#endif
446
Gilles Peskinec4672fd2019-09-11 13:39:11 +0200447#endif /* MBEDTLS_LIBRARY_COMMON_H */