1a8e1175bSopenharmony_ci/**
2a8e1175bSopenharmony_ci * \file common.h
3a8e1175bSopenharmony_ci *
4a8e1175bSopenharmony_ci * \brief Utility macros for internal use in the library
5a8e1175bSopenharmony_ci */
6a8e1175bSopenharmony_ci/*
7a8e1175bSopenharmony_ci *  Copyright The Mbed TLS Contributors
8a8e1175bSopenharmony_ci *  SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
9a8e1175bSopenharmony_ci */
10a8e1175bSopenharmony_ci
11a8e1175bSopenharmony_ci#ifndef MBEDTLS_LIBRARY_COMMON_H
12a8e1175bSopenharmony_ci#define MBEDTLS_LIBRARY_COMMON_H
13a8e1175bSopenharmony_ci
14a8e1175bSopenharmony_ci#include "mbedtls/build_info.h"
15a8e1175bSopenharmony_ci#include "alignment.h"
16a8e1175bSopenharmony_ci
17a8e1175bSopenharmony_ci#include <assert.h>
18a8e1175bSopenharmony_ci#include <stddef.h>
19a8e1175bSopenharmony_ci#include <stdint.h>
20a8e1175bSopenharmony_ci#include <stddef.h>
21a8e1175bSopenharmony_ci
22a8e1175bSopenharmony_ci#if defined(__ARM_NEON)
23a8e1175bSopenharmony_ci#include <arm_neon.h>
24a8e1175bSopenharmony_ci#define MBEDTLS_HAVE_NEON_INTRINSICS
25a8e1175bSopenharmony_ci#elif defined(MBEDTLS_PLATFORM_IS_WINDOWS_ON_ARM64)
26a8e1175bSopenharmony_ci#include <arm64_neon.h>
27a8e1175bSopenharmony_ci#define MBEDTLS_HAVE_NEON_INTRINSICS
28a8e1175bSopenharmony_ci#endif
29a8e1175bSopenharmony_ci
30a8e1175bSopenharmony_ci/** Helper to define a function as static except when building invasive tests.
31a8e1175bSopenharmony_ci *
32a8e1175bSopenharmony_ci * If a function is only used inside its own source file and should be
33a8e1175bSopenharmony_ci * declared `static` to allow the compiler to optimize for code size,
34a8e1175bSopenharmony_ci * but that function has unit tests, define it with
35a8e1175bSopenharmony_ci * ```
36a8e1175bSopenharmony_ci * MBEDTLS_STATIC_TESTABLE int mbedtls_foo(...) { ... }
37a8e1175bSopenharmony_ci * ```
38a8e1175bSopenharmony_ci * and declare it in a header in the `library/` directory with
39a8e1175bSopenharmony_ci * ```
40a8e1175bSopenharmony_ci * #if defined(MBEDTLS_TEST_HOOKS)
41a8e1175bSopenharmony_ci * int mbedtls_foo(...);
42a8e1175bSopenharmony_ci * #endif
43a8e1175bSopenharmony_ci * ```
44a8e1175bSopenharmony_ci */
45a8e1175bSopenharmony_ci#if defined(MBEDTLS_TEST_HOOKS)
46a8e1175bSopenharmony_ci#define MBEDTLS_STATIC_TESTABLE
47a8e1175bSopenharmony_ci#else
48a8e1175bSopenharmony_ci#define MBEDTLS_STATIC_TESTABLE static
49a8e1175bSopenharmony_ci#endif
50a8e1175bSopenharmony_ci
51a8e1175bSopenharmony_ci#if defined(MBEDTLS_TEST_HOOKS)
52a8e1175bSopenharmony_ciextern void (*mbedtls_test_hook_test_fail)(const char *test, int line, const char *file);
53a8e1175bSopenharmony_ci#define MBEDTLS_TEST_HOOK_TEST_ASSERT(TEST) \
54a8e1175bSopenharmony_ci    do { \
55a8e1175bSopenharmony_ci        if ((!(TEST)) && ((*mbedtls_test_hook_test_fail) != NULL)) \
56a8e1175bSopenharmony_ci        { \
57a8e1175bSopenharmony_ci            (*mbedtls_test_hook_test_fail)( #TEST, __LINE__, __FILE__); \
58a8e1175bSopenharmony_ci        } \
59a8e1175bSopenharmony_ci    } while (0)
60a8e1175bSopenharmony_ci#else
61a8e1175bSopenharmony_ci#define MBEDTLS_TEST_HOOK_TEST_ASSERT(TEST)
62a8e1175bSopenharmony_ci#endif /* defined(MBEDTLS_TEST_HOOKS) */
63a8e1175bSopenharmony_ci
64a8e1175bSopenharmony_ci/** \def ARRAY_LENGTH
65a8e1175bSopenharmony_ci * Return the number of elements of a static or stack array.
66a8e1175bSopenharmony_ci *
67a8e1175bSopenharmony_ci * \param array         A value of array (not pointer) type.
68a8e1175bSopenharmony_ci *
69a8e1175bSopenharmony_ci * \return The number of elements of the array.
70a8e1175bSopenharmony_ci */
71a8e1175bSopenharmony_ci/* A correct implementation of ARRAY_LENGTH, but which silently gives
72a8e1175bSopenharmony_ci * a nonsensical result if called with a pointer rather than an array. */
73a8e1175bSopenharmony_ci#define ARRAY_LENGTH_UNSAFE(array)            \
74a8e1175bSopenharmony_ci    (sizeof(array) / sizeof(*(array)))
75a8e1175bSopenharmony_ci
76a8e1175bSopenharmony_ci#if defined(__GNUC__)
77a8e1175bSopenharmony_ci/* Test if arg and &(arg)[0] have the same type. This is true if arg is
78a8e1175bSopenharmony_ci * an array but not if it's a pointer. */
79a8e1175bSopenharmony_ci#define IS_ARRAY_NOT_POINTER(arg)                                     \
80a8e1175bSopenharmony_ci    (!__builtin_types_compatible_p(__typeof__(arg),                \
81a8e1175bSopenharmony_ci                                   __typeof__(&(arg)[0])))
82a8e1175bSopenharmony_ci/* A compile-time constant with the value 0. If `const_expr` is not a
83a8e1175bSopenharmony_ci * compile-time constant with a nonzero value, cause a compile-time error. */
84a8e1175bSopenharmony_ci#define STATIC_ASSERT_EXPR(const_expr)                                \
85a8e1175bSopenharmony_ci    (0 && sizeof(struct { unsigned int STATIC_ASSERT : 1 - 2 * !(const_expr); }))
86a8e1175bSopenharmony_ci
87a8e1175bSopenharmony_ci/* Return the scalar value `value` (possibly promoted). This is a compile-time
88a8e1175bSopenharmony_ci * constant if `value` is. `condition` must be a compile-time constant.
89a8e1175bSopenharmony_ci * If `condition` is false, arrange to cause a compile-time error. */
90a8e1175bSopenharmony_ci#define STATIC_ASSERT_THEN_RETURN(condition, value)   \
91a8e1175bSopenharmony_ci    (STATIC_ASSERT_EXPR(condition) ? 0 : (value))
92a8e1175bSopenharmony_ci
93a8e1175bSopenharmony_ci#define ARRAY_LENGTH(array)                                           \
94a8e1175bSopenharmony_ci    (STATIC_ASSERT_THEN_RETURN(IS_ARRAY_NOT_POINTER(array),         \
95a8e1175bSopenharmony_ci                               ARRAY_LENGTH_UNSAFE(array)))
96a8e1175bSopenharmony_ci
97a8e1175bSopenharmony_ci#else
98a8e1175bSopenharmony_ci/* If we aren't sure the compiler supports our non-standard tricks,
99a8e1175bSopenharmony_ci * fall back to the unsafe implementation. */
100a8e1175bSopenharmony_ci#define ARRAY_LENGTH(array) ARRAY_LENGTH_UNSAFE(array)
101a8e1175bSopenharmony_ci#endif
102a8e1175bSopenharmony_ci/** Allow library to access its structs' private members.
103a8e1175bSopenharmony_ci *
104a8e1175bSopenharmony_ci * Although structs defined in header files are publicly available,
105a8e1175bSopenharmony_ci * their members are private and should not be accessed by the user.
106a8e1175bSopenharmony_ci */
107a8e1175bSopenharmony_ci#define MBEDTLS_ALLOW_PRIVATE_ACCESS
108a8e1175bSopenharmony_ci
109a8e1175bSopenharmony_ci/**
110a8e1175bSopenharmony_ci * \brief       Securely zeroize a buffer then free it.
111a8e1175bSopenharmony_ci *
112a8e1175bSopenharmony_ci *              Similar to making consecutive calls to
113a8e1175bSopenharmony_ci *              \c mbedtls_platform_zeroize() and \c mbedtls_free(), but has
114a8e1175bSopenharmony_ci *              code size savings, and potential for optimisation in the future.
115a8e1175bSopenharmony_ci *
116a8e1175bSopenharmony_ci *              Guaranteed to be a no-op if \p buf is \c NULL and \p len is 0.
117a8e1175bSopenharmony_ci *
118a8e1175bSopenharmony_ci * \param buf   Buffer to be zeroized then freed.
119a8e1175bSopenharmony_ci * \param len   Length of the buffer in bytes
120a8e1175bSopenharmony_ci */
121a8e1175bSopenharmony_civoid mbedtls_zeroize_and_free(void *buf, size_t len);
122a8e1175bSopenharmony_ci
123a8e1175bSopenharmony_ci/** Return an offset into a buffer.
124a8e1175bSopenharmony_ci *
125a8e1175bSopenharmony_ci * This is just the addition of an offset to a pointer, except that this
126a8e1175bSopenharmony_ci * function also accepts an offset of 0 into a buffer whose pointer is null.
127a8e1175bSopenharmony_ci * (`p + n` has undefined behavior when `p` is null, even when `n == 0`.
128a8e1175bSopenharmony_ci * A null pointer is a valid buffer pointer when the size is 0, for example
129a8e1175bSopenharmony_ci * as the result of `malloc(0)` on some platforms.)
130a8e1175bSopenharmony_ci *
131a8e1175bSopenharmony_ci * \param p     Pointer to a buffer of at least n bytes.
132a8e1175bSopenharmony_ci *              This may be \p NULL if \p n is zero.
133a8e1175bSopenharmony_ci * \param n     An offset in bytes.
134a8e1175bSopenharmony_ci * \return      Pointer to offset \p n in the buffer \p p.
135a8e1175bSopenharmony_ci *              Note that this is only a valid pointer if the size of the
136a8e1175bSopenharmony_ci *              buffer is at least \p n + 1.
137a8e1175bSopenharmony_ci */
138a8e1175bSopenharmony_cistatic inline unsigned char *mbedtls_buffer_offset(
139a8e1175bSopenharmony_ci    unsigned char *p, size_t n)
140a8e1175bSopenharmony_ci{
141a8e1175bSopenharmony_ci    return p == NULL ? NULL : p + n;
142a8e1175bSopenharmony_ci}
143a8e1175bSopenharmony_ci
144a8e1175bSopenharmony_ci/** Return an offset into a read-only buffer.
145a8e1175bSopenharmony_ci *
146a8e1175bSopenharmony_ci * Similar to mbedtls_buffer_offset(), but for const pointers.
147a8e1175bSopenharmony_ci *
148a8e1175bSopenharmony_ci * \param p     Pointer to a buffer of at least n bytes.
149a8e1175bSopenharmony_ci *              This may be \p NULL if \p n is zero.
150a8e1175bSopenharmony_ci * \param n     An offset in bytes.
151a8e1175bSopenharmony_ci * \return      Pointer to offset \p n in the buffer \p p.
152a8e1175bSopenharmony_ci *              Note that this is only a valid pointer if the size of the
153a8e1175bSopenharmony_ci *              buffer is at least \p n + 1.
154a8e1175bSopenharmony_ci */
155a8e1175bSopenharmony_cistatic inline const unsigned char *mbedtls_buffer_offset_const(
156a8e1175bSopenharmony_ci    const unsigned char *p, size_t n)
157a8e1175bSopenharmony_ci{
158a8e1175bSopenharmony_ci    return p == NULL ? NULL : p + n;
159a8e1175bSopenharmony_ci}
160a8e1175bSopenharmony_ci
161a8e1175bSopenharmony_civoid mbedtls_xor(unsigned char *r, const unsigned char *a, const unsigned char *b, size_t n);
162a8e1175bSopenharmony_ci
163a8e1175bSopenharmony_civoid mbedtls_xor_no_simd(unsigned char *r,
164a8e1175bSopenharmony_ci                         const unsigned char *a,
165a8e1175bSopenharmony_ci                         const unsigned char *b,
166a8e1175bSopenharmony_ci                         size_t n);
167a8e1175bSopenharmony_ci
168a8e1175bSopenharmony_ci/* Fix MSVC C99 compatible issue
169a8e1175bSopenharmony_ci *      MSVC support __func__ from visual studio 2015( 1900 )
170a8e1175bSopenharmony_ci *      Use MSVC predefine macro to avoid name check fail.
171a8e1175bSopenharmony_ci */
172a8e1175bSopenharmony_ci#if (defined(_MSC_VER) && (_MSC_VER <= 1900))
173a8e1175bSopenharmony_ci#define /*no-check-names*/ __func__ __FUNCTION__
174a8e1175bSopenharmony_ci#endif
175a8e1175bSopenharmony_ci
176a8e1175bSopenharmony_ci/* Define `asm` for compilers which don't define it. */
177a8e1175bSopenharmony_ci/* *INDENT-OFF* */
178a8e1175bSopenharmony_ci#ifndef asm
179a8e1175bSopenharmony_ci#if defined(__IAR_SYSTEMS_ICC__)
180a8e1175bSopenharmony_ci#define asm __asm
181a8e1175bSopenharmony_ci#else
182a8e1175bSopenharmony_ci#define asm __asm__
183a8e1175bSopenharmony_ci#endif
184a8e1175bSopenharmony_ci#endif
185a8e1175bSopenharmony_ci/* *INDENT-ON* */
186a8e1175bSopenharmony_ci
187a8e1175bSopenharmony_ci/*
188a8e1175bSopenharmony_ci * Define the constraint used for read-only pointer operands to aarch64 asm.
189a8e1175bSopenharmony_ci *
190a8e1175bSopenharmony_ci * This is normally the usual "r", but for aarch64_32 (aka ILP32,
191a8e1175bSopenharmony_ci * as found in watchos), "p" is required to avoid warnings from clang.
192a8e1175bSopenharmony_ci *
193a8e1175bSopenharmony_ci * Note that clang does not recognise '+p' or '=p', and armclang
194a8e1175bSopenharmony_ci * does not recognise 'p' at all. Therefore, to update a pointer from
195a8e1175bSopenharmony_ci * aarch64 assembly, it is necessary to use something like:
196a8e1175bSopenharmony_ci *
197a8e1175bSopenharmony_ci * uintptr_t uptr = (uintptr_t) ptr;
198a8e1175bSopenharmony_ci * asm( "ldr x4, [%x0], #8" ... : "+r" (uptr) : : )
199a8e1175bSopenharmony_ci * ptr = (void*) uptr;
200a8e1175bSopenharmony_ci *
201a8e1175bSopenharmony_ci * Note that the "x" in "%x0" is neccessary; writing "%0" will cause warnings.
202a8e1175bSopenharmony_ci */
203a8e1175bSopenharmony_ci#if defined(__aarch64__) && defined(MBEDTLS_HAVE_ASM)
204a8e1175bSopenharmony_ci#if UINTPTR_MAX == 0xfffffffful
205a8e1175bSopenharmony_ci/* ILP32: Specify the pointer operand slightly differently, as per #7787. */
206a8e1175bSopenharmony_ci#define MBEDTLS_ASM_AARCH64_PTR_CONSTRAINT "p"
207a8e1175bSopenharmony_ci#elif UINTPTR_MAX == 0xfffffffffffffffful
208a8e1175bSopenharmony_ci/* Normal case (64-bit pointers): use "r" as the constraint for pointer operands to asm */
209a8e1175bSopenharmony_ci#define MBEDTLS_ASM_AARCH64_PTR_CONSTRAINT "r"
210a8e1175bSopenharmony_ci#else
211a8e1175bSopenharmony_ci#error "Unrecognised pointer size for aarch64"
212a8e1175bSopenharmony_ci#endif
213a8e1175bSopenharmony_ci#endif
214a8e1175bSopenharmony_ci
215a8e1175bSopenharmony_ci/* Always provide a static assert macro, so it can be used unconditionally.
216a8e1175bSopenharmony_ci * It will expand to nothing on some systems.
217a8e1175bSopenharmony_ci * Can be used outside functions (but don't add a trailing ';' in that case:
218a8e1175bSopenharmony_ci * the semicolon is included here to avoid triggering -Wextra-semi when
219a8e1175bSopenharmony_ci * MBEDTLS_STATIC_ASSERT() expands to nothing).
220a8e1175bSopenharmony_ci * Can't use the C11-style `defined(static_assert)` on FreeBSD, since it
221a8e1175bSopenharmony_ci * defines static_assert even with -std=c99, but then complains about it.
222a8e1175bSopenharmony_ci */
223a8e1175bSopenharmony_ci#if defined(static_assert) && !defined(__FreeBSD__)
224a8e1175bSopenharmony_ci#define MBEDTLS_STATIC_ASSERT(expr, msg)    static_assert(expr, msg);
225a8e1175bSopenharmony_ci#else
226a8e1175bSopenharmony_ci#define MBEDTLS_STATIC_ASSERT(expr, msg)
227a8e1175bSopenharmony_ci#endif
228a8e1175bSopenharmony_ci
229a8e1175bSopenharmony_ci#if defined(__has_builtin)
230a8e1175bSopenharmony_ci#define MBEDTLS_HAS_BUILTIN(x) __has_builtin(x)
231a8e1175bSopenharmony_ci#else
232a8e1175bSopenharmony_ci#define MBEDTLS_HAS_BUILTIN(x) 0
233a8e1175bSopenharmony_ci#endif
234a8e1175bSopenharmony_ci
235a8e1175bSopenharmony_ci/* Define compiler branch hints */
236a8e1175bSopenharmony_ci#if MBEDTLS_HAS_BUILTIN(__builtin_expect)
237a8e1175bSopenharmony_ci#define MBEDTLS_LIKELY(x)       __builtin_expect(!!(x), 1)
238a8e1175bSopenharmony_ci#define MBEDTLS_UNLIKELY(x)     __builtin_expect(!!(x), 0)
239a8e1175bSopenharmony_ci#else
240a8e1175bSopenharmony_ci#define MBEDTLS_LIKELY(x)       x
241a8e1175bSopenharmony_ci#define MBEDTLS_UNLIKELY(x)     x
242a8e1175bSopenharmony_ci#endif
243a8e1175bSopenharmony_ci
244a8e1175bSopenharmony_ci/* MBEDTLS_ASSUME may be used to provide additional information to the compiler
245a8e1175bSopenharmony_ci * which can result in smaller code-size. */
246a8e1175bSopenharmony_ci#if MBEDTLS_HAS_BUILTIN(__builtin_assume)
247a8e1175bSopenharmony_ci/* clang provides __builtin_assume */
248a8e1175bSopenharmony_ci#define MBEDTLS_ASSUME(x)       __builtin_assume(x)
249a8e1175bSopenharmony_ci#elif MBEDTLS_HAS_BUILTIN(__builtin_unreachable)
250a8e1175bSopenharmony_ci/* gcc and IAR can use __builtin_unreachable */
251a8e1175bSopenharmony_ci#define MBEDTLS_ASSUME(x)       do { if (!(x)) __builtin_unreachable(); } while (0)
252a8e1175bSopenharmony_ci#elif defined(_MSC_VER)
253a8e1175bSopenharmony_ci/* Supported by MSVC since VS 2005 */
254a8e1175bSopenharmony_ci#define MBEDTLS_ASSUME(x)       __assume(x)
255a8e1175bSopenharmony_ci#else
256a8e1175bSopenharmony_ci#define MBEDTLS_ASSUME(x)       do { } while (0)
257a8e1175bSopenharmony_ci#endif
258a8e1175bSopenharmony_ci
259a8e1175bSopenharmony_ci/* For gcc -Os, override with -O2 for a given function.
260a8e1175bSopenharmony_ci *
261a8e1175bSopenharmony_ci * This will not affect behaviour for other optimisation settings, e.g. -O0.
262a8e1175bSopenharmony_ci */
263a8e1175bSopenharmony_ci#if defined(MBEDTLS_COMPILER_IS_GCC) && defined(__OPTIMIZE_SIZE__)
264a8e1175bSopenharmony_ci#define MBEDTLS_OPTIMIZE_FOR_PERFORMANCE __attribute__((optimize("-O2")))
265a8e1175bSopenharmony_ci#else
266a8e1175bSopenharmony_ci#define MBEDTLS_OPTIMIZE_FOR_PERFORMANCE
267a8e1175bSopenharmony_ci#endif
268a8e1175bSopenharmony_ci
269a8e1175bSopenharmony_ci/* Suppress compiler warnings for unused functions and variables. */
270a8e1175bSopenharmony_ci#if !defined(MBEDTLS_MAYBE_UNUSED) && defined(__has_attribute)
271a8e1175bSopenharmony_ci#    if __has_attribute(unused)
272a8e1175bSopenharmony_ci#        define MBEDTLS_MAYBE_UNUSED __attribute__((unused))
273a8e1175bSopenharmony_ci#    endif
274a8e1175bSopenharmony_ci#endif
275a8e1175bSopenharmony_ci#if !defined(MBEDTLS_MAYBE_UNUSED) && defined(__GNUC__)
276a8e1175bSopenharmony_ci#    define MBEDTLS_MAYBE_UNUSED __attribute__((unused))
277a8e1175bSopenharmony_ci#endif
278a8e1175bSopenharmony_ci#if !defined(MBEDTLS_MAYBE_UNUSED) && defined(__IAR_SYSTEMS_ICC__) && defined(__VER__)
279a8e1175bSopenharmony_ci/* IAR does support __attribute__((unused)), but only if the -e flag (extended language support)
280a8e1175bSopenharmony_ci * is given; the pragma always works.
281a8e1175bSopenharmony_ci * Unfortunately the pragma affects the rest of the file where it is used, but this is harmless.
282a8e1175bSopenharmony_ci * Check for version 5.2 or later - this pragma may be supported by earlier versions, but I wasn't
283a8e1175bSopenharmony_ci * able to find documentation).
284a8e1175bSopenharmony_ci */
285a8e1175bSopenharmony_ci#    if (__VER__ >= 5020000)
286a8e1175bSopenharmony_ci#        define MBEDTLS_MAYBE_UNUSED _Pragma("diag_suppress=Pe177")
287a8e1175bSopenharmony_ci#    endif
288a8e1175bSopenharmony_ci#endif
289a8e1175bSopenharmony_ci#if !defined(MBEDTLS_MAYBE_UNUSED) && defined(_MSC_VER)
290a8e1175bSopenharmony_ci#    define MBEDTLS_MAYBE_UNUSED __pragma(warning(suppress:4189))
291a8e1175bSopenharmony_ci#endif
292a8e1175bSopenharmony_ci#if !defined(MBEDTLS_MAYBE_UNUSED)
293a8e1175bSopenharmony_ci#    define MBEDTLS_MAYBE_UNUSED
294a8e1175bSopenharmony_ci#endif
295a8e1175bSopenharmony_ci
296a8e1175bSopenharmony_ci#endif /* MBEDTLS_LIBRARY_COMMON_H */
297