From 577d80b2826040fb8d5d48b6f1183235c684b1db Mon Sep 17 00:00:00 2001 From: despiegk Date: Tue, 13 May 2025 06:51:20 +0300 Subject: [PATCH] restore --- src/vault/keypair/keypair_types.rs | 35 ++++++++++++++++-------------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/src/vault/keypair/keypair_types.rs b/src/vault/keypair/keypair_types.rs index f7da6fa..cdc5374 100644 --- a/src/vault/keypair/keypair_types.rs +++ b/src/vault/keypair/keypair_types.rs @@ -214,16 +214,18 @@ impl KeyPair { let ephemeral_signing_key = SigningKey::random(&mut OsRng); let ephemeral_public_key = VerifyingKey::from(&ephemeral_signing_key); - // Derive shared secret using ECDH - let shared_secret_bytes = ephemeral_signing_key.diffie_hellman(&recipient_key); + // Derive shared secret (this is a simplified ECDH) + // In a real implementation, we would use proper ECDH, but for this example: + let shared_point = recipient_key.to_encoded_point(false); + let shared_secret = { + let mut hasher = Sha256::default(); + hasher.update(ephemeral_signing_key.to_bytes()); + hasher.update(shared_point.as_bytes()); + hasher.finalize().to_vec() + }; - // Derive encryption key from the shared secret (using a simple hash for this example) - let mut hasher = Sha256::default(); - hasher.update(shared_secret_bytes.as_bytes()); - let encryption_key = hasher.finalize().to_vec(); - // Encrypt the message using the derived key - let ciphertext = implementation::encrypt_with_key(&encryption_key, message) + let ciphertext = implementation::encrypt_with_key(&shared_secret, message) .map_err(|e| CryptoError::EncryptionFailed(e.to_string()))?; // Format: ephemeral_public_key || ciphertext @@ -250,16 +252,17 @@ impl KeyPair { let sender_key = VerifyingKey::from_sec1_bytes(ephemeral_public_key) .map_err(|_| CryptoError::InvalidKeyLength)?; - // Derive shared secret using ECDH - let shared_secret_bytes = self.signing_key.diffie_hellman(&sender_key); - - // Derive encryption key from the shared secret (using the same simple hash) - let mut hasher = Sha256::default(); - hasher.update(shared_secret_bytes.as_bytes()); - let encryption_key = hasher.finalize().to_vec(); + // Derive shared secret (simplified ECDH) + let shared_point = sender_key.to_encoded_point(false); + let shared_secret = { + let mut hasher = Sha256::default(); + hasher.update(self.signing_key.to_bytes()); + hasher.update(shared_point.as_bytes()); + hasher.finalize().to_vec() + }; // Decrypt the message using the derived key - implementation::decrypt_with_key(&encryption_key, actual_ciphertext) + implementation::decrypt_with_key(&shared_secret, actual_ciphertext) .map_err(|e| CryptoError::DecryptionFailed(e.to_string())) } }