]>
Commit | Line | Data |
---|---|---|
1 | //-----------------------------------------------------------------------------\r | |
2 | // Merlok, May 2011, 2012\r | |
3 | // Many authors, whom made it possible\r | |
4 | //\r | |
5 | // This code is licensed to you under the terms of the GNU GPL, version 2 or,\r | |
6 | // at your option, any later version. See the LICENSE.txt file for the text of\r | |
7 | // the license.\r | |
8 | //-----------------------------------------------------------------------------\r | |
9 | // Work with mifare cards.\r | |
10 | //-----------------------------------------------------------------------------\r | |
11 | \r | |
12 | #include "proxmark3.h"\r | |
13 | #include "apps.h"\r | |
14 | #include "util.h"\r | |
15 | #include "string.h"\r | |
16 | \r | |
17 | #include "iso14443crc.h"\r | |
18 | #include "iso14443a.h"\r | |
19 | #include "crapto1.h"\r | |
20 | #include "mifareutil.h"\r | |
21 | #include "parity.h"\r | |
22 | #include "des.h"\r | |
23 | \r | |
24 | int MF_DBGLEVEL = MF_DBG_ALL;\r | |
25 | \r | |
26 | // crypto1 helpers\r | |
27 | void mf_crypto1_decrypt(struct Crypto1State *pcs, uint8_t *data, int len){\r | |
28 | uint8_t bt = 0;\r | |
29 | int i;\r | |
30 | \r | |
31 | if (len != 1) {\r | |
32 | for (i = 0; i < len; i++)\r | |
33 | data[i] = crypto1_byte(pcs, 0x00, 0) ^ data[i];\r | |
34 | } else {\r | |
35 | bt = 0;\r | |
36 | for (i = 0; i < 4; i++)\r | |
37 | bt |= (crypto1_bit(pcs, 0, 0) ^ BIT(data[0], i)) << i;\r | |
38 | \r | |
39 | data[0] = bt;\r | |
40 | }\r | |
41 | return;\r | |
42 | }\r | |
43 | \r | |
44 | void mf_crypto1_encrypt(struct Crypto1State *pcs, uint8_t *data, uint16_t len, uint8_t *par) {\r | |
45 | uint8_t bt = 0;\r | |
46 | int i;\r | |
47 | par[0] = 0;\r | |
48 | \r | |
49 | for (i = 0; i < len; i++) {\r | |
50 | bt = data[i];\r | |
51 | data[i] = crypto1_byte(pcs, 0x00, 0) ^ data[i];\r | |
52 | if((i&0x0007) == 0) \r | |
53 | par[i>>3] = 0;\r | |
54 | par[i>>3] |= (((filter(pcs->odd) ^ oddparity8(bt)) & 0x01)<<(7-(i&0x0007)));\r | |
55 | } \r | |
56 | return;\r | |
57 | }\r | |
58 | \r | |
59 | uint8_t mf_crypto1_encrypt4bit(struct Crypto1State *pcs, uint8_t data) {\r | |
60 | uint8_t bt = 0;\r | |
61 | int i;\r | |
62 | \r | |
63 | for (i = 0; i < 4; i++)\r | |
64 | bt |= (crypto1_bit(pcs, 0, 0) ^ BIT(data, i)) << i;\r | |
65 | \r | |
66 | return bt;\r | |
67 | }\r | |
68 | \r | |
69 | // send X byte basic commands\r | |
70 | int mifare_sendcmd(uint8_t cmd, uint8_t* data, uint8_t data_size, uint8_t* answer, uint8_t *answer_parity, uint32_t *timing)\r | |
71 | {\r | |
72 | uint8_t dcmd[data_size+3];\r | |
73 | dcmd[0] = cmd;\r | |
74 | memcpy(dcmd+1,data,data_size);\r | |
75 | AppendCrc14443a(dcmd, data_size+1);\r | |
76 | ReaderTransmit(dcmd, sizeof(dcmd), timing);\r | |
77 | int len = ReaderReceive(answer, answer_parity);\r | |
78 | if(!len) {\r | |
79 | if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("%02X Cmd failed. Card timeout.", cmd);\r | |
80 | len = ReaderReceive(answer,answer_parity);\r | |
81 | //return 0;\r | |
82 | }\r | |
83 | return len;\r | |
84 | }\r | |
85 | \r | |
86 | // send 2 byte commands\r | |
87 | int mifare_sendcmd_short(struct Crypto1State *pcs, uint8_t crypted, uint8_t cmd, uint8_t data, uint8_t *answer, uint8_t *answer_parity, uint32_t *timing)\r | |
88 | {\r | |
89 | uint8_t dcmd[4], ecmd[4];\r | |
90 | uint16_t pos, res;\r | |
91 | uint8_t par[1]; // 1 Byte parity is enough here\r | |
92 | dcmd[0] = cmd;\r | |
93 | dcmd[1] = data;\r | |
94 | AppendCrc14443a(dcmd, 2);\r | |
95 | \r | |
96 | memcpy(ecmd, dcmd, sizeof(dcmd));\r | |
97 | \r | |
98 | if (crypted) {\r | |
99 | par[0] = 0;\r | |
100 | for (pos = 0; pos < 4; pos++)\r | |
101 | {\r | |
102 | ecmd[pos] = crypto1_byte(pcs, 0x00, 0) ^ dcmd[pos];\r | |
103 | par[0] |= (((filter(pcs->odd) ^ oddparity8(dcmd[pos])) & 0x01) << (7-pos));\r | |
104 | } \r | |
105 | \r | |
106 | ReaderTransmitPar(ecmd, sizeof(ecmd), par, timing);\r | |
107 | \r | |
108 | } else {\r | |
109 | ReaderTransmit(dcmd, sizeof(dcmd), timing);\r | |
110 | }\r | |
111 | \r | |
112 | int len = ReaderReceive(answer, par);\r | |
113 | \r | |
114 | if (answer_parity) *answer_parity = par[0];\r | |
115 | \r | |
116 | if (crypted == CRYPT_ALL) {\r | |
117 | if (len == 1) {\r | |
118 | res = 0;\r | |
119 | for (pos = 0; pos < 4; pos++)\r | |
120 | res |= (crypto1_bit(pcs, 0, 0) ^ BIT(answer[0], pos)) << pos;\r | |
121 | \r | |
122 | answer[0] = res;\r | |
123 | \r | |
124 | } else {\r | |
125 | for (pos = 0; pos < len; pos++)\r | |
126 | {\r | |
127 | answer[pos] = crypto1_byte(pcs, 0x00, 0) ^ answer[pos];\r | |
128 | }\r | |
129 | }\r | |
130 | }\r | |
131 | \r | |
132 | return len;\r | |
133 | }\r | |
134 | \r | |
135 | // mifare classic commands\r | |
136 | int mifare_classic_auth(struct Crypto1State *pcs, uint32_t uid, uint8_t blockNo, uint8_t keyType, uint64_t ui64Key, uint8_t isNested) \r | |
137 | {\r | |
138 | return mifare_classic_authex(pcs, uid, blockNo, keyType, ui64Key, isNested, NULL, NULL);\r | |
139 | }\r | |
140 | \r | |
141 | int mifare_classic_authex(struct Crypto1State *pcs, uint32_t uid, uint8_t blockNo, uint8_t keyType, uint64_t ui64Key, uint8_t isNested, uint32_t *ntptr, uint32_t *timing) \r | |
142 | {\r | |
143 | // variables\r | |
144 | int len; \r | |
145 | uint32_t pos;\r | |
146 | uint8_t tmp4[4];\r | |
147 | uint8_t par[1] = {0x00};\r | |
148 | \r | |
149 | // "random" reader nonce:\r | |
150 | byte_t nr[4] = {0x55, 0x41, 0x49, 0x92};\r | |
151 | //byte_t nr[4] = {0x01, 0x01, 0x01, 0x01};\r | |
152 | \r | |
153 | uint32_t nt, ntpp; // Supplied tag nonce\r | |
154 | \r | |
155 | uint8_t mf_nr_ar[] = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };\r | |
156 | uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];\r | |
157 | uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE];\r | |
158 | \r | |
159 | // Transmit MIFARE_CLASSIC_AUTH\r | |
160 | len = mifare_sendcmd_short(pcs, isNested, 0x60 + (keyType & 0x01), blockNo, receivedAnswer, receivedAnswerPar, timing);\r | |
161 | if (MF_DBGLEVEL >= 4) Dbprintf("rand tag nonce len: %x", len); \r | |
162 | if (len != 4) return 1;\r | |
163 | \r | |
164 | // Save the tag nonce (nt)\r | |
165 | nt = bytes_to_num(receivedAnswer, 4);\r | |
166 | \r | |
167 | // ----------------------------- crypto1 create\r | |
168 | if (isNested)\r | |
169 | crypto1_destroy(pcs);\r | |
170 | \r | |
171 | // Init cipher with key\r | |
172 | crypto1_create(pcs, ui64Key);\r | |
173 | \r | |
174 | if (isNested == AUTH_NESTED) {\r | |
175 | // decrypt nt with help of new key \r | |
176 | nt = crypto1_word(pcs, nt ^ uid, 1) ^ nt;\r | |
177 | } else {\r | |
178 | // Load (plain) uid^nt into the cipher\r | |
179 | crypto1_word(pcs, nt ^ uid, 0);\r | |
180 | }\r | |
181 | \r | |
182 | // some statistic\r | |
183 | if (!ntptr && (MF_DBGLEVEL >= 3))\r | |
184 | Dbprintf("auth uid: %08x nt: %08x", uid, nt); \r | |
185 | \r | |
186 | // save Nt\r | |
187 | if (ntptr)\r | |
188 | *ntptr = nt;\r | |
189 | \r | |
190 | // Generate (encrypted) nr+parity by loading it into the cipher (Nr)\r | |
191 | par[0] = 0;\r | |
192 | for (pos = 0; pos < 4; pos++)\r | |
193 | {\r | |
194 | mf_nr_ar[pos] = crypto1_byte(pcs, nr[pos], 0) ^ nr[pos];\r | |
195 | par[0] |= (((filter(pcs->odd) ^ oddparity8(nr[pos])) & 0x01) << (7-pos));\r | |
196 | } \r | |
197 | \r | |
198 | // Skip 32 bits in pseudo random generator\r | |
199 | nt = prng_successor(nt,32);\r | |
200 | \r | |
201 | // ar+parity\r | |
202 | for (pos = 4; pos < 8; pos++)\r | |
203 | {\r | |
204 | nt = prng_successor(nt,8);\r | |
205 | mf_nr_ar[pos] = crypto1_byte(pcs,0x00,0) ^ (nt & 0xff);\r | |
206 | par[0] |= (((filter(pcs->odd) ^ oddparity8(nt & 0xff)) & 0x01) << (7-pos));\r | |
207 | } \r | |
208 | \r | |
209 | // Transmit reader nonce and reader answer\r | |
210 | ReaderTransmitPar(mf_nr_ar, sizeof(mf_nr_ar), par, NULL);\r | |
211 | \r | |
212 | // Receive 4 byte tag answer\r | |
213 | len = ReaderReceive(receivedAnswer, receivedAnswerPar);\r | |
214 | if (!len)\r | |
215 | {\r | |
216 | if (MF_DBGLEVEL >= 1) Dbprintf("Authentication failed. Card timeout.");\r | |
217 | return 2;\r | |
218 | }\r | |
219 | \r | |
220 | memcpy(tmp4, receivedAnswer, 4);\r | |
221 | ntpp = prng_successor(nt, 32) ^ crypto1_word(pcs, 0,0);\r | |
222 | \r | |
223 | if (ntpp != bytes_to_num(tmp4, 4)) {\r | |
224 | if (MF_DBGLEVEL >= 1) Dbprintf("Authentication failed. Error card response.");\r | |
225 | return 3;\r | |
226 | }\r | |
227 | \r | |
228 | return 0;\r | |
229 | }\r | |
230 | \r | |
231 | int mifare_classic_readblock(struct Crypto1State *pcs, uint32_t uid, uint8_t blockNo, uint8_t *blockData) \r | |
232 | {\r | |
233 | // variables\r | |
234 | int len; \r | |
235 | uint8_t bt[2];\r | |
236 | \r | |
237 | uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];\r | |
238 | uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE];\r | |
239 | \r | |
240 | // command MIFARE_CLASSIC_READBLOCK\r | |
241 | len = mifare_sendcmd_short(pcs, 1, 0x30, blockNo, receivedAnswer, receivedAnswerPar, NULL);\r | |
242 | if (len == 1) {\r | |
243 | if (MF_DBGLEVEL >= 1) Dbprintf("Cmd Error: %02x", receivedAnswer[0]); \r | |
244 | return 1;\r | |
245 | }\r | |
246 | if (len != 18) {\r | |
247 | if (MF_DBGLEVEL >= 1) Dbprintf("Cmd Error: card timeout. len: %x", len); \r | |
248 | return 2;\r | |
249 | }\r | |
250 | \r | |
251 | memcpy(bt, receivedAnswer + 16, 2);\r | |
252 | AppendCrc14443a(receivedAnswer, 16);\r | |
253 | if (bt[0] != receivedAnswer[16] || bt[1] != receivedAnswer[17]) {\r | |
254 | if (MF_DBGLEVEL >= 1) Dbprintf("Cmd CRC response error."); \r | |
255 | return 3;\r | |
256 | }\r | |
257 | \r | |
258 | memcpy(blockData, receivedAnswer, 16);\r | |
259 | return 0;\r | |
260 | }\r | |
261 | \r | |
262 | // mifare ultralight commands\r | |
263 | int mifare_ul_ev1_auth(uint8_t *keybytes, uint8_t *pack){\r | |
264 | \r | |
265 | uint16_t len;\r | |
266 | uint8_t resp[4];\r | |
267 | uint8_t respPar[1];\r | |
268 | uint8_t key[4] = {0x00};\r | |
269 | memcpy(key, keybytes, 4);\r | |
270 | \r | |
271 | if (MF_DBGLEVEL >= MF_DBG_EXTENDED)\r | |
272 | Dbprintf("EV1 Auth : %02x%02x%02x%02x", key[0], key[1], key[2], key[3]);\r | |
273 | len = mifare_sendcmd(0x1B, key, sizeof(key), resp, respPar, NULL);\r | |
274 | \r | |
275 | if (len != 4) {\r | |
276 | if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Cmd Error: %02x %u", resp[0], len);\r | |
277 | return 0;\r | |
278 | }\r | |
279 | \r | |
280 | if (MF_DBGLEVEL >= MF_DBG_EXTENDED)\r | |
281 | Dbprintf("Auth Resp: %02x%02x%02x%02x", resp[0],resp[1],resp[2],resp[3]);\r | |
282 | \r | |
283 | memcpy(pack, resp, 4);\r | |
284 | return 1;\r | |
285 | }\r | |
286 | \r | |
287 | int mifare_ultra_auth(uint8_t *keybytes){\r | |
288 | \r | |
289 | /// 3des2k\r | |
290 | \r | |
291 | uint8_t random_a[8] = {1,1,1,1,1,1,1,1};\r | |
292 | uint8_t random_b[8] = {0x00};\r | |
293 | uint8_t enc_random_b[8] = {0x00};\r | |
294 | uint8_t rnd_ab[16] = {0x00};\r | |
295 | uint8_t IV[8] = {0x00};\r | |
296 | uint8_t key[16] = {0x00};\r | |
297 | memcpy(key, keybytes, 16);\r | |
298 | \r | |
299 | uint16_t len;\r | |
300 | uint8_t resp[19] = {0x00};\r | |
301 | uint8_t respPar[3] = {0,0,0};\r | |
302 | \r | |
303 | // REQUEST AUTHENTICATION\r | |
304 | len = mifare_sendcmd_short(NULL, 1, 0x1A, 0x00, resp, respPar ,NULL);\r | |
305 | if (len != 11) {\r | |
306 | if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Cmd Error: %02x", resp[0]);\r | |
307 | return 0;\r | |
308 | }\r | |
309 | \r | |
310 | // tag nonce.\r | |
311 | memcpy(enc_random_b,resp+1,8);\r | |
312 | \r | |
313 | // decrypt nonce.\r | |
314 | tdes_2key_dec(random_b, enc_random_b, sizeof(random_b), key, IV );\r | |
315 | rol(random_b,8);\r | |
316 | memcpy(rnd_ab ,random_a,8);\r | |
317 | memcpy(rnd_ab+8,random_b,8);\r | |
318 | \r | |
319 | if (MF_DBGLEVEL >= MF_DBG_EXTENDED) {\r | |
320 | Dbprintf("enc_B: %02x %02x %02x %02x %02x %02x %02x %02x",\r | |
321 | enc_random_b[0],enc_random_b[1],enc_random_b[2],enc_random_b[3],enc_random_b[4],enc_random_b[5],enc_random_b[6],enc_random_b[7]);\r | |
322 | \r | |
323 | Dbprintf(" B: %02x %02x %02x %02x %02x %02x %02x %02x",\r | |
324 | random_b[0],random_b[1],random_b[2],random_b[3],random_b[4],random_b[5],random_b[6],random_b[7]);\r | |
325 | \r | |
326 | Dbprintf("rnd_ab: %02x %02x %02x %02x %02x %02x %02x %02x",\r | |
327 | rnd_ab[0],rnd_ab[1],rnd_ab[2],rnd_ab[3],rnd_ab[4],rnd_ab[5],rnd_ab[6],rnd_ab[7]);\r | |
328 | \r | |
329 | Dbprintf("rnd_ab: %02x %02x %02x %02x %02x %02x %02x %02x",\r | |
330 | rnd_ab[8],rnd_ab[9],rnd_ab[10],rnd_ab[11],rnd_ab[12],rnd_ab[13],rnd_ab[14],rnd_ab[15] );\r | |
331 | }\r | |
332 | \r | |
333 | // encrypt out, in, length, key, iv\r | |
334 | tdes_2key_enc(rnd_ab, rnd_ab, sizeof(rnd_ab), key, enc_random_b);\r | |
335 | //len = mifare_sendcmd_short_mfucauth(NULL, 1, 0xAF, rnd_ab, resp, respPar, NULL);\r | |
336 | len = mifare_sendcmd(0xAF, rnd_ab, sizeof(rnd_ab), resp, respPar, NULL);\r | |
337 | if (len != 11) {\r | |
338 | if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Cmd Error: %02x", resp[0]);\r | |
339 | return 0;\r | |
340 | }\r | |
341 | \r | |
342 | uint8_t enc_resp[8] = { 0,0,0,0,0,0,0,0 };\r | |
343 | uint8_t resp_random_a[8] = { 0,0,0,0,0,0,0,0 };\r | |
344 | memcpy(enc_resp, resp+1, 8);\r | |
345 | \r | |
346 | // decrypt out, in, length, key, iv \r | |
347 | tdes_2key_dec(resp_random_a, enc_resp, 8, key, enc_random_b);\r | |
348 | if ( memcmp(resp_random_a, random_a, 8) != 0 ) {\r | |
349 | if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("failed authentication");\r | |
350 | return 0;\r | |
351 | }\r | |
352 | \r | |
353 | if (MF_DBGLEVEL >= MF_DBG_EXTENDED) {\r | |
354 | Dbprintf("e_AB: %02x %02x %02x %02x %02x %02x %02x %02x", \r | |
355 | rnd_ab[0],rnd_ab[1],rnd_ab[2],rnd_ab[3],\r | |
356 | rnd_ab[4],rnd_ab[5],rnd_ab[6],rnd_ab[7]);\r | |
357 | \r | |
358 | Dbprintf("e_AB: %02x %02x %02x %02x %02x %02x %02x %02x",\r | |
359 | rnd_ab[8],rnd_ab[9],rnd_ab[10],rnd_ab[11],\r | |
360 | rnd_ab[12],rnd_ab[13],rnd_ab[14],rnd_ab[15]);\r | |
361 | \r | |
362 | Dbprintf("a: %02x %02x %02x %02x %02x %02x %02x %02x",\r | |
363 | random_a[0],random_a[1],random_a[2],random_a[3],\r | |
364 | random_a[4],random_a[5],random_a[6],random_a[7]);\r | |
365 | \r | |
366 | Dbprintf("b: %02x %02x %02x %02x %02x %02x %02x %02x",\r | |
367 | resp_random_a[0],resp_random_a[1],resp_random_a[2],resp_random_a[3],\r | |
368 | resp_random_a[4],resp_random_a[5],resp_random_a[6],resp_random_a[7]);\r | |
369 | }\r | |
370 | return 1;\r | |
371 | }\r | |
372 | \r | |
373 | int mifare_ultra_readblock(uint8_t blockNo, uint8_t *blockData)\r | |
374 | {\r | |
375 | uint16_t len;\r | |
376 | uint8_t bt[2];\r | |
377 | uint8_t receivedAnswer[MAX_FRAME_SIZE];\r | |
378 | uint8_t receivedAnswerPar[MAX_PARITY_SIZE];\r | |
379 | \r | |
380 | \r | |
381 | len = mifare_sendcmd_short(NULL, 1, 0x30, blockNo, receivedAnswer, receivedAnswerPar, NULL);\r | |
382 | if (len == 1) {\r | |
383 | if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Cmd Error: %02x", receivedAnswer[0]);\r | |
384 | return 1;\r | |
385 | }\r | |
386 | if (len != 18) {\r | |
387 | if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Cmd Error: card timeout. len: %x", len);\r | |
388 | return 2;\r | |
389 | }\r | |
390 | \r | |
391 | memcpy(bt, receivedAnswer + 16, 2);\r | |
392 | AppendCrc14443a(receivedAnswer, 16);\r | |
393 | if (bt[0] != receivedAnswer[16] || bt[1] != receivedAnswer[17]) {\r | |
394 | if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Cmd CRC response error.");\r | |
395 | return 3;\r | |
396 | }\r | |
397 | \r | |
398 | memcpy(blockData, receivedAnswer, 14);\r | |
399 | return 0;\r | |
400 | }\r | |
401 | \r | |
402 | int mifare_classic_writeblock(struct Crypto1State *pcs, uint32_t uid, uint8_t blockNo, uint8_t *blockData) \r | |
403 | {\r | |
404 | // variables\r | |
405 | uint16_t len, i; \r | |
406 | uint32_t pos = 0;\r | |
407 | uint8_t par[3] = {0x00}; // enough for 18 Bytes to send\r | |
408 | byte_t res = 0;\r | |
409 | \r | |
410 | uint8_t d_block[18], d_block_enc[18];\r | |
411 | uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];\r | |
412 | uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE];\r | |
413 | \r | |
414 | // command MIFARE_CLASSIC_WRITEBLOCK\r | |
415 | len = mifare_sendcmd_short(pcs, 1, 0xA0, blockNo, receivedAnswer, receivedAnswerPar, NULL);\r | |
416 | \r | |
417 | if ((len != 1) || (receivedAnswer[0] != 0x0A)) { // 0x0a - ACK\r | |
418 | if (MF_DBGLEVEL >= 1) Dbprintf("Cmd Error: %02x", receivedAnswer[0]); \r | |
419 | return 1;\r | |
420 | }\r | |
421 | \r | |
422 | memcpy(d_block, blockData, 16);\r | |
423 | AppendCrc14443a(d_block, 16);\r | |
424 | \r | |
425 | // crypto\r | |
426 | for (pos = 0; pos < 18; pos++)\r | |
427 | {\r | |
428 | d_block_enc[pos] = crypto1_byte(pcs, 0x00, 0) ^ d_block[pos];\r | |
429 | par[pos>>3] |= (((filter(pcs->odd) ^ oddparity8(d_block[pos])) & 0x01) << (7 - (pos&0x0007)));\r | |
430 | } \r | |
431 | \r | |
432 | ReaderTransmitPar(d_block_enc, sizeof(d_block_enc), par, NULL);\r | |
433 | \r | |
434 | // Receive the response\r | |
435 | len = ReaderReceive(receivedAnswer, receivedAnswerPar); \r | |
436 | \r | |
437 | res = 0;\r | |
438 | for (i = 0; i < 4; i++)\r | |
439 | res |= (crypto1_bit(pcs, 0, 0) ^ BIT(receivedAnswer[0], i)) << i;\r | |
440 | \r | |
441 | if ((len != 1) || (res != 0x0A)) {\r | |
442 | if (MF_DBGLEVEL >= 1) Dbprintf("Cmd send data2 Error: %02x", res); \r | |
443 | return 2;\r | |
444 | }\r | |
445 | \r | |
446 | return 0;\r | |
447 | }\r | |
448 | \r | |
449 | /* // command not needed, but left for future testing\r | |
450 | int mifare_ultra_writeblock_compat(uint8_t blockNo, uint8_t *blockData) \r | |
451 | {\r | |
452 | uint16_t len;\r | |
453 | uint8_t par[3] = {0}; // enough for 18 parity bits\r | |
454 | uint8_t d_block[18] = {0x00};\r | |
455 | uint8_t receivedAnswer[MAX_FRAME_SIZE];\r | |
456 | uint8_t receivedAnswerPar[MAX_PARITY_SIZE];\r | |
457 | \r | |
458 | len = mifare_sendcmd_short(NULL, true, 0xA0, blockNo, receivedAnswer, receivedAnswerPar, NULL);\r | |
459 | \r | |
460 | if ((len != 1) || (receivedAnswer[0] != 0x0A)) { // 0x0a - ACK\r | |
461 | if (MF_DBGLEVEL >= MF_DBG_ERROR)\r | |
462 | Dbprintf("Cmd Addr Error: %02x", receivedAnswer[0]);\r | |
463 | return 1;\r | |
464 | }\r | |
465 | \r | |
466 | memcpy(d_block, blockData, 16);\r | |
467 | AppendCrc14443a(d_block, 16);\r | |
468 | \r | |
469 | ReaderTransmitPar(d_block, sizeof(d_block), par, NULL);\r | |
470 | \r | |
471 | len = ReaderReceive(receivedAnswer, receivedAnswerPar);\r | |
472 | \r | |
473 | if ((len != 1) || (receivedAnswer[0] != 0x0A)) { // 0x0a - ACK\r | |
474 | if (MF_DBGLEVEL >= MF_DBG_ERROR)\r | |
475 | Dbprintf("Cmd Data Error: %02x %d", receivedAnswer[0],len);\r | |
476 | return 2;\r | |
477 | }\r | |
478 | return 0;\r | |
479 | }\r | |
480 | */\r | |
481 | \r | |
482 | int mifare_ultra_writeblock(uint8_t blockNo, uint8_t *blockData)\r | |
483 | {\r | |
484 | uint16_t len;\r | |
485 | uint8_t d_block[5] = {0x00};\r | |
486 | uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];\r | |
487 | uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE];\r | |
488 | \r | |
489 | // command MIFARE_CLASSIC_WRITEBLOCK\r | |
490 | d_block[0]= blockNo;\r | |
491 | memcpy(d_block+1,blockData,4);\r | |
492 | //AppendCrc14443a(d_block, 6);\r | |
493 | \r | |
494 | len = mifare_sendcmd(0xA2, d_block, sizeof(d_block), receivedAnswer, receivedAnswerPar, NULL);\r | |
495 | \r | |
496 | if (receivedAnswer[0] != 0x0A) { // 0x0a - ACK\r | |
497 | if (MF_DBGLEVEL >= MF_DBG_ERROR)\r | |
498 | Dbprintf("Cmd Send Error: %02x %d", receivedAnswer[0],len);\r | |
499 | return 1;\r | |
500 | }\r | |
501 | return 0;\r | |
502 | }\r | |
503 | int mifare_classic_halt_ex(struct Crypto1State *pcs) {\r | |
504 | uint16_t len; \r | |
505 | uint8_t receivedAnswer[4];\r | |
506 | uint8_t receivedAnswerPar[4];\r | |
507 | \r | |
508 | len = mifare_sendcmd_short(pcs, pcs == NULL ? false:true, 0x50, 0x00, receivedAnswer, receivedAnswerPar, NULL);\r | |
509 | if (len != 0) {\r | |
510 | if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("halt error. response len: %x", len); \r | |
511 | return 1;\r | |
512 | }\r | |
513 | return 0;\r | |
514 | }\r | |
515 | int mifare_classic_halt(struct Crypto1State *pcs, uint32_t uid) {\r | |
516 | return mifare_classic_halt_ex(pcs);\r | |
517 | }\r | |
518 | \r | |
519 | int mifare_ultra_halt()\r | |
520 | {\r | |
521 | uint16_t len;\r | |
522 | uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];\r | |
523 | uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE];\r | |
524 | \r | |
525 | len = mifare_sendcmd_short(NULL, true, 0x50, 0x00, receivedAnswer, receivedAnswerPar, NULL);\r | |
526 | if (len != 0) {\r | |
527 | if (MF_DBGLEVEL >= MF_DBG_ERROR)\r | |
528 | Dbprintf("halt error. response len: %x", len);\r | |
529 | return 1;\r | |
530 | }\r | |
531 | return 0;\r | |
532 | }\r | |
533 | \r | |
534 | \r | |
535 | // Mifare Memory Structure: up to 32 Sectors with 4 blocks each (1k and 2k cards),\r | |
536 | // plus evtl. 8 sectors with 16 blocks each (4k cards)\r | |
537 | uint8_t NumBlocksPerSector(uint8_t sectorNo) \r | |
538 | {\r | |
539 | if (sectorNo < 32) \r | |
540 | return 4;\r | |
541 | else\r | |
542 | return 16;\r | |
543 | }\r | |
544 | \r | |
545 | uint8_t FirstBlockOfSector(uint8_t sectorNo) \r | |
546 | {\r | |
547 | if (sectorNo < 32)\r | |
548 | return sectorNo * 4;\r | |
549 | else\r | |
550 | return 32*4 + (sectorNo - 32) * 16;\r | |
551 | \r | |
552 | }\r | |
553 | \r | |
554 | \r | |
555 | // work with emulator memory\r | |
556 | void emlSetMem(uint8_t *data, int blockNum, int blocksCount) {\r | |
557 | emlSetMem_xt(data, blockNum, blocksCount, 16);\r | |
558 | }\r | |
559 | \r | |
560 | void emlSetMem_xt(uint8_t *data, int blockNum, int blocksCount, int blockBtWidth) {\r | |
561 | uint8_t* emCARD = BigBuf_get_EM_addr();\r | |
562 | memcpy(emCARD + blockNum * blockBtWidth, data, blocksCount * blockBtWidth);\r | |
563 | }\r | |
564 | \r | |
565 | void emlGetMem(uint8_t *data, int blockNum, int blocksCount) {\r | |
566 | uint8_t* emCARD = BigBuf_get_EM_addr();\r | |
567 | memcpy(data, emCARD + blockNum * 16, blocksCount * 16);\r | |
568 | }\r | |
569 | \r | |
570 | void emlGetMemBt(uint8_t *data, int bytePtr, int byteCount) {\r | |
571 | uint8_t* emCARD = BigBuf_get_EM_addr();\r | |
572 | memcpy(data, emCARD + bytePtr, byteCount);\r | |
573 | }\r | |
574 | \r | |
575 | int emlCheckValBl(int blockNum) {\r | |
576 | uint8_t* emCARD = BigBuf_get_EM_addr();\r | |
577 | uint8_t* data = emCARD + blockNum * 16;\r | |
578 | \r | |
579 | if ((data[0] != (data[4] ^ 0xff)) || (data[0] != data[8]) ||\r | |
580 | (data[1] != (data[5] ^ 0xff)) || (data[1] != data[9]) ||\r | |
581 | (data[2] != (data[6] ^ 0xff)) || (data[2] != data[10]) ||\r | |
582 | (data[3] != (data[7] ^ 0xff)) || (data[3] != data[11]) ||\r | |
583 | (data[12] != (data[13] ^ 0xff)) || (data[12] != data[14]) ||\r | |
584 | (data[12] != (data[15] ^ 0xff))\r | |
585 | ) \r | |
586 | return 1;\r | |
587 | return 0;\r | |
588 | }\r | |
589 | \r | |
590 | int emlGetValBl(uint32_t *blReg, uint8_t *blBlock, int blockNum) {\r | |
591 | uint8_t* emCARD = BigBuf_get_EM_addr();\r | |
592 | uint8_t* data = emCARD + blockNum * 16;\r | |
593 | \r | |
594 | if (emlCheckValBl(blockNum)) {\r | |
595 | return 1;\r | |
596 | }\r | |
597 | \r | |
598 | memcpy(blReg, data, 4);\r | |
599 | *blBlock = data[12];\r | |
600 | return 0;\r | |
601 | }\r | |
602 | \r | |
603 | int emlSetValBl(uint32_t blReg, uint8_t blBlock, int blockNum) {\r | |
604 | uint8_t* emCARD = BigBuf_get_EM_addr();\r | |
605 | uint8_t* data = emCARD + blockNum * 16;\r | |
606 | \r | |
607 | memcpy(data + 0, &blReg, 4);\r | |
608 | memcpy(data + 8, &blReg, 4);\r | |
609 | blReg = blReg ^ 0xffffffff;\r | |
610 | memcpy(data + 4, &blReg, 4);\r | |
611 | \r | |
612 | data[12] = blBlock;\r | |
613 | data[13] = blBlock ^ 0xff;\r | |
614 | data[14] = blBlock;\r | |
615 | data[15] = blBlock ^ 0xff;\r | |
616 | \r | |
617 | return 0;\r | |
618 | }\r | |
619 | \r | |
620 | uint64_t emlGetKey(int sectorNum, int keyType) {\r | |
621 | uint8_t key[6];\r | |
622 | uint8_t* emCARD = BigBuf_get_EM_addr();\r | |
623 | \r | |
624 | memcpy(key, emCARD + 16 * (FirstBlockOfSector(sectorNum) + NumBlocksPerSector(sectorNum) - 1) + keyType * 10, 6);\r | |
625 | return bytes_to_num(key, 6);\r | |
626 | }\r | |
627 | \r | |
628 | void emlClearMem(void) {\r | |
629 | int b;\r | |
630 | \r | |
631 | const uint8_t trailer[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x07, 0x80, 0x69, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};\r | |
632 | const uint8_t uid[] = {0xe6, 0x84, 0x87, 0xf3, 0x16, 0x88, 0x04, 0x00, 0x46, 0x8e, 0x45, 0x55, 0x4d, 0x70, 0x41, 0x04};\r | |
633 | uint8_t* emCARD = BigBuf_get_EM_addr();\r | |
634 | \r | |
635 | memset(emCARD, 0, CARD_MEMORY_SIZE);\r | |
636 | \r | |
637 | // fill sectors trailer data\r | |
638 | for(b = 3; b < 256; b<127?(b+=4):(b+=16)) {\r | |
639 | emlSetMem((uint8_t *)trailer, b , 1);\r | |
640 | } \r | |
641 | \r | |
642 | // uid\r | |
643 | emlSetMem((uint8_t *)uid, 0, 1);\r | |
644 | return;\r | |
645 | }\r | |
646 | \r | |
647 | \r | |
648 | // Mifare desfire commands\r | |
649 | int mifare_sendcmd_special(struct Crypto1State *pcs, uint8_t crypted, uint8_t cmd, uint8_t* data, uint8_t* answer, uint8_t *answer_parity, uint32_t *timing)\r | |
650 | {\r | |
651 | uint8_t dcmd[5] = {0x00};\r | |
652 | dcmd[0] = cmd;\r | |
653 | memcpy(dcmd+1,data,2);\r | |
654 | AppendCrc14443a(dcmd, 3);\r | |
655 | \r | |
656 | ReaderTransmit(dcmd, sizeof(dcmd), NULL);\r | |
657 | int len = ReaderReceive(answer, answer_parity);\r | |
658 | if(!len) {\r | |
659 | if (MF_DBGLEVEL >= MF_DBG_ERROR) \r | |
660 | Dbprintf("Authentication failed. Card timeout.");\r | |
661 | return 1;\r | |
662 | }\r | |
663 | return len;\r | |
664 | }\r | |
665 | \r | |
666 | int mifare_sendcmd_special2(struct Crypto1State *pcs, uint8_t crypted, uint8_t cmd, uint8_t* data, uint8_t* answer,uint8_t *answer_parity, uint32_t *timing)\r | |
667 | {\r | |
668 | uint8_t dcmd[20] = {0x00};\r | |
669 | dcmd[0] = cmd;\r | |
670 | memcpy(dcmd+1,data,17);\r | |
671 | AppendCrc14443a(dcmd, 18);\r | |
672 | \r | |
673 | ReaderTransmit(dcmd, sizeof(dcmd), NULL);\r | |
674 | int len = ReaderReceive(answer, answer_parity);\r | |
675 | if(!len){\r | |
676 | if (MF_DBGLEVEL >= MF_DBG_ERROR)\r | |
677 | Dbprintf("Authentication failed. Card timeout.");\r | |
678 | return 1;\r | |
679 | }\r | |
680 | return len;\r | |
681 | }\r | |
682 | \r | |
683 | int mifare_desfire_des_auth1(uint32_t uid, uint8_t *blockData){\r | |
684 | \r | |
685 | int len;\r | |
686 | // load key, keynumber\r | |
687 | uint8_t data[2]={0x0a, 0x00};\r | |
688 | uint8_t receivedAnswer[MAX_FRAME_SIZE];\r | |
689 | uint8_t receivedAnswerPar[MAX_PARITY_SIZE];\r | |
690 | \r | |
691 | len = mifare_sendcmd_special(NULL, 1, 0x02, data, receivedAnswer,receivedAnswerPar,NULL);\r | |
692 | if (len == 1) {\r | |
693 | if (MF_DBGLEVEL >= MF_DBG_ERROR)\r | |
694 | Dbprintf("Cmd Error: %02x", receivedAnswer[0]);\r | |
695 | return 1;\r | |
696 | }\r | |
697 | \r | |
698 | if (len == 12) {\r | |
699 | if (MF_DBGLEVEL >= MF_DBG_EXTENDED) {\r | |
700 | Dbprintf("Auth1 Resp: %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",\r | |
701 | receivedAnswer[0],receivedAnswer[1],receivedAnswer[2],receivedAnswer[3],receivedAnswer[4],\r | |
702 | receivedAnswer[5],receivedAnswer[6],receivedAnswer[7],receivedAnswer[8],receivedAnswer[9],\r | |
703 | receivedAnswer[10],receivedAnswer[11]);\r | |
704 | }\r | |
705 | memcpy(blockData, receivedAnswer, 12);\r | |
706 | return 0;\r | |
707 | }\r | |
708 | return 1;\r | |
709 | }\r | |
710 | \r | |
711 | int mifare_desfire_des_auth2(uint32_t uid, uint8_t *key, uint8_t *blockData){\r | |
712 | \r | |
713 | int len;\r | |
714 | uint8_t data[17] = {0x00};\r | |
715 | data[0] = 0xAF;\r | |
716 | memcpy(data+1,key,16);\r | |
717 | \r | |
718 | uint8_t receivedAnswer[MAX_FRAME_SIZE];\r | |
719 | uint8_t receivedAnswerPar[MAX_PARITY_SIZE];\r | |
720 | \r | |
721 | len = mifare_sendcmd_special2(NULL, 1, 0x03, data, receivedAnswer, receivedAnswerPar ,NULL);\r | |
722 | \r | |
723 | if ((receivedAnswer[0] == 0x03) && (receivedAnswer[1] == 0xae)) {\r | |
724 | if (MF_DBGLEVEL >= MF_DBG_ERROR)\r | |
725 | Dbprintf("Auth Error: %02x %02x", receivedAnswer[0], receivedAnswer[1]);\r | |
726 | return 1;\r | |
727 | }\r | |
728 | \r | |
729 | if (len == 12){\r | |
730 | if (MF_DBGLEVEL >= MF_DBG_EXTENDED) {\r | |
731 | Dbprintf("Auth2 Resp: %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",\r | |
732 | receivedAnswer[0],receivedAnswer[1],receivedAnswer[2],receivedAnswer[3],receivedAnswer[4],\r | |
733 | receivedAnswer[5],receivedAnswer[6],receivedAnswer[7],receivedAnswer[8],receivedAnswer[9],\r | |
734 | receivedAnswer[10],receivedAnswer[11]);\r | |
735 | }\r | |
736 | memcpy(blockData, receivedAnswer, 12);\r | |
737 | return 0;\r | |
738 | }\r | |
739 | return 1;\r | |
740 | }\r |