mifare_ultralight.c 77 KB

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