]> cvs.zerfleddert.de Git - proxmark3-svn/blame_incremental - armsrc/mifaredesfire.c
CHG: syntax suger
[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,true,0);
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 if ( flags & INIT ){
70 if ( !InitDesfireCard() )
71 return;
72 }
73
74 int len = DesfireAPDU(datain, datalen, resp);
75 if (MF_DBGLEVEL >= 4)
76 print_result("ERR <--: ", resp, len);
77
78 if ( !len ) {
79 OnError(2);
80 return;
81 }
82
83 // reset the pcb_blocknum,
84 pcb_blocknum = 0;
85
86 if ( flags & DISCONNECT )
87 OnSuccess();
88
89 cmd_send(CMD_ACK,1,len,0,resp,len);
90}
91
92void MifareDesfireGetInformation(){
93
94 int len = 0;
95 uint8_t resp[USB_CMD_DATA_SIZE] = {0x00};
96 uint8_t dataout[USB_CMD_DATA_SIZE] = {0x00};
97 byte_t cardbuf[USB_CMD_DATA_SIZE] = {0x00};
98
99 /*
100 1 = PCB 1
101 2 = cid 2
102 3 = desfire command 3
103 4-5 = crc 4 key
104 5-6 crc
105 PCB == 0x0A because sending CID byte.
106 CID == 0x00 first card?
107 */
108 clear_trace();
109 set_tracing(TRUE);
110 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);
111
112 // card select - information
113 iso14a_card_select_t *card = (iso14a_card_select_t*)cardbuf;
114 byte_t isOK = iso14443a_select_card(NULL, card, NULL, true, 0);
115 if ( isOK == 0) {
116 if (MF_DBGLEVEL >= MF_DBG_ERROR) {
117 Dbprintf("Can't select card");
118 }
119 OnError(1);
120 return;
121 }
122
123 memcpy(dataout,card->uid,7);
124
125 LED_A_ON();
126 LED_B_OFF();
127 LED_C_OFF();
128
129 uint8_t cmd[] = {GET_VERSION};
130 size_t cmd_len = sizeof(cmd);
131
132 len = DesfireAPDU(cmd, cmd_len, resp);
133 if ( !len ) {
134 print_result("ERROR <--: ", resp, len);
135 OnError(2);
136 return;
137 }
138
139 LED_A_OFF();
140 LED_B_ON();
141 memcpy(dataout+7,resp+3,7);
142
143 // ADDITION_FRAME 1
144 cmd[0] = ADDITIONAL_FRAME;
145 len = DesfireAPDU(cmd, cmd_len, resp);
146 if ( !len ) {
147 print_result("ERROR <--: ", resp, len);
148 OnError(2);
149 return;
150 }
151
152 LED_B_OFF();
153 LED_C_ON();
154 memcpy(dataout+7+7,resp+3,7);
155
156 // ADDITION_FRAME 2
157 len = DesfireAPDU(cmd, cmd_len, resp);
158 if ( !len ) {
159 print_result("ERROR <--: ", resp, len);
160 OnError(2);
161 return;
162 }
163
164 memcpy(dataout+7+7+7,resp+3,14);
165
166 cmd_send(CMD_ACK,1,0,0,dataout,sizeof(dataout));
167
168 // reset the pcb_blocknum,
169 pcb_blocknum = 0;
170 OnSuccess();
171}
172
173void MifareDES_Auth1(uint8_t mode, uint8_t algo, uint8_t keyno, uint8_t *datain){
174
175 int len = 0;
176 //uint8_t PICC_MASTER_KEY8[8] = { 0x40,0x41,0x42,0x43,0x44,0x45,0x46,0x47};
177 uint8_t PICC_MASTER_KEY16[16] = { 0x40,0x41,0x42,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x4a,0x4b,0x4c,0x4d,0x4e,0x4f };
178 uint8_t null_key_data8[8] = {0x00};
179 //uint8_t null_key_data16[16] = {0x00};
180 //uint8_t new_key_data8[8] = { 0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77};
181 //uint8_t new_key_data16[16] = { 0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,0x99,0xAA,0xBB,0xCC,0xDD,0xEE,0xFF};
182
183 uint8_t resp[256] = {0x00};
184 uint8_t IV[16] = {0x00};
185
186 size_t datalen = datain[0];
187
188 uint8_t cmd[40] = {0x00};
189 uint8_t encRndB[16] = {0x00};
190 uint8_t decRndB[16] = {0x00};
191 uint8_t nonce[16] = {0x00};
192 uint8_t both[32] = {0x00};
193 uint8_t encBoth[32] = {0x00};
194
195 InitDesfireCard();
196
197 LED_A_ON();
198 LED_B_OFF();
199 LED_C_OFF();
200
201