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