move crc search function to desfirecrypto

This commit is contained in:
merlokk 2021-07-09 13:09:59 +03:00
parent ec79e35dcc
commit a7f39a1b79
3 changed files with 50 additions and 34 deletions

View file

@ -109,6 +109,50 @@ size_t DesfireGetMACLength(DesfireContext *ctx) {
return mac_length;
}
size_t DesfireSearchCRCPos(uint8_t *data, size_t datalen, uint8_t respcode, uint8_t crclen) {
size_t crcpos = datalen - 1;
while (crcpos > 0)
if (data[crcpos] == 0)
crcpos--;
else
break;
crcpos++; // crc may be 0x00000000 or 0x0000
PrintAndLogEx(INFO, "crcpos: %d", crcpos);
if (crcpos < crclen) {
PrintAndLogEx(WARNING, "No space for crc. pos: %d", crcpos);
return 0;
}
uint8_t crcdata[1024] = {0};
bool crcok = false;
for (int i = 0; i < crclen + 1; i++) {
PrintAndLogEx(INFO, "--crcpos: %d", crcpos - i);
if (crcpos - i == 0)
break;
if (crcpos - i + crclen > datalen)
continue;
PrintAndLogEx(INFO, "--crcposcheck: %d", crcpos - i);
memcpy(crcdata, data, crcpos - i);
crcdata[crcpos - i] = respcode;
bool res;
if (crclen == 4)
res = desfire_crc32_check(crcdata, crcpos - i + 1, &data[crcpos - i]);
else
res = iso14443a_crc_check(crcdata, crcpos - i + 1, &data[crcpos - i]);
if (res) {
PrintAndLogEx(INFO, "--crc OK pos: %d", crcpos - i);
crcpos -= i;
crcok = true;
break;
}
}
if (!crcok)
crcpos = 0;
return crcpos;
}
static void DesfireCryptoEncDecSingleBlock(uint8_t *key, DesfireCryptoAlgorythm keyType, uint8_t *data, uint8_t *dstdata, uint8_t *ivect, bool dir_to_send, bool encode) {
size_t block_size = desfire_get_key_block_length(keyType);
uint8_t sdata[MAX_CRYPTO_BLOCK_SIZE] = {0};

View file

@ -95,6 +95,7 @@ void DesfireSetKdf(DesfireContext *ctx, uint8_t kdfAlgo, uint8_t *kdfInput, uint
bool DesfireIsAuthenticated(DesfireContext *dctx);
size_t DesfireGetMACLength(DesfireContext *ctx);
size_t DesfireSearchCRCPos(uint8_t *data, size_t datalen, uint8_t respcode, uint8_t crclen);
void DesfireCryptoEncDec(DesfireContext *ctx, bool use_session_key, uint8_t *srcdata, size_t srcdatalen, uint8_t *dstdata, bool encode);
void DesfireCryptoEncDecEx(DesfireContext *ctx, bool use_session_key, uint8_t *srcdata, size_t srcdatalen, uint8_t *dstdata, bool encode, uint8_t *iv);

View file

@ -161,41 +161,12 @@ static void DesfireSecureChannelDecodeEV1(DesfireContext *ctx, uint8_t *srcdata,
DesfireCryptoEncDec(ctx, true, srcdata, srcdatalen, dstdata, false);
PrintAndLogEx(INFO, "decoded[%d]: %s", srcdatalen, sprint_hex(dstdata, srcdatalen));
size_t crcpos = srcdatalen - 1;
while (crcpos > 0)
if (dstdata[crcpos] == 0)
crcpos--;
else
break;
crcpos++; // crc may be 0x00000000
*dstdatalen = crcpos;
PrintAndLogEx(INFO, "crcpos: %d", crcpos);
if (crcpos < 4) {
PrintAndLogEx(WARNING, "No space for crc. pos: %d", crcpos);
return;
}
uint8_t crcdata[1024] = {0};
bool crcok = false;
for (int i = 0; i < 5; i++) {
if (crcpos == 0)
break;
if (crcpos - i + 4 > srcdatalen)
continue;
memcpy(crcdata, dstdata, crcpos - i);
crcdata[crcpos - i] = respcode;
if (desfire_crc32_check(crcdata, crcpos - i + 1, &dstdata[crcpos - i])) {
crcpos -= i;
crcok = true;
break;
}
}
if (crcok) {
*dstdatalen = crcpos;
size_t puredatalen = DesfireSearchCRCPos(dstdata, srcdatalen, respcode, 4);
if (puredatalen != 0) {
*dstdatalen = puredatalen;
} else {
PrintAndLogEx(WARNING, "CRC32 error. pos: %d", crcpos);
PrintAndLogEx(WARNING, "CRC32 error.");
*dstdatalen = srcdatalen;
}
} else {