Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
5ee56fb
sketched pattern for sync and async return types
janiejestemja Nov 29, 2025
79f93e8
it's a mess :sob: but it works :partying_face:
janiejestemja Nov 29, 2025
cd6b275
add MaybeAsync to CryptoTrait
janiejestemja Nov 30, 2025
1a19c82
refactor MaybeAsync
janiejestemja Dec 1, 2025
dfa1cb9
add signatures beneath native crypto flag to comhub
janiejestemja Dec 1, 2025
ffa1abc
rename errors for consistency and nostd adjustments
janiejestemja Dec 1, 2025
4a3ad6f
cut of padding of signatures
janiejestemja Dec 3, 2025
fc9fded
refactored function definitions
janiejestemja Dec 3, 2025
83c6bf9
add hkdf to cryptotrait
janiejestemja Dec 4, 2025
dd8cfd6
edits around hkdf
janiejestemja Dec 4, 2025
e202624
add placeholder implementation for encrypted signatures
janiejestemja Dec 4, 2025
8de73e9
add sha256 hash function and add body to signed data
janiejestemja Dec 4, 2025
81703ad
propagate async and outfactor maybe async scaffolding
janiejestemja Dec 9, 2025
29b8310
remove maybe async enum
janiejestemja Dec 9, 2025
8acc079
revert edit in mock setup
janiejestemja Dec 9, 2025
35411b6
change async-stream to futures async stream
janiejestemja Dec 9, 2025
4460c2b
removed some async
janiejestemja Dec 9, 2025
c9a9c6c
remove some more async
janiejestemja Dec 10, 2025
08ff8bb
edits addressing peer reviews and PR conversations
janiejestemja Dec 10, 2025
8399db1
addressing #219
janiejestemja Dec 12, 2025
57cb2b7
addressing #111 and #112
janiejestemja Dec 12, 2025
2e0bccf
edits in errorhandling
janiejestemja Dec 13, 2025
02bd417
edits in com hubs error handling
janiejestemja Dec 13, 2025
804f2a8
renamed unit tests
janiejestemja Dec 13, 2025
a7d11bb
add hex and base64 to native crypto
janiejestemja Dec 13, 2025
f8a56d2
propagate errors through com hub
janiejestemja Dec 14, 2025
3658ee8
addressing #178
janiejestemja Dec 15, 2025
2d3b491
fix typo in comment...
janiejestemja Dec 15, 2025
b8c0152
Revert "add hex and base64 to native crypto"
janiejestemja Dec 15, 2025
5bd7454
add base58 fingerprints
janiejestemja Dec 15, 2025
d9c7013
edit base58 for nostd
janiejestemja Dec 15, 2025
ce310e1
add comments to base58 functions
janiejestemja Dec 16, 2025
4ec51f7
add ttl equals zero check to block redirect in com hub
janiejestemja Dec 16, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ serde-big-array = { version = "0.5.1", optional = true }
realhydroper-lsp = { version = "0.22.0", optional = true, default-features = false, features = [
"proposed",
] }
bs58 = { version = "0.5.1", default-features = false }

# macros
strum_macros = { version = "0.27.1" }
Expand Down
99 changes: 65 additions & 34 deletions src/crypto/crypto.rs
Original file line number Diff line number Diff line change
@@ -1,123 +1,154 @@
use crate::stdlib::boxed::Box;
use crate::stdlib::string::String;
use crate::stdlib::vec::Vec;
use crate::stdlib::{future::Future, pin::Pin};
use bs58;
use core::fmt::Display;
use core::prelude::rust_2024::*;
use core::result::Result;
pub type CryptoResult<'a, T> =
Pin<Box<dyn Future<Output = Result<T, CryptoError>> + 'a>>;

use crate::stdlib::{future::Future, pin::Pin};
pub trait CryptoTrait: Send + Sync {
/// Creates a new UUID.
fn create_uuid(&self) -> String;

/// Generates cryptographically secure random bytes of the specified length.
fn random_bytes(&self, length: usize) -> Vec<u8>;

/// Sha256 hash
fn hash_sha256<'a>(
&'a self,
to_digest: &'a [u8],
) -> CryptoResult<'a, [u8; 32]>;

/// Encodes 32 bytes to base58
fn enc_b58<'a>(
&'a self,
to_encode: &'a [u8; 32],
) -> Result<[u8; 44], CryptoError> {
let mut out_buf = [0u8; 44];
bs58::encode(to_encode)
.onto(&mut out_buf[..])
.map_err(|_| CryptoError::Decryption)?;
Ok(out_buf)
}

/// Decodes 32 bytes from base58
fn dec_b58<'a>(
&'a self,
to_decode: &'a [u8; 44],
) -> Result<[u8; 32], CryptoError> {
let mut out_buf = [0u8; 32];
bs58::decode(to_decode)
.onto(&mut out_buf[..])
.map_err(|_| CryptoError::Decryption)?;
Ok(out_buf)
}

/// Hash key derivation function.
fn hkdf_sha256<'a>(
&'a self,
ikm: &'a [u8],
salt: &'a [u8],
) -> CryptoResult<'a, [u8; 32]>;

/// Generates an Ed25519 key pair.
fn gen_ed25519(
&self,
) -> Pin<
Box<
dyn Future<Output = Result<(Vec<u8>, Vec<u8>), CryptoError>>
+ 'static,
>,
>;
fn gen_ed25519<'a>(&'a self) -> CryptoResult<'a, (Vec<u8>, Vec<u8>)>;

/// Signs data with the given Ed25519 private key.
fn sig_ed25519<'a>(
&'a self,
pri_key: &'a [u8],
data: &'a [u8],
) -> Pin<Box<dyn Future<Output = Result<[u8; 64], CryptoError>> + 'a>>;
) -> CryptoResult<'a, [u8; 64]>;

/// Verifies an Ed25519 signature with the given public key and data.
fn ver_ed25519<'a>(
&'a self,
pub_key: &'a [u8],
sig: &'a [u8],
data: &'a [u8],
) -> Pin<Box<dyn Future<Output = Result<bool, CryptoError>> + 'a>>;
) -> CryptoResult<'a, bool>;

/// AES-256 in CTR mode encryption, returns the ciphertext.
fn aes_ctr_encrypt<'a>(
&'a self,
key: &'a [u8; 32],
iv: &'a [u8; 16],
plaintext: &'a [u8],
) -> Pin<Box<dyn Future<Output = Result<Vec<u8>, CryptoError>> + 'a>>;
) -> CryptoResult<'a, Vec<u8>>;

/// AES-256 in CTR mode decryption, returns the plaintext.
fn aes_ctr_decrypt<'a>(
&'a self,
key: &'a [u8; 32],
iv: &'a [u8; 16],
cipher: &'a [u8],
) -> Pin<Box<dyn Future<Output = Result<Vec<u8>, CryptoError>> + 'a>>;
) -> CryptoResult<'a, Vec<u8>>;

/// AES Key Wrap (RFC 3394), returns the wrapped key (ciphertext).
fn key_upwrap<'a>(
&'a self,
kek_bytes: &'a [u8; 32],
rb: &'a [u8; 32],
) -> Pin<Box<dyn Future<Output = Result<[u8; 40], CryptoError>> + 'a>>;
) -> CryptoResult<'a, [u8; 40]>;

/// AES Key Unwrap (RFC 3394), returns the unwrapped key (plaintext).
fn key_unwrap<'a>(
&'a self,
kek_bytes: &'a [u8; 32],
cipher: &'a [u8; 40],
) -> Pin<Box<dyn Future<Output = Result<[u8; 32], CryptoError>> + 'a>>;
) -> CryptoResult<'a, [u8; 32]>;

/// Generates an X25519 key pair, returns (public_key, private_key).
fn gen_x25519(
&self,
) -> Pin<Box<dyn Future<Output = Result<([u8; 44], [u8; 48]), CryptoError>>>>;
fn gen_x25519<'a>(&'a self) -> CryptoResult<'a, ([u8; 44], [u8; 48])>;

/// Derives a shared secret using X25519 given my private key and the peer's public key.
fn derive_x25519<'a>(
&'a self,
pri_key: &'a [u8; 48],
peer_pub: &'a [u8; 44],
) -> Pin<Box<dyn Future<Output = Result<Vec<u8>, CryptoError>> + 'a>>;
) -> CryptoResult<'a, Vec<u8>>;
}

pub struct Crypto;

#[derive(Debug, Clone)]
pub enum CryptoError {
Other(String),
KeyGeneratorFailed,
KeyExportFailed,
KeyImportFailed,
EncryptionError,
DecryptionError,
SigningError,
VerificationError,
KeyGeneration,
KeyExport,
KeyImport,
Encryption,
Decryption,
Signing,
Verification,
}

impl Display for CryptoError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
CryptoError::Other(msg) => core::write!(f, "CryptoError: {}", msg),
CryptoError::KeyGeneratorFailed => {
CryptoError::Other(msg) => core::write!(f, "Crypto: {}", msg),
CryptoError::KeyGeneration => {
core::write!(f, "CryptoError: Key generation failed")
}
CryptoError::KeyExportFailed => {
CryptoError::KeyExport => {
core::write!(f, "CryptoError: Key export failed")
}
CryptoError::KeyImportFailed => {
CryptoError::KeyImport => {
core::write!(f, "CryptoError: Key import failed")
}
CryptoError::EncryptionError => {
CryptoError::Encryption => {
core::write!(f, "CryptoError: Encryption failed")
}
CryptoError::DecryptionError => {
CryptoError::Decryption => {
core::write!(f, "CryptoError: Decryption failed")
}
CryptoError::SigningError => {
CryptoError::Signing => {
core::write!(f, "CryptoError: Signing failed")
}
CryptoError::VerificationError => {
CryptoError::Verification => {
core::write!(f, "CryptoError: Verification failed")
}
}
Expand Down
Loading
Loading