keeloq.c 29 KB

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