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