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