]> git.zerfleddert.de Git - proxmark3-svn/blame_incremental - armsrc/mifaredesfire.c
FIX: an error that I introduced to the csetblock command with wrong length of crc...
[proxmark3-svn] / armsrc / mifaredesfire.c
... / ...
CommitLineData
1#include "mifaredesfire.h"
2#include "BigBuf.h"
3
4#define MAX_APPLICATION_COUNT 28
5#define MAX_FILE_COUNT 16
6#define MAX_DESFIRE_FRAME_SIZE 60
7#define NOT_YET_AUTHENTICATED 255
8#define FRAME_PAYLOAD_SIZE (MAX_DESFIRE_FRAME_SIZE - 5)
9#define RECEIVE_SIZE 64
10
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
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
20bool InitDesfireCard(){
21
22 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);
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;
27
28 int len = iso14443a_select_card(NULL,card,NULL);
29
30 if (!len) {
31 if (MF_DBGLEVEL >= MF_DBG_ERROR)
32 Dbprintf("Can't select card");
33 OnError(1);
34 return false;
35 }
36 return true;
37}
38
39// ARG0 flag enums
40enum {
41 NONE = 0x00,
42 INIT = 0x01,
43 DISCONNECT = 0x02,
44 CLEARTRACE = 0x04,
45 BAR = 0x08,
46} CmdOptions ;
47
48void MifareSendCommand(uint8_t arg0, uint8_t arg1, uint8_t *datain){
49
50 /* ARG0 contains flags.
51 0x01 = init card.
52 0x02 = Disconnect
53 0x03
54 */
55 uint8_t flags = arg0;
56 size_t datalen = arg1;
57 uint8_t resp[RECEIVE_SIZE];
58 memset(resp,0,sizeof(resp));
59
60 if (MF_DBGLEVEL >= 4) {
61 Dbprintf(" flags : %02X", flags);
62 Dbprintf(" len : %02X", datalen);
63 print_result(" RX : ", datain, datalen);
64 }
65
66 if ( flags & CLEARTRACE ){
67 clear_trace();
68 }
69
70 if ( flags & INIT ){
71 if ( !InitDesfireCard() )
72 return;
73 }
74
75 int len = DesfireAPDU(datain, datalen, resp);
76 if (MF_DBGLEVEL >= 4) {
77 print_result("ERR <--: ", resp, len);
78 }
79
80 if ( !len ) {
81 OnError(2);
82 return;
83 }
84
85 // reset the pcb_blocknum,
86 pcb_blocknum = 0;
87
88 if ( flags & DISCONNECT ){
89 OnSuccess();
90 }
91
92 cmd_send(CMD_ACK,1,len,0,resp,len);
93}
94
95void MifareDesfireGetInformation(){
96
97 int len = 0;
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};
101
102 /*
103 1 = PCB 1
104 2 = cid 2
105 3 = desfire command 3
106 4-5 = crc 4 key
107 5-6 crc
108 PCB == 0x0A because sending CID byte.
109 CID == 0x00 first card?
110 */
111 clear_trace();
112 set_tracing(TRUE);
113 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);
114
115 // card select - information
116 iso14a_card_select_t *card = (iso14a_card_select_t*)cardbuf;
117 byte_t isOK = iso14443a_select_card(NULL, card, NULL);
118 if ( isOK == 0) {
119 if (MF_DBGLEVEL >= MF_DBG_ERROR) {
120 Dbprintf("Can't select card");
121 }
122 OnError(1);
123 return;
124 }
125
126 memcpy(dataout,card->uid,7);
127
128 LED_A_ON();
129 LED_B_OFF();
130 LED_C_OFF();
131
132 uint8_t cmd[] = {GET_VERSION};
133 size_t cmd_len = sizeof(cmd);
134
135 len = DesfireAPDU(cmd, cmd_len, resp);
136 if ( !len ) {
137 print_result("ERROR <--: ", resp, len);
138 OnError(2);
139 return;
140 }
141
142 LED_A_OFF();
143 LED_B_ON();
144 memcpy(dataout+7,resp+3,7);
145
146 // ADDITION_FRAME 1
147 cmd[0] = ADDITIONAL_FRAME;
148 len = DesfireAPDU(cmd, cmd_len, resp);
149 if ( !len ) {
150 print_result("ERROR <--: ", resp, len);
151 OnError(2);
152 return;
153 }
154
155 LED_B_OFF();
156 LED_C_ON();
157 memcpy(dataout+7+7,resp+3,7);
158
159 // ADDITION_FRAME 2
160 len = DesfireAPDU(cmd, cmd_len, resp);
161 if ( !len ) {
162 print_result("ERROR <--: ", resp, len);
163 OnError(2);
164 return;
165 }
166
167 memcpy(dataout+7+7+7,resp+3,14);
168
169 cmd_send(CMD_ACK,1,0,0,dataout,sizeof(dataout));
170
171 // reset the pcb_blocknum,
172 pcb_blocknum = 0;
173 OnSuccess();
174}
175
176void MifareDES_Auth1(uint8_t mode, uint8_t algo, uint8_t keyno, uint8_t *datain){
177
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 };
181 uint8_t null_key_data8[8] = {0x00};
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};
185
186 uint8_t resp[256] = {0x00};
187 uint8_t IV[16] = {0x00};
188
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};
197
198 InitDesfireCard();
199
200 LED_A_ON();
201 LED_B_OFF();
202 LED_C_OFF();
203
204