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