]> git.zerfleddert.de Git - proxmark3-svn/blame - armsrc/mifareutil.c
CHG: moved into header files.
[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 10//-----------------------------------------------------------------------------\r
20f9a2a1
M
11#include "mifareutil.h"\r
12\r
3de04590 13int MF_DBGLEVEL = MF_DBG_ALL;\r
f397b5cc 14\r
8f51ddb0
M
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
d798d31c 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
8f51ddb0
M
28 data[0] = bt;\r
29 }\r
30 return;\r
31}\r
32\r
a501c82b 33void mf_crypto1_encrypt(struct Crypto1State *pcs, uint8_t *data, uint16_t len, uint8_t *par) {\r
8f51ddb0
M
34 uint8_t bt = 0;\r
35 int i;\r
a501c82b 36 par[0] = 0;\r
37 \r
8f51ddb0
M
38 for (i = 0; i < len; i++) {\r
39 bt = data[i];\r
40 data[i] = crypto1_byte(pcs, 0x00, 0) ^ data[i];\r
2abdfa49 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
8f51ddb0 44 } \r
8f51ddb0
M
45}\r
46\r
47uint8_t mf_crypto1_encrypt4bit(struct Crypto1State *pcs, uint8_t data) {\r
48 uint8_t bt = 0;\r
d798d31c 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
8f51ddb0 53 return bt;\r
20f9a2a1
M
54}\r
55\r
95aeb706 56// send X byte basic commands\r
2abdfa49 57int mifare_sendcmd(uint8_t cmd, uint8_t* data, uint8_t data_size, uint8_t* answer, uint8_t *answer_parity, uint32_t *timing) {\r
95aeb706 58 uint8_t dcmd[data_size+3];\r
64d1b4ef 59 dcmd[0] = cmd;\r
2abdfa49 60 memcpy(dcmd+1, data, data_size);\r
95aeb706 61 AppendCrc14443a(dcmd, data_size+1);\r
62 ReaderTransmit(dcmd, sizeof(dcmd), timing);\r
64d1b4ef 63 int len = ReaderReceive(answer, answer_parity);\r
64 if(!len) {\r
2abdfa49 65 if (MF_DBGLEVEL >= MF_DBG_ERROR)\r
66 Dbprintf("%02X Cmd failed. Card timeout.", cmd);\r
67 len = ReaderReceive(answer,answer_parity);\r
f38a1528 68 }\r
69 return len;\r
70}\r
71\r
95aeb706 72// send 2 byte commands\r
2abdfa49 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
a501c82b 74 uint16_t pos, res;\r
2abdfa49 75 uint8_t dcmd[4] = {cmd, data, 0x00, 0x00};\r
76 uint8_t ecmd[4] = {0x00, 0x00, 0x00, 0x00};\r
d798d31c 77 uint8_t par[1] = {0x00}; // 1 Byte parity is enough here\r
20f9a2a1 78 AppendCrc14443a(dcmd, 2);\r
20f9a2a1
M
79 memcpy(ecmd, dcmd, sizeof(dcmd));\r
80 \r
81 if (crypted) {\r
a501c82b 82 par[0] = 0;\r
d798d31c 83 for (pos = 0; pos < 4; pos++) {\r
20f9a2a1 84 ecmd[pos] = crypto1_byte(pcs, 0x00, 0) ^ dcmd[pos];\r
f8ada309 85 par[0] |= (((filter(pcs->odd) ^ oddparity8(dcmd[pos])) & 0x01) << (7-pos));\r
20f9a2a1 86 } \r
9492e0b0 87 ReaderTransmitPar(ecmd, sizeof(ecmd), par, timing);\r
20f9a2a1 88 } else {\r
9492e0b0 89 ReaderTransmit(dcmd, sizeof(dcmd), timing);\r
20f9a2a1
M
90 }\r
91\r
a501c82b 92 int len = ReaderReceive(answer, par);\r
f89c7050 93 \r
a501c82b 94 if (answer_parity) *answer_parity = par[0];\r
f89c7050 95 \r
4abe4f58 96 if (crypted == CRYPT_ALL) {\r
20f9a2a1
M
97 if (len == 1) {\r
98 res = 0;\r
d798d31c 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
2abdfa49 103 answer[0] = res; \r
20f9a2a1
M
104 } else {\r
105 for (pos = 0; pos < len; pos++)\r
20f9a2a1 106 answer[pos] = crypto1_byte(pcs, 0x00, 0) ^ answer[pos];\r
20f9a2a1
M
107 }\r
108 }\r
20f9a2a1
M
109 return len;\r
110}\r
111\r
225ccb91 112// mifare classic commands\r
2abdfa49 113int mifare_classic_auth(struct Crypto1State *pcs, uint32_t uid, uint8_t blockNo, uint8_t keyType, uint64_t ui64Key, uint8_t isNested) {\r
9492e0b0 114 return mifare_classic_authex(pcs, uid, blockNo, keyType, ui64Key, isNested, NULL, NULL);\r
f89c7050
M
115}\r
116\r
2abdfa49 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
4abe4f58 118 int len; \r
20f9a2a1 119 uint32_t pos;\r
a501c82b 120 uint8_t par[1] = {0x00};\r
7d5169a0 121\r
122 // "random" reader nonce:\r
838c15a6 123 //byte_t nr[4] = {0x55, 0x41, 0x49, 0x92};\r
124 byte_t nr[4] = {0x01, 0x01, 0x01, 0x01};\r
7d5169a0 125 \r
20f9a2a1
M
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
bd9dfaa8 129 uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE] = {0x00};\r
130 uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE] = {0x00};\r
6a1f2d82 131 \r
4abe4f58 132 // Transmit MIFARE_CLASSIC_AUTH\r
a501c82b 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
4abe4f58 135 if (len != 4) return 1;\r
20f9a2a1 136 \r
20f9a2a1
M
137 // Save the tag nonce (nt)\r
138 nt = bytes_to_num(receivedAnswer, 4);\r
20f9a2a1
M
139\r
140 // ----------------------------- crypto1 create\r
4abe4f58
M
141 if (isNested)\r
142 crypto1_destroy(pcs);\r
143\r
144 // Init cipher with key\r
20f9a2a1
M
145 crypto1_create(pcs, ui64Key);\r
146\r
4abe4f58
M
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
f397b5cc 156 if (!ntptr && (MF_DBGLEVEL >= 3))\r
f89c7050
M
157 Dbprintf("auth uid: %08x nt: %08x", uid, nt); \r
158 \r
159 // save Nt\r
160 if (ntptr)\r
161 *ntptr = nt;\r
20f9a2a1 162\r
4abe4f58 163 // Generate (encrypted) nr+parity by loading it into the cipher (Nr)\r
a501c82b 164 par[0] = 0;\r
bd9dfaa8 165 for (pos = 0; pos < 4; pos++) {\r
a501c82b 166 mf_nr_ar[pos] = crypto1_byte(pcs, nr[pos], 0) ^ nr[pos];\r
f8ada309 167 par[0] |= (((filter(pcs->odd) ^ oddparity8(nr[pos])) & 0x01) << (7-pos));\r
4abe4f58 168 } \r
20f9a2a1 169 \r
4abe4f58 170 // Skip 32 bits in pseudo random generator\r
46f983fe 171 nt = prng_successor(nt, 32);\r
20f9a2a1
M
172\r
173 // ar+parity\r
bd9dfaa8 174 for (pos = 4; pos < 8; pos++) {\r
20f9a2a1 175 nt = prng_successor(nt,8);\r
4abe4f58 176 mf_nr_ar[pos] = crypto1_byte(pcs,0x00,0) ^ (nt & 0xff);\r
f8ada309 177 par[0] |= (((filter(pcs->odd) ^ oddparity8(nt & 0xff)) & 0x01) << (7-pos));\r
4abe4f58 178 } \r
20f9a2a1 179 \r
4abe4f58 180 // Transmit reader nonce and reader answer\r
9492e0b0 181 ReaderTransmitPar(mf_nr_ar, sizeof(mf_nr_ar), par, NULL);\r
20f9a2a1 182\r
a501c82b 183 // Receive 4 byte tag answer\r
184 len = ReaderReceive(receivedAnswer, receivedAnswerPar);\r
bd9dfaa8 185 if (!len) {\r
f397b5cc 186 if (MF_DBGLEVEL >= 1) Dbprintf("Authentication failed. Card timeout.");\r
20f9a2a1 187 return 2;\r
4abe4f58 188 }\r
2abdfa49 189\r
20f9a2a1 190 ntpp = prng_successor(nt, 32) ^ crypto1_word(pcs, 0,0);\r
838c15a6 191\r
192 if (ntpp != bytes_to_num(receivedAnswer, 4)) {\r
f397b5cc 193 if (MF_DBGLEVEL >= 1) Dbprintf("Authentication failed. Error card response.");\r
20f9a2a1
M
194 return 3;\r
195 }\r
20f9a2a1
M
196 return 0;\r
197}\r
198\r
2abdfa49 199int mifare_classic_readblock(struct Crypto1State *pcs, uint32_t uid, uint8_t blockNo, uint8_t *blockData) {\r
200\r
4abe4f58 201 int len; \r
2abdfa49 202 uint8_t bt[2] = {0x00, 0x00}; \r
d798d31c 203 uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE] = {0x00};\r
204 uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE] = {0x00};\r
20f9a2a1 205 \r
2abdfa49 206 len = mifare_sendcmd_short(pcs, 1, ISO14443A_CMD_READBLOCK, blockNo, receivedAnswer, receivedAnswerPar, NULL);\r
20f9a2a1 207 if (len == 1) {\r
f397b5cc 208 if (MF_DBGLEVEL >= 1) Dbprintf("Cmd Error: %02x", receivedAnswer[0]); \r
20f9a2a1
M
209 return 1;\r
210 }\r
211 if (len != 18) {\r
f397b5cc 212 if (MF_DBGLEVEL >= 1) Dbprintf("Cmd Error: card timeout. len: %x", len); \r
20f9a2a1
M
213 return 2;\r
214 }\r
215\r
216 memcpy(bt, receivedAnswer + 16, 2);\r
4abe4f58 217 AppendCrc14443a(receivedAnswer, 16);\r
20f9a2a1 218 if (bt[0] != receivedAnswer[16] || bt[1] != receivedAnswer[17]) {\r
f397b5cc 219 if (MF_DBGLEVEL >= 1) Dbprintf("Cmd CRC response error."); \r
20f9a2a1
M
220 return 3;\r
221 }\r
222 \r
223 memcpy(blockData, receivedAnswer, 16);\r
64d1b4ef 224 return 0;\r
f38a1528 225}\r
226\r
225ccb91 227// mifare ultralight commands\r
4d2e4eea 228int mifare_ul_ev1_auth(uint8_t *keybytes, uint8_t *pack){\r
225ccb91 229\r
2abdfa49 230 uint16_t len = 0;\r
231 uint8_t resp[4] = {0x00, 0x00, 0x00, 0x00};\r
d798d31c 232 uint8_t respPar[1] = {0x00};\r
2abdfa49 233 uint8_t key[4] = {0x00, 0x00, 0x00, 0x00};\r
4d2e4eea 234 memcpy(key, keybytes, 4);\r
8258f409 235\r
95aeb706 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
d798d31c 238 \r
95aeb706 239 len = mifare_sendcmd(0x1B, key, sizeof(key), resp, respPar, NULL);\r
0de8e387 240\r
4d2e4eea 241 if (len != 4) {\r
242 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Cmd Error: %02x %u", resp[0], len);\r
e7e95088 243 return 0;\r
f38a1528 244 }\r
8258f409 245\r
4d2e4eea 246 if (MF_DBGLEVEL >= MF_DBG_EXTENDED)\r
8258f409 247 Dbprintf("Auth Resp: %02x%02x%02x%02x", resp[0],resp[1],resp[2],resp[3]);\r
248\r
4d2e4eea 249 memcpy(pack, resp, 4);\r
e7e95088 250 return 1;\r
f38a1528 251}\r
252\r
4d2e4eea 253int mifare_ultra_auth(uint8_t *keybytes){\r
225ccb91 254\r
8258f409 255 /// 3des2k\r
4d2e4eea 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
8258f409 261 uint8_t key[16] = {0x00};\r
4d2e4eea 262 memcpy(key, keybytes, 16);\r
a631936e 263\r
2abdfa49 264 uint16_t len = 0;\r
4d2e4eea 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
e7e95088 272 return 0;\r
4d2e4eea 273 }\r
274\r
275 // tag nonce.\r
276 memcpy(enc_random_b,resp+1,8);\r
277\r
278 // decrypt nonce.\r
53d5dc64 279 tdes_2key_dec((void*)random_b, (void*)enc_random_b, sizeof(random_b), (const void*)key, IV );\r
4d2e4eea 280 rol(random_b,8);\r
281 memcpy(rnd_ab ,random_a,8);\r
282 memcpy(rnd_ab+8,random_b,8);\r
8258f409 283\r
4d2e4eea 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
a631936e 287\r
4d2e4eea 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
8258f409 293\r
4d2e4eea 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
8258f409 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
2abdfa49 300\r
95aeb706 301 len = mifare_sendcmd(0xAF, rnd_ab, sizeof(rnd_ab), resp, respPar, NULL);\r
f168b263 302 if (len != 11) {\r
8258f409 303 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Cmd Error: %02x", resp[0]);\r
9d87eb66 304 return 0;\r
a631936e 305 }\r
8258f409 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
9d87eb66 315 return 0;\r
8258f409 316 }\r
317\r
225ccb91 318 if (MF_DBGLEVEL >= MF_DBG_EXTENDED) {\r
4d2e4eea 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
8258f409 330\r
4d2e4eea 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
f38a1528 334 }\r
e7e95088 335 return 1;\r
a631936e 336}\r
337\r
2abdfa49 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
d798d31c 341 uint8_t receivedAnswer[MAX_FRAME_SIZE] = {0x00};\r
342 uint8_t receivedAnswerPar[MAX_PARITY_SIZE] = {0x00};\r
3fe4ff4f 343 \r
64d1b4ef 344 len = mifare_sendcmd_short(NULL, 1, 0x30, blockNo, receivedAnswer, receivedAnswerPar, NULL);\r
345 if (len == 1) {\r
aa60d156 346 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Cmd Error: %02x", receivedAnswer[0]);\r
64d1b4ef 347 return 1;\r
348 }\r
349 if (len != 18) {\r
aa60d156 350 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Cmd Error: card timeout. len: %x", len);\r
64d1b4ef 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
aa60d156 357 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Cmd CRC response error.");\r
64d1b4ef 358 return 3;\r
359 }\r
360 \r
361 memcpy(blockData, receivedAnswer, 14);\r
362 return 0;\r
363}\r
364\r
2abdfa49 365int mifare_classic_writeblock(struct Crypto1State *pcs, uint32_t uid, uint8_t blockNo, uint8_t *blockData) {\r
64d1b4ef 366 // variables\r
2abdfa49 367 uint16_t len = 0; \r
add0504d 368 uint32_t pos = 0;\r
2abdfa49 369 uint8_t par[3] = {0x00, 0x00, 0x00}; // enough for 18 Bytes to send\r
add0504d 370 byte_t res = 0;\r
20f9a2a1
M
371 \r
372 uint8_t d_block[18], d_block_enc[18];\r
d798d31c 373 uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE] = {0x00};\r
374 uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE] = {0x00};\r
20f9a2a1 375 \r
4abe4f58 376 // command MIFARE_CLASSIC_WRITEBLOCK\r
a501c82b 377 len = mifare_sendcmd_short(pcs, 1, 0xA0, blockNo, receivedAnswer, receivedAnswerPar, NULL);\r
20f9a2a1
M
378\r
379 if ((len != 1) || (receivedAnswer[0] != 0x0A)) { // 0x0a - ACK\r
f397b5cc 380 if (MF_DBGLEVEL >= 1) Dbprintf("Cmd Error: %02x", receivedAnswer[0]); \r
20f9a2a1
M
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
bd9dfaa8 388 for (pos = 0; pos < 18; pos++) {\r
4abe4f58 389 d_block_enc[pos] = crypto1_byte(pcs, 0x00, 0) ^ d_block[pos];\r
f8ada309 390 par[pos>>3] |= (((filter(pcs->odd) ^ oddparity8(d_block[pos])) & 0x01) << (7 - (pos&0x0007)));\r
4abe4f58 391 } \r
20f9a2a1 392\r
9492e0b0 393 ReaderTransmitPar(d_block_enc, sizeof(d_block_enc), par, NULL);\r
20f9a2a1 394\r
4abe4f58 395 // Receive the response\r
a501c82b 396 len = ReaderReceive(receivedAnswer, receivedAnswerPar); \r
20f9a2a1
M
397\r
398 res = 0;\r
bd9dfaa8 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
20f9a2a1
M
403\r
404 if ((len != 1) || (res != 0x0A)) {\r
f397b5cc 405 if (MF_DBGLEVEL >= 1) Dbprintf("Cmd send data2 Error: %02x", res); \r
20f9a2a1
M
406 return 2;\r
407 }\r
64d1b4ef 408 return 0;\r
409}\r
410\r
95aeb706 411/* // command not needed, but left for future testing\r
2abdfa49 412int mifare_ultra_writeblock_compat(uint8_t blockNo, uint8_t *blockData) {\r
f168b263 413 uint16_t len;\r
64d1b4ef 414 uint8_t par[3] = {0}; // enough for 18 parity bits\r
415 uint8_t d_block[18] = {0x00};\r
aa60d156 416 uint8_t receivedAnswer[MAX_FRAME_SIZE];\r
417 uint8_t receivedAnswerPar[MAX_PARITY_SIZE];\r
f168b263 418\r
64d1b4ef 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
225ccb91 422 if (MF_DBGLEVEL >= MF_DBG_ERROR)\r
f168b263 423 Dbprintf("Cmd Addr Error: %02x", receivedAnswer[0]);\r
64d1b4ef 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
f168b263 432 len = ReaderReceive(receivedAnswer, receivedAnswerPar);\r
64d1b4ef 433\r
434 if ((len != 1) || (receivedAnswer[0] != 0x0A)) { // 0x0a - ACK\r
225ccb91 435 if (MF_DBGLEVEL >= MF_DBG_ERROR)\r
64d1b4ef 436 Dbprintf("Cmd Data Error: %02x %d", receivedAnswer[0],len);\r
437 return 2;\r
f168b263 438 }\r
64d1b4ef 439 return 0;\r
f168b263 440}\r
95aeb706 441*/\r
64d1b4ef 442\r
2abdfa49 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
d798d31c 446 uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE] = {0x00};\r
447 uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE] = {0x00};\r
64d1b4ef 448\r
449 // command MIFARE_CLASSIC_WRITEBLOCK\r
2abdfa49 450 memcpy(block+1, blockData, 4);\r
64d1b4ef 451\r
2abdfa49 452 len = mifare_sendcmd( MIFARE_ULC_WRITE, block, sizeof(block), receivedAnswer, receivedAnswerPar, NULL);\r
64d1b4ef 453\r
454 if (receivedAnswer[0] != 0x0A) { // 0x0a - ACK\r
225ccb91 455 if (MF_DBGLEVEL >= MF_DBG_ERROR)\r
64d1b4ef 456 Dbprintf("Cmd Send Error: %02x %d", receivedAnswer[0],len);\r
457 return 1;\r
458 }\r
459 return 0;\r
460}\r
c2731f37 461int mifare_classic_halt_ex(struct Crypto1State *pcs) {\r
2abdfa49 462 uint8_t receivedAnswer[4] = {0x00, 0x00, 0x00, 0x00};\r
dd83c457 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
64d1b4ef 468 return 0;\r
469}\r
c2731f37 470int mifare_classic_halt(struct Crypto1State *pcs, uint32_t uid) {\r
471 return mifare_classic_halt_ex(pcs);\r
472}\r
64d1b4ef 473\r
2abdfa49 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
64d1b4ef 478 if (len != 0) {\r
2abdfa49 479 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("halt error. response len: %x", len);\r
64d1b4ef 480 return 1;\r
225ccb91 481 }\r
64d1b4ef 482 return 0;\r
483}\r
484\r
baeaf579 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
2abdfa49 488uint8_t NumBlocksPerSector(uint8_t sectorNo) {\r
489 return (sectorNo < 32) ? 4 : 16;\r
baeaf579 490}\r
491\r
2abdfa49 492uint8_t FirstBlockOfSector(uint8_t sectorNo) {\r
baeaf579 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
64d1b4ef 500// work with emulator memory\r
501void emlSetMem(uint8_t *data, int blockNum, int blocksCount) {\r
0de8e387 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
64d1b4ef 506 uint8_t* emCARD = BigBuf_get_EM_addr();\r
0de8e387 507 memcpy(emCARD + blockNum * blockBtWidth, data, blocksCount * blockBtWidth);\r
8f51ddb0
M
508}\r
509\r
510void emlGetMem(uint8_t *data, int blockNum, int blocksCount) {\r
f71f4deb 511 uint8_t* emCARD = BigBuf_get_EM_addr();\r
8f51ddb0
M
512 memcpy(data, emCARD + blockNum * 16, blocksCount * 16);\r
513}\r
514\r
515void emlGetMemBt(uint8_t *data, int bytePtr, int byteCount) {\r
f71f4deb 516 uint8_t* emCARD = BigBuf_get_EM_addr();\r
8f51ddb0
M
517 memcpy(data, emCARD + bytePtr, byteCount);\r
518}\r
519\r
0014cb46 520int emlCheckValBl(int blockNum) {\r
f71f4deb 521 uint8_t* emCARD = BigBuf_get_EM_addr();\r
0014cb46
M
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
f71f4deb 536 uint8_t* emCARD = BigBuf_get_EM_addr();\r
0014cb46
M
537 uint8_t* data = emCARD + blockNum * 16;\r
538 \r
d798d31c 539 if (emlCheckValBl(blockNum))\r
0014cb46 540 return 1;\r
0014cb46
M
541 \r
542 memcpy(blReg, data, 4);\r
543 *blBlock = data[12];\r
0014cb46
M
544 return 0;\r
545}\r
546\r
547int emlSetValBl(uint32_t blReg, uint8_t blBlock, int blockNum) {\r
f71f4deb 548 uint8_t* emCARD = BigBuf_get_EM_addr();\r
0014cb46
M
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
8556b852 564uint64_t emlGetKey(int sectorNum, int keyType) {\r
d798d31c 565 uint8_t key[6] = {0x00};\r
f71f4deb 566 uint8_t* emCARD = BigBuf_get_EM_addr();\r
8556b852 567 \r
baeaf579 568 memcpy(key, emCARD + 16 * (FirstBlockOfSector(sectorNum) + NumBlocksPerSector(sectorNum) - 1) + keyType * 10, 6);\r
8556b852
M
569 return bytes_to_num(key, 6);\r
570}\r
571\r
8f51ddb0 572void emlClearMem(void) {\r
aea4d766 573 int b;\r
8f51ddb0
M
574 \r
575 const uint8_t trailer[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x07, 0x80, 0x69, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};\r
8f51ddb0 576 const uint8_t uid[] = {0xe6, 0x84, 0x87, 0xf3, 0x16, 0x88, 0x04, 0x00, 0x46, 0x8e, 0x45, 0x55, 0x4d, 0x70, 0x41, 0x04};\r
f71f4deb 577 uint8_t* emCARD = BigBuf_get_EM_addr();\r
aea4d766 578 \r
a501c82b 579 memset(emCARD, 0, CARD_MEMORY_SIZE);\r
aea4d766 580 \r
581 // fill sectors trailer data\r
bd9dfaa8 582 for(b = 3; b < 256; b<127?(b+=4):(b+=16))\r
aea4d766 583 emlSetMem((uint8_t *)trailer, b , 1);\r
8f51ddb0
M
584\r
585 // uid\r
586 emlSetMem((uint8_t *)uid, 0, 1);\r
587 return;\r
588}\r
6ff6ade2 589\r
225ccb91 590\r
591// Mifare desfire commands\r
6ff6ade2 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
2abdfa49 594 uint8_t dcmd[5] = {cmd, data[0], data[1], 0x00, 0x00};\r
6ff6ade2 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
225ccb91 600 if (MF_DBGLEVEL >= MF_DBG_ERROR) \r
601 Dbprintf("Authentication failed. Card timeout.");\r
602 return 1;\r
6ff6ade2 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
225ccb91 617 if (MF_DBGLEVEL >= MF_DBG_ERROR)\r
618 Dbprintf("Authentication failed. Card timeout.");\r
619 return 1;\r
6ff6ade2 620 }\r
621 return len;\r
622}\r
623\r
624int mifare_desfire_des_auth1(uint32_t uid, uint8_t *blockData){\r
225ccb91 625\r
6ff6ade2 626 int len;\r
e3ab50ca 627 // load key, keynumber\r
6ff6ade2 628 uint8_t data[2]={0x0a, 0x00};\r
d798d31c 629 uint8_t receivedAnswer[MAX_FRAME_SIZE] = {0x00};\r
630 uint8_t receivedAnswerPar[MAX_PARITY_SIZE] = {0x00};\r
6ff6ade2 631 \r
6ff6ade2 632 len = mifare_sendcmd_special(NULL, 1, 0x02, data, receivedAnswer,receivedAnswerPar,NULL);\r
633 if (len == 1) {\r
225ccb91 634 if (MF_DBGLEVEL >= MF_DBG_ERROR)\r
635 Dbprintf("Cmd Error: %02x", receivedAnswer[0]);\r
6ff6ade2 636 return 1;\r
637 }\r
638 \r
639 if (len == 12) {\r
225ccb91 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
6ff6ade2 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
225ccb91 653\r
6ff6ade2 654 int len;\r
e3ab50ca 655 uint8_t data[17] = {0x00};\r
656 data[0] = 0xAF;\r
6ff6ade2 657 memcpy(data+1,key,16);\r
658 \r
d798d31c 659 uint8_t receivedAnswer[MAX_FRAME_SIZE] = {0x00};\r
660 uint8_t receivedAnswerPar[MAX_PARITY_SIZE] = {0x00};\r
6ff6ade2 661 \r
6ff6ade2 662 len = mifare_sendcmd_special2(NULL, 1, 0x03, data, receivedAnswer, receivedAnswerPar ,NULL);\r
663 \r
e3ab50ca 664 if ((receivedAnswer[0] == 0x03) && (receivedAnswer[1] == 0xae)) {\r
225ccb91 665 if (MF_DBGLEVEL >= MF_DBG_ERROR)\r
666 Dbprintf("Auth Error: %02x %02x", receivedAnswer[0], receivedAnswer[1]);\r
6ff6ade2 667 return 1;\r
668 }\r
225ccb91 669 \r
6ff6ade2 670 if (len == 12){\r
225ccb91 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
6ff6ade2 677 memcpy(blockData, receivedAnswer, 12);\r
678 return 0;\r
679 }\r
680 return 1;\r
64d1b4ef 681}\r
Impressum, Datenschutz