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