From 31f41ecf75678e41a03f5aaf32e8e17ee81286e1 Mon Sep 17 00:00:00 2001 From: Sadeep Madurange Date: Fri, 11 Apr 2025 11:39:53 +0800 Subject: Add keygen() to utils. --- lock/Back.Makefile | 44 ----------------- lock/Frnt.Makefile | 44 ----------------- lock/Scan.Makefile | 44 +++++++++++++++++ lock/Servo.Makefile | 44 +++++++++++++++++ lock/back.c | 136 ---------------------------------------------------- lock/frnt.c | 89 ---------------------------------- lock/scan.c | 36 ++++++++++++++ lock/servo.c | 136 ++++++++++++++++++++++++++++++++++++++++++++++++++++ lock/util.c | 31 ++++++++++++ lock/util.h | 13 ++--- 10 files changed, 296 insertions(+), 321 deletions(-) delete mode 100644 lock/Back.Makefile delete mode 100644 lock/Frnt.Makefile create mode 100644 lock/Scan.Makefile create mode 100644 lock/Servo.Makefile delete mode 100644 lock/back.c delete mode 100644 lock/frnt.c create mode 100644 lock/scan.c create mode 100644 lock/servo.c diff --git a/lock/Back.Makefile b/lock/Back.Makefile deleted file mode 100644 index 51ede95..0000000 --- a/lock/Back.Makefile +++ /dev/null @@ -1,44 +0,0 @@ -CC = avr-gcc -MCU = atmega328p -TARGET = back - -SRC = back.c uart.c nrfm.c util.c -OBJ = $(SRC:.c=.o) - -CFLAGS = -std=gnu99 -CFLAGS += -Os -CFLAGS += -Wall -CFLAGS += -mmcu=$(MCU) -CFLAGS += -DBAUD=115200 -CFLAGS += -DF_CPU=16000000UL -CFLAGS += -ffunction-sections -fdata-sections - -LDFLAGS = -mmcu=$(MCU) -LDFLAGS += -Wl,--gc-sections - -HEX_FLAGS = -O ihex -HEX_FLAGS += -j .text -j .data - -AVRDUDE_FLAGS = -p $(MCU) -AVRDUDE_FLAGS += -c arduino -AVRDUDE_FLAGS += -P /dev/cuaU0 -AVRDUDE_FLAGS += -D -U - -%.o: %.c - $(CC) $(CFLAGS) -c -o $@ $< - -elf: $(OBJ) - $(CC) $(LDFLAGS) $(OBJ) -o $(TARGET).elf - -hex: elf - avr-objcopy $(HEX_FLAGS) $(TARGET).elf $(TARGET).hex - -upload: hex - avrdude $(AVRDUDE_FLAGS) flash:w:$(TARGET).hex:i - -.PHONY: clean - -clean: - rm *.o *.elf *.hex - - diff --git a/lock/Frnt.Makefile b/lock/Frnt.Makefile deleted file mode 100644 index f57494d..0000000 --- a/lock/Frnt.Makefile +++ /dev/null @@ -1,44 +0,0 @@ -CC = avr-gcc -MCU = atmega328p -TARGET = front - -SRC = frnt.c uart.c nrfm.c util.c -OBJ = $(SRC:.c=.o) - -CFLAGS = -std=gnu99 -CFLAGS += -Os -CFLAGS += -Wall -CFLAGS += -mmcu=$(MCU) -CFLAGS += -DBAUD=115200 -CFLAGS += -DF_CPU=16000000UL -CFLAGS += -ffunction-sections -fdata-sections - -LDFLAGS = -mmcu=$(MCU) -LDFLAGS += -Wl,--gc-sections - -HEX_FLAGS = -O ihex -HEX_FLAGS += -j .text -j .data - -AVRDUDE_FLAGS = -p $(MCU) -AVRDUDE_FLAGS += -c arduino -AVRDUDE_FLAGS += -P /dev/cuaU0 -AVRDUDE_FLAGS += -D -U - -%.o: %.c - $(CC) $(CFLAGS) -c -o $@ $< - -elf: $(OBJ) - $(CC) $(LDFLAGS) $(OBJ) -o $(TARGET).elf - -hex: elf - avr-objcopy $(HEX_FLAGS) $(TARGET).elf $(TARGET).hex - -upload: hex - avrdude $(AVRDUDE_FLAGS) flash:w:$(TARGET).hex:i - -.PHONY: clean - -clean: - rm *.o *.elf *.hex - - diff --git a/lock/Scan.Makefile b/lock/Scan.Makefile new file mode 100644 index 0000000..eedf079 --- /dev/null +++ b/lock/Scan.Makefile @@ -0,0 +1,44 @@ +CC = avr-gcc +MCU = atmega328p +TARGET = scan + +SRC = scan.c uart.c nrfm.c util.c +OBJ = $(SRC:.c=.o) + +CFLAGS = -std=gnu99 +CFLAGS += -Os +CFLAGS += -Wall +CFLAGS += -mmcu=$(MCU) +CFLAGS += -DBAUD=115200 +CFLAGS += -DF_CPU=16000000UL +CFLAGS += -ffunction-sections -fdata-sections + +LDFLAGS = -mmcu=$(MCU) +LDFLAGS += -Wl,--gc-sections + +HEX_FLAGS = -O ihex +HEX_FLAGS += -j .text -j .data + +AVRDUDE_FLAGS = -p $(MCU) +AVRDUDE_FLAGS += -c arduino +AVRDUDE_FLAGS += -P /dev/cuaU0 +AVRDUDE_FLAGS += -D -U + +%.o: %.c + $(CC) $(CFLAGS) -c -o $@ $< + +elf: $(OBJ) + $(CC) $(LDFLAGS) $(OBJ) -o $(TARGET).elf + +hex: elf + avr-objcopy $(HEX_FLAGS) $(TARGET).elf $(TARGET).hex + +upload: hex + avrdude $(AVRDUDE_FLAGS) flash:w:$(TARGET).hex:i + +.PHONY: clean + +clean: + rm *.o *.elf *.hex + + diff --git a/lock/Servo.Makefile b/lock/Servo.Makefile new file mode 100644 index 0000000..51ede95 --- /dev/null +++ b/lock/Servo.Makefile @@ -0,0 +1,44 @@ +CC = avr-gcc +MCU = atmega328p +TARGET = back + +SRC = back.c uart.c nrfm.c util.c +OBJ = $(SRC:.c=.o) + +CFLAGS = -std=gnu99 +CFLAGS += -Os +CFLAGS += -Wall +CFLAGS += -mmcu=$(MCU) +CFLAGS += -DBAUD=115200 +CFLAGS += -DF_CPU=16000000UL +CFLAGS += -ffunction-sections -fdata-sections + +LDFLAGS = -mmcu=$(MCU) +LDFLAGS += -Wl,--gc-sections + +HEX_FLAGS = -O ihex +HEX_FLAGS += -j .text -j .data + +AVRDUDE_FLAGS = -p $(MCU) +AVRDUDE_FLAGS += -c arduino +AVRDUDE_FLAGS += -P /dev/cuaU0 +AVRDUDE_FLAGS += -D -U + +%.o: %.c + $(CC) $(CFLAGS) -c -o $@ $< + +elf: $(OBJ) + $(CC) $(LDFLAGS) $(OBJ) -o $(TARGET).elf + +hex: elf + avr-objcopy $(HEX_FLAGS) $(TARGET).elf $(TARGET).hex + +upload: hex + avrdude $(AVRDUDE_FLAGS) flash:w:$(TARGET).hex:i + +.PHONY: clean + +clean: + rm *.o *.elf *.hex + + diff --git a/lock/back.c b/lock/back.c deleted file mode 100644 index dd4938a..0000000 --- a/lock/back.c +++ /dev/null @@ -1,136 +0,0 @@ -/* Door back, connected to the fingerprint scanner */ - -#include -#include - -#include -#include -#include - -#include "nrfm.h" -#include "uart.h" -#include "util.h" - -#define PWM_MIN 500 -#define PWM_MID 1500 -#define PWM_MAX 2500 -#define PWM_TOP 20000 - -#define RX_PIN PD7 -#define RX_DDR DDRD -#define RX_PORT PORTD -#define RX_PCIE PCIE2 -#define RX_PCINT PCINT23 -#define RX_PCMSK PCMSK2 -#define RX_PCINTVEC PCINT2_vect - -static char chars[] = { - '0', '8', '3', '6', 'a', 'Z', '$', '4', 'v', 'R', '@', - 'E', '1', 'o', '#', ')', '2', '5', 'q', ';', '.', 'I', - 'c', '7', '9', '*', 'L', 'V', '&', 'k', 'K', '!', 'm', - 'N', '(', 'O', 'Q', 'A', '>', 'T', 't', '?', 'S', 'h', - 'w', '/', 'n', 'W', 'l', 'M', 'e', 'H', 'j', 'g', '[', - 'P', 'f', ':', 'B', ']', 'Y', '^', 'F', '%', 'C', 'x' -}; - -static volatile int rxdr = 0; -static uint8_t charslen = sizeof(chars) / sizeof(chars[0]); - -static inline void await_reply(void) -{ - int i; - - radio_listen(); - for (i = 0; i < 500 && rxdr == 0; i += 100) - _delay_ms(100); -} - -static inline void keygen(char *buf, uint8_t n) -{ - int i; - uint8_t sreg; - uint16_t idx; - - sreg = SREG; - cli(); - idx = TCNT1; - SREG = sreg; - - for (i = 0; i < n; i++, idx++) - buf[i] = chars[(idx % charslen)] ; - buf[n - 1] = '\0'; -} - -static inline void timer_init(void) -{ - TCCR1A |= (1 << WGM11) | (1 << COM1A1); - TCCR1B |= (1 << WGM13) | (1 << CS11); - ICR1 = PWM_TOP; /* we use the same timer for servo's PWM */ -} - -int main(void) -{ - uint8_t n; - uint8_t rxaddr[ADDRLEN] = { 194, 178, 83 }; - uint8_t txaddr[ADDRLEN] = { 194, 178, 82 }; - - char buf[WDLEN + 1], key[WDLEN + 1], msg[WDLEN + 1]; - - RX_DDR &= ~(1 << RX_PIN); - RX_PORT |= (1 << RX_PIN); - PCICR |= (1 << RX_PCIE); - RX_PCMSK |= (1 << RX_PCINT); - - uart_init(); - timer_init(); - radio_init(rxaddr); - radio_print_config(); - - sei(); - radio_listen(); - - for (;;) { - if (rxdr) { - n = radio_recv(buf, WDLEN); - buf[n] = '\0'; - rxdr = 0; - - xor(KEY, buf, msg, WDLEN); - uart_write("DEBUG: msg recv = "); - uart_write_line(msg); - - if (strncmp(msg, SYN, WDLEN) == 0) { - keygen(key, WDLEN); - uart_write("DEBUG: session key = "); - uart_write_line(key); - - xor(KEY, key, msg, WDLEN); - radio_sendto(txaddr, msg, WDLEN); - await_reply(); - - if (rxdr) { - n = radio_recv(buf, WDLEN); - buf[n] = '\0'; - rxdr = 0; - xor(key, buf, msg, WDLEN); - uart_write("DEBUG: msg = "); - uart_write_line(msg); - - xor(key, ACK, msg, WDLEN); - radio_sendto(txaddr, msg, WDLEN); - } - } else { - uart_write_line("ERROR: handshake failed"); - } - } else { - uart_write_line("DEBUG: no new packets"); - _delay_ms(2000); - } - } - return 0; -} - -ISR(RX_PCINTVEC) -{ - rxdr = 1; -} diff --git a/lock/frnt.c b/lock/frnt.c deleted file mode 100644 index 4ad2d11..0000000 --- a/lock/frnt.c +++ /dev/null @@ -1,89 +0,0 @@ -/* Door front, connected to the fingerprint scanner */ - -#include -#include - -#include -#include - -#include "nrfm.h" -#include "uart.h" -#include "util.h" - -#define RX_PIN PD7 -#define RX_DDR DDRD -#define RX_PORT PORTD -#define RX_PCIE PCIE2 -#define RX_PCINT PCINT23 -#define RX_PCMSK PCMSK2 -#define RX_PCINTVEC PCINT2_vect - -// todo: atomic var -static volatile int rxdr = 0; - -static inline void await_reply(void) -{ - int i; - - radio_listen(); - for (i = 0; i < 500 && rxdr == 0; i += 100) - _delay_ms(100); -} - -int main(void) -{ - uint8_t n; - uint8_t rxaddr[ADDRLEN] = { 194, 178, 82 }; - uint8_t txaddr[ADDRLEN] = { 194, 178, 83 }; - - char buf[WDLEN + 1], key[WDLEN + 1], msg[WDLEN + 1]; - - RX_DDR &= ~(1 << RX_PIN); - RX_PORT |= (1 << RX_PIN); - PCICR |= (1 << RX_PCIE); - RX_PCMSK |= (1 << RX_PCINT); - - uart_init(); - radio_init(rxaddr); - radio_print_config(); - - sei(); - - for (;;) { - _delay_ms(2000); /* todo: fingerprint check */ - xor(KEY, SYN, msg, WDLEN); - radio_sendto(txaddr, msg, WDLEN); - await_reply(); - if (rxdr) { - uart_write_line("reply received"); - n = radio_recv(buf, WDLEN); - buf[n] = '\0'; - if (n > 0) { - rxdr = 0; - xor(KEY, buf, key, WDLEN); - xor(key, MSG, msg, WDLEN); - radio_sendto(txaddr, msg, WDLEN); - await_reply(); - if (rxdr) { - n = radio_recv(buf, WDLEN); - buf[n] = '\0'; - rxdr = 0; - xor(key, buf, msg, WDLEN); - uart_write_line(msg); - } else { - // power down - uart_write_line("ERROR: no reply"); - } - } - } else { - // power down - uart_write_line("ERROR: no session key"); - } - } - return 0; -} - -ISR(RX_PCINTVEC) -{ - rxdr = 1; -} diff --git a/lock/scan.c b/lock/scan.c new file mode 100644 index 0000000..e375111 --- /dev/null +++ b/lock/scan.c @@ -0,0 +1,36 @@ +/* Lock front, connected to the fingerprint scanner */ + +#include +#include + +#include +#include + +#include "nrfm.h" +#include "uart.h" +#include "util.h" + +int main(void) +{ + uint8_t n; + uint8_t rxaddr[ADDRLEN] = { 194, 178, 82 }; + + char buf[WDLEN + 1], msg[WDLEN + 1]; + + /* timer for keygen */ + TCCR1A |= (1 << WGM11) | (1 << COM1A1); + TCCR1B |= (1 << WGM13) | (1 << CS11); + ICR1 = 20000; + + uart_init(); + radio_init(rxaddr); + radio_print_config(); + + sei(); + + for (;;) { + } + + return 0; +} + diff --git a/lock/servo.c b/lock/servo.c new file mode 100644 index 0000000..dd4938a --- /dev/null +++ b/lock/servo.c @@ -0,0 +1,136 @@ +/* Door back, connected to the fingerprint scanner */ + +#include +#include + +#include +#include +#include + +#include "nrfm.h" +#include "uart.h" +#include "util.h" + +#define PWM_MIN 500 +#define PWM_MID 1500 +#define PWM_MAX 2500 +#define PWM_TOP 20000 + +#define RX_PIN PD7 +#define RX_DDR DDRD +#define RX_PORT PORTD +#define RX_PCIE PCIE2 +#define RX_PCINT PCINT23 +#define RX_PCMSK PCMSK2 +#define RX_PCINTVEC PCINT2_vect + +static char chars[] = { + '0', '8', '3', '6', 'a', 'Z', '$', '4', 'v', 'R', '@', + 'E', '1', 'o', '#', ')', '2', '5', 'q', ';', '.', 'I', + 'c', '7', '9', '*', 'L', 'V', '&', 'k', 'K', '!', 'm', + 'N', '(', 'O', 'Q', 'A', '>', 'T', 't', '?', 'S', 'h', + 'w', '/', 'n', 'W', 'l', 'M', 'e', 'H', 'j', 'g', '[', + 'P', 'f', ':', 'B', ']', 'Y', '^', 'F', '%', 'C', 'x' +}; + +static volatile int rxdr = 0; +static uint8_t charslen = sizeof(chars) / sizeof(chars[0]); + +static inline void await_reply(void) +{ + int i; + + radio_listen(); + for (i = 0; i < 500 && rxdr == 0; i += 100) + _delay_ms(100); +} + +static inline void keygen(char *buf, uint8_t n) +{ + int i; + uint8_t sreg; + uint16_t idx; + + sreg = SREG; + cli(); + idx = TCNT1; + SREG = sreg; + + for (i = 0; i < n; i++, idx++) + buf[i] = chars[(idx % charslen)] ; + buf[n - 1] = '\0'; +} + +static inline void timer_init(void) +{ + TCCR1A |= (1 << WGM11) | (1 << COM1A1); + TCCR1B |= (1 << WGM13) | (1 << CS11); + ICR1 = PWM_TOP; /* we use the same timer for servo's PWM */ +} + +int main(void) +{ + uint8_t n; + uint8_t rxaddr[ADDRLEN] = { 194, 178, 83 }; + uint8_t txaddr[ADDRLEN] = { 194, 178, 82 }; + + char buf[WDLEN + 1], key[WDLEN + 1], msg[WDLEN + 1]; + + RX_DDR &= ~(1 << RX_PIN); + RX_PORT |= (1 << RX_PIN); + PCICR |= (1 << RX_PCIE); + RX_PCMSK |= (1 << RX_PCINT); + + uart_init(); + timer_init(); + radio_init(rxaddr); + radio_print_config(); + + sei(); + radio_listen(); + + for (;;) { + if (rxdr) { + n = radio_recv(buf, WDLEN); + buf[n] = '\0'; + rxdr = 0; + + xor(KEY, buf, msg, WDLEN); + uart_write("DEBUG: msg recv = "); + uart_write_line(msg); + + if (strncmp(msg, SYN, WDLEN) == 0) { + keygen(key, WDLEN); + uart_write("DEBUG: session key = "); + uart_write_line(key); + + xor(KEY, key, msg, WDLEN); + radio_sendto(txaddr, msg, WDLEN); + await_reply(); + + if (rxdr) { + n = radio_recv(buf, WDLEN); + buf[n] = '\0'; + rxdr = 0; + xor(key, buf, msg, WDLEN); + uart_write("DEBUG: msg = "); + uart_write_line(msg); + + xor(key, ACK, msg, WDLEN); + radio_sendto(txaddr, msg, WDLEN); + } + } else { + uart_write_line("ERROR: handshake failed"); + } + } else { + uart_write_line("DEBUG: no new packets"); + _delay_ms(2000); + } + } + return 0; +} + +ISR(RX_PCINTVEC) +{ + rxdr = 1; +} diff --git a/lock/util.c b/lock/util.c index 95bef77..bc62438 100644 --- a/lock/util.c +++ b/lock/util.c @@ -1,5 +1,35 @@ +#include +#include + #include "util.h" +static char tab[] = { + '0', '8', '3', '6', 'a', 'Z', '$', '4', 'v', 'R', '@', + 'E', '1', 'o', '#', ')', '2', '5', 'q', ';', '.', 'I', + 'c', '7', '9', '*', 'L', 'V', '&', 'k', 'K', '!', 'm', + 'N', '(', 'O', 'Q', 'A', '>', 'T', 't', '?', 'S', 'h', + 'w', '/', 'n', 'W', 'l', 'M', 'e', 'H', 'j', 'g', '[', + 'P', 'f', ':', 'B', ']', 'Y', '^', 'F', '%', 'C', 'x' +}; + +static uint16_t tablen = sizeof(tab) / sizeof(tab[0]); + +void keygen(char *buf, uint8_t n) +{ + int i, imax; + uint8_t sreg; + uint16_t idx; + + sreg = SREG; + cli(); + idx = TCNT1; + SREG = sreg; + + for (i = 0, imax = n - 1; i < imax; i++, idx++) + buf[i] = tab[(idx % tablen)]; + buf[imax] = '\0'; +} + void xor(const char *k, const char *s, char *d, uint8_t n) { int i; @@ -7,3 +37,4 @@ void xor(const char *k, const char *s, char *d, uint8_t n) for (i = 0; i < n; i++) d[i] = s[i] ^ k[i]; } + diff --git a/lock/util.h b/lock/util.h index c3449c8..54dc66b 100644 --- a/lock/util.h +++ b/lock/util.h @@ -3,16 +3,13 @@ #include -#define WDLEN 32 +#define KEY "dM>}jdb,6gsnC$J^K 8(I5vyPemPs%;K" +#define LOCK "R,I7l^E4j]KyLR9'*Q{Jd'zu.~!84}Ij" +#define UNLOCK "R,I7l^E4j]KyLR9'*Q{Jd'zu.~!84}Ik" -#define KEY "dM>}jdb,6gsnC$J^K 8(I5vyPemPs%;K" -//#define SYN "43iqr5$NB8SpN?Z/52{iVl>o|i!.'dsT" -//#define LOCK "R,I7l^E4j]KyLR9'*Q{Jd'zu.~!84}Ij" -//#define UNLOCK "R,I7l^E4j]KyLR9'*Q{Jd'zu.~!84}Ik" +#define WDLEN 32 -#define SYN "SYNSYNSYNSYNSYNSYNSYNSYNSYNSYNSY" -#define ACK "ACKACKACKACKACKACKACKACKACKACKAC" -#define MSG "MSGMSGMSGMSGMSGMSGMSGMSGMSGMSGMS" +void keygen(char *buf, uint8_t n); void xor(const char *k, const char *s, char *d, uint8_t n); -- cgit v1.2.3