mifare_classic.c 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542
  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_ACK_CMD 0xAU
  8. #define MF_CLASSIC_NACK_BUF_VALID_CMD 0x0U
  9. #define MF_CLASSIC_NACK_BUF_INVALID_CMD 0x4U
  10. #define MF_CLASSIC_AUTH_KEY_A_CMD 0x60U
  11. #define MF_CLASSIC_AUTH_KEY_B_CMD 0x61U
  12. #define MF_CLASSIC_READ_BLOCK_CMD 0x30U
  13. #define MF_CLASSIC_WRITE_BLOCK_CMD 0xA0U
  14. #define MF_CLASSIC_TRANSFER_CMD 0xB0U
  15. #define MF_CLASSIC_DECREMENT_CMD 0xC0U
  16. #define MF_CLASSIC_INCREMENT_CMD 0xC1U
  17. #define MF_CLASSIC_RESTORE_CMD 0xC2U
  18. const char* mf_classic_get_type_str(MfClassicType type) {
  19. if(type == MfClassicTypeMini) {
  20. return "MIFARE Mini 0.3K";
  21. } else if(type == MfClassicType1k) {
  22. return "MIFARE Classic 1K";
  23. } else if(type == MfClassicType4k) {
  24. return "MIFARE Classic 4K";
  25. } else {
  26. return "Unknown";
  27. }
  28. }
  29. static uint8_t mf_classic_get_first_block_num_of_sector(uint8_t sector) {
  30. furi_assert(sector < 40);
  31. if(sector < 32) {
  32. return sector * 4;
  33. } else {
  34. return 32 * 4 + (sector - 32) * 16;
  35. }
  36. }
  37. uint8_t mf_classic_get_sector_trailer_block_num_by_sector(uint8_t sector) {
  38. furi_assert(sector < 40);
  39. if(sector < 32) {
  40. return sector * 4 + 3;
  41. } else {
  42. return 32 * 4 + (sector - 32) * 16 + 15;
  43. }
  44. }
  45. uint8_t mf_classic_get_sector_by_block(uint8_t block) {
  46. if(block < 128) {
  47. return (block | 0x03) / 4;
  48. } else {
  49. return 32 + ((block | 0xf) - 32 * 4) / 16;
  50. }
  51. }
  52. static uint8_t mf_classic_get_blocks_num_in_sector(uint8_t sector) {
  53. furi_assert(sector < 40);
  54. return sector < 32 ? 4 : 16;
  55. }
  56. uint8_t mf_classic_get_sector_trailer_num_by_block(uint8_t block) {
  57. if(block < 128) {
  58. return block | 0x03;
  59. } else {
  60. return block | 0x0f;
  61. }
  62. }
  63. bool mf_classic_is_sector_trailer(uint8_t block) {
  64. return block == mf_classic_get_sector_trailer_num_by_block(block);
  65. }
  66. MfClassicSectorTrailer*
  67. mf_classic_get_sector_trailer_by_sector(MfClassicData* data, uint8_t sector) {
  68. furi_assert(data);
  69. uint8_t sec_tr_block_num = mf_classic_get_sector_trailer_block_num_by_sector(sector);
  70. return (MfClassicSectorTrailer*)data->block[sec_tr_block_num].value;
  71. }
  72. uint8_t mf_classic_get_total_sectors_num(MfClassicType type) {
  73. if(type == MfClassicTypeMini) {
  74. return MF_MINI_TOTAL_SECTORS_NUM;
  75. } else 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. uint16_t mf_classic_get_total_block_num(MfClassicType type) {
  84. if(type == MfClassicTypeMini) {
  85. return 20;
  86. } else if(type == MfClassicType1k) {
  87. return 64;
  88. } else if(type == MfClassicType4k) {
  89. return 256;
  90. } else {
  91. return 0;
  92. }
  93. }
  94. bool mf_classic_is_block_read(MfClassicData* data, uint8_t block_num) {
  95. furi_assert(data);
  96. return (FURI_BIT(data->block_read_mask[block_num / 32], block_num % 32) == 1);
  97. }
  98. void mf_classic_set_block_read(MfClassicData* data, uint8_t block_num, MfClassicBlock* block_data) {
  99. furi_assert(data);
  100. if(mf_classic_is_sector_trailer(block_num)) {
  101. memcpy(&data->block[block_num].value[6], &block_data->value[6], 4);
  102. } else {
  103. memcpy(data->block[block_num].value, block_data->value, MF_CLASSIC_BLOCK_SIZE);
  104. }
  105. FURI_BIT_SET(data->block_read_mask[block_num / 32], block_num % 32);
  106. }
  107. bool mf_classic_is_sector_data_read(MfClassicData* data, uint8_t sector_num) {
  108. furi_assert(data);
  109. uint8_t first_block = mf_classic_get_first_block_num_of_sector(sector_num);
  110. uint8_t total_blocks = mf_classic_get_blocks_num_in_sector(sector_num);
  111. bool data_read = true;
  112. for(size_t i = first_block; i < first_block + total_blocks; i++) {
  113. data_read &= mf_classic_is_block_read(data, i);
  114. }
  115. return data_read;
  116. }
  117. void mf_classic_set_sector_data_not_read(MfClassicData* data) {
  118. furi_assert(data);
  119. memset(data->block_read_mask, 0, sizeof(data->block_read_mask));
  120. }
  121. bool mf_classic_is_key_found(MfClassicData* data, uint8_t sector_num, MfClassicKey key_type) {
  122. furi_assert(data);
  123. bool key_found = false;
  124. if(key_type == MfClassicKeyA) {
  125. key_found = (FURI_BIT(data->key_a_mask, sector_num) == 1);
  126. } else if(key_type == MfClassicKeyB) {
  127. key_found = (FURI_BIT(data->key_b_mask, sector_num) == 1);
  128. }
  129. return key_found;
  130. }
  131. void mf_classic_set_key_found(
  132. MfClassicData* data,
  133. uint8_t sector_num,
  134. MfClassicKey key_type,
  135. uint64_t key) {
  136. furi_assert(data);
  137. uint8_t key_arr[6] = {};
  138. MfClassicSectorTrailer* sec_trailer =
  139. mf_classic_get_sector_trailer_by_sector(data, sector_num);
  140. nfc_util_num2bytes(key, 6, key_arr);
  141. if(key_type == MfClassicKeyA) {
  142. memcpy(sec_trailer->key_a, key_arr, sizeof(sec_trailer->key_a));
  143. FURI_BIT_SET(data->key_a_mask, sector_num);
  144. } else if(key_type == MfClassicKeyB) {
  145. memcpy(sec_trailer->key_b, key_arr, sizeof(sec_trailer->key_b));
  146. FURI_BIT_SET(data->key_b_mask, sector_num);
  147. }
  148. }
  149. void mf_classic_set_key_not_found(MfClassicData* data, uint8_t sector_num, MfClassicKey key_type) {
  150. furi_assert(data);
  151. if(key_type == MfClassicKeyA) {
  152. FURI_BIT_CLEAR(data->key_a_mask, sector_num);
  153. } else if(key_type == MfClassicKeyB) {
  154. FURI_BIT_CLEAR(data->key_b_mask, sector_num);
  155. }
  156. }
  157. bool mf_classic_is_sector_read(MfClassicData* data, uint8_t sector_num) {
  158. furi_assert(data);
  159. bool sector_read = false;
  160. do {
  161. if(!mf_classic_is_key_found(data, sector_num, MfClassicKeyA)) break;
  162. if(!mf_classic_is_key_found(data, sector_num, MfClassicKeyB)) break;
  163. uint8_t start_block = mf_classic_get_first_block_num_of_sector(sector_num);
  164. uint8_t total_blocks = mf_classic_get_blocks_num_in_sector(sector_num);
  165. uint8_t block_read = true;
  166. for(size_t i = start_block; i < start_block + total_blocks; i++) {
  167. block_read = mf_classic_is_block_read(data, i);
  168. if(!block_read) break;
  169. }
  170. sector_read = block_read;
  171. } while(false);
  172. return sector_read;
  173. }
  174. void mf_classic_get_read_sectors_and_keys(
  175. MfClassicData* data,
  176. uint8_t* sectors_read,
  177. uint8_t* keys_found) {
  178. furi_assert(data);
  179. furi_assert(sectors_read);
  180. furi_assert(keys_found);
  181. *sectors_read = 0;
  182. *keys_found = 0;
  183. uint8_t sectors_total = mf_classic_get_total_sectors_num(data->type);
  184. for(size_t i = 0; i < sectors_total; i++) {
  185. if(mf_classic_is_key_found(data, i, MfClassicKeyA)) {
  186. *keys_found += 1;
  187. }
  188. if(mf_classic_is_key_found(data, i, MfClassicKeyB)) {
  189. *keys_found += 1;
  190. }
  191. uint8_t first_block = mf_classic_get_first_block_num_of_sector(i);
  192. uint8_t total_blocks_in_sec = mf_classic_get_blocks_num_in_sector(i);
  193. bool blocks_read = true;
  194. for(size_t j = first_block; j < first_block + total_blocks_in_sec; j++) {
  195. blocks_read = mf_classic_is_block_read(data, j);
  196. if(!blocks_read) break;
  197. }
  198. if(blocks_read) {
  199. *sectors_read += 1;
  200. }
  201. }
  202. }
  203. bool mf_classic_is_card_read(MfClassicData* data) {
  204. furi_assert(data);
  205. uint8_t sectors_total = mf_classic_get_total_sectors_num(data->type);
  206. uint8_t sectors_read = 0;
  207. uint8_t keys_found = 0;
  208. mf_classic_get_read_sectors_and_keys(data, &sectors_read, &keys_found);
  209. bool card_read = (sectors_read == sectors_total) && (keys_found == sectors_total * 2);
  210. return card_read;
  211. }
  212. bool mf_classic_is_allowed_access_sector_trailer(
  213. MfClassicData* data,
  214. uint8_t block_num,
  215. MfClassicKey key,
  216. MfClassicAction action) {
  217. uint8_t* sector_trailer = data->block[block_num].value;
  218. uint8_t AC = ((sector_trailer[7] >> 5) & 0x04) | ((sector_trailer[8] >> 2) & 0x02) |
  219. ((sector_trailer[8] >> 7) & 0x01);
  220. switch(action) {
  221. case MfClassicActionKeyARead: {
  222. return false;
  223. }
  224. case MfClassicActionKeyAWrite:
  225. case MfClassicActionKeyBWrite: {
  226. return (
  227. (key == MfClassicKeyA && (AC == 0x00 || AC == 0x01)) ||
  228. (key == MfClassicKeyB && (AC == 0x04 || AC == 0x03)));
  229. }
  230. case MfClassicActionKeyBRead: {
  231. return (key == MfClassicKeyA && (AC == 0x00 || AC == 0x02 || AC == 0x01));
  232. }
  233. case MfClassicActionACRead: {
  234. return (
  235. (key == MfClassicKeyA) ||
  236. (key == MfClassicKeyB && !(AC == 0x00 || AC == 0x02 || AC == 0x01)));
  237. }
  238. case MfClassicActionACWrite: {
  239. return (
  240. (key == MfClassicKeyA && (AC == 0x01)) ||
  241. (key == MfClassicKeyB && (AC == 0x03 || AC == 0x05)));
  242. }
  243. default:
  244. return false;
  245. }
  246. return true;
  247. }
  248. bool mf_classic_is_allowed_access_data_block(
  249. MfClassicData* data,
  250. uint8_t block_num,
  251. MfClassicKey key,
  252. MfClassicAction action) {
  253. uint8_t* sector_trailer =
  254. data->block[mf_classic_get_sector_trailer_num_by_block(block_num)].value;
  255. if(block_num == 0 && action == MfClassicActionDataWrite) {
  256. return false;
  257. }
  258. uint8_t sector_block;
  259. if(block_num <= 128) {
  260. sector_block = block_num & 0x03;
  261. } else {
  262. sector_block = (block_num & 0x0f) / 5;
  263. }
  264. uint8_t AC;
  265. switch(sector_block) {
  266. case 0x00: {
  267. AC = ((sector_trailer[7] >> 2) & 0x04) | ((sector_trailer[8] << 1) & 0x02) |
  268. ((sector_trailer[8] >> 4) & 0x01);
  269. break;
  270. }
  271. case 0x01: {
  272. AC = ((sector_trailer[7] >> 3) & 0x04) | ((sector_trailer[8] >> 0) & 0x02) |
  273. ((sector_trailer[8] >> 5) & 0x01);
  274. break;
  275. }
  276. case 0x02: {
  277. AC = ((sector_trailer[7] >> 4) & 0x04) | ((sector_trailer[8] >> 1) & 0x02) |
  278. ((sector_trailer[8] >> 6) & 0x01);
  279. break;
  280. }
  281. default:
  282. return false;
  283. }
  284. switch(action) {
  285. case MfClassicActionDataRead: {
  286. return (
  287. (key == MfClassicKeyA && !(AC == 0x03 || AC == 0x05 || AC == 0x07)) ||
  288. (key == MfClassicKeyB && !(AC == 0x07)));
  289. }
  290. case MfClassicActionDataWrite: {
  291. return (
  292. (key == MfClassicKeyA && (AC == 0x00)) ||
  293. (key == MfClassicKeyB && (AC == 0x00 || AC == 0x04 || AC == 0x06 || AC == 0x03)));
  294. }
  295. case MfClassicActionDataInc: {
  296. return (
  297. (key == MfClassicKeyA && (AC == 0x00)) ||
  298. (key == MfClassicKeyB && (AC == 0x00 || AC == 0x06)));
  299. }
  300. case MfClassicActionDataDec: {
  301. return (
  302. (key == MfClassicKeyA && (AC == 0x00 || AC == 0x06 || AC == 0x01)) ||
  303. (key == MfClassicKeyB && (AC == 0x00 || AC == 0x06 || AC == 0x01)));
  304. }
  305. default:
  306. return false;
  307. }
  308. return false;
  309. }
  310. static bool mf_classic_is_allowed_access(
  311. MfClassicEmulator* emulator,
  312. uint8_t block_num,
  313. MfClassicKey key,
  314. MfClassicAction action) {
  315. if(mf_classic_is_sector_trailer(block_num)) {
  316. return mf_classic_is_allowed_access_sector_trailer(
  317. &emulator->data, block_num, key, action);
  318. } else {
  319. return mf_classic_is_allowed_access_data_block(&emulator->data, block_num, key, action);
  320. }
  321. }
  322. bool mf_classic_is_value_block(MfClassicData* data, uint8_t block_num) {
  323. // Check if key A can write, if it can, it's transport configuration, not data block
  324. return !mf_classic_is_allowed_access_data_block(
  325. data, block_num, MfClassicKeyA, MfClassicActionDataWrite) &&
  326. (mf_classic_is_allowed_access_data_block(
  327. data, block_num, MfClassicKeyB, MfClassicActionDataInc) ||
  328. mf_classic_is_allowed_access_data_block(
  329. data, block_num, MfClassicKeyB, MfClassicActionDataDec));
  330. }
  331. bool mf_classic_check_card_type(uint8_t ATQA0, uint8_t ATQA1, uint8_t SAK) {
  332. UNUSED(ATQA1);
  333. if((ATQA0 == 0x44 || ATQA0 == 0x04) && (SAK == 0x08 || SAK == 0x88 || SAK == 0x09)) {
  334. return true;
  335. } else if((ATQA0 == 0x01) && (ATQA1 == 0x0F) && (SAK == 0x01)) {
  336. //skylanders support
  337. return true;
  338. } else if((ATQA0 == 0x42 || ATQA0 == 0x02) && (SAK == 0x18)) {
  339. return true;
  340. } else {
  341. return false;
  342. }
  343. }
  344. MfClassicType mf_classic_get_classic_type(uint8_t ATQA0, uint8_t ATQA1, uint8_t SAK) {
  345. UNUSED(ATQA1);
  346. if((ATQA0 == 0x44 || ATQA0 == 0x04)) {
  347. if((SAK == 0x08 || SAK == 0x88)) {
  348. return MfClassicType1k;
  349. } else if(SAK == 0x09) {
  350. return MfClassicTypeMini;
  351. }
  352. } else if((ATQA0 == 0x01) && (ATQA1 == 0x0F) && (SAK == 0x01)) {
  353. //skylanders support
  354. return MfClassicType1k;
  355. } else if((ATQA0 == 0x42 || ATQA0 == 0x02) && (SAK == 0x18)) {
  356. return MfClassicType4k;
  357. }
  358. return MfClassicType1k;
  359. }
  360. void mf_classic_reader_add_sector(
  361. MfClassicReader* reader,
  362. uint8_t sector,
  363. uint64_t key_a,
  364. uint64_t key_b) {
  365. furi_assert(reader);
  366. furi_assert(sector < MF_CLASSIC_SECTORS_MAX);
  367. furi_assert((key_a != MF_CLASSIC_NO_KEY) || (key_b != MF_CLASSIC_NO_KEY));
  368. if(reader->sectors_to_read < MF_CLASSIC_SECTORS_MAX) {
  369. reader->sector_reader[reader->sectors_to_read].key_a = key_a;
  370. reader->sector_reader[reader->sectors_to_read].key_b = key_b;
  371. reader->sector_reader[reader->sectors_to_read].sector_num = sector;
  372. reader->sectors_to_read++;
  373. }
  374. }
  375. bool mf_classic_block_to_value(const uint8_t* block, int32_t* value, uint8_t* addr) {
  376. uint32_t v = *(uint32_t*)&block[0];
  377. uint32_t v_inv = *(uint32_t*)&block[4];
  378. uint32_t v1 = *(uint32_t*)&block[8];
  379. bool val_checks =
  380. ((v == v1) && (v == ~v_inv) && (block[12] == (~block[13] & 0xFF)) &&
  381. (block[14] == (~block[15] & 0xFF)) && (block[12] == block[14]));
  382. if(value) {
  383. *value = (int32_t)v;
  384. }
  385. if(addr) {
  386. *addr = block[12];
  387. }
  388. return val_checks;
  389. }
  390. void mf_classic_value_to_block(int32_t value, uint8_t addr, uint8_t* block) {
  391. uint32_t v_inv = ~((uint32_t)value);
  392. memcpy(block, &value, 4); //-V1086
  393. memcpy(block + 4, &v_inv, 4); //-V1086
  394. memcpy(block + 8, &value, 4); //-V1086
  395. block[12] = addr;
  396. block[13] = ~addr & 0xFF;
  397. block[14] = addr;
  398. block[15] = ~addr & 0xFF;
  399. }
  400. void mf_classic_auth_init_context(MfClassicAuthContext* auth_ctx, uint8_t sector) {
  401. furi_assert(auth_ctx);
  402. auth_ctx->sector = sector;
  403. auth_ctx->key_a = MF_CLASSIC_NO_KEY;
  404. auth_ctx->key_b = MF_CLASSIC_NO_KEY;
  405. }
  406. static bool mf_classic_auth(
  407. FuriHalNfcTxRxContext* tx_rx,
  408. uint32_t block,
  409. uint64_t key,
  410. MfClassicKey key_type,
  411. Crypto1* crypto,
  412. bool skip_activate,
  413. uint32_t cuid) {
  414. bool auth_success = false;
  415. memset(tx_rx->tx_data, 0, sizeof(tx_rx->tx_data));
  416. memset(tx_rx->tx_parity, 0, sizeof(tx_rx->tx_parity));
  417. tx_rx->tx_rx_type = FuriHalNfcTxRxTypeDefault;
  418. do {
  419. if(!skip_activate && !furi_hal_nfc_activate_nfca(200, &cuid)) break;
  420. if(key_type == MfClassicKeyA) {
  421. tx_rx->tx_data[0] = MF_CLASSIC_AUTH_KEY_A_CMD;
  422. } else {
  423. tx_rx->tx_data[0] = MF_CLASSIC_AUTH_KEY_B_CMD;
  424. }
  425. tx_rx->tx_data[1] = block;
  426. tx_rx->tx_rx_type = FuriHalNfcTxRxTypeRxNoCrc;
  427. tx_rx->tx_bits = 2 * 8;
  428. if(!furi_hal_nfc_tx_rx(tx_rx, 6)) break;
  429. uint32_t nt = (uint32_t)nfc_util_bytes2num(tx_rx->rx_data, 4);
  430. crypto1_init(crypto, key);
  431. crypto1_word(crypto, nt ^ cuid, 0);
  432. uint8_t nr[4] = {};
  433. nfc_util_num2bytes(prng_successor(DWT->CYCCNT, 32), 4, nr);
  434. for(uint8_t i = 0; i < 4; i++) {
  435. tx_rx->tx_data[i] = crypto1_byte(crypto, nr[i], 0) ^ nr[i];
  436. tx_rx->tx_parity[0] |=
  437. (((crypto1_filter(crypto->odd) ^ nfc_util_odd_parity8(nr[i])) & 0x01) << (7 - i));
  438. }
  439. nt = prng_successor(nt, 32);
  440. for(uint8_t i = 4; i < 8; i++) {
  441. nt = prng_successor(nt, 8);
  442. tx_rx->tx_data[i] = crypto1_byte(crypto, 0x00, 0) ^ (nt & 0xff);
  443. tx_rx->tx_parity[0] |=
  444. (((crypto1_filter(crypto->odd) ^ nfc_util_odd_parity8(nt & 0xff)) & 0x01)
  445. << (7 - i));
  446. }
  447. tx_rx->tx_rx_type = FuriHalNfcTxRxTypeRaw;
  448. tx_rx->tx_bits = 8 * 8;
  449. if(!furi_hal_nfc_tx_rx(tx_rx, 6)) break;
  450. if(tx_rx->rx_bits == 32) {
  451. crypto1_word(crypto, 0, 0);
  452. auth_success = true;
  453. }
  454. } while(false);
  455. return auth_success;
  456. }
  457. bool mf_classic_authenticate(
  458. FuriHalNfcTxRxContext* tx_rx,
  459. uint8_t block_num,
  460. uint64_t key,
  461. MfClassicKey key_type) {
  462. furi_assert(tx_rx);
  463. Crypto1 crypto = {};
  464. bool key_found = mf_classic_auth(tx_rx, block_num, key, key_type, &crypto, false, 0);
  465. furi_hal_nfc_sleep();
  466. return key_found;
  467. }
  468. bool mf_classic_authenticate_skip_activate(
  469. FuriHalNfcTxRxContext* tx_rx,
  470. uint8_t block_num,
  471. uint64_t key,
  472. MfClassicKey key_type,
  473. bool skip_activate,
  474. uint32_t cuid) {
  475. furi_assert(tx_rx);
  476. Crypto1 crypto = {};
  477. bool key_found =
  478. mf_classic_auth(tx_rx, block_num, key, key_type, &crypto, skip_activate, cuid);
  479. furi_hal_nfc_sleep();
  480. return key_found;
  481. }
  482. bool mf_classic_auth_attempt(
  483. FuriHalNfcTxRxContext* tx_rx,
  484. Crypto1* crypto,
  485. MfClassicAuthContext* auth_ctx,
  486. uint64_t key) {
  487. furi_assert(tx_rx);
  488. furi_assert(auth_ctx);
  489. bool found_key = false;
  490. bool need_halt = (auth_ctx->key_a == MF_CLASSIC_NO_KEY) &&
  491. (auth_ctx->key_b == MF_CLASSIC_NO_KEY);
  492. if(auth_ctx->key_a == MF_CLASSIC_NO_KEY) {
  493. // Try AUTH with key A
  494. if(mf_classic_auth(
  495. tx_rx,
  496. mf_classic_get_sector_trailer_block_num_by_sector(auth_ctx->sector),
  497. key,
  498. MfClassicKeyA,
  499. crypto,
  500. false,
  501. 0)) {
  502. auth_ctx->key_a = key;
  503. found_key = true;
  504. }
  505. }
  506. if(need_halt) {
  507. furi_hal_nfc_sleep();
  508. }
  509. if(auth_ctx->key_b == MF_CLASSIC_NO_KEY) {
  510. // Try AUTH with key B
  511. if(mf_classic_auth(
  512. tx_rx,
  513. mf_classic_get_sector_trailer_block_num_by_sector(auth_ctx->sector),
  514. key,
  515. MfClassicKeyB,
  516. crypto,
  517. false,
  518. 0)) {
  519. auth_ctx->key_b = key;
  520. found_key = true;
  521. }
  522. }
  523. return found_key;
  524. }
  525. bool mf_classic_read_block(
  526. FuriHalNfcTxRxContext* tx_rx,
  527. Crypto1* crypto,
  528. uint8_t block_num,
  529. MfClassicBlock* block) {
  530. furi_assert(tx_rx);
  531. furi_assert(crypto);
  532. furi_assert(block);
  533. bool read_block_success = false;
  534. uint8_t plain_cmd[4] = {MF_CLASSIC_READ_BLOCK_CMD, block_num, 0x00, 0x00};
  535. nfca_append_crc16(plain_cmd, 2);
  536. crypto1_encrypt(
  537. crypto, NULL, plain_cmd, sizeof(plain_cmd) * 8, tx_rx->tx_data, tx_rx->tx_parity);
  538. tx_rx->tx_bits = sizeof(plain_cmd) * 8;
  539. tx_rx->tx_rx_type = FuriHalNfcTxRxTypeRaw;
  540. if(furi_hal_nfc_tx_rx(tx_rx, 50)) {
  541. if(tx_rx->rx_bits == 8 * (MF_CLASSIC_BLOCK_SIZE + 2)) {
  542. uint8_t block_received[MF_CLASSIC_BLOCK_SIZE + 2];
  543. crypto1_decrypt(crypto, tx_rx->rx_data, tx_rx->rx_bits, block_received);
  544. uint16_t crc_calc = nfca_get_crc16(block_received, MF_CLASSIC_BLOCK_SIZE);
  545. uint16_t crc_received = (block_received[MF_CLASSIC_BLOCK_SIZE + 1] << 8) |
  546. block_received[MF_CLASSIC_BLOCK_SIZE];
  547. if(crc_received != crc_calc) {
  548. FURI_LOG_E(
  549. TAG,
  550. "Incorrect CRC while reading block %d. Expected %04X, Received %04X",
  551. block_num,
  552. crc_received,
  553. crc_calc);
  554. } else {
  555. memcpy(block->value, block_received, MF_CLASSIC_BLOCK_SIZE);
  556. read_block_success = true;
  557. }
  558. }
  559. }
  560. return read_block_success;
  561. }
  562. void mf_classic_read_sector(FuriHalNfcTxRxContext* tx_rx, MfClassicData* data, uint8_t sec_num) {
  563. furi_assert(tx_rx);
  564. furi_assert(data);
  565. furi_hal_nfc_sleep();
  566. bool key_a_found = mf_classic_is_key_found(data, sec_num, MfClassicKeyA);
  567. bool key_b_found = mf_classic_is_key_found(data, sec_num, MfClassicKeyB);
  568. uint8_t start_block = mf_classic_get_first_block_num_of_sector(sec_num);
  569. uint8_t total_blocks = mf_classic_get_blocks_num_in_sector(sec_num);
  570. MfClassicBlock block_tmp = {};
  571. uint64_t key = 0;
  572. MfClassicSectorTrailer* sec_tr = mf_classic_get_sector_trailer_by_sector(data, sec_num);
  573. Crypto1 crypto = {};
  574. uint8_t blocks_read = 0;
  575. do {
  576. if(!key_a_found) break;
  577. FURI_LOG_D(TAG, "Try to read blocks with key A");
  578. key = nfc_util_bytes2num(sec_tr->key_a, sizeof(sec_tr->key_a));
  579. if(!mf_classic_auth(tx_rx, start_block, key, MfClassicKeyA, &crypto, false, 0)) {
  580. mf_classic_set_key_not_found(data, sec_num, MfClassicKeyA);
  581. FURI_LOG_D(TAG, "Key %dA not found in read", sec_num);
  582. break;
  583. }
  584. for(size_t i = start_block; i < start_block + total_blocks; i++) {
  585. if(!mf_classic_is_block_read(data, i)) {
  586. if(mf_classic_read_block(tx_rx, &crypto, i, &block_tmp)) {
  587. mf_classic_set_block_read(data, i, &block_tmp);
  588. blocks_read++;
  589. } else if(i > start_block) {
  590. // Try to re-auth to read block in case prevous block was protected from read
  591. furi_hal_nfc_sleep();
  592. if(!mf_classic_auth(tx_rx, i, key, MfClassicKeyA, &crypto, false, 0)) {
  593. mf_classic_set_key_not_found(data, sec_num, MfClassicKeyA);
  594. FURI_LOG_D(TAG, "Key %dA not found in read", sec_num);
  595. break;
  596. }
  597. if(mf_classic_read_block(tx_rx, &crypto, i, &block_tmp)) {
  598. mf_classic_set_block_read(data, i, &block_tmp);
  599. blocks_read++;
  600. }
  601. }
  602. } else {
  603. blocks_read++;
  604. }
  605. }
  606. FURI_LOG_D(TAG, "Read %d blocks out of %d", blocks_read, total_blocks);
  607. } while(false);
  608. do {
  609. if(blocks_read == total_blocks) break;
  610. if(!key_b_found) break;
  611. if(key_a_found) {
  612. furi_hal_nfc_sleep();
  613. }
  614. FURI_LOG_D(TAG, "Try to read blocks with key B");
  615. key = nfc_util_bytes2num(sec_tr->key_b, sizeof(sec_tr->key_b));
  616. if(!mf_classic_auth(tx_rx, start_block, key, MfClassicKeyB, &crypto, false, 0)) {
  617. mf_classic_set_key_not_found(data, sec_num, MfClassicKeyB);
  618. FURI_LOG_D(TAG, "Key %dB not found in read", sec_num);
  619. break;
  620. }
  621. for(size_t i = start_block; i < start_block + total_blocks; i++) {
  622. if(!mf_classic_is_block_read(data, i)) {
  623. if(mf_classic_read_block(tx_rx, &crypto, i, &block_tmp)) {
  624. mf_classic_set_block_read(data, i, &block_tmp);
  625. blocks_read++;
  626. } else if(i > start_block) {
  627. // Try to re-auth to read block in case prevous block was protected from read
  628. furi_hal_nfc_sleep();
  629. if(!mf_classic_auth(tx_rx, i, key, MfClassicKeyB, &crypto, false, 0)) {
  630. mf_classic_set_key_not_found(data, sec_num, MfClassicKeyB);
  631. FURI_LOG_D(TAG, "Key %dB not found in read", sec_num);
  632. break;
  633. }
  634. if(mf_classic_read_block(tx_rx, &crypto, i, &block_tmp)) {
  635. mf_classic_set_block_read(data, i, &block_tmp);
  636. blocks_read++;
  637. }
  638. }
  639. } else {
  640. blocks_read++;
  641. }
  642. }
  643. FURI_LOG_D(TAG, "Read %d blocks out of %d", blocks_read, total_blocks);
  644. } while(false);
  645. }
  646. static bool mf_classic_read_sector_with_reader(
  647. FuriHalNfcTxRxContext* tx_rx,
  648. Crypto1* crypto,
  649. MfClassicSectorReader* sector_reader,
  650. MfClassicSector* sector) {
  651. furi_assert(tx_rx);
  652. furi_assert(sector_reader);
  653. furi_assert(sector);
  654. uint64_t key;
  655. MfClassicKey key_type;
  656. uint8_t first_block;
  657. bool sector_read = false;
  658. furi_hal_nfc_sleep();
  659. do {
  660. // Activate card
  661. first_block = mf_classic_get_first_block_num_of_sector(sector_reader->sector_num);
  662. if(sector_reader->key_a != MF_CLASSIC_NO_KEY) {
  663. key = sector_reader->key_a;
  664. key_type = MfClassicKeyA;
  665. } else if(sector_reader->key_b != MF_CLASSIC_NO_KEY) {
  666. key = sector_reader->key_b;
  667. key_type = MfClassicKeyB;
  668. } else {
  669. break;
  670. }
  671. // Auth to first block in sector
  672. if(!mf_classic_auth(tx_rx, first_block, key, key_type, crypto, false, 0)) {
  673. // Set key to MF_CLASSIC_NO_KEY to prevent further attempts
  674. if(key_type == MfClassicKeyA) {
  675. sector_reader->key_a = MF_CLASSIC_NO_KEY;
  676. } else {
  677. sector_reader->key_b = MF_CLASSIC_NO_KEY;
  678. }
  679. break;
  680. }
  681. sector->total_blocks = mf_classic_get_blocks_num_in_sector(sector_reader->sector_num);
  682. // Read blocks
  683. for(uint8_t i = 0; i < sector->total_blocks; i++) {
  684. if(mf_classic_read_block(tx_rx, crypto, first_block + i, &sector->block[i])) continue;
  685. if(i == 0) continue;
  686. // Try to auth to read next block in case previous is locked
  687. furi_hal_nfc_sleep();
  688. if(!mf_classic_auth(tx_rx, first_block + i, key, key_type, crypto, false, 0)) continue;
  689. mf_classic_read_block(tx_rx, crypto, first_block + i, &sector->block[i]);
  690. }
  691. // Save sector keys in last block
  692. if(sector_reader->key_a != MF_CLASSIC_NO_KEY) {
  693. nfc_util_num2bytes(
  694. sector_reader->key_a, 6, &sector->block[sector->total_blocks - 1].value[0]);
  695. }
  696. if(sector_reader->key_b != MF_CLASSIC_NO_KEY) {
  697. nfc_util_num2bytes(
  698. sector_reader->key_b, 6, &sector->block[sector->total_blocks - 1].value[10]);
  699. }
  700. sector_read = true;
  701. } while(false);
  702. return sector_read;
  703. }
  704. uint8_t mf_classic_read_card(
  705. FuriHalNfcTxRxContext* tx_rx,
  706. MfClassicReader* reader,
  707. MfClassicData* data) {
  708. furi_assert(tx_rx);
  709. furi_assert(reader);
  710. furi_assert(data);
  711. uint8_t sectors_read = 0;
  712. data->type = reader->type;
  713. data->key_a_mask = 0;
  714. data->key_b_mask = 0;
  715. MfClassicSector temp_sector = {};
  716. for(uint8_t i = 0; i < reader->sectors_to_read; i++) {
  717. if(mf_classic_read_sector_with_reader(
  718. tx_rx, &reader->crypto, &reader->sector_reader[i], &temp_sector)) {
  719. uint8_t first_block =
  720. mf_classic_get_first_block_num_of_sector(reader->sector_reader[i].sector_num);
  721. for(uint8_t j = 0; j < temp_sector.total_blocks; j++) {
  722. mf_classic_set_block_read(data, first_block + j, &temp_sector.block[j]);
  723. }
  724. if(reader->sector_reader[i].key_a != MF_CLASSIC_NO_KEY) {
  725. mf_classic_set_key_found(
  726. data,
  727. reader->sector_reader[i].sector_num,
  728. MfClassicKeyA,
  729. reader->sector_reader[i].key_a);
  730. }
  731. if(reader->sector_reader[i].key_b != MF_CLASSIC_NO_KEY) {
  732. mf_classic_set_key_found(
  733. data,
  734. reader->sector_reader[i].sector_num,
  735. MfClassicKeyB,
  736. reader->sector_reader[i].key_b);
  737. }
  738. sectors_read++;
  739. }
  740. }
  741. return sectors_read;
  742. }
  743. uint8_t mf_classic_update_card(FuriHalNfcTxRxContext* tx_rx, MfClassicData* data) {
  744. furi_assert(tx_rx);
  745. furi_assert(data);
  746. uint8_t total_sectors = mf_classic_get_total_sectors_num(data->type);
  747. for(size_t i = 0; i < total_sectors; i++) {
  748. mf_classic_read_sector(tx_rx, data, i);
  749. }
  750. uint8_t sectors_read = 0;
  751. uint8_t keys_found = 0;
  752. mf_classic_get_read_sectors_and_keys(data, &sectors_read, &keys_found);
  753. FURI_LOG_D(TAG, "Read %d sectors and %d keys", sectors_read, keys_found);
  754. return sectors_read;
  755. }
  756. bool mf_classic_emulator(MfClassicEmulator* emulator, FuriHalNfcTxRxContext* tx_rx) {
  757. furi_assert(emulator);
  758. furi_assert(tx_rx);
  759. bool command_processed = false;
  760. bool is_encrypted = false;
  761. uint8_t plain_data[MF_CLASSIC_MAX_DATA_SIZE];
  762. MfClassicKey access_key = MfClassicKeyA;
  763. // Used for decrement and increment - copy to block on transfer
  764. uint8_t transfer_buf[MF_CLASSIC_BLOCK_SIZE] = {};
  765. bool transfer_buf_valid = false;
  766. // Read command
  767. while(!command_processed) { //-V654
  768. if(!is_encrypted) {
  769. crypto1_reset(&emulator->crypto);
  770. memcpy(plain_data, tx_rx->rx_data, tx_rx->rx_bits / 8);
  771. } else {
  772. if(!furi_hal_nfc_tx_rx(tx_rx, 300)) {
  773. FURI_LOG_D(
  774. TAG,
  775. "Error in tx rx. Tx :%d bits, Rx: %d bits",
  776. tx_rx->tx_bits,
  777. tx_rx->rx_bits);
  778. break;
  779. }
  780. crypto1_decrypt(&emulator->crypto, tx_rx->rx_data, tx_rx->rx_bits, plain_data);
  781. }
  782. // After increment, decrement or restore the only allowed command is transfer
  783. uint8_t cmd = plain_data[0];
  784. if(transfer_buf_valid && cmd != MF_CLASSIC_TRANSFER_CMD) {
  785. break;
  786. }
  787. if(cmd == 0x50 && plain_data[1] == 0x00) {
  788. FURI_LOG_T(TAG, "Halt received");
  789. furi_hal_nfc_listen_sleep();
  790. command_processed = true;
  791. break;
  792. }
  793. if(cmd == MF_CLASSIC_AUTH_KEY_A_CMD || cmd == MF_CLASSIC_AUTH_KEY_B_CMD) {
  794. uint8_t block = plain_data[1];
  795. uint64_t key = 0;
  796. uint8_t sector_trailer_block = mf_classic_get_sector_trailer_num_by_block(block);
  797. MfClassicSectorTrailer* sector_trailer =
  798. (MfClassicSectorTrailer*)emulator->data.block[sector_trailer_block].value;
  799. if(cmd == MF_CLASSIC_AUTH_KEY_A_CMD) {
  800. key = nfc_util_bytes2num(sector_trailer->key_a, 6);
  801. access_key = MfClassicKeyA;
  802. } else {
  803. key = nfc_util_bytes2num(sector_trailer->key_b, 6);
  804. access_key = MfClassicKeyB;
  805. }
  806. uint32_t nonce = prng_successor(DWT->CYCCNT, 32) ^ 0xAA;
  807. uint8_t nt[4];
  808. uint8_t nt_keystream[4];
  809. nfc_util_num2bytes(nonce, 4, nt);
  810. nfc_util_num2bytes(nonce ^ emulator->cuid, 4, nt_keystream);
  811. crypto1_init(&emulator->crypto, key);
  812. if(!is_encrypted) {
  813. crypto1_word(&emulator->crypto, emulator->cuid ^ nonce, 0);
  814. memcpy(tx_rx->tx_data, nt, sizeof(nt));
  815. tx_rx->tx_parity[0] = 0;
  816. nfc_util_odd_parity(tx_rx->tx_data, tx_rx->tx_parity, sizeof(nt));
  817. tx_rx->tx_bits = sizeof(nt) * 8;
  818. tx_rx->tx_rx_type = FuriHalNfcTxRxTransparent;
  819. } else {
  820. crypto1_encrypt(
  821. &emulator->crypto,
  822. nt_keystream,
  823. nt,
  824. sizeof(nt) * 8,
  825. tx_rx->tx_data,
  826. tx_rx->tx_parity);
  827. tx_rx->tx_bits = sizeof(nt) * 8;
  828. tx_rx->tx_rx_type = FuriHalNfcTxRxTransparent;
  829. }
  830. if(!furi_hal_nfc_tx_rx(tx_rx, 500)) {
  831. FURI_LOG_E(TAG, "Error in NT exchange");
  832. command_processed = true;
  833. break;
  834. }
  835. if(tx_rx->rx_bits != 64) {
  836. FURI_LOG_W(TAG, "Incorrect nr + ar length: %d", tx_rx->rx_bits);
  837. command_processed = true;
  838. break;
  839. }
  840. uint32_t nr = nfc_util_bytes2num(tx_rx->rx_data, 4);
  841. uint32_t ar = nfc_util_bytes2num(&tx_rx->rx_data[4], 4);
  842. crypto1_word(&emulator->crypto, nr, 1);
  843. uint32_t cardRr = ar ^ crypto1_word(&emulator->crypto, 0, 0);
  844. if(cardRr != prng_successor(nonce, 64)) {
  845. FURI_LOG_T(TAG, "Wrong AUTH! %08lX != %08lX", cardRr, prng_successor(nonce, 64));
  846. // Don't send NACK, as the tag doesn't send it
  847. command_processed = true;
  848. break;
  849. }
  850. uint32_t ans = prng_successor(nonce, 96);
  851. uint8_t response[4] = {};
  852. nfc_util_num2bytes(ans, 4, response);
  853. crypto1_encrypt(
  854. &emulator->crypto,
  855. NULL,
  856. response,
  857. sizeof(response) * 8,
  858. tx_rx->tx_data,
  859. tx_rx->tx_parity);
  860. tx_rx->tx_bits = sizeof(response) * 8;
  861. tx_rx->tx_rx_type = FuriHalNfcTxRxTransparent;
  862. is_encrypted = true;
  863. continue;
  864. }
  865. if(!is_encrypted) {
  866. FURI_LOG_T(TAG, "Invalid command before auth session established: %02X", cmd);
  867. break;
  868. }
  869. if(cmd == MF_CLASSIC_READ_BLOCK_CMD) {
  870. uint8_t block = plain_data[1];
  871. uint8_t block_data[MF_CLASSIC_BLOCK_SIZE + 2] = {};
  872. memcpy(block_data, emulator->data.block[block].value, MF_CLASSIC_BLOCK_SIZE);
  873. if(mf_classic_is_sector_trailer(block)) {
  874. if(!mf_classic_is_allowed_access(
  875. emulator, block, access_key, MfClassicActionKeyARead)) {
  876. memset(block_data, 0, 6); //-V1086
  877. }
  878. if(!mf_classic_is_allowed_access(
  879. emulator, block, access_key, MfClassicActionKeyBRead)) {
  880. memset(&block_data[10], 0, 6);
  881. }
  882. if(!mf_classic_is_allowed_access(
  883. emulator, block, access_key, MfClassicActionACRead)) {
  884. memset(&block_data[6], 0, 4);
  885. }
  886. } else if(!mf_classic_is_allowed_access(
  887. emulator, block, access_key, MfClassicActionDataRead)) {
  888. // Send NACK
  889. uint8_t nack = 0x04;
  890. crypto1_encrypt(
  891. &emulator->crypto, NULL, &nack, 4, tx_rx->tx_data, tx_rx->tx_parity);
  892. tx_rx->tx_rx_type = FuriHalNfcTxRxTransparent;
  893. tx_rx->tx_bits = 4;
  894. furi_hal_nfc_tx_rx(tx_rx, 300);
  895. break;
  896. }
  897. nfca_append_crc16(block_data, 16);
  898. crypto1_encrypt(
  899. &emulator->crypto,
  900. NULL,
  901. block_data,
  902. sizeof(block_data) * 8,
  903. tx_rx->tx_data,
  904. tx_rx->tx_parity);
  905. tx_rx->tx_bits = (MF_CLASSIC_BLOCK_SIZE + 2) * 8;
  906. tx_rx->tx_rx_type = FuriHalNfcTxRxTransparent;
  907. } else if(cmd == MF_CLASSIC_WRITE_BLOCK_CMD) {
  908. uint8_t block = plain_data[1];
  909. if(block > mf_classic_get_total_block_num(emulator->data.type)) {
  910. break;
  911. }
  912. // Send ACK
  913. uint8_t ack = MF_CLASSIC_ACK_CMD;
  914. crypto1_encrypt(&emulator->crypto, NULL, &ack, 4, tx_rx->tx_data, tx_rx->tx_parity);
  915. tx_rx->tx_rx_type = FuriHalNfcTxRxTransparent;
  916. tx_rx->tx_bits = 4;
  917. if(!furi_hal_nfc_tx_rx(tx_rx, 300)) break;
  918. if(tx_rx->rx_bits != (MF_CLASSIC_BLOCK_SIZE + 2) * 8) break;
  919. crypto1_decrypt(&emulator->crypto, tx_rx->rx_data, tx_rx->rx_bits, plain_data);
  920. uint8_t block_data[MF_CLASSIC_BLOCK_SIZE] = {};
  921. memcpy(block_data, emulator->data.block[block].value, MF_CLASSIC_BLOCK_SIZE);
  922. if(mf_classic_is_sector_trailer(block)) {
  923. if(mf_classic_is_allowed_access(
  924. emulator, block, access_key, MfClassicActionKeyAWrite)) {
  925. memcpy(block_data, plain_data, 6); //-V1086
  926. }
  927. if(mf_classic_is_allowed_access(
  928. emulator, block, access_key, MfClassicActionKeyBWrite)) {
  929. memcpy(&block_data[10], &plain_data[10], 6);
  930. }
  931. if(mf_classic_is_allowed_access(
  932. emulator, block, access_key, MfClassicActionACWrite)) {
  933. memcpy(&block_data[6], &plain_data[6], 4);
  934. }
  935. } else {
  936. if(mf_classic_is_allowed_access(
  937. emulator, block, access_key, MfClassicActionDataWrite)) {
  938. memcpy(block_data, plain_data, MF_CLASSIC_BLOCK_SIZE);
  939. } else {
  940. break;
  941. }
  942. }
  943. if(memcmp(block_data, emulator->data.block[block].value, MF_CLASSIC_BLOCK_SIZE) != 0) {
  944. memcpy(emulator->data.block[block].value, block_data, MF_CLASSIC_BLOCK_SIZE);
  945. emulator->data_changed = true;
  946. }
  947. // Send ACK
  948. ack = MF_CLASSIC_ACK_CMD;
  949. crypto1_encrypt(&emulator->crypto, NULL, &ack, 4, tx_rx->tx_data, tx_rx->tx_parity);
  950. tx_rx->tx_rx_type = FuriHalNfcTxRxTransparent;
  951. tx_rx->tx_bits = 4;
  952. } else if(
  953. cmd == MF_CLASSIC_DECREMENT_CMD || cmd == MF_CLASSIC_INCREMENT_CMD ||
  954. cmd == MF_CLASSIC_RESTORE_CMD) {
  955. uint8_t block = plain_data[1];
  956. if(block > mf_classic_get_total_block_num(emulator->data.type)) {
  957. break;
  958. }
  959. MfClassicAction action = MfClassicActionDataDec;
  960. if(cmd == MF_CLASSIC_INCREMENT_CMD) {
  961. action = MfClassicActionDataInc;
  962. }
  963. if(!mf_classic_is_allowed_access(emulator, block, access_key, action)) {
  964. break;
  965. }
  966. int32_t prev_value;
  967. uint8_t addr;
  968. if(!mf_classic_block_to_value(emulator->data.block[block].value, &prev_value, &addr)) {
  969. break;
  970. }
  971. // Send ACK
  972. uint8_t ack = MF_CLASSIC_ACK_CMD;
  973. crypto1_encrypt(&emulator->crypto, NULL, &ack, 4, tx_rx->tx_data, tx_rx->tx_parity);
  974. tx_rx->tx_rx_type = FuriHalNfcTxRxTransparent;
  975. tx_rx->tx_bits = 4;
  976. if(!furi_hal_nfc_tx_rx(tx_rx, 300)) break;
  977. if(tx_rx->rx_bits != (sizeof(int32_t) + 2) * 8) break;
  978. crypto1_decrypt(&emulator->crypto, tx_rx->rx_data, tx_rx->rx_bits, plain_data);
  979. int32_t value = *(int32_t*)&plain_data[0];
  980. if(value < 0) {
  981. value = -value;
  982. }
  983. if(cmd == MF_CLASSIC_DECREMENT_CMD) {
  984. value = -value;
  985. } else if(cmd == MF_CLASSIC_RESTORE_CMD) {
  986. value = 0;
  987. }
  988. mf_classic_value_to_block(prev_value + value, addr, transfer_buf);
  989. transfer_buf_valid = true;
  990. // Commands do not ACK
  991. tx_rx->tx_bits = 0;
  992. } else if(cmd == MF_CLASSIC_TRANSFER_CMD) {
  993. uint8_t block = plain_data[1];
  994. if(!mf_classic_is_allowed_access(emulator, block, access_key, MfClassicActionDataDec)) {
  995. break;
  996. }
  997. if(memcmp(transfer_buf, emulator->data.block[block].value, MF_CLASSIC_BLOCK_SIZE) !=
  998. 0) {
  999. memcpy(emulator->data.block[block].value, transfer_buf, MF_CLASSIC_BLOCK_SIZE);
  1000. emulator->data_changed = true;
  1001. }
  1002. transfer_buf_valid = false;
  1003. uint8_t ack = MF_CLASSIC_ACK_CMD;
  1004. crypto1_encrypt(&emulator->crypto, NULL, &ack, 4, tx_rx->tx_data, tx_rx->tx_parity);
  1005. tx_rx->tx_rx_type = FuriHalNfcTxRxTransparent;
  1006. tx_rx->tx_bits = 4;
  1007. } else {
  1008. FURI_LOG_T(TAG, "Unknown command: %02X", cmd);
  1009. break;
  1010. }
  1011. }
  1012. if(!command_processed) {
  1013. // Send NACK
  1014. uint8_t nack = transfer_buf_valid ? MF_CLASSIC_NACK_BUF_VALID_CMD :
  1015. MF_CLASSIC_NACK_BUF_INVALID_CMD;
  1016. if(is_encrypted) {
  1017. crypto1_encrypt(&emulator->crypto, NULL, &nack, 4, tx_rx->tx_data, tx_rx->tx_parity);
  1018. } else {
  1019. tx_rx->tx_data[0] = nack;
  1020. }
  1021. tx_rx->tx_rx_type = FuriHalNfcTxRxTransparent;
  1022. tx_rx->tx_bits = 4;
  1023. furi_hal_nfc_tx_rx(tx_rx, 300);
  1024. }
  1025. return true;
  1026. }
  1027. void mf_classic_halt(FuriHalNfcTxRxContext* tx_rx, Crypto1* crypto) {
  1028. furi_assert(tx_rx);
  1029. uint8_t plain_data[4] = {0x50, 0x00, 0x00, 0x00};
  1030. nfca_append_crc16(plain_data, 2);
  1031. if(crypto) {
  1032. crypto1_encrypt(
  1033. crypto, NULL, plain_data, sizeof(plain_data) * 8, tx_rx->tx_data, tx_rx->tx_parity);
  1034. } else {
  1035. memcpy(tx_rx->tx_data, plain_data, sizeof(plain_data));
  1036. nfc_util_odd_parity(tx_rx->tx_data, tx_rx->tx_parity, sizeof(plain_data));
  1037. }
  1038. tx_rx->tx_bits = sizeof(plain_data) * 8;
  1039. tx_rx->tx_rx_type = FuriHalNfcTxRxTypeRaw;
  1040. furi_hal_nfc_tx_rx(tx_rx, 50);
  1041. }
  1042. bool mf_classic_write_block(
  1043. FuriHalNfcTxRxContext* tx_rx,
  1044. Crypto1* crypto,
  1045. uint8_t block_num,
  1046. MfClassicBlock* src_block) {
  1047. furi_assert(tx_rx);
  1048. furi_assert(crypto);
  1049. furi_assert(src_block);
  1050. bool write_success = false;
  1051. uint8_t plain_data[MF_CLASSIC_BLOCK_SIZE + 2] = {};
  1052. uint8_t resp;
  1053. do {
  1054. // Send write command
  1055. plain_data[0] = MF_CLASSIC_WRITE_BLOCK_CMD;
  1056. plain_data[1] = block_num;
  1057. nfca_append_crc16(plain_data, 2);
  1058. crypto1_encrypt(crypto, NULL, plain_data, 4 * 8, tx_rx->tx_data, tx_rx->tx_parity);
  1059. tx_rx->tx_bits = 4 * 8;
  1060. tx_rx->tx_rx_type = FuriHalNfcTxRxTypeRaw;
  1061. if(furi_hal_nfc_tx_rx(tx_rx, 50)) {
  1062. if(tx_rx->rx_bits == 4) {
  1063. crypto1_decrypt(crypto, tx_rx->rx_data, 4, &resp);
  1064. if(resp != 0x0A) {
  1065. FURI_LOG_D(TAG, "NACK received on write cmd: %02X", resp);
  1066. break;
  1067. }
  1068. } else {
  1069. FURI_LOG_D(TAG, "Not ACK received");
  1070. break;
  1071. }
  1072. } else {
  1073. FURI_LOG_D(TAG, "Failed to send write cmd");
  1074. break;
  1075. }
  1076. // Send data
  1077. memcpy(plain_data, src_block->value, MF_CLASSIC_BLOCK_SIZE);
  1078. nfca_append_crc16(plain_data, MF_CLASSIC_BLOCK_SIZE);
  1079. crypto1_encrypt(
  1080. crypto,
  1081. NULL,
  1082. plain_data,
  1083. (MF_CLASSIC_BLOCK_SIZE + 2) * 8,
  1084. tx_rx->tx_data,
  1085. tx_rx->tx_parity);
  1086. tx_rx->tx_bits = (MF_CLASSIC_BLOCK_SIZE + 2) * 8;
  1087. tx_rx->tx_rx_type = FuriHalNfcTxRxTypeRaw;
  1088. if(furi_hal_nfc_tx_rx(tx_rx, 50)) {
  1089. if(tx_rx->rx_bits == 4) {
  1090. crypto1_decrypt(crypto, tx_rx->rx_data, 4, &resp);
  1091. if(resp != MF_CLASSIC_ACK_CMD) {
  1092. FURI_LOG_D(TAG, "NACK received on sending data");
  1093. break;
  1094. }
  1095. } else {
  1096. FURI_LOG_D(TAG, "Not ACK received");
  1097. break;
  1098. }
  1099. } else {
  1100. FURI_LOG_D(TAG, "Failed to send data");
  1101. break;
  1102. }
  1103. write_success = true;
  1104. } while(false);
  1105. return write_success;
  1106. }
  1107. bool mf_classic_auth_write_block(
  1108. FuriHalNfcTxRxContext* tx_rx,
  1109. MfClassicBlock* src_block,
  1110. uint8_t block_num,
  1111. MfClassicKey key_type,
  1112. uint64_t key) {
  1113. furi_assert(tx_rx);
  1114. furi_assert(src_block);
  1115. Crypto1 crypto = {};
  1116. bool write_success = false;
  1117. do {
  1118. furi_hal_nfc_sleep();
  1119. if(!mf_classic_auth(tx_rx, block_num, key, key_type, &crypto, false, 0)) {
  1120. FURI_LOG_D(TAG, "Auth fail");
  1121. break;
  1122. }
  1123. if(!mf_classic_write_block(tx_rx, &crypto, block_num, src_block)) {
  1124. FURI_LOG_D(TAG, "Write fail");
  1125. break;
  1126. }
  1127. write_success = true;
  1128. mf_classic_halt(tx_rx, &crypto);
  1129. } while(false);
  1130. return write_success;
  1131. }
  1132. bool mf_classic_transfer(FuriHalNfcTxRxContext* tx_rx, Crypto1* crypto, uint8_t block_num) {
  1133. furi_assert(tx_rx);
  1134. furi_assert(crypto);
  1135. // Send transfer command
  1136. uint8_t plain_data[4] = {MF_CLASSIC_TRANSFER_CMD, block_num, 0, 0};
  1137. uint8_t resp = 0;
  1138. bool transfer_success = false;
  1139. nfca_append_crc16(plain_data, 2);
  1140. crypto1_encrypt(
  1141. crypto, NULL, plain_data, sizeof(plain_data) * 8, tx_rx->tx_data, tx_rx->tx_parity);
  1142. tx_rx->tx_bits = sizeof(plain_data) * 8;
  1143. tx_rx->tx_rx_type = FuriHalNfcTxRxTypeRaw;
  1144. do {
  1145. if(furi_hal_nfc_tx_rx(tx_rx, 50)) {
  1146. if(tx_rx->rx_bits == 4) {
  1147. crypto1_decrypt(crypto, tx_rx->rx_data, 4, &resp);
  1148. if(resp != 0x0A) {
  1149. FURI_LOG_D(TAG, "NACK received on transfer cmd: %02X", resp);
  1150. break;
  1151. }
  1152. } else {
  1153. FURI_LOG_D(TAG, "Not ACK received");
  1154. break;
  1155. }
  1156. } else {
  1157. FURI_LOG_D(TAG, "Failed to send transfer cmd");
  1158. break;
  1159. }
  1160. transfer_success = true;
  1161. } while(false);
  1162. return transfer_success;
  1163. }
  1164. bool mf_classic_value_cmd(
  1165. FuriHalNfcTxRxContext* tx_rx,
  1166. Crypto1* crypto,
  1167. uint8_t block_num,
  1168. uint8_t cmd,
  1169. int32_t d_value) {
  1170. furi_assert(tx_rx);
  1171. furi_assert(crypto);
  1172. furi_assert(
  1173. cmd == MF_CLASSIC_INCREMENT_CMD || cmd == MF_CLASSIC_DECREMENT_CMD ||
  1174. cmd == MF_CLASSIC_RESTORE_CMD);
  1175. furi_assert(d_value >= 0);
  1176. uint8_t plain_data[sizeof(d_value) + 2] = {};
  1177. uint8_t resp = 0;
  1178. bool success = false;
  1179. do {
  1180. // Send cmd
  1181. plain_data[0] = cmd;
  1182. plain_data[1] = block_num;
  1183. nfca_append_crc16(plain_data, 2);
  1184. crypto1_encrypt(crypto, NULL, plain_data, 4 * 8, tx_rx->tx_data, tx_rx->tx_parity);
  1185. tx_rx->tx_bits = 4 * 8;
  1186. tx_rx->tx_rx_type = FuriHalNfcTxRxTypeRaw;
  1187. if(furi_hal_nfc_tx_rx(tx_rx, 50)) {
  1188. if(tx_rx->rx_bits == 4) {
  1189. crypto1_decrypt(crypto, tx_rx->rx_data, 4, &resp);
  1190. if(resp != 0x0A) {
  1191. FURI_LOG_D(TAG, "NACK received on write cmd: %02X", resp);
  1192. break;
  1193. }
  1194. } else {
  1195. FURI_LOG_D(TAG, "Not ACK received");
  1196. break;
  1197. }
  1198. } else {
  1199. FURI_LOG_D(TAG, "Failed to send write cmd");
  1200. break;
  1201. }
  1202. // Send data
  1203. memcpy(plain_data, &d_value, sizeof(d_value));
  1204. nfca_append_crc16(plain_data, sizeof(d_value));
  1205. crypto1_encrypt(
  1206. crypto, NULL, plain_data, (sizeof(d_value) + 2) * 8, tx_rx->tx_data, tx_rx->tx_parity);
  1207. tx_rx->tx_bits = (sizeof(d_value) + 2) * 8;
  1208. tx_rx->tx_rx_type = FuriHalNfcTxRxTypeRaw;
  1209. // inc, dec, restore do not ACK, but they do NACK
  1210. if(furi_hal_nfc_tx_rx(tx_rx, 50)) {
  1211. if(tx_rx->rx_bits == 4) {
  1212. crypto1_decrypt(crypto, tx_rx->rx_data, 4, &resp);
  1213. if(resp != 0x0A) {
  1214. FURI_LOG_D(TAG, "NACK received on transfer cmd: %02X", resp);
  1215. break;
  1216. }
  1217. } else {
  1218. FURI_LOG_D(TAG, "Not NACK received");
  1219. break;
  1220. }
  1221. }
  1222. success = true;
  1223. } while(false);
  1224. return success;
  1225. }
  1226. bool mf_classic_value_cmd_full(
  1227. FuriHalNfcTxRxContext* tx_rx,
  1228. MfClassicBlock* src_block,
  1229. uint8_t block_num,
  1230. MfClassicKey key_type,
  1231. uint64_t key,
  1232. int32_t d_value) {
  1233. furi_assert(tx_rx);
  1234. furi_assert(src_block);
  1235. Crypto1 crypto = {};
  1236. uint8_t cmd;
  1237. bool success = false;
  1238. if(d_value > 0) {
  1239. cmd = MF_CLASSIC_INCREMENT_CMD;
  1240. } else if(d_value < 0) {
  1241. cmd = MF_CLASSIC_DECREMENT_CMD;
  1242. d_value = -d_value;
  1243. } else {
  1244. cmd = MF_CLASSIC_RESTORE_CMD;
  1245. }
  1246. do {
  1247. furi_hal_nfc_sleep();
  1248. if(!mf_classic_auth(tx_rx, block_num, key, key_type, &crypto, false, 0)) {
  1249. FURI_LOG_D(TAG, "Value cmd auth fail");
  1250. break;
  1251. }
  1252. if(!mf_classic_value_cmd(tx_rx, &crypto, block_num, cmd, d_value)) {
  1253. FURI_LOG_D(TAG, "Value cmd inc/dec/res fail");
  1254. break;
  1255. }
  1256. if(!mf_classic_transfer(tx_rx, &crypto, block_num)) {
  1257. FURI_LOG_D(TAG, "Value cmd transfer fail");
  1258. break;
  1259. }
  1260. success = true;
  1261. // Send Halt
  1262. mf_classic_halt(tx_rx, &crypto);
  1263. } while(false);
  1264. return success;
  1265. }
  1266. bool mf_classic_write_sector(
  1267. FuriHalNfcTxRxContext* tx_rx,
  1268. MfClassicData* dest_data,
  1269. MfClassicData* src_data,
  1270. uint8_t sec_num) {
  1271. furi_assert(tx_rx);
  1272. furi_assert(dest_data);
  1273. furi_assert(src_data);
  1274. uint8_t first_block = mf_classic_get_first_block_num_of_sector(sec_num);
  1275. uint8_t total_blocks = mf_classic_get_blocks_num_in_sector(sec_num);
  1276. MfClassicSectorTrailer* sec_tr = mf_classic_get_sector_trailer_by_sector(dest_data, sec_num);
  1277. bool key_a_found = mf_classic_is_key_found(dest_data, sec_num, MfClassicKeyA);
  1278. bool key_b_found = mf_classic_is_key_found(dest_data, sec_num, MfClassicKeyB);
  1279. bool write_success = true;
  1280. for(size_t i = first_block; i < first_block + total_blocks; i++) {
  1281. // Compare blocks
  1282. if(memcmp(dest_data->block[i].value, src_data->block[i].value, MF_CLASSIC_BLOCK_SIZE) !=
  1283. 0) {
  1284. if(mf_classic_is_value_block(dest_data, i)) {
  1285. bool key_a_inc_allowed = mf_classic_is_allowed_access_data_block(
  1286. dest_data, i, MfClassicKeyA, MfClassicActionDataInc);
  1287. bool key_b_inc_allowed = mf_classic_is_allowed_access_data_block(
  1288. dest_data, i, MfClassicKeyB, MfClassicActionDataInc);
  1289. bool key_a_dec_allowed = mf_classic_is_allowed_access_data_block(
  1290. dest_data, i, MfClassicKeyA, MfClassicActionDataDec);
  1291. bool key_b_dec_allowed = mf_classic_is_allowed_access_data_block(
  1292. dest_data, i, MfClassicKeyB, MfClassicActionDataDec);
  1293. int32_t src_value, dst_value;
  1294. mf_classic_block_to_value(src_data->block[i].value, &src_value, NULL);
  1295. mf_classic_block_to_value(dest_data->block[i].value, &dst_value, NULL);
  1296. int32_t diff = src_value - dst_value;
  1297. if(diff > 0) {
  1298. if(key_a_found && key_a_inc_allowed) {
  1299. FURI_LOG_I(TAG, "Incrementing block %d with key A by %ld", i, diff);
  1300. uint64_t key = nfc_util_bytes2num(sec_tr->key_a, 6);
  1301. if(!mf_classic_value_cmd_full(
  1302. tx_rx, &src_data->block[i], i, MfClassicKeyA, key, diff)) {
  1303. FURI_LOG_E(TAG, "Failed to increment block %d", i);
  1304. write_success = false;
  1305. break;
  1306. }
  1307. } else if(key_b_found && key_b_inc_allowed) {
  1308. FURI_LOG_I(TAG, "Incrementing block %d with key B by %ld", i, diff);
  1309. uint64_t key = nfc_util_bytes2num(sec_tr->key_b, 6);
  1310. if(!mf_classic_value_cmd_full(
  1311. tx_rx, &src_data->block[i], i, MfClassicKeyB, key, diff)) {
  1312. FURI_LOG_E(TAG, "Failed to increment block %d", i);
  1313. write_success = false;
  1314. break;
  1315. }
  1316. } else {
  1317. FURI_LOG_E(TAG, "Failed to increment block %d", i);
  1318. }
  1319. } else if(diff < 0) {
  1320. if(key_a_found && key_a_dec_allowed) {
  1321. FURI_LOG_I(TAG, "Decrementing block %d with key A by %ld", i, -diff);
  1322. uint64_t key = nfc_util_bytes2num(sec_tr->key_a, 6);
  1323. if(!mf_classic_value_cmd_full(
  1324. tx_rx, &src_data->block[i], i, MfClassicKeyA, key, diff)) {
  1325. FURI_LOG_E(TAG, "Failed to decrement block %d", i);
  1326. write_success = false;
  1327. break;
  1328. }
  1329. } else if(key_b_found && key_b_dec_allowed) {
  1330. FURI_LOG_I(TAG, "Decrementing block %d with key B by %ld", i, diff);
  1331. uint64_t key = nfc_util_bytes2num(sec_tr->key_b, 6);
  1332. if(!mf_classic_value_cmd_full(
  1333. tx_rx, &src_data->block[i], i, MfClassicKeyB, key, diff)) {
  1334. FURI_LOG_E(TAG, "Failed to decrement block %d", i);
  1335. write_success = false;
  1336. break;
  1337. }
  1338. } else {
  1339. FURI_LOG_E(TAG, "Failed to decrement block %d", i);
  1340. }
  1341. } else {
  1342. FURI_LOG_E(TAG, "Value block %d address changed, cannot write it", i);
  1343. }
  1344. } else {
  1345. bool key_a_write_allowed = mf_classic_is_allowed_access_data_block(
  1346. dest_data, i, MfClassicKeyA, MfClassicActionDataWrite);
  1347. bool key_b_write_allowed = mf_classic_is_allowed_access_data_block(
  1348. dest_data, i, MfClassicKeyB, MfClassicActionDataWrite);
  1349. if(key_a_found && key_a_write_allowed) {
  1350. FURI_LOG_I(TAG, "Writing block %d with key A", i);
  1351. uint64_t key = nfc_util_bytes2num(sec_tr->key_a, 6);
  1352. if(!mf_classic_auth_write_block(
  1353. tx_rx, &src_data->block[i], i, MfClassicKeyA, key)) {
  1354. FURI_LOG_E(TAG, "Failed to write block %d", i);
  1355. write_success = false;
  1356. break;
  1357. }
  1358. } else if(key_b_found && key_b_write_allowed) {
  1359. FURI_LOG_I(TAG, "Writing block %d with key A", i);
  1360. uint64_t key = nfc_util_bytes2num(sec_tr->key_b, 6);
  1361. if(!mf_classic_auth_write_block(
  1362. tx_rx, &src_data->block[i], i, MfClassicKeyB, key)) {
  1363. FURI_LOG_E(TAG, "Failed to write block %d", i);
  1364. write_success = false;
  1365. break;
  1366. }
  1367. } else {
  1368. FURI_LOG_E(TAG, "Failed to find key with write access");
  1369. write_success = false;
  1370. break;
  1371. }
  1372. }
  1373. } else {
  1374. FURI_LOG_D(TAG, "Blocks %d are equal", i);
  1375. }
  1376. }
  1377. return write_success;
  1378. }