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