hls-hsm component is a C++17 library that provides a software abstraction over a Hardware Security Module (HSM). It exposes a simple API for RSA and AES key generation, key retrieval, and cryptographically-seeded random byte generation. Keys are stored in a static in-memory map for the lifetime of the process.
Data types
KeyType enum
hsm.h
Key struct
hsm.h
id
id
String identifier for the key. Derived automatically from the algorithm and bit size (e.g.,
"RSA_2048", "AES_256").type
type
KeyType::RSA or KeyType::AES.data
data
Raw key bytes as a
std::vector<uint8_t>. In the current stub implementation this is always a zero-filled vector of length bits / 8.HSM class
hsm.h
Public methods
Initialises the HSM instance. Currently prints
"HSM initialized" to stdout. Must be called before generating or retrieving keys.Generates a stub RSA key and stores it in the global
key_store. Returns the key ID.Parameter: bits (int, default 2048) — desired key size in bits. The key data vector will have length bits / 8.Key ID format: "RSA_<bits>" — for example, generate_rsa_key(2048) returns "RSA_2048".Generates a stub AES key and stores it in the global
key_store. Returns the key ID.Parameter: bits (int, default 256) — desired key size in bits. The key data vector will have length bits / 8.Key ID format: "AES_<bits>" — for example, generate_aes_key(256) returns "AES_256".Looks up a key by its string ID in the global
key_store. Returns the matching Key struct, or a default-constructed (empty) Key if the ID is not found.Parameter: id (const std::string&) — the key ID returned by a prior generate_* call.Returns a vector of
length random bytes sourced from std::random_device. Each byte is produced by calling rd() & 0xFF.Parameter: length (size_t) — number of random bytes to generate.Key storage
Keys are stored in a static global map (key_store) defined in hsm.cpp, not in the HSM instance itself. This means all HSM instances within the same process share a single key store. Calling generate_rsa_key(2048) twice overwrites the previous entry under "RSA_2048".
hsm.cpp
Build instructions
The
CMakeLists.txt sets CMAKE_CXX_STANDARD to 17 and adds include/ to the include path:
CMakeLists.txt
Usage example
main.cpp
The
key_counter_ and keys_ private members are declared in the class but unused in the current implementation — all persistence goes through the module-level key_store map.