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