blob: 75d28ff1f24077cf5092ea1d85b67188b5c8d96c [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 Rodgmane7cd1372022-11-23 19:14:26 +000077/** MBEDTLS_ALLOW_UNALIGNED_ACCESS is defined for architectures where unaligned
78 * memory accesses are safe and performant.
79 *
80 * Unaligned accesses must be made via the UNALIGNED_UINT32_PTR type
81 * defined here.
Dave Rodgmanfdd967e2022-11-22 18:55:17 +000082 *
83 * This list is incomplete.
84 */
Dave Rodgmane7cd1372022-11-23 19:14:26 +000085#if defined(__i386__) || defined(__amd64__) || defined( __x86_64__) \
Dave Rodgmanfdd967e2022-11-22 18:55:17 +000086 || defined(__ARM_FEATURE_UNALIGNED) \
87 || defined(__aarch64__) \
88 || defined(__ARM_ARCH_8__) || defined(__ARM_ARCH_8A__) || defined(__ARM_ARCH_8M__) \
Dave Rodgmane7cd1372022-11-23 19:14:26 +000089 || defined(__ARM_ARCH_7A__)
90#if (defined(__GNUC__) && __GNUC__ >= 4) \
91 || (defined(__clang__) && __has_attribute(aligned)) \
92 || (defined(__ARMCC_VERSION) && __ARMCC_VERSION >= 5000000 )
Dave Rodgmanfdd967e2022-11-22 18:55:17 +000093#define MBEDTLS_ALLOW_UNALIGNED_ACCESS
Dave Rodgmane7cd1372022-11-23 19:14:26 +000094__attribute__((aligned(1))) typedef uint32_t unaligned_uint32_t;
95#define UNALIGNED_UINT32_PTR unaligned_uint32_t
96#elif defined(_MSC_VER)
97#define MBEDTLS_ALLOW_UNALIGNED_ACCESS
98#define UNALIGNED_UINT32_PTR __declspec(align(1)) uint32_t
99#endif
Dave Rodgmanfdd967e2022-11-22 18:55:17 +0000100#endif
101
Joe Subbiani50dde562021-06-22 15:51:53 +0100102/** Byte Reading Macros
Joe Subbiani6f2bb0c2021-06-24 09:06:23 +0100103 *
Joe Subbiani9ab18662021-07-21 16:35:48 +0100104 * Given a multi-byte integer \p x, MBEDTLS_BYTE_n retrieves the n-th
Joe Subbianid0687852021-07-21 15:22:47 +0100105 * byte from x, where byte 0 is the least significant byte.
Joe Subbiani50dde562021-06-22 15:51:53 +0100106 */
Joe Subbiani2194dc42021-07-14 12:31:31 +0100107#define MBEDTLS_BYTE_0( x ) ( (uint8_t) ( ( x ) & 0xff ) )
Joe Subbiani5ecac212021-06-24 13:00:03 +0100108#define MBEDTLS_BYTE_1( x ) ( (uint8_t) ( ( ( x ) >> 8 ) & 0xff ) )
109#define MBEDTLS_BYTE_2( x ) ( (uint8_t) ( ( ( x ) >> 16 ) & 0xff ) )
110#define MBEDTLS_BYTE_3( x ) ( (uint8_t) ( ( ( x ) >> 24 ) & 0xff ) )
Joe Subbiani2194dc42021-07-14 12:31:31 +0100111#define MBEDTLS_BYTE_4( x ) ( (uint8_t) ( ( ( x ) >> 32 ) & 0xff ) )
112#define MBEDTLS_BYTE_5( x ) ( (uint8_t) ( ( ( x ) >> 40 ) & 0xff ) )
113#define MBEDTLS_BYTE_6( x ) ( (uint8_t) ( ( ( x ) >> 48 ) & 0xff ) )
114#define MBEDTLS_BYTE_7( x ) ( (uint8_t) ( ( ( x ) >> 56 ) & 0xff ) )
Joe Subbianicd84d762021-07-08 14:59:52 +0100115
Joe Subbiani394bdd62021-07-07 15:16:56 +0100116/**
Joe Subbianif5462d92021-07-13 12:13:19 +0100117 * Get the unsigned 32 bits integer corresponding to four bytes in
Joe Subbiani635231a2021-07-14 11:53:07 +0100118 * big-endian order (MSB first).
Joe Subbiani394bdd62021-07-07 15:16:56 +0100119 *
Joe Subbiani635231a2021-07-14 11:53:07 +0100120 * \param data Base address of the memory to get the four bytes from.
Jerry Yu29287a42021-10-28 10:26:13 +0800121 * \param offset Offset from \p data of the first and most significant
Joe Subbianif5462d92021-07-13 12:13:19 +0100122 * byte of the four bytes to build the 32 bits unsigned
Joe Subbiani635231a2021-07-14 11:53:07 +0100123 * integer from.
Joe Subbiani6a506312021-07-07 16:56:29 +0100124 */
125#ifndef MBEDTLS_GET_UINT32_BE
Joe Subbiani5241e342021-07-19 15:29:18 +0100126#define MBEDTLS_GET_UINT32_BE( data , offset ) \
127 ( \
128 ( (uint32_t) ( data )[( offset ) ] << 24 ) \
129 | ( (uint32_t) ( data )[( offset ) + 1] << 16 ) \
130 | ( (uint32_t) ( data )[( offset ) + 2] << 8 ) \
131 | ( (uint32_t) ( data )[( offset ) + 3] ) \
Joe Subbiani6a506312021-07-07 16:56:29 +0100132 )
133#endif
134
135/**
Joe Subbiani635231a2021-07-14 11:53:07 +0100136 * Put in memory a 32 bits unsigned integer in big-endian order.
Joe Subbiani6a506312021-07-07 16:56:29 +0100137 *
Joe Subbianif5462d92021-07-13 12:13:19 +0100138 * \param n 32 bits unsigned integer to put in memory.
139 * \param data Base address of the memory where to put the 32
Joe Subbiani635231a2021-07-14 11:53:07 +0100140 * bits unsigned integer in.
Jerry Yu29287a42021-10-28 10:26:13 +0800141 * \param offset Offset from \p data where to put the most significant
Joe Subbiani635231a2021-07-14 11:53:07 +0100142 * byte of the 32 bits unsigned integer \p n.
Joe Subbiani394bdd62021-07-07 15:16:56 +0100143 */
Joe Subbiani5ecac212021-06-24 13:00:03 +0100144#ifndef MBEDTLS_PUT_UINT32_BE
Joe Subbiani5241e342021-07-19 15:29:18 +0100145#define MBEDTLS_PUT_UINT32_BE( n, data, offset ) \
146{ \
147 ( data )[( offset ) ] = MBEDTLS_BYTE_3( n ); \
148 ( data )[( offset ) + 1] = MBEDTLS_BYTE_2( n ); \
149 ( data )[( offset ) + 2] = MBEDTLS_BYTE_1( n ); \
150 ( data )[( offset ) + 3] = MBEDTLS_BYTE_0( n ); \
151}
Joe Subbiani30d974c2021-06-23 11:49:03 +0100152#endif
153
Joe Subbiani394bdd62021-07-07 15:16:56 +0100154/**
Joe Subbianif5462d92021-07-13 12:13:19 +0100155 * Get the unsigned 32 bits integer corresponding to four bytes in
Joe Subbiani635231a2021-07-14 11:53:07 +0100156 * little-endian order (LSB first).
Joe Subbiani6a506312021-07-07 16:56:29 +0100157 *
Joe Subbiani635231a2021-07-14 11:53:07 +0100158 * \param data Base address of the memory to get the four bytes from.
Jerry Yu29287a42021-10-28 10:26:13 +0800159 * \param offset Offset from \p data of the first and least significant
Joe Subbianif5462d92021-07-13 12:13:19 +0100160 * byte of the four bytes to build the 32 bits unsigned
Joe Subbiani635231a2021-07-14 11:53:07 +0100161 * integer from.
Joe Subbiani54c61342021-06-23 12:16:47 +0100162 */
Joe Subbiani5ecac212021-06-24 13:00:03 +0100163#ifndef MBEDTLS_GET_UINT32_LE
Joe Subbiani5241e342021-07-19 15:29:18 +0100164#define MBEDTLS_GET_UINT32_LE( data, offset ) \
165 ( \
166 ( (uint32_t) ( data )[( offset ) ] ) \
167 | ( (uint32_t) ( data )[( offset ) + 1] << 8 ) \
168 | ( (uint32_t) ( data )[( offset ) + 2] << 16 ) \
169 | ( (uint32_t) ( data )[( offset ) + 3] << 24 ) \
Joe Subbiani6a506312021-07-07 16:56:29 +0100170 )
Joe Subbiani54c61342021-06-23 12:16:47 +0100171#endif
172
Joe Subbiani6a506312021-07-07 16:56:29 +0100173/**
Joe Subbiani635231a2021-07-14 11:53:07 +0100174 * Put in memory a 32 bits unsigned integer in little-endian order.
Joe Subbiani6a506312021-07-07 16:56:29 +0100175 *
Joe Subbianif5462d92021-07-13 12:13:19 +0100176 * \param n 32 bits unsigned integer to put in memory.
177 * \param data Base address of the memory where to put the 32
Joe Subbiani635231a2021-07-14 11:53:07 +0100178 * bits unsigned integer in.
Jerry Yu29287a42021-10-28 10:26:13 +0800179 * \param offset Offset from \p data where to put the least significant
Joe Subbiani635231a2021-07-14 11:53:07 +0100180 * byte of the 32 bits unsigned integer \p n.
Joe Subbiani6a506312021-07-07 16:56:29 +0100181 */
Joe Subbiani5ecac212021-06-24 13:00:03 +0100182#ifndef MBEDTLS_PUT_UINT32_LE
Joe Subbiani5241e342021-07-19 15:29:18 +0100183#define MBEDTLS_PUT_UINT32_LE( n, data, offset ) \
184{ \
185 ( data )[( offset ) ] = MBEDTLS_BYTE_0( n ); \
186 ( data )[( offset ) + 1] = MBEDTLS_BYTE_1( n ); \
187 ( data )[( offset ) + 2] = MBEDTLS_BYTE_2( n ); \
188 ( data )[( offset ) + 3] = MBEDTLS_BYTE_3( n ); \
189}
Joe Subbiani54c61342021-06-23 12:16:47 +0100190#endif
191
Joe Subbiani6f2bb0c2021-06-24 09:06:23 +0100192/**
Joe Subbianibf7ea842021-07-14 12:05:51 +0100193 * Get the unsigned 16 bits integer corresponding to two bytes in
Joe Subbiani635231a2021-07-14 11:53:07 +0100194 * little-endian order (LSB first).
Joe Subbiani6a506312021-07-07 16:56:29 +0100195 *
Joe Subbianibf7ea842021-07-14 12:05:51 +0100196 * \param data Base address of the memory to get the two bytes from.
Jerry Yu29287a42021-10-28 10:26:13 +0800197 * \param offset Offset from \p data of the first and least significant
Joe Subbianibf7ea842021-07-14 12:05:51 +0100198 * byte of the two bytes to build the 16 bits unsigned
Joe Subbiani635231a2021-07-14 11:53:07 +0100199 * integer from.
Joe Subbiani3b394502021-06-23 11:23:44 +0100200 */
Joe Subbiani6a506312021-07-07 16:56:29 +0100201#ifndef MBEDTLS_GET_UINT16_LE
Joe Subbiani5241e342021-07-19 15:29:18 +0100202#define MBEDTLS_GET_UINT16_LE( data, offset ) \
203 ( \
204 ( (uint16_t) ( data )[( offset ) ] ) \
205 | ( (uint16_t) ( data )[( offset ) + 1] << 8 ) \
Joe Subbiani3b394502021-06-23 11:23:44 +0100206 )
Joe Subbiani6a506312021-07-07 16:56:29 +0100207#endif
Joe Subbiani3b394502021-06-23 11:23:44 +0100208
Joe Subbiani394bdd62021-07-07 15:16:56 +0100209/**
Joe Subbiani635231a2021-07-14 11:53:07 +0100210 * Put in memory a 16 bits unsigned integer in little-endian order.
Joe Subbiani394bdd62021-07-07 15:16:56 +0100211 *
Joe Subbianif5462d92021-07-13 12:13:19 +0100212 * \param n 16 bits unsigned integer to put in memory.
213 * \param data Base address of the memory where to put the 16
Joe Subbiani635231a2021-07-14 11:53:07 +0100214 * bits unsigned integer in.
Jerry Yu29287a42021-10-28 10:26:13 +0800215 * \param offset Offset from \p data where to put the least significant
Joe Subbiani635231a2021-07-14 11:53:07 +0100216 * byte of the 16 bits unsigned integer \p n.
Joe Subbiani394bdd62021-07-07 15:16:56 +0100217 */
Joe Subbiani9fa9ac32021-07-05 15:37:39 +0100218#ifndef MBEDTLS_PUT_UINT16_LE
Joe Subbiani5241e342021-07-19 15:29:18 +0100219#define MBEDTLS_PUT_UINT16_LE( n, data, offset ) \
220{ \
221 ( data )[( offset ) ] = MBEDTLS_BYTE_0( n ); \
222 ( data )[( offset ) + 1] = MBEDTLS_BYTE_1( n ); \
Joe Subbiani9fa9ac32021-07-05 15:37:39 +0100223}
224#endif
225
Joe Subbiani99edd6c2021-07-16 12:29:49 +0100226/**
Joe Subbiani6dd73642021-07-19 11:56:54 +0100227 * Get the unsigned 16 bits integer corresponding to two bytes in
Joe Subbiani5241e342021-07-19 15:29:18 +0100228 * big-endian order (MSB first).
Joe Subbiani6dd73642021-07-19 11:56:54 +0100229 *
230 * \param data Base address of the memory to get the two bytes from.
Jerry Yu29287a42021-10-28 10:26:13 +0800231 * \param offset Offset from \p data of the first and most significant
Joe Subbiani6dd73642021-07-19 11:56:54 +0100232 * byte of the two bytes to build the 16 bits unsigned
233 * integer from.
234 */
235#ifndef MBEDTLS_GET_UINT16_BE
Joe Subbiani5241e342021-07-19 15:29:18 +0100236#define MBEDTLS_GET_UINT16_BE( data, offset ) \
237 ( \
238 ( (uint16_t) ( data )[( offset ) ] << 8 ) \
239 | ( (uint16_t) ( data )[( offset ) + 1] ) \
Joe Subbiani6dd73642021-07-19 11:56:54 +0100240 )
241#endif
242
243/**
244 * Put in memory a 16 bits unsigned integer in big-endian order.
245 *
246 * \param n 16 bits unsigned integer to put in memory.
247 * \param data Base address of the memory where to put the 16
248 * bits unsigned integer in.
Jerry Yu29287a42021-10-28 10:26:13 +0800249 * \param offset Offset from \p data where to put the most significant
Joe Subbiani6dd73642021-07-19 11:56:54 +0100250 * byte of the 16 bits unsigned integer \p n.
251 */
252#ifndef MBEDTLS_PUT_UINT16_BE
Joe Subbiani5241e342021-07-19 15:29:18 +0100253#define MBEDTLS_PUT_UINT16_BE( n, data, offset ) \
254{ \
255 ( data )[( offset ) ] = MBEDTLS_BYTE_1( n ); \
256 ( data )[( offset ) + 1] = MBEDTLS_BYTE_0( n ); \
Joe Subbiani6dd73642021-07-19 11:56:54 +0100257}
258#endif
259
260/**
Jerry Yuf3f5c212021-10-27 17:05:49 +0800261 * Get the unsigned 24 bits integer corresponding to three bytes in
Jerry Yu643d1162021-10-27 13:52:04 +0800262 * big-endian order (MSB first).
263 *
Jerry Yuf3f5c212021-10-27 17:05:49 +0800264 * \param data Base address of the memory to get the three bytes from.
265 * \param offset Offset from \p data of the first and most significant
266 * byte of the three bytes to build the 24 bits unsigned
Jerry Yu643d1162021-10-27 13:52:04 +0800267 * integer from.
268 */
269#ifndef MBEDTLS_GET_UINT24_BE
270#define MBEDTLS_GET_UINT24_BE( data , offset ) \
271 ( \
272 ( (uint32_t) ( data )[( offset ) ] << 16 ) \
273 | ( (uint32_t) ( data )[( offset ) + 1] << 8 ) \
274 | ( (uint32_t) ( data )[( offset ) + 2] ) \
275 )
276#endif
277
278/**
279 * Put in memory a 24 bits unsigned integer in big-endian order.
280 *
281 * \param n 24 bits unsigned integer to put in memory.
282 * \param data Base address of the memory where to put the 24
283 * bits unsigned integer in.
Jerry Yuf3f5c212021-10-27 17:05:49 +0800284 * \param offset Offset from \p data where to put the most significant
Jerry Yu643d1162021-10-27 13:52:04 +0800285 * byte of the 24 bits unsigned integer \p n.
286 */
287#ifndef MBEDTLS_PUT_UINT24_BE
288#define MBEDTLS_PUT_UINT24_BE( n, data, offset ) \
289{ \
290 ( data )[( offset ) ] = MBEDTLS_BYTE_2( n ); \
291 ( data )[( offset ) + 1] = MBEDTLS_BYTE_1( n ); \
292 ( data )[( offset ) + 2] = MBEDTLS_BYTE_0( n ); \
293}
294#endif
295
296/**
Jerry Yuf3f5c212021-10-27 17:05:49 +0800297 * Get the unsigned 24 bits integer corresponding to three bytes in
Jerry Yu643d1162021-10-27 13:52:04 +0800298 * little-endian order (LSB first).
299 *
Jerry Yuf3f5c212021-10-27 17:05:49 +0800300 * \param data Base address of the memory to get the three bytes from.
301 * \param offset Offset from \p data of the first and least significant
302 * byte of the three bytes to build the 24 bits unsigned
Jerry Yu643d1162021-10-27 13:52:04 +0800303 * integer from.
304 */
305#ifndef MBEDTLS_GET_UINT24_LE
306#define MBEDTLS_GET_UINT24_LE( data, offset ) \
307 ( \
308 ( (uint32_t) ( data )[( offset ) ] ) \
309 | ( (uint32_t) ( data )[( offset ) + 1] << 8 ) \
310 | ( (uint32_t) ( data )[( offset ) + 2] << 16 ) \
311 )
312#endif
313
314/**
315 * Put in memory a 24 bits unsigned integer in little-endian order.
316 *
317 * \param n 24 bits unsigned integer to put in memory.
318 * \param data Base address of the memory where to put the 24
319 * bits unsigned integer in.
Jerry Yuf3f5c212021-10-27 17:05:49 +0800320 * \param offset Offset from \p data where to put the least significant
Jerry Yu643d1162021-10-27 13:52:04 +0800321 * byte of the 24 bits unsigned integer \p n.
322 */
323#ifndef MBEDTLS_PUT_UINT24_LE
324#define MBEDTLS_PUT_UINT24_LE( n, data, offset ) \
325{ \
326 ( data )[( offset ) ] = MBEDTLS_BYTE_0( n ); \
327 ( data )[( offset ) + 1] = MBEDTLS_BYTE_1( n ); \
328 ( data )[( offset ) + 2] = MBEDTLS_BYTE_2( n ); \
329}
330#endif
331
332/**
Joe Subbiani99edd6c2021-07-16 12:29:49 +0100333 * Get the unsigned 64 bits integer corresponding to eight bytes in
334 * big-endian order (MSB first).
335 *
336 * \param data Base address of the memory to get the eight bytes from.
Jerry Yu29287a42021-10-28 10:26:13 +0800337 * \param offset Offset from \p data of the first and most significant
Joe Subbiani99edd6c2021-07-16 12:29:49 +0100338 * byte of the eight bytes to build the 64 bits unsigned
339 * integer from.
340 */
341#ifndef MBEDTLS_GET_UINT64_BE
Joe Subbiani5241e342021-07-19 15:29:18 +0100342#define MBEDTLS_GET_UINT64_BE( data, offset ) \
343 ( \
344 ( (uint64_t) ( data )[( offset ) ] << 56 ) \
345 | ( (uint64_t) ( data )[( offset ) + 1] << 48 ) \
346 | ( (uint64_t) ( data )[( offset ) + 2] << 40 ) \
347 | ( (uint64_t) ( data )[( offset ) + 3] << 32 ) \
348 | ( (uint64_t) ( data )[( offset ) + 4] << 24 ) \
349 | ( (uint64_t) ( data )[( offset ) + 5] << 16 ) \
350 | ( (uint64_t) ( data )[( offset ) + 6] << 8 ) \
351 | ( (uint64_t) ( data )[( offset ) + 7] ) \
Joe Subbiani99edd6c2021-07-16 12:29:49 +0100352 )
353#endif
354
355/**
356 * Put in memory a 64 bits unsigned integer in big-endian order.
357 *
358 * \param n 64 bits unsigned integer to put in memory.
359 * \param data Base address of the memory where to put the 64
360 * bits unsigned integer in.
Jerry Yu29287a42021-10-28 10:26:13 +0800361 * \param offset Offset from \p data where to put the most significant
Joe Subbiani99edd6c2021-07-16 12:29:49 +0100362 * byte of the 64 bits unsigned integer \p n.
363 */
364#ifndef MBEDTLS_PUT_UINT64_BE
Joe Subbiani5241e342021-07-19 15:29:18 +0100365#define MBEDTLS_PUT_UINT64_BE( n, data, offset ) \
366{ \
367 ( data )[( offset ) ] = MBEDTLS_BYTE_7( n ); \
368 ( data )[( offset ) + 1] = MBEDTLS_BYTE_6( n ); \
369 ( data )[( offset ) + 2] = MBEDTLS_BYTE_5( n ); \
370 ( data )[( offset ) + 3] = MBEDTLS_BYTE_4( n ); \
371 ( data )[( offset ) + 4] = MBEDTLS_BYTE_3( n ); \
372 ( data )[( offset ) + 5] = MBEDTLS_BYTE_2( n ); \
373 ( data )[( offset ) + 6] = MBEDTLS_BYTE_1( n ); \
374 ( data )[( offset ) + 7] = MBEDTLS_BYTE_0( n ); \
Joe Subbiani99edd6c2021-07-16 12:29:49 +0100375}
376#endif
377
378/**
379 * Get the unsigned 64 bits integer corresponding to eight bytes in
380 * little-endian order (LSB first).
381 *
382 * \param data Base address of the memory to get the eight bytes from.
Jerry Yu29287a42021-10-28 10:26:13 +0800383 * \param offset Offset from \p data of the first and least significant
Joe Subbiani99edd6c2021-07-16 12:29:49 +0100384 * byte of the eight bytes to build the 64 bits unsigned
385 * integer from.
386 */
387#ifndef MBEDTLS_GET_UINT64_LE
Joe Subbiani5241e342021-07-19 15:29:18 +0100388#define MBEDTLS_GET_UINT64_LE( data, offset ) \
389 ( \
390 ( (uint64_t) ( data )[( offset ) + 7] << 56 ) \
391 | ( (uint64_t) ( data )[( offset ) + 6] << 48 ) \
392 | ( (uint64_t) ( data )[( offset ) + 5] << 40 ) \
393 | ( (uint64_t) ( data )[( offset ) + 4] << 32 ) \
394 | ( (uint64_t) ( data )[( offset ) + 3] << 24 ) \
395 | ( (uint64_t) ( data )[( offset ) + 2] << 16 ) \
396 | ( (uint64_t) ( data )[( offset ) + 1] << 8 ) \
397 | ( (uint64_t) ( data )[( offset ) ] ) \
Joe Subbiani99edd6c2021-07-16 12:29:49 +0100398 )
399#endif
400
401/**
402 * Put in memory a 64 bits unsigned integer in little-endian order.
403 *
404 * \param n 64 bits unsigned integer to put in memory.
405 * \param data Base address of the memory where to put the 64
406 * bits unsigned integer in.
Jerry Yu29287a42021-10-28 10:26:13 +0800407 * \param offset Offset from \p data where to put the least significant
Joe Subbiani99edd6c2021-07-16 12:29:49 +0100408 * byte of the 64 bits unsigned integer \p n.
409 */
410#ifndef MBEDTLS_PUT_UINT64_LE
Joe Subbiani5241e342021-07-19 15:29:18 +0100411#define MBEDTLS_PUT_UINT64_LE( n, data, offset ) \
412{ \
413 ( data )[( offset ) ] = MBEDTLS_BYTE_0( n ); \
414 ( data )[( offset ) + 1] = MBEDTLS_BYTE_1( n ); \
415 ( data )[( offset ) + 2] = MBEDTLS_BYTE_2( n ); \
416 ( data )[( offset ) + 3] = MBEDTLS_BYTE_3( n ); \
417 ( data )[( offset ) + 4] = MBEDTLS_BYTE_4( n ); \
418 ( data )[( offset ) + 5] = MBEDTLS_BYTE_5( n ); \
419 ( data )[( offset ) + 6] = MBEDTLS_BYTE_6( n ); \
420 ( data )[( offset ) + 7] = MBEDTLS_BYTE_7( n ); \
Joe Subbiani99edd6c2021-07-16 12:29:49 +0100421}
422#endif
Joe Subbiani9fa9ac32021-07-05 15:37:39 +0100423
Dave Rodgmanc3d80412022-11-22 15:01:39 +0000424/**
425 * Perform a fast block XOR operation, such that
426 * r[i] = a[i] ^ b[i] where 0 <= i < n
427 *
428 * \param r Pointer to result (buffer of at least \p n bytes). \p r
429 * may be equal to either \p a or \p b, but behaviour when
430 * it overlaps in other ways is undefined.
431 * \param a Pointer to input (buffer of at least \p n bytes)
432 * \param b Pointer to input (buffer of at least \p n bytes)
433 * \param n Number of bytes to process.
434 */
Dave Rodgman3c8eb7e2022-11-23 14:50:03 +0000435inline void mbedtls_xor( unsigned char *r, unsigned char const *a, unsigned char const *b, size_t n )
Dave Rodgmanc3d80412022-11-22 15:01:39 +0000436{
Dave Rodgmanfdd967e2022-11-22 18:55:17 +0000437#if defined(MBEDTLS_ALLOW_UNALIGNED_ACCESS)
Dave Rodgmane7cd1372022-11-23 19:14:26 +0000438 UNALIGNED_UINT32_PTR *a32 = (uint32_t *)a;
439 UNALIGNED_UINT32_PTR *b32 = (uint32_t *)b;
440 UNALIGNED_UINT32_PTR *r32 = (uint32_t *)r;
Dave Rodgmanf9a1c372022-11-23 14:02:00 +0000441 for ( size_t i = 0; i < ( n >> 2 ); i++ )
Dave Rodgmanc3d80412022-11-22 15:01:39 +0000442 {
443 r32[i] = a32[i] ^ b32[i];
444 }
Dave Rodgmanf9a1c372022-11-23 14:02:00 +0000445 for ( size_t i = n - ( n % 4 ) ; i < n; i++ )
Dave Rodgmanc3d80412022-11-22 15:01:39 +0000446 {
447 r[i] = a[i] ^ b[i];
448 }
Dave Rodgmanfdd967e2022-11-22 18:55:17 +0000449#else
450 for ( size_t i = 0; i < n; i++ )
451 {
452 r[i] = a[i] ^ b[i];
453 }
454#endif
Dave Rodgmanc3d80412022-11-22 15:01:39 +0000455}
456
Jerry Yu6c983522021-09-24 12:45:36 +0800457/* Fix MSVC C99 compatible issue
458 * MSVC support __func__ from visual studio 2015( 1900 )
459 * Use MSVC predefine macro to avoid name check fail.
460 */
461#if (defined(_MSC_VER) && ( _MSC_VER <= 1900 ))
Jerry Yud52398d2021-09-28 16:13:44 +0800462#define /*no-check-names*/ __func__ __FUNCTION__
Jerry Yu6c983522021-09-24 12:45:36 +0800463#endif
464
Gilles Peskinec4672fd2019-09-11 13:39:11 +0200465#endif /* MBEDTLS_LIBRARY_COMMON_H */