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