From 7347034f9a531ea08e683b0b2d0e9e76bd3e71e1 Mon Sep 17 00:00:00 2001 From: Mark Qvist Date: Sun, 23 Aug 2026 22:27:53 +0200 Subject: [PATCH] Optimized outbound IFAC handling --- RNS/Transport.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/RNS/Transport.py b/RNS/Transport.py index 3143047d..3160985e 100755 --- a/RNS/Transport.py +++ b/RNS/Transport.py @@ -1257,6 +1257,27 @@ class Transport: def handle_outgoing_announces(outgoing): for packet in sorted(outgoing, key=lambda p: p.hops): packet.send() + @staticmethod + def handle_outgoing_ifac(interface, raw): + # Calculate packet access code + ifac_size = interface.ifac_size + ifac = interface.ifac_identity.sign(raw)[-ifac_size:] + + # Generate mask + mask = RNS.Cryptography.hkdf(length=len(raw) + ifac_size, derive_from=ifac, + salt=interface.ifac_key, context=None) + + # Set IFAC flag and assemble new payload with IFAC + new_raw = bytes([raw[0] | 0x80, raw[1]]) + ifac + raw[2:] + + # Mask header bytes and payload with a single bignum XOR, leaving + # the IFAC itself untouched, then ensure the IFAC flag is set. + n = len(new_raw) + x = int.from_bytes(new_raw, "big") ^ int.from_bytes(mask, "big") + if ifac_size: x ^= int.from_bytes(mask[2:2 + ifac_size], "big") << (8 * (n - 2 - ifac_size)) + x |= 1 << (8 * n - 1) + return x.to_bytes(n, "big") + @staticmethod def handle_outgoing_ifac_legacy(interface, raw): # Calculate packet access code