#define DELAY_ICLASS_VICC_TO_VCD_READER DELAY_ISO15693_VICC_TO_VCD_READER
// times in samples @ 212kHz when acting as reader
#define ICLASS_READER_TIMEOUT_ACTALL 330 // 1558us, nominal 330us + 7slots*160us = 1450us
+#define ICLASS_READER_TIMEOUT_UPDATE 3390 // 16000us, nominal 4-15ms
#define ICLASS_READER_TIMEOUT_OTHERS 80 // 380us, nominal 330us
static bool sendCmdGetResponseWithRetries(uint8_t* command, size_t cmdsize, uint8_t* resp, size_t max_resp_size,
- uint8_t expected_size, uint8_t retries, uint32_t start_time, uint32_t *eof_time) {
+ uint8_t expected_size, uint8_t retries, uint32_t start_time, uint32_t timeout, uint32_t *eof_time) {
while (retries-- > 0) {
ReaderTransmitIClass(command, cmdsize, &start_time);
- if (expected_size == GetIso15693AnswerFromTag(resp, max_resp_size, ICLASS_READER_TIMEOUT_OTHERS, eof_time)) {
+ if (expected_size == GetIso15693AnswerFromTag(resp, max_resp_size, timeout, eof_time)) {
return true;
}
}
// Select an iClass tag and read all blocks which are always readable without authentication
-void ReaderIClass(uint8_t arg0) {
+void ReaderIClass(uint8_t flags) {
LED_A_ON();
uint8_t result_status = 0;
- // test flags for what blocks to be sure to read
- uint8_t flagReadConfig = arg0 & FLAG_ICLASS_READER_CONF;
- uint8_t flagReadCC = arg0 & FLAG_ICLASS_READER_CC;
- uint8_t flagReadAA = arg0 & FLAG_ICLASS_READER_AA;
+ if (flags & FLAG_ICLASS_READER_INIT) {
+ Iso15693InitReader();
+ }
- set_tracing(true);
- clear_trace();
- Iso15693InitReader();
+ if (flags & FLAG_ICLASS_READER_CLEARTRACE) {
+ set_tracing(true);
+ clear_trace();
+ StartCountSspClk();
+ }
- StartCountSspClk();
uint32_t start_time = 0;
uint32_t eof_time = 0;
result_status = FLAG_ICLASS_READER_CSN;
memcpy(card_data, resp, 8);
}
-
+
start_time = eof_time + DELAY_ICLASS_VICC_TO_VCD_READER;
-
+
//Read block 1, config
- if (flagReadConfig) {
- if (sendCmdGetResponseWithRetries(readConf, sizeof(readConf), resp, sizeof(resp), 10, 10, start_time, &eof_time)) {
+ if (flags & FLAG_ICLASS_READER_CONF) {
+ if (sendCmdGetResponseWithRetries(readConf, sizeof(readConf), resp, sizeof(resp), 10, 10, start_time, ICLASS_READER_TIMEOUT_OTHERS, &eof_time)) {
result_status |= FLAG_ICLASS_READER_CONF;
memcpy(card_data+8, resp, 8);
} else {
}
//Read block 2, e-purse
- if (flagReadCC) {
- if (sendCmdGetResponseWithRetries(readEpurse, sizeof(readEpurse), resp, sizeof(resp), 10, 10, start_time, &eof_time)) {
+ if (flags & FLAG_ICLASS_READER_CC) {
+ if (sendCmdGetResponseWithRetries(readEpurse, sizeof(readEpurse), resp, sizeof(resp), 10, 10, start_time, ICLASS_READER_TIMEOUT_OTHERS, &eof_time)) {
result_status |= FLAG_ICLASS_READER_CC;
memcpy(card_data + (8*2), resp, 8);
} else {
}
//Read block 5, AA
- if (flagReadAA) {
- if (sendCmdGetResponseWithRetries(readAA, sizeof(readAA), resp, sizeof(resp), 10, 10, start_time, &eof_time)) {
+ if (flags & FLAG_ICLASS_READER_AA) {
+ if (sendCmdGetResponseWithRetries(readAA, sizeof(readAA), resp, sizeof(resp), 10, 10, start_time, ICLASS_READER_TIMEOUT_OTHERS, &eof_time)) {
result_status |= FLAG_ICLASS_READER_AA;
memcpy(card_data + (8*5), resp, 8);
} else {
if (!selectIclassTag(card_data, &eof_time)) continue;
start_time = eof_time + DELAY_ICLASS_VICC_TO_VCD_READER;
- if (!sendCmdGetResponseWithRetries(readcheck_cc, sizeof(readcheck_cc), resp, sizeof(resp), 8, 3, start_time, &eof_time)) continue;
+ if (!sendCmdGetResponseWithRetries(readcheck_cc, sizeof(readcheck_cc), resp, sizeof(resp), 8, 3, start_time, ICLASS_READER_TIMEOUT_OTHERS, &eof_time)) continue;
// replay captured auth (cc must not have been updated)
memcpy(check+5, MAC, 4);
start_time = eof_time + DELAY_ICLASS_VICC_TO_VCD_READER;
- if (!sendCmdGetResponseWithRetries(check, sizeof(check), resp, sizeof(resp), 4, 5, start_time, &eof_time)) {
+ if (!sendCmdGetResponseWithRetries(check, sizeof(check), resp, sizeof(resp), 4, 5, start_time, ICLASS_READER_TIMEOUT_OTHERS, &eof_time)) {
Dbprintf("Error: Authentication Fail!");
continue;
}
read[3] = crc & 0xff;
start_time = eof_time + DELAY_ICLASS_VICC_TO_VCD_READER;
- if (!sendCmdGetResponseWithRetries(read, sizeof(read), resp, sizeof(resp), 10, 10, start_time, &eof_time)) {
+ if (!sendCmdGetResponseWithRetries(read, sizeof(read), resp, sizeof(resp), 10, 10, start_time, ICLASS_READER_TIMEOUT_OTHERS, &eof_time)) {
start_time = eof_time + DELAY_ICLASS_VICC_TO_VCD_READER;
Dbprintf("Dump config (block 1) failed");
continue;
read[3] = crc & 0xff;
start_time = eof_time + DELAY_ICLASS_VICC_TO_VCD_READER;
- if (sendCmdGetResponseWithRetries(read, sizeof(read), resp, sizeof(resp), 10, 10, start_time, &eof_time)) {
+ if (sendCmdGetResponseWithRetries(read, sizeof(read), resp, sizeof(resp), 10, 10, start_time, ICLASS_READER_TIMEOUT_OTHERS, &eof_time)) {
Dbprintf(" %02x: %02x %02x %02x %02x %02x %02x %02x %02x",
block, resp[0], resp[1], resp[2],
resp[3], resp[4], resp[5],
void iClass_Check(uint8_t *MAC) {
- uint8_t check[9] = {ICLASS_CMD_CHECK_KD, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
+ uint8_t check[9] = {ICLASS_CMD_CHECK_KD, 0x00};
uint8_t resp[4];
memcpy(check+5, MAC, 4);
uint32_t eof_time;
- bool isOK = sendCmdGetResponseWithRetries(check, sizeof(check), resp, sizeof(resp), 4, 6, 0, &eof_time);
+ bool isOK = sendCmdGetResponseWithRetries(check, sizeof(check), resp, sizeof(resp), 4, 6, 0, ICLASS_READER_TIMEOUT_OTHERS, &eof_time);
cmd_send(CMD_ACK, isOK, 0, 0, resp, sizeof(resp));
}
}
uint8_t resp[8];
uint32_t eof_time;
- bool isOK = sendCmdGetResponseWithRetries(readcheck, sizeof(readcheck), resp, sizeof(resp), 8, 6, 0, &eof_time);
+ bool isOK = sendCmdGetResponseWithRetries(readcheck, sizeof(readcheck), resp, sizeof(resp), 8, 6, 0, ICLASS_READER_TIMEOUT_OTHERS, &eof_time);
cmd_send(CMD_ACK, isOK, 0, 0, resp, sizeof(resp));
}
readcmd[2] = rdCrc >> 8;
readcmd[3] = rdCrc & 0xff;
uint8_t resp[10];
- bool isOK = false;
uint32_t eof_time;
- isOK = sendCmdGetResponseWithRetries(readcmd, sizeof(readcmd), resp, sizeof(resp), 10, 10, 0, &eof_time);
+ bool isOK = sendCmdGetResponseWithRetries(readcmd, sizeof(readcmd), resp, sizeof(resp), 10, 10, 0, ICLASS_READER_TIMEOUT_OTHERS, &eof_time);
memcpy(readdata, resp, sizeof(resp));
return isOK;
LED_A_ON();
uint8_t readblockdata[] = {0,0,0,0,0,0,0,0,0,0};
- bool isOK = false;
- isOK = iClass_ReadBlock(blockno, readblockdata);
+ bool isOK = iClass_ReadBlock(blockno, readblockdata);
cmd_send(CMD_ACK, isOK, 0, 0, readblockdata, 8);
FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
LED_D_OFF();
LED_A_OFF();
}
-void iClass_Dump(uint8_t blockno, uint8_t numblks) {
+
+void iClass_Dump(uint8_t startblock, uint8_t numblks) {
LED_A_ON();
- uint8_t readblockdata[] = {0,0,0,0,0,0,0,0,0,0};
+ uint8_t readblockdata[USB_CMD_DATA_SIZE+2] = {0};
bool isOK = false;
- uint8_t blkCnt = 0;
+ uint16_t blkCnt = 0;
- BigBuf_free();
- uint8_t *dataout = BigBuf_malloc(255*8);
- if (dataout == NULL) {
- Dbprintf("out of memory");
- FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
- LED_D_OFF();
- cmd_send(CMD_ACK, 0, 1, 0, 0, 0);
- LED_A_OFF();
- return;
+ if (numblks > USB_CMD_DATA_SIZE / 8) {
+ numblks = USB_CMD_DATA_SIZE / 8;
}
- memset(dataout, 0xFF, 255*8);
-
- for ( ; blkCnt < numblks; blkCnt++) {
- isOK = iClass_ReadBlock(blockno+blkCnt, readblockdata);
- if (!isOK || (readblockdata[0] == 0xBB || readblockdata[7] == 0xBB || readblockdata[2] == 0xBB)) { //try again
- isOK = iClass_ReadBlock(blockno+blkCnt, readblockdata);
- if (!isOK) {
- Dbprintf("Block %02X failed to read", blkCnt+blockno);
- break;
- }
+
+ for (blkCnt = 0; blkCnt < numblks; blkCnt++) {
+ isOK = iClass_ReadBlock(startblock+blkCnt, readblockdata+8*blkCnt);
+ if (!isOK) {
+ Dbprintf("Block %02X failed to read", startblock+blkCnt);
+ break;
}
- memcpy(dataout + (blkCnt*8), readblockdata, 8);
}
- //return pointer to dump memory in arg3
- cmd_send(CMD_ACK, isOK, blkCnt, BigBuf_max_traceLen(), 0, 0);
- FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
- LED_D_OFF();
- BigBuf_free();
+ cmd_send(CMD_ACK, isOK, blkCnt, 0, readblockdata, blkCnt*8);
LED_A_OFF();
}
static bool iClass_WriteBlock_ext(uint8_t blockNo, uint8_t *data) {
- LED_A_ON();
-
- uint8_t write[] = { ICLASS_CMD_UPDATE, blockNo, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
- //uint8_t readblockdata[10];
- //write[1] = blockNo;
+ uint8_t write[16] = {ICLASS_CMD_UPDATE, blockNo};
memcpy(write+2, data, 12); // data + mac
- char *wrCmd = (char *)(write+1);
- uint16_t wrCrc = iclass_crc16(wrCmd, 13);
- write[14] = wrCrc >> 8;
- write[15] = wrCrc & 0xff;
+ AppendCrc(write+1, 13);
uint8_t resp[10];
bool isOK = false;
uint32_t eof_time = 0;
- isOK = sendCmdGetResponseWithRetries(write, sizeof(write), resp, sizeof(resp), 10, 10, 0, &eof_time);
- uint32_t start_time = eof_time + DELAY_ICLASS_VICC_TO_VCD_READER;
- if (isOK) { //if reader responded correctly
- //Dbprintf("WriteResp: %02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",resp[0],resp[1],resp[2],resp[3],resp[4],resp[5],resp[6],resp[7],resp[8],resp[9]);
- if (memcmp(write+2, resp, 8)) { //if response is not equal to write values
- if (blockNo != 3 && blockNo != 4) { //if not programming key areas (note key blocks don't get programmed with actual key data it is xor data)
- //error try again
- isOK = sendCmdGetResponseWithRetries(write, sizeof(write), resp, sizeof(resp), 10, 10, start_time, &eof_time);
- }
- }
+ isOK = sendCmdGetResponseWithRetries(write, sizeof(write), resp, sizeof(resp), 10, 10, 0, ICLASS_READER_TIMEOUT_UPDATE, &eof_time);
+ if (isOK && blockNo != 3 && blockNo != 4 && memcmp(write+2, resp, 8)) { // check response
+ isOK = false;
}
- LED_A_OFF();
-
return isOK;
}
LED_A_ON();
bool isOK = iClass_WriteBlock_ext(blockNo, data);
- if (isOK){
+ if (isOK) {
Dbprintf("Write block [%02x] successful", blockNo);
} else {
Dbprintf("Write block [%02x] failed", blockNo);
LED_A_OFF();
}
+
void iClass_Clone(uint8_t startblock, uint8_t endblock, uint8_t *data) {
int i;
int written = 0;
} iclass_block_t;
+// iclass / picopass chip config structures and shared routines
+typedef struct {
+ uint8_t app_limit; //[8]
+ uint8_t otp[2]; //[9-10]
+ uint8_t block_writelock;//[11]
+ uint8_t chip_config; //[12]
+ uint8_t mem_config; //[13]
+ uint8_t eas; //[14]
+ uint8_t fuses; //[15]
+} picopass_conf_block;
+
+typedef struct {
+ uint8_t csn[8];
+ picopass_conf_block conf;
+ uint8_t epurse[8];
+ uint8_t key_d[8];
+ uint8_t key_c[8];
+ uint8_t app_issuer_area[8];
+} picopass_hdr;
+
+
+static void fuse_config(const picopass_hdr *hdr) {
+ uint8_t fuses = hdr->conf.fuses;
+
+ if (fuses & FUSE_FPERS)
+ PrintAndLog(" Mode: Personalization [Programmable]");
+ else
+ PrintAndLog(" Mode: Application [Locked]");
+
+ if (fuses & FUSE_CODING1)
+ PrintAndLog("Coding: RFU");
+ else {
+ if (fuses & FUSE_CODING0)
+ PrintAndLog("Coding: ISO 14443-2 B/ISO 15693");
+ else
+ PrintAndLog("Coding: ISO 14443B only");
+ }
+ if ((fuses & FUSE_CRYPT1) && (fuses & FUSE_CRYPT0)) PrintAndLog(" Crypt: Secured page, keys not locked");
+ if ((fuses & FUSE_CRYPT1) && !(fuses & FUSE_CRYPT0)) PrintAndLog(" Crypt: Secured page, keys locked");
+ if (!(fuses & FUSE_CRYPT1) && (fuses & FUSE_CRYPT0)) PrintAndLog(" Crypt: Non secured page");
+ if (!(fuses & FUSE_CRYPT1) && !(fuses & FUSE_CRYPT0)) PrintAndLog(" Crypt: No auth possible. Read only if RA is enabled");
+
+ if (fuses & FUSE_RA)
+ PrintAndLog(" RA: Read access enabled");
+ else
+ PrintAndLog(" RA: Read access not enabled");
+}
+
+
+static void getMemConfig(uint8_t mem_cfg, uint8_t chip_cfg, uint8_t *max_blk, uint8_t *app_areas, uint8_t *kb) {
+ // mem-bit 5, mem-bit 7, chip-bit 4: defines chip type
+ if((chip_cfg & 0x10) && !(mem_cfg & 0x80) && !(mem_cfg & 0x20)) {
+ *kb = 2;
+ *app_areas = 2;
+ *max_blk = 31;
+ } else if((chip_cfg & 0x10) && (mem_cfg & 0x80) && !(mem_cfg & 0x20)) {
+ *kb = 16;
+ *app_areas = 2;
+ *max_blk = 255; //16kb
+ } else if(!(chip_cfg & 0x10) && !(mem_cfg & 0x80) && !(mem_cfg & 0x20)) {
+ *kb = 16;
+ *app_areas = 16;
+ *max_blk = 255; //16kb
+ } else if((chip_cfg & 0x10) && (mem_cfg & 0x80) && (mem_cfg & 0x20)) {
+ *kb = 32;
+ *app_areas = 3;
+ *max_blk = 255; //16kb
+ } else if(!(chip_cfg & 0x10) && !(mem_cfg & 0x80) && (mem_cfg & 0x20)) {
+ *kb = 32;
+ *app_areas = 17;
+ *max_blk = 255; //16kb
+ } else {
+ *kb = 32;
+ *app_areas = 2;
+ *max_blk = 255;
+ }
+}
+
+
+static void mem_app_config(const picopass_hdr *hdr) {
+ uint8_t mem = hdr->conf.mem_config;
+ uint8_t chip = hdr->conf.chip_config;
+ uint8_t applimit = hdr->conf.app_limit;
+ if (applimit < 6) applimit = 26;
+ uint8_t kb = 2;
+ uint8_t app_areas = 2;
+ uint8_t max_blk = 31;
+ getMemConfig(mem, chip, &max_blk, &app_areas, &kb);
+ PrintAndLog(" Mem: %u KBits/%u App Areas (%u * 8 bytes) [%02X]", kb, app_areas, max_blk+1, mem);
+ PrintAndLog(" AA1: blocks 06-%02X", applimit);
+ PrintAndLog(" AA2: blocks %02X-%02X", applimit+1, max_blk);
+}
+
+
+static void printIclassDumpInfo(uint8_t* iclass_dump) {
+ fuse_config((picopass_hdr*)iclass_dump);
+ mem_app_config((picopass_hdr*)iclass_dump);
+}
+
+
static void usage_hf_iclass_chk(void) {
PrintAndLog("Checkkeys loads a dictionary text file with 8byte hex keys to test authenticating against a iClass tag");
PrintAndLog("Usage: hf iclass chk [h|e|r] <f (*.dic)>");
}
-int HFiClassReader(const char *Cmd, bool loop, bool verbose) {
+int HFiClassReader(bool loop, bool verbose) {
+
bool tagFound = false;
- UsbCommand c = {CMD_READER_ICLASS, {FLAG_ICLASS_READER_CSN |
- FLAG_ICLASS_READER_CC | FLAG_ICLASS_READER_CONF | FLAG_ICLASS_READER_AA} };
- // loop in client not device - else on windows have a communication error
+ UsbCommand c = {CMD_READER_ICLASS, {FLAG_ICLASS_READER_INIT | FLAG_ICLASS_READER_CLEARTRACE | FLAG_ICLASS_READER_CSN | FLAG_ICLASS_READER_CONF | FLAG_ICLASS_READER_CC | FLAG_ICLASS_READER_AA} };
UsbCommand resp;
- while(!ukbhit()){
+
+ while (!ukbhit()) {
SendCommand(&c);
if (WaitForResponseTimeout(CMD_ACK,&resp, 4500)) {
uint8_t readStatus = resp.arg[0] & 0xff;
uint8_t *data = resp.d.asBytes;
- // no tag found or button pressed
- if( (readStatus == 0 && !loop) || readStatus == 0xFF) {
+ // no tag found
+ if (readStatus == 0 && !loop) {
// abort
if (verbose) PrintAndLog("Quitting...");
+ DropField();
return 0;
}
- if( readStatus & FLAG_ICLASS_READER_CSN) {
+ if (readStatus & FLAG_ICLASS_READER_CSN) {
PrintAndLog(" CSN: %s",sprint_hex(data,8));
tagFound = true;
}
- if( readStatus & FLAG_ICLASS_READER_CC) {
+ if (readStatus & FLAG_ICLASS_READER_CC) {
PrintAndLog(" CC: %s",sprint_hex(data+16,8));
}
- if( readStatus & FLAG_ICLASS_READER_CONF) {
+ if (readStatus & FLAG_ICLASS_READER_CONF) {
printIclassDumpInfo(data);
}
if (readStatus & FLAG_ICLASS_READER_AA) {
}
if (!loop) break;
}
+
+ DropField();
return 0;
}
+static void usage_hf_iclass_reader(void) {
+ PrintAndLogEx(NORMAL, "Act as a Iclass reader. Look for iClass tags until Enter or the pm3 button is pressed\n");
+ PrintAndLogEx(NORMAL, "Usage: hf iclass reader [h] [1]\n");
+ PrintAndLogEx(NORMAL, "Options:");
+ PrintAndLogEx(NORMAL, " h This help text");
+ PrintAndLogEx(NORMAL, " 1 read only 1 tag");
+ PrintAndLogEx(NORMAL, "Examples:");
+ PrintAndLogEx(NORMAL, " hf iclass reader 1");
+}
+
+
static int CmdHFiClassReader(const char *Cmd) {
- return HFiClassReader(Cmd, true, true);
+ char cmdp = tolower(param_getchar(Cmd, 0));
+ if (cmdp == 'h') {
+ usage_hf_iclass_reader();
+ return 0;
+ }
+ bool findone = (cmdp == '1') ? false : true;
+ return HFiClassReader(findone, true);
}
static void Calc_wb_mac(uint8_t blockno, uint8_t *data, uint8_t *div_key, uint8_t MAC[4]) {
uint8_t WB[9];
WB[0] = blockno;
- memcpy(WB + 1,data,8);
- doMAC_N(WB,sizeof(WB),div_key,MAC);
+ memcpy(WB+1, data, 8);
+ doMAC_N(WB, sizeof(WB), div_key, MAC);
//printf("Cal wb mac block [%02x][%02x%02x%02x%02x%02x%02x%02x%02x] : MAC [%02x%02x%02x%02x]",WB[0],WB[1],WB[2],WB[3],WB[4],WB[5],WB[6],WB[7],WB[8],MAC[0],MAC[1],MAC[2],MAC[3]);
}
-static bool select_only(uint8_t *CSN, bool verbose) {
- UsbCommand resp;
+static bool iClass_select(uint8_t *CSN, bool verbose, bool cleartrace, bool init) {
- UsbCommand c = {CMD_READER_ICLASS, {0}};
+ UsbCommand c = {CMD_READER_ICLASS, {FLAG_ICLASS_READER_CSN}};
+ if (init) c.arg[0] |= FLAG_ICLASS_READER_INIT;
+ if (cleartrace) c.arg[0] |= FLAG_ICLASS_READER_CLEARTRACE;
+ UsbCommand resp;
clearCommandBuffer();
SendCommand(&c);
if (!WaitForResponseTimeout(CMD_ACK, &resp, 4500)) {
uint8_t isOK = resp.arg[0] & 0xff;
uint8_t *data = resp.d.asBytes;
- memcpy(CSN, data, 8);
-
- if (isOK > 0) {
+ if (isOK & FLAG_ICLASS_READER_CSN) {
+ memcpy(CSN, data, 8);
if (verbose) PrintAndLog("CSN: %s", sprint_hex(CSN, 8));
} else {
PrintAndLog("Failed to select card! Aborting");
}
-static bool select_and_auth(uint8_t *KEY, uint8_t *MAC, uint8_t *div_key, bool use_credit_key, bool elite, bool rawkey, bool verbose) {
- uint8_t CSN[8] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
- uint8_t CCNR[12] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
-
- if (!select_only(CSN, verbose))
- return false;
+static bool iClass_authenticate(uint8_t *CSN, uint8_t *KEY, uint8_t *MAC, uint8_t *div_key, bool use_credit_key, bool elite, bool rawkey, bool verbose) {
//get div_key
if (rawkey)
if (verbose) PrintAndLog("Couldn't get Card Challenge");
return false;
}
+
+ uint8_t CCNR[12];
memcpy(CCNR, resp.d.asBytes, 8);
+ memset(CCNR+8, 0x00, 4); // NR = {0, 0, 0, 0}
doMAC(CCNR, div_key, MAC);
uint8_t div_key[8] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
uint8_t c_div_key[8] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
uint8_t blockno = 0;
- uint8_t numblks = 0;
+ uint8_t AA1_maxBlk = 0;
uint8_t maxBlk = 31;
uint8_t app_areas = 1;
uint8_t kb = 2;
bool elite = false;
bool rawkey = false;
bool errors = false;
+ bool verbose = false;
uint8_t cmdp = 0;
- while(param_getchar(Cmd, cmdp) != 0x00)
- {
- switch(param_getchar(Cmd, cmdp))
- {
- case 'h':
- case 'H':
- usage_hf_iclass_dump();
- return 0;
- case 'c':
- case 'C':
- have_credit_key = true;
- dataLen = param_getstr(Cmd, cmdp+1, tempStr, sizeof(tempStr));
- if (dataLen == 16) {
- errors = param_gethex(tempStr, 0, CreditKEY, dataLen);
- } else if (dataLen == 1) {
- keyNbr = param_get8(Cmd, cmdp+1);
- if (keyNbr < ICLASS_KEYS_MAX) {
- memcpy(CreditKEY, iClass_Key_Table[keyNbr], 8);
+ while (param_getchar(Cmd, cmdp) != 0x00 && !errors) {
+ switch(param_getchar(Cmd, cmdp)) {
+ case 'h':
+ case 'H':
+ usage_hf_iclass_dump();
+ return 0;
+ case 'c':
+ case 'C':
+ have_credit_key = true;
+ dataLen = param_getstr(Cmd, cmdp+1, tempStr, sizeof(tempStr));
+ if (dataLen == 16) {
+ errors = param_gethex(tempStr, 0, CreditKEY, dataLen);
+ } else if (dataLen == 1) {
+ keyNbr = param_get8(Cmd, cmdp+1);
+ if (keyNbr < ICLASS_KEYS_MAX) {
+ memcpy(CreditKEY, iClass_Key_Table[keyNbr], 8);
+ } else {
+ PrintAndLog("\nERROR: Credit KeyNbr is invalid\n");
+ errors = true;
+ }
} else {
- PrintAndLog("\nERROR: Credit KeyNbr is invalid\n");
+ PrintAndLog("\nERROR: Credit Key is incorrect length\n");
errors = true;
}
- } else {
- PrintAndLog("\nERROR: Credit Key is incorrect length\n");
- errors = true;
- }
- cmdp += 2;
- break;
- case 'e':
- case 'E':
- elite = true;
- cmdp++;
- break;
- case 'f':
- case 'F':
- fileNameLen = param_getstr(Cmd, cmdp+1, filename, sizeof(filename));
- if (fileNameLen < 1) {
- PrintAndLog("No filename found after f");
- errors = true;
- }
- cmdp += 2;
- break;
- case 'k':
- case 'K':
- have_debit_key = true;
- dataLen = param_getstr(Cmd, cmdp+1, tempStr, sizeof(tempStr));
- if (dataLen == 16) {
- errors = param_gethex(tempStr, 0, KEY, dataLen);
- } else if (dataLen == 1) {
- keyNbr = param_get8(Cmd, cmdp+1);
- if (keyNbr < ICLASS_KEYS_MAX) {
- memcpy(KEY, iClass_Key_Table[keyNbr], 8);
+ cmdp += 2;
+ break;
+ case 'e':
+ case 'E':
+ elite = true;
+ cmdp++;
+ break;
+ case 'f':
+ case 'F':
+ fileNameLen = param_getstr(Cmd, cmdp+1, filename, sizeof(filename));
+ if (fileNameLen < 1) {
+ PrintAndLog("No filename found after f");
+ errors = true;
+ }
+ cmdp += 2;
+ break;
+ case 'k':
+ case 'K':
+ have_debit_key = true;
+ dataLen = param_getstr(Cmd, cmdp+1, tempStr, sizeof(tempStr));
+ if (dataLen == 16) {
+ errors = param_gethex(tempStr, 0, KEY, dataLen);
+ } else if (dataLen == 1) {
+ keyNbr = param_get8(Cmd, cmdp+1);
+ if (keyNbr < ICLASS_KEYS_MAX) {
+ memcpy(KEY, iClass_Key_Table[keyNbr], 8);
+ } else {
+ PrintAndLog("\nERROR: Credit KeyNbr is invalid\n");
+ errors = true;
+ }
} else {
- PrintAndLog("\nERROR: Credit KeyNbr is invalid\n");
+ PrintAndLog("\nERROR: Credit Key is incorrect length\n");
errors = true;
}
- } else {
- PrintAndLog("\nERROR: Credit Key is incorrect length\n");
+ cmdp += 2;
+ break;
+ case 'r':
+ case 'R':
+ rawkey = true;
+ cmdp++;
+ break;
+ case 'v':
+ case 'V':
+ verbose = true;
+ cmdp++;
+ break;
+ default:
+ PrintAndLog("Unknown parameter '%c'\n", param_getchar(Cmd, cmdp));
errors = true;
- }
- cmdp += 2;
- break;
- case 'r':
- case 'R':
- rawkey = true;
- cmdp++;
- break;
- default:
- PrintAndLog("Unknown parameter '%c'\n", param_getchar(Cmd, cmdp));
- errors = true;
- break;
- }
- if (errors) {
- usage_hf_iclass_dump();
- return 0;
+ break;
}
}
- if (cmdp < 2) {
+ if (errors || cmdp < 2) {
usage_hf_iclass_dump();
return 0;
}
- // if no debit key given try credit key on AA1 (not for iclass but for some picopass this will work)
- if (!have_debit_key && have_credit_key) use_credit_key = true;
- //get config and first 3 blocks
- UsbCommand c = {CMD_READER_ICLASS, {FLAG_ICLASS_READER_CSN | FLAG_ICLASS_READER_CONF}};
+ // if only credit key is given: try for AA1 as well (not for iclass but for some picopass this will work)
+ if (!have_debit_key && have_credit_key) {
+ use_credit_key = true;
+ memcpy(KEY, CreditKEY, 8);
+ }
+
+ // clear trace and get first 3 blocks
+ UsbCommand c = {CMD_READER_ICLASS, {FLAG_ICLASS_READER_INIT | FLAG_ICLASS_READER_CLEARTRACE | FLAG_ICLASS_READER_CSN | FLAG_ICLASS_READER_CONF | FLAG_ICLASS_READER_CC}};
UsbCommand resp;
- uint8_t tag_data[255*8];
+ uint8_t tag_data[256*8];
clearCommandBuffer();
SendCommand(&c);
DropField();
return 0;
}
+
uint8_t readStatus = resp.arg[0] & 0xff;
uint8_t *data = resp.d.asBytes;
+ uint8_t status_mask = FLAG_ICLASS_READER_CSN | FLAG_ICLASS_READER_CONF | FLAG_ICLASS_READER_CC;
- if(readStatus == 0){
- PrintAndLog("No tag found...");
- DropField();
+ if (readStatus != status_mask) {
+ PrintAndLog("No tag found ...");
return 0;
- }
- if( readStatus & (FLAG_ICLASS_READER_CSN|FLAG_ICLASS_READER_CONF|FLAG_ICLASS_READER_CC)){
+ } else {
memcpy(tag_data, data, 8*3);
- blockno+=2; // 2 to force re-read of block 2 later. (seems to respond differently..)
- numblks = data[8];
+ if (verbose) PrintAndLog("CSN: %s", sprint_hex(tag_data, 8));
+ AA1_maxBlk = data[8];
getMemConfig(data[13], data[12], &maxBlk, &app_areas, &kb);
// large memory - not able to dump pages currently
- if (numblks > maxBlk) numblks = maxBlk;
- }
- DropField();
- // authenticate debit key and get div_key - later store in dump block 3
- if (!select_and_auth(KEY, MAC, div_key, use_credit_key, elite, rawkey, false)){
- //try twice - for some reason it sometimes fails the first time...
- if (!select_and_auth(KEY, MAC, div_key, use_credit_key, elite, rawkey, false)){
- DropField();
- return 0;
- }
+ if (AA1_maxBlk > maxBlk) AA1_maxBlk = maxBlk;
}
- // begin dump
- UsbCommand w = {CMD_ICLASS_DUMP, {blockno, numblks-blockno+1}};
- clearCommandBuffer();
- SendCommand(&w);
- if (!WaitForResponseTimeout(CMD_ACK, &resp, 4500)) {
- PrintAndLog("Command execute time-out 1");
- DropField();
- return 1;
- }
- uint32_t blocksRead = resp.arg[1];
- uint8_t isOK = resp.arg[0] & 0xff;
- if (!isOK && !blocksRead) {
- PrintAndLog("Read Block Failed");
+ // authenticate debit key (or credit key if we have no debit key) and get div_key - later store in dump block 3
+ if (!iClass_authenticate(tag_data, KEY, MAC, div_key, use_credit_key, elite, rawkey, verbose)){
DropField();
return 0;
}
- uint32_t startindex = resp.arg[2];
- if (blocksRead*8 > sizeof(tag_data)-(blockno*8)) {
- PrintAndLog("Data exceeded Buffer size!");
- blocksRead = (sizeof(tag_data)/8) - blockno;
+
+ // read AA1
+ UsbCommand w = {CMD_ICLASS_DUMP};
+ uint32_t blocksRead = 0;
+ for (blockno = 3; blockno <= AA1_maxBlk; blockno += blocksRead) {
+ w.arg[0] = blockno;
+ w.arg[1] = AA1_maxBlk - blockno + 1;
+ clearCommandBuffer();
+ SendCommand(&w);
+ if (!WaitForResponseTimeout(CMD_ACK, &resp, 4500)) {
+ PrintAndLog("Command execute time-out 1");
+ DropField();
+ return 1;
+ }
+ blocksRead = resp.arg[1];
+ bool isOK = resp.arg[0];
+ if (!isOK) {
+ PrintAndLog("Reading AA1 block failed");
+ DropField();
+ return 0;
+ }
+ memcpy(tag_data + blockno*8, resp.d.asBytes, blocksRead*8);
}
- // response ok - now get bigbuf content of the dump
- GetFromBigBuf(tag_data+(blockno*8), blocksRead*8, startindex, NULL, -1, false);
- size_t gotBytes = blocksRead*8 + blockno*8;
- // try AA2
- if (have_credit_key) {
- //turn off hf field before authenticating with different key
- DropField();
- memset(MAC,0,4);
- // AA2 authenticate credit key and git c_div_key - later store in dump block 4
- if (!select_and_auth(CreditKEY, MAC, c_div_key, true, false, false, false)){
- //try twice - for some reason it sometimes fails the first time...
- if (!select_and_auth(CreditKEY, MAC, c_div_key, true, false, false, false)){
+ // do we still need to read more blocks (AA2 enabled)?
+ if (have_credit_key && maxBlk > AA1_maxBlk) {
+ if (!use_credit_key) {
+ //turn off hf field before authenticating with different key
+ DropField();
+ // AA2 authenticate credit key and git c_div_key - later store in dump block 4
+ uint8_t CSN[8];
+ if (!iClass_select(CSN, verbose, false, true) || !iClass_authenticate(CSN, CreditKEY, MAC, c_div_key, true, false, false, verbose)){
DropField();
return 0;
}
}
- // do we still need to read more block? (aa2 enabled?)
- if (maxBlk > blockno+numblks+1) {
- // setup dump and start
- w.arg[0] = blockno + blocksRead;
- w.arg[1] = maxBlk - (blockno + blocksRead);
+ for ( ; blockno <= maxBlk; blockno += blocksRead) {
+ w.arg[0] = blockno;
+ w.arg[1] = maxBlk - blockno + 1;
clearCommandBuffer();
SendCommand(&w);
if (!WaitForResponseTimeout(CMD_ACK, &resp, 4500)) {
- PrintAndLog("Command execute timeout 2");
+ PrintAndLog("Command execute time-out 1");
DropField();
- return 0;
+ return 1;
}
- uint8_t isOK = resp.arg[0] & 0xff;
blocksRead = resp.arg[1];
- if (!isOK && !blocksRead) {
- PrintAndLog("Read Block Failed 2");
+ bool isOK = resp.arg[0];
+ if (!isOK) {
+ PrintAndLog("Reading AA2 block failed");
DropField();
return 0;
}
-
- startindex = resp.arg[2];
- if (blocksRead*8 > sizeof(tag_data)-gotBytes) {
- PrintAndLog("Data exceeded Buffer size!");
- blocksRead = (sizeof(tag_data) - gotBytes)/8;
- }
- // get dumped data from bigbuf
- GetFromBigBuf(tag_data+gotBytes, blocksRead*8, startindex, NULL, -1, false);
-
- gotBytes += blocksRead*8;
- } else { //field is still on - turn it off...
- DropField();
+ memcpy(tag_data + blockno*8, resp.d.asBytes, blocksRead*8);
}
}
+ DropField();
+
// add diversified keys to dump
- if (have_debit_key) memcpy(tag_data+(3*8),div_key,8);
- if (have_credit_key) memcpy(tag_data+(4*8),c_div_key,8);
+ if (have_debit_key) {
+ memcpy(tag_data + 3*8, div_key, 8);
+ } else {
+ memset(tag_data + 3*8, 0xff, 8);
+ }
+ if (have_credit_key) {
+ memcpy(tag_data + 4*8, c_div_key, 8);
+ } else {
+ memset(tag_data + 4*8, 0xff, 8);
+ }
+
// print the dump
printf("------+--+-------------------------+\n");
printf("CSN |00| %s|\n",sprint_hex(tag_data, 8));
- printIclassDumpContents(tag_data, 1, (gotBytes/8), gotBytes);
+ printIclassDumpContents(tag_data, 1, blockno-1, blockno*8);
- if (filename[0] == 0){
+ if (filename[0] == 0) {
snprintf(filename, FILE_PATH_SIZE,"iclass_tagdump-%02x%02x%02x%02x%02x%02x%02x%02x",
tag_data[0],tag_data[1],tag_data[2],tag_data[3],
tag_data[4],tag_data[5],tag_data[6],tag_data[7]);
}
// save the dump to .bin file
- PrintAndLog("Saving dump file - %d blocks read", gotBytes/8);
- saveFile(filename, "bin", tag_data, gotBytes);
+ PrintAndLog("Saving dump file - %d blocks read", blockno);
+ saveFile(filename, "bin", tag_data, blockno*8);
return 1;
}
static int WriteBlock(uint8_t blockno, uint8_t *bldata, uint8_t *KEY, bool use_credit_key, bool elite, bool rawkey, bool verbose) {
- uint8_t MAC[4]={0x00,0x00,0x00,0x00};
- uint8_t div_key[8]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
- if (!select_and_auth(KEY, MAC, div_key, use_credit_key, elite, rawkey, verbose))
+
+ uint8_t MAC[4] = {0x00,0x00,0x00,0x00};
+ uint8_t div_key[8] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
+ uint8_t CSN[8];
+
+ if (!iClass_select(CSN, verbose, true, true) || !iClass_authenticate(CSN, KEY, MAC, div_key, use_credit_key, elite, rawkey, verbose)) {
+ DropField();
return 0;
+ }
UsbCommand resp;
- Calc_wb_mac(blockno,bldata,div_key,MAC);
+ Calc_wb_mac(blockno, bldata, div_key, MAC);
+
UsbCommand w = {CMD_ICLASS_WRITEBLOCK, {blockno}};
memcpy(w.d.asBytes, bldata, 8);
memcpy(w.d.asBytes + 8, MAC, 4);
clearCommandBuffer();
SendCommand(&w);
- if (!WaitForResponseTimeout(CMD_ACK,&resp,4500))
- {
+ if (!WaitForResponseTimeout(CMD_ACK, &resp, 4500)) {
PrintAndLog("Write Command execute timeout");
+ DropField();
return 0;
}
- uint8_t isOK = resp.arg[0] & 0xff;
+ bool isOK = resp.arg[0];
if (!isOK) {
PrintAndLog("Write Block Failed");
+ DropField();
return 0;
}
+
PrintAndLog("Write Block Successful");
return 1;
}
break;
case 'd':
case 'D':
- if (param_gethex(Cmd, cmdp+1, bldata, 16))
- {
- PrintAndLog("KEY must include 16 HEX symbols\n");
+ if (param_gethex(Cmd, cmdp+1, bldata, 16)) {
+ PrintAndLog("Data must include 16 HEX symbols\n");
errors = true;
}
cmdp += 2;
errors = true;
break;
}
- if(errors) {
+ if (errors) {
usage_hf_iclass_writeblock();
return 0;
}
}
// file handling and reading
f = fopen(filename,"rb");
- if(!f) {
+ if (!f) {
PrintAndLog("Failed to read from file '%s'", filename);
return 1;
}
// then copy to usbcommand->asbytes; the max is 32 - 6 = 24 block 12 bytes each block 288 bytes then we can only accept to clone 21 blocks at the time,
// else we have to create a share memory
int i;
- fseek(f,startblock*8,SEEK_SET);
- if ( fread(tag_data,sizeof(iclass_block_t),endblock - startblock + 1,f) == 0 ) {
+ fseek(f, startblock*8, SEEK_SET);
+ if (fread(tag_data, sizeof(iclass_block_t), endblock - startblock + 1, f) == 0 ) {
PrintAndLog("File reading error.");
fclose(f);
return 2;
uint8_t MAC[4]={0x00,0x00,0x00,0x00};
uint8_t div_key[8]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
+ uint8_t CSN[8];
- if (!select_and_auth(KEY, MAC, div_key, use_credit_key, elite, rawkey, true))
+ if (!iClass_select(CSN, true, false, false) || !iClass_authenticate(CSN, KEY, MAC, div_key, use_credit_key, elite, rawkey, true)) {
+ DropField();
return 0;
+ }
- UsbCommand w = {CMD_ICLASS_CLONE,{startblock,endblock}};
+ UsbCommand w = {CMD_ICLASS_CLONE, {startblock, endblock}};
uint8_t *ptr;
// calculate all mac for every the block we will write
for (i = startblock; i <= endblock; i++){
- Calc_wb_mac(i,tag_data[i - startblock].d,div_key,MAC);
+ Calc_wb_mac(i, tag_data[i - startblock].d, div_key, MAC);
// usb command d start pointer = d + (i - 6) * 12
// memcpy(pointer,tag_data[i - 6],8) 8 bytes
// memcpy(pointer + 8,mac,sizoof(mac) 4 bytes;
printf(" %02x%02x%02x%02x%02x%02x%02x%02x |",p[0],p[1],p[2],p[3],p[4],p[5],p[6],p[7]);
printf(" MAC |%02x%02x%02x%02x|\n",p[8],p[9],p[10],p[11]);
}
+
UsbCommand resp;
SendCommand(&w);
if (!WaitForResponseTimeout(CMD_ACK,&resp,4500))
{
PrintAndLog("Command execute timeout");
+ DropField();
return 0;
}
return 1;
uint8_t MAC[4]={0x00,0x00,0x00,0x00};
uint8_t div_key[8]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
+ uint8_t CSN[8];
+
+ if (!iClass_select(CSN, verbose, true, true)) {
+ DropField();
+ return 0;
+ }
if (auth) {
- if (!select_and_auth(KEY, MAC, div_key, (keyType==0x18), elite, rawkey, verbose))
- return 0;
- } else {
- uint8_t CSN[8];
- if (!select_only(CSN, verbose))
+ if (!iClass_authenticate(CSN, KEY, MAC, div_key, (keyType==0x18), elite, rawkey, verbose)) {
+ DropField();
return 0;
+ }
}
UsbCommand resp;
SendCommand(&w);
if (!WaitForResponseTimeout(CMD_ACK, &resp, 4500)) {
PrintAndLog("Command execute timeout");
+ DropField();
return 0;
}
- uint8_t isOK = resp.arg[0] & 0xff;
+ bool isOK = resp.arg[0];
if (!isOK) {
PrintAndLog("Read Block Failed");
+ DropField();
return 0;
}
//data read is stored in: resp.d.asBytes[0-15]
- if (verbose) PrintAndLog("Block %02X: %s\n",blockno, sprint_hex(resp.d.asBytes,8));
+ if (verbose)
+ PrintAndLog("Block %02X: %s\n",blockno, sprint_hex(resp.d.asBytes,8));
+
return 1;
}
}
if (!givenCSN)
- if (!select_only(CSN, true))
+ if (!iClass_select(CSN, true, true, true)) {
+ DropField();
return 0;
+ }
+ DropField();
HFiClassCalcNewKey(CSN, OLDKEY, NEWKEY, xor_div_key, elite, oldElite, true);
return 0;
// time
uint64_t t1 = msclock();
- for (uint32_t c = 0; c < keycnt; c += 1) {
- printf("."); fflush(stdout);
- if (ukbhit()) {
- int gc = getchar(); (void)gc;
- printf("\naborted via keyboard!\n");
- break;
- }
+ uint8_t CSN[8];
+ if (!iClass_select(CSN, false, true, true)) {
+ PrintAndLog("Couldn't select card");
+ DropField();
+ return 0;
+ }
- memcpy(key, keyBlock + 8 * c , 8);
+ for (uint32_t c = 0; c < keycnt; c++) {
+ printf("."); fflush(stdout);
+ if (ukbhit()) {
+ int gc = getchar(); (void)gc;
+ printf("\naborted via keyboard!\n");
+ break;
+ }
- // debit key. try twice
- for (int foo = 0; foo < 2 && !found_debit; foo++) {
- if (!select_and_auth(key, mac, div_key, false, use_elite, use_raw, false))
- continue;
+ memcpy(key, keyBlock + 8 * c , 8);
- // key found.
- PrintAndLog("\n--------------------------------------------------------");
- PrintAndLog(" Found AA1 debit key\t\t[%s]", sprint_hex(key, 8));
- found_debit = true;
- }
+ // debit key
+ if (!iClass_authenticate(CSN, key, mac, div_key, false, use_elite, use_raw, false))
+ continue;
- // credit key. try twice
- for (int foo = 0; foo < 2 && !found_credit; foo++) {
- if (!select_and_auth(key, mac, div_key, true, use_elite, use_raw, false))
- continue;
+ // key found.
+ PrintAndLog("\n--------------------------------------------------------");
+ PrintAndLog(" Found AA1 debit key\t\t[%s]", sprint_hex(key, 8));
+ found_debit = true;
- // key found
- PrintAndLog("\n--------------------------------------------------------");
- PrintAndLog(" Found AA2 credit key\t\t[%s]", sprint_hex(key, 8));
- found_credit = true;
- }
+ // credit key
+ if (!iClass_authenticate(CSN, key, mac, div_key, true, use_elite, use_raw, false))
+ continue;
- // both keys found.
- if ( found_debit && found_credit )
- break;
+ // key found
+ PrintAndLog("\n--------------------------------------------------------");
+ PrintAndLog(" Found AA2 credit key\t\t[%s]", sprint_hex(key, 8));
+ found_credit = true;
+
+ // both keys found.
+ if (found_debit && found_credit)
+ break;
}
t1 = msclock() - t1;