]> git.zerfleddert.de Git - proxmark3-svn/blame_incremental - armsrc/mifarecmd.c
MFU info adjustments
[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\r
20#include "des.h"\r
21#include "crc.h"\r
22\r
23// the block number for the ISO14443-4 PCB\r
24uint8_t pcb_blocknum = 0;\r
25// Deselect card by sending a s-block. the crc is precalced for speed\r
26static uint8_t deselect_cmd[] = {0xc2,0xe0,0xb4};\r
27\r
28\r
29//-----------------------------------------------------------------------------\r
30// Select, Authenticate, Read a MIFARE tag. \r
31// read block\r
32//-----------------------------------------------------------------------------\r
33void MifareReadBlock(uint8_t arg0, uint8_t arg1, uint8_t arg2, uint8_t *datain)\r
34{\r
35 // params\r
36 uint8_t blockNo = arg0;\r
37 uint8_t keyType = arg1;\r
38 uint64_t ui64Key = 0;\r
39 ui64Key = bytes_to_num(datain, 6);\r
40 \r
41 // variables\r
42 byte_t isOK = 0;\r
43 byte_t dataoutbuf[16];\r
44 uint8_t uid[10];\r
45 uint32_t cuid;\r
46 struct Crypto1State mpcs = {0, 0};\r
47 struct Crypto1State *pcs;\r
48 pcs = &mpcs;\r
49\r
50 // clear trace\r
51 clear_trace();\r
52 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r
53\r
54 LED_A_ON();\r
55 LED_B_OFF();\r
56 LED_C_OFF();\r
57\r
58 while (true) {\r
59 if(!iso14443a_select_card(uid, NULL, &cuid)) {\r
60 if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");\r
61 break;\r
62 };\r
63\r
64 if(mifare_classic_auth(pcs, cuid, blockNo, keyType, ui64Key, AUTH_FIRST)) {\r
65 if (MF_DBGLEVEL >= 1) Dbprintf("Auth error");\r
66 break;\r
67 };\r
68 \r
69 if(mifare_classic_readblock(pcs, cuid, blockNo, dataoutbuf)) {\r
70 if (MF_DBGLEVEL >= 1) Dbprintf("Read block error");\r
71 break;\r
72 };\r
73\r
74 if(mifare_classic_halt(pcs, cuid)) {\r
75 if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");\r
76 break;\r
77 };\r
78 \r
79 isOK = 1;\r
80 break;\r
81 }\r
82 \r
83 // ----------------------------- crypto1 destroy\r
84 crypto1_destroy(pcs);\r
85 \r
86 if (MF_DBGLEVEL >= 2) DbpString("READ BLOCK FINISHED");\r
87\r
88 LED_B_ON();\r
89 cmd_send(CMD_ACK,isOK,0,0,dataoutbuf,16);\r
90 LED_B_OFF();\r
91\r
92 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
93 LEDsoff();\r
94}\r
95\r
96void MifareUC_Auth1(uint8_t arg0, uint8_t *datain){\r
97\r
98 byte_t dataoutbuf[16] = {0x00};\r
99 uint8_t uid[10] = {0x00};\r
100 uint32_t cuid = 0x00;\r
101\r
102 LED_A_ON(); LED_B_OFF(); LED_C_OFF();\r
103 \r
104 clear_trace();\r
105 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r
106\r
107 if(!iso14443a_select_card(uid, NULL, &cuid)) {\r
108 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Can't select card");\r
109 OnError(0);\r
110 return;\r
111 };\r
112 \r
113 if(mifare_ultra_auth1(dataoutbuf)){\r
114 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Authentication part1: Fail.");\r
115 OnError(1);\r
116 return;\r
117 }\r
118\r
119 if (MF_DBGLEVEL >= MF_DBG_EXTENDED) DbpString("AUTH 1 FINISHED");\r
120 \r
121 cmd_send(CMD_ACK,1,cuid,0,dataoutbuf,11);\r
122 LEDsoff();\r
123}\r
124void MifareUC_Auth2(uint32_t arg0, uint8_t *datain){\r
125\r
126 uint8_t key[16] = {0x00};\r
127 byte_t dataoutbuf[16] = {0x00};\r
128 \r
129 memcpy(key, datain, 16);\r
130 \r
131 LED_A_ON(); LED_B_OFF(); LED_C_OFF();\r
132 \r
133 if(mifare_ultra_auth2(key, dataoutbuf)){\r
134 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Authentication part2: Fail...");\r
135 OnError(1);\r
136 return; \r
137 }\r
138 \r
139 if (MF_DBGLEVEL >= MF_DBG_EXTENDED) DbpString("AUTH 2 FINISHED");\r
140 \r
141 cmd_send(CMD_ACK,1,0,0,dataoutbuf,11);\r
142 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
143 LEDsoff();\r
144}\r
145\r
146// Arg0 = BlockNo,\r
147// Arg1 = UsePwd bool\r
148// datain = PWD bytes,\r
149void MifareUReadBlock(uint8_t arg0, uint8_t arg1, uint8_t *datain)\r
150{\r
151 uint8_t blockNo = arg0;\r
152 byte_t dataout[16] = {0x00};\r
153 uint8_t uid[10] = {0x00};\r
154 uint8_t key[16] = {0x00};\r
155 bool usePwd = (arg1 == 1);\r
156\r
157 LEDsoff();\r
158 LED_A_ON();\r
159 clear_trace();\r
160 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r
161\r
162 int len = iso14443a_select_card(uid, NULL, NULL);\r
163 if(!len) {\r
164 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Can't select card (RC:%02X)",len);\r
165 OnError(1);\r
166 return;\r
167 }\r
168\r
169 // authenticate here.\r
170 if ( usePwd ) {\r
171\r
172 memcpy(key, datain, 16);\r
173\r
174 // Dbprintf("KEY: %02x %02x %02x %02x %02x %02x %02x %02x", key[0],key[1],key[2],key[3],key[4],key[5],key[6],key[7] );\r
175 // Dbprintf("KEY: %02x %02x %02x %02x %02x %02x %02x %02x", key[8],key[9],key[10],key[11],key[12],key[13],key[14],key[15] );\r
176\r
177 uint8_t random_a[8] = {1,1,1,1,1,1,1,1 };\r
178 uint8_t random_b[8] = {0x00};\r
179 uint8_t enc_random_b[8] = {0x00};\r
180 uint8_t rnd_ab[16] = {0x00};\r
181 uint8_t IV[8] = {0x00};\r
182\r
183 uint16_t len;\r
184 uint8_t receivedAnswer[MAX_FRAME_SIZE];\r
185 uint8_t receivedAnswerPar[MAX_PARITY_SIZE];\r
186\r
187 len = mifare_sendcmd_short(NULL, 1, 0x1A, 0x00, receivedAnswer,receivedAnswerPar ,NULL);\r
188 if (len != 11) {\r
189 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Cmd Error: %02x", receivedAnswer[0]);\r
190 OnError(1);\r
191 return;\r
192 }\r
193 \r
194 // tag nonce.\r
195 memcpy(enc_random_b,receivedAnswer+1,8);\r
196\r
197 // decrypt nonce.\r
198 tdes_2key_dec(random_b, enc_random_b, sizeof(random_b), key, IV );\r
199 rol(random_b,8);\r
200 memcpy(rnd_ab ,random_a,8);\r
201 memcpy(rnd_ab+8,random_b,8);\r
202\r
203 if (MF_DBGLEVEL >= MF_DBG_EXTENDED) {\r
204 Dbprintf("enc_B: %02x %02x %02x %02x %02x %02x %02x %02x",\r
205 enc_random_b[0],enc_random_b[1],enc_random_b[2],enc_random_b[3],\r
206 enc_random_b[4],enc_random_b[5],enc_random_b[6],enc_random_b[7]);\r
207 \r
208 Dbprintf(" B: %02x %02x %02x %02x %02x %02x %02x %02x",\r
209 random_b[0],random_b[1],random_b[2],random_b[3],\r
210 random_b[4],random_b[5],random_b[6],random_b[7]);\r
211\r
212 Dbprintf("rnd_ab: %02x %02x %02x %02x %02x %02x %02x %02x",\r
213 rnd_ab[0],rnd_ab[1],rnd_ab[2],rnd_ab[3],\r
214 rnd_ab[4],rnd_ab[5],rnd_ab[6],rnd_ab[7]);\r
215 \r
216 Dbprintf("rnd_ab: %02x %02x %02x %02x %02x %02x %02x %02x",\r
217 rnd_ab[8],rnd_ab[9],rnd_ab[10],rnd_ab[11],\r
218 rnd_ab[12],rnd_ab[13],rnd_ab[14],rnd_ab[15] );\r
219 }\r
220 \r
221 // encrypt out, in, length, key, iv\r
222 tdes_2key_enc(rnd_ab, rnd_ab, sizeof(rnd_ab), key, enc_random_b);\r
223\r
224 len = mifare_sendcmd_short_mfucauth(NULL, 1, 0xAF, rnd_ab, receivedAnswer, receivedAnswerPar, NULL);\r
225 if (len != 11) {\r
226 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Cmd Error: %02x", receivedAnswer[0]);\r
227 OnError(1);\r
228 return;\r
229 }\r
230\r
231 uint8_t enc_resp[8] = { 0 };\r
232 uint8_t resp_random_a[8] = { 0 };\r
233 memcpy(enc_resp, receivedAnswer+1, 8);\r
234 \r
235 // decrypt out, in, length, key, iv \r
236 tdes_2key_dec(resp_random_a, enc_resp, 8, key, enc_random_b);\r
237 if ( memcmp(resp_random_a, random_a, 8) != 0 )\r
238 Dbprintf("failed authentication");\r
239\r
240 if (MF_DBGLEVEL >= MF_DBG_EXTENDED) {\r
241 Dbprintf("e_AB: %02x %02x %02x %02x %02x %02x %02x %02x", \r
242 rnd_ab[0],rnd_ab[1],rnd_ab[2],rnd_ab[3],\r
243 rnd_ab[4],rnd_ab[5],rnd_ab[6],rnd_ab[7]);\r
244\r
245 Dbprintf("e_AB: %02x %02x %02x %02x %02x %02x %02x %02x",\r
246 rnd_ab[8],rnd_ab[9],rnd_ab[10],rnd_ab[11],\r
247 rnd_ab[12],rnd_ab[13],rnd_ab[14],rnd_ab[15]);\r
248\r
249 Dbprintf("a: %02x %02x %02x %02x %02x %02x %02x %02x",\r
250 random_a[0],random_a[1],random_a[2],random_a[3],\r
251 random_a[4],random_a[5],random_a[6],random_a[7]);\r
252 \r
253 Dbprintf("b: %02x %02x %02x %02x %02x %02x %02x %02x",\r
254 resp_random_a[0],resp_random_a[1],resp_random_a[2],resp_random_a[3],\r
255 resp_random_a[4],resp_random_a[5],resp_random_a[6],resp_random_a[7]);\r
256 }\r
257 }\r
258 \r
259 if( mifare_ultra_readblock(blockNo, dataout) ) {\r
260 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Read block error");\r
261 OnError(2);\r
262 return;\r
263 }\r
264 \r
265 if( mifare_ultra_halt() ) {\r
266 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Halt error");\r
267 OnError(3);\r
268 return;\r
269 }\r
270 \r
271 cmd_send(CMD_ACK,1,0,0,dataout,16);\r
272 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
273 LEDsoff();\r
274}\r
275\r
276//-----------------------------------------------------------------------------\r
277// Select, Authenticate, Read a MIFARE tag. \r
278// read sector (data = 4 x 16 bytes = 64 bytes, or 16 x 16 bytes = 256 bytes)\r
279//-----------------------------------------------------------------------------\r
280void MifareReadSector(uint8_t arg0, uint8_t arg1, uint8_t arg2, uint8_t *datain)\r
281{\r
282 // params\r
283 uint8_t sectorNo = arg0;\r
284 uint8_t keyType = arg1;\r
285 uint64_t ui64Key = 0;\r
286 ui64Key = bytes_to_num(datain, 6);\r
287 \r
288 // variables\r
289 byte_t isOK = 0;\r
290 byte_t dataoutbuf[16 * 16];\r
291 uint8_t uid[10];\r
292 uint32_t cuid;\r
293 struct Crypto1State mpcs = {0, 0};\r
294 struct Crypto1State *pcs;\r
295 pcs = &mpcs;\r
296\r
297 // clear trace\r
298 clear_trace();\r
299\r
300 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r
301\r
302 LED_A_ON();\r
303 LED_B_OFF();\r
304 LED_C_OFF();\r
305\r
306 isOK = 1;\r
307 if(!iso14443a_select_card(uid, NULL, &cuid)) {\r
308 isOK = 0;\r
309 if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");\r
310 }\r
311\r
312 \r
313 if(isOK && mifare_classic_auth(pcs, cuid, FirstBlockOfSector(sectorNo), keyType, ui64Key, AUTH_FIRST)) {\r
314 isOK = 0;\r
315 if (MF_DBGLEVEL >= 1) Dbprintf("Auth error");\r
316 }\r
317 \r
318 for (uint8_t blockNo = 0; isOK && blockNo < NumBlocksPerSector(sectorNo); blockNo++) {\r
319 if(mifare_classic_readblock(pcs, cuid, FirstBlockOfSector(sectorNo) + blockNo, dataoutbuf + 16 * blockNo)) {\r
320 isOK = 0;\r
321 if (MF_DBGLEVEL >= 1) Dbprintf("Read sector %2d block %2d error", sectorNo, blockNo);\r
322 break;\r
323 }\r
324 }\r
325 \r
326 if(mifare_classic_halt(pcs, cuid)) {\r
327 if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");\r
328 }\r
329\r
330 // ----------------------------- crypto1 destroy\r
331 crypto1_destroy(pcs);\r
332 \r
333 if (MF_DBGLEVEL >= 2) DbpString("READ SECTOR FINISHED");\r
334\r
335 LED_B_ON();\r
336 cmd_send(CMD_ACK,isOK,0,0,dataoutbuf,16*NumBlocksPerSector(sectorNo));\r
337 LED_B_OFF();\r
338\r
339 // Thats it...\r
340 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
341 LEDsoff();\r
342}\r
343\r
344void MifareUReadCard(uint8_t arg0, uint16_t arg1, uint8_t arg2, uint8_t *datain)\r
345{\r
346 // params\r
347 uint8_t blockNo = arg0;\r
348 uint16_t blocks = arg1;\r
349 bool useKey = (arg2 == 1);\r
350 int countblocks = 0;\r
351 uint8_t dataout[176] = {0x00};\r
352\r
353 LEDsoff();\r
354 LED_A_ON();\r
355 clear_trace();\r
356 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r
357\r
358 int len = iso14443a_select_card(NULL, NULL, NULL);\r
359 if (!len) {\r
360 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Can't select card (RC:%d)",len);\r
361 OnError(1);\r
362 return;\r
363 }\r
364\r
365 // authenticate\r
366 if ( useKey ) {\r
367 uint8_t key[16] = {0x00};\r
368 memcpy(key, datain, 16);\r
369\r
370 uint8_t random_a[8] = {1,1,1,1,1,1,1,1 };\r
371 uint8_t random_b[8] = {0x00};\r
372 uint8_t enc_random_b[8] = {0x00};\r
373 uint8_t rnd_ab[16] = {0x00};\r
374 uint8_t IV[8] = {0x00};\r
375\r
376 uint16_t len;\r
377 uint8_t receivedAnswer[MAX_FRAME_SIZE];\r
378 uint8_t receivedAnswerPar[MAX_PARITY_SIZE];\r
379\r
380 len = mifare_sendcmd_short(NULL, 1, 0x1A, 0x00, receivedAnswer,receivedAnswerPar ,NULL);\r
381 if (len != 11) {\r
382 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Cmd Error: %02x", receivedAnswer[0]);\r
383 OnError(1);\r
384 return;\r
385 }\r
386\r
387 // tag nonce.\r
388 memcpy(enc_random_b,receivedAnswer+1,8);\r
389\r
390 // decrypt nonce.\r
391 tdes_2key_dec(random_b, enc_random_b, sizeof(random_b), key, IV );\r
392 rol(random_b,8);\r
393 memcpy(rnd_ab ,random_a,8);\r
394 memcpy(rnd_ab+8,random_b,8);\r
395\r
396 // encrypt out, in, length, key, iv\r
397 tdes_2key_enc(rnd_ab, rnd_ab, sizeof(rnd_ab), key, enc_random_b);\r
398\r
399 len = mifare_sendcmd_short_mfucauth(NULL, 1, 0xAF, rnd_ab, receivedAnswer, receivedAnswerPar, NULL);\r
400 if (len != 11) {\r
401 OnError(1);\r
402 return;\r
403 }\r
404\r
405 uint8_t enc_resp[8] = { 0 };\r
406 uint8_t resp_random_a[8] = { 0 };\r
407 memcpy(enc_resp, receivedAnswer+1, 8);\r
408\r
409 // decrypt out, in, length, key, iv \r
410 tdes_2key_dec(resp_random_a, enc_resp, 8, key, enc_random_b);\r
411 if ( memcmp(resp_random_a, random_a, 8) != 0 )\r
412 Dbprintf("failed authentication"); \r
413 }\r
414\r
415 for (int i = 0; i < blocks; i++){\r
416 len = mifare_ultra_readblock(blockNo * 4 + i, dataout + 4 * i);\r
417\r
418 if (len) {\r
419 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Read block %d error",i);\r
420 OnError(2);\r
421 return;\r
422 } else {\r
423 countblocks++;\r
424 }\r
425 }\r
426\r
427 len = mifare_ultra_halt();\r
428 if (len) {\r
429 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Halt error");\r
430 OnError(3);\r
431 return;\r
432 }\r
433\r
434 if (MF_DBGLEVEL >= MF_DBG_EXTENDED) Dbprintf("Blocks read %d", countblocks);\r
435\r
436 len = blocks * 4;\r
437\r
438 cmd_send(CMD_ACK, 1, len, 0, dataout, len); \r
439 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
440 LEDsoff();\r
441}\r
442\r
443//-----------------------------------------------------------------------------\r
444// Select, Authenticate, Write a MIFARE tag. \r
445// read block\r
446//-----------------------------------------------------------------------------\r
447void MifareWriteBlock(uint8_t arg0, uint8_t arg1, uint8_t arg2, uint8_t *datain)\r
448{\r
449 // params\r
450 uint8_t blockNo = arg0;\r
451 uint8_t keyType = arg1;\r
452 uint64_t ui64Key = 0;\r
453 byte_t blockdata[16];\r
454\r
455 ui64Key = bytes_to_num(datain, 6);\r
456 memcpy(blockdata, datain + 10, 16);\r
457 \r
458 // variables\r
459 byte_t isOK = 0;\r
460 uint8_t uid[10];\r
461 uint32_t cuid;\r
462 struct Crypto1State mpcs = {0, 0};\r
463 struct Crypto1State *pcs;\r
464 pcs = &mpcs;\r
465\r
466 // clear trace\r
467 clear_trace();\r
468\r
469 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r
470\r
471 LED_A_ON();\r
472 LED_B_OFF();\r
473 LED_C_OFF();\r
474\r
475 while (true) {\r
476 if(!iso14443a_select_card(uid, NULL, &cuid)) {\r
477 if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");\r
478 break;\r
479 };\r
480\r
481 if(mifare_classic_auth(pcs, cuid, blockNo, keyType, ui64Key, AUTH_FIRST)) {\r
482 if (MF_DBGLEVEL >= 1) Dbprintf("Auth error");\r
483 break;\r
484 };\r
485 \r
486 if(mifare_classic_writeblock(pcs, cuid, blockNo, blockdata)) {\r
487 if (MF_DBGLEVEL >= 1) Dbprintf("Write block error");\r
488 break;\r
489 };\r
490\r
491 if(mifare_classic_halt(pcs, cuid)) {\r
492 if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");\r
493 break;\r
494 };\r
495 \r
496 isOK = 1;\r
497 break;\r
498 }\r
499 \r
500 // ----------------------------- crypto1 destroy\r
501 crypto1_destroy(pcs);\r
502 \r
503 if (MF_DBGLEVEL >= 2) DbpString("WRITE BLOCK FINISHED");\r
504\r
505 LED_B_ON();\r
506 cmd_send(CMD_ACK,isOK,0,0,0,0);\r
507 LED_B_OFF();\r
508\r
509\r
510 // Thats it...\r
511 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
512 LEDsoff();\r
513}\r
514\r
515void MifareUWriteBlock(uint8_t arg0, uint8_t *datain)\r
516{\r
517 uint8_t blockNo = arg0;\r
518 byte_t blockdata[16] = {0x00};\r
519\r
520 memcpy(blockdata, datain, 16);\r
521\r
522 uint8_t uid[10] = {0x00};\r
523\r
524 LED_A_ON(); LED_B_OFF(); LED_C_OFF();\r
525\r
526 clear_trace();\r
527 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r
528\r
529 if(!iso14443a_select_card(uid, NULL, NULL)) {\r
530 if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");\r
531 OnError(0);\r
532 return;\r
533 };\r
534\r
535 if(mifare_ultra_writeblock(blockNo, blockdata)) {\r
536 if (MF_DBGLEVEL >= 1) Dbprintf("Write block error");\r
537 OnError(0);\r
538 return; };\r
539\r
540 if(mifare_ultra_halt()) {\r
541 if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");\r
542 OnError(0);\r
543 return;\r
544 };\r
545\r
546 if (MF_DBGLEVEL >= 2) DbpString("WRITE BLOCK FINISHED");\r
547\r
548 cmd_send(CMD_ACK,1,0,0,0,0);\r
549 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
550 LEDsoff();\r
551}\r
552\r
553void MifareUWriteBlock_Special(uint8_t arg0, uint8_t *datain)\r
554{\r
555 uint8_t blockNo = arg0;\r
556 byte_t blockdata[4] = {0x00};\r
557 \r
558 memcpy(blockdata, datain,4);\r
559\r
560 uint8_t uid[10] = {0x00};\r
561 \r
562 LED_A_ON(); LED_B_OFF(); LED_C_OFF();\r
563 clear_trace();\r
564 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r
565\r
566 if(!iso14443a_select_card(uid, NULL, NULL)) {\r
567 if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");\r
568 OnError(0);\r
569 return;\r
570 };\r
571\r
572 if(mifare_ultra_special_writeblock(blockNo, blockdata)) {\r
573 if (MF_DBGLEVEL >= 1) Dbprintf("Write block error");\r
574 OnError(0);\r
575 return;\r
576 };\r
577\r
578 if(mifare_ultra_halt()) {\r
579 if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");\r
580 OnError(0);\r
581 return;\r
582 };\r
583\r
584 if (MF_DBGLEVEL >= 2) DbpString("WRITE BLOCK FINISHED");\r
585\r
586 cmd_send(CMD_ACK,1,0,0,0,0);\r
587 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
588 LEDsoff();\r
589}\r
590\r
591void MifareUSetPwd(uint8_t arg0, uint8_t *datain){\r
592 \r
593 uint8_t pwd[16] = {0x00};\r
594 byte_t blockdata[4] = {0x00};\r
595 \r
596 memcpy(pwd, datain, 16);\r
597 \r
598 LED_A_ON(); LED_B_OFF(); LED_C_OFF();\r
599 clear_trace();\r
600 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r
601\r
602 if(!iso14443a_select_card(NULL, NULL, NULL)) {\r
603 if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");\r
604 OnError(0);\r
605 return;\r
606 };\r
607\r
608 blockdata[0] = pwd[7];\r
609 blockdata[1] = pwd[6];\r
610 blockdata[2] = pwd[5];\r
611 blockdata[3] = pwd[4];\r
612 if(mifare_ultra_special_writeblock( 44, blockdata)) {\r
613 if (MF_DBGLEVEL >= 1) Dbprintf("Write block error");\r
614 OnError(44);\r
615 return;\r
616 };\r
617\r
618 blockdata[0] = pwd[3];\r
619 blockdata[1] = pwd[2];\r
620 blockdata[2] = pwd[1];\r
621 blockdata[3] = pwd[0];\r
622 if(mifare_ultra_special_writeblock( 45, blockdata)) {\r
623 if (MF_DBGLEVEL >= 1) Dbprintf("Write block error");\r
624 OnError(45);\r
625 return;\r
626 };\r
627\r
628 blockdata[0] = pwd[15];\r
629 blockdata[1] = pwd[14];\r
630 blockdata[2] = pwd[13];\r
631 blockdata[3] = pwd[12];\r
632 if(mifare_ultra_special_writeblock( 46, blockdata)) {\r
633 if (MF_DBGLEVEL >= 1) Dbprintf("Write block error");\r
634 OnError(46);\r
635 return;\r
636 };\r
637\r
638 blockdata[0] = pwd[11];\r
639 blockdata[1] = pwd[10];\r
640 blockdata[2] = pwd[9];\r
641 blockdata[3] = pwd[8];\r
642 if(mifare_ultra_special_writeblock( 47, blockdata)) {\r
643 if (MF_DBGLEVEL >= 1) Dbprintf("Write block error");\r
644 OnError(47);\r
645 return;\r
646 }; \r
647\r
648 if(mifare_ultra_halt()) {\r
649 if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");\r
650 OnError(0);\r
651 return;\r
652 };\r
653\r
654 cmd_send(CMD_ACK,1,0,0,0,0);\r
655 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
656 LEDsoff();\r
657}\r
658\r
659// Return 1 if the nonce is invalid else return 0\r
660int valid_nonce(uint32_t Nt, uint32_t NtEnc, uint32_t Ks1, uint8_t *parity) {\r
661 return ((oddparity((Nt >> 24) & 0xFF) == ((parity[0]) ^ oddparity((NtEnc >> 24) & 0xFF) ^ BIT(Ks1,16))) & \\r
662 (oddparity((Nt >> 16) & 0xFF) == ((parity[1]) ^ oddparity((NtEnc >> 16) & 0xFF) ^ BIT(Ks1,8))) & \\r
663 (oddparity((Nt >> 8) & 0xFF) == ((parity[2]) ^ oddparity((NtEnc >> 8) & 0xFF) ^ BIT(Ks1,0)))) ? 1 : 0;\r
664}\r
665\r
666\r
667//-----------------------------------------------------------------------------\r
668// MIFARE nested authentication. \r
669// \r
670//-----------------------------------------------------------------------------\r
671void MifareNested(uint32_t arg0, uint32_t arg1, uint32_t calibrate, uint8_t *datain)\r
672{\r
673 // params\r
674 uint8_t blockNo = arg0 & 0xff;\r
675 uint8_t keyType = (arg0 >> 8) & 0xff;\r
676 uint8_t targetBlockNo = arg1 & 0xff;\r
677 uint8_t targetKeyType = (arg1 >> 8) & 0xff;\r
678 uint64_t ui64Key = 0;\r
679\r
680 ui64Key = bytes_to_num(datain, 6);\r
681 \r
682 // variables\r
683 uint16_t rtr, i, j, len;\r
684 uint16_t davg;\r
685 static uint16_t dmin, dmax;\r
686 uint8_t uid[10];\r
687 uint32_t cuid, nt1, nt2, nttmp, nttest, ks1;\r
688 uint8_t par[1];\r
689 uint32_t target_nt[2], target_ks[2];\r
690 \r
691 uint8_t par_array[4];\r
692 uint16_t ncount = 0;\r
693 struct Crypto1State mpcs = {0, 0};\r
694 struct Crypto1State *pcs;\r
695 pcs = &mpcs;\r
696 uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];\r
697\r
698 uint32_t auth1_time, auth2_time;\r
699 static uint16_t delta_time;\r
700\r
701 // free eventually allocated BigBuf memory\r
702 BigBuf_free();\r
703 // clear trace\r
704 clear_trace();\r
705 set_tracing(false);\r
706 \r
707 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r
708\r
709 LED_A_ON();\r
710 LED_C_OFF();\r
711\r
712\r
713 // statistics on nonce distance\r
714 if (calibrate) { // for first call only. Otherwise reuse previous calibration\r
715 LED_B_ON();\r
716 WDT_HIT();\r
717\r
718 davg = dmax = 0;\r
719 dmin = 2000;\r
720 delta_time = 0;\r
721 \r
722 for (rtr = 0; rtr < 17; rtr++) {\r
723\r
724 // prepare next select. No need to power down the card.\r
725 if(mifare_classic_halt(pcs, cuid)) {\r
726 if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Halt error");\r
727 rtr--;\r
728 continue;\r
729 }\r
730\r
731 if(!iso14443a_select_card(uid, NULL, &cuid)) {\r
732 if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Can't select card");\r
733 rtr--;\r
734 continue;\r
735 };\r
736\r
737 auth1_time = 0;\r
738 if(mifare_classic_authex(pcs, cuid, blockNo, keyType, ui64Key, AUTH_FIRST, &nt1, &auth1_time)) {\r
739 if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Auth1 error");\r
740 rtr--;\r
741 continue;\r
742 };\r
743\r
744 if (delta_time) {\r
745 auth2_time = auth1_time + delta_time;\r
746 } else {\r
747 auth2_time = 0;\r
748 }\r
749 if(mifare_classic_authex(pcs, cuid, blockNo, keyType, ui64Key, AUTH_NESTED, &nt2, &auth2_time)) {\r
750 if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Auth2 error");\r
751 rtr--;\r
752 continue;\r
753 };\r
754\r
755 nttmp = prng_successor(nt1, 100); //NXP Mifare is typical around 840,but for some unlicensed/compatible mifare card this can be 160\r
756 for (i = 101; i < 1200; i++) {\r
757 nttmp = prng_successor(nttmp, 1);\r
758 if (nttmp == nt2) break;\r
759 }\r
760\r
761 if (i != 1200) {\r
762 if (rtr != 0) {\r
763 davg += i;\r
764 dmin = MIN(dmin, i);\r
765 dmax = MAX(dmax, i);\r
766 }\r
767 else {\r
768 delta_time = auth2_time - auth1_time + 32; // allow some slack for proper timing\r
769 }\r
770 if (MF_DBGLEVEL >= 3) Dbprintf("Nested: calibrating... ntdist=%d", i);\r
771 }\r
772 }\r
773 \r
774 if (rtr <= 1) return;\r
775\r
776 davg = (davg + (rtr - 1)/2) / (rtr - 1);\r
777 \r
778 if (MF_DBGLEVEL >= 3) Dbprintf("min=%d max=%d avg=%d, delta_time=%d", dmin, dmax, davg, delta_time);\r
779\r
780 dmin = davg - 2;\r
781 dmax = davg + 2;\r
782 \r
783 LED_B_OFF();\r
784 \r
785 }\r
786// ------------------------------------------------------------------------------------------------- \r
787 \r
788 LED_C_ON();\r
789\r
790 // get crypted nonces for target sector\r
791 for(i=0; i < 2; i++) { // look for exactly two different nonces\r
792\r
793 target_nt[i] = 0;\r
794 while(target_nt[i] == 0) { // continue until we have an unambiguous nonce\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 continue;\r
800 }\r
801\r
802 if(!iso14443a_select_card(uid, NULL, &cuid)) {\r
803 if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Can't select card");\r
804 continue;\r
805 };\r
806 \r
807 auth1_time = 0;\r
808 if(mifare_classic_authex(pcs, cuid, blockNo, keyType, ui64Key, AUTH_FIRST, &nt1, &auth1_time)) {\r
809 if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Auth1 error");\r
810 continue;\r
811 };\r
812\r
813 // nested authentication\r
814 auth2_time = auth1_time + delta_time;\r
815 len = mifare_sendcmd_shortex(pcs, AUTH_NESTED, 0x60 + (targetKeyType & 0x01), targetBlockNo, receivedAnswer, par, &auth2_time);\r
816 if (len != 4) {\r
817 if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Auth2 error len=%d", len);\r
818 continue;\r
819 };\r
820 \r
821 nt2 = bytes_to_num(receivedAnswer, 4); \r
822 if (MF_DBGLEVEL >= 3) Dbprintf("Nonce#%d: Testing nt1=%08x nt2enc=%08x nt2par=%02x", i+1, nt1, nt2, par[0]);\r
823 \r
824 // Parity validity check\r
825 for (j = 0; j < 4; j++) {\r
826 par_array[j] = (oddparity(receivedAnswer[j]) != ((par[0] >> (7-j)) & 0x01));\r
827 }\r
828 \r
829 ncount = 0;\r
830 nttest = prng_successor(nt1, dmin - 1);\r
831 for (j = dmin; j < dmax + 1; j++) {\r
832 nttest = prng_successor(nttest, 1);\r
833 ks1 = nt2 ^ nttest;\r
834\r
835 if (valid_nonce(nttest, nt2, ks1, par_array)){\r
836 if (ncount > 0) { // we are only interested in disambiguous nonces, try again\r
837 if (MF_DBGLEVEL >= 3) Dbprintf("Nonce#%d: dismissed (ambigous), ntdist=%d", i+1, j);\r
838 target_nt[i] = 0;\r
839 break;\r
840 }\r
841 target_nt[i] = nttest;\r
842 target_ks[i] = ks1;\r
843 ncount++;\r
844 if (i == 1 && target_nt[1] == target_nt[0]) { // we need two different nonces\r
845 target_nt[i] = 0;\r
846 if (MF_DBGLEVEL >= 3) Dbprintf("Nonce#2: dismissed (= nonce#1), ntdist=%d", j);\r
847 break;\r
848 }\r
849 if (MF_DBGLEVEL >= 3) Dbprintf("Nonce#%d: valid, ntdist=%d", i+1, j);\r
850 }\r
851 }\r
852 if (target_nt[i] == 0 && j == dmax+1 && MF_DBGLEVEL >= 3) Dbprintf("Nonce#%d: dismissed (all invalid)", i+1);\r
853 }\r
854 }\r
855\r
856 LED_C_OFF();\r
857 \r
858 // ----------------------------- crypto1 destroy\r
859 crypto1_destroy(pcs);\r
860 \r
861 byte_t buf[4 + 4 * 4];\r
862 memcpy(buf, &cuid, 4);\r
863 memcpy(buf+4, &target_nt[0], 4);\r
864 memcpy(buf+8, &target_ks[0], 4);\r
865 memcpy(buf+12, &target_nt[1], 4);\r
866 memcpy(buf+16, &target_ks[1], 4);\r
867 \r
868 LED_B_ON();\r
869 cmd_send(CMD_ACK, 0, 2, targetBlockNo + (targetKeyType * 0x100), buf, sizeof(buf));\r
870 LED_B_OFF();\r
871\r
872 if (MF_DBGLEVEL >= 3) DbpString("NESTED FINISHED");\r
873\r
874 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
875 LEDsoff();\r
876 set_tracing(TRUE);\r
877}\r
878\r
879//-----------------------------------------------------------------------------\r
880// MIFARE check keys. key count up to 85. \r
881// \r
882//-----------------------------------------------------------------------------\r
883void MifareChkKeys(uint8_t arg0, uint8_t arg1, uint8_t arg2, uint8_t *datain)\r
884{\r
885 // params\r
886 uint8_t blockNo = arg0;\r
887 uint8_t keyType = arg1;\r
888 uint8_t keyCount = arg2;\r
889 uint64_t ui64Key = 0;\r
890 \r
891 // variables\r
892 int i;\r
893 byte_t isOK = 0;\r
894 uint8_t uid[10];\r
895 uint32_t cuid;\r
896 struct Crypto1State mpcs = {0, 0};\r
897 struct Crypto1State *pcs;\r
898 pcs = &mpcs;\r
899 \r
900 // clear debug level\r
901 int OLD_MF_DBGLEVEL = MF_DBGLEVEL; \r
902 MF_DBGLEVEL = MF_DBG_NONE;\r
903 \r
904 // clear trace\r
905 clear_trace();\r
906 set_tracing(TRUE);\r
907\r
908 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r
909\r
910 LED_A_ON();\r
911 LED_B_OFF();\r
912 LED_C_OFF();\r
913\r
914 for (i = 0; i < keyCount; i++) {\r
915 if(mifare_classic_halt(pcs, cuid)) {\r
916 if (MF_DBGLEVEL >= 1) Dbprintf("ChkKeys: Halt error");\r
917 }\r
918\r
919 if(!iso14443a_select_card(uid, NULL, &cuid)) {\r
920 if (OLD_MF_DBGLEVEL >= 1) Dbprintf("ChkKeys: Can't select card");\r
921 break;\r
922 };\r
923\r
924 ui64Key = bytes_to_num(datain + i * 6, 6);\r
925 if(mifare_classic_auth(pcs, cuid, blockNo, keyType, ui64Key, AUTH_FIRST)) {\r
926 continue;\r
927 };\r
928 \r
929 isOK = 1;\r
930 break;\r
931 }\r
932 \r
933 // ----------------------------- crypto1 destroy\r
934 crypto1_destroy(pcs);\r
935 \r
936 LED_B_ON();\r
937 cmd_send(CMD_ACK,isOK,0,0,datain + i * 6,6);\r
938 LED_B_OFF();\r
939\r
940 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
941 LEDsoff();\r
942\r
943 // restore debug level\r
944 MF_DBGLEVEL = OLD_MF_DBGLEVEL; \r
945}\r
946\r
947//-----------------------------------------------------------------------------\r
948// MIFARE commands set debug level\r
949// \r
950//-----------------------------------------------------------------------------\r
951void MifareSetDbgLvl(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain){\r
952 MF_DBGLEVEL = arg0;\r
953 Dbprintf("Debug level: %d", MF_DBGLEVEL);\r
954}\r
955\r
956//-----------------------------------------------------------------------------\r
957// Work with emulator memory\r
958// \r
959//-----------------------------------------------------------------------------\r
960void MifareEMemClr(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain){\r
961 emlClearMem();\r
962}\r
963\r
964void MifareEMemSet(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain){\r
965 emlSetMem(datain, arg0, arg1); // data, block num, blocks count\r
966}\r
967\r
968void MifareEMemGet(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain){\r
969 byte_t buf[USB_CMD_DATA_SIZE];\r
970 emlGetMem(buf, arg0, arg1); // data, block num, blocks count (max 4)\r
971\r
972 LED_B_ON();\r
973 cmd_send(CMD_ACK,arg0,arg1,0,buf,USB_CMD_DATA_SIZE);\r
974 LED_B_OFF();\r
975}\r
976\r
977//-----------------------------------------------------------------------------\r
978// Load a card into the emulator memory\r
979// \r
980//-----------------------------------------------------------------------------\r
981void MifareECardLoad(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain){\r
982 uint8_t numSectors = arg0;\r
983 uint8_t keyType = arg1;\r
984 uint64_t ui64Key = 0;\r
985 uint32_t cuid;\r
986 struct Crypto1State mpcs = {0, 0};\r
987 struct Crypto1State *pcs;\r
988 pcs = &mpcs;\r
989\r
990 // variables\r
991 byte_t dataoutbuf[16];\r
992 byte_t dataoutbuf2[16];\r
993 uint8_t uid[10];\r
994\r
995 // clear trace\r
996 clear_trace();\r
997 set_tracing(false);\r
998 \r
999 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r
1000\r
1001 LED_A_ON();\r
1002 LED_B_OFF();\r
1003 LED_C_OFF();\r
1004 \r
1005 bool isOK = true;\r
1006\r
1007 if(!iso14443a_select_card(uid, NULL, &cuid)) {\r
1008 isOK = false;\r
1009 if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");\r
1010 }\r
1011 \r
1012 for (uint8_t sectorNo = 0; isOK && sectorNo < numSectors; sectorNo++) {\r
1013 ui64Key = emlGetKey(sectorNo, keyType);\r
1014 if (sectorNo == 0){\r
1015 if(isOK && mifare_classic_auth(pcs, cuid, FirstBlockOfSector(sectorNo), keyType, ui64Key, AUTH_FIRST)) {\r
1016 isOK = false;\r
1017 if (MF_DBGLEVEL >= 1) Dbprintf("Sector[%2d]. Auth error", sectorNo);\r
1018 break;\r
1019 }\r
1020 } else {\r
1021 if(isOK && mifare_classic_auth(pcs, cuid, FirstBlockOfSector(sectorNo), keyType, ui64Key, AUTH_NESTED)) {\r
1022 isOK = false;\r
1023 if (MF_DBGLEVEL >= 1) Dbprintf("Sector[%2d]. Auth nested error", sectorNo);\r
1024 break;\r
1025 }\r
1026 }\r
1027 \r
1028 for (uint8_t blockNo = 0; isOK && blockNo < NumBlocksPerSector(sectorNo); blockNo++) {\r
1029 if(isOK && mifare_classic_readblock(pcs, cuid, FirstBlockOfSector(sectorNo) + blockNo, dataoutbuf)) {\r
1030 isOK = false;\r
1031 if (MF_DBGLEVEL >= 1) Dbprintf("Error reading sector %2d block %2d", sectorNo, blockNo);\r
1032 break;\r
1033 };\r
1034 if (isOK) {\r
1035 if (blockNo < NumBlocksPerSector(sectorNo) - 1) {\r
1036 emlSetMem(dataoutbuf, FirstBlockOfSector(sectorNo) + blockNo, 1);\r
1037 } else { // sector trailer, keep the keys, set only the AC\r
1038 emlGetMem(dataoutbuf2, FirstBlockOfSector(sectorNo) + blockNo, 1);\r
1039 memcpy(&dataoutbuf2[6], &dataoutbuf[6], 4);\r
1040 emlSetMem(dataoutbuf2, FirstBlockOfSector(sectorNo) + blockNo, 1);\r
1041 }\r
1042 }\r
1043 }\r
1044\r
1045 }\r
1046\r
1047 if(mifare_classic_halt(pcs, cuid)) {\r
1048 if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");\r
1049 };\r
1050\r
1051 // ----------------------------- crypto1 destroy\r
1052 crypto1_destroy(pcs);\r
1053\r
1054 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
1055 LEDsoff();\r
1056 \r
1057 if (MF_DBGLEVEL >= 2) DbpString("EMUL FILL SECTORS FINISHED");\r
1058\r
1059}\r
1060\r
1061\r
1062//-----------------------------------------------------------------------------\r
1063// Work with "magic Chinese" card (email him: ouyangweidaxian@live.cn)\r
1064// \r
1065//-----------------------------------------------------------------------------\r
1066void MifareCSetBlock(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain){\r
1067 \r
1068 // params\r
1069 uint8_t needWipe = arg0;\r
1070 // bit 0 - need get UID\r
1071 // bit 1 - need wupC\r
1072 // bit 2 - need HALT after sequence\r
1073 // bit 3 - need init FPGA and field before sequence\r
1074 // bit 4 - need reset FPGA and LED\r
1075 uint8_t workFlags = arg1;\r
1076 uint8_t blockNo = arg2;\r
1077 \r
1078 // card commands\r
1079 uint8_t wupC1[] = { 0x40 }; \r
1080 uint8_t wupC2[] = { 0x43 }; \r
1081 uint8_t wipeC[] = { 0x41 }; \r
1082 \r
1083 // variables\r
1084 byte_t isOK = 0;\r
1085 uint8_t uid[10] = {0x00};\r
1086 uint8_t d_block[18] = {0x00};\r
1087 uint32_t cuid;\r
1088 \r
1089 uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];\r
1090 uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE];\r
1091\r
1092 // reset FPGA and LED\r
1093 if (workFlags & 0x08) {\r
1094 LED_A_ON();\r
1095 LED_B_OFF();\r
1096 LED_C_OFF();\r
1097 \r
1098 clear_trace();\r
1099 set_tracing(TRUE);\r
1100 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r
1101 }\r
1102\r
1103 while (true) {\r
1104\r
1105 // get UID from chip\r
1106 if (workFlags & 0x01) {\r
1107 if(!iso14443a_select_card(uid, NULL, &cuid)) {\r
1108 if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");\r
1109 break;\r
1110 };\r
1111\r
1112 if(mifare_classic_halt(NULL, cuid)) {\r
1113 if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");\r
1114 break;\r
1115 };\r
1116 };\r
1117 \r
1118 // reset chip\r
1119 if (needWipe){\r
1120 ReaderTransmitBitsPar(wupC1,7,0, NULL);\r
1121 if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != 0x0a)) {\r
1122 if (MF_DBGLEVEL >= 1) Dbprintf("wupC1 error");\r
1123 break;\r
1124 };\r
1125\r
1126 ReaderTransmit(wipeC, sizeof(wipeC), NULL);\r
1127 if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != 0x0a)) {\r
1128 if (MF_DBGLEVEL >= 1) Dbprintf("wipeC error");\r
1129 break;\r
1130 };\r
1131\r
1132 if(mifare_classic_halt(NULL, cuid)) {\r
1133 if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");\r
1134 break;\r
1135 };\r
1136 }; \r
1137\r
1138 // write block\r
1139 if (workFlags & 0x02) {\r
1140 ReaderTransmitBitsPar(wupC1,7,0, NULL);\r
1141 if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != 0x0a)) {\r
1142 if (MF_DBGLEVEL >= 1) Dbprintf("wupC1 error");\r
1143 break;\r
1144 };\r
1145\r
1146 ReaderTransmit(wupC2, sizeof(wupC2), NULL);\r
1147 if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != 0x0a)) {\r
1148 if (MF_DBGLEVEL >= 1) Dbprintf("wupC2 error");\r
1149 break;\r
1150 };\r
1151 }\r
1152\r
1153 if ((mifare_sendcmd_short(NULL, 0, 0xA0, blockNo, receivedAnswer, receivedAnswerPar, NULL) != 1) || (receivedAnswer[0] != 0x0a)) {\r
1154 if (MF_DBGLEVEL >= 1) Dbprintf("write block send command error");\r
1155 break;\r
1156 };\r
1157 \r
1158 memcpy(d_block, datain, 16);\r
1159 AppendCrc14443a(d_block, 16);\r
1160 \r
1161 ReaderTransmit(d_block, sizeof(d_block), NULL);\r
1162 if ((ReaderReceive(receivedAnswer, receivedAnswerPar) != 1) || (receivedAnswer[0] != 0x0a)) {\r
1163 if (MF_DBGLEVEL >= 1) Dbprintf("write block send data error");\r
1164 break;\r
1165 }; \r
1166 \r
1167 if (workFlags & 0x04) {\r
1168 if (mifare_classic_halt(NULL, cuid)) {\r
1169 if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");\r
1170 break;\r
1171 };\r
1172 }\r
1173 \r
1174 isOK = 1;\r
1175 break;\r
1176 }\r
1177 \r
1178 LED_B_ON();\r
1179 cmd_send(CMD_ACK,isOK,0,0,uid,4);\r
1180 LED_B_OFF();\r
1181\r
1182 if ((workFlags & 0x10) || (!isOK)) {\r
1183 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
1184 LEDsoff();\r
1185 }\r
1186}\r
1187\r
1188\r
1189void MifareCGetBlock(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain){\r
1190 \r
1191 // params\r
1192 // bit 1 - need wupC\r
1193 // bit 2 - need HALT after sequence\r
1194 // bit 3 - need init FPGA and field before sequence\r
1195 // bit 4 - need reset FPGA and LED\r
1196 uint8_t workFlags = arg0;\r
1197 uint8_t blockNo = arg2;\r
1198 \r
1199 // card commands\r
1200 uint8_t wupC1[] = { 0x40 }; \r
1201 uint8_t wupC2[] = { 0x43 }; \r
1202 \r
1203 // variables\r
1204 byte_t isOK = 0;\r
1205 uint8_t data[18] = {0x00};\r
1206 uint32_t cuid = 0;\r
1207 \r
1208 uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];\r
1209 uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE];\r
1210 \r
1211 if (workFlags & 0x08) {\r
1212 LED_A_ON();\r
1213 LED_B_OFF();\r
1214 LED_C_OFF();\r
1215 \r
1216 clear_trace();\r
1217 set_tracing(TRUE);\r
1218 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r
1219 }\r
1220\r
1221 while (true) {\r
1222 if (workFlags & 0x02) {\r
1223 ReaderTransmitBitsPar(wupC1,7,0, NULL);\r
1224 if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != 0x0a)) {\r
1225 if (MF_DBGLEVEL >= 1) Dbprintf("wupC1 error");\r
1226 break;\r
1227 };\r
1228\r
1229 ReaderTransmit(wupC2, sizeof(wupC2), NULL);\r
1230 if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != 0x0a)) {\r
1231 if (MF_DBGLEVEL >= 1) Dbprintf("wupC2 error");\r
1232 break;\r
1233 };\r
1234 }\r
1235\r
1236 // read block\r
1237 if ((mifare_sendcmd_short(NULL, 0, 0x30, blockNo, receivedAnswer, receivedAnswerPar, NULL) != 18)) {\r
1238 if (MF_DBGLEVEL >= 1) Dbprintf("read block send command error");\r
1239 break;\r
1240 };\r
1241 memcpy(data, receivedAnswer, 18);\r
1242 \r
1243 if (workFlags & 0x04) {\r
1244 if (mifare_classic_halt(NULL, cuid)) {\r
1245 if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");\r
1246 break;\r
1247 };\r
1248 }\r
1249 \r
1250 isOK = 1;\r
1251 break;\r
1252 }\r
1253 \r
1254 LED_B_ON();\r
1255 cmd_send(CMD_ACK,isOK,0,0,data,18);\r
1256 LED_B_OFF();\r
1257\r
1258 if ((workFlags & 0x10) || (!isOK)) {\r
1259 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
1260 LEDsoff();\r
1261 }\r
1262}\r
1263\r
1264void MifareCIdent(){\r
1265 \r
1266 // card commands\r
1267 uint8_t wupC1[] = { 0x40 }; \r
1268 uint8_t wupC2[] = { 0x43 }; \r
1269 \r
1270 // variables\r
1271 byte_t isOK = 1;\r
1272 \r
1273 uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];\r
1274 uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE];\r
1275\r
1276 ReaderTransmitBitsPar(wupC1,7,0, NULL);\r
1277 if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != 0x0a)) {\r
1278 isOK = 0;\r
1279 };\r
1280\r
1281 ReaderTransmit(wupC2, sizeof(wupC2), NULL);\r
1282 if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != 0x0a)) {\r
1283 isOK = 0;\r
1284 };\r
1285\r
1286 if (mifare_classic_halt(NULL, 0)) {\r
1287 isOK = 0;\r
1288 };\r
1289\r
1290 cmd_send(CMD_ACK,isOK,0,0,0,0);\r
1291}\r
1292\r
1293 //\r
1294// DESFIRE\r
1295//\r
1296\r
1297void Mifare_DES_Auth1(uint8_t arg0, uint8_t *datain){\r
1298\r
1299 byte_t dataout[11] = {0x00};\r
1300 uint8_t uid[10] = {0x00};\r
1301 uint32_t cuid;\r
1302 \r
1303 clear_trace();\r
1304 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r
1305\r
1306 int len = iso14443a_select_card(uid, NULL, &cuid);\r
1307 if(!len) {\r
1308 if (MF_DBGLEVEL >= MF_DBG_ERROR) \r
1309 Dbprintf("Can't select card");\r
1310 //OnError(1);\r
1311 return;\r
1312 };\r
1313\r
1314 if(mifare_desfire_des_auth1(cuid, dataout)){\r
1315 if (MF_DBGLEVEL >= MF_DBG_ERROR) \r
1316 Dbprintf("Authentication part1: Fail.");\r
1317 //OnError(4);\r
1318 return;\r
1319 }\r
1320\r
1321 if (MF_DBGLEVEL >= MF_DBG_EXTENDED) DbpString("AUTH 1 FINISHED");\r
1322 \r
1323 cmd_send(CMD_ACK,1,cuid,0,dataout, sizeof(dataout));\r
1324}\r
1325\r
1326void Mifare_DES_Auth2(uint32_t arg0, uint8_t *datain){\r
1327\r
1328 uint32_t cuid = arg0;\r
1329 uint8_t key[16] = {0x00};\r
1330 byte_t isOK = 0;\r
1331 byte_t dataout[12] = {0x00};\r
1332 \r
1333 memcpy(key, datain, 16);\r
1334 \r
1335 isOK = mifare_desfire_des_auth2(cuid, key, dataout);\r
1336 \r
1337 if( isOK) {\r
1338 if (MF_DBGLEVEL >= MF_DBG_EXTENDED) \r
1339 Dbprintf("Authentication part2: Failed"); \r
1340 //OnError(4);\r
1341 return;\r
1342 }\r
1343\r
1344 if (MF_DBGLEVEL >= MF_DBG_EXTENDED) \r
1345 DbpString("AUTH 2 FINISHED");\r
1346\r
1347 cmd_send(CMD_ACK, isOK, 0, 0, dataout, sizeof(dataout));\r
1348 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
1349 LEDsoff();\r
1350}\r
1351\r
1352void OnSuccess(){\r
1353 pcb_blocknum = 0;\r
1354 ReaderTransmit(deselect_cmd, 3 , NULL);\r
1355 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
1356 LEDsoff();\r
1357}\r
1358\r
1359void OnError(uint8_t reason){\r
1360 pcb_blocknum = 0;\r
1361 ReaderTransmit(deselect_cmd, 3 , NULL);\r
1362 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
1363 cmd_send(CMD_ACK,0,reason,0,0,0);\r
1364 LEDsoff();\r
1365}\r
Impressum, Datenschutz