mifare_ultralight.c 78 KB

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