From 775d4a84f36d4d5d0700e9c44dfa4114a0cc022c Mon Sep 17 00:00:00 2001 From: Sadeep Madurange Date: Sun, 27 Apr 2025 13:16:46 +0800 Subject: Yay! working with session key encryption. --- lock/bend.c | 19 ++++++++++++++----- lock/fend.c | 44 +++++++++++++++++++++++++++----------------- 2 files changed, 41 insertions(+), 22 deletions(-) diff --git a/lock/bend.c b/lock/bend.c index d7459eb..3957c55 100644 --- a/lock/bend.c +++ b/lock/bend.c @@ -36,7 +36,6 @@ static char tab[] = { 'P', 'f', ':', 'B', ']', 'Y', '^', 'F', '%', 'C', 'x' }; -static uint8_t sync = 0; static volatile uint8_t rxd = 0; static uint16_t tablen = sizeof(tab) / sizeof(tab[0]); @@ -129,10 +128,20 @@ int main(void) radio_recv(buf, WDLEN); rxd = 0; xor(KEY, buf, msg, WDLEN); - if (memcmp(msg, LOCK, WDLEN) == 0) - lock(); - else if (memcmp(msg, UNLOCK, WDLEN) == 0) - unlock(); + if (memcmp(msg, SYN, WDLEN) == 0) { + keygen(key, WDLEN); + xor(KEY, key, buf, WDLEN); + radio_sendto(txaddr, buf, WDLEN); + } else { + xor(key, buf, msg, WDLEN); + if (memcmp(msg, LOCK, WDLEN) == 0) { + lock(); + keydel(key, WDLEN); + } else if (memcmp(msg, UNLOCK, WDLEN) == 0) { + unlock(); + keydel(key, WDLEN); + } + } } } return 0; diff --git a/lock/fend.c b/lock/fend.c index cae6a94..966bfde 100644 --- a/lock/fend.c +++ b/lock/fend.c @@ -21,8 +21,8 @@ #define RX_PCMSK PCMSK2 #define RX_PCINTVEC PCINT2_vect -uint8_t sync = 0; static volatile uint8_t rxd = 0; +static volatile uint8_t sync = 0; static volatile uint8_t islock = 0; static volatile uint8_t isunlock = 0; @@ -45,6 +45,7 @@ static inline void init_btns(void) int main(void) { + uint8_t n; uint8_t rxaddr[ADDRLEN] = { 194, 178, 82 }; uint8_t txaddr[ADDRLEN] = { 194, 178, 83 }; @@ -58,28 +59,33 @@ int main(void) radio_print_config(); sei(); + radio_listen(); for (;;) { - if (islock) { - xor(KEY, LOCK, buf, WDLEN); + if (!sync && (islock || isunlock)) { + xor(KEY, SYN, buf, WDLEN); do { sync = radio_sendto(txaddr, buf, WDLEN); _delay_ms(50); } while (!sync); - sync = 0; - islock = 0; - uart_write_line("sent LOCK"); + uart_write_line("sent SYN"); } - if (isunlock) { - xor(KEY, UNLOCK, buf, WDLEN); - do { - sync = radio_sendto(txaddr, buf, WDLEN); - _delay_ms(50); - } while (!sync); - sync = 0; - isunlock = 0; - uart_write_line("sent UNLOCK"); + if (rxd) { + n = radio_recv(buf, WDLEN); + rxd = 0; + if (sync && (islock || isunlock)) { + sync = 0; + xor(KEY, buf, key, WDLEN); + if (islock) { + islock = 0; + xor(key, LOCK, buf, WDLEN); + } else if (isunlock) { + isunlock = 0; + xor(key, UNLOCK, buf, WDLEN); + } + radio_sendto(txaddr, buf, WDLEN); + } } } return 0; @@ -92,12 +98,16 @@ ISR(RX_PCINTVEC) ISR(INT0_vect) { - if (is_btn_pressed(PIND, LOCK_PIN)) + if (is_btn_pressed(PIND, LOCK_PIN)) { + sync = 0; islock = 1; + } } ISR(INT1_vect) { - if (is_btn_pressed(PIND, UNLOCK_PIN)) + if (is_btn_pressed(PIND, UNLOCK_PIN)) { + sync = 0; isunlock = 1; + } } -- cgit v1.2.3