Files
0mega 62b31bfe95 More Memory optimizations and new protocols
- Split RX registries into more plugins: AM, AM VAG, FM, FM F4, and FM Honda1
- Add per-protocol TX plugins so emulation loads only the selected encoder
- Sub Decode (enabled by default) and Timing Tuner as plugins
- Max history increased to 20 signals
- Fix Sub Decode and simplified UI
- Add Check Saved setting ported from dexter_pester PR !
- Fix Fiat V1 decoder and add HITAG2 key TX support
- Add Renault V0, Fiat V2, Honda V2, Ford V3 (& US variant) Thanks Ash

Sorry this is a lot at once x)
2026-06-30 21:28:26 +02:00

95 lines
2.8 KiB
C

#include "psa_crypto.h"
#include "../defines.h"
#define TEA_DELTA 0x9E3779B9U
#define TEA_ROUNDS 32
const uint32_t psa_crypto_bf1_key_schedule[4] = {
0x4A434915U,
0xD6743C2BU,
0x1F29D308U,
0xE6B79A64U,
};
const uint32_t psa_crypto_bf2_key_schedule[4] = {
0x4039C240U,
0xEDA92CABU,
0x4306C02AU,
0x02192A04U,
};
#if PROTOPIRATE_WITH_ENCODER
void psa_crypto_tea_encrypt(uint32_t* v0, uint32_t* v1, const uint32_t* key) {
uint32_t sum = 0;
for(int i = 0; i < TEA_ROUNDS; i++) {
uint32_t k_idx1 = sum & 3;
uint32_t temp = key[k_idx1] + sum;
sum = sum + TEA_DELTA;
*v0 = *v0 + (temp ^ (((*v1 >> 5) ^ (*v1 << 4)) + *v1));
uint32_t k_idx2 = (sum >> 11) & 3;
temp = key[k_idx2] + sum;
*v1 = *v1 + (temp ^ (((*v0 >> 5) ^ (*v0 << 4)) + *v0));
}
}
#endif
void psa_crypto_setup_byte_buffer(
uint8_t* buffer,
uint32_t key1_low,
uint32_t key1_high,
uint32_t key2_low) {
for(int i = 0; i < 8; i++) {
int shift = i * 8;
uint8_t byte_val;
if(shift < 32) {
byte_val = (uint8_t)((key1_low >> shift) & 0xFF);
} else {
byte_val = (uint8_t)((key1_high >> (shift - 32)) & 0xFF);
}
buffer[7 - i] = byte_val;
}
buffer[9] = (uint8_t)(key2_low & 0xFF);
buffer[8] = (uint8_t)((key2_low >> 8) & 0xFF);
}
void psa_crypto_prepare_tea_data(const uint8_t* buffer, uint32_t* w0, uint32_t* w1) {
*w0 = ((uint32_t)buffer[3] << 16) | ((uint32_t)buffer[2] << 24) | ((uint32_t)buffer[4] << 8) |
(uint32_t)buffer[5];
*w1 = ((uint32_t)buffer[7] << 16) | ((uint32_t)buffer[6] << 24) | ((uint32_t)buffer[8] << 8) |
(uint32_t)buffer[9];
}
uint8_t psa_crypto_tea_crc(uint32_t v0, uint32_t v1) {
uint32_t crc = ((v0 >> 24) & 0xFF) + ((v0 >> 16) & 0xFF) + ((v0 >> 8) & 0xFF) + (v0 & 0xFF);
crc += ((v1 >> 24) & 0xFF) + ((v1 >> 16) & 0xFF) + ((v1 >> 8) & 0xFF);
return (uint8_t)(crc & 0xFF);
}
uint16_t psa_crypto_crc16_bf2(uint8_t* buffer, int length) {
uint16_t crc = 0;
for(int i = 0; i < length; i++) {
crc = crc ^ ((uint16_t)buffer[i] << 8);
for(int j = 0; j < 8; j++) {
if(crc & 0x8000) {
crc = (crc << 1) ^ 0x8005;
} else {
crc = crc << 1;
}
crc = crc & 0xFFFF;
}
}
return crc & 0xFFFF;
}
void psa_crypto_unpack_tea_result_to_buffer(uint8_t* buffer, uint32_t v0, uint32_t v1) {
buffer[2] = (uint8_t)((v0 >> 24) & 0xFF);
buffer[3] = (uint8_t)((v0 >> 16) & 0xFF);
buffer[4] = (uint8_t)((v0 >> 8) & 0xFF);
buffer[5] = (uint8_t)(v0 & 0xFF);
buffer[6] = (uint8_t)((v1 >> 24) & 0xFF);
buffer[7] = (uint8_t)((v1 >> 16) & 0xFF);
buffer[8] = (uint8_t)((v1 >> 8) & 0xFF);
buffer[9] = (uint8_t)(v1 & 0xFF);
}