mirror of
https://github.com/zeek/zeek.git
synced 2025-10-16 21:48:21 +00:00
Avoid OpenSSL header dependencies
This commit is contained in:
parent
4eb1b71d1b
commit
c500370563
13 changed files with 370 additions and 204 deletions
|
@ -6,11 +6,38 @@
|
|||
|
||||
#include "zeek/digest.h"
|
||||
|
||||
#include <openssl/evp.h>
|
||||
#include <openssl/md5.h>
|
||||
#include <openssl/sha.h>
|
||||
|
||||
#include "zeek/Reporter.h"
|
||||
|
||||
#if ( OPENSSL_VERSION_NUMBER < 0x10100000L ) || defined(LIBRESSL_VERSION_NUMBER)
|
||||
#define EVP_MD_CTX_new EVP_MD_CTX_create
|
||||
#define EVP_MD_CTX_free EVP_MD_CTX_destroy
|
||||
#endif
|
||||
|
||||
static_assert(ZEEK_MD5_DIGEST_LENGTH == MD5_DIGEST_LENGTH);
|
||||
|
||||
static_assert(ZEEK_SHA_DIGEST_LENGTH == SHA_DIGEST_LENGTH);
|
||||
|
||||
static_assert(ZEEK_SHA224_DIGEST_LENGTH == SHA224_DIGEST_LENGTH);
|
||||
|
||||
static_assert(ZEEK_SHA256_DIGEST_LENGTH == SHA256_DIGEST_LENGTH);
|
||||
|
||||
static_assert(ZEEK_SHA384_DIGEST_LENGTH == SHA384_DIGEST_LENGTH);
|
||||
|
||||
static_assert(ZEEK_SHA512_DIGEST_LENGTH == SHA512_DIGEST_LENGTH);
|
||||
|
||||
namespace zeek::detail {
|
||||
|
||||
EVP_MD_CTX* hash_init(HashAlgorithm alg) {
|
||||
namespace {
|
||||
auto* to_native_ptr(HashDigestState* ptr) { return reinterpret_cast<EVP_MD_CTX*>(ptr); }
|
||||
auto* to_native_ptr(const HashDigestState* ptr) { return reinterpret_cast<const EVP_MD_CTX*>(ptr); }
|
||||
auto* to_opaque_ptr(EVP_MD_CTX* ptr) { return reinterpret_cast<HashDigestState*>(ptr); }
|
||||
} // namespace
|
||||
|
||||
HashDigestState* hash_init(HashAlgorithm alg) {
|
||||
EVP_MD_CTX* c = EVP_MD_CTX_new();
|
||||
const EVP_MD* md;
|
||||
|
||||
|
@ -33,19 +60,31 @@ EVP_MD_CTX* hash_init(HashAlgorithm alg) {
|
|||
if ( ! EVP_DigestInit_ex(c, md, NULL) )
|
||||
reporter->InternalError("EVP_DigestInit failed");
|
||||
|
||||
return c;
|
||||
return to_opaque_ptr(c);
|
||||
}
|
||||
|
||||
void hash_update(EVP_MD_CTX* c, const void* data, unsigned long len) {
|
||||
if ( ! EVP_DigestUpdate(c, data, len) )
|
||||
void hash_update(HashDigestState* c, const void* data, unsigned long len) {
|
||||
if ( ! EVP_DigestUpdate(to_native_ptr(c), data, len) )
|
||||
reporter->InternalError("EVP_DigestUpdate failed");
|
||||
}
|
||||
|
||||
void hash_final(EVP_MD_CTX* c, u_char* md) {
|
||||
if ( ! EVP_DigestFinal(c, md, NULL) )
|
||||
reporter->InternalError("EVP_DigestFinal failed");
|
||||
void hash_final(HashDigestState* c, u_char* md) {
|
||||
hash_final_no_free(c, md);
|
||||
EVP_MD_CTX_free(to_native_ptr(c));
|
||||
}
|
||||
|
||||
EVP_MD_CTX_free(c);
|
||||
void hash_final_no_free(HashDigestState* c, u_char* md) {
|
||||
if ( ! EVP_DigestFinal(to_native_ptr(c), md, NULL) )
|
||||
reporter->InternalError("EVP_DigestFinal failed");
|
||||
}
|
||||
|
||||
void hash_state_free(HashDigestState* c) {
|
||||
if ( c != nullptr )
|
||||
EVP_MD_CTX_free(to_native_ptr(c));
|
||||
}
|
||||
|
||||
void hash_copy(HashDigestState* out, const HashDigestState* in) {
|
||||
EVP_MD_CTX_copy_ex(to_native_ptr(out), to_native_ptr(in));
|
||||
}
|
||||
|
||||
unsigned char* internal_md5(const unsigned char* data, unsigned long len, unsigned char* out) {
|
||||
|
@ -59,7 +98,7 @@ unsigned char* calculate_digest(HashAlgorithm alg, const unsigned char* data, ui
|
|||
if ( ! out )
|
||||
out = static_out; // use static array for return, see OpenSSL man page
|
||||
|
||||
EVP_MD_CTX* c = hash_init(alg);
|
||||
auto* c = hash_init(alg);
|
||||
hash_update(c, data, len);
|
||||
hash_final(c, out);
|
||||
return out;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue