]> git.zerfleddert.de Git - proxmark3-svn/blobdiff - client/cmdhfmfu.c
FIX: camelcase on a variable caused some issues.
[proxmark3-svn] / client / cmdhfmfu.c
index 482d14ba2fef268d54cd035f2d1097ba2121dbaf..ea381736d249c65ea76698d34afe431eebf5f0fd 100644 (file)
@@ -208,6 +208,27 @@ static int ulev1_requestAuthentication( uint8_t *pwd, uint8_t *pack, uint16_t pa
        return len;
 }
 
+static int ul_auth_select( iso14a_card_select_t *card, TagTypeUL_t tagtype, bool hasAuthKey, uint8_t *authenticationkey, uint8_t *pack, uint8_t packSize){
+       if ( hasAuthKey && (tagtype & UL_C)) {
+               //will select card automatically and close connection on error
+               if (!ulc_authentication(authenticationkey, false)) {
+                       PrintAndLog("Error: Authentication Failed UL-C");
+                       return 0;
+               }
+       } else {
+               if ( !ul_select(card) ) return 0;
+
+               if (hasAuthKey) {
+                       if (ulev1_requestAuthentication(authenticationkey, pack, packSize) < 1) {
+                               ul_switch_off_field();
+                               PrintAndLog("Error: Authentication Failed UL-EV1/NTAG");
+                               return 0;
+                       }
+               }
+       }
+       return 1;
+}
+
 static int ulev1_getVersion( uint8_t *response, uint16_t responseLength ){
        
        uint8_t cmd[] = {MIFARE_ULEV1_VERSION}; 
@@ -555,7 +576,7 @@ uint32_t GetHF14AMfU_Type(void){
                                }
                        case 0x01: tagtype = UL_C; break;
                        case 0x00: tagtype = UL; break;
-                       case -1  : tagtype = (UL | UL_C | NTAG_203); break;  //when does this happen?  -- if getversion fails, it assumes it is either UL/ULC -- but why? magic tags?
+                       case -1  : tagtype = (UL | UL_C | NTAG_203); break;  // could be UL | UL_C magic tags
                        default  : tagtype = UNKNOWN; break;
                }
                // UL vs UL-C vs ntag203 test
@@ -579,7 +600,7 @@ uint32_t GetHF14AMfU_Type(void){
                                        tagtype = UL;
                                } else {
                                        // read page 0x30 (should error if it is a ntag203)
-                                       status = ul_read(30, data, sizeof(data));
+                                       status = ul_read(0x30, data, sizeof(data));
                                        if ( status <= 1 ){
                                                tagtype = NTAG_203;
                                        } else {
@@ -610,17 +631,19 @@ int CmdHF14AMfUInfo(const char *Cmd){
        uint8_t authlim = 0xff;
        uint8_t data[16] = {0x00};
        iso14a_card_select_t card;
-       uint8_t *key;
        int status;     
        bool errors = false;
        bool hasAuthKey = false;
-       bool hasPwdKey = false;
        bool locked = false;
+       bool swapEndian = false;
        uint8_t cmdp = 0;
-       uint8_t datalen = 0;
-       uint8_t authenticationkey[16] = {0x00};
+       uint8_t dataLen = 0;
+       uint8_t authenticationkey[16] = {0x00}; 
+       uint8_t *authkeyptr = authenticationkey;
+       uint8_t *key;
        uint8_t pack[4] = {0,0,0,0};
        int len = 0;
+       char tempStr[50];
                                
        while(param_getchar(Cmd, cmdp) != 0x00)
        {
@@ -631,23 +654,21 @@ int CmdHF14AMfUInfo(const char *Cmd){
                        return usage_hf_mfu_info();
                case 'k':
                case 'K':
-                       // EV1/NTAG size key
-                       datalen = param_gethex(Cmd, cmdp+1, data, 8);
-                       if ( !datalen ) {
-                               memcpy(authenticationkey, data, 4);
-                               cmdp += 2;
-                               hasPwdKey = true;
-                               break;
-                       }
-                       // UL-C size key        
-                       datalen = param_gethex(Cmd, cmdp+1, data, 32);
-                       if (!datalen){
-                               memcpy(authenticationkey, data, 16);
-                               cmdp += 2;
-                               hasAuthKey = true;
-                               break;
-                       }
+                       dataLen = param_getstr(Cmd, cmdp+1, tempStr);
+                       if (dataLen == 32 || dataLen == 8) { //ul-c or ev1/ntag key length
+                               errors = param_gethex(tempStr, 0, authenticationkey, dataLen);
+                               dataLen /= 2; // handled as bytes from now on
+                       } else {
+                               PrintAndLog("\nERROR: Key is incorrect length\n");
                        errors = true; 
+                       }
+                       cmdp += 2;
+                       hasAuthKey = true;
+                       break;
+               case 'l':
+               case 'L':
+                       swapEndian = true;
+                       cmdp++;
                        break;
                default:
                        PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd, cmdp));
@@ -667,24 +688,10 @@ int CmdHF14AMfUInfo(const char *Cmd){
        PrintAndLog("-------------------------------------------------------------");
        ul_print_type(tagtype, 6);
        
-       if (!ul_select(&card)) return 0;
-       
-       if ( hasAuthKey && (tagtype & UL_C)) {
-               //will select card automatically and close connection on error
-               if (!ulc_authentication(authenticationkey, false)) {
-                       PrintAndLog("Error: Authentication Failed UL-C");
-                       return 0;
-               }
-       }
+       // Swap endianness 
+       if (swapEndian && hasAuthKey) authkeyptr = SwapEndian64(authenticationkey, dataLen, (dataLen == 16) ? 8 : 4 );
        
-       if ( hasPwdKey ) {
-               len = ulev1_requestAuthentication(authenticationkey, pack, sizeof(pack));
-               if (len < 1) {
-                       ul_switch_off_field();
-                       PrintAndLog("Error: Authentication Failed UL-EV1/NTAG");
-                       return 0;
-               } 
-       }
+       if (!ul_auth_select( &card, tagtype, hasAuthKey, authkeyptr, pack, sizeof(pack))) return -1;
 
        // read pages 0,1,2,3 (should read 4pages)
        status = ul_read(0, data, sizeof(data));
@@ -692,13 +699,10 @@ int CmdHF14AMfUInfo(const char *Cmd){
                ul_switch_off_field();
                PrintAndLog("Error: tag didn't answer to READ");
                return status;
-       }
-       
-       if (status == 16) {
+       } else if (status == 16) {
                ul_print_default(data);
                ndef_print_CC(data+12);
-       }
-       else {
+       }       else {
                locked = true;
        }
 
@@ -741,11 +745,10 @@ int CmdHF14AMfUInfo(const char *Cmd){
                                        uint8_t keySwap[16];
                                        memcpy(keySwap, SwapEndian64(key,16,8), 16);
                                        ulc_print_3deskey(keySwap);
-                                       break;
+                                       return 1;
                                } 
                        }
-                       // reselect for future tests (ntag test)
-                       if ( !ul_select(&card) ) return 0;
+                       return 1;
                }
        }
 
@@ -755,7 +758,7 @@ int CmdHF14AMfUInfo(const char *Cmd){
        if ((tagtype & (UL_EV1_48 | UL_EV1_128))) {
                if (ulev1_print_counters() != 3) {
                        // failed - re-select
-                       if ( !ul_select(&card) ) return 0;
+                       if (!ul_auth_select( &card, tagtype, hasAuthKey, authkeyptr, pack, sizeof(pack))) return -1;
                }
        }
 
@@ -770,7 +773,7 @@ int CmdHF14AMfUInfo(const char *Cmd){
                if (status == 32) ulev1_print_signature( ulev1_signature, sizeof(ulev1_signature));
                else {
                        // re-select
-                       if ( !ul_select(&card) ) return 0;
+                       if (!ul_auth_select( &card, tagtype, hasAuthKey, authkeyptr, pack, sizeof(pack))) return -1;
                }
        }
        
@@ -781,9 +784,12 @@ int CmdHF14AMfUInfo(const char *Cmd){
                        PrintAndLog("Error: tag didn't answer to GETVERSION");
                        ul_switch_off_field();
                        return status;
+               } else if (status == 10) {
+                       ulev1_print_version(version);
+               } else {
+                       locked = true;
+                       if (!ul_auth_select( &card, tagtype, hasAuthKey, authkeyptr, pack, sizeof(pack))) return -1;
                }
-               if (status == 10) ulev1_print_version(version);
-               else locked = true;
 
                uint8_t startconfigblock = 0;
                uint8_t ulev1_conf[16] = {0x00};
@@ -819,7 +825,7 @@ int CmdHF14AMfUInfo(const char *Cmd){
                                        PrintAndLog("Found a default password: %s || Pack: %02X %02X",sprint_hex(key, 4), pack[0], pack[1]);
                                        break;
                                } else {
-                                       if ( !ul_select(&card) ) return 0;
+                                       if (!ul_auth_select( &card, tagtype, hasAuthKey, authkeyptr, pack, sizeof(pack))) return -1;
                        }
                        }
                        if (len < 1) PrintAndLog("password not known");
@@ -833,105 +839,242 @@ int CmdHF14AMfUInfo(const char *Cmd){
 }
 
 //
-//  Mifare Ultralight Write Single Block
+//  Write Single Block
 //
 int CmdHF14AMfUWrBl(const char *Cmd){
-       uint8_t blockNo    = -1;
-       bool chinese_card  = FALSE;
-       uint8_t bldata[16] = {0x00};
-       UsbCommand resp;
 
-       char cmdp = param_getchar(Cmd, 0);
-       if (strlen(Cmd) < 3 || cmdp == 'h' || cmdp == 'H') {
-               PrintAndLog("Usage:  hf mfu wrbl <block number> <block data (8 hex symbols)> [w]");
-               PrintAndLog("       [block number]");
-               PrintAndLog("       [block data] - (8 hex symbols)");
-               PrintAndLog("       [w] - Chinese magic ultralight tag");
-               PrintAndLog("");
-               PrintAndLog("        sample: hf mfu wrbl 0 01020304");
-               PrintAndLog("");                
-               return 0;
-       }       
-       
-       blockNo = param_get8(Cmd, 0);
+       int blockNo = -1;       
+       bool errors = false;
+       bool hasAuthKey = false;
+       bool hasPwdKey = false;
+       bool swapEndian = false;
 
-       if (blockNo > MAX_UL_BLOCKS){
-               PrintAndLog("Error: Maximum number of blocks is 15 for Ultralight Cards!");
-               return 1;
-       }
+       uint8_t cmdp = 0;
+       uint8_t keylen = 0;
+       uint8_t blockdata[20] = {0x00};
+       uint8_t data[16] = {0x00};
+       uint8_t authenticationkey[16] = {0x00};
+       uint8_t *authkeyptr = authenticationkey;
        
-       if (param_gethex(Cmd, 1, bldata, 8)) {
-               PrintAndLog("Block data must include 8 HEX symbols");
-               return 1;
+       // starting with getting tagtype
+       TagTypeUL_t tagtype = GetHF14AMfU_Type();
+       if (tagtype == UL_ERROR) return -1;
+       
+       while(param_getchar(Cmd, cmdp) != 0x00)
+       {
+               switch(param_getchar(Cmd, cmdp))
+               {
+                       case 'h':
+                       case 'H':
+                               return usage_hf_mfu_wrbl();
+                       case 'k':
+                       case 'K':
+                               // EV1/NTAG size key
+                               keylen = param_gethex(Cmd, cmdp+1, data, 8);
+                               if ( !keylen ) {
+                                       memcpy(authenticationkey, data, 4);
+                                       cmdp += 2;
+                                       hasPwdKey = true;
+                                       break;
+                               }
+                               // UL-C size key        
+                               keylen = param_gethex(Cmd, cmdp+1, data, 32);
+                               if (!keylen){
+                                       memcpy(authenticationkey, data, 16);
+                                       cmdp += 2;
+                                       hasAuthKey = true;
+                                       break;
+                               }
+                               PrintAndLog("\nERROR: Key is incorrect length\n");
+                               errors = true; 
+                               break;
+                       case 'b':
+                       case 'B':
+                               blockNo = param_get8(Cmd, cmdp+1);
+                               
+                               uint8_t maxblockno = 0;
+                               for (uint8_t idx = 0; idx < MAX_UL_TYPES; idx++){
+                                       if (tagtype & UL_TYPES_ARRAY[idx])
+                                               maxblockno = UL_MEMORY_ARRAY[idx]+1;
+                               }
+               
+                               if (blockNo < 0) {
+                                       PrintAndLog("Wrong block number");
+                                       errors = true;                                  
+                               }
+                               if (blockNo > maxblockno){
+                                       PrintAndLog("block number to large. Max block is %u/0x%02X \n", maxblockno,maxblockno);
+                                       errors = true;                                                                  
+                               }
+                               cmdp += 2;
+                               break;
+                       case 'l':
+                       case 'L':
+                               swapEndian = true;
+                               cmdp++; 
+                               break;
+                       case 'd':
+                       case 'D':
+                               if ( param_gethex(Cmd, cmdp+1, blockdata, 8) ) {
+                                       PrintAndLog("Block data must include 8 HEX symbols");
+                                       errors = true;
+                                       break;
+                               }
+                               cmdp += 2;
+                               break;
+                       default:
+                               PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd, cmdp));
+                               errors = true;
+                               break;
+               }
+               //Validations
+               if(errors) return usage_hf_mfu_wrbl();
        }
+
+       if ( blockNo == -1 ) return usage_hf_mfu_wrbl();
+       
+       // Swap endianness 
+       if (swapEndian && hasAuthKey) authkeyptr = SwapEndian64(authenticationkey, 16, 8);
+       if (swapEndian && hasPwdKey)  authkeyptr = SwapEndian64(authenticationkey, 4, 4);
+
+
+       if ( blockNo <= 3)              
+               PrintAndLog("Special Block: %0d (0x%02X) [ %s]", blockNo, blockNo, sprint_hex(blockdata, 4));
+       else
+               PrintAndLog("Block: %0d (0x%02X) [ %s]", blockNo, blockNo, sprint_hex(blockdata, 4));
        
-       if (strchr(Cmd,'w') != 0  || strchr(Cmd,'W') != 0 ) {
-               chinese_card = TRUE; 
+       
+
+       //Send write Block
+       UsbCommand c = {CMD_MIFAREU_WRITEBL, {blockNo}};
+       memcpy(c.d.asBytes,blockdata,4);
+
+       if ( hasAuthKey ){
+               c.arg[1] = 1;
+               memcpy(c.d.asBytes+4,authkeyptr,16);
+       }
+       else if ( hasPwdKey ) {
+               c.arg[1] = 2;
+               memcpy(c.d.asBytes+4,authkeyptr,4);
        }
        
-       if ( blockNo <= 3) {
-               if (!chinese_card){
-                       PrintAndLog("Access Denied");
-               } else {
-                       PrintAndLog("--specialblock no:%02x", blockNo);
-                       PrintAndLog("--data: %s", sprint_hex(bldata, 4));
-                       UsbCommand d = {CMD_MIFAREU_WRITEBL, {blockNo}};
-                       memcpy(d.d.asBytes,bldata, 4);
-                       SendCommand(&d);
-                       if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {
-                               uint8_t isOK  = resp.arg[0] & 0xff;
-                               PrintAndLog("isOk:%02x", isOK);
-                       } else {
-                               PrintAndLog("Command execute timeout");
-                       }  
-               }
+       SendCommand(&c);
+       UsbCommand resp;
+       if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {
+               uint8_t isOK  = resp.arg[0] & 0xff;
+               PrintAndLog("isOk:%02x", isOK);
        } else {
-               PrintAndLog("--block no:%02x", blockNo);
-               PrintAndLog("--data: %s", sprint_hex(bldata, 4));               
-               UsbCommand e = {CMD_MIFAREU_WRITEBL, {blockNo}};
-               memcpy(e.d.asBytes,bldata, 4);
-               SendCommand(&e);
-               if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {
-                       uint8_t isOK  = resp.arg[0] & 0xff;
-                       PrintAndLog("isOk:%02x", isOK);
-               } else {
-                       PrintAndLog("Command execute timeout");
-               }
+               PrintAndLog("Command execute timeout");
        }
+       
        return 0;
 }
-
 //
-//  Mifare Ultralight Read Single Block
+//  Read Single Block
 //
 int CmdHF14AMfURdBl(const char *Cmd){
 
-       UsbCommand resp;
-       uint8_t blockNo = -1;   
-       char cmdp = param_getchar(Cmd, 0);
+       int blockNo = -1;       
+       bool errors = false;
+       bool hasAuthKey = false;
+       bool hasPwdKey = false;
+       bool swapEndian = false;
+       uint8_t cmdp = 0;
+       uint8_t keylen = 0;
+       uint8_t data[16] = {0x00};
+       uint8_t authenticationkey[16] = {0x00};
+       uint8_t *authkeyptr = authenticationkey;
+               
+       // starting with getting tagtype
+       TagTypeUL_t tagtype = GetHF14AMfU_Type();
+       if (tagtype == UL_ERROR) return -1;
        
-       if (strlen(Cmd) < 1 || cmdp == 'h' || cmdp == 'H') {    
-               PrintAndLog("Usage:  hf mfu rdbl <block number>");
-               PrintAndLog("        sample: hfu mfu rdbl 0");
-               return 0;
-       }       
+       while(param_getchar(Cmd, cmdp) != 0x00)
+       {
+               switch(param_getchar(Cmd, cmdp))
+               {
+                       case 'h':
+                       case 'H':
+                               return usage_hf_mfu_rdbl();
+                       case 'k':
+                       case 'K':
+                               // EV1/NTAG size key
+                               keylen = param_gethex(Cmd, cmdp+1, data, 8);
+                               if ( !keylen ) {
+                                       memcpy(authenticationkey, data, 4);
+                                       cmdp += 2;
+                                       hasPwdKey = true;
+                                       break;
+                               }
+                               // UL-C size key        
+                               keylen = param_gethex(Cmd, cmdp+1, data, 32);
+                               if (!keylen){
+                                       memcpy(authenticationkey, data, 16);
+                                       cmdp += 2;
+                                       hasAuthKey = true;
+                                       break;
+                               }
+                               PrintAndLog("\nERROR: Key is incorrect length\n");
+                               errors = true; 
+                               break;
+                       case 'b':
+                       case 'B':
+                               blockNo = param_get8(Cmd, cmdp+1);
+                               
+                               uint8_t maxblockno = 0;
+                               for (uint8_t idx = 0; idx < MAX_UL_TYPES; idx++){
+                                       if (tagtype & UL_TYPES_ARRAY[idx])
+                                               maxblockno = UL_MEMORY_ARRAY[idx]+1;
+                               }
                
-       blockNo = param_get8(Cmd, 0);
-
-       if (blockNo > MAX_UL_BLOCKS){
-          PrintAndLog("Error: Maximum number of blocks is 15 for Ultralight");
-          return 1;
+                               if (blockNo < 0) {
+                                       PrintAndLog("Wrong block number");
+                                       errors = true;                                  
+                               }
+                               if (blockNo > maxblockno){
+                                       PrintAndLog("block number to large. Max block is %u/0x%02X \n", maxblockno,maxblockno);
+                                       errors = true;                                                                  
+                               }
+                               cmdp += 2;
+                               break;
+                       case 'l':
+                       case 'L':
+                               swapEndian = true;
+                               cmdp++; 
+                               break;                          
+                       default:
+                               PrintAndLog("Unknown parameter '%c'", param_getchar(Cmd, cmdp));
+                               errors = true;
+                               break;
+               }
+               //Validations
+               if(errors) return usage_hf_mfu_rdbl();
        }
 
+       if ( blockNo == -1 ) return usage_hf_mfu_rdbl();
+       
+       // Swap endianness 
+       if (swapEndian && hasAuthKey) authkeyptr = SwapEndian64(authenticationkey, 16, 8);
+       if (swapEndian && hasPwdKey)  authkeyptr = SwapEndian64(authenticationkey, 4, 4);
+       
+       //Read Block
        UsbCommand c = {CMD_MIFAREU_READBL, {blockNo}};
+       if ( hasAuthKey ){
+               c.arg[1] = 1;
+               memcpy(c.d.asBytes,authkeyptr,16);
+       }
+       else if ( hasPwdKey ) {
+               c.arg[1] = 2;
+               memcpy(c.d.asBytes,authkeyptr,4);
+       }
+       
        SendCommand(&c);
-
-
+       UsbCommand resp;
        if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {
                uint8_t isOK = resp.arg[0] & 0xff;
                if (isOK) {
                        uint8_t *data = resp.d.asBytes;
-                       PrintAndLog("Block: %0d (0x%02X) [ %s]", (int)blockNo, blockNo, sprint_hex(data, 4));
+                       PrintAndLog("Block: %0d (0x%02X) [ %s]", blockNo, blockNo, sprint_hex(data, 4));
                }
                else {
                        PrintAndLog("Failed reading block: (%02x)", isOK);
@@ -939,36 +1082,36 @@ int CmdHF14AMfURdBl(const char *Cmd){
        } else {
                PrintAndLog("Command execute time-out");
        }
-       
        return 0;
 }
 
-int usage_hf_mfu_info(void)
-{
+int usage_hf_mfu_info(void) {
        PrintAndLog("It gathers information about the tag and tries to detect what kind it is.");
        PrintAndLog("Sometimes the tags are locked down, and you may need a key to be able to read the information");
        PrintAndLog("The following tags can be identified:\n");
-       PrintAndLog("Ultralight, Ultralight-C, Ultralight EV1");
-       PrintAndLog("NTAG 213, NTAG 215, NTAG 216");
+       PrintAndLog("Ultralight, Ultralight-C, Ultralight EV1, NTAG 203, NTAG 210,");
+       PrintAndLog("NTAG 212, NTAG 213, NTAG 215, NTAG 216, NTAG I2C 1K & 2K");
        PrintAndLog("my-d, my-d NFC, my-d move, my-d move NFC\n");
-       PrintAndLog("Usage:  hf mfu info k <key>");
+       PrintAndLog("Usage:  hf mfu info k <key> l");
        PrintAndLog("  Options : ");
-       PrintAndLog("  k <key> : key for authentication [UL-C 16bytes, EV1/NTAG 4bytes]");
+       PrintAndLog("  k <key> : (optional) key for authentication [UL-C 16bytes, EV1/NTAG 4bytes]");
+       PrintAndLog("  l       : (optional) swap entered key's endianness");
        PrintAndLog("");
        PrintAndLog("   sample : hf mfu info");
-       PrintAndLog("          : hf mfu info k 11223344");
+       PrintAndLog("          : hf mfu info k 00112233445566778899AABBCCDDEEFF");
+       PrintAndLog("          : hf mfu info k AABBCCDDD");
        return 0;
 }
 
-int usage_hf_mfu_dump(void)
-{
+int usage_hf_mfu_dump(void) {
        PrintAndLog("Reads all pages from Ultralight, Ultralight-C, Ultralight EV1");
+       PrintAndLog("NTAG 203, NTAG 210, NTAG 212, NTAG 213, NTAG 215, NTAG 216");
        PrintAndLog("and saves binary dump into the file `filename.bin` or `cardUID.bin`");
        PrintAndLog("It autodetects card type.\n");     
-       PrintAndLog("Usage:  hf mfu dump l k <key> n <filename w/o .bin> p <> q <>");
+       PrintAndLog("Usage:  hf mfu dump k <key> l n <filename w/o .bin>");
        PrintAndLog("  Options : ");
-       PrintAndLog("  k <key> : key for authentication [UL-C 16bytes, EV1/NTAG 4bytes]");
-       PrintAndLog("  l       : swap entered key's endianness for auth");
+       PrintAndLog("  k <key> : (optional) key for authentication [UL-C 16bytes, EV1/NTAG 4bytes]");
+       PrintAndLog("  l       : (optional) swap entered key's endianness");
        PrintAndLog("  n <FN > : filename w/o .bin to save the dump as");       
        PrintAndLog("  p <Pg > : starting Page number to manually set a page to start the dump at");    
        PrintAndLog("  q <qty> : number of Pages to manually set how many pages to dump");      
@@ -977,6 +1120,35 @@ int usage_hf_mfu_dump(void)
        PrintAndLog("   sample : hf mfu dump");
        PrintAndLog("          : hf mfu dump n myfile");
        PrintAndLog("          : hf mfu dump k 00112233445566778899AABBCCDDEEFF");
+       PrintAndLog("          : hf mfu dump k AABBCCDDD\n");
+       return 0;
+}
+
+int usage_hf_mfu_rdbl(void) {
+       PrintAndLog("Read a block and print. It autodetects card type.\n");     
+       PrintAndLog("Usage:  hf mfu rdbl b <block number> k <key> l\n");
+       PrintAndLog("  Options:");
+       PrintAndLog("  b <no>  : block to read");
+       PrintAndLog("  k <key> : (optional) key for authentication [UL-C 16bytes, EV1/NTAG 4bytes]");
+       PrintAndLog("  l       : (optional) swap entered key's endianness");    
+       PrintAndLog("");
+       PrintAndLog("   sample : hf mfu rdbl b 0");
+       PrintAndLog("          : hf mfu rdbl b 0 k 00112233445566778899AABBCCDDEEFF");
+       PrintAndLog("          : hf mfu rdbl b 0 k AABBCCDDD\n");
+       return 0;
+}
+
+int usage_hf_mfu_wrbl(void) {
+       PrintAndLog("Write a block. It autodetects card type.\n");              
+       PrintAndLog("Usage:  hf mfu wrbl b <block number> d <data> k <key> l\n");
+       PrintAndLog("  Options:");      
+       PrintAndLog("  b <no>   : block to write");
+       PrintAndLog("  d <data> : block data - (8 hex symbols)");
+       PrintAndLog("  k <key>  : (optional) key for authentication [UL-C 16bytes, EV1/NTAG 4bytes]");
+       PrintAndLog("  l        : (optional) swap entered key's endianness");   
+       PrintAndLog("");
+       PrintAndLog("    sample : hf mfu wrbl b 0 d 01234567");
+       PrintAndLog("           : hf mfu wrbl b 0 d 01234567 k AABBCCDDD\n");
        return 0;
 }
 
@@ -997,14 +1169,14 @@ int CmdHF14AMfUDump(const char *Cmd){
        bool bit[16]  = {0x00};
        bool bit2[16] = {0x00};
        uint8_t data[1024] = {0x00};
-       bool hasPwd = false;
+       bool hasAuthKey = false;
        int i = 0;
        int Pages = 16;
        bool tmplockbit = false;
        uint8_t dataLen=0;
        uint8_t cmdp =0;
-       uint8_t key[16] = {0x00};
-       uint8_t *keyPtr = key;
+       uint8_t authenticationkey[16] = {0x00};
+       uint8_t *authKeyPtr = authenticationkey;
        size_t fileNlen = 0;
        bool errors = false;
        bool swapEndian = false;
@@ -1022,17 +1194,15 @@ int CmdHF14AMfUDump(const char *Cmd){
                case 'k':
                case 'K':
                        dataLen = param_getstr(Cmd, cmdp+1, tempStr);
-                       if (dataLen == 32) //ul-c
-                               errors = param_gethex(tempStr, 0, key, dataLen);
-                       else if (dataLen == 8) //ev1/ntag
-                               errors = param_gethex(tempStr, 0, key, dataLen);
-                       else{
+                       if (dataLen == 32 || dataLen == 8) { //ul-c or ev1/ntag key length
+                               errors = param_gethex(tempStr, 0, authenticationkey, dataLen);
+                               dataLen /= 2;
+                       } else {
                                PrintAndLog("\nERROR: Key is incorrect length\n");
                                errors = true;
                        }
-                               
                        cmdp += 2;
-                               hasPwd = true;
+                       hasAuthKey = true;
                        break;
                case 'l':
                case 'L':
@@ -1069,8 +1239,8 @@ int CmdHF14AMfUDump(const char *Cmd){
        //Validations
        if(errors) return usage_hf_mfu_dump();
        
-       if (swapEndian && dataLen == 32)
-               keyPtr = SwapEndian64(data, 16, 8);
+       if (swapEndian && hasAuthKey) 
+               authKeyPtr = SwapEndian64(authenticationkey, dataLen, (dataLen == 16) ? 8 : 4);
 
        TagTypeUL_t tagtype = GetHF14AMfU_Type();
        if (tagtype == UL_ERROR) return -1;
@@ -1083,13 +1253,13 @@ int CmdHF14AMfUDump(const char *Cmd){
        ul_print_type(tagtype, 0);
        PrintAndLog("Reading tag memory...");
        UsbCommand c = {CMD_MIFAREU_READCARD, {startPage,Pages}};
-       if ( hasPwd ) { 
+       if ( hasAuthKey ) { 
                if (tagtype & UL_C)
                        c.arg[2] = 1; //UL_C auth
                else
                        c.arg[2] = 2; //UL_EV1/NTAG auth
 
-               memcpy(c.d.asBytes, key, dataLen/2);
+               memcpy(c.d.asBytes, authKeyPtr, dataLen);
        }
        SendCommand(&c);
        UsbCommand resp;
@@ -1133,12 +1303,14 @@ int CmdHF14AMfUDump(const char *Cmd){
        }
 
        // add keys to block dump
-       if (hasPwd && (tagtype & UL_C)){ //UL_C
-               memcpy(data + Pages*4, key, dataLen/2);
-               Pages += 4;
-       } else if (hasPwd) { //not sure output is in correct location.
-               memcpy(data + Pages*4, key, dataLen/2);
-               Pages += 1;
+       if (hasAuthKey) {
+               if (!swapEndian) {
+                       authKeyPtr = SwapEndian64(authenticationkey, dataLen, (dataLen == 16) ? 8 : 4);
+                       memcpy(data + Pages*4, authKeyPtr, dataLen);
+               } else {
+                       memcpy(data + Pages*4, authenticationkey, dataLen);
+               }
+               Pages += dataLen/4;  //not sure output is in correct location for all tag types.
        }
        
        for (i = 0; i < Pages; ++i) {
@@ -1190,7 +1362,7 @@ int CmdHF14AMfUDump(const char *Cmd){
                        case 43: tmplockbit = bit2[9]; break;  //auth1
                        default: break;
                }
-               PrintAndLog("Block %02x:%s [%d]", i,sprint_hex(data + i * 4, 4),tmplockbit);
+               PrintAndLog("Block %02x:%s [%d] {%.4s}", i, sprint_hex(data + i * 4, 4), tmplockbit, data+i*4);
        }  
        
        // user supplied filename?
@@ -1357,142 +1529,6 @@ int CmdTestDES(const char * cmd)
 }
 **/
 
-//
-// Ultralight C Read Single Block
-//
-int CmdHF14AMfUCRdBl(const char *Cmd)
-{
-       UsbCommand resp;
-       bool hasPwd = FALSE;
-       uint8_t blockNo = -1;
-       uint8_t key[16];
-       char cmdp = param_getchar(Cmd, 0);
-       
-       if (strlen(Cmd) < 1 || cmdp == 'h' || cmdp == 'H') {
-               PrintAndLog("Usage:  hf mfu crdbl  <block number> <key>");
-               PrintAndLog("");
-               PrintAndLog("sample: hf mfu crdbl 0");
-               PrintAndLog("        hf mfu crdbl 0 00112233445566778899AABBCCDDEEFF");
-               return 0;
-       }       
-               
-       blockNo = param_get8(Cmd, 0);
-       if (blockNo < 0) {
-               PrintAndLog("Wrong block number");
-               return 1;
-       }
-       
-       if (blockNo > MAX_ULC_BLOCKS ){
-               PrintAndLog("Error: Maximum number of blocks is 47 for Ultralight-C");
-               return 1;
-       } 
-       
-       // key
-       if ( strlen(Cmd) > 3){
-               if (param_gethex(Cmd, 1, key, 32)) {
-                       PrintAndLog("Key must include %d HEX symbols", 32);
-                       return 1;
-               } else {
-                       hasPwd = TRUE;
-               }       
-       }       
-
-       //Read Block
-       UsbCommand c = {CMD_MIFAREU_READBL, {blockNo}};
-       if ( hasPwd ) {
-               c.arg[1] = 1;
-               memcpy(c.d.asBytes,key,16);
-       }
-       SendCommand(&c);
-
-       if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {
-               uint8_t isOK = resp.arg[0] & 0xff;
-               if (isOK) {
-                       uint8_t *data = resp.d.asBytes;
-                       PrintAndLog("Block: %0d (0x%02X) [ %s]", (int)blockNo, blockNo, sprint_hex(data, 4));
-               }
-               else {
-                       PrintAndLog("Failed reading block: (%02x)", isOK);
-               }
-       } else {
-               PrintAndLog("Command execute time-out");
-       }
-       return 0;
-}
-
-//
-//  Mifare Ultralight C Write Single Block
-//
-int CmdHF14AMfUCWrBl(const char *Cmd){
-       
-       uint8_t blockNo = -1;
-       bool chinese_card = FALSE;
-       uint8_t bldata[16] = {0x00};
-       UsbCommand resp;
-
-       char cmdp = param_getchar(Cmd, 0);
-       
-       if (strlen(Cmd) < 3 || cmdp == 'h' || cmdp == 'H') {    
-               PrintAndLog("Usage:  hf mfu cwrbl <block number> <block data (8 hex symbols)> [w]");
-               PrintAndLog("       [block number]");
-               PrintAndLog("       [block data] - (8 hex symbols)");
-               PrintAndLog("       [w] - Chinese magic ultralight tag");
-               PrintAndLog("");
-               PrintAndLog("        sample: hf mfu cwrbl 0 01020304");
-               PrintAndLog("");
-               return 0;
-       }
-       
-       blockNo = param_get8(Cmd, 0);
-       if (blockNo > MAX_ULC_BLOCKS ){
-               PrintAndLog("Error: Maximum number of blocks is 47 for Ultralight-C Cards!");
-               return 1;
-       }
-       
-       if (param_gethex(Cmd, 1, bldata, 8)) {
-               PrintAndLog("Block data must include 8 HEX symbols");
-               return 1;
-       }
-       
-       if (strchr(Cmd,'w') != 0  || strchr(Cmd,'W') != 0 ) {
-               chinese_card = TRUE; 
-       }
-       
-       if ( blockNo <= 3 ) {
-               if (!chinese_card){
-                        PrintAndLog("Access Denied");  
-                       return 1;
-               } else {
-                       PrintAndLog("--Special block no: 0x%02x", blockNo);
-                       PrintAndLog("--Data: %s", sprint_hex(bldata, 4));
-                       UsbCommand d = {CMD_MIFAREU_WRITEBL, {blockNo}};
-                       memcpy(d.d.asBytes,bldata, 4);
-                       SendCommand(&d);
-                       if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {
-                               uint8_t isOK  = resp.arg[0] & 0xff;
-                               PrintAndLog("isOk:%02x", isOK);
-                       } else {
-                               PrintAndLog("Command execute timeout");
-                               return 1;
-                       }  
-               }       
-       } else {
-                       PrintAndLog("--Block no : 0x%02x", blockNo);
-                       PrintAndLog("--Data: %s", sprint_hex(bldata, 4));               
-                       UsbCommand e = {CMD_MIFAREU_WRITEBL, {blockNo}};
-                       memcpy(e.d.asBytes,bldata, 4);
-                       SendCommand(&e);
-                       if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {
-                               uint8_t isOK  = resp.arg[0] & 0xff;
-                               PrintAndLog("isOk : %02x", isOK);
-                       } else {
-                               PrintAndLog("Command execute timeout");
-                               return 1;
-                       }
-       }
-       return 0;
-}
-
 // 
 // Mifare Ultralight C - Set password
 //
@@ -1724,10 +1760,8 @@ static command_t CommandTable[] =
        {"dbg",         CmdHF14AMfDbg,          0, "Set default debug mode"},
        {"info",        CmdHF14AMfUInfo,        0, "Tag information"},
        {"dump",        CmdHF14AMfUDump,        0, "Dump Ultralight / Ultralight-C tag to binary file"},
-       {"rdbl",        CmdHF14AMfURdBl,        0, "Read block  - Ultralight"},
-       {"wrbl",        CmdHF14AMfUWrBl,        0, "Write block - Ultralight"},    
-       {"crdbl",       CmdHF14AMfUCRdBl,       0, "Read block        - Ultralight C"},
-       {"cwrbl",       CmdHF14AMfUCWrBl,       0, "Write block       - Ultralight C"},
+       {"rdbl",        CmdHF14AMfURdBl,        0, "Read block"},
+       {"wrbl",        CmdHF14AMfUWrBl,        0, "Write block"},    
        {"cauth",       CmdHF14AMfucAuth,       0, "Authentication    - Ultralight C"},
        {"setpwd",      CmdHF14AMfucSetPwd, 1, "Set 3des password - Ultralight-C"},
        {"setuid",      CmdHF14AMfucSetUid, 1, "Set UID - MAGIC tags only"},
Impressum, Datenschutz