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