blob: c17506416b02ad92d23c957f088a2f262d02eb3b [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"
Dave Rodgmanfbc23222022-11-24 18:07:37 +000027#include "alignment.h"
Gilles Peskinec4672fd2019-09-11 13:39:11 +020028
Joe Subbiani2194dc42021-07-14 12:31:31 +010029#include <stdint.h>
Dave Rodgmanc3d80412022-11-22 15:01:39 +000030#include <stddef.h>
Joe Subbiani2194dc42021-07-14 12:31:31 +010031
Dave Rodgman468df312022-11-23 16:56:35 +000032#if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \
33 !defined(inline) && !defined(__cplusplus)
34#define inline __inline
35#endif
36
Gilles Peskinec4672fd2019-09-11 13:39:11 +020037/** Helper to define a function as static except when building invasive tests.
38 *
39 * If a function is only used inside its own source file and should be
40 * declared `static` to allow the compiler to optimize for code size,
41 * but that function has unit tests, define it with
42 * ```
43 * MBEDTLS_STATIC_TESTABLE int mbedtls_foo(...) { ... }
44 * ```
45 * and declare it in a header in the `library/` directory with
46 * ```
47 * #if defined(MBEDTLS_TEST_HOOKS)
48 * int mbedtls_foo(...);
49 * #endif
50 * ```
51 */
52#if defined(MBEDTLS_TEST_HOOKS)
53#define MBEDTLS_STATIC_TESTABLE
54#else
55#define MBEDTLS_STATIC_TESTABLE static
56#endif
57
TRodziewicz7871c2e2021-07-07 17:29:43 +020058#if defined(MBEDTLS_TEST_HOOKS)
59extern void (*mbedtls_test_hook_test_fail)( const char * test, int line, const char * file );
60#define MBEDTLS_TEST_HOOK_TEST_ASSERT( TEST ) \
61 do { \
62 if( ( ! ( TEST ) ) && ( ( *mbedtls_test_hook_test_fail ) != NULL ) ) \
63 { \
64 ( *mbedtls_test_hook_test_fail )( #TEST, __LINE__, __FILE__ ); \
65 } \
66 } while( 0 )
67#else
68#define MBEDTLS_TEST_HOOK_TEST_ASSERT( TEST )
69#endif /* defined(MBEDTLS_TEST_HOOKS) */
70
Mateusz Starzyk57d1d192021-05-27 14:39:53 +020071/** Allow library to access its structs' private members.
Mateusz Starzyk2c09c9b2021-05-14 22:20:10 +020072 *
73 * Although structs defined in header files are publicly available,
74 * their members are private and should not be accessed by the user.
75 */
76#define MBEDTLS_ALLOW_PRIVATE_ACCESS
77
Dave Rodgmanc3d80412022-11-22 15:01:39 +000078/**
79 * Perform a fast block XOR operation, such that
80 * r[i] = a[i] ^ b[i] where 0 <= i < n
81 *
82 * \param r Pointer to result (buffer of at least \p n bytes). \p r
83 * may be equal to either \p a or \p b, but behaviour when
84 * it overlaps in other ways is undefined.
85 * \param a Pointer to input (buffer of at least \p n bytes)
86 * \param b Pointer to input (buffer of at least \p n bytes)
87 * \param n Number of bytes to process.
88 */
Dave Rodgman3c8eb7e2022-11-23 14:50:03 +000089inline void mbedtls_xor( unsigned char *r, unsigned char const *a, unsigned char const *b, size_t n )
Dave Rodgmanc3d80412022-11-22 15:01:39 +000090{
Dave Rodgman96d61d12022-11-24 19:33:22 +000091 size_t i;
Dave Rodgmanc5885882022-11-24 20:35:04 +000092 for ( i = 0; ( i + 4 ) <= n; i += 4 )
Dave Rodgmanc3d80412022-11-22 15:01:39 +000093 {
Dave Rodgman4b910c12022-11-24 19:44:52 +000094 uint32_t x = mbedtls_get_unaligned_uint32( a + i ) ^ mbedtls_get_unaligned_uint32( b + i );
95 mbedtls_put_unaligned_uint32( r + i, x );
Dave Rodgmanc3d80412022-11-22 15:01:39 +000096 }
Dave Rodgman96d61d12022-11-24 19:33:22 +000097 for ( ; i < n; i++ )
Dave Rodgmanc3d80412022-11-22 15:01:39 +000098 {
99 r[i] = a[i] ^ b[i];
100 }
101}
102
Jerry Yu6c983522021-09-24 12:45:36 +0800103/* Fix MSVC C99 compatible issue
104 * MSVC support __func__ from visual studio 2015( 1900 )
105 * Use MSVC predefine macro to avoid name check fail.
106 */
107#if (defined(_MSC_VER) && ( _MSC_VER <= 1900 ))
Jerry Yud52398d2021-09-28 16:13:44 +0800108#define /*no-check-names*/ __func__ __FUNCTION__
Jerry Yu6c983522021-09-24 12:45:36 +0800109#endif
110
Gilles Peskinec4672fd2019-09-11 13:39:11 +0200111#endif /* MBEDTLS_LIBRARY_COMMON_H */