]>
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 | |
de77d4ac | 17 | \r |
0b4efbde | 18 | #include <stdint.h>\r |
19 | \r | |
20 | #include "proxmark3.h"\r | |
867e10a5 | 21 | #include "usb_cdc.h"\r |
0b4efbde | 22 | #include "crapto1/crapto1.h"\r |
23 | #include "iso14443a.h"\r | |
24 | #include "BigBuf.h"\r | |
25 | #include "mifareutil.h"\r | |
26 | #include "apps.h"\r | |
27 | #include "protocols.h"\r | |
787b5bd8 | 28 | #include "util.h"\r |
1f065e1d | 29 | #include "parity.h"\r |
a631936e | 30 | #include "crc.h"\r |
fc52fbd4 | 31 | #include "fpgaloader.h"\r |
a631936e | 32 | \r |
0b4efbde | 33 | #define HARDNESTED_AUTHENTICATION_TIMEOUT 848 // card times out 1ms after wrong authentication (according to NXP documentation)\r |
34 | #define HARDNESTED_PRE_AUTHENTICATION_LEADTIME 400 // some (non standard) cards need a pause after select before they are ready for first authentication\r | |
d1f9ec06 | 35 | \r |
b8dd1ef6 | 36 | /*\r |
f168b263 | 37 | // the block number for the ISO14443-4 PCB\r |
de77d4ac | 38 | static uint8_t pcb_blocknum = 0;\r |
f168b263 | 39 | // Deselect card by sending a s-block. the crc is precalced for speed\r |
40 | static uint8_t deselect_cmd[] = {0xc2,0xe0,0xb4};\r | |
41 | \r | |
b8dd1ef6 | 42 | static void OnSuccess(){\r |
43 | pcb_blocknum = 0;\r | |
44 | ReaderTransmit(deselect_cmd, 3 , NULL);\r | |
45 | FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r | |
46 | LEDsoff();\r | |
47 | }\r | |
48 | */\r | |
49 | \r | |
50 | static void OnError(uint8_t reason){\r | |
51 | // pcb_blocknum = 0;\r | |
52 | // ReaderTransmit(deselect_cmd, 3 , NULL);\r | |
53 | FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r | |
54 | LED_D_OFF();\r | |
55 | cmd_send(CMD_ACK,0,reason,0,0,0);\r | |
56 | LED_A_OFF();\r | |
57 | }\r | |
58 | \r | |
8556b852 | 59 | //-----------------------------------------------------------------------------\r |
7906cb41 | 60 | // Select, Authenticate, Read a MIFARE tag.\r |
8556b852 M |
61 | // read block\r |
62 | //-----------------------------------------------------------------------------\r | |
63 | void MifareReadBlock(uint8_t arg0, uint8_t arg1, uint8_t arg2, uint8_t *datain)\r | |
64 | {\r | |
b8dd1ef6 | 65 | LED_A_ON();\r |
66 | \r | |
8556b852 M |
67 | uint8_t blockNo = arg0;\r |
68 | uint8_t keyType = arg1;\r | |
69 | uint64_t ui64Key = 0;\r | |
70 | ui64Key = bytes_to_num(datain, 6);\r | |
7906cb41 | 71 | \r |
8556b852 M |
72 | byte_t isOK = 0;\r |
73 | byte_t dataoutbuf[16];\r | |
1c611bbd | 74 | uint8_t uid[10];\r |
8556b852 M |
75 | uint32_t cuid;\r |
76 | struct Crypto1State mpcs = {0, 0};\r | |
77 | struct Crypto1State *pcs;\r | |
78 | pcs = &mpcs;\r | |
79 | \r | |
7bc95e2e | 80 | iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r |
8556b852 | 81 | \r |
09ffd16e | 82 | clear_trace();\r |
83 | \r | |
8556b852 | 84 | while (true) {\r |
c04a4b60 | 85 | if(!iso14443a_select_card(uid, NULL, &cuid, true, 0, true)) {\r |
0b4efbde | 86 | if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");\r |
8556b852 M |
87 | break;\r |
88 | };\r | |
89 | \r | |
a749b1e5 | 90 | if(mifare_classic_auth(pcs, cuid, blockNo, keyType, ui64Key, AUTH_FIRST, NULL)) {\r |
0b4efbde | 91 | if (MF_DBGLEVEL >= 1) Dbprintf("Auth error");\r |
8556b852 M |
92 | break;\r |
93 | };\r | |
7906cb41 | 94 | \r |
8556b852 | 95 | if(mifare_classic_readblock(pcs, cuid, blockNo, dataoutbuf)) {\r |
0b4efbde | 96 | if (MF_DBGLEVEL >= 1) Dbprintf("Read block error");\r |
8556b852 M |
97 | break;\r |
98 | };\r | |
99 | \r | |
100 | if(mifare_classic_halt(pcs, cuid)) {\r | |
0b4efbde | 101 | if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");\r |
8556b852 M |
102 | break;\r |
103 | };\r | |
7906cb41 | 104 | \r |
8556b852 M |
105 | isOK = 1;\r |
106 | break;\r | |
107 | }\r | |
7906cb41 | 108 | \r |
8556b852 M |
109 | // ----------------------------- crypto1 destroy\r |
110 | crypto1_destroy(pcs);\r | |
7906cb41 | 111 | \r |
0b4efbde | 112 | if (MF_DBGLEVEL >= 2) DbpString("READ BLOCK FINISHED");\r |
8556b852 | 113 | \r |
929b61c6 | 114 | FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r |
115 | \r | |
8556b852 | 116 | LED_B_ON();\r |
baeaf579 | 117 | cmd_send(CMD_ACK,isOK,0,0,dataoutbuf,16);\r |
8556b852 M |
118 | LED_B_OFF();\r |
119 | \r | |
a631936e | 120 | LEDsoff();\r |
121 | }\r | |
122 | \r | |
8258f409 | 123 | void MifareUC_Auth(uint8_t arg0, uint8_t *keybytes){\r |
a631936e | 124 | \r |
b8dd1ef6 | 125 | LED_A_ON();\r |
8258f409 | 126 | bool turnOffField = (arg0 == 1);\r |
a631936e | 127 | \r |
a631936e | 128 | iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r |
129 | \r | |
b8dd1ef6 | 130 | if (!iso14443a_select_card(NULL, NULL, NULL, true, 0, true)) {\r |
f168b263 | 131 | if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Can't select card");\r |
132 | OnError(0);\r | |
a631936e | 133 | return;\r |
134 | };\r | |
7906cb41 | 135 | \r |
b8dd1ef6 | 136 | if (!mifare_ultra_auth(keybytes)){\r |
8258f409 | 137 | if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Authentication failed");\r |
f168b263 | 138 | OnError(1);\r |
a631936e | 139 | return;\r |
140 | }\r | |
141 | \r | |
8258f409 | 142 | if (turnOffField) {\r |
cceabb79 | 143 | FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r |
b8dd1ef6 | 144 | LED_D_OFF();\r |
cceabb79 | 145 | }\r |
b8dd1ef6 | 146 | \r |
9d87eb66 | 147 | cmd_send(CMD_ACK,1,0,0,0,0);\r |
b8dd1ef6 | 148 | LED_A_OFF();\r |
7cc204bf | 149 | }\r |
150 | \r | |
75377d29 | 151 | // Arg0 = BlockNo,\r |
152 | // Arg1 = UsePwd bool\r | |
153 | // datain = PWD bytes,\r | |
f168b263 | 154 | void MifareUReadBlock(uint8_t arg0, uint8_t arg1, uint8_t *datain)\r |
7cc204bf | 155 | {\r |
b8dd1ef6 | 156 | LED_A_ON();\r |
157 | \r | |
7cc204bf | 158 | uint8_t blockNo = arg0;\r |
787b5bd8 | 159 | byte_t dataout[16] = {0x00};\r |
8258f409 | 160 | bool useKey = (arg1 == 1); //UL_C\r |
161 | bool usePwd = (arg1 == 2); //UL_EV1/NTAG\r | |
f168b263 | 162 | \r |
787b5bd8 | 163 | iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r |
f168b263 | 164 | \r |
c04a4b60 | 165 | int len = iso14443a_select_card(NULL, NULL, NULL, true, 0, true);\r |
787b5bd8 | 166 | if(!len) {\r |
f168b263 | 167 | if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Can't select card (RC:%02X)",len);\r |
168 | OnError(1);\r | |
787b5bd8 | 169 | return;\r |
f168b263 | 170 | }\r |
171 | \r | |
8258f409 | 172 | // UL-C authentication\r |
b8dd1ef6 | 173 | if (useKey) {\r |
8258f409 | 174 | uint8_t key[16] = {0x00};\r |
175 | memcpy(key, datain, sizeof(key) );\r | |
f168b263 | 176 | \r |
9d87eb66 | 177 | if ( !mifare_ultra_auth(key) ) {\r |
f168b263 | 178 | OnError(1);\r |
179 | return;\r | |
180 | }\r | |
8258f409 | 181 | }\r |
f168b263 | 182 | \r |
8258f409 | 183 | // UL-EV1 / NTAG authentication\r |
b8dd1ef6 | 184 | if (usePwd) {\r |
8258f409 | 185 | uint8_t pwd[4] = {0x00};\r |
186 | memcpy(pwd, datain, 4);\r | |
187 | uint8_t pack[4] = {0,0,0,0};\r | |
9d87eb66 | 188 | if (!mifare_ul_ev1_auth(pwd, pack)) {\r |
f168b263 | 189 | OnError(1);\r |
190 | return;\r | |
191 | }\r | |
7906cb41 | 192 | }\r |
f168b263 | 193 | \r |
b8dd1ef6 | 194 | if (mifare_ultra_readblock(blockNo, dataout)) {\r |
f168b263 | 195 | if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Read block error");\r |
196 | OnError(2);\r | |
787b5bd8 | 197 | return;\r |
f168b263 | 198 | }\r |
8258f409 | 199 | \r |
b8dd1ef6 | 200 | if (mifare_ultra_halt()) {\r |
f168b263 | 201 | if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Halt error");\r |
202 | OnError(3);\r | |
787b5bd8 | 203 | return;\r |
f168b263 | 204 | }\r |
8258f409 | 205 | \r |
7cc204bf | 206 | FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r |
b8dd1ef6 | 207 | LED_D_OFF();\r |
929b61c6 | 208 | \r |
209 | cmd_send(CMD_ACK,1,0,0,dataout,16);\r | |
b8dd1ef6 | 210 | LED_A_OFF();\r |
7cc204bf | 211 | }\r |
b3125340 | 212 | \r |
7cc204bf | 213 | //-----------------------------------------------------------------------------\r |
7906cb41 | 214 | // Select, Authenticate, Read a MIFARE tag.\r |
baeaf579 | 215 | // read sector (data = 4 x 16 bytes = 64 bytes, or 16 x 16 bytes = 256 bytes)\r |
8556b852 M |
216 | //-----------------------------------------------------------------------------\r |
217 | void MifareReadSector(uint8_t arg0, uint8_t arg1, uint8_t arg2, uint8_t *datain)\r | |
218 | {\r | |
219 | // params\r | |
220 | uint8_t sectorNo = arg0;\r | |
221 | uint8_t keyType = arg1;\r | |
222 | uint64_t ui64Key = 0;\r | |
223 | ui64Key = bytes_to_num(datain, 6);\r | |
7906cb41 | 224 | \r |
8556b852 | 225 | // variables\r |
3fe4ff4f | 226 | byte_t isOK = 0;\r |
baeaf579 | 227 | byte_t dataoutbuf[16 * 16];\r |
1c611bbd | 228 | uint8_t uid[10];\r |
8556b852 M |
229 | uint32_t cuid;\r |
230 | struct Crypto1State mpcs = {0, 0};\r | |
231 | struct Crypto1State *pcs;\r | |
232 | pcs = &mpcs;\r | |
233 | \r | |
7bc95e2e | 234 | iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r |
8556b852 | 235 | \r |
09ffd16e | 236 | clear_trace();\r |
237 | \r | |
8556b852 M |
238 | LED_A_ON();\r |
239 | LED_B_OFF();\r | |
240 | LED_C_OFF();\r | |
241 | \r | |
baeaf579 | 242 | isOK = 1;\r |
c04a4b60 | 243 | if(!iso14443a_select_card(uid, NULL, &cuid, true, 0, true)) {\r |
baeaf579 | 244 | isOK = 0;\r |
0b4efbde | 245 | if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");\r |
baeaf579 | 246 | }\r |
8556b852 | 247 | \r |
7906cb41 | 248 | \r |
a749b1e5 | 249 | if(isOK && mifare_classic_auth(pcs, cuid, FirstBlockOfSector(sectorNo), keyType, ui64Key, AUTH_FIRST, NULL)) {\r |
baeaf579 | 250 | isOK = 0;\r |
0b4efbde | 251 | if (MF_DBGLEVEL >= 1) Dbprintf("Auth error");\r |
baeaf579 | 252 | }\r |
7906cb41 | 253 | \r |
baeaf579 | 254 | for (uint8_t blockNo = 0; isOK && blockNo < NumBlocksPerSector(sectorNo); blockNo++) {\r |
255 | if(mifare_classic_readblock(pcs, cuid, FirstBlockOfSector(sectorNo) + blockNo, dataoutbuf + 16 * blockNo)) {\r | |
256 | isOK = 0;\r | |
0b4efbde | 257 | if (MF_DBGLEVEL >= 1) Dbprintf("Read sector %2d block %2d error", sectorNo, blockNo);\r |
8556b852 | 258 | break;\r |
baeaf579 | 259 | }\r |
260 | }\r | |
7906cb41 | 261 | \r |
baeaf579 | 262 | if(mifare_classic_halt(pcs, cuid)) {\r |
0b4efbde | 263 | if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");\r |
8556b852 | 264 | }\r |
baeaf579 | 265 | \r |
8556b852 M |
266 | // ----------------------------- crypto1 destroy\r |
267 | crypto1_destroy(pcs);\r | |
7906cb41 | 268 | \r |
8556b852 M |
269 | if (MF_DBGLEVEL >= 2) DbpString("READ SECTOR FINISHED");\r |
270 | \r | |
929b61c6 | 271 | // Thats it...\r |
272 | FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r | |
273 | \r | |
8556b852 | 274 | LED_B_ON();\r |
baeaf579 | 275 | cmd_send(CMD_ACK,isOK,0,0,dataoutbuf,16*NumBlocksPerSector(sectorNo));\r |
6e82300d | 276 | LED_B_OFF();\r |
8556b852 | 277 | \r |
8556b852 | 278 | LEDsoff();\r |
7cc204bf | 279 | }\r |
280 | \r | |
79d7bcbb | 281 | // arg0 = blockNo (start)\r |
282 | // arg1 = Pages (number of blocks)\r | |
283 | // arg2 = useKey\r | |
284 | // datain = KEY bytes\r | |
75377d29 | 285 | void MifareUReadCard(uint8_t arg0, uint16_t arg1, uint8_t arg2, uint8_t *datain)\r |
7cc204bf | 286 | {\r |
09ffd16e | 287 | LED_A_ON();\r |
288 | iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r | |
289 | \r | |
d7acc640 | 290 | // free eventually allocated BigBuf memory\r |
291 | BigBuf_free();\r | |
d7acc640 | 292 | \r |
f168b263 | 293 | // params\r |
75377d29 | 294 | uint8_t blockNo = arg0;\r |
295 | uint16_t blocks = arg1;\r | |
cceabb79 | 296 | bool useKey = (arg2 == 1); //UL_C\r |
297 | bool usePwd = (arg2 == 2); //UL_EV1/NTAG\r | |
9d87eb66 | 298 | uint32_t countblocks = 0;\r |
d7acc640 | 299 | uint8_t *dataout = BigBuf_malloc(CARD_MEMORY_SIZE);\r |
300 | if (dataout == NULL){\r | |
301 | Dbprintf("out of memory");\r | |
302 | OnError(1);\r | |
303 | return;\r | |
304 | }\r | |
7cc204bf | 305 | \r |
c04a4b60 | 306 | int len = iso14443a_select_card(NULL, NULL, NULL, true, 0, true);\r |
787b5bd8 | 307 | if (!len) {\r |
f168b263 | 308 | if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Can't select card (RC:%d)",len);\r |
309 | OnError(1);\r | |
787b5bd8 | 310 | return;\r |
311 | }\r | |
75377d29 | 312 | \r |
8258f409 | 313 | // UL-C authentication\r |
b8dd1ef6 | 314 | if (useKey) {\r |
75377d29 | 315 | uint8_t key[16] = {0x00};\r |
8258f409 | 316 | memcpy(key, datain, sizeof(key) );\r |
75377d29 | 317 | \r |
9d87eb66 | 318 | if ( !mifare_ultra_auth(key) ) {\r |
75377d29 | 319 | OnError(1);\r |
320 | return;\r | |
321 | }\r | |
75377d29 | 322 | }\r |
323 | \r | |
8258f409 | 324 | // UL-EV1 / NTAG authentication\r |
325 | if (usePwd) {\r | |
326 | uint8_t pwd[4] = {0x00};\r | |
327 | memcpy(pwd, datain, sizeof(pwd));\r | |
cceabb79 | 328 | uint8_t pack[4] = {0,0,0,0};\r |
329 | \r | |
9d87eb66 | 330 | if (!mifare_ul_ev1_auth(pwd, pack)){\r |
cceabb79 | 331 | OnError(1);\r |
7906cb41 | 332 | return;\r |
cceabb79 | 333 | }\r |
334 | }\r | |
335 | \r | |
75377d29 | 336 | for (int i = 0; i < blocks; i++){\r |
22342f6d | 337 | if ((i*4) + 4 >= CARD_MEMORY_SIZE) {\r |
9d87eb66 | 338 | Dbprintf("Data exceeds buffer!!");\r |
339 | break;\r | |
340 | }\r | |
7906cb41 | 341 | \r |
9d87eb66 | 342 | len = mifare_ultra_readblock(blockNo + i, dataout + 4 * i);\r |
75377d29 | 343 | \r |
787b5bd8 | 344 | if (len) {\r |
f168b263 | 345 | if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Read block %d error",i);\r |
9d87eb66 | 346 | // if no blocks read - error out\r |
347 | if (i==0){\r | |
348 | OnError(2);\r | |
349 | return;\r | |
350 | } else {\r | |
351 | //stop at last successful read block and return what we got\r | |
352 | break;\r | |
353 | }\r | |
787b5bd8 | 354 | } else {\r |
75377d29 | 355 | countblocks++;\r |
787b5bd8 | 356 | }\r |
357 | }\r | |
75377d29 | 358 | \r |
f168b263 | 359 | len = mifare_ultra_halt();\r |
787b5bd8 | 360 | if (len) {\r |
f168b263 | 361 | if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Halt error");\r |
362 | OnError(3);\r | |
787b5bd8 | 363 | return;\r |
364 | }\r | |
7cc204bf | 365 | \r |
b8dd1ef6 | 366 | if (MF_DBGLEVEL >= MF_DBG_DEBUG) Dbprintf("Blocks read %d", countblocks);\r |
75377d29 | 367 | \r |
31d1caa5 | 368 | FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r |
b8dd1ef6 | 369 | LED_D_OFF();\r |
929b61c6 | 370 | \r |
371 | cmd_send(CMD_ACK, 1, countblocks*4, BigBuf_max_traceLen(), 0, 0);\r | |
372 | \r | |
22342f6d | 373 | BigBuf_free();\r |
b8dd1ef6 | 374 | LED_A_OFF();\r |
7cc204bf | 375 | }\r |
376 | \r | |
7cc204bf | 377 | //-----------------------------------------------------------------------------\r |
7906cb41 | 378 | // Select, Authenticate, Write a MIFARE tag.\r |
7cc204bf | 379 | // read block\r |
8556b852 M |
380 | //-----------------------------------------------------------------------------\r |
381 | void MifareWriteBlock(uint8_t arg0, uint8_t arg1, uint8_t arg2, uint8_t *datain)\r | |
382 | {\r | |
383 | // params\r | |
384 | uint8_t blockNo = arg0;\r | |
385 | uint8_t keyType = arg1;\r | |
386 | uint64_t ui64Key = 0;\r | |
387 | byte_t blockdata[16];\r | |
388 | \r | |
389 | ui64Key = bytes_to_num(datain, 6);\r | |
390 | memcpy(blockdata, datain + 10, 16);\r | |
7906cb41 | 391 | \r |
8556b852 M |
392 | // variables\r |
393 | byte_t isOK = 0;\r | |
1c611bbd | 394 | uint8_t uid[10];\r |
8556b852 M |
395 | uint32_t cuid;\r |
396 | struct Crypto1State mpcs = {0, 0};\r | |
397 | struct Crypto1State *pcs;\r | |
398 | pcs = &mpcs;\r | |
399 | \r | |
7bc95e2e | 400 | iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r |
8556b852 | 401 | \r |
09ffd16e | 402 | clear_trace();\r |
403 | \r | |
8556b852 M |
404 | LED_A_ON();\r |
405 | LED_B_OFF();\r | |
406 | LED_C_OFF();\r | |
407 | \r | |
408 | while (true) {\r | |
c04a4b60 | 409 | if(!iso14443a_select_card(uid, NULL, &cuid, true, 0, true)) {\r |
0b4efbde | 410 | if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");\r |
8556b852 M |
411 | break;\r |
412 | };\r | |
413 | \r | |
a749b1e5 | 414 | if(mifare_classic_auth(pcs, cuid, blockNo, keyType, ui64Key, AUTH_FIRST, NULL)) {\r |
0b4efbde | 415 | if (MF_DBGLEVEL >= 1) Dbprintf("Auth error");\r |
8556b852 M |
416 | break;\r |
417 | };\r | |
7906cb41 | 418 | \r |
8556b852 | 419 | if(mifare_classic_writeblock(pcs, cuid, blockNo, blockdata)) {\r |
0b4efbde | 420 | if (MF_DBGLEVEL >= 1) Dbprintf("Write block error");\r |
8556b852 M |
421 | break;\r |
422 | };\r | |
423 | \r | |
424 | if(mifare_classic_halt(pcs, cuid)) {\r | |
0b4efbde | 425 | if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");\r |
8556b852 M |
426 | break;\r |
427 | };\r | |
7906cb41 | 428 | \r |
8556b852 M |
429 | isOK = 1;\r |
430 | break;\r | |
431 | }\r | |
7906cb41 | 432 | \r |
8556b852 M |
433 | // ----------------------------- crypto1 destroy\r |
434 | crypto1_destroy(pcs);\r | |
7906cb41 | 435 | \r |
0b4efbde | 436 | if (MF_DBGLEVEL >= 2) DbpString("WRITE BLOCK FINISHED");\r |
8556b852 | 437 | \r |
929b61c6 | 438 | // Thats it...\r |
439 | FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r | |
440 | \r | |
8556b852 | 441 | LED_B_ON();\r |
9492e0b0 | 442 | cmd_send(CMD_ACK,isOK,0,0,0,0);\r |
6e82300d | 443 | LED_B_OFF();\r |
8556b852 M |
444 | \r |
445 | \r | |
8556b852 | 446 | LEDsoff();\r |
7cc204bf | 447 | }\r |
448 | \r | |
7906cb41 | 449 | /* // Command not needed but left for future testing\r |
4973f23d | 450 | void MifareUWriteBlockCompat(uint8_t arg0, uint8_t *datain)\r |
7cc204bf | 451 | {\r |
f168b263 | 452 | uint8_t blockNo = arg0;\r |
787b5bd8 | 453 | byte_t blockdata[16] = {0x00};\r |
7cc204bf | 454 | \r |
f168b263 | 455 | memcpy(blockdata, datain, 16);\r |
456 | \r | |
787b5bd8 | 457 | uint8_t uid[10] = {0x00};\r |
7cc204bf | 458 | \r |
f168b263 | 459 | LED_A_ON(); LED_B_OFF(); LED_C_OFF();\r |
7cc204bf | 460 | \r |
f168b263 | 461 | clear_trace();\r |
462 | iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r | |
463 | \r | |
de77d4ac | 464 | if(!iso14443a_select_card(uid, NULL, NULL, true, 0)) {\r |
f168b263 | 465 | if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");\r |
466 | OnError(0);\r | |
467 | return;\r | |
468 | };\r | |
469 | \r | |
4973f23d | 470 | if(mifare_ultra_writeblock_compat(blockNo, blockdata)) {\r |
f168b263 | 471 | if (MF_DBGLEVEL >= 1) Dbprintf("Write block error");\r |
472 | OnError(0);\r | |
0b4efbde | 473 | return; };\r |
f168b263 | 474 | \r |
475 | if(mifare_ultra_halt()) {\r | |
476 | if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");\r | |
477 | OnError(0);\r | |
478 | return;\r | |
479 | };\r | |
480 | \r | |
481 | if (MF_DBGLEVEL >= 2) DbpString("WRITE BLOCK FINISHED");\r | |
482 | \r | |
f168b263 | 483 | FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r |
929b61c6 | 484 | \r |
485 | cmd_send(CMD_ACK,1,0,0,0,0);\r | |
f168b263 | 486 | LEDsoff();\r |
7cc204bf | 487 | }\r |
4973f23d | 488 | */\r |
7cc204bf | 489 | \r |
79d7bcbb | 490 | // Arg0 : Block to write to.\r |
491 | // Arg1 : 0 = use no authentication.\r | |
492 | // 1 = use 0x1A authentication.\r | |
493 | // 2 = use 0x1B authentication.\r | |
494 | // datain : 4 first bytes is data to be written.\r | |
495 | // : 4/16 next bytes is authentication key.\r | |
4973f23d | 496 | void MifareUWriteBlock(uint8_t arg0, uint8_t arg1, uint8_t *datain)\r |
7cc204bf | 497 | {\r |
baeaf579 | 498 | uint8_t blockNo = arg0;\r |
79d7bcbb | 499 | bool useKey = (arg1 == 1); //UL_C\r |
500 | bool usePwd = (arg1 == 2); //UL_EV1/NTAG\r | |
787b5bd8 | 501 | byte_t blockdata[4] = {0x00};\r |
7cc204bf | 502 | \r |
8258f409 | 503 | memcpy(blockdata, datain,4);\r |
7906cb41 | 504 | \r |
8258f409 | 505 | LEDsoff();\r |
506 | LED_A_ON();\r | |
f168b263 | 507 | iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r |
508 | \r | |
09ffd16e | 509 | clear_trace();\r |
510 | \r | |
c04a4b60 | 511 | if(!iso14443a_select_card(NULL, NULL, NULL, true, 0, true)) {\r |
f168b263 | 512 | if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");\r |
513 | OnError(0);\r | |
514 | return;\r | |
515 | };\r | |
7cc204bf | 516 | \r |
79d7bcbb | 517 | // UL-C authentication\r |
518 | if ( useKey ) {\r | |
519 | uint8_t key[16] = {0x00};\r | |
520 | memcpy(key, datain+4, sizeof(key) );\r | |
521 | \r | |
522 | if ( !mifare_ultra_auth(key) ) {\r | |
523 | OnError(1);\r | |
524 | return;\r | |
525 | }\r | |
526 | }\r | |
7906cb41 | 527 | \r |
79d7bcbb | 528 | // UL-EV1 / NTAG authentication\r |
529 | if (usePwd) {\r | |
530 | uint8_t pwd[4] = {0x00};\r | |
531 | memcpy(pwd, datain+4, 4);\r | |
532 | uint8_t pack[4] = {0,0,0,0};\r | |
533 | if (!mifare_ul_ev1_auth(pwd, pack)) {\r | |
534 | OnError(1);\r | |
535 | return;\r | |
536 | }\r | |
537 | }\r | |
538 | \r | |
4973f23d | 539 | if(mifare_ultra_writeblock(blockNo, blockdata)) {\r |
f168b263 | 540 | if (MF_DBGLEVEL >= 1) Dbprintf("Write block error");\r |
541 | OnError(0);\r | |
542 | return;\r | |
543 | };\r | |
544 | \r | |
545 | if(mifare_ultra_halt()) {\r | |
546 | if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");\r | |
547 | OnError(0);\r | |
548 | return;\r | |
549 | };\r | |
550 | \r | |
551 | if (MF_DBGLEVEL >= 2) DbpString("WRITE BLOCK FINISHED");\r | |
552 | \r | |
f168b263 | 553 | FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r |
929b61c6 | 554 | \r |
555 | cmd_send(CMD_ACK,1,0,0,0,0);\r | |
f168b263 | 556 | LEDsoff();\r |
557 | }\r | |
558 | \r | |
559 | void MifareUSetPwd(uint8_t arg0, uint8_t *datain){\r | |
7906cb41 | 560 | \r |
f168b263 | 561 | uint8_t pwd[16] = {0x00};\r |
562 | byte_t blockdata[4] = {0x00};\r | |
7906cb41 | 563 | \r |
f168b263 | 564 | memcpy(pwd, datain, 16);\r |
7906cb41 | 565 | \r |
f168b263 | 566 | LED_A_ON(); LED_B_OFF(); LED_C_OFF();\r |
baeaf579 | 567 | iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r |
7cc204bf | 568 | \r |
09ffd16e | 569 | clear_trace();\r |
570 | \r | |
c04a4b60 | 571 | if(!iso14443a_select_card(NULL, NULL, NULL, true, 0, true)) {\r |
f168b263 | 572 | if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");\r |
573 | OnError(0);\r | |
574 | return;\r | |
575 | };\r | |
7cc204bf | 576 | \r |
f168b263 | 577 | blockdata[0] = pwd[7];\r |
578 | blockdata[1] = pwd[6];\r | |
579 | blockdata[2] = pwd[5];\r | |
580 | blockdata[3] = pwd[4];\r | |
4973f23d | 581 | if(mifare_ultra_writeblock( 44, blockdata)) {\r |
f168b263 | 582 | if (MF_DBGLEVEL >= 1) Dbprintf("Write block error");\r |
583 | OnError(44);\r | |
584 | return;\r | |
585 | };\r | |
7cc204bf | 586 | \r |
f168b263 | 587 | blockdata[0] = pwd[3];\r |
588 | blockdata[1] = pwd[2];\r | |
589 | blockdata[2] = pwd[1];\r | |
590 | blockdata[3] = pwd[0];\r | |
4973f23d | 591 | if(mifare_ultra_writeblock( 45, blockdata)) {\r |
f168b263 | 592 | if (MF_DBGLEVEL >= 1) Dbprintf("Write block error");\r |
593 | OnError(45);\r | |
594 | return;\r | |
595 | };\r | |
7cc204bf | 596 | \r |
f168b263 | 597 | blockdata[0] = pwd[15];\r |
598 | blockdata[1] = pwd[14];\r | |
599 | blockdata[2] = pwd[13];\r | |
600 | blockdata[3] = pwd[12];\r | |
4973f23d | 601 | if(mifare_ultra_writeblock( 46, blockdata)) {\r |
f168b263 | 602 | if (MF_DBGLEVEL >= 1) Dbprintf("Write block error");\r |
603 | OnError(46);\r | |
604 | return;\r | |
605 | };\r | |
7cc204bf | 606 | \r |
f168b263 | 607 | blockdata[0] = pwd[11];\r |
608 | blockdata[1] = pwd[10];\r | |
609 | blockdata[2] = pwd[9];\r | |
610 | blockdata[3] = pwd[8];\r | |
4973f23d | 611 | if(mifare_ultra_writeblock( 47, blockdata)) {\r |
f168b263 | 612 | if (MF_DBGLEVEL >= 1) Dbprintf("Write block error");\r |
613 | OnError(47);\r | |
614 | return;\r | |
7906cb41 | 615 | };\r |
7cc204bf | 616 | \r |
f168b263 | 617 | if(mifare_ultra_halt()) {\r |
618 | if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");\r | |
619 | OnError(0);\r | |
620 | return;\r | |
621 | };\r | |
7cc204bf | 622 | \r |
baeaf579 | 623 | FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r |
929b61c6 | 624 | \r |
625 | cmd_send(CMD_ACK,1,0,0,0,0);\r | |
baeaf579 | 626 | LEDsoff();\r |
7cc204bf | 627 | }\r |
628 | \r | |
629 | // Return 1 if the nonce is invalid else return 0\r | |
6a1f2d82 | 630 | int valid_nonce(uint32_t Nt, uint32_t NtEnc, uint32_t Ks1, uint8_t *parity) {\r |
1f065e1d | 631 | return ((oddparity8((Nt >> 24) & 0xFF) == ((parity[0]) ^ oddparity8((NtEnc >> 24) & 0xFF) ^ BIT(Ks1,16))) & \\r |
632 | (oddparity8((Nt >> 16) & 0xFF) == ((parity[1]) ^ oddparity8((NtEnc >> 16) & 0xFF) ^ BIT(Ks1,8))) & \\r | |
633 | (oddparity8((Nt >> 8) & 0xFF) == ((parity[2]) ^ oddparity8((NtEnc >> 8) & 0xFF) ^ BIT(Ks1,0)))) ? 1 : 0;\r | |
8556b852 M |
634 | }\r |
635 | \r | |
9492e0b0 | 636 | \r |
de77d4ac | 637 | //-----------------------------------------------------------------------------\r |
638 | // acquire encrypted nonces in order to perform the attack described in\r | |
639 | // Carlo Meijer, Roel Verdult, "Ciphertext-only Cryptanalysis on Hardened\r | |
7906cb41 | 640 | // Mifare Classic Cards" in Proceedings of the 22nd ACM SIGSAC Conference on\r |
de77d4ac | 641 | // Computer and Communications Security, 2015\r |
642 | //-----------------------------------------------------------------------------\r | |
643 | void MifareAcquireEncryptedNonces(uint32_t arg0, uint32_t arg1, uint32_t flags, uint8_t *datain)\r | |
644 | {\r | |
645 | uint64_t ui64Key = 0;\r | |
646 | uint8_t uid[10];\r | |
647 | uint32_t cuid;\r | |
648 | uint8_t cascade_levels = 0;\r | |
649 | struct Crypto1State mpcs = {0, 0};\r | |
650 | struct Crypto1State *pcs;\r | |
651 | pcs = &mpcs;\r | |
652 | uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];\r | |
653 | int16_t isOK = 0;\r | |
654 | uint8_t par_enc[1];\r | |
655 | uint8_t nt_par_enc = 0;\r | |
656 | uint8_t buf[USB_CMD_DATA_SIZE];\r | |
657 | uint32_t timeout;\r | |
7906cb41 | 658 | \r |
de77d4ac | 659 | uint8_t blockNo = arg0 & 0xff;\r |
660 | uint8_t keyType = (arg0 >> 8) & 0xff;\r | |
661 | uint8_t targetBlockNo = arg1 & 0xff;\r | |
662 | uint8_t targetKeyType = (arg1 >> 8) & 0xff;\r | |
663 | ui64Key = bytes_to_num(datain, 6);\r | |
664 | bool initialize = flags & 0x0001;\r | |
665 | bool slow = flags & 0x0002;\r | |
666 | bool field_off = flags & 0x0004;\r | |
7906cb41 | 667 | \r |
de77d4ac | 668 | LED_A_ON();\r |
669 | LED_C_OFF();\r | |
670 | \r | |
671 | if (initialize) {\r | |
672 | iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r | |
673 | clear_trace();\r | |
674 | set_tracing(true);\r | |
675 | }\r | |
7906cb41 | 676 | \r |
de77d4ac | 677 | LED_C_ON();\r |
7906cb41 | 678 | \r |
de77d4ac | 679 | uint16_t num_nonces = 0;\r |
680 | bool have_uid = false;\r | |
681 | for (uint16_t i = 0; i <= USB_CMD_DATA_SIZE - 9; ) {\r | |
682 | \r | |
683 | // Test if the action was cancelled\r | |
684 | if(BUTTON_PRESS()) {\r | |
685 | isOK = 2;\r | |
686 | field_off = true;\r | |
687 | break;\r | |
688 | }\r | |
689 | \r | |
690 | if (!have_uid) { // need a full select cycle to get the uid first\r | |
7906cb41 | 691 | iso14a_card_select_t card_info;\r |
c04a4b60 | 692 | if(!iso14443a_select_card(uid, &card_info, &cuid, true, 0, true)) {\r |
0b4efbde | 693 | if (MF_DBGLEVEL >= 1) Dbprintf("AcquireNonces: Can't select card (ALL)");\r |
de77d4ac | 694 | continue;\r |
695 | }\r | |
696 | switch (card_info.uidlen) {\r | |
697 | case 4 : cascade_levels = 1; break;\r | |
698 | case 7 : cascade_levels = 2; break;\r | |
699 | case 10: cascade_levels = 3; break;\r | |
700 | default: break;\r | |
701 | }\r | |
7906cb41 | 702 | have_uid = true;\r |
de77d4ac | 703 | } else { // no need for anticollision. We can directly select the card\r |
c04a4b60 | 704 | if(!iso14443a_select_card(uid, NULL, NULL, false, cascade_levels, true)) {\r |
0b4efbde | 705 | if (MF_DBGLEVEL >= 1) Dbprintf("AcquireNonces: Can't select card (UID)");\r |
de77d4ac | 706 | continue;\r |
707 | }\r | |
708 | }\r | |
7906cb41 | 709 | \r |
de77d4ac | 710 | if (slow) {\r |
d1f9ec06 | 711 | timeout = GetCountSspClk() + HARDNESTED_PRE_AUTHENTICATION_LEADTIME;\r |
de77d4ac | 712 | while(GetCountSspClk() < timeout);\r |
713 | }\r | |
714 | \r | |
715 | uint32_t nt1;\r | |
a749b1e5 | 716 | if (mifare_classic_authex(pcs, cuid, blockNo, keyType, ui64Key, AUTH_FIRST, &nt1, NULL, NULL)) {\r |
0b4efbde | 717 | if (MF_DBGLEVEL >= 1) Dbprintf("AcquireNonces: Auth1 error");\r |
de77d4ac | 718 | continue;\r |
719 | }\r | |
720 | \r | |
721 | // nested authentication\r | |
722 | uint16_t len = mifare_sendcmd_short(pcs, AUTH_NESTED, 0x60 + (targetKeyType & 0x01), targetBlockNo, receivedAnswer, par_enc, NULL);\r | |
723 | if (len != 4) {\r | |
0b4efbde | 724 | if (MF_DBGLEVEL >= 1) Dbprintf("AcquireNonces: Auth2 error len=%d", len);\r |
de77d4ac | 725 | continue;\r |
726 | }\r | |
7906cb41 | 727 | \r |
d1f9ec06 | 728 | // send an incomplete dummy response in order to trigger the card's authentication failure timeout\r |
729 | uint8_t dummy_answer[1] = {0};\r | |
730 | ReaderTransmit(dummy_answer, 1, NULL);\r | |
7906cb41 | 731 | \r |
d1f9ec06 | 732 | timeout = GetCountSspClk() + HARDNESTED_AUTHENTICATION_TIMEOUT;\r |
0b4efbde | 733 | \r |
de77d4ac | 734 | num_nonces++;\r |
735 | if (num_nonces % 2) {\r | |
736 | memcpy(buf+i, receivedAnswer, 4);\r | |
737 | nt_par_enc = par_enc[0] & 0xf0;\r | |
738 | } else {\r | |
739 | nt_par_enc |= par_enc[0] >> 4;\r | |
740 | memcpy(buf+i+4, receivedAnswer, 4);\r | |
741 | memcpy(buf+i+8, &nt_par_enc, 1);\r | |
742 | i += 9;\r | |
743 | }\r | |
744 | \r | |
d1f9ec06 | 745 | // wait for the card to become ready again\r |
746 | while(GetCountSspClk() < timeout);\r | |
747 | \r | |
de77d4ac | 748 | }\r |
749 | \r | |
750 | LED_C_OFF();\r | |
7906cb41 | 751 | \r |
de77d4ac | 752 | crypto1_destroy(pcs);\r |
7906cb41 | 753 | \r |
929b61c6 | 754 | if (field_off) {\r |
755 | FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r | |
756 | LEDsoff();\r | |
757 | }\r | |
758 | \r | |
de77d4ac | 759 | LED_B_ON();\r |
760 | cmd_send(CMD_ACK, isOK, cuid, num_nonces, buf, sizeof(buf));\r | |
761 | LED_B_OFF();\r | |
762 | \r | |
0b4efbde | 763 | if (MF_DBGLEVEL >= 3) DbpString("AcquireEncryptedNonces finished");\r |
de77d4ac | 764 | \r |
de77d4ac | 765 | }\r |
766 | \r | |
767 | \r | |
8556b852 | 768 | //-----------------------------------------------------------------------------\r |
7906cb41 F |
769 | // MIFARE nested authentication.\r |
770 | //\r | |
8556b852 | 771 | //-----------------------------------------------------------------------------\r |
a749b1e5 | 772 | void MifareNested(uint32_t arg0, uint32_t arg1, uint32_t calibrate, uint8_t *datain) {\r |
773 | \r | |
9492e0b0 | 774 | uint8_t blockNo = arg0 & 0xff;\r |
775 | uint8_t keyType = (arg0 >> 8) & 0xff;\r | |
776 | uint8_t targetBlockNo = arg1 & 0xff;\r | |
777 | uint8_t targetKeyType = (arg1 >> 8) & 0xff;\r | |
8556b852 M |
778 | uint64_t ui64Key = 0;\r |
779 | \r | |
780 | ui64Key = bytes_to_num(datain, 6);\r | |
7906cb41 | 781 | \r |
9492e0b0 | 782 | uint16_t rtr, i, j, len;\r |
783 | uint16_t davg;\r | |
784 | static uint16_t dmin, dmax;\r | |
1c611bbd | 785 | uint8_t uid[10];\r |
6a1f2d82 | 786 | uint32_t cuid, nt1, nt2, nttmp, nttest, ks1;\r |
787 | uint8_t par[1];\r | |
9492e0b0 | 788 | uint32_t target_nt[2], target_ks[2];\r |
5a03ea99 | 789 | uint8_t target_nt_duplicate_count = 0;\r |
7906cb41 | 790 | \r |
8556b852 | 791 | uint8_t par_array[4];\r |
9492e0b0 | 792 | uint16_t ncount = 0;\r |
8556b852 M |
793 | struct Crypto1State mpcs = {0, 0};\r |
794 | struct Crypto1State *pcs;\r | |
795 | pcs = &mpcs;\r | |
f71f4deb | 796 | uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];\r |
8556b852 | 797 | \r |
a749b1e5 | 798 | uint32_t auth1_time, auth2_time, authentication_timeout = 0;\r |
9492e0b0 | 799 | static uint16_t delta_time;\r |
800 | \r | |
09ffd16e | 801 | LED_A_ON();\r |
09ffd16e | 802 | iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r |
803 | \r | |
f71f4deb | 804 | // free eventually allocated BigBuf memory\r |
805 | BigBuf_free();\r | |
09ffd16e | 806 | \r |
5330f532 | 807 | if (calibrate) clear_trace();\r |
808 | set_tracing(true);\r | |
7906cb41 | 809 | \r |
9492e0b0 | 810 | // statistics on nonce distance\r |
dc8ba239 | 811 | int16_t isOK = 0;\r |
812 | #define NESTED_MAX_TRIES 12\r | |
813 | uint16_t unsuccessfull_tries = 0;\r | |
0b4efbde | 814 | if (calibrate) { // for first call only. Otherwise reuse previous calibration\r |
9492e0b0 | 815 | LED_B_ON();\r |
3fe4ff4f | 816 | WDT_HIT();\r |
8556b852 | 817 | \r |
9492e0b0 | 818 | davg = dmax = 0;\r |
819 | dmin = 2000;\r | |
820 | delta_time = 0;\r | |
7906cb41 | 821 | \r |
9492e0b0 | 822 | for (rtr = 0; rtr < 17; rtr++) {\r |
8556b852 | 823 | \r |
9492e0b0 | 824 | // prepare next select. No need to power down the card.\r |
a749b1e5 | 825 | if (mifare_classic_halt(pcs, cuid)) {\r |
0b4efbde | 826 | if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Halt error");\r |
9492e0b0 | 827 | rtr--;\r |
828 | continue;\r | |
829 | }\r | |
830 | \r | |
5a03ea99 | 831 | // Test if the action was cancelled\r |
a749b1e5 | 832 | if (BUTTON_PRESS()) {\r |
5a03ea99 | 833 | isOK = -2;\r |
834 | break;\r | |
835 | }\r | |
836 | \r | |
a749b1e5 | 837 | if (!iso14443a_select_card(uid, NULL, &cuid, true, 0, true)) {\r |
0b4efbde | 838 | if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Can't select card");\r |
9492e0b0 | 839 | rtr--;\r |
840 | continue;\r | |
841 | };\r | |
842 | \r | |
843 | auth1_time = 0;\r | |
a749b1e5 | 844 | if (mifare_classic_authex(pcs, cuid, blockNo, keyType, ui64Key, AUTH_FIRST, &nt1, &auth1_time, NULL)) {\r |
0b4efbde | 845 | if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Auth1 error");\r |
9492e0b0 | 846 | rtr--;\r |
847 | continue;\r | |
848 | };\r | |
849 | \r | |
850 | if (delta_time) {\r | |
851 | auth2_time = auth1_time + delta_time;\r | |
852 | } else {\r | |
853 | auth2_time = 0;\r | |
854 | }\r | |
a749b1e5 | 855 | if (mifare_classic_authex(pcs, cuid, blockNo, keyType, ui64Key, AUTH_NESTED, &nt2, &auth2_time, NULL)) {\r |
0b4efbde | 856 | if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Auth2 error");\r |
9492e0b0 | 857 | rtr--;\r |
858 | continue;\r | |
859 | };\r | |
860 | \r | |
0b4efbde | 861 | nttmp = prng_successor(nt1, 100); //NXP Mifare is typical around 840,but for some unlicensed/compatible mifare card this can be 160\r |
b19bd5d6 | 862 | for (i = 101; i < 1200; i++) {\r |
9492e0b0 | 863 | nttmp = prng_successor(nttmp, 1);\r |
864 | if (nttmp == nt2) break;\r | |
865 | }\r | |
866 | \r | |
867 | if (i != 1200) {\r | |
868 | if (rtr != 0) {\r | |
869 | davg += i;\r | |
870 | dmin = MIN(dmin, i);\r | |
871 | dmax = MAX(dmax, i);\r | |
872 | }\r | |
873 | else {\r | |
874 | delta_time = auth2_time - auth1_time + 32; // allow some slack for proper timing\r | |
875 | }\r | |
876 | if (MF_DBGLEVEL >= 3) Dbprintf("Nested: calibrating... ntdist=%d", i);\r | |
dc8ba239 | 877 | } else {\r |
878 | unsuccessfull_tries++;\r | |
0b4efbde | 879 | if (unsuccessfull_tries > NESTED_MAX_TRIES) { // card isn't vulnerable to nested attack (random numbers are not predictable)\r |
dc8ba239 | 880 | isOK = -3;\r |
881 | }\r | |
9492e0b0 | 882 | }\r |
8556b852 | 883 | }\r |
8556b852 | 884 | \r |
9492e0b0 | 885 | davg = (davg + (rtr - 1)/2) / (rtr - 1);\r |
7906cb41 | 886 | \r |
dc8ba239 | 887 | 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 | 888 | \r |
9492e0b0 | 889 | dmin = davg - 2;\r |
890 | dmax = davg + 2;\r | |
7906cb41 | 891 | \r |
9492e0b0 | 892 | LED_B_OFF();\r |
7906cb41 | 893 | \r |
9492e0b0 | 894 | }\r |
7906cb41 F |
895 | // -------------------------------------------------------------------------------------------------\r |
896 | \r | |
8556b852 M |
897 | LED_C_ON();\r |
898 | \r | |
899 | // get crypted nonces for target sector\r | |
a749b1e5 | 900 | for (i=0; i < 2 && !isOK; i++) { // look for exactly two different nonces\r |
8556b852 | 901 | \r |
9492e0b0 | 902 | target_nt[i] = 0;\r |
a749b1e5 | 903 | while (target_nt[i] == 0 && !isOK) { // continue until we have an unambiguous nonce\r |
7906cb41 | 904 | \r |
9492e0b0 | 905 | // prepare next select. No need to power down the card.\r |
906 | if(mifare_classic_halt(pcs, cuid)) {\r | |
0b4efbde | 907 | if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Halt error");\r |
9492e0b0 | 908 | continue;\r |
909 | }\r | |
8556b852 | 910 | \r |
5a03ea99 | 911 | // break out of the loop on button press\r |
a749b1e5 | 912 | if (BUTTON_PRESS()) {\r |
5a03ea99 | 913 | isOK = -2;\r |
914 | break;\r | |
915 | }\r | |
916 | \r | |
a749b1e5 | 917 | if (!iso14443a_select_card(uid, NULL, &cuid, true, 0, true)) {\r |
0b4efbde | 918 | if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Can't select card");\r |
9492e0b0 | 919 | continue;\r |
5a03ea99 | 920 | }\r |
7906cb41 | 921 | \r |
9492e0b0 | 922 | auth1_time = 0;\r |
a749b1e5 | 923 | authentication_timeout = 0;\r |
924 | if (mifare_classic_authex(pcs, cuid, blockNo, keyType, ui64Key, AUTH_FIRST, &nt1, &auth1_time, &authentication_timeout)) {\r | |
925 | if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Auth1 error");\r | |
9492e0b0 | 926 | continue;\r |
5a03ea99 | 927 | }\r |
8556b852 | 928 | \r |
9492e0b0 | 929 | // nested authentication\r |
930 | auth2_time = auth1_time + delta_time;\r | |
e35031d2 | 931 | len = mifare_sendcmd_short(pcs, AUTH_NESTED, 0x60 + (targetKeyType & 0x01), targetBlockNo, receivedAnswer, par, &auth2_time);\r |
9492e0b0 | 932 | if (len != 4) {\r |
0b4efbde | 933 | if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Auth2 error len=%d", len);\r |
9492e0b0 | 934 | continue;\r |
5a03ea99 | 935 | }\r |
7906cb41 F |
936 | \r |
937 | nt2 = bytes_to_num(receivedAnswer, 4);\r | |
6a1f2d82 | 938 | if (MF_DBGLEVEL >= 3) Dbprintf("Nonce#%d: Testing nt1=%08x nt2enc=%08x nt2par=%02x", i+1, nt1, nt2, par[0]);\r |
7906cb41 | 939 | \r |
9492e0b0 | 940 | // Parity validity check\r |
941 | for (j = 0; j < 4; j++) {\r | |
1f065e1d | 942 | par_array[j] = (oddparity8(receivedAnswer[j]) != ((par[0] >> (7-j)) & 0x01));\r |
9492e0b0 | 943 | }\r |
7906cb41 | 944 | \r |
9492e0b0 | 945 | ncount = 0;\r |
946 | nttest = prng_successor(nt1, dmin - 1);\r | |
947 | for (j = dmin; j < dmax + 1; j++) {\r | |
948 | nttest = prng_successor(nttest, 1);\r | |
949 | ks1 = nt2 ^ nttest;\r | |
950 | \r | |
951 | if (valid_nonce(nttest, nt2, ks1, par_array)){\r | |
0b4efbde | 952 | if (ncount > 0) { // we are only interested in disambiguous nonces, try again\r |
9492e0b0 | 953 | if (MF_DBGLEVEL >= 3) Dbprintf("Nonce#%d: dismissed (ambigous), ntdist=%d", i+1, j);\r |
954 | target_nt[i] = 0;\r | |
955 | break;\r | |
956 | }\r | |
957 | target_nt[i] = nttest;\r | |
958 | target_ks[i] = ks1;\r | |
959 | ncount++;\r | |
960 | if (i == 1 && target_nt[1] == target_nt[0]) { // we need two different nonces\r | |
5a03ea99 | 961 | if( ++target_nt_duplicate_count >= NESTED_MAX_TRIES ) { // unable to get a 2nd nonce after NESTED_MAX_TRIES tries, probably a fixed nonce\r |
962 | if (MF_DBGLEVEL >= 2) Dbprintf("Nonce#2: cannot get nonce that != nonce#1, continuing anyway with single nonce! ntdist=%d", j);\r | |
963 | break;\r | |
964 | }\r | |
965 | \r | |
966 | target_nt[1] = 0;\r | |
9492e0b0 | 967 | if (MF_DBGLEVEL >= 3) Dbprintf("Nonce#2: dismissed (= nonce#1), ntdist=%d", j);\r |
8556b852 M |
968 | break;\r |
969 | }\r | |
9492e0b0 | 970 | if (MF_DBGLEVEL >= 3) Dbprintf("Nonce#%d: valid, ntdist=%d", i+1, j);\r |
8556b852 | 971 | }\r |
8556b852 | 972 | }\r |
9492e0b0 | 973 | if (target_nt[i] == 0 && j == dmax+1 && MF_DBGLEVEL >= 3) Dbprintf("Nonce#%d: dismissed (all invalid)", i+1);\r |
8556b852 M |
974 | }\r |
975 | }\r | |
976 | \r | |
977 | LED_C_OFF();\r | |
7906cb41 | 978 | \r |
8556b852 M |
979 | // ----------------------------- crypto1 destroy\r |
980 | crypto1_destroy(pcs);\r | |
7906cb41 | 981 | \r |
a749b1e5 | 982 | uint8_t buf[4 + 4 * 4 + 4];\r |
9492e0b0 | 983 | memcpy(buf, &cuid, 4);\r |
984 | memcpy(buf+4, &target_nt[0], 4);\r | |
985 | memcpy(buf+8, &target_ks[0], 4);\r | |
986 | memcpy(buf+12, &target_nt[1], 4);\r | |
987 | memcpy(buf+16, &target_ks[1], 4);\r | |
a749b1e5 | 988 | memcpy(buf+20, &authentication_timeout, 4);\r |
7906cb41 | 989 | \r |
929b61c6 | 990 | FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r |
991 | \r | |
992 | if (MF_DBGLEVEL >= 3) DbpString("NESTED FINISHED");\r | |
993 | \r | |
8556b852 | 994 | LED_B_ON();\r |
dc8ba239 | 995 | cmd_send(CMD_ACK, isOK, 0, targetBlockNo + (targetKeyType * 0x100), buf, sizeof(buf));\r |
6e82300d | 996 | LED_B_OFF();\r |
8556b852 | 997 | \r |
8556b852 | 998 | LEDsoff();\r |
8556b852 M |
999 | }\r |
1000 | \r | |
a749b1e5 | 1001 | \r |
8556b852 | 1002 | //-----------------------------------------------------------------------------\r |
7906cb41 F |
1003 | // MIFARE check keys. key count up to 85.\r |
1004 | //\r | |
8556b852 | 1005 | //-----------------------------------------------------------------------------\r |
a749b1e5 | 1006 | void MifareChkKeys(uint16_t arg0, uint32_t arg1, uint8_t arg2, uint8_t *datain) {\r |
1007 | \r | |
5330f532 | 1008 | uint8_t blockNo = arg0 & 0xff;\r |
a749b1e5 | 1009 | uint8_t keyType = arg0 >> 8;\r |
275d9e61 OM |
1010 | bool clearTrace = arg1 & 0x01;\r |
1011 | bool multisectorCheck = arg1 & 0x02;\r | |
a749b1e5 | 1012 | bool init = arg1 & 0x04;\r |
1013 | bool drop_field = arg1 & 0x08;\r | |
1014 | uint32_t auth_timeout = arg1 >> 16;\r | |
8556b852 | 1015 | uint8_t keyCount = arg2;\r |
7906cb41 | 1016 | \r |
f98702ba | 1017 | LED_A_ON();\r |
1018 | \r | |
a749b1e5 | 1019 | if (init) {\r |
1020 | iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r | |
1021 | }\r | |
1022 | \r | |
f98702ba | 1023 | if (clearTrace) {\r |
1024 | clear_trace();\r | |
1025 | }\r | |
de77d4ac | 1026 | set_tracing(true);\r |
8556b852 | 1027 | \r |
a749b1e5 | 1028 | // clear debug level. We are expecting lots of authentication failures...\r |
1029 | int OLD_MF_DBGLEVEL = MF_DBGLEVEL;\r | |
1030 | MF_DBGLEVEL = MF_DBG_NONE;\r | |
0b4efbde | 1031 | \r |
a749b1e5 | 1032 | int res = 0;\r |
275d9e61 OM |
1033 | if (multisectorCheck) {\r |
1034 | TKeyIndex keyIndex = {{0}};\r | |
1035 | uint8_t sectorCnt = blockNo;\r | |
a749b1e5 | 1036 | res = MifareMultisectorChk(datain, keyCount, sectorCnt, keyType, &auth_timeout, OLD_MF_DBGLEVEL, &keyIndex);\r |
275d9e61 | 1037 | if (res >= 0) {\r |
5a03ea99 | 1038 | cmd_send(CMD_ACK, 1, res, 0, keyIndex, 80);\r |
275d9e61 | 1039 | } else {\r |
5a03ea99 | 1040 | cmd_send(CMD_ACK, 0, res, 0, NULL, 0);\r |
9492e0b0 | 1041 | }\r |
275d9e61 | 1042 | } else { \r |
a749b1e5 | 1043 | res = MifareChkBlockKeys(datain, keyCount, blockNo, keyType, &auth_timeout, OLD_MF_DBGLEVEL);\r |
275d9e61 | 1044 | if (res > 0) {\r |
5a03ea99 | 1045 | cmd_send(CMD_ACK, 1, res, 0, datain + (res - 1) * 6, 6);\r |
275d9e61 | 1046 | } else {\r |
5a03ea99 | 1047 | cmd_send(CMD_ACK, 0, res, 0, NULL, 0);\r |
de77d4ac | 1048 | }\r |
8556b852 | 1049 | }\r |
7906cb41 | 1050 | \r |
a749b1e5 | 1051 | if (drop_field || res != 0) {\r |
1052 | FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r | |
1053 | LED_D_OFF();\r | |
1054 | }\r | |
8556b852 M |
1055 | \r |
1056 | // restore debug level\r | |
7906cb41 | 1057 | MF_DBGLEVEL = OLD_MF_DBGLEVEL;\r |
a749b1e5 | 1058 | \r |
f98702ba | 1059 | LED_A_OFF();\r |
8556b852 M |
1060 | }\r |
1061 | \r | |
0b4efbde | 1062 | \r |
1063 | //-----------------------------------------------------------------------------\r | |
1064 | // MIFARE Personalize UID. Only for Mifare Classic EV1 7Byte UID\r | |
1065 | //-----------------------------------------------------------------------------\r | |
1066 | void MifarePersonalizeUID(uint8_t keyType, uint8_t perso_option, uint8_t *data) {\r | |
1067 | \r | |
1068 | uint8_t uid[10];\r | |
1069 | uint32_t cuid;\r | |
1070 | struct Crypto1State mpcs = {0, 0};\r | |
1071 | struct Crypto1State *pcs;\r | |
1072 | pcs = &mpcs;\r | |
1073 | \r | |
1074 | LED_A_ON();\r | |
1075 | clear_trace();\r | |
1076 | \r | |
1077 | iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r | |
1078 | \r | |
1079 | bool isOK = false;\r | |
1080 | while (true) {\r | |
1081 | if (!iso14443a_select_card(uid, NULL, &cuid, true, 0, true)) {\r | |
1082 | if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");\r | |
1083 | break;\r | |
1084 | }\r | |
1085 | \r | |
1086 | uint8_t block_number = 0;\r | |
1087 | uint64_t key = bytes_to_num(data, 6);\r | |
a749b1e5 | 1088 | if (mifare_classic_auth(pcs, cuid, block_number, keyType, key, AUTH_FIRST, NULL)) {\r |
0b4efbde | 1089 | if (MF_DBGLEVEL >= 1) Dbprintf("Auth error");\r |
1090 | break;\r | |
1091 | }\r | |
1092 | \r | |
1093 | uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];\r | |
1094 | uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE];\r | |
1095 | int len = mifare_sendcmd_short(pcs, true, MIFARE_EV1_PERSONAL_UID, perso_option, receivedAnswer, receivedAnswerPar, NULL);\r | |
1096 | if (len != 1 || receivedAnswer[0] != CARD_ACK) {\r | |
1097 | if (MF_DBGLEVEL >= 1) Dbprintf("Cmd Error: %02x", receivedAnswer[0]);\r | |
1098 | break;;\r | |
1099 | }\r | |
1100 | isOK = true;\r | |
1101 | break;\r | |
1102 | }\r | |
1103 | \r | |
1104 | FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r | |
1105 | LED_D_OFF();\r | |
1106 | \r | |
1107 | crypto1_destroy(pcs);\r | |
1108 | \r | |
1109 | if (MF_DBGLEVEL >= 2) DbpString("PERSONALIZE UID FINISHED");\r | |
1110 | \r | |
1111 | cmd_send(CMD_ACK, isOK, 0, 0, NULL, 0);\r | |
1112 | \r | |
1113 | LED_A_OFF();\r | |
1114 | }\r | |
1115 | \r | |
8556b852 M |
1116 | //-----------------------------------------------------------------------------\r |
1117 | // MIFARE commands set debug level\r | |
7906cb41 | 1118 | //\r |
8556b852 M |
1119 | //-----------------------------------------------------------------------------\r |
1120 | void MifareSetDbgLvl(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain){\r | |
1121 | MF_DBGLEVEL = arg0;\r | |
1122 | Dbprintf("Debug level: %d", MF_DBGLEVEL);\r | |
1123 | }\r | |
1124 | \r | |
1125 | //-----------------------------------------------------------------------------\r | |
1126 | // Work with emulator memory\r | |
7906cb41 | 1127 | //\r |
09ffd16e | 1128 | // Note: we call FpgaDownloadAndGo(FPGA_BITSTREAM_HF) here although FPGA is not\r |
1129 | // involved in dealing with emulator memory. But if it is called later, it might\r | |
1130 | // destroy the Emulator Memory.\r | |
8556b852 | 1131 | //-----------------------------------------------------------------------------\r |
09ffd16e | 1132 | \r |
8556b852 | 1133 | void MifareEMemClr(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain){\r |
09ffd16e | 1134 | FpgaDownloadAndGo(FPGA_BITSTREAM_HF);\r |
8556b852 M |
1135 | emlClearMem();\r |
1136 | }\r | |
1137 | \r | |
1138 | void MifareEMemSet(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain){\r | |
09ffd16e | 1139 | FpgaDownloadAndGo(FPGA_BITSTREAM_HF);\r |
8556b852 M |
1140 | emlSetMem(datain, arg0, arg1); // data, block num, blocks count\r |
1141 | }\r | |
1142 | \r | |
1143 | void MifareEMemGet(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain){\r | |
09ffd16e | 1144 | FpgaDownloadAndGo(FPGA_BITSTREAM_HF);\r |
3fe4ff4f | 1145 | byte_t buf[USB_CMD_DATA_SIZE];\r |
baeaf579 | 1146 | emlGetMem(buf, arg0, arg1); // data, block num, blocks count (max 4)\r |
8556b852 M |
1147 | \r |
1148 | LED_B_ON();\r | |
3fe4ff4f | 1149 | cmd_send(CMD_ACK,arg0,arg1,0,buf,USB_CMD_DATA_SIZE);\r |
8556b852 M |
1150 | LED_B_OFF();\r |
1151 | }\r | |
1152 | \r | |
1153 | //-----------------------------------------------------------------------------\r | |
1154 | // Load a card into the emulator memory\r | |
7906cb41 | 1155 | //\r |
8556b852 M |
1156 | //-----------------------------------------------------------------------------\r |
1157 | void MifareECardLoad(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain){\r | |
baeaf579 | 1158 | uint8_t numSectors = arg0;\r |
8556b852 M |
1159 | uint8_t keyType = arg1;\r |
1160 | uint64_t ui64Key = 0;\r | |
1161 | uint32_t cuid;\r | |
1162 | struct Crypto1State mpcs = {0, 0};\r | |
1163 | struct Crypto1State *pcs;\r | |
1164 | pcs = &mpcs;\r | |
1165 | \r | |
1166 | // variables\r | |
1167 | byte_t dataoutbuf[16];\r | |
ab8b654e | 1168 | byte_t dataoutbuf2[16];\r |
1c611bbd | 1169 | uint8_t uid[10];\r |
8556b852 | 1170 | \r |
8556b852 M |
1171 | LED_A_ON();\r |
1172 | LED_B_OFF();\r | |
1173 | LED_C_OFF();\r | |
09ffd16e | 1174 | iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r |
7906cb41 | 1175 | \r |
09ffd16e | 1176 | clear_trace();\r |
1177 | set_tracing(false);\r | |
7906cb41 | 1178 | \r |
baeaf579 | 1179 | bool isOK = true;\r |
1180 | \r | |
c04a4b60 | 1181 | if(!iso14443a_select_card(uid, NULL, &cuid, true, 0, true)) {\r |
baeaf579 | 1182 | isOK = false;\r |
0b4efbde | 1183 | if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");\r |
baeaf579 | 1184 | }\r |
7906cb41 | 1185 | \r |
baeaf579 | 1186 | for (uint8_t sectorNo = 0; isOK && sectorNo < numSectors; sectorNo++) {\r |
1187 | ui64Key = emlGetKey(sectorNo, keyType);\r | |
1188 | if (sectorNo == 0){\r | |
a749b1e5 | 1189 | if(isOK && mifare_classic_auth(pcs, cuid, FirstBlockOfSector(sectorNo), keyType, ui64Key, AUTH_FIRST, NULL)) {\r |
baeaf579 | 1190 | isOK = false;\r |
0b4efbde | 1191 | if (MF_DBGLEVEL >= 1) Dbprintf("Sector[%2d]. Auth error", sectorNo);\r |
8556b852 | 1192 | break;\r |
baeaf579 | 1193 | }\r |
1194 | } else {\r | |
a749b1e5 | 1195 | if(isOK && mifare_classic_auth(pcs, cuid, FirstBlockOfSector(sectorNo), keyType, ui64Key, AUTH_NESTED, NULL)) {\r |
baeaf579 | 1196 | isOK = false;\r |
0b4efbde | 1197 | if (MF_DBGLEVEL >= 1) Dbprintf("Sector[%2d]. Auth nested error", sectorNo);\r |
8556b852 | 1198 | break;\r |
baeaf579 | 1199 | }\r |
1200 | }\r | |
7906cb41 | 1201 | \r |
baeaf579 | 1202 | for (uint8_t blockNo = 0; isOK && blockNo < NumBlocksPerSector(sectorNo); blockNo++) {\r |
1203 | if(isOK && mifare_classic_readblock(pcs, cuid, FirstBlockOfSector(sectorNo) + blockNo, dataoutbuf)) {\r | |
1204 | isOK = false;\r | |
0b4efbde | 1205 | if (MF_DBGLEVEL >= 1) Dbprintf("Error reading sector %2d block %2d", sectorNo, blockNo);\r |
ab8b654e M |
1206 | break;\r |
1207 | };\r | |
baeaf579 | 1208 | if (isOK) {\r |
1209 | if (blockNo < NumBlocksPerSector(sectorNo) - 1) {\r | |
1210 | emlSetMem(dataoutbuf, FirstBlockOfSector(sectorNo) + blockNo, 1);\r | |
0b4efbde | 1211 | } else { // sector trailer, keep the keys, set only the AC\r |
baeaf579 | 1212 | emlGetMem(dataoutbuf2, FirstBlockOfSector(sectorNo) + blockNo, 1);\r |
1213 | memcpy(&dataoutbuf2[6], &dataoutbuf[6], 4);\r | |
1214 | emlSetMem(dataoutbuf2, FirstBlockOfSector(sectorNo) + blockNo, 1);\r | |
1215 | }\r | |
1216 | }\r | |
8556b852 M |
1217 | }\r |
1218 | \r | |
baeaf579 | 1219 | }\r |
1220 | \r | |
1221 | if(mifare_classic_halt(pcs, cuid)) {\r | |
0b4efbde | 1222 | if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");\r |
baeaf579 | 1223 | };\r |
8556b852 M |
1224 | \r |
1225 | // ----------------------------- crypto1 destroy\r | |
1226 | crypto1_destroy(pcs);\r | |
ab8b654e M |
1227 | \r |
1228 | FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r | |
1229 | LEDsoff();\r | |
7906cb41 | 1230 | \r |
8556b852 M |
1231 | if (MF_DBGLEVEL >= 2) DbpString("EMUL FILL SECTORS FINISHED");\r |
1232 | \r | |
8556b852 M |
1233 | }\r |
1234 | \r | |
0675f200 M |
1235 | \r |
1236 | //-----------------------------------------------------------------------------\r | |
1237 | // Work with "magic Chinese" card (email him: ouyangweidaxian@live.cn)\r | |
7906cb41 | 1238 | //\r |
0675f200 | 1239 | //-----------------------------------------------------------------------------\r |
3a05a1e7 OM |
1240 | \r |
1241 | static bool isBlockTrailer(int blockN) {\r | |
1242 | if (blockN >= 0 && blockN < 128) {\r | |
1243 | return ((blockN & 0x03) == 0x03);\r | |
1244 | }\r | |
1245 | if (blockN >= 128 && blockN <= 256) {\r | |
1246 | return ((blockN & 0x0F) == 0x0F);\r | |
1247 | }\r | |
44964fd1 | 1248 | return false;\r |
3a05a1e7 OM |
1249 | }\r |
1250 | \r | |
1251 | void MifareCWipe(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain){\r | |
1252 | // var\r | |
1253 | byte_t isOK = 0;\r | |
1254 | uint32_t numBlocks = arg0;\r | |
1255 | // cmdParams:\r | |
1256 | // bit 0 - wipe gen1a\r | |
1257 | // bit 1 - fill card with default data\r | |
1258 | // bit 2 - gen1a = 0, gen1b = 1\r | |
1259 | uint8_t cmdParams = arg1;\r | |
1260 | bool needWipe = cmdParams & 0x01;\r | |
1261 | bool needFill = cmdParams & 0x02;\r | |
1262 | bool gen1b = cmdParams & 0x04;\r | |
0b4efbde | 1263 | \r |
3a05a1e7 OM |
1264 | uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];\r |
1265 | uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE];\r | |
0b4efbde | 1266 | \r |
3a05a1e7 OM |
1267 | uint8_t block0[16] = {0x01, 0x02, 0x03, 0x04, 0x04, 0x08, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xBE, 0xAF};\r |
1268 | uint8_t block1[16] = {0x00};\r | |
1269 | uint8_t blockK[16] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x08, 0x77, 0x8F, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};\r | |
1270 | uint8_t d_block[18] = {0x00};\r | |
0b4efbde | 1271 | \r |
3a05a1e7 OM |
1272 | // card commands\r |
1273 | uint8_t wupC1[] = { 0x40 };\r | |
1274 | uint8_t wupC2[] = { 0x43 };\r | |
1275 | uint8_t wipeC[] = { 0x41 };\r | |
0b4efbde | 1276 | \r |
3a05a1e7 OM |
1277 | // iso14443 setup\r |
1278 | LED_A_ON();\r | |
1279 | LED_B_OFF();\r | |
1280 | LED_C_OFF();\r | |
1281 | iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r | |
1282 | \r | |
1283 | // tracing\r | |
1284 | clear_trace();\r | |
1285 | set_tracing(true);\r | |
0b4efbde | 1286 | \r |
3a05a1e7 OM |
1287 | while (true){\r |
1288 | // wipe\r | |
1289 | if (needWipe){\r | |
1290 | ReaderTransmitBitsPar(wupC1,7,0, NULL);\r | |
0b4efbde | 1291 | if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != CARD_ACK)) {\r |
1292 | if (MF_DBGLEVEL >= 1) Dbprintf("wupC1 error");\r | |
3a05a1e7 OM |
1293 | break;\r |
1294 | };\r | |
1295 | \r | |
1296 | ReaderTransmit(wipeC, sizeof(wipeC), NULL);\r | |
0b4efbde | 1297 | if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != CARD_ACK)) {\r |
1298 | if (MF_DBGLEVEL >= 1) Dbprintf("wipeC error");\r | |
3a05a1e7 OM |
1299 | break;\r |
1300 | };\r | |
1301 | \r | |
1302 | if(mifare_classic_halt(NULL, 0)) {\r | |
0b4efbde | 1303 | if (MF_DBGLEVEL > 2) Dbprintf("Halt error");\r |
3a05a1e7 OM |
1304 | };\r |
1305 | };\r | |
0b4efbde | 1306 | \r |
3a05a1e7 OM |
1307 | // put default data\r |
1308 | if (needFill){\r | |
1309 | // select commands\r | |
1310 | ReaderTransmitBitsPar(wupC1, 7, 0, NULL);\r | |
1311 | \r | |
0b4efbde | 1312 | // gen1b magic tag : do no issue wupC2 and don't expect CARD_ACK response after SELECT_UID (after getting UID from chip in 'hf mf csetuid' command)\r |
1313 | if (!gen1b) {\r | |
3a05a1e7 | 1314 | \r |
0b4efbde | 1315 | if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != CARD_ACK)) {\r |
1316 | if (MF_DBGLEVEL >= 1) Dbprintf("wupC1 error");\r | |
3a05a1e7 OM |
1317 | break;\r |
1318 | };\r | |
1319 | \r | |
1320 | ReaderTransmit(wupC2, sizeof(wupC2), NULL);\r | |
0b4efbde | 1321 | if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != CARD_ACK)) {\r |
1322 | if (MF_DBGLEVEL >= 1) Dbprintf("wupC2 error");\r | |
3a05a1e7 OM |
1323 | break;\r |
1324 | };\r | |
1325 | }\r | |
1326 | \r | |
1327 | // send blocks command\r | |
1328 | for (int blockNo = 0; blockNo < numBlocks; blockNo++) {\r | |
0b4efbde | 1329 | if ((mifare_sendcmd_short(NULL, 0, 0xA0, blockNo, receivedAnswer, receivedAnswerPar, NULL) != 1) || (receivedAnswer[0] != CARD_ACK)) {\r |
1330 | if (MF_DBGLEVEL >= 1) Dbprintf("write block send command error");\r | |
3a05a1e7 OM |
1331 | break;\r |
1332 | };\r | |
0b4efbde | 1333 | \r |
3a05a1e7 OM |
1334 | // check type of block and add crc\r |
1335 | if (!isBlockTrailer(blockNo)){\r | |
1336 | memcpy(d_block, block1, 16);\r | |
1337 | } else {\r | |
1338 | memcpy(d_block, blockK, 16);\r | |
1339 | }\r | |
1340 | if (blockNo == 0) {\r | |
1341 | memcpy(d_block, block0, 16);\r | |
1342 | }\r | |
1343 | AppendCrc14443a(d_block, 16);\r | |
1344 | \r | |
1345 | // send write command\r | |
1346 | ReaderTransmit(d_block, sizeof(d_block), NULL);\r | |
0b4efbde | 1347 | if ((ReaderReceive(receivedAnswer, receivedAnswerPar) != 1) || (receivedAnswer[0] != CARD_ACK)) {\r |
1348 | if (MF_DBGLEVEL >= 1) Dbprintf("write block send data error");\r | |
3a05a1e7 OM |
1349 | break;\r |
1350 | };\r | |
1351 | }\r | |
0b4efbde | 1352 | \r |
3a05a1e7 | 1353 | // halt\r |
0b4efbde | 1354 | // do no issue halt command for gen1b\r |
3a05a1e7 OM |
1355 | if (!gen1b) {\r |
1356 | if (mifare_classic_halt(NULL, 0)) {\r | |
0b4efbde | 1357 | if (MF_DBGLEVEL > 2) Dbprintf("Halt error");\r |
3a05a1e7 OM |
1358 | break;\r |
1359 | }\r | |
1360 | }\r | |
1361 | }\r | |
1362 | break;\r | |
0b4efbde | 1363 | }\r |
3a05a1e7 | 1364 | \r |
929b61c6 | 1365 | // reset fpga\r |
1366 | FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r | |
1367 | \r | |
3a05a1e7 OM |
1368 | // send USB response\r |
1369 | LED_B_ON();\r | |
1370 | cmd_send(CMD_ACK,isOK,0,0,NULL,0);\r | |
1371 | LED_B_OFF();\r | |
0b4efbde | 1372 | \r |
3a05a1e7 | 1373 | LEDsoff();\r |
0b4efbde | 1374 | \r |
3a05a1e7 OM |
1375 | return;\r |
1376 | }\r | |
1377 | \r | |
0675f200 | 1378 | void MifareCSetBlock(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain){\r |
7906cb41 | 1379 | \r |
0675f200 M |
1380 | // params\r |
1381 | uint8_t needWipe = arg0;\r | |
208a0166 M |
1382 | // bit 0 - need get UID\r |
1383 | // bit 1 - need wupC\r | |
1384 | // bit 2 - need HALT after sequence\r | |
1385 | // bit 3 - need init FPGA and field before sequence\r | |
1386 | // bit 4 - need reset FPGA and LED\r | |
7906cb41 | 1387 | // bit 6 - gen1b backdoor type\r |
208a0166 | 1388 | uint8_t workFlags = arg1;\r |
0675f200 | 1389 | uint8_t blockNo = arg2;\r |
7906cb41 | 1390 | \r |
0675f200 | 1391 | // card commands\r |
7906cb41 F |
1392 | uint8_t wupC1[] = { 0x40 };\r |
1393 | uint8_t wupC2[] = { 0x43 };\r | |
1394 | uint8_t wipeC[] = { 0x41 };\r | |
1395 | \r | |
0675f200 M |
1396 | // variables\r |
1397 | byte_t isOK = 0;\r | |
3fe4ff4f | 1398 | uint8_t uid[10] = {0x00};\r |
1399 | uint8_t d_block[18] = {0x00};\r | |
0675f200 | 1400 | uint32_t cuid;\r |
7906cb41 | 1401 | \r |
f71f4deb | 1402 | uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];\r |
1403 | uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE];\r | |
6a1f2d82 | 1404 | \r |
3fe4ff4f | 1405 | // reset FPGA and LED\r |
208a0166 | 1406 | if (workFlags & 0x08) {\r |
208a0166 M |
1407 | LED_A_ON();\r |
1408 | LED_B_OFF();\r | |
1409 | LED_C_OFF();\r | |
09ffd16e | 1410 | iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r |
7906cb41 | 1411 | \r |
3000dc4e | 1412 | clear_trace();\r |
de77d4ac | 1413 | set_tracing(true);\r |
208a0166 | 1414 | }\r |
0675f200 M |
1415 | \r |
1416 | while (true) {\r | |
3fe4ff4f | 1417 | \r |
0675f200 | 1418 | // get UID from chip\r |
208a0166 | 1419 | if (workFlags & 0x01) {\r |
c04a4b60 | 1420 | if(!iso14443a_select_card(uid, NULL, &cuid, true, 0, true)) {\r |
0b4efbde | 1421 | if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");\r |
3a05a1e7 OM |
1422 | // Continue, if we set wrong UID or wrong UID checksum or some ATQA or SAK we will can't select card. But we need to write block 0 to make card work.\r |
1423 | //break;\r | |
7906cb41 | 1424 | };\r |
0675f200 | 1425 | \r |
7906cb41 | 1426 | if(mifare_classic_halt(NULL, cuid)) {\r |
0b4efbde | 1427 | if (MF_DBGLEVEL > 2) Dbprintf("Halt error");\r |
7906cb41 | 1428 | // Continue, some magic tags misbehavies and send an answer to it.\r |
3a05a1e7 | 1429 | // break;\r |
7906cb41 | 1430 | };\r |
0675f200 | 1431 | };\r |
7906cb41 | 1432 | \r |
0675f200 | 1433 | // reset chip\r |
7906cb41 F |
1434 | // Wipe command don't work with gen1b\r |
1435 | if (needWipe && !(workFlags & 0x40)){\r | |
6a1f2d82 | 1436 | ReaderTransmitBitsPar(wupC1,7,0, NULL);\r |
0b4efbde | 1437 | if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != CARD_ACK)) {\r |
1438 | if (MF_DBGLEVEL >= 1) Dbprintf("wupC1 error");\r | |
0675f200 M |
1439 | break;\r |
1440 | };\r | |
1441 | \r | |
9492e0b0 | 1442 | ReaderTransmit(wipeC, sizeof(wipeC), NULL);\r |
0b4efbde | 1443 | if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != CARD_ACK)) {\r |
1444 | if (MF_DBGLEVEL >= 1) Dbprintf("wipeC error");\r | |
0675f200 M |
1445 | break;\r |
1446 | };\r | |
1447 | \r | |
3a05a1e7 | 1448 | if(mifare_classic_halt(NULL, 0)) {\r |
0b4efbde | 1449 | if (MF_DBGLEVEL > 2) Dbprintf("Halt error");\r |
7906cb41 | 1450 | // Continue, some magic tags misbehavies and send an answer to it.\r |
0b4efbde | 1451 | // break;\r |
0675f200 | 1452 | };\r |
7906cb41 | 1453 | };\r |
0675f200 | 1454 | \r |
545a1f38 | 1455 | // write block\r |
208a0166 | 1456 | if (workFlags & 0x02) {\r |
6a1f2d82 | 1457 | ReaderTransmitBitsPar(wupC1,7,0, NULL);\r |
0675f200 | 1458 | \r |
0b4efbde | 1459 | // gen1b magic tag : do no issue wupC2 and don't expect CARD_ACK response after SELECT_UID (after getting UID from chip in 'hf mf csetuid' command)\r |
7906cb41 F |
1460 | if (!(workFlags & 0x40)) {\r |
1461 | \r | |
0b4efbde | 1462 | if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != CARD_ACK)) {\r |
1463 | if (MF_DBGLEVEL >= 1) Dbprintf("wupC1 error");\r | |
7906cb41 F |
1464 | break;\r |
1465 | };\r | |
1466 | \r | |
1467 | ReaderTransmit(wupC2, sizeof(wupC2), NULL);\r | |
0b4efbde | 1468 | if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != CARD_ACK)) {\r |
1469 | if (MF_DBGLEVEL >= 1) Dbprintf("wupC2 error");\r | |
7906cb41 F |
1470 | break;\r |
1471 | };\r | |
1472 | }\r | |
208a0166 | 1473 | }\r |
0675f200 | 1474 | \r |
0b4efbde | 1475 | if ((mifare_sendcmd_short(NULL, 0, 0xA0, blockNo, receivedAnswer, receivedAnswerPar, NULL) != 1) || (receivedAnswer[0] != CARD_ACK)) {\r |
1476 | if (MF_DBGLEVEL >= 1) Dbprintf("write block send command error");\r | |
0675f200 M |
1477 | break;\r |
1478 | };\r | |
7906cb41 | 1479 | \r |
0675f200 M |
1480 | memcpy(d_block, datain, 16);\r |
1481 | AppendCrc14443a(d_block, 16);\r | |
7906cb41 | 1482 | \r |
9492e0b0 | 1483 | ReaderTransmit(d_block, sizeof(d_block), NULL);\r |
0b4efbde | 1484 | if ((ReaderReceive(receivedAnswer, receivedAnswerPar) != 1) || (receivedAnswer[0] != CARD_ACK)) {\r |
1485 | if (MF_DBGLEVEL >= 1) Dbprintf("write block send data error");\r | |
0675f200 | 1486 | break;\r |
7906cb41 F |
1487 | };\r |
1488 | \r | |
208a0166 | 1489 | if (workFlags & 0x04) {\r |
7906cb41 F |
1490 | // do no issue halt command for gen1b magic tag (#db# halt error. response len: 1)\r |
1491 | if (!(workFlags & 0x40)) {\r | |
3a05a1e7 | 1492 | if (mifare_classic_halt(NULL, 0)) {\r |
0b4efbde | 1493 | if (MF_DBGLEVEL > 2) Dbprintf("Halt error");\r |
7906cb41 F |
1494 | // Continue, some magic tags misbehavies and send an answer to it.\r |
1495 | // break;\r | |
1496 | }\r | |
1497 | }\r | |
208a0166 | 1498 | }\r |
7906cb41 | 1499 | \r |
0675f200 M |
1500 | isOK = 1;\r |
1501 | break;\r | |
1502 | }\r | |
7906cb41 | 1503 | \r |
929b61c6 | 1504 | if ((workFlags & 0x10) || (!isOK)) {\r |
1505 | FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r | |
1506 | }\r | |
1507 | \r | |
0675f200 | 1508 | LED_B_ON();\r |
baeaf579 | 1509 | cmd_send(CMD_ACK,isOK,0,0,uid,4);\r |
0675f200 M |
1510 | LED_B_OFF();\r |
1511 | \r | |
929b61c6 | 1512 | LEDsoff();\r |
0675f200 | 1513 | }\r |
545a1f38 | 1514 | \r |
baeaf579 | 1515 | \r |
545a1f38 | 1516 | void MifareCGetBlock(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain){\r |
7906cb41 F |
1517 | \r |
1518 | // params\r | |
545a1f38 M |
1519 | // bit 1 - need wupC\r |
1520 | // bit 2 - need HALT after sequence\r | |
1521 | // bit 3 - need init FPGA and field before sequence\r | |
1522 | // bit 4 - need reset FPGA and LED\r | |
c89274cc | 1523 | // bit 5 - need to set datain instead of issuing USB reply (called via ARM for StandAloneMode14a)\r |
7906cb41 | 1524 | // bit 6 - gen1b backdoor type\r |
545a1f38 M |
1525 | uint8_t workFlags = arg0;\r |
1526 | uint8_t blockNo = arg2;\r | |
7906cb41 | 1527 | \r |
545a1f38 | 1528 | // card commands\r |
7906cb41 F |
1529 | uint8_t wupC1[] = { 0x40 };\r |
1530 | uint8_t wupC2[] = { 0x43 };\r | |
1531 | \r | |
545a1f38 M |
1532 | // variables\r |
1533 | byte_t isOK = 0;\r | |
3fe4ff4f | 1534 | uint8_t data[18] = {0x00};\r |
545a1f38 | 1535 | uint32_t cuid = 0;\r |
7906cb41 | 1536 | \r |
f71f4deb | 1537 | uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];\r |
1538 | uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE];\r | |
7906cb41 | 1539 | \r |
545a1f38 | 1540 | if (workFlags & 0x08) {\r |
545a1f38 M |
1541 | LED_A_ON();\r |
1542 | LED_B_OFF();\r | |
1543 | LED_C_OFF();\r | |
09ffd16e | 1544 | iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r |
1545 | \r | |
3000dc4e | 1546 | clear_trace();\r |
de77d4ac | 1547 | set_tracing(true);\r |
545a1f38 M |
1548 | }\r |
1549 | \r | |
1550 | while (true) {\r | |
1551 | if (workFlags & 0x02) {\r | |
7bc95e2e | 1552 | ReaderTransmitBitsPar(wupC1,7,0, NULL);\r |
0b4efbde | 1553 | if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != CARD_ACK)) {\r |
1554 | if (MF_DBGLEVEL >= 1) Dbprintf("wupC1 error");\r | |
545a1f38 | 1555 | break;\r |
7906cb41 F |
1556 | };\r |
1557 | // do no issue for gen1b magic tag\r | |
1558 | if (!(workFlags & 0x40)) {\r | |
9492e0b0 | 1559 | ReaderTransmit(wupC2, sizeof(wupC2), NULL);\r |
0b4efbde | 1560 | if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != CARD_ACK)) {\r |
1561 | if (MF_DBGLEVEL >= 1) Dbprintf("wupC2 error");\r | |
545a1f38 M |
1562 | break;\r |
1563 | };\r | |
1564 | }\r | |
7906cb41 | 1565 | }\r |
545a1f38 M |
1566 | \r |
1567 | // read block\r | |
6a1f2d82 | 1568 | if ((mifare_sendcmd_short(NULL, 0, 0x30, blockNo, receivedAnswer, receivedAnswerPar, NULL) != 18)) {\r |
0b4efbde | 1569 | if (MF_DBGLEVEL >= 1) Dbprintf("read block send command error");\r |
545a1f38 M |
1570 | break;\r |
1571 | };\r | |
1572 | memcpy(data, receivedAnswer, 18);\r | |
7906cb41 | 1573 | \r |
545a1f38 | 1574 | if (workFlags & 0x04) {\r |
7906cb41 F |
1575 | // do no issue halt command for gen1b magic tag (#db# halt error. response len: 1)\r |
1576 | if (!(workFlags & 0x40)) {\r | |
1577 | if (mifare_classic_halt(NULL, cuid)) {\r | |
0b4efbde | 1578 | if (MF_DBGLEVEL > 1) Dbprintf("Halt error");\r |
7906cb41 | 1579 | // Continue, some magic tags misbehavies and send an answer to it.\r |
0b4efbde | 1580 | // break;\r |
7906cb41 F |
1581 | }\r |
1582 | }\r | |
545a1f38 | 1583 | }\r |
7906cb41 | 1584 | \r |
545a1f38 M |
1585 | isOK = 1;\r |
1586 | break;\r | |
1587 | }\r | |
7906cb41 | 1588 | \r |
929b61c6 | 1589 | if ((workFlags & 0x10) || (!isOK)) {\r |
1590 | FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r | |
1591 | }\r | |
1592 | \r | |
545a1f38 | 1593 | LED_B_ON();\r |
c89274cc CY |
1594 | if (workFlags & 0x20) {\r |
1595 | if (isOK)\r | |
1596 | memcpy(datain, data, 18);\r | |
1597 | }\r | |
1598 | else\r | |
1599 | cmd_send(CMD_ACK,isOK,0,0,data,18);\r | |
545a1f38 M |
1600 | LED_B_OFF();\r |
1601 | \r | |
929b61c6 | 1602 | LEDsoff();\r |
545a1f38 M |
1603 | }\r |
1604 | \r | |
3fe4ff4f | 1605 | void MifareCIdent(){\r |
7906cb41 | 1606 | \r |
3fe4ff4f | 1607 | // card commands\r |
7906cb41 F |
1608 | uint8_t wupC1[] = { 0x40 };\r |
1609 | uint8_t wupC2[] = { 0x43 };\r | |
1610 | \r | |
3fe4ff4f | 1611 | // variables\r |
7906cb41 F |
1612 | byte_t isOK = 0;\r |
1613 | \r | |
f71f4deb | 1614 | uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];\r |
1615 | uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE];\r | |
0b4efbde | 1616 | \r |
8bdb6043 OM |
1617 | LED_A_ON();\r |
1618 | LED_B_OFF();\r | |
1619 | LED_C_OFF();\r | |
8bdb6043 OM |
1620 | iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r |
1621 | \r | |
1622 | clear_trace();\r | |
0b4efbde | 1623 | set_tracing(true);\r |
3fe4ff4f | 1624 | \r |
1625 | ReaderTransmitBitsPar(wupC1,7,0, NULL);\r | |
0b4efbde | 1626 | if(ReaderReceive(receivedAnswer, receivedAnswerPar) && (receivedAnswer[0] == CARD_ACK)) {\r |
7906cb41 | 1627 | isOK = 2;\r |
3fe4ff4f | 1628 | \r |
7906cb41 | 1629 | ReaderTransmit(wupC2, sizeof(wupC2), NULL);\r |
0b4efbde | 1630 | if(ReaderReceive(receivedAnswer, receivedAnswerPar) && (receivedAnswer[0] == CARD_ACK)) {\r |
7906cb41 F |
1631 | isOK = 1;\r |
1632 | };\r | |
3fe4ff4f | 1633 | };\r |
1634 | \r | |
7906cb41 F |
1635 | // From iceman1001: removed the if, since some magic tags misbehavies and send an answer to it.\r |
1636 | mifare_classic_halt(NULL, 0);\r | |
0b4efbde | 1637 | \r |
929b61c6 | 1638 | FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r |
1639 | \r | |
8bdb6043 | 1640 | LED_B_ON();\r |
3fe4ff4f | 1641 | cmd_send(CMD_ACK,isOK,0,0,0,0);\r |
8bdb6043 OM |
1642 | LED_B_OFF();\r |
1643 | \r | |
0b4efbde | 1644 | LEDsoff();\r |
3fe4ff4f | 1645 | }\r |
1646 | \r | |
09ffd16e | 1647 | //\r |
3fe4ff4f | 1648 | // DESFIRE\r |
1649 | //\r | |
a631936e | 1650 | \r |
1651 | void Mifare_DES_Auth1(uint8_t arg0, uint8_t *datain){\r | |
1652 | \r | |
1653 | byte_t dataout[11] = {0x00};\r | |
1654 | uint8_t uid[10] = {0x00};\r | |
1655 | uint32_t cuid;\r | |
7906cb41 | 1656 | \r |
a631936e | 1657 | iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r |
09ffd16e | 1658 | clear_trace();\r |
a631936e | 1659 | \r |
c04a4b60 | 1660 | int len = iso14443a_select_card(uid, NULL, &cuid, true, 0, true);\r |
a631936e | 1661 | if(!len) {\r |
8258f409 | 1662 | if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Can't select card");\r |
1663 | OnError(1);\r | |
a631936e | 1664 | return;\r |
1665 | };\r | |
1666 | \r | |
1667 | if(mifare_desfire_des_auth1(cuid, dataout)){\r | |
8258f409 | 1668 | if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Authentication part1: Fail.");\r |
1669 | OnError(4);\r | |
a631936e | 1670 | return;\r |
1671 | }\r | |
1672 | \r | |
1673 | if (MF_DBGLEVEL >= MF_DBG_EXTENDED) DbpString("AUTH 1 FINISHED");\r | |
929b61c6 | 1674 | \r |
1675 | cmd_send(CMD_ACK, 1, cuid, 0, dataout, sizeof(dataout));\r | |
a631936e | 1676 | }\r |
1677 | \r | |
1678 | void Mifare_DES_Auth2(uint32_t arg0, uint8_t *datain){\r | |
1679 | \r | |
1680 | uint32_t cuid = arg0;\r | |
1681 | uint8_t key[16] = {0x00};\r | |
1682 | byte_t isOK = 0;\r | |
1683 | byte_t dataout[12] = {0x00};\r | |
7906cb41 | 1684 | \r |
a631936e | 1685 | memcpy(key, datain, 16);\r |
7906cb41 | 1686 | \r |
a631936e | 1687 | isOK = mifare_desfire_des_auth2(cuid, key, dataout);\r |
7906cb41 | 1688 | \r |
929b61c6 | 1689 | if (isOK) {\r |
7906cb41 | 1690 | if (MF_DBGLEVEL >= MF_DBG_EXTENDED) Dbprintf("Authentication part2: Failed");\r |
4973f23d | 1691 | OnError(4);\r |
a631936e | 1692 | return;\r |
1693 | }\r | |
1694 | \r | |
929b61c6 | 1695 | FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r |
1696 | \r | |
4973f23d | 1697 | if (MF_DBGLEVEL >= MF_DBG_EXTENDED) DbpString("AUTH 2 FINISHED");\r |
a631936e | 1698 | \r |
1699 | cmd_send(CMD_ACK, isOK, 0, 0, dataout, sizeof(dataout));\r | |
a631936e | 1700 | LEDsoff();\r |
3000dc4e | 1701 | }\r |
f168b263 | 1702 | \r |