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  */
mbedtls_buffer_offset(unsigned char * p,size_t n)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  */
mbedtls_buffer_offset_const(const unsigned char * p,size_t n)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  */
mbedtls_xor(unsigned char * r,const unsigned char * a,const unsigned char * b,size_t n)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__)
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     if (n % 16 == 0) {
218         return;
219     }
220 #endif
221 #elif defined(MBEDTLS_ARCH_IS_X64) || defined(MBEDTLS_ARCH_IS_ARM64)
222     /* This codepath probably only makes sense on architectures with 64-bit registers */
223     for (; (i + 8) <= n; i += 8) {
224         uint64_t x = mbedtls_get_unaligned_uint64(a + i) ^ mbedtls_get_unaligned_uint64(b + i);
225         mbedtls_put_unaligned_uint64(r + i, x);
226     }
227 #if defined(__IAR_SYSTEMS_ICC__)
228     if (n % 8 == 0) {
229         return;
230     }
231 #endif
232 #else
233     for (; (i + 4) <= n; i += 4) {
234         uint32_t x = mbedtls_get_unaligned_uint32(a + i) ^ mbedtls_get_unaligned_uint32(b + i);
235         mbedtls_put_unaligned_uint32(r + i, x);
236     }
237 #if defined(__IAR_SYSTEMS_ICC__)
238     if (n % 4 == 0) {
239         return;
240     }
241 #endif
242 #endif
243 #endif
244     for (; i < n; i++) {
245         r[i] = a[i] ^ b[i];
246     }
247 }
248 
249 /* Always inline mbedtls_xor_no_simd() as we see significant perf regressions when it does not get
250  * inlined (e.g., observed about 3x perf difference in gcm_mult_largetable with gcc 7 - 12) */
251 #if defined(__IAR_SYSTEMS_ICC__)
252 #pragma inline = forced
253 #elif defined(__GNUC__)
254 __attribute__((always_inline))
255 #endif
256 /**
257  * Perform a fast block XOR operation, such that
258  * r[i] = a[i] ^ b[i] where 0 <= i < n
259  *
260  * In some situations, this can perform better than mbedtls_xor() (e.g., it's about 5%
261  * better in AES-CBC).
262  *
263  * \param   r Pointer to result (buffer of at least \p n bytes). \p r
264  *            may be equal to either \p a or \p b, but behaviour when
265  *            it overlaps in other ways is undefined.
266  * \param   a Pointer to input (buffer of at least \p n bytes)
267  * \param   b Pointer to input (buffer of at least \p n bytes)
268  * \param   n Number of bytes to process.
269  *
270  * \note      Depending on the situation, it may be faster to use either mbedtls_xor() or
271  *            mbedtls_xor_no_simd() (these are functionally equivalent).
272  *            If the result is used immediately after the xor operation in non-SIMD code (e.g, in
273  *            AES-CBC), there may be additional latency to transfer the data from SIMD to scalar
274  *            registers, and in this case, mbedtls_xor_no_simd() may be faster. In other cases where
275  *            the result is not used immediately (e.g., in AES-CTR), mbedtls_xor() may be faster.
276  *            For targets without SIMD support, they will behave the same.
277  */
mbedtls_xor_no_simd(unsigned char * r,const unsigned char * a,const unsigned char * b,size_t n)278 static inline void mbedtls_xor_no_simd(unsigned char *r,
279                                        const unsigned char *a,
280                                        const unsigned char *b,
281                                        size_t n)
282 {
283     size_t i = 0;
284 #if defined(MBEDTLS_EFFICIENT_UNALIGNED_ACCESS)
285 #if defined(MBEDTLS_ARCH_IS_X64) || defined(MBEDTLS_ARCH_IS_ARM64)
286     /* This codepath probably only makes sense on architectures with 64-bit registers */
287     for (; (i + 8) <= n; i += 8) {
288         uint64_t x = mbedtls_get_unaligned_uint64(a + i) ^ mbedtls_get_unaligned_uint64(b + i);
289         mbedtls_put_unaligned_uint64(r + i, x);
290     }
291 #if defined(__IAR_SYSTEMS_ICC__)
292     /* This if statement helps some compilers (e.g., IAR) optimise out the byte-by-byte tail case
293      * where n is a constant multiple of 8.
294      * For other compilers (e.g. recent gcc and clang) it makes no difference if n is a compile-time
295      * constant, and is a very small perf regression if n is not a compile-time constant. */
296     if (n % 8 == 0) {
297         return;
298     }
299 #endif
300 #else
301     for (; (i + 4) <= n; i += 4) {
302         uint32_t x = mbedtls_get_unaligned_uint32(a + i) ^ mbedtls_get_unaligned_uint32(b + i);
303         mbedtls_put_unaligned_uint32(r + i, x);
304     }
305 #if defined(__IAR_SYSTEMS_ICC__)
306     if (n % 4 == 0) {
307         return;
308     }
309 #endif
310 #endif
311 #endif
312     for (; i < n; i++) {
313         r[i] = a[i] ^ b[i];
314     }
315 }
316 
317 /* Fix MSVC C99 compatible issue
318  *      MSVC support __func__ from visual studio 2015( 1900 )
319  *      Use MSVC predefine macro to avoid name check fail.
320  */
321 #if (defined(_MSC_VER) && (_MSC_VER <= 1900))
322 #define /*no-check-names*/ __func__ __FUNCTION__
323 #endif
324 
325 /* Define `asm` for compilers which don't define it. */
326 /* *INDENT-OFF* */
327 #ifndef asm
328 #if defined(__IAR_SYSTEMS_ICC__)
329 #define asm __asm
330 #else
331 #define asm __asm__
332 #endif
333 #endif
334 /* *INDENT-ON* */
335 
336 /*
337  * Define the constraint used for read-only pointer operands to aarch64 asm.
338  *
339  * This is normally the usual "r", but for aarch64_32 (aka ILP32,
340  * as found in watchos), "p" is required to avoid warnings from clang.
341  *
342  * Note that clang does not recognise '+p' or '=p', and armclang
343  * does not recognise 'p' at all. Therefore, to update a pointer from
344  * aarch64 assembly, it is necessary to use something like:
345  *
346  * uintptr_t uptr = (uintptr_t) ptr;
347  * asm( "ldr x4, [%x0], #8" ... : "+r" (uptr) : : )
348  * ptr = (void*) uptr;
349  *
350  * Note that the "x" in "%x0" is neccessary; writing "%0" will cause warnings.
351  */
352 #if defined(__aarch64__) && defined(MBEDTLS_HAVE_ASM)
353 #if UINTPTR_MAX == 0xfffffffful
354 /* ILP32: Specify the pointer operand slightly differently, as per #7787. */
355 #define MBEDTLS_ASM_AARCH64_PTR_CONSTRAINT "p"
356 #elif UINTPTR_MAX == 0xfffffffffffffffful
357 /* Normal case (64-bit pointers): use "r" as the constraint for pointer operands to asm */
358 #define MBEDTLS_ASM_AARCH64_PTR_CONSTRAINT "r"
359 #else
360 #error "Unrecognised pointer size for aarch64"
361 #endif
362 #endif
363 
364 /* Always provide a static assert macro, so it can be used unconditionally.
365  * It does nothing on systems where we don't know how to define a static assert.
366  */
367 /* Can't use the C11-style `defined(static_assert)` on FreeBSD, since it
368  * defines static_assert even with -std=c99, but then complains about it.
369  */
370 #if defined(static_assert) && !defined(__FreeBSD__)
371 #define MBEDTLS_STATIC_ASSERT(expr, msg)    static_assert(expr, msg)
372 #else
373 /* Make sure `MBEDTLS_STATIC_ASSERT(expr, msg);` is valid both inside and
374  * outside a function. We choose a struct declaration, which can be repeated
375  * any number of times and does not need a matching definition. */
376 #define MBEDTLS_STATIC_ASSERT(expr, msg)                                \
377     struct ISO_C_does_not_allow_extra_semicolon_outside_of_a_function
378 #endif
379 
380 #if defined(__has_builtin)
381 #define MBEDTLS_HAS_BUILTIN(x) __has_builtin(x)
382 #else
383 #define MBEDTLS_HAS_BUILTIN(x) 0
384 #endif
385 
386 /* Define compiler branch hints */
387 #if MBEDTLS_HAS_BUILTIN(__builtin_expect)
388 #define MBEDTLS_LIKELY(x)       __builtin_expect(!!(x), 1)
389 #define MBEDTLS_UNLIKELY(x)     __builtin_expect(!!(x), 0)
390 #else
391 #define MBEDTLS_LIKELY(x)       x
392 #define MBEDTLS_UNLIKELY(x)     x
393 #endif
394 
395 /* MBEDTLS_ASSUME may be used to provide additional information to the compiler
396  * which can result in smaller code-size. */
397 #if MBEDTLS_HAS_BUILTIN(__builtin_assume)
398 /* clang provides __builtin_assume */
399 #define MBEDTLS_ASSUME(x)       __builtin_assume(x)
400 #elif MBEDTLS_HAS_BUILTIN(__builtin_unreachable)
401 /* gcc and IAR can use __builtin_unreachable */
402 #define MBEDTLS_ASSUME(x)       do { if (!(x)) __builtin_unreachable(); } while (0)
403 #elif defined(_MSC_VER)
404 /* Supported by MSVC since VS 2005 */
405 #define MBEDTLS_ASSUME(x)       __assume(x)
406 #else
407 #define MBEDTLS_ASSUME(x)       do { } while (0)
408 #endif
409 
410 /* For gcc -Os, override with -O2 for a given function.
411  *
412  * This will not affect behaviour for other optimisation settings, e.g. -O0.
413  */
414 #if defined(MBEDTLS_COMPILER_IS_GCC) && defined(__OPTIMIZE_SIZE__)
415 #define MBEDTLS_OPTIMIZE_FOR_PERFORMANCE __attribute__((optimize("-O2")))
416 #else
417 #define MBEDTLS_OPTIMIZE_FOR_PERFORMANCE
418 #endif
419 
420 /* Suppress compiler warnings for unused functions and variables. */
421 #if !defined(MBEDTLS_MAYBE_UNUSED) && defined(__has_attribute)
422 #    if __has_attribute(unused)
423 #        define MBEDTLS_MAYBE_UNUSED __attribute__((unused))
424 #    endif
425 #endif
426 #if !defined(MBEDTLS_MAYBE_UNUSED) && defined(__GNUC__)
427 #    define MBEDTLS_MAYBE_UNUSED __attribute__((unused))
428 #endif
429 #if !defined(MBEDTLS_MAYBE_UNUSED) && defined(__IAR_SYSTEMS_ICC__) && defined(__VER__)
430 /* IAR does support __attribute__((unused)), but only if the -e flag (extended language support)
431  * is given; the pragma always works.
432  * Unfortunately the pragma affects the rest of the file where it is used, but this is harmless.
433  * Check for version 5.2 or later - this pragma may be supported by earlier versions, but I wasn't
434  * able to find documentation).
435  */
436 #    if (__VER__ >= 5020000)
437 #        define MBEDTLS_MAYBE_UNUSED _Pragma("diag_suppress=Pe177")
438 #    endif
439 #endif
440 #if !defined(MBEDTLS_MAYBE_UNUSED) && defined(_MSC_VER)
441 #    define MBEDTLS_MAYBE_UNUSED __pragma(warning(suppress:4189))
442 #endif
443 #if !defined(MBEDTLS_MAYBE_UNUSED)
444 #    define MBEDTLS_MAYBE_UNUSED
445 #endif
446 
447 #endif /* MBEDTLS_LIBRARY_COMMON_H */
448