subghz_protocol_keeloq.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. #include "subghz_protocol_keeloq.h"
  2. #include "subghz_protocol_keeloq_common.h"
  3. #include "../subghz_keystore.h"
  4. #include <furi.h>
  5. #include <m-string.h>
  6. struct SubGhzProtocolKeeloq {
  7. SubGhzProtocolCommon common;
  8. SubGhzKeystore* keystore;
  9. const char* manufacture_name;
  10. };
  11. SubGhzProtocolKeeloq* subghz_protocol_keeloq_alloc(SubGhzKeystore* keystore) {
  12. SubGhzProtocolKeeloq* instance = furi_alloc(sizeof(SubGhzProtocolKeeloq));
  13. instance->keystore = keystore;
  14. instance->common.name = "KeeLoq";
  15. instance->common.code_min_count_bit_for_found = 64;
  16. instance->common.te_shot = 400;
  17. instance->common.te_long = 800;
  18. instance->common.te_delta = 140;
  19. instance->common.to_string = (SubGhzProtocolCommonToStr)subghz_protocol_keeloq_to_str;
  20. return instance;
  21. }
  22. void subghz_protocol_keeloq_free(SubGhzProtocolKeeloq* instance) {
  23. furi_assert(instance);
  24. free(instance);
  25. }
  26. /** Checking the accepted code against the database manafacture key
  27. *
  28. * @param instance SubGhzProtocolKeeloq instance
  29. * @param fix fix part of the parcel
  30. * @param hop hop encrypted part of the parcel
  31. * @return true on successful search
  32. */
  33. uint8_t subghz_protocol_keeloq_check_remote_controller_selector(SubGhzProtocolKeeloq* instance, uint32_t fix , uint32_t hop) {
  34. uint16_t end_serial = (uint16_t)(fix&0x3FF);
  35. uint8_t btn = (uint8_t)(fix>>28);
  36. uint32_t decrypt = 0;
  37. uint64_t man_normal_learning;
  38. for
  39. M_EACH(manufacture_code, *subghz_keystore_get_data(instance->keystore), SubGhzKeyArray_t) {
  40. switch (manufacture_code->type){
  41. case KEELOQ_LEARNING_SIMPLE:
  42. //Simple Learning
  43. decrypt = subghz_protocol_keeloq_common_decrypt(hop, manufacture_code->key);
  44. if((decrypt>>28 == btn) && ((((uint16_t)(decrypt>>16)) & 0x3FF) == end_serial)){
  45. instance->manufacture_name = string_get_cstr(manufacture_code->name);
  46. instance->common.cnt = decrypt & 0x0000FFFF;
  47. return 1;
  48. }
  49. break;
  50. case KEELOQ_LEARNING_NORMAL:
  51. // Normal_Learning
  52. // https://phreakerclub.com/forum/showpost.php?p=43557&postcount=37
  53. man_normal_learning = subghz_protocol_keeloq_common_normal_learning(fix, manufacture_code->key);
  54. decrypt=subghz_protocol_keeloq_common_decrypt(hop, man_normal_learning);
  55. if( (decrypt>>28 ==btn)&& ((((uint16_t)(decrypt>>16))&0x3FF)==end_serial)){
  56. instance->manufacture_name = string_get_cstr(manufacture_code->name);
  57. instance->common.cnt = decrypt & 0x0000FFFF;
  58. return 1;
  59. }
  60. break;
  61. case KEELOQ_LEARNING_UNKNOWN:
  62. // Simple Learning
  63. decrypt=subghz_protocol_keeloq_common_decrypt(hop, manufacture_code->key);
  64. if( (decrypt>>28 ==btn) && ((((uint16_t)(decrypt>>16))&0x3FF)==end_serial)){
  65. instance->manufacture_name = string_get_cstr(manufacture_code->name);
  66. instance->common.cnt = decrypt & 0x0000FFFF;
  67. return 1;
  68. }
  69. // Check for mirrored man
  70. uint64_t man_rev=0;
  71. uint64_t man_rev_byte=0;
  72. for(uint8_t i=0; i<64; i+=8){
  73. man_rev_byte=(uint8_t)(manufacture_code->key >> i);
  74. man_rev = man_rev | man_rev_byte << (56-i);
  75. }
  76. decrypt=subghz_protocol_keeloq_common_decrypt(hop, man_rev);
  77. if( (decrypt>>28 ==btn) && ((((uint16_t)(decrypt>>16))&0x3FF)==end_serial)){
  78. instance->manufacture_name = string_get_cstr(manufacture_code->name);
  79. instance->common.cnt= decrypt&0x0000FFFF;
  80. return 1;
  81. }
  82. //###########################
  83. // Normal_Learning
  84. // https://phreakerclub.com/forum/showpost.php?p=43557&postcount=37
  85. man_normal_learning = subghz_protocol_keeloq_common_normal_learning(fix, manufacture_code->key);
  86. decrypt=subghz_protocol_keeloq_common_decrypt(hop, man_normal_learning);
  87. if( (decrypt>>28 ==btn)&& ((((uint16_t)(decrypt>>16))&0x3FF)==end_serial)){
  88. instance->manufacture_name = string_get_cstr(manufacture_code->name);
  89. instance->common.cnt= decrypt&0x0000FFFF;
  90. return 1;
  91. }
  92. // Check for mirrored man
  93. man_rev=0;
  94. man_rev_byte=0;
  95. for(uint8_t i=0; i<64; i+=8){
  96. man_rev_byte = (uint8_t)(manufacture_code->key >> i);
  97. man_rev = man_rev | man_rev_byte << (56-i);
  98. }
  99. man_normal_learning = subghz_protocol_keeloq_common_normal_learning(fix, man_rev);
  100. decrypt=subghz_protocol_keeloq_common_decrypt(hop, man_normal_learning);
  101. if( (decrypt>>28 ==btn) && ((((uint16_t)(decrypt>>16))&0x3FF)==end_serial)){
  102. instance->manufacture_name = string_get_cstr(manufacture_code->name);
  103. instance->common.cnt= decrypt&0x0000FFFF;
  104. return 1;
  105. }
  106. break;
  107. }
  108. }
  109. instance->manufacture_name = "Unknown";
  110. instance->common.cnt=0;
  111. return 0;
  112. }
  113. /** Analysis of received data
  114. *
  115. * @param instance SubGhzProtocolKeeloq instance
  116. */
  117. void subghz_protocol_keeloq_check_remote_controller(SubGhzProtocolKeeloq* instance) {
  118. uint64_t key = subghz_protocol_common_reverse_key(instance->common.code_found, instance->common.code_count_bit);
  119. uint32_t key_fix = key >> 32;
  120. uint32_t key_hop = key & 0x00000000ffffffff;
  121. // Check key AN-Motors
  122. if((key_hop >> 24) == ((key_hop>>16)&0x00ff) && (key_fix>>28) ==((key_hop>>12)&0x0f) && (key_hop & 0xFFF ) == 0x404){
  123. instance->manufacture_name = "AN-Motors";
  124. instance->common.cnt = key_hop>>16;
  125. } else if((key_hop & 0xFFF) == (0x000) && (key_fix>>28) ==((key_hop>>12)&0x0f) ){
  126. instance->manufacture_name = "HCS101";
  127. instance->common.cnt = key_hop>>16;
  128. } else {
  129. subghz_protocol_keeloq_check_remote_controller_selector(instance, key_fix, key_hop);
  130. }
  131. instance ->common.serial= key_fix&0x0FFFFFFF;
  132. instance->common.btn = key_fix >> 28;
  133. if (instance->common.callback) instance->common.callback((SubGhzProtocolCommon*)instance, instance->common.context);
  134. }
  135. /** Send bit
  136. *
  137. * @param instance - SubGhzProtocolKeeloq instance
  138. * @param bit - bit
  139. */
  140. void subghz_protocol_keeloq_send_bit(SubGhzProtocolKeeloq* instance, uint8_t bit) {
  141. if (bit) {
  142. // send bit 1
  143. SUBGHZ_TX_PIN_HIGTH();
  144. delay_us(instance->common.te_shot);
  145. SUBGHZ_TX_PIN_LOW();
  146. delay_us(instance->common.te_long);
  147. } else {
  148. // send bit 0
  149. SUBGHZ_TX_PIN_HIGTH();
  150. delay_us(instance->common.te_long);
  151. SUBGHZ_TX_PIN_LOW();
  152. delay_us(instance->common.te_shot);
  153. }
  154. }
  155. void subghz_protocol_keeloq_send_key(SubGhzProtocolKeeloq* instance, uint64_t key, uint8_t bit, uint8_t repeat) {
  156. while (repeat--) {
  157. // Send header
  158. for (uint8_t i = 11; i > 0; i--) {
  159. SUBGHZ_TX_PIN_HIGTH();
  160. delay_us(instance->common.te_shot);
  161. SUBGHZ_TX_PIN_LOW();
  162. delay_us(instance->common.te_shot);
  163. }
  164. delay_us(instance->common.te_shot * 9); //+1 up Send header
  165. for (uint8_t i = bit; i > 0; i--) {
  166. subghz_protocol_keeloq_send_bit(instance, bit_read(key, i - 1));
  167. }
  168. // +send 2 status bit
  169. subghz_protocol_keeloq_send_bit(instance, 0);
  170. subghz_protocol_keeloq_send_bit(instance, 0);
  171. // send end
  172. subghz_protocol_keeloq_send_bit(instance, 0);
  173. delay_us(instance->common.te_shot * 2); //+2 interval END SEND
  174. }
  175. }
  176. void subghz_protocol_keeloq_reset(SubGhzProtocolKeeloq* instance) {
  177. instance->common.parser_step = 0;
  178. }
  179. void subghz_protocol_keeloq_parse(SubGhzProtocolKeeloq* instance, bool level, uint32_t duration) {
  180. switch (instance->common.parser_step) {
  181. case 0:
  182. if ((level) && DURATION_DIFF(duration, instance->common.te_shot)< instance->common.te_delta) {
  183. instance->common.parser_step = 1;
  184. instance->common.header_count++;
  185. } else {
  186. instance->common.parser_step = 0;
  187. }
  188. break;
  189. case 1:
  190. if ((!level) && (DURATION_DIFF(duration, instance->common.te_shot ) < instance->common.te_delta)) {
  191. instance->common.parser_step = 0;
  192. break;
  193. }
  194. if ((instance->common.header_count > 2) && ( DURATION_DIFF(duration, instance->common.te_shot * 10)< instance->common.te_delta * 10)) {
  195. // Found header
  196. instance->common.parser_step = 2;
  197. instance->common.code_found = 0;
  198. instance->common.code_count_bit = 0;
  199. } else {
  200. instance->common.parser_step = 0;
  201. instance->common.header_count = 0;
  202. }
  203. break;
  204. case 2:
  205. if (level) {
  206. instance->common.te_last = duration;
  207. instance->common.parser_step = 3;
  208. }
  209. break;
  210. case 3:
  211. if (!level) {
  212. if (duration >= (instance->common.te_shot * 2 + instance->common.te_delta)) {
  213. // Found end TX
  214. instance->common.parser_step = 0;
  215. if (instance->common.code_count_bit >= instance->common.code_min_count_bit_for_found) {
  216. if(instance->common.code_last_found != instance->common.code_found ){
  217. subghz_protocol_keeloq_check_remote_controller(instance);
  218. }
  219. instance->common.code_last_found = instance->common.code_found;
  220. instance->common.code_found = 0;
  221. instance->common.code_count_bit = 0;
  222. instance->common.header_count = 0;
  223. }
  224. break;
  225. } else if ((DURATION_DIFF(instance->common.te_last, instance->common.te_shot) < instance->common.te_delta)
  226. && (DURATION_DIFF(duration, instance->common.te_long) < instance->common.te_delta)) {
  227. if (instance->common.code_count_bit < instance->common.code_min_count_bit_for_found) {
  228. subghz_protocol_common_add_bit(&instance->common, 1);
  229. }
  230. instance->common.parser_step = 2;
  231. } else if ((DURATION_DIFF(instance->common.te_last, instance->common.te_long) < instance->common.te_delta)
  232. && (DURATION_DIFF(duration, instance->common.te_shot) < instance->common.te_delta)) {
  233. if (instance->common.code_count_bit < instance->common.code_min_count_bit_for_found) {
  234. subghz_protocol_common_add_bit(&instance->common, 0);
  235. }
  236. instance->common.parser_step = 2;
  237. } else {
  238. instance->common.parser_step = 0;
  239. instance->common.header_count = 0;
  240. }
  241. } else {
  242. instance->common.parser_step = 0;
  243. instance->common.header_count = 0;
  244. }
  245. break;
  246. }
  247. }
  248. void subghz_protocol_keeloq_to_str(SubGhzProtocolKeeloq* instance, string_t output) {
  249. uint32_t code_found_hi = instance->common.code_found >> 32;
  250. uint32_t code_found_lo = instance->common.code_found & 0x00000000ffffffff;
  251. uint64_t code_found_reverse = subghz_protocol_common_reverse_key(instance->common.code_found, instance->common.code_count_bit);
  252. uint32_t code_found_reverse_hi = code_found_reverse>>32;
  253. uint32_t code_found_reverse_lo = code_found_reverse&0x00000000ffffffff;
  254. string_cat_printf(
  255. output,
  256. "Protocol %s, %d Bit\r\n"
  257. "KEY:0x%lX%lX\r\n"
  258. "FIX:%08lX MF:%s \r\n"
  259. "HOP:%08lX \r\n"
  260. "SN:%07lX CNT:%04X B:%02lX\r\n",
  261. instance->common.name,
  262. instance->common.code_count_bit,
  263. code_found_hi,
  264. code_found_lo,
  265. code_found_reverse_hi,
  266. instance->manufacture_name,
  267. code_found_reverse_lo,
  268. instance->common.serial,
  269. instance->common.cnt,
  270. instance->common.btn
  271. );
  272. }