macros.h 11 KB

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