A Discrete-Event Network Simulator
API
onionmanager.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 
3 
4 /*
5 * Copyright (c) 2020 DLTLT
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation;
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 * Corresponding author: Niki Hrovatin <niki.hrovatin@famnit.upr.si>
21 */
22 
23 
24 #include "onionmanager.h"
25 
26 namespace ns3 {
27 
28 /* ... */
29 
30 //Zagotovi, da se registrira TypeId
31 NS_OBJECT_ENSURE_REGISTERED (OnionManager);
32 
33 TypeId
35 {
36  static TypeId tid =
37  TypeId ("ns3::OnionManager").SetParent<OnionRouting> ().SetGroupName ("OnionRouting");
38  return tid;
39 }
40 
41 OnionManager::OnionManager () : OnionRouting (crypto_box_SEALBYTES, Ipv4L3Protocol::PROT_NUMBER)
42 {
43 }
44 
46 {
47 }
48 
54 void
55 OnionManager::EncryptLayer (unsigned char *ciphertext, unsigned char *message, int len,
56  unsigned char *key) const
57 {
58  if (crypto_box_seal (ciphertext, message, len, key) != 0)
59  {
60  std::cout << "Error during encryption" << std::endl;
61  }
62 }
63 
64 void
65 OnionManager::DecryptLayer (unsigned char *innerLayer, unsigned char *onion, uint16_t onionLen,
66  unsigned char *pk, unsigned char *sk) const
67 {
68  if (crypto_box_seal_open (innerLayer, onion, onionLen, pk, sk) != 0)
69  {
70  std::cout << "messge corrupted or not for this node" << std::endl;
71  }
72 }
73 
79 //Generate new key pair
80 void
82 {
83  crypto_box_keypair (m_publickey, m_secretkey);
84 }
85 
86 //return public key
87 unsigned char *
89 {
90  // klici tako unsigned char* pk = kh.GetPK();
91  return m_publickey;
92 }
93 
94 //Get secret key
95 unsigned char *
97 {
98  return m_secretkey;
99 }
100 
101 //Return public key in string form
102 std::string
104 {
105  std::string strForm (&m_publickey[0], &m_publickey[0] + crypto_box_PUBLICKEYBYTES);
106  return strForm;
107 }
108 
109 //Return private key in string form
110 std::string
112 {
113  std::string strForm (&m_secretkey[0], &m_secretkey[0] + crypto_box_SECRETKEYBYTES);
114  return strForm;
115 }
116 
117 //set public key
118 void
119 OnionManager::SetPK (unsigned char *pk)
120 {
121  memcpy (&m_publickey, pk, crypto_box_PUBLICKEYBYTES);
122 }
123 
124 //set secret key
125 void
126 OnionManager::SetSK (unsigned char *sk)
127 {
128  memcpy (&m_secretkey, sk, crypto_box_SECRETKEYBYTES);
129 }
130 
136 //Convert string to unsigned char *
137 unsigned char *
139 {
140  unsigned char *out = new unsigned char[in.length ()];
141  memcpy (&out[0], &in[0], in.length ());
142  return out;
143 }
144 
145 //convert unsigned char to string
146 std::string
147 OnionManager::UcharToString (unsigned char *seq, int len)
148 {
149  std::string strForm (&seq[0], &seq[0] + len);
150  return strForm;
151 }
152 
153 //Convert ip to unsigned char *
154 unsigned char *
156 {
157  unsigned char *out = new unsigned char[4];
158  Ipv4Address (in).Serialize (&out[0]);
159  return out;
160 }
161 
162 } // namespace ns3
ns3::OnionManager::IpToBuff
unsigned char * IpToBuff(uint32_t in)
Convert an Ipv4 address given as an unsigned integer value to buffer array of 4Bytes.
Definition: onionmanager.cc:155
ns3::OnionManager::m_publickey
unsigned char m_publickey[crypto_box_PUBLICKEYBYTES]
the public encryption key
Definition: onionmanager.h:191
ns3::OnionManager::GetPKtoString
std::string GetPKtoString()
accessor
Definition: onionmanager.cc:103
ns3::OnionRouting
Abstract class for creation and decryption of Onion messages.
Definition: onion-routing.h:29
ns3
Definition: sensornode-helper.cc:26
ns3::OnionManager::m_secretkey
unsigned char m_secretkey[crypto_box_SECRETKEYBYTES]
the secret encryption key
Definition: onionmanager.h:192
ns3::OnionManager::UcharToString
std::string UcharToString(unsigned char *seq, int len)
Convert an array of unsigned chars to a std::string.
Definition: onionmanager.cc:147
ns3::OnionManager::StringToUchar
unsigned char * StringToUchar(std::string in)
Convert a string to an array of unsigned chars.
Definition: onionmanager.cc:138
ns3::OnionManager::GetSKtoString
std::string GetSKtoString()
accessor
Definition: onionmanager.cc:111
ns3::OnionManager::OnionManager
OnionManager()
Default constructor.
Definition: onionmanager.cc:41
ns3::OnionManager::GetPK
unsigned char * GetPK(void)
accessor
Definition: onionmanager.cc:88
ns3::OnionManager::GetTypeId
static TypeId GetTypeId(void)
Register this type.
Definition: onionmanager.cc:34
ns3::OnionManager::EncryptLayer
virtual void EncryptLayer(unsigned char *ciphertext, unsigned char *message, int len, unsigned char *key) const
Implementing encryption using the libsodium library.
Definition: onionmanager.cc:55
ns3::OnionManager::GenerateNewKeyPair
void GenerateNewKeyPair(void)
Generate a new public/private keypair using the libsodium library.
Definition: onionmanager.cc:81
ns3::OnionManager::GetSK
unsigned char * GetSK(void)
accessor
Definition: onionmanager.cc:96
onionmanager.h
ns3::OnionManager::SetSK
void SetSK(unsigned char *sk)
setter
Definition: onionmanager.cc:126
ns3::OnionManager::DecryptLayer
virtual void DecryptLayer(unsigned char *innerLayer, unsigned char *onion, uint16_t onionLen, unsigned char *pk, unsigned char *sk) const
Implementing decryption using the libsodium library.
Definition: onionmanager.cc:65
ns3::OnionManager::SetPK
void SetPK(unsigned char *pk)
setter
Definition: onionmanager.cc:119
ns3::OnionManager::~OnionManager
~OnionManager()
Default destructor.
Definition: onionmanager.cc:45