1 //-----------------------------------------------------------------------------
3 // Copyright (C) 2010 iZsh <izsh at fail0verflow.com>, Hagen Fritsch
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
8 //-----------------------------------------------------------------------------
9 // High frequency ISO14443A commands
10 //-----------------------------------------------------------------------------
21 #include "util_posix.h"
22 #include "iso14443crc.h"
25 #include "cmdparser.h"
30 #include "mifarehost.h"
31 #include "cliparser/cliparser.h"
32 #include "emv/apduinfo.h"
33 #include "emv/emvcore.h"
36 static int CmdHelp(const char *Cmd
);
37 static int waitCmd(uint8_t iLen
);
39 int CmdHF14AList(const char *Cmd
)
41 PrintAndLog("Deprecated command, use 'hf list 14a' instead");
45 int Hf14443_4aGetCardData(iso14a_card_select_t
* card
) {
46 UsbCommand c
= {CMD_READER_ISO_14443a
, {ISO14A_CONNECT
, 0, 0}};
50 WaitForResponse(CMD_ACK
,&resp
);
52 memcpy(card
, (iso14a_card_select_t
*)resp
.d
.asBytes
, sizeof(iso14a_card_select_t
));
54 uint64_t select_status
= resp
.arg
[0]; // 0: couldn't read, 1: OK, with ATS, 2: OK, no ATS, 3: proprietary Anticollision
56 if(select_status
== 0) {
57 PrintAndLog("E->iso14443a card select failed");
61 if(select_status
== 2) {
62 PrintAndLog("E->Card doesn't support iso14443-4 mode");
66 if(select_status
== 3) {
67 PrintAndLog("E->Card doesn't support standard iso14443-3 anticollision");
68 PrintAndLog("\tATQA : %02x %02x", card
->atqa
[1], card
->atqa
[0]);
72 PrintAndLog(" UID: %s", sprint_hex(card
->uid
, card
->uidlen
));
73 PrintAndLog("ATQA: %02x %02x", card
->atqa
[1], card
->atqa
[0]);
74 PrintAndLog(" SAK: %02x [%" PRIu64
"]", card
->sak
, resp
.arg
[0]);
75 if(card
->ats_len
< 3) { // a valid ATS consists of at least the length byte (TL) and 2 CRC bytes
76 PrintAndLog("E-> Error ATS length(%d) : %s", card
->ats_len
, sprint_hex(card
->ats
, card
->ats_len
));
79 PrintAndLog(" ATS: %s", sprint_hex(card
->ats
, card
->ats_len
));
84 int CmdHF14AReader(const char *Cmd
) {
85 uint32_t cm
= ISO14A_CONNECT
;
86 bool leaveSignalON
= false;
88 CLIParserInit("hf 14a reader", "Executes ISO1443A anticollision-select group of commands.", NULL
);
91 arg_lit0("kK", "keep", "keep the field active after command executed"),
92 arg_lit0("xX", "drop", "just drop the signal field"),
93 arg_lit0("3", NULL
, "ISO14443-3 select only (skip RATS)"),
96 if (CLIParserParseString(Cmd
, argtable
, arg_getsize(argtable
), true)){
101 leaveSignalON
= arg_get_lit(1);
102 if (arg_get_lit(2)) {
103 cm
= cm
- ISO14A_CONNECT
;
105 if (arg_get_lit(3)) {
106 cm
|= ISO14A_NO_RATS
;
112 cm
|= ISO14A_NO_DISCONNECT
;
114 UsbCommand c
= {CMD_READER_ISO_14443a
, {cm
, 0, 0}};
117 if (ISO14A_CONNECT
& cm
) {
119 WaitForResponse(CMD_ACK
,&resp
);
121 iso14a_card_select_t card
;
122 memcpy(&card
, (iso14a_card_select_t
*)resp
.d
.asBytes
, sizeof(iso14a_card_select_t
));
124 uint64_t select_status
= resp
.arg
[0]; // 0: couldn't read, 1: OK, with ATS, 2: OK, no ATS, 3: proprietary Anticollision
126 if(select_status
== 0) {
127 PrintAndLog("iso14443a card select failed");
131 if(select_status
== 3) {
132 PrintAndLog("Card doesn't support standard iso14443-3 anticollision");
133 PrintAndLog("ATQA : %02x %02x", card
.atqa
[1], card
.atqa
[0]);
137 PrintAndLog(" UID : %s", sprint_hex(card
.uid
, card
.uidlen
));
138 PrintAndLog("ATQA : %02x %02x", card
.atqa
[1], card
.atqa
[0]);
139 PrintAndLog(" SAK : %02x [%" PRIu64
"]", card
.sak
, resp
.arg
[0]);
140 if(card
.ats_len
>= 3) { // a valid ATS consists of at least the length byte (TL) and 2 CRC bytes
141 PrintAndLog(" ATS : %s", sprint_hex(card
.ats
, card
.ats_len
));
144 PrintAndLog("Card is selected. You can now start sending commands");
148 if (!leaveSignalON
) {
149 PrintAndLog("Field dropped.");
155 int CmdHF14AInfo(const char *Cmd
)
157 UsbCommand c
= {CMD_READER_ISO_14443a
, {ISO14A_CONNECT
| ISO14A_NO_DISCONNECT
, 0, 0}};
161 WaitForResponse(CMD_ACK
,&resp
);
163 iso14a_card_select_t card
;
164 memcpy(&card
, (iso14a_card_select_t
*)resp
.d
.asBytes
, sizeof(iso14a_card_select_t
));
166 uint64_t select_status
= resp
.arg
[0]; // 0: couldn't read, 1: OK, with ATS, 2: OK, no ATS, 3: proprietary Anticollision
168 if(select_status
== 0) {
169 if (Cmd
[0] != 's') PrintAndLog("iso14443a card select failed");
178 if(select_status
== 3) {
179 PrintAndLog("Card doesn't support standard iso14443-3 anticollision");
180 PrintAndLog("ATQA : %02x %02x", card
.atqa
[1], card
.atqa
[0]);
189 PrintAndLog(" UID : %s", sprint_hex(card
.uid
, card
.uidlen
));
190 PrintAndLog("ATQA : %02x %02x", card
.atqa
[1], card
.atqa
[0]);
191 PrintAndLog(" SAK : %02x [%" PRIu64
"]", card
.sak
, resp
.arg
[0]);
193 bool isMifareClassic
= true;
196 isMifareClassic
= false;
198 //***************************************test****************
205 uint32_t tagT
= GetHF14AMfU_Type();
206 ul_print_type(tagT
, 0);
208 //reconnect for further tests
209 c
.arg
[0] = ISO14A_CONNECT
| ISO14A_NO_DISCONNECT
;
216 WaitForResponse(CMD_ACK
,&resp
);
218 memcpy(&card
, (iso14a_card_select_t
*)resp
.d
.asBytes
, sizeof(iso14a_card_select_t
));
220 select_status
= resp
.arg
[0]; // 0: couldn't read, 1: OK, with ATS, 2: OK, no ATS
222 if(select_status
== 0) {
223 //PrintAndLog("iso14443a card select failed");
233 // check if the tag answers to GETVERSION (0x60)
234 c.arg[0] = ISO14A_RAW | ISO14A_APPEND_CRC | ISO14A_NO_DISCONNECT;
237 c.d.asBytes[0] = 0x60;
239 WaitForResponse(CMD_ACK,&resp);
241 uint8_t version[10] = {0};
242 memcpy(version, resp.d.asBytes, resp.arg[0] < sizeof(version) ? resp.arg[0] : sizeof(version));
243 uint8_t len = resp.arg[0] & 0xff;
245 // todo, identify "Magic UL-C tags". // they usually have a static nonce response to 0x1A command.
246 // UL-EV1, size, check version[6] == 0x0b (smaller) 0x0b * 4 == 48
247 case 0x0A:PrintAndLog("TYPE : NXP MIFARE Ultralight EV1 %d bytes", (version[6] == 0xB) ? 48 : 128);break;
248 case 0x01:PrintAndLog("TYPE : NXP MIFARE Ultralight C");break;
249 case 0x00:PrintAndLog("TYPE : NXP MIFARE Ultralight");break;
253 case 0x01: PrintAndLog("TYPE : NXP TNP3xxx Activision Game Appliance"); break;
254 case 0x04: PrintAndLog("TYPE : NXP MIFARE (various !DESFire !DESFire EV1)"); break;
255 case 0x08: PrintAndLog("TYPE : NXP MIFARE CLASSIC 1k | Plus 2k SL1"); break;
256 case 0x09: PrintAndLog("TYPE : NXP MIFARE Mini 0.3k"); break;
257 case 0x10: PrintAndLog("TYPE : NXP MIFARE Plus 2k SL2"); break;
258 case 0x11: PrintAndLog("TYPE : NXP MIFARE Plus 4k SL2"); break;
259 case 0x18: PrintAndLog("TYPE : NXP MIFARE Classic 4k | Plus 4k SL1"); break;
260 case 0x20: PrintAndLog("TYPE : NXP MIFARE DESFire 4k | DESFire EV1 2k/4k/8k | Plus 2k/4k SL3 | JCOP 31/41"); break;
261 case 0x24: PrintAndLog("TYPE : NXP MIFARE DESFire | DESFire EV1"); break;
262 case 0x28: PrintAndLog("TYPE : JCOP31 or JCOP41 v2.3.1"); break;
263 case 0x38: PrintAndLog("TYPE : Nokia 6212 or 6131 MIFARE CLASSIC 4K"); break;
264 case 0x88: PrintAndLog("TYPE : Infineon MIFARE CLASSIC 1K"); break;
265 case 0x98: PrintAndLog("TYPE : Gemplus MPCOS"); break;
269 // Double & triple sized UID, can be mapped to a manufacturer.
270 // HACK: does this apply for Ultralight cards?
271 if ( card
.uidlen
> 4 ) {
272 PrintAndLog("MANUFACTURER : %s", getManufacturerName(card
.uid
[0]));
275 // try to request ATS even if tag claims not to support it
276 if (select_status
== 2) {
277 uint8_t rats
[] = { 0xE0, 0x80 }; // FSDI=8 (FSD=256), CID=0
278 c
.arg
[0] = ISO14A_RAW
| ISO14A_APPEND_CRC
| ISO14A_NO_DISCONNECT
;
281 memcpy(c
.d
.asBytes
, rats
, 2);
283 WaitForResponse(CMD_ACK
,&resp
);
285 memcpy(card
.ats
, resp
.d
.asBytes
, resp
.arg
[0]);
286 card
.ats_len
= resp
.arg
[0]; // note: ats_len includes CRC Bytes
289 if(card
.ats_len
>= 3) { // a valid ATS consists of at least the length byte (TL) and 2 CRC bytes
290 bool ta1
= 0, tb1
= 0, tc1
= 0;
293 if (select_status
== 2) {
294 PrintAndLog("SAK incorrectly claims that card doesn't support RATS");
296 PrintAndLog(" ATS : %s", sprint_hex(card
.ats
, card
.ats_len
));
297 PrintAndLog(" - TL : length is %d bytes", card
.ats
[0]);
298 if (card
.ats
[0] != card
.ats_len
- 2) {
299 PrintAndLog("ATS may be corrupted. Length of ATS (%d bytes incl. 2 Bytes CRC) doesn't match TL", card
.ats_len
);
302 if (card
.ats
[0] > 1) { // there is a format byte (T0)
303 ta1
= (card
.ats
[1] & 0x10) == 0x10;
304 tb1
= (card
.ats
[1] & 0x20) == 0x20;
305 tc1
= (card
.ats
[1] & 0x40) == 0x40;
306 int16_t fsci
= card
.ats
[1] & 0x0f;
307 PrintAndLog(" - T0 : TA1 is%s present, TB1 is%s present, "
308 "TC1 is%s present, FSCI is %d (FSC = %ld)",
309 (ta1
? "" : " NOT"), (tb1
? "" : " NOT"), (tc1
? "" : " NOT"),
311 fsci
< 5 ? (fsci
- 2) * 8 :
312 fsci
< 8 ? (fsci
- 3) * 32 :
320 dr
[0] = ds
[0] = '\0';
321 if (card
.ats
[pos
] & 0x10) strcat(ds
, "2, ");
322 if (card
.ats
[pos
] & 0x20) strcat(ds
, "4, ");
323 if (card
.ats
[pos
] & 0x40) strcat(ds
, "8, ");
324 if (card
.ats
[pos
] & 0x01) strcat(dr
, "2, ");
325 if (card
.ats
[pos
] & 0x02) strcat(dr
, "4, ");
326 if (card
.ats
[pos
] & 0x04) strcat(dr
, "8, ");
327 if (strlen(ds
) != 0) ds
[strlen(ds
) - 2] = '\0';
328 if (strlen(dr
) != 0) dr
[strlen(dr
) - 2] = '\0';
329 PrintAndLog(" - TA1 : different divisors are%s supported, "
330 "DR: [%s], DS: [%s]",
331 (card
.ats
[pos
] & 0x80 ? " NOT" : ""), dr
, ds
);
335 uint32_t sfgi
= card
.ats
[pos
] & 0x0F;
336 uint32_t fwi
= card
.ats
[pos
] >> 4;
337 PrintAndLog(" - TB1 : SFGI = %d (SFGT = %s%ld/fc), FWI = %d (FWT = %ld/fc)",
339 sfgi
? "" : "(not needed) ",
340 sfgi
? (1 << 12) << sfgi
: 0,
347 PrintAndLog(" - TC1 : NAD is%s supported, CID is%s supported",
348 (card
.ats
[pos
] & 0x01) ? "" : " NOT",
349 (card
.ats
[pos
] & 0x02) ? "" : " NOT");
352 if (card
.ats
[0] > pos
) {
354 if (card
.ats
[0] - pos
>= 7) {
355 if (memcmp(card
.ats
+ pos
, "\xC1\x05\x2F\x2F\x01\xBC\xD6", 7) == 0) {
356 tip
= "-> MIFARE Plus X 2K or 4K";
357 } else if (memcmp(card
.ats
+ pos
, "\xC1\x05\x2F\x2F\x00\x35\xC7", 7) == 0) {
358 tip
= "-> MIFARE Plus S 2K or 4K";
361 PrintAndLog(" - HB : %s%s", sprint_hex(card
.ats
+ pos
, card
.ats
[0] - pos
), tip
);
362 if (card
.ats
[pos
] == 0xC1) {
363 PrintAndLog(" c1 -> Mifare or (multiple) virtual cards of various type");
364 PrintAndLog(" %02x -> Length is %d bytes",
365 card
.ats
[pos
+ 1], card
.ats
[pos
+ 1]);
366 switch (card
.ats
[pos
+ 2] & 0xf0) {
368 PrintAndLog(" 1x -> MIFARE DESFire");
371 PrintAndLog(" 2x -> MIFARE Plus");
374 switch (card
.ats
[pos
+ 2] & 0x0f) {
376 PrintAndLog(" x0 -> <1 kByte");
379 PrintAndLog(" x1 -> 1 kByte");
382 PrintAndLog(" x2 -> 2 kByte");
385 PrintAndLog(" x3 -> 4 kByte");
388 PrintAndLog(" x4 -> 8 kByte");
391 switch (card
.ats
[pos
+ 3] & 0xf0) {
393 PrintAndLog(" 0x -> Engineering sample");
396 PrintAndLog(" 2x -> Released");
399 switch (card
.ats
[pos
+ 3] & 0x0f) {
401 PrintAndLog(" x0 -> Generation 1");
404 PrintAndLog(" x1 -> Generation 2");
407 PrintAndLog(" x2 -> Generation 3");
410 switch (card
.ats
[pos
+ 4] & 0x0f) {
412 PrintAndLog(" x0 -> Only VCSL supported");
415 PrintAndLog(" x1 -> VCS, VCSL, and SVC supported");
418 PrintAndLog(" xE -> no VCS command supported");
424 PrintAndLog("proprietary non iso14443-4 card found, RATS not supported");
428 // try to see if card responses to "chinese magic backdoor" commands.
431 if (isMifareClassic
) {
432 switch(DetectClassicPrng()) {
434 PrintAndLog("Prng detection: HARDENED (hardnested)");
437 PrintAndLog("Prng detection: WEAK");
440 PrintAndLog("Prng detection error.");
444 return select_status
;
447 // Collect ISO14443 Type A UIDs
448 int CmdHF14ACUIDs(const char *Cmd
)
450 // requested number of UIDs
452 // collect at least 1 (e.g. if no parameter was given)
455 PrintAndLog("Collecting %d UIDs", n
);
456 PrintAndLog("Start: %" PRIu64
, msclock()/1000);
458 for (int i
= 0; i
< n
; i
++) {
459 // execute anticollision procedure
460 UsbCommand c
= {CMD_READER_ISO_14443a
, {ISO14A_CONNECT
| ISO14A_NO_RATS
, 0, 0}};
464 WaitForResponse(CMD_ACK
,&resp
);
466 iso14a_card_select_t
*card
= (iso14a_card_select_t
*) resp
.d
.asBytes
;
468 // check if command failed
469 if (resp
.arg
[0] == 0) {
470 PrintAndLog("Card select failed.");
473 for (uint16_t i
= 0; i
< card
->uidlen
; i
++) {
474 sprintf(&uid_string
[2*i
], "%02X", card
->uid
[i
]);
476 PrintAndLog("%s", uid_string
);
479 PrintAndLog("End: %" PRIu64
, msclock()/1000);
484 // ## simulate iso14443a tag
485 // ## greg - added ability to specify tag UID
486 int CmdHF14ASim(const char *Cmd
)
488 UsbCommand c
= {CMD_SIMULATE_TAG_ISO_14443a
,{0,0,0}};
490 // Retrieve the tag type
491 uint8_t tagtype
= param_get8ex(Cmd
,0,0,10);
493 // When no argument was given, just print help message
496 PrintAndLog(" Emulating ISO/IEC 14443 type A tag with 4 or 7 byte UID");
498 PrintAndLog(" syntax: hf 14a sim <type> <uid>");
499 PrintAndLog(" types: 1 = MIFARE Classic");
500 PrintAndLog(" 2 = MIFARE Ultralight");
501 PrintAndLog(" 3 = MIFARE Desfire");
502 PrintAndLog(" 4 = ISO/IEC 14443-4");
503 PrintAndLog(" 5 = MIFARE Tnp3xxx");
508 // Store the tag type
511 // Retrieve the full 4 or 7 byte long uid
512 uint64_t long_uid
= param_get64ex(Cmd
,1,0,16);
514 // Are we handling the (optional) second part uid?
515 if (long_uid
> 0xffffffff) {
516 PrintAndLog("Emulating ISO/IEC 14443 type A tag with 7 byte UID (%014" PRIx64
")",long_uid
);
517 // Store the second part
518 c
.arg
[2] = (long_uid
& 0xffffffff);
520 // Store the first part, ignore the first byte, it is replaced by cascade byte (0x88)
521 c
.arg
[1] = (long_uid
& 0xffffff);
523 PrintAndLog("Emulating ISO/IEC 14443 type A tag with 4 byte UID (%08x)",long_uid
);
524 // Only store the first part
525 c
.arg
[1] = long_uid
& 0xffffffff;
528 // At lease save the mandatory first part of the UID
529 c.arg[0] = long_uid & 0xffffffff;
532 PrintAndLog("Emulating ISO/IEC 14443 type A tag with UID %01d %08x %08x",c.arg[0],c.arg[1],c.arg[2]);
537 PrintAndLog("Emulating ISO/IEC 14443-3 type A tag with 4 byte UID");
538 UsbCommand c = {CMD_SIMULATE_TAG_ISO_14443a,param_get32ex(Cmd,0,0,10),param_get32ex(Cmd,1,0,16),param_get32ex(Cmd,2,0,16)};
541 PrintAndLog("Emulating ISO/IEC 14443-4 type A tag with 7 byte UID");
544 PrintAndLog("Error: unkown tag type (%d)",c.arg[0]);
545 PrintAndLog("syntax: hf 14a sim <uid>",c.arg[0]);
546 PrintAndLog(" type1: 4 ",c.arg[0]);
553 unsigned int hi = 0, lo = 0;
555 while (sscanf(&Cmd[i++], "%1x", &n ) == 1) {
556 hi= (hi << 4) | (lo >> 28);
557 lo= (lo << 4) | (n & 0xf);
560 // UsbCommand c = {CMD_SIMULATE_TAG_ISO_14443a,param_get32ex(Cmd,0,0,10),param_get32ex(Cmd,1,0,16),param_get32ex(Cmd,2,0,16)};
561 // PrintAndLog("Emulating ISO/IEC 14443 type A tag with UID %01d %08x %08x",c.arg[0],c.arg[1],c.arg[2]);
566 int CmdHF14ASnoop(const char *Cmd
) {
569 uint8_t ctmp
= param_getchar(Cmd
, 0) ;
570 if (ctmp
== 'h' || ctmp
== 'H') {
571 PrintAndLog("It get data from the field and saves it into command buffer.");
572 PrintAndLog("Buffer accessible from command hf list 14a.");
573 PrintAndLog("Usage: hf 14a snoop [c][r]");
574 PrintAndLog("c - triggered by first data from card");
575 PrintAndLog("r - triggered by first 7-bit request from reader (REQ,WUP,...)");
576 PrintAndLog("sample: hf 14a snoop c r");
580 for (int i
= 0; i
< 2; i
++) {
581 ctmp
= param_getchar(Cmd
, i
);
582 if (ctmp
== 'c' || ctmp
== 'C') param
|= 0x01;
583 if (ctmp
== 'r' || ctmp
== 'R') param
|= 0x02;
586 UsbCommand c
= {CMD_SNOOP_ISO_14443a
, {param
, 0, 0}};
592 UsbCommand c
= {CMD_READER_ISO_14443a
, {0, 0, 0}};
596 int ExchangeRAW14a(uint8_t *datain
, int datainlen
, bool activateField
, bool leaveSignalON
, uint8_t *dataout
, int maxdataoutlen
, int *dataoutlen
) {
597 static bool responseNum
= false;
605 // Anticollision + SELECT card
606 UsbCommand ca
= {CMD_READER_ISO_14443a
, {ISO14A_CONNECT
| ISO14A_NO_DISCONNECT
| ISO14A_CLEAR_TRACE
, 0, 0}};
608 if (!WaitForResponseTimeout(CMD_ACK
, &resp
, 1500)) {
609 PrintAndLog("14aRAW ERROR: Proxmark connection timeout.");
614 if (resp
.arg
[0] == 0) {
615 PrintAndLog("14aRAW ERROR: No card in field.");
619 if (resp
.arg
[0] != 1 && resp
.arg
[0] != 2) {
620 PrintAndLog("14aRAW ERROR: card not in iso14443-4. res=%d.", resp
.arg
[0]);
624 if (resp
.arg
[0] == 2) { // 0: couldn't read, 1: OK, with ATS, 2: OK, no ATS, 3: proprietary Anticollision
626 UsbCommand cr
= {CMD_READER_ISO_14443a
, {ISO14A_RAW
| ISO14A_APPEND_CRC
| ISO14A_NO_DISCONNECT
, 2, 0}};
627 uint8_t rats
[] = { 0xE0, 0x80 }; // FSDI=8 (FSD=256), CID=0
628 memcpy(cr
.d
.asBytes
, rats
, 2);
630 if (!WaitForResponseTimeout(CMD_ACK
, &resp
, 1500)) {
631 PrintAndLog("14aRAW ERROR: Proxmark connection timeout.");
635 if (resp
.arg
[0] <= 0) { // ats_len
636 PrintAndLog("14aRAW ERROR: Can't get ATS.");
643 cmdc
|= ISO14A_NO_DISCONNECT
;
645 UsbCommand c
= {CMD_READER_ISO_14443a
, {ISO14A_RAW
| ISO14A_APPEND_CRC
| cmdc
, (datainlen
& 0xFFFF) + 2, 0}};
646 uint8_t header
[] = {0x0a | responseNum
, 0x00};
648 memcpy(c
.d
.asBytes
, header
, 2);
649 memcpy(&c
.d
.asBytes
[2], datain
, datainlen
);
655 if (WaitForResponseTimeout(CMD_ACK
, &resp
, 1500)) {
656 recv
= resp
.d
.asBytes
;
657 int iLen
= resp
.arg
[0];
660 PrintAndLog("14aRAW ERROR: No card response.");
664 *dataoutlen
= iLen
- 2;
668 if (maxdataoutlen
&& *dataoutlen
> maxdataoutlen
) {
669 PrintAndLog("14aRAW ERROR: Buffer too small(%d). Needs %d bytes", *dataoutlen
, maxdataoutlen
);
673 if (recv
[0] != header
[0]) {
674 PrintAndLog("14aRAW ERROR: iso14443-4 framing error. Card send %2x must be %2x", dataout
[0], header
[0]);
678 memcpy(dataout
, &recv
[2], *dataoutlen
);
682 PrintAndLog("14aRAW ERROR: ISO 14443A CRC error.");
688 PrintAndLog("14aRAW ERROR: Reply timeout.");
695 int CmdExchangeAPDU(uint8_t *datain
, int datainlen
, bool activateField
, uint8_t *dataout
, int maxdataoutlen
, int *dataoutlen
, bool *chaining
) {
701 cmdc
|= ISO14A_CONNECT
| ISO14A_CLEAR_TRACE
;
704 // "Command APDU" length should be 5+255+1, but javacard's APDU buffer might be smaller - 133 bytes
705 // https://stackoverflow.com/questions/32994936/safe-max-java-card-apdu-data-command-and-respond-size
706 // here length USB_CMD_DATA_SIZE=512
707 // timeout must be authomatically set by "get ATS"
708 UsbCommand c
= {CMD_READER_ISO_14443a
, {ISO14A_APDU
| ISO14A_NO_DISCONNECT
| cmdc
, (datainlen
& 0xFFFF), 0}};
709 memcpy(c
.d
.asBytes
, datain
, datainlen
);
716 if (!WaitForResponseTimeout(CMD_ACK
, &resp
, 1500)) {
717 PrintAndLog("APDU ERROR: Proxmark connection timeout.");
720 if (resp
.arg
[0] != 1) {
721 PrintAndLog("APDU ERROR: Proxmark error %d.", resp
.arg
[0]);
727 if (WaitForResponseTimeout(CMD_ACK
, &resp
, 1500)) {
728 recv
= resp
.d
.asBytes
;
729 int iLen
= resp
.arg
[0];
730 uint8_t res
= resp
.arg
[1];
737 if (maxdataoutlen
&& *dataoutlen
> maxdataoutlen
) {
738 PrintAndLog("APDU ERROR: Buffer too small(%d). Needs %d bytes", *dataoutlen
, maxdataoutlen
);
743 PrintAndLog("APDU ERROR: No APDU response.");
748 if (iLen
< 4 && iLen
>= 0) {
749 PrintAndLog("APDU ERROR: Small APDU response. Len=%d", iLen
);
755 PrintAndLog("APDU ERROR: Block type mismatch.");
759 memcpy(dataout
, recv
, dlen
);
762 if ((res
& 0x10) != 0) {
768 PrintAndLog("APDU ERROR: ISO 14443A CRC error.");
772 PrintAndLog("APDU ERROR: Reply timeout.");
780 int ExchangeAPDU14a(uint8_t *datain
, int datainlen
, bool activateField
, bool leaveSignalON
, uint8_t *dataout
, int maxdataoutlen
, int *dataoutlen
) {
782 bool chaining
= false;
784 int res
= CmdExchangeAPDU(datain
, datainlen
, activateField
, dataout
, maxdataoutlen
, dataoutlen
, &chaining
);
787 // I-block with chaining
788 res
= CmdExchangeAPDU(NULL
, 0, false, &dataout
[*dataoutlen
], maxdataoutlen
, dataoutlen
, &chaining
);
804 // ISO14443-4. 7. Half-duplex block transmission protocol
805 int CmdHF14AAPDU(const char *cmd
) {
806 uint8_t data
[USB_CMD_DATA_SIZE
];
808 bool activateField
= false;
809 bool leaveSignalON
= false;
810 bool decodeTLV
= false;
812 CLIParserInit("hf 14a apdu",
813 "Sends an ISO 7816-4 APDU via ISO 14443-4 block transmission protocol (T=CL)",
814 "Sample:\n\thf 14a apdu -st 00A404000E325041592E5359532E444446303100\n");
818 arg_lit0("sS", "select", "activate field and select card"),
819 arg_lit0("kK", "keep", "leave the signal field ON after receive response"),
820 arg_lit0("tT", "tlv", "executes TLV decoder if it possible"),
821 arg_strx1(NULL
, NULL
, "<APDU (hex)>", NULL
),
824 CLIExecWithReturn(cmd
, argtable
, false);
826 activateField
= arg_get_lit(1);
827 leaveSignalON
= arg_get_lit(2);
828 decodeTLV
= arg_get_lit(3);
829 // len = data + PCB(1b) + CRC(2b)
830 CLIGetHexBLessWithReturn(4, data
, &datalen
, 1 + 2);
834 // PrintAndLog("---str [%d] %s", arg_get_str(4)->count, arg_get_str(4)->sval[0]);
835 PrintAndLog(">>>>[%s%s%s] %s", activateField
? "sel ": "", leaveSignalON
? "keep ": "", decodeTLV
? "TLV": "", sprint_hex(data
, datalen
));
837 int res
= ExchangeAPDU14a(data
, datalen
, activateField
, leaveSignalON
, data
, USB_CMD_DATA_SIZE
, &datalen
);
842 PrintAndLog("<<<< %s", sprint_hex(data
, datalen
));
844 PrintAndLog("APDU response: %02x %02x - %s", data
[datalen
- 2], data
[datalen
- 1], GetAPDUCodeDescription(data
[datalen
- 2], data
[datalen
- 1]));
847 if (decodeTLV
&& datalen
> 4) {
848 TLVPrintFromBuffer(data
, datalen
- 2);
854 int CmdHF14ACmdRaw(const char *cmd
) {
855 UsbCommand c
= {CMD_READER_ISO_14443a
, {0, 0, 0}};
860 bool active_select
= false;
861 bool no_rats
= false;
862 uint16_t numbits
= 0;
863 bool bTimeout
= false;
864 uint32_t timeout
= 0;
865 bool topazmode
= false;
866 uint8_t data
[USB_CMD_DATA_SIZE
];
869 // extract parameters
870 CLIParserInit("hf 14a raw", "Send raw hex data to tag",
872 "\thf 14a raw -pa -b7 -t1000 52 -- execute WUPA\n"\
873 "\thf 14a raw -p 9320 -- anticollision\n"\
874 "\thf 14a raw -psc 60 00 -- select and mifare AUTH\n");
877 arg_lit0("rR", "nreply", "do not read response"),
878 arg_lit0("cC", "crc", "calculate and append CRC"),
879 arg_lit0("pP", "power", "leave the signal field ON after receive"),
880 arg_lit0("aA", "active", "active signal field ON without select"),
881 arg_lit0("sS", "actives", "active signal field ON with select"),
882 arg_int0("bB", "bits", NULL
, "number of bits to send. Useful for send partial byte"),
883 arg_int0("t", "timeout", NULL
, "timeout in ms"),
884 arg_lit0("T", "topaz", "use Topaz protocol to send command"),
885 arg_lit0("3", NULL
, "ISO14443-3 select only (skip RATS)"),
886 arg_strx1(NULL
, NULL
, "<data (hex)>", NULL
),
893 if (CLIParserParseString(cmd
, argtable
, arg_getsize(argtable
), false)){
898 reply
= !arg_get_lit(1);
899 crc
= arg_get_lit(2);
900 power
= arg_get_lit(3);
901 active
= arg_get_lit(4);
902 active_select
= arg_get_lit(5);
903 numbits
= arg_get_int(6) & 0xFFFF;
904 timeout
= arg_get_int(7);
905 bTimeout
= (timeout
> 0);
906 topazmode
= arg_get_lit(8);
907 no_rats
= arg_get_lit(9);
908 // len = data + CRC(2b)
909 if (CLIParamHexToBuf(arg_get_str(10), data
, sizeof(data
) -2, &datalen
)) {
917 if(crc
&& datalen
>0 && datalen
<sizeof(data
)-2)
919 uint8_t first
, second
;
921 ComputeCrc14443(CRC_14443_B
, data
, datalen
, &first
, &second
);
923 ComputeCrc14443(CRC_14443_A
, data
, datalen
, &first
, &second
);
925 data
[datalen
++] = first
;
926 data
[datalen
++] = second
;
929 if(active
|| active_select
)
931 c
.arg
[0] |= ISO14A_CONNECT
| ISO14A_CLEAR_TRACE
;
933 c
.arg
[0] |= ISO14A_NO_SELECT
;
937 #define MAX_TIMEOUT 40542464 // = (2^32-1) * (8*16) / 13560000Hz * 1000ms/s
938 c
.arg
[0] |= ISO14A_SET_TIMEOUT
;
939 if(timeout
> MAX_TIMEOUT
) {
940 timeout
= MAX_TIMEOUT
;
941 PrintAndLog("Set timeout to 40542 seconds (11.26 hours). The max we can wait for response");
943 c
.arg
[2] = 13560000 / 1000 / (8*16) * timeout
; // timeout in ETUs (time to transfer 1 bit, approx. 9.4 us)
947 c
.arg
[0] |= ISO14A_NO_DISCONNECT
;
951 c
.arg
[0] |= ISO14A_RAW
;
955 c
.arg
[0] |= ISO14A_TOPAZMODE
;
959 c
.arg
[0] |= ISO14A_NO_RATS
;
962 // Max buffer is USB_CMD_DATA_SIZE (512)
963 c
.arg
[1] = (datalen
& 0xFFFF) | ((uint32_t)numbits
<< 16);
964 memcpy(c
.d
.asBytes
,data
,datalen
);
972 if (!res
&& datalen
> 0)
979 static int waitCmd(uint8_t iSelect
) {
984 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1500)) {
985 recv
= resp
.d
.asBytes
;
986 uint8_t iLen
= resp
.arg
[0];
990 PrintAndLog("Card selected. UID[%i]:", iLen
);
992 PrintAndLog("Can't select card.");
995 PrintAndLog("received %i bytes:", iLen
);
999 hexout
= (char *)malloc(iLen
* 3 + 1);
1000 if (hexout
!= NULL
) {
1001 for (int i
= 0; i
< iLen
; i
++) { // data in hex
1002 sprintf(&hexout
[i
* 3], "%02X ", recv
[i
]);
1004 PrintAndLog("%s", hexout
);
1007 PrintAndLog("malloc failed your client has low memory?");
1011 PrintAndLog("timeout while waiting for reply.");
1017 static command_t CommandTable
[] =
1019 {"help", CmdHelp
, 1, "This help"},
1020 {"list", CmdHF14AList
, 0, "[Deprecated] List ISO 14443a history"},
1021 {"reader", CmdHF14AReader
, 0, "Start acting like an ISO14443 Type A reader"},
1022 {"info", CmdHF14AInfo
, 0, "Reads card and shows information about it"},
1023 {"cuids", CmdHF14ACUIDs
, 0, "<n> Collect n>0 ISO14443 Type A UIDs in one go"},
1024 {"sim", CmdHF14ASim
, 0, "<UID> -- Simulate ISO 14443a tag"},
1025 {"snoop", CmdHF14ASnoop
, 0, "Eavesdrop ISO 14443 Type A"},
1026 {"apdu", CmdHF14AAPDU
, 0, "Send an ISO 7816-4 APDU via ISO 14443-4 block transmission protocol"},
1027 {"raw", CmdHF14ACmdRaw
, 0, "Send raw hex data to tag"},
1028 {NULL
, NULL
, 0, NULL
}
1031 int CmdHF14A(const char *Cmd
) {
1032 (void)WaitForResponseTimeout(CMD_ACK
,NULL
,100);
1033 CmdsParse(CommandTable
, Cmd
);
1037 int CmdHelp(const char *Cmd
)
1039 CmdsHelp(CommandTable
);