subghz_protocol_nice_flor_s.c 9.7 KB

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