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