///| 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]) }