subghz_protocol_nice_flor_s.c 9.2 KB

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