]>
Commit | Line | Data |
---|---|---|
1 | #include "mifaredesfire.h" | |
2 | #include "des.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 | |
12 | uint8_t pcb_blocknum = 0; | |
13 | // Deselect card by sending a s-block. the crc is precalced for speed | |
14 | static 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 | ||
20 | bool 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); | |
31 | ||
32 | iso14a_card_select_t *card = (iso14a_card_select_t*)cardbuf; | |
33 | int len = iso14443a_select_card(NULL,card,NULL); | |
34 | ||
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 | ||
45 | // ARG0 flag enums | |
46 | enum { | |
47 | NONE = 0x00, | |
48 | INIT = 0x01, | |
49 | DISCONNECT = 0x02, | |
50 | CLEARTRACE = 0x04, | |
51 | BAR = 0x08, | |
52 | } CmdOptions ; | |
53 | ||
54 | void MifareSendCommand(uint8_t arg0, uint8_t arg1, uint8_t *datain){ | |
55 | ||
56 | /* ARG0 contains flags. | |
57 | 0x01 = init card. | |
58 | 0x02 = Disconnect | |
59 | 0x03 | |
60 | */ | |
61 | uint8_t flags = arg0; | |
62 | size_t datalen = arg1; | |
63 | uint8_t resp[RECEIVE_SIZE]; | |
64 | memset(resp,0,sizeof(resp)); | |
65 | ||
66 | if (MF_DBGLEVEL >= 4) { | |
67 | Dbprintf(" flags : %02X", flags); | |
68 | Dbprintf(" len : %02X", datalen); | |
69 | print_result(" RX : ", datain, datalen); | |
70 | } | |
71 | ||
72 | if ( flags & CLEARTRACE ){ | |
73 | iso14a_clear_trace(); | |
74 | } | |
75 | ||
76 | if ( flags & INIT ){ | |
77 | if ( !InitDesfireCard() ) | |
78 | return; | |
79 | } | |
80 | ||
81 | int len = DesfireAPDU(datain, datalen, resp); | |
82 | if (MF_DBGLEVEL >= 4) { | |
83 | print_result("ERR <--: ", resp, len); | |
84 | } | |
85 | ||
86 | if ( !len ) { | |
87 | OnError(); | |
88 | return; | |
89 | } | |
90 | ||
91 | // reset the pcb_blocknum, | |
92 | pcb_blocknum = 0; | |
93 | ||
94 | if ( flags & DISCONNECT ){ | |
95 | OnSuccess(); | |
96 | } | |
97 | ||
98 | cmd_send(CMD_ACK,1,len,0,resp,len); | |
99 | } | |
100 | ||
101 | void 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]; | |
107 | ||
108 | memset(resp,0,sizeof(resp)); | |
109 | memset(dataout,0, sizeof(dataout)); | |
110 | memset(cardbuf,0,sizeof(cardbuf)); | |
111 | ||
112 | /* | |
113 | 1 = PCB 1 | |
114 | 2 = cid 2 | |
115 | 3 = desfire command 3 | |
116 | 4-5 = crc 4 key | |
117 | 5-6 crc | |
118 | PCB == 0x0A because sending CID byte. | |
119 | CID == 0x00 first card? | |
120 | */ | |
121 | iso14a_clear_trace(); | |
122 | iso14a_set_tracing(TRUE); | |
123 | iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN); | |
124 | ||
125 | // card select - information | |
126 | iso14a_card_select_t *card = (iso14a_card_select_t*)cardbuf; | |
127 | byte_t isOK = iso14443a_select_card(NULL, card, NULL); | |
128 | if ( isOK == 0) { | |
129 | if (MF_DBGLEVEL >= 1) { | |
130 | Dbprintf("Can't select card"); | |
131 | } | |
132 | OnError(); | |
133 | return; | |
134 | } | |
135 | ||
136 | memcpy(dataout,card->uid,7); | |
137 | ||
138 | LED_A_ON(); | |
139 | LED_B_OFF(); | |
140 | LED_C_OFF(); | |
141 | ||
142 | uint8_t cmd[] = {GET_VERSION}; | |
143 | size_t cmd_len = sizeof(cmd); | |
144 | ||
145 | len = DesfireAPDU(cmd, cmd_len, resp); | |
146 | if ( !len ) { | |
147 | print_result("ERROR <--: ", resp, len); | |
148 | OnError(); | |
149 | return; | |
150 | } | |
151 | ||
152 | LED_A_OFF(); | |
153 | LED_B_ON(); | |
154 | memcpy(dataout+7,resp+3,7); | |
155 | ||
156 | // ADDITION_FRAME 1 | |
157 | cmd[0] = ADDITIONAL_FRAME; | |
158 | len = DesfireAPDU(cmd, cmd_len, resp); | |
159 | if ( !len ) { | |
160 | print_result("ERROR <--: ", resp, len); | |
161 | OnError(); | |
162 | return; | |
163 | } | |
164 | ||
165 | LED_B_OFF(); | |
166 | LED_C_ON(); | |
167 | memcpy(dataout+7+7,resp+3,7); | |
168 | ||
169 | // ADDITION_FRAME 2 | |
170 | len = DesfireAPDU(cmd, cmd_len, resp); | |
171 | if ( !len ) { | |
172 | print_result("ERROR <--: ", resp, len); | |
173 | OnError(); | |
174 | return; | |
175 | } | |
176 | ||
177 | memcpy(dataout+7+7+7,resp+3,14); | |
178 | ||
179 | cmd_send(CMD_ACK,1,0,0,dataout,sizeof(dataout)); | |
180 | ||
181 | // reset the pcb_blocknum, | |
182 | pcb_blocknum = 0; | |
183 | OnSuccess(); | |
184 | } | |
185 | ||
186 | void MifareDES_Auth1(uint8_t mode, uint8_t algo, uint8_t keyno, uint8_t *datain){ | |
187 | ||
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 }; | |
191 | uint8_t null_key_data8[8] = {0x00}; | |
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}; | |
195 | ||
196 | //uint8_t* bigbuffer = get_bigbufptr_recvrespbuf(); | |
197 | uint8_t resp[256] = {0x00}; | |
198 | uint8_t IV[16] = {0x00}; | |
199 | ||
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}; | |
208 | ||
209 | InitDesfireCard(); | |
210 | ||
211 | LED_A_ON(); | |
212 | LED_B_OFF(); | |
213 | LED_C_OFF(); | |
214 | ||
215 |