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