+void printIclassDumpContents(uint8_t *iclass_dump, uint8_t startblock, uint8_t endblock, size_t filesize) {
+ uint8_t mem_config;
+ memcpy(&mem_config, iclass_dump + 13,1);
+ uint8_t maxmemcount;
+ uint8_t filemaxblock = filesize / 8;
+ if (mem_config & 0x80)
+ maxmemcount = 255;
+ else
+ maxmemcount = 31;
+ //PrintAndLog ("endblock: %d, filesize: %d, maxmemcount: %d, filemaxblock: %d", endblock,filesize, maxmemcount, filemaxblock);
+
+ if (startblock == 0)
+ startblock = 6;
+ if ((endblock > maxmemcount) || (endblock == 0))
+ endblock = maxmemcount;
+
+ // remember endblock need to relate to zero-index arrays.
+ if (endblock > filemaxblock-1)
+ endblock = filemaxblock;
+
+ int i = startblock;
+ printf("------+--+-------------------------+\n");
+ while (i <= endblock) {
+ uint8_t *blk = iclass_dump + (i * 8);
+ printf("Block |%02X| %s|\n", i, sprint_hex(blk, 8) );
+ i++;
+ }
+ printf("------+--+-------------------------+\n");
+}
+
+int usage_hf_iclass_readtagfile() {
+ PrintAndLog("Usage: hf iclass readtagfile <filename> [startblock] [endblock]");
+ return 1;
+}
+
+int CmdHFiClassReadTagFile(const char *Cmd) {
+ int startblock = 0;
+ int endblock = 0;
+ char tempnum[5];
+ FILE *f;
+ char filename[FILE_PATH_SIZE];
+ if (param_getstr(Cmd, 0, filename, sizeof(filename)) < 1)
+ return usage_hf_iclass_readtagfile();
+ if (param_getstr(Cmd, 1, tempnum, sizeof(tempnum)) < 1)
+ startblock = 0;
+ else
+ sscanf(tempnum,"%d",&startblock);
+
+ if (param_getstr(Cmd,2, tempnum, sizeof(tempnum)) < 1)
+ endblock = 0;
+ else
+ sscanf(tempnum,"%d",&endblock);
+ // file handling and reading
+ f = fopen(filename,"rb");
+ if(!f) {
+ PrintAndLog("Failed to read from file '%s'", filename);
+ return 1;
+ }
+ fseek(f, 0, SEEK_END);
+ long fsize = ftell(f);
+ fseek(f, 0, SEEK_SET);
+
+ if ( fsize < 0 ) {
+ PrintAndLog("Error, when getting filesize");
+ fclose(f);
+ return 1;
+ }
+
+ uint8_t *dump = malloc(fsize);
+
+ size_t bytes_read = fread(dump, 1, fsize, f);
+ fclose(f);
+ uint8_t *csn = dump;
+ printf("------+--+-------------------------+\n");
+ printf("CSN |00| %s|\n", sprint_hex(csn, 8) );
+ // printIclassDumpInfo(dump);
+ printIclassDumpContents(dump,startblock,endblock,bytes_read);
+ free(dump);
+ return 0;
+}
+
+/*
+uint64_t xorcheck(uint64_t sdiv,uint64_t hdiv) {
+ uint64_t new_div = 0x00;
+ new_div ^= sdiv;
+ new_div ^= hdiv;
+ return new_div;
+}
+
+uint64_t hexarray_to_uint64(uint8_t *key) {
+ char temp[17];
+ uint64_t uint_key;
+ for (int i = 0;i < 8;i++)
+ sprintf(&temp[(i *2)],"%02X",key[i]);
+ temp[16] = '\0';
+ if (sscanf(temp,"%016" SCNx64,&uint_key) < 1)
+ return 0;
+ return uint_key;
+}
+*/
+void HFiClassCalcDivKey(uint8_t *CSN, uint8_t *KEY, uint8_t *div_key, bool elite){
+ uint8_t keytable[128] = {0};
+ uint8_t key_index[8] = {0};
+ if (elite) {
+ uint8_t key_sel[8] = { 0 };
+ uint8_t key_sel_p[8] = { 0 };
+ hash2(KEY, keytable);
+ hash1(CSN, key_index);
+ for(uint8_t i = 0; i < 8 ; i++)
+ key_sel[i] = keytable[key_index[i]] & 0xFF;
+
+ //Permute from iclass format to standard format
+ permutekey_rev(key_sel, key_sel_p);
+ diversifyKey(CSN, key_sel_p, div_key);
+ } else {
+ diversifyKey(CSN, KEY, div_key);
+ }
+}
+
+//when told CSN, oldkey, newkey, if new key is elite (elite), and if old key was elite (oldElite)
+//calculate and return xor_div_key (ready for a key write command)
+//print all div_keys if verbose
+static void HFiClassCalcNewKey(uint8_t *CSN, uint8_t *OLDKEY, uint8_t *NEWKEY, uint8_t *xor_div_key, bool elite, bool oldElite, bool verbose){
+ uint8_t old_div_key[8] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
+ uint8_t new_div_key[8] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
+ //get old div key
+ HFiClassCalcDivKey(CSN, OLDKEY, old_div_key, oldElite);
+ //get new div key
+ HFiClassCalcDivKey(CSN, NEWKEY, new_div_key, elite);
+
+ for (uint8_t i = 0; i < sizeof(old_div_key); i++){
+ xor_div_key[i] = old_div_key[i] ^ new_div_key[i];
+ }
+ if (verbose) {
+ printf("Old Div Key : %s\n",sprint_hex(old_div_key,8));
+ printf("New Div Key : %s\n",sprint_hex(new_div_key,8));
+ printf("Xor Div Key : %s\n",sprint_hex(xor_div_key,8));
+ }
+}
+
+int usage_hf_iclass_calc_newkey(void) {
+ PrintAndLog("HELP : Manage iClass Keys in client memory:\n");
+ PrintAndLog("Usage: hf iclass calc_newkey o <Old key> n <New key> s [csn] e");
+ PrintAndLog(" Options:");
+ PrintAndLog(" o <oldkey> : *specify a key as 16 hex symbols or a key number as 1 symbol");
+ PrintAndLog(" n <newkey> : *specify a key as 16 hex symbols or a key number as 1 symbol");
+ PrintAndLog(" s <csn> : specify a card Serial number to diversify the key (if omitted will attempt to read a csn)");
+ PrintAndLog(" e : specify new key as elite calc");
+ PrintAndLog(" ee : specify old and new key as elite calc");
+ PrintAndLog("Samples:");
+ PrintAndLog(" e key to e key given csn : hf iclass calcnewkey o 1122334455667788 n 2233445566778899 s deadbeafdeadbeaf ee");
+ PrintAndLog(" std key to e key read csn: hf iclass calcnewkey o 1122334455667788 n 2233445566778899 e");
+ PrintAndLog(" std to std read csn : hf iclass calcnewkey o 1122334455667788 n 2233445566778899");
+ PrintAndLog("NOTE: * = required\n");
+
+ return 1;
+}
+
+int CmdHFiClassCalcNewKey(const char *Cmd) {
+ uint8_t OLDKEY[8] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
+ uint8_t NEWKEY[8] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
+ uint8_t xor_div_key[8] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
+ 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};
+ uint8_t keyNbr = 0;
+ uint8_t dataLen = 0;
+ char tempStr[50] = {0};
+ bool givenCSN = false;
+ bool oldElite = false;
+ bool elite = false;
+ bool errors = false;
+ uint8_t cmdp = 0;
+ while(param_getchar(Cmd, cmdp) != 0x00)
+ {
+ switch(param_getchar(Cmd, cmdp))
+ {
+ case 'h':
+ case 'H':
+ return usage_hf_iclass_calc_newkey();
+ case 'e':
+ case 'E':
+ dataLen = param_getstr(Cmd, cmdp, tempStr, sizeof(tempStr));
+ if (dataLen==2)
+ oldElite = true;
+ elite = true;
+ cmdp++;
+ break;
+ case 'n':
+ case 'N':
+ dataLen = param_getstr(Cmd, cmdp+1, tempStr, sizeof(tempStr));
+ if (dataLen == 16) {
+ errors = param_gethex(tempStr, 0, NEWKEY, dataLen);
+ } else if (dataLen == 1) {
+ keyNbr = param_get8(Cmd, cmdp+1);
+ if (keyNbr < ICLASS_KEYS_MAX) {
+ memcpy(NEWKEY, iClass_Key_Table[keyNbr], 8);
+ } else {
+ PrintAndLog("\nERROR: NewKey Nbr is invalid\n");
+ errors = true;
+ }
+ } else {
+ PrintAndLog("\nERROR: NewKey is incorrect length\n");
+ errors = true;
+ }
+ cmdp += 2;
+ break;
+ case 'o':
+ case 'O':
+ dataLen = param_getstr(Cmd, cmdp+1, tempStr, sizeof(tempStr));
+ if (dataLen == 16) {
+ errors = param_gethex(tempStr, 0, OLDKEY, dataLen);
+ } else if (dataLen == 1) {
+ keyNbr = param_get8(Cmd, cmdp+1);
+ if (keyNbr < ICLASS_KEYS_MAX) {
+ memcpy(OLDKEY, iClass_Key_Table[keyNbr], 8);
+ } else {
+ PrintAndLog("\nERROR: Credit KeyNbr is invalid\n");
+ errors = true;
+ }
+ } else {
+ PrintAndLog("\nERROR: Credit Key is incorrect length\n");
+ errors = true;
+ }
+ cmdp += 2;
+ break;
+ case 's':
+ case 'S':
+ givenCSN = true;
+ if (param_gethex(Cmd, cmdp+1, CSN, 16))
+ return usage_hf_iclass_calc_newkey();
+ cmdp += 2;
+ break;
+ default:
+ PrintAndLog("Unknown parameter '%c'\n", param_getchar(Cmd, cmdp));
+ errors = true;
+ break;
+ }
+ if(errors) return usage_hf_iclass_calc_newkey();
+ }
+
+ if (cmdp < 4) return usage_hf_iclass_calc_newkey();
+
+ if (!givenCSN)
+ if (!select_only(CSN, CCNR, false, true))
+ return 0;
+
+ HFiClassCalcNewKey(CSN, OLDKEY, NEWKEY, xor_div_key, elite, oldElite, true);
+ return 0;
+}
+
+static int loadKeys(char *filename) {
+ FILE *f;
+ f = fopen(filename,"rb");
+ if(!f) {
+ PrintAndLog("Failed to read from file '%s'", filename);
+ return 0;
+ }
+ fseek(f, 0, SEEK_END);
+ long fsize = ftell(f);
+ fseek(f, 0, SEEK_SET);
+
+ if ( fsize < 0 ) {
+ PrintAndLog("Error, when getting filesize");
+ fclose(f);
+ return 1;
+ }
+
+ uint8_t *dump = malloc(fsize);
+
+ size_t bytes_read = fread(dump, 1, fsize, f);
+ fclose(f);
+ if (bytes_read > ICLASS_KEYS_MAX * 8){
+ PrintAndLog("File is too long to load - bytes: %u", bytes_read);
+ free(dump);
+ return 0;
+ }
+ uint8_t i = 0;
+ for (; i < bytes_read/8; i++){
+ memcpy(iClass_Key_Table[i],dump+(i*8),8);
+ }
+ free(dump);
+ PrintAndLog("%u keys loaded", i);
+ return 1;
+}
+
+static int saveKeys(char *filename) {
+ FILE *f;
+ f = fopen(filename,"wb");
+ if (f == NULL) {
+ printf("error opening file %s\n",filename);
+ return 0;
+ }
+ for (uint8_t i = 0; i < ICLASS_KEYS_MAX; i++){
+ if (fwrite(iClass_Key_Table[i],8,1,f) != 1){
+ PrintAndLog("save key failed to write to file: %s", filename);
+ break;
+ }
+ }
+ fclose(f);
+ return 0;
+}
+
+static int printKeys(void) {
+ PrintAndLog("");
+ for (uint8_t i = 0; i < ICLASS_KEYS_MAX; i++){
+ PrintAndLog("%u: %s",i,sprint_hex(iClass_Key_Table[i],8));
+ }
+ PrintAndLog("");
+ return 0;
+}
+
+int usage_hf_iclass_managekeys(void) {
+ PrintAndLog("HELP : Manage iClass Keys in client memory:\n");
+ PrintAndLog("Usage: hf iclass managekeys n [keynbr] k [key] f [filename] s l p\n");
+ PrintAndLog(" Options:");
+ PrintAndLog(" n <keynbr> : specify the keyNbr to set in memory");
+ PrintAndLog(" k <key> : set a key in memory");
+ PrintAndLog(" f <filename>: specify a filename to use with load or save operations");
+ PrintAndLog(" s : save keys in memory to file specified by filename");
+ PrintAndLog(" l : load keys to memory from file specified by filename");
+ PrintAndLog(" p : print keys loaded into memory\n");
+ PrintAndLog("Samples:");
+ PrintAndLog(" set key : hf iclass managekeys n 0 k 1122334455667788");
+ PrintAndLog(" save key file: hf iclass managekeys f mykeys.bin s");
+ PrintAndLog(" load key file: hf iclass managekeys f mykeys.bin l");
+ PrintAndLog(" print keys : hf iclass managekeys p\n");
+ return 0;
+}
+
+int CmdHFiClassManageKeys(const char *Cmd) {
+ uint8_t keyNbr = 0;
+ uint8_t dataLen = 0;
+ uint8_t KEY[8] = {0};
+ char filename[FILE_PATH_SIZE];
+ uint8_t fileNameLen = 0;
+ bool errors = false;
+ uint8_t operation = 0;
+ char tempStr[20];
+ uint8_t cmdp = 0;
+
+ while(param_getchar(Cmd, cmdp) != 0x00)
+ {
+ switch(param_getchar(Cmd, cmdp))
+ {
+ case 'h':
+ case 'H':
+ return usage_hf_iclass_managekeys();
+ 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 'n':
+ case 'N':
+ keyNbr = param_get8(Cmd, cmdp+1);
+ if (keyNbr >= ICLASS_KEYS_MAX) {
+ PrintAndLog("Invalid block number");
+ errors = true;
+ }
+ cmdp += 2;
+ break;
+ case 'k':
+ case 'K':
+ operation += 3; //set key
+ dataLen = param_getstr(Cmd, cmdp+1, tempStr, sizeof(tempStr));
+ if (dataLen == 16) { //ul-c or ev1/ntag key length
+ errors = param_gethex(tempStr, 0, KEY, dataLen);
+ } else {
+ PrintAndLog("\nERROR: Key is incorrect length\n");
+ errors = true;
+ }
+ cmdp += 2;
+ break;
+ case 'p':
+ case 'P':
+ operation += 4; //print keys in memory
+ cmdp++;
+ break;
+ case 'l':
+ case 'L':
+ operation += 5; //load keys from file
+ cmdp++;
+ break;
+ case 's':
+ case 'S':
+ operation += 6; //save keys to file
+ cmdp++;
+ break;
+ default:
+ PrintAndLog("Unknown parameter '%c'\n", param_getchar(Cmd, cmdp));
+ errors = true;
+ break;
+ }
+ if(errors) return usage_hf_iclass_managekeys();
+ }
+ if (operation == 0){
+ PrintAndLog("no operation specified (load, save, or print)\n");
+ return usage_hf_iclass_managekeys();
+ }
+ if (operation > 6){
+ PrintAndLog("Too many operations specified\n");
+ return usage_hf_iclass_managekeys();
+ }
+ if (operation > 4 && fileNameLen == 0){
+ PrintAndLog("You must enter a filename when loading or saving\n");
+ return usage_hf_iclass_managekeys();
+ }
+
+ switch (operation){
+ case 3: memcpy(iClass_Key_Table[keyNbr], KEY, 8); return 1;
+ case 4: return printKeys();
+ case 5: return loadKeys(filename);
+ case 6: return saveKeys(filename);
+ break;
+ }
+ return 0;
+}
+
+int CmdHFiClassCheckKeys(const char *Cmd) {
+
+ uint8_t mac[4] = {0x00,0x00,0x00,0x00};
+ uint8_t key[8] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
+ uint8_t div_key[8] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
+
+ // elite key, raw key, standard key
+ bool use_elite = false;
+ bool use_raw = false;
+ bool found_debit = false;
+ bool found_credit = false;
+ bool errors = false;
+ uint8_t cmdp = 0x00;
+ FILE * f;
+ char filename[FILE_PATH_SIZE] = {0};
+ uint8_t fileNameLen = 0;
+ char buf[17];
+ uint8_t *keyBlock = NULL, *p;
+ int keyitems = 0, keycnt = 0;
+
+ while (param_getchar(Cmd, cmdp) != 0x00 && !errors) {
+ switch (param_getchar(Cmd, cmdp)) {
+ case 'h':
+ case 'H':
+ return usage_hf_iclass_chk();
+ 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 'e':
+ case 'E':
+ use_elite = true;
+ cmdp++;
+ break;
+ case 'r':
+ case 'R':
+ use_raw = true;
+ cmdp++;
+ break;
+ default:
+ PrintAndLog("Unknown parameter '%c'\n", param_getchar(Cmd, cmdp));
+ errors = true;
+ break;
+ }
+ }
+ if (errors) return usage_hf_iclass_chk();
+
+ if ( !(f = fopen( filename , "r")) ) {
+ PrintAndLog("File: %s: not found or locked.", filename);
+ return 1;
+ }
+
+ while( fgets(buf, sizeof(buf), f) ){
+ if (strlen(buf) < 16 || buf[15] == '\n')
+ continue;
+
+ while (fgetc(f) != '\n' && !feof(f)) ; //goto next line
+
+ if( buf[0]=='#' ) continue; //The line start with # is comment, skip
+
+ if (!isxdigit(buf[0])){
+ PrintAndLog("File content error. '%s' must include 16 HEX symbols",buf);
+ continue;
+ }
+
+ buf[16] = 0;
+
+ p = realloc(keyBlock, 8 * (keyitems += 64));
+ if (!p) {
+ PrintAndLog("Cannot allocate memory for default keys");
+ free(keyBlock);
+ fclose(f);
+ return 2;
+ }
+ keyBlock = p;
+
+ memset(keyBlock + 8 * keycnt, 0, 8);
+ num_to_bytes(strtoull(buf, NULL, 16), 8, keyBlock + 8 * keycnt);
+
+ //PrintAndLog("check key[%2d] %016" PRIx64, keycnt, bytes_to_num(keyBlock + 8*keycnt, 8));
+ keycnt++;
+ memset(buf, 0, sizeof(buf));
+ }
+ fclose(f);
+ PrintAndLog("Loaded %2d keys from %s", keycnt, filename);
+
+ // 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;
+ }
+
+ memcpy(key, keyBlock + 8 * c , 8);
+
+ // 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;
+
+ // key found.
+ PrintAndLog("\n--------------------------------------------------------");
+ PrintAndLog(" Found AA1 debit key\t\t[%s]", sprint_hex(key, 8));
+ found_debit = true;
+ }
+
+ // 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 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;
+
+ PrintAndLog("\nTime in iclass checkkeys: %.0f seconds\n", (float)t1/1000.0);
+
+ DropField();
+ free(keyBlock);
+ PrintAndLog("");
+ return 0;
+}
+