From 93dc631353e11cffe7028393160676c010460866 Mon Sep 17 00:00:00 2001 From: merlokk Date: Wed, 11 Oct 2017 14:05:06 +0300 Subject: [PATCH] arm side multisector `hf mf chk` works --- armsrc/mifarecmd.c | 33 +++++++++++++++++++++++++-------- armsrc/mifareutil.c | 28 +++++++++++++++++----------- armsrc/mifareutil.h | 3 ++- 3 files changed, 44 insertions(+), 20 deletions(-) diff --git a/armsrc/mifarecmd.c b/armsrc/mifarecmd.c index eb2201c4..f20f071c 100644 --- a/armsrc/mifarecmd.c +++ b/armsrc/mifarecmd.c @@ -962,6 +962,7 @@ void MifareChkKeys(uint16_t arg0, uint8_t arg1, uint8_t arg2, uint8_t *datain) uint8_t blockNo = arg0 & 0xff; uint8_t keyType = (arg0 >> 8) & 0xff; bool clearTrace = arg1 & 0x01; + bool multisectorCheck = arg1 & 0x02; uint8_t keyCount = arg2; // clear debug level @@ -976,15 +977,31 @@ void MifareChkKeys(uint16_t arg0, uint8_t arg1, uint8_t arg2, uint8_t *datain) if (clearTrace) clear_trace(); set_tracing(true); - int res = MifareChkBlockKeys(datain, keyCount, blockNo, keyType, OLD_MF_DBGLEVEL); - - LED_B_ON(); - if (res > 0) { - cmd_send(CMD_ACK, 1, 0, 0, datain + (res - 1) * 6, 6); - } else { - cmd_send(CMD_ACK, 0, 0, 0, NULL, 0); + if (multisectorCheck) { + Dbprintf("multisector"); + TKeyIndex keyIndex = {0}; + uint8_t sectorCnt = blockNo; + int res = MifareMultisectorChk(datain, keyCount, sectorCnt, keyType, OLD_MF_DBGLEVEL, &keyIndex); + Dbprintf("[0][0]=%d [0][13]=%d [0][15]=%d ", keyIndex[0][0], keyIndex[0][13], keyIndex[0][15]); + + LED_B_ON(); + if (res > 0) { + cmd_send(CMD_ACK, 1, 0, 0, keyIndex, 80); + } else { + cmd_send(CMD_ACK, 0, 0, 0, NULL, 0); + } + LED_B_OFF(); + } else { + int res = MifareChkBlockKeys(datain, keyCount, blockNo, keyType, OLD_MF_DBGLEVEL); + + LED_B_ON(); + if (res > 0) { + cmd_send(CMD_ACK, 1, 0, 0, datain + (res - 1) * 6, 6); + } else { + cmd_send(CMD_ACK, 0, 0, 0, NULL, 0); + } + LED_B_OFF(); } - LED_B_OFF(); FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF); LEDsoff(); diff --git a/armsrc/mifareutil.c b/armsrc/mifareutil.c index 3cc49b06..0f8c0416 100644 --- a/armsrc/mifareutil.c +++ b/armsrc/mifareutil.c @@ -772,7 +772,6 @@ int mifare_desfire_des_auth2(uint32_t uid, uint8_t *key, uint8_t *blockData){ // one key check int MifareChkBlockKey(uint8_t *uid, uint32_t *cuid, uint8_t *cascade_levels, uint64_t ui64Key, uint8_t blockNo, uint8_t keyType, uint8_t debugLevel) { - uint32_t timeout = 0; struct Crypto1State mpcs = {0, 0}; struct Crypto1State *pcs; pcs = &mpcs; @@ -792,19 +791,17 @@ int MifareChkBlockKey(uint8_t *uid, uint32_t *cuid, uint8_t *cascade_levels, uin } } else { // no need for anticollision. We can directly select the card if(!iso14443a_select_card(uid, NULL, NULL, false, *cascade_levels, true)) { - if (debugLevel >= 1) Dbprintf("ChkKeys: Can't select card (UID) %d", *cascade_levels); + if (debugLevel >= 1) Dbprintf("ChkKeys: Can't select card (UID) lvl=%d", *cascade_levels); return 1; } } if(mifare_classic_auth(pcs, *cuid, blockNo, keyType, ui64Key, AUTH_FIRST)) { - uint8_t dummy_answer = 0; - ReaderTransmit(&dummy_answer, 1, NULL); - timeout = GetCountSspClk() + AUTHENTICATION_TIMEOUT; - - // wait for the card to become ready again - while(GetCountSspClk() < timeout); + SpinDelayUs(AUTHENTICATION_TIMEOUT); return 2; + } else { + // it needs after success authentication + mifare_classic_halt(pcs, *cuid); } return 0; @@ -832,10 +829,12 @@ int MifareChkBlockKeys(uint8_t *keys, uint8_t keyCount, uint8_t blockNo, uint8_t // can't select if (res == 1) { retryCount++; - if (retryCount > 10) { + if (retryCount >= 5) { return -1; } - --i; // try same key once again + --i; // try the same key once again + SpinDelay(50); +// Dbprintf("ChkKeys: block=%d key=%d. Try the same key once again...", blockNo, keyType); continue; } @@ -852,11 +851,15 @@ int MifareChkBlockKeys(uint8_t *keys, uint8_t keyCount, uint8_t blockNo, uint8_t } // multisector multikey check -int MifareMultisectorChk(uint8_t *keys, uint8_t keyCount, uint8_t SectorCount, uint8_t keyType, uint8_t debugLevel, uint8_t (*keyIndex)[2][40]) { +int MifareMultisectorChk(uint8_t *keys, uint8_t keyCount, uint8_t SectorCount, uint8_t keyType, uint8_t debugLevel, TKeyIndex *keyIndex) { int res = 0; + +// 3.2 ms/auth + int clk = GetCountSspClk(); for(int sc = 0; sc < SectorCount; sc++){ for(int key = keyType & 0x01; key < 2; keyType==2?(key++):(key = 2)) { + WDT_HIT(); res = MifareChkBlockKeys(keys, keyCount, FirstBlockOfSector(sc), key, debugLevel); if (res < 0) { return res; @@ -867,6 +870,9 @@ int MifareMultisectorChk(uint8_t *keys, uint8_t keyCount, uint8_t SectorCount, u } } } + + Dbprintf("%d %d", GetCountSspClk() - clk, (GetCountSspClk() - clk)/(SectorCount*keyCount*2)); + return 0; } diff --git a/armsrc/mifareutil.h b/armsrc/mifareutil.h index fcaba0df..8ffd5e89 100644 --- a/armsrc/mifareutil.h +++ b/armsrc/mifareutil.h @@ -103,8 +103,9 @@ int emlSetValBl(uint32_t blReg, uint8_t blBlock, int blockNum); int emlCheckValBl(int blockNum); // mifare check keys +typedef uint8_t TKeyIndex[2][40]; int MifareChkBlockKey(uint8_t *uid, uint32_t *cuid, uint8_t *cascade_levels, uint64_t ui64Key, uint8_t blockNo, uint8_t keyType, uint8_t debugLevel); int MifareChkBlockKeys(uint8_t *keys, uint8_t keyCount, uint8_t blockNo, uint8_t keyType, uint8_t debugLevel); -int MifareMultisectorChk(uint8_t *keys, uint8_t keyCount, uint8_t SectorCount, uint8_t keyType, uint8_t debugLevel, uint8_t (*keyIndex)[2][40]); +int MifareMultisectorChk(uint8_t *keys, uint8_t keyCount, uint8_t SectorCount, uint8_t keyType, uint8_t debugLevel, TKeyIndex *keyIndex); #endif