Skip to content

Commit 2810671

Browse files
committed
Implement HSM functionality with gRPC communication
1 parent 5dcb766 commit 2810671

52 files changed

Lines changed: 31363 additions & 3922 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

CMakeLists.txt

Lines changed: 65 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,75 @@
11
cmake_minimum_required(VERSION 3.10)
2-
project(VehicleComputingSimulator)
3-
4-
# Include directories
5-
include_directories(src)
6-
include_directories(${CMAKE_SOURCE_DIR}/include)
7-
include_directories(${CMAKE_SOURCE_DIR}/logger)
8-
2+
# Set the project name
3+
project(grpc_server)
94
# Find GMP library
105
find_path(GMP_INCLUDE_DIR NAMES gmp.h)
116
find_library(GMP_LIBRARY NAMES gmp)
127
find_library(GMPXX_LIBRARY NAMES gmpxx)
138
if(NOT GMP_INCLUDE_DIR OR NOT GMP_LIBRARY OR NOT GMPXX_LIBRARY)
14-
message(FATAL_ERROR "Could not find GMP or GMPXX libraries")
9+
message(FATAL_ERROR "Could not find GMP or GMPXX libraries")
1510
endif()
1611
include_directories(${GMP_INCLUDE_DIR})
17-
18-
# Find Google Test
19-
find_package(GTest REQUIRED)
20-
21-
# Add source files
22-
file(GLOB SOURCES "src/*.cpp" "logger/*.cpp")
23-
24-
# Check if SYCL is enabled
25-
option(USE_SYCL "Enable SYCL support" OFF)
26-
27-
if(USE_SYCL)
28-
# Set the icpx compiler with SYCL support
29-
set(CMAKE_CXX_COMPILER icpx)
30-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsycl -g")
31-
# Add oneAPI include directories
32-
include_directories(${CMAKE_SOURCE_DIR}/include /opt/intel/oneapi/compiler/latest/linux/include)
33-
# Add oneAPI library directories
34-
link_directories(/opt/intel/oneapi/compiler/latest/linux/lib)
35-
message(STATUS "Compiling with SYCL support")
36-
add_definitions(-DUSE_SYCL)
37-
else()
38-
message(STATUS "Compiling without SYCL support")
39-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g")
40-
remove_definitions(-DUSE_SYCL)
41-
endif()
42-
43-
# Set build type to Debug
44-
set(CMAKE_BUILD_TYPE Debug)
45-
46-
# Add the executable for the tests, using the test source files
47-
add_executable(runTests
48-
tests/aes_tests.cpp
49-
tests/ecc_tests.cpp
50-
tests/hash_tests.cpp
51-
${SOURCES}
12+
# Specify C++ standard
13+
set(CMAKE_CXX_STANDARD 14)
14+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
15+
# Find Protobuf and gRPC packages
16+
find_package(Protobuf REQUIRED)
17+
find_package(gRPC REQUIRED)
18+
# Gather all source files in src directory
19+
file(GLOB SOURCES "src/*.cpp" )
20+
# Specify the path to the proto files
21+
set(PROTO_FILES
22+
${CMAKE_SOURCE_DIR}/proto/encryption.proto
5223
)
53-
54-
# Link libraries
55-
target_link_libraries(runTests
56-
${GMP_LIBRARY}
57-
${GMPXX_LIBRARY}
58-
gtest
59-
gtest_main
60-
pthread
61-
)
62-
63-
# Optional: Set the output directory for the executable
64-
set_target_properties(runTests PROPERTIES
65-
RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}
24+
# Paths to the protoc and grpc_cpp_plugin binaries
25+
set(PROTOC_PATH "/usr/local/bin/protoc")
26+
set(GRPC_CPP_PLUGIN_PATH "/usr/local/bin/grpc_cpp_plugin")
27+
# Specify output directory for generated files
28+
set(PROTO_GEN_DIR "${CMAKE_BINARY_DIR}/generated")
29+
file(MAKE_DIRECTORY ${PROTO_GEN_DIR})
30+
# Generate C++ source files from proto files
31+
foreach(proto_file ${PROTO_FILES})
32+
get_filename_component(proto_name ${proto_file} NAME_WE)
33+
34+
# Protobuf C++ source files
35+
add_custom_command(
36+
OUTPUT ${PROTO_GEN_DIR}/${proto_name}.pb.cc ${PROTO_GEN_DIR}/${proto_name}.pb.h
37+
COMMAND ${PROTOC_PATH} --cpp_out=${PROTO_GEN_DIR} --proto_path=${CMAKE_SOURCE_DIR}/proto ${proto_file}
38+
DEPENDS ${proto_file}
39+
COMMENT "Generating protobuf code for ${proto_file}"
40+
)
41+
42+
# gRPC C++ source files
43+
add_custom_command(
44+
OUTPUT ${PROTO_GEN_DIR}/${proto_name}.grpc.pb.cc ${PROTO_GEN_DIR}/${proto_name}.grpc.pb.h
45+
COMMAND ${PROTOC_PATH} --grpc_out=${PROTO_GEN_DIR} --plugin=protoc-gen-grpc=${GRPC_CPP_PLUGIN_PATH} --proto_path=${CMAKE_SOURCE_DIR}/proto ${proto_file}
46+
DEPENDS ${proto_file}
47+
COMMENT "Generating gRPC code for ${proto_file}"
48+
)
49+
50+
list(APPEND PROTO_SRCS ${PROTO_GEN_DIR}/${proto_name}.pb.cc ${PROTO_GEN_DIR}/${proto_name}.grpc.pb.cc)
51+
list(APPEND PROTO_HDRS ${PROTO_GEN_DIR}/${proto_name}.pb.h ${PROTO_GEN_DIR}/${proto_name}.grpc.pb.h)
52+
endforeach()
53+
# Include the generated files directory
54+
include_directories(${PROTO_GEN_DIR})
55+
# Include directories for protobuf and gRPC
56+
include_directories(${Protobuf_INCLUDE_DIRS} ${GRPC_INCLUDE_DIRS})
57+
# Add the logger library
58+
file(GLOB LOGGER_SOURCES "logger/*.cpp")
59+
add_library(logger STATIC ${LOGGER_SOURCES})
60+
# Add the executable
61+
add_executable(grpc_server ${SOURCES} ${PROTO_SRCS})
62+
# Link against protobuf, gRPC, GMP, and logger libraries
63+
target_link_libraries(grpc_server ${Protobuf_LIBRARIES} ${GMP_LIBRARY} ${GMPXX_LIBRARY} gRPC::grpc++ logger)
64+
# Ensure that protobuf and gRPC code generation is properly configured
65+
add_custom_target(proto_gen ALL
66+
DEPENDS ${PROTO_SRCS} ${PROTO_HDRS}
67+
COMMENT "Generating protobuf and gRPC code"
6668
)
69+
# Add dependencies to ensure proper build order
70+
add_dependencies(grpc_server proto_gen)
71+
# Set build type to Debug
72+
set(CMAKE_BUILD_TYPE Debug)
73+
# Add debug flags
74+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g")
75+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g")

include/IHash.h

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
#ifndef IHASH_H
22
#define IHASH_H
3-
#include "return_codes.h"
4-
#include <vector>
3+
#include "general.h"
54
#include <cstdint>
5+
#include <vector>
66

77
class IHash {
88
public:
9-
enum SHAAlgorithm{
10-
SHA256,
11-
SHA3_512
12-
};
13-
virtual CK_RV update(const std::vector<uint8_t>& data) = 0;
14-
virtual CK_RV finalize(std::vector<uint8_t>& output) = 0;
15-
virtual ~IHash() = default;
9+
enum SHAAlgorithm { SHA256, SHA3_512 };
10+
virtual CK_RV update(const std::vector<uint8_t> &data) = 0;
11+
virtual CK_RV finalize(std::vector<uint8_t> &output) = 0;
12+
virtual ~IHash() = default;
1613
};
14+
1715
#endif // IHASH_H

include/SHA3-512.h

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,32 @@
11
#ifndef SHA3_512_H
22
#define SHA3_512_H
33

4-
#include <vector>
5-
#include <string>
6-
#include <cstdint> // For fixed-width integer types
7-
#include <sstream> // For std::ostringstream
84
#include "IHash.h"
9-
#include "return_codes.h"
5+
#include "general.h"
6+
#include <cstdint> // For fixed-width integer types
7+
#include <sstream> // For std::ostringstream
8+
#include <string>
9+
#include <vector>
1010

11-
class SHA3_512: public IHash
12-
{
11+
class SHA3_512 : public IHash {
1312
public:
14-
SHA3_512(); // Constructor to initialize the state
15-
CK_RV update(const std::vector<uint8_t>& data) override; // Update the hash with more data
16-
CK_RV finalize(std::vector<uint8_t>& output) override; // Finalize and get the hash value
13+
SHA3_512(); // Constructor to initialize the state
14+
CK_RV update(const std::vector<uint8_t> &data)
15+
override; // Update the hash with more data
16+
CK_RV finalize(
17+
std::vector<uint8_t> &output) override; // Finalize and get the hash value
1718

1819
private:
19-
uint64_t S[5][5]; // State matrix
20-
uint8_t buffer[576]; // Buffer to hold input data
21-
std::size_t buffer_length; // Current length of data in the buffer
20+
uint64_t S[5][5]; // State matrix
21+
uint8_t buffer[576]; // Buffer to hold input data
22+
std::size_t buffer_length; // Current length of data in the buffer
2223

23-
void round(uint64_t A[5][5], uint64_t RC);
24-
void f_function(uint64_t A[5][5]);
25-
void padding(uint8_t input[], std::size_t &in_len, int &absorb_times) ;
26-
void assign_S_xor_p(uint64_t S[5][5], uint64_t *p);
27-
void endianSwap(uint64_t &x);
28-
std::vector<uint8_t> hashPartToHexVector(uint64_t S[5][5]);
24+
void round(uint64_t A[5][5], uint64_t RC);
25+
void f_function(uint64_t A[5][5]);
26+
void padding(uint8_t input[], std::size_t &in_len, int &absorb_times);
27+
void assign_S_xor_p(uint64_t S[5][5], uint64_t *p);
28+
void endianSwap(uint64_t &x);
29+
std::vector<uint8_t> hashPartToHexVector(uint64_t S[5][5]);
2930
};
3031

31-
#endif // SHA3_512_H
32+
#endif // SHA3_512_H

include/aes.h

Lines changed: 38 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#ifndef _AES_H_
22
#define _AES_H_
33
#include <functional>
4+
#include "general.h"
45
#include <cstdio>
56
#include <cstring>
67
#include <functional>
@@ -9,59 +10,49 @@
910
#define NUM_BLOCKS 4
1011
#define BLOCK_BYTES_LEN (AES_STATE_ROWS * (NUM_BLOCKS) * sizeof(unsigned char))
1112

12-
enum AESChainingMode {
13-
ECB, /*Electronic Codebook*/
14-
CBC, /*Cipher Block Chaining*/
15-
CFB, /*Cipher Feedback*/
16-
OFB, /*Output Feedback*/
17-
CTR /*Counter*/
18-
};
19-
20-
enum class AESKeyLength
21-
{
22-
AES_128,
23-
AES_192,
24-
AES_256
25-
};
2613

27-
struct AESData
28-
{
14+
struct AESData {
2915
unsigned int numWord;
3016
unsigned int numRound;
3117
unsigned int keySize;
3218
};
3319

34-
typedef std::function<void(unsigned char*, unsigned int, unsigned char*, unsigned char*&, unsigned int&, const unsigned char*, unsigned char*, AESKeyLength)> EncryptDecryptFunc;
35-
static std::map<AESKeyLength,AESData> aesKeyLengthData = {
36-
{ AESKeyLength::AES_128, {4, 10, 128} },
37-
{ AESKeyLength::AES_192, {6, 12, 192} },
38-
{ AESKeyLength::AES_256, {8, 14, 256} }
39-
};
4020

41-
void padMessage(unsigned char* &message, unsigned int& length, unsigned int& paddedLength);
42-
void unpadMessage(unsigned char* message, unsigned int& length);
43-
void addRoundKey(unsigned char state[AES_STATE_ROWS][NUM_BLOCKS], unsigned char* roundKey);
44-
void checkLength(unsigned int length);
45-
void encryptBlock(const unsigned char in[], unsigned char out[], unsigned char* roundKeys, AESKeyLength keyLength);
46-
void decryptBlock(const unsigned char in[], unsigned char out[], unsigned char* roundKeys, AESKeyLength keyLength);
47-
void subBytes(unsigned char state[AES_STATE_ROWS][NUM_BLOCKS]);
48-
void invSubBytes(unsigned char state[AES_STATE_ROWS][NUM_BLOCKS]);
49-
void invShiftRows(unsigned char state[AES_STATE_ROWS][NUM_BLOCKS]);
50-
void invMixColumns(unsigned char state[AES_STATE_ROWS][NUM_BLOCKS]);
51-
void mixColumns(unsigned char state[AES_STATE_ROWS][NUM_BLOCKS]);
52-
void shiftRows(unsigned char state[AES_STATE_ROWS][NUM_BLOCKS]);
53-
void keyExpansion(const unsigned char* key, unsigned char roundKeys[], AESKeyLength keyLength);
54-
unsigned char xtime(unsigned char x);
55-
void rotWord(unsigned char word[AES_STATE_ROWS]);
56-
void subWord(unsigned char word[AES_STATE_ROWS]);
57-
void rconWord(unsigned char rcon[AES_STATE_ROWS], unsigned int n);
58-
unsigned char multiply(unsigned char x, unsigned char y);
59-
void xorBlocks(const unsigned char *a, const unsigned char *b,
60-
unsigned char *c, unsigned int len);
61-
void generateRandomIV(unsigned char* iv);
62-
size_t calculatEncryptedLenAES(size_t inLen, bool isFirst);
63-
size_t calculatDecryptedLenAES(size_t inLen, bool isFirst);
64-
void generateKey(unsigned char*, AESKeyLength keyLength);
21+
static std::map<AESKeyLength, AESData> aesKeyLengthData = {
22+
{AESKeyLength::AES_128, {4, 10, 16}},
23+
{AESKeyLength::AES_192, {6, 12, 24}},
24+
{AESKeyLength::AES_256, {8, 14, 32}}};
25+
26+
unsigned int getPaddedLength(unsigned int originalLength);
27+
void padMessage(unsigned char *originalMessage, unsigned int originalLength,unsigned char * paddedMessage);
28+
unsigned int getUnpadMessageLength(unsigned char *message,unsigned int paddedLength);
29+
void addRoundKey(unsigned char state[AES_STATE_ROWS][NUM_BLOCKS],
30+
unsigned char *roundKey);
31+
void checkLength(unsigned int length);
32+
void encryptBlock(const unsigned char in[], unsigned char out[],
33+
unsigned char *roundKeys, AESKeyLength keyLength);
34+
void decryptBlock(const unsigned char in[], unsigned char out[],
35+
unsigned char *roundKeys, AESKeyLength keyLength);
36+
void subBytes(unsigned char state[AES_STATE_ROWS][NUM_BLOCKS]);
37+
void invSubBytes(unsigned char state[AES_STATE_ROWS][NUM_BLOCKS]);
38+
void invShiftRows(unsigned char state[AES_STATE_ROWS][NUM_BLOCKS]);
39+
void invMixColumns(unsigned char state[AES_STATE_ROWS][NUM_BLOCKS]);
40+
void mixColumns(unsigned char state[AES_STATE_ROWS][NUM_BLOCKS]);
41+
void shiftRows(unsigned char state[AES_STATE_ROWS][NUM_BLOCKS]);
42+
void keyExpansion(const unsigned char *key, unsigned char roundKeys[],
43+
AESKeyLength keyLength);
44+
unsigned char xtime(unsigned char x);
45+
void rotWord(unsigned char word[AES_STATE_ROWS]);
46+
void subWord(unsigned char word[AES_STATE_ROWS]);
47+
void rconWord(unsigned char rcon[AES_STATE_ROWS], unsigned int n);
48+
unsigned char multiply(unsigned char x, unsigned char y);
49+
void xorBlocks(const unsigned char *a, const unsigned char *b, unsigned char *c,
50+
unsigned int len);
51+
void generateRandomIV(unsigned char *iv);
52+
size_t calculatEncryptedLenAES(size_t inLen, bool isFirst,
53+
AESChainingMode chainingMode);
54+
size_t calculatDecryptedLenAES(size_t inLen, bool isFirst,AESChainingMode chainingMode);
55+
void generateKey(unsigned char *, AESKeyLength keyLength);
6556

6657
/*Inverse S-Box*/
6758
const unsigned char invSBox[16][16] = {
@@ -133,4 +124,4 @@ const unsigned char sBox[16][16] = {
133124
{0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68, 0x41, 0x99, 0x2d, 0x0f,
134125
0xb0, 0x54, 0xbb, 0x16}};
135126

136-
#endif
127+
#endif

0 commit comments

Comments
 (0)