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