fix one too small
[RRG-proxmark3.git] / client / lualibs / read14a.lua
blob782fca6d50502d8dcbdf2b0c2d90fc7f73f8db9a
1 --[[
2 This is a library to read 14443a tags. It can be used something like this
4 local reader = require('read14a')
5 result, err = reader.read14443a()
6 if not result then
7 print(err)
8 return
9 end
10 print(result.name)
12 --]]
13 -- Loads the commands-library
14 local taglib = require('taglib')
15 local cmds = require('commands')
17 -- Shouldn't take longer than 2 seconds
18 local TIMEOUT = 2000
20 local ISO14A_COMMAND = {
21 ISO14A_CONNECT = 1,
22 ISO14A_NO_DISCONNECT = 2,
23 ISO14A_APDU = 4,
24 ISO14A_RAW = 8,
25 ISO14A_REQUEST_TRIGGER = 0x10,
26 ISO14A_APPEND_CRC = 0x20,
27 ISO14A_SET_TIMEOUT = 0x40,
28 ISO14A_NO_SELECT = 0x80,
29 ISO14A_TOPAZMODE = 0x100,
30 ISO14A_NO_RATS = 0x200,
31 ISO14A_SEND_CHAINING = 0x400,
32 ISO14A_USE_ECP = 0x800,
33 ISO14A_USE_MAGSAFE = 0x1000,
36 local ISO14443a_TYPES = {}
37 ISO14443a_TYPES[0x00] = "NXP MIFARE Ultralight | Ultralight C | NTAG"
38 ISO14443a_TYPES[0x01] = "NXP MIFARE TNP3xxx Activision Game Appliance"
39 ISO14443a_TYPES[0x04] = "NXP MIFARE (various !DESFire !DESFire EV1)"
40 ISO14443a_TYPES[0x08] = "NXP MIFARE CLASSIC 1k | Plus 2k"
41 ISO14443a_TYPES[0x09] = "NXP MIFARE Mini 0.3k"
42 ISO14443a_TYPES[0x0A] = "FM11RF005SH (Shanghai Metro)"
43 ISO14443a_TYPES[0x10] = "NXP MIFARE Plus 2k"
44 ISO14443a_TYPES[0x11] = "NXP MIFARE Plus 4k"
45 ISO14443a_TYPES[0x18] = "NXP MIFARE Classic 4k | Plus 4k"
46 ISO14443a_TYPES[0x20] = "NXP MIFARE DESFire 4k | DESFire EV1 2k/4k/8k | Plus 2k/4k | JCOP 31/41"
47 ISO14443a_TYPES[0x24] = "NXP MIFARE DESFire | DESFire EV1"
48 ISO14443a_TYPES[0x28] = "JCOP31 or JCOP41 v2.3.1"
49 ISO14443a_TYPES[0x38] = "Nokia 6212 or 6131 MIFARE CLASSIC 4K"
50 ISO14443a_TYPES[0x88] = "Infineon MIFARE CLASSIC 1K"
51 ISO14443a_TYPES[0x98] = "Gemplus MPCOS"
53 local function tostring_14443a(sak)
54 return ISO14443a_TYPES[sak] or ("Unknown (SAK=%x)"):format(sak)
55 end
57 local function parse14443a(data)
58 --[[
59 typedef struct {
60 uint8_t uid[10];
61 uint8_t uidlen;
62 uint8_t atqa[2];
63 uint8_t sak;
64 uint8_t ats_len;
65 uint8_t ats[256];
66 } PACKED iso14a_card_select_t;
67 --]]
69 local count, uid, uidlen, atqa, sak, ats_len, ats = bin.unpack('H10CH2CCH', data)
70 uid = uid:sub(1, 2 * uidlen)
71 local man_byte = tonumber(uid:sub(1,2), 16)
73 return {
74 uid = uid,
75 atqa = atqa,
76 sak = sak,
77 name = tostring_14443a(sak),
78 data = data,
79 manufacturer = taglib.lookupManufacturer(man_byte),
80 ats = ats
82 end
84 -- This function does a connect and retrieves som einfo
85 -- @param dont_disconnect - if true, does not disable the field
86 -- @return if successful: an table containing card info
87 -- @return if unsuccessful : nil, error
88 local function read14443a(dont_disconnect, no_rats)
89 local command, result, info, err, data
91 command = Command:newMIX{
92 cmd = cmds.CMD_HF_ISO14443A_READER,
93 arg1 = ISO14A_COMMAND.ISO14A_CONNECT
96 if dont_disconnect then
97 command.arg1 = command.arg1 + ISO14A_COMMAND.ISO14A_NO_DISCONNECT
98 end
99 if no_rats then
100 command.arg1 = command.arg1 + ISO14A_COMMAND.ISO14A_NO_RATS
103 local result, err = command:sendMIX()
104 if result then
105 local count, cmd, arg1, arg2, arg3 = bin.unpack('LLLL',result)
106 if arg1 == 0 then
107 return nil, 'iso14443a card select failed'
109 data = string.sub(result, count)
110 info = parse14443a(data)
111 else
112 err = 'No response from card'
115 if err then
116 print(err)
117 return nil, err
119 return info, nil
123 -- Waits for a mifare card to be placed within the vicinity of the reader.
124 -- @return if successful: an table containing card info
125 -- @return if unsuccessful : nil, error
126 local function waitFor14443a()
127 print('Waiting for card... press <Enter> to quit')
128 while not core.kbd_enter_pressed() do
129 res, err = read14443a()
130 if res then return res end
131 -- err means that there was no response from card
133 return nil, 'Aborted by user'
136 -- Sends an instruction to do nothing, only disconnect
137 local function disconnect14443a()
138 local c = Command:newMIX{cmd = cmds.CMD_HF_ISO14443A_READER}
139 -- We can ignore the response here, no ACK is returned for this command
140 -- Check /armsrc/iso14443a.c, ReaderIso14443a() for details
141 return c:sendMIX(true)
144 local library = {
145 read = read14443a,
146 waitFor14443a = waitFor14443a,
147 parse14443a = parse14443a,
148 disconnect = disconnect14443a,
149 ISO14A_COMMAND = ISO14A_COMMAND,
152 return library