ntag i2c 2k - fast write
[RRG-proxmark3.git] / client / luascripts / hf_14b_calypso.lua
bloba3afa4e965ec78da5918006fbcc16402601206f8
1 local cmds = require('commands')
2 local getopt = require('getopt')
3 local lib14b = require('read14b')
4 local utils = require('utils')
5 local iso7816 = require('7816_error')
6 local ansicolors = require('ansicolors')
8 copyright = ''
9 author = 'Iceman'
10 version = 'v1.0.5'
11 desc = [[
12 This is a script to communicate with a CALYSPO / 14443b tag using the '14b raw' commands
14 example = [[
15 script run hf_14b_calypso -b 11223344
18 usage = [[
19 script run hf_14b_calypso -h -b
21 arguments = [[
22 h this helptext
23 b raw bytes to send
26 --[[
27 This script communicates with /armsrc/iso14443b.c,
28 Check there for details about data format and how commands are interpreted on the
29 device-side.
32 local function calypso_parse(result)
33 if result.Oldarg0 >= 0 then
34 local len = result.Oldarg0 * 2
35 if len > 0 then
36 local d = string.sub(result.Data, 0, len);
37 return d, nil
38 end
39 end
40 return nil, "calypso_parse failed"
41 end
42 ---
43 -- A debug printout-function
44 local function dbg(args)
45 if not DEBUG then return end
46 if type(args) == 'table' then
47 local i = 1
48 while args[i] do
49 dbg(args[i])
50 i = i+1
51 end
52 else
53 print('###', args)
54 end
55 end
56 ---
57 -- This is only meant to be used when errors occur
58 local function oops(err)
59 print('ERROR: ', err)
60 lib14b.disconnect()
61 return nil, err
62 end
63 ---
64 -- Usage help
65 local function help()
66 print(copyright)
67 print(author)
68 print(version)
69 print(desc)
70 print(ansicolors.cyan..'Usage'..ansicolors.reset)
71 print(usage)
72 print(ansicolors.cyan..'Arguments'..ansicolors.reset)
73 print(arguments)
74 print(ansicolors.cyan..'Example usage'..ansicolors.reset)
75 print(example)
76 end
78 -- helper function, give current count of items in lua-table.
79 local function tablelen(T)
80 local count = 0
81 for _ in pairs(T) do count = count + 1 end
82 return count
83 end
84 ---
85 -- helper function, gives a sorted table from table t,
86 -- order can be a seperate sorting-order function.
87 local function spairs(t, order)
88 -- collect the keys
89 local keys = {}
90 for k in pairs(t) do keys[#keys+1] = k end
92 -- if order function given, sort by it by passing the table and keys a, b,
93 -- otherwise just sort the keys
94 if order then
95 table.sort(keys, function(a,b) return order(t, a, b) end)
96 else
97 table.sort(keys)
98 end
100 -- return the iterator function
101 local i = 0
102 return function()
103 i = i + 1
104 if keys[i] then
105 return keys[i], t[keys[i]]
110 -- Sends a usbpackage , "hf 14b raw"
111 -- if it reads the response, it converts it to a lua object "Command" first and the Data is cut to correct length.
112 local function calypso_send_cmd_raw(data, ignoreresponse )
114 local flags = lib14b.ISO14B_COMMAND.ISO14B_APDU
115 -- flags = lib14b.ISO14B_COMMAND.ISO14B_RAW +
116 -- lib14b.ISO14B_COMMAND.ISO14B_APPEND_CRC
117 local flags = lib14b.ISO14B_COMMAND.ISO14B_APDU
118 data = data or ""
119 -- LEN of data, half the length of the ASCII-string hex string
120 -- 2 bytes flags
121 -- 4 bytes timeout
122 -- 2 bytes raw len
123 -- n bytes raw
125 local flags_str = ('%04x'):format(utils.SwapEndianness(('%04x'):format(flags), 16))
126 local time_str = ('%08x'):format(0)
127 local rawlen_str = ('%04x'):format(utils.SwapEndianness(('%04x'):format(( 8 + #data/2)), 16))
128 local senddata = ('%s%s%s%s'):format(flags_str, time_str, rawlen_str,data)
129 local c = Command:newNG{cmd = cmds.CMD_HF_ISO14443B_COMMAND, data = senddata}
130 local result, err = c:sendNG(ignoreresponse, 2000)
131 if result then
132 if result.Oldarg0 >= 0 then
133 return calypso_parse(result)
134 else
135 err = 'card response failed'
137 else
138 err = 'No response from card'
140 return result, err
143 -- calypso_card_num : Reads card number from ATR and
144 -- writes it in the tree in decimal format.
145 local function calypso_card_num(card)
146 if not card then return end
147 local card_num = tonumber( card.uid:sub(1,8),16 )
148 print('')
149 print('Card UID ' ..ansicolors.green..card.uid:format('%x')..ansicolors.reset)
150 print('Card Number ' ..ansicolors.green..string.format('%u', card_num)..ansicolors.reset)
151 print('-----------------------')
154 -- analyse CALYPSO apdu status bytes.
155 local function calypso_apdu_status(apdu)
156 -- last two is CRC
157 -- next two is APDU status bytes.
158 local mess = 'FAIL'
159 local sw = apdu:sub( #apdu-7, #apdu-4)
160 desc, err = iso7816.tostring(sw)
161 --print ('SW', sw, desc, err )
162 local status = ( sw == '9000' )
163 return status, desc, err
166 local CLA = '94'
167 local _calypso_cmds = {
169 -- Break down of command bytes:
170 -- A4 = select
171 -- Master File 3F00
172 -- 0x3F = master file
173 -- 0x00 = master file id, is constant to 0x00.
175 -- DF Dedicated File 38nn
176 -- can be seen as directories
177 -- 0x38
178 -- 0xNN id
179 -- ["01.Select ICC file"] = '0294 a4 080004 3f00 0002',
181 -- EF Elementary File
182 -- EF1 Pin file
183 -- EF2 Key file
184 -- Grey Lock file
185 -- Electronic deposit file
186 -- Electronic Purse file
187 -- Electronic Transaction log file
189 ['01.Select ICC file'] = CLA..'a4 080004 3f00 0002',
190 ['02.ICC'] = CLA..'b2 01 041d',
191 ['03.Select EnvHol file'] = CLA..'a4 080004 2000 2001',
192 ['04.EnvHol1'] = CLA..'b2 01 041d',
193 ['05.Select EvLog file'] = CLA..'a4 080004 2000 2010',
194 ['06.EvLog1'] = CLA..'b2 01 041d',
195 ['07.EvLog2'] = CLA..'b2 02 041d',
196 ['08.EvLog3'] = CLA..'b2 03 041d',
197 ['09.Select ConList file']= CLA..'a4 080004 2000 2050',
198 ['10.ConList'] = CLA..'b2 01 041d',
199 ['11.Select Contra file'] = CLA..'a4 080004 2000 2020',
200 ['12.Contra1'] = CLA..'b2 01 041d',
201 ['13.Contra2'] = CLA..'b2 02 041d',
202 ['14.Contra3'] = CLA..'b2 03 041d',
203 ['15.Contra4'] = CLA..'b2 04 041d',
204 ['16.Select Counter file']= CLA..'a4 080004 2000 2069',
205 ['17.Counter'] = CLA..'b2 01 041d',
206 ['18.Select SpecEv file'] = CLA..'a4 080004 2000 2040',
207 ['19.SpecEv1'] = CLA..'b2 01 041d',
211 -- The main entry point
212 function main(args)
214 print( string.rep('--',20) )
215 print( string.rep('--',20) )
216 print()
218 local data, apdu, flags, uid, cid, result, err, card
219 -- Read the parameters
220 for o, a in getopt.getopt(args, 'h') do
221 if o == 'h' then return help() end
222 if o == 'b' then bytes = a end
225 -- lib14b.connect()
227 -- Select 14b tag.
228 card, err = lib14b.waitFor14443b()
229 if not card then return oops(err) end
231 calypso_card_num(card)
232 cid = card.cid
234 --[[
235 NAME VALUE APDU_POS
236 PCB 0x0A 0
237 CID 0x00 1
238 CLA 0x94 2
239 SELECT FILE 0xA4 3
240 READ FILE 0xB2 3
241 P1 4
242 P2 5
243 LEN_
244 0 1 2 3 4 5 6 7
245 apdu = '02 94 a4 08 00 04 3f 00 00 02' --select ICC file
246 DF_NAME = "1TIC.ICA"
247 --]]
248 --for i = 1,10 do
249 --result, err = calypso_send_cmd_raw('0294a40800043f000002',false) --select ICC file
250 for i, apdu in spairs(_calypso_cmds) do
251 print('>> '..ansicolors.yellow..i..ansicolors.reset)
252 apdu = apdu:gsub('%s+', '')
253 data, err = calypso_send_cmd_raw(apdu , false)
254 if err then
255 print('<< '..err)
256 else
257 if data then
258 local status, desc, err = calypso_apdu_status(data)
259 local d = data:sub(3, (#data - 8))
260 if status then
261 print('<< '..d..' ('..ansicolors.green..'ok'..ansicolors.reset..')')
262 else
263 print('<< '..d..' '..ansicolors.red..err..ansicolors.reset )
265 else
266 print('<< no answer')
270 lib14b.disconnect()
273 -- a simple selftest function, tries to convert
274 function selftest()
275 DEBUG = true
276 dbg('Performing test')
277 dbg('Tests done')
279 -- Flip the switch here to perform a sanity check.
280 -- It read a nonce in two different ways, as specified in the usage-section
281 if '--test'==args then
282 selftest()
283 else
284 -- Call the main
285 main(args)