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