fix refactor of remote_peer key being montgomery

pull/19/head
Niko PLP 1 year ago
parent 240b836624
commit b44838a1bb
  1. 6
      ng-sdk-js/src/lib.rs
  2. 12
      ng-wallet/src/types.rs
  3. 41
      ngd/src/main.rs
  4. 5
      p2p-broker/src/server_ws.rs
  5. 10
      p2p-client-ws/src/remote_ws.rs
  6. 4
      p2p-client-ws/src/remote_ws_wasm.rs
  7. 42
      p2p-net/src/broker.rs
  8. 19
      p2p-net/src/connection.rs
  9. 14
      p2p-repo/src/types.rs
  10. 13
      p2p-repo/src/utils.rs

@ -265,20 +265,20 @@ pub async fn start() {
//res.expect_throw("assume the connection succeeds"); //res.expect_throw("assume the connection succeeds");
async fn timer_close(remote_peer_id: DirectPeerId) -> ResultSend<()> { async fn timer_close(remote_peer_id: DirectPeerId, user: Option<PubKey>) -> ResultSend<()> {
async move { async move {
sleep!(std::time::Duration::from_secs(3)); sleep!(std::time::Duration::from_secs(3));
log_info!("timeout"); log_info!("timeout");
BROKER BROKER
.write() .write()
.await .await
.close_peer_connection(&remote_peer_id) .close_peer_connection(&remote_peer_id, user)
.await; .await;
} }
.await; .await;
Ok(()) Ok(())
} }
spawn_and_log_error(timer_close(server_key)); spawn_and_log_error(timer_close(server_key, Some(user)));
//Broker::graceful_shutdown().await; //Broker::graceful_shutdown().await;

@ -151,7 +151,17 @@ pub struct WalletContentV0 {
/// Wallet Log /// Wallet Log
#[derive(Clone, Debug, Serialize, Deserialize)] #[derive(Clone, Debug, Serialize, Deserialize)]
pub struct WalletLog0 { pub struct WalletLog0 {
pub log: Vec<(SystemTime, WalletOperationV0)>, pub log: Vec<(u128, WalletOperationV0)>,
}
impl WalletLog0 {
pub fn add(&mut self, op: WalletOperationV0) {
let duration = SystemTime::now()
.duration_since(SystemTime::UNIX_EPOCH)
.unwrap()
.as_nanos();
self.log.push((duration, op));
}
} }
/// WalletOperation /// WalletOperation

@ -28,6 +28,8 @@ use p2p_net::utils::{
}; };
use p2p_net::{WS_PORT, WS_PORT_REVERSE_PROXY}; use p2p_net::{WS_PORT, WS_PORT_REVERSE_PROXY};
use p2p_repo::log::*; use p2p_repo::log::*;
use p2p_repo::types::SymKey;
use p2p_repo::utils::ed_keypair_from_priv_bytes;
use p2p_repo::{ use p2p_repo::{
types::{PrivKey, PubKey}, types::{PrivKey, PubKey},
utils::{decode_key, generate_keypair, sign, verify}, utils::{decode_key, generate_keypair, sign, verify},
@ -285,7 +287,10 @@ fn parse_domain_and_port(
Ok((parts[0].to_string(), domain_with_port, port)) Ok((parts[0].to_string(), domain_with_port, port))
} }
fn prepare_accept_forward_for_domain(domain: String, args: &Cli) -> Result<AcceptForwardForV0, ()> { fn prepare_accept_forward_for_domain(
domain: String,
args: &mut Cli,
) -> Result<AcceptForwardForV0, ()> {
if args.domain_peer.is_some() { if args.domain_peer.is_some() {
let key = decode_key(args.domain_peer.as_ref().unwrap().as_str())?; let key = decode_key(args.domain_peer.as_ref().unwrap().as_str())?;
args.domain_peer.as_mut().unwrap().zeroize(); args.domain_peer.as_mut().unwrap().zeroize();
@ -308,7 +313,7 @@ async fn main() -> std::io::Result<()> {
} }
async fn main_inner() -> Result<(), ()> { async fn main_inner() -> Result<(), ()> {
let args = Cli::parse(); let mut args = Cli::parse();
if args.list_interfaces { if args.list_interfaces {
println!("list of network interfaces"); println!("list of network interfaces");
@ -348,10 +353,10 @@ async fn main_inner() -> Result<(), ()> {
key_path.push("key"); key_path.push("key");
let key_from_file: Option<[u8; 32]>; let key_from_file: Option<[u8; 32]>;
let res = |key_path| -> Result<[u8; 32], &str> { let res = |key_path| -> Result<[u8; 32], &str> {
let file = read_to_string(key_path).map_err(|_| "")?; let mut file = read_to_string(key_path).map_err(|_| "")?;
let first_line = file.lines().nth(0).ok_or("empty file")?; let first_line = file.lines().nth(0).ok_or("empty file")?;
let res = decode_key(first_line.trim()).map_err(|_| "invalid file"); let res = decode_key(first_line.trim()).map_err(|_| "invalid file");
first_line.zeroize(); file.zeroize();
res res
}(&key_path); }(&key_path);
@ -364,34 +369,35 @@ async fn main_inner() -> Result<(), ()> {
} }
key_from_file = res.ok(); key_from_file = res.ok();
let keys: [[u8; 32]; 4] = match &args.key { let mut keys: [[u8; 32]; 4] = match &args.key {
Some(key_string) => { Some(key_string) => {
if key_from_file.is_some() { if key_from_file.is_some() {
log_err!("provided --key option will not be used as a key file is already present"); log_err!("provided --key option will not be used as a key file is already present");
args.key.as_mut().unwrap().zeroize();
gen_broker_keys(key_from_file) gen_broker_keys(key_from_file)
} else { } else {
let res = decode_key(key_string.as_str()) let res = decode_key(key_string.as_str())
.map_err(|_| log_err!("provided key is invalid. cannot start"))?; .map_err(|_| log_err!("provided key is invalid. cannot start"))?;
if args.save_key { if args.save_key {
let master_key = base64_url::encode(&res); let mut master_key = base64_url::encode(&res);
write(key_path.clone(), master_key).map_err(|e| { write(key_path.clone(), &master_key).map_err(|e| {
log_err!("cannot save key to file. {}.cannot start", e.to_string()) log_err!("cannot save key to file. {}.cannot start", e.to_string())
})?; })?;
master_key.zeroize(); master_key.zeroize();
log_info!("The key has been saved to {}", key_path.to_str().unwrap()); log_info!("The key has been saved to {}", key_path.to_str().unwrap());
} }
args.key.as_mut().unwrap().zeroize();
gen_broker_keys(Some(res)) gen_broker_keys(Some(res))
} }
args.key.as_mut().unwrap().zeroize();
} }
None => { None => {
if key_from_file.is_some() { if key_from_file.is_some() {
gen_broker_keys(key_from_file) gen_broker_keys(key_from_file)
} else { } else {
let res = gen_broker_keys(None); let res = gen_broker_keys(None);
let master_key = base64_url::encode(&res[0]); let mut master_key = base64_url::encode(&res[0]);
if args.save_key { if args.save_key {
write(key_path.clone(), master_key).map_err(|e| { write(key_path.clone(), &master_key).map_err(|e| {
log_err!("cannot save key to file. {}.cannot start", e.to_string()) log_err!("cannot save key to file. {}.cannot start", e.to_string())
})?; })?;
log_info!("The key has been saved to {}", key_path.to_str().unwrap()); log_info!("The key has been saved to {}", key_path.to_str().unwrap());
@ -409,7 +415,7 @@ async fn main_inner() -> Result<(), ()> {
key_from_file.and_then(|mut key| { key_from_file.and_then(|mut key| {
key.zeroize(); key.zeroize();
None None::<()>
}); });
// DEALING WITH CONFIG // DEALING WITH CONFIG
@ -500,7 +506,7 @@ async fn main_inner() -> Result<(), ()> {
overlays_config.server = BrokerOverlayPermission::AllRegisteredUser; overlays_config.server = BrokerOverlayPermission::AllRegisteredUser;
let mut listener = ListenerV0::new_direct(loopback, !args.no_ipv6, local_port); let mut listener = ListenerV0::new_direct(loopback, !args.no_ipv6, local_port);
listener.accept_direct = false; listener.accept_direct = false;
let res = prepare_accept_forward_for_domain(domain, &args).map_err(|_| { let res = prepare_accept_forward_for_domain(domain, &mut args).map_err(|_| {
log_err!("The --domain-peer option has an invalid key. it must be a base64_url encoded serialization of a PrivKey. cannot start") log_err!("The --domain-peer option has an invalid key. it must be a base64_url encoded serialization of a PrivKey. cannot start")
})?; })?;
listener.accept_forward_for = res; listener.accept_forward_for = res;
@ -770,7 +776,7 @@ async fn main_inner() -> Result<(), ()> {
Some(inter) => { Some(inter) => {
overlays_config.server = BrokerOverlayPermission::AllRegisteredUser; overlays_config.server = BrokerOverlayPermission::AllRegisteredUser;
let res = prepare_accept_forward_for_domain(domain, &args).map_err(|_| { let res = prepare_accept_forward_for_domain(domain, &mut args).map_err(|_| {
log_err!("The --domain-peer option has an invalid key. it must be a base64_url encoded serialization of a PrivKey. cannot start")})?; log_err!("The --domain-peer option has an invalid key. it must be a base64_url encoded serialization of a PrivKey. cannot start")})?;
if listeners.last().is_some() if listeners.last().is_some()
@ -957,14 +963,7 @@ async fn main_inner() -> Result<(), ()> {
match config.unwrap() { match config.unwrap() {
DaemonConfig::V0(v0) => { DaemonConfig::V0(v0) => {
run_server_v0( run_server_v0(privkey, pubkey, SymKey::from_array(keys[2]), v0, path).await?
privkey,
pubkey,
Sensitive::<[u8; 32]>::from_slice(&keys[2]),
v0,
path,
)
.await?
} }
} }

@ -35,10 +35,11 @@ use p2p_client_ws::remote_ws::ConnectionWebSocket;
use p2p_net::broker::*; use p2p_net::broker::*;
use p2p_net::connection::IAccept; use p2p_net::connection::IAccept;
use p2p_net::types::*; use p2p_net::types::*;
use p2p_net::utils::get_domain_without_port;
use p2p_net::utils::is_private_ip; use p2p_net::utils::is_private_ip;
use p2p_net::utils::is_public_ip; use p2p_net::utils::is_public_ip;
use p2p_net::utils::{get_domain_without_port, Sensitive, U8Array};
use p2p_repo::log::*; use p2p_repo::log::*;
use p2p_repo::types::SymKey;
use p2p_repo::types::{PrivKey, PubKey}; use p2p_repo::types::{PrivKey, PubKey};
use p2p_repo::utils::generate_keypair; use p2p_repo::utils::generate_keypair;
use rust_embed::RustEmbed; use rust_embed::RustEmbed;
@ -582,7 +583,7 @@ pub async fn run_server_accept_one(
pub async fn run_server_v0( pub async fn run_server_v0(
peer_priv_key: PrivKey, peer_priv_key: PrivKey,
peer_id: PubKey, peer_id: PubKey,
wallet_master_key: Sensitive<[u8; 32]>, wallet_master_key: SymKey,
config: DaemonConfigV0, config: DaemonConfigV0,
mut path: PathBuf, mut path: PathBuf,
) -> Result<(), ()> { ) -> Result<(), ()> {

@ -27,7 +27,7 @@ use futures::{FutureExt, SinkExt};
use async_std::task; use async_std::task;
use p2p_net::errors::*; use p2p_net::errors::*;
use p2p_net::types::*; use p2p_net::types::*;
use p2p_net::utils::{spawn_and_log_error, Receiver, ResultSend, Sender, Sensitive}; use p2p_net::utils::{spawn_and_log_error, Receiver, ResultSend, Sender};
use p2p_net::{connection::*, WS_PORT}; use p2p_net::{connection::*, WS_PORT};
use p2p_repo::log::*; use p2p_repo::log::*;
use p2p_repo::types::*; use p2p_repo::types::*;
@ -44,7 +44,7 @@ impl IConnect for ConnectionWebSocket {
async fn open( async fn open(
&self, &self,
url: String, url: String,
peer_privk: Sensitive<[u8; 32]>, peer_privk: PrivKey,
peer_pubk: PubKey, peer_pubk: PubKey,
remote_peer: DirectPeerId, remote_peer: DirectPeerId,
config: StartConfig, config: StartConfig,
@ -346,20 +346,20 @@ mod test {
BROKER.read().await.print_status(); BROKER.read().await.print_status();
async fn timer_close(remote_peer_id: DirectPeerId) -> ResultSend<()> { async fn timer_close(remote_peer_id: DirectPeerId, user: Option<PubKey>) -> ResultSend<()> {
async move { async move {
sleep!(std::time::Duration::from_secs(3)); sleep!(std::time::Duration::from_secs(3));
log_info!("timeout"); log_info!("timeout");
BROKER BROKER
.write() .write()
.await .await
.close_peer_connection(&remote_peer_id) .close_peer_connection(&remote_peer_id, user)
.await; .await;
} }
.await; .await;
Ok(()) Ok(())
} }
spawn_and_log_error(timer_close(server_key)); spawn_and_log_error(timer_close(server_key, Some(user)));
//Broker::graceful_shutdown().await; //Broker::graceful_shutdown().await;

@ -38,7 +38,7 @@ impl IConnect for ConnectionWebSocket {
async fn open( async fn open(
&self, &self,
url: String, url: String,
peer_privk: Sensitive<[u8; 32]>, peer_privk: PrivKey,
peer_pubk: PubKey, peer_pubk: PubKey,
remote_peer: DirectPeerId, remote_peer: DirectPeerId,
config: StartConfig, config: StartConfig,
@ -94,7 +94,7 @@ async fn ws_loop(
mut stream: WsStream, mut stream: WsStream,
sender: Receiver<ConnectionCommand>, sender: Receiver<ConnectionCommand>,
mut receiver: Sender<ConnectionCommand>, mut receiver: Sender<ConnectionCommand>,
mut shutdown: Sender<Either<NetError, PubKey>>, mut shutdown: Sender<Either<NetError, X25519PrivKey>>,
) -> ResultSend<()> { ) -> ResultSend<()> {
async fn inner_loop( async fn inner_loop(
stream: &mut WsStream, stream: &mut WsStream,

@ -53,7 +53,7 @@ pub struct BrokerPeerInfo {
pub struct DirectConnection { pub struct DirectConnection {
ip: IP, ip: IP,
interface: String, interface: String,
remote_peer_id: DirectPeerId, remote_peer_id: X25519PrivKey,
tp: TransportProtocol, tp: TransportProtocol,
//dir: ConnectionDir, //dir: ConnectionDir,
cnx: ConnectionBase, cnx: ConnectionBase,
@ -249,8 +249,8 @@ impl Broker {
(rx, tx.clone()) (rx, tx.clone())
} }
pub fn reconnecting(&mut self, peer_id: &DirectPeerId, user: Option<PubKey>) { pub fn reconnecting(&mut self, peer_id: X25519PrivKey, user: Option<PubKey>) {
let peerinfo = self.peers.get_mut(&(user, peer_id.to_dh_slice())); let peerinfo = self.peers.get_mut(&(user, peer_id));
match peerinfo { match peerinfo {
Some(info) => match &info.connected { Some(info) => match &info.connected {
PeerConnection::NONE => {} PeerConnection::NONE => {}
@ -265,8 +265,8 @@ impl Broker {
None => {} None => {}
} }
} }
pub fn remove_peer_id(&mut self, peer_id: &DirectPeerId, user: Option<PubKey>) { pub fn remove_peer_id(&mut self, peer_id: X25519PrivKey, user: Option<PubKey>) {
let removed = self.peers.remove(&(user, peer_id.to_dh_slice())); let removed = self.peers.remove(&(user, peer_id));
match removed { match removed {
Some(info) => match info.connected { Some(info) => match info.connected {
PeerConnection::NONE => {} PeerConnection::NONE => {}
@ -404,7 +404,8 @@ impl Broker {
return Err(NetError::Closing); return Err(NetError::Closing);
} }
let join: mpsc::UnboundedReceiver<Either<NetError, PubKey>> = connection.take_shutdown(); let join: mpsc::UnboundedReceiver<Either<NetError, X25519PrivKey>> =
connection.take_shutdown();
if self if self
.anonymous_connections .anonymous_connections
.insert((local_bind_address, remote_bind_address), connection) .insert((local_bind_address, remote_bind_address), connection)
@ -418,7 +419,7 @@ impl Broker {
} }
async fn watch_close( async fn watch_close(
mut join: Receiver<Either<NetError, PubKey>>, mut join: Receiver<Either<NetError, X25519PrivKey>>,
remote_bind_address: BindAddress, remote_bind_address: BindAddress,
local_bind_address: BindAddress, local_bind_address: BindAddress,
) -> ResultSend<()> { ) -> ResultSend<()> {
@ -428,7 +429,7 @@ impl Broker {
Some(Either::Right(remote_peer_id)) => { Some(Either::Right(remote_peer_id)) => {
let res = join.next().await; let res = join.next().await;
log_info!("SOCKET IS CLOSED {:?} peer_id: {:?}", res, remote_peer_id); log_info!("SOCKET IS CLOSED {:?} peer_id: {:?}", res, remote_peer_id);
BROKER.write().await.remove_peer_id(&remote_peer_id, None); BROKER.write().await.remove_peer_id(remote_peer_id, None);
} }
_ => { _ => {
log_info!( log_info!(
@ -456,10 +457,10 @@ impl Broker {
&mut self, &mut self,
remote_bind_address: BindAddress, remote_bind_address: BindAddress,
local_bind_address: BindAddress, local_bind_address: BindAddress,
remote_peer_id: PubKey, remote_peer_id: X25519PrivKey,
core: Option<String>, core: Option<String>,
) -> Result<(), NetError> { ) -> Result<(), NetError> {
log_debug!("ATTACH PEER_ID {}", remote_peer_id); log_debug!("ATTACH PEER_ID {:?}", remote_peer_id);
let mut connection = self let mut connection = self
.anonymous_connections .anonymous_connections
.remove(&(local_bind_address, remote_bind_address)) .remove(&(local_bind_address, remote_bind_address))
@ -484,7 +485,7 @@ impl Broker {
lastPeerAdvert: None, lastPeerAdvert: None,
connected, connected,
}; };
self.peers.insert((None, remote_peer_id.to_dh_slice()), bpi); self.peers.insert((None, remote_peer_id), bpi);
Ok(()) Ok(())
} }
@ -528,14 +529,14 @@ impl Broker {
.await?; .await?;
let join = connection.take_shutdown(); let join = connection.take_shutdown();
let remote_peer_id_dh = remote_peer_id.to_dh_slice();
let connected = match &config { let connected = match &config {
StartConfig::Core(config) => { StartConfig::Core(config) => {
let ip = config.addr.ip.clone(); let ip = config.addr.ip.clone();
let dc = DirectConnection { let dc = DirectConnection {
ip, ip,
interface: config.interface.clone(), interface: config.interface.clone(),
remote_peer_id, remote_peer_id: remote_peer_id_dh,
tp: connection.transport_protocol(), tp: connection.transport_protocol(),
cnx: connection, cnx: connection,
}; };
@ -550,27 +551,28 @@ impl Broker {
lastPeerAdvert: None, lastPeerAdvert: None,
connected, connected,
}; };
self.peers self.peers
.insert((config.get_user(), remote_peer_id.to_dh_slice()), bpi); .insert((config.get_user(), remote_peer_id_dh), bpi);
async fn watch_close( async fn watch_close(
mut join: Receiver<Either<NetError, PubKey>>, mut join: Receiver<Either<NetError, X25519PrivKey>>,
cnx: Box<dyn IConnect>, cnx: Box<dyn IConnect>,
peer_privk: PrivKey, peer_privk: PrivKey,
peer_pubkey: PubKey, peer_pubkey: PubKey,
remote_peer_id: DirectPeerId, remote_peer_id: [u8; 32],
config: StartConfig, config: StartConfig,
) -> ResultSend<()> { ) -> ResultSend<()> {
async move { async move {
let res = join.next().await; let res = join.next().await;
log_info!("SOCKET IS CLOSED {:?} {:?}", res, &remote_peer_id); log_info!("SOCKET IS CLOSED {:?} {:?}", res, remote_peer_id);
if res.is_some() if res.is_some()
&& res.as_ref().unwrap().is_left() && res.as_ref().unwrap().is_left()
&& res.unwrap().unwrap_left() != NetError::Closing && res.unwrap().unwrap_left() != NetError::Closing
{ {
// we intend to reconnect // we intend to reconnect
let mut broker = BROKER.write().await; let mut broker = BROKER.write().await;
broker.reconnecting(&remote_peer_id, config.get_user()); broker.reconnecting(remote_peer_id, config.get_user());
// TODO: deal with cycle error https://users.rust-lang.org/t/recursive-async-method-causes-cycle-error/84628/5 // TODO: deal with cycle error https://users.rust-lang.org/t/recursive-async-method-causes-cycle-error/84628/5
// let result = broker // let result = broker
// .connect(cnx, ip, core, peer_pubk, peer_privk, remote_peer_id) // .connect(cnx, ip, core, peer_pubk, peer_privk, remote_peer_id)
@ -582,7 +584,7 @@ impl Broker {
BROKER BROKER
.write() .write()
.await .await
.remove_peer_id(&remote_peer_id, config.get_user()); .remove_peer_id(remote_peer_id, config.get_user());
} }
} }
.await; .await;
@ -593,7 +595,7 @@ impl Broker {
cnx, cnx,
peer_privk, peer_privk,
peer_pubk, peer_pubk,
remote_peer_id, remote_peer_id_dh,
config, config,
)); ));
Ok(()) Ok(())

@ -32,7 +32,7 @@ use noise_protocol::{patterns::noise_xk, CipherState, HandshakeState};
use noise_rust_crypto::sensitive::Sensitive; use noise_rust_crypto::sensitive::Sensitive;
use noise_rust_crypto::*; use noise_rust_crypto::*;
use p2p_repo::log::*; use p2p_repo::log::*;
use p2p_repo::types::{PrivKey, PubKey}; use p2p_repo::types::{PrivKey, PubKey, X25519PrivKey};
use p2p_repo::utils::{sign, verify}; use p2p_repo::utils::{sign, verify};
use serde_bare::from_slice; use serde_bare::from_slice;
use unique_id::sequence::SequenceGenerator; use unique_id::sequence::SequenceGenerator;
@ -537,8 +537,8 @@ impl NoiseFSM {
if !handshake.completed() { if !handshake.completed() {
return Err(ProtocolError::NoiseHandshakeFailed); return Err(ProtocolError::NoiseHandshakeFailed);
} }
let peer_id = PubKey::Ed25519PubKey(handshake.get_rs().unwrap()); let peer_id = handshake.get_rs().unwrap();
self.remote = Some(peer_id); //self.remote = Some(peer_id);
let (local_bind_address, remote_bind_address) = let (local_bind_address, remote_bind_address) =
self.bind_addresses.ok_or(ProtocolError::BrokerError)?; self.bind_addresses.ok_or(ProtocolError::BrokerError)?;
BROKER BROKER
@ -688,8 +688,8 @@ pub struct ConnectionBase {
receiver: Option<Sender<ConnectionCommand>>, receiver: Option<Sender<ConnectionCommand>>,
sender_tx: Option<Sender<ConnectionCommand>>, sender_tx: Option<Sender<ConnectionCommand>>,
receiver_tx: Option<Sender<ConnectionCommand>>, receiver_tx: Option<Sender<ConnectionCommand>>,
shutdown: Option<Receiver<Either<NetError, PubKey>>>, shutdown: Option<Receiver<Either<NetError, X25519PrivKey>>>,
shutdown_sender: Option<Sender<Either<NetError, PubKey>>>, shutdown_sender: Option<Sender<Either<NetError, X25519PrivKey>>>,
dir: ConnectionDir, dir: ConnectionDir,
next_request_id: SequenceGenerator, next_request_id: SequenceGenerator,
tp: TransportProtocol, tp: TransportProtocol,
@ -718,7 +718,7 @@ impl ConnectionBase {
self.tp self.tp
} }
pub fn take_shutdown(&mut self) -> Receiver<Either<NetError, PubKey>> { pub fn take_shutdown(&mut self) -> Receiver<Either<NetError, X25519PrivKey>> {
self.shutdown.take().unwrap() self.shutdown.take().unwrap()
} }
@ -735,7 +735,7 @@ impl ConnectionBase {
} }
// only used by accept // only used by accept
pub async fn reset_shutdown(&mut self, remote_peer_id: PubKey) { pub async fn reset_shutdown(&mut self, remote_peer_id: X25519PrivKey) {
let _ = self let _ = self
.shutdown_sender .shutdown_sender
.take() .take()
@ -744,8 +744,9 @@ impl ConnectionBase {
.await; .await;
} }
pub fn set_shutdown(&mut self) -> Sender<Either<NetError, PubKey>> { pub fn set_shutdown(&mut self) -> Sender<Either<NetError, X25519PrivKey>> {
let (shutdown_sender, shutdown_receiver) = mpsc::unbounded::<Either<NetError, PubKey>>(); let (shutdown_sender, shutdown_receiver) =
mpsc::unbounded::<Either<NetError, X25519PrivKey>>();
self.shutdown = Some(shutdown_receiver); self.shutdown = Some(shutdown_receiver);
self.shutdown_sender = Some(shutdown_sender.clone()); self.shutdown_sender = Some(shutdown_sender.clone());
shutdown_sender shutdown_sender

@ -64,6 +64,9 @@ impl SymKey {
pub fn random() -> Self { pub fn random() -> Self {
SymKey::ChaCha20Key(random_key()) SymKey::ChaCha20Key(random_key())
} }
pub fn from_array(array: [u8; 32]) -> Self {
SymKey::ChaCha20Key(array)
}
} }
/// Curve25519 public key Edwards form /// Curve25519 public key Edwards form
@ -99,11 +102,14 @@ impl PubKey {
), ),
} }
} }
pub fn dh_from_ed_slice(slice: &[u8]) -> PubKey { // pub fn dh_from_ed_slice(slice: &[u8]) -> PubKey {
dh_pubkey_from_ed_pubkey_slice(slice) // dh_pubkey_from_ed_pubkey_slice(slice)
} // }
pub fn to_dh_slice(&self) -> [u8; 32] { pub fn to_dh_slice(&self) -> [u8; 32] {
dh_pubkey_array_from_ed_pubkey_slice(self.slice()) match self {
PubKey::Ed25519PubKey(o) => dh_pubkey_array_from_ed_pubkey_slice(o),
_ => panic!("can only convert an edward key to montgomery"),
}
} }
} }

@ -85,16 +85,15 @@ pub fn dh_pubkey_from_ed_pubkey_slice(public: &[u8]) -> PubKey {
} }
pub fn dh_pubkey_array_from_ed_pubkey_slice(public: &[u8]) -> X25519PubKey { pub fn dh_pubkey_array_from_ed_pubkey_slice(public: &[u8]) -> X25519PubKey {
// the zeroize are not mandatory, because it is a PubKey.
let mut bits: [u8; 32] = [0u8; 32]; let mut bits: [u8; 32] = [0u8; 32];
bits.copy_from_slice(public); bits.copy_from_slice(public);
let mut compressed = CompressedEdwardsY(bits); let compressed = CompressedEdwardsY(bits);
let mut ed_point: EdwardsPoint = compressed.decompress().unwrap(); let ed_point: EdwardsPoint = compressed.decompress().unwrap();
compressed.zeroize(); //compressed.zeroize();
let mut mon_point = ed_point.to_montgomery(); let mon_point = ed_point.to_montgomery();
ed_point.zeroize(); //ed_point.zeroize();
let array = mon_point.to_bytes(); let array = mon_point.to_bytes();
mon_point.zeroize(); //mon_point.zeroize();
array array
} }

Loading…
Cancel
Save