add replay detection to packet processing

This commit is contained in:
Simon Wicky
2024-02-14 14:32:52 +01:00
parent 6dddeaff1d
commit 00aa88b578
@@ -3,6 +3,7 @@
use crate::measure;
use crate::packet_processor::error::MixProcessingError;
use crate::packet_processor::replay_detection::ReplayDetector;
use log::*;
use nym_sphinx_acknowledgements::surb_ack::SurbAck;
use nym_sphinx_addressing::nodes::NymNodeRoutingAddress;
@@ -40,6 +41,9 @@ pub enum MixProcessingResult {
pub struct SphinxPacketProcessor {
/// Private sphinx key of this node required to unwrap received sphinx packet.
sphinx_key: Arc<PrivateKey>,
/// Detector of replay attack
replay_detector: ReplayDetector,
}
impl SphinxPacketProcessor {
@@ -47,6 +51,7 @@ impl SphinxPacketProcessor {
pub fn new(sphinx_key: PrivateKey) -> Self {
SphinxPacketProcessor {
sphinx_key: Arc::new(sphinx_key),
replay_detector: ReplayDetector::new(),
}
}
@@ -184,7 +189,7 @@ impl SphinxPacketProcessor {
match packet {
NymProcessedPacket::Sphinx(packet) => {
match packet {
ProcessedPacket::ForwardHop(packet, address, delay) => self
ProcessedPacket::ForwardHop(packet, address, delay, _) => self
.process_forward_hop(
NymPacket::Sphinx(*packet),
address,
@@ -193,12 +198,13 @@ impl SphinxPacketProcessor {
),
// right now there's no use for the surb_id included in the header - probably it should get removed from the
// sphinx all together?
ProcessedPacket::FinalHop(destination, _, payload) => self.process_final_hop(
destination,
payload.recover_plaintext()?,
packet_size,
packet_type,
),
ProcessedPacket::FinalHop(destination, _, payload, _) => self
.process_final_hop(
destination,
payload.recover_plaintext()?,
packet_size,
packet_type,
),
}
}
NymProcessedPacket::Outfox(packet) => {
@@ -239,6 +245,10 @@ impl SphinxPacketProcessor {
// unwrap the sphinx packet and if possible and appropriate, cache keys
let processed_packet = self.perform_initial_unwrapping(received)?;
//check for replay attack
self.replay_detector
.handle_replay_tag(&processed_packet.replay_tag())?;
// for forward packets, extract next hop and set delay (but do NOT delay here)
// for final packets, extract SURBAck
let final_processing_result =