Skip to content
This repository was archived by the owner on Oct 28, 2021. It is now read-only.

Commit c93edbb

Browse files
committed
Use SHA256 from secp256k1 in encryptECIES()
1 parent 65ae98e commit c93edbb

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

libdevcrypto/CryptoPP.cpp

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,11 @@ void Secp256k1PP::encryptECIES(Public const& _k, bytesConstRef _sharedMacData, b
9797
auto key = ecies::kdf(z, bytes(), 32);
9898
bytesConstRef eKey = bytesConstRef(&key).cropped(0, 16);
9999
bytesRef mKeyMaterial = bytesRef(&key).cropped(16, 16);
100-
CryptoPP::SHA256 ctx;
101-
ctx.Update(mKeyMaterial.data(), mKeyMaterial.size());
100+
secp256k1_sha256_t ctx;
101+
secp256k1_sha256_initialize(&ctx);
102+
secp256k1_sha256_write(&ctx, mKeyMaterial.data(), mKeyMaterial.size());
102103
bytes mKey(32);
103-
ctx.Final(mKey.data());
104+
secp256k1_sha256_finalize(&ctx, mKey.data());
104105

105106
auto iv = h128::random();
106107
bytes cipherText = encryptSymNoAuth(SecureFixedHash<16>(eKey), iv, bytesConstRef(&io_cipher));
@@ -113,14 +114,15 @@ void Secp256k1PP::encryptECIES(Public const& _k, bytesConstRef _sharedMacData, b
113114
iv.ref().copyTo(bytesRef(&msg).cropped(1 + Public::size, h128::size));
114115
bytesRef msgCipherRef = bytesRef(&msg).cropped(1 + Public::size + h128::size, cipherText.size());
115116
bytesConstRef(&cipherText).copyTo(msgCipherRef);
116-
117+
117118
// tag message
118-
CryptoPP::HMAC<SHA256> hmacctx(mKey.data(), mKey.size());
119+
secp256k1_hmac_sha256_t hmacCtx;
120+
secp256k1_hmac_sha256_initialize(&hmacCtx, mKey.data(), mKey.size());
119121
bytesConstRef cipherWithIV = bytesRef(&msg).cropped(1 + Public::size, h128::size + cipherText.size());
120-
hmacctx.Update(cipherWithIV.data(), cipherWithIV.size());
121-
hmacctx.Update(_sharedMacData.data(), _sharedMacData.size());
122-
hmacctx.Final(msg.data() + 1 + Public::size + cipherWithIV.size());
123-
122+
secp256k1_hmac_sha256_write(&hmacCtx, cipherWithIV.data(), cipherWithIV.size());
123+
secp256k1_hmac_sha256_write(&hmacCtx, _sharedMacData.data(), _sharedMacData.size());
124+
secp256k1_hmac_sha256_finalize(&hmacCtx, msg.data() + 1 + Public::size + cipherWithIV.size());
125+
124126
io_cipher.resize(msg.size());
125127
io_cipher.swap(msg);
126128
}

0 commit comments

Comments
 (0)