mirror of
https://github.com/RfidResearchGroup/proxmark3.git
synced 2025-07-15 09:33:05 -07:00
Fixed: "hf mfdes info"
This commit is contained in:
parent
b44e523300
commit
313ee67ea2
10 changed files with 268 additions and 200 deletions
|
@ -597,7 +597,7 @@ void UsbPacketReceived(uint8_t *packet, int len)
|
||||||
{
|
{
|
||||||
UsbCommand *c = (UsbCommand *)packet;
|
UsbCommand *c = (UsbCommand *)packet;
|
||||||
|
|
||||||
// Dbprintf("received %d bytes, with command: 0x%04x and args: %d %d %d",len,c->cmd,c->arg[0],c->arg[1],c->arg[2]);
|
//Dbprintf("received %d bytes, with command: 0x%04x and args: %d %d %d",len,c->cmd,c->arg[0],c->arg[1],c->arg[2]);
|
||||||
|
|
||||||
switch(c->cmd) {
|
switch(c->cmd) {
|
||||||
#ifdef WITH_LF
|
#ifdef WITH_LF
|
||||||
|
@ -846,10 +846,14 @@ void UsbPacketReceived(uint8_t *packet, int len)
|
||||||
break;
|
break;
|
||||||
// case CMD_MIFARE_DES_READER:
|
// case CMD_MIFARE_DES_READER:
|
||||||
// ReaderMifareDES(c->arg[0], c->arg[1], c->d.asBytes);
|
// ReaderMifareDES(c->arg[0], c->arg[1], c->d.asBytes);
|
||||||
break;
|
//break;
|
||||||
case CMD_MIFARE_DESFIRE_INFO:
|
case CMD_MIFARE_DESFIRE_INFO:
|
||||||
MifareDesfireGetInformation();
|
MifareDesfireGetInformation();
|
||||||
break;
|
break;
|
||||||
|
case CMD_MIFARE_DESFIRE:
|
||||||
|
MifareSendCommand(c->arg[0], c->arg[1], c->d.asBytes);
|
||||||
|
break;
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef WITH_ICLASS
|
#ifdef WITH_ICLASS
|
||||||
|
|
|
@ -209,13 +209,15 @@ void MifareCSetBlock(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datai
|
||||||
void MifareCGetBlock(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain);
|
void MifareCGetBlock(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain);
|
||||||
|
|
||||||
// mifaredesfire.h
|
// mifaredesfire.h
|
||||||
|
bool InitDesfireCard();
|
||||||
|
void MifareSendCommand(uint8_t arg0,uint8_t arg1, uint8_t *datain);
|
||||||
void MifareDesfireGetInformation();
|
void MifareDesfireGetInformation();
|
||||||
void MifareDES_Auth1(uint8_t arg0,uint8_t arg1,uint8_t arg2, uint8_t *datain);
|
void MifareDES_Auth1(uint8_t arg0,uint8_t arg1,uint8_t arg2, uint8_t *datain);
|
||||||
void MifareDES_Auth2(uint32_t arg0, uint8_t *datain);
|
void MifareDES_Auth2(uint32_t arg0, uint8_t *datain);
|
||||||
int mifare_des_auth2(uint32_t uid, uint8_t *key, uint8_t *blockData);
|
int mifare_des_auth2(uint32_t uid, uint8_t *key, uint8_t *blockData);
|
||||||
void ReaderMifareDES(uint32_t param, uint32_t param2, uint8_t * datain);
|
void ReaderMifareDES(uint32_t param, uint32_t param2, uint8_t * datain);
|
||||||
int SendDesfireCommand(enum DESFIRE_CMD desfire_cmd, uint8_t *dataout, uint8_t fromscratch);
|
int DesfireAPDU(uint8_t *cmd, size_t cmd_len, uint8_t *dataout);
|
||||||
uint8_t* CreateAPDU( uint8_t *datain, size_t len);
|
size_t CreateAPDU( uint8_t *datain, size_t len, uint8_t *dataout);
|
||||||
void OnSuccess();
|
void OnSuccess();
|
||||||
void OnError();
|
void OnError();
|
||||||
|
|
||||||
|
@ -247,8 +249,6 @@ void cmac (const desfirekey_t key, uint8_t *ivect, const uint8_t *
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/// iso15693.h
|
/// iso15693.h
|
||||||
void RecordRawAdcSamplesIso15693(void);
|
void RecordRawAdcSamplesIso15693(void);
|
||||||
void AcquireRawAdcSamplesIso15693(void);
|
void AcquireRawAdcSamplesIso15693(void);
|
||||||
|
|
|
@ -1615,13 +1615,6 @@ int ReaderReceive(uint8_t* receivedAnswer)
|
||||||
return ReaderReceiveOffset(receivedAnswer, 0);
|
return ReaderReceiveOffset(receivedAnswer, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
int ReaderReceiveDesfiresAuthTiming(uint8_t *receivedAnswer, uint32_t *elapsedTime)
|
|
||||||
{
|
|
||||||
int len = ReaderReceiveOffset(receivedAnswer, 0);
|
|
||||||
*elapsedTime = (Demod.endTime*16 - DELAY_AIR2ARM_AS_READER) - (Demod.startTime*16 - DELAY_AIR2ARM_AS_READER);
|
|
||||||
return len;
|
|
||||||
}
|
|
||||||
|
|
||||||
int ReaderReceivePar(uint8_t *receivedAnswer, uint32_t *parptr)
|
int ReaderReceivePar(uint8_t *receivedAnswer, uint32_t *parptr)
|
||||||
{
|
{
|
||||||
if (!GetIso14443aAnswerFromTag(receivedAnswer,0,160)) return FALSE;
|
if (!GetIso14443aAnswerFromTag(receivedAnswer,0,160)) return FALSE;
|
||||||
|
@ -1856,7 +1849,7 @@ void ReaderIso14443a(UsbCommand *c)
|
||||||
}
|
}
|
||||||
|
|
||||||
if(param & ISO14A_SET_TIMEOUT) {
|
if(param & ISO14A_SET_TIMEOUT) {
|
||||||
iso14a_timeout = c->arg[2];
|
iso14a_set_timeout(c->arg[2]);
|
||||||
}
|
}
|
||||||
|
|
||||||
if(param & ISO14A_APDU) {
|
if(param & ISO14A_APDU) {
|
||||||
|
|
|
@ -84,7 +84,6 @@ extern void ReaderTransmitBitsPar(uint8_t *frame, int bits, uint32_t par, uint32
|
||||||
extern void ReaderTransmitPar(uint8_t *frame, int len, uint32_t par, uint32_t *timing);
|
extern void ReaderTransmitPar(uint8_t *frame, int len, uint32_t par, uint32_t *timing);
|
||||||
extern int ReaderReceive(uint8_t *receivedAnswer);
|
extern int ReaderReceive(uint8_t *receivedAnswer);
|
||||||
extern int ReaderReceivePar(uint8_t *receivedAnswer, uint32_t *parptr);
|
extern int ReaderReceivePar(uint8_t *receivedAnswer, uint32_t *parptr);
|
||||||
extern int ReaderReceiveDesfiresAuthTiming(uint8_t *receivedAnswer, uint32_t *elapsedTime);
|
|
||||||
|
|
||||||
extern void iso14443a_setup(uint8_t fpga_minor_mode);
|
extern void iso14443a_setup(uint8_t fpga_minor_mode);
|
||||||
extern int iso14_apdu(uint8_t *cmd, size_t cmd_len, void *data);
|
extern int iso14_apdu(uint8_t *cmd, size_t cmd_len, void *data);
|
||||||
|
|
|
@ -107,9 +107,6 @@ void MifareUC_Auth1(uint8_t arg0, uint8_t *datain){
|
||||||
if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card, something went wrong before auth");
|
if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card, something went wrong before auth");
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if(mifare_ultra_auth1(cuid, dataoutbuf)){
|
if(mifare_ultra_auth1(cuid, dataoutbuf)){
|
||||||
if (MF_DBGLEVEL >= 1) Dbprintf("Authentication part1: Fail.");
|
if (MF_DBGLEVEL >= 1) Dbprintf("Authentication part1: Fail.");
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,21 +6,87 @@
|
||||||
#define NOT_YET_AUTHENTICATED 255
|
#define NOT_YET_AUTHENTICATED 255
|
||||||
#define FRAME_PAYLOAD_SIZE (MAX_FRAME_SIZE - 5)
|
#define FRAME_PAYLOAD_SIZE (MAX_FRAME_SIZE - 5)
|
||||||
|
|
||||||
|
// the block number for the ISO14443-4 PCB
|
||||||
|
uint8_t pcb_blocknum = 0;
|
||||||
|
// Deselect card by sending a s-block. the crc is precalced for speed
|
||||||
|
static uint8_t deselect_cmd[] = {0xc2,0xe0,0xb4};
|
||||||
|
|
||||||
//static uint8_t __msg[MAX_FRAME_SIZE] = { 0x0A, 0x00, 0x00, /* ..., */ 0x00 };
|
//static uint8_t __msg[MAX_FRAME_SIZE] = { 0x0A, 0x00, 0x00, /* ..., */ 0x00 };
|
||||||
/* PCB CID CMD PAYLOAD */
|
/* PCB CID CMD PAYLOAD */
|
||||||
//static uint8_t __res[MAX_FRAME_SIZE];
|
//static uint8_t __res[MAX_FRAME_SIZE];
|
||||||
|
|
||||||
|
bool InitDesfireCard(){
|
||||||
|
|
||||||
|
// Make sure it is off.
|
||||||
|
// FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
|
||||||
|
// SpinDelay(300);
|
||||||
|
|
||||||
|
byte_t cardbuf[USB_CMD_DATA_SIZE];
|
||||||
|
memset(cardbuf,0,sizeof(cardbuf));
|
||||||
|
|
||||||
|
iso14a_set_tracing(TRUE);
|
||||||
|
iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);
|
||||||
|
|
||||||
|
iso14a_card_select_t *card = (iso14a_card_select_t*)cardbuf;
|
||||||
|
int len = iso14443a_select_card(NULL,card,NULL);
|
||||||
|
|
||||||
|
if (!len) {
|
||||||
|
if (MF_DBGLEVEL >= 1) {
|
||||||
|
Dbprintf("Can't select card");
|
||||||
|
}
|
||||||
|
OnError();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void MifareSendCommand(uint8_t arg0, uint8_t arg1, uint8_t *datain){
|
||||||
|
|
||||||
|
/* ARG0 contains flags.
|
||||||
|
0x01 = init card.
|
||||||
|
0x02 =
|
||||||
|
0x03
|
||||||
|
*/
|
||||||
|
uint8_t flags = arg0;
|
||||||
|
size_t datalen = arg1;
|
||||||
|
uint8_t resp[RECV_RES_SIZE];
|
||||||
|
memset(resp,0,sizeof(resp));
|
||||||
|
|
||||||
|
if (MF_DBGLEVEL >= 4) {
|
||||||
|
Dbprintf(" flags: %02X", flags);
|
||||||
|
Dbprintf(" len : %02X", datalen);
|
||||||
|
print_result("to send: ", datain, datalen);
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( flags & 0x01 ){
|
||||||
|
if ( !InitDesfireCard() )
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
int len = DesfireAPDU(datain, datalen, resp);
|
||||||
|
if ( !len ) {
|
||||||
|
if (MF_DBGLEVEL >= 4) {
|
||||||
|
print_result("ERR <--: ", resp, len);
|
||||||
|
}
|
||||||
|
OnError();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
cmd_send(CMD_ACK,1,0,0,resp,len);
|
||||||
|
|
||||||
|
|
||||||
|
OnSuccess();
|
||||||
|
}
|
||||||
|
|
||||||
void MifareDesfireGetInformation(){
|
void MifareDesfireGetInformation(){
|
||||||
|
|
||||||
|
int len = 0;
|
||||||
uint8_t len = 0;
|
uint8_t resp[USB_CMD_DATA_SIZE];
|
||||||
uint8_t resp[RECV_RES_SIZE];
|
uint8_t dataout[USB_CMD_DATA_SIZE];
|
||||||
uint8_t dataout[RECV_CMD_SIZE];
|
byte_t cardbuf[USB_CMD_DATA_SIZE];
|
||||||
byte_t buf[RECV_RES_SIZE];
|
|
||||||
|
|
||||||
memset(resp,0,sizeof(resp));
|
memset(resp,0,sizeof(resp));
|
||||||
memset(dataout,0, sizeof(dataout));
|
memset(dataout,0, sizeof(dataout));
|
||||||
memset(buf,0,sizeof(buf));
|
memset(cardbuf,0,sizeof(cardbuf));
|
||||||
|
|
||||||
/*
|
/*
|
||||||
1 = PCB 1
|
1 = PCB 1
|
||||||
|
@ -28,20 +94,15 @@ void MifareDesfireGetInformation(){
|
||||||
3 = desfire command 3
|
3 = desfire command 3
|
||||||
4-5 = crc 4 key
|
4-5 = crc 4 key
|
||||||
5-6 crc
|
5-6 crc
|
||||||
|
|
||||||
PCB == 0x0A because sending CID byte.
|
PCB == 0x0A because sending CID byte.
|
||||||
CID == 0x00 first card?
|
CID == 0x00 first card?
|
||||||
|
|
||||||
*/
|
*/
|
||||||
uint8_t cmd1[] = {0x0a,0x00,GET_VERSION, 0x00, 0x00 };
|
|
||||||
uint8_t cmd2[] = {0x0a,0x00,GET_KEY_VERSION, 0x00, 0x00, 0x00 };
|
|
||||||
|
|
||||||
iso14a_clear_trace();
|
iso14a_clear_trace();
|
||||||
iso14a_set_tracing(TRUE);
|
iso14a_set_tracing(TRUE);
|
||||||
iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);
|
iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);
|
||||||
|
|
||||||
// card select - information
|
// card select - information
|
||||||
iso14a_card_select_t *card = (iso14a_card_select_t*)buf;
|
iso14a_card_select_t *card = (iso14a_card_select_t*)cardbuf;
|
||||||
byte_t isOK = iso14443a_select_card(NULL, card, NULL);
|
byte_t isOK = iso14443a_select_card(NULL, card, NULL);
|
||||||
if (isOK != 1) {
|
if (isOK != 1) {
|
||||||
if (MF_DBGLEVEL >= 1) {
|
if (MF_DBGLEVEL >= 1) {
|
||||||
|
@ -51,45 +112,42 @@ void MifareDesfireGetInformation(){
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
memcpy(dataout,card->uid,7);
|
memcpy(dataout,card->uid,7);
|
||||||
|
|
||||||
LED_A_ON();
|
LED_A_ON();
|
||||||
LED_B_OFF();
|
LED_B_OFF();
|
||||||
LED_C_OFF();
|
LED_C_OFF();
|
||||||
|
|
||||||
// GET INFORMATION
|
uint8_t cmd[] = {GET_VERSION};
|
||||||
AppendCrc14443a(cmd1, 3);
|
size_t cmd_len = sizeof(cmd);
|
||||||
ReaderTransmit(cmd1, sizeof(cmd1), NULL);
|
|
||||||
len = ReaderReceive(resp);
|
len = DesfireAPDU(cmd, cmd_len, resp);
|
||||||
if ( resp[2] != ADDITIONAL_FRAME) {
|
if ( !len ) {
|
||||||
print_result("ERROR <--: ", resp, len);
|
print_result("ERROR <--: ", resp, len);
|
||||||
OnError();
|
OnError();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
LED_A_OFF();
|
||||||
|
LED_B_ON();
|
||||||
memcpy(dataout+7,resp+3,7);
|
memcpy(dataout+7,resp+3,7);
|
||||||
|
|
||||||
// ADDITION_FRAME 1
|
// ADDITION_FRAME 1
|
||||||
++cmd1[0];
|
cmd[0] = ADDITIONAL_FRAME;
|
||||||
cmd1[2] = ADDITIONAL_FRAME;
|
len = DesfireAPDU(cmd, cmd_len, resp);
|
||||||
AppendCrc14443a(cmd1, 3);
|
if ( !len ) {
|
||||||
ReaderTransmit(cmd1, sizeof(cmd1), NULL);
|
|
||||||
len = ReaderReceive(resp);
|
|
||||||
|
|
||||||
if ( resp[2] != ADDITIONAL_FRAME) {
|
|
||||||
print_result("ERROR <--: ", resp, len);
|
print_result("ERROR <--: ", resp, len);
|
||||||
OnError();
|
OnError();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
LED_B_OFF();
|
||||||
|
LED_C_ON();
|
||||||
memcpy(dataout+7+7,resp+3,7);
|
memcpy(dataout+7+7,resp+3,7);
|
||||||
|
|
||||||
// ADDITION_FRAME 2
|
// ADDITION_FRAME 2
|
||||||
--cmd1[0];
|
len = DesfireAPDU(cmd, cmd_len, resp);
|
||||||
AppendCrc14443a(cmd1, 3);
|
if ( !len ) {
|
||||||
ReaderTransmit(cmd1, sizeof(cmd1), NULL);
|
|
||||||
len = ReaderReceive(resp);
|
|
||||||
if ( resp[2] != OPERATION_OK) {
|
|
||||||
print_result("ERROR <--: ", resp, len);
|
print_result("ERROR <--: ", resp, len);
|
||||||
OnError();
|
OnError();
|
||||||
return;
|
return;
|
||||||
|
@ -97,34 +155,10 @@ void MifareDesfireGetInformation(){
|
||||||
|
|
||||||
memcpy(dataout+7+7+7,resp+3,14);
|
memcpy(dataout+7+7+7,resp+3,14);
|
||||||
|
|
||||||
// GET MASTER KEYSETTINGS
|
|
||||||
cmd1[2] = GET_KEY_SETTINGS;
|
|
||||||
AppendCrc14443a(cmd1, 3);
|
|
||||||
ReaderTransmit(cmd1, sizeof(cmd1), NULL);
|
|
||||||
len = ReaderReceive(resp);
|
|
||||||
if (len){
|
|
||||||
memcpy(dataout+7+7+7+14,resp+3,2);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// GET MASTER KEY VERSION
|
|
||||||
AppendCrc14443a(cmd2, 4);
|
|
||||||
ReaderTransmit(cmd2, sizeof(cmd2), NULL);
|
|
||||||
len = ReaderReceive(resp);
|
|
||||||
if (len){
|
|
||||||
memcpy(dataout+7+7+7+14+2,resp+3,1);
|
|
||||||
}
|
|
||||||
|
|
||||||
// GET FREE MEMORY
|
|
||||||
cmd1[2] = GET_FREE_MEMORY;
|
|
||||||
AppendCrc14443a(cmd1, 3);
|
|
||||||
ReaderTransmit(cmd1, sizeof(cmd1), NULL);
|
|
||||||
len = ReaderReceive(resp);
|
|
||||||
if (len){
|
|
||||||
memcpy(dataout+7+7+7+14+2+1,resp+3,3);
|
|
||||||
}
|
|
||||||
|
|
||||||
cmd_send(CMD_ACK,1,0,0,dataout,sizeof(dataout));
|
cmd_send(CMD_ACK,1,0,0,dataout,sizeof(dataout));
|
||||||
|
|
||||||
|
// reset the pcb_blocknum,
|
||||||
|
pcb_blocknum = 0;
|
||||||
OnSuccess();
|
OnSuccess();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -309,49 +343,58 @@ void MifareDES_Auth1(uint8_t mode, uint8_t algo, uint8_t keyno, uint8_t *datain
|
||||||
OnSuccess(resp);
|
OnSuccess(resp);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 3 olika ISO sätt att skicka data till DESFIRE (direkt, inkapslat, inkapslat ISO)
|
||||||
|
// cmd = cmd bytes to send
|
||||||
|
// cmd_len = length of cmd
|
||||||
|
// dataout = pointer to response data array
|
||||||
|
int DesfireAPDU(uint8_t *cmd, size_t cmd_len, uint8_t *dataout){
|
||||||
|
|
||||||
// desfire_cmd = enum DESFIRE_CMD in desfire.h
|
uint32_t status = 0;
|
||||||
// cmd = pointer to
|
size_t wrappedLen = 0;
|
||||||
// dataout = point to array for response data.
|
uint8_t wCmd[USB_CMD_DATA_SIZE];
|
||||||
int SendDesfireCommand(enum DESFIRE_CMD desfire_cmd,uint8_t *dataout, uint8_t fromscratch){
|
|
||||||
|
|
||||||
uint8_t resp[80];
|
wrappedLen = CreateAPDU( cmd, cmd_len, wCmd);
|
||||||
uint8_t len;
|
|
||||||
|
|
||||||
if ( fromscratch){
|
if (MF_DBGLEVEL >= 4) {
|
||||||
|
print_result("WCMD <--: ", wCmd, wrappedLen);
|
||||||
FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
|
|
||||||
|
|
||||||
// power up the field
|
|
||||||
iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);
|
|
||||||
// select the card
|
|
||||||
iso14443a_select_card(NULL, NULL, NULL);
|
|
||||||
}
|
}
|
||||||
|
ReaderTransmit( wCmd, wrappedLen, NULL);
|
||||||
|
|
||||||
// 3 olika ISO sätt att skicka data till DESFIRE (direkt, inkapslat, inkapslat ISO)
|
status = ReaderReceive(dataout);
|
||||||
uint8_t real_cmd[4];
|
|
||||||
real_cmd[0] = 0x02;
|
|
||||||
real_cmd[1] = desfire_cmd;
|
|
||||||
AppendCrc14443a(real_cmd, 2);
|
|
||||||
ReaderTransmit(real_cmd, sizeof(real_cmd), NULL);
|
|
||||||
len = ReaderReceive(resp);
|
|
||||||
if(!len)
|
|
||||||
return -1; //DATA LINK ERROR
|
|
||||||
|
|
||||||
if ( fromscratch){
|
if(!status){
|
||||||
FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
|
return FALSE; //DATA LINK ERROR
|
||||||
}
|
}
|
||||||
|
// if we received an I- or R(ACK)-Block with a block number equal to the
|
||||||
|
// current block number, toggle the current block number
|
||||||
|
else if (status >= 4 // PCB+CID+CRC = 4 bytes
|
||||||
|
&& ((dataout[0] & 0xC0) == 0 // I-Block
|
||||||
|
|| (dataout[0] & 0xD0) == 0x80) // R-Block with ACK bit set to 0
|
||||||
|
&& (dataout[0] & 0x01) == pcb_blocknum) // equal block numbers
|
||||||
|
{
|
||||||
|
pcb_blocknum ^= 1; //toggle next block
|
||||||
|
}
|
||||||
|
return status;
|
||||||
|
}
|
||||||
|
|
||||||
enum DESFIRE_STATUS status = resp[1];
|
// CreateAPDU
|
||||||
//1 bytes iso, 1 byte status, in the end: 2 bytes crc
|
size_t CreateAPDU( uint8_t *datain, size_t len, uint8_t *dataout){
|
||||||
if ( status == OPERATION_OK || status == ADDITIONAL_FRAME) {
|
|
||||||
memcpy(dataout, resp+2, 2);
|
size_t cmdlen = MIN(len+4, USB_CMD_DATA_SIZE-1);
|
||||||
return len;
|
|
||||||
}
|
uint8_t cmd[cmdlen];
|
||||||
else {
|
memset(cmd, 0, cmdlen);
|
||||||
Dbprintf("unexpected desfire response: %X (to %X)", status, desfire_cmd);
|
|
||||||
return -status;
|
cmd[0] = 0x0A; // 0x0A = skicka cid, 0x02 = ingen cid. Särskilda bitar //
|
||||||
}
|
cmd[0] |= pcb_blocknum; // OR the block number into the PCB
|
||||||
|
cmd[1] = 0x00; // CID: 0x00 //FIXME: allow multiple selected cards
|
||||||
|
|
||||||
|
memcpy(cmd+2, datain, len);
|
||||||
|
AppendCrc14443a(cmd, len+2);
|
||||||
|
|
||||||
|
memcpy(dataout, cmd, cmdlen);
|
||||||
|
|
||||||
|
return cmdlen;
|
||||||
}
|
}
|
||||||
|
|
||||||
// crc_update(&desfire_crc32, 0, 1); /* CMD_WRITE */
|
// crc_update(&desfire_crc32, 0, 1); /* CMD_WRITE */
|
||||||
|
@ -512,56 +555,20 @@ void MifareDES_Auth2(uint32_t arg0, uint8_t *datain){
|
||||||
LEDsoff();
|
LEDsoff();
|
||||||
}
|
}
|
||||||
|
|
||||||
// CreateAPDU
|
|
||||||
uint8_t* CreateAPDU( uint8_t *datain, size_t len){
|
|
||||||
|
|
||||||
len = MIN(len, USB_CMD_DATA_SIZE);
|
|
||||||
|
|
||||||
uint8_t tmpcmd[len];
|
|
||||||
uint8_t *cmd = tmpcmd;
|
|
||||||
memset(cmd, 0, len);
|
|
||||||
cmd[0] = 0x0a;
|
|
||||||
cmd[1] = 0x00;
|
|
||||||
|
|
||||||
memcpy(cmd, datain,len);
|
|
||||||
AppendCrc14443a(cmd, len+2);
|
|
||||||
return cmd;
|
|
||||||
}
|
|
||||||
|
|
||||||
void SelectCard(){
|
|
||||||
|
|
||||||
uint8_t resp[RECV_RES_SIZE];
|
|
||||||
byte_t buf[RECV_RES_SIZE];
|
|
||||||
|
|
||||||
memset(resp,0,sizeof(resp));
|
|
||||||
memset(buf,0,sizeof(buf));
|
|
||||||
|
|
||||||
iso14a_clear_trace();
|
|
||||||
iso14a_set_tracing(TRUE);
|
|
||||||
iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);
|
|
||||||
|
|
||||||
// card select - information
|
|
||||||
iso14a_card_select_t *card = (iso14a_card_select_t*)buf;
|
|
||||||
byte_t isOK = iso14443a_select_card(NULL, card, NULL);
|
|
||||||
if (isOK != 1) {
|
|
||||||
if (MF_DBGLEVEL >= 1) {
|
|
||||||
Dbprintf("Can't select card");
|
|
||||||
}
|
|
||||||
OnError();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void OnSuccess(){
|
void OnSuccess(){
|
||||||
// Deselect card by sending a s-block. the crc is precalced for speed
|
// transmit a DESELECT COMMAND for Desfire.
|
||||||
uint8_t cmd[] = {0xc2,0xe0,0xb4};
|
ReaderTransmit(deselect_cmd, 3 , NULL);
|
||||||
ReaderTransmit(cmd, sizeof(cmd), NULL);
|
// reset the pcb_blocknum,
|
||||||
|
pcb_blocknum = 0;
|
||||||
FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
|
FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
|
||||||
LEDsoff();
|
LEDsoff();
|
||||||
}
|
}
|
||||||
|
|
||||||
void OnError(){
|
void OnError(){
|
||||||
cmd_send(CMD_ACK,0,0,0,0,0);
|
cmd_send(CMD_ACK,0,0,0,0,0);
|
||||||
|
ReaderTransmit(deselect_cmd, 3 , NULL);
|
||||||
|
// reset the pcb_blocknum,
|
||||||
|
pcb_blocknum = 0;
|
||||||
FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
|
FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
|
||||||
LEDsoff();
|
LEDsoff();
|
||||||
}
|
}
|
||||||
|
|
|
@ -134,18 +134,19 @@ int CmdHF14ADesRb(const char *Cmd)
|
||||||
|
|
||||||
int CmdHF14ADesInfo(const char *Cmd){
|
int CmdHF14ADesInfo(const char *Cmd){
|
||||||
|
|
||||||
UsbCommand c = {CMD_MIFARE_DESFIRE_INFO, { 0x00 }};
|
UsbCommand c = {CMD_MIFARE_DESFIRE_INFO};
|
||||||
|
|
||||||
SendCommand(&c);
|
SendCommand(&c);
|
||||||
UsbCommand resp;
|
UsbCommand resp;
|
||||||
|
|
||||||
if (WaitForResponseTimeout(CMD_ACK,&resp,1500)) {
|
if ( !WaitForResponseTimeout(CMD_ACK,&resp,1500) ) {
|
||||||
uint8_t isOK = resp.arg[0] & 0xff;
|
|
||||||
PrintAndLog("isOk:%02x", isOK);
|
|
||||||
} else {
|
|
||||||
PrintAndLog("Command execute timeout");
|
PrintAndLog("Command execute timeout");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
uint8_t isOK = resp.arg[0] & 0xff;
|
||||||
|
if ( !isOK ){
|
||||||
|
PrintAndLog("Command unsuccessfull");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
PrintAndLog("---Desfire Information---------------------------------------");
|
PrintAndLog("---Desfire Information---------------------------------------");
|
||||||
PrintAndLog("-------------------------------------------------------------");
|
PrintAndLog("-------------------------------------------------------------");
|
||||||
|
@ -170,34 +171,59 @@ int CmdHF14ADesInfo(const char *Cmd){
|
||||||
PrintAndLog(" Protocol : %s", GetProtocolStr(resp.d.asBytes[20]));
|
PrintAndLog(" Protocol : %s", GetProtocolStr(resp.d.asBytes[20]));
|
||||||
PrintAndLog("-------------------------------------------------------------");
|
PrintAndLog("-------------------------------------------------------------");
|
||||||
|
|
||||||
|
|
||||||
|
UsbCommand c1 = {CMD_MIFARE_DESFIRE, { 0x01, 0x01 }};
|
||||||
|
c1.d.asBytes[0] = GET_KEY_SETTINGS;
|
||||||
|
SendCommand(&c1);
|
||||||
|
if ( !WaitForResponseTimeout(CMD_ACK,&resp,1500) ) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
PrintAndLog(" Master Key settings");
|
PrintAndLog(" Master Key settings");
|
||||||
if ( resp.d.asBytes[35] & (1 << 3 ) )
|
if ( resp.d.asBytes[3] & (1 << 3 ) )
|
||||||
PrintAndLog(" 0x08 Configuration changeable;");
|
PrintAndLog(" 0x08 Configuration changeable;");
|
||||||
else
|
else
|
||||||
PrintAndLog(" 0x08 Configuration NOT changeable;");
|
PrintAndLog(" 0x08 Configuration NOT changeable;");
|
||||||
|
|
||||||
if ( resp.d.asBytes[35] & (1 << 2 ) )
|
if ( resp.d.asBytes[3] & (1 << 2 ) )
|
||||||
PrintAndLog(" 0x04 PICC Master Key not required for create / delete;");
|
PrintAndLog(" 0x04 PICC Master Key not required for create / delete;");
|
||||||
else
|
else
|
||||||
PrintAndLog(" 0x04 PICC Master Key required for create / delete;");
|
PrintAndLog(" 0x04 PICC Master Key required for create / delete;");
|
||||||
|
|
||||||
if ( resp.d.asBytes[35] & (1 << 1 ) )
|
if ( resp.d.asBytes[3] & (1 << 1 ) )
|
||||||
PrintAndLog(" 0x02 Free directory list access without PICC Master Key;");
|
PrintAndLog(" 0x02 Free directory list access without PICC Master Key;");
|
||||||
else
|
else
|
||||||
PrintAndLog(" 0x02 Directory list access with PICC Master Key;");
|
PrintAndLog(" 0x02 Directory list access with PICC Master Key;");
|
||||||
|
|
||||||
if ( resp.d.asBytes[35] & (1 << 0 ) )
|
if ( resp.d.asBytes[3] & (1 << 0 ) )
|
||||||
PrintAndLog(" 0x01 Allow changing the Master Key;");
|
PrintAndLog(" 0x01 Allow changing the Master Key;");
|
||||||
else
|
else
|
||||||
PrintAndLog(" 0x01 Master Key is not changeable anymore;");
|
PrintAndLog(" 0x01 Master Key is not changeable anymore;");
|
||||||
|
|
||||||
|
// init len
|
||||||
|
UsbCommand c2 = {CMD_MIFARE_DESFIRE, { 0x01, 0x02 }};
|
||||||
|
c2.d.asBytes[0] = GET_KEY_VERSION;
|
||||||
|
c2.d.asBytes[1] = 0x00;
|
||||||
|
SendCommand(&c2);
|
||||||
|
if ( !WaitForResponseTimeout(CMD_ACK,&resp,1500) ) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
PrintAndLog("");
|
PrintAndLog("");
|
||||||
PrintAndLog(" Max number of keys : %d", resp.d.asBytes[36]);
|
PrintAndLog(" Max number of keys : %d", resp.d.asBytes[2]);
|
||||||
PrintAndLog(" Master key Version : %d (0x%02x)", resp.d.asBytes[37], resp.d.asBytes[37]);
|
PrintAndLog(" Master key Version : %d (0x%02x)", resp.d.asBytes[3], resp.d.asBytes[3]);
|
||||||
PrintAndLog("-------------------------------------------------------------");
|
PrintAndLog("-------------------------------------------------------------");
|
||||||
|
|
||||||
|
|
||||||
|
UsbCommand c3 = {CMD_MIFARE_DESFIRE, { 0x01, 0x01 }};
|
||||||
|
c3.d.asBytes[0] = GET_FREE_MEMORY;
|
||||||
|
SendCommand(&c3);
|
||||||
|
if ( !WaitForResponseTimeout(CMD_ACK,&resp,1500)) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
uint8_t tmp[3];
|
uint8_t tmp[3];
|
||||||
memcpy(tmp, resp.d.asBytes+38,3);
|
memcpy(tmp, resp.d.asBytes+3,3);
|
||||||
|
|
||||||
PrintAndLog(" Free memory on card : %d bytes", le24toh( tmp ));
|
PrintAndLog(" Free memory on card : %d bytes", le24toh( tmp ));
|
||||||
PrintAndLog("-------------------------------------------------------------");
|
PrintAndLog("-------------------------------------------------------------");
|
||||||
|
@ -226,7 +252,6 @@ int CmdHF14ADesInfo(const char *Cmd){
|
||||||
AES 16 : RndA(byte0-byte3) + RndB(byte0-byte3) + RndA(byte12-byte15) + RndB(byte12-byte15)
|
AES 16 : RndA(byte0-byte3) + RndB(byte0-byte3) + RndA(byte12-byte15) + RndB(byte12-byte15)
|
||||||
*/
|
*/
|
||||||
|
|
||||||
PrintAndLog(" RX :%s",sprint_hex(resp.d.asBytes, 40));
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -434,3 +459,5 @@ int CmdHelp(const char *Cmd)
|
||||||
CmdsHelp(CommandTable);
|
CmdsHelp(CommandTable);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -18,3 +18,41 @@ int CmdHF14ADesNonces(const char *Cmd);
|
||||||
char * GetCardSizeStr( uint8_t fsize );
|
char * GetCardSizeStr( uint8_t fsize );
|
||||||
char * GetVendorStr( uint8_t id);
|
char * GetVendorStr( uint8_t id);
|
||||||
char * GetProtocolStr(uint8_t id);
|
char * GetProtocolStr(uint8_t id);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#define CREATE_APPLICATION 0xca
|
||||||
|
#define DELETE_APPLICATION 0xda
|
||||||
|
#define GET_APPLICATION_IDS 0x6a
|
||||||
|
#define SELECT_APPLICATION 0x5a
|
||||||
|
#define FORMAT_PICC 0xfc
|
||||||
|
#define GET_VERSION 0x60
|
||||||
|
#define READ_DATA 0xbd
|
||||||
|
#define WRITE_DATA 0x3d
|
||||||
|
#define GET_VALUE 0x6c
|
||||||
|
#define CREDIT 0x0c
|
||||||
|
#define DEBIT 0xdc
|
||||||
|
#define LIMITED_CREDIT 0x1c
|
||||||
|
#define WRITE_RECORD 0x3b
|
||||||
|
#define READ_RECORDS 0xbb
|
||||||
|
#define CLEAR_RECORD_FILE 0xeb
|
||||||
|
#define COMMIT_TRANSACTION 0xc7
|
||||||
|
#define ABORT_TRANSACTION 0xa7
|
||||||
|
#define GET_FREE_MEMORY 0x6e
|
||||||
|
#define GET_FILE_IDS 0x6f
|
||||||
|
#define GET_FILE_SETTINGS 0xf5
|
||||||
|
#define CHANGE_FILE_SETTINGS 0x5f
|
||||||
|
#define CREATE_STD_DATA_FILE 0xcd
|
||||||
|
#define CREATE_BACKUP_DATA_FILE 0xcb
|
||||||
|
#define CREATE_VALUE_FILE 0xcc
|
||||||
|
#define CREATE_LINEAR_RECORD_FILE 0xc1
|
||||||
|
#define CREATE_CYCLIC_RECORD_FILE 0xc0
|
||||||
|
#define DELETE_FILE 0xdf
|
||||||
|
#define AUTHENTICATE 0x0a // AUTHENTICATE_NATIVE
|
||||||
|
#define AUTHENTICATE_ISO 0x1a // AUTHENTICATE_STANDARD
|
||||||
|
#define AUTHENTICATE_AES 0xaa
|
||||||
|
#define CHANGE_KEY_SETTINGS 0x54
|
||||||
|
#define GET_KEY_SETTINGS 0x45
|
||||||
|
#define CHANGE_KEY 0xc4
|
||||||
|
#define GET_KEY_VERSION 0x64
|
||||||
|
#define AUTHENTICATION_FRAME 0xAF
|
|
@ -11,8 +11,8 @@
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
int fileExists(const char *filename) {
|
int fileExists(const char *filename) {
|
||||||
struct _stat st;
|
struct _stat fileStat;
|
||||||
int result = stat(filename, &st);
|
int result = _stat(filename, &fileStat);
|
||||||
return result == 0;
|
return result == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -145,10 +145,13 @@ typedef struct {
|
||||||
|
|
||||||
#define CMD_MIFARE_READBL 0x0620
|
#define CMD_MIFARE_READBL 0x0620
|
||||||
#define CMD_MIFAREU_READBL 0x0720
|
#define CMD_MIFAREU_READBL 0x0720
|
||||||
|
|
||||||
#define CMD_MIFARE_READSC 0x0621
|
#define CMD_MIFARE_READSC 0x0621
|
||||||
#define CMD_MIFAREU_READCARD 0x0721
|
#define CMD_MIFAREU_READCARD 0x0721
|
||||||
|
|
||||||
#define CMD_MIFARE_WRITEBL 0x0622
|
#define CMD_MIFARE_WRITEBL 0x0622
|
||||||
#define CMD_MIFAREU_WRITEBL_COMPAT 0x0722
|
#define CMD_MIFAREU_WRITEBL_COMPAT 0x0722
|
||||||
|
|
||||||
#define CMD_MIFAREU_WRITEBL 0x0723
|
#define CMD_MIFAREU_WRITEBL 0x0723
|
||||||
#define CMD_MIFARE_CHKKEYS 0x0623
|
#define CMD_MIFARE_CHKKEYS 0x0623
|
||||||
|
|
||||||
|
@ -158,7 +161,6 @@ typedef struct {
|
||||||
#define CMD_MIFAREUC_AUTH2 0x0725
|
#define CMD_MIFAREUC_AUTH2 0x0725
|
||||||
#define CMD_MIFAREUC_READCARD 0x0726
|
#define CMD_MIFAREUC_READCARD 0x0726
|
||||||
|
|
||||||
|
|
||||||
// mifare desfire
|
// mifare desfire
|
||||||
#define CMD_MIFARE_DESFIRE_READBL 0x0728
|
#define CMD_MIFARE_DESFIRE_READBL 0x0728
|
||||||
#define CMD_MIFARE_DESFIRE_WRITEBL 0x0729
|
#define CMD_MIFARE_DESFIRE_WRITEBL 0x0729
|
||||||
|
@ -166,6 +168,7 @@ typedef struct {
|
||||||
#define CMD_MIFARE_DESFIRE_AUTH2 0x072b
|
#define CMD_MIFARE_DESFIRE_AUTH2 0x072b
|
||||||
#define CMD_MIFARE_DES_READER 0x072c
|
#define CMD_MIFARE_DES_READER 0x072c
|
||||||
#define CMD_MIFARE_DESFIRE_INFO 0x072d
|
#define CMD_MIFARE_DESFIRE_INFO 0x072d
|
||||||
|
#define CMD_MIFARE_DESFIRE 0x072e
|
||||||
|
|
||||||
#define CMD_UNKNOWN 0xFFFF
|
#define CMD_UNKNOWN 0xFFFF
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue