Optimized inbound IFAC handling

This commit is contained in:
Mark Qvist 2026-08-23 21:57:22 +02:00
parent 602085a1cd
commit e806ae5837
No known key found for this signature in database

View file

@ -1651,6 +1651,36 @@ class Transport:
RNS.log(f"Error while processing inbound on {interface}: {e}", RNS.LOG_ERROR)
RNS.trace_exception(e)
@staticmethod
def handle_ifac(raw, interface):
# Extract IFAC
ifac_size = interface.ifac_size
ifac = raw[2:2 + ifac_size]
# Generate mask
mask = RNS.Cryptography.hkdf(length=len(raw), derive_from=ifac,
salt=interface.ifac_key, context=None)
# Unmask header bytes and payload with a single bignum XOR,
# leaving the IFAC tag itself untouched, then drop the IFAC
# flag and strip to actual frame payload.
n = len(raw)
x = int.from_bytes(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))
unmasked = x.to_bytes(n, "big")
# Unset IFAC flag and re-assemble packet
new_raw = bytes([unmasked[0] & 0x7f, unmasked[1]]) + unmasked[2 + ifac_size:]
# Calculate expected IFAC
expected_ifac = interface.ifac_identity.sign(new_raw)[-ifac_size:]
# Check it
if ifac == expected_ifac: return True, new_raw
else:
interface.ifac_violation("Invalid IFAC on packet")
return False, None
@staticmethod
def handle_ifac_legacy(raw, interface):
# Extract IFAC