veilid/veilid-core/src/veilid_api/privacy.rs

148 lines
4.0 KiB
Rust
Raw Normal View History

use super::*;
////////////////////////////////////////////////////////////////////////////////////////////////////
// Compiled Privacy Objects
2022-11-14 18:09:33 +00:00
/// An encrypted private/safety route hop
#[derive(Clone, Debug)]
pub struct RouteHopData {
2022-11-14 18:09:33 +00:00
/// The nonce used in the encryption ENC(Xn,DH(PKn,SKapr))
pub nonce: Nonce,
2022-11-14 18:09:33 +00:00
/// The encrypted blob
pub blob: Vec<u8>,
}
2022-11-14 18:09:33 +00:00
/// How to find a route node
2022-10-10 02:07:15 +00:00
#[derive(Clone, Debug)]
pub enum RouteNode {
2022-11-14 18:09:33 +00:00
/// Route node is optimized, no contact method information as this node id has been seen before
2022-10-19 01:53:45 +00:00
NodeId(NodeId),
2022-11-14 18:09:33 +00:00
/// Route node with full contact method information to ensure the peer is reachable
2022-10-10 02:07:15 +00:00
PeerInfo(PeerInfo),
}
impl fmt::Display for RouteNode {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"{}",
match self {
2022-10-19 01:53:45 +00:00
RouteNode::NodeId(x) => x.key.encode(),
2022-10-10 02:07:15 +00:00
RouteNode::PeerInfo(pi) => pi.node_id.key.encode(),
}
)
}
}
2022-11-14 18:09:33 +00:00
/// An unencrypted private/safety route hop
#[derive(Clone, Debug)]
pub struct RouteHop {
2022-11-14 18:09:33 +00:00
/// The location of the hop
2022-10-10 02:07:15 +00:00
pub node: RouteNode,
2022-11-14 18:09:33 +00:00
/// The encrypted blob to pass to the next hop as its data (None for stubs)
2022-10-30 02:15:50 +00:00
pub next_hop: Option<RouteHopData>,
}
2022-11-14 18:09:33 +00:00
/// The kind of hops a private route can have
#[derive(Clone, Debug)]
pub enum PrivateRouteHops {
/// The first hop of a private route, unencrypted, route_hops == total hop count
FirstHop(RouteHop),
/// Private route internal node. Has > 0 private route hops left but < total hop count
Data(RouteHopData),
/// Private route has ended (hop count = 0)
Empty,
}
/// A private route for receiver privacy
#[derive(Clone, Debug)]
pub struct PrivateRoute {
2022-11-14 18:09:33 +00:00
/// The public key used for the entire route
pub public_key: DHTKey,
pub hop_count: u8,
2022-11-14 18:09:33 +00:00
pub hops: PrivateRouteHops,
}
impl PrivateRoute {
2022-10-30 02:15:50 +00:00
/// Empty private route is the form used when receiving the last hop
pub fn new_empty(public_key: DHTKey) -> Self {
Self {
public_key,
hop_count: 0,
2022-11-14 18:09:33 +00:00
hops: PrivateRouteHops::Empty,
}
}
2022-10-30 02:15:50 +00:00
/// Stub route is the form used when no privacy is required, but you need to specify the destination for a safety route
pub fn new_stub(public_key: DHTKey, node: RouteNode) -> Self {
Self {
public_key,
hop_count: 1,
2022-11-14 18:09:33 +00:00
hops: PrivateRouteHops::FirstHop(RouteHop {
2022-10-30 02:15:50 +00:00
node,
next_hop: None,
}),
}
}
}
impl fmt::Display for PrivateRoute {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"PR({:?}+{}{})",
self.public_key,
self.hop_count,
2022-11-14 18:09:33 +00:00
match &self.hops {
PrivateRouteHops::FirstHop(fh) => {
format!("->{}", fh.node)
}
PrivateRouteHops::Data(_) => {
"->?".to_owned()
}
PrivateRouteHops::Empty => {
"".to_owned()
}
}
)
}
}
#[derive(Clone, Debug)]
pub enum SafetyRouteHops {
2022-10-29 02:26:21 +00:00
/// Has >= 1 safety route hops
Data(RouteHopData),
2022-10-29 02:26:21 +00:00
/// Has 0 safety route hops
Private(PrivateRoute),
}
#[derive(Clone, Debug)]
pub struct SafetyRoute {
pub public_key: DHTKey,
pub hop_count: u8,
pub hops: SafetyRouteHops,
}
2022-10-20 19:09:04 +00:00
impl SafetyRoute {
pub fn new_stub(public_key: DHTKey, private_route: PrivateRoute) -> Self {
Self {
public_key,
hop_count: 0,
hops: SafetyRouteHops::Private(private_route),
}
}
}
impl fmt::Display for SafetyRoute {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"SR({:?}+{}{})",
self.public_key,
self.hop_count,
match &self.hops {
SafetyRouteHops::Data(_) => "".to_owned(),
SafetyRouteHops::Private(p) => format!("->{}", p),
}
)
}
}