keeloq_common.h 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #pragma once
  2. #include "base.h"
  3. #include <furi.h>
  4. /*
  5. * Keeloq
  6. * https://ru.wikipedia.org/wiki/KeeLoq
  7. * https://phreakerclub.com/forum/showthread.php?t=1094
  8. *
  9. */
  10. #define KEELOQ_NLF 0x3A5C742E
  11. #define bit(x, n) (((x) >> (n)) & 1)
  12. #define g5(x, a, b, c, d, e) \
  13. (bit(x, a) + bit(x, b) * 2 + bit(x, c) * 4 + bit(x, d) * 8 + bit(x, e) * 16)
  14. /*
  15. * KeeLoq learning types
  16. * https://phreakerclub.com/forum/showthread.php?t=67
  17. */
  18. #define KEELOQ_LEARNING_UNKNOWN 0u
  19. #define KEELOQ_LEARNING_SIMPLE 1u
  20. #define KEELOQ_LEARNING_NORMAL 2u
  21. #define KEELOQ_LEARNING_SECURE 3u
  22. #define KEELOQ_LEARNING_MAGIC_XOR_TYPE_1 4u
  23. /** Simple Learning Encrypt
  24. * @param data - 0xBSSSCCCC, B(4bit) key, S(10bit) serial&0x3FF, C(16bit) counter
  25. * @param key - manufacture (64bit)
  26. * @return keeloq encrypt data
  27. */
  28. uint32_t subghz_protocol_keeloq_common_encrypt(const uint32_t data, const uint64_t key);
  29. /** Simple Learning Decrypt
  30. * @param data - keeloq encrypt data
  31. * @param key - manufacture (64bit)
  32. * @return 0xBSSSCCCC, B(4bit) key, S(10bit) serial&0x3FF, C(16bit) counter
  33. */
  34. uint32_t subghz_protocol_keeloq_common_decrypt(const uint32_t data, const uint64_t key);
  35. /** Normal Learning
  36. * @param data - serial number (28bit)
  37. * @param key - manufacture (64bit)
  38. * @return manufacture for this serial number (64bit)
  39. */
  40. uint64_t subghz_protocol_keeloq_common_normal_learning(uint32_t data, const uint64_t key);
  41. /** Secure Learning
  42. * @param data - serial number (28bit)
  43. * @param seed - seed number (32bit)
  44. * @param key - manufacture (64bit)
  45. * @return manufacture for this serial number (64bit)
  46. */
  47. uint64_t
  48. subghz_protocol_keeloq_common_secure_learning(uint32_t data, uint32_t seed, const uint64_t key);
  49. /** Magic_xor_type1 Learning
  50. * @param data - serial number (28bit)
  51. * @param xor - magic xor (64bit)
  52. * @return manufacture for this serial number (64bit)
  53. */
  54. uint64_t subghz_protocol_keeloq_common_magic_xor_type1_learning(uint32_t data, uint64_t xor);