mifare_ultralight.c 75 KB

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