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