Skip to content

Commit

Permalink
dded encryption of zip and few other things
Browse files Browse the repository at this point in the history
  • Loading branch information
oleeng committed Jul 3, 2024
1 parent 6dac8ee commit 052908a
Show file tree
Hide file tree
Showing 4 changed files with 331 additions and 47 deletions.
4 changes: 3 additions & 1 deletion plugins/netlist_modifier/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ if(PL_NETLIST_MODIFIER OR BUILD_ALL_PLUGINS)
file(GLOB_RECURSE NETLIST_MODIFIER_SRC ${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp)
file(GLOB_RECURSE NETLIST_MODIFIER_PYTHON_SRC ${CMAKE_CURRENT_SOURCE_DIR}/python/*.cpp)

pkg_check_modules(Cryptopp REQUIRED IMPORTED_TARGET libcrypto++)

hal_add_plugin(netlist_modifier
SHARED
HEADER ${NETLIST_MODIFIER_INC}
SOURCES ${NETLIST_MODIFIER_SRC} ${NETLIST_MODIFIER_PYTHON_SRC}
LINK_LIBRARIES PUBLIC gui netlist_simulator_controller
LINK_LIBRARIES PUBLIC gui netlist_simulator_controller PkgConfig::Cryptopp
PYDOC SPHINX_DOC_INDEX_FILE ${CMAKE_CURRENT_SOURCE_DIR}/documentation/netlist_modifier.rst
)
endif()
195 changes: 161 additions & 34 deletions plugins/netlist_modifier/src/netlist_modifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,19 @@
#include "hal_core/netlist/netlist_writer/netlist_writer_manager.h"
#include "hal_core/netlist/project_manager.h"

#include "hal_core/netlist/persistent/netlist_serializer.h"

#include <deque>
#include <filesystem>
#include <iostream>
#include <fstream>

#include <JlCompress.h>

#include <cryptopp/aes.h>
#include <cryptopp/modes.h>
#include <cryptopp/filters.h>
#include <cryptopp/osrng.h>

namespace hal
{
Expand Down Expand Up @@ -55,6 +65,7 @@ namespace hal
std::set<std::string> retval;
retval.insert("hal_gui");
retval.insert("verilog_writer");
retval.insert("verilog_writer");
return retval;
}

Expand All @@ -76,23 +87,6 @@ namespace hal
return false;
}

/*
// dynamicly creating missing gatetype does not work as returned gatelib by netlist is const and adding type does not work
if (!new_gate_type){
// gate type does not exist in gatelib
new_gate_type = netlist->get_gate_library()->create_gate_type("UNKNOWN_"+std::to_string(num_of_in)+"IN_"+std::to_string(num_of_out)+"OUT");
for (int i = 0; i < num_of_in; i++)
{
new_gate_type->create_pin("IN_"+std::to_string(i), PinDirection::input);
}
for (int i = 0; i < num_of_out; i++)
{
new_gate_type->create_pin("OUT_"+std::to_string(i), PinDirection::output);
}
}*/

std::string gate_name = "UNKNOWN_" + std::to_string(gate->get_id());
u32 gate_id = gate->get_id();

Expand Down Expand Up @@ -131,33 +125,165 @@ namespace hal
return true;
}

bool NetlistModifierPlugin::modify_in_place()
{
GuiApi* guiAPI = new GuiApi();
std::string encryptAES(const std::string& plaintext, const std::string& key) {
if(key.size() != static_cast<int>(CryptoPP::AES::DEFAULT_KEYLENGTH)){
log_error("netlist_modifier", "Key needs to be "+std::to_string(static_cast<int>(CryptoPP::AES::DEFAULT_KEYLENGTH))+" bytes long!");
return "";
}
std::string ciphertext;

/*
// loading copy does not work as one can not add a type to the second gatelib and adding a gate of this type to the netlist if the gatelib of the gui does not have this type
CryptoPP::AES::Encryption aesEncryption((const CryptoPP::byte*)key.data(), CryptoPP::AES::DEFAULT_KEYLENGTH);
CryptoPP::CBC_Mode_ExternalCipher::Encryption cbcEncryption(aesEncryption, (const CryptoPP::byte*)key.data());

ProjectManager* pm = ProjectManager::instance();
CryptoPP::StringSource encryptor(plaintext, true, new CryptoPP::StreamTransformationFilter(cbcEncryption, new CryptoPP::StringSink(ciphertext)));

return ciphertext;
}

std::string decryptAES(const std::string& ciphertext, const std::string& key) {
if(key.size() != static_cast<int>(CryptoPP::AES::DEFAULT_KEYLENGTH)){
log_error("netlist_modifier", "Key needs to be "+std::to_string(static_cast<int>(CryptoPP::AES::DEFAULT_KEYLENGTH))+" bytes long!");
return "";
}
std::string decryptedtext;
CryptoPP::AES::Decryption aesDecryption((const CryptoPP::byte*)key.data(), CryptoPP::AES::DEFAULT_KEYLENGTH);
CryptoPP::CBC_Mode_ExternalCipher::Decryption cbcDecryption(aesDecryption, (const CryptoPP::byte*)key.data());

CryptoPP::StringSource decryptor(ciphertext, true, new CryptoPP::StreamTransformationFilter(cbcDecryption, new CryptoPP::StringSink(decryptedtext)));

return decryptedtext;
}

bool create_encrypted_zip(){
ProjectManager* pm = ProjectManager::instance();
std::filesystem::path project_dir_path(pm->get_project_directory().string());

if (!std::filesystem::exists(project_dir_path/"generated/gatelib_obfuscated.hgl"))
{
if (std::filesystem::exists(project_dir_path/"generated") && !std::filesystem::is_directory(project_dir_path/"generated")){
log_error("netlist_modifier", "A file called 'generated' in the project directory exists but it is expected to be a directory!");
return false;
}
std::filesystem::create_directories(project_dir_path/"generated");
// create tmp original netlist file
netlist_serializer::serialize_to_file(gNetlist, project_dir_path / "original/tmp/original.hal");

// create tmp ini file
std::string ini_content = R"([section1]
; wether to use t probe (n arbitrary probes) or scan chain (n probes at FF output)
; true = t probe
; false = scan chain
t_probe=true
max_probes=5)";

// Open the file for writing
std::ofstream ini_outFile((project_dir_path / "original/tmp/settings.ini").c_str());

std::filesystem::copy(gNetlist->get_gate_library()->get_path(), project_dir_path/"generated/gatelib_obfuscated.hgl");
// Check if the file is successfully opened
if (!ini_outFile.is_open()) {
log_error("netlist_modifier", "Error opening new ini file!");
return false;
}

// Write the content to the file
ini_outFile << ini_content;

// Close the file
ini_outFile.close();

// create tmp zip file
QuaZip zip(QString::fromStdString(project_dir_path / "original/tmp/original.zip"));

if (!zip.open(QuaZip::mdCreate)) {
log_error("netlist_modifier", "Failed to create ZIP archive!");
return false;
}


QFile netlist_file(QString::fromStdString(project_dir_path / "original/tmp/original.hal"));
if (!netlist_file.open(QIODevice::ReadOnly)) {
log_error("netlist_modifier", "Failed to open file!");
return false;
}
QuaZipFile netlist_zip_outFile(&zip);
netlist_zip_outFile.open(QIODevice::WriteOnly, QuaZipNewInfo(QFileInfo(netlist_file.fileName()).fileName()));

netlist_zip_outFile.write(netlist_file.readAll());

netlist_zip_outFile.close();
netlist_file.close();

QFile ini_file(QString::fromStdString(project_dir_path / "original/tmp/settings.ini"));
if (!ini_file.open(QIODevice::ReadOnly)) {
log_error("netlist_modifier", "Failed to open file!");
return false;
}
QuaZipFile ini_zip_outFile(&zip);
ini_zip_outFile.open(QIODevice::WriteOnly, QuaZipNewInfo(QFileInfo(ini_file.fileName()).fileName()));

GateLibrary* lib = gate_library_manager::load(project_dir_path/"generated/gatelib_obfuscated.hgl");*/
ini_zip_outFile.write(ini_file.readAll());

ini_zip_outFile.close();
ini_file.close();

zip.close();

// read content of zip file
std::ifstream in_file_zip(project_dir_path / "original/tmp/original.zip");
std::stringstream buffer;
buffer << in_file_zip.rdbuf();

std::string key = "0123456789abcdef"; // 16-byte key for AES-128
std::string enc_content_zip = encryptAES(buffer.str(), key);

// save back encrypted zip file
std::ofstream out_file_zip(project_dir_path / "original/original.encrypted");

// Check if the file is successfully opened
if (!out_file_zip.is_open()) {
log_error("netlist_modifier", "Error opening new encrypted zip file!");
return false;
}
out_file_zip << enc_content_zip;
out_file_zip.close(); // Close the file

// delete tmp folder
try{
std::filesystem::remove(project_dir_path / "original/tmp/settings.ini");
std::filesystem::remove(project_dir_path / "original/tmp/original.zip");
std::filesystem::remove(project_dir_path / "original/tmp/original.hal");
std::filesystem::remove(project_dir_path / "original/tmp");
} catch (const std::filesystem::filesystem_error& e){
log_error("netlist_modifier", "Failed to delete tmp directory");
std::cerr << e.what() << std::endl;
}




/*std::filesystem::path tmp_directory(project_dir_path / "original/tmp");
if (std::filesystem::exists(tmp_directory) && std::filesystem::is_directory(tmp_directory)) {
try {
std::filesystem::remove_all(tmp_directory); // Recursively delete directory and its contents
std::cout << "Tmp directory successfully deleted." << std::endl;
} catch (const std::filesystem::filesystem_error& e) {
std::cerr << "Failed to delete tmp directory: " << e.what() << std::endl;
}
}*/

return true;
}

bool NetlistModifierPlugin::modify_in_place()
{
GuiApi* guiAPI = new GuiApi();

std::vector<Gate*> gates = guiAPI->getSelectedGates();

// save original netlist if it does not contain any UNKNOWN gates
bool contains_unknown = false;
for (Gate* gate: gNetlist->get_gates()){
if(gate->get_type()->get_name().find("UNKNOWN_") != std::string::npos){
contains_unknown = true;
}
}
if(!contains_unknown){
create_encrypted_zip();
}

for (Gate* gate : gates)
{
if (!replace_gate_in_netlist(gNetlist, gate))
Expand Down Expand Up @@ -186,9 +312,10 @@ namespace hal
std::filesystem::create_directories(project_dir_path / "generated");
}

netlist_writer_manager::write(gNetlist, project_dir_path / "generated/generated_netlist_obfuscated.v");
netlist_serializer::serialize_to_file(gNetlist, project_dir_path / "generated/generated_netlist_obfuscated.hal");
// netlist_writer_manager::write(gNetlist, project_dir_path / "generated/generated_netlist_obfuscated.hal");

std::filesystem::copy(gNetlist->get_gate_library()->get_path(), project_dir_path / "generated/generated_gatelib_obfuscated.hgl");
std::filesystem::copy(gNetlist->get_gate_library()->get_path(), project_dir_path / "generated/generated_gatelib_obfuscated.hgl", std::filesystem::copy_options::overwrite_existing);

return true;
}
Expand Down
4 changes: 3 additions & 1 deletion plugins/netlist_simulator_study/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ if(PL_NETLIST_SIMULATOR_STUDY OR BUILD_ALL_PLUGINS)
file(GLOB_RECURSE NETLIST_SIMULATOR_STUDY_SRC ${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp)
file(GLOB_RECURSE NETLIST_SIMULATOR_STUDY_PYTHON_SRC ${CMAKE_CURRENT_SOURCE_DIR}/python/*.cpp)

pkg_check_modules(Cryptopp REQUIRED IMPORTED_TARGET libcrypto++)

hal_add_plugin(netlist_simulator_study
SHARED
HEADER ${NETLIST_SIMULATOR_STUDY_INC}
SOURCES ${NETLIST_SIMULATOR_STUDY_SRC} ${NETLIST_SIMULATOR_STUDY_PYTHON_SRC}
LINK_LIBRARIES PUBLIC gui netlist_simulator_controller
LINK_LIBRARIES PUBLIC gui netlist_simulator_controller PkgConfig::Cryptopp
PYDOC SPHINX_DOC_INDEX_FILE ${CMAKE_CURRENT_SOURCE_DIR}/documentation/netlist_simulator_study.rst
)
endif()
Loading

0 comments on commit 052908a

Please sign in to comment.