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