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