]> git.zerfleddert.de Git - proxmark3-svn/blobdiff - armsrc/iso14443a.c
BUG: don't try to fix things that ain't broken.. or not. My try for a fix ended...
[proxmark3-svn] / armsrc / iso14443a.c
index bb73d5b7b4515b4754075f9b32d9f31a05bc58da..fcd51d63e0946b7e8e35ee53a531f69a0d834bf7 100644 (file)
 // Routines to support ISO 14443 type A.
 //-----------------------------------------------------------------------------
 
-#include "proxmark3.h"
+#include "../include/proxmark3.h"
 #include "apps.h"
 #include "util.h"
 #include "string.h"
-#include "cmd.h"
-
-#include "iso14443crc.h"
+#include "../common/cmd.h"
+#include "../common/iso14443crc.h"
 #include "iso14443a.h"
 #include "crapto1.h"
 #include "mifareutil.h"
@@ -125,6 +124,8 @@ uint32_t LastProxToAirDuration;
 #define        SEC_Y 0x00
 #define        SEC_Z 0xc0
 
+//replaced large parity table with small parity generation function - saves flash code
+/*
 const uint8_t OddByteParity[256] = {
   1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
   0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
@@ -143,7 +144,7 @@ const uint8_t OddByteParity[256] = {
   0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
   1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1
 };
-
+*/
 
 void iso14a_set_trigger(bool enable) {
        trigger = enable;
@@ -166,10 +167,12 @@ void iso14a_set_timeout(uint32_t timeout) {
 // Generate the parity value for a byte sequence
 //
 //-----------------------------------------------------------------------------
+/*
 byte_t oddparity (const byte_t bt)
 {
        return OddByteParity[bt];
 }
+*/
 
 uint32_t GetParity(const uint8_t * pbtCmd, int iLen)
 {
@@ -179,7 +182,8 @@ uint32_t GetParity(const uint8_t * pbtCmd, int iLen)
        // Generate the parity bits
        for (i = 0; i < iLen; i++) {
                // and save them to a 32Bit word
-               dwPar |= ((OddByteParity[pbtCmd[i]]) << i);
+               //dwPar |= ((OddByteParity[pbtCmd[i]]) << i);
+               dwPar |= (oddparity(pbtCmd[i]) << i);
        }
        return dwPar;
 }
@@ -684,7 +688,8 @@ static void CodeIso14443aAsTagPar(const uint8_t *cmd, int len, uint32_t dwParity
                }
 
                // Get the parity bit
-               if ((dwParity >> i) & 0x01) {
+               //if ((dwParity >> i) & 0x01) {
+               if (oddparity(cmd[i]) & 0x01) {
                        ToSend[++ToSendMax] = SEC_D;
                        LastProxToAirDuration = 8 * ToSendMax - 4;
                } else {
@@ -892,6 +897,12 @@ void SimulateIso14443aTag(int tagType, int uid_1st, int uid_2nd, byte_t* data)
                        response1[1] = 0x00;
                        sak = 0x28;
                } break;
+               case 5: { // MIFARE TNP3XXX
+                       // Says: I am a toy
+                       response1[0] = 0x01;
+                       response1[1] = 0x0f;
+                       sak = 0x01;
+               } break;                
                default: {
                        Dbprintf("Error: unkown tagtype (%d)",tagType);
                        return;
@@ -1206,13 +1217,6 @@ static void TransmitFor14443a(const uint8_t *cmd, int len, uint32_t *timing)
        // clear TXRDY
        AT91C_BASE_SSC->SSC_THR = SEC_Y;
 
-       // for(uint16_t c = 0; c < 10;) {       // standard delay for each transfer (allow tag to be ready after last transmission)
-               // if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_TXRDY)) {
-                       // AT91C_BASE_SSC->SSC_THR = SEC_Y;     
-                       // c++;
-               // }
-       // }
-
        uint16_t c = 0;
        for(;;) {
                if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_TXRDY)) {
@@ -1224,8 +1228,7 @@ static void TransmitFor14443a(const uint8_t *cmd, int len, uint32_t *timing)
                }
        }
        
-       NextTransferTime = MAX(NextTransferTime, LastTimeProxToAirStart + REQUEST_GUARD_TIME);
-       
+       NextTransferTime = MAX(NextTransferTime, LastTimeProxToAirStart + REQUEST_GUARD_TIME);  
 }
 
 
@@ -1704,7 +1707,7 @@ int iso14443a_select_card(byte_t* uid_ptr, iso14a_card_select_t* p_hi14a_card, u
                memcpy(uid_resp,resp,4);
        }
        uid_resp_len = 4;
-       // Dbprintf("uid: %02x %02x %02x %02x",uid_resp[0],uid_resp[1],uid_resp[2],uid_resp[3]);
+
 
     // calculate crypto UID. Always use last 4 Bytes.
     if(cuid_ptr) {
@@ -1722,11 +1725,19 @@ int iso14443a_select_card(byte_t* uid_ptr, iso14a_card_select_t* p_hi14a_card, u
     if (!ReaderReceive(resp)) return 0;
     sak = resp[0];
 
+       //Dbprintf("SAK: %02x",resp[0]);
+       
     // Test if more parts of the uid are comming
     if ((sak & 0x04) /* && uid_resp[0] == 0x88 */) {
       // Remove first byte, 0x88 is not an UID byte, it CT, see page 3 of:
       // http://www.nxp.com/documents/application_note/AN10927.pdf
-      memcpy(uid_resp, uid_resp + 1, 3);
+      // This was earlier:
+         //memcpy(uid_resp, uid_resp + 1, 3);
+         // But memcpy should not be used for overlapping arrays,
+         // and memmove appears to not be available in the arm build.
+         // So this has been replaced with a for-loop:
+         for(int xx = 0; xx < 3; xx++) 
+            uid_resp[xx] = uid_resp[xx+1];
       uid_resp_len = 3;
     }
 
@@ -1773,8 +1784,7 @@ void iso14443a_setup(uint8_t fpga_minor_mode) {
        SetAdcMuxFor(GPIO_MUXSEL_HIPKD);
 
        // Signal field is on with the appropriate LED
-       if (fpga_minor_mode == FPGA_HF_ISO14443A_READER_MOD
-               || fpga_minor_mode == FPGA_HF_ISO14443A_READER_LISTEN) {
+       if (fpga_minor_mode == FPGA_HF_ISO14443A_READER_MOD     || fpga_minor_mode == FPGA_HF_ISO14443A_READER_LISTEN) {
                LED_D_ON();
        } else {
                LED_D_OFF();
@@ -1787,7 +1797,7 @@ void iso14443a_setup(uint8_t fpga_minor_mode) {
        DemodReset();
        UartReset();
        NextTransferTime = 2*DELAY_ARM2AIR_AS_READER;
-       iso14a_set_timeout(1050); // 10ms default
+       iso14a_set_timeout(1050); // 10ms default  10*105 = 
 }
 
 int iso14_apdu(uint8_t * cmd, size_t cmd_len, void * data) {
@@ -1825,8 +1835,8 @@ void ReaderIso14443a(UsbCommand *c)
 {
        iso14a_command_t param = c->arg[0];
        uint8_t *cmd = c->d.asBytes;
-       size_t len = c->arg[1];
-       size_t lenbits = c->arg[2];
+       size_t len = c->arg[1] & 0xFFFF;
+       size_t lenbits = c->arg[1] >> 16;
        uint32_t arg0 = 0;
        byte_t buf[USB_CMD_DATA_SIZE];
   
@@ -1850,7 +1860,7 @@ void ReaderIso14443a(UsbCommand *c)
        }
 
        if(param & ISO14A_SET_TIMEOUT) {
-               iso14a_timeout = c->arg[2];
+               iso14a_set_timeout(c->arg[2]);
        }
 
        if(param & ISO14A_APDU) {
@@ -1862,9 +1872,10 @@ void ReaderIso14443a(UsbCommand *c)
                if(param & ISO14A_APPEND_CRC) {
                        AppendCrc14443a(cmd,len);
                        len += 2;
-                       if (lenbits) lenbits += 16;
+                       if(lenbits>0) 
+                               lenbits += 16; 
                }
-               if(lenbits>0) {
+               if(lenbits>0) {                 
                        ReaderTransmitBitsPar(cmd,lenbits,GetParity(cmd,lenbits/8), NULL);
                } else {
                        ReaderTransmit(cmd,len, NULL);
@@ -1936,10 +1947,11 @@ void ReaderMifare(bool first_try)
        uint8_t uid[10];
        uint32_t cuid;
 
-       uint32_t nt, previous_nt;
+       uint32_t nt = 0;
+       uint32_t previous_nt = 0;
        static uint32_t nt_attacked = 0;
-       byte_t par_list[8] = {0,0,0,0,0,0,0,0};
-       byte_t ks_list[8] = {0,0,0,0,0,0,0,0};
+       byte_t par_list[8] = {0x00};
+       byte_t ks_list[8] = {0x00};
 
        static uint32_t sync_time;
        static uint32_t sync_cycles;
@@ -1948,8 +1960,6 @@ void ReaderMifare(bool first_try)
        uint16_t consecutive_resyncs = 0;
        int isOK = 0;
 
-
-
        if (first_try) { 
                mf_nr_ar3 = 0;
                iso14443a_setup(FPGA_HF_ISO14443A_READER_MOD);
@@ -1972,6 +1982,7 @@ void ReaderMifare(bool first_try)
        LED_C_OFF();
        
   
+    Dbprintf("Mifare: Before loopen");
        for(uint16_t i = 0; TRUE; i++) {
                
                WDT_HIT();
@@ -2206,12 +2217,9 @@ void Mifare1ksim(uint8_t flags, uint8_t exitAfterNReads, uint8_t arg2, uint8_t *
 
        if (MF_DBGLEVEL >= 1)   {
                if (!_7BUID) {
-                       Dbprintf("4B UID: %02x%02x%02x%02x", 
-                               rUIDBCC1[0], rUIDBCC1[1], rUIDBCC1[2], rUIDBCC1[3]);
+                       Dbprintf("4B UID: %02x%02x%02x%02x",rUIDBCC1[0] , rUIDBCC1[1] , rUIDBCC1[2] , rUIDBCC1[3]);
                } else {
-                       Dbprintf("7B UID: (%02x)%02x%02x%02x%02x%02x%02x%02x",
-                               rUIDBCC1[0], rUIDBCC1[1], rUIDBCC1[2], rUIDBCC1[3],
-                               rUIDBCC2[0], rUIDBCC2[1] ,rUIDBCC2[2], rUIDBCC2[3]);
+                       Dbprintf("7B UID: (%02x)%02x%02x%02x%02x%02x%02x%02x",rUIDBCC1[0] , rUIDBCC1[1] , rUIDBCC1[2] , rUIDBCC1[3],rUIDBCC2[0],rUIDBCC2[1] ,rUIDBCC2[2] , rUIDBCC2[3]);
                }
        }
 
@@ -2279,7 +2287,7 @@ void Mifare1ksim(uint8_t flags, uint8_t exitAfterNReads, uint8_t arg2, uint8_t *
                                // select card
                                if (len == 9 && 
                                                (receivedCmd[0] == 0x93 && receivedCmd[1] == 0x70 && memcmp(&receivedCmd[2], rUIDBCC1, 4) == 0)) {
-                                       EmSendCmd(_7BUID?rSAK1:rSAK, sizeof(_7BUID?rSAK1:rSAK));
+                                       EmSendCmd(_7BUID?rSAK1:rSAK, _7BUID?sizeof(rSAK1):sizeof(rSAK));
                                        cuid = bytes_to_num(rUIDBCC1, 4);
                                        if (!_7BUID) {
                                                cardSTATE = MFEMUL_WORK;
@@ -2321,9 +2329,7 @@ void Mifare1ksim(uint8_t flags, uint8_t exitAfterNReads, uint8_t arg2, uint8_t *
 
                                // test if auth OK
                                if (cardRr != prng_successor(nonce, 64)){
-                                       if (MF_DBGLEVEL >= 2) Dbprintf("AUTH FAILED for sector %d with key %c. cardRr=%08x, succ=%08x",
-                                                       cardAUTHSC, cardAUTHKEY == 0 ? 'A' : 'B',
-                                                       cardRr, prng_successor(nonce, 64));
+                                       if (MF_DBGLEVEL >= 2)   Dbprintf("AUTH FAILED. cardRr=%08x, succ=%08x",cardRr, prng_successor(nonce, 64));
                                        // Shouldn't we respond anything here?
                                        // Right now, we don't nack or anything, which causes the
                                        // reader to do a WUPA after a while. /Martin
Impressum, Datenschutz