gen2_poller_i.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629
  1. #include "gen2_poller_i.h"
  2. #include <nfc/helpers/iso14443_crc.h>
  3. #include <bit_lib/bit_lib.h>
  4. #include "furi_hal_random.h"
  5. #include <furi/furi.h>
  6. #define TAG "GEN2_I"
  7. MfClassicError mf_classic_process_error(Iso14443_3aError error) {
  8. MfClassicError ret = MfClassicErrorNone;
  9. switch(error) {
  10. case Iso14443_3aErrorNone:
  11. ret = MfClassicErrorNone;
  12. break;
  13. case Iso14443_3aErrorNotPresent:
  14. ret = MfClassicErrorNotPresent;
  15. break;
  16. case Iso14443_3aErrorColResFailed:
  17. case Iso14443_3aErrorCommunication:
  18. case Iso14443_3aErrorWrongCrc:
  19. ret = MfClassicErrorProtocol;
  20. break;
  21. case Iso14443_3aErrorTimeout:
  22. ret = MfClassicErrorTimeout;
  23. break;
  24. default:
  25. ret = MfClassicErrorProtocol;
  26. break;
  27. }
  28. return ret;
  29. }
  30. Gen2PollerError gen2_poller_process_iso3_error(Iso14443_3aError error) {
  31. Gen2PollerError ret = Gen2PollerErrorNone;
  32. switch(error) {
  33. case Iso14443_3aErrorNone:
  34. ret = Gen2PollerErrorNone;
  35. break;
  36. case Iso14443_3aErrorNotPresent:
  37. ret = Gen2PollerErrorNotPresent;
  38. break;
  39. case Iso14443_3aErrorWrongCrc:
  40. ret = Gen2PollerErrorProtocol;
  41. break;
  42. case Iso14443_3aErrorTimeout:
  43. ret = Gen2PollerErrorTimeout;
  44. break;
  45. default:
  46. ret = Gen2PollerErrorProtocol;
  47. break;
  48. }
  49. return ret;
  50. }
  51. Gen2PollerError gen2_poller_process_mifare_classic_error(MfClassicError error) {
  52. Gen2PollerError ret = Gen2PollerErrorNone;
  53. switch(error) {
  54. case MfClassicErrorNone:
  55. ret = Gen2PollerErrorNone;
  56. break;
  57. case MfClassicErrorNotPresent:
  58. ret = Gen2PollerErrorNotPresent;
  59. break;
  60. case MfClassicErrorProtocol:
  61. ret = Gen2PollerErrorProtocol;
  62. break;
  63. case MfClassicErrorAuth:
  64. ret = Gen2PollerErrorAuth;
  65. break;
  66. case MfClassicErrorTimeout:
  67. ret = Gen2PollerErrorTimeout;
  68. break;
  69. default:
  70. ret = Gen2PollerErrorProtocol;
  71. break;
  72. }
  73. return ret;
  74. }
  75. static Gen2PollerError gen2_poller_get_nt_common(
  76. Gen2Poller* instance,
  77. uint8_t block_num,
  78. MfClassicKeyType key_type,
  79. MfClassicNt* nt,
  80. bool is_nested) {
  81. MfClassicError ret = MfClassicErrorNone;
  82. Iso14443_3aError error = Iso14443_3aErrorNone;
  83. do {
  84. uint8_t auth_type = (key_type == MfClassicKeyTypeB) ? MF_CLASSIC_CMD_AUTH_KEY_B :
  85. MF_CLASSIC_CMD_AUTH_KEY_A;
  86. uint8_t auth_cmd[2] = {auth_type, block_num};
  87. bit_buffer_copy_bytes(instance->tx_plain_buffer, auth_cmd, sizeof(auth_cmd));
  88. if(is_nested) {
  89. iso14443_crc_append(Iso14443CrcTypeA, instance->tx_plain_buffer);
  90. crypto1_encrypt(
  91. instance->crypto, NULL, instance->tx_plain_buffer, instance->tx_encrypted_buffer);
  92. error = iso14443_3a_poller_txrx_custom_parity(
  93. instance->iso3_poller,
  94. instance->tx_encrypted_buffer,
  95. instance->rx_plain_buffer, // NT gets decrypted by mf_classic_async_auth
  96. GEN2_POLLER_MAX_FWT);
  97. if(error != Iso14443_3aErrorNone) {
  98. ret = mf_classic_process_error(error);
  99. break;
  100. }
  101. } else {
  102. FURI_LOG_D(TAG, "Plain auth cmd");
  103. error = iso14443_3a_poller_send_standard_frame(
  104. instance->iso3_poller,
  105. instance->tx_plain_buffer,
  106. instance->rx_plain_buffer,
  107. GEN2_POLLER_MAX_FWT);
  108. if(error != Iso14443_3aErrorWrongCrc) {
  109. ret = mf_classic_process_error(error);
  110. break;
  111. }
  112. }
  113. if(bit_buffer_get_size_bytes(instance->rx_plain_buffer) != sizeof(MfClassicNt)) {
  114. ret = MfClassicErrorProtocol;
  115. break;
  116. }
  117. if(nt) {
  118. bit_buffer_write_bytes(instance->rx_plain_buffer, nt->data, sizeof(MfClassicNt));
  119. }
  120. } while(false);
  121. return gen2_poller_process_mifare_classic_error(ret);
  122. }
  123. Gen2PollerError gen2_poller_get_nt(
  124. Gen2Poller* instance,
  125. uint8_t block_num,
  126. MfClassicKeyType key_type,
  127. MfClassicNt* nt) {
  128. return gen2_poller_get_nt_common(instance, block_num, key_type, nt, false);
  129. }
  130. Gen2PollerError gen2_poller_get_nt_nested(
  131. Gen2Poller* instance,
  132. uint8_t block_num,
  133. MfClassicKeyType key_type,
  134. MfClassicNt* nt) {
  135. return gen2_poller_get_nt_common(instance, block_num, key_type, nt, true);
  136. }
  137. static Gen2PollerError gen2_poller_auth_common(
  138. Gen2Poller* instance,
  139. uint8_t block_num,
  140. MfClassicKey* key,
  141. MfClassicKeyType key_type,
  142. MfClassicAuthContext* data,
  143. bool is_nested) {
  144. Gen2PollerError ret = Gen2PollerErrorNone;
  145. Iso14443_3aError error = Iso14443_3aErrorNone;
  146. do {
  147. iso14443_3a_copy(instance->data->iso14443_3a_data, nfc_poller_get_data(instance->poller));
  148. MfClassicNt nt = {};
  149. if(is_nested) {
  150. ret = gen2_poller_get_nt_nested(instance, block_num, key_type, &nt);
  151. } else {
  152. ret = gen2_poller_get_nt(instance, block_num, key_type, &nt);
  153. }
  154. if(ret != Gen2PollerErrorNone) break;
  155. if(data) {
  156. data->nt = nt;
  157. }
  158. uint32_t cuid = iso14443_3a_get_cuid(instance->data->iso14443_3a_data);
  159. uint64_t key_num = bit_lib_bytes_to_num_be(key->data, sizeof(MfClassicKey));
  160. MfClassicNr nr = {};
  161. furi_hal_random_fill_buf(nr.data, sizeof(MfClassicNr));
  162. crypto1_encrypt_reader_nonce(
  163. instance->crypto,
  164. key_num,
  165. cuid,
  166. nt.data,
  167. nr.data,
  168. instance->tx_encrypted_buffer,
  169. is_nested);
  170. error = iso14443_3a_poller_txrx_custom_parity(
  171. instance->iso3_poller,
  172. instance->tx_encrypted_buffer,
  173. instance->rx_encrypted_buffer,
  174. GEN2_POLLER_MAX_FWT);
  175. if(error != Iso14443_3aErrorNone) {
  176. ret = gen2_poller_process_iso3_error(error);
  177. break;
  178. }
  179. if(bit_buffer_get_size_bytes(instance->rx_encrypted_buffer) != 4) {
  180. ret = Gen2PollerErrorAuth;
  181. }
  182. crypto1_word(instance->crypto, 0, 0);
  183. instance->auth_state = Gen2AuthStatePassed;
  184. if(data) {
  185. data->nr = nr;
  186. const uint8_t* nr_ar = bit_buffer_get_data(instance->tx_encrypted_buffer);
  187. memcpy(data->ar.data, &nr_ar[4], sizeof(MfClassicAr));
  188. bit_buffer_write_bytes(
  189. instance->rx_encrypted_buffer, data->at.data, sizeof(MfClassicAt));
  190. }
  191. } while(false);
  192. if(ret != Gen2PollerErrorNone) {
  193. iso14443_3a_poller_halt(instance->iso3_poller);
  194. }
  195. return ret;
  196. }
  197. Gen2PollerError gen2_poller_auth(
  198. Gen2Poller* instance,
  199. uint8_t block_num,
  200. MfClassicKey* key,
  201. MfClassicKeyType key_type,
  202. MfClassicAuthContext* data) {
  203. return gen2_poller_auth_common(instance, block_num, key, key_type, data, false);
  204. }
  205. Gen2PollerError gen2_poller_halt(Gen2Poller* instance) {
  206. Gen2PollerError ret = Gen2PollerErrorNone;
  207. Iso14443_3aError error = Iso14443_3aErrorNone;
  208. do {
  209. uint8_t halt_cmd[2] = {MF_CLASSIC_CMD_HALT_MSB, MF_CLASSIC_CMD_HALT_LSB};
  210. bit_buffer_copy_bytes(instance->tx_plain_buffer, halt_cmd, sizeof(halt_cmd));
  211. iso14443_crc_append(Iso14443CrcTypeA, instance->tx_plain_buffer);
  212. if(instance->auth_state == Gen2AuthStatePassed) {
  213. // Send an encrypted halt command
  214. crypto1_encrypt(
  215. instance->crypto, NULL, instance->tx_plain_buffer, instance->tx_encrypted_buffer);
  216. FURI_LOG_D(TAG, "Send enc halt");
  217. error = iso14443_3a_poller_txrx_custom_parity(
  218. instance->iso3_poller,
  219. instance->tx_encrypted_buffer,
  220. instance->rx_encrypted_buffer,
  221. GEN2_POLLER_MAX_FWT);
  222. }
  223. if(error != Iso14443_3aErrorNone) {
  224. FURI_LOG_D(TAG, "Enc halt error");
  225. // Do not break because we still need to halt the iso3 poller
  226. }
  227. // Send a regular halt command to halt the iso3 poller
  228. FURI_LOG_D(TAG, "Send reg halt");
  229. error = iso14443_3a_poller_halt(instance->iso3_poller);
  230. if(error != Iso14443_3aErrorTimeout) {
  231. FURI_LOG_D(TAG, "Reg halt error");
  232. // Do not break as well becaue the first halt command might have worked
  233. // and the card didn't respond because it was already halted
  234. }
  235. crypto1_reset(instance->crypto);
  236. instance->auth_state = Gen2AuthStateIdle;
  237. } while(false);
  238. return ret;
  239. }
  240. Gen2PollerError
  241. gen2_poller_write_block(Gen2Poller* instance, uint8_t block_num, const MfClassicBlock* data) {
  242. Gen2PollerError ret = Gen2PollerErrorNone;
  243. Iso14443_3aError error = Iso14443_3aErrorNone;
  244. do {
  245. uint8_t write_block_cmd[2] = {MF_CLASSIC_CMD_WRITE_BLOCK, block_num};
  246. bit_buffer_copy_bytes(instance->tx_plain_buffer, write_block_cmd, sizeof(write_block_cmd));
  247. iso14443_crc_append(Iso14443CrcTypeA, instance->tx_plain_buffer);
  248. crypto1_encrypt(
  249. instance->crypto, NULL, instance->tx_plain_buffer, instance->tx_encrypted_buffer);
  250. error = iso14443_3a_poller_txrx_custom_parity(
  251. instance->iso3_poller,
  252. instance->tx_encrypted_buffer,
  253. instance->rx_encrypted_buffer,
  254. GEN2_POLLER_MAX_FWT);
  255. if(error != Iso14443_3aErrorNone) {
  256. ret = gen2_poller_process_iso3_error(error);
  257. break;
  258. }
  259. if(bit_buffer_get_size(instance->rx_encrypted_buffer) != 4) {
  260. ret = Gen2PollerErrorProtocol;
  261. break;
  262. }
  263. crypto1_decrypt(
  264. instance->crypto, instance->rx_encrypted_buffer, instance->rx_plain_buffer);
  265. if(bit_buffer_get_byte(instance->rx_plain_buffer, 0) != MF_CLASSIC_CMD_ACK) {
  266. FURI_LOG_D(TAG, "NACK received");
  267. ret = Gen2PollerErrorProtocol;
  268. break;
  269. }
  270. bit_buffer_copy_bytes(instance->tx_plain_buffer, data->data, sizeof(MfClassicBlock));
  271. iso14443_crc_append(Iso14443CrcTypeA, instance->tx_plain_buffer);
  272. crypto1_encrypt(
  273. instance->crypto, NULL, instance->tx_plain_buffer, instance->tx_encrypted_buffer);
  274. error = iso14443_3a_poller_txrx_custom_parity(
  275. instance->iso3_poller,
  276. instance->tx_encrypted_buffer,
  277. instance->rx_encrypted_buffer,
  278. GEN2_POLLER_MAX_FWT);
  279. if(error != Iso14443_3aErrorNone) {
  280. ret = gen2_poller_process_iso3_error(error);
  281. break;
  282. }
  283. if(bit_buffer_get_size(instance->rx_encrypted_buffer) != 4) {
  284. ret = Gen2PollerErrorProtocol;
  285. break;
  286. }
  287. crypto1_decrypt(
  288. instance->crypto, instance->rx_encrypted_buffer, instance->rx_plain_buffer);
  289. if(bit_buffer_get_byte(instance->rx_plain_buffer, 0) != MF_CLASSIC_CMD_ACK) {
  290. FURI_LOG_D(TAG, "NACK received");
  291. ret = Gen2PollerErrorProtocol;
  292. break;
  293. }
  294. } while(false);
  295. return ret;
  296. }
  297. bool gen2_poller_can_write_block(const MfClassicData* target_data, uint8_t block_num) {
  298. furi_assert(target_data);
  299. bool can_write = true;
  300. if(block_num == 0 && target_data->iso14443_3a_data->uid_len == 7) {
  301. // 7-byte UID gen2 cards are not supported yet, need further testing
  302. can_write = false;
  303. }
  304. if(mf_classic_is_sector_trailer(block_num)) {
  305. can_write = gen2_poller_can_write_sector_trailer(target_data, block_num).all_problems == 0;
  306. } else {
  307. can_write = gen2_poller_can_write_data_block(target_data, block_num).all_problems == 0;
  308. }
  309. return can_write;
  310. }
  311. Gen2PollerWriteProblems
  312. gen2_poller_can_write_data_block(const MfClassicData* target_data, uint8_t block_num) {
  313. // Check whether it's possible to write the block
  314. furi_assert(target_data);
  315. // Check rules:
  316. // 1. Check if block is read
  317. // 2. Check if we have any of the keys
  318. // 3. For each key, check if we can write the block
  319. // 3.1. If none of the keys can write the block, check whether access conditions can be reset to allow writing
  320. // 3.2 If the above conditions are not met, return an error code
  321. Gen2PollerWriteProblems can_write = {0};
  322. bool has_key_a = mf_classic_is_key_found(
  323. target_data, mf_classic_get_sector_by_block(block_num), MfClassicKeyTypeA);
  324. bool has_key_b = mf_classic_is_key_found(
  325. target_data, mf_classic_get_sector_by_block(block_num), MfClassicKeyTypeB);
  326. if(!has_key_a && !has_key_b) {
  327. can_write.missing_target_keys = true;
  328. }
  329. if(!gen2_is_allowed_access(
  330. target_data, block_num, MfClassicKeyTypeA, MfClassicActionDataWrite) &&
  331. !gen2_is_allowed_access(
  332. target_data, block_num, MfClassicKeyTypeB, MfClassicActionDataWrite)) {
  333. if(!gen2_can_reset_access_conditions(target_data, block_num)) {
  334. can_write.locked_access_bits = true;
  335. }
  336. }
  337. return can_write;
  338. }
  339. Gen2PollerWriteProblems
  340. gen2_poller_can_write_sector_trailer(const MfClassicData* target_data, uint8_t block_num) {
  341. // Check whether it's possible to write the sector trailer
  342. furi_assert(target_data);
  343. // Check rules:
  344. // 1. Check if block is read
  345. // 2. Check if we have any of the keys
  346. // 3. For each key, check if we can write the block
  347. // 3.1 Check that at least one of the keys can write Key A
  348. // 3.1.1 If none of the keys can write Key A, check whether access conditions can be reset to allow writing
  349. // 3.2 Check that at least one of the keys can write the Access Conditions
  350. // 3.3 Check that at least one of the keys can write Key B
  351. // 3.3.1 If none of the keys can write Key B, check whether access conditions can be reset to allow writing
  352. // 3.4 If any of the above conditions are not met, return an error code
  353. Gen2PollerWriteProblems can_write = {0};
  354. bool has_key_a = mf_classic_is_key_found(
  355. target_data, mf_classic_get_sector_by_block(block_num), MfClassicKeyTypeA);
  356. bool has_key_b = mf_classic_is_key_found(
  357. target_data, mf_classic_get_sector_by_block(block_num), MfClassicKeyTypeB);
  358. if(!has_key_a && !has_key_b) {
  359. can_write.missing_target_keys = true;
  360. }
  361. if(!gen2_is_allowed_access(
  362. target_data, block_num, MfClassicKeyTypeA, MfClassicActionKeyAWrite) &&
  363. !gen2_is_allowed_access(
  364. target_data, block_num, MfClassicKeyTypeB, MfClassicActionKeyAWrite)) {
  365. if(!gen2_can_reset_access_conditions(target_data, block_num)) {
  366. can_write.locked_access_bits = true;
  367. }
  368. }
  369. if(!gen2_is_allowed_access(target_data, block_num, MfClassicKeyTypeA, MfClassicActionACWrite) &&
  370. !gen2_is_allowed_access(target_data, block_num, MfClassicKeyTypeB, MfClassicActionACWrite)) {
  371. can_write.locked_access_bits = true;
  372. }
  373. if(!gen2_is_allowed_access(
  374. target_data, block_num, MfClassicKeyTypeA, MfClassicActionKeyBWrite) &&
  375. !gen2_is_allowed_access(
  376. target_data, block_num, MfClassicKeyTypeB, MfClassicActionKeyBWrite)) {
  377. if(!gen2_can_reset_access_conditions(target_data, block_num)) {
  378. can_write.locked_access_bits = true;
  379. }
  380. }
  381. return can_write;
  382. }
  383. bool gen2_can_reset_access_conditions(const MfClassicData* target_data, uint8_t block_num) {
  384. // Check whether it's possible to reset the access conditions
  385. furi_assert(target_data);
  386. // Check rules:
  387. // 1. Check if the sector trailer for this block is read
  388. // 2. Check if we have any of the keys
  389. // 3. For each key, check if we can write the access conditions
  390. // 3.1. If none of the keys can write the access conditions, return false
  391. bool can_reset = false;
  392. bool has_key_a = mf_classic_is_key_found(
  393. target_data, mf_classic_get_sector_by_block(block_num), MfClassicKeyTypeA);
  394. bool has_key_b = mf_classic_is_key_found(
  395. target_data, mf_classic_get_sector_by_block(block_num), MfClassicKeyTypeB);
  396. uint8_t sector_tr_num = mf_classic_get_sector_trailer_num_by_block(block_num);
  397. if(!mf_classic_is_block_read(target_data, sector_tr_num)) {
  398. can_reset = false;
  399. return can_reset;
  400. }
  401. if(!has_key_a && !has_key_b) {
  402. can_reset = false;
  403. return can_reset;
  404. }
  405. if(gen2_is_allowed_access(target_data, block_num, MfClassicKeyTypeA, MfClassicActionACWrite) ||
  406. gen2_is_allowed_access(target_data, block_num, MfClassicKeyTypeB, MfClassicActionACWrite)) {
  407. can_reset = true;
  408. }
  409. return can_reset;
  410. }
  411. MfClassicKeyType
  412. gen2_poller_get_key_type_to_write(const MfClassicData* target_data, uint8_t block_num) {
  413. // Get the key type to use for writing
  414. // We assume that at least one of the keys can write the block
  415. furi_assert(target_data);
  416. MfClassicKeyType key_type = MfClassicKeyTypeA;
  417. if(gen2_is_allowed_access(
  418. target_data, block_num, MfClassicKeyTypeA, MfClassicActionDataWrite)) {
  419. key_type = MfClassicKeyTypeA;
  420. } else if(gen2_is_allowed_access(
  421. target_data, block_num, MfClassicKeyTypeB, MfClassicActionDataWrite)) {
  422. key_type = MfClassicKeyTypeB;
  423. }
  424. return key_type;
  425. }
  426. static bool gen2_is_allowed_access_sector_trailer(
  427. const MfClassicData* data,
  428. uint8_t block_num,
  429. MfClassicKeyType key_type,
  430. MfClassicAction action) {
  431. uint8_t sector_num = mf_classic_get_sector_by_block(block_num);
  432. MfClassicSectorTrailer* sec_tr = mf_classic_get_sector_trailer_by_sector(data, sector_num);
  433. uint8_t* access_bits_arr = sec_tr->access_bits.data;
  434. uint8_t AC = ((access_bits_arr[1] >> 5) & 0x04) | ((access_bits_arr[2] >> 2) & 0x02) |
  435. ((access_bits_arr[2] >> 7) & 0x01);
  436. FURI_LOG_T("NFC", "AC: %02X", AC);
  437. switch(action) {
  438. case MfClassicActionKeyARead: {
  439. return false;
  440. }
  441. case MfClassicActionKeyAWrite:
  442. case MfClassicActionKeyBWrite: {
  443. return (
  444. (key_type == MfClassicKeyTypeA && (AC == 0x00 || AC == 0x01)) ||
  445. (key_type == MfClassicKeyTypeB &&
  446. (AC == 0x00 || AC == 0x04 || AC == 0x03 || AC == 0x01)));
  447. }
  448. case MfClassicActionKeyBRead: {
  449. return (key_type == MfClassicKeyTypeA && (AC == 0x00 || AC == 0x02 || AC == 0x01)) ||
  450. (key_type == MfClassicKeyTypeB && (AC == 0x00 || AC == 0x02 || AC == 0x01));
  451. }
  452. case MfClassicActionACRead: {
  453. return ((key_type == MfClassicKeyTypeA) || (key_type == MfClassicKeyTypeB));
  454. }
  455. case MfClassicActionACWrite: {
  456. return (
  457. (key_type == MfClassicKeyTypeA && (AC == 0x01)) ||
  458. (key_type == MfClassicKeyTypeB && (AC == 0x01 || AC == 0x03 || AC == 0x05)));
  459. }
  460. default:
  461. return false;
  462. }
  463. return true;
  464. }
  465. bool gen2_is_allowed_access_data_block(
  466. MfClassicSectorTrailer* sec_tr,
  467. uint8_t block_num,
  468. MfClassicKeyType key_type,
  469. MfClassicAction action) {
  470. // Same as mf_classic_is_allowed_access_data_block but with sector 0 allowed
  471. furi_assert(sec_tr);
  472. uint8_t* access_bits_arr = sec_tr->access_bits.data;
  473. uint8_t sector_block = 0;
  474. if(block_num <= 128) {
  475. sector_block = block_num & 0x03;
  476. } else {
  477. sector_block = (block_num & 0x0f) / 5;
  478. }
  479. uint8_t AC;
  480. switch(sector_block) {
  481. case 0x00: {
  482. AC = ((access_bits_arr[1] >> 2) & 0x04) | ((access_bits_arr[2] << 1) & 0x02) |
  483. ((access_bits_arr[2] >> 4) & 0x01);
  484. break;
  485. }
  486. case 0x01: {
  487. AC = ((access_bits_arr[1] >> 3) & 0x04) | ((access_bits_arr[2] >> 0) & 0x02) |
  488. ((access_bits_arr[2] >> 5) & 0x01);
  489. break;
  490. }
  491. case 0x02: {
  492. AC = ((access_bits_arr[1] >> 4) & 0x04) | ((access_bits_arr[2] >> 1) & 0x02) |
  493. ((access_bits_arr[2] >> 6) & 0x01);
  494. break;
  495. }
  496. default:
  497. return false;
  498. }
  499. switch(action) {
  500. case MfClassicActionDataRead: {
  501. return (
  502. (key_type == MfClassicKeyTypeA && !(AC == 0x03 || AC == 0x05 || AC == 0x07)) ||
  503. (key_type == MfClassicKeyTypeB && !(AC == 0x07)));
  504. }
  505. case MfClassicActionDataWrite: {
  506. return (
  507. (key_type == MfClassicKeyTypeA && (AC == 0x00)) ||
  508. (key_type == MfClassicKeyTypeB &&
  509. (AC == 0x00 || AC == 0x04 || AC == 0x06 || AC == 0x03)));
  510. }
  511. case MfClassicActionDataInc: {
  512. return (
  513. (key_type == MfClassicKeyTypeA && (AC == 0x00)) ||
  514. (key_type == MfClassicKeyTypeB && (AC == 0x00 || AC == 0x06)));
  515. }
  516. case MfClassicActionDataDec: {
  517. return (
  518. (key_type == MfClassicKeyTypeA && (AC == 0x00 || AC == 0x06 || AC == 0x01)) ||
  519. (key_type == MfClassicKeyTypeB && (AC == 0x00 || AC == 0x06 || AC == 0x01)));
  520. }
  521. default:
  522. return false;
  523. }
  524. return false;
  525. }
  526. bool gen2_is_allowed_access(
  527. const MfClassicData* data,
  528. uint8_t block_num,
  529. MfClassicKeyType key_type,
  530. MfClassicAction action) {
  531. // Same as mf_classic_is_allowed_access but with sector 0 allowed
  532. furi_assert(data);
  533. bool access_allowed = false;
  534. if(mf_classic_is_sector_trailer(block_num)) {
  535. access_allowed = gen2_is_allowed_access_sector_trailer(data, block_num, key_type, action);
  536. } else {
  537. uint8_t sector_num = mf_classic_get_sector_by_block(block_num);
  538. MfClassicSectorTrailer* sec_tr = mf_classic_get_sector_trailer_by_sector(data, sector_num);
  539. access_allowed = gen2_is_allowed_access_data_block(sec_tr, block_num, key_type, action);
  540. }
  541. return access_allowed;
  542. }