]> git.zerfleddert.de Git - proxmark3-svn/blame_incremental - armsrc/mifarecmd.c
FIX: 'LF PYRAMID' the crc8_MAXIM and crc16_DNP was calling the wrong crc method....
[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,2015,2016\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#include "protocols.h"\r
21#include "parity.h"\r
22\r
23//-----------------------------------------------------------------------------\r
24// Select, Authenticate, Read a MIFARE tag. \r
25// read block\r
26//-----------------------------------------------------------------------------\r
27void MifareReadBlock(uint8_t arg0, uint8_t arg1, uint8_t arg2, uint8_t *datain)\r
28{\r
29 // params\r
30 uint8_t blockNo = arg0;\r
31 uint8_t keyType = arg1;\r
32 uint64_t ui64Key = 0;\r
33 ui64Key = bytes_to_num(datain, 6);\r
34 \r
35 // variables\r
36 byte_t isOK = 0;\r
37 byte_t dataoutbuf[16] = {0x00};\r
38 uint8_t uid[10] = {0x00};\r
39 uint32_t cuid = 0;\r
40 struct Crypto1State mpcs = {0, 0};\r
41 struct Crypto1State *pcs;\r
42 pcs = &mpcs;\r
43\r
44 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r
45\r
46 clear_trace();\r
47 set_tracing(true);\r
48\r
49 LED_A_ON();\r
50 LED_B_OFF();\r
51 LED_C_OFF();\r
52\r
53 while (true) {\r
54 if(!iso14443a_select_card(uid, NULL, &cuid, true, 0)) {\r
55 if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");\r
56 break;\r
57 };\r
58\r
59 if(mifare_classic_auth(pcs, cuid, blockNo, keyType, ui64Key, AUTH_FIRST)) {\r
60 if (MF_DBGLEVEL >= 1) Dbprintf("Auth error");\r
61 break;\r
62 };\r
63 \r
64 if(mifare_classic_readblock(pcs, cuid, blockNo, dataoutbuf)) {\r
65 if (MF_DBGLEVEL >= 1) Dbprintf("Read block error");\r
66 break;\r
67 };\r
68\r
69 if(mifare_classic_halt(pcs, cuid)) {\r
70 if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");\r
71 break;\r
72 };\r
73 \r
74 isOK = 1;\r
75 break;\r
76 }\r
77 \r
78 crypto1_destroy(pcs);\r
79 \r
80 if (MF_DBGLEVEL >= 2) DbpString("READ BLOCK FINISHED");\r
81\r
82 LED_B_ON();\r
83 cmd_send(CMD_ACK,isOK,0,0,dataoutbuf,16);\r
84 LED_B_OFF();\r
85\r
86 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
87 LEDsoff();\r
88}\r
89\r
90void MifareUC_Auth(uint8_t arg0, uint8_t *keybytes){\r
91\r
92 bool turnOffField = (arg0 == 1);\r
93\r
94 LED_A_ON(); LED_B_OFF(); LED_C_OFF();\r
95\r
96 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r
97\r
98 clear_trace();\r
99 set_tracing(true);\r
100\r
101 if(!iso14443a_select_card(NULL, NULL, NULL, true, 0)) {\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 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r
133\r
134 clear_trace();\r
135 set_tracing(true);\r
136\r
137 int len = iso14443a_select_card(NULL, NULL, NULL, true, 0);\r
138 if(!len) {\r
139 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Can't select card (RC:%02X)",len);\r
140 OnError(1);\r
141 return;\r
142 }\r
143\r
144 // UL-C authentication\r
145 if ( useKey ) {\r
146 uint8_t key[16] = {0x00};\r
147 memcpy(key, datain, sizeof(key) );\r
148\r
149 if ( !mifare_ultra_auth(key) ) {\r
150 OnError(1);\r
151 return;\r
152 }\r
153 }\r
154\r
155 // UL-EV1 / NTAG authentication\r
156 if ( usePwd ) {\r
157 uint8_t pwd[4] = {0x00};\r
158 memcpy(pwd, datain, 4);\r
159 uint8_t pack[4] = {0,0,0,0};\r
160 if (!mifare_ul_ev1_auth(pwd, pack)) {\r
161 OnError(1);\r
162 return;\r
163 }\r
164 } \r
165\r
166 if( mifare_ultra_readblock(blockNo, dataout) ) {\r
167 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Read block error");\r
168 OnError(2);\r
169 return;\r
170 }\r
171\r
172 if( mifare_ultra_halt() ) {\r
173 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Halt error");\r
174 OnError(3);\r
175 return;\r
176 }\r
177\r
178 cmd_send(CMD_ACK,1,0,0,dataout,16);\r
179 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
180 LEDsoff();\r
181}\r
182\r
183//-----------------------------------------------------------------------------\r
184// Select, Authenticate, Read a MIFARE tag. \r
185// read sector (data = 4 x 16 bytes = 64 bytes, or 16 x 16 bytes = 256 bytes)\r
186//-----------------------------------------------------------------------------\r
187void MifareReadSector(uint8_t arg0, uint8_t arg1, uint8_t arg2, uint8_t *datain)\r
188{\r
189 // params\r
190 uint8_t sectorNo = arg0;\r
191 uint8_t keyType = arg1;\r
192 uint64_t ui64Key = 0;\r
193 ui64Key = bytes_to_num(datain, 6);\r
194 \r
195 // variables\r
196 byte_t isOK = 0;\r
197 byte_t dataoutbuf[16 * 16];\r
198 uint8_t uid[10] = {0x00};\r
199 uint32_t cuid = 0;\r
200 struct Crypto1State mpcs = {0, 0};\r
201 struct Crypto1State *pcs;\r
202 pcs = &mpcs;\r
203\r
204 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r
205\r
206 clear_trace();\r
207 set_tracing(true);\r
208 \r
209 LED_A_ON();\r
210 LED_B_OFF();\r
211 LED_C_OFF();\r
212\r
213 isOK = 1;\r
214 if(!iso14443a_select_card(uid, NULL, &cuid, true, 0)) {\r
215 isOK = 0;\r
216 if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");\r
217 }\r
218 \r
219 \r
220 if(isOK && mifare_classic_auth(pcs, cuid, FirstBlockOfSector(sectorNo), keyType, ui64Key, AUTH_FIRST)) {\r
221 isOK = 0;\r
222 if (MF_DBGLEVEL >= 1) Dbprintf("Auth error");\r
223 }\r
224 \r
225 for (uint8_t blockNo = 0; isOK && blockNo < NumBlocksPerSector(sectorNo); blockNo++) {\r
226 if(mifare_classic_readblock(pcs, cuid, FirstBlockOfSector(sectorNo) + blockNo, dataoutbuf + 16 * blockNo)) {\r
227 isOK = 0;\r
228 if (MF_DBGLEVEL >= 1) Dbprintf("Read sector %2d block %2d error", sectorNo, blockNo);\r
229 break;\r
230 }\r
231 }\r
232 \r
233 if(mifare_classic_halt(pcs, cuid)) {\r
234 if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");\r
235 }\r
236\r
237 if (MF_DBGLEVEL >= 2) DbpString("READ SECTOR FINISHED");\r
238\r
239 crypto1_destroy(pcs);\r
240\r
241 LED_B_ON();\r
242 cmd_send(CMD_ACK,isOK,0,0,dataoutbuf,16*NumBlocksPerSector(sectorNo));\r
243 LED_B_OFF();\r
244\r
245 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
246 LEDsoff();\r
247 set_tracing(FALSE);\r
248}\r
249\r
250// arg0 = blockNo (start)\r
251// arg1 = Pages (number of blocks)\r
252// arg2 = useKey\r
253// datain = KEY bytes\r
254void MifareUReadCard(uint8_t arg0, uint16_t arg1, uint8_t arg2, uint8_t *datain)\r
255{\r
256 LEDsoff();\r
257 LED_A_ON();\r
258 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r
259\r
260 // free eventually allocated BigBuf memory\r
261 BigBuf_free(); BigBuf_Clear_ext(false);\r
262 clear_trace();\r
263 set_tracing(true);\r
264 \r
265 // params\r
266 uint8_t blockNo = arg0;\r
267 uint16_t blocks = arg1;\r
268 bool useKey = (arg2 == 1); //UL_C\r
269 bool usePwd = (arg2 == 2); //UL_EV1/NTAG\r
270 uint32_t countblocks = 0;\r
271 uint8_t *dataout = BigBuf_malloc(CARD_MEMORY_SIZE);\r
272 if (dataout == NULL){\r
273 Dbprintf("out of memory");\r
274 OnError(1);\r
275 return;\r
276 }\r
277\r
278 int len = iso14443a_select_card(NULL, NULL, NULL, true, 0);\r
279 if (!len) {\r
280 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Can't select card (RC:%d)",len);\r
281 OnError(1);\r
282 return;\r
283 }\r
284\r
285 // UL-C authentication\r
286 if ( useKey ) {\r
287 uint8_t key[16] = {0x00};\r
288 memcpy(key, datain, sizeof(key) );\r
289\r
290 if ( !mifare_ultra_auth(key) ) {\r
291 OnError(1);\r
292 return;\r
293 }\r
294 }\r
295\r
296 // UL-EV1 / NTAG authentication\r
297 if (usePwd) {\r
298 uint8_t pwd[4] = {0x00};\r
299 memcpy(pwd, datain, sizeof(pwd));\r
300 uint8_t pack[4] = {0,0,0,0};\r
301\r
302 if (!mifare_ul_ev1_auth(pwd, pack)){\r
303 OnError(1);\r
304 return; \r
305 }\r
306 }\r
307\r
308 for (int i = 0; i < blocks; i++){\r
309 if ((i*4) + 4 >= CARD_MEMORY_SIZE) {\r
310 Dbprintf("Data exceeds buffer!!");\r
311 break;\r
312 }\r
313\r
314 len = mifare_ultra_readblock(blockNo + i, dataout + 4 * i);\r
315 \r
316 if (len) {\r
317 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Read block %d error",i);\r
318 // if no blocks read - error out\r
319 if (i==0){\r
320 OnError(2);\r
321 return;\r
322 } else {\r
323 //stop at last successful read block and return what we got\r
324 break;\r
325 }\r
326 } else {\r
327 countblocks++;\r
328 }\r
329 }\r
330\r
331 len = mifare_ultra_halt();\r
332 if (len) {\r
333 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Halt error");\r
334 OnError(3);\r
335 return;\r
336 }\r
337\r
338 if (MF_DBGLEVEL >= MF_DBG_EXTENDED) Dbprintf("Blocks read %d", countblocks);\r
339\r
340 countblocks *= 4;\r
341\r
342 cmd_send(CMD_ACK, 1, countblocks, BigBuf_max_traceLen(), 0, 0);\r
343 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
344 LEDsoff();\r
345 BigBuf_free();\r
346 set_tracing(FALSE);\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] = {0x00};\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] = {0x00};\r
367 uint32_t cuid = 0;\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 set_tracing(true);\r
376 \r
377 LED_A_ON();\r
378 LED_B_OFF();\r
379 LED_C_OFF();\r
380\r
381 while (true) {\r
382 if(!iso14443a_select_card(uid, NULL, &cuid, true, 0)) {\r
383 if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");\r
384 break;\r
385 };\r
386\r
387 if(mifare_classic_auth(pcs, cuid, blockNo, keyType, ui64Key, AUTH_FIRST)) {\r
388 if (MF_DBGLEVEL >= 1) Dbprintf("Auth error");\r
389 break;\r
390 };\r
391 \r
392 if(mifare_classic_writeblock(pcs, cuid, blockNo, blockdata)) {\r
393 if (MF_DBGLEVEL >= 1) Dbprintf("Write block error");\r
394 break;\r
395 };\r
396\r
397 if(mifare_classic_halt(pcs, cuid)) {\r
398 if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");\r
399 break;\r
400 };\r
401 \r
402 isOK = 1;\r
403 break;\r
404 }\r
405 \r
406 crypto1_destroy(pcs);\r
407 \r
408 if (MF_DBGLEVEL >= 2) DbpString("WRITE BLOCK FINISHED");\r
409\r
410 cmd_send(CMD_ACK,isOK,0,0,0,0);\r
411\r
412 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
413 LEDsoff();\r
414 set_tracing(FALSE);\r
415}\r
416\r
417/* // Command not needed but left for future testing \r
418void MifareUWriteBlockCompat(uint8_t arg0, uint8_t *datain)\r
419{\r
420 uint8_t blockNo = arg0;\r
421 byte_t blockdata[16] = {0x00};\r
422\r
423 memcpy(blockdata, datain, 16);\r
424\r
425 uint8_t uid[10] = {0x00};\r
426\r
427 LED_A_ON(); LED_B_OFF(); LED_C_OFF();\r
428\r
429 clear_trace();\r
430 set_tracing(true);\r
431 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r
432\r
433 if(!iso14443a_select_card(uid, NULL, NULL, true, 0)) {\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 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r
476\r
477 clear_trace();\r
478 set_tracing(true);\r
479 \r
480 if(!iso14443a_select_card(NULL, NULL, NULL, true, 0)) {\r
481 if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");\r
482 OnError(0);\r
483 return;\r
484 };\r
485\r
486 // UL-C authentication\r
487 if ( useKey ) {\r
488 uint8_t key[16] = {0x00}; \r
489 memcpy(key, datain+4, sizeof(key) );\r
490\r
491 if ( !mifare_ultra_auth(key) ) {\r
492 OnError(1);\r
493 return; \r
494 }\r
495 }\r
496 \r
497 // UL-EV1 / NTAG authentication\r
498 if (usePwd) { \r
499 uint8_t pwd[4] = {0x00};\r
500 memcpy(pwd, datain+4, 4);\r
501 uint8_t pack[4] = {0,0,0,0};\r
502 if (!mifare_ul_ev1_auth(pwd, pack)) {\r
503 OnError(1);\r
504 return; \r
505 }\r
506 }\r
507 \r
508 if(mifare_ultra_writeblock(blockNo, blockdata)) {\r
509 if (MF_DBGLEVEL >= 1) Dbprintf("Write block error");\r
510 OnError(0);\r
511 return;\r
512 };\r
513\r
514 if(mifare_ultra_halt()) {\r
515 if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");\r
516 OnError(0);\r
517 return;\r
518 };\r
519\r
520 if (MF_DBGLEVEL >= 2) DbpString("WRITE BLOCK FINISHED");\r
521\r
522 cmd_send(CMD_ACK,1,0,0,0,0);\r
523 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
524 LEDsoff();\r
525 set_tracing(FALSE);\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 set_tracing(true);\r
540 \r
541 if(!iso14443a_select_card(NULL, NULL, NULL, true, 0)) {\r
542 if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");\r
543 OnError(0);\r
544 return;\r
545 };\r
546\r
547 blockdata[0] = pwd[7];\r
548 blockdata[1] = pwd[6];\r
549 blockdata[2] = pwd[5];\r
550 blockdata[3] = pwd[4];\r
551 if(mifare_ultra_writeblock( 44, blockdata)) {\r
552 if (MF_DBGLEVEL >= 1) Dbprintf("Write block error");\r
553 OnError(44);\r
554 return;\r
555 };\r
556\r
557 blockdata[0] = pwd[3];\r
558 blockdata[1] = pwd[2];\r
559 blockdata[2] = pwd[1];\r
560 blockdata[3] = pwd[0];\r
561 if(mifare_ultra_writeblock( 45, blockdata)) {\r
562 if (MF_DBGLEVEL >= 1) Dbprintf("Write block error");\r
563 OnError(45);\r
564 return;\r
565 };\r
566\r
567 blockdata[0] = pwd[15];\r
568 blockdata[1] = pwd[14];\r
569 blockdata[2] = pwd[13];\r
570 blockdata[3] = pwd[12];\r
571 if(mifare_ultra_writeblock( 46, blockdata)) {\r
572 if (MF_DBGLEVEL >= 1) Dbprintf("Write block error");\r
573 OnError(46);\r
574 return;\r
575 };\r
576\r
577 blockdata[0] = pwd[11];\r
578 blockdata[1] = pwd[10];\r
579 blockdata[2] = pwd[9];\r
580 blockdata[3] = pwd[8];\r
581 if(mifare_ultra_writeblock( 47, blockdata)) {\r
582 if (MF_DBGLEVEL >= 1) Dbprintf("Write block error");\r
583 OnError(47);\r
584 return;\r
585 }; \r
586\r
587 if(mifare_ultra_halt()) {\r
588 if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");\r
589 OnError(0);\r
590 return;\r
591 };\r
592\r
593 cmd_send(CMD_ACK,1,0,0,0,0);\r
594 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
595 LEDsoff();\r
596 set_tracing(FALSE);\r
597}\r
598\r
599// Return 1 if the nonce is invalid else return 0\r
600int valid_nonce(uint32_t Nt, uint32_t NtEnc, uint32_t Ks1, uint8_t *parity) {\r
601 return ((oddparity8((Nt >> 24) & 0xFF) == ((parity[0]) ^ oddparity8((NtEnc >> 24) & 0xFF) ^ BIT(Ks1,16))) & \\r
602 (oddparity8((Nt >> 16) & 0xFF) == ((parity[1]) ^ oddparity8((NtEnc >> 16) & 0xFF) ^ BIT(Ks1,8))) & \\r
603 (oddparity8((Nt >> 8) & 0xFF) == ((parity[2]) ^ oddparity8((NtEnc >> 8) & 0xFF) ^ BIT(Ks1,0)))) ? 1 : 0;\r
604}\r
605\r
606\r
607//-----------------------------------------------------------------------------\r
608// acquire encrypted nonces in order to perform the attack described in\r
609// Carlo Meijer, Roel Verdult, "Ciphertext-only Cryptanalysis on Hardened\r
610// Mifare Classic Cards" in Proceedings of the 22nd ACM SIGSAC Conference on \r
611// Computer and Communications Security, 2015\r
612//-----------------------------------------------------------------------------\r
613#define AUTHENTICATION_TIMEOUT 848 //848 // card times out 1ms after wrong authentication (according to NXP documentation)\r
614#define PRE_AUTHENTICATION_LEADTIME 400 // some (non standard) cards need a pause after select before they are ready for first authentication \r
615\r
616void MifareAcquireEncryptedNonces(uint32_t arg0, uint32_t arg1, uint32_t flags, uint8_t *datain)\r
617{\r
618 uint64_t ui64Key = 0;\r
619 uint8_t uid[10] = {0x00};\r
620 uint32_t cuid = 0;\r
621 uint8_t cascade_levels = 0;\r
622 struct Crypto1State mpcs = {0, 0};\r
623 struct Crypto1State *pcs;\r
624 pcs = &mpcs;\r
625 uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE] = {0x00};\r
626 int16_t isOK = 0;\r
627 uint8_t par_enc[1] = {0x00};\r
628 uint8_t nt_par_enc = 0;\r
629 uint8_t buf[USB_CMD_DATA_SIZE] = {0x00};\r
630 uint32_t timeout = 0;\r
631 \r
632 uint8_t blockNo = arg0 & 0xff;\r
633 uint8_t keyType = (arg0 >> 8) & 0xff;\r
634 uint8_t targetBlockNo = arg1 & 0xff;\r
635 uint8_t targetKeyType = (arg1 >> 8) & 0xff;\r
636 ui64Key = bytes_to_num(datain, 6);\r
637 bool initialize = flags & 0x0001;\r
638 bool slow = flags & 0x0002;\r
639 bool field_off = flags & 0x0004;\r
640 \r
641 LED_A_ON();\r
642 LED_C_OFF();\r
643\r
644 if (initialize) {\r
645 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r
646 clear_trace();\r
647 set_tracing(FALSE);\r
648 }\r
649 LED_C_ON();\r
650 \r
651 uint16_t num_nonces = 0;\r
652 bool have_uid = false;\r
653 for (uint16_t i = 0; i <= USB_CMD_DATA_SIZE - 9; ) {\r
654\r
655 // Test if the action was cancelled\r
656 if(BUTTON_PRESS()) {\r
657 isOK = 2;\r
658 field_off = true;\r
659 break;\r
660 }\r
661\r
662 if (!have_uid) { // need a full select cycle to get the uid first\r
663 iso14a_card_select_t card_info; \r
664 if(!iso14443a_select_card(uid, &card_info, &cuid, true, 0)) {\r
665 if (MF_DBGLEVEL >= 1) Dbprintf("AcquireNonces: Can't select card (ALL)");\r
666 continue;\r
667 }\r
668 switch (card_info.uidlen) {\r
669 case 4 : cascade_levels = 1; break;\r
670 case 7 : cascade_levels = 2; break;\r
671 case 10: cascade_levels = 3; break;\r
672 default: break;\r
673 }\r
674 have_uid = true; \r
675 } else { // no need for anticollision. We can directly select the card\r
676 if(!iso14443a_select_card(uid, NULL, NULL, false, cascade_levels)) {\r
677 if (MF_DBGLEVEL >= 1) Dbprintf("AcquireNonces: Can't select card (UID)");\r
678 continue;\r
679 }\r
680 }\r
681 \r
682 if (slow) {\r
683 timeout = GetCountSspClk() + PRE_AUTHENTICATION_LEADTIME;\r
684 while(GetCountSspClk() < timeout);\r
685 }\r
686\r
687 uint32_t nt1;\r
688 if (mifare_classic_authex(pcs, cuid, blockNo, keyType, ui64Key, AUTH_FIRST, &nt1, NULL)) {\r
689 if (MF_DBGLEVEL >= 1) Dbprintf("AcquireNonces: Auth1 error");\r
690 continue;\r
691 }\r
692\r
693 // nested authentication\r
694 uint16_t len = mifare_sendcmd_short(pcs, AUTH_NESTED, 0x60 + (targetKeyType & 0x01), targetBlockNo, receivedAnswer, par_enc, NULL);\r
695 if (len != 4) {\r
696 if (MF_DBGLEVEL >= 1) Dbprintf("AcquireNonces: Auth2 error len=%d", len);\r
697 continue;\r
698 }\r
699 \r
700 // send a dummy byte as reader response in order to trigger the cards authentication timeout\r
701 uint8_t dummy_answer = 0;\r
702 ReaderTransmit(&dummy_answer, 1, NULL);\r
703 timeout = GetCountSspClk() + AUTHENTICATION_TIMEOUT;\r
704 \r
705 num_nonces++;\r
706 if (num_nonces % 2) {\r
707 memcpy(buf+i, receivedAnswer, 4);\r
708 nt_par_enc = par_enc[0] & 0xf0;\r
709 } else {\r
710 nt_par_enc |= par_enc[0] >> 4;\r
711 memcpy(buf+i+4, receivedAnswer, 4);\r
712 memcpy(buf+i+8, &nt_par_enc, 1);\r
713 i += 9;\r
714 }\r
715 // wait for the card to become ready again\r
716 while(GetCountSspClk() < timeout); \r
717 }\r
718\r
719 LED_C_OFF();\r
720 crypto1_destroy(pcs); \r
721 LED_B_ON();\r
722 cmd_send(CMD_ACK, isOK, cuid, num_nonces, buf, sizeof(buf));\r
723 LED_B_OFF();\r
724\r
725 if (MF_DBGLEVEL >= 3) DbpString("AcquireEncryptedNonces finished");\r
726\r
727 if (field_off) {\r
728 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
729 LEDsoff();\r
730 //set_tracing(FALSE);\r
731 }\r
732}\r
733\r
734\r
735//-----------------------------------------------------------------------------\r
736// MIFARE nested authentication. \r
737// \r
738//-----------------------------------------------------------------------------\r
739void MifareNested(uint32_t arg0, uint32_t arg1, uint32_t calibrate, uint8_t *datain)\r
740{\r
741 // params\r
742 uint8_t blockNo = arg0 & 0xff;\r
743 uint8_t keyType = (arg0 >> 8) & 0xff;\r
744 uint8_t targetBlockNo = arg1 & 0xff;\r
745 uint8_t targetKeyType = (arg1 >> 8) & 0xff;\r
746 uint64_t ui64Key = 0;\r
747\r
748 ui64Key = bytes_to_num(datain, 6);\r
749 \r
750 // variables\r
751 uint16_t rtr, i, j, len;\r
752 uint16_t davg = 0;\r
753 static uint16_t dmin, dmax;\r
754 uint8_t uid[10] = {0x00};\r
755 uint32_t cuid = 0, nt1, nt2, nttmp, nttest, ks1;\r
756 uint8_t par[1] = {0x00};\r
757 uint32_t target_nt[2] = {0x00}, target_ks[2] = {0x00};\r
758 \r
759 uint8_t par_array[4] = {0x00};\r
760 uint16_t ncount = 0;\r
761 struct Crypto1State mpcs = {0, 0};\r
762 struct Crypto1State *pcs;\r
763 pcs = &mpcs;\r
764 uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE] = {0x00};\r
765\r
766 uint32_t auth1_time, auth2_time;\r
767 static uint16_t delta_time = 0;\r
768\r
769 LED_A_ON();\r
770 LED_C_OFF();\r
771 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r
772\r
773 // free eventually allocated BigBuf memory\r
774 BigBuf_free(); BigBuf_Clear_ext(false);\r
775 \r
776 if (calibrate) clear_trace();\r
777 set_tracing(true);\r
778\r
779 // statistics on nonce distance\r
780 int16_t isOK = 0;\r
781 #define NESTED_MAX_TRIES 12\r
782 uint16_t unsuccessfull_tries = 0;\r
783 if (calibrate) { // for first call only. Otherwise reuse previous calibration\r
784 LED_B_ON();\r
785 WDT_HIT();\r
786\r
787 davg = dmax = 0;\r
788 dmin = 2000;\r
789 delta_time = 0;\r
790 \r
791 for (rtr = 0; rtr < 17; rtr++) {\r
792\r
793 // Test if the action was cancelled\r
794 if(BUTTON_PRESS()) {\r
795 isOK = -2;\r
796 break;\r
797 }\r
798\r
799 // prepare next select. No need to power down the card.\r
800 if(mifare_classic_halt(pcs, cuid)) {\r
801 if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Halt error");\r
802 rtr--;\r
803 continue;\r
804 }\r
805\r
806 if(!iso14443a_select_card(uid, NULL, &cuid, true, 0)) {\r
807 if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Can't select card");\r
808 rtr--;\r
809 continue;\r
810 };\r
811\r
812 auth1_time = 0;\r
813 if(mifare_classic_authex(pcs, cuid, blockNo, keyType, ui64Key, AUTH_FIRST, &nt1, &auth1_time)) {\r
814 if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Auth1 error");\r
815 rtr--;\r
816 continue;\r
817 };\r
818 auth2_time = (delta_time) ? auth1_time + delta_time : 0;\r
819\r
820 if(mifare_classic_authex(pcs, cuid, blockNo, keyType, ui64Key, AUTH_NESTED, &nt2, &auth2_time)) {\r
821 if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Auth2 error");\r
822 rtr--;\r
823 continue;\r
824 };\r
825\r
826 nttmp = prng_successor(nt1, 100); //NXP Mifare is typical around 840,but for some unlicensed/compatible mifare card this can be 160\r
827 for (i = 101; i < 1200; i++) {\r
828 nttmp = prng_successor(nttmp, 1);\r
829 if (nttmp == nt2) break;\r
830 }\r
831\r
832 if (i != 1200) {\r
833 if (rtr != 0) {\r
834 davg += i;\r
835 dmin = MIN(dmin, i);\r
836 dmax = MAX(dmax, i);\r
837 }\r
838 else {\r
839 delta_time = auth2_time - auth1_time + 32; // allow some slack for proper timing\r
840 }\r
841 if (MF_DBGLEVEL >= 3) Dbprintf("Nested: calibrating... ntdist=%d", i);\r
842 } else {\r
843 unsuccessfull_tries++;\r
844 if (unsuccessfull_tries > NESTED_MAX_TRIES) { // card isn't vulnerable to nested attack (random numbers are not predictable)\r
845 isOK = -3;\r
846 }\r
847 }\r
848 }\r
849\r
850 davg = (davg + (rtr - 1)/2) / (rtr - 1);\r
851 \r
852 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
853\r
854 dmin = davg - 2;\r
855 dmax = davg + 2;\r
856 \r
857 LED_B_OFF();\r
858 }\r
859// ------------------------------------------------------------------------------------------------- \r
860 \r
861 LED_C_ON();\r
862\r
863 // get crypted nonces for target sector\r
864 for(i=0; i < 2 && !isOK; i++) { // look for exactly two different nonces\r
865\r
866 target_nt[i] = 0;\r
867 while(target_nt[i] == 0) { // continue until we have an unambiguous nonce\r
868 \r
869 // prepare next select. No need to power down the card.\r
870 if(mifare_classic_halt(pcs, cuid)) {\r
871 if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Halt error");\r
872 continue;\r
873 }\r
874\r
875 if(!iso14443a_select_card(uid, NULL, &cuid, true, 0)) {\r
876 if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Can't select card");\r
877 continue;\r
878 };\r
879 \r
880 auth1_time = 0;\r
881 if(mifare_classic_authex(pcs, cuid, blockNo, keyType, ui64Key, AUTH_FIRST, &nt1, &auth1_time)) {\r
882 if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Auth1 error");\r
883 continue;\r
884 };\r
885\r
886 // nested authentication\r
887 auth2_time = auth1_time + delta_time;\r
888\r
889 len = mifare_sendcmd_short(pcs, AUTH_NESTED, 0x60 + (targetKeyType & 0x01), targetBlockNo, receivedAnswer, par, &auth2_time);\r
890 if (len != 4) {\r
891 if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Auth2 error len=%d", len);\r
892 continue;\r
893 };\r
894 \r
895 nt2 = bytes_to_num(receivedAnswer, 4); \r
896 if (MF_DBGLEVEL >= 3) Dbprintf("Nonce#%d: Testing nt1=%08x nt2enc=%08x nt2par=%02x", i+1, nt1, nt2, par[0]);\r
897 \r
898 // Parity validity check\r
899// for (j = 0; j < 4; j++) {\r
900// par_array[j] = (oddparity8(receivedAnswer[j]) != ((par[0] >> (7-j)) & 0x01));\r
901// }\r
902 par_array[0] = (oddparity8(receivedAnswer[0]) != ((par[0] >> (7-0)) & 0x01));\r
903 par_array[1] = (oddparity8(receivedAnswer[1]) != ((par[0] >> (7-1)) & 0x01));\r
904 par_array[2] = (oddparity8(receivedAnswer[2]) != ((par[0] >> (7-2)) & 0x01));\r
905 par_array[3] = (oddparity8(receivedAnswer[3]) != ((par[0] >> (7-3)) & 0x01));\r
906 \r
907 ncount = 0;\r
908 nttest = prng_successor(nt1, dmin - 1);\r
909 for (j = dmin; j < dmax + 1; j++) {\r
910 nttest = prng_successor(nttest, 1);\r
911 ks1 = nt2 ^ nttest;\r
912\r
913 if (valid_nonce(nttest, nt2, ks1, par_array)){\r
914 if (ncount > 0) { // we are only interested in disambiguous nonces, try again\r
915 if (MF_DBGLEVEL >= 3) Dbprintf("Nonce#%d: dismissed (ambigous), ntdist=%d", i+1, j);\r
916 target_nt[i] = 0;\r
917 break;\r
918 }\r
919 target_nt[i] = nttest;\r
920 target_ks[i] = ks1;\r
921 ncount++;\r
922 if (i == 1 && target_nt[1] == target_nt[0]) { // we need two different nonces\r
923 target_nt[i] = 0;\r
924 if (MF_DBGLEVEL >= 3) Dbprintf("Nonce#2: dismissed (= nonce#1), ntdist=%d", j);\r
925 break;\r
926 }\r
927 if (MF_DBGLEVEL >= 3) Dbprintf("Nonce#%d: valid, ntdist=%d", i+1, j);\r
928 }\r
929 }\r
930 if (target_nt[i] == 0 && j == dmax+1 && MF_DBGLEVEL >= 3) Dbprintf("Nonce#%d: dismissed (all invalid)", i+1);\r
931 }\r
932 }\r
933\r
934 LED_C_OFF();\r
935 \r
936 crypto1_destroy(pcs);\r
937 \r
938 byte_t buf[4 + 4 * 4] = {0};\r
939 memcpy(buf, &cuid, 4);\r
940 memcpy(buf+4, &target_nt[0], 4);\r
941 memcpy(buf+8, &target_ks[0], 4);\r
942 memcpy(buf+12, &target_nt[1], 4);\r
943 memcpy(buf+16, &target_ks[1], 4);\r
944 \r
945 LED_B_ON();\r
946 cmd_send(CMD_ACK, isOK, 0, targetBlockNo + (targetKeyType * 0x100), buf, sizeof(buf));\r
947 LED_B_OFF();\r
948\r
949 if (MF_DBGLEVEL >= 3) DbpString("NESTED FINISHED");\r
950\r
951 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
952 LEDsoff();\r
953 set_tracing(FALSE);\r
954}\r
955\r
956//-----------------------------------------------------------------------------\r
957// MIFARE check keys. key count up to 85. \r
958// \r
959//-----------------------------------------------------------------------------\r
960void MifareChkKeys(uint16_t arg0, uint8_t arg1, uint8_t arg2, uint8_t *datain) {\r
961 uint8_t blockNo = arg0 & 0xff;\r
962 uint8_t keyType = (arg0 >> 8) & 0xff;\r
963 bool clearTrace = arg1;\r
964 uint8_t keyCount = arg2;\r
965 uint64_t ui64Key = 0;\r
966 \r
967 bool have_uid = FALSE;\r
968 uint8_t cascade_levels = 0;\r
969 uint32_t timeout = 0;\r
970 \r
971 int i;\r
972 byte_t isOK = 0;\r
973 uint8_t uid[10] = {0x00};\r
974 uint32_t cuid = 0;\r
975 struct Crypto1State mpcs = {0, 0};\r
976 struct Crypto1State *pcs;\r
977 pcs = &mpcs;\r
978 \r
979 // save old debuglevel, and tempory turn off dbg printing. speedissues.\r
980 int OLD_MF_DBGLEVEL = MF_DBGLEVEL; \r
981 MF_DBGLEVEL = MF_DBG_NONE;\r
982 \r
983 LEDsoff();\r
984 LED_A_ON();\r
985 \r
986 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r
987\r
988 if (clearTrace) \r
989 clear_trace();\r
990 \r
991 set_tracing(TRUE);\r
992 \r
993 for (i = 0; i < keyCount; ++i) {\r
994\r
995 //mifare_classic_halt(pcs, cuid);\r
996\r
997 // this part is from Piwi's faster nonce collecting part in Hardnested.\r
998 if (!have_uid) { // need a full select cycle to get the uid first\r
999 iso14a_card_select_t card_info; \r
1000 if(!iso14443a_select_card(uid, &card_info, &cuid, true, 0)) {\r
1001 if (MF_DBGLEVEL >= 1) Dbprintf("ChkKeys: Can't select card (ALL)");\r
1002 break;\r
1003 }\r
1004 switch (card_info.uidlen) {\r
1005 case 4 : cascade_levels = 1; break;\r
1006 case 7 : cascade_levels = 2; break;\r
1007 case 10: cascade_levels = 3; break;\r
1008 default: break;\r
1009 }\r
1010 have_uid = TRUE; \r
1011 } else { // no need for anticollision. We can directly select the card\r
1012 if(!iso14443a_select_card(uid, NULL, NULL, false, cascade_levels)) {\r
1013 if (MF_DBGLEVEL >= 1) Dbprintf("ChkKeys: Can't select card (UID)");\r
1014 continue;\r
1015 }\r
1016 }\r
1017 \r
1018 ui64Key = bytes_to_num(datain + i * 6, 6);\r
1019 \r
1020 if (mifare_classic_auth(pcs, cuid, blockNo, keyType, ui64Key, AUTH_FIRST)) {\r
1021\r
1022 uint8_t dummy_answer = 0;\r
1023 ReaderTransmit(&dummy_answer, 1, NULL);\r
1024 timeout = GetCountSspClk() + AUTHENTICATION_TIMEOUT;\r
1025 \r
1026 // wait for the card to become ready again\r
1027 while(GetCountSspClk() < timeout);\r
1028 \r
1029 continue;\r
1030 }\r
1031 isOK = 1;\r
1032 break;\r
1033 }\r
1034 \r
1035 LED_B_ON();\r
1036 cmd_send(CMD_ACK, isOK, 0, 0, datain + i * 6, 6);\r
1037\r
1038 // restore debug level\r
1039 MF_DBGLEVEL = OLD_MF_DBGLEVEL; \r
1040 \r
1041 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
1042 LEDsoff();\r
1043 set_tracing(FALSE);\r
1044 crypto1_destroy(pcs);\r
1045}\r
1046\r
1047//-----------------------------------------------------------------------------\r
1048// MIFARE commands set debug level\r
1049// \r
1050//-----------------------------------------------------------------------------\r
1051void MifareSetDbgLvl(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain){\r
1052 MF_DBGLEVEL = arg0;\r
1053 Dbprintf("Debug level: %d", MF_DBGLEVEL);\r
1054}\r
1055\r
1056//-----------------------------------------------------------------------------\r
1057// Work with emulator memory\r
1058// \r
1059// Note: we call FpgaDownloadAndGo(FPGA_BITSTREAM_HF) here although FPGA is not\r
1060// involved in dealing with emulator memory. But if it is called later, it might\r
1061// destroy the Emulator Memory.\r
1062//-----------------------------------------------------------------------------\r
1063\r
1064void MifareEMemClr(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain){\r
1065 FpgaDownloadAndGo(FPGA_BITSTREAM_HF);\r
1066 emlClearMem();\r
1067}\r
1068\r
1069void MifareEMemSet(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain){\r
1070 FpgaDownloadAndGo(FPGA_BITSTREAM_HF);\r
1071 if (arg2==0) arg2 = 16; // backwards compat... default bytewidth\r
1072 emlSetMem_xt(datain, arg0, arg1, arg2); // data, block num, blocks count, block byte width\r
1073}\r
1074\r
1075void MifareEMemGet(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain){\r
1076 FpgaDownloadAndGo(FPGA_BITSTREAM_HF);\r
1077 byte_t buf[USB_CMD_DATA_SIZE] = {0x00};\r
1078 emlGetMem(buf, arg0, arg1); // data, block num, blocks count (max 4)\r
1079\r
1080 LED_B_ON();\r
1081 cmd_send(CMD_ACK,arg0,arg1,0,buf,USB_CMD_DATA_SIZE);\r
1082 LED_B_OFF();\r
1083}\r
1084\r
1085//-----------------------------------------------------------------------------\r
1086// Load a card into the emulator memory\r
1087// \r
1088//-----------------------------------------------------------------------------\r
1089void MifareECardLoad(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain){\r
1090 uint8_t numSectors = arg0;\r
1091 uint8_t keyType = arg1;\r
1092 uint64_t ui64Key = 0;\r
1093 uint32_t cuid = 0;\r
1094 struct Crypto1State mpcs = {0, 0};\r
1095 struct Crypto1State *pcs;\r
1096 pcs = &mpcs;\r
1097\r
1098 // variables\r
1099 byte_t dataoutbuf[16] = {0x00};\r
1100 byte_t dataoutbuf2[16] = {0x00};\r
1101 uint8_t uid[10] = {0x00};\r
1102\r
1103 LED_A_ON();\r
1104 LED_B_OFF();\r
1105 LED_C_OFF();\r
1106 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r
1107 \r
1108 clear_trace();\r
1109 set_tracing(TRUE);\r
1110 \r
1111 bool isOK = true;\r
1112\r
1113 if(!iso14443a_select_card(uid, NULL, &cuid, true, 0)) {\r
1114 isOK = false;\r
1115 if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");\r
1116 }\r
1117 \r
1118 for (uint8_t sectorNo = 0; isOK && sectorNo < numSectors; sectorNo++) {\r
1119 ui64Key = emlGetKey(sectorNo, keyType);\r
1120 if (sectorNo == 0){\r
1121 if(isOK && mifare_classic_auth(pcs, cuid, FirstBlockOfSector(sectorNo), keyType, ui64Key, AUTH_FIRST)) {\r
1122 isOK = false;\r
1123 if (MF_DBGLEVEL >= 1) Dbprintf("Sector[%2d]. Auth error", sectorNo);\r
1124 break;\r
1125 }\r
1126 } else {\r
1127 if(isOK && mifare_classic_auth(pcs, cuid, FirstBlockOfSector(sectorNo), keyType, ui64Key, AUTH_NESTED)) {\r
1128 isOK = false;\r
1129 if (MF_DBGLEVEL >= 1) Dbprintf("Sector[%2d]. Auth nested error", sectorNo);\r
1130 break;\r
1131 }\r
1132 }\r
1133 \r
1134 for (uint8_t blockNo = 0; isOK && blockNo < NumBlocksPerSector(sectorNo); blockNo++) {\r
1135 if(isOK && mifare_classic_readblock(pcs, cuid, FirstBlockOfSector(sectorNo) + blockNo, dataoutbuf)) {\r
1136 isOK = false;\r
1137 if (MF_DBGLEVEL >= 1) Dbprintf("Error reading sector %2d block %2d", sectorNo, blockNo);\r
1138 break;\r
1139 }\r
1140 if (isOK) {\r
1141 if (blockNo < NumBlocksPerSector(sectorNo) - 1) {\r
1142 emlSetMem(dataoutbuf, FirstBlockOfSector(sectorNo) + blockNo, 1);\r
1143 } else { // sector trailer, keep the keys, set only the AC\r
1144 emlGetMem(dataoutbuf2, FirstBlockOfSector(sectorNo) + blockNo, 1);\r
1145 memcpy(&dataoutbuf2[6], &dataoutbuf[6], 4);\r
1146 emlSetMem(dataoutbuf2, FirstBlockOfSector(sectorNo) + blockNo, 1);\r
1147 }\r
1148 }\r
1149 }\r
1150\r
1151 }\r
1152\r
1153 if(mifare_classic_halt(pcs, cuid))\r
1154 if (MF_DBGLEVEL >= 1)\r
1155 Dbprintf("Halt error");\r
1156\r
1157 // ----------------------------- crypto1 destroy\r
1158 crypto1_destroy(pcs);\r
1159\r
1160 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
1161 LEDsoff();\r
1162 \r
1163 if (MF_DBGLEVEL >= 2) DbpString("EMUL FILL SECTORS FINISHED");\r
1164\r
1165 set_tracing(FALSE);\r
1166}\r
1167\r
1168\r
1169//-----------------------------------------------------------------------------\r
1170// Work with "magic Chinese" card (email him: ouyangweidaxian@live.cn)\r
1171// \r
1172// PARAMS - workFlags\r
1173// bit 0 - need get UID\r
1174// bit 1 - need wupC\r
1175// bit 2 - need HALT after sequence\r
1176// bit 3 - need turn on FPGA before sequence\r
1177// bit 4 - need turn off FPGA\r
1178// bit 5 - need to set datain instead of issuing USB reply (called via ARM for StandAloneMode14a)\r
1179// bit 6 - wipe tag.\r
1180//-----------------------------------------------------------------------------\r
1181// magic uid card generation 1 commands\r
1182uint8_t wupC1[] = { MIFARE_MAGICWUPC1 }; \r
1183uint8_t wupC2[] = { MIFARE_MAGICWUPC2 }; \r
1184uint8_t wipeC[] = { MIFARE_MAGICWIPEC }; \r
1185 \r
1186void MifareCSetBlock(uint32_t arg0, uint32_t arg1, uint8_t *datain){\r
1187 \r
1188 // params\r
1189 uint8_t workFlags = arg0;\r
1190 uint8_t blockNo = arg1;\r
1191 \r
1192 // variables\r
1193 bool isOK = false; //assume we will get an error\r
1194 uint8_t errormsg = 0x00;\r
1195 uint8_t uid[10] = {0x00};\r
1196 uint8_t data[18] = {0x00};\r
1197 uint32_t cuid = 0;\r
1198 \r
1199 uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE] = {0x00};\r
1200 uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE] = {0x00};\r
1201\r
1202 if (workFlags & MAGIC_INIT) {\r
1203 LED_A_ON();\r
1204 LED_B_OFF();\r
1205 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r
1206 clear_trace();\r
1207 set_tracing(TRUE);\r
1208 }\r
1209\r
1210 //loop doesn't loop just breaks out if error\r
1211 while (true) {\r
1212 // read UID and return to client with write\r
1213 if (workFlags & MAGIC_UID) {\r
1214 if(!iso14443a_select_card(uid, NULL, &cuid, true, 0)) {\r
1215 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Can't select card");\r
1216 errormsg = MAGIC_UID;\r
1217 }\r
1218 mifare_classic_halt_ex(NULL);\r
1219 break;\r
1220 }\r
1221 \r
1222 // wipe tag, fill it with zeros\r
1223 if (workFlags & MAGIC_WIPE){\r
1224 ReaderTransmitBitsPar(wupC1, 7, NULL, NULL);\r
1225 if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != 0x0a)) {\r
1226 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("wupC1 error");\r
1227 errormsg = MAGIC_WIPE;\r
1228 break;\r
1229 }\r
1230\r
1231 ReaderTransmit(wipeC, sizeof(wipeC), NULL);\r
1232 if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != 0x0a)) {\r
1233 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("wipeC error");\r
1234 errormsg = MAGIC_WIPE;\r
1235 break;\r
1236 }\r
1237\r
1238 mifare_classic_halt_ex(NULL);\r
1239 } \r
1240\r
1241 // write block\r
1242 if (workFlags & MAGIC_WUPC) {\r
1243 ReaderTransmitBitsPar(wupC1, 7, NULL, NULL);\r
1244 if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != 0x0a)) {\r
1245 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("wupC1 error");\r
1246 errormsg = MAGIC_WUPC;\r
1247 break;\r
1248 }\r
1249\r
1250 ReaderTransmit(wupC2, sizeof(wupC2), NULL);\r
1251 if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != 0x0a)) {\r
1252 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("wupC2 error");\r
1253 errormsg = MAGIC_WUPC;\r
1254 break;\r
1255 }\r
1256 }\r
1257\r
1258 if ((mifare_sendcmd_short(NULL, 0, ISO14443A_CMD_WRITEBLOCK, blockNo, receivedAnswer, receivedAnswerPar, NULL) != 1) || (receivedAnswer[0] != 0x0a)) {\r
1259 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("write block send command error");\r
1260 errormsg = 4;\r
1261 break;\r
1262 }\r
1263 \r
1264 memcpy(data, datain, 16);\r
1265 AppendCrc14443a(data, 16);\r
1266 \r
1267 ReaderTransmit(data, sizeof(data), NULL);\r
1268 if ((ReaderReceive(receivedAnswer, receivedAnswerPar) != 1) || (receivedAnswer[0] != 0x0a)) {\r
1269 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("write block send data error");\r
1270 errormsg = 0;\r
1271 break;\r
1272 } \r
1273 \r
1274 if (workFlags & MAGIC_OFF) \r
1275 mifare_classic_halt_ex(NULL);\r
1276 \r
1277 isOK = true;\r
1278 break;\r
1279\r
1280 } // end while \r
1281\r
1282 if (isOK )\r
1283 cmd_send(CMD_ACK,1,0,0,uid,sizeof(uid));\r
1284 else\r
1285 OnErrorMagic(errormsg);\r
1286\r
1287 if (workFlags & MAGIC_OFF)\r
1288 OnSuccessMagic();\r
1289}\r
1290\r
1291void MifareCGetBlock(uint32_t arg0, uint32_t arg1, uint8_t *datain){\r
1292 \r
1293 uint8_t workFlags = arg0;\r
1294 uint8_t blockNo = arg1;\r
1295 uint8_t errormsg = 0x00;\r
1296 bool isOK = false; //assume we will get an error\r
1297 \r
1298 // variables\r
1299 uint8_t data[MAX_MIFARE_FRAME_SIZE];\r
1300 uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE] = {0x00};\r
1301 uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE] = {0x00};\r
1302 \r
1303 memset(data, 0x00, sizeof(data));\r
1304 \r
1305 if (workFlags & MAGIC_INIT) {\r
1306 LED_A_ON();\r
1307 LED_B_OFF();\r
1308 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN); \r
1309 clear_trace();\r
1310 set_tracing(TRUE);\r
1311 }\r
1312\r
1313 //loop doesn't loop just breaks out if error or done\r
1314 while (true) {\r
1315 if (workFlags & MAGIC_WUPC) {\r
1316 ReaderTransmitBitsPar(wupC1, 7, NULL, NULL);\r
1317 if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != 0x0a)) {\r
1318 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("wupC1 error");\r
1319 errormsg = MAGIC_WUPC;\r
1320 break;\r
1321 }\r
1322\r
1323 ReaderTransmit(wupC2, sizeof(wupC2), NULL);\r
1324 if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != 0x0a)) {\r
1325 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("wupC2 error");\r
1326 errormsg = MAGIC_WUPC;\r
1327 break;\r
1328 }\r
1329 }\r
1330\r
1331 // read block \r
1332 if ((mifare_sendcmd_short(NULL, 0, ISO14443A_CMD_READBLOCK, blockNo, receivedAnswer, receivedAnswerPar, NULL) != 18)) {\r
1333 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("read block send command error");\r
1334 errormsg = 0;\r
1335 break;\r
1336 }\r
1337 \r
1338 memcpy(data, receivedAnswer, sizeof(data));\r
1339 \r
1340 // send HALT\r
1341 if (workFlags & MAGIC_HALT)\r
1342 mifare_classic_halt_ex(NULL);\r
1343\r
1344 isOK = true;\r
1345 break;\r
1346 }\r
1347 // if MAGIC_DATAIN, the data stays on device side.\r
1348 if (workFlags & MAGIC_DATAIN) {\r
1349 if (isOK)\r
1350 memcpy(datain, data, sizeof(data));\r
1351 } else {\r
1352 if (isOK) \r
1353 cmd_send(CMD_ACK,1,0,0,data,sizeof(data)); \r
1354 else \r
1355 OnErrorMagic(errormsg); \r
1356 }\r
1357 \r
1358 if (workFlags & MAGIC_OFF)\r
1359 OnSuccessMagic();\r
1360}\r
1361\r
1362void MifareCIdent(){\r
1363 \r
1364 // variables\r
1365 bool isOK = true; \r
1366 uint8_t receivedAnswer[1] = {0x00};\r
1367 uint8_t receivedAnswerPar[1] = {0x00};\r
1368\r
1369 ReaderTransmitBitsPar(wupC1, 7, NULL, NULL);\r
1370 if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != 0x0a)) {\r
1371 isOK = false;\r
1372 }\r
1373\r
1374 ReaderTransmit(wupC2, sizeof(wupC2), NULL);\r
1375 if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != 0x0a)) {\r
1376 isOK = false;\r
1377 }\r
1378\r
1379 // removed the if, since some magic tags misbehavies and send an answer to it.\r
1380 mifare_classic_halt(NULL, 0);\r
1381 cmd_send(CMD_ACK,isOK,0,0,0,0);\r
1382}\r
1383\r
1384void OnSuccessMagic(){\r
1385 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
1386 LEDsoff();\r
1387 set_tracing(FALSE); \r
1388}\r
1389void OnErrorMagic(uint8_t reason){\r
1390 // ACK, ISOK, reason,0,0,0\r
1391 cmd_send(CMD_ACK,0,reason,0,0,0);\r
1392 OnSuccessMagic();\r
1393}\r
1394//\r
1395// DESFIRE\r
1396//\r
1397void Mifare_DES_Auth1(uint8_t arg0, uint8_t *datain){\r
1398 byte_t dataout[12] = {0x00};\r
1399 uint8_t uid[10] = {0x00};\r
1400 uint32_t cuid = 0;\r
1401 \r
1402 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r
1403 clear_trace();\r
1404 set_tracing(true);\r
1405\r
1406 int len = iso14443a_select_card(uid, NULL, &cuid, true, 0);\r
1407 if(!len) {\r
1408 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Can't select card");\r
1409 OnError(1);\r
1410 return;\r
1411 };\r
1412\r
1413 if(mifare_desfire_des_auth1(cuid, dataout)){\r
1414 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Authentication part1: Fail.");\r
1415 OnError(4);\r
1416 return;\r
1417 }\r
1418\r
1419 if (MF_DBGLEVEL >= MF_DBG_EXTENDED) DbpString("AUTH 1 FINISHED");\r
1420 cmd_send(CMD_ACK, 1, cuid, 0, dataout, sizeof(dataout));\r
1421}\r
1422\r
1423void Mifare_DES_Auth2(uint32_t arg0, uint8_t *datain){\r
1424 uint32_t cuid = arg0;\r
1425 uint8_t key[16] = {0x00};\r
1426 byte_t dataout[12] = {0x00};\r
1427 byte_t isOK = 0;\r
1428 \r
1429 memcpy(key, datain, 16);\r
1430 \r
1431 isOK = mifare_desfire_des_auth2(cuid, key, dataout);\r
1432 \r
1433 if( isOK) {\r
1434 if (MF_DBGLEVEL >= MF_DBG_EXTENDED) Dbprintf("Authentication part2: Failed"); \r
1435 OnError(4);\r
1436 return;\r
1437 }\r
1438\r
1439 if (MF_DBGLEVEL >= MF_DBG_EXTENDED) DbpString("AUTH 2 FINISHED");\r
1440\r
1441 cmd_send(CMD_ACK, isOK, 0, 0, dataout, sizeof(dataout));\r
1442 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
1443 LEDsoff();\r
1444 set_tracing(FALSE);\r
1445}
Impressum, Datenschutz