Skip to content

Commit

Permalink
Skorp sub ghz add protocol (#581)
Browse files Browse the repository at this point in the history
* SubGhz: Add Star Line protocol
* Rollback ReadMe changes
* SubGhz: add shared keystore for keeloq derived protocols.
* SubGhz: add missing key load routine

Co-authored-by: Aleksandr Kutuzov <[email protected]>
  • Loading branch information
Skorpionm and skotopes authored Jul 16, 2021
1 parent a837bc5 commit 1a039a8
Show file tree
Hide file tree
Showing 9 changed files with 584 additions and 154 deletions.
62 changes: 18 additions & 44 deletions lib/fl_subghz/protocols/subghz_protocol.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include "subghz_protocol.h"

#include "subghz_protocol_came.h"
#include "subghz_protocol_cfm.h"
#include "subghz_protocol_keeloq.h"
Expand All @@ -10,14 +9,16 @@
#include "subghz_protocol_ido.h"
#include "subghz_protocol_faac_slh.h"
#include "subghz_protocol_nero_sketch.h"
#include "subghz_protocol_star_line.h"

#include "../subghz_keystore.h"

#include <furi.h>
#include <m-string.h>
#include <filesystem-api.h>

#define FILE_BUFFER_SIZE 64

struct SubGhzProtocol {
SubGhzKeystore* keystore;

SubGhzProtocolCame* came;
SubGhzProtocolKeeloq* keeloq;
SubGhzProtocolNiceFlo* nice_flo;
Expand All @@ -27,6 +28,7 @@ struct SubGhzProtocol {
SubGhzProtocolIDo* ido;
SubGhzProtocolFaacSLH* faac_slh;
SubGhzProtocolNeroSketch* nero_sketch;
SubGhzProtocolStarLine* star_line;

SubGhzProtocolTextCallback text_callback;
void* text_callback_context;
Expand Down Expand Up @@ -58,15 +60,18 @@ static void subghz_protocol_parser_rx_callback(SubGhzProtocolCommon* parser, voi
SubGhzProtocol* subghz_protocol_alloc() {
SubGhzProtocol* instance = furi_alloc(sizeof(SubGhzProtocol));

instance->keystore = subghz_keystore_alloc();

instance->came = subghz_protocol_came_alloc();
instance->keeloq = subghz_protocol_keeloq_alloc();
instance->keeloq = subghz_protocol_keeloq_alloc(instance->keystore);
instance->princeton = subghz_protocol_princeton_alloc();
instance->nice_flo = subghz_protocol_nice_flo_alloc();
instance->nice_flor_s = subghz_protocol_nice_flor_s_alloc();
instance->gate_tx = subghz_protocol_gate_tx_alloc();
instance->ido = subghz_protocol_ido_alloc();
instance->faac_slh = subghz_protocol_faac_slh_alloc();
instance->nero_sketch = subghz_protocol_nero_sketch_alloc();
instance->star_line = subghz_protocol_star_line_alloc(instance->keystore);

return instance;
}
Expand All @@ -83,6 +88,9 @@ void subghz_protocol_free(SubGhzProtocol* instance) {
subghz_protocol_ido_free(instance->ido);
subghz_protocol_faac_slh_free(instance->faac_slh);
subghz_protocol_nero_sketch_free(instance->nero_sketch);
subghz_protocol_star_line_free(instance->star_line);

subghz_keystore_free(instance->keystore);

free(instance);
}
Expand All @@ -99,6 +107,7 @@ void subghz_protocol_enable_dump_text(SubGhzProtocol* instance, SubGhzProtocolTe
subghz_protocol_common_set_callback((SubGhzProtocolCommon*)instance->ido, subghz_protocol_text_rx_callback, instance);
subghz_protocol_common_set_callback((SubGhzProtocolCommon*)instance->faac_slh, subghz_protocol_text_rx_callback, instance);
subghz_protocol_common_set_callback((SubGhzProtocolCommon*)instance->nero_sketch, subghz_protocol_text_rx_callback, instance);
subghz_protocol_common_set_callback((SubGhzProtocolCommon*)instance->star_line, subghz_protocol_text_rx_callback, instance);

instance->text_callback = callback;
instance->text_callback_context = context;
Expand All @@ -116,56 +125,19 @@ void subghz_protocol_enable_dump(SubGhzProtocol* instance, SubGhzProtocolCommonC
subghz_protocol_common_set_callback((SubGhzProtocolCommon*)instance->ido, subghz_protocol_parser_rx_callback, instance);
subghz_protocol_common_set_callback((SubGhzProtocolCommon*)instance->faac_slh, subghz_protocol_parser_rx_callback, instance);
subghz_protocol_common_set_callback((SubGhzProtocolCommon*)instance->nero_sketch, subghz_protocol_parser_rx_callback, instance);
subghz_protocol_common_set_callback((SubGhzProtocolCommon*)instance->star_line, subghz_protocol_parser_rx_callback, instance);

instance->parser_callback = callback;
instance->parser_callback_context = context;
}

static void subghz_protocol_load_keeloq_file_process_line(SubGhzProtocol* instance, string_t line) {
uint64_t key = 0;
uint16_t type = 0;
char skey[17] = {0};
char name[65] = {0};
int ret = sscanf(string_get_cstr(line), "%16s:%hu:%64s", skey, &type, name);
key = strtoull(skey, NULL, 16);
if (ret == 3) {
subghz_protocol_keeloq_add_manafacture_key(instance->keeloq, name, key, type);
} else {
printf("Failed to load line: %s\r\n", string_get_cstr(line));
}
}

void subghz_protocol_load_nice_flor_s_file(SubGhzProtocol* instance, const char* file_name) {
subghz_protocol_nice_flor_s_name_file(instance->nice_flor_s, file_name);
}

void subghz_protocol_load_keeloq_file(SubGhzProtocol* instance, const char* file_name) {
File manufacture_keys_file;
FS_Api* fs_api = furi_record_open("sdcard");
fs_api->file.open(&manufacture_keys_file, file_name, FSAM_READ, FSOM_OPEN_EXISTING);
string_t line;
string_init(line);
if(manufacture_keys_file.error_id == FSE_OK) {
printf("Loading manufacture keys file %s\r\n", file_name);
char buffer[FILE_BUFFER_SIZE];
uint16_t ret;
do {
ret = fs_api->file.read(&manufacture_keys_file, buffer, FILE_BUFFER_SIZE);
for (uint16_t i=0; i < ret; i++) {
if (buffer[i] == '\n' && string_size(line) > 0) {
subghz_protocol_load_keeloq_file_process_line(instance, line);
string_clean(line);
} else {
string_push_back(line, buffer[i]);
}
}
} while(ret > 0);
} else {
printf("Manufacture keys file is not found: %s\r\n", file_name);
}
string_clear(line);
fs_api->file.close(&manufacture_keys_file);
furi_record_close("sdcard");
subghz_keystore_load(instance->keystore, file_name);
}

void subghz_protocol_reset(SubGhzProtocol* instance) {
Expand All @@ -178,6 +150,7 @@ void subghz_protocol_reset(SubGhzProtocol* instance) {
subghz_protocol_ido_reset(instance->ido);
subghz_protocol_faac_slh_reset(instance->faac_slh);
subghz_protocol_nero_sketch_reset(instance->nero_sketch);
subghz_protocol_star_line_reset(instance->star_line);
}

void subghz_protocol_parse(SubGhzProtocol* instance, bool level, uint32_t duration) {
Expand All @@ -190,4 +163,5 @@ void subghz_protocol_parse(SubGhzProtocol* instance, bool level, uint32_t durati
subghz_protocol_ido_parse(instance->ido, level, duration);
subghz_protocol_faac_slh_parse(instance->faac_slh, level, duration);
subghz_protocol_nero_sketch_parse(instance->nero_sketch, level, duration);
subghz_protocol_star_line_parse(instance->star_line, level, duration);
}
117 changes: 17 additions & 100 deletions lib/fl_subghz/protocols/subghz_protocol_keeloq.c
Original file line number Diff line number Diff line change
@@ -1,121 +1,38 @@
#include "subghz_protocol_keeloq.h"
#include "subghz_protocol_keeloq_common.h"

#include "../subghz_keystore.h"

#include <furi.h>

#include <m-string.h>
#include <m-array.h>

/*
* Keeloq
* https://ru.wikipedia.org/wiki/KeeLoq
* https://phreakerclub.com/forum/showthread.php?t=1094
*
*/

#define KEELOQ_NLF 0x3A5C742E
#define bit(x,n) (((x)>>(n))&1)
#define g5(x,a,b,c,d,e) (bit(x,a)+bit(x,b)*2+bit(x,c)*4+bit(x,d)*8+bit(x,e)*16)

/*
* KeeLoq learning types
* https://phreakerclub.com/forum/showthread.php?t=67
*/
#define KEELOQ_LEARNING_UNKNOWN 0u
#define KEELOQ_LEARNING_SIMPLE 1u
#define KEELOQ_LEARNING_NORMAL 2u
#define KEELOQ_LEARNING_SECURE 3u

typedef struct {
string_t name;
uint64_t key;
uint16_t type;
} KeeLoqManufactureCode;

ARRAY_DEF(KeeLoqManufactureCodeArray, KeeLoqManufactureCode, M_POD_OPLIST)
#define M_OPL_KeeLoqManufactureCodeArray_t() ARRAY_OPLIST(KeeLoqManufactureCodeArray, M_POD_OPLIST)

struct SubGhzProtocolKeeloq {
SubGhzProtocolCommon common;
KeeLoqManufactureCodeArray_t manufacture_codes;
SubGhzKeystore* keystore;
const char* manufacture_name;
};

/** Simple Learning Encrypt
* @param data - 0xBSSSCCCC, B(4bit) key, S(10bit) serial&0x3FF, C(16bit) counter
* @param key - manufacture (64bit)
* @return keelog encrypt data
*/
inline uint32_t subghz_protocol_keeloq_encrypt(const uint32_t data, const uint64_t key) {
uint32_t x = data, r;
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);
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_decrypt(const uint32_t data, const uint64_t key) {
uint32_t x = data, r;
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));
return x;
}

/** Normal Learning
* @param data - serial number (28bit)
* @param key - manufacture (64bit)
* @return manufacture for this serial number (64bit)
*/
inline uint64_t subghz_protocol_keeloq_normal_learning(uint32_t data, const uint64_t key){
uint32_t k1,k2;

data&=0x0FFFFFFF;
data|=0x20000000;
k1=subghz_protocol_keeloq_decrypt(data, key);

data&=0x0FFFFFFF;
data|=0x60000000;
k2=subghz_protocol_keeloq_decrypt(data, key);

return ((uint64_t)k2<<32)| k1; // key - shifrovanoya
}

SubGhzProtocolKeeloq* subghz_protocol_keeloq_alloc() {
SubGhzProtocolKeeloq* subghz_protocol_keeloq_alloc(SubGhzKeystore* keystore) {
SubGhzProtocolKeeloq* instance = furi_alloc(sizeof(SubGhzProtocolKeeloq));

instance->keystore = keystore;

instance->common.name = "KeeLoq";
instance->common.code_min_count_bit_for_found = 64;
instance->common.te_shot = 400;
instance->common.te_long = 800;
instance->common.te_delta = 140;
instance->common.to_string = (SubGhzProtocolCommonToStr)subghz_protocol_keeloq_to_str;

KeeLoqManufactureCodeArray_init(instance->manufacture_codes);

return instance;
}

void subghz_protocol_keeloq_free(SubGhzProtocolKeeloq* instance) {
furi_assert(instance);
for
M_EACH(manufacture_code, instance->manufacture_codes, KeeLoqManufactureCodeArray_t) {
string_clear(manufacture_code->name);
manufacture_code->key = 0;
}
KeeLoqManufactureCodeArray_clear(instance->manufacture_codes);
free(instance);
}

void subghz_protocol_keeloq_add_manafacture_key(SubGhzProtocolKeeloq* instance, const char* name, uint64_t key, uint16_t type) {
KeeLoqManufactureCode* manufacture_code = KeeLoqManufactureCodeArray_push_raw(instance->manufacture_codes);
string_init_set_str(manufacture_code->name, name);
manufacture_code->key = key;
manufacture_code->type = type;
}

/** Checking the accepted code against the database manafacture key
*
* @param instance SubGhzProtocolKeeloq instance
Expand All @@ -130,11 +47,11 @@ uint8_t subghz_protocol_keeloq_check_remote_controller_selector(SubGhzProtocolKe
uint64_t man_normal_learning;

for
M_EACH(manufacture_code, instance->manufacture_codes, KeeLoqManufactureCodeArray_t) {
M_EACH(manufacture_code, *subghz_keystore_get_data(instance->keystore), SubGhzKeyArray_t) {
switch (manufacture_code->type){
case KEELOQ_LEARNING_SIMPLE:
//Simple Learning
decrypt = subghz_protocol_keeloq_decrypt(hop, manufacture_code->key);
decrypt = subghz_protocol_keeloq_common_decrypt(hop, manufacture_code->key);
if((decrypt>>28 == btn) && ((((uint16_t)(decrypt>>16)) & 0x3FF) == end_serial)){
instance->manufacture_name = string_get_cstr(manufacture_code->name);
instance->common.cnt = decrypt & 0x0000FFFF;
Expand All @@ -144,8 +61,8 @@ uint8_t subghz_protocol_keeloq_check_remote_controller_selector(SubGhzProtocolKe
case KEELOQ_LEARNING_NORMAL:
// Normal_Learning
// https://phreakerclub.com/forum/showpost.php?p=43557&postcount=37
man_normal_learning = subghz_protocol_keeloq_normal_learning(fix, manufacture_code->key);
decrypt=subghz_protocol_keeloq_decrypt(hop, man_normal_learning);
man_normal_learning = subghz_protocol_keeloq_common_normal_learning(fix, manufacture_code->key);
decrypt=subghz_protocol_keeloq_common_decrypt(hop, man_normal_learning);
if( (decrypt>>28 ==btn)&& ((((uint16_t)(decrypt>>16))&0x3FF)==end_serial)){
instance->manufacture_name = string_get_cstr(manufacture_code->name);
instance->common.cnt = decrypt & 0x0000FFFF;
Expand All @@ -154,7 +71,7 @@ uint8_t subghz_protocol_keeloq_check_remote_controller_selector(SubGhzProtocolKe
break;
case KEELOQ_LEARNING_UNKNOWN:
// Simple Learning
decrypt=subghz_protocol_keeloq_decrypt(hop, manufacture_code->key);
decrypt=subghz_protocol_keeloq_common_decrypt(hop, manufacture_code->key);
if( (decrypt>>28 ==btn) && ((((uint16_t)(decrypt>>16))&0x3FF)==end_serial)){
instance->manufacture_name = string_get_cstr(manufacture_code->name);
instance->common.cnt = decrypt & 0x0000FFFF;
Expand All @@ -167,7 +84,7 @@ uint8_t subghz_protocol_keeloq_check_remote_controller_selector(SubGhzProtocolKe
man_rev_byte=(uint8_t)(manufacture_code->key >> i);
man_rev = man_rev | man_rev_byte << (56-i);
}
decrypt=subghz_protocol_keeloq_decrypt(hop, man_rev);
decrypt=subghz_protocol_keeloq_common_decrypt(hop, man_rev);
if( (decrypt>>28 ==btn) && ((((uint16_t)(decrypt>>16))&0x3FF)==end_serial)){
instance->manufacture_name = string_get_cstr(manufacture_code->name);
instance->common.cnt= decrypt&0x0000FFFF;
Expand All @@ -176,8 +93,8 @@ uint8_t subghz_protocol_keeloq_check_remote_controller_selector(SubGhzProtocolKe
//###########################
// Normal_Learning
// https://phreakerclub.com/forum/showpost.php?p=43557&postcount=37
man_normal_learning = subghz_protocol_keeloq_normal_learning(fix, manufacture_code->key);
decrypt=subghz_protocol_keeloq_decrypt(hop, man_normal_learning);
man_normal_learning = subghz_protocol_keeloq_common_normal_learning(fix, manufacture_code->key);
decrypt=subghz_protocol_keeloq_common_decrypt(hop, man_normal_learning);
if( (decrypt>>28 ==btn)&& ((((uint16_t)(decrypt>>16))&0x3FF)==end_serial)){
instance->manufacture_name = string_get_cstr(manufacture_code->name);
instance->common.cnt= decrypt&0x0000FFFF;
Expand All @@ -190,8 +107,8 @@ uint8_t subghz_protocol_keeloq_check_remote_controller_selector(SubGhzProtocolKe
man_rev_byte = (uint8_t)(manufacture_code->key >> i);
man_rev = man_rev | man_rev_byte << (56-i);
}
man_normal_learning = subghz_protocol_keeloq_normal_learning(fix, man_rev);
decrypt=subghz_protocol_keeloq_decrypt(hop, man_normal_learning);
man_normal_learning = subghz_protocol_keeloq_common_normal_learning(fix, man_rev);
decrypt=subghz_protocol_keeloq_common_decrypt(hop, man_normal_learning);
if( (decrypt>>28 ==btn) && ((((uint16_t)(decrypt>>16))&0x3FF)==end_serial)){
instance->manufacture_name = string_get_cstr(manufacture_code->name);
instance->common.cnt= decrypt&0x0000FFFF;
Expand Down
13 changes: 3 additions & 10 deletions lib/fl_subghz/protocols/subghz_protocol_keeloq.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,22 @@

#include "subghz_protocol_common.h"

typedef struct SubGhzKeystore SubGhzKeystore;

typedef struct SubGhzProtocolKeeloq SubGhzProtocolKeeloq;

/** Allocate SubGhzProtocolKeeloq
*
* @return SubGhzProtocolKeeloq*
*/
SubGhzProtocolKeeloq* subghz_protocol_keeloq_alloc();
SubGhzProtocolKeeloq* subghz_protocol_keeloq_alloc(SubGhzKeystore* keystore);

/** Free SubGhzProtocolKeeloq
*
* @param instance
*/
void subghz_protocol_keeloq_free(SubGhzProtocolKeeloq* instance);

/** Loading of manufacture keys
*
* @param instance - SubGhzProtocolKeeloq instance
* @param name - key name
* @param key - manufacture (64bit)
* @param type - type manufacture key
*/
void subghz_protocol_keeloq_add_manafacture_key(SubGhzProtocolKeeloq* instance, const char* name, uint64_t key, uint16_t type);

/** Sends the key on the air
*
* @param instance - SubGhzProtocolKeeloq instance
Expand Down
Loading

0 comments on commit 1a039a8

Please sign in to comment.