mifare_ultralight.c 76 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865
  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->has_auth = 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. }
  678. return card_read;
  679. }
  680. static void mf_ul_protect_auth_data_on_read_command_i2c(
  681. uint8_t* tx_buff,
  682. uint8_t start_page,
  683. uint8_t end_page,
  684. MfUltralightEmulator* emulator) {
  685. if(emulator->data.type >= MfUltralightTypeNTAGI2CPlus1K) {
  686. // Blank out PWD and PACK
  687. if(start_page <= 229 && end_page >= 229) {
  688. uint16_t offset = (229 - start_page) * 4;
  689. uint8_t count = 4;
  690. if(end_page >= 230) count += 2;
  691. memset(&tx_buff[offset], 0, count);
  692. }
  693. // Handle AUTH0 for sector 0
  694. if(!emulator->auth_success) {
  695. if(emulator->config_cache.access.prot) {
  696. uint8_t auth0 = emulator->config_cache.auth0;
  697. if(auth0 < end_page) {
  698. // start_page is always < auth0; otherwise is NAK'd already
  699. uint8_t page_offset = auth0 - start_page;
  700. uint8_t page_count = end_page - auth0;
  701. memset(&tx_buff[page_offset * 4], 0, page_count * 4);
  702. }
  703. }
  704. }
  705. }
  706. }
  707. static void mf_ul_ntag_i2c_fill_cross_area_read(
  708. uint8_t* tx_buff,
  709. uint8_t start_page,
  710. uint8_t end_page,
  711. MfUltralightEmulator* emulator) {
  712. // For copying config or session registers in fast read
  713. int16_t tx_page_offset;
  714. int16_t data_page_offset;
  715. uint8_t page_length;
  716. bool apply = false;
  717. MfUltralightType type = emulator->data.type;
  718. if(emulator->curr_sector == 0) {
  719. if(type == MfUltralightTypeNTAGI2C1K) {
  720. if(start_page <= 233 && end_page >= 232) {
  721. tx_page_offset = start_page - 232;
  722. data_page_offset = 227;
  723. page_length = 2;
  724. apply = true;
  725. }
  726. } else if(type == MfUltralightTypeNTAGI2CPlus1K || type == MfUltralightTypeNTAGI2CPlus2K) {
  727. if(start_page <= 237 && end_page >= 236) {
  728. tx_page_offset = start_page - 236;
  729. data_page_offset = 234;
  730. page_length = 2;
  731. apply = true;
  732. }
  733. }
  734. } else if(emulator->curr_sector == 1) {
  735. if(type == MfUltralightTypeNTAGI2C2K) {
  736. if(start_page <= 233 && end_page >= 232) {
  737. tx_page_offset = start_page - 232;
  738. data_page_offset = 483;
  739. page_length = 2;
  740. apply = true;
  741. }
  742. }
  743. }
  744. if(apply) {
  745. while(tx_page_offset < 0 && page_length > 0) {
  746. ++tx_page_offset;
  747. ++data_page_offset;
  748. --page_length;
  749. }
  750. memcpy(
  751. &tx_buff[tx_page_offset * 4],
  752. &emulator->data.data[data_page_offset * 4],
  753. page_length * 4);
  754. }
  755. }
  756. static bool mf_ul_check_auth(MfUltralightEmulator* emulator, uint8_t start_page, bool is_write) {
  757. if(!emulator->auth_success) {
  758. if(start_page >= emulator->config_cache.auth0 &&
  759. (emulator->config_cache.access.prot || is_write))
  760. return false;
  761. }
  762. if(is_write && emulator->config_cache.access.cfglck) {
  763. uint16_t config_start_page = emulator->page_num - 4;
  764. if(start_page == config_start_page || start_page == config_start_page + 1) return false;
  765. }
  766. return true;
  767. }
  768. static bool mf_ul_ntag_i2c_plus_check_auth(
  769. MfUltralightEmulator* emulator,
  770. uint8_t start_page,
  771. bool is_write) {
  772. if(!emulator->auth_success) {
  773. // Check NFC_PROT
  774. if(emulator->curr_sector == 0 && (emulator->config_cache.access.prot || is_write)) {
  775. if(start_page >= emulator->config_cache.auth0) return false;
  776. } else if(emulator->curr_sector == 1) {
  777. // We don't have to specifically check for type because this is done
  778. // by address translator
  779. uint8_t pt_i2c = emulator->data.data[231 * 4];
  780. // Check 2K_PROT
  781. if(pt_i2c & 0x08) return false;
  782. }
  783. }
  784. if(emulator->curr_sector == 1) {
  785. // Check NFC_DIS_SEC1
  786. if(emulator->config_cache.access.nfc_dis_sec1) return false;
  787. }
  788. return true;
  789. }
  790. static int16_t mf_ul_get_dynamic_lock_page_addr(MfUltralightData* data) {
  791. switch(data->type) {
  792. case MfUltralightTypeNTAG203:
  793. return 0x28;
  794. case MfUltralightTypeUL21:
  795. case MfUltralightTypeNTAG213:
  796. case MfUltralightTypeNTAG215:
  797. case MfUltralightTypeNTAG216:
  798. return data->data_size / 4 - 5;
  799. case MfUltralightTypeNTAGI2C1K:
  800. case MfUltralightTypeNTAGI2CPlus1K:
  801. case MfUltralightTypeNTAGI2CPlus2K:
  802. return 0xe2;
  803. case MfUltralightTypeNTAGI2C2K:
  804. return 0x1e0;
  805. default:
  806. return -1; // No dynamic lock bytes
  807. }
  808. }
  809. // Returns true if page not locked
  810. // write_page is tag address
  811. static bool mf_ul_check_lock(MfUltralightEmulator* emulator, int16_t write_page) {
  812. if(write_page < 2) return false; // Page 0-1 is always locked
  813. if(write_page == 2) return true; // Page 2 does not have a lock flag
  814. // Check static lock bytes
  815. if(write_page <= 15) {
  816. uint16_t static_lock_bytes = emulator->data.data[10] | (emulator->data.data[11] << 8);
  817. return (static_lock_bytes & (1 << write_page)) == 0;
  818. }
  819. // Check dynamic lock bytes
  820. // Check max page
  821. switch(emulator->data.type) {
  822. case MfUltralightTypeNTAG203:
  823. // Counter page can be locked and is after dynamic locks
  824. if(write_page == 40) return true;
  825. break;
  826. case MfUltralightTypeUL21:
  827. case MfUltralightTypeNTAG213:
  828. case MfUltralightTypeNTAG215:
  829. case MfUltralightTypeNTAG216:
  830. if(write_page >= emulator->page_num - 5) return true;
  831. break;
  832. case MfUltralightTypeNTAGI2C1K:
  833. case MfUltralightTypeNTAGI2CPlus1K:
  834. if(write_page > 225) return true;
  835. break;
  836. case MfUltralightTypeNTAGI2C2K:
  837. if(write_page > 479) return true;
  838. break;
  839. case MfUltralightTypeNTAGI2CPlus2K:
  840. if(write_page >= 226 && write_page <= 255) return true;
  841. if(write_page >= 512) return true;
  842. break;
  843. default:
  844. furi_crash("Unknown MFUL");
  845. return true;
  846. }
  847. int16_t dynamic_lock_index = mf_ul_get_dynamic_lock_page_addr(&emulator->data);
  848. if(dynamic_lock_index == -1) return true;
  849. // Run address through converter because NTAG I2C 2K is special
  850. uint16_t valid_pages; // unused
  851. dynamic_lock_index =
  852. mf_ultralight_ntag_i2c_addr_tag_to_lin(
  853. &emulator->data, dynamic_lock_index & 0xff, dynamic_lock_index >> 8, &valid_pages) *
  854. 4;
  855. uint16_t dynamic_lock_bytes = emulator->data.data[dynamic_lock_index] |
  856. (emulator->data.data[dynamic_lock_index + 1] << 8);
  857. uint8_t shift;
  858. switch(emulator->data.type) {
  859. // low byte LSB range, MSB range
  860. case MfUltralightTypeNTAG203:
  861. if(write_page >= 16 && write_page <= 27)
  862. shift = (write_page - 16) / 4 + 1;
  863. else if(write_page >= 28 && write_page <= 39)
  864. shift = (write_page - 28) / 4 + 5;
  865. else if(write_page == 41)
  866. shift = 12;
  867. else {
  868. furi_crash("Unknown MFUL");
  869. }
  870. break;
  871. case MfUltralightTypeUL21:
  872. case MfUltralightTypeNTAG213:
  873. // 16-17, 30-31
  874. shift = (write_page - 16) / 2;
  875. break;
  876. case MfUltralightTypeNTAG215:
  877. case MfUltralightTypeNTAG216:
  878. case MfUltralightTypeNTAGI2C1K:
  879. case MfUltralightTypeNTAGI2CPlus1K:
  880. // 16-31, 128-129
  881. // 16-31, 128-143
  882. shift = (write_page - 16) / 16;
  883. break;
  884. case MfUltralightTypeNTAGI2C2K:
  885. // 16-47, 240-271
  886. shift = (write_page - 16) / 32;
  887. break;
  888. case MfUltralightTypeNTAGI2CPlus2K:
  889. // 16-47, 256-271
  890. if(write_page >= 208 && write_page <= 225)
  891. shift = 6;
  892. else if(write_page >= 256 && write_page <= 271)
  893. shift = 7;
  894. else
  895. shift = (write_page - 16) / 32;
  896. break;
  897. default:
  898. furi_crash("Unknown MFUL");
  899. break;
  900. }
  901. return (dynamic_lock_bytes & (1 << shift)) == 0;
  902. }
  903. static void mf_ul_make_ascii_mirror(MfUltralightEmulator* emulator, FuriString* str) {
  904. // Locals to improve readability
  905. uint8_t mirror_page = emulator->config->mirror_page;
  906. uint8_t mirror_byte = emulator->config->mirror.mirror_byte;
  907. MfUltralightMirrorConf mirror_conf = emulator->config_cache.mirror.mirror_conf;
  908. uint16_t last_user_page_index = emulator->page_num - 6;
  909. bool uid_printed = false;
  910. if(mirror_conf == MfUltralightMirrorUid || mirror_conf == MfUltralightMirrorUidCounter) {
  911. // UID range check
  912. if(mirror_page < 4 || mirror_page > last_user_page_index - 3 ||
  913. (mirror_page == last_user_page_index - 3 && mirror_byte > 2)) {
  914. if(mirror_conf == MfUltralightMirrorUid) return;
  915. // NTAG21x has the peculiar behavior when UID+counter selected, if UID does not fit but
  916. // counter will fit, it will actually mirror the counter
  917. furi_string_cat(str, " ");
  918. } else {
  919. for(int i = 0; i < 3; ++i) {
  920. furi_string_cat_printf(str, "%02X", emulator->data.data[i]);
  921. }
  922. // Skip BCC0
  923. for(int i = 4; i < 8; ++i) {
  924. furi_string_cat_printf(str, "%02X", emulator->data.data[i]);
  925. }
  926. uid_printed = true;
  927. }
  928. uint16_t next_byte_offset = mirror_page * 4 + mirror_byte + 14;
  929. if(mirror_conf == MfUltralightMirrorUidCounter) ++next_byte_offset;
  930. mirror_page = next_byte_offset / 4;
  931. mirror_byte = next_byte_offset % 4;
  932. }
  933. if(mirror_conf == MfUltralightMirrorCounter || mirror_conf == MfUltralightMirrorUidCounter) {
  934. // Counter is only printed if counter enabled
  935. if(emulator->config_cache.access.nfc_cnt_en) {
  936. // Counter protection check
  937. if(emulator->config_cache.access.nfc_cnt_pwd_prot && !emulator->auth_success) return;
  938. // Counter range check
  939. if(mirror_page < 4) return;
  940. if(mirror_page > last_user_page_index - 1) return;
  941. if(mirror_page == last_user_page_index - 1 && mirror_byte > 2) return;
  942. if(mirror_conf == MfUltralightMirrorUidCounter)
  943. furi_string_cat(str, uid_printed ? "x" : " ");
  944. furi_string_cat_printf(str, "%06lX", emulator->data.counter[2]);
  945. }
  946. }
  947. }
  948. static void mf_ul_increment_single_counter(MfUltralightEmulator* emulator) {
  949. if(!emulator->read_counter_incremented && emulator->config_cache.access.nfc_cnt_en) {
  950. if(emulator->data.counter[2] < 0xFFFFFF) {
  951. ++emulator->data.counter[2];
  952. emulator->data_changed = true;
  953. }
  954. emulator->read_counter_incremented = true;
  955. }
  956. }
  957. static bool
  958. mf_ul_emulate_ntag203_counter_write(MfUltralightEmulator* emulator, uint8_t* page_buff) {
  959. // We'll reuse the existing counters for other NTAGs as staging
  960. // Counter 0 stores original value, data is new value
  961. uint32_t counter_value;
  962. if(emulator->data.tearing[0] == MF_UL_TEARING_FLAG_DEFAULT) {
  963. counter_value = emulator->data.data[MF_UL_NTAG203_COUNTER_PAGE * 4] |
  964. (emulator->data.data[MF_UL_NTAG203_COUNTER_PAGE * 4 + 1] << 8);
  965. } else {
  966. // We've had a reset here, so load from original value
  967. counter_value = emulator->data.counter[0];
  968. }
  969. // Although the datasheet says increment by 0 is always possible, this is not the case on
  970. // an actual tag. If the counter is at 0xFFFF, any writes are locked out.
  971. if(counter_value == 0xFFFF) return false;
  972. uint32_t increment = page_buff[0] | (page_buff[1] << 8);
  973. if(counter_value == 0) {
  974. counter_value = increment;
  975. } else {
  976. // Per datasheet specifying > 0x000F is supposed to NAK, but actual tag doesn't
  977. increment &= 0x000F;
  978. if(counter_value + increment > 0xFFFF) return false;
  979. counter_value += increment;
  980. }
  981. // Commit to new value counter
  982. emulator->data.data[MF_UL_NTAG203_COUNTER_PAGE * 4] = (uint8_t)counter_value;
  983. emulator->data.data[MF_UL_NTAG203_COUNTER_PAGE * 4 + 1] = (uint8_t)(counter_value >> 8);
  984. emulator->data.tearing[0] = MF_UL_TEARING_FLAG_DEFAULT;
  985. if(counter_value == 0xFFFF) {
  986. // Tag will lock out counter if final number is 0xFFFF, even if you try to roll it back
  987. emulator->data.counter[1] = 0xFFFF;
  988. }
  989. emulator->data_changed = true;
  990. return true;
  991. }
  992. static void mf_ul_emulate_write(
  993. MfUltralightEmulator* emulator,
  994. int16_t tag_addr,
  995. int16_t write_page,
  996. uint8_t* page_buff) {
  997. // Assumption: all access checks have been completed
  998. if(tag_addr == 2) {
  999. // Handle static locks
  1000. uint16_t orig_static_locks = emulator->data.data[write_page * 4 + 2] |
  1001. (emulator->data.data[write_page * 4 + 3] << 8);
  1002. uint16_t new_static_locks = page_buff[2] | (page_buff[3] << 8);
  1003. if(orig_static_locks & 1) new_static_locks &= ~0x08;
  1004. if(orig_static_locks & 2) new_static_locks &= ~0xF0;
  1005. if(orig_static_locks & 4) new_static_locks &= 0xFF;
  1006. new_static_locks |= orig_static_locks;
  1007. page_buff[0] = emulator->data.data[write_page * 4];
  1008. page_buff[1] = emulator->data.data[write_page * 4 + 1];
  1009. page_buff[2] = new_static_locks & 0xff;
  1010. page_buff[3] = new_static_locks >> 8;
  1011. } else if(tag_addr == 3) {
  1012. // Handle OTP/capability container
  1013. *(uint32_t*)page_buff |= *(uint32_t*)&emulator->data.data[write_page * 4];
  1014. } else if(tag_addr == mf_ul_get_dynamic_lock_page_addr(&emulator->data)) {
  1015. // Handle dynamic locks
  1016. if(emulator->data.type == MfUltralightTypeNTAG203) {
  1017. // NTAG203 lock bytes are a bit different from the others
  1018. uint8_t orig_page_lock_byte = emulator->data.data[write_page * 4];
  1019. uint8_t orig_cnt_lock_byte = emulator->data.data[write_page * 4 + 1];
  1020. uint8_t new_page_lock_byte = page_buff[0];
  1021. uint8_t new_cnt_lock_byte = page_buff[1];
  1022. if(orig_page_lock_byte & 0x01) // Block lock bits 1-3
  1023. new_page_lock_byte &= ~0x0E;
  1024. if(orig_page_lock_byte & 0x10) // Block lock bits 5-7
  1025. new_page_lock_byte &= ~0xE0;
  1026. for(uint8_t i = 0; i < 4; ++i) {
  1027. if(orig_cnt_lock_byte & (1 << i)) // Block lock counter bit
  1028. new_cnt_lock_byte &= ~(1 << (4 + i));
  1029. }
  1030. new_page_lock_byte |= orig_page_lock_byte;
  1031. new_cnt_lock_byte |= orig_cnt_lock_byte;
  1032. page_buff[0] = new_page_lock_byte;
  1033. page_buff[1] = new_cnt_lock_byte;
  1034. } else {
  1035. uint16_t orig_locks = emulator->data.data[write_page * 4] |
  1036. (emulator->data.data[write_page * 4 + 1] << 8);
  1037. uint8_t orig_block_locks = emulator->data.data[write_page * 4 + 2];
  1038. uint16_t new_locks = page_buff[0] | (page_buff[1] << 8);
  1039. uint8_t new_block_locks = page_buff[2];
  1040. int block_lock_count;
  1041. switch(emulator->data.type) {
  1042. case MfUltralightTypeUL21:
  1043. block_lock_count = 5;
  1044. break;
  1045. case MfUltralightTypeNTAG213:
  1046. block_lock_count = 6;
  1047. break;
  1048. case MfUltralightTypeNTAG215:
  1049. block_lock_count = 4;
  1050. break;
  1051. case MfUltralightTypeNTAG216:
  1052. case MfUltralightTypeNTAGI2C1K:
  1053. case MfUltralightTypeNTAGI2CPlus1K:
  1054. block_lock_count = 7;
  1055. break;
  1056. case MfUltralightTypeNTAGI2C2K:
  1057. case MfUltralightTypeNTAGI2CPlus2K:
  1058. block_lock_count = 8;
  1059. break;
  1060. default:
  1061. furi_crash("Unknown MFUL");
  1062. break;
  1063. }
  1064. for(int i = 0; i < block_lock_count; ++i) {
  1065. if(orig_block_locks & (1 << i)) new_locks &= ~(3 << (2 * i));
  1066. }
  1067. new_locks |= orig_locks;
  1068. new_block_locks |= orig_block_locks;
  1069. page_buff[0] = new_locks & 0xff;
  1070. page_buff[1] = new_locks >> 8;
  1071. page_buff[2] = new_block_locks;
  1072. if(emulator->data.type >= MfUltralightTypeUL21 &&
  1073. emulator->data.type <= MfUltralightTypeNTAG216)
  1074. page_buff[3] = MF_UL_TEARING_FLAG_DEFAULT;
  1075. else
  1076. page_buff[3] = 0;
  1077. }
  1078. }
  1079. memcpy(&emulator->data.data[write_page * 4], page_buff, 4);
  1080. emulator->data_changed = true;
  1081. }
  1082. void mf_ul_reset_emulation(MfUltralightEmulator* emulator, bool is_power_cycle) {
  1083. emulator->curr_sector = 0;
  1084. emulator->ntag_i2c_plus_sector3_lockout = false;
  1085. emulator->auth_success = false;
  1086. if(is_power_cycle) {
  1087. if(emulator->config != NULL) emulator->config_cache = *emulator->config;
  1088. if(emulator->supported_features & MfUltralightSupportSingleCounter) {
  1089. emulator->read_counter_incremented = false;
  1090. }
  1091. if(emulator->data.type == MfUltralightTypeNTAG203) {
  1092. // Apply lockout if counter ever reached 0xFFFF
  1093. if(emulator->data.counter[1] == 0xFFFF) {
  1094. emulator->data.data[MF_UL_NTAG203_COUNTER_PAGE * 4] = 0xFF;
  1095. emulator->data.data[MF_UL_NTAG203_COUNTER_PAGE * 4 + 1] = 0xFF;
  1096. }
  1097. // Copy original counter value from data
  1098. emulator->data.counter[0] =
  1099. emulator->data.data[MF_UL_NTAG203_COUNTER_PAGE * 4] |
  1100. (emulator->data.data[MF_UL_NTAG203_COUNTER_PAGE * 4 + 1] << 8);
  1101. }
  1102. } else {
  1103. if(emulator->config != NULL) {
  1104. // ACCESS (less CFGLCK) and AUTH0 are updated when reactivated
  1105. // MIRROR_CONF is not; don't know about STRG_MOD_EN, but we're not using that anyway
  1106. emulator->config_cache.access.value = (emulator->config->access.value & 0xBF) |
  1107. (emulator->config_cache.access.value & 0x40);
  1108. emulator->config_cache.auth0 = emulator->config->auth0;
  1109. }
  1110. }
  1111. if(emulator->data.type == MfUltralightTypeNTAG203) {
  1112. // Mark counter as dirty
  1113. emulator->data.tearing[0] = 0;
  1114. }
  1115. }
  1116. void mf_ul_prepare_emulation(MfUltralightEmulator* emulator, MfUltralightData* data) {
  1117. FURI_LOG_D(TAG, "Prepare emulation");
  1118. emulator->data = *data;
  1119. emulator->supported_features = mf_ul_get_features(data->type);
  1120. emulator->config = mf_ultralight_get_config_pages(&emulator->data);
  1121. emulator->page_num = emulator->data.data_size / 4;
  1122. emulator->data_changed = false;
  1123. emulator->comp_write_cmd_started = false;
  1124. emulator->sector_select_cmd_started = false;
  1125. mf_ul_reset_emulation(emulator, true);
  1126. }
  1127. bool mf_ul_prepare_emulation_response(
  1128. uint8_t* buff_rx,
  1129. uint16_t buff_rx_len,
  1130. uint8_t* buff_tx,
  1131. uint16_t* buff_tx_len,
  1132. uint32_t* data_type,
  1133. void* context) {
  1134. furi_assert(context);
  1135. MfUltralightEmulator* emulator = context;
  1136. uint16_t tx_bytes = 0;
  1137. uint16_t tx_bits = 0;
  1138. bool command_parsed = false;
  1139. bool send_ack = false;
  1140. bool respond_nothing = false;
  1141. bool reset_idle = false;
  1142. #ifdef FURI_DEBUG
  1143. FuriString* debug_buf;
  1144. debug_buf = furi_string_alloc();
  1145. for(int i = 0; i < (buff_rx_len + 7) / 8; ++i) {
  1146. furi_string_cat_printf(debug_buf, "%02x ", buff_rx[i]);
  1147. }
  1148. furi_string_trim(debug_buf);
  1149. FURI_LOG_T(TAG, "Emu RX (%d): %s", buff_rx_len, furi_string_get_cstr(debug_buf));
  1150. furi_string_reset(debug_buf);
  1151. #endif
  1152. // Check composite commands
  1153. if(emulator->comp_write_cmd_started) {
  1154. if(buff_rx_len == 16 * 8) {
  1155. if(emulator->data.type == MfUltralightTypeNTAG203 &&
  1156. emulator->comp_write_page_addr == MF_UL_NTAG203_COUNTER_PAGE) {
  1157. send_ack = mf_ul_emulate_ntag203_counter_write(emulator, buff_rx);
  1158. command_parsed = send_ack;
  1159. } else {
  1160. mf_ul_emulate_write(
  1161. emulator,
  1162. emulator->comp_write_page_addr,
  1163. emulator->comp_write_page_addr,
  1164. buff_rx);
  1165. send_ack = true;
  1166. command_parsed = true;
  1167. }
  1168. }
  1169. emulator->comp_write_cmd_started = false;
  1170. } else if(emulator->sector_select_cmd_started) {
  1171. if(buff_rx_len == 4 * 8) {
  1172. if(buff_rx[0] <= 0xFE) {
  1173. emulator->curr_sector = buff_rx[0] > 3 ? 0 : buff_rx[0];
  1174. emulator->ntag_i2c_plus_sector3_lockout = false;
  1175. command_parsed = true;
  1176. respond_nothing = true;
  1177. FURI_LOG_D(TAG, "Changing sector to %d", emulator->curr_sector);
  1178. }
  1179. }
  1180. emulator->sector_select_cmd_started = false;
  1181. } else if(buff_rx_len >= 8) {
  1182. uint8_t cmd = buff_rx[0];
  1183. if(cmd == MF_UL_GET_VERSION_CMD) {
  1184. if(emulator->data.type >= MfUltralightTypeUL11) {
  1185. if(buff_rx_len == 1 * 8) {
  1186. tx_bytes = sizeof(emulator->data.version);
  1187. memcpy(buff_tx, &emulator->data.version, tx_bytes);
  1188. *data_type = FURI_HAL_NFC_TXRX_DEFAULT;
  1189. command_parsed = true;
  1190. }
  1191. }
  1192. } else if(cmd == MF_UL_READ_CMD) {
  1193. if(buff_rx_len == (1 + 1) * 8) {
  1194. int16_t start_page = buff_rx[1];
  1195. tx_bytes = 16;
  1196. if(emulator->data.type < MfUltralightTypeNTAGI2C1K) {
  1197. if(start_page < emulator->page_num) {
  1198. do {
  1199. uint8_t copied_pages = 0;
  1200. uint8_t src_page = start_page;
  1201. uint8_t last_page_plus_one = start_page + 4;
  1202. uint8_t pwd_page = emulator->page_num - 2;
  1203. FuriString* ascii_mirror = NULL;
  1204. size_t ascii_mirror_len = 0;
  1205. const char* ascii_mirror_cptr = NULL;
  1206. uint8_t ascii_mirror_curr_page = 0;
  1207. uint8_t ascii_mirror_curr_byte = 0;
  1208. if(last_page_plus_one > emulator->page_num)
  1209. last_page_plus_one = emulator->page_num;
  1210. if(emulator->supported_features & MfUltralightSupportAuth) {
  1211. if(!mf_ul_check_auth(emulator, start_page, false)) break;
  1212. if(!emulator->auth_success && emulator->config_cache.access.prot &&
  1213. emulator->config_cache.auth0 < last_page_plus_one)
  1214. last_page_plus_one = emulator->config_cache.auth0;
  1215. }
  1216. if(emulator->supported_features & MfUltralightSupportSingleCounter)
  1217. mf_ul_increment_single_counter(emulator);
  1218. if(emulator->supported_features & MfUltralightSupportAsciiMirror &&
  1219. emulator->config_cache.mirror.mirror_conf !=
  1220. MfUltralightMirrorNone) {
  1221. ascii_mirror_curr_byte = emulator->config->mirror.mirror_byte;
  1222. ascii_mirror_curr_page = emulator->config->mirror_page;
  1223. // Try to avoid wasting time making mirror if we won't copy it
  1224. // Conservatively check with UID+counter mirror size
  1225. if(last_page_plus_one > ascii_mirror_curr_page &&
  1226. start_page + 3 >= ascii_mirror_curr_page &&
  1227. start_page <= ascii_mirror_curr_page + 6) {
  1228. ascii_mirror = furi_string_alloc();
  1229. mf_ul_make_ascii_mirror(emulator, ascii_mirror);
  1230. ascii_mirror_len = furi_string_utf8_length(ascii_mirror);
  1231. ascii_mirror_cptr = furi_string_get_cstr(ascii_mirror);
  1232. // Move pointer to where it should be to start copying
  1233. if(ascii_mirror_len > 0 &&
  1234. ascii_mirror_curr_page < start_page &&
  1235. ascii_mirror_curr_byte != 0) {
  1236. uint8_t diff = 4 - ascii_mirror_curr_byte;
  1237. ascii_mirror_len -= diff;
  1238. ascii_mirror_cptr += diff;
  1239. ascii_mirror_curr_byte = 0;
  1240. ++ascii_mirror_curr_page;
  1241. }
  1242. while(ascii_mirror_len > 0 &&
  1243. ascii_mirror_curr_page < start_page) {
  1244. uint8_t diff = ascii_mirror_len > 4 ? 4 : ascii_mirror_len;
  1245. ascii_mirror_len -= diff;
  1246. ascii_mirror_cptr += diff;
  1247. ++ascii_mirror_curr_page;
  1248. }
  1249. }
  1250. }
  1251. uint8_t* dest_ptr = buff_tx;
  1252. while(copied_pages < 4) {
  1253. // Copy page
  1254. memcpy(dest_ptr, &emulator->data.data[src_page * 4], 4);
  1255. // Note: don't have to worry about roll-over with ASCII mirror because
  1256. // lowest valid page for it is 4, while roll-over will at best read
  1257. // pages 0-2
  1258. if(ascii_mirror_len > 0 && src_page == ascii_mirror_curr_page) {
  1259. // Copy ASCII mirror
  1260. size_t copy_len = 4 - ascii_mirror_curr_byte;
  1261. if(copy_len > ascii_mirror_len) copy_len = ascii_mirror_len;
  1262. for(size_t i = 0; i < copy_len; ++i) {
  1263. if(*ascii_mirror_cptr != ' ')
  1264. dest_ptr[ascii_mirror_curr_byte] =
  1265. (uint8_t)*ascii_mirror_cptr;
  1266. ++ascii_mirror_curr_byte;
  1267. ++ascii_mirror_cptr;
  1268. }
  1269. ascii_mirror_len -= copy_len;
  1270. // Don't care if this is inaccurate after ascii_mirror_len = 0
  1271. ascii_mirror_curr_byte = 0;
  1272. ++ascii_mirror_curr_page;
  1273. }
  1274. if(emulator->supported_features & MfUltralightSupportAuth) {
  1275. if(src_page == pwd_page || src_page == pwd_page + 1) {
  1276. // Blank out PWD and PACK pages
  1277. memset(dest_ptr, 0, 4);
  1278. }
  1279. }
  1280. dest_ptr += 4;
  1281. ++copied_pages;
  1282. ++src_page;
  1283. if(src_page >= last_page_plus_one) src_page = 0;
  1284. }
  1285. if(ascii_mirror != NULL) {
  1286. furi_string_free(ascii_mirror);
  1287. }
  1288. *data_type = FURI_HAL_NFC_TXRX_DEFAULT;
  1289. command_parsed = true;
  1290. } while(false);
  1291. }
  1292. } else {
  1293. uint16_t valid_pages;
  1294. start_page = mf_ultralight_ntag_i2c_addr_tag_to_lin(
  1295. &emulator->data, start_page, emulator->curr_sector, &valid_pages);
  1296. if(start_page != -1) {
  1297. if(emulator->data.type < MfUltralightTypeNTAGI2CPlus1K ||
  1298. mf_ul_ntag_i2c_plus_check_auth(emulator, buff_rx[1], false)) {
  1299. if(emulator->data.type >= MfUltralightTypeNTAGI2CPlus1K &&
  1300. emulator->curr_sector == 3 && valid_pages == 1) {
  1301. // Rewind back a sector to match behavior on a real tag
  1302. --start_page;
  1303. ++valid_pages;
  1304. }
  1305. uint16_t copy_count = (valid_pages > 4 ? 4 : valid_pages) * 4;
  1306. FURI_LOG_D(
  1307. TAG,
  1308. "NTAG I2C Emu: page valid, %02x:%02x -> %d, %d",
  1309. emulator->curr_sector,
  1310. buff_rx[1],
  1311. start_page,
  1312. valid_pages);
  1313. memcpy(buff_tx, &emulator->data.data[start_page * 4], copy_count);
  1314. // For NTAG I2C, there's no roll-over; remainder is filled by null bytes
  1315. if(copy_count < tx_bytes)
  1316. memset(&buff_tx[copy_count], 0, tx_bytes - copy_count);
  1317. // Special case: NTAG I2C Plus sector 0 page 233 read crosses into page 236
  1318. if(start_page == 233)
  1319. memcpy(
  1320. &buff_tx[12], &emulator->data.data[(start_page + 1) * 4], 4);
  1321. mf_ul_protect_auth_data_on_read_command_i2c(
  1322. buff_tx, start_page, start_page + copy_count / 4 - 1, emulator);
  1323. *data_type = FURI_HAL_NFC_TXRX_DEFAULT;
  1324. command_parsed = true;
  1325. }
  1326. } else {
  1327. FURI_LOG_D(
  1328. TAG,
  1329. "NTAG I2C Emu: page invalid, %02x:%02x",
  1330. emulator->curr_sector,
  1331. buff_rx[1]);
  1332. if(emulator->data.type >= MfUltralightTypeNTAGI2CPlus1K &&
  1333. emulator->curr_sector == 3 &&
  1334. !emulator->ntag_i2c_plus_sector3_lockout) {
  1335. // NTAG I2C Plus has a weird behavior where if you read sector 3
  1336. // at an invalid address, it responds with zeroes then locks
  1337. // the read out, while if you read the mirrored session registers,
  1338. // it returns both session registers on either pages
  1339. memset(buff_tx, 0, tx_bytes);
  1340. *data_type = FURI_HAL_NFC_TXRX_DEFAULT;
  1341. command_parsed = true;
  1342. emulator->ntag_i2c_plus_sector3_lockout = true;
  1343. }
  1344. }
  1345. }
  1346. if(!command_parsed) tx_bytes = 0;
  1347. }
  1348. } else if(cmd == MF_UL_FAST_READ_CMD) {
  1349. if(emulator->supported_features & MfUltralightSupportFastRead) {
  1350. if(buff_rx_len == (1 + 2) * 8) {
  1351. int16_t start_page = buff_rx[1];
  1352. uint8_t end_page = buff_rx[2];
  1353. if(start_page <= end_page) {
  1354. tx_bytes = ((end_page + 1) - start_page) * 4;
  1355. if(emulator->data.type < MfUltralightTypeNTAGI2C1K) {
  1356. if((start_page < emulator->page_num) &&
  1357. (end_page < emulator->page_num)) {
  1358. do {
  1359. if(emulator->supported_features & MfUltralightSupportAuth) {
  1360. // NAK if not authenticated and requested pages cross over AUTH0
  1361. if(!emulator->auth_success &&
  1362. emulator->config_cache.access.prot &&
  1363. (start_page >= emulator->config_cache.auth0 ||
  1364. end_page >= emulator->config_cache.auth0))
  1365. break;
  1366. }
  1367. if(emulator->supported_features &
  1368. MfUltralightSupportSingleCounter)
  1369. mf_ul_increment_single_counter(emulator);
  1370. // Copy requested pages
  1371. memcpy(
  1372. buff_tx, &emulator->data.data[start_page * 4], tx_bytes);
  1373. if(emulator->supported_features &
  1374. MfUltralightSupportAsciiMirror &&
  1375. emulator->config_cache.mirror.mirror_conf !=
  1376. MfUltralightMirrorNone) {
  1377. // Copy ASCII mirror
  1378. // Less stringent check here, because expecting FAST_READ to
  1379. // only be issued once rather than repeatedly
  1380. FuriString* ascii_mirror;
  1381. ascii_mirror = furi_string_alloc();
  1382. mf_ul_make_ascii_mirror(emulator, ascii_mirror);
  1383. size_t ascii_mirror_len =
  1384. furi_string_utf8_length(ascii_mirror);
  1385. const char* ascii_mirror_cptr =
  1386. furi_string_get_cstr(ascii_mirror);
  1387. int16_t mirror_start_offset =
  1388. (emulator->config->mirror_page - start_page) * 4 +
  1389. emulator->config->mirror.mirror_byte;
  1390. if(mirror_start_offset < 0) {
  1391. if(mirror_start_offset < -(int16_t)ascii_mirror_len) {
  1392. // Past ASCII mirror, don't copy
  1393. ascii_mirror_len = 0;
  1394. } else {
  1395. ascii_mirror_cptr += -mirror_start_offset;
  1396. ascii_mirror_len -= -mirror_start_offset;
  1397. mirror_start_offset = 0;
  1398. }
  1399. }
  1400. if(ascii_mirror_len > 0) {
  1401. int16_t mirror_end_offset =
  1402. mirror_start_offset + ascii_mirror_len;
  1403. if(mirror_end_offset > (end_page + 1) * 4) {
  1404. mirror_end_offset = (end_page + 1) * 4;
  1405. ascii_mirror_len =
  1406. mirror_end_offset - mirror_start_offset;
  1407. }
  1408. for(size_t i = 0; i < ascii_mirror_len; ++i) {
  1409. if(*ascii_mirror_cptr != ' ')
  1410. buff_tx[mirror_start_offset] =
  1411. (uint8_t)*ascii_mirror_cptr;
  1412. ++mirror_start_offset;
  1413. ++ascii_mirror_cptr;
  1414. }
  1415. }
  1416. furi_string_free(ascii_mirror);
  1417. }
  1418. if(emulator->supported_features & MfUltralightSupportAuth) {
  1419. // Clear PWD and PACK pages
  1420. uint8_t pwd_page = emulator->page_num - 2;
  1421. int16_t pwd_page_offset = pwd_page - start_page;
  1422. // PWD page
  1423. if(pwd_page_offset >= 0 && pwd_page <= end_page) {
  1424. memset(&buff_tx[pwd_page_offset * 4], 0, 4);
  1425. // PACK page
  1426. if(pwd_page + 1 <= end_page)
  1427. memset(&buff_tx[(pwd_page_offset + 1) * 4], 0, 4);
  1428. }
  1429. }
  1430. *data_type = FURI_HAL_NFC_TXRX_DEFAULT;
  1431. command_parsed = true;
  1432. } while(false);
  1433. }
  1434. } else {
  1435. uint16_t valid_pages;
  1436. start_page = mf_ultralight_ntag_i2c_addr_tag_to_lin(
  1437. &emulator->data, start_page, emulator->curr_sector, &valid_pages);
  1438. if(start_page != -1) {
  1439. if(emulator->data.type < MfUltralightTypeNTAGI2CPlus1K ||
  1440. mf_ul_ntag_i2c_plus_check_auth(emulator, buff_rx[1], false)) {
  1441. uint16_t copy_count = tx_bytes;
  1442. if(copy_count > valid_pages * 4) copy_count = valid_pages * 4;
  1443. memcpy(
  1444. buff_tx, &emulator->data.data[start_page * 4], copy_count);
  1445. if(copy_count < tx_bytes)
  1446. memset(&buff_tx[copy_count], 0, tx_bytes - copy_count);
  1447. mf_ul_ntag_i2c_fill_cross_area_read(
  1448. buff_tx, buff_rx[1], buff_rx[2], emulator);
  1449. mf_ul_protect_auth_data_on_read_command_i2c(
  1450. buff_tx,
  1451. start_page,
  1452. start_page + copy_count / 4 - 1,
  1453. emulator);
  1454. *data_type = FURI_HAL_NFC_TXRX_DEFAULT;
  1455. command_parsed = true;
  1456. }
  1457. }
  1458. }
  1459. if(!command_parsed) tx_bytes = 0;
  1460. }
  1461. }
  1462. }
  1463. } else if(cmd == MF_UL_WRITE) {
  1464. if(buff_rx_len == (1 + 5) * 8) {
  1465. do {
  1466. uint8_t orig_write_page = buff_rx[1];
  1467. int16_t write_page = orig_write_page;
  1468. uint16_t valid_pages; // unused
  1469. write_page = mf_ultralight_ntag_i2c_addr_tag_to_lin(
  1470. &emulator->data, write_page, emulator->curr_sector, &valid_pages);
  1471. if(write_page == -1) // NTAG I2C range check
  1472. break;
  1473. else if(write_page < 2 || write_page >= emulator->page_num) // Other MFUL/NTAG range check
  1474. break;
  1475. if(emulator->supported_features & MfUltralightSupportAuth) {
  1476. if(emulator->data.type >= MfUltralightTypeNTAGI2CPlus1K) {
  1477. if(!mf_ul_ntag_i2c_plus_check_auth(emulator, orig_write_page, true))
  1478. break;
  1479. } else {
  1480. if(!mf_ul_check_auth(emulator, orig_write_page, true)) break;
  1481. }
  1482. }
  1483. int16_t tag_addr = mf_ultralight_page_addr_to_tag_addr(
  1484. emulator->curr_sector, orig_write_page);
  1485. if(!mf_ul_check_lock(emulator, tag_addr)) break;
  1486. if(emulator->data.type == MfUltralightTypeNTAG203 &&
  1487. orig_write_page == MF_UL_NTAG203_COUNTER_PAGE) {
  1488. send_ack = mf_ul_emulate_ntag203_counter_write(emulator, &buff_rx[2]);
  1489. command_parsed = send_ack;
  1490. } else {
  1491. mf_ul_emulate_write(emulator, tag_addr, write_page, &buff_rx[2]);
  1492. send_ack = true;
  1493. command_parsed = true;
  1494. }
  1495. } while(false);
  1496. }
  1497. } else if(cmd == MF_UL_FAST_WRITE) {
  1498. if(emulator->supported_features & MfUltralightSupportFastWrite) {
  1499. if(buff_rx_len == (1 + 66) * 8) {
  1500. if(buff_rx[1] == 0xF0 && buff_rx[2] == 0xFF) {
  1501. // TODO: update when SRAM emulation implemented
  1502. send_ack = true;
  1503. command_parsed = true;
  1504. }
  1505. }
  1506. }
  1507. } else if(cmd == MF_UL_COMP_WRITE) {
  1508. if(emulator->supported_features & MfUltralightSupportCompatWrite) {
  1509. if(buff_rx_len == (1 + 1) * 8) {
  1510. uint8_t write_page = buff_rx[1];
  1511. do {
  1512. if(write_page < 2 || write_page >= emulator->page_num) break;
  1513. if(emulator->supported_features & MfUltralightSupportAuth &&
  1514. !mf_ul_check_auth(emulator, write_page, true))
  1515. break;
  1516. // Note we don't convert to tag addr here because there's only one sector
  1517. if(!mf_ul_check_lock(emulator, write_page)) break;
  1518. emulator->comp_write_cmd_started = true;
  1519. emulator->comp_write_page_addr = write_page;
  1520. send_ack = true;
  1521. command_parsed = true;
  1522. } while(false);
  1523. }
  1524. }
  1525. } else if(cmd == MF_UL_READ_CNT) {
  1526. if(emulator->supported_features & MfUltralightSupportReadCounter) {
  1527. if(buff_rx_len == (1 + 1) * 8) {
  1528. do {
  1529. uint8_t cnt_num = buff_rx[1];
  1530. // NTAG21x checks
  1531. if(emulator->supported_features & MfUltralightSupportSingleCounter) {
  1532. if(cnt_num != 2) break; // Only counter 2 is available
  1533. if(!emulator->config_cache.access.nfc_cnt_en)
  1534. break; // NAK if counter not enabled
  1535. if(emulator->config_cache.access.nfc_cnt_pwd_prot &&
  1536. !emulator->auth_success)
  1537. break;
  1538. }
  1539. if(cnt_num < 3) {
  1540. buff_tx[0] = emulator->data.counter[cnt_num] & 0xFF;
  1541. buff_tx[1] = (emulator->data.counter[cnt_num] >> 8) & 0xFF;
  1542. buff_tx[2] = (emulator->data.counter[cnt_num] >> 16) & 0xFF;
  1543. tx_bytes = 3;
  1544. *data_type = FURI_HAL_NFC_TXRX_DEFAULT;
  1545. command_parsed = true;
  1546. }
  1547. } while(false);
  1548. }
  1549. }
  1550. } else if(cmd == MF_UL_INC_CNT) {
  1551. if(emulator->supported_features & MfUltralightSupportIncrCounter) {
  1552. if(buff_rx_len == (1 + 5) * 8) {
  1553. uint8_t cnt_num = buff_rx[1];
  1554. uint32_t inc = (buff_rx[2] | (buff_rx[3] << 8) | (buff_rx[4] << 16));
  1555. // TODO: can you increment by 0 when counter is at 0xffffff?
  1556. if((cnt_num < 3) && (emulator->data.counter[cnt_num] != 0x00FFFFFF) &&
  1557. (emulator->data.counter[cnt_num] + inc <= 0x00FFFFFF)) {
  1558. emulator->data.counter[cnt_num] += inc;
  1559. // We're RAM-backed, so tearing never happens
  1560. emulator->data.tearing[cnt_num] = MF_UL_TEARING_FLAG_DEFAULT;
  1561. emulator->data_changed = true;
  1562. send_ack = true;
  1563. command_parsed = true;
  1564. }
  1565. }
  1566. }
  1567. } else if(cmd == MF_UL_AUTH) {
  1568. if(emulator->supported_features & MfUltralightSupportAuth) {
  1569. if(buff_rx_len == (1 + 4) * 8) {
  1570. uint16_t scaled_authlim = mf_ultralight_calc_auth_count(&emulator->data);
  1571. if(scaled_authlim != 0 && emulator->data.curr_authlim >= scaled_authlim) {
  1572. if(emulator->data.curr_authlim != UINT16_MAX) {
  1573. // Handle case where AUTHLIM has been lowered or changed from 0
  1574. emulator->data.curr_authlim = UINT16_MAX;
  1575. emulator->data_changed = true;
  1576. }
  1577. // AUTHLIM reached, always fail
  1578. buff_tx[0] = MF_UL_NAK_AUTHLIM_REACHED;
  1579. tx_bits = 4;
  1580. *data_type = FURI_HAL_NFC_TX_RAW_RX_DEFAULT;
  1581. mf_ul_reset_emulation(emulator, false);
  1582. command_parsed = true;
  1583. } else {
  1584. if(memcmp(&buff_rx[1], emulator->config->auth_data.pwd.raw, 4) == 0) {
  1585. // Correct password
  1586. buff_tx[0] = emulator->config->auth_data.pack.raw[0];
  1587. buff_tx[1] = emulator->config->auth_data.pack.raw[1];
  1588. tx_bytes = 2;
  1589. *data_type = FURI_HAL_NFC_TXRX_DEFAULT;
  1590. emulator->auth_success = true;
  1591. command_parsed = true;
  1592. if(emulator->data.curr_authlim != 0) {
  1593. // Reset current AUTHLIM
  1594. emulator->data.curr_authlim = 0;
  1595. emulator->data_changed = true;
  1596. }
  1597. } else if(!emulator->config->auth_data.pwd.value) {
  1598. // Unknown password, pretend to be an Amiibo
  1599. buff_tx[0] = 0x80;
  1600. buff_tx[1] = 0x80;
  1601. tx_bytes = 2;
  1602. *data_type = FURI_HAL_NFC_TXRX_DEFAULT;
  1603. emulator->auth_success = true;
  1604. command_parsed = true;
  1605. } else {
  1606. // Wrong password, increase negative verification count
  1607. if(emulator->data.curr_authlim < UINT16_MAX) {
  1608. ++emulator->data.curr_authlim;
  1609. emulator->data_changed = true;
  1610. }
  1611. if(scaled_authlim != 0 &&
  1612. emulator->data.curr_authlim >= scaled_authlim) {
  1613. emulator->data.curr_authlim = UINT16_MAX;
  1614. buff_tx[0] = MF_UL_NAK_AUTHLIM_REACHED;
  1615. tx_bits = 4;
  1616. *data_type = FURI_HAL_NFC_TX_RAW_RX_DEFAULT;
  1617. mf_ul_reset_emulation(emulator, false);
  1618. command_parsed = true;
  1619. } else {
  1620. // Should delay here to slow brute forcing
  1621. }
  1622. }
  1623. }
  1624. }
  1625. }
  1626. } else if(cmd == MF_UL_READ_SIG) {
  1627. if(emulator->supported_features & MfUltralightSupportSignature) {
  1628. // Check 2nd byte = 0x00 - RFU
  1629. if(buff_rx_len == (1 + 1) * 8 && buff_rx[1] == 0x00) {
  1630. tx_bytes = sizeof(emulator->data.signature);
  1631. memcpy(buff_tx, emulator->data.signature, tx_bytes);
  1632. *data_type = FURI_HAL_NFC_TXRX_DEFAULT;
  1633. command_parsed = true;
  1634. }
  1635. }
  1636. } else if(cmd == MF_UL_CHECK_TEARING) {
  1637. if(emulator->supported_features & MfUltralightSupportTearingFlags) {
  1638. if(buff_rx_len == (1 + 1) * 8) {
  1639. uint8_t cnt_num = buff_rx[1];
  1640. if(cnt_num < 3) {
  1641. buff_tx[0] = emulator->data.tearing[cnt_num];
  1642. tx_bytes = 1;
  1643. *data_type = FURI_HAL_NFC_TXRX_DEFAULT;
  1644. command_parsed = true;
  1645. }
  1646. }
  1647. }
  1648. } else if(cmd == MF_UL_HALT_START) {
  1649. reset_idle = true;
  1650. FURI_LOG_D(TAG, "Received HLTA");
  1651. } else if(cmd == MF_UL_SECTOR_SELECT) {
  1652. if(emulator->supported_features & MfUltralightSupportSectorSelect) {
  1653. if(buff_rx_len == (1 + 1) * 8 && buff_rx[1] == 0xFF) {
  1654. // Send ACK
  1655. emulator->sector_select_cmd_started = true;
  1656. send_ack = true;
  1657. command_parsed = true;
  1658. }
  1659. }
  1660. } else if(cmd == MF_UL_READ_VCSL) {
  1661. if(emulator->supported_features & MfUltralightSupportVcsl) {
  1662. if(buff_rx_len == (1 + 20) * 8) {
  1663. buff_tx[0] = emulator->config_cache.vctid;
  1664. tx_bytes = 1;
  1665. *data_type = FURI_HAL_NFC_TXRX_DEFAULT;
  1666. command_parsed = true;
  1667. }
  1668. }
  1669. } else {
  1670. // NTAG203 appears to NAK instead of just falling off on invalid commands
  1671. if(emulator->data.type != MfUltralightTypeNTAG203) reset_idle = true;
  1672. FURI_LOG_D(TAG, "Received invalid command");
  1673. }
  1674. } else {
  1675. reset_idle = true;
  1676. FURI_LOG_D(TAG, "Received invalid buffer less than 8 bits in length");
  1677. }
  1678. if(reset_idle) {
  1679. mf_ul_reset_emulation(emulator, false);
  1680. tx_bits = 0;
  1681. command_parsed = true;
  1682. }
  1683. if(!command_parsed) {
  1684. // Send NACK
  1685. buff_tx[0] = MF_UL_NAK_INVALID_ARGUMENT;
  1686. tx_bits = 4;
  1687. *data_type = FURI_HAL_NFC_TX_RAW_RX_DEFAULT;
  1688. // Every NAK should cause reset to IDLE
  1689. mf_ul_reset_emulation(emulator, false);
  1690. } else if(send_ack) {
  1691. buff_tx[0] = MF_UL_ACK;
  1692. tx_bits = 4;
  1693. *data_type = FURI_HAL_NFC_TX_RAW_RX_DEFAULT;
  1694. }
  1695. if(respond_nothing) {
  1696. *buff_tx_len = UINT16_MAX;
  1697. *data_type = FURI_HAL_NFC_TX_RAW_RX_DEFAULT;
  1698. } else {
  1699. // Return tx buffer size in bits
  1700. if(tx_bytes) {
  1701. tx_bits = tx_bytes * 8;
  1702. }
  1703. *buff_tx_len = tx_bits;
  1704. }
  1705. #ifdef FURI_DEBUG
  1706. if(*buff_tx_len == UINT16_MAX) {
  1707. FURI_LOG_T(TAG, "Emu TX: no reply");
  1708. } else if(*buff_tx_len > 0) {
  1709. int count = (*buff_tx_len + 7) / 8;
  1710. for(int i = 0; i < count; ++i) {
  1711. furi_string_cat_printf(debug_buf, "%02x ", buff_tx[i]);
  1712. }
  1713. furi_string_trim(debug_buf);
  1714. FURI_LOG_T(TAG, "Emu TX (%d): %s", *buff_tx_len, furi_string_get_cstr(debug_buf));
  1715. furi_string_free(debug_buf);
  1716. } else {
  1717. FURI_LOG_T(TAG, "Emu TX: HALT");
  1718. }
  1719. #endif
  1720. return tx_bits > 0;
  1721. }