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