Skip to main content

qualia_core_db/crypto/network/
hardware.rs

1//! Hardware token / HSM primitive surface.
2//!
3//! Every operation returns `PlatformUnsupported`. Callers must not fall back to
4//! the software adapters across this trust boundary (including WASM).
5
6use super::errors::CryptoError;
7use super::types::{
8    AEAD_KEY_LEN, AEAD_NONCE_LEN, AEAD_TAG_LEN, ED25519_SIG_LEN, ML_DSA_65_SIG_LEN,
9    ML_DSA_65_SK_LEN, ML_KEM_768_CT_LEN, ML_KEM_768_SK_LEN, ML_KEM_SS_LEN, X25519_LEN,
10};
11
12/// Hardware CSPRNG / token RNG. Unsupported in this build.
13pub fn fill_entropy(_out: &mut [u8]) -> Result<(), CryptoError> {
14    Err(CryptoError::PlatformUnsupported)
15}
16
17/// `?::sign_ed25519`.
18pub fn sign_ed25519(
19    _seed: &[u8; 32],
20    _message: &[u8],
21    _context: &[u8],
22    _sig_out: &mut [u8; ED25519_SIG_LEN],
23) -> Result<(), CryptoError> {
24    Err(CryptoError::PlatformUnsupported)
25}
26
27/// Hardware ML-DSA-65 sign — `PlatformUnsupported`.
28pub fn sign_mldsa(
29    _secret: &[u8; ML_DSA_65_SK_LEN],
30    _message: &[u8],
31    _context: &[u8],
32    _sig_out: &mut [u8; ML_DSA_65_SIG_LEN],
33) -> Result<(), CryptoError> {
34    Err(CryptoError::PlatformUnsupported)
35}
36
37/// Hardware X25519 public-key derivation — `PlatformUnsupported`.
38pub fn x25519_public(_secret: &[u8; X25519_LEN]) -> Result<[u8; X25519_LEN], CryptoError> {
39    Err(CryptoError::PlatformUnsupported)
40}
41
42/// Hardware X25519 DH — `PlatformUnsupported`.
43pub fn x25519_dh(
44    _secret: &[u8; X25519_LEN],
45    _peer: &[u8; X25519_LEN],
46) -> Result<[u8; X25519_LEN], CryptoError> {
47    Err(CryptoError::PlatformUnsupported)
48}
49
50/// Hardware ML-KEM-768 decapsulation — `PlatformUnsupported`.
51pub fn mlkem_decaps(
52    _secret: &[u8; ML_KEM_768_SK_LEN],
53    _ciphertext: &[u8; ML_KEM_768_CT_LEN],
54) -> Result<[u8; ML_KEM_SS_LEN], CryptoError> {
55    Err(CryptoError::PlatformUnsupported)
56}
57
58/// Hardware AEAD encrypt — `PlatformUnsupported`.
59pub fn aead_encrypt(
60    _key: &[u8; AEAD_KEY_LEN],
61    _nonce: &[u8; AEAD_NONCE_LEN],
62    _aad: &[u8],
63    _buffer: &mut [u8],
64    _tag_out: &mut [u8; AEAD_TAG_LEN],
65) -> Result<(), CryptoError> {
66    Err(CryptoError::PlatformUnsupported)
67}
68
69/// Hardware AEAD decrypt — `PlatformUnsupported`.
70pub fn aead_decrypt(
71    _key: &[u8; AEAD_KEY_LEN],
72    _nonce: &[u8; AEAD_NONCE_LEN],
73    _aad: &[u8],
74    _buffer: &mut [u8],
75    _tag: &[u8; AEAD_TAG_LEN],
76) -> Result<(), CryptoError> {
77    Err(CryptoError::PlatformUnsupported)
78}
79
80#[cfg(test)]
81mod tests {
82    use super::*;
83    use crate::crypto::network::ed25519;
84    use crate::crypto::network::x25519::X25519Secret;
85
86    #[test]
87    fn fill_entropy_is_platform_unsupported_without_software_fill() {
88        let mut buf = [0xAAu8; 16];
89        assert_eq!(
90            fill_entropy(&mut buf),
91            Err(CryptoError::PlatformUnsupported)
92        );
93        assert_eq!(buf, [0xAAu8; 16]);
94    }
95
96    #[test]
97    fn sign_ed25519_does_not_fall_back_to_software() {
98        let seed = [3u8; 32];
99        let mut sig = [0u8; ED25519_SIG_LEN];
100        assert_eq!(
101            sign_ed25519(&seed, b"msg", b"ctx", &mut sig),
102            Err(CryptoError::PlatformUnsupported)
103        );
104        assert_eq!(sig, [0u8; ED25519_SIG_LEN]);
105        let mut software = [0u8; ED25519_SIG_LEN];
106        ed25519::sign_with_context(&seed, b"msg", b"ctx", &mut software).unwrap();
107        assert_ne!(software, [0u8; ED25519_SIG_LEN]);
108    }
109
110    #[test]
111    fn x25519_does_not_compute_in_software() {
112        let secret = [5u8; 32];
113        assert_eq!(
114            x25519_public(&secret),
115            Err(CryptoError::PlatformUnsupported)
116        );
117        let peer = X25519Secret::from_bytes([6u8; 32]).public();
118        assert_eq!(
119            x25519_dh(&secret, &peer),
120            Err(CryptoError::PlatformUnsupported)
121        );
122        let expected = X25519Secret::from_bytes(secret).public();
123        assert_ne!(expected, [0u8; 32]);
124    }
125
126    #[test]
127    fn remaining_ops_are_platform_unsupported() {
128        let mut mldsa_sig = [0u8; ML_DSA_65_SIG_LEN];
129        assert_eq!(
130            sign_mldsa(&[0u8; ML_DSA_65_SK_LEN], b"m", b"c", &mut mldsa_sig),
131            Err(CryptoError::PlatformUnsupported)
132        );
133        assert_eq!(mldsa_sig, [0u8; ML_DSA_65_SIG_LEN]);
134        assert_eq!(
135            mlkem_decaps(&[0u8; ML_KEM_768_SK_LEN], &[0u8; ML_KEM_768_CT_LEN]),
136            Err(CryptoError::PlatformUnsupported)
137        );
138        let mut buf = *b"hello-qdnf";
139        let mut tag = [0u8; AEAD_TAG_LEN];
140        assert_eq!(
141            aead_encrypt(&[7u8; 32], &[9u8; 12], b"aad", &mut buf, &mut tag),
142            Err(CryptoError::PlatformUnsupported)
143        );
144        assert_eq!(&buf, b"hello-qdnf");
145        assert_eq!(
146            aead_decrypt(&[7u8; 32], &[9u8; 12], b"aad", &mut buf, &tag),
147            Err(CryptoError::PlatformUnsupported)
148        );
149    }
150}