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