]> git.zerfleddert.de Git - proxmark3-svn/blob - armsrc/mifarecmd.c
FIX: a suggested fix for #136 where the "lf t55x7 read" command when called with...
[proxmark3-svn] / armsrc / mifarecmd.c
1 //-----------------------------------------------------------------------------
2 // Merlok - June 2011, 2012
3 // Gerhard de Koning Gans - May 2008
4 // Hagen Fritsch - June 2010
5 // Midnitesnake - Dec 2013
6 // Andy Davies - Apr 2014
7 // Iceman - May 2014
8 //
9 // This code is licensed to you under the terms of the GNU GPL, version 2 or,
10 // at your option, any later version. See the LICENSE.txt file for the text of
11 // the license.
12 //-----------------------------------------------------------------------------
13 // Routines to support ISO 14443 type A.
14 //-----------------------------------------------------------------------------
15
16 #include "mifarecmd.h"
17 #include "apps.h"
18 #include "util.h"
19 #include "crc.h"
20
21 //-----------------------------------------------------------------------------
22 // Select, Authenticate, Read a MIFARE tag.
23 // read block
24 //-----------------------------------------------------------------------------
25 void MifareReadBlock(uint8_t arg0, uint8_t arg1, uint8_t arg2, uint8_t *datain)
26 {
27 // params
28 uint8_t blockNo = arg0;
29 uint8_t keyType = arg1;
30 uint64_t ui64Key = 0;
31 ui64Key = bytes_to_num(datain, 6);
32
33 // variables
34 byte_t isOK = 0;
35 byte_t dataoutbuf[16];
36 uint8_t uid[10];
37 uint32_t cuid;
38 struct Crypto1State mpcs = {0, 0};
39 struct Crypto1State *pcs;
40 pcs = &mpcs;
41
42 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);
43
44 clear_trace();
45
46 LED_A_ON();
47 LED_B_OFF();
48 LED_C_OFF();
49
50 while (true) {
51 if(!iso14443a_select_card(uid, NULL, &cuid)) {
52 if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");
53 break;
54 };
55
56 if(mifare_classic_auth(pcs, cuid, blockNo, keyType, ui64Key, AUTH_FIRST)) {
57 if (MF_DBGLEVEL >= 1) Dbprintf("Auth error");
58 break;
59 };
60
61 if(mifare_classic_readblock(pcs, cuid, blockNo, dataoutbuf)) {
62 if (MF_DBGLEVEL >= 1) Dbprintf("Read block error");
63 break;
64 };
65
66 if(mifare_classic_halt(pcs, cuid)) {
67 if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");
68 break;
69 };
70
71 isOK = 1;
72 break;
73 }
74
75 // ----------------------------- crypto1 destroy
76 crypto1_destroy(pcs);
77
78 if (MF_DBGLEVEL >= 2) DbpString("READ BLOCK FINISHED");
79
80 LED_B_ON();
81 cmd_send(CMD_ACK,isOK,0,0,dataoutbuf,16);
82 LED_B_OFF();
83
84 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
85 LEDsoff();
86 }
87
88 void MifareUC_Auth(uint8_t arg0, uint8_t *keybytes){
89
90 bool turnOffField = (arg0 == 1);
91
92 LED_A_ON(); LED_B_OFF(); LED_C_OFF();
93
94 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);
95
96 clear_trace();
97
98 if(!iso14443a_select_card(NULL, NULL, NULL)) {
99 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Can't select card");
100 OnError(0);
101 return;
102 };
103
104 if(!mifare_ultra_auth(keybytes)){
105 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Authentication failed");
106 OnError(1);
107 return;
108 }
109
110 if (turnOffField) {
111 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
112 LEDsoff();
113 }
114 cmd_send(CMD_ACK,1,0,0,0,0);
115 }
116
117 // Arg0 = BlockNo,
118 // Arg1 = UsePwd bool
119 // datain = PWD bytes,
120 void MifareUReadBlock(uint8_t arg0, uint8_t arg1, uint8_t *datain)
121 {
122 uint8_t blockNo = arg0;
123 byte_t dataout[16] = {0x00};
124 bool useKey = (arg1 == 1); //UL_C
125 bool usePwd = (arg1 == 2); //UL_EV1/NTAG
126
127 LEDsoff();
128 LED_A_ON();
129 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);
130
131 clear_trace();
132
133 int len = iso14443a_select_card(NULL, NULL, NULL);
134 if(!len) {
135 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Can't select card (RC:%02X)",len);
136 OnError(1);
137 return;
138 }
139
140 // UL-C authentication
141 if ( useKey ) {
142 uint8_t key[16] = {0x00};
143 memcpy(key, datain, sizeof(key) );
144
145 if ( !mifare_ultra_auth(key) ) {
146 OnError(1);
147 return;
148 }
149 }
150
151 // UL-EV1 / NTAG authentication
152 if ( usePwd ) {
153 uint8_t pwd[4] = {0x00};
154 memcpy(pwd, datain, 4);
155 uint8_t pack[4] = {0,0,0,0};
156 if (!mifare_ul_ev1_auth(pwd, pack)) {
157 OnError(1);
158 return;
159 }
160 }
161
162 if( mifare_ultra_readblock(blockNo, dataout) ) {
163 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Read block error");
164 OnError(2);
165 return;
166 }
167
168 if( mifare_ultra_halt() ) {
169 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Halt error");
170 OnError(3);
171 return;
172 }
173
174 cmd_send(CMD_ACK,1,0,0,dataout,16);
175 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
176 LEDsoff();
177 }
178
179 //-----------------------------------------------------------------------------
180 // Select, Authenticate, Read a MIFARE tag.
181 // read sector (data = 4 x 16 bytes = 64 bytes, or 16 x 16 bytes = 256 bytes)
182 //-----------------------------------------------------------------------------
183 void MifareReadSector(uint8_t arg0, uint8_t arg1, uint8_t arg2, uint8_t *datain)
184 {
185 // params
186 uint8_t sectorNo = arg0;
187 uint8_t keyType = arg1;
188 uint64_t ui64Key = 0;
189 ui64Key = bytes_to_num(datain, 6);
190
191 // variables
192 byte_t isOK = 0;
193 byte_t dataoutbuf[16 * 16];
194 uint8_t uid[10];
195 uint32_t cuid;
196 struct Crypto1State mpcs = {0, 0};
197 struct Crypto1State *pcs;
198 pcs = &mpcs;
199
200 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);
201
202 clear_trace();
203
204 LED_A_ON();
205 LED_B_OFF();
206 LED_C_OFF();
207
208 isOK = 1;
209 if(!iso14443a_select_card(uid, NULL, &cuid)) {
210 isOK = 0;
211 if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");
212 }
213
214
215 if(isOK && mifare_classic_auth(pcs, cuid, FirstBlockOfSector(sectorNo), keyType, ui64Key, AUTH_FIRST)) {
216 isOK = 0;
217 if (MF_DBGLEVEL >= 1) Dbprintf("Auth error");
218 }
219
220 for (uint8_t blockNo = 0; isOK && blockNo < NumBlocksPerSector(sectorNo); blockNo++) {
221 if(mifare_classic_readblock(pcs, cuid, FirstBlockOfSector(sectorNo) + blockNo, dataoutbuf + 16 * blockNo)) {
222 isOK = 0;
223 if (MF_DBGLEVEL >= 1) Dbprintf("Read sector %2d block %2d error", sectorNo, blockNo);
224 break;
225 }
226 }
227
228 if(mifare_classic_halt(pcs, cuid)) {
229 if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");
230 }
231
232 // ----------------------------- crypto1 destroy
233 crypto1_destroy(pcs);
234
235 if (MF_DBGLEVEL >= 2) DbpString("READ SECTOR FINISHED");
236
237 LED_B_ON();
238 cmd_send(CMD_ACK,isOK,0,0,dataoutbuf,16*NumBlocksPerSector(sectorNo));
239 LED_B_OFF();
240
241 // Thats it...
242 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
243 LEDsoff();
244 }
245
246 // arg0 = blockNo (start)
247 // arg1 = Pages (number of blocks)
248 // arg2 = useKey
249 // datain = KEY bytes
250 void MifareUReadCard(uint8_t arg0, uint16_t arg1, uint8_t arg2, uint8_t *datain)
251 {
252 LEDsoff();
253 LED_A_ON();
254 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);
255
256 // free eventually allocated BigBuf memory
257 BigBuf_free();
258 clear_trace();
259
260 // params
261 uint8_t blockNo = arg0;
262 uint16_t blocks = arg1;
263 bool useKey = (arg2 == 1); //UL_C
264 bool usePwd = (arg2 == 2); //UL_EV1/NTAG
265 uint32_t countblocks = 0;
266 uint8_t *dataout = BigBuf_malloc(CARD_MEMORY_SIZE);
267 if (dataout == NULL){
268 Dbprintf("out of memory");
269 OnError(1);
270 return;
271 }
272
273 int len = iso14443a_select_card(NULL, NULL, NULL);
274 if (!len) {
275 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Can't select card (RC:%d)",len);
276 OnError(1);
277 return;
278 }
279
280 // UL-C authentication
281 if ( useKey ) {
282 uint8_t key[16] = {0x00};
283 memcpy(key, datain, sizeof(key) );
284
285 if ( !mifare_ultra_auth(key) ) {
286 OnError(1);
287 return;
288 }
289 }
290
291 // UL-EV1 / NTAG authentication
292 if (usePwd) {
293 uint8_t pwd[4] = {0x00};
294 memcpy(pwd, datain, sizeof(pwd));
295 uint8_t pack[4] = {0,0,0,0};
296
297 if (!mifare_ul_ev1_auth(pwd, pack)){
298 OnError(1);
299 return;
300 }
301 }
302
303 for (int i = 0; i < blocks; i++){
304 if ((i*4) + 4 >= CARD_MEMORY_SIZE) {
305 Dbprintf("Data exceeds buffer!!");
306 break;
307 }
308
309 len = mifare_ultra_readblock(blockNo + i, dataout + 4 * i);
310
311 if (len) {
312 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Read block %d error",i);
313 // if no blocks read - error out
314 if (i==0){
315 OnError(2);
316 return;
317 } else {
318 //stop at last successful read block and return what we got
319 break;
320 }
321 } else {
322 countblocks++;
323 }
324 }
325
326 len = mifare_ultra_halt();
327 if (len) {
328 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Halt error");
329 OnError(3);
330 return;
331 }
332
333 if (MF_DBGLEVEL >= MF_DBG_EXTENDED) Dbprintf("Blocks read %d", countblocks);
334
335 countblocks *= 4;
336
337 cmd_send(CMD_ACK, 1, countblocks, BigBuf_max_traceLen(), 0, 0);
338 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
339 LEDsoff();
340 BigBuf_free();
341 }
342
343 //-----------------------------------------------------------------------------
344 // Select, Authenticate, Write a MIFARE tag.
345 // read block
346 //-----------------------------------------------------------------------------
347 void MifareWriteBlock(uint8_t arg0, uint8_t arg1, uint8_t arg2, uint8_t *datain)
348 {
349 // params
350 uint8_t blockNo = arg0;
351 uint8_t keyType = arg1;
352 uint64_t ui64Key = 0;
353 byte_t blockdata[16];
354
355 ui64Key = bytes_to_num(datain, 6);
356 memcpy(blockdata, datain + 10, 16);
357
358 // variables
359 byte_t isOK = 0;
360 uint8_t uid[10];
361 uint32_t cuid;
362 struct Crypto1State mpcs = {0, 0};
363 struct Crypto1State *pcs;
364 pcs = &mpcs;
365
366 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);
367
368 clear_trace();
369
370 LED_A_ON();
371 LED_B_OFF();
372 LED_C_OFF();
373
374 while (true) {
375 if(!iso14443a_select_card(uid, NULL, &cuid)) {
376 if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");
377 break;
378 };
379
380 if(mifare_classic_auth(pcs, cuid, blockNo, keyType, ui64Key, AUTH_FIRST)) {
381 if (MF_DBGLEVEL >= 1) Dbprintf("Auth error");
382 break;
383 };
384
385 if(mifare_classic_writeblock(pcs, cuid, blockNo, blockdata)) {
386 if (MF_DBGLEVEL >= 1) Dbprintf("Write block error");
387 break;
388 };
389
390 if(mifare_classic_halt(pcs, cuid)) {
391 if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");
392 break;
393 };
394
395 isOK = 1;
396 break;
397 }
398
399 // ----------------------------- crypto1 destroy
400 crypto1_destroy(pcs);
401
402 if (MF_DBGLEVEL >= 2) DbpString("WRITE BLOCK FINISHED");
403
404 LED_B_ON();
405 cmd_send(CMD_ACK,isOK,0,0,0,0);
406 LED_B_OFF();
407
408
409 // Thats it...
410 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
411 LEDsoff();
412 }
413
414 /* // Command not needed but left for future testing
415 void MifareUWriteBlockCompat(uint8_t arg0, uint8_t *datain)
416 {
417 uint8_t blockNo = arg0;
418 byte_t blockdata[16] = {0x00};
419
420 memcpy(blockdata, datain, 16);
421
422 uint8_t uid[10] = {0x00};
423
424 LED_A_ON(); LED_B_OFF(); LED_C_OFF();
425
426 clear_trace();
427 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);
428
429 if(!iso14443a_select_card(uid, NULL, NULL)) {
430 if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");
431 OnError(0);
432 return;
433 };
434
435 if(mifare_ultra_writeblock_compat(blockNo, blockdata)) {
436 if (MF_DBGLEVEL >= 1) Dbprintf("Write block error");
437 OnError(0);
438 return; };
439
440 if(mifare_ultra_halt()) {
441 if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");
442 OnError(0);
443 return;
444 };
445
446 if (MF_DBGLEVEL >= 2) DbpString("WRITE BLOCK FINISHED");
447
448 cmd_send(CMD_ACK,1,0,0,0,0);
449 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
450 LEDsoff();
451 }
452 */
453
454 // Arg0 : Block to write to.
455 // Arg1 : 0 = use no authentication.
456 // 1 = use 0x1A authentication.
457 // 2 = use 0x1B authentication.
458 // datain : 4 first bytes is data to be written.
459 // : 4/16 next bytes is authentication key.
460 void MifareUWriteBlock(uint8_t arg0, uint8_t arg1, uint8_t *datain)
461 {
462 uint8_t blockNo = arg0;
463 bool useKey = (arg1 == 1); //UL_C
464 bool usePwd = (arg1 == 2); //UL_EV1/NTAG
465 byte_t blockdata[4] = {0x00};
466
467 memcpy(blockdata, datain,4);
468
469 LEDsoff();
470 LED_A_ON();
471 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);
472
473 clear_trace();
474
475 if(!iso14443a_select_card(NULL, NULL, NULL)) {
476 if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");
477 OnError(0);
478 return;
479 };
480
481 // UL-C authentication
482 if ( useKey ) {
483 uint8_t key[16] = {0x00};
484 memcpy(key, datain+4, sizeof(key) );
485
486 if ( !mifare_ultra_auth(key) ) {
487 OnError(1);
488 return;
489 }
490 }
491
492 // UL-EV1 / NTAG authentication
493 if (usePwd) {
494 uint8_t pwd[4] = {0x00};
495 memcpy(pwd, datain+4, 4);
496 uint8_t pack[4] = {0,0,0,0};
497 if (!mifare_ul_ev1_auth(pwd, pack)) {
498 OnError(1);
499 return;
500 }
501 }
502
503 if(mifare_ultra_writeblock(blockNo, blockdata)) {
504 if (MF_DBGLEVEL >= 1) Dbprintf("Write block error");
505 OnError(0);
506 return;
507 };
508
509 if(mifare_ultra_halt()) {
510 if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");
511 OnError(0);
512 return;
513 };
514
515 if (MF_DBGLEVEL >= 2) DbpString("WRITE BLOCK FINISHED");
516
517 cmd_send(CMD_ACK,1,0,0,0,0);
518 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
519 LEDsoff();
520 }
521
522 void MifareUSetPwd(uint8_t arg0, uint8_t *datain){
523
524 uint8_t pwd[16] = {0x00};
525 byte_t blockdata[4] = {0x00};
526
527 memcpy(pwd, datain, 16);
528
529 LED_A_ON(); LED_B_OFF(); LED_C_OFF();
530 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);
531
532 clear_trace();
533
534 if(!iso14443a_select_card(NULL, NULL, NULL)) {
535 if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");
536 OnError(0);
537 return;
538 };
539
540 blockdata[0] = pwd[7];
541 blockdata[1] = pwd[6];
542 blockdata[2] = pwd[5];
543 blockdata[3] = pwd[4];
544 if(mifare_ultra_writeblock( 44, blockdata)) {
545 if (MF_DBGLEVEL >= 1) Dbprintf("Write block error");
546 OnError(44);
547 return;
548 };
549
550 blockdata[0] = pwd[3];
551 blockdata[1] = pwd[2];
552 blockdata[2] = pwd[1];
553 blockdata[3] = pwd[0];
554 if(mifare_ultra_writeblock( 45, blockdata)) {
555 if (MF_DBGLEVEL >= 1) Dbprintf("Write block error");
556 OnError(45);
557 return;
558 };
559
560 blockdata[0] = pwd[15];
561 blockdata[1] = pwd[14];
562 blockdata[2] = pwd[13];
563 blockdata[3] = pwd[12];
564 if(mifare_ultra_writeblock( 46, blockdata)) {
565 if (MF_DBGLEVEL >= 1) Dbprintf("Write block error");
566 OnError(46);
567 return;
568 };
569
570 blockdata[0] = pwd[11];
571 blockdata[1] = pwd[10];
572 blockdata[2] = pwd[9];
573 blockdata[3] = pwd[8];
574 if(mifare_ultra_writeblock( 47, blockdata)) {
575 if (MF_DBGLEVEL >= 1) Dbprintf("Write block error");
576 OnError(47);
577 return;
578 };
579
580 if(mifare_ultra_halt()) {
581 if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");
582 OnError(0);
583 return;
584 };
585
586 cmd_send(CMD_ACK,1,0,0,0,0);
587 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
588 LEDsoff();
589 }
590
591 // Return 1 if the nonce is invalid else return 0
592 int valid_nonce(uint32_t Nt, uint32_t NtEnc, uint32_t Ks1, uint8_t *parity) {
593 return ((oddparity((Nt >> 24) & 0xFF) == ((parity[0]) ^ oddparity((NtEnc >> 24) & 0xFF) ^ BIT(Ks1,16))) & \
594 (oddparity((Nt >> 16) & 0xFF) == ((parity[1]) ^ oddparity((NtEnc >> 16) & 0xFF) ^ BIT(Ks1,8))) & \
595 (oddparity((Nt >> 8) & 0xFF) == ((parity[2]) ^ oddparity((NtEnc >> 8) & 0xFF) ^ BIT(Ks1,0)))) ? 1 : 0;
596 }
597
598
599 //-----------------------------------------------------------------------------
600 // MIFARE nested authentication.
601 //
602 //-----------------------------------------------------------------------------
603 void MifareNested(uint32_t arg0, uint32_t arg1, uint32_t calibrate, uint8_t *datain)
604 {
605 // params
606 uint8_t blockNo = arg0 & 0xff;
607 uint8_t keyType = (arg0 >> 8) & 0xff;
608 uint8_t targetBlockNo = arg1 & 0xff;
609 uint8_t targetKeyType = (arg1 >> 8) & 0xff;
610 uint64_t ui64Key = 0;
611
612 ui64Key = bytes_to_num(datain, 6);
613
614 // variables
615 uint16_t rtr, i, j, len;
616 uint16_t davg;
617 static uint16_t dmin, dmax;
618 uint8_t uid[10];
619 uint32_t cuid, nt1, nt2, nttmp, nttest, ks1;
620 uint8_t par[1];
621 uint32_t target_nt[2], target_ks[2];
622
623 uint8_t par_array[4];
624 uint16_t ncount = 0;
625 struct Crypto1State mpcs = {0, 0};
626 struct Crypto1State *pcs;
627 pcs = &mpcs;
628 uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];
629
630 uint32_t auth1_time, auth2_time;
631 static uint16_t delta_time;
632
633 LED_A_ON();
634 LED_C_OFF();
635 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);
636
637 // free eventually allocated BigBuf memory
638 BigBuf_free();
639
640 if (calibrate) clear_trace();
641 set_tracing(true);
642
643 // statistics on nonce distance
644 int16_t isOK = 0;
645 #define NESTED_MAX_TRIES 12
646 uint16_t unsuccessfull_tries = 0;
647 if (calibrate) { // for first call only. Otherwise reuse previous calibration
648 LED_B_ON();
649 WDT_HIT();
650
651 davg = dmax = 0;
652 dmin = 2000;
653 delta_time = 0;
654
655 for (rtr = 0; rtr < 17; rtr++) {
656
657 // Test if the action was cancelled
658 if(BUTTON_PRESS()) {
659 isOK = -2;
660 break;
661 }
662
663 // prepare next select. No need to power down the card.
664 if(mifare_classic_halt(pcs, cuid)) {
665 if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Halt error");
666 rtr--;
667 continue;
668 }
669
670 if(!iso14443a_select_card(uid, NULL, &cuid)) {
671 if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Can't select card");
672 rtr--;
673 continue;
674 };
675
676 auth1_time = 0;
677 if(mifare_classic_authex(pcs, cuid, blockNo, keyType, ui64Key, AUTH_FIRST, &nt1, &auth1_time)) {
678 if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Auth1 error");
679 rtr--;
680 continue;
681 };
682
683 if (delta_time) {
684 auth2_time = auth1_time + delta_time;
685 } else {
686 auth2_time = 0;
687 }
688 if(mifare_classic_authex(pcs, cuid, blockNo, keyType, ui64Key, AUTH_NESTED, &nt2, &auth2_time)) {
689 if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Auth2 error");
690 rtr--;
691 continue;
692 };
693
694 nttmp = prng_successor(nt1, 100); //NXP Mifare is typical around 840,but for some unlicensed/compatible mifare card this can be 160
695 for (i = 101; i < 1200; i++) {
696 nttmp = prng_successor(nttmp, 1);
697 if (nttmp == nt2) break;
698 }
699
700 if (i != 1200) {
701 if (rtr != 0) {
702 davg += i;
703 dmin = MIN(dmin, i);
704 dmax = MAX(dmax, i);
705 }
706 else {
707 delta_time = auth2_time - auth1_time + 32; // allow some slack for proper timing
708 }
709 if (MF_DBGLEVEL >= 3) Dbprintf("Nested: calibrating... ntdist=%d", i);
710 } else {
711 unsuccessfull_tries++;
712 if (unsuccessfull_tries > NESTED_MAX_TRIES) { // card isn't vulnerable to nested attack (random numbers are not predictable)
713 isOK = -3;
714 }
715 }
716 }
717
718 davg = (davg + (rtr - 1)/2) / (rtr - 1);
719
720 if (MF_DBGLEVEL >= 3) Dbprintf("rtr=%d isOK=%d min=%d max=%d avg=%d, delta_time=%d", rtr, isOK, dmin, dmax, davg, delta_time);
721
722 dmin = davg - 2;
723 dmax = davg + 2;
724
725 LED_B_OFF();
726 }
727 // -------------------------------------------------------------------------------------------------
728
729 LED_C_ON();
730
731 // get crypted nonces for target sector
732 for(i=0; i < 2 && !isOK; i++) { // look for exactly two different nonces
733
734 target_nt[i] = 0;
735 while(target_nt[i] == 0) { // continue until we have an unambiguous nonce
736
737 // prepare next select. No need to power down the card.
738 if(mifare_classic_halt(pcs, cuid)) {
739 if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Halt error");
740 continue;
741 }
742
743 if(!iso14443a_select_card(uid, NULL, &cuid)) {
744 if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Can't select card");
745 continue;
746 };
747
748 auth1_time = 0;
749 if(mifare_classic_authex(pcs, cuid, blockNo, keyType, ui64Key, AUTH_FIRST, &nt1, &auth1_time)) {
750 if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Auth1 error");
751 continue;
752 };
753
754 // nested authentication
755 auth2_time = auth1_time + delta_time;
756 len = mifare_sendcmd_short(pcs, AUTH_NESTED, 0x60 + (targetKeyType & 0x01), targetBlockNo, receivedAnswer, par, &auth2_time);
757 if (len != 4) {
758 if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Auth2 error len=%d", len);
759 continue;
760 };
761
762 nt2 = bytes_to_num(receivedAnswer, 4);
763 if (MF_DBGLEVEL >= 3) Dbprintf("Nonce#%d: Testing nt1=%08x nt2enc=%08x nt2par=%02x", i+1, nt1, nt2, par[0]);
764
765 // Parity validity check
766 for (j = 0; j < 4; j++) {
767 par_array[j] = (oddparity(receivedAnswer[j]) != ((par[0] >> (7-j)) & 0x01));
768 }
769
770 ncount = 0;
771 nttest = prng_successor(nt1, dmin - 1);
772 for (j = dmin; j < dmax + 1; j++) {
773 nttest = prng_successor(nttest, 1);
774 ks1 = nt2 ^ nttest;
775
776 if (valid_nonce(nttest, nt2, ks1, par_array)){
777 if (ncount > 0) { // we are only interested in disambiguous nonces, try again
778 if (MF_DBGLEVEL >= 3) Dbprintf("Nonce#%d: dismissed (ambigous), ntdist=%d", i+1, j);
779 target_nt[i] = 0;
780 break;
781 }
782 target_nt[i] = nttest;
783 target_ks[i] = ks1;
784 ncount++;
785 if (i == 1 && target_nt[1] == target_nt[0]) { // we need two different nonces
786 target_nt[i] = 0;
787 if (MF_DBGLEVEL >= 3) Dbprintf("Nonce#2: dismissed (= nonce#1), ntdist=%d", j);
788 break;
789 }
790 if (MF_DBGLEVEL >= 3) Dbprintf("Nonce#%d: valid, ntdist=%d", i+1, j);
791 }
792 }
793 if (target_nt[i] == 0 && j == dmax+1 && MF_DBGLEVEL >= 3) Dbprintf("Nonce#%d: dismissed (all invalid)", i+1);
794 }
795 }
796
797 LED_C_OFF();
798
799 // ----------------------------- crypto1 destroy
800 crypto1_destroy(pcs);
801
802 byte_t buf[4 + 4 * 4];
803 memcpy(buf, &cuid, 4);
804 memcpy(buf+4, &target_nt[0], 4);
805 memcpy(buf+8, &target_ks[0], 4);
806 memcpy(buf+12, &target_nt[1], 4);
807 memcpy(buf+16, &target_ks[1], 4);
808
809 LED_B_ON();
810 cmd_send(CMD_ACK, isOK, 0, targetBlockNo + (targetKeyType * 0x100), buf, sizeof(buf));
811 LED_B_OFF();
812
813 if (MF_DBGLEVEL >= 3) DbpString("NESTED FINISHED");
814
815 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
816 LEDsoff();
817 set_tracing(FALSE);
818 }
819
820 //-----------------------------------------------------------------------------
821 // MIFARE check keys. key count up to 85.
822 //
823 //-----------------------------------------------------------------------------
824 void MifareChkKeys(uint16_t arg0, uint8_t arg1, uint8_t arg2, uint8_t *datain)
825 {
826 // params
827 uint8_t blockNo = arg0 & 0xff;
828 uint8_t keyType = (arg0 >> 8) & 0xff;
829 bool clearTrace = arg1;
830 uint8_t keyCount = arg2;
831 uint64_t ui64Key = 0;
832
833 // variables
834 int i;
835 byte_t isOK = 0;
836 uint8_t uid[10];
837 uint32_t cuid;
838 struct Crypto1State mpcs = {0, 0};
839 struct Crypto1State *pcs;
840 pcs = &mpcs;
841
842 // clear debug level
843 int OLD_MF_DBGLEVEL = MF_DBGLEVEL;
844 MF_DBGLEVEL = MF_DBG_NONE;
845
846 LED_A_ON();
847 LED_B_OFF();
848 LED_C_OFF();
849 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);
850
851 if (clearTrace) clear_trace();
852 set_tracing(TRUE);
853
854 for (i = 0; i < keyCount; i++) {
855 if(mifare_classic_halt(pcs, cuid)) {
856 if (MF_DBGLEVEL >= 1) Dbprintf("ChkKeys: Halt error");
857 }
858
859 if(!iso14443a_select_card(uid, NULL, &cuid)) {
860 if (OLD_MF_DBGLEVEL >= 1) Dbprintf("ChkKeys: Can't select card");
861 break;
862 };
863
864 ui64Key = bytes_to_num(datain + i * 6, 6);
865 if(mifare_classic_auth(pcs, cuid, blockNo, keyType, ui64Key, AUTH_FIRST)) {
866 continue;
867 };
868
869 isOK = 1;
870 break;
871 }
872
873 // ----------------------------- crypto1 destroy
874 crypto1_destroy(pcs);
875
876 LED_B_ON();
877 cmd_send(CMD_ACK,isOK,0,0,datain + i * 6,6);
878 LED_B_OFF();
879
880 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
881 LEDsoff();
882 set_tracing(FALSE);
883
884 // restore debug level
885 MF_DBGLEVEL = OLD_MF_DBGLEVEL;
886 }
887
888 //-----------------------------------------------------------------------------
889 // MIFARE commands set debug level
890 //
891 //-----------------------------------------------------------------------------
892 void MifareSetDbgLvl(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain){
893 MF_DBGLEVEL = arg0;
894 Dbprintf("Debug level: %d", MF_DBGLEVEL);
895 }
896
897 //-----------------------------------------------------------------------------
898 // Work with emulator memory
899 //
900 // Note: we call FpgaDownloadAndGo(FPGA_BITSTREAM_HF) here although FPGA is not
901 // involved in dealing with emulator memory. But if it is called later, it might
902 // destroy the Emulator Memory.
903 //-----------------------------------------------------------------------------
904
905 void MifareEMemClr(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain){
906 FpgaDownloadAndGo(FPGA_BITSTREAM_HF);
907 emlClearMem();
908 }
909
910 void MifareEMemSet(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain){
911 FpgaDownloadAndGo(FPGA_BITSTREAM_HF);
912 //emlSetMem(datain, arg0, arg1); // data, block num, blocks count
913 emlSetMem_xt(datain, arg0, arg1, arg2); // data, block num, blocks count, block byte width
914 }
915
916 void MifareEMemGet(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain){
917 FpgaDownloadAndGo(FPGA_BITSTREAM_HF);
918 byte_t buf[USB_CMD_DATA_SIZE];
919 emlGetMem(buf, arg0, arg1); // data, block num, blocks count (max 4)
920
921 LED_B_ON();
922 cmd_send(CMD_ACK,arg0,arg1,0,buf,USB_CMD_DATA_SIZE);
923 LED_B_OFF();
924 }
925
926 //-----------------------------------------------------------------------------
927 // Load a card into the emulator memory
928 //
929 //-----------------------------------------------------------------------------
930 void MifareECardLoad(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain){
931 uint8_t numSectors = arg0;
932 uint8_t keyType = arg1;
933 uint64_t ui64Key = 0;
934 uint32_t cuid;
935 struct Crypto1State mpcs = {0, 0};
936 struct Crypto1State *pcs;
937 pcs = &mpcs;
938
939 // variables
940 byte_t dataoutbuf[16];
941 byte_t dataoutbuf2[16];
942 uint8_t uid[10];
943
944 LED_A_ON();
945 LED_B_OFF();
946 LED_C_OFF();
947 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);
948
949 clear_trace();
950 set_tracing(TRUE);
951
952 bool isOK = true;
953
954 if(!iso14443a_select_card(uid, NULL, &cuid)) {
955 isOK = false;
956 if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");
957 }
958
959 for (uint8_t sectorNo = 0; isOK && sectorNo < numSectors; sectorNo++) {
960 ui64Key = emlGetKey(sectorNo, keyType);
961 if (sectorNo == 0){
962 if(isOK && mifare_classic_auth(pcs, cuid, FirstBlockOfSector(sectorNo), keyType, ui64Key, AUTH_FIRST)) {
963 isOK = false;
964 if (MF_DBGLEVEL >= 1) Dbprintf("Sector[%2d]. Auth error", sectorNo);
965 break;
966 }
967 } else {
968 if(isOK && mifare_classic_auth(pcs, cuid, FirstBlockOfSector(sectorNo), keyType, ui64Key, AUTH_NESTED)) {
969 isOK = false;
970 if (MF_DBGLEVEL >= 1) Dbprintf("Sector[%2d]. Auth nested error", sectorNo);
971 break;
972 }
973 }
974
975 for (uint8_t blockNo = 0; isOK && blockNo < NumBlocksPerSector(sectorNo); blockNo++) {
976 if(isOK && mifare_classic_readblock(pcs, cuid, FirstBlockOfSector(sectorNo) + blockNo, dataoutbuf)) {
977 isOK = false;
978 if (MF_DBGLEVEL >= 1) Dbprintf("Error reading sector %2d block %2d", sectorNo, blockNo);
979 break;
980 };
981 if (isOK) {
982 if (blockNo < NumBlocksPerSector(sectorNo) - 1) {
983 emlSetMem(dataoutbuf, FirstBlockOfSector(sectorNo) + blockNo, 1);
984 } else { // sector trailer, keep the keys, set only the AC
985 emlGetMem(dataoutbuf2, FirstBlockOfSector(sectorNo) + blockNo, 1);
986 memcpy(&dataoutbuf2[6], &dataoutbuf[6], 4);
987 emlSetMem(dataoutbuf2, FirstBlockOfSector(sectorNo) + blockNo, 1);
988 }
989 }
990 }
991
992 }
993
994 if(mifare_classic_halt(pcs, cuid)) {
995 if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");
996 };
997
998 // ----------------------------- crypto1 destroy
999 crypto1_destroy(pcs);
1000
1001 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
1002 LEDsoff();
1003
1004 if (MF_DBGLEVEL >= 2) DbpString("EMUL FILL SECTORS FINISHED");
1005
1006 set_tracing(FALSE);
1007 }
1008
1009
1010 //-----------------------------------------------------------------------------
1011 // Work with "magic Chinese" card (email him: ouyangweidaxian@live.cn)
1012 //
1013 //-----------------------------------------------------------------------------
1014 void MifareCSetBlock(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain){
1015
1016 // params
1017 uint8_t needWipe = arg0;
1018 // bit 0 - need get UID
1019 // bit 1 - need wupC
1020 // bit 2 - need HALT after sequence
1021 // bit 3 - need init FPGA and field before sequence
1022 // bit 4 - need reset FPGA and LED
1023 uint8_t workFlags = arg1;
1024 uint8_t blockNo = arg2;
1025
1026 // card commands
1027 uint8_t wupC1[] = { 0x40 };
1028 uint8_t wupC2[] = { 0x43 };
1029 uint8_t wipeC[] = { 0x41 };
1030
1031 // variables
1032 byte_t isOK = 0;
1033 uint8_t uid[10] = {0x00};
1034 uint8_t d_block[18] = {0x00};
1035 uint32_t cuid;
1036
1037 uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];
1038 uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE];
1039
1040 // reset FPGA and LED
1041 if (workFlags & 0x08) {
1042 LED_A_ON();
1043 LED_B_OFF();
1044 LED_C_OFF();
1045 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);
1046
1047 clear_trace();
1048 set_tracing(TRUE);
1049 }
1050
1051 while (true) {
1052
1053 // get UID from chip
1054 if (workFlags & 0x01) {
1055 if(!iso14443a_select_card(uid, NULL, &cuid)) {
1056 if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");
1057 //break;
1058 };
1059
1060 if(mifare_classic_halt(NULL, cuid)) {
1061 if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");
1062 //break;
1063 };
1064 };
1065
1066 // reset chip
1067 if (needWipe){
1068 ReaderTransmitBitsPar(wupC1,7,0, NULL);
1069 if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != 0x0a)) {
1070 if (MF_DBGLEVEL >= 1) Dbprintf("wupC1 error");
1071 break;
1072 };
1073
1074 ReaderTransmit(wipeC, sizeof(wipeC), NULL);
1075 if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != 0x0a)) {
1076 if (MF_DBGLEVEL >= 1) Dbprintf("wipeC error");
1077 break;
1078 };
1079
1080 if(mifare_classic_halt(NULL, cuid)) {
1081 if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");
1082 break;
1083 };
1084 };
1085
1086 // write block
1087 if (workFlags & 0x02) {
1088 ReaderTransmitBitsPar(wupC1,7,0, NULL);
1089 if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != 0x0a)) {
1090 if (MF_DBGLEVEL >= 1) Dbprintf("wupC1 error");
1091 break;
1092 };
1093
1094 ReaderTransmit(wupC2, sizeof(wupC2), NULL);
1095 if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != 0x0a)) {
1096 if (MF_DBGLEVEL >= 1) Dbprintf("wupC2 error");
1097 break;
1098 };
1099 }
1100
1101 if ((mifare_sendcmd_short(NULL, 0, 0xA0, blockNo, receivedAnswer, receivedAnswerPar, NULL) != 1) || (receivedAnswer[0] != 0x0a)) {
1102 if (MF_DBGLEVEL >= 1) Dbprintf("write block send command error");
1103 break;
1104 };
1105
1106 memcpy(d_block, datain, 16);
1107 AppendCrc14443a(d_block, 16);
1108
1109 ReaderTransmit(d_block, sizeof(d_block), NULL);
1110 if ((ReaderReceive(receivedAnswer, receivedAnswerPar) != 1) || (receivedAnswer[0] != 0x0a)) {
1111 if (MF_DBGLEVEL >= 1) Dbprintf("write block send data error");
1112 break;
1113 };
1114
1115 if (workFlags & 0x04) {
1116 if (mifare_classic_halt(NULL, cuid)) {
1117 if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");
1118 break;
1119 };
1120 }
1121
1122 isOK = 1;
1123 break;
1124 }
1125
1126 LED_B_ON();
1127 cmd_send(CMD_ACK,isOK,0,0,uid,4);
1128 LED_B_OFF();
1129
1130 if ((workFlags & 0x10) || (!isOK)) {
1131 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
1132 LEDsoff();
1133 set_tracing(FALSE);
1134 }
1135 }
1136
1137
1138 void MifareCGetBlock(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain){
1139
1140 // params
1141 // bit 1 - need wupC
1142 // bit 2 - need HALT after sequence
1143 // bit 3 - need init FPGA and field before sequence
1144 // bit 4 - need reset FPGA and LED
1145 // bit 5 - need to set datain instead of issuing USB reply (called via ARM for StandAloneMode14a)
1146 uint8_t workFlags = arg0;
1147 uint8_t blockNo = arg2;
1148
1149 // card commands
1150 uint8_t wupC1[] = { 0x40 };
1151 uint8_t wupC2[] = { 0x43 };
1152
1153 // variables
1154 byte_t isOK = 0;
1155 uint8_t data[18] = {0x00};
1156 uint32_t cuid = 0;
1157
1158 uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];
1159 uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE];
1160
1161 if (workFlags & 0x08) {
1162 LED_A_ON();
1163 LED_B_OFF();
1164 LED_C_OFF();
1165 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);
1166
1167 clear_trace();
1168 set_tracing(TRUE);
1169 }
1170
1171 while (true) {
1172 if (workFlags & 0x02) {
1173 ReaderTransmitBitsPar(wupC1,7,0, NULL);
1174 if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != 0x0a)) {
1175 if (MF_DBGLEVEL >= 1) Dbprintf("wupC1 error");
1176 break;
1177 };
1178
1179 ReaderTransmit(wupC2, sizeof(wupC2), NULL);
1180 if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != 0x0a)) {
1181 if (MF_DBGLEVEL >= 1) Dbprintf("wupC2 error");
1182 break;
1183 };
1184 }
1185
1186 // read block
1187 if ((mifare_sendcmd_short(NULL, 0, 0x30, blockNo, receivedAnswer, receivedAnswerPar, NULL) != 18)) {
1188 if (MF_DBGLEVEL >= 1) Dbprintf("read block send command error");
1189 break;
1190 };
1191 memcpy(data, receivedAnswer, 18);
1192
1193 if (workFlags & 0x04) {
1194 if (mifare_classic_halt(NULL, cuid)) {
1195 if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");
1196 break;
1197 };
1198 }
1199
1200 isOK = 1;
1201 break;
1202 }
1203
1204 LED_B_ON();
1205 if (workFlags & 0x20) {
1206 if (isOK)
1207 memcpy(datain, data, 18);
1208 }
1209 else
1210 cmd_send(CMD_ACK,isOK,0,0,data,18);
1211 LED_B_OFF();
1212
1213 if ((workFlags & 0x10) || (!isOK)) {
1214 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
1215 LEDsoff();
1216 set_tracing(FALSE);
1217 }
1218 }
1219
1220 void MifareCIdent(){
1221
1222 // card commands
1223 uint8_t wupC1[] = { 0x40 };
1224 uint8_t wupC2[] = { 0x43 };
1225
1226 // variables
1227 byte_t isOK = 1;
1228
1229 uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];
1230 uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE];
1231
1232 ReaderTransmitBitsPar(wupC1,7,0, NULL);
1233 if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != 0x0a)) {
1234 isOK = 0;
1235 };
1236
1237 ReaderTransmit(wupC2, sizeof(wupC2), NULL);
1238 if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != 0x0a)) {
1239 isOK = 0;
1240 };
1241
1242 if (mifare_classic_halt(NULL, 0)) {
1243 isOK = 0;
1244 };
1245
1246 cmd_send(CMD_ACK,isOK,0,0,0,0);
1247 }
1248
1249 void MifareCollectNonces(uint32_t arg0, uint32_t arg1){
1250
1251 BigBuf_free();
1252
1253 uint32_t iterations = arg0;
1254 uint8_t uid[10] = {0x00};
1255
1256 uint8_t *response = BigBuf_malloc(MAX_MIFARE_FRAME_SIZE);
1257 uint8_t *responsePar = BigBuf_malloc(MAX_MIFARE_PARITY_SIZE);
1258
1259 uint8_t mf_auth[] = { 0x60,0x00,0xf5,0x7b };
1260
1261 // get memory from BigBuf.
1262 uint8_t *nonces = BigBuf_malloc(iterations * 4);
1263
1264 LED_A_ON();
1265 LED_B_OFF();
1266 LED_C_OFF();
1267
1268 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);
1269 clear_trace();
1270 set_tracing(TRUE);
1271
1272 for (int i = 0; i < iterations; i++) {
1273
1274 WDT_HIT();
1275
1276 // Test if the action was cancelled
1277 if(BUTTON_PRESS()) break;
1278
1279 // if(mifare_classic_halt(pcs, cuid)) {
1280 // if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");
1281 //}
1282
1283 if(!iso14443a_select_card(uid, NULL, NULL)) {
1284 if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");
1285 continue;
1286 };
1287
1288 // Transmit MIFARE_CLASSIC_AUTH.
1289 ReaderTransmit(mf_auth, sizeof(mf_auth), NULL);
1290
1291 // Receive the (4 Byte) "random" nonce
1292 if (!ReaderReceive(response, responsePar)) {
1293 if (MF_DBGLEVEL >= 1) Dbprintf("Couldn't receive tag nonce");
1294 continue;
1295 }
1296
1297 nonces[i*4] = bytes_to_num(response, 4);
1298 }
1299
1300 int packLen = iterations * 4;
1301 int packSize = 0;
1302 int packNum = 0;
1303 while (packLen > 0) {
1304 packSize = MIN(USB_CMD_DATA_SIZE, packLen);
1305 LED_B_ON();
1306 cmd_send(CMD_ACK, 77, 0, packSize, nonces - packLen, packSize);
1307 LED_B_OFF();
1308
1309 packLen -= packSize;
1310 packNum++;
1311 }
1312 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
1313 LEDsoff();
1314 set_tracing(FALSE);
1315 }
1316
1317 //
1318 // DESFIRE
1319 //
1320
1321 void Mifare_DES_Auth1(uint8_t arg0, uint8_t *datain){
1322
1323 byte_t dataout[11] = {0x00};
1324 uint8_t uid[10] = {0x00};
1325 uint32_t cuid = 0x00;
1326
1327 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);
1328 clear_trace();
1329
1330 int len = iso14443a_select_card(uid, NULL, &cuid);
1331 if(!len) {
1332 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Can't select card");
1333 OnError(1);
1334 return;
1335 };
1336
1337 if(mifare_desfire_des_auth1(cuid, dataout)){
1338 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Authentication part1: Fail.");
1339 OnError(4);
1340 return;
1341 }
1342
1343 if (MF_DBGLEVEL >= MF_DBG_EXTENDED) DbpString("AUTH 1 FINISHED");
1344 cmd_send(CMD_ACK,1,cuid,0,dataout, sizeof(dataout));
1345 }
1346
1347 void Mifare_DES_Auth2(uint32_t arg0, uint8_t *datain){
1348
1349 uint32_t cuid = arg0;
1350 uint8_t key[16] = {0x00};
1351 byte_t dataout[12] = {0x00};
1352 byte_t isOK = 0;
1353
1354 memcpy(key, datain, 16);
1355
1356 isOK = mifare_desfire_des_auth2(cuid, key, dataout);
1357
1358 if( isOK) {
1359 if (MF_DBGLEVEL >= MF_DBG_EXTENDED) Dbprintf("Authentication part2: Failed");
1360 OnError(4);
1361 return;
1362 }
1363
1364 if (MF_DBGLEVEL >= MF_DBG_EXTENDED) DbpString("AUTH 2 FINISHED");
1365
1366 cmd_send(CMD_ACK, isOK, 0, 0, dataout, sizeof(dataout));
1367 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
1368 LEDsoff();
1369 }
Impressum, Datenschutz