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