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