Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
dda926f
HSM: Implement and test ECC algorithm.
miryamW Jul 25, 2024
8aca6bd
HSM: Convert ECC class-based implementation to functional approach.
miryamW Aug 11, 2024
b17bbf7
HSM: Implement and test AES algorithm
miryamW Aug 6, 2024
1616a7c
HSM: Refactor - Change AES Implementation from Class-Based to Functio…
miryamW Sep 2, 2024
90cbf54
HSM: Add SYCL support for AES operations and update CMake for conditi…
miryamW Sep 2, 2024
2a1bbd4
HSM: Add support for additional chaining modes
rut-git Sep 3, 2024
ae005a7
HSM: Add Streaming blocks support
miryamW Sep 8, 2024
8f665d9
HSM: Add threading support to functions for improved performance and …
rut-git Sep 8, 2024
c3dc0e5
HSM: Add SYCL support for ECC operations and update CMake for conditi…
rut-git Sep 2, 2024
e673048
HSM: Implement and test SHA256 algorithm
mali053 Aug 11, 2024
1563fe5
HSM: Replace class-based design with function declarations
mali053 Aug 11, 2024
8000ea0
HSM: Add SYCL parallelization to SHA256 implementation
mali053 Sep 5, 2024
5e170e4
HSM: Implement and test SHA3-512 algorithm
Sep 5, 2024
e5eff85
HSM: Implement and test RSA algorithm
TehilaTheStudent Sep 16, 2024
c7a2d26
hsm: Add SYCL parallelization to SHA256 implementation
mali053 Sep 5, 2024
5ab3a0f
HSM: Correct AES Streaming Implementation
miryamW Sep 18, 2024
5dcb766
HSM: add shared interface and factory pattern for SHA algorithms
mali053 Sep 18, 2024
c40080d
Implement HSM functionality with gRPC communication
rut-git Sep 30, 2024
911ba3b
HSM: Preparation for integration
rut-git Oct 1, 2024
2abd6f4
HSM: Add Dockerfile
TehilaTheStudent Oct 9, 2024
b384858
HSM: Change directory name for easier integration
TehilaTheStudent Oct 13, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion README.md

This file was deleted.

61 changes: 61 additions & 0 deletions hsm-server/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# build system files & directories
/build/
/CMakeFiles/
/CMakeCache.txt
/cmake_install.cmake
/Makefile

# gtest files
/gtest/

#clangd
.cache
.clang-format
# VS Code files
.vscode/
.devcontainer/

# binary files
*.o
*.a
*.so
*.exe
*.dll

# log files
*.log

# Vim swap files
*.swp

# Emacs backup files
*~

# other files of C++ projects
*.obj
*.pdb
*.pch
*.ilk
*.lib
*.exp
*.idb
*.ipch
*.rsp
*.tlog
*.log
*.vspscc
*.vssscc
*.suo
*.user
*.userosscache
*.sln.docstates

# log files
*.log

# operating system files
Thumbs.db
Desktop.ini

# linux operating system files
*~
75 changes: 75 additions & 0 deletions hsm-server/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
cmake_minimum_required(VERSION 3.10)
# Set the project name
project(grpc_server)
# Find GMP library
find_path(GMP_INCLUDE_DIR NAMES gmp.h)
find_library(GMP_LIBRARY NAMES gmp)
find_library(GMPXX_LIBRARY NAMES gmpxx)
if(NOT GMP_INCLUDE_DIR OR NOT GMP_LIBRARY OR NOT GMPXX_LIBRARY)
message(FATAL_ERROR "Could not find GMP or GMPXX libraries")
endif()
include_directories(${GMP_INCLUDE_DIR})
# Specify C++ standard
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
# Find Protobuf and gRPC packages
find_package(Protobuf REQUIRED)
find_package(gRPC REQUIRED)
# Gather all source files in src directory
file(GLOB SOURCES "src/*.cpp" )
# Specify the path to the proto files
set(PROTO_FILES
${CMAKE_SOURCE_DIR}/proto/encryption.proto
)
# Paths to the protoc and grpc_cpp_plugin binaries
set(PROTOC_PATH "/usr/local/bin/protoc")
set(GRPC_CPP_PLUGIN_PATH "/usr/local/bin/grpc_cpp_plugin")
# Specify output directory for generated files
set(PROTO_GEN_DIR "${CMAKE_BINARY_DIR}/generated")
file(MAKE_DIRECTORY ${PROTO_GEN_DIR})
# Generate C++ source files from proto files
foreach(proto_file ${PROTO_FILES})
get_filename_component(proto_name ${proto_file} NAME_WE)

# Protobuf C++ source files
add_custom_command(
OUTPUT ${PROTO_GEN_DIR}/${proto_name}.pb.cc ${PROTO_GEN_DIR}/${proto_name}.pb.h
COMMAND ${PROTOC_PATH} --cpp_out=${PROTO_GEN_DIR} --proto_path=${CMAKE_SOURCE_DIR}/proto ${proto_file}
DEPENDS ${proto_file}
COMMENT "Generating protobuf code for ${proto_file}"
)

# gRPC C++ source files
add_custom_command(
OUTPUT ${PROTO_GEN_DIR}/${proto_name}.grpc.pb.cc ${PROTO_GEN_DIR}/${proto_name}.grpc.pb.h
COMMAND ${PROTOC_PATH} --grpc_out=${PROTO_GEN_DIR} --plugin=protoc-gen-grpc=${GRPC_CPP_PLUGIN_PATH} --proto_path=${CMAKE_SOURCE_DIR}/proto ${proto_file}
DEPENDS ${proto_file}
COMMENT "Generating gRPC code for ${proto_file}"
)

list(APPEND PROTO_SRCS ${PROTO_GEN_DIR}/${proto_name}.pb.cc ${PROTO_GEN_DIR}/${proto_name}.grpc.pb.cc)
list(APPEND PROTO_HDRS ${PROTO_GEN_DIR}/${proto_name}.pb.h ${PROTO_GEN_DIR}/${proto_name}.grpc.pb.h)
endforeach()
# Include the generated files directory
include_directories(${PROTO_GEN_DIR})
# Include directories for protobuf and gRPC
include_directories(${Protobuf_INCLUDE_DIRS} ${GRPC_INCLUDE_DIRS})
# Add the logger library
file(GLOB LOGGER_SOURCES "logger/*.cpp")
add_library(logger STATIC ${LOGGER_SOURCES})
# Add the executable
add_executable(grpc_server src/my_logger.cpp ${SOURCES} ${PROTO_SRCS})
# Link against protobuf, gRPC, GMP, and logger libraries
target_link_libraries(grpc_server ${Protobuf_LIBRARIES} ${GMP_LIBRARY} ${GMPXX_LIBRARY} gRPC::grpc++ logger)
# Ensure that protobuf and gRPC code generation is properly configured
add_custom_target(proto_gen ALL
DEPENDS ${PROTO_SRCS} ${PROTO_HDRS}
COMMENT "Generating protobuf and gRPC code"
)
# Add dependencies to ensure proper build order
add_dependencies(grpc_server proto_gen)
# Set build type to Debug
set(CMAKE_BUILD_TYPE Debug)
# Add debug flags
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g")
52 changes: 52 additions & 0 deletions hsm-server/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
FROM ubuntu:20.04
RUN apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y \
build-essential \
cmake \
libgtest-dev \
g++ \
make \
libgmp-dev \
curl \
libprotobuf-dev \
protobuf-compiler \
git \
libtool \
autoconf \
ca-certificates \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*
# Clone gRPC and install it
RUN git config --global http.sslVerify false && \
git clone -b v1.41.1 --recursive https://github.com/grpc/grpc /grpc && \
cd /grpc && \
mkdir -p cmake/build && cd cmake/build && \
cmake ../.. && \
make && make install
# התקן protobuf
RUN git clone https://github.com/protocolbuffers/protobuf.git && \
cd protobuf && \
git checkout v3.18.1 && \
git submodule update --init --recursive && \
./autogen.sh && \
./configure && \
make && \
make install && \
ldconfig
# הגדר את תיקיית העבודה בתוך הקונטיינר
WORKDIR /app
# העתק את תוכן התיקייה הנוכחית לתוך הקונטיינר בכתובת /app
COPY . .
# צור תיקיית build
RUN rm -rf build && mkdir build
# שנה לתיקיית build
WORKDIR /app/build
# התקן את Google Test
RUN cd /usr/src/gtest && cmake CMakeLists.txt && make && \
find /usr/src/gtest -name "*.a" -exec cp {} /usr/lib \;
# בנה את הפרויקט באמצעות CMake ו-Make
RUN cmake .. -DUSE_SYCL=OFF -DUSE_DEBUG=OFF
RUN make
WORKDIR /app
EXPOSE 50051
# פקודה להריץ את השרת (אופציונלי)
CMD ["./build/grpc_server"]
17 changes: 17 additions & 0 deletions hsm-server/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
version: '3.8'

services:
grpc_server:
build:
context: .
dockerfile: Dockerfile
ports:
- "50051:50051"
volumes:
- .:/app
networks:
- grpc_network

networks:
grpc_network:
external: true # Use the external network created
15 changes: 15 additions & 0 deletions hsm-server/include/IHash.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#ifndef IHASH_H
#define IHASH_H
#include "general.h"
#include <cstdint>
#include <vector>

class IHash {
public:
enum SHAAlgorithm { SHA256, SHA3_512 };
virtual CK_RV update(const std::vector<uint8_t> &data) = 0;
virtual CK_RV finalize(std::vector<uint8_t> &output) = 0;
virtual ~IHash() = default;
};

#endif // IHASH_H
32 changes: 32 additions & 0 deletions hsm-server/include/SHA3-512.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#ifndef SHA3_512_H
#define SHA3_512_H

#include "IHash.h"
#include "general.h"
#include <cstdint> // For fixed-width integer types
#include <sstream> // For std::ostringstream
#include <string>
#include <vector>

class SHA3_512 : public IHash {
public:
SHA3_512(); // Constructor to initialize the state
CK_RV update(const std::vector<uint8_t> &data)
override; // Update the hash with more data
CK_RV finalize(std::vector<uint8_t> &output)
override; // Finalize and get the hash value

private:
uint64_t S[5][5]; // State matrix
uint8_t buffer[576]; // Buffer to hold input data
std::size_t buffer_length; // Current length of data in the buffer

void round(uint64_t A[5][5], uint64_t RC);
void f_function(uint64_t A[5][5]);
void padding(uint8_t input[], std::size_t &in_len, int &absorb_times);
void assign_S_xor_p(uint64_t S[5][5], uint64_t *p);
void endianSwap(uint64_t &x);
std::vector<uint8_t> hashPartToHexVector(uint64_t S[5][5]);
};

#endif // SHA3_512_H
128 changes: 128 additions & 0 deletions hsm-server/include/aes.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
#ifndef _AES_H_
#define _AES_H_
#include <functional>
#include "general.h"
#include <cstdio>
#include <cstring>
#include <functional>
#include <map>
#define AES_STATE_ROWS 4
#define NUM_BLOCKS 4
#define BLOCK_BYTES_LEN (AES_STATE_ROWS * (NUM_BLOCKS) * sizeof(unsigned char))

struct AESData {
unsigned int numWord;
unsigned int numRound;
unsigned int keySize;
};

static std::map<AESKeyLength, AESData> aesKeyLengthData = {
{AESKeyLength::AES_128, {4, 10, 16}},
{AESKeyLength::AES_192, {6, 12, 24}},
{AESKeyLength::AES_256, {8, 14, 32}}};

unsigned int getPaddedLength(unsigned int originalLength);
void padMessage(unsigned char *originalMessage, unsigned int originalLength,
unsigned char *paddedMessage);
unsigned int getUnpadMessageLength(unsigned char *message,
unsigned int paddedLength);
void addRoundKey(unsigned char state[AES_STATE_ROWS][NUM_BLOCKS],
unsigned char *roundKey);
void checkLength(unsigned int length);
void encryptBlock(const unsigned char in[], unsigned char out[],
unsigned char *roundKeys, AESKeyLength keyLength);
void decryptBlock(const unsigned char in[], unsigned char out[],
unsigned char *roundKeys, AESKeyLength keyLength);
void subBytes(unsigned char state[AES_STATE_ROWS][NUM_BLOCKS]);
void invSubBytes(unsigned char state[AES_STATE_ROWS][NUM_BLOCKS]);
void invShiftRows(unsigned char state[AES_STATE_ROWS][NUM_BLOCKS]);
void invMixColumns(unsigned char state[AES_STATE_ROWS][NUM_BLOCKS]);
void mixColumns(unsigned char state[AES_STATE_ROWS][NUM_BLOCKS]);
void shiftRows(unsigned char state[AES_STATE_ROWS][NUM_BLOCKS]);
void keyExpansion(const unsigned char *key, unsigned char roundKeys[],
AESKeyLength keyLength);
unsigned char xtime(unsigned char x);
void rotWord(unsigned char word[AES_STATE_ROWS]);
void subWord(unsigned char word[AES_STATE_ROWS]);
void rconWord(unsigned char rcon[AES_STATE_ROWS], unsigned int n);
unsigned char multiply(unsigned char x, unsigned char y);
void xorBlocks(const unsigned char *a, const unsigned char *b, unsigned char *c,
unsigned int len);
void generateRandomIV(unsigned char *iv);
size_t calculatEncryptedLenAES(size_t inLen, bool isFirst,
AESChainingMode chainingMode);
size_t calculatDecryptedLenAES(size_t inLen, bool isFirst,
AESChainingMode chainingMode);
void generateKey(unsigned char *, AESKeyLength keyLength);

/*Inverse S-Box*/
const unsigned char invSBox[16][16] = {
{0x52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x20, 0x9a, 0xdb, 0xc0, 0xfe,
0x78, 0xcd, 0x5a, 0xf4},
{0x1f, 0xdd, 0xa8, 0x33, 0x88, 0x07, 0xc7, 0x31, 0xb1, 0x12, 0x10, 0x59,
0x27, 0x80, 0xec, 0x5f},
{0x60, 0x51, 0x7f, 0xa9, 0x19, 0xb5, 0x4a, 0x0d, 0x2d, 0xe5, 0x7a, 0x9f,
0x93, 0xc9, 0x9c, 0xef},
{0xa0, 0xe0, 0x3b, 0x4d, 0xae, 0x2a, 0xf5, 0xb0, 0xc8, 0xeb, 0xbb, 0x3c,
0x83, 0x53, 0x99, 0x61},
{0x17, 0x2b, 0x04, 0x7e, 0xba, 0x77, 0xd6, 0x26, 0xe1, 0x69, 0x14, 0x63,
0x55, 0x21, 0x0c, 0x7d}};

/*S-Box*/
const unsigned char sBox[16][16] = {
{0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5, 0x30, 0x01, 0x67, 0x2b,
0xfe, 0xd7, 0xab, 0x76},
{0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0, 0xad, 0xd4, 0xa2, 0xaf,
0x9c, 0xa4, 0x72, 0xc0},
{0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc, 0x34, 0xa5, 0xe5, 0xf1,
0x71, 0xd8, 0x31, 0x15},
{0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a, 0x07, 0x12, 0x80, 0xe2,
0xeb, 0x27, 0xb2, 0x75},
{0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0, 0x52, 0x3b, 0xd6, 0xb3,
0x29, 0xe3, 0x2f, 0x84},
{0x53, 0xd1, 0x00, 0xed, 0x20, 0xfc, 0xb1, 0x5b, 0x6a, 0xcb, 0xbe, 0x39,
0x4a, 0x4c, 0x58, 0xcf},
{0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85, 0x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