- 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)
323 lines
9.4 KiB
MoonBit
323 lines
9.4 KiB
MoonBit
///|
|
|
/// Proposal 144 ECIES-X25519-AEAD-Ratchet payload block codec & Garlic Clove delivery instructions.
|
|
|
|
///|
|
|
#derive(Eq, Debug)
|
|
pub suberror RatchetError {
|
|
Crypto(@crypto.CryptoError)
|
|
Noise(@noise.NoiseError)
|
|
Encoding(@encoding.EncodingError)
|
|
Common(@common.CommonError)
|
|
I2np(@i2np.I2npError)
|
|
InvalidLength(String)
|
|
InvalidFormat(String)
|
|
UnknownSession
|
|
AuthenticationFailed
|
|
}
|
|
|
|
///|
|
|
/// Delivery target inside a Garlic Clove block (Block Type 11).
|
|
#derive(Eq, Debug)
|
|
pub(all) enum GarlicDelivery {
|
|
Local
|
|
Destination(@common.Hash)
|
|
Router(@common.Hash)
|
|
Tunnel(@common.Hash, UInt32)
|
|
}
|
|
|
|
///|
|
|
fn encode_garlic_delivery(
|
|
delivery : GarlicDelivery,
|
|
w : @encoding.Writer,
|
|
) -> Unit {
|
|
match delivery {
|
|
Local => w.write_u8(b'\x00')
|
|
Destination(h) => {
|
|
w.write_u8(b'\x20') // delivery type 1 (bits 6..5 = 01)
|
|
h.encode(w)
|
|
}
|
|
Router(h) => {
|
|
w.write_u8(b'\x40') // delivery type 2 (bits 6..5 = 10)
|
|
h.encode(w)
|
|
}
|
|
Tunnel(gw, tid) => {
|
|
w.write_u8(b'\x60') // delivery type 3 (bits 6..5 = 11)
|
|
gw.encode(w)
|
|
w.write_u32_be(tid)
|
|
}
|
|
}
|
|
}
|
|
|
|
///|
|
|
fn decode_garlic_delivery(
|
|
r : @encoding.Reader,
|
|
) -> GarlicDelivery raise RatchetError {
|
|
let flag = (r.read_u8() catch { e => raise Encoding(e) }).to_int()
|
|
if (flag & 0x80) != 0 {
|
|
// Individual clove encryption is unsupported/unused in ECIES
|
|
let _ = r.read_bytes(32) catch { e => raise Encoding(e) }
|
|
}
|
|
let dtype = (flag >> 5) & 0x03
|
|
let res : GarlicDelivery = match dtype {
|
|
0 => Local
|
|
1 => {
|
|
let h = @common.Hash::decode(r) catch { e => raise Common(e) }
|
|
Destination(h)
|
|
}
|
|
2 => {
|
|
let h = @common.Hash::decode(r) catch { e => raise Common(e) }
|
|
Router(h)
|
|
}
|
|
_ => {
|
|
let gw = @common.Hash::decode(r) catch { e => raise Common(e) }
|
|
let tid = r.read_u32_be() catch { e => raise Encoding(e) }
|
|
Tunnel(gw, tid)
|
|
}
|
|
}
|
|
if (flag & 0x10) != 0 {
|
|
let _ = r.read_u32_be() catch { e => raise Encoding(e) }
|
|
}
|
|
res
|
|
}
|
|
|
|
///|
|
|
/// Proposal 144 ECIES-X25519-AEAD-Ratchet payload blocks.
|
|
#derive(Eq, Debug)
|
|
pub(all) enum RatchetBlock {
|
|
/// Type 0: DateTime (4-byte uint32 Unix timestamp in seconds).
|
|
DateTime(UInt32)
|
|
/// Type 4: Termination (`reason`, `additional_data`).
|
|
Termination(reason~ : Byte, additional_data~ : Bytes)
|
|
/// Type 5: Options.
|
|
Options(Bytes)
|
|
/// Type 6: MessageNumber (`pn` uint16 BE previous chain length).
|
|
MessageNumber(UInt32)
|
|
/// Type 7: NextKey (`flags`, `key_id` uint16 BE, optional 32-byte X25519 public key).
|
|
NextKey(flags~ : Byte, key_id~ : UInt32, public_key~ : Bytes?)
|
|
/// Type 8: Ack (pairs of `(key_id, msg_num)` uint16 BE).
|
|
Ack(Array[(UInt32, UInt32)])
|
|
/// Type 9: AckRequest (`flags` 1 byte).
|
|
AckRequest(Byte)
|
|
/// Type 11: GarlicClove (`delivery`, 9-byte short I2NP header + payload bytes).
|
|
GarlicClove(delivery~ : GarlicDelivery, i2np_short~ : Bytes)
|
|
/// Type 254: Padding (must be last block if present).
|
|
Padding(Bytes)
|
|
/// Fallback for other block types.
|
|
Unknown(Byte, Bytes)
|
|
}
|
|
|
|
///|
|
|
pub fn RatchetBlock::encode(
|
|
self : RatchetBlock,
|
|
w : @encoding.Writer,
|
|
) -> Unit raise RatchetError {
|
|
match self {
|
|
DateTime(ts) => {
|
|
w.write_u8(b'\x00')
|
|
w.write_u16_be(4U)
|
|
w.write_u32_be(ts)
|
|
}
|
|
Termination(reason~, additional_data~) => {
|
|
let sz = 1 + additional_data.length()
|
|
if sz > 65535 {
|
|
raise InvalidLength("Termination block exceeds 65535 bytes")
|
|
}
|
|
w.write_u8(b'\x04')
|
|
w.write_u16_be(sz.reinterpret_as_uint())
|
|
w.write_u8(reason)
|
|
w.write_bytes(additional_data)
|
|
}
|
|
Options(data) => {
|
|
if data.length() > 65535 {
|
|
raise InvalidLength("Options block exceeds 65535 bytes")
|
|
}
|
|
w.write_u8(b'\x05')
|
|
w.write_u16_be(data.length().reinterpret_as_uint())
|
|
w.write_bytes(data)
|
|
}
|
|
MessageNumber(pn) => {
|
|
w.write_u8(b'\x06')
|
|
w.write_u16_be(2U)
|
|
w.write_u16_be(pn)
|
|
}
|
|
NextKey(flags~, key_id~, public_key~) => {
|
|
let bw = @encoding.Writer::new(capacity=35)
|
|
bw.write_u8(flags)
|
|
bw.write_u16_be(key_id)
|
|
match public_key {
|
|
Some(pk) => {
|
|
if pk.length() != 32 {
|
|
raise InvalidLength("NextKey public_key must be 32 bytes")
|
|
}
|
|
bw.write_bytes(pk)
|
|
}
|
|
None => ()
|
|
}
|
|
let body = bw.to_bytes()
|
|
w.write_u8(b'\x07')
|
|
w.write_u16_be(body.length().reinterpret_as_uint())
|
|
w.write_bytes(body)
|
|
}
|
|
Ack(pairs) => {
|
|
let sz = pairs.length() * 4
|
|
if sz > 65535 {
|
|
raise InvalidLength("Ack block exceeds 65535 bytes")
|
|
}
|
|
w.write_u8(b'\x08')
|
|
w.write_u16_be(sz.reinterpret_as_uint())
|
|
for p in pairs {
|
|
w.write_u16_be(p.0)
|
|
w.write_u16_be(p.1)
|
|
}
|
|
}
|
|
AckRequest(flags) => {
|
|
w.write_u8(b'\x09')
|
|
w.write_u16_be(1U)
|
|
w.write_u8(flags)
|
|
}
|
|
GarlicClove(delivery~, i2np_short~) => {
|
|
let bw = @encoding.Writer::new(capacity=40 + i2np_short.length())
|
|
encode_garlic_delivery(delivery, bw)
|
|
bw.write_bytes(i2np_short)
|
|
let body = bw.to_bytes()
|
|
if body.length() > 65535 {
|
|
raise InvalidLength("GarlicClove block exceeds 65535 bytes")
|
|
}
|
|
w.write_u8(b'\x0b')
|
|
w.write_u16_be(body.length().reinterpret_as_uint())
|
|
w.write_bytes(body)
|
|
}
|
|
Padding(data) => {
|
|
if data.length() > 65535 {
|
|
raise InvalidLength("Padding block exceeds 65535 bytes")
|
|
}
|
|
w.write_u8(b'\xfe')
|
|
w.write_u16_be(data.length().reinterpret_as_uint())
|
|
w.write_bytes(data)
|
|
}
|
|
Unknown(t, data) => {
|
|
if data.length() > 65535 {
|
|
raise InvalidLength("Ratchet block exceeds 65535 bytes")
|
|
}
|
|
w.write_u8(t)
|
|
w.write_u16_be(data.length().reinterpret_as_uint())
|
|
w.write_bytes(data)
|
|
}
|
|
}
|
|
}
|
|
|
|
///|
|
|
pub fn encode_blocks(blocks : Array[RatchetBlock]) -> Bytes raise RatchetError {
|
|
let w = @encoding.Writer::new(capacity=256)
|
|
for blk in blocks {
|
|
blk.encode(w)
|
|
}
|
|
w.to_bytes()
|
|
}
|
|
|
|
///|
|
|
pub fn decode_blocks(data : Bytes) -> Array[RatchetBlock] raise RatchetError {
|
|
let r = @encoding.Reader::new(data)
|
|
let out : Array[RatchetBlock] = []
|
|
while !r.is_eof() {
|
|
let btype = r.read_u8() catch { e => raise Encoding(e) }
|
|
let size = (r.read_u16_be() catch { e => raise Encoding(e) }).reinterpret_as_int()
|
|
let body = r.read_bytes(size) catch { e => raise Encoding(e) }
|
|
match btype.to_int() {
|
|
0 => {
|
|
if size != 4 {
|
|
raise InvalidLength("DateTime block must be 4 bytes, got \{size}")
|
|
}
|
|
let br = @encoding.Reader::new(body)
|
|
let ts = br.read_u32_be() catch { e => raise Encoding(e) }
|
|
out.push(DateTime(ts))
|
|
}
|
|
4 => {
|
|
if size < 1 {
|
|
raise InvalidLength("Termination block must be at least 1 byte")
|
|
}
|
|
let br = @encoding.Reader::new(body)
|
|
let reason = br.read_u8() catch { e => raise Encoding(e) }
|
|
out.push(Termination(reason~, additional_data=br.read_remaining()))
|
|
}
|
|
5 => out.push(Options(body))
|
|
6 => {
|
|
if size != 2 {
|
|
raise InvalidLength(
|
|
"MessageNumber block must be 2 bytes, got \{size}",
|
|
)
|
|
}
|
|
let br = @encoding.Reader::new(body)
|
|
let pn = br.read_u16_be() catch { e => raise Encoding(e) }
|
|
out.push(MessageNumber(pn))
|
|
}
|
|
7 => {
|
|
if size < 3 {
|
|
raise InvalidLength("NextKey block shorter than 3 bytes")
|
|
}
|
|
let br = @encoding.Reader::new(body)
|
|
let flags = br.read_u8() catch { e => raise Encoding(e) }
|
|
let key_id = br.read_u16_be() catch { e => raise Encoding(e) }
|
|
let public_key = if (flags.to_int() & 0x01) != 0 {
|
|
Some(br.read_bytes(32) catch { e => raise Encoding(e) })
|
|
} else {
|
|
None
|
|
}
|
|
out.push(NextKey(flags~, key_id~, public_key~))
|
|
}
|
|
8 => {
|
|
if size % 4 != 0 {
|
|
raise InvalidLength("Ack block size must be multiple of 4 bytes")
|
|
}
|
|
let br = @encoding.Reader::new(body)
|
|
let pairs : Array[(UInt32, UInt32)] = []
|
|
while !br.is_eof() {
|
|
let kid = br.read_u16_be() catch { e => raise Encoding(e) }
|
|
let n = br.read_u16_be() catch { e => raise Encoding(e) }
|
|
pairs.push((kid, n))
|
|
}
|
|
out.push(Ack(pairs))
|
|
}
|
|
9 => {
|
|
if size != 1 {
|
|
raise InvalidLength("AckRequest block must be 1 byte")
|
|
}
|
|
out.push(AckRequest(body[0]))
|
|
}
|
|
11 => {
|
|
let br = @encoding.Reader::new(body)
|
|
let delivery = decode_garlic_delivery(br)
|
|
let i2np_short = br.read_remaining()
|
|
out.push(GarlicClove(delivery~, i2np_short~))
|
|
}
|
|
254 => out.push(Padding(body))
|
|
_ => out.push(Unknown(btype, body))
|
|
}
|
|
}
|
|
out
|
|
}
|
|
|
|
///|
|
|
/// Builds a `GarlicClove` block (type 11) wrapping a 9-byte short I2NP message.
|
|
pub fn garlic_clove_block(
|
|
delivery : GarlicDelivery,
|
|
msg_id : UInt32,
|
|
expiration_sec : UInt32,
|
|
payload : @i2np.I2npPayload,
|
|
) -> RatchetBlock raise RatchetError {
|
|
let short_msg = @i2np.encode_short_message(msg_id, expiration_sec, payload) catch {
|
|
e => raise I2np(e)
|
|
}
|
|
GarlicClove(delivery~, i2np_short=short_msg)
|
|
}
|
|
|
|
///|
|
|
/// Decodes the 9-byte short I2NP message inside a `GarlicClove` block.
|
|
pub fn decode_garlic_clove_i2np(
|
|
i2np_short : Bytes,
|
|
) -> (@i2np.ShortHeader, @i2np.I2npPayload) raise RatchetError {
|
|
@i2np.decode_short_message(@encoding.Reader::new(i2np_short)) catch {
|
|
e => raise I2np(e)
|
|
}
|
|
}
|