keeloq.c 29 KB

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