Skip to main content

KeyStore

Trait KeyStore 

Source
pub trait KeyStore {
    // Required methods
    fn generate(
        &mut self,
        purpose: KeyPurpose,
        random: &[u8; 32],
    ) -> Result<(KeyHandle, PublicKey)>;
    fn import(
        &mut self,
        purpose: KeyPurpose,
        secret: &[u8; 32],
    ) -> Result<KeyHandle>;
    fn public_key(&self, handle: KeyHandle) -> Result<PublicKey>;
    fn sign(&self, handle: KeyHandle, message: &[u8]) -> Result<Signature>;
    fn ecdh(
        &self,
        handle: KeyHandle,
        peer: &PublicKey,
    ) -> Result<Secret<GROUP_SIZE_BYTES>>;
    fn remove(&mut self, handle: KeyHandle) -> Result<()>;
}
Expand description

Custody of private keys.

Every operation that needs a private key goes through here, and none of them returns one. A node whose keys are in an ATECC608, an nRF KMU, an ESP32 HMAC peripheral or a TPM implements this over that; a node without one gets SoftKeyStore, which keeps them in zeroising memory.

This is a trait — unlike the cryptosuite — because where the keys live really does differ between two nodes running the same binary.

Required Methods§

Source

fn generate( &mut self, purpose: KeyPurpose, random: &[u8; 32], ) -> Result<(KeyHandle, PublicKey)>

Generates a key pair from 32 octets of randomness, returning its handle and public key.

The randomness is a parameter rather than something this crate fetches, for the same reason every other source of entropy is: it comes from the one Rng the integrator installed, so a platform with a hardware generator uses it everywhere and a test can be deterministic.

Returns ErrorCode::InvalidArgument if the value is not a valid private key — zero, or at or above the group order. A caller should draw again.

Source

fn import( &mut self, purpose: KeyPurpose, secret: &[u8; 32], ) -> Result<KeyHandle>

Imports an existing private key. A store backed by hardware may refuse.

Source

fn public_key(&self, handle: KeyHandle) -> Result<PublicKey>

The public key for a handle.

Source

fn sign(&self, handle: KeyHandle, message: &[u8]) -> Result<Signature>

Signs message — ECDSA over SHA-256 (§3.5.3.1).

Source

fn ecdh( &self, handle: KeyHandle, peer: &PublicKey, ) -> Result<Secret<GROUP_SIZE_BYTES>>

Computes the ECDH shared secret with peer (§3.5.4).

“The output of ECDH() SHALL be the serialization of the x-coordinate of the resultant point.”

Source

fn remove(&mut self, handle: KeyHandle) -> Result<()>

Destroys a key. Removing a fabric destroys its operational key, and that has to actually happen rather than merely be forgotten about.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§

Source§

impl<const N: usize> KeyStore for SoftKeyStore<N>

Available on crate feature rustcrypto only.