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