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