44 #endif // HAVE_CONFIG_H
57 #define SAK_FLAG_ATS_SUPPORTED 0x20
59 #define MAX_FRAME_LEN 264
61 static uint8_t abtRx[MAX_FRAME_LEN];
63 static size_t szRx =
sizeof(abtRx);
64 static uint8_t abtRawUid[12];
65 static uint8_t abtAtqa[2];
66 static uint8_t abtSak;
67 static uint8_t abtAts[MAX_FRAME_LEN];
68 static uint8_t szAts = 0;
69 static size_t szCL = 1;
72 bool quiet_output =
false;
73 bool force_rats =
false;
75 bool iso_ats_supported =
false;
78 uint8_t abtReqa[1] = { 0x26 };
79 uint8_t abtSelectAll[2] = { 0x93, 0x20 };
80 uint8_t abtSelectTag[9] = { 0x93, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
81 uint8_t abtRats[4] = { 0xe0, 0x50, 0x00, 0x00 };
82 uint8_t abtHalt[4] = { 0x50, 0x00, 0x00, 0x00 };
83 #define CASCADE_BIT 0x04
86 transmit_bits(
const uint8_t *pbtTx,
const size_t szTxBits)
91 printf(
"Sent bits: ");
92 print_hex_bits(pbtTx, szTxBits);
98 if ((!quiet_output) && (szRxBits > 0)) {
99 printf(
"Response after %u cycles\n", cycles);
107 printf(
"Received bits: ");
108 print_hex_bits(abtRx, szRxBits);
116 transmit_bytes(
const uint8_t *pbtTx,
const size_t szTx)
121 printf(
"Sent bits: ");
122 print_hex(pbtTx, szTx);
129 if ((!quiet_output) && (res > 0)) {
130 printf(
"Response after %u cycles\n", cycles);
139 printf(
"Received bits: ");
140 print_hex(abtRx, szRx);
147 print_usage(
char *argv[])
149 printf(
"Usage: %s [OPTIONS]\n", argv[0]);
150 printf(
"Options:\n");
151 printf(
"\t-h\tHelp. Print this message.\n");
152 printf(
"\t-q\tQuiet mode. Suppress output of READER and EMULATOR data (improves timing).\n");
153 printf(
"\t-f\tForce RATS.\n");
154 printf(
"\t-t\tMeasure response time (in cycles).\n");
158 main(
int argc,
char *argv[])
163 for (arg = 1; arg < argc; arg++) {
164 if (0 == strcmp(argv[arg],
"-h")) {
167 }
else if (0 == strcmp(argv[arg],
"-q")) {
169 }
else if (0 == strcmp(argv[arg],
"-f")) {
171 }
else if (0 == strcmp(argv[arg],
"-t")) {
174 ERR(
"%s is not supported option.", argv[arg]);
182 if (context == NULL) {
183 ERR(
"Unable to init libnfc (malloc)");
191 ERR(
"Error opening NFC reader");
206 nfc_perror(pnd,
"nfc_device_set_property_bool");
213 nfc_perror(pnd,
"nfc_device_set_property_bool");
220 nfc_perror(pnd,
"nfc_device_set_property_bool");
229 if (!transmit_bits(abtReqa, 7)) {
230 printf(
"Error: No tag available\n");
235 memcpy(abtAtqa, abtRx, 2);
238 transmit_bytes(abtSelectAll, 2);
241 if ((abtRx[0] ^ abtRx[1] ^ abtRx[2] ^ abtRx[3] ^ abtRx[4]) != 0) {
242 printf(
"WARNING: BCC check failed!\n");
246 memcpy(abtRawUid, abtRx, 4);
249 memcpy(abtSelectTag + 2, abtRx, 5);
250 iso14443a_crc_append(abtSelectTag, 7);
251 transmit_bytes(abtSelectTag, 9);
255 if (abtSak & CASCADE_BIT) {
258 if (abtRawUid[0] != 0x88) {
259 printf(
"WARNING: Cascade bit set but CT != 0x88!\n");
267 abtSelectAll[0] = 0x95;
270 transmit_bytes(abtSelectAll, 2);
273 if ((abtRx[0] ^ abtRx[1] ^ abtRx[2] ^ abtRx[3] ^ abtRx[4]) != 0) {
274 printf(
"WARNING: BCC check failed!\n");
278 memcpy(abtRawUid + 4, abtRx, 4);
281 abtSelectTag[0] = 0x95;
282 memcpy(abtSelectTag + 2, abtRx, 5);
283 iso14443a_crc_append(abtSelectTag, 7);
284 transmit_bytes(abtSelectTag, 9);
288 if (abtSak & CASCADE_BIT) {
291 if (abtRawUid[0] != 0x88) {
292 printf(
"WARNING: Cascade bit set but CT != 0x88!\n");
300 abtSelectAll[0] = 0x97;
301 transmit_bytes(abtSelectAll, 2);
304 if ((abtRx[0] ^ abtRx[1] ^ abtRx[2] ^ abtRx[3] ^ abtRx[4]) != 0) {
305 printf(
"WARNING: BCC check failed!\n");
309 memcpy(abtRawUid + 8, abtRx, 4);
312 abtSelectTag[0] = 0x97;
313 memcpy(abtSelectTag + 2, abtRx, 5);
314 iso14443a_crc_append(abtSelectTag, 7);
315 transmit_bytes(abtSelectTag, 9);
322 iso_ats_supported =
true;
325 iso14443a_crc_append(abtRats, 2);
326 if (transmit_bytes(abtRats, 4)) {
327 memcpy(abtAts, abtRx, szRx);
333 iso14443a_crc_append(abtHalt, 2);
334 transmit_bytes(abtHalt, 4);
336 printf(
"\nFound tag with\n UID: ");
339 printf(
"%02x%02x%02x%02x", abtRawUid[0], abtRawUid[1], abtRawUid[2], abtRawUid[3]);
342 printf(
"%02x%02x%02x", abtRawUid[1], abtRawUid[2], abtRawUid[3]);
343 printf(
"%02x%02x%02x%02x", abtRawUid[4], abtRawUid[5], abtRawUid[6], abtRawUid[7]);
346 printf(
"%02x%02x%02x", abtRawUid[1], abtRawUid[2], abtRawUid[3]);
347 printf(
"%02x%02x%02x", abtRawUid[5], abtRawUid[6], abtRawUid[7]);
348 printf(
"%02x%02x%02x%02x", abtRawUid[8], abtRawUid[9], abtRawUid[10], abtRawUid[11]);
352 printf(
"ATQA: %02x%02x\n SAK: %02x\n", abtAtqa[1], abtAtqa[0], abtSak);
354 if (force_rats && ! iso_ats_supported) {
355 printf(
" RATS forced\n");
358 print_hex(abtAts, szAts);