1 local bin = require('bin')
 
   2 local getopt = require('getopt')
 
   3 local utils =  require('utils')
 
   9          script run calc_ev1_it -u 11223344556677
 
  12 usage = "script run calc_ev1_it -u <uid> "
 
  19 -- A debug printout-function
 
  21     if type(args) == "table" then
 
  32 -- This is only meant to be used when errors occur
 
  41         print("Example usage")
 
  47         print( string.rep('--',20) )
 
  48         print( string.rep('--',20) )
 
  54     --[[ position, 4byte xor
 
  90 local function findEntryByUid( uid )
 
  92         -- xor UID4,UID5,UID6,UID7
 
  94         local pos = (bxor(bxor(bxor(uid[4],uid[5]), uid[6]),uid[7])) % 32
 
  96         -- convert to hexstring
 
  97         pos = string.format('%02X', pos)
 
  99     for k, v in pairs(_xortable) do
 
 100                 if ( v[1]  == pos ) then
 
 101                         return utils.ConvertHexToBytes(v[2])
 
 107 local function main(args)
 
 109         print( string.rep('--',20) )
 
 110         print( string.rep('--',20) )    
 
 114         local uid = '04111211121110'
 
 116         -- Arguments for the script
 
 117         for o, a in getopt.getopt(args, 'hu:') do
 
 118                 if o == "h" then return help() end              
 
 119                 if o == "u" then uid = a end            
 
 123         if uid == nil then return oops('empty uid string') end
 
 124         if #uid == 0 then return oops('empty uid string') end
 
 125         if #uid ~= 14 then return oops('uid wrong length. Should be 7 hex bytes') end
 
 127         local uidbytes = utils.ConvertHexToBytes(uid)
 
 129     local entry = findEntryByUid(uidbytes)
 
 130         if entry == nil then return oops("Can't find a xor entry") end
 
 133         --      PWD0  =  T0 xor B xor C xor D
 
 134         --      PWD1  =  T1 xor A xor C xor E 
 
 135         --      PWD2  =  T2 xor A xor B xor F 
 
 138     local pwd0 = bxor( bxor( bxor( entry[1], uidbytes[2]), uidbytes[3]), uidbytes[4])
 
 139         local pwd1 = bxor( bxor( bxor( entry[2], uidbytes[1]), uidbytes[3]), uidbytes[5])
 
 140         local pwd2 = bxor( bxor( bxor( entry[3], uidbytes[1]), uidbytes[2]), uidbytes[6])
 
 141         local pwd3 = bxor( entry[4], uidbytes[7])
 
 144         print(string.format('PWD | %02X%02X%02X%02X', pwd0, pwd1, pwd2, pwd3))