Skip to main content

qualia_core_db/platform/
tee_ffi.rs

1// QualiaDB Trusted Execution Environment (TEE) Bindings
2// Handles Cryptographic signatures (Bilateral Guardianship/DID) natively within the
3// Secure Enclave, guaranteeing private keys never leak into the Rust RAM boundaries.
4
5#[cfg(target_os = "android")]
6pub mod android_keystore {
7    use crate::*;
8    use std::os::raw::c_int;
9
10    extern "C" {
11        // Native C-API hooks into the Android Hardware-Backed Keystore (Titan M / TrustZone)
12        // Passes the 48-byte NQuin memory block directly to the secure enclave for signing.
13        pub fn AKeyStore_signData(
14            key_alias: *const u8,
15            key_alias_len: usize,
16            data: *const NQuin,
17            data_len: usize,
18            out_signature: *mut *mut u8,
19            out_signature_len: *mut usize,
20        ) -> c_int;
21
22        pub fn AKeyStore_verifySignature(
23            key_alias: *const u8,
24            key_alias_len: usize,
25            data: *const NQuin,
26            data_len: usize,
27            signature: *const u8,
28            signature_len: usize,
29        ) -> c_int;
30    }
31}
32
33#[cfg(target_vendor = "apple")]
34pub mod apple_secure_enclave {
35    use std::ffi::c_void;
36
37    #[link(name = "Security", kind = "framework")]
38    extern "C" {
39        // CoreFoundation & Security framework native bindings for the Apple Secure Enclave
40        // `dataToSign` points to the CFDataRef wrapper of the 48-byte NQuin
41        pub fn SecKeyCreateSignature(
42            key: *mut c_void,        // SecKeyRef
43            algorithm: *mut c_void,  // SecKeyAlgorithm
44            dataToSign: *mut c_void, // CFDataRef
45            error: *mut *mut c_void, // CFErrorRef
46        ) -> *mut c_void; // Returns CFDataRef signature
47
48        pub fn SecKeyVerifySignature(
49            key: *mut c_void,        // SecKeyRef
50            algorithm: *mut c_void,  // SecKeyAlgorithm
51            signedData: *mut c_void, // CFDataRef
52            signature: *mut c_void,  // CFDataRef
53            error: *mut *mut c_void, // CFErrorRef
54        ) -> bool;
55    }
56}