mifare_classic.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746
  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, 0, sizeof(FuriHalNfcTxRxContext));
  246. do {
  247. if(key_type == MfClassicKeyA) {
  248. tx_rx->tx_data[0] = MF_CLASSIC_AUTH_KEY_A_CMD;
  249. } else {
  250. tx_rx->tx_data[0] = MF_CLASSIC_AUTH_KEY_B_CMD;
  251. }
  252. tx_rx->tx_data[1] = block;
  253. tx_rx->tx_rx_type = FuriHalNfcTxRxTypeRxNoCrc;
  254. tx_rx->tx_bits = 2 * 8;
  255. if(!furi_hal_nfc_tx_rx(tx_rx, 6)) break;
  256. uint32_t nt = (uint32_t)nfc_util_bytes2num(tx_rx->rx_data, 4);
  257. crypto1_init(crypto, key);
  258. crypto1_word(crypto, nt ^ cuid, 0);
  259. uint8_t nr[4] = {};
  260. nfc_util_num2bytes(prng_successor(DWT->CYCCNT, 32), 4, nr);
  261. for(uint8_t i = 0; i < 4; i++) {
  262. tx_rx->tx_data[i] = crypto1_byte(crypto, nr[i], 0) ^ nr[i];
  263. tx_rx->tx_parity[0] |=
  264. (((crypto1_filter(crypto->odd) ^ nfc_util_odd_parity8(nr[i])) & 0x01) << (7 - i));
  265. }
  266. nt = prng_successor(nt, 32);
  267. for(uint8_t i = 4; i < 8; i++) {
  268. nt = prng_successor(nt, 8);
  269. tx_rx->tx_data[i] = crypto1_byte(crypto, 0x00, 0) ^ (nt & 0xff);
  270. tx_rx->tx_parity[0] |=
  271. (((crypto1_filter(crypto->odd) ^ nfc_util_odd_parity8(nt & 0xff)) & 0x01)
  272. << (7 - i));
  273. }
  274. tx_rx->tx_rx_type = FuriHalNfcTxRxTypeRaw;
  275. tx_rx->tx_bits = 8 * 8;
  276. if(!furi_hal_nfc_tx_rx(tx_rx, 6)) break;
  277. if(tx_rx->rx_bits == 32) {
  278. crypto1_word(crypto, 0, 0);
  279. auth_success = true;
  280. }
  281. } while(false);
  282. return auth_success;
  283. }
  284. bool mf_classic_auth_attempt(
  285. FuriHalNfcTxRxContext* tx_rx,
  286. MfClassicAuthContext* auth_ctx,
  287. uint64_t key) {
  288. furi_assert(tx_rx);
  289. furi_assert(auth_ctx);
  290. bool found_key = false;
  291. bool need_halt = (auth_ctx->key_a == MF_CLASSIC_NO_KEY) &&
  292. (auth_ctx->key_b == MF_CLASSIC_NO_KEY);
  293. Crypto1 crypto;
  294. if(auth_ctx->key_a == MF_CLASSIC_NO_KEY) {
  295. // Try AUTH with key A
  296. if(mf_classic_auth(
  297. tx_rx,
  298. auth_ctx->cuid,
  299. mf_classic_get_first_block_num_of_sector(auth_ctx->sector),
  300. key,
  301. MfClassicKeyA,
  302. &crypto)) {
  303. auth_ctx->key_a = key;
  304. found_key = true;
  305. }
  306. }
  307. if(need_halt) {
  308. furi_hal_nfc_sleep();
  309. furi_hal_nfc_activate_nfca(300, &auth_ctx->cuid);
  310. }
  311. if(auth_ctx->key_b == MF_CLASSIC_NO_KEY) {
  312. // Try AUTH with key B
  313. if(mf_classic_auth(
  314. tx_rx,
  315. auth_ctx->cuid,
  316. mf_classic_get_first_block_num_of_sector(auth_ctx->sector),
  317. key,
  318. MfClassicKeyB,
  319. &crypto)) {
  320. auth_ctx->key_b = key;
  321. found_key = true;
  322. }
  323. }
  324. return found_key;
  325. }
  326. bool mf_classic_read_block(
  327. FuriHalNfcTxRxContext* tx_rx,
  328. Crypto1* crypto,
  329. uint8_t block_num,
  330. MfClassicBlock* block) {
  331. furi_assert(tx_rx);
  332. furi_assert(crypto);
  333. furi_assert(block);
  334. bool read_block_success = false;
  335. uint8_t plain_cmd[4] = {MF_CLASSIC_READ_SECT_CMD, block_num, 0x00, 0x00};
  336. nfca_append_crc16(plain_cmd, 2);
  337. memset(tx_rx, 0, sizeof(FuriHalNfcTxRxContext));
  338. for(uint8_t i = 0; i < 4; i++) {
  339. tx_rx->tx_data[i] = crypto1_byte(crypto, 0x00, 0) ^ plain_cmd[i];
  340. tx_rx->tx_parity[0] |=
  341. ((crypto1_filter(crypto->odd) ^ nfc_util_odd_parity8(plain_cmd[i])) & 0x01) << (7 - i);
  342. }
  343. tx_rx->tx_bits = 4 * 9;
  344. tx_rx->tx_rx_type = FuriHalNfcTxRxTypeRaw;
  345. if(furi_hal_nfc_tx_rx(tx_rx, 50)) {
  346. if(tx_rx->rx_bits == 8 * 18) {
  347. for(uint8_t i = 0; i < 18; i++) {
  348. block->value[i] = crypto1_byte(crypto, 0, 0) ^ tx_rx->rx_data[i];
  349. }
  350. read_block_success = true;
  351. }
  352. }
  353. return read_block_success;
  354. }
  355. bool mf_classic_read_sector(
  356. FuriHalNfcTxRxContext* tx_rx,
  357. Crypto1* crypto,
  358. MfClassicSectorReader* sector_reader,
  359. MfClassicSector* sector) {
  360. furi_assert(tx_rx);
  361. furi_assert(sector_reader);
  362. furi_assert(sector);
  363. uint32_t cuid = 0;
  364. uint64_t key;
  365. MfClassicKey key_type;
  366. uint8_t first_block;
  367. bool sector_read = false;
  368. furi_hal_nfc_sleep();
  369. do {
  370. // Activate card
  371. if(!furi_hal_nfc_activate_nfca(200, &cuid)) break;
  372. first_block = mf_classic_get_first_block_num_of_sector(sector_reader->sector_num);
  373. if(sector_reader->key_a != MF_CLASSIC_NO_KEY) {
  374. key = sector_reader->key_a;
  375. key_type = MfClassicKeyA;
  376. } else if(sector_reader->key_b != MF_CLASSIC_NO_KEY) {
  377. key = sector_reader->key_b;
  378. key_type = MfClassicKeyB;
  379. } else {
  380. break;
  381. }
  382. // Auth to first block in sector
  383. if(!mf_classic_auth(tx_rx, cuid, first_block, key, key_type, crypto)) break;
  384. sector->total_blocks = mf_classic_get_blocks_num_in_sector(sector_reader->sector_num);
  385. // Read blocks
  386. for(uint8_t i = 0; i < sector->total_blocks; i++) {
  387. mf_classic_read_block(tx_rx, crypto, first_block + i, &sector->block[i]);
  388. }
  389. // Save sector keys in last block
  390. if(sector_reader->key_a != MF_CLASSIC_NO_KEY) {
  391. nfc_util_num2bytes(
  392. sector_reader->key_a, 6, &sector->block[sector->total_blocks - 1].value[0]);
  393. }
  394. if(sector_reader->key_b != MF_CLASSIC_NO_KEY) {
  395. nfc_util_num2bytes(
  396. sector_reader->key_b, 6, &sector->block[sector->total_blocks - 1].value[10]);
  397. }
  398. sector_read = true;
  399. } while(false);
  400. return sector_read;
  401. }
  402. uint8_t mf_classic_read_card(
  403. FuriHalNfcTxRxContext* tx_rx,
  404. MfClassicReader* reader,
  405. MfClassicData* data) {
  406. furi_assert(tx_rx);
  407. furi_assert(reader);
  408. furi_assert(data);
  409. uint8_t sectors_read = 0;
  410. data->type = reader->type;
  411. data->key_a_mask = 0;
  412. data->key_b_mask = 0;
  413. MfClassicSector temp_sector = {};
  414. for(uint8_t i = 0; i < reader->sectors_to_read; i++) {
  415. if(mf_classic_read_sector(
  416. tx_rx, &reader->crypto, &reader->sector_reader[i], &temp_sector)) {
  417. uint8_t first_block =
  418. mf_classic_get_first_block_num_of_sector(reader->sector_reader[i].sector_num);
  419. for(uint8_t j = 0; j < temp_sector.total_blocks; j++) {
  420. data->block[first_block + j] = temp_sector.block[j];
  421. }
  422. if(reader->sector_reader[i].key_a != MF_CLASSIC_NO_KEY) {
  423. data->key_a_mask |= 1 << reader->sector_reader[i].sector_num;
  424. }
  425. if(reader->sector_reader[i].key_b != MF_CLASSIC_NO_KEY) {
  426. data->key_b_mask |= 1 << reader->sector_reader[i].sector_num;
  427. }
  428. sectors_read++;
  429. }
  430. }
  431. return sectors_read;
  432. }
  433. void mf_crypto1_decrypt(
  434. Crypto1* crypto,
  435. uint8_t* encrypted_data,
  436. uint16_t encrypted_data_bits,
  437. uint8_t* decrypted_data) {
  438. if(encrypted_data_bits < 8) {
  439. uint8_t decrypted_byte = 0;
  440. decrypted_byte |= (crypto1_bit(crypto, 0, 0) ^ FURI_BIT(encrypted_data[0], 0)) << 0;
  441. decrypted_byte |= (crypto1_bit(crypto, 0, 0) ^ FURI_BIT(encrypted_data[0], 1)) << 1;
  442. decrypted_byte |= (crypto1_bit(crypto, 0, 0) ^ FURI_BIT(encrypted_data[0], 2)) << 2;
  443. decrypted_byte |= (crypto1_bit(crypto, 0, 0) ^ FURI_BIT(encrypted_data[0], 3)) << 3;
  444. decrypted_data[0] = decrypted_byte;
  445. } else {
  446. for(size_t i = 0; i < encrypted_data_bits / 8; i++) {
  447. decrypted_data[i] = crypto1_byte(crypto, 0, 0) ^ encrypted_data[i];
  448. }
  449. }
  450. }
  451. void mf_crypto1_encrypt(
  452. Crypto1* crypto,
  453. uint8_t* keystream,
  454. uint8_t* plain_data,
  455. uint16_t plain_data_bits,
  456. uint8_t* encrypted_data,
  457. uint8_t* encrypted_parity) {
  458. if(plain_data_bits < 8) {
  459. encrypted_data[0] = 0;
  460. for(size_t i = 0; i < plain_data_bits; i++) {
  461. encrypted_data[0] |= (crypto1_bit(crypto, 0, 0) ^ FURI_BIT(plain_data[0], i)) << i;
  462. }
  463. } else {
  464. memset(encrypted_parity, 0, plain_data_bits / 8 + 1);
  465. for(uint8_t i = 0; i < plain_data_bits / 8; i++) {
  466. encrypted_data[i] = crypto1_byte(crypto, keystream ? keystream[i] : 0, 0) ^
  467. plain_data[i];
  468. encrypted_parity[i / 8] |=
  469. (((crypto1_filter(crypto->odd) ^ nfc_util_odd_parity8(plain_data[i])) & 0x01)
  470. << (7 - (i & 0x0007)));
  471. }
  472. }
  473. }
  474. bool mf_classic_emulator(MfClassicEmulator* emulator, FuriHalNfcTxRxContext* tx_rx) {
  475. furi_assert(emulator);
  476. furi_assert(tx_rx);
  477. bool command_processed = false;
  478. bool is_encrypted = false;
  479. uint8_t plain_data[MF_CLASSIC_MAX_DATA_SIZE];
  480. MfClassicKey access_key = MfClassicKeyA;
  481. // Read command
  482. while(!command_processed) {
  483. if(!is_encrypted) {
  484. // Read first frame
  485. tx_rx->tx_bits = 0;
  486. tx_rx->tx_rx_type = FuriHalNfcTxRxTypeDefault;
  487. }
  488. if(!furi_hal_nfc_tx_rx(tx_rx, 300)) {
  489. FURI_LOG_D(
  490. TAG, "Error in tx rx. Tx :%d bits, Rx: %d bits", tx_rx->tx_bits, tx_rx->rx_bits);
  491. break;
  492. }
  493. if(!is_encrypted) {
  494. memcpy(plain_data, tx_rx->rx_data, tx_rx->rx_bits / 8);
  495. } else {
  496. mf_crypto1_decrypt(&emulator->crypto, tx_rx->rx_data, tx_rx->rx_bits, plain_data);
  497. }
  498. // TODO Check crc
  499. if(plain_data[0] == 0x50 && plain_data[1] == 00) {
  500. FURI_LOG_T(TAG, "Halt received");
  501. command_processed = true;
  502. break;
  503. } else if(plain_data[0] == 0x60 || plain_data[0] == 0x61) {
  504. uint8_t block = plain_data[1];
  505. uint64_t key = 0;
  506. uint8_t sector_trailer_block = mf_classic_get_sector_trailer(block);
  507. MfClassicSectorTrailer* sector_trailer =
  508. (MfClassicSectorTrailer*)emulator->data.block[sector_trailer_block].value;
  509. if(plain_data[0] == 0x61) {
  510. key = nfc_util_bytes2num(sector_trailer->key_b, 6);
  511. access_key = MfClassicKeyA;
  512. } else {
  513. key = nfc_util_bytes2num(sector_trailer->key_a, 6);
  514. access_key = MfClassicKeyB;
  515. }
  516. uint32_t nonce = prng_successor(DWT->CYCCNT, 32);
  517. uint8_t nt[4];
  518. uint8_t nt_keystream[4];
  519. nfc_util_num2bytes(nonce, 4, nt);
  520. nfc_util_num2bytes(nonce ^ emulator->cuid, 4, nt_keystream);
  521. crypto1_init(&emulator->crypto, key);
  522. if(!is_encrypted) {
  523. crypto1_word(&emulator->crypto, emulator->cuid ^ nonce, 0);
  524. memcpy(tx_rx->tx_data, nt, sizeof(nt));
  525. tx_rx->tx_bits = sizeof(nt) * 8;
  526. tx_rx->tx_rx_type = FuriHalNfcTxRxTypeRxRaw;
  527. } else {
  528. mf_crypto1_encrypt(
  529. &emulator->crypto,
  530. nt_keystream,
  531. nt,
  532. sizeof(nt) * 8,
  533. tx_rx->tx_data,
  534. tx_rx->tx_parity);
  535. tx_rx->tx_bits = sizeof(nt) * 8;
  536. tx_rx->tx_rx_type = FuriHalNfcTxRxTransparent;
  537. }
  538. if(!furi_hal_nfc_tx_rx(tx_rx, 500)) {
  539. FURI_LOG_E(TAG, "Error in NT exchange");
  540. command_processed = true;
  541. break;
  542. }
  543. if(tx_rx->rx_bits != 64) {
  544. FURI_LOG_W(TAG, "Incorrect nr + ar");
  545. command_processed = true;
  546. break;
  547. }
  548. // Check if we store valid key
  549. if(access_key == MfClassicKeyA) {
  550. if(FURI_BIT(emulator->data.key_a_mask, mf_classic_get_sector_by_block(block)) ==
  551. 0) {
  552. FURI_LOG_D(TAG, "Unsupported sector key A for block %d", sector_trailer_block);
  553. break;
  554. }
  555. } else if(access_key == MfClassicKeyB) {
  556. if(FURI_BIT(emulator->data.key_b_mask, mf_classic_get_sector_by_block(block)) ==
  557. 0) {
  558. FURI_LOG_D(TAG, "Unsupported sector key B for block %d", sector_trailer_block);
  559. break;
  560. }
  561. }
  562. uint32_t nr = nfc_util_bytes2num(tx_rx->rx_data, 4);
  563. uint32_t ar = nfc_util_bytes2num(&tx_rx->rx_data[4], 4);
  564. crypto1_word(&emulator->crypto, nr, 1);
  565. uint32_t cardRr = ar ^ crypto1_word(&emulator->crypto, 0, 0);
  566. if(cardRr != prng_successor(nonce, 64)) {
  567. FURI_LOG_T(TAG, "Wrong AUTH! %08X != %08X", cardRr, prng_successor(nonce, 64));
  568. // Don't send NACK, as tag don't send it
  569. command_processed = true;
  570. break;
  571. }
  572. uint32_t ans = prng_successor(nonce, 96);
  573. uint8_t responce[4] = {};
  574. nfc_util_num2bytes(ans, 4, responce);
  575. mf_crypto1_encrypt(
  576. &emulator->crypto,
  577. NULL,
  578. responce,
  579. sizeof(responce) * 8,
  580. tx_rx->tx_data,
  581. tx_rx->tx_parity);
  582. tx_rx->tx_bits = sizeof(responce) * 8;
  583. tx_rx->tx_rx_type = FuriHalNfcTxRxTransparent;
  584. is_encrypted = true;
  585. } else if(is_encrypted && plain_data[0] == 0x30) {
  586. uint8_t block = plain_data[1];
  587. uint8_t block_data[18] = {};
  588. memcpy(block_data, emulator->data.block[block].value, MF_CLASSIC_BLOCK_SIZE);
  589. if(mf_classic_is_sector_trailer(block)) {
  590. if(!mf_classic_is_allowed_access(
  591. emulator, block, access_key, MfClassicActionKeyARead)) {
  592. memset(block_data, 0, 6);
  593. }
  594. if(!mf_classic_is_allowed_access(
  595. emulator, block, access_key, MfClassicActionKeyBRead)) {
  596. memset(&block_data[10], 0, 6);
  597. }
  598. if(!mf_classic_is_allowed_access(
  599. emulator, block, access_key, MfClassicActionACRead)) {
  600. memset(&block_data[6], 0, 4);
  601. }
  602. } else {
  603. if(!mf_classic_is_allowed_access(
  604. emulator, block, access_key, MfClassicActionDataRead)) {
  605. memset(block_data, 0, 16);
  606. }
  607. }
  608. nfca_append_crc16(block_data, 16);
  609. mf_crypto1_encrypt(
  610. &emulator->crypto,
  611. NULL,
  612. block_data,
  613. sizeof(block_data) * 8,
  614. tx_rx->tx_data,
  615. tx_rx->tx_parity);
  616. tx_rx->tx_bits = 18 * 8;
  617. tx_rx->tx_rx_type = FuriHalNfcTxRxTransparent;
  618. } else if(is_encrypted && plain_data[0] == 0xA0) {
  619. uint8_t block = plain_data[1];
  620. if(block > mf_classic_get_total_block_num(emulator->data.type)) {
  621. break;
  622. }
  623. // Send ACK
  624. uint8_t ack = 0x0A;
  625. mf_crypto1_encrypt(&emulator->crypto, NULL, &ack, 4, tx_rx->tx_data, tx_rx->tx_parity);
  626. tx_rx->tx_rx_type = FuriHalNfcTxRxTransparent;
  627. tx_rx->tx_bits = 4;
  628. if(!furi_hal_nfc_tx_rx(tx_rx, 300)) break;
  629. if(tx_rx->rx_bits != 18 * 8) break;
  630. mf_crypto1_decrypt(&emulator->crypto, tx_rx->rx_data, tx_rx->rx_bits, plain_data);
  631. uint8_t block_data[16] = {};
  632. memcpy(block_data, emulator->data.block[block].value, MF_CLASSIC_BLOCK_SIZE);
  633. if(mf_classic_is_sector_trailer(block)) {
  634. if(mf_classic_is_allowed_access(
  635. emulator, block, access_key, MfClassicActionKeyAWrite)) {
  636. memcpy(block_data, plain_data, 6);
  637. }
  638. if(mf_classic_is_allowed_access(
  639. emulator, block, access_key, MfClassicActionKeyBWrite)) {
  640. memcpy(&block_data[10], &plain_data[10], 6);
  641. }
  642. if(mf_classic_is_allowed_access(
  643. emulator, block, access_key, MfClassicActionACWrite)) {
  644. memcpy(&block_data[6], &plain_data[6], 4);
  645. }
  646. } else {
  647. if(mf_classic_is_allowed_access(
  648. emulator, block, access_key, MfClassicActionDataWrite)) {
  649. memcpy(block_data, plain_data, MF_CLASSIC_BLOCK_SIZE);
  650. }
  651. }
  652. if(memcmp(block_data, emulator->data.block[block].value, MF_CLASSIC_BLOCK_SIZE)) {
  653. memcpy(emulator->data.block[block].value, block_data, MF_CLASSIC_BLOCK_SIZE);
  654. emulator->data_changed = true;
  655. }
  656. // Send ACK
  657. ack = 0x0A;
  658. mf_crypto1_encrypt(&emulator->crypto, NULL, &ack, 4, tx_rx->tx_data, tx_rx->tx_parity);
  659. tx_rx->tx_rx_type = FuriHalNfcTxRxTransparent;
  660. tx_rx->tx_bits = 4;
  661. } else {
  662. // Unknown command
  663. break;
  664. }
  665. }
  666. if(!command_processed) {
  667. // Send NACK
  668. uint8_t nack = 0x04;
  669. if(is_encrypted) {
  670. mf_crypto1_encrypt(
  671. &emulator->crypto, NULL, &nack, 4, tx_rx->tx_data, tx_rx->tx_parity);
  672. } else {
  673. tx_rx->tx_data[0] = nack;
  674. }
  675. tx_rx->tx_rx_type = FuriHalNfcTxRxTransparent;
  676. tx_rx->tx_bits = 4;
  677. furi_hal_nfc_tx_rx(tx_rx, 300);
  678. }
  679. return true;
  680. }