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