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