proxmark3/client/mifare4.c

312 lines
7.9 KiB
C
Raw Normal View History

2018-10-24 23:18:05 +08:00
//-----------------------------------------------------------------------------
// Copyright (C) 2018 Merlok
2018-11-01 01:39:32 +08:00
// Copyright (C) 2018 drHatson
2018-10-24 23:18:05 +08:00
//
// This code is licensed to you under the terms of the GNU GPL, version 2 or,
// at your option, any later version. See the LICENSE.txt file for the text of
// the license.
//-----------------------------------------------------------------------------
// iso14443-4 mifare commands
//-----------------------------------------------------------------------------
#include "mifare4.h"
#include <ctype.h>
#include <string.h>
#include "cmdhf14a.h"
#include "util.h"
#include "ui.h"
2018-11-08 00:05:02 +08:00
#include "crypto/libpcrypto.h"
2018-10-24 23:18:05 +08:00
2018-11-29 22:05:57 +08:00
AccessConditions_t MFAccessConditions[] = {
{0x00, "rdAB wrAB incAB dectrAB"},
{0x01, "rdAB dectrAB"},
{0x02, "rdAB"},
{0x03, "rdB wrB"},
{0x04, "rdAB wrB"},
{0x05, "rdB"},
{0x06, "rdAB wrB incB dectrAB"},
{0x07, "none"}
};
AccessConditions_t MFAccessConditionsTrailer[] = {
{0x00, "rdAbyA rdCbyA rdBbyA wrBbyA"},
{0x01, "wrAbyA rdCbyA wrCbyA rdBbyA wrBbyA"},
{0x02, "rdCbyA rdBbyA"},
{0x03, "wrAbyB rdCbyAB wrCbyB wrBbyB"},
{0x04, "wrAbyB rdCbyAB wrBbyB"},
{0x05, "rdCbyAB wrCbyB"},
{0x06, "rdCbyAB"},
{0x07, "rdCbyAB"}
};
char *mfGetAccessConditionsDesc(uint8_t blockn, uint8_t *data) {
static char StaticNone[] = "none";
uint8_t data1 = ((data[1] >> 4) & 0x0f) >> blockn;
uint8_t data2 = ((data[2]) & 0x0f) >> blockn;
uint8_t data3 = ((data[2] >> 4) & 0x0f) >> blockn;
uint8_t cond = (data1 & 0x01) << 2 | (data2 & 0x01) << 1 | (data3 & 0x01);
if (blockn == 3) {
for (int i = 0; i < ARRAYLEN(MFAccessConditionsTrailer); i++)
if (MFAccessConditionsTrailer[i].cond == cond) {
return MFAccessConditionsTrailer[i].description;
}
} else {
for (int i = 0; i < ARRAYLEN(MFAccessConditions); i++)
if (MFAccessConditions[i].cond == cond) {
return MFAccessConditions[i].description;
}
};
return StaticNone;
};
2018-11-01 01:39:32 +08:00
int CalculateEncIVCommand(mf4Session *session, uint8_t *iv, bool verbose) {
memcpy(&iv[0], session->TI, 4);
memcpy(&iv[4], &session->R_Ctr, 2);
memcpy(&iv[6], &session->W_Ctr, 2);
memcpy(&iv[8], &session->R_Ctr, 2);
memcpy(&iv[10], &session->W_Ctr, 2);
memcpy(&iv[12], &session->R_Ctr, 2);
memcpy(&iv[14], &session->W_Ctr, 2);
return 0;
}
int CalculateEncIVResponse(mf4Session *session, uint8_t *iv, bool verbose) {
memcpy(&iv[0], &session->R_Ctr, 2);
memcpy(&iv[2], &session->W_Ctr, 2);
memcpy(&iv[4], &session->R_Ctr, 2);
memcpy(&iv[6], &session->W_Ctr, 2);
memcpy(&iv[8], &session->R_Ctr, 2);
memcpy(&iv[10], &session->W_Ctr, 2);
memcpy(&iv[12], session->TI, 4);
return 0;
}
int CalculateMAC(mf4Session *session, MACType_t mtype, uint8_t blockNum, uint8_t blockCount, uint8_t *data, int datalen, uint8_t *mac, bool verbose) {
if (!session || !session->Authenticated || !mac || !data || !datalen || datalen < 1)
2018-10-26 19:12:20 +08:00
return 1;
memset(mac, 0x00, 8);
2018-11-01 01:39:32 +08:00
uint16_t ctr = session->R_Ctr;
switch(mtype) {
case mtypWriteCmd:
case mtypWriteResp:
ctr = session->W_Ctr;
break;
case mtypReadCmd:
case mtypReadResp:
break;
}
uint8_t macdata[2049] = {data[0], (ctr & 0xFF), (ctr >> 8), 0};
int macdatalen = datalen;
memcpy(&macdata[3], session->TI, 4);
switch(mtype) {
case mtypReadCmd:
memcpy(&macdata[7], &data[1], datalen - 1);
macdatalen = datalen + 6;
break;
case mtypReadResp:
macdata[7] = blockNum;
macdata[8] = 0;
macdata[9] = blockCount;
memcpy(&macdata[10], &data[1], datalen - 1);
macdatalen = datalen + 9;
break;
case mtypWriteCmd:
memcpy(&macdata[7], &data[1], datalen - 1);
macdatalen = datalen + 6;
break;
case mtypWriteResp:
macdatalen = 1 + 6;
break;
}
2018-10-26 19:12:20 +08:00
if (verbose)
2018-11-01 01:39:32 +08:00
PrintAndLog("MAC data[%d]: %s", macdatalen, sprint_hex(macdata, macdatalen));
2018-10-26 19:12:20 +08:00
2018-11-01 01:39:32 +08:00
return aes_cmac8(NULL, session->Kmac, macdata, mac, macdatalen);
2018-10-26 19:12:20 +08:00
}
2018-10-24 23:18:05 +08:00
int MifareAuth4(mf4Session *session, uint8_t *keyn, uint8_t *key, bool activateField, bool leaveSignalON, bool verbose) {
uint8_t data[257] = {0};
int datalen = 0;
2018-11-01 01:39:32 +08:00
uint8_t RndA[17] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x00};
uint8_t RndB[17] = {0};
2018-10-24 23:18:05 +08:00
if (session)
session->Authenticated = false;
uint8_t cmd1[] = {0x70, keyn[1], keyn[0], 0x00};
int res = ExchangeRAW14a(cmd1, sizeof(cmd1), activateField, true, data, sizeof(data), &datalen);
if (res) {
2018-10-25 00:05:30 +08:00
PrintAndLogEx(ERR, "Exchande raw error: %d", res);
2018-10-24 23:18:05 +08:00
DropField();
return 2;
}
if (verbose)
2018-10-25 00:05:30 +08:00
PrintAndLogEx(INFO, "<phase1: %s", sprint_hex(data, datalen));
2018-10-24 23:18:05 +08:00
if (datalen < 1) {
2018-10-25 00:05:30 +08:00
PrintAndLogEx(ERR, "Card response wrong length: %d", datalen);
2018-10-24 23:18:05 +08:00
DropField();
return 3;
}
if (data[0] != 0x90) {
2018-10-25 00:05:30 +08:00
PrintAndLogEx(ERR, "Card response error: %02x", data[2]);
2018-10-24 23:18:05 +08:00
DropField();
return 3;
}
if (datalen != 19) { // code 1b + 16b + crc 2b
2018-10-25 00:05:30 +08:00
PrintAndLogEx(ERR, "Card response must be 19 bytes long instead of: %d", datalen);
2018-10-24 23:18:05 +08:00
DropField();
return 3;
}
2018-11-01 01:39:32 +08:00
aes_decode(NULL, key, &data[1], RndB, 16);
RndB[16] = RndB[0];
2018-10-24 23:18:05 +08:00
if (verbose)
2018-11-01 01:39:32 +08:00
PrintAndLogEx(INFO, "RndB: %s", sprint_hex(RndB, 16));
2018-10-24 23:18:05 +08:00
uint8_t cmd2[33] = {0};
cmd2[0] = 0x72;
uint8_t raw[32] = {0};
2018-11-01 01:39:32 +08:00
memmove(raw, RndA, 16);
memmove(&raw[16], &RndB[1], 16);
2018-10-24 23:18:05 +08:00
aes_encode(NULL, key, raw, &cmd2[1], 32);
if (verbose)
2018-10-25 00:05:30 +08:00
PrintAndLogEx(INFO, ">phase2: %s", sprint_hex(cmd2, 33));
2018-10-24 23:18:05 +08:00
res = ExchangeRAW14a(cmd2, sizeof(cmd2), false, true, data, sizeof(data), &datalen);
if (res) {
2018-10-25 00:05:30 +08:00
PrintAndLogEx(ERR, "Exchande raw error: %d", res);
2018-10-24 23:18:05 +08:00
DropField();
return 4;
}
if (verbose)
2018-10-25 00:05:30 +08:00
PrintAndLogEx(INFO, "<phase2: %s", sprint_hex(data, datalen));
2018-10-24 23:18:05 +08:00
aes_decode(NULL, key, &data[1], raw, 32);
if (verbose) {
2018-10-25 00:05:30 +08:00
PrintAndLogEx(INFO, "res: %s", sprint_hex(raw, 32));
2018-11-01 01:39:32 +08:00
PrintAndLogEx(INFO, "RndA`: %s", sprint_hex(&raw[4], 16));
2018-10-24 23:18:05 +08:00
}
2018-11-01 01:39:32 +08:00
if (memcmp(&raw[4], &RndA[1], 16)) {
2018-10-25 00:05:30 +08:00
PrintAndLogEx(ERR, "\nAuthentication FAILED. rnd not equal");
2018-10-24 23:18:05 +08:00
if (verbose) {
2018-11-01 01:39:32 +08:00
PrintAndLogEx(ERR, "RndA reader: %s", sprint_hex(&RndA[1], 16));
PrintAndLogEx(ERR, "RndA card: %s", sprint_hex(&raw[4], 16));
2018-10-24 23:18:05 +08:00
}
DropField();
return 5;
}
2018-11-01 01:39:32 +08:00
if (verbose) {
PrintAndLogEx(INFO, " TI: %s", sprint_hex(raw, 4));
PrintAndLogEx(INFO, "pic: %s", sprint_hex(&raw[20], 6));
PrintAndLogEx(INFO, "pcd: %s", sprint_hex(&raw[26], 6));
}
uint8_t kenc[16] = {0};
memcpy(&kenc[0], &RndA[11], 5);
memcpy(&kenc[5], &RndB[11], 5);
for(int i = 0; i < 5; i++)
kenc[10 + i] = RndA[4 + i] ^ RndB[4 + i];
kenc[15] = 0x11;
aes_encode(NULL, key, kenc, kenc, 16);
if (verbose) {
PrintAndLogEx(INFO, "kenc: %s", sprint_hex(kenc, 16));
}
uint8_t kmac[16] = {0};
memcpy(&kmac[0], &RndA[7], 5);
memcpy(&kmac[5], &RndB[7], 5);
for(int i = 0; i < 5; i++)
kmac[10 + i] = RndA[0 + i] ^ RndB[0 + i];
kmac[15] = 0x22;
aes_encode(NULL, key, kmac, kmac, 16);
if (verbose) {
PrintAndLogEx(INFO, "kmac: %s", sprint_hex(kmac, 16));
}
2018-10-24 23:18:05 +08:00
if (!leaveSignalON)
DropField();
if (verbose)
PrintAndLog("");
if (session) {
session->Authenticated = true;
2018-11-01 01:39:32 +08:00
session->R_Ctr = 0;
session->W_Ctr = 0;
2018-10-24 23:18:05 +08:00
session->KeyNum = keyn[1] + (keyn[0] << 8);
2018-11-01 01:39:32 +08:00
memmove(session->RndA, RndA, 16);
memmove(session->RndB, RndB, 16);
memmove(session->Key, key, 16);
memmove(session->TI, raw, 4);
memmove(session->PICCap2, &raw[20], 6);
memmove(session->PCDCap2, &raw[26], 6);
memmove(session->Kenc, kenc, 16);
memmove(session->Kmac, kmac, 16);
2018-10-24 23:18:05 +08:00
}
2018-10-25 00:05:30 +08:00
PrintAndLogEx(INFO, "Authentication OK");
2018-10-24 23:18:05 +08:00
return 0;
}
// Mifare Memory Structure: up to 32 Sectors with 4 blocks each (1k and 2k cards),
// plus evtl. 8 sectors with 16 blocks each (4k cards)
uint8_t mfNumBlocksPerSector(uint8_t sectorNo) {
if (sectorNo < 32)
return 4;
else
return 16;
}
uint8_t mfFirstBlockOfSector(uint8_t sectorNo) {
if (sectorNo < 32)
return sectorNo * 4;
else
return 32 * 4 + (sectorNo - 32) * 16;
}
uint8_t mfSectorTrailer(uint8_t blockNo) {
if (blockNo < 32*4) {
return (blockNo | 0x03);
} else {
return (blockNo | 0x0f);
}
}
bool mfIsSectorTrailer(uint8_t blockNo) {
return (blockNo == mfSectorTrailer(blockNo));
}
uint8_t mfSectorNum(uint8_t blockNo) {
if (blockNo < 32 * 4)
return blockNo / 4;
else
return 32 + (blockNo - 32 * 4) / 16;
}