blob: 5565b307cdd19317f022ed34a94ded441e271108 [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 Rodgman7ff79652023-11-03 12:04:52 +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
14#if defined(MBEDTLS_CONFIG_FILE)
15#include MBEDTLS_CONFIG_FILE
16#else
17#include "mbedtls/config.h"
18#endif
19
Tom Cosgrovebdd01a72023-03-08 14:19:51 +000020#include <assert.h>
Gilles Peskine01bf6312022-11-23 14:15:57 +010021#include <stddef.h>
Joe Subbianic045dc12021-07-14 12:31:31 +010022#include <stdint.h>
23
Gilles Peskine8fe23a02022-11-23 17:24:37 +010024/* Define `inline` on some non-C99-compliant compilers. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010025#if (defined(__ARMCC_VERSION) || defined(_MSC_VER)) && \
Gilles Peskine8fe23a02022-11-23 17:24:37 +010026 !defined(inline) && !defined(__cplusplus)
27#define inline __inline
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
Gilles Peskine01bf6312022-11-23 14:15:57 +010051/** Return an offset into a buffer.
52 *
53 * This is just the addition of an offset to a pointer, except that this
54 * function also accepts an offset of 0 into a buffer whose pointer is null.
Gilles Peskineff97f332022-11-25 13:34:59 +010055 * (`p + n` has undefined behavior when `p` is null, even when `n == 0`.
56 * A null pointer is a valid buffer pointer when the size is 0, for example
57 * as the result of `malloc(0)` on some platforms.)
Gilles Peskine01bf6312022-11-23 14:15:57 +010058 *
59 * \param p Pointer to a buffer of at least n bytes.
60 * This may be \p NULL if \p n is zero.
61 * \param n An offset in bytes.
62 * \return Pointer to offset \p n in the buffer \p p.
63 * Note that this is only a valid pointer if the size of the
64 * buffer is at least \p n + 1.
65 */
66static inline unsigned char *mbedtls_buffer_offset(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010067 unsigned char *p, size_t n)
Gilles Peskine01bf6312022-11-23 14:15:57 +010068{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010069 return p == NULL ? NULL : p + n;
Gilles Peskine01bf6312022-11-23 14:15:57 +010070}
71
72/** Return an offset into a read-only buffer.
73 *
Gilles Peskineff97f332022-11-25 13:34:59 +010074 * Similar to mbedtls_buffer_offset(), but for const pointers.
Gilles Peskine01bf6312022-11-23 14:15:57 +010075 *
76 * \param p Pointer to a buffer of at least n bytes.
77 * This may be \p NULL if \p n is zero.
78 * \param n An offset in bytes.
79 * \return Pointer to offset \p n in the buffer \p p.
80 * Note that this is only a valid pointer if the size of the
81 * buffer is at least \p n + 1.
82 */
83static inline const unsigned char *mbedtls_buffer_offset_const(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010084 const unsigned char *p, size_t n)
Gilles Peskine01bf6312022-11-23 14:15:57 +010085{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010086 return p == NULL ? NULL : p + n;
Gilles Peskine01bf6312022-11-23 14:15:57 +010087}
88
Joe Subbianiba486b02021-06-22 15:51:53 +010089/** Byte Reading Macros
Joe Subbiani61f7d732021-06-24 09:06:23 +010090 *
Joe Subbiani8799e542021-07-21 16:35:48 +010091 * Given a multi-byte integer \p x, MBEDTLS_BYTE_n retrieves the n-th
Joe Subbianid3a3f212021-07-21 15:22:47 +010092 * byte from x, where byte 0 is the least significant byte.
Joe Subbianiba486b02021-06-22 15:51:53 +010093 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010094#define MBEDTLS_BYTE_0(x) ((uint8_t) ((x) & 0xff))
95#define MBEDTLS_BYTE_1(x) ((uint8_t) (((x) >> 8) & 0xff))
96#define MBEDTLS_BYTE_2(x) ((uint8_t) (((x) >> 16) & 0xff))
97#define MBEDTLS_BYTE_3(x) ((uint8_t) (((x) >> 24) & 0xff))
98#define MBEDTLS_BYTE_4(x) ((uint8_t) (((x) >> 32) & 0xff))
99#define MBEDTLS_BYTE_5(x) ((uint8_t) (((x) >> 40) & 0xff))
100#define MBEDTLS_BYTE_6(x) ((uint8_t) (((x) >> 48) & 0xff))
101#define MBEDTLS_BYTE_7(x) ((uint8_t) (((x) >> 56) & 0xff))
Joe Subbiani6b897c92021-07-08 14:59:52 +0100102
Joe Subbiani266476d2021-07-07 15:16:56 +0100103/**
Joe Subbiani6350d3a2021-07-13 12:13:19 +0100104 * Get the unsigned 32 bits integer corresponding to four bytes in
Joe Subbiani0a65d532021-07-14 11:53:07 +0100105 * big-endian order (MSB first).
Joe Subbiani266476d2021-07-07 15:16:56 +0100106 *
Joe Subbiani0a65d532021-07-14 11:53:07 +0100107 * \param data Base address of the memory to get the four bytes from.
Joe Subbiani6350d3a2021-07-13 12:13:19 +0100108 * \param offset Offset from \p base of the first and most significant
109 * byte of the four bytes to build the 32 bits unsigned
Joe Subbiani0a65d532021-07-14 11:53:07 +0100110 * integer from.
Joe Subbiani9231d5f2021-07-07 16:56:29 +0100111 */
112#ifndef MBEDTLS_GET_UINT32_BE
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100113#define MBEDTLS_GET_UINT32_BE(data, offset) \
Joe Subbiani896f4ee2021-07-19 15:29:18 +0100114 ( \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100115 ((uint32_t) (data)[(offset)] << 24) \
116 | ((uint32_t) (data)[(offset) + 1] << 16) \
117 | ((uint32_t) (data)[(offset) + 2] << 8) \
118 | ((uint32_t) (data)[(offset) + 3]) \
Joe Subbiani9231d5f2021-07-07 16:56:29 +0100119 )
120#endif
121
122/**
Joe Subbiani0a65d532021-07-14 11:53:07 +0100123 * Put in memory a 32 bits unsigned integer in big-endian order.
Joe Subbiani9231d5f2021-07-07 16:56:29 +0100124 *
Joe Subbiani6350d3a2021-07-13 12:13:19 +0100125 * \param n 32 bits unsigned integer to put in memory.
126 * \param data Base address of the memory where to put the 32
Joe Subbiani0a65d532021-07-14 11:53:07 +0100127 * bits unsigned integer in.
Joe Subbiani6350d3a2021-07-13 12:13:19 +0100128 * \param offset Offset from \p base where to put the most significant
Joe Subbiani0a65d532021-07-14 11:53:07 +0100129 * byte of the 32 bits unsigned integer \p n.
Joe Subbiani266476d2021-07-07 15:16:56 +0100130 */
Joe Subbiani2bbafda2021-06-24 13:00:03 +0100131#ifndef MBEDTLS_PUT_UINT32_BE
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100132#define MBEDTLS_PUT_UINT32_BE(n, data, offset) \
133 { \
134 (data)[(offset)] = MBEDTLS_BYTE_3(n); \
135 (data)[(offset) + 1] = MBEDTLS_BYTE_2(n); \
136 (data)[(offset) + 2] = MBEDTLS_BYTE_1(n); \
137 (data)[(offset) + 3] = MBEDTLS_BYTE_0(n); \
138 }
Joe Subbianiaa5f6a62021-06-23 11:49:03 +0100139#endif
140
Joe Subbiani266476d2021-07-07 15:16:56 +0100141/**
Joe Subbiani6350d3a2021-07-13 12:13:19 +0100142 * Get the unsigned 32 bits integer corresponding to four bytes in
Joe Subbiani0a65d532021-07-14 11:53:07 +0100143 * little-endian order (LSB first).
Joe Subbiani9231d5f2021-07-07 16:56:29 +0100144 *
Joe Subbiani0a65d532021-07-14 11:53:07 +0100145 * \param data Base address of the memory to get the four bytes from.
Joe Subbiani6350d3a2021-07-13 12:13:19 +0100146 * \param offset Offset from \p base of the first and least significant
147 * byte of the four bytes to build the 32 bits unsigned
Joe Subbiani0a65d532021-07-14 11:53:07 +0100148 * integer from.
Joe Subbiani4fb75552021-06-23 12:16:47 +0100149 */
Joe Subbiani2bbafda2021-06-24 13:00:03 +0100150#ifndef MBEDTLS_GET_UINT32_LE
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100151#define MBEDTLS_GET_UINT32_LE(data, offset) \
Joe Subbiani896f4ee2021-07-19 15:29:18 +0100152 ( \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100153 ((uint32_t) (data)[(offset)]) \
154 | ((uint32_t) (data)[(offset) + 1] << 8) \
155 | ((uint32_t) (data)[(offset) + 2] << 16) \
156 | ((uint32_t) (data)[(offset) + 3] << 24) \
Joe Subbiani9231d5f2021-07-07 16:56:29 +0100157 )
Joe Subbiani4fb75552021-06-23 12:16:47 +0100158#endif
159
Joe Subbiani9231d5f2021-07-07 16:56:29 +0100160/**
Joe Subbiani0a65d532021-07-14 11:53:07 +0100161 * Put in memory a 32 bits unsigned integer in little-endian order.
Joe Subbiani9231d5f2021-07-07 16:56:29 +0100162 *
Joe Subbiani6350d3a2021-07-13 12:13:19 +0100163 * \param n 32 bits unsigned integer to put in memory.
164 * \param data Base address of the memory where to put the 32
Joe Subbiani0a65d532021-07-14 11:53:07 +0100165 * bits unsigned integer in.
Joe Subbiani6350d3a2021-07-13 12:13:19 +0100166 * \param offset Offset from \p base where to put the least significant
Joe Subbiani0a65d532021-07-14 11:53:07 +0100167 * byte of the 32 bits unsigned integer \p n.
Joe Subbiani9231d5f2021-07-07 16:56:29 +0100168 */
Joe Subbiani2bbafda2021-06-24 13:00:03 +0100169#ifndef MBEDTLS_PUT_UINT32_LE
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100170#define MBEDTLS_PUT_UINT32_LE(n, data, offset) \
171 { \
172 (data)[(offset)] = MBEDTLS_BYTE_0(n); \
173 (data)[(offset) + 1] = MBEDTLS_BYTE_1(n); \
174 (data)[(offset) + 2] = MBEDTLS_BYTE_2(n); \
175 (data)[(offset) + 3] = MBEDTLS_BYTE_3(n); \
176 }
Joe Subbiani4fb75552021-06-23 12:16:47 +0100177#endif
178
Joe Subbiani61f7d732021-06-24 09:06:23 +0100179/**
Joe Subbiani5b96e672021-07-14 12:05:51 +0100180 * Get the unsigned 16 bits integer corresponding to two bytes in
Joe Subbiani0a65d532021-07-14 11:53:07 +0100181 * little-endian order (LSB first).
Joe Subbiani9231d5f2021-07-07 16:56:29 +0100182 *
Joe Subbiani5b96e672021-07-14 12:05:51 +0100183 * \param data Base address of the memory to get the two bytes from.
Joe Subbiani6350d3a2021-07-13 12:13:19 +0100184 * \param offset Offset from \p base of the first and least significant
Joe Subbiani5b96e672021-07-14 12:05:51 +0100185 * byte of the two bytes to build the 16 bits unsigned
Joe Subbiani0a65d532021-07-14 11:53:07 +0100186 * integer from.
Joe Subbiani927488e2021-06-23 11:23:44 +0100187 */
Joe Subbiani9231d5f2021-07-07 16:56:29 +0100188#ifndef MBEDTLS_GET_UINT16_LE
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100189#define MBEDTLS_GET_UINT16_LE(data, offset) \
Joe Subbiani896f4ee2021-07-19 15:29:18 +0100190 ( \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100191 ((uint16_t) (data)[(offset)]) \
192 | ((uint16_t) (data)[(offset) + 1] << 8) \
Joe Subbiani927488e2021-06-23 11:23:44 +0100193 )
Joe Subbiani9231d5f2021-07-07 16:56:29 +0100194#endif
Joe Subbiani927488e2021-06-23 11:23:44 +0100195
Joe Subbiani266476d2021-07-07 15:16:56 +0100196/**
Joe Subbiani0a65d532021-07-14 11:53:07 +0100197 * Put in memory a 16 bits unsigned integer in little-endian order.
Joe Subbiani266476d2021-07-07 15:16:56 +0100198 *
Joe Subbiani6350d3a2021-07-13 12:13:19 +0100199 * \param n 16 bits unsigned integer to put in memory.
200 * \param data Base address of the memory where to put the 16
Joe Subbiani0a65d532021-07-14 11:53:07 +0100201 * bits unsigned integer in.
Joe Subbiani6350d3a2021-07-13 12:13:19 +0100202 * \param offset Offset from \p base where to put the least significant
Joe Subbiani0a65d532021-07-14 11:53:07 +0100203 * byte of the 16 bits unsigned integer \p n.
Joe Subbiani266476d2021-07-07 15:16:56 +0100204 */
Joe Subbiani4530b272021-07-05 15:37:39 +0100205#ifndef MBEDTLS_PUT_UINT16_LE
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100206#define MBEDTLS_PUT_UINT16_LE(n, data, offset) \
207 { \
208 (data)[(offset)] = MBEDTLS_BYTE_0(n); \
209 (data)[(offset) + 1] = MBEDTLS_BYTE_1(n); \
210 }
Joe Subbiani4530b272021-07-05 15:37:39 +0100211#endif
212
Joe Subbiani1bd5d7d2021-07-16 12:29:49 +0100213/**
Joe Subbianic54e9082021-07-19 11:56:54 +0100214 * Get the unsigned 16 bits integer corresponding to two bytes in
Joe Subbiani896f4ee2021-07-19 15:29:18 +0100215 * big-endian order (MSB first).
Joe Subbianic54e9082021-07-19 11:56:54 +0100216 *
217 * \param data Base address of the memory to get the two bytes from.
218 * \param offset Offset from \p base of the first and most significant
219 * byte of the two bytes to build the 16 bits unsigned
220 * integer from.
221 */
222#ifndef MBEDTLS_GET_UINT16_BE
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100223#define MBEDTLS_GET_UINT16_BE(data, offset) \
Joe Subbiani896f4ee2021-07-19 15:29:18 +0100224 ( \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100225 ((uint16_t) (data)[(offset)] << 8) \
226 | ((uint16_t) (data)[(offset) + 1]) \
Joe Subbianic54e9082021-07-19 11:56:54 +0100227 )
228#endif
229
230/**
231 * Put in memory a 16 bits unsigned integer in big-endian order.
232 *
233 * \param n 16 bits unsigned integer to put in memory.
234 * \param data Base address of the memory where to put the 16
235 * bits unsigned integer in.
236 * \param offset Offset from \p base where to put the most significant
237 * byte of the 16 bits unsigned integer \p n.
238 */
239#ifndef MBEDTLS_PUT_UINT16_BE
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100240#define MBEDTLS_PUT_UINT16_BE(n, data, offset) \
241 { \
242 (data)[(offset)] = MBEDTLS_BYTE_1(n); \
243 (data)[(offset) + 1] = MBEDTLS_BYTE_0(n); \
244 }
Joe Subbianic54e9082021-07-19 11:56:54 +0100245#endif
246
247/**
Joe Subbiani1bd5d7d2021-07-16 12:29:49 +0100248 * Get the unsigned 64 bits integer corresponding to eight bytes in
249 * big-endian order (MSB first).
250 *
251 * \param data Base address of the memory to get the eight bytes from.
252 * \param offset Offset from \p base of the first and most significant
253 * byte of the eight bytes to build the 64 bits unsigned
254 * integer from.
255 */
256#ifndef MBEDTLS_GET_UINT64_BE
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100257#define MBEDTLS_GET_UINT64_BE(data, offset) \
Joe Subbiani896f4ee2021-07-19 15:29:18 +0100258 ( \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100259 ((uint64_t) (data)[(offset)] << 56) \
260 | ((uint64_t) (data)[(offset) + 1] << 48) \
261 | ((uint64_t) (data)[(offset) + 2] << 40) \
262 | ((uint64_t) (data)[(offset) + 3] << 32) \
263 | ((uint64_t) (data)[(offset) + 4] << 24) \
264 | ((uint64_t) (data)[(offset) + 5] << 16) \
265 | ((uint64_t) (data)[(offset) + 6] << 8) \
266 | ((uint64_t) (data)[(offset) + 7]) \
Joe Subbiani1bd5d7d2021-07-16 12:29:49 +0100267 )
268#endif
269
270/**
271 * Put in memory a 64 bits unsigned integer in big-endian order.
272 *
273 * \param n 64 bits unsigned integer to put in memory.
274 * \param data Base address of the memory where to put the 64
275 * bits unsigned integer in.
276 * \param offset Offset from \p base where to put the most significant
277 * byte of the 64 bits unsigned integer \p n.
278 */
279#ifndef MBEDTLS_PUT_UINT64_BE
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100280#define MBEDTLS_PUT_UINT64_BE(n, data, offset) \
281 { \
282 (data)[(offset)] = MBEDTLS_BYTE_7(n); \
283 (data)[(offset) + 1] = MBEDTLS_BYTE_6(n); \
284 (data)[(offset) + 2] = MBEDTLS_BYTE_5(n); \
285 (data)[(offset) + 3] = MBEDTLS_BYTE_4(n); \
286 (data)[(offset) + 4] = MBEDTLS_BYTE_3(n); \
287 (data)[(offset) + 5] = MBEDTLS_BYTE_2(n); \
288 (data)[(offset) + 6] = MBEDTLS_BYTE_1(n); \
289 (data)[(offset) + 7] = MBEDTLS_BYTE_0(n); \
290 }
Joe Subbiani1bd5d7d2021-07-16 12:29:49 +0100291#endif
292
293/**
294 * Get the unsigned 64 bits integer corresponding to eight bytes in
295 * little-endian order (LSB first).
296 *
297 * \param data Base address of the memory to get the eight bytes from.
298 * \param offset Offset from \p base of the first and least significant
299 * byte of the eight bytes to build the 64 bits unsigned
300 * integer from.
301 */
302#ifndef MBEDTLS_GET_UINT64_LE
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100303#define MBEDTLS_GET_UINT64_LE(data, offset) \
Joe Subbiani896f4ee2021-07-19 15:29:18 +0100304 ( \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100305 ((uint64_t) (data)[(offset) + 7] << 56) \
306 | ((uint64_t) (data)[(offset) + 6] << 48) \
307 | ((uint64_t) (data)[(offset) + 5] << 40) \
308 | ((uint64_t) (data)[(offset) + 4] << 32) \
309 | ((uint64_t) (data)[(offset) + 3] << 24) \
310 | ((uint64_t) (data)[(offset) + 2] << 16) \
311 | ((uint64_t) (data)[(offset) + 1] << 8) \
312 | ((uint64_t) (data)[(offset)]) \
Joe Subbiani1bd5d7d2021-07-16 12:29:49 +0100313 )
314#endif
315
316/**
317 * Put in memory a 64 bits unsigned integer in little-endian order.
318 *
319 * \param n 64 bits unsigned integer to put in memory.
320 * \param data Base address of the memory where to put the 64
321 * bits unsigned integer in.
322 * \param offset Offset from \p base where to put the least significant
323 * byte of the 64 bits unsigned integer \p n.
324 */
325#ifndef MBEDTLS_PUT_UINT64_LE
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100326#define MBEDTLS_PUT_UINT64_LE(n, data, offset) \
327 { \
328 (data)[(offset)] = MBEDTLS_BYTE_0(n); \
329 (data)[(offset) + 1] = MBEDTLS_BYTE_1(n); \
330 (data)[(offset) + 2] = MBEDTLS_BYTE_2(n); \
331 (data)[(offset) + 3] = MBEDTLS_BYTE_3(n); \
332 (data)[(offset) + 4] = MBEDTLS_BYTE_4(n); \
333 (data)[(offset) + 5] = MBEDTLS_BYTE_5(n); \
334 (data)[(offset) + 6] = MBEDTLS_BYTE_6(n); \
335 (data)[(offset) + 7] = MBEDTLS_BYTE_7(n); \
336 }
Joe Subbiani1bd5d7d2021-07-16 12:29:49 +0100337#endif
Joe Subbiani4530b272021-07-05 15:37:39 +0100338
Tom Cosgrovebdd01a72023-03-08 14:19:51 +0000339/* Always provide a static assert macro, so it can be used unconditionally.
Gilles Peskinef555a4e2024-06-12 19:13:19 +0200340 * It will expand to nothing on some systems. */
341/* Can't use the C11-style `defined(static_assert)` on FreeBSD, since it
Tom Cosgrovebdd01a72023-03-08 14:19:51 +0000342 * defines static_assert even with -std=c99, but then complains about it.
343 */
344#if defined(static_assert) && !defined(__FreeBSD__)
Gilles Peskinef555a4e2024-06-12 19:13:19 +0200345#define MBEDTLS_STATIC_ASSERT(expr, msg) static_assert(expr, msg)
Tom Cosgrovebdd01a72023-03-08 14:19:51 +0000346#else
Gilles Peskinef555a4e2024-06-12 19:13:19 +0200347/* Make sure `MBEDTLS_STATIC_ASSERT(expr, msg);` is valid both inside and
348 * outside a function. We choose a struct declaration, which can be repeated
349 * any number of times and does not need a matching definition. */
350#define MBEDTLS_STATIC_ASSERT(expr, msg) \
351 struct ISO_C_does_not_allow_extra_semicolon_outside_of_a_function
Tom Cosgrovebdd01a72023-03-08 14:19:51 +0000352#endif
353
Dave Rodgman52c294a2024-01-04 11:37:17 +0000354/* Suppress compiler warnings for unused functions and variables. */
355#if !defined(MBEDTLS_MAYBE_UNUSED) && defined(__has_attribute)
356# if __has_attribute(unused)
357# define MBEDTLS_MAYBE_UNUSED __attribute__((unused))
358# endif
359#endif
360#if !defined(MBEDTLS_MAYBE_UNUSED) && defined(__GNUC__)
361# define MBEDTLS_MAYBE_UNUSED __attribute__((unused))
362#endif
363#if !defined(MBEDTLS_MAYBE_UNUSED) && defined(__IAR_SYSTEMS_ICC__) && defined(__VER__)
364/* IAR does support __attribute__((unused)), but only if the -e flag (extended language support)
365 * is given; the pragma always works.
366 * Unfortunately the pragma affects the rest of the file where it is used, but this is harmless.
367 * Check for version 5.2 or later - this pragma may be supported by earlier versions, but I wasn't
368 * able to find documentation).
369 */
370# if (__VER__ >= 5020000)
371# define MBEDTLS_MAYBE_UNUSED _Pragma("diag_suppress=Pe177")
372# endif
373#endif
374#if !defined(MBEDTLS_MAYBE_UNUSED) && defined(_MSC_VER)
375# define MBEDTLS_MAYBE_UNUSED __pragma(warning(suppress:4189))
376#endif
377#if !defined(MBEDTLS_MAYBE_UNUSED)
378# define MBEDTLS_MAYBE_UNUSED
379#endif
380
Gilles Peskinec4672fd2019-09-11 13:39:11 +0200381#endif /* MBEDTLS_LIBRARY_COMMON_H */