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