Make CRC size explicit in some cmd/reply size calculations

This commit is contained in:
Philippe Teuwen 2025-06-16 10:51:40 +02:00
commit 67da1c8ca5
4 changed files with 10 additions and 9 deletions

View file

@ -1764,7 +1764,7 @@ void SimulateIso14443aTag(uint8_t tagType, uint16_t flags, uint8_t *useruid, uin
} else { } else {
// first blocks of emu are header // first blocks of emu are header
uint16_t start = (block * 4) + MFU_DUMP_PREFIX_LENGTH; uint16_t start = (block * 4) + MFU_DUMP_PREFIX_LENGTH;
uint8_t emdata[MIFARE_BLOCK_SIZE + 2] = {0}; uint8_t emdata[MIFARE_BLOCK_SIZE + CRC16_SIZE] = {0};
emlGet(emdata, start, MIFARE_BLOCK_SIZE); emlGet(emdata, start, MIFARE_BLOCK_SIZE);
AddCrc14A(emdata, MIFARE_BLOCK_SIZE); AddCrc14A(emdata, MIFARE_BLOCK_SIZE);
EmSendCmd(emdata, sizeof(emdata)); EmSendCmd(emdata, sizeof(emdata));
@ -1783,7 +1783,7 @@ void SimulateIso14443aTag(uint8_t tagType, uint16_t flags, uint8_t *useruid, uin
// block1 = 4byte UID. // block1 = 4byte UID.
p_response = &responses[RESP_INDEX_UIDC1]; p_response = &responses[RESP_INDEX_UIDC1];
} else { // all other tags (16 byte block tags) } else { // all other tags (16 byte block tags)
uint8_t emdata[MIFARE_BLOCK_SIZE + 2] = {0}; uint8_t emdata[MIFARE_BLOCK_SIZE + CRC16_SIZE] = {0};
emlGet(emdata, block, MIFARE_BLOCK_SIZE); emlGet(emdata, block, MIFARE_BLOCK_SIZE);
AddCrc14A(emdata, MIFARE_BLOCK_SIZE); AddCrc14A(emdata, MIFARE_BLOCK_SIZE);
EmSendCmd(emdata, sizeof(emdata)); EmSendCmd(emdata, sizeof(emdata));

View file

@ -2909,7 +2909,7 @@ void MifareCGetBlock(uint32_t arg0, uint32_t arg1, uint8_t *datain) {
} }
// read block // read block
if ((mifare_sendcmd_short(NULL, CRYPT_NONE, ISO14443A_CMD_READBLOCK, blockNo, receivedAnswer, sizeof(receivedAnswer), receivedAnswerPar, NULL) != MIFARE_BLOCK_SIZE + 2)) { if ((mifare_sendcmd_short(NULL, CRYPT_NONE, ISO14443A_CMD_READBLOCK, blockNo, receivedAnswer, sizeof(receivedAnswer), receivedAnswerPar, NULL) != MIFARE_BLOCK_SIZE + CRC16_SIZE)) {
if (g_dbglevel >= DBG_ERROR) Dbprintf("read block send command error"); if (g_dbglevel >= DBG_ERROR) Dbprintf("read block send command error");
errormsg = 0; errormsg = 0;
break; break;
@ -3515,7 +3515,7 @@ void MifareGen3Blk(uint8_t block_len, uint8_t *block) {
int retval = PM3_SUCCESS; int retval = PM3_SUCCESS;
uint8_t block_cmd[5] = { 0x90, 0xf0, 0xcc, 0xcc, 0x10 }; uint8_t block_cmd[5] = { 0x90, 0xf0, 0xcc, 0xcc, 0x10 };
uint8_t cmdlen = sizeof(block_cmd) + MIFARE_BLOCK_SIZE + 2; uint8_t cmdlen = sizeof(block_cmd) + MIFARE_BLOCK_SIZE + CRC16_SIZE;
uint8_t *cmd = BigBuf_calloc(cmdlen); uint8_t *cmd = BigBuf_calloc(cmdlen);
iso14a_card_select_t *card_info = (iso14a_card_select_t *) BigBuf_calloc(sizeof(iso14a_card_select_t)); iso14a_card_select_t *card_info = (iso14a_card_select_t *) BigBuf_calloc(sizeof(iso14a_card_select_t));
@ -3532,7 +3532,7 @@ void MifareGen3Blk(uint8_t block_len, uint8_t *block) {
bool doReselect = false; bool doReselect = false;
if (block_len < MIFARE_BLOCK_SIZE) { if (block_len < MIFARE_BLOCK_SIZE) {
if ((mifare_sendcmd_short(NULL, CRYPT_NONE, ISO14443A_CMD_READBLOCK, 0, &cmd[sizeof(block_cmd)], MIFARE_BLOCK_SIZE + 2, NULL, NULL) != MIFARE_BLOCK_SIZE + 2)) { if ((mifare_sendcmd_short(NULL, CRYPT_NONE, ISO14443A_CMD_READBLOCK, 0, &cmd[sizeof(block_cmd)], MIFARE_BLOCK_SIZE + CRC16_SIZE, NULL, NULL) != MIFARE_BLOCK_SIZE + CRC16_SIZE)) {
if (g_dbglevel >= DBG_ERROR) Dbprintf("Read manufacturer block failed"); if (g_dbglevel >= DBG_ERROR) Dbprintf("Read manufacturer block failed");
retval = PM3_ESOFT; retval = PM3_ESOFT;
goto OUT; goto OUT;
@ -3567,7 +3567,7 @@ void MifareGen3Blk(uint8_t block_len, uint8_t *block) {
} }
} }
retval = DoGen3Cmd(cmd, sizeof(block_cmd) + MIFARE_BLOCK_SIZE + 2); retval = DoGen3Cmd(cmd, sizeof(block_cmd) + MIFARE_BLOCK_SIZE + CRC16_SIZE);
} }
OUT: OUT:

View file

@ -1039,8 +1039,8 @@ void Mifare1ksim(uint16_t flags, uint8_t exitAfterNReads, uint8_t *uid, uint16_t
} }
} }
AddCrc14A(response, MIFARE_BLOCK_SIZE); AddCrc14A(response, MIFARE_BLOCK_SIZE);
mf_crypto1_encrypt(pcs, response, MIFARE_BLOCK_SIZE + 2, response_par); mf_crypto1_encrypt(pcs, response, MIFARE_BLOCK_SIZE + CRC16_SIZE, response_par);
EmSendCmdPar(response, MIFARE_BLOCK_SIZE + 2, response_par); EmSendCmdPar(response, MIFARE_BLOCK_SIZE + CRC16_SIZE, response_par);
FpgaDisableTracing(); FpgaDisableTracing();
if (g_dbglevel >= DBG_EXTENDED) { if (g_dbglevel >= DBG_EXTENDED) {
@ -1309,7 +1309,7 @@ void Mifare1ksim(uint16_t flags, uint8_t exitAfterNReads, uint8_t *uid, uint16_t
// WRITE BL2 // WRITE BL2
case MFEMUL_WRITEBL2: { case MFEMUL_WRITEBL2: {
if (receivedCmd_len == MIFARE_BLOCK_SIZE + 2) { if (receivedCmd_len == MIFARE_BLOCK_SIZE + CRC16_SIZE) {
mf_crypto1_decryptEx(pcs, receivedCmd, receivedCmd_len, receivedCmd_dec); mf_crypto1_decryptEx(pcs, receivedCmd, receivedCmd_len, receivedCmd_dec);

View file

@ -20,6 +20,7 @@
#include "common.h" #include "common.h"
#define CRC16_SIZE 2
#define CRC16_POLY_CCITT 0x1021 #define CRC16_POLY_CCITT 0x1021
#define CRC16_POLY_KERMIT 0x8408 #define CRC16_POLY_KERMIT 0x8408
#define CRC16_POLY_LEGIC 0xc6c6 //0x6363 #define CRC16_POLY_LEGIC 0xc6c6 //0x6363