keeloq.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701
  1. #include "keeloq.h"
  2. #include "keeloq_common.h"
  3. #include "../subghz_keystore.h"
  4. #include <m-string.h>
  5. #include <m-array.h>
  6. #include "../blocks/const.h"
  7. #include "../blocks/decoder.h"
  8. #include "../blocks/encoder.h"
  9. #include "../blocks/generic.h"
  10. #include "../blocks/math.h"
  11. #define TAG "SubGhzProtocolKeeloq"
  12. static const SubGhzBlockConst subghz_protocol_keeloq_const = {
  13. .te_short = 400,
  14. .te_long = 800,
  15. .te_delta = 140,
  16. .min_count_bit_for_found = 64,
  17. };
  18. struct SubGhzProtocolDecoderKeeloq {
  19. SubGhzProtocolDecoderBase base;
  20. SubGhzBlockDecoder decoder;
  21. SubGhzBlockGeneric generic;
  22. uint16_t header_count;
  23. SubGhzKeystore* keystore;
  24. const char* manufacture_name;
  25. };
  26. struct SubGhzProtocolEncoderKeeloq {
  27. SubGhzProtocolEncoderBase base;
  28. SubGhzProtocolBlockEncoder encoder;
  29. SubGhzBlockGeneric generic;
  30. SubGhzKeystore* keystore;
  31. const char* manufacture_name;
  32. };
  33. typedef enum {
  34. KeeloqDecoderStepReset = 0,
  35. KeeloqDecoderStepCheckPreambula,
  36. KeeloqDecoderStepSaveDuration,
  37. KeeloqDecoderStepCheckDuration,
  38. } KeeloqDecoderStep;
  39. const SubGhzProtocolDecoder subghz_protocol_keeloq_decoder = {
  40. .alloc = subghz_protocol_decoder_keeloq_alloc,
  41. .free = subghz_protocol_decoder_keeloq_free,
  42. .feed = subghz_protocol_decoder_keeloq_feed,
  43. .reset = subghz_protocol_decoder_keeloq_reset,
  44. .get_hash_data = subghz_protocol_decoder_keeloq_get_hash_data,
  45. .serialize = subghz_protocol_decoder_keeloq_serialize,
  46. .deserialize = subghz_protocol_decoder_keeloq_deserialize,
  47. .get_string = subghz_protocol_decoder_keeloq_get_string,
  48. };
  49. const SubGhzProtocolEncoder subghz_protocol_keeloq_encoder = {
  50. .alloc = subghz_protocol_encoder_keeloq_alloc,
  51. .free = subghz_protocol_encoder_keeloq_free,
  52. .deserialize = subghz_protocol_encoder_keeloq_deserialize,
  53. .stop = subghz_protocol_encoder_keeloq_stop,
  54. .yield = subghz_protocol_encoder_keeloq_yield,
  55. };
  56. const SubGhzProtocol subghz_protocol_keeloq = {
  57. .name = SUBGHZ_PROTOCOL_KEELOQ_NAME,
  58. .type = SubGhzProtocolTypeDynamic,
  59. .flag = SubGhzProtocolFlag_433 | SubGhzProtocolFlag_868 | SubGhzProtocolFlag_315 |
  60. SubGhzProtocolFlag_AM | SubGhzProtocolFlag_Decodable | SubGhzProtocolFlag_Load |
  61. SubGhzProtocolFlag_Send,
  62. .decoder = &subghz_protocol_keeloq_decoder,
  63. .encoder = &subghz_protocol_keeloq_encoder,
  64. };
  65. /**
  66. * Analysis of received data
  67. * @param instance Pointer to a SubGhzBlockGeneric* instance
  68. * @param keystore Pointer to a SubGhzKeystore* instance
  69. * @param manufacture_name
  70. */
  71. static void subghz_protocol_keeloq_check_remote_controller(
  72. SubGhzBlockGeneric* instance,
  73. SubGhzKeystore* keystore,
  74. const char** manufacture_name);
  75. void* subghz_protocol_encoder_keeloq_alloc(SubGhzEnvironment* environment) {
  76. SubGhzProtocolEncoderKeeloq* instance = malloc(sizeof(SubGhzProtocolEncoderKeeloq));
  77. instance->base.protocol = &subghz_protocol_keeloq;
  78. instance->generic.protocol_name = instance->base.protocol->name;
  79. instance->keystore = subghz_environment_get_keystore(environment);
  80. instance->encoder.repeat = 10;
  81. instance->encoder.size_upload = 256;
  82. instance->encoder.upload = malloc(instance->encoder.size_upload * sizeof(LevelDuration));
  83. instance->encoder.is_runing = false;
  84. return instance;
  85. }
  86. void subghz_protocol_encoder_keeloq_free(void* context) {
  87. furi_assert(context);
  88. SubGhzProtocolEncoderKeeloq* instance = context;
  89. free(instance->encoder.upload);
  90. free(instance);
  91. }
  92. /**
  93. * Key generation from simple data
  94. * @param instance Pointer to a SubGhzProtocolEncoderKeeloq* instance
  95. * @param btn Button number, 4 bit
  96. */
  97. static bool subghz_protocol_keeloq_gen_data(SubGhzProtocolEncoderKeeloq* instance, uint8_t btn) {
  98. instance->generic.cnt++;
  99. uint32_t fix = btn << 28 | instance->generic.serial;
  100. uint32_t decrypt = btn << 28 |
  101. (instance->generic.serial & 0x3FF)
  102. << 16 | //ToDo in some protocols the discriminator is 0
  103. instance->generic.cnt;
  104. uint32_t hop = 0;
  105. uint64_t man = 0;
  106. int res = 0;
  107. for
  108. M_EACH(manufacture_code, *subghz_keystore_get_data(instance->keystore), SubGhzKeyArray_t) {
  109. res = strcmp(string_get_cstr(manufacture_code->name), instance->manufacture_name);
  110. if(res == 0) {
  111. switch(manufacture_code->type) {
  112. case KEELOQ_LEARNING_SIMPLE:
  113. //Simple Learning
  114. hop = subghz_protocol_keeloq_common_encrypt(decrypt, manufacture_code->key);
  115. break;
  116. case KEELOQ_LEARNING_NORMAL:
  117. //Simple Learning
  118. man =
  119. subghz_protocol_keeloq_common_normal_learning(fix, manufacture_code->key);
  120. hop = subghz_protocol_keeloq_common_encrypt(decrypt, man);
  121. break;
  122. case KEELOQ_LEARNING_MAGIC_XOR_TYPE_1:
  123. man = subghz_protocol_keeloq_common_magic_xor_type1_learning(
  124. instance->generic.serial, manufacture_code->key);
  125. hop = subghz_protocol_keeloq_common_encrypt(decrypt, man);
  126. break;
  127. case KEELOQ_LEARNING_UNKNOWN:
  128. hop = 0; //todo
  129. break;
  130. }
  131. break;
  132. }
  133. }
  134. if(hop) {
  135. uint64_t yek = (uint64_t)fix << 32 | hop;
  136. instance->generic.data =
  137. subghz_protocol_blocks_reverse_key(yek, instance->generic.data_count_bit);
  138. return true;
  139. } else {
  140. instance->manufacture_name = "Unknown";
  141. return false;
  142. }
  143. }
  144. bool subghz_protocol_keeloq_create_data(
  145. void* context,
  146. FlipperFormat* flipper_format,
  147. uint32_t serial,
  148. uint8_t btn,
  149. uint16_t cnt,
  150. const char* manufacture_name,
  151. uint32_t frequency,
  152. FuriHalSubGhzPreset preset) {
  153. furi_assert(context);
  154. SubGhzProtocolEncoderKeeloq* instance = context;
  155. instance->generic.serial = serial;
  156. instance->generic.cnt = cnt;
  157. instance->manufacture_name = manufacture_name;
  158. instance->generic.data_count_bit = 64;
  159. bool res = subghz_protocol_keeloq_gen_data(instance, btn);
  160. if(res) {
  161. res =
  162. subghz_block_generic_serialize(&instance->generic, flipper_format, frequency, preset);
  163. }
  164. return res;
  165. }
  166. /**
  167. * Generating an upload from data.
  168. * @param instance Pointer to a SubGhzProtocolEncoderKeeloq instance
  169. * @return true On success
  170. */
  171. static bool
  172. subghz_protocol_encoder_keeloq_get_upload(SubGhzProtocolEncoderKeeloq* instance, uint8_t btn) {
  173. furi_assert(instance);
  174. //gen new key
  175. if(subghz_protocol_keeloq_gen_data(instance, btn)) {
  176. //ToDo if you need to add a callback to automatically update the data on the display
  177. } else {
  178. return false;
  179. }
  180. size_t index = 0;
  181. size_t size_upload = 11 * 2 + 2 + (instance->generic.data_count_bit * 2) + 4;
  182. if(size_upload > instance->encoder.size_upload) {
  183. FURI_LOG_E(TAG, "Size upload exceeds allocated encoder buffer.");
  184. return false;
  185. } else {
  186. instance->encoder.size_upload = size_upload;
  187. }
  188. //Send header
  189. for(uint8_t i = 11; i > 0; i--) {
  190. instance->encoder.upload[index++] =
  191. level_duration_make(true, (uint32_t)subghz_protocol_keeloq_const.te_short);
  192. instance->encoder.upload[index++] =
  193. level_duration_make(false, (uint32_t)subghz_protocol_keeloq_const.te_short);
  194. }
  195. instance->encoder.upload[index++] =
  196. level_duration_make(true, (uint32_t)subghz_protocol_keeloq_const.te_short);
  197. instance->encoder.upload[index++] =
  198. level_duration_make(false, (uint32_t)subghz_protocol_keeloq_const.te_short * 10);
  199. //Send key data
  200. for(uint8_t i = instance->generic.data_count_bit; i > 0; i--) {
  201. if(bit_read(instance->generic.data, i - 1)) {
  202. //send bit 1
  203. instance->encoder.upload[index++] =
  204. level_duration_make(true, (uint32_t)subghz_protocol_keeloq_const.te_short);
  205. instance->encoder.upload[index++] =
  206. level_duration_make(false, (uint32_t)subghz_protocol_keeloq_const.te_long);
  207. } else {
  208. //send bit 0
  209. instance->encoder.upload[index++] =
  210. level_duration_make(true, (uint32_t)subghz_protocol_keeloq_const.te_long);
  211. instance->encoder.upload[index++] =
  212. level_duration_make(false, (uint32_t)subghz_protocol_keeloq_const.te_short);
  213. }
  214. }
  215. // +send 2 status bit
  216. instance->encoder.upload[index++] =
  217. level_duration_make(true, (uint32_t)subghz_protocol_keeloq_const.te_short);
  218. instance->encoder.upload[index++] =
  219. level_duration_make(false, (uint32_t)subghz_protocol_keeloq_const.te_long);
  220. // send end
  221. instance->encoder.upload[index++] =
  222. level_duration_make(true, (uint32_t)subghz_protocol_keeloq_const.te_short);
  223. instance->encoder.upload[index++] =
  224. level_duration_make(false, (uint32_t)subghz_protocol_keeloq_const.te_short * 40);
  225. return true;
  226. }
  227. bool subghz_protocol_encoder_keeloq_deserialize(void* context, FlipperFormat* flipper_format) {
  228. furi_assert(context);
  229. SubGhzProtocolEncoderKeeloq* instance = context;
  230. bool res = false;
  231. do {
  232. if(!subghz_block_generic_deserialize(&instance->generic, flipper_format)) {
  233. FURI_LOG_E(TAG, "Deserialize error");
  234. break;
  235. }
  236. subghz_protocol_keeloq_check_remote_controller(
  237. &instance->generic, instance->keystore, &instance->manufacture_name);
  238. if(strcmp(instance->manufacture_name, "DoorHan")) {
  239. break;
  240. }
  241. //optional parameter parameter
  242. flipper_format_read_uint32(
  243. flipper_format, "Repeat", (uint32_t*)&instance->encoder.repeat, 1);
  244. subghz_protocol_encoder_keeloq_get_upload(instance, instance->generic.btn);
  245. if(!flipper_format_rewind(flipper_format)) {
  246. FURI_LOG_E(TAG, "Rewind error");
  247. break;
  248. }
  249. uint8_t key_data[sizeof(uint64_t)] = {0};
  250. for(size_t i = 0; i < sizeof(uint64_t); i++) {
  251. key_data[sizeof(uint64_t) - i - 1] = (instance->generic.data >> i * 8) & 0xFF;
  252. }
  253. if(!flipper_format_update_hex(flipper_format, "Key", key_data, sizeof(uint64_t))) {
  254. FURI_LOG_E(TAG, "Unable to add Key");
  255. break;
  256. }
  257. instance->encoder.is_runing = true;
  258. res = true;
  259. } while(false);
  260. return res;
  261. }
  262. void subghz_protocol_encoder_keeloq_stop(void* context) {
  263. SubGhzProtocolEncoderKeeloq* instance = context;
  264. instance->encoder.is_runing = false;
  265. }
  266. LevelDuration subghz_protocol_encoder_keeloq_yield(void* context) {
  267. SubGhzProtocolEncoderKeeloq* instance = context;
  268. if(instance->encoder.repeat == 0 || !instance->encoder.is_runing) {
  269. instance->encoder.is_runing = false;
  270. return level_duration_reset();
  271. }
  272. LevelDuration ret = instance->encoder.upload[instance->encoder.front];
  273. if(++instance->encoder.front == instance->encoder.size_upload) {
  274. instance->encoder.repeat--;
  275. instance->encoder.front = 0;
  276. }
  277. return ret;
  278. }
  279. void* subghz_protocol_decoder_keeloq_alloc(SubGhzEnvironment* environment) {
  280. SubGhzProtocolDecoderKeeloq* instance = malloc(sizeof(SubGhzProtocolDecoderKeeloq));
  281. instance->base.protocol = &subghz_protocol_keeloq;
  282. instance->generic.protocol_name = instance->base.protocol->name;
  283. instance->keystore = subghz_environment_get_keystore(environment);
  284. return instance;
  285. }
  286. void subghz_protocol_decoder_keeloq_free(void* context) {
  287. furi_assert(context);
  288. SubGhzProtocolDecoderKeeloq* instance = context;
  289. free(instance);
  290. }
  291. void subghz_protocol_decoder_keeloq_reset(void* context) {
  292. furi_assert(context);
  293. SubGhzProtocolDecoderKeeloq* instance = context;
  294. instance->decoder.parser_step = KeeloqDecoderStepReset;
  295. }
  296. void subghz_protocol_decoder_keeloq_feed(void* context, bool level, uint32_t duration) {
  297. furi_assert(context);
  298. SubGhzProtocolDecoderKeeloq* instance = context;
  299. switch(instance->decoder.parser_step) {
  300. case KeeloqDecoderStepReset:
  301. if((level) && DURATION_DIFF(duration, subghz_protocol_keeloq_const.te_short) <
  302. subghz_protocol_keeloq_const.te_delta) {
  303. instance->decoder.parser_step = KeeloqDecoderStepCheckPreambula;
  304. instance->header_count++;
  305. }
  306. break;
  307. case KeeloqDecoderStepCheckPreambula:
  308. if((!level) && (DURATION_DIFF(duration, subghz_protocol_keeloq_const.te_short) <
  309. subghz_protocol_keeloq_const.te_delta)) {
  310. instance->decoder.parser_step = KeeloqDecoderStepReset;
  311. break;
  312. }
  313. if((instance->header_count > 2) &&
  314. (DURATION_DIFF(duration, subghz_protocol_keeloq_const.te_short * 10) <
  315. subghz_protocol_keeloq_const.te_delta * 10)) {
  316. // Found header
  317. instance->decoder.parser_step = KeeloqDecoderStepSaveDuration;
  318. instance->decoder.decode_data = 0;
  319. instance->decoder.decode_count_bit = 0;
  320. } else {
  321. instance->decoder.parser_step = KeeloqDecoderStepReset;
  322. instance->header_count = 0;
  323. }
  324. break;
  325. case KeeloqDecoderStepSaveDuration:
  326. if(level) {
  327. instance->decoder.te_last = duration;
  328. instance->decoder.parser_step = KeeloqDecoderStepCheckDuration;
  329. }
  330. break;
  331. case KeeloqDecoderStepCheckDuration:
  332. if(!level) {
  333. if(duration >= ((uint32_t)subghz_protocol_keeloq_const.te_short * 2 +
  334. subghz_protocol_keeloq_const.te_delta)) {
  335. // Found end TX
  336. instance->decoder.parser_step = KeeloqDecoderStepReset;
  337. if(instance->decoder.decode_count_bit >=
  338. subghz_protocol_keeloq_const.min_count_bit_for_found) {
  339. if(instance->generic.data != instance->decoder.decode_data) {
  340. instance->generic.data = instance->decoder.decode_data;
  341. instance->generic.data_count_bit = instance->decoder.decode_count_bit;
  342. if(instance->base.callback)
  343. instance->base.callback(&instance->base, instance->base.context);
  344. }
  345. instance->decoder.decode_data = 0;
  346. instance->decoder.decode_count_bit = 0;
  347. instance->header_count = 0;
  348. }
  349. break;
  350. } else if(
  351. (DURATION_DIFF(instance->decoder.te_last, subghz_protocol_keeloq_const.te_short) <
  352. subghz_protocol_keeloq_const.te_delta) &&
  353. (DURATION_DIFF(duration, subghz_protocol_keeloq_const.te_long) <
  354. subghz_protocol_keeloq_const.te_delta)) {
  355. if(instance->decoder.decode_count_bit <
  356. subghz_protocol_keeloq_const.min_count_bit_for_found) {
  357. subghz_protocol_blocks_add_bit(&instance->decoder, 1);
  358. }
  359. instance->decoder.parser_step = KeeloqDecoderStepSaveDuration;
  360. } else if(
  361. (DURATION_DIFF(instance->decoder.te_last, subghz_protocol_keeloq_const.te_long) <
  362. subghz_protocol_keeloq_const.te_delta) &&
  363. (DURATION_DIFF(duration, subghz_protocol_keeloq_const.te_short) <
  364. subghz_protocol_keeloq_const.te_delta)) {
  365. if(instance->decoder.decode_count_bit <
  366. subghz_protocol_keeloq_const.min_count_bit_for_found) {
  367. subghz_protocol_blocks_add_bit(&instance->decoder, 0);
  368. }
  369. instance->decoder.parser_step = KeeloqDecoderStepSaveDuration;
  370. } else {
  371. instance->decoder.parser_step = KeeloqDecoderStepReset;
  372. instance->header_count = 0;
  373. }
  374. } else {
  375. instance->decoder.parser_step = KeeloqDecoderStepReset;
  376. instance->header_count = 0;
  377. }
  378. break;
  379. }
  380. }
  381. /**
  382. * Validation of decrypt data.
  383. * @param instance Pointer to a SubGhzBlockGeneric instance
  384. * @param decrypt Decrypd data
  385. * @param btn Button number, 4 bit
  386. * @param end_serial decrement the last 10 bits of the serial number
  387. * @return true On success
  388. */
  389. static inline bool subghz_protocol_keeloq_check_decrypt(
  390. SubGhzBlockGeneric* instance,
  391. uint32_t decrypt,
  392. uint8_t btn,
  393. uint32_t end_serial) {
  394. furi_assert(instance);
  395. if((decrypt >> 28 == btn) && (((((uint16_t)(decrypt >> 16)) & 0xFF) == end_serial) ||
  396. ((((uint16_t)(decrypt >> 16)) & 0xFF) == 0))) {
  397. instance->cnt = decrypt & 0x0000FFFF;
  398. return true;
  399. }
  400. return false;
  401. }
  402. /**
  403. * Checking the accepted code against the database manafacture key
  404. * @param instance Pointer to a SubGhzBlockGeneric* instance
  405. * @param fix Fix part of the parcel
  406. * @param hop Hop encrypted part of the parcel
  407. * @param keystore Pointer to a SubGhzKeystore* instance
  408. * @param manufacture_name
  409. * @return true on successful search
  410. */
  411. static uint8_t subghz_protocol_keeloq_check_remote_controller_selector(
  412. SubGhzBlockGeneric* instance,
  413. uint32_t fix,
  414. uint32_t hop,
  415. SubGhzKeystore* keystore,
  416. const char** manufacture_name) {
  417. // protocol HCS300 uses 10 bits in discriminator, HCS200 uses 8 bits, for backward compatibility, we are looking for the 8-bit pattern
  418. // HCS300 -> uint16_t end_serial = (uint16_t)(fix & 0x3FF);
  419. // HCS200 -> uint16_t end_serial = (uint16_t)(fix & 0xFF);
  420. uint16_t end_serial = (uint16_t)(fix & 0xFF);
  421. uint8_t btn = (uint8_t)(fix >> 28);
  422. uint32_t decrypt = 0;
  423. uint64_t man;
  424. uint32_t seed = 0;
  425. for
  426. M_EACH(manufacture_code, *subghz_keystore_get_data(keystore), SubGhzKeyArray_t) {
  427. switch(manufacture_code->type) {
  428. case KEELOQ_LEARNING_SIMPLE:
  429. // Simple Learning
  430. decrypt = subghz_protocol_keeloq_common_decrypt(hop, manufacture_code->key);
  431. if(subghz_protocol_keeloq_check_decrypt(instance, decrypt, btn, end_serial)) {
  432. *manufacture_name = string_get_cstr(manufacture_code->name);
  433. return 1;
  434. }
  435. break;
  436. case KEELOQ_LEARNING_NORMAL:
  437. // Normal Learning
  438. // https://phreakerclub.com/forum/showpost.php?p=43557&postcount=37
  439. man = subghz_protocol_keeloq_common_normal_learning(fix, manufacture_code->key);
  440. decrypt = subghz_protocol_keeloq_common_decrypt(hop, man);
  441. if(subghz_protocol_keeloq_check_decrypt(instance, decrypt, btn, end_serial)) {
  442. *manufacture_name = string_get_cstr(manufacture_code->name);
  443. return 1;
  444. }
  445. break;
  446. case KEELOQ_LEARNING_SECURE:
  447. man = subghz_protocol_keeloq_common_secure_learning(
  448. fix, seed, manufacture_code->key);
  449. decrypt = subghz_protocol_keeloq_common_decrypt(hop, man);
  450. if(subghz_protocol_keeloq_check_decrypt(instance, decrypt, btn, end_serial)) {
  451. *manufacture_name = string_get_cstr(manufacture_code->name);
  452. return 1;
  453. }
  454. break;
  455. case KEELOQ_LEARNING_MAGIC_XOR_TYPE_1:
  456. man = subghz_protocol_keeloq_common_magic_xor_type1_learning(
  457. fix, manufacture_code->key);
  458. decrypt = subghz_protocol_keeloq_common_decrypt(hop, man);
  459. if(subghz_protocol_keeloq_check_decrypt(instance, decrypt, btn, end_serial)) {
  460. *manufacture_name = string_get_cstr(manufacture_code->name);
  461. return 1;
  462. }
  463. break;
  464. case KEELOQ_LEARNING_UNKNOWN:
  465. // Simple Learning
  466. decrypt = subghz_protocol_keeloq_common_decrypt(hop, manufacture_code->key);
  467. if(subghz_protocol_keeloq_check_decrypt(instance, decrypt, btn, end_serial)) {
  468. *manufacture_name = string_get_cstr(manufacture_code->name);
  469. return 1;
  470. }
  471. // Check for mirrored man
  472. uint64_t man_rev = 0;
  473. uint64_t man_rev_byte = 0;
  474. for(uint8_t i = 0; i < 64; i += 8) {
  475. man_rev_byte = (uint8_t)(manufacture_code->key >> i);
  476. man_rev = man_rev | man_rev_byte << (56 - i);
  477. }
  478. decrypt = subghz_protocol_keeloq_common_decrypt(hop, man_rev);
  479. if(subghz_protocol_keeloq_check_decrypt(instance, decrypt, btn, end_serial)) {
  480. *manufacture_name = string_get_cstr(manufacture_code->name);
  481. return 1;
  482. }
  483. //###########################
  484. // Normal Learning
  485. // https://phreakerclub.com/forum/showpost.php?p=43557&postcount=37
  486. man = subghz_protocol_keeloq_common_normal_learning(fix, manufacture_code->key);
  487. decrypt = subghz_protocol_keeloq_common_decrypt(hop, man);
  488. if(subghz_protocol_keeloq_check_decrypt(instance, decrypt, btn, end_serial)) {
  489. *manufacture_name = string_get_cstr(manufacture_code->name);
  490. return 1;
  491. }
  492. // Check for mirrored man
  493. man = subghz_protocol_keeloq_common_normal_learning(fix, man_rev);
  494. decrypt = subghz_protocol_keeloq_common_decrypt(hop, man);
  495. if(subghz_protocol_keeloq_check_decrypt(instance, decrypt, btn, end_serial)) {
  496. *manufacture_name = string_get_cstr(manufacture_code->name);
  497. return 1;
  498. }
  499. // Secure Learning
  500. man = subghz_protocol_keeloq_common_secure_learning(
  501. fix, seed, manufacture_code->key);
  502. decrypt = subghz_protocol_keeloq_common_decrypt(hop, man);
  503. if(subghz_protocol_keeloq_check_decrypt(instance, decrypt, btn, end_serial)) {
  504. *manufacture_name = string_get_cstr(manufacture_code->name);
  505. return 1;
  506. }
  507. // Check for mirrored man
  508. man = subghz_protocol_keeloq_common_secure_learning(fix, seed, man_rev);
  509. decrypt = subghz_protocol_keeloq_common_decrypt(hop, man);
  510. if(subghz_protocol_keeloq_check_decrypt(instance, decrypt, btn, end_serial)) {
  511. *manufacture_name = string_get_cstr(manufacture_code->name);
  512. return 1;
  513. }
  514. // Magic xor type1 learning
  515. man = subghz_protocol_keeloq_common_magic_xor_type1_learning(
  516. fix, manufacture_code->key);
  517. decrypt = subghz_protocol_keeloq_common_decrypt(hop, man);
  518. if(subghz_protocol_keeloq_check_decrypt(instance, decrypt, btn, end_serial)) {
  519. *manufacture_name = string_get_cstr(manufacture_code->name);
  520. return 1;
  521. }
  522. // Check for mirrored man
  523. man = subghz_protocol_keeloq_common_magic_xor_type1_learning(fix, man_rev);
  524. decrypt = subghz_protocol_keeloq_common_decrypt(hop, man);
  525. if(subghz_protocol_keeloq_check_decrypt(instance, decrypt, btn, end_serial)) {
  526. *manufacture_name = string_get_cstr(manufacture_code->name);
  527. return 1;
  528. }
  529. break;
  530. }
  531. }
  532. *manufacture_name = "Unknown";
  533. instance->cnt = 0;
  534. return 0;
  535. }
  536. static void subghz_protocol_keeloq_check_remote_controller(
  537. SubGhzBlockGeneric* instance,
  538. SubGhzKeystore* keystore,
  539. const char** manufacture_name) {
  540. uint64_t key = subghz_protocol_blocks_reverse_key(instance->data, instance->data_count_bit);
  541. uint32_t key_fix = key >> 32;
  542. uint32_t key_hop = key & 0x00000000ffffffff;
  543. // Check key AN-Motors
  544. if((key_hop >> 24) == ((key_hop >> 16) & 0x00ff) &&
  545. (key_fix >> 28) == ((key_hop >> 12) & 0x0f) && (key_hop & 0xFFF) == 0x404) {
  546. *manufacture_name = "AN-Motors";
  547. instance->cnt = key_hop >> 16;
  548. } else if((key_hop & 0xFFF) == (0x000) && (key_fix >> 28) == ((key_hop >> 12) & 0x0f)) {
  549. *manufacture_name = "HCS101";
  550. instance->cnt = key_hop >> 16;
  551. } else {
  552. subghz_protocol_keeloq_check_remote_controller_selector(
  553. instance, key_fix, key_hop, keystore, manufacture_name);
  554. }
  555. instance->serial = key_fix & 0x0FFFFFFF;
  556. instance->btn = key_fix >> 28;
  557. }
  558. uint8_t subghz_protocol_decoder_keeloq_get_hash_data(void* context) {
  559. furi_assert(context);
  560. SubGhzProtocolDecoderKeeloq* instance = context;
  561. return subghz_protocol_blocks_get_hash_data(
  562. &instance->decoder, (instance->decoder.decode_count_bit / 8) + 1);
  563. }
  564. bool subghz_protocol_decoder_keeloq_serialize(
  565. void* context,
  566. FlipperFormat* flipper_format,
  567. uint32_t frequency,
  568. FuriHalSubGhzPreset preset) {
  569. furi_assert(context);
  570. SubGhzProtocolDecoderKeeloq* instance = context;
  571. subghz_protocol_keeloq_check_remote_controller(
  572. &instance->generic, instance->keystore, &instance->manufacture_name);
  573. bool res =
  574. subghz_block_generic_serialize(&instance->generic, flipper_format, frequency, preset);
  575. if(res && !flipper_format_write_string_cstr(
  576. flipper_format, "Manufacture", instance->manufacture_name)) {
  577. FURI_LOG_E(TAG, "Unable to add manufacture name");
  578. res = false;
  579. }
  580. return res;
  581. }
  582. bool subghz_protocol_decoder_keeloq_deserialize(void* context, FlipperFormat* flipper_format) {
  583. furi_assert(context);
  584. SubGhzProtocolDecoderKeeloq* instance = context;
  585. bool res = false;
  586. do {
  587. if(!subghz_block_generic_deserialize(&instance->generic, flipper_format)) {
  588. FURI_LOG_E(TAG, "Deserialize error");
  589. break;
  590. }
  591. res = true;
  592. } while(false);
  593. return res;
  594. }
  595. void subghz_protocol_decoder_keeloq_get_string(void* context, string_t output) {
  596. furi_assert(context);
  597. SubGhzProtocolDecoderKeeloq* instance = context;
  598. subghz_protocol_keeloq_check_remote_controller(
  599. &instance->generic, instance->keystore, &instance->manufacture_name);
  600. uint32_t code_found_hi = instance->generic.data >> 32;
  601. uint32_t code_found_lo = instance->generic.data & 0x00000000ffffffff;
  602. uint64_t code_found_reverse = subghz_protocol_blocks_reverse_key(
  603. instance->generic.data, instance->generic.data_count_bit);
  604. uint32_t code_found_reverse_hi = code_found_reverse >> 32;
  605. uint32_t code_found_reverse_lo = code_found_reverse & 0x00000000ffffffff;
  606. string_cat_printf(
  607. output,
  608. "%s %dbit\r\n"
  609. "Key:%08lX%08lX\r\n"
  610. "Fix:0x%08lX Cnt:%04X\r\n"
  611. "Hop:0x%08lX Btn:%01lX\r\n"
  612. "MF:%s\r\n"
  613. "Sn:0x%07lX \r\n",
  614. instance->generic.protocol_name,
  615. instance->generic.data_count_bit,
  616. code_found_hi,
  617. code_found_lo,
  618. code_found_reverse_hi,
  619. instance->generic.cnt,
  620. code_found_reverse_lo,
  621. instance->generic.btn,
  622. instance->manufacture_name,
  623. instance->generic.serial);
  624. }