mifare_ultralight.c 75 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. #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[1] << 8) | tx_rx->rx_data[0];
  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. bool mf_ultralight_read_tearing_flags(FuriHalNfcTxRxContext* tx_rx, MfUltralightData* data) {
  626. uint8_t flag_read = 0;
  627. FURI_LOG_D(TAG, "Reading tearing flags");
  628. for(size_t i = 0; i < 3; i++) {
  629. tx_rx->tx_data[0] = MF_UL_CHECK_TEARING;
  630. tx_rx->rx_data[1] = i;
  631. tx_rx->tx_bits = 16;
  632. tx_rx->tx_rx_type = FuriHalNfcTxRxTypeDefault;
  633. if(!furi_hal_nfc_tx_rx(tx_rx, 50)) {
  634. FURI_LOG_D(TAG, "Failed to read %d tearing flag", i);
  635. break;
  636. }
  637. data->tearing[i] = tx_rx->rx_data[0];
  638. flag_read++;
  639. }
  640. return flag_read == 2;
  641. }
  642. bool mf_ul_read_card(
  643. FuriHalNfcTxRxContext* tx_rx,
  644. MfUltralightReader* reader,
  645. MfUltralightData* data) {
  646. furi_assert(tx_rx);
  647. furi_assert(reader);
  648. furi_assert(data);
  649. bool card_read = false;
  650. // Read Mifare Ultralight version
  651. if(mf_ultralight_read_version(tx_rx, reader, data)) {
  652. if(reader->supported_features & MfUltralightSupportSignature) {
  653. // Read Signature
  654. mf_ultralight_read_signature(tx_rx, data);
  655. }
  656. } else {
  657. // No GET_VERSION command, check for NTAG203 by reading last page (41)
  658. uint8_t dummy[16];
  659. if(mf_ultralight_read_pages_direct(tx_rx, 41, dummy)) {
  660. mf_ul_set_version_ntag203(reader, data);
  661. reader->supported_features = mf_ul_get_features(data->type);
  662. } else {
  663. // We're really an original Mifare Ultralight, reset tag for safety
  664. furi_hal_nfc_sleep();
  665. furi_hal_nfc_activate_nfca(300, NULL);
  666. }
  667. }
  668. card_read = mf_ultralight_read_pages(tx_rx, reader, data);
  669. if(card_read) {
  670. if(reader->supported_features & MfUltralightSupportReadCounter &&
  671. mf_ultralight_should_read_counters(data)) {
  672. mf_ultralight_read_counters(tx_rx, data);
  673. }
  674. if(reader->supported_features & MfUltralightSupportTearingFlags) {
  675. mf_ultralight_read_tearing_flags(tx_rx, data);
  676. }
  677. data->curr_authlim = 0;
  678. }
  679. return card_read;
  680. }
  681. static void mf_ul_protect_auth_data_on_read_command_i2c(
  682. uint8_t* tx_buff,
  683. uint8_t start_page,
  684. uint8_t end_page,
  685. MfUltralightEmulator* emulator) {
  686. if(emulator->data.type >= MfUltralightTypeNTAGI2CPlus1K) {
  687. // Blank out PWD and PACK
  688. if(start_page <= 229 && end_page >= 229) {
  689. uint16_t offset = (229 - start_page) * 4;
  690. uint8_t count = 4;
  691. if(end_page >= 230) count += 2;
  692. memset(&tx_buff[offset], 0, count);
  693. }
  694. // Handle AUTH0 for sector 0
  695. if(!emulator->auth_success) {
  696. if(emulator->config_cache.access.prot) {
  697. uint8_t auth0 = emulator->config_cache.auth0;
  698. if(auth0 < end_page) {
  699. // start_page is always < auth0; otherwise is NAK'd already
  700. uint8_t page_offset = auth0 - start_page;
  701. uint8_t page_count = end_page - auth0;
  702. memset(&tx_buff[page_offset * 4], 0, page_count * 4);
  703. }
  704. }
  705. }
  706. }
  707. }
  708. static void mf_ul_ntag_i2c_fill_cross_area_read(
  709. uint8_t* tx_buff,
  710. uint8_t start_page,
  711. uint8_t end_page,
  712. MfUltralightEmulator* emulator) {
  713. // For copying config or session registers in fast read
  714. int16_t tx_page_offset;
  715. int16_t data_page_offset;
  716. uint8_t page_length;
  717. bool apply = false;
  718. MfUltralightType type = emulator->data.type;
  719. if(emulator->curr_sector == 0) {
  720. if(type == MfUltralightTypeNTAGI2C1K) {
  721. if(start_page <= 233 && end_page >= 232) {
  722. tx_page_offset = start_page - 232;
  723. data_page_offset = 227;
  724. page_length = 2;
  725. apply = true;
  726. }
  727. } else if(type == MfUltralightTypeNTAGI2CPlus1K || type == MfUltralightTypeNTAGI2CPlus2K) {
  728. if(start_page <= 237 && end_page >= 236) {
  729. tx_page_offset = start_page - 236;
  730. data_page_offset = 234;
  731. page_length = 2;
  732. apply = true;
  733. }
  734. }
  735. } else if(emulator->curr_sector == 1) {
  736. if(type == MfUltralightTypeNTAGI2C2K) {
  737. if(start_page <= 233 && end_page >= 232) {
  738. tx_page_offset = start_page - 232;
  739. data_page_offset = 483;
  740. page_length = 2;
  741. apply = true;
  742. }
  743. }
  744. }
  745. if(apply) {
  746. while(tx_page_offset < 0 && page_length > 0) {
  747. ++tx_page_offset;
  748. ++data_page_offset;
  749. --page_length;
  750. }
  751. memcpy(
  752. &tx_buff[tx_page_offset * 4],
  753. &emulator->data.data[data_page_offset * 4],
  754. page_length * 4);
  755. }
  756. }
  757. static bool mf_ul_check_auth(MfUltralightEmulator* emulator, uint8_t start_page, bool is_write) {
  758. if(!emulator->auth_success) {
  759. if(start_page >= emulator->config_cache.auth0 &&
  760. (emulator->config_cache.access.prot || is_write))
  761. return false;
  762. }
  763. if(is_write && emulator->config_cache.access.cfglck) {
  764. uint16_t config_start_page = emulator->page_num - 4;
  765. if(start_page == config_start_page || start_page == config_start_page + 1) return false;
  766. }
  767. return true;
  768. }
  769. static bool mf_ul_ntag_i2c_plus_check_auth(
  770. MfUltralightEmulator* emulator,
  771. uint8_t start_page,
  772. bool is_write) {
  773. if(!emulator->auth_success) {
  774. // Check NFC_PROT
  775. if(emulator->curr_sector == 0 && (emulator->config_cache.access.prot || is_write)) {
  776. if(start_page >= emulator->config_cache.auth0) return false;
  777. } else if(emulator->curr_sector == 1) {
  778. // We don't have to specifically check for type because this is done
  779. // by address translator
  780. uint8_t pt_i2c = emulator->data.data[231 * 4];
  781. // Check 2K_PROT
  782. if(pt_i2c & 0x08) return false;
  783. }
  784. }
  785. if(emulator->curr_sector == 1) {
  786. // Check NFC_DIS_SEC1
  787. if(emulator->config_cache.access.nfc_dis_sec1) return false;
  788. }
  789. return true;
  790. }
  791. static int16_t mf_ul_get_dynamic_lock_page_addr(MfUltralightData* data) {
  792. switch(data->type) {
  793. case MfUltralightTypeNTAG203:
  794. return 0x28;
  795. case MfUltralightTypeUL21:
  796. case MfUltralightTypeNTAG213:
  797. case MfUltralightTypeNTAG215:
  798. case MfUltralightTypeNTAG216:
  799. return data->data_size / 4 - 5;
  800. case MfUltralightTypeNTAGI2C1K:
  801. case MfUltralightTypeNTAGI2CPlus1K:
  802. case MfUltralightTypeNTAGI2CPlus2K:
  803. return 0xe2;
  804. case MfUltralightTypeNTAGI2C2K:
  805. return 0x1e0;
  806. default:
  807. return -1; // No dynamic lock bytes
  808. }
  809. }
  810. // Returns true if page not locked
  811. // write_page is tag address
  812. static bool mf_ul_check_lock(MfUltralightEmulator* emulator, int16_t write_page) {
  813. if(write_page < 2) return false; // Page 0-1 is always locked
  814. if(write_page == 2) return true; // Page 2 does not have a lock flag
  815. // Check static lock bytes
  816. if(write_page <= 15) {
  817. uint16_t static_lock_bytes = emulator->data.data[10] | (emulator->data.data[11] << 8);
  818. return (static_lock_bytes & (1 << write_page)) == 0;
  819. }
  820. // Check dynamic lock bytes
  821. // Check max page
  822. switch(emulator->data.type) {
  823. case MfUltralightTypeNTAG203:
  824. // Counter page can be locked and is after dynamic locks
  825. if(write_page == 40) return true;
  826. break;
  827. case MfUltralightTypeUL21:
  828. case MfUltralightTypeNTAG213:
  829. case MfUltralightTypeNTAG215:
  830. case MfUltralightTypeNTAG216:
  831. if(write_page >= emulator->page_num - 5) return true;
  832. break;
  833. case MfUltralightTypeNTAGI2C1K:
  834. case MfUltralightTypeNTAGI2CPlus1K:
  835. if(write_page > 225) return true;
  836. break;
  837. case MfUltralightTypeNTAGI2C2K:
  838. if(write_page > 479) return true;
  839. break;
  840. case MfUltralightTypeNTAGI2CPlus2K:
  841. if(write_page >= 226 && write_page <= 255) return true;
  842. if(write_page >= 512) return true;
  843. break;
  844. default:
  845. furi_crash("Unknown MFUL");
  846. return true;
  847. }
  848. int16_t dynamic_lock_index = mf_ul_get_dynamic_lock_page_addr(&emulator->data);
  849. if(dynamic_lock_index == -1) return true;
  850. // Run address through converter because NTAG I2C 2K is special
  851. uint16_t valid_pages; // unused
  852. dynamic_lock_index =
  853. mf_ultralight_ntag_i2c_addr_tag_to_lin(
  854. &emulator->data, dynamic_lock_index & 0xff, dynamic_lock_index >> 8, &valid_pages) *
  855. 4;
  856. uint16_t dynamic_lock_bytes = emulator->data.data[dynamic_lock_index] |
  857. (emulator->data.data[dynamic_lock_index + 1] << 8);
  858. uint8_t shift;
  859. switch(emulator->data.type) {
  860. // low byte LSB range, MSB range
  861. case MfUltralightTypeNTAG203:
  862. if(write_page >= 16 && write_page <= 27)
  863. shift = (write_page - 16) / 4 + 1;
  864. else if(write_page >= 28 && write_page <= 39)
  865. shift = (write_page - 28) / 4 + 5;
  866. else if(write_page == 41)
  867. shift = 12;
  868. else {
  869. furi_crash("Unknown MFUL");
  870. }
  871. break;
  872. case MfUltralightTypeUL21:
  873. case MfUltralightTypeNTAG213:
  874. // 16-17, 30-31
  875. shift = (write_page - 16) / 2;
  876. break;
  877. case MfUltralightTypeNTAG215:
  878. case MfUltralightTypeNTAG216:
  879. case MfUltralightTypeNTAGI2C1K:
  880. case MfUltralightTypeNTAGI2CPlus1K:
  881. // 16-31, 128-129
  882. // 16-31, 128-143
  883. shift = (write_page - 16) / 16;
  884. break;
  885. case MfUltralightTypeNTAGI2C2K:
  886. // 16-47, 240-271
  887. shift = (write_page - 16) / 32;
  888. break;
  889. case MfUltralightTypeNTAGI2CPlus2K:
  890. // 16-47, 256-271
  891. if(write_page >= 208 && write_page <= 225)
  892. shift = 6;
  893. else if(write_page >= 256 && write_page <= 271)
  894. shift = 7;
  895. else
  896. shift = (write_page - 16) / 32;
  897. break;
  898. default:
  899. furi_crash("Unknown MFUL");
  900. break;
  901. }
  902. return (dynamic_lock_bytes & (1 << shift)) == 0;
  903. }
  904. static void mf_ul_make_ascii_mirror(MfUltralightEmulator* emulator, string_t str) {
  905. // Locals to improve readability
  906. uint8_t mirror_page = emulator->config->mirror_page;
  907. uint8_t mirror_byte = emulator->config->mirror.mirror_byte;
  908. MfUltralightMirrorConf mirror_conf = emulator->config_cache.mirror.mirror_conf;
  909. uint16_t last_user_page_index = emulator->page_num - 6;
  910. bool uid_printed = false;
  911. if(mirror_conf == MfUltralightMirrorUid || mirror_conf == MfUltralightMirrorUidCounter) {
  912. // UID range check
  913. if(mirror_page < 4 || mirror_page > last_user_page_index - 3 ||
  914. (mirror_page == last_user_page_index - 3 && mirror_byte > 2)) {
  915. if(mirror_conf == MfUltralightMirrorUid) return;
  916. // NTAG21x has the peculiar behavior when UID+counter selected, if UID does not fit but
  917. // counter will fit, it will actually mirror the counter
  918. string_cat_str(str, " ");
  919. } else {
  920. for(int i = 0; i < 3; ++i) {
  921. string_cat_printf(str, "%02X", emulator->data.data[i]);
  922. }
  923. // Skip BCC0
  924. for(int i = 4; i < 8; ++i) {
  925. string_cat_printf(str, "%02X", emulator->data.data[i]);
  926. }
  927. uid_printed = true;
  928. }
  929. uint16_t next_byte_offset = mirror_page * 4 + mirror_byte + 14;
  930. if(mirror_conf == MfUltralightMirrorUidCounter) ++next_byte_offset;
  931. mirror_page = next_byte_offset / 4;
  932. mirror_byte = next_byte_offset % 4;
  933. }
  934. if(mirror_conf == MfUltralightMirrorCounter || mirror_conf == MfUltralightMirrorUidCounter) {
  935. // Counter is only printed if counter enabled
  936. if(emulator->config_cache.access.nfc_cnt_en) {
  937. // Counter protection check
  938. if(emulator->config_cache.access.nfc_cnt_pwd_prot && !emulator->auth_success) return;
  939. // Counter range check
  940. if(mirror_page < 4) return;
  941. if(mirror_page > last_user_page_index - 1) return;
  942. if(mirror_page == last_user_page_index - 1 && mirror_byte > 2) return;
  943. if(mirror_conf == MfUltralightMirrorUidCounter)
  944. string_cat_str(str, uid_printed ? "x" : " ");
  945. string_cat_printf(str, "%06X", emulator->data.counter[2]);
  946. }
  947. }
  948. }
  949. static void mf_ul_increment_single_counter(MfUltralightEmulator* emulator) {
  950. if(!emulator->read_counter_incremented && emulator->config_cache.access.nfc_cnt_en) {
  951. if(emulator->data.counter[2] < 0xFFFFFF) {
  952. ++emulator->data.counter[2];
  953. emulator->data_changed = true;
  954. }
  955. emulator->read_counter_incremented = true;
  956. }
  957. }
  958. static bool
  959. mf_ul_emulate_ntag203_counter_write(MfUltralightEmulator* emulator, uint8_t* page_buff) {
  960. // We'll reuse the existing counters for other NTAGs as staging
  961. // Counter 0 stores original value, data is new value
  962. uint32_t counter_value;
  963. if(emulator->data.tearing[0] == MF_UL_TEARING_FLAG_DEFAULT) {
  964. counter_value = emulator->data.data[MF_UL_NTAG203_COUNTER_PAGE * 4] |
  965. (emulator->data.data[MF_UL_NTAG203_COUNTER_PAGE * 4 + 1] << 8);
  966. } else {
  967. // We've had a reset here, so load from original value
  968. counter_value = emulator->data.counter[0];
  969. }
  970. // Although the datasheet says increment by 0 is always possible, this is not the case on
  971. // an actual tag. If the counter is at 0xFFFF, any writes are locked out.
  972. if(counter_value == 0xFFFF) return false;
  973. uint32_t increment = page_buff[0] | (page_buff[1] << 8);
  974. if(counter_value == 0) {
  975. counter_value = increment;
  976. } else {
  977. // Per datasheet specifying > 0x000F is supposed to NAK, but actual tag doesn't
  978. increment &= 0x000F;
  979. if(counter_value + increment > 0xFFFF) return false;
  980. counter_value += increment;
  981. }
  982. // Commit to new value counter
  983. emulator->data.data[MF_UL_NTAG203_COUNTER_PAGE * 4] = (uint8_t)counter_value;
  984. emulator->data.data[MF_UL_NTAG203_COUNTER_PAGE * 4 + 1] = (uint8_t)(counter_value >> 8);
  985. emulator->data.tearing[0] = MF_UL_TEARING_FLAG_DEFAULT;
  986. if(counter_value == 0xFFFF) {
  987. // Tag will lock out counter if final number is 0xFFFF, even if you try to roll it back
  988. emulator->data.counter[1] = 0xFFFF;
  989. }
  990. emulator->data_changed = true;
  991. return true;
  992. }
  993. static void mf_ul_emulate_write(
  994. MfUltralightEmulator* emulator,
  995. int16_t tag_addr,
  996. int16_t write_page,
  997. uint8_t* page_buff) {
  998. // Assumption: all access checks have been completed
  999. if(tag_addr == 2) {
  1000. // Handle static locks
  1001. uint16_t orig_static_locks = emulator->data.data[write_page * 4 + 2] |
  1002. (emulator->data.data[write_page * 4 + 3] << 8);
  1003. uint16_t new_static_locks = page_buff[2] | (page_buff[3] << 8);
  1004. if(orig_static_locks & 1) new_static_locks &= ~0x08;
  1005. if(orig_static_locks & 2) new_static_locks &= ~0xF0;
  1006. if(orig_static_locks & 4) new_static_locks &= 0xFF;
  1007. new_static_locks |= orig_static_locks;
  1008. page_buff[0] = emulator->data.data[write_page * 4];
  1009. page_buff[1] = emulator->data.data[write_page * 4 + 1];
  1010. page_buff[2] = new_static_locks & 0xff;
  1011. page_buff[3] = new_static_locks >> 8;
  1012. } else if(tag_addr == 3) {
  1013. // Handle OTP/capability container
  1014. *(uint32_t*)page_buff |= *(uint32_t*)&emulator->data.data[write_page * 4];
  1015. } else if(tag_addr == mf_ul_get_dynamic_lock_page_addr(&emulator->data)) {
  1016. // Handle dynamic locks
  1017. if(emulator->data.type == MfUltralightTypeNTAG203) {
  1018. // NTAG203 lock bytes are a bit different from the others
  1019. uint8_t orig_page_lock_byte = emulator->data.data[write_page * 4];
  1020. uint8_t orig_cnt_lock_byte = emulator->data.data[write_page * 4 + 1];
  1021. uint8_t new_page_lock_byte = page_buff[0];
  1022. uint8_t new_cnt_lock_byte = page_buff[1];
  1023. if(orig_page_lock_byte & 0x01) // Block lock bits 1-3
  1024. new_page_lock_byte &= ~0x0E;
  1025. if(orig_page_lock_byte & 0x10) // Block lock bits 5-7
  1026. new_page_lock_byte &= ~0xE0;
  1027. for(uint8_t i = 0; i < 4; ++i) {
  1028. if(orig_cnt_lock_byte & (1 << i)) // Block lock counter bit
  1029. new_cnt_lock_byte &= ~(1 << (4 + i));
  1030. }
  1031. new_page_lock_byte |= orig_page_lock_byte;
  1032. new_cnt_lock_byte |= orig_cnt_lock_byte;
  1033. page_buff[0] = new_page_lock_byte;
  1034. page_buff[1] = new_cnt_lock_byte;
  1035. } else {
  1036. uint16_t orig_locks = emulator->data.data[write_page * 4] |
  1037. (emulator->data.data[write_page * 4 + 1] << 8);
  1038. uint8_t orig_block_locks = emulator->data.data[write_page * 4 + 2];
  1039. uint16_t new_locks = page_buff[0] | (page_buff[1] << 8);
  1040. uint8_t new_block_locks = page_buff[2];
  1041. int block_lock_count;
  1042. switch(emulator->data.type) {
  1043. case MfUltralightTypeUL21:
  1044. block_lock_count = 5;
  1045. break;
  1046. case MfUltralightTypeNTAG213:
  1047. block_lock_count = 6;
  1048. break;
  1049. case MfUltralightTypeNTAG215:
  1050. block_lock_count = 4;
  1051. break;
  1052. case MfUltralightTypeNTAG216:
  1053. case MfUltralightTypeNTAGI2C1K:
  1054. case MfUltralightTypeNTAGI2CPlus1K:
  1055. block_lock_count = 7;
  1056. break;
  1057. case MfUltralightTypeNTAGI2C2K:
  1058. case MfUltralightTypeNTAGI2CPlus2K:
  1059. block_lock_count = 8;
  1060. break;
  1061. default:
  1062. furi_crash("Unknown MFUL");
  1063. break;
  1064. }
  1065. for(int i = 0; i < block_lock_count; ++i) {
  1066. if(orig_block_locks & (1 << i)) new_locks &= ~(3 << (2 * i));
  1067. }
  1068. new_locks |= orig_locks;
  1069. new_block_locks |= orig_block_locks;
  1070. page_buff[0] = new_locks & 0xff;
  1071. page_buff[1] = new_locks >> 8;
  1072. page_buff[2] = new_block_locks;
  1073. if(emulator->data.type >= MfUltralightTypeUL21 &&
  1074. emulator->data.type <= MfUltralightTypeNTAG216)
  1075. page_buff[3] = MF_UL_TEARING_FLAG_DEFAULT;
  1076. else
  1077. page_buff[3] = 0;
  1078. }
  1079. }
  1080. memcpy(&emulator->data.data[write_page * 4], page_buff, 4);
  1081. emulator->data_changed = true;
  1082. }
  1083. void mf_ul_reset_emulation(MfUltralightEmulator* emulator, bool is_power_cycle) {
  1084. emulator->curr_sector = 0;
  1085. emulator->ntag_i2c_plus_sector3_lockout = false;
  1086. emulator->auth_success = false;
  1087. if(is_power_cycle) {
  1088. if(emulator->config != NULL) emulator->config_cache = *emulator->config;
  1089. if(emulator->supported_features & MfUltralightSupportSingleCounter) {
  1090. emulator->read_counter_incremented = false;
  1091. }
  1092. if(emulator->data.type == MfUltralightTypeNTAG203) {
  1093. // Apply lockout if counter ever reached 0xFFFF
  1094. if(emulator->data.counter[1] == 0xFFFF) {
  1095. emulator->data.data[MF_UL_NTAG203_COUNTER_PAGE * 4] = 0xFF;
  1096. emulator->data.data[MF_UL_NTAG203_COUNTER_PAGE * 4 + 1] = 0xFF;
  1097. }
  1098. // Copy original counter value from data
  1099. emulator->data.counter[0] =
  1100. emulator->data.data[MF_UL_NTAG203_COUNTER_PAGE * 4] |
  1101. (emulator->data.data[MF_UL_NTAG203_COUNTER_PAGE * 4 + 1] << 8);
  1102. }
  1103. } else {
  1104. if(emulator->config != NULL) {
  1105. // ACCESS (less CFGLCK) and AUTH0 are updated when reactivated
  1106. // MIRROR_CONF is not; don't know about STRG_MOD_EN, but we're not using that anyway
  1107. emulator->config_cache.access.value = (emulator->config->access.value & 0xBF) |
  1108. (emulator->config_cache.access.value & 0x40);
  1109. emulator->config_cache.auth0 = emulator->config->auth0;
  1110. }
  1111. }
  1112. if(emulator->data.type == MfUltralightTypeNTAG203) {
  1113. // Mark counter as dirty
  1114. emulator->data.tearing[0] = 0;
  1115. }
  1116. }
  1117. void mf_ul_prepare_emulation(MfUltralightEmulator* emulator, MfUltralightData* data) {
  1118. FURI_LOG_D(TAG, "Prepare emulation");
  1119. emulator->data = *data;
  1120. emulator->supported_features = mf_ul_get_features(data->type);
  1121. emulator->config = mf_ultralight_get_config_pages(&emulator->data);
  1122. emulator->page_num = emulator->data.data_size / 4;
  1123. emulator->data_changed = false;
  1124. emulator->comp_write_cmd_started = false;
  1125. emulator->sector_select_cmd_started = false;
  1126. mf_ul_reset_emulation(emulator, true);
  1127. }
  1128. bool mf_ul_prepare_emulation_response(
  1129. uint8_t* buff_rx,
  1130. uint16_t buff_rx_len,
  1131. uint8_t* buff_tx,
  1132. uint16_t* buff_tx_len,
  1133. uint32_t* data_type,
  1134. void* context) {
  1135. furi_assert(context);
  1136. MfUltralightEmulator* emulator = context;
  1137. uint16_t tx_bytes = 0;
  1138. uint16_t tx_bits = 0;
  1139. bool command_parsed = false;
  1140. bool send_ack = false;
  1141. bool respond_nothing = false;
  1142. bool reset_idle = false;
  1143. #ifdef FURI_DEBUG
  1144. string_t debug_buf;
  1145. string_init(debug_buf);
  1146. for(int i = 0; i < (buff_rx_len + 7) / 8; ++i) {
  1147. string_cat_printf(debug_buf, "%02x ", buff_rx[i]);
  1148. }
  1149. string_strim(debug_buf);
  1150. FURI_LOG_T(TAG, "Emu RX (%d): %s", buff_rx_len, string_get_cstr(debug_buf));
  1151. string_reset(debug_buf);
  1152. #endif
  1153. // Check composite commands
  1154. if(emulator->comp_write_cmd_started) {
  1155. if(buff_rx_len == 16 * 8) {
  1156. if(emulator->data.type == MfUltralightTypeNTAG203 &&
  1157. emulator->comp_write_page_addr == MF_UL_NTAG203_COUNTER_PAGE) {
  1158. send_ack = mf_ul_emulate_ntag203_counter_write(emulator, buff_rx);
  1159. command_parsed = send_ack;
  1160. } else {
  1161. mf_ul_emulate_write(
  1162. emulator,
  1163. emulator->comp_write_page_addr,
  1164. emulator->comp_write_page_addr,
  1165. buff_rx);
  1166. send_ack = true;
  1167. command_parsed = true;
  1168. }
  1169. }
  1170. emulator->comp_write_cmd_started = false;
  1171. } else if(emulator->sector_select_cmd_started) {
  1172. if(buff_rx_len == 4 * 8) {
  1173. if(buff_rx[0] <= 0xFE) {
  1174. emulator->curr_sector = buff_rx[0] > 3 ? 0 : buff_rx[0];
  1175. emulator->ntag_i2c_plus_sector3_lockout = false;
  1176. command_parsed = true;
  1177. respond_nothing = true;
  1178. FURI_LOG_D(TAG, "Changing sector to %d", emulator->curr_sector);
  1179. }
  1180. }
  1181. emulator->sector_select_cmd_started = false;
  1182. } else if(buff_rx_len >= 8) {
  1183. uint8_t cmd = buff_rx[0];
  1184. if(cmd == MF_UL_GET_VERSION_CMD) {
  1185. if(emulator->data.type >= MfUltralightTypeUL11) {
  1186. if(buff_rx_len == 1 * 8) {
  1187. tx_bytes = sizeof(emulator->data.version);
  1188. memcpy(buff_tx, &emulator->data.version, tx_bytes);
  1189. *data_type = FURI_HAL_NFC_TXRX_DEFAULT;
  1190. command_parsed = true;
  1191. }
  1192. }
  1193. } else if(cmd == MF_UL_READ_CMD) {
  1194. if(buff_rx_len == (1 + 1) * 8) {
  1195. int16_t start_page = buff_rx[1];
  1196. tx_bytes = 16;
  1197. if(emulator->data.type < MfUltralightTypeNTAGI2C1K) {
  1198. if(start_page < emulator->page_num) {
  1199. do {
  1200. uint8_t copied_pages = 0;
  1201. uint8_t src_page = start_page;
  1202. uint8_t last_page_plus_one = start_page + 4;
  1203. uint8_t pwd_page = emulator->page_num - 2;
  1204. string_t ascii_mirror;
  1205. size_t ascii_mirror_len = 0;
  1206. const char* ascii_mirror_cptr = NULL;
  1207. uint8_t ascii_mirror_curr_page = 0;
  1208. uint8_t ascii_mirror_curr_byte = 0;
  1209. if(last_page_plus_one > emulator->page_num)
  1210. last_page_plus_one = emulator->page_num;
  1211. if(emulator->supported_features & MfUltralightSupportAuth) {
  1212. if(!mf_ul_check_auth(emulator, start_page, false)) break;
  1213. if(!emulator->auth_success && emulator->config_cache.access.prot &&
  1214. emulator->config_cache.auth0 < last_page_plus_one)
  1215. last_page_plus_one = emulator->config_cache.auth0;
  1216. }
  1217. if(emulator->supported_features & MfUltralightSupportSingleCounter)
  1218. mf_ul_increment_single_counter(emulator);
  1219. if(emulator->supported_features & MfUltralightSupportAsciiMirror &&
  1220. emulator->config_cache.mirror.mirror_conf !=
  1221. MfUltralightMirrorNone) {
  1222. ascii_mirror_curr_byte = emulator->config->mirror.mirror_byte;
  1223. ascii_mirror_curr_page = emulator->config->mirror_page;
  1224. // Try to avoid wasting time making mirror if we won't copy it
  1225. // Conservatively check with UID+counter mirror size
  1226. if(last_page_plus_one > ascii_mirror_curr_page &&
  1227. start_page + 3 >= ascii_mirror_curr_page &&
  1228. start_page <= ascii_mirror_curr_page + 6) {
  1229. string_init(ascii_mirror);
  1230. mf_ul_make_ascii_mirror(emulator, ascii_mirror);
  1231. ascii_mirror_len = string_length_u(ascii_mirror);
  1232. ascii_mirror_cptr = string_get_cstr(ascii_mirror);
  1233. // Move pointer to where it should be to start copying
  1234. if(ascii_mirror_len > 0 &&
  1235. ascii_mirror_curr_page < start_page &&
  1236. ascii_mirror_curr_byte != 0) {
  1237. uint8_t diff = 4 - ascii_mirror_curr_byte;
  1238. ascii_mirror_len -= diff;
  1239. ascii_mirror_cptr += diff;
  1240. ascii_mirror_curr_byte = 0;
  1241. ++ascii_mirror_curr_page;
  1242. }
  1243. while(ascii_mirror_len > 0 &&
  1244. ascii_mirror_curr_page < start_page) {
  1245. uint8_t diff = ascii_mirror_len > 4 ? 4 : ascii_mirror_len;
  1246. ascii_mirror_len -= diff;
  1247. ascii_mirror_cptr += diff;
  1248. ++ascii_mirror_curr_page;
  1249. }
  1250. }
  1251. }
  1252. uint8_t* dest_ptr = buff_tx;
  1253. while(copied_pages < 4) {
  1254. // Copy page
  1255. memcpy(dest_ptr, &emulator->data.data[src_page * 4], 4);
  1256. // Note: don't have to worry about roll-over with ASCII mirror because
  1257. // lowest valid page for it is 4, while roll-over will at best read
  1258. // pages 0-2
  1259. if(ascii_mirror_len > 0 && src_page == ascii_mirror_curr_page) {
  1260. // Copy ASCII mirror
  1261. size_t copy_len = 4 - ascii_mirror_curr_byte;
  1262. if(copy_len > ascii_mirror_len) copy_len = ascii_mirror_len;
  1263. for(size_t i = 0; i < copy_len; ++i) {
  1264. if(*ascii_mirror_cptr != ' ')
  1265. dest_ptr[ascii_mirror_curr_byte] =
  1266. (uint8_t)*ascii_mirror_cptr;
  1267. ++ascii_mirror_curr_byte;
  1268. ++ascii_mirror_cptr;
  1269. }
  1270. ascii_mirror_len -= copy_len;
  1271. // Don't care if this is inaccurate after ascii_mirror_len = 0
  1272. ascii_mirror_curr_byte = 0;
  1273. ++ascii_mirror_curr_page;
  1274. }
  1275. if(emulator->supported_features & MfUltralightSupportAuth) {
  1276. if(src_page == pwd_page || src_page == pwd_page + 1) {
  1277. // Blank out PWD and PACK pages
  1278. memset(dest_ptr, 0, 4);
  1279. }
  1280. }
  1281. dest_ptr += 4;
  1282. ++copied_pages;
  1283. ++src_page;
  1284. if(src_page >= last_page_plus_one) src_page = 0;
  1285. }
  1286. if(ascii_mirror_cptr != NULL) {
  1287. string_clear(ascii_mirror);
  1288. }
  1289. *data_type = FURI_HAL_NFC_TXRX_DEFAULT;
  1290. command_parsed = true;
  1291. } while(false);
  1292. }
  1293. } else {
  1294. uint16_t valid_pages;
  1295. start_page = mf_ultralight_ntag_i2c_addr_tag_to_lin(
  1296. &emulator->data, start_page, emulator->curr_sector, &valid_pages);
  1297. if(start_page != -1) {
  1298. if(emulator->data.type < MfUltralightTypeNTAGI2CPlus1K ||
  1299. mf_ul_ntag_i2c_plus_check_auth(emulator, buff_rx[1], false)) {
  1300. if(emulator->data.type >= MfUltralightTypeNTAGI2CPlus1K &&
  1301. emulator->curr_sector == 3 && valid_pages == 1) {
  1302. // Rewind back a sector to match behavior on a real tag
  1303. --start_page;
  1304. ++valid_pages;
  1305. }
  1306. uint16_t copy_count = (valid_pages > 4 ? 4 : valid_pages) * 4;
  1307. FURI_LOG_D(
  1308. TAG,
  1309. "NTAG I2C Emu: page valid, %02x:%02x -> %d, %d",
  1310. emulator->curr_sector,
  1311. buff_rx[1],
  1312. start_page,
  1313. valid_pages);
  1314. memcpy(buff_tx, &emulator->data.data[start_page * 4], copy_count);
  1315. // For NTAG I2C, there's no roll-over; remainder is filled by null bytes
  1316. if(copy_count < tx_bytes)
  1317. memset(&buff_tx[copy_count], 0, tx_bytes - copy_count);
  1318. // Special case: NTAG I2C Plus sector 0 page 233 read crosses into page 236
  1319. if(start_page == 233)
  1320. memcpy(
  1321. &buff_tx[12], &emulator->data.data[(start_page + 1) * 4], 4);
  1322. mf_ul_protect_auth_data_on_read_command_i2c(
  1323. buff_tx, start_page, start_page + copy_count / 4 - 1, emulator);
  1324. *data_type = FURI_HAL_NFC_TXRX_DEFAULT;
  1325. command_parsed = true;
  1326. }
  1327. } else {
  1328. FURI_LOG_D(
  1329. TAG,
  1330. "NTAG I2C Emu: page invalid, %02x:%02x",
  1331. emulator->curr_sector,
  1332. buff_rx[1]);
  1333. if(emulator->data.type >= MfUltralightTypeNTAGI2CPlus1K &&
  1334. emulator->curr_sector == 3 &&
  1335. !emulator->ntag_i2c_plus_sector3_lockout) {
  1336. // NTAG I2C Plus has a weird behavior where if you read sector 3
  1337. // at an invalid address, it responds with zeroes then locks
  1338. // the read out, while if you read the mirrored session registers,
  1339. // it returns both session registers on either pages
  1340. memset(buff_tx, 0, tx_bytes);
  1341. *data_type = FURI_HAL_NFC_TXRX_DEFAULT;
  1342. command_parsed = true;
  1343. emulator->ntag_i2c_plus_sector3_lockout = true;
  1344. }
  1345. }
  1346. }
  1347. if(!command_parsed) tx_bytes = 0;
  1348. }
  1349. } else if(cmd == MF_UL_FAST_READ_CMD) {
  1350. if(emulator->supported_features & MfUltralightSupportFastRead) {
  1351. if(buff_rx_len == (1 + 2) * 8) {
  1352. int16_t start_page = buff_rx[1];
  1353. uint8_t end_page = buff_rx[2];
  1354. if(start_page <= end_page) {
  1355. tx_bytes = ((end_page + 1) - start_page) * 4;
  1356. if(emulator->data.type < MfUltralightTypeNTAGI2C1K) {
  1357. if((start_page < emulator->page_num) &&
  1358. (end_page < emulator->page_num)) {
  1359. do {
  1360. if(emulator->supported_features & MfUltralightSupportAuth) {
  1361. // NAK if not authenticated and requested pages cross over AUTH0
  1362. if(!emulator->auth_success &&
  1363. emulator->config_cache.access.prot &&
  1364. (start_page >= emulator->config_cache.auth0 ||
  1365. end_page >= emulator->config_cache.auth0))
  1366. break;
  1367. }
  1368. if(emulator->supported_features &
  1369. MfUltralightSupportSingleCounter)
  1370. mf_ul_increment_single_counter(emulator);
  1371. // Copy requested pages
  1372. memcpy(
  1373. buff_tx, &emulator->data.data[start_page * 4], tx_bytes);
  1374. if(emulator->supported_features &
  1375. MfUltralightSupportAsciiMirror &&
  1376. emulator->config_cache.mirror.mirror_conf !=
  1377. MfUltralightMirrorNone) {
  1378. // Copy ASCII mirror
  1379. // Less stringent check here, because expecting FAST_READ to
  1380. // only be issued once rather than repeatedly
  1381. string_t ascii_mirror;
  1382. string_init(ascii_mirror);
  1383. mf_ul_make_ascii_mirror(emulator, ascii_mirror);
  1384. size_t ascii_mirror_len = string_length_u(ascii_mirror);
  1385. const char* ascii_mirror_cptr =
  1386. 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. string_clear(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. string_cat_printf(debug_buf, "%02x ", buff_tx[i]);
  1712. }
  1713. string_strim(debug_buf);
  1714. FURI_LOG_T(TAG, "Emu TX (%d): %s", *buff_tx_len, string_get_cstr(debug_buf));
  1715. string_clear(debug_buf);
  1716. } else {
  1717. FURI_LOG_T(TAG, "Emu TX: HALT");
  1718. }
  1719. #endif
  1720. return tx_bits > 0;
  1721. }