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