106 lines
3.4 KiB
Python
106 lines
3.4 KiB
Python
"""ring code: a static qr in the middle (opens the page, anchors geometry) plus rotating rings of dashes
|
|
that carry the time token. layout and bit order must match static/js/ringcode.js exactly.
|
|
|
|
payload = slot mod 2^16 (2 bytes) + first 4 bytes of the token hmac, reed-solomon(12, 6) over gf(256),
|
|
whitened, spread over 3 data rings. the inner ring is a fixed sync pattern used to find the rotation."""
|
|
import hmac
|
|
|
|
SYNC = "010001100101001000111111" # 24 sectors, min distance 12 to any of its rotations
|
|
RING_SECTORS = (24, 28, 32, 36) # ring 0 is sync, 1..3 carry 96 data bits
|
|
NSYM = 6 # rs parity bytes, fixes up to 3 bad bytes
|
|
SLOT_BITS = 16
|
|
|
|
_EXP = [0] * 512
|
|
_LOG = [0] * 256
|
|
_x = 1
|
|
for _i in range(255):
|
|
_EXP[_i] = _x
|
|
_LOG[_x] = _i
|
|
_x <<= 1
|
|
if _x & 0x100:
|
|
_x ^= 0x11D
|
|
for _i in range(255, 512):
|
|
_EXP[_i] = _EXP[_i - 255]
|
|
|
|
|
|
def _mul(a, b):
|
|
return 0 if a == 0 or b == 0 else _EXP[_LOG[a] + _LOG[b]]
|
|
|
|
|
|
def _generator(nsym):
|
|
g = [1]
|
|
for i in range(nsym):
|
|
# g *= (x - a^i), coefficients highest degree first
|
|
ng = [0] * (len(g) + 1)
|
|
for j, c in enumerate(g):
|
|
ng[j] ^= c
|
|
ng[j + 1] ^= _mul(c, _EXP[i])
|
|
g = ng
|
|
return g
|
|
|
|
|
|
_GEN = _generator(NSYM)
|
|
|
|
|
|
def rs_encode(msg: bytes) -> bytes:
|
|
rem = [0] * NSYM
|
|
for byte in msg:
|
|
f = byte ^ rem[0]
|
|
rem = rem[1:] + [0]
|
|
if f:
|
|
for j in range(NSYM):
|
|
rem[j] ^= _mul(_GEN[j + 1], f)
|
|
return bytes(msg) + bytes(rem)
|
|
|
|
|
|
def whitening(n):
|
|
"""16-bit galois lfsr, same sequence as the js side"""
|
|
lfsr, out = 0xACE1, []
|
|
for _ in range(n):
|
|
bit = lfsr & 1
|
|
lfsr >>= 1
|
|
if bit:
|
|
lfsr ^= 0xB400
|
|
out.append(bit)
|
|
return out
|
|
|
|
|
|
def mac32(secret: bytes, poll_id: str, interval_ms: int, slot: int) -> bytes:
|
|
msg = f"{poll_id}:{interval_ms}:{slot}".encode()
|
|
return hmac.new(secret, msg, "sha256").digest()[:4]
|
|
|
|
|
|
def payload(secret: bytes, poll_id: str, interval_ms: int, slot: int) -> bytes:
|
|
return (slot & 0xFFFF).to_bytes(2, "big") + mac32(secret, poll_id, interval_ms, slot)
|
|
|
|
|
|
def data_bits(codeword: bytes) -> str:
|
|
bits = [(b >> (7 - k)) & 1 for b in codeword for k in range(8)]
|
|
return "".join(str(b ^ w) for b, w in zip(bits, whitening(len(bits))))
|
|
|
|
|
|
def render_bits(secret: bytes, poll_id: str, interval_ms: int, slot: int) -> str:
|
|
"""what the projector draws: sync ring then the 96 data sectors, ring by ring"""
|
|
return SYNC + data_bits(rs_encode(payload(secret, poll_id, interval_ms, slot)))
|
|
|
|
|
|
def token(secret: bytes, poll_id: str, interval_ms: int, slot: int) -> str:
|
|
"""the compact token the phone sends after decoding: r<slot16>.<mac hex>"""
|
|
return f"r{slot & 0xFFFF}.{mac32(secret, poll_id, interval_ms, slot).hex()}"
|
|
|
|
|
|
def resolve(secret: bytes, poll_id: str, interval_ms: int, tok: str, now_slot: int, back: int) -> int | None:
|
|
"""full slot of a ring token if it matches one of the last `back` slots, else None"""
|
|
try:
|
|
s16, mac = tok[1:].split(".", 1)
|
|
s16 = int(s16)
|
|
given = bytes.fromhex(mac)
|
|
except (ValueError, IndexError):
|
|
return None
|
|
if len(given) != 4 or not 0 <= s16 <= 0xFFFF:
|
|
return None
|
|
for slot in range(now_slot, now_slot - back - 1, -1):
|
|
if slot & 0xFFFF == s16:
|
|
return slot if hmac.compare_digest(given, mac32(secret, poll_id, interval_ms, slot)) else None
|
|
return None
|