feat: add ratchet, streaming, SU3 reseed, and router floodfill
- 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:
1 parent
8eafa11b22
commit
ae72b00323
54 files changed
+9607
-704
No files matched your search
+119
-41
@@ -119,10 +119,12 @@ fn encode_fragment(frag : TunnelFragment) -> Bytes {
|
||||
|
||||
///|
|
||||
/// Packs one or more `TunnelFragment`s into a cleartext 1024-byte `TunnelData` cell
|
||||
/// with its 16-byte `iv`, 4-byte SHA-256 checksum, non-zero padding, and `0x00` delimiter.
|
||||
/// with its 16-byte `iv`, 4-byte SHA-256 checksum, non-zero padding (using caller-provided
|
||||
/// random `padding` with any `0x00` replaced by `0x01`, or deterministic fallback), and `0x00` delimiter.
|
||||
pub fn pack_tunnel_cell(
|
||||
iv : Bytes,
|
||||
fragments : Array[TunnelFragment],
|
||||
padding~ : Bytes = b"",
|
||||
) -> Bytes raise TunnelError {
|
||||
if iv.length() != 16 {
|
||||
raise InvalidLength("TunnelData IV must be 16 bytes, got \{iv.length()}")
|
||||
@@ -147,9 +149,18 @@ pub fn pack_tunnel_cell(
|
||||
for i = 0; i < 4; i = i + 1 {
|
||||
cell[16 + i] = checksum[i]
|
||||
}
|
||||
// Non-zero deterministic padding bytes
|
||||
// Strictly non-zero padding bytes
|
||||
for i = 0; i < pad_len; i = i + 1 {
|
||||
cell[20 + i] = (i % 254 + 1).to_byte()
|
||||
let b = if i < padding.length() {
|
||||
if padding[i] == b'\x00' {
|
||||
b'\x01'
|
||||
} else {
|
||||
padding[i]
|
||||
}
|
||||
} else {
|
||||
(i % 254 + 1).to_byte()
|
||||
}
|
||||
cell[20 + i] = b
|
||||
}
|
||||
// Zero delimiter at 20 + pad_len
|
||||
cell[20 + pad_len] = b'\x00'
|
||||
@@ -238,6 +249,7 @@ pub fn fragment_message_to_cells(
|
||||
msg_id : UInt32,
|
||||
payload : Bytes,
|
||||
base_iv : Bytes,
|
||||
padding~ : Bytes = b"",
|
||||
) -> Array[Bytes] raise TunnelError {
|
||||
if base_iv.length() != 16 {
|
||||
raise InvalidLength("base_iv must be 16 bytes")
|
||||
@@ -249,18 +261,22 @@ pub fn fragment_message_to_cells(
|
||||
}
|
||||
let single_max = 1003 - (3 + target_overhead)
|
||||
if payload.length() <= single_max {
|
||||
let cell = pack_tunnel_cell(base_iv, [
|
||||
First(target~, fragmented=false, msg_id=0U, data=payload),
|
||||
])
|
||||
let cell = pack_tunnel_cell(
|
||||
base_iv,
|
||||
[First(target~, fragmented=false, msg_id=0U, data=payload)],
|
||||
padding~,
|
||||
)
|
||||
return [cell]
|
||||
}
|
||||
let cells : Array[Bytes] = []
|
||||
let first_max = 1003 - (7 + target_overhead)
|
||||
let first_chunk = t_slice(payload, 0, first_max)
|
||||
cells.push(
|
||||
pack_tunnel_cell(base_iv, [
|
||||
First(target~, fragmented=true, msg_id~, data=first_chunk),
|
||||
]),
|
||||
pack_tunnel_cell(
|
||||
base_iv,
|
||||
[First(target~, fragmented=true, msg_id~, data=first_chunk)],
|
||||
padding~,
|
||||
),
|
||||
)
|
||||
let mut off = first_max
|
||||
let mut frag_num = 1
|
||||
@@ -276,9 +292,11 @@ pub fn fragment_message_to_cells(
|
||||
cell_iv[i] = (base_iv[i].to_int() ^ frag_num).to_byte()
|
||||
}
|
||||
cells.push(
|
||||
pack_tunnel_cell(Bytes::from_array(cell_iv), [
|
||||
FollowOn(frag_num~, is_last~, msg_id~, data=chunk),
|
||||
]),
|
||||
pack_tunnel_cell(
|
||||
Bytes::from_array(cell_iv),
|
||||
[FollowOn(frag_num~, is_last~, msg_id~, data=chunk)],
|
||||
padding~,
|
||||
),
|
||||
)
|
||||
off += take
|
||||
frag_num += 1
|
||||
@@ -289,13 +307,13 @@ pub fn fragment_message_to_cells(
|
||||
///|
|
||||
priv struct PendingReassembly {
|
||||
msg_id : UInt32
|
||||
target : DeliveryTarget
|
||||
mut target : DeliveryTarget?
|
||||
chunks : Array[(Int32, Bytes)]
|
||||
mut last_num : Int32
|
||||
}
|
||||
|
||||
///|
|
||||
/// Stateful reassembler for fragmented `TunnelData` messages.
|
||||
/// Stateful reassembler for fragmented `TunnelData` messages (supports out-of-order fragments).
|
||||
pub struct FragmentReassembler {
|
||||
priv pending : Array[PendingReassembly]
|
||||
}
|
||||
@@ -305,6 +323,65 @@ pub fn FragmentReassembler::new() -> FragmentReassembler {
|
||||
{ pending: [], }
|
||||
}
|
||||
|
||||
///|
|
||||
fn FragmentReassembler::find_or_insert(
|
||||
self : FragmentReassembler,
|
||||
msg_id : UInt32,
|
||||
) -> PendingReassembly {
|
||||
for p in self.pending {
|
||||
if p.msg_id == msg_id {
|
||||
return p
|
||||
}
|
||||
}
|
||||
if self.pending.length() >= 64 {
|
||||
let _ = self.pending.remove(0)
|
||||
}
|
||||
let created : PendingReassembly = {
|
||||
msg_id,
|
||||
target: None,
|
||||
chunks: [],
|
||||
last_num: -1,
|
||||
}
|
||||
self.pending.push(created)
|
||||
created
|
||||
}
|
||||
|
||||
///|
|
||||
fn FragmentReassembler::try_finish(
|
||||
self : FragmentReassembler,
|
||||
msg_id : UInt32,
|
||||
) -> (DeliveryTarget, Bytes)? {
|
||||
for idx = 0; idx < self.pending.length(); idx = idx + 1 {
|
||||
let p = self.pending[idx]
|
||||
if p.msg_id == msg_id {
|
||||
match p.target {
|
||||
None => return None
|
||||
Some(target) =>
|
||||
if p.last_num > 0 && p.chunks.length() == p.last_num + 1 {
|
||||
let w = @encoding.Writer::new()
|
||||
for n = 0; n <= p.last_num; n = n + 1 {
|
||||
let mut found = false
|
||||
for ch in p.chunks {
|
||||
if ch.0 == n {
|
||||
w.write_bytes(ch.1)
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
return None
|
||||
}
|
||||
}
|
||||
let _ = self.pending.remove(idx)
|
||||
return Some((target, w.to_bytes()))
|
||||
}
|
||||
}
|
||||
return None
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
///|
|
||||
/// Feeds a decoded `TunnelFragment` into the reassembler.
|
||||
/// Returns `Some((target, full_payload))` once all fragments of a message have arrived.
|
||||
@@ -317,35 +394,36 @@ pub fn FragmentReassembler::push(
|
||||
if !fragmented {
|
||||
return Some((target, data))
|
||||
}
|
||||
self.pending.push({ msg_id, target, chunks: [(0, data)], last_num: -1, })
|
||||
None
|
||||
}
|
||||
FollowOn(frag_num~, is_last~, msg_id~, data~) => {
|
||||
for idx = 0; idx < self.pending.length(); idx = idx + 1 {
|
||||
let p = self.pending[idx]
|
||||
if p.msg_id == msg_id {
|
||||
p.chunks.push((frag_num, data))
|
||||
if is_last {
|
||||
p.last_num = frag_num
|
||||
}
|
||||
if p.last_num > 0 && p.chunks.length() == p.last_num + 1 {
|
||||
let w = @encoding.Writer::new()
|
||||
for n = 0; n <= p.last_num; n = n + 1 {
|
||||
for ch in p.chunks {
|
||||
if ch.0 == n {
|
||||
w.write_bytes(ch.1)
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
let target = p.target
|
||||
let _ = self.pending.remove(idx)
|
||||
return Some((target, w.to_bytes()))
|
||||
}
|
||||
return None
|
||||
let p = self.find_or_insert(msg_id)
|
||||
p.target = Some(target)
|
||||
let mut dup = false
|
||||
for ch in p.chunks {
|
||||
if ch.0 == 0 {
|
||||
dup = true
|
||||
break
|
||||
}
|
||||
}
|
||||
None
|
||||
if !dup {
|
||||
p.chunks.push((0, data))
|
||||
}
|
||||
self.try_finish(msg_id)
|
||||
}
|
||||
FollowOn(frag_num~, is_last~, msg_id~, data~) => {
|
||||
let p = self.find_or_insert(msg_id)
|
||||
let mut dup = false
|
||||
for ch in p.chunks {
|
||||
if ch.0 == frag_num {
|
||||
dup = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !dup {
|
||||
p.chunks.push((frag_num, data))
|
||||
}
|
||||
if is_last {
|
||||
p.last_num = frag_num
|
||||
}
|
||||
self.try_finish(msg_id)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user