]>
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 | |
88e20c9f | 7 | // Iceman - May 2014,2015,2016\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 | |
a631936e | 17 | \r |
8556b852 | 18 | //-----------------------------------------------------------------------------\r |
baeaf579 | 19 | // Select, Authenticate, Read a MIFARE tag. \r |
8556b852 M |
20 | // read block\r |
21 | //-----------------------------------------------------------------------------\r | |
22 | void MifareReadBlock(uint8_t arg0, uint8_t arg1, uint8_t arg2, uint8_t *datain)\r | |
23 | {\r | |
24 | // params\r | |
25 | uint8_t blockNo = arg0;\r | |
26 | uint8_t keyType = arg1;\r | |
27 | uint64_t ui64Key = 0;\r | |
28 | ui64Key = bytes_to_num(datain, 6);\r | |
29 | \r | |
30 | // variables\r | |
31 | byte_t isOK = 0;\r | |
d798d31c | 32 | byte_t dataoutbuf[16] = {0x00};\r |
33 | uint8_t uid[10] = {0x00};\r | |
34 | uint32_t cuid = 0;\r | |
8556b852 M |
35 | struct Crypto1State mpcs = {0, 0};\r |
36 | struct Crypto1State *pcs;\r | |
37 | pcs = &mpcs;\r | |
38 | \r | |
7bc95e2e | 39 | iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r |
8556b852 | 40 | \r |
99cf19d9 | 41 | clear_trace();\r |
810f5379 | 42 | set_tracing(true);\r |
99cf19d9 | 43 | \r |
8556b852 M |
44 | LED_A_ON();\r |
45 | LED_B_OFF();\r | |
46 | LED_C_OFF();\r | |
47 | \r | |
48 | while (true) {\r | |
c188b1b9 | 49 | if(!iso14443a_select_card(uid, NULL, &cuid, true, 0)) {\r |
baeaf579 | 50 | if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");\r |
8556b852 M |
51 | break;\r |
52 | };\r | |
53 | \r | |
54 | if(mifare_classic_auth(pcs, cuid, blockNo, keyType, ui64Key, AUTH_FIRST)) {\r | |
baeaf579 | 55 | if (MF_DBGLEVEL >= 1) Dbprintf("Auth error");\r |
8556b852 M |
56 | break;\r |
57 | };\r | |
58 | \r | |
59 | if(mifare_classic_readblock(pcs, cuid, blockNo, dataoutbuf)) {\r | |
baeaf579 | 60 | if (MF_DBGLEVEL >= 1) Dbprintf("Read block error");\r |
8556b852 M |
61 | break;\r |
62 | };\r | |
63 | \r | |
64 | if(mifare_classic_halt(pcs, cuid)) {\r | |
baeaf579 | 65 | if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");\r |
8556b852 M |
66 | break;\r |
67 | };\r | |
68 | \r | |
69 | isOK = 1;\r | |
70 | break;\r | |
71 | }\r | |
72 | \r | |
8556b852 M |
73 | crypto1_destroy(pcs);\r |
74 | \r | |
75 | if (MF_DBGLEVEL >= 2) DbpString("READ BLOCK FINISHED");\r | |
76 | \r | |
8556b852 | 77 | LED_B_ON();\r |
baeaf579 | 78 | cmd_send(CMD_ACK,isOK,0,0,dataoutbuf,16);\r |
8556b852 M |
79 | LED_B_OFF();\r |
80 | \r | |
a631936e | 81 | FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r |
82 | LEDsoff();\r | |
83 | }\r | |
84 | \r | |
4d2e4eea | 85 | void MifareUC_Auth(uint8_t arg0, uint8_t *keybytes){\r |
a631936e | 86 | \r |
4d2e4eea | 87 | bool turnOffField = (arg0 == 1);\r |
a631936e | 88 | \r |
aa60d156 | 89 | LED_A_ON(); LED_B_OFF(); LED_C_OFF();\r |
99cf19d9 | 90 | \r |
a631936e | 91 | iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r |
92 | \r | |
99cf19d9 | 93 | clear_trace();\r |
810f5379 | 94 | set_tracing(true);\r |
99cf19d9 | 95 | \r |
c188b1b9 | 96 | if(!iso14443a_select_card(NULL, NULL, NULL, true, 0)) {\r |
aa60d156 | 97 | if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Can't select card");\r |
0ec548dc | 98 | OnError(0);\r |
a631936e | 99 | return;\r |
100 | };\r | |
101 | \r | |
e7e95088 | 102 | if(!mifare_ultra_auth(keybytes)){\r |
4d2e4eea | 103 | if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Authentication failed");\r |
0ec548dc | 104 | OnError(1);\r |
a631936e | 105 | return;\r |
106 | }\r | |
107 | \r | |
4d2e4eea | 108 | if (turnOffField) {\r |
109 | FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r | |
110 | LEDsoff();\r | |
cceabb79 | 111 | }\r |
9d87eb66 | 112 | cmd_send(CMD_ACK,1,0,0,0,0);\r |
7cc204bf | 113 | }\r |
114 | \r | |
8d53ea14 | 115 | // Arg0 = BlockNo,\r |
116 | // Arg1 = UsePwd bool\r | |
75377d29 | 117 | // datain = PWD bytes,\r |
0ec548dc | 118 | void MifareUReadBlock(uint8_t arg0, uint8_t arg1, uint8_t *datain)\r |
7cc204bf | 119 | {\r |
7cc204bf | 120 | uint8_t blockNo = arg0;\r |
787b5bd8 | 121 | byte_t dataout[16] = {0x00};\r |
4d2e4eea | 122 | bool useKey = (arg1 == 1); //UL_C\r |
123 | bool usePwd = (arg1 == 2); //UL_EV1/NTAG\r | |
8d53ea14 | 124 | \r |
75377d29 | 125 | LEDsoff();\r |
126 | LED_A_ON();\r | |
787b5bd8 | 127 | iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r |
f168b263 | 128 | \r |
99cf19d9 | 129 | clear_trace();\r |
810f5379 | 130 | set_tracing(true);\r |
99cf19d9 | 131 | \r |
c188b1b9 | 132 | int len = iso14443a_select_card(NULL, NULL, NULL, true, 0);\r |
787b5bd8 | 133 | if(!len) {\r |
aa60d156 | 134 | if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Can't select card (RC:%02X)",len);\r |
0ec548dc | 135 | OnError(1);\r |
787b5bd8 | 136 | return;\r |
aa60d156 | 137 | }\r |
f168b263 | 138 | \r |
4d2e4eea | 139 | // UL-C authentication\r |
140 | if ( useKey ) {\r | |
8258f409 | 141 | uint8_t key[16] = {0x00};\r |
4d2e4eea | 142 | memcpy(key, datain, sizeof(key) );\r |
143 | \r | |
e7e95088 | 144 | if ( !mifare_ultra_auth(key) ) {\r |
aa60d156 | 145 | OnError(1);\r |
f168b263 | 146 | return;\r |
0ec548dc | 147 | }\r |
4d2e4eea | 148 | }\r |
f168b263 | 149 | \r |
4d2e4eea | 150 | // UL-EV1 / NTAG authentication\r |
8258f409 | 151 | if ( usePwd ) {\r |
4d2e4eea | 152 | uint8_t pwd[4] = {0x00};\r |
153 | memcpy(pwd, datain, 4);\r | |
154 | uint8_t pack[4] = {0,0,0,0};\r | |
e7e95088 | 155 | if (!mifare_ul_ev1_auth(pwd, pack)) {\r |
0ec548dc | 156 | OnError(1);\r |
f168b263 | 157 | return;\r |
062b7cb9 | 158 | }\r |
8258f409 | 159 | } \r |
f168b263 | 160 | \r |
aa60d156 | 161 | if( mifare_ultra_readblock(blockNo, dataout) ) {\r |
162 | if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Read block error");\r | |
0ec548dc | 163 | OnError(2);\r |
787b5bd8 | 164 | return;\r |
aa60d156 | 165 | }\r |
8258f409 | 166 | \r |
aa60d156 | 167 | if( mifare_ultra_halt() ) {\r |
168 | if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Halt error");\r | |
0ec548dc | 169 | OnError(3);\r |
787b5bd8 | 170 | return;\r |
aa60d156 | 171 | }\r |
8258f409 | 172 | \r |
787b5bd8 | 173 | cmd_send(CMD_ACK,1,0,0,dataout,16);\r |
7cc204bf | 174 | FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r |
175 | LEDsoff();\r | |
176 | }\r | |
e7e95088 | 177 | \r |
7cc204bf | 178 | //-----------------------------------------------------------------------------\r |
baeaf579 | 179 | // Select, Authenticate, Read a MIFARE tag. \r |
180 | // read sector (data = 4 x 16 bytes = 64 bytes, or 16 x 16 bytes = 256 bytes)\r | |
8556b852 M |
181 | //-----------------------------------------------------------------------------\r |
182 | void MifareReadSector(uint8_t arg0, uint8_t arg1, uint8_t arg2, uint8_t *datain)\r | |
183 | {\r | |
184 | // params\r | |
185 | uint8_t sectorNo = arg0;\r | |
186 | uint8_t keyType = arg1;\r | |
187 | uint64_t ui64Key = 0;\r | |
188 | ui64Key = bytes_to_num(datain, 6);\r | |
189 | \r | |
190 | // variables\r | |
3fe4ff4f | 191 | byte_t isOK = 0;\r |
baeaf579 | 192 | byte_t dataoutbuf[16 * 16];\r |
d798d31c | 193 | uint8_t uid[10] = {0x00};\r |
194 | uint32_t cuid = 0;\r | |
8556b852 M |
195 | struct Crypto1State mpcs = {0, 0};\r |
196 | struct Crypto1State *pcs;\r | |
197 | pcs = &mpcs;\r | |
198 | \r | |
7bc95e2e | 199 | iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r |
8556b852 | 200 | \r |
99cf19d9 | 201 | clear_trace();\r |
810f5379 | 202 | set_tracing(true);\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 |
c188b1b9 | 209 | if(!iso14443a_select_card(uid, NULL, &cuid, true, 0)) {\r |
baeaf579 | 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 | if (MF_DBGLEVEL >= 2) DbpString("READ SECTOR FINISHED");\r |
233 | \r | |
88e20c9f | 234 | crypto1_destroy(pcs);\r |
235 | \r | |
8556b852 | 236 | LED_B_ON();\r |
baeaf579 | 237 | cmd_send(CMD_ACK,isOK,0,0,dataoutbuf,16*NumBlocksPerSector(sectorNo));\r |
6e82300d | 238 | LED_B_OFF();\r |
8556b852 | 239 | \r |
8556b852 M |
240 | FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r |
241 | LEDsoff();\r | |
88e20c9f | 242 | set_tracing(FALSE);\r |
7cc204bf | 243 | }\r |
244 | \r | |
8d53ea14 | 245 | // arg0 = blockNo (start)\r |
246 | // arg1 = Pages (number of blocks)\r | |
247 | // arg2 = useKey\r | |
248 | // datain = KEY bytes\r | |
249 | void MifareUReadCard(uint8_t arg0, uint16_t arg1, uint8_t arg2, uint8_t *datain)\r | |
7cc204bf | 250 | {\r |
99cf19d9 | 251 | LEDsoff();\r |
252 | LED_A_ON();\r | |
253 | iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r | |
254 | \r | |
0a0d9a58 | 255 | // free eventually allocated BigBuf memory\r |
aaa1a9a2 | 256 | BigBuf_free(); BigBuf_Clear_ext(false);\r |
0a0d9a58 | 257 | clear_trace();\r |
810f5379 | 258 | set_tracing(true);\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 |
c188b1b9 | 273 | int len = iso14443a_select_card(NULL, NULL, NULL, true, 0);\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 |
88e20c9f | 341 | set_tracing(FALSE);\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 | |
d798d31c | 354 | byte_t blockdata[16] = {0x00};\r |
8556b852 M |
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 | |
d798d31c | 361 | uint8_t uid[10] = {0x00};\r |
362 | uint32_t cuid = 0;\r | |
8556b852 M |
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 |
810f5379 | 370 | set_tracing(true);\r |
371 | \r | |
8556b852 M |
372 | LED_A_ON();\r |
373 | LED_B_OFF();\r | |
374 | LED_C_OFF();\r | |
375 | \r | |
376 | while (true) {\r | |
f8850434 | 377 | if(!iso14443a_select_card(uid, NULL, &cuid, true, 0)) {\r |
8556b852 M |
378 | if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");\r |
379 | break;\r | |
380 | };\r | |
381 | \r | |
382 | if(mifare_classic_auth(pcs, cuid, blockNo, keyType, ui64Key, AUTH_FIRST)) {\r | |
383 | if (MF_DBGLEVEL >= 1) Dbprintf("Auth error");\r | |
384 | break;\r | |
385 | };\r | |
386 | \r | |
387 | if(mifare_classic_writeblock(pcs, cuid, blockNo, blockdata)) {\r | |
388 | if (MF_DBGLEVEL >= 1) Dbprintf("Write block error");\r | |
389 | break;\r | |
390 | };\r | |
391 | \r | |
392 | if(mifare_classic_halt(pcs, cuid)) {\r | |
393 | if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");\r | |
394 | break;\r | |
395 | };\r | |
396 | \r | |
397 | isOK = 1;\r | |
398 | break;\r | |
399 | }\r | |
400 | \r | |
8556b852 M |
401 | crypto1_destroy(pcs);\r |
402 | \r | |
403 | if (MF_DBGLEVEL >= 2) DbpString("WRITE BLOCK FINISHED");\r | |
404 | \r | |
9492e0b0 | 405 | cmd_send(CMD_ACK,isOK,0,0,0,0);\r |
8556b852 | 406 | \r |
8556b852 M |
407 | FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r |
408 | LEDsoff();\r | |
88e20c9f | 409 | set_tracing(FALSE);\r |
7cc204bf | 410 | }\r |
411 | \r | |
95aeb706 | 412 | /* // Command not needed but left for future testing \r |
413 | void MifareUWriteBlockCompat(uint8_t arg0, uint8_t *datain)\r | |
7cc204bf | 414 | {\r |
f168b263 | 415 | uint8_t blockNo = arg0;\r |
787b5bd8 | 416 | byte_t blockdata[16] = {0x00};\r |
7cc204bf | 417 | \r |
f168b263 | 418 | memcpy(blockdata, datain, 16);\r |
419 | \r | |
787b5bd8 | 420 | uint8_t uid[10] = {0x00};\r |
7cc204bf | 421 | \r |
aa60d156 | 422 | LED_A_ON(); LED_B_OFF(); LED_C_OFF();\r |
7cc204bf | 423 | \r |
aa60d156 | 424 | clear_trace();\r |
810f5379 | 425 | set_tracing(true);\r |
aa60d156 | 426 | iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r |
7cc204bf | 427 | \r |
c188b1b9 | 428 | if(!iso14443a_select_card(uid, NULL, NULL, true, 0)) {\r |
aa60d156 | 429 | if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");\r |
430 | OnError(0);\r | |
431 | return;\r | |
432 | };\r | |
433 | \r | |
95aeb706 | 434 | if(mifare_ultra_writeblock_compat(blockNo, blockdata)) {\r |
aa60d156 | 435 | if (MF_DBGLEVEL >= 1) Dbprintf("Write block error");\r |
436 | OnError(0);\r | |
437 | return; };\r | |
438 | \r | |
439 | if(mifare_ultra_halt()) {\r | |
440 | if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");\r | |
441 | OnError(0);\r | |
442 | return;\r | |
443 | };\r | |
f168b263 | 444 | \r |
aa60d156 | 445 | if (MF_DBGLEVEL >= 2) DbpString("WRITE BLOCK FINISHED");\r |
7cc204bf | 446 | \r |
aa60d156 | 447 | cmd_send(CMD_ACK,1,0,0,0,0);\r |
448 | FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r | |
449 | LEDsoff();\r | |
7cc204bf | 450 | }\r |
95aeb706 | 451 | */\r |
7cc204bf | 452 | \r |
fff69a1e | 453 | // Arg0 : Block to write to.\r |
454 | // Arg1 : 0 = use no authentication.\r | |
455 | // 1 = use 0x1A authentication.\r | |
456 | // 2 = use 0x1B authentication.\r | |
457 | // datain : 4 first bytes is data to be written.\r | |
458 | // : 4/16 next bytes is authentication key.\r | |
95aeb706 | 459 | void MifareUWriteBlock(uint8_t arg0, uint8_t arg1, uint8_t *datain)\r |
7cc204bf | 460 | {\r |
baeaf579 | 461 | uint8_t blockNo = arg0;\r |
fff69a1e | 462 | bool useKey = (arg1 == 1); //UL_C\r |
463 | bool usePwd = (arg1 == 2); //UL_EV1/NTAG\r | |
787b5bd8 | 464 | byte_t blockdata[4] = {0x00};\r |
7cc204bf | 465 | \r |
8258f409 | 466 | memcpy(blockdata, datain,4);\r |
aa60d156 | 467 | \r |
8d53ea14 | 468 | LEDsoff();\r |
469 | LED_A_ON();\r | |
aa60d156 | 470 | iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r |
471 | \r | |
99cf19d9 | 472 | clear_trace();\r |
810f5379 | 473 | set_tracing(true);\r |
474 | \r | |
c188b1b9 | 475 | if(!iso14443a_select_card(NULL, NULL, NULL, true, 0)) {\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 | |
88e20c9f | 520 | set_tracing(FALSE);\r |
aa60d156 | 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 |
810f5379 | 534 | set_tracing(true);\r |
535 | \r | |
c188b1b9 | 536 | if(!iso14443a_select_card(NULL, NULL, NULL, true, 0)) {\r |
aa60d156 | 537 | if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");\r |
538 | OnError(0);\r | |
539 | return;\r | |
540 | };\r | |
7cc204bf | 541 | \r |
aa60d156 | 542 | blockdata[0] = pwd[7];\r |
543 | blockdata[1] = pwd[6];\r | |
544 | blockdata[2] = pwd[5];\r | |
545 | blockdata[3] = pwd[4];\r | |
95aeb706 | 546 | if(mifare_ultra_writeblock( 44, blockdata)) {\r |
aa60d156 | 547 | if (MF_DBGLEVEL >= 1) Dbprintf("Write block error");\r |
548 | OnError(44);\r | |
549 | return;\r | |
550 | };\r | |
7cc204bf | 551 | \r |
aa60d156 | 552 | blockdata[0] = pwd[3];\r |
553 | blockdata[1] = pwd[2];\r | |
554 | blockdata[2] = pwd[1];\r | |
555 | blockdata[3] = pwd[0];\r | |
95aeb706 | 556 | if(mifare_ultra_writeblock( 45, blockdata)) {\r |
aa60d156 | 557 | if (MF_DBGLEVEL >= 1) Dbprintf("Write block error");\r |
558 | OnError(45);\r | |
559 | return;\r | |
560 | };\r | |
7cc204bf | 561 | \r |
aa60d156 | 562 | blockdata[0] = pwd[15];\r |
563 | blockdata[1] = pwd[14];\r | |
564 | blockdata[2] = pwd[13];\r | |
565 | blockdata[3] = pwd[12];\r | |
95aeb706 | 566 | if(mifare_ultra_writeblock( 46, blockdata)) {\r |
aa60d156 | 567 | if (MF_DBGLEVEL >= 1) Dbprintf("Write block error");\r |
568 | OnError(46);\r | |
569 | return;\r | |
570 | };\r | |
7cc204bf | 571 | \r |
aa60d156 | 572 | blockdata[0] = pwd[11];\r |
573 | blockdata[1] = pwd[10];\r | |
574 | blockdata[2] = pwd[9];\r | |
575 | blockdata[3] = pwd[8];\r | |
95aeb706 | 576 | if(mifare_ultra_writeblock( 47, blockdata)) {\r |
aa60d156 | 577 | if (MF_DBGLEVEL >= 1) Dbprintf("Write block error");\r |
578 | OnError(47);\r | |
579 | return;\r | |
580 | }; \r | |
7cc204bf | 581 | \r |
aa60d156 | 582 | if(mifare_ultra_halt()) {\r |
583 | if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");\r | |
584 | OnError(0);\r | |
585 | return;\r | |
586 | };\r | |
7cc204bf | 587 | \r |
aa60d156 | 588 | cmd_send(CMD_ACK,1,0,0,0,0);\r |
baeaf579 | 589 | FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r |
590 | LEDsoff();\r | |
88e20c9f | 591 | set_tracing(FALSE);\r |
7cc204bf | 592 | }\r |
593 | \r | |
594 | // Return 1 if the nonce is invalid else return 0\r | |
6a1f2d82 | 595 | int valid_nonce(uint32_t Nt, uint32_t NtEnc, uint32_t Ks1, uint8_t *parity) {\r |
f8ada309 | 596 | return ((oddparity8((Nt >> 24) & 0xFF) == ((parity[0]) ^ oddparity8((NtEnc >> 24) & 0xFF) ^ BIT(Ks1,16))) & \\r |
597 | (oddparity8((Nt >> 16) & 0xFF) == ((parity[1]) ^ oddparity8((NtEnc >> 16) & 0xFF) ^ BIT(Ks1,8))) & \\r | |
598 | (oddparity8((Nt >> 8) & 0xFF) == ((parity[2]) ^ oddparity8((NtEnc >> 8) & 0xFF) ^ BIT(Ks1,0)))) ? 1 : 0;\r | |
8556b852 M |
599 | }\r |
600 | \r | |
9492e0b0 | 601 | \r |
c188b1b9 | 602 | //-----------------------------------------------------------------------------\r |
603 | // acquire encrypted nonces in order to perform the attack described in\r | |
604 | // Carlo Meijer, Roel Verdult, "Ciphertext-only Cryptanalysis on Hardened\r | |
605 | // Mifare Classic Cards" in Proceedings of the 22nd ACM SIGSAC Conference on \r | |
606 | // Computer and Communications Security, 2015\r | |
607 | //-----------------------------------------------------------------------------\r | |
8d61d9b1 | 608 | #define AUTHENTICATION_TIMEOUT 848 //848 // card times out 1ms after wrong authentication (according to NXP documentation)\r |
86d597a5 | 609 | #define PRE_AUTHENTICATION_LEADTIME 400 // some (non standard) cards need a pause after select before they are ready for first authentication \r |
610 | \r | |
c188b1b9 | 611 | void MifareAcquireEncryptedNonces(uint32_t arg0, uint32_t arg1, uint32_t flags, uint8_t *datain)\r |
612 | {\r | |
613 | uint64_t ui64Key = 0;\r | |
d798d31c | 614 | uint8_t uid[10] = {0x00};\r |
4a71da5a | 615 | uint32_t cuid = 0;\r |
c188b1b9 | 616 | uint8_t cascade_levels = 0;\r |
617 | struct Crypto1State mpcs = {0, 0};\r | |
618 | struct Crypto1State *pcs;\r | |
619 | pcs = &mpcs;\r | |
d798d31c | 620 | uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE] = {0x00};\r |
c188b1b9 | 621 | int16_t isOK = 0;\r |
d798d31c | 622 | uint8_t par_enc[1] = {0x00};\r |
c188b1b9 | 623 | uint8_t nt_par_enc = 0;\r |
d798d31c | 624 | uint8_t buf[USB_CMD_DATA_SIZE] = {0x00};\r |
625 | uint32_t timeout = 0;\r | |
c188b1b9 | 626 | \r |
627 | uint8_t blockNo = arg0 & 0xff;\r | |
628 | uint8_t keyType = (arg0 >> 8) & 0xff;\r | |
629 | uint8_t targetBlockNo = arg1 & 0xff;\r | |
630 | uint8_t targetKeyType = (arg1 >> 8) & 0xff;\r | |
631 | ui64Key = bytes_to_num(datain, 6);\r | |
632 | bool initialize = flags & 0x0001;\r | |
633 | bool slow = flags & 0x0002;\r | |
634 | bool field_off = flags & 0x0004;\r | |
635 | \r | |
c188b1b9 | 636 | LED_A_ON();\r |
637 | LED_C_OFF();\r | |
638 | \r | |
77f3f9ff | 639 | BigBuf_free(); BigBuf_Clear_ext(false); \r |
640 | clear_trace();\r | |
641 | set_tracing(FALSE);\r | |
642 | \r | |
c188b1b9 | 643 | if (initialize) {\r |
644 | iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r | |
c188b1b9 | 645 | }\r |
77f3f9ff | 646 | \r |
c188b1b9 | 647 | LED_C_ON();\r |
648 | \r | |
77f3f9ff | 649 | uint8_t dummy_answer = 0; \r |
c188b1b9 | 650 | uint16_t num_nonces = 0;\r |
651 | bool have_uid = false;\r | |
652 | for (uint16_t i = 0; i <= USB_CMD_DATA_SIZE - 9; ) {\r | |
653 | \r | |
654 | // Test if the action was cancelled\r | |
655 | if(BUTTON_PRESS()) {\r | |
656 | isOK = 2;\r | |
657 | field_off = true;\r | |
658 | break;\r | |
659 | }\r | |
660 | \r | |
661 | if (!have_uid) { // need a full select cycle to get the uid first\r | |
662 | iso14a_card_select_t card_info; \r | |
663 | if(!iso14443a_select_card(uid, &card_info, &cuid, true, 0)) {\r | |
664 | if (MF_DBGLEVEL >= 1) Dbprintf("AcquireNonces: Can't select card (ALL)");\r | |
665 | continue;\r | |
666 | }\r | |
667 | switch (card_info.uidlen) {\r | |
668 | case 4 : cascade_levels = 1; break;\r | |
669 | case 7 : cascade_levels = 2; break;\r | |
670 | case 10: cascade_levels = 3; break;\r | |
671 | default: break;\r | |
672 | }\r | |
673 | have_uid = true; \r | |
674 | } else { // no need for anticollision. We can directly select the card\r | |
675 | if(!iso14443a_select_card(uid, NULL, NULL, false, cascade_levels)) {\r | |
676 | if (MF_DBGLEVEL >= 1) Dbprintf("AcquireNonces: Can't select card (UID)");\r | |
677 | continue;\r | |
678 | }\r | |
679 | }\r | |
680 | \r | |
681 | if (slow) {\r | |
682 | timeout = GetCountSspClk() + PRE_AUTHENTICATION_LEADTIME;\r | |
683 | while(GetCountSspClk() < timeout);\r | |
684 | }\r | |
685 | \r | |
686 | uint32_t nt1;\r | |
687 | if (mifare_classic_authex(pcs, cuid, blockNo, keyType, ui64Key, AUTH_FIRST, &nt1, NULL)) {\r | |
688 | if (MF_DBGLEVEL >= 1) Dbprintf("AcquireNonces: Auth1 error");\r | |
689 | continue;\r | |
690 | }\r | |
691 | \r | |
692 | // nested authentication\r | |
693 | uint16_t len = mifare_sendcmd_short(pcs, AUTH_NESTED, 0x60 + (targetKeyType & 0x01), targetBlockNo, receivedAnswer, par_enc, NULL);\r | |
694 | if (len != 4) {\r | |
695 | if (MF_DBGLEVEL >= 1) Dbprintf("AcquireNonces: Auth2 error len=%d", len);\r | |
696 | continue;\r | |
697 | }\r | |
698 | \r | |
699 | // send a dummy byte as reader response in order to trigger the cards authentication timeout\r | |
c188b1b9 | 700 | ReaderTransmit(&dummy_answer, 1, NULL);\r |
701 | timeout = GetCountSspClk() + AUTHENTICATION_TIMEOUT;\r | |
702 | \r | |
703 | num_nonces++;\r | |
704 | if (num_nonces % 2) {\r | |
705 | memcpy(buf+i, receivedAnswer, 4);\r | |
706 | nt_par_enc = par_enc[0] & 0xf0;\r | |
707 | } else {\r | |
0b53530a | 708 | nt_par_enc |= par_enc[0] >> 4;\r |
c188b1b9 | 709 | memcpy(buf+i+4, receivedAnswer, 4);\r |
710 | memcpy(buf+i+8, &nt_par_enc, 1);\r | |
711 | i += 9;\r | |
712 | }\r | |
c188b1b9 | 713 | // wait for the card to become ready again\r |
68410a48 | 714 | while(GetCountSspClk() < timeout); \r |
c188b1b9 | 715 | }\r |
716 | \r | |
717 | LED_C_OFF();\r | |
0b53530a | 718 | crypto1_destroy(pcs); \r |
c188b1b9 | 719 | LED_B_ON();\r |
720 | cmd_send(CMD_ACK, isOK, cuid, num_nonces, buf, sizeof(buf));\r | |
721 | LED_B_OFF();\r | |
722 | \r | |
723 | if (MF_DBGLEVEL >= 3) DbpString("AcquireEncryptedNonces finished");\r | |
724 | \r | |
725 | if (field_off) {\r | |
726 | FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r | |
727 | LEDsoff();\r | |
77f3f9ff | 728 | set_tracing(FALSE);\r |
c188b1b9 | 729 | }\r |
730 | }\r | |
731 | \r | |
732 | \r | |
8556b852 M |
733 | //-----------------------------------------------------------------------------\r |
734 | // MIFARE nested authentication. \r | |
735 | // \r | |
736 | //-----------------------------------------------------------------------------\r | |
9492e0b0 | 737 | void MifareNested(uint32_t arg0, uint32_t arg1, uint32_t calibrate, uint8_t *datain)\r |
8556b852 M |
738 | {\r |
739 | // params\r | |
9492e0b0 | 740 | uint8_t blockNo = arg0 & 0xff;\r |
741 | uint8_t keyType = (arg0 >> 8) & 0xff;\r | |
742 | uint8_t targetBlockNo = arg1 & 0xff;\r | |
743 | uint8_t targetKeyType = (arg1 >> 8) & 0xff;\r | |
8556b852 M |
744 | uint64_t ui64Key = 0;\r |
745 | \r | |
746 | ui64Key = bytes_to_num(datain, 6);\r | |
747 | \r | |
748 | // variables\r | |
9492e0b0 | 749 | uint16_t rtr, i, j, len;\r |
d798d31c | 750 | uint16_t davg = 0;\r |
9492e0b0 | 751 | static uint16_t dmin, dmax;\r |
d798d31c | 752 | uint8_t uid[10] = {0x00};\r |
5ebcb867 | 753 | uint32_t cuid = 0, nt1, nt2, nttmp, nttest, ks1;\r |
d798d31c | 754 | uint8_t par[1] = {0x00};\r |
755 | uint32_t target_nt[2] = {0x00}, target_ks[2] = {0x00};\r | |
9492e0b0 | 756 | \r |
d798d31c | 757 | uint8_t par_array[4] = {0x00};\r |
9492e0b0 | 758 | uint16_t ncount = 0;\r |
8556b852 M |
759 | struct Crypto1State mpcs = {0, 0};\r |
760 | struct Crypto1State *pcs;\r | |
761 | pcs = &mpcs;\r | |
5ebcb867 | 762 | uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE] = {0x00};\r |
8556b852 | 763 | \r |
9492e0b0 | 764 | uint32_t auth1_time, auth2_time;\r |
7504dc50 | 765 | static uint16_t delta_time = 0;\r |
9492e0b0 | 766 | \r |
99cf19d9 | 767 | LED_A_ON();\r |
768 | LED_C_OFF();\r | |
769 | iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r | |
770 | \r | |
f71f4deb | 771 | // free eventually allocated BigBuf memory\r |
aaa1a9a2 | 772 | BigBuf_free(); BigBuf_Clear_ext(false);\r |
773 | \r | |
b10a759f | 774 | if (calibrate) clear_trace();\r |
775 | set_tracing(true);\r | |
8556b852 | 776 | \r |
9492e0b0 | 777 | // statistics on nonce distance\r |
c830303d | 778 | int16_t isOK = 0;\r |
779 | #define NESTED_MAX_TRIES 12\r | |
780 | uint16_t unsuccessfull_tries = 0;\r | |
9492e0b0 | 781 | if (calibrate) { // for first call only. Otherwise reuse previous calibration\r |
782 | LED_B_ON();\r | |
3fe4ff4f | 783 | WDT_HIT();\r |
8556b852 | 784 | \r |
9492e0b0 | 785 | davg = dmax = 0;\r |
786 | dmin = 2000;\r | |
787 | delta_time = 0;\r | |
8556b852 | 788 | \r |
9492e0b0 | 789 | for (rtr = 0; rtr < 17; rtr++) {\r |
8556b852 | 790 | \r |
c830303d | 791 | // Test if the action was cancelled\r |
792 | if(BUTTON_PRESS()) {\r | |
793 | isOK = -2;\r | |
794 | break;\r | |
795 | }\r | |
796 | \r | |
9492e0b0 | 797 | // prepare next select. No need to power down the card.\r |
798 | if(mifare_classic_halt(pcs, cuid)) {\r | |
799 | if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Halt error");\r | |
800 | rtr--;\r | |
801 | continue;\r | |
802 | }\r | |
803 | \r | |
c188b1b9 | 804 | if(!iso14443a_select_card(uid, NULL, &cuid, true, 0)) {\r |
9492e0b0 | 805 | if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Can't select card");\r |
806 | rtr--;\r | |
807 | continue;\r | |
808 | };\r | |
809 | \r | |
810 | auth1_time = 0;\r | |
811 | if(mifare_classic_authex(pcs, cuid, blockNo, keyType, ui64Key, AUTH_FIRST, &nt1, &auth1_time)) {\r | |
812 | if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Auth1 error");\r | |
813 | rtr--;\r | |
814 | continue;\r | |
815 | };\r | |
7504dc50 | 816 | auth2_time = (delta_time) ? auth1_time + delta_time : 0;\r |
9492e0b0 | 817 | \r |
9492e0b0 | 818 | if(mifare_classic_authex(pcs, cuid, blockNo, keyType, ui64Key, AUTH_NESTED, &nt2, &auth2_time)) {\r |
819 | if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Auth2 error");\r | |
820 | rtr--;\r | |
821 | continue;\r | |
822 | };\r | |
823 | \r | |
b19bd5d6 | 824 | nttmp = prng_successor(nt1, 100); //NXP Mifare is typical around 840,but for some unlicensed/compatible mifare card this can be 160\r |
825 | for (i = 101; i < 1200; i++) {\r | |
2abdfa49 | 826 | nttmp = prng_successor(nttmp, 1);\r |
9492e0b0 | 827 | if (nttmp == nt2) break;\r |
828 | }\r | |
829 | \r | |
830 | if (i != 1200) {\r | |
831 | if (rtr != 0) {\r | |
832 | davg += i;\r | |
833 | dmin = MIN(dmin, i);\r | |
834 | dmax = MAX(dmax, i);\r | |
835 | }\r | |
836 | else {\r | |
837 | delta_time = auth2_time - auth1_time + 32; // allow some slack for proper timing\r | |
838 | }\r | |
839 | if (MF_DBGLEVEL >= 3) Dbprintf("Nested: calibrating... ntdist=%d", i);\r | |
c830303d | 840 | } else {\r |
841 | unsuccessfull_tries++;\r | |
842 | if (unsuccessfull_tries > NESTED_MAX_TRIES) { // card isn't vulnerable to nested attack (random numbers are not predictable)\r | |
843 | isOK = -3;\r | |
844 | }\r | |
9492e0b0 | 845 | }\r |
8556b852 | 846 | }\r |
8556b852 | 847 | \r |
9492e0b0 | 848 | davg = (davg + (rtr - 1)/2) / (rtr - 1);\r |
849 | \r | |
c830303d | 850 | 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 | 851 | \r |
9492e0b0 | 852 | dmin = davg - 2;\r |
853 | dmax = davg + 2;\r | |
854 | \r | |
855 | LED_B_OFF();\r | |
9492e0b0 | 856 | }\r |
8556b852 M |
857 | // ------------------------------------------------------------------------------------------------- \r |
858 | \r | |
859 | LED_C_ON();\r | |
860 | \r | |
861 | // get crypted nonces for target sector\r | |
c830303d | 862 | for(i=0; i < 2 && !isOK; i++) { // look for exactly two different nonces\r |
8556b852 | 863 | \r |
9492e0b0 | 864 | target_nt[i] = 0;\r |
865 | while(target_nt[i] == 0) { // continue until we have an unambiguous nonce\r | |
8556b852 | 866 | \r |
9492e0b0 | 867 | // prepare next select. No need to power down the card.\r |
868 | if(mifare_classic_halt(pcs, cuid)) {\r | |
869 | if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Halt error");\r | |
870 | continue;\r | |
871 | }\r | |
8556b852 | 872 | \r |
c188b1b9 | 873 | if(!iso14443a_select_card(uid, NULL, &cuid, true, 0)) {\r |
9492e0b0 | 874 | if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Can't select card");\r |
875 | continue;\r | |
876 | };\r | |
8556b852 | 877 | \r |
9492e0b0 | 878 | auth1_time = 0;\r |
879 | if(mifare_classic_authex(pcs, cuid, blockNo, keyType, ui64Key, AUTH_FIRST, &nt1, &auth1_time)) {\r | |
880 | if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Auth1 error");\r | |
881 | continue;\r | |
882 | };\r | |
8556b852 | 883 | \r |
9492e0b0 | 884 | // nested authentication\r |
885 | auth2_time = auth1_time + delta_time;\r | |
7504dc50 | 886 | \r |
2d2f7d19 | 887 | len = mifare_sendcmd_short(pcs, AUTH_NESTED, 0x60 + (targetKeyType & 0x01), targetBlockNo, receivedAnswer, par, &auth2_time);\r |
9492e0b0 | 888 | if (len != 4) {\r |
889 | if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Auth2 error len=%d", len);\r | |
890 | continue;\r | |
891 | };\r | |
8556b852 | 892 | \r |
9492e0b0 | 893 | nt2 = bytes_to_num(receivedAnswer, 4); \r |
6a1f2d82 | 894 | if (MF_DBGLEVEL >= 3) Dbprintf("Nonce#%d: Testing nt1=%08x nt2enc=%08x nt2par=%02x", i+1, nt1, nt2, par[0]);\r |
8556b852 | 895 | \r |
9492e0b0 | 896 | // Parity validity check\r |
7504dc50 | 897 | // for (j = 0; j < 4; j++) {\r |
898 | // par_array[j] = (oddparity8(receivedAnswer[j]) != ((par[0] >> (7-j)) & 0x01));\r | |
899 | // }\r | |
900 | par_array[0] = (oddparity8(receivedAnswer[0]) != ((par[0] >> (7-0)) & 0x01));\r | |
901 | par_array[1] = (oddparity8(receivedAnswer[1]) != ((par[0] >> (7-1)) & 0x01));\r | |
902 | par_array[2] = (oddparity8(receivedAnswer[2]) != ((par[0] >> (7-2)) & 0x01));\r | |
903 | par_array[3] = (oddparity8(receivedAnswer[3]) != ((par[0] >> (7-3)) & 0x01));\r | |
9492e0b0 | 904 | \r |
905 | ncount = 0;\r | |
906 | nttest = prng_successor(nt1, dmin - 1);\r | |
907 | for (j = dmin; j < dmax + 1; j++) {\r | |
2abdfa49 | 908 | nttest = prng_successor(nttest, 1);\r |
9492e0b0 | 909 | ks1 = nt2 ^ nttest;\r |
910 | \r | |
911 | if (valid_nonce(nttest, nt2, ks1, par_array)){\r | |
912 | if (ncount > 0) { // we are only interested in disambiguous nonces, try again\r | |
913 | if (MF_DBGLEVEL >= 3) Dbprintf("Nonce#%d: dismissed (ambigous), ntdist=%d", i+1, j);\r | |
914 | target_nt[i] = 0;\r | |
915 | break;\r | |
916 | }\r | |
917 | target_nt[i] = nttest;\r | |
918 | target_ks[i] = ks1;\r | |
919 | ncount++;\r | |
920 | if (i == 1 && target_nt[1] == target_nt[0]) { // we need two different nonces\r | |
921 | target_nt[i] = 0;\r | |
922 | if (MF_DBGLEVEL >= 3) Dbprintf("Nonce#2: dismissed (= nonce#1), ntdist=%d", j);\r | |
8556b852 M |
923 | break;\r |
924 | }\r | |
9492e0b0 | 925 | if (MF_DBGLEVEL >= 3) Dbprintf("Nonce#%d: valid, ntdist=%d", i+1, j);\r |
8556b852 | 926 | }\r |
8556b852 | 927 | }\r |
9492e0b0 | 928 | if (target_nt[i] == 0 && j == dmax+1 && MF_DBGLEVEL >= 3) Dbprintf("Nonce#%d: dismissed (all invalid)", i+1);\r |
8556b852 M |
929 | }\r |
930 | }\r | |
931 | \r | |
932 | LED_C_OFF();\r | |
933 | \r | |
8556b852 M |
934 | crypto1_destroy(pcs);\r |
935 | \r | |
7504dc50 | 936 | byte_t buf[4 + 4 * 4] = {0};\r |
82885445 | 937 | memcpy(buf, &cuid, 4);\r |
9492e0b0 | 938 | memcpy(buf+4, &target_nt[0], 4);\r |
939 | memcpy(buf+8, &target_ks[0], 4);\r | |
940 | memcpy(buf+12, &target_nt[1], 4);\r | |
941 | memcpy(buf+16, &target_ks[1], 4);\r | |
8556b852 M |
942 | \r |
943 | LED_B_ON();\r | |
c830303d | 944 | cmd_send(CMD_ACK, isOK, 0, targetBlockNo + (targetKeyType * 0x100), buf, sizeof(buf));\r |
6e82300d | 945 | LED_B_OFF();\r |
8556b852 | 946 | \r |
9492e0b0 | 947 | if (MF_DBGLEVEL >= 3) DbpString("NESTED FINISHED");\r |
8556b852 | 948 | \r |
8556b852 M |
949 | FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r |
950 | LEDsoff();\r | |
5ee53a0e | 951 | set_tracing(FALSE);\r |
8556b852 M |
952 | }\r |
953 | \r | |
954 | //-----------------------------------------------------------------------------\r | |
9492e0b0 | 955 | // MIFARE check keys. key count up to 85. \r |
8556b852 M |
956 | // \r |
957 | //-----------------------------------------------------------------------------\r | |
86d597a5 | 958 | void MifareChkKeys(uint16_t arg0, uint8_t arg1, uint8_t arg2, uint8_t *datain) {\r |
b10a759f | 959 | uint8_t blockNo = arg0 & 0xff;\r |
960 | uint8_t keyType = (arg0 >> 8) & 0xff;\r | |
961 | bool clearTrace = arg1;\r | |
8556b852 M |
962 | uint8_t keyCount = arg2;\r |
963 | uint64_t ui64Key = 0;\r | |
964 | \r | |
86d597a5 | 965 | bool have_uid = FALSE;\r |
966 | uint8_t cascade_levels = 0;\r | |
967 | uint32_t timeout = 0;\r | |
968 | \r | |
8556b852 M |
969 | int i;\r |
970 | byte_t isOK = 0;\r | |
d798d31c | 971 | uint8_t uid[10] = {0x00};\r |
972 | uint32_t cuid = 0;\r | |
8556b852 M |
973 | struct Crypto1State mpcs = {0, 0};\r |
974 | struct Crypto1State *pcs;\r | |
975 | pcs = &mpcs;\r | |
976 | \r | |
2abdfa49 | 977 | // save old debuglevel, and tempory turn off dbg printing. speedissues.\r |
8556b852 M |
978 | int OLD_MF_DBGLEVEL = MF_DBGLEVEL; \r |
979 | MF_DBGLEVEL = MF_DBG_NONE;\r | |
980 | \r | |
7504dc50 | 981 | LEDsoff();\r |
8556b852 | 982 | LED_A_ON();\r |
7504dc50 | 983 | \r |
99cf19d9 | 984 | iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r |
985 | \r | |
810f5379 | 986 | if (clearTrace) \r |
987 | clear_trace();\r | |
988 | \r | |
99cf19d9 | 989 | set_tracing(TRUE);\r |
2abdfa49 | 990 | \r |
65c2d21d | 991 | for (i = 0; i < keyCount; ++i) {\r |
8556b852 | 992 | \r |
86d597a5 | 993 | //mifare_classic_halt(pcs, cuid);\r |
8556b852 | 994 | \r |
86d597a5 | 995 | // this part is from Piwi's faster nonce collecting part in Hardnested.\r |
996 | if (!have_uid) { // need a full select cycle to get the uid first\r | |
997 | iso14a_card_select_t card_info; \r | |
998 | if(!iso14443a_select_card(uid, &card_info, &cuid, true, 0)) {\r | |
999 | if (MF_DBGLEVEL >= 1) Dbprintf("ChkKeys: Can't select card (ALL)");\r | |
1000 | break;\r | |
1001 | }\r | |
1002 | switch (card_info.uidlen) {\r | |
1003 | case 4 : cascade_levels = 1; break;\r | |
1004 | case 7 : cascade_levels = 2; break;\r | |
1005 | case 10: cascade_levels = 3; break;\r | |
1006 | default: break;\r | |
1007 | }\r | |
1008 | have_uid = TRUE; \r | |
1009 | } else { // no need for anticollision. We can directly select the card\r | |
1010 | if(!iso14443a_select_card(uid, NULL, NULL, false, cascade_levels)) {\r | |
1011 | if (MF_DBGLEVEL >= 1) Dbprintf("ChkKeys: Can't select card (UID)");\r | |
1012 | continue;\r | |
1013 | }\r | |
1014 | }\r | |
1015 | \r | |
8556b852 | 1016 | ui64Key = bytes_to_num(datain + i * 6, 6);\r |
2abdfa49 | 1017 | \r |
86d597a5 | 1018 | if (mifare_classic_auth(pcs, cuid, blockNo, keyType, ui64Key, AUTH_FIRST)) {\r |
1019 | \r | |
1020 | uint8_t dummy_answer = 0;\r | |
1021 | ReaderTransmit(&dummy_answer, 1, NULL);\r | |
1022 | timeout = GetCountSspClk() + AUTHENTICATION_TIMEOUT;\r | |
1023 | \r | |
1024 | // wait for the card to become ready again\r | |
1025 | while(GetCountSspClk() < timeout);\r | |
1026 | \r | |
8556b852 | 1027 | continue;\r |
86d597a5 | 1028 | }\r |
8556b852 M |
1029 | isOK = 1;\r |
1030 | break;\r | |
1031 | }\r | |
8556b852 | 1032 | \r |
8556b852 | 1033 | LED_B_ON();\r |
2abdfa49 | 1034 | cmd_send(CMD_ACK, isOK, 0, 0, datain + i * 6, 6);\r |
838c15a6 | 1035 | \r |
2abdfa49 | 1036 | // restore debug level\r |
1037 | MF_DBGLEVEL = OLD_MF_DBGLEVEL; \r | |
1038 | \r | |
8556b852 M |
1039 | FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r |
1040 | LEDsoff();\r | |
5ee53a0e | 1041 | set_tracing(FALSE);\r |
838c15a6 | 1042 | crypto1_destroy(pcs);\r |
8556b852 M |
1043 | }\r |
1044 | \r | |
1045 | //-----------------------------------------------------------------------------\r | |
1046 | // MIFARE commands set debug level\r | |
1047 | // \r | |
1048 | //-----------------------------------------------------------------------------\r | |
1049 | void MifareSetDbgLvl(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain){\r | |
1050 | MF_DBGLEVEL = arg0;\r | |
1051 | Dbprintf("Debug level: %d", MF_DBGLEVEL);\r | |
1052 | }\r | |
1053 | \r | |
1054 | //-----------------------------------------------------------------------------\r | |
1055 | // Work with emulator memory\r | |
1056 | // \r | |
99cf19d9 | 1057 | // Note: we call FpgaDownloadAndGo(FPGA_BITSTREAM_HF) here although FPGA is not\r |
1058 | // involved in dealing with emulator memory. But if it is called later, it might\r | |
1059 | // destroy the Emulator Memory.\r | |
8556b852 | 1060 | //-----------------------------------------------------------------------------\r |
99cf19d9 | 1061 | \r |
8556b852 | 1062 | void MifareEMemClr(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain){\r |
99cf19d9 | 1063 | FpgaDownloadAndGo(FPGA_BITSTREAM_HF);\r |
8556b852 M |
1064 | emlClearMem();\r |
1065 | }\r | |
1066 | \r | |
1067 | void MifareEMemSet(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain){\r | |
99cf19d9 | 1068 | FpgaDownloadAndGo(FPGA_BITSTREAM_HF);\r |
9332b857 | 1069 | if (arg2==0) arg2 = 16; // backwards compat... default bytewidth\r |
0de8e387 | 1070 | emlSetMem_xt(datain, arg0, arg1, arg2); // data, block num, blocks count, block byte width\r |
8556b852 M |
1071 | }\r |
1072 | \r | |
1073 | void MifareEMemGet(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain){\r | |
99cf19d9 | 1074 | FpgaDownloadAndGo(FPGA_BITSTREAM_HF);\r |
d798d31c | 1075 | byte_t buf[USB_CMD_DATA_SIZE] = {0x00};\r |
baeaf579 | 1076 | emlGetMem(buf, arg0, arg1); // data, block num, blocks count (max 4)\r |
8556b852 M |
1077 | \r |
1078 | LED_B_ON();\r | |
3fe4ff4f | 1079 | cmd_send(CMD_ACK,arg0,arg1,0,buf,USB_CMD_DATA_SIZE);\r |
8556b852 M |
1080 | LED_B_OFF();\r |
1081 | }\r | |
1082 | \r | |
1083 | //-----------------------------------------------------------------------------\r | |
1084 | // Load a card into the emulator memory\r | |
1085 | // \r | |
1086 | //-----------------------------------------------------------------------------\r | |
1087 | void MifareECardLoad(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain){\r | |
baeaf579 | 1088 | uint8_t numSectors = arg0;\r |
8556b852 M |
1089 | uint8_t keyType = arg1;\r |
1090 | uint64_t ui64Key = 0;\r | |
d798d31c | 1091 | uint32_t cuid = 0;\r |
8556b852 M |
1092 | struct Crypto1State mpcs = {0, 0};\r |
1093 | struct Crypto1State *pcs;\r | |
1094 | pcs = &mpcs;\r | |
1095 | \r | |
1096 | // variables\r | |
d798d31c | 1097 | byte_t dataoutbuf[16] = {0x00};\r |
1098 | byte_t dataoutbuf2[16] = {0x00};\r | |
1099 | uint8_t uid[10] = {0x00};\r | |
8556b852 | 1100 | \r |
8556b852 M |
1101 | LED_A_ON();\r |
1102 | LED_B_OFF();\r | |
1103 | LED_C_OFF();\r | |
99cf19d9 | 1104 | iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r |
1105 | \r | |
1106 | clear_trace();\r | |
5ee53a0e | 1107 | set_tracing(TRUE);\r |
8556b852 | 1108 | \r |
baeaf579 | 1109 | bool isOK = true;\r |
1110 | \r | |
c188b1b9 | 1111 | if(!iso14443a_select_card(uid, NULL, &cuid, true, 0)) {\r |
baeaf579 | 1112 | isOK = false;\r |
1113 | if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");\r | |
1114 | }\r | |
8556b852 | 1115 | \r |
baeaf579 | 1116 | for (uint8_t sectorNo = 0; isOK && sectorNo < numSectors; sectorNo++) {\r |
1117 | ui64Key = emlGetKey(sectorNo, keyType);\r | |
1118 | if (sectorNo == 0){\r | |
1119 | if(isOK && mifare_classic_auth(pcs, cuid, FirstBlockOfSector(sectorNo), keyType, ui64Key, AUTH_FIRST)) {\r | |
1120 | isOK = false;\r | |
1121 | if (MF_DBGLEVEL >= 1) Dbprintf("Sector[%2d]. Auth error", sectorNo);\r | |
8556b852 | 1122 | break;\r |
baeaf579 | 1123 | }\r |
1124 | } else {\r | |
1125 | if(isOK && mifare_classic_auth(pcs, cuid, FirstBlockOfSector(sectorNo), keyType, ui64Key, AUTH_NESTED)) {\r | |
1126 | isOK = false;\r | |
1127 | if (MF_DBGLEVEL >= 1) Dbprintf("Sector[%2d]. Auth nested error", sectorNo);\r | |
8556b852 | 1128 | break;\r |
baeaf579 | 1129 | }\r |
1130 | }\r | |
1131 | \r | |
1132 | for (uint8_t blockNo = 0; isOK && blockNo < NumBlocksPerSector(sectorNo); blockNo++) {\r | |
1133 | if(isOK && mifare_classic_readblock(pcs, cuid, FirstBlockOfSector(sectorNo) + blockNo, dataoutbuf)) {\r | |
1134 | isOK = false;\r | |
1135 | if (MF_DBGLEVEL >= 1) Dbprintf("Error reading sector %2d block %2d", sectorNo, blockNo);\r | |
ab8b654e | 1136 | break;\r |
d798d31c | 1137 | }\r |
baeaf579 | 1138 | if (isOK) {\r |
1139 | if (blockNo < NumBlocksPerSector(sectorNo) - 1) {\r | |
1140 | emlSetMem(dataoutbuf, FirstBlockOfSector(sectorNo) + blockNo, 1);\r | |
1141 | } else { // sector trailer, keep the keys, set only the AC\r | |
1142 | emlGetMem(dataoutbuf2, FirstBlockOfSector(sectorNo) + blockNo, 1);\r | |
1143 | memcpy(&dataoutbuf2[6], &dataoutbuf[6], 4);\r | |
1144 | emlSetMem(dataoutbuf2, FirstBlockOfSector(sectorNo) + blockNo, 1);\r | |
1145 | }\r | |
1146 | }\r | |
8556b852 M |
1147 | }\r |
1148 | \r | |
baeaf579 | 1149 | }\r |
1150 | \r | |
d798d31c | 1151 | if(mifare_classic_halt(pcs, cuid))\r |
1152 | if (MF_DBGLEVEL >= 1)\r | |
1153 | Dbprintf("Halt error");\r | |
8556b852 M |
1154 | \r |
1155 | // ----------------------------- crypto1 destroy\r | |
1156 | crypto1_destroy(pcs);\r | |
ab8b654e M |
1157 | \r |
1158 | FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r | |
1159 | LEDsoff();\r | |
8556b852 M |
1160 | \r |
1161 | if (MF_DBGLEVEL >= 2) DbpString("EMUL FILL SECTORS FINISHED");\r | |
1162 | \r | |
5ee53a0e | 1163 | set_tracing(FALSE);\r |
8556b852 M |
1164 | }\r |
1165 | \r | |
0675f200 M |
1166 | \r |
1167 | //-----------------------------------------------------------------------------\r | |
1168 | // Work with "magic Chinese" card (email him: ouyangweidaxian@live.cn)\r | |
1169 | // \r | |
c2731f37 | 1170 | // PARAMS - workFlags\r |
1171 | // bit 0 - need get UID\r | |
1172 | // bit 1 - need wupC\r | |
1173 | // bit 2 - need HALT after sequence\r | |
1174 | // bit 3 - need turn on FPGA before sequence\r | |
1175 | // bit 4 - need turn off FPGA\r | |
1176 | // bit 5 - need to set datain instead of issuing USB reply (called via ARM for StandAloneMode14a)\r | |
1177 | // bit 6 - wipe tag.\r | |
0675f200 | 1178 | //-----------------------------------------------------------------------------\r |
c2731f37 | 1179 | // magic uid card generation 1 commands\r |
1180 | uint8_t wupC1[] = { MIFARE_MAGICWUPC1 }; \r | |
1181 | uint8_t wupC2[] = { MIFARE_MAGICWUPC2 }; \r | |
1182 | uint8_t wipeC[] = { MIFARE_MAGICWIPEC }; \r | |
1183 | \r | |
1184 | void MifareCSetBlock(uint32_t arg0, uint32_t arg1, uint8_t *datain){\r | |
0675f200 | 1185 | \r |
c2731f37 | 1186 | // params\r |
1187 | uint8_t workFlags = arg0;\r | |
1188 | uint8_t blockNo = arg1;\r | |
0675f200 | 1189 | \r |
0675f200 | 1190 | // variables\r |
6a18e46f | 1191 | bool isOK = false; //assume we will get an error\r |
1192 | uint8_t errormsg = 0x00;\r | |
3fe4ff4f | 1193 | uint8_t uid[10] = {0x00};\r |
c2731f37 | 1194 | uint8_t data[18] = {0x00};\r |
d798d31c | 1195 | uint32_t cuid = 0;\r |
0675f200 | 1196 | \r |
6a18e46f | 1197 | uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE] = {0x00};\r |
1198 | uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE] = {0x00};\r | |
6a1f2d82 | 1199 | \r |
c2731f37 | 1200 | if (workFlags & MAGIC_INIT) {\r |
208a0166 M |
1201 | LED_A_ON();\r |
1202 | LED_B_OFF();\r | |
99cf19d9 | 1203 | iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r |
3000dc4e MHS |
1204 | clear_trace();\r |
1205 | set_tracing(TRUE);\r | |
208a0166 | 1206 | }\r |
0675f200 | 1207 | \r |
6a18e46f | 1208 | //loop doesn't loop just breaks out if error\r |
1209 | while (true) {\r | |
1210 | // read UID and return to client with write\r | |
1211 | if (workFlags & MAGIC_UID) {\r | |
1212 | if(!iso14443a_select_card(uid, NULL, &cuid, true, 0)) {\r | |
1213 | if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Can't select card");\r | |
1214 | errormsg = MAGIC_UID;\r | |
6a18e46f | 1215 | }\r |
d2094433 | 1216 | mifare_classic_halt_ex(NULL);\r |
1217 | break;\r | |
9332b857 | 1218 | }\r |
0675f200 | 1219 | \r |
6a18e46f | 1220 | // wipe tag, fill it with zeros\r |
1221 | if (workFlags & MAGIC_WIPE){\r | |
2abdfa49 | 1222 | ReaderTransmitBitsPar(wupC1, 7, NULL, NULL);\r |
6a18e46f | 1223 | if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != 0x0a)) {\r |
1224 | if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("wupC1 error");\r | |
1225 | errormsg = MAGIC_WIPE;\r | |
1226 | break;\r | |
1227 | }\r | |
0675f200 | 1228 | \r |
6a18e46f | 1229 | ReaderTransmit(wipeC, sizeof(wipeC), NULL);\r |
1230 | if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != 0x0a)) {\r | |
1231 | if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("wipeC error");\r | |
1232 | errormsg = MAGIC_WIPE;\r | |
1233 | break;\r | |
1234 | }\r | |
0675f200 | 1235 | \r |
d2094433 | 1236 | mifare_classic_halt_ex(NULL);\r |
6a18e46f | 1237 | } \r |
0675f200 | 1238 | \r |
6a18e46f | 1239 | // write block\r |
1240 | if (workFlags & MAGIC_WUPC) {\r | |
2abdfa49 | 1241 | ReaderTransmitBitsPar(wupC1, 7, NULL, NULL);\r |
6a18e46f | 1242 | if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != 0x0a)) {\r |
1243 | if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("wupC1 error");\r | |
1244 | errormsg = MAGIC_WUPC;\r | |
1245 | break;\r | |
1246 | }\r | |
1247 | \r | |
1248 | ReaderTransmit(wupC2, sizeof(wupC2), NULL);\r | |
1249 | if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != 0x0a)) {\r | |
1250 | if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("wupC2 error");\r | |
1251 | errormsg = MAGIC_WUPC;\r | |
1252 | break;\r | |
1253 | }\r | |
9332b857 | 1254 | }\r |
c2731f37 | 1255 | \r |
6a18e46f | 1256 | if ((mifare_sendcmd_short(NULL, 0, ISO14443A_CMD_WRITEBLOCK, blockNo, receivedAnswer, receivedAnswerPar, NULL) != 1) || (receivedAnswer[0] != 0x0a)) {\r |
1257 | if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("write block send command error");\r | |
1258 | errormsg = 4;\r | |
1259 | break;\r | |
1260 | }\r | |
0675f200 | 1261 | \r |
6a18e46f | 1262 | memcpy(data, datain, 16);\r |
1263 | AppendCrc14443a(data, 16);\r | |
0675f200 | 1264 | \r |
6a18e46f | 1265 | ReaderTransmit(data, sizeof(data), NULL);\r |
1266 | if ((ReaderReceive(receivedAnswer, receivedAnswerPar) != 1) || (receivedAnswer[0] != 0x0a)) {\r | |
1267 | if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("write block send data error");\r | |
1268 | errormsg = 0;\r | |
1269 | break;\r | |
1270 | } \r | |
0675f200 | 1271 | \r |
6a18e46f | 1272 | if (workFlags & MAGIC_OFF) \r |
d2094433 | 1273 | mifare_classic_halt_ex(NULL);\r |
6a18e46f | 1274 | \r |
1275 | isOK = true;\r | |
1276 | break;\r | |
1277 | \r | |
1278 | } // end while \r | |
1279 | \r | |
1280 | if (isOK )\r | |
1281 | cmd_send(CMD_ACK,1,0,0,uid,sizeof(uid));\r | |
1282 | else\r | |
1283 | OnErrorMagic(errormsg);\r | |
0675f200 | 1284 | \r |
c2731f37 | 1285 | if (workFlags & MAGIC_OFF)\r |
1286 | OnSuccessMagic();\r | |
0675f200 | 1287 | }\r |
545a1f38 | 1288 | \r |
c2731f37 | 1289 | void MifareCGetBlock(uint32_t arg0, uint32_t arg1, uint8_t *datain){\r |
1290 | \r | |
545a1f38 | 1291 | uint8_t workFlags = arg0;\r |
c2731f37 | 1292 | uint8_t blockNo = arg1;\r |
6a18e46f | 1293 | uint8_t errormsg = 0x00;\r |
1294 | bool isOK = false; //assume we will get an error\r | |
1295 | \r | |
545a1f38 | 1296 | // variables\r |
c2731f37 | 1297 | uint8_t data[MAX_MIFARE_FRAME_SIZE];\r |
6a18e46f | 1298 | uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE] = {0x00};\r |
1299 | uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE] = {0x00};\r | |
545a1f38 | 1300 | \r |
c2731f37 | 1301 | memset(data, 0x00, sizeof(data));\r |
1302 | \r | |
1303 | if (workFlags & MAGIC_INIT) {\r | |
545a1f38 M |
1304 | LED_A_ON();\r |
1305 | LED_B_OFF();\r | |
c2731f37 | 1306 | iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN); \r |
3000dc4e MHS |
1307 | clear_trace();\r |
1308 | set_tracing(TRUE);\r | |
545a1f38 M |
1309 | }\r |
1310 | \r | |
6a18e46f | 1311 | //loop doesn't loop just breaks out if error or done\r |
1312 | while (true) {\r | |
1313 | if (workFlags & MAGIC_WUPC) {\r | |
2abdfa49 | 1314 | ReaderTransmitBitsPar(wupC1, 7, NULL, NULL);\r |
6a18e46f | 1315 | if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != 0x0a)) {\r |
1316 | if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("wupC1 error");\r | |
1317 | errormsg = MAGIC_WUPC;\r | |
1318 | break;\r | |
1319 | }\r | |
545a1f38 | 1320 | \r |
6a18e46f | 1321 | ReaderTransmit(wupC2, sizeof(wupC2), NULL);\r |
1322 | if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != 0x0a)) {\r | |
1323 | if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("wupC2 error");\r | |
1324 | errormsg = MAGIC_WUPC;\r | |
1325 | break;\r | |
1326 | }\r | |
9332b857 | 1327 | }\r |
c2731f37 | 1328 | \r |
6a18e46f | 1329 | // read block \r |
1330 | if ((mifare_sendcmd_short(NULL, 0, ISO14443A_CMD_READBLOCK, blockNo, receivedAnswer, receivedAnswerPar, NULL) != 18)) {\r | |
1331 | if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("read block send command error");\r | |
1332 | errormsg = 0;\r | |
1333 | break;\r | |
1334 | }\r | |
1335 | \r | |
1336 | memcpy(data, receivedAnswer, sizeof(data));\r | |
1337 | \r | |
1338 | // send HALT\r | |
c7962313 | 1339 | if (workFlags & MAGIC_HALT)\r |
6a18e46f | 1340 | mifare_classic_halt_ex(NULL);\r |
c7962313 | 1341 | \r |
6a18e46f | 1342 | isOK = true;\r |
1343 | break;\r | |
9332b857 | 1344 | }\r |
c2731f37 | 1345 | // if MAGIC_DATAIN, the data stays on device side.\r |
6a18e46f | 1346 | if (workFlags & MAGIC_DATAIN) {\r |
1347 | if (isOK)\r | |
1348 | memcpy(datain, data, sizeof(data));\r | |
1349 | } else {\r | |
1350 | if (isOK) \r | |
1351 | cmd_send(CMD_ACK,1,0,0,data,sizeof(data)); \r | |
1352 | else \r | |
1353 | OnErrorMagic(errormsg); \r | |
1354 | }\r | |
c2731f37 | 1355 | \r |
c2731f37 | 1356 | if (workFlags & MAGIC_OFF)\r |
1357 | OnSuccessMagic();\r | |
545a1f38 M |
1358 | }\r |
1359 | \r | |
3fe4ff4f | 1360 | void MifareCIdent(){\r |
3fe4ff4f | 1361 | \r |
1362 | // variables\r | |
9332b857 | 1363 | bool isOK = true; \r |
d798d31c | 1364 | uint8_t receivedAnswer[1] = {0x00};\r |
1365 | uint8_t receivedAnswerPar[1] = {0x00};\r | |
3fe4ff4f | 1366 | \r |
2abdfa49 | 1367 | ReaderTransmitBitsPar(wupC1, 7, NULL, NULL);\r |
3fe4ff4f | 1368 | if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != 0x0a)) {\r |
9332b857 | 1369 | isOK = false;\r |
1370 | }\r | |
3fe4ff4f | 1371 | \r |
1372 | ReaderTransmit(wupC2, sizeof(wupC2), NULL);\r | |
1373 | if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != 0x0a)) {\r | |
9332b857 | 1374 | isOK = false;\r |
1375 | }\r | |
3fe4ff4f | 1376 | \r |
acd7ccdb | 1377 | // removed the if, since some magic tags misbehavies and send an answer to it.\r |
1378 | mifare_classic_halt(NULL, 0);\r | |
3fe4ff4f | 1379 | cmd_send(CMD_ACK,isOK,0,0,0,0);\r |
1380 | }\r | |
1381 | \r | |
c2731f37 | 1382 | void OnSuccessMagic(){\r |
add0504d | 1383 | FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r |
1384 | LEDsoff();\r | |
c2731f37 | 1385 | set_tracing(FALSE); \r |
1386 | }\r | |
1387 | void OnErrorMagic(uint8_t reason){\r | |
1388 | // ACK, ISOK, reason,0,0,0\r | |
1389 | cmd_send(CMD_ACK,0,reason,0,0,0);\r | |
1390 | OnSuccessMagic();\r | |
1391 | }\r | |
add0504d | 1392 | //\r |
3fe4ff4f | 1393 | // DESFIRE\r |
1394 | //\r | |
a631936e | 1395 | void Mifare_DES_Auth1(uint8_t arg0, uint8_t *datain){\r |
4a71da5a | 1396 | byte_t dataout[12] = {0x00};\r |
a631936e | 1397 | uint8_t uid[10] = {0x00};\r |
d798d31c | 1398 | uint32_t cuid = 0;\r |
a631936e | 1399 | \r |
a631936e | 1400 | iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r |
99cf19d9 | 1401 | clear_trace();\r |
810f5379 | 1402 | set_tracing(true);\r |
a631936e | 1403 | \r |
c188b1b9 | 1404 | int len = iso14443a_select_card(uid, NULL, &cuid, true, 0);\r |
a631936e | 1405 | if(!len) {\r |
aa60d156 | 1406 | if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Can't select card");\r |
0ec548dc | 1407 | OnError(1);\r |
a631936e | 1408 | return;\r |
1409 | };\r | |
1410 | \r | |
1411 | if(mifare_desfire_des_auth1(cuid, dataout)){\r | |
aa60d156 | 1412 | if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Authentication part1: Fail.");\r |
0ec548dc | 1413 | OnError(4);\r |
a631936e | 1414 | return;\r |
1415 | }\r | |
1416 | \r | |
1417 | if (MF_DBGLEVEL >= MF_DBG_EXTENDED) DbpString("AUTH 1 FINISHED");\r | |
88e20c9f | 1418 | cmd_send(CMD_ACK, 1, cuid, 0, dataout, sizeof(dataout));\r |
a631936e | 1419 | }\r |
1420 | \r | |
1421 | void Mifare_DES_Auth2(uint32_t arg0, uint8_t *datain){\r | |
a631936e | 1422 | uint32_t cuid = arg0;\r |
1423 | uint8_t key[16] = {0x00};\r | |
a631936e | 1424 | byte_t dataout[12] = {0x00};\r |
aa60d156 | 1425 | byte_t isOK = 0;\r |
a631936e | 1426 | \r |
1427 | memcpy(key, datain, 16);\r | |
1428 | \r | |
1429 | isOK = mifare_desfire_des_auth2(cuid, key, dataout);\r | |
1430 | \r | |
1431 | if( isOK) {\r | |
aa60d156 | 1432 | if (MF_DBGLEVEL >= MF_DBG_EXTENDED) Dbprintf("Authentication part2: Failed"); \r |
0ec548dc | 1433 | OnError(4);\r |
a631936e | 1434 | return;\r |
1435 | }\r | |
1436 | \r | |
aa60d156 | 1437 | if (MF_DBGLEVEL >= MF_DBG_EXTENDED) DbpString("AUTH 2 FINISHED");\r |
a631936e | 1438 | \r |
1439 | cmd_send(CMD_ACK, isOK, 0, 0, dataout, sizeof(dataout));\r | |
1440 | FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r | |
1441 | LEDsoff();\r | |
88e20c9f | 1442 | set_tracing(FALSE);\r |
ce432659 | 1443 | } |