CHG: remade selftest structure

CHG: removed debugstatements
This commit is contained in:
iceman1001 2015-05-31 22:33:12 +02:00
commit af3b8b2dc6

View file

@ -14,6 +14,7 @@ This is a script to dump and decrypt the data of a specific type of Mifare Mini
Arguments: Arguments:
-h : this help -h : this help
-t : selftest
-k <key> : Mifare Key A. -k <key> : Mifare Key A.
]] ]]
@ -364,14 +365,22 @@ end
local function selftest() local function selftest()
local testdata = '000F42430D0A14000001D11F'..'5D738517' local testdata = '000F42430D0A14000001D11F'..'5D738517'
local chksum = getChecksum(testdata) local chksum = getChecksum(testdata)
local calc = calculateChecksum( utils.ConvertHexToBytes(testdata:sub(1,24))) local calc = calculateChecksum( utils.ConvertHexToBytes(testdata:sub(1,24)))
print ('TESTDATA :: '..testdata) local isValid = false
print ('DATA :: '..testdata:sub(1,24)) local validStr = "FAIL"
print (('CHKSUM :: %X'):format(chksum)) if calc == chksum then
print (('CHKSUM CALC :: %X'):format(calc)) isValid = true
print ('UPDATE CHKSUM :: '..updateChecksum(testdata)) validStr = "OK"
end
local newtestdata = updateChecksum(testdata)
local revalidated = "FAIL"
if newtestdata == testdata then
revalidated = "OK"
end
print ('TESTDATA :: '..testdata)
print ('DATA :: '..testdata:sub(1,24))
print (('VALID CHKSUM :: %s'):format(validStr ))
print (('UPDATE CHKSUM :: %s'):format(revalidated))
end end
--- ---
-- The main entry point -- The main entry point
@ -382,18 +391,15 @@ function main(args)
local cmd, result, err, blockNo, keyA local cmd, result, err, blockNo, keyA
local blocks = {} local blocks = {}
local decryptkey = '' local magic = ''
-- Read the parameters -- Read the parameters
for o, a in getopt.getopt(args, 'hk:') do for o, a in getopt.getopt(args, 'hk:t') do
if o == "h" then help() return end if o == "h" then help() return end
if o == "k" then keyA = a end if o == "k" then keyA = a end
if o == "t" then return selftest() end
end end
selftest()
local tst2 = '00100100030209094312356432324E34B79A349B'
-- validate input args. -- validate input args.
keyA = keyA or '6dd747e86975' keyA = keyA or '6dd747e86975'
if #(keyA) ~= 12 then if #(keyA) ~= 12 then
@ -410,15 +416,7 @@ function main(args)
if not result then if not result then
return oops(err) return oops(err)
end end
core.clearCommandBuffer() core.clearCommandBuffer()
print(result.uid, keyA)
local my = result.uid
if 1 == 1 then
return
end
-- Show tag info -- Show tag info
print((' Found tag %s'):format(result.name)) print((' Found tag %s'):format(result.name))
@ -426,11 +424,10 @@ function main(args)
local longrandom = RANDOM..result.uid local longrandom = RANDOM..result.uid
local res = utils.Sha1Hex(longrandom) local res = utils.Sha1Hex(longrandom)
res = utils.ConvertBytesToHex(utils.ConvertAsciiToBytes(res:sub(1,16))) res = utils.ConvertBytesToHex(utils.ConvertAsciiToBytes(res:sub(1,16)))
decryptkey = utils.SwapEndiannessStr(res:sub(1,8) , 32) magic = utils.SwapEndiannessStr(res:sub(1,8) , 32)
decryptkey = decryptkey..utils.SwapEndiannessStr( res:sub(9,16),32) magic = magic..utils.SwapEndiannessStr( res:sub(9,16),32)
decryptkey = decryptkey..utils.SwapEndiannessStr( res:sub(17,24),32) magic = magic..utils.SwapEndiannessStr( res:sub(17,24),32)
decryptkey = decryptkey..utils.SwapEndiannessStr( res:sub(25,32),32) magic = magic..utils.SwapEndiannessStr( res:sub(25,32),32)
print('Decrypt key::',decryptkey)
print('Reading card data') print('Reading card data')
print('Raw','Decrypted') print('Raw','Decrypted')
for blockNo = 0, numBlocks-1, 1 do for blockNo = 0, numBlocks-1, 1 do
@ -452,7 +449,7 @@ function main(args)
if string.find(blockdata, '^0+$') then if string.find(blockdata, '^0+$') then
print(blockdata, blockdata) print(blockdata, blockdata)
else else
local aes = core.aes128_decrypt_ecb(decryptkey, blockdata) local aes = core.aes128_decrypt_ecb(magic, blockdata)
local bytes = utils.ConvertAsciiToBytes(aes) local bytes = utils.ConvertAsciiToBytes(aes)
local hex = utils.ConvertBytesToHex(bytes) local hex = utils.ConvertBytesToHex(bytes)
print(blockdata , hex) print(blockdata , hex)
@ -465,9 +462,6 @@ function main(args)
print(sectortrailer, sectortrailer, blockdata:sub(13,20)) print(sectortrailer, sectortrailer, blockdata:sub(13,20))
end end
end end
-- checksum fyra sista bytes i varje rad. (kanske inte för s0)
-- s0b1,s1b0,s2b0,s3b0
--
end end
main(args) main(args)