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