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