-int CmdHFiClassReader_Dump(const char *Cmd)
-{
- uint8_t readerType = 0;
- uint8_t MAC[4]={0x00,0x00,0x00,0x00};
- uint8_t 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 CC_temp[8]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
- uint8_t div_key[8]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
- uint8_t keytable[128] = {0};
- int elite = 0;
- uint8_t *used_key;
- int i;
- if (strlen(Cmd)<1)
- {
- PrintAndLog("Usage: hf iclass dump <Key> [e]");
- PrintAndLog(" Key - A 16 byte master key");
- PrintAndLog(" e - If 'e' is specified, the key is interpreted as the 16 byte");
- PrintAndLog(" Custom Key (KCus), which can be obtained via reader-attack");
- PrintAndLog(" See 'hf iclass sim 2'. This key should be on iclass-format");
- PrintAndLog(" sample: hf iclass dump 0011223344556677");
-
-
- return 0;
- }
-
- if (param_gethex(Cmd, 0, KEY, 16))
- {
- PrintAndLog("KEY must include 16 HEX symbols");
- return 1;
- }
-
- if (param_getchar(Cmd, 1) == 'e')
- {
- PrintAndLog("Elite switch on");
- elite = 1;
-
- //calc h2
- hash2(KEY, keytable);
- printarr_human_readable("keytable", keytable, 128);
-
- }
-
- UsbCommand resp;
- uint8_t key_sel[8] = {0};
- uint8_t key_sel_p[8] = { 0 };
-
- //HACK -- Below is for testing without access to a tag
- uint8_t xdata[16] = {0x01,0x02,0x03,0x04,0xF7,0xFF,0x12,0xE0, //CSN from http://www.proxmark.org/forum/viewtopic.php?pid=11230#p11230
- 0xFE,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF}; // Just a random CC. Would be good to add a real testcase here
- memcpy(resp.d.asBytes,xdata, 16);
- resp.arg[0] = 2;
- uint8_t fake_dummy_test = false;
- //End hack
-
-
- UsbCommand c = {CMD_READER_ICLASS, {0}};
- c.arg[0] = FLAG_ICLASS_READER_ONLY_ONCE;
- if(!fake_dummy_test)
- SendCommand(&c);
-
-
-
- if (fake_dummy_test || WaitForResponseTimeout(CMD_ACK,&resp,4500)) {
- uint8_t isOK = resp.arg[0] & 0xff;
- uint8_t * data = resp.d.asBytes;
-
- memcpy(CSN,data,8);
- memcpy(CCNR,data+8,8);
-
- PrintAndLog("isOk:%02x", isOK);
-
- if(isOK > 0)
- {
- PrintAndLog("CSN: %s",sprint_hex(CSN,8));
- }
- if(isOK > 1)
- {
- if(elite)
- {
- //Get the key index (hash1)
- uint8_t key_index[8] = {0};
-
- hash1(CSN, key_index);
- printvar("hash1", key_index,8);
- for(i = 0; i < 8 ; i++)
- key_sel[i] = keytable[key_index[i]] & 0xFF;
- PrintAndLog("Pre-fortified 'permuted' HS key that would be needed by an iclass reader to talk to above CSN:");
- printvar("k_sel", key_sel,8);
- //Permute from iclass format to standard format
- permutekey_rev(key_sel,key_sel_p);
- used_key = key_sel_p;
- }else{
- //Perhaps this should also be permuted to std format?
- // Something like the code below? I have no std system
- // to test this with /Martin
-
- //uint8_t key_sel_p[8] = { 0 };
- //permutekey_rev(KEY,key_sel_p);
- //used_key = key_sel_p;
-
- used_key = KEY;
-
- }
-
- PrintAndLog("Pre-fortified key that would be needed by the OmniKey reader to talk to above CSN:");
- printvar("Used key",used_key,8);
- diversifyKey(CSN,used_key, div_key);
- PrintAndLog("Hash0, a.k.a diversified key, that is computed using Ksel and stored in the card (Block 3):");
- printvar("Div key", div_key, 8);
- printvar("CC_NR:",CCNR,12);
- doMAC(CCNR,12,div_key, MAC);
- printvar("MAC", MAC, 4);
-
- UsbCommand d = {CMD_READER_ICLASS_REPLAY, {readerType}};
- memcpy(d.d.asBytes, MAC, 4);
- if(!fake_dummy_test) SendCommand(&d);
-
- }else{
- PrintAndLog("Failed to obtain CC! Aborting");
- }
- } else {
- PrintAndLog("Command execute timeout");
- }
-
- return 0;
-}
-
-int CmdHFiClass_iso14443A_write(const char *Cmd)
-{
- uint8_t readerType = 0;
- uint8_t MAC[4]={0x00,0x00,0x00,0x00};
- uint8_t 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 div_key[8]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
-
- uint8_t blockNo=0;
- uint8_t bldata[8]={0};
-
- if (strlen(Cmd)<3)
- {
- PrintAndLog("Usage: hf iclass write <Key> <Block> <Data>");
- PrintAndLog(" sample: hf iclass write 0011223344556677 10 AAAAAAAAAAAAAAAA");
- return 0;
- }
-
- if (param_gethex(Cmd, 0, KEY, 16))
- {
- PrintAndLog("KEY must include 16 HEX symbols");
- return 1;
- }
-
- blockNo = param_get8(Cmd, 1);
- if (blockNo>32)
- {
- PrintAndLog("Error: Maximum number of blocks is 32 for iClass 2K Cards!");
- return 1;
- }
- if (param_gethex(Cmd, 2, bldata, 8))
- {
- PrintAndLog("Block data must include 8 HEX symbols");
- return 1;
- }
-
- UsbCommand c = {CMD_ICLASS_ISO14443A_WRITE, {0}};
- SendCommand(&c);
- UsbCommand resp;
-
- if (WaitForResponseTimeout(CMD_ACK,&resp,4500)) {
- uint8_t isOK = resp.arg[0] & 0xff;
- uint8_t * data = resp.d.asBytes;
-
- memcpy(CSN,data,8);
- memcpy(CCNR,data+8,8);
- PrintAndLog("DEBUG: %s",sprint_hex(CSN,8));
- PrintAndLog("DEBUG: %s",sprint_hex(CCNR,8));
- PrintAndLog("isOk:%02x", isOK);
- } else {
- PrintAndLog("Command execute timeout");
- }
-
- diversifyKey(CSN,KEY, div_key);
-
- PrintAndLog("Div Key: %s",sprint_hex(div_key,8));
- doMAC(CCNR, 12,div_key, MAC);
-
- UsbCommand c2 = {CMD_ICLASS_ISO14443A_WRITE, {readerType,blockNo}};
- memcpy(c2.d.asBytes, bldata, 8);
- memcpy(c2.d.asBytes+8, MAC, 4);
- SendCommand(&c2);
-
- if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {
- uint8_t isOK = resp.arg[0] & 0xff;
- uint8_t * data = resp.d.asBytes;
-
- if (isOK)
- PrintAndLog("isOk:%02x data:%s", isOK, sprint_hex(data, 4));
- else
- PrintAndLog("isOk:%02x", isOK);
- } else {
- PrintAndLog("Command execute timeout");
- }
- 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;