Skip to content

Commit

Permalink
cleanup and fixed a few bugs like keep same position etc.
Browse files Browse the repository at this point in the history
  • Loading branch information
oleeng committed Jul 22, 2024
1 parent 72b362e commit 2851122
Show file tree
Hide file tree
Showing 17 changed files with 716 additions and 560 deletions.
Original file line number Diff line number Diff line change
@@ -1,25 +1,28 @@
#pragma once

#include <QDialog>

#include <QPushButton>
#include <QRadioButton>
#include <QSpinBox>
#include <QPushButton>

namespace hal{
class IniSettingsPopup : public QDialog {
namespace hal
{
class IniSettingsPopup : public QDialog
{
Q_OBJECT

public:
explicit IniSettingsPopup(QWidget *parent = nullptr);
explicit IniSettingsPopup(QWidget* parent = nullptr);
int get_selected_probing_model();
int get_probe_limit();

private:
QRadioButton *t_probe_option;
QRadioButton *scan_chain_option;
QSpinBox *probe_limit;
QPushButton *okButton;
QPushButton *cancelButton;
QRadioButton* t_probe_option;
QRadioButton* scan_chain_option;
QSpinBox* probe_limit;
QPushButton* okButton;
QPushButton* cancelButton;

void acceptSelection();
};
}
} // namespace hal
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
#pragma once
#include "hal_core/defines.h"
#include "hal_core/netlist/netlist.h"
#include "hal_core/plugin_system/gui_extension_interface.h"
#include "hal_core/plugin_system/plugin_interface_base.h"

#define SECRET_PASSWORD "test12345"

namespace hal
{
extern Netlist* gNetlist;

class GuiExtensionNetlistModifier;

class PLUGIN_API NetlistModifierPlugin : public BasePluginInterface
Expand All @@ -15,8 +18,9 @@ namespace hal
static bool replace_gate_in_netlist(Netlist* netlist, Gate* gate);
static std::string obfuscated_gate_name(int num_in, int num_out, int num_io = 0);
bool modify_gatelibrary();
bool create_encrypted_zip(std::string password, int probe_type, int probe_limit, std::string salt = "", bool existing_hal_file=false);
bool create_encrypted_zip(std::string password, int probe_type, int probe_limit, std::string salt = "", bool existing_hal_file = false);
bool update_encrypted_zip(std::string password, int probe_type, int probe_limit);
bool modify_in_place(int probe_type, int probe_limit);

public:
std::string get_name() const override;
Expand All @@ -30,7 +34,6 @@ namespace hal

NetlistModifierPlugin();

bool modify_in_place(int probe_type, int probe_limit);
void open_popup();
};

Expand Down
110 changes: 65 additions & 45 deletions plugins/netlist_modifier/src/encrypted_zip.cpp
Original file line number Diff line number Diff line change
@@ -1,38 +1,36 @@
#include "netlist_modifier/netlist_modifier.h"

#include "hal_core/netlist/project_manager.h"

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

#include <sstream>
#include <fstream>
#include "hal_core/netlist/project_manager.h"
#include "netlist_modifier/netlist_modifier.h"

#include <JlCompress.h>
#include <fstream>
#include <sstream>

namespace hal
{
extern Netlist* gNetlist;

std::string gen_random_string(int len){
std::string gen_random_string(int len)
{
const std::string characters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
std::string random_string;
random_string.reserve(len);
srand((unsigned)time(NULL));

srand((unsigned)time(NULL));

for (int i = 0; i < len; i++)
{
random_string += characters[rand() % (characters.size())];
}

return random_string;
}

std::string gen_salted_password(std::string password, std::string salt){
std::string gen_salted_password(std::string password, std::string salt)
{
return password + salt;
}

std::string generate_ini_content(int probe_type, int probe_limit){
std::string generate_ini_content(int probe_type, int probe_limit)
{
std::ostringstream ini_oss;
ini_oss << "[section1]\n"
<< "; select what probe type to use\n"
Expand All @@ -43,50 +41,61 @@ namespace hal
return ini_oss.str();
}

bool NetlistModifierPlugin::create_encrypted_zip(std::string password, int probe_type, int probe_limit, std::string salt, bool existing_hal_file){
bool NetlistModifierPlugin::create_encrypted_zip(std::string password, int probe_type, int probe_limit, std::string salt, bool existing_hal_file)
{
ProjectManager* pm = ProjectManager::instance();
std::filesystem::path project_dir_path(pm->get_project_directory().string());

if(salt == ""){
if (salt == "")
{
salt = gen_random_string(20);
}
std::string salted_password = gen_salted_password(password, salt);

// create original folder if missing
if (std::filesystem::exists(project_dir_path / "original/")){
if (!std::filesystem::is_directory(project_dir_path / "original/")) {
if (std::filesystem::exists(project_dir_path / "original/"))
{
if (!std::filesystem::is_directory(project_dir_path / "original/"))
{
log_error("netlist_modifier", "A file with the name 'original' already exists.");
return false;
}
}else{
if (!std::filesystem::create_directory(project_dir_path / "original/")) {
}
else
{
if (!std::filesystem::create_directory(project_dir_path / "original/"))
{
log_error("netlist_modifier", "Could not create original folder!");
return false;
}
}

// create tmp original netlist file
if(!existing_hal_file){
if (!existing_hal_file)
{
netlist_serializer::serialize_to_file(gNetlist, project_dir_path / "original/original.hal");
}

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

if (!zip.open(QuaZip::mdCreate)) {
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/original.hal"));
if (!netlist_file.open(QIODevice::ReadOnly)) {
if (!netlist_file.open(QIODevice::ReadOnly))
{
log_error("netlist_modifier", "Failed to open original.hal file!");
return false;
}
QuaZipFile netlist_zip_outFile(&zip);
if (!netlist_zip_outFile.open(QIODevice::WriteOnly, QuaZipNewInfo(QFileInfo(netlist_file.fileName()).fileName()), salted_password.c_str())) {
if (!netlist_zip_outFile.open(QIODevice::WriteOnly, QuaZipNewInfo(QFileInfo(netlist_file.fileName()).fileName()), salted_password.c_str()))
{
log_error("File could not be added with encryption!");
netlist_file.close();
return false;
}

Expand All @@ -97,39 +106,45 @@ namespace hal

// create tmp ini file
std::string ini_content = generate_ini_content(probe_type, probe_limit);

QuaZipFile ini_zip_outFile(&zip);
if (!ini_zip_outFile.open(QIODevice::WriteOnly, QuaZipNewInfo("settings.ini"), salted_password.c_str())) {
if (!ini_zip_outFile.open(QIODevice::WriteOnly, QuaZipNewInfo("settings.ini"), salted_password.c_str()))
{
log_error("File could not be added with encryption!");
return false;
}
ini_zip_outFile.write(ini_content.c_str());
ini_zip_outFile.close();

QuaZipFile salt_zip_outFile(&zip);
if (!salt_zip_outFile.open(QIODevice::WriteOnly, QuaZipNewInfo("salt.encrypt"), password.c_str())) {
if (!salt_zip_outFile.open(QIODevice::WriteOnly, QuaZipNewInfo("salt.encrypt"), password.c_str()))
{
log_error("File could not be added with encryption!");
return false;
}

salt_zip_outFile.write(salt.c_str());
salt_zip_outFile.close();

zip.close();

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

return true;
}

bool NetlistModifierPlugin::update_encrypted_zip(std::string password, int probe_type, int probe_limit){
bool NetlistModifierPlugin::update_encrypted_zip(std::string password, int probe_type, int probe_limit)
{
ProjectManager* pm = ProjectManager::instance();
std::filesystem::path project_dir_path(pm->get_project_directory().string());

Expand All @@ -143,25 +158,26 @@ namespace hal

zip.setCurrentFile("salt.encrypt");
QuaZipFile zipFile(&zip);
if (!zipFile.open(QIODevice::ReadOnly, password.c_str())) {
if (!zipFile.open(QIODevice::ReadOnly, password.c_str()))
{
log_error("netlist_simulator_study", "Failed to open salt.encrypt file");
zip.close();
return false;
}

QByteArray salt_buffer = zipFile.readAll();
std::string salt = std::string(salt_buffer.constData(), salt_buffer.size());
QByteArray salt_buffer = zipFile.readAll();
std::string salt = std::string(salt_buffer.constData(), salt_buffer.size());
std::string salted_password = gen_salted_password(password, salt);
zipFile.close();

zip.setCurrentFile("original.hal");
if (!zipFile.open(QIODevice::ReadOnly, salted_password.c_str())) {
if (!zipFile.open(QIODevice::ReadOnly, salted_password.c_str()))
{
log_error("netlist_simulator_study", "Failed to open original.hal file");
zip.close();
return false;
}

QByteArray hal_buffer = zipFile.readAll();
QByteArray hal_buffer = zipFile.readAll();
std::string original_hal = std::string(hal_buffer.constData(), hal_buffer.size());
zipFile.close();
zip.close();
Expand All @@ -170,7 +186,8 @@ namespace hal
std::ofstream hal_out_file((project_dir_path / "original/original.hal").c_str());

// Check if the file is successfully opened
if (!hal_out_file.is_open()) {
if (!hal_out_file.is_open())
{
log_error("netlist_modifier", "Error opening tmp original.hal file!");
return false;
}
Expand All @@ -182,14 +199,17 @@ namespace hal
hal_out_file.close();

// delete tmp file
try{
try
{
std::filesystem::remove(project_dir_path / "original/original.zip");
} catch (const std::filesystem::filesystem_error& e){
}
catch (const std::filesystem::filesystem_error& e)
{
log_error("netlist_modifier", "Failed to delete old zip");
std::cerr << e.what() << std::endl;
return false;
}

return create_encrypted_zip(password, probe_type, probe_limit, salt, true);
}
}
} // namespace hal
24 changes: 10 additions & 14 deletions plugins/netlist_modifier/src/modify_gatelibrary.cpp
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
#include "netlist_modifier/netlist_modifier.h"

#include "hal_core/netlist/project_manager.h"

#include "hal_core/netlist/gate_library/gate_library_writer/gate_library_writer_manager.h"
#include "hal_core/netlist/project_manager.h"
#include "netlist_modifier/netlist_modifier.h"

#include <rapidjson/filereadstream.h>
#include <rapidjson/filewritestream.h>
#include <rapidjson/writer.h>

namespace hal
{
const char* OBFUSCATED = "_obfuscated";
const char* OBFUSCATED = "_obfuscated";
const char* GATE_LIB_TAG = "gate_library";

extern Netlist* gNetlist;

bool NetlistModifierPlugin::modify_gatelibrary()
{
ProjectManager* pm = ProjectManager::instance();
Expand Down Expand Up @@ -56,7 +52,7 @@ namespace hal
else
{
log_warning("netlist_modifier", "Cannot find mandatory '{}' tag in project file '{}'.", GATE_LIB_TAG, projFilePath.string());
return false; // gate library entry missing in project file
return false; // gate library entry missing in project file
}

// yes, we know what we are doing when casting away const ;-)
Expand All @@ -65,7 +61,7 @@ namespace hal
GateLibrary* gl = const_cast<GateLibrary*>(gNetlist->get_gate_library());

// map gate type categories by number of pins
std::unordered_map<u32,int> pinCountMap;
std::unordered_map<u32, int> pinCountMap;
for (auto const& [key, gt] : gl->get_gate_types())
{
int count[5] = {0, 0, 0, 0, 0};
Expand All @@ -78,17 +74,17 @@ namespace hal
}

// create dummy gate types with appropriate number of pins
for (auto const& [pc,count] : pinCountMap)
for (auto const& [pc, count] : pinCountMap)
{
int inCount = pc & 0x3FF;
int outCount = (pc >> 10) & 0x3FF;
int ioCount = (pc >> 20) & 0x3FF;
GateType* gt = gl->create_gate_type(obfuscated_gate_name(inCount, outCount, ioCount));
for (int i=0; i<inCount; i++)
for (int i = 0; i < inCount; i++)
gt->create_pin("I" + std::to_string(i), PinDirection::input, PinType::none, true);
for (int i=0; i<outCount; i++)
for (int i = 0; i < outCount; i++)
gt->create_pin("O" + std::to_string(i), PinDirection::output, PinType::none, true);
for (int i=0; i<ioCount; i++)
for (int i = 0; i < ioCount; i++)
gt->create_pin("IO" + std::to_string(i), PinDirection::inout, PinType::none, true);
}

Expand Down Expand Up @@ -117,4 +113,4 @@ namespace hal

return true;
}
}
} // namespace hal
Loading

0 comments on commit 2851122

Please sign in to comment.