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