39 length((_header[0] * 256 + _header[1]) * 256 + _header[2]),
40 padding((16 - (length % 16)) % 16),
41 data(_header.cropped(3).toBytes()),
43 protocolId(header[0].
toInt<uint16_t>()),
44 multiFrame(header.itemCount() > 1),
45 sequenceId(multiFrame ? header[1].
toInt<uint16_t>() : 0),
46 totalLength(header.itemCount() == 3 ? header[2].
toInt<uint32_t>() : 0)
60 CryptoPP::CTR_Mode<CryptoPP::AES>::Encryption
frameEnc;
63 CryptoPP::CTR_Mode<CryptoPP::AES>::Encryption
frameDec;
66 CryptoPP::ECB_Mode<CryptoPP::AES>::Encryption
macEnc;
89 setup(_originated, _remoteEphemeral, _remoteNonce, _ecdhe, _nonce, _ackCipher, _authCipher);
94 bytes keyMaterialBytes(64);
95 bytesRef keyMaterial(&keyMaterialBytes);
99 _ecdhe.
agree(_remoteEphemeral, ephemeralShared);
102 h256 const& leftNonce = _originated ? _remoteNonce : _nonce;
103 h256 const& rightNonce = _originated ? _nonce : _remoteNonce;
107 sha3(nonceMaterial.
ref(), outRef);
109 sha3(keyMaterial, outRef);
113 sha3(keyMaterial, outRef);
123 sha3(keyMaterial, outRef);
133 (*(
h256*)outRef.data() ^ _remoteNonce).
ref().copyTo(keyMaterial);
134 bytesConstRef egressCipher = _originated ? _authCipher : _ackCipher;
136 keyMaterial.
retarget(keyMaterialBytes.data(), keyMaterialBytes.size());
141 (*(
h256*)keyMaterial.
data() ^ _remoteNonce ^ _nonce).
ref().copyTo(keyMaterial);
142 bytesConstRef ingressCipher = _originated ? _ackCipher : _authCipher;
144 keyMaterial.
retarget(keyMaterialBytes.data(), keyMaterialBytes.size());
146 m_impl->ingressMac.Update(keyMaterial.
data(), keyMaterial.
size());
152 uint32_t len = (uint32_t)_payload.
size();
161 uint32_t len = (uint32_t)_payload.
size();
163 header.
appendList(2) << _protocolType << _seqId;
170 uint32_t len = (uint32_t)_payload.
size();
172 header.
appendList(3) << _protocolType << _seqId << _totalSize;
181 m_impl->frameEnc.ProcessData(headerWithMac.data(), headerWithMac.data(), 16);
185 auto padding = (16 - (_payload.
size() % 16)) % 16;
186 o_bytes.swap(headerWithMac);
188 bytesRef packetRef(o_bytes.data() + 32, _payload.
size());
189 m_impl->frameEnc.ProcessData(packetRef.data(), _payload.
data(), _payload.
size());
190 bytesRef paddingRef(o_bytes.data() + 32 + _payload.
size(), padding);
192 m_impl->frameEnc.ProcessData(paddingRef.data(), paddingRef.data(), padding);
193 bytesRef packetWithPaddingRef(o_bytes.data() + 32, _payload.
size() + padding);
202 uint32_t len = (uint32_t)_packet.
size();
214 if (*(
h128*)macRef.
data() != expected)
277 h128 prevDigestOut = encDigest;
281 macEnc.ProcessData(encDigest.
data(), encDigest.
data(), 16);
286 encDigest ^= *(
h128*)prevDigestOut.
data();
bytes m_ackCipher
Ciphertext of egress or ingress Ack message.
Adapted from code found on http://stackoverflow.com/questions/180947/base64-decode-snippet-in-c Origi...
vector_ref< _T const > ref(_T const &_t)
void updateEgressMACWithHeader(bytesConstRef _headerCipher)
Update state of egress MAC with frame header.
CryptoPP::SecByteBlock frameDecKey
Key for m_frameDec.
Class file for modes of operation.
void updateIngressMACWithFrame(bytesConstRef _cipher)
Update state of ingress MAC with frame.
h256 m_nonce
Nonce generated by this host for handshake.
bytes const & out() const
Read the byte stream.
Classes for Keccak message digests.
bool authAndDecryptFrame(bytesRef io_cipherWithMac)
Authenticate and decrypt frame in-place.
std::hash for asio::adress
void writeFrame(uint16_t _protocolType, bytesConstRef _payload, bytes &o_bytes)
Write single-frame payload of packet(s).
vector_ref< _T > cropped(size_t _begin, size_t _count) const
void agree(Public const &_remoteEphemeral, Secret &o_sharedSecret) const
Input public key for dh agreement, output generated shared secret.
std::unique_ptr< class RLPXFrameCoderImpl > m_impl
CryptoPP::ECB_Mode< CryptoPP::AES >::Encryption macEnc
Key for m_macEnd.
CryptoPP::Keccak_256 ingressMac
State of MAC for ingress ciphertext.
bytes m_authCipher
Ciphertext of egress or ingress Auth message.
void retarget(_T *_d, size_t _s)
crypto::ECDHE m_ecdhe
Ephemeral ECDH secret and agreement.
bytesConstRef ref() const
vector_ref< byte > bytesRef
std::lock_guard< std::mutex > Guard
bool authAndDecryptHeader(bytesRef io_cipherWithMac)
Authenticate and decrypt header in-place.
Class file for the AES cipher (Rijndael)
SecBlock< byte > SecByteBlock
CryptoPP::CTR_Mode< CryptoPP::AES >::Encryption frameEnc
Encoder for egress plaintext.
std::vector< byte > bytes
vector_ref< byte const > bytesConstRef
Public m_remoteEphemeral
Remote ephemeral public key.
RLPStream & appendList(size_t _items)
Appends a list.
CryptoPP::Keccak_256 egressMac
One-way coder used by updateMAC for ingress and egress MAC updates.
Keccak_Final< 32 > Keccak_256
void updateEgressMACWithFrame(bytesConstRef _cipher)
Update state of egress MAC with frame.
void updateMAC(CryptoPP::Keccak_256 &_mac, bytesConstRef _seed={})
Update state of _mac.
CryptoPP::CTR_Mode< CryptoPP::AES >::Encryption frameDec
Decoder for egress plaintext.
bool m_originated
True if connection is outbound.
void updateIngressMACWithHeader(bytesConstRef _headerCipher)
Update state of ingress MAC with frame header.
void * memcpy(void *a, const void *b, size_t c)
void copyTo(vector_ref< typename std::remove_const< _T >::type > _t) const
Copies the contents of this vector_ref to the contents of _t, up to the max size of _t...
bool sha3(bytesConstRef _input, bytesRef o_output)
Calculate SHA3-256 hash of the given input and load it into the given output.
void setup(bool _originated, h512 const &_remoteEphemeral, h256 const &_remoteNonce, crypto::ECDHE const &_ephemeral, h256 const &_nonce, bytesConstRef _ackCipher, bytesConstRef _authCipher)
Establish shared secrets and setup AES and MAC states.
void writeSingleFramePacket(bytesConstRef _packet, bytes &o_bytes)
Legacy. Encrypt _packet as ill-defined legacy RLPx frame.
Keccak-256 message digest.
h128 ingressDigest()
Return first 16 bytes of current digest from ingress mac.
h256 m_remoteNonce
Nonce generated by remote host for handshake.
h128 egressDigest()
Return first 16 bytes of current digest from egress mac.
CryptoPP::SecByteBlock frameEncKey
Key for m_frameEnc.
CryptoPP::SecByteBlock macEncKey
RLPXFrameCoder(RLPXHandshake const &_init)
Construct; requires instance of RLPXHandshake which has encrypted ECDH key exchange (first two phases...
Class for writing to an RLP bytestream.
Derive DH shared secret from EC keypairs.
RLPStream & appendRaw(bytesConstRef _rlp, size_t _itemCount=1)
Appends raw (pre-serialised) RLP data. Use with caution.
Class for interpreting Recursive Linear-Prefix Data.
u256 toInt(json_spirit::mValue const &_v)
Setup inbound or outbound connection for communication over RLPXFrameCoder.