Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -1327,6 +1327,8 @@ AC_CHECK_FUNCS([ \
X509_get0_signature \
ERR_get_error_all \
SHA1 \
SHA256_Init \
MD5_Init \
SSL_SESSION_dup \
])

Expand Down
48 changes: 43 additions & 5 deletions include/tscore/SHA256.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,30 +26,68 @@
#include "tscore/ink_code.h"
#include "tscore/ink_defs.h"
#include "tscore/CryptoHash.h"
#if HAVE_SHA256_INIT
#include <openssl/sha.h>
#else
#include <openssl/evp.h>
#endif

class SHA256Context : public ats::CryptoContextBase
{
#ifndef HAVE_SHA256_INIT
protected:
EVP_MD_CTX *ctx;
#endif

public:
SHA256Context()
{
ctx = EVP_MD_CTX_new();
EVP_DigestInit_ex(ctx, EVP_sha256(), nullptr);
#if HAVE_SHA256_INIT
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
SHA256_Init(&_sha256ctx);
#pragma GCC diagnostic pop
#else
_ctx = EVP_MD_CTX_new();
EVP_DigestInit_ex(_ctx, EVP_sha256(), nullptr);
#endif
}
~SHA256Context()
{
#if HAVE_SHA256_INIT
// _sha256ctx does not need to be freed
#else
EVP_MD_CTX_free(_ctx);
#endif
}
~SHA256Context() { EVP_MD_CTX_free(ctx); }
/// Update the hash with @a data of @a length bytes.
bool
update(void const *data, int length) override
{
return EVP_DigestUpdate(ctx, data, length);
#if HAVE_SHA256_INIT
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
return SHA256_Update(&_sha256ctx, data, length);
#pragma GCC diagnostic pop
#else
return EVP_DigestUpdate(_ctx, data, length);
#endif
}
/// Finalize and extract the @a hash.
bool
finalize(CryptoHash &hash) override
{
return EVP_DigestFinal_ex(ctx, hash.u8, nullptr);
#if HAVE_SHA256_INIT
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
return SHA256_Final(hash.u8, &_sha256ctx);
#pragma GCC diagnostic pop
#else
return EVP_DigestFinal_ex(_ctx, hash.u8, nullptr);
#endif
}
#if HAVE_SHA256_INIT
private:
SHA256_CTX _sha256ctx;
#endif
};