]> git.zerfleddert.de Git - proxmark3-svn/blob - armsrc/mifarecmd.c
integrated MIFARE ultralight features, contributed by 'midnitesnake'
[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();
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();
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();
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();
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();
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();
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();
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 StartCountMifare();
558
559 // clear trace
560 iso14a_clear_trace();
561 iso14a_set_tracing(false);
562
563 iso14443a_setup();
564
565 LED_A_ON();
566 LED_C_OFF();
567
568
569 while((GetCountMifare() & 0xffff0000) != 0x00010000); // wait for counter to reset and "warm up"
570
571 // statistics on nonce distance
572 if (calibrate) { // for first call only. Otherwise reuse previous calibration
573 LED_B_ON();
574
575 davg = dmax = 0;
576 dmin = 2000;
577 delta_time = 0;
578
579 for (rtr = 0; rtr < 17; rtr++) {
580
581 // prepare next select. No need to power down the card.
582 if(mifare_classic_halt(pcs, cuid)) {
583 if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Halt error");
584 rtr--;
585 continue;
586 }
587
588 if(!iso14443a_select_card(uid, NULL, &cuid)) {
589 if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Can't select card");
590 rtr--;
591 continue;
592 };
593
594 auth1_time = 0;
595 if(mifare_classic_authex(pcs, cuid, blockNo, keyType, ui64Key, AUTH_FIRST, &nt1, &auth1_time)) {
596 if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Auth1 error");
597 rtr--;
598 continue;
599 };
600
601 if (delta_time) {
602 auth2_time = auth1_time + delta_time;
603 } else {
604 auth2_time = 0;
605 }
606 if(mifare_classic_authex(pcs, cuid, blockNo, keyType, ui64Key, AUTH_NESTED, &nt2, &auth2_time)) {
607 if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Auth2 error");
608 rtr--;
609 continue;
610 };
611
612 nttmp = prng_successor(nt1, 100); //NXP Mifare is typical around 840,but for some unlicensed/compatible mifare card this can be 160
613 for (i = 101; i < 1200; i++) {
614 nttmp = prng_successor(nttmp, 1);
615 if (nttmp == nt2) break;
616 }
617
618 if (i != 1200) {
619 if (rtr != 0) {
620 davg += i;
621 dmin = MIN(dmin, i);
622 dmax = MAX(dmax, i);
623 }
624 else {
625 delta_time = auth2_time - auth1_time + 32; // allow some slack for proper timing
626 }
627 if (MF_DBGLEVEL >= 3) Dbprintf("Nested: calibrating... ntdist=%d", i);
628 }
629 }
630
631 if (rtr <= 1) return;
632
633 davg = (davg + (rtr - 1)/2) / (rtr - 1);
634
635 if (MF_DBGLEVEL >= 3) Dbprintf("min=%d max=%d avg=%d, delta_time=%d", dmin, dmax, davg, delta_time);
636
637 dmin = davg - 2;
638 dmax = davg + 2;
639
640 LED_B_OFF();
641
642 }
643 // -------------------------------------------------------------------------------------------------
644
645 LED_C_ON();
646
647 // get crypted nonces for target sector
648 for(i=0; i < 2; i++) { // look for exactly two different nonces
649
650 target_nt[i] = 0;
651 while(target_nt[i] == 0) { // continue until we have an unambiguous nonce
652
653 // prepare next select. No need to power down the card.
654 if(mifare_classic_halt(pcs, cuid)) {
655 if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Halt error");
656 continue;
657 }
658
659 if(!iso14443a_select_card(uid, NULL, &cuid)) {
660 if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Can't select card");
661 continue;
662 };
663
664 auth1_time = 0;
665 if(mifare_classic_authex(pcs, cuid, blockNo, keyType, ui64Key, AUTH_FIRST, &nt1, &auth1_time)) {
666 if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Auth1 error");
667 continue;
668 };
669
670 // nested authentication
671 auth2_time = auth1_time + delta_time;
672 len = mifare_sendcmd_shortex(pcs, AUTH_NESTED, 0x60 + (targetKeyType & 0x01), targetBlockNo, receivedAnswer, &par, &auth2_time);
673 if (len != 4) {
674 if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Auth2 error len=%d", len);
675 continue;
676 };
677
678 nt2 = bytes_to_num(receivedAnswer, 4);
679 if (MF_DBGLEVEL >= 3) Dbprintf("Nonce#%d: Testing nt1=%08x nt2enc=%08x nt2par=%02x", i+1, nt1, nt2, par);
680
681 // Parity validity check
682 for (j = 0; j < 4; j++) {
683 par_array[j] = (oddparity(receivedAnswer[j]) != ((par & 0x08) >> 3));
684 par = par << 1;
685 }
686
687 ncount = 0;
688 nttest = prng_successor(nt1, dmin - 1);
689 for (j = dmin; j < dmax + 1; j++) {
690 nttest = prng_successor(nttest, 1);
691 ks1 = nt2 ^ nttest;
692
693 if (valid_nonce(nttest, nt2, ks1, par_array)){
694 if (ncount > 0) { // we are only interested in disambiguous nonces, try again
695 if (MF_DBGLEVEL >= 3) Dbprintf("Nonce#%d: dismissed (ambigous), ntdist=%d", i+1, j);
696 target_nt[i] = 0;
697 break;
698 }
699 target_nt[i] = nttest;
700 target_ks[i] = ks1;
701 ncount++;
702 if (i == 1 && target_nt[1] == target_nt[0]) { // we need two different nonces
703 target_nt[i] = 0;
704 if (MF_DBGLEVEL >= 3) Dbprintf("Nonce#2: dismissed (= nonce#1), ntdist=%d", j);
705 break;
706 }
707 if (MF_DBGLEVEL >= 3) Dbprintf("Nonce#%d: valid, ntdist=%d", i+1, j);
708 }
709 }
710 if (target_nt[i] == 0 && j == dmax+1 && MF_DBGLEVEL >= 3) Dbprintf("Nonce#%d: dismissed (all invalid)", i+1);
711 }
712 }
713
714 LED_C_OFF();
715
716 // ----------------------------- crypto1 destroy
717 crypto1_destroy(pcs);
718
719 // add trace trailer
720 memset(uid, 0x44, 4);
721 LogTrace(uid, 4, 0, 0, TRUE);
722
723 byte_t buf[4 + 4 * 4];
724 memcpy(buf, &cuid, 4);
725 memcpy(buf+4, &target_nt[0], 4);
726 memcpy(buf+8, &target_ks[0], 4);
727 memcpy(buf+12, &target_nt[1], 4);
728 memcpy(buf+16, &target_ks[1], 4);
729
730 LED_B_ON();
731 cmd_send(CMD_ACK, 0, 2, targetBlockNo + (targetKeyType * 0x100), buf, sizeof(buf));
732 LED_B_OFF();
733
734 if (MF_DBGLEVEL >= 3) DbpString("NESTED FINISHED");
735
736 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
737 LEDsoff();
738 iso14a_set_tracing(TRUE);
739 }
740
741 //-----------------------------------------------------------------------------
742 // MIFARE check keys. key count up to 85.
743 //
744 //-----------------------------------------------------------------------------
745 void MifareChkKeys(uint8_t arg0, uint8_t arg1, uint8_t arg2, uint8_t *datain)
746 {
747 // params
748 uint8_t blockNo = arg0;
749 uint8_t keyType = arg1;
750 uint8_t keyCount = arg2;
751 uint64_t ui64Key = 0;
752
753 // variables
754 int i;
755 byte_t isOK = 0;
756 uint8_t uid[10];
757 uint32_t cuid;
758 struct Crypto1State mpcs = {0, 0};
759 struct Crypto1State *pcs;
760 pcs = &mpcs;
761
762 // clear debug level
763 int OLD_MF_DBGLEVEL = MF_DBGLEVEL;
764 MF_DBGLEVEL = MF_DBG_NONE;
765
766 // clear trace
767 iso14a_clear_trace();
768 iso14a_set_tracing(TRUE);
769
770 iso14443a_setup();
771
772 LED_A_ON();
773 LED_B_OFF();
774 LED_C_OFF();
775
776 // SpinDelay(300);
777 for (i = 0; i < keyCount; i++) {
778 // FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
779 // SpinDelay(100);
780 // FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A | FPGA_HF_ISO14443A_READER_MOD);
781 // prepare next select by sending a HALT. There is no need to power down the card.
782 if(mifare_classic_halt(pcs, cuid)) {
783 if (MF_DBGLEVEL >= 1) Dbprintf("ChkKeys: Halt error");
784 }
785
786 // SpinDelay(50);
787
788 if(!iso14443a_select_card(uid, NULL, &cuid)) {
789 if (OLD_MF_DBGLEVEL >= 1) Dbprintf("ChkKeys: Can't select card");
790 break;
791 };
792
793 ui64Key = bytes_to_num(datain + i * 6, 6);
794 if(mifare_classic_auth(pcs, cuid, blockNo, keyType, ui64Key, AUTH_FIRST)) {
795 continue;
796 };
797
798 isOK = 1;
799 break;
800 }
801
802 // ----------------------------- crypto1 destroy
803 crypto1_destroy(pcs);
804
805 // add trace trailer
806 memset(uid, 0x44, 4);
807 LogTrace(uid, 4, 0, 0, TRUE);
808
809 LED_B_ON();
810 cmd_send(CMD_ACK,isOK,0,0,datain + i * 6,6);
811 LED_B_OFF();
812
813 // Thats it...
814 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
815 LEDsoff();
816
817 // restore debug level
818 MF_DBGLEVEL = OLD_MF_DBGLEVEL;
819 }
820
821 //-----------------------------------------------------------------------------
822 // MIFARE commands set debug level
823 //
824 //-----------------------------------------------------------------------------
825 void MifareSetDbgLvl(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain){
826 MF_DBGLEVEL = arg0;
827 Dbprintf("Debug level: %d", MF_DBGLEVEL);
828 }
829
830 //-----------------------------------------------------------------------------
831 // Work with emulator memory
832 //
833 //-----------------------------------------------------------------------------
834 void MifareEMemClr(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain){
835 emlClearMem();
836 }
837
838 void MifareEMemSet(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain){
839 emlSetMem(datain, arg0, arg1); // data, block num, blocks count
840 }
841
842 void MifareEMemGet(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain){
843 // UsbCommand ack = {CMD_ACK, {arg0, arg1, 0}};
844
845 byte_t buf[48];
846 emlGetMem(buf, arg0, arg1); // data, block num, blocks count
847
848 LED_B_ON();
849 cmd_send(CMD_ACK,arg0,arg1,0,buf,48);
850 // UsbSendPacket((uint8_t *)&ack, sizeof(UsbCommand));
851 LED_B_OFF();
852 }
853
854 //-----------------------------------------------------------------------------
855 // Load a card into the emulator memory
856 //
857 //-----------------------------------------------------------------------------
858 void MifareECardLoad(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain){
859 int i;
860 uint8_t sectorNo = 0;
861 uint8_t keyType = arg1;
862 uint64_t ui64Key = 0;
863 uint32_t cuid;
864 struct Crypto1State mpcs = {0, 0};
865 struct Crypto1State *pcs;
866 pcs = &mpcs;
867
868 // variables
869 byte_t dataoutbuf[16];
870 byte_t dataoutbuf2[16];
871 uint8_t uid[10];
872
873 // clear trace
874 iso14a_clear_trace();
875 iso14a_set_tracing(false);
876
877 iso14443a_setup();
878
879 LED_A_ON();
880 LED_B_OFF();
881 LED_C_OFF();
882
883 while (true) {
884 if(!iso14443a_select_card(uid, NULL, &cuid)) {
885 if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");
886 break;
887 };
888
889 for (i = 0; i < 16; i++) {
890 sectorNo = i;
891 ui64Key = emlGetKey(sectorNo, keyType);
892
893 if (!i){
894 if(mifare_classic_auth(pcs, cuid, sectorNo * 4, keyType, ui64Key, AUTH_FIRST)) {
895 if (MF_DBGLEVEL >= 1) Dbprintf("Sector[%d]. Auth error", i);
896 break;
897 }
898 } else {
899 if(mifare_classic_auth(pcs, cuid, sectorNo * 4, keyType, ui64Key, AUTH_NESTED)) {
900 if (MF_DBGLEVEL >= 1) Dbprintf("Sector[%d]. Auth nested error", i);
901 break;
902 }
903 }
904
905 if(mifare_classic_readblock(pcs, cuid, sectorNo * 4 + 0, dataoutbuf)) {
906 if (MF_DBGLEVEL >= 1) Dbprintf("Read block 0 error");
907 break;
908 };
909 emlSetMem(dataoutbuf, sectorNo * 4 + 0, 1);
910
911 if(mifare_classic_readblock(pcs, cuid, sectorNo * 4 + 1, dataoutbuf)) {
912 if (MF_DBGLEVEL >= 1) Dbprintf("Read block 1 error");
913 break;
914 };
915 emlSetMem(dataoutbuf, sectorNo * 4 + 1, 1);
916
917 if(mifare_classic_readblock(pcs, cuid, sectorNo * 4 + 2, dataoutbuf)) {
918 if (MF_DBGLEVEL >= 1) Dbprintf("Read block 2 error");
919 break;
920 };
921 emlSetMem(dataoutbuf, sectorNo * 4 + 2, 1);
922
923 // get block 3 bytes 6-9
924 if(mifare_classic_readblock(pcs, cuid, sectorNo * 4 + 3, dataoutbuf)) {
925 if (MF_DBGLEVEL >= 1) Dbprintf("Read block 3 error");
926 break;
927 };
928 emlGetMem(dataoutbuf2, sectorNo * 4 + 3, 1);
929 memcpy(&dataoutbuf2[6], &dataoutbuf[6], 4);
930 emlSetMem(dataoutbuf2, sectorNo * 4 + 3, 1);
931 }
932
933 if(mifare_classic_halt(pcs, cuid)) {
934 if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");
935 break;
936 };
937
938 break;
939 }
940
941 // ----------------------------- crypto1 destroy
942 crypto1_destroy(pcs);
943
944 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
945 LEDsoff();
946
947 if (MF_DBGLEVEL >= 2) DbpString("EMUL FILL SECTORS FINISHED");
948
949 // add trace trailer
950 memset(uid, 0x44, 4);
951 LogTrace(uid, 4, 0, 0, TRUE);
952 }
953
954 //-----------------------------------------------------------------------------
955 // MIFARE 1k emulator
956 //
957 //-----------------------------------------------------------------------------
958
959
960 //-----------------------------------------------------------------------------
961 // Work with "magic Chinese" card (email him: ouyangweidaxian@live.cn)
962 //
963 //-----------------------------------------------------------------------------
964 void MifareCSetBlock(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain){
965
966 // params
967 uint8_t needWipe = arg0;
968 // bit 0 - need get UID
969 // bit 1 - need wupC
970 // bit 2 - need HALT after sequence
971 // bit 3 - need init FPGA and field before sequence
972 // bit 4 - need reset FPGA and LED
973 uint8_t workFlags = arg1;
974 uint8_t blockNo = arg2;
975
976 // card commands
977 uint8_t wupC1[] = { 0x40 };
978 uint8_t wupC2[] = { 0x43 };
979 uint8_t wipeC[] = { 0x41 };
980
981 // variables
982 byte_t isOK = 0;
983 uint8_t uid[10];
984 uint8_t d_block[18];
985 uint32_t cuid;
986
987 memset(uid, 0x00, 10);
988 uint8_t* receivedAnswer = mifare_get_bigbufptr();
989
990 if (workFlags & 0x08) {
991 // clear trace
992 iso14a_clear_trace();
993 iso14a_set_tracing(TRUE);
994
995 iso14443a_setup();
996
997 LED_A_ON();
998 LED_B_OFF();
999 LED_C_OFF();
1000
1001 SpinDelay(300);
1002 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
1003 SpinDelay(100);
1004 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A | FPGA_HF_ISO14443A_READER_MOD);
1005 }
1006
1007 while (true) {
1008 // get UID from chip
1009 if (workFlags & 0x01) {
1010 if(!iso14443a_select_card(uid, NULL, &cuid)) {
1011 if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");
1012 break;
1013 };
1014
1015 if(mifare_classic_halt(NULL, cuid)) {
1016 if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");
1017 break;
1018 };
1019 };
1020
1021 // reset chip
1022 if (needWipe){
1023 ReaderTransmitBitsPar(wupC1,7,0, NULL);
1024 if(!ReaderReceive(receivedAnswer) || (receivedAnswer[0] != 0x0a)) {
1025 if (MF_DBGLEVEL >= 1) Dbprintf("wupC1 error");
1026 break;
1027 };
1028
1029 ReaderTransmit(wipeC, sizeof(wipeC), NULL);
1030 if(!ReaderReceive(receivedAnswer) || (receivedAnswer[0] != 0x0a)) {
1031 if (MF_DBGLEVEL >= 1) Dbprintf("wipeC error");
1032 break;
1033 };
1034
1035 if(mifare_classic_halt(NULL, cuid)) {
1036 if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");
1037 break;
1038 };
1039 };
1040
1041 // write block
1042 if (workFlags & 0x02) {
1043 ReaderTransmitBitsPar(wupC1,7,0, NULL);
1044 if(!ReaderReceive(receivedAnswer) || (receivedAnswer[0] != 0x0a)) {
1045 if (MF_DBGLEVEL >= 1) Dbprintf("wupC1 error");
1046 break;
1047 };
1048
1049 ReaderTransmit(wupC2, sizeof(wupC2), NULL);
1050 if(!ReaderReceive(receivedAnswer) || (receivedAnswer[0] != 0x0a)) {
1051 if (MF_DBGLEVEL >= 1) Dbprintf("wupC2 error");
1052 break;
1053 };
1054 }
1055
1056 if ((mifare_sendcmd_short(NULL, 0, 0xA0, blockNo, receivedAnswer, NULL) != 1) || (receivedAnswer[0] != 0x0a)) {
1057 if (MF_DBGLEVEL >= 1) Dbprintf("write block send command error");
1058 break;
1059 };
1060
1061 memcpy(d_block, datain, 16);
1062 AppendCrc14443a(d_block, 16);
1063
1064 ReaderTransmit(d_block, sizeof(d_block), NULL);
1065 if ((ReaderReceive(receivedAnswer) != 1) || (receivedAnswer[0] != 0x0a)) {
1066 if (MF_DBGLEVEL >= 1) Dbprintf("write block send data error");
1067 break;
1068 };
1069
1070 if (workFlags & 0x04) {
1071 if (mifare_classic_halt(NULL, cuid)) {
1072 if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");
1073 break;
1074 };
1075 }
1076
1077 isOK = 1;
1078 break;
1079 }
1080
1081 // UsbCommand ack = {CMD_ACK, {isOK, 0, 0}};
1082 // if (isOK) memcpy(ack.d.asBytes, uid, 4);
1083
1084 // add trace trailer
1085 /**
1086 * Removed by Martin, the uid is overwritten with 0x44,
1087 * which can 't be intended.
1088 *
1089 * memset(uid, 0x44, 4);
1090 * LogTrace(uid, 4, 0, 0, TRUE);
1091 **/
1092
1093
1094 LED_B_ON();
1095 cmd_send(CMD_ACK,isOK,0,0,uid,4);
1096 // UsbSendPacket((uint8_t *)&ack, sizeof(UsbCommand));
1097 LED_B_OFF();
1098
1099 if ((workFlags & 0x10) || (!isOK)) {
1100 // Thats it...
1101 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
1102 LEDsoff();
1103 }
1104 }
1105
1106 void MifareCGetBlock(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain){
1107
1108 // params
1109 // bit 1 - need wupC
1110 // bit 2 - need HALT after sequence
1111 // bit 3 - need init FPGA and field before sequence
1112 // bit 4 - need reset FPGA and LED
1113 uint8_t workFlags = arg0;
1114 uint8_t blockNo = arg2;
1115
1116 // card commands
1117 uint8_t wupC1[] = { 0x40 };
1118 uint8_t wupC2[] = { 0x43 };
1119
1120 // variables
1121 byte_t isOK = 0;
1122 uint8_t data[18];
1123 uint32_t cuid = 0;
1124
1125 memset(data, 0x00, 18);
1126 uint8_t* receivedAnswer = mifare_get_bigbufptr();
1127
1128 if (workFlags & 0x08) {
1129 // clear trace
1130 iso14a_clear_trace();
1131 iso14a_set_tracing(TRUE);
1132
1133 iso14443a_setup();
1134
1135 LED_A_ON();
1136 LED_B_OFF();
1137 LED_C_OFF();
1138
1139 SpinDelay(300);
1140 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
1141 SpinDelay(100);
1142 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A | FPGA_HF_ISO14443A_READER_MOD);
1143 }
1144
1145 while (true) {
1146 if (workFlags & 0x02) {
1147 ReaderTransmitBitsPar(wupC1,7,0, NULL);
1148 if(!ReaderReceive(receivedAnswer) || (receivedAnswer[0] != 0x0a)) {
1149 if (MF_DBGLEVEL >= 1) Dbprintf("wupC1 error");
1150 break;
1151 };
1152
1153 ReaderTransmit(wupC2, sizeof(wupC2), NULL);
1154 if(!ReaderReceive(receivedAnswer) || (receivedAnswer[0] != 0x0a)) {
1155 if (MF_DBGLEVEL >= 1) Dbprintf("wupC2 error");
1156 break;
1157 };
1158 }
1159
1160 // read block
1161 if ((mifare_sendcmd_short(NULL, 0, 0x30, blockNo, receivedAnswer, NULL) != 18)) {
1162 if (MF_DBGLEVEL >= 1) Dbprintf("read block send command error");
1163 break;
1164 };
1165 memcpy(data, receivedAnswer, 18);
1166
1167 if (workFlags & 0x04) {
1168 if (mifare_classic_halt(NULL, cuid)) {
1169 if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");
1170 break;
1171 };
1172 }
1173
1174 isOK = 1;
1175 break;
1176 }
1177
1178 // UsbCommand ack = {CMD_ACK, {isOK, 0, 0}};
1179 // if (isOK) memcpy(ack.d.asBytes, data, 18);
1180
1181 // add trace trailer
1182 /*
1183 * Removed by Martin, this piece of overwrites the 'data' variable
1184 * which is sent two lines down, and is obviously not correct.
1185 *
1186 * memset(data, 0x44, 4);
1187 * LogTrace(data, 4, 0, 0, TRUE);
1188 */
1189 LED_B_ON();
1190 cmd_send(CMD_ACK,isOK,0,0,data,18);
1191 // UsbSendPacket((uint8_t *)&ack, sizeof(UsbCommand));
1192 LED_B_OFF();
1193
1194 if ((workFlags & 0x10) || (!isOK)) {
1195 // Thats it...
1196 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
1197 LEDsoff();
1198 }
1199 }
1200
Impressum, Datenschutz