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