KMBox

@rebind / kmbox-netv0.2.4Public

Let programs written for KMBox Net, Ferrum, or DHZBox send keyboard and mouse input through Rebind Link over your network.

Files

xxtea.luau1.5 KB

--[[ KMBox Net packet decryption

  Encrypted commands arrive as 128 bytes: 32 little-endian words enciphered
  with XXTEA, 6 fixed rounds. The key is the device code's four bytes in
  hex-string order followed by 12 zero bytes, so only its first word is set.
  This is the inverse of my_encrypt in the vendor's NetConfig/my_enc.cpp.
]]

local DELTA = 0x9E3779B9
local ROUNDS = 6
local WORDS = 32
local MOD = 2 ^ 32
local FORMAT = "<" .. string.rep("I4", WORDS)

local function mx(sum, y, z, p, e, key)
  local a = bit32.bxor(bit32.rshift(z, 5), bit32.lshift(y, 2))
    + bit32.bxor(bit32.rshift(y, 3), bit32.lshift(z, 4))
  local b = bit32.bxor(sum, y)
    + bit32.bxor(key[bit32.bxor(bit32.band(p, 3), e) + 1], z)
  return bit32.bxor(a % MOD, b % MOD)
end

-- key word for an eight-hex-digit device code held as a number
local function key_for(code)
  return { string.unpack("<I4", string.pack(">I4", code)), 0, 0, 0 }
end

-- decrypt one 128-byte packet with the key from key_for
local function decrypt(data, key)
  local v = { string.unpack(FORMAT, data) }
  local sum = (ROUNDS * DELTA) % MOD
  local y = v[1]
  for _ = 1, ROUNDS do
    local e = bit32.band(bit32.rshift(sum, 2), 3)
    for p = WORDS - 1, 1, -1 do
      v[p + 1] = (v[p + 1] - mx(sum, y, v[p], p, e, key)) % MOD
      y = v[p + 1]
    end
    v[1] = (v[1] - mx(sum, y, v[WORDS], 0, e, key)) % MOD
    y = v[1]
    sum = (sum - DELTA) % MOD
  end
  return string.pack(FORMAT, table.unpack(v, 1, WORDS))
end

return { key_for = key_for, decrypt = decrypt }

5 files, 27.8 KB.