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