A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
qkd-key.cc
Go to the documentation of this file.
1/*
2 * Copyright(c) 2005,2006 INRIA
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 *
7 *
8 * Author: Miralem Mehic <miralem.mehic@ieee.org>
9 */
10#include "ns3/packet.h"
11#include "ns3/assert.h"
12#include "ns3/log.h"
13#include "qkd-key.h"
14#include <string>
15#include <cstdarg>
16
17#include <iostream>
18#include <ctime>
19#include <unistd.h>
20#include <cryptopp/base64.h>
21
22
23namespace ns3 {
24
26
28
29 TypeId
31 {
32 static TypeId tid = TypeId("ns3::QKDKey")
34 .AddTraceSource("StateTransition",
35 "Trace fired upon every QKDKey state transition.",
37 "ns3::Application::StateTransitionCallback")
38 ;
39 return tid;
40 }
41
42 uint64_t QKDKey::m_globalUid = 0;
43
46 {
47 // we need to invoke the copy constructor directly
48 // rather than calling Create because the copy constructor
49 // is private.
50 return Ptr<QKDKey>(new QKDKey(*this), false);
51 }
52
53
54 /*
55 @toDO: this funtion will need modification
56 need to look like the other constructor!
57 */
58 QKDKey::QKDKey(uint64_t size)
59 : m_size(size),
60 m_state(INIT)
61 {
62 NS_LOG_FUNCTION (this << size );
63
67 m_id += "-";
70
71 //ETSI 014 - Keys are encoded using Base64
72 std::string randomString = GenerateRandomString(size/7);
73 std::string keyBinary;
74 for(std::size_t i = 0; i < randomString.size(); ++i)
75 keyBinary += std::bitset<8>(randomString.c_str()[i]).to_string();
76
77 CryptoPP::StringSource(keyBinary, true,
78 new CryptoPP::Base64Encoder(
79 new CryptoPP::StringSink(m_key)
80 ) // Base64Encoder
81 ); // StringSource
82 m_key = m_key.substr(0, m_size);
83 NS_LOG_FUNCTION (this << m_id << m_size << m_key.length() << GetStateString() );
84
86
87 }
88
90 std::string keyId,
91 uint64_t keyIdnum,
92 uint64_t size)
93 : m_id(keyId),
94 m_size(size),
95 m_state(INIT)
96 {
97 NS_LOG_FUNCTION (this << m_id << m_size );
98
100 //m_internalID = m_globalUid;
103
105 m_key = randomString.substr(0, m_size);
106
107 //NS_LOG_FUNCTION (this << m_id << m_key.length() << randomString.length() << GetStateString() );
108 NS_LOG_FUNCTION( this << m_internalID << m_key.length() << m_key );
110 }
111
112
114 std::string keyId,
115 uint64_t keyIdnum,
116 std::string key)
117 : m_id(keyId),
118 m_key(key),
119 m_state(INIT)
120 {
121 NS_LOG_FUNCTION (this << m_id << m_key.length() );
122
123 m_globalUid++;
124 //m_internalID = m_globalUid;
127 m_size = m_key.length();
128
129 //NS_LOG_FUNCTION (this << m_id << m_key.length() << randomString.length() << GetStateString() );
130 NS_LOG_FUNCTION( this << m_internalID << m_key.length() << m_key );
132 }
133
135 std::string keyId,
136 std::string key)
137 : m_id(keyId),
138 m_key(key),
139 m_state(INIT)
140 {
142 m_size = m_key.length();
144 }
145
146 std::string
147 QKDKey::GetKeyString() //To get key content without changing its state!
148 {
149 NS_LOG_FUNCTION( this );
150 return m_key;
151 }
152
153 void
155 {
156 NS_LOG_FUNCTION(this << value);
157 m_key = value;
158 m_size = value.length();
159 }
160
161 std::string
163
164 NS_LOG_FUNCTION(this);
165
166 std::string keyBinary;
167 for(std::size_t i = 0; i < m_key.size(); ++i){
168 keyBinary += std::bitset<8>(m_key.c_str()[i]).to_string();
169 }
170 NS_LOG_FUNCTION(this);
171 return keyBinary;
172 }
173
174 std::string
176 {
177 return m_id;
178 }
179
180 void
181 QKDKey::SetId(std::string value)
182 {
183 m_id = value;
184 }
185
186 uint64_t
188 {
189 NS_LOG_FUNCTION (this << m_id << m_size);
190 return m_size; //@NOTE: this is in bytes!!!
191 }
192
193 uint64_t
195 {
196 NS_LOG_FUNCTION(this << m_id << m_size*8);
197 return m_size*8;
198 }
199
200 void
202 {
203 NS_LOG_FUNCTION (this << m_id << value);
204
205 if(value%8 != 0)
206 NS_FATAL_ERROR( this << "Key size must be in bits and divided with 8!" );
207
208 m_size = value;
209 }
210
211 std::string
213 {
214 NS_LOG_FUNCTION (this << m_id << m_key.length() );
216 return m_key;
217 }
218
219 std::string
221 {
222 NS_LOG_FUNCTION (this << m_id << m_key.length() );
223 return m_key;
224 }
225
226 uint8_t *
228 {
229 NS_LOG_FUNCTION (this << m_id << m_key.length() );
231 uint8_t* temp = new uint8_t [m_key.length()];
232 memcpy( temp, m_key.data(), m_key.length());
233 return temp;
234 }
235
236 void
238 NS_LOG_FUNCTION( this << m_id << m_key.length() << m_state );
240 }
241
242 void
244
245 NS_LOG_FUNCTION (this << m_id << m_key.length() << m_state );
247 }
248
249 void
251
252 NS_LOG_FUNCTION (this << m_id << m_key.length() << m_state );
254 }
255
256 void
258
259 NS_LOG_FUNCTION (this << m_id << m_key.length() << m_state );
261
262 }
263
264 void
266 NS_LOG_FUNCTION( this << m_id << m_key.length() << m_state );
268 }
269
270 std::string
272
273 NS_LOG_FUNCTION( this << m_internalID << len );
274
275 std::string tmp_s;
276 static const char alphanum[] =
277 "0123456789"
278 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
279 "abcdefghijklmnopqrstuvwxyz";
280 //srand( m_internalID );
281 for(int i = 0; i < len; ++i){
282 tmp_s += alphanum[rand() %(sizeof(alphanum) - 1)];
283 }
284 return tmp_s;
285 }
286
289 {
290 return m_state;
291 }
292
293
294 // static
295 std::string
297 {
298 switch(state)
299 {
300 case INIT:
301 return "INIT";
302 break;
303 case READY:
304 return "READY";
305 break;
306 case SERVED:
307 return "SERVED";
308 break;
309 case USED:
310 return "USED";
311 break;
312 case OBSOLETE:
313 return "OBSOLETE";
314 break;
315 case RESTORED:
316 return "RESTORED";
317 break;
318 case RESERVED:
319 return "RESERVED";
320 break;
321 default:
322 NS_FATAL_ERROR("Unknown state");
323 return "FATAL_ERROR";
324 break;
325 }
326 }
327
328
329 std::string
331 {
332 return GetStateString(m_state);
333 }
334
335 void
337 {
338 const std::string oldState = GetStateString();
339 const std::string newState = GetStateString(state);
341
342 m_state = state;
343
344 NS_LOG_INFO(this << " QKDKey " << oldState
345 << " --> " << newState << ".");
347 }
348
349 Time
351 {
352 return m_timestamp;
353 }
354
355 void
357 {
359 }
360
361 std::string
363 {
364 return m_moduleId;
365 }
366
367} // namespace ns3
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
The QKD key is an elementary class of QKDNetSim.
Definition qkd-key.h:59
void MarkUsed()
Definition qkd-key.cc:250
std::string GetModuleId()
Returns the id of the QKD module that generated key.
Definition qkd-key.cc:362
Ptr< QKDKey > Copy() const
Help function - Copy key.
Definition qkd-key.cc:45
std::string GetKeyString()
Get QKD key.
Definition qkd-key.cc:147
void SetId(std::string)
Definition qkd-key.cc:181
std::string GenerateRandomString(const int len)
Return random string.
Definition qkd-key.cc:271
void SetModuleId(std::string)
Save details about the QKD module that generated key.
Definition qkd-key.cc:356
Time GetKeyTimestamp()
Get timestamp of the key.
Definition qkd-key.cc:350
static TypeId GetTypeId()
Get the TypeId.
Definition qkd-key.cc:30
std::string ToString()
Return the raw key in std::string format.
Definition qkd-key.cc:220
uint64_t m_internalID
Definition qkd-key.h:224
std::string GetKeyBinary()
Return key in bits which is necessery for encryption or authentication.
Definition qkd-key.cc:162
void MarkReady()
Definition qkd-key.cc:237
QKDKeyState_e
QKD Key States.
Definition qkd-key.h:65
std::string m_moduleId
id of QKD module that generated key
Definition qkd-key.h:228
QKDKeyState_e m_state
state of the key
Definition qkd-key.h:227
void MarkRestored()
Definition qkd-key.cc:257
ns3::TracedCallback< const std::string &, const std::string & > m_stateTransitionTrace
The StateTransition trace source.
Definition qkd-key.h:217
void SetSize(uint64_t)
Set the size of the key.
Definition qkd-key.cc:201
std::string ConsumeKeyString()
Return the raw key in std::string format and switch to SERVED state.
Definition qkd-key.cc:212
std::string m_key
Definition qkd-key.h:221
std::string GetId() const
Definition qkd-key.cc:175
uint64_t m_size
Definition qkd-key.h:222
uint64_t GetSize() const
Get the size of the key.
Definition qkd-key.cc:187
void MarkServed()
Definition qkd-key.cc:243
std::string GetStateString() const
Returns the current state of the key in string format.
Definition qkd-key.cc:330
void SetValue(std::string)
Definition qkd-key.cc:154
void SwitchToState(QKDKeyState_e state)
Change the state of the key.
Definition qkd-key.cc:336
void MarkReserved()
Definition qkd-key.cc:265
uint64_t GetSizeInBits() const
Get the size of the key in bits.
Definition qkd-key.cc:194
std::string m_id
Definition qkd-key.h:220
Time m_timestamp
Definition qkd-key.h:226
QKDKey(uint64_t keySize)
Create an empty QKD key of a key size.
Definition qkd-key.cc:58
QKDKeyState_e GetState() const
Returns the current state of the key.
Definition qkd-key.cc:288
uint8_t * GetKey()
Return key in byte* which is necessery for encryption or authentication Convert key from std::String ...
Definition qkd-key.cc:227
static uint64_t m_globalUid
Definition qkd-key.h:225
static Time Now()
Return the current simulation virtual time.
Definition simulator.cc:197
Simulation virtual time values and global simulation resolution.
Definition nstime.h:94
a unique identifier for an interface.
Definition type-id.h:49
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition type-id.cc:1001
#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.
Definition log.h:191
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition log.h:264
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition object-base.h:35
Ptr< T > Create(Ts &&... args)
Create class instances by constructors with varying numbers of arguments and return them by Ptr.
Definition ptr.h:436
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 unsigned int value(char c)