mifare_ultralight.c 79 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946
  1. #include <limits.h>
  2. #include <mbedtls/sha1.h>
  3. #include "mifare_ultralight.h"
  4. #include "nfc_util.h"
  5. #include <furi.h>
  6. #include "../furi_hal_nfc.h"
  7. #define TAG "MfUltralight"
  8. // Algorithms from: https://github.com/RfidResearchGroup/proxmark3/blob/0f6061c16f072372b7d4d381911f1542afbc3a69/common/generator.c#L110
  9. uint32_t mf_ul_pwdgen_xiaomi(FurryHalNfcDevData* data) {
  10. uint8_t hash[20];
  11. mbedtls_sha1(data->uid, data->uid_len, hash);
  12. uint32_t pwd = 0;
  13. pwd |= (hash[hash[0] % 20]) << 24;
  14. pwd |= (hash[(hash[0] + 5) % 20]) << 16;
  15. pwd |= (hash[(hash[0] + 13) % 20]) << 8;
  16. pwd |= (hash[(hash[0] + 17) % 20]);
  17. return pwd;
  18. }
  19. uint32_t mf_ul_pwdgen_amiibo(FurryHalNfcDevData* data) {
  20. uint8_t* uid = data->uid;
  21. uint32_t pwd = 0;
  22. pwd |= (uid[1] ^ uid[3] ^ 0xAA) << 24;
  23. pwd |= (uid[2] ^ uid[4] ^ 0x55) << 16;
  24. pwd |= (uid[3] ^ uid[5] ^ 0xAA) << 8;
  25. pwd |= uid[4] ^ uid[6] ^ 0x55;
  26. return pwd;
  27. }
  28. bool mf_ul_check_card_type(uint8_t ATQA0, uint8_t ATQA1, uint8_t SAK) {
  29. if((ATQA0 == 0x44) && (ATQA1 == 0x00) && (SAK == 0x00)) {
  30. return true;
  31. }
  32. return false;
  33. }
  34. void mf_ul_reset(MfUltralightData* data) {
  35. furi_assert(data);
  36. data->type = MfUltralightTypeUnknown;
  37. memset(&data->version, 0, sizeof(MfUltralightVersion));
  38. memset(data->signature, 0, sizeof(data->signature));
  39. memset(data->counter, 0, sizeof(data->counter));
  40. memset(data->tearing, 0, sizeof(data->tearing));
  41. memset(data->data, 0, sizeof(data->data));
  42. data->data_size = 0;
  43. data->data_read = 0;
  44. data->curr_authlim = 0;
  45. data->auth_success = false;
  46. }
  47. static MfUltralightFeatures mf_ul_get_features(MfUltralightType type) {
  48. switch(type) {
  49. case MfUltralightTypeUL11:
  50. case MfUltralightTypeUL21:
  51. return MfUltralightSupportFastRead | MfUltralightSupportCompatWrite |
  52. MfUltralightSupportReadCounter | MfUltralightSupportIncrCounter |
  53. MfUltralightSupportAuth | MfUltralightSupportSignature |
  54. MfUltralightSupportTearingFlags | MfUltralightSupportVcsl;
  55. case MfUltralightTypeNTAG213:
  56. case MfUltralightTypeNTAG215:
  57. case MfUltralightTypeNTAG216:
  58. return MfUltralightSupportFastRead | MfUltralightSupportCompatWrite |
  59. MfUltralightSupportReadCounter | MfUltralightSupportAuth |
  60. MfUltralightSupportSignature | MfUltralightSupportSingleCounter |
  61. MfUltralightSupportAsciiMirror;
  62. case MfUltralightTypeNTAGI2C1K:
  63. case MfUltralightTypeNTAGI2C2K:
  64. return MfUltralightSupportFastRead | MfUltralightSupportSectorSelect;
  65. case MfUltralightTypeNTAGI2CPlus1K:
  66. case MfUltralightTypeNTAGI2CPlus2K:
  67. return MfUltralightSupportFastRead | MfUltralightSupportAuth |
  68. MfUltralightSupportFastWrite | MfUltralightSupportSignature |
  69. MfUltralightSupportSectorSelect;
  70. case MfUltralightTypeNTAG203:
  71. return MfUltralightSupportCompatWrite | MfUltralightSupportCounterInMemory;
  72. case MfUltralightTypeULC:
  73. return MfUltralightSupportCompatWrite | MfUltralightSupport3DesAuth;
  74. default:
  75. // Assumed original MFUL 512-bit
  76. return MfUltralightSupportCompatWrite;
  77. }
  78. }
  79. static void mf_ul_set_default_version(MfUltralightReader* reader, MfUltralightData* data) {
  80. data->type = MfUltralightTypeUnknown;
  81. reader->pages_to_read = 16;
  82. }
  83. static void mf_ul_set_version_ntag203(MfUltralightReader* reader, MfUltralightData* data) {
  84. data->type = MfUltralightTypeNTAG203;
  85. reader->pages_to_read = 42;
  86. }
  87. static void mf_ul_set_version_ulc(MfUltralightReader* reader, MfUltralightData* data) {
  88. data->type = MfUltralightTypeULC;
  89. reader->pages_to_read = 48;
  90. }
  91. bool mf_ultralight_read_version(
  92. FurryHalNfcTxRxContext* tx_rx,
  93. MfUltralightReader* reader,
  94. MfUltralightData* data) {
  95. bool version_read = false;
  96. do {
  97. FURI_LOG_D(TAG, "Reading version");
  98. tx_rx->tx_data[0] = MF_UL_GET_VERSION_CMD;
  99. tx_rx->tx_bits = 8;
  100. tx_rx->tx_rx_type = FurryHalNfcTxRxTypeDefault;
  101. if(!furry_hal_nfc_tx_rx(tx_rx, 50) || tx_rx->rx_bits != 64) {
  102. FURI_LOG_D(TAG, "Failed reading version");
  103. mf_ul_set_default_version(reader, data);
  104. furry_hal_nfc_sleep();
  105. furry_hal_nfc_activate_nfca(300, NULL);
  106. break;
  107. }
  108. MfUltralightVersion* version = (MfUltralightVersion*)tx_rx->rx_data;
  109. data->version = *version;
  110. if(version->storage_size == 0x0B || version->storage_size == 0x00) {
  111. data->type = MfUltralightTypeUL11;
  112. reader->pages_to_read = 20;
  113. } else if(version->storage_size == 0x0E) {
  114. data->type = MfUltralightTypeUL21;
  115. reader->pages_to_read = 41;
  116. } else if(version->storage_size == 0x0F) {
  117. data->type = MfUltralightTypeNTAG213;
  118. reader->pages_to_read = 45;
  119. } else if(version->storage_size == 0x11) {
  120. data->type = MfUltralightTypeNTAG215;
  121. reader->pages_to_read = 135;
  122. } else if(version->prod_subtype == 5 && version->prod_ver_major == 2) {
  123. // NTAG I2C
  124. bool known = false;
  125. if(version->prod_ver_minor == 1) {
  126. if(version->storage_size == 0x13) {
  127. data->type = MfUltralightTypeNTAGI2C1K;
  128. reader->pages_to_read = 231;
  129. known = true;
  130. } else if(version->storage_size == 0x15) {
  131. data->type = MfUltralightTypeNTAGI2C2K;
  132. reader->pages_to_read = 485;
  133. known = true;
  134. }
  135. } else if(version->prod_ver_minor == 2) {
  136. if(version->storage_size == 0x13) {
  137. data->type = MfUltralightTypeNTAGI2CPlus1K;
  138. reader->pages_to_read = 236;
  139. known = true;
  140. } else if(version->storage_size == 0x15) {
  141. data->type = MfUltralightTypeNTAGI2CPlus2K;
  142. reader->pages_to_read = 492;
  143. known = true;
  144. }
  145. }
  146. if(!known) {
  147. mf_ul_set_default_version(reader, data);
  148. }
  149. } else if(version->storage_size == 0x13) {
  150. data->type = MfUltralightTypeNTAG216;
  151. reader->pages_to_read = 231;
  152. } else {
  153. mf_ul_set_default_version(reader, data);
  154. break;
  155. }
  156. version_read = true;
  157. } while(false);
  158. reader->supported_features = mf_ul_get_features(data->type);
  159. return version_read;
  160. }
  161. bool mf_ultralight_authenticate(FurryHalNfcTxRxContext* tx_rx, uint32_t key, uint16_t* pack) {
  162. furi_assert(pack);
  163. bool authenticated = false;
  164. do {
  165. FURI_LOG_D(TAG, "Authenticating");
  166. tx_rx->tx_data[0] = MF_UL_PWD_AUTH;
  167. nfc_util_num2bytes(key, 4, &tx_rx->tx_data[1]);
  168. tx_rx->tx_bits = 40;
  169. tx_rx->tx_rx_type = FurryHalNfcTxRxTypeDefault;
  170. if(!furry_hal_nfc_tx_rx(tx_rx, 50)) {
  171. FURI_LOG_D(TAG, "Tag did not respond to authentication");
  172. break;
  173. }
  174. // PACK
  175. if(tx_rx->rx_bits < 2 * 8) {
  176. FURI_LOG_D(TAG, "Authentication failed");
  177. break;
  178. }
  179. *pack = (tx_rx->rx_data[1] << 8) | tx_rx->rx_data[0];
  180. FURI_LOG_I(TAG, "Auth success. Password: %08lX. PACK: %04X", key, *pack);
  181. authenticated = true;
  182. } while(false);
  183. return authenticated;
  184. }
  185. static int16_t mf_ultralight_page_addr_to_tag_addr(uint8_t sector, uint8_t page) {
  186. return sector * 256 + page;
  187. }
  188. static int16_t mf_ultralight_ntag_i2c_addr_lin_to_tag_1k(
  189. int16_t linear_address,
  190. uint8_t* sector,
  191. int16_t* valid_pages) {
  192. // 0 - 226: sector 0
  193. // 227 - 228: config registers
  194. // 229 - 230: session registers
  195. if(linear_address > 230) {
  196. *valid_pages = 0;
  197. return -1;
  198. } else if(linear_address >= 229) {
  199. *sector = 3;
  200. *valid_pages = 2 - (linear_address - 229);
  201. return linear_address - 229 + 248;
  202. } else if(linear_address >= 227) {
  203. *sector = 0;
  204. *valid_pages = 2 - (linear_address - 227);
  205. return linear_address - 227 + 232;
  206. } else {
  207. *sector = 0;
  208. *valid_pages = 227 - linear_address;
  209. return linear_address;
  210. }
  211. }
  212. static int16_t mf_ultralight_ntag_i2c_addr_lin_to_tag_2k(
  213. int16_t linear_address,
  214. uint8_t* sector,
  215. int16_t* valid_pages) {
  216. // 0 - 255: sector 0
  217. // 256 - 480: sector 1
  218. // 481 - 482: config registers
  219. // 483 - 484: session registers
  220. if(linear_address > 484) {
  221. *valid_pages = 0;
  222. return -1;
  223. } else if(linear_address >= 483) {
  224. *sector = 3;
  225. *valid_pages = 2 - (linear_address - 483);
  226. return linear_address - 483 + 248;
  227. } else if(linear_address >= 481) {
  228. *sector = 1;
  229. *valid_pages = 2 - (linear_address - 481);
  230. return linear_address - 481 + 232;
  231. } else if(linear_address >= 256) {
  232. *sector = 1;
  233. *valid_pages = 225 - (linear_address - 256);
  234. return linear_address - 256;
  235. } else {
  236. *sector = 0;
  237. *valid_pages = 256 - linear_address;
  238. return linear_address;
  239. }
  240. }
  241. static int16_t mf_ultralight_ntag_i2c_addr_lin_to_tag_plus_1k(
  242. int16_t linear_address,
  243. uint8_t* sector,
  244. int16_t* valid_pages) {
  245. // 0 - 233: sector 0 + registers
  246. // 234 - 235: session registers
  247. if(linear_address > 235) {
  248. *valid_pages = 0;
  249. return -1;
  250. } else if(linear_address >= 234) {
  251. *sector = 0;
  252. *valid_pages = 2 - (linear_address - 234);
  253. return linear_address - 234 + 236;
  254. } else {
  255. *sector = 0;
  256. *valid_pages = 234 - linear_address;
  257. return linear_address;
  258. }
  259. }
  260. static int16_t mf_ultralight_ntag_i2c_addr_lin_to_tag_plus_2k(
  261. int16_t linear_address,
  262. uint8_t* sector,
  263. int16_t* valid_pages) {
  264. // 0 - 233: sector 0 + registers
  265. // 234 - 235: session registers
  266. // 236 - 491: sector 1
  267. if(linear_address > 491) {
  268. *valid_pages = 0;
  269. return -1;
  270. } else if(linear_address >= 236) {
  271. *sector = 1;
  272. *valid_pages = 256 - (linear_address - 236);
  273. return linear_address - 236;
  274. } else if(linear_address >= 234) {
  275. *sector = 0;
  276. *valid_pages = 2 - (linear_address - 234);
  277. return linear_address - 234 + 236;
  278. } else {
  279. *sector = 0;
  280. *valid_pages = 234 - linear_address;
  281. return linear_address;
  282. }
  283. }
  284. static int16_t mf_ultralight_ntag_i2c_addr_lin_to_tag(
  285. MfUltralightData* data,
  286. MfUltralightReader* reader,
  287. int16_t linear_address,
  288. uint8_t* sector,
  289. int16_t* valid_pages) {
  290. switch(data->type) {
  291. case MfUltralightTypeNTAGI2C1K:
  292. return mf_ultralight_ntag_i2c_addr_lin_to_tag_1k(linear_address, sector, valid_pages);
  293. case MfUltralightTypeNTAGI2C2K:
  294. return mf_ultralight_ntag_i2c_addr_lin_to_tag_2k(linear_address, sector, valid_pages);
  295. case MfUltralightTypeNTAGI2CPlus1K:
  296. return mf_ultralight_ntag_i2c_addr_lin_to_tag_plus_1k(linear_address, sector, valid_pages);
  297. case MfUltralightTypeNTAGI2CPlus2K:
  298. return mf_ultralight_ntag_i2c_addr_lin_to_tag_plus_2k(linear_address, sector, valid_pages);
  299. default:
  300. *sector = 0xff;
  301. *valid_pages = reader->pages_to_read - linear_address;
  302. return linear_address;
  303. }
  304. }
  305. static int16_t
  306. mf_ultralight_ntag_i2c_addr_tag_to_lin_1k(uint8_t page, uint8_t sector, uint16_t* valid_pages) {
  307. bool valid = false;
  308. int16_t translated_page;
  309. if(sector == 0) {
  310. if(page <= 226) {
  311. *valid_pages = 227 - page;
  312. translated_page = page;
  313. valid = true;
  314. } else if(page >= 232 && page <= 233) {
  315. *valid_pages = 2 - (page - 232);
  316. translated_page = page - 232 + 227;
  317. valid = true;
  318. }
  319. } else if(sector == 3) {
  320. if(page >= 248 && page <= 249) {
  321. *valid_pages = 2 - (page - 248);
  322. translated_page = page - 248 + 229;
  323. valid = true;
  324. }
  325. }
  326. if(!valid) {
  327. *valid_pages = 0;
  328. translated_page = -1;
  329. }
  330. return translated_page;
  331. }
  332. static int16_t
  333. mf_ultralight_ntag_i2c_addr_tag_to_lin_2k(uint8_t page, uint8_t sector, uint16_t* valid_pages) {
  334. bool valid = false;
  335. int16_t translated_page;
  336. if(sector == 0) {
  337. *valid_pages = 256 - page;
  338. translated_page = page;
  339. valid = true;
  340. } else if(sector == 1) {
  341. if(page <= 224) {
  342. *valid_pages = 225 - page;
  343. translated_page = 256 + page;
  344. valid = true;
  345. } else if(page >= 232 && page <= 233) {
  346. *valid_pages = 2 - (page - 232);
  347. translated_page = page - 232 + 481;
  348. valid = true;
  349. }
  350. } else if(sector == 3) {
  351. if(page >= 248 && page <= 249) {
  352. *valid_pages = 2 - (page - 248);
  353. translated_page = page - 248 + 483;
  354. valid = true;
  355. }
  356. }
  357. if(!valid) {
  358. *valid_pages = 0;
  359. translated_page = -1;
  360. }
  361. return translated_page;
  362. }
  363. static int16_t mf_ultralight_ntag_i2c_addr_tag_to_lin_plus_1k(
  364. uint8_t page,
  365. uint8_t sector,
  366. uint16_t* valid_pages) {
  367. bool valid = false;
  368. int16_t translated_page;
  369. if(sector == 0) {
  370. if(page <= 233) {
  371. *valid_pages = 234 - page;
  372. translated_page = page;
  373. valid = true;
  374. } else if(page >= 236 && page <= 237) {
  375. *valid_pages = 2 - (page - 236);
  376. translated_page = page - 236 + 234;
  377. valid = true;
  378. }
  379. } else if(sector == 3) {
  380. if(page >= 248 && page <= 249) {
  381. *valid_pages = 2 - (page - 248);
  382. translated_page = page - 248 + 234;
  383. valid = true;
  384. }
  385. }
  386. if(!valid) {
  387. *valid_pages = 0;
  388. translated_page = -1;
  389. }
  390. return translated_page;
  391. }
  392. static int16_t mf_ultralight_ntag_i2c_addr_tag_to_lin_plus_2k(
  393. uint8_t page,
  394. uint8_t sector,
  395. uint16_t* valid_pages) {
  396. bool valid = false;
  397. int16_t translated_page;
  398. if(sector == 0) {
  399. if(page <= 233) {
  400. *valid_pages = 234 - page;
  401. translated_page = page;
  402. valid = true;
  403. } else if(page >= 236 && page <= 237) {
  404. *valid_pages = 2 - (page - 236);
  405. translated_page = page - 236 + 234;
  406. valid = true;
  407. }
  408. } else if(sector == 1) {
  409. *valid_pages = 256 - page;
  410. translated_page = page + 236;
  411. valid = true;
  412. } else if(sector == 3) {
  413. if(page >= 248 && page <= 249) {
  414. *valid_pages = 2 - (page - 248);
  415. translated_page = page - 248 + 234;
  416. valid = true;
  417. }
  418. }
  419. if(!valid) {
  420. *valid_pages = 0;
  421. translated_page = -1;
  422. }
  423. return translated_page;
  424. }
  425. static int16_t mf_ultralight_ntag_i2c_addr_tag_to_lin(
  426. MfUltralightData* data,
  427. uint8_t page,
  428. uint8_t sector,
  429. uint16_t* valid_pages) {
  430. switch(data->type) {
  431. case MfUltralightTypeNTAGI2C1K:
  432. return mf_ultralight_ntag_i2c_addr_tag_to_lin_1k(page, sector, valid_pages);
  433. case MfUltralightTypeNTAGI2C2K:
  434. return mf_ultralight_ntag_i2c_addr_tag_to_lin_2k(page, sector, valid_pages);
  435. case MfUltralightTypeNTAGI2CPlus1K:
  436. return mf_ultralight_ntag_i2c_addr_tag_to_lin_plus_1k(page, sector, valid_pages);
  437. case MfUltralightTypeNTAGI2CPlus2K:
  438. return mf_ultralight_ntag_i2c_addr_tag_to_lin_plus_2k(page, sector, valid_pages);
  439. default:
  440. *valid_pages = data->data_size / 4 - page;
  441. return page;
  442. }
  443. }
  444. MfUltralightConfigPages* mf_ultralight_get_config_pages(MfUltralightData* data) {
  445. if(data->type >= MfUltralightTypeUL11 && data->type <= MfUltralightTypeNTAG216) {
  446. return (MfUltralightConfigPages*)&data->data[data->data_size - 4 * 4];
  447. } else if(
  448. data->type >= MfUltralightTypeNTAGI2CPlus1K &&
  449. data->type <= MfUltralightTypeNTAGI2CPlus2K) {
  450. return (MfUltralightConfigPages*)&data->data[0xe3 * 4]; //-V641
  451. } else {
  452. return NULL;
  453. }
  454. }
  455. static uint16_t mf_ultralight_calc_auth_count(MfUltralightData* data) {
  456. if(mf_ul_get_features(data->type) & MfUltralightSupportAuth) {
  457. MfUltralightConfigPages* config = mf_ultralight_get_config_pages(data);
  458. uint16_t scaled_authlim = config->access.authlim;
  459. // NTAG I2C Plus uses 2^AUTHLIM attempts rather than the direct number
  460. if(scaled_authlim > 0 && data->type >= MfUltralightTypeNTAGI2CPlus1K &&
  461. data->type <= MfUltralightTypeNTAGI2CPlus2K) {
  462. scaled_authlim = 1 << scaled_authlim;
  463. }
  464. return scaled_authlim;
  465. }
  466. return 0;
  467. }
  468. // NTAG21x will NAK if NFC_CNT_EN unset, so preempt
  469. static bool mf_ultralight_should_read_counters(MfUltralightData* data) {
  470. if(data->type < MfUltralightTypeNTAG213 || data->type > MfUltralightTypeNTAG216) return true;
  471. MfUltralightConfigPages* config = mf_ultralight_get_config_pages(data);
  472. return config->access.nfc_cnt_en;
  473. }
  474. static bool mf_ultralight_sector_select(FurryHalNfcTxRxContext* tx_rx, uint8_t sector) {
  475. FURI_LOG_D(TAG, "Selecting sector %u", sector);
  476. tx_rx->tx_data[0] = MF_UL_SECTOR_SELECT;
  477. tx_rx->tx_data[1] = 0xff;
  478. tx_rx->tx_bits = 16;
  479. tx_rx->tx_rx_type = FurryHalNfcTxRxTypeDefault;
  480. if(!furry_hal_nfc_tx_rx(tx_rx, 50)) {
  481. FURI_LOG_D(TAG, "Failed to issue sector select command");
  482. return false;
  483. }
  484. tx_rx->tx_data[0] = sector;
  485. tx_rx->tx_data[1] = 0x00;
  486. tx_rx->tx_data[2] = 0x00;
  487. tx_rx->tx_data[3] = 0x00;
  488. tx_rx->tx_bits = 32;
  489. tx_rx->tx_rx_type = FurryHalNfcTxRxTypeDefault;
  490. // This is NOT a typo! The tag ACKs by not sending a response within 1ms.
  491. if(furry_hal_nfc_tx_rx(tx_rx, 20)) {
  492. // TODO: what gets returned when an actual NAK is received?
  493. FURI_LOG_D(TAG, "Sector %u select NAK'd", sector);
  494. return false;
  495. }
  496. return true;
  497. }
  498. bool mf_ultralight_read_pages_direct(
  499. FurryHalNfcTxRxContext* tx_rx,
  500. uint8_t start_index,
  501. uint8_t* data) {
  502. FURI_LOG_D(TAG, "Reading pages %d - %d", start_index, start_index + 3);
  503. tx_rx->tx_data[0] = MF_UL_READ_CMD;
  504. tx_rx->tx_data[1] = start_index;
  505. tx_rx->tx_bits = 16;
  506. tx_rx->tx_rx_type = FurryHalNfcTxRxTypeDefault;
  507. if(!furry_hal_nfc_tx_rx(tx_rx, 50) || tx_rx->rx_bits < 16 * 8) {
  508. FURI_LOG_D(TAG, "Failed to read pages %d - %d", start_index, start_index + 3);
  509. return false;
  510. }
  511. memcpy(data, tx_rx->rx_data, 16); //-V1086
  512. return true;
  513. }
  514. bool mf_ultralight_read_pages(
  515. FurryHalNfcTxRxContext* tx_rx,
  516. MfUltralightReader* reader,
  517. MfUltralightData* data) {
  518. uint8_t pages_read_cnt = 0;
  519. uint8_t curr_sector_index = 0xff;
  520. reader->pages_read = 0;
  521. for(size_t i = 0; i < reader->pages_to_read; i += pages_read_cnt) {
  522. uint8_t tag_sector;
  523. int16_t valid_pages;
  524. int16_t tag_page = mf_ultralight_ntag_i2c_addr_lin_to_tag(
  525. data, reader, (int16_t)i, &tag_sector, &valid_pages);
  526. furi_assert(tag_page != -1);
  527. if(curr_sector_index != tag_sector) {
  528. if(!mf_ultralight_sector_select(tx_rx, tag_sector)) return false;
  529. curr_sector_index = tag_sector;
  530. }
  531. FURI_LOG_D(
  532. TAG, "Reading pages %zu - %zu", i, i + (valid_pages > 4 ? 4 : valid_pages) - 1U);
  533. tx_rx->tx_data[0] = MF_UL_READ_CMD;
  534. tx_rx->tx_data[1] = tag_page;
  535. tx_rx->tx_bits = 16;
  536. tx_rx->tx_rx_type = FurryHalNfcTxRxTypeDefault;
  537. if(!furry_hal_nfc_tx_rx(tx_rx, 50) || tx_rx->rx_bits < 16 * 8) {
  538. FURI_LOG_D(
  539. TAG,
  540. "Failed to read pages %zu - %zu",
  541. i,
  542. i + (valid_pages > 4 ? 4 : valid_pages) - 1U);
  543. break;
  544. }
  545. if(valid_pages > 4) {
  546. pages_read_cnt = 4;
  547. } else {
  548. pages_read_cnt = valid_pages;
  549. }
  550. reader->pages_read += pages_read_cnt;
  551. memcpy(&data->data[i * 4], tx_rx->rx_data, pages_read_cnt * 4);
  552. }
  553. data->data_size = reader->pages_to_read * 4;
  554. data->data_read = reader->pages_read * 4;
  555. return reader->pages_read > 0;
  556. }
  557. bool mf_ultralight_fast_read_pages(
  558. FurryHalNfcTxRxContext* tx_rx,
  559. MfUltralightReader* reader,
  560. MfUltralightData* data) {
  561. uint8_t curr_sector_index = 0xff;
  562. reader->pages_read = 0;
  563. while(reader->pages_read < reader->pages_to_read) {
  564. uint8_t tag_sector;
  565. int16_t valid_pages;
  566. int16_t tag_page = mf_ultralight_ntag_i2c_addr_lin_to_tag(
  567. data, reader, reader->pages_read, &tag_sector, &valid_pages);
  568. furi_assert(tag_page != -1);
  569. if(curr_sector_index != tag_sector) {
  570. if(!mf_ultralight_sector_select(tx_rx, tag_sector)) return false;
  571. curr_sector_index = tag_sector;
  572. }
  573. FURI_LOG_D(
  574. TAG, "Reading pages %d - %d", reader->pages_read, reader->pages_read + valid_pages - 1);
  575. tx_rx->tx_data[0] = MF_UL_FAST_READ_CMD;
  576. tx_rx->tx_data[1] = tag_page;
  577. tx_rx->tx_data[2] = valid_pages - 1;
  578. tx_rx->tx_bits = 24;
  579. tx_rx->tx_rx_type = FurryHalNfcTxRxTypeDefault;
  580. if(furry_hal_nfc_tx_rx(tx_rx, 50)) {
  581. memcpy(&data->data[reader->pages_read * 4], tx_rx->rx_data, valid_pages * 4);
  582. reader->pages_read += valid_pages;
  583. data->data_size = reader->pages_read * 4;
  584. } else {
  585. FURI_LOG_D(
  586. TAG,
  587. "Failed to read pages %d - %d",
  588. reader->pages_read,
  589. reader->pages_read + valid_pages - 1);
  590. break;
  591. }
  592. }
  593. return reader->pages_read == reader->pages_to_read;
  594. }
  595. bool mf_ultralight_read_signature(FurryHalNfcTxRxContext* tx_rx, MfUltralightData* data) {
  596. bool signature_read = false;
  597. FURI_LOG_D(TAG, "Reading signature");
  598. tx_rx->tx_data[0] = MF_UL_READ_SIG;
  599. tx_rx->tx_data[1] = 0;
  600. tx_rx->tx_bits = 16;
  601. tx_rx->tx_rx_type = FurryHalNfcTxRxTypeDefault;
  602. if(furry_hal_nfc_tx_rx(tx_rx, 50)) {
  603. memcpy(data->signature, tx_rx->rx_data, sizeof(data->signature));
  604. signature_read = true;
  605. } else {
  606. FURI_LOG_D(TAG, "Failed redaing signature");
  607. }
  608. return signature_read;
  609. }
  610. bool mf_ultralight_read_counters(FurryHalNfcTxRxContext* tx_rx, MfUltralightData* data) {
  611. uint8_t counter_read = 0;
  612. FURI_LOG_D(TAG, "Reading counters");
  613. bool is_single_counter = (mf_ul_get_features(data->type) & MfUltralightSupportSingleCounter) !=
  614. 0;
  615. for(size_t i = is_single_counter ? 2 : 0; i < 3; i++) {
  616. tx_rx->tx_data[0] = MF_UL_READ_CNT;
  617. tx_rx->tx_data[1] = i;
  618. tx_rx->tx_bits = 16;
  619. tx_rx->tx_rx_type = FurryHalNfcTxRxTypeDefault;
  620. if(!furry_hal_nfc_tx_rx(tx_rx, 50)) {
  621. FURI_LOG_D(TAG, "Failed to read %d counter", i);
  622. break;
  623. }
  624. data->counter[i] = (tx_rx->rx_data[2] << 16) | (tx_rx->rx_data[1] << 8) |
  625. tx_rx->rx_data[0];
  626. counter_read++;
  627. }
  628. return counter_read == (is_single_counter ? 1 : 3);
  629. }
  630. bool mf_ultralight_read_tearing_flags(FurryHalNfcTxRxContext* tx_rx, MfUltralightData* data) {
  631. uint8_t flag_read = 0;
  632. FURI_LOG_D(TAG, "Reading tearing flags");
  633. for(size_t i = 0; i < 3; i++) {
  634. tx_rx->tx_data[0] = MF_UL_CHECK_TEARING;
  635. tx_rx->tx_data[1] = i;
  636. tx_rx->tx_bits = 16;
  637. tx_rx->tx_rx_type = FurryHalNfcTxRxTypeDefault;
  638. if(!furry_hal_nfc_tx_rx(tx_rx, 50)) {
  639. FURI_LOG_D(TAG, "Failed to read %d tearing flag", i);
  640. break;
  641. }
  642. data->tearing[i] = tx_rx->rx_data[0];
  643. flag_read++;
  644. }
  645. return flag_read == 2;
  646. }
  647. static bool mf_ul_probe_3des_auth(FurryHalNfcTxRxContext* tx_rx) {
  648. tx_rx->tx_data[0] = MF_UL_AUTHENTICATE_1;
  649. tx_rx->tx_data[1] = 0;
  650. tx_rx->tx_bits = 16;
  651. tx_rx->tx_rx_type = FurryHalNfcTxRxTypeDefault;
  652. bool rc = furry_hal_nfc_tx_rx(tx_rx, 50) && tx_rx->rx_bits == 9 * 8 &&
  653. tx_rx->rx_data[0] == 0xAF;
  654. // Reset just in case, we're not going to finish authenticating and need to if tag doesn't support auth
  655. furry_hal_nfc_sleep();
  656. furry_hal_nfc_activate_nfca(300, NULL);
  657. return rc;
  658. }
  659. bool mf_ul_read_card(
  660. FurryHalNfcTxRxContext* tx_rx,
  661. MfUltralightReader* reader,
  662. MfUltralightData* data) {
  663. furi_assert(tx_rx);
  664. furi_assert(reader);
  665. furi_assert(data);
  666. bool card_read = false;
  667. // Read Mifare Ultralight version
  668. if(mf_ultralight_read_version(tx_rx, reader, data)) {
  669. if(reader->supported_features & MfUltralightSupportSignature) {
  670. // Read Signature
  671. mf_ultralight_read_signature(tx_rx, data);
  672. }
  673. } else {
  674. uint8_t dummy[16];
  675. // No GET_VERSION command, check if AUTHENTICATE command available (detect UL C).
  676. if(mf_ul_probe_3des_auth(tx_rx)) {
  677. mf_ul_set_version_ulc(reader, data);
  678. } else if(mf_ultralight_read_pages_direct(tx_rx, 41, dummy)) {
  679. // No AUTHENTICATE, check for NTAG203 by reading last page (41)
  680. mf_ul_set_version_ntag203(reader, data);
  681. } else {
  682. // We're really an original Mifare Ultralight, reset tag for safety
  683. furry_hal_nfc_sleep();
  684. furry_hal_nfc_activate_nfca(300, NULL);
  685. }
  686. reader->supported_features = mf_ul_get_features(data->type);
  687. }
  688. card_read = mf_ultralight_read_pages(tx_rx, reader, data);
  689. if(card_read) {
  690. if(reader->supported_features & MfUltralightSupportReadCounter &&
  691. mf_ultralight_should_read_counters(data)) {
  692. mf_ultralight_read_counters(tx_rx, data);
  693. }
  694. if(reader->supported_features & MfUltralightSupportTearingFlags) {
  695. mf_ultralight_read_tearing_flags(tx_rx, data);
  696. }
  697. data->curr_authlim = 0;
  698. if(reader->pages_read == reader->pages_to_read &&
  699. reader->supported_features & MfUltralightSupportAuth && !data->auth_success) {
  700. MfUltralightConfigPages* config = mf_ultralight_get_config_pages(data);
  701. if(config->access.authlim == 0) {
  702. // Attempt to auth with default PWD
  703. uint16_t pack;
  704. data->auth_success = mf_ultralight_authenticate(tx_rx, MF_UL_DEFAULT_PWD, &pack);
  705. if(data->auth_success) {
  706. config->auth_data.pwd.value = MF_UL_DEFAULT_PWD;
  707. config->auth_data.pack.value = pack;
  708. } else {
  709. furry_hal_nfc_sleep();
  710. furry_hal_nfc_activate_nfca(300, NULL);
  711. }
  712. }
  713. }
  714. }
  715. if(reader->pages_read != reader->pages_to_read) {
  716. if(reader->supported_features & MfUltralightSupportAuth) {
  717. // Probably password protected, fix AUTH0 and PROT so before AUTH0
  718. // can be written and since AUTH0 won't be readable, like on the
  719. // original card
  720. MfUltralightConfigPages* config = mf_ultralight_get_config_pages(data);
  721. config->auth0 = reader->pages_read;
  722. config->access.prot = true;
  723. }
  724. }
  725. return card_read;
  726. }
  727. static void mf_ul_protect_auth_data_on_read_command_i2c(
  728. uint8_t* tx_buff,
  729. uint8_t start_page,
  730. uint8_t end_page,
  731. MfUltralightEmulator* emulator) {
  732. if(emulator->data.type >= MfUltralightTypeNTAGI2CPlus1K) {
  733. // Blank out PWD and PACK
  734. if(start_page <= 229 && end_page >= 229) {
  735. uint16_t offset = (229 - start_page) * 4;
  736. uint8_t count = 4;
  737. if(end_page >= 230) count += 2;
  738. memset(&tx_buff[offset], 0, count);
  739. }
  740. // Handle AUTH0 for sector 0
  741. if(!emulator->auth_success) {
  742. if(emulator->config_cache.access.prot) {
  743. uint8_t auth0 = emulator->config_cache.auth0;
  744. if(auth0 < end_page) {
  745. // start_page is always < auth0; otherwise is NAK'd already
  746. uint8_t page_offset = auth0 - start_page;
  747. uint8_t page_count = end_page - auth0;
  748. memset(&tx_buff[page_offset * 4], 0, page_count * 4);
  749. }
  750. }
  751. }
  752. }
  753. }
  754. static void mf_ul_ntag_i2c_fill_cross_area_read(
  755. uint8_t* tx_buff,
  756. uint8_t start_page,
  757. uint8_t end_page,
  758. MfUltralightEmulator* emulator) {
  759. // For copying config or session registers in fast read
  760. int16_t tx_page_offset;
  761. int16_t data_page_offset;
  762. uint8_t page_length;
  763. bool apply = false;
  764. MfUltralightType type = emulator->data.type;
  765. if(emulator->curr_sector == 0) {
  766. if(type == MfUltralightTypeNTAGI2C1K) {
  767. if(start_page <= 233 && end_page >= 232) {
  768. tx_page_offset = start_page - 232;
  769. data_page_offset = 227;
  770. page_length = 2;
  771. apply = true;
  772. }
  773. } else if(type == MfUltralightTypeNTAGI2CPlus1K || type == MfUltralightTypeNTAGI2CPlus2K) {
  774. if(start_page <= 237 && end_page >= 236) {
  775. tx_page_offset = start_page - 236;
  776. data_page_offset = 234;
  777. page_length = 2;
  778. apply = true;
  779. }
  780. }
  781. } else if(emulator->curr_sector == 1) {
  782. if(type == MfUltralightTypeNTAGI2C2K) {
  783. if(start_page <= 233 && end_page >= 232) {
  784. tx_page_offset = start_page - 232;
  785. data_page_offset = 483;
  786. page_length = 2;
  787. apply = true;
  788. }
  789. }
  790. }
  791. if(apply) {
  792. while(tx_page_offset < 0 && page_length > 0) { //-V614
  793. ++tx_page_offset;
  794. ++data_page_offset;
  795. --page_length;
  796. }
  797. memcpy(
  798. &tx_buff[tx_page_offset * 4],
  799. &emulator->data.data[data_page_offset * 4],
  800. page_length * 4);
  801. }
  802. }
  803. static bool mf_ul_check_auth(MfUltralightEmulator* emulator, uint8_t start_page, bool is_write) {
  804. if(!emulator->auth_success) {
  805. if(start_page >= emulator->config_cache.auth0 &&
  806. (emulator->config_cache.access.prot || is_write))
  807. return false;
  808. }
  809. if(is_write && emulator->config_cache.access.cfglck) {
  810. uint16_t config_start_page = emulator->page_num - 4;
  811. if(start_page == config_start_page || start_page == config_start_page + 1) return false;
  812. }
  813. return true;
  814. }
  815. static bool mf_ul_ntag_i2c_plus_check_auth(
  816. MfUltralightEmulator* emulator,
  817. uint8_t start_page,
  818. bool is_write) {
  819. if(!emulator->auth_success) {
  820. // Check NFC_PROT
  821. if(emulator->curr_sector == 0 && (emulator->config_cache.access.prot || is_write)) {
  822. if(start_page >= emulator->config_cache.auth0) return false;
  823. } else if(emulator->curr_sector == 1) {
  824. // We don't have to specifically check for type because this is done
  825. // by address translator
  826. uint8_t pt_i2c = emulator->data.data[231 * 4];
  827. // Check 2K_PROT
  828. if(pt_i2c & 0x08) return false;
  829. }
  830. }
  831. if(emulator->curr_sector == 1) {
  832. // Check NFC_DIS_SEC1
  833. if(emulator->config_cache.access.nfc_dis_sec1) return false;
  834. }
  835. return true;
  836. }
  837. static int16_t mf_ul_get_dynamic_lock_page_addr(MfUltralightData* data) {
  838. switch(data->type) {
  839. case MfUltralightTypeNTAG203:
  840. return 0x28;
  841. case MfUltralightTypeUL21:
  842. case MfUltralightTypeNTAG213:
  843. case MfUltralightTypeNTAG215:
  844. case MfUltralightTypeNTAG216:
  845. return data->data_size / 4 - 5;
  846. case MfUltralightTypeNTAGI2C1K:
  847. case MfUltralightTypeNTAGI2CPlus1K:
  848. case MfUltralightTypeNTAGI2CPlus2K:
  849. return 0xe2;
  850. case MfUltralightTypeNTAGI2C2K:
  851. return 0x1e0;
  852. default:
  853. return -1; // No dynamic lock bytes
  854. }
  855. }
  856. // Returns true if page not locked
  857. // write_page is tag address
  858. static bool mf_ul_check_lock(MfUltralightEmulator* emulator, int16_t write_page) {
  859. if(write_page < 2) return false; // Page 0-1 is always locked
  860. if(write_page == 2) return true; // Page 2 does not have a lock flag
  861. // Check static lock bytes
  862. if(write_page <= 15) {
  863. uint16_t static_lock_bytes = emulator->data.data[10] | (emulator->data.data[11] << 8);
  864. return (static_lock_bytes & (1 << write_page)) == 0;
  865. }
  866. // Check dynamic lock bytes
  867. // Check max page
  868. switch(emulator->data.type) {
  869. case MfUltralightTypeNTAG203:
  870. // Counter page can be locked and is after dynamic locks
  871. if(write_page == 40) return true;
  872. break;
  873. case MfUltralightTypeUL21:
  874. case MfUltralightTypeNTAG213:
  875. case MfUltralightTypeNTAG215:
  876. case MfUltralightTypeNTAG216:
  877. if(write_page >= emulator->page_num - 5) return true;
  878. break;
  879. case MfUltralightTypeNTAGI2C1K:
  880. case MfUltralightTypeNTAGI2CPlus1K:
  881. if(write_page > 225) return true;
  882. break;
  883. case MfUltralightTypeNTAGI2C2K:
  884. if(write_page > 479) return true;
  885. break;
  886. case MfUltralightTypeNTAGI2CPlus2K:
  887. if(write_page >= 226 && write_page <= 255) return true;
  888. if(write_page >= 512) return true;
  889. break;
  890. default:
  891. furi_crash("Unknown MFUL");
  892. return true;
  893. }
  894. int16_t dynamic_lock_index = mf_ul_get_dynamic_lock_page_addr(&emulator->data);
  895. if(dynamic_lock_index == -1) return true;
  896. // Run address through converter because NTAG I2C 2K is special
  897. uint16_t valid_pages; // unused
  898. dynamic_lock_index =
  899. mf_ultralight_ntag_i2c_addr_tag_to_lin(
  900. &emulator->data, dynamic_lock_index & 0xff, dynamic_lock_index >> 8, &valid_pages) *
  901. 4;
  902. uint16_t dynamic_lock_bytes = emulator->data.data[dynamic_lock_index] |
  903. (emulator->data.data[dynamic_lock_index + 1] << 8);
  904. uint8_t shift;
  905. switch(emulator->data.type) {
  906. // low byte LSB range, MSB range
  907. case MfUltralightTypeNTAG203:
  908. if(write_page >= 16 && write_page <= 27) //-V560
  909. shift = (write_page - 16) / 4 + 1;
  910. else if(write_page >= 28 && write_page <= 39) //-V560
  911. shift = (write_page - 28) / 4 + 5;
  912. else if(write_page == 41)
  913. shift = 12;
  914. else {
  915. furi_crash("Unknown MFUL");
  916. }
  917. break;
  918. case MfUltralightTypeUL21:
  919. case MfUltralightTypeNTAG213:
  920. // 16-17, 30-31
  921. shift = (write_page - 16) / 2;
  922. break;
  923. case MfUltralightTypeNTAG215:
  924. case MfUltralightTypeNTAG216:
  925. case MfUltralightTypeNTAGI2C1K:
  926. case MfUltralightTypeNTAGI2CPlus1K:
  927. // 16-31, 128-129
  928. // 16-31, 128-143
  929. shift = (write_page - 16) / 16;
  930. break;
  931. case MfUltralightTypeNTAGI2C2K:
  932. // 16-47, 240-271
  933. shift = (write_page - 16) / 32;
  934. break;
  935. case MfUltralightTypeNTAGI2CPlus2K:
  936. // 16-47, 256-271
  937. if(write_page >= 208 && write_page <= 225)
  938. shift = 6;
  939. else if(write_page >= 256 && write_page <= 271)
  940. shift = 7;
  941. else
  942. shift = (write_page - 16) / 32;
  943. break;
  944. default:
  945. furi_crash("Unknown MFUL");
  946. break;
  947. }
  948. return (dynamic_lock_bytes & (1 << shift)) == 0;
  949. }
  950. static void mf_ul_make_ascii_mirror(MfUltralightEmulator* emulator, FuriString* str) {
  951. // Locals to improve readability
  952. uint8_t mirror_page = emulator->config->mirror_page;
  953. uint8_t mirror_byte = emulator->config->mirror.mirror_byte;
  954. MfUltralightMirrorConf mirror_conf = emulator->config_cache.mirror.mirror_conf;
  955. uint16_t last_user_page_index = emulator->page_num - 6;
  956. bool uid_printed = false;
  957. if(mirror_conf == MfUltralightMirrorUid || mirror_conf == MfUltralightMirrorUidCounter) {
  958. // UID range check
  959. if(mirror_page < 4 || mirror_page > last_user_page_index - 3 ||
  960. (mirror_page == last_user_page_index - 3 && mirror_byte > 2)) {
  961. if(mirror_conf == MfUltralightMirrorUid) return;
  962. // NTAG21x has the peculiar behavior when UID+counter selected, if UID does not fit but
  963. // counter will fit, it will actually mirror the counter
  964. furi_string_cat(str, " ");
  965. } else {
  966. for(int i = 0; i < 3; ++i) {
  967. furi_string_cat_printf(str, "%02X", emulator->data.data[i]);
  968. }
  969. // Skip BCC0
  970. for(int i = 4; i < 8; ++i) {
  971. furi_string_cat_printf(str, "%02X", emulator->data.data[i]);
  972. }
  973. uid_printed = true;
  974. }
  975. uint16_t next_byte_offset = mirror_page * 4 + mirror_byte + 14;
  976. if(mirror_conf == MfUltralightMirrorUidCounter) ++next_byte_offset;
  977. mirror_page = next_byte_offset / 4;
  978. mirror_byte = next_byte_offset % 4;
  979. }
  980. if(mirror_conf == MfUltralightMirrorCounter || mirror_conf == MfUltralightMirrorUidCounter) {
  981. // Counter is only printed if counter enabled
  982. if(emulator->config_cache.access.nfc_cnt_en) {
  983. // Counter protection check
  984. if(emulator->config_cache.access.nfc_cnt_pwd_prot && !emulator->auth_success) return;
  985. // Counter range check
  986. if(mirror_page < 4) return;
  987. if(mirror_page > last_user_page_index - 1) return;
  988. if(mirror_page == last_user_page_index - 1 && mirror_byte > 2) return;
  989. if(mirror_conf == MfUltralightMirrorUidCounter)
  990. furi_string_cat(str, uid_printed ? "x" : " ");
  991. furi_string_cat_printf(str, "%06lX", emulator->data.counter[2]);
  992. }
  993. }
  994. }
  995. static void mf_ul_increment_single_counter(MfUltralightEmulator* emulator) {
  996. if(!emulator->read_counter_incremented && emulator->config_cache.access.nfc_cnt_en) {
  997. if(emulator->data.counter[2] < 0xFFFFFF) {
  998. ++emulator->data.counter[2];
  999. emulator->data_changed = true;
  1000. }
  1001. emulator->read_counter_incremented = true;
  1002. }
  1003. }
  1004. static bool
  1005. mf_ul_emulate_ntag203_counter_write(MfUltralightEmulator* emulator, uint8_t* page_buff) {
  1006. // We'll reuse the existing counters for other NTAGs as staging
  1007. // Counter 0 stores original value, data is new value
  1008. uint32_t counter_value;
  1009. if(emulator->data.tearing[0] == MF_UL_TEARING_FLAG_DEFAULT) {
  1010. counter_value = emulator->data.data[MF_UL_NTAG203_COUNTER_PAGE * 4] |
  1011. (emulator->data.data[MF_UL_NTAG203_COUNTER_PAGE * 4 + 1] << 8);
  1012. } else {
  1013. // We've had a reset here, so load from original value
  1014. counter_value = emulator->data.counter[0];
  1015. }
  1016. // Although the datasheet says increment by 0 is always possible, this is not the case on
  1017. // an actual tag. If the counter is at 0xFFFF, any writes are locked out.
  1018. if(counter_value == 0xFFFF) return false;
  1019. uint32_t increment = page_buff[0] | (page_buff[1] << 8);
  1020. if(counter_value == 0) {
  1021. counter_value = increment;
  1022. } else {
  1023. // Per datasheet specifying > 0x000F is supposed to NAK, but actual tag doesn't
  1024. increment &= 0x000F;
  1025. if(counter_value + increment > 0xFFFF) return false;
  1026. counter_value += increment;
  1027. }
  1028. // Commit to new value counter
  1029. emulator->data.data[MF_UL_NTAG203_COUNTER_PAGE * 4] = (uint8_t)counter_value;
  1030. emulator->data.data[MF_UL_NTAG203_COUNTER_PAGE * 4 + 1] = (uint8_t)(counter_value >> 8);
  1031. emulator->data.tearing[0] = MF_UL_TEARING_FLAG_DEFAULT;
  1032. if(counter_value == 0xFFFF) {
  1033. // Tag will lock out counter if final number is 0xFFFF, even if you try to roll it back
  1034. emulator->data.counter[1] = 0xFFFF;
  1035. }
  1036. emulator->data_changed = true;
  1037. return true;
  1038. }
  1039. static void mf_ul_emulate_write(
  1040. MfUltralightEmulator* emulator,
  1041. int16_t tag_addr,
  1042. int16_t write_page,
  1043. uint8_t* page_buff) {
  1044. // Assumption: all access checks have been completed
  1045. if(tag_addr == 2) {
  1046. // Handle static locks
  1047. uint16_t orig_static_locks = emulator->data.data[write_page * 4 + 2] |
  1048. (emulator->data.data[write_page * 4 + 3] << 8);
  1049. uint16_t new_static_locks = page_buff[2] | (page_buff[3] << 8);
  1050. if(orig_static_locks & 1) new_static_locks &= ~0x08;
  1051. if(orig_static_locks & 2) new_static_locks &= ~0xF0;
  1052. if(orig_static_locks & 4) new_static_locks &= 0xFF;
  1053. new_static_locks |= orig_static_locks;
  1054. page_buff[0] = emulator->data.data[write_page * 4];
  1055. page_buff[1] = emulator->data.data[write_page * 4 + 1];
  1056. page_buff[2] = new_static_locks & 0xff;
  1057. page_buff[3] = new_static_locks >> 8;
  1058. } else if(tag_addr == 3) {
  1059. // Handle OTP/capability container
  1060. *(uint32_t*)page_buff |= *(uint32_t*)&emulator->data.data[write_page * 4];
  1061. } else if(tag_addr == mf_ul_get_dynamic_lock_page_addr(&emulator->data)) {
  1062. // Handle dynamic locks
  1063. if(emulator->data.type == MfUltralightTypeNTAG203) {
  1064. // NTAG203 lock bytes are a bit different from the others
  1065. uint8_t orig_page_lock_byte = emulator->data.data[write_page * 4];
  1066. uint8_t orig_cnt_lock_byte = emulator->data.data[write_page * 4 + 1];
  1067. uint8_t new_page_lock_byte = page_buff[0];
  1068. uint8_t new_cnt_lock_byte = page_buff[1];
  1069. if(orig_page_lock_byte & 0x01) // Block lock bits 1-3
  1070. new_page_lock_byte &= ~0x0E;
  1071. if(orig_page_lock_byte & 0x10) // Block lock bits 5-7
  1072. new_page_lock_byte &= ~0xE0;
  1073. for(uint8_t i = 0; i < 4; ++i) {
  1074. if(orig_cnt_lock_byte & (1 << i)) // Block lock counter bit
  1075. new_cnt_lock_byte &= ~(1 << (4 + i));
  1076. }
  1077. new_page_lock_byte |= orig_page_lock_byte;
  1078. new_cnt_lock_byte |= orig_cnt_lock_byte;
  1079. page_buff[0] = new_page_lock_byte;
  1080. page_buff[1] = new_cnt_lock_byte;
  1081. } else {
  1082. uint16_t orig_locks = emulator->data.data[write_page * 4] |
  1083. (emulator->data.data[write_page * 4 + 1] << 8);
  1084. uint8_t orig_block_locks = emulator->data.data[write_page * 4 + 2];
  1085. uint16_t new_locks = page_buff[0] | (page_buff[1] << 8);
  1086. uint8_t new_block_locks = page_buff[2];
  1087. int block_lock_count;
  1088. switch(emulator->data.type) {
  1089. case MfUltralightTypeUL21:
  1090. block_lock_count = 5;
  1091. break;
  1092. case MfUltralightTypeNTAG213:
  1093. block_lock_count = 6;
  1094. break;
  1095. case MfUltralightTypeNTAG215:
  1096. block_lock_count = 4;
  1097. break;
  1098. case MfUltralightTypeNTAG216:
  1099. case MfUltralightTypeNTAGI2C1K:
  1100. case MfUltralightTypeNTAGI2CPlus1K:
  1101. block_lock_count = 7;
  1102. break;
  1103. case MfUltralightTypeNTAGI2C2K:
  1104. case MfUltralightTypeNTAGI2CPlus2K:
  1105. block_lock_count = 8;
  1106. break;
  1107. default:
  1108. furi_crash("Unknown MFUL");
  1109. break;
  1110. }
  1111. for(int i = 0; i < block_lock_count; ++i) {
  1112. if(orig_block_locks & (1 << i)) new_locks &= ~(3 << (2 * i));
  1113. }
  1114. new_locks |= orig_locks;
  1115. new_block_locks |= orig_block_locks;
  1116. page_buff[0] = new_locks & 0xff;
  1117. page_buff[1] = new_locks >> 8;
  1118. page_buff[2] = new_block_locks;
  1119. if(emulator->data.type >= MfUltralightTypeUL21 && //-V1016
  1120. emulator->data.type <= MfUltralightTypeNTAG216)
  1121. page_buff[3] = MF_UL_TEARING_FLAG_DEFAULT;
  1122. else
  1123. page_buff[3] = 0;
  1124. }
  1125. }
  1126. memcpy(&emulator->data.data[write_page * 4], page_buff, 4);
  1127. emulator->data_changed = true;
  1128. }
  1129. bool mf_ul_emulation_supported(MfUltralightData* data) {
  1130. return data->type != MfUltralightTypeULC;
  1131. }
  1132. void mf_ul_reset_emulation(MfUltralightEmulator* emulator, bool is_power_cycle) {
  1133. emulator->comp_write_cmd_started = false;
  1134. emulator->sector_select_cmd_started = false;
  1135. emulator->curr_sector = 0;
  1136. emulator->ntag_i2c_plus_sector3_lockout = false;
  1137. emulator->auth_success = false;
  1138. if(is_power_cycle) {
  1139. if(emulator->config != NULL) emulator->config_cache = *emulator->config;
  1140. if(emulator->supported_features & MfUltralightSupportSingleCounter) {
  1141. emulator->read_counter_incremented = false;
  1142. }
  1143. if(emulator->data.type == MfUltralightTypeNTAG203) {
  1144. // Apply lockout if counter ever reached 0xFFFF
  1145. if(emulator->data.counter[1] == 0xFFFF) {
  1146. emulator->data.data[MF_UL_NTAG203_COUNTER_PAGE * 4] = 0xFF;
  1147. emulator->data.data[MF_UL_NTAG203_COUNTER_PAGE * 4 + 1] = 0xFF;
  1148. }
  1149. // Copy original counter value from data
  1150. emulator->data.counter[0] =
  1151. emulator->data.data[MF_UL_NTAG203_COUNTER_PAGE * 4] |
  1152. (emulator->data.data[MF_UL_NTAG203_COUNTER_PAGE * 4 + 1] << 8);
  1153. }
  1154. } else {
  1155. if(emulator->config != NULL) {
  1156. // ACCESS (less CFGLCK) and AUTH0 are updated when reactivated
  1157. // MIRROR_CONF is not; don't know about STRG_MOD_EN, but we're not using that anyway
  1158. emulator->config_cache.access.value = (emulator->config->access.value & 0xBF) |
  1159. (emulator->config_cache.access.value & 0x40);
  1160. emulator->config_cache.auth0 = emulator->config->auth0;
  1161. }
  1162. }
  1163. if(emulator->data.type == MfUltralightTypeNTAG203) {
  1164. // Mark counter as dirty
  1165. emulator->data.tearing[0] = 0;
  1166. }
  1167. }
  1168. void mf_ul_prepare_emulation(MfUltralightEmulator* emulator, MfUltralightData* data) {
  1169. FURI_LOG_D(TAG, "Prepare emulation");
  1170. emulator->data = *data;
  1171. emulator->supported_features = mf_ul_get_features(data->type);
  1172. emulator->config = mf_ultralight_get_config_pages(&emulator->data);
  1173. emulator->page_num = emulator->data.data_size / 4;
  1174. emulator->data_changed = false;
  1175. memset(&emulator->auth_attempt, 0, sizeof(MfUltralightAuth));
  1176. mf_ul_reset_emulation(emulator, true);
  1177. }
  1178. bool mf_ul_prepare_emulation_response(
  1179. uint8_t* buff_rx,
  1180. uint16_t buff_rx_len,
  1181. uint8_t* buff_tx,
  1182. uint16_t* buff_tx_len,
  1183. uint32_t* data_type,
  1184. void* context) {
  1185. furi_assert(context);
  1186. MfUltralightEmulator* emulator = context;
  1187. uint16_t tx_bytes = 0;
  1188. uint16_t tx_bits = 0;
  1189. bool command_parsed = false;
  1190. bool send_ack = false;
  1191. bool respond_nothing = false;
  1192. bool reset_idle = false;
  1193. #ifdef FURI_DEBUG
  1194. FuriString* debug_buf;
  1195. debug_buf = furi_string_alloc();
  1196. for(int i = 0; i < (buff_rx_len + 7) / 8; ++i) {
  1197. furi_string_cat_printf(debug_buf, "%02x ", buff_rx[i]);
  1198. }
  1199. furi_string_trim(debug_buf);
  1200. FURI_LOG_T(TAG, "Emu RX (%d): %s", buff_rx_len, furi_string_get_cstr(debug_buf));
  1201. furi_string_reset(debug_buf);
  1202. #endif
  1203. // Check composite commands
  1204. if(emulator->comp_write_cmd_started) {
  1205. if(buff_rx_len == 16 * 8) {
  1206. if(emulator->data.type == MfUltralightTypeNTAG203 &&
  1207. emulator->comp_write_page_addr == MF_UL_NTAG203_COUNTER_PAGE) {
  1208. send_ack = mf_ul_emulate_ntag203_counter_write(emulator, buff_rx);
  1209. command_parsed = send_ack;
  1210. } else {
  1211. mf_ul_emulate_write(
  1212. emulator,
  1213. emulator->comp_write_page_addr,
  1214. emulator->comp_write_page_addr,
  1215. buff_rx);
  1216. send_ack = true;
  1217. command_parsed = true;
  1218. }
  1219. }
  1220. emulator->comp_write_cmd_started = false;
  1221. } else if(emulator->sector_select_cmd_started) {
  1222. if(buff_rx_len == 4 * 8) {
  1223. if(buff_rx[0] <= 0xFE) {
  1224. emulator->curr_sector = buff_rx[0] > 3 ? 0 : buff_rx[0];
  1225. emulator->ntag_i2c_plus_sector3_lockout = false;
  1226. command_parsed = true;
  1227. respond_nothing = true;
  1228. FURI_LOG_D(TAG, "Changing sector to %d", emulator->curr_sector);
  1229. }
  1230. }
  1231. emulator->sector_select_cmd_started = false;
  1232. } else if(buff_rx_len >= 8) {
  1233. uint8_t cmd = buff_rx[0];
  1234. if(cmd == MF_UL_GET_VERSION_CMD) {
  1235. if(emulator->data.type >= MfUltralightTypeUL11) {
  1236. if(buff_rx_len == 1 * 8) {
  1237. tx_bytes = sizeof(emulator->data.version);
  1238. memcpy(buff_tx, &emulator->data.version, tx_bytes);
  1239. *data_type = FURRY_HAL_NFC_TXRX_DEFAULT;
  1240. command_parsed = true;
  1241. }
  1242. }
  1243. } else if(cmd == MF_UL_READ_CMD) {
  1244. if(buff_rx_len == (1 + 1) * 8) {
  1245. int16_t start_page = buff_rx[1];
  1246. tx_bytes = 16;
  1247. if(emulator->data.type < MfUltralightTypeNTAGI2C1K) {
  1248. if(start_page < emulator->page_num) {
  1249. do {
  1250. uint8_t copied_pages = 0;
  1251. uint8_t src_page = start_page;
  1252. uint8_t last_page_plus_one = start_page + 4;
  1253. uint8_t pwd_page = emulator->page_num - 2;
  1254. FuriString* ascii_mirror = NULL;
  1255. size_t ascii_mirror_len = 0;
  1256. const char* ascii_mirror_cptr = NULL;
  1257. uint8_t ascii_mirror_curr_page = 0;
  1258. uint8_t ascii_mirror_curr_byte = 0;
  1259. if(last_page_plus_one > emulator->page_num)
  1260. last_page_plus_one = emulator->page_num;
  1261. if(emulator->supported_features & MfUltralightSupportAuth) {
  1262. if(!mf_ul_check_auth(emulator, start_page, false)) break;
  1263. if(!emulator->auth_success && emulator->config_cache.access.prot &&
  1264. emulator->config_cache.auth0 < last_page_plus_one)
  1265. last_page_plus_one = emulator->config_cache.auth0;
  1266. }
  1267. if(emulator->supported_features & MfUltralightSupportSingleCounter)
  1268. mf_ul_increment_single_counter(emulator);
  1269. if(emulator->supported_features & MfUltralightSupportAsciiMirror &&
  1270. emulator->config_cache.mirror.mirror_conf !=
  1271. MfUltralightMirrorNone) {
  1272. ascii_mirror_curr_byte = emulator->config->mirror.mirror_byte;
  1273. ascii_mirror_curr_page = emulator->config->mirror_page;
  1274. // Try to avoid wasting time making mirror if we won't copy it
  1275. // Conservatively check with UID+counter mirror size
  1276. if(last_page_plus_one > ascii_mirror_curr_page &&
  1277. start_page + 3 >= ascii_mirror_curr_page &&
  1278. start_page <= ascii_mirror_curr_page + 6) {
  1279. ascii_mirror = furi_string_alloc();
  1280. mf_ul_make_ascii_mirror(emulator, ascii_mirror);
  1281. ascii_mirror_len = furi_string_utf8_length(ascii_mirror);
  1282. ascii_mirror_cptr = furi_string_get_cstr(ascii_mirror);
  1283. // Move pointer to where it should be to start copying
  1284. if(ascii_mirror_len > 0 &&
  1285. ascii_mirror_curr_page < start_page &&
  1286. ascii_mirror_curr_byte != 0) {
  1287. uint8_t diff = 4 - ascii_mirror_curr_byte;
  1288. ascii_mirror_len -= diff;
  1289. ascii_mirror_cptr += diff;
  1290. ascii_mirror_curr_byte = 0;
  1291. ++ascii_mirror_curr_page;
  1292. }
  1293. while(ascii_mirror_len > 0 &&
  1294. ascii_mirror_curr_page < start_page) {
  1295. uint8_t diff = ascii_mirror_len > 4 ? 4 : ascii_mirror_len;
  1296. ascii_mirror_len -= diff;
  1297. ascii_mirror_cptr += diff;
  1298. ++ascii_mirror_curr_page;
  1299. }
  1300. }
  1301. }
  1302. uint8_t* dest_ptr = buff_tx;
  1303. while(copied_pages < 4) {
  1304. // Copy page
  1305. memcpy(dest_ptr, &emulator->data.data[src_page * 4], 4);
  1306. // Note: don't have to worry about roll-over with ASCII mirror because
  1307. // lowest valid page for it is 4, while roll-over will at best read
  1308. // pages 0-2
  1309. if(ascii_mirror_len > 0 && src_page == ascii_mirror_curr_page) {
  1310. // Copy ASCII mirror
  1311. size_t copy_len = 4 - ascii_mirror_curr_byte;
  1312. if(copy_len > ascii_mirror_len) copy_len = ascii_mirror_len;
  1313. for(size_t i = 0; i < copy_len; ++i) {
  1314. if(*ascii_mirror_cptr != ' ')
  1315. dest_ptr[ascii_mirror_curr_byte] =
  1316. (uint8_t)*ascii_mirror_cptr;
  1317. ++ascii_mirror_curr_byte;
  1318. ++ascii_mirror_cptr;
  1319. }
  1320. ascii_mirror_len -= copy_len;
  1321. // Don't care if this is inaccurate after ascii_mirror_len = 0
  1322. ascii_mirror_curr_byte = 0;
  1323. ++ascii_mirror_curr_page;
  1324. }
  1325. if(emulator->supported_features & MfUltralightSupportAuth) {
  1326. if(src_page == pwd_page || src_page == pwd_page + 1) {
  1327. // Blank out PWD and PACK pages
  1328. memset(dest_ptr, 0, 4);
  1329. }
  1330. }
  1331. dest_ptr += 4;
  1332. ++copied_pages;
  1333. ++src_page;
  1334. if(src_page >= last_page_plus_one) src_page = 0;
  1335. }
  1336. if(ascii_mirror != NULL) {
  1337. furi_string_free(ascii_mirror);
  1338. }
  1339. *data_type = FURRY_HAL_NFC_TXRX_DEFAULT;
  1340. command_parsed = true;
  1341. } while(false);
  1342. }
  1343. } else {
  1344. uint16_t valid_pages;
  1345. start_page = mf_ultralight_ntag_i2c_addr_tag_to_lin(
  1346. &emulator->data, start_page, emulator->curr_sector, &valid_pages);
  1347. if(start_page != -1) {
  1348. if(emulator->data.type < MfUltralightTypeNTAGI2CPlus1K ||
  1349. mf_ul_ntag_i2c_plus_check_auth(emulator, buff_rx[1], false)) {
  1350. if(emulator->data.type >= MfUltralightTypeNTAGI2CPlus1K &&
  1351. emulator->curr_sector == 3 && valid_pages == 1) {
  1352. // Rewind back a sector to match behavior on a real tag
  1353. --start_page;
  1354. ++valid_pages;
  1355. }
  1356. uint16_t copy_count = (valid_pages > 4 ? 4 : valid_pages) * 4;
  1357. FURI_LOG_D(
  1358. TAG,
  1359. "NTAG I2C Emu: page valid, %02x:%02x -> %d, %d",
  1360. emulator->curr_sector,
  1361. buff_rx[1],
  1362. start_page,
  1363. valid_pages);
  1364. memcpy(buff_tx, &emulator->data.data[start_page * 4], copy_count);
  1365. // For NTAG I2C, there's no roll-over; remainder is filled by null bytes
  1366. if(copy_count < tx_bytes)
  1367. memset(&buff_tx[copy_count], 0, tx_bytes - copy_count);
  1368. // Special case: NTAG I2C Plus sector 0 page 233 read crosses into page 236
  1369. if(start_page == 233)
  1370. memcpy(
  1371. &buff_tx[12], &emulator->data.data[(start_page + 1) * 4], 4);
  1372. mf_ul_protect_auth_data_on_read_command_i2c(
  1373. buff_tx, start_page, start_page + copy_count / 4 - 1, emulator);
  1374. *data_type = FURRY_HAL_NFC_TXRX_DEFAULT;
  1375. command_parsed = true;
  1376. }
  1377. } else {
  1378. FURI_LOG_D(
  1379. TAG,
  1380. "NTAG I2C Emu: page invalid, %02x:%02x",
  1381. emulator->curr_sector,
  1382. buff_rx[1]);
  1383. if(emulator->data.type >= MfUltralightTypeNTAGI2CPlus1K &&
  1384. emulator->curr_sector == 3 &&
  1385. !emulator->ntag_i2c_plus_sector3_lockout) {
  1386. // NTAG I2C Plus has a weird behavior where if you read sector 3
  1387. // at an invalid address, it responds with zeroes then locks
  1388. // the read out, while if you read the mirrored session registers,
  1389. // it returns both session registers on either pages
  1390. memset(buff_tx, 0, tx_bytes);
  1391. *data_type = FURRY_HAL_NFC_TXRX_DEFAULT;
  1392. command_parsed = true;
  1393. emulator->ntag_i2c_plus_sector3_lockout = true;
  1394. }
  1395. }
  1396. }
  1397. if(!command_parsed) tx_bytes = 0;
  1398. }
  1399. } else if(cmd == MF_UL_FAST_READ_CMD) {
  1400. if(emulator->supported_features & MfUltralightSupportFastRead) {
  1401. if(buff_rx_len == (1 + 2) * 8) {
  1402. int16_t start_page = buff_rx[1];
  1403. uint8_t end_page = buff_rx[2];
  1404. if(start_page <= end_page) {
  1405. tx_bytes = ((end_page + 1) - start_page) * 4;
  1406. if(emulator->data.type < MfUltralightTypeNTAGI2C1K) {
  1407. if((start_page < emulator->page_num) &&
  1408. (end_page < emulator->page_num)) {
  1409. do {
  1410. if(emulator->supported_features & MfUltralightSupportAuth) {
  1411. // NAK if not authenticated and requested pages cross over AUTH0
  1412. if(!emulator->auth_success &&
  1413. emulator->config_cache.access.prot &&
  1414. (start_page >= emulator->config_cache.auth0 ||
  1415. end_page >= emulator->config_cache.auth0))
  1416. break;
  1417. }
  1418. if(emulator->supported_features &
  1419. MfUltralightSupportSingleCounter)
  1420. mf_ul_increment_single_counter(emulator);
  1421. // Copy requested pages
  1422. memcpy(
  1423. buff_tx, &emulator->data.data[start_page * 4], tx_bytes);
  1424. if(emulator->supported_features &
  1425. MfUltralightSupportAsciiMirror &&
  1426. emulator->config_cache.mirror.mirror_conf !=
  1427. MfUltralightMirrorNone) {
  1428. // Copy ASCII mirror
  1429. // Less stringent check here, because expecting FAST_READ to
  1430. // only be issued once rather than repeatedly
  1431. FuriString* ascii_mirror;
  1432. ascii_mirror = furi_string_alloc();
  1433. mf_ul_make_ascii_mirror(emulator, ascii_mirror);
  1434. size_t ascii_mirror_len =
  1435. furi_string_utf8_length(ascii_mirror);
  1436. const char* ascii_mirror_cptr =
  1437. furi_string_get_cstr(ascii_mirror);
  1438. int16_t mirror_start_offset =
  1439. (emulator->config->mirror_page - start_page) * 4 +
  1440. emulator->config->mirror.mirror_byte;
  1441. if(mirror_start_offset < 0) {
  1442. if(mirror_start_offset < -(int16_t)ascii_mirror_len) {
  1443. // Past ASCII mirror, don't copy
  1444. ascii_mirror_len = 0;
  1445. } else {
  1446. ascii_mirror_cptr += -mirror_start_offset;
  1447. ascii_mirror_len -= -mirror_start_offset;
  1448. mirror_start_offset = 0;
  1449. }
  1450. }
  1451. if(ascii_mirror_len > 0) {
  1452. int16_t mirror_end_offset =
  1453. mirror_start_offset + ascii_mirror_len;
  1454. if(mirror_end_offset > (end_page + 1) * 4) {
  1455. mirror_end_offset = (end_page + 1) * 4;
  1456. ascii_mirror_len =
  1457. mirror_end_offset - mirror_start_offset;
  1458. }
  1459. for(size_t i = 0; i < ascii_mirror_len; ++i) {
  1460. if(*ascii_mirror_cptr != ' ')
  1461. buff_tx[mirror_start_offset] =
  1462. (uint8_t)*ascii_mirror_cptr;
  1463. ++mirror_start_offset;
  1464. ++ascii_mirror_cptr;
  1465. }
  1466. }
  1467. furi_string_free(ascii_mirror);
  1468. }
  1469. if(emulator->supported_features & MfUltralightSupportAuth) {
  1470. // Clear PWD and PACK pages
  1471. uint8_t pwd_page = emulator->page_num - 2;
  1472. int16_t pwd_page_offset = pwd_page - start_page;
  1473. // PWD page
  1474. if(pwd_page_offset >= 0 && pwd_page <= end_page) {
  1475. memset(&buff_tx[pwd_page_offset * 4], 0, 4);
  1476. // PACK page
  1477. if(pwd_page + 1 <= end_page)
  1478. memset(&buff_tx[(pwd_page_offset + 1) * 4], 0, 4);
  1479. }
  1480. }
  1481. *data_type = FURRY_HAL_NFC_TXRX_DEFAULT;
  1482. command_parsed = true;
  1483. } while(false);
  1484. }
  1485. } else {
  1486. uint16_t valid_pages;
  1487. start_page = mf_ultralight_ntag_i2c_addr_tag_to_lin(
  1488. &emulator->data, start_page, emulator->curr_sector, &valid_pages);
  1489. if(start_page != -1) {
  1490. if(emulator->data.type < MfUltralightTypeNTAGI2CPlus1K ||
  1491. mf_ul_ntag_i2c_plus_check_auth(emulator, buff_rx[1], false)) {
  1492. uint16_t copy_count = tx_bytes;
  1493. if(copy_count > valid_pages * 4) copy_count = valid_pages * 4;
  1494. memcpy(
  1495. buff_tx, &emulator->data.data[start_page * 4], copy_count);
  1496. if(copy_count < tx_bytes)
  1497. memset(&buff_tx[copy_count], 0, tx_bytes - copy_count);
  1498. mf_ul_ntag_i2c_fill_cross_area_read(
  1499. buff_tx, buff_rx[1], buff_rx[2], emulator);
  1500. mf_ul_protect_auth_data_on_read_command_i2c(
  1501. buff_tx,
  1502. start_page,
  1503. start_page + copy_count / 4 - 1,
  1504. emulator);
  1505. *data_type = FURRY_HAL_NFC_TXRX_DEFAULT;
  1506. command_parsed = true;
  1507. }
  1508. }
  1509. }
  1510. if(!command_parsed) tx_bytes = 0;
  1511. }
  1512. }
  1513. }
  1514. } else if(cmd == MF_UL_WRITE) {
  1515. if(buff_rx_len == (1 + 5) * 8) {
  1516. do {
  1517. uint8_t orig_write_page = buff_rx[1];
  1518. int16_t write_page = orig_write_page;
  1519. uint16_t valid_pages; // unused
  1520. write_page = mf_ultralight_ntag_i2c_addr_tag_to_lin(
  1521. &emulator->data, write_page, emulator->curr_sector, &valid_pages);
  1522. if(write_page == -1) // NTAG I2C range check
  1523. break;
  1524. else if(write_page < 2 || write_page >= emulator->page_num) // Other MFUL/NTAG range check
  1525. break;
  1526. if(emulator->supported_features & MfUltralightSupportAuth) {
  1527. if(emulator->data.type >= MfUltralightTypeNTAGI2CPlus1K) {
  1528. if(!mf_ul_ntag_i2c_plus_check_auth(emulator, orig_write_page, true))
  1529. break;
  1530. } else {
  1531. if(!mf_ul_check_auth(emulator, orig_write_page, true)) break;
  1532. }
  1533. }
  1534. int16_t tag_addr = mf_ultralight_page_addr_to_tag_addr(
  1535. emulator->curr_sector, orig_write_page);
  1536. if(!mf_ul_check_lock(emulator, tag_addr)) break;
  1537. if(emulator->data.type == MfUltralightTypeNTAG203 &&
  1538. orig_write_page == MF_UL_NTAG203_COUNTER_PAGE) {
  1539. send_ack = mf_ul_emulate_ntag203_counter_write(emulator, &buff_rx[2]);
  1540. command_parsed = send_ack;
  1541. } else {
  1542. mf_ul_emulate_write(emulator, tag_addr, write_page, &buff_rx[2]);
  1543. send_ack = true;
  1544. command_parsed = true;
  1545. }
  1546. } while(false);
  1547. }
  1548. } else if(cmd == MF_UL_FAST_WRITE) {
  1549. if(emulator->supported_features & MfUltralightSupportFastWrite) {
  1550. if(buff_rx_len == (1 + 66) * 8) {
  1551. if(buff_rx[1] == 0xF0 && buff_rx[2] == 0xFF) {
  1552. // TODO: update when SRAM emulation implemented
  1553. send_ack = true;
  1554. command_parsed = true;
  1555. }
  1556. }
  1557. }
  1558. } else if(cmd == MF_UL_COMP_WRITE) {
  1559. if(emulator->supported_features & MfUltralightSupportCompatWrite) {
  1560. if(buff_rx_len == (1 + 1) * 8) {
  1561. uint8_t write_page = buff_rx[1];
  1562. do {
  1563. if(write_page < 2 || write_page >= emulator->page_num) break;
  1564. if(emulator->supported_features & MfUltralightSupportAuth &&
  1565. !mf_ul_check_auth(emulator, write_page, true))
  1566. break;
  1567. // Note we don't convert to tag addr here because there's only one sector
  1568. if(!mf_ul_check_lock(emulator, write_page)) break;
  1569. emulator->comp_write_cmd_started = true;
  1570. emulator->comp_write_page_addr = write_page;
  1571. send_ack = true;
  1572. command_parsed = true;
  1573. } while(false);
  1574. }
  1575. }
  1576. } else if(cmd == MF_UL_READ_CNT) {
  1577. if(emulator->supported_features & MfUltralightSupportReadCounter) {
  1578. if(buff_rx_len == (1 + 1) * 8) {
  1579. do {
  1580. uint8_t cnt_num = buff_rx[1];
  1581. // NTAG21x checks
  1582. if(emulator->supported_features & MfUltralightSupportSingleCounter) {
  1583. if(cnt_num != 2) break; // Only counter 2 is available
  1584. if(!emulator->config_cache.access.nfc_cnt_en)
  1585. break; // NAK if counter not enabled
  1586. if(emulator->config_cache.access.nfc_cnt_pwd_prot &&
  1587. !emulator->auth_success)
  1588. break;
  1589. }
  1590. if(cnt_num < 3) {
  1591. buff_tx[0] = emulator->data.counter[cnt_num] & 0xFF;
  1592. buff_tx[1] = (emulator->data.counter[cnt_num] >> 8) & 0xFF;
  1593. buff_tx[2] = (emulator->data.counter[cnt_num] >> 16) & 0xFF;
  1594. tx_bytes = 3;
  1595. *data_type = FURRY_HAL_NFC_TXRX_DEFAULT;
  1596. command_parsed = true;
  1597. }
  1598. } while(false);
  1599. }
  1600. }
  1601. } else if(cmd == MF_UL_INC_CNT) {
  1602. if(emulator->supported_features & MfUltralightSupportIncrCounter) {
  1603. if(buff_rx_len == (1 + 5) * 8) {
  1604. uint8_t cnt_num = buff_rx[1];
  1605. uint32_t inc = (buff_rx[2] | (buff_rx[3] << 8) | (buff_rx[4] << 16));
  1606. // TODO: can you increment by 0 when counter is at 0xffffff?
  1607. if((cnt_num < 3) && (emulator->data.counter[cnt_num] != 0x00FFFFFF) &&
  1608. (emulator->data.counter[cnt_num] + inc <= 0x00FFFFFF)) {
  1609. emulator->data.counter[cnt_num] += inc;
  1610. // We're RAM-backed, so tearing never happens
  1611. emulator->data.tearing[cnt_num] = MF_UL_TEARING_FLAG_DEFAULT;
  1612. emulator->data_changed = true;
  1613. send_ack = true;
  1614. command_parsed = true;
  1615. }
  1616. }
  1617. }
  1618. } else if(cmd == MF_UL_PWD_AUTH) {
  1619. if(emulator->supported_features & MfUltralightSupportAuth) {
  1620. if(buff_rx_len == (1 + 4) * 8) {
  1621. // Record password sent by PCD
  1622. memcpy(
  1623. emulator->auth_attempt.pwd.raw,
  1624. &buff_rx[1],
  1625. sizeof(emulator->auth_attempt.pwd.raw));
  1626. emulator->auth_attempted = true;
  1627. if(emulator->auth_received_callback) {
  1628. emulator->auth_received_callback(
  1629. emulator->auth_attempt, emulator->context);
  1630. }
  1631. uint16_t scaled_authlim = mf_ultralight_calc_auth_count(&emulator->data);
  1632. if(scaled_authlim != 0 && emulator->data.curr_authlim >= scaled_authlim) {
  1633. if(emulator->data.curr_authlim != UINT16_MAX) {
  1634. // Handle case where AUTHLIM has been lowered or changed from 0
  1635. emulator->data.curr_authlim = UINT16_MAX;
  1636. emulator->data_changed = true;
  1637. }
  1638. // AUTHLIM reached, always fail
  1639. buff_tx[0] = MF_UL_NAK_AUTHLIM_REACHED;
  1640. tx_bits = 4;
  1641. *data_type = FURRY_HAL_NFC_TX_RAW_RX_DEFAULT;
  1642. mf_ul_reset_emulation(emulator, false);
  1643. command_parsed = true;
  1644. } else {
  1645. if(memcmp(&buff_rx[1], emulator->config->auth_data.pwd.raw, 4) == 0) {
  1646. // Correct password
  1647. buff_tx[0] = emulator->config->auth_data.pack.raw[0];
  1648. buff_tx[1] = emulator->config->auth_data.pack.raw[1];
  1649. tx_bytes = 2;
  1650. *data_type = FURRY_HAL_NFC_TXRX_DEFAULT;
  1651. emulator->auth_success = true;
  1652. command_parsed = true;
  1653. if(emulator->data.curr_authlim != 0) {
  1654. // Reset current AUTHLIM
  1655. emulator->data.curr_authlim = 0;
  1656. emulator->data_changed = true;
  1657. }
  1658. } else if(!emulator->config->auth_data.pwd.value) {
  1659. // Unknown password, pretend to be an Amiibo
  1660. buff_tx[0] = 0x80;
  1661. buff_tx[1] = 0x80;
  1662. tx_bytes = 2;
  1663. *data_type = FURRY_HAL_NFC_TXRX_DEFAULT;
  1664. emulator->auth_success = true;
  1665. command_parsed = true;
  1666. } else {
  1667. // Wrong password, increase negative verification count
  1668. if(emulator->data.curr_authlim < UINT16_MAX) {
  1669. ++emulator->data.curr_authlim;
  1670. emulator->data_changed = true;
  1671. }
  1672. if(scaled_authlim != 0 &&
  1673. emulator->data.curr_authlim >= scaled_authlim) {
  1674. emulator->data.curr_authlim = UINT16_MAX;
  1675. buff_tx[0] = MF_UL_NAK_AUTHLIM_REACHED;
  1676. tx_bits = 4;
  1677. *data_type = FURRY_HAL_NFC_TX_RAW_RX_DEFAULT;
  1678. mf_ul_reset_emulation(emulator, false);
  1679. command_parsed = true;
  1680. } else {
  1681. // Should delay here to slow brute forcing
  1682. }
  1683. }
  1684. }
  1685. }
  1686. }
  1687. } else if(cmd == MF_UL_READ_SIG) {
  1688. if(emulator->supported_features & MfUltralightSupportSignature) {
  1689. // Check 2nd byte = 0x00 - RFU
  1690. if(buff_rx_len == (1 + 1) * 8 && buff_rx[1] == 0x00) {
  1691. tx_bytes = sizeof(emulator->data.signature);
  1692. memcpy(buff_tx, emulator->data.signature, tx_bytes);
  1693. *data_type = FURRY_HAL_NFC_TXRX_DEFAULT;
  1694. command_parsed = true;
  1695. }
  1696. }
  1697. } else if(cmd == MF_UL_CHECK_TEARING) {
  1698. if(emulator->supported_features & MfUltralightSupportTearingFlags) {
  1699. if(buff_rx_len == (1 + 1) * 8) {
  1700. uint8_t cnt_num = buff_rx[1];
  1701. if(cnt_num < 3) {
  1702. buff_tx[0] = emulator->data.tearing[cnt_num];
  1703. tx_bytes = 1;
  1704. *data_type = FURRY_HAL_NFC_TXRX_DEFAULT;
  1705. command_parsed = true;
  1706. }
  1707. }
  1708. }
  1709. } else if(cmd == MF_UL_HALT_START) {
  1710. reset_idle = true;
  1711. FURI_LOG_D(TAG, "Received HLTA");
  1712. } else if(cmd == MF_UL_SECTOR_SELECT) {
  1713. if(emulator->supported_features & MfUltralightSupportSectorSelect) {
  1714. if(buff_rx_len == (1 + 1) * 8 && buff_rx[1] == 0xFF) {
  1715. // Send ACK
  1716. emulator->sector_select_cmd_started = true;
  1717. send_ack = true;
  1718. command_parsed = true;
  1719. }
  1720. }
  1721. } else if(cmd == MF_UL_READ_VCSL) {
  1722. if(emulator->supported_features & MfUltralightSupportVcsl) {
  1723. if(buff_rx_len == (1 + 20) * 8) {
  1724. buff_tx[0] = emulator->config_cache.vctid;
  1725. tx_bytes = 1;
  1726. *data_type = FURRY_HAL_NFC_TXRX_DEFAULT;
  1727. command_parsed = true;
  1728. }
  1729. }
  1730. } else {
  1731. // NTAG203 appears to NAK instead of just falling off on invalid commands
  1732. if(emulator->data.type != MfUltralightTypeNTAG203) reset_idle = true;
  1733. FURI_LOG_D(TAG, "Received invalid command");
  1734. }
  1735. } else {
  1736. reset_idle = true;
  1737. FURI_LOG_D(TAG, "Received invalid buffer less than 8 bits in length");
  1738. }
  1739. if(reset_idle) {
  1740. mf_ul_reset_emulation(emulator, false);
  1741. tx_bits = 0;
  1742. command_parsed = true;
  1743. }
  1744. if(!command_parsed) {
  1745. // Send NACK
  1746. buff_tx[0] = MF_UL_NAK_INVALID_ARGUMENT;
  1747. tx_bits = 4;
  1748. *data_type = FURRY_HAL_NFC_TX_RAW_RX_DEFAULT;
  1749. // Every NAK should cause reset to IDLE
  1750. mf_ul_reset_emulation(emulator, false);
  1751. } else if(send_ack) {
  1752. buff_tx[0] = MF_UL_ACK;
  1753. tx_bits = 4;
  1754. *data_type = FURRY_HAL_NFC_TX_RAW_RX_DEFAULT;
  1755. }
  1756. if(respond_nothing) {
  1757. *buff_tx_len = UINT16_MAX;
  1758. *data_type = FURRY_HAL_NFC_TX_RAW_RX_DEFAULT;
  1759. } else {
  1760. // Return tx buffer size in bits
  1761. if(tx_bytes) {
  1762. tx_bits = tx_bytes * 8;
  1763. }
  1764. *buff_tx_len = tx_bits;
  1765. }
  1766. #ifdef FURI_DEBUG
  1767. if(*buff_tx_len == UINT16_MAX) {
  1768. FURI_LOG_T(TAG, "Emu TX: no reply");
  1769. } else if(*buff_tx_len > 0) {
  1770. int count = (*buff_tx_len + 7) / 8;
  1771. for(int i = 0; i < count; ++i) {
  1772. furi_string_cat_printf(debug_buf, "%02x ", buff_tx[i]);
  1773. }
  1774. furi_string_trim(debug_buf);
  1775. FURI_LOG_T(TAG, "Emu TX (%d): %s", *buff_tx_len, furi_string_get_cstr(debug_buf));
  1776. furi_string_free(debug_buf);
  1777. } else {
  1778. FURI_LOG_T(TAG, "Emu TX: HALT");
  1779. }
  1780. #endif
  1781. return tx_bits > 0;
  1782. }
  1783. bool mf_ul_is_full_capture(MfUltralightData* data) {
  1784. if(data->data_read != data->data_size) return false;
  1785. // Having read all the pages doesn't mean that we've got everything.
  1786. // By default PWD is 0xFFFFFFFF, but if read back it is always 0x00000000,
  1787. // so a default read on an auth-supported NTAG is never complete.
  1788. if(!(mf_ul_get_features(data->type) & MfUltralightSupportAuth)) return true;
  1789. MfUltralightConfigPages* config = mf_ultralight_get_config_pages(data);
  1790. return config->auth_data.pwd.value != 0 || config->auth_data.pack.value != 0;
  1791. }