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