⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 des.asm

📁 microchip网站上找的pic18F458单片机的示例代码
💻 ASM
📖 第 1 页 / 共 4 页
字号:
 LIST    C=132, p=17c42, r=dec, length=50

;********************************************************
;* Project: Software implementation of DES algorithm.   *
;*                                                      *
;* Copyright (c) 1993 Microchip Technology              *
;*                                                      *
;* Revision history:                                    *
;*       09/07/93        original                       *
;********************************************************

#define _NOTDEF         FALSE

	include "17c42.reg"
	include "17c42.mac"

; Global variables

blkPtr          equ     0x20
iteration       equ     blkPtr+1
mode            equ     iteration+1     ; Encode/Decode flag

w1              equ     mode+1          ; plaintext/ciphertext
w2              equ     w1+1            ; plaintext/ciphertext
w3              equ     w2+1            ; plaintext/ciphertext
w4              equ     w3+1            ; plaintext/ciphertext
w5              equ     w4+1            ; plaintext/ciphertext
w6              equ     w5+1            ; plaintext/ciphertext
w7              equ     w6+1            ; plaintext/ciphertext
w8              equ     w7+1            ; plaintext/ciphertext

plainText       equ     w1

rw1             equ     w8+1            ; Right half
rw2             equ     rw1+1           ; Right half
rw3             equ     rw2+1           ; Right half
rw4             equ     rw3+1           ; Right half
lw1             equ     rw4+1           ; Left half
lw2             equ     lw1+1           ; Left half
lw3             equ     lw2+1           ; Left half
lw4             equ     lw3+1           ; Left half

copy_rw1        equ     lw4+1
copy_rw2        equ     copy_rw1+1
copy_rw3        equ     copy_rw2+1
copy_rw4        equ     copy_rw3+1

cipherText      equ     copy_rw4+1      ; 64-bit ciphertext
subkey1         equ     cipherText+64/8 ; subkey #1
subkey2         equ     subkey1+48/8    ; subkey #2
subkey3         equ     subkey2+48/8    ; subkey #3
subkey4         equ     subkey3+48/8    ; subkey #4
subkey5         equ     subkey4+48/8    ; subkey #5
subkey6         equ     subkey5+48/8    ; subkey #6
subkey7         equ     subkey6+48/8    ; subkey #7
subkey8         equ     subkey7+48/8    ; subkey #8
subkey9         equ     subkey8+48/8    ; subkey #9
subkey10        equ     subkey9+48/8    ; subkey #10
subkey11        equ     subkey10+48/8   ; subkey #11
subkey12        equ     subkey11+48/8   ; subkey #12
subkey13        equ     subkey12+48/8   ; subkey #13
subkey14        equ     subkey13+48/8   ; subkey #14
subkey15        equ     subkey14+48/8   ; subkey #15
subkey16        equ     subkey15+48/8   ; subkey #16

darray          equ     subkey16+48/8   ; D array (temp locations)
trw1            equ     darray
trw2            equ     darray+1
trw3            equ     darray+2
trw4            equ     darray+3
trw5            equ     darray+4
trw6            equ     darray+5


CBLOCK  darray+64/8
	k1, k2, k3, k4, k5, k6, k7, k8
	kin, temp, bit, zero, row
 ENDC


;********************************************************
; Constant definitions                                  *
;********************************************************

MPARITY         equ     0x80

;********************************************************
; Macro definitions                                     *
;********************************************************

permute macro   k, test, bit
	  btfsc   k, test
	  bsf     indf0, bit
	endm

swap    macro   a, b
	 movfp   a, wreg
	 xorwf   b, 0
	 movwf   a

	 movfp   b, wreg
	 xorwf   a, 0
	 movwf   b

	 movfp   a, wreg
	 xorwf   b, 0
	 movwf   a
	endm

;********************************************************
;  DES code                                             *
;********************************************************

	org     _ResetVector
	goto    Start

	org     _Text
Start
	bsf     _glintd                 ; disable all interrupts
	AUTONO                          ; no auto increment or decrement
	movlw   0x20                    ; clear all memory locations [18,FF]
	movpf   wreg,fsr0

Memory_Loop
	clrf    indf0
	incfsz  fsr0
	goto    Memory_Loop

; Transfer the plaintext information from PM to DM. Read only 64-bit
; at a time.

	movlw   plainText               ; Move plain text to RAM
	movpf   wreg, fsr0
	movlw   0xf                     ; Setup indirect addressing
	andwf   alusta, f               ;  with post increment option
	bsf     _fs0
	movlw   high Cipher
	movpf   wreg, tblptrh
	movlw   low Cipher
	movpf   wreg, tblptrl           ; (tblptr) = Cipher
	tablrd  1, 1, wreg              ; (tblat) = Cipher --> junk value
					; (tblptr) = Cipher++
	tlrd    1, indf0
	tablrd  0, 1, indf0
	tlrd    1, indf0
	tablrd  0, 1, indf0
	tlrd    1, indf0
	tablrd  0, 1, indf0
	tlrd    1, indf0
	tablrd  0, 1, indf0

; Transfer the key from PM to DM (56 bits only).

	movlw   k1
	movpf   wreg, fsr0              ; Move key to RAM
	movlw   high My_Key
	movpf   wreg, tblptrh
	movlw   low My_Key
	movpf   wreg, tblptrl
	tablrd  1, 1, wreg              ; Junk value

	tlrd    1, indf0
	tablrd  0, 1, indf0
	tlrd    1, indf0
	tablrd  0, 1, indf0
	tlrd    1, indf0
	tablrd  0, 1, indf0
	tlrd    1, indf0
	tablrd  0, 1, indf0

	call    Key_Generation          ; Generate DES session keys

	nop

	call    des						; Encrypt

	nop

	swap    subkey1, subkey16
	swap    subkey1+1, subkey16+1
	swap    subkey1+2, subkey16+2
	swap    subkey1+3, subkey16+3
	swap    subkey1+4, subkey16+4
	swap    subkey1+5, subkey16+5

	swap    subkey2, subkey15
	swap    subkey2+1, subkey15+1
	swap    subkey2+2, subkey15+2
	swap    subkey2+3, subkey15+3
	swap    subkey2+4, subkey15+4
	swap    subkey2+5, subkey15+5
	
	swap    subkey3, subkey14
	swap    subkey3+1, subkey14+1
	swap    subkey3+2, subkey14+2
	swap    subkey3+3, subkey14+3
	swap    subkey3+4, subkey14+4
	swap    subkey3+5, subkey14+5

	swap    subkey4, subkey13
	swap    subkey4+1, subkey13+1
	swap    subkey4+2, subkey13+2
	swap    subkey4+3, subkey13+3
	swap    subkey4+4, subkey13+4
	swap    subkey4+5, subkey13+5

	swap    subkey5, subkey12
	swap    subkey5+1, subkey12+1
	swap    subkey5+2, subkey12+2
	swap    subkey5+3, subkey12+3
	swap    subkey5+4, subkey12+4
	swap    subkey5+5, subkey12+5

	swap    subkey6, subkey11
	swap    subkey6+1, subkey11+1
	swap    subkey6+2, subkey11+2
	swap    subkey6+3, subkey11+3
	swap    subkey6+4, subkey11+4
	swap    subkey6+5, subkey11+5

	swap    subkey7, subkey10
	swap    subkey7+1, subkey10+1
	swap    subkey7+2, subkey10+2
	swap    subkey7+3, subkey10+3
	swap    subkey7+4, subkey10+4
	swap    subkey7+5, subkey10+5

	swap    subkey8, subkey9
	swap    subkey8+1, subkey9+1
	swap    subkey8+2, subkey9+2
	swap    subkey8+3, subkey9+3
	swap    subkey8+4, subkey9+4
	swap    subkey8+5, subkey9+5

	call	des						; Decrypt

Here
	nop
	goto    Here

;********************************************************
;*  Program DES                                         *
;*                                                      *
;* Program to implement both the encoding and decoding  *
;* portion of the data encryption standard              *
;*                                                      *
;* Memory location mode contains the encoding or        *
;* decoding flag. If mode=0 then encrypt                *
;*                If mode=1 then decrypt                *
;*                                                      *
;* The text to be scrambled is to be provided in local  *
;* RAM locations starting at plainText (w1, w2, w3, w4  *
;* w5, w6, w7, w8).                                     *
;*                                                      *
;* On exit, w1 thru w8 contain the encrypted data       *
;********************************************************

des
	call    Initial_Permutation

	movlw   16                      ; Number of iterations
	movwf   iteration

; Store left and right data

	MOV32   w1, lw1                 ; Store left and right bytes
	MOV32   w5, rw1

;-- Point to subkey[i]

	movlw   subkey1
	movwf   fsr1

	movlw   0xf
	andwf   alusta, f
	bsf     _fs2                    ; fsr1 Auto-increment
;       movlw   subkey1
;       movwf   fsr1

;-- Check mode flag, if decryption then point to subkey + 136 (K16)

;	btfss   mode, 0                 ; Check mode
;	goto    Next_Loop

;	movlw   0xf
;	andwf   alusta, f               ; fsr1 Auto-decrement
;	movlw   subkey16+48/8
;	movwf   fsr1

; Actual encyphering loop

Next_Loop
	movfp   rw1, wreg               ; Save right word i
	movwf   copy_rw1
	movfp   rw2, wreg
	movwf   copy_rw2
	movfp   rw3, wreg
	movwf   copy_rw3
	movfp   rw4, wreg
	movwf   copy_rw4

	call    Function                ; Compute Ri+1=Li+f(Ri+Ki+1)
	
	movfp   copy_rw1, wreg          ; Set Li+1=Ri
	movwf   lw1
	movfp   copy_rw2, wreg
	movwf   lw2
	movfp   copy_rw3, wreg
	movwf   lw3
	movfp   copy_rw4, wreg
	movwf   lw4

	nop

	decfsz  iteration, f            ; Repeat 16 times
	goto    Next_Loop

	movfp   rw1, wreg
	movwf   w1
	movfp   rw2, wreg
	movwf   w2
	movfp   rw3, wreg
	movwf   w3
	movfp   rw4, wreg
	movwf   w4
	movfp   lw1, wreg
	movwf   w5
	movfp   lw2, wreg
	movwf   w6
	movfp   lw3, wreg
	movwf   w7
	movfp   lw4, wreg
	movwf   w8

	call    Inverse_Permutation
	nop                             ; DEBUG

	return

;********************************************************
;*    SUBROUTINE Function                               *
;*                                                      *
;*    Subroutine to scramble 32 bits into another 32    *
;* bits with the aid of the key.  Input words are       *
;* stored in RW1 thru RW4.  The output is also stored   *
;* there.                                               *
;********************************************************
;*                                                      *
;* Generate 48 bits out of the 32 using the matrix E    *
;* shown below. The input is read from rw1, rw2, rw3,   *
;* and rw4 and expanded into 48-bits and result stored  *
;* darray.                                              *
;*                                                      *
;*                 Matrix E                             *
;*                                                      *
;*            32  1  2  3  4  5                         *
;*             4  5  6  7  8  9                         *
;*             8  9 10 11 12 13                         *
;*            12 13 14 15 16 17                         *
;*            16 17 18 19 20 21                         *
;*            20 21 22 23 24 25                         *
;*            24 25 26 27 28 29                         *
;*            28 29 30 31 32  1                         *
;*                                                      *
;********************************************************
Function
	movlw   darray
	movpf   wreg, blkPtr            ; Prepare for indirect addressing
	movlw   0xcf                    ; Do not change fsr1 status
	andwf   alusta, f
	bsf     _fs0
	movfp   blkPtr, wreg            ; Load destination address
	movwf   fsr0
	clrf    indf0                   ; Clear the destination block D0
	clrf    indf0                   ; Clear the destination block D1
	clrf    indf0                   ; Clear the destination block D2
	clrf    indf0                   ; Clear the destination block D3
	clrf    indf0                   ; Clear the destination block D4
	clrf    indf0                   ; Clear the destination block D5
	movfp   blkPtr, wreg            ; Reload destination address
	movwf   fsr0
	bsf     _fs1                    ; Turn off FSR0 auto increment
								    
	permute rw4, 0, 7               ; The FSR now points to D0
	permute rw1, 7, 6
	permute rw1, 6, 5
	permute rw1, 5, 4
	permute rw1, 4, 3
	permute rw1, 3, 2
	permute rw1, 4, 1
	permute rw1, 3, 0
								    
	incf    fsr0, f                 ; The FSR now points to D1  
	permute rw1, 2, 7
	permute rw1, 1, 6
	permute rw1, 0, 5
	permute rw2, 7, 4
	permute rw1, 0, 3
	permute rw2, 7, 2
	permute rw2, 6, 1
	permute rw2, 5, 0
								    
	incf    fsr0, f                 ; The FSR now points to D2  
	permute rw2, 4, 7               ; 12
	permute rw2, 3, 6               ; 13
	permute rw2, 4, 5               ; 12
	permute rw2, 3, 4               ; 13
	permute rw2, 2, 3               ; 14
	permute rw2, 1, 2               ; 15
	permute rw2, 0, 1               ; 16
	permute rw3, 7, 0               ; 17
								    
	incf    fsr0, f                 ; The FSR now points to D3  
	permute rw2, 0, 7
	permute rw3, 7, 6
	permute rw3, 6, 5
	permute rw3, 5, 4
	permute rw3, 4, 3
	permute rw3, 3, 2
	permute rw3, 4, 1
	permute rw3, 3, 0
								    
	incf    fsr0, f                 ; The FSR now points to D4  
	permute rw3, 2, 7
	permute rw3, 1, 6
	permute rw3, 0, 5
	permute rw4, 7, 4

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -