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