mifare_classic.c 53 KB

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