]> git.zerfleddert.de Git - proxmark3-svn/blame - armsrc/mifarecmd.c
Minor typo
[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
41 iso14443a_setup();\r
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
981bd429 94
95}
96
97void MifareUReadBlock(uint8_t arg0,uint8_t *datain)
98{
99 // params
100 uint8_t blockNo = arg0;
101
102 // variables
103 byte_t isOK = 0;
104 byte_t dataoutbuf[16];
105 uint8_t uid[10];
106 uint32_t cuid;
107
108 // clear trace
109 iso14a_clear_trace();
110 iso14443a_setup();
111
112 LED_A_ON();
113 LED_B_OFF();
114 LED_C_OFF();
115
116 while (true) {
117 if(!iso14443a_select_card(uid, NULL, &cuid)) {
118 if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");
119 break;
120 };
121
122 if(mifare_ultra_readblock(cuid, blockNo, dataoutbuf)) {
123 if (MF_DBGLEVEL >= 1) Dbprintf("Read block error");
124 break;
125 };
126
127 if(mifare_ultra_halt(cuid)) {
128 if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");
129 break;
130 };
131
132 isOK = 1;
133 break;
134 }
135
136 if (MF_DBGLEVEL >= 2) DbpString("READ BLOCK FINISHED");
137
138 // add trace trailer
139 memset(uid, 0x44, 4);
140 LogTrace(uid, 4, 0, 0, TRUE);
141 LED_B_ON();
142 cmd_send(CMD_ACK,isOK,0,0,dataoutbuf,16);
143 LED_B_OFF();
144
145
146 // Thats it...
147 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
148 LEDsoff();
149}
150
151//-----------------------------------------------------------------------------
152// Select, Authenticaate, Read an MIFARE tag.
153// read sector (data = 4 x 16 bytes = 64 bytes)
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
176 iso14443a_setup();\r
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
981bd429 245
246}
247
248void MifareUReadCard(uint8_t arg0, uint8_t *datain)
249{
250 // params
251 uint8_t sectorNo = arg0;
252
253 // variables
254 byte_t isOK = 0;
255 byte_t dataoutbuf[16 * 4];
256 uint8_t uid[10];
257 uint32_t cuid;
258
259 // clear trace
260 iso14a_clear_trace();
261// iso14a_set_tracing(false);
262
263 iso14443a_setup();
264
265 LED_A_ON();
266 LED_B_OFF();
267 LED_C_OFF();
268
269 while (true) {
270 if(!iso14443a_select_card(uid, NULL, &cuid)) {
271 if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");
272 break;
273 };
274 for(int sec=0;sec<16;sec++){
275 if(mifare_ultra_readblock(cuid, sectorNo * 4 + sec, dataoutbuf + 4 * sec)) {
276 if (MF_DBGLEVEL >= 1) Dbprintf("Read block %d error",sec);
277 break;
278 };
279 }
280 if(mifare_ultra_halt(cuid)) {
281 if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");
282 break;
283 };
284
285 isOK = 1;
286 break;
287 }
288
289 if (MF_DBGLEVEL >= 2) DbpString("READ CARD FINISHED");
290
291 // add trace trailer
292 memset(uid, 0x44, 4);
293 LogTrace(uid, 4, 0, 0, TRUE);
294
295 LED_B_ON();
296 cmd_send(CMD_ACK,isOK,0,0,dataoutbuf,64);
297 //cmd_send(CMD_ACK,isOK,0,0,dataoutbuf+32, 32);
298 LED_B_OFF();
299
300 // Thats it...
301 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
302 LEDsoff();
303// iso14a_set_tracing(TRUE);
304
305}
306
307
308//-----------------------------------------------------------------------------
309// Select, Authenticaate, Read an MIFARE tag.
310// read block
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
335 iso14443a_setup();\r
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
981bd429 387
388}
389
390void MifareUWriteBlock(uint8_t arg0, uint8_t *datain)
391{
392 // params
393 uint8_t blockNo = arg0;
394 byte_t blockdata[16];
395
396 memset(blockdata,'\0',16);
397 memcpy(blockdata, datain,16);
398
399 // variables
400 byte_t isOK = 0;
401 uint8_t uid[10];
402 uint32_t cuid;
403
404 // clear trace
405 iso14a_clear_trace();
406 // iso14a_set_tracing(false);
407
408 iso14443a_setup();
409
410 LED_A_ON();
411 LED_B_OFF();
412 LED_C_OFF();
413
414 while (true) {
415 if(!iso14443a_select_card(uid, NULL, &cuid)) {
416 if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");
417 break;
418 };
419
420 if(mifare_ultra_writeblock(cuid, blockNo, blockdata)) {
421 if (MF_DBGLEVEL >= 1) Dbprintf("Write block error");
422 break;
423 };
424
425 if(mifare_ultra_halt(cuid)) {
426 if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");
427 break;
428 };
429
430 isOK = 1;
431 break;
432 }
433
434 if (MF_DBGLEVEL >= 2) DbpString("WRITE BLOCK FINISHED");
435
436 // add trace trailer
437 memset(uid, 0x44, 4);
438 LogTrace(uid, 4, 0, 0, TRUE);
439
440 LED_B_ON();
441 cmd_send(CMD_ACK,isOK,0,0,0,0);
442// UsbSendPacket((uint8_t *)&ack, sizeof(UsbCommand));
443 LED_B_OFF();
444
445
446 // Thats it...
447 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
448 LEDsoff();
449// iso14a_set_tracing(TRUE);
450
451}
452
453void MifareUWriteBlock_Special(uint8_t arg0, uint8_t *datain)
454{
455 // params
456 uint8_t blockNo = arg0;
457 byte_t blockdata[4];
458
459 memcpy(blockdata, datain,4);
460
461 // variables
462 byte_t isOK = 0;
463 uint8_t uid[10];
464 uint32_t cuid;
465
466 // clear trace
467 iso14a_clear_trace();
468 // iso14a_set_tracing(false);
469
470 iso14443a_setup();
471
472 LED_A_ON();
473 LED_B_OFF();
474 LED_C_OFF();
475
476 while (true) {
477 if(!iso14443a_select_card(uid, NULL, &cuid)) {
478 if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");
479 break;
480 };
481
482 if(mifare_ultra_special_writeblock(cuid, blockNo, blockdata)) {
483 if (MF_DBGLEVEL >= 1) Dbprintf("Write block error");
484 break;
485 };
486
487 if(mifare_ultra_halt(cuid)) {
488 if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");
489 break;
490 };
491
492 isOK = 1;
493 break;
494 }
495
496 if (MF_DBGLEVEL >= 2) DbpString("WRITE BLOCK FINISHED");
497
498 // add trace trailer
499 memset(uid, 0x44, 4);
500 LogTrace(uid, 4, 0, 0, TRUE);
501
502 LED_B_ON();
503 cmd_send(CMD_ACK,isOK,0,0,0,0);
504// UsbSendPacket((uint8_t *)&ack, sizeof(UsbCommand));
505 LED_B_OFF();
506
507
508 // Thats it...
509 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
510 LEDsoff();
511// iso14a_set_tracing(TRUE);
512
513}
514
515// Return 1 if the nonce is invalid else return 0
516int valid_nonce(uint32_t Nt, uint32_t NtEnc, uint32_t Ks1, byte_t * parity) {
517 return ((oddparity((Nt >> 24) & 0xFF) == ((parity[0]) ^ oddparity((NtEnc >> 24) & 0xFF) ^ BIT(Ks1,16))) & \
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
557 StartCountMifare();\r
558\r
8556b852 559 // clear trace\r
d19929cb 560 iso14a_clear_trace();\r
9492e0b0 561 iso14a_set_tracing(false);\r
8556b852
M
562 \r
563 iso14443a_setup();\r
564\r
565 LED_A_ON();\r
8556b852
M
566 LED_C_OFF();\r
567\r
8556b852 568\r
9492e0b0 569 while((GetCountMifare() & 0xffff0000) != 0x00010000); // wait for counter to reset and "warm up" \r
8556b852 570\r
9492e0b0 571 // statistics on nonce distance\r
572 if (calibrate) { // for first call only. Otherwise reuse previous calibration\r
573 LED_B_ON();\r
8556b852 574\r
9492e0b0 575 davg = dmax = 0;\r
576 dmin = 2000;\r
577 delta_time = 0;\r
8556b852 578 \r
9492e0b0 579 for (rtr = 0; rtr < 17; rtr++) {\r
8556b852 580\r
9492e0b0 581 // prepare next select. No need to power down the card.\r
582 if(mifare_classic_halt(pcs, cuid)) {\r
583 if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Halt error");\r
584 rtr--;\r
585 continue;\r
586 }\r
587\r
588 if(!iso14443a_select_card(uid, NULL, &cuid)) {\r
589 if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Can't select card");\r
590 rtr--;\r
591 continue;\r
592 };\r
593\r
594 auth1_time = 0;\r
595 if(mifare_classic_authex(pcs, cuid, blockNo, keyType, ui64Key, AUTH_FIRST, &nt1, &auth1_time)) {\r
596 if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Auth1 error");\r
597 rtr--;\r
598 continue;\r
599 };\r
600\r
601 if (delta_time) {\r
602 auth2_time = auth1_time + delta_time;\r
603 } else {\r
604 auth2_time = 0;\r
605 }\r
606 if(mifare_classic_authex(pcs, cuid, blockNo, keyType, ui64Key, AUTH_NESTED, &nt2, &auth2_time)) {\r
607 if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Auth2 error");\r
608 rtr--;\r
609 continue;\r
610 };\r
611\r
b19bd5d6 612 nttmp = prng_successor(nt1, 100); //NXP Mifare is typical around 840,but for some unlicensed/compatible mifare card this can be 160\r
613 for (i = 101; i < 1200; i++) {\r
9492e0b0 614 nttmp = prng_successor(nttmp, 1);\r
615 if (nttmp == nt2) break;\r
616 }\r
617\r
618 if (i != 1200) {\r
619 if (rtr != 0) {\r
620 davg += i;\r
621 dmin = MIN(dmin, i);\r
622 dmax = MAX(dmax, i);\r
623 }\r
624 else {\r
625 delta_time = auth2_time - auth1_time + 32; // allow some slack for proper timing\r
626 }\r
627 if (MF_DBGLEVEL >= 3) Dbprintf("Nested: calibrating... ntdist=%d", i);\r
628 }\r
8556b852
M
629 }\r
630 \r
9492e0b0 631 if (rtr <= 1) return;\r
8556b852 632\r
9492e0b0 633 davg = (davg + (rtr - 1)/2) / (rtr - 1);\r
634 \r
635 if (MF_DBGLEVEL >= 3) Dbprintf("min=%d max=%d avg=%d, delta_time=%d", dmin, dmax, davg, delta_time);\r
8556b852 636\r
9492e0b0 637 dmin = davg - 2;\r
638 dmax = davg + 2;\r
639 \r
640 LED_B_OFF();\r
641 \r
642 }\r
8556b852
M
643// ------------------------------------------------------------------------------------------------- \r
644 \r
645 LED_C_ON();\r
646\r
647 // get crypted nonces for target sector\r
9492e0b0 648 for(i=0; i < 2; i++) { // look for exactly two different nonces\r
8556b852 649\r
9492e0b0 650 target_nt[i] = 0;\r
651 while(target_nt[i] == 0) { // continue until we have an unambiguous nonce\r
8556b852 652 \r
9492e0b0 653 // prepare next select. No need to power down the card.\r
654 if(mifare_classic_halt(pcs, cuid)) {\r
655 if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Halt error");\r
656 continue;\r
657 }\r
8556b852 658\r
9492e0b0 659 if(!iso14443a_select_card(uid, NULL, &cuid)) {\r
660 if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Can't select card");\r
661 continue;\r
662 };\r
8556b852 663 \r
9492e0b0 664 auth1_time = 0;\r
665 if(mifare_classic_authex(pcs, cuid, blockNo, keyType, ui64Key, AUTH_FIRST, &nt1, &auth1_time)) {\r
666 if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Auth1 error");\r
667 continue;\r
668 };\r
8556b852 669\r
9492e0b0 670 // nested authentication\r
671 auth2_time = auth1_time + delta_time;\r
672 len = mifare_sendcmd_shortex(pcs, AUTH_NESTED, 0x60 + (targetKeyType & 0x01), targetBlockNo, receivedAnswer, &par, &auth2_time);\r
673 if (len != 4) {\r
674 if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Auth2 error len=%d", len);\r
675 continue;\r
676 };\r
8556b852 677 \r
9492e0b0 678 nt2 = bytes_to_num(receivedAnswer, 4); \r
679 if (MF_DBGLEVEL >= 3) Dbprintf("Nonce#%d: Testing nt1=%08x nt2enc=%08x nt2par=%02x", i+1, nt1, nt2, par);\r
8556b852 680 \r
9492e0b0 681 // Parity validity check\r
682 for (j = 0; j < 4; j++) {\r
683 par_array[j] = (oddparity(receivedAnswer[j]) != ((par & 0x08) >> 3));\r
684 par = par << 1;\r
685 }\r
686 \r
687 ncount = 0;\r
688 nttest = prng_successor(nt1, dmin - 1);\r
689 for (j = dmin; j < dmax + 1; j++) {\r
690 nttest = prng_successor(nttest, 1);\r
691 ks1 = nt2 ^ nttest;\r
692\r
693 if (valid_nonce(nttest, nt2, ks1, par_array)){\r
694 if (ncount > 0) { // we are only interested in disambiguous nonces, try again\r
695 if (MF_DBGLEVEL >= 3) Dbprintf("Nonce#%d: dismissed (ambigous), ntdist=%d", i+1, j);\r
696 target_nt[i] = 0;\r
697 break;\r
698 }\r
699 target_nt[i] = nttest;\r
700 target_ks[i] = ks1;\r
701 ncount++;\r
702 if (i == 1 && target_nt[1] == target_nt[0]) { // we need two different nonces\r
703 target_nt[i] = 0;\r
704 if (MF_DBGLEVEL >= 3) Dbprintf("Nonce#2: dismissed (= nonce#1), ntdist=%d", j);\r
8556b852
M
705 break;\r
706 }\r
9492e0b0 707 if (MF_DBGLEVEL >= 3) Dbprintf("Nonce#%d: valid, ntdist=%d", i+1, j);\r
8556b852 708 }\r
8556b852 709 }\r
9492e0b0 710 if (target_nt[i] == 0 && j == dmax+1 && MF_DBGLEVEL >= 3) Dbprintf("Nonce#%d: dismissed (all invalid)", i+1);\r
8556b852
M
711 }\r
712 }\r
713\r
714 LED_C_OFF();\r
715 \r
716 // ----------------------------- crypto1 destroy\r
717 crypto1_destroy(pcs);\r
718 \r
719 // add trace trailer\r
720 memset(uid, 0x44, 4);\r
721 LogTrace(uid, 4, 0, 0, TRUE);\r
722\r
9492e0b0 723 byte_t buf[4 + 4 * 4];\r
724 memcpy(buf, &cuid, 4);\r
725 memcpy(buf+4, &target_nt[0], 4);\r
726 memcpy(buf+8, &target_ks[0], 4);\r
727 memcpy(buf+12, &target_nt[1], 4);\r
728 memcpy(buf+16, &target_ks[1], 4);\r
8556b852
M
729 \r
730 LED_B_ON();\r
9492e0b0 731 cmd_send(CMD_ACK, 0, 2, targetBlockNo + (targetKeyType * 0x100), buf, sizeof(buf));\r
6e82300d 732 LED_B_OFF();\r
8556b852 733\r
9492e0b0 734 if (MF_DBGLEVEL >= 3) DbpString("NESTED FINISHED");\r
8556b852 735\r
8556b852
M
736 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
737 LEDsoff();\r
9492e0b0 738 iso14a_set_tracing(TRUE);\r
8556b852
M
739}\r
740\r
741//-----------------------------------------------------------------------------\r
9492e0b0 742// MIFARE check keys. key count up to 85. \r
8556b852
M
743// \r
744//-----------------------------------------------------------------------------\r
745void MifareChkKeys(uint8_t arg0, uint8_t arg1, uint8_t arg2, uint8_t *datain)\r
746{\r
747 // params\r
748 uint8_t blockNo = arg0;\r
749 uint8_t keyType = arg1;\r
750 uint8_t keyCount = arg2;\r
751 uint64_t ui64Key = 0;\r
752 \r
753 // variables\r
754 int i;\r
755 byte_t isOK = 0;\r
1c611bbd 756 uint8_t uid[10];\r
8556b852
M
757 uint32_t cuid;\r
758 struct Crypto1State mpcs = {0, 0};\r
759 struct Crypto1State *pcs;\r
760 pcs = &mpcs;\r
761 \r
762 // clear debug level\r
763 int OLD_MF_DBGLEVEL = MF_DBGLEVEL; \r
764 MF_DBGLEVEL = MF_DBG_NONE;\r
765 \r
766 // clear trace\r
d19929cb 767 iso14a_clear_trace();\r
9492e0b0 768 iso14a_set_tracing(TRUE);\r
8556b852
M
769\r
770 iso14443a_setup();\r
771\r
772 LED_A_ON();\r
773 LED_B_OFF();\r
774 LED_C_OFF();\r
775\r
9492e0b0 776// SpinDelay(300);\r
8556b852 777 for (i = 0; i < keyCount; i++) {\r
9492e0b0 778// FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
779// SpinDelay(100);\r
780// FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A | FPGA_HF_ISO14443A_READER_MOD);\r
781 // prepare next select by sending a HALT. There is no need to power down the card.\r
782 if(mifare_classic_halt(pcs, cuid)) {\r
783 if (MF_DBGLEVEL >= 1) Dbprintf("ChkKeys: Halt error");\r
784 }\r
8556b852 785\r
9492e0b0 786 // SpinDelay(50);\r
787 \r
8556b852 788 if(!iso14443a_select_card(uid, NULL, &cuid)) {\r
9492e0b0 789 if (OLD_MF_DBGLEVEL >= 1) Dbprintf("ChkKeys: Can't select card");\r
8556b852
M
790 break;\r
791 };\r
792\r
793 ui64Key = bytes_to_num(datain + i * 6, 6);\r
794 if(mifare_classic_auth(pcs, cuid, blockNo, keyType, ui64Key, AUTH_FIRST)) {\r
795 continue;\r
796 };\r
797 \r
798 isOK = 1;\r
799 break;\r
800 }\r
801 \r
802 // ----------------------------- crypto1 destroy\r
803 crypto1_destroy(pcs);\r
804 \r
805 // add trace trailer\r
806 memset(uid, 0x44, 4);\r
807 LogTrace(uid, 4, 0, 0, TRUE);\r
808\r
8556b852 809 LED_B_ON();\r
6e82300d 810 cmd_send(CMD_ACK,isOK,0,0,datain + i * 6,6);\r
8556b852
M
811 LED_B_OFF();\r
812\r
813 // Thats it...\r
814 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
815 LEDsoff();\r
816\r
817 // restore debug level\r
818 MF_DBGLEVEL = OLD_MF_DBGLEVEL; \r
819}\r
820\r
821//-----------------------------------------------------------------------------\r
822// MIFARE commands set debug level\r
823// \r
824//-----------------------------------------------------------------------------\r
825void MifareSetDbgLvl(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain){\r
826 MF_DBGLEVEL = arg0;\r
827 Dbprintf("Debug level: %d", MF_DBGLEVEL);\r
828}\r
829\r
830//-----------------------------------------------------------------------------\r
831// Work with emulator memory\r
832// \r
833//-----------------------------------------------------------------------------\r
834void MifareEMemClr(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain){\r
835 emlClearMem();\r
836}\r
837\r
838void MifareEMemSet(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain){\r
839 emlSetMem(datain, arg0, arg1); // data, block num, blocks count\r
840}\r
841\r
842void MifareEMemGet(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain){\r
6e82300d 843// UsbCommand ack = {CMD_ACK, {arg0, arg1, 0}};\r
8556b852 844\r
6e82300d 845 byte_t buf[48];\r
846 emlGetMem(buf, arg0, arg1); // data, block num, blocks count\r
8556b852
M
847\r
848 LED_B_ON();\r
6e82300d 849 cmd_send(CMD_ACK,arg0,arg1,0,buf,48);\r
850// UsbSendPacket((uint8_t *)&ack, sizeof(UsbCommand));\r
8556b852
M
851 LED_B_OFF();\r
852}\r
853\r
854//-----------------------------------------------------------------------------\r
855// Load a card into the emulator memory\r
856// \r
857//-----------------------------------------------------------------------------\r
858void MifareECardLoad(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain){\r
859 int i;\r
860 uint8_t sectorNo = 0;\r
861 uint8_t keyType = arg1;\r
862 uint64_t ui64Key = 0;\r
863 uint32_t cuid;\r
864 struct Crypto1State mpcs = {0, 0};\r
865 struct Crypto1State *pcs;\r
866 pcs = &mpcs;\r
867\r
868 // variables\r
869 byte_t dataoutbuf[16];\r
ab8b654e 870 byte_t dataoutbuf2[16];\r
1c611bbd 871 uint8_t uid[10];\r
8556b852
M
872\r
873 // clear trace\r
d19929cb 874 iso14a_clear_trace();\r
8556b852
M
875 iso14a_set_tracing(false);\r
876 \r
877 iso14443a_setup();\r
878\r
879 LED_A_ON();\r
880 LED_B_OFF();\r
881 LED_C_OFF();\r
882 \r
883 while (true) {\r
884 if(!iso14443a_select_card(uid, NULL, &cuid)) {\r
885 if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");\r
886 break;\r
887 };\r
888 \r
889 for (i = 0; i < 16; i++) {\r
890 sectorNo = i;\r
891 ui64Key = emlGetKey(sectorNo, keyType);\r
892 \r
893 if (!i){\r
894 if(mifare_classic_auth(pcs, cuid, sectorNo * 4, keyType, ui64Key, AUTH_FIRST)) {\r
895 if (MF_DBGLEVEL >= 1) Dbprintf("Sector[%d]. Auth error", i);\r
896 break;\r
897 }\r
898 } else {\r
899 if(mifare_classic_auth(pcs, cuid, sectorNo * 4, keyType, ui64Key, AUTH_NESTED)) {\r
900 if (MF_DBGLEVEL >= 1) Dbprintf("Sector[%d]. Auth nested error", i);\r
901 break;\r
902 }\r
903 }\r
904 \r
905 if(mifare_classic_readblock(pcs, cuid, sectorNo * 4 + 0, dataoutbuf)) {\r
906 if (MF_DBGLEVEL >= 1) Dbprintf("Read block 0 error");\r
907 break;\r
908 };\r
909 emlSetMem(dataoutbuf, sectorNo * 4 + 0, 1);\r
910 \r
911 if(mifare_classic_readblock(pcs, cuid, sectorNo * 4 + 1, dataoutbuf)) {\r
912 if (MF_DBGLEVEL >= 1) Dbprintf("Read block 1 error");\r
913 break;\r
914 };\r
915 emlSetMem(dataoutbuf, sectorNo * 4 + 1, 1);\r
916\r
917 if(mifare_classic_readblock(pcs, cuid, sectorNo * 4 + 2, dataoutbuf)) {\r
918 if (MF_DBGLEVEL >= 1) Dbprintf("Read block 2 error");\r
919 break;\r
920 };\r
921 emlSetMem(dataoutbuf, sectorNo * 4 + 2, 1);\r
ab8b654e
M
922\r
923 // get block 3 bytes 6-9\r
924 if(mifare_classic_readblock(pcs, cuid, sectorNo * 4 + 3, dataoutbuf)) {\r
925 if (MF_DBGLEVEL >= 1) Dbprintf("Read block 3 error");\r
926 break;\r
927 };\r
928 emlGetMem(dataoutbuf2, sectorNo * 4 + 3, 1);\r
929 memcpy(&dataoutbuf2[6], &dataoutbuf[6], 4);\r
930 emlSetMem(dataoutbuf2, sectorNo * 4 + 3, 1);\r
8556b852
M
931 }\r
932\r
933 if(mifare_classic_halt(pcs, cuid)) {\r
934 if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");\r
935 break;\r
936 };\r
937 \r
938 break;\r
939 } \r
940\r
941 // ----------------------------- crypto1 destroy\r
942 crypto1_destroy(pcs);\r
ab8b654e
M
943\r
944 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
945 LEDsoff();\r
8556b852
M
946 \r
947 if (MF_DBGLEVEL >= 2) DbpString("EMUL FILL SECTORS FINISHED");\r
948\r
949 // add trace trailer\r
950 memset(uid, 0x44, 4);\r
951 LogTrace(uid, 4, 0, 0, TRUE);\r
8556b852
M
952}\r
953\r
954//-----------------------------------------------------------------------------\r
955// MIFARE 1k emulator\r
956// \r
957//-----------------------------------------------------------------------------\r
958\r
0675f200
M
959\r
960//-----------------------------------------------------------------------------\r
961// Work with "magic Chinese" card (email him: ouyangweidaxian@live.cn)\r
962// \r
963//-----------------------------------------------------------------------------\r
964void MifareCSetBlock(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain){\r
965 \r
966 // params\r
967 uint8_t needWipe = arg0;\r
208a0166
M
968 // bit 0 - need get UID\r
969 // bit 1 - need wupC\r
970 // bit 2 - need HALT after sequence\r
971 // bit 3 - need init FPGA and field before sequence\r
972 // bit 4 - need reset FPGA and LED\r
973 uint8_t workFlags = arg1;\r
0675f200
M
974 uint8_t blockNo = arg2;\r
975 \r
976 // card commands\r
977 uint8_t wupC1[] = { 0x40 }; \r
978 uint8_t wupC2[] = { 0x43 }; \r
979 uint8_t wipeC[] = { 0x41 }; \r
980 \r
981 // variables\r
982 byte_t isOK = 0;\r
1c611bbd 983 uint8_t uid[10];\r
0675f200
M
984 uint8_t d_block[18];\r
985 uint32_t cuid;\r
986 \r
1c611bbd 987 memset(uid, 0x00, 10);\r
0675f200
M
988 uint8_t* receivedAnswer = mifare_get_bigbufptr();\r
989 \r
208a0166
M
990 if (workFlags & 0x08) {\r
991 // clear trace\r
d19929cb 992 iso14a_clear_trace();\r
208a0166 993 iso14a_set_tracing(TRUE);\r
0675f200 994\r
208a0166 995 iso14443a_setup();\r
0675f200 996\r
208a0166
M
997 LED_A_ON();\r
998 LED_B_OFF();\r
999 LED_C_OFF();\r
0675f200 1000 \r
208a0166
M
1001 SpinDelay(300);\r
1002 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
1003 SpinDelay(100);\r
1004 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A | FPGA_HF_ISO14443A_READER_MOD);\r
1005 }\r
0675f200
M
1006\r
1007 while (true) {\r
1008 // get UID from chip\r
208a0166 1009 if (workFlags & 0x01) {\r
0675f200
M
1010 if(!iso14443a_select_card(uid, NULL, &cuid)) {\r
1011 if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");\r
1012 break;\r
1013 };\r
1014\r
1015 if(mifare_classic_halt(NULL, cuid)) {\r
1016 if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");\r
1017 break;\r
1018 };\r
1019 };\r
1020 \r
1021 // reset chip\r
1022 if (needWipe){\r
9492e0b0 1023 ReaderTransmitBitsPar(wupC1,7,0, NULL);\r
0675f200
M
1024 if(!ReaderReceive(receivedAnswer) || (receivedAnswer[0] != 0x0a)) {\r
1025 if (MF_DBGLEVEL >= 1) Dbprintf("wupC1 error");\r
1026 break;\r
1027 };\r
1028\r
9492e0b0 1029 ReaderTransmit(wipeC, sizeof(wipeC), NULL);\r
0675f200
M
1030 if(!ReaderReceive(receivedAnswer) || (receivedAnswer[0] != 0x0a)) {\r
1031 if (MF_DBGLEVEL >= 1) Dbprintf("wipeC error");\r
1032 break;\r
1033 };\r
1034\r
1035 if(mifare_classic_halt(NULL, cuid)) {\r
1036 if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");\r
1037 break;\r
1038 };\r
1039 }; \r
1040\r
545a1f38 1041 // write block\r
208a0166 1042 if (workFlags & 0x02) {\r
9492e0b0 1043 ReaderTransmitBitsPar(wupC1,7,0, NULL);\r
208a0166
M
1044 if(!ReaderReceive(receivedAnswer) || (receivedAnswer[0] != 0x0a)) {\r
1045 if (MF_DBGLEVEL >= 1) Dbprintf("wupC1 error");\r
1046 break;\r
1047 };\r
0675f200 1048\r
9492e0b0 1049 ReaderTransmit(wupC2, sizeof(wupC2), NULL);\r
208a0166
M
1050 if(!ReaderReceive(receivedAnswer) || (receivedAnswer[0] != 0x0a)) {\r
1051 if (MF_DBGLEVEL >= 1) Dbprintf("wupC2 error");\r
1052 break;\r
1053 };\r
1054 }\r
0675f200 1055\r
9492e0b0 1056 if ((mifare_sendcmd_short(NULL, 0, 0xA0, blockNo, receivedAnswer, NULL) != 1) || (receivedAnswer[0] != 0x0a)) {\r
0675f200
M
1057 if (MF_DBGLEVEL >= 1) Dbprintf("write block send command error");\r
1058 break;\r
1059 };\r
1060 \r
1061 memcpy(d_block, datain, 16);\r
1062 AppendCrc14443a(d_block, 16);\r
1063 \r
9492e0b0 1064 ReaderTransmit(d_block, sizeof(d_block), NULL);\r
0675f200
M
1065 if ((ReaderReceive(receivedAnswer) != 1) || (receivedAnswer[0] != 0x0a)) {\r
1066 if (MF_DBGLEVEL >= 1) Dbprintf("write block send data error");\r
1067 break;\r
1068 }; \r
1069 \r
208a0166
M
1070 if (workFlags & 0x04) {\r
1071 if (mifare_classic_halt(NULL, cuid)) {\r
1072 if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");\r
1073 break;\r
1074 };\r
1075 }\r
0675f200
M
1076 \r
1077 isOK = 1;\r
1078 break;\r
1079 }\r
1080 \r
6e82300d 1081// UsbCommand ack = {CMD_ACK, {isOK, 0, 0}};\r
1082// if (isOK) memcpy(ack.d.asBytes, uid, 4);\r
0675f200
M
1083 \r
1084 // add trace trailer\r
9836a77f 1085 /**\r
1086 * Removed by Martin, the uid is overwritten with 0x44, \r
1087 * which can 't be intended. \r
1088 *\r
1089 * memset(uid, 0x44, 4);\r
1090 * LogTrace(uid, 4, 0, 0, TRUE);\r
1091 **/\r
1092 \r
0675f200
M
1093\r
1094 LED_B_ON();\r
6e82300d 1095 cmd_send(CMD_ACK,isOK,0,0,uid,4);\r
1096// UsbSendPacket((uint8_t *)&ack, sizeof(UsbCommand));\r
0675f200
M
1097 LED_B_OFF();\r
1098\r
545a1f38 1099 if ((workFlags & 0x10) || (!isOK)) {\r
208a0166
M
1100 // Thats it...\r
1101 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
1102 LEDsoff();\r
1103 }\r
0675f200 1104}\r
545a1f38
M
1105\r
1106void MifareCGetBlock(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain){\r
1107 \r
1108 // params\r
1109 // bit 1 - need wupC\r
1110 // bit 2 - need HALT after sequence\r
1111 // bit 3 - need init FPGA and field before sequence\r
1112 // bit 4 - need reset FPGA and LED\r
1113 uint8_t workFlags = arg0;\r
1114 uint8_t blockNo = arg2;\r
1115 \r
1116 // card commands\r
1117 uint8_t wupC1[] = { 0x40 }; \r
1118 uint8_t wupC2[] = { 0x43 }; \r
1119 \r
1120 // variables\r
1121 byte_t isOK = 0;\r
1122 uint8_t data[18];\r
1123 uint32_t cuid = 0;\r
1124 \r
1125 memset(data, 0x00, 18);\r
1126 uint8_t* receivedAnswer = mifare_get_bigbufptr();\r
1127 \r
1128 if (workFlags & 0x08) {\r
1129 // clear trace\r
d19929cb 1130 iso14a_clear_trace();\r
545a1f38
M
1131 iso14a_set_tracing(TRUE);\r
1132\r
1133 iso14443a_setup();\r
1134\r
1135 LED_A_ON();\r
1136 LED_B_OFF();\r
1137 LED_C_OFF();\r
1138 \r
1139 SpinDelay(300);\r
1140 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
1141 SpinDelay(100);\r
1142 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A | FPGA_HF_ISO14443A_READER_MOD);\r
1143 }\r
1144\r
1145 while (true) {\r
1146 if (workFlags & 0x02) {\r
9492e0b0 1147 ReaderTransmitBitsPar(wupC1,7,0, NULL);\r
545a1f38
M
1148 if(!ReaderReceive(receivedAnswer) || (receivedAnswer[0] != 0x0a)) {\r
1149 if (MF_DBGLEVEL >= 1) Dbprintf("wupC1 error");\r
1150 break;\r
1151 };\r
1152\r
9492e0b0 1153 ReaderTransmit(wupC2, sizeof(wupC2), NULL);\r
545a1f38
M
1154 if(!ReaderReceive(receivedAnswer) || (receivedAnswer[0] != 0x0a)) {\r
1155 if (MF_DBGLEVEL >= 1) Dbprintf("wupC2 error");\r
1156 break;\r
1157 };\r
1158 }\r
1159\r
1160 // read block\r
9492e0b0 1161 if ((mifare_sendcmd_short(NULL, 0, 0x30, blockNo, receivedAnswer, NULL) != 18)) {\r
545a1f38
M
1162 if (MF_DBGLEVEL >= 1) Dbprintf("read block send command error");\r
1163 break;\r
1164 };\r
1165 memcpy(data, receivedAnswer, 18);\r
1166 \r
1167 if (workFlags & 0x04) {\r
1168 if (mifare_classic_halt(NULL, cuid)) {\r
1169 if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");\r
1170 break;\r
1171 };\r
1172 }\r
1173 \r
1174 isOK = 1;\r
1175 break;\r
1176 }\r
1177 \r
6e82300d 1178// UsbCommand ack = {CMD_ACK, {isOK, 0, 0}};\r
1179// if (isOK) memcpy(ack.d.asBytes, data, 18);\r
545a1f38
M
1180 \r
1181 // add trace trailer\r
354d3a33 1182 /*\r
1183 * Removed by Martin, this piece of overwrites the 'data' variable \r
1184 * which is sent two lines down, and is obviously not correct. \r
1185 * \r
1186 * memset(data, 0x44, 4);\r
1187 * LogTrace(data, 4, 0, 0, TRUE);\r
1188 */\r
545a1f38 1189 LED_B_ON();\r
6e82300d 1190 cmd_send(CMD_ACK,isOK,0,0,data,18);\r
1191// UsbSendPacket((uint8_t *)&ack, sizeof(UsbCommand));\r
545a1f38
M
1192 LED_B_OFF();\r
1193\r
1194 if ((workFlags & 0x10) || (!isOK)) {\r
1195 // Thats it...\r
1196 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
1197 LEDsoff();\r
1198 }\r
1199}\r
1200\r
Impressum, Datenschutz