1/*
2 * Copyright 2019-2026 The OpenSSL Project Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
8 */
9
10#ifndef OPENSSL_MACROS_H
11#define OPENSSL_MACROS_H
12#pragma once
13
14#include <openssl/configuration.h>
15#include <openssl/opensslv.h>
16
17/* Helper macros for CPP string composition */
18#define OPENSSL_MSTR_HELPER(x) #x
19#define OPENSSL_MSTR(x) OPENSSL_MSTR_HELPER(x)
20
21/*
22 * Sometimes OPENSSL_NO_xxx ends up with an empty file and some compilers
23 * don't like that. This will hopefully silence them.
24 */
25#define NON_EMPTY_TRANSLATION_UNIT static void *dummy = &dummy;
26
27/*
28 * Generic deprecation macro
29 *
30 * If OPENSSL_SUPPRESS_DEPRECATED is defined, then OSSL_DEPRECATED and
31 * OSSL_DEPRECATED_FOR become no-ops
32 */
33#ifndef OSSL_DEPRECATED
34#undef OSSL_DEPRECATED_FOR
35#ifndef OPENSSL_SUPPRESS_DEPRECATED
36#if defined(_MSC_VER)
37/*
38 * MSVC supports __declspec(deprecated) since MSVC 2003 (13.10),
39 * and __declspec(deprecated(message)) since MSVC 2005 (14.00)
40 */
41#if _MSC_VER >= 1400
42#define OSSL_DEPRECATED(since) \
43 __declspec(deprecated("Since OpenSSL " #since))
44#define OSSL_DEPRECATED_FOR(since, message) \
45 __declspec(deprecated("Since OpenSSL " #since "; " message))
46#define OSSL_DEPRECATED_MESSAGE(message) __declspec(deprecated(message))
47#elif _MSC_VER >= 1310
48#define OSSL_DEPRECATED(since) __declspec(deprecated)
49#define OSSL_DEPRECATED_FOR(since, message) __declspec(deprecated)
50#define OSSL_DEPRECATED_MESSAGE(message) __declspec(deprecated)
51#endif
52#define OSSL_BEGIN_ALLOW_DEPRECATED \
53 __pragma(warning(push)) __pragma(warning(disable : 4996))
54#define OSSL_END_ALLOW_DEPRECATED __pragma(warning(pop))
55#elif defined(__GNUC__)
56#define OSSL_DEPRECATED(since) \
57 __attribute__((deprecated("Since OpenSSL " #since)))
58#define OSSL_DEPRECATED_FOR(since, message) \
59 __attribute__((deprecated("Since OpenSSL " #since "; " message)))
60#define OSSL_DEPRECATED_MESSAGE(message) __attribute__((deprecated(message)))
61#define OSSL_BEGIN_ALLOW_DEPRECATED \
62 _Pragma("GCC diagnostic push") \
63 _Pragma("GCC diagnostic ignored \"-Wdeprecated-declarations\"")
64#define OSSL_END_ALLOW_DEPRECATED _Pragma("GCC diagnostic pop")
65#elif defined(__SUNPRO_C)
66#if (__SUNPRO_C >= 0x5130)
67#define OSSL_DEPRECATED(since) __attribute__((deprecated))
68#define OSSL_DEPRECATED_FOR(since, message) __attribute__((deprecated))
69#define OSSL_DEPRECATED_MESSAGE(message) __attribute__((deprecated))
70#endif
71#define OSSL_BEGIN_ALLOW_DEPRECATED \
72 #pragma error_messages(off, E_DEPRECATED_ATT, E_DEPRECATED_ATT_MESS)
73#define OSSL_END_ALLOW_DEPRECATED \
74 #pragma error_messages(on, E_DEPRECATED_ATT, E_DEPRECATED_ATT_MESS)
75#endif
76#endif
77#endif
78
79/*
80 * Still not defined? Then define no-op macros. This means these macros
81 * are unsuitable for use in a typedef except OSSL_DEPRECATED_MESSAGE.
82 */
83#ifndef OSSL_DEPRECATED
84#define OSSL_DEPRECATED(since) extern
85#define OSSL_DEPRECATED_FOR(since, message) extern
86#define OSSL_DEPRECATED_MESSAGE(message)
87#define OSSL_BEGIN_ALLOW_DEPRECATED
88#define OSSL_END_ALLOW_DEPRECATED
89#endif
90
91/*
92 * Applications should use -DOPENSSL_API_COMPAT=<version> to suppress the
93 * declarations of functions deprecated in or before <version>. If this is
94 * undefined, the value of the macro OPENSSL_CONFIGURED_API (defined in
95 * <openssl/opensslconf.h>) is the default.
96 *
97 * For any version number up until version 1.1.x, <version> is expected to be
98 * the calculated version number 0xMNNFFPPSL.
99 * For version numbers 3.0 and on, <version> is expected to be a computation
100 * of the major and minor numbers in decimal using this formula:
101 *
102 * MAJOR * 10000 + MINOR * 100
103 *
104 * So version 3.0 becomes 30000, version 3.2 becomes 30200, etc.
105 */
106
107/*
108 * We use the OPENSSL_API_COMPAT value to define API level macros. These
109 * macros are used to enable or disable features at that API version boundary.
110 */
111
112#ifdef OPENSSL_API_LEVEL
113#error "OPENSSL_API_LEVEL must not be defined by application"
114#endif
115
116/*
117 * We figure out what API level was intended by simple numeric comparison.
118 * The lowest old style number we recognise is 0x00908000L, so we take some
119 * safety margin and assume that anything below 0x00900000L is a new style
120 * number. This allows new versions up to and including v943.71.83.
121 */
122#ifdef OPENSSL_API_COMPAT
123#if OPENSSL_API_COMPAT < 0x900000L
124#define OPENSSL_API_LEVEL (OPENSSL_API_COMPAT)
125#else
126#define OPENSSL_API_LEVEL \
127 (((OPENSSL_API_COMPAT >> 28) & 0xF) * 10000 \
128 + ((OPENSSL_API_COMPAT >> 20) & 0xFF) * 100 \
129 + ((OPENSSL_API_COMPAT >> 12) & 0xFF))
130#endif
131#endif
132
133/*
134 * If OPENSSL_API_COMPAT wasn't given, we use default numbers to set
135 * the API compatibility level.
136 */
137#ifndef OPENSSL_API_LEVEL
138#if OPENSSL_CONFIGURED_API > 0
139#define OPENSSL_API_LEVEL (OPENSSL_CONFIGURED_API)
140#else
141#define OPENSSL_API_LEVEL \
142 (OPENSSL_VERSION_MAJOR * 10000 + OPENSSL_VERSION_MINOR * 100)
143#endif
144#endif
145
146#if OPENSSL_API_LEVEL > OPENSSL_CONFIGURED_API
147#error "The requested API level higher than the configured API compatibility level"
148#endif
149
150/*
151 * Check of sane values.
152 */
153/* Can't go higher than the current version. */
154#if OPENSSL_API_LEVEL > (OPENSSL_VERSION_MAJOR * 10000 + OPENSSL_VERSION_MINOR * 100)
155#error "OPENSSL_API_COMPAT expresses an impossible API compatibility level"
156#endif
157/* OpenSSL will have no version 2.y.z */
158#if OPENSSL_API_LEVEL < 30000 && OPENSSL_API_LEVEL >= 20000
159#error "OPENSSL_API_COMPAT expresses an impossible API compatibility level"
160#endif
161/* Below 0.9.8 is unacceptably low */
162#if OPENSSL_API_LEVEL < 908
163#error "OPENSSL_API_COMPAT expresses an impossible API compatibility level"
164#endif
165
166/*
167 * Define macros for deprecation and simulated removal purposes.
168 *
169 * The macros OSSL_DEPRECATEDIN_{major}_{minor} are always defined for
170 * all OpenSSL versions we care for. They can be used as attributes
171 * in function declarations where appropriate.
172 *
173 * The macros OPENSSL_NO_DEPRECATED_{major}_{minor} are defined for
174 * all OpenSSL versions up to or equal to the version given with
175 * OPENSSL_API_COMPAT. They are used as guards around anything that's
176 * deprecated up to that version, as an effect of the developer option
177 * 'no-deprecated'.
178 */
179
180#undef OPENSSL_NO_DEPRECATED_4_1
181#undef OPENSSL_NO_DEPRECATED_4_0
182#undef OPENSSL_NO_DEPRECATED_3_6
183#undef OPENSSL_NO_DEPRECATED_3_5
184#undef OPENSSL_NO_DEPRECATED_3_4
185#undef OPENSSL_NO_DEPRECATED_3_1
186#undef OPENSSL_NO_DEPRECATED_3_0
187#undef OPENSSL_NO_DEPRECATED_1_1_1
188#undef OPENSSL_NO_DEPRECATED_1_1_0
189#undef OPENSSL_NO_DEPRECATED_1_0_2
190#undef OPENSSL_NO_DEPRECATED_1_0_1
191#undef OPENSSL_NO_DEPRECATED_1_0_0
192#undef OPENSSL_NO_DEPRECATED_0_9_8
193
194#if OPENSSL_API_LEVEL >= 40100
195#ifndef OPENSSL_NO_DEPRECATED
196#define OSSL_DEPRECATEDIN_4_1 OSSL_DEPRECATED(4.1)
197#define OSSL_DEPRECATEDIN_4_1_FOR(msg) OSSL_DEPRECATED_FOR(4.1, msg)
198#else
199#define OPENSSL_NO_DEPRECATED_4_1
200#endif
201#else
202#define OSSL_DEPRECATEDIN_4_1
203#define OSSL_DEPRECATEDIN_4_1_FOR(msg)
204#endif
205#if OPENSSL_API_LEVEL >= 40000
206#ifndef OPENSSL_NO_DEPRECATED
207#define OSSL_DEPRECATEDIN_4_0 OSSL_DEPRECATED(4.0)
208#define OSSL_DEPRECATEDIN_4_0_FOR(msg) OSSL_DEPRECATED_FOR(4.0, msg)
209#else
210#define OPENSSL_NO_DEPRECATED_4_0
211#endif
212#else
213#define OSSL_DEPRECATEDIN_4_0
214#define OSSL_DEPRECATEDIN_4_0_FOR(msg)
215#endif
216#if OPENSSL_API_LEVEL >= 30600
217#ifndef OPENSSL_NO_DEPRECATED
218#define OSSL_DEPRECATEDIN_3_6 OSSL_DEPRECATED(3.6)
219#define OSSL_DEPRECATEDIN_3_6_FOR(msg) OSSL_DEPRECATED_FOR(3.6, msg)
220#else
221#define OPENSSL_NO_DEPRECATED_3_6
222#endif
223#else
224#define OSSL_DEPRECATEDIN_3_6
225#define OSSL_DEPRECATEDIN_3_6_FOR(msg)
226#endif
227#if OPENSSL_API_LEVEL >= 30500
228#ifndef OPENSSL_NO_DEPRECATED
229#define OSSL_DEPRECATEDIN_3_5 OSSL_DEPRECATED(3.5)
230#define OSSL_DEPRECATEDIN_3_5_FOR(msg) OSSL_DEPRECATED_FOR(3.5, msg)
231#else
232#define OPENSSL_NO_DEPRECATED_3_5
233#endif
234#else
235#define OSSL_DEPRECATEDIN_3_5
236#define OSSL_DEPRECATEDIN_3_5_FOR(msg)
237#endif
238#if OPENSSL_API_LEVEL >= 30400
239#ifndef OPENSSL_NO_DEPRECATED
240#define OSSL_DEPRECATEDIN_3_4 OSSL_DEPRECATED(3.4)
241#define OSSL_DEPRECATEDIN_3_4_FOR(msg) OSSL_DEPRECATED_FOR(3.4, msg)
242#else
243#define OPENSSL_NO_DEPRECATED_3_4
244#endif
245#else
246#define OSSL_DEPRECATEDIN_3_4
247#define OSSL_DEPRECATEDIN_3_4_FOR(msg)
248#endif
249#if OPENSSL_API_LEVEL >= 30100
250#ifndef OPENSSL_NO_DEPRECATED
251#define OSSL_DEPRECATEDIN_3_1 OSSL_DEPRECATED(3.1)
252#define OSSL_DEPRECATEDIN_3_1_FOR(msg) OSSL_DEPRECATED_FOR(3.1, msg)
253#else
254#define OPENSSL_NO_DEPRECATED_3_1
255#endif
256#else
257#define OSSL_DEPRECATEDIN_3_1
258#define OSSL_DEPRECATEDIN_3_1_FOR(msg)
259#endif
260#if OPENSSL_API_LEVEL >= 30000
261#ifndef OPENSSL_NO_DEPRECATED
262#define OSSL_DEPRECATEDIN_3_0 OSSL_DEPRECATED(3.0)
263#define OSSL_DEPRECATEDIN_3_0_FOR(msg) OSSL_DEPRECATED_FOR(3.0, msg)
264#else
265#define OPENSSL_NO_DEPRECATED_3_0
266#endif
267#else
268#define OSSL_DEPRECATEDIN_3_0
269#define OSSL_DEPRECATEDIN_3_0_FOR(msg)
270#endif
271#if OPENSSL_API_LEVEL >= 10101
272#ifndef OPENSSL_NO_DEPRECATED
273#define OSSL_DEPRECATEDIN_1_1_1 OSSL_DEPRECATED(1.1.1)
274#define OSSL_DEPRECATEDIN_1_1_1_FOR(msg) OSSL_DEPRECATED_FOR(1.1.1, msg)
275#else
276#define OPENSSL_NO_DEPRECATED_1_1_1
277#endif
278#else
279#define OSSL_DEPRECATEDIN_1_1_1
280#define OSSL_DEPRECATEDIN_1_1_1_FOR(msg)
281#endif
282#if OPENSSL_API_LEVEL >= 10100
283#ifndef OPENSSL_NO_DEPRECATED
284#define OSSL_DEPRECATEDIN_1_1_0 OSSL_DEPRECATED(1.1.0)
285#define OSSL_DEPRECATEDIN_1_1_0_FOR(msg) OSSL_DEPRECATED_FOR(1.1.0, msg)
286#else
287#define OPENSSL_NO_DEPRECATED_1_1_0
288#endif
289#else
290#define OSSL_DEPRECATEDIN_1_1_0
291#define OSSL_DEPRECATEDIN_1_1_0_FOR(msg)
292#endif
293#if OPENSSL_API_LEVEL >= 10002
294#ifndef OPENSSL_NO_DEPRECATED
295#define OSSL_DEPRECATEDIN_1_0_2 OSSL_DEPRECATED(1.0.2)
296#define OSSL_DEPRECATEDIN_1_0_2_FOR(msg) OSSL_DEPRECATED_FOR(1.0.2, msg)
297#else
298#define OPENSSL_NO_DEPRECATED_1_0_2
299#endif
300#else
301#define OSSL_DEPRECATEDIN_1_0_2
302#define OSSL_DEPRECATEDIN_1_0_2_FOR(msg)
303#endif
304#if OPENSSL_API_LEVEL >= 10001
305#ifndef OPENSSL_NO_DEPRECATED
306#define OSSL_DEPRECATEDIN_1_0_1 OSSL_DEPRECATED(1.0.1)
307#define OSSL_DEPRECATEDIN_1_0_1_FOR(msg) OSSL_DEPRECATED_FOR(1.0.1, msg)
308#else
309#define OPENSSL_NO_DEPRECATED_1_0_1
310#endif
311#else
312#define OSSL_DEPRECATEDIN_1_0_1
313#define OSSL_DEPRECATEDIN_1_0_1_FOR(msg)
314#endif
315#if OPENSSL_API_LEVEL >= 10000
316#ifndef OPENSSL_NO_DEPRECATED
317#define OSSL_DEPRECATEDIN_1_0_0 OSSL_DEPRECATED(1.0.0)
318#define OSSL_DEPRECATEDIN_1_0_0_FOR(msg) OSSL_DEPRECATED_FOR(1.0.0, msg)
319#else
320#define OPENSSL_NO_DEPRECATED_1_0_0
321#endif
322#else
323#define OSSL_DEPRECATEDIN_1_0_0
324#define OSSL_DEPRECATEDIN_1_0_0_FOR(msg)
325#endif
326#if OPENSSL_API_LEVEL >= 908
327#ifndef OPENSSL_NO_DEPRECATED
328#define OSSL_DEPRECATEDIN_0_9_8 OSSL_DEPRECATED(0.9.8)
329#define OSSL_DEPRECATEDIN_0_9_8_FOR(msg) OSSL_DEPRECATED_FOR(0.9.8, msg)
330#else
331#define OPENSSL_NO_DEPRECATED_0_9_8
332#endif
333#else
334#define OSSL_DEPRECATEDIN_0_9_8
335#define OSSL_DEPRECATEDIN_0_9_8_FOR(msg)
336#endif
337
338/*
339 * Make our own variants of __FILE__ and __LINE__, depending on configuration
340 */
341
342#ifndef OPENSSL_FILE
343#ifdef OPENSSL_NO_FILENAMES
344#define OPENSSL_FILE ""
345#define OPENSSL_LINE 0
346#else
347#define OPENSSL_FILE __FILE__
348#define OPENSSL_LINE __LINE__
349#endif
350#endif
351
352/*
353 * __func__ was standardized in C99, so for any compiler that claims
354 * to implement that language level or newer, we assume we can safely
355 * use that symbol.
356 *
357 * GNU C also provides __FUNCTION__ since version 2, which predates
358 * C99. We can, however, only use this if __STDC_VERSION__ exists,
359 * as it's otherwise not allowed according to ISO C standards (C90).
360 * (compiling with GNU C's -pedantic tells us so)
361 *
362 * If none of the above applies, we check if the compiler is MSVC,
363 * and use __FUNCTION__ if that's the case.
364 */
365#ifndef OPENSSL_FUNC
366#if defined(__STDC_VERSION__)
367#if __STDC_VERSION__ >= 199901L
368#define OPENSSL_FUNC __func__
369#elif defined(__GNUC__)
370#define OPENSSL_FUNC __FUNCTION__
371#endif
372#elif defined(_MSC_VER)
373#define OPENSSL_FUNC __FUNCTION__
374#endif
375/*
376 * If all these possibilities are exhausted, we give up and use a
377 * static string.
378 */
379#ifndef OPENSSL_FUNC
380#define OPENSSL_FUNC "(unknown function)"
381#endif
382#endif
383
384#ifndef OSSL_CRYPTO_ALLOC
385#if defined(__GNUC__)
386#define OSSL_CRYPTO_ALLOC __attribute__((__malloc__))
387#elif defined(_MSC_VER)
388#define OSSL_CRYPTO_ALLOC __declspec(restrict)
389#else
390#define OSSL_CRYPTO_ALLOC
391#endif
392#endif
393
394#endif /* OPENSSL_MACROS_H */
395