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