2014-09-12 05:23:46 +08:00
|
|
|
#include "mifaredesfire.h"
|
|
|
|
|
2019-08-08 22:57:33 +08:00
|
|
|
#include "common.h"
|
|
|
|
#include "proxmark3_arm.h"
|
|
|
|
#include "string.h"
|
|
|
|
#include "BigBuf.h"
|
|
|
|
#include "mifareutil.h"
|
2020-04-12 23:39:11 +08:00
|
|
|
#include "desfire_crypto.h"
|
2019-08-08 22:57:33 +08:00
|
|
|
#include "cmd.h"
|
|
|
|
#include "dbprint.h"
|
|
|
|
#include "fpgaloader.h"
|
|
|
|
#include "iso14443a.h"
|
|
|
|
#include "crc16.h"
|
|
|
|
#include "commonutil.h"
|
2020-03-17 04:51:45 +08:00
|
|
|
#include "util.h"
|
2020-03-17 18:37:38 +08:00
|
|
|
#include "mifare.h"
|
2020-04-10 05:30:49 +08:00
|
|
|
#include "ticks.h"
|
2020-04-12 23:59:46 +08:00
|
|
|
#include "protocols.h"
|
2019-08-08 22:57:33 +08:00
|
|
|
|
2014-09-12 05:23:46 +08:00
|
|
|
#define MAX_APPLICATION_COUNT 28
|
|
|
|
#define MAX_FILE_COUNT 16
|
2014-12-18 03:33:21 +08:00
|
|
|
#define MAX_DESFIRE_FRAME_SIZE 60
|
2014-09-12 05:23:46 +08:00
|
|
|
#define NOT_YET_AUTHENTICATED 255
|
2014-12-18 03:33:21 +08:00
|
|
|
#define FRAME_PAYLOAD_SIZE (MAX_DESFIRE_FRAME_SIZE - 5)
|
|
|
|
#define RECEIVE_SIZE 64
|
2014-09-12 05:23:46 +08:00
|
|
|
|
2014-09-18 18:38:31 +08:00
|
|
|
// 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
|
2019-03-10 07:00:59 +08:00
|
|
|
static uint8_t deselect_cmd[] = {0xc2, 0xe0, 0xb4};
|
2014-09-18 18:38:31 +08:00
|
|
|
|
2014-09-12 05:23:46 +08:00
|
|
|
//static uint8_t __msg[MAX_FRAME_SIZE] = { 0x0A, 0x00, 0x00, /* ..., */ 0x00 };
|
|
|
|
/* PCB CID CMD PAYLOAD */
|
|
|
|
//static uint8_t __res[MAX_FRAME_SIZE];
|
|
|
|
|
2020-04-12 23:59:46 +08:00
|
|
|
struct desfire_key skey = {0};
|
|
|
|
static desfirekey_t sessionkey = &skey;
|
2020-03-17 08:50:27 +08:00
|
|
|
|
2019-03-10 18:20:22 +08:00
|
|
|
bool InitDesfireCard() {
|
2015-01-07 00:43:31 +08:00
|
|
|
|
2020-03-17 18:37:38 +08:00
|
|
|
pcb_blocknum = 0;
|
|
|
|
|
2019-03-10 03:34:41 +08:00
|
|
|
iso14a_card_select_t card;
|
2019-03-09 15:59:13 +08:00
|
|
|
|
2019-03-10 03:34:41 +08:00
|
|
|
iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);
|
|
|
|
set_tracing(true);
|
2015-06-30 04:36:55 +08:00
|
|
|
|
2019-03-10 03:34:41 +08:00
|
|
|
if (!iso14443a_select_card(NULL, &card, NULL, true, 0, false)) {
|
2019-06-06 16:05:09 +08:00
|
|
|
if (DBGLEVEL >= DBG_ERROR) DbpString("Can't select card");
|
2019-03-10 03:34:41 +08:00
|
|
|
OnError(1);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2014-09-18 18:38:31 +08:00
|
|
|
}
|
|
|
|
|
2020-04-11 17:32:31 +08:00
|
|
|
typedef struct {
|
|
|
|
uint8_t len;
|
|
|
|
uint8_t data[RECEIVE_SIZE];
|
|
|
|
} cmdres_t;
|
|
|
|
|
2020-04-11 04:52:16 +08:00
|
|
|
void MifareSendCommand(uint8_t *datain) {
|
|
|
|
struct p {
|
|
|
|
uint8_t flags;
|
|
|
|
uint8_t datalen;
|
|
|
|
uint8_t datain[FRAME_PAYLOAD_SIZE];
|
|
|
|
} PACKED;
|
|
|
|
struct p *payload = (struct p *) datain;
|
2019-03-09 15:59:13 +08:00
|
|
|
|
2019-03-10 03:34:41 +08:00
|
|
|
uint8_t resp[RECEIVE_SIZE];
|
2019-03-10 07:00:59 +08:00
|
|
|
memset(resp, 0, sizeof(resp));
|
2019-03-10 03:34:41 +08:00
|
|
|
|
2020-03-17 18:37:38 +08:00
|
|
|
if (DBGLEVEL >= DBG_EXTENDED) {
|
2020-04-11 04:52:16 +08:00
|
|
|
Dbprintf(" flags : %02X", payload->flags);
|
|
|
|
Dbprintf(" len : %02X", payload->datalen);
|
|
|
|
print_result(" RX : ", payload->datain, payload->datalen);
|
2019-03-10 03:34:41 +08:00
|
|
|
}
|
|
|
|
|
2020-04-11 04:52:16 +08:00
|
|
|
if (payload->flags & CLEARTRACE)
|
2019-03-10 03:34:41 +08:00
|
|
|
clear_trace();
|
|
|
|
|
2020-04-11 04:52:16 +08:00
|
|
|
if (payload->flags & INIT) {
|
2020-03-17 18:37:38 +08:00
|
|
|
if (!InitDesfireCard()) {
|
2019-03-10 03:34:41 +08:00
|
|
|
return;
|
2020-03-17 18:37:38 +08:00
|
|
|
}
|
2019-03-10 03:34:41 +08:00
|
|
|
}
|
|
|
|
|
2020-04-11 04:52:16 +08:00
|
|
|
int len = DesfireAPDU(payload->datain, payload->datalen, resp);
|
2020-03-17 18:37:38 +08:00
|
|
|
if (DBGLEVEL >= DBG_EXTENDED)
|
2020-03-17 04:51:45 +08:00
|
|
|
print_result("RESP <--: ", resp, len);
|
2019-03-10 03:34:41 +08:00
|
|
|
|
2019-03-10 07:00:59 +08:00
|
|
|
if (!len) {
|
2019-03-10 03:34:41 +08:00
|
|
|
OnError(2);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-04-11 04:52:16 +08:00
|
|
|
if (payload->flags & DISCONNECT)
|
2019-03-10 03:34:41 +08:00
|
|
|
OnSuccess();
|
|
|
|
|
2020-04-11 04:52:16 +08:00
|
|
|
//reply_mix(CMD_ACK, 1, len, 0, resp, len);
|
|
|
|
LED_B_ON();
|
|
|
|
|
2020-04-11 17:32:31 +08:00
|
|
|
|
|
|
|
cmdres_t rpayload;
|
2020-04-11 04:59:55 +08:00
|
|
|
rpayload.len = len;
|
|
|
|
memcpy(rpayload.data, resp, rpayload.len);
|
2020-04-11 17:32:31 +08:00
|
|
|
reply_ng(CMD_HF_DESFIRE_COMMAND, PM3_SUCCESS, (uint8_t *)&rpayload, sizeof(rpayload));
|
2020-04-11 04:52:16 +08:00
|
|
|
LED_B_OFF();
|
2014-09-18 18:38:31 +08:00
|
|
|
}
|
|
|
|
|
2019-03-10 18:20:22 +08:00
|
|
|
void MifareDesfireGetInformation() {
|
2019-03-09 15:59:13 +08:00
|
|
|
|
2020-03-17 04:51:45 +08:00
|
|
|
LEDsoff();
|
|
|
|
|
2019-03-10 03:34:41 +08:00
|
|
|
int len = 0;
|
|
|
|
iso14a_card_select_t card;
|
2019-05-01 03:10:11 +08:00
|
|
|
uint8_t resp[PM3_CMD_DATA_SIZE] = {0x00};
|
2019-03-10 03:34:41 +08:00
|
|
|
|
2020-03-17 04:51:45 +08:00
|
|
|
struct p {
|
|
|
|
uint8_t isOK;
|
|
|
|
uint8_t uid[7];
|
|
|
|
uint8_t versionHW[7];
|
|
|
|
uint8_t versionSW[7];
|
|
|
|
uint8_t details[14];
|
|
|
|
} PACKED payload;
|
2020-04-10 07:18:48 +08:00
|
|
|
|
2019-03-10 03:34:41 +08:00
|
|
|
/*
|
|
|
|
1 = PCB 1
|
|
|
|
2 = cid 2
|
|
|
|
3 = desfire command 3
|
|
|
|
4-5 = crc 4 key
|
|
|
|
5-6 crc
|
|
|
|
PCB == 0x0A because sending CID byte.
|
|
|
|
CID == 0x00 first card?
|
|
|
|
*/
|
|
|
|
clear_trace();
|
|
|
|
set_tracing(true);
|
|
|
|
iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);
|
2020-04-11 08:32:55 +08:00
|
|
|
|
2020-04-10 16:27:28 +08:00
|
|
|
// reset the pcb_blocknum,
|
|
|
|
pcb_blocknum = 0;
|
2019-03-10 03:34:41 +08:00
|
|
|
|
|
|
|
// card select - information
|
2019-03-10 07:00:59 +08:00
|
|
|
if (!iso14443a_select_card(NULL, &card, NULL, true, 0, false)) {
|
2019-06-06 16:05:09 +08:00
|
|
|
if (DBGLEVEL >= DBG_ERROR) DbpString("Can't select card");
|
2020-03-17 04:51:45 +08:00
|
|
|
payload.isOK = 1; // 2 == can not select
|
|
|
|
reply_ng(CMD_HF_DESFIRE_INFO, PM3_ESOFT, (uint8_t *)&payload, sizeof(payload));
|
|
|
|
switch_off();
|
2019-03-10 03:34:41 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-03-10 07:00:59 +08:00
|
|
|
if (card.uidlen != 7) {
|
2019-06-06 16:05:09 +08:00
|
|
|
if (DBGLEVEL >= DBG_ERROR) Dbprintf("Wrong UID size. Expected 7byte got %d", card.uidlen);
|
2020-03-17 04:51:45 +08:00
|
|
|
payload.isOK = 2; // 2 == WRONG UID
|
|
|
|
reply_ng(CMD_HF_DESFIRE_INFO, PM3_ESOFT, (uint8_t *)&payload, sizeof(payload));
|
|
|
|
switch_off();
|
2019-03-10 03:34:41 +08:00
|
|
|
return;
|
|
|
|
}
|
2020-03-17 04:51:45 +08:00
|
|
|
// add uid.
|
2020-03-24 20:27:33 +08:00
|
|
|
memcpy(payload.uid, card.uid, sizeof(payload.uid));
|
2019-03-10 03:34:41 +08:00
|
|
|
|
|
|
|
LED_A_ON();
|
2020-04-12 23:59:46 +08:00
|
|
|
uint8_t cmd[] = {0x90, MFDES_GET_VERSION, 0x00, 0x00, 0x00};
|
2019-03-10 03:34:41 +08:00
|
|
|
size_t cmd_len = sizeof(cmd);
|
|
|
|
|
|
|
|
len = DesfireAPDU(cmd, cmd_len, resp);
|
2019-03-10 07:00:59 +08:00
|
|
|
if (!len) {
|
2019-03-10 03:34:41 +08:00
|
|
|
print_result("ERROR <--: ", resp, len);
|
2020-03-17 04:51:45 +08:00
|
|
|
payload.isOK = 3; // 3 == DOESNT ANSWER TO GET_VERSION
|
|
|
|
reply_ng(CMD_HF_DESFIRE_INFO, PM3_ESOFT, (uint8_t *)&payload, sizeof(payload));
|
|
|
|
switch_off();
|
2019-03-10 03:34:41 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-03-17 04:51:45 +08:00
|
|
|
memcpy(payload.versionHW, resp + 1, sizeof(payload.versionHW));
|
2019-03-10 03:34:41 +08:00
|
|
|
|
|
|
|
// ADDITION_FRAME 1
|
2020-04-12 23:59:46 +08:00
|
|
|
cmd[1] = MFDES_ADDITIONAL_FRAME;
|
2019-03-10 03:34:41 +08:00
|
|
|
len = DesfireAPDU(cmd, cmd_len, resp);
|
2019-03-10 07:00:59 +08:00
|
|
|
if (!len) {
|
2019-03-10 03:34:41 +08:00
|
|
|
print_result("ERROR <--: ", resp, len);
|
2020-03-17 04:51:45 +08:00
|
|
|
payload.isOK = 3; // 3 == DOESNT ANSWER TO GET_VERSION
|
|
|
|
reply_ng(CMD_HF_DESFIRE_INFO, PM3_ESOFT, (uint8_t *)&payload, sizeof(payload));
|
|
|
|
switch_off();
|
2019-03-10 03:34:41 +08:00
|
|
|
return;
|
|
|
|
}
|
2020-03-17 04:51:45 +08:00
|
|
|
memcpy(payload.versionSW, resp + 1, sizeof(payload.versionSW));
|
2019-03-10 03:34:41 +08:00
|
|
|
|
|
|
|
// ADDITION_FRAME 2
|
|
|
|
len = DesfireAPDU(cmd, cmd_len, resp);
|
2019-03-10 07:00:59 +08:00
|
|
|
if (!len) {
|
2019-03-10 03:34:41 +08:00
|
|
|
print_result("ERROR <--: ", resp, len);
|
2020-03-17 04:51:45 +08:00
|
|
|
payload.isOK = 3; // 3 == DOESNT ANSWER TO GET_VERSION
|
|
|
|
reply_ng(CMD_HF_DESFIRE_INFO, PM3_ESOFT, (uint8_t *)&payload, sizeof(payload));
|
|
|
|
switch_off();
|
2019-03-10 03:34:41 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-03-17 04:51:45 +08:00
|
|
|
memcpy(payload.details, resp + 1, sizeof(payload.details));
|
2019-03-10 03:34:41 +08:00
|
|
|
|
2020-03-17 04:51:45 +08:00
|
|
|
LED_B_ON();
|
|
|
|
reply_ng(CMD_HF_DESFIRE_INFO, PM3_SUCCESS, (uint8_t *)&payload, sizeof(payload));
|
|
|
|
LED_B_OFF();
|
2020-04-10 07:18:48 +08:00
|
|
|
|
2019-03-10 03:34:41 +08:00
|
|
|
// reset the pcb_blocknum,
|
|
|
|
pcb_blocknum = 0;
|
|
|
|
OnSuccess();
|
2014-09-12 05:23:46 +08:00
|
|
|
}
|
|
|
|
|
2020-04-11 17:32:31 +08:00
|
|
|
typedef struct {
|
|
|
|
uint8_t sessionkeylen;
|
|
|
|
uint8_t sessionkey[24];
|
|
|
|
} authres_t;
|
2020-04-11 03:02:46 +08:00
|
|
|
|
|
|
|
void MifareDES_Auth1(uint8_t *datain) {
|
2019-03-10 03:34:41 +08:00
|
|
|
int len = 0;
|
2020-04-11 03:02:46 +08:00
|
|
|
struct p {
|
|
|
|
uint8_t mode;
|
|
|
|
uint8_t algo;
|
|
|
|
uint8_t keyno;
|
|
|
|
uint8_t keylen;
|
2020-04-11 17:32:31 +08:00
|
|
|
uint8_t key[24];
|
2020-04-11 03:02:46 +08:00
|
|
|
} PACKED;
|
|
|
|
struct p *payload = (struct p *) datain;
|
2014-09-12 05:23:46 +08:00
|
|
|
|
2020-04-11 03:02:46 +08:00
|
|
|
// 3 different way to authenticate AUTH (CRC16) , AUTH_ISO (CRC32) , AUTH_AES (CRC32)
|
|
|
|
// 4 different crypto arg1 DES, 3DES, 3K3DES, AES
|
|
|
|
// 3 different communication modes, PLAIN,MAC,CRYPTO
|
2014-09-12 05:23:46 +08:00
|
|
|
|
2020-04-11 03:02:46 +08:00
|
|
|
mbedtls_aes_context ctx;
|
2019-03-09 15:59:13 +08:00
|
|
|
|
2020-04-11 03:02:46 +08:00
|
|
|
uint8_t keybytes[24];
|
|
|
|
uint8_t resp[256] = {0x00};
|
2019-03-10 03:34:41 +08:00
|
|
|
uint8_t cmd[40] = {0x00};
|
2020-04-11 03:02:46 +08:00
|
|
|
|
|
|
|
// Crypt constants
|
|
|
|
uint8_t IV[16] = {0x00};
|
|
|
|
uint8_t RndA[16] = {0x00};
|
|
|
|
uint8_t RndB[16] = {0x00};
|
2019-03-10 03:34:41 +08:00
|
|
|
uint8_t encRndB[16] = {0x00};
|
2020-04-11 03:02:46 +08:00
|
|
|
uint8_t rotRndB[16] = {0x00}; //RndB'
|
|
|
|
uint8_t both[32] = {0x00}; // ek/dk_keyNo(RndA+RndB')
|
|
|
|
|
|
|
|
// Generate Random Value
|
|
|
|
uint32_t value = prng_successor(GetTickCount(), 32);
|
|
|
|
num_to_bytes(value, 4, &RndA[0]);
|
|
|
|
value = prng_successor(GetTickCount(), 32);
|
|
|
|
num_to_bytes(value, 4, &RndA[4]);
|
|
|
|
value = prng_successor(GetTickCount(), 32);
|
|
|
|
num_to_bytes(value, 4, &RndA[8]);
|
|
|
|
value = prng_successor(GetTickCount(), 32);
|
|
|
|
num_to_bytes(value, 4, &RndA[12]);
|
|
|
|
|
|
|
|
// Default Keys
|
2020-04-12 23:39:11 +08:00
|
|
|
uint8_t PICC_MASTER_KEY8[8] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
|
|
|
|
uint8_t PICC_MASTER_KEY16[16] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
2020-04-12 23:41:36 +08:00
|
|
|
0x00, 0x00
|
|
|
|
};
|
2020-04-12 23:39:11 +08:00
|
|
|
uint8_t PICC_MASTER_KEY24[24] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
2020-04-12 23:41:36 +08:00
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
|
|
|
|
};
|
2020-04-11 03:02:46 +08:00
|
|
|
//uint8_t null_key_data16[16] = {0x00};
|
|
|
|
//uint8_t new_key_data8[8] = { 0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77};
|
|
|
|
//uint8_t new_key_data16[16] = { 0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,0x99,0xAA,0xBB,0xCC,0xDD,0xEE,0xFF};
|
|
|
|
|
2014-09-12 05:23:46 +08:00
|
|
|
|
2020-04-07 15:14:44 +08:00
|
|
|
//InitDesfireCard();
|
2019-03-09 15:59:13 +08:00
|
|
|
|
2020-04-11 03:02:46 +08:00
|
|
|
// Part 1
|
2019-03-10 03:34:41 +08:00
|
|
|
LED_A_ON();
|
|
|
|
LED_B_OFF();
|
|
|
|
LED_C_OFF();
|
2019-03-09 15:59:13 +08:00
|
|
|
|
2020-04-11 04:59:55 +08:00
|
|
|
if (payload->key == NULL) {
|
2020-04-12 23:59:46 +08:00
|
|
|
if (payload->algo == MFDES_AUTH_DES) {
|
2020-04-11 04:59:55 +08:00
|
|
|
memcpy(keybytes, PICC_MASTER_KEY8, 8);
|
2020-04-12 23:59:46 +08:00
|
|
|
} else if (payload->algo == MFDES_ALGO_AES || payload->algo == MFDES_ALGO_3DES) {
|
2020-04-11 04:59:55 +08:00
|
|
|
memcpy(keybytes, PICC_MASTER_KEY16, 16);
|
2020-04-11 08:32:55 +08:00
|
|
|
} else if (payload->algo == MFDES_ALGO_3DES) {
|
|
|
|
memcpy(keybytes, PICC_MASTER_KEY24, 24);
|
2020-04-11 03:02:46 +08:00
|
|
|
}
|
2020-04-11 04:59:55 +08:00
|
|
|
} else {
|
|
|
|
memcpy(keybytes, payload->key, payload->keylen);
|
|
|
|
}
|
2020-04-11 03:02:46 +08:00
|
|
|
|
2020-04-12 23:59:46 +08:00
|
|
|
|
2020-04-11 03:02:46 +08:00
|
|
|
struct desfire_key defaultkey = {0};
|
|
|
|
desfirekey_t key = &defaultkey;
|
2019-03-09 15:59:13 +08:00
|
|
|
|
2020-04-11 04:59:55 +08:00
|
|
|
if (payload->algo == MFDES_ALGO_AES) {
|
|
|
|
mbedtls_aes_init(&ctx);
|
|
|
|
Desfire_aes_key_new(keybytes, key);
|
|
|
|
} else if (payload->algo == MFDES_ALGO_3DES) {
|
2020-04-11 03:02:46 +08:00
|
|
|
Desfire_3des_key_new_with_version(keybytes, key);
|
2020-04-11 04:59:55 +08:00
|
|
|
} else if (payload->algo == MFDES_ALGO_DES) {
|
2020-04-11 03:02:46 +08:00
|
|
|
Desfire_des_key_new(keybytes, key);
|
2020-04-11 04:59:55 +08:00
|
|
|
} else if (payload->algo == MFDES_ALGO_3K3DES) {
|
2020-04-11 03:02:46 +08:00
|
|
|
Desfire_3k3des_key_new_with_version(keybytes, key);
|
|
|
|
}
|
2019-03-09 15:59:13 +08:00
|
|
|
|
2020-04-12 23:59:46 +08:00
|
|
|
uint8_t subcommand = MFDES_AUTHENTICATE;
|
2020-04-11 03:02:46 +08:00
|
|
|
|
2020-04-11 04:59:55 +08:00
|
|
|
if (payload->mode == MFDES_AUTH_AES)
|
2020-04-12 23:59:46 +08:00
|
|
|
subcommand = MFDES_AUTHENTICATE_AES;
|
2020-04-11 04:59:55 +08:00
|
|
|
else if (payload->mode == MFDES_AUTH_ISO)
|
2020-04-12 23:59:46 +08:00
|
|
|
subcommand = MFDES_AUTHENTICATE_ISO;
|
2020-04-11 03:02:46 +08:00
|
|
|
|
|
|
|
if (payload->mode != MFDES_AUTH_PICC) {
|
|
|
|
// Let's send our auth command
|
|
|
|
cmd[0] = 0x90;
|
|
|
|
cmd[1] = subcommand;
|
|
|
|
cmd[2] = 0x0;
|
|
|
|
cmd[3] = 0x0;
|
|
|
|
cmd[4] = 0x1;
|
|
|
|
cmd[5] = payload->keyno;
|
|
|
|
cmd[6] = 0x0;
|
|
|
|
len = DesfireAPDU(cmd, 7, resp);
|
2020-04-11 04:59:55 +08:00
|
|
|
} else {
|
2020-04-12 23:59:46 +08:00
|
|
|
cmd[0] = MFDES_AUTHENTICATE;
|
2020-04-11 03:02:46 +08:00
|
|
|
cmd[1] = payload->keyno;
|
|
|
|
len = DesfireAPDU(cmd, 2, resp);
|
|
|
|
}
|
2019-03-09 15:59:13 +08:00
|
|
|
|
2020-04-11 03:02:46 +08:00
|
|
|
if (!len) {
|
|
|
|
if (DBGLEVEL >= DBG_ERROR) {
|
|
|
|
DbpString("Authentication failed. Card timeout.");
|
|
|
|
}
|
2020-04-11 04:59:55 +08:00
|
|
|
OnErrorNG(CMD_HF_DESFIRE_AUTH1, 3);
|
2020-04-11 03:02:46 +08:00
|
|
|
return;
|
|
|
|
}
|
2019-03-09 15:59:13 +08:00
|
|
|
|
2020-04-12 23:39:11 +08:00
|
|
|
if (resp[2] == (uint8_t)MFDES_ADDITIONAL_FRAME) {
|
2020-04-11 03:02:46 +08:00
|
|
|
DbpString("Authentication failed. Invalid key number.");
|
2020-04-11 04:59:55 +08:00
|
|
|
OnErrorNG(CMD_HF_DESFIRE_AUTH1, 3);
|
2020-04-11 03:02:46 +08:00
|
|
|
return;
|
|
|
|
}
|
2019-03-09 15:59:13 +08:00
|
|
|
|
2020-04-16 15:01:14 +08:00
|
|
|
int rndlen = 8;
|
2020-04-11 08:32:55 +08:00
|
|
|
int expectedlen = 1 + 8 + 2 + 2;
|
|
|
|
if (payload->algo == MFDES_ALGO_AES || payload->algo == MFDES_ALGO_3K3DES) {
|
|
|
|
expectedlen = 1 + 16 + 2 + 2;
|
2020-04-16 15:01:14 +08:00
|
|
|
rndlen = 16;
|
2020-04-11 08:32:55 +08:00
|
|
|
}
|
|
|
|
|
2020-04-13 07:19:26 +08:00
|
|
|
if (payload->mode == MFDES_AUTH_PICC) {
|
|
|
|
expectedlen = 1 + 1 + 8 + 2;
|
2020-04-16 15:01:14 +08:00
|
|
|
rndlen = 8;
|
2020-04-13 07:19:26 +08:00
|
|
|
}
|
2020-04-12 23:39:11 +08:00
|
|
|
|
2020-04-11 08:32:55 +08:00
|
|
|
if (len != expectedlen) {
|
|
|
|
if (DBGLEVEL >= DBG_ERROR) {
|
|
|
|
DbpString("Authentication failed. Length of answer doesn't match algo.");
|
|
|
|
print_result("Res-Buffer: ", resp, len);
|
|
|
|
}
|
|
|
|
OnErrorNG(CMD_HF_DESFIRE_AUTH1, 3);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-04-11 03:02:46 +08:00
|
|
|
// Part 2
|
|
|
|
if (payload->mode != MFDES_AUTH_PICC) {
|
2020-04-12 23:39:11 +08:00
|
|
|
memcpy(encRndB, resp + 1, rndlen);
|
2020-04-11 03:02:46 +08:00
|
|
|
} else {
|
2020-04-12 23:39:11 +08:00
|
|
|
memcpy(encRndB, resp + 2, rndlen);
|
2020-04-11 03:02:46 +08:00
|
|
|
}
|
2020-04-10 05:30:49 +08:00
|
|
|
|
2020-04-11 03:02:46 +08:00
|
|
|
// Part 3
|
2020-04-11 04:59:55 +08:00
|
|
|
if (payload->algo == MFDES_ALGO_AES) {
|
2020-04-11 04:52:16 +08:00
|
|
|
if (mbedtls_aes_setkey_dec(&ctx, key->data, 128) != 0) {
|
|
|
|
if (DBGLEVEL >= DBG_EXTENDED) {
|
|
|
|
DbpString("mbedtls_aes_setkey_dec failed");
|
|
|
|
}
|
2020-04-11 04:59:55 +08:00
|
|
|
OnErrorNG(CMD_HF_DESFIRE_AUTH1, 7);
|
2020-04-11 04:52:16 +08:00
|
|
|
return;
|
|
|
|
}
|
2020-04-11 03:02:46 +08:00
|
|
|
mbedtls_aes_crypt_cbc(&ctx, MBEDTLS_AES_DECRYPT, 16, IV, encRndB, RndB);
|
2020-04-12 23:39:11 +08:00
|
|
|
} else if (payload->algo == MFDES_ALGO_DES)
|
2020-04-13 02:58:41 +08:00
|
|
|
des_decrypt(RndB, encRndB, key->data);
|
2020-04-12 23:39:11 +08:00
|
|
|
else if (payload->algo == MFDES_ALGO_3DES)
|
|
|
|
tdes_nxp_receive(encRndB, RndB, rndlen, key->data, IV, 2);
|
2020-04-11 08:32:55 +08:00
|
|
|
else if (payload->algo == MFDES_ALGO_3K3DES)
|
2020-04-12 23:39:11 +08:00
|
|
|
tdes_nxp_receive(encRndB, RndB, rndlen, key->data, IV, 3);
|
2019-03-09 15:59:13 +08:00
|
|
|
|
2020-04-11 03:02:46 +08:00
|
|
|
// - Rotate RndB by 8 bits
|
2020-04-12 23:39:11 +08:00
|
|
|
memcpy(rotRndB, RndB, rndlen);
|
|
|
|
rol(rotRndB, rndlen);
|
2019-03-09 15:59:13 +08:00
|
|
|
|
2020-04-11 03:02:46 +08:00
|
|
|
uint8_t encRndA[16] = {0x00};
|
2019-03-09 15:59:13 +08:00
|
|
|
|
2020-04-11 03:02:46 +08:00
|
|
|
// - Encrypt our response
|
2020-04-12 23:39:11 +08:00
|
|
|
if (payload->mode == MFDES_AUTH_DES || payload->mode == MFDES_AUTH_PICC) {
|
2020-04-13 02:58:41 +08:00
|
|
|
des_decrypt(encRndA, RndA, key->data);
|
2020-04-12 23:39:11 +08:00
|
|
|
memcpy(both, encRndA, rndlen);
|
2019-03-09 15:59:13 +08:00
|
|
|
|
2020-04-12 23:39:11 +08:00
|
|
|
for (int x = 0; x < rndlen; x++) {
|
2020-04-11 03:02:46 +08:00
|
|
|
rotRndB[x] = rotRndB[x] ^ encRndA[x];
|
|
|
|
}
|
|
|
|
|
2020-04-13 02:58:41 +08:00
|
|
|
des_decrypt(encRndB, rotRndB, key->data);
|
2020-04-12 23:39:11 +08:00
|
|
|
memcpy(both + 8, encRndB, rndlen);
|
2020-04-12 23:41:36 +08:00
|
|
|
} else if (payload->mode == MFDES_AUTH_ISO) {
|
2020-04-11 08:32:55 +08:00
|
|
|
if (payload->algo == MFDES_ALGO_3DES) {
|
2020-04-12 23:39:11 +08:00
|
|
|
uint8_t tmp[16] = {0x00};
|
|
|
|
memcpy(tmp, RndA, rndlen);
|
|
|
|
memcpy(tmp + rndlen, rotRndB, rndlen);
|
2020-04-12 23:41:36 +08:00
|
|
|
tdes_nxp_send(tmp, both, 16, key->data, IV, 2);
|
2020-04-11 08:32:55 +08:00
|
|
|
} else if (payload->algo == MFDES_ALGO_3K3DES) {
|
2020-04-12 23:39:11 +08:00
|
|
|
uint8_t tmp[32] = {0x00};
|
|
|
|
memcpy(tmp, RndA, rndlen);
|
|
|
|
memcpy(tmp + rndlen, rotRndB, rndlen);
|
2020-04-12 23:41:36 +08:00
|
|
|
tdes_nxp_send(tmp, both, 32, key->data, IV, 3);
|
2020-04-11 08:32:55 +08:00
|
|
|
}
|
|
|
|
} else if (payload->mode == MFDES_AUTH_AES) {
|
2020-04-11 03:02:46 +08:00
|
|
|
uint8_t tmp[32] = {0x00};
|
2020-04-12 23:39:11 +08:00
|
|
|
memcpy(tmp, RndA, rndlen);
|
|
|
|
memcpy(tmp + 16, rotRndB, rndlen);
|
2020-04-11 04:59:55 +08:00
|
|
|
if (payload->algo == MFDES_ALGO_AES) {
|
2020-04-11 03:02:46 +08:00
|
|
|
if (mbedtls_aes_setkey_enc(&ctx, key->data, 128) != 0) {
|
|
|
|
if (DBGLEVEL >= DBG_EXTENDED) {
|
|
|
|
DbpString("mbedtls_aes_setkey_enc failed");
|
2014-12-20 11:44:32 +08:00
|
|
|
}
|
2020-04-11 04:59:55 +08:00
|
|
|
OnErrorNG(CMD_HF_DESFIRE_AUTH1, 7);
|
2014-12-20 11:44:32 +08:00
|
|
|
return;
|
|
|
|
}
|
2020-04-11 03:02:46 +08:00
|
|
|
mbedtls_aes_crypt_cbc(&ctx, MBEDTLS_AES_ENCRYPT, 32, IV, tmp, both);
|
|
|
|
}
|
|
|
|
}
|
2019-03-09 15:59:13 +08:00
|
|
|
|
2020-04-11 08:32:55 +08:00
|
|
|
int bothlen = 16;
|
|
|
|
if (payload->algo == MFDES_ALGO_AES || payload->algo == MFDES_ALGO_3K3DES) {
|
|
|
|
bothlen = 32;
|
|
|
|
}
|
2020-04-11 03:02:46 +08:00
|
|
|
if (payload->mode != MFDES_AUTH_PICC) {
|
|
|
|
cmd[0] = 0x90;
|
2020-04-12 23:59:46 +08:00
|
|
|
cmd[1] = MFDES_ADDITIONAL_FRAME;
|
2020-04-11 03:02:46 +08:00
|
|
|
cmd[2] = 0x00;
|
|
|
|
cmd[3] = 0x00;
|
2020-04-11 08:32:55 +08:00
|
|
|
cmd[4] = bothlen;
|
|
|
|
memcpy(cmd + 5, both, bothlen);
|
|
|
|
cmd[bothlen + 5] = 0x0;
|
|
|
|
len = DesfireAPDU(cmd, 5 + bothlen + 1, resp);
|
2020-04-11 03:02:46 +08:00
|
|
|
} else {
|
2020-04-12 23:59:46 +08:00
|
|
|
cmd[0] = MFDES_ADDITIONAL_FRAME;
|
2020-04-12 23:39:11 +08:00
|
|
|
memcpy(cmd + 1, both, bothlen);
|
|
|
|
len = DesfireAPDU(cmd, 1 + bothlen, resp);
|
2020-04-11 03:02:46 +08:00
|
|
|
}
|
2019-03-09 15:59:13 +08:00
|
|
|
|
2020-04-11 03:02:46 +08:00
|
|
|
if (!len) {
|
|
|
|
if (DBGLEVEL >= DBG_ERROR) {
|
|
|
|
DbpString("Authentication failed. Card timeout.");
|
|
|
|
}
|
2020-04-11 04:59:55 +08:00
|
|
|
OnErrorNG(CMD_HF_DESFIRE_AUTH1, 3);
|
2020-04-11 03:02:46 +08:00
|
|
|
return;
|
|
|
|
}
|
2019-03-09 15:59:13 +08:00
|
|
|
|
2020-04-11 03:02:46 +08:00
|
|
|
if (payload->mode != MFDES_AUTH_PICC) {
|
|
|
|
if ((resp[len - 4] != 0x91) || (resp[len - 3] != 0x00)) {
|
|
|
|
DbpString("Authentication failed.");
|
2020-04-11 04:59:55 +08:00
|
|
|
OnErrorNG(CMD_HF_DESFIRE_AUTH1, 6);
|
2020-04-11 03:02:46 +08:00
|
|
|
return;
|
|
|
|
}
|
2020-04-11 04:59:55 +08:00
|
|
|
} else {
|
2020-04-11 03:02:46 +08:00
|
|
|
if (resp[1] != 0x00) {
|
|
|
|
DbpString("Authentication failed.");
|
2020-04-11 04:59:55 +08:00
|
|
|
OnErrorNG(CMD_HF_DESFIRE_AUTH1, 6);
|
2020-04-11 03:02:46 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2019-03-09 15:59:13 +08:00
|
|
|
|
2020-04-11 03:02:46 +08:00
|
|
|
// Part 4
|
2020-04-12 23:39:11 +08:00
|
|
|
|
|
|
|
Desfire_session_key_new(RndA, RndB, key, sessionkey);
|
|
|
|
|
2020-04-11 04:52:16 +08:00
|
|
|
if (DBGLEVEL >= DBG_EXTENDED)
|
2020-04-12 23:59:46 +08:00
|
|
|
print_result("SESSIONKEY : ", sessionkey->data, payload->keylen);
|
2020-04-11 03:02:46 +08:00
|
|
|
|
|
|
|
if (payload->mode != MFDES_AUTH_PICC) {
|
2020-04-12 23:39:11 +08:00
|
|
|
memcpy(encRndA, resp + 1, rndlen);
|
2020-04-11 04:59:55 +08:00
|
|
|
} else {
|
2020-04-12 23:39:11 +08:00
|
|
|
memcpy(encRndA, resp + 2, rndlen);
|
2020-04-11 03:02:46 +08:00
|
|
|
}
|
2019-03-09 15:59:13 +08:00
|
|
|
|
2020-04-11 04:59:55 +08:00
|
|
|
if (payload->mode == MFDES_AUTH_DES || payload->mode == MFDES_AUTH_PICC) {
|
2020-04-12 23:39:11 +08:00
|
|
|
if (payload->algo == MFDES_ALGO_DES)
|
2020-04-13 02:58:41 +08:00
|
|
|
des_decrypt(encRndA, encRndA, key->data);
|
2020-04-12 23:39:11 +08:00
|
|
|
else if (payload->algo == MFDES_ALGO_3DES)
|
2020-04-12 23:41:36 +08:00
|
|
|
tdes_nxp_receive(encRndA, encRndA, rndlen, key->data, IV, 2);
|
2020-04-11 08:32:55 +08:00
|
|
|
else if (payload->algo == MFDES_ALGO_3K3DES)
|
2020-04-12 23:41:36 +08:00
|
|
|
tdes_nxp_receive(encRndA, encRndA, rndlen, key->data, IV, 3);
|
2020-04-11 04:59:55 +08:00
|
|
|
} else if (payload->mode == MFDES_AUTH_AES) {
|
2020-04-11 04:52:16 +08:00
|
|
|
if (mbedtls_aes_setkey_dec(&ctx, key->data, 128) != 0) {
|
|
|
|
if (DBGLEVEL >= DBG_EXTENDED) {
|
|
|
|
DbpString("mbedtls_aes_setkey_dec failed");
|
|
|
|
}
|
2020-04-11 04:59:55 +08:00
|
|
|
OnErrorNG(CMD_HF_DESFIRE_AUTH1, 7);
|
2020-04-11 04:52:16 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
mbedtls_aes_crypt_cbc(&ctx, MBEDTLS_AES_DECRYPT, 16, IV, encRndA, encRndA);
|
2020-04-11 03:02:46 +08:00
|
|
|
}
|
2019-03-09 15:59:13 +08:00
|
|
|
|
2020-04-12 23:39:11 +08:00
|
|
|
rol(RndA, rndlen);
|
2020-04-11 04:52:16 +08:00
|
|
|
if (DBGLEVEL >= DBG_EXTENDED) {
|
2020-04-12 23:39:11 +08:00
|
|
|
print_result("RndA : ", RndA, rndlen);
|
|
|
|
print_result("RndB: ", RndB, rndlen);
|
|
|
|
print_result("encRndA : ", encRndA, rndlen);
|
2020-04-11 04:52:16 +08:00
|
|
|
}
|
2020-04-12 23:39:11 +08:00
|
|
|
for (int x = 0; x < rndlen; x++) {
|
2020-04-11 03:02:46 +08:00
|
|
|
if (RndA[x] != encRndA[x]) {
|
|
|
|
DbpString("Authentication failed. Cannot verify Session Key.");
|
2020-04-11 04:59:55 +08:00
|
|
|
OnErrorNG(CMD_HF_DESFIRE_AUTH1, 4);
|
2020-04-11 03:02:46 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2020-04-11 04:59:55 +08:00
|
|
|
//Change the selected key to a new value.
|
2020-04-11 03:02:46 +08:00
|
|
|
|
2020-04-11 04:59:55 +08:00
|
|
|
/*
|
|
|
|
// Current key is a 3DES key, change it to a DES key
|
|
|
|
if (payload->algo == 2) {
|
|
|
|
cmd[0] = 0x90;
|
|
|
|
cmd[1] = CHANGE_KEY;
|
|
|
|
cmd[2] = payload->keyno;
|
|
|
|
|
|
|
|
uint8_t newKey[16] = {0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77};
|
|
|
|
|
|
|
|
uint8_t first, second;
|
|
|
|
uint8_t buff1[8] = {0x00};
|
|
|
|
uint8_t buff2[8] = {0x00};
|
|
|
|
uint8_t buff3[8] = {0x00};
|
|
|
|
|
|
|
|
memcpy(buff1,newKey, 8);
|
|
|
|
memcpy(buff2,newKey + 8, 8);
|
|
|
|
|
|
|
|
compute_crc(CRC_14443_A, newKey, 16, &first, &second);
|
|
|
|
memcpy(buff3, &first, 1);
|
|
|
|
memcpy(buff3 + 1, &second, 1);
|
|
|
|
|
|
|
|
tdes_dec(&buff1, &buff1, skey->data);
|
|
|
|
memcpy(cmd+2,buff1,8);
|
|
|
|
|
|
|
|
for (int x = 0; x < 8; x++) {
|
|
|
|
buff2[x] = buff2[x] ^ buff1[x];
|
|
|
|
}
|
|
|
|
tdes_dec(&buff2, &buff2, skey->data);
|
|
|
|
memcpy(cmd+10,buff2,8);
|
|
|
|
|
|
|
|
for (int x = 0; x < 8; x++) {
|
|
|
|
buff3[x] = buff3[x] ^ buff2[x];
|
|
|
|
}
|
|
|
|
tdes_dec(&buff3, &buff3, skey->data);
|
|
|
|
memcpy(cmd+19,buff3,8);
|
|
|
|
|
|
|
|
// The command always times out on the first attempt, this will retry until a response
|
|
|
|
// is recieved.
|
|
|
|
len = 0;
|
|
|
|
while(!len) {
|
|
|
|
len = DesfireAPDU(cmd,27,resp);
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
// Current key is a DES key, change it to a 3DES key
|
|
|
|
if (payload->algo == 1) {
|
|
|
|
cmd[0] = 0x90;
|
|
|
|
cmd[1] = CHANGE_KEY;
|
|
|
|
cmd[2] = payload->keyno;
|
|
|
|
|
|
|
|
uint8_t newKey[16] = {0x40,0x41,0x42,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x4a,0x4b,0x4c,0x4d,0x4e,0x4f};
|
|
|
|
|
|
|
|
uint8_t first, second;
|
|
|
|
uint8_t buff1[8] = {0x00};
|
|
|
|
uint8_t buff2[8] = {0x00};
|
|
|
|
uint8_t buff3[8] = {0x00};
|
|
|
|
|
|
|
|
memcpy(buff1,newKey, 8);
|
|
|
|
memcpy(buff2,newKey + 8, 8);
|
|
|
|
|
|
|
|
compute_crc(CRC_14443_A, newKey, 16, &first, &second);
|
|
|
|
memcpy(buff3, &first, 1);
|
|
|
|
memcpy(buff3 + 1, &second, 1);
|
|
|
|
|
|
|
|
des_dec(&buff1, &buff1, skey->data);
|
|
|
|
memcpy(cmd+3,buff1,8);
|
|
|
|
|
|
|
|
for (int x = 0; x < 8; x++) {
|
|
|
|
buff2[x] = buff2[x] ^ buff1[x];
|
|
|
|
}
|
|
|
|
des_dec(&buff2, &buff2, skey->data);
|
|
|
|
memcpy(cmd+11,buff2,8);
|
|
|
|
|
|
|
|
for (int x = 0; x < 8; x++) {
|
|
|
|
buff3[x] = buff3[x] ^ buff2[x];
|
|
|
|
}
|
|
|
|
des_dec(&buff3, &buff3, skey->data);
|
|
|
|
memcpy(cmd+19,buff3,8);
|
|
|
|
|
|
|
|
// The command always times out on the first attempt, this will retry until a response
|
|
|
|
// is recieved.
|
|
|
|
len = 0;
|
|
|
|
while(!len) {
|
|
|
|
len = DesfireAPDU(cmd,27,resp);
|
|
|
|
}
|
2020-04-10 07:18:48 +08:00
|
|
|
}
|
2020-04-11 04:59:55 +08:00
|
|
|
}
|
|
|
|
*/
|
2019-03-10 03:34:41 +08:00
|
|
|
|
|
|
|
|
2020-04-10 06:03:23 +08:00
|
|
|
//OnSuccess();
|
2020-04-11 03:02:46 +08:00
|
|
|
//reply_old(CMD_ACK, 1, 0, 0, skey->data, payload->keylen);
|
|
|
|
//reply_mix(CMD_ACK, 1, len, 0, resp, len);
|
|
|
|
|
|
|
|
LED_B_ON();
|
2020-04-11 17:32:31 +08:00
|
|
|
authres_t rpayload;
|
2020-04-11 04:59:55 +08:00
|
|
|
rpayload.sessionkeylen = payload->keylen;
|
2020-04-12 23:59:46 +08:00
|
|
|
memcpy(rpayload.sessionkey, sessionkey->data, rpayload.sessionkeylen);
|
2020-04-11 17:32:31 +08:00
|
|
|
reply_ng(CMD_HF_DESFIRE_AUTH1, PM3_SUCCESS, (uint8_t *)&rpayload, sizeof(rpayload));
|
2020-04-11 03:02:46 +08:00
|
|
|
LED_B_OFF();
|
2014-09-12 05:23:46 +08:00
|
|
|
}
|
|
|
|
|
2019-04-06 21:50:45 +08:00
|
|
|
// 3 different ISO ways to send data to a DESFIRE (direct, capsuled, capsuled ISO)
|
2014-09-18 18:38:31 +08:00
|
|
|
// cmd = cmd bytes to send
|
|
|
|
// cmd_len = length of cmd
|
|
|
|
// dataout = pointer to response data array
|
2019-03-10 18:20:22 +08:00
|
|
|
int DesfireAPDU(uint8_t *cmd, size_t cmd_len, uint8_t *dataout) {
|
2014-09-12 05:23:46 +08:00
|
|
|
|
2019-03-10 03:34:41 +08:00
|
|
|
size_t len = 0;
|
|
|
|
size_t wrappedLen = 0;
|
2019-05-01 03:10:11 +08:00
|
|
|
uint8_t wCmd[PM3_CMD_DATA_SIZE] = {0x00};
|
2019-03-10 03:34:41 +08:00
|
|
|
uint8_t resp[MAX_FRAME_SIZE];
|
2015-03-24 18:45:31 +08:00
|
|
|
uint8_t par[MAX_PARITY_SIZE];
|
2019-03-09 15:59:13 +08:00
|
|
|
|
2019-03-10 07:00:59 +08:00
|
|
|
wrappedLen = CreateAPDU(cmd, cmd_len, wCmd);
|
2019-03-10 03:34:41 +08:00
|
|
|
|
2020-03-17 18:37:38 +08:00
|
|
|
if (DBGLEVEL >= DBG_EXTENDED)
|
2019-03-10 03:34:41 +08:00
|
|
|
print_result("WCMD <--: ", wCmd, wrappedLen);
|
|
|
|
|
2019-03-10 07:00:59 +08:00
|
|
|
ReaderTransmit(wCmd, wrappedLen, NULL);
|
2019-03-10 03:34:41 +08:00
|
|
|
|
|
|
|
len = ReaderReceive(resp, par);
|
2019-03-10 07:00:59 +08:00
|
|
|
if (!len) {
|
2020-03-17 18:37:38 +08:00
|
|
|
if (DBGLEVEL >= DBG_EXTENDED) Dbprintf("fukked");
|
2019-03-10 03:34:41 +08:00
|
|
|
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
|
2020-03-17 18:37:38 +08:00
|
|
|
if (len >= 4 // PCB+CID+CRC = 4 bytes
|
2020-04-10 07:18:48 +08:00
|
|
|
&& ((resp[0] & 0xC0) == 0 // I-Block
|
|
|
|
|| (resp[0] & 0xD0) == 0x80) // R-Block with ACK bit set to 0
|
|
|
|
&& (resp[0] & 0x01) == pcb_blocknum) { // equal block numbers
|
|
|
|
pcb_blocknum ^= 1; //toggle next block
|
2019-03-10 03:34:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(dataout, resp, len);
|
|
|
|
return len;
|
2019-03-09 15:59:13 +08:00
|
|
|
}
|
2014-09-12 05:23:46 +08:00
|
|
|
|
2014-09-18 18:38:31 +08:00
|
|
|
// CreateAPDU
|
2019-03-10 18:20:22 +08:00
|
|
|
size_t CreateAPDU(uint8_t *datain, size_t len, uint8_t *dataout) {
|
2019-03-09 15:59:13 +08:00
|
|
|
|
2020-04-10 05:49:14 +08:00
|
|
|
size_t cmdlen = MIN(len + 3, PM3_CMD_DATA_SIZE - 1);
|
2014-09-18 18:38:31 +08:00
|
|
|
|
2019-03-10 03:34:41 +08:00
|
|
|
uint8_t cmd[cmdlen];
|
|
|
|
memset(cmd, 0, cmdlen);
|
2019-03-09 15:59:13 +08:00
|
|
|
|
2020-03-17 04:51:45 +08:00
|
|
|
cmd[0] = 0x02; // 0x0A = send cid, 0x02 = no cid.
|
2019-03-10 03:34:41 +08:00
|
|
|
cmd[0] |= pcb_blocknum; // OR the block number into the PCB
|
2020-03-17 04:51:45 +08:00
|
|
|
|
2020-04-10 07:18:48 +08:00
|
|
|
if (DBGLEVEL >= DBG_EXTENDED) Dbprintf("pcb_blocknum %d == %d ", pcb_blocknum, cmd[0]);
|
2020-03-17 18:37:38 +08:00
|
|
|
|
2020-04-10 05:49:14 +08:00
|
|
|
//cmd[1] = 0x90; // CID: 0x00 //TODO: allow multiple selected cards
|
2019-03-09 15:59:13 +08:00
|
|
|
|
2020-04-10 05:49:14 +08:00
|
|
|
memcpy(cmd + 1, datain, len);
|
|
|
|
AddCrc14A(cmd, len + 1);
|
2020-04-10 07:18:48 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
hf 14a apdu -sk 90 60 00 00 00
|
|
|
|
hf 14a apdu -k 90 AF 00 00 00
|
|
|
|
hf 14a apdu 90AF000000
|
|
|
|
*/
|
2019-03-10 03:34:41 +08:00
|
|
|
memcpy(dataout, cmd, cmdlen);
|
|
|
|
return cmdlen;
|
2014-09-18 18:38:31 +08:00
|
|
|
}
|
|
|
|
|
2019-03-10 07:00:59 +08:00
|
|
|
// crc_update(&desfire_crc32, 0, 1); /* CMD_WRITE */
|
|
|
|
// crc_update(&desfire_crc32, addr, addr_sz);
|
|
|
|
// crc_update(&desfire_crc32, byte, 8);
|
|
|
|
// uint32_t crc = crc_finish(&desfire_crc32);
|
2014-09-12 05:23:46 +08:00
|
|
|
|
2019-03-10 18:20:22 +08:00
|
|
|
void OnSuccess() {
|
2019-03-10 03:34:41 +08:00
|
|
|
pcb_blocknum = 0;
|
2019-03-10 07:00:59 +08:00
|
|
|
ReaderTransmit(deselect_cmd, 3, NULL);
|
2019-10-10 04:48:54 +08:00
|
|
|
if (mifare_ultra_halt()) {
|
|
|
|
if (DBGLEVEL >= DBG_ERROR) Dbprintf("Halt error");
|
|
|
|
}
|
2019-03-10 03:34:41 +08:00
|
|
|
switch_off();
|
2014-09-12 05:23:46 +08:00
|
|
|
}
|
|
|
|
|
2019-03-10 18:20:22 +08:00
|
|
|
void OnError(uint8_t reason) {
|
2020-04-08 11:27:54 +08:00
|
|
|
reply_mix(CMD_ACK, 0, reason, 0, 0, 0);
|
2019-03-10 03:34:41 +08:00
|
|
|
OnSuccess();
|
2014-09-12 05:23:46 +08:00
|
|
|
}
|
2020-04-11 03:02:46 +08:00
|
|
|
|
|
|
|
void OnErrorNG(uint16_t cmd, uint8_t reason) {
|
|
|
|
reply_ng(cmd, reason, NULL, 0);
|
|
|
|
OnSuccess();
|
2020-04-11 04:59:55 +08:00
|
|
|
}
|