Files
moon-i2p/common/router_info.mbt
T
nandi ae72b00323
check / check (windows-latest) (push) Failing after 27s
check / check (ubuntu-latest) (push) Failing after 29s
check / check (macos-latest) (push) Failing after 27s
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)
2026-11-27 22:23:46 -08:00

517 lines
14 KiB
MoonBit

///|
/// RouterAddress: transport endpoint advertised inside a RouterInfo (e.g., NTCP2, SSU2).
#derive(Eq, Debug)
pub(all) struct RouterAddress {
cost : Byte
expiration : UInt64 // milliseconds since Unix epoch (0 = never)
transport_style : String
options : Mapping
}
///|
pub fn RouterAddress::new(
cost : Byte,
transport_style : String,
options : Mapping,
expiration~ : UInt64 = 0UL,
) -> RouterAddress {
{ cost, expiration, transport_style, options, }
}
///|
pub fn RouterAddress::encode(
self : RouterAddress,
w : @encoding.Writer,
) -> Unit raise CommonError {
w.write_u8(self.cost)
w.write_u64_be(self.expiration)
w.write_i2p_string(self.transport_style) catch {
e => raise Encoding(e)
}
self.options.encode(w)
}
///|
pub fn RouterAddress::decode(
r : @encoding.Reader,
) -> RouterAddress raise CommonError {
let cost = r.read_u8() catch { e => raise Encoding(e) }
let expiration = r.read_u64_be() catch { e => raise Encoding(e) }
let transport_style = r.read_i2p_string() catch { e => raise Encoding(e) }
let options = Mapping::decode(r)
{ cost, expiration, transport_style, options, }
}
///|
/// RouterInfo: NetDB record advertising a router's identity, transport addresses, and capabilities.
#derive(Eq, Debug)
pub(all) struct RouterInfo {
identity : RouterIdentity
published : UInt64 // milliseconds since Unix epoch
addresses : Array[RouterAddress]
options : Mapping
signature : Bytes
}
///|
/// Encodes the unsigned body of RouterInfo (the exact bytes signed by `signature`).
pub fn RouterInfo::unsigned_bytes(self : RouterInfo) -> Bytes raise CommonError {
let w = @encoding.Writer::new(capacity=512)
self.identity.encode(w)
w.write_u64_be(self.published)
w.write_u8(self.addresses.length().to_byte())
for addr in self.addresses {
addr.encode(w)
}
// peer_size is always 0 in I2P spec
w.write_u8(b'\x00')
self.options.encode(w)
w.to_bytes()
}
///|
pub fn RouterInfo::encode(
self : RouterInfo,
w : @encoding.Writer,
) -> Unit raise CommonError {
w.write_bytes(self.unsigned_bytes())
w.write_bytes(self.signature)
}
///|
pub fn RouterInfo::to_bytes(self : RouterInfo) -> Bytes raise CommonError {
let w = @encoding.Writer::new(capacity=600)
self.encode(w)
w.to_bytes()
}
///|
/// Signs the `RouterInfo` using an Ed25519 32-byte private `seed` and returns
/// a new `RouterInfo` with `signature` populated.
pub fn RouterInfo::sign_ed25519(
self : RouterInfo,
seed : Bytes,
) -> RouterInfo raise CommonError {
if self.identity.signing_type() != EdDsaSha512Ed25519 {
raise InvalidFormat(
"RouterInfo identity signing type must be EdDsaSha512Ed25519",
)
}
let body = self.unsigned_bytes()
let signature = @crypto.ed25519_sign(seed, body) catch {
e => raise Crypto(e)
}
{
identity: self.identity,
published: self.published,
addresses: self.addresses,
options: self.options,
signature,
}
}
///|
/// Verifies the `RouterInfo` Ed25519 signature against `self.identity.signing_public_key()`.
pub fn RouterInfo::verify_signature(self : RouterInfo) -> Bool {
if self.identity.signing_type() != EdDsaSha512Ed25519 {
return false
}
let body = self.unsigned_bytes() catch { _ => return false }
@crypto.ed25519_verify(
self.identity.signing_public_key(),
body,
self.signature,
)
}
///|
pub fn RouterInfo::decode(r : @encoding.Reader) -> RouterInfo raise CommonError {
let identity = RouterIdentity::decode(r)
let published = r.read_u64_be() catch { e => raise Encoding(e) }
let addr_count = (r.read_u8() catch { e => raise Encoding(e) }).to_int()
let addresses : Array[RouterAddress] = Array(capacity=addr_count)
for _i = 0; _i < addr_count; _i = _i + 1 {
addresses.push(RouterAddress::decode(r))
}
let peer_size = (r.read_u8() catch { e => raise Encoding(e) }).to_int()
for _i = 0; _i < peer_size; _i = _i + 1 {
let _ = Hash::decode(r)
}
let options = Mapping::decode(r)
let sig_len = identity.signing_type().signature_length()
let signature = if sig_len > 0 {
r.read_bytes(sig_len) catch {
e => raise Encoding(e)
}
} else {
r.read_remaining()
}
{ identity, published, addresses, options, signature, }
}
///|
/// Legacy Lease (44 bytes): 32-byte tunnel gateway hash, 4-byte tunnel ID, 8-byte ms end date.
#derive(Eq, Debug)
pub(all) struct Lease {
tunnel_gw : Hash
tunnel_id : UInt32
end_date_ms : UInt64
}
///|
pub fn Lease::encode(self : Lease, w : @encoding.Writer) -> Unit {
self.tunnel_gw.encode(w)
w.write_u32_be(self.tunnel_id)
w.write_u64_be(self.end_date_ms)
}
///|
pub fn Lease::decode(r : @encoding.Reader) -> Lease raise CommonError {
let tunnel_gw = Hash::decode(r)
let tunnel_id = r.read_u32_be() catch { e => raise Encoding(e) }
let end_date_ms = r.read_u64_be() catch { e => raise Encoding(e) }
{ tunnel_gw, tunnel_id, end_date_ms, }
}
///|
/// Modern Lease2 (40 bytes, used in LeaseSet2): 32-byte tunnel gateway hash, 4-byte tunnel ID,
/// 4-byte end date in seconds since Unix epoch.
#derive(Eq, Debug)
pub(all) struct Lease2 {
tunnel_gw : Hash
tunnel_id : UInt32
end_date_sec : UInt32
}
///|
pub fn Lease2::encode(self : Lease2, w : @encoding.Writer) -> Unit {
self.tunnel_gw.encode(w)
w.write_u32_be(self.tunnel_id)
w.write_u32_be(self.end_date_sec)
}
///|
pub fn Lease2::decode(r : @encoding.Reader) -> Lease2 raise CommonError {
let tunnel_gw = Hash::decode(r)
let tunnel_id = r.read_u32_be() catch { e => raise Encoding(e) }
let end_date_sec = r.read_u32_be() catch { e => raise Encoding(e) }
{ tunnel_gw, tunnel_id, end_date_sec, }
}
///|
/// Encryption key entry inside a LeaseSet2.
#derive(Eq, Debug)
pub(all) struct LeaseSet2EncryptionKey {
key_type : CryptoPublicKeyType
key_data : Bytes
}
///|
/// Offline signature block inside a `LeaseSet2` (Proposal 123, `flags & 1 != 0`):
/// allows a transient signing key to sign the LeaseSet2 while the Destination's
/// long-term signing key stays offline.
#derive(Eq, Debug)
pub(all) struct OfflineSignature {
expires_sec : UInt32
transient_sig_type : SigningPublicKeyType
transient_pub_key : Bytes
signature : Bytes
}
///|
pub fn OfflineSignature::unsigned_bytes(self : OfflineSignature) -> Bytes {
let w = @encoding.Writer::new(capacity=6 + self.transient_pub_key.length())
w.write_u32_be(self.expires_sec)
w.write_u16_be(self.transient_sig_type.to_u16())
w.write_bytes(self.transient_pub_key)
w.to_bytes()
}
///|
pub fn OfflineSignature::encode(
self : OfflineSignature,
w : @encoding.Writer,
) -> Unit {
w.write_bytes(self.unsigned_bytes())
w.write_bytes(self.signature)
}
///|
pub fn OfflineSignature::decode(
r : @encoding.Reader,
dest_sig_type : SigningPublicKeyType,
) -> OfflineSignature raise CommonError {
let expires_sec = r.read_u32_be() catch { e => raise Encoding(e) }
let transient_sig_type = SigningPublicKeyType::from_u16(
r.read_u16_be() catch {
e => raise Encoding(e)
},
)
let tkey_len = transient_sig_type.public_key_length()
if tkey_len <= 0 {
raise InvalidFormat(
"unsupported transient signing type in OfflineSignature",
)
}
let transient_pub_key = r.read_bytes(tkey_len) catch {
e => raise Encoding(e)
}
let dsig_len = dest_sig_type.signature_length()
if dsig_len <= 0 {
raise InvalidFormat(
"unsupported destination signing type in OfflineSignature",
)
}
let signature = r.read_bytes(dsig_len) catch { e => raise Encoding(e) }
{ expires_sec, transient_sig_type, transient_pub_key, signature, }
}
///|
pub fn OfflineSignature::sign_ed25519(
self : OfflineSignature,
dest_seed : Bytes,
) -> OfflineSignature raise CommonError {
let sig = @crypto.ed25519_sign(dest_seed, self.unsigned_bytes()) catch {
e => raise Crypto(e)
}
{
expires_sec: self.expires_sec,
transient_sig_type: self.transient_sig_type,
transient_pub_key: self.transient_pub_key,
signature: sig,
}
}
///|
pub fn OfflineSignature::verify_ed25519(
self : OfflineSignature,
dest_signing_pub : Bytes,
) -> Bool {
@crypto.ed25519_verify(
dest_signing_pub,
self.unsigned_bytes(),
self.signature,
)
}
///|
/// Modern LeaseSet2 (I2P Proposal 123, NetDB type 3).
#derive(Eq, Debug)
pub(all) struct LeaseSet2 {
destination : Destination
published_sec : UInt32
expires_offset : UInt32 // uint16 offset in seconds from published_sec
flags : UInt32 // uint16 (bit 0 = offline keys present)
offline_sig : OfflineSignature?
options : Mapping
encryption_keys : Array[LeaseSet2EncryptionKey]
leases : Array[Lease2]
signature : Bytes
}
///|
/// Encodes the unsigned body of LeaseSet2 (everything before `signature`).
pub fn LeaseSet2::unsigned_bytes(self : LeaseSet2) -> Bytes raise CommonError {
let w = @encoding.Writer::new(capacity=512)
self.destination.encode(w)
w.write_u32_be(self.published_sec)
w.write_u16_be(self.expires_offset)
w.write_u16_be(self.flags)
if (self.flags & 0x0001U) != 0U {
match self.offline_sig {
Some(os) => os.encode(w)
None =>
raise InvalidFormat(
"LeaseSet2 offline signature flag set without offline_sig",
)
}
}
self.options.encode(w)
w.write_u8(self.encryption_keys.length().to_byte())
for ek in self.encryption_keys {
w.write_u16_be(ek.key_type.to_u16())
w.write_u16_be(ek.key_data.length().reinterpret_as_uint())
w.write_bytes(ek.key_data)
}
w.write_u8(self.leases.length().to_byte())
for lease in self.leases {
lease.encode(w)
}
w.to_bytes()
}
///|
/// Returns the exact bytes covered by the LeaseSet2 signature:
/// single-byte DatabaseStore type `0x03` prepended to `unsigned_bytes()` (Proposal 123).
pub fn LeaseSet2::signing_bytes(self : LeaseSet2) -> Bytes raise CommonError {
let body = self.unsigned_bytes()
let w = @encoding.Writer::new(capacity=1 + body.length())
w.write_u8(b'\x03')
w.write_bytes(body)
w.to_bytes()
}
///|
pub fn LeaseSet2::encode(
self : LeaseSet2,
w : @encoding.Writer,
) -> Unit raise CommonError {
w.write_bytes(self.unsigned_bytes())
w.write_bytes(self.signature)
}
///|
pub fn LeaseSet2::to_bytes(self : LeaseSet2) -> Bytes raise CommonError {
let w = @encoding.Writer::new(capacity=600)
self.encode(w)
w.to_bytes()
}
///|
/// Signs the `LeaseSet2` using an Ed25519 32-byte private `seed` (over `0x03 || unsigned_bytes`).
pub fn LeaseSet2::sign_ed25519(
self : LeaseSet2,
seed : Bytes,
) -> LeaseSet2 raise CommonError {
if self.destination.inner.signing_type() != EdDsaSha512Ed25519 {
raise InvalidFormat(
"LeaseSet2 destination signing type must be EdDsaSha512Ed25519",
)
}
let to_sign = self.signing_bytes()
let signature = @crypto.ed25519_sign(seed, to_sign) catch {
e => raise Crypto(e)
}
{
destination: self.destination,
published_sec: self.published_sec,
expires_offset: self.expires_offset,
flags: self.flags,
offline_sig: self.offline_sig,
options: self.options,
encryption_keys: self.encryption_keys,
leases: self.leases,
signature,
}
}
///|
/// Signs a `LeaseSet2` that carries an `OfflineSignature` (`flags & 1 != 0`)
/// using the transient Ed25519 signing seed.
pub fn LeaseSet2::sign_with_transient_ed25519(
self : LeaseSet2,
transient_seed : Bytes,
) -> LeaseSet2 raise CommonError {
if (self.flags & 0x0001U) == 0U {
raise InvalidFormat("LeaseSet2 offline signature flag (bit 0) must be set")
}
match self.offline_sig {
Some(os) =>
if os.transient_sig_type != EdDsaSha512Ed25519 {
raise InvalidFormat(
"LeaseSet2 transient signing type must be EdDsaSha512Ed25519",
)
}
None => raise InvalidFormat("LeaseSet2 offline_sig is missing")
}
let to_sign = self.signing_bytes()
let signature = @crypto.ed25519_sign(transient_seed, to_sign) catch {
e => raise Crypto(e)
}
{
destination: self.destination,
published_sec: self.published_sec,
expires_offset: self.expires_offset,
flags: self.flags,
offline_sig: self.offline_sig,
options: self.options,
encryption_keys: self.encryption_keys,
leases: self.leases,
signature,
}
}
///|
/// Verifies the `LeaseSet2` Ed25519 signature (including any `OfflineSignature` chain).
pub fn LeaseSet2::verify_signature(self : LeaseSet2) -> Bool {
if self.destination.inner.signing_type() != EdDsaSha512Ed25519 {
return false
}
let to_sign = self.signing_bytes() catch { _ => return false }
if (self.flags & 0x0001U) != 0U {
match self.offline_sig {
Some(os) => {
if os.transient_sig_type != EdDsaSha512Ed25519 {
return false
}
if !os.verify_ed25519(self.destination.inner.signing_public_key()) {
return false
}
@crypto.ed25519_verify(os.transient_pub_key, to_sign, self.signature)
}
None => false
}
} else {
@crypto.ed25519_verify(
self.destination.inner.signing_public_key(),
to_sign,
self.signature,
)
}
}
///|
pub fn LeaseSet2::decode(r : @encoding.Reader) -> LeaseSet2 raise CommonError {
let destination = Destination::decode(r)
let published_sec = r.read_u32_be() catch { e => raise Encoding(e) }
let expires_offset = r.read_u16_be() catch { e => raise Encoding(e) }
let flags = r.read_u16_be() catch { e => raise Encoding(e) }
let offline_sig = if (flags & 0x0001U) != 0U {
Some(OfflineSignature::decode(r, destination.inner.signing_type()))
} else {
None
}
let options = Mapping::decode(r)
let key_count = (r.read_u8() catch { e => raise Encoding(e) }).to_int()
let encryption_keys : Array[LeaseSet2EncryptionKey] = Array(
capacity=key_count,
)
for _i = 0; _i < key_count; _i = _i + 1 {
let kt = CryptoPublicKeyType::from_u16(
r.read_u16_be() catch {
e => raise Encoding(e)
},
)
let klen = (r.read_u16_be() catch { e => raise Encoding(e) }).reinterpret_as_int()
let kd = r.read_bytes(klen) catch { e => raise Encoding(e) }
encryption_keys.push({ key_type: kt, key_data: kd, })
}
let lease_count = (r.read_u8() catch { e => raise Encoding(e) }).to_int()
let leases : Array[Lease2] = Array(capacity=lease_count)
for _i = 0; _i < lease_count; _i = _i + 1 {
leases.push(Lease2::decode(r))
}
let sig_len = match offline_sig {
Some(os) => os.transient_sig_type.signature_length()
None => destination.inner.signing_type().signature_length()
}
let signature = if sig_len > 0 {
r.read_bytes(sig_len) catch {
e => raise Encoding(e)
}
} else {
r.read_remaining()
}
{
destination,
published_sec,
expires_offset,
flags,
offline_sig,
options,
encryption_keys,
leases,
signature,
}
}