This commit is contained in:
Bjoern Kerler 2020-04-12 17:59:46 +02:00
commit 309ec0d942
3 changed files with 22 additions and 375 deletions

View file

@ -16,6 +16,7 @@
#include "util.h"
#include "mifare.h"
#include "ticks.h"
#include "protocols.h"
#define MAX_APPLICATION_COUNT 28
#define MAX_FILE_COUNT 16
@ -33,8 +34,8 @@ static uint8_t deselect_cmd[] = {0xc2, 0xe0, 0xb4};
/* PCB CID CMD PAYLOAD */
//static uint8_t __res[MAX_FRAME_SIZE];
struct desfire_key defaultkey = {0};
static desfirekey_t sessionkey = &defaultkey;
struct desfire_key skey = {0};
static desfirekey_t sessionkey = &skey;
bool InitDesfireCard() {
@ -159,7 +160,7 @@ void MifareDesfireGetInformation() {
memcpy(payload.uid, card.uid, sizeof(payload.uid));
LED_A_ON();
uint8_t cmd[] = {0x90, GET_VERSION, 0x00, 0x00, 0x00};
uint8_t cmd[] = {0x90, MFDES_GET_VERSION, 0x00, 0x00, 0x00};
size_t cmd_len = sizeof(cmd);
len = DesfireAPDU(cmd, cmd_len, resp);
@ -174,7 +175,7 @@ void MifareDesfireGetInformation() {
memcpy(payload.versionHW, resp + 1, sizeof(payload.versionHW));
// ADDITION_FRAME 1
cmd[1] = ADDITIONAL_FRAME;
cmd[1] = MFDES_ADDITIONAL_FRAME;
len = DesfireAPDU(cmd, cmd_len, resp);
if (!len) {
print_result("ERROR <--: ", resp, len);
@ -271,10 +272,9 @@ void MifareDES_Auth1(uint8_t *datain) {
LED_C_OFF();
if (payload->key == NULL) {
if (payload->algo == MFDES_AUTH_DES) {
if (payload->algo == MFDES_AUTH_DES) {
memcpy(keybytes, PICC_MASTER_KEY8, 8);
} else if (payload->algo == MFDES_ALGO_AES || payload->algo == MFDES_ALGO_3DES ||
payload->algo == MFDES_ALGO_2K3DES) {
} else if (payload->algo == MFDES_ALGO_AES || payload->algo == MFDES_ALGO_3DES) {
memcpy(keybytes, PICC_MASTER_KEY16, 16);
} else if (payload->algo == MFDES_ALGO_3DES) {
memcpy(keybytes, PICC_MASTER_KEY24, 24);
@ -283,6 +283,7 @@ void MifareDES_Auth1(uint8_t *datain) {
memcpy(keybytes, payload->key, payload->keylen);
}
struct desfire_key defaultkey = {0};
desfirekey_t key = &defaultkey;
@ -297,12 +298,12 @@ void MifareDES_Auth1(uint8_t *datain) {
Desfire_3k3des_key_new_with_version(keybytes, key);
}
uint8_t subcommand = AUTHENTICATE;
uint8_t subcommand = MFDES_AUTHENTICATE;
if (payload->mode == MFDES_AUTH_AES)
subcommand = AUTHENTICATE_AES;
subcommand = MFDES_AUTHENTICATE_AES;
else if (payload->mode == MFDES_AUTH_ISO)
subcommand = AUTHENTICATE_ISO;
subcommand = MFDES_AUTHENTICATE_ISO;
if (payload->mode != MFDES_AUTH_PICC) {
// Let's send our auth command
@ -315,7 +316,7 @@ void MifareDES_Auth1(uint8_t *datain) {
cmd[6] = 0x0;
len = DesfireAPDU(cmd, 7, resp);
} else {
cmd[0] = AUTHENTICATE;
cmd[0] = MFDES_AUTHENTICATE;
cmd[1] = payload->keyno;
len = DesfireAPDU(cmd, 2, resp);
}
@ -339,7 +340,7 @@ void MifareDES_Auth1(uint8_t *datain) {
expectedlen = 1 + 16 + 2 + 2;
}
int rndlen = recv_len - 1 - 2 - 2;
int rndlen = len - 1 - 2 - 2;
if (len != expectedlen) {
if (DBGLEVEL >= DBG_ERROR) {
@ -425,7 +426,7 @@ void MifareDES_Auth1(uint8_t *datain) {
}
if (payload->mode != MFDES_AUTH_PICC) {
cmd[0] = 0x90;
cmd[1] = ADDITIONAL_FRAME;
cmd[1] = MFDES_ADDITIONAL_FRAME;
cmd[2] = 0x00;
cmd[3] = 0x00;
cmd[4] = bothlen;
@ -433,7 +434,7 @@ void MifareDES_Auth1(uint8_t *datain) {
cmd[bothlen + 5] = 0x0;
len = DesfireAPDU(cmd, 5 + bothlen + 1, resp);
} else {
cmd[0] = ADDITIONAL_FRAME;
cmd[0] = MFDES_ADDITIONAL_FRAME;
memcpy(cmd + 1, both, bothlen);
len = DesfireAPDU(cmd, 1 + bothlen, resp);
}
@ -465,7 +466,7 @@ void MifareDES_Auth1(uint8_t *datain) {
Desfire_session_key_new(RndA, RndB, key, sessionkey);
if (DBGLEVEL >= DBG_EXTENDED)
print_result("SESSIONKEY : ", sessionKey.data, payload->keylen);
print_result("SESSIONKEY : ", sessionkey->data, payload->keylen);
if (payload->mode != MFDES_AUTH_PICC) {
memcpy(encRndA, resp + 1, rndlen);
@ -603,7 +604,7 @@ void MifareDES_Auth1(uint8_t *datain) {
LED_B_ON();
authres_t rpayload;
rpayload.sessionkeylen = payload->keylen;
memcpy(rpayload.sessionkey, sessionKey.data, rpayload.sessionkeylen);
memcpy(rpayload.sessionkey, sessionkey->data, rpayload.sessionkeylen);
reply_ng(CMD_HF_DESFIRE_AUTH1, PM3_SUCCESS, (uint8_t *)&rpayload, sizeof(rpayload));
LED_B_OFF();
}