blob: 760dff49e730849098a520f95515655679ae8bb1 [file] [log] [blame]
Gilles Peskinec4672fd2019-09-11 13:39:11 +02001/**
2 * \file common.h
3 *
4 * \brief Utility macros for internal use in the library
5 */
6/*
Bence Szépkúti1e148272020-08-07 13:07:28 +02007 * Copyright The Mbed TLS Contributors
Dave Rodgman16799db2023-11-02 19:47:20 +00008 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Gilles Peskinec4672fd2019-09-11 13:39:11 +02009 */
10
11#ifndef MBEDTLS_LIBRARY_COMMON_H
12#define MBEDTLS_LIBRARY_COMMON_H
13
Bence Szépkútic662b362021-05-27 11:25:03 +020014#include "mbedtls/build_info.h"
Dave Rodgmanfbc23222022-11-24 18:07:37 +000015#include "alignment.h"
Gilles Peskinec4672fd2019-09-11 13:39:11 +020016
Tom Cosgrove6ef9bb32023-03-08 14:19:51 +000017#include <assert.h>
Gilles Peskine42649d92022-11-23 14:15:57 +010018#include <stddef.h>
Joe Subbiani2194dc42021-07-14 12:31:31 +010019#include <stdint.h>
Dave Rodgmanc3d80412022-11-22 15:01:39 +000020#include <stddef.h>
Joe Subbiani2194dc42021-07-14 12:31:31 +010021
Dave Rodgman3f47b3f2023-05-23 16:11:22 +010022#if defined(__ARM_NEON)
Dave Rodgman6f40f8b2023-05-22 18:21:20 +010023#include <arm_neon.h>
Dave Rodgman4ffd7c72023-09-05 11:43:02 +010024#define MBEDTLS_HAVE_NEON_INTRINSICS
Dave Rodgman0a487172023-09-15 11:52:06 +010025#elif defined(MBEDTLS_PLATFORM_IS_WINDOWS_ON_ARM64)
Dave Rodgman4ffd7c72023-09-05 11:43:02 +010026#include <arm64_neon.h>
27#define MBEDTLS_HAVE_NEON_INTRINSICS
28#endif
29
Gilles Peskinec4672fd2019-09-11 13:39:11 +020030/** Helper to define a function as static except when building invasive tests.
31 *
32 * If a function is only used inside its own source file and should be
33 * declared `static` to allow the compiler to optimize for code size,
34 * but that function has unit tests, define it with
35 * ```
36 * MBEDTLS_STATIC_TESTABLE int mbedtls_foo(...) { ... }
37 * ```
38 * and declare it in a header in the `library/` directory with
39 * ```
40 * #if defined(MBEDTLS_TEST_HOOKS)
41 * int mbedtls_foo(...);
42 * #endif
43 * ```
44 */
45#if defined(MBEDTLS_TEST_HOOKS)
46#define MBEDTLS_STATIC_TESTABLE
47#else
48#define MBEDTLS_STATIC_TESTABLE static
49#endif
50
TRodziewicz7871c2e2021-07-07 17:29:43 +020051#if defined(MBEDTLS_TEST_HOOKS)
Gilles Peskine449bd832023-01-11 14:50:10 +010052extern void (*mbedtls_test_hook_test_fail)(const char *test, int line, const char *file);
53#define MBEDTLS_TEST_HOOK_TEST_ASSERT(TEST) \
54 do { \
55 if ((!(TEST)) && ((*mbedtls_test_hook_test_fail) != NULL)) \
56 { \
57 (*mbedtls_test_hook_test_fail)( #TEST, __LINE__, __FILE__); \
58 } \
59 } while (0)
TRodziewicz7871c2e2021-07-07 17:29:43 +020060#else
Gilles Peskine449bd832023-01-11 14:50:10 +010061#define MBEDTLS_TEST_HOOK_TEST_ASSERT(TEST)
TRodziewicz7871c2e2021-07-07 17:29:43 +020062#endif /* defined(MBEDTLS_TEST_HOOKS) */
63
Andrzej Kurekb22b9772023-05-30 09:44:20 -040064/** \def ARRAY_LENGTH
65 * Return the number of elements of a static or stack array.
66 *
67 * \param array A value of array (not pointer) type.
68 *
69 * \return The number of elements of the array.
70 */
71/* A correct implementation of ARRAY_LENGTH, but which silently gives
72 * a nonsensical result if called with a pointer rather than an array. */
73#define ARRAY_LENGTH_UNSAFE(array) \
74 (sizeof(array) / sizeof(*(array)))
75
76#if defined(__GNUC__)
77/* Test if arg and &(arg)[0] have the same type. This is true if arg is
78 * an array but not if it's a pointer. */
79#define IS_ARRAY_NOT_POINTER(arg) \
80 (!__builtin_types_compatible_p(__typeof__(arg), \
81 __typeof__(&(arg)[0])))
82/* A compile-time constant with the value 0. If `const_expr` is not a
83 * compile-time constant with a nonzero value, cause a compile-time error. */
84#define STATIC_ASSERT_EXPR(const_expr) \
85 (0 && sizeof(struct { unsigned int STATIC_ASSERT : 1 - 2 * !(const_expr); }))
86
87/* Return the scalar value `value` (possibly promoted). This is a compile-time
88 * constant if `value` is. `condition` must be a compile-time constant.
89 * If `condition` is false, arrange to cause a compile-time error. */
90#define STATIC_ASSERT_THEN_RETURN(condition, value) \
91 (STATIC_ASSERT_EXPR(condition) ? 0 : (value))
92
93#define ARRAY_LENGTH(array) \
94 (STATIC_ASSERT_THEN_RETURN(IS_ARRAY_NOT_POINTER(array), \
95 ARRAY_LENGTH_UNSAFE(array)))
96
97#else
98/* If we aren't sure the compiler supports our non-standard tricks,
99 * fall back to the unsafe implementation. */
100#define ARRAY_LENGTH(array) ARRAY_LENGTH_UNSAFE(array)
101#endif
Mateusz Starzyk57d1d192021-05-27 14:39:53 +0200102/** Allow library to access its structs' private members.
Mateusz Starzyk2c09c9b2021-05-14 22:20:10 +0200103 *
104 * Although structs defined in header files are publicly available,
105 * their members are private and should not be accessed by the user.
106 */
107#define MBEDTLS_ALLOW_PRIVATE_ACCESS
108
Tom Cosgroveca8c61b2023-07-17 15:17:40 +0100109/**
110 * \brief Securely zeroize a buffer then free it.
111 *
Tom Cosgrove3a11bb82023-07-18 16:26:29 +0100112 * Similar to making consecutive calls to
113 * \c mbedtls_platform_zeroize() and \c mbedtls_free(), but has
Tom Cosgroveca8c61b2023-07-17 15:17:40 +0100114 * code size savings, and potential for optimisation in the future.
115 *
Tom Cosgrove3a11bb82023-07-18 16:26:29 +0100116 * Guaranteed to be a no-op if \p buf is \c NULL and \p len is 0.
117 *
118 * \param buf Buffer to be zeroized then freed.
Tom Cosgroveca8c61b2023-07-17 15:17:40 +0100119 * \param len Length of the buffer in bytes
120 */
121void mbedtls_zeroize_and_free(void *buf, size_t len);
122
Gilles Peskine42649d92022-11-23 14:15:57 +0100123/** Return an offset into a buffer.
124 *
125 * This is just the addition of an offset to a pointer, except that this
126 * function also accepts an offset of 0 into a buffer whose pointer is null.
Gilles Peskine7d237782022-11-25 13:34:59 +0100127 * (`p + n` has undefined behavior when `p` is null, even when `n == 0`.
128 * A null pointer is a valid buffer pointer when the size is 0, for example
129 * as the result of `malloc(0)` on some platforms.)
Gilles Peskine42649d92022-11-23 14:15:57 +0100130 *
131 * \param p Pointer to a buffer of at least n bytes.
132 * This may be \p NULL if \p n is zero.
133 * \param n An offset in bytes.
134 * \return Pointer to offset \p n in the buffer \p p.
135 * Note that this is only a valid pointer if the size of the
136 * buffer is at least \p n + 1.
137 */
138static inline unsigned char *mbedtls_buffer_offset(
Gilles Peskine449bd832023-01-11 14:50:10 +0100139 unsigned char *p, size_t n)
Gilles Peskine42649d92022-11-23 14:15:57 +0100140{
Gilles Peskine449bd832023-01-11 14:50:10 +0100141 return p == NULL ? NULL : p + n;
Gilles Peskine42649d92022-11-23 14:15:57 +0100142}
143
144/** Return an offset into a read-only buffer.
145 *
Gilles Peskine7d237782022-11-25 13:34:59 +0100146 * Similar to mbedtls_buffer_offset(), but for const pointers.
Gilles Peskine42649d92022-11-23 14:15:57 +0100147 *
148 * \param p Pointer to a buffer of at least n bytes.
149 * This may be \p NULL if \p n is zero.
150 * \param n An offset in bytes.
151 * \return Pointer to offset \p n in the buffer \p p.
152 * Note that this is only a valid pointer if the size of the
153 * buffer is at least \p n + 1.
154 */
155static inline const unsigned char *mbedtls_buffer_offset_const(
Gilles Peskine449bd832023-01-11 14:50:10 +0100156 const unsigned char *p, size_t n)
Gilles Peskine42649d92022-11-23 14:15:57 +0100157{
Gilles Peskine449bd832023-01-11 14:50:10 +0100158 return p == NULL ? NULL : p + n;
Gilles Peskine42649d92022-11-23 14:15:57 +0100159}
160
Dave Rodgman18d90d72024-01-19 14:08:04 +0000161/* Always inline mbedtls_xor for similar reasons as mbedtls_xor_no_simd. */
162#if defined(__IAR_SYSTEMS_ICC__)
163#pragma inline = forced
164#elif defined(__GNUC__)
165__attribute__((always_inline))
166#endif
Dave Rodgmanc3d80412022-11-22 15:01:39 +0000167/**
168 * Perform a fast block XOR operation, such that
169 * r[i] = a[i] ^ b[i] where 0 <= i < n
170 *
171 * \param r Pointer to result (buffer of at least \p n bytes). \p r
172 * may be equal to either \p a or \p b, but behaviour when
173 * it overlaps in other ways is undefined.
174 * \param a Pointer to input (buffer of at least \p n bytes)
175 * \param b Pointer to input (buffer of at least \p n bytes)
176 * \param n Number of bytes to process.
177 */
Dave Rodgman18d90d72024-01-19 14:08:04 +0000178static inline void mbedtls_xor(unsigned char *r,
179 const unsigned char *a,
180 const unsigned char *b,
181 size_t n)
Dave Rodgmanc3d80412022-11-22 15:01:39 +0000182{
Dave Rodgmanb9cd19b2022-12-30 21:32:03 +0000183 size_t i = 0;
184#if defined(MBEDTLS_EFFICIENT_UNALIGNED_ACCESS)
Dave Rodgman59059ec2023-11-30 09:31:26 +0000185#if defined(MBEDTLS_HAVE_NEON_INTRINSICS) && \
Dave Rodgmand879b472023-11-30 09:35:14 +0000186 (!(defined(MBEDTLS_COMPILER_IS_GCC) && MBEDTLS_GCC_VERSION < 70300))
187 /* Old GCC versions generate a warning here, so disable the NEON path for these compilers */
Dave Rodgman6f40f8b2023-05-22 18:21:20 +0100188 for (; (i + 16) <= n; i += 16) {
Dave Rodgmanf32176c2023-06-09 16:25:49 +0100189 uint8x16_t v1 = vld1q_u8(a + i);
190 uint8x16_t v2 = vld1q_u8(b + i);
Dave Rodgman2070c202023-06-07 16:25:58 +0100191 uint8x16_t x = veorq_u8(v1, v2);
Dave Rodgmanf32176c2023-06-09 16:25:49 +0100192 vst1q_u8(r + i, x);
Dave Rodgman6f40f8b2023-05-22 18:21:20 +0100193 }
Dave Rodgman69b5a862024-01-19 14:02:08 +0000194 // This if statement helps some compilers (e.g., IAR) optimise out the byte-by-byte tail case
195 // where n is a constant multiple of 16.
196 // It makes no difference for others (e.g. recent gcc and clang) if n is a compile-time
197 // constant, and very little difference if n is not a compile-time constant.
198 if (n % 16 != 0)
Dave Rodgmanc5cc7272023-09-15 11:41:17 +0100199#elif defined(MBEDTLS_ARCH_IS_X64) || defined(MBEDTLS_ARCH_IS_ARM64)
Dave Rodgman0805ad12023-05-19 11:48:10 +0100200 /* This codepath probably only makes sense on architectures with 64-bit registers */
201 for (; (i + 8) <= n; i += 8) {
202 uint64_t x = mbedtls_get_unaligned_uint64(a + i) ^ mbedtls_get_unaligned_uint64(b + i);
203 mbedtls_put_unaligned_uint64(r + i, x);
204 }
Dave Rodgman69b5a862024-01-19 14:02:08 +0000205 if (n % 8 != 0)
Dave Rodgman5c394ff2023-06-09 20:10:36 +0100206#else
Dave Rodgmanb9cd19b2022-12-30 21:32:03 +0000207 for (; (i + 4) <= n; i += 4) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100208 uint32_t x = mbedtls_get_unaligned_uint32(a + i) ^ mbedtls_get_unaligned_uint32(b + i);
209 mbedtls_put_unaligned_uint32(r + i, x);
Dave Rodgmanc3d80412022-11-22 15:01:39 +0000210 }
Dave Rodgman69b5a862024-01-19 14:02:08 +0000211 if (n % 4 != 0)
Dave Rodgmanb9cd19b2022-12-30 21:32:03 +0000212#endif
Dave Rodgman5c394ff2023-06-09 20:10:36 +0100213#endif
Dave Rodgman69b5a862024-01-19 14:02:08 +0000214 {
215 for (; i < n; i++) {
216 r[i] = a[i] ^ b[i];
217 }
Dave Rodgmanc3d80412022-11-22 15:01:39 +0000218 }
219}
220
Dave Rodgman18d90d72024-01-19 14:08:04 +0000221/* Always inline mbedtls_xor_no_simd as we see significant perf regressions when it does not get
222 * inlined (e.g., observed about 3x perf difference in gcm_mult_largetable with gcc 7 - 12) */
223#if defined(__IAR_SYSTEMS_ICC__)
224#pragma inline = forced
225#elif defined(__GNUC__)
226__attribute__((always_inline))
227#endif
Dave Rodgman03bb5262023-06-15 18:43:24 +0100228/**
229 * Perform a fast block XOR operation, such that
230 * r[i] = a[i] ^ b[i] where 0 <= i < n
231 *
232 * In some situations, this can perform better than mbedtls_xor (e.g., it's about 5%
233 * better in AES-CBC).
234 *
235 * \param r Pointer to result (buffer of at least \p n bytes). \p r
236 * may be equal to either \p a or \p b, but behaviour when
237 * it overlaps in other ways is undefined.
238 * \param a Pointer to input (buffer of at least \p n bytes)
239 * \param b Pointer to input (buffer of at least \p n bytes)
240 * \param n Number of bytes to process.
241 */
Dave Rodgman2dd15b32023-06-15 20:27:53 +0100242static inline void mbedtls_xor_no_simd(unsigned char *r,
243 const unsigned char *a,
244 const unsigned char *b,
245 size_t n)
Dave Rodgman03bb5262023-06-15 18:43:24 +0100246{
247 size_t i = 0;
248#if defined(MBEDTLS_EFFICIENT_UNALIGNED_ACCESS)
Dave Rodgmanc5cc7272023-09-15 11:41:17 +0100249#if defined(MBEDTLS_ARCH_IS_X64) || defined(MBEDTLS_ARCH_IS_ARM64)
Dave Rodgman03bb5262023-06-15 18:43:24 +0100250 /* This codepath probably only makes sense on architectures with 64-bit registers */
251 for (; (i + 8) <= n; i += 8) {
252 uint64_t x = mbedtls_get_unaligned_uint64(a + i) ^ mbedtls_get_unaligned_uint64(b + i);
253 mbedtls_put_unaligned_uint64(r + i, x);
254 }
Dave Rodgman69b5a862024-01-19 14:02:08 +0000255 // This if statement helps some compilers (e.g., IAR) optimise out the byte-by-byte tail case
256 // where n is a constant multiple of 8.
257 // It makes no difference for others (e.g. recent gcc and clang) if n is a compile-time
258 // constant, and very little difference if n is not a compile-time constant.
259 if (n % 8 != 0)
Dave Rodgman03bb5262023-06-15 18:43:24 +0100260#else
261 for (; (i + 4) <= n; i += 4) {
262 uint32_t x = mbedtls_get_unaligned_uint32(a + i) ^ mbedtls_get_unaligned_uint32(b + i);
263 mbedtls_put_unaligned_uint32(r + i, x);
264 }
Dave Rodgman69b5a862024-01-19 14:02:08 +0000265 if (n % 4 != 0)
Dave Rodgman03bb5262023-06-15 18:43:24 +0100266#endif
267#endif
Dave Rodgman69b5a862024-01-19 14:02:08 +0000268 {
269 for (; i < n; i++) {
270 r[i] = a[i] ^ b[i];
271 }
Dave Rodgman03bb5262023-06-15 18:43:24 +0100272 }
273}
274
Jerry Yu6c983522021-09-24 12:45:36 +0800275/* Fix MSVC C99 compatible issue
276 * MSVC support __func__ from visual studio 2015( 1900 )
277 * Use MSVC predefine macro to avoid name check fail.
278 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100279#if (defined(_MSC_VER) && (_MSC_VER <= 1900))
Jerry Yud52398d2021-09-28 16:13:44 +0800280#define /*no-check-names*/ __func__ __FUNCTION__
Jerry Yu6c983522021-09-24 12:45:36 +0800281#endif
282
Dave Rodgmanfa960262023-01-10 11:14:02 +0000283/* Define `asm` for compilers which don't define it. */
284/* *INDENT-OFF* */
285#ifndef asm
Agathiyan Bragadeesh789e50e2023-07-14 16:59:36 +0100286#if defined(__IAR_SYSTEMS_ICC__)
287#define asm __asm
288#else
Dave Rodgmanfa960262023-01-10 11:14:02 +0000289#define asm __asm__
290#endif
Agathiyan Bragadeesh789e50e2023-07-14 16:59:36 +0100291#endif
Dave Rodgmanfa960262023-01-10 11:14:02 +0000292/* *INDENT-ON* */
293
Dave Rodgman0400ae22023-06-21 16:14:46 +0100294/*
Dave Rodgman28e2ca52023-06-27 15:25:38 +0100295 * Define the constraint used for read-only pointer operands to aarch64 asm.
Dave Rodgman0400ae22023-06-21 16:14:46 +0100296 *
297 * This is normally the usual "r", but for aarch64_32 (aka ILP32,
298 * as found in watchos), "p" is required to avoid warnings from clang.
Dave Rodgmane6c99962023-06-21 21:16:23 +0100299 *
300 * Note that clang does not recognise '+p' or '=p', and armclang
Dave Rodgman28e2ca52023-06-27 15:25:38 +0100301 * does not recognise 'p' at all. Therefore, to update a pointer from
302 * aarch64 assembly, it is necessary to use something like:
303 *
304 * uintptr_t uptr = (uintptr_t) ptr;
305 * asm( "ldr x4, [%x0], #8" ... : "+r" (uptr) : : )
306 * ptr = (void*) uptr;
307 *
308 * Note that the "x" in "%x0" is neccessary; writing "%0" will cause warnings.
Dave Rodgman0400ae22023-06-21 16:14:46 +0100309 */
310#if defined(__aarch64__) && defined(MBEDTLS_HAVE_ASM)
311#if UINTPTR_MAX == 0xfffffffful
312/* ILP32: Specify the pointer operand slightly differently, as per #7787. */
313#define MBEDTLS_ASM_AARCH64_PTR_CONSTRAINT "p"
314#elif UINTPTR_MAX == 0xfffffffffffffffful
315/* Normal case (64-bit pointers): use "r" as the constraint for pointer operands to asm */
316#define MBEDTLS_ASM_AARCH64_PTR_CONSTRAINT "r"
317#else
Antonio de Angelis1ee4d122023-08-16 12:26:37 +0100318#error "Unrecognised pointer size for aarch64"
Dave Rodgman0400ae22023-06-21 16:14:46 +0100319#endif
320#endif
321
Tom Cosgrove6ef9bb32023-03-08 14:19:51 +0000322/* Always provide a static assert macro, so it can be used unconditionally.
Tom Cosgrove57f04b82023-03-14 12:03:47 +0000323 * It will expand to nothing on some systems.
Tom Cosgrove6ef9bb32023-03-08 14:19:51 +0000324 * Can be used outside functions (but don't add a trailing ';' in that case:
325 * the semicolon is included here to avoid triggering -Wextra-semi when
326 * MBEDTLS_STATIC_ASSERT() expands to nothing).
327 * Can't use the C11-style `defined(static_assert)` on FreeBSD, since it
328 * defines static_assert even with -std=c99, but then complains about it.
329 */
330#if defined(static_assert) && !defined(__FreeBSD__)
331#define MBEDTLS_STATIC_ASSERT(expr, msg) static_assert(expr, msg);
Tom Cosgrove6ef9bb32023-03-08 14:19:51 +0000332#else
333#define MBEDTLS_STATIC_ASSERT(expr, msg)
334#endif
335
Dave Rodgman360e04f2023-06-09 17:18:32 +0100336#if defined(__has_builtin)
Dave Rodgman9ba640d2023-10-31 23:30:09 +0000337#define MBEDTLS_HAS_BUILTIN(x) __has_builtin(x)
338#else
339#define MBEDTLS_HAS_BUILTIN(x) 0
340#endif
341
342/* Define compiler branch hints */
343#if MBEDTLS_HAS_BUILTIN(__builtin_expect)
Dave Rodgmane9fcffd2023-07-19 15:42:19 +0100344#define MBEDTLS_LIKELY(x) __builtin_expect(!!(x), 1)
345#define MBEDTLS_UNLIKELY(x) __builtin_expect(!!(x), 0)
Dave Rodgman9ba640d2023-10-31 23:30:09 +0000346#else
Dave Rodgman360e04f2023-06-09 17:18:32 +0100347#define MBEDTLS_LIKELY(x) x
348#define MBEDTLS_UNLIKELY(x) x
349#endif
350
Dave Rodgmanfb24a842023-07-12 13:16:49 +0100351/* MBEDTLS_ASSUME may be used to provide additional information to the compiler
352 * which can result in smaller code-size. */
Dave Rodgman9ba640d2023-10-31 23:30:09 +0000353#if MBEDTLS_HAS_BUILTIN(__builtin_assume)
Dave Rodgman52e70522023-10-31 23:26:44 +0000354/* clang provides __builtin_assume */
Dave Rodgmanfb24a842023-07-12 13:16:49 +0100355#define MBEDTLS_ASSUME(x) __builtin_assume(x)
Dave Rodgman9ba640d2023-10-31 23:30:09 +0000356#elif MBEDTLS_HAS_BUILTIN(__builtin_unreachable)
Dave Rodgmane91d7c52023-11-02 10:36:38 +0000357/* gcc and IAR can use __builtin_unreachable */
Dave Rodgmanfb24a842023-07-12 13:16:49 +0100358#define MBEDTLS_ASSUME(x) do { if (!(x)) __builtin_unreachable(); } while (0)
Dave Rodgman9ba640d2023-10-31 23:30:09 +0000359#elif defined(_MSC_VER)
Dave Rodgman90c8ac22023-10-31 23:27:24 +0000360/* Supported by MSVC since VS 2005 */
361#define MBEDTLS_ASSUME(x) __assume(x)
362#else
Dave Rodgman64bdeb82023-10-31 23:27:04 +0000363#define MBEDTLS_ASSUME(x) do { } while (0)
Dave Rodgman7fdfd702023-06-15 18:42:25 +0100364#endif
365
Dave Rodgman9bb7e6f2023-06-16 09:41:21 +0100366/* For gcc -Os, override with -O2 for a given function.
367 *
368 * This will not affect behaviour for other optimisation settings, e.g. -O0.
369 */
Dave Rodgmanb055f752023-06-15 18:42:59 +0100370#if defined(MBEDTLS_COMPILER_IS_GCC) && defined(__OPTIMIZE_SIZE__)
Dave Rodgman9bb7e6f2023-06-16 09:41:21 +0100371#define MBEDTLS_OPTIMIZE_FOR_PERFORMANCE __attribute__((optimize("-O2")))
Dave Rodgmanb055f752023-06-15 18:42:59 +0100372#else
Dave Rodgman9bb7e6f2023-06-16 09:41:21 +0100373#define MBEDTLS_OPTIMIZE_FOR_PERFORMANCE
Dave Rodgmanb055f752023-06-15 18:42:59 +0100374#endif
375
Dave Rodgman1ec1a0f2023-10-04 13:50:54 +0100376/* Suppress compiler warnings for unused functions and variables. */
Dave Rodgman2457bcd2023-10-13 12:31:45 +0100377#if !defined(MBEDTLS_MAYBE_UNUSED) && defined(__has_attribute)
378# if __has_attribute(unused)
379# define MBEDTLS_MAYBE_UNUSED __attribute__((unused))
380# endif
381#endif
382#if !defined(MBEDTLS_MAYBE_UNUSED) && defined(__GNUC__)
383# define MBEDTLS_MAYBE_UNUSED __attribute__((unused))
Dave Rodgmanfeadcaf2023-10-04 15:27:33 +0100384#endif
Dave Rodgman749f2222023-10-04 15:38:58 +0100385#if !defined(MBEDTLS_MAYBE_UNUSED) && defined(__IAR_SYSTEMS_ICC__) && defined(__VER__)
Dave Rodgmanf8428682023-10-24 14:18:38 +0100386/* IAR does support __attribute__((unused)), but only if the -e flag (extended language support)
387 * is given; the pragma always works.
Dave Rodgmand1c4fb02023-10-25 15:07:35 +0100388 * Unfortunately the pragma affects the rest of the file where it is used, but this is harmless.
389 * Check for version 5.2 or later - this pragma may be supported by earlier versions, but I wasn't
390 * able to find documentation).
391 */
392# if (__VER__ >= 5020000)
Dave Rodgmanf8428682023-10-24 14:18:38 +0100393# define MBEDTLS_MAYBE_UNUSED _Pragma("diag_suppress=Pe177")
Dave Rodgman2457bcd2023-10-13 12:31:45 +0100394# endif
Dave Rodgman749f2222023-10-04 15:38:58 +0100395#endif
396#if !defined(MBEDTLS_MAYBE_UNUSED) && defined(_MSC_VER)
Dave Rodgman2457bcd2023-10-13 12:31:45 +0100397# define MBEDTLS_MAYBE_UNUSED __pragma(warning(suppress:4189))
Dave Rodgman749f2222023-10-04 15:38:58 +0100398#endif
399#if !defined(MBEDTLS_MAYBE_UNUSED)
Dave Rodgman2457bcd2023-10-13 12:31:45 +0100400# define MBEDTLS_MAYBE_UNUSED
Dave Rodgman1ec1a0f2023-10-04 13:50:54 +0100401#endif
402
Gilles Peskinec4672fd2019-09-11 13:39:11 +0200403#endif /* MBEDTLS_LIBRARY_COMMON_H */