From 3b6e34119d8773a2363779d2a7fa39dc693f8376 Mon Sep 17 00:00:00 2001 From: creating2morrow Date: Tue, 9 Apr 2024 23:57:14 -0400 Subject: [PATCH] get the nmpk from lmdb on contact share --- neveko-core/src/contact.rs | 7 +++---- neveko-core/src/neveko25519.rs | 21 ++++++++++++++------ neveko-core/src/utils.rs | 36 ++++++++++++++++++++++++---------- 3 files changed, 44 insertions(+), 20 deletions(-) diff --git a/neveko-core/src/contact.rs b/neveko-core/src/contact.rs index cecfdd8..3371df8 100644 --- a/neveko-core/src/contact.rs +++ b/neveko-core/src/contact.rs @@ -1,10 +1,10 @@ -// Contact repo/service layer +//! contact operations module + use crate::{ db, i2p, models::*, monero, - neveko25519, reqres, utils, }; @@ -128,8 +128,7 @@ pub async fn share() -> Contact { monero::open_wallet(&wallet_name, &wallet_password).await; let m_address: reqres::XmrRpcAddressResponse = monero::get_address().await; monero::close_wallet(&wallet_name, &wallet_password).await; - let nmk = neveko25519::generate_neveko_message_keys().await; - let nmpk = nmk.hex_nmpk; + let nmpk = utils::get_nmpk(); let i2p_address = i2p::get_destination(None); let xmr_address = m_address.result.address; Contact { diff --git a/neveko-core/src/neveko25519.rs b/neveko-core/src/neveko25519.rs index acdd8cb..525fb77 100644 --- a/neveko-core/src/neveko25519.rs +++ b/neveko-core/src/neveko25519.rs @@ -1,3 +1,5 @@ +//! NEVEKO modified ed25519 library extending curve25519-dalek + use curve25519_dalek::{ edwards::{ CompressedEdwardsY, @@ -69,7 +71,14 @@ fn hash_to_scalar(s: Vec<&str>) -> Scalar { hasher.update(&result); let hash = hasher.finalize().to_owned(); let mut hash_container: [u8; 32] = [0u8; 32]; - hex::decode_to_slice(result, &mut hash_container as &mut [u8]).unwrap_or_default(); + let mut index = 0; + for byte in result.as_bytes() { + if index == hash_container.len() - 1 { + break; + } + hash_container[index] = *byte; + index += 1; + } let hash_value = BigInt::from_bytes_le(Sign::Plus, &hash_container); if hash_value < curve_l_as_big_int() { return Scalar::from_bytes_mod_order(hash_container); @@ -98,6 +107,7 @@ pub async fn generate_neveko_message_keys() -> NevekoMessageKeys { monero::close_wallet(&filename, &password).await; let svk = svk_res.result.key; let scalar_nmsk = hash_to_scalar(vec![&svk[..], crate::APP_NAME]); + log::debug!("scalar_nmsk: {:?}", &scalar_nmsk); let point_nmpk = EdwardsPoint::mul_base(&scalar_nmsk); let nmsk = *scalar_nmsk.as_bytes(); let nmpk: [u8; 32] = *point_nmpk.compress().as_bytes(); @@ -173,13 +183,12 @@ mod tests { hex_nmpk, hex_nmsk, }; - // shared secret = pvk * svk - let scalar_svk = Scalar::from_bytes_mod_order(keys.nmsk); + // shared secret = nmpk * nmks + let scalar_nmsk = Scalar::from_bytes_mod_order(keys.nmsk); let compress_y = CompressedEdwardsY::from_slice(&nmpk).unwrap_or_default(); - let pvk = compress_y.decompress().unwrap_or_default(); - let shared_secret = pvk * scalar_svk; + let nmpk_compress = compress_y.decompress().unwrap_or_default(); + let shared_secret = nmpk_compress * scalar_nmsk; let ss_hex = hex::encode(shared_secret.compress().as_bytes()); - log::debug!("shared_secret: {:?}", ss_hex); // x = m + h or x = m - h' let h = hash_to_scalar(vec![&ss_hex[..]]); let h_bi = BigInt::from_bytes_le(Sign::Plus, h.as_bytes()); diff --git a/neveko-core/src/utils.rs b/neveko-core/src/utils.rs index 36c43cc..0b36167 100644 --- a/neveko-core/src/utils.rs +++ b/neveko-core/src/utils.rs @@ -1,14 +1,7 @@ +//! Generic functions for startup and convenience + use crate::{ - args, - contact, - db, - dispute, - i2p, - message, - models, - monero, - reqres, - utils, + args, contact, db, dispute, i2p, message, models, monero, neveko25519, reqres, utils }; use clap::Parser; use log::{ @@ -460,6 +453,28 @@ pub fn get_jwp_secret_key() -> String { r } +/// Returns the hex encoded neveko message public key from LMDB +pub fn get_nmpk() -> String { + let s = db::Interface::open(); + let r = db::Interface::read(&s.env, &s.handle, crate::NEVEKO_NMPK); + if r == utils::empty_string() { + error!("neveko message public key not found"); + return Default::default(); + } + r +} + +async fn generate_nmpk() { + info!("generating neveko message public key"); + let nmpk: String = get_nmpk(); + // send to db + let s = db::Interface::open(); + if nmpk == utils::empty_string() { + let nmk: neveko25519::NevekoMessageKeys = neveko25519::generate_neveko_message_keys().await; + db::Interface::write(&s.env, &s.handle, crate::NEVEKO_NMPK, &nmk.hex_nmpk); + } +} + /// Put all app pre-checks here pub async fn start_up() { info!("neveko is starting up"); @@ -492,6 +507,7 @@ pub async fn start_up() { wallet_password = read_password().unwrap(); std::env::set_var(crate::MONERO_WALLET_PASSWORD, &wallet_password); } + generate_nmpk().await; let env: String = get_release_env().value(); if !args.i2p_advanced { i2p::start().await;