A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
qkd-encryptor.h
Go to the documentation of this file.
1/*
2 * Copyright(c) 2020 DOTFEESA www.tk.etf.unsa.ba
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 *
7 *
8 * Author: Miralem Mehic <miralem.mehic@ieee.org>
9 */
10
11#ifndef QKDEncryptor_H
12#define QKDEncryptor_H
13
14#include <algorithm>
15#include <stdint.h>
16
17#include "ns3/header.h"
18#include "ns3/tcp-header.h"
19#include "ns3/udp-header.h"
20#include "ns3/icmpv4.h"
21
22#include "ns3/dsdv-packet.h"
23#include "ns3/aodv-packet.h"
24#include "ns3/olsr-header.h"
25
26#include "ns3/packet.h"
27#include "ns3/tag.h"
28#include "ns3/object.h"
29#include "ns3/callback.h"
30#include "ns3/assert.h"
31#include "ns3/ptr.h"
32#include "ns3/deprecated.h"
33#include "ns3/traced-value.h"
34#include "ns3/packet-metadata.h"
35#include "ns3/trace-source-accessor.h"
36#include "qkd-key.h"
37#include "ns3/net-device.h"
38#include "ns3/node.h"
39
40#include <crypto++/aes.h>
41#include <crypto++/modes.h>
42#include <crypto++/filters.h>
43#include <crypto++/hex.h>
44#include <crypto++/osrng.h>
45#include <crypto++/ccm.h>
46#include <crypto++/vmac.h>
47#include <crypto++/iterhash.h>
48#include <crypto++/secblock.h>
49#include <crypto++/sha.h>
50#include <cryptopp/base64.h>
51#include <vector>
52
53#define CRYPTOPP_ENABLE_NAMESPACE_WEAK 1
54#include <crypto++/md5.h>
55
56namespace ns3 {
57
58/**
59 * @ingroup qkd
60 * @class QKD Encryptor
61 * @brief QKD Encryptor is a class used to perform encryption, decryption, authentication,
62 * atuhentication-check operations and reassembly of previously fragmented packets.
63 *
64 * @note QKD Encryptor uses cryptographic algorithms and schemes from
65 * Crypto++ free and open source C++ class cryptographic library. Currently,
66 * QKD Encryptor supports following crypto-graphic algorithms and schemes:
67 * - One-Time Pad(OTP) cipher,
68 * - Advanced Encryption Standard(AES) block cipher,
69 * - VMAC message authentication code(MAC) algorithm,
70 * - MD5 MAC algorithm,
71 * - SHA1 MAC algorithm.
72 *
73 * As these algorithms can put a significant computational load on machines performing
74 * the simulation, the users can turn off actual execution of such algorithms and allow
75 * efficient simulation with more significant QKD topologies.
76 */
77class QKDEncryptor : public Object
78{
79public:
80
81 /**
82 * @brief Encryption type
83 */
89
90 /**
91 * @brief Authentication type
92 */
99
100
101 QKDEncryptor();
102
103 /**
104 * @brief Constructor
105 */
107
108 /**
109 * @brief Constructor
110 */
112 /**
113 * @brief Constructor
114 */
116 /**
117 * @brief Constructor
118 */
120 /**
121 * @brief Destructor
122 */
123 ~QKDEncryptor() override;
124
125 /**
126 * @brief Get the TypeId
127 * @return The TypeId for this class
128 */
129 static TypeId GetTypeId();
130
131 /**
132 * @brief Set node on which qkd encryptor is installed
133 * @param Ptr<Node> node
134 */
135 void SetNode(Ptr<Node> node);
136
137 /**
138 * @brief Get details about the node on which qkd encryptor is installed
139 * @return Ptr<Node> node
140 */
141 Ptr<Node> GetNode() const;
142
143 /**
144 * @brief Set internal index identifier in qkd encryptor container. @featureTask
145 * @param uint32_t index
146 */
147 void SetIndex(uint32_t index);
148
149 /**
150 * @brief Get internal index identifier in qkd encryptor container. @featureTask
151 * @return uint32_t index
152 */
153 uint32_t GetIndex() const;
154
155 /**
156 * @brief One-time cipher
157 * @param key key for encryption
158 * @param data message to encrypt/decrypt
159 * @return string encypted/decrypted message
160 */
161 std::string OTP(const std::string& key, const std::string& data);
162
163 /**
164 * @brief One-Time Pad cipher where output is alfabet/number symbols
165 * @param key symmetric key
166 * @param input input message
167 * @return string encrypted/decrypted message
168 *
169 * Solution adapted from: https://stackoverflow.com/questions/12671510/xor-on-two-hexadeicmal-values-stored-as-string-in-c
170 */
171 std::string COTP(const std::string& key, const std::string& input);
172
173 /**
174 * AES encryption
175 * @param std::string data
176 * @param Ptr<QKDKey> key
177 * @return std::string
178 */
179 std::string AESEncrypt(const std::string& key, const std::string& data);
180
181 /**
182 * AES decryption
183 * @param std::string data
184 * @param Ptr<QKDKey> key
185 * @return std::string
186 */
187 std::string AESDecrypt(const std::string& key, const std::string& data);
188
189 /**
190 * @brief Perform encryption of plaintext
191 * @param input plaintext
192 * @param key encryption key
193 * @return string ciphertext
194 */
195 std::string EncryptMsg(std::string input, std::string key);
196
197 /**
198 * @brief Perform decryption of ciphertext
199 * @param input ciphertext
200 * @param key encryption key
201 * @return string plaintext
202 */
203 std::string DecryptMsg(std::string input, std::string key);
204
205 /**
206 * Help parent function used for calling child authentication functions
207 * @param std::string data
208 * @param Ptr<QKDKey> key
209 * @param uint8_t authentic
210 * @return std::string
211 */
212 std::string Authenticate(std::string&, std::string key = "0");
213
214 /**
215 * @brief Check Authentication on packet payload for authenticated packet
216 * @param payload payload data
217 * @param key key for authentication
218 * @return bool authentication check result
219 */
220 bool CheckAuthentication(std::string payload, std::string authTag, std::string key = "0");
221
222 /**
223 * Help function used to encode string to HEX string
224 * @param std::string data
225 * @return std::string
226 */
227 std::string HexEncode(const std::string& data);
228
229 /**
230 * Help function used to decode string to HEX string
231 * @param std::string data
232 * @return std::string
233 */
234 std::string HexDecode(const std::string& data);
235
236 /**
237 * @brief Base64 encoder
238 * @param input input data
239 * @return string base64 encoded input
240 */
241 std::string Base64Encode(std::string input);
242
243 /**
244 * @brief Base64 decoder
245 * @param input input data
246 * @return string decoded input
247 */
248 std::string Base64Decode(std::string input);
249
250 /**
251 * Authentication function in Wegman-Carter fashion
252 * @param std::string data
253 * @param std::string data
254 * @param uint32_t length of auth tag
255 * @return std::string
256 */
257 std::string VMAC(std::string& key, std::string& inputString);
258
259 /**
260 * MD5 Authentication function
261 * @param std::string data
262 * @return std::string
263 */
264 std::string MD5(std::string& inputString);
265
266 /**
267 * SHA1 Authentication function
268 * @param std::string data
269 * @return std::string
270 */
271 std::string SHA1(std::string& inputString);
272
273private:
274
275 unsigned char m_iv [ CryptoPP::AES::BLOCKSIZE ];
276
277 Ptr<Node> m_node; //!< pointer to node on which encryptor is installed
278 uint32_t m_index; //!< index in the qkd encryptor container
279
280 bool m_encryptionEnabled; //!< real encryption used?
281 bool m_compressionEnabled; //!< should compression algorithms be used?
282 uint32_t m_authenticationTagLengthInBits; //!< length of the authentication tag in bits(32 by default)
283
286
287 TracedCallback<Ptr<Packet> > m_encryptionTrace; //!< trace callback for encryption
288 TracedCallback<Ptr<Packet> > m_decryptionTrace; //!< trace callback for decryption
289
290 TracedCallback<Ptr<Packet>, std::string > m_authenticationTrace; //!< trace callback for authentication
291 TracedCallback<Ptr<Packet>, std::string > m_deauthenticationTrace; //!< trace callback for authentication check
292
293
294};
295} // namespace ns3
296
297#endif /* QKDEncryptor_QKD_H */
A base class which provides memory management and object aggregation.
Definition object.h:78
Smart pointer class similar to boost::intrusive_ptr.
Definition ptr.h:66
Introspection did not find any typical Config paths.
~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.
Forward calls to a chain of Callback.
a unique identifier for an interface.
Definition type-id.h:49
Ptr< T > Create(Ts &&... args)
Create class instances by constructors with varying numbers of arguments and return them by Ptr.
Definition ptr.h:436
Every class exported by the ns3 library is enclosed in the ns3 namespace.
uint8_t data[writeSize]