165 lines
5.1 KiB
MoonBit
165 lines
5.1 KiB
MoonBit
///|
|
|
test "Proposal 157 ShortTunnelBuild 218-byte record & reply round-trip" {
|
|
let hop_static = @noise.KeyPair::from_private(Bytes::make(32, b'\x11'))
|
|
let hop_hash = @common.Hash::sha256(b"hop-router-1")
|
|
let wrong_hash = @common.Hash::sha256(b"other-router")
|
|
let ephem = @noise.KeyPair::from_private(Bytes::make(32, b'\x22'))
|
|
let next_hash = @common.Hash::sha256(b"hop-router-2")
|
|
|
|
let req : @tunnel.ShortBuildRequestRecord = {
|
|
receive_tunnel_id: 1001U,
|
|
next_tunnel_id: 2002U,
|
|
next_ident: next_hash,
|
|
flag: b'\x80', // IBGW
|
|
layer_enc_type: b'\x00',
|
|
request_time_min: 28333333U,
|
|
request_expiration_sec: 600U,
|
|
send_message_id: 5555U,
|
|
options: @common.Mapping::new(),
|
|
}
|
|
let (enc_rec, creator_keys) = @tunnel.encrypt_short_build_record(
|
|
req,
|
|
hop_hash,
|
|
hop_static.public_key,
|
|
ephem,
|
|
)
|
|
assert_eq(enc_rec.length(), 218)
|
|
|
|
// Wrong hop rejects with RecordNotForUs
|
|
let mut rejected = false
|
|
try {
|
|
let _ = @tunnel.decrypt_short_build_record(enc_rec, wrong_hash, hop_static)
|
|
} catch {
|
|
RecordNotForUs => rejected = true
|
|
_ => ()
|
|
}
|
|
assert_eq(rejected, true)
|
|
|
|
// Target hop decrypts record and derives identical HopKeys
|
|
let (dec_rec, hop_keys) = @tunnel.decrypt_short_build_record(
|
|
enc_rec, hop_hash, hop_static,
|
|
)
|
|
assert_eq(dec_rec, req)
|
|
assert_eq(hop_keys, creator_keys)
|
|
|
|
// Hop encrypts 218-byte accept reply (code 0); creator decrypts it
|
|
let reply = @tunnel.encrypt_short_build_reply(hop_keys, b'\x00')
|
|
assert_eq(reply.length(), 218)
|
|
assert_eq(@tunnel.decrypt_short_build_reply(creator_keys, reply), b'\x00')
|
|
}
|
|
|
|
///|
|
|
test "TunnelData 1024-byte AES-256-CBC layer crypto and multi-hop relay with fragmentation" {
|
|
// Build a 3-hop outbound tunnel with 3 transit managers
|
|
let hops : Array[@tunnel.TunnelHop] = []
|
|
let managers : Array[@tunnel.TunnelManager] = []
|
|
for i = 0; i < 3; i = i + 1 {
|
|
let b = (i + 1).to_byte()
|
|
let layer_key = @crypto.sha256(Bytes::make(8, b))
|
|
let iv_key = @crypto.sha256(Bytes::make(8, (i + 11).to_byte()))
|
|
let recv_tid = (1000 + i).reinterpret_as_uint()
|
|
let next_tid = (1001 + i).reinterpret_as_uint()
|
|
let rhash = @common.Hash::sha256(Bytes::make(4, b))
|
|
let next_rhash = @common.Hash::sha256(Bytes::make(4, (i + 2).to_byte()))
|
|
hops.push({
|
|
router_hash: rhash,
|
|
receive_tunnel_id: recv_tid,
|
|
next_tunnel_id: next_tid,
|
|
layer_key,
|
|
iv_key,
|
|
})
|
|
let mgr = @tunnel.TunnelManager::new()
|
|
mgr.add_transit_hop({
|
|
receive_tunnel_id: recv_tid,
|
|
next_tunnel_id: next_tid,
|
|
next_router: next_rhash,
|
|
layer_key,
|
|
iv_key,
|
|
is_obep: i == 2,
|
|
expires_sec: 1700000600U,
|
|
})
|
|
managers.push(mgr)
|
|
}
|
|
|
|
let out_tunnel : @tunnel.Tunnel = {
|
|
id: 1000U,
|
|
direction: Outbound,
|
|
hops,
|
|
created_sec: 1700000000U,
|
|
expires_sec: 1700000600U,
|
|
}
|
|
|
|
// Create a 2500-byte payload so it fragments across 3 TunnelData cells
|
|
let big_buf = FixedArray::make(2500, b'\x00')
|
|
for i = 0; i < 2500; i = i + 1 {
|
|
big_buf[i] = ((i * 37 + 13) & 0xFF).to_byte()
|
|
}
|
|
let big_payload = Bytes::from_array(big_buf)
|
|
let base_iv = Bytes::make(16, b'\x5a')
|
|
let dest_gw = @common.Hash::sha256(b"target-ibgw")
|
|
let cells = @tunnel.fragment_message_to_cells(
|
|
Tunnel(tunnel_id=7777U, gateway=dest_gw),
|
|
98765U,
|
|
big_payload,
|
|
base_iv,
|
|
)
|
|
assert_eq(cells.length(), 3)
|
|
|
|
// Originator pre-decrypts each cell, relays through all 3 hops, and OBEP unpacks & reassembles
|
|
let reasm = @tunnel.FragmentReassembler::new()
|
|
let mut delivered : (@tunnel.DeliveryTarget, Bytes)? = None
|
|
for cell in cells {
|
|
let mut wire = out_tunnel.prepare_outbound_cell(cell)
|
|
let mut cur_tid = 1000U
|
|
for hop_idx = 0; hop_idx < 3; hop_idx = hop_idx + 1 {
|
|
match managers[hop_idx].relay_cell(cur_tid, wire) {
|
|
Some((_next_r, next_tid, next_cell, is_obep)) => {
|
|
wire = next_cell
|
|
cur_tid = next_tid
|
|
assert_eq(is_obep, hop_idx == 2)
|
|
}
|
|
None => fail("transit hop did not match tunnel id")
|
|
}
|
|
}
|
|
// After hop 2 (OBEP), `wire` is the original cleartext 1024-byte cell!
|
|
assert_eq(wire, cell)
|
|
let frags = @tunnel.unpack_tunnel_cell(wire)
|
|
for f in frags {
|
|
match reasm.push(f) {
|
|
Some(res) => delivered = Some(res)
|
|
None => ()
|
|
}
|
|
}
|
|
}
|
|
assert_eq(
|
|
delivered,
|
|
Some((Tunnel(tunnel_id=7777U, gateway=dest_gw), big_payload)),
|
|
)
|
|
|
|
// Also verify inbound multi-hop encryption -> originator decrypt_inbound_cell
|
|
let in_tunnel : @tunnel.Tunnel = {
|
|
id: 1000U,
|
|
direction: Inbound,
|
|
hops,
|
|
created_sec: 1700000000U,
|
|
expires_sec: 1700000600U,
|
|
}
|
|
let in_cell = @tunnel.pack_tunnel_cell(base_iv, [
|
|
First(target=Local, fragmented=false, msg_id=0U, data=b"inbound-i2np"),
|
|
])
|
|
let mut enc_in = in_cell
|
|
for hop_idx = 0; hop_idx < 3; hop_idx = hop_idx + 1 {
|
|
enc_in = @tunnel.tunnel_layer_encrypt(
|
|
hops[hop_idx].layer_key,
|
|
hops[hop_idx].iv_key,
|
|
enc_in,
|
|
)
|
|
}
|
|
let dec_in = in_tunnel.decrypt_inbound_cell(enc_in)
|
|
assert_eq(dec_in, in_cell)
|
|
let in_frags = @tunnel.unpack_tunnel_cell(dec_in)
|
|
assert_eq(in_frags, [
|
|
First(target=Local, fragmented=false, msg_id=0U, data=b"inbound-i2np"),
|
|
])
|
|
}
|