1 local cmds = require('commands')
2 local getopt = require('getopt')
3 local lib14b = require('read14b')
4 local utils = require('utils')
6 example = "script runs 14b raw commands to query a CAPLYPSO tag"
7 author = "Iceman, 2016"
10 This is a script to communicate with a CALYSPO / 14443b tag using the '14b raw' commands
15 script run f -b 11223344
20 # 1. Connect and don't disconnect
22 # 2. Send mf auth, read response
30 This script communicates with /armsrc/iso14443b.c,
31 Check there for details about data format and how commands are interpreted on the
37 local function calypso_switch_on_field()
38 local flags = lib14b.ISO14B_COMMAND.ISO14B_CONNECT
39 local c = Command:new{cmd = cmds.CMD_ISO_14443B_COMMAND, arg1 = flags}
40 return lib14b.sendToDevice(c, true)
43 -- Disconnect (poweroff) the antenna forcing a disconnect of a 14b tag.
44 local function calypso_switch_off_field()
45 local flags = lib14b.ISO14B_COMMAND.ISO14B_DISCONNECT
46 local c = Command:new{cmd = cmds.CMD_ISO_14443B_COMMAND, arg1 = flags}
47 return lib14b.sendToDevice(c, true)
50 local function calypso_parse(result)
51 local r = Command.parse(result)
52 local len = r.arg2 * 2
53 r.data = string.sub(r.data, 0, len);
60 -- A debug printout-function
61 local function dbg(args)
67 -- This is only meant to be used when errors occur
68 local function oops(err)
70 calypso_switch_off_field()
77 print("Example usage")
81 -- helper function, give current count of items in lua-table.
82 local function tablelen(T)
84 for _ in pairs(T) do count = count + 1 end
88 -- helper function, gives a sorted table from table t,
89 -- order can be a seperate sorting-order function.
90 local function spairs(t, order)
93 for k in pairs(t) do keys[#keys+1] = k end
95 -- if order function given, sort by it by passing the table and keys a, b,
96 -- otherwise just sort the keys
98 table.sort(keys, function(a,b) return order(t, a, b) end)
103 -- return the iterator function
108 return keys[i], t[keys[i]]
113 -- Sends a usbpackage , "hf 14b raw"
114 -- if it reads the response, it converts it to a lua object "Command" first and the Data is cut to correct length.
115 local function calypso_send_cmd_raw(data, ignoreresponse )
117 local command, flags, result, err
118 flags = lib14b.ISO14B_COMMAND.ISO14B_RAW +
119 lib14b.ISO14B_COMMAND.ISO14B_APPEND_CRC
123 command = Command:new{cmd = cmds.CMD_ISO_14443B_COMMAND,
125 arg2 = #data/2, -- LEN of data, half the length of the ASCII-string hex string
127 data = data} -- data bytes (commands etc)
128 result, err = lib14b.sendToDevice(command, false)
130 if ignoreresponse then return response, err end
133 local r = calypso_parse(result)
139 -- calypso_card_num : Reads card number from ATR and
140 -- writes it in the tree in decimal format.
141 local function calypso_card_num(card)
142 if not card then return end
143 local card_num = tonumber( card.uid:sub(1,8),16 )
144 print('Card UID', card.uid)
145 print('Card Number', card_num)
148 -- analyse CALYPSO apdu status bytes.
149 local function calypso_apdu_status(apdu)
151 -- next two is APDU status bytes.
154 local sw = apdu:sub( #apdu-7, #apdu-4)
159 print ('SW', sw, mess )
163 local _calypso_cmds = {
164 ["01.Select ICC file"] = '02 94 a4 08 00 04 3f 00 00 02',
165 ["02.ICC"] = '02 94 b2 01 04 1d',
166 ["03.Select EnvHol file"]= '02 94 a4 08 00 04 20 00 20 01',
167 ["04.EnvHol1"] = '02 94 b2 01 04 1d',
168 ["05.Select EvLog file"] = '02 94 a4 08 00 04 20 00 20 10',
169 ["06.EvLog1"] = '06 00b2 0104 1d',
170 ["07.EvLog2"] = '06 00b2 0204 1d',
171 ["08.EvLog3"] = '06 00b2 0304 1d',
172 ["09.Select ConList file"]= '42 01 04 0a 00a4 0800 04 2000 2050',
173 ["10.ConList"] = '42 01 06 06 00b2 0104 1d',
174 ["11.Select Contra file"]= '42 01 08 0a 00a4 0800 04 2000 2020',
175 ["12.Contra1"] = '42 01 0a 06 00b2 0104 1d',
176 ["13.Contra2"] = '42 01 0c 06 00b2 0204 1d',
177 ["14.Contra3"] = '42 01 0e 06 00b2 0304 1d',
178 ["15.Contra4"] = '42 01 00 06 00b2 0404 1d',
179 ["16.Select Counter file"]= '42 01 02 0a 00a4 0800 04 2000 2069',
180 ["17.Counter"] = '42 01 04 06 00b2 0104 1d',
181 ["18.Select SpecEv file"]= '42 01 06 0a 00a4 08 0004 2000 2040',
182 ["19.SpecEv1"] = '42 01 08 06 00b2 0104 1d',
186 -- The main entry point
189 local data, apdu, flags, uid, cid, result, err, card
190 -- Read the parameters
191 for o, a in getopt.getopt(args, 'h') do
192 if o == "h" then return help() end
195 calypso_switch_on_field()
198 card, err = lib14b.waitFor14443b()
199 if not card then return oops(err) end
201 calypso_card_num(card)
215 apdu = '0a 00 94 a4 08 00 04 3f 00 00 02' --select ICC file
219 --result, err = calypso_send_cmd_raw('0294a40800043f000002',false) --select ICC file
220 for i, apdu in spairs(_calypso_cmds) do
222 apdu = apdu:gsub("%s+","")
223 result, err = calypso_send_cmd_raw(apdu , false)
225 calypso_apdu_status(result.data)
226 print('<<', result.data )
228 print('<< no answer')
231 calypso_switch_off_field()
234 -- a simple selftest function, tries to convert
237 dbg("Performing test")
240 -- Flip the switch here to perform a sanity check.
241 -- It read a nonce in two different ways, as specified in the usage-section
242 if "--test"==args then