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