Skip to content

Commit

Permalink
sha1.hpp uses new OpenSSL apis
Browse files Browse the repository at this point in the history
  • Loading branch information
herumi committed Jul 12, 2022
1 parent 2b50e2c commit 4adc910
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 6 deletions.
2 changes: 1 addition & 1 deletion common.mk
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ EXTDIR:=$(TOPDIR)../cybozulib_ext/

CFLAGS+= -I$(TOPDIR)include
LDFLAGS += -lz -lpthread -lssl -lcrypto
HAS_BOOST=$(shell echo "\#include <boost/version.hpp>" | ($(CXX) -E - 2>/dev/null) | grep "boost/version.hpp" >/dev/null && echo "1")
HAS_BOOST=0#$(shell echo "\#include <boost/version.hpp>" | ($(CXX) -E - 2>/dev/null) | grep "boost/version.hpp" >/dev/null && echo "1")
HAS_MECAB=$(shell echo "\#include <mecab.h>" | ($(CXX) -E - 2>/dev/null) | grep "mecab.h" >/dev/null && echo "1")
HAS_EIGEN=$(shell echo "\#include <eigen3/Eigen/Sparse>" | ($(CXX) -E - 2>/dev/null) | grep "eigen3/Eigen/Sparse" >/dev/null && echo "1")
ifeq ($(HAS_BOOST),1)
Expand Down
64 changes: 59 additions & 5 deletions include/cybozu/crypto.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@
*/

#include <cybozu/exception.hpp>

#ifndef CYBOZU_USE_OPENSSL_NEW_HASH
#ifndef _MSC_VER
#define CYBOZU_USE_OPENSSL_NEW_HASH 1
#endif
#endif

#ifdef __APPLE__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
Expand All @@ -22,8 +29,10 @@
#else
#include <openssl/hmac.h>
#include <openssl/evp.h>
#if CYBOZU_USE_OPENSSL_NEW_HASH != 1
#include <openssl/sha.h>
#endif
#endif
#ifdef _MSC_VER
#include <cybozu/link_libeay32.hpp>
#endif
Expand All @@ -44,20 +53,33 @@ class Hash {
private:
Name name_;
size_t hashSize_;
#if CYBOZU_USE_OPENSSL_NEW_HASH == 1
EVP_MD_CTX *ctx_;
void setCTX(const char *name)
{
const EVP_MD *md = EVP_get_digestbyname(name);
if (md == 0) {
throw cybozu::Exception("EVP_get_digestbyname") << name;
}
EVP_MD_CTX_reset(ctx_);
EVP_DigestInit_ex2(ctx_, md, NULL);
}
#else
union {
SHA_CTX sha1;
SHA256_CTX sha256;
SHA512_CTX sha512;
} ctx_;
#endif
public:
static inline size_t getSize(Name name)
{
switch (name) {
case N_SHA1: return SHA_DIGEST_LENGTH;
case N_SHA224: return SHA224_DIGEST_LENGTH;
case N_SHA256: return SHA256_DIGEST_LENGTH;
case N_SHA384: return SHA384_DIGEST_LENGTH;
case N_SHA512: return SHA512_DIGEST_LENGTH;
case N_SHA1: return 160 / 8;
case N_SHA224: return 224 / 8;
case N_SHA256: return 256 / 8;
case N_SHA384: return 384 / 8;
case N_SHA512: return 512 / 8;
default:
throw cybozu::Exception("crypto:Hash:getSize") << name;
}
Expand Down Expand Up @@ -94,18 +116,31 @@ class Hash {
explicit Hash(Name name = N_SHA1)
: name_(name)
, hashSize_(getSize(name))
#if CYBOZU_USE_OPENSSL_NEW_HASH == 1
, ctx_(EVP_MD_CTX_new())
#endif
{
reset();
}
#if CYBOZU_USE_OPENSSL_NEW_HASH == 1
~Hash()
{
EVP_MD_CTX_free(ctx_);
}
#endif
void update(const void *buf, size_t bufSize)
{
#if CYBOZU_USE_OPENSSL_NEW_HASH == 1
EVP_DigestUpdate(ctx_, buf, bufSize);
#else
switch (name_) {
case N_SHA1: SHA1_Update(&ctx_.sha1, buf, bufSize); break;
case N_SHA224: SHA224_Update(&ctx_.sha256, buf, bufSize); break;
case N_SHA256: SHA256_Update(&ctx_.sha256, buf, bufSize); break;
case N_SHA384: SHA384_Update(&ctx_.sha512, buf, bufSize); break;
case N_SHA512: SHA512_Update(&ctx_.sha512, buf, bufSize); break;
}
#endif
}
void update(const std::string& buf)
{
Expand All @@ -114,11 +149,19 @@ class Hash {
void reset()
{
switch (name_) {
#if CYBOZU_USE_OPENSSL_NEW_HASH == 1
case N_SHA1: setCTX("sha1"); break;
case N_SHA224: setCTX("sha224"); break;
case N_SHA256: setCTX("sha256"); break;
case N_SHA384: setCTX("sha384"); break;
case N_SHA512: setCTX("sha512"); break;
#else
case N_SHA1: SHA1_Init(&ctx_.sha1); break;
case N_SHA224: SHA224_Init(&ctx_.sha256); break;
case N_SHA256: SHA256_Init(&ctx_.sha256); break;
case N_SHA384: SHA384_Init(&ctx_.sha512); break;
case N_SHA512: SHA512_Init(&ctx_.sha512); break;
#endif
default:
throw cybozu::Exception("crypto:Hash:rset") << name_;
}
Expand All @@ -131,6 +174,10 @@ class Hash {
{
update(buf, bufSize);
unsigned char *md = reinterpret_cast<unsigned char*>(out);
#if CYBOZU_USE_OPENSSL_NEW_HASH == 1
unsigned int len;
EVP_DigestFinal_ex(ctx_, md, &len);
#else
switch (name_) {
case N_SHA1: SHA1_Final(md, &ctx_.sha1); break;
case N_SHA224: SHA224_Final(md, &ctx_.sha256); break;
Expand All @@ -140,6 +187,7 @@ class Hash {
default:
throw cybozu::Exception("crypto:Hash:digest") << name_;
}
#endif
reset();
}
std::string digest(const void *buf, size_t bufSize)
Expand All @@ -159,6 +207,11 @@ class Hash {
*/
static inline size_t digest(void *out, Name name, const void *buf, size_t bufSize)
{
#if 1
Hash h(name);
h.digest(out, buf, bufSize);
return getSize(name);
#else
unsigned char *md = (unsigned char*)out;
const unsigned char *src = cybozu::cast<const unsigned char *>(buf);
switch (name) {
Expand All @@ -170,6 +223,7 @@ class Hash {
default:
return 0;
}
#endif
}
static inline std::string digest(Name name, const void *buf, size_t bufSize)
{
Expand Down

0 comments on commit 4adc910

Please sign in to comment.