Skip to main content

datex_crypto_facade/
crypto.rs

1use crate::error::CryptoError;
2use alloc::{boxed::Box, string::String, vec::Vec};
3use bs58;
4use core::{pin::Pin, result::Result};
5
6pub type CryptoResult<'a, T> =
7    Pin<Box<dyn Future<Output = Result<T, CryptoError>> + 'a>>;
8
9pub trait Crypto: Send + Sync {
10    /// Creates a new UUID.
11    fn create_uuid() -> String;
12
13    /// Generates cryptographically secure random bytes of the specified length.
14    fn random_bytes(length: usize) -> Vec<u8>;
15
16    /// Sha256 hash
17    fn hash_sha256<'a>(to_digest: &'a [u8]) -> CryptoResult<'a, [u8; 32]>;
18
19    /// Encodes 32 bytes to base58
20    fn enc_b58(to_encode: &[u8; 32]) -> Result<[u8; 44], CryptoError> {
21        let mut out_buf = [0u8; 44];
22        bs58::encode(to_encode)
23            .onto(&mut out_buf[..])
24            .map_err(|_| CryptoError::Decryption)?;
25        Ok(out_buf)
26    }
27
28    /// Decodes 32 bytes from base58
29    fn dec_b58(to_decode: &[u8; 44]) -> Result<[u8; 32], CryptoError> {
30        let mut out_buf = [0u8; 32];
31        bs58::decode(to_decode)
32            .onto(&mut out_buf[..])
33            .map_err(|_| CryptoError::Decryption)?;
34        Ok(out_buf)
35    }
36
37    /// Hash key derivation function.
38    fn hkdf_sha256<'a>(
39        ikm: &'a [u8],
40        salt: &'a [u8],
41    ) -> CryptoResult<'a, [u8; 32]>;
42
43    /// Generates an Ed25519 key pair.
44    fn gen_ed25519<'a>() -> CryptoResult<'a, (Vec<u8>, Vec<u8>)>;
45
46    /// Signs data with the given Ed25519 private key.
47    fn sig_ed25519<'a>(
48        pri_key: &'a [u8],
49        data: &'a [u8],
50    ) -> CryptoResult<'a, [u8; 64]>;
51
52    /// Verifies an Ed25519 signature with the given public key and data.
53    fn ver_ed25519<'a>(
54        pub_key: &'a [u8],
55        sig: &'a [u8],
56        data: &'a [u8],
57    ) -> CryptoResult<'a, bool>;
58
59    /// AES-256 in CTR mode encryption, returns the ciphertext.
60    fn aes_ctr_encrypt<'a>(
61        key: &'a [u8; 32],
62        iv: &'a [u8; 16],
63        plaintext: &'a [u8],
64    ) -> CryptoResult<'a, Vec<u8>>;
65
66    /// AES-256 in CTR mode decryption, returns the plaintext.
67    fn aes_ctr_decrypt<'a>(
68        key: &'a [u8; 32],
69        iv: &'a [u8; 16],
70        cipher: &'a [u8],
71    ) -> CryptoResult<'a, Vec<u8>>;
72
73    /// AES Key Wrap (RFC 3394), returns the wrapped key (ciphertext).
74    fn key_upwrap<'a>(
75        kek_bytes: &'a [u8; 32],
76        rb: &'a [u8; 32],
77    ) -> CryptoResult<'a, [u8; 40]>;
78
79    /// AES Key Unwrap (RFC 3394), returns the unwrapped key (plaintext).
80    fn key_unwrap<'a>(
81        kek_bytes: &'a [u8; 32],
82        cipher: &'a [u8; 40],
83    ) -> CryptoResult<'a, [u8; 32]>;
84
85    /// Generates an X25519 key pair, returns (public_key, private_key).
86    fn gen_x25519<'a>() -> CryptoResult<'a, ([u8; 44], [u8; 48])>;
87
88    /// Derives a shared secret using X25519 given my private key and the peer's public key.
89    fn derive_x25519<'a>(
90        pri_key: &'a [u8; 48],
91        peer_pub: &'a [u8; 44],
92    ) -> CryptoResult<'a, Vec<u8>>;
93}