Avoid OpenSSL header dependencies

This commit is contained in:
Dominik Charousset 2023-11-03 15:54:46 +01:00
parent 4eb1b71d1b
commit c500370563
13 changed files with 370 additions and 204 deletions

View file

@ -6,18 +6,31 @@
#pragma once
#include <openssl/evp.h>
#include <openssl/md5.h>
#include <openssl/sha.h>
#include <sys/types.h> // for u_char
#include <cstdint>
#include <cstdio>
#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
// Required buffer size for an MD5 digest.
#define ZEEK_MD5_DIGEST_LENGTH 16
inline void* EVP_MD_CTX_md_data(const EVP_MD_CTX* ctx) { return ctx->md_data; }
#endif
// Required buffer size for an SHA1 digest.
#define ZEEK_SHA_DIGEST_LENGTH 20
// Required buffer size for an SHA224 digest.
#define ZEEK_SHA224_DIGEST_LENGTH 28
// Required buffer size for an SHA256 digest.
#define ZEEK_SHA256_DIGEST_LENGTH 32
// Required buffer size for an SHA384 digest.
#define ZEEK_SHA384_DIGEST_LENGTH 48
// Required buffer size for an SHA512 digest.
#define ZEEK_SHA512_DIGEST_LENGTH 64
// Buffer size for a digest of any type in hex representation plus size for at
// least a null terminator.
#define ZEEK_DIGEST_PRINT_LENGTH (ZEEK_SHA512_DIGEST_LENGTH * 2) + 1
namespace zeek::detail {
@ -26,29 +39,55 @@ namespace zeek::detail {
enum HashAlgorithm { Hash_MD5, Hash_SHA1, Hash_SHA224, Hash_SHA256, Hash_SHA384, Hash_SHA512 };
inline const char* digest_print(const u_char* digest, size_t n) {
static char buf[256]; // big enough for any of md5/sha1/sha256
static char buf[ZEEK_DIGEST_PRINT_LENGTH];
for ( size_t i = 0; i < n; ++i )
snprintf(buf + i * 2, 3, "%02x", digest[i]);
return buf;
}
inline const char* md5_digest_print(const u_char digest[MD5_DIGEST_LENGTH]) {
return digest_print(digest, MD5_DIGEST_LENGTH);
inline const char* md5_digest_print(const u_char digest[ZEEK_MD5_DIGEST_LENGTH]) {
return digest_print(digest, ZEEK_MD5_DIGEST_LENGTH);
}
inline const char* sha1_digest_print(const u_char digest[SHA_DIGEST_LENGTH]) {
return digest_print(digest, SHA_DIGEST_LENGTH);
inline const char* sha1_digest_print(const u_char digest[ZEEK_SHA_DIGEST_LENGTH]) {
return digest_print(digest, ZEEK_SHA_DIGEST_LENGTH);
}
inline const char* sha256_digest_print(const u_char digest[SHA256_DIGEST_LENGTH]) {
return digest_print(digest, SHA256_DIGEST_LENGTH);
inline const char* sha256_digest_print(const u_char digest[ZEEK_SHA256_DIGEST_LENGTH]) {
return digest_print(digest, ZEEK_SHA256_DIGEST_LENGTH);
}
EVP_MD_CTX* hash_init(HashAlgorithm alg);
struct HashDigestState;
void hash_update(EVP_MD_CTX* c, const void* data, unsigned long len);
/**
* Allocates and initializes a new HashDigestState.
*/
HashDigestState* hash_init(HashAlgorithm alg);
void hash_final(EVP_MD_CTX* c, u_char* md);
/**
* Adds data to the digest.
*/
void hash_update(HashDigestState* c, const void* data, unsigned long len);
/**
* Finalizes the digest, writes it to the given buffer and deletes it.
*/
void hash_final(HashDigestState* c, u_char* md);
/**
* Finalizes the digest and writes it to the given buffer without deleting it afterwards.
*/
void hash_final_no_free(HashDigestState* c, u_char* md);
/**
* Frees the HashDigestState.
*/
void hash_state_free(HashDigestState* c);
/**
* Copies the HashDigestState from in to out.
*/
void hash_copy(HashDigestState* out, const HashDigestState* in);
unsigned char* internal_md5(const unsigned char* data, unsigned long len, unsigned char* out);