2019-09-11 13:39:11 +02:00
|
|
|
/**
|
|
|
|
* \file common.h
|
|
|
|
*
|
|
|
|
* \brief Utility macros for internal use in the library
|
|
|
|
*/
|
|
|
|
/*
|
2020-08-07 13:07:28 +02:00
|
|
|
* Copyright The Mbed TLS Contributors
|
2019-09-11 13:39:11 +02:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
|
|
* not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef MBEDTLS_LIBRARY_COMMON_H
|
|
|
|
#define MBEDTLS_LIBRARY_COMMON_H
|
|
|
|
|
2021-05-27 11:25:03 +02:00
|
|
|
#include "mbedtls/build_info.h"
|
2022-11-24 18:07:37 +00:00
|
|
|
#include "alignment.h"
|
2019-09-11 13:39:11 +02:00
|
|
|
|
2021-07-14 12:31:31 +01:00
|
|
|
#include <stdint.h>
|
2022-11-22 15:01:39 +00:00
|
|
|
#include <stddef.h>
|
2021-07-14 12:31:31 +01:00
|
|
|
|
2022-11-23 16:56:35 +00:00
|
|
|
#if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \
|
|
|
|
!defined(inline) && !defined(__cplusplus)
|
|
|
|
#define inline __inline
|
|
|
|
#endif
|
|
|
|
|
2019-09-11 13:39:11 +02:00
|
|
|
/** Helper to define a function as static except when building invasive tests.
|
|
|
|
*
|
|
|
|
* If a function is only used inside its own source file and should be
|
|
|
|
* declared `static` to allow the compiler to optimize for code size,
|
|
|
|
* but that function has unit tests, define it with
|
|
|
|
* ```
|
|
|
|
* MBEDTLS_STATIC_TESTABLE int mbedtls_foo(...) { ... }
|
|
|
|
* ```
|
|
|
|
* and declare it in a header in the `library/` directory with
|
|
|
|
* ```
|
|
|
|
* #if defined(MBEDTLS_TEST_HOOKS)
|
|
|
|
* int mbedtls_foo(...);
|
|
|
|
* #endif
|
|
|
|
* ```
|
|
|
|
*/
|
|
|
|
#if defined(MBEDTLS_TEST_HOOKS)
|
|
|
|
#define MBEDTLS_STATIC_TESTABLE
|
|
|
|
#else
|
|
|
|
#define MBEDTLS_STATIC_TESTABLE static
|
|
|
|
#endif
|
|
|
|
|
2021-07-07 17:29:43 +02:00
|
|
|
#if defined(MBEDTLS_TEST_HOOKS)
|
|
|
|
extern void (*mbedtls_test_hook_test_fail)( const char * test, int line, const char * file );
|
|
|
|
#define MBEDTLS_TEST_HOOK_TEST_ASSERT( TEST ) \
|
|
|
|
do { \
|
|
|
|
if( ( ! ( TEST ) ) && ( ( *mbedtls_test_hook_test_fail ) != NULL ) ) \
|
|
|
|
{ \
|
|
|
|
( *mbedtls_test_hook_test_fail )( #TEST, __LINE__, __FILE__ ); \
|
|
|
|
} \
|
|
|
|
} while( 0 )
|
|
|
|
#else
|
|
|
|
#define MBEDTLS_TEST_HOOK_TEST_ASSERT( TEST )
|
|
|
|
#endif /* defined(MBEDTLS_TEST_HOOKS) */
|
|
|
|
|
2021-05-27 14:39:53 +02:00
|
|
|
/** Allow library to access its structs' private members.
|
2021-05-14 22:20:10 +02:00
|
|
|
*
|
|
|
|
* Although structs defined in header files are publicly available,
|
|
|
|
* their members are private and should not be accessed by the user.
|
|
|
|
*/
|
|
|
|
#define MBEDTLS_ALLOW_PRIVATE_ACCESS
|
|
|
|
|
2022-11-22 15:01:39 +00:00
|
|
|
/**
|
|
|
|
* Perform a fast block XOR operation, such that
|
|
|
|
* r[i] = a[i] ^ b[i] where 0 <= i < n
|
|
|
|
*
|
|
|
|
* \param r Pointer to result (buffer of at least \p n bytes). \p r
|
|
|
|
* may be equal to either \p a or \p b, but behaviour when
|
|
|
|
* it overlaps in other ways is undefined.
|
|
|
|
* \param a Pointer to input (buffer of at least \p n bytes)
|
|
|
|
* \param b Pointer to input (buffer of at least \p n bytes)
|
|
|
|
* \param n Number of bytes to process.
|
|
|
|
*/
|
2022-11-23 14:50:03 +00:00
|
|
|
inline void mbedtls_xor( unsigned char *r, unsigned char const *a, unsigned char const *b, size_t n )
|
2022-11-22 15:01:39 +00:00
|
|
|
{
|
2022-11-22 18:55:17 +00:00
|
|
|
#if defined(MBEDTLS_ALLOW_UNALIGNED_ACCESS)
|
2022-11-23 19:42:13 +00:00
|
|
|
UNALIGNED_UINT32_T *a32 = (uint32_t *)a;
|
|
|
|
UNALIGNED_UINT32_T *b32 = (uint32_t *)b;
|
|
|
|
UNALIGNED_UINT32_T *r32 = (uint32_t *)r;
|
2022-11-23 14:02:00 +00:00
|
|
|
for ( size_t i = 0; i < ( n >> 2 ); i++ )
|
2022-11-22 15:01:39 +00:00
|
|
|
{
|
|
|
|
r32[i] = a32[i] ^ b32[i];
|
|
|
|
}
|
2022-11-23 14:02:00 +00:00
|
|
|
for ( size_t i = n - ( n % 4 ) ; i < n; i++ )
|
2022-11-22 15:01:39 +00:00
|
|
|
{
|
|
|
|
r[i] = a[i] ^ b[i];
|
|
|
|
}
|
2022-11-22 18:55:17 +00:00
|
|
|
#else
|
|
|
|
for ( size_t i = 0; i < n; i++ )
|
|
|
|
{
|
|
|
|
r[i] = a[i] ^ b[i];
|
|
|
|
}
|
|
|
|
#endif
|
2022-11-22 15:01:39 +00:00
|
|
|
}
|
|
|
|
|
2021-09-24 12:45:36 +08:00
|
|
|
/* Fix MSVC C99 compatible issue
|
|
|
|
* MSVC support __func__ from visual studio 2015( 1900 )
|
|
|
|
* Use MSVC predefine macro to avoid name check fail.
|
|
|
|
*/
|
|
|
|
#if (defined(_MSC_VER) && ( _MSC_VER <= 1900 ))
|
2021-09-28 16:13:44 +08:00
|
|
|
#define /*no-check-names*/ __func__ __FUNCTION__
|
2021-09-24 12:45:36 +08:00
|
|
|
#endif
|
|
|
|
|
2019-09-11 13:39:11 +02:00
|
|
|
#endif /* MBEDTLS_LIBRARY_COMMON_H */
|