]> git.zerfleddert.de Git - proxmark3-svn/blame - armsrc/mifaredesfire.c
FIX: minor fixes in hf mfu, from @marshmello42 's branch.
[proxmark3-svn] / armsrc / mifaredesfire.c
CommitLineData
f38a1528 1#include "mifaredesfire.h"
0ec548dc 2#include "BigBuf.h"
f38a1528 3
4#define MAX_APPLICATION_COUNT 28
5#define MAX_FILE_COUNT 16
a501c82b 6#define MAX_DESFIRE_FRAME_SIZE 60
f38a1528 7#define NOT_YET_AUTHENTICATED 255
a501c82b 8#define FRAME_PAYLOAD_SIZE (MAX_DESFIRE_FRAME_SIZE - 5)
9#define RECEIVE_SIZE 64
f38a1528 10
313ee67e 11// the block number for the ISO14443-4 PCB
12uint8_t pcb_blocknum = 0;
13// Deselect card by sending a s-block. the crc is precalced for speed
14static uint8_t deselect_cmd[] = {0xc2,0xe0,0xb4};
15
f38a1528 16//static uint8_t __msg[MAX_FRAME_SIZE] = { 0x0A, 0x00, 0x00, /* ..., */ 0x00 };
17/* PCB CID CMD PAYLOAD */
18//static uint8_t __res[MAX_FRAME_SIZE];
19
313ee67e 20bool InitDesfireCard(){
e3ab50ca 21
313ee67e 22 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);
99cf19d9 23 set_tracing(TRUE);
24
25 byte_t cardbuf[USB_CMD_DATA_SIZE] = {0x00};
26 iso14a_card_select_t *card = (iso14a_card_select_t*)cardbuf;
f38a1528 27
c188b1b9 28 int len = iso14443a_select_card(NULL,card,NULL,true,0);
f38a1528 29
313ee67e 30 if (!len) {
225ccb91 31 if (MF_DBGLEVEL >= MF_DBG_ERROR)
32 Dbprintf("Can't select card");
33 OnError(1);
313ee67e 34 return false;
35 }
36 return true;
37}
38
75465377 39// ARG0 flag enums
40enum {
41 NONE = 0x00,
42 INIT = 0x01,
43 DISCONNECT = 0x02,
f6c18637 44 CLEARTRACE = 0x04,
75465377 45 BAR = 0x08,
46} CmdOptions ;
47
313ee67e 48void MifareSendCommand(uint8_t arg0, uint8_t arg1, uint8_t *datain){
49
50 /* ARG0 contains flags.
51 0x01 = init card.
f6c18637 52 0x02 = Disconnect
313ee67e 53 0x03
54 */
55 uint8_t flags = arg0;
56 size_t datalen = arg1;
a501c82b 57 uint8_t resp[RECEIVE_SIZE];
313ee67e 58 memset(resp,0,sizeof(resp));
59
60 if (MF_DBGLEVEL >= 4) {
75465377 61 Dbprintf(" flags : %02X", flags);
62 Dbprintf(" len : %02X", datalen);
63 print_result(" RX : ", datain, datalen);
313ee67e 64 }
65
f6c18637 66 if ( flags & CLEARTRACE ){
0ec548dc 67 clear_trace();
f6c18637 68 }
69
75465377 70 if ( flags & INIT ){
313ee67e 71 if ( !InitDesfireCard() )
72 return;
73 }
74
75 int len = DesfireAPDU(datain, datalen, resp);
f5ed4d12 76 if (MF_DBGLEVEL >= 4) {
77 print_result("ERR <--: ", resp, len);
78 }
f6c18637 79
80 if ( !len ) {
225ccb91 81 OnError(2);
313ee67e 82 return;
83 }
313ee67e 84
75465377 85 // reset the pcb_blocknum,
86 pcb_blocknum = 0;
87
f6c18637 88 if ( flags & DISCONNECT ){
75465377 89 OnSuccess();
f6c18637 90 }
75465377 91
92 cmd_send(CMD_ACK,1,len,0,resp,len);
313ee67e 93}
94
95void MifareDesfireGetInformation(){
96
97 int len = 0;
e3ab50ca 98 uint8_t resp[USB_CMD_DATA_SIZE] = {0x00};
99 uint8_t dataout[USB_CMD_DATA_SIZE] = {0x00};
100 byte_t cardbuf[USB_CMD_DATA_SIZE] = {0x00};
f38a1528 101
102 /*
103 1 = PCB 1
104 2 = cid 2
105 3 = desfire command 3
106 4-5 = crc 4 key
313ee67e 107 5-6 crc
f38a1528 108 PCB == 0x0A because sending CID byte.
313ee67e 109 CID == 0x00 first card?
f38a1528 110 */
0ec548dc 111 clear_trace();
112 set_tracing(TRUE);
f38a1528 113 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);
114
115 // card select - information
313ee67e 116 iso14a_card_select_t *card = (iso14a_card_select_t*)cardbuf;
c188b1b9 117 byte_t isOK = iso14443a_select_card(NULL, card, NULL, true, 0);
f5ed4d12 118 if ( isOK == 0) {
225ccb91 119 if (MF_DBGLEVEL >= MF_DBG_ERROR) {
f38a1528 120 Dbprintf("Can't select card");
121 }
225ccb91 122 OnError(1);
f38a1528 123 return;
124 }
125
f38a1528 126 memcpy(dataout,card->uid,7);
127
128 LED_A_ON();
129 LED_B_OFF();
130 LED_C_OFF();
131
313ee67e 132 uint8_t cmd[] = {GET_VERSION};
133 size_t cmd_len = sizeof(cmd);
134
135 len = DesfireAPDU(cmd, cmd_len, resp);
136 if ( !len ) {
f38a1528 137 print_result("ERROR <--: ", resp, len);
225ccb91 138 OnError(2);
f38a1528 139 return;
140 }
313ee67e 141
142 LED_A_OFF();
143 LED_B_ON();
f38a1528 144 memcpy(dataout+7,resp+3,7);
145
146 // ADDITION_FRAME 1
313ee67e 147 cmd[0] = ADDITIONAL_FRAME;
148 len = DesfireAPDU(cmd, cmd_len, resp);
149 if ( !len ) {
f38a1528 150 print_result("ERROR <--: ", resp, len);
225ccb91 151 OnError(2);
f38a1528 152 return;
153 }
313ee67e 154
155 LED_B_OFF();
156 LED_C_ON();
f38a1528 157 memcpy(dataout+7+7,resp+3,7);
158
159 // ADDITION_FRAME 2
313ee67e 160 len = DesfireAPDU(cmd, cmd_len, resp);
161 if ( !len ) {
f38a1528 162 print_result("ERROR <--: ", resp, len);
225ccb91 163 OnError(2);
f38a1528 164 return;
165 }
166
167 memcpy(dataout+7+7+7,resp+3,14);
168
f38a1528 169 cmd_send(CMD_ACK,1,0,0,dataout,sizeof(dataout));
313ee67e 170
171 // reset the pcb_blocknum,
172 pcb_blocknum = 0;
f38a1528 173 OnSuccess();
174}
175
176void MifareDES_Auth1(uint8_t mode, uint8_t algo, uint8_t keyno, uint8_t *datain){
177
f6c18637 178 int len = 0;
179 //uint8_t PICC_MASTER_KEY8[8] = { 0x40,0x41,0x42,0x43,0x44,0x45,0x46,0x47};
180 uint8_t PICC_MASTER_KEY16[16] = { 0x40,0x41,0x42,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x4a,0x4b,0x4c,0x4d,0x4e,0x4f };
b6f41bfd 181 uint8_t null_key_data8[8] = {0x00};
f6c18637 182 //uint8_t null_key_data16[16] = {0x00};
183 //uint8_t new_key_data8[8] = { 0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77};
184 //uint8_t new_key_data16[16] = { 0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,0x99,0xAA,0xBB,0xCC,0xDD,0xEE,0xFF};
f38a1528 185
f6c18637 186 uint8_t resp[256] = {0x00};
187 uint8_t IV[16] = {0x00};
f38a1528 188
f6c18637 189 size_t datalen = datain[0];
190
191 uint8_t cmd[40] = {0x00};
192 uint8_t encRndB[16] = {0x00};
193 uint8_t decRndB[16] = {0x00};
194 uint8_t nonce[16] = {0x00};
195 uint8_t both[32] = {0x00};
196 uint8_t encBoth[32] = {0x00};
f38a1528 197
f6c18637 198 InitDesfireCard();
f38a1528 199
857bc2ff 200 LED_A_ON();
201 LED_B_OFF();
202 LED_C_OFF();
203
f38a1528 204