Skip to content
This repository has been archived by the owner on Feb 8, 2018. It is now read-only.

libp2p: EIP-8 changes #46

Merged
merged 5 commits into from
Feb 17, 2016
Merged
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
14 changes: 12 additions & 2 deletions libdevcrypto/Common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,16 +124,26 @@ bool dev::decrypt(Secret const& _k, bytesConstRef _cipher, bytes& o_plaintext)
}

void dev::encryptECIES(Public const& _k, bytesConstRef _plain, bytes& o_cipher)
{
encryptECIES(_k, bytesConstRef(), _plain, o_cipher);
}

void dev::encryptECIES(Public const& _k, bytesConstRef _sharedMacData, bytesConstRef _plain, bytes& o_cipher)
{
bytes io = _plain.toBytes();
Secp256k1PP::get()->encryptECIES(_k, io);
Secp256k1PP::get()->encryptECIES(_k, _sharedMacData, io);
o_cipher = std::move(io);
}

bool dev::decryptECIES(Secret const& _k, bytesConstRef _cipher, bytes& o_plaintext)
{
return decryptECIES(_k, bytesConstRef(), _cipher, o_plaintext);
}

bool dev::decryptECIES(Secret const& _k, bytesConstRef _sharedMacData, bytesConstRef _cipher, bytes& o_plaintext)
{
bytes io = _cipher.toBytes();
if (!Secp256k1PP::get()->decryptECIES(_k, io))
if (!Secp256k1PP::get()->decryptECIES(_k, _sharedMacData, io))
return false;
o_plaintext = std::move(io);
return true;
Expand Down
8 changes: 8 additions & 0 deletions libdevcrypto/Common.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,17 @@ bool decryptSym(Secret const& _k, bytesConstRef _cipher, bytes& o_plaintext);
/// Encrypt payload using ECIES standard with AES128-CTR.
void encryptECIES(Public const& _k, bytesConstRef _plain, bytes& o_cipher);

/// Encrypt payload using ECIES standard with AES128-CTR.
/// @a _sharedMacData is shared authenticated data.
void encryptECIES(Public const& _k, bytesConstRef _sharedMacData, bytesConstRef _plain, bytes& o_cipher);

/// Decrypt payload using ECIES standard with AES128-CTR.
bool decryptECIES(Secret const& _k, bytesConstRef _cipher, bytes& o_plaintext);

/// Decrypt payload using ECIES standard with AES128-CTR.
/// @a _sharedMacData is shared authenticated data.
bool decryptECIES(Secret const& _k, bytesConstRef _sharedMacData, bytesConstRef _cipher, bytes& o_plaintext);

/// Encrypts payload with random IV/ctr using AES128-CTR.
std::pair<bytes, h128> encryptSymNoAuth(SecureFixedHash<16> const& _k, bytesConstRef _plain);

Expand Down
13 changes: 13 additions & 0 deletions libdevcrypto/CryptoPP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ bytes Secp256k1PP::eciesKDF(Secret const& _z, bytes _s1, unsigned kdByteLen)
}

void Secp256k1PP::encryptECIES(Public const& _k, bytes& io_cipher)
{
encryptECIES(_k, bytesConstRef(), io_cipher);
}

void Secp256k1PP::encryptECIES(Public const& _k, bytesConstRef _sharedMacData, bytes& io_cipher)
{
// interop w/go ecies implementation
auto r = KeyPair::create();
Expand Down Expand Up @@ -93,6 +98,7 @@ void Secp256k1PP::encryptECIES(Public const& _k, bytes& io_cipher)
CryptoPP::HMAC<SHA256> hmacctx(mKey.data(), mKey.size());
bytesConstRef cipherWithIV = bytesRef(&msg).cropped(1 + Public::size, h128::size + cipherText.size());
hmacctx.Update(cipherWithIV.data(), cipherWithIV.size());
hmacctx.Update(_sharedMacData.data(), _sharedMacData.size());
hmacctx.Final(msg.data() + 1 + Public::size + cipherWithIV.size());

io_cipher.resize(msg.size());
Expand All @@ -101,6 +107,12 @@ void Secp256k1PP::encryptECIES(Public const& _k, bytes& io_cipher)

bool Secp256k1PP::decryptECIES(Secret const& _k, bytes& io_text)
{
return decryptECIES(_k, bytesConstRef(), io_text);
}

bool Secp256k1PP::decryptECIES(Secret const& _k, bytesConstRef _sharedMacData, bytes& io_text)
{

// interop w/go ecies implementation

// io_cipher[0] must be 2, 3, or 4, else invalidpublickey
Expand Down Expand Up @@ -133,6 +145,7 @@ bool Secp256k1PP::decryptECIES(Secret const& _k, bytes& io_text)
// verify tag
CryptoPP::HMAC<SHA256> hmacctx(mKey.data(), mKey.size());
hmacctx.Update(cipherWithIV.data(), cipherWithIV.size());
hmacctx.Update(_sharedMacData.data(), _sharedMacData.size());
h256 mac;
hmacctx.Final(mac.data());
for (unsigned i = 0; i < h256::size; i++)
Expand Down
11 changes: 10 additions & 1 deletion libdevcrypto/CryptoPP.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ inline ECP::Point publicToPoint(Public const& _p) { Integer x(_p.data(), 32); In

inline Integer secretToExponent(Secret const& _s) { return std::move(Integer(_s.data(), Secret::size)); }

/// Amount of bytes added when encrypting with encryptECIES.
static const unsigned c_eciesOverhead = 113;

/**
* CryptoPP secp256k1 algorithms.
* @todo Collect ECIES methods into class.
Expand All @@ -82,10 +85,16 @@ class Secp256k1PP

/// Encrypts text (replace input). (ECIES w/AES128-CTR-SHA256)
void encryptECIES(Public const& _k, bytes& io_cipher);


/// Encrypts text (replace input). (ECIES w/AES128-CTR-SHA256)
void encryptECIES(Public const& _k, bytesConstRef _sharedMacData, bytes& io_cipher);

/// Decrypts text (replace input). (ECIES w/AES128-CTR-SHA256)
bool decryptECIES(Secret const& _k, bytes& io_text);

/// Decrypts text (replace input). (ECIES w/AES128-CTR-SHA256)
bool decryptECIES(Secret const& _k, bytesConstRef _sharedMacData, bytes& io_text);

/// Key derivation function used by encryptECIES and decryptECIES.
bytes eciesKDF(Secret const& _z, bytes _s1, unsigned kdBitLen = 256);

Expand Down
7 changes: 7 additions & 0 deletions libp2p/Common.h
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,13 @@ class NodeIPEndpoint
operator bool() const { return !address.is_unspecified() && udpPort > 0 && tcpPort > 0; }

bool isAllowed() const { return NodeIPEndpoint::test_allowLocal ? !address.is_unspecified() : isPublicAddress(address); }

bool operator==(NodeIPEndpoint const& _cmp) const {
return address == _cmp.address && udpPort == _cmp.udpPort && tcpPort == _cmp.tcpPort;
}
bool operator!=(NodeIPEndpoint const& _cmp) const {
return !operator==(_cmp);
}

void streamRLP(RLPStream& _s, RLPAppend _append = StreamList) const;
void interpretRLP(RLP const& _r);
Expand Down
11 changes: 8 additions & 3 deletions libp2p/Host.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,20 +98,25 @@ bytes ReputationManager::data(Session const& _s, std::string const& _sub) const
return bytes();
}

Host::Host(std::string const& _clientVersion, NetworkPreferences const& _n, bytesConstRef _restoreNetwork):
Host::Host(string const& _clientVersion, KeyPair const& _alias, NetworkPreferences const& _n):
Worker("p2p", 0),
m_restoreNetwork(_restoreNetwork.toBytes()),
m_clientVersion(_clientVersion),
m_netPrefs(_n),
m_ifAddresses(Network::getInterfaceAddresses()),
m_ioService(2),
m_tcp4Acceptor(m_ioService),
m_alias(networkAlias(_restoreNetwork)),
m_alias(_alias),
m_lastPing(chrono::steady_clock::time_point::min())
{
clog(NetNote) << "Id:" << id();
}

Host::Host(string const& _clientVersion, NetworkPreferences const& _n, bytesConstRef _restoreNetwork):
Host(_clientVersion, networkAlias(_restoreNetwork), _n)
{
m_restoreNetwork = _restoreNetwork.toBytes();
}

Host::~Host()
{
stop();
Expand Down
8 changes: 8 additions & 0 deletions libp2p/Host.h
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,14 @@ class Host: public Worker
bytesConstRef _restoreNetwork = bytesConstRef()
);

/// Alternative constructor that allows providing the node key directly
/// without restoring the network.
Host(
std::string const& _clientVersion,
KeyPair const& _alias,
NetworkPreferences const& _n = NetworkPreferences()
);

/// Will block on network process events.
virtual ~Host();

Expand Down
Loading