Skip to main content
Drive a YubiHSM 2 from a cargo test: generate a key, sign data with it, and manage the objects on the device. It requires box software 0.59.0 or later.

Handle

Methods

Login

Every method except info() logs in with an authentication key. Give the key and its password in an HsmAuth:
HsmAuth::new uses authentication key 1, the factory key. The Debug output of an HsmAuth leaves out the password, so it cannot reach a test log through {:?}.
If the network between your computer and the box is not trusted, reach the box only through Tailscale or an SSH tunnel. The crate sends the password to the box API over plain HTTP, the same as all other box API traffic. The box removes the password from its logs.
The factory login is authentication key 1 with the password password. Replace that key before you put keys on the HSM that matter.

Types

GenerateKey

GenerateKey::new(algorithm) sets every other field to its default.

HsmObject

Each object from objects() carries id, object_type, label, algorithm, domains, capabilities, size and sequence.

Method Reference

generate(auth: &HsmAuth, key: &GenerateKey) -> Result<GeneratedKey>

Generate an asymmetric key on the device.
Returns: GeneratedKey, with the key’s id and its public_key_pem. The box refuses RSA generation, because it can take minutes. To make an RSA key, use the Python API.

sign(auth: &HsmAuth, object_id: u16, data: &[u8], mechanism: &str) -> Result<Vec<u8>>

Sign data with a stored key, and return the signature bytes. The HSM hashes the data for the ECDSA and RSA mechanisms, so pass the message itself, not a digest of it.
mechanism is one of these:
  • ecdsa-sha256, ecdsa-sha384, ecdsa-sha512
  • eddsa
  • rsa-pkcs1-sha256, rsa-pkcs1-sha384, rsa-pkcs1-sha512
  • rsa-pss-sha256, rsa-pss-sha384, rsa-pss-sha512

objects(auth: &HsmAuth, object_type: Option<&str>, label: Option<&str>) -> Result<Vec<HsmObject>>

List the objects that the authentication key can see. Pass a type or a label to filter the list. The types are asymmetric-key, authentication-key, opaque, wrap-key, hmac-key, template and otp-aead-key.

delete(auth: &HsmAuth, object_id: u16, object_type: Option<&str>) -> Result<()>

Delete an object. object_type defaults to asymmetric-key.

reset(auth: &HsmAuth) -> Result<()>

reset() deletes every key and object on the device, and you cannot undo it. The device reboots and keeps only the factory login. Call it only on an HSM that holds no key you need.

Examples

Generate a key, sign, and clean up

Notes

  • An unknown algorithm, mechanism or object type fails with Error::Box and HTTP 400. The message lists the valid names.
  • A failed login or an error on the device fails with Error::Box and HTTP 502.
  • The data for sign() and the signature travel as base64. The crate encodes and decodes them.
  • AsyncHsm from AsyncLagerBox::hsm provides the same methods, .awaited.