]> cvs.zerfleddert.de Git - proxmark3-svn/commitdiff
ADD: new defines: iso14443_CMD_AUTH_KEYA, iso14443_CMD_AUTH_KEYB, iso14443_CMD_AUTH_R...
authoriceman1001 <iceman@iuse.se>
Wed, 7 Jan 2015 16:42:57 +0000 (17:42 +0100)
committericeman1001 <iceman@iuse.se>
Wed, 7 Jan 2015 16:42:57 +0000 (17:42 +0100)
REM: removed old comments in bootrom.c
CHG: mifare ultralight & desfire commands inside arm has been cleaned up. Next step is to refactor it into armsrc/mifaredesfire.c

armsrc/apps.h
armsrc/mifarecmd.c
armsrc/mifaredesfire.c
armsrc/mifareutil.c
bootrom/bootrom.c
client/Makefile
client/cmdhf.c
client/cmdhfmf.c
client/cmdhfmfu.c
client/flasher.c

index 81124a1a3363e23c1330687b18cad14ddca966ab..30010f1b3290b34a1799743fa22790529e98bc98 100644 (file)
@@ -227,7 +227,7 @@ void        ReaderMifareDES(uint32_t param, uint32_t param2, uint8_t * datain);
 int    DesfireAPDU(uint8_t *cmd, size_t cmd_len, uint8_t *dataout);
 size_t CreateAPDU( uint8_t *datain, size_t len, uint8_t *dataout);
 void   OnSuccess();
 int    DesfireAPDU(uint8_t *cmd, size_t cmd_len, uint8_t *dataout);
 size_t CreateAPDU( uint8_t *datain, size_t len, uint8_t *dataout);
 void   OnSuccess();
-void   OnError();
+void   OnError(uint8_t reason);
 
 
 
 
 
 
index d2497977df350e48940fb6de07ec1b823cebe19b..27c0ce139b3cc066745712ca6279594b7ef725b6 100644 (file)
@@ -148,48 +148,40 @@ void MifareUC_Auth2(uint32_t arg0, uint8_t *datain){
 \r
 void MifareUReadBlock(uint8_t arg0,uint8_t *datain)\r
 {\r
 \r
 void MifareUReadBlock(uint8_t arg0,uint8_t *datain)\r
 {\r
-    // params\r
        uint8_t blockNo = arg0;\r
        uint8_t blockNo = arg0;\r
-       \r
-       // variables\r
-       byte_t isOK = 0;\r
-       byte_t dataoutbuf[16] = {0x00};\r
+       byte_t dataout[16] = {0x00};\r
        uint8_t uid[10] = {0x00};\r
        uint32_t cuid;\r
     \r
        uint8_t uid[10] = {0x00};\r
        uint32_t cuid;\r
     \r
-       // clear trace\r
-       iso14a_clear_trace();\r
-       iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r
-    \r
        LED_A_ON();\r
        LED_B_OFF();\r
        LED_C_OFF();\r
        LED_A_ON();\r
        LED_B_OFF();\r
        LED_C_OFF();\r
+\r
+       iso14a_clear_trace();\r
+       iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r
     \r
     \r
-       while (true) {\r
-               if(!iso14443a_select_card(uid, NULL, &cuid)) {\r
-            if (MF_DBGLEVEL >= 1)      Dbprintf("Can't select card");\r
-                       break;\r
-               };\r
-        \r
-               if(mifare_ultra_readblock(cuid, blockNo, dataoutbuf)) {\r
-            if (MF_DBGLEVEL >= 1)      Dbprintf("Read block error");\r
-                       break;\r
-               };\r
-        \r
-               if(mifare_ultra_halt(cuid)) {\r
-            if (MF_DBGLEVEL >= 1)      Dbprintf("Halt error");\r
-                       break;\r
-               };\r
-               \r
-               isOK = 1;\r
-               break;\r
-       }\r
+       int len = iso14443a_select_card(uid, NULL, &cuid);\r
+       if(!len) {\r
+               if (MF_DBGLEVEL >= MF_DBG_ERROR)        Dbprintf("Can't select card");\r
+               OnError(1);\r
+               return;\r
+       };\r
        \r
        \r
-       if (MF_DBGLEVEL >= 2)   DbpString("READ BLOCK FINISHED");\r
-    \r
-       LED_B_ON();\r
-    cmd_send(CMD_ACK,isOK,0,0,dataoutbuf,16);\r
-       LED_B_OFF();\r
+       len = mifare_ultra_readblock(cuid, blockNo, dataout);\r
+       if(len) {\r
+               if (MF_DBGLEVEL >= MF_DBG_ERROR)        Dbprintf("Read block error");\r
+               OnError(2);\r
+               return;\r
+       };\r
+       \r
+       len = mifare_ultra_halt(cuid);\r
+       if(len) {\r
+               if (MF_DBGLEVEL >= MF_DBG_ERROR)        Dbprintf("Halt error");\r
+               OnError(3);\r
+               return;\r
+       };\r
+               \r
+    cmd_send(CMD_ACK,1,0,0,dataout,16);\r
        FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
        LEDsoff();\r
 }\r
        FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
        LEDsoff();\r
 }\r
@@ -268,58 +260,62 @@ void MifareUReadCard(uint8_t arg0, int arg1, uint8_t *datain)
        uint8_t sectorNo = arg0;\r
        int Pages = arg1;\r
        int count_Pages = 0;\r
        uint8_t sectorNo = arg0;\r
        int Pages = arg1;\r
        int count_Pages = 0;\r
-       byte_t dataoutbuf[176] = {0x00};;\r
+       byte_t dataout[176] = {0x00};;\r
        uint8_t uid[10] = {0x00};\r
        uint32_t cuid;\r
        uint8_t uid[10] = {0x00};\r
        uint32_t cuid;\r
-\r
-       iso14a_clear_trace();\r
-       iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r
-\r
+       \r
        LED_A_ON();\r
        LED_B_OFF();\r
        LED_C_OFF();\r
        LED_A_ON();\r
        LED_B_OFF();\r
        LED_C_OFF();\r
-       \r
+\r
        if (MF_DBGLEVEL >= MF_DBG_ALL) \r
                Dbprintf("Pages %d",Pages);\r
        \r
        if (MF_DBGLEVEL >= MF_DBG_ALL) \r
                Dbprintf("Pages %d",Pages);\r
        \r
-       if (!iso14443a_select_card(uid, NULL, &cuid)) {\r
+       iso14a_clear_trace();\r
+       iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r
+\r
+       int len = iso14443a_select_card(uid, NULL, &cuid);\r
+       \r
+       if (!len) {\r
                if (MF_DBGLEVEL >= MF_DBG_ERROR)\r
                        Dbprintf("Can't select card");\r
                if (MF_DBGLEVEL >= MF_DBG_ERROR)\r
                        Dbprintf("Can't select card");\r
-               OnError();\r
+               OnError(1);\r
                return;\r
        }\r
        \r
        for (int i = 0; i < Pages; i++){\r
                return;\r
        }\r
        \r
        for (int i = 0; i < Pages; i++){\r
-               if (mifare_ultra_readblock(cuid, sectorNo * 4 + i, dataoutbuf + 4 * i)) {\r
+       \r
+               len = mifare_ultra_readblock(cuid, sectorNo * 4 + i, dataout + 4 * i);\r
+               \r
+               if (len) {\r
                        if (MF_DBGLEVEL >= MF_DBG_ERROR)\r
                                Dbprintf("Read block %d error",i);\r
                        if (MF_DBGLEVEL >= MF_DBG_ERROR)\r
                                Dbprintf("Read block %d error",i);\r
-                       OnError();\r
+                       OnError(2);\r
                        return;\r
                } else {\r
                        count_Pages++;\r
                }\r
        }\r
                \r
                        return;\r
                } else {\r
                        count_Pages++;\r
                }\r
        }\r
                \r
-       if (mifare_ultra_halt(cuid)) {\r
+       len = mifare_ultra_halt(cuid);\r
+       if (len) {\r
                if (MF_DBGLEVEL >= MF_DBG_ERROR)\r
                        Dbprintf("Halt error");\r
                if (MF_DBGLEVEL >= MF_DBG_ERROR)\r
                        Dbprintf("Halt error");\r
-               OnError();\r
+               OnError(3);\r
                return;\r
        }\r
        \r
        if (MF_DBGLEVEL >= MF_DBG_ALL) {\r
                return;\r
        }\r
        \r
        if (MF_DBGLEVEL >= MF_DBG_ALL) {\r
-               Dbprintf("Pages read %d",count_Pages);\r
-               DbpString("Read card finished");\r
+               Dbprintf("Pages read %d", count_Pages);\r
        }\r
 \r
        }\r
 \r
-       int len = 16*4; //64 bytes\r
+       len = 16*4; //64 bytes\r
        \r
        // Read a UL-C\r
        if (Pages == 44 && count_Pages > 16) \r
                len = 176;\r
 \r
        \r
        // Read a UL-C\r
        if (Pages == 44 && count_Pages > 16) \r
                len = 176;\r
 \r
-       cmd_send(CMD_ACK, 1, 0, 0, dataoutbuf, len);\r
-       \r
+       cmd_send(CMD_ACK, 1, 0, 0, dataout, len);       \r
        FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
        LEDsoff();\r
 }\r
        FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
        LEDsoff();\r
 }\r
@@ -1132,13 +1128,12 @@ void MifareCIdent(){
        cmd_send(CMD_ACK,isOK,0,0,0,0);\r
 }\r
 \r
        cmd_send(CMD_ACK,isOK,0,0,0,0);\r
 }\r
 \r
-                       //\r
+//\r
 // DESFIRE\r
 //\r
 \r
 void Mifare_DES_Auth1(uint8_t arg0, uint8_t *datain){\r
 // DESFIRE\r
 //\r
 \r
 void Mifare_DES_Auth1(uint8_t arg0, uint8_t *datain){\r
-       // variables\r
-       byte_t isOK = 0;\r
+\r
        byte_t dataout[11] = {0x00};\r
        uint8_t uid[10] = {0x00};\r
        uint32_t cuid;\r
        byte_t dataout[11] = {0x00};\r
        uint8_t uid[10] = {0x00};\r
        uint32_t cuid;\r
@@ -1146,19 +1141,24 @@ void Mifare_DES_Auth1(uint8_t arg0, uint8_t *datain){
        iso14a_clear_trace();\r
        iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r
 \r
        iso14a_clear_trace();\r
        iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r
 \r
-       if(!iso14443a_select_card(uid, NULL, &cuid)) {\r
-          if (MF_DBGLEVEL >= 1)        Dbprintf("Can't select card, something went wrong before auth");\r
+       int len = iso14443a_select_card(uid, NULL, &cuid);\r
+       if(!len) {\r
+               if (MF_DBGLEVEL >= MF_DBG_ERROR)        \r
+                       Dbprintf("Can't select card");\r
+               OnError(1);\r
+               return;\r
        };\r
 \r
        if(mifare_desfire_des_auth1(cuid, dataout)){\r
        };\r
 \r
        if(mifare_desfire_des_auth1(cuid, dataout)){\r
-         if (MF_DBGLEVEL >= 1) \r
-               Dbprintf("Authentication part1: Fail."); \r
+               if (MF_DBGLEVEL >= MF_DBG_ERROR)        \r
+                       Dbprintf("Authentication part1: Fail.");\r
+               OnError(4);\r
+               return;\r
        }\r
 \r
        }\r
 \r
-       isOK = 1;\r
-       if (MF_DBGLEVEL >= 2) DbpString("AUTH 1 FINISHED");\r
+       if (MF_DBGLEVEL >= MF_DBG_EXTENDED) DbpString("AUTH 1 FINISHED");\r
     \r
     \r
-    cmd_send(CMD_ACK,isOK,cuid,0,dataout, sizeof(dataout));\r
+    cmd_send(CMD_ACK,1,cuid,0,dataout, sizeof(dataout));\r
 }\r
 \r
 void Mifare_DES_Auth2(uint32_t arg0, uint8_t *datain){\r
 }\r
 \r
 void Mifare_DES_Auth2(uint32_t arg0, uint8_t *datain){\r
@@ -1169,24 +1169,20 @@ void Mifare_DES_Auth2(uint32_t arg0, uint8_t *datain){
        byte_t dataout[12] = {0x00};\r
     \r
        memcpy(key, datain, 16);\r
        byte_t dataout[12] = {0x00};\r
     \r
        memcpy(key, datain, 16);\r
-   \r
-       LED_A_ON();\r
-       LED_B_OFF();\r
-       LED_C_OFF();\r
        \r
        isOK = mifare_desfire_des_auth2(cuid, key, dataout);\r
        \r
        \r
        isOK = mifare_desfire_des_auth2(cuid, key, dataout);\r
        \r
-       if(isOK){\r
-               if (MF_DBGLEVEL >= 2) \r
-                       DbpString("AUTH 2 FINISHED");\r
-               cmd_send(CMD_ACK,isOK,0,0,dataout,sizeof(dataout));\r
-       }\r
-       else {\r
-           if (MF_DBGLEVEL >= 2) \r
+       if( isOK) {\r
+           if (MF_DBGLEVEL >= MF_DBG_EXTENDED) \r
                        Dbprintf("Authentication part2: Failed");  \r
                        Dbprintf("Authentication part2: Failed");  \r
-               OnError();\r
+               OnError(4);\r
+               return;\r
        }\r
 \r
        }\r
 \r
+       if (MF_DBGLEVEL >= MF_DBG_EXTENDED) \r
+               DbpString("AUTH 2 FINISHED");\r
+\r
+       cmd_send(CMD_ACK, isOK, 0, 0, dataout, sizeof(dataout));\r
        FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
        LEDsoff();\r
 }
\ No newline at end of file
        FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
        LEDsoff();\r
 }
\ No newline at end of file
index e064c33aa45068e5924075d4b1cbcddcfec9988a..5737615106c9a0cddd2d8a92d3763807c97d6b01 100644 (file)
@@ -29,8 +29,9 @@ bool InitDesfireCard(){
        int len = iso14443a_select_card(NULL,card,NULL);
 
        if (!len) {
        int len = iso14443a_select_card(NULL,card,NULL);
 
        if (!len) {
-               if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");
-               OnError();
+               if (MF_DBGLEVEL >= MF_DBG_ERROR)
+                       Dbprintf("Can't select card");
+               OnError(1);
                return false;
        }
        return true;
                return false;
        }
        return true;
@@ -78,7 +79,7 @@ void MifareSendCommand(uint8_t arg0, uint8_t arg1, uint8_t *datain){
        }
 
        if ( !len ) {
        }
 
        if ( !len ) {
-               OnError();
+               OnError(2);
                return;
        }
        
                return;
        }
        
@@ -116,10 +117,10 @@ void MifareDesfireGetInformation(){
        iso14a_card_select_t *card = (iso14a_card_select_t*)cardbuf;
        byte_t isOK = iso14443a_select_card(NULL, card, NULL);
        if ( isOK == 0) {
        iso14a_card_select_t *card = (iso14a_card_select_t*)cardbuf;
        byte_t isOK = iso14443a_select_card(NULL, card, NULL);
        if ( isOK == 0) {
-               if (MF_DBGLEVEL >= 1) {
+               if (MF_DBGLEVEL >= MF_DBG_ERROR) {
                        Dbprintf("Can't select card");
                }
                        Dbprintf("Can't select card");
                }
-               OnError();
+               OnError(1);
                return;
        }
 
                return;
        }
 
@@ -135,7 +136,7 @@ void MifareDesfireGetInformation(){
        len =  DesfireAPDU(cmd, cmd_len, resp);
        if ( !len ) {
                print_result("ERROR <--: ", resp, len); 
        len =  DesfireAPDU(cmd, cmd_len, resp);
        if ( !len ) {
                print_result("ERROR <--: ", resp, len); 
-               OnError();
+               OnError(2);
                return;
        }
        
                return;
        }
        
@@ -148,7 +149,7 @@ void MifareDesfireGetInformation(){
        len =  DesfireAPDU(cmd, cmd_len, resp);
        if ( !len ) {
                print_result("ERROR <--: ", resp, len); 
        len =  DesfireAPDU(cmd, cmd_len, resp);
        if ( !len ) {
                print_result("ERROR <--: ", resp, len); 
-               OnError();
+               OnError(2);
                return;
        }       
        
                return;
        }       
        
@@ -160,7 +161,7 @@ void MifareDesfireGetInformation(){
        len =  DesfireAPDU(cmd, cmd_len, resp);
        if ( !len ) {
                print_result("ERROR <--: ", resp, len); 
        len =  DesfireAPDU(cmd, cmd_len, resp);
        if ( !len ) {
                print_result("ERROR <--: ", resp, len); 
-               OnError();
+               OnError(2);
                return;
        }
        
                return;
        }
        
@@ -196,10 +197,6 @@ void MifareDES_Auth1(uint8_t mode, uint8_t algo, uint8_t keyno,  uint8_t *datain
        uint8_t encBoth[32] = {0x00};
 
        InitDesfireCard();
        uint8_t encBoth[32] = {0x00};
 
        InitDesfireCard();
-
-       LED_A_ON();
-       LED_B_OFF();
-       LED_C_OFF();
        
        // 3 olika sätt att authenticera.   AUTH (CRC16) , AUTH_ISO (CRC32) , AUTH_AES (CRC32)
        // 4 olika crypto algo   DES, 3DES, 3K3DES, AES
        
        // 3 olika sätt att authenticera.   AUTH (CRC16) , AUTH_ISO (CRC32) , AUTH_AES (CRC32)
        // 4 olika crypto algo   DES, 3DES, 3K3DES, AES
@@ -228,17 +225,17 @@ void MifareDES_Auth1(uint8_t mode, uint8_t algo, uint8_t keyno,  uint8_t *datain
             cmd[1] = keyno;  //keynumber
             len = DesfireAPDU(cmd, 2, resp);
             if ( !len ) {
             cmd[1] = keyno;  //keynumber
             len = DesfireAPDU(cmd, 2, resp);
             if ( !len ) {
-                if (MF_DBGLEVEL >= 1) {
+                if (MF_DBGLEVEL >= MF_DBG_ERROR) {
                     DbpString("Authentication failed. Card timeout.");
                 }
                     DbpString("Authentication failed. Card timeout.");
                 }
-                OnError();
+                OnError(3);
                 return;
             }
             
             if ( resp[2] == 0xaf ){
             } else {
                 DbpString("Authetication failed. Invalid key number.");
                 return;
             }
             
             if ( resp[2] == 0xaf ){
             } else {
                 DbpString("Authetication failed. Invalid key number.");
-                OnError();
+                OnError(3);
                 return;
             }
             
                 return;
             }
             
@@ -270,10 +267,10 @@ void MifareDES_Auth1(uint8_t mode, uint8_t algo, uint8_t keyno,  uint8_t *datain
             
             len = DesfireAPDU(cmd, 17, resp);
             if ( !len ) {
             
             len = DesfireAPDU(cmd, 17, resp);
             if ( !len ) {
-                if (MF_DBGLEVEL >= 1) {
+                if (MF_DBGLEVEL >= MF_DBG_ERROR) {
                     DbpString("Authentication failed. Card timeout.");
                 }
                     DbpString("Authentication failed. Card timeout.");
                 }
-                OnError();
+                OnError(3);
                 return;
             }
             
                 return;
             }
             
@@ -290,7 +287,7 @@ void MifareDES_Auth1(uint8_t mode, uint8_t algo, uint8_t keyno,  uint8_t *datain
                 for (int x = 0; x < 8; x++) {
                     if (decRndA[x] != encRndA[x]) {
                         DbpString("Authetication failed. Cannot varify PICC.");
                 for (int x = 0; x < 8; x++) {
                     if (decRndA[x] != encRndA[x]) {
                         DbpString("Authetication failed. Cannot varify PICC.");
-                        OnError();
+                        OnError(4);
                         return;
                     }
                 }
                         return;
                     }
                 }
@@ -343,7 +340,7 @@ void MifareDES_Auth1(uint8_t mode, uint8_t algo, uint8_t keyno,  uint8_t *datain
                 
             } else {
                 DbpString("Authetication failed.");
                 
             } else {
                 DbpString("Authetication failed.");
-                OnError();
+                OnError(6);
                 return;
             }
             
                 return;
             }
             
@@ -372,7 +369,7 @@ void MifareDES_Auth1(uint8_t mode, uint8_t algo, uint8_t keyno,  uint8_t *datain
                                if( MF_DBGLEVEL >= 4) {
                                        Dbprintf("AES context failed to init");
                                }
                                if( MF_DBGLEVEL >= 4) {
                                        Dbprintf("AES context failed to init");
                                }
-                               OnError();
+                               OnError(7);
                                return;
                        }
                        
                                return;
                        }
                        
@@ -380,10 +377,10 @@ void MifareDES_Auth1(uint8_t mode, uint8_t algo, uint8_t keyno,  uint8_t *datain
                        cmd[1] = 0x00;  //keynumber
                        len = DesfireAPDU(cmd, 2, resp);
                        if ( !len ) {
                        cmd[1] = 0x00;  //keynumber
                        len = DesfireAPDU(cmd, 2, resp);
                        if ( !len ) {
-                               if (MF_DBGLEVEL >= 1) {
+                               if (MF_DBGLEVEL >= MF_DBG_ERROR) {
                                        DbpString("Authentication failed. Card timeout.");
                                }
                                        DbpString("Authentication failed. Card timeout.");
                                }
-                               OnError();
+                               OnError(3);
                                return;
                        }
                        
                                return;
                        }
                        
@@ -401,10 +398,10 @@ void MifareDES_Auth1(uint8_t mode, uint8_t algo, uint8_t keyno,  uint8_t *datain
                        
                        len = DesfireAPDU(cmd, 33, resp);  // 1 + 32 == 33
                        if ( !len ) {
                        
                        len = DesfireAPDU(cmd, 33, resp);  // 1 + 32 == 33
                        if ( !len ) {
-                               if (MF_DBGLEVEL >= 1) {
+                               if (MF_DBGLEVEL >= MF_DBG_ERROR) {
                                        DbpString("Authentication failed. Card timeout.");
                                }
                                        DbpString("Authentication failed. Card timeout.");
                                }
-                OnError();
+                OnError(3);
                                return;
                        }
                        
                                return;
                        }
                        
@@ -416,10 +413,9 @@ void MifareDES_Auth1(uint8_t mode, uint8_t algo, uint8_t keyno,  uint8_t *datain
                                print_result("SESSION : ", skey->data, 16);
                        } else {
                                DbpString("Authetication failed.");
                                print_result("SESSION : ", skey->data, 16);
                        } else {
                                DbpString("Authetication failed.");
-                               OnError();
+                               OnError(7);
                                return;
                        }
                                return;
                        }
-                       
                        break;
                }       
        }
                        break;
                }       
        }
@@ -502,10 +498,10 @@ void OnSuccess(){
        LEDsoff();
 }
 
        LEDsoff();
 }
 
-void OnError(){
+void OnError(uint8_t reason){
        pcb_blocknum = 0;
        ReaderTransmit(deselect_cmd, 3 , NULL);
        FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
        pcb_blocknum = 0;
        ReaderTransmit(deselect_cmd, 3 , NULL);
        FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
-       cmd_send(CMD_ACK,0,0,0,0,0);
+       cmd_send(CMD_ACK,0,reason,0,0,0);
        LEDsoff();
 }
        LEDsoff();
 }
index d51da21ccb9a08f57d9ebb0d01c821df497611df..e91a87c1d0f9a0fbe756deaa2693faa45d22feb6 100644 (file)
@@ -84,7 +84,7 @@ int mifare_sendcmd_short(struct Crypto1State *pcs, uint8_t crypted, uint8_t cmd,
 int mifare_sendcmd_short_special(struct Crypto1State *pcs, uint8_t crypted, uint8_t cmd, uint8_t* data, uint8_t* answer, uint8_t *answer_parity, uint32_t *timing)
 {
        uint8_t dcmd[8];
 int mifare_sendcmd_short_special(struct Crypto1State *pcs, uint8_t crypted, uint8_t cmd, uint8_t* data, uint8_t* answer, uint8_t *answer_parity, uint32_t *timing)
 {
        uint8_t dcmd[8];
-       dcmd[0] = cmd;
+       dcmd[0] = cmd;\r
     dcmd[1] = data[0];\r
        dcmd[2] = data[1];
        dcmd[3] = data[2];\r
     dcmd[1] = data[0];\r
        dcmd[2] = data[1];
        dcmd[3] = data[2];\r
@@ -111,11 +111,11 @@ int mifare_sendcmd_short_mfucauth(struct Crypto1State *pcs, uint8_t crypted, uin
        ReaderTransmit(dcmd, sizeof(dcmd), timing);\r
        len = ReaderReceive(answer, answer_parity);\r
        if(!len) {\r
        ReaderTransmit(dcmd, sizeof(dcmd), timing);\r
        len = ReaderReceive(answer, answer_parity);\r
        if(!len) {\r
-        if (MF_DBGLEVEL >= 1)   Dbprintf("Authentication failed. Card timeout.");\r
+        if (MF_DBGLEVEL >= MF_DBG_ERROR)   Dbprintf("Authentication failed. Card timeout.");\r
         len = ReaderReceive(answer,answer_parity);\r
     }\r
     if(len==1) {\r
         len = ReaderReceive(answer,answer_parity);\r
     }\r
     if(len==1) {\r
-               if (MF_DBGLEVEL >= 1)   Dbprintf("NAK - Authentication failed.");\r
+               if (MF_DBGLEVEL >= MF_DBG_ERROR)   Dbprintf("NAK - Authentication failed.");\r
                return 1;\r
         }
        return len;
                return 1;\r
         }
        return len;
@@ -169,7 +169,7 @@ int mifare_sendcmd_shortex(struct Crypto1State *pcs, uint8_t crypted, uint8_t cm
        return len;\r
 }\r
 \r
        return len;\r
 }\r
 \r
-// mifare commands\r
+// mifare classic commands\r
 int mifare_classic_auth(struct Crypto1State *pcs, uint32_t uid, uint8_t blockNo, uint8_t keyType, uint64_t ui64Key, uint8_t isNested) \r
 {\r
        return mifare_classic_authex(pcs, uid, blockNo, keyType, ui64Key, isNested, NULL, NULL);\r
 int mifare_classic_auth(struct Crypto1State *pcs, uint32_t uid, uint8_t blockNo, uint8_t keyType, uint64_t ui64Key, uint8_t isNested) \r
 {\r
        return mifare_classic_authex(pcs, uid, blockNo, keyType, ui64Key, isNested, NULL, NULL);\r
@@ -298,81 +298,83 @@ int mifare_classic_readblock(struct Crypto1State *pcs, uint32_t uid, uint8_t blo
        return 0;
 }\r
 \r
        return 0;
 }\r
 \r
+// mifare ultralight commands\r
 int mifare_ultra_auth1(uint32_t uid, uint8_t *blockData){\r
 int mifare_ultra_auth1(uint32_t uid, uint8_t *blockData){\r
-       // variables\r
+\r
        uint16_t len;\r
        uint16_t len;\r
+       uint8_t *receivedAnswer = get_bigbufptr_recvrespbuf();\r
+       uint8_t *receivedAnswerPar = receivedAnswer + MAX_FRAME_SIZE;\r
        \r
        \r
-       uint8_t* receivedAnswer = get_bigbufptr_recvrespbuf();\r
-       uint8_t* receivedAnswerPar = receivedAnswer + MAX_FRAME_SIZE;\r
-       \r
-       // command MIFARE_CLASSIC_READBLOCK\r
        len = mifare_sendcmd_short(NULL, 1, 0x1A, 0x00, receivedAnswer,receivedAnswerPar ,NULL);\r
        if (len == 1) {\r
        len = mifare_sendcmd_short(NULL, 1, 0x1A, 0x00, receivedAnswer,receivedAnswerPar ,NULL);\r
        if (len == 1) {\r
-               if (MF_DBGLEVEL >= 1)   Dbprintf("Cmd Error: %02x", receivedAnswer[0]);\r
+               if (MF_DBGLEVEL >= MF_DBG_ERROR)\r
+                       Dbprintf("Cmd Error: %02x", receivedAnswer[0]);\r
                return 1;\r
        }\r
                return 1;\r
        }\r
-       if (len == 11) {\r
-               if (MF_DBGLEVEL >= 1)   Dbprintf("Auth1 Resp: %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",\r
-                 receivedAnswer[0],receivedAnswer[1],receivedAnswer[2],receivedAnswer[3],receivedAnswer[4],\r
-                 receivedAnswer[5],receivedAnswer[6],receivedAnswer[7],receivedAnswer[8],receivedAnswer[9],\r
-                 receivedAnswer[10]);\r
-               memcpy(blockData, receivedAnswer, 11);\r
-           return 0;\r
-       }\r
-       //else something went wrong???\r
-       return 1;\r
+       if (len != 11)\r
+               return 1;\r
+\r
+       if (MF_DBGLEVEL >= MF_DBG_EXTENDED) {\r
+               Dbprintf("Auth1 Resp: %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",\r
+                       receivedAnswer[0],receivedAnswer[1],receivedAnswer[2],receivedAnswer[3],receivedAnswer[4],\r
+                       receivedAnswer[5],receivedAnswer[6],receivedAnswer[7],receivedAnswer[8],receivedAnswer[9],\r
+                       receivedAnswer[10]);\r
+               }\r
+       memcpy(blockData, receivedAnswer, 11);\r
+       return 0;\r
 }\r
 \r
 int mifare_ultra_auth2(uint32_t uid, uint8_t *key, uint8_t *blockData){\r
 }\r
 \r
 int mifare_ultra_auth2(uint32_t uid, uint8_t *key, uint8_t *blockData){\r
-       // variables\r
+\r
        uint16_t len;\r
        uint16_t len;\r
+       uint8_t *receivedAnswer = get_bigbufptr_recvrespbuf();\r
+       uint8_t *receivedAnswerPar = receivedAnswer + MAX_FRAME_SIZE;\r
        \r
        \r
-       uint8_t* receivedAnswer = get_bigbufptr_recvrespbuf();\r
-       uint8_t* receivedAnswerPar = receivedAnswer + MAX_FRAME_SIZE;\r
-       \r
-       // command MIFARE_CLASSIC_READBLOCK\r
        len = mifare_sendcmd_short_mfucauth(NULL, 1, 0xAF, key, receivedAnswer, receivedAnswerPar, NULL);\r
        if (len == 1) {\r
        len = mifare_sendcmd_short_mfucauth(NULL, 1, 0xAF, key, receivedAnswer, receivedAnswerPar, NULL);\r
        if (len == 1) {\r
-               if (MF_DBGLEVEL >= 1)   Dbprintf("Cmd Error: %02x", receivedAnswer[0]);\r
+               if (MF_DBGLEVEL >= MF_DBG_ERROR)\r
+                       Dbprintf("Cmd Error: %02x", receivedAnswer[0]);\r
                return 1;\r
        }\r
                return 1;\r
        }\r
-       if (len == 11){\r
-               if (MF_DBGLEVEL >= 1)   Dbprintf("Auth2 Resp: %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",\r
-                 receivedAnswer[0],receivedAnswer[1],receivedAnswer[2],receivedAnswer[3],receivedAnswer[4],\r
-                 receivedAnswer[5],receivedAnswer[6],receivedAnswer[7],receivedAnswer[8],receivedAnswer[9],\r
-                 receivedAnswer[10]);\r
-               memcpy(blockData, receivedAnswer, 11);\r
-               return 0;\r
+       if (len != 11)\r
+               return 1;       \r
+       \r
+       if (MF_DBGLEVEL >= MF_DBG_EXTENDED) {\r
+               Dbprintf("Auth2 Resp: %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",\r
+                       receivedAnswer[0],receivedAnswer[1],receivedAnswer[2],receivedAnswer[3],receivedAnswer[4],\r
+                       receivedAnswer[5],receivedAnswer[6],receivedAnswer[7],receivedAnswer[8],receivedAnswer[9],\r
+                       receivedAnswer[10]);\r
        }\r
        }\r
-       //something went wrong?\r
-       return 1;\r
+       memcpy(blockData, receivedAnswer, 11);\r
+       return 0;\r
 }
 
 int mifare_ultra_readblock(uint32_t uid, uint8_t blockNo, uint8_t *blockData)
 {
 }
 
 int mifare_ultra_readblock(uint32_t uid, uint8_t blockNo, uint8_t *blockData)
 {
-       // variables
        uint16_t len;
        uint8_t bt[2];
        uint16_t len;
        uint8_t bt[2];
-       
-       uint8_t* receivedAnswer = get_bigbufptr_recvrespbuf();
-       uint8_t* receivedAnswerPar = receivedAnswer + MAX_FRAME_SIZE;\r
+       uint8_t *receivedAnswer = get_bigbufptr_recvrespbuf();
+       uint8_t *receivedAnswerPar = receivedAnswer + MAX_FRAME_SIZE;\r
        \r
        
        // command MIFARE_CLASSIC_READBLOCK
        len = mifare_sendcmd_short(NULL, 1, 0x30, blockNo, receivedAnswer, receivedAnswerPar, NULL);
        if (len == 1) {
        \r
        
        // command MIFARE_CLASSIC_READBLOCK
        len = mifare_sendcmd_short(NULL, 1, 0x30, blockNo, receivedAnswer, receivedAnswerPar, NULL);
        if (len == 1) {
-               if (MF_DBGLEVEL >= 1)   Dbprintf("Cmd Error: %02x", receivedAnswer[0]);
+               if (MF_DBGLEVEL >= MF_DBG_ERROR)\r
+                       Dbprintf("Cmd Error: %02x", receivedAnswer[0]);
                return 1;
        }
        if (len != 18) {
                return 1;
        }
        if (len != 18) {
-               if (MF_DBGLEVEL >= 1)   Dbprintf("Cmd Error: card timeout. len: %x", len);
+               if (MF_DBGLEVEL >= MF_DBG_ERROR)\r
+                       Dbprintf("Cmd Error: card timeout. len: %x", len);
                return 2;
        }
     
        memcpy(bt, receivedAnswer + 16, 2);
        AppendCrc14443a(receivedAnswer, 16);
        if (bt[0] != receivedAnswer[16] || bt[1] != receivedAnswer[17]) {
                return 2;
        }
     
        memcpy(bt, receivedAnswer + 16, 2);
        AppendCrc14443a(receivedAnswer, 16);
        if (bt[0] != receivedAnswer[16] || bt[1] != receivedAnswer[17]) {
-               if (MF_DBGLEVEL >= 1)   Dbprintf("Cmd CRC response error.");
+               if (MF_DBGLEVEL >= MF_DBG_ERROR)\r
+                       Dbprintf("Cmd CRC response error.");
                return 3;
        }
        
                return 3;
        }
        
@@ -430,56 +432,53 @@ int mifare_classic_writeblock(struct Crypto1State *pcs, uint32_t uid, uint8_t bl
 
 int mifare_ultra_writeblock(uint32_t uid, uint8_t blockNo, uint8_t *blockData) 
 {
 
 int mifare_ultra_writeblock(uint32_t uid, uint8_t blockNo, uint8_t *blockData) 
 {
-       // variables
        uint16_t len;     
        uint8_t par[3] = {0}; // enough for 18 parity bits
        uint16_t len;     
        uint8_t par[3] = {0}; // enough for 18 parity bits
-       uint8_t d_block[18];
-       uint8_treceivedAnswer = get_bigbufptr_recvrespbuf();\r
-       uint8_treceivedAnswerPar = receivedAnswer + MAX_FRAME_SIZE;
+       uint8_t d_block[18] = {0x00};
+       uint8_t *receivedAnswer = get_bigbufptr_recvrespbuf();\r
+       uint8_t *receivedAnswerPar = receivedAnswer + MAX_FRAME_SIZE;
        
        // command MIFARE_CLASSIC_WRITEBLOCK
        len = mifare_sendcmd_short(NULL, true, 0xA0, blockNo, receivedAnswer, receivedAnswerPar, NULL);
 
        if ((len != 1) || (receivedAnswer[0] != 0x0A)) {   //  0x0a - ACK
        
        // command MIFARE_CLASSIC_WRITEBLOCK
        len = mifare_sendcmd_short(NULL, true, 0xA0, blockNo, receivedAnswer, receivedAnswerPar, NULL);
 
        if ((len != 1) || (receivedAnswer[0] != 0x0A)) {   //  0x0a - ACK
-                       if (MF_DBGLEVEL >= 1)   Dbprintf("Cmd Addr Error: %02x", receivedAnswer[0]);  
-                       return 1;
+               if (MF_DBGLEVEL >= MF_DBG_ERROR)\r
+                       Dbprintf("Cmd Addr Error: %02x", receivedAnswer[0]);  
+               return 1;
        }
 
        }
 
-       memset(d_block,'\0',18);
        memcpy(d_block, blockData, 16);
     AppendCrc14443a(d_block, 16);
 
        ReaderTransmitPar(d_block, sizeof(d_block), par, NULL);
 \r
        memcpy(d_block, blockData, 16);
     AppendCrc14443a(d_block, 16);
 
        ReaderTransmitPar(d_block, sizeof(d_block), par, NULL);
 \r
-       // Receive the response
     len = ReaderReceive(receivedAnswer, receivedAnswerPar);    
 
        if ((len != 1) || (receivedAnswer[0] != 0x0A)) {   //  0x0a - ACK
     len = ReaderReceive(receivedAnswer, receivedAnswerPar);    
 
        if ((len != 1) || (receivedAnswer[0] != 0x0A)) {   //  0x0a - ACK
-               if (MF_DBGLEVEL >= 1)   Dbprintf("Cmd Data Error: %02x %d", receivedAnswer[0],len);
+               if (MF_DBGLEVEL >= MF_DBG_ERROR)\r
+                       Dbprintf("Cmd Data Error: %02x %d", receivedAnswer[0],len);
                return 2;
        }        
                return 2;
        }        
-
     return 0;
 } 
 
 int mifare_ultra_special_writeblock(uint32_t uid, uint8_t blockNo, uint8_t *blockData)
 {
     uint16_t len;
     return 0;
 } 
 
 int mifare_ultra_special_writeblock(uint32_t uid, uint8_t blockNo, uint8_t *blockData)
 {
     uint16_t len;
-       uint8_t d_block[8];
-       uint8_treceivedAnswer = get_bigbufptr_recvrespbuf();\r
+       uint8_t d_block[8] = {0x00};
+       uint8_t *receivedAnswer = get_bigbufptr_recvrespbuf();\r
        uint8_t *receivedAnswerPar = receivedAnswer + MAX_FRAME_SIZE;
 
     // command MIFARE_CLASSIC_WRITEBLOCK
        uint8_t *receivedAnswerPar = receivedAnswer + MAX_FRAME_SIZE;
 
     // command MIFARE_CLASSIC_WRITEBLOCK
-       memset(d_block,'\0',8);
        d_block[0]= blockNo;
        memcpy(d_block+1,blockData,4);
        AppendCrc14443a(d_block, 6);
 
        d_block[0]= blockNo;
        memcpy(d_block+1,blockData,4);
        AppendCrc14443a(d_block, 6);
 
-       //i know the data send here is correct
     len = mifare_sendcmd_short_special(NULL, 1, 0xA2, d_block, receivedAnswer, receivedAnswerPar, NULL);
 
        if (receivedAnswer[0] != 0x0A) {   //  0x0a - ACK
     len = mifare_sendcmd_short_special(NULL, 1, 0xA2, d_block, receivedAnswer, receivedAnswerPar, NULL);
 
        if (receivedAnswer[0] != 0x0A) {   //  0x0a - ACK
-               if (MF_DBGLEVEL >= 1)   Dbprintf("Cmd Send Error: %02x %d", receivedAnswer[0],len);
+               if (MF_DBGLEVEL >= MF_DBG_ERROR)\r
+                       Dbprintf("Cmd Send Error: %02x %d", receivedAnswer[0],len);
                return 1;
        }
        return 0;
                return 1;
        }
        return 0;
@@ -493,7 +492,8 @@ int mifare_classic_halt(struct Crypto1State *pcs, uint32_t uid)
 \r
        len = mifare_sendcmd_short(pcs, pcs == NULL ? false:true, 0x50, 0x00, receivedAnswer, receivedAnswerPar, NULL);\r
        if (len != 0) {\r
 \r
        len = mifare_sendcmd_short(pcs, pcs == NULL ? false:true, 0x50, 0x00, receivedAnswer, receivedAnswerPar, NULL);\r
        if (len != 0) {\r
-               if (MF_DBGLEVEL >= 1)   Dbprintf("halt error. response len: %x", len);  \r
+               if (MF_DBGLEVEL >= MF_DBG_ERROR)\r
+                       Dbprintf("halt error. response len: %x", len);  \r
                return 1;\r
        }\r
 \r
                return 1;\r
        }\r
 \r
@@ -503,15 +503,15 @@ int mifare_classic_halt(struct Crypto1State *pcs, uint32_t uid)
 int mifare_ultra_halt(uint32_t uid)
 {
        uint16_t len;   \r
 int mifare_ultra_halt(uint32_t uid)
 {
        uint16_t len;   \r
-       uint8_treceivedAnswer = get_bigbufptr_recvrespbuf();\r
+       uint8_t *receivedAnswer = get_bigbufptr_recvrespbuf();\r
        uint8_t *receivedAnswerPar = receivedAnswer + MAX_FRAME_SIZE;\r
     
        len = mifare_sendcmd_short(NULL, true, 0x50, 0x00, receivedAnswer, receivedAnswerPar, NULL);
        if (len != 0) {
        uint8_t *receivedAnswerPar = receivedAnswer + MAX_FRAME_SIZE;\r
     
        len = mifare_sendcmd_short(NULL, true, 0x50, 0x00, receivedAnswer, receivedAnswerPar, NULL);
        if (len != 0) {
-               if (MF_DBGLEVEL >= 1)   Dbprintf("halt error. response len: %x", len);
+               if (MF_DBGLEVEL >= MF_DBG_ERROR)\r
+                       Dbprintf("halt error. response len: %x", len);
                return 1;
                return 1;
-       }
-\r
+       }\r
        return 0;
 }
 
        return 0;
 }
 
@@ -624,9 +624,8 @@ void emlClearMem(void) {
        return;\r
 }\r
 \r
        return;\r
 }\r
 \r
-//\r
-//DESFIRE\r
-//\r
+\r
+// Mifare desfire commands\r
 int mifare_sendcmd_special(struct Crypto1State *pcs, uint8_t crypted, uint8_t cmd, uint8_t* data, uint8_t* answer, uint8_t *answer_parity, uint32_t *timing)\r
 {\r
     uint8_t dcmd[5] = {0x00};\r
 int mifare_sendcmd_special(struct Crypto1State *pcs, uint8_t crypted, uint8_t cmd, uint8_t* data, uint8_t* answer, uint8_t *answer_parity, uint32_t *timing)\r
 {\r
     uint8_t dcmd[5] = {0x00};\r
@@ -637,8 +636,9 @@ int mifare_sendcmd_special(struct Crypto1State *pcs, uint8_t crypted, uint8_t cm
        ReaderTransmit(dcmd, sizeof(dcmd), NULL);\r
        int len = ReaderReceive(answer, answer_parity);\r
        if(!len) {\r
        ReaderTransmit(dcmd, sizeof(dcmd), NULL);\r
        int len = ReaderReceive(answer, answer_parity);\r
        if(!len) {\r
-               if (MF_DBGLEVEL >= 1)   Dbprintf("Authentication failed. Card timeout.");\r
-                       return 1;\r
+               if (MF_DBGLEVEL >= MF_DBG_ERROR) \r
+                       Dbprintf("Authentication failed. Card timeout.");\r
+               return 1;\r
     }\r
        return len;\r
 }\r
     }\r
        return len;\r
 }\r
@@ -653,40 +653,43 @@ int mifare_sendcmd_special2(struct Crypto1State *pcs, uint8_t crypted, uint8_t c
        ReaderTransmit(dcmd, sizeof(dcmd), NULL);\r
        int len = ReaderReceive(answer, answer_parity);\r
        if(!len){\r
        ReaderTransmit(dcmd, sizeof(dcmd), NULL);\r
        int len = ReaderReceive(answer, answer_parity);\r
        if(!len){\r
-        if (MF_DBGLEVEL >= 1)   Dbprintf("Authentication failed. Card timeout.");\r
-                       return 1;\r
+        if (MF_DBGLEVEL >= MF_DBG_ERROR)\r
+                       Dbprintf("Authentication failed. Card timeout.");\r
+               return 1;\r
     }\r
        return len;\r
 }\r
 \r
 int mifare_desfire_des_auth1(uint32_t uid, uint8_t *blockData){\r
     }\r
        return len;\r
 }\r
 \r
 int mifare_desfire_des_auth1(uint32_t uid, uint8_t *blockData){\r
-       // variables\r
+\r
        int len;\r
        // load key, keynumber\r
        uint8_t data[2]={0x0a, 0x00};\r
        uint8_t* receivedAnswer = get_bigbufptr_recvrespbuf();\r
        uint8_t *receivedAnswerPar = receivedAnswer + MAX_FRAME_SIZE;\r
        \r
        int len;\r
        // load key, keynumber\r
        uint8_t data[2]={0x0a, 0x00};\r
        uint8_t* receivedAnswer = get_bigbufptr_recvrespbuf();\r
        uint8_t *receivedAnswerPar = receivedAnswer + MAX_FRAME_SIZE;\r
        \r
-       // command MIFARE_CLASSIC_READBLOCK\r
        len = mifare_sendcmd_special(NULL, 1, 0x02, data, receivedAnswer,receivedAnswerPar,NULL);\r
        if (len == 1) {\r
        len = mifare_sendcmd_special(NULL, 1, 0x02, data, receivedAnswer,receivedAnswerPar,NULL);\r
        if (len == 1) {\r
-               if (MF_DBGLEVEL >= 1)   Dbprintf("Cmd Error: %02x", receivedAnswer[0]);\r
+               if (MF_DBGLEVEL >= MF_DBG_ERROR)\r
+                       Dbprintf("Cmd Error: %02x", receivedAnswer[0]);\r
                return 1;\r
        }\r
        \r
        if (len == 12) {\r
                return 1;\r
        }\r
        \r
        if (len == 12) {\r
-               if (MF_DBGLEVEL >= 1)   Dbprintf("Auth1 Resp: %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",\r
-                 receivedAnswer[0],receivedAnswer[1],receivedAnswer[2],receivedAnswer[3],receivedAnswer[4],\r
-                 receivedAnswer[5],receivedAnswer[6],receivedAnswer[7],receivedAnswer[8],receivedAnswer[9],\r
-                 receivedAnswer[10],receivedAnswer[11]);\r
-               memcpy(blockData, receivedAnswer, 12);\r
+               if (MF_DBGLEVEL >= MF_DBG_EXTENDED)     {\r
+                       Dbprintf("Auth1 Resp: %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",\r
+                               receivedAnswer[0],receivedAnswer[1],receivedAnswer[2],receivedAnswer[3],receivedAnswer[4],\r
+                               receivedAnswer[5],receivedAnswer[6],receivedAnswer[7],receivedAnswer[8],receivedAnswer[9],\r
+                               receivedAnswer[10],receivedAnswer[11]);\r
+                       }\r
+                       memcpy(blockData, receivedAnswer, 12);\r
                return 0;\r
        }\r
        return 1;\r
 }\r
 \r
 int mifare_desfire_des_auth2(uint32_t uid, uint8_t *key, uint8_t *blockData){\r
                return 0;\r
        }\r
        return 1;\r
 }\r
 \r
 int mifare_desfire_des_auth2(uint32_t uid, uint8_t *key, uint8_t *blockData){\r
-       // variables\r
+\r
        int len;\r
        uint8_t data[17] = {0x00};\r
        data[0] = 0xAF;\r
        int len;\r
        uint8_t data[17] = {0x00};\r
        data[0] = 0xAF;\r
@@ -695,18 +698,21 @@ int mifare_desfire_des_auth2(uint32_t uid, uint8_t *key, uint8_t *blockData){
        uint8_t* receivedAnswer = get_bigbufptr_recvrespbuf();\r
        uint8_t *receivedAnswerPar = receivedAnswer + MAX_FRAME_SIZE;\r
        \r
        uint8_t* receivedAnswer = get_bigbufptr_recvrespbuf();\r
        uint8_t *receivedAnswerPar = receivedAnswer + MAX_FRAME_SIZE;\r
        \r
-       // command MIFARE_CLASSIC_READBLOCK\r
        len = mifare_sendcmd_special2(NULL, 1, 0x03, data, receivedAnswer, receivedAnswerPar ,NULL);\r
        \r
        if ((receivedAnswer[0] == 0x03) && (receivedAnswer[1] == 0xae)) {\r
        len = mifare_sendcmd_special2(NULL, 1, 0x03, data, receivedAnswer, receivedAnswerPar ,NULL);\r
        \r
        if ((receivedAnswer[0] == 0x03) && (receivedAnswer[1] == 0xae)) {\r
-               if (MF_DBGLEVEL >= 1)   Dbprintf("Auth Error: %02x %02x", receivedAnswer[0], receivedAnswer[1]);\r
+               if (MF_DBGLEVEL >= MF_DBG_ERROR)\r
+                       Dbprintf("Auth Error: %02x %02x", receivedAnswer[0], receivedAnswer[1]);\r
                return 1;\r
        }\r
                return 1;\r
        }\r
+       \r
        if (len == 12){\r
        if (len == 12){\r
-               if (MF_DBGLEVEL >= 1)   Dbprintf("Auth2 Resp: %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",\r
-                 receivedAnswer[0],receivedAnswer[1],receivedAnswer[2],receivedAnswer[3],receivedAnswer[4],\r
-                 receivedAnswer[5],receivedAnswer[6],receivedAnswer[7],receivedAnswer[8],receivedAnswer[9],\r
-                 receivedAnswer[10],receivedAnswer[11]);\r
+               if (MF_DBGLEVEL >= MF_DBG_EXTENDED) {\r
+                       Dbprintf("Auth2 Resp: %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",\r
+                               receivedAnswer[0],receivedAnswer[1],receivedAnswer[2],receivedAnswer[3],receivedAnswer[4],\r
+                               receivedAnswer[5],receivedAnswer[6],receivedAnswer[7],receivedAnswer[8],receivedAnswer[9],\r
+                               receivedAnswer[10],receivedAnswer[11]);\r
+                       }\r
                memcpy(blockData, receivedAnswer, 12);\r
                return 0;\r
        }\r
                memcpy(blockData, receivedAnswer, 12);\r
                return 0;\r
        }\r
index 0c4831c8d29e3ca589c66ccdfce190ba6784455b..7f4aa17811290496b314434b1e54b5bc082b6fb6 100644 (file)
@@ -103,13 +103,11 @@ void UsbPacketReceived(uint8_t *packet, int len) {
   switch(c->cmd) {
     case CMD_DEVICE_INFO: {
       dont_ack = 1;
   switch(c->cmd) {
     case CMD_DEVICE_INFO: {
       dont_ack = 1;
-//      c->cmd = CMD_DEVICE_INFO;
       arg0 = DEVICE_INFO_FLAG_BOOTROM_PRESENT | DEVICE_INFO_FLAG_CURRENT_MODE_BOOTROM |
       DEVICE_INFO_FLAG_UNDERSTANDS_START_FLASH;
       if(common_area.flags.osimage_present) {
         arg0 |= DEVICE_INFO_FLAG_OSIMAGE_PRESENT;
       }
       arg0 = DEVICE_INFO_FLAG_BOOTROM_PRESENT | DEVICE_INFO_FLAG_CURRENT_MODE_BOOTROM |
       DEVICE_INFO_FLAG_UNDERSTANDS_START_FLASH;
       if(common_area.flags.osimage_present) {
         arg0 |= DEVICE_INFO_FLAG_OSIMAGE_PRESENT;
       }
-//      UsbSendPacket(packet, len);
       cmd_send(CMD_DEVICE_INFO,arg0,1,2,0,0);
     } break;
       
       cmd_send(CMD_DEVICE_INFO,arg0,1,2,0,0);
     } break;
       
@@ -125,10 +123,8 @@ void UsbPacketReceived(uint8_t *packet, int len) {
       
     case CMD_FINISH_WRITE: {
       uint32_t* flash_mem = (uint32_t*)(&_flash_start);
       
     case CMD_FINISH_WRITE: {
       uint32_t* flash_mem = (uint32_t*)(&_flash_start);
-//      p = (volatile uint32_t *)&_flash_start;
       for (size_t j=0; j<2; j++) {
         for(i = 0+(64*j); i < 64+(64*j); i++) {
       for (size_t j=0; j<2; j++) {
         for(i = 0+(64*j); i < 64+(64*j); i++) {
-          //p[i+60] = c->d.asDwords[i];
           flash_mem[i] = c->d.asDwords[i];
         }
         
           flash_mem[i] = c->d.asDwords[i];
         }
         
@@ -138,8 +134,6 @@ void UsbPacketReceived(uint8_t *packet, int len) {
         if( ((flash_address+AT91C_IFLASH_PAGE_SIZE-1) >= end_addr) || (flash_address < start_addr) ) {
           /* Disallow write */
           dont_ack = 1;
         if( ((flash_address+AT91C_IFLASH_PAGE_SIZE-1) >= end_addr) || (flash_address < start_addr) ) {
           /* Disallow write */
           dont_ack = 1;
-          //        c->cmd = CMD_NACK;
-          //        UsbSendPacket(packet, len);
           cmd_send(CMD_NACK,0,0,0,0,0);
         } else {
           uint32_t page_n = (flash_address - ((uint32_t)flash_mem)) / AT91C_IFLASH_PAGE_SIZE;
           cmd_send(CMD_NACK,0,0,0,0,0);
         } else {
           uint32_t page_n = (flash_address - ((uint32_t)flash_mem)) / AT91C_IFLASH_PAGE_SIZE;
@@ -147,7 +141,6 @@ void UsbPacketReceived(uint8_t *packet, int len) {
           AT91C_BASE_EFC0->EFC_FCR = MC_FLASH_COMMAND_KEY |
           MC_FLASH_COMMAND_PAGEN(page_n) |
           AT91C_MC_FCMD_START_PROG;
           AT91C_BASE_EFC0->EFC_FCR = MC_FLASH_COMMAND_KEY |
           MC_FLASH_COMMAND_PAGEN(page_n) |
           AT91C_MC_FCMD_START_PROG;
-          //        arg0 = (address - ((uint32_t)flash_s));
         }
         
         // Wait until flashing of page finishes
         }
         
         // Wait until flashing of page finishes
@@ -155,15 +148,12 @@ void UsbPacketReceived(uint8_t *packet, int len) {
         while(!((sr = AT91C_BASE_EFC0->EFC_FSR) & AT91C_MC_FRDY));
         if(sr & (AT91C_MC_LOCKE | AT91C_MC_PROGE)) {
           dont_ack = 1;
         while(!((sr = AT91C_BASE_EFC0->EFC_FSR) & AT91C_MC_FRDY));
         if(sr & (AT91C_MC_LOCKE | AT91C_MC_PROGE)) {
           dont_ack = 1;
-          //        c->cmd = CMD_NACK;
           cmd_send(CMD_NACK,0,0,0,0,0);
           cmd_send(CMD_NACK,0,0,0,0,0);
-          //        UsbSendPacket(packet, len);
         }
       }
     } break;
       
     case CMD_HARDWARE_RESET: {
         }
       }
     } break;
       
     case CMD_HARDWARE_RESET: {
-//      USB_D_PLUS_PULLUP_OFF();
       usb_disable();
       AT91C_BASE_RSTC->RSTC_RCR = RST_CONTROL_KEY | AT91C_RSTC_PROCRST;
     } break;
       usb_disable();
       AT91C_BASE_RSTC->RSTC_RCR = RST_CONTROL_KEY | AT91C_RSTC_PROCRST;
     } break;
@@ -189,8 +179,6 @@ void UsbPacketReceived(uint8_t *packet, int len) {
         } else {
           start_addr = end_addr = 0;
           dont_ack = 1;
         } else {
           start_addr = end_addr = 0;
           dont_ack = 1;
-//          c->cmd = CMD_NACK;
-//          UsbSendPacket(packet, len);
           cmd_send(CMD_NACK,0,0,0,0,0);
         }
       }
           cmd_send(CMD_NACK,0,0,0,0,0);
         }
       }
@@ -202,8 +190,6 @@ void UsbPacketReceived(uint8_t *packet, int len) {
   }
   
   if(!dont_ack) {
   }
   
   if(!dont_ack) {
-//    c->cmd = CMD_ACK;
-//    UsbSendPacket(packet, len);
     cmd_send(CMD_ACK,arg0,0,0,0,0);
   }
 }
     cmd_send(CMD_ACK,arg0,0,0,0,0);
   }
 }
@@ -219,24 +205,19 @@ static void flash_mode(int externally_entered)
   usb_enable();
   for (volatile size_t i=0; i<0x100000; i++);
 
   usb_enable();
   for (volatile size_t i=0; i<0x100000; i++);
 
-//     UsbStart();
        for(;;) {
                WDT_HIT();
 
     if (usb_poll()) {
       rx_len = usb_read(rx,sizeof(UsbCommand));
       if (rx_len) {
        for(;;) {
                WDT_HIT();
 
     if (usb_poll()) {
       rx_len = usb_read(rx,sizeof(UsbCommand));
       if (rx_len) {
-//        DbpString("starting to flash");
         UsbPacketReceived(rx,rx_len);
       }
     }
 
         UsbPacketReceived(rx,rx_len);
       }
     }
 
-//             UsbPoll(TRUE);
-
                if(!externally_entered && !BUTTON_PRESS()) {
                        /* Perform a reset to leave flash mode */
                if(!externally_entered && !BUTTON_PRESS()) {
                        /* Perform a reset to leave flash mode */
-//                     USB_D_PLUS_PULLUP_OFF();
-      usb_disable();
+               usb_disable();
                        LED_B_ON();
                        AT91C_BASE_RSTC->RSTC_RCR = RST_CONTROL_KEY | AT91C_RSTC_PROCRST;
                        for(;;);
                        LED_B_ON();
                        AT91C_BASE_RSTC->RSTC_RCR = RST_CONTROL_KEY | AT91C_RSTC_PROCRST;
                        for(;;);
index ecbbc16cbf627b549eb565cd572ee13ad977aca1..24b976700a0be59bc9ae7e1e964cb8e2ea3815de 100644 (file)
@@ -15,7 +15,7 @@ OBJDIR = obj
 
 LDLIBS =  -L/mingw/lib -L/opt/local/lib -L/usr/local/lib ../liblua/liblua.a -lm -lreadline -lpthread -lcrypto -lgdi32
 LDFLAGS = $(COMMON_FLAGS)
 
 LDLIBS =  -L/mingw/lib -L/opt/local/lib -L/usr/local/lib ../liblua/liblua.a -lm -lreadline -lpthread -lcrypto -lgdi32
 LDFLAGS = $(COMMON_FLAGS)
-CFLAGS = -std=c99 -I. -I../include -I../common -I/mingw/include -I/opt/local/include -I../liblua -Wall $(COMMON_FLAGS) -g -O4
+CFLAGS = -std=c99 -lcrypto -I. -I../include -I../common -I/mingw/include -I/opt/local/include -I../liblua -Wall $(COMMON_FLAGS) -g -O4
 LUAPLATFORM = generic
 
 ifneq (,$(findstring MINGW,$(platform)))
 LUAPLATFORM = generic
 
 ifneq (,$(findstring MINGW,$(platform)))
index 0ed3f0136a206fdc4ac65cbb7cdb475f361bf5ae..a2d7511c587e68df7a6b558dd8e3c9f95c5fbdbf 100644 (file)
@@ -50,9 +50,11 @@ int CmdHFTune(const char *Cmd)
 #define iso14443_CMD_WUPA       0x52
 #define iso14443_CMD_SELECT     0x93
 #define iso14443_CMD_SELECT_2   0x95
 #define iso14443_CMD_WUPA       0x52
 #define iso14443_CMD_SELECT     0x93
 #define iso14443_CMD_SELECT_2   0x95
+#define iso14443_CMD_SELECT_3   0x97
 #define iso14443_CMD_REQ        0x26
 #define iso14443_CMD_READBLOCK  0x30
 #define iso14443_CMD_WRITEBLOCK 0xA0
 #define iso14443_CMD_REQ        0x26
 #define iso14443_CMD_READBLOCK  0x30
 #define iso14443_CMD_WRITEBLOCK 0xA0
+#define iso14443_CMD_WRITE             0xA2
 #define iso14443_CMD_INC        0xC0
 #define iso14443_CMD_DEC        0xC1
 #define iso14443_CMD_RESTORE    0xC2
 #define iso14443_CMD_INC        0xC0
 #define iso14443_CMD_DEC        0xC1
 #define iso14443_CMD_RESTORE    0xC2
@@ -60,6 +62,15 @@ int CmdHFTune(const char *Cmd)
 #define iso14443_CMD_HALT       0x50
 #define iso14443_CMD_RATS       0xE0
 
 #define iso14443_CMD_HALT       0x50
 #define iso14443_CMD_RATS       0xE0
 
+#define iso14443_CMD_AUTH_KEYA 0x60
+#define iso14443_CMD_AUTH_KEYB 0x61
+
+#define iso14443_CMD_AUTH_STEP1        0x1A
+#define iso14443_CMD_AUTH_STEP2        0xAA
+#define iso14443_CMD_AUTH_RESPONSE     0xAF
+
+#define CHINESE_BACKDOOR_INIT   0x40 
+#define CHINESE_BACKDOOR_STEP2   0x43 
 
 void annotateIso14443a(char *exp, size_t size, uint8_t* cmd, uint8_t cmdsize)
 {
 
 void annotateIso14443a(char *exp, size_t size, uint8_t* cmd, uint8_t cmdsize)
 {
@@ -79,12 +90,22 @@ void annotateIso14443a(char *exp, size_t size, uint8_t* cmd, uint8_t cmdsize)
        case iso14443_CMD_REQ:         snprintf(exp,size,"REW"); break;
        case iso14443_CMD_READBLOCK:   snprintf(exp,size,"READBLOCK(%d)",cmd[1]); break;
        case iso14443_CMD_WRITEBLOCK:  snprintf(exp,size,"WRITEBLOCK(%d)",cmd[1]); break;
        case iso14443_CMD_REQ:         snprintf(exp,size,"REW"); break;
        case iso14443_CMD_READBLOCK:   snprintf(exp,size,"READBLOCK(%d)",cmd[1]); break;
        case iso14443_CMD_WRITEBLOCK:  snprintf(exp,size,"WRITEBLOCK(%d)",cmd[1]); break;
+       case iso14443_CMD_WRITE:           snprintf(exp,size,"WRITE"); break;
        case iso14443_CMD_INC:         snprintf(exp,size,"INC(%d)",cmd[1]); break;
        case iso14443_CMD_DEC:         snprintf(exp,size,"DEC(%d)",cmd[1]); break;
        case iso14443_CMD_RESTORE:     snprintf(exp,size,"RESTORE(%d)",cmd[1]); break;
        case iso14443_CMD_TRANSFER:    snprintf(exp,size,"TRANSFER(%d)",cmd[1]); break;
        case iso14443_CMD_HALT:        snprintf(exp,size,"HALT"); break;
        case iso14443_CMD_RATS:        snprintf(exp,size,"RATS"); break;
        case iso14443_CMD_INC:         snprintf(exp,size,"INC(%d)",cmd[1]); break;
        case iso14443_CMD_DEC:         snprintf(exp,size,"DEC(%d)",cmd[1]); break;
        case iso14443_CMD_RESTORE:     snprintf(exp,size,"RESTORE(%d)",cmd[1]); break;
        case iso14443_CMD_TRANSFER:    snprintf(exp,size,"TRANSFER(%d)",cmd[1]); break;
        case iso14443_CMD_HALT:        snprintf(exp,size,"HALT"); break;
        case iso14443_CMD_RATS:        snprintf(exp,size,"RATS"); break;
+       
+       case iso14443_CMD_AUTH_KEYA:   snprintf(exp,size,"AUTH KEY A"); break;
+       case iso14443_CMD_AUTH_KEYB:   snprintf(exp,size,"AUTH KEY B"); break;
+       case iso14443_CMD_AUTH_STEP1:  snprintf(exp,size,"AUTH REQ NONCE"); break;
+       case iso14443_CMD_AUTH_STEP2:  snprintf(exp,size,"AUTH STEP 2"); break;
+       case iso14443_CMD_AUTH_RESPONSE:  snprintf(exp,size,"AUTH RESPONSE"); break;
+       
+       case CHINESE_BACKDOOR_INIT:    snprintf(exp,size,"BACKDOOR INIT");break;
+       case CHINESE_BACKDOOR_STEP2:    snprintf(exp,size,"BACKDOOR STEP2");break;
        default:                       snprintf(exp,size,"?"); break;
        }
        return;
        default:                       snprintf(exp,size,"?"); break;
        }
        return;
@@ -92,7 +113,6 @@ void annotateIso14443a(char *exp, size_t size, uint8_t* cmd, uint8_t cmdsize)
 
 void annotateIclass(char *exp, size_t size, uint8_t* cmd, uint8_t cmdsize)
 {
 
 void annotateIclass(char *exp, size_t size, uint8_t* cmd, uint8_t cmdsize)
 {
-
        if(cmdsize > 1 && cmd[0] == ICLASS_CMD_READ)
        {
                  snprintf(exp,size,"READ(%d)",cmd[1]);
        if(cmdsize > 1 && cmd[0] == ICLASS_CMD_READ)
        {
                  snprintf(exp,size,"READ(%d)",cmd[1]);
@@ -115,7 +135,6 @@ void annotateIclass(char *exp, size_t size, uint8_t* cmd, uint8_t cmdsize)
 }
 
 
 }
 
 
-
 uint16_t printTraceLine(uint16_t tracepos, uint8_t* trace, bool iclass, bool showWaitCycles)
 {
        bool isResponse;
 uint16_t printTraceLine(uint16_t tracepos, uint8_t* trace, bool iclass, bool showWaitCycles)
 {
        bool isResponse;
@@ -181,8 +200,7 @@ uint16_t printTraceLine(uint16_t tracepos, uint8_t* trace, bool iclass, bool sho
                                // Rough guess that this is a command from the reader
                                // For iClass the command byte is not part of the CRC
                                ComputeCrc14443(CRC_ICLASS, &frame[1], data_len-3, &b1, &b2);
                                // Rough guess that this is a command from the reader
                                // For iClass the command byte is not part of the CRC
                                ComputeCrc14443(CRC_ICLASS, &frame[1], data_len-3, &b1, &b2);
-                       }
-                       else {
+                       } else {
                                // For other data.. CRC might not be applicable (UPDATE commands etc.)
                                ComputeCrc14443(CRC_ICLASS, frame, data_len-2, &b1, &b2);
                        }
                                // For other data.. CRC might not be applicable (UPDATE commands etc.)
                                ComputeCrc14443(CRC_ICLASS, frame, data_len-2, &b1, &b2);
                        }
@@ -191,7 +209,7 @@ uint16_t printTraceLine(uint16_t tracepos, uint8_t* trace, bool iclass, bool sho
                                crcError = true;
                        }
 
                                crcError = true;
                        }
 
-               }else{//Iso 14443a
+               } else {//Iso 14443a
 
                        ComputeCrc14443(CRC_14443_A, frame, data_len-2, &b1, &b2);
 
 
                        ComputeCrc14443(CRC_14443_A, frame, data_len-2, &b1, &b2);
 
@@ -202,7 +220,6 @@ uint16_t printTraceLine(uint16_t tracepos, uint8_t* trace, bool iclass, bool sho
                                }
                        }
                }
                                }
                        }
                }
-
        }
        char *crc = crcError ? "!crc" :"    ";
 
        }
        char *crc = crcError ? "!crc" :"    ";
 
@@ -210,8 +227,10 @@ uint16_t printTraceLine(uint16_t tracepos, uint8_t* trace, bool iclass, bool sho
 
        if(!isResponse)
        {
 
        if(!isResponse)
        {
-               if(iclass)      annotateIclass(explanation,sizeof(explanation),frame,data_len);
-               else annotateIso14443a(explanation,sizeof(explanation),frame,data_len);
+               if(iclass)
+                       annotateIclass(explanation,sizeof(explanation),frame,data_len);
+               else 
+                       annotateIso14443a(explanation,sizeof(explanation),frame,data_len);
        }
 
        int num_lines = (data_len - 1)/16 + 1;
        }
 
        int num_lines = (data_len - 1)/16 + 1;
@@ -227,7 +246,7 @@ uint16_t printTraceLine(uint16_t tracepos, uint8_t* trace, bool iclass, bool sho
                } else {
                        PrintAndLog("           |           |     | %-64s| %s| %s",
                                line[j],
                } else {
                        PrintAndLog("           |           |     | %-64s| %s| %s",
                                line[j],
-                               (j == num_lines-1)?crc:"    ",
+                               (j == num_lines-1) ? crc : "    ",
                                (j == num_lines-1) ? explanation : "");
                }
        }
                                (j == num_lines-1) ? explanation : "");
                }
        }
index 47b0a4ab5cc6acded016be3944e6bea3f3309783..0229c9477489137e9c722f786bac6c44ffc53f51 100644 (file)
@@ -1956,7 +1956,7 @@ int GetCardSize()
        if ( ((atqa & 0xffff) == 0x0008) && (sak == 0x38) ) return 4;\r
 \r
        \r
        if ( ((atqa & 0xffff) == 0x0008) && (sak == 0x38) ) return 4;\r
 \r
        \r
-       PrintAndLog("BEFOOO  1K %02X",  (atqa & 0xff0f));\r
+       //PrintAndLog("BEFOOO  1K %02X",  (atqa & 0xff0f));\r
        \r
        // MIFARE Plus (4 Byte UID or 4 Byte RID)\r
        // MIFARE Plus (7 Byte UID)\r
        \r
        // MIFARE Plus (4 Byte UID or 4 Byte RID)\r
        // MIFARE Plus (7 Byte UID)\r
index a0af68875dd0eff177c6aad1f9ad7dc3515d8dfd..e236be3303308fc359d0fc42ba9a03c7d21d9f12 100644 (file)
@@ -88,12 +88,12 @@ int CmdHF14AMfUWrBl(const char *Cmd){
     UsbCommand resp;
         
     if (strlen(Cmd)<3) {
     UsbCommand resp;
         
     if (strlen(Cmd)<3) {
-        PrintAndLog("Usage:  hf mfu uwrbl <block number> <block data > [w]");
+        PrintAndLog("Usage:  hf mfu wrbl <block number> <block data > [w]");
                PrintAndLog("       [block number] ");
                PrintAndLog("       [block data] - (8 hex symbols)");
                PrintAndLog("       [w] - Chinese magic ultralight-c tag ");
                PrintAndLog("");
                PrintAndLog("       [block number] ");
                PrintAndLog("       [block data] - (8 hex symbols)");
                PrintAndLog("       [w] - Chinese magic ultralight-c tag ");
                PrintAndLog("");
-        PrintAndLog("        sample: hf mfu uwrbl 0 01020304");
+        PrintAndLog("        sample: hf mfu wrbl 0 01020304");
         return 0;
     }       
     blockNo = param_get8(Cmd, 0);
         return 0;
     }       
     blockNo = param_get8(Cmd, 0);
@@ -198,8 +198,8 @@ int CmdHF14AMfURdBl(const char *Cmd){
     uint8_t blockNo = 0;       
         
     if (strlen(Cmd)<1) {
     uint8_t blockNo = 0;       
         
     if (strlen(Cmd)<1) {
-        PrintAndLog("Usage:  hf mfu urdbl <block number>");
-        PrintAndLog("        sample: hfu mfu urdbl 0");
+        PrintAndLog("Usage:  hf mfu rdbl <block number>");
+        PrintAndLog("        sample: hfu mfu rdbl 0");
         return 0;
     }       
         
         return 0;
     }       
         
@@ -436,60 +436,70 @@ int CmdHF14AMfucAuth(const char *Cmd){
     DES_key_schedule ks1,ks2;
     DES_cblock key1,key2;
 
     DES_key_schedule ks1,ks2;
     DES_cblock key1,key2;
 
+       char cmdp = param_getchar(Cmd, 0);
        // 
        memset(iv, 0, 8);
        
        // 
        memset(iv, 0, 8);
        
-    if (strlen(Cmd)<1) {
+    if (cmdp == 'h' || cmdp == 'H') {
         PrintAndLog("Usage:  hf mfu auth k <key number>");
         PrintAndLog("Usage:  hf mfu auth k <key number>");
-        PrintAndLog("        sample: hf mfu auth k 0");
+               PrintAndLog("      1 = all zeros key");
+               PrintAndLog("      2 = 0x00-0x0F key");
+               PrintAndLog("      3 = nfc key");
+               PrintAndLog("      4 = all ones key");
+               PrintAndLog("      defaults to 3DES standard key");
+        PrintAndLog("        sample : hf mfu auth k");
+               PrintAndLog("               : hf mfu auth k 3");
         return 0;
     } 
     
     //Change key to user defined one
         return 0;
     } 
     
     //Change key to user defined one
-    if (strchr(Cmd,'k') != 0){
-        //choose a key
-        keyNo = param_get8(Cmd, 1);
-        switch(keyNo){
+    if (cmdp == 'k' || cmdp == 'K'){
+
+               keyNo = param_get8(Cmd, 1);
+        
+               switch(keyNo){
             case 0:
                 memcpy(key,key1_blnk_data,16);
                 break;
             case 1:
                 memcpy(key,key2_defa_data,16);
                 break;
             case 0:
                 memcpy(key,key1_blnk_data,16);
                 break;
             case 1:
                 memcpy(key,key2_defa_data,16);
                 break;
-           case 2:
+                       case 2: 
                 memcpy(key,key4_nfc_data,16);
                 break;
                 memcpy(key,key4_nfc_data,16);
                 break;
-               case 3: 
+                       case 3: 
                                memcpy(key,key5_ones_data,16);
                 break;
             default:
                 memcpy(key,key3_3des_data,16);
                 break;
         }
                                memcpy(key,key5_ones_data,16);
                 break;
             default:
                 memcpy(key,key3_3des_data,16);
                 break;
         }
-    }else{
+    } else {
         memcpy(key,key3_3des_data,16);  
     }
         memcpy(key,key3_3des_data,16);  
     }
+       
     memcpy(key1,key,8);
     memcpy(key2,key+8,8);
     DES_set_key((DES_cblock *)key1,&ks1);
     DES_set_key((DES_cblock *)key2,&ks2);
         
     memcpy(key1,key,8);
     memcpy(key2,key+8,8);
     DES_set_key((DES_cblock *)key1,&ks1);
     DES_set_key((DES_cblock *)key2,&ks2);
         
-    //Auth1
-    UsbCommand c = {CMD_MIFAREUC_AUTH1, {blockNo}};
-    SendCommand(&c);
-    UsbCommand resp;
-    if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {
-        uint8_t isOK  = resp.arg[0] & 0xff;
-               cuid  = resp.arg[1];
-        uint8_t * data= resp.d.asBytes;
-
-         if (isOK){
-             PrintAndLog("enc(RndB):%s", sprint_hex(data+1, 8));
-             memcpy(e_RndB,data+1,8);
+       //Auth1
+       UsbCommand c = {CMD_MIFAREUC_AUTH1, {blockNo}};
+       SendCommand(&c);
+       UsbCommand resp;
+       if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {
+               uint8_t isOK  = resp.arg[0] & 0xff;
+               cuid  = resp.arg[1];
+               uint8_t * data= resp.d.asBytes;
+
+               if (isOK){
+                       PrintAndLog("enc(RndB):%s", sprint_hex(data+1, 8));
+                       memcpy(e_RndB,data+1,8);
+               }
+       } else {
+               PrintAndLog("Command execute timeout");
+               return 0;
        }
        }
-    } else {
-        PrintAndLog("Command execute timeout");
-    }
        
     //Do crypto magic
     DES_random_key(&RndA);
        
     //Do crypto magic
     DES_random_key(&RndA);
@@ -508,18 +518,18 @@ int CmdHF14AMfucAuth(const char *Cmd){
     memcpy(d.d.asBytes,RndARndB, 16);
     SendCommand(&d);
 
     memcpy(d.d.asBytes,RndARndB, 16);
     SendCommand(&d);
 
-    UsbCommand respb;
-    if (WaitForResponseTimeout(CMD_ACK,&respb,1500)) {
-        uint8_t  isOK  = respb.arg[0] & 0xff;
-        uint8_t * data2= respb.d.asBytes;
+       UsbCommand respb;
+       if (WaitForResponseTimeout(CMD_ACK,&respb,1500)) {
+               uint8_t  isOK  = respb.arg[0] & 0xff;
+               uint8_t * data2= respb.d.asBytes;
 
 
-        if (isOK){
-            PrintAndLog("enc(RndA'):%s", sprint_hex(data2+1, 8));
-       }
-                 
-    } else {
-        PrintAndLog("Command execute timeout");
-    } 
+               if (isOK){
+                       PrintAndLog("enc(RndA'):%s", sprint_hex(data2+1, 8));
+               }
+       } else {
+               PrintAndLog("Command execute timeout");
+               return 0;
+       
     return 1;
 }
 
     return 1;
 }
 
@@ -531,8 +541,8 @@ int CmdHF14AMfUCRdBl(const char *Cmd)
     uint8_t blockNo = 0;
         
     if (strlen(Cmd)<1) {
     uint8_t blockNo = 0;
         
     if (strlen(Cmd)<1) {
-        PrintAndLog("Usage:  hf mfu ucrdbl  <block number>");
-        PrintAndLog("        sample: hf mfu ucrdbl 0");
+        PrintAndLog("Usage:  hf mfu crdbl  <block number>");
+        PrintAndLog("        sample: hf mfu crdbl 0");
         return 0;
     }       
         
         return 0;
     }       
         
@@ -571,8 +581,8 @@ int CmdHF14AMfUCWrBl(const char *Cmd){
     UsbCommand resp;
         
     if (strlen(Cmd)<3) {
     UsbCommand resp;
         
     if (strlen(Cmd)<3) {
-        PrintAndLog("Usage:  hf mfu ucwrbl <block number> <block data (8 hex symbols)> [w]");
-        PrintAndLog("        sample: hf mfu uwrbl 0 01020304");
+        PrintAndLog("Usage:  hf mfu cwrbl <block number> <block data (8 hex symbols)> [w]");
+        PrintAndLog("        sample: hf mfu wrbl 0 01020304");
         return 0;
     }       
     blockNo = param_get8(Cmd, 0);
         return 0;
     }       
     blockNo = param_get8(Cmd, 0);
index 1390d8171cebb853149b349bdef16182312ffeb1..e982ecf19141f474e3f90a858b80848c55d22f74 100644 (file)
@@ -52,11 +52,8 @@ void ReceiveCommand(UsbCommand* rxcmd) {
   while (true) {
     rxlen = sizeof(UsbCommand) - (prx-prxcmd);
     if (uart_receive(sp,prx,&rxlen)) {
   while (true) {
     rxlen = sizeof(UsbCommand) - (prx-prxcmd);
     if (uart_receive(sp,prx,&rxlen)) {
-//      printf("received [%zd] bytes\n",rxlen);
       prx += rxlen;
       if ((prx-prxcmd) >= sizeof(UsbCommand)) {
       prx += rxlen;
       if ((prx-prxcmd) >= sizeof(UsbCommand)) {
-//        printf("received: ");
-//        cmd_debug(rxcmd);
         return;
       }
     }
         return;
       }
     }
Impressum, Datenschutz