Path: blob/master/thirdparty/mbedtls/library/common.h
9898 views
/**1* \file common.h2*3* \brief Utility macros for internal use in the library4*/5/*6* Copyright The Mbed TLS Contributors7* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later8*/910#ifndef MBEDTLS_LIBRARY_COMMON_H11#define MBEDTLS_LIBRARY_COMMON_H1213#include "mbedtls/build_info.h"14#include "alignment.h"1516#include <assert.h>17#include <stddef.h>18#include <stdint.h>19#include <stddef.h>2021#if defined(__ARM_NEON)22#include <arm_neon.h>23#define MBEDTLS_HAVE_NEON_INTRINSICS24#elif defined(MBEDTLS_PLATFORM_IS_WINDOWS_ON_ARM64)25#include <arm64_neon.h>26#define MBEDTLS_HAVE_NEON_INTRINSICS27#endif2829/** Helper to define a function as static except when building invasive tests.30*31* If a function is only used inside its own source file and should be32* declared `static` to allow the compiler to optimize for code size,33* but that function has unit tests, define it with34* ```35* MBEDTLS_STATIC_TESTABLE int mbedtls_foo(...) { ... }36* ```37* and declare it in a header in the `library/` directory with38* ```39* #if defined(MBEDTLS_TEST_HOOKS)40* int mbedtls_foo(...);41* #endif42* ```43*/44#if defined(MBEDTLS_TEST_HOOKS)45#define MBEDTLS_STATIC_TESTABLE46#else47#define MBEDTLS_STATIC_TESTABLE static48#endif4950#if defined(MBEDTLS_TEST_HOOKS)51extern void (*mbedtls_test_hook_test_fail)(const char *test, int line, const char *file);52#define MBEDTLS_TEST_HOOK_TEST_ASSERT(TEST) \53do { \54if ((!(TEST)) && ((*mbedtls_test_hook_test_fail) != NULL)) \55{ \56(*mbedtls_test_hook_test_fail)( #TEST, __LINE__, __FILE__); \57} \58} while (0)59#else60#define MBEDTLS_TEST_HOOK_TEST_ASSERT(TEST)61#endif /* defined(MBEDTLS_TEST_HOOKS) */6263/** \def ARRAY_LENGTH64* Return the number of elements of a static or stack array.65*66* \param array A value of array (not pointer) type.67*68* \return The number of elements of the array.69*/70/* A correct implementation of ARRAY_LENGTH, but which silently gives71* a nonsensical result if called with a pointer rather than an array. */72#define ARRAY_LENGTH_UNSAFE(array) \73(sizeof(array) / sizeof(*(array)))7475#if defined(__GNUC__)76/* Test if arg and &(arg)[0] have the same type. This is true if arg is77* an array but not if it's a pointer. */78#define IS_ARRAY_NOT_POINTER(arg) \79(!__builtin_types_compatible_p(__typeof__(arg), \80__typeof__(&(arg)[0])))81/* A compile-time constant with the value 0. If `const_expr` is not a82* compile-time constant with a nonzero value, cause a compile-time error. */83#define STATIC_ASSERT_EXPR(const_expr) \84(0 && sizeof(struct { unsigned int STATIC_ASSERT : 1 - 2 * !(const_expr); }))8586/* Return the scalar value `value` (possibly promoted). This is a compile-time87* constant if `value` is. `condition` must be a compile-time constant.88* If `condition` is false, arrange to cause a compile-time error. */89#define STATIC_ASSERT_THEN_RETURN(condition, value) \90(STATIC_ASSERT_EXPR(condition) ? 0 : (value))9192#define ARRAY_LENGTH(array) \93(STATIC_ASSERT_THEN_RETURN(IS_ARRAY_NOT_POINTER(array), \94ARRAY_LENGTH_UNSAFE(array)))9596#else97/* If we aren't sure the compiler supports our non-standard tricks,98* fall back to the unsafe implementation. */99#define ARRAY_LENGTH(array) ARRAY_LENGTH_UNSAFE(array)100#endif101/** Allow library to access its structs' private members.102*103* Although structs defined in header files are publicly available,104* their members are private and should not be accessed by the user.105*/106#define MBEDTLS_ALLOW_PRIVATE_ACCESS107108/**109* \brief Securely zeroize a buffer then free it.110*111* Similar to making consecutive calls to112* \c mbedtls_platform_zeroize() and \c mbedtls_free(), but has113* code size savings, and potential for optimisation in the future.114*115* Guaranteed to be a no-op if \p buf is \c NULL and \p len is 0.116*117* \param buf Buffer to be zeroized then freed.118* \param len Length of the buffer in bytes119*/120void mbedtls_zeroize_and_free(void *buf, size_t len);121122/** Return an offset into a buffer.123*124* This is just the addition of an offset to a pointer, except that this125* function also accepts an offset of 0 into a buffer whose pointer is null.126* (`p + n` has undefined behavior when `p` is null, even when `n == 0`.127* A null pointer is a valid buffer pointer when the size is 0, for example128* as the result of `malloc(0)` on some platforms.)129*130* \param p Pointer to a buffer of at least n bytes.131* This may be \p NULL if \p n is zero.132* \param n An offset in bytes.133* \return Pointer to offset \p n in the buffer \p p.134* Note that this is only a valid pointer if the size of the135* buffer is at least \p n + 1.136*/137static inline unsigned char *mbedtls_buffer_offset(138unsigned char *p, size_t n)139{140return p == NULL ? NULL : p + n;141}142143/** Return an offset into a read-only buffer.144*145* Similar to mbedtls_buffer_offset(), but for const pointers.146*147* \param p Pointer to a buffer of at least n bytes.148* This may be \p NULL if \p n is zero.149* \param n An offset in bytes.150* \return Pointer to offset \p n in the buffer \p p.151* Note that this is only a valid pointer if the size of the152* buffer is at least \p n + 1.153*/154static inline const unsigned char *mbedtls_buffer_offset_const(155const unsigned char *p, size_t n)156{157return p == NULL ? NULL : p + n;158}159160/* Always inline mbedtls_xor() for similar reasons as mbedtls_xor_no_simd(). */161#if defined(__IAR_SYSTEMS_ICC__)162#pragma inline = forced163#elif defined(__GNUC__)164__attribute__((always_inline))165#endif166/**167* Perform a fast block XOR operation, such that168* r[i] = a[i] ^ b[i] where 0 <= i < n169*170* \param r Pointer to result (buffer of at least \p n bytes). \p r171* may be equal to either \p a or \p b, but behaviour when172* it overlaps in other ways is undefined.173* \param a Pointer to input (buffer of at least \p n bytes)174* \param b Pointer to input (buffer of at least \p n bytes)175* \param n Number of bytes to process.176*177* \note Depending on the situation, it may be faster to use either mbedtls_xor() or178* mbedtls_xor_no_simd() (these are functionally equivalent).179* If the result is used immediately after the xor operation in non-SIMD code (e.g, in180* AES-CBC), there may be additional latency to transfer the data from SIMD to scalar181* registers, and in this case, mbedtls_xor_no_simd() may be faster. In other cases where182* the result is not used immediately (e.g., in AES-CTR), mbedtls_xor() may be faster.183* For targets without SIMD support, they will behave the same.184*/185static inline void mbedtls_xor(unsigned char *r,186const unsigned char *a,187const unsigned char *b,188size_t n)189{190size_t i = 0;191#if defined(MBEDTLS_EFFICIENT_UNALIGNED_ACCESS)192#if defined(MBEDTLS_HAVE_NEON_INTRINSICS) && \193(!(defined(MBEDTLS_COMPILER_IS_GCC) && MBEDTLS_GCC_VERSION < 70300))194/* Old GCC versions generate a warning here, so disable the NEON path for these compilers */195for (; (i + 16) <= n; i += 16) {196uint8x16_t v1 = vld1q_u8(a + i);197uint8x16_t v2 = vld1q_u8(b + i);198uint8x16_t x = veorq_u8(v1, v2);199vst1q_u8(r + i, x);200}201#if defined(__IAR_SYSTEMS_ICC__)202/* This if statement helps some compilers (e.g., IAR) optimise out the byte-by-byte tail case203* where n is a constant multiple of 16.204* For other compilers (e.g. recent gcc and clang) it makes no difference if n is a compile-time205* constant, and is a very small perf regression if n is not a compile-time constant. */206if (n % 16 == 0) {207return;208}209#endif210#elif defined(MBEDTLS_ARCH_IS_X64) || defined(MBEDTLS_ARCH_IS_ARM64)211/* This codepath probably only makes sense on architectures with 64-bit registers */212for (; (i + 8) <= n; i += 8) {213uint64_t x = mbedtls_get_unaligned_uint64(a + i) ^ mbedtls_get_unaligned_uint64(b + i);214mbedtls_put_unaligned_uint64(r + i, x);215}216#if defined(__IAR_SYSTEMS_ICC__)217if (n % 8 == 0) {218return;219}220#endif221#else222for (; (i + 4) <= n; i += 4) {223uint32_t x = mbedtls_get_unaligned_uint32(a + i) ^ mbedtls_get_unaligned_uint32(b + i);224mbedtls_put_unaligned_uint32(r + i, x);225}226#if defined(__IAR_SYSTEMS_ICC__)227if (n % 4 == 0) {228return;229}230#endif231#endif232#endif233for (; i < n; i++) {234r[i] = a[i] ^ b[i];235}236}237238/* Always inline mbedtls_xor_no_simd() as we see significant perf regressions when it does not get239* inlined (e.g., observed about 3x perf difference in gcm_mult_largetable with gcc 7 - 12) */240#if defined(__IAR_SYSTEMS_ICC__)241#pragma inline = forced242#elif defined(__GNUC__)243__attribute__((always_inline))244#endif245/**246* Perform a fast block XOR operation, such that247* r[i] = a[i] ^ b[i] where 0 <= i < n248*249* In some situations, this can perform better than mbedtls_xor() (e.g., it's about 5%250* better in AES-CBC).251*252* \param r Pointer to result (buffer of at least \p n bytes). \p r253* may be equal to either \p a or \p b, but behaviour when254* it overlaps in other ways is undefined.255* \param a Pointer to input (buffer of at least \p n bytes)256* \param b Pointer to input (buffer of at least \p n bytes)257* \param n Number of bytes to process.258*259* \note Depending on the situation, it may be faster to use either mbedtls_xor() or260* mbedtls_xor_no_simd() (these are functionally equivalent).261* If the result is used immediately after the xor operation in non-SIMD code (e.g, in262* AES-CBC), there may be additional latency to transfer the data from SIMD to scalar263* registers, and in this case, mbedtls_xor_no_simd() may be faster. In other cases where264* the result is not used immediately (e.g., in AES-CTR), mbedtls_xor() may be faster.265* For targets without SIMD support, they will behave the same.266*/267static inline void mbedtls_xor_no_simd(unsigned char *r,268const unsigned char *a,269const unsigned char *b,270size_t n)271{272size_t i = 0;273#if defined(MBEDTLS_EFFICIENT_UNALIGNED_ACCESS)274#if defined(MBEDTLS_ARCH_IS_X64) || defined(MBEDTLS_ARCH_IS_ARM64)275/* This codepath probably only makes sense on architectures with 64-bit registers */276for (; (i + 8) <= n; i += 8) {277uint64_t x = mbedtls_get_unaligned_uint64(a + i) ^ mbedtls_get_unaligned_uint64(b + i);278mbedtls_put_unaligned_uint64(r + i, x);279}280#if defined(__IAR_SYSTEMS_ICC__)281/* This if statement helps some compilers (e.g., IAR) optimise out the byte-by-byte tail case282* where n is a constant multiple of 8.283* For other compilers (e.g. recent gcc and clang) it makes no difference if n is a compile-time284* constant, and is a very small perf regression if n is not a compile-time constant. */285if (n % 8 == 0) {286return;287}288#endif289#else290for (; (i + 4) <= n; i += 4) {291uint32_t x = mbedtls_get_unaligned_uint32(a + i) ^ mbedtls_get_unaligned_uint32(b + i);292mbedtls_put_unaligned_uint32(r + i, x);293}294#if defined(__IAR_SYSTEMS_ICC__)295if (n % 4 == 0) {296return;297}298#endif299#endif300#endif301for (; i < n; i++) {302r[i] = a[i] ^ b[i];303}304}305306/* Fix MSVC C99 compatible issue307* MSVC support __func__ from visual studio 2015( 1900 )308* Use MSVC predefine macro to avoid name check fail.309*/310#if (defined(_MSC_VER) && (_MSC_VER <= 1900))311#define /*no-check-names*/ __func__ __FUNCTION__312#endif313314/* Define `asm` for compilers which don't define it. */315/* *INDENT-OFF* */316#ifndef asm317#if defined(__IAR_SYSTEMS_ICC__)318#define asm __asm319#else320#define asm __asm__321#endif322#endif323/* *INDENT-ON* */324325/*326* Define the constraint used for read-only pointer operands to aarch64 asm.327*328* This is normally the usual "r", but for aarch64_32 (aka ILP32,329* as found in watchos), "p" is required to avoid warnings from clang.330*331* Note that clang does not recognise '+p' or '=p', and armclang332* does not recognise 'p' at all. Therefore, to update a pointer from333* aarch64 assembly, it is necessary to use something like:334*335* uintptr_t uptr = (uintptr_t) ptr;336* asm( "ldr x4, [%x0], #8" ... : "+r" (uptr) : : )337* ptr = (void*) uptr;338*339* Note that the "x" in "%x0" is neccessary; writing "%0" will cause warnings.340*/341#if defined(__aarch64__) && defined(MBEDTLS_HAVE_ASM)342#if UINTPTR_MAX == 0xfffffffful343/* ILP32: Specify the pointer operand slightly differently, as per #7787. */344#define MBEDTLS_ASM_AARCH64_PTR_CONSTRAINT "p"345#elif UINTPTR_MAX == 0xfffffffffffffffful346/* Normal case (64-bit pointers): use "r" as the constraint for pointer operands to asm */347#define MBEDTLS_ASM_AARCH64_PTR_CONSTRAINT "r"348#else349#error "Unrecognised pointer size for aarch64"350#endif351#endif352353/* Always provide a static assert macro, so it can be used unconditionally.354* It does nothing on systems where we don't know how to define a static assert.355*/356/* Can't use the C11-style `defined(static_assert)` on FreeBSD, since it357* defines static_assert even with -std=c99, but then complains about it.358*/359#if defined(static_assert) && !defined(__FreeBSD__)360#define MBEDTLS_STATIC_ASSERT(expr, msg) static_assert(expr, msg)361#else362/* Make sure `MBEDTLS_STATIC_ASSERT(expr, msg);` is valid both inside and363* outside a function. We choose a struct declaration, which can be repeated364* any number of times and does not need a matching definition. */365#define MBEDTLS_STATIC_ASSERT(expr, msg) \366struct ISO_C_does_not_allow_extra_semicolon_outside_of_a_function367#endif368369#if defined(__has_builtin)370#define MBEDTLS_HAS_BUILTIN(x) __has_builtin(x)371#else372#define MBEDTLS_HAS_BUILTIN(x) 0373#endif374375/* Define compiler branch hints */376#if MBEDTLS_HAS_BUILTIN(__builtin_expect)377#define MBEDTLS_LIKELY(x) __builtin_expect(!!(x), 1)378#define MBEDTLS_UNLIKELY(x) __builtin_expect(!!(x), 0)379#else380#define MBEDTLS_LIKELY(x) x381#define MBEDTLS_UNLIKELY(x) x382#endif383384/* MBEDTLS_ASSUME may be used to provide additional information to the compiler385* which can result in smaller code-size. */386#if MBEDTLS_HAS_BUILTIN(__builtin_assume)387/* clang provides __builtin_assume */388#define MBEDTLS_ASSUME(x) __builtin_assume(x)389#elif MBEDTLS_HAS_BUILTIN(__builtin_unreachable)390/* gcc and IAR can use __builtin_unreachable */391#define MBEDTLS_ASSUME(x) do { if (!(x)) __builtin_unreachable(); } while (0)392#elif defined(_MSC_VER)393/* Supported by MSVC since VS 2005 */394#define MBEDTLS_ASSUME(x) __assume(x)395#else396#define MBEDTLS_ASSUME(x) do { } while (0)397#endif398399/* For gcc -Os, override with -O2 for a given function.400*401* This will not affect behaviour for other optimisation settings, e.g. -O0.402*/403#if defined(MBEDTLS_COMPILER_IS_GCC) && defined(__OPTIMIZE_SIZE__)404#define MBEDTLS_OPTIMIZE_FOR_PERFORMANCE __attribute__((optimize("-O2")))405#else406#define MBEDTLS_OPTIMIZE_FOR_PERFORMANCE407#endif408409/* Suppress compiler warnings for unused functions and variables. */410#if !defined(MBEDTLS_MAYBE_UNUSED) && defined(__has_attribute)411# if __has_attribute(unused)412# define MBEDTLS_MAYBE_UNUSED __attribute__((unused))413# endif414#endif415#if !defined(MBEDTLS_MAYBE_UNUSED) && defined(__GNUC__)416# define MBEDTLS_MAYBE_UNUSED __attribute__((unused))417#endif418#if !defined(MBEDTLS_MAYBE_UNUSED) && defined(__IAR_SYSTEMS_ICC__) && defined(__VER__)419/* IAR does support __attribute__((unused)), but only if the -e flag (extended language support)420* is given; the pragma always works.421* Unfortunately the pragma affects the rest of the file where it is used, but this is harmless.422* Check for version 5.2 or later - this pragma may be supported by earlier versions, but I wasn't423* able to find documentation).424*/425# if (__VER__ >= 5020000)426# define MBEDTLS_MAYBE_UNUSED _Pragma("diag_suppress=Pe177")427# endif428#endif429#if !defined(MBEDTLS_MAYBE_UNUSED) && defined(_MSC_VER)430# define MBEDTLS_MAYBE_UNUSED __pragma(warning(suppress:4189))431#endif432#if !defined(MBEDTLS_MAYBE_UNUSED)433# define MBEDTLS_MAYBE_UNUSED434#endif435436/* GCC >= 15 has a warning 'unterminated-string-initialization' which complains if you initialize437* a string into an array without space for a terminating NULL character. In some places in the438* codebase this behaviour is intended, so we add the macro MBEDTLS_ATTRIBUTE_UNTERMINATED_STRING439* to suppress the warning in these places.440*/441#if defined(__has_attribute)442#if __has_attribute(nonstring)443#define MBEDTLS_HAS_ATTRIBUTE_NONSTRING444#endif /* __has_attribute(nonstring) */445#endif /* __has_attribute */446#if defined(MBEDTLS_HAS_ATTRIBUTE_NONSTRING)447#define MBEDTLS_ATTRIBUTE_UNTERMINATED_STRING __attribute__((nonstring))448#else449#define MBEDTLS_ATTRIBUTE_UNTERMINATED_STRING450#endif /* MBEDTLS_HAS_ATTRIBUTE_NONSTRING */451452#endif /* MBEDTLS_LIBRARY_COMMON_H */453454455