Skip to content

Commit 0f5f813

Browse files
committed
Fix hmac sha256 crypto and test.
1 parent de14dd1 commit 0f5f813

File tree

3 files changed

+67
-5
lines changed

3 files changed

+67
-5
lines changed

packages/crypto/include/crypto/Crypto.h

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <string>
44

55
#include "logging/LoggingAll.h"
6+
#include "serialization/Base16.h"
67
#include "serialization/Base64.h"
78

89
#include "cryptopp/xed25519.h"
@@ -18,8 +19,11 @@ namespace l::crypto {
1819
CryptoSigner() = default;
1920
virtual ~CryptoSigner() = default;
2021

22+
virtual std::string_view GetAssociatedKey() = 0;
2123
virtual void AccumulateMessage(std::string_view message) = 0;
2224
virtual std::string SignMessageB64() = 0;
25+
virtual std::string SignMessageHex() = 0;
26+
2327
};
2428

2529
std::string ToPublicKeyFormat(std::string_view pkcsFormat);
@@ -30,14 +34,19 @@ namespace l::crypto {
3034
CryptoHMacSha256() = default;
3135
~CryptoHMacSha256() = default;
3236

37+
void SetAssociatedKey(std::string_view associatedKey) {
38+
mAssociatedKey = associatedKey;
39+
}
40+
3341
bool LoadSecretKeyAscii(std::string_view privateKeyAscii) {
34-
CryptoPP::SHA256 sha256;
3542
auto keyBytes = reinterpret_cast<const CryptoPP::byte*>(privateKeyAscii.data());
36-
sha256.CalculateDigest(mSecret, keyBytes, privateKeyAscii.size());
37-
mHmac.SetKey(mSecret, 32);
43+
mHmac.SetKey(keyBytes, privateKeyAscii.size());
3844
return true;
3945
}
4046

47+
std::string_view GetAssociatedKey() override {
48+
return mAssociatedKey;
49+
}
4150
virtual void AccumulateMessage(std::string_view message) override {
4251
auto p = reinterpret_cast<const CryptoPP::byte*>(message.data());
4352
mHmac.Update(p, message.size());
@@ -48,6 +57,11 @@ namespace l::crypto {
4857
return l::serialization::base64_encode(mSignature, 32);
4958
}
5059

60+
virtual std::string SignMessageHex() override {
61+
SignMessage();
62+
return l::serialization::base16_encode(mSignature, 32);
63+
}
64+
5165
protected:
5266
void SignMessage() {
5367
mHmac.Final(mSignature);
@@ -56,6 +70,8 @@ namespace l::crypto {
5670
CryptoPP::HMAC<CryptoPP::SHA256> mHmac;
5771
CryptoPP::byte mSecret[32];
5872
CryptoPP::byte mSignature[32];
73+
74+
std::string mAssociatedKey;
5975
};
6076

6177
class CryptoXED25519 : public CryptoSigner {
@@ -65,17 +81,24 @@ namespace l::crypto {
6581

6682
void CreateNewKeys();
6783

84+
void SetAssociatedKey(std::string_view associatedKey) {
85+
mAssociatedKey = associatedKey;
86+
}
87+
6888
bool LoadPrivateKeyB64(std::string_view privateKeyB64);
6989
bool LoadPrivateKeyHex(std::string_view privateKeyHex);
7090
bool LoadPublicKeyB64(std::string_view publicKeyB64);
7191
bool LoadPublicKeyHex(std::string_view publicKeyHex);
7292

7393
CryptoPP::byte* SignMessage();
74-
std::string SignMessageHex();
7594
std::string SignMessagePem();
7695

7796
virtual void AccumulateMessage(std::string_view message) override;
7897
virtual std::string SignMessageB64() override;
98+
virtual std::string SignMessageHex() override;
99+
std::string_view GetAssociatedKey() override {
100+
return "";
101+
}
79102

80103
bool VerifyB64(std::string_view signatureB64);
81104
bool VerifyHex(std::string_view signatureHex);
@@ -114,6 +137,8 @@ namespace l::crypto {
114137
CryptoPP::byte mPrivateKey[32];
115138
CryptoPP::byte mPublicKey[32];
116139
CryptoPP::byte mSignature[64];
140+
141+
std::string mAssociatedKey;
117142
};
118143

119144

packages/crypto/tests/common/CryptoppTest.cpp

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@
1414

1515
using namespace l;
1616

17+
1718
TEST(CryptoPP, hmacsha256) {
1819

1920
CryptoPP::HMAC<CryptoPP::SHA256> mHmac;
2021
CryptoPP::byte mSecret[32];
2122
CryptoPP::byte mSignature[32];
2223
CryptoPP::SHA256 sha256;
2324

24-
2525
std::string_view privateKeyAscii = "Ab0z9aZvAb0z9aZvAb0z9aZvAb0z9aZvAb0z9aZvAb0z9aZvAb0z9aZvAb0z9aZv";
2626
std::string_view message = "test";
2727

@@ -40,6 +40,36 @@ TEST(CryptoPP, hmacsha256) {
4040
return 0;
4141
}
4242

43+
TEST(CryptoPP, verifydigest) {
44+
45+
CryptoPP::HMAC<CryptoPP::SHA256> mHmac;
46+
CryptoPP::byte mSecret[32];
47+
CryptoPP::byte mSignature[32];
48+
49+
std::string_view privateKeyAscii = "NhqPtmdSJYdKjVHjA7PZj4Mge3R5YNiP1e3UZjInClVN65XAbvqqM6A7H5fATj0j";
50+
std::string_view message = "apiKey=test";
51+
52+
auto keyBytes = reinterpret_cast<const CryptoPP::byte*>(privateKeyAscii.data());
53+
mHmac.SetKey(keyBytes, privateKeyAscii.size());
54+
const CryptoPP::byte* p = reinterpret_cast<const CryptoPP::byte*>(message.data());
55+
mHmac.Update(p, message.size());
56+
mHmac.Final(mSignature);
57+
auto sign = l::serialization::base16_encode(mSignature, 32);
58+
LOG(LogTest) << sign;
59+
60+
/*
61+
echo -n 'apiKey=test' | openssl dgst -hex -sha256 -hmac 'NhqPtmdSJYdKjVHjA7PZj4Mge3R5YNiP1e3UZjInClVN65XAbvqqM6A7H5fATj0j'
62+
> SHA2-256(stdin)= ffc10e6024b05c27e03e7515b976ac1a0f73cb6797fed05cd1e5f01a00a84e45
63+
*/
64+
auto correctSign = "ffc10e6024b05c27e03e7515b976ac1a0f73cb6797fed05cd1e5f01a00a84e45";
65+
TEST_TRUE(sign == correctSign, "");
66+
67+
auto verify = mHmac.VerifyDigest(mSignature, p, message.size());
68+
TEST_TRUE(verify, "");
69+
70+
return 0;
71+
}
72+
4373
#ifdef CRYPTOPP_TEST_1
4474
TEST(Cryptopp, printPemKeys) {
4575
auto messageHex = "ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f";

packages/network/source/common/NetworkConnection.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,9 @@ namespace l::network {
244244
if (res == CURLE_OK) {
245245
return static_cast<int32_t>(sentBytes);
246246
}
247+
else if (res == CURLE_GOT_NOTHING) {
248+
mWebSocketReceivingData = false;
249+
}
247250
LOG(LogError) << "Failed wss write, error: " << res;
248251
return -res;
249252
}
@@ -266,6 +269,10 @@ namespace l::network {
266269
}
267270
return static_cast<int32_t>(readBytes);
268271
}
272+
else if (res == CURLE_GOT_NOTHING) {
273+
mWebSocketReceivingData = false;
274+
LOG(LogError) << "Failed wss read, connection closed, error: " << res;
275+
}
269276
//LOG(LogError) << "Failed wss read, error: " << res;
270277
return -res;
271278
}

0 commit comments

Comments
 (0)