subghz: add beninca arc protocol

This commit is contained in:
MX
2026-01-21 07:58:42 +03:00
parent edb86fa279
commit db2dc8f64f
20 changed files with 1238 additions and 69 deletions

View File

@@ -0,0 +1,252 @@
#include "aes_common.h"
static const uint8_t aes_sbox[256] = {
0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5, 0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab,
0x76, 0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0, 0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4,
0x72, 0xc0, 0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc, 0x34, 0xa5, 0xe5, 0xf1, 0x71,
0xd8, 0x31, 0x15, 0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a, 0x07, 0x12, 0x80, 0xe2,
0xeb, 0x27, 0xb2, 0x75, 0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0, 0x52, 0x3b, 0xd6,
0xb3, 0x29, 0xe3, 0x2f, 0x84, 0x53, 0xd1, 0x00, 0xed, 0x20, 0xfc, 0xb1, 0x5b, 0x6a, 0xcb,
0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf, 0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85, 0x45,
0xf9, 0x02, 0x7f, 0x50, 0x3c, 0x9f, 0xa8, 0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5,
0xbc, 0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2, 0xcd, 0x0c, 0x13, 0xec, 0x5f, 0x97, 0x44,
0x17, 0xc4, 0xa7, 0x7e, 0x3d, 0x64, 0x5d, 0x19, 0x73, 0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a,
0x90, 0x88, 0x46, 0xee, 0xb8, 0x14, 0xde, 0x5e, 0x0b, 0xdb, 0xe0, 0x32, 0x3a, 0x0a, 0x49,
0x06, 0x24, 0x5c, 0xc2, 0xd3, 0xac, 0x62, 0x91, 0x95, 0xe4, 0x79, 0xe7, 0xc8, 0x37, 0x6d,
0x8d, 0xd5, 0x4e, 0xa9, 0x6c, 0x56, 0xf4, 0xea, 0x65, 0x7a, 0xae, 0x08, 0xba, 0x78, 0x25,
0x2e, 0x1c, 0xa6, 0xb4, 0xc6, 0xe8, 0xdd, 0x74, 0x1f, 0x4b, 0xbd, 0x8b, 0x8a, 0x70, 0x3e,
0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e, 0x61, 0x35, 0x57, 0xb9, 0x86, 0xc1, 0x1d, 0x9e, 0xe1,
0xf8, 0x98, 0x11, 0x69, 0xd9, 0x8e, 0x94, 0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf,
0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68, 0x41, 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb,
0x16};
static const uint8_t aes_sbox_inv[256] = {
0x52, 0x09, 0x6a, 0xd5, 0x30, 0x36, 0xa5, 0x38, 0xbf, 0x40, 0xa3, 0x9e, 0x81, 0xf3, 0xd7,
0xfb, 0x7c, 0xe3, 0x39, 0x82, 0x9b, 0x2f, 0xff, 0x87, 0x34, 0x8e, 0x43, 0x44, 0xc4, 0xde,
0xe9, 0xcb, 0x54, 0x7b, 0x94, 0x32, 0xa6, 0xc2, 0x23, 0x3d, 0xee, 0x4c, 0x95, 0x0b, 0x42,
0xfa, 0xc3, 0x4e, 0x08, 0x2e, 0xa1, 0x66, 0x28, 0xd9, 0x24, 0xb2, 0x76, 0x5b, 0xa2, 0x49,
0x6d, 0x8b, 0xd1, 0x25, 0x72, 0xf8, 0xf6, 0x64, 0x86, 0x68, 0x98, 0x16, 0xd4, 0xa4, 0x5c,
0xcc, 0x5d, 0x65, 0xb6, 0x92, 0x6c, 0x70, 0x48, 0x50, 0xfd, 0xed, 0xb9, 0xda, 0x5e, 0x15,
0x46, 0x57, 0xa7, 0x8d, 0x9d, 0x84, 0x90, 0xd8, 0xab, 0x00, 0x8c, 0xbc, 0xd3, 0x0a, 0xf7,
0xe4, 0x58, 0x05, 0xb8, 0xb3, 0x45, 0x06, 0xd0, 0x2c, 0x1e, 0x8f, 0xca, 0x3f, 0x0f, 0x02,
0xc1, 0xaf, 0xbd, 0x03, 0x01, 0x13, 0x8a, 0x6b, 0x3a, 0x91, 0x11, 0x41, 0x4f, 0x67, 0xdc,
0xea, 0x97, 0xf2, 0xcf, 0xce, 0xf0, 0xb4, 0xe6, 0x73, 0x96, 0xac, 0x74, 0x22, 0xe7, 0xad,
0x35, 0x85, 0xe2, 0xf9, 0x37, 0xe8, 0x1c, 0x75, 0xdf, 0x6e, 0x47, 0xf1, 0x1a, 0x71, 0x1d,
0x29, 0xc5, 0x89, 0x6f, 0xb7, 0x62, 0x0e, 0xaa, 0x18, 0xbe, 0x1b, 0xfc, 0x56, 0x3e, 0x4b,
0xc6, 0xd2, 0x79, 0x20, 0x9a, 0xdb, 0xc0, 0xfe, 0x78, 0xcd, 0x5a, 0xf4, 0x1f, 0xdd, 0xa8,
0x33, 0x88, 0x07, 0xc7, 0x31, 0xb1, 0x12, 0x10, 0x59, 0x27, 0x80, 0xec, 0x5f, 0x60, 0x51,
0x7f, 0xa9, 0x19, 0xb5, 0x4a, 0x0d, 0x2d, 0xe5, 0x7a, 0x9f, 0x93, 0xc9, 0x9c, 0xef, 0xa0,
0xe0, 0x3b, 0x4d, 0xae, 0x2a, 0xf5, 0xb0, 0xc8, 0xeb, 0xbb, 0x3c, 0x83, 0x53, 0x99, 0x61,
0x17, 0x2b, 0x04, 0x7e, 0xba, 0x77, 0xd6, 0x26, 0xe1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0c,
0x7d};
static const uint8_t aes_rcon[10] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36};
static uint8_t gf_mul2(uint8_t x) {
return ((x >> 7) * 0x1b) ^ (x << 1);
}
static void aes_subbytes(uint8_t* state) {
for(uint8_t row = 0; row < 4; row++) {
for(uint8_t col = 0; col < 4; col++) {
state[row + col * 4] = aes_sbox[state[row + col * 4]];
}
}
}
static void aes_subbytes_inv(uint8_t* state) {
for(uint8_t row = 0; row < 4; row++) {
for(uint8_t col = 0; col < 4; col++) {
state[row + col * 4] = aes_sbox_inv[state[row + col * 4]];
}
}
}
static void aes_shiftrows(uint8_t* state) {
uint8_t temp;
temp = state[1];
state[1] = state[5];
state[5] = state[9];
state[9] = state[13];
state[13] = temp;
temp = state[2];
state[2] = state[10];
state[10] = temp;
temp = state[6];
state[6] = state[14];
state[14] = temp;
temp = state[15];
state[15] = state[11];
state[11] = state[7];
state[7] = state[3];
state[3] = temp;
}
static void aes_shiftrows_inv(uint8_t* state) {
uint8_t temp;
temp = state[13];
state[13] = state[9];
state[9] = state[5];
state[5] = state[1];
state[1] = temp;
temp = state[2];
state[2] = state[10];
state[10] = temp;
temp = state[6];
state[6] = state[14];
state[14] = temp;
temp = state[3];
state[3] = state[7];
state[7] = state[11];
state[11] = state[15];
state[15] = temp;
}
static void aes_mixcolumns(uint8_t* state) {
uint8_t a, b, c, d;
for(uint8_t i = 0; i < 4; i++) {
a = state[i * 4];
b = state[i * 4 + 1];
c = state[i * 4 + 2];
d = state[i * 4 + 3];
uint8_t a2 = gf_mul2(a);
uint8_t b2 = gf_mul2(b);
uint8_t c2 = gf_mul2(c);
uint8_t d2 = gf_mul2(d);
state[i * 4] = a2 ^ b2 ^ b ^ c ^ d;
state[i * 4 + 1] = a ^ b2 ^ c2 ^ c ^ d;
state[i * 4 + 2] = a ^ b ^ c2 ^ d2 ^ d;
state[i * 4 + 3] = a2 ^ a ^ b ^ c ^ d2;
}
}
static void aes_mixcolumns_inv(uint8_t* state) {
uint8_t a, b, c, d;
for(uint8_t i = 0; i < 4; i++) {
a = state[i * 4];
b = state[i * 4 + 1];
c = state[i * 4 + 2];
d = state[i * 4 + 3];
uint8_t a2 = gf_mul2(a);
uint8_t a4 = gf_mul2(a2);
uint8_t a8 = gf_mul2(a4);
uint8_t b2 = gf_mul2(b);
uint8_t b4 = gf_mul2(b2);
uint8_t b8 = gf_mul2(b4);
uint8_t c2 = gf_mul2(c);
uint8_t c4 = gf_mul2(c2);
uint8_t c8 = gf_mul2(c4);
uint8_t d2 = gf_mul2(d);
uint8_t d4 = gf_mul2(d2);
uint8_t d8 = gf_mul2(d4);
state[i * 4] = (a8 ^ a4 ^ a2) ^ (b8 ^ b2 ^ b) ^ (c8 ^ c4 ^ c) ^ (d8 ^ d);
state[i * 4 + 1] = (a8 ^ a) ^ (b8 ^ b4 ^ b2) ^ (c8 ^ c2 ^ c) ^ (d8 ^ d4 ^ d);
state[i * 4 + 2] = (a8 ^ a4 ^ a) ^ (b8 ^ b) ^ (c8 ^ c4 ^ c2) ^ (d8 ^ d2 ^ d);
state[i * 4 + 3] = (a8 ^ a2 ^ a) ^ (b8 ^ b4 ^ b) ^ (c8 ^ c) ^ (d8 ^ d4 ^ d2);
}
}
static void aes_addroundkey(uint8_t* state, const uint8_t* round_key) {
for(uint8_t col = 0; col < 4; col++) {
state[col * 4] ^= round_key[col * 4];
state[col * 4 + 1] ^= round_key[col * 4 + 1];
state[col * 4 + 2] ^= round_key[col * 4 + 2];
state[col * 4 + 3] ^= round_key[col * 4 + 3];
}
}
void aes_key_expansion(const uint8_t* key, uint8_t* round_keys) {
for(uint8_t i = 0; i < 16; i++) {
round_keys[i] = key[i];
}
for(uint8_t i = 4; i < 44; i++) {
uint8_t prev_word_idx = (i - 1) * 4;
uint8_t b0 = round_keys[prev_word_idx];
uint8_t b1 = round_keys[prev_word_idx + 1];
uint8_t b2 = round_keys[prev_word_idx + 2];
uint8_t b3 = round_keys[prev_word_idx + 3];
if((i % 4) == 0) {
uint8_t new_b0 = aes_sbox[b1] ^ aes_rcon[(i / 4) - 1];
uint8_t new_b1 = aes_sbox[b2];
uint8_t new_b2 = aes_sbox[b3];
uint8_t new_b3 = aes_sbox[b0];
b0 = new_b0;
b1 = new_b1;
b2 = new_b2;
b3 = new_b3;
}
uint8_t back_word_idx = (i - 4) * 4;
b0 ^= round_keys[back_word_idx];
b1 ^= round_keys[back_word_idx + 1];
b2 ^= round_keys[back_word_idx + 2];
b3 ^= round_keys[back_word_idx + 3];
uint8_t curr_word_idx = i * 4;
round_keys[curr_word_idx] = b0;
round_keys[curr_word_idx + 1] = b1;
round_keys[curr_word_idx + 2] = b2;
round_keys[curr_word_idx + 3] = b3;
}
}
void aes128_encrypt(const uint8_t* expanded_key, uint8_t* data) {
uint8_t state[16];
memcpy(state, data, 16);
aes_addroundkey(state, &expanded_key[0]);
for(uint8_t round = 1; round < 10; round++) {
aes_subbytes(state);
aes_shiftrows(state);
aes_mixcolumns(state);
aes_addroundkey(state, &expanded_key[round * 16]);
}
aes_subbytes(state);
aes_shiftrows(state);
aes_addroundkey(state, &expanded_key[160]);
memcpy(data, state, 16);
}
void aes128_decrypt(const uint8_t* expanded_key, uint8_t* data) {
uint8_t state[16];
memcpy(state, data, 16);
aes_addroundkey(state, &expanded_key[160]);
for(uint8_t round = 9; round > 0; round--) {
aes_shiftrows_inv(state);
aes_subbytes_inv(state);
aes_addroundkey(state, &expanded_key[round * 16]);
aes_mixcolumns_inv(state);
}
aes_shiftrows_inv(state);
aes_subbytes_inv(state);
aes_addroundkey(state, &expanded_key[0]);
memcpy(data, state, 16);
}
void reverse_bits_in_bytes(uint8_t* data, uint8_t len) {
for(uint8_t i = 0; i < len; i++) {
uint8_t byte = data[i];
uint8_t step1 = ((byte & 0x55) << 1) | ((byte >> 1) & 0x55);
uint8_t step2 = ((step1 & 0x33) << 2) | ((step1 >> 2) & 0x33);
data[i] = ((step2 & 0x0F) << 4) | (step2 >> 4);
}
}

View File

@@ -0,0 +1,10 @@
#pragma once
#include "base.h"
#include <furi.h>
void reverse_bits_in_bytes(uint8_t* data, uint8_t len);
void aes128_decrypt(const uint8_t* expanded_key, uint8_t* data);
void aes128_encrypt(const uint8_t* expanded_key, uint8_t* data);
void aes_key_expansion(const uint8_t* key, uint8_t* round_keys);

View File

@@ -0,0 +1,679 @@
#include "beninca_arc.h"
#include "../blocks/const.h"
#include "../blocks/decoder.h"
#include "../blocks/encoder.h"
#include "../blocks/generic.h"
#include "../blocks/math.h"
#include "core/log.h"
#include <stddef.h>
#include <stdint.h>
#include "aes_common.h"
#include "../blocks/custom_btn_i.h"
#define TAG "BenincaARC"
#define BENINCA_ARC_KEY_TYPE 9u
static const SubGhzBlockConst subghz_protocol_beninca_arc_const = {
.te_short = 300,
.te_long = 600,
.te_delta = 155,
.min_count_bit_for_found = 128,
};
typedef enum {
BenincaARCDecoderStart = 0,
BenincaARCDecoderHighLevel,
BenincaARCDecoderLowLevel,
} BenincaARCDecoderState;
struct SubGhzProtocolDecoderBenincaARC {
SubGhzProtocolDecoderBase base;
SubGhzBlockDecoder decoder;
SubGhzBlockGeneric generic;
SubGhzKeystore* keystore;
};
struct SubGhzProtocolEncoderBenincaARC {
SubGhzProtocolEncoderBase base;
SubGhzProtocolBlockEncoder encoder;
SubGhzBlockGeneric generic;
SubGhzKeystore* keystore;
};
const SubGhzProtocolDecoder subghz_protocol_beninca_arc_decoder = {
.alloc = subghz_protocol_decoder_beninca_arc_alloc,
.free = subghz_protocol_decoder_beninca_arc_free,
.feed = subghz_protocol_decoder_beninca_arc_feed,
.reset = subghz_protocol_decoder_beninca_arc_reset,
.get_hash_data = subghz_protocol_decoder_beninca_arc_get_hash_data,
.serialize = subghz_protocol_decoder_beninca_arc_serialize,
.deserialize = subghz_protocol_decoder_beninca_arc_deserialize,
.get_string = subghz_protocol_decoder_beninca_arc_get_string,
};
const SubGhzProtocolEncoder subghz_protocol_beninca_arc_encoder = {
.alloc = subghz_protocol_encoder_beninca_arc_alloc,
.free = subghz_protocol_encoder_beninca_arc_free,
.deserialize = subghz_protocol_encoder_beninca_arc_deserialize,
.stop = subghz_protocol_encoder_beninca_arc_stop,
.yield = subghz_protocol_encoder_beninca_arc_yield,
};
const SubGhzProtocol subghz_protocol_beninca_arc = {
.name = SUBGHZ_PROTOCOL_BENINCA_ARC_NAME,
.type = SubGhzProtocolTypeDynamic,
.flag = SubGhzProtocolFlag_433 | SubGhzProtocolFlag_AM | SubGhzProtocolFlag_Decodable |
SubGhzProtocolFlag_Load | SubGhzProtocolFlag_Save | SubGhzProtocolFlag_Send,
.decoder = &subghz_protocol_beninca_arc_decoder,
.encoder = &subghz_protocol_beninca_arc_encoder,
};
// Get custom button code
static uint8_t subghz_protocol_beninca_arc_get_btn_code(void) {
uint8_t custom_btn_id = subghz_custom_btn_get();
uint8_t original_btn_code = subghz_custom_btn_get_original();
uint8_t btn = original_btn_code;
// Set custom button
if((custom_btn_id == SUBGHZ_CUSTOM_BTN_OK) && (original_btn_code != 0)) {
// Restore original button code
btn = original_btn_code;
} else if(custom_btn_id == SUBGHZ_CUSTOM_BTN_UP) {
switch(original_btn_code) {
case 0x02:
btn = 0x04;
break;
case 0x04:
btn = 0x02;
break;
case 0xFF:
btn = 0x04;
break;
default:
break;
}
} else if(custom_btn_id == SUBGHZ_CUSTOM_BTN_DOWN) {
switch(original_btn_code) {
case 0x02:
btn = 0xFF;
break;
case 0x04:
btn = 0xFF;
break;
case 0xFF:
btn = 0x02;
break;
default:
break;
}
}
return btn;
}
static void get_subghz_protocol_beninca_arc_aes_key(SubGhzKeystore* keystore, uint8_t* aes_key) {
uint64_t mfkey = 0;
for
M_EACH(manufacture_code, *subghz_keystore_get_data(keystore), SubGhzKeyArray_t) {
if(manufacture_code->type == BENINCA_ARC_KEY_TYPE) {
mfkey = manufacture_code->key;
break;
}
}
uint32_t derived_lo = (uint32_t)(mfkey & 0xFFFFFFFF);
uint32_t derived_hi = (uint32_t)((mfkey >> 32) & 0xFFFFFFFF);
uint64_t val64_a = ((uint64_t)derived_hi << 32) | derived_lo;
for(uint8_t i = 0; i < 8; i++) {
aes_key[i] = (val64_a >> (56 - i * 8)) & 0xFF;
}
uint32_t new_lo = ((derived_hi >> 24) & 0xFF) | ((derived_hi >> 8) & 0xFF00) |
((derived_hi << 8) & 0xFF0000) | ((derived_hi << 24) & 0xFF000000);
uint32_t new_hi = ((derived_lo >> 24) & 0xFF) | ((derived_lo >> 8) & 0xFF00) |
((derived_lo << 8) & 0xFF0000) | ((derived_lo << 24) & 0xFF000000);
uint64_t val64_b = ((uint64_t)new_hi << 32) | new_lo;
for(uint8_t i = 0; i < 8; i++) {
aes_key[i + 8] = (val64_b >> (56 - i * 8)) & 0xFF;
}
}
static uint64_t
subghz_protocol_beninca_arc_decrypt(SubGhzBlockGeneric* generic, SubGhzKeystore* keystore) {
// Beninca ARC Decoder
// 01.2026 - @xMasterX (MMX) & @zero-mega
// Decrypt data
uint8_t encrypted_data[16];
for(uint8_t i = 0; i < 8; i++) {
encrypted_data[i] = (generic->data >> (56 - i * 8)) & 0xFF;
encrypted_data[i + 8] = (generic->data_2 >> (56 - i * 8)) & 0xFF;
}
reverse_bits_in_bytes(encrypted_data, 16);
uint8_t aes_key[16];
get_subghz_protocol_beninca_arc_aes_key(keystore, aes_key);
uint8_t expanded_key[176];
aes_key_expansion(aes_key, expanded_key);
aes128_decrypt(expanded_key, encrypted_data);
// Serial number of remote
generic->serial = ((uint32_t)encrypted_data[0] << 24) | ((uint32_t)encrypted_data[1] << 16) |
((uint32_t)encrypted_data[2] << 8) | encrypted_data[3];
// Button code
generic->btn = encrypted_data[4];
// Middle bytes contains mini counter that is increased while button is held
// its value mostly stored in encrypted_data[9] but might be in other bytes as well
// In order to support all variants we read all middle bytes as uint64_t
// In case you have the remote with ARC rolling code please share RAW recording where you hold button for 15+ sec with us to improve this part!
uint64_t middle_bytes = 0;
middle_bytes = ((uint64_t)encrypted_data[5] << 32) | ((uint64_t)encrypted_data[6] << 24) |
((uint64_t)encrypted_data[7] << 16) | ((uint64_t)encrypted_data[8] << 8) |
encrypted_data[9];
// 32-bit counter
generic->cnt = ((uint32_t)encrypted_data[10] << 24) | ((uint32_t)encrypted_data[11] << 16) |
((uint32_t)encrypted_data[12] << 8) | encrypted_data[13];
// Fixed constant value AA 55
generic->seed = ((uint16_t)encrypted_data[14] << 8) | encrypted_data[15];
// Save original button for later use
if(subghz_custom_btn_get_original() == 0) {
subghz_custom_btn_set_original(generic->btn);
}
subghz_custom_btn_set_max(2);
return middle_bytes;
}
static void subghz_protocol_beninca_arc_encrypt(
SubGhzBlockGeneric* generic,
SubGhzKeystore* keystore,
uint64_t middle_bytes) {
// Beninca ARC Encoder
// 01.2026 - @xMasterX (MMX) & @zero-mega
// Encrypt data
uint8_t plaintext[16];
plaintext[0] = (generic->serial >> 24) & 0xFF;
plaintext[1] = (generic->serial >> 16) & 0xFF;
plaintext[2] = (generic->serial >> 8) & 0xFF;
plaintext[3] = generic->serial & 0xFF;
plaintext[4] = generic->btn;
plaintext[5] = (middle_bytes >> 32) & 0xFF;
plaintext[6] = (middle_bytes >> 24) & 0xFF;
plaintext[7] = (middle_bytes >> 16) & 0xFF;
plaintext[8] = (middle_bytes >> 8) & 0xFF;
plaintext[9] = middle_bytes & 0xFF;
plaintext[10] = (generic->cnt >> 24) & 0xFF;
plaintext[11] = (generic->cnt >> 16) & 0xFF;
plaintext[12] = (generic->cnt >> 8) & 0xFF;
plaintext[13] = generic->cnt & 0xFF;
plaintext[14] = (generic->seed >> 8) & 0xFF;
plaintext[15] = generic->seed & 0xFF;
uint8_t aes_key[16];
get_subghz_protocol_beninca_arc_aes_key(keystore, aes_key);
uint8_t expanded_key[176];
aes_key_expansion(aes_key, expanded_key);
aes128_encrypt(expanded_key, plaintext);
reverse_bits_in_bytes(plaintext, 16);
for(uint8_t i = 0; i < 8; i++) {
generic->data = (generic->data << 8) | plaintext[i];
generic->data_2 = (generic->data_2 << 8) | plaintext[i + 8];
}
return;
}
void* subghz_protocol_encoder_beninca_arc_alloc(SubGhzEnvironment* environment) {
SubGhzProtocolEncoderBenincaARC* instance = malloc(sizeof(SubGhzProtocolEncoderBenincaARC));
instance->base.protocol = &subghz_protocol_beninca_arc;
instance->generic.protocol_name = instance->base.protocol->name;
instance->keystore = subghz_environment_get_keystore(environment);
instance->encoder.repeat = 10;
instance->encoder.size_upload = 800;
instance->encoder.upload = malloc(instance->encoder.size_upload * sizeof(LevelDuration));
instance->encoder.is_running = false;
return instance;
}
void subghz_protocol_encoder_beninca_arc_free(void* context) {
furi_assert(context);
SubGhzProtocolEncoderBenincaARC* instance = context;
free(instance->encoder.upload);
free(instance);
}
void subghz_protocol_encoder_beninca_arc_stop(void* context) {
furi_assert(context);
SubGhzProtocolEncoderBenincaARC* instance = context;
instance->encoder.is_running = false;
}
static void subghz_protocol_beninca_arc_encoder_get_upload(
SubGhzProtocolEncoderBenincaARC* instance,
size_t* index) {
furi_assert(instance);
size_t index_local = *index;
// First part of data 64 bits
for(uint8_t i = 64; i > 0; i--) {
if(bit_read(instance->generic.data, i - 1)) {
// Send bit 1
instance->encoder.upload[index_local++] =
level_duration_make(true, (uint32_t)subghz_protocol_beninca_arc_const.te_short);
instance->encoder.upload[index_local++] =
level_duration_make(false, (uint32_t)subghz_protocol_beninca_arc_const.te_long);
} else {
// Send bit 0
instance->encoder.upload[index_local++] =
level_duration_make(true, (uint32_t)subghz_protocol_beninca_arc_const.te_long);
instance->encoder.upload[index_local++] =
level_duration_make(false, (uint32_t)subghz_protocol_beninca_arc_const.te_short);
}
}
// Second part of data 64 bits - total 128bits data
for(uint8_t i = 64; i > 0; i--) {
if(bit_read(instance->generic.data_2, i - 1)) {
// Send bit 1
instance->encoder.upload[index_local++] =
level_duration_make(true, (uint32_t)subghz_protocol_beninca_arc_const.te_short);
instance->encoder.upload[index_local++] =
level_duration_make(false, (uint32_t)subghz_protocol_beninca_arc_const.te_long);
} else {
// Send bit 0
instance->encoder.upload[index_local++] =
level_duration_make(true, (uint32_t)subghz_protocol_beninca_arc_const.te_long);
instance->encoder.upload[index_local++] =
level_duration_make(false, (uint32_t)subghz_protocol_beninca_arc_const.te_short);
}
}
// Add stop bit
instance->encoder.upload[index_local++] =
level_duration_make(true, (uint32_t)subghz_protocol_beninca_arc_const.te_short);
// Add gap between packets
instance->encoder.upload[index_local++] =
level_duration_make(false, (uint32_t)subghz_protocol_beninca_arc_const.te_long * 15);
*index = index_local;
}
static void subghz_protocol_beninca_arc_encoder_prepare_packets(
SubGhzProtocolEncoderBenincaARC* instance) {
furi_assert(instance);
// Counter increment
// check OFEX mode
if(furi_hal_subghz_get_rolling_counter_mult() != -0x7FFFFFFF) {
// standart counter mode. PULL data from subghz_block_generic_global variables
if(!subghz_block_generic_global_counter_override_get(&instance->generic.cnt)) {
// if counter_override_get return FALSE then counter was not changed and we increase counter by standart mult value
if((instance->generic.cnt + furi_hal_subghz_get_rolling_counter_mult()) > 0xFFFFFFFF) {
instance->generic.cnt = 0;
} else {
instance->generic.cnt += furi_hal_subghz_get_rolling_counter_mult();
}
}
} else {
// TODO: OFEX mode
instance->generic.cnt += 1;
}
// Index for upload array
size_t index = 0;
// Generate new key using custom or default button
instance->generic.btn = subghz_protocol_beninca_arc_get_btn_code();
// Make 3 packets with different mini counter values - 2, 4, 6
for(uint8_t i = 0; i < 3; i++) {
subghz_protocol_beninca_arc_encrypt(
&instance->generic, instance->keystore, (uint64_t)((i + 1) * 2));
subghz_protocol_beninca_arc_encoder_get_upload(instance, &index);
}
// Set final size of upload array
instance->encoder.size_upload = index;
}
bool subghz_protocol_beninca_arc_create_data(
void* context,
FlipperFormat* flipper_format,
uint32_t serial,
uint8_t btn,
uint32_t cnt,
SubGhzRadioPreset* preset) {
furi_assert(context);
// UwU
SubGhzProtocolEncoderBenincaARC* instance = context;
instance->generic.serial = serial;
instance->generic.btn = btn; // 02 / 04
instance->generic.cnt = cnt;
instance->generic.seed = 0xAA55; // Fixed value constant
instance->generic.data_count_bit = 128;
subghz_protocol_beninca_arc_encrypt(&instance->generic, instance->keystore, 0x1);
SubGhzProtocolStatus res =
subghz_block_generic_serialize(&instance->generic, flipper_format, preset);
uint8_t key_data[sizeof(uint64_t)] = {0};
for(size_t i = 0; i < sizeof(uint64_t); i++) {
key_data[sizeof(uint64_t) - i - 1] = (instance->generic.data_2 >> (i * 8)) & 0xFF;
}
if(!flipper_format_rewind(flipper_format)) {
FURI_LOG_E(TAG, "Rewind error");
res = SubGhzProtocolStatusErrorParserOthers;
}
if((res == SubGhzProtocolStatusOk) &&
!flipper_format_insert_or_update_hex(flipper_format, "Data", key_data, sizeof(uint64_t))) {
FURI_LOG_E(TAG, "Unable to add Data2");
res = SubGhzProtocolStatusErrorParserOthers;
}
return res == SubGhzProtocolStatusOk;
}
SubGhzProtocolStatus
subghz_protocol_encoder_beninca_arc_deserialize(void* context, FlipperFormat* flipper_format) {
furi_assert(context);
SubGhzProtocolEncoderBenincaARC* instance = context;
SubGhzProtocolStatus res = SubGhzProtocolStatusError;
do {
if(SubGhzProtocolStatusOk !=
subghz_block_generic_deserialize(&instance->generic, flipper_format)) {
FURI_LOG_E(TAG, "Deserialize error");
break;
}
//optional parameter parameter
flipper_format_read_uint32(
flipper_format, "Repeat", (uint32_t*)&instance->encoder.repeat, 1);
if(!flipper_format_rewind(flipper_format)) {
FURI_LOG_E(TAG, "Rewind error");
break;
}
uint8_t key_data[sizeof(uint64_t)] = {0};
if(!flipper_format_read_hex(flipper_format, "Data", key_data, sizeof(uint64_t))) {
FURI_LOG_E(TAG, "Missing Data");
break;
}
for(uint8_t i = 0; i < sizeof(uint64_t); i++) {
instance->generic.data_2 = instance->generic.data_2 << 8 | key_data[i];
}
// TODO: if minicounter having larger value use it instead of fixed values
subghz_protocol_beninca_arc_decrypt(&instance->generic, instance->keystore);
subghz_protocol_beninca_arc_encoder_prepare_packets(instance);
if(!flipper_format_rewind(flipper_format)) {
FURI_LOG_E(TAG, "Rewind error");
break;
}
for(size_t i = 0; i < sizeof(uint64_t); i++) {
key_data[sizeof(uint64_t) - i - 1] = (instance->generic.data >> i * 8) & 0xFF;
}
if(!flipper_format_update_hex(flipper_format, "Key", key_data, sizeof(uint64_t))) {
FURI_LOG_E(TAG, "Unable to update Key");
break;
}
for(size_t i = 0; i < sizeof(uint64_t); i++) {
key_data[sizeof(uint64_t) - i - 1] = (instance->generic.data_2 >> i * 8) & 0xFF;
}
if(!flipper_format_update_hex(flipper_format, "Data", key_data, sizeof(uint64_t))) {
FURI_LOG_E(TAG, "Unable to update Data");
break;
}
instance->encoder.is_running = true;
res = SubGhzProtocolStatusOk;
} while(false);
return res;
}
LevelDuration subghz_protocol_encoder_beninca_arc_yield(void* context) {
furi_assert(context);
SubGhzProtocolEncoderBenincaARC* instance = context;
if(instance->encoder.repeat == 0 || !instance->encoder.is_running) {
instance->encoder.is_running = false;
return level_duration_reset();
}
LevelDuration ret = instance->encoder.upload[instance->encoder.front];
if(++instance->encoder.front == instance->encoder.size_upload) {
instance->encoder.repeat--;
instance->encoder.front = 0;
}
return ret;
}
void* subghz_protocol_decoder_beninca_arc_alloc(SubGhzEnvironment* environment) {
SubGhzProtocolDecoderBenincaARC* instance = malloc(sizeof(SubGhzProtocolDecoderBenincaARC));
instance->base.protocol = &subghz_protocol_beninca_arc;
instance->generic.protocol_name = instance->base.protocol->name;
instance->keystore = subghz_environment_get_keystore(environment);
instance->decoder.parser_step = BenincaARCDecoderStart;
return instance;
}
void subghz_protocol_decoder_beninca_arc_free(void* context) {
furi_assert(context);
SubGhzProtocolDecoderBenincaARC* instance = context;
free(instance);
}
void subghz_protocol_decoder_beninca_arc_reset(void* context) {
furi_assert(context);
SubGhzProtocolDecoderBenincaARC* instance = context;
instance->decoder.parser_step = BenincaARCDecoderStart;
}
void subghz_protocol_decoder_beninca_arc_feed(void* context, bool level, uint32_t duration) {
furi_assert(context);
SubGhzProtocolDecoderBenincaARC* instance = context;
switch(instance->decoder.parser_step) {
case BenincaARCDecoderStart:
if((!level) && (DURATION_DIFF(duration, subghz_protocol_beninca_arc_const.te_long * 16) <
subghz_protocol_beninca_arc_const.te_delta * 15)) {
// GAP (9300 +- 2325 us) found switch to next state
instance->decoder.decode_data = 0;
instance->decoder.decode_count_bit = 0;
instance->decoder.parser_step = BenincaARCDecoderHighLevel;
break;
}
// No GAP so stay in current state
break;
case BenincaARCDecoderHighLevel:
if(level) {
instance->decoder.te_last = duration;
instance->decoder.parser_step = BenincaARCDecoderLowLevel;
// Check if we have collected enough bits
if((instance->decoder.decode_count_bit ==
(subghz_protocol_beninca_arc_const.min_count_bit_for_found / 2)) &&
(instance->decoder.decode_data != 0)) {
// Half data captured 64 bits
instance->generic.data = instance->decoder.decode_data;
instance->decoder.decode_data = 0;
} else if(
instance->decoder.decode_count_bit ==
subghz_protocol_beninca_arc_const.min_count_bit_for_found) {
// Full data captured 128 bits
instance->generic.data_2 = instance->decoder.decode_data;
instance->generic.data_count_bit = instance->decoder.decode_count_bit;
instance->decoder.parser_step = BenincaARCDecoderStart;
if(instance->base.callback) {
instance->base.callback(&instance->base, instance->base.context);
}
break;
}
} else {
instance->decoder.parser_step = BenincaARCDecoderStart;
}
break;
case BenincaARCDecoderLowLevel:
if(!level) {
// Bit 1 is short and long timing = 300us HIGH (te_last) and 600us LOW
if((DURATION_DIFF(
instance->decoder.te_last, subghz_protocol_beninca_arc_const.te_short) <
subghz_protocol_beninca_arc_const.te_delta) &&
(DURATION_DIFF(duration, subghz_protocol_beninca_arc_const.te_long) <
subghz_protocol_beninca_arc_const.te_delta)) {
subghz_protocol_blocks_add_bit(&instance->decoder, 1);
instance->decoder.parser_step = BenincaARCDecoderHighLevel;
// Bit 0 is long and short timing = 600us HIGH (te_last) and 300us LOW
} else if(
(DURATION_DIFF(
instance->decoder.te_last, subghz_protocol_beninca_arc_const.te_long) <
subghz_protocol_beninca_arc_const.te_delta) &&
(DURATION_DIFF(duration, subghz_protocol_beninca_arc_const.te_short) <
subghz_protocol_beninca_arc_const.te_delta)) {
subghz_protocol_blocks_add_bit(&instance->decoder, 0);
instance->decoder.parser_step = BenincaARCDecoderHighLevel;
} else {
instance->decoder.parser_step = BenincaARCDecoderStart;
}
break;
} else {
instance->decoder.parser_step = BenincaARCDecoderStart;
break;
}
}
}
uint8_t subghz_protocol_decoder_beninca_arc_get_hash_data(void* context) {
furi_assert(context);
SubGhzProtocolDecoderBenincaARC* instance = context;
return subghz_protocol_blocks_get_hash_data(
&instance->decoder, (instance->decoder.decode_count_bit / 8) + 1);
}
SubGhzProtocolStatus subghz_protocol_decoder_beninca_arc_serialize(
void* context,
FlipperFormat* flipper_format,
SubGhzRadioPreset* preset) {
furi_assert(context);
SubGhzProtocolDecoderBenincaARC* instance = context;
SubGhzProtocolStatus ret =
subghz_block_generic_serialize(&instance->generic, flipper_format, preset);
uint8_t key_data[sizeof(uint64_t)] = {0};
for(size_t i = 0; i < sizeof(uint64_t); i++) {
key_data[sizeof(uint64_t) - i - 1] = (instance->generic.data_2 >> (i * 8)) & 0xFF;
}
if(!flipper_format_rewind(flipper_format)) {
FURI_LOG_E(TAG, "Rewind error");
ret = SubGhzProtocolStatusErrorParserOthers;
}
if((ret == SubGhzProtocolStatusOk) &&
!flipper_format_insert_or_update_hex(flipper_format, "Data", key_data, sizeof(uint64_t))) {
FURI_LOG_E(TAG, "Unable to add Data");
ret = SubGhzProtocolStatusErrorParserOthers;
}
return ret;
}
SubGhzProtocolStatus
subghz_protocol_decoder_beninca_arc_deserialize(void* context, FlipperFormat* flipper_format) {
furi_assert(context);
SubGhzProtocolDecoderBenincaARC* instance = context;
SubGhzProtocolStatus ret = SubGhzProtocolStatusError;
do {
ret = subghz_block_generic_deserialize_check_count_bit(
&instance->generic,
flipper_format,
subghz_protocol_beninca_arc_const.min_count_bit_for_found);
if(ret != SubGhzProtocolStatusOk) {
break;
}
if(!flipper_format_rewind(flipper_format)) {
FURI_LOG_E(TAG, "Rewind error");
ret = SubGhzProtocolStatusErrorParserOthers;
break;
}
uint8_t key_data[sizeof(uint64_t)] = {0};
if(!flipper_format_read_hex(flipper_format, "Data", key_data, sizeof(uint64_t))) {
FURI_LOG_E(TAG, "Missing Data");
ret = SubGhzProtocolStatusErrorParserOthers;
break;
}
for(uint8_t i = 0; i < sizeof(uint64_t); i++) {
instance->generic.data_2 = instance->generic.data_2 << 8 | key_data[i];
}
} while(false);
return ret;
}
void subghz_protocol_decoder_beninca_arc_get_string(void* context, FuriString* output) {
furi_assert(context);
SubGhzProtocolDecoderBenincaARC* instance = context;
uint64_t middle_bytes_dec =
subghz_protocol_beninca_arc_decrypt(&instance->generic, instance->keystore);
// push protocol data to global variable
subghz_block_generic_global.cnt_is_available = true;
subghz_block_generic_global.cnt_length_bit = 32;
subghz_block_generic_global.current_cnt = instance->generic.cnt;
furi_string_printf(
output,
"%s %db\r\n"
"Key1:%08llX\r\n"
"Key2:%08llX\r\n"
"Sn:%08lX Btn:%02X\r\n"
"Mc:%0lX Cnt:%0lX\r\n"
"Fx:%04lX",
instance->base.protocol->name,
instance->generic.data_count_bit,
instance->generic.data,
instance->generic.data_2,
instance->generic.serial,
instance->generic.btn,
(uint32_t)(middle_bytes_dec & 0xFFFFFFFF),
instance->generic.cnt,
instance->generic.seed & 0xFFFF);
}

View File

@@ -0,0 +1,108 @@
#pragma once
#include "base.h"
#define SUBGHZ_PROTOCOL_BENINCA_ARC_NAME "Beninca ARC"
typedef struct SubGhzProtocolDecoderBenincaARC SubGhzProtocolDecoderBenincaARC;
typedef struct SubGhzProtocolEncoderBenincaARC SubGhzProtocolEncoderBenincaARC;
extern const SubGhzProtocolDecoder subghz_protocol_beninca_arc_decoder;
extern const SubGhzProtocolEncoder subghz_protocol_beninca_arc_encoder;
extern const SubGhzProtocol subghz_protocol_beninca_arc;
/**
* Allocate SubGhzProtocolEncoderBenincaARC.
* @param environment Pointer to a SubGhzEnvironment instance
* @return SubGhzProtocolEncoderBenincaARC* pointer to a SubGhzProtocolEncoderBenincaARC instance
*/
void* subghz_protocol_encoder_beninca_arc_alloc(SubGhzEnvironment* environment);
/**
* Free SubGhzProtocolEncoderBenincaARC.
* @param context Pointer to a SubGhzProtocolEncoderBenincaARC instance
*/
void subghz_protocol_encoder_beninca_arc_free(void* context);
/**
* Deserialize and generating an upload to send.
* @param context Pointer to a SubGhzProtocolEncoderBenincaARC instance
* @param flipper_format Pointer to a FlipperFormat instance
* @return true On success
*/
SubGhzProtocolStatus
subghz_protocol_encoder_beninca_arc_deserialize(void* context, FlipperFormat* flipper_format);
/**
* Forced transmission stop.
* @param context Pointer to a SubGhzProtocolEncoderBenincaARC instance
*/
void subghz_protocol_encoder_beninca_arc_stop(void* context);
/**
* Getting the level and duration of the upload to be loaded into DMA.
* @param context Pointer to a SubGhzProtocolEncoderBenincaARC instance
* @return LevelDuration
*/
LevelDuration subghz_protocol_encoder_beninca_arc_yield(void* context);
/**
* Allocate SubGhzProtocolDecoderBenincaARC.
* @param environment Pointer to a SubGhzEnvironment instance
* @return SubGhzProtocolDecoderBenincaARC* pointer to a SubGhzProtocolDecoderBenincaARC instance
*/
void* subghz_protocol_decoder_beninca_arc_alloc(SubGhzEnvironment* environment);
/**
* Free SubGhzProtocolDecoderBenincaARC.
* @param context Pointer to a SubGhzProtocolDecoderBenincaARC instance
*/
void subghz_protocol_decoder_beninca_arc_free(void* context);
/**
* Reset decoder SubGhzProtocolDecoderBenincaARC.
* @param context Pointer to a SubGhzProtocolDecoderBenincaARC instance
*/
void subghz_protocol_decoder_beninca_arc_reset(void* context);
/**
* Parse a raw sequence of levels and durations received from the air.
* @param context Pointer to a SubGhzProtocolDecoderBenincaARC instance
* @param level Signal level true-high false-low
* @param duration Duration of this level in, us
*/
void subghz_protocol_decoder_beninca_arc_feed(void* context, bool level, uint32_t duration);
/**
* Getting the hash sum of the last randomly received parcel.
* @param context Pointer to a SubGhzProtocolDecoderBenincaARC instance
* @return hash Hash sum
*/
uint8_t subghz_protocol_decoder_beninca_arc_get_hash_data(void* context);
/**
* Serialize data SubGhzProtocolDecoderBenincaARC.
* @param context Pointer to a SubGhzProtocolDecoderBenincaARC instance
* @param flipper_format Pointer to a FlipperFormat instance
* @param preset The modulation on which the signal was received, SubGhzRadioPreset
* @return status
*/
SubGhzProtocolStatus subghz_protocol_decoder_beninca_arc_serialize(
void* context,
FlipperFormat* flipper_format,
SubGhzRadioPreset* preset);
/**
* Deserialize data SubGhzProtocolDecoderBenincaARC.
* @param context Pointer to a SubGhzProtocolDecoderBenincaARC instance
* @param flipper_format Pointer to a FlipperFormat instance
* @return status
*/
SubGhzProtocolStatus
subghz_protocol_decoder_beninca_arc_deserialize(void* context, FlipperFormat* flipper_format);
/**
* Getting a textual representation of the received data.
* @param context Pointer to a SubGhzProtocolDecoderBenincaARC instance
* @param output Resulting text
*/
void subghz_protocol_decoder_beninca_arc_get_string(void* context, FuriString* output);

View File

@@ -25,6 +25,7 @@
#define KEELOQ_LEARNING_MAGIC_SERIAL_TYPE_1 6u
#define KEELOQ_LEARNING_MAGIC_SERIAL_TYPE_2 7u
#define KEELOQ_LEARNING_MAGIC_SERIAL_TYPE_3 8u
// #define BENINCA_ARC_KEY_TYPE 9u -- RESERVED
/**
* Simple Learning Encrypt

View File

@@ -27,6 +27,7 @@ const SubGhzProtocol* const subghz_protocol_registry_items[] = {
&subghz_protocol_hay21, &subghz_protocol_revers_rb2,
&subghz_protocol_feron, &subghz_protocol_roger,
&subghz_protocol_elplast, &subghz_protocol_treadmill37,
&subghz_protocol_beninca_arc,
};
const SubGhzProtocolRegistry subghz_protocol_registry = {

View File

@@ -54,3 +54,4 @@
#include "roger.h"
#include "elplast.h"
#include "treadmill37.h"
#include "beninca_arc.h"

View File

@@ -213,6 +213,24 @@ bool subghz_protocol_kinggates_stylo_4k_create_data(
uint16_t cnt,
SubGhzRadioPreset* preset);
/**
* Key generation from simple data.
* @param context Pointer to a SubGhzProtocolEncoderBenincaARC instance
* @param flipper_format Pointer to a FlipperFormat instance
* @param serial Serial number, 32 bit
* @param btn Button number, 8 bit
* @param cnt Counter value, 32 bit
* @param preset Modulation, SubGhzRadioPreset
* @return true On success
*/
bool subghz_protocol_beninca_arc_create_data(
void* context,
FlipperFormat* flipper_format,
uint32_t serial,
uint8_t btn,
uint32_t cnt,
SubGhzRadioPreset* preset);
typedef struct SubGhzProtocolDecoderBinRAW SubGhzProtocolDecoderBinRAW;
void subghz_protocol_decoder_bin_raw_data_input_rssi(