keeloq_common.h 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. /**
  24. * Simple Learning Encrypt
  25. * @param data - 0xBSSSCCCC, B(4bit) key, S(10bit) serial&0x3FF, C(16bit) counter
  26. * @param key - manufacture (64bit)
  27. * @return keeloq encrypt data
  28. */
  29. uint32_t subghz_protocol_keeloq_common_encrypt(const uint32_t data, const uint64_t key);
  30. /**
  31. * Simple Learning Decrypt
  32. * @param data - keeloq encrypt data
  33. * @param key - manufacture (64bit)
  34. * @return 0xBSSSCCCC, B(4bit) key, S(10bit) serial&0x3FF, C(16bit) counter
  35. */
  36. uint32_t subghz_protocol_keeloq_common_decrypt(const uint32_t data, const uint64_t key);
  37. /**
  38. * Normal Learning
  39. * @param data - serial number (28bit)
  40. * @param key - manufacture (64bit)
  41. * @return manufacture for this serial number (64bit)
  42. */
  43. uint64_t subghz_protocol_keeloq_common_normal_learning(uint32_t data, const uint64_t key);
  44. /**
  45. * Secure Learning
  46. * @param data - serial number (28bit)
  47. * @param seed - seed number (32bit)
  48. * @param key - manufacture (64bit)
  49. * @return manufacture for this serial number (64bit)
  50. */
  51. uint64_t
  52. subghz_protocol_keeloq_common_secure_learning(uint32_t data, uint32_t seed, const uint64_t key);
  53. /**
  54. * Magic_xor_type1 Learning
  55. * @param data - serial number (28bit)
  56. * @param xor - magic xor (64bit)
  57. * @return manufacture for this serial number (64bit)
  58. */
  59. uint64_t subghz_protocol_keeloq_common_magic_xor_type1_learning(uint32_t data, uint64_t xor);