Skip to content

crypto: add HMAC to crypto.timingSafeEqual() #38488

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

Closed
wants to merge 1 commit into from
Closed
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
43 changes: 42 additions & 1 deletion src/crypto/crypto_timing.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#include "node.h"

#include <openssl/crypto.h>
#include <openssl/hmac.h>
#include <openssl/sha.h>

namespace node {

Expand Down Expand Up @@ -42,8 +44,47 @@ void TimingSafeEqual(const FunctionCallbackInfo<Value>& args) {
return;
}

uint16_t bufKey[8];
CHECK(crypto::EntropySource(reinterpret_cast<unsigned char*>(bufKey),
sizeof(bufKey)));
char key[kKeySize];
snprintf(key,
sizeof(key),
"%04x%04x%04x%04x%04x%04x%04x%04x",
bufKey[0],
bufKey[1],
bufKey[2],
bufKey[3],
bufKey[4],
bufKey[5],
bufKey[6],
bufKey[7]);

std::array<unsigned char, EVP_MAX_MD_SIZE> hash1;
std::array<unsigned char, EVP_MAX_MD_SIZE> hash2;
Comment on lines +63 to +64
Copy link
Member

Choose a reason for hiding this comment

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

why not just unsigned char hash1[EVP_MAX_MD_SIZE] here?

Copy link
Member Author

Choose a reason for hiding this comment

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

Sure, I don't know the difference, so that works for me. 😬

Copy link

Choose a reason for hiding this comment

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

Afaik, C-style array lacks the higher-level functionalities of std::array, hence, they do not track their own size, so you need to manage size information manually. So for this particular case, I think it's better to keep std::array , since we keeps track of arrays own size.

unsigned int hash1Len;
unsigned int hash2Len;

HMAC(EVP_sha256(),
key,
kKeySize,
reinterpret_cast<unsigned char const*>(buf1.data()),
static_cast<int>(buf1.size()),
hash1.data(),
&hash1Len);

HMAC(EVP_sha256(),
key,
kKeySize,
reinterpret_cast<unsigned char const*>(buf2.data()),
static_cast<int>(buf2.size()),
hash2.data(),
&hash2Len);

assert(hash1Len == hash2Len);

return args.GetReturnValue().Set(
CRYPTO_memcmp(buf1.data(), buf2.data(), buf1.size()) == 0);
CRYPTO_memcmp(hash1.data(), hash2.data(), hash1Len) == 0);
}

void Initialize(Environment* env, Local<Object> target) {
Expand Down
3 changes: 3 additions & 0 deletions src/crypto/crypto_timing.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ namespace crypto {
namespace Timing {
void Initialize(Environment* env, v8::Local<v8::Object> target);
void RegisterExternalReferences(ExternalReferenceRegistry* registry);

static const int kKeySize = 256;

} // namespace Timing
} // namespace crypto
} // namespace node
Expand Down