Refactor KeeLoq crypto functions and add new KeeLoq TX app (#2990)

This commit is contained in:
lifegame1lu111
2026-02-16 14:19:50 +00:00
committed by GitHub
parent 00f40bf171
commit 83a4580353
14 changed files with 545 additions and 37 deletions
+36
View File
@@ -0,0 +1,36 @@
#include "keeloq_common.hpp"
uint32_t 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;
}
uint32_t 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;
}
uint64_t keeloq_normal_learning(uint32_t data, const uint64_t key) {
uint32_t k1, k2;
data &= 0x0FFFFFFF;
data |= 0x20000000;
k1 = keeloq_decrypt(data, key);
data &= 0x0FFFFFFF;
data |= 0x60000000;
k2 = keeloq_decrypt(data, key);
return ((uint64_t)k2 << 32) | k1;
}