Skip to content

Commit

Permalink
Merge pull request #2021 from nvx/mfc_arm_cleanup
Browse files Browse the repository at this point in the history
Cleanup arm-side reader-mode mifare classic block read/write code.
  • Loading branch information
iceman1001 authored Jul 5, 2023
2 parents 9c947ed + edd8e36 commit 81702ba
Show file tree
Hide file tree
Showing 10 changed files with 242 additions and 516 deletions.
10 changes: 5 additions & 5 deletions armsrc/Standalone/hf_colin.c
Original file line number Diff line number Diff line change
Expand Up @@ -821,7 +821,7 @@ int e_MifareECardLoad(uint32_t numofsectors, uint8_t keytype) {
}

for (uint8_t blockNo = 0; isOK && blockNo < NumBlocksPerSector(s); blockNo++) {
if (isOK && mifare_classic_readblock(pcs, colin_cjcuid, FirstBlockOfSector(s) + blockNo, dataoutbuf)) {
if (isOK && mifare_classic_readblock(pcs, FirstBlockOfSector(s) + blockNo, dataoutbuf)) {
isOK = false;
break;
};
Expand All @@ -838,7 +838,7 @@ int e_MifareECardLoad(uint32_t numofsectors, uint8_t keytype) {
}
}

int res = mifare_classic_halt(pcs, colin_cjcuid);
int res = mifare_classic_halt(pcs);
(void)res;

crypto1_deinit(pcs);
Expand Down Expand Up @@ -986,7 +986,7 @@ int saMifareCSetBlock(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *data
break;
};

if (mifare_classic_halt(NULL, colin_cjcuid)) {
if (mifare_classic_halt(NULL)) {
DbprintfEx(FLAG_NEWLINE, "Halt error");
break;
};
Expand All @@ -1006,7 +1006,7 @@ int saMifareCSetBlock(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *data
break;
};

if (mifare_classic_halt(NULL, colin_cjcuid)) {
if (mifare_classic_halt(NULL)) {
DbprintfEx(FLAG_NEWLINE, "Halt error");
break;
};
Expand Down Expand Up @@ -1043,7 +1043,7 @@ int saMifareCSetBlock(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *data
};

if (workFlags & 0x04) {
if (mifare_classic_halt(NULL, colin_cjcuid)) {
if (mifare_classic_halt(NULL)) {
cjSetCursFRight();

DbprintfEx(FLAG_NEWLINE, "Halt error");
Expand Down
10 changes: 5 additions & 5 deletions armsrc/Standalone/hf_mattyrun.c
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ static int saMifareCSetBlock(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_
break;
};

if (mifare_classic_halt(NULL, mattyrun_cuid)) {
if (mifare_classic_halt(NULL)) {
DbprintfEx(FLAG_NEWLINE, "Halt error");
break;
};
Expand All @@ -129,7 +129,7 @@ static int saMifareCSetBlock(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_
break;
};

if (mifare_classic_halt(NULL, mattyrun_cuid)) {
if (mifare_classic_halt(NULL)) {
DbprintfEx(FLAG_NEWLINE, "Halt error");
break;
};
Expand Down Expand Up @@ -165,7 +165,7 @@ static int saMifareCSetBlock(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_
};

if (workFlags & 0x04) {
if (mifare_classic_halt(NULL, mattyrun_cuid)) {
if (mifare_classic_halt(NULL)) {
DbprintfEx(FLAG_NEWLINE, "Halt error");
break;
};
Expand Down Expand Up @@ -267,7 +267,7 @@ static int saMifareECardLoad(uint32_t numofsectors, uint8_t keytype) {

// failure to read one block, skips to next sector.
for (uint8_t blockNo = 0; blockNo < NumBlocksPerSector(s); blockNo++) {
if (mifare_classic_readblock(pcs, mattyrun_cuid, FirstBlockOfSector(s) + blockNo, dataoutbuf)) {
if (mifare_classic_readblock(pcs, FirstBlockOfSector(s) + blockNo, dataoutbuf)) {
retval = PM3_ESOFT;
break;
};
Expand All @@ -283,7 +283,7 @@ static int saMifareECardLoad(uint32_t numofsectors, uint8_t keytype) {
}
}

int res = mifare_classic_halt(pcs, mattyrun_cuid);
int res = mifare_classic_halt(pcs);
(void)res;

out:
Expand Down
31 changes: 25 additions & 6 deletions armsrc/appmain.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
#include "ticks.h"
#include "commonutil.h"
#include "crc16.h"
#include "protocols.h"


#ifdef WITH_LCD
Expand Down Expand Up @@ -1556,7 +1557,9 @@ static void PacketReceived(PacketCommandNG *packet) {
}
case CMD_HF_MIFARE_READBL: {
mf_readblock_t *payload = (mf_readblock_t *)packet->data.asBytes;
MifareReadBlock(payload->blockno, payload->keytype, payload->key);
uint8_t outbuf[16];
int16_t retval = mifare_cmd_readblocks(MIFARE_AUTH_KEYA + (payload->keytype & 1), payload->key, ISO14443A_CMD_READBLOCK, payload->blockno, 1, outbuf);
reply_ng(CMD_HF_MIFARE_READBL, retval, outbuf, sizeof(outbuf));
break;
}
case CMD_HF_MIFAREU_READBL: {
Expand All @@ -1580,7 +1583,19 @@ static void PacketReceived(PacketCommandNG *packet) {
break;
}
case CMD_HF_MIFARE_WRITEBL: {
MifareWriteBlock(packet->oldarg[0], packet->oldarg[1], packet->data.asBytes);
uint8_t block_no = packet->oldarg[0];
uint8_t key_type = packet->oldarg[1];
uint8_t *key = packet->data.asBytes;
uint8_t *block_data = packet->data.asBytes + 10;

int16_t retval = mifare_cmd_writeblocks(MIFARE_AUTH_KEYA + (key_type & 1), key, ISO14443A_CMD_WRITEBLOCK, block_no, 1, block_data);

// convert ng style retval to old status
if (retval >= 0) {
retval = 1;
}

reply_mix(CMD_ACK, retval, 0, 0, 0, 0);
break;
}
case CMD_HF_MIFARE_VALUE: {
Expand Down Expand Up @@ -1740,26 +1755,30 @@ static void PacketReceived(PacketCommandNG *packet) {
uint8_t key[6];
} PACKED;
struct p *payload = (struct p *) packet->data.asBytes;
MifareReadConfigBlockGDM(payload->key);
uint8_t outbuf[16];
int16_t retval = mifare_cmd_readblocks(MIFARE_MAGIC_GDM_AUTH_KEY, payload->key, MIFARE_MAGIC_GDM_READ_CFG, 0, 1, outbuf);
reply_ng(CMD_HF_MIFARE_G4_GDM_CONFIG, retval, outbuf, sizeof(outbuf));
break;
}
case CMD_HF_MIFARE_G4_GDM_WRCFG: {
struct p {
uint8_t data[16];
} PACKED;
struct p *payload = (struct p *) packet->data.asBytes;
MifareWriteConfigBlockGDM(payload->data);
uint8_t key[6] = {0, 0, 0, 0, 0, 0};
int16_t retval = mifare_cmd_writeblocks(MIFARE_MAGIC_GDM_AUTH_KEY, key, MIFARE_MAGIC_GDM_WRITE_CFG, 0, 1, payload->data);
reply_ng(CMD_HF_MIFARE_G4_GDM_WRCFG, retval, NULL, 0);
break;
}
case CMD_HF_MIFARE_G4_GDM_WRBL: {
struct p {
uint8_t blockno;
uint8_t keytype;
uint8_t key[6];
uint8_t data[16]; // data to be written
} PACKED;
struct p *payload = (struct p *) packet->data.asBytes;
MifareWriteBlockGDM(payload->blockno, payload->keytype, payload->key, payload->data);
int16_t retval = mifare_cmd_writeblocks(MIFARE_MAGIC_GDM_AUTH_KEY, payload->key, MIFARE_MAGIC_GDM_WRITEBLOCK, payload->blockno, 1, payload->data);
reply_ng(CMD_HF_MIFARE_G4_GDM_WRBL, retval, NULL, 0);
break;
}
case CMD_HF_MIFARE_PERSONALIZE_UID: {
Expand Down
10 changes: 7 additions & 3 deletions armsrc/iso14443a.c
Original file line number Diff line number Diff line change
Expand Up @@ -2538,7 +2538,7 @@ static int GetATQA(uint8_t *resp, uint8_t *resp_par, bool use_ecp, bool use_mags

bool first_try = true;
uint32_t retry_timeout = use_ecp ? ECP_RETRY_TIMEOUT : WUPA_RETRY_TIMEOUT;
uint32_t start_time = GetTickCount();
uint32_t start_time;
int len;

// we may need several tries if we did send an unknown command or a wrong authentication before...
Expand All @@ -2562,6 +2562,11 @@ static int GetATQA(uint8_t *resp, uint8_t *resp_par, bool use_ecp, bool use_mags
// Receive the ATQA
len = ReaderReceive(resp, resp_par);

// We set the start_time here otherwise in some cases we miss the window and only ever try once
if (first_try) {
start_time = GetTickCount();
}

first_try = false;
} while (len == 0 && GetTickCountDelta(start_time) <= retry_timeout);

Expand Down Expand Up @@ -2830,10 +2835,9 @@ int iso14443a_fast_select_card(uint8_t *uid_ptr, uint8_t num_cascades) {
// which case we need to make a cascade 2 request and select - this is a long UID
// While the UID is not complete, the 3nd bit (from the right) is set in the SAK.
for (; sak & 0x04; cascade_level++) {
uint8_t sel_all[] = { ISO14443A_CMD_ANTICOLL_OR_SELECT, 0x20 };
uint8_t sel_uid[] = { ISO14443A_CMD_ANTICOLL_OR_SELECT, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
// SELECT_* (L1: 0x93, L2: 0x95, L3: 0x97)
sel_uid[0] = sel_all[0] = 0x93 + cascade_level * 2;
sel_uid[0] = ISO14443A_CMD_ANTICOLL_OR_SELECT + cascade_level * 2;

if (cascade_level < num_cascades - 1) {
uid_resp[0] = 0x88;
Expand Down
Loading

0 comments on commit 81702ba

Please sign in to comment.