mifare_classic.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989
  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 == 0x42 || ATQA0 == 0x02) && (SAK == 0x18)) {
  292. return true;
  293. } else {
  294. return false;
  295. }
  296. }
  297. MfClassicType mf_classic_get_classic_type(int8_t ATQA0, uint8_t ATQA1, uint8_t SAK) {
  298. UNUSED(ATQA1);
  299. if((ATQA0 == 0x44 || ATQA0 == 0x04) && (SAK == 0x08 || SAK == 0x88 || SAK == 0x09)) {
  300. return MfClassicType1k;
  301. } else if((ATQA0 == 0x42 || ATQA0 == 0x02) && (SAK == 0x18)) {
  302. return MfClassicType4k;
  303. }
  304. return MfClassicType1k;
  305. }
  306. bool mf_classic_get_type(uint8_t ATQA0, uint8_t ATQA1, uint8_t SAK, MfClassicReader* reader) {
  307. UNUSED(ATQA1);
  308. furi_assert(reader);
  309. memset(reader, 0, sizeof(MfClassicReader));
  310. if((ATQA0 == 0x44 || ATQA0 == 0x04) && (SAK == 0x08 || SAK == 0x88 || SAK == 0x09)) {
  311. reader->type = MfClassicType1k;
  312. } else if((ATQA0 == 0x42 || ATQA0 == 0x02) && (SAK == 0x18)) {
  313. reader->type = MfClassicType4k;
  314. } else {
  315. return false;
  316. }
  317. return true;
  318. }
  319. void mf_classic_reader_add_sector(
  320. MfClassicReader* reader,
  321. uint8_t sector,
  322. uint64_t key_a,
  323. uint64_t key_b) {
  324. furi_assert(reader);
  325. furi_assert(sector < MF_CLASSIC_SECTORS_MAX);
  326. furi_assert((key_a != MF_CLASSIC_NO_KEY) || (key_b != MF_CLASSIC_NO_KEY));
  327. if(reader->sectors_to_read < MF_CLASSIC_SECTORS_MAX) {
  328. reader->sector_reader[reader->sectors_to_read].key_a = key_a;
  329. reader->sector_reader[reader->sectors_to_read].key_b = key_b;
  330. reader->sector_reader[reader->sectors_to_read].sector_num = sector;
  331. reader->sectors_to_read++;
  332. }
  333. }
  334. void mf_classic_auth_init_context(MfClassicAuthContext* auth_ctx, uint8_t sector) {
  335. furi_assert(auth_ctx);
  336. auth_ctx->sector = sector;
  337. auth_ctx->key_a = MF_CLASSIC_NO_KEY;
  338. auth_ctx->key_b = MF_CLASSIC_NO_KEY;
  339. }
  340. static bool mf_classic_auth(
  341. FuriHalNfcTxRxContext* tx_rx,
  342. uint32_t block,
  343. uint64_t key,
  344. MfClassicKey key_type,
  345. Crypto1* crypto) {
  346. bool auth_success = false;
  347. uint32_t cuid = 0;
  348. memset(tx_rx->tx_data, 0, sizeof(tx_rx->tx_data));
  349. memset(tx_rx->tx_parity, 0, sizeof(tx_rx->tx_parity));
  350. tx_rx->tx_rx_type = FuriHalNfcTxRxTypeDefault;
  351. do {
  352. if(!furi_hal_nfc_activate_nfca(200, &cuid)) break;
  353. if(key_type == MfClassicKeyA) {
  354. tx_rx->tx_data[0] = MF_CLASSIC_AUTH_KEY_A_CMD;
  355. } else {
  356. tx_rx->tx_data[0] = MF_CLASSIC_AUTH_KEY_B_CMD;
  357. }
  358. tx_rx->tx_data[1] = block;
  359. tx_rx->tx_rx_type = FuriHalNfcTxRxTypeRxNoCrc;
  360. tx_rx->tx_bits = 2 * 8;
  361. if(!furi_hal_nfc_tx_rx(tx_rx, 6)) break;
  362. uint32_t nt = (uint32_t)nfc_util_bytes2num(tx_rx->rx_data, 4);
  363. crypto1_init(crypto, key);
  364. crypto1_word(crypto, nt ^ cuid, 0);
  365. uint8_t nr[4] = {};
  366. nfc_util_num2bytes(prng_successor(DWT->CYCCNT, 32), 4, nr);
  367. for(uint8_t i = 0; i < 4; i++) {
  368. tx_rx->tx_data[i] = crypto1_byte(crypto, nr[i], 0) ^ nr[i];
  369. tx_rx->tx_parity[0] |=
  370. (((crypto1_filter(crypto->odd) ^ nfc_util_odd_parity8(nr[i])) & 0x01) << (7 - i));
  371. }
  372. nt = prng_successor(nt, 32);
  373. for(uint8_t i = 4; i < 8; i++) {
  374. nt = prng_successor(nt, 8);
  375. tx_rx->tx_data[i] = crypto1_byte(crypto, 0x00, 0) ^ (nt & 0xff);
  376. tx_rx->tx_parity[0] |=
  377. (((crypto1_filter(crypto->odd) ^ nfc_util_odd_parity8(nt & 0xff)) & 0x01)
  378. << (7 - i));
  379. }
  380. tx_rx->tx_rx_type = FuriHalNfcTxRxTypeRaw;
  381. tx_rx->tx_bits = 8 * 8;
  382. if(!furi_hal_nfc_tx_rx(tx_rx, 6)) break;
  383. if(tx_rx->rx_bits == 32) {
  384. crypto1_word(crypto, 0, 0);
  385. auth_success = true;
  386. }
  387. } while(false);
  388. return auth_success;
  389. }
  390. bool mf_classic_authenticate(
  391. FuriHalNfcTxRxContext* tx_rx,
  392. uint8_t block_num,
  393. uint64_t key,
  394. MfClassicKey key_type) {
  395. furi_assert(tx_rx);
  396. Crypto1 crypto = {};
  397. bool key_found = mf_classic_auth(tx_rx, block_num, key, key_type, &crypto);
  398. furi_hal_nfc_sleep();
  399. return key_found;
  400. }
  401. bool mf_classic_auth_attempt(
  402. FuriHalNfcTxRxContext* tx_rx,
  403. MfClassicAuthContext* auth_ctx,
  404. uint64_t key) {
  405. furi_assert(tx_rx);
  406. furi_assert(auth_ctx);
  407. bool found_key = false;
  408. bool need_halt = (auth_ctx->key_a == MF_CLASSIC_NO_KEY) &&
  409. (auth_ctx->key_b == MF_CLASSIC_NO_KEY);
  410. Crypto1 crypto;
  411. if(auth_ctx->key_a == MF_CLASSIC_NO_KEY) {
  412. // Try AUTH with key A
  413. if(mf_classic_auth(
  414. tx_rx,
  415. mf_classic_get_first_block_num_of_sector(auth_ctx->sector),
  416. key,
  417. MfClassicKeyA,
  418. &crypto)) {
  419. auth_ctx->key_a = key;
  420. found_key = true;
  421. }
  422. }
  423. if(need_halt) {
  424. furi_hal_nfc_sleep();
  425. }
  426. if(auth_ctx->key_b == MF_CLASSIC_NO_KEY) {
  427. // Try AUTH with key B
  428. if(mf_classic_auth(
  429. tx_rx,
  430. mf_classic_get_first_block_num_of_sector(auth_ctx->sector),
  431. key,
  432. MfClassicKeyB,
  433. &crypto)) {
  434. auth_ctx->key_b = key;
  435. found_key = true;
  436. }
  437. }
  438. return found_key;
  439. }
  440. bool mf_classic_read_block(
  441. FuriHalNfcTxRxContext* tx_rx,
  442. Crypto1* crypto,
  443. uint8_t block_num,
  444. MfClassicBlock* block) {
  445. furi_assert(tx_rx);
  446. furi_assert(crypto);
  447. furi_assert(block);
  448. bool read_block_success = false;
  449. uint8_t plain_cmd[4] = {MF_CLASSIC_READ_SECT_CMD, block_num, 0x00, 0x00};
  450. nfca_append_crc16(plain_cmd, 2);
  451. memset(tx_rx->tx_data, 0, sizeof(tx_rx->tx_data));
  452. memset(tx_rx->tx_parity, 0, sizeof(tx_rx->tx_parity));
  453. for(uint8_t i = 0; i < 4; i++) {
  454. tx_rx->tx_data[i] = crypto1_byte(crypto, 0x00, 0) ^ plain_cmd[i];
  455. tx_rx->tx_parity[0] |=
  456. ((crypto1_filter(crypto->odd) ^ nfc_util_odd_parity8(plain_cmd[i])) & 0x01) << (7 - i);
  457. }
  458. tx_rx->tx_bits = 4 * 9;
  459. tx_rx->tx_rx_type = FuriHalNfcTxRxTypeRaw;
  460. if(furi_hal_nfc_tx_rx(tx_rx, 50)) {
  461. if(tx_rx->rx_bits == 8 * (MF_CLASSIC_BLOCK_SIZE + 2)) {
  462. uint8_t block_received[MF_CLASSIC_BLOCK_SIZE + 2];
  463. for(uint8_t i = 0; i < MF_CLASSIC_BLOCK_SIZE + 2; i++) {
  464. block_received[i] = crypto1_byte(crypto, 0, 0) ^ tx_rx->rx_data[i];
  465. }
  466. uint16_t crc_calc = nfca_get_crc16(block_received, MF_CLASSIC_BLOCK_SIZE);
  467. uint16_t crc_received = (block_received[MF_CLASSIC_BLOCK_SIZE + 1] << 8) |
  468. block_received[MF_CLASSIC_BLOCK_SIZE];
  469. if(crc_received != crc_calc) {
  470. FURI_LOG_E(
  471. TAG,
  472. "Incorrect CRC while reading block %d. Expected %04X, Received %04X",
  473. block_num,
  474. crc_received,
  475. crc_calc);
  476. } else {
  477. memcpy(block->value, block_received, MF_CLASSIC_BLOCK_SIZE);
  478. read_block_success = true;
  479. }
  480. }
  481. }
  482. return read_block_success;
  483. }
  484. void mf_classic_read_sector(FuriHalNfcTxRxContext* tx_rx, MfClassicData* data, uint8_t sec_num) {
  485. furi_assert(tx_rx);
  486. furi_assert(data);
  487. furi_hal_nfc_sleep();
  488. bool key_a_found = mf_classic_is_key_found(data, sec_num, MfClassicKeyA);
  489. bool key_b_found = mf_classic_is_key_found(data, sec_num, MfClassicKeyB);
  490. uint8_t start_block = mf_classic_get_first_block_num_of_sector(sec_num);
  491. uint8_t total_blocks = mf_classic_get_blocks_num_in_sector(sec_num);
  492. MfClassicBlock block_tmp = {};
  493. uint64_t key = 0;
  494. MfClassicSectorTrailer* sec_tr = mf_classic_get_sector_trailer_by_sector(data, sec_num);
  495. Crypto1 crypto = {};
  496. uint8_t blocks_read = 0;
  497. do {
  498. if(!key_a_found) break;
  499. FURI_LOG_D(TAG, "Try to read blocks with key A");
  500. key = nfc_util_bytes2num(sec_tr->key_a, sizeof(sec_tr->key_a));
  501. if(!mf_classic_auth(tx_rx, start_block, key, MfClassicKeyA, &crypto)) break;
  502. for(size_t i = start_block; i < start_block + total_blocks; i++) {
  503. if(!mf_classic_is_block_read(data, i)) {
  504. if(mf_classic_read_block(tx_rx, &crypto, i, &block_tmp)) {
  505. mf_classic_set_block_read(data, i, &block_tmp);
  506. blocks_read++;
  507. }
  508. } else {
  509. blocks_read++;
  510. }
  511. }
  512. FURI_LOG_D(TAG, "Read %d blocks out of %d", blocks_read, total_blocks);
  513. } while(false);
  514. do {
  515. if(blocks_read == total_blocks) break;
  516. if(!key_b_found) break;
  517. FURI_LOG_D(TAG, "Try to read blocks with key B");
  518. key = nfc_util_bytes2num(sec_tr->key_b, sizeof(sec_tr->key_b));
  519. furi_hal_nfc_sleep();
  520. if(!mf_classic_auth(tx_rx, start_block, key, MfClassicKeyB, &crypto)) break;
  521. for(size_t i = start_block; i < start_block + total_blocks; i++) {
  522. if(!mf_classic_is_block_read(data, i)) {
  523. if(mf_classic_read_block(tx_rx, &crypto, i, &block_tmp)) {
  524. mf_classic_set_block_read(data, i, &block_tmp);
  525. blocks_read++;
  526. }
  527. } else {
  528. blocks_read++;
  529. }
  530. }
  531. FURI_LOG_D(TAG, "Read %d blocks out of %d", blocks_read, total_blocks);
  532. } while(false);
  533. }
  534. static bool mf_classic_read_sector_with_reader(
  535. FuriHalNfcTxRxContext* tx_rx,
  536. Crypto1* crypto,
  537. MfClassicSectorReader* sector_reader,
  538. MfClassicSector* sector) {
  539. furi_assert(tx_rx);
  540. furi_assert(sector_reader);
  541. furi_assert(sector);
  542. uint64_t key;
  543. MfClassicKey key_type;
  544. uint8_t first_block;
  545. bool sector_read = false;
  546. furi_hal_nfc_sleep();
  547. do {
  548. // Activate card
  549. first_block = mf_classic_get_first_block_num_of_sector(sector_reader->sector_num);
  550. if(sector_reader->key_a != MF_CLASSIC_NO_KEY) {
  551. key = sector_reader->key_a;
  552. key_type = MfClassicKeyA;
  553. } else if(sector_reader->key_b != MF_CLASSIC_NO_KEY) {
  554. key = sector_reader->key_b;
  555. key_type = MfClassicKeyB;
  556. } else {
  557. break;
  558. }
  559. // Auth to first block in sector
  560. if(!mf_classic_auth(tx_rx, first_block, key, key_type, crypto)) break;
  561. sector->total_blocks = mf_classic_get_blocks_num_in_sector(sector_reader->sector_num);
  562. // Read blocks
  563. for(uint8_t i = 0; i < sector->total_blocks; i++) {
  564. mf_classic_read_block(tx_rx, crypto, first_block + i, &sector->block[i]);
  565. }
  566. // Save sector keys in last block
  567. if(sector_reader->key_a != MF_CLASSIC_NO_KEY) {
  568. nfc_util_num2bytes(
  569. sector_reader->key_a, 6, &sector->block[sector->total_blocks - 1].value[0]);
  570. }
  571. if(sector_reader->key_b != MF_CLASSIC_NO_KEY) {
  572. nfc_util_num2bytes(
  573. sector_reader->key_b, 6, &sector->block[sector->total_blocks - 1].value[10]);
  574. }
  575. sector_read = true;
  576. } while(false);
  577. return sector_read;
  578. }
  579. uint8_t mf_classic_read_card(
  580. FuriHalNfcTxRxContext* tx_rx,
  581. MfClassicReader* reader,
  582. MfClassicData* data) {
  583. furi_assert(tx_rx);
  584. furi_assert(reader);
  585. furi_assert(data);
  586. uint8_t sectors_read = 0;
  587. data->type = reader->type;
  588. data->key_a_mask = 0;
  589. data->key_b_mask = 0;
  590. MfClassicSector temp_sector = {};
  591. for(uint8_t i = 0; i < reader->sectors_to_read; i++) {
  592. if(mf_classic_read_sector_with_reader(
  593. tx_rx, &reader->crypto, &reader->sector_reader[i], &temp_sector)) {
  594. uint8_t first_block =
  595. mf_classic_get_first_block_num_of_sector(reader->sector_reader[i].sector_num);
  596. for(uint8_t j = 0; j < temp_sector.total_blocks; j++) {
  597. mf_classic_set_block_read(data, first_block + j, &temp_sector.block[j]);
  598. }
  599. if(reader->sector_reader[i].key_a != MF_CLASSIC_NO_KEY) {
  600. mf_classic_set_key_found(
  601. data,
  602. reader->sector_reader[i].sector_num,
  603. MfClassicKeyA,
  604. reader->sector_reader[i].key_a);
  605. }
  606. if(reader->sector_reader[i].key_b != MF_CLASSIC_NO_KEY) {
  607. mf_classic_set_key_found(
  608. data,
  609. reader->sector_reader[i].sector_num,
  610. MfClassicKeyB,
  611. reader->sector_reader[i].key_b);
  612. }
  613. sectors_read++;
  614. }
  615. }
  616. return sectors_read;
  617. }
  618. uint8_t mf_classic_update_card(FuriHalNfcTxRxContext* tx_rx, MfClassicData* data) {
  619. furi_assert(tx_rx);
  620. furi_assert(data);
  621. uint8_t sectors_read = 0;
  622. Crypto1 crypto = {};
  623. uint8_t total_sectors = mf_classic_get_total_sectors_num(data->type);
  624. uint64_t key_a = 0;
  625. uint64_t key_b = 0;
  626. MfClassicSectorReader sec_reader = {};
  627. MfClassicSector temp_sector = {};
  628. for(size_t i = 0; i < total_sectors; i++) {
  629. MfClassicSectorTrailer* sec_tr = mf_classic_get_sector_trailer_by_sector(data, i);
  630. // Load key A
  631. if(mf_classic_is_key_found(data, i, MfClassicKeyA)) {
  632. sec_reader.key_a = nfc_util_bytes2num(sec_tr->key_a, 6);
  633. } else {
  634. sec_reader.key_a = MF_CLASSIC_NO_KEY;
  635. }
  636. // Load key B
  637. if(mf_classic_is_key_found(data, i, MfClassicKeyB)) {
  638. sec_reader.key_b = nfc_util_bytes2num(sec_tr->key_b, 6);
  639. } else {
  640. sec_reader.key_b = MF_CLASSIC_NO_KEY;
  641. }
  642. if((key_a != MF_CLASSIC_NO_KEY) || (key_b != MF_CLASSIC_NO_KEY)) {
  643. sec_reader.sector_num = i;
  644. if(mf_classic_read_sector_with_reader(tx_rx, &crypto, &sec_reader, &temp_sector)) {
  645. uint8_t first_block = mf_classic_get_first_block_num_of_sector(i);
  646. for(uint8_t j = 0; j < temp_sector.total_blocks; j++) {
  647. mf_classic_set_block_read(data, first_block + j, &temp_sector.block[j]);
  648. }
  649. sectors_read++;
  650. }
  651. }
  652. }
  653. return sectors_read;
  654. }
  655. void mf_crypto1_decrypt(
  656. Crypto1* crypto,
  657. uint8_t* encrypted_data,
  658. uint16_t encrypted_data_bits,
  659. uint8_t* decrypted_data) {
  660. if(encrypted_data_bits < 8) {
  661. uint8_t decrypted_byte = 0;
  662. decrypted_byte |= (crypto1_bit(crypto, 0, 0) ^ FURI_BIT(encrypted_data[0], 0)) << 0;
  663. decrypted_byte |= (crypto1_bit(crypto, 0, 0) ^ FURI_BIT(encrypted_data[0], 1)) << 1;
  664. decrypted_byte |= (crypto1_bit(crypto, 0, 0) ^ FURI_BIT(encrypted_data[0], 2)) << 2;
  665. decrypted_byte |= (crypto1_bit(crypto, 0, 0) ^ FURI_BIT(encrypted_data[0], 3)) << 3;
  666. decrypted_data[0] = decrypted_byte;
  667. } else {
  668. for(size_t i = 0; i < encrypted_data_bits / 8; i++) {
  669. decrypted_data[i] = crypto1_byte(crypto, 0, 0) ^ encrypted_data[i];
  670. }
  671. }
  672. }
  673. void mf_crypto1_encrypt(
  674. Crypto1* crypto,
  675. uint8_t* keystream,
  676. uint8_t* plain_data,
  677. uint16_t plain_data_bits,
  678. uint8_t* encrypted_data,
  679. uint8_t* encrypted_parity) {
  680. if(plain_data_bits < 8) {
  681. encrypted_data[0] = 0;
  682. for(size_t i = 0; i < plain_data_bits; i++) {
  683. encrypted_data[0] |= (crypto1_bit(crypto, 0, 0) ^ FURI_BIT(plain_data[0], i)) << i;
  684. }
  685. } else {
  686. memset(encrypted_parity, 0, plain_data_bits / 8 + 1);
  687. for(uint8_t i = 0; i < plain_data_bits / 8; i++) {
  688. encrypted_data[i] = crypto1_byte(crypto, keystream ? keystream[i] : 0, 0) ^
  689. plain_data[i];
  690. encrypted_parity[i / 8] |=
  691. (((crypto1_filter(crypto->odd) ^ nfc_util_odd_parity8(plain_data[i])) & 0x01)
  692. << (7 - (i & 0x0007)));
  693. }
  694. }
  695. }
  696. bool mf_classic_emulator(MfClassicEmulator* emulator, FuriHalNfcTxRxContext* tx_rx) {
  697. furi_assert(emulator);
  698. furi_assert(tx_rx);
  699. bool command_processed = false;
  700. bool is_encrypted = false;
  701. uint8_t plain_data[MF_CLASSIC_MAX_DATA_SIZE];
  702. MfClassicKey access_key = MfClassicKeyA;
  703. // Read command
  704. while(!command_processed) {
  705. if(!is_encrypted) {
  706. memcpy(plain_data, tx_rx->rx_data, tx_rx->rx_bits / 8);
  707. } else {
  708. if(!furi_hal_nfc_tx_rx(tx_rx, 300)) {
  709. FURI_LOG_D(
  710. TAG,
  711. "Error in tx rx. Tx :%d bits, Rx: %d bits",
  712. tx_rx->tx_bits,
  713. tx_rx->rx_bits);
  714. break;
  715. }
  716. mf_crypto1_decrypt(&emulator->crypto, tx_rx->rx_data, tx_rx->rx_bits, plain_data);
  717. }
  718. if(plain_data[0] == 0x50 && plain_data[1] == 0x00) {
  719. FURI_LOG_T(TAG, "Halt received");
  720. furi_hal_nfc_listen_sleep();
  721. command_processed = true;
  722. break;
  723. } else if(plain_data[0] == 0x60 || plain_data[0] == 0x61) {
  724. uint8_t block = plain_data[1];
  725. uint64_t key = 0;
  726. uint8_t sector_trailer_block = mf_classic_get_sector_trailer_num_by_block(block);
  727. MfClassicSectorTrailer* sector_trailer =
  728. (MfClassicSectorTrailer*)emulator->data.block[sector_trailer_block].value;
  729. if(plain_data[0] == 0x60) {
  730. key = nfc_util_bytes2num(sector_trailer->key_a, 6);
  731. access_key = MfClassicKeyA;
  732. } else {
  733. key = nfc_util_bytes2num(sector_trailer->key_b, 6);
  734. access_key = MfClassicKeyB;
  735. }
  736. uint32_t nonce = prng_successor(DWT->CYCCNT, 32);
  737. uint8_t nt[4];
  738. uint8_t nt_keystream[4];
  739. nfc_util_num2bytes(nonce, 4, nt);
  740. nfc_util_num2bytes(nonce ^ emulator->cuid, 4, nt_keystream);
  741. crypto1_init(&emulator->crypto, key);
  742. if(!is_encrypted) {
  743. crypto1_word(&emulator->crypto, emulator->cuid ^ nonce, 0);
  744. memcpy(tx_rx->tx_data, nt, sizeof(nt));
  745. tx_rx->tx_parity[0] = 0;
  746. for(size_t i = 0; i < sizeof(nt); i++) {
  747. tx_rx->tx_parity[0] |= nfc_util_odd_parity8(nt[i]) << (7 - i);
  748. }
  749. tx_rx->tx_bits = sizeof(nt) * 8;
  750. tx_rx->tx_rx_type = FuriHalNfcTxRxTransparent;
  751. } else {
  752. mf_crypto1_encrypt(
  753. &emulator->crypto,
  754. nt_keystream,
  755. nt,
  756. sizeof(nt) * 8,
  757. tx_rx->tx_data,
  758. tx_rx->tx_parity);
  759. tx_rx->tx_bits = sizeof(nt) * 8;
  760. tx_rx->tx_rx_type = FuriHalNfcTxRxTransparent;
  761. }
  762. if(!furi_hal_nfc_tx_rx(tx_rx, 500)) {
  763. FURI_LOG_E(TAG, "Error in NT exchange");
  764. command_processed = true;
  765. break;
  766. }
  767. if(tx_rx->rx_bits != 64) {
  768. FURI_LOG_W(TAG, "Incorrect nr + ar");
  769. command_processed = true;
  770. break;
  771. }
  772. uint32_t nr = nfc_util_bytes2num(tx_rx->rx_data, 4);
  773. uint32_t ar = nfc_util_bytes2num(&tx_rx->rx_data[4], 4);
  774. FURI_LOG_D(
  775. TAG,
  776. "%08x key%c block %d nt/nr/ar: %08x %08x %08x",
  777. emulator->cuid,
  778. access_key == MfClassicKeyA ? 'A' : 'B',
  779. sector_trailer_block,
  780. nonce,
  781. nr,
  782. ar);
  783. crypto1_word(&emulator->crypto, nr, 1);
  784. uint32_t cardRr = ar ^ crypto1_word(&emulator->crypto, 0, 0);
  785. if(cardRr != prng_successor(nonce, 64)) {
  786. FURI_LOG_T(TAG, "Wrong AUTH! %08X != %08X", cardRr, prng_successor(nonce, 64));
  787. // Don't send NACK, as tag don't send it
  788. command_processed = true;
  789. break;
  790. }
  791. uint32_t ans = prng_successor(nonce, 96);
  792. uint8_t responce[4] = {};
  793. nfc_util_num2bytes(ans, 4, responce);
  794. mf_crypto1_encrypt(
  795. &emulator->crypto,
  796. NULL,
  797. responce,
  798. sizeof(responce) * 8,
  799. tx_rx->tx_data,
  800. tx_rx->tx_parity);
  801. tx_rx->tx_bits = sizeof(responce) * 8;
  802. tx_rx->tx_rx_type = FuriHalNfcTxRxTransparent;
  803. is_encrypted = true;
  804. } else if(is_encrypted && plain_data[0] == 0x30) {
  805. uint8_t block = plain_data[1];
  806. uint8_t block_data[18] = {};
  807. memcpy(block_data, emulator->data.block[block].value, MF_CLASSIC_BLOCK_SIZE);
  808. if(mf_classic_is_sector_trailer(block)) {
  809. if(!mf_classic_is_allowed_access(
  810. emulator, block, access_key, MfClassicActionKeyARead)) {
  811. memset(block_data, 0, 6);
  812. }
  813. if(!mf_classic_is_allowed_access(
  814. emulator, block, access_key, MfClassicActionKeyBRead)) {
  815. memset(&block_data[10], 0, 6);
  816. }
  817. if(!mf_classic_is_allowed_access(
  818. emulator, block, access_key, MfClassicActionACRead)) {
  819. memset(&block_data[6], 0, 4);
  820. }
  821. } else {
  822. if(!mf_classic_is_allowed_access(
  823. emulator, block, access_key, MfClassicActionDataRead)) {
  824. memset(block_data, 0, 16);
  825. }
  826. }
  827. nfca_append_crc16(block_data, 16);
  828. mf_crypto1_encrypt(
  829. &emulator->crypto,
  830. NULL,
  831. block_data,
  832. sizeof(block_data) * 8,
  833. tx_rx->tx_data,
  834. tx_rx->tx_parity);
  835. tx_rx->tx_bits = 18 * 8;
  836. tx_rx->tx_rx_type = FuriHalNfcTxRxTransparent;
  837. } else if(is_encrypted && plain_data[0] == 0xA0) {
  838. uint8_t block = plain_data[1];
  839. if(block > mf_classic_get_total_block_num(emulator->data.type)) {
  840. break;
  841. }
  842. // Send ACK
  843. uint8_t ack = 0x0A;
  844. mf_crypto1_encrypt(&emulator->crypto, NULL, &ack, 4, tx_rx->tx_data, tx_rx->tx_parity);
  845. tx_rx->tx_rx_type = FuriHalNfcTxRxTransparent;
  846. tx_rx->tx_bits = 4;
  847. if(!furi_hal_nfc_tx_rx(tx_rx, 300)) break;
  848. if(tx_rx->rx_bits != 18 * 8) break;
  849. mf_crypto1_decrypt(&emulator->crypto, tx_rx->rx_data, tx_rx->rx_bits, plain_data);
  850. uint8_t block_data[16] = {};
  851. memcpy(block_data, emulator->data.block[block].value, MF_CLASSIC_BLOCK_SIZE);
  852. if(mf_classic_is_sector_trailer(block)) {
  853. if(mf_classic_is_allowed_access(
  854. emulator, block, access_key, MfClassicActionKeyAWrite)) {
  855. memcpy(block_data, plain_data, 6);
  856. }
  857. if(mf_classic_is_allowed_access(
  858. emulator, block, access_key, MfClassicActionKeyBWrite)) {
  859. memcpy(&block_data[10], &plain_data[10], 6);
  860. }
  861. if(mf_classic_is_allowed_access(
  862. emulator, block, access_key, MfClassicActionACWrite)) {
  863. memcpy(&block_data[6], &plain_data[6], 4);
  864. }
  865. } else {
  866. if(mf_classic_is_allowed_access(
  867. emulator, block, access_key, MfClassicActionDataWrite)) {
  868. memcpy(block_data, plain_data, MF_CLASSIC_BLOCK_SIZE);
  869. }
  870. }
  871. if(memcmp(block_data, emulator->data.block[block].value, MF_CLASSIC_BLOCK_SIZE)) {
  872. memcpy(emulator->data.block[block].value, block_data, MF_CLASSIC_BLOCK_SIZE);
  873. emulator->data_changed = true;
  874. }
  875. // Send ACK
  876. ack = 0x0A;
  877. mf_crypto1_encrypt(&emulator->crypto, NULL, &ack, 4, tx_rx->tx_data, tx_rx->tx_parity);
  878. tx_rx->tx_rx_type = FuriHalNfcTxRxTransparent;
  879. tx_rx->tx_bits = 4;
  880. } else {
  881. // Unknown command
  882. break;
  883. }
  884. }
  885. if(!command_processed) {
  886. // Send NACK
  887. uint8_t nack = 0x04;
  888. if(is_encrypted) {
  889. mf_crypto1_encrypt(
  890. &emulator->crypto, NULL, &nack, 4, tx_rx->tx_data, tx_rx->tx_parity);
  891. } else {
  892. tx_rx->tx_data[0] = nack;
  893. }
  894. tx_rx->tx_rx_type = FuriHalNfcTxRxTransparent;
  895. tx_rx->tx_bits = 4;
  896. furi_hal_nfc_tx_rx(tx_rx, 300);
  897. }
  898. return true;
  899. }