Skip to main content

capsule_core/crypto/keys/
software.rs

1//! Software (no-secure-element) device signer — the universal fallback [`HardwareSigner`].
2//!
3//! [`SoftwareSigner`] is the software peer of the per-platform hardware backends (Secure
4//! Enclave / StrongBox / TPM): it implements the same [`HardwareSigner`] contract, so the
5//! [`HardwareBackedSigner`](super::HardwareBackedSigner) /
6//! `create_with_hardware_signer`
7//! path runs unchanged on a device with **no** secure element. The classical Ed25519 key lives
8//! in ordinary process memory, derived deterministically (HKDF-SHA512, per `key_alias`) from a
9//! 32-byte seed the caller is responsible for sealing under the master key — exactly as the
10//! ML-DSA-65 `ξ` seed is sealed — so the published device key is stable across restarts.
11//!
12//! Unlike a real element it offers **no** hardware non-exportability guarantee, and
13//! [`assert_non_exportable`](HardwareSigner::assert_non_exportable) says so by returning
14//! [`HardwareSignerError::Exportable`]. It is the reference the native software-signer examples
15//! (Kotlin / Swift) mirror, and the only backend the CI smoke test exercises (Linux, in-process,
16//! bypassing the FFI and any hardware/TPM requirement).
17//!
18//! For a device that never touches the hardware seam, the simpler path is the fully-software
19//! [`HybridSigningKey`](super::HybridSigningKey) via
20//! `Workspace::create` (both halves in software);
21//! `SoftwareSigner` exists to drive the hardware-signer seam uniformly across all backends.
22
23use ed25519_dalek::{Signer as _, SigningKey};
24
25use super::hardware::{HardwareSigner, HardwareSignerError};
26use crate::crypto::{kdf, rng};
27
28/// HKDF-SHA512 `info` label binding the per-alias Ed25519 derivation to this construction.
29const SOFTWARE_SIGNER_INFO: &[u8] = b"capsule/software-signer/ed25519/v1";
30
31/// A software [`HardwareSigner`]: the fallback for devices with no secure element.
32#[derive(Clone)]
33pub struct SoftwareSigner {
34    seed: [u8; 32],
35}
36
37impl SoftwareSigner {
38    /// Build a signer from a 32-byte seed. The caller **must** seal `seed` (e.g. under the
39    /// master key, like the ML-DSA-65 `ξ` seed) so the device key survives restarts.
40    pub fn from_seed(seed: [u8; 32]) -> Self {
41        Self { seed }
42    }
43
44    /// Generate a signer from a fresh OS-CSPRNG seed. Persist [`seed`](Self::seed) before
45    /// dropping it — it cannot be recovered afterwards.
46    pub fn generate() -> Self {
47        Self {
48            seed: rng::random_array::<32>(),
49        }
50    }
51
52    /// The seed, for sealed storage. Present *because* this is a software key — a real secure
53    /// element has no equivalent (its private bytes never leave hardware).
54    pub fn seed(&self) -> [u8; 32] {
55        self.seed
56    }
57
58    /// The deterministic per-alias Ed25519 key. Distinct aliases get distinct keys.
59    fn signing_key(&self, key_alias: &str) -> SigningKey {
60        let ed_seed = kdf::derive_key32(&self.seed, key_alias.as_bytes(), SOFTWARE_SIGNER_INFO);
61        SigningKey::from_bytes(&ed_seed)
62    }
63}
64
65impl HardwareSigner for SoftwareSigner {
66    fn enroll(&self, key_alias: String) -> Result<Vec<u8>, HardwareSignerError> {
67        // Derivation is idempotent per alias, so enrollment is just the public key.
68        self.classical_public_key(key_alias)
69    }
70
71    fn classical_public_key(&self, key_alias: String) -> Result<Vec<u8>, HardwareSignerError> {
72        Ok(self
73            .signing_key(&key_alias)
74            .verifying_key()
75            .to_bytes()
76            .to_vec())
77    }
78
79    fn sign_classical(
80        &self,
81        key_alias: String,
82        msg: Vec<u8>,
83    ) -> Result<Vec<u8>, HardwareSignerError> {
84        Ok(self.signing_key(&key_alias).sign(&msg).to_bytes().to_vec())
85    }
86
87    fn assert_non_exportable(&self, _key_alias: String) -> Result<(), HardwareSignerError> {
88        // Honest by design: a software key lives in ordinary memory and *is* readable, so it can
89        // never satisfy the non-exportability contract a real secure element does. The native
90        // smoke harnesses assert this `Err` to prove they distinguish software from hardware.
91        Err(HardwareSignerError::Exportable)
92    }
93}
94
95#[cfg(test)]
96mod tests;