nice_flor_s.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. #include "nice_flor_s.h"
  2. #include "../blocks/const.h"
  3. #include "../blocks/decoder.h"
  4. #include "../blocks/encoder.h"
  5. #include "../blocks/generic.h"
  6. #include "../blocks/math.h"
  7. /*
  8. * https://phreakerclub.com/1615
  9. * https://phreakerclub.com/forum/showthread.php?t=2360
  10. * https://vrtp.ru/index.php?showtopic=27867
  11. */
  12. #define TAG "SubGhzProtocoNiceFlorS"
  13. #define NICE_ONE_COUNT_BIT 72
  14. #define NICE_ONE_NAME "Nice One"
  15. static const SubGhzBlockConst subghz_protocol_nice_flor_s_const = {
  16. .te_short = 500,
  17. .te_long = 1000,
  18. .te_delta = 300,
  19. .min_count_bit_for_found = 52,
  20. };
  21. struct SubGhzProtocolDecoderNiceFlorS {
  22. SubGhzProtocolDecoderBase base;
  23. SubGhzBlockDecoder decoder;
  24. SubGhzBlockGeneric generic;
  25. const char* nice_flor_s_rainbow_table_file_name;
  26. uint64_t data;
  27. };
  28. struct SubGhzProtocolEncoderNiceFlorS {
  29. SubGhzProtocolEncoderBase base;
  30. SubGhzProtocolBlockEncoder encoder;
  31. SubGhzBlockGeneric generic;
  32. };
  33. typedef enum {
  34. NiceFlorSDecoderStepReset = 0,
  35. NiceFlorSDecoderStepCheckHeader,
  36. NiceFlorSDecoderStepFoundHeader,
  37. NiceFlorSDecoderStepSaveDuration,
  38. NiceFlorSDecoderStepCheckDuration,
  39. } NiceFlorSDecoderStep;
  40. const SubGhzProtocolDecoder subghz_protocol_nice_flor_s_decoder = {
  41. .alloc = subghz_protocol_decoder_nice_flor_s_alloc,
  42. .free = subghz_protocol_decoder_nice_flor_s_free,
  43. .feed = subghz_protocol_decoder_nice_flor_s_feed,
  44. .reset = subghz_protocol_decoder_nice_flor_s_reset,
  45. .get_hash_data = subghz_protocol_decoder_nice_flor_s_get_hash_data,
  46. .serialize = subghz_protocol_decoder_nice_flor_s_serialize,
  47. .deserialize = subghz_protocol_decoder_nice_flor_s_deserialize,
  48. .get_string = subghz_protocol_decoder_nice_flor_s_get_string,
  49. };
  50. const SubGhzProtocolEncoder subghz_protocol_nice_flor_s_encoder = {
  51. .alloc = NULL,
  52. .free = NULL,
  53. .deserialize = NULL,
  54. .stop = NULL,
  55. .yield = NULL,
  56. };
  57. const SubGhzProtocol subghz_protocol_nice_flor_s = {
  58. .name = SUBGHZ_PROTOCOL_NICE_FLOR_S_NAME,
  59. .type = SubGhzProtocolTypeDynamic,
  60. .flag = SubGhzProtocolFlag_433 | SubGhzProtocolFlag_868 | SubGhzProtocolFlag_AM |
  61. SubGhzProtocolFlag_Decodable,
  62. .decoder = &subghz_protocol_nice_flor_s_decoder,
  63. .encoder = &subghz_protocol_nice_flor_s_encoder,
  64. };
  65. // /**
  66. // * Read bytes from rainbow table
  67. // * @param p array[10] P0-P1|P2-P3-P4-P5-P6-P7-P8-P9-P10
  68. // * @return crc
  69. // */
  70. // static uint32_t subghz_protocol_nice_one_crc(uint8_t* p) {
  71. // uint8_t crc = 0;
  72. // uint8_t crc_data = 0xff;
  73. // for(uint8_t i = 4; i < 68; i++) {
  74. // if(subghz_protocol_blocks_get_bit_array(p, i)) {
  75. // crc = crc_data ^ 1;
  76. // } else {
  77. // crc = crc_data;
  78. // }
  79. // crc_data >>= 1;
  80. // if((crc & 0x01)) {
  81. // crc_data ^= 0x97;
  82. // }
  83. // }
  84. // crc = 0;
  85. // for(uint8_t i = 0; i < 8; i++) {
  86. // crc <<= 1;
  87. // if((crc_data >> i) & 0x01) crc = crc | 1;
  88. // }
  89. // return crc;
  90. // }
  91. // /**
  92. // * Read bytes from rainbow table
  93. // * @param p array[10] P0-P1|P2-P3-P4-P5-P6-P7-XX-XX-XX
  94. // * @param num_parcel parcel number 0..15
  95. // * @param hold_bit 0 - the button was only pressed, 1 - the button was held down
  96. // */
  97. // static void subghz_protocol_nice_one_get_data(uint8_t* p, uint8_t num_parcel, uint8_t hold_bit) {
  98. // uint8_t k = 0;
  99. // uint8_t crc = 0;
  100. // p[1] = (p[1] & 0x0f) | ((0x0f ^ (p[0] & 0x0F) ^ num_parcel) << 4);
  101. // if(num_parcel < 4) {
  102. // k = 0x8f;
  103. // } else {
  104. // k = 0x80;
  105. // }
  106. // if(!hold_bit) {
  107. // hold_bit = 0;
  108. // } else {
  109. // hold_bit = 0x10;
  110. // }
  111. // k = num_parcel ^ k;
  112. // p[7] = k;
  113. // p[8] = hold_bit ^ (k << 4);
  114. // crc = subghz_protocol_nice_one_crc(p);
  115. // p[8] |= crc >> 4;
  116. // p[9] = crc << 4;
  117. // }
  118. /**
  119. * Read bytes from rainbow table
  120. * @param file_name Full path to rainbow table the file
  121. * @param address Byte address in file
  122. * @return data
  123. */
  124. static uint8_t
  125. subghz_protocol_nice_flor_s_get_byte_in_file(const char* file_name, uint32_t address) {
  126. if(!file_name) return 0;
  127. uint8_t buffer[1] = {0};
  128. if(subghz_keystore_raw_get_data(file_name, address, buffer, sizeof(uint8_t))) {
  129. return buffer[0];
  130. } else {
  131. return 0;
  132. }
  133. }
  134. static inline void subghz_protocol_decoder_nice_flor_s_magic_xor(uint8_t* p, uint8_t k) {
  135. for(uint8_t i = 1; i < 6; i++) {
  136. p[i] ^= k;
  137. }
  138. }
  139. uint64_t subghz_protocol_nice_flor_s_encrypt(uint64_t data, const char* file_name) {
  140. uint8_t* p = (uint8_t*)&data;
  141. uint8_t k = 0;
  142. for(uint8_t y = 0; y < 2; y++) {
  143. k = subghz_protocol_nice_flor_s_get_byte_in_file(file_name, p[0] & 0x1f);
  144. subghz_protocol_decoder_nice_flor_s_magic_xor(p, k);
  145. p[5] &= 0x0f;
  146. p[0] ^= k & 0xe0;
  147. k = subghz_protocol_nice_flor_s_get_byte_in_file(file_name, p[0] >> 3) + 0x25;
  148. subghz_protocol_decoder_nice_flor_s_magic_xor(p, k);
  149. p[5] &= 0x0f;
  150. p[0] ^= k & 0x7;
  151. if(y == 0) {
  152. k = p[0];
  153. p[0] = p[1];
  154. p[1] = k;
  155. }
  156. }
  157. p[5] = ~p[5] & 0x0f;
  158. k = ~p[4];
  159. p[4] = ~p[0];
  160. p[0] = ~p[2];
  161. p[2] = k;
  162. k = ~p[3];
  163. p[3] = ~p[1];
  164. p[1] = k;
  165. return data;
  166. }
  167. static uint64_t
  168. subghz_protocol_nice_flor_s_decrypt(SubGhzBlockGeneric* instance, const char* file_name) {
  169. furi_assert(instance);
  170. uint64_t data = instance->data;
  171. uint8_t* p = (uint8_t*)&data;
  172. uint8_t k = 0;
  173. k = ~p[4];
  174. p[5] = ~p[5];
  175. p[4] = ~p[2];
  176. p[2] = ~p[0];
  177. p[0] = k;
  178. k = ~p[3];
  179. p[3] = ~p[1];
  180. p[1] = k;
  181. for(uint8_t y = 0; y < 2; y++) {
  182. k = subghz_protocol_nice_flor_s_get_byte_in_file(file_name, p[0] >> 3) + 0x25;
  183. subghz_protocol_decoder_nice_flor_s_magic_xor(p, k);
  184. p[5] &= 0x0f;
  185. p[0] ^= k & 0x7;
  186. k = subghz_protocol_nice_flor_s_get_byte_in_file(file_name, p[0] & 0x1f);
  187. subghz_protocol_decoder_nice_flor_s_magic_xor(p, k);
  188. p[5] &= 0x0f;
  189. p[0] ^= k & 0xe0;
  190. if(y == 0) {
  191. k = p[0];
  192. p[0] = p[1];
  193. p[1] = k;
  194. }
  195. }
  196. return data;
  197. }
  198. void* subghz_protocol_decoder_nice_flor_s_alloc(SubGhzEnvironment* environment) {
  199. SubGhzProtocolDecoderNiceFlorS* instance = malloc(sizeof(SubGhzProtocolDecoderNiceFlorS));
  200. instance->base.protocol = &subghz_protocol_nice_flor_s;
  201. instance->generic.protocol_name = instance->base.protocol->name;
  202. instance->nice_flor_s_rainbow_table_file_name =
  203. subghz_environment_get_nice_flor_s_rainbow_table_file_name(environment);
  204. if(instance->nice_flor_s_rainbow_table_file_name) {
  205. FURI_LOG_I(
  206. TAG, "Loading rainbow table from %s", instance->nice_flor_s_rainbow_table_file_name);
  207. }
  208. return instance;
  209. }
  210. void subghz_protocol_decoder_nice_flor_s_free(void* context) {
  211. furi_assert(context);
  212. SubGhzProtocolDecoderNiceFlorS* instance = context;
  213. instance->nice_flor_s_rainbow_table_file_name = NULL;
  214. free(instance);
  215. }
  216. void subghz_protocol_decoder_nice_flor_s_reset(void* context) {
  217. furi_assert(context);
  218. SubGhzProtocolDecoderNiceFlorS* instance = context;
  219. instance->decoder.parser_step = NiceFlorSDecoderStepReset;
  220. }
  221. void subghz_protocol_decoder_nice_flor_s_feed(void* context, bool level, uint32_t duration) {
  222. furi_assert(context);
  223. SubGhzProtocolDecoderNiceFlorS* instance = context;
  224. switch(instance->decoder.parser_step) {
  225. case NiceFlorSDecoderStepReset:
  226. if((!level) && (DURATION_DIFF(duration, subghz_protocol_nice_flor_s_const.te_short * 38) <
  227. subghz_protocol_nice_flor_s_const.te_delta * 38)) {
  228. //Found start header Nice Flor-S
  229. instance->decoder.parser_step = NiceFlorSDecoderStepCheckHeader;
  230. }
  231. break;
  232. case NiceFlorSDecoderStepCheckHeader:
  233. if((level) && (DURATION_DIFF(duration, subghz_protocol_nice_flor_s_const.te_short * 3) <
  234. subghz_protocol_nice_flor_s_const.te_delta * 3)) {
  235. //Found next header Nice Flor-S
  236. instance->decoder.parser_step = NiceFlorSDecoderStepFoundHeader;
  237. } else {
  238. instance->decoder.parser_step = NiceFlorSDecoderStepReset;
  239. }
  240. break;
  241. case NiceFlorSDecoderStepFoundHeader:
  242. if((!level) && (DURATION_DIFF(duration, subghz_protocol_nice_flor_s_const.te_short * 3) <
  243. subghz_protocol_nice_flor_s_const.te_delta * 3)) {
  244. //Found header Nice Flor-S
  245. instance->decoder.parser_step = NiceFlorSDecoderStepSaveDuration;
  246. instance->decoder.decode_data = 0;
  247. instance->decoder.decode_count_bit = 0;
  248. } else {
  249. instance->decoder.parser_step = NiceFlorSDecoderStepReset;
  250. }
  251. break;
  252. case NiceFlorSDecoderStepSaveDuration:
  253. if(level) {
  254. if(DURATION_DIFF(duration, subghz_protocol_nice_flor_s_const.te_short * 3) <
  255. subghz_protocol_nice_flor_s_const.te_delta) {
  256. //Found STOP bit
  257. instance->decoder.parser_step = NiceFlorSDecoderStepReset;
  258. if((instance->decoder.decode_count_bit ==
  259. subghz_protocol_nice_flor_s_const.min_count_bit_for_found) ||
  260. (instance->decoder.decode_count_bit == NICE_ONE_COUNT_BIT)) {
  261. instance->generic.data = instance->data;
  262. instance->data = instance->decoder.decode_data;
  263. instance->decoder.decode_data = instance->generic.data;
  264. instance->generic.data_count_bit = instance->decoder.decode_count_bit;
  265. if(instance->base.callback)
  266. instance->base.callback(&instance->base, instance->base.context);
  267. }
  268. break;
  269. } else {
  270. //save interval
  271. instance->decoder.te_last = duration;
  272. instance->decoder.parser_step = NiceFlorSDecoderStepCheckDuration;
  273. }
  274. }
  275. break;
  276. case NiceFlorSDecoderStepCheckDuration:
  277. if(!level) {
  278. if((DURATION_DIFF(
  279. instance->decoder.te_last, subghz_protocol_nice_flor_s_const.te_short) <
  280. subghz_protocol_nice_flor_s_const.te_delta) &&
  281. (DURATION_DIFF(duration, subghz_protocol_nice_flor_s_const.te_long) <
  282. subghz_protocol_nice_flor_s_const.te_delta)) {
  283. subghz_protocol_blocks_add_bit(&instance->decoder, 0);
  284. instance->decoder.parser_step = NiceFlorSDecoderStepSaveDuration;
  285. } else if(
  286. (DURATION_DIFF(
  287. instance->decoder.te_last, subghz_protocol_nice_flor_s_const.te_long) <
  288. subghz_protocol_nice_flor_s_const.te_delta) &&
  289. (DURATION_DIFF(duration, subghz_protocol_nice_flor_s_const.te_short) <
  290. subghz_protocol_nice_flor_s_const.te_delta)) {
  291. subghz_protocol_blocks_add_bit(&instance->decoder, 1);
  292. instance->decoder.parser_step = NiceFlorSDecoderStepSaveDuration;
  293. } else
  294. instance->decoder.parser_step = NiceFlorSDecoderStepReset;
  295. } else {
  296. instance->decoder.parser_step = NiceFlorSDecoderStepReset;
  297. }
  298. if(instance->decoder.decode_count_bit ==
  299. subghz_protocol_nice_flor_s_const.min_count_bit_for_found) {
  300. instance->data = instance->decoder.decode_data;
  301. instance->decoder.decode_data = 0;
  302. }
  303. break;
  304. }
  305. }
  306. /**
  307. * Analysis of received data
  308. * @param instance Pointer to a SubGhzBlockGeneric* instance
  309. * @param file_name Full path to rainbow table the file
  310. */
  311. static void subghz_protocol_nice_flor_s_remote_controller(
  312. SubGhzBlockGeneric* instance,
  313. const char* file_name) {
  314. /*
  315. * Protocol Nice Flor-S
  316. * Packet format Nice Flor-s: START-P0-P1-P2-P3-P4-P5-P6-P7-STOP
  317. * P0 (4-bit) - button positional code - 1:0x1, 2:0x2, 3:0x4, 4:0x8;
  318. * P1 (4-bit) - batch repetition number, calculated by the formula:
  319. * P1 = 0xF ^ P0 ^ n; where n changes from 1 to 15, then 0, and then in a circle
  320. * key 1: {0xE,0xF,0xC,0xD,0xA,0xB,0x8,0x9,0x6,0x7,0x4,0x5,0x2,0x3,0x0,0x1};
  321. * key 2: {0xD,0xC,0xF,0xE,0x9,0x8,0xB,0xA,0x5,0x4,0x7,0x6,0x1,0x0,0x3,0x2};
  322. * key 3: {0xB,0xA,0x9,0x8,0xF,0xE,0xD,0xC,0x3,0x2,0x1,0x0,0x7,0x6,0x5,0x4};
  323. * key 4: {0x7,0x6,0x5,0x4,0x3,0x2,0x1,0x0,0xF,0xE,0xD,0xC,0xB,0xA,0x9,0x8};
  324. * P2 (4-bit) - part of the serial number, P2 = (K ^ S3) & 0xF;
  325. * P3 (byte) - the major part of the encrypted index
  326. * P4 (byte) - the low-order part of the encrypted index
  327. * P5 (byte) - part of the serial number, P5 = K ^ S2;
  328. * P6 (byte) - part of the serial number, P6 = K ^ S1;
  329. * P7 (byte) - part of the serial number, P7 = K ^ S0;
  330. * K (byte) - depends on P3 and P4, K = Fk(P3, P4);
  331. * S3,S2,S1,S0 - serial number of the console 28 bit.
  332. *
  333. * data => 0x1c5783607f7b3 key serial cnt
  334. * decrypt => 0x10436c6820444 => 0x1 0436c682 0444
  335. *
  336. * Protocol Nice One
  337. * Generally repeats the Nice Flor-S protocol, but there are a few changes
  338. * Packet format first 52 bytes repeat Nice Flor-S protocol
  339. * The additional 20 bytes contain the code of the pressed button,
  340. * the button hold bit and the CRC of the entire message.
  341. * START-P0-P1-P2-P3-P4-P5-P6-P7-P8-P9-P10-STOP
  342. * P7 (byte) - if (n<4) k=0x8f : k=0x80; P7= k^n;
  343. * P8 (byte) - if (hold bit) b=0x00 : b=0x10; P8= b^(k<<4) | 4 hi bit crc
  344. * P10 (4-bit) - 4 lo bit crc
  345. * key+b crc
  346. * data => 0x1724A7D9A522F 899 D6 hold bit = 0 - just pressed the button
  347. * data => 0x1424A7D9A522F 8AB 03 hold bit = 1 - button hold
  348. *
  349. * A small button hold counter (0..15) is stored between each press,
  350. * i.e. if 1 press of the button stops counter 6, then the next press
  351. * of the button will start from the value 7 (hold bit = 0), 8 (hold bit = 1)...
  352. * further up to 15 with overflow
  353. *
  354. */
  355. if(!file_name) {
  356. instance->cnt = 0;
  357. instance->serial = 0;
  358. instance->btn = 0;
  359. } else {
  360. uint64_t decrypt = subghz_protocol_nice_flor_s_decrypt(instance, file_name);
  361. instance->cnt = decrypt & 0xFFFF;
  362. instance->serial = (decrypt >> 16) & 0xFFFFFFF;
  363. instance->btn = (decrypt >> 48) & 0xF;
  364. }
  365. }
  366. uint8_t subghz_protocol_decoder_nice_flor_s_get_hash_data(void* context) {
  367. furi_assert(context);
  368. SubGhzProtocolDecoderNiceFlorS* instance = context;
  369. return subghz_protocol_blocks_get_hash_data(
  370. &instance->decoder, (instance->decoder.decode_count_bit / 8) + 1);
  371. }
  372. bool subghz_protocol_decoder_nice_flor_s_serialize(
  373. void* context,
  374. FlipperFormat* flipper_format,
  375. SubGhzRadioPreset* preset) {
  376. furi_assert(context);
  377. SubGhzProtocolDecoderNiceFlorS* instance = context;
  378. bool res = subghz_block_generic_serialize(&instance->generic, flipper_format, preset);
  379. if(instance->generic.data_count_bit == NICE_ONE_COUNT_BIT) {
  380. if(res &&
  381. !flipper_format_write_uint32(flipper_format, "Data", (uint32_t*)&instance->data, 1)) {
  382. FURI_LOG_E(TAG, "Unable to add Data");
  383. res = false;
  384. }
  385. }
  386. return res;
  387. }
  388. bool subghz_protocol_decoder_nice_flor_s_deserialize(void* context, FlipperFormat* flipper_format) {
  389. furi_assert(context);
  390. SubGhzProtocolDecoderNiceFlorS* instance = context;
  391. bool ret = false;
  392. do {
  393. if(!subghz_block_generic_deserialize(&instance->generic, flipper_format)) {
  394. break;
  395. }
  396. if((instance->generic.data_count_bit !=
  397. subghz_protocol_nice_flor_s_const.min_count_bit_for_found) &&
  398. (instance->generic.data_count_bit != NICE_ONE_COUNT_BIT)) {
  399. FURI_LOG_E(TAG, "Wrong number of bits in key");
  400. break;
  401. }
  402. if(instance->generic.data_count_bit == NICE_ONE_COUNT_BIT) {
  403. if(!flipper_format_rewind(flipper_format)) {
  404. FURI_LOG_E(TAG, "Rewind error");
  405. break;
  406. }
  407. uint32_t temp = 0;
  408. if(!flipper_format_read_uint32(flipper_format, "Data", (uint32_t*)&temp, 1)) {
  409. FURI_LOG_E(TAG, "Missing Data");
  410. break;
  411. }
  412. instance->data = (uint64_t)temp;
  413. }
  414. ret = true;
  415. } while(false);
  416. return ret;
  417. }
  418. void subghz_protocol_decoder_nice_flor_s_get_string(void* context, FuriString* output) {
  419. furi_assert(context);
  420. SubGhzProtocolDecoderNiceFlorS* instance = context;
  421. subghz_protocol_nice_flor_s_remote_controller(
  422. &instance->generic, instance->nice_flor_s_rainbow_table_file_name);
  423. if(instance->generic.data_count_bit == NICE_ONE_COUNT_BIT) {
  424. furi_string_cat_printf(
  425. output,
  426. "%s %dbit\r\n"
  427. "Key:0x%013llX%llX\r\n"
  428. "Sn:%05lX\r\n"
  429. "Cnt:%04lX Btn:%02X\r\n",
  430. NICE_ONE_NAME,
  431. instance->generic.data_count_bit,
  432. instance->generic.data,
  433. instance->data,
  434. instance->generic.serial,
  435. instance->generic.cnt,
  436. instance->generic.btn);
  437. } else {
  438. furi_string_cat_printf(
  439. output,
  440. "%s %dbit\r\n"
  441. "Key:0x%013llX\r\n"
  442. "Sn:%05lX\r\n"
  443. "Cnt:%04lX Btn:%02X\r\n",
  444. instance->generic.protocol_name,
  445. instance->generic.data_count_bit,
  446. instance->generic.data,
  447. instance->generic.serial,
  448. instance->generic.cnt,
  449. instance->generic.btn);
  450. }
  451. }