3232#ifndef OPENSSL_NO_ENGINE
3333#include < openssl/engine.h>
3434#endif // !OPENSSL_NO_ENGINE
35+
36+ #ifndef OPENSSL_VERSION_PREREQ
37+ #define OPENSSL_VERSION_PREREQ (maj, min ) \
38+ (OPENSSL_VERSION_NUMBER >= (((maj) << 28 ) | ((min) << 20 )))
39+ #endif
40+
41+ // BoringSSL declares the EVP_*_do_all* APIs, but their implementation may
42+ // live in libdecrepit. This matches standalone ncrypto's build flag.
43+ #ifndef NCRYPTO_BSSL_LIBDECREPIT_MISSING
44+ #define NCRYPTO_BSSL_LIBDECREPIT_MISSING 0
45+ #endif
46+
47+ #if defined(OPENSSL_IS_BORINGSSL) && NCRYPTO_BSSL_LIBDECREPIT_MISSING
48+ #define NCRYPTO_USE_BORINGSSL_EVP_DO_ALL_FALLBACK 1
49+ #else
50+ #define NCRYPTO_USE_BORINGSSL_EVP_DO_ALL_FALLBACK 0
51+ #endif
52+
3553// The FIPS-related functions are only available
3654// when the OpenSSL itself was compiled with FIPS support.
37- #if defined(OPENSSL_FIPS) && OPENSSL_VERSION_MAJOR < 3
55+ #if defined(OPENSSL_FIPS) && !OPENSSL_VERSION_PREREQ(3, 0)
3856#include < openssl/fips.h>
3957#endif // OPENSSL_FIPS
4058
41- // Define OPENSSL_WITH_PQC for post-quantum cryptography support
42- #if OPENSSL_VERSION_NUMBER >= 0x30500000L
43- #define OPENSSL_WITH_PQC 1
59+ #if OPENSSL_VERSION_PREREQ(3, 0)
60+ #define OPENSSL_WITH_AES_OCB 1
61+ #else
62+ #define OPENSSL_WITH_AES_OCB 0
63+ #endif
64+
65+ #if !defined(OPENSSL_NO_ARGON2) && OPENSSL_VERSION_PREREQ(3, 2)
66+ #define OPENSSL_WITH_ARGON2 1
67+ #else
68+ #define OPENSSL_WITH_ARGON2 0
69+ #endif
70+
71+ #if OPENSSL_VERSION_PREREQ(3, 0) || defined(OPENSSL_IS_BORINGSSL)
72+ #define OPENSSL_WITH_KEM 1
73+ #else
74+ #define OPENSSL_WITH_KEM 0
75+ #endif
76+
77+ #if OPENSSL_VERSION_PREREQ(3, 0)
78+ #define OPENSSL_WITH_KMAC 1
79+ #else
80+ #define OPENSSL_WITH_KMAC 0
81+ #endif
82+
83+ #if defined(OPENSSL_IS_BORINGSSL) || OPENSSL_VERSION_PREREQ(3, 2)
84+ #define OPENSSL_WITH_SIGNATURE_CONTEXT_STRING 1
85+ #else
86+ #define OPENSSL_WITH_SIGNATURE_CONTEXT_STRING 0
87+ #endif
88+
89+ #if !defined(OPENSSL_IS_BORINGSSL) && OPENSSL_VERSION_PREREQ(3, 2)
90+ #define OPENSSL_WITH_OPENSSL_DHKEM 1
91+ #else
92+ #define OPENSSL_WITH_OPENSSL_DHKEM 0
93+ #endif
94+
95+ #if OPENSSL_WITH_KEM && !defined(OPENSSL_IS_BORINGSSL) && \
96+ !OPENSSL_VERSION_PREREQ (3 , 5 )
97+ #define OPENSSL_WITH_KEM_OPERATION_PARAM 1
98+ #else
99+ #define OPENSSL_WITH_KEM_OPERATION_PARAM 0
100+ #endif
101+
102+ // Post-quantum cryptography support. Keep these explicit so code can
103+ // distinguish provider API shape from the available algorithm set.
104+ #if !defined(OPENSSL_IS_BORINGSSL) && OPENSSL_VERSION_PREREQ(3, 5)
105+ #define OPENSSL_WITH_OPENSSL_PQC 1
106+ #else
107+ #define OPENSSL_WITH_OPENSSL_PQC 0
108+ #endif
109+
110+ #ifdef OPENSSL_IS_BORINGSSL
111+ #define OPENSSL_WITH_BORINGSSL_PQC 1
112+ #else
113+ #define OPENSSL_WITH_BORINGSSL_PQC 0
114+ #endif
115+
116+ #define OPENSSL_WITH_PQC \
117+ (OPENSSL_WITH_OPENSSL_PQC || OPENSSL_WITH_BORINGSSL_PQC )
118+ #define OPENSSL_WITH_PQC_ML_KEM_512 OPENSSL_WITH_OPENSSL_PQC
119+ #define OPENSSL_WITH_PQC_SLH_DSA OPENSSL_WITH_OPENSSL_PQC
120+
121+ #if OPENSSL_WITH_OPENSSL_PQC
44122#define EVP_PKEY_ML_KEM_512 NID_ML_KEM_512
45123#define EVP_PKEY_ML_KEM_768 NID_ML_KEM_768
46124#define EVP_PKEY_ML_KEM_1024 NID_ML_KEM_1024
47125#include < openssl/core_names.h>
126+ #elif OPENSSL_WITH_BORINGSSL_PQC
127+ #define EVP_PKEY_ML_KEM_768 NID_ML_KEM_768
128+ #define EVP_PKEY_ML_KEM_1024 NID_ML_KEM_1024
48129#endif
49130
50- #if OPENSSL_VERSION_MAJOR >= 3
131+ #if OPENSSL_VERSION_PREREQ(3, 0)
51132#define OSSL3_CONST const
52133#else
53134#define OSSL3_CONST
@@ -1515,7 +1596,7 @@ class HMACCtxPointer final {
15151596 DeleteFnPtr<HMAC_CTX , HMAC_CTX_free> ctx_;
15161597};
15171598
1518- #if OPENSSL_VERSION_MAJOR >= 3
1599+ #if OPENSSL_WITH_KMAC
15191600class EVPMacPointer final {
15201601 public:
15211602 EVPMacPointer () = default ;
@@ -1563,7 +1644,7 @@ class EVPMacCtxPointer final {
15631644 private:
15641645 DeleteFnPtr<EVP_MAC_CTX , EVP_MAC_CTX_free> ctx_;
15651646};
1566- #endif // OPENSSL_VERSION_MAJOR >= 3
1647+ #endif // OPENSSL_WITH_KMAC
15671648
15681649#ifndef OPENSSL_NO_ENGINE
15691650class EnginePointer final {
@@ -1703,8 +1784,7 @@ DataPointer pbkdf2(const Digest& md,
17031784 uint32_t iterations,
17041785 size_t length);
17051786
1706- #if OPENSSL_VERSION_NUMBER >= 0x30200000L
1707- #ifndef OPENSSL_NO_ARGON2
1787+ #if OPENSSL_WITH_ARGON2
17081788enum class Argon2Type { ARGON2D , ARGON2I , ARGON2ID };
17091789
17101790DataPointer argon2 (const Buffer<const char >& pass,
@@ -1718,11 +1798,10 @@ DataPointer argon2(const Buffer<const char>& pass,
17181798 const Buffer<const unsigned char >& ad,
17191799 Argon2Type type);
17201800#endif
1721- #endif
17221801
17231802// ============================================================================
17241803// KEM (Key Encapsulation Mechanism)
1725- #if OPENSSL_VERSION_MAJOR >= 3
1804+ #if OPENSSL_WITH_KEM
17261805
17271806class KEM final {
17281807 public:
@@ -1746,13 +1825,13 @@ class KEM final {
17461825 const Buffer<const void >& ciphertext);
17471826
17481827 private:
1749- #if !OPENSSL_VERSION_PREREQ(3, 5)
1828+ #if OPENSSL_WITH_KEM_OPERATION_PARAM
17501829 static bool SetOperationParameter (EVP_PKEY_CTX * ctx,
17511830 const EVPKeyPointer& key);
17521831#endif
17531832};
17541833
1755- #endif // OPENSSL_VERSION_MAJOR >= 3
1834+ #endif // OPENSSL_WITH_KEM
17561835
17571836#include " ncrypto/version.h"
17581837
0 commit comments