HSM (PKCS#11) Operations Guide
Hardware Security Module integration for @anoobot/encryption using PKCS#11 (SoftHSM, Luna, AWS CloudHSM, etc.).
Environment Variables
| Variable | Required | Description |
ANOOBOT_HSM_ENABLED | Production | Set true to use real PKCS#11 (not software simulator) |
HSM_LIBRARY_PATH | Yes (when enabled) | Path to PKCS#11 .so / .dylib / .dll |
PKCS11_LIBRARY_PATH | Alias | Same as HSM_LIBRARY_PATH |
HSM_SLOT_ID | No | Token slot index (default 0) |
HSM_PIN | Yes (when enabled) | User PIN for token login |
ALLOW_SOFTWARE_HSM | Dev only | Allow in-process simulator when true |
HSM_TEST | CI/integration | Run SoftHSM integration tests when true |
Development (Software Simulator)
export NODE_ENV=development
export ALLOW_SOFTWARE_HSM=true
# Do not set ANOOBOT_HSM_ENABLED
pnpm exec vitest run packages/encryption/src/__tests__/hsm.test.ts
SoftHSM2 (Docker)
docker run -d --name softhsm \
-e SOFTHSM_PIN=0000 \
-v softhsm-tokens:/var/lib/softhsm/tokens \
ghcr.io/opendnssec/softhsm2:latest
export ANOOBOT_HSM_ENABLED=true
export HSM_LIBRARY_PATH=/usr/lib/softhsm/libsofthsm2.so
export HSM_SLOT_ID=0
export HSM_PIN=0000
export HSM_TEST=true
pnpm exec vitest run packages/encryption/test/hsm.integration.test.ts
Production Checklist
- Provision HSM token and create AES-256 keys with labels matching your key hierarchy.
- Set
ANOOBOT_HSM_ENABLED=true and never set ALLOW_SOFTWARE_HSM in production.
- Run
validateHsmProductionConfig() at gateway/desktop startup.
- Monitor HSM login failures and encrypt/decrypt error rates in Prometheus.
- Rotate PINs via your HSM vendor procedure; update secrets manager, not git.
Supported Operations
- Key generation (
AES-256)
- Encrypt / decrypt (AES-GCM via PKCS#11 when token supports
CKM_AES_GCM)
- Sign / verify (PKCS#11 backend only)
Troubleshooting
| Symptom | Action |
pkcs11js native module is not installed | pnpm add pkcs11js --filter @anoobot/encryption and rebuild native bindings |
No PKCS#11 token slots available | Verify token initialized, slot ID, and library path |
| Login failed | Check HSM_PIN and token lock state |