nice_flor_s.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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. static const SubGhzBlockConst subghz_protocol_nice_flor_s_const = {
  14. .te_short = 500,
  15. .te_long = 1000,
  16. .te_delta = 300,
  17. .min_count_bit_for_found = 52,
  18. };
  19. struct SubGhzProtocolDecoderNiceFlorS {
  20. SubGhzProtocolDecoderBase base;
  21. SubGhzBlockDecoder decoder;
  22. SubGhzBlockGeneric generic;
  23. const char* nice_flor_s_rainbow_table_file_name;
  24. };
  25. struct SubGhzProtocolEncoderNiceFlorS {
  26. SubGhzProtocolEncoderBase base;
  27. SubGhzProtocolBlockEncoder encoder;
  28. SubGhzBlockGeneric generic;
  29. };
  30. typedef enum {
  31. NiceFlorSDecoderStepReset = 0,
  32. NiceFlorSDecoderStepCheckHeader,
  33. NiceFlorSDecoderStepFoundHeader,
  34. NiceFlorSDecoderStepSaveDuration,
  35. NiceFlorSDecoderStepCheckDuration,
  36. } NiceFlorSDecoderStep;
  37. const SubGhzProtocolDecoder subghz_protocol_nice_flor_s_decoder = {
  38. .alloc = subghz_protocol_decoder_nice_flor_s_alloc,
  39. .free = subghz_protocol_decoder_nice_flor_s_free,
  40. .feed = subghz_protocol_decoder_nice_flor_s_feed,
  41. .reset = subghz_protocol_decoder_nice_flor_s_reset,
  42. .get_hash_data = subghz_protocol_decoder_nice_flor_s_get_hash_data,
  43. .serialize = subghz_protocol_decoder_nice_flor_s_serialize,
  44. .deserialize = subghz_protocol_decoder_nice_flor_s_deserialize,
  45. .get_string = subghz_protocol_decoder_nice_flor_s_get_string,
  46. };
  47. const SubGhzProtocolEncoder subghz_protocol_nice_flor_s_encoder = {
  48. .alloc = NULL,
  49. .free = NULL,
  50. .deserialize = NULL,
  51. .stop = NULL,
  52. .yield = NULL,
  53. };
  54. const SubGhzProtocol subghz_protocol_nice_flor_s = {
  55. .name = SUBGHZ_PROTOCOL_NICE_FLOR_S_NAME,
  56. .type = SubGhzProtocolTypeDynamic,
  57. .flag = SubGhzProtocolFlag_433 | SubGhzProtocolFlag_868 | SubGhzProtocolFlag_AM |
  58. SubGhzProtocolFlag_Decodable,
  59. .decoder = &subghz_protocol_nice_flor_s_decoder,
  60. .encoder = &subghz_protocol_nice_flor_s_encoder,
  61. };
  62. /**
  63. * Read bytes from rainbow table
  64. * @param file_name Full path to rainbow table the file
  65. * @param address Byte address in file
  66. * @return data
  67. */
  68. static uint8_t
  69. subghz_protocol_nice_flor_s_get_byte_in_file(const char* file_name, uint32_t address) {
  70. if(!file_name) return 0;
  71. uint8_t buffer[1] = {0};
  72. if(subghz_keystore_raw_get_data(file_name, address, buffer, sizeof(uint8_t))) {
  73. return buffer[0];
  74. } else {
  75. return 0;
  76. }
  77. }
  78. static inline void subghz_protocol_decoder_nice_flor_s_magic_xor(uint8_t* p, uint8_t k) {
  79. for(uint8_t i = 1; i < 6; i++) {
  80. p[i] ^= k;
  81. }
  82. }
  83. uint64_t subghz_protocol_nice_flor_s_encrypt(uint64_t data, const char* file_name) {
  84. uint8_t* p = (uint8_t*)&data;
  85. uint8_t k = 0;
  86. for(uint8_t y = 0; y < 2; y++) {
  87. k = subghz_protocol_nice_flor_s_get_byte_in_file(file_name, p[0] & 0x1f);
  88. subghz_protocol_decoder_nice_flor_s_magic_xor(p, k);
  89. p[5] &= 0x0f;
  90. p[0] ^= k & 0xe0;
  91. k = subghz_protocol_nice_flor_s_get_byte_in_file(file_name, p[0] >> 3) + 0x25;
  92. subghz_protocol_decoder_nice_flor_s_magic_xor(p, k);
  93. p[5] &= 0x0f;
  94. p[0] ^= k & 0x7;
  95. if(y == 0) {
  96. k = p[0];
  97. p[0] = p[1];
  98. p[1] = k;
  99. }
  100. }
  101. p[5] = ~p[5] & 0x0f;
  102. k = ~p[4];
  103. p[4] = ~p[0];
  104. p[0] = ~p[2];
  105. p[2] = k;
  106. k = ~p[3];
  107. p[3] = ~p[1];
  108. p[1] = k;
  109. return data;
  110. }
  111. static uint64_t
  112. subghz_protocol_nice_flor_s_decrypt(SubGhzBlockGeneric* instance, const char* file_name) {
  113. furi_assert(instance);
  114. uint64_t data = instance->data;
  115. uint8_t* p = (uint8_t*)&data;
  116. uint8_t k = 0;
  117. k = ~p[4];
  118. p[5] = ~p[5];
  119. p[4] = ~p[2];
  120. p[2] = ~p[0];
  121. p[0] = k;
  122. k = ~p[3];
  123. p[3] = ~p[1];
  124. p[1] = k;
  125. for(uint8_t y = 0; y < 2; y++) {
  126. k = subghz_protocol_nice_flor_s_get_byte_in_file(file_name, p[0] >> 3) + 0x25;
  127. subghz_protocol_decoder_nice_flor_s_magic_xor(p, k);
  128. p[5] &= 0x0f;
  129. p[0] ^= k & 0x7;
  130. k = subghz_protocol_nice_flor_s_get_byte_in_file(file_name, p[0] & 0x1f);
  131. subghz_protocol_decoder_nice_flor_s_magic_xor(p, k);
  132. p[5] &= 0x0f;
  133. p[0] ^= k & 0xe0;
  134. if(y == 0) {
  135. k = p[0];
  136. p[0] = p[1];
  137. p[1] = k;
  138. }
  139. }
  140. return data;
  141. }
  142. void* subghz_protocol_decoder_nice_flor_s_alloc(SubGhzEnvironment* environment) {
  143. SubGhzProtocolDecoderNiceFlorS* instance = malloc(sizeof(SubGhzProtocolDecoderNiceFlorS));
  144. instance->base.protocol = &subghz_protocol_nice_flor_s;
  145. instance->generic.protocol_name = instance->base.protocol->name;
  146. instance->nice_flor_s_rainbow_table_file_name =
  147. subghz_environment_get_nice_flor_s_rainbow_table_file_name(environment);
  148. if(instance->nice_flor_s_rainbow_table_file_name) {
  149. FURI_LOG_I(
  150. TAG, "Loading rainbow table from %s", instance->nice_flor_s_rainbow_table_file_name);
  151. }
  152. return instance;
  153. }
  154. void subghz_protocol_decoder_nice_flor_s_free(void* context) {
  155. furi_assert(context);
  156. SubGhzProtocolDecoderNiceFlorS* instance = context;
  157. instance->nice_flor_s_rainbow_table_file_name = NULL;
  158. free(instance);
  159. }
  160. void subghz_protocol_decoder_nice_flor_s_reset(void* context) {
  161. furi_assert(context);
  162. SubGhzProtocolDecoderNiceFlorS* instance = context;
  163. instance->decoder.parser_step = NiceFlorSDecoderStepReset;
  164. }
  165. void subghz_protocol_decoder_nice_flor_s_feed(void* context, bool level, uint32_t duration) {
  166. furi_assert(context);
  167. SubGhzProtocolDecoderNiceFlorS* instance = context;
  168. switch(instance->decoder.parser_step) {
  169. case NiceFlorSDecoderStepReset:
  170. if((!level) && (DURATION_DIFF(duration, subghz_protocol_nice_flor_s_const.te_short * 38) <
  171. subghz_protocol_nice_flor_s_const.te_delta * 38)) {
  172. //Found start header Nice Flor-S
  173. instance->decoder.parser_step = NiceFlorSDecoderStepCheckHeader;
  174. }
  175. break;
  176. case NiceFlorSDecoderStepCheckHeader:
  177. if((level) && (DURATION_DIFF(duration, subghz_protocol_nice_flor_s_const.te_short * 3) <
  178. subghz_protocol_nice_flor_s_const.te_delta * 3)) {
  179. //Found next header Nice Flor-S
  180. instance->decoder.parser_step = NiceFlorSDecoderStepFoundHeader;
  181. } else {
  182. instance->decoder.parser_step = NiceFlorSDecoderStepReset;
  183. }
  184. break;
  185. case NiceFlorSDecoderStepFoundHeader:
  186. if((!level) && (DURATION_DIFF(duration, subghz_protocol_nice_flor_s_const.te_short * 3) <
  187. subghz_protocol_nice_flor_s_const.te_delta * 3)) {
  188. //Found header Nice Flor-S
  189. instance->decoder.parser_step = NiceFlorSDecoderStepSaveDuration;
  190. instance->decoder.decode_data = 0;
  191. instance->decoder.decode_count_bit = 0;
  192. } else {
  193. instance->decoder.parser_step = NiceFlorSDecoderStepReset;
  194. }
  195. break;
  196. case NiceFlorSDecoderStepSaveDuration:
  197. if(level) {
  198. if(DURATION_DIFF(duration, subghz_protocol_nice_flor_s_const.te_short * 3) <
  199. subghz_protocol_nice_flor_s_const.te_delta) {
  200. //Found STOP bit
  201. instance->decoder.parser_step = NiceFlorSDecoderStepReset;
  202. if(instance->decoder.decode_count_bit ==
  203. subghz_protocol_nice_flor_s_const.min_count_bit_for_found) {
  204. instance->generic.data = instance->decoder.decode_data;
  205. instance->generic.data_count_bit = instance->decoder.decode_count_bit;
  206. if(instance->base.callback)
  207. instance->base.callback(&instance->base, instance->base.context);
  208. }
  209. break;
  210. } else {
  211. //save interval
  212. instance->decoder.te_last = duration;
  213. instance->decoder.parser_step = NiceFlorSDecoderStepCheckDuration;
  214. }
  215. }
  216. break;
  217. case NiceFlorSDecoderStepCheckDuration:
  218. if(!level) {
  219. if((DURATION_DIFF(
  220. instance->decoder.te_last, subghz_protocol_nice_flor_s_const.te_short) <
  221. subghz_protocol_nice_flor_s_const.te_delta) &&
  222. (DURATION_DIFF(duration, subghz_protocol_nice_flor_s_const.te_long) <
  223. subghz_protocol_nice_flor_s_const.te_delta)) {
  224. subghz_protocol_blocks_add_bit(&instance->decoder, 0);
  225. instance->decoder.parser_step = NiceFlorSDecoderStepSaveDuration;
  226. } else if(
  227. (DURATION_DIFF(
  228. instance->decoder.te_last, subghz_protocol_nice_flor_s_const.te_long) <
  229. subghz_protocol_nice_flor_s_const.te_delta) &&
  230. (DURATION_DIFF(duration, subghz_protocol_nice_flor_s_const.te_short) <
  231. subghz_protocol_nice_flor_s_const.te_delta)) {
  232. subghz_protocol_blocks_add_bit(&instance->decoder, 1);
  233. instance->decoder.parser_step = NiceFlorSDecoderStepSaveDuration;
  234. } else
  235. instance->decoder.parser_step = NiceFlorSDecoderStepReset;
  236. } else {
  237. instance->decoder.parser_step = NiceFlorSDecoderStepReset;
  238. }
  239. break;
  240. }
  241. }
  242. /**
  243. * Analysis of received data
  244. * @param instance Pointer to a SubGhzBlockGeneric* instance
  245. * @param file_name Full path to rainbow table the file
  246. */
  247. static void subghz_protocol_nice_flor_s_remote_controller(
  248. SubGhzBlockGeneric* instance,
  249. const char* file_name) {
  250. /*
  251. * Packet format Nice Flor-s: START-P0-P1-P2-P3-P4-P5-P6-P7-STOP
  252. * P0 (4-bit) - button positional code - 1:0x1, 2:0x2, 3:0x4, 4:0x8;
  253. * P1 (4-bit) - batch repetition number, calculated by the formula:
  254. * P1 = 0xF ^ P0 ^ n; where n changes from 1 to 15, then 0, and then in a circle
  255. * key 1: {0xE,0xF,0xC,0xD,0xA,0xB,0x8,0x9,0x6,0x7,0x4,0x5,0x2,0x3,0x0,0x1};
  256. * key 2: {0xD,0xC,0xF,0xE,0x9,0x8,0xB,0xA,0x5,0x4,0x7,0x6,0x1,0x0,0x3,0x2};
  257. * key 3: {0xB,0xA,0x9,0x8,0xF,0xE,0xD,0xC,0x3,0x2,0x1,0x0,0x7,0x6,0x5,0x4};
  258. * key 4: {0x7,0x6,0x5,0x4,0x3,0x2,0x1,0x0,0xF,0xE,0xD,0xC,0xB,0xA,0x9,0x8};
  259. * P2 (4-bit) - part of the serial number, P2 = (K ^ S3) & 0xF;
  260. * P3 (byte) - the major part of the encrypted index
  261. * P4 (byte) - the low-order part of the encrypted index
  262. * P5 (byte) - part of the serial number, P5 = K ^ S2;
  263. * P6 (byte) - part of the serial number, P6 = K ^ S1;
  264. * P7 (byte) - part of the serial number, P7 = K ^ S0;
  265. * K (byte) - depends on P3 and P4, K = Fk(P3, P4);
  266. * S3,S2,S1,S0 - serial number of the console 28 bit.
  267. *
  268. * data => 0x1c5783607f7b3 key serial cnt
  269. * decrypt => 0x10436c6820444 => 0x1 0436c682 0444
  270. *
  271. */
  272. if(!file_name) {
  273. instance->cnt = 0;
  274. instance->serial = 0;
  275. instance->btn = 0;
  276. } else {
  277. uint64_t decrypt = subghz_protocol_nice_flor_s_decrypt(instance, file_name);
  278. instance->cnt = decrypt & 0xFFFF;
  279. instance->serial = (decrypt >> 16) & 0xFFFFFFF;
  280. instance->btn = (decrypt >> 48) & 0xF;
  281. }
  282. }
  283. uint8_t subghz_protocol_decoder_nice_flor_s_get_hash_data(void* context) {
  284. furi_assert(context);
  285. SubGhzProtocolDecoderNiceFlorS* instance = context;
  286. return subghz_protocol_blocks_get_hash_data(
  287. &instance->decoder, (instance->decoder.decode_count_bit / 8) + 1);
  288. }
  289. bool subghz_protocol_decoder_nice_flor_s_serialize(
  290. void* context,
  291. FlipperFormat* flipper_format,
  292. SubGhzRadioPreset* preset) {
  293. furi_assert(context);
  294. SubGhzProtocolDecoderNiceFlorS* instance = context;
  295. return subghz_block_generic_serialize(&instance->generic, flipper_format, preset);
  296. }
  297. bool subghz_protocol_decoder_nice_flor_s_deserialize(void* context, FlipperFormat* flipper_format) {
  298. furi_assert(context);
  299. SubGhzProtocolDecoderNiceFlorS* instance = context;
  300. bool ret = false;
  301. do {
  302. if(!subghz_block_generic_deserialize(&instance->generic, flipper_format)) {
  303. break;
  304. }
  305. if(instance->generic.data_count_bit !=
  306. subghz_protocol_nice_flor_s_const.min_count_bit_for_found) {
  307. FURI_LOG_E(TAG, "Wrong number of bits in key");
  308. break;
  309. }
  310. ret = true;
  311. } while(false);
  312. return ret;
  313. }
  314. void subghz_protocol_decoder_nice_flor_s_get_string(void* context, FuriString* output) {
  315. furi_assert(context);
  316. SubGhzProtocolDecoderNiceFlorS* instance = context;
  317. subghz_protocol_nice_flor_s_remote_controller(
  318. &instance->generic, instance->nice_flor_s_rainbow_table_file_name);
  319. uint32_t code_found_hi = instance->generic.data >> 32;
  320. uint32_t code_found_lo = instance->generic.data & 0x00000000ffffffff;
  321. furi_string_cat_printf(
  322. output,
  323. "%s %dbit\r\n"
  324. "Key:0x%lX%08lX\r\n"
  325. "Sn:%05lX\r\n"
  326. "Cnt:%04lX Btn:%02X\r\n",
  327. instance->generic.protocol_name,
  328. instance->generic.data_count_bit,
  329. code_found_hi,
  330. code_found_lo,
  331. instance->generic.serial,
  332. instance->generic.cnt,
  333. instance->generic.btn);
  334. }