]> cvs.zerfleddert.de Git - proxmark3-svn/blame - client/cmdhf14a.c
OSX: delete another reference to readline
[proxmark3-svn] / client / cmdhf14a.c
CommitLineData
a553f267 1//-----------------------------------------------------------------------------
f89c7050 2// 2011, Merlok
534983d7 3// Copyright (C) 2010 iZsh <izsh at fail0verflow.com>, Hagen Fritsch
a553f267 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// High frequency ISO14443A commands
10//-----------------------------------------------------------------------------
11
7fe9b0b7 12#include <stdio.h>
590f8ff9 13#include <stdlib.h>
43534cba 14#include <inttypes.h>
7fe9b0b7 15#include <string.h>
f397b5cc 16#include <unistd.h>
20f9a2a1 17#include "util.h"
ec9c7112 18#include "util_posix.h"
7fe9b0b7 19#include "iso14443crc.h"
20#include "data.h"
902cb3c0 21#include "proxmark3.h"
7fe9b0b7 22#include "ui.h"
23#include "cmdparser.h"
24#include "cmdhf14a.h"
534983d7 25#include "common.h"
20f9a2a1 26#include "cmdmain.h"
902cb3c0 27#include "mifare.h"
8ceb6b03 28#include "cmdhfmfu.h"
7fe9b0b7 29
30static int CmdHelp(const char *Cmd);
5f6d6c90 31static void waitCmd(uint8_t iLen);
7fe9b0b7 32
52ab55ab 33// structure and database for uid -> tagtype lookups
34typedef struct {
35 uint8_t uid;
36 char* desc;
37} manufactureName;
38
39const manufactureName manufactureMapping[] = {
40 // ID, "Vendor Country"
41 { 0x01, "Motorola UK" },
42 { 0x02, "ST Microelectronics SA France" },
43 { 0x03, "Hitachi, Ltd Japan" },
44 { 0x04, "NXP Semiconductors Germany" },
45 { 0x05, "Infineon Technologies AG Germany" },
46 { 0x06, "Cylink USA" },
47 { 0x07, "Texas Instrument France" },
48 { 0x08, "Fujitsu Limited Japan" },
49 { 0x09, "Matsushita Electronics Corporation, Semiconductor Company Japan" },
50 { 0x0A, "NEC Japan" },
51 { 0x0B, "Oki Electric Industry Co. Ltd Japan" },
52 { 0x0C, "Toshiba Corp. Japan" },
53 { 0x0D, "Mitsubishi Electric Corp. Japan" },
54 { 0x0E, "Samsung Electronics Co. Ltd Korea" },
55 { 0x0F, "Hynix / Hyundai, Korea" },
56 { 0x10, "LG-Semiconductors Co. Ltd Korea" },
57 { 0x11, "Emosyn-EM Microelectronics USA" },
58 { 0x12, "INSIDE Technology France" },
59 { 0x13, "ORGA Kartensysteme GmbH Germany" },
60 { 0x14, "SHARP Corporation Japan" },
61 { 0x15, "ATMEL France" },
62 { 0x16, "EM Microelectronic-Marin SA Switzerland" },
63 { 0x17, "KSW Microtec GmbH Germany" },
64 { 0x18, "ZMD AG Germany" },
65 { 0x19, "XICOR, Inc. USA" },
66 { 0x1A, "Sony Corporation Japan Identifier Company Country" },
67 { 0x1B, "Malaysia Microelectronic Solutions Sdn. Bhd Malaysia" },
68 { 0x1C, "Emosyn USA" },
69 { 0x1D, "Shanghai Fudan Microelectronics Co. Ltd. P.R. China" },
70 { 0x1E, "Magellan Technology Pty Limited Australia" },
71 { 0x1F, "Melexis NV BO Switzerland" },
72 { 0x20, "Renesas Technology Corp. Japan" },
73 { 0x21, "TAGSYS France" },
74 { 0x22, "Transcore USA" },
75 { 0x23, "Shanghai belling corp., ltd. China" },
76 { 0x24, "Masktech Germany Gmbh Germany" },
77 { 0x25, "Innovision Research and Technology Plc UK" },
78 { 0x26, "Hitachi ULSI Systems Co., Ltd. Japan" },
79 { 0x27, "Cypak AB Sweden" },
80 { 0x28, "Ricoh Japan" },
81 { 0x29, "ASK France" },
82 { 0x2A, "Unicore Microsystems, LLC Russian Federation" },
83 { 0x2B, "Dallas Semiconductor/Maxim USA" },
84 { 0x2C, "Impinj, Inc. USA" },
85 { 0x2D, "RightPlug Alliance USA" },
86 { 0x2E, "Broadcom Corporation USA" },
87 { 0x2F, "MStar Semiconductor, Inc Taiwan, ROC" },
88 { 0x30, "BeeDar Technology Inc. USA" },
89 { 0x31, "RFIDsec Denmark" },
90 { 0x32, "Schweizer Electronic AG Germany" },
91 { 0x33, "AMIC Technology Corp Taiwan" },
92 { 0x34, "Mikron JSC Russia" },
93 { 0x35, "Fraunhofer Institute for Photonic Microsystems Germany" },
94 { 0x36, "IDS Microchip AG Switzerland" },
95 { 0x37, "Kovio USA" },
96 { 0x38, "HMT Microelectronic Ltd Switzerland Identifier Company Country" },
97 { 0x39, "Silicon Craft Technology Thailand" },
98 { 0x3A, "Advanced Film Device Inc. Japan" },
99 { 0x3B, "Nitecrest Ltd UK" },
100 { 0x3C, "Verayo Inc. USA" },
101 { 0x3D, "HID Global USA" },
102 { 0x3E, "Productivity Engineering Gmbh Germany" },
103 { 0x3F, "Austriamicrosystems AG (reserved) Austria" },
104 { 0x40, "Gemalto SA France" },
105 { 0x41, "Renesas Electronics Corporation Japan" },
106 { 0x42, "3Alogics Inc Korea" },
107 { 0x43, "Top TroniQ Asia Limited Hong Kong" },
108 { 0x44, "Gentag Inc (USA) USA" },
109 { 0x00, "no tag-info available" } // must be the last entry
110};
111
112
113// get a product description based on the UID
114// uid[8] tag uid
115// returns description of the best match
b915fda3 116char* getTagInfo(uint8_t uid) {
52ab55ab 117
68033ed7 118 int i;
52ab55ab 119 int len = sizeof(manufactureMapping) / sizeof(manufactureName);
120
68033ed7
MHS
121 for ( i = 0; i < len; ++i )
122 if ( uid == manufactureMapping[i].uid)
123 return manufactureMapping[i].desc;
52ab55ab 124
68033ed7
MHS
125 //No match, return default
126 return manufactureMapping[len-1].desc;
52ab55ab 127}
128
7fe9b0b7 129int CmdHF14AList(const char *Cmd)
130{
4c3de57a 131 PrintAndLog("Deprecated command, use 'hf list 14a' instead");
f89c7050 132 return 0;
7fe9b0b7 133}
134
7fe9b0b7 135int CmdHF14AReader(const char *Cmd)
136{
19d6d91f 137 UsbCommand c = {CMD_READER_ISO_14443a, {ISO14A_CONNECT | ISO14A_NO_DISCONNECT, 0, 0}};
534983d7 138 SendCommand(&c);
902cb3c0 139
140 UsbCommand resp;
7bc95e2e 141 WaitForResponse(CMD_ACK,&resp);
902cb3c0 142
19d6d91f 143 iso14a_card_select_t card;
144 memcpy(&card, (iso14a_card_select_t *)resp.d.asBytes, sizeof(iso14a_card_select_t));
534983d7 145
ee1eadee 146 uint64_t select_status = resp.arg[0]; // 0: couldn't read, 1: OK, with ATS, 2: OK, no ATS, 3: proprietary Anticollision
19d6d91f 147
148 if(select_status == 0) {
6ce0e538 149 if (Cmd[0] != 's') PrintAndLog("iso14443a card select failed");
52bfb955 150 // disconnect
151 c.arg[0] = 0;
152 c.arg[1] = 0;
153 c.arg[2] = 0;
154 SendCommand(&c);
534983d7 155 return 0;
156 }
157
ee1eadee 158 if(select_status == 3) {
159 PrintAndLog("Card doesn't support standard iso14443-3 anticollision");
160 PrintAndLog("ATQA : %02x %02x", card.atqa[1], card.atqa[0]);
161 // disconnect
162 c.arg[0] = 0;
163 c.arg[1] = 0;
164 c.arg[2] = 0;
165 SendCommand(&c);
166 return 0;
167 }
168
19d6d91f 169 PrintAndLog(" UID : %s", sprint_hex(card.uid, card.uidlen));
4745afb6 170 PrintAndLog("ATQA : %02x %02x", card.atqa[1], card.atqa[0]);
19d6d91f 171 PrintAndLog(" SAK : %02x [%d]", card.sak, resp.arg[0]);
713e7ffb 172
19d6d91f 173 switch (card.sak) {
4745afb6 174 case 0x00:
8ceb6b03 175
176 //***************************************test****************
177 // disconnect
178 c.arg[0] = 0;
179 c.arg[1] = 0;
180 c.arg[2] = 0;
181 SendCommand(&c);
182
c7442b76 183 uint32_t tagT = GetHF14AMfU_Type();
8ceb6b03 184 ul_print_type(tagT, 0);
185
186 //reconnect for further tests
187 c.arg[0] = ISO14A_CONNECT | ISO14A_NO_DISCONNECT;
188 c.arg[1] = 0;
189 c.arg[2] = 0;
190
191 SendCommand(&c);
192
193 UsbCommand resp;
194 WaitForResponse(CMD_ACK,&resp);
195
196 memcpy(&card, (iso14a_card_select_t *)resp.d.asBytes, sizeof(iso14a_card_select_t));
197
198 select_status = resp.arg[0]; // 0: couldn't read, 1: OK, with ATS, 2: OK, no ATS
199
200 if(select_status == 0) {
201 //PrintAndLog("iso14443a card select failed");
202 // disconnect
203 c.arg[0] = 0;
204 c.arg[1] = 0;
205 c.arg[2] = 0;
206 SendCommand(&c);
207 return 0;
208 }
209
210 /* orig
4745afb6 211 // check if the tag answers to GETVERSION (0x60)
212 c.arg[0] = ISO14A_RAW | ISO14A_APPEND_CRC | ISO14A_NO_DISCONNECT;
213 c.arg[1] = 1;
214 c.arg[2] = 0;
215 c.d.asBytes[0] = 0x60;
216 SendCommand(&c);
217 WaitForResponse(CMD_ACK,&resp);
218
4b360379
MHS
219 uint8_t version[10] = {0};
220 memcpy(version, resp.d.asBytes, resp.arg[0] < sizeof(version) ? resp.arg[0] : sizeof(version));
4745afb6 221 uint8_t len = resp.arg[0] & 0xff;
222 switch ( len ){
223 // todo, identify "Magic UL-C tags". // they usually have a static nonce response to 0x1A command.
224 // UL-EV1, size, check version[6] == 0x0b (smaller) 0x0b * 4 == 48
225 case 0x0A:PrintAndLog("TYPE : NXP MIFARE Ultralight EV1 %d bytes", (version[6] == 0xB) ? 48 : 128);break;
226 case 0x01:PrintAndLog("TYPE : NXP MIFARE Ultralight C");break;
227 case 0x00:PrintAndLog("TYPE : NXP MIFARE Ultralight");break;
228 }
8ceb6b03 229 */
4745afb6 230 break;
3fe4ff4f 231 case 0x01: PrintAndLog("TYPE : NXP TNP3xxx Activision Game Appliance"); break;
79a73ab2 232 case 0x04: PrintAndLog("TYPE : NXP MIFARE (various !DESFire !DESFire EV1)"); break;
e691fc45 233 case 0x08: PrintAndLog("TYPE : NXP MIFARE CLASSIC 1k | Plus 2k SL1"); break;
79a73ab2 234 case 0x09: PrintAndLog("TYPE : NXP MIFARE Mini 0.3k"); break;
e691fc45 235 case 0x10: PrintAndLog("TYPE : NXP MIFARE Plus 2k SL2"); break;
236 case 0x11: PrintAndLog("TYPE : NXP MIFARE Plus 4k SL2"); break;
237 case 0x18: PrintAndLog("TYPE : NXP MIFARE Classic 4k | Plus 4k SL1"); break;
238 case 0x20: PrintAndLog("TYPE : NXP MIFARE DESFire 4k | DESFire EV1 2k/4k/8k | Plus 2k/4k SL3 | JCOP 31/41"); break;
79a73ab2 239 case 0x24: PrintAndLog("TYPE : NXP MIFARE DESFire | DESFire EV1"); break;
240 case 0x28: PrintAndLog("TYPE : JCOP31 or JCOP41 v2.3.1"); break;
241 case 0x38: PrintAndLog("TYPE : Nokia 6212 or 6131 MIFARE CLASSIC 4K"); break;
242 case 0x88: PrintAndLog("TYPE : Infineon MIFARE CLASSIC 1K"); break;
243 case 0x98: PrintAndLog("TYPE : Gemplus MPCOS"); break;
9ca155ba
M
244 default: ;
245 }
19d6d91f 246
4745afb6 247 // Double & triple sized UID, can be mapped to a manufacturer.
248 // HACK: does this apply for Ultralight cards?
249 if ( card.uidlen > 4 ) {
250 PrintAndLog("MANUFACTURER : %s", getTagInfo(card.uid[0]));
251 }
252
19d6d91f 253 // try to request ATS even if tag claims not to support it
254 if (select_status == 2) {
255 uint8_t rats[] = { 0xE0, 0x80 }; // FSDI=8 (FSD=256), CID=0
256 c.arg[0] = ISO14A_RAW | ISO14A_APPEND_CRC | ISO14A_NO_DISCONNECT;
257 c.arg[1] = 2;
258 c.arg[2] = 0;
259 memcpy(c.d.asBytes, rats, 2);
260 SendCommand(&c);
261 WaitForResponse(CMD_ACK,&resp);
262
4b360379 263 memcpy(card.ats, resp.d.asBytes, resp.arg[0]);
9a573554 264 card.ats_len = resp.arg[0]; // note: ats_len includes CRC Bytes
19d6d91f 265 }
266
9a573554 267 if(card.ats_len >= 3) { // a valid ATS consists of at least the length byte (TL) and 2 CRC bytes
561f7c11 268 bool ta1 = 0, tb1 = 0, tc1 = 0;
269 int pos;
270
9a573554 271 if (select_status == 2) {
19d6d91f 272 PrintAndLog("SAK incorrectly claims that card doesn't support RATS");
273 }
274 PrintAndLog(" ATS : %s", sprint_hex(card.ats, card.ats_len));
9a573554 275 PrintAndLog(" - TL : length is %d bytes", card.ats[0]);
276 if (card.ats[0] != card.ats_len - 2) {
277 PrintAndLog("ATS may be corrupted. Length of ATS (%d bytes incl. 2 Bytes CRC) doesn't match TL", card.ats_len);
561f7c11 278 }
9a573554 279
280 if (card.ats[0] > 1) { // there is a format byte (T0)
19d6d91f 281 ta1 = (card.ats[1] & 0x10) == 0x10;
282 tb1 = (card.ats[1] & 0x20) == 0x20;
283 tc1 = (card.ats[1] & 0x40) == 0x40;
9a573554 284 int16_t fsci = card.ats[1] & 0x0f;
561f7c11 285 PrintAndLog(" - T0 : TA1 is%s present, TB1 is%s present, "
9a573554 286 "TC1 is%s present, FSCI is %d (FSC = %ld)",
561f7c11 287 (ta1 ? "" : " NOT"), (tb1 ? "" : " NOT"), (tc1 ? "" : " NOT"),
9a573554 288 fsci,
289 fsci < 5 ? (fsci - 2) * 8 :
290 fsci < 8 ? (fsci - 3) * 32 :
291 fsci == 8 ? 256 :
292 -1
293 );
561f7c11 294 }
295 pos = 2;
9a573554 296 if (ta1) {
561f7c11 297 char dr[16], ds[16];
298 dr[0] = ds[0] = '\0';
19d6d91f 299 if (card.ats[pos] & 0x10) strcat(ds, "2, ");
300 if (card.ats[pos] & 0x20) strcat(ds, "4, ");
301 if (card.ats[pos] & 0x40) strcat(ds, "8, ");
302 if (card.ats[pos] & 0x01) strcat(dr, "2, ");
303 if (card.ats[pos] & 0x02) strcat(dr, "4, ");
304 if (card.ats[pos] & 0x04) strcat(dr, "8, ");
561f7c11 305 if (strlen(ds) != 0) ds[strlen(ds) - 2] = '\0';
306 if (strlen(dr) != 0) dr[strlen(dr) - 2] = '\0';
307 PrintAndLog(" - TA1 : different divisors are%s supported, "
308 "DR: [%s], DS: [%s]",
19d6d91f 309 (card.ats[pos] & 0x80 ? " NOT" : ""), dr, ds);
561f7c11 310 pos++;
311 }
9a573554 312 if (tb1) {
313 uint32_t sfgi = card.ats[pos] & 0x0F;
314 uint32_t fwi = card.ats[pos] >> 4;
315 PrintAndLog(" - TB1 : SFGI = %d (SFGT = %s%ld/fc), FWI = %d (FWT = %ld/fc)",
316 (sfgi),
317 sfgi ? "" : "(not needed) ",
318 sfgi ? (1 << 12) << sfgi : 0,
319 fwi,
320 (1 << 12) << fwi
321 );
561f7c11 322 pos++;
323 }
9a573554 324 if (tc1) {
561f7c11 325 PrintAndLog(" - TC1 : NAD is%s supported, CID is%s supported",
19d6d91f 326 (card.ats[pos] & 0x01) ? "" : " NOT",
327 (card.ats[pos] & 0x02) ? "" : " NOT");
561f7c11 328 pos++;
329 }
9a573554 330 if (card.ats[0] > pos) {
561f7c11 331 char *tip = "";
9a573554 332 if (card.ats[0] - pos >= 7) {
19d6d91f 333 if (memcmp(card.ats + pos, "\xC1\x05\x2F\x2F\x01\xBC\xD6", 7) == 0) {
561f7c11 334 tip = "-> MIFARE Plus X 2K or 4K";
19d6d91f 335 } else if (memcmp(card.ats + pos, "\xC1\x05\x2F\x2F\x00\x35\xC7", 7) == 0) {
561f7c11 336 tip = "-> MIFARE Plus S 2K or 4K";
337 }
338 }
9a573554 339 PrintAndLog(" - HB : %s%s", sprint_hex(card.ats + pos, card.ats[0] - pos), tip);
19d6d91f 340 if (card.ats[pos] == 0xC1) {
561f7c11 341 PrintAndLog(" c1 -> Mifare or (multiple) virtual cards of various type");
342 PrintAndLog(" %02x -> Length is %d bytes",
19d6d91f 343 card.ats[pos + 1], card.ats[pos + 1]);
344 switch (card.ats[pos + 2] & 0xf0) {
561f7c11 345 case 0x10:
346 PrintAndLog(" 1x -> MIFARE DESFire");
347 break;
348 case 0x20:
349 PrintAndLog(" 2x -> MIFARE Plus");
350 break;
351 }
19d6d91f 352 switch (card.ats[pos + 2] & 0x0f) {
561f7c11 353 case 0x00:
354 PrintAndLog(" x0 -> <1 kByte");
355 break;
356 case 0x01:
7cdf6236 357 PrintAndLog(" x1 -> 1 kByte");
561f7c11 358 break;
359 case 0x02:
7cdf6236 360 PrintAndLog(" x2 -> 2 kByte");
561f7c11 361 break;
362 case 0x03:
7cdf6236 363 PrintAndLog(" x3 -> 4 kByte");
561f7c11 364 break;
365 case 0x04:
7cdf6236 366 PrintAndLog(" x4 -> 8 kByte");
561f7c11 367 break;
368 }
19d6d91f 369 switch (card.ats[pos + 3] & 0xf0) {
561f7c11 370 case 0x00:
371 PrintAndLog(" 0x -> Engineering sample");
372 break;
373 case 0x20:
374 PrintAndLog(" 2x -> Released");
375 break;
376 }
19d6d91f 377 switch (card.ats[pos + 3] & 0x0f) {
561f7c11 378 case 0x00:
379 PrintAndLog(" x0 -> Generation 1");
380 break;
381 case 0x01:
382 PrintAndLog(" x1 -> Generation 2");
383 break;
384 case 0x02:
385 PrintAndLog(" x2 -> Generation 3");
386 break;
387 }
19d6d91f 388 switch (card.ats[pos + 4] & 0x0f) {
561f7c11 389 case 0x00:
390 PrintAndLog(" x0 -> Only VCSL supported");
391 break;
392 case 0x01:
393 PrintAndLog(" x1 -> VCS, VCSL, and SVC supported");
394 break;
395 case 0x0E:
396 PrintAndLog(" xE -> no VCS command supported");
397 break;
398 }
399 }
400 }
79a73ab2 401 } else {
19d6d91f 402 PrintAndLog("proprietary non iso14443-4 card found, RATS not supported");
403 }
534983d7 404
52ab55ab 405
406 // try to see if card responses to "chinese magic backdoor" commands.
407 c.cmd = CMD_MIFARE_CIDENT;
408 c.arg[0] = 0;
409 c.arg[1] = 0;
410 c.arg[2] = 0;
411 SendCommand(&c);
412 WaitForResponse(CMD_ACK,&resp);
413 uint8_t isOK = resp.arg[0] & 0xff;
19a700a8 414 PrintAndLog("Answers to chinese magic backdoor commands: %s", (isOK ? "YES" : "NO") );
52ab55ab 415
416 // disconnect
417 c.cmd = CMD_READER_ISO_14443a;
418 c.arg[0] = 0;
419 c.arg[1] = 0;
420 c.arg[2] = 0;
421 SendCommand(&c);
422
19d6d91f 423 return select_status;
7fe9b0b7 424}
425
db22dfe6 426// Collect ISO14443 Type A UIDs
427int CmdHF14ACUIDs(const char *Cmd)
428{
429 // requested number of UIDs
430 int n = atoi(Cmd);
431 // collect at least 1 (e.g. if no parameter was given)
432 n = n > 0 ? n : 1;
433
434 PrintAndLog("Collecting %d UIDs", n);
acf0582d 435 PrintAndLog("Start: %" PRIu64, msclock()/1000);
db22dfe6 436 // repeat n times
437 for (int i = 0; i < n; i++) {
438 // execute anticollision procedure
439 UsbCommand c = {CMD_READER_ISO_14443a, {ISO14A_CONNECT, 0, 0}};
440 SendCommand(&c);
902cb3c0 441
72b1090a 442 UsbCommand resp;
443 WaitForResponse(CMD_ACK,&resp);
902cb3c0 444
72b1090a 445 iso14a_card_select_t *card = (iso14a_card_select_t *) resp.d.asBytes;
db22dfe6 446
447 // check if command failed
902cb3c0 448 if (resp.arg[0] == 0) {
db22dfe6 449 PrintAndLog("Card select failed.");
450 } else {
72b1090a 451 char uid_string[20];
452 for (uint16_t i = 0; i < card->uidlen; i++) {
453 sprintf(&uid_string[2*i], "%02X", card->uid[i]);
db22dfe6 454 }
72b1090a 455 PrintAndLog("%s", uid_string);
db22dfe6 456 }
457 }
acf0582d 458 PrintAndLog("End: %" PRIu64, msclock()/1000);
db22dfe6 459
460 return 1;
461}
462
7fe9b0b7 463// ## simulate iso14443a tag
464// ## greg - added ability to specify tag UID
465int CmdHF14ASim(const char *Cmd)
81cd0474 466{
467 UsbCommand c = {CMD_SIMULATE_TAG_ISO_14443a,{0,0,0}};
468
469 // Retrieve the tag type
470 uint8_t tagtype = param_get8ex(Cmd,0,0,10);
471
472 // When no argument was given, just print help message
473 if (tagtype == 0) {
474 PrintAndLog("");
475 PrintAndLog(" Emulating ISO/IEC 14443 type A tag with 4 or 7 byte UID");
476 PrintAndLog("");
477 PrintAndLog(" syntax: hf 14a sim <type> <uid>");
478 PrintAndLog(" types: 1 = MIFARE Classic");
479 PrintAndLog(" 2 = MIFARE Ultralight");
5ee70129 480 PrintAndLog(" 3 = MIFARE Desfire");
81cd0474 481 PrintAndLog(" 4 = ISO/IEC 14443-4");
5ee70129 482 PrintAndLog(" 5 = MIFARE Tnp3xxx");
81cd0474 483 PrintAndLog("");
484 return 1;
485 }
486
487 // Store the tag type
488 c.arg[0] = tagtype;
489
490 // Retrieve the full 4 or 7 byte long uid
491 uint64_t long_uid = param_get64ex(Cmd,1,0,16);
492
493 // Are we handling the (optional) second part uid?
494 if (long_uid > 0xffffffff) {
43534cba 495 PrintAndLog("Emulating ISO/IEC 14443 type A tag with 7 byte UID (%014" PRIx64 ")",long_uid);
81cd0474 496 // Store the second part
497 c.arg[2] = (long_uid & 0xffffffff);
498 long_uid >>= 32;
499 // Store the first part, ignore the first byte, it is replaced by cascade byte (0x88)
500 c.arg[1] = (long_uid & 0xffffff);
501 } else {
502 PrintAndLog("Emulating ISO/IEC 14443 type A tag with 4 byte UID (%08x)",long_uid);
503 // Only store the first part
504 c.arg[1] = long_uid & 0xffffffff;
505 }
506/*
507 // At lease save the mandatory first part of the UID
508 c.arg[0] = long_uid & 0xffffffff;
509
81cd0474 510 if (c.arg[1] == 0) {
511 PrintAndLog("Emulating ISO/IEC 14443 type A tag with UID %01d %08x %08x",c.arg[0],c.arg[1],c.arg[2]);
512 }
513
514 switch (c.arg[0]) {
515 case 1: {
516 PrintAndLog("Emulating ISO/IEC 14443-3 type A tag with 4 byte UID");
517 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)};
518 } break;
519 case 2: {
520 PrintAndLog("Emulating ISO/IEC 14443-4 type A tag with 7 byte UID");
521 } break;
522 default: {
523 PrintAndLog("Error: unkown tag type (%d)",c.arg[0]);
524 PrintAndLog("syntax: hf 14a sim <uid>",c.arg[0]);
525 PrintAndLog(" type1: 4 ",c.arg[0]);
526
527 return 1;
528 } break;
529 }
530*/
531/*
7fe9b0b7 532 unsigned int hi = 0, lo = 0;
533 int n = 0, i = 0;
534 while (sscanf(&Cmd[i++], "%1x", &n ) == 1) {
535 hi= (hi << 4) | (lo >> 28);
536 lo= (lo << 4) | (n & 0xf);
537 }
81cd0474 538*/
539// 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)};
540// PrintAndLog("Emulating ISO/IEC 14443 type A tag with UID %01d %08x %08x",c.arg[0],c.arg[1],c.arg[2]);
7fe9b0b7 541 SendCommand(&c);
542 return 0;
543}
544
5cd9ec01
M
545int CmdHF14ASnoop(const char *Cmd) {
546 int param = 0;
547
df3e429d 548 uint8_t ctmp = param_getchar(Cmd, 0) ;
549 if (ctmp == 'h' || ctmp == 'H') {
5cd9ec01 550 PrintAndLog("It get data from the field and saves it into command buffer.");
4c3de57a 551 PrintAndLog("Buffer accessible from command hf list 14a.");
5cd9ec01
M
552 PrintAndLog("Usage: hf 14a snoop [c][r]");
553 PrintAndLog("c - triggered by first data from card");
554 PrintAndLog("r - triggered by first 7-bit request from reader (REQ,WUP,...)");
555 PrintAndLog("sample: hf 14a snoop c r");
556 return 0;
557 }
558
559 for (int i = 0; i < 2; i++) {
df3e429d 560 ctmp = param_getchar(Cmd, i);
5cd9ec01
M
561 if (ctmp == 'c' || ctmp == 'C') param |= 0x01;
562 if (ctmp == 'r' || ctmp == 'R') param |= 0x02;
563 }
564
e57c8b2e 565 UsbCommand c = {CMD_SNOOP_ISO_14443a, {param, 0, 0}};
566 SendCommand(&c);
567 return 0;
7fe9b0b7 568}
569
48ece4a7 570
5f6d6c90 571int CmdHF14ACmdRaw(const char *cmd) {
e57c8b2e 572 UsbCommand c = {CMD_READER_ISO_14443a, {0, 0, 0}};
573 bool reply=1;
574 bool crc = false;
575 bool power = false;
576 bool active = false;
577 bool active_select = false;
578 uint16_t numbits = 0;
7cb8516c 579 bool bTimeout = false;
48ece4a7 580 uint32_t timeout = 0;
7cb8516c 581 bool topazmode = false;
e57c8b2e 582 char buf[5]="";
583 int i = 0;
584 uint8_t data[USB_CMD_DATA_SIZE];
48ece4a7 585 uint16_t datalen = 0;
19a700a8 586 uint32_t temp;
5f6d6c90 587
e57c8b2e 588 if (strlen(cmd)<2) {
589 PrintAndLog("Usage: hf 14a raw [-r] [-c] [-p] [-f] [-b] [-t] <number of bits> <0A 0B 0C ... hex>");
590 PrintAndLog(" -r do not read response");
591 PrintAndLog(" -c calculate and append CRC");
592 PrintAndLog(" -p leave the signal field ON after receive");
593 PrintAndLog(" -a active signal field ON without select");
594 PrintAndLog(" -s active signal field ON with select");
595 PrintAndLog(" -b number of bits to send. Useful for send partial byte");
19a700a8 596 PrintAndLog(" -t timeout in ms");
48ece4a7 597 PrintAndLog(" -T use Topaz protocol to send command");
e57c8b2e 598 return 0;
599 }
5f6d6c90 600
e57c8b2e 601
602 // strip
603 while (*cmd==' ' || *cmd=='\t') cmd++;
604
605 while (cmd[i]!='\0') {
606 if (cmd[i]==' ' || cmd[i]=='\t') { i++; continue; }
607 if (cmd[i]=='-') {
608 switch (cmd[i+1]) {
609 case 'r':
610 reply = false;
611 break;
612 case 'c':
613 crc = true;
614 break;
615 case 'p':
616 power = true;
617 break;
618 case 'a':
619 active = true;
620 break;
621 case 's':
622 active_select = true;
623 break;
624 case 'b':
625 sscanf(cmd+i+2,"%d",&temp);
626 numbits = temp & 0xFFFF;
627 i+=3;
628 while(cmd[i]!=' ' && cmd[i]!='\0') { i++; }
629 i-=2;
630 break;
79bf1ad2 631 case 't':
7cb8516c 632 bTimeout = true;
79bf1ad2 633 sscanf(cmd+i+2,"%d",&temp);
19a700a8 634 timeout = temp;
79bf1ad2 635 i+=3;
636 while(cmd[i]!=' ' && cmd[i]!='\0') { i++; }
04bc1c66 637 i-=2;
79bf1ad2 638 break;
e57c8b2e 639 case 'T':
7cb8516c 640 topazmode = true;
48ece4a7 641 break;
e57c8b2e 642 default:
643 PrintAndLog("Invalid option");
644 return 0;
645 }
646 i+=2;
647 continue;
648 }
649 if ((cmd[i]>='0' && cmd[i]<='9') ||
650 (cmd[i]>='a' && cmd[i]<='f') ||
651 (cmd[i]>='A' && cmd[i]<='F') ) {
652 buf[strlen(buf)+1]=0;
653 buf[strlen(buf)]=cmd[i];
654 i++;
655
656 if (strlen(buf)>=2) {
657 sscanf(buf,"%x",&temp);
658 data[datalen]=(uint8_t)(temp & 0xff);
659 *buf=0;
660 if (datalen > sizeof(data)-1) {
79bf1ad2 661 if (crc)
662 PrintAndLog("Buffer is full, we can't add CRC to your data");
663 break;
e57c8b2e 664 } else {
b4810303 665 datalen++;
79bf1ad2 666 }
e57c8b2e 667 }
668 continue;
669 }
670 PrintAndLog("Invalid char on input");
671 return 0;
672 }
673
674 if(crc && datalen>0 && datalen<sizeof(data)-2)
675 {
676 uint8_t first, second;
48ece4a7 677 if (topazmode) {
678 ComputeCrc14443(CRC_14443_B, data, datalen, &first, &second);
679 } else {
680 ComputeCrc14443(CRC_14443_A, data, datalen, &first, &second);
681 }
e57c8b2e 682 data[datalen++] = first;
683 data[datalen++] = second;
684 }
5f6d6c90 685
e57c8b2e 686 if(active || active_select)
687 {
688 c.arg[0] |= ISO14A_CONNECT;
689 if(active)
690 c.arg[0] |= ISO14A_NO_SELECT;
691 }
04bc1c66 692
79bf1ad2 693 if(bTimeout){
e57c8b2e 694 #define MAX_TIMEOUT 40542464 // = (2^32-1) * (8*16) / 13560000Hz * 1000ms/s
695 c.arg[0] |= ISO14A_SET_TIMEOUT;
696 if(timeout > MAX_TIMEOUT) {
697 timeout = MAX_TIMEOUT;
698 PrintAndLog("Set timeout to 40542 seconds (11.26 hours). The max we can wait for response");
699 }
04bc1c66 700 c.arg[2] = 13560000 / 1000 / (8*16) * timeout; // timeout in ETUs (time to transfer 1 bit, approx. 9.4 us)
79bf1ad2 701 }
48ece4a7 702
e57c8b2e 703 if(power) {
704 c.arg[0] |= ISO14A_NO_DISCONNECT;
705 }
48ece4a7 706
29435274 707 if(datalen > 0) {
e57c8b2e 708 c.arg[0] |= ISO14A_RAW;
709 }
5f6d6c90 710
29435274 711 if(topazmode) {
48ece4a7 712 c.arg[0] |= ISO14A_TOPAZMODE;
e57c8b2e 713 }
714
715 // Max buffer is USB_CMD_DATA_SIZE (512)
716 c.arg[1] = (datalen & 0xFFFF) | ((uint32_t)numbits << 16);
717 memcpy(c.d.asBytes,data,datalen);
718
719 SendCommand(&c);
720
721 if (reply) {
722 if(active_select)
723 waitCmd(1);
724 if(datalen>0)
725 waitCmd(0);
726 } // if reply
727 return 0;
5f6d6c90 728}
729
48ece4a7 730
5f6d6c90 731static void waitCmd(uint8_t iSelect)
732{
733 uint8_t *recv;
734 UsbCommand resp;
735 char *hexout;
736
b915fda3 737 if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {
5f6d6c90 738 recv = resp.d.asBytes;
739 uint8_t iLen = iSelect ? resp.arg[1] : resp.arg[0];
48ece4a7 740 PrintAndLog("received %i octets", iLen);
5f6d6c90 741 if(!iLen)
742 return;
743 hexout = (char *)malloc(iLen * 3 + 1);
744 if (hexout != NULL) {
5f6d6c90 745 for (int i = 0; i < iLen; i++) { // data in hex
f66021cf 746 sprintf(&hexout[i * 3], "%02X ", recv[i]);
5f6d6c90 747 }
748 PrintAndLog("%s", hexout);
749 free(hexout);
750 } else {
751 PrintAndLog("malloc failed your client has low memory?");
752 }
753 } else {
754 PrintAndLog("timeout while waiting for reply.");
755 }
756}
757
7fe9b0b7 758static command_t CommandTable[] =
759{
5acd09bd 760 {"help", CmdHelp, 1, "This help"},
4c3de57a 761 {"list", CmdHF14AList, 0, "[Deprecated] List ISO 14443a history"},
5acd09bd 762 {"reader", CmdHF14AReader, 0, "Act like an ISO14443 Type A reader"},
763 {"cuids", CmdHF14ACUIDs, 0, "<n> Collect n>0 ISO14443 Type A UIDs in one go"},
df3e429d 764 {"sim", CmdHF14ASim, 0, "<UID> -- Simulate ISO 14443a tag"},
5acd09bd 765 {"snoop", CmdHF14ASnoop, 0, "Eavesdrop ISO 14443 Type A"},
5f6d6c90 766 {"raw", CmdHF14ACmdRaw, 0, "Send raw hex data to tag"},
7fe9b0b7 767 {NULL, NULL, 0, NULL}
768};
769
902cb3c0 770int CmdHF14A(const char *Cmd) {
f397b5cc 771 // flush
902cb3c0 772 WaitForResponseTimeout(CMD_ACK,NULL,100);
f397b5cc
M
773
774 // parse
7fe9b0b7 775 CmdsParse(CommandTable, Cmd);
776 return 0;
777}
778
779int CmdHelp(const char *Cmd)
780{
781 CmdsHelp(CommandTable);
782 return 0;
783}
Impressum, Datenschutz