Browse Source

fix public import/export

cl-refactor
subtly 10 years ago
parent
commit
90641948e5
  1. 26
      libdevcrypto/CryptoPP.cpp
  2. 4
      libdevcrypto/CryptoPP.h
  3. 19
      libdevcrypto/EC.cpp
  4. 6
      libdevcrypto/EC.h
  5. 16
      test/crypto.cpp

26
libdevcrypto/CryptoPP.cpp

@ -29,9 +29,9 @@ using namespace pp;
using namespace CryptoPP;
void pp::exportDL_PublicKey_EC(CryptoPP::DL_PublicKey_EC<CryptoPP::ECP> const& _k, Public& _p) {
ECP::Point q(_k.GetPublicElement());
q.x.Encode(_p.data(), 32);
q.y.Encode(&_p.data()[32], 32);
bytes prefixedKey(65);
_k.GetGroupParameters().GetCurve().EncodePoint(prefixedKey.data(), _k.GetPublicElement(), false);
memcpy(_p.data(), &prefixedKey[1], 64);
}
void pp::exportDL_PrivateKey_EC(CryptoPP::DL_PrivateKey_EC<CryptoPP::ECP> const& _k, Secret& _s) {
@ -40,9 +40,23 @@ void pp::exportDL_PrivateKey_EC(CryptoPP::DL_PrivateKey_EC<CryptoPP::ECP> const&
ECP::Point pp::PointFromPublic(Public const& _p)
{
Integer x(&_p.data()[0], 32);
Integer y(&_p.data()[32], 32);
return std::move(ECP::Point(x,y));
bytes prefixedKey(65);
prefixedKey[0] = 0x04;
memcpy(&prefixedKey[1], _p.data(), 64);
ECP::Point p;
CryptoPP::DL_PublicKey_EC<CryptoPP::ECP> pub;
pub.AccessGroupParameters().Initialize(pp::secp256k1());
pub.GetGroupParameters().GetCurve().DecodePoint(p, prefixedKey.data(), 65);
// Manually:
// Integer x(_p.data(), 32);
// Integer y(&_p.data()[31], 32); // unsure why offset must be 31
// ECP::Point p(x,y);
// ECP ecp;
// ecp.DecodePoint(p, _p.data(), _p.size);
return std::move(p);
}
Integer pp::ExponentFromSecret(Secret const& _s)

4
libdevcrypto/CryptoPP.h

@ -47,9 +47,9 @@ void exportDL_PrivateKey_EC(CryptoPP::DL_PrivateKey_EC<CryptoPP::ECP> const& _k,
CryptoPP::ECP::Point PointFromPublic(Public const& _p);
CryptoPP::Integer ExponentFromSecret(Secret const& _s);
void ECIESEncrypt(CryptoPP::ECP::Point const& _point);
void ECIESEncrypt(CryptoPP::ECP::Point const& _point, byte*);
void ECIESDecrypt(CryptoPP::Integer const& _exponent);
void ECIESDecrypt(CryptoPP::Integer const& _exponent, byte*);
/**
* @brief CryptoPP-specific EC keypair

19
libdevcrypto/EC.cpp

@ -44,15 +44,9 @@ using namespace CryptoPP;
void dev::crypto::encrypt(Public const& _key, bytes& _plain)
{
Integer x(&_key.data()[0], 32);
Integer y(&_key.data()[32], 32);
// DL_PublicKey_EC<ECP> p;
// p.Initialize(pp::secp256k1(), ECP::Point(x,y));
ECIES<ECP>::Encryptor e;
e.AccessKey().AccessGroupParameters().Initialize(pp::secp256k1());
e.AccessKey().SetPublicElement(ECP::Point(x,y));
e.AccessKey().SetPublicElement(pp::PointFromPublic(_key));
size_t plen = _plain.size();
_plain.resize(e.CiphertextLength(plen));
e.Encrypt(pp::PRNG(), _plain.data(), plen, _plain.data());
@ -96,11 +90,14 @@ void ECKeyPair::encrypt(bytes& _text)
_text = std::move(asBytes(c));
}
dev::bytes ECKeyPair::decrypt(bytesConstRef _c)
void ECKeyPair::decrypt(bytes& _c)
{
std::string p;
StringSource ss(_c.data(), _c.size(), true, new PK_DecryptorFilter(pp::PRNG(), m_decryptor, new StringSink(p)));
return std::move(asBytes(p));
DecodingResult r = m_decryptor.Decrypt(pp::PRNG(), _c.data(), _c.size(), _c.data());
_c.resize(r.messageLength);
// std::string p;
// StringSource ss(_c.data(), _c.size(), true, new PK_DecryptorFilter(pp::PRNG(), m_decryptor, new StringSink(p)));
// return std::move(asBytes(p));
}

6
libdevcrypto/EC.h

@ -72,10 +72,10 @@ public:
/// Sign message.
Signature sign(h256 _messageHash);
/// Decrypt ciphertext.
bytes decrypt(bytesConstRef _cipher);
/// Decrypt ciphertext (in place).
void decrypt(bytes& _cipher);
/// Encrypt using our own public key.
/// Encrypt using public key (in place).
void encrypt(bytes& _text);
private:

16
test/crypto.cpp

@ -51,15 +51,10 @@ BOOST_AUTO_TEST_CASE(cryptopp_public_export_import)
Public p;
pp::exportDL_PublicKey_EC(e.GetKey(), p);
Integer x(&p[0], 32);
Integer y(&p[32], 32);
DL_PublicKey_EC<ECP> pub;
pub.Initialize(pp::secp256k1(), ECP::Point(x,y));
assert(pub == e.GetKey());
DL_PublicKey_EC<ECP> pub2;
pub.Initialize(pp::secp256k1(), ECP::Point(x,y));
pub.Initialize(pp::secp256k1(), pp::PointFromPublic(p));
assert(pub.GetPublicElement() == e.GetKey().GetPublicElement());
}
BOOST_AUTO_TEST_CASE(ecies_eckeypair)
@ -78,9 +73,10 @@ BOOST_AUTO_TEST_CASE(ecies_eckeypair)
// Fix Me!
// encrypt(k.publicKey(), b);
// assert(b != asBytes(original));
// bytes plain = k.decrypt(&b);
// assert(plain == asBytes(original));
k.encrypt(b);
assert(b != asBytes(original));
k.decrypt(b);
assert(b == asBytes(original));
}
BOOST_AUTO_TEST_CASE(ecdhe_aes128_ctr_sha3mac)

Loading…
Cancel
Save