]> cvs.zerfleddert.de Git - proxmark3-svn/blame - armsrc/mifarecmd.c
FIX: since the "hf mf ecfill" command supports 0,1,2,4 in card sizes, I consequently...
[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
f38a1528 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
f38a1528 18#include "util.h"\r
19#include "desfire.h"\r
20#include "../common/crc.h"\r
8556b852
M
21\r
22//-----------------------------------------------------------------------------\r
f38a1528 23// Select, Authenticaate, Read an MIFARE tag. \r
8556b852
M
24// read block\r
25//-----------------------------------------------------------------------------\r
26void MifareReadBlock(uint8_t arg0, uint8_t arg1, uint8_t arg2, uint8_t *datain)\r
27{\r
28 // params\r
29 uint8_t blockNo = arg0;\r
30 uint8_t keyType = arg1;\r
31 uint64_t ui64Key = 0;\r
32 ui64Key = bytes_to_num(datain, 6);\r
33 \r
34 // variables\r
35 byte_t isOK = 0;\r
36 byte_t dataoutbuf[16];\r
1c611bbd 37 uint8_t uid[10];\r
8556b852
M
38 uint32_t cuid;\r
39 struct Crypto1State mpcs = {0, 0};\r
40 struct Crypto1State *pcs;\r
41 pcs = &mpcs;\r
42\r
43 // clear trace\r
baeaf579 44 iso14a_clear_trace();\r
7bc95e2e 45 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r
8556b852
M
46\r
47 LED_A_ON();\r
48 LED_B_OFF();\r
49 LED_C_OFF();\r
50\r
51 while (true) {\r
52 if(!iso14443a_select_card(uid, NULL, &cuid)) {\r
baeaf579 53 if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");\r
8556b852
M
54 break;\r
55 };\r
56\r
57 if(mifare_classic_auth(pcs, cuid, blockNo, keyType, ui64Key, AUTH_FIRST)) {\r
baeaf579 58 if (MF_DBGLEVEL >= 1) Dbprintf("Auth error");\r
8556b852
M
59 break;\r
60 };\r
61 \r
62 if(mifare_classic_readblock(pcs, cuid, blockNo, dataoutbuf)) {\r
baeaf579 63 if (MF_DBGLEVEL >= 1) Dbprintf("Read block error");\r
8556b852
M
64 break;\r
65 };\r
66\r
67 if(mifare_classic_halt(pcs, cuid)) {\r
baeaf579 68 if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");\r
8556b852
M
69 break;\r
70 };\r
71 \r
72 isOK = 1;\r
73 break;\r
74 }\r
75 \r
76 // ----------------------------- crypto1 destroy\r
77 crypto1_destroy(pcs);\r
78 \r
22f1c577 79 if (MF_DBGLEVEL >= 2) DbpString("READ BLOCK FINISHED");\r
8556b852 80\r
8556b852 81 LED_B_ON();\r
baeaf579 82 cmd_send(CMD_ACK,isOK,0,0,dataoutbuf,16);\r
8556b852
M
83 LED_B_OFF();\r
84\r
8556b852
M
85 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
86 LEDsoff();\r
f38a1528 87}\r
7cc204bf 88\r
f38a1528 89\r
90void MifareUC_Auth1(uint8_t arg0, uint8_t *datain){\r
91 // variables\r
92 byte_t isOK = 0;\r
93 byte_t dataoutbuf[16];\r
94 uint8_t uid[10];\r
95 uint32_t cuid;\r
96 \r
97 // clear trace\r
98 iso14a_clear_trace();\r
99 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r
100 \r
101 LED_A_ON();\r
102 LED_B_OFF();\r
103 LED_C_OFF();\r
104 \r
105\r
106 if(!iso14443a_select_card(uid, NULL, &cuid)) {\r
107 if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card, something went wrong before auth");\r
108 };\r
f38a1528 109 \r
110 if(mifare_ultra_auth1(cuid, dataoutbuf)){\r
111 if (MF_DBGLEVEL >= 1) Dbprintf("Authentication part1: Fail."); \r
112 }\r
113\r
114 isOK=1;\r
115 if (MF_DBGLEVEL >= 2) DbpString("AUTH 1 FINISHED");\r
116 \r
117 LED_B_ON();\r
118 cmd_send(CMD_ACK,isOK,cuid,0,dataoutbuf,11);\r
119 LED_B_OFF();\r
120 \r
121 // Thats it...\r
122 LEDsoff();\r
123}\r
124void MifareUC_Auth2(uint32_t arg0, uint8_t *datain){\r
125 // params\r
126 uint32_t cuid = arg0;\r
127 uint8_t key[16]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};\r
128 // variables\r
129 byte_t isOK = 0;\r
130 byte_t dataoutbuf[16];\r
131 \r
132 memcpy(key, datain, 16);\r
133 \r
134 LED_A_ON();\r
135 LED_B_OFF();\r
136 LED_C_OFF();\r
137 \r
138 if(mifare_ultra_auth2(cuid, key, dataoutbuf)){\r
139 if (MF_DBGLEVEL >= 1) Dbprintf("Authentication part2: Fail..."); \r
140 }\r
141 isOK=1;\r
142 if (MF_DBGLEVEL >= 2) DbpString("AUTH 2 FINISHED");\r
143 \r
144 LED_B_ON();\r
145 cmd_send(CMD_ACK,isOK,0,0,dataoutbuf,11);\r
146 LED_B_OFF();\r
147 \r
148 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
149 LEDsoff();\r
7cc204bf 150}\r
151\r
152void MifareUReadBlock(uint8_t arg0,uint8_t *datain)\r
153{\r
154 // params\r
155 uint8_t blockNo = arg0;\r
156 \r
157 // variables\r
158 byte_t isOK = 0;\r
159 byte_t dataoutbuf[16];\r
160 uint8_t uid[10];\r
161 uint32_t cuid;\r
162 \r
163 // clear trace\r
164 iso14a_clear_trace();\r
7bc95e2e 165 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r
7cc204bf 166 \r
167 LED_A_ON();\r
168 LED_B_OFF();\r
169 LED_C_OFF();\r
170 \r
171 while (true) {\r
172 if(!iso14443a_select_card(uid, NULL, &cuid)) {\r
173 if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");\r
174 break;\r
175 };\r
176 \r
177 if(mifare_ultra_readblock(cuid, blockNo, dataoutbuf)) {\r
178 if (MF_DBGLEVEL >= 1) Dbprintf("Read block error");\r
179 break;\r
180 };\r
181 \r
182 if(mifare_ultra_halt(cuid)) {\r
183 if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");\r
184 break;\r
185 };\r
186 \r
187 isOK = 1;\r
188 break;\r
189 }\r
190 \r
191 if (MF_DBGLEVEL >= 2) DbpString("READ BLOCK FINISHED");\r
192 \r
7cc204bf 193 LED_B_ON();\r
4a79e52c 194 cmd_send(CMD_ACK,isOK,0,0,dataoutbuf,16);\r
7cc204bf 195 LED_B_OFF();\r
7cc204bf 196 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
197 LEDsoff();\r
198}\r
199\r
200//-----------------------------------------------------------------------------\r
baeaf579 201// Select, Authenticate, Read a MIFARE tag. \r
202// read sector (data = 4 x 16 bytes = 64 bytes, or 16 x 16 bytes = 256 bytes)\r
8556b852
M
203//-----------------------------------------------------------------------------\r
204void MifareReadSector(uint8_t arg0, uint8_t arg1, uint8_t arg2, uint8_t *datain)\r
205{\r
206 // params\r
207 uint8_t sectorNo = arg0;\r
208 uint8_t keyType = arg1;\r
209 uint64_t ui64Key = 0;\r
210 ui64Key = bytes_to_num(datain, 6);\r
211 \r
212 // variables\r
4a79e52c 213 byte_t isOK = 0;\r
baeaf579 214 byte_t dataoutbuf[16 * 16];\r
1c611bbd 215 uint8_t uid[10];\r
8556b852
M
216 uint32_t cuid;\r
217 struct Crypto1State mpcs = {0, 0};\r
218 struct Crypto1State *pcs;\r
219 pcs = &mpcs;\r
220\r
221 // clear trace\r
baeaf579 222 iso14a_clear_trace();\r
8556b852 223\r
7bc95e2e 224 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r
8556b852
M
225\r
226 LED_A_ON();\r
227 LED_B_OFF();\r
228 LED_C_OFF();\r
229\r
baeaf579 230 isOK = 1;\r
231 if(!iso14443a_select_card(uid, NULL, &cuid)) {\r
232 isOK = 0;\r
8556b852 233 if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");\r
baeaf579 234 }\r
8556b852 235\r
baeaf579 236 \r
237 if(isOK && mifare_classic_auth(pcs, cuid, FirstBlockOfSector(sectorNo), keyType, ui64Key, AUTH_FIRST)) {\r
238 isOK = 0;\r
8556b852 239 if (MF_DBGLEVEL >= 1) Dbprintf("Auth error");\r
baeaf579 240 }\r
241 \r
242 for (uint8_t blockNo = 0; isOK && blockNo < NumBlocksPerSector(sectorNo); blockNo++) {\r
243 if(mifare_classic_readblock(pcs, cuid, FirstBlockOfSector(sectorNo) + blockNo, dataoutbuf + 16 * blockNo)) {\r
244 isOK = 0;\r
245 if (MF_DBGLEVEL >= 1) Dbprintf("Read sector %2d block %2d error", sectorNo, blockNo);\r
8556b852 246 break;\r
baeaf579 247 }\r
248 }\r
8556b852 249 \r
baeaf579 250 if(mifare_classic_halt(pcs, cuid)) {\r
8556b852 251 if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");\r
8556b852 252 }\r
baeaf579 253\r
8556b852
M
254 // ----------------------------- crypto1 destroy\r
255 crypto1_destroy(pcs);\r
256 \r
257 if (MF_DBGLEVEL >= 2) DbpString("READ SECTOR FINISHED");\r
258\r
8556b852 259 LED_B_ON();\r
baeaf579 260 cmd_send(CMD_ACK,isOK,0,0,dataoutbuf,16*NumBlocksPerSector(sectorNo));\r
6e82300d 261 LED_B_OFF();\r
8556b852
M
262\r
263 // Thats it...\r
264 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
265 LEDsoff();\r
7cc204bf 266}\r
267\r
f38a1528 268void MifareUReadCard(uint8_t arg0, int arg1, uint8_t *datain)\r
7cc204bf 269{\r
270 // params\r
271 uint8_t sectorNo = arg0;\r
f38a1528 272 int Pages=arg1;\r
273 int count_Pages=0;\r
7cc204bf 274 // variables\r
275 byte_t isOK = 0;\r
f38a1528 276 byte_t dataoutbuf[44 * 4];\r
7cc204bf 277 uint8_t uid[10];\r
278 uint32_t cuid;\r
279\r
280 // clear trace\r
281 iso14a_clear_trace();\r
7cc204bf 282\r
7bc95e2e 283 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r
7cc204bf 284\r
285 LED_A_ON();\r
286 LED_B_OFF();\r
287 LED_C_OFF();\r
f38a1528 288 Dbprintf("Pages %d",Pages);\r
7cc204bf 289 while (true) {\r
290 if(!iso14443a_select_card(uid, NULL, &cuid)) {\r
291 if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");\r
292 break;\r
293 };\r
f38a1528 294 for(int sec=0;sec<Pages;sec++){\r
7cc204bf 295 if(mifare_ultra_readblock(cuid, sectorNo * 4 + sec, dataoutbuf + 4 * sec)) {\r
296 if (MF_DBGLEVEL >= 1) Dbprintf("Read block %d error",sec);\r
297 break;\r
f38a1528 298 }else{\r
299 count_Pages++;\r
7cc204bf 300 };\r
301 }\r
302 if(mifare_ultra_halt(cuid)) {\r
303 if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");\r
304 break;\r
305 };\r
306\r
307 isOK = 1;\r
308 break;\r
309 }\r
f38a1528 310 Dbprintf("Pages read %d",count_Pages);\r
7cc204bf 311 if (MF_DBGLEVEL >= 2) DbpString("READ CARD FINISHED");\r
312\r
7cc204bf 313 LED_B_ON();\r
f38a1528 314 if (Pages==16) cmd_send(CMD_ACK,isOK,0,0,dataoutbuf,64);\r
315 if (Pages==44 && count_Pages==16) cmd_send(CMD_ACK,isOK,0,0,dataoutbuf,64);\r
316 if (Pages==44 && count_Pages>16) cmd_send(CMD_ACK,isOK,0,0,dataoutbuf,176);\r
7cc204bf 317 LED_B_OFF();\r
318\r
319 // Thats it...\r
320 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
321 LEDsoff();\r
7cc204bf 322\r
323}\r
324\r
325\r
326//-----------------------------------------------------------------------------\r
baeaf579 327// Select, Authenticate, Write a MIFARE tag. \r
7cc204bf 328// read block\r
8556b852
M
329//-----------------------------------------------------------------------------\r
330void MifareWriteBlock(uint8_t arg0, uint8_t arg1, uint8_t arg2, uint8_t *datain)\r
331{\r
332 // params\r
333 uint8_t blockNo = arg0;\r
334 uint8_t keyType = arg1;\r
335 uint64_t ui64Key = 0;\r
336 byte_t blockdata[16];\r
337\r
338 ui64Key = bytes_to_num(datain, 6);\r
339 memcpy(blockdata, datain + 10, 16);\r
340 \r
341 // variables\r
342 byte_t isOK = 0;\r
1c611bbd 343 uint8_t uid[10];\r
8556b852
M
344 uint32_t cuid;\r
345 struct Crypto1State mpcs = {0, 0};\r
346 struct Crypto1State *pcs;\r
347 pcs = &mpcs;\r
348\r
349 // clear trace\r
d19929cb 350 iso14a_clear_trace();\r
8556b852 351\r
7bc95e2e 352 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r
8556b852
M
353\r
354 LED_A_ON();\r
355 LED_B_OFF();\r
356 LED_C_OFF();\r
357\r
358 while (true) {\r
359 if(!iso14443a_select_card(uid, NULL, &cuid)) {\r
360 if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");\r
361 break;\r
362 };\r
363\r
364 if(mifare_classic_auth(pcs, cuid, blockNo, keyType, ui64Key, AUTH_FIRST)) {\r
365 if (MF_DBGLEVEL >= 1) Dbprintf("Auth error");\r
366 break;\r
367 };\r
368 \r
369 if(mifare_classic_writeblock(pcs, cuid, blockNo, blockdata)) {\r
370 if (MF_DBGLEVEL >= 1) Dbprintf("Write block error");\r
371 break;\r
372 };\r
373\r
374 if(mifare_classic_halt(pcs, cuid)) {\r
375 if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");\r
376 break;\r
377 };\r
378 \r
379 isOK = 1;\r
380 break;\r
381 }\r
382 \r
383 // ----------------------------- crypto1 destroy\r
384 crypto1_destroy(pcs);\r
385 \r
386 if (MF_DBGLEVEL >= 2) DbpString("WRITE BLOCK FINISHED");\r
387\r
8556b852 388 LED_B_ON();\r
9492e0b0 389 cmd_send(CMD_ACK,isOK,0,0,0,0);\r
6e82300d 390 LED_B_OFF();\r
8556b852
M
391\r
392\r
393 // Thats it...\r
394 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
395 LEDsoff();\r
7cc204bf 396}\r
397\r
398void MifareUWriteBlock(uint8_t arg0, uint8_t *datain)\r
399{\r
400 // params\r
401 uint8_t blockNo = arg0;\r
402 byte_t blockdata[16];\r
403\r
404 memset(blockdata,'\0',16);\r
405 memcpy(blockdata, datain,16);\r
406 \r
407 // variables\r
408 byte_t isOK = 0;\r
409 uint8_t uid[10];\r
410 uint32_t cuid;\r
411\r
412 // clear trace\r
413 iso14a_clear_trace();\r
7cc204bf 414\r
7bc95e2e 415 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r
7cc204bf 416\r
417 LED_A_ON();\r
418 LED_B_OFF();\r
419 LED_C_OFF();\r
420\r
421 while (true) {\r
422 if(!iso14443a_select_card(uid, NULL, &cuid)) {\r
423 if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");\r
424 break;\r
425 };\r
426\r
427 if(mifare_ultra_writeblock(cuid, blockNo, blockdata)) {\r
428 if (MF_DBGLEVEL >= 1) Dbprintf("Write block error");\r
429 break;\r
430 };\r
431\r
432 if(mifare_ultra_halt(cuid)) {\r
433 if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");\r
434 break;\r
435 };\r
436 \r
437 isOK = 1;\r
438 break;\r
439 }\r
440 \r
441 if (MF_DBGLEVEL >= 2) DbpString("WRITE BLOCK FINISHED");\r
442\r
7cc204bf 443 LED_B_ON();\r
baeaf579 444 cmd_send(CMD_ACK,isOK,0,0,0,0);\r
7cc204bf 445 LED_B_OFF();\r
446\r
447\r
448 // Thats it...\r
449 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
450 LEDsoff();\r
451// iso14a_set_tracing(TRUE);\r
7cc204bf 452}\r
453\r
454void MifareUWriteBlock_Special(uint8_t arg0, uint8_t *datain)\r
455{\r
baeaf579 456 // params\r
457 uint8_t blockNo = arg0;\r
458 byte_t blockdata[4];\r
459 \r
7cc204bf 460 memcpy(blockdata, datain,4);\r
461\r
baeaf579 462 // variables\r
463 byte_t isOK = 0;\r
464 uint8_t uid[10];\r
465 uint32_t cuid;\r
7cc204bf 466\r
baeaf579 467 // clear trace\r
468 iso14a_clear_trace();\r
7cc204bf 469\r
baeaf579 470 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r
7cc204bf 471\r
baeaf579 472 LED_A_ON();\r
473 LED_B_OFF();\r
474 LED_C_OFF();\r
7cc204bf 475\r
baeaf579 476 while (true) {\r
477 if(!iso14443a_select_card(uid, NULL, &cuid)) {\r
478 if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");\r
479 break;\r
480 };\r
7cc204bf 481\r
baeaf579 482 if(mifare_ultra_special_writeblock(cuid, blockNo, blockdata)) {\r
483 if (MF_DBGLEVEL >= 1) Dbprintf("Write block error");\r
484 break;\r
485 };\r
7cc204bf 486\r
baeaf579 487 if(mifare_ultra_halt(cuid)) {\r
488 if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");\r
489 break;\r
490 };\r
7cc204bf 491\r
baeaf579 492 isOK = 1;\r
493 break;\r
494 }\r
7cc204bf 495\r
baeaf579 496 if (MF_DBGLEVEL >= 2) DbpString("WRITE BLOCK FINISHED");\r
7cc204bf 497\r
baeaf579 498 LED_B_ON();\r
499 cmd_send(CMD_ACK,isOK,0,0,0,0);\r
500 LED_B_OFF();\r
7cc204bf 501\r
baeaf579 502 // Thats it...\r
503 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
504 LEDsoff();\r
7cc204bf 505}\r
506\r
507// Return 1 if the nonce is invalid else return 0\r
508int valid_nonce(uint32_t Nt, uint32_t NtEnc, uint32_t Ks1, byte_t * parity) {\r
509 return ((oddparity((Nt >> 24) & 0xFF) == ((parity[0]) ^ oddparity((NtEnc >> 24) & 0xFF) ^ BIT(Ks1,16))) & \\r
8556b852
M
510 (oddparity((Nt >> 16) & 0xFF) == ((parity[1]) ^ oddparity((NtEnc >> 16) & 0xFF) ^ BIT(Ks1,8))) & \\r
511 (oddparity((Nt >> 8) & 0xFF) == ((parity[2]) ^ oddparity((NtEnc >> 8) & 0xFF) ^ BIT(Ks1,0)))) ? 1 : 0;\r
512}\r
513\r
9492e0b0 514\r
8556b852
M
515//-----------------------------------------------------------------------------\r
516// MIFARE nested authentication. \r
517// \r
518//-----------------------------------------------------------------------------\r
9492e0b0 519void MifareNested(uint32_t arg0, uint32_t arg1, uint32_t calibrate, uint8_t *datain)\r
8556b852
M
520{\r
521 // params\r
9492e0b0 522 uint8_t blockNo = arg0 & 0xff;\r
523 uint8_t keyType = (arg0 >> 8) & 0xff;\r
524 uint8_t targetBlockNo = arg1 & 0xff;\r
525 uint8_t targetKeyType = (arg1 >> 8) & 0xff;\r
8556b852
M
526 uint64_t ui64Key = 0;\r
527\r
528 ui64Key = bytes_to_num(datain, 6);\r
529 \r
530 // variables\r
9492e0b0 531 uint16_t rtr, i, j, len;\r
532 uint16_t davg;\r
533 static uint16_t dmin, dmax;\r
1c611bbd 534 uint8_t uid[10];\r
8556b852 535 uint32_t cuid, nt1, nt2, nttmp, nttest, par, ks1;\r
9492e0b0 536 uint32_t target_nt[2], target_ks[2];\r
537 \r
8556b852 538 uint8_t par_array[4];\r
9492e0b0 539 uint16_t ncount = 0;\r
8556b852
M
540 struct Crypto1State mpcs = {0, 0};\r
541 struct Crypto1State *pcs;\r
542 pcs = &mpcs;\r
543 uint8_t* receivedAnswer = mifare_get_bigbufptr();\r
544\r
9492e0b0 545 uint32_t auth1_time, auth2_time;\r
546 static uint16_t delta_time;\r
547\r
8556b852 548 // clear trace\r
d19929cb 549 iso14a_clear_trace();\r
9492e0b0 550 iso14a_set_tracing(false);\r
8556b852 551 \r
7bc95e2e 552 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r
8556b852
M
553\r
554 LED_A_ON();\r
8556b852
M
555 LED_C_OFF();\r
556\r
8556b852 557\r
9492e0b0 558 // statistics on nonce distance\r
559 if (calibrate) { // for first call only. Otherwise reuse previous calibration\r
560 LED_B_ON();\r
22f1c577 561 WDT_HIT();\r
8556b852 562\r
9492e0b0 563 davg = dmax = 0;\r
564 dmin = 2000;\r
565 delta_time = 0;\r
8556b852 566 \r
9492e0b0 567 for (rtr = 0; rtr < 17; rtr++) {\r
8556b852 568\r
9492e0b0 569 // prepare next select. No need to power down the card.\r
570 if(mifare_classic_halt(pcs, cuid)) {\r
571 if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Halt error");\r
572 rtr--;\r
573 continue;\r
574 }\r
575\r
576 if(!iso14443a_select_card(uid, NULL, &cuid)) {\r
577 if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Can't select card");\r
578 rtr--;\r
579 continue;\r
580 };\r
581\r
582 auth1_time = 0;\r
583 if(mifare_classic_authex(pcs, cuid, blockNo, keyType, ui64Key, AUTH_FIRST, &nt1, &auth1_time)) {\r
584 if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Auth1 error");\r
585 rtr--;\r
586 continue;\r
587 };\r
588\r
589 if (delta_time) {\r
590 auth2_time = auth1_time + delta_time;\r
591 } else {\r
592 auth2_time = 0;\r
593 }\r
594 if(mifare_classic_authex(pcs, cuid, blockNo, keyType, ui64Key, AUTH_NESTED, &nt2, &auth2_time)) {\r
595 if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Auth2 error");\r
596 rtr--;\r
597 continue;\r
598 };\r
599\r
22f1c577 600 nttmp = prng_successor(nt1, 140); //NXP Mifare is typical around 840,but for some unlicensed/compatible mifare card this can be 160\r
601 for (i = 141; i < 1200; i++) {\r
9492e0b0 602 nttmp = prng_successor(nttmp, 1);\r
22f1c577 603 if (nttmp == nt2) {break;}\r
9492e0b0 604 }\r
605\r
606 if (i != 1200) {\r
607 if (rtr != 0) {\r
608 davg += i;\r
609 dmin = MIN(dmin, i);\r
610 dmax = MAX(dmax, i);\r
611 }\r
612 else {\r
613 delta_time = auth2_time - auth1_time + 32; // allow some slack for proper timing\r
614 }\r
615 if (MF_DBGLEVEL >= 3) Dbprintf("Nested: calibrating... ntdist=%d", i);\r
616 }\r
8556b852
M
617 }\r
618 \r
22f1c577 619 if (rtr <= 1) return;\r
8556b852 620\r
9492e0b0 621 davg = (davg + (rtr - 1)/2) / (rtr - 1);\r
622 \r
623 if (MF_DBGLEVEL >= 3) Dbprintf("min=%d max=%d avg=%d, delta_time=%d", dmin, dmax, davg, delta_time);\r
8556b852 624\r
9492e0b0 625 dmin = davg - 2;\r
626 dmax = davg + 2;\r
627 \r
628 LED_B_OFF();\r
629 \r
630 }\r
8556b852
M
631// ------------------------------------------------------------------------------------------------- \r
632 \r
633 LED_C_ON();\r
634\r
635 // get crypted nonces for target sector\r
9492e0b0 636 for(i=0; i < 2; i++) { // look for exactly two different nonces\r
8556b852 637\r
22f1c577 638 WDT_HIT(); \r
639 if(BUTTON_PRESS()) {\r
640 DbpString("Nested: cancelled");\r
641 crypto1_destroy(pcs);\r
642 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
643 LEDsoff();\r
644 return;\r
645 }\r
646\r
9492e0b0 647 target_nt[i] = 0;\r
648 while(target_nt[i] == 0) { // continue until we have an unambiguous nonce\r
22f1c577 649 \r
9492e0b0 650 // prepare next select. No need to power down the card.\r
651 if(mifare_classic_halt(pcs, cuid)) {\r
652 if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Halt error");\r
653 continue;\r
654 }\r
8556b852 655\r
9492e0b0 656 if(!iso14443a_select_card(uid, NULL, &cuid)) {\r
657 if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Can't select card");\r
658 continue;\r
659 };\r
8556b852 660 \r
9492e0b0 661 auth1_time = 0;\r
662 if(mifare_classic_authex(pcs, cuid, blockNo, keyType, ui64Key, AUTH_FIRST, &nt1, &auth1_time)) {\r
663 if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Auth1 error");\r
664 continue;\r
665 };\r
8556b852 666\r
9492e0b0 667 // nested authentication\r
668 auth2_time = auth1_time + delta_time;\r
669 len = mifare_sendcmd_shortex(pcs, AUTH_NESTED, 0x60 + (targetKeyType & 0x01), targetBlockNo, receivedAnswer, &par, &auth2_time);\r
670 if (len != 4) {\r
671 if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Auth2 error len=%d", len);\r
672 continue;\r
673 };\r
8556b852 674 \r
9492e0b0 675 nt2 = bytes_to_num(receivedAnswer, 4); \r
676 if (MF_DBGLEVEL >= 3) Dbprintf("Nonce#%d: Testing nt1=%08x nt2enc=%08x nt2par=%02x", i+1, nt1, nt2, par);\r
8556b852 677 \r
9492e0b0 678 // Parity validity check\r
679 for (j = 0; j < 4; j++) {\r
680 par_array[j] = (oddparity(receivedAnswer[j]) != ((par & 0x08) >> 3));\r
681 par = par << 1;\r
682 }\r
683 \r
684 ncount = 0;\r
685 nttest = prng_successor(nt1, dmin - 1);\r
686 for (j = dmin; j < dmax + 1; j++) {\r
687 nttest = prng_successor(nttest, 1);\r
688 ks1 = nt2 ^ nttest;\r
689\r
690 if (valid_nonce(nttest, nt2, ks1, par_array)){\r
691 if (ncount > 0) { // we are only interested in disambiguous nonces, try again\r
692 if (MF_DBGLEVEL >= 3) Dbprintf("Nonce#%d: dismissed (ambigous), ntdist=%d", i+1, j);\r
693 target_nt[i] = 0;\r
694 break;\r
695 }\r
696 target_nt[i] = nttest;\r
697 target_ks[i] = ks1;\r
698 ncount++;\r
699 if (i == 1 && target_nt[1] == target_nt[0]) { // we need two different nonces\r
700 target_nt[i] = 0;\r
701 if (MF_DBGLEVEL >= 3) Dbprintf("Nonce#2: dismissed (= nonce#1), ntdist=%d", j);\r
8556b852
M
702 break;\r
703 }\r
9492e0b0 704 if (MF_DBGLEVEL >= 3) Dbprintf("Nonce#%d: valid, ntdist=%d", i+1, j);\r
8556b852 705 }\r
8556b852 706 }\r
9492e0b0 707 if (target_nt[i] == 0 && j == dmax+1 && MF_DBGLEVEL >= 3) Dbprintf("Nonce#%d: dismissed (all invalid)", i+1);\r
8556b852
M
708 }\r
709 }\r
22f1c577 710 \r
8556b852
M
711 LED_C_OFF();\r
712 \r
713 // ----------------------------- crypto1 destroy\r
714 crypto1_destroy(pcs);\r
715 \r
716 // add trace trailer\r
22f1c577 717// memset(uid, 0x44, 4);\r
718// LogTrace(uid, 4, 0, 0, TRUE);\r
8556b852 719\r
9492e0b0 720 byte_t buf[4 + 4 * 4];\r
721 memcpy(buf, &cuid, 4);\r
722 memcpy(buf+4, &target_nt[0], 4);\r
723 memcpy(buf+8, &target_ks[0], 4);\r
724 memcpy(buf+12, &target_nt[1], 4);\r
725 memcpy(buf+16, &target_ks[1], 4);\r
8556b852
M
726 \r
727 LED_B_ON();\r
9492e0b0 728 cmd_send(CMD_ACK, 0, 2, targetBlockNo + (targetKeyType * 0x100), buf, sizeof(buf));\r
6e82300d 729 LED_B_OFF();\r
8556b852 730\r
9492e0b0 731 if (MF_DBGLEVEL >= 3) DbpString("NESTED FINISHED");\r
8556b852 732\r
8556b852
M
733 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
734 LEDsoff();\r
9492e0b0 735 iso14a_set_tracing(TRUE);\r
8556b852
M
736}\r
737\r
738//-----------------------------------------------------------------------------\r
9492e0b0 739// MIFARE check keys. key count up to 85. \r
8556b852
M
740// \r
741//-----------------------------------------------------------------------------\r
742void MifareChkKeys(uint8_t arg0, uint8_t arg1, uint8_t arg2, uint8_t *datain)\r
743{\r
744 // params\r
745 uint8_t blockNo = arg0;\r
746 uint8_t keyType = arg1;\r
747 uint8_t keyCount = arg2;\r
748 uint64_t ui64Key = 0;\r
749 \r
750 // variables\r
751 int i;\r
752 byte_t isOK = 0;\r
1c611bbd 753 uint8_t uid[10];\r
8556b852
M
754 uint32_t cuid;\r
755 struct Crypto1State mpcs = {0, 0};\r
756 struct Crypto1State *pcs;\r
757 pcs = &mpcs;\r
758 \r
759 // clear debug level\r
760 int OLD_MF_DBGLEVEL = MF_DBGLEVEL; \r
761 MF_DBGLEVEL = MF_DBG_NONE;\r
762 \r
763 // clear trace\r
d19929cb 764 iso14a_clear_trace();\r
9492e0b0 765 iso14a_set_tracing(TRUE);\r
8556b852 766\r
7bc95e2e 767 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r
8556b852
M
768\r
769 LED_A_ON();\r
770 LED_B_OFF();\r
771 LED_C_OFF();\r
772\r
8556b852 773 for (i = 0; i < keyCount; i++) {\r
9492e0b0 774 if(mifare_classic_halt(pcs, cuid)) {\r
775 if (MF_DBGLEVEL >= 1) Dbprintf("ChkKeys: Halt error");\r
776 }\r
8556b852
M
777\r
778 if(!iso14443a_select_card(uid, NULL, &cuid)) {\r
9492e0b0 779 if (OLD_MF_DBGLEVEL >= 1) Dbprintf("ChkKeys: Can't select card");\r
8556b852
M
780 break;\r
781 };\r
782\r
783 ui64Key = bytes_to_num(datain + i * 6, 6);\r
784 if(mifare_classic_auth(pcs, cuid, blockNo, keyType, ui64Key, AUTH_FIRST)) {\r
785 continue;\r
786 };\r
787 \r
788 isOK = 1;\r
789 break;\r
790 }\r
791 \r
792 // ----------------------------- crypto1 destroy\r
793 crypto1_destroy(pcs);\r
794 \r
8556b852 795 LED_B_ON();\r
6e82300d 796 cmd_send(CMD_ACK,isOK,0,0,datain + i * 6,6);\r
8556b852
M
797 LED_B_OFF();\r
798\r
799 // Thats it...\r
800 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
801 LEDsoff();\r
802\r
803 // restore debug level\r
804 MF_DBGLEVEL = OLD_MF_DBGLEVEL; \r
805}\r
806\r
807//-----------------------------------------------------------------------------\r
808// MIFARE commands set debug level\r
809// \r
810//-----------------------------------------------------------------------------\r
811void MifareSetDbgLvl(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain){\r
812 MF_DBGLEVEL = arg0;\r
813 Dbprintf("Debug level: %d", MF_DBGLEVEL);\r
814}\r
815\r
816//-----------------------------------------------------------------------------\r
817// Work with emulator memory\r
818// \r
819//-----------------------------------------------------------------------------\r
820void MifareEMemClr(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain){\r
821 emlClearMem();\r
822}\r
823\r
824void MifareEMemSet(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain){\r
825 emlSetMem(datain, arg0, arg1); // data, block num, blocks count\r
826}\r
827\r
828void MifareEMemGet(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain){\r
baeaf579 829 byte_t buf[48];\r
830 emlGetMem(buf, arg0, arg1); // data, block num, blocks count (max 4)\r
8556b852
M
831\r
832 LED_B_ON();\r
baeaf579 833 cmd_send(CMD_ACK,arg0,arg1,0,buf,48);\r
8556b852
M
834 LED_B_OFF();\r
835}\r
836\r
837//-----------------------------------------------------------------------------\r
838// Load a card into the emulator memory\r
839// \r
840//-----------------------------------------------------------------------------\r
841void MifareECardLoad(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain){\r
baeaf579 842 uint8_t numSectors = arg0;\r
8556b852
M
843 uint8_t keyType = arg1;\r
844 uint64_t ui64Key = 0;\r
845 uint32_t cuid;\r
846 struct Crypto1State mpcs = {0, 0};\r
847 struct Crypto1State *pcs;\r
848 pcs = &mpcs;\r
849\r
850 // variables\r
851 byte_t dataoutbuf[16];\r
ab8b654e 852 byte_t dataoutbuf2[16];\r
1c611bbd 853 uint8_t uid[10];\r
8556b852
M
854\r
855 // clear trace\r
d19929cb 856 iso14a_clear_trace();\r
8556b852
M
857 iso14a_set_tracing(false);\r
858 \r
7bc95e2e 859 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r
8556b852
M
860\r
861 LED_A_ON();\r
862 LED_B_OFF();\r
863 LED_C_OFF();\r
864 \r
baeaf579 865 bool isOK = true;\r
866\r
867 if(!iso14443a_select_card(uid, NULL, &cuid)) {\r
868 isOK = false;\r
869 if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");\r
870 }\r
8556b852 871 \r
baeaf579 872 for (uint8_t sectorNo = 0; isOK && sectorNo < numSectors; sectorNo++) {\r
873 ui64Key = emlGetKey(sectorNo, keyType);\r
874 if (sectorNo == 0){\r
875 if(isOK && mifare_classic_auth(pcs, cuid, FirstBlockOfSector(sectorNo), keyType, ui64Key, AUTH_FIRST)) {\r
876 isOK = false;\r
877 if (MF_DBGLEVEL >= 1) Dbprintf("Sector[%2d]. Auth error", sectorNo);\r
8556b852 878 break;\r
baeaf579 879 }\r
880 } else {\r
881 if(isOK && mifare_classic_auth(pcs, cuid, FirstBlockOfSector(sectorNo), keyType, ui64Key, AUTH_NESTED)) {\r
882 isOK = false;\r
883 if (MF_DBGLEVEL >= 1) Dbprintf("Sector[%2d]. Auth nested error", sectorNo);\r
8556b852 884 break;\r
baeaf579 885 }\r
886 }\r
887 \r
888 for (uint8_t blockNo = 0; isOK && blockNo < NumBlocksPerSector(sectorNo); blockNo++) {\r
889 if(isOK && mifare_classic_readblock(pcs, cuid, FirstBlockOfSector(sectorNo) + blockNo, dataoutbuf)) {\r
890 isOK = false;\r
891 if (MF_DBGLEVEL >= 1) Dbprintf("Error reading sector %2d block %2d", sectorNo, blockNo);\r
ab8b654e
M
892 break;\r
893 };\r
baeaf579 894 if (isOK) {\r
895 if (blockNo < NumBlocksPerSector(sectorNo) - 1) {\r
896 emlSetMem(dataoutbuf, FirstBlockOfSector(sectorNo) + blockNo, 1);\r
897 } else { // sector trailer, keep the keys, set only the AC\r
898 emlGetMem(dataoutbuf2, FirstBlockOfSector(sectorNo) + blockNo, 1);\r
899 memcpy(&dataoutbuf2[6], &dataoutbuf[6], 4);\r
900 emlSetMem(dataoutbuf2, FirstBlockOfSector(sectorNo) + blockNo, 1);\r
901 }\r
902 }\r
8556b852
M
903 }\r
904\r
baeaf579 905 }\r
906\r
907 if(mifare_classic_halt(pcs, cuid)) {\r
908 if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");\r
909 };\r
8556b852
M
910\r
911 // ----------------------------- crypto1 destroy\r
912 crypto1_destroy(pcs);\r
ab8b654e
M
913\r
914 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
915 LEDsoff();\r
8556b852
M
916 \r
917 if (MF_DBGLEVEL >= 2) DbpString("EMUL FILL SECTORS FINISHED");\r
918\r
8556b852
M
919}\r
920\r
0675f200
M
921\r
922//-----------------------------------------------------------------------------\r
923// Work with "magic Chinese" card (email him: ouyangweidaxian@live.cn)\r
924// \r
925//-----------------------------------------------------------------------------\r
926void MifareCSetBlock(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain){\r
927 \r
928 // params\r
929 uint8_t needWipe = arg0;\r
208a0166
M
930 // bit 0 - need get UID\r
931 // bit 1 - need wupC\r
932 // bit 2 - need HALT after sequence\r
933 // bit 3 - need init FPGA and field before sequence\r
934 // bit 4 - need reset FPGA and LED\r
935 uint8_t workFlags = arg1;\r
0675f200
M
936 uint8_t blockNo = arg2;\r
937 \r
938 // card commands\r
939 uint8_t wupC1[] = { 0x40 }; \r
940 uint8_t wupC2[] = { 0x43 }; \r
941 uint8_t wipeC[] = { 0x41 }; \r
942 \r
943 // variables\r
944 byte_t isOK = 0;\r
1c611bbd 945 uint8_t uid[10];\r
0675f200
M
946 uint8_t d_block[18];\r
947 uint32_t cuid;\r
948 \r
1c611bbd 949 memset(uid, 0x00, 10);\r
0675f200
M
950 uint8_t* receivedAnswer = mifare_get_bigbufptr();\r
951 \r
208a0166
M
952 if (workFlags & 0x08) {\r
953 // clear trace\r
d19929cb 954 iso14a_clear_trace();\r
208a0166 955 iso14a_set_tracing(TRUE);\r
0675f200 956\r
7bc95e2e 957 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r
0675f200 958\r
208a0166
M
959 LED_A_ON();\r
960 LED_B_OFF();\r
961 LED_C_OFF();\r
0675f200 962 \r
208a0166
M
963 SpinDelay(300);\r
964 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
965 SpinDelay(100);\r
966 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A | FPGA_HF_ISO14443A_READER_MOD);\r
967 }\r
0675f200
M
968\r
969 while (true) {\r
970 // get UID from chip\r
208a0166 971 if (workFlags & 0x01) {\r
0675f200
M
972 if(!iso14443a_select_card(uid, NULL, &cuid)) {\r
973 if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");\r
974 break;\r
975 };\r
976\r
977 if(mifare_classic_halt(NULL, cuid)) {\r
978 if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");\r
979 break;\r
980 };\r
981 };\r
982 \r
983 // reset chip\r
984 if (needWipe){\r
9492e0b0 985 ReaderTransmitBitsPar(wupC1,7,0, NULL);\r
0675f200
M
986 if(!ReaderReceive(receivedAnswer) || (receivedAnswer[0] != 0x0a)) {\r
987 if (MF_DBGLEVEL >= 1) Dbprintf("wupC1 error");\r
988 break;\r
989 };\r
990\r
9492e0b0 991 ReaderTransmit(wipeC, sizeof(wipeC), NULL);\r
0675f200
M
992 if(!ReaderReceive(receivedAnswer) || (receivedAnswer[0] != 0x0a)) {\r
993 if (MF_DBGLEVEL >= 1) Dbprintf("wipeC error");\r
994 break;\r
995 };\r
996\r
997 if(mifare_classic_halt(NULL, cuid)) {\r
998 if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");\r
999 break;\r
1000 };\r
1001 }; \r
1002\r
545a1f38 1003 // write block\r
208a0166 1004 if (workFlags & 0x02) {\r
9492e0b0 1005 ReaderTransmitBitsPar(wupC1,7,0, NULL);\r
208a0166
M
1006 if(!ReaderReceive(receivedAnswer) || (receivedAnswer[0] != 0x0a)) {\r
1007 if (MF_DBGLEVEL >= 1) Dbprintf("wupC1 error");\r
1008 break;\r
1009 };\r
0675f200 1010\r
9492e0b0 1011 ReaderTransmit(wupC2, sizeof(wupC2), NULL);\r
208a0166
M
1012 if(!ReaderReceive(receivedAnswer) || (receivedAnswer[0] != 0x0a)) {\r
1013 if (MF_DBGLEVEL >= 1) Dbprintf("wupC2 error");\r
1014 break;\r
1015 };\r
1016 }\r
0675f200 1017\r
9492e0b0 1018 if ((mifare_sendcmd_short(NULL, 0, 0xA0, blockNo, receivedAnswer, NULL) != 1) || (receivedAnswer[0] != 0x0a)) {\r
0675f200
M
1019 if (MF_DBGLEVEL >= 1) Dbprintf("write block send command error");\r
1020 break;\r
1021 };\r
1022 \r
1023 memcpy(d_block, datain, 16);\r
1024 AppendCrc14443a(d_block, 16);\r
1025 \r
9492e0b0 1026 ReaderTransmit(d_block, sizeof(d_block), NULL);\r
0675f200
M
1027 if ((ReaderReceive(receivedAnswer) != 1) || (receivedAnswer[0] != 0x0a)) {\r
1028 if (MF_DBGLEVEL >= 1) Dbprintf("write block send data error");\r
1029 break;\r
1030 }; \r
1031 \r
208a0166
M
1032 if (workFlags & 0x04) {\r
1033 if (mifare_classic_halt(NULL, cuid)) {\r
1034 if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");\r
1035 break;\r
1036 };\r
1037 }\r
0675f200
M
1038 \r
1039 isOK = 1;\r
1040 break;\r
1041 }\r
1042 \r
0675f200 1043 LED_B_ON();\r
baeaf579 1044 cmd_send(CMD_ACK,isOK,0,0,uid,4);\r
0675f200
M
1045 LED_B_OFF();\r
1046\r
545a1f38 1047 if ((workFlags & 0x10) || (!isOK)) {\r
208a0166
M
1048 // Thats it...\r
1049 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
1050 LEDsoff();\r
1051 }\r
0675f200 1052}\r
545a1f38 1053\r
baeaf579 1054\r
545a1f38
M
1055void MifareCGetBlock(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain){\r
1056 \r
1057 // params\r
1058 // bit 1 - need wupC\r
1059 // bit 2 - need HALT after sequence\r
1060 // bit 3 - need init FPGA and field before sequence\r
1061 // bit 4 - need reset FPGA and LED\r
1062 uint8_t workFlags = arg0;\r
1063 uint8_t blockNo = arg2;\r
1064 \r
1065 // card commands\r
1066 uint8_t wupC1[] = { 0x40 }; \r
1067 uint8_t wupC2[] = { 0x43 }; \r
1068 \r
1069 // variables\r
1070 byte_t isOK = 0;\r
1071 uint8_t data[18];\r
1072 uint32_t cuid = 0;\r
1073 \r
1074 memset(data, 0x00, 18);\r
1075 uint8_t* receivedAnswer = mifare_get_bigbufptr();\r
1076 \r
1077 if (workFlags & 0x08) {\r
1078 // clear trace\r
d19929cb 1079 iso14a_clear_trace();\r
545a1f38
M
1080 iso14a_set_tracing(TRUE);\r
1081\r
7bc95e2e 1082 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r
545a1f38
M
1083\r
1084 LED_A_ON();\r
1085 LED_B_OFF();\r
1086 LED_C_OFF();\r
1087 \r
1088 SpinDelay(300);\r
1089 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
1090 SpinDelay(100);\r
1091 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A | FPGA_HF_ISO14443A_READER_MOD);\r
1092 }\r
1093\r
1094 while (true) {\r
1095 if (workFlags & 0x02) {\r
7bc95e2e 1096 ReaderTransmitBitsPar(wupC1,7,0, NULL);\r
545a1f38
M
1097 if(!ReaderReceive(receivedAnswer) || (receivedAnswer[0] != 0x0a)) {\r
1098 if (MF_DBGLEVEL >= 1) Dbprintf("wupC1 error");\r
1099 break;\r
1100 };\r
1101\r
9492e0b0 1102 ReaderTransmit(wupC2, sizeof(wupC2), NULL);\r
545a1f38
M
1103 if(!ReaderReceive(receivedAnswer) || (receivedAnswer[0] != 0x0a)) {\r
1104 if (MF_DBGLEVEL >= 1) Dbprintf("wupC2 error");\r
1105 break;\r
1106 };\r
1107 }\r
1108\r
1109 // read block\r
9492e0b0 1110 if ((mifare_sendcmd_short(NULL, 0, 0x30, blockNo, receivedAnswer, NULL) != 18)) {\r
545a1f38
M
1111 if (MF_DBGLEVEL >= 1) Dbprintf("read block send command error");\r
1112 break;\r
1113 };\r
1114 memcpy(data, receivedAnswer, 18);\r
1115 \r
1116 if (workFlags & 0x04) {\r
1117 if (mifare_classic_halt(NULL, cuid)) {\r
1118 if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");\r
1119 break;\r
1120 };\r
1121 }\r
1122 \r
1123 isOK = 1;\r
1124 break;\r
1125 }\r
1126 \r
545a1f38 1127 LED_B_ON();\r
baeaf579 1128 cmd_send(CMD_ACK,isOK,0,0,data,18);\r
545a1f38
M
1129 LED_B_OFF();\r
1130\r
1131 if ((workFlags & 0x10) || (!isOK)) {\r
1132 // Thats it...\r
1133 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
1134 LEDsoff();\r
1135 }\r
1136}\r
1137\r
Impressum, Datenschutz