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