keeloq.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697
  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. SubGhzPesetDefinition* preset) {
  152. furi_assert(context);
  153. SubGhzProtocolEncoderKeeloq* instance = context;
  154. instance->generic.serial = serial;
  155. instance->generic.cnt = cnt;
  156. instance->manufacture_name = manufacture_name;
  157. instance->generic.data_count_bit = 64;
  158. bool res = subghz_protocol_keeloq_gen_data(instance, btn);
  159. if(res) {
  160. res = subghz_block_generic_serialize(&instance->generic, flipper_format, preset);
  161. }
  162. return res;
  163. }
  164. /**
  165. * Generating an upload from data.
  166. * @param instance Pointer to a SubGhzProtocolEncoderKeeloq instance
  167. * @return true On success
  168. */
  169. static bool
  170. subghz_protocol_encoder_keeloq_get_upload(SubGhzProtocolEncoderKeeloq* instance, uint8_t btn) {
  171. furi_assert(instance);
  172. //gen new key
  173. if(subghz_protocol_keeloq_gen_data(instance, btn)) {
  174. //ToDo if you need to add a callback to automatically update the data on the display
  175. } else {
  176. return false;
  177. }
  178. size_t index = 0;
  179. size_t size_upload = 11 * 2 + 2 + (instance->generic.data_count_bit * 2) + 4;
  180. if(size_upload > instance->encoder.size_upload) {
  181. FURI_LOG_E(TAG, "Size upload exceeds allocated encoder buffer.");
  182. return false;
  183. } else {
  184. instance->encoder.size_upload = size_upload;
  185. }
  186. //Send header
  187. for(uint8_t i = 11; i > 0; i--) {
  188. instance->encoder.upload[index++] =
  189. level_duration_make(true, (uint32_t)subghz_protocol_keeloq_const.te_short);
  190. instance->encoder.upload[index++] =
  191. level_duration_make(false, (uint32_t)subghz_protocol_keeloq_const.te_short);
  192. }
  193. instance->encoder.upload[index++] =
  194. level_duration_make(true, (uint32_t)subghz_protocol_keeloq_const.te_short);
  195. instance->encoder.upload[index++] =
  196. level_duration_make(false, (uint32_t)subghz_protocol_keeloq_const.te_short * 10);
  197. //Send key data
  198. for(uint8_t i = instance->generic.data_count_bit; i > 0; i--) {
  199. if(bit_read(instance->generic.data, i - 1)) {
  200. //send bit 1
  201. instance->encoder.upload[index++] =
  202. level_duration_make(true, (uint32_t)subghz_protocol_keeloq_const.te_short);
  203. instance->encoder.upload[index++] =
  204. level_duration_make(false, (uint32_t)subghz_protocol_keeloq_const.te_long);
  205. } else {
  206. //send bit 0
  207. instance->encoder.upload[index++] =
  208. level_duration_make(true, (uint32_t)subghz_protocol_keeloq_const.te_long);
  209. instance->encoder.upload[index++] =
  210. level_duration_make(false, (uint32_t)subghz_protocol_keeloq_const.te_short);
  211. }
  212. }
  213. // +send 2 status bit
  214. instance->encoder.upload[index++] =
  215. level_duration_make(true, (uint32_t)subghz_protocol_keeloq_const.te_short);
  216. instance->encoder.upload[index++] =
  217. level_duration_make(false, (uint32_t)subghz_protocol_keeloq_const.te_long);
  218. // send end
  219. instance->encoder.upload[index++] =
  220. level_duration_make(true, (uint32_t)subghz_protocol_keeloq_const.te_short);
  221. instance->encoder.upload[index++] =
  222. level_duration_make(false, (uint32_t)subghz_protocol_keeloq_const.te_short * 40);
  223. return true;
  224. }
  225. bool subghz_protocol_encoder_keeloq_deserialize(void* context, FlipperFormat* flipper_format) {
  226. furi_assert(context);
  227. SubGhzProtocolEncoderKeeloq* instance = context;
  228. bool res = false;
  229. do {
  230. if(!subghz_block_generic_deserialize(&instance->generic, flipper_format)) {
  231. FURI_LOG_E(TAG, "Deserialize error");
  232. break;
  233. }
  234. subghz_protocol_keeloq_check_remote_controller(
  235. &instance->generic, instance->keystore, &instance->manufacture_name);
  236. if(strcmp(instance->manufacture_name, "DoorHan")) {
  237. break;
  238. }
  239. //optional parameter parameter
  240. flipper_format_read_uint32(
  241. flipper_format, "Repeat", (uint32_t*)&instance->encoder.repeat, 1);
  242. subghz_protocol_encoder_keeloq_get_upload(instance, instance->generic.btn);
  243. if(!flipper_format_rewind(flipper_format)) {
  244. FURI_LOG_E(TAG, "Rewind error");
  245. break;
  246. }
  247. uint8_t key_data[sizeof(uint64_t)] = {0};
  248. for(size_t i = 0; i < sizeof(uint64_t); i++) {
  249. key_data[sizeof(uint64_t) - i - 1] = (instance->generic.data >> i * 8) & 0xFF;
  250. }
  251. if(!flipper_format_update_hex(flipper_format, "Key", key_data, sizeof(uint64_t))) {
  252. FURI_LOG_E(TAG, "Unable to add Key");
  253. break;
  254. }
  255. instance->encoder.is_runing = true;
  256. res = true;
  257. } while(false);
  258. return res;
  259. }
  260. void subghz_protocol_encoder_keeloq_stop(void* context) {
  261. SubGhzProtocolEncoderKeeloq* instance = context;
  262. instance->encoder.is_runing = false;
  263. }
  264. LevelDuration subghz_protocol_encoder_keeloq_yield(void* context) {
  265. SubGhzProtocolEncoderKeeloq* instance = context;
  266. if(instance->encoder.repeat == 0 || !instance->encoder.is_runing) {
  267. instance->encoder.is_runing = false;
  268. return level_duration_reset();
  269. }
  270. LevelDuration ret = instance->encoder.upload[instance->encoder.front];
  271. if(++instance->encoder.front == instance->encoder.size_upload) {
  272. instance->encoder.repeat--;
  273. instance->encoder.front = 0;
  274. }
  275. return ret;
  276. }
  277. void* subghz_protocol_decoder_keeloq_alloc(SubGhzEnvironment* environment) {
  278. SubGhzProtocolDecoderKeeloq* instance = malloc(sizeof(SubGhzProtocolDecoderKeeloq));
  279. instance->base.protocol = &subghz_protocol_keeloq;
  280. instance->generic.protocol_name = instance->base.protocol->name;
  281. instance->keystore = subghz_environment_get_keystore(environment);
  282. return instance;
  283. }
  284. void subghz_protocol_decoder_keeloq_free(void* context) {
  285. furi_assert(context);
  286. SubGhzProtocolDecoderKeeloq* instance = context;
  287. free(instance);
  288. }
  289. void subghz_protocol_decoder_keeloq_reset(void* context) {
  290. furi_assert(context);
  291. SubGhzProtocolDecoderKeeloq* instance = context;
  292. instance->decoder.parser_step = KeeloqDecoderStepReset;
  293. }
  294. void subghz_protocol_decoder_keeloq_feed(void* context, bool level, uint32_t duration) {
  295. furi_assert(context);
  296. SubGhzProtocolDecoderKeeloq* instance = context;
  297. switch(instance->decoder.parser_step) {
  298. case KeeloqDecoderStepReset:
  299. if((level) && DURATION_DIFF(duration, subghz_protocol_keeloq_const.te_short) <
  300. subghz_protocol_keeloq_const.te_delta) {
  301. instance->decoder.parser_step = KeeloqDecoderStepCheckPreambula;
  302. instance->header_count++;
  303. }
  304. break;
  305. case KeeloqDecoderStepCheckPreambula:
  306. if((!level) && (DURATION_DIFF(duration, subghz_protocol_keeloq_const.te_short) <
  307. subghz_protocol_keeloq_const.te_delta)) {
  308. instance->decoder.parser_step = KeeloqDecoderStepReset;
  309. break;
  310. }
  311. if((instance->header_count > 2) &&
  312. (DURATION_DIFF(duration, subghz_protocol_keeloq_const.te_short * 10) <
  313. subghz_protocol_keeloq_const.te_delta * 10)) {
  314. // Found header
  315. instance->decoder.parser_step = KeeloqDecoderStepSaveDuration;
  316. instance->decoder.decode_data = 0;
  317. instance->decoder.decode_count_bit = 0;
  318. } else {
  319. instance->decoder.parser_step = KeeloqDecoderStepReset;
  320. instance->header_count = 0;
  321. }
  322. break;
  323. case KeeloqDecoderStepSaveDuration:
  324. if(level) {
  325. instance->decoder.te_last = duration;
  326. instance->decoder.parser_step = KeeloqDecoderStepCheckDuration;
  327. }
  328. break;
  329. case KeeloqDecoderStepCheckDuration:
  330. if(!level) {
  331. if(duration >= ((uint32_t)subghz_protocol_keeloq_const.te_short * 2 +
  332. subghz_protocol_keeloq_const.te_delta)) {
  333. // Found end TX
  334. instance->decoder.parser_step = KeeloqDecoderStepReset;
  335. if(instance->decoder.decode_count_bit >=
  336. subghz_protocol_keeloq_const.min_count_bit_for_found) {
  337. if(instance->generic.data != instance->decoder.decode_data) {
  338. instance->generic.data = instance->decoder.decode_data;
  339. instance->generic.data_count_bit = instance->decoder.decode_count_bit;
  340. if(instance->base.callback)
  341. instance->base.callback(&instance->base, instance->base.context);
  342. }
  343. instance->decoder.decode_data = 0;
  344. instance->decoder.decode_count_bit = 0;
  345. instance->header_count = 0;
  346. }
  347. break;
  348. } else if(
  349. (DURATION_DIFF(instance->decoder.te_last, subghz_protocol_keeloq_const.te_short) <
  350. subghz_protocol_keeloq_const.te_delta) &&
  351. (DURATION_DIFF(duration, subghz_protocol_keeloq_const.te_long) <
  352. subghz_protocol_keeloq_const.te_delta)) {
  353. if(instance->decoder.decode_count_bit <
  354. subghz_protocol_keeloq_const.min_count_bit_for_found) {
  355. subghz_protocol_blocks_add_bit(&instance->decoder, 1);
  356. }
  357. instance->decoder.parser_step = KeeloqDecoderStepSaveDuration;
  358. } else if(
  359. (DURATION_DIFF(instance->decoder.te_last, subghz_protocol_keeloq_const.te_long) <
  360. subghz_protocol_keeloq_const.te_delta) &&
  361. (DURATION_DIFF(duration, subghz_protocol_keeloq_const.te_short) <
  362. subghz_protocol_keeloq_const.te_delta)) {
  363. if(instance->decoder.decode_count_bit <
  364. subghz_protocol_keeloq_const.min_count_bit_for_found) {
  365. subghz_protocol_blocks_add_bit(&instance->decoder, 0);
  366. }
  367. instance->decoder.parser_step = KeeloqDecoderStepSaveDuration;
  368. } else {
  369. instance->decoder.parser_step = KeeloqDecoderStepReset;
  370. instance->header_count = 0;
  371. }
  372. } else {
  373. instance->decoder.parser_step = KeeloqDecoderStepReset;
  374. instance->header_count = 0;
  375. }
  376. break;
  377. }
  378. }
  379. /**
  380. * Validation of decrypt data.
  381. * @param instance Pointer to a SubGhzBlockGeneric instance
  382. * @param decrypt Decrypd data
  383. * @param btn Button number, 4 bit
  384. * @param end_serial decrement the last 10 bits of the serial number
  385. * @return true On success
  386. */
  387. static inline bool subghz_protocol_keeloq_check_decrypt(
  388. SubGhzBlockGeneric* instance,
  389. uint32_t decrypt,
  390. uint8_t btn,
  391. uint32_t end_serial) {
  392. furi_assert(instance);
  393. if((decrypt >> 28 == btn) && (((((uint16_t)(decrypt >> 16)) & 0xFF) == end_serial) ||
  394. ((((uint16_t)(decrypt >> 16)) & 0xFF) == 0))) {
  395. instance->cnt = decrypt & 0x0000FFFF;
  396. return true;
  397. }
  398. return false;
  399. }
  400. /**
  401. * Checking the accepted code against the database manafacture key
  402. * @param instance Pointer to a SubGhzBlockGeneric* instance
  403. * @param fix Fix part of the parcel
  404. * @param hop Hop encrypted part of the parcel
  405. * @param keystore Pointer to a SubGhzKeystore* instance
  406. * @param manufacture_name
  407. * @return true on successful search
  408. */
  409. static uint8_t subghz_protocol_keeloq_check_remote_controller_selector(
  410. SubGhzBlockGeneric* instance,
  411. uint32_t fix,
  412. uint32_t hop,
  413. SubGhzKeystore* keystore,
  414. const char** manufacture_name) {
  415. // protocol HCS300 uses 10 bits in discriminator, HCS200 uses 8 bits, for backward compatibility, we are looking for the 8-bit pattern
  416. // HCS300 -> uint16_t end_serial = (uint16_t)(fix & 0x3FF);
  417. // HCS200 -> uint16_t end_serial = (uint16_t)(fix & 0xFF);
  418. uint16_t end_serial = (uint16_t)(fix & 0xFF);
  419. uint8_t btn = (uint8_t)(fix >> 28);
  420. uint32_t decrypt = 0;
  421. uint64_t man;
  422. uint32_t seed = 0;
  423. for
  424. M_EACH(manufacture_code, *subghz_keystore_get_data(keystore), SubGhzKeyArray_t) {
  425. switch(manufacture_code->type) {
  426. case KEELOQ_LEARNING_SIMPLE:
  427. // Simple Learning
  428. decrypt = subghz_protocol_keeloq_common_decrypt(hop, manufacture_code->key);
  429. if(subghz_protocol_keeloq_check_decrypt(instance, decrypt, btn, end_serial)) {
  430. *manufacture_name = string_get_cstr(manufacture_code->name);
  431. return 1;
  432. }
  433. break;
  434. case KEELOQ_LEARNING_NORMAL:
  435. // Normal Learning
  436. // https://phreakerclub.com/forum/showpost.php?p=43557&postcount=37
  437. man = subghz_protocol_keeloq_common_normal_learning(fix, manufacture_code->key);
  438. decrypt = subghz_protocol_keeloq_common_decrypt(hop, man);
  439. if(subghz_protocol_keeloq_check_decrypt(instance, decrypt, btn, end_serial)) {
  440. *manufacture_name = string_get_cstr(manufacture_code->name);
  441. return 1;
  442. }
  443. break;
  444. case KEELOQ_LEARNING_SECURE:
  445. man = subghz_protocol_keeloq_common_secure_learning(
  446. fix, seed, manufacture_code->key);
  447. decrypt = subghz_protocol_keeloq_common_decrypt(hop, man);
  448. if(subghz_protocol_keeloq_check_decrypt(instance, decrypt, btn, end_serial)) {
  449. *manufacture_name = string_get_cstr(manufacture_code->name);
  450. return 1;
  451. }
  452. break;
  453. case KEELOQ_LEARNING_MAGIC_XOR_TYPE_1:
  454. man = subghz_protocol_keeloq_common_magic_xor_type1_learning(
  455. fix, manufacture_code->key);
  456. decrypt = subghz_protocol_keeloq_common_decrypt(hop, man);
  457. if(subghz_protocol_keeloq_check_decrypt(instance, decrypt, btn, end_serial)) {
  458. *manufacture_name = string_get_cstr(manufacture_code->name);
  459. return 1;
  460. }
  461. break;
  462. case KEELOQ_LEARNING_UNKNOWN:
  463. // Simple Learning
  464. decrypt = subghz_protocol_keeloq_common_decrypt(hop, manufacture_code->key);
  465. if(subghz_protocol_keeloq_check_decrypt(instance, decrypt, btn, end_serial)) {
  466. *manufacture_name = string_get_cstr(manufacture_code->name);
  467. return 1;
  468. }
  469. // Check for mirrored man
  470. uint64_t man_rev = 0;
  471. uint64_t man_rev_byte = 0;
  472. for(uint8_t i = 0; i < 64; i += 8) {
  473. man_rev_byte = (uint8_t)(manufacture_code->key >> i);
  474. man_rev = man_rev | man_rev_byte << (56 - i);
  475. }
  476. decrypt = subghz_protocol_keeloq_common_decrypt(hop, man_rev);
  477. if(subghz_protocol_keeloq_check_decrypt(instance, decrypt, btn, end_serial)) {
  478. *manufacture_name = string_get_cstr(manufacture_code->name);
  479. return 1;
  480. }
  481. //###########################
  482. // Normal Learning
  483. // https://phreakerclub.com/forum/showpost.php?p=43557&postcount=37
  484. man = subghz_protocol_keeloq_common_normal_learning(fix, manufacture_code->key);
  485. decrypt = subghz_protocol_keeloq_common_decrypt(hop, man);
  486. if(subghz_protocol_keeloq_check_decrypt(instance, decrypt, btn, end_serial)) {
  487. *manufacture_name = string_get_cstr(manufacture_code->name);
  488. return 1;
  489. }
  490. // Check for mirrored man
  491. man = subghz_protocol_keeloq_common_normal_learning(fix, man_rev);
  492. decrypt = subghz_protocol_keeloq_common_decrypt(hop, man);
  493. if(subghz_protocol_keeloq_check_decrypt(instance, decrypt, btn, end_serial)) {
  494. *manufacture_name = string_get_cstr(manufacture_code->name);
  495. return 1;
  496. }
  497. // Secure Learning
  498. man = subghz_protocol_keeloq_common_secure_learning(
  499. fix, seed, manufacture_code->key);
  500. decrypt = subghz_protocol_keeloq_common_decrypt(hop, man);
  501. if(subghz_protocol_keeloq_check_decrypt(instance, decrypt, btn, end_serial)) {
  502. *manufacture_name = string_get_cstr(manufacture_code->name);
  503. return 1;
  504. }
  505. // Check for mirrored man
  506. man = subghz_protocol_keeloq_common_secure_learning(fix, seed, man_rev);
  507. decrypt = subghz_protocol_keeloq_common_decrypt(hop, man);
  508. if(subghz_protocol_keeloq_check_decrypt(instance, decrypt, btn, end_serial)) {
  509. *manufacture_name = string_get_cstr(manufacture_code->name);
  510. return 1;
  511. }
  512. // Magic xor type1 learning
  513. man = subghz_protocol_keeloq_common_magic_xor_type1_learning(
  514. fix, manufacture_code->key);
  515. decrypt = subghz_protocol_keeloq_common_decrypt(hop, man);
  516. if(subghz_protocol_keeloq_check_decrypt(instance, decrypt, btn, end_serial)) {
  517. *manufacture_name = string_get_cstr(manufacture_code->name);
  518. return 1;
  519. }
  520. // Check for mirrored man
  521. man = subghz_protocol_keeloq_common_magic_xor_type1_learning(fix, man_rev);
  522. decrypt = subghz_protocol_keeloq_common_decrypt(hop, man);
  523. if(subghz_protocol_keeloq_check_decrypt(instance, decrypt, btn, end_serial)) {
  524. *manufacture_name = string_get_cstr(manufacture_code->name);
  525. return 1;
  526. }
  527. break;
  528. }
  529. }
  530. *manufacture_name = "Unknown";
  531. instance->cnt = 0;
  532. return 0;
  533. }
  534. static void subghz_protocol_keeloq_check_remote_controller(
  535. SubGhzBlockGeneric* instance,
  536. SubGhzKeystore* keystore,
  537. const char** manufacture_name) {
  538. uint64_t key = subghz_protocol_blocks_reverse_key(instance->data, instance->data_count_bit);
  539. uint32_t key_fix = key >> 32;
  540. uint32_t key_hop = key & 0x00000000ffffffff;
  541. // Check key AN-Motors
  542. if((key_hop >> 24) == ((key_hop >> 16) & 0x00ff) &&
  543. (key_fix >> 28) == ((key_hop >> 12) & 0x0f) && (key_hop & 0xFFF) == 0x404) {
  544. *manufacture_name = "AN-Motors";
  545. instance->cnt = key_hop >> 16;
  546. } else if((key_hop & 0xFFF) == (0x000) && (key_fix >> 28) == ((key_hop >> 12) & 0x0f)) {
  547. *manufacture_name = "HCS101";
  548. instance->cnt = key_hop >> 16;
  549. } else {
  550. subghz_protocol_keeloq_check_remote_controller_selector(
  551. instance, key_fix, key_hop, keystore, manufacture_name);
  552. }
  553. instance->serial = key_fix & 0x0FFFFFFF;
  554. instance->btn = key_fix >> 28;
  555. }
  556. uint8_t subghz_protocol_decoder_keeloq_get_hash_data(void* context) {
  557. furi_assert(context);
  558. SubGhzProtocolDecoderKeeloq* instance = context;
  559. return subghz_protocol_blocks_get_hash_data(
  560. &instance->decoder, (instance->decoder.decode_count_bit / 8) + 1);
  561. }
  562. bool subghz_protocol_decoder_keeloq_serialize(
  563. void* context,
  564. FlipperFormat* flipper_format,
  565. SubGhzPesetDefinition* preset) {
  566. furi_assert(context);
  567. SubGhzProtocolDecoderKeeloq* instance = context;
  568. subghz_protocol_keeloq_check_remote_controller(
  569. &instance->generic, instance->keystore, &instance->manufacture_name);
  570. bool res = subghz_block_generic_serialize(&instance->generic, flipper_format, preset);
  571. if(res && !flipper_format_write_string_cstr(
  572. flipper_format, "Manufacture", instance->manufacture_name)) {
  573. FURI_LOG_E(TAG, "Unable to add manufacture name");
  574. res = false;
  575. }
  576. return res;
  577. }
  578. bool subghz_protocol_decoder_keeloq_deserialize(void* context, FlipperFormat* flipper_format) {
  579. furi_assert(context);
  580. SubGhzProtocolDecoderKeeloq* instance = context;
  581. bool res = false;
  582. do {
  583. if(!subghz_block_generic_deserialize(&instance->generic, flipper_format)) {
  584. FURI_LOG_E(TAG, "Deserialize error");
  585. break;
  586. }
  587. res = true;
  588. } while(false);
  589. return res;
  590. }
  591. void subghz_protocol_decoder_keeloq_get_string(void* context, string_t output) {
  592. furi_assert(context);
  593. SubGhzProtocolDecoderKeeloq* instance = context;
  594. subghz_protocol_keeloq_check_remote_controller(
  595. &instance->generic, instance->keystore, &instance->manufacture_name);
  596. uint32_t code_found_hi = instance->generic.data >> 32;
  597. uint32_t code_found_lo = instance->generic.data & 0x00000000ffffffff;
  598. uint64_t code_found_reverse = subghz_protocol_blocks_reverse_key(
  599. instance->generic.data, instance->generic.data_count_bit);
  600. uint32_t code_found_reverse_hi = code_found_reverse >> 32;
  601. uint32_t code_found_reverse_lo = code_found_reverse & 0x00000000ffffffff;
  602. string_cat_printf(
  603. output,
  604. "%s %dbit\r\n"
  605. "Key:%08lX%08lX\r\n"
  606. "Fix:0x%08lX Cnt:%04X\r\n"
  607. "Hop:0x%08lX Btn:%01lX\r\n"
  608. "MF:%s\r\n"
  609. "Sn:0x%07lX \r\n",
  610. instance->generic.protocol_name,
  611. instance->generic.data_count_bit,
  612. code_found_hi,
  613. code_found_lo,
  614. code_found_reverse_hi,
  615. instance->generic.cnt,
  616. code_found_reverse_lo,
  617. instance->generic.btn,
  618. instance->manufacture_name,
  619. instance->generic.serial);
  620. }