Skip to content

Commit ede5ccf

Browse files

File tree

3 files changed

+29
-33
lines changed

3 files changed

+29
-33
lines changed

src/crypto/hash.cc

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,24 @@
88

99
#include "crypto/error.hpp"
1010

11+
#if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
12+
#define EVP_MD_CTX_new EVP_MD_CTX_create
13+
#define EVP_MD_CTX_free EVP_MD_CTX_destroy
14+
#endif
15+
1116
namespace crypto {
1217

1318
class evp_md_ctx_wrapper_t {
1419
public:
1520
evp_md_ctx_wrapper_t() {
16-
#if OPENSSL_VERSION_NUMBER < 0x10100000L
17-
m_evp_md_ctx = EVP_MD_CTX_create();
18-
#else
1921
m_evp_md_ctx = EVP_MD_CTX_new();
20-
#endif
2122
if (m_evp_md_ctx == nullptr) {
2223
throw openssl_error_t(ERR_get_error());
2324
}
2425
}
2526

2627
~evp_md_ctx_wrapper_t() {
27-
#if OPENSSL_VERSION_NUMBER < 0x10100000L
28-
EVP_MD_CTX_destroy(m_evp_md_ctx);
29-
#else
3028
EVP_MD_CTX_free(m_evp_md_ctx);
31-
#endif
3229
}
3330

3431
EVP_MD_CTX *get() {

src/crypto/hmac.cc

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,43 +7,45 @@
77

88
#include "crypto/error.hpp"
99

10+
#if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
11+
12+
inline HMAC_CTX *HMAC_CTX_new() {
13+
HMAC_CTX *tmp = (HMAC_CTX *)OPENSSL_malloc(sizeof(HMAC_CTX));
14+
if (tmp)
15+
HMAC_CTX_init(tmp);
16+
return tmp;
17+
}
18+
19+
inline void HMAC_CTX_free(HMAC_CTX *ctx) {
20+
if (ctx) {
21+
HMAC_CTX_cleanup(ctx);
22+
OPENSSL_free(ctx);
23+
}
24+
}
25+
26+
#endif
27+
1028
namespace crypto {
1129

1230
class hmac_ctx_wrapper_t {
1331
public:
1432
hmac_ctx_wrapper_t() {
15-
#if OPENSSL_VERSION_NUMBER < 0x10100000L
16-
HMAC_CTX_init(&m_hmac_ctx);
17-
#else
1833
m_hmac_ctx = HMAC_CTX_new();
1934
if (m_hmac_ctx == nullptr) {
2035
throw openssl_error_t(ERR_get_error());
2136
}
22-
#endif
2337
}
2438

2539
~hmac_ctx_wrapper_t() {
26-
#if OPENSSL_VERSION_NUMBER < 0x10100000L
27-
HMAC_CTX_cleanup(&m_hmac_ctx);
28-
#else
2940
HMAC_CTX_free(m_hmac_ctx);
30-
#endif
3141
}
3242

3343
HMAC_CTX *get() {
34-
#if OPENSSL_VERSION_NUMBER < 0x10100000L
35-
return &m_hmac_ctx;
36-
#else
3744
return m_hmac_ctx;
38-
#endif
3945
}
4046

4147
private:
42-
#if OPENSSL_VERSION_NUMBER < 0x10100000L
43-
HMAC_CTX m_hmac_ctx;
44-
#else
4548
HMAC_CTX *m_hmac_ctx;
46-
#endif
4749
};
4850

4951
std::array<unsigned char, SHA256_DIGEST_LENGTH> detail::hmac_sha256(

src/crypto/initialization_guard.cc

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,17 @@
1414
#include "arch/io/concurrency.hpp"
1515
#include "arch/runtime/runtime.hpp"
1616

17+
#if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
18+
#define OPENSSL_init_ssl(x, y) SSL_library_init()
19+
#define OPENSSL_init_crypto(x, y) SSL_load_error_strings()
20+
#define OPENSSL_cleanup ERR_free_strings
21+
#endif
22+
1723
namespace crypto {
1824

1925
initialization_guard_t::initialization_guard_t() {
20-
#if OPENSSL_VERSION_NUMBER < 0x10100000L
21-
SSL_library_init();
22-
SSL_load_error_strings();
23-
#else
2426
OPENSSL_init_ssl(0, nullptr);
2527
OPENSSL_init_crypto(0, nullptr);
26-
#endif
2728

2829
// Make OpenSSL thread-safe by registering the required callbacks
2930
CRYPTO_THREADID_set_callback([](CRYPTO_THREADID *thread_out) {
@@ -49,11 +50,7 @@ initialization_guard_t::initialization_guard_t() {
4950
}
5051

5152
initialization_guard_t::~initialization_guard_t() {
52-
#if OPENSSL_VERSION_NUMBER < 0x10100000L
53-
ERR_free_strings();
54-
#else
5553
OPENSSL_cleanup();
56-
#endif
5754
}
5855

5956
} // namespace crypto

0 commit comments

Comments
 (0)