keeloq.c 29 KB

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