]> git.zerfleddert.de Git - proxmark3-svn/blame_incremental - armsrc/mifarecmd.c
chg: unused .. but not for long
[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#define STD_SEARCH 1\r
960#define EXT_SEARCH 2\r
961\r
962 uint8_t blockNo = arg0 & 0xFF;\r
963 uint8_t keyType = (arg0 >> 8) & 0xFF;\r
964 //uint8_t searchType = (arg1 >> 8 ) & 0xFF;\r
965 bool clearTrace = arg1 & 0xFF;\r
966 uint8_t keyCount = arg2;\r
967 uint64_t ui64Key = 0;\r
968 \r
969 bool have_uid = FALSE;\r
970 uint8_t cascade_levels = 0;\r
971 uint32_t timeout = 0;\r
972 \r
973 int i;\r
974 byte_t isOK = 0;\r
975 uint8_t uid[10] = {0x00};\r
976 uint32_t cuid = 0;\r
977 struct Crypto1State mpcs = {0, 0};\r
978 struct Crypto1State *pcs;\r
979 pcs = &mpcs;\r
980 \r
981 // save old debuglevel, and tempory turn off dbg printing. speedissues.\r
982 int OLD_MF_DBGLEVEL = MF_DBGLEVEL; \r
983 MF_DBGLEVEL = MF_DBG_NONE;\r
984 \r
985 LEDsoff();\r
986 LED_A_ON();\r
987 \r
988 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r
989\r
990 if (clearTrace) \r
991 clear_trace();\r
992 \r
993 set_tracing(TRUE);\r
994 \r
995 for (i = 0; i < keyCount; ++i) {\r
996\r
997 //mifare_classic_halt(pcs, cuid);\r
998\r
999 // this part is from Piwi's faster nonce collecting part in Hardnested.\r
1000 if (!have_uid) { // need a full select cycle to get the uid first\r
1001 iso14a_card_select_t card_info; \r
1002 if(!iso14443a_select_card(uid, &card_info, &cuid, true, 0)) {\r
1003 if (MF_DBGLEVEL >= 1) Dbprintf("ChkKeys: Can't select card (ALL)");\r
1004 break;\r
1005 }\r
1006 switch (card_info.uidlen) {\r
1007 case 4 : cascade_levels = 1; break;\r
1008 case 7 : cascade_levels = 2; break;\r
1009 case 10: cascade_levels = 3; break;\r
1010 default: break;\r
1011 }\r
1012 have_uid = TRUE; \r
1013 } else { // no need for anticollision. We can directly select the card\r
1014 if(!iso14443a_select_card(uid, NULL, NULL, false, cascade_levels)) {\r
1015 if (MF_DBGLEVEL >= 1) Dbprintf("ChkKeys: Can't select card (UID)");\r
1016 continue;\r
1017 }\r
1018 }\r
1019 \r
1020 ui64Key = bytes_to_num(datain + i * 6, 6);\r
1021 \r
1022 if (mifare_classic_auth(pcs, cuid, blockNo, keyType, ui64Key, AUTH_FIRST)) {\r
1023\r
1024 uint8_t dummy_answer = 0;\r
1025 ReaderTransmit(&dummy_answer, 1, NULL);\r
1026 timeout = GetCountSspClk() + AUTHENTICATION_TIMEOUT;\r
1027 \r
1028 // wait for the card to become ready again\r
1029 while(GetCountSspClk() < timeout);\r
1030 \r
1031 continue;\r
1032 }\r
1033 \r
1034 // found a key.\r
1035 //\r
1036 //if ( searchType == EXT_SEARCH) {\r
1037 \r
1038 //}\r
1039 //else {\r
1040 isOK = 1;\r
1041 break;\r
1042 //}\r
1043 }\r
1044 \r
1045 LED_B_ON();\r
1046 cmd_send(CMD_ACK, isOK, 0, 0, datain + i * 6, 6);\r
1047\r
1048 // restore debug level\r
1049 MF_DBGLEVEL = OLD_MF_DBGLEVEL; \r
1050 \r
1051 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
1052 LEDsoff();\r
1053 set_tracing(FALSE);\r
1054 crypto1_destroy(pcs);\r
1055}\r
1056\r
1057//-----------------------------------------------------------------------------\r
1058// MIFARE commands set debug level\r
1059// \r
1060//-----------------------------------------------------------------------------\r
1061void MifareSetDbgLvl(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain){\r
1062 MF_DBGLEVEL = arg0;\r
1063 Dbprintf("Debug level: %d", MF_DBGLEVEL);\r
1064}\r
1065\r
1066//-----------------------------------------------------------------------------\r
1067// Work with emulator memory\r
1068// \r
1069// Note: we call FpgaDownloadAndGo(FPGA_BITSTREAM_HF) here although FPGA is not\r
1070// involved in dealing with emulator memory. But if it is called later, it might\r
1071// destroy the Emulator Memory.\r
1072//-----------------------------------------------------------------------------\r
1073\r
1074void MifareEMemClr(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain){\r
1075 FpgaDownloadAndGo(FPGA_BITSTREAM_HF);\r
1076 emlClearMem();\r
1077}\r
1078\r
1079void MifareEMemSet(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain){\r
1080 FpgaDownloadAndGo(FPGA_BITSTREAM_HF);\r
1081 if (arg2==0) arg2 = 16; // backwards compat... default bytewidth\r
1082 emlSetMem_xt(datain, arg0, arg1, arg2); // data, block num, blocks count, block byte width\r
1083}\r
1084\r
1085void MifareEMemGet(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain){\r
1086 FpgaDownloadAndGo(FPGA_BITSTREAM_HF);\r
1087 byte_t buf[USB_CMD_DATA_SIZE] = {0x00};\r
1088 emlGetMem(buf, arg0, arg1); // data, block num, blocks count (max 4)\r
1089\r
1090 LED_B_ON();\r
1091 cmd_send(CMD_ACK,arg0,arg1,0,buf,USB_CMD_DATA_SIZE);\r
1092 LED_B_OFF();\r
1093}\r
1094\r
1095//-----------------------------------------------------------------------------\r
1096// Load a card into the emulator memory\r
1097// \r
1098//-----------------------------------------------------------------------------\r
1099void MifareECardLoad(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain){\r
1100 uint8_t numSectors = arg0;\r
1101 uint8_t keyType = arg1;\r
1102 uint64_t ui64Key = 0;\r
1103 uint32_t cuid = 0;\r
1104 struct Crypto1State mpcs = {0, 0};\r
1105 struct Crypto1State *pcs;\r
1106 pcs = &mpcs;\r
1107\r
1108 // variables\r
1109 byte_t dataoutbuf[16] = {0x00};\r
1110 byte_t dataoutbuf2[16] = {0x00};\r
1111 uint8_t uid[10] = {0x00};\r
1112\r
1113 LED_A_ON();\r
1114 LED_B_OFF();\r
1115 LED_C_OFF();\r
1116 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r
1117 \r
1118 clear_trace();\r
1119 set_tracing(TRUE);\r
1120 \r
1121 bool isOK = true;\r
1122\r
1123 if(!iso14443a_select_card(uid, NULL, &cuid, true, 0)) {\r
1124 isOK = false;\r
1125 if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");\r
1126 }\r
1127 \r
1128 for (uint8_t sectorNo = 0; isOK && sectorNo < numSectors; sectorNo++) {\r
1129 ui64Key = emlGetKey(sectorNo, keyType);\r
1130 if (sectorNo == 0){\r
1131 if(isOK && mifare_classic_auth(pcs, cuid, FirstBlockOfSector(sectorNo), keyType, ui64Key, AUTH_FIRST)) {\r
1132 isOK = false;\r
1133 if (MF_DBGLEVEL >= 1) Dbprintf("Sector[%2d]. Auth error", sectorNo);\r
1134 break;\r
1135 }\r
1136 } else {\r
1137 if(isOK && mifare_classic_auth(pcs, cuid, FirstBlockOfSector(sectorNo), keyType, ui64Key, AUTH_NESTED)) {\r
1138 isOK = false;\r
1139 if (MF_DBGLEVEL >= 1) Dbprintf("Sector[%2d]. Auth nested error", sectorNo);\r
1140 break;\r
1141 }\r
1142 }\r
1143 \r
1144 for (uint8_t blockNo = 0; isOK && blockNo < NumBlocksPerSector(sectorNo); blockNo++) {\r
1145 if(isOK && mifare_classic_readblock(pcs, cuid, FirstBlockOfSector(sectorNo) + blockNo, dataoutbuf)) {\r
1146 isOK = false;\r
1147 if (MF_DBGLEVEL >= 1) Dbprintf("Error reading sector %2d block %2d", sectorNo, blockNo);\r
1148 break;\r
1149 }\r
1150 if (isOK) {\r
1151 if (blockNo < NumBlocksPerSector(sectorNo) - 1) {\r
1152 emlSetMem(dataoutbuf, FirstBlockOfSector(sectorNo) + blockNo, 1);\r
1153 } else { // sector trailer, keep the keys, set only the AC\r
1154 emlGetMem(dataoutbuf2, FirstBlockOfSector(sectorNo) + blockNo, 1);\r
1155 memcpy(&dataoutbuf2[6], &dataoutbuf[6], 4);\r
1156 emlSetMem(dataoutbuf2, FirstBlockOfSector(sectorNo) + blockNo, 1);\r
1157 }\r
1158 }\r
1159 }\r
1160\r
1161 }\r
1162\r
1163 if(mifare_classic_halt(pcs, cuid))\r
1164 if (MF_DBGLEVEL >= 1)\r
1165 Dbprintf("Halt error");\r
1166\r
1167 // ----------------------------- crypto1 destroy\r
1168 crypto1_destroy(pcs);\r
1169\r
1170 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
1171 LEDsoff();\r
1172 \r
1173 if (MF_DBGLEVEL >= 2) DbpString("EMUL FILL SECTORS FINISHED");\r
1174\r
1175 set_tracing(FALSE);\r
1176}\r
1177\r
1178\r
1179//-----------------------------------------------------------------------------\r
1180// Work with "magic Chinese" card (email him: ouyangweidaxian@live.cn)\r
1181// \r
1182// PARAMS - workFlags\r
1183// bit 0 - need get UID\r
1184// bit 1 - need wupC\r
1185// bit 2 - need HALT after sequence\r
1186// bit 3 - need turn on FPGA before sequence\r
1187// bit 4 - need turn off FPGA\r
1188// bit 5 - need to set datain instead of issuing USB reply (called via ARM for StandAloneMode14a)\r
1189// bit 6 - wipe tag.\r
1190//-----------------------------------------------------------------------------\r
1191// magic uid card generation 1 commands\r
1192uint8_t wupC1[] = { MIFARE_MAGICWUPC1 }; \r
1193uint8_t wupC2[] = { MIFARE_MAGICWUPC2 }; \r
1194uint8_t wipeC[] = { MIFARE_MAGICWIPEC }; \r
1195 \r
1196void MifareCSetBlock(uint32_t arg0, uint32_t arg1, uint8_t *datain){\r
1197 \r
1198 // params\r
1199 uint8_t workFlags = arg0;\r
1200 uint8_t blockNo = arg1;\r
1201 \r
1202 // variables\r
1203 bool isOK = false; //assume we will get an error\r
1204 uint8_t errormsg = 0x00;\r
1205 uint8_t uid[10] = {0x00};\r
1206 uint8_t data[18] = {0x00};\r
1207 uint32_t cuid = 0;\r
1208 \r
1209 uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE] = {0x00};\r
1210 uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE] = {0x00};\r
1211\r
1212 if (workFlags & MAGIC_INIT) {\r
1213 LED_A_ON();\r
1214 LED_B_OFF();\r
1215 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r
1216 clear_trace();\r
1217 set_tracing(TRUE);\r
1218 }\r
1219\r
1220 //loop doesn't loop just breaks out if error\r
1221 while (true) {\r
1222 // read UID and return to client with write\r
1223 if (workFlags & MAGIC_UID) {\r
1224 if(!iso14443a_select_card(uid, NULL, &cuid, true, 0)) {\r
1225 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Can't select card");\r
1226 errormsg = MAGIC_UID;\r
1227 }\r
1228 mifare_classic_halt_ex(NULL);\r
1229 break;\r
1230 }\r
1231 \r
1232 // wipe tag, fill it with zeros\r
1233 if (workFlags & MAGIC_WIPE){\r
1234 ReaderTransmitBitsPar(wupC1, 7, NULL, NULL);\r
1235 if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != 0x0a)) {\r
1236 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("wupC1 error");\r
1237 errormsg = MAGIC_WIPE;\r
1238 break;\r
1239 }\r
1240\r
1241 ReaderTransmit(wipeC, sizeof(wipeC), NULL);\r
1242 if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != 0x0a)) {\r
1243 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("wipeC error");\r
1244 errormsg = MAGIC_WIPE;\r
1245 break;\r
1246 }\r
1247\r
1248 mifare_classic_halt_ex(NULL);\r
1249 } \r
1250\r
1251 // write block\r
1252 if (workFlags & MAGIC_WUPC) {\r
1253 ReaderTransmitBitsPar(wupC1, 7, NULL, NULL);\r
1254 if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != 0x0a)) {\r
1255 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("wupC1 error");\r
1256 errormsg = MAGIC_WUPC;\r
1257 break;\r
1258 }\r
1259\r
1260 ReaderTransmit(wupC2, sizeof(wupC2), NULL);\r
1261 if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != 0x0a)) {\r
1262 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("wupC2 error");\r
1263 errormsg = MAGIC_WUPC;\r
1264 break;\r
1265 }\r
1266 }\r
1267\r
1268 if ((mifare_sendcmd_short(NULL, 0, ISO14443A_CMD_WRITEBLOCK, blockNo, receivedAnswer, receivedAnswerPar, NULL) != 1) || (receivedAnswer[0] != 0x0a)) {\r
1269 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("write block send command error");\r
1270 errormsg = 4;\r
1271 break;\r
1272 }\r
1273 \r
1274 memcpy(data, datain, 16);\r
1275 AppendCrc14443a(data, 16);\r
1276 \r
1277 ReaderTransmit(data, sizeof(data), NULL);\r
1278 if ((ReaderReceive(receivedAnswer, receivedAnswerPar) != 1) || (receivedAnswer[0] != 0x0a)) {\r
1279 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("write block send data error");\r
1280 errormsg = 0;\r
1281 break;\r
1282 } \r
1283 \r
1284 if (workFlags & MAGIC_OFF) \r
1285 mifare_classic_halt_ex(NULL);\r
1286 \r
1287 isOK = true;\r
1288 break;\r
1289\r
1290 } // end while \r
1291\r
1292 if (isOK )\r
1293 cmd_send(CMD_ACK,1,0,0,uid,sizeof(uid));\r
1294 else\r
1295 OnErrorMagic(errormsg);\r
1296\r
1297 if (workFlags & MAGIC_OFF)\r
1298 OnSuccessMagic();\r
1299}\r
1300\r
1301void MifareCGetBlock(uint32_t arg0, uint32_t arg1, uint8_t *datain){\r
1302 \r
1303 uint8_t workFlags = arg0;\r
1304 uint8_t blockNo = arg1;\r
1305 uint8_t errormsg = 0x00;\r
1306 bool isOK = false; //assume we will get an error\r
1307 \r
1308 // variables\r
1309 uint8_t data[MAX_MIFARE_FRAME_SIZE];\r
1310 uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE] = {0x00};\r
1311 uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE] = {0x00};\r
1312 \r
1313 memset(data, 0x00, sizeof(data));\r
1314 \r
1315 if (workFlags & MAGIC_INIT) {\r
1316 LED_A_ON();\r
1317 LED_B_OFF();\r
1318 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN); \r
1319 clear_trace();\r
1320 set_tracing(TRUE);\r
1321 }\r
1322\r
1323 //loop doesn't loop just breaks out if error or done\r
1324 while (true) {\r
1325 if (workFlags & MAGIC_WUPC) {\r
1326 ReaderTransmitBitsPar(wupC1, 7, NULL, NULL);\r
1327 if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != 0x0a)) {\r
1328 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("wupC1 error");\r
1329 errormsg = MAGIC_WUPC;\r
1330 break;\r
1331 }\r
1332\r
1333 ReaderTransmit(wupC2, sizeof(wupC2), NULL);\r
1334 if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != 0x0a)) {\r
1335 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("wupC2 error");\r
1336 errormsg = MAGIC_WUPC;\r
1337 break;\r
1338 }\r
1339 }\r
1340\r
1341 // read block \r
1342 if ((mifare_sendcmd_short(NULL, 0, ISO14443A_CMD_READBLOCK, blockNo, receivedAnswer, receivedAnswerPar, NULL) != 18)) {\r
1343 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("read block send command error");\r
1344 errormsg = 0;\r
1345 break;\r
1346 }\r
1347 \r
1348 memcpy(data, receivedAnswer, sizeof(data));\r
1349 \r
1350 // send HALT\r
1351 if (workFlags & MAGIC_HALT)\r
1352 mifare_classic_halt_ex(NULL);\r
1353\r
1354 isOK = true;\r
1355 break;\r
1356 }\r
1357 // if MAGIC_DATAIN, the data stays on device side.\r
1358 if (workFlags & MAGIC_DATAIN) {\r
1359 if (isOK)\r
1360 memcpy(datain, data, sizeof(data));\r
1361 } else {\r
1362 if (isOK) \r
1363 cmd_send(CMD_ACK,1,0,0,data,sizeof(data)); \r
1364 else \r
1365 OnErrorMagic(errormsg); \r
1366 }\r
1367 \r
1368 if (workFlags & MAGIC_OFF)\r
1369 OnSuccessMagic();\r
1370}\r
1371\r
1372void MifareCIdent(){\r
1373 #define GEN_1A 1\r
1374 #define GEN_1B 2\r
1375 #define GEN_2 4\r
1376 // variables\r
1377 uint8_t isGen = 0;\r
1378 uint8_t rec[1] = {0x00};\r
1379 uint8_t recpar[1] = {0x00};\r
1380 \r
1381 // Generation 1 test\r
1382 ReaderTransmitBitsPar(wupC1, 7, NULL, NULL);\r
1383 if(!ReaderReceive(rec, recpar) || (rec[0] != 0x0a)) {\r
1384 goto TEST2;\r
1385 };\r
1386 isGen = GEN_1B;\r
1387 \r
1388 ReaderTransmit(wupC2, sizeof(wupC2), NULL);\r
1389 if(!ReaderReceive(rec, recpar) || (rec[0] != 0x0a)) {\r
1390 goto OUT;\r
1391 }; \r
1392 isGen = GEN_1A;\r
1393 goto OUT;\r
1394\r
1395TEST2:;\r
1396/*\r
1397 // Generation 2 test\r
1398 struct Crypto1State mpcs = {0, 0};\r
1399 struct Crypto1State *pcs = &mpcs;\r
1400\r
1401 // halt previous.\r
1402 mifare_classic_halt(NULL, 0);\r
1403 \r
1404 //select\r
1405 if (!iso14443a_select_card(NULL, NULL, NULL, true, 0)) {\r
1406 goto OUT;\r
1407 };\r
1408 \r
1409 // MIFARE_CLASSIC_WRITEBLOCK 0xA0\r
1410 // ACK 0x0a\r
1411 uint16_t len = mifare_sendcmd_short(pcs, 1, 0xA0, 0, rec, recpar, NULL);\r
1412 if ((len != 1) || (rec[0] != 0x0A)) { \r
1413 isGen = GEN_2; \r
1414 };\r
1415 */\r
1416OUT:;\r
1417 // removed the if, since some magic tags misbehavies and send an answer to it.\r
1418 mifare_classic_halt(NULL, 0);\r
1419 cmd_send(CMD_ACK,isGen, 0, 0, 0, 0);\r
1420}\r
1421\r
1422void OnSuccessMagic(){\r
1423 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
1424 LEDsoff();\r
1425 set_tracing(FALSE); \r
1426}\r
1427void OnErrorMagic(uint8_t reason){\r
1428 // ACK, ISOK, reason,0,0,0\r
1429 cmd_send(CMD_ACK,0,reason,0,0,0);\r
1430 OnSuccessMagic();\r
1431}\r
1432//\r
1433// DESFIRE\r
1434//\r
1435void Mifare_DES_Auth1(uint8_t arg0, uint8_t *datain){\r
1436 byte_t dataout[12] = {0x00};\r
1437 uint8_t uid[10] = {0x00};\r
1438 uint32_t cuid = 0;\r
1439 \r
1440 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r
1441 clear_trace();\r
1442 set_tracing(true);\r
1443\r
1444 int len = iso14443a_select_card(uid, NULL, &cuid, true, 0);\r
1445 if(!len) {\r
1446 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Can't select card");\r
1447 OnError(1);\r
1448 return;\r
1449 };\r
1450\r
1451 if(mifare_desfire_des_auth1(cuid, dataout)){\r
1452 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Authentication part1: Fail.");\r
1453 OnError(4);\r
1454 return;\r
1455 }\r
1456\r
1457 if (MF_DBGLEVEL >= MF_DBG_EXTENDED) DbpString("AUTH 1 FINISHED");\r
1458 cmd_send(CMD_ACK, 1, cuid, 0, dataout, sizeof(dataout));\r
1459}\r
1460\r
1461void Mifare_DES_Auth2(uint32_t arg0, uint8_t *datain){\r
1462 uint32_t cuid = arg0;\r
1463 uint8_t key[16] = {0x00};\r
1464 byte_t dataout[12] = {0x00};\r
1465 byte_t isOK = 0;\r
1466 \r
1467 memcpy(key, datain, 16);\r
1468 \r
1469 isOK = mifare_desfire_des_auth2(cuid, key, dataout);\r
1470 \r
1471 if( isOK) {\r
1472 if (MF_DBGLEVEL >= MF_DBG_EXTENDED) Dbprintf("Authentication part2: Failed"); \r
1473 OnError(4);\r
1474 return;\r
1475 }\r
1476\r
1477 if (MF_DBGLEVEL >= MF_DBG_EXTENDED) DbpString("AUTH 2 FINISHED");\r
1478\r
1479 cmd_send(CMD_ACK, isOK, 0, 0, dataout, sizeof(dataout));\r
1480 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
1481 LEDsoff();\r
1482 set_tracing(FALSE);\r
1483}
Impressum, Datenschutz