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