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