///| fn det_rand_gen(seed_init : Byte) -> () -> Bytes { let ctr : Ref[Int32] = { val: seed_init.to_int(), } fn() { let idx = ctr.val ctr.val = idx + 1 let w = @encoding.Writer::new(capacity=8) w.write_u32_be(idx.reinterpret_as_uint()) @crypto.sha256(w.to_bytes()) } } ///| test "Proposal 144 GarlicClove and RatchetBlock encode/decode round-trip" { let clove = @ratchet.garlic_clove_block( Destination(@common.Hash::sha256(b"dest-a")), 42U, 1700000600U, Data(data=b"hello-clove"), ) let blocks : Array[@ratchet.RatchetBlock] = [ DateTime(1700000123U), Options(b"\x01\x02"), MessageNumber(5U), NextKey(flags=b'\x05', key_id=1U, public_key=Some(Bytes::make(32, b'\xaa'))), Ack([(1U, 3U), (1U, 4U)]), AckRequest(b'\x00'), clove, Padding(b"\x00\x00\x00"), ] let wire = @ratchet.encode_blocks(blocks) let decoded = @ratchet.decode_blocks(wire) assert_eq(decoded, blocks) match decoded[6] { GarlicClove(delivery~, i2np_short~) => { assert_eq(delivery, Destination(@common.Hash::sha256(b"dest-a"))) let (hdr, payload) = @ratchet.decode_garlic_clove_i2np(i2np_short) assert_eq(hdr.msg_id, 42U) assert_eq(payload, Data(data=b"hello-clove")) } _ => fail("expected GarlicClove") } } ///| test "Proposal 144 ECIES-X25519-AEAD-Ratchet NS -> NSR -> bidirectional ES + DH ratchet + Unbound + DSM" { let alice_s = @noise.KeyPair::from_private( @crypto.sha256(b"alice-ecies-static"), ) let bob_s = @noise.KeyPair::from_private(@crypto.sha256(b"bob-ecies-static")) let alice_mgr = @ratchet.RatchetSessionManager::new(alice_s) let bob_mgr = @ratchet.RatchetSessionManager::new(bob_s) let rand_a = det_rand_gen(b'\x10') let rand_b = det_rand_gen(b'\x50') // 1. Alice -> Bob: Bound New Session let c1 = @ratchet.garlic_clove_block( Local, 1U, 1700000600U, Data(data=b"msg-1-ns"), ) let ns_wire = alice_mgr.encrypt_to_peer( bob_s.public_key, [c1], 1700000100U, rand_a, ) let (bob_from, bob_dec1) = bob_mgr.decrypt_incoming(ns_wire) assert_eq(bob_from, Some(alice_s.public_key)) assert_eq(bob_dec1.length(), 2) // DateTime + GarlicClove // 2. Bob -> Alice: New Session Reply (NSR) let c2 = @ratchet.garlic_clove_block( Local, 2U, 1700000600U, Data(data=b"msg-2-nsr"), ) let nsr_wire = bob_mgr.encrypt_to_peer( alice_s.public_key, [c2], 1700000101U, rand_b, ) let (alice_from, alice_dec2) = alice_mgr.decrypt_incoming(nsr_wire) assert_eq(alice_from, Some(bob_s.public_key)) assert_eq(alice_dec2, [c2]) // 3. Alice <-> Bob: Existing Session (ES) bidirectional messages for i = 0; i < 4; i = i + 1 { let ca = @ratchet.garlic_clove_block( Local, (10 + i).reinterpret_as_uint(), 1700000600U, Data(data=b"alice-es"), ) let w_ab = alice_mgr.encrypt_to_peer( bob_s.public_key, [ca], 1700000102U, rand_a, ) let (f1, d1) = bob_mgr.decrypt_incoming(w_ab) assert_eq(f1, Some(alice_s.public_key)) assert_eq(d1, [ca]) let cb = @ratchet.garlic_clove_block( Local, (20 + i).reinterpret_as_uint(), 1700000600U, Data(data=b"bob-es"), ) let w_ba = bob_mgr.encrypt_to_peer( alice_s.public_key, [cb], 1700000103U, rand_b, ) let (f2, d2) = alice_mgr.decrypt_incoming(w_ba) assert_eq(f2, Some(bob_s.public_key)) assert_eq(d2, [cb]) } // 3b. DH Ratchet rotation with NextKey block match alice_mgr.create_next_key_block(bob_s.public_key, rand_a) { Some(nk_blk) => { let w_nk = alice_mgr.encrypt_to_peer( bob_s.public_key, [nk_blk], 1700000105U, rand_a, ) let (_, d_nk) = bob_mgr.decrypt_incoming(w_nk) assert_eq(d_nk, [nk_blk]) } None => fail("expected NextKey block on established session") } // 4. Unbound New Session let ephem_u = @ratchet.generate_elligator_keypair(rand_a) let (ub_wire, ub_pending) = @ratchet.encrypt_new_session( None, bob_s.public_key, ephem_u, [DateTime(1700000110U), GarlicClove(delivery=Local, i2np_short=b"unbound")], ) assert_true(ub_pending is None) let (ub_dec, ub_rx_pending) = @ratchet.decrypt_new_session(bob_s, ub_wire) assert_true(ub_rx_pending is None) assert_eq(ub_dec.length(), 2) // 5. Proposal 154 ECIES DatabaseLookup reply round-trip let rkey = Bytes::make(32, b'\x91') let rtag = Bytes::make(8, b'\x92') let dsm_blk = @ratchet.garlic_clove_block( Local, 99U, 1700000600U, Data(data=b"dsm-ls2"), ) let enc_dsm = @ratchet.encrypt_ecies_lookup_reply(rkey, rtag, [dsm_blk]) let dec_dsm = @ratchet.decrypt_ecies_lookup_reply(rkey, rtag, enc_dsm) assert_eq(dec_dsm, [dsm_blk]) // 6. Router-targeted Noise_N Garlic round-trip & auth check let router_s = @noise.KeyPair::from_private( @crypto.sha256(b"router-n-static"), ) let ephem_n = @noise.KeyPair::from_private(@crypto.sha256(b"router-n-ephem")) let wrong_s = @noise.KeyPair::from_private(@crypto.sha256(b"wrong-n-static")) let router_clove = @ratchet.garlic_clove_block( Local, 123U, 1700000600U, Data(data=b"lookup-payload"), ) let rg_wire = @ratchet.encrypt_router_garlic(router_s.public_key, ephem_n, [ DateTime(1700000200U), router_clove, ]) let rg_dec = @ratchet.decrypt_router_garlic(router_s, rg_wire) assert_eq(rg_dec, [DateTime(1700000200U), router_clove]) let mut wrong_failed = false try { let _ = @ratchet.decrypt_router_garlic(wrong_s, rg_wire) } catch { AuthenticationFailed => wrong_failed = true _ => () } assert_eq(wrong_failed, true) } ///| test "Proposal 144 ECIES Ratchet idle session expiry (>300s) and drop_session recovery" { let alice_s = @noise.KeyPair::from_private(@crypto.sha256(b"alice-idle-s")) let bob_s = @noise.KeyPair::from_private(@crypto.sha256(b"bob-idle-s")) let alice_mgr = @ratchet.RatchetSessionManager::new(alice_s) let bob_mgr = @ratchet.RatchetSessionManager::new(bob_s) let rand_a = det_rand_gen(b'\x21') let rand_b = det_rand_gen(b'\x61') // Initial NS -> NSR -> ES at t=1700000100 let c1 = @ratchet.garlic_clove_block( Local, 1U, 1700000600U, Data(data=b"init"), ) let w1 = alice_mgr.encrypt_to_peer( bob_s.public_key, [c1], 1700000100U, rand_a, ) let _ = bob_mgr.decrypt_incoming(w1) let c2 = @ratchet.garlic_clove_block( Local, 2U, 1700000600U, Data(data=b"nsr"), ) let w2 = bob_mgr.encrypt_to_peer( alice_s.public_key, [c2], 1700000101U, rand_b, ) let _ = alice_mgr.decrypt_incoming(w2) // Encrypting within window at t=1700000105 uses ExistingSession (accepted by bob_mgr) let c3 = @ratchet.garlic_clove_block( Local, 3U, 1700000600U, Data(data=b"es-alive"), ) let w3 = alice_mgr.encrypt_to_peer( bob_s.public_key, [c3], 1700000105U, rand_a, ) let (_, dec3) = bob_mgr.decrypt_incoming(w3) assert_eq(dec3, [c3]) // After >300s idle (t=1700000406 > 1700000105+300), alice initiates a fresh Bound NS // that even a restarted responder with empty session state can decrypt let bob_restarted = @ratchet.RatchetSessionManager::new(bob_s) let c4 = @ratchet.garlic_clove_block( Local, 4U, 1700000900U, Data(data=b"after-idle"), ) let w4 = alice_mgr.encrypt_to_peer( bob_s.public_key, [c4], 1700000406U, rand_a, ) let (from4, dec4) = bob_restarted.decrypt_incoming(w4) assert_eq(from4, Some(alice_s.public_key)) assert_eq(dec4.length(), 2) // DateTime + c4 // Responder replies with NSR so alice establishes new session let c5 = @ratchet.garlic_clove_block( Local, 5U, 1700000900U, Data(data=b"nsr2"), ) let w5 = bob_restarted.encrypt_to_peer( alice_s.public_key, [c5], 1700000407U, rand_b, ) let (_, dec5) = alice_mgr.decrypt_incoming(w5) assert_eq(dec5, [c5]) // Explicit drop_session also forces fresh Bound NS immediately alice_mgr.drop_session(bob_s.public_key) let bob_fresh = @ratchet.RatchetSessionManager::new(bob_s) let c6 = @ratchet.garlic_clove_block( Local, 6U, 1700000900U, Data(data=b"after-drop"), ) let w6 = alice_mgr.encrypt_to_peer( bob_s.public_key, [c6], 1700000408U, rand_a, ) let (from6, dec6) = bob_fresh.decrypt_incoming(w6) assert_eq(from6, Some(alice_s.public_key)) assert_eq(dec6.length(), 2) } ///| test "Proposal 144 multi-NSR replies, mark_send_stale preservation, and unconfirmed expiry" { let alice_s = @noise.KeyPair::from_private(@crypto.sha256(b"alice-multi-nsr")) let bob_s = @noise.KeyPair::from_private(@crypto.sha256(b"bob-multi-nsr")) let rand_a = det_rand_gen(b'\x31') let rand_b = det_rand_gen(b'\x71') // 1. Multi-NSR: two NSR replies (tag 0 + tag 1) to one Bound NS both decrypt cleanly // and do not clobber an already-confirmed ES session for sending/receiving. let alice_mgr = @ratchet.RatchetSessionManager::new(alice_s) let c_ns = @ratchet.garlic_clove_block( Local, 10U, 1700000900U, Data(data=b"ns-init"), ) let ns_wire = alice_mgr.encrypt_to_peer( bob_s.public_key, [c_ns], 1700000100U, rand_a, ) let (_, pending_opt) = @ratchet.decrypt_new_session(bob_s, ns_wire) let pending = match pending_opt { Some(p) => p None => fail("expected bound PendingNewSession") } let c_nsr0 = @ratchet.garlic_clove_block( Local, 11U, 1700000900U, Data(data=b"nsr-0"), ) let c_nsr1 = @ratchet.garlic_clove_block( Local, 12U, 1700000900U, Data(data=b"nsr-1"), ) let (nsr0, bob_sess0) = @ratchet.encrypt_new_session_reply( pending, @ratchet.generate_elligator_keypair(rand_b), [c_nsr0], ) let (nsr1, bob_sess1) = @ratchet.encrypt_new_session_reply( pending, @ratchet.generate_elligator_keypair(rand_b), [c_nsr1], ) let (from0, dec0) = alice_mgr.decrypt_incoming(nsr0, now_sec=1700000101U) assert_eq(from0, Some(bob_s.public_key)) assert_eq(dec0, [c_nsr0]) // Confirm session 0 by sending ES from Alice -> Bob and receiving ES from Bob -> Alice let c_es_ab = @ratchet.garlic_clove_block( Local, 13U, 1700000900U, Data(data=b"es-confirm"), ) let es_ab = alice_mgr.encrypt_to_peer( bob_s.public_key, [c_es_ab], 1700000102U, rand_a, ) assert_true(es_ab.length() < 96) let c_es_b0 = @ratchet.garlic_clove_block( Local, 14U, 1700000900U, Data(data=b"es-bob0"), ) let es_b0 = bob_sess0.encrypt_existing([c_es_b0]) let (_, dec_b0) = alice_mgr.decrypt_incoming(es_b0, now_sec=1700000103U) assert_eq(dec_b0, [c_es_b0]) assert_eq(alice_mgr.last_recv_sec(bob_s.public_key), 1700000103U) // Second NSR (tag 1) still decrypts cleanly without discarding session 0's recv window let (from1, dec1) = alice_mgr.decrypt_incoming(nsr1, now_sec=1700000104U) assert_eq(from1, Some(bob_s.public_key)) assert_eq(dec1, [c_nsr1]) assert_true( alice_mgr.has_active_session(bob_s.public_key, now_sec=1700000104U), ) let es_after_nsr1 = alice_mgr.encrypt_to_peer( bob_s.public_key, [c_es_ab], 1700000104U, rand_a, ) assert_true(es_after_nsr1.length() < 96) // Session 0 ES continues decrypting on Alice, and Session 1 can also receive ES let c_es_b0_2 = @ratchet.garlic_clove_block( Local, 15U, 1700000900U, Data(data=b"es-bob0-still-alive"), ) let (_, dec_b0_2) = alice_mgr.decrypt_incoming( bob_sess0.encrypt_existing([c_es_b0_2]), now_sec=1700000105U, ) assert_eq(dec_b0_2, [c_es_b0_2]) let c_es_b1 = @ratchet.garlic_clove_block( Local, 16U, 1700000900U, Data(data=b"es-bob1"), ) let (_, dec_b1) = alice_mgr.decrypt_incoming( bob_sess1.encrypt_existing([c_es_b1]), now_sec=1700000106U, ) assert_eq(dec_b1, [c_es_b1]) // 2. mark_send_stale forces a fresh Bound NS while keeping old recv_window & re-enabling on ES recv alice_mgr.mark_send_stale(bob_s.public_key) assert_true( !alice_mgr.has_active_session(bob_s.public_key, now_sec=1700000107U), ) let c_re_ns = @ratchet.garlic_clove_block( Local, 17U, 1700000900U, Data(data=b"re-ns-after-stale"), ) let re_ns_wire = alice_mgr.encrypt_to_peer( bob_s.public_key, [c_re_ns], 1700000107U, rand_a, ) assert_true(re_ns_wire.length() >= 96) let bob_fresh = @ratchet.RatchetSessionManager::new(bob_s) let (f_re, dec_re) = bob_fresh.decrypt_incoming( re_ns_wire, now_sec=1700000107U, ) assert_eq(f_re, Some(alice_s.public_key)) assert_eq(dec_re.length(), 2) // Receiving ES on a send-stale session still succeeds and re-enables it let c_stale_rx = @ratchet.garlic_clove_block( Local, 18U, 1700000900U, Data(data=b"bob1-re-enable"), ) let (_, dec_stale_rx) = alice_mgr.decrypt_incoming( bob_sess1.encrypt_existing([c_stale_rx]), now_sec=1700000108U, ) assert_eq(dec_stale_rx, [c_stale_rx]) assert_true( alice_mgr.has_active_session(bob_s.public_key, now_sec=1700000108U), ) // mark_send_stale also preserves in-flight outbound_pending NSR tags let a2 = @ratchet.RatchetSessionManager::new(alice_s) let b2 = @ratchet.RatchetSessionManager::new(bob_s) let w_a2 = a2.encrypt_to_peer(bob_s.public_key, [c_ns], 1700000200U, rand_a) let _ = b2.decrypt_incoming(w_a2, now_sec=1700000200U) a2.mark_send_stale(bob_s.public_key) let w_b2 = b2.encrypt_to_peer( alice_s.public_key, [c_nsr0], 1700000201U, rand_b, ) let (f_b2, dec_b2) = a2.decrypt_incoming(w_b2, now_sec=1700000201U) assert_eq(f_b2, Some(bob_s.public_key)) assert_eq(dec_b2, [c_nsr0]) // 3. Unconfirmed NSR expiry (>45s) forces a fresh Bound NS when no ES was sent/received assert_true(a2.has_active_session(bob_s.public_key, now_sec=1700000240U)) assert_true(!a2.has_active_session(bob_s.public_key, now_sec=1700000247U)) let w_expired_ns = a2.encrypt_to_peer( bob_s.public_key, [c_re_ns], 1700000247U, rand_a, ) let bob_clean = @ratchet.RatchetSessionManager::new(bob_s) let (f_exp, dec_exp) = bob_clean.decrypt_incoming( w_expired_ns, now_sec=1700000247U, ) assert_eq(f_exp, Some(alice_s.public_key)) assert_eq(dec_exp.length(), 2) } ///| test "Proposal 144 high-concurrency NSR survival, same-tag multi-NSR, and zombie ES expiry" { let alice_s = @noise.KeyPair::from_private(@crypto.sha256(b"alice-scale-s")) let peer0_s = @noise.KeyPair::from_private(@crypto.sha256(b"peer0-scale-s")) let rand_a = det_rand_gen(b'\x41') let rand_b = det_rand_gen(b'\x81') let alice_mgr = @ratchet.RatchetSessionManager::new(alice_s) // 1. High-concurrency outbound_pending survival: 40 peers (240 tags > old 128 cap) let c0 = @ratchet.garlic_clove_block( Local, 101U, 1700000900U, Data(data=b"p0-ns"), ) let w0 = alice_mgr.encrypt_to_peer( peer0_s.public_key, [c0], 1700000100U, rand_a, ) let (_, p0_opt) = @ratchet.decrypt_new_session(peer0_s, w0) let p0 = match p0_opt { Some(p) => p None => fail("expected bound PendingNewSession for peer0") } for i = 1; i <= 40; i = i + 1 { let ps = @noise.KeyPair::from_private( @crypto.sha256(@encoding.string_to_utf8_bytes("flood-peer-\{i}")), ) let ci = @ratchet.garlic_clove_block( Local, (200 + i).reinterpret_as_uint(), 1700000900U, Data(data=b"flood"), ) let _ = alice_mgr.encrypt_to_peer(ps.public_key, [ci], 1700000101U, rand_a) } let c0_nsr = @ratchet.garlic_clove_block( Local, 102U, 1700000900U, Data(data=b"p0-nsr"), ) let (w0_nsr, _) = @ratchet.encrypt_new_session_reply( p0, @ratchet.generate_elligator_keypair(rand_b), [c0_nsr], ) let (from0, dec0) = alice_mgr.decrypt_incoming(w0_nsr, now_sec=1700000105U) assert_eq(from0, Some(peer0_s.public_key)) assert_eq(dec0, [c0_nsr]) // 2. Same-tag multi-NSR tolerance: two distinct NSRs using the exact same 8B nsr_tag let bob_s = @noise.KeyPair::from_private(@crypto.sha256(b"bob-sametag-s")) let a_same = @ratchet.RatchetSessionManager::new(alice_s) let c_ns = @ratchet.garlic_clove_block( Local, 300U, 1700000900U, Data(data=b"same-tag-ns"), ) let ns_wire = a_same.encrypt_to_peer( bob_s.public_key, [c_ns], 1700000100U, rand_a, ) let (_, p_copy1_opt) = @ratchet.decrypt_new_session(bob_s, ns_wire) let (_, p_copy2_opt) = @ratchet.decrypt_new_session(bob_s, ns_wire) let p_copy1 = match p_copy1_opt { Some(p) => p None => fail("expected p_copy1") } let p_copy2 = match p_copy2_opt { Some(p) => p None => fail("expected p_copy2") } let c_nsr_a = @ratchet.garlic_clove_block( Local, 301U, 1700000900U, Data(data=b"same-tag-nsr-a"), ) let c_nsr_b = @ratchet.garlic_clove_block( Local, 302U, 1700000900U, Data(data=b"same-tag-nsr-b"), ) let (nsr_a, bob_sess_a) = @ratchet.encrypt_new_session_reply( p_copy1, @ratchet.generate_elligator_keypair(rand_b), [c_nsr_a], ) let (nsr_b, _bob_sess_b) = @ratchet.encrypt_new_session_reply( p_copy2, @ratchet.generate_elligator_keypair(rand_b), [c_nsr_b], ) assert_eq(nsr_a[:8].to_owned(), nsr_b[:8].to_owned()) let (fa, da) = a_same.decrypt_incoming(nsr_a, now_sec=1700000101U) let (fb, db) = a_same.decrypt_incoming(nsr_b, now_sec=1700000102U) assert_eq(fa, Some(bob_s.public_key)) assert_eq(fb, Some(bob_s.public_key)) assert_eq(da, [c_nsr_a]) assert_eq(db, [c_nsr_b]) let c_es = @ratchet.garlic_clove_block( Local, 303U, 1700000900U, Data(data=b"es-after-sametag"), ) let es_wire = a_same.encrypt_to_peer( bob_s.public_key, [c_es], 1700000103U, rand_a, ) assert_true(es_wire.length() < 96) let _ = c_es let _ = bob_sess_a // 3a. Zombie ES expiry: unconfirmed NSR -> unanswered ES sends expire >45s after last_recv_sec let a_z1 = @ratchet.RatchetSessionManager::new(alice_s) let b_z1 = @ratchet.RatchetSessionManager::new(bob_s) let wz1 = a_z1.encrypt_to_peer(bob_s.public_key, [c_ns], 100U, rand_a) let _ = b_z1.decrypt_incoming(wz1, now_sec=100U) let wz1_nsr = b_z1.encrypt_to_peer( alice_s.public_key, [c_nsr_a], 100U, rand_b, ) let _ = a_z1.decrypt_incoming(wz1_nsr, now_sec=100U) let _ = a_z1.encrypt_to_peer(bob_s.public_key, [c_es], 110U, rand_a) let _ = a_z1.encrypt_to_peer(bob_s.public_key, [c_es], 130U, rand_a) assert_true(a_z1.has_active_session(bob_s.public_key, now_sec=140U)) assert_true(!a_z1.has_active_session(bob_s.public_key, now_sec=146U)) let wz1_re = a_z1.encrypt_to_peer(bob_s.public_key, [c_es], 146U, rand_a) assert_true(wz1_re.length() >= 96) let b_z1_fresh = @ratchet.RatchetSessionManager::new(bob_s) let (fz1, dz1) = b_z1_fresh.decrypt_incoming(wz1_re, now_sec=146U) assert_eq(fz1, Some(alice_s.public_key)) assert_eq(dz1.length(), 2) // 3b. Zombie ES expiry: confirmed ES -> unanswered ES sends expire >180s after last_recv_sec let a_z2 = @ratchet.RatchetSessionManager::new(alice_s) let b_z2 = @ratchet.RatchetSessionManager::new(bob_s) let wz2 = a_z2.encrypt_to_peer(bob_s.public_key, [c_ns], 100U, rand_a) let _ = b_z2.decrypt_incoming(wz2, now_sec=100U) let wz2_nsr = b_z2.encrypt_to_peer( alice_s.public_key, [c_nsr_a], 101U, rand_b, ) let _ = a_z2.decrypt_incoming(wz2_nsr, now_sec=101U) let wz2_es_ab = a_z2.encrypt_to_peer(bob_s.public_key, [c_es], 102U, rand_a) let _ = b_z2.decrypt_incoming(wz2_es_ab, now_sec=103U) let wz2_es_ba = b_z2.encrypt_to_peer(alice_s.public_key, [c_es], 104U, rand_b) let _ = a_z2.decrypt_incoming(wz2_es_ba, now_sec=105U) let _ = a_z2.encrypt_to_peer(bob_s.public_key, [c_es], 200U, rand_a) let _ = a_z2.encrypt_to_peer(bob_s.public_key, [c_es], 260U, rand_a) assert_true(a_z2.has_active_session(bob_s.public_key, now_sec=280U)) assert_true(!a_z2.has_active_session(bob_s.public_key, now_sec=286U)) let wz2_re = a_z2.encrypt_to_peer(bob_s.public_key, [c_es], 286U, rand_a) assert_true(wz2_re.length() >= 96) let b_z2_fresh = @ratchet.RatchetSessionManager::new(bob_s) let (fz2, dz2) = b_z2_fresh.decrypt_incoming(wz2_re, now_sec=286U) assert_eq(fz2, Some(alice_s.public_key)) assert_eq(dz2.length(), 2) }