A Discrete-Event Network Simulator
API
qkd-key.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2005,2006 INRIA
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Author: Miralem Mehic <miralem.mehic@ieee.org>
19  */
20 #include "ns3/packet.h"
21 #include "ns3/assert.h"
22 #include "ns3/log.h"
23 #include "qkd-key.h"
24 #include <string>
25 #include <cstdarg>
26 
27 #include <iostream>
28 #include <ctime>
29 #include <unistd.h>
30 #include <cryptopp/base64.h>
31 
32 
33 namespace ns3 {
34 
35  NS_LOG_COMPONENT_DEFINE ("QKDKey");
36 
38 
39  TypeId
41  {
42  static TypeId tid = TypeId ("ns3::QKDKey")
43  .SetParent<Object> ()
44  .AddTraceSource ("StateTransition",
45  "Trace fired upon every QKDKey state transition.",
47  "ns3::Application::StateTransitionCallback")
48  ;
49  return tid;
50  }
51 
52  uint64_t QKDKey::m_globalUid = 0;
53 
55  QKDKey::Copy (void) const
56  {
57  // we need to invoke the copy constructor directly
58  // rather than calling Create because the copy constructor
59  // is private.
60  return Ptr<QKDKey> (new QKDKey (*this), false);
61  }
62 
63 
64  /*
65  @toDO: this funtion will need modification
66  need to look like the other constructor!
67  */
68  QKDKey::QKDKey (uint64_t size)
69  : m_sizeInBits (size),
70  m_state (INIT)
71  {
72  NS_LOG_FUNCTION (this << size );
73 
74  m_globalUid++;
77  m_id += "-";
78  m_id += m_globalUid;
80  m_random = CreateObject<UniformRandomVariable> ();
81 
82  if(m_sizeInBits%8 != 0) {
83  NS_FATAL_ERROR( this << "Key size must divided with 8!" );
84  }
86 
87  //ETSI 014 - Keys are encoded using Base64
88  std::string randomString = GenerateRandomString(size/7);
89  std::string keyBinary;
90  for (std::size_t i = 0; i < randomString.size(); ++i){
91  keyBinary += std::bitset<8>(randomString.c_str()[i]).to_string();
92  }
93 
94  CryptoPP::StringSource(keyBinary, true,
95  new CryptoPP::Base64Encoder(
96  new CryptoPP::StringSink(m_key)
97  ) // Base64Encoder
98  ); // StringSource
99  m_key = m_key.substr(0, m_sizeInBytes);
100  NS_LOG_FUNCTION (this << m_id << m_sizeInBytes << m_key.length() << GetStateString() );
101 
103 
104  }
105 
107  std::string keyId,
108  uint64_t keyIdnum,
109  uint64_t size)
110  : m_id (keyId),
111  m_sizeInBits (size),
112  m_state (INIT)
113  {
114  NS_LOG_FUNCTION (this << m_id << m_sizeInBytes );
115 
116  m_globalUid++;
117  m_internalID = keyIdnum;
119  m_random = CreateObject<UniformRandomVariable> ();
120 
121  if(m_sizeInBits%8 != 0) {
122  NS_FATAL_ERROR( this << "Key size must divided with 8! You tried to create a key of size " << m_sizeInBits << " (bits), " << m_sizeInBytes << " (bytes)" );
123  }
125 
126  std::string randomString = GenerateRandomString(m_sizeInBytes);
127  m_key = randomString.substr(0, m_sizeInBytes);
128 
129  NS_LOG_FUNCTION ( this << m_internalID << m_key.length() << m_key );
131  }
132 
133 
135  std::string keyId,
136  uint64_t keyIdnum,
137  std::string key)
138  : m_id (keyId),
139  m_key (key),
140  m_state (INIT)
141  {
142  NS_LOG_FUNCTION (this << m_id << m_key.length() );
143 
144  m_globalUid++;
145  m_internalID = keyIdnum;
147  m_sizeInBytes = m_key.length();
149  m_random = CreateObject<UniformRandomVariable> ();
150 
151  NS_LOG_FUNCTION ( this << m_internalID << m_key.length() << m_key );
153  }
154 
156  std::string keyId,
157  std::string key)
158  : m_id (keyId),
159  m_key (key),
160  m_state (INIT)
161  {
163  m_sizeInBytes = m_key.length();
165  m_random = CreateObject<UniformRandomVariable> ();
167  }
168 
169  std::string
170  QKDKey::GetKeyString() //To get key content without changing its state!
171  {
172  NS_LOG_FUNCTION( this );
173  return m_key;
174  }
175 
176  std::string
178 
179  NS_LOG_FUNCTION(this);
180 
181  std::string keyBinary;
182  for (std::size_t i = 0; i < m_key.size(); ++i){
183  keyBinary += std::bitset<8>(m_key.c_str()[i]).to_string();
184  }
185  NS_LOG_FUNCTION(this);
186  return keyBinary;
187  }
188 
189  std::string
190  QKDKey::GetId (void) const
191  {
192  return m_id;
193  }
194 
195  void
196  QKDKey::SetId (std::string value)
197  {
198  m_id = value;
199  }
200 
201  uint64_t
202  QKDKey::GetSize (void) const
203  {
204  NS_LOG_FUNCTION (this << m_id << m_sizeInBytes);
205  return m_sizeInBytes;
206  }
207 
208  uint64_t
210  {
211  NS_LOG_FUNCTION(this << m_id << m_sizeInBytes*8);
212  return m_sizeInBytes*8;
213  }
214 
215  void
216  QKDKey::SetSize (uint64_t sizeInBytes)
217  {
218  NS_LOG_FUNCTION (this << m_id << sizeInBytes);
219 
220  if(sizeInBytes%8 != 0) NS_FATAL_ERROR( this << "Key size must divided with 8!" );
221 
222  m_sizeInBytes = sizeInBytes;
224  }
225 
226  std::string
228  {
229  NS_LOG_FUNCTION (this << m_id << m_key.length() );
231  return m_key;
232  }
233 
234  std::string
236  {
237  NS_LOG_FUNCTION (this << m_id << m_key.length() );
238  return m_key;
239  }
240 
241  uint8_t *
243  {
244  NS_LOG_FUNCTION (this << m_id << m_key.length() );
246  uint8_t* temp = new uint8_t [m_key.length()];
247  memcpy( temp, m_key.data(), m_key.length());
248  return temp;
249  }
250 
251  void
253  NS_LOG_FUNCTION( this << m_id << m_key.length() << m_state );
255  }
256 
257  void
259 
260  NS_LOG_FUNCTION (this << m_id << m_key.length() << m_state );
262  }
263 
264  void
266 
267  NS_LOG_FUNCTION (this << m_id << m_key.length() << m_state );
269  }
270 
271  void
273 
274  NS_LOG_FUNCTION (this << m_id << m_key.length() << m_state );
276 
277  }
278 
279  void
281  NS_LOG_FUNCTION( this << m_id << m_key.length() << m_state );
283  }
284 
285  void
287  NS_LOG_FUNCTION( this << m_id << m_key.length() << m_state );
289  }
290 
291  std::string
293 
294  NS_LOG_FUNCTION ( this << m_internalID << len );
295 
296  std::string tmp_s;
297  static const char alphanum[] =
298  "0123456789"
299  "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
300  "abcdefghijklmnopqrstuvwxyz";
301 
302  uint32_t randVal = 0;
303  for (int i = 0; i < len; ++i){
304  randVal = round(m_random->GetValue (0, sizeof(alphanum) - 1));
305  tmp_s += alphanum[ randVal ];
306  }
307  return tmp_s;
308  }
309 
312  {
313  return m_state;
314  }
315 
316 
317  // static
318  std::string
320  {
321  switch (state)
322  {
323  case INIT:
324  return "INIT";
325  break;
326  case READY:
327  return "READY";
328  break;
329  case SERVED:
330  return "SERVED";
331  break;
332  case USED:
333  return "USED";
334  break;
335  case TRANSFORMED:
336  return "TRANSFORMED";
337  break;
338  case OBSOLETE:
339  return "OBSOLETE";
340  break;
341  case RESTORED:
342  return "RESTORED";
343  break;
344  case RESERVED:
345  return "RESERVED";
346  break;
347  default:
348  NS_FATAL_ERROR ("Unknown state");
349  return "FATAL_ERROR";
350  break;
351  }
352  }
353 
354 
355  std::string
357  {
358  return GetStateString (m_state);
359  }
360 
361  void
363  {
364  const std::string oldState = GetStateString ();
365  const std::string newState = GetStateString (state);
366  NS_LOG_FUNCTION (this << oldState << newState);
367 
368  m_state = state;
369 
370  NS_LOG_INFO (this << " QKDKey " << oldState
371  << " --> " << newState << ".");
372  m_stateTransitionTrace (oldState, newState);
373  }
374 
375  Time
377  {
378  return m_timestamp;
379  }
380 
381 } // namespace ns3
A base class which provides memory management and object aggregation.
Definition: object.h:88
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:74
The QKD key is an elementary class of QKDNetSim.
Definition: qkd-key.h:70
void MarkUsed()
Mark the key as used.
Definition: qkd-key.cc:265
std::string GenerateRandomString(const int len)
Get a random string.
Definition: qkd-key.cc:292
void SetId(std::string value)
Set the key identifier.
Definition: qkd-key.cc:196
uint64_t m_sizeInBits
Definition: qkd-key.h:267
Time GetKeyTimestamp()
Get the key timestamp.
Definition: qkd-key.cc:376
std::string GetId(void) const
Get the key identifier.
Definition: qkd-key.cc:190
std::string GetKeyString(void)
Get QKD key value.
Definition: qkd-key.cc:170
uint64_t m_internalID
Definition: qkd-key.h:263
std::string GetKeyBinary()
Get key value in bit notation.
Definition: qkd-key.cc:177
void MarkReady()
Mark the key as ready.
Definition: qkd-key.cc:252
QKDKeyState_e
The QKD key states.
Definition: qkd-key.h:76
@ OBSOLETE
Definition: qkd-key.h:82
@ TRANSFORMED
Definition: qkd-key.h:80
@ RESTORED
Definition: qkd-key.h:83
@ RESERVED
Definition: qkd-key.h:84
std::string ConsumeKeyString(void)
Get the key value and switch the key state to SERVED.
Definition: qkd-key.cc:227
uint8_t * GetKey(void)
Get key value in byte* format.
Definition: qkd-key.cc:242
QKDKeyState_e m_state
The key state.
Definition: qkd-key.h:270
uint64_t GetSizeInBits(void) const
Get the key size in bits.
Definition: qkd-key.cc:209
void MarkRestored()
Mark the key as restored.
Definition: qkd-key.cc:272
ns3::TracedCallback< const std::string &, const std::string & > m_stateTransitionTrace
The StateTransition trace source.
Definition: qkd-key.h:260
std::string m_key
Definition: qkd-key.h:268
void SetSize(uint64_t sizeInBytes)
Set the key size.
Definition: qkd-key.cc:216
void MarkServed()
Mark the key as served.
Definition: qkd-key.cc:258
std::string GetStateString() const
Get the current state of the key in a string format.
Definition: qkd-key.cc:356
Ptr< UniformRandomVariable > m_random
Definition: qkd-key.h:271
void SwitchToState(QKDKeyState_e state)
Change the state of the key.
Definition: qkd-key.cc:362
void MarkReserved()
Mark the key as reserved.
Definition: qkd-key.cc:280
uint64_t m_sizeInBytes
Definition: qkd-key.h:266
static TypeId GetTypeId(void)
Get the TypeId.
Definition: qkd-key.cc:40
Ptr< QKDKey > Copy(void) const
Copy the key.
Definition: qkd-key.cc:55
std::string m_id
Definition: qkd-key.h:264
Time m_timestamp
Definition: qkd-key.h:269
QKDKey(uint64_t keySize)
Constructor.
Definition: qkd-key.cc:68
std::string ToString(void)
Get the key value.
Definition: qkd-key.cc:235
QKDKeyState_e GetState() const
Get the current state of the key.
Definition: qkd-key.cc:311
uint64_t GetSize(void) const
Get the key size in bytes.
Definition: qkd-key.cc:202
void MarkTransformed()
Mark the key as transformed.
Definition: qkd-key.cc:286
static uint64_t m_globalUid
Definition: qkd-key.h:265
static Time Now(void)
Return the current simulation virtual time.
Definition: simulator.cc:195
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:103
a unique identifier for an interface.
Definition: type-id.h:59
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:922
double GetValue(double min, double max)
Get the next random value, as a double in the specified range .
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
Definition: fatal-error.h:165
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
#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:281
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:45
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.