You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
102 lines
3.1 KiB
102 lines
3.1 KiB
10 years ago
|
/*
|
||
|
This file is part of cpp-ethereum.
|
||
|
|
||
|
cpp-ethereum is free software: you can redistribute it and/or modify
|
||
|
it under the terms of the GNU General Public License as published by
|
||
|
the Free Software Foundation, either version 3 of the License, or
|
||
|
(at your option) any later version.
|
||
|
|
||
|
cpp-ethereum is distributed in the hope that it will be useful,
|
||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
|
GNU General Public License for more details.
|
||
|
|
||
|
You should have received a copy of the GNU General Public License
|
||
|
along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
|
||
|
*/
|
||
10 years ago
|
/** @file RLPXFrameCoder.h
|
||
10 years ago
|
* @author Alex Leverington <nessence@gmail.com>
|
||
|
* @date 2015
|
||
|
*/
|
||
|
|
||
|
|
||
|
#pragma once
|
||
|
|
||
|
#include <memory>
|
||
10 years ago
|
#include <libdevcore/Guards.h>
|
||
10 years ago
|
#include <libdevcrypto/ECDHE.h>
|
||
|
#include <libdevcrypto/CryptoPP.h>
|
||
|
#include "Common.h"
|
||
|
|
||
|
namespace dev
|
||
|
{
|
||
|
namespace p2p
|
||
|
{
|
||
|
|
||
|
class RLPXHandshake;
|
||
|
|
||
10 years ago
|
/**
|
||
10 years ago
|
* @brief Encoder/decoder transport for RLPx connection established by RLPXHandshake.
|
||
10 years ago
|
*
|
||
|
* Thread Safety
|
||
|
* Distinct Objects: Safe.
|
||
|
* Shared objects: Unsafe.
|
||
|
*/
|
||
10 years ago
|
class RLPXFrameCoder
|
||
10 years ago
|
{
|
||
10 years ago
|
friend class RLPXFrameIOMux;
|
||
10 years ago
|
friend class Session;
|
||
|
public:
|
||
10 years ago
|
/// Constructor.
|
||
|
/// Requires instance of RLPXHandshake which has completed first two phases of handshake.
|
||
10 years ago
|
RLPXFrameCoder(RLPXHandshake const& _init);
|
||
|
~RLPXFrameCoder() {}
|
||
10 years ago
|
|
||
10 years ago
|
/// Encrypt _packet as RLPx frame.
|
||
10 years ago
|
void writeSingleFramePacket(bytesConstRef _packet, bytes& o_bytes);
|
||
|
|
||
10 years ago
|
/// Authenticate and decrypt header in-place.
|
||
10 years ago
|
bool authAndDecryptHeader(bytesRef io_cipherWithMac);
|
||
10 years ago
|
|
||
10 years ago
|
/// Authenticate and decrypt frame in-place.
|
||
10 years ago
|
bool authAndDecryptFrame(bytesRef io_cipherWithMac);
|
||
|
|
||
10 years ago
|
/// Return first 16 bytes of current digest from egress mac.
|
||
10 years ago
|
h128 egressDigest();
|
||
10 years ago
|
|
||
|
/// Return first 16 bytes of current digest from ingress mac.
|
||
10 years ago
|
h128 ingressDigest();
|
||
10 years ago
|
|
||
10 years ago
|
protected:
|
||
|
/// Update state of egress MAC with frame header.
|
||
10 years ago
|
void updateEgressMACWithHeader(bytesConstRef _headerCipher);
|
||
10 years ago
|
|
||
|
/// Update state of egress MAC with frame.
|
||
|
void updateEgressMACWithFrame(bytesConstRef _cipher);
|
||
10 years ago
|
|
||
10 years ago
|
/// Update state of ingress MAC with frame header.
|
||
10 years ago
|
void updateIngressMACWithHeader(bytesConstRef _headerCipher);
|
||
|
|
||
10 years ago
|
/// Update state of ingress MAC with frame.
|
||
|
void updateIngressMACWithFrame(bytesConstRef _cipher);
|
||
10 years ago
|
|
||
10 years ago
|
private:
|
||
10 years ago
|
/// Update state of _mac.
|
||
10 years ago
|
void updateMAC(CryptoPP::SHA3_256& _mac, bytesConstRef _seed = bytesConstRef());
|
||
10 years ago
|
|
||
10 years ago
|
CryptoPP::SecByteBlock m_frameEncKey; ///< Key for m_frameEnc
|
||
|
CryptoPP::CTR_Mode<CryptoPP::AES>::Encryption m_frameEnc; ///< Encoder for egress plaintext.
|
||
|
|
||
|
CryptoPP::SecByteBlock m_frameDecKey; ///< Key for m_frameDec
|
||
|
CryptoPP::CTR_Mode<CryptoPP::AES>::Encryption m_frameDec; ///< Decoder for egress plaintext.
|
||
|
|
||
|
CryptoPP::SecByteBlock m_macEncKey; /// Key for m_macEnd
|
||
|
CryptoPP::ECB_Mode<CryptoPP::AES>::Encryption m_macEnc; /// One-way coder used by updateMAC for ingress and egress MAC updates.
|
||
|
Mutex x_macEnc; /// Mutex
|
||
|
|
||
|
CryptoPP::SHA3_256 m_egressMac; ///< State of MAC for egress ciphertext.
|
||
|
CryptoPP::SHA3_256 m_ingressMac; ///< State of MAC for ingress ciphertext.
|
||
10 years ago
|
};
|
||
10 years ago
|
|
||
10 years ago
|
}
|
||
|
}
|