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