#include <p16f877.inc> ; global DECRYPT global KEY0,KEY1,KEY2,KEY3,KEY4,KEY5,KEY6,KEY7 global CSR0,CSR1,CSR2,CSR3,CSR4,CSR5,CSR6,CSR7,CSR8 ; #define HOP1 CSR0 ; ﹚竡 HOP1~4 單 CSR1~4 #define HOP2 CSR1 #define HOP3 CSR2 #define HOP4 CSR3
#define MAX_CODE_LENGTH .72 #define CODE_LENGTH .66 ; UDATA_SHR ; Difine CSR0:CSR7 on Share memory CSR0 res 1 ; Hopping Code 鋇Μ纗既竟 (LSB) CSR1 res 1 CSR2 res 1 CSR3 res 1 CSR4 res 1 CSR5 res 1 CSR6 res 1 CSR7 res 1 CSR8 res 1 ; Hopping Code 鋇Μ纗既竟 (MSB) ; Encrypt_Data UDATA 0x20 KEY0 res 1 ; 64-bit 稈盞じン戈 (LSB) KEY1 res 1 KEY2 res 1 KEY3 res 1 KEY4 res 1 KEY5 res 1 KEY6 res 1 KEY7 res 1 ; UDATA CNT0 res 1 CNT1 res 1 HOP_LENGTH res 1 MASK res 1 KEELOQ_Temp res 1 ; ; KEELOQ_CODE CODE ;
;***************************************************** ;**** Rotate CSRs register right 1 bit ;***************************************************** ROT_CSRs RRF CSR8,F RRF CSR7,F RRF CSR6,F RRF CSR5,F RRF CSR4,F RRF CSR3,F RRF CSR2,F RRF CSR1,F RRF CSR0,F return ; ; ; ; ;**************************************************************** ; ; FUNCTION : DECRYPT () ; ; DESCRIPTION : DECRYPTS 32 BIT [HOP1:HOP4] USING [CSR0:CSR7] ; ;**************************************************************** DECRYPT BANKSEL CNT1 MOVLW (.11+1) ; OUTER LOOP 11+1 TIMES MOVWF CNT1 ; OUTER LOOP 11+1 TIMES
DECRYPT_OUTER MOVLW .48 ; INNER LOOP 48 TIMES MOVWF CNT0 ; INNER LOOP 48 TIMES
DECRYPT_INNER CLRWDT ; RESET WATCHDOG TIMER MOVFW CNT1 ; LAST 48 LOOPS RESTORE THE KEY XORLW .1 ; LAST 48 LOOPS RESTORE THE KEY btfsc STATUS,Z ; LAST 48 LOOPS RESTORE THE KEY GOTO ROTATE_KEY ; LAST 48 LOOPS RESTORE THE KEY
; THE LOOKUP TABLE IS COMPRESSED INTO IN 4 BYTES TO SAVE SPACE ; USE THE 3 LOW INDEX BITS TO MAKE UP AN 8-BIT BIT MASK ; USE THE 2 HIGH INDEX BITS TO LOOK UP THE VALUE IN THE TABLE ; USE THE BIT MASK TO ISOLATE THE CORRECT BIT IN THE BYTE ; PART OF THE REASON FOR THIS SCHEME IS BECAUSE NORMAL TABLE ; LOOKUP REQUIRES AN ADDITIONAL STACK LEVEL bcf STATUS,C ; CLEAR CARRY (FOR THE LEFT SHIFT) MOVLW .1 ; INITIALISE MASK = 1 BTFSC HOP3,3 ; SHIFT MASK 4X IF BIT 2 SET MOVLW B00010000 ; SHIFT MASK 4X IF BIT 2 SET MOVWF MASK ; INITIALISE MASK = 1
BTFSS HOP2,0 ; SHIFT MASK ANOTHER 2X IF BIT 1 SET GOTO $+3 RLF MASK,F RLF MASK,F
BTFSC HOP1,0 ; SHIFT MASK ANOTHER 1X IF BIT 0 SET RLF MASK,F
; MASK HAS NOW BEEN SHIFTED 0-7 TIMES ACCORDING TO BITS 2:1:0
MOVlw HIGH TABLE MOVwf PCLATH
MOVlw LOW TABLE MOVwf KEELOQ_Temp MOVLW 0 ; TABLE INDEX = 0 BTFSC HOP4,1 IORLW .2 ; IF BIT 3 SET ADD 2 TO THE TABLE INDEX BTFSC HOP4,6 IORLW .4 ; IF BIT 4 SET ADD 4 TO THE TABLE INDEX
addwf KEELOQ_Temp,W btfsc STATUS,C incf PCLATH,F MOVwf PCL ; ADD THE INDEX TO THE PROGRAM COUNTER ; [ MUST BE IN LOWER HALF OF PAGE ] TABLE MOVLW 0x2E ; BITS 4:3 WERE 00 GOTO TABLE_END ; END OF LOOKUP
MOVLW 0x74 ; BITS 4:3 WERE 01 GOTO TABLE_END ; END OF LOOKUP
MOVLW 0x5C ; BITS 4:3 WERE 10 GOTO TABLE_END ; END OF LOOKUP
MOVLW 0x3A ; BITS 4:3 WERE 11 TABLE_END ANDWF MASK,1 ; ISOLATE THE CORRECT BIT MOVLW 0 ; COPY THE BIT TO BIT 7 btfss STATUS,Z ; COPY THE BIT TO BIT 7 MOVLW B10000000 ; COPY THE BIT TO BIT 7
XORWF HOP2,W ; ONLY INTERESTED IN BIT HOP2,7 XORWF HOP4,W ; ONLY INTERESTED IN BIT HOP4,7 XORWF KEY1,W ; ONLY INTERESTED IN BIT KEYREG1,7
MOVWF MASK ; STORE W TEMPORARILY (WE NEED BIT 7) RLF MASK,F ; LEFT ROTATE MASK TO GET BIT 7 INTO CARRY RLF HOP1,F ; SHIFT IN THE NEW BIT RLF HOP2,F RLF HOP3,F RLF HOP4,F
ROTATE_KEY bcf STATUS,C ; CLEAR CARRY BTFSC KEY7,7 ; SET CARRY IF LEFTMOST BIT SET bsf STATUS,C ; SET CARRY IF LEFTMOST BIT SET
RLF KEY0,F ; LEFT-ROTATE THE 64-BIT KEY RLF KEY1,F RLF KEY2,F RLF KEY3,F RLF KEY4,F RLF KEY5,F RLF KEY6,F RLF KEY7,F
DECFSZ CNT0,F ; INNER LOOP 48 TIMES GOTO DECRYPT_INNER ; INNER LOOP 48 TIMES
DECFSZ CNT1,F ; OUTER LOOP 12 TIMES (11+1 TO RESTORE KEY) GOTO DECRYPT_OUTER ; OUTER LOOP 12 TIMES (11+1 TO RESTORE KEY)
RETLW 0 ; RETURN |