Browse Source

bound assertions.

cl-refactor
subtly 10 years ago
parent
commit
c85d64a331
  1. 5
      libdevcrypto/Common.cpp
  2. 22
      libdevcrypto/CryptoPP.cpp
  3. 16
      libdevcrypto/EC.cpp

5
libdevcrypto/Common.cpp

@ -120,10 +120,9 @@ bool dev::decrypt(Secret _k, bytesConstRef _cipher, bytes& _plain)
{
bytes io = _cipher.toBytes();
crypto::decrypt(_k, io);
if (io.size())
_plain = std::move(io);
else
if (io.empty())
return false;
_plain = std::move(io);
return true;
}

22
libdevcrypto/CryptoPP.cpp

@ -23,19 +23,20 @@
using namespace dev;
using namespace dev::crypto;
using namespace pp;
using namespace CryptoPP;
ECP::Point pp::PointFromPublic(Public const& _p)
{
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);
bytes prefixedKey(pub.GetGroupParameters().GetEncodedElementSize(true));
prefixedKey[0] = 0x04;
assert(Public::size == prefixedKey.size() - 1);
memcpy(&prefixedKey[1], _p.data(), prefixedKey.size() - 1);
pub.GetGroupParameters().GetCurve().DecodePoint(p, prefixedKey.data(), prefixedKey.size());
return std::move(p);
}
@ -58,12 +59,15 @@ void pp::PublicFromExponent(Integer const& _e, Public& _p)
void pp::PublicFromDL_PublicKey_EC(CryptoPP::DL_PublicKey_EC<CryptoPP::ECP> const& _k, Public& _p)
{
bytes prefixedKey(65);
bytes prefixedKey(_k.GetGroupParameters().GetEncodedElementSize(true));
_k.GetGroupParameters().GetCurve().EncodePoint(prefixedKey.data(), _k.GetPublicElement(), false);
memcpy(_p.data(), &prefixedKey[1], 64);
static_assert(Public::size == 64, "Public key must be 64 bytes.");
assert(Public::size + 1 == _k.GetGroupParameters().GetEncodedElementSize(true));
memcpy(_p.data(), &prefixedKey[1], Public::size);
}
void pp::SecretFromDL_PrivateKey_EC(CryptoPP::DL_PrivateKey_EC<CryptoPP::ECP> const& _k, Secret& _s)
{
_k.GetPrivateExponent().Encode(_s.data(), 32);
_k.GetPrivateExponent().Encode(_s.data(), Secret::size);
}

16
libdevcrypto/EC.cpp

@ -48,11 +48,12 @@ void dev::crypto::encrypt(Public const& _key, bytes& _plain)
e.AccessKey().AccessGroupParameters().Initialize(pp::secp256k1());
e.AccessKey().SetPublicElement(pp::PointFromPublic(_key));
size_t plen = _plain.size();
std::string c;
bytes c;
c.resize(e.CiphertextLength(plen));
e.Encrypt(pp::PRNG(), _plain.data(), plen, (byte*)c.data());
_plain.resize(c.size());
memcpy(_plain.data(), c.data(), c.size());
// todo: use StringSource with _plain as input and output.
e.Encrypt(pp::PRNG(), _plain.data(), plen, c.data());
bzero(_plain.data(), _plain.size());
_plain = std::move(c);
}
void dev::crypto::decrypt(Secret const& _k, bytes& _c)
@ -61,11 +62,12 @@ void dev::crypto::decrypt(Secret const& _k, bytes& _c)
d.AccessKey().AccessGroupParameters().Initialize(pp::secp256k1());
d.AccessKey().SetPrivateExponent(pp::ExponentFromSecret(_k));
size_t clen = _c.size();
std::string p;
bytes p;
p.resize(d.MaxPlaintextLength(_c.size()));
DecodingResult r = d.Decrypt(pp::PRNG(), _c.data(), clen, (byte*)p.data());
// todo: use StringSource with _c as input and output.
DecodingResult r = d.Decrypt(pp::PRNG(), _c.data(), clen, p.data());
assert(r.messageLength);
_c.resize(r.messageLength);
memcpy(_c.data(), p.data(), _c.size());
_c = std::move(p);
}

Loading…
Cancel
Save