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