Skip to content

Commit 4ffe970

Browse files
HSM: Change directory name for easier integration
1 parent 2abd6f4 commit 4ffe970

Some content is hidden

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

74 files changed

+25135
-16852
lines changed
File renamed without changes.

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/HSM_Communication.txt

Lines changed: 183 additions & 0 deletions
Large diffs are not rendered by default.

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) {

0 commit comments

Comments
 (0)