feat: add ratchet, streaming, SU3 reseed, and router floodfill
check / check (windows-latest) (push) Failing after 27s
check / check (ubuntu-latest) (push) Failing after 29s
check / check (macos-latest) (push) Failing after 27s

- Add ratchet/ with ECIES-X25519-AEAD-Ratchet (Proposal 144), Garlic cloves, and ECIES DatabaseLookup replies
- Add streaming/ with Ed25519-signed I2P Streaming (SYN/ACK/NACK/CLOSE/RESET, retransmit, window) and Repliable/Raw Datagrams
- Add common/su3.mbt to parse and verify reseed .su3 bundles
- Add Elligator2 encoding in crypto/ and OfflineSignature support in LeaseSet2 (flags bit 0)
- Router: NTCP2 peer pool, floodfill NetDB crawl, transit tunnel relay, and Garlic/Streaming bridge
- cmd/router: add --peer-dir, --su3, --reseed, --floodfill, --no-clock-skew
- cmd/client: add --dest for persistent destination keys
- Mark Phase 6 complete in README and document the transport scope (NTCP2 only, no SSU2)
This commit is contained in:
nandi committed 2026-11-27 22:23:46 -08:00
1 parent 8eafa11b22
commit ae72b00323
54 files changed
+9607 -704

No files matched your search

+3
View File
@@ -10,6 +10,9 @@ pub suberror Ntcp2Error {
InvalidState(String)
InvalidFormat(String)
InvalidLength(String)
InvalidNetworkId(Byte)
ClockSkew(UInt32, UInt32)
ReplayDetected
}
///|
+81
View File
@@ -331,12 +331,89 @@ pub fn Ntcp2Handshake::set_ephemeral(
self.local_ephemeral = Some(ephemeral)
}
///|
/// Validates that `peer_ts` is within `max_skew_sec` of `now_sec`.
pub fn validate_clock_skew(
peer_ts : UInt32,
now_sec : UInt32,
max_skew_sec~ : UInt32 = 60U,
) -> Unit raise Ntcp2Error {
let diff = if peer_ts >= now_sec {
peer_ts - now_sec
} else {
now_sec - peer_ts
}
if diff > max_skew_sec {
raise ClockSkew(peer_ts, now_sec)
}
}
///|
/// Bounded replay cache for incoming NTCP2 ephemeral X keys (`X` de-obfuscated in Msg 1).
pub struct EphemeralReplayCache {
entries : Array[(Bytes, UInt32)]
max_entries : Int32
}
///|
pub fn EphemeralReplayCache::new(
max_entries~ : Int32 = 2048,
) -> EphemeralReplayCache {
{ entries: [], max_entries, }
}
///|
/// Returns `true` if `ephem_x` has not been seen within `window_sec` and records it,
/// or `false` if a replay was detected.
pub fn EphemeralReplayCache::check_and_insert(
self : EphemeralReplayCache,
ephem_x : Bytes,
now_sec : UInt32,
window_sec~ : UInt32 = 120U,
) -> Bool {
let mut i = 0
while i < self.entries.length() {
if now_sec > self.entries[i].1 + window_sec {
let _ = self.entries.remove(i)
} else {
if @crypto.ct_eq(self.entries[i].0, ephem_x) {
return false
}
i += 1
}
}
if self.entries.length() >= self.max_entries {
let _ = self.entries.remove(0)
}
self.entries.push((ephem_x, now_sec))
true
}
///|
/// Like `check_and_insert`, raising `Ntcp2Error::ReplayDetected` on replay.
pub fn EphemeralReplayCache::check_or_raise(
self : EphemeralReplayCache,
ephem_x : Bytes,
now_sec : UInt32,
window_sec~ : UInt32 = 120U,
) -> Unit raise Ntcp2Error {
if !self.check_and_insert(ephem_x, now_sec, window_sec~) {
raise ReplayDetected
}
}
///|
/// Returns the advertised Msg 3 Part 2 ciphertext length (including 16-byte MAC).
pub fn Ntcp2Handshake::m3p2_len(self : Ntcp2Handshake) -> Int32 {
self.m3p2_len
}
///|
/// Returns the decoded remote ephemeral X25519 key (after Msg 1 / Msg 2 head).
pub fn Ntcp2Handshake::remote_ephemeral(self : Ntcp2Handshake) -> Bytes? {
self.remote_ephemeral
}
///|
/// Initiator: writes Message 1 (SessionRequest).
/// Returns `X_obf (32B) || AEAD(options_16B) (32B) || cleartext_padding`.
@@ -404,6 +481,7 @@ pub fn Ntcp2Handshake::write_session_request(
pub fn Ntcp2Handshake::read_session_request_head(
self : Ntcp2Handshake,
head64 : Bytes,
expected_network_id~ : Byte = b'\x02',
) -> SessionRequestOptions raise Ntcp2Error {
if self.initiator || self.step != 0 {
raise InvalidState(
@@ -439,6 +517,9 @@ pub fn Ntcp2Handshake::read_session_request_head(
e => raise Noise(e)
}
let opts = SessionRequestOptions::decode(opts_bytes)
if opts.network_id != expected_network_id {
raise InvalidNetworkId(opts.network_id)
}
if opts.version != b'\x02' {
raise InvalidFormat("unsupported NTCP2 version: \{opts.version.to_int()}")
}
+46
View File
@@ -192,3 +192,49 @@ test "NTCP2 full XKaesobfse handshake + Data Phase SipHash framing round-trip" {
_ => fail("expected I2np block from Bob")
}
}
///|
test "NTCP2 replay cache, clock skew validation, and network_id check" {
let rc = @ntcp2.EphemeralReplayCache::new(max_entries=4)
let x1 = Bytes::make(32, b'\x01')
let x2 = Bytes::make(32, b'\x02')
assert_eq(rc.check_and_insert(x1, 1000U), true)
assert_eq(rc.check_and_insert(x1, 1010U), false) // replay within window
assert_eq(rc.check_and_insert(x2, 1010U), true)
assert_eq(rc.check_and_insert(x1, 1200U), true) // expired after 120s window
@ntcp2.validate_clock_skew(1000U, 1059U)
let mut skewed = false
@ntcp2.validate_clock_skew(1000U, 1061U) catch {
ClockSkew(_, _) => skewed = true
_ => ()
}
assert_eq(skewed, true)
let alice_s = @noise.KeyPair::from_private(Bytes::make(32, b'\x33'))
let alice_e = @noise.KeyPair::from_private(Bytes::make(32, b'\x44'))
let bob_s = @noise.KeyPair::from_private(Bytes::make(32, b'\x55'))
let bob_hash = @crypto.sha256(b"bob-netid")
let bob_iv = Bytes::make(16, b'\x66')
let alice = @ntcp2.Ntcp2Handshake::new_initiator(
alice_s,
bob_s.public_key,
bob_hash,
bob_iv,
)
alice.set_ephemeral(alice_e)
let bad_msg1 = alice.write_session_request(
64,
1700000000U,
network_id=b'\x09',
)
let bob = @ntcp2.Ntcp2Handshake::new_responder(bob_s, bob_hash, bob_iv)
let mut bad_net = false
try {
let _ = bob.read_session_request(bad_msg1)
} catch {
InvalidNetworkId(b'\x09') => bad_net = true
_ => ()
}
assert_eq(bad_net, true)
}
+15 -1
View File
@@ -23,6 +23,8 @@ pub fn i2np_block(UInt32, UInt32, @i2np.I2npPayload) -> Ntcp2Block raise Ntcp2Er
pub fn router_info_block(@common.RouterInfo, flood? : Bool) -> Ntcp2Block raise Ntcp2Error
pub fn validate_clock_skew(UInt32, UInt32, max_skew_sec? : UInt32) -> Unit raise Ntcp2Error
// Errors
#derive(Eq, @debug.Debug)
pub suberror Ntcp2Error {
@@ -34,9 +36,20 @@ pub suberror Ntcp2Error {
InvalidState(String)
InvalidFormat(String)
InvalidLength(String)
InvalidNetworkId(Byte)
ClockSkew(UInt32, UInt32)
ReplayDetected
}
// Types and methods
pub struct EphemeralReplayCache {
entries : Array[(Bytes, UInt32)]
max_entries : Int32
}
pub fn EphemeralReplayCache::check_and_insert(Self, Bytes, UInt32, window_sec? : UInt32) -> Bool
pub fn EphemeralReplayCache::check_or_raise(Self, Bytes, UInt32, window_sec? : UInt32) -> Unit raise Ntcp2Error
pub fn EphemeralReplayCache::new(max_entries? : Int32) -> Self
#derive(Eq, @debug.Debug)
pub(all) enum Ntcp2Block {
DateTime(UInt32)
@@ -87,7 +100,8 @@ pub fn Ntcp2Handshake::read_session_confirmed(Self, Bytes) -> Array[Ntcp2Block]
pub fn Ntcp2Handshake::read_session_created(Self, Bytes) -> SessionCreatedOptions raise Ntcp2Error
pub fn Ntcp2Handshake::read_session_created_head(Self, Bytes) -> SessionCreatedOptions raise Ntcp2Error
pub fn Ntcp2Handshake::read_session_request(Self, Bytes) -> SessionRequestOptions raise Ntcp2Error
pub fn Ntcp2Handshake::read_session_request_head(Self, Bytes) -> SessionRequestOptions raise Ntcp2Error
pub fn Ntcp2Handshake::read_session_request_head(Self, Bytes, expected_network_id? : Byte) -> SessionRequestOptions raise Ntcp2Error
pub fn Ntcp2Handshake::remote_ephemeral(Self) -> Bytes?
pub fn Ntcp2Handshake::set_ephemeral(Self, @noise.KeyPair) -> Unit
pub fn Ntcp2Handshake::write_session_confirmed(Self, Array[Ntcp2Block]) -> Bytes raise Ntcp2Error
pub fn Ntcp2Handshake::write_session_created(Self, UInt32, padding? : Bytes) -> Bytes raise Ntcp2Error