11#define NS_LOG_APPEND_CONTEXT \
12 if(GetObject<Node>()) { std::clog << "[node " << GetObject<Node>()->GetId() << "] "; }
18#include "ns3/packet.h"
19#include "ns3/assert.h"
31 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
32 "abcdefghijklmnopqrstuvwxyz"
44 .AddAttribute(
"CompressionEnabled",
"Indicates whether a compression of packets is enabled.",
48 .AddAttribute(
"EncryptionEnabled",
"Indicates whether a real encryption of packets is enabled.",
53 .AddTraceSource(
"PacketEncrypted",
54 "The change trance for currenly ecrypted packet",
56 "ns3::QKDEncryptor::PacketEncrypted")
57 .AddTraceSource(
"PacketDecrypted",
58 "The change trance for currenly decrypted packet",
60 "ns3::QKDEncryptor::PacketDecrypted")
62 .AddTraceSource(
"PacketAuthenticated",
63 "The change trance for currenly authenticated packet",
65 "ns3::QKDEncryptor::PacketAuthenticated")
66 .AddTraceSource(
"PacketDeAuthenticated",
67 "The change trance for currenly deauthenticated packet",
69 "ns3::QKDEncryptor::PacketDeAuthenticated")
91 memset(
m_iv, 0x00, CryptoPP::AES::BLOCKSIZE );
110 NS_FATAL_ERROR(
this <<
"Crypto++ supports VMAC with 16 or 32 bytes authentication tag length!");
226 CryptoPP::StringSource(
input,
true,
227 new CryptoPP::Base64Encoder(
228 new CryptoPP::StringSink(
output)
238 CryptoPP::StringSource(
input,
true,
239 new CryptoPP::Base64Decoder(
240 new CryptoPP::StringSink(
output)
254 NS_FATAL_ERROR(
"KEY SIZE DO NOT MATCH FOR OTP! \nKeySize:" << key.size() <<
"\nCipterText:" <<
cipherText.size() <<
"\n" );
269 if(
c >=
'0' &&
c <=
'9') {
return c -
'0'; }
270 if(
c >=
'a' &&
c <=
'z') {
return c -
'a' + 10; }
271 if(
c >=
'A' &&
c <=
'Z') {
return c -
'A' + 36; }
272 if(
c ==
'*') {
return 62; }
273 if(
c ==
'$') {
return 63; }
283 "abcdefghijklmnopqrstuvwxyz"
284 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
288 output.reserve(key.length());
289 if(key.size() ==
input.size()){
290 for(std::size_t
i = 0;
i <
input.size();
i++){
310 memset(
m_iv, 0x00, CryptoPP::AES::BLOCKSIZE );
314 CryptoPP::CTR_Mode< CryptoPP::AES >::Encryption
encryptor;
315 encryptor.SetKeyWithIV((
unsigned char*) key.c_str(), key.length(),
m_iv);
319 stf.Put((
unsigned char*)
data.c_str(),
data.size() );
329 memset(
m_iv, 0x00, CryptoPP::AES::BLOCKSIZE );
333 CryptoPP::CTR_Mode< CryptoPP::AES >::Decryption
decryptor;
334 decryptor.SetKeyWithIV((
unsigned char*) key.c_str(), key.length(),
m_iv);
338 stf.Put((
unsigned char*)
data.c_str(),
data.size() );
351 CryptoPP::StringSource
ss(
352 (
unsigned char*)
data.data(),
data.size(),
true,
353 new CryptoPP::HexEncoder(
new CryptoPP::StringSink(
encoded))
364 CryptoPP::StringSource
ss(
365 (
unsigned char*)
data.data(),
data.size(),
true,
366 new CryptoPP::HexDecoder(
new CryptoPP::StringSink(
decoded))
378 CryptoPP::VMAC<CryptoPP::AES>
vmac;
380 reinterpret_cast<const CryptoPP::byte*
>(key.data()),
383 CryptoPP::AES::BLOCKSIZE
389 vmac.CalculateDigest(
391 reinterpret_cast<const CryptoPP::byte*
>(
inputString.data()),
408 unsigned char digestBytes[CryptoPP::Weak::MD5::DIGESTSIZE];
410 CryptoPP::Weak1::MD5
md5;
429 unsigned char digestBytes[CryptoPP::SHA1::DIGESTSIZE];
AttributeValue implementation for Boolean.
A base class which provides memory management and object aggregation.
Smart pointer class similar to boost::intrusive_ptr.
~QKDEncryptor() override
Destructor.
uint32_t m_index
index in the qkd encryptor container
unsigned char m_iv[CryptoPP::AES::BLOCKSIZE]
bool m_encryptionEnabled
real encryption used?
std::string EncryptMsg(std::string input, std::string key)
Perform encryption of plaintext.
std::string HexDecode(const std::string &data)
Help function used to decode string to HEX string.
std::string AESEncrypt(const std::string &key, const std::string &data)
AES encryption.
EncryptionType
Encryption type.
void SetNode(Ptr< Node > node)
Set node on which qkd encryptor is installed.
Ptr< Node > GetNode() const
Get details about the node on which qkd encryptor is installed.
void SetIndex(uint32_t index)
Set internal index identifier in qkd encryptor container.
bool CheckAuthentication(std::string payload, std::string authTag, std::string key="0")
Check Authentication on packet payload for authenticated packet.
std::string Base64Decode(std::string input)
Base64 decoder.
TracedCallback< Ptr< Packet >, std::string > m_authenticationTrace
trace callback for authentication
std::string VMAC(std::string &key, std::string &inputString)
Authentication function in Wegman-Carter fashion.
std::string OTP(const std::string &key, const std::string &data)
One-time cipher.
uint32_t m_authenticationTagLengthInBits
length of the authentication tag in bits(32 by default)
std::string SHA1(std::string &inputString)
SHA1 Authentication function.
TracedCallback< Ptr< Packet >, std::string > m_deauthenticationTrace
trace callback for authentication check
AuthenticationType m_authenticationType
Ptr< Node > m_node
pointer to node on which encryptor is installed
uint32_t GetIndex() const
Get internal index identifier in qkd encryptor container.
TracedCallback< Ptr< Packet > > m_decryptionTrace
trace callback for decryption
AuthenticationType
Authentication type.
TracedCallback< Ptr< Packet > > m_encryptionTrace
trace callback for encryption
std::string HexEncode(const std::string &data)
Help function used to encode string to HEX string.
std::string Authenticate(std::string &, std::string key="0")
Help parent function used for calling child authentication functions.
std::string COTP(const std::string &key, const std::string &input)
One-Time Pad cipher where output is alfabet/number symbols.
void ChangeSettings(EncryptionType type1, AuthenticationType type2, uint32_t authTagLength)
Constructor.
bool m_compressionEnabled
should compression algorithms be used?
std::string DecryptMsg(std::string input, std::string key)
Perform decryption of ciphertext.
static TypeId GetTypeId()
Get the TypeId.
std::string Base64Encode(std::string input)
Base64 encoder.
EncryptionType m_encryptionType
std::string MD5(std::string &inputString)
MD5 Authentication function.
std::string AESDecrypt(const std::string &key, const std::string &data)
AES decryption.
a unique identifier for an interface.
TypeId SetParent(TypeId tid)
Set the parent TypeId.
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Ptr< T > Create(Ts &&... args)
Create class instances by constructors with varying numbers of arguments and return them by Ptr.
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
Create a TraceSourceAccessor which will control access to the underlying trace source.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Ptr< const AttributeChecker > MakeBooleanChecker()
static unsigned int value(char c)
static const std::string base64_chars
static bool is_base64(unsigned char c)
Ptr< const AttributeAccessor > MakeBooleanAccessor(T1 a1)