]>
Commit | Line | Data |
---|---|---|
8556b852 | 1 | //-----------------------------------------------------------------------------\r |
b62a5a84 | 2 | // Merlok - June 2011, 2012\r |
8556b852 M |
3 | // Gerhard de Koning Gans - May 2008\r |
4 | // Hagen Fritsch - June 2010\r | |
3fe4ff4f | 5 | // Midnitesnake - Dec 2013\r |
6 | // Andy Davies - Apr 2014\r | |
7 | // Iceman - May 2014\r | |
8556b852 M |
8 | //\r |
9 | // This code is licensed to you under the terms of the GNU GPL, version 2 or,\r | |
10 | // at your option, any later version. See the LICENSE.txt file for the text of\r | |
11 | // the license.\r | |
12 | //-----------------------------------------------------------------------------\r | |
13 | // Routines to support ISO 14443 type A.\r | |
14 | //-----------------------------------------------------------------------------\r | |
15 | \r | |
16 | #include "mifarecmd.h"\r | |
17 | #include "apps.h"\r | |
787b5bd8 | 18 | #include "util.h"\r |
0ec548dc | 19 | //#include "../client/loclass/des.h"\r |
20 | #include "des.h"\r | |
a631936e | 21 | #include "crc.h"\r |
22 | \r | |
8556b852 | 23 | //-----------------------------------------------------------------------------\r |
baeaf579 | 24 | // Select, Authenticate, Read a MIFARE tag. \r |
8556b852 M |
25 | // read block\r |
26 | //-----------------------------------------------------------------------------\r | |
27 | void MifareReadBlock(uint8_t arg0, uint8_t arg1, uint8_t arg2, uint8_t *datain)\r | |
28 | {\r | |
29 | // params\r | |
30 | uint8_t blockNo = arg0;\r | |
31 | uint8_t keyType = arg1;\r | |
32 | uint64_t ui64Key = 0;\r | |
33 | ui64Key = bytes_to_num(datain, 6);\r | |
34 | \r | |
35 | // variables\r | |
36 | byte_t isOK = 0;\r | |
37 | byte_t dataoutbuf[16];\r | |
1c611bbd | 38 | uint8_t uid[10];\r |
8556b852 M |
39 | uint32_t cuid;\r |
40 | struct Crypto1State mpcs = {0, 0};\r | |
41 | struct Crypto1State *pcs;\r | |
42 | pcs = &mpcs;\r | |
43 | \r | |
44 | // clear trace\r | |
3000dc4e | 45 | clear_trace();\r |
7bc95e2e | 46 | iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r |
8556b852 M |
47 | \r |
48 | LED_A_ON();\r | |
49 | LED_B_OFF();\r | |
50 | LED_C_OFF();\r | |
51 | \r | |
52 | while (true) {\r | |
53 | if(!iso14443a_select_card(uid, NULL, &cuid)) {\r | |
baeaf579 | 54 | if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");\r |
8556b852 M |
55 | break;\r |
56 | };\r | |
57 | \r | |
58 | if(mifare_classic_auth(pcs, cuid, blockNo, keyType, ui64Key, AUTH_FIRST)) {\r | |
baeaf579 | 59 | if (MF_DBGLEVEL >= 1) Dbprintf("Auth error");\r |
8556b852 M |
60 | break;\r |
61 | };\r | |
62 | \r | |
63 | if(mifare_classic_readblock(pcs, cuid, blockNo, dataoutbuf)) {\r | |
baeaf579 | 64 | if (MF_DBGLEVEL >= 1) Dbprintf("Read block error");\r |
8556b852 M |
65 | break;\r |
66 | };\r | |
67 | \r | |
68 | if(mifare_classic_halt(pcs, cuid)) {\r | |
baeaf579 | 69 | if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");\r |
8556b852 M |
70 | break;\r |
71 | };\r | |
72 | \r | |
73 | isOK = 1;\r | |
74 | break;\r | |
75 | }\r | |
76 | \r | |
77 | // ----------------------------- crypto1 destroy\r | |
78 | crypto1_destroy(pcs);\r | |
79 | \r | |
80 | if (MF_DBGLEVEL >= 2) DbpString("READ BLOCK FINISHED");\r | |
81 | \r | |
8556b852 | 82 | LED_B_ON();\r |
baeaf579 | 83 | cmd_send(CMD_ACK,isOK,0,0,dataoutbuf,16);\r |
8556b852 M |
84 | LED_B_OFF();\r |
85 | \r | |
a631936e | 86 | FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r |
87 | LEDsoff();\r | |
88 | }\r | |
89 | \r | |
90 | \r | |
91 | void MifareUC_Auth1(uint8_t arg0, uint8_t *datain){\r | |
92 | \r | |
a631936e | 93 | byte_t dataoutbuf[16] = {0x00};\r |
94 | uint8_t uid[10] = {0x00};\r | |
aa60d156 | 95 | uint32_t cuid = 0x00;\r |
a631936e | 96 | \r |
aa60d156 | 97 | LED_A_ON(); LED_B_OFF(); LED_C_OFF();\r |
a631936e | 98 | \r |
3000dc4e | 99 | clear_trace();\r |
a631936e | 100 | iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r |
101 | \r | |
102 | if(!iso14443a_select_card(uid, NULL, &cuid)) {\r | |
aa60d156 | 103 | if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Can't select card");\r |
0ec548dc | 104 | OnError(0);\r |
a631936e | 105 | return;\r |
106 | };\r | |
107 | \r | |
aa60d156 | 108 | if(mifare_ultra_auth1(dataoutbuf)){\r |
109 | if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Authentication part1: Fail.");\r | |
0ec548dc | 110 | OnError(1);\r |
a631936e | 111 | return;\r |
112 | }\r | |
113 | \r | |
aa60d156 | 114 | if (MF_DBGLEVEL >= MF_DBG_EXTENDED) DbpString("AUTH 1 FINISHED");\r |
a631936e | 115 | \r |
aa60d156 | 116 | cmd_send(CMD_ACK,1,cuid,0,dataoutbuf,11);\r |
a631936e | 117 | LEDsoff();\r |
118 | }\r | |
119 | void MifareUC_Auth2(uint32_t arg0, uint8_t *datain){\r | |
120 | \r | |
a631936e | 121 | uint8_t key[16] = {0x00};\r |
a631936e | 122 | byte_t dataoutbuf[16] = {0x00};\r |
123 | \r | |
124 | memcpy(key, datain, 16);\r | |
125 | \r | |
aa60d156 | 126 | LED_A_ON(); LED_B_OFF(); LED_C_OFF();\r |
a631936e | 127 | \r |
aa60d156 | 128 | if(mifare_ultra_auth2(key, dataoutbuf)){\r |
129 | if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Authentication part2: Fail...");\r | |
0ec548dc | 130 | OnError(1);\r |
a631936e | 131 | return; \r |
132 | }\r | |
133 | \r | |
aa60d156 | 134 | if (MF_DBGLEVEL >= MF_DBG_EXTENDED) DbpString("AUTH 2 FINISHED");\r |
a631936e | 135 | \r |
aa60d156 | 136 | cmd_send(CMD_ACK,1,0,0,dataoutbuf,11);\r |
8556b852 M |
137 | FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r |
138 | LEDsoff();\r | |
7cc204bf | 139 | }\r |
140 | \r | |
0ec548dc | 141 | void MifareUReadBlock(uint8_t arg0, uint8_t arg1, uint8_t *datain)\r |
7cc204bf | 142 | {\r |
7cc204bf | 143 | uint8_t blockNo = arg0;\r |
787b5bd8 | 144 | byte_t dataout[16] = {0x00};\r |
145 | uint8_t uid[10] = {0x00};\r | |
aa60d156 | 146 | uint8_t key[16] = {0x00};\r |
147 | bool usePwd = (arg1 == 1);\r | |
0ec548dc | 148 | \r |
aa60d156 | 149 | LED_A_ON(); LED_B_OFF(); LED_C_OFF();\r |
7cc204bf | 150 | \r |
3000dc4e | 151 | clear_trace();\r |
787b5bd8 | 152 | iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r |
153 | \r | |
aa60d156 | 154 | int len = iso14443a_select_card(uid, NULL, NULL);\r |
787b5bd8 | 155 | if(!len) {\r |
aa60d156 | 156 | if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Can't select card (RC:%02X)",len);\r |
0ec548dc | 157 | OnError(1);\r |
787b5bd8 | 158 | return;\r |
aa60d156 | 159 | }\r |
0ec548dc | 160 | \r |
161 | // authenticate here.\r | |
162 | if ( usePwd ) {\r | |
0ec548dc | 163 | \r |
aa60d156 | 164 | memcpy(key, datain, 16);\r |
165 | \r | |
166 | // Dbprintf("KEY: %02x %02x %02x %02x %02x %02x %02x %02x", key[0],key[1],key[2],key[3],key[4],key[5],key[6],key[7] );\r | |
167 | // Dbprintf("KEY: %02x %02x %02x %02x %02x %02x %02x %02x", key[8],key[9],key[10],key[11],key[12],key[13],key[14],key[15] );\r | |
168 | \r | |
169 | uint8_t a[8] = {1,1,1,1,1,1,1,1 };\r | |
170 | uint8_t b[8] = {0x00};\r | |
171 | uint8_t enc_b[8] = {0x00};\r | |
172 | uint8_t ab[16] = {0x00};\r | |
173 | uint8_t enc_ab[16] = {0x00}; \r | |
174 | uint8_t enc_key[8] = {0x00};\r | |
0ec548dc | 175 | \r |
176 | uint16_t len;\r | |
aa60d156 | 177 | uint8_t receivedAnswer[MAX_FRAME_SIZE];\r |
178 | uint8_t receivedAnswerPar[MAX_PARITY_SIZE];\r | |
0ec548dc | 179 | \r |
180 | len = mifare_sendcmd_short(NULL, 1, 0x1A, 0x00, receivedAnswer,receivedAnswerPar ,NULL);\r | |
aa60d156 | 181 | if (len != 11) {\r |
182 | if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Cmd Error: %02x", receivedAnswer[0]);\r | |
183 | OnError(1);\r | |
184 | return;\r | |
0ec548dc | 185 | }\r |
aa60d156 | 186 | \r |
0ec548dc | 187 | // tag nonce.\r |
188 | memcpy(enc_b,receivedAnswer+1,8);\r | |
189 | \r | |
190 | // decrypt nonce.\r | |
aa60d156 | 191 | tdes_2key_dec(b, enc_b, 8, key );\r |
0ec548dc | 192 | \r |
193 | Dbprintf("enc_B: %02x %02x %02x %02x %02x %02x %02x %02x", enc_b[0],enc_b[1],enc_b[2],enc_b[3],enc_b[4],enc_b[5],enc_b[6],enc_b[7] );\r | |
aa60d156 | 194 | Dbprintf(" B: %02x %02x %02x %02x %02x %02x %02x %02x", b[0],b[1],b[2],b[3],b[4],b[5],b[6],b[7] );\r |
0ec548dc | 195 | rol(b,8);\r |
196 | \r | |
197 | memcpy(ab ,a,8);\r | |
198 | memcpy(ab+8,b,8);\r | |
199 | \r | |
200 | Dbprintf("AB: %02x %02x %02x %02x %02x %02x %02x %02x", ab[0],ab[1],ab[2],ab[3],ab[4],ab[5],ab[6],ab[7] );\r | |
201 | Dbprintf("AB: %02x %02x %02x %02x %02x %02x %02x %02x", ab[8],ab[9],ab[10],ab[11],ab[12],ab[13],ab[14],ab[15] );\r | |
202 | \r | |
203 | // encrypt\r | |
aa60d156 | 204 | tdes_2key_enc(enc_ab, ab, 16, key);\r |
0ec548dc | 205 | \r |
aa60d156 | 206 | Dbprintf("e_AB: %02x %02x %02x %02x %02x %02x %02x %02x", enc_ab[0],enc_ab[1],enc_ab[2],enc_ab[3],enc_ab[4],enc_ab[5],enc_ab[6],enc_ab[7] );\r |
207 | Dbprintf("e_enc_ab: %02x %02x %02x %02x %02x %02x %02x %02x", enc_ab[8],enc_ab[9],enc_ab[10],enc_ab[11],enc_ab[12],enc_ab[13],enc_ab[14],enc_ab[15] );\r | |
0ec548dc | 208 | \r |
aa60d156 | 209 | len = mifare_sendcmd_short_mfucauth(NULL, 1, 0xAF, enc_ab, receivedAnswer, receivedAnswerPar, NULL);\r |
210 | if (len != 11) {\r | |
211 | if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Cmd Error: %02x", receivedAnswer[0]);\r | |
0ec548dc | 212 | OnError(1);\r |
213 | return;\r | |
214 | }\r | |
215 | \r | |
aa60d156 | 216 | // the tags' encryption of our nonce, A.\r |
217 | memcpy(enc_key, receivedAnswer+1, 8);\r | |
218 | \r | |
219 | // clear B.\r | |
220 | memset(b, 0x00, 8);\r | |
221 | \r | |
222 | // decrypt \r | |
223 | tdes_2key_dec(b, enc_key, 8, key );\r | |
224 | if ( memcmp(a, b, 8) == 0 )\r | |
225 | Dbprintf("Verified key");\r | |
226 | else\r | |
227 | Dbprintf("failed authentication");\r | |
228 | \r | |
229 | Dbprintf("a: %02x %02x %02x %02x %02x %02x %02x %02x", a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7] );\r | |
230 | Dbprintf("b: %02x %02x %02x %02x %02x %02x %02x %02x", b[0],b[1],b[2],b[3],b[4],b[5],b[6],b[7] );\r | |
0ec548dc | 231 | }\r |
232 | \r | |
aa60d156 | 233 | if( mifare_ultra_readblock(blockNo, dataout) ) {\r |
234 | if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Read block error");\r | |
0ec548dc | 235 | OnError(2);\r |
787b5bd8 | 236 | return;\r |
aa60d156 | 237 | }\r |
7cc204bf | 238 | \r |
aa60d156 | 239 | if( mifare_ultra_halt() ) {\r |
240 | if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Halt error");\r | |
0ec548dc | 241 | OnError(3);\r |
787b5bd8 | 242 | return;\r |
aa60d156 | 243 | }\r |
7cc204bf | 244 | \r |
787b5bd8 | 245 | cmd_send(CMD_ACK,1,0,0,dataout,16);\r |
7cc204bf | 246 | FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r |
247 | LEDsoff();\r | |
248 | }\r | |
7cc204bf | 249 | //-----------------------------------------------------------------------------\r |
baeaf579 | 250 | // Select, Authenticate, Read a MIFARE tag. \r |
251 | // read sector (data = 4 x 16 bytes = 64 bytes, or 16 x 16 bytes = 256 bytes)\r | |
8556b852 M |
252 | //-----------------------------------------------------------------------------\r |
253 | void MifareReadSector(uint8_t arg0, uint8_t arg1, uint8_t arg2, uint8_t *datain)\r | |
254 | {\r | |
255 | // params\r | |
256 | uint8_t sectorNo = arg0;\r | |
257 | uint8_t keyType = arg1;\r | |
258 | uint64_t ui64Key = 0;\r | |
259 | ui64Key = bytes_to_num(datain, 6);\r | |
260 | \r | |
261 | // variables\r | |
3fe4ff4f | 262 | byte_t isOK = 0;\r |
baeaf579 | 263 | byte_t dataoutbuf[16 * 16];\r |
1c611bbd | 264 | uint8_t uid[10];\r |
8556b852 M |
265 | uint32_t cuid;\r |
266 | struct Crypto1State mpcs = {0, 0};\r | |
267 | struct Crypto1State *pcs;\r | |
268 | pcs = &mpcs;\r | |
269 | \r | |
270 | // clear trace\r | |
3000dc4e | 271 | clear_trace();\r |
8556b852 | 272 | \r |
7bc95e2e | 273 | iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r |
8556b852 M |
274 | \r |
275 | LED_A_ON();\r | |
276 | LED_B_OFF();\r | |
277 | LED_C_OFF();\r | |
278 | \r | |
baeaf579 | 279 | isOK = 1;\r |
280 | if(!iso14443a_select_card(uid, NULL, &cuid)) {\r | |
281 | isOK = 0;\r | |
8556b852 | 282 | if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");\r |
baeaf579 | 283 | }\r |
8556b852 | 284 | \r |
baeaf579 | 285 | \r |
286 | if(isOK && mifare_classic_auth(pcs, cuid, FirstBlockOfSector(sectorNo), keyType, ui64Key, AUTH_FIRST)) {\r | |
287 | isOK = 0;\r | |
8556b852 | 288 | if (MF_DBGLEVEL >= 1) Dbprintf("Auth error");\r |
baeaf579 | 289 | }\r |
290 | \r | |
291 | for (uint8_t blockNo = 0; isOK && blockNo < NumBlocksPerSector(sectorNo); blockNo++) {\r | |
292 | if(mifare_classic_readblock(pcs, cuid, FirstBlockOfSector(sectorNo) + blockNo, dataoutbuf + 16 * blockNo)) {\r | |
293 | isOK = 0;\r | |
294 | if (MF_DBGLEVEL >= 1) Dbprintf("Read sector %2d block %2d error", sectorNo, blockNo);\r | |
8556b852 | 295 | break;\r |
baeaf579 | 296 | }\r |
297 | }\r | |
8556b852 | 298 | \r |
baeaf579 | 299 | if(mifare_classic_halt(pcs, cuid)) {\r |
8556b852 | 300 | if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");\r |
8556b852 | 301 | }\r |
baeaf579 | 302 | \r |
8556b852 M |
303 | // ----------------------------- crypto1 destroy\r |
304 | crypto1_destroy(pcs);\r | |
305 | \r | |
306 | if (MF_DBGLEVEL >= 2) DbpString("READ SECTOR FINISHED");\r | |
307 | \r | |
8556b852 | 308 | LED_B_ON();\r |
baeaf579 | 309 | cmd_send(CMD_ACK,isOK,0,0,dataoutbuf,16*NumBlocksPerSector(sectorNo));\r |
6e82300d | 310 | LED_B_OFF();\r |
8556b852 M |
311 | \r |
312 | // Thats it...\r | |
313 | FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r | |
314 | LEDsoff();\r | |
7cc204bf | 315 | }\r |
316 | \r | |
787b5bd8 | 317 | void MifareUReadCard(uint8_t arg0, int arg1, uint8_t *datain)\r |
7cc204bf | 318 | {\r |
0ec548dc | 319 | // params\r |
320 | uint8_t sectorNo = arg0;\r | |
787b5bd8 | 321 | int Pages = arg1;\r |
aa60d156 | 322 | int countpages = 0;\r |
787b5bd8 | 323 | byte_t dataout[176] = {0x00};;\r |
aa60d156 | 324 | uint32_t cuid = 0x00;\r |
7cc204bf | 325 | \r |
aa60d156 | 326 | LED_A_ON(); LED_B_OFF(); LED_C_OFF();\r |
3000dc4e | 327 | clear_trace();\r |
787b5bd8 | 328 | iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r |
7cc204bf | 329 | \r |
aa60d156 | 330 | int len = iso14443a_select_card(NULL, NULL, &cuid);\r |
787b5bd8 | 331 | if (!len) {\r |
aa60d156 | 332 | if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Can't select card (RC:%d)",len);\r |
0ec548dc | 333 | OnError(1);\r |
787b5bd8 | 334 | return;\r |
335 | }\r | |
336 | \r | |
337 | for (int i = 0; i < Pages; i++){\r | |
338 | \r | |
aa60d156 | 339 | len = mifare_ultra_readblock(sectorNo * 4 + i, dataout + 4 * i);\r |
787b5bd8 | 340 | \r |
341 | if (len) {\r | |
aa60d156 | 342 | if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Read block %d error",i);\r |
0ec548dc | 343 | OnError(2);\r |
787b5bd8 | 344 | return;\r |
345 | } else {\r | |
aa60d156 | 346 | countpages++;\r |
787b5bd8 | 347 | }\r |
348 | }\r | |
349 | \r | |
aa60d156 | 350 | len = mifare_ultra_halt();\r |
787b5bd8 | 351 | if (len) {\r |
aa60d156 | 352 | if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Halt error");\r |
0ec548dc | 353 | OnError(3);\r |
787b5bd8 | 354 | return;\r |
355 | }\r | |
356 | \r | |
aa60d156 | 357 | if (MF_DBGLEVEL >= MF_DBG_ALL) Dbprintf("Pages read %d", countpages);\r |
7cc204bf | 358 | \r |
787b5bd8 | 359 | len = 16*4; //64 bytes\r |
aa60d156 | 360 | \r |
787b5bd8 | 361 | // Read a UL-C\r |
aa60d156 | 362 | if (Pages == 44 && countpages > 16) \r |
787b5bd8 | 363 | len = 176;\r |
7cc204bf | 364 | \r |
787b5bd8 | 365 | cmd_send(CMD_ACK, 1, 0, 0, dataout, len); \r |
31d1caa5 MHS |
366 | FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r |
367 | LEDsoff();\r | |
7cc204bf | 368 | }\r |
369 | \r | |
370 | \r | |
371 | //-----------------------------------------------------------------------------\r | |
baeaf579 | 372 | // Select, Authenticate, Write a MIFARE tag. \r |
7cc204bf | 373 | // read block\r |
8556b852 M |
374 | //-----------------------------------------------------------------------------\r |
375 | void MifareWriteBlock(uint8_t arg0, uint8_t arg1, uint8_t arg2, uint8_t *datain)\r | |
376 | {\r | |
377 | // params\r | |
378 | uint8_t blockNo = arg0;\r | |
379 | uint8_t keyType = arg1;\r | |
380 | uint64_t ui64Key = 0;\r | |
381 | byte_t blockdata[16];\r | |
382 | \r | |
383 | ui64Key = bytes_to_num(datain, 6);\r | |
384 | memcpy(blockdata, datain + 10, 16);\r | |
385 | \r | |
386 | // variables\r | |
387 | byte_t isOK = 0;\r | |
1c611bbd | 388 | uint8_t uid[10];\r |
8556b852 M |
389 | uint32_t cuid;\r |
390 | struct Crypto1State mpcs = {0, 0};\r | |
391 | struct Crypto1State *pcs;\r | |
392 | pcs = &mpcs;\r | |
393 | \r | |
394 | // clear trace\r | |
3000dc4e | 395 | clear_trace();\r |
8556b852 | 396 | \r |
7bc95e2e | 397 | iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r |
8556b852 M |
398 | \r |
399 | LED_A_ON();\r | |
400 | LED_B_OFF();\r | |
401 | LED_C_OFF();\r | |
402 | \r | |
403 | while (true) {\r | |
404 | if(!iso14443a_select_card(uid, NULL, &cuid)) {\r | |
405 | if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");\r | |
406 | break;\r | |
407 | };\r | |
408 | \r | |
409 | if(mifare_classic_auth(pcs, cuid, blockNo, keyType, ui64Key, AUTH_FIRST)) {\r | |
410 | if (MF_DBGLEVEL >= 1) Dbprintf("Auth error");\r | |
411 | break;\r | |
412 | };\r | |
413 | \r | |
414 | if(mifare_classic_writeblock(pcs, cuid, blockNo, blockdata)) {\r | |
415 | if (MF_DBGLEVEL >= 1) Dbprintf("Write block error");\r | |
416 | break;\r | |
417 | };\r | |
418 | \r | |
419 | if(mifare_classic_halt(pcs, cuid)) {\r | |
420 | if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");\r | |
421 | break;\r | |
422 | };\r | |
423 | \r | |
424 | isOK = 1;\r | |
425 | break;\r | |
426 | }\r | |
427 | \r | |
428 | // ----------------------------- crypto1 destroy\r | |
429 | crypto1_destroy(pcs);\r | |
430 | \r | |
431 | if (MF_DBGLEVEL >= 2) DbpString("WRITE BLOCK FINISHED");\r | |
432 | \r | |
8556b852 | 433 | LED_B_ON();\r |
9492e0b0 | 434 | cmd_send(CMD_ACK,isOK,0,0,0,0);\r |
6e82300d | 435 | LED_B_OFF();\r |
8556b852 M |
436 | \r |
437 | \r | |
438 | // Thats it...\r | |
439 | FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r | |
440 | LEDsoff();\r | |
7cc204bf | 441 | }\r |
442 | \r | |
443 | void MifareUWriteBlock(uint8_t arg0, uint8_t *datain)\r | |
444 | {\r | |
aa60d156 | 445 | uint8_t blockNo = arg0;\r |
787b5bd8 | 446 | byte_t blockdata[16] = {0x00};\r |
7cc204bf | 447 | \r |
aa60d156 | 448 | memcpy(blockdata, datain, 16);\r |
7cc204bf | 449 | \r |
787b5bd8 | 450 | uint8_t uid[10] = {0x00};\r |
7cc204bf | 451 | \r |
aa60d156 | 452 | LED_A_ON(); LED_B_OFF(); LED_C_OFF();\r |
453 | \r | |
454 | clear_trace();\r | |
455 | iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r | |
7cc204bf | 456 | \r |
aa60d156 | 457 | if(!iso14443a_select_card(uid, NULL, NULL)) {\r |
458 | if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");\r | |
459 | OnError(0);\r | |
460 | return;\r | |
461 | };\r | |
462 | \r | |
463 | if(mifare_ultra_writeblock(blockNo, blockdata)) {\r | |
464 | if (MF_DBGLEVEL >= 1) Dbprintf("Write block error");\r | |
465 | OnError(0);\r | |
466 | return; };\r | |
467 | \r | |
468 | if(mifare_ultra_halt()) {\r | |
469 | if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");\r | |
470 | OnError(0);\r | |
471 | return;\r | |
472 | };\r | |
473 | \r | |
474 | if (MF_DBGLEVEL >= 2) DbpString("WRITE BLOCK FINISHED");\r | |
7cc204bf | 475 | \r |
aa60d156 | 476 | cmd_send(CMD_ACK,1,0,0,0,0);\r |
477 | FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r | |
478 | LEDsoff();\r | |
7cc204bf | 479 | }\r |
480 | \r | |
481 | void MifareUWriteBlock_Special(uint8_t arg0, uint8_t *datain)\r | |
482 | {\r | |
baeaf579 | 483 | uint8_t blockNo = arg0;\r |
787b5bd8 | 484 | byte_t blockdata[4] = {0x00};\r |
baeaf579 | 485 | \r |
7cc204bf | 486 | memcpy(blockdata, datain,4);\r |
487 | \r | |
787b5bd8 | 488 | uint8_t uid[10] = {0x00};\r |
aa60d156 | 489 | \r |
490 | LED_A_ON(); LED_B_OFF(); LED_C_OFF();\r | |
491 | clear_trace();\r | |
492 | iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r | |
493 | \r | |
494 | if(!iso14443a_select_card(uid, NULL, NULL)) {\r | |
495 | if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");\r | |
496 | OnError(0);\r | |
497 | return;\r | |
498 | };\r | |
7cc204bf | 499 | \r |
aa60d156 | 500 | if(mifare_ultra_special_writeblock(blockNo, blockdata)) {\r |
501 | if (MF_DBGLEVEL >= 1) Dbprintf("Write block error");\r | |
502 | OnError(0);\r | |
503 | return;\r | |
504 | };\r | |
505 | \r | |
506 | if(mifare_ultra_halt()) {\r | |
507 | if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");\r | |
508 | OnError(0);\r | |
509 | return;\r | |
510 | };\r | |
511 | \r | |
512 | if (MF_DBGLEVEL >= 2) DbpString("WRITE BLOCK FINISHED");\r | |
513 | \r | |
514 | cmd_send(CMD_ACK,1,0,0,0,0);\r | |
515 | FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r | |
516 | LEDsoff();\r | |
517 | }\r | |
518 | \r | |
519 | void MifareUSetPwd(uint8_t arg0, uint8_t *datain){\r | |
520 | \r | |
521 | uint8_t pwd[16] = {0x00};\r | |
522 | byte_t blockdata[4] = {0x00};\r | |
523 | \r | |
524 | memcpy(pwd, datain, 16);\r | |
525 | \r | |
526 | LED_A_ON(); LED_B_OFF(); LED_C_OFF();\r | |
3000dc4e | 527 | clear_trace();\r |
baeaf579 | 528 | iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r |
7cc204bf | 529 | \r |
aa60d156 | 530 | if(!iso14443a_select_card(NULL, NULL, NULL)) {\r |
531 | if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");\r | |
532 | OnError(0);\r | |
533 | return;\r | |
534 | };\r | |
7cc204bf | 535 | \r |
aa60d156 | 536 | blockdata[0] = pwd[7];\r |
537 | blockdata[1] = pwd[6];\r | |
538 | blockdata[2] = pwd[5];\r | |
539 | blockdata[3] = pwd[4];\r | |
540 | if(mifare_ultra_special_writeblock( 44, blockdata)) {\r | |
541 | if (MF_DBGLEVEL >= 1) Dbprintf("Write block error");\r | |
542 | OnError(44);\r | |
543 | return;\r | |
544 | };\r | |
7cc204bf | 545 | \r |
aa60d156 | 546 | blockdata[0] = pwd[3];\r |
547 | blockdata[1] = pwd[2];\r | |
548 | blockdata[2] = pwd[1];\r | |
549 | blockdata[3] = pwd[0];\r | |
550 | if(mifare_ultra_special_writeblock( 45, blockdata)) {\r | |
551 | if (MF_DBGLEVEL >= 1) Dbprintf("Write block error");\r | |
552 | OnError(45);\r | |
553 | return;\r | |
554 | };\r | |
7cc204bf | 555 | \r |
aa60d156 | 556 | blockdata[0] = pwd[15];\r |
557 | blockdata[1] = pwd[14];\r | |
558 | blockdata[2] = pwd[13];\r | |
559 | blockdata[3] = pwd[12];\r | |
560 | if(mifare_ultra_special_writeblock( 46, blockdata)) {\r | |
561 | if (MF_DBGLEVEL >= 1) Dbprintf("Write block error");\r | |
562 | OnError(46);\r | |
563 | return;\r | |
564 | };\r | |
7cc204bf | 565 | \r |
aa60d156 | 566 | blockdata[0] = pwd[11];\r |
567 | blockdata[1] = pwd[10];\r | |
568 | blockdata[2] = pwd[9];\r | |
569 | blockdata[3] = pwd[8];\r | |
570 | if(mifare_ultra_special_writeblock( 47, blockdata)) {\r | |
571 | if (MF_DBGLEVEL >= 1) Dbprintf("Write block error");\r | |
572 | OnError(47);\r | |
573 | return;\r | |
574 | }; \r | |
7cc204bf | 575 | \r |
aa60d156 | 576 | if(mifare_ultra_halt()) {\r |
577 | if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");\r | |
578 | OnError(0);\r | |
579 | return;\r | |
580 | };\r | |
7cc204bf | 581 | \r |
aa60d156 | 582 | cmd_send(CMD_ACK,1,0,0,0,0);\r |
baeaf579 | 583 | FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r |
584 | LEDsoff();\r | |
7cc204bf | 585 | }\r |
586 | \r | |
587 | // Return 1 if the nonce is invalid else return 0\r | |
6a1f2d82 | 588 | int valid_nonce(uint32_t Nt, uint32_t NtEnc, uint32_t Ks1, uint8_t *parity) {\r |
7cc204bf | 589 | return ((oddparity((Nt >> 24) & 0xFF) == ((parity[0]) ^ oddparity((NtEnc >> 24) & 0xFF) ^ BIT(Ks1,16))) & \\r |
8556b852 M |
590 | (oddparity((Nt >> 16) & 0xFF) == ((parity[1]) ^ oddparity((NtEnc >> 16) & 0xFF) ^ BIT(Ks1,8))) & \\r |
591 | (oddparity((Nt >> 8) & 0xFF) == ((parity[2]) ^ oddparity((NtEnc >> 8) & 0xFF) ^ BIT(Ks1,0)))) ? 1 : 0;\r | |
592 | }\r | |
593 | \r | |
9492e0b0 | 594 | \r |
8556b852 M |
595 | //-----------------------------------------------------------------------------\r |
596 | // MIFARE nested authentication. \r | |
597 | // \r | |
598 | //-----------------------------------------------------------------------------\r | |
9492e0b0 | 599 | void MifareNested(uint32_t arg0, uint32_t arg1, uint32_t calibrate, uint8_t *datain)\r |
8556b852 M |
600 | {\r |
601 | // params\r | |
9492e0b0 | 602 | uint8_t blockNo = arg0 & 0xff;\r |
603 | uint8_t keyType = (arg0 >> 8) & 0xff;\r | |
604 | uint8_t targetBlockNo = arg1 & 0xff;\r | |
605 | uint8_t targetKeyType = (arg1 >> 8) & 0xff;\r | |
8556b852 M |
606 | uint64_t ui64Key = 0;\r |
607 | \r | |
608 | ui64Key = bytes_to_num(datain, 6);\r | |
609 | \r | |
610 | // variables\r | |
9492e0b0 | 611 | uint16_t rtr, i, j, len;\r |
612 | uint16_t davg;\r | |
613 | static uint16_t dmin, dmax;\r | |
1c611bbd | 614 | uint8_t uid[10];\r |
6a1f2d82 | 615 | uint32_t cuid, nt1, nt2, nttmp, nttest, ks1;\r |
616 | uint8_t par[1];\r | |
9492e0b0 | 617 | uint32_t target_nt[2], target_ks[2];\r |
618 | \r | |
8556b852 | 619 | uint8_t par_array[4];\r |
9492e0b0 | 620 | uint16_t ncount = 0;\r |
8556b852 M |
621 | struct Crypto1State mpcs = {0, 0};\r |
622 | struct Crypto1State *pcs;\r | |
623 | pcs = &mpcs;\r | |
f71f4deb | 624 | uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];\r |
8556b852 | 625 | \r |
9492e0b0 | 626 | uint32_t auth1_time, auth2_time;\r |
627 | static uint16_t delta_time;\r | |
628 | \r | |
f71f4deb | 629 | // free eventually allocated BigBuf memory\r |
630 | BigBuf_free();\r | |
8556b852 | 631 | // clear trace\r |
3000dc4e MHS |
632 | clear_trace();\r |
633 | set_tracing(false);\r | |
8556b852 | 634 | \r |
7bc95e2e | 635 | iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r |
8556b852 M |
636 | \r |
637 | LED_A_ON();\r | |
8556b852 M |
638 | LED_C_OFF();\r |
639 | \r | |
8556b852 | 640 | \r |
9492e0b0 | 641 | // statistics on nonce distance\r |
642 | if (calibrate) { // for first call only. Otherwise reuse previous calibration\r | |
643 | LED_B_ON();\r | |
3fe4ff4f | 644 | WDT_HIT();\r |
8556b852 | 645 | \r |
9492e0b0 | 646 | davg = dmax = 0;\r |
647 | dmin = 2000;\r | |
648 | delta_time = 0;\r | |
8556b852 | 649 | \r |
9492e0b0 | 650 | for (rtr = 0; rtr < 17; rtr++) {\r |
8556b852 | 651 | \r |
9492e0b0 | 652 | // prepare next select. No need to power down the card.\r |
653 | if(mifare_classic_halt(pcs, cuid)) {\r | |
654 | if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Halt error");\r | |
655 | rtr--;\r | |
656 | continue;\r | |
657 | }\r | |
658 | \r | |
659 | if(!iso14443a_select_card(uid, NULL, &cuid)) {\r | |
660 | if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Can't select card");\r | |
661 | rtr--;\r | |
662 | continue;\r | |
663 | };\r | |
664 | \r | |
665 | auth1_time = 0;\r | |
666 | if(mifare_classic_authex(pcs, cuid, blockNo, keyType, ui64Key, AUTH_FIRST, &nt1, &auth1_time)) {\r | |
667 | if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Auth1 error");\r | |
668 | rtr--;\r | |
669 | continue;\r | |
670 | };\r | |
671 | \r | |
672 | if (delta_time) {\r | |
673 | auth2_time = auth1_time + delta_time;\r | |
674 | } else {\r | |
675 | auth2_time = 0;\r | |
676 | }\r | |
677 | if(mifare_classic_authex(pcs, cuid, blockNo, keyType, ui64Key, AUTH_NESTED, &nt2, &auth2_time)) {\r | |
678 | if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Auth2 error");\r | |
679 | rtr--;\r | |
680 | continue;\r | |
681 | };\r | |
682 | \r | |
b19bd5d6 | 683 | nttmp = prng_successor(nt1, 100); //NXP Mifare is typical around 840,but for some unlicensed/compatible mifare card this can be 160\r |
684 | for (i = 101; i < 1200; i++) {\r | |
9492e0b0 | 685 | nttmp = prng_successor(nttmp, 1);\r |
686 | if (nttmp == nt2) break;\r | |
687 | }\r | |
688 | \r | |
689 | if (i != 1200) {\r | |
690 | if (rtr != 0) {\r | |
691 | davg += i;\r | |
692 | dmin = MIN(dmin, i);\r | |
693 | dmax = MAX(dmax, i);\r | |
694 | }\r | |
695 | else {\r | |
696 | delta_time = auth2_time - auth1_time + 32; // allow some slack for proper timing\r | |
697 | }\r | |
698 | if (MF_DBGLEVEL >= 3) Dbprintf("Nested: calibrating... ntdist=%d", i);\r | |
699 | }\r | |
8556b852 M |
700 | }\r |
701 | \r | |
9492e0b0 | 702 | if (rtr <= 1) return;\r |
8556b852 | 703 | \r |
9492e0b0 | 704 | davg = (davg + (rtr - 1)/2) / (rtr - 1);\r |
705 | \r | |
706 | if (MF_DBGLEVEL >= 3) Dbprintf("min=%d max=%d avg=%d, delta_time=%d", dmin, dmax, davg, delta_time);\r | |
8556b852 | 707 | \r |
9492e0b0 | 708 | dmin = davg - 2;\r |
709 | dmax = davg + 2;\r | |
710 | \r | |
711 | LED_B_OFF();\r | |
712 | \r | |
713 | }\r | |
8556b852 M |
714 | // ------------------------------------------------------------------------------------------------- \r |
715 | \r | |
716 | LED_C_ON();\r | |
717 | \r | |
718 | // get crypted nonces for target sector\r | |
9492e0b0 | 719 | for(i=0; i < 2; i++) { // look for exactly two different nonces\r |
8556b852 | 720 | \r |
9492e0b0 | 721 | target_nt[i] = 0;\r |
722 | while(target_nt[i] == 0) { // continue until we have an unambiguous nonce\r | |
8556b852 | 723 | \r |
9492e0b0 | 724 | // prepare next select. No need to power down the card.\r |
725 | if(mifare_classic_halt(pcs, cuid)) {\r | |
726 | if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Halt error");\r | |
727 | continue;\r | |
728 | }\r | |
8556b852 | 729 | \r |
9492e0b0 | 730 | if(!iso14443a_select_card(uid, NULL, &cuid)) {\r |
731 | if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Can't select card");\r | |
732 | continue;\r | |
733 | };\r | |
8556b852 | 734 | \r |
9492e0b0 | 735 | auth1_time = 0;\r |
736 | if(mifare_classic_authex(pcs, cuid, blockNo, keyType, ui64Key, AUTH_FIRST, &nt1, &auth1_time)) {\r | |
737 | if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Auth1 error");\r | |
738 | continue;\r | |
739 | };\r | |
8556b852 | 740 | \r |
9492e0b0 | 741 | // nested authentication\r |
742 | auth2_time = auth1_time + delta_time;\r | |
6a1f2d82 | 743 | len = mifare_sendcmd_shortex(pcs, AUTH_NESTED, 0x60 + (targetKeyType & 0x01), targetBlockNo, receivedAnswer, par, &auth2_time);\r |
9492e0b0 | 744 | if (len != 4) {\r |
745 | if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Auth2 error len=%d", len);\r | |
746 | continue;\r | |
747 | };\r | |
8556b852 | 748 | \r |
9492e0b0 | 749 | nt2 = bytes_to_num(receivedAnswer, 4); \r |
6a1f2d82 | 750 | if (MF_DBGLEVEL >= 3) Dbprintf("Nonce#%d: Testing nt1=%08x nt2enc=%08x nt2par=%02x", i+1, nt1, nt2, par[0]);\r |
8556b852 | 751 | \r |
9492e0b0 | 752 | // Parity validity check\r |
753 | for (j = 0; j < 4; j++) {\r | |
6a1f2d82 | 754 | par_array[j] = (oddparity(receivedAnswer[j]) != ((par[0] >> (7-j)) & 0x01));\r |
9492e0b0 | 755 | }\r |
756 | \r | |
757 | ncount = 0;\r | |
758 | nttest = prng_successor(nt1, dmin - 1);\r | |
759 | for (j = dmin; j < dmax + 1; j++) {\r | |
760 | nttest = prng_successor(nttest, 1);\r | |
761 | ks1 = nt2 ^ nttest;\r | |
762 | \r | |
763 | if (valid_nonce(nttest, nt2, ks1, par_array)){\r | |
764 | if (ncount > 0) { // we are only interested in disambiguous nonces, try again\r | |
765 | if (MF_DBGLEVEL >= 3) Dbprintf("Nonce#%d: dismissed (ambigous), ntdist=%d", i+1, j);\r | |
766 | target_nt[i] = 0;\r | |
767 | break;\r | |
768 | }\r | |
769 | target_nt[i] = nttest;\r | |
770 | target_ks[i] = ks1;\r | |
771 | ncount++;\r | |
772 | if (i == 1 && target_nt[1] == target_nt[0]) { // we need two different nonces\r | |
773 | target_nt[i] = 0;\r | |
774 | if (MF_DBGLEVEL >= 3) Dbprintf("Nonce#2: dismissed (= nonce#1), ntdist=%d", j);\r | |
8556b852 M |
775 | break;\r |
776 | }\r | |
9492e0b0 | 777 | if (MF_DBGLEVEL >= 3) Dbprintf("Nonce#%d: valid, ntdist=%d", i+1, j);\r |
8556b852 | 778 | }\r |
8556b852 | 779 | }\r |
9492e0b0 | 780 | if (target_nt[i] == 0 && j == dmax+1 && MF_DBGLEVEL >= 3) Dbprintf("Nonce#%d: dismissed (all invalid)", i+1);\r |
8556b852 M |
781 | }\r |
782 | }\r | |
783 | \r | |
784 | LED_C_OFF();\r | |
785 | \r | |
786 | // ----------------------------- crypto1 destroy\r | |
787 | crypto1_destroy(pcs);\r | |
788 | \r | |
9492e0b0 | 789 | byte_t buf[4 + 4 * 4];\r |
790 | memcpy(buf, &cuid, 4);\r | |
791 | memcpy(buf+4, &target_nt[0], 4);\r | |
792 | memcpy(buf+8, &target_ks[0], 4);\r | |
793 | memcpy(buf+12, &target_nt[1], 4);\r | |
794 | memcpy(buf+16, &target_ks[1], 4);\r | |
8556b852 M |
795 | \r |
796 | LED_B_ON();\r | |
9492e0b0 | 797 | cmd_send(CMD_ACK, 0, 2, targetBlockNo + (targetKeyType * 0x100), buf, sizeof(buf));\r |
6e82300d | 798 | LED_B_OFF();\r |
8556b852 | 799 | \r |
9492e0b0 | 800 | if (MF_DBGLEVEL >= 3) DbpString("NESTED FINISHED");\r |
8556b852 | 801 | \r |
8556b852 M |
802 | FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r |
803 | LEDsoff();\r | |
3000dc4e | 804 | set_tracing(TRUE);\r |
8556b852 M |
805 | }\r |
806 | \r | |
807 | //-----------------------------------------------------------------------------\r | |
9492e0b0 | 808 | // MIFARE check keys. key count up to 85. \r |
8556b852 M |
809 | // \r |
810 | //-----------------------------------------------------------------------------\r | |
811 | void MifareChkKeys(uint8_t arg0, uint8_t arg1, uint8_t arg2, uint8_t *datain)\r | |
812 | {\r | |
813 | // params\r | |
814 | uint8_t blockNo = arg0;\r | |
815 | uint8_t keyType = arg1;\r | |
816 | uint8_t keyCount = arg2;\r | |
817 | uint64_t ui64Key = 0;\r | |
818 | \r | |
819 | // variables\r | |
820 | int i;\r | |
821 | byte_t isOK = 0;\r | |
1c611bbd | 822 | uint8_t uid[10];\r |
8556b852 M |
823 | uint32_t cuid;\r |
824 | struct Crypto1State mpcs = {0, 0};\r | |
825 | struct Crypto1State *pcs;\r | |
826 | pcs = &mpcs;\r | |
827 | \r | |
828 | // clear debug level\r | |
829 | int OLD_MF_DBGLEVEL = MF_DBGLEVEL; \r | |
830 | MF_DBGLEVEL = MF_DBG_NONE;\r | |
831 | \r | |
832 | // clear trace\r | |
3000dc4e MHS |
833 | clear_trace();\r |
834 | set_tracing(TRUE);\r | |
8556b852 | 835 | \r |
7bc95e2e | 836 | iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r |
8556b852 M |
837 | \r |
838 | LED_A_ON();\r | |
839 | LED_B_OFF();\r | |
840 | LED_C_OFF();\r | |
841 | \r | |
8556b852 | 842 | for (i = 0; i < keyCount; i++) {\r |
9492e0b0 | 843 | if(mifare_classic_halt(pcs, cuid)) {\r |
844 | if (MF_DBGLEVEL >= 1) Dbprintf("ChkKeys: Halt error");\r | |
845 | }\r | |
8556b852 M |
846 | \r |
847 | if(!iso14443a_select_card(uid, NULL, &cuid)) {\r | |
9492e0b0 | 848 | if (OLD_MF_DBGLEVEL >= 1) Dbprintf("ChkKeys: Can't select card");\r |
8556b852 M |
849 | break;\r |
850 | };\r | |
851 | \r | |
852 | ui64Key = bytes_to_num(datain + i * 6, 6);\r | |
853 | if(mifare_classic_auth(pcs, cuid, blockNo, keyType, ui64Key, AUTH_FIRST)) {\r | |
854 | continue;\r | |
855 | };\r | |
856 | \r | |
857 | isOK = 1;\r | |
858 | break;\r | |
859 | }\r | |
860 | \r | |
861 | // ----------------------------- crypto1 destroy\r | |
862 | crypto1_destroy(pcs);\r | |
863 | \r | |
8556b852 | 864 | LED_B_ON();\r |
6e82300d | 865 | cmd_send(CMD_ACK,isOK,0,0,datain + i * 6,6);\r |
8556b852 M |
866 | LED_B_OFF();\r |
867 | \r | |
8556b852 M |
868 | FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r |
869 | LEDsoff();\r | |
870 | \r | |
871 | // restore debug level\r | |
872 | MF_DBGLEVEL = OLD_MF_DBGLEVEL; \r | |
873 | }\r | |
874 | \r | |
875 | //-----------------------------------------------------------------------------\r | |
876 | // MIFARE commands set debug level\r | |
877 | // \r | |
878 | //-----------------------------------------------------------------------------\r | |
879 | void MifareSetDbgLvl(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain){\r | |
880 | MF_DBGLEVEL = arg0;\r | |
881 | Dbprintf("Debug level: %d", MF_DBGLEVEL);\r | |
882 | }\r | |
883 | \r | |
884 | //-----------------------------------------------------------------------------\r | |
885 | // Work with emulator memory\r | |
886 | // \r | |
887 | //-----------------------------------------------------------------------------\r | |
888 | void MifareEMemClr(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain){\r | |
889 | emlClearMem();\r | |
890 | }\r | |
891 | \r | |
892 | void MifareEMemSet(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain){\r | |
893 | emlSetMem(datain, arg0, arg1); // data, block num, blocks count\r | |
894 | }\r | |
895 | \r | |
896 | void MifareEMemGet(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain){\r | |
3fe4ff4f | 897 | byte_t buf[USB_CMD_DATA_SIZE];\r |
baeaf579 | 898 | emlGetMem(buf, arg0, arg1); // data, block num, blocks count (max 4)\r |
8556b852 M |
899 | \r |
900 | LED_B_ON();\r | |
3fe4ff4f | 901 | cmd_send(CMD_ACK,arg0,arg1,0,buf,USB_CMD_DATA_SIZE);\r |
8556b852 M |
902 | LED_B_OFF();\r |
903 | }\r | |
904 | \r | |
905 | //-----------------------------------------------------------------------------\r | |
906 | // Load a card into the emulator memory\r | |
907 | // \r | |
908 | //-----------------------------------------------------------------------------\r | |
909 | void MifareECardLoad(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain){\r | |
baeaf579 | 910 | uint8_t numSectors = arg0;\r |
8556b852 M |
911 | uint8_t keyType = arg1;\r |
912 | uint64_t ui64Key = 0;\r | |
913 | uint32_t cuid;\r | |
914 | struct Crypto1State mpcs = {0, 0};\r | |
915 | struct Crypto1State *pcs;\r | |
916 | pcs = &mpcs;\r | |
917 | \r | |
918 | // variables\r | |
919 | byte_t dataoutbuf[16];\r | |
ab8b654e | 920 | byte_t dataoutbuf2[16];\r |
1c611bbd | 921 | uint8_t uid[10];\r |
8556b852 M |
922 | \r |
923 | // clear trace\r | |
3000dc4e MHS |
924 | clear_trace();\r |
925 | set_tracing(false);\r | |
8556b852 | 926 | \r |
7bc95e2e | 927 | iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r |
8556b852 M |
928 | \r |
929 | LED_A_ON();\r | |
930 | LED_B_OFF();\r | |
931 | LED_C_OFF();\r | |
932 | \r | |
baeaf579 | 933 | bool isOK = true;\r |
934 | \r | |
935 | if(!iso14443a_select_card(uid, NULL, &cuid)) {\r | |
936 | isOK = false;\r | |
937 | if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");\r | |
938 | }\r | |
8556b852 | 939 | \r |
baeaf579 | 940 | for (uint8_t sectorNo = 0; isOK && sectorNo < numSectors; sectorNo++) {\r |
941 | ui64Key = emlGetKey(sectorNo, keyType);\r | |
942 | if (sectorNo == 0){\r | |
943 | if(isOK && mifare_classic_auth(pcs, cuid, FirstBlockOfSector(sectorNo), keyType, ui64Key, AUTH_FIRST)) {\r | |
944 | isOK = false;\r | |
945 | if (MF_DBGLEVEL >= 1) Dbprintf("Sector[%2d]. Auth error", sectorNo);\r | |
8556b852 | 946 | break;\r |
baeaf579 | 947 | }\r |
948 | } else {\r | |
949 | if(isOK && mifare_classic_auth(pcs, cuid, FirstBlockOfSector(sectorNo), keyType, ui64Key, AUTH_NESTED)) {\r | |
950 | isOK = false;\r | |
951 | if (MF_DBGLEVEL >= 1) Dbprintf("Sector[%2d]. Auth nested error", sectorNo);\r | |
8556b852 | 952 | break;\r |
baeaf579 | 953 | }\r |
954 | }\r | |
955 | \r | |
956 | for (uint8_t blockNo = 0; isOK && blockNo < NumBlocksPerSector(sectorNo); blockNo++) {\r | |
957 | if(isOK && mifare_classic_readblock(pcs, cuid, FirstBlockOfSector(sectorNo) + blockNo, dataoutbuf)) {\r | |
958 | isOK = false;\r | |
959 | if (MF_DBGLEVEL >= 1) Dbprintf("Error reading sector %2d block %2d", sectorNo, blockNo);\r | |
ab8b654e M |
960 | break;\r |
961 | };\r | |
baeaf579 | 962 | if (isOK) {\r |
963 | if (blockNo < NumBlocksPerSector(sectorNo) - 1) {\r | |
964 | emlSetMem(dataoutbuf, FirstBlockOfSector(sectorNo) + blockNo, 1);\r | |
965 | } else { // sector trailer, keep the keys, set only the AC\r | |
966 | emlGetMem(dataoutbuf2, FirstBlockOfSector(sectorNo) + blockNo, 1);\r | |
967 | memcpy(&dataoutbuf2[6], &dataoutbuf[6], 4);\r | |
968 | emlSetMem(dataoutbuf2, FirstBlockOfSector(sectorNo) + blockNo, 1);\r | |
969 | }\r | |
970 | }\r | |
8556b852 M |
971 | }\r |
972 | \r | |
baeaf579 | 973 | }\r |
974 | \r | |
975 | if(mifare_classic_halt(pcs, cuid)) {\r | |
976 | if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");\r | |
977 | };\r | |
8556b852 M |
978 | \r |
979 | // ----------------------------- crypto1 destroy\r | |
980 | crypto1_destroy(pcs);\r | |
ab8b654e M |
981 | \r |
982 | FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r | |
983 | LEDsoff();\r | |
8556b852 M |
984 | \r |
985 | if (MF_DBGLEVEL >= 2) DbpString("EMUL FILL SECTORS FINISHED");\r | |
986 | \r | |
8556b852 M |
987 | }\r |
988 | \r | |
0675f200 M |
989 | \r |
990 | //-----------------------------------------------------------------------------\r | |
991 | // Work with "magic Chinese" card (email him: ouyangweidaxian@live.cn)\r | |
992 | // \r | |
993 | //-----------------------------------------------------------------------------\r | |
994 | void MifareCSetBlock(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain){\r | |
995 | \r | |
996 | // params\r | |
997 | uint8_t needWipe = arg0;\r | |
208a0166 M |
998 | // bit 0 - need get UID\r |
999 | // bit 1 - need wupC\r | |
1000 | // bit 2 - need HALT after sequence\r | |
1001 | // bit 3 - need init FPGA and field before sequence\r | |
1002 | // bit 4 - need reset FPGA and LED\r | |
1003 | uint8_t workFlags = arg1;\r | |
0675f200 M |
1004 | uint8_t blockNo = arg2;\r |
1005 | \r | |
1006 | // card commands\r | |
1007 | uint8_t wupC1[] = { 0x40 }; \r | |
1008 | uint8_t wupC2[] = { 0x43 }; \r | |
1009 | uint8_t wipeC[] = { 0x41 }; \r | |
1010 | \r | |
1011 | // variables\r | |
1012 | byte_t isOK = 0;\r | |
3fe4ff4f | 1013 | uint8_t uid[10] = {0x00};\r |
1014 | uint8_t d_block[18] = {0x00};\r | |
0675f200 M |
1015 | uint32_t cuid;\r |
1016 | \r | |
f71f4deb | 1017 | uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];\r |
1018 | uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE];\r | |
6a1f2d82 | 1019 | \r |
3fe4ff4f | 1020 | // reset FPGA and LED\r |
208a0166 | 1021 | if (workFlags & 0x08) {\r |
208a0166 M |
1022 | LED_A_ON();\r |
1023 | LED_B_OFF();\r | |
1024 | LED_C_OFF();\r | |
0675f200 | 1025 | \r |
3000dc4e MHS |
1026 | clear_trace();\r |
1027 | set_tracing(TRUE);\r | |
3fe4ff4f | 1028 | iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r |
208a0166 | 1029 | }\r |
0675f200 M |
1030 | \r |
1031 | while (true) {\r | |
3fe4ff4f | 1032 | \r |
0675f200 | 1033 | // get UID from chip\r |
208a0166 | 1034 | if (workFlags & 0x01) {\r |
0675f200 M |
1035 | if(!iso14443a_select_card(uid, NULL, &cuid)) {\r |
1036 | if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");\r | |
3ac59c7f | 1037 | //break;\r |
0675f200 M |
1038 | };\r |
1039 | \r | |
1040 | if(mifare_classic_halt(NULL, cuid)) {\r | |
1041 | if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");\r | |
3ac59c7f | 1042 | //break;\r |
0675f200 M |
1043 | };\r |
1044 | };\r | |
1045 | \r | |
1046 | // reset chip\r | |
1047 | if (needWipe){\r | |
6a1f2d82 | 1048 | ReaderTransmitBitsPar(wupC1,7,0, NULL);\r |
1049 | if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != 0x0a)) {\r | |
0675f200 M |
1050 | if (MF_DBGLEVEL >= 1) Dbprintf("wupC1 error");\r |
1051 | break;\r | |
1052 | };\r | |
1053 | \r | |
9492e0b0 | 1054 | ReaderTransmit(wipeC, sizeof(wipeC), NULL);\r |
6a1f2d82 | 1055 | if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != 0x0a)) {\r |
0675f200 M |
1056 | if (MF_DBGLEVEL >= 1) Dbprintf("wipeC error");\r |
1057 | break;\r | |
1058 | };\r | |
1059 | \r | |
1060 | if(mifare_classic_halt(NULL, cuid)) {\r | |
1061 | if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");\r | |
1062 | break;\r | |
1063 | };\r | |
1064 | }; \r | |
1065 | \r | |
545a1f38 | 1066 | // write block\r |
208a0166 | 1067 | if (workFlags & 0x02) {\r |
6a1f2d82 | 1068 | ReaderTransmitBitsPar(wupC1,7,0, NULL);\r |
1069 | if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != 0x0a)) {\r | |
208a0166 M |
1070 | if (MF_DBGLEVEL >= 1) Dbprintf("wupC1 error");\r |
1071 | break;\r | |
1072 | };\r | |
0675f200 | 1073 | \r |
9492e0b0 | 1074 | ReaderTransmit(wupC2, sizeof(wupC2), NULL);\r |
6a1f2d82 | 1075 | if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != 0x0a)) {\r |
208a0166 M |
1076 | if (MF_DBGLEVEL >= 1) Dbprintf("wupC2 error");\r |
1077 | break;\r | |
1078 | };\r | |
1079 | }\r | |
0675f200 | 1080 | \r |
6a1f2d82 | 1081 | if ((mifare_sendcmd_short(NULL, 0, 0xA0, blockNo, receivedAnswer, receivedAnswerPar, NULL) != 1) || (receivedAnswer[0] != 0x0a)) {\r |
0675f200 M |
1082 | if (MF_DBGLEVEL >= 1) Dbprintf("write block send command error");\r |
1083 | break;\r | |
1084 | };\r | |
1085 | \r | |
1086 | memcpy(d_block, datain, 16);\r | |
1087 | AppendCrc14443a(d_block, 16);\r | |
1088 | \r | |
9492e0b0 | 1089 | ReaderTransmit(d_block, sizeof(d_block), NULL);\r |
6a1f2d82 | 1090 | if ((ReaderReceive(receivedAnswer, receivedAnswerPar) != 1) || (receivedAnswer[0] != 0x0a)) {\r |
0675f200 M |
1091 | if (MF_DBGLEVEL >= 1) Dbprintf("write block send data error");\r |
1092 | break;\r | |
1093 | }; \r | |
1094 | \r | |
208a0166 M |
1095 | if (workFlags & 0x04) {\r |
1096 | if (mifare_classic_halt(NULL, cuid)) {\r | |
1097 | if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");\r | |
1098 | break;\r | |
1099 | };\r | |
1100 | }\r | |
0675f200 M |
1101 | \r |
1102 | isOK = 1;\r | |
1103 | break;\r | |
1104 | }\r | |
1105 | \r | |
0675f200 | 1106 | LED_B_ON();\r |
baeaf579 | 1107 | cmd_send(CMD_ACK,isOK,0,0,uid,4);\r |
0675f200 M |
1108 | LED_B_OFF();\r |
1109 | \r | |
545a1f38 | 1110 | if ((workFlags & 0x10) || (!isOK)) {\r |
208a0166 M |
1111 | FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r |
1112 | LEDsoff();\r | |
1113 | }\r | |
0675f200 | 1114 | }\r |
545a1f38 | 1115 | \r |
baeaf579 | 1116 | \r |
545a1f38 M |
1117 | void MifareCGetBlock(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain){\r |
1118 | \r | |
1119 | // params\r | |
1120 | // bit 1 - need wupC\r | |
1121 | // bit 2 - need HALT after sequence\r | |
1122 | // bit 3 - need init FPGA and field before sequence\r | |
1123 | // bit 4 - need reset FPGA and LED\r | |
1124 | uint8_t workFlags = arg0;\r | |
1125 | uint8_t blockNo = arg2;\r | |
1126 | \r | |
1127 | // card commands\r | |
1128 | uint8_t wupC1[] = { 0x40 }; \r | |
1129 | uint8_t wupC2[] = { 0x43 }; \r | |
1130 | \r | |
1131 | // variables\r | |
1132 | byte_t isOK = 0;\r | |
3fe4ff4f | 1133 | uint8_t data[18] = {0x00};\r |
545a1f38 M |
1134 | uint32_t cuid = 0;\r |
1135 | \r | |
f71f4deb | 1136 | uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];\r |
1137 | uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE];\r | |
545a1f38 M |
1138 | \r |
1139 | if (workFlags & 0x08) {\r | |
545a1f38 M |
1140 | LED_A_ON();\r |
1141 | LED_B_OFF();\r | |
1142 | LED_C_OFF();\r | |
1143 | \r | |
3000dc4e MHS |
1144 | clear_trace();\r |
1145 | set_tracing(TRUE);\r | |
3fe4ff4f | 1146 | iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r |
545a1f38 M |
1147 | }\r |
1148 | \r | |
1149 | while (true) {\r | |
1150 | if (workFlags & 0x02) {\r | |
7bc95e2e | 1151 | ReaderTransmitBitsPar(wupC1,7,0, NULL);\r |
6a1f2d82 | 1152 | if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != 0x0a)) {\r |
545a1f38 M |
1153 | if (MF_DBGLEVEL >= 1) Dbprintf("wupC1 error");\r |
1154 | break;\r | |
1155 | };\r | |
1156 | \r | |
9492e0b0 | 1157 | ReaderTransmit(wupC2, sizeof(wupC2), NULL);\r |
6a1f2d82 | 1158 | if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != 0x0a)) {\r |
545a1f38 M |
1159 | if (MF_DBGLEVEL >= 1) Dbprintf("wupC2 error");\r |
1160 | break;\r | |
1161 | };\r | |
1162 | }\r | |
1163 | \r | |
1164 | // read block\r | |
6a1f2d82 | 1165 | if ((mifare_sendcmd_short(NULL, 0, 0x30, blockNo, receivedAnswer, receivedAnswerPar, NULL) != 18)) {\r |
545a1f38 M |
1166 | if (MF_DBGLEVEL >= 1) Dbprintf("read block send command error");\r |
1167 | break;\r | |
1168 | };\r | |
1169 | memcpy(data, receivedAnswer, 18);\r | |
1170 | \r | |
1171 | if (workFlags & 0x04) {\r | |
1172 | if (mifare_classic_halt(NULL, cuid)) {\r | |
1173 | if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");\r | |
1174 | break;\r | |
1175 | };\r | |
1176 | }\r | |
1177 | \r | |
1178 | isOK = 1;\r | |
1179 | break;\r | |
1180 | }\r | |
1181 | \r | |
545a1f38 | 1182 | LED_B_ON();\r |
baeaf579 | 1183 | cmd_send(CMD_ACK,isOK,0,0,data,18);\r |
545a1f38 M |
1184 | LED_B_OFF();\r |
1185 | \r | |
1186 | if ((workFlags & 0x10) || (!isOK)) {\r | |
545a1f38 M |
1187 | FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r |
1188 | LEDsoff();\r | |
1189 | }\r | |
1190 | }\r | |
1191 | \r | |
3fe4ff4f | 1192 | void MifareCIdent(){\r |
1193 | \r | |
1194 | // card commands\r | |
1195 | uint8_t wupC1[] = { 0x40 }; \r | |
1196 | uint8_t wupC2[] = { 0x43 }; \r | |
1197 | \r | |
1198 | // variables\r | |
1199 | byte_t isOK = 1;\r | |
1200 | \r | |
f71f4deb | 1201 | uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];\r |
1202 | uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE];\r | |
3fe4ff4f | 1203 | \r |
1204 | ReaderTransmitBitsPar(wupC1,7,0, NULL);\r | |
1205 | if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != 0x0a)) {\r | |
1206 | isOK = 0;\r | |
1207 | };\r | |
1208 | \r | |
1209 | ReaderTransmit(wupC2, sizeof(wupC2), NULL);\r | |
1210 | if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != 0x0a)) {\r | |
1211 | isOK = 0;\r | |
1212 | };\r | |
1213 | \r | |
1214 | if (mifare_classic_halt(NULL, 0)) {\r | |
1215 | isOK = 0;\r | |
1216 | };\r | |
1217 | \r | |
1218 | cmd_send(CMD_ACK,isOK,0,0,0,0);\r | |
1219 | }\r | |
1220 | \r | |
add0504d | 1221 | void MifareCollectNonces(uint32_t arg0, uint32_t arg1){\r |
1222 | \r | |
1223 | BigBuf_free();\r | |
1224 | \r | |
1225 | uint32_t iterations = arg0;\r | |
1226 | uint8_t uid[10] = {0x00};\r | |
1227 | \r | |
1228 | uint8_t *response = BigBuf_malloc(MAX_MIFARE_FRAME_SIZE);\r | |
1229 | uint8_t *responsePar = BigBuf_malloc(MAX_MIFARE_PARITY_SIZE);\r | |
1230 | \r | |
1231 | uint8_t mf_auth[] = { 0x60,0x00,0xf5,0x7b };\r | |
1232 | \r | |
1233 | // get memory from BigBuf.\r | |
1234 | uint8_t *nonces = BigBuf_malloc(iterations * 4);\r | |
1235 | \r | |
1236 | LED_A_ON();\r | |
1237 | LED_B_OFF();\r | |
1238 | LED_C_OFF();\r | |
1239 | \r | |
1240 | clear_trace();\r | |
1241 | set_tracing(TRUE);\r | |
1242 | iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r | |
1243 | \r | |
1244 | for (int i = 0; i < iterations; i++) {\r | |
1245 | \r | |
1246 | WDT_HIT();\r | |
1247 | \r | |
1248 | // Test if the action was cancelled\r | |
1249 | if(BUTTON_PRESS()) break;\r | |
1250 | \r | |
1251 | // if(mifare_classic_halt(pcs, cuid)) {\r | |
1252 | // if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");\r | |
1253 | //}\r | |
1254 | \r | |
1255 | if(!iso14443a_select_card(uid, NULL, NULL)) {\r | |
1256 | if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");\r | |
1257 | continue;\r | |
1258 | };\r | |
1259 | \r | |
1260 | // Transmit MIFARE_CLASSIC_AUTH.\r | |
1261 | ReaderTransmit(mf_auth, sizeof(mf_auth), NULL);\r | |
1262 | \r | |
1263 | // Receive the (4 Byte) "random" nonce\r | |
1264 | if (!ReaderReceive(response, responsePar)) {\r | |
1265 | if (MF_DBGLEVEL >= 1) Dbprintf("Couldn't receive tag nonce");\r | |
1266 | continue;\r | |
1267 | } \r | |
1268 | \r | |
1269 | nonces[i*4] = bytes_to_num(response, 4);\r | |
1270 | }\r | |
1271 | \r | |
1272 | int packLen = iterations * 4;\r | |
1273 | int packSize = 0;\r | |
1274 | int packNum = 0;\r | |
1275 | while (packLen > 0) {\r | |
1276 | packSize = MIN(USB_CMD_DATA_SIZE, packLen);\r | |
1277 | LED_B_ON();\r | |
1278 | cmd_send(CMD_ACK, 77, 0, packSize, nonces - packLen, packSize);\r | |
1279 | LED_B_OFF();\r | |
1280 | \r | |
1281 | packLen -= packSize;\r | |
1282 | packNum++;\r | |
1283 | }\r | |
1284 | \r | |
1285 | FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r | |
1286 | LEDsoff();\r | |
1287 | }\r | |
1288 | \r | |
1289 | //\r | |
3fe4ff4f | 1290 | // DESFIRE\r |
1291 | //\r | |
a631936e | 1292 | \r |
1293 | void Mifare_DES_Auth1(uint8_t arg0, uint8_t *datain){\r | |
1294 | \r | |
1295 | byte_t dataout[11] = {0x00};\r | |
1296 | uint8_t uid[10] = {0x00};\r | |
aa60d156 | 1297 | uint32_t cuid = 0x00;\r |
a631936e | 1298 | \r |
3000dc4e | 1299 | clear_trace();\r |
a631936e | 1300 | iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r |
1301 | \r | |
1302 | int len = iso14443a_select_card(uid, NULL, &cuid);\r | |
1303 | if(!len) {\r | |
aa60d156 | 1304 | if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Can't select card");\r |
0ec548dc | 1305 | OnError(1);\r |
a631936e | 1306 | return;\r |
1307 | };\r | |
1308 | \r | |
1309 | if(mifare_desfire_des_auth1(cuid, dataout)){\r | |
aa60d156 | 1310 | if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Authentication part1: Fail.");\r |
0ec548dc | 1311 | OnError(4);\r |
a631936e | 1312 | return;\r |
1313 | }\r | |
1314 | \r | |
1315 | if (MF_DBGLEVEL >= MF_DBG_EXTENDED) DbpString("AUTH 1 FINISHED");\r | |
a631936e | 1316 | cmd_send(CMD_ACK,1,cuid,0,dataout, sizeof(dataout));\r |
1317 | }\r | |
1318 | \r | |
1319 | void Mifare_DES_Auth2(uint32_t arg0, uint8_t *datain){\r | |
1320 | \r | |
1321 | uint32_t cuid = arg0;\r | |
1322 | uint8_t key[16] = {0x00};\r | |
a631936e | 1323 | byte_t dataout[12] = {0x00};\r |
aa60d156 | 1324 | byte_t isOK = 0;\r |
a631936e | 1325 | \r |
1326 | memcpy(key, datain, 16);\r | |
1327 | \r | |
1328 | isOK = mifare_desfire_des_auth2(cuid, key, dataout);\r | |
1329 | \r | |
1330 | if( isOK) {\r | |
aa60d156 | 1331 | if (MF_DBGLEVEL >= MF_DBG_EXTENDED) Dbprintf("Authentication part2: Failed"); \r |
0ec548dc | 1332 | OnError(4);\r |
a631936e | 1333 | return;\r |
1334 | }\r | |
1335 | \r | |
aa60d156 | 1336 | if (MF_DBGLEVEL >= MF_DBG_EXTENDED) DbpString("AUTH 2 FINISHED");\r |
a631936e | 1337 | \r |
1338 | cmd_send(CMD_ACK, isOK, 0, 0, dataout, sizeof(dataout));\r | |
1339 | FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r | |
1340 | LEDsoff();\r | |
3000dc4e | 1341 | }\r |