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 //-----------------------------------------------------------------------------
17 #include "iso14443crc.h"
21 #include "cmdparser.h"
26 static int CmdHelp(const char *Cmd
);
28 int CmdHF14AList(const char *Cmd
)
31 GetFromBigBuf(got
, sizeof(got
));
33 PrintAndLog("recorded activity:");
34 PrintAndLog(" ETU :rssi: who bytes");
35 PrintAndLog("---------+----+----+-----------");
46 int timestamp
= *((uint32_t *)(got
+i
));
47 if (timestamp
& 0x80000000) {
48 timestamp
&= 0x7fffffff;
55 int parityBits
= *((uint32_t *)(got
+i
+4));
56 // 4 bytes of additional information...
57 // maximum of 32 additional parity bit information
60 // at each quarter bit period we can send power level (16 levels)
61 // or each half bit period in 256 levels.
69 if (i
+ len
>= 1900) {
73 uint8_t *frame
= (got
+i
+9);
75 // Break and stick with current result if buffer was not completely full
76 if (frame
[0] == 0x44 && frame
[1] == 0x44 && frame
[3] == 0x44) { break; }
80 for (j
= 0; j
< len
; j
++) {
85 oddparity
^= (((frame
[j
] & 0xFF) >> k
) & 0x01);
88 //if((parityBits >> (len - j - 1)) & 0x01) {
89 if (isResponse
&& (oddparity
!= ((parityBits
>> (len
- j
- 1)) & 0x01))) {
90 sprintf(line
+(j
*4), "%02x! ", frame
[j
]);
93 sprintf(line
+(j
*4), "%02x ", frame
[j
]);
101 for (j
= 0; j
< (len
- 1); j
++) {
102 // gives problems... search for the reason..
103 /*if(frame[j] == 0xAA) {
106 crc = "[1] Two drops close after each other";
109 crc = "[2] Potential SOC with a drop in second half of bitperiod";
112 crc = "[3] Segment Z after segment X is not possible";
115 crc = "[4] Parity bit of a fully received byte was wrong";
118 crc = "[?] Unknown error";
125 if (strlen(crc
)==0) {
126 ComputeCrc14443(CRC_14443_A
, frame
, len
-2, &b1
, &b2
);
127 if (b1
!= frame
[len
-2] || b2
!= frame
[len
-1]) {
128 crc
= (isResponse
& (len
< 6)) ? "" : " !crc";
137 char metricString
[100];
139 sprintf(metricString
, "%3d", metric
);
141 strcpy(metricString
, " ");
144 PrintAndLog(" +%7d: %s: %s %s %s",
145 (prev
< 0 ? 0 : (timestamp
- prev
)),
147 (isResponse
? "TAG" : " "), line
, crc
);
155 void iso14a_set_timeout(uint32_t timeout
) {
156 UsbCommand c
= {CMD_READER_ISO_14443a
, {ISO14A_SET_TIMEOUT
, 0, timeout
}};
160 int CmdHF14AReader(const char *Cmd
)
162 UsbCommand c
= {CMD_READER_ISO_14443a
, {ISO14A_CONNECT
, 0, 0}};
164 UsbCommand
* resp
= WaitForResponse(CMD_ACK
);
165 uint8_t * uid
= resp
->d
.asBytes
;
166 iso14a_card_select_t
* card
= (iso14a_card_select_t
*)(uid
+ 12);
168 if(resp
->arg
[0] == 0) {
169 PrintAndLog("iso14443a card select failed");
173 PrintAndLog("ATQA : %02x %02x", card
->atqa
[0], card
->atqa
[1]);
174 PrintAndLog(" UID : %s", sprint_hex(uid
, 12));
175 PrintAndLog(" SAK : %02x [%d]", card
->sak
, resp
->arg
[0]);
178 case 0x00: PrintAndLog(" SAK : NXP MIFARE Ultralight | Ultralight C"); break;
179 case 0x04: PrintAndLog(" SAK : NXP MIFARE (various !DESFire !DESFire EV1)"); break;
181 case 0x08: PrintAndLog(" SAK : NXP MIFARE CLASSIC 1k | Plus 2k"); break;
182 case 0x09: PrintAndLog(" SAK : NXP MIFARE Mini 0.3k"); break;
183 case 0x10: PrintAndLog(" SAK : NXP MIFARE Plus 2k"); break;
184 case 0x11: PrintAndLog(" SAK : NXP MIFARE Plus 4k"); break;
185 case 0x18: PrintAndLog(" SAK : NXP MIFARE Classic 4k | Plus 4k"); break;
186 case 0x20: PrintAndLog(" SAK : NXP MIFARE DESFire 4k | DESFire EV1 2k/4k/8k | Plus 2k/4k | JCOP 31/41"); break;
187 case 0x24: PrintAndLog(" SAK : NXP MIFARE DESFire | DESFire EV1"); break;
188 case 0x28: PrintAndLog(" SAK : JCOP31 or JCOP41 v2.3.1"); break;
189 case 0x38: PrintAndLog(" SAK : Nokia 6212 or 6131 MIFARE CLASSIC 4K"); break;
190 case 0x88: PrintAndLog(" SAK : Infineon MIFARE CLASSIC 1K"); break;
191 case 0x98: PrintAndLog(" SAK : Gemplus MPCOS"); break;
194 if(resp
->arg
[0] == 1)
195 PrintAndLog(" ATS : %s", sprint_hex(card
->ats
, card
->ats_len
));
197 PrintAndLog("proprietary non-iso14443a card found, RATS not supported");
202 // ## simulate iso14443a tag
203 // ## greg - added ability to specify tag UID
204 int CmdHF14ASim(const char *Cmd
)
206 UsbCommand c
= {CMD_SIMULATE_TAG_ISO_14443a
,{0,0,0}};
208 // Retrieve the tag type
209 uint8_t tagtype
= param_get8ex(Cmd
,0,0,10);
211 // When no argument was given, just print help message
214 PrintAndLog(" Emulating ISO/IEC 14443 type A tag with 4 or 7 byte UID");
216 PrintAndLog(" syntax: hf 14a sim <type> <uid>");
217 PrintAndLog(" types: 1 = MIFARE Classic");
218 PrintAndLog(" 2 = MIFARE Ultralight");
219 PrintAndLog(" 3 = MIFARE DESFIRE");
220 PrintAndLog(" 4 = ISO/IEC 14443-4");
225 // Store the tag type
228 // Retrieve the full 4 or 7 byte long uid
229 uint64_t long_uid
= param_get64ex(Cmd
,1,0,16);
231 // Are we handling the (optional) second part uid?
232 if (long_uid
> 0xffffffff) {
233 PrintAndLog("Emulating ISO/IEC 14443 type A tag with 7 byte UID (%014llx)",long_uid
);
234 // Store the second part
235 c
.arg
[2] = (long_uid
& 0xffffffff);
237 // Store the first part, ignore the first byte, it is replaced by cascade byte (0x88)
238 c
.arg
[1] = (long_uid
& 0xffffff);
240 PrintAndLog("Emulating ISO/IEC 14443 type A tag with 4 byte UID (%08x)",long_uid
);
241 // Only store the first part
242 c
.arg
[1] = long_uid
& 0xffffffff;
245 // At lease save the mandatory first part of the UID
246 c.arg[0] = long_uid & 0xffffffff;
249 // At lease save the mandatory first part of the UID
250 c.arg[0] = long_uid & 0xffffffff;
253 PrintAndLog("Emulating ISO/IEC 14443 type A tag with UID %01d %08x %08x",c.arg[0],c.arg[1],c.arg[2]);
258 PrintAndLog("Emulating ISO/IEC 14443-3 type A tag with 4 byte UID");
259 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)};
262 PrintAndLog("Emulating ISO/IEC 14443-4 type A tag with 7 byte UID");
265 PrintAndLog("Error: unkown tag type (%d)",c.arg[0]);
266 PrintAndLog("syntax: hf 14a sim <uid>",c.arg[0]);
267 PrintAndLog(" type1: 4 ",c.arg[0]);
274 unsigned int hi = 0, lo = 0;
276 while (sscanf(&Cmd[i++], "%1x", &n ) == 1) {
277 hi= (hi << 4) | (lo >> 28);
278 lo= (lo << 4) | (n & 0xf);
281 // 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)};
282 // PrintAndLog("Emulating ISO/IEC 14443 type A tag with UID %01d %08x %08x",c.arg[0],c.arg[1],c.arg[2]);
287 int CmdHF14ASnoop(const char *Cmd
)
289 UsbCommand c
= {CMD_SNOOP_ISO_14443a
};
294 static command_t CommandTable
[] =
296 {"help", CmdHelp
, 1, "This help"},
297 {"list", CmdHF14AList
, 0, "List ISO 14443a history"},
298 {"reader", CmdHF14AReader
, 0, "Act like an ISO14443 Type A reader"},
299 {"sim", CmdHF14ASim
, 0, "<UID> -- Fake ISO 14443a tag"},
300 {"snoop", CmdHF14ASnoop
, 0, "Eavesdrop ISO 14443 Type A"},
301 {NULL
, NULL
, 0, NULL
}
304 int CmdHF14A(const char *Cmd
)
307 while (WaitForResponseTimeout(CMD_ACK
, 500) != NULL
) ;
310 CmdsParse(CommandTable
, Cmd
);
314 int CmdHelp(const char *Cmd
)
316 CmdsHelp(CommandTable
);