datex_core/crypto/
crypto.rs

1use crate::stdlib::boxed::Box;
2use crate::stdlib::string::String;
3use crate::stdlib::vec::Vec;
4use crate::stdlib::{future::Future, pin::Pin};
5use bs58;
6use core::fmt::Display;
7use core::prelude::rust_2024::*;
8use core::result::Result;
9pub type CryptoResult<'a, T> =
10    Pin<Box<dyn Future<Output = Result<T, CryptoError>> + 'a>>;
11
12pub trait CryptoTrait: Send + Sync {
13    /// Creates a new UUID.
14    fn create_uuid(&self) -> String;
15
16    /// Generates cryptographically secure random bytes of the specified length.
17    fn random_bytes(&self, length: usize) -> Vec<u8>;
18
19    /// Sha256 hash
20    fn hash_sha256<'a>(
21        &'a self,
22        to_digest: &'a [u8],
23    ) -> CryptoResult<'a, [u8; 32]>;
24
25    /// Encodes 32 bytes to base58
26    fn enc_b58<'a>(
27        &'a self,
28        to_encode: &'a [u8; 32],
29    ) -> Result<[u8; 44], CryptoError> {
30        let mut out_buf = [0u8; 44];
31        bs58::encode(to_encode)
32            .onto(&mut out_buf[..])
33            .map_err(|_| CryptoError::Decryption)?;
34        Ok(out_buf)
35    }
36
37    /// Decodes 32 bytes from base58
38    fn dec_b58<'a>(
39        &'a self,
40        to_decode: &'a [u8; 44],
41    ) -> Result<[u8; 32], CryptoError> {
42        let mut out_buf = [0u8; 32];
43        bs58::decode(to_decode)
44            .onto(&mut out_buf[..])
45            .map_err(|_| CryptoError::Decryption)?;
46        Ok(out_buf)
47    }
48
49    /// Hash key derivation function.
50    fn hkdf_sha256<'a>(
51        &'a self,
52        ikm: &'a [u8],
53        salt: &'a [u8],
54    ) -> CryptoResult<'a, [u8; 32]>;
55
56    /// Generates an Ed25519 key pair.
57    fn gen_ed25519<'a>(&'a self) -> CryptoResult<'a, (Vec<u8>, Vec<u8>)>;
58
59    /// Signs data with the given Ed25519 private key.
60    fn sig_ed25519<'a>(
61        &'a self,
62        pri_key: &'a [u8],
63        data: &'a [u8],
64    ) -> CryptoResult<'a, [u8; 64]>;
65
66    /// Verifies an Ed25519 signature with the given public key and data.
67    fn ver_ed25519<'a>(
68        &'a self,
69        pub_key: &'a [u8],
70        sig: &'a [u8],
71        data: &'a [u8],
72    ) -> CryptoResult<'a, bool>;
73
74    /// AES-256 in CTR mode encryption, returns the ciphertext.
75    fn aes_ctr_encrypt<'a>(
76        &'a self,
77        key: &'a [u8; 32],
78        iv: &'a [u8; 16],
79        plaintext: &'a [u8],
80    ) -> CryptoResult<'a, Vec<u8>>;
81
82    /// AES-256 in CTR mode decryption, returns the plaintext.
83    fn aes_ctr_decrypt<'a>(
84        &'a self,
85        key: &'a [u8; 32],
86        iv: &'a [u8; 16],
87        cipher: &'a [u8],
88    ) -> CryptoResult<'a, Vec<u8>>;
89
90    /// AES Key Wrap (RFC 3394), returns the wrapped key (ciphertext).
91    fn key_upwrap<'a>(
92        &'a self,
93        kek_bytes: &'a [u8; 32],
94        rb: &'a [u8; 32],
95    ) -> CryptoResult<'a, [u8; 40]>;
96
97    /// AES Key Unwrap (RFC 3394), returns the unwrapped key (plaintext).
98    fn key_unwrap<'a>(
99        &'a self,
100        kek_bytes: &'a [u8; 32],
101        cipher: &'a [u8; 40],
102    ) -> CryptoResult<'a, [u8; 32]>;
103
104    /// Generates an X25519 key pair, returns (public_key, private_key).
105    fn gen_x25519<'a>(&'a self) -> CryptoResult<'a, ([u8; 44], [u8; 48])>;
106
107    /// Derives a shared secret using X25519 given my private key and the peer's public key.
108    fn derive_x25519<'a>(
109        &'a self,
110        pri_key: &'a [u8; 48],
111        peer_pub: &'a [u8; 44],
112    ) -> CryptoResult<'a, Vec<u8>>;
113}
114
115pub struct Crypto;
116
117#[derive(Debug, Clone)]
118pub enum CryptoError {
119    Other(String),
120    KeyGeneration,
121    KeyExport,
122    KeyImport,
123    Encryption,
124    Decryption,
125    Signing,
126    Verification,
127}
128
129impl Display for CryptoError {
130    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
131        match self {
132            CryptoError::Other(msg) => core::write!(f, "Crypto: {}", msg),
133            CryptoError::KeyGeneration => {
134                core::write!(f, "CryptoError: Key generation failed")
135            }
136            CryptoError::KeyExport => {
137                core::write!(f, "CryptoError: Key export failed")
138            }
139            CryptoError::KeyImport => {
140                core::write!(f, "CryptoError: Key import failed")
141            }
142            CryptoError::Encryption => {
143                core::write!(f, "CryptoError: Encryption failed")
144            }
145            CryptoError::Decryption => {
146                core::write!(f, "CryptoError: Decryption failed")
147            }
148            CryptoError::Signing => {
149                core::write!(f, "CryptoError: Signing failed")
150            }
151            CryptoError::Verification => {
152                core::write!(f, "CryptoError: Verification failed")
153            }
154        }
155    }
156}