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