Skip to content

Read/Write Encrypted XML Files #3125

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 11 commits into from
Closed
5 changes: 5 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,13 @@ option(SPEC_CPU "Enable SPEC CPU v8 support" OFF)
#Allow the user to decide whether to compile the graphics library
set(VPR_USE_EZGL "auto" CACHE STRING "Specify whether vpr uses the graphics library")
set_property(CACHE VPR_USE_EZGL PROPERTY STRINGS auto off on)

# Allow the user to decide whether to compile the capnproto library.
option(VTR_ENABLE_CAPNPROTO "Enable capnproto binary serialization support in VPR." ON)

# Allow the user to decide whether to compile encryption/decryption libraries.
option(VTR_ENABLE_ENCRYPTION "Enable encryption/decryption support in VPR." OFF)

#Allow the user to decide whether to compile the server module
option(VPR_USE_SERVER "Specify whether vpr enables the server mode" ON)

Expand Down
4 changes: 4 additions & 0 deletions libs/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,8 @@ add_subdirectory(librtlnumber)
if(${VTR_ENABLE_CAPNPROTO})
add_subdirectory(libvtrcapnproto)
endif()
if(${VTR_ENABLE_ENCRYPTION})
add_subdirectory(libencrypt)
add_subdirectory(libdecrypt)
endif()
add_subdirectory(librrgraph)
4 changes: 2 additions & 2 deletions libs/libarchfpga/src/read_xml_arch_file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -398,10 +398,10 @@ void XmlReadArch(const char* ArchFile,
pugi::xml_node Next;
ReqOpt POWER_REQD, SWITCHBLOCKLIST_REQD;

if (!vtr::check_file_name_extension(ArchFile, ".xml")) {
if ((vtr::check_file_name_extension(ArchFile, ".xml") == false) && (vtr::check_file_name_extension(ArchFile, ".xmle") == false)) {
VTR_LOG_WARN(
"Architecture file '%s' may be in incorrect format. "
"Expecting .xml format for architecture files.\n",
"Expecting .xml or .xmle format for architecture files.\n",
ArchFile);
}

Expand Down
98 changes: 98 additions & 0 deletions libs/libdecrypt/ README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
# libdecrypt
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this a fully custom library, or something from the internet? I feel like encrypting XML files should not be complicated enough to warent its own library? Are we sure a library for this does not already exist?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with you and have noted both your and Soheil’s concerns. If you’re okay with it, I suggest we prioritize completing the full integration with OpenFPGA first, and we can revisit and roll back any changes we don’t want afterward.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure I agree. I thought I understood from the meeting last Friday that the goal was to bring in the necessary data structures first, then the necessary features, then the bonus features last. This feels more like a bonus feature which requires a bit more discussion.

My worry about this is that this is another thing that we will need to support in VTR; and encryption can come with a good amount of baggage. We need to really think about how much we want this feature.


libdecrypt is a C++ library for decrypting encrypted files. It provides a simple interface to decrypt files using OpenSSL for encryption and decryption operations and pugixml for XML parsing.

## Features

- Decrypts encrypted files using RSA encryption algorithm.
- Supports loading private key from PEM string.
- Retrieves encrypted data and session key from XML file.
- Decrypts session key using RSA private key.
- Decrypts XML string using the decrypted session key.
- Outputs the decrypted content as a string.

## Installation

1. Build the library using CMake

cd libdecrypt
mkdir build
cd build
cmake ..
make
2. Install the library (optional):

make install
## Dependencies

Make sure you have the following dependencies installed on your system:

- **OpenSSL**: The library depends on OpenSSL for encryption and decryption operations. Make sure you have OpenSSL installed on your system.

- **pugixml**: The library uses pugixml for XML parsing. It is included as a submodule in the project.

## Usage

To use the `libdecrypt` library in your C++ projects, follow the steps below:

1. Include the `decryption.h` header file in your source code:

```cpp
#include "decryption.h"

2. Create a `Decryption` object with the path to the encrypted file:

std::string encryptedFile = "path/to/encrypted/file";
Decryption decryption(encryptedFile);

3. Decrypt the contents of the encrypted file:

decryption.decryptFile();

4. Retrieve the decrypted content as a string:

std::string decryptedContent = decryption.getDecryptedContent();

5. Optional: Write the decrypted content to a file:

std::ofstream outputFile("output.txt");
outputFile << decryptedContent;
outputFile.close();

## Configuration

To configure `libdecrypt` for your specific use case, follow these steps:

### Private Key:

- **Option 1: Define `PRIVATE_KEY` in the code:**
- Locate the `decryption.h` file in the project.
- Uncomment the `#define PRIVATE_KEY` line.
- Replace the dummy private key string with your actual private key in PEM format.

- **Option 2: Provide a `private_key.pem` file:**
- Place your private key file in the project directory.
- Make sure the file is named `private_key.pem`.
- `libdecrypt` will automatically load the private key from this file.

### Passphrase:

- **Option 1: Define `PASSPHRASE` in the code:**
- Locate the `decryption.h` file in the project.
- Uncomment the `#define PASSPHRASE` line.
- Replace the empty string with your actual passphrase.

- **Option 2: Provide a `config.txt` file:**
- Create a plain text file named `config.txt` in the project directory.
- Write your passphrase in the file.
- `libdecrypt` will read the passphrase from this file.

Make sure to configure the private key and passphrase according to your specific requirements before using the `libdecrypt` library.

## License

This project is licensed under the MIT License

## Contributing

Contributions are welcome! If you find any issues or have suggestions for improvements, please feel free to open an issue or submit a pull request. We appreciate your contributions to make this project better.
49 changes: 49 additions & 0 deletions libs/libdecrypt/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
cmake_minimum_required(VERSION 3.16)
project(libdecrypt)

file(READ "private_key.pem" PRIVATE_KEY_CONTENTS)
file(READ "config.txt" PASSPHRASE_CONTENTS)

string(REPLACE "\n" "\\n" PRIVATE_KEY_CONTENTS "${PRIVATE_KEY_CONTENTS}")
string(REPLACE "\n" "\\n" PASSPHRASE_CONTENTS "${PASSPHRASE_CONTENTS}")
set(PRIVATE_KEY "${PRIVATE_KEY_CONTENTS}")
set(PASS_PHRASE "${PASSPHRASE_CONTENTS}")
add_compile_definitions(PASS_PHRASE="${PASS_PHRASE}")
add_compile_definitions(PRIVATE_KEY="${PRIVATE_KEY}")
find_package(PkgConfig REQUIRED)
pkg_search_module(OPENSSL REQUIRED openssl)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line makes OpenSSL a required library for all of VTR. Be aware. I would recommend creating a top-level CMake variable to allow the user to build with this feature or not. I think this is a large dependency for this feature.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe that now, with the addition of the new variable VTR_ENABLE_ENCRYPTION to indicate whether the user wants encryption, this CMake will only be triggered if the user wants.


if( OPENSSL_FOUND )
include_directories(${OPENSSL_INCLUDE_DIRS})
message(STATUS "Using OpenSSL ${OPENSSL_VERSION}")
else()
message("SSL not found")
# Error; with REQUIRED, pkg_search_module() will throw an error by it's own
endif()
# Source files and library
file(GLOB_RECURSE LIB_SOURCES src/*.cpp)
file(GLOB_RECURSE LIB_HEADERS src/*.hpp src/*.h)
set(LIB_INCLUDE_DIRS "")
foreach (_headerFile ${LIB_HEADERS})
get_filename_component(_dir ${_headerFile} PATH)
list(APPEND LIB_INCLUDE_DIRS ${_dir})
endforeach ()
list(REMOVE_DUPLICATES LIB_INCLUDE_DIRS)



# Create the library
add_library(libdecrypt STATIC
${LIB_HEADERS}
${LIB_SOURCES})
target_include_directories(libdecrypt PUBLIC ${LIB_INCLUDE_DIRS})
set_target_properties(libdecrypt PROPERTIES PREFIX "") # Avoid extra 'lib' prefix

target_link_libraries(libdecrypt
libpugixml
${OPENSSL_LIBRARIES}
)
install(TARGETS libdecrypt DESTINATION bin)
install(FILES ${LIB_HEADERS} DESTINATION include/libdecrypt)

add_subdirectory(test)
1 change: 1 addition & 0 deletions libs/libdecrypt/config.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Write your passphrase in the file
30 changes: 30 additions & 0 deletions libs/libdecrypt/private_key.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
-----BEGIN ENCRYPTED PRIVATE KEY-----
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it common to include a private key in a repository like this? Shouldnt it be public?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It’s just a placeholder for the user to insert the private key they intend to use.

MIIFLTBXBgkqhkiG9w0BBQ0wSjApBgkqhkiG9w0BBQwwHAQIxiWq9/gKEI4CAggA
MAwGCCqGSIb3DQIJBQAwHQYJYIZIAWUDBAEqBBCM12wmpHSRMA8no6WSnyiRBIIE
0HWEpZCWG230mIezxiEQGtR/W0HliJIU/fkJukt7hyqt6ECwcr9te80KIcqftUsH
64d8GBwCAfIpP5gDtojsm2XwM6acdGyFO4ZFyF2yO+kO9kdUVPQbe6Um2wYFGpXq
wMeMclkL+ZRxhttu6j9RsB9iQKqh13/Nw2x7y2xCDK0lkbHKprdO466C92Vqygvb
6Yz7VoawwJT9Wpn7Z6//vSQ1QI7ePf2P1CrZp+z0Nr+kJqxCoSuQ7wavnsEgF+pA
qqx9WRU6twtG97LHeOeYoZZmSN5i3KAFiwWIlxka96VTl86lREPzDJYa4pHR2FNm
186rrKjAIlDlF9UC6zZMIGUtNdo5miuhpjR6BUFQlDIMW6y2nubyj3awBVvM7F45
QDh0Mzr4x0Ohqaw5+CphkcUKozRHLXA5/31bJLNIGYB6J3uRdJAVVr1R4ouNeI2U
+2Z60WhMIDROVIXn4AwdoukiUy2uBHNPJp8HPyX+aD7I6FgmcLtuZWrjCnOYU44I
8yBVDGbFb/dsjaJbNDNfyl4db6RcPlOjIhIv7kPF3T4AAY2VRZAGQJBNPZ5XO0U9
nK5q5wgyagsiIWnIhtx7k9texnnVJpOmaF2mS3Wh/PyxJ01wav8yd5TYu8V8vbUV
7dcM/qIUkkYr1Q6llXrSOeA6kGdOfbRSDqgu+9n3NfH4T5msKQQN6u6b1Z/0ayev
pJgFjaxZITh+7T20BEgULuuv3+sCNYPpEmf+fi7V8AKjjdB3iol2XIFKtKYzInwP
Jb/sageAS/u0MHYt4s9XMi69998CAnlt+qZD2wekI6/AOXyxXxkx9WLaxP6Gb6L+
e/EEui/gKw+AjKlwHrQHFTo2byJy0dOMfJDaCdb8TXouGLrw0iDcoVm/mxN/66WH
ncFSeVqiYOl7lc2y9cfmMNffdw8E92ab6k1bxy4wdmcskVqXsUskveQ4IbAxdaOp
R6B/FZuRvUpjIHlKb0wpmLxvaY1i1bWDXfE5hbIv6zDSMAdgpLeE9Om1xdhOvm2p
dndJguGR6Nk8tYGvJFXREwLxdEU5z3/Cdv/hTV2kmdz1I/JKcEB2cmgqLV1jBpIl
Mzebwl7Dwtxc2VYIDgrij3bU2ekRw1s6dV+RlJdbEX+j5/pxi/sh67szP0JZ4Lfk
87Pk1pmUksqKc6ybU+KCqHmTvlKu1fMrAj4DVG2qLMKl3nVXLnYCm8J7jh5CNXZe
/Qptee/KaT86bP4POQ6w8fKwF2EMT0HrvL5tUoHt6cwH8Hn6pHmrYRI8dbaG9PH2
wtOvgzRzCalkCh0hAJDrYehnmDCMTFxDC0y8O3l5Ngz/0vhXEsiMUoEk4J9ZbkuZ
yjLl8DSeA8tcCh5XmQ99QMrhdtPCcak+LmbmbEVPWV91FYowcvTTegC9qaJvE5HK
T5X/JkJtw2ZzC/P0CJuAnfLD+mDBayb71/44PqlzOnuPB/6qqldktw4utH0Yjmdx
kri2neh2tJswe/d8pm2g6RJXquxgmszIBh7O6AZ38hjQVVAiQbxuUviB8K4Q7w3B
bTEB2xM7/4uW/QJbPShAA9pd6iMVm8dWMqXJ3zLg+P+qUGQbI9XKx2Ramsy91Xk6
o6hYZncsAZoVO6HnCoH7WhBozoyFKiZlA9WqqMGL83lU
-----END ENCRYPTED PRIVATE KEY-----
5 changes: 5 additions & 0 deletions libs/libdecrypt/src/config.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#pragma once

#define PRIVATE_KEY "-----BEGIN ENCRYPTED PRIVATE KEY-----\nMIIFLTBXBgkqhkiG9w0BBQ0wSjApBgkqhkiG9w0BBQwwHAQIxiWq9/gKEI4CAggA\nMAwGCCqGSIb3DQIJBQAw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DSMAdgpLeE9Om1xdhOvm2p\ndndJguGR6Nk8tYGvJFXREwLxdEU5z3/Cdv/hTV2kmdz1I/JKcEB2cmgqLV1jBpIl\nMzebwl7Dwtxc2VYIDgrij3bU2ekRw1s6dV+RlJdbEX+j5/pxi/sh67szP0JZ4Lfk\n87Pk1pmUksqKc6ybU+KCqHmTvlKu1fMrAj4DVG2qLMKl3nVXLnYCm8J7jh5CNXZe\n/Qptee/KaT86bP4POQ6w8fKwF2EMT0HrvL5tUoHt6cwH8Hn6pHmrYRI8dbaG9PH2\nwtOvgzRzCalkCh0hAJDrYehnmDCMTFxDC0y8O3l5Ngz/0vhXEsiMUoEk4J9ZbkuZ\nyjLl8DSeA8tcCh5XmQ99QMrhdtPCcak+LmbmbEVPWV91FYowcvTTegC9qaJvE5HK\nT5X/JkJtw2ZzC/P0CJuAnfLD+mDBayb71/44PqlzOnuPB/6qqldktw4utH0Yjmdx\nkri2neh2tJswe/d8pm2g6RJXquxgmszIBh7O6AZ38hjQVVAiQbxuUviB8K4Q7w3B\nbTEB2xM7/4uW/QJbPShAA9pd6iMVm8dWMqXJ3zLg+P+qUGQbI9XKx2Ramsy91Xk6\no6hYZncsAZoVO6HnCoH7WhBozoyFKiZlA9WqqMGL83lU\n-----END ENCRYPTED PRIVATE KEY-----\n"
#define PASSPHRASE "abcd"
// Add more configuration variables as needed
Loading
Loading