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