]>
Commit | Line | Data |
---|---|---|
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 | \r | |
18 | #include "util.h"\r | |
19 | #include "parity.h"\r | |
20 | #include "crc.h"\r | |
21 | #include "fpgaloader.h"\r | |
22 | \r | |
23 | #define HARDNESTED_AUTHENTICATION_TIMEOUT 848 // card times out 1ms after wrong authentication (according to NXP documentation)\r | |
24 | #define HARDNESTED_PRE_AUTHENTICATION_LEADTIME 400 // some (non standard) cards need a pause after select before they are ready for first authentication \r | |
25 | \r | |
26 | /*\r | |
27 | // the block number for the ISO14443-4 PCB\r | |
28 | static uint8_t pcb_blocknum = 0;\r | |
29 | // Deselect card by sending a s-block. the crc is precalced for speed\r | |
30 | static uint8_t deselect_cmd[] = {0xc2,0xe0,0xb4};\r | |
31 | \r | |
32 | static void OnSuccess(){\r | |
33 | pcb_blocknum = 0;\r | |
34 | ReaderTransmit(deselect_cmd, 3 , NULL);\r | |
35 | FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r | |
36 | LEDsoff();\r | |
37 | }\r | |
38 | */\r | |
39 | \r | |
40 | static void OnError(uint8_t reason){\r | |
41 | // pcb_blocknum = 0;\r | |
42 | // ReaderTransmit(deselect_cmd, 3 , NULL);\r | |
43 | FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r | |
44 | LED_D_OFF();\r | |
45 | cmd_send(CMD_ACK,0,reason,0,0,0);\r | |
46 | LED_A_OFF();\r | |
47 | }\r | |
48 | \r | |
49 | //-----------------------------------------------------------------------------\r | |
50 | // Select, Authenticate, Read a MIFARE tag.\r | |
51 | // read block\r | |
52 | //-----------------------------------------------------------------------------\r | |
53 | void MifareReadBlock(uint8_t arg0, uint8_t arg1, uint8_t arg2, uint8_t *datain)\r | |
54 | {\r | |
55 | LED_A_ON();\r | |
56 | \r | |
57 | uint8_t blockNo = arg0;\r | |
58 | uint8_t keyType = arg1;\r | |
59 | uint64_t ui64Key = 0;\r | |
60 | ui64Key = bytes_to_num(datain, 6);\r | |
61 | \r | |
62 | byte_t isOK = 0;\r | |
63 | byte_t dataoutbuf[16];\r | |
64 | uint8_t uid[10];\r | |
65 | uint32_t cuid;\r | |
66 | struct Crypto1State mpcs = {0, 0};\r | |
67 | struct Crypto1State *pcs;\r | |
68 | pcs = &mpcs;\r | |
69 | \r | |
70 | iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r | |
71 | \r | |
72 | clear_trace();\r | |
73 | \r | |
74 | while (true) {\r | |
75 | if(!iso14443a_select_card(uid, NULL, &cuid, true, 0, true)) {\r | |
76 | if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");\r | |
77 | break;\r | |
78 | };\r | |
79 | \r | |
80 | if(mifare_classic_auth(pcs, cuid, blockNo, keyType, ui64Key, AUTH_FIRST)) {\r | |
81 | if (MF_DBGLEVEL >= 1) Dbprintf("Auth error");\r | |
82 | break;\r | |
83 | };\r | |
84 | \r | |
85 | if(mifare_classic_readblock(pcs, cuid, blockNo, dataoutbuf)) {\r | |
86 | if (MF_DBGLEVEL >= 1) Dbprintf("Read block error");\r | |
87 | break;\r | |
88 | };\r | |
89 | \r | |
90 | if(mifare_classic_halt(pcs, cuid)) {\r | |
91 | if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");\r | |
92 | break;\r | |
93 | };\r | |
94 | \r | |
95 | isOK = 1;\r | |
96 | break;\r | |
97 | }\r | |
98 | \r | |
99 | // ----------------------------- crypto1 destroy\r | |
100 | crypto1_destroy(pcs);\r | |
101 | \r | |
102 | if (MF_DBGLEVEL >= 2) DbpString("READ BLOCK FINISHED");\r | |
103 | \r | |
104 | LED_B_ON();\r | |
105 | cmd_send(CMD_ACK,isOK,0,0,dataoutbuf,16);\r | |
106 | LED_B_OFF();\r | |
107 | \r | |
108 | FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r | |
109 | LEDsoff();\r | |
110 | }\r | |
111 | \r | |
112 | void MifareUC_Auth(uint8_t arg0, uint8_t *keybytes){\r | |
113 | \r | |
114 | LED_A_ON();\r | |
115 | bool turnOffField = (arg0 == 1);\r | |
116 | \r | |
117 | iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r | |
118 | \r | |
119 | if (!iso14443a_select_card(NULL, NULL, NULL, true, 0, true)) {\r | |
120 | if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Can't select card");\r | |
121 | OnError(0);\r | |
122 | return;\r | |
123 | };\r | |
124 | \r | |
125 | if (!mifare_ultra_auth(keybytes)){\r | |
126 | if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Authentication failed");\r | |
127 | OnError(1);\r | |
128 | return;\r | |
129 | }\r | |
130 | \r | |
131 | if (turnOffField) {\r | |
132 | FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r | |
133 | LED_D_OFF();\r | |
134 | }\r | |
135 | \r | |
136 | cmd_send(CMD_ACK,1,0,0,0,0);\r | |
137 | LED_A_OFF();\r | |
138 | }\r | |
139 | \r | |
140 | // Arg0 = BlockNo,\r | |
141 | // Arg1 = UsePwd bool\r | |
142 | // datain = PWD bytes,\r | |
143 | void MifareUReadBlock(uint8_t arg0, uint8_t arg1, uint8_t *datain)\r | |
144 | {\r | |
145 | LED_A_ON();\r | |
146 | \r | |
147 | uint8_t blockNo = arg0;\r | |
148 | byte_t dataout[16] = {0x00};\r | |
149 | bool useKey = (arg1 == 1); //UL_C\r | |
150 | bool usePwd = (arg1 == 2); //UL_EV1/NTAG\r | |
151 | \r | |
152 | iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r | |
153 | \r | |
154 | int len = iso14443a_select_card(NULL, NULL, NULL, true, 0, true);\r | |
155 | if(!len) {\r | |
156 | if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Can't select card (RC:%02X)",len);\r | |
157 | OnError(1);\r | |
158 | return;\r | |
159 | }\r | |
160 | \r | |
161 | // UL-C authentication\r | |
162 | if (useKey) {\r | |
163 | uint8_t key[16] = {0x00};\r | |
164 | memcpy(key, datain, sizeof(key) );\r | |
165 | \r | |
166 | if ( !mifare_ultra_auth(key) ) {\r | |
167 | OnError(1);\r | |
168 | return;\r | |
169 | }\r | |
170 | }\r | |
171 | \r | |
172 | // UL-EV1 / NTAG authentication\r | |
173 | if (usePwd) {\r | |
174 | uint8_t pwd[4] = {0x00};\r | |
175 | memcpy(pwd, datain, 4);\r | |
176 | uint8_t pack[4] = {0,0,0,0};\r | |
177 | if (!mifare_ul_ev1_auth(pwd, pack)) {\r | |
178 | OnError(1);\r | |
179 | return;\r | |
180 | }\r | |
181 | }\r | |
182 | \r | |
183 | if (mifare_ultra_readblock(blockNo, dataout)) {\r | |
184 | if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Read block error");\r | |
185 | OnError(2);\r | |
186 | return;\r | |
187 | }\r | |
188 | \r | |
189 | if (mifare_ultra_halt()) {\r | |
190 | if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Halt error");\r | |
191 | OnError(3);\r | |
192 | return;\r | |
193 | }\r | |
194 | \r | |
195 | cmd_send(CMD_ACK,1,0,0,dataout,16);\r | |
196 | FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r | |
197 | LED_D_OFF();\r | |
198 | LED_A_OFF();\r | |
199 | }\r | |
200 | \r | |
201 | //-----------------------------------------------------------------------------\r | |
202 | // Select, Authenticate, Read a MIFARE tag.\r | |
203 | // read sector (data = 4 x 16 bytes = 64 bytes, or 16 x 16 bytes = 256 bytes)\r | |
204 | //-----------------------------------------------------------------------------\r | |
205 | void MifareReadSector(uint8_t arg0, uint8_t arg1, uint8_t arg2, uint8_t *datain)\r | |
206 | {\r | |
207 | // params\r | |
208 | uint8_t sectorNo = arg0;\r | |
209 | uint8_t keyType = arg1;\r | |
210 | uint64_t ui64Key = 0;\r | |
211 | ui64Key = bytes_to_num(datain, 6);\r | |
212 | \r | |
213 | // variables\r | |
214 | byte_t isOK = 0;\r | |
215 | byte_t dataoutbuf[16 * 16];\r | |
216 | uint8_t uid[10];\r | |
217 | uint32_t cuid;\r | |
218 | struct Crypto1State mpcs = {0, 0};\r | |
219 | struct Crypto1State *pcs;\r | |
220 | pcs = &mpcs;\r | |
221 | \r | |
222 | iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r | |
223 | \r | |
224 | clear_trace();\r | |
225 | \r | |
226 | LED_A_ON();\r | |
227 | LED_B_OFF();\r | |
228 | LED_C_OFF();\r | |
229 | \r | |
230 | isOK = 1;\r | |
231 | if(!iso14443a_select_card(uid, NULL, &cuid, true, 0, true)) {\r | |
232 | isOK = 0;\r | |
233 | if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");\r | |
234 | }\r | |
235 | \r | |
236 | \r | |
237 | if(isOK && mifare_classic_auth(pcs, cuid, FirstBlockOfSector(sectorNo), keyType, ui64Key, AUTH_FIRST)) {\r | |
238 | isOK = 0;\r | |
239 | if (MF_DBGLEVEL >= 1) Dbprintf("Auth error");\r | |
240 | }\r | |
241 | \r | |
242 | for (uint8_t blockNo = 0; isOK && blockNo < NumBlocksPerSector(sectorNo); blockNo++) {\r | |
243 | if(mifare_classic_readblock(pcs, cuid, FirstBlockOfSector(sectorNo) + blockNo, dataoutbuf + 16 * blockNo)) {\r | |
244 | isOK = 0;\r | |
245 | if (MF_DBGLEVEL >= 1) Dbprintf("Read sector %2d block %2d error", sectorNo, blockNo);\r | |
246 | break;\r | |
247 | }\r | |
248 | }\r | |
249 | \r | |
250 | if(mifare_classic_halt(pcs, cuid)) {\r | |
251 | if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");\r | |
252 | }\r | |
253 | \r | |
254 | // ----------------------------- crypto1 destroy\r | |
255 | crypto1_destroy(pcs);\r | |
256 | \r | |
257 | if (MF_DBGLEVEL >= 2) DbpString("READ SECTOR FINISHED");\r | |
258 | \r | |
259 | LED_B_ON();\r | |
260 | cmd_send(CMD_ACK,isOK,0,0,dataoutbuf,16*NumBlocksPerSector(sectorNo));\r | |
261 | LED_B_OFF();\r | |
262 | \r | |
263 | // Thats it...\r | |
264 | FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r | |
265 | LEDsoff();\r | |
266 | }\r | |
267 | \r | |
268 | // arg0 = blockNo (start)\r | |
269 | // arg1 = Pages (number of blocks)\r | |
270 | // arg2 = useKey\r | |
271 | // datain = KEY bytes\r | |
272 | void MifareUReadCard(uint8_t arg0, uint16_t arg1, uint8_t arg2, uint8_t *datain)\r | |
273 | {\r | |
274 | LED_A_ON();\r | |
275 | iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r | |
276 | \r | |
277 | // free eventually allocated BigBuf memory\r | |
278 | BigBuf_free();\r | |
279 | \r | |
280 | // params\r | |
281 | uint8_t blockNo = arg0;\r | |
282 | uint16_t blocks = arg1;\r | |
283 | bool useKey = (arg2 == 1); //UL_C\r | |
284 | bool usePwd = (arg2 == 2); //UL_EV1/NTAG\r | |
285 | uint32_t countblocks = 0;\r | |
286 | uint8_t *dataout = BigBuf_malloc(CARD_MEMORY_SIZE);\r | |
287 | if (dataout == NULL){\r | |
288 | Dbprintf("out of memory");\r | |
289 | OnError(1);\r | |
290 | return;\r | |
291 | }\r | |
292 | \r | |
293 | int len = iso14443a_select_card(NULL, NULL, NULL, true, 0, true);\r | |
294 | if (!len) {\r | |
295 | if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Can't select card (RC:%d)",len);\r | |
296 | OnError(1);\r | |
297 | return;\r | |
298 | }\r | |
299 | \r | |
300 | // UL-C authentication\r | |
301 | if (useKey) {\r | |
302 | uint8_t key[16] = {0x00};\r | |
303 | memcpy(key, datain, sizeof(key) );\r | |
304 | \r | |
305 | if ( !mifare_ultra_auth(key) ) {\r | |
306 | OnError(1);\r | |
307 | return;\r | |
308 | }\r | |
309 | }\r | |
310 | \r | |
311 | // UL-EV1 / NTAG authentication\r | |
312 | if (usePwd) {\r | |
313 | uint8_t pwd[4] = {0x00};\r | |
314 | memcpy(pwd, datain, sizeof(pwd));\r | |
315 | uint8_t pack[4] = {0,0,0,0};\r | |
316 | \r | |
317 | if (!mifare_ul_ev1_auth(pwd, pack)){\r | |
318 | OnError(1);\r | |
319 | return;\r | |
320 | }\r | |
321 | }\r | |
322 | \r | |
323 | for (int i = 0; i < blocks; i++){\r | |
324 | if ((i*4) + 4 >= CARD_MEMORY_SIZE) {\r | |
325 | Dbprintf("Data exceeds buffer!!");\r | |
326 | break;\r | |
327 | }\r | |
328 | \r | |
329 | len = mifare_ultra_readblock(blockNo + i, dataout + 4 * i);\r | |
330 | \r | |
331 | if (len) {\r | |
332 | if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Read block %d error",i);\r | |
333 | // if no blocks read - error out\r | |
334 | if (i==0){\r | |
335 | OnError(2);\r | |
336 | return;\r | |
337 | } else {\r | |
338 | //stop at last successful read block and return what we got\r | |
339 | break;\r | |
340 | }\r | |
341 | } else {\r | |
342 | countblocks++;\r | |
343 | }\r | |
344 | }\r | |
345 | \r | |
346 | len = mifare_ultra_halt();\r | |
347 | if (len) {\r | |
348 | if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Halt error");\r | |
349 | OnError(3);\r | |
350 | return;\r | |
351 | }\r | |
352 | \r | |
353 | if (MF_DBGLEVEL >= MF_DBG_DEBUG) Dbprintf("Blocks read %d", countblocks);\r | |
354 | \r | |
355 | cmd_send(CMD_ACK, 1, countblocks*4, BigBuf_max_traceLen(), 0, 0);\r | |
356 | \r | |
357 | FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r | |
358 | LED_D_OFF();\r | |
359 | BigBuf_free();\r | |
360 | LED_A_OFF();\r | |
361 | }\r | |
362 | \r | |
363 | //-----------------------------------------------------------------------------\r | |
364 | // Select, Authenticate, Write a MIFARE tag.\r | |
365 | // read block\r | |
366 | //-----------------------------------------------------------------------------\r | |
367 | void MifareWriteBlock(uint8_t arg0, uint8_t arg1, uint8_t arg2, uint8_t *datain)\r | |
368 | {\r | |
369 | // params\r | |
370 | uint8_t blockNo = arg0;\r | |
371 | uint8_t keyType = arg1;\r | |
372 | uint64_t ui64Key = 0;\r | |
373 | byte_t blockdata[16];\r | |
374 | \r | |
375 | ui64Key = bytes_to_num(datain, 6);\r | |
376 | memcpy(blockdata, datain + 10, 16);\r | |
377 | \r | |
378 | // variables\r | |
379 | byte_t isOK = 0;\r | |
380 | uint8_t uid[10];\r | |
381 | uint32_t cuid;\r | |
382 | struct Crypto1State mpcs = {0, 0};\r | |
383 | struct Crypto1State *pcs;\r | |
384 | pcs = &mpcs;\r | |
385 | \r | |
386 | iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r | |
387 | \r | |
388 | clear_trace();\r | |
389 | \r | |
390 | LED_A_ON();\r | |
391 | LED_B_OFF();\r | |
392 | LED_C_OFF();\r | |
393 | \r | |
394 | while (true) {\r | |
395 | if(!iso14443a_select_card(uid, NULL, &cuid, true, 0, true)) {\r | |
396 | if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");\r | |
397 | break;\r | |
398 | };\r | |
399 | \r | |
400 | if(mifare_classic_auth(pcs, cuid, blockNo, keyType, ui64Key, AUTH_FIRST)) {\r | |
401 | if (MF_DBGLEVEL >= 1) Dbprintf("Auth error");\r | |
402 | break;\r | |
403 | };\r | |
404 | \r | |
405 | if(mifare_classic_writeblock(pcs, cuid, blockNo, blockdata)) {\r | |
406 | if (MF_DBGLEVEL >= 1) Dbprintf("Write block error");\r | |
407 | break;\r | |
408 | };\r | |
409 | \r | |
410 | if(mifare_classic_halt(pcs, cuid)) {\r | |
411 | if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");\r | |
412 | break;\r | |
413 | };\r | |
414 | \r | |
415 | isOK = 1;\r | |
416 | break;\r | |
417 | }\r | |
418 | \r | |
419 | // ----------------------------- crypto1 destroy\r | |
420 | crypto1_destroy(pcs);\r | |
421 | \r | |
422 | if (MF_DBGLEVEL >= 2) DbpString("WRITE BLOCK FINISHED");\r | |
423 | \r | |
424 | LED_B_ON();\r | |
425 | cmd_send(CMD_ACK,isOK,0,0,0,0);\r | |
426 | LED_B_OFF();\r | |
427 | \r | |
428 | \r | |
429 | // Thats it...\r | |
430 | FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r | |
431 | LEDsoff();\r | |
432 | }\r | |
433 | \r | |
434 | /* // Command not needed but left for future testing\r | |
435 | void MifareUWriteBlockCompat(uint8_t arg0, uint8_t *datain)\r | |
436 | {\r | |
437 | uint8_t blockNo = arg0;\r | |
438 | byte_t blockdata[16] = {0x00};\r | |
439 | \r | |
440 | memcpy(blockdata, datain, 16);\r | |
441 | \r | |
442 | uint8_t uid[10] = {0x00};\r | |
443 | \r | |
444 | LED_A_ON(); LED_B_OFF(); LED_C_OFF();\r | |
445 | \r | |
446 | clear_trace();\r | |
447 | iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r | |
448 | \r | |
449 | if(!iso14443a_select_card(uid, NULL, NULL, true, 0)) {\r | |
450 | if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");\r | |
451 | OnError(0);\r | |
452 | return;\r | |
453 | };\r | |
454 | \r | |
455 | if(mifare_ultra_writeblock_compat(blockNo, blockdata)) {\r | |
456 | if (MF_DBGLEVEL >= 1) Dbprintf("Write block error");\r | |
457 | OnError(0);\r | |
458 | return; };\r | |
459 | \r | |
460 | if(mifare_ultra_halt()) {\r | |
461 | if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");\r | |
462 | OnError(0);\r | |
463 | return;\r | |
464 | };\r | |
465 | \r | |
466 | if (MF_DBGLEVEL >= 2) DbpString("WRITE BLOCK FINISHED");\r | |
467 | \r | |
468 | cmd_send(CMD_ACK,1,0,0,0,0);\r | |
469 | FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r | |
470 | LEDsoff();\r | |
471 | }\r | |
472 | */\r | |
473 | \r | |
474 | // Arg0 : Block to write to.\r | |
475 | // Arg1 : 0 = use no authentication.\r | |
476 | // 1 = use 0x1A authentication.\r | |
477 | // 2 = use 0x1B authentication.\r | |
478 | // datain : 4 first bytes is data to be written.\r | |
479 | // : 4/16 next bytes is authentication key.\r | |
480 | void MifareUWriteBlock(uint8_t arg0, uint8_t arg1, uint8_t *datain)\r | |
481 | {\r | |
482 | uint8_t blockNo = arg0;\r | |
483 | bool useKey = (arg1 == 1); //UL_C\r | |
484 | bool usePwd = (arg1 == 2); //UL_EV1/NTAG\r | |
485 | byte_t blockdata[4] = {0x00};\r | |
486 | \r | |
487 | memcpy(blockdata, datain,4);\r | |
488 | \r | |
489 | LEDsoff();\r | |
490 | LED_A_ON();\r | |
491 | iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r | |
492 | \r | |
493 | clear_trace();\r | |
494 | \r | |
495 | if(!iso14443a_select_card(NULL, NULL, NULL, true, 0, true)) {\r | |
496 | if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");\r | |
497 | OnError(0);\r | |
498 | return;\r | |
499 | };\r | |
500 | \r | |
501 | // UL-C authentication\r | |
502 | if ( useKey ) {\r | |
503 | uint8_t key[16] = {0x00};\r | |
504 | memcpy(key, datain+4, sizeof(key) );\r | |
505 | \r | |
506 | if ( !mifare_ultra_auth(key) ) {\r | |
507 | OnError(1);\r | |
508 | return;\r | |
509 | }\r | |
510 | }\r | |
511 | \r | |
512 | // UL-EV1 / NTAG authentication\r | |
513 | if (usePwd) {\r | |
514 | uint8_t pwd[4] = {0x00};\r | |
515 | memcpy(pwd, datain+4, 4);\r | |
516 | uint8_t pack[4] = {0,0,0,0};\r | |
517 | if (!mifare_ul_ev1_auth(pwd, pack)) {\r | |
518 | OnError(1);\r | |
519 | return;\r | |
520 | }\r | |
521 | }\r | |
522 | \r | |
523 | if(mifare_ultra_writeblock(blockNo, blockdata)) {\r | |
524 | if (MF_DBGLEVEL >= 1) Dbprintf("Write block error");\r | |
525 | OnError(0);\r | |
526 | return;\r | |
527 | };\r | |
528 | \r | |
529 | if(mifare_ultra_halt()) {\r | |
530 | if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");\r | |
531 | OnError(0);\r | |
532 | return;\r | |
533 | };\r | |
534 | \r | |
535 | if (MF_DBGLEVEL >= 2) DbpString("WRITE BLOCK FINISHED");\r | |
536 | \r | |
537 | cmd_send(CMD_ACK,1,0,0,0,0);\r | |
538 | FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r | |
539 | LEDsoff();\r | |
540 | }\r | |
541 | \r | |
542 | void MifareUSetPwd(uint8_t arg0, uint8_t *datain){\r | |
543 | \r | |
544 | uint8_t pwd[16] = {0x00};\r | |
545 | byte_t blockdata[4] = {0x00};\r | |
546 | \r | |
547 | memcpy(pwd, datain, 16);\r | |
548 | \r | |
549 | LED_A_ON(); LED_B_OFF(); LED_C_OFF();\r | |
550 | iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r | |
551 | \r | |
552 | clear_trace();\r | |
553 | \r | |
554 | if(!iso14443a_select_card(NULL, NULL, NULL, true, 0, true)) {\r | |
555 | if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");\r | |
556 | OnError(0);\r | |
557 | return;\r | |
558 | };\r | |
559 | \r | |
560 | blockdata[0] = pwd[7];\r | |
561 | blockdata[1] = pwd[6];\r | |
562 | blockdata[2] = pwd[5];\r | |
563 | blockdata[3] = pwd[4];\r | |
564 | if(mifare_ultra_writeblock( 44, blockdata)) {\r | |
565 | if (MF_DBGLEVEL >= 1) Dbprintf("Write block error");\r | |
566 | OnError(44);\r | |
567 | return;\r | |
568 | };\r | |
569 | \r | |
570 | blockdata[0] = pwd[3];\r | |
571 | blockdata[1] = pwd[2];\r | |
572 | blockdata[2] = pwd[1];\r | |
573 | blockdata[3] = pwd[0];\r | |
574 | if(mifare_ultra_writeblock( 45, blockdata)) {\r | |
575 | if (MF_DBGLEVEL >= 1) Dbprintf("Write block error");\r | |
576 | OnError(45);\r | |
577 | return;\r | |
578 | };\r | |
579 | \r | |
580 | blockdata[0] = pwd[15];\r | |
581 | blockdata[1] = pwd[14];\r | |
582 | blockdata[2] = pwd[13];\r | |
583 | blockdata[3] = pwd[12];\r | |
584 | if(mifare_ultra_writeblock( 46, blockdata)) {\r | |
585 | if (MF_DBGLEVEL >= 1) Dbprintf("Write block error");\r | |
586 | OnError(46);\r | |
587 | return;\r | |
588 | };\r | |
589 | \r | |
590 | blockdata[0] = pwd[11];\r | |
591 | blockdata[1] = pwd[10];\r | |
592 | blockdata[2] = pwd[9];\r | |
593 | blockdata[3] = pwd[8];\r | |
594 | if(mifare_ultra_writeblock( 47, blockdata)) {\r | |
595 | if (MF_DBGLEVEL >= 1) Dbprintf("Write block error");\r | |
596 | OnError(47);\r | |
597 | return;\r | |
598 | };\r | |
599 | \r | |
600 | if(mifare_ultra_halt()) {\r | |
601 | if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");\r | |
602 | OnError(0);\r | |
603 | return;\r | |
604 | };\r | |
605 | \r | |
606 | cmd_send(CMD_ACK,1,0,0,0,0);\r | |
607 | FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r | |
608 | LEDsoff();\r | |
609 | }\r | |
610 | \r | |
611 | // Return 1 if the nonce is invalid else return 0\r | |
612 | int valid_nonce(uint32_t Nt, uint32_t NtEnc, uint32_t Ks1, uint8_t *parity) {\r | |
613 | return ((oddparity8((Nt >> 24) & 0xFF) == ((parity[0]) ^ oddparity8((NtEnc >> 24) & 0xFF) ^ BIT(Ks1,16))) & \\r | |
614 | (oddparity8((Nt >> 16) & 0xFF) == ((parity[1]) ^ oddparity8((NtEnc >> 16) & 0xFF) ^ BIT(Ks1,8))) & \\r | |
615 | (oddparity8((Nt >> 8) & 0xFF) == ((parity[2]) ^ oddparity8((NtEnc >> 8) & 0xFF) ^ BIT(Ks1,0)))) ? 1 : 0;\r | |
616 | }\r | |
617 | \r | |
618 | \r | |
619 | //-----------------------------------------------------------------------------\r | |
620 | // acquire encrypted nonces in order to perform the attack described in\r | |
621 | // Carlo Meijer, Roel Verdult, "Ciphertext-only Cryptanalysis on Hardened\r | |
622 | // Mifare Classic Cards" in Proceedings of the 22nd ACM SIGSAC Conference on\r | |
623 | // Computer and Communications Security, 2015\r | |
624 | //-----------------------------------------------------------------------------\r | |
625 | void MifareAcquireEncryptedNonces(uint32_t arg0, uint32_t arg1, uint32_t flags, uint8_t *datain)\r | |
626 | {\r | |
627 | uint64_t ui64Key = 0;\r | |
628 | uint8_t uid[10];\r | |
629 | uint32_t cuid;\r | |
630 | uint8_t cascade_levels = 0;\r | |
631 | struct Crypto1State mpcs = {0, 0};\r | |
632 | struct Crypto1State *pcs;\r | |
633 | pcs = &mpcs;\r | |
634 | uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];\r | |
635 | int16_t isOK = 0;\r | |
636 | uint8_t par_enc[1];\r | |
637 | uint8_t nt_par_enc = 0;\r | |
638 | uint8_t buf[USB_CMD_DATA_SIZE];\r | |
639 | uint32_t timeout;\r | |
640 | \r | |
641 | uint8_t blockNo = arg0 & 0xff;\r | |
642 | uint8_t keyType = (arg0 >> 8) & 0xff;\r | |
643 | uint8_t targetBlockNo = arg1 & 0xff;\r | |
644 | uint8_t targetKeyType = (arg1 >> 8) & 0xff;\r | |
645 | ui64Key = bytes_to_num(datain, 6);\r | |
646 | bool initialize = flags & 0x0001;\r | |
647 | bool slow = flags & 0x0002;\r | |
648 | bool field_off = flags & 0x0004;\r | |
649 | \r | |
650 | LED_A_ON();\r | |
651 | LED_C_OFF();\r | |
652 | \r | |
653 | if (initialize) {\r | |
654 | iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r | |
655 | clear_trace();\r | |
656 | set_tracing(true);\r | |
657 | }\r | |
658 | \r | |
659 | LED_C_ON();\r | |
660 | \r | |
661 | uint16_t num_nonces = 0;\r | |
662 | bool have_uid = false;\r | |
663 | for (uint16_t i = 0; i <= USB_CMD_DATA_SIZE - 9; ) {\r | |
664 | \r | |
665 | // Test if the action was cancelled\r | |
666 | if(BUTTON_PRESS()) {\r | |
667 | isOK = 2;\r | |
668 | field_off = true;\r | |
669 | break;\r | |
670 | }\r | |
671 | \r | |
672 | if (!have_uid) { // need a full select cycle to get the uid first\r | |
673 | iso14a_card_select_t card_info;\r | |
674 | if(!iso14443a_select_card(uid, &card_info, &cuid, true, 0, true)) {\r | |
675 | if (MF_DBGLEVEL >= 1) Dbprintf("AcquireNonces: Can't select card (ALL)");\r | |
676 | continue;\r | |
677 | }\r | |
678 | switch (card_info.uidlen) {\r | |
679 | case 4 : cascade_levels = 1; break;\r | |
680 | case 7 : cascade_levels = 2; break;\r | |
681 | case 10: cascade_levels = 3; break;\r | |
682 | default: break;\r | |
683 | }\r | |
684 | have_uid = true;\r | |
685 | } else { // no need for anticollision. We can directly select the card\r | |
686 | if(!iso14443a_select_card(uid, NULL, NULL, false, cascade_levels, true)) {\r | |
687 | if (MF_DBGLEVEL >= 1) Dbprintf("AcquireNonces: Can't select card (UID)");\r | |
688 | continue;\r | |
689 | }\r | |
690 | }\r | |
691 | \r | |
692 | if (slow) {\r | |
693 | timeout = GetCountSspClk() + HARDNESTED_PRE_AUTHENTICATION_LEADTIME;\r | |
694 | while(GetCountSspClk() < timeout);\r | |
695 | }\r | |
696 | \r | |
697 | uint32_t nt1;\r | |
698 | if (mifare_classic_authex(pcs, cuid, blockNo, keyType, ui64Key, AUTH_FIRST, &nt1, NULL)) {\r | |
699 | if (MF_DBGLEVEL >= 1) Dbprintf("AcquireNonces: Auth1 error");\r | |
700 | continue;\r | |
701 | }\r | |
702 | \r | |
703 | // nested authentication\r | |
704 | uint16_t len = mifare_sendcmd_short(pcs, AUTH_NESTED, 0x60 + (targetKeyType & 0x01), targetBlockNo, receivedAnswer, par_enc, NULL);\r | |
705 | if (len != 4) {\r | |
706 | if (MF_DBGLEVEL >= 1) Dbprintf("AcquireNonces: Auth2 error len=%d", len);\r | |
707 | continue;\r | |
708 | }\r | |
709 | \r | |
710 | // send an incomplete dummy response in order to trigger the card's authentication failure timeout\r | |
711 | uint8_t dummy_answer[1] = {0};\r | |
712 | ReaderTransmit(dummy_answer, 1, NULL);\r | |
713 | \r | |
714 | timeout = GetCountSspClk() + HARDNESTED_AUTHENTICATION_TIMEOUT;\r | |
715 | \r | |
716 | num_nonces++;\r | |
717 | if (num_nonces % 2) {\r | |
718 | memcpy(buf+i, receivedAnswer, 4);\r | |
719 | nt_par_enc = par_enc[0] & 0xf0;\r | |
720 | } else {\r | |
721 | nt_par_enc |= par_enc[0] >> 4;\r | |
722 | memcpy(buf+i+4, receivedAnswer, 4);\r | |
723 | memcpy(buf+i+8, &nt_par_enc, 1);\r | |
724 | i += 9;\r | |
725 | }\r | |
726 | \r | |
727 | // wait for the card to become ready again\r | |
728 | while(GetCountSspClk() < timeout);\r | |
729 | \r | |
730 | }\r | |
731 | \r | |
732 | LED_C_OFF();\r | |
733 | \r | |
734 | crypto1_destroy(pcs);\r | |
735 | \r | |
736 | LED_B_ON();\r | |
737 | cmd_send(CMD_ACK, isOK, cuid, num_nonces, buf, sizeof(buf));\r | |
738 | LED_B_OFF();\r | |
739 | \r | |
740 | if (MF_DBGLEVEL >= 3) DbpString("AcquireEncryptedNonces finished");\r | |
741 | \r | |
742 | if (field_off) {\r | |
743 | FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r | |
744 | LEDsoff();\r | |
745 | }\r | |
746 | }\r | |
747 | \r | |
748 | \r | |
749 | //-----------------------------------------------------------------------------\r | |
750 | // MIFARE nested authentication.\r | |
751 | //\r | |
752 | //-----------------------------------------------------------------------------\r | |
753 | void MifareNested(uint32_t arg0, uint32_t arg1, uint32_t calibrate, uint8_t *datain)\r | |
754 | {\r | |
755 | // params\r | |
756 | uint8_t blockNo = arg0 & 0xff;\r | |
757 | uint8_t keyType = (arg0 >> 8) & 0xff;\r | |
758 | uint8_t targetBlockNo = arg1 & 0xff;\r | |
759 | uint8_t targetKeyType = (arg1 >> 8) & 0xff;\r | |
760 | uint64_t ui64Key = 0;\r | |
761 | \r | |
762 | ui64Key = bytes_to_num(datain, 6);\r | |
763 | \r | |
764 | // variables\r | |
765 | uint16_t rtr, i, j, len;\r | |
766 | uint16_t davg;\r | |
767 | static uint16_t dmin, dmax;\r | |
768 | uint8_t uid[10];\r | |
769 | uint32_t cuid, nt1, nt2, nttmp, nttest, ks1;\r | |
770 | uint8_t par[1];\r | |
771 | uint32_t target_nt[2], target_ks[2];\r | |
772 | \r | |
773 | uint8_t par_array[4];\r | |
774 | uint16_t ncount = 0;\r | |
775 | struct Crypto1State mpcs = {0, 0};\r | |
776 | struct Crypto1State *pcs;\r | |
777 | pcs = &mpcs;\r | |
778 | uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];\r | |
779 | \r | |
780 | uint32_t auth1_time, auth2_time;\r | |
781 | static uint16_t delta_time;\r | |
782 | \r | |
783 | LED_A_ON();\r | |
784 | LED_C_OFF();\r | |
785 | iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r | |
786 | \r | |
787 | // free eventually allocated BigBuf memory\r | |
788 | BigBuf_free();\r | |
789 | \r | |
790 | if (calibrate) clear_trace();\r | |
791 | set_tracing(true);\r | |
792 | \r | |
793 | // statistics on nonce distance\r | |
794 | int16_t isOK = 0;\r | |
795 | #define NESTED_MAX_TRIES 12\r | |
796 | uint16_t unsuccessfull_tries = 0;\r | |
797 | if (calibrate) { // for first call only. Otherwise reuse previous calibration\r | |
798 | LED_B_ON();\r | |
799 | WDT_HIT();\r | |
800 | \r | |
801 | davg = dmax = 0;\r | |
802 | dmin = 2000;\r | |
803 | delta_time = 0;\r | |
804 | \r | |
805 | for (rtr = 0; rtr < 17; rtr++) {\r | |
806 | \r | |
807 | // Test if the action was cancelled\r | |
808 | if(BUTTON_PRESS()) {\r | |
809 | isOK = -2;\r | |
810 | break;\r | |
811 | }\r | |
812 | \r | |
813 | // prepare next select. No need to power down the card.\r | |
814 | if(mifare_classic_halt(pcs, cuid)) {\r | |
815 | if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Halt error");\r | |
816 | rtr--;\r | |
817 | continue;\r | |
818 | }\r | |
819 | \r | |
820 | if(!iso14443a_select_card(uid, NULL, &cuid, true, 0, true)) {\r | |
821 | if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Can't select card");\r | |
822 | rtr--;\r | |
823 | continue;\r | |
824 | };\r | |
825 | \r | |
826 | auth1_time = 0;\r | |
827 | if(mifare_classic_authex(pcs, cuid, blockNo, keyType, ui64Key, AUTH_FIRST, &nt1, &auth1_time)) {\r | |
828 | if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Auth1 error");\r | |
829 | rtr--;\r | |
830 | continue;\r | |
831 | };\r | |
832 | \r | |
833 | if (delta_time) {\r | |
834 | auth2_time = auth1_time + delta_time;\r | |
835 | } else {\r | |
836 | auth2_time = 0;\r | |
837 | }\r | |
838 | if(mifare_classic_authex(pcs, cuid, blockNo, keyType, ui64Key, AUTH_NESTED, &nt2, &auth2_time)) {\r | |
839 | if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Auth2 error");\r | |
840 | rtr--;\r | |
841 | continue;\r | |
842 | };\r | |
843 | \r | |
844 | nttmp = prng_successor(nt1, 100); //NXP Mifare is typical around 840,but for some unlicensed/compatible mifare card this can be 160\r | |
845 | for (i = 101; i < 1200; i++) {\r | |
846 | nttmp = prng_successor(nttmp, 1);\r | |
847 | if (nttmp == nt2) break;\r | |
848 | }\r | |
849 | \r | |
850 | if (i != 1200) {\r | |
851 | if (rtr != 0) {\r | |
852 | davg += i;\r | |
853 | dmin = MIN(dmin, i);\r | |
854 | dmax = MAX(dmax, i);\r | |
855 | }\r | |
856 | else {\r | |
857 | delta_time = auth2_time - auth1_time + 32; // allow some slack for proper timing\r | |
858 | }\r | |
859 | if (MF_DBGLEVEL >= 3) Dbprintf("Nested: calibrating... ntdist=%d", i);\r | |
860 | } else {\r | |
861 | unsuccessfull_tries++;\r | |
862 | if (unsuccessfull_tries > NESTED_MAX_TRIES) { // card isn't vulnerable to nested attack (random numbers are not predictable)\r | |
863 | isOK = -3;\r | |
864 | }\r | |
865 | }\r | |
866 | }\r | |
867 | \r | |
868 | davg = (davg + (rtr - 1)/2) / (rtr - 1);\r | |
869 | \r | |
870 | if (MF_DBGLEVEL >= 3) Dbprintf("rtr=%d isOK=%d min=%d max=%d avg=%d, delta_time=%d", rtr, isOK, dmin, dmax, davg, delta_time);\r | |
871 | \r | |
872 | dmin = davg - 2;\r | |
873 | dmax = davg + 2;\r | |
874 | \r | |
875 | LED_B_OFF();\r | |
876 | \r | |
877 | }\r | |
878 | // -------------------------------------------------------------------------------------------------\r | |
879 | \r | |
880 | LED_C_ON();\r | |
881 | \r | |
882 | // get crypted nonces for target sector\r | |
883 | for(i=0; i < 2 && !isOK; i++) { // look for exactly two different nonces\r | |
884 | \r | |
885 | target_nt[i] = 0;\r | |
886 | while(target_nt[i] == 0) { // continue until we have an unambiguous nonce\r | |
887 | \r | |
888 | // prepare next select. No need to power down the card.\r | |
889 | if(mifare_classic_halt(pcs, cuid)) {\r | |
890 | if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Halt error");\r | |
891 | continue;\r | |
892 | }\r | |
893 | \r | |
894 | if(!iso14443a_select_card(uid, NULL, &cuid, true, 0, true)) {\r | |
895 | if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Can't select card");\r | |
896 | continue;\r | |
897 | };\r | |
898 | \r | |
899 | auth1_time = 0;\r | |
900 | if(mifare_classic_authex(pcs, cuid, blockNo, keyType, ui64Key, AUTH_FIRST, &nt1, &auth1_time)) {\r | |
901 | if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Auth1 error");\r | |
902 | continue;\r | |
903 | };\r | |
904 | \r | |
905 | // nested authentication\r | |
906 | auth2_time = auth1_time + delta_time;\r | |
907 | len = mifare_sendcmd_short(pcs, AUTH_NESTED, 0x60 + (targetKeyType & 0x01), targetBlockNo, receivedAnswer, par, &auth2_time);\r | |
908 | if (len != 4) {\r | |
909 | if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Auth2 error len=%d", len);\r | |
910 | continue;\r | |
911 | };\r | |
912 | \r | |
913 | nt2 = bytes_to_num(receivedAnswer, 4);\r | |
914 | if (MF_DBGLEVEL >= 3) Dbprintf("Nonce#%d: Testing nt1=%08x nt2enc=%08x nt2par=%02x", i+1, nt1, nt2, par[0]);\r | |
915 | \r | |
916 | // Parity validity check\r | |
917 | for (j = 0; j < 4; j++) {\r | |
918 | par_array[j] = (oddparity8(receivedAnswer[j]) != ((par[0] >> (7-j)) & 0x01));\r | |
919 | }\r | |
920 | \r | |
921 | ncount = 0;\r | |
922 | nttest = prng_successor(nt1, dmin - 1);\r | |
923 | for (j = dmin; j < dmax + 1; j++) {\r | |
924 | nttest = prng_successor(nttest, 1);\r | |
925 | ks1 = nt2 ^ nttest;\r | |
926 | \r | |
927 | if (valid_nonce(nttest, nt2, ks1, par_array)){\r | |
928 | if (ncount > 0) { // we are only interested in disambiguous nonces, try again\r | |
929 | if (MF_DBGLEVEL >= 3) Dbprintf("Nonce#%d: dismissed (ambigous), ntdist=%d", i+1, j);\r | |
930 | target_nt[i] = 0;\r | |
931 | break;\r | |
932 | }\r | |
933 | target_nt[i] = nttest;\r | |
934 | target_ks[i] = ks1;\r | |
935 | ncount++;\r | |
936 | if (i == 1 && target_nt[1] == target_nt[0]) { // we need two different nonces\r | |
937 | target_nt[i] = 0;\r | |
938 | if (MF_DBGLEVEL >= 3) Dbprintf("Nonce#2: dismissed (= nonce#1), ntdist=%d", j);\r | |
939 | break;\r | |
940 | }\r | |
941 | if (MF_DBGLEVEL >= 3) Dbprintf("Nonce#%d: valid, ntdist=%d", i+1, j);\r | |
942 | }\r | |
943 | }\r | |
944 | if (target_nt[i] == 0 && j == dmax+1 && MF_DBGLEVEL >= 3) Dbprintf("Nonce#%d: dismissed (all invalid)", i+1);\r | |
945 | }\r | |
946 | }\r | |
947 | \r | |
948 | LED_C_OFF();\r | |
949 | \r | |
950 | // ----------------------------- crypto1 destroy\r | |
951 | crypto1_destroy(pcs);\r | |
952 | \r | |
953 | byte_t buf[4 + 4 * 4];\r | |
954 | memcpy(buf, &cuid, 4);\r | |
955 | memcpy(buf+4, &target_nt[0], 4);\r | |
956 | memcpy(buf+8, &target_ks[0], 4);\r | |
957 | memcpy(buf+12, &target_nt[1], 4);\r | |
958 | memcpy(buf+16, &target_ks[1], 4);\r | |
959 | \r | |
960 | LED_B_ON();\r | |
961 | cmd_send(CMD_ACK, isOK, 0, targetBlockNo + (targetKeyType * 0x100), buf, sizeof(buf));\r | |
962 | LED_B_OFF();\r | |
963 | \r | |
964 | if (MF_DBGLEVEL >= 3) DbpString("NESTED FINISHED");\r | |
965 | \r | |
966 | FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r | |
967 | LEDsoff();\r | |
968 | }\r | |
969 | \r | |
970 | //-----------------------------------------------------------------------------\r | |
971 | // MIFARE check keys. key count up to 85.\r | |
972 | //\r | |
973 | //-----------------------------------------------------------------------------\r | |
974 | void MifareChkKeys(uint16_t arg0, uint16_t arg1, uint8_t arg2, uint8_t *datain)\r | |
975 | {\r | |
976 | uint8_t blockNo = arg0 & 0xff;\r | |
977 | uint8_t keyType = (arg0 >> 8) & 0xff;\r | |
978 | bool clearTrace = arg1 & 0x01;\r | |
979 | bool multisectorCheck = arg1 & 0x02;\r | |
980 | uint8_t set14aTimeout = (arg1 >> 8) & 0xff;\r | |
981 | uint8_t keyCount = arg2;\r | |
982 | \r | |
983 | // clear debug level\r | |
984 | int OLD_MF_DBGLEVEL = MF_DBGLEVEL;\r | |
985 | MF_DBGLEVEL = MF_DBG_NONE;\r | |
986 | \r | |
987 | LED_A_ON();\r | |
988 | LED_B_OFF();\r | |
989 | LED_C_OFF();\r | |
990 | iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r | |
991 | \r | |
992 | if (clearTrace) clear_trace();\r | |
993 | set_tracing(true);\r | |
994 | \r | |
995 | if (set14aTimeout){\r | |
996 | iso14a_set_timeout(set14aTimeout * 10); // timeout: ms = x/106 35-minimum, 50-OK 106-recommended 500-safe\r | |
997 | }\r | |
998 | \r | |
999 | if (multisectorCheck) {\r | |
1000 | TKeyIndex keyIndex = {{0}};\r | |
1001 | uint8_t sectorCnt = blockNo;\r | |
1002 | int res = MifareMultisectorChk(datain, keyCount, sectorCnt, keyType, OLD_MF_DBGLEVEL, &keyIndex);\r | |
1003 | \r | |
1004 | LED_B_ON();\r | |
1005 | if (res >= 0) {\r | |
1006 | cmd_send(CMD_ACK, 1, 0, 0, keyIndex, 80);\r | |
1007 | } else {\r | |
1008 | cmd_send(CMD_ACK, 0, 0, 0, NULL, 0);\r | |
1009 | }\r | |
1010 | LED_B_OFF();\r | |
1011 | } else { \r | |
1012 | int res = MifareChkBlockKeys(datain, keyCount, blockNo, keyType, OLD_MF_DBGLEVEL);\r | |
1013 | \r | |
1014 | LED_B_ON();\r | |
1015 | if (res > 0) {\r | |
1016 | cmd_send(CMD_ACK, 1, 0, 0, datain + (res - 1) * 6, 6);\r | |
1017 | } else {\r | |
1018 | cmd_send(CMD_ACK, 0, 0, 0, NULL, 0);\r | |
1019 | }\r | |
1020 | LED_B_OFF();\r | |
1021 | }\r | |
1022 | \r | |
1023 | FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r | |
1024 | LEDsoff();\r | |
1025 | \r | |
1026 | // restore debug level\r | |
1027 | MF_DBGLEVEL = OLD_MF_DBGLEVEL;\r | |
1028 | }\r | |
1029 | \r | |
1030 | //-----------------------------------------------------------------------------\r | |
1031 | // MIFARE commands set debug level\r | |
1032 | //\r | |
1033 | //-----------------------------------------------------------------------------\r | |
1034 | void MifareSetDbgLvl(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain){\r | |
1035 | MF_DBGLEVEL = arg0;\r | |
1036 | Dbprintf("Debug level: %d", MF_DBGLEVEL);\r | |
1037 | }\r | |
1038 | \r | |
1039 | //-----------------------------------------------------------------------------\r | |
1040 | // Work with emulator memory\r | |
1041 | //\r | |
1042 | // Note: we call FpgaDownloadAndGo(FPGA_BITSTREAM_HF) here although FPGA is not\r | |
1043 | // involved in dealing with emulator memory. But if it is called later, it might\r | |
1044 | // destroy the Emulator Memory.\r | |
1045 | //-----------------------------------------------------------------------------\r | |
1046 | \r | |
1047 | void MifareEMemClr(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain){\r | |
1048 | FpgaDownloadAndGo(FPGA_BITSTREAM_HF);\r | |
1049 | emlClearMem();\r | |
1050 | }\r | |
1051 | \r | |
1052 | void MifareEMemSet(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain){\r | |
1053 | FpgaDownloadAndGo(FPGA_BITSTREAM_HF);\r | |
1054 | emlSetMem(datain, arg0, arg1); // data, block num, blocks count\r | |
1055 | }\r | |
1056 | \r | |
1057 | void MifareEMemGet(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain){\r | |
1058 | FpgaDownloadAndGo(FPGA_BITSTREAM_HF);\r | |
1059 | byte_t buf[USB_CMD_DATA_SIZE];\r | |
1060 | emlGetMem(buf, arg0, arg1); // data, block num, blocks count (max 4)\r | |
1061 | \r | |
1062 | LED_B_ON();\r | |
1063 | cmd_send(CMD_ACK,arg0,arg1,0,buf,USB_CMD_DATA_SIZE);\r | |
1064 | LED_B_OFF();\r | |
1065 | }\r | |
1066 | \r | |
1067 | //-----------------------------------------------------------------------------\r | |
1068 | // Load a card into the emulator memory\r | |
1069 | //\r | |
1070 | //-----------------------------------------------------------------------------\r | |
1071 | void MifareECardLoad(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain){\r | |
1072 | uint8_t numSectors = arg0;\r | |
1073 | uint8_t keyType = arg1;\r | |
1074 | uint64_t ui64Key = 0;\r | |
1075 | uint32_t cuid;\r | |
1076 | struct Crypto1State mpcs = {0, 0};\r | |
1077 | struct Crypto1State *pcs;\r | |
1078 | pcs = &mpcs;\r | |
1079 | \r | |
1080 | // variables\r | |
1081 | byte_t dataoutbuf[16];\r | |
1082 | byte_t dataoutbuf2[16];\r | |
1083 | uint8_t uid[10];\r | |
1084 | \r | |
1085 | LED_A_ON();\r | |
1086 | LED_B_OFF();\r | |
1087 | LED_C_OFF();\r | |
1088 | iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r | |
1089 | \r | |
1090 | clear_trace();\r | |
1091 | set_tracing(false);\r | |
1092 | \r | |
1093 | bool isOK = true;\r | |
1094 | \r | |
1095 | if(!iso14443a_select_card(uid, NULL, &cuid, true, 0, true)) {\r | |
1096 | isOK = false;\r | |
1097 | if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");\r | |
1098 | }\r | |
1099 | \r | |
1100 | for (uint8_t sectorNo = 0; isOK && sectorNo < numSectors; sectorNo++) {\r | |
1101 | ui64Key = emlGetKey(sectorNo, keyType);\r | |
1102 | if (sectorNo == 0){\r | |
1103 | if(isOK && mifare_classic_auth(pcs, cuid, FirstBlockOfSector(sectorNo), keyType, ui64Key, AUTH_FIRST)) {\r | |
1104 | isOK = false;\r | |
1105 | if (MF_DBGLEVEL >= 1) Dbprintf("Sector[%2d]. Auth error", sectorNo);\r | |
1106 | break;\r | |
1107 | }\r | |
1108 | } else {\r | |
1109 | if(isOK && mifare_classic_auth(pcs, cuid, FirstBlockOfSector(sectorNo), keyType, ui64Key, AUTH_NESTED)) {\r | |
1110 | isOK = false;\r | |
1111 | if (MF_DBGLEVEL >= 1) Dbprintf("Sector[%2d]. Auth nested error", sectorNo);\r | |
1112 | break;\r | |
1113 | }\r | |
1114 | }\r | |
1115 | \r | |
1116 | for (uint8_t blockNo = 0; isOK && blockNo < NumBlocksPerSector(sectorNo); blockNo++) {\r | |
1117 | if(isOK && mifare_classic_readblock(pcs, cuid, FirstBlockOfSector(sectorNo) + blockNo, dataoutbuf)) {\r | |
1118 | isOK = false;\r | |
1119 | if (MF_DBGLEVEL >= 1) Dbprintf("Error reading sector %2d block %2d", sectorNo, blockNo);\r | |
1120 | break;\r | |
1121 | };\r | |
1122 | if (isOK) {\r | |
1123 | if (blockNo < NumBlocksPerSector(sectorNo) - 1) {\r | |
1124 | emlSetMem(dataoutbuf, FirstBlockOfSector(sectorNo) + blockNo, 1);\r | |
1125 | } else { // sector trailer, keep the keys, set only the AC\r | |
1126 | emlGetMem(dataoutbuf2, FirstBlockOfSector(sectorNo) + blockNo, 1);\r | |
1127 | memcpy(&dataoutbuf2[6], &dataoutbuf[6], 4);\r | |
1128 | emlSetMem(dataoutbuf2, FirstBlockOfSector(sectorNo) + blockNo, 1);\r | |
1129 | }\r | |
1130 | }\r | |
1131 | }\r | |
1132 | \r | |
1133 | }\r | |
1134 | \r | |
1135 | if(mifare_classic_halt(pcs, cuid)) {\r | |
1136 | if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");\r | |
1137 | };\r | |
1138 | \r | |
1139 | // ----------------------------- crypto1 destroy\r | |
1140 | crypto1_destroy(pcs);\r | |
1141 | \r | |
1142 | FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r | |
1143 | LEDsoff();\r | |
1144 | \r | |
1145 | if (MF_DBGLEVEL >= 2) DbpString("EMUL FILL SECTORS FINISHED");\r | |
1146 | \r | |
1147 | }\r | |
1148 | \r | |
1149 | \r | |
1150 | //-----------------------------------------------------------------------------\r | |
1151 | // Work with "magic Chinese" card (email him: ouyangweidaxian@live.cn)\r | |
1152 | //\r | |
1153 | //-----------------------------------------------------------------------------\r | |
1154 | \r | |
1155 | static bool isBlockTrailer(int blockN) {\r | |
1156 | if (blockN >= 0 && blockN < 128) {\r | |
1157 | return ((blockN & 0x03) == 0x03);\r | |
1158 | }\r | |
1159 | if (blockN >= 128 && blockN <= 256) {\r | |
1160 | return ((blockN & 0x0F) == 0x0F);\r | |
1161 | }\r | |
1162 | return false;\r | |
1163 | }\r | |
1164 | \r | |
1165 | void MifareCWipe(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain){\r | |
1166 | // var\r | |
1167 | byte_t isOK = 0;\r | |
1168 | uint32_t numBlocks = arg0;\r | |
1169 | // cmdParams:\r | |
1170 | // bit 0 - wipe gen1a\r | |
1171 | // bit 1 - fill card with default data\r | |
1172 | // bit 2 - gen1a = 0, gen1b = 1\r | |
1173 | uint8_t cmdParams = arg1;\r | |
1174 | bool needWipe = cmdParams & 0x01;\r | |
1175 | bool needFill = cmdParams & 0x02;\r | |
1176 | bool gen1b = cmdParams & 0x04;\r | |
1177 | \r | |
1178 | uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];\r | |
1179 | uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE];\r | |
1180 | \r | |
1181 | uint8_t block0[16] = {0x01, 0x02, 0x03, 0x04, 0x04, 0x08, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xBE, 0xAF};\r | |
1182 | uint8_t block1[16] = {0x00};\r | |
1183 | uint8_t blockK[16] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x08, 0x77, 0x8F, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};\r | |
1184 | uint8_t d_block[18] = {0x00};\r | |
1185 | \r | |
1186 | // card commands\r | |
1187 | uint8_t wupC1[] = { 0x40 };\r | |
1188 | uint8_t wupC2[] = { 0x43 };\r | |
1189 | uint8_t wipeC[] = { 0x41 };\r | |
1190 | \r | |
1191 | // iso14443 setup\r | |
1192 | LED_A_ON();\r | |
1193 | LED_B_OFF();\r | |
1194 | LED_C_OFF();\r | |
1195 | iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r | |
1196 | \r | |
1197 | // tracing\r | |
1198 | clear_trace();\r | |
1199 | set_tracing(true);\r | |
1200 | \r | |
1201 | while (true){\r | |
1202 | // wipe\r | |
1203 | if (needWipe){\r | |
1204 | ReaderTransmitBitsPar(wupC1,7,0, NULL);\r | |
1205 | if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != 0x0a)) {\r | |
1206 | if (MF_DBGLEVEL >= 1) Dbprintf("wupC1 error");\r | |
1207 | break;\r | |
1208 | };\r | |
1209 | \r | |
1210 | ReaderTransmit(wipeC, sizeof(wipeC), NULL);\r | |
1211 | if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != 0x0a)) {\r | |
1212 | if (MF_DBGLEVEL >= 1) Dbprintf("wipeC error");\r | |
1213 | break;\r | |
1214 | };\r | |
1215 | \r | |
1216 | if(mifare_classic_halt(NULL, 0)) {\r | |
1217 | if (MF_DBGLEVEL > 2) Dbprintf("Halt error");\r | |
1218 | };\r | |
1219 | };\r | |
1220 | \r | |
1221 | // put default data\r | |
1222 | if (needFill){\r | |
1223 | // select commands\r | |
1224 | ReaderTransmitBitsPar(wupC1, 7, 0, NULL);\r | |
1225 | \r | |
1226 | // gen1b magic tag : do no issue wupC2 and don't expect 0x0a response after SELECT_UID (after getting UID from chip in 'hf mf csetuid' command)\r | |
1227 | if (!gen1b) { \r | |
1228 | \r | |
1229 | if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != 0x0a)) {\r | |
1230 | if (MF_DBGLEVEL >= 1) Dbprintf("wupC1 error");\r | |
1231 | break;\r | |
1232 | };\r | |
1233 | \r | |
1234 | ReaderTransmit(wupC2, sizeof(wupC2), NULL);\r | |
1235 | if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != 0x0a)) {\r | |
1236 | if (MF_DBGLEVEL >= 1) Dbprintf("wupC2 error");\r | |
1237 | break;\r | |
1238 | };\r | |
1239 | }\r | |
1240 | \r | |
1241 | // send blocks command\r | |
1242 | for (int blockNo = 0; blockNo < numBlocks; blockNo++) {\r | |
1243 | if ((mifare_sendcmd_short(NULL, 0, 0xA0, blockNo, receivedAnswer, receivedAnswerPar, NULL) != 1) || (receivedAnswer[0] != 0x0a)) {\r | |
1244 | if (MF_DBGLEVEL >= 1) Dbprintf("write block send command error");\r | |
1245 | break;\r | |
1246 | };\r | |
1247 | \r | |
1248 | // check type of block and add crc\r | |
1249 | if (!isBlockTrailer(blockNo)){\r | |
1250 | memcpy(d_block, block1, 16);\r | |
1251 | } else {\r | |
1252 | memcpy(d_block, blockK, 16);\r | |
1253 | }\r | |
1254 | if (blockNo == 0) {\r | |
1255 | memcpy(d_block, block0, 16);\r | |
1256 | }\r | |
1257 | AppendCrc14443a(d_block, 16);\r | |
1258 | \r | |
1259 | // send write command\r | |
1260 | ReaderTransmit(d_block, sizeof(d_block), NULL);\r | |
1261 | if ((ReaderReceive(receivedAnswer, receivedAnswerPar) != 1) || (receivedAnswer[0] != 0x0a)) {\r | |
1262 | if (MF_DBGLEVEL >= 1) Dbprintf("write block send data error");\r | |
1263 | break;\r | |
1264 | };\r | |
1265 | }\r | |
1266 | \r | |
1267 | // halt\r | |
1268 | // do no issue halt command for gen1b \r | |
1269 | if (!gen1b) {\r | |
1270 | if (mifare_classic_halt(NULL, 0)) {\r | |
1271 | if (MF_DBGLEVEL > 2) Dbprintf("Halt error");\r | |
1272 | break;\r | |
1273 | }\r | |
1274 | }\r | |
1275 | }\r | |
1276 | break;\r | |
1277 | } \r | |
1278 | \r | |
1279 | // send USB response\r | |
1280 | LED_B_ON();\r | |
1281 | cmd_send(CMD_ACK,isOK,0,0,NULL,0);\r | |
1282 | LED_B_OFF();\r | |
1283 | \r | |
1284 | // reset fpga\r | |
1285 | FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r | |
1286 | LEDsoff();\r | |
1287 | \r | |
1288 | return;\r | |
1289 | }\r | |
1290 | \r | |
1291 | void MifareCSetBlock(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain){\r | |
1292 | \r | |
1293 | // params\r | |
1294 | uint8_t needWipe = arg0;\r | |
1295 | // bit 0 - need get UID\r | |
1296 | // bit 1 - need wupC\r | |
1297 | // bit 2 - need HALT after sequence\r | |
1298 | // bit 3 - need init FPGA and field before sequence\r | |
1299 | // bit 4 - need reset FPGA and LED\r | |
1300 | // bit 6 - gen1b backdoor type\r | |
1301 | uint8_t workFlags = arg1;\r | |
1302 | uint8_t blockNo = arg2;\r | |
1303 | \r | |
1304 | // card commands\r | |
1305 | uint8_t wupC1[] = { 0x40 };\r | |
1306 | uint8_t wupC2[] = { 0x43 };\r | |
1307 | uint8_t wipeC[] = { 0x41 };\r | |
1308 | \r | |
1309 | // variables\r | |
1310 | byte_t isOK = 0;\r | |
1311 | uint8_t uid[10] = {0x00};\r | |
1312 | uint8_t d_block[18] = {0x00};\r | |
1313 | uint32_t cuid;\r | |
1314 | \r | |
1315 | uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];\r | |
1316 | uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE];\r | |
1317 | \r | |
1318 | // reset FPGA and LED\r | |
1319 | if (workFlags & 0x08) {\r | |
1320 | LED_A_ON();\r | |
1321 | LED_B_OFF();\r | |
1322 | LED_C_OFF();\r | |
1323 | iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r | |
1324 | \r | |
1325 | clear_trace();\r | |
1326 | set_tracing(true);\r | |
1327 | }\r | |
1328 | \r | |
1329 | while (true) {\r | |
1330 | \r | |
1331 | // get UID from chip\r | |
1332 | if (workFlags & 0x01) {\r | |
1333 | if(!iso14443a_select_card(uid, NULL, &cuid, true, 0, true)) {\r | |
1334 | if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");\r | |
1335 | // Continue, if we set wrong UID or wrong UID checksum or some ATQA or SAK we will can't select card. But we need to write block 0 to make card work.\r | |
1336 | //break;\r | |
1337 | };\r | |
1338 | \r | |
1339 | if(mifare_classic_halt(NULL, cuid)) {\r | |
1340 | if (MF_DBGLEVEL > 2) Dbprintf("Halt error");\r | |
1341 | // Continue, some magic tags misbehavies and send an answer to it.\r | |
1342 | // break;\r | |
1343 | };\r | |
1344 | };\r | |
1345 | \r | |
1346 | // reset chip\r | |
1347 | // Wipe command don't work with gen1b\r | |
1348 | if (needWipe && !(workFlags & 0x40)){\r | |
1349 | ReaderTransmitBitsPar(wupC1,7,0, NULL);\r | |
1350 | if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != 0x0a)) {\r | |
1351 | if (MF_DBGLEVEL >= 1) Dbprintf("wupC1 error");\r | |
1352 | break;\r | |
1353 | };\r | |
1354 | \r | |
1355 | ReaderTransmit(wipeC, sizeof(wipeC), NULL);\r | |
1356 | if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != 0x0a)) {\r | |
1357 | if (MF_DBGLEVEL >= 1) Dbprintf("wipeC error");\r | |
1358 | break;\r | |
1359 | };\r | |
1360 | \r | |
1361 | if(mifare_classic_halt(NULL, 0)) {\r | |
1362 | if (MF_DBGLEVEL > 2) Dbprintf("Halt error");\r | |
1363 | // Continue, some magic tags misbehavies and send an answer to it.\r | |
1364 | // break;\r | |
1365 | };\r | |
1366 | };\r | |
1367 | \r | |
1368 | // write block\r | |
1369 | if (workFlags & 0x02) {\r | |
1370 | ReaderTransmitBitsPar(wupC1,7,0, NULL);\r | |
1371 | \r | |
1372 | // gen1b magic tag : do no issue wupC2 and don't expect 0x0a response after SELECT_UID (after getting UID from chip in 'hf mf csetuid' command)\r | |
1373 | if (!(workFlags & 0x40)) {\r | |
1374 | \r | |
1375 | if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != 0x0a)) {\r | |
1376 | if (MF_DBGLEVEL >= 1) Dbprintf("wupC1 error");\r | |
1377 | break;\r | |
1378 | };\r | |
1379 | \r | |
1380 | ReaderTransmit(wupC2, sizeof(wupC2), NULL);\r | |
1381 | if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != 0x0a)) {\r | |
1382 | if (MF_DBGLEVEL >= 1) Dbprintf("wupC2 error");\r | |
1383 | break;\r | |
1384 | };\r | |
1385 | }\r | |
1386 | }\r | |
1387 | \r | |
1388 | if ((mifare_sendcmd_short(NULL, 0, 0xA0, blockNo, receivedAnswer, receivedAnswerPar, NULL) != 1) || (receivedAnswer[0] != 0x0a)) {\r | |
1389 | if (MF_DBGLEVEL >= 1) Dbprintf("write block send command error");\r | |
1390 | break;\r | |
1391 | };\r | |
1392 | \r | |
1393 | memcpy(d_block, datain, 16);\r | |
1394 | AppendCrc14443a(d_block, 16);\r | |
1395 | \r | |
1396 | ReaderTransmit(d_block, sizeof(d_block), NULL);\r | |
1397 | if ((ReaderReceive(receivedAnswer, receivedAnswerPar) != 1) || (receivedAnswer[0] != 0x0a)) {\r | |
1398 | if (MF_DBGLEVEL >= 1) Dbprintf("write block send data error");\r | |
1399 | break;\r | |
1400 | };\r | |
1401 | \r | |
1402 | if (workFlags & 0x04) {\r | |
1403 | // do no issue halt command for gen1b magic tag (#db# halt error. response len: 1)\r | |
1404 | if (!(workFlags & 0x40)) {\r | |
1405 | if (mifare_classic_halt(NULL, 0)) {\r | |
1406 | if (MF_DBGLEVEL > 2) Dbprintf("Halt error");\r | |
1407 | // Continue, some magic tags misbehavies and send an answer to it.\r | |
1408 | // break;\r | |
1409 | }\r | |
1410 | }\r | |
1411 | }\r | |
1412 | \r | |
1413 | isOK = 1;\r | |
1414 | break;\r | |
1415 | }\r | |
1416 | \r | |
1417 | LED_B_ON();\r | |
1418 | cmd_send(CMD_ACK,isOK,0,0,uid,4);\r | |
1419 | LED_B_OFF();\r | |
1420 | \r | |
1421 | if ((workFlags & 0x10) || (!isOK)) {\r | |
1422 | FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r | |
1423 | LEDsoff();\r | |
1424 | }\r | |
1425 | }\r | |
1426 | \r | |
1427 | \r | |
1428 | void MifareCGetBlock(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain){\r | |
1429 | \r | |
1430 | // params\r | |
1431 | // bit 1 - need wupC\r | |
1432 | // bit 2 - need HALT after sequence\r | |
1433 | // bit 3 - need init FPGA and field before sequence\r | |
1434 | // bit 4 - need reset FPGA and LED\r | |
1435 | // bit 5 - need to set datain instead of issuing USB reply (called via ARM for StandAloneMode14a)\r | |
1436 | // bit 6 - gen1b backdoor type\r | |
1437 | uint8_t workFlags = arg0;\r | |
1438 | uint8_t blockNo = arg2;\r | |
1439 | \r | |
1440 | // card commands\r | |
1441 | uint8_t wupC1[] = { 0x40 };\r | |
1442 | uint8_t wupC2[] = { 0x43 };\r | |
1443 | \r | |
1444 | // variables\r | |
1445 | byte_t isOK = 0;\r | |
1446 | uint8_t data[18] = {0x00};\r | |
1447 | uint32_t cuid = 0;\r | |
1448 | \r | |
1449 | uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];\r | |
1450 | uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE];\r | |
1451 | \r | |
1452 | if (workFlags & 0x08) {\r | |
1453 | LED_A_ON();\r | |
1454 | LED_B_OFF();\r | |
1455 | LED_C_OFF();\r | |
1456 | iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r | |
1457 | \r | |
1458 | clear_trace();\r | |
1459 | set_tracing(true);\r | |
1460 | }\r | |
1461 | \r | |
1462 | while (true) {\r | |
1463 | if (workFlags & 0x02) {\r | |
1464 | ReaderTransmitBitsPar(wupC1,7,0, NULL);\r | |
1465 | if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != 0x0a)) {\r | |
1466 | if (MF_DBGLEVEL >= 1) Dbprintf("wupC1 error");\r | |
1467 | break;\r | |
1468 | };\r | |
1469 | // do no issue for gen1b magic tag\r | |
1470 | if (!(workFlags & 0x40)) {\r | |
1471 | ReaderTransmit(wupC2, sizeof(wupC2), NULL);\r | |
1472 | if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != 0x0a)) {\r | |
1473 | if (MF_DBGLEVEL >= 1) Dbprintf("wupC2 error");\r | |
1474 | break;\r | |
1475 | };\r | |
1476 | }\r | |
1477 | }\r | |
1478 | \r | |
1479 | // read block\r | |
1480 | if ((mifare_sendcmd_short(NULL, 0, 0x30, blockNo, receivedAnswer, receivedAnswerPar, NULL) != 18)) {\r | |
1481 | if (MF_DBGLEVEL >= 1) Dbprintf("read block send command error");\r | |
1482 | break;\r | |
1483 | };\r | |
1484 | memcpy(data, receivedAnswer, 18);\r | |
1485 | \r | |
1486 | if (workFlags & 0x04) {\r | |
1487 | // do no issue halt command for gen1b magic tag (#db# halt error. response len: 1)\r | |
1488 | if (!(workFlags & 0x40)) {\r | |
1489 | if (mifare_classic_halt(NULL, cuid)) {\r | |
1490 | if (MF_DBGLEVEL > 1) Dbprintf("Halt error");\r | |
1491 | // Continue, some magic tags misbehavies and send an answer to it.\r | |
1492 | // break;\r | |
1493 | }\r | |
1494 | }\r | |
1495 | }\r | |
1496 | \r | |
1497 | isOK = 1;\r | |
1498 | break;\r | |
1499 | }\r | |
1500 | \r | |
1501 | LED_B_ON();\r | |
1502 | if (workFlags & 0x20) {\r | |
1503 | if (isOK)\r | |
1504 | memcpy(datain, data, 18);\r | |
1505 | }\r | |
1506 | else\r | |
1507 | cmd_send(CMD_ACK,isOK,0,0,data,18);\r | |
1508 | LED_B_OFF();\r | |
1509 | \r | |
1510 | if ((workFlags & 0x10) || (!isOK)) {\r | |
1511 | FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r | |
1512 | LEDsoff();\r | |
1513 | }\r | |
1514 | }\r | |
1515 | \r | |
1516 | void MifareCIdent(){\r | |
1517 | \r | |
1518 | // card commands\r | |
1519 | uint8_t wupC1[] = { 0x40 };\r | |
1520 | uint8_t wupC2[] = { 0x43 };\r | |
1521 | \r | |
1522 | // variables\r | |
1523 | byte_t isOK = 0;\r | |
1524 | \r | |
1525 | uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];\r | |
1526 | uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE];\r | |
1527 | \r | |
1528 | LED_A_ON();\r | |
1529 | LED_B_OFF();\r | |
1530 | LED_C_OFF();\r | |
1531 | iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r | |
1532 | \r | |
1533 | clear_trace();\r | |
1534 | set_tracing(true); \r | |
1535 | \r | |
1536 | ReaderTransmitBitsPar(wupC1,7,0, NULL);\r | |
1537 | if(ReaderReceive(receivedAnswer, receivedAnswerPar) && (receivedAnswer[0] == 0x0a)) {\r | |
1538 | isOK = 2;\r | |
1539 | \r | |
1540 | ReaderTransmit(wupC2, sizeof(wupC2), NULL);\r | |
1541 | if(ReaderReceive(receivedAnswer, receivedAnswerPar) && (receivedAnswer[0] == 0x0a)) {\r | |
1542 | isOK = 1;\r | |
1543 | };\r | |
1544 | };\r | |
1545 | \r | |
1546 | // From iceman1001: removed the if, since some magic tags misbehavies and send an answer to it.\r | |
1547 | mifare_classic_halt(NULL, 0);\r | |
1548 | \r | |
1549 | LED_B_ON();\r | |
1550 | cmd_send(CMD_ACK,isOK,0,0,0,0);\r | |
1551 | LED_B_OFF();\r | |
1552 | \r | |
1553 | FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r | |
1554 | LEDsoff(); \r | |
1555 | }\r | |
1556 | \r | |
1557 | //\r | |
1558 | // DESFIRE\r | |
1559 | //\r | |
1560 | \r | |
1561 | void Mifare_DES_Auth1(uint8_t arg0, uint8_t *datain){\r | |
1562 | \r | |
1563 | byte_t dataout[11] = {0x00};\r | |
1564 | uint8_t uid[10] = {0x00};\r | |
1565 | uint32_t cuid;\r | |
1566 | \r | |
1567 | iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r | |
1568 | clear_trace();\r | |
1569 | \r | |
1570 | int len = iso14443a_select_card(uid, NULL, &cuid, true, 0, true);\r | |
1571 | if(!len) {\r | |
1572 | if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Can't select card");\r | |
1573 | OnError(1);\r | |
1574 | return;\r | |
1575 | };\r | |
1576 | \r | |
1577 | if(mifare_desfire_des_auth1(cuid, dataout)){\r | |
1578 | if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Authentication part1: Fail.");\r | |
1579 | OnError(4);\r | |
1580 | return;\r | |
1581 | }\r | |
1582 | \r | |
1583 | if (MF_DBGLEVEL >= MF_DBG_EXTENDED) DbpString("AUTH 1 FINISHED");\r | |
1584 | cmd_send(CMD_ACK,1,cuid,0,dataout, sizeof(dataout));\r | |
1585 | }\r | |
1586 | \r | |
1587 | void Mifare_DES_Auth2(uint32_t arg0, uint8_t *datain){\r | |
1588 | \r | |
1589 | uint32_t cuid = arg0;\r | |
1590 | uint8_t key[16] = {0x00};\r | |
1591 | byte_t isOK = 0;\r | |
1592 | byte_t dataout[12] = {0x00};\r | |
1593 | \r | |
1594 | memcpy(key, datain, 16);\r | |
1595 | \r | |
1596 | isOK = mifare_desfire_des_auth2(cuid, key, dataout);\r | |
1597 | \r | |
1598 | if( isOK) {\r | |
1599 | if (MF_DBGLEVEL >= MF_DBG_EXTENDED) Dbprintf("Authentication part2: Failed");\r | |
1600 | OnError(4);\r | |
1601 | return;\r | |
1602 | }\r | |
1603 | \r | |
1604 | if (MF_DBGLEVEL >= MF_DBG_EXTENDED) DbpString("AUTH 2 FINISHED");\r | |
1605 | \r | |
1606 | cmd_send(CMD_ACK, isOK, 0, 0, dataout, sizeof(dataout));\r | |
1607 | FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r | |
1608 | LEDsoff();\r | |
1609 | }\r | |
1610 | \r |