subghz_protocol_nice_flor_s.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. #include "subghz_protocol_nice_flor_s.h"
  2. #include <furi.h>
  3. #include "file-worker.h"
  4. #include "../subghz_keystore.h"
  5. /*
  6. * https://phreakerclub.com/1615
  7. * https://phreakerclub.com/forum/showthread.php?t=2360
  8. * https://vrtp.ru/index.php?showtopic=27867
  9. */
  10. #define TAG "SubGhzNiceFlorS"
  11. struct SubGhzProtocolNiceFlorS {
  12. SubGhzProtocolCommon common;
  13. const char* rainbow_table_file_name;
  14. };
  15. typedef enum {
  16. NiceFlorSDecoderStepReset = 0,
  17. NiceFlorSDecoderStepCheckHeader,
  18. NiceFlorSDecoderStepFoundHeader,
  19. NiceFlorSDecoderStepSaveDuration,
  20. NiceFlorSDecoderStepCheckDuration,
  21. } NiceFlorSDecoderStep;
  22. SubGhzProtocolNiceFlorS* subghz_protocol_nice_flor_s_alloc() {
  23. SubGhzProtocolNiceFlorS* instance = furi_alloc(sizeof(SubGhzProtocolNiceFlorS));
  24. instance->common.name = "Nice FloR-S";
  25. instance->common.code_min_count_bit_for_found = 52;
  26. instance->common.te_short = 500;
  27. instance->common.te_long = 1000;
  28. instance->common.te_delta = 300;
  29. instance->common.type_protocol = SubGhzProtocolCommonTypeDynamic;
  30. instance->common.to_string = (SubGhzProtocolCommonToStr)subghz_protocol_nice_flor_s_to_str;
  31. instance->common.to_load_protocol =
  32. (SubGhzProtocolCommonLoadFromRAW)subghz_decoder_nice_flor_s_to_load_protocol;
  33. return instance;
  34. }
  35. void subghz_protocol_nice_flor_s_free(SubGhzProtocolNiceFlorS* instance) {
  36. furi_assert(instance);
  37. free(instance);
  38. }
  39. void subghz_protocol_nice_flor_s_name_file(SubGhzProtocolNiceFlorS* instance, const char* name) {
  40. instance->rainbow_table_file_name = name;
  41. FURI_LOG_I(TAG, "Loading rainbow table from %s", name);
  42. }
  43. /** Send bit
  44. *
  45. * @param instance - SubGhzProtocolNiceFlorS instance
  46. * @param bit - bit
  47. */
  48. void subghz_protocol_nice_flor_s_send_bit(SubGhzProtocolNiceFlorS* instance, uint8_t bit) {
  49. if(bit) {
  50. //send bit 1
  51. SUBGHZ_TX_PIN_HIGH();
  52. delay_us(instance->common.te_long);
  53. SUBGHZ_TX_PIN_LOW();
  54. delay_us(instance->common.te_short);
  55. } else {
  56. //send bit 0
  57. SUBGHZ_TX_PIN_HIGH();
  58. delay_us(instance->common.te_short);
  59. SUBGHZ_TX_PIN_LOW();
  60. delay_us(instance->common.te_long);
  61. }
  62. }
  63. void subghz_protocol_nice_flor_s_send_key(
  64. SubGhzProtocolNiceFlorS* instance,
  65. uint64_t key,
  66. uint8_t bit,
  67. uint8_t repeat) {
  68. while(repeat--) {
  69. //Send header
  70. SUBGHZ_TX_PIN_LOW();
  71. delay_us(instance->common.te_short * 34);
  72. //Send Start Bit
  73. SUBGHZ_TX_PIN_HIGH();
  74. delay_us(instance->common.te_short * 3);
  75. SUBGHZ_TX_PIN_LOW();
  76. delay_us(instance->common.te_short * 3);
  77. //Send key data
  78. for(uint8_t i = bit; i > 0; i--) {
  79. subghz_protocol_nice_flor_s_send_bit(instance, bit_read(key, i - 1));
  80. }
  81. //Send Stop Bit
  82. SUBGHZ_TX_PIN_HIGH();
  83. delay_us(instance->common.te_short * 3);
  84. SUBGHZ_TX_PIN_LOW();
  85. delay_us(instance->common.te_short * 3);
  86. }
  87. }
  88. /** Read bytes from rainbow table
  89. *
  90. * @param instance - SubGhzProtocolNiceFlorS* instance
  91. * @param address - address byte
  92. * @return byte data
  93. */
  94. uint8_t subghz_nice_flor_s_get_byte_in_file(SubGhzProtocolNiceFlorS* instance, uint32_t address) {
  95. if(!instance->rainbow_table_file_name) return 0;
  96. uint8_t buffer[1] = {0};
  97. if(subghz_keystore_raw_get_data(
  98. instance->rainbow_table_file_name, address, buffer, sizeof(uint8_t))) {
  99. return buffer[0];
  100. } else {
  101. return 0;
  102. }
  103. }
  104. /** Decrypt protocol Nice Flor S
  105. *
  106. * @param instance - SubGhzProtocolNiceFlorS* instance
  107. */
  108. void subghz_nice_flor_s_decoder_decrypt(SubGhzProtocolNiceFlorS* instance) {
  109. /*
  110. * Packet format Nice Flor-s: START-P0-P1-P2-P3-P4-P5-P6-P7-STOP
  111. * P0 (4-bit) - button positional code - 1:0x1, 2:0x2, 3:0x4, 4:0x8;
  112. * P1 (4-bit) - batch repetition number, calculated by the formula:
  113. * P1 = 0xF ^ P0 ^ n; where n changes from 1 to 15, then 0, and then in a circle
  114. * key 1: {0xF,0xC,0xD,0xA,0xB,0x8,0x9,0x6,0x7,0x4,0x5,0x2,0x3,0x0,0x1,0xE};
  115. * key 2: {0xC,0xF,0xE,0x9,0x8,0xB,0xA,0x5,0x4,0x7,0x6,0x1,0x0,0x3,0x2,0xD};
  116. * key 3: {0xA,0x9,0x8,0xF,0xE,0xD,0xC,0x3,0x2,0x1,0x0,0x7,0x6,0x5,0x4,0xB};
  117. * P2 (4-bit) - part of the serial number, P2 = (K ^ S3) & 0xF;
  118. * P3 (byte) - the major part of the encrypted index
  119. * P4 (byte) - the low-order part of the encrypted index
  120. * P5 (byte) - part of the serial number, P5 = K ^ S2;
  121. * P6 (byte) - part of the serial number, P6 = K ^ S1;
  122. * P7 (byte) - part of the serial number, P7 = K ^ S0;
  123. * K (byte) - depends on P3 and P4, K = Fk(P3, P4);
  124. * S3,S2,S1,S0 - serial number of the console 28 bit.
  125. */
  126. uint16_t p3p4 = (uint16_t)(instance->common.code_last_found >> 24);
  127. instance->common.cnt = subghz_nice_flor_s_get_byte_in_file(instance, p3p4 * 2) << 8 |
  128. subghz_nice_flor_s_get_byte_in_file(instance, p3p4 * 2 + 1);
  129. uint8_t k =
  130. (uint8_t)(p3p4 & 0x00FF) ^
  131. subghz_nice_flor_s_get_byte_in_file(instance, (0x20000 | (instance->common.cnt & 0x00ff)));
  132. uint8_t s3 = ((uint8_t)(instance->common.code_last_found >> 40) ^ k) & 0x0f;
  133. uint8_t s2 = ((uint8_t)(instance->common.code_last_found >> 16) ^ k);
  134. uint8_t s1 = ((uint8_t)(instance->common.code_last_found >> 8) ^ k);
  135. uint8_t s0 = ((uint8_t)(instance->common.code_last_found) ^ k);
  136. instance->common.serial = s3 << 24 | s2 << 16 | s1 << 8 | s0;
  137. instance->common.btn = (instance->common.code_last_found >> 48) & 0x0f;
  138. }
  139. void subghz_protocol_nice_flor_s_reset(SubGhzProtocolNiceFlorS* instance) {
  140. instance->common.parser_step = NiceFlorSDecoderStepReset;
  141. }
  142. void subghz_protocol_nice_flor_s_parse(
  143. SubGhzProtocolNiceFlorS* instance,
  144. bool level,
  145. uint32_t duration) {
  146. switch(instance->common.parser_step) {
  147. case NiceFlorSDecoderStepReset:
  148. if((!level) && (DURATION_DIFF(duration, instance->common.te_short * 38) <
  149. instance->common.te_delta * 38)) {
  150. //Found start header Nice Flor-S
  151. instance->common.parser_step = NiceFlorSDecoderStepCheckHeader;
  152. }
  153. break;
  154. case NiceFlorSDecoderStepCheckHeader:
  155. if((level) && (DURATION_DIFF(duration, instance->common.te_short * 3) <
  156. instance->common.te_delta * 3)) {
  157. //Found next header Nice Flor-S
  158. instance->common.parser_step = NiceFlorSDecoderStepFoundHeader;
  159. } else {
  160. instance->common.parser_step = NiceFlorSDecoderStepReset;
  161. }
  162. break;
  163. case NiceFlorSDecoderStepFoundHeader:
  164. if((!level) && (DURATION_DIFF(duration, instance->common.te_short * 3) <
  165. instance->common.te_delta * 3)) {
  166. //Found header Nice Flor-S
  167. instance->common.parser_step = NiceFlorSDecoderStepSaveDuration;
  168. instance->common.code_found = 0;
  169. instance->common.code_count_bit = 0;
  170. } else {
  171. instance->common.parser_step = NiceFlorSDecoderStepReset;
  172. }
  173. break;
  174. case NiceFlorSDecoderStepSaveDuration:
  175. if(level) {
  176. if(DURATION_DIFF(duration, instance->common.te_short * 3) <
  177. instance->common.te_delta) {
  178. //Found STOP bit
  179. instance->common.parser_step = NiceFlorSDecoderStepReset;
  180. if(instance->common.code_count_bit >=
  181. instance->common.code_min_count_bit_for_found) {
  182. instance->common.code_last_found = instance->common.code_found;
  183. instance->common.code_last_count_bit = instance->common.code_count_bit;
  184. if(instance->common.callback)
  185. instance->common.callback(
  186. (SubGhzProtocolCommon*)instance, instance->common.context);
  187. }
  188. break;
  189. } else {
  190. //save interval
  191. instance->common.te_last = duration;
  192. instance->common.parser_step = NiceFlorSDecoderStepCheckDuration;
  193. }
  194. }
  195. break;
  196. case NiceFlorSDecoderStepCheckDuration:
  197. if(!level) {
  198. if((DURATION_DIFF(instance->common.te_last, instance->common.te_short) <
  199. instance->common.te_delta) &&
  200. (DURATION_DIFF(duration, instance->common.te_long) < instance->common.te_delta)) {
  201. subghz_protocol_common_add_bit(&instance->common, 0);
  202. instance->common.parser_step = NiceFlorSDecoderStepSaveDuration;
  203. } else if(
  204. (DURATION_DIFF(instance->common.te_last, instance->common.te_long) <
  205. instance->common.te_delta) &&
  206. (DURATION_DIFF(duration, instance->common.te_short) < instance->common.te_delta)) {
  207. subghz_protocol_common_add_bit(&instance->common, 1);
  208. instance->common.parser_step = NiceFlorSDecoderStepSaveDuration;
  209. } else
  210. instance->common.parser_step = NiceFlorSDecoderStepReset;
  211. } else {
  212. instance->common.parser_step = NiceFlorSDecoderStepReset;
  213. }
  214. break;
  215. }
  216. }
  217. void subghz_protocol_nice_flor_s_to_str(SubGhzProtocolNiceFlorS* instance, string_t output) {
  218. subghz_nice_flor_s_decoder_decrypt(instance);
  219. uint32_t code_found_hi = instance->common.code_last_found >> 32;
  220. uint32_t code_found_lo = instance->common.code_last_found & 0x00000000ffffffff;
  221. string_cat_printf(
  222. output,
  223. "%s %dbit\r\n"
  224. "Key:0x%lX%08lX\r\n"
  225. "Sn:%05lX\r\n"
  226. "Cnt:%04X Btn:%02lX\r\n",
  227. instance->common.name,
  228. instance->common.code_last_count_bit,
  229. code_found_hi,
  230. code_found_lo,
  231. instance->common.serial,
  232. instance->common.cnt,
  233. instance->common.btn);
  234. }
  235. void subghz_decoder_nice_flor_s_to_load_protocol(SubGhzProtocolNiceFlorS* instance, void* context) {
  236. furi_assert(context);
  237. furi_assert(instance);
  238. SubGhzProtocolCommonLoad* data = context;
  239. instance->common.code_last_found = data->code_found;
  240. instance->common.code_last_count_bit = data->code_count_bit;
  241. subghz_nice_flor_s_decoder_decrypt(instance);
  242. }