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