From f7987dd6beb1b400bcdb0dbc42ce4e46fa82a489 Mon Sep 17 00:00:00 2001 From: Samuel Onoja Date: Sat, 14 Sep 2024 17:30:20 +0100 Subject: [PATCH] minor changes + merge session and session_key mod --- mm2src/kdf_walletconnect/src/error.rs | 9 ++- mm2src/kdf_walletconnect/src/lib.rs | 1 - mm2src/kdf_walletconnect/src/session.rs | 73 ++++++++++++++++++--- mm2src/kdf_walletconnect/src/session_key.rs | 68 ------------------- 4 files changed, 71 insertions(+), 80 deletions(-) delete mode 100644 mm2src/kdf_walletconnect/src/session_key.rs diff --git a/mm2src/kdf_walletconnect/src/error.rs b/mm2src/kdf_walletconnect/src/error.rs index 7e7679e504..c11fd3cc38 100644 --- a/mm2src/kdf_walletconnect/src/error.rs +++ b/mm2src/kdf_walletconnect/src/error.rs @@ -4,8 +4,6 @@ use relay_client::error::{ClientError, Error}; use relay_rpc::rpc::{PublishError, SubscriptionError}; use serde::{Deserialize, Serialize}; -use crate::session_key::SessionError; - #[derive(Debug, Display, Serialize, Deserialize)] pub enum WalletConnectCtxError { PairingError(String), @@ -43,3 +41,10 @@ impl From for WalletConnectCtxError { impl From for WalletConnectCtxError { fn from(value: SessionError) -> Self { WalletConnectCtxError::SessionError(value.to_string()) } } + +/// Session key and topic derivation errors. +#[derive(Debug, Clone, thiserror::Error)] +pub enum SessionError { + #[error("Failed to generate symmetric session key: {0}")] + SymKeyGeneration(String), +} diff --git a/mm2src/kdf_walletconnect/src/lib.rs b/mm2src/kdf_walletconnect/src/lib.rs index 1a3fd9cd60..f6884aa81d 100644 --- a/mm2src/kdf_walletconnect/src/lib.rs +++ b/mm2src/kdf_walletconnect/src/lib.rs @@ -4,7 +4,6 @@ mod inbound_message; mod metadata; #[allow(unused)] mod pairing; #[allow(unused)] mod session; -mod session_key; use common::{executor::Timer, log::info}; use error::WalletConnectCtxError; diff --git a/mm2src/kdf_walletconnect/src/session.rs b/mm2src/kdf_walletconnect/src/session.rs index fdaedc353e..fec1394457 100644 --- a/mm2src/kdf_walletconnect/src/session.rs +++ b/mm2src/kdf_walletconnect/src/session.rs @@ -1,10 +1,10 @@ -use crate::{error::WalletConnectCtxError, session_key::SessionKey, WalletConnectCtx, SUPPORTED_ACCOUNTS, - SUPPORTED_CHAINS, SUPPORTED_EVENTS, SUPPORTED_METHODS, SUPPORTED_PROTOCOL}; +use crate::error::SessionError; +use crate::{error::WalletConnectCtxError, WalletConnectCtx, SUPPORTED_ACCOUNTS, SUPPORTED_CHAINS, SUPPORTED_EVENTS, + SUPPORTED_METHODS, SUPPORTED_PROTOCOL}; use chrono::Utc; use common::log::info; use futures::lock::Mutex; use mm2_err_handle::prelude::{MapToMmResult, MmResult}; -use rand::rngs::OsRng; use relay_rpc::auth::ed25519_dalek::SigningKey; use relay_rpc::rpc::params::session_delete::SessionDeleteRequest; use relay_rpc::rpc::params::session_extend::SessionExtendRequest; @@ -20,12 +20,73 @@ use serde_json::Value; use std::collections::HashMap; use std::ops::Deref; use std::{collections::BTreeMap, sync::Arc}; +use {hkdf::Hkdf, + rand::{rngs::OsRng, CryptoRng, RngCore}, + sha2::{Digest, Sha256}, + std::fmt::Debug, + x25519_dalek::{EphemeralSecret, PublicKey}}; const FIVE_MINUTES: u64 = 300; pub(crate) const THIRTY_DAYS: u64 = 60 * 60 * 30; pub(crate) type WcRequestResponseResult = MmResult<(Value, IrnMetadata), WalletConnectCtxError>; +#[derive(Clone)] +pub struct SessionKey { + sym_key: [u8; 32], + public_key: PublicKey, +} + +impl std::fmt::Debug for SessionKey { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("SessionKey") + .field("sym_key", &"*******") + .field("public_key", &self.public_key) + .finish() + } +} + +impl SessionKey { + /// Creates new session key from `osrng`. + pub fn from_osrng(sender_public_key: &[u8; 32]) -> Result { + SessionKey::diffie_hellman(OsRng, sender_public_key) + } + + /// Performs Diffie-Hellman symmetric key derivation. + pub fn diffie_hellman(csprng: T, sender_public_key: &[u8; 32]) -> Result + where + T: RngCore + CryptoRng, + { + let single_use_private_key = EphemeralSecret::random_from_rng(csprng); + let public_key = PublicKey::from(&single_use_private_key); + + let ikm = single_use_private_key.diffie_hellman(&PublicKey::from(*sender_public_key)); + + let mut session_sym_key = Self { + sym_key: [0u8; 32], + public_key, + }; + let hk = Hkdf::::new(None, ikm.as_bytes()); + hk.expand(&[], &mut session_sym_key.sym_key) + .map_err(|e| SessionError::SymKeyGeneration(e.to_string()))?; + + Ok(session_sym_key) + } + + /// Gets symmetic key reference. + pub fn symmetric_key(&self) -> &[u8; 32] { &self.sym_key } + + /// Gets "our" public key used in symmetric key derivation. + pub fn diffie_public_key(&self) -> &[u8; 32] { self.public_key.as_bytes() } + + /// Generates new session topic. + pub fn generate_topic(&self) -> String { + let mut hasher = Sha256::new(); + hasher.update(self.sym_key); + hex::encode(hasher.finalize()) + } +} + #[derive(Debug, Clone)] pub enum SessionType { Controller, @@ -152,12 +213,6 @@ impl Session { } } - pub fn from_session_info(topic: Topic, session_info: SessionInfo) -> Self { - Self { - session: Arc::new(Mutex::new(HashMap::from([(topic, session_info)]))), - } - } - pub(crate) async fn create_proposal_session( ctx: &WalletConnectCtx, topic: Topic, diff --git a/mm2src/kdf_walletconnect/src/session_key.rs b/mm2src/kdf_walletconnect/src/session_key.rs deleted file mode 100644 index b1b1f7a89f..0000000000 --- a/mm2src/kdf_walletconnect/src/session_key.rs +++ /dev/null @@ -1,68 +0,0 @@ -use {hkdf::Hkdf, - rand::{rngs::OsRng, CryptoRng, RngCore}, - sha2::{Digest, Sha256}, - std::fmt::Debug, - x25519_dalek::{EphemeralSecret, PublicKey}}; - -/// Session key and topic derivation errors. -#[derive(Debug, Clone, thiserror::Error)] -pub enum SessionError { - #[error("Failed to generate symmetric session key: {0}")] - SymKeyGeneration(String), -} - -#[derive(Clone)] -pub struct SessionKey { - sym_key: [u8; 32], - public_key: PublicKey, -} - -impl std::fmt::Debug for SessionKey { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - f.debug_struct("SessionKey") - .field("sym_key", &"*******") - .field("public_key", &self.public_key) - .finish() - } -} - -impl SessionKey { - /// Creates new session key from `osrng`. - pub fn from_osrng(sender_public_key: &[u8; 32]) -> Result { - SessionKey::diffie_hellman(OsRng, sender_public_key) - } - - /// Performs Diffie-Hellman symmetric key derivation. - pub fn diffie_hellman(csprng: T, sender_public_key: &[u8; 32]) -> Result - where - T: RngCore + CryptoRng, - { - let single_use_private_key = EphemeralSecret::random_from_rng(csprng); - let public_key = PublicKey::from(&single_use_private_key); - - let ikm = single_use_private_key.diffie_hellman(&PublicKey::from(*sender_public_key)); - - let mut session_sym_key = Self { - sym_key: [0u8; 32], - public_key, - }; - let hk = Hkdf::::new(None, ikm.as_bytes()); - hk.expand(&[], &mut session_sym_key.sym_key) - .map_err(|e| SessionError::SymKeyGeneration(e.to_string()))?; - - Ok(session_sym_key) - } - - /// Gets symmetic key reference. - pub fn symmetric_key(&self) -> &[u8; 32] { &self.sym_key } - - /// Gets "our" public key used in symmetric key derivation. - pub fn diffie_public_key(&self) -> &[u8; 32] { self.public_key.as_bytes() } - - /// Generates new session topic. - pub fn generate_topic(&self) -> String { - let mut hasher = Sha256::new(); - hasher.update(self.sym_key); - hex::encode(hasher.finalize()) - } -}