Skip to content

Commit a3b0147

Browse files
TehilaTheStudentmiryamW
authored andcommitted
HSM: Change directory name for easier integration
1 parent 2abd6f4 commit a3b0147

Some content is hidden

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

71 files changed

+2621
-1856
lines changed

hsm/.gitignore renamed to hsm-server/.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
#clangd
1212
.cache
13-
13+
.clang-format
1414
# VS Code files
1515
.vscode/
1616
.devcontainer/
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ if(NOT GMP_INCLUDE_DIR OR NOT GMP_LIBRARY OR NOT GMPXX_LIBRARY)
1010
endif()
1111
include_directories(${GMP_INCLUDE_DIR})
1212
# Specify C++ standard
13-
set(CMAKE_CXX_STANDARD 14)
13+
set(CMAKE_CXX_STANDARD 17)
1414
set(CMAKE_CXX_STANDARD_REQUIRED ON)
1515
# Find Protobuf and gRPC packages
1616
find_package(Protobuf REQUIRED)
@@ -72,4 +72,4 @@ add_dependencies(grpc_server proto_gen)
7272
set(CMAKE_BUILD_TYPE Debug)
7373
# Add debug flags
7474
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g")
75-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g")
75+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g")
File renamed without changes.

hsm-server/include/IHash.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#ifndef IHASH_H
2+
#define IHASH_H
3+
#include "general.h"
4+
#include <cstdint>
5+
#include <vector>
6+
7+
class IHash {
8+
public:
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;
13+
};
14+
15+
#endif // IHASH_H

hsm-server/include/SHA3-512.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#ifndef SHA3_512_H
2+
#define SHA3_512_H
3+
4+
#include "IHash.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>
10+
11+
class SHA3_512 : public IHash {
12+
public:
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(std::vector<uint8_t> &output)
17+
override; // Finalize and get the hash value
18+
19+
private:
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
23+
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]);
30+
};
31+
32+
#endif // SHA3_512_H
Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,22 @@
1010
#define NUM_BLOCKS 4
1111
#define BLOCK_BYTES_LEN (AES_STATE_ROWS * (NUM_BLOCKS) * sizeof(unsigned char))
1212

13-
1413
struct AESData {
1514
unsigned int numWord;
1615
unsigned int numRound;
1716
unsigned int keySize;
1817
};
1918

20-
2119
static std::map<AESKeyLength, AESData> aesKeyLengthData = {
2220
{AESKeyLength::AES_128, {4, 10, 16}},
2321
{AESKeyLength::AES_192, {6, 12, 24}},
2422
{AESKeyLength::AES_256, {8, 14, 32}}};
2523

2624
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);
25+
void padMessage(unsigned char *originalMessage, unsigned int originalLength,
26+
unsigned char *paddedMessage);
27+
unsigned int getUnpadMessageLength(unsigned char *message,
28+
unsigned int paddedLength);
2929
void addRoundKey(unsigned char state[AES_STATE_ROWS][NUM_BLOCKS],
3030
unsigned char *roundKey);
3131
void checkLength(unsigned int length);
@@ -51,7 +51,8 @@ void xorBlocks(const unsigned char *a, const unsigned char *b, unsigned char *c,
5151
void generateRandomIV(unsigned char *iv);
5252
size_t calculatEncryptedLenAES(size_t inLen, bool isFirst,
5353
AESChainingMode chainingMode);
54-
size_t calculatDecryptedLenAES(size_t inLen, bool isFirst,AESChainingMode chainingMode);
54+
size_t calculatDecryptedLenAES(size_t inLen, bool isFirst,
55+
AESChainingMode chainingMode);
5556
void generateKey(unsigned char *, AESKeyLength keyLength);
5657

5758
/*Inverse S-Box*/
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ class StreamAES {
1111
unsigned char *key = nullptr;
1212
unsigned char *lastData = nullptr;
1313

14-
StreamAES() : iv(new unsigned char[16]), lastBlock(new unsigned char[16]), lastData(new unsigned char[16]){};
14+
StreamAES()
15+
: iv(new unsigned char[16]),
16+
lastBlock(new unsigned char[16]),
17+
lastData(new unsigned char[16]){};
1518
virtual ~StreamAES()
1619
{
1720
if (iv != nullptr) {
Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,14 @@
66
/**
77
* @brief Singleton class for managing StreamAESFactory instances.
88
*/
9-
class FactoryManager
10-
{
9+
class FactoryManager {
1110
public:
1211
/**
1312
* @brief Gets the singleton instance of FactoryManager.
1413
*
1514
* @return The singleton instance of FactoryManager.
1615
*/
17-
static FactoryManager& getInstance()
16+
static FactoryManager &getInstance()
1817
{
1918
static FactoryManager instance;
2019
return instance;
@@ -26,24 +25,22 @@ class FactoryManager
2625
* @param type The AES chaining mode.
2726
* @return A pointer to the newly created StreamAES object.
2827
*/
29-
StreamAES* create(const AESChainingMode& type) const
28+
StreamAES *create(const AESChainingMode &type) const
3029
{
3130
auto it = factories.find(type);
32-
if (it != factories.end())
31+
if (it != factories.end())
3332
return it->second;
34-
33+
3534
return nullptr;
3635
}
3736

3837
private:
39-
std::map<AESChainingMode, StreamAES*> factories =
40-
{
41-
{AESChainingMode::ECB, new AESEcb()},
42-
{AESChainingMode::CBC, new AESCbc()},
43-
{AESChainingMode::CFB, new AESCfb()},
44-
{AESChainingMode::OFB, new AESOfb()},
45-
{AESChainingMode::CTR, new AESCtr()}
46-
};
38+
std::map<AESChainingMode, StreamAES *> factories = {
39+
{AESChainingMode::ECB, new AESEcb()},
40+
{AESChainingMode::CBC, new AESCbc()},
41+
{AESChainingMode::CFB, new AESCfb()},
42+
{AESChainingMode::OFB, new AESOfb()},
43+
{AESChainingMode::CTR, new AESCtr()}};
4744

4845
/**
4946
* @brief Private constructor for singleton pattern.

0 commit comments

Comments
 (0)