Lajule/hsminer
PKCS#11 in Zig
3b06a336ef27e5ffe04075109d67e309b83a337a
a4e784da8399c51d5eeb5783e6a485b960d5c1f9
hsminer is a Zig-based project designed to interact with a Hardware Security Module (HSM) using the PKCS#11 standard. It allows you to encrypt or decrypt text using cryptographic keys.
Use Zig to build the project:
zig build
Use Zig to build and run the project:
zig build run -- -c cert.pem -k key.pem /usr/lib/softhsm/libsofthsm2.so "${SLOT_ID}" "${PIN}"
or HSMiner binary directly:
./hsminer -c cert.pem -k key.pem /usr/lib/softhsm/libsofthsm2.so "${SLOT_ID}" "${PIN}"
Make sure the following tools are installed on your system:
Or build a docker image with:
docker build -t hsminer .
And run it with:
docker run -it --rm -p 3000:3000 -v "${PWD}":/hsminer -v "${PWD}"/tokens:/var/lib/softhsm/tokens -w /hsminer hsminer bash
Before running hsminer
, generate a self-signed certificate and a private key:
openssl req -x509 -nodes -days 365 -sha256 -newkey rsa:2048 -keyout key.pem -out cert.pem
This creates:
key.pem
: the private key.cert.pem
: the matching self-signed certificate.softhsm2-util --init-token --free --label "HSMiner" --so-pin "${PIN}" --pin "${PIN}"
pkcs11-tool --module /usr/lib/softhsm/libsofthsm2.so --slot "${SLOT_ID}" --login --pin "${PIN}" --keygen --key-type aes:32 --label "key 1"
This command creates a 256-bit AES key with the label key 1
in the initialized slot.