;Dongle1.asm ;Dhananjay V. Gadre ;27th July 1999 ;1st August 1999 ;Synopsis: ;--------- ;Security Dongle ;Implements an electronic lock based on LFSR. For illustration ;8-bit LFSR has been implemented. For real applications, longer ;length shift registers could be used. ;The controller is connected to the parallel port for power ;as well as for communication. ;I/O signals: PB0 ---> Data in/out ; PB1 ---> strobe_in ; PB2 ---> ACK_out ;At reset, PB0 is put in input mode to receive 2 bytes. First ;byte is a shift register preset value and the second byte ;is shift count. The program loads the shift register with the ;first byte and then rotates the shift register for a count ;equal to the second byte. AFter that PB0 is put in output ;mode and the result is shifted out. ;Data is transferred using a handshake protocol. ;Any 8 pin (or more) processor can be used. .include "2323def.inc" .equ DATAIO=0 .equ ACK=2 .equ STROBE=1 .def save_status = r0 ;used to store the SREG during an ISR .def sundry=r16 .def count=r17 .def low_del=r18 ;delay variable .def high_del=r19 ;another delay variable .def temp=r20 ;temperory variable .def temp1=r21 ;another temperory variable .def temp2=r22 ;yet another temporary variable .def lfsr_preset=r23 .def lfsr_shift=r24 .def lfsr_result=r25 .cseg .org 0 rjmp RESET ;Reset Handle RESET: ldi sundry, low(RAMEND) out SPL, sundry ;Init the Stack Pointer ldi sundry, 0b00011100 ;PB0, PB1=i/p; rest o/p out DDRB, sundry ;configure PORTB sbi PORTB, ACK ;indicate not ready main_loop: rcall get_byte mov lfsr_preset, temp rcall get_byte mov lfsr_shift, temp rcall calc_result rcall send_result rjmp main_loop get_byte: ldi temp, 0 mov temp1, temp mov temp2, temp cbi DDRB, DATAIO ;change mode of PB0 to input sbi PORTB, DATAIO get_bit: cbi PORTB, ACK ;now ready to take data stb_0: sbis PINB, STROBE rjmp stb_0 sbi PORTB, ACK in temp, PINB andi temp, 1 lsl temp1 or temp1, temp stb_1: sbic PINB, STROBE rjmp stb_1 inc temp2 cpi temp2, 8 brne get_bit mov temp, temp1 ret send_result: ldi temp, 0 mov temp1, lfsr_result mov temp2, temp sbi DDRB, DATAIO send_bit: cbi PORTB, ACK nstb_0: sbis PINB, STROBE rjmp nstb_0 rol temp1 brcs its_1 cbi PORTB, DATAIO rjmp skip_over its_1: sbi PORTB, DATAIO skip_over: sbi PORTB, ACK sbic PINB, STROBE rjmp skip_over inc temp2 cpi temp2, 8 brne send_bit ret ;A routine to generate random number sequence using LFSR ;This is an 8-bit LFSR ;temp register is used as the 8-bit LFSR ;The Taps for an 8-bit LFSR are at: 1, 2, 3 and 7 for ;maximal length. lfsr: mov temp1, temp andi temp1, 0b10001110 ror temp1 mov temp2, temp1 andi temp2, 0b00000001 ror temp1 mov low_del, temp1 andi low_del, 0b00000001 ror temp1 mov high_del, temp1 andi high_del, 0b00000001 ror temp1 ror temp1 ror temp1 ror temp1 eor temp1, temp2 eor low_del, high_del eor temp1, low_del andi temp1, 0b00000001 breq shift sec rjmp no_shift shift: clc no_shift: rol temp ;temp has the shifted number ret calc_result: cpi lfsr_preset, 0 breq its_over cpi lfsr_shift, 0 breq its_over get_lfsr: mov temp, lfsr_preset rcall lfsr mov lfsr_preset, temp dec lfsr_shift cpi lfsr_shift, 0 brne get_lfsr its_over: mov lfsr_result, lfsr_preset ret