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 /// JNI shim: signs the NQuin in the Android Keystore; symbol supplied by the embedding app (not an NDK builtin).
14 pub fn AKeyStore_signData(
15 key_alias: *const u8,
16 key_alias_len: usize,
17 data: *const NQuin,
18 data_len: usize,
19 out_signature: *mut *mut u8,
20 out_signature_len: *mut usize,
21 ) -> c_int;
22
23 /// JNI shim: verifies a signature in the Android Keystore; symbol supplied by the embedding app.
24 pub fn AKeyStore_verifySignature(
25 key_alias: *const u8,
26 key_alias_len: usize,
27 data: *const NQuin,
28 data_len: usize,
29 signature: *const u8,
30 signature_len: usize,
31 ) -> c_int;
32 }
33}
34
35#[cfg(target_vendor = "apple")]
36pub mod apple_secure_enclave {
37 use std::ffi::c_void;
38
39 #[link(name = "Security", kind = "framework")]
40 extern "C" {
41 // CoreFoundation & Security framework native bindings for the Apple Secure Enclave
42 // `dataToSign` points to the CFDataRef wrapper of the 48-byte NQuin
43 /// `?::SecKeyCreateSignature`.
44 pub fn SecKeyCreateSignature(
45 key: *mut c_void, // SecKeyRef
46 algorithm: *mut c_void, // SecKeyAlgorithm
47 dataToSign: *mut c_void, // CFDataRef
48 error: *mut *mut c_void, // CFErrorRef
49 ) -> *mut c_void; // Returns CFDataRef signature
50
51 /// Apple Security.framework: verifies a signature in the Secure Enclave.
52 pub fn SecKeyVerifySignature(
53 key: *mut c_void, // SecKeyRef
54 algorithm: *mut c_void, // SecKeyAlgorithm
55 signedData: *mut c_void, // CFDataRef
56 signature: *mut c_void, // CFDataRef
57 error: *mut *mut c_void, // CFErrorRef
58 ) -> bool;
59 }
60}