datex_core/crypto/
crypto.rs

1use std::fmt::Display;
2
3use crate::stdlib::{future::Future, pin::Pin};
4pub trait CryptoTrait: Send + Sync {
5    /// Creates a new UUID.
6    fn create_uuid(&self) -> String;
7
8    /// Generates cryptographically secure random bytes of the specified length.
9    fn random_bytes(&self, length: usize) -> Vec<u8>;
10
11    /// Generates an Ed25519 key pair.
12    fn gen_ed25519(
13        &self,
14    ) -> Pin<
15        Box<
16            dyn Future<Output = Result<(Vec<u8>, Vec<u8>), CryptoError>>
17                + 'static,
18        >,
19    >;
20
21    /// Signs data with the given Ed25519 private key.
22    fn sig_ed25519<'a>(
23        &'a self,
24        pri_key: &'a [u8],
25        data: &'a [u8],
26    ) -> Pin<Box<dyn Future<Output = Result<[u8; 64], CryptoError>> + 'a>>;
27
28    /// Verifies an Ed25519 signature with the given public key and data.
29    fn ver_ed25519<'a>(
30        &'a self,
31        pub_key: &'a [u8],
32        sig: &'a [u8],
33        data: &'a [u8],
34    ) -> Pin<Box<dyn Future<Output = Result<bool, CryptoError>> + 'a>>;
35
36    /// AES-256 in CTR mode encryption, returns the ciphertext.
37    fn aes_ctr_encrypt<'a>(
38        &'a self,
39        key: &'a [u8; 32],
40        iv: &'a [u8; 16],
41        plaintext: &'a [u8],
42    ) -> Pin<Box<dyn Future<Output = Result<Vec<u8>, CryptoError>> + 'a>>;
43
44    /// AES-256 in CTR mode decryption, returns the plaintext.
45    fn aes_ctr_decrypt<'a>(
46        &'a self,
47        key: &'a [u8; 32],
48        iv: &'a [u8; 16],
49        cipher: &'a [u8],
50    ) -> Pin<Box<dyn Future<Output = Result<Vec<u8>, CryptoError>> + 'a>>;
51
52    /// AES Key Wrap (RFC 3394), returns the wrapped key (ciphertext).
53    fn key_upwrap<'a>(
54        &'a self,
55        kek_bytes: &'a [u8; 32],
56        rb: &'a [u8; 32],
57    ) -> Pin<Box<dyn Future<Output = Result<[u8; 40], CryptoError>> + 'a>>;
58
59    /// AES Key Unwrap (RFC 3394), returns the unwrapped key (plaintext).
60    fn key_unwrap<'a>(
61        &'a self,
62        kek_bytes: &'a [u8; 32],
63        cipher: &'a [u8; 40],
64    ) -> Pin<Box<dyn Future<Output = Result<[u8; 32], CryptoError>> + 'a>>;
65
66    /// Generates an X25519 key pair, returns (public_key, private_key).
67    fn gen_x25519(
68        &self,
69    ) -> Pin<Box<dyn Future<Output = Result<([u8; 44], [u8; 48]), CryptoError>>>>;
70
71    /// Derives a shared secret using X25519 given my private key and the peer's public key.
72    fn derive_x25519<'a>(
73        &'a self,
74        pri_key: &'a [u8; 48],
75        peer_pub: &'a [u8; 44],
76    ) -> Pin<Box<dyn Future<Output = Result<Vec<u8>, CryptoError>> + 'a>>;
77}
78
79pub struct Crypto;
80
81#[derive(Debug, Clone)]
82pub enum CryptoError {
83    Other(String),
84    KeyGeneratorFailed,
85    KeyExportFailed,
86    KeyImportFailed,
87    EncryptionError,
88    DecryptionError,
89    SigningError,
90    VerificationError,
91}
92
93impl Display for CryptoError {
94    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
95        match self {
96            CryptoError::Other(msg) => write!(f, "CryptoError: {}", msg),
97            CryptoError::KeyGeneratorFailed => {
98                write!(f, "CryptoError: Key generation failed")
99            }
100            CryptoError::KeyExportFailed => {
101                write!(f, "CryptoError: Key export failed")
102            }
103            CryptoError::KeyImportFailed => {
104                write!(f, "CryptoError: Key import failed")
105            }
106            CryptoError::EncryptionError => {
107                write!(f, "CryptoError: Encryption failed")
108            }
109            CryptoError::DecryptionError => {
110                write!(f, "CryptoError: Decryption failed")
111            }
112            CryptoError::SigningError => {
113                write!(f, "CryptoError: Signing failed")
114            }
115            CryptoError::VerificationError => {
116                write!(f, "CryptoError: Verification failed")
117            }
118        }
119    }
120}