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