A Discrete-Event Network Simulator
API
qkd-app-header.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2020 DOTFEESA www.tk.etf.unsa.ba
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 
21 #include "ns3/log.h"
22 #include "ns3/object-vector.h"
23 #include "ns3/pointer.h"
24 #include "ns3/uinteger.h"
25 #include "qkd-app-header.h"
26 
27 namespace ns3 {
28 
29 NS_LOG_COMPONENT_DEFINE ("QKDAppHeader");
30 
31 NS_OBJECT_ENSURE_REGISTERED (QKDAppHeader);
32 
33 QKDAppHeader::QKDAppHeader ():m_valid (true)
34 {
35  m_length = 0;
36  m_messageId = 0;
37  m_encryped = 0;
38  m_authenticated = 0;
39 }
40 
41 TypeId
43 {
44  static TypeId tid = TypeId ("ns3::QKDAppHeader")
45  .SetParent<Header> ()
46  .AddConstructor<QKDAppHeader> ()
47  ;
48  return tid;
49 }
50 
51 TypeId
53 {
54  return GetTypeId ();
55 }
56 
57 uint32_t
59 {
60  return 2 * sizeof(uint32_t)
61  + 2 * sizeof(uint8_t)
62  + 3 * 32 * sizeof(uint8_t); //@toDo: AuthTag is variable length!
63 }
64 
65 void
67 {
68  i.WriteHtonU32 ((uint32_t) m_length);
69  i.WriteHtonU32 ((uint32_t) m_messageId);
70  i.WriteU8 ((uint8_t) m_encryped);
71  i.WriteU8 ((uint8_t) m_authenticated);
72 
73  char tmpBuffer1 [m_encryptionKeyId.length()];
74  NS_LOG_FUNCTION( "CRYPTOTAGID:" << sizeof(tmpBuffer1)/sizeof(tmpBuffer1[0]) << " ---- " << m_encryptionKeyId.length() );
75  strcpy (tmpBuffer1, m_encryptionKeyId.c_str());
76  i.Write ((uint8_t *)tmpBuffer1, m_encryptionKeyId.length());
77 
78  char tmpBuffer2 [m_authenticationKeyId.length()];
79  NS_LOG_FUNCTION( "AUTHKEYID:" << sizeof(tmpBuffer2)/sizeof(tmpBuffer2[0]) << " ---- " << m_authenticationKeyId.length() );
80  strcpy (tmpBuffer2, m_authenticationKeyId.c_str());
81  i.Write ((uint8_t *)tmpBuffer2, m_authenticationKeyId.length());
82 
83  char tmpBuffer3 [m_authTag.length()];
84  NS_LOG_FUNCTION( "AUTHTAG:" << sizeof(tmpBuffer3)/sizeof(tmpBuffer3[0]) << " ---- " << m_authTag.length() );
85  strcpy (tmpBuffer3, m_authTag.c_str());
86  i.Write ((uint8_t *)tmpBuffer3, m_authTag.length());
87 }
88 
89 uint32_t
91 {
92 
94  m_valid = false;
95 
96  m_length = i.ReadNtohU32 ();
97  m_messageId = i.ReadNtohU32 ();
98  m_encryped = i.ReadU8 ();
99  m_authenticated = i.ReadU8 ();
100 
101  uint32_t len1 = 32;
102  char tmpBuffer1 [len1];
103  i.Read ((uint8_t*)tmpBuffer1, len1);
104  m_encryptionKeyId = std::string(tmpBuffer1).substr(0, len1);
105 
106  uint32_t len2 = 32;
107  char tmpBuffer2 [len2];
108  i.Read ((uint8_t*)tmpBuffer2, len2);
109  m_authenticationKeyId = std::string(tmpBuffer2).substr(0, len2);
110 
111  uint32_t len3 = 32;
112  char tmpBuffer3 [len3];
113  i.Read ((uint8_t*)tmpBuffer3, len3);
114  m_authTag = std::string(tmpBuffer3).substr(0, len3);
115 
116  NS_LOG_DEBUG ("Deserialize m_length: " << (uint32_t) m_length
117  << " m_messageId: " << (uint32_t) m_messageId
118  << " m_encryptionKeyId: " << m_encryptionKeyId
119  << " m_authenticationKeyId: " << m_authenticationKeyId
120  << " m_valid: " << (uint32_t) m_valid
121  << " m_authTag: " << m_authTag
122  );
123 
124  uint32_t dist = i.GetDistanceFrom (start);
125  NS_LOG_FUNCTION( this << dist << GetSerializedSize() );
126  NS_ASSERT (dist == GetSerializedSize ());
127  return dist;
128 }
129 
130 void
131 QKDAppHeader::Print (std::ostream &os) const
132 {
133  os << "\n"
134  << "MESSAGE ID: " << (uint32_t) m_messageId << "\t"
135  << "Length: " << (uint32_t) m_length << "\t"
136 
137  << "Authenticated: " << (uint32_t) m_authenticated << "\t"
138  << "Encrypted: " << (uint32_t) m_encryped << "\t"
139 
140  << "EncryptKeyID: " << m_encryptionKeyId << "\t"
141  << "AuthKeyID: " << m_authenticationKeyId << "\t"
142 
143  << "AuthTag: " << m_authTag << "\t\n";
144 
145 }
146 
147 bool
149 {
151 }
152 
153 std::ostream &
154 operator<< (std::ostream & os, QKDAppHeader const & h)
155 {
156  h.Print (os);
157  return os;
158 }
159 
160 
161 void
162 QKDAppHeader::SetLength (uint32_t value){
163 
164  NS_LOG_FUNCTION (this << value);
165  m_length = value;
166 }
167 uint32_t
169 
170  NS_LOG_FUNCTION (this << m_length);
171  return m_length;
172 }
173 
174 void
175 QKDAppHeader::SetMessageId (uint32_t value){
176 
177  NS_LOG_FUNCTION (this << value);
178  m_messageId = value;
179 }
180 uint32_t
182 
183  NS_LOG_FUNCTION (this << m_messageId);
184  return m_messageId;
185 }
186 
187 void
189 
190  NS_LOG_FUNCTION (this << value);
191 
192  NS_ASSERT(value.size() <= 32);
193  if (value.size() < 32) {
194  uint32_t diff = 32-value.size();
195  std::string newValue = std::string(diff, '0') + value;
196  m_encryptionKeyId = newValue;
197  } else
198  m_encryptionKeyId = value;
199 }
200 
201 std::string
203 
205  return m_encryptionKeyId;
206 }
207 
208 
209 void
211 
212  NS_LOG_FUNCTION (this << value);
213 
214  NS_ASSERT(value.size() <= 32);
215  if (value.size() < 32) {
216  uint32_t diff = 32-value.size();
217  std::string newValue = std::string(diff, '0') + value;
218  m_authenticationKeyId = newValue;
219  } else
220  m_authenticationKeyId = value;
221 }
222 
223 std::string
225 
227  return m_authenticationKeyId;
228 }
229 
230 void
231 QKDAppHeader::SetAuthTag (std::string value){
232 
233  NS_LOG_FUNCTION (this << value << value.size());
234  m_authTag = value;
235 }
236 
237 std::string
239 
240  NS_LOG_FUNCTION (this << m_authTag << m_authTag.size());
241  return m_authTag;
242 }
243 
244 void
245 QKDAppHeader::SetEncrypted (uint32_t value){
246 
247  NS_LOG_FUNCTION (this << value);
248  m_encryped = value;
249 }
250 uint32_t
252 
253  NS_LOG_FUNCTION (this << m_encryped);
254  return (uint32_t) m_encryped;
255 }
256 
257 void
259 
260  NS_LOG_FUNCTION (this << value);
261  m_authenticated = value;
262 }
263 uint32_t
265 
266  NS_LOG_FUNCTION (this << m_authenticated);
267  return (uint32_t) m_authenticated;
268 }
269 
270 
271 } // namespace ns3
iterator in a Buffer instance
Definition: buffer.h:99
void Write(uint8_t const *buffer, uint32_t size)
Definition: buffer.cc:954
void WriteU8(uint8_t data)
Definition: buffer.h:869
uint8_t ReadU8(void)
Definition: buffer.h:1021
void Read(uint8_t *buffer, uint32_t size)
Definition: buffer.cc:1124
void WriteHtonU32(uint32_t data)
Definition: buffer.h:924
uint32_t GetDistanceFrom(Iterator const &o) const
Definition: buffer.cc:788
uint32_t ReadNtohU32(void)
Definition: buffer.h:970
Protocol header serialization and deserialization.
Definition: header.h:43
virtual uint32_t Deserialize(Buffer::Iterator start)=0
Deserialize the object from a buffer iterator.
QKD app packet header that carries info about used encryption, auth tag and other.
bool m_valid
Is the header valid or corrupted?
bool operator==(QKDAppHeader const &o) const
Compare the two QKDApp packets.
QKDAppHeader()
Constructor.
std::string GetEncryptionKeyId(void) const
Read the encryption key identifier.
std::string GetAuthTag(void) const
Read the authentication tag.
uint32_t m_messageId
The message identifier field.
uint32_t GetSerializedSize() const
Get the serialized size of a packet.
std::string m_authTag
The authentication tag field.
uint32_t m_length
The message length field.
uint8_t m_authenticated
The authentication flag.
void SetMessageId(uint32_t value)
Set message identifier.
void SetEncryptionKeyId(std::string value)
Set the encryption key identifier.
uint8_t m_encryped
The encryption flag.
void SetEncrypted(uint32_t value)
Set the encrypted field.
uint32_t GetLength(void) const
Get message length.
void SetLength(uint32_t value)
Set the message length.
void SetAuthenticated(uint32_t value)
Set the authentication field.
uint32_t GetAuthenticated(void) const
Read the authentication field.
void SetAuthTag(std::string value)
Set the authentication tag.
std::string GetAuthenticationKeyId(void) const
Read the authentication key identifier.
uint32_t GetMessageId(void) const
Get message identifier.
TypeId GetInstanceTypeId() const
Get the type ID.
std::string m_authenticationKeyId
The authentication key identifier field.
void Print(std::ostream &os) const
Print the QKDApp packet.
void SetAuthenticationKeyId(std::string keyID)
Set the authentication key identifier.
static TypeId GetTypeId()
Get the type ID.
uint32_t GetEncrypted(void) const
Read the encrypted field.
std::string m_encryptionKeyId
The encryption key identifier field.
void Serialize(Buffer::Iterator start) const
Serialize the packet.
a unique identifier for an interface.
Definition: type-id.h:59
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:922
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition: assert.h:67
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:273
#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.
Definition: object-base.h:45
Every class exported by the ns3 library is enclosed in the ns3 namespace.
std::ostream & operator<<(std::ostream &os, const Angles &a)
Definition: angles.cc:139
def start()
Definition: core.py:1853