]>
Commit | Line | Data |
---|---|---|
1 | //----------------------------------------------------------------------------- | |
2 | // Copyright (C) 2010 iZsh <izsh at fail0verflow.com> | |
3 | // Copyright (C) Merlok - 2017 | |
4 | // | |
5 | // This code is licensed to you under the terms of the GNU GPL, version 2 or, | |
6 | // at your option, any later version. See the LICENSE.txt file for the text of | |
7 | // the license. | |
8 | //----------------------------------------------------------------------------- | |
9 | // Command: hf list. It shows data from arm buffer. | |
10 | //----------------------------------------------------------------------------- | |
11 | ||
12 | #include "cmdhflist.h" | |
13 | ||
14 | #include <stdlib.h> | |
15 | #include <stdio.h> | |
16 | #include <string.h> | |
17 | #include <stdint.h> | |
18 | #include <stdbool.h> | |
19 | #include "util.h" | |
20 | #include "ui.h" | |
21 | #include "cliparser/cliparser.h" | |
22 | #include "comms.h" | |
23 | #include "iso14443crc.h" | |
24 | #include "iso15693tools.h" | |
25 | #include "parity.h" | |
26 | #include "protocols.h" | |
27 | #include "crapto1/crapto1.h" | |
28 | #include "mifare/mifarehost.h" | |
29 | #include "mifare/mifaredefault.h" | |
30 | #include "usb_cmd.h" | |
31 | #include "pcsc.h" | |
32 | ||
33 | typedef struct { | |
34 | uint32_t uid; // UID | |
35 | uint32_t nt; // tag challenge | |
36 | uint32_t nt_enc; // encrypted tag challenge | |
37 | uint8_t nt_enc_par; // encrypted tag challenge parity | |
38 | uint32_t nr_enc; // encrypted reader challenge | |
39 | uint32_t ar_enc; // encrypted reader response | |
40 | uint8_t ar_enc_par; // encrypted reader response parity | |
41 | uint32_t at_enc; // encrypted tag response | |
42 | uint8_t at_enc_par; // encrypted tag response parity | |
43 | bool first_auth; // is first authentication | |
44 | uint32_t ks2; // ar ^ ar_enc | |
45 | uint32_t ks3; // at ^ at_enc | |
46 | } TAuthData; | |
47 | ||
48 | enum MifareAuthSeq { | |
49 | masNone, | |
50 | masNt, | |
51 | masNrAr, | |
52 | masAt, | |
53 | masAuthComplete, | |
54 | masFirstData, | |
55 | masData, | |
56 | masError, | |
57 | }; | |
58 | ||
59 | static enum MifareAuthSeq MifareAuthState; | |
60 | static TAuthData AuthData; | |
61 | ||
62 | static void ClearAuthData() { | |
63 | AuthData.uid = 0; | |
64 | AuthData.nt = 0; | |
65 | AuthData.first_auth = true; | |
66 | AuthData.ks2 = 0; | |
67 | AuthData.ks3 = 0; | |
68 | } | |
69 | ||
70 | /** | |
71 | * @brief iso14443A_CRC_check Checks CRC in command or response | |
72 | * @param isResponse | |
73 | * @param data | |
74 | * @param len | |
75 | * @return 0 : CRC-command, CRC not ok | |
76 | * 1 : CRC-command, CRC ok | |
77 | * 2 : Not crc-command | |
78 | */ | |
79 | static uint8_t iso14443A_CRC_check(bool isResponse, uint8_t* data, uint8_t len) | |
80 | { | |
81 | uint8_t b1,b2; | |
82 | ||
83 | if(len <= 2) return 2; | |
84 | ||
85 | if(isResponse & (len < 6)) return 2; | |
86 | ||
87 | ComputeCrc14443(CRC_14443_A, data, len-2, &b1, &b2); | |
88 | if (b1 != data[len-2] || b2 != data[len-1]) { | |
89 | return 0; | |
90 | } else { | |
91 | return 1; | |
92 | } | |
93 | } | |
94 | ||
95 | ||
96 | static uint8_t iso14443_4_CRC_check(uint8_t* data, uint8_t len) | |
97 | { | |
98 | uint8_t b1,b2; | |
99 | ||
100 | if(len <= 2) return 2; | |
101 | ||
102 | ComputeCrc14443(CRC_14443_A, data, len-2, &b1, &b2); | |
103 | if (b1 != data[len-2] || b2 != data[len-1]) { | |
104 | return 0; | |
105 | } else { | |
106 | return 1; | |
107 | } | |
108 | } | |
109 | ||
110 | ||
111 | static uint8_t mifare_CRC_check(bool isResponse, uint8_t* data, uint8_t len) | |
112 | { | |
113 | switch(MifareAuthState) { | |
114 | case masNone: | |
115 | case masError: | |
116 | return iso14443A_CRC_check(isResponse, data, len); | |
117 | default: | |
118 | return 2; | |
119 | } | |
120 | } | |
121 | ||
122 | ||
123 | /** | |
124 | * @brief iso14443B_CRC_check Checks CRC in command or response | |
125 | * @param isResponse | |
126 | * @param data | |
127 | * @param len | |
128 | * @return 0 : CRC-command, CRC not ok | |
129 | * 1 : CRC-command, CRC ok | |
130 | * 2 : Not crc-command | |
131 | */ | |
132 | static uint8_t iso14443B_CRC_check(bool isResponse, uint8_t* data, uint8_t len) | |
133 | { | |
134 | uint8_t b1,b2; | |
135 | ||
136 | if(len <= 2) return 2; | |
137 | ||
138 | ComputeCrc14443(CRC_14443_B, data, len-2, &b1, &b2); | |
139 | if(b1 != data[len-2] || b2 != data[len-1]) { | |
140 | return 0; | |
141 | } else { | |
142 | return 1; | |
143 | } | |
144 | } | |
145 | ||
146 | ||
147 | static uint8_t iso15693_CRC_check(uint8_t* d, uint16_t n) | |
148 | { | |
149 | if (n <= 2) return 2; | |
150 | ||
151 | return (Iso15693Crc(d, n) == ISO15693_CRC_CHECK ? 1 : 0); | |
152 | } | |
153 | ||
154 | ||
155 | /** | |
156 | * @brief iclass_CRC_Ok Checks CRC in command or response | |
157 | * @param isResponse | |
158 | * @param data | |
159 | * @param len | |
160 | * @return 0 : CRC-command, CRC not ok | |
161 | * 1 : CRC-command, CRC ok | |
162 | * 2 : Not crc-command | |
163 | */ | |
164 | uint8_t iclass_CRC_check(bool isResponse, uint8_t* data, uint8_t len) | |
165 | { | |
166 | if(len < 4) return 2;//CRC commands (and responses) are all at least 4 bytes | |
167 | ||
168 | uint8_t b1, b2; | |
169 | ||
170 | if(!isResponse)//Commands to tag | |
171 | { | |
172 | /** | |
173 | These commands should have CRC. Total length leftmost | |
174 | 4 READ | |
175 | 4 READ4 | |
176 | 12 UPDATE - unsecured, ends with CRC16 | |
177 | 14 UPDATE - secured, ends with signature instead | |
178 | 4 PAGESEL | |
179 | **/ | |
180 | if(len == 4 || len == 12)//Covers three of them | |
181 | { | |
182 | //Don't include the command byte | |
183 | ComputeCrc14443(CRC_ICLASS, (data+1), len-3, &b1, &b2); | |
184 | return b1 == data[len -2] && b2 == data[len-1]; | |
185 | } | |
186 | return 2; | |
187 | }else{ | |
188 | /** | |
189 | These tag responses should have CRC. Total length leftmost | |
190 | ||
191 | 10 READ data[8] crc[2] | |
192 | 34 READ4 data[32]crc[2] | |
193 | 10 UPDATE data[8] crc[2] | |
194 | 10 SELECT csn[8] crc[2] | |
195 | 10 IDENTIFY asnb[8] crc[2] | |
196 | 10 PAGESEL block1[8] crc[2] | |
197 | 10 DETECT csn[8] crc[2] | |
198 | ||
199 | These should not | |
200 | ||
201 | 4 CHECK chip_response[4] | |
202 | 8 READCHECK data[8] | |
203 | 1 ACTALL sof[1] | |
204 | 1 ACT sof[1] | |
205 | ||
206 | In conclusion, without looking at the command; any response | |
207 | of length 10 or 34 should have CRC | |
208 | **/ | |
209 | if(len != 10 && len != 34) return true; | |
210 | ||
211 | ComputeCrc14443(CRC_ICLASS, data, len-2, &b1, &b2); | |
212 | return b1 == data[len -2] && b2 == data[len-1]; | |
213 | } | |
214 | } | |
215 | ||
216 | ||
217 | void annotateIclass(char *exp, size_t size, uint8_t* cmd, uint8_t cmdsize) { | |
218 | switch(cmd[0]) | |
219 | { | |
220 | case ICLASS_CMD_ACTALL: snprintf(exp, size, "ACTALL"); break; | |
221 | case ICLASS_CMD_READ_OR_IDENTIFY: { | |
222 | if (cmdsize > 1){ | |
223 | snprintf(exp,size,"READ(%d)",cmd[1]); | |
224 | } else { | |
225 | snprintf(exp,size,"IDENTIFY"); | |
226 | } | |
227 | break; | |
228 | } | |
229 | case ICLASS_CMD_SELECT: snprintf(exp,size, "SELECT"); break; | |
230 | case ICLASS_CMD_PAGESEL: snprintf(exp,size, "PAGESEL(%d)", cmd[1]); break; | |
231 | case ICLASS_CMD_READCHECK_KC: snprintf(exp,size, "READCHECK[Kc](%d)", cmd[1]); break; | |
232 | case ICLASS_CMD_READCHECK_KD: snprintf(exp,size, "READCHECK[Kd](%d)", cmd[1]); break; | |
233 | case ICLASS_CMD_CHECK_KC: | |
234 | case ICLASS_CMD_CHECK_KD: snprintf(exp,size, "CHECK"); break; | |
235 | case ICLASS_CMD_DETECT: snprintf(exp,size, "DETECT"); break; | |
236 | case ICLASS_CMD_HALT: snprintf(exp,size, "HALT"); break; | |
237 | case ICLASS_CMD_UPDATE: snprintf(exp,size, "UPDATE(%d)",cmd[1]); break; | |
238 | case ICLASS_CMD_ACT: snprintf(exp,size, "ACT"); break; | |
239 | case ICLASS_CMD_READ4: snprintf(exp,size, "READ4(%d)",cmd[1]); break; | |
240 | default: snprintf(exp,size, "?"); break; | |
241 | } | |
242 | return; | |
243 | } | |
244 | ||
245 | ||
246 | void annotateIso15693(char *exp, size_t size, uint8_t* cmd, uint8_t cmdsize) { | |
247 | if (cmdsize >= 2) { | |
248 | switch (cmd[1]) { | |
249 | // Mandatory Commands, all Tags must support them: | |
250 | case ISO15693_INVENTORY :snprintf(exp, size, "INVENTORY");return; | |
251 | case ISO15693_STAYQUIET :snprintf(exp, size, "STAY_QUIET");return; | |
252 | // Optional Commands, Tags may support them: | |
253 | case ISO15693_READBLOCK :snprintf(exp, size, "READBLOCK");return; | |
254 | case ISO15693_WRITEBLOCK :snprintf(exp, size, "WRITEBLOCK");return; | |
255 | case ISO15693_LOCKBLOCK :snprintf(exp, size, "LOCKBLOCK");return; | |
256 | case ISO15693_READ_MULTI_BLOCK :snprintf(exp, size, "READ_MULTI_BLOCK");return; | |
257 | case ISO15693_WRITE_MULTI_BLOCK :snprintf(exp, size, "WRITE_MULTI_BLOCK");return; | |
258 | case ISO15693_SELECT :snprintf(exp, size, "SELECT");return; | |
259 | case ISO15693_RESET_TO_READY :snprintf(exp, size, "RESET_TO_READY");return; | |
260 | case ISO15693_WRITE_AFI :snprintf(exp, size, "WRITE_AFI");return; | |
261 | case ISO15693_LOCK_AFI :snprintf(exp, size, "LOCK_AFI");return; | |
262 | case ISO15693_WRITE_DSFID :snprintf(exp, size, "WRITE_DSFID");return; | |
263 | case ISO15693_LOCK_DSFID :snprintf(exp, size, "LOCK_DSFID");return; | |
264 | case ISO15693_GET_SYSTEM_INFO :snprintf(exp, size, "GET_SYSTEM_INFO");return; | |
265 | case ISO15693_READ_MULTI_SECSTATUS :snprintf(exp, size, "READ_MULTI_SECSTATUS");return; | |
266 | default: break; | |
267 | } | |
268 | ||
269 | if (cmd[1] > ISO15693_STAYQUIET && cmd[1] < ISO15693_READBLOCK) snprintf(exp, size, "Mandatory RFU"); | |
270 | else if (cmd[1] > ISO15693_READ_MULTI_SECSTATUS && cmd[1] <= 0x9F) snprintf(exp, size, "Optional RFU"); | |
271 | else if ( cmd[1] >= 0xA0 && cmd[1] <= 0xDF ) snprintf(exp, size, "Custom command"); | |
272 | else if ( cmd[1] >= 0xE0 && cmd[1] <= 0xFF ) snprintf(exp, size, "Proprietary command"); | |
273 | } | |
274 | } | |
275 | ||
276 | ||
277 | void annotateTopaz(char *exp, size_t size, uint8_t* cmd, uint8_t cmdsize) | |
278 | { | |
279 | switch(cmd[0]) { | |
280 | case TOPAZ_REQA :snprintf(exp, size, "REQA");break; | |
281 | case TOPAZ_WUPA :snprintf(exp, size, "WUPA");break; | |
282 | case TOPAZ_RID :snprintf(exp, size, "RID");break; | |
283 | case TOPAZ_RALL :snprintf(exp, size, "RALL");break; | |
284 | case TOPAZ_READ :snprintf(exp, size, "READ");break; | |
285 | case TOPAZ_WRITE_E :snprintf(exp, size, "WRITE-E");break; | |
286 | case TOPAZ_WRITE_NE :snprintf(exp, size, "WRITE-NE");break; | |
287 | case TOPAZ_RSEG :snprintf(exp, size, "RSEG");break; | |
288 | case TOPAZ_READ8 :snprintf(exp, size, "READ8");break; | |
289 | case TOPAZ_WRITE_E8 :snprintf(exp, size, "WRITE-E8");break; | |
290 | case TOPAZ_WRITE_NE8 :snprintf(exp, size, "WRITE-NE8");break; | |
291 | default: snprintf(exp,size,"?"); break; | |
292 | } | |
293 | } | |
294 | ||
295 | ||
296 | void annotateIso7816(char *exp, size_t size, uint8_t* cmd, uint8_t cmdsize) | |
297 | { | |
298 | switch ( cmd[1] ){ | |
299 | case ISO7816_READ_BINARY :snprintf(exp, size, "READ BINARY");break; | |
300 | case ISO7816_WRITE_BINARY :snprintf(exp, size, "WRITE BINARY");break; | |
301 | case ISO7816_UPDATE_BINARY :snprintf(exp, size, "UPDATE BINARY");break; | |
302 | case ISO7816_ERASE_BINARY :snprintf(exp, size, "ERASE BINARY");break; | |
303 | case ISO7816_READ_RECORDS :snprintf(exp, size, "READ RECORD(S)");break; | |
304 | case ISO7816_WRITE_RECORD :snprintf(exp, size, "WRITE RECORD");break; | |
305 | case ISO7816_APPEND_RECORD :snprintf(exp, size, "APPEND RECORD");break; | |
306 | case ISO7816_UPDATE_DATA :snprintf(exp, size, "UPDATE DATA");break; | |
307 | case ISO7816_GET_DATA :snprintf(exp, size, "GET DATA");break; | |
308 | case ISO7816_PUT_DATA :snprintf(exp, size, "PUT DATA");break; | |
309 | case ISO7816_SELECT_FILE :snprintf(exp, size, "SELECT FILE");break; | |
310 | case ISO7816_VERIFY :snprintf(exp, size, "VERIFY");break; | |
311 | case ISO7816_INTERNAL_AUTHENTICATE :snprintf(exp, size, "INTERNAL AUTHENTICATE");break; | |
312 | case ISO7816_EXTERNAL_AUTHENTICATE :snprintf(exp, size, "EXTERNAL AUTHENTICATE");break; | |
313 | case ISO7816_GET_CHALLENGE :snprintf(exp, size, "GET CHALLENGE");break; | |
314 | case ISO7816_MANAGE_CHANNEL :snprintf(exp, size, "MANAGE CHANNEL");break; | |
315 | case ISO7816_GET_RESPONSE :snprintf(exp, size, "GET RESPONSE");break; | |
316 | case ISO7816_ENVELOPE :snprintf(exp, size, "ENVELOPE");break; | |
317 | case ISO7816_GET_PROCESSING_OPTIONS :snprintf(exp, size, "GET PROCESSING OPTIONS");break; | |
318 | default :snprintf(exp,size,"?"); break; | |
319 | } | |
320 | } | |
321 | ||
322 | ||
323 | void annotateIso14443_4(char *exp, size_t size, uint8_t* cmd, uint8_t cmdsize) { | |
324 | // S-block | |
325 | if ((cmd[0] & 0xc3) == 0xc2) { | |
326 | switch (cmd[0] & 0x30) { | |
327 | case 0x00 : snprintf(exp, size, "S-block DESELECT"); break; | |
328 | case 0x30 : snprintf(exp, size, "S-block WTX"); break; | |
329 | default : snprintf(exp, size, "S-block (RFU)"); break; | |
330 | } | |
331 | } | |
332 | // R-block (ack) | |
333 | else if ((cmd[0] & 0xe0) == 0xa0) { | |
334 | if ((cmd[0] & 0x10) == 0) | |
335 | snprintf(exp, size, "R-block ACK"); | |
336 | else | |
337 | snprintf(exp, size, "R-block NACK"); | |
338 | } | |
339 | // I-block | |
340 | else { | |
341 | int pos = 1; | |
342 | switch (cmd[0] & 0x0c) { | |
343 | case 0x08: // CID following | |
344 | case 0x04: // NAD following | |
345 | pos = 2; | |
346 | break; | |
347 | case 0x0c: // CID and NAD following | |
348 | pos = 3; | |
349 | break; | |
350 | default: | |
351 | pos = 1; // no CID, no NAD | |
352 | break; | |
353 | } | |
354 | annotateIso7816(exp, size, &cmd[pos], cmdsize-pos); | |
355 | } | |
356 | } | |
357 | ||
358 | ||
359 | /** | |
360 | 06 00 = INITIATE | |
361 | 0E xx = SELECT ID (xx = Chip-ID) | |
362 | 0B = Get UID | |
363 | 08 yy = Read Block (yy = block number) | |
364 | 09 yy dd dd dd dd = Write Block (yy = block number; dd dd dd dd = data to be written) | |
365 | 0C = Reset to Inventory | |
366 | 0F = Completion | |
367 | 0A 11 22 33 44 55 66 = Authenticate (11 22 33 44 55 66 = data to authenticate) | |
368 | **/ | |
369 | ||
370 | void annotateIso14443b(char *exp, size_t size, uint8_t* cmd, uint8_t cmdsize) { | |
371 | switch(cmd[0]){ | |
372 | case ISO14443B_REQB : snprintf(exp,size,"REQB");break; | |
373 | case ISO14443B_ATTRIB : snprintf(exp,size,"ATTRIB");break; | |
374 | case ISO14443B_HALT : snprintf(exp,size,"HALT");break; | |
375 | case ISO14443B_INITIATE : snprintf(exp,size,"INITIATE");break; | |
376 | case ISO14443B_SELECT : snprintf(exp,size,"SELECT(%d)",cmd[1]);break; | |
377 | case ISO14443B_GET_UID : snprintf(exp,size,"GET UID");break; | |
378 | case ISO14443B_READ_BLK : snprintf(exp,size,"READ_BLK(%d)", cmd[1]);break; | |
379 | case ISO14443B_WRITE_BLK : snprintf(exp,size,"WRITE_BLK(%d)",cmd[1]);break; | |
380 | case ISO14443B_RESET : snprintf(exp,size,"RESET");break; | |
381 | case ISO14443B_COMPLETION : snprintf(exp,size,"COMPLETION");break; | |
382 | case ISO14443B_AUTHENTICATE : snprintf(exp,size,"AUTHENTICATE");break; | |
383 | default : snprintf(exp,size ,"?");break; | |
384 | } | |
385 | ||
386 | } | |
387 | ||
388 | void annotateIso14443a(char *exp, size_t size, uint8_t* cmd, uint8_t cmdsize) { | |
389 | switch(cmd[0]) | |
390 | { | |
391 | case ISO14443A_CMD_WUPA: | |
392 | snprintf(exp,size,"WUPA"); | |
393 | break; | |
394 | case ISO14443A_CMD_ANTICOLL_OR_SELECT:{ | |
395 | // 93 20 = Anticollision (usage: 9320 - answer: 4bytes UID+1byte UID-bytes-xor) | |
396 | // 93 70 = Select (usage: 9370+5bytes 9320 answer - answer: 1byte SAK) | |
397 | if(cmd[1] == 0x70) | |
398 | { | |
399 | snprintf(exp,size,"SELECT_UID"); break; | |
400 | }else | |
401 | { | |
402 | snprintf(exp,size,"ANTICOLL"); break; | |
403 | } | |
404 | } | |
405 | case ISO14443A_CMD_ANTICOLL_OR_SELECT_2:{ | |
406 | //95 20 = Anticollision of cascade level2 | |
407 | //95 70 = Select of cascade level2 | |
408 | if(cmd[2] == 0x70) | |
409 | { | |
410 | snprintf(exp,size,"SELECT_UID-2"); break; | |
411 | }else | |
412 | { | |
413 | snprintf(exp,size,"ANTICOLL-2"); break; | |
414 | } | |
415 | } | |
416 | case ISO14443A_CMD_REQA: | |
417 | snprintf(exp,size,"REQA"); | |
418 | break; | |
419 | case MIFARE_CMD_READBLOCK: snprintf(exp,size,"READBLOCK(%d)",cmd[1]); break; | |
420 | case MIFARE_CMD_WRITEBLOCK: snprintf(exp,size,"WRITEBLOCK(%d)",cmd[1]); break; | |
421 | case ISO14443A_CMD_HALT: | |
422 | snprintf(exp,size,"HALT"); | |
423 | MifareAuthState = masNone; | |
424 | break; | |
425 | case ISO14443A_CMD_RATS: snprintf(exp,size,"RATS"); break; | |
426 | case MIFARE_CMD_INC: snprintf(exp,size,"INC(%d)",cmd[1]); break; | |
427 | case MIFARE_CMD_DEC: snprintf(exp,size,"DEC(%d)",cmd[1]); break; | |
428 | case MIFARE_CMD_RESTORE: snprintf(exp,size,"RESTORE(%d)",cmd[1]); break; | |
429 | case MIFARE_CMD_TRANSFER: snprintf(exp,size,"TRANSFER(%d)",cmd[1]); break; | |
430 | case MIFARE_AUTH_KEYA: | |
431 | if ( cmdsize > 3) { | |
432 | snprintf(exp,size,"AUTH-A(%d)",cmd[1]); | |
433 | MifareAuthState = masNt; | |
434 | } else { | |
435 | // case MIFARE_ULEV1_VERSION : both 0x60. | |
436 | snprintf(exp,size,"EV1 VERSION"); | |
437 | } | |
438 | break; | |
439 | case MIFARE_AUTH_KEYB: | |
440 | MifareAuthState = masNt; | |
441 | snprintf(exp,size,"AUTH-B(%d)",cmd[1]); | |
442 | break; | |
443 | case MIFARE_MAGICWUPC1: snprintf(exp,size,"MAGIC WUPC1"); break; | |
444 | case MIFARE_MAGICWUPC2: snprintf(exp,size,"MAGIC WUPC2"); break; | |
445 | case MIFARE_MAGICWIPEC: snprintf(exp,size,"MAGIC WIPEC"); break; | |
446 | case MIFARE_ULC_AUTH_1: snprintf(exp,size,"AUTH "); break; | |
447 | case MIFARE_ULC_AUTH_2: snprintf(exp,size,"AUTH_ANSW"); break; | |
448 | case MIFARE_ULEV1_AUTH: | |
449 | if ( cmdsize == 7 ) | |
450 | snprintf(exp,size,"PWD-AUTH KEY: 0x%02x%02x%02x%02x", cmd[1], cmd[2], cmd[3], cmd[4] ); | |
451 | else | |
452 | snprintf(exp,size,"PWD-AUTH"); | |
453 | break; | |
454 | case MIFARE_ULEV1_FASTREAD:{ | |
455 | if ( cmdsize >=3 && cmd[2] <= 0xE6) | |
456 | snprintf(exp,size,"READ RANGE (%d-%d)",cmd[1],cmd[2]); | |
457 | else | |
458 | snprintf(exp,size,"?"); | |
459 | break; | |
460 | } | |
461 | case MIFARE_ULC_WRITE:{ | |
462 | if ( cmd[1] < 0x21 ) | |
463 | snprintf(exp,size,"WRITEBLOCK(%d)",cmd[1]); | |
464 | else | |
465 | snprintf(exp,size,"?"); | |
466 | break; | |
467 | } | |
468 | case MIFARE_ULEV1_READ_CNT:{ | |
469 | if ( cmd[1] < 5 ) | |
470 | snprintf(exp,size,"READ CNT(%d)",cmd[1]); | |
471 | else | |
472 | snprintf(exp,size,"?"); | |
473 | break; | |
474 | } | |
475 | case MIFARE_ULEV1_INCR_CNT:{ | |
476 | if ( cmd[1] < 5 ) | |
477 | snprintf(exp,size,"INCR(%d)",cmd[1]); | |
478 | else | |
479 | snprintf(exp,size,"?"); | |
480 | break; | |
481 | } | |
482 | case MIFARE_ULEV1_READSIG: snprintf(exp,size,"READ_SIG"); break; | |
483 | case MIFARE_ULEV1_CHECKTEAR: snprintf(exp,size,"CHK_TEARING(%d)",cmd[1]); break; | |
484 | case MIFARE_ULEV1_VCSL: snprintf(exp,size,"VCSL"); break; | |
485 | default: snprintf(exp,size,"?"); break; | |
486 | } | |
487 | return; | |
488 | } | |
489 | ||
490 | void annotateMifare(char *exp, size_t size, uint8_t* cmd, uint8_t cmdsize, uint8_t* parity, uint8_t paritysize, bool isResponse) { | |
491 | if (!isResponse && cmdsize == 1) { | |
492 | switch(cmd[0]) { | |
493 | case ISO14443A_CMD_WUPA: | |
494 | case ISO14443A_CMD_REQA: | |
495 | MifareAuthState = masNone; | |
496 | break; | |
497 | default: | |
498 | break; | |
499 | } | |
500 | } | |
501 | ||
502 | // get UID | |
503 | if (MifareAuthState == masNone) { | |
504 | if (cmdsize == 9 && cmd[0] == ISO14443A_CMD_ANTICOLL_OR_SELECT && cmd[1] == 0x70) { | |
505 | ClearAuthData(); | |
506 | AuthData.uid = bytes_to_num(&cmd[2], 4); | |
507 | } | |
508 | if (cmdsize == 9 && cmd[0] == ISO14443A_CMD_ANTICOLL_OR_SELECT_2 && cmd[1] == 0x70) { | |
509 | ClearAuthData(); | |
510 | AuthData.uid = bytes_to_num(&cmd[2], 4); | |
511 | } | |
512 | } | |
513 | ||
514 | switch(MifareAuthState) { | |
515 | case masNt: | |
516 | if (cmdsize == 4 && isResponse) { | |
517 | snprintf(exp,size,"AUTH: nt %s", (AuthData.first_auth) ? "" : "(enc)"); | |
518 | MifareAuthState = masNrAr; | |
519 | if (AuthData.first_auth) { | |
520 | AuthData.nt = bytes_to_num(cmd, 4); | |
521 | } else { | |
522 | AuthData.nt_enc = bytes_to_num(cmd, 4); | |
523 | AuthData.nt_enc_par = parity[0]; | |
524 | } | |
525 | return; | |
526 | } else { | |
527 | MifareAuthState = masError; | |
528 | } | |
529 | break; | |
530 | case masNrAr: | |
531 | if (cmdsize == 8 && !isResponse) { | |
532 | snprintf(exp,size,"AUTH: nr ar (enc)"); | |
533 | MifareAuthState = masAt; | |
534 | AuthData.nr_enc = bytes_to_num(cmd, 4); | |
535 | AuthData.ar_enc = bytes_to_num(&cmd[4], 4); | |
536 | AuthData.ar_enc_par = parity[0] << 4; | |
537 | return; | |
538 | } else { | |
539 | MifareAuthState = masError; | |
540 | } | |
541 | break; | |
542 | case masAt: | |
543 | if (cmdsize == 4 && isResponse) { | |
544 | snprintf(exp,size,"AUTH: at (enc)"); | |
545 | MifareAuthState = masAuthComplete; | |
546 | AuthData.at_enc = bytes_to_num(cmd, 4); | |
547 | AuthData.at_enc_par = parity[0]; | |
548 | return; | |
549 | } else { | |
550 | MifareAuthState = masError; | |
551 | } | |
552 | break; | |
553 | default: | |
554 | break; | |
555 | } | |
556 | ||
557 | if (!isResponse && ((MifareAuthState == masNone) || (MifareAuthState == masError))) | |
558 | annotateIso14443a(exp, size, cmd, cmdsize); | |
559 | ||
560 | } | |
561 | ||
562 | ||
563 | static uint64_t GetCrypto1ProbableKey(TAuthData *ad) { | |
564 | struct Crypto1State *revstate = lfsr_recovery64(ad->ks2, ad->ks3); | |
565 | lfsr_rollback_word(revstate, 0, 0); | |
566 | lfsr_rollback_word(revstate, 0, 0); | |
567 | lfsr_rollback_word(revstate, ad->nr_enc, 1); | |
568 | lfsr_rollback_word(revstate, ad->uid ^ ad->nt, 0); | |
569 | ||
570 | uint64_t lfsr = 0; | |
571 | crypto1_get_lfsr(revstate, &lfsr); | |
572 | crypto1_destroy(revstate); | |
573 | ||
574 | return lfsr; | |
575 | } | |
576 | ||
577 | ||
578 | static bool NTParityChk(TAuthData *ad, uint32_t ntx) { | |
579 | if ( | |
580 | (oddparity8(ntx >> 8 & 0xff) ^ (ntx & 0x01) ^ ((ad->nt_enc_par >> 5) & 0x01) ^ (ad->nt_enc & 0x01)) || | |
581 | (oddparity8(ntx >> 16 & 0xff) ^ (ntx >> 8 & 0x01) ^ ((ad->nt_enc_par >> 6) & 0x01) ^ (ad->nt_enc >> 8 & 0x01)) || | |
582 | (oddparity8(ntx >> 24 & 0xff) ^ (ntx >> 16 & 0x01) ^ ((ad->nt_enc_par >> 7) & 0x01) ^ (ad->nt_enc >> 16 & 0x01)) | |
583 | ) | |
584 | return false; | |
585 | ||
586 | uint32_t ar = prng_successor(ntx, 64); | |
587 | if ( | |
588 | (oddparity8(ar >> 8 & 0xff) ^ (ar & 0x01) ^ ((ad->ar_enc_par >> 5) & 0x01) ^ (ad->ar_enc & 0x01)) || | |
589 | (oddparity8(ar >> 16 & 0xff) ^ (ar >> 8 & 0x01) ^ ((ad->ar_enc_par >> 6) & 0x01) ^ (ad->ar_enc >> 8 & 0x01)) || | |
590 | (oddparity8(ar >> 24 & 0xff) ^ (ar >> 16 & 0x01) ^ ((ad->ar_enc_par >> 7) & 0x01) ^ (ad->ar_enc >> 16 & 0x01)) | |
591 | ) | |
592 | return false; | |
593 | ||
594 | uint32_t at = prng_successor(ntx, 96); | |
595 | if ( | |
596 | (oddparity8(ar & 0xff) ^ (at >> 24 & 0x01) ^ ((ad->ar_enc_par >> 4) & 0x01) ^ (ad->at_enc >> 24 & 0x01)) || | |
597 | (oddparity8(at >> 8 & 0xff) ^ (at & 0x01) ^ ((ad->at_enc_par >> 5) & 0x01) ^ (ad->at_enc & 0x01)) || | |
598 | (oddparity8(at >> 16 & 0xff) ^ (at >> 8 & 0x01) ^ ((ad->at_enc_par >> 6) & 0x01) ^ (ad->at_enc >> 8 & 0x01)) || | |
599 | (oddparity8(at >> 24 & 0xff) ^ (at >> 16 & 0x01) ^ ((ad->at_enc_par >> 7) & 0x01) ^ (ad->at_enc >> 16 & 0x01)) | |
600 | ) | |
601 | return false; | |
602 | ||
603 | return true; | |
604 | } | |
605 | ||
606 | ||
607 | static bool CheckCrypto1Parity(uint8_t *cmd_enc, uint8_t cmdsize, uint8_t *cmd, uint8_t *parity_enc) { | |
608 | for (int i = 0; i < cmdsize - 1; i++) { | |
609 | if (oddparity8(cmd[i]) ^ (cmd[i + 1] & 0x01) ^ ((parity_enc[i / 8] >> (7 - i % 8)) & 0x01) ^ (cmd_enc[i + 1] & 0x01)) | |
610 | return false; | |
611 | } | |
612 | ||
613 | return true; | |
614 | } | |
615 | ||
616 | ||
617 | static bool NestedCheckKey(uint64_t key, TAuthData *ad, uint8_t *cmd, uint8_t cmdsize, uint8_t *parity) { | |
618 | uint8_t buf[32] = {0}; | |
619 | struct Crypto1State *pcs; | |
620 | ||
621 | AuthData.ks2 = 0; | |
622 | AuthData.ks3 = 0; | |
623 | ||
624 | pcs = crypto1_create(key); | |
625 | uint32_t nt1 = crypto1_word(pcs, ad->nt_enc ^ ad->uid, 1) ^ ad->nt_enc; | |
626 | uint32_t ar = prng_successor(nt1, 64); | |
627 | uint32_t at = prng_successor(nt1, 96); | |
628 | ||
629 | crypto1_word(pcs, ad->nr_enc, 1); | |
630 | // uint32_t nr1 = crypto1_word(pcs, ad->nr_enc, 1) ^ ad->nr_enc; // if needs deciphered nr | |
631 | uint32_t ar1 = crypto1_word(pcs, 0, 0) ^ ad->ar_enc; | |
632 | uint32_t at1 = crypto1_word(pcs, 0, 0) ^ ad->at_enc; | |
633 | ||
634 | if (!(ar == ar1 && at == at1 && NTParityChk(ad, nt1))) { | |
635 | crypto1_destroy(pcs); | |
636 | return false; | |
637 | } | |
638 | ||
639 | memcpy(buf, cmd, cmdsize); | |
640 | mf_crypto1_decrypt(pcs, buf, cmdsize, 0); | |
641 | ||
642 | crypto1_destroy(pcs); | |
643 | ||
644 | if (!CheckCrypto1Parity(cmd, cmdsize, buf, parity)) | |
645 | return false; | |
646 | ||
647 | if(!CheckCrc14443(CRC_14443_A, buf, cmdsize)) | |
648 | return false; | |
649 | ||
650 | AuthData.nt = nt1; | |
651 | AuthData.ks2 = AuthData.ar_enc ^ ar; | |
652 | AuthData.ks3 = AuthData.at_enc ^ at; | |
653 | ||
654 | return true; | |
655 | } | |
656 | ||
657 | ||
658 | static bool DecodeMifareData(uint8_t *cmd, uint8_t cmdsize, uint8_t *parity, bool isResponse, uint8_t *mfData, size_t *mfDataLen) { | |
659 | static struct Crypto1State *traceCrypto1; | |
660 | static uint64_t mfLastKey; | |
661 | ||
662 | *mfDataLen = 0; | |
663 | ||
664 | if (MifareAuthState == masAuthComplete) { | |
665 | if (traceCrypto1) { | |
666 | crypto1_destroy(traceCrypto1); | |
667 | traceCrypto1 = NULL; | |
668 | } | |
669 | ||
670 | MifareAuthState = masFirstData; | |
671 | return false; | |
672 | } | |
673 | ||
674 | if (cmdsize > 32) | |
675 | return false; | |
676 | ||
677 | if (MifareAuthState == masFirstData) { | |
678 | if (AuthData.first_auth) { | |
679 | AuthData.ks2 = AuthData.ar_enc ^ prng_successor(AuthData.nt, 64); | |
680 | AuthData.ks3 = AuthData.at_enc ^ prng_successor(AuthData.nt, 96); | |
681 | ||
682 | mfLastKey = GetCrypto1ProbableKey(&AuthData); | |
683 | PrintAndLog(" | * | key | probable key:%012"PRIx64" Prng:%s ks2:%08x ks3:%08x | |", | |
684 | mfLastKey, | |
685 | validate_prng_nonce(AuthData.nt) ? "WEAK": "HARD", | |
686 | AuthData.ks2, | |
687 | AuthData.ks3); | |
688 | ||
689 | AuthData.first_auth = false; | |
690 | ||
691 | traceCrypto1 = lfsr_recovery64(AuthData.ks2, AuthData.ks3); | |
692 | } else { | |
693 | if (traceCrypto1) { | |
694 | crypto1_destroy(traceCrypto1); | |
695 | traceCrypto1 = NULL; | |
696 | } | |
697 | ||
698 | // check last used key | |
699 | if (mfLastKey) { | |
700 | if (NestedCheckKey(mfLastKey, &AuthData, cmd, cmdsize, parity)) { | |
701 | PrintAndLog(" | * | key | last used key:%012"PRIx64" ks2:%08x ks3:%08x | |", | |
702 | mfLastKey, | |
703 | AuthData.ks2, | |
704 | AuthData.ks3); | |
705 | ||
706 | traceCrypto1 = lfsr_recovery64(AuthData.ks2, AuthData.ks3); | |
707 | }; | |
708 | } | |
709 | ||
710 | // check default keys | |
711 | if (!traceCrypto1) { | |
712 | for (int defaultKeyCounter = 0; defaultKeyCounter < MifareDefaultKeysSize; defaultKeyCounter++){ | |
713 | if (NestedCheckKey(MifareDefaultKeys[defaultKeyCounter], &AuthData, cmd, cmdsize, parity)) { | |
714 | PrintAndLog(" | * | key | default key:%012"PRIx64" ks2:%08x ks3:%08x | |", | |
715 | MifareDefaultKeys[defaultKeyCounter], | |
716 | AuthData.ks2, | |
717 | AuthData.ks3); | |
718 | ||
719 | mfLastKey = MifareDefaultKeys[defaultKeyCounter]; | |
720 | traceCrypto1 = lfsr_recovery64(AuthData.ks2, AuthData.ks3); | |
721 | break; | |
722 | }; | |
723 | } | |
724 | } | |
725 | ||
726 | // nested | |
727 | if (!traceCrypto1 && validate_prng_nonce(AuthData.nt)) { | |
728 | uint32_t ntx = prng_successor(AuthData.nt, 90); | |
729 | for (int i = 0; i < 16383; i++) { | |
730 | ntx = prng_successor(ntx, 1); | |
731 | if (NTParityChk(&AuthData, ntx)){ | |
732 | ||
733 | uint32_t ks2 = AuthData.ar_enc ^ prng_successor(ntx, 64); | |
734 | uint32_t ks3 = AuthData.at_enc ^ prng_successor(ntx, 96); | |
735 | struct Crypto1State *pcs = lfsr_recovery64(ks2, ks3); | |
736 | memcpy(mfData, cmd, cmdsize); | |
737 | mf_crypto1_decrypt(pcs, mfData, cmdsize, 0); | |
738 | ||
739 | crypto1_destroy(pcs); | |
740 | if (CheckCrypto1Parity(cmd, cmdsize, mfData, parity) && CheckCrc14443(CRC_14443_A, mfData, cmdsize)) { | |
741 | AuthData.ks2 = ks2; | |
742 | AuthData.ks3 = ks3; | |
743 | ||
744 | AuthData.nt = ntx; | |
745 | mfLastKey = GetCrypto1ProbableKey(&AuthData); | |
746 | PrintAndLog(" | * | key | nested probable key:%012"PRIx64" ks2:%08x ks3:%08x | |", | |
747 | mfLastKey, | |
748 | AuthData.ks2, | |
749 | AuthData.ks3); | |
750 | ||
751 | traceCrypto1 = lfsr_recovery64(AuthData.ks2, AuthData.ks3); | |
752 | break; | |
753 | } | |
754 | } | |
755 | } | |
756 | } | |
757 | ||
758 | //hardnested | |
759 | if (!traceCrypto1) { | |
760 | printf("hardnested not implemented. uid:%x nt:%x ar_enc:%x at_enc:%x\n", AuthData.uid, AuthData.nt, AuthData.ar_enc, AuthData.at_enc); | |
761 | MifareAuthState = masError; | |
762 | ||
763 | /* TOO SLOW( needs to have more strong filter. with this filter - aprox 4 mln tests | |
764 | uint32_t t = msclock(); | |
765 | uint32_t t1 = t; | |
766 | int n = 0; | |
767 | for (uint32_t i = 0; i < 0xFFFFFFFF; i++) { | |
768 | if (NTParityChk(&AuthData, i)){ | |
769 | ||
770 | uint32_t ks2 = AuthData.ar_enc ^ prng_successor(i, 64); | |
771 | uint32_t ks3 = AuthData.at_enc ^ prng_successor(i, 96); | |
772 | struct Crypto1State *pcs = lfsr_recovery64(ks2, ks3); | |
773 | ||
774 | ||
775 | ||
776 | ||
777 | n++; | |
778 | ||
779 | if (!(n % 100000)) { | |
780 | printf("delta=%d n=%d ks2=%x ks3=%x \n", msclock() - t1 , n, ks2, ks3); | |
781 | t1 = msclock(); | |
782 | } | |
783 | ||
784 | } | |
785 | } | |
786 | printf("delta=%d n=%d\n", msclock() - t, n); | |
787 | */ | |
788 | } | |
789 | } | |
790 | ||
791 | ||
792 | ||
793 | MifareAuthState = masData; | |
794 | } | |
795 | ||
796 | if (MifareAuthState == masData && traceCrypto1) { | |
797 | memcpy(mfData, cmd, cmdsize); | |
798 | mf_crypto1_decrypt(traceCrypto1, mfData, cmdsize, 0); | |
799 | *mfDataLen = cmdsize; | |
800 | } | |
801 | ||
802 | return *mfDataLen > 0; | |
803 | } | |
804 | ||
805 | ||
806 | bool is_last_record(uint16_t tracepos, uint8_t *trace, uint16_t traceLen) { | |
807 | return(tracepos + sizeof(uint32_t) + sizeof(uint16_t) + sizeof(uint16_t) >= traceLen); | |
808 | } | |
809 | ||
810 | ||
811 | bool next_record_is_response(uint16_t tracepos, uint8_t *trace) { | |
812 | uint16_t next_records_datalen = *((uint16_t *)(trace + tracepos + sizeof(uint32_t) + sizeof(uint16_t))); | |
813 | return(next_records_datalen & 0x8000); | |
814 | } | |
815 | ||
816 | ||
817 | bool merge_topaz_reader_frames(uint32_t timestamp, uint32_t *duration, uint16_t *tracepos, uint16_t traceLen, uint8_t *trace, uint8_t *frame, uint8_t *topaz_reader_command, uint16_t *data_len) { | |
818 | ||
819 | #define MAX_TOPAZ_READER_CMD_LEN 16 | |
820 | ||
821 | uint32_t last_timestamp = timestamp + *duration; | |
822 | ||
823 | if ((*data_len != 1) || (frame[0] == TOPAZ_WUPA) || (frame[0] == TOPAZ_REQA)) return false; | |
824 | ||
825 | memcpy(topaz_reader_command, frame, *data_len); | |
826 | ||
827 | while (!is_last_record(*tracepos, trace, traceLen) && !next_record_is_response(*tracepos, trace)) { | |
828 | uint32_t next_timestamp = *((uint32_t *)(trace + *tracepos)); | |
829 | *tracepos += sizeof(uint32_t); | |
830 | uint16_t next_duration = *((uint16_t *)(trace + *tracepos)); | |
831 | *tracepos += sizeof(uint16_t); | |
832 | uint16_t next_data_len = *((uint16_t *)(trace + *tracepos)) & 0x7FFF; | |
833 | *tracepos += sizeof(uint16_t); | |
834 | uint8_t *next_frame = (trace + *tracepos); | |
835 | *tracepos += next_data_len; | |
836 | if ((next_data_len == 1) && (*data_len + next_data_len <= MAX_TOPAZ_READER_CMD_LEN)) { | |
837 | memcpy(topaz_reader_command + *data_len, next_frame, next_data_len); | |
838 | *data_len += next_data_len; | |
839 | last_timestamp = next_timestamp + next_duration; | |
840 | } else { | |
841 | // rewind and exit | |
842 | *tracepos = *tracepos - next_data_len - sizeof(uint16_t) - sizeof(uint16_t) - sizeof(uint32_t); | |
843 | break; | |
844 | } | |
845 | uint16_t next_parity_len = (next_data_len-1)/8 + 1; | |
846 | *tracepos += next_parity_len; | |
847 | } | |
848 | ||
849 | *duration = last_timestamp - timestamp; | |
850 | ||
851 | return true; | |
852 | } | |
853 | ||
854 | ||
855 | uint16_t printTraceLine(uint16_t tracepos, uint16_t traceLen, uint8_t *trace, uint8_t protocol, bool showWaitCycles, bool markCRCBytes, uint32_t *prev_EOT, bool times_in_us) { | |
856 | bool isResponse; | |
857 | uint16_t data_len, parity_len; | |
858 | uint32_t duration; | |
859 | uint8_t topaz_reader_command[9]; | |
860 | uint32_t timestamp, first_timestamp; | |
861 | uint32_t EndOfTransmissionTimestamp = 0; | |
862 | char explanation[30] = {0}; | |
863 | uint8_t mfData[32] = {0}; | |
864 | size_t mfDataLen = 0; | |
865 | ||
866 | if (tracepos + sizeof(uint32_t) + sizeof(uint16_t) + sizeof(uint16_t) > traceLen) return traceLen; | |
867 | ||
868 | first_timestamp = *((uint32_t *)(trace)); | |
869 | timestamp = *((uint32_t *)(trace + tracepos)); | |
870 | ||
871 | tracepos += 4; | |
872 | duration = *((uint16_t *)(trace + tracepos)); | |
873 | tracepos += 2; | |
874 | data_len = *((uint16_t *)(trace + tracepos)); | |
875 | tracepos += 2; | |
876 | ||
877 | if (data_len & 0x8000) { | |
878 | data_len &= 0x7fff; | |
879 | isResponse = true; | |
880 | } else { | |
881 | isResponse = false; | |
882 | } | |
883 | parity_len = (data_len-1)/8 + 1; | |
884 | ||
885 | if (tracepos + data_len + parity_len > traceLen) { | |
886 | return traceLen; | |
887 | } | |
888 | uint8_t *frame = trace + tracepos; | |
889 | tracepos += data_len; | |
890 | uint8_t *parityBytes = trace + tracepos; | |
891 | tracepos += parity_len; | |
892 | ||
893 | if (protocol == TOPAZ && !isResponse) { | |
894 | // topaz reader commands come in 1 or 9 separate frames with 7 or 8 Bits each. | |
895 | // merge them: | |
896 | if (merge_topaz_reader_frames(timestamp, &duration, &tracepos, traceLen, trace, frame, topaz_reader_command, &data_len)) { | |
897 | frame = topaz_reader_command; | |
898 | } | |
899 | } | |
900 | ||
901 | // adjust for different time scales | |
902 | if (protocol == ICLASS || protocol == ISO_15693) { | |
903 | duration *= 32; | |
904 | } | |
905 | ||
906 | //Check the CRC status | |
907 | uint8_t crcStatus = 2; | |
908 | ||
909 | if (data_len > 2) { | |
910 | switch (protocol) { | |
911 | case ICLASS: | |
912 | crcStatus = iclass_CRC_check(isResponse, frame, data_len); | |
913 | break; | |
914 | case ISO_14443B: | |
915 | case TOPAZ: | |
916 | crcStatus = iso14443B_CRC_check(isResponse, frame, data_len); | |
917 | break; | |
918 | case PROTO_MIFARE: | |
919 | crcStatus = mifare_CRC_check(isResponse, frame, data_len); | |
920 | break; | |
921 | case ISO_14443A: | |
922 | crcStatus = iso14443A_CRC_check(isResponse, frame, data_len); | |
923 | break; | |
924 | case ISO_14443_4: | |
925 | crcStatus = iso14443_4_CRC_check(frame, data_len); | |
926 | break; | |
927 | case ISO_15693: | |
928 | crcStatus = iso15693_CRC_check(frame, data_len); | |
929 | break; | |
930 | default: | |
931 | break; | |
932 | } | |
933 | } | |
934 | //0 CRC-command, CRC not ok | |
935 | //1 CRC-command, CRC ok | |
936 | //2 Not crc-command | |
937 | ||
938 | //--- Draw the data column | |
939 | char line[16][110]; | |
940 | ||
941 | for (int j = 0; j < data_len && j/16 < 16; j++) { | |
942 | uint8_t parityBits = parityBytes[j>>3]; | |
943 | if (protocol != ISO_14443B | |
944 | && protocol != ISO_15693 | |
945 | && protocol != ICLASS | |
946 | && protocol != ISO_7816_4 | |
947 | && (isResponse || protocol == ISO_14443A) | |
948 | && (oddparity8(frame[j]) != ((parityBits >> (7-(j&0x0007))) & 0x01))) { | |
949 | snprintf(line[j/16]+(( j % 16) * 4), 110, " %02x!", frame[j]); | |
950 | } else { | |
951 | snprintf(line[j/16]+(( j % 16) * 4), 110, " %02x ", frame[j]); | |
952 | } | |
953 | } | |
954 | ||
955 | if (markCRCBytes) { | |
956 | if (crcStatus == 0 || crcStatus == 1) { //CRC-command | |
957 | char *pos1 = line[(data_len-2)/16]+(((data_len-2) % 16) * 4); | |
958 | (*pos1) = '['; | |
959 | char *pos2 = line[(data_len)/16]+(((data_len) % 16) * 4); | |
960 | sprintf(pos2, "%c", ']'); | |
961 | } | |
962 | } | |
963 | ||
964 | // mark short bytes (less than 8 Bit + Parity) | |
965 | if (protocol == ISO_14443A || protocol == PROTO_MIFARE) { | |
966 | if (duration < 128 * (9 * data_len)) { | |
967 | line[(data_len-1)/16][((data_len-1)%16) * 4 + 3] = '\''; | |
968 | } | |
969 | } | |
970 | ||
971 | if (data_len == 0) { | |
972 | if (protocol == ICLASS && duration == 2048) { | |
973 | sprintf(line[0], " <SOF>"); | |
974 | } else if (protocol == ISO_15693 && duration == 512) { | |
975 | sprintf(line[0], " <EOF>"); | |
976 | } else { | |
977 | sprintf(line[0], " <empty trace - possible error>"); | |
978 | } | |
979 | } | |
980 | ||
981 | //--- Draw the CRC column | |
982 | char *crc = (crcStatus == 0 ? "!crc" : (crcStatus == 1 ? " ok " : " ")); | |
983 | ||
984 | if (protocol == PROTO_MIFARE) | |
985 | annotateMifare(explanation, sizeof(explanation), frame, data_len, parityBytes, parity_len, isResponse); | |
986 | ||
987 | if (!isResponse) { | |
988 | switch(protocol) { | |
989 | case ICLASS: annotateIclass(explanation,sizeof(explanation),frame,data_len); break; | |
990 | case ISO_14443A: annotateIso14443a(explanation,sizeof(explanation),frame,data_len); break; | |
991 | case ISO_14443B: annotateIso14443b(explanation,sizeof(explanation),frame,data_len); break; | |
992 | case TOPAZ: annotateTopaz(explanation,sizeof(explanation),frame,data_len); break; | |
993 | case ISO_15693: annotateIso15693(explanation,sizeof(explanation),frame,data_len); break; | |
994 | case ISO_7816_4: annotateIso7816(explanation, sizeof(explanation), frame, data_len); break; | |
995 | case ISO_14443_4: annotateIso14443_4(explanation, sizeof(explanation), frame, data_len); break; | |
996 | default: break; | |
997 | } | |
998 | } | |
999 | ||
1000 | uint32_t previousEndOfTransmissionTimestamp = 0; | |
1001 | if (prev_EOT) { | |
1002 | if (*prev_EOT) { | |
1003 | previousEndOfTransmissionTimestamp = *prev_EOT; | |
1004 | } else { | |
1005 | previousEndOfTransmissionTimestamp = timestamp; | |
1006 | } | |
1007 | } | |
1008 | EndOfTransmissionTimestamp = timestamp + duration; | |
1009 | if (prev_EOT) *prev_EOT = EndOfTransmissionTimestamp; | |
1010 | ||
1011 | int num_lines = MIN((data_len - 1)/16 + 1, 16); | |
1012 | for (int j = 0; j < num_lines ; j++) { | |
1013 | if (j == 0) { | |
1014 | uint32_t time1 = timestamp - first_timestamp; | |
1015 | uint32_t time2 = EndOfTransmissionTimestamp - first_timestamp; | |
1016 | if (prev_EOT) { | |
1017 | time1 = timestamp - previousEndOfTransmissionTimestamp; | |
1018 | time2 = duration; | |
1019 | } | |
1020 | if (times_in_us) { | |
1021 | PrintAndLog(" %10.1f | %10.1f | %s |%-64s | %s| %s", | |
1022 | (float)time1/13.56, | |
1023 | (float)time2/13.56, | |
1024 | isResponse ? "Tag" : "Rdr", | |
1025 | line[j], | |
1026 | (j == num_lines-1) ? crc : " ", | |
1027 | (j == num_lines-1) ? explanation : ""); | |
1028 | } else { | |
1029 | PrintAndLog(" %10" PRIu32 " | %10" PRIu32 " | %s |%-64s | %s| %s", | |
1030 | time1, | |
1031 | time2, | |
1032 | isResponse ? "Tag" : "Rdr", | |
1033 | line[j], | |
1034 | (j == num_lines-1) ? crc : " ", | |
1035 | (j == num_lines-1) ? explanation : ""); | |
1036 | } | |
1037 | } else { | |
1038 | PrintAndLog(" | | |%-64s | %s| %s", | |
1039 | line[j], | |
1040 | (j == num_lines-1) ? crc : " ", | |
1041 | (j == num_lines-1) ? explanation : ""); | |
1042 | } | |
1043 | } | |
1044 | ||
1045 | if (DecodeMifareData(frame, data_len, parityBytes, isResponse, mfData, &mfDataLen)) { | |
1046 | memset(explanation, 0x00, sizeof(explanation)); | |
1047 | if (!isResponse) { | |
1048 | explanation[0] = '>'; | |
1049 | annotateIso14443a(&explanation[1], sizeof(explanation) - 1, mfData, mfDataLen); | |
1050 | } | |
1051 | uint8_t crcc = iso14443A_CRC_check(isResponse, mfData, mfDataLen); | |
1052 | PrintAndLog(" | * | dec |%-64s | %-4s| %s", | |
1053 | sprint_hex(mfData, mfDataLen), | |
1054 | (crcc == 0 ? "!crc" : (crcc == 1 ? " ok " : " ")), | |
1055 | (true) ? explanation : ""); | |
1056 | }; | |
1057 | ||
1058 | if (is_last_record(tracepos, trace, traceLen)) return traceLen; | |
1059 | ||
1060 | if (showWaitCycles && !isResponse && next_record_is_response(tracepos, trace)) { | |
1061 | uint32_t next_timestamp = *((uint32_t *)(trace + tracepos)); | |
1062 | ||
1063 | PrintAndLog(" %10d | %10d | %s | fdt (Frame Delay Time): %d", | |
1064 | (EndOfTransmissionTimestamp - first_timestamp), | |
1065 | (next_timestamp - first_timestamp), | |
1066 | " ", | |
1067 | (next_timestamp - EndOfTransmissionTimestamp)); | |
1068 | } | |
1069 | ||
1070 | return tracepos; | |
1071 | } | |
1072 | ||
1073 | ||
1074 | int CmdHFList(const char *Cmd) { | |
1075 | ||
1076 | CLIParserInit("hf list", "\nList or save protocol data.", | |
1077 | "examples: hf list 14a -f -- interpret as ISO14443A communication and display Frame Delay Times\n"\ | |
1078 | " hf list iclass -- interpret as iClass trace\n"\ | |
1079 | " hf list -s myCardTrace.trc -- save trace for later use\n"\ | |
1080 | " hf list 14a -l myCardTrace.trc -- load trace and interpret as ISO14443A communication\n"); | |
1081 | void* argtable[] = { | |
1082 | arg_param_begin, | |
1083 | arg_lit0("f", "fdt", "display fdt (frame delay times)"), | |
1084 | arg_lit0("r", "relative", "show relative times (gap and duration)"), | |
1085 | arg_lit0("c", "crc" , "mark CRC bytes"), | |
1086 | arg_lit0("p", "pcsc", "show trace buffer from PCSC card reader instead of PM3"), | |
1087 | arg_str0("l", "load", "<filename>", "load trace from file"), | |
1088 | arg_str0("s", "save", "<filename>", "save trace to file"), | |
1089 | arg_lit0("u", "us", "display times in microseconds instead of clock cycles"), | |
1090 | arg_str0(NULL, NULL, "<protocol>", "protocol to interpret. Possible values:\n"\ | |
1091 | "\traw - just show raw data without annotations (default)\n"\ | |
1092 | "\t14a - interpret data as ISO14443A communications\n"\ | |
1093 | "\tmf - interpret data as ISO14443A communications and decrypt Mifare Crypto1 stream\n"\ | |
1094 | "\t14b - interpret data as ISO14443B communications\n"\ | |
1095 | "\t15 - interpret data as ISO15693 communications\n"\ | |
1096 | "\ticlass - interpret data as iClass communications\n"\ | |
1097 | "\ttopaz - interpret data as Topaz communications\n"\ | |
1098 | "\t7816 - interpret data as 7816-4 APDU communications\n"\ | |
1099 | "\t14-4 - interpret data as ISO14443-4 communications"), | |
1100 | arg_param_end | |
1101 | }; | |
1102 | ||
1103 | if (CLIParserParseString(Cmd, argtable, arg_getsize(argtable), true)){ | |
1104 | CLIParserFree(); | |
1105 | return 0; | |
1106 | } | |
1107 | ||
1108 | bool showWaitCycles = arg_get_lit(1); | |
1109 | bool relative_times = arg_get_lit(2); | |
1110 | bool markCRCBytes = arg_get_lit(3); | |
1111 | bool PCSCtrace = arg_get_lit(4); | |
1112 | bool loadFromFile = arg_get_str_len(5); | |
1113 | bool saveToFile = arg_get_str_len(6); | |
1114 | bool times_in_us = arg_get_lit(7); | |
1115 | ||
1116 | uint32_t previous_EOT = 0; | |
1117 | uint32_t *prev_EOT = NULL; | |
1118 | if (relative_times) { | |
1119 | prev_EOT = &previous_EOT; | |
1120 | } | |
1121 | ||
1122 | char load_filename[FILE_PATH_SIZE+1] = {0}; | |
1123 | if (loadFromFile) { | |
1124 | strncpy(load_filename, arg_get_str(5)->sval[0], FILE_PATH_SIZE); | |
1125 | } | |
1126 | char save_filename[FILE_PATH_SIZE+1] = {0}; | |
1127 | if (saveToFile) { | |
1128 | strncpy(save_filename, arg_get_str(6)->sval[0], FILE_PATH_SIZE); | |
1129 | } | |
1130 | ||
1131 | uint8_t protocol = -1; | |
1132 | if (arg_get_str_len(8)) { | |
1133 | if (strcmp(arg_get_str(8)->sval[0], "iclass") == 0) protocol = ICLASS; | |
1134 | else if(strcmp(arg_get_str(8)->sval[0], "14a") == 0) protocol = ISO_14443A; | |
1135 | else if(strcmp(arg_get_str(8)->sval[0], "mf") == 0) protocol = PROTO_MIFARE; | |
1136 | else if(strcmp(arg_get_str(8)->sval[0], "14b") == 0) protocol = ISO_14443B; | |
1137 | else if(strcmp(arg_get_str(8)->sval[0], "topaz") == 0) protocol = TOPAZ; | |
1138 | else if(strcmp(arg_get_str(8)->sval[0], "7816") == 0) protocol = ISO_7816_4; | |
1139 | else if(strcmp(arg_get_str(8)->sval[0], "14-4") == 0) protocol = ISO_14443_4; | |
1140 | else if(strcmp(arg_get_str(8)->sval[0], "15") == 0) protocol = ISO_15693; | |
1141 | else if(strcmp(arg_get_str(8)->sval[0], "raw") == 0) protocol = -1;//No crc, no annotations | |
1142 | else { | |
1143 | PrintAndLog("hf list: invalid argument \"%s\"\nTry 'hf list --help' for more information.", arg_get_str(8)->sval[0]); | |
1144 | CLIParserFree(); | |
1145 | return 0; | |
1146 | } | |
1147 | } | |
1148 | ||
1149 | CLIParserFree(); | |
1150 | ||
1151 | ||
1152 | uint8_t *trace; | |
1153 | uint32_t tracepos = 0; | |
1154 | uint32_t traceLen = 0; | |
1155 | ||
1156 | if (loadFromFile) { | |
1157 | #define TRACE_CHUNK_SIZE (1<<16) // 64K to start with. Will be enough for BigBuf and some room for future extensions | |
1158 | FILE *tracefile = NULL; | |
1159 | size_t bytes_read; | |
1160 | trace = malloc(TRACE_CHUNK_SIZE); | |
1161 | if (trace == NULL) { | |
1162 | PrintAndLog("Cannot allocate memory for trace"); | |
1163 | return 2; | |
1164 | } | |
1165 | if ((tracefile = fopen(load_filename,"rb")) == NULL) { | |
1166 | PrintAndLog("Could not open file %s", load_filename); | |
1167 | free(trace); | |
1168 | return 0; | |
1169 | } | |
1170 | while (!feof(tracefile)) { | |
1171 | bytes_read = fread(trace+traceLen, 1, TRACE_CHUNK_SIZE, tracefile); | |
1172 | traceLen += bytes_read; | |
1173 | if (!feof(tracefile)) { | |
1174 | uint8_t *p = realloc(trace, traceLen + TRACE_CHUNK_SIZE); | |
1175 | if (p == NULL) { | |
1176 | PrintAndLog("Cannot allocate memory for trace"); | |
1177 | free(trace); | |
1178 | fclose(tracefile); | |
1179 | return 2; | |
1180 | } | |
1181 | trace = p; | |
1182 | } | |
1183 | } | |
1184 | fclose(tracefile); | |
1185 | } else if (PCSCtrace) { | |
1186 | trace = pcsc_get_trace_addr(); | |
1187 | traceLen = pcsc_get_traceLen(); | |
1188 | } else { | |
1189 | trace = malloc(USB_CMD_DATA_SIZE); | |
1190 | // Query for the size of the trace | |
1191 | UsbCommand response; | |
1192 | if (!(GetFromBigBuf(trace, USB_CMD_DATA_SIZE, 0, &response, 500, false))) { | |
1193 | return 1; | |
1194 | } | |
1195 | traceLen = response.arg[2]; | |
1196 | if (traceLen > USB_CMD_DATA_SIZE) { | |
1197 | uint8_t *p = realloc(trace, traceLen); | |
1198 | if (p == NULL) { | |
1199 | PrintAndLog("Cannot allocate memory for trace"); | |
1200 | free(trace); | |
1201 | return 2; | |
1202 | } | |
1203 | trace = p; | |
1204 | if (!(GetFromBigBuf(trace, traceLen, 0, NULL, 500, false))) { | |
1205 | return 1; | |
1206 | } | |
1207 | } | |
1208 | } | |
1209 | ||
1210 | if (saveToFile) { | |
1211 | FILE *tracefile = NULL; | |
1212 | if ((tracefile = fopen(save_filename,"wb")) == NULL) { | |
1213 | PrintAndLog("Could not create file %s", save_filename); | |
1214 | return 1; | |
1215 | } | |
1216 | fwrite(trace, 1, traceLen, tracefile); | |
1217 | PrintAndLog("Recorded Activity (TraceLen = %d bytes) written to file %s", traceLen, save_filename); | |
1218 | fclose(tracefile); | |
1219 | } else { | |
1220 | PrintAndLog("Recorded Activity (TraceLen = %d bytes)", traceLen); | |
1221 | PrintAndLog(""); | |
1222 | if (relative_times) { | |
1223 | PrintAndLog("Gap = time between transfers. Duration = duration of data transfer. Src = Source of transfer"); | |
1224 | } else { | |
1225 | PrintAndLog("Start = Start of Frame, End = End of Frame. Src = Source of transfer"); | |
1226 | } | |
1227 | if (times_in_us) { | |
1228 | PrintAndLog("All times are in microseconds"); | |
1229 | } else { | |
1230 | PrintAndLog("All times are in carrier periods (1/13.56Mhz)"); | |
1231 | } | |
1232 | PrintAndLog(""); | |
1233 | if (relative_times) { | |
1234 | PrintAndLog(" Gap | Duration | Src | Data (! denotes parity error, ' denotes short bytes) | CRC | Annotation |"); | |
1235 | } else { | |
1236 | PrintAndLog(" Start | End | Src | Data (! denotes parity error, ' denotes short bytes) | CRC | Annotation |"); | |
1237 | } | |
1238 | PrintAndLog("------------|------------|-----|-----------------------------------------------------------------|-----|--------------------|"); | |
1239 | ||
1240 | ClearAuthData(); | |
1241 | while(tracepos < traceLen) { | |
1242 | tracepos = printTraceLine(tracepos, traceLen, trace, protocol, showWaitCycles, markCRCBytes, prev_EOT, times_in_us); | |
1243 | } | |
1244 | } | |
1245 | ||
1246 | free(trace); | |
1247 | return 0; | |
1248 | } | |
1249 |