📄 test.asm
字号:
;
; test program for "relocatable" ENCRYPT routine
; using an HCS410 in IFF-W mode
;
radix hex
include <evkit.inc>
include "hcs410.inc" ; encryption lib module exports
include "fastdec.inc" ; decryption lib module exports
;---------------------------------------------------------------------------
;
;
BANK1 UDATA ; make sure gets allocated in BANK0
KEY0 RES 1 ; crypto key stored lsb first
KEY1 RES 1
KEY2 RES 1
KEY3 RES 1
KEY4 RES 1
KEY5 RES 1
KEY6 RES 1
KEY7 RES 1 ; crypto key msb
BANK0 UDATA
HOP0 RES 1 ; hopping code 32 bit register
HOP1 RES 1
HOP2 RES 1
HOP3 RES 1
; make this variables accessible to enc/decrypt modules
GLOBAL KEY0, KEY1, KEY2, KEY3, KEY4, KEY5, KEY6, KEY7
GLOBAL HOP0, HOP1, HOP2, HOP3
SN RES 4 ; temp storage for Serial Number
CHG RES 4 ; challenge
STARTUP CODE
goto START
CODE ; reset vector
START
banksel TRISA
movlw DEFPA
movwf TRISA
movlw DEFPB
movwf TRISB
movlw DEFPC
movwf TRISC
movlw DEFPD
movwf TRISD
movlw DEFPE
movwf TRISE
banksel ADCON1
movlw 0x0e ; AN0 only analog input
movwf ADCON1
banksel OPTION_REG
movlw b'10001011' ; prescaler to WDT 1:8
movwf OPTION_REG
; init CRYPTO KEY
banksel KEY0
movlw 0xEF ; lsb
movwf KEY0
movlw 0xCD
movwf KEY1
movlw 0xAB
movwf KEY2
movlw 0x89
movwf KEY3
movlw 0x67
movwf KEY4
movlw 0x45
movwf KEY5
movlw 0x23
movwf KEY6
movlw 0x01
movwf KEY7 ; msb
; load init value for challenge
banksel CHG
movlw 0x67
movwf CHG+0
movlw 0x45
movwf CHG+1
movlw 0x23
movwf CHG+2
movlw 0x01
movwf CHG+3
MainLoop
; 1. read Serial Number locations
movlw -1 ; SN 16 Lsb
call ReadUser
movf HOP0,W ; save value in local variable
movwf SN+0
movf HOP1,W
movwf SN+1
movlw -2 ; SN 16 Msb
call ReadUser
movf HOP0,W ; save value in local variable
movwf SN+2
movf HOP1,W
movwf SN+3
; 2. write to all EEPROM USER locations (4321)
movlw 21
movwf HOP0
movlw 43
movwf HOP1
; write to EEPROM location 0
movlw 0
call WriteUser
; write to EEPROM location 1
movlw 1
call WriteUser
; write to an EEPROM location 2
movlw 2
call WriteUser
; write to an EEPROM location 3
movlw 3
call WriteUser
; 3. read back values from USER EEPROM locations
; read EEPROM location 0
movlw 0
call ReadUser
; read EEPROM location 1
movlw 1
call ReadUser
; read EEPROM location 2
movlw 2
call ReadUser
; read EEPROM location 3
movlw 3
call ReadUser
; 4. generate a new challenge (and turn off LED)
; challenges should be randomized,
; Hint: use again Keeloq with a different Crypt Key
; to act as a random number generator
; for simplicity a sequential challenge generation is used here
bcf LED
incf CHG+0,F
btfsc STATUS,Z
incf CHG+1,F
; move challenge into data buffer
movf CHG+0,W
movwf HOP0
movf CHG+1,W
movwf HOP1
movf CHG+2,W
movwf HOP2
movf CHG+3,W
movwf HOP3
; 5. call HCS410 encrypt library function
call Encrypt1
; 6. verify the response using Keeloq Decrypt
movlw KEY0 ; enter with W pointing to CRYPTO KEY
call Decrypt
movf HOP0,W ; compare decrypted response with challenge
xorwf CHG+0,W
btfss STATUS,Z
goto AError
movf HOP1,W ; compare decrypted response with challenge
xorwf CHG+1,W
btfss STATUS,Z
goto AError
movf HOP2,W ; compare decrypted response with challenge
xorwf CHG+2,W
btfss STATUS,Z
goto AError
movf HOP3,W ; compare decrypted response with challenge
xorwf CHG+3,W
btfss STATUS,Z
goto AError
; if successful turn LED on
bsf LED
; 7. loop
goto MainLoop
AError
goto 0 ; reset in case of error
END
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -