mifare_classic.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749
  1. #include "mifare_classic.h"
  2. #include "nfca.h"
  3. #include "nfc_util.h"
  4. #include <furi_hal_rtc.h>
  5. // Algorithm from https://github.com/RfidResearchGroup/proxmark3.git
  6. #define TAG "MfClassic"
  7. #define MF_CLASSIC_AUTH_KEY_A_CMD (0x60U)
  8. #define MF_CLASSIC_AUTH_KEY_B_CMD (0x61U)
  9. #define MF_CLASSIC_READ_SECT_CMD (0x30)
  10. typedef enum {
  11. MfClassicActionDataRead,
  12. MfClassicActionDataWrite,
  13. MfClassicActionDataInc,
  14. MfClassicActionDataDec,
  15. MfClassicActionKeyARead,
  16. MfClassicActionKeyAWrite,
  17. MfClassicActionKeyBRead,
  18. MfClassicActionKeyBWrite,
  19. MfClassicActionACRead,
  20. MfClassicActionACWrite,
  21. } MfClassicAction;
  22. static uint8_t mf_classic_get_first_block_num_of_sector(uint8_t sector) {
  23. furi_assert(sector < 40);
  24. if(sector < 32) {
  25. return sector * 4;
  26. } else {
  27. return 32 * 4 + (sector - 32) * 16;
  28. }
  29. }
  30. static uint8_t mf_classic_get_sector_by_block(uint8_t block) {
  31. if(block < 128) {
  32. return (block | 0x03) / 4;
  33. } else {
  34. return 32 + ((block | 0xf) - 32 * 4) / 16;
  35. }
  36. }
  37. static uint8_t mf_classic_get_blocks_num_in_sector(uint8_t sector) {
  38. furi_assert(sector < 40);
  39. return sector < 32 ? 4 : 16;
  40. }
  41. static uint8_t mf_classic_get_sector_trailer(uint8_t block) {
  42. if(block < 128) {
  43. return block | 0x03;
  44. } else {
  45. return block | 0x0f;
  46. }
  47. }
  48. static bool mf_classic_is_sector_trailer(uint8_t block) {
  49. return block == mf_classic_get_sector_trailer(block);
  50. }
  51. uint8_t mf_classic_get_total_sectors_num(MfClassicReader* reader) {
  52. furi_assert(reader);
  53. if(reader->type == MfClassicType1k) {
  54. return MF_CLASSIC_1K_TOTAL_SECTORS_NUM;
  55. } else if(reader->type == MfClassicType4k) {
  56. return MF_CLASSIC_4K_TOTAL_SECTORS_NUM;
  57. } else {
  58. return 0;
  59. }
  60. }
  61. static uint16_t mf_classic_get_total_block_num(MfClassicType type) {
  62. if(type == MfClassicType1k) {
  63. return 64;
  64. } else if(type == MfClassicType4k) {
  65. return 256;
  66. } else {
  67. return 0;
  68. }
  69. }
  70. static bool mf_classic_is_allowed_access_sector_trailer(
  71. MfClassicEmulator* emulator,
  72. uint8_t block_num,
  73. MfClassicKey key,
  74. MfClassicAction action) {
  75. uint8_t* sector_trailer = emulator->data.block[block_num].value;
  76. uint8_t AC = ((sector_trailer[7] >> 5) & 0x04) | ((sector_trailer[8] >> 2) & 0x02) |
  77. ((sector_trailer[8] >> 7) & 0x01);
  78. switch(action) {
  79. case MfClassicActionKeyARead: {
  80. return false;
  81. }
  82. case MfClassicActionKeyAWrite: {
  83. return (
  84. (key == MfClassicKeyA && (AC == 0x00 || AC == 0x01)) ||
  85. (key == MfClassicKeyB && (AC == 0x04 || AC == 0x03)));
  86. }
  87. case MfClassicActionKeyBRead: {
  88. return (key == MfClassicKeyA && (AC == 0x00 || AC == 0x02 || AC == 0x01));
  89. }
  90. case MfClassicActionKeyBWrite: {
  91. return (
  92. (key == MfClassicKeyA && (AC == 0x00 || AC == 0x01)) ||
  93. (key == MfClassicKeyB && (AC == 0x04 || AC == 0x03)));
  94. }
  95. case MfClassicActionACRead: {
  96. return (
  97. (key == MfClassicKeyA) ||
  98. (key == MfClassicKeyB && !(AC == 0x00 || AC == 0x02 || AC == 0x01)));
  99. }
  100. case MfClassicActionACWrite: {
  101. return (
  102. (key == MfClassicKeyA && (AC == 0x01)) ||
  103. (key == MfClassicKeyB && (AC == 0x03 || AC == 0x05)));
  104. }
  105. default:
  106. return false;
  107. }
  108. return true;
  109. }
  110. static bool mf_classic_is_allowed_access_data_block(
  111. MfClassicEmulator* emulator,
  112. uint8_t block_num,
  113. MfClassicKey key,
  114. MfClassicAction action) {
  115. uint8_t* sector_trailer = emulator->data.block[mf_classic_get_sector_trailer(block_num)].value;
  116. uint8_t sector_block;
  117. if(block_num <= 128) {
  118. sector_block = block_num & 0x03;
  119. } else {
  120. sector_block = (block_num & 0x0f) / 5;
  121. }
  122. uint8_t AC;
  123. switch(sector_block) {
  124. case 0x00: {
  125. AC = ((sector_trailer[7] >> 2) & 0x04) | ((sector_trailer[8] << 1) & 0x02) |
  126. ((sector_trailer[8] >> 4) & 0x01);
  127. break;
  128. }
  129. case 0x01: {
  130. AC = ((sector_trailer[7] >> 3) & 0x04) | ((sector_trailer[8] >> 0) & 0x02) |
  131. ((sector_trailer[8] >> 5) & 0x01);
  132. break;
  133. }
  134. case 0x02: {
  135. AC = ((sector_trailer[7] >> 4) & 0x04) | ((sector_trailer[8] >> 1) & 0x02) |
  136. ((sector_trailer[8] >> 6) & 0x01);
  137. break;
  138. }
  139. default:
  140. return false;
  141. }
  142. switch(action) {
  143. case MfClassicActionDataRead: {
  144. return (
  145. (key == MfClassicKeyA && !(AC == 0x03 || AC == 0x05 || AC == 0x07)) ||
  146. (key == MfClassicKeyB && !(AC == 0x07)));
  147. }
  148. case MfClassicActionDataWrite: {
  149. return (
  150. (key == MfClassicKeyA && (AC == 0x00)) ||
  151. (key == MfClassicKeyB && (AC == 0x00 || AC == 0x04 || AC == 0x06 || AC == 0x03)));
  152. }
  153. case MfClassicActionDataInc: {
  154. return (
  155. (key == MfClassicKeyA && (AC == 0x00)) ||
  156. (key == MfClassicKeyB && (AC == 0x00 || AC == 0x06)));
  157. }
  158. case MfClassicActionDataDec: {
  159. return (
  160. (key == MfClassicKeyA && (AC == 0x00 || AC == 0x06 || AC == 0x01)) ||
  161. (key == MfClassicKeyB && (AC == 0x00 || AC == 0x06 || AC == 0x01)));
  162. }
  163. default:
  164. return false;
  165. }
  166. return false;
  167. }
  168. static bool mf_classic_is_allowed_access(
  169. MfClassicEmulator* emulator,
  170. uint8_t block_num,
  171. MfClassicKey key,
  172. MfClassicAction action) {
  173. if(mf_classic_is_sector_trailer(block_num)) {
  174. return mf_classic_is_allowed_access_sector_trailer(emulator, block_num, key, action);
  175. } else {
  176. return mf_classic_is_allowed_access_data_block(emulator, block_num, key, action);
  177. }
  178. }
  179. bool mf_classic_check_card_type(uint8_t ATQA0, uint8_t ATQA1, uint8_t SAK) {
  180. UNUSED(ATQA1);
  181. if((ATQA0 == 0x44 || ATQA0 == 0x04) && (SAK == 0x08)) {
  182. return true;
  183. } else if((ATQA0 == 0x42 || ATQA0 == 0x02) && (SAK == 0x18)) {
  184. return true;
  185. } else {
  186. return false;
  187. }
  188. }
  189. bool mf_classic_get_type(
  190. uint8_t* uid,
  191. uint8_t uid_len,
  192. uint8_t ATQA0,
  193. uint8_t ATQA1,
  194. uint8_t SAK,
  195. MfClassicReader* reader) {
  196. UNUSED(ATQA1);
  197. furi_assert(uid);
  198. furi_assert(reader);
  199. memset(reader, 0, sizeof(MfClassicReader));
  200. if((ATQA0 == 0x44 || ATQA0 == 0x04) && (SAK == 0x08)) {
  201. reader->type = MfClassicType1k;
  202. } else if((ATQA0 == 0x42 || ATQA0 == 0x02) && (SAK == 0x18)) {
  203. reader->type = MfClassicType4k;
  204. } else {
  205. return false;
  206. }
  207. uint8_t* cuid_start = uid;
  208. if(uid_len == 7) {
  209. cuid_start = &uid[3];
  210. }
  211. reader->cuid = (cuid_start[0] << 24) | (cuid_start[1] << 16) | (cuid_start[2] << 8) |
  212. (cuid_start[3]);
  213. return true;
  214. }
  215. void mf_classic_reader_add_sector(
  216. MfClassicReader* reader,
  217. uint8_t sector,
  218. uint64_t key_a,
  219. uint64_t key_b) {
  220. furi_assert(reader);
  221. furi_assert(sector < MF_CLASSIC_SECTORS_MAX);
  222. furi_assert((key_a != MF_CLASSIC_NO_KEY) || (key_b != MF_CLASSIC_NO_KEY));
  223. if(reader->sectors_to_read < MF_CLASSIC_SECTORS_MAX - 1) {
  224. reader->sector_reader[reader->sectors_to_read].key_a = key_a;
  225. reader->sector_reader[reader->sectors_to_read].key_b = key_b;
  226. reader->sector_reader[reader->sectors_to_read].sector_num = sector;
  227. reader->sectors_to_read++;
  228. }
  229. }
  230. void mf_classic_auth_init_context(MfClassicAuthContext* auth_ctx, uint32_t cuid, uint8_t sector) {
  231. furi_assert(auth_ctx);
  232. auth_ctx->cuid = cuid;
  233. auth_ctx->sector = sector;
  234. auth_ctx->key_a = MF_CLASSIC_NO_KEY;
  235. auth_ctx->key_b = MF_CLASSIC_NO_KEY;
  236. }
  237. static bool mf_classic_auth(
  238. FuriHalNfcTxRxContext* tx_rx,
  239. uint32_t cuid,
  240. uint32_t block,
  241. uint64_t key,
  242. MfClassicKey key_type,
  243. Crypto1* crypto) {
  244. bool auth_success = false;
  245. memset(tx_rx->tx_data, 0, sizeof(tx_rx->tx_data));
  246. memset(tx_rx->tx_parity, 0, sizeof(tx_rx->tx_parity));
  247. tx_rx->tx_rx_type = FuriHalNfcTxRxTypeDefault;
  248. do {
  249. if(key_type == MfClassicKeyA) {
  250. tx_rx->tx_data[0] = MF_CLASSIC_AUTH_KEY_A_CMD;
  251. } else {
  252. tx_rx->tx_data[0] = MF_CLASSIC_AUTH_KEY_B_CMD;
  253. }
  254. tx_rx->tx_data[1] = block;
  255. tx_rx->tx_rx_type = FuriHalNfcTxRxTypeRxNoCrc;
  256. tx_rx->tx_bits = 2 * 8;
  257. if(!furi_hal_nfc_tx_rx(tx_rx, 6)) break;
  258. uint32_t nt = (uint32_t)nfc_util_bytes2num(tx_rx->rx_data, 4);
  259. crypto1_init(crypto, key);
  260. crypto1_word(crypto, nt ^ cuid, 0);
  261. uint8_t nr[4] = {};
  262. nfc_util_num2bytes(prng_successor(DWT->CYCCNT, 32), 4, nr);
  263. for(uint8_t i = 0; i < 4; i++) {
  264. tx_rx->tx_data[i] = crypto1_byte(crypto, nr[i], 0) ^ nr[i];
  265. tx_rx->tx_parity[0] |=
  266. (((crypto1_filter(crypto->odd) ^ nfc_util_odd_parity8(nr[i])) & 0x01) << (7 - i));
  267. }
  268. nt = prng_successor(nt, 32);
  269. for(uint8_t i = 4; i < 8; i++) {
  270. nt = prng_successor(nt, 8);
  271. tx_rx->tx_data[i] = crypto1_byte(crypto, 0x00, 0) ^ (nt & 0xff);
  272. tx_rx->tx_parity[0] |=
  273. (((crypto1_filter(crypto->odd) ^ nfc_util_odd_parity8(nt & 0xff)) & 0x01)
  274. << (7 - i));
  275. }
  276. tx_rx->tx_rx_type = FuriHalNfcTxRxTypeRaw;
  277. tx_rx->tx_bits = 8 * 8;
  278. if(!furi_hal_nfc_tx_rx(tx_rx, 6)) break;
  279. if(tx_rx->rx_bits == 32) {
  280. crypto1_word(crypto, 0, 0);
  281. auth_success = true;
  282. }
  283. } while(false);
  284. return auth_success;
  285. }
  286. bool mf_classic_auth_attempt(
  287. FuriHalNfcTxRxContext* tx_rx,
  288. MfClassicAuthContext* auth_ctx,
  289. uint64_t key) {
  290. furi_assert(tx_rx);
  291. furi_assert(auth_ctx);
  292. bool found_key = false;
  293. bool need_halt = (auth_ctx->key_a == MF_CLASSIC_NO_KEY) &&
  294. (auth_ctx->key_b == MF_CLASSIC_NO_KEY);
  295. Crypto1 crypto;
  296. if(auth_ctx->key_a == MF_CLASSIC_NO_KEY) {
  297. // Try AUTH with key A
  298. if(mf_classic_auth(
  299. tx_rx,
  300. auth_ctx->cuid,
  301. mf_classic_get_first_block_num_of_sector(auth_ctx->sector),
  302. key,
  303. MfClassicKeyA,
  304. &crypto)) {
  305. auth_ctx->key_a = key;
  306. found_key = true;
  307. }
  308. }
  309. if(need_halt) {
  310. furi_hal_nfc_sleep();
  311. furi_hal_nfc_activate_nfca(300, &auth_ctx->cuid);
  312. }
  313. if(auth_ctx->key_b == MF_CLASSIC_NO_KEY) {
  314. // Try AUTH with key B
  315. if(mf_classic_auth(
  316. tx_rx,
  317. auth_ctx->cuid,
  318. mf_classic_get_first_block_num_of_sector(auth_ctx->sector),
  319. key,
  320. MfClassicKeyB,
  321. &crypto)) {
  322. auth_ctx->key_b = key;
  323. found_key = true;
  324. }
  325. }
  326. return found_key;
  327. }
  328. bool mf_classic_read_block(
  329. FuriHalNfcTxRxContext* tx_rx,
  330. Crypto1* crypto,
  331. uint8_t block_num,
  332. MfClassicBlock* block) {
  333. furi_assert(tx_rx);
  334. furi_assert(crypto);
  335. furi_assert(block);
  336. bool read_block_success = false;
  337. uint8_t plain_cmd[4] = {MF_CLASSIC_READ_SECT_CMD, block_num, 0x00, 0x00};
  338. nfca_append_crc16(plain_cmd, 2);
  339. memset(tx_rx->tx_data, 0, sizeof(tx_rx->tx_data));
  340. memset(tx_rx->tx_parity, 0, sizeof(tx_rx->tx_parity));
  341. for(uint8_t i = 0; i < 4; i++) {
  342. tx_rx->tx_data[i] = crypto1_byte(crypto, 0x00, 0) ^ plain_cmd[i];
  343. tx_rx->tx_parity[0] |=
  344. ((crypto1_filter(crypto->odd) ^ nfc_util_odd_parity8(plain_cmd[i])) & 0x01) << (7 - i);
  345. }
  346. tx_rx->tx_bits = 4 * 9;
  347. tx_rx->tx_rx_type = FuriHalNfcTxRxTypeRaw;
  348. if(furi_hal_nfc_tx_rx(tx_rx, 50)) {
  349. if(tx_rx->rx_bits == 8 * 18) {
  350. for(uint8_t i = 0; i < 18; i++) {
  351. block->value[i] = crypto1_byte(crypto, 0, 0) ^ tx_rx->rx_data[i];
  352. }
  353. read_block_success = true;
  354. }
  355. }
  356. return read_block_success;
  357. }
  358. bool mf_classic_read_sector(
  359. FuriHalNfcTxRxContext* tx_rx,
  360. Crypto1* crypto,
  361. MfClassicSectorReader* sector_reader,
  362. MfClassicSector* sector) {
  363. furi_assert(tx_rx);
  364. furi_assert(sector_reader);
  365. furi_assert(sector);
  366. uint32_t cuid = 0;
  367. uint64_t key;
  368. MfClassicKey key_type;
  369. uint8_t first_block;
  370. bool sector_read = false;
  371. furi_hal_nfc_sleep();
  372. do {
  373. // Activate card
  374. if(!furi_hal_nfc_activate_nfca(200, &cuid)) break;
  375. first_block = mf_classic_get_first_block_num_of_sector(sector_reader->sector_num);
  376. if(sector_reader->key_a != MF_CLASSIC_NO_KEY) {
  377. key = sector_reader->key_a;
  378. key_type = MfClassicKeyA;
  379. } else if(sector_reader->key_b != MF_CLASSIC_NO_KEY) {
  380. key = sector_reader->key_b;
  381. key_type = MfClassicKeyB;
  382. } else {
  383. break;
  384. }
  385. // Auth to first block in sector
  386. if(!mf_classic_auth(tx_rx, cuid, first_block, key, key_type, crypto)) break;
  387. sector->total_blocks = mf_classic_get_blocks_num_in_sector(sector_reader->sector_num);
  388. // Read blocks
  389. for(uint8_t i = 0; i < sector->total_blocks; i++) {
  390. mf_classic_read_block(tx_rx, crypto, first_block + i, &sector->block[i]);
  391. }
  392. // Save sector keys in last block
  393. if(sector_reader->key_a != MF_CLASSIC_NO_KEY) {
  394. nfc_util_num2bytes(
  395. sector_reader->key_a, 6, &sector->block[sector->total_blocks - 1].value[0]);
  396. }
  397. if(sector_reader->key_b != MF_CLASSIC_NO_KEY) {
  398. nfc_util_num2bytes(
  399. sector_reader->key_b, 6, &sector->block[sector->total_blocks - 1].value[10]);
  400. }
  401. sector_read = true;
  402. } while(false);
  403. return sector_read;
  404. }
  405. uint8_t mf_classic_read_card(
  406. FuriHalNfcTxRxContext* tx_rx,
  407. MfClassicReader* reader,
  408. MfClassicData* data) {
  409. furi_assert(tx_rx);
  410. furi_assert(reader);
  411. furi_assert(data);
  412. uint8_t sectors_read = 0;
  413. data->type = reader->type;
  414. data->key_a_mask = 0;
  415. data->key_b_mask = 0;
  416. MfClassicSector temp_sector = {};
  417. for(uint8_t i = 0; i < reader->sectors_to_read; i++) {
  418. if(mf_classic_read_sector(
  419. tx_rx, &reader->crypto, &reader->sector_reader[i], &temp_sector)) {
  420. uint8_t first_block =
  421. mf_classic_get_first_block_num_of_sector(reader->sector_reader[i].sector_num);
  422. for(uint8_t j = 0; j < temp_sector.total_blocks; j++) {
  423. data->block[first_block + j] = temp_sector.block[j];
  424. }
  425. if(reader->sector_reader[i].key_a != MF_CLASSIC_NO_KEY) {
  426. data->key_a_mask |= 1 << reader->sector_reader[i].sector_num;
  427. }
  428. if(reader->sector_reader[i].key_b != MF_CLASSIC_NO_KEY) {
  429. data->key_b_mask |= 1 << reader->sector_reader[i].sector_num;
  430. }
  431. sectors_read++;
  432. }
  433. }
  434. return sectors_read;
  435. }
  436. void mf_crypto1_decrypt(
  437. Crypto1* crypto,
  438. uint8_t* encrypted_data,
  439. uint16_t encrypted_data_bits,
  440. uint8_t* decrypted_data) {
  441. if(encrypted_data_bits < 8) {
  442. uint8_t decrypted_byte = 0;
  443. decrypted_byte |= (crypto1_bit(crypto, 0, 0) ^ FURI_BIT(encrypted_data[0], 0)) << 0;
  444. decrypted_byte |= (crypto1_bit(crypto, 0, 0) ^ FURI_BIT(encrypted_data[0], 1)) << 1;
  445. decrypted_byte |= (crypto1_bit(crypto, 0, 0) ^ FURI_BIT(encrypted_data[0], 2)) << 2;
  446. decrypted_byte |= (crypto1_bit(crypto, 0, 0) ^ FURI_BIT(encrypted_data[0], 3)) << 3;
  447. decrypted_data[0] = decrypted_byte;
  448. } else {
  449. for(size_t i = 0; i < encrypted_data_bits / 8; i++) {
  450. decrypted_data[i] = crypto1_byte(crypto, 0, 0) ^ encrypted_data[i];
  451. }
  452. }
  453. }
  454. void mf_crypto1_encrypt(
  455. Crypto1* crypto,
  456. uint8_t* keystream,
  457. uint8_t* plain_data,
  458. uint16_t plain_data_bits,
  459. uint8_t* encrypted_data,
  460. uint8_t* encrypted_parity) {
  461. if(plain_data_bits < 8) {
  462. encrypted_data[0] = 0;
  463. for(size_t i = 0; i < plain_data_bits; i++) {
  464. encrypted_data[0] |= (crypto1_bit(crypto, 0, 0) ^ FURI_BIT(plain_data[0], i)) << i;
  465. }
  466. } else {
  467. memset(encrypted_parity, 0, plain_data_bits / 8 + 1);
  468. for(uint8_t i = 0; i < plain_data_bits / 8; i++) {
  469. encrypted_data[i] = crypto1_byte(crypto, keystream ? keystream[i] : 0, 0) ^
  470. plain_data[i];
  471. encrypted_parity[i / 8] |=
  472. (((crypto1_filter(crypto->odd) ^ nfc_util_odd_parity8(plain_data[i])) & 0x01)
  473. << (7 - (i & 0x0007)));
  474. }
  475. }
  476. }
  477. bool mf_classic_emulator(MfClassicEmulator* emulator, FuriHalNfcTxRxContext* tx_rx) {
  478. furi_assert(emulator);
  479. furi_assert(tx_rx);
  480. bool command_processed = false;
  481. bool is_encrypted = false;
  482. uint8_t plain_data[MF_CLASSIC_MAX_DATA_SIZE];
  483. MfClassicKey access_key = MfClassicKeyA;
  484. // Read command
  485. while(!command_processed) {
  486. if(!is_encrypted) {
  487. // Read first frame
  488. tx_rx->tx_bits = 0;
  489. tx_rx->tx_rx_type = FuriHalNfcTxRxTypeDefault;
  490. }
  491. if(!furi_hal_nfc_tx_rx(tx_rx, 300)) {
  492. FURI_LOG_D(
  493. TAG, "Error in tx rx. Tx :%d bits, Rx: %d bits", tx_rx->tx_bits, tx_rx->rx_bits);
  494. break;
  495. }
  496. if(!is_encrypted) {
  497. memcpy(plain_data, tx_rx->rx_data, tx_rx->rx_bits / 8);
  498. } else {
  499. mf_crypto1_decrypt(&emulator->crypto, tx_rx->rx_data, tx_rx->rx_bits, plain_data);
  500. }
  501. // TODO Check crc
  502. if(plain_data[0] == 0x50 && plain_data[1] == 00) {
  503. FURI_LOG_T(TAG, "Halt received");
  504. command_processed = true;
  505. break;
  506. } else if(plain_data[0] == 0x60 || plain_data[0] == 0x61) {
  507. uint8_t block = plain_data[1];
  508. uint64_t key = 0;
  509. uint8_t sector_trailer_block = mf_classic_get_sector_trailer(block);
  510. MfClassicSectorTrailer* sector_trailer =
  511. (MfClassicSectorTrailer*)emulator->data.block[sector_trailer_block].value;
  512. if(plain_data[0] == 0x61) {
  513. key = nfc_util_bytes2num(sector_trailer->key_b, 6);
  514. access_key = MfClassicKeyA;
  515. } else {
  516. key = nfc_util_bytes2num(sector_trailer->key_a, 6);
  517. access_key = MfClassicKeyB;
  518. }
  519. uint32_t nonce = prng_successor(DWT->CYCCNT, 32);
  520. uint8_t nt[4];
  521. uint8_t nt_keystream[4];
  522. nfc_util_num2bytes(nonce, 4, nt);
  523. nfc_util_num2bytes(nonce ^ emulator->cuid, 4, nt_keystream);
  524. crypto1_init(&emulator->crypto, key);
  525. if(!is_encrypted) {
  526. crypto1_word(&emulator->crypto, emulator->cuid ^ nonce, 0);
  527. memcpy(tx_rx->tx_data, nt, sizeof(nt));
  528. tx_rx->tx_bits = sizeof(nt) * 8;
  529. tx_rx->tx_rx_type = FuriHalNfcTxRxTypeRxRaw;
  530. } else {
  531. mf_crypto1_encrypt(
  532. &emulator->crypto,
  533. nt_keystream,
  534. nt,
  535. sizeof(nt) * 8,
  536. tx_rx->tx_data,
  537. tx_rx->tx_parity);
  538. tx_rx->tx_bits = sizeof(nt) * 8;
  539. tx_rx->tx_rx_type = FuriHalNfcTxRxTransparent;
  540. }
  541. if(!furi_hal_nfc_tx_rx(tx_rx, 500)) {
  542. FURI_LOG_E(TAG, "Error in NT exchange");
  543. command_processed = true;
  544. break;
  545. }
  546. if(tx_rx->rx_bits != 64) {
  547. FURI_LOG_W(TAG, "Incorrect nr + ar");
  548. command_processed = true;
  549. break;
  550. }
  551. // Check if we store valid key
  552. if(access_key == MfClassicKeyA) {
  553. if(FURI_BIT(emulator->data.key_a_mask, mf_classic_get_sector_by_block(block)) ==
  554. 0) {
  555. FURI_LOG_D(TAG, "Unsupported sector key A for block %d", sector_trailer_block);
  556. break;
  557. }
  558. } else if(access_key == MfClassicKeyB) {
  559. if(FURI_BIT(emulator->data.key_b_mask, mf_classic_get_sector_by_block(block)) ==
  560. 0) {
  561. FURI_LOG_D(TAG, "Unsupported sector key B for block %d", sector_trailer_block);
  562. break;
  563. }
  564. }
  565. uint32_t nr = nfc_util_bytes2num(tx_rx->rx_data, 4);
  566. uint32_t ar = nfc_util_bytes2num(&tx_rx->rx_data[4], 4);
  567. crypto1_word(&emulator->crypto, nr, 1);
  568. uint32_t cardRr = ar ^ crypto1_word(&emulator->crypto, 0, 0);
  569. if(cardRr != prng_successor(nonce, 64)) {
  570. FURI_LOG_T(TAG, "Wrong AUTH! %08X != %08X", cardRr, prng_successor(nonce, 64));
  571. // Don't send NACK, as tag don't send it
  572. command_processed = true;
  573. break;
  574. }
  575. uint32_t ans = prng_successor(nonce, 96);
  576. uint8_t responce[4] = {};
  577. nfc_util_num2bytes(ans, 4, responce);
  578. mf_crypto1_encrypt(
  579. &emulator->crypto,
  580. NULL,
  581. responce,
  582. sizeof(responce) * 8,
  583. tx_rx->tx_data,
  584. tx_rx->tx_parity);
  585. tx_rx->tx_bits = sizeof(responce) * 8;
  586. tx_rx->tx_rx_type = FuriHalNfcTxRxTransparent;
  587. is_encrypted = true;
  588. } else if(is_encrypted && plain_data[0] == 0x30) {
  589. uint8_t block = plain_data[1];
  590. uint8_t block_data[18] = {};
  591. memcpy(block_data, emulator->data.block[block].value, MF_CLASSIC_BLOCK_SIZE);
  592. if(mf_classic_is_sector_trailer(block)) {
  593. if(!mf_classic_is_allowed_access(
  594. emulator, block, access_key, MfClassicActionKeyARead)) {
  595. memset(block_data, 0, 6);
  596. }
  597. if(!mf_classic_is_allowed_access(
  598. emulator, block, access_key, MfClassicActionKeyBRead)) {
  599. memset(&block_data[10], 0, 6);
  600. }
  601. if(!mf_classic_is_allowed_access(
  602. emulator, block, access_key, MfClassicActionACRead)) {
  603. memset(&block_data[6], 0, 4);
  604. }
  605. } else {
  606. if(!mf_classic_is_allowed_access(
  607. emulator, block, access_key, MfClassicActionDataRead)) {
  608. memset(block_data, 0, 16);
  609. }
  610. }
  611. nfca_append_crc16(block_data, 16);
  612. mf_crypto1_encrypt(
  613. &emulator->crypto,
  614. NULL,
  615. block_data,
  616. sizeof(block_data) * 8,
  617. tx_rx->tx_data,
  618. tx_rx->tx_parity);
  619. tx_rx->tx_bits = 18 * 8;
  620. tx_rx->tx_rx_type = FuriHalNfcTxRxTransparent;
  621. } else if(is_encrypted && plain_data[0] == 0xA0) {
  622. uint8_t block = plain_data[1];
  623. if(block > mf_classic_get_total_block_num(emulator->data.type)) {
  624. break;
  625. }
  626. // Send ACK
  627. uint8_t ack = 0x0A;
  628. mf_crypto1_encrypt(&emulator->crypto, NULL, &ack, 4, tx_rx->tx_data, tx_rx->tx_parity);
  629. tx_rx->tx_rx_type = FuriHalNfcTxRxTransparent;
  630. tx_rx->tx_bits = 4;
  631. if(!furi_hal_nfc_tx_rx(tx_rx, 300)) break;
  632. if(tx_rx->rx_bits != 18 * 8) break;
  633. mf_crypto1_decrypt(&emulator->crypto, tx_rx->rx_data, tx_rx->rx_bits, plain_data);
  634. uint8_t block_data[16] = {};
  635. memcpy(block_data, emulator->data.block[block].value, MF_CLASSIC_BLOCK_SIZE);
  636. if(mf_classic_is_sector_trailer(block)) {
  637. if(mf_classic_is_allowed_access(
  638. emulator, block, access_key, MfClassicActionKeyAWrite)) {
  639. memcpy(block_data, plain_data, 6);
  640. }
  641. if(mf_classic_is_allowed_access(
  642. emulator, block, access_key, MfClassicActionKeyBWrite)) {
  643. memcpy(&block_data[10], &plain_data[10], 6);
  644. }
  645. if(mf_classic_is_allowed_access(
  646. emulator, block, access_key, MfClassicActionACWrite)) {
  647. memcpy(&block_data[6], &plain_data[6], 4);
  648. }
  649. } else {
  650. if(mf_classic_is_allowed_access(
  651. emulator, block, access_key, MfClassicActionDataWrite)) {
  652. memcpy(block_data, plain_data, MF_CLASSIC_BLOCK_SIZE);
  653. }
  654. }
  655. if(memcmp(block_data, emulator->data.block[block].value, MF_CLASSIC_BLOCK_SIZE)) {
  656. memcpy(emulator->data.block[block].value, block_data, MF_CLASSIC_BLOCK_SIZE);
  657. emulator->data_changed = true;
  658. }
  659. // Send ACK
  660. ack = 0x0A;
  661. mf_crypto1_encrypt(&emulator->crypto, NULL, &ack, 4, tx_rx->tx_data, tx_rx->tx_parity);
  662. tx_rx->tx_rx_type = FuriHalNfcTxRxTransparent;
  663. tx_rx->tx_bits = 4;
  664. } else {
  665. // Unknown command
  666. break;
  667. }
  668. }
  669. if(!command_processed) {
  670. // Send NACK
  671. uint8_t nack = 0x04;
  672. if(is_encrypted) {
  673. mf_crypto1_encrypt(
  674. &emulator->crypto, NULL, &nack, 4, tx_rx->tx_data, tx_rx->tx_parity);
  675. } else {
  676. tx_rx->tx_data[0] = nack;
  677. }
  678. tx_rx->tx_rx_type = FuriHalNfcTxRxTransparent;
  679. tx_rx->tx_bits = 4;
  680. furi_hal_nfc_tx_rx(tx_rx, 300);
  681. }
  682. return true;
  683. }