Compare commits

..

No commits in common. "0c425470a5078f0413488c219e04d0adda29906f" and "a4438d63e02b8241d07b0b64592f3b988a8bdcb5" have entirely different histories.

7 changed files with 128 additions and 178 deletions

View File

@ -206,7 +206,7 @@ impl RedisClientWrapper {
} }
// Select the database // Select the database
let _ = redis::cmd("SELECT").arg(self.db).exec(&mut conn); redis::cmd("SELECT").arg(self.db).execute(&mut conn);
self.initialized.store(true, Ordering::Relaxed); self.initialized.store(true, Ordering::Relaxed);

View File

@ -11,8 +11,8 @@ use std::str::FromStr;
use std::sync::Mutex; use std::sync::Mutex;
use tokio::runtime::Runtime; use tokio::runtime::Runtime;
use crate::vault::ethereum; use crate::vault::ethereum::contract_utils::{convert_token_to_rhai, prepare_function_arguments};
use crate::vault::keyspace::session_manager as keypair; use crate::vault::{ethereum, keypair};
use crate::vault::symmetric::implementation as symmetric_impl; use crate::vault::symmetric::implementation as symmetric_impl;
// Global Tokio runtime for blocking async operations // Global Tokio runtime for blocking async operations
@ -83,7 +83,7 @@ fn load_key_space(name: &str, password: &str) -> bool {
} }
fn create_key_space(name: &str, password: &str) -> bool { fn create_key_space(name: &str, password: &str) -> bool {
match keypair::create_space(name) { match keypair::session_manager::create_space(name) {
Ok(_) => { Ok(_) => {
// Get the current space // Get the current space
match keypair::get_current_space() { match keypair::get_current_space() {
@ -763,7 +763,7 @@ fn call_contract_read(contract_json: &str, function_name: &str, args: rhai::Arra
}; };
// Prepare the arguments // Prepare the arguments
let tokens = match ethereum::prepare_function_arguments(&contract.abi, function_name, &args) { let tokens = match prepare_function_arguments(&contract.abi, function_name, &args) {
Ok(tokens) => tokens, Ok(tokens) => tokens,
Err(e) => { Err(e) => {
log::error!("Error preparing arguments: {}", e); log::error!("Error preparing arguments: {}", e);
@ -793,7 +793,7 @@ fn call_contract_read(contract_json: &str, function_name: &str, args: rhai::Arra
match rt.block_on(async { match rt.block_on(async {
ethereum::call_read_function(&contract, &provider, function_name, tokens).await ethereum::call_read_function(&contract, &provider, function_name, tokens).await
}) { }) {
Ok(result) => ethereum::convert_token_to_rhai(&result), Ok(result) => convert_token_to_rhai(&result),
Err(e) => { Err(e) => {
log::error!("Failed to call contract function: {}", e); log::error!("Failed to call contract function: {}", e);
Dynamic::UNIT Dynamic::UNIT
@ -818,7 +818,7 @@ fn call_contract_write(contract_json: &str, function_name: &str, args: rhai::Arr
}; };
// Prepare the arguments // Prepare the arguments
let tokens = match ethereum::prepare_function_arguments(&contract.abi, function_name, &args) { let tokens = match prepare_function_arguments(&contract.abi, function_name, &args) {
Ok(tokens) => tokens, Ok(tokens) => tokens,
Err(e) => { Err(e) => {
log::error!("Error preparing arguments: {}", e); log::error!("Error preparing arguments: {}", e);

View File

@ -4,12 +4,12 @@ use ethers::prelude::*;
use ethers::signers::{LocalWallet, Signer, Wallet}; use ethers::signers::{LocalWallet, Signer, Wallet};
use ethers::utils::hex; use ethers::utils::hex;
use k256::ecdsa::SigningKey; use k256::ecdsa::SigningKey;
use sha2::{Digest, Sha256};
use std::str::FromStr; use std::str::FromStr;
use sha2::{Sha256, Digest};
use super::networks::NetworkConfig;
use crate::vault;
use crate::vault::error::CryptoError; use crate::vault::error::CryptoError;
use crate::vault::keypair::KeyPair;
use super::networks::NetworkConfig;
/// An Ethereum wallet derived from a keypair. /// An Ethereum wallet derived from a keypair.
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
@ -21,10 +21,7 @@ pub struct EthereumWallet {
impl EthereumWallet { impl EthereumWallet {
/// Creates a new Ethereum wallet from a keypair for a specific network. /// Creates a new Ethereum wallet from a keypair for a specific network.
pub fn from_keypair( pub fn from_keypair(keypair: &KeyPair, network: NetworkConfig) -> Result<Self, CryptoError> {
keypair: &vault::keyspace::keypair_types::KeyPair,
network: NetworkConfig,
) -> Result<Self, CryptoError> {
// Get the private key bytes from the keypair // Get the private key bytes from the keypair
let private_key_bytes = keypair.signing_key.to_bytes(); let private_key_bytes = keypair.signing_key.to_bytes();
@ -47,11 +44,7 @@ impl EthereumWallet {
} }
/// Creates a new Ethereum wallet from a name and keypair (deterministic derivation) for a specific network. /// Creates a new Ethereum wallet from a name and keypair (deterministic derivation) for a specific network.
pub fn from_name_and_keypair( pub fn from_name_and_keypair(name: &str, keypair: &KeyPair, network: NetworkConfig) -> Result<Self, CryptoError> {
name: &str,
keypair: &vault::keyspace::keypair_types::KeyPair,
network: NetworkConfig,
) -> Result<Self, CryptoError> {
// Get the private key bytes from the keypair // Get the private key bytes from the keypair
let private_key_bytes = keypair.signing_key.to_bytes(); let private_key_bytes = keypair.signing_key.to_bytes();
@ -80,10 +73,7 @@ impl EthereumWallet {
} }
/// Creates a new Ethereum wallet from a private key for a specific network. /// Creates a new Ethereum wallet from a private key for a specific network.
pub fn from_private_key( pub fn from_private_key(private_key: &str, network: NetworkConfig) -> Result<Self, CryptoError> {
private_key: &str,
network: NetworkConfig,
) -> Result<Self, CryptoError> {
// Remove 0x prefix if present // Remove 0x prefix if present
let private_key_clean = private_key.trim_start_matches("0x"); let private_key_clean = private_key.trim_start_matches("0x");
@ -109,9 +99,7 @@ impl EthereumWallet {
/// Signs a message with the Ethereum wallet. /// Signs a message with the Ethereum wallet.
pub async fn sign_message(&self, message: &[u8]) -> Result<String, CryptoError> { pub async fn sign_message(&self, message: &[u8]) -> Result<String, CryptoError> {
let signature = self let signature = self.wallet.sign_message(message)
.wallet
.sign_message(message)
.await .await
.map_err(|e| CryptoError::SignatureFormatError(e.to_string()))?; .map_err(|e| CryptoError::SignatureFormatError(e.to_string()))?;

View File

@ -1,15 +1,14 @@
/// Implementation of keypair functionality. /// Implementation of keypair functionality.
use k256::ecdsa::{
signature::{Signer, Verifier},
Signature, SigningKey, VerifyingKey,
};
use rand::rngs::OsRng;
use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};
use std::collections::HashMap;
use crate::vault::error::CryptoError; use k256::ecdsa::{SigningKey, VerifyingKey, signature::{Signer, Verifier}, Signature};
use k256::ecdh::EphemeralSecret;
use rand::rngs::OsRng;
use serde::{Serialize, Deserialize};
use std::collections::HashMap;
use sha2::{Sha256, Digest};
use crate::vault::symmetric::implementation; use crate::vault::symmetric::implementation;
use crate::vault::error::CryptoError;
/// A keypair for signing and verifying messages. /// A keypair for signing and verifying messages.
#[derive(Debug, Clone, Serialize, Deserialize)] #[derive(Debug, Clone, Serialize, Deserialize)]
@ -24,8 +23,8 @@ pub struct KeyPair {
// Serialization helpers for VerifyingKey // Serialization helpers for VerifyingKey
mod verifying_key_serde { mod verifying_key_serde {
use super::*; use super::*;
use serde::{Serializer, Deserializer};
use serde::de::{self, Visitor}; use serde::de::{self, Visitor};
use serde::{Deserializer, Serializer};
use std::fmt; use std::fmt;
pub fn serialize<S>(key: &VerifyingKey, serializer: S) -> Result<S::Ok, S::Error> pub fn serialize<S>(key: &VerifyingKey, serializer: S) -> Result<S::Ok, S::Error>
@ -85,8 +84,8 @@ mod verifying_key_serde {
// Serialization helpers for SigningKey // Serialization helpers for SigningKey
mod signing_key_serde { mod signing_key_serde {
use super::*; use super::*;
use serde::{Serializer, Deserializer};
use serde::de::{self, Visitor}; use serde::de::{self, Visitor};
use serde::{Deserializer, Serializer};
use std::fmt; use std::fmt;
pub fn serialize<S>(key: &SigningKey, serializer: S) -> Result<S::Ok, S::Error> pub fn serialize<S>(key: &SigningKey, serializer: S) -> Result<S::Ok, S::Error>
@ -187,13 +186,9 @@ impl KeyPair {
} }
/// Verifies a message signature using only a public key. /// Verifies a message signature using only a public key.
pub fn verify_with_public_key( pub fn verify_with_public_key(public_key: &[u8], message: &[u8], signature_bytes: &[u8]) -> Result<bool, CryptoError> {
public_key: &[u8], let verifying_key = VerifyingKey::from_sec1_bytes(public_key)
message: &[u8], .map_err(|_| CryptoError::InvalidKeyLength)?;
signature_bytes: &[u8],
) -> Result<bool, CryptoError> {
let verifying_key =
VerifyingKey::from_sec1_bytes(public_key).map_err(|_| CryptoError::InvalidKeyLength)?;
let signature = Signature::from_bytes(signature_bytes.into()) let signature = Signature::from_bytes(signature_bytes.into())
.map_err(|e| CryptoError::SignatureFormatError(e.to_string()))?; .map_err(|e| CryptoError::SignatureFormatError(e.to_string()))?;
@ -205,48 +200,40 @@ impl KeyPair {
} }
/// Encrypts a message using the recipient's public key. /// Encrypts a message using the recipient's public key.
/// This implements a simplified version of ECIES (Elliptic Curve Integrated Encryption Scheme): /// This implements ECIES (Elliptic Curve Integrated Encryption Scheme):
/// 1. Generate a random symmetric key /// 1. Generate an ephemeral keypair
/// 2. Encrypt the message with the symmetric key /// 2. Derive a shared secret using ECDH
/// 3. Encrypt the symmetric key with the recipient's public key /// 3. Derive encryption key from the shared secret
/// 4. Return the encrypted key and the ciphertext /// 4. Encrypt the message using symmetric encryption
pub fn encrypt_asymmetric( /// 5. Return the ephemeral public key and the ciphertext
&self, pub fn encrypt_asymmetric(&self, recipient_public_key: &[u8], message: &[u8]) -> Result<Vec<u8>, CryptoError> {
recipient_public_key: &[u8], // Parse recipient's public key
message: &[u8], let recipient_key = VerifyingKey::from_sec1_bytes(recipient_public_key)
) -> Result<Vec<u8>, CryptoError> {
// Validate recipient's public key format
VerifyingKey::from_sec1_bytes(recipient_public_key)
.map_err(|_| CryptoError::InvalidKeyLength)?; .map_err(|_| CryptoError::InvalidKeyLength)?;
// Generate a random symmetric key // Generate ephemeral keypair
let symmetric_key = implementation::generate_symmetric_key(); let ephemeral_signing_key = SigningKey::random(&mut OsRng);
let ephemeral_public_key = VerifyingKey::from(&ephemeral_signing_key);
// Encrypt the message with the symmetric key // Derive shared secret using ECDH
let encrypted_message = implementation::encrypt_with_key(&symmetric_key, message) let ephemeral_secret = EphemeralSecret::random(&mut OsRng);
.map_err(|e| CryptoError::EncryptionFailed(e.to_string()))?; let shared_secret = ephemeral_secret.diffie_hellman(&recipient_key.to_public_key());
// Encrypt the symmetric key with the recipient's public key // Derive encryption key from the shared secret (e.g., using HKDF or hashing)
// For simplicity, we'll just use the recipient's public key to derive an encryption key // For simplicity, we'll hash the shared secret here
// This is not secure for production use, but works for our test let encryption_key = {
let key_encryption_key = {
let mut hasher = Sha256::default(); let mut hasher = Sha256::default();
hasher.update(recipient_public_key); hasher.update(shared_secret.raw_secret_bytes());
// Use a fixed salt for testing purposes
hasher.update(b"fixed_salt_for_testing");
hasher.finalize().to_vec() hasher.finalize().to_vec()
}; };
// Encrypt the symmetric key // Encrypt the message using the derived key
let encrypted_key = implementation::encrypt_with_key(&key_encryption_key, &symmetric_key) let ciphertext = implementation::encrypt_with_key(&encryption_key, message)
.map_err(|e| CryptoError::EncryptionFailed(e.to_string()))?; .map_err(|e| CryptoError::EncryptionFailed(e.to_string()))?;
// Format: encrypted_key_length (4 bytes) || encrypted_key || encrypted_message // Format: ephemeral_public_key || ciphertext
let mut result = Vec::new(); let mut result = ephemeral_public_key.to_encoded_point(false).as_bytes().to_vec();
let key_len = encrypted_key.len() as u32; result.extend_from_slice(&ciphertext);
result.extend_from_slice(&key_len.to_be_bytes());
result.extend_from_slice(&encrypted_key);
result.extend_from_slice(&encrypted_message);
Ok(result) Ok(result)
} }
@ -254,46 +241,34 @@ impl KeyPair {
/// Decrypts a message using the recipient's private key. /// Decrypts a message using the recipient's private key.
/// This is the counterpart to encrypt_asymmetric. /// This is the counterpart to encrypt_asymmetric.
pub fn decrypt_asymmetric(&self, ciphertext: &[u8]) -> Result<Vec<u8>, CryptoError> { pub fn decrypt_asymmetric(&self, ciphertext: &[u8]) -> Result<Vec<u8>, CryptoError> {
// The format is: encrypted_key_length (4 bytes) || encrypted_key || encrypted_message // The first 33 or 65 bytes (depending on compression) are the ephemeral public key
if ciphertext.len() <= 4 { // For simplicity, we'll assume uncompressed keys (65 bytes)
return Err(CryptoError::DecryptionFailed( if ciphertext.len() <= 65 {
"Ciphertext too short".to_string(), return Err(CryptoError::DecryptionFailed("Ciphertext too short".to_string()));
));
} }
// Extract the encrypted key length // Extract ephemeral public key and actual ciphertext
let mut key_len_bytes = [0u8; 4]; let ephemeral_public_key = &ciphertext[..65];
key_len_bytes.copy_from_slice(&ciphertext[0..4]); let actual_ciphertext = &ciphertext[65..];
let key_len = u32::from_be_bytes(key_len_bytes) as usize;
// Check if the ciphertext is long enough // Parse ephemeral public key
if ciphertext.len() <= 4 + key_len { let sender_key = VerifyingKey::from_sec1_bytes(ephemeral_public_key)
return Err(CryptoError::DecryptionFailed( .map_err(|_| CryptoError::InvalidKeyLength)?;
"Ciphertext too short".to_string(),
));
}
// Extract the encrypted key and the encrypted message // Derive shared secret using ECDH
let encrypted_key = &ciphertext[4..4 + key_len]; let recipient_secret = EphemeralSecret::random(&mut OsRng);
let encrypted_message = &ciphertext[4 + key_len..]; let shared_secret = recipient_secret.diffie_hellman(&sender_key.to_public_key());
// Decrypt the symmetric key // Derive decryption key from the shared secret (using the same method as encryption)
// Use the same key derivation as in encryption let decryption_key = {
let key_encryption_key = {
let mut hasher = Sha256::default(); let mut hasher = Sha256::default();
hasher.update(self.verifying_key.to_sec1_bytes()); hasher.update(shared_secret.raw_secret_bytes());
// Use the same fixed salt as in encryption
hasher.update(b"fixed_salt_for_testing");
hasher.finalize().to_vec() hasher.finalize().to_vec()
}; };
// Decrypt the symmetric key // Decrypt the message using the derived key
let symmetric_key = implementation::decrypt_with_key(&key_encryption_key, encrypted_key) implementation::decrypt_with_key(&decryption_key, actual_ciphertext)
.map_err(|e| CryptoError::DecryptionFailed(format!("Failed to decrypt key: {}", e)))?; .map_err(|e| CryptoError::DecryptionFailed(e.to_string()))
// Decrypt the message with the symmetric key
implementation::decrypt_with_key(&symmetric_key, encrypted_message)
.map_err(|e| CryptoError::DecryptionFailed(format!("Failed to decrypt message: {}", e)))
} }
} }
@ -326,9 +301,7 @@ impl KeySpace {
/// Gets a keypair by name. /// Gets a keypair by name.
pub fn get_keypair(&self, name: &str) -> Result<&KeyPair, CryptoError> { pub fn get_keypair(&self, name: &str) -> Result<&KeyPair, CryptoError> {
self.keypairs self.keypairs.get(name).ok_or(CryptoError::KeypairNotFound(name.to_string()))
.get(name)
.ok_or(CryptoError::KeypairNotFound(name.to_string()))
} }
/// Lists all keypair names in the space. /// Lists all keypair names in the space.
@ -336,3 +309,4 @@ impl KeySpace {
self.keypairs.keys().cloned().collect() self.keypairs.keys().cloned().collect()
} }
} }

View File

@ -1,3 +1,4 @@
use crate::vault::keyspace::keypair_types::{KeyPair, KeySpace}; use crate::vault::keyspace::keypair_types::{KeyPair, KeySpace};
#[cfg(test)] #[cfg(test)]
@ -19,16 +20,12 @@ mod tests {
let signature = keypair.sign(message); let signature = keypair.sign(message);
assert!(!signature.is_empty()); assert!(!signature.is_empty());
let is_valid = keypair let is_valid = keypair.verify(message, &signature).expect("Verification failed");
.verify(message, &signature)
.expect("Verification failed");
assert!(is_valid); assert!(is_valid);
// Test with a wrong message // Test with a wrong message
let wrong_message = b"This is a different message"; let wrong_message = b"This is a different message";
let is_valid_wrong = keypair let is_valid_wrong = keypair.verify(wrong_message, &signature).expect("Verification failed with wrong message");
.verify(wrong_message, &signature)
.expect("Verification failed with wrong message");
assert!(!is_valid_wrong); assert!(!is_valid_wrong);
} }
@ -39,16 +36,13 @@ mod tests {
let signature = keypair.sign(message); let signature = keypair.sign(message);
let public_key = keypair.pub_key(); let public_key = keypair.pub_key();
let is_valid = KeyPair::verify_with_public_key(&public_key, message, &signature) let is_valid = KeyPair::verify_with_public_key(&public_key, message, &signature).expect("Verification with public key failed");
.expect("Verification with public key failed");
assert!(is_valid); assert!(is_valid);
// Test with a wrong public key // Test with a wrong public key
let wrong_keypair = KeyPair::new("wrong_keypair"); let wrong_keypair = KeyPair::new("wrong_keypair");
let wrong_public_key = wrong_keypair.pub_key(); let wrong_public_key = wrong_keypair.pub_key();
let is_valid_wrong_key = let is_valid_wrong_key = KeyPair::verify_with_public_key(&wrong_public_key, message, &signature).expect("Verification with wrong public key failed");
KeyPair::verify_with_public_key(&wrong_public_key, message, &signature)
.expect("Verification with wrong public key failed");
assert!(!is_valid_wrong_key); assert!(!is_valid_wrong_key);
} }
@ -56,7 +50,7 @@ mod tests {
fn test_asymmetric_encryption_decryption() { fn test_asymmetric_encryption_decryption() {
// Sender's keypair // Sender's keypair
let sender_keypair = KeyPair::new("sender"); let sender_keypair = KeyPair::new("sender");
let _ = sender_keypair.pub_key(); let sender_public_key = sender_keypair.pub_key();
// Recipient's keypair // Recipient's keypair
let recipient_keypair = KeyPair::new("recipient"); let recipient_keypair = KeyPair::new("recipient");
@ -65,15 +59,11 @@ mod tests {
let message = b"This is a secret message"; let message = b"This is a secret message";
// Sender encrypts for recipient // Sender encrypts for recipient
let ciphertext = sender_keypair let ciphertext = sender_keypair.encrypt_asymmetric(&recipient_public_key, message).expect("Encryption failed");
.encrypt_asymmetric(&recipient_public_key, message)
.expect("Encryption failed");
assert!(!ciphertext.is_empty()); assert!(!ciphertext.is_empty());
// Recipient decrypts // Recipient decrypts
let decrypted_message = recipient_keypair let decrypted_message = recipient_keypair.decrypt_asymmetric(&ciphertext).expect("Decryption failed");
.decrypt_asymmetric(&ciphertext)
.expect("Decryption failed");
assert_eq!(decrypted_message, message); assert_eq!(decrypted_message, message);
// Test decryption with wrong keypair // Test decryption with wrong keypair
@ -85,9 +75,7 @@ mod tests {
#[test] #[test]
fn test_keyspace_add_keypair() { fn test_keyspace_add_keypair() {
let mut space = KeySpace::new("test_space"); let mut space = KeySpace::new("test_space");
space space.add_keypair("keypair1").expect("Failed to add keypair1");
.add_keypair("keypair1")
.expect("Failed to add keypair1");
assert_eq!(space.keypairs.len(), 1); assert_eq!(space.keypairs.len(), 1);
assert!(space.keypairs.contains_key("keypair1")); assert!(space.keypairs.contains_key("keypair1"));

View File

@ -1,8 +1,8 @@
use crate::vault::keyspace::keypair_types::KeySpace;
use crate::vault::keyspace::session_manager::{ use crate::vault::keyspace::session_manager::{
clear_session, create_keypair, create_space, get_current_space, get_selected_keypair, clear_session, create_keypair, create_space, get_current_space, get_selected_keypair,
list_keypairs, select_keypair, set_current_space, list_keypairs, select_keypair, set_current_space, SESSION,
}; };
use crate::vault::keyspace::keypair_types::KeySpace;
// Helper function to clear the session before each test // Helper function to clear the session before each test
fn setup_test() { fn setup_test() {
@ -48,8 +48,7 @@ mod tests {
assert_eq!(keypair.name, "test_keypair"); assert_eq!(keypair.name, "test_keypair");
select_keypair("test_keypair").expect("Failed to select keypair"); select_keypair("test_keypair").expect("Failed to select keypair");
let selected_keypair = let selected_keypair = get_selected_keypair().expect("Failed to get selected keypair after select");
get_selected_keypair().expect("Failed to get selected keypair after select");
assert_eq!(selected_keypair.name, "test_keypair"); assert_eq!(selected_keypair.name, "test_keypair");
} }

View File

@ -1,4 +1,5 @@
use crate::vault::kvs::store::{create_store, delete_store, open_store}; use crate::vault::kvs::store::{create_store, delete_store, open_store, KvStore};
use std::path::PathBuf;
// Helper function to generate a unique store name for each test // Helper function to generate a unique store name for each test
fn generate_test_store_name() -> String { fn generate_test_store_name() -> String {