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