subghz_protocol_nice_flor_s.c 10 KB

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