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