xref: /optee_os/lib/libmbedtls/mbedtls/library/common.h (revision 6b1c18580069a7c71e32deb57f609031fffb6e68)
1 /**
2  * \file common.h
3  *
4  * \brief Utility macros for internal use in the library
5  */
6 /*
7  *  Copyright The Mbed TLS Contributors
8  *  SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
9  */
10 
11 #ifndef MBEDTLS_LIBRARY_COMMON_H
12 #define MBEDTLS_LIBRARY_COMMON_H
13 
14 #include "mbedtls/build_info.h"
15 #include "alignment.h"
16 
17 #include <assert.h>
18 #include <stddef.h>
19 #include <stdint.h>
20 #include <stddef.h>
21 
22 #if defined(__ARM_NEON)
23 /*
24  * Undefine and restore __section and __data from compiler.h to prevent
25  * collision with arm_neon.h
26  */
27 #pragma push_macro("__section")
28 #pragma push_macro("__data")
29 #undef __section
30 #undef __data
31 #include <arm_neon.h>
32 #pragma pop_macro("__data")
33 #pragma pop_macro("__section")
34 #define MBEDTLS_HAVE_NEON_INTRINSICS
35 #elif defined(MBEDTLS_PLATFORM_IS_WINDOWS_ON_ARM64)
36 #include <arm64_neon.h>
37 #define MBEDTLS_HAVE_NEON_INTRINSICS
38 #endif
39 
40 /** Helper to define a function as static except when building invasive tests.
41  *
42  * If a function is only used inside its own source file and should be
43  * declared `static` to allow the compiler to optimize for code size,
44  * but that function has unit tests, define it with
45  * ```
46  * MBEDTLS_STATIC_TESTABLE int mbedtls_foo(...) { ... }
47  * ```
48  * and declare it in a header in the `library/` directory with
49  * ```
50  * #if defined(MBEDTLS_TEST_HOOKS)
51  * int mbedtls_foo(...);
52  * #endif
53  * ```
54  */
55 #if defined(MBEDTLS_TEST_HOOKS)
56 #define MBEDTLS_STATIC_TESTABLE
57 #else
58 #define MBEDTLS_STATIC_TESTABLE static
59 #endif
60 
61 #if defined(MBEDTLS_TEST_HOOKS)
62 extern void (*mbedtls_test_hook_test_fail)(const char *test, int line, const char *file);
63 #define MBEDTLS_TEST_HOOK_TEST_ASSERT(TEST) \
64     do { \
65         if ((!(TEST)) && ((*mbedtls_test_hook_test_fail) != NULL)) \
66         { \
67             (*mbedtls_test_hook_test_fail)( #TEST, __LINE__, __FILE__); \
68         } \
69     } while (0)
70 #else
71 #define MBEDTLS_TEST_HOOK_TEST_ASSERT(TEST)
72 #endif /* defined(MBEDTLS_TEST_HOOKS) */
73 
74 /** \def ARRAY_LENGTH
75  * Return the number of elements of a static or stack array.
76  *
77  * \param array         A value of array (not pointer) type.
78  *
79  * \return The number of elements of the array.
80  */
81 /* A correct implementation of ARRAY_LENGTH, but which silently gives
82  * a nonsensical result if called with a pointer rather than an array. */
83 #define ARRAY_LENGTH_UNSAFE(array)            \
84     (sizeof(array) / sizeof(*(array)))
85 
86 #if defined(__GNUC__)
87 /* Test if arg and &(arg)[0] have the same type. This is true if arg is
88  * an array but not if it's a pointer. */
89 #define IS_ARRAY_NOT_POINTER(arg)                                     \
90     (!__builtin_types_compatible_p(__typeof__(arg),                \
91                                    __typeof__(&(arg)[0])))
92 /* A compile-time constant with the value 0. If `const_expr` is not a
93  * compile-time constant with a nonzero value, cause a compile-time error. */
94 #define STATIC_ASSERT_EXPR(const_expr)                                \
95     (0 && sizeof(struct { unsigned int STATIC_ASSERT : 1 - 2 * !(const_expr); }))
96 
97 /* Return the scalar value `value` (possibly promoted). This is a compile-time
98  * constant if `value` is. `condition` must be a compile-time constant.
99  * If `condition` is false, arrange to cause a compile-time error. */
100 #define STATIC_ASSERT_THEN_RETURN(condition, value)   \
101     (STATIC_ASSERT_EXPR(condition) ? 0 : (value))
102 
103 #define ARRAY_LENGTH(array)                                           \
104     (STATIC_ASSERT_THEN_RETURN(IS_ARRAY_NOT_POINTER(array),         \
105                                ARRAY_LENGTH_UNSAFE(array)))
106 
107 #else
108 /* If we aren't sure the compiler supports our non-standard tricks,
109  * fall back to the unsafe implementation. */
110 #define ARRAY_LENGTH(array) ARRAY_LENGTH_UNSAFE(array)
111 #endif
112 /** Allow library to access its structs' private members.
113  *
114  * Although structs defined in header files are publicly available,
115  * their members are private and should not be accessed by the user.
116  */
117 #define MBEDTLS_ALLOW_PRIVATE_ACCESS
118 
119 /**
120  * \brief       Securely zeroize a buffer then free it.
121  *
122  *              Similar to making consecutive calls to
123  *              \c mbedtls_platform_zeroize() and \c mbedtls_free(), but has
124  *              code size savings, and potential for optimisation in the future.
125  *
126  *              Guaranteed to be a no-op if \p buf is \c NULL and \p len is 0.
127  *
128  * \param buf   Buffer to be zeroized then freed.
129  * \param len   Length of the buffer in bytes
130  */
131 void mbedtls_zeroize_and_free(void *buf, size_t len);
132 
133 /** Return an offset into a buffer.
134  *
135  * This is just the addition of an offset to a pointer, except that this
136  * function also accepts an offset of 0 into a buffer whose pointer is null.
137  * (`p + n` has undefined behavior when `p` is null, even when `n == 0`.
138  * A null pointer is a valid buffer pointer when the size is 0, for example
139  * as the result of `malloc(0)` on some platforms.)
140  *
141  * \param p     Pointer to a buffer of at least n bytes.
142  *              This may be \p NULL if \p n is zero.
143  * \param n     An offset in bytes.
144  * \return      Pointer to offset \p n in the buffer \p p.
145  *              Note that this is only a valid pointer if the size of the
146  *              buffer is at least \p n + 1.
147  */
148 static inline unsigned char *mbedtls_buffer_offset(
149     unsigned char *p, size_t n)
150 {
151     return p == NULL ? NULL : p + n;
152 }
153 
154 /** Return an offset into a read-only buffer.
155  *
156  * Similar to mbedtls_buffer_offset(), but for const pointers.
157  *
158  * \param p     Pointer to a buffer of at least n bytes.
159  *              This may be \p NULL if \p n is zero.
160  * \param n     An offset in bytes.
161  * \return      Pointer to offset \p n in the buffer \p p.
162  *              Note that this is only a valid pointer if the size of the
163  *              buffer is at least \p n + 1.
164  */
165 static inline const unsigned char *mbedtls_buffer_offset_const(
166     const unsigned char *p, size_t n)
167 {
168     return p == NULL ? NULL : p + n;
169 }
170 
171 /* Always inline mbedtls_xor() for similar reasons as mbedtls_xor_no_simd(). */
172 #if defined(__IAR_SYSTEMS_ICC__)
173 #pragma inline = forced
174 #elif defined(__GNUC__)
175 __attribute__((always_inline))
176 #endif
177 /**
178  * Perform a fast block XOR operation, such that
179  * r[i] = a[i] ^ b[i] where 0 <= i < n
180  *
181  * \param   r Pointer to result (buffer of at least \p n bytes). \p r
182  *            may be equal to either \p a or \p b, but behaviour when
183  *            it overlaps in other ways is undefined.
184  * \param   a Pointer to input (buffer of at least \p n bytes)
185  * \param   b Pointer to input (buffer of at least \p n bytes)
186  * \param   n Number of bytes to process.
187  *
188  * \note      Depending on the situation, it may be faster to use either mbedtls_xor() or
189  *            mbedtls_xor_no_simd() (these are functionally equivalent).
190  *            If the result is used immediately after the xor operation in non-SIMD code (e.g, in
191  *            AES-CBC), there may be additional latency to transfer the data from SIMD to scalar
192  *            registers, and in this case, mbedtls_xor_no_simd() may be faster. In other cases where
193  *            the result is not used immediately (e.g., in AES-CTR), mbedtls_xor() may be faster.
194  *            For targets without SIMD support, they will behave the same.
195  */
196 static inline void mbedtls_xor(unsigned char *r,
197                                const unsigned char *a,
198                                const unsigned char *b,
199                                size_t n)
200 {
201     size_t i = 0;
202 #if defined(MBEDTLS_EFFICIENT_UNALIGNED_ACCESS)
203 #if defined(MBEDTLS_HAVE_NEON_INTRINSICS) && \
204     (!(defined(MBEDTLS_COMPILER_IS_GCC) && MBEDTLS_GCC_VERSION < 70300))
205     /* Old GCC versions generate a warning here, so disable the NEON path for these compilers */
206     for (; (i + 16) <= n; i += 16) {
207         uint8x16_t v1 = vld1q_u8(a + i);
208         uint8x16_t v2 = vld1q_u8(b + i);
209         uint8x16_t x = veorq_u8(v1, v2);
210         vst1q_u8(r + i, x);
211     }
212 #if defined(__IAR_SYSTEMS_ICC__) || defined(MBEDTLS_COMPILER_IS_GCC)
213     /* This if statement helps some compilers (e.g., IAR) optimise out the byte-by-byte tail case
214      * where n is a constant multiple of 16.
215      * For other compilers (e.g. recent gcc and clang) it makes no difference if n is a compile-time
216      * constant, and is a very small perf regression if n is not a compile-time constant.
217      * GCC 14.2 outputs a warning "array subscript 48 is outside array bounds" if we don't return
218      * early. */
219     if (n % 16 == 0) {
220         return;
221     }
222 #endif
223 #elif defined(MBEDTLS_ARCH_IS_X64) || defined(MBEDTLS_ARCH_IS_ARM64)
224     /* This codepath probably only makes sense on architectures with 64-bit registers */
225     for (; (i + 8) <= n; i += 8) {
226         uint64_t x = mbedtls_get_unaligned_uint64(a + i) ^ mbedtls_get_unaligned_uint64(b + i);
227         mbedtls_put_unaligned_uint64(r + i, x);
228     }
229 #if defined(__IAR_SYSTEMS_ICC__)
230     if (n % 8 == 0) {
231         return;
232     }
233 #endif
234 #else
235     for (; (i + 4) <= n; i += 4) {
236         uint32_t x = mbedtls_get_unaligned_uint32(a + i) ^ mbedtls_get_unaligned_uint32(b + i);
237         mbedtls_put_unaligned_uint32(r + i, x);
238     }
239 #if defined(__IAR_SYSTEMS_ICC__)
240     if (n % 4 == 0) {
241         return;
242     }
243 #endif
244 #endif
245 #endif
246     for (; i < n; i++) {
247         r[i] = a[i] ^ b[i];
248     }
249 }
250 
251 /* Always inline mbedtls_xor_no_simd() as we see significant perf regressions when it does not get
252  * inlined (e.g., observed about 3x perf difference in gcm_mult_largetable with gcc 7 - 12) */
253 #if defined(__IAR_SYSTEMS_ICC__)
254 #pragma inline = forced
255 #elif defined(__GNUC__)
256 __attribute__((always_inline))
257 #endif
258 /**
259  * Perform a fast block XOR operation, such that
260  * r[i] = a[i] ^ b[i] where 0 <= i < n
261  *
262  * In some situations, this can perform better than mbedtls_xor() (e.g., it's about 5%
263  * better in AES-CBC).
264  *
265  * \param   r Pointer to result (buffer of at least \p n bytes). \p r
266  *            may be equal to either \p a or \p b, but behaviour when
267  *            it overlaps in other ways is undefined.
268  * \param   a Pointer to input (buffer of at least \p n bytes)
269  * \param   b Pointer to input (buffer of at least \p n bytes)
270  * \param   n Number of bytes to process.
271  *
272  * \note      Depending on the situation, it may be faster to use either mbedtls_xor() or
273  *            mbedtls_xor_no_simd() (these are functionally equivalent).
274  *            If the result is used immediately after the xor operation in non-SIMD code (e.g, in
275  *            AES-CBC), there may be additional latency to transfer the data from SIMD to scalar
276  *            registers, and in this case, mbedtls_xor_no_simd() may be faster. In other cases where
277  *            the result is not used immediately (e.g., in AES-CTR), mbedtls_xor() may be faster.
278  *            For targets without SIMD support, they will behave the same.
279  */
280 static inline void mbedtls_xor_no_simd(unsigned char *r,
281                                        const unsigned char *a,
282                                        const unsigned char *b,
283                                        size_t n)
284 {
285     size_t i = 0;
286 #if defined(MBEDTLS_EFFICIENT_UNALIGNED_ACCESS)
287 #if defined(MBEDTLS_ARCH_IS_X64) || defined(MBEDTLS_ARCH_IS_ARM64)
288     /* This codepath probably only makes sense on architectures with 64-bit registers */
289     for (; (i + 8) <= n; i += 8) {
290         uint64_t x = mbedtls_get_unaligned_uint64(a + i) ^ mbedtls_get_unaligned_uint64(b + i);
291         mbedtls_put_unaligned_uint64(r + i, x);
292     }
293 #if defined(__IAR_SYSTEMS_ICC__)
294     /* This if statement helps some compilers (e.g., IAR) optimise out the byte-by-byte tail case
295      * where n is a constant multiple of 8.
296      * For other compilers (e.g. recent gcc and clang) it makes no difference if n is a compile-time
297      * constant, and is a very small perf regression if n is not a compile-time constant. */
298     if (n % 8 == 0) {
299         return;
300     }
301 #endif
302 #else
303     for (; (i + 4) <= n; i += 4) {
304         uint32_t x = mbedtls_get_unaligned_uint32(a + i) ^ mbedtls_get_unaligned_uint32(b + i);
305         mbedtls_put_unaligned_uint32(r + i, x);
306     }
307 #if defined(__IAR_SYSTEMS_ICC__)
308     if (n % 4 == 0) {
309         return;
310     }
311 #endif
312 #endif
313 #endif
314     for (; i < n; i++) {
315         r[i] = a[i] ^ b[i];
316     }
317 }
318 
319 /* Fix MSVC C99 compatible issue
320  *      MSVC support __func__ from visual studio 2015( 1900 )
321  *      Use MSVC predefine macro to avoid name check fail.
322  */
323 #if (defined(_MSC_VER) && (_MSC_VER <= 1900))
324 #define /*no-check-names*/ __func__ __FUNCTION__
325 #endif
326 
327 /* Define `asm` for compilers which don't define it. */
328 /* *INDENT-OFF* */
329 #ifndef asm
330 #if defined(__IAR_SYSTEMS_ICC__)
331 #define asm __asm
332 #else
333 #define asm __asm__
334 #endif
335 #endif
336 /* *INDENT-ON* */
337 
338 /*
339  * Define the constraint used for read-only pointer operands to aarch64 asm.
340  *
341  * This is normally the usual "r", but for aarch64_32 (aka ILP32,
342  * as found in watchos), "p" is required to avoid warnings from clang.
343  *
344  * Note that clang does not recognise '+p' or '=p', and armclang
345  * does not recognise 'p' at all. Therefore, to update a pointer from
346  * aarch64 assembly, it is necessary to use something like:
347  *
348  * uintptr_t uptr = (uintptr_t) ptr;
349  * asm( "ldr x4, [%x0], #8" ... : "+r" (uptr) : : )
350  * ptr = (void*) uptr;
351  *
352  * Note that the "x" in "%x0" is neccessary; writing "%0" will cause warnings.
353  */
354 #if defined(__aarch64__) && defined(MBEDTLS_HAVE_ASM)
355 #if UINTPTR_MAX == 0xfffffffful
356 /* ILP32: Specify the pointer operand slightly differently, as per #7787. */
357 #define MBEDTLS_ASM_AARCH64_PTR_CONSTRAINT "p"
358 #elif UINTPTR_MAX == 0xfffffffffffffffful
359 /* Normal case (64-bit pointers): use "r" as the constraint for pointer operands to asm */
360 #define MBEDTLS_ASM_AARCH64_PTR_CONSTRAINT "r"
361 #else
362 #error "Unrecognised pointer size for aarch64"
363 #endif
364 #endif
365 
366 /* Always provide a static assert macro, so it can be used unconditionally.
367  * It does nothing on systems where we don't know how to define a static assert.
368  */
369 /* Can't use the C11-style `defined(static_assert)` on FreeBSD, since it
370  * defines static_assert even with -std=c99, but then complains about it.
371  */
372 #if defined(static_assert) && !defined(__FreeBSD__)
373 #define MBEDTLS_STATIC_ASSERT(expr, msg)    static_assert(expr, msg)
374 #else
375 /* Make sure `MBEDTLS_STATIC_ASSERT(expr, msg);` is valid both inside and
376  * outside a function. We choose a struct declaration, which can be repeated
377  * any number of times and does not need a matching definition. */
378 #define MBEDTLS_STATIC_ASSERT(expr, msg)                                \
379     struct ISO_C_does_not_allow_extra_semicolon_outside_of_a_function
380 #endif
381 
382 #if defined(__has_builtin)
383 #define MBEDTLS_HAS_BUILTIN(x) __has_builtin(x)
384 #else
385 #define MBEDTLS_HAS_BUILTIN(x) 0
386 #endif
387 
388 /* Define compiler branch hints */
389 #if MBEDTLS_HAS_BUILTIN(__builtin_expect)
390 #define MBEDTLS_LIKELY(x)       __builtin_expect(!!(x), 1)
391 #define MBEDTLS_UNLIKELY(x)     __builtin_expect(!!(x), 0)
392 #else
393 #define MBEDTLS_LIKELY(x)       x
394 #define MBEDTLS_UNLIKELY(x)     x
395 #endif
396 
397 /* MBEDTLS_ASSUME may be used to provide additional information to the compiler
398  * which can result in smaller code-size. */
399 #if MBEDTLS_HAS_BUILTIN(__builtin_assume)
400 /* clang provides __builtin_assume */
401 #define MBEDTLS_ASSUME(x)       __builtin_assume(x)
402 #elif MBEDTLS_HAS_BUILTIN(__builtin_unreachable)
403 /* gcc and IAR can use __builtin_unreachable */
404 #define MBEDTLS_ASSUME(x)       do { if (!(x)) __builtin_unreachable(); } while (0)
405 #elif defined(_MSC_VER)
406 /* Supported by MSVC since VS 2005 */
407 #define MBEDTLS_ASSUME(x)       __assume(x)
408 #else
409 #define MBEDTLS_ASSUME(x)       do { } while (0)
410 #endif
411 
412 /* For gcc -Os, override with -O2 for a given function.
413  *
414  * This will not affect behaviour for other optimisation settings, e.g. -O0.
415  */
416 #if defined(MBEDTLS_COMPILER_IS_GCC) && defined(__OPTIMIZE_SIZE__)
417 #define MBEDTLS_OPTIMIZE_FOR_PERFORMANCE __attribute__((optimize("-O2")))
418 #else
419 #define MBEDTLS_OPTIMIZE_FOR_PERFORMANCE
420 #endif
421 
422 /* Suppress compiler warnings for unused functions and variables. */
423 #if !defined(MBEDTLS_MAYBE_UNUSED) && defined(__has_attribute)
424 #    if __has_attribute(unused)
425 #        define MBEDTLS_MAYBE_UNUSED __attribute__((unused))
426 #    endif
427 #endif
428 #if !defined(MBEDTLS_MAYBE_UNUSED) && defined(__GNUC__)
429 #    define MBEDTLS_MAYBE_UNUSED __attribute__((unused))
430 #endif
431 #if !defined(MBEDTLS_MAYBE_UNUSED) && defined(__IAR_SYSTEMS_ICC__) && defined(__VER__)
432 /* IAR does support __attribute__((unused)), but only if the -e flag (extended language support)
433  * is given; the pragma always works.
434  * Unfortunately the pragma affects the rest of the file where it is used, but this is harmless.
435  * Check for version 5.2 or later - this pragma may be supported by earlier versions, but I wasn't
436  * able to find documentation).
437  */
438 #    if (__VER__ >= 5020000)
439 #        define MBEDTLS_MAYBE_UNUSED _Pragma("diag_suppress=Pe177")
440 #    endif
441 #endif
442 #if !defined(MBEDTLS_MAYBE_UNUSED) && defined(_MSC_VER)
443 #    define MBEDTLS_MAYBE_UNUSED __pragma(warning(suppress:4189))
444 #endif
445 #if !defined(MBEDTLS_MAYBE_UNUSED)
446 #    define MBEDTLS_MAYBE_UNUSED
447 #endif
448 
449 #endif /* MBEDTLS_LIBRARY_COMMON_H */
450