2022-03-03 09:48:56 +00:00
|
|
|
#include "keeloq_common.h"
|
2021-10-17 22:54:19 +00:00
|
|
|
|
|
|
|
#include <furi.h>
|
|
|
|
|
|
|
|
#include <m-string.h>
|
|
|
|
#include <m-array.h>
|
|
|
|
|
|
|
|
/** Simple Learning Encrypt
|
|
|
|
* @param data - 0xBSSSCCCC, B(4bit) key, S(10bit) serial&0x3FF, C(16bit) counter
|
|
|
|
* @param key - manufacture (64bit)
|
2022-03-03 09:48:56 +00:00
|
|
|
* @return keeloq encrypt data
|
2021-10-17 22:54:19 +00:00
|
|
|
*/
|
|
|
|
inline uint32_t subghz_protocol_keeloq_common_encrypt(const uint32_t data, const uint64_t key) {
|
|
|
|
uint32_t x = data, r;
|
2021-11-17 13:52:06 +00:00
|
|
|
for(r = 0; r < 528; r++)
|
|
|
|
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);
|
2021-10-17 22:54:19 +00:00
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Simple Learning Decrypt
|
|
|
|
* @param data - keelog encrypt data
|
|
|
|
* @param key - manufacture (64bit)
|
|
|
|
* @return 0xBSSSCCCC, B(4bit) key, S(10bit) serial&0x3FF, C(16bit) counter
|
|
|
|
*/
|
|
|
|
inline uint32_t subghz_protocol_keeloq_common_decrypt(const uint32_t data, const uint64_t key) {
|
|
|
|
uint32_t x = data, r;
|
2021-11-17 13:52:06 +00:00
|
|
|
for(r = 0; r < 528; r++)
|
|
|
|
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));
|
2021-10-17 22:54:19 +00:00
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Normal Learning
|
|
|
|
* @param data - serial number (28bit)
|
|
|
|
* @param key - manufacture (64bit)
|
|
|
|
* @return manufacture for this serial number (64bit)
|
|
|
|
*/
|
2021-11-17 13:52:06 +00:00
|
|
|
inline uint64_t subghz_protocol_keeloq_common_normal_learning(uint32_t data, const uint64_t key) {
|
|
|
|
uint32_t k1, k2;
|
2021-10-17 22:54:19 +00:00
|
|
|
|
2021-11-17 13:52:06 +00:00
|
|
|
data &= 0x0FFFFFFF;
|
|
|
|
data |= 0x20000000;
|
|
|
|
k1 = subghz_protocol_keeloq_common_decrypt(data, key);
|
2021-10-17 22:54:19 +00:00
|
|
|
|
2021-11-17 13:52:06 +00:00
|
|
|
data &= 0x0FFFFFFF;
|
|
|
|
data |= 0x60000000;
|
|
|
|
k2 = subghz_protocol_keeloq_common_decrypt(data, key);
|
2021-10-17 22:54:19 +00:00
|
|
|
|
2021-11-17 13:52:06 +00:00
|
|
|
return ((uint64_t)k2 << 32) | k1; // key - shifrovanoya
|
2021-10-17 22:54:19 +00:00
|
|
|
}
|
2021-11-17 13:52:06 +00:00
|
|
|
|
|
|
|
/** Secure Learning
|
|
|
|
* @param data - serial number (28bit)
|
2022-01-21 13:55:09 +00:00
|
|
|
* @param seed - seed number (32bit)
|
2021-11-17 13:52:06 +00:00
|
|
|
* @param key - manufacture (64bit)
|
|
|
|
* @return manufacture for this serial number (64bit)
|
|
|
|
*/
|
|
|
|
|
|
|
|
inline uint64_t subghz_protocol_keeloq_common_secure_learning(
|
|
|
|
uint32_t data,
|
|
|
|
uint32_t seed,
|
|
|
|
const uint64_t key) {
|
|
|
|
uint32_t k1, k2;
|
|
|
|
|
|
|
|
data &= 0x0FFFFFFF;
|
|
|
|
k1 = subghz_protocol_keeloq_common_decrypt(data, key);
|
|
|
|
k2 = subghz_protocol_keeloq_common_decrypt(seed, key);
|
|
|
|
|
|
|
|
return ((uint64_t)k1 << 32) | k2;
|
2022-01-21 13:55:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Magic_xor_type1 Learning
|
|
|
|
* @param data - serial number (28bit)
|
|
|
|
* @param xor - magic xor (64bit)
|
|
|
|
* @return manufacture for this serial number (64bit)
|
|
|
|
*/
|
|
|
|
|
|
|
|
inline uint64_t
|
|
|
|
subghz_protocol_keeloq_common_magic_xor_type1_learning(uint32_t data, uint64_t xor) {
|
|
|
|
data &= 0x0FFFFFFF;
|
|
|
|
return (((uint64_t)data << 32) | data) ^ xor;
|
2022-04-08 21:29:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Faac SLH (Spa) Learning
|
|
|
|
* @param seed - seed number (32bit)
|
|
|
|
* @param key - mfkey (64bit)
|
2022-04-09 02:09:31 +00:00
|
|
|
* @return man_learning for this seed number (64bit)
|
2022-04-08 21:29:15 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
inline uint64_t
|
2022-04-09 12:15:36 +00:00
|
|
|
subghz_protocol_keeloq_common_faac_learning(const uint32_t seed, const uint64_t key) {
|
2022-04-08 21:29:15 +00:00
|
|
|
uint16_t hs = seed >> 16;
|
2022-04-09 01:06:41 +00:00
|
|
|
const uint16_t ending = 0x544D;
|
|
|
|
uint32_t lsb = (uint32_t)hs << 16 | ending;
|
2022-04-09 01:33:32 +00:00
|
|
|
uint64_t man = (uint64_t)subghz_protocol_keeloq_common_encrypt(seed, key) << 32 |
|
|
|
|
subghz_protocol_keeloq_common_encrypt(lsb, key);
|
|
|
|
return man;
|
2021-11-17 13:52:06 +00:00
|
|
|
}
|