///| 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) at slot 0; creator decrypts it let reply = @tunnel.encrypt_short_build_reply(hop_keys, 0, b'\x00') assert_eq(reply.length(), 218) assert_eq(@tunnel.decrypt_short_build_reply(creator_keys, 0, reply), b'\x00') } ///| test "Proposal 157 multi-hop ShortTunnelBuild onion build, hop relay, OBEP garlic keys, and reply decrypt" { let hops : Array[@tunnel.ShortHopBuildSpec] = [] let hop_statics : Array[@noise.KeyPair] = [] let hop_idents : Array[@common.Hash] = [] for i = 0; i < 3; i = i + 1 { let sp = Bytes::make(32, (0x10 + i).to_byte()) let skp = @noise.KeyPair::from_private(sp) let ep = Bytes::make(32, (0x20 + i).to_byte()) let ekp = @noise.KeyPair::from_private(ep) let ident = @common.Hash::sha256(Bytes::make(4, (0x30 + i).to_byte())) let next_ident = @common.Hash::sha256(Bytes::make(4, (0x31 + i).to_byte())) let flag = if i == 0 { b'\x80' // IBGW } else if i == 2 { b'\x40' // OBEP } else { b'\x00' } let rec : @tunnel.ShortBuildRequestRecord = { receive_tunnel_id: (100 + i).reinterpret_as_uint(), next_tunnel_id: (200 + i).reinterpret_as_uint(), next_ident, flag, layer_enc_type: b'\x00', request_time_min: 28333333U, request_expiration_sec: 600U, send_message_id: (500 + i).reinterpret_as_uint(), options: @common.Mapping::new(), } hop_statics.push(skp) hop_idents.push(ident) hops.push({ slot_idx: (i + 1) % 4, // scrambled slot positions (1, 2, 3) with slot 0 dummy hop_ident: ident, hop_static_pub: skp.public_key, ephemeral: ekp, record: rec, padding: Bytes::make(16, (0x40 + i).to_byte()), }) } let (req_records, creator_hop_keys) = @tunnel.build_short_tunnel_request( 4, hops, [Bytes::make(218, b'\xde')], ) assert_eq(req_records.length(), 4) assert_true(creator_hop_keys[0].1.garlic_key is None) assert_true(creator_hop_keys[2].1.garlic_key is Some(_)) assert_true(creator_hop_keys[2].1.garlic_tag is Some(_)) let mut wire_records = req_records for i = 0; i < 3; i = i + 1 { let (rec, keys, slot, next_records) = @tunnel.process_short_tunnel_build_hop( wire_records, hop_idents[i], hop_statics[i], b'\x00', reply_padding=Bytes::make(16, b'\x77'), ) assert_eq(slot, hops[i].slot_idx) assert_eq(rec, hops[i].record) assert_eq(keys, creator_hop_keys[i].1) wire_records = next_records } let codes = @tunnel.decrypt_short_tunnel_replies( wire_records, creator_hop_keys, ) assert_eq(codes, [b'\x00', b'\x00', b'\x00']) } ///| test "TunnelData FragmentReassembler handles out-of-order FollowOn before First" { let iv = Bytes::make(16, b'\x33') let payload = Bytes::make(2200, b'\xab') let cells = @tunnel.fragment_message_to_cells( Local, 777U, payload, iv, padding=Bytes::make(64, b'\x00'), // zeros sanitized to 0x01 ) assert_eq(cells.length(), 3) let r = @tunnel.FragmentReassembler::new() // Deliver cell 2, then cell 1, then cell 0 assert_eq(r.push(@tunnel.unpack_tunnel_cell(cells[2])[0]), None) assert_eq(r.push(@tunnel.unpack_tunnel_cell(cells[1])[0]), None) assert_eq( r.push(@tunnel.unpack_tunnel_cell(cells[0])[0]), Some((Local, payload)), ) } ///| 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"), ]) }