subghz_protocol_keeloq_common.c 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #include "subghz_protocol_keeloq_common.h"
  2. #include <furi.h>
  3. #include <m-string.h>
  4. #include <m-array.h>
  5. /** Simple Learning Encrypt
  6. * @param data - 0xBSSSCCCC, B(4bit) key, S(10bit) serial&0x3FF, C(16bit) counter
  7. * @param key - manufacture (64bit)
  8. * @return keelog encrypt data
  9. */
  10. inline uint32_t subghz_protocol_keeloq_common_encrypt(const uint32_t data, const uint64_t key) {
  11. uint32_t x = data, r;
  12. for (r = 0; r < 528; r++)
  13. x = (x>>1)^((bit(x,0)^bit(x,16)^(uint32_t)bit(key,r&63)^bit(KEELOQ_NLF,g5(x,1,9,20,26,31)))<<31);
  14. return x;
  15. }
  16. /** Simple Learning Decrypt
  17. * @param data - keelog encrypt data
  18. * @param key - manufacture (64bit)
  19. * @return 0xBSSSCCCC, B(4bit) key, S(10bit) serial&0x3FF, C(16bit) counter
  20. */
  21. inline uint32_t subghz_protocol_keeloq_common_decrypt(const uint32_t data, const uint64_t key) {
  22. uint32_t x = data, r;
  23. for (r = 0; r < 528; r++)
  24. x = (x<<1)^bit(x,31)^bit(x,15)^(uint32_t)bit(key,(15-r)&63)^bit(KEELOQ_NLF,g5(x,0,8,19,25,30));
  25. return x;
  26. }
  27. /** Normal Learning
  28. * @param data - serial number (28bit)
  29. * @param key - manufacture (64bit)
  30. * @return manufacture for this serial number (64bit)
  31. */
  32. inline uint64_t subghz_protocol_keeloq_common_normal_learning(uint32_t data, const uint64_t key){
  33. uint32_t k1,k2;
  34. data&=0x0FFFFFFF;
  35. data|=0x20000000;
  36. k1=subghz_protocol_keeloq_common_decrypt(data, key);
  37. data&=0x0FFFFFFF;
  38. data|=0x60000000;
  39. k2=subghz_protocol_keeloq_common_decrypt(data, key);
  40. return ((uint64_t)k2<<32)| k1; // key - shifrovanoya
  41. }