Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

config: Port openssl sha224 to use EVP functions #244

Merged
merged 1 commit into from
Feb 10, 2020
Merged
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
32 changes: 24 additions & 8 deletions src/core/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
#include <sstream>
#include <stdexcept>
#include <boost/property_tree/json_parser.hpp>
#include <openssl/sha.h>
#include <openssl/evp.h>
using namespace std;
using namespace boost::property_tree;

Expand Down Expand Up @@ -126,14 +126,30 @@ bool Config::sip003() {
}

string Config::SHA224(const string &message) {
uint8_t digest[SHA224_DIGEST_LENGTH];
SHA256_CTX ctx;
SHA224_Init(&ctx);
SHA224_Update(&ctx, message.c_str(), message.length());
SHA224_Final(digest, &ctx);
char mdString[(SHA224_DIGEST_LENGTH << 1) + 1];
for (int i = 0; i < SHA224_DIGEST_LENGTH; ++i) {
uint8_t digest[EVP_MAX_MD_SIZE];
char mdString[(EVP_MAX_MD_SIZE << 1) + 1];
unsigned int digest_len;
EVP_MD_CTX *ctx;
if ((ctx = EVP_MD_CTX_new()) == NULL) {
throw runtime_error("could not create hash context");
}
if (!EVP_DigestInit_ex(ctx, EVP_sha224(), NULL)) {
EVP_MD_CTX_free(ctx);
throw runtime_error("could not initialize hash context");
}
if (!EVP_DigestUpdate(ctx, message.c_str(), message.length())) {
EVP_MD_CTX_free(ctx);
throw runtime_error("could not update hash");
}
if (!EVP_DigestFinal_ex(ctx, digest, &digest_len)) {
EVP_MD_CTX_free(ctx);
throw runtime_error("could not output hash");
}

for (unsigned int i = 0; i < digest_len; ++i) {
sprintf(mdString + (i << 1), "%02x", (unsigned int)digest[i]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As you can see from the diff, I didn’t change this line. But I’d be curious in seeing how you intend to exploit this with a single byte ...

}
mdString[digest_len << 1] = '\0';
EVP_MD_CTX_free(ctx);
return string(mdString);
}