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