]> cvs.zerfleddert.de Git - proxmark3-svn/blame - armsrc/mifarecmd.c
fix bug in mfu cauth
[proxmark3-svn] / armsrc / mifarecmd.c
CommitLineData
8556b852 1//-----------------------------------------------------------------------------\r
b62a5a84 2// Merlok - June 2011, 2012\r
8556b852
M
3// Gerhard de Koning Gans - May 2008\r
4// Hagen Fritsch - June 2010\r
3fe4ff4f 5// Midnitesnake - Dec 2013\r
6// Andy Davies - Apr 2014\r
7// Iceman - May 2014\r
8556b852
M
8//\r
9// This code is licensed to you under the terms of the GNU GPL, version 2 or,\r
10// at your option, any later version. See the LICENSE.txt file for the text of\r
11// the license.\r
12//-----------------------------------------------------------------------------\r
13// Routines to support ISO 14443 type A.\r
14//-----------------------------------------------------------------------------\r
15\r
16#include "mifarecmd.h"\r
17#include "apps.h"\r
787b5bd8 18#include "util.h"\r
8556b852 19\r
f168b263 20#include "des.h"\r
a631936e 21#include "crc.h"\r
22\r
f168b263 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
8556b852 29//-----------------------------------------------------------------------------\r
baeaf579 30// Select, Authenticate, Read a MIFARE tag. \r
8556b852
M
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
1c611bbd 44 uint8_t uid[10];\r
8556b852
M
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
3000dc4e 51 clear_trace();\r
7bc95e2e 52 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r
8556b852
M
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
baeaf579 60 if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");\r
8556b852
M
61 break;\r
62 };\r
63\r
64 if(mifare_classic_auth(pcs, cuid, blockNo, keyType, ui64Key, AUTH_FIRST)) {\r
baeaf579 65 if (MF_DBGLEVEL >= 1) Dbprintf("Auth error");\r
8556b852
M
66 break;\r
67 };\r
68 \r
69 if(mifare_classic_readblock(pcs, cuid, blockNo, dataoutbuf)) {\r
baeaf579 70 if (MF_DBGLEVEL >= 1) Dbprintf("Read block error");\r
8556b852
M
71 break;\r
72 };\r
73\r
74 if(mifare_classic_halt(pcs, cuid)) {\r
baeaf579 75 if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");\r
8556b852
M
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
8556b852 88 LED_B_ON();\r
baeaf579 89 cmd_send(CMD_ACK,isOK,0,0,dataoutbuf,16);\r
8556b852
M
90 LED_B_OFF();\r
91\r
a631936e 92 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
93 LEDsoff();\r
94}\r
95\r
a631936e 96void MifareUC_Auth1(uint8_t arg0, uint8_t *datain){\r
97\r
a631936e 98 byte_t dataoutbuf[16] = {0x00};\r
99 uint8_t uid[10] = {0x00};\r
f168b263 100 uint32_t cuid = 0x00;\r
a631936e 101\r
f168b263 102 LED_A_ON(); LED_B_OFF(); LED_C_OFF();\r
a631936e 103 \r
3000dc4e 104 clear_trace();\r
a631936e 105 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r
106\r
107 if(!iso14443a_select_card(uid, NULL, &cuid)) {\r
f168b263 108 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Can't select card");\r
109 OnError(0);\r
a631936e 110 return;\r
111 };\r
112 \r
f168b263 113 if(mifare_ultra_auth1(dataoutbuf)){\r
114 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Authentication part1: Fail.");\r
115 OnError(1);\r
a631936e 116 return;\r
117 }\r
118\r
f168b263 119 if (MF_DBGLEVEL >= MF_DBG_EXTENDED) DbpString("AUTH 1 FINISHED");\r
a631936e 120 \r
f168b263 121 cmd_send(CMD_ACK,1,cuid,0,dataoutbuf,11);\r
a631936e 122 LEDsoff();\r
123}\r
124void MifareUC_Auth2(uint32_t arg0, uint8_t *datain){\r
125\r
a631936e 126 uint8_t key[16] = {0x00};\r
a631936e 127 byte_t dataoutbuf[16] = {0x00};\r
128 \r
129 memcpy(key, datain, 16);\r
130 \r
f168b263 131 LED_A_ON(); LED_B_OFF(); LED_C_OFF();\r
a631936e 132 \r
f168b263 133 if(mifare_ultra_auth2(key, dataoutbuf)){\r
134 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Authentication part2: Fail...");\r
135 OnError(1);\r
a631936e 136 return; \r
137 }\r
138 \r
f168b263 139 if (MF_DBGLEVEL >= MF_DBG_EXTENDED) DbpString("AUTH 2 FINISHED");\r
a631936e 140 \r
f168b263 141 cmd_send(CMD_ACK,1,0,0,dataoutbuf,11);\r
8556b852
M
142 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
143 LEDsoff();\r
7cc204bf 144}\r
145\r
f168b263 146void MifareUReadBlock(uint8_t arg0, uint8_t arg1, uint8_t *datain)\r
7cc204bf 147{\r
7cc204bf 148 uint8_t blockNo = arg0;\r
787b5bd8 149 byte_t dataout[16] = {0x00};\r
150 uint8_t uid[10] = {0x00};\r
f168b263 151 uint8_t key[16] = {0x00};\r
152 bool usePwd = (arg1 == 1);\r
153\r
154 LED_A_ON(); LED_B_OFF(); LED_C_OFF();\r
155\r
3000dc4e 156 clear_trace();\r
787b5bd8 157 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r
f168b263 158\r
159 int len = iso14443a_select_card(uid, NULL, NULL);\r
787b5bd8 160 if(!len) {\r
f168b263 161 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Can't select card (RC:%02X)",len);\r
162 OnError(1);\r
787b5bd8 163 return;\r
f168b263 164 }\r
165\r
166 // authenticate here.\r
167 if ( usePwd ) {\r
168\r
169 memcpy(key, datain, 16);\r
170\r
171 // 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
172 // 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
173\r
174 uint8_t a[8] = {1,1,1,1,1,1,1,1 };\r
175 uint8_t b[8] = {0x00};\r
176 uint8_t enc_b[8] = {0x00};\r
177 uint8_t ab[16] = {0x00};\r
178 uint8_t enc_ab[16] = {0x00};\r
179 uint8_t enc_key[8] = {0x00};\r
180 \r
181 uint16_t len;\r
182 uint8_t receivedAnswer[MAX_FRAME_SIZE];\r
183 uint8_t receivedAnswerPar[MAX_PARITY_SIZE];\r
184\r
185 len = mifare_sendcmd_short(NULL, 1, 0x1A, 0x00, receivedAnswer,receivedAnswerPar ,NULL);\r
186 if (len != 11) {\r
187 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Cmd Error: %02x", receivedAnswer[0]);\r
188 OnError(1);\r
189 return;\r
190 }\r
191\r
192 // tag nonce.\r
193 memcpy(enc_b,receivedAnswer+1,8);\r
194\r
195 // decrypt nonce.\r
196 tdes_2key_dec(b, enc_b, 8, key );\r
197\r
198 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
199 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
200 rol(b,8);\r
201 \r
202 memcpy(ab ,a,8);\r
203 memcpy(ab+8,b,8);\r
204\r
205 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
206 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
207\r
208 // encrypt\r
209 tdes_2key_enc(enc_ab, ab, 16, key);\r
210\r
211 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
212 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
213\r
214 len = mifare_sendcmd_short_mfucauth(NULL, 1, 0xAF, enc_ab, receivedAnswer, receivedAnswerPar, NULL);\r
215 if (len != 11) {\r
216 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Cmd Error: %02x", receivedAnswer[0]);\r
217 OnError(1);\r
218 return;\r
219 }\r
220\r
221 // the tags' encryption of our nonce, A.\r
222 memcpy(enc_key, receivedAnswer+1, 8);\r
223 \r
224 // clear B.\r
225 memset(b, 0x00, 8);\r
226\r
227 // decrypt \r
228 tdes_2key_dec(b, enc_key, 8, key );\r
229 if ( memcmp(a, b, 8) == 0 )\r
230 Dbprintf("Verified key");\r
231 else\r
232 Dbprintf("failed authentication");\r
233 \r
234 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
235 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
236 }\r
237\r
238 if( mifare_ultra_readblock(blockNo, dataout) ) {\r
239 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Read block error");\r
240 OnError(2);\r
787b5bd8 241 return;\r
f168b263 242 }\r
243\r
244 if( mifare_ultra_halt() ) {\r
245 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Halt error");\r
246 OnError(3);\r
787b5bd8 247 return;\r
f168b263 248 }\r
249 \r
250 cmd_send(CMD_ACK,1,0,0,dataout,16);\r
7cc204bf 251 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
252 LEDsoff();\r
253}\r
7cc204bf 254//-----------------------------------------------------------------------------\r
baeaf579 255// Select, Authenticate, Read a MIFARE tag. \r
256// read sector (data = 4 x 16 bytes = 64 bytes, or 16 x 16 bytes = 256 bytes)\r
8556b852
M
257//-----------------------------------------------------------------------------\r
258void MifareReadSector(uint8_t arg0, uint8_t arg1, uint8_t arg2, uint8_t *datain)\r
259{\r
260 // params\r
261 uint8_t sectorNo = arg0;\r
262 uint8_t keyType = arg1;\r
263 uint64_t ui64Key = 0;\r
264 ui64Key = bytes_to_num(datain, 6);\r
265 \r
266 // variables\r
3fe4ff4f 267 byte_t isOK = 0;\r
baeaf579 268 byte_t dataoutbuf[16 * 16];\r
1c611bbd 269 uint8_t uid[10];\r
8556b852
M
270 uint32_t cuid;\r
271 struct Crypto1State mpcs = {0, 0};\r
272 struct Crypto1State *pcs;\r
273 pcs = &mpcs;\r
274\r
275 // clear trace\r
3000dc4e 276 clear_trace();\r
8556b852 277\r
7bc95e2e 278 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r
8556b852
M
279\r
280 LED_A_ON();\r
281 LED_B_OFF();\r
282 LED_C_OFF();\r
283\r
baeaf579 284 isOK = 1;\r
285 if(!iso14443a_select_card(uid, NULL, &cuid)) {\r
286 isOK = 0;\r
8556b852 287 if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");\r
baeaf579 288 }\r
8556b852 289\r
baeaf579 290 \r
291 if(isOK && mifare_classic_auth(pcs, cuid, FirstBlockOfSector(sectorNo), keyType, ui64Key, AUTH_FIRST)) {\r
292 isOK = 0;\r
8556b852 293 if (MF_DBGLEVEL >= 1) Dbprintf("Auth error");\r
baeaf579 294 }\r
295 \r
296 for (uint8_t blockNo = 0; isOK && blockNo < NumBlocksPerSector(sectorNo); blockNo++) {\r
297 if(mifare_classic_readblock(pcs, cuid, FirstBlockOfSector(sectorNo) + blockNo, dataoutbuf + 16 * blockNo)) {\r
298 isOK = 0;\r
299 if (MF_DBGLEVEL >= 1) Dbprintf("Read sector %2d block %2d error", sectorNo, blockNo);\r
8556b852 300 break;\r
baeaf579 301 }\r
302 }\r
8556b852 303 \r
baeaf579 304 if(mifare_classic_halt(pcs, cuid)) {\r
8556b852 305 if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");\r
8556b852 306 }\r
baeaf579 307\r
8556b852
M
308 // ----------------------------- crypto1 destroy\r
309 crypto1_destroy(pcs);\r
310 \r
311 if (MF_DBGLEVEL >= 2) DbpString("READ SECTOR FINISHED");\r
312\r
8556b852 313 LED_B_ON();\r
baeaf579 314 cmd_send(CMD_ACK,isOK,0,0,dataoutbuf,16*NumBlocksPerSector(sectorNo));\r
6e82300d 315 LED_B_OFF();\r
8556b852
M
316\r
317 // Thats it...\r
318 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
319 LEDsoff();\r
7cc204bf 320}\r
321\r
787b5bd8 322void MifareUReadCard(uint8_t arg0, int arg1, uint8_t *datain)\r
7cc204bf 323{\r
f168b263 324 // params\r
325 uint8_t sectorNo = arg0;\r
787b5bd8 326 int Pages = arg1;\r
f168b263 327 int countpages = 0;\r
787b5bd8 328 byte_t dataout[176] = {0x00};;\r
7cc204bf 329\r
92690507 330 LEDsoff();\r
331 LED_A_ON();\r
3000dc4e 332 clear_trace();\r
787b5bd8 333 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r
7cc204bf 334\r
92690507 335 int len = iso14443a_select_card(NULL, NULL, NULL);\r
787b5bd8 336 if (!len) {\r
f168b263 337 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Can't select card (RC:%d)",len);\r
338 OnError(1);\r
787b5bd8 339 return;\r
340 }\r
341 \r
342 for (int i = 0; i < Pages; i++){\r
343 \r
f168b263 344 len = mifare_ultra_readblock(sectorNo * 4 + i, dataout + 4 * i);\r
787b5bd8 345 \r
346 if (len) {\r
f168b263 347 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Read block %d error",i);\r
348 OnError(2);\r
787b5bd8 349 return;\r
350 } else {\r
f168b263 351 countpages++;\r
787b5bd8 352 }\r
353 }\r
354 \r
f168b263 355 len = mifare_ultra_halt();\r
787b5bd8 356 if (len) {\r
f168b263 357 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Halt error");\r
358 OnError(3);\r
787b5bd8 359 return;\r
360 }\r
361 \r
92690507 362 if (MF_DBGLEVEL >= MF_DBG_EXTENDED) Dbprintf("Pages read %d", countpages);\r
7cc204bf 363\r
f168b263 364 len = Pages * 4;\r
7cc204bf 365\r
787b5bd8 366 cmd_send(CMD_ACK, 1, 0, 0, dataout, len); \r
31d1caa5
MHS
367 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
368 LEDsoff();\r
7cc204bf 369}\r
370\r
7cc204bf 371//-----------------------------------------------------------------------------\r
baeaf579 372// Select, Authenticate, Write a MIFARE tag. \r
7cc204bf 373// read block\r
8556b852
M
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
1c611bbd 388 uint8_t uid[10];\r
8556b852
M
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
3000dc4e 395 clear_trace();\r
8556b852 396\r
7bc95e2e 397 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r
8556b852
M
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
8556b852 433 LED_B_ON();\r
9492e0b0 434 cmd_send(CMD_ACK,isOK,0,0,0,0);\r
6e82300d 435 LED_B_OFF();\r
8556b852
M
436\r
437\r
438 // Thats it...\r
439 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
440 LEDsoff();\r
7cc204bf 441}\r
442\r
443void MifareUWriteBlock(uint8_t arg0, uint8_t *datain)\r
444{\r
f168b263 445 uint8_t blockNo = arg0;\r
787b5bd8 446 byte_t blockdata[16] = {0x00};\r
7cc204bf 447\r
f168b263 448 memcpy(blockdata, datain, 16);\r
449\r
787b5bd8 450 uint8_t uid[10] = {0x00};\r
7cc204bf 451\r
f168b263 452 LED_A_ON(); LED_B_OFF(); LED_C_OFF();\r
7cc204bf 453\r
f168b263 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
7cc204bf 479}\r
480\r
481void MifareUWriteBlock_Special(uint8_t arg0, uint8_t *datain)\r
482{\r
baeaf579 483 uint8_t blockNo = arg0;\r
787b5bd8 484 byte_t blockdata[4] = {0x00};\r
baeaf579 485 \r
7cc204bf 486 memcpy(blockdata, datain,4);\r
487\r
787b5bd8 488 uint8_t uid[10] = {0x00};\r
f168b263 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
7cc204bf 499\r
f168b263 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
3000dc4e 527 clear_trace();\r
baeaf579 528 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r
7cc204bf 529\r
f168b263 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
7cc204bf 535\r
f168b263 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
7cc204bf 545\r
f168b263 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
7cc204bf 555\r
f168b263 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
7cc204bf 565\r
f168b263 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
7cc204bf 575\r
f168b263 576 if(mifare_ultra_halt()) {\r
577 if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");\r
578 OnError(0);\r
579 return;\r
580 };\r
7cc204bf 581\r
f168b263 582 cmd_send(CMD_ACK,1,0,0,0,0);\r
baeaf579 583 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
584 LEDsoff();\r
7cc204bf 585}\r
586\r
587// Return 1 if the nonce is invalid else return 0\r
6a1f2d82 588int valid_nonce(uint32_t Nt, uint32_t NtEnc, uint32_t Ks1, uint8_t *parity) {\r
7cc204bf 589 return ((oddparity((Nt >> 24) & 0xFF) == ((parity[0]) ^ oddparity((NtEnc >> 24) & 0xFF) ^ BIT(Ks1,16))) & \\r
8556b852
M
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
9492e0b0 594\r
8556b852
M
595//-----------------------------------------------------------------------------\r
596// MIFARE nested authentication. \r
597// \r
598//-----------------------------------------------------------------------------\r
9492e0b0 599void MifareNested(uint32_t arg0, uint32_t arg1, uint32_t calibrate, uint8_t *datain)\r
8556b852
M
600{\r
601 // params\r
9492e0b0 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
8556b852
M
606 uint64_t ui64Key = 0;\r
607\r
608 ui64Key = bytes_to_num(datain, 6);\r
609 \r
610 // variables\r
9492e0b0 611 uint16_t rtr, i, j, len;\r
612 uint16_t davg;\r
613 static uint16_t dmin, dmax;\r
1c611bbd 614 uint8_t uid[10];\r
6a1f2d82 615 uint32_t cuid, nt1, nt2, nttmp, nttest, ks1;\r
616 uint8_t par[1];\r
9492e0b0 617 uint32_t target_nt[2], target_ks[2];\r
618 \r
8556b852 619 uint8_t par_array[4];\r
9492e0b0 620 uint16_t ncount = 0;\r
8556b852
M
621 struct Crypto1State mpcs = {0, 0};\r
622 struct Crypto1State *pcs;\r
623 pcs = &mpcs;\r
f71f4deb 624 uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];\r
8556b852 625\r
9492e0b0 626 uint32_t auth1_time, auth2_time;\r
627 static uint16_t delta_time;\r
628\r
f71f4deb 629 // free eventually allocated BigBuf memory\r
630 BigBuf_free();\r
8556b852 631 // clear trace\r
3000dc4e
MHS
632 clear_trace();\r
633 set_tracing(false);\r
8556b852 634 \r
7bc95e2e 635 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r
8556b852
M
636\r
637 LED_A_ON();\r
8556b852
M
638 LED_C_OFF();\r
639\r
8556b852 640\r
9492e0b0 641 // statistics on nonce distance\r
642 if (calibrate) { // for first call only. Otherwise reuse previous calibration\r
643 LED_B_ON();\r
3fe4ff4f 644 WDT_HIT();\r
8556b852 645\r
9492e0b0 646 davg = dmax = 0;\r
647 dmin = 2000;\r
648 delta_time = 0;\r
8556b852 649 \r
9492e0b0 650 for (rtr = 0; rtr < 17; rtr++) {\r
8556b852 651\r
9492e0b0 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
b19bd5d6 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
9492e0b0 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
8556b852
M
700 }\r
701 \r
9492e0b0 702 if (rtr <= 1) return;\r
8556b852 703\r
9492e0b0 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
8556b852 707\r
9492e0b0 708 dmin = davg - 2;\r
709 dmax = davg + 2;\r
710 \r
711 LED_B_OFF();\r
712 \r
713 }\r
8556b852
M
714// ------------------------------------------------------------------------------------------------- \r
715 \r
716 LED_C_ON();\r
717\r
718 // get crypted nonces for target sector\r
9492e0b0 719 for(i=0; i < 2; i++) { // look for exactly two different nonces\r
8556b852 720\r
9492e0b0 721 target_nt[i] = 0;\r
722 while(target_nt[i] == 0) { // continue until we have an unambiguous nonce\r
8556b852 723 \r
9492e0b0 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
8556b852 729\r
9492e0b0 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
8556b852 734 \r
9492e0b0 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
8556b852 740\r
9492e0b0 741 // nested authentication\r
742 auth2_time = auth1_time + delta_time;\r
6a1f2d82 743 len = mifare_sendcmd_shortex(pcs, AUTH_NESTED, 0x60 + (targetKeyType & 0x01), targetBlockNo, receivedAnswer, par, &auth2_time);\r
9492e0b0 744 if (len != 4) {\r
745 if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Auth2 error len=%d", len);\r
746 continue;\r
747 };\r
8556b852 748 \r
9492e0b0 749 nt2 = bytes_to_num(receivedAnswer, 4); \r
6a1f2d82 750 if (MF_DBGLEVEL >= 3) Dbprintf("Nonce#%d: Testing nt1=%08x nt2enc=%08x nt2par=%02x", i+1, nt1, nt2, par[0]);\r
8556b852 751 \r
9492e0b0 752 // Parity validity check\r
753 for (j = 0; j < 4; j++) {\r
6a1f2d82 754 par_array[j] = (oddparity(receivedAnswer[j]) != ((par[0] >> (7-j)) & 0x01));\r
9492e0b0 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
8556b852
M
775 break;\r
776 }\r
9492e0b0 777 if (MF_DBGLEVEL >= 3) Dbprintf("Nonce#%d: valid, ntdist=%d", i+1, j);\r
8556b852 778 }\r
8556b852 779 }\r
9492e0b0 780 if (target_nt[i] == 0 && j == dmax+1 && MF_DBGLEVEL >= 3) Dbprintf("Nonce#%d: dismissed (all invalid)", i+1);\r
8556b852
M
781 }\r
782 }\r
783\r
784 LED_C_OFF();\r
785 \r
786 // ----------------------------- crypto1 destroy\r
787 crypto1_destroy(pcs);\r
788 \r
9492e0b0 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
8556b852
M
795 \r
796 LED_B_ON();\r
9492e0b0 797 cmd_send(CMD_ACK, 0, 2, targetBlockNo + (targetKeyType * 0x100), buf, sizeof(buf));\r
6e82300d 798 LED_B_OFF();\r
8556b852 799\r
9492e0b0 800 if (MF_DBGLEVEL >= 3) DbpString("NESTED FINISHED");\r
8556b852 801\r
8556b852
M
802 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
803 LEDsoff();\r
3000dc4e 804 set_tracing(TRUE);\r
8556b852
M
805}\r
806\r
807//-----------------------------------------------------------------------------\r
9492e0b0 808// MIFARE check keys. key count up to 85. \r
8556b852
M
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
1c611bbd 822 uint8_t uid[10];\r
8556b852
M
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
3000dc4e
MHS
833 clear_trace();\r
834 set_tracing(TRUE);\r
8556b852 835\r
7bc95e2e 836 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r
8556b852
M
837\r
838 LED_A_ON();\r
839 LED_B_OFF();\r
840 LED_C_OFF();\r
841\r
8556b852 842 for (i = 0; i < keyCount; i++) {\r
9492e0b0 843 if(mifare_classic_halt(pcs, cuid)) {\r
844 if (MF_DBGLEVEL >= 1) Dbprintf("ChkKeys: Halt error");\r
845 }\r
8556b852
M
846\r
847 if(!iso14443a_select_card(uid, NULL, &cuid)) {\r
9492e0b0 848 if (OLD_MF_DBGLEVEL >= 1) Dbprintf("ChkKeys: Can't select card");\r
8556b852
M
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
8556b852 864 LED_B_ON();\r
6e82300d 865 cmd_send(CMD_ACK,isOK,0,0,datain + i * 6,6);\r
8556b852
M
866 LED_B_OFF();\r
867\r
8556b852
M
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
3fe4ff4f 897 byte_t buf[USB_CMD_DATA_SIZE];\r
baeaf579 898 emlGetMem(buf, arg0, arg1); // data, block num, blocks count (max 4)\r
8556b852
M
899\r
900 LED_B_ON();\r
3fe4ff4f 901 cmd_send(CMD_ACK,arg0,arg1,0,buf,USB_CMD_DATA_SIZE);\r
8556b852
M
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
baeaf579 910 uint8_t numSectors = arg0;\r
8556b852
M
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
ab8b654e 920 byte_t dataoutbuf2[16];\r
1c611bbd 921 uint8_t uid[10];\r
8556b852
M
922\r
923 // clear trace\r
3000dc4e
MHS
924 clear_trace();\r
925 set_tracing(false);\r
8556b852 926 \r
7bc95e2e 927 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r
8556b852
M
928\r
929 LED_A_ON();\r
930 LED_B_OFF();\r
931 LED_C_OFF();\r
932 \r
baeaf579 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
8556b852 939 \r
baeaf579 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
8556b852 946 break;\r
baeaf579 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
8556b852 952 break;\r
baeaf579 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
ab8b654e
M
960 break;\r
961 };\r
baeaf579 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
8556b852
M
971 }\r
972\r
baeaf579 973 }\r
974\r
975 if(mifare_classic_halt(pcs, cuid)) {\r
976 if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");\r
977 };\r
8556b852
M
978\r
979 // ----------------------------- crypto1 destroy\r
980 crypto1_destroy(pcs);\r
ab8b654e
M
981\r
982 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
983 LEDsoff();\r
8556b852
M
984 \r
985 if (MF_DBGLEVEL >= 2) DbpString("EMUL FILL SECTORS FINISHED");\r
986\r
8556b852
M
987}\r
988\r
0675f200
M
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
208a0166
M
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
0675f200
M
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
3fe4ff4f 1013 uint8_t uid[10] = {0x00};\r
1014 uint8_t d_block[18] = {0x00};\r
0675f200
M
1015 uint32_t cuid;\r
1016 \r
f71f4deb 1017 uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];\r
1018 uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE];\r
6a1f2d82 1019\r
3fe4ff4f 1020 // reset FPGA and LED\r
208a0166 1021 if (workFlags & 0x08) {\r
208a0166
M
1022 LED_A_ON();\r
1023 LED_B_OFF();\r
1024 LED_C_OFF();\r
0675f200 1025 \r
3000dc4e
MHS
1026 clear_trace();\r
1027 set_tracing(TRUE);\r
3fe4ff4f 1028 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r
208a0166 1029 }\r
0675f200
M
1030\r
1031 while (true) {\r
3fe4ff4f 1032\r
0675f200 1033 // get UID from chip\r
208a0166 1034 if (workFlags & 0x01) {\r
0675f200
M
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
6a1f2d82 1048 ReaderTransmitBitsPar(wupC1,7,0, NULL);\r
1049 if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != 0x0a)) {\r
0675f200
M
1050 if (MF_DBGLEVEL >= 1) Dbprintf("wupC1 error");\r
1051 break;\r
1052 };\r
1053\r
9492e0b0 1054 ReaderTransmit(wipeC, sizeof(wipeC), NULL);\r
6a1f2d82 1055 if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != 0x0a)) {\r
0675f200
M
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
545a1f38 1066 // write block\r
208a0166 1067 if (workFlags & 0x02) {\r
6a1f2d82 1068 ReaderTransmitBitsPar(wupC1,7,0, NULL);\r
1069 if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != 0x0a)) {\r
208a0166
M
1070 if (MF_DBGLEVEL >= 1) Dbprintf("wupC1 error");\r
1071 break;\r
1072 };\r
0675f200 1073\r
9492e0b0 1074 ReaderTransmit(wupC2, sizeof(wupC2), NULL);\r
6a1f2d82 1075 if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != 0x0a)) {\r
208a0166
M
1076 if (MF_DBGLEVEL >= 1) Dbprintf("wupC2 error");\r
1077 break;\r
1078 };\r
1079 }\r
0675f200 1080\r
6a1f2d82 1081 if ((mifare_sendcmd_short(NULL, 0, 0xA0, blockNo, receivedAnswer, receivedAnswerPar, NULL) != 1) || (receivedAnswer[0] != 0x0a)) {\r
0675f200
M
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
9492e0b0 1089 ReaderTransmit(d_block, sizeof(d_block), NULL);\r
6a1f2d82 1090 if ((ReaderReceive(receivedAnswer, receivedAnswerPar) != 1) || (receivedAnswer[0] != 0x0a)) {\r
0675f200
M
1091 if (MF_DBGLEVEL >= 1) Dbprintf("write block send data error");\r
1092 break;\r
1093 }; \r
1094 \r
208a0166
M
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
0675f200
M
1101 \r
1102 isOK = 1;\r
1103 break;\r
1104 }\r
1105 \r
0675f200 1106 LED_B_ON();\r
baeaf579 1107 cmd_send(CMD_ACK,isOK,0,0,uid,4);\r
0675f200
M
1108 LED_B_OFF();\r
1109\r
545a1f38 1110 if ((workFlags & 0x10) || (!isOK)) {\r
208a0166
M
1111 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
1112 LEDsoff();\r
1113 }\r
0675f200 1114}\r
545a1f38 1115\r
baeaf579 1116\r
545a1f38
M
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
3fe4ff4f 1133 uint8_t data[18] = {0x00};\r
545a1f38
M
1134 uint32_t cuid = 0;\r
1135 \r
f71f4deb 1136 uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];\r
1137 uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE];\r
545a1f38
M
1138 \r
1139 if (workFlags & 0x08) {\r
545a1f38
M
1140 LED_A_ON();\r
1141 LED_B_OFF();\r
1142 LED_C_OFF();\r
1143 \r
3000dc4e
MHS
1144 clear_trace();\r
1145 set_tracing(TRUE);\r
3fe4ff4f 1146 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r
545a1f38
M
1147 }\r
1148\r
1149 while (true) {\r
1150 if (workFlags & 0x02) {\r
7bc95e2e 1151 ReaderTransmitBitsPar(wupC1,7,0, NULL);\r
6a1f2d82 1152 if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != 0x0a)) {\r
545a1f38
M
1153 if (MF_DBGLEVEL >= 1) Dbprintf("wupC1 error");\r
1154 break;\r
1155 };\r
1156\r
9492e0b0 1157 ReaderTransmit(wupC2, sizeof(wupC2), NULL);\r
6a1f2d82 1158 if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != 0x0a)) {\r
545a1f38
M
1159 if (MF_DBGLEVEL >= 1) Dbprintf("wupC2 error");\r
1160 break;\r
1161 };\r
1162 }\r
1163\r
1164 // read block\r
6a1f2d82 1165 if ((mifare_sendcmd_short(NULL, 0, 0x30, blockNo, receivedAnswer, receivedAnswerPar, NULL) != 18)) {\r
545a1f38
M
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
545a1f38 1182 LED_B_ON();\r
baeaf579 1183 cmd_send(CMD_ACK,isOK,0,0,data,18);\r
545a1f38
M
1184 LED_B_OFF();\r
1185\r
1186 if ((workFlags & 0x10) || (!isOK)) {\r
545a1f38
M
1187 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
1188 LEDsoff();\r
1189 }\r
1190}\r
1191\r
3fe4ff4f 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
f71f4deb 1201 uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];\r
1202 uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE];\r
3fe4ff4f 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
a631936e 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;\r
1230 \r
3000dc4e 1231 clear_trace();\r
a631936e 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) \r
1237 Dbprintf("Can't select card");\r
c8b6da22 1238 //OnError(1);\r
a631936e 1239 return;\r
1240 };\r
1241\r
1242 if(mifare_desfire_des_auth1(cuid, dataout)){\r
1243 if (MF_DBGLEVEL >= MF_DBG_ERROR) \r
1244 Dbprintf("Authentication part1: Fail.");\r
c8b6da22 1245 //OnError(4);\r
a631936e 1246 return;\r
1247 }\r
1248\r
1249 if (MF_DBGLEVEL >= MF_DBG_EXTENDED) DbpString("AUTH 1 FINISHED");\r
1250 \r
1251 cmd_send(CMD_ACK,1,cuid,0,dataout, sizeof(dataout));\r
1252}\r
1253\r
1254void Mifare_DES_Auth2(uint32_t arg0, uint8_t *datain){\r
1255\r
1256 uint32_t cuid = arg0;\r
1257 uint8_t key[16] = {0x00};\r
1258 byte_t isOK = 0;\r
1259 byte_t dataout[12] = {0x00};\r
1260 \r
1261 memcpy(key, datain, 16);\r
1262 \r
1263 isOK = mifare_desfire_des_auth2(cuid, key, dataout);\r
1264 \r
1265 if( isOK) {\r
1266 if (MF_DBGLEVEL >= MF_DBG_EXTENDED) \r
1267 Dbprintf("Authentication part2: Failed"); \r
c8b6da22 1268 //OnError(4);\r
a631936e 1269 return;\r
1270 }\r
1271\r
1272 if (MF_DBGLEVEL >= MF_DBG_EXTENDED) \r
1273 DbpString("AUTH 2 FINISHED");\r
1274\r
1275 cmd_send(CMD_ACK, isOK, 0, 0, dataout, sizeof(dataout));\r
1276 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
1277 LEDsoff();\r
3000dc4e 1278}\r
f168b263 1279\r
1280void OnSuccess(){\r
1281 pcb_blocknum = 0;\r
1282 ReaderTransmit(deselect_cmd, 3 , NULL);\r
1283 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
1284 LEDsoff();\r
1285}\r
1286\r
1287void OnError(uint8_t reason){\r
1288 pcb_blocknum = 0;\r
1289 ReaderTransmit(deselect_cmd, 3 , NULL);\r
1290 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
1291 cmd_send(CMD_ACK,0,reason,0,0,0);\r
1292 LEDsoff();\r
1293}\r
Impressum, Datenschutz