21 #define NS_LOG_APPEND_CONTEXT \
22 if (GetObject<Node> ()) { std::clog << "[node " << GetObject<Node> ()->GetId () << "] "; }
28 #include "ns3/packet.h"
29 #include "ns3/assert.h"
41 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
42 "abcdefghijklmnopqrstuvwxyz"
46 return (isalnum(c) || (c ==
'+') || (c ==
'/'));
54 .AddAttribute (
"CompressionEnabled",
"Indicates whether a compression of packets is enabled.",
58 .AddAttribute (
"EncryptionEnabled",
"Indicates whether a real encryption of packets is enabled.",
63 .AddTraceSource (
"PacketEncrypted",
64 "The change trance for currenly ecrypted packet",
66 "ns3::QKDEncryptor::PacketEncrypted")
67 .AddTraceSource (
"PacketDecrypted",
68 "The change trance for currenly decrypted packet",
70 "ns3::QKDEncryptor::PacketDecrypted")
72 .AddTraceSource (
"PacketAuthenticated",
73 "The change trance for currenly authenticated packet",
75 "ns3::QKDEncryptor::PacketAuthenticated")
76 .AddTraceSource (
"PacketDeAuthenticated",
77 "The change trance for currenly deauthenticated packet",
79 "ns3::QKDEncryptor::PacketDeAuthenticated")
90 memset(
m_iv, 0x00, CryptoPP::AES::BLOCKSIZE );
96 uint32_t authTagLength
106 uint32_t authTagLength
108 if(authTagLength != 128 && authTagLength != 256 ){
109 NS_FATAL_ERROR(
this <<
"Crypto++ supports VMAC with 16 or 32 bytes authentication tag length!");
153 output =
OTP(key, input);
174 output =
OTP(key, input);
192 return VMAC (key, inputString);
195 return MD5 (inputString);
198 return SHA1 (inputString);
211 if (genAuthTag == authTag)
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)
253 if(key.size() != cipherText.size()){
254 NS_FATAL_ERROR (
"KEY SIZE DO NOT MATCH FOR OTP! \nKeySize:" << key.size() <<
"\nCipterText:" << cipherText.size() <<
"\n" );
258 for (std::size_t i = 0; i < cipherText.size(); i++){
259 output.push_back(key[i] ^ cipherText[i]);
272 memset(
m_iv, 0x00, CryptoPP::AES::BLOCKSIZE );
273 std::string encryptData;
276 CryptoPP::CTR_Mode< CryptoPP::AES >::Encryption encryptor;
277 encryptor.SetKeyWithIV((
byte*) key.c_str(), key.length(),
m_iv);
280 CryptoPP::StreamTransformationFilter stf( encryptor,
new CryptoPP::StringSink( encryptData ) );
281 stf.Put( (
byte*)
data.c_str(),
data.size() );
291 memset(
m_iv, 0x00, CryptoPP::AES::BLOCKSIZE );
292 std::string decryptData;
295 CryptoPP::CTR_Mode< CryptoPP::AES >::Decryption decryptor;
296 decryptor.SetKeyWithIV((
byte*) key.c_str(), key.length(),
m_iv);
299 CryptoPP::StreamTransformationFilter stf( decryptor,
new CryptoPP::StringSink( decryptData ) );
300 stf.Put( (
byte*)
data.c_str(),
data.size() );
313 CryptoPP::StringSource ss(
314 (
byte*)
data.data(),
data.size(),
true,
315 new CryptoPP::HexEncoder(
new CryptoPP::StringSink(encoded))
326 CryptoPP::StringSource ss(
327 (
byte*)
data.data(),
data.size(),
true,
328 new CryptoPP::HexDecoder(
new CryptoPP::StringSink(decoded))
336 NS_LOG_FUNCTION (
this << inputString.length() << key.length() << CryptoPP::AES::BLOCKSIZE );
337 std::string outputString;
339 memset(
m_iv, 0x00, CryptoPP::AES::BLOCKSIZE );
341 byte digestBytes[key.length()];
342 CryptoPP::VMAC<CryptoPP::AES> vmac;
344 vmac.SetKeyWithIV((
byte*) key.c_str(), key.length(),
m_iv, CryptoPP::AES::BLOCKSIZE);
345 vmac.CalculateDigest(digestBytes, (
byte *) inputString.c_str(), inputString.length());
347 CryptoPP::HexEncoder encoder;
348 encoder.Attach(
new CryptoPP::StringSink(outputString));
349 encoder.Put(digestBytes,
sizeof(digestBytes));
350 encoder.MessageEnd();
362 byte digestBytes[CryptoPP::Weak::MD5::DIGESTSIZE];
364 CryptoPP::Weak1::MD5 md5;
365 md5.CalculateDigest(digestBytes, (
byte *) inputString.c_str(), inputString.length());
367 std::string outputString;
368 CryptoPP::HexEncoder encoder;
370 encoder.Attach(
new CryptoPP::StringSink(outputString));
371 encoder.Put(digestBytes,
sizeof(digestBytes));
372 encoder.MessageEnd();
383 byte digestBytes[CryptoPP::SHA1::DIGESTSIZE];
386 sha1.CalculateDigest(digestBytes, (
byte *) inputString.c_str(), inputString.length());
388 std::string outputString;
389 CryptoPP::HexEncoder encoder;
391 encoder.Attach(
new CryptoPP::StringSink(outputString));
392 encoder.Put(digestBytes,
sizeof(digestBytes));
393 encoder.MessageEnd();
AttributeValue implementation for Boolean.
A base class which provides memory management and object aggregation.
uint32_t m_index
An index in the qkd encryptor container.
bool m_encryptionEnabled
Execute the actual encryption algorithm?
std::string EncryptMsg(std::string input, std::string key)
Encrypt a plaintext.
std::string HexDecode(const std::string &data)
Decode the HEX string.
std::string AESEncrypt(const std::string &key, const std::string &data)
Encrypt data using AES algorithm.
EncryptionType
Encryption type.
uint32_t GetIndex()
Get the internal index identifier in the qkd encryptor container.
void SetNode(Ptr< Node > node)
Set node on which qkd encryptor is installed.
void SetIndex(uint32_t index)
Set the internal index identifier in the qkd encryptor container.
static TypeId GetTypeId(void)
Get the TypeId.
std::string Authenticate(std::string &data, std::string key="0")
Calculate an authentication tag on a message.
bool CheckAuthentication(std::string payload, std::string authTag, std::string key="0")
Authenticate the packet.
byte m_iv[CryptoPP::AES::BLOCKSIZE]
std::string Base64Decode(std::string input)
Base64 decoder.
TracedCallback< Ptr< Packet >, std::string > m_authenticationTrace
A trace callback for the authentication.
std::string VMAC(std::string &key, std::string &inputString)
Calculate authentication tag in Wegman-Carter fashion.
std::string OTP(const std::string &key, const std::string &data)
Apply the One-Time Pad cipher.
uint32_t m_authenticationTagLengthInBits
A length of the authentication tag in bits (32 by default).
std::string SHA1(std::string &inputString)
Calucale SHA1 authentication tag.
TracedCallback< Ptr< Packet >, std::string > m_deauthenticationTrace
A trace callback for the authentication check.
AuthenticationType m_authenticationType
The authentication algorithm.
Ptr< Node > m_node
A pointer to the node on which the encryptor is installed.
Ptr< Node > GetNode()
Get details about the node on which qkd encryptor is installed.
TracedCallback< Ptr< Packet > > m_decryptionTrace
A trace callback for the decryption.
AuthenticationType
Authentication type.
TracedCallback< Ptr< Packet > > m_encryptionTrace
A trace callback for the encryption.
QKDEncryptor(EncryptionType type1, AuthenticationType type2)
Constructor.
std::string HexEncode(const std::string &data)
Encode the string to the HEX string.
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)
Decrypt a ciphertext.
std::string Base64Encode(std::string input)
Base64 encoder.
virtual ~QKDEncryptor()
Destructor.
EncryptionType m_encryptionType
The encryption algorithm.
std::string MD5(std::string &inputString)
Calculate MD5 authentication tag.
std::string AESDecrypt(const std::string &key, const std::string &data)
Decrypt data using AES algorithm.
a unique identifier for an interface.
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Ptr< const AttributeChecker > MakeBooleanChecker(void)
Ptr< const AttributeAccessor > MakeBooleanAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method.
#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< 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.
static const std::string base64_chars
static bool is_base64(unsigned char c)