mifare_classic.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992
  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. const char* mf_classic_get_type_str(MfClassicType type) {
  23. if(type == MfClassicType1k) {
  24. return "MIFARE Classic 1K";
  25. } else if(type == MfClassicType4k) {
  26. return "MIFARE Classic 4K";
  27. } else {
  28. return "Unknown";
  29. }
  30. }
  31. static uint8_t mf_classic_get_first_block_num_of_sector(uint8_t sector) {
  32. furi_assert(sector < 40);
  33. if(sector < 32) {
  34. return sector * 4;
  35. } else {
  36. return 32 * 4 + (sector - 32) * 16;
  37. }
  38. }
  39. uint8_t mf_classic_get_sector_trailer_block_num_by_sector(uint8_t sector) {
  40. furi_assert(sector < 40);
  41. if(sector < 32) {
  42. return sector * 4 + 3;
  43. } else {
  44. return 32 * 4 + (sector - 32) * 16 + 15;
  45. }
  46. }
  47. uint8_t mf_classic_get_sector_by_block(uint8_t block) {
  48. if(block < 128) {
  49. return (block | 0x03) / 4;
  50. } else {
  51. return 32 + ((block | 0xf) - 32 * 4) / 16;
  52. }
  53. }
  54. static uint8_t mf_classic_get_blocks_num_in_sector(uint8_t sector) {
  55. furi_assert(sector < 40);
  56. return sector < 32 ? 4 : 16;
  57. }
  58. uint8_t mf_classic_get_sector_trailer_num_by_block(uint8_t block) {
  59. if(block < 128) {
  60. return block | 0x03;
  61. } else {
  62. return block | 0x0f;
  63. }
  64. }
  65. bool mf_classic_is_sector_trailer(uint8_t block) {
  66. return block == mf_classic_get_sector_trailer_num_by_block(block);
  67. }
  68. MfClassicSectorTrailer*
  69. mf_classic_get_sector_trailer_by_sector(MfClassicData* data, uint8_t sector) {
  70. furi_assert(data);
  71. uint8_t sec_tr_block_num = mf_classic_get_sector_trailer_block_num_by_sector(sector);
  72. return (MfClassicSectorTrailer*)data->block[sec_tr_block_num].value;
  73. }
  74. uint8_t mf_classic_get_total_sectors_num(MfClassicType type) {
  75. if(type == MfClassicType1k) {
  76. return MF_CLASSIC_1K_TOTAL_SECTORS_NUM;
  77. } else if(type == MfClassicType4k) {
  78. return MF_CLASSIC_4K_TOTAL_SECTORS_NUM;
  79. } else {
  80. return 0;
  81. }
  82. }
  83. static uint16_t mf_classic_get_total_block_num(MfClassicType type) {
  84. if(type == MfClassicType1k) {
  85. return 64;
  86. } else if(type == MfClassicType4k) {
  87. return 256;
  88. } else {
  89. return 0;
  90. }
  91. }
  92. bool mf_classic_is_block_read(MfClassicData* data, uint8_t block_num) {
  93. furi_assert(data);
  94. return (FURI_BIT(data->block_read_mask[block_num / 32], block_num % 32) == 1);
  95. }
  96. void mf_classic_set_block_read(MfClassicData* data, uint8_t block_num, MfClassicBlock* block_data) {
  97. furi_assert(data);
  98. if(mf_classic_is_sector_trailer(block_num)) {
  99. memcpy(&data->block[block_num].value[6], &block_data->value[6], 4);
  100. } else {
  101. memcpy(data->block[block_num].value, block_data->value, MF_CLASSIC_BLOCK_SIZE);
  102. }
  103. FURI_BIT_SET(data->block_read_mask[block_num / 32], block_num % 32);
  104. }
  105. bool mf_classic_is_key_found(MfClassicData* data, uint8_t sector_num, MfClassicKey key_type) {
  106. furi_assert(data);
  107. bool key_found = false;
  108. if(key_type == MfClassicKeyA) {
  109. key_found = (FURI_BIT(data->key_a_mask, sector_num) == 1);
  110. } else if(key_type == MfClassicKeyB) {
  111. key_found = (FURI_BIT(data->key_b_mask, sector_num) == 1);
  112. }
  113. return key_found;
  114. }
  115. void mf_classic_set_key_found(
  116. MfClassicData* data,
  117. uint8_t sector_num,
  118. MfClassicKey key_type,
  119. uint64_t key) {
  120. furi_assert(data);
  121. uint8_t key_arr[6] = {};
  122. MfClassicSectorTrailer* sec_trailer =
  123. mf_classic_get_sector_trailer_by_sector(data, sector_num);
  124. nfc_util_num2bytes(key, 6, key_arr);
  125. if(key_type == MfClassicKeyA) {
  126. memcpy(sec_trailer->key_a, key_arr, sizeof(sec_trailer->key_a));
  127. FURI_BIT_SET(data->key_a_mask, sector_num);
  128. } else if(key_type == MfClassicKeyB) {
  129. memcpy(sec_trailer->key_b, key_arr, sizeof(sec_trailer->key_b));
  130. FURI_BIT_SET(data->key_b_mask, sector_num);
  131. }
  132. }
  133. bool mf_classic_is_sector_read(MfClassicData* data, uint8_t sector_num) {
  134. furi_assert(data);
  135. bool sector_read = false;
  136. do {
  137. if(!mf_classic_is_key_found(data, sector_num, MfClassicKeyA)) break;
  138. if(!mf_classic_is_key_found(data, sector_num, MfClassicKeyB)) break;
  139. uint8_t start_block = mf_classic_get_first_block_num_of_sector(sector_num);
  140. uint8_t total_blocks = mf_classic_get_blocks_num_in_sector(sector_num);
  141. uint8_t block_read = true;
  142. for(size_t i = start_block; i < start_block + total_blocks; i++) {
  143. block_read = mf_classic_is_block_read(data, i);
  144. if(!block_read) break;
  145. }
  146. sector_read = block_read;
  147. } while(false);
  148. return sector_read;
  149. }
  150. void mf_classic_get_read_sectors_and_keys(
  151. MfClassicData* data,
  152. uint8_t* sectors_read,
  153. uint8_t* keys_found) {
  154. furi_assert(data);
  155. *sectors_read = 0;
  156. *keys_found = 0;
  157. uint8_t sectors_total = mf_classic_get_total_sectors_num(data->type);
  158. for(size_t i = 0; i < sectors_total; i++) {
  159. if(mf_classic_is_key_found(data, i, MfClassicKeyA)) {
  160. *keys_found += 1;
  161. }
  162. if(mf_classic_is_key_found(data, i, MfClassicKeyB)) {
  163. *keys_found += 1;
  164. }
  165. uint8_t first_block = mf_classic_get_first_block_num_of_sector(i);
  166. uint8_t total_blocks_in_sec = mf_classic_get_blocks_num_in_sector(i);
  167. bool blocks_read = true;
  168. for(size_t i = first_block; i < first_block + total_blocks_in_sec; i++) {
  169. blocks_read = mf_classic_is_block_read(data, i);
  170. if(!blocks_read) break;
  171. }
  172. if(blocks_read) {
  173. *sectors_read += 1;
  174. }
  175. }
  176. }
  177. static bool mf_classic_is_allowed_access_sector_trailer(
  178. MfClassicEmulator* emulator,
  179. uint8_t block_num,
  180. MfClassicKey key,
  181. MfClassicAction action) {
  182. uint8_t* sector_trailer = emulator->data.block[block_num].value;
  183. uint8_t AC = ((sector_trailer[7] >> 5) & 0x04) | ((sector_trailer[8] >> 2) & 0x02) |
  184. ((sector_trailer[8] >> 7) & 0x01);
  185. switch(action) {
  186. case MfClassicActionKeyARead: {
  187. return false;
  188. }
  189. case MfClassicActionKeyAWrite: {
  190. return (
  191. (key == MfClassicKeyA && (AC == 0x00 || AC == 0x01)) ||
  192. (key == MfClassicKeyB && (AC == 0x04 || AC == 0x03)));
  193. }
  194. case MfClassicActionKeyBRead: {
  195. return (key == MfClassicKeyA && (AC == 0x00 || AC == 0x02 || AC == 0x01));
  196. }
  197. case MfClassicActionKeyBWrite: {
  198. return (
  199. (key == MfClassicKeyA && (AC == 0x00 || AC == 0x01)) ||
  200. (key == MfClassicKeyB && (AC == 0x04 || AC == 0x03)));
  201. }
  202. case MfClassicActionACRead: {
  203. return (
  204. (key == MfClassicKeyA) ||
  205. (key == MfClassicKeyB && !(AC == 0x00 || AC == 0x02 || AC == 0x01)));
  206. }
  207. case MfClassicActionACWrite: {
  208. return (
  209. (key == MfClassicKeyA && (AC == 0x01)) ||
  210. (key == MfClassicKeyB && (AC == 0x03 || AC == 0x05)));
  211. }
  212. default:
  213. return false;
  214. }
  215. return true;
  216. }
  217. static bool mf_classic_is_allowed_access_data_block(
  218. MfClassicEmulator* emulator,
  219. uint8_t block_num,
  220. MfClassicKey key,
  221. MfClassicAction action) {
  222. uint8_t* sector_trailer =
  223. emulator->data.block[mf_classic_get_sector_trailer_num_by_block(block_num)].value;
  224. uint8_t sector_block;
  225. if(block_num <= 128) {
  226. sector_block = block_num & 0x03;
  227. } else {
  228. sector_block = (block_num & 0x0f) / 5;
  229. }
  230. uint8_t AC;
  231. switch(sector_block) {
  232. case 0x00: {
  233. AC = ((sector_trailer[7] >> 2) & 0x04) | ((sector_trailer[8] << 1) & 0x02) |
  234. ((sector_trailer[8] >> 4) & 0x01);
  235. break;
  236. }
  237. case 0x01: {
  238. AC = ((sector_trailer[7] >> 3) & 0x04) | ((sector_trailer[8] >> 0) & 0x02) |
  239. ((sector_trailer[8] >> 5) & 0x01);
  240. break;
  241. }
  242. case 0x02: {
  243. AC = ((sector_trailer[7] >> 4) & 0x04) | ((sector_trailer[8] >> 1) & 0x02) |
  244. ((sector_trailer[8] >> 6) & 0x01);
  245. break;
  246. }
  247. default:
  248. return false;
  249. }
  250. switch(action) {
  251. case MfClassicActionDataRead: {
  252. return (
  253. (key == MfClassicKeyA && !(AC == 0x03 || AC == 0x05 || AC == 0x07)) ||
  254. (key == MfClassicKeyB && !(AC == 0x07)));
  255. }
  256. case MfClassicActionDataWrite: {
  257. return (
  258. (key == MfClassicKeyA && (AC == 0x00)) ||
  259. (key == MfClassicKeyB && (AC == 0x00 || AC == 0x04 || AC == 0x06 || AC == 0x03)));
  260. }
  261. case MfClassicActionDataInc: {
  262. return (
  263. (key == MfClassicKeyA && (AC == 0x00)) ||
  264. (key == MfClassicKeyB && (AC == 0x00 || AC == 0x06)));
  265. }
  266. case MfClassicActionDataDec: {
  267. return (
  268. (key == MfClassicKeyA && (AC == 0x00 || AC == 0x06 || AC == 0x01)) ||
  269. (key == MfClassicKeyB && (AC == 0x00 || AC == 0x06 || AC == 0x01)));
  270. }
  271. default:
  272. return false;
  273. }
  274. return false;
  275. }
  276. static bool mf_classic_is_allowed_access(
  277. MfClassicEmulator* emulator,
  278. uint8_t block_num,
  279. MfClassicKey key,
  280. MfClassicAction action) {
  281. if(mf_classic_is_sector_trailer(block_num)) {
  282. return mf_classic_is_allowed_access_sector_trailer(emulator, block_num, key, action);
  283. } else {
  284. return mf_classic_is_allowed_access_data_block(emulator, block_num, key, action);
  285. }
  286. }
  287. bool mf_classic_check_card_type(uint8_t ATQA0, uint8_t ATQA1, uint8_t SAK) {
  288. UNUSED(ATQA1);
  289. if((ATQA0 == 0x44 || ATQA0 == 0x04) && (SAK == 0x08 || SAK == 0x88 || SAK == 0x09)) {
  290. return true;
  291. } else if((ATQA0 == 0x01) && (ATQA1 == 0x0F) && (SAK == 0x01)) {
  292. //skylanders support
  293. return true;
  294. } else if((ATQA0 == 0x42 || ATQA0 == 0x02) && (SAK == 0x18)) {
  295. return true;
  296. } else {
  297. return false;
  298. }
  299. }
  300. MfClassicType mf_classic_get_classic_type(int8_t ATQA0, uint8_t ATQA1, uint8_t SAK) {
  301. UNUSED(ATQA1);
  302. if((ATQA0 == 0x44 || ATQA0 == 0x04) && (SAK == 0x08 || SAK == 0x88 || SAK == 0x09)) {
  303. return MfClassicType1k;
  304. } else if((ATQA0 == 0x01) && (ATQA1 == 0x0F) && (SAK == 0x01)) {
  305. //skylanders support
  306. return MfClassicType1k;
  307. } else if((ATQA0 == 0x42 || ATQA0 == 0x02) && (SAK == 0x18)) {
  308. return MfClassicType4k;
  309. }
  310. return MfClassicType1k;
  311. }
  312. void mf_classic_reader_add_sector(
  313. MfClassicReader* reader,
  314. uint8_t sector,
  315. uint64_t key_a,
  316. uint64_t key_b) {
  317. furi_assert(reader);
  318. furi_assert(sector < MF_CLASSIC_SECTORS_MAX);
  319. furi_assert((key_a != MF_CLASSIC_NO_KEY) || (key_b != MF_CLASSIC_NO_KEY));
  320. if(reader->sectors_to_read < MF_CLASSIC_SECTORS_MAX) {
  321. reader->sector_reader[reader->sectors_to_read].key_a = key_a;
  322. reader->sector_reader[reader->sectors_to_read].key_b = key_b;
  323. reader->sector_reader[reader->sectors_to_read].sector_num = sector;
  324. reader->sectors_to_read++;
  325. }
  326. }
  327. void mf_classic_auth_init_context(MfClassicAuthContext* auth_ctx, uint8_t sector) {
  328. furi_assert(auth_ctx);
  329. auth_ctx->sector = sector;
  330. auth_ctx->key_a = MF_CLASSIC_NO_KEY;
  331. auth_ctx->key_b = MF_CLASSIC_NO_KEY;
  332. }
  333. static bool mf_classic_auth(
  334. FuriHalNfcTxRxContext* tx_rx,
  335. uint32_t block,
  336. uint64_t key,
  337. MfClassicKey key_type,
  338. Crypto1* crypto) {
  339. bool auth_success = false;
  340. uint32_t cuid = 0;
  341. memset(tx_rx->tx_data, 0, sizeof(tx_rx->tx_data));
  342. memset(tx_rx->tx_parity, 0, sizeof(tx_rx->tx_parity));
  343. tx_rx->tx_rx_type = FuriHalNfcTxRxTypeDefault;
  344. do {
  345. if(!furi_hal_nfc_activate_nfca(200, &cuid)) break;
  346. if(key_type == MfClassicKeyA) {
  347. tx_rx->tx_data[0] = MF_CLASSIC_AUTH_KEY_A_CMD;
  348. } else {
  349. tx_rx->tx_data[0] = MF_CLASSIC_AUTH_KEY_B_CMD;
  350. }
  351. tx_rx->tx_data[1] = block;
  352. tx_rx->tx_rx_type = FuriHalNfcTxRxTypeRxNoCrc;
  353. tx_rx->tx_bits = 2 * 8;
  354. if(!furi_hal_nfc_tx_rx(tx_rx, 6)) break;
  355. uint32_t nt = (uint32_t)nfc_util_bytes2num(tx_rx->rx_data, 4);
  356. crypto1_init(crypto, key);
  357. crypto1_word(crypto, nt ^ cuid, 0);
  358. uint8_t nr[4] = {};
  359. nfc_util_num2bytes(prng_successor(DWT->CYCCNT, 32), 4, nr);
  360. for(uint8_t i = 0; i < 4; i++) {
  361. tx_rx->tx_data[i] = crypto1_byte(crypto, nr[i], 0) ^ nr[i];
  362. tx_rx->tx_parity[0] |=
  363. (((crypto1_filter(crypto->odd) ^ nfc_util_odd_parity8(nr[i])) & 0x01) << (7 - i));
  364. }
  365. nt = prng_successor(nt, 32);
  366. for(uint8_t i = 4; i < 8; i++) {
  367. nt = prng_successor(nt, 8);
  368. tx_rx->tx_data[i] = crypto1_byte(crypto, 0x00, 0) ^ (nt & 0xff);
  369. tx_rx->tx_parity[0] |=
  370. (((crypto1_filter(crypto->odd) ^ nfc_util_odd_parity8(nt & 0xff)) & 0x01)
  371. << (7 - i));
  372. }
  373. tx_rx->tx_rx_type = FuriHalNfcTxRxTypeRaw;
  374. tx_rx->tx_bits = 8 * 8;
  375. if(!furi_hal_nfc_tx_rx(tx_rx, 6)) break;
  376. if(tx_rx->rx_bits == 32) {
  377. crypto1_word(crypto, 0, 0);
  378. auth_success = true;
  379. }
  380. } while(false);
  381. return auth_success;
  382. }
  383. bool mf_classic_authenticate(
  384. FuriHalNfcTxRxContext* tx_rx,
  385. uint8_t block_num,
  386. uint64_t key,
  387. MfClassicKey key_type) {
  388. furi_assert(tx_rx);
  389. Crypto1 crypto = {};
  390. bool key_found = mf_classic_auth(tx_rx, block_num, key, key_type, &crypto);
  391. furi_hal_nfc_sleep();
  392. return key_found;
  393. }
  394. bool mf_classic_auth_attempt(
  395. FuriHalNfcTxRxContext* tx_rx,
  396. MfClassicAuthContext* auth_ctx,
  397. uint64_t key) {
  398. furi_assert(tx_rx);
  399. furi_assert(auth_ctx);
  400. bool found_key = false;
  401. bool need_halt = (auth_ctx->key_a == MF_CLASSIC_NO_KEY) &&
  402. (auth_ctx->key_b == MF_CLASSIC_NO_KEY);
  403. Crypto1 crypto;
  404. if(auth_ctx->key_a == MF_CLASSIC_NO_KEY) {
  405. // Try AUTH with key A
  406. if(mf_classic_auth(
  407. tx_rx,
  408. mf_classic_get_first_block_num_of_sector(auth_ctx->sector),
  409. key,
  410. MfClassicKeyA,
  411. &crypto)) {
  412. auth_ctx->key_a = key;
  413. found_key = true;
  414. }
  415. }
  416. if(need_halt) {
  417. furi_hal_nfc_sleep();
  418. }
  419. if(auth_ctx->key_b == MF_CLASSIC_NO_KEY) {
  420. // Try AUTH with key B
  421. if(mf_classic_auth(
  422. tx_rx,
  423. mf_classic_get_first_block_num_of_sector(auth_ctx->sector),
  424. key,
  425. MfClassicKeyB,
  426. &crypto)) {
  427. auth_ctx->key_b = key;
  428. found_key = true;
  429. }
  430. }
  431. return found_key;
  432. }
  433. bool mf_classic_read_block(
  434. FuriHalNfcTxRxContext* tx_rx,
  435. Crypto1* crypto,
  436. uint8_t block_num,
  437. MfClassicBlock* block) {
  438. furi_assert(tx_rx);
  439. furi_assert(crypto);
  440. furi_assert(block);
  441. bool read_block_success = false;
  442. uint8_t plain_cmd[4] = {MF_CLASSIC_READ_SECT_CMD, block_num, 0x00, 0x00};
  443. nfca_append_crc16(plain_cmd, 2);
  444. memset(tx_rx->tx_data, 0, sizeof(tx_rx->tx_data));
  445. memset(tx_rx->tx_parity, 0, sizeof(tx_rx->tx_parity));
  446. for(uint8_t i = 0; i < 4; i++) {
  447. tx_rx->tx_data[i] = crypto1_byte(crypto, 0x00, 0) ^ plain_cmd[i];
  448. tx_rx->tx_parity[0] |=
  449. ((crypto1_filter(crypto->odd) ^ nfc_util_odd_parity8(plain_cmd[i])) & 0x01) << (7 - i);
  450. }
  451. tx_rx->tx_bits = 4 * 9;
  452. tx_rx->tx_rx_type = FuriHalNfcTxRxTypeRaw;
  453. if(furi_hal_nfc_tx_rx(tx_rx, 50)) {
  454. if(tx_rx->rx_bits == 8 * (MF_CLASSIC_BLOCK_SIZE + 2)) {
  455. uint8_t block_received[MF_CLASSIC_BLOCK_SIZE + 2];
  456. for(uint8_t i = 0; i < MF_CLASSIC_BLOCK_SIZE + 2; i++) {
  457. block_received[i] = crypto1_byte(crypto, 0, 0) ^ tx_rx->rx_data[i];
  458. }
  459. uint16_t crc_calc = nfca_get_crc16(block_received, MF_CLASSIC_BLOCK_SIZE);
  460. uint16_t crc_received = (block_received[MF_CLASSIC_BLOCK_SIZE + 1] << 8) |
  461. block_received[MF_CLASSIC_BLOCK_SIZE];
  462. if(crc_received != crc_calc) {
  463. FURI_LOG_E(
  464. TAG,
  465. "Incorrect CRC while reading block %d. Expected %04X, Received %04X",
  466. block_num,
  467. crc_received,
  468. crc_calc);
  469. } else {
  470. memcpy(block->value, block_received, MF_CLASSIC_BLOCK_SIZE);
  471. read_block_success = true;
  472. }
  473. }
  474. }
  475. return read_block_success;
  476. }
  477. void mf_classic_read_sector(FuriHalNfcTxRxContext* tx_rx, MfClassicData* data, uint8_t sec_num) {
  478. furi_assert(tx_rx);
  479. furi_assert(data);
  480. furi_hal_nfc_sleep();
  481. bool key_a_found = mf_classic_is_key_found(data, sec_num, MfClassicKeyA);
  482. bool key_b_found = mf_classic_is_key_found(data, sec_num, MfClassicKeyB);
  483. uint8_t start_block = mf_classic_get_first_block_num_of_sector(sec_num);
  484. uint8_t total_blocks = mf_classic_get_blocks_num_in_sector(sec_num);
  485. MfClassicBlock block_tmp = {};
  486. uint64_t key = 0;
  487. MfClassicSectorTrailer* sec_tr = mf_classic_get_sector_trailer_by_sector(data, sec_num);
  488. Crypto1 crypto = {};
  489. uint8_t blocks_read = 0;
  490. do {
  491. if(!key_a_found) break;
  492. FURI_LOG_D(TAG, "Try to read blocks with key A");
  493. key = nfc_util_bytes2num(sec_tr->key_a, sizeof(sec_tr->key_a));
  494. if(!mf_classic_auth(tx_rx, start_block, key, MfClassicKeyA, &crypto)) break;
  495. for(size_t i = start_block; i < start_block + total_blocks; i++) {
  496. if(!mf_classic_is_block_read(data, i)) {
  497. if(mf_classic_read_block(tx_rx, &crypto, i, &block_tmp)) {
  498. mf_classic_set_block_read(data, i, &block_tmp);
  499. blocks_read++;
  500. }
  501. } else {
  502. blocks_read++;
  503. }
  504. }
  505. FURI_LOG_D(TAG, "Read %d blocks out of %d", blocks_read, total_blocks);
  506. } while(false);
  507. do {
  508. if(blocks_read == total_blocks) break;
  509. if(!key_b_found) break;
  510. FURI_LOG_D(TAG, "Try to read blocks with key B");
  511. key = nfc_util_bytes2num(sec_tr->key_b, sizeof(sec_tr->key_b));
  512. furi_hal_nfc_sleep();
  513. if(!mf_classic_auth(tx_rx, start_block, key, MfClassicKeyB, &crypto)) break;
  514. for(size_t i = start_block; i < start_block + total_blocks; i++) {
  515. if(!mf_classic_is_block_read(data, i)) {
  516. if(mf_classic_read_block(tx_rx, &crypto, i, &block_tmp)) {
  517. mf_classic_set_block_read(data, i, &block_tmp);
  518. blocks_read++;
  519. }
  520. } else {
  521. blocks_read++;
  522. }
  523. }
  524. FURI_LOG_D(TAG, "Read %d blocks out of %d", blocks_read, total_blocks);
  525. } while(false);
  526. }
  527. static bool mf_classic_read_sector_with_reader(
  528. FuriHalNfcTxRxContext* tx_rx,
  529. Crypto1* crypto,
  530. MfClassicSectorReader* sector_reader,
  531. MfClassicSector* sector) {
  532. furi_assert(tx_rx);
  533. furi_assert(sector_reader);
  534. furi_assert(sector);
  535. uint64_t key;
  536. MfClassicKey key_type;
  537. uint8_t first_block;
  538. bool sector_read = false;
  539. furi_hal_nfc_sleep();
  540. do {
  541. // Activate card
  542. first_block = mf_classic_get_first_block_num_of_sector(sector_reader->sector_num);
  543. if(sector_reader->key_a != MF_CLASSIC_NO_KEY) {
  544. key = sector_reader->key_a;
  545. key_type = MfClassicKeyA;
  546. } else if(sector_reader->key_b != MF_CLASSIC_NO_KEY) {
  547. key = sector_reader->key_b;
  548. key_type = MfClassicKeyB;
  549. } else {
  550. break;
  551. }
  552. // Auth to first block in sector
  553. if(!mf_classic_auth(tx_rx, first_block, key, key_type, crypto)) break;
  554. sector->total_blocks = mf_classic_get_blocks_num_in_sector(sector_reader->sector_num);
  555. // Read blocks
  556. for(uint8_t i = 0; i < sector->total_blocks; i++) {
  557. mf_classic_read_block(tx_rx, crypto, first_block + i, &sector->block[i]);
  558. }
  559. // Save sector keys in last block
  560. if(sector_reader->key_a != MF_CLASSIC_NO_KEY) {
  561. nfc_util_num2bytes(
  562. sector_reader->key_a, 6, &sector->block[sector->total_blocks - 1].value[0]);
  563. }
  564. if(sector_reader->key_b != MF_CLASSIC_NO_KEY) {
  565. nfc_util_num2bytes(
  566. sector_reader->key_b, 6, &sector->block[sector->total_blocks - 1].value[10]);
  567. }
  568. sector_read = true;
  569. } while(false);
  570. return sector_read;
  571. }
  572. uint8_t mf_classic_read_card(
  573. FuriHalNfcTxRxContext* tx_rx,
  574. MfClassicReader* reader,
  575. MfClassicData* data) {
  576. furi_assert(tx_rx);
  577. furi_assert(reader);
  578. furi_assert(data);
  579. uint8_t sectors_read = 0;
  580. data->type = reader->type;
  581. data->key_a_mask = 0;
  582. data->key_b_mask = 0;
  583. MfClassicSector temp_sector = {};
  584. for(uint8_t i = 0; i < reader->sectors_to_read; i++) {
  585. if(mf_classic_read_sector_with_reader(
  586. tx_rx, &reader->crypto, &reader->sector_reader[i], &temp_sector)) {
  587. uint8_t first_block =
  588. mf_classic_get_first_block_num_of_sector(reader->sector_reader[i].sector_num);
  589. for(uint8_t j = 0; j < temp_sector.total_blocks; j++) {
  590. mf_classic_set_block_read(data, first_block + j, &temp_sector.block[j]);
  591. }
  592. if(reader->sector_reader[i].key_a != MF_CLASSIC_NO_KEY) {
  593. mf_classic_set_key_found(
  594. data,
  595. reader->sector_reader[i].sector_num,
  596. MfClassicKeyA,
  597. reader->sector_reader[i].key_a);
  598. }
  599. if(reader->sector_reader[i].key_b != MF_CLASSIC_NO_KEY) {
  600. mf_classic_set_key_found(
  601. data,
  602. reader->sector_reader[i].sector_num,
  603. MfClassicKeyB,
  604. reader->sector_reader[i].key_b);
  605. }
  606. sectors_read++;
  607. }
  608. }
  609. return sectors_read;
  610. }
  611. uint8_t mf_classic_update_card(FuriHalNfcTxRxContext* tx_rx, MfClassicData* data) {
  612. furi_assert(tx_rx);
  613. furi_assert(data);
  614. uint8_t sectors_read = 0;
  615. Crypto1 crypto = {};
  616. uint8_t total_sectors = mf_classic_get_total_sectors_num(data->type);
  617. uint64_t key_a = 0;
  618. uint64_t key_b = 0;
  619. MfClassicSectorReader sec_reader = {};
  620. MfClassicSector temp_sector = {};
  621. for(size_t i = 0; i < total_sectors; i++) {
  622. MfClassicSectorTrailer* sec_tr = mf_classic_get_sector_trailer_by_sector(data, i);
  623. // Load key A
  624. if(mf_classic_is_key_found(data, i, MfClassicKeyA)) {
  625. sec_reader.key_a = nfc_util_bytes2num(sec_tr->key_a, 6);
  626. } else {
  627. sec_reader.key_a = MF_CLASSIC_NO_KEY;
  628. }
  629. // Load key B
  630. if(mf_classic_is_key_found(data, i, MfClassicKeyB)) {
  631. sec_reader.key_b = nfc_util_bytes2num(sec_tr->key_b, 6);
  632. } else {
  633. sec_reader.key_b = MF_CLASSIC_NO_KEY;
  634. }
  635. if((key_a != MF_CLASSIC_NO_KEY) || (key_b != MF_CLASSIC_NO_KEY)) {
  636. sec_reader.sector_num = i;
  637. if(mf_classic_read_sector_with_reader(tx_rx, &crypto, &sec_reader, &temp_sector)) {
  638. uint8_t first_block = mf_classic_get_first_block_num_of_sector(i);
  639. for(uint8_t j = 0; j < temp_sector.total_blocks; j++) {
  640. mf_classic_set_block_read(data, first_block + j, &temp_sector.block[j]);
  641. }
  642. sectors_read++;
  643. }
  644. }
  645. }
  646. return sectors_read;
  647. }
  648. void mf_crypto1_decrypt(
  649. Crypto1* crypto,
  650. uint8_t* encrypted_data,
  651. uint16_t encrypted_data_bits,
  652. uint8_t* decrypted_data) {
  653. if(encrypted_data_bits < 8) {
  654. uint8_t decrypted_byte = 0;
  655. decrypted_byte |= (crypto1_bit(crypto, 0, 0) ^ FURI_BIT(encrypted_data[0], 0)) << 0;
  656. decrypted_byte |= (crypto1_bit(crypto, 0, 0) ^ FURI_BIT(encrypted_data[0], 1)) << 1;
  657. decrypted_byte |= (crypto1_bit(crypto, 0, 0) ^ FURI_BIT(encrypted_data[0], 2)) << 2;
  658. decrypted_byte |= (crypto1_bit(crypto, 0, 0) ^ FURI_BIT(encrypted_data[0], 3)) << 3;
  659. decrypted_data[0] = decrypted_byte;
  660. } else {
  661. for(size_t i = 0; i < encrypted_data_bits / 8; i++) {
  662. decrypted_data[i] = crypto1_byte(crypto, 0, 0) ^ encrypted_data[i];
  663. }
  664. }
  665. }
  666. void mf_crypto1_encrypt(
  667. Crypto1* crypto,
  668. uint8_t* keystream,
  669. uint8_t* plain_data,
  670. uint16_t plain_data_bits,
  671. uint8_t* encrypted_data,
  672. uint8_t* encrypted_parity) {
  673. if(plain_data_bits < 8) {
  674. encrypted_data[0] = 0;
  675. for(size_t i = 0; i < plain_data_bits; i++) {
  676. encrypted_data[0] |= (crypto1_bit(crypto, 0, 0) ^ FURI_BIT(plain_data[0], i)) << i;
  677. }
  678. } else {
  679. memset(encrypted_parity, 0, plain_data_bits / 8 + 1);
  680. for(uint8_t i = 0; i < plain_data_bits / 8; i++) {
  681. encrypted_data[i] = crypto1_byte(crypto, keystream ? keystream[i] : 0, 0) ^
  682. plain_data[i];
  683. encrypted_parity[i / 8] |=
  684. (((crypto1_filter(crypto->odd) ^ nfc_util_odd_parity8(plain_data[i])) & 0x01)
  685. << (7 - (i & 0x0007)));
  686. }
  687. }
  688. }
  689. bool mf_classic_emulator(MfClassicEmulator* emulator, FuriHalNfcTxRxContext* tx_rx) {
  690. furi_assert(emulator);
  691. furi_assert(tx_rx);
  692. bool command_processed = false;
  693. bool is_encrypted = false;
  694. uint8_t plain_data[MF_CLASSIC_MAX_DATA_SIZE];
  695. MfClassicKey access_key = MfClassicKeyA;
  696. // Read command
  697. while(!command_processed) {
  698. if(!is_encrypted) {
  699. crypto1_reset(&emulator->crypto);
  700. memcpy(plain_data, tx_rx->rx_data, tx_rx->rx_bits / 8);
  701. } else {
  702. if(!furi_hal_nfc_tx_rx(tx_rx, 300)) {
  703. FURI_LOG_D(
  704. TAG,
  705. "Error in tx rx. Tx :%d bits, Rx: %d bits",
  706. tx_rx->tx_bits,
  707. tx_rx->rx_bits);
  708. break;
  709. }
  710. mf_crypto1_decrypt(&emulator->crypto, tx_rx->rx_data, tx_rx->rx_bits, plain_data);
  711. }
  712. if(plain_data[0] == 0x50 && plain_data[1] == 0x00) {
  713. FURI_LOG_T(TAG, "Halt received");
  714. furi_hal_nfc_listen_sleep();
  715. command_processed = true;
  716. break;
  717. } else if(plain_data[0] == 0x60 || plain_data[0] == 0x61) {
  718. uint8_t block = plain_data[1];
  719. uint64_t key = 0;
  720. uint8_t sector_trailer_block = mf_classic_get_sector_trailer_num_by_block(block);
  721. MfClassicSectorTrailer* sector_trailer =
  722. (MfClassicSectorTrailer*)emulator->data.block[sector_trailer_block].value;
  723. if(plain_data[0] == 0x60) {
  724. key = nfc_util_bytes2num(sector_trailer->key_a, 6);
  725. access_key = MfClassicKeyA;
  726. } else {
  727. key = nfc_util_bytes2num(sector_trailer->key_b, 6);
  728. access_key = MfClassicKeyB;
  729. }
  730. uint32_t nonce = prng_successor(DWT->CYCCNT, 32) ^ 0xAA;
  731. uint8_t nt[4];
  732. uint8_t nt_keystream[4];
  733. nfc_util_num2bytes(nonce, 4, nt);
  734. nfc_util_num2bytes(nonce ^ emulator->cuid, 4, nt_keystream);
  735. crypto1_init(&emulator->crypto, key);
  736. if(!is_encrypted) {
  737. crypto1_word(&emulator->crypto, emulator->cuid ^ nonce, 0);
  738. memcpy(tx_rx->tx_data, nt, sizeof(nt));
  739. tx_rx->tx_parity[0] = 0;
  740. for(size_t i = 0; i < sizeof(nt); i++) {
  741. tx_rx->tx_parity[0] |= nfc_util_odd_parity8(nt[i]) << (7 - i);
  742. }
  743. tx_rx->tx_bits = sizeof(nt) * 8;
  744. tx_rx->tx_rx_type = FuriHalNfcTxRxTransparent;
  745. } else {
  746. mf_crypto1_encrypt(
  747. &emulator->crypto,
  748. nt_keystream,
  749. nt,
  750. sizeof(nt) * 8,
  751. tx_rx->tx_data,
  752. tx_rx->tx_parity);
  753. tx_rx->tx_bits = sizeof(nt) * 8;
  754. tx_rx->tx_rx_type = FuriHalNfcTxRxTransparent;
  755. }
  756. if(!furi_hal_nfc_tx_rx(tx_rx, 500)) {
  757. FURI_LOG_E(TAG, "Error in NT exchange");
  758. command_processed = true;
  759. break;
  760. }
  761. if(tx_rx->rx_bits != 64) {
  762. FURI_LOG_W(TAG, "Incorrect nr + ar");
  763. command_processed = true;
  764. break;
  765. }
  766. uint32_t nr = nfc_util_bytes2num(tx_rx->rx_data, 4);
  767. uint32_t ar = nfc_util_bytes2num(&tx_rx->rx_data[4], 4);
  768. FURI_LOG_D(
  769. TAG,
  770. "%08lx key%c block %d nt/nr/ar: %08lx %08lx %08lx",
  771. emulator->cuid,
  772. access_key == MfClassicKeyA ? 'A' : 'B',
  773. sector_trailer_block,
  774. nonce,
  775. nr,
  776. ar);
  777. crypto1_word(&emulator->crypto, nr, 1);
  778. uint32_t cardRr = ar ^ crypto1_word(&emulator->crypto, 0, 0);
  779. if(cardRr != prng_successor(nonce, 64)) {
  780. FURI_LOG_T(TAG, "Wrong AUTH! %08lX != %08lX", cardRr, prng_successor(nonce, 64));
  781. // Don't send NACK, as the tag doesn't send it
  782. command_processed = true;
  783. break;
  784. }
  785. uint32_t ans = prng_successor(nonce, 96);
  786. uint8_t responce[4] = {};
  787. nfc_util_num2bytes(ans, 4, responce);
  788. mf_crypto1_encrypt(
  789. &emulator->crypto,
  790. NULL,
  791. responce,
  792. sizeof(responce) * 8,
  793. tx_rx->tx_data,
  794. tx_rx->tx_parity);
  795. tx_rx->tx_bits = sizeof(responce) * 8;
  796. tx_rx->tx_rx_type = FuriHalNfcTxRxTransparent;
  797. is_encrypted = true;
  798. } else if(is_encrypted && plain_data[0] == 0x30) {
  799. uint8_t block = plain_data[1];
  800. uint8_t block_data[18] = {};
  801. memcpy(block_data, emulator->data.block[block].value, MF_CLASSIC_BLOCK_SIZE);
  802. if(mf_classic_is_sector_trailer(block)) {
  803. if(!mf_classic_is_allowed_access(
  804. emulator, block, access_key, MfClassicActionKeyARead)) {
  805. memset(block_data, 0, 6);
  806. }
  807. if(!mf_classic_is_allowed_access(
  808. emulator, block, access_key, MfClassicActionKeyBRead)) {
  809. memset(&block_data[10], 0, 6);
  810. }
  811. if(!mf_classic_is_allowed_access(
  812. emulator, block, access_key, MfClassicActionACRead)) {
  813. memset(&block_data[6], 0, 4);
  814. }
  815. } else {
  816. if(!mf_classic_is_allowed_access(
  817. emulator, block, access_key, MfClassicActionDataRead)) {
  818. // Send NACK
  819. uint8_t nack = 0x04;
  820. if(is_encrypted) {
  821. mf_crypto1_encrypt(
  822. &emulator->crypto, NULL, &nack, 4, tx_rx->tx_data, tx_rx->tx_parity);
  823. } else {
  824. tx_rx->tx_data[0] = nack;
  825. }
  826. tx_rx->tx_rx_type = FuriHalNfcTxRxTransparent;
  827. tx_rx->tx_bits = 4;
  828. furi_hal_nfc_tx_rx(tx_rx, 300);
  829. break;
  830. }
  831. }
  832. nfca_append_crc16(block_data, 16);
  833. mf_crypto1_encrypt(
  834. &emulator->crypto,
  835. NULL,
  836. block_data,
  837. sizeof(block_data) * 8,
  838. tx_rx->tx_data,
  839. tx_rx->tx_parity);
  840. tx_rx->tx_bits = 18 * 8;
  841. tx_rx->tx_rx_type = FuriHalNfcTxRxTransparent;
  842. } else if(is_encrypted && plain_data[0] == 0xA0) {
  843. uint8_t block = plain_data[1];
  844. if(block > mf_classic_get_total_block_num(emulator->data.type)) {
  845. break;
  846. }
  847. // Send ACK
  848. uint8_t ack = 0x0A;
  849. mf_crypto1_encrypt(&emulator->crypto, NULL, &ack, 4, tx_rx->tx_data, tx_rx->tx_parity);
  850. tx_rx->tx_rx_type = FuriHalNfcTxRxTransparent;
  851. tx_rx->tx_bits = 4;
  852. if(!furi_hal_nfc_tx_rx(tx_rx, 300)) break;
  853. if(tx_rx->rx_bits != 18 * 8) break;
  854. mf_crypto1_decrypt(&emulator->crypto, tx_rx->rx_data, tx_rx->rx_bits, plain_data);
  855. uint8_t block_data[16] = {};
  856. memcpy(block_data, emulator->data.block[block].value, MF_CLASSIC_BLOCK_SIZE);
  857. if(mf_classic_is_sector_trailer(block)) {
  858. if(mf_classic_is_allowed_access(
  859. emulator, block, access_key, MfClassicActionKeyAWrite)) {
  860. memcpy(block_data, plain_data, 6);
  861. }
  862. if(mf_classic_is_allowed_access(
  863. emulator, block, access_key, MfClassicActionKeyBWrite)) {
  864. memcpy(&block_data[10], &plain_data[10], 6);
  865. }
  866. if(mf_classic_is_allowed_access(
  867. emulator, block, access_key, MfClassicActionACWrite)) {
  868. memcpy(&block_data[6], &plain_data[6], 4);
  869. }
  870. } else {
  871. if(mf_classic_is_allowed_access(
  872. emulator, block, access_key, MfClassicActionDataWrite)) {
  873. memcpy(block_data, plain_data, MF_CLASSIC_BLOCK_SIZE);
  874. }
  875. }
  876. if(memcmp(block_data, emulator->data.block[block].value, MF_CLASSIC_BLOCK_SIZE)) {
  877. memcpy(emulator->data.block[block].value, block_data, MF_CLASSIC_BLOCK_SIZE);
  878. emulator->data_changed = true;
  879. }
  880. // Send ACK
  881. ack = 0x0A;
  882. mf_crypto1_encrypt(&emulator->crypto, NULL, &ack, 4, tx_rx->tx_data, tx_rx->tx_parity);
  883. tx_rx->tx_rx_type = FuriHalNfcTxRxTransparent;
  884. tx_rx->tx_bits = 4;
  885. } else {
  886. // Unknown command
  887. break;
  888. }
  889. }
  890. if(!command_processed) {
  891. // Send NACK
  892. uint8_t nack = 0x04;
  893. if(is_encrypted) {
  894. mf_crypto1_encrypt(
  895. &emulator->crypto, NULL, &nack, 4, tx_rx->tx_data, tx_rx->tx_parity);
  896. } else {
  897. tx_rx->tx_data[0] = nack;
  898. }
  899. tx_rx->tx_rx_type = FuriHalNfcTxRxTransparent;
  900. tx_rx->tx_bits = 4;
  901. furi_hal_nfc_tx_rx(tx_rx, 300);
  902. }
  903. return true;
  904. }