Ns3 Projects for B.E/B.Tech M.E/M.Tech PhD Scholars.  Phone-Number:9790238391   E-mail: ns3simulation@gmail.com

How to Implement Cryptographic Hashing in ns3

To implement the cryptographic hashing in ns3, by creating a secure interactive environment, the nodes can compute and attest cryptographic hashes of messages. We have implemented cryptographic hashing in the ns3tool, and we’re here to help you learn how to use this tool for your projects on popular topics. Make sure to keep in touch with ns3simulation.com! Here, we provide step-by-step guide to help you implement cryptographic hashing in ns3.

Step-by-Step Implementation:

Step 1: Set Up ns3 Environment

  1. Install ns3: Make sure that you have installed the ns3 and follow the installation guide suitable for your operating system.
  2. Familiarize Yourself with ns3: To know the basic concepts and simulation structure of ns3, we have to go through the ns3 tutorial.

Step 2: Set Up a Cryptographic Hash Library

  1. Install a Cryptographic Hash Library: To implement a different cryptographic hash functions (e.g., SHA-256), we can you use libraries like OpenSSL.
  2. Install the Library: Follow the installation instructions for OpenSSL. For instance:

sudo apt-get install libssl-dev

Step 3: Define the Network Topology

  1. Create a Simple Network: Use ns3, to determine a basic network topology using ns-3 and it includes creating nodes, setting up channels, and configuring IP addresses. We’ll use a point-to-point topology for simplicity.

#include “ns3/core-module.h”

#include “ns3/network-module.h”

#include “ns3/internet-module.h”

#include “ns3/point-to-point-module.h”

#include “ns3/applications-module.h”

#include <openssl/sha.h>

#include <iostream>

#include <iomanip>

#include <sstream>

using namespace ns3;

// Helper function to compute SHA-256 hash

std::string ComputeSHA256(const std::string &data) {

unsigned char hash[SHA256_DIGEST_LENGTH];

SHA256_CTX sha256;

SHA256_Init(&sha256);

SHA256_Update(&sha256, data.c_str(), data.size());

SHA256_Final(hash, &sha256);

std::stringstream ss;

for (int i = 0; i < SHA256_DIGEST_LENGTH; ++i) {

ss << std::hex << std::setw(2) << std::setfill(‘0’) << (int)hash[i];

}

return ss.str();

}

class HashingApp : public Application {

public:

HashingApp() {}

virtual ~HashingApp() {}

 

void Setup(Address address, uint16_t port) {

m_peerAddress = address;

m_peerPort = port;

}

private:

virtual void StartApplication() {

m_socket = Socket::CreateSocket(GetNode(), TypeId::LookupByName(“ns3::UdpSocketFactory”));

m_socket->Bind();

m_socket->Connect(InetSocketAddress(m_peerAddress, m_peerPort));

// Schedule the first packet transmission

Simulator::Schedule(Seconds(1.0), &HashingApp::SendPacket, this);

// Set up the receive callback

m_socket->SetRecvCallback(MakeCallback(&HashingApp::ReceivePacket, this));

}

virtual void StopApplication() {

if (m_socket) {

m_socket->Close();

m_socket = 0;

}

}

void SendPacket() {

std::string message = “Hello, this is a secure message!”;

std::string hash = ComputeSHA256(message);

std::string packetData = message + “:” + hash;

Ptr<Packet> packet = Create<Packet>((uint8_t*)packetData.c_str(), packetData.size());

m_socket->Send(packet);

// Schedule the next packet transmission

Simulator::Schedule(Seconds(1.0), &HashingApp::SendPacket, this);

}

void ReceivePacket(Ptr<Socket> socket) {

Ptr<Packet> packet = socket->Recv();

uint8_t buffer[1024];

packet->CopyData(buffer, packet->GetSize());

std::string packetData((char*)buffer, packet->GetSize());

size_t pos = packetData.find_last_of(‘:’);

std::string receivedMessage = packetData.substr(0, pos);

std::string receivedHash = packetData.substr(pos + 1);

std::string computedHash = ComputeSHA256(receivedMessage);

if (computedHash == receivedHash) {

std::cout << “Received valid message: ” << receivedMessage << std::endl;

} else {

std::cout << “Hash mismatch! Message integrity compromised.” << std::endl;

}

}

Ptr<Socket> m_socket;

Address m_peerAddress;

uint16_t m_peerPort;

};

int main(int argc, char *argv[]) {

NodeContainer nodes;

nodes.Create(2); // Example: 2 nodes (1 client, 1 server)

PointToPointHelper pointToPoint;

pointToPoint.SetDeviceAttribute(“DataRate”, StringValue(“1Gbps”));

pointToPoint.SetChannelAttribute(“Delay”, StringValue(“2ms”));

NetDeviceContainer devices;

devices = pointToPoint.Install(nodes);

InternetStackHelper stack;

stack.Install(nodes);

Ipv4AddressHelper address;

address.SetBase(“10.1.1.0”, “255.255.255.0”);

Ipv4InterfaceContainer interfaces = address.Assign(devices);

uint16_t port = 9;

Ptr<HashingApp> clientApp = CreateObject<HashingApp>();

clientApp->Setup(InetSocketAddress(interfaces.GetAddress(1), port), port);

nodes.Get(0)->AddApplication(clientApp);

clientApp->SetStartTime(Seconds(2.0));

clientApp->SetStopTime(Seconds(10.0));

Ptr<HashingApp> serverApp = CreateObject<HashingApp>();

serverApp->Setup(InetSocketAddress(Ipv4Address::GetAny(), port), port);

nodes.Get(1)->AddApplication(serverApp);

serverApp->SetStartTime(Seconds(1.0));

serverApp->SetStopTime(Seconds(10.0));

Simulator::Run();

Simulator::Destroy();

return 0;

}

Explanation:

  1. OpenSSL Integration:
    • Compute the SHA-256 hashes with the help of OpenSSL. Make sure, it is properly installed and connected.
  2. Helper Function:
    • The helper function called ComputeSHA256 computes the SHA-256 hash of a given data string and returns the hash as a hexadecimal string.
  3. HashingApp Class:
    • HashingApp class knobs the application logic, including sending and receiving hashed messages.
    • Application that includes peer address and port initialized by Setup method.
    • StartApplication method sets up the socket connection, schedules the first packet transmission, and sets up the receive callback.
    • SendPacket method calculates the hash of a message, combines the message and hash, and sends it to the peer node.
    • ReceivePacket method extracts the message and hash from the received packet, computes the hash of the message and validates the integrity of the message by relating the received and computed hashes.
  4. Main Function:
    • Creates a simple point-to-point network with 2 nodes (client and server).
    • Prepares the HashingApp applications on both client and server nodes.
    • The client sends hashed messages to the server, and the server authenticates the reliability of the received messages.

Compile and Run:

  1. Compile the Code: Make sure that OpenSSL is installed on your system. Then, compile the code using the following command:

g++ -std=c++11 -o ns3-hashing main.cc -lssl -lcrypto `pkg-config –cflags –libs ns3-dev`

  1. Run the Simulation: Execute the compiled program:

./ns3-hashing

This setup demonstrates a simple implementation of cryptographic hashing in ns3. You can expand it further to include more nodes, complex topologies, and additional cryptographic functionalities as needed.

Finally, we get to know the step-by-step details on how to implement the cryptographic hashing in the ns3 tool with the help of this script. We will provide any additional details about this topic through another script, as per your needs.