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

📄 des.asm

📁 microchip网站上找的pic18F458单片机的示例代码
💻 ASM
📖 第 1 页 / 共 4 页
字号:
	permute trw3, 5, 7
	permute trw2, 3, 6
	permute trw4, 2, 5
	permute trw1, 2, 4
	permute trw3, 2, 3
	permute trw2, 5, 2
	permute trw1, 4, 1
	permute trw4, 7, 0

	return

;********************************************************
;*  This routine performs the initial permutation of    *
;*  the 64-bit vector for DES. The permutation is       *
;*  defined by the matrix IP below. Both the initial    *
;*  and the final vectors are stored in 8 bytes. Each   *
;*  of these 8 bytes represents a row of the matrix IP. *
;*  All 8 bytes are generated in a similar fashion      *
;*  after shifting the input vector by an appropriate   *
;*  amount.                                             *
;*                                                      *
;*                      Matrix IP                       *
;*                                                      *
;*              58 50 42 34 26 18 10  2                 *
;*              60 52 44 36 28 20 12  4                 *
;*              62 54 46 38 30 22 14  6                 *
;*              64 56 48 40 32 24 16  8                 *
;*              57 49 41 33 25 17  9  1                 *
;*              59 51 43 35 27 19 11  3                 *
;*              61 53 45 37 29 21 13  5                 *
;*              63 55 47 39 31 23 15  7                 *
;*                                                      *
;********************************************************

Initial_Permutation

	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
	clrf    indf0                   ; Clear the destination block D6
	clrf    indf0                   ; Clear the destination block D7
	movfp   blkPtr, wreg            ; Reload destination address
	movwf   fsr0
	bsf     _fs1                    ; Turn off FSR0 auto increment
		
	permute w8, 6, 7                ; The FSR now points to D0
	permute w7, 6, 6
	permute w6, 6, 5
	permute w5, 6, 4
	permute w4, 6, 3
	permute w3, 6, 2
	permute w2, 6, 1
	permute w1, 6, 0

	incf    fsr0, f                 ; The FSR now points to D1
	permute w8, 4, 7
	permute w7, 4, 6
	permute w6, 4, 5
	permute w5, 4, 4
	permute w4, 4, 3
	permute w3, 4, 2
	permute w2, 4, 1
	permute w1, 4, 0

	incf    fsr0, f                 ; The FSR now points to D2
	permute w8, 2, 7
	permute w7, 2, 6
	permute w6, 2, 5
	permute w5, 2, 4
	permute w4, 2, 3
	permute w3, 2, 2
	permute w2, 2, 1
	permute w1, 2, 0

	incf    fsr0, f                 ; The FSR now points to D3
	permute w8, 0, 7
	permute w7, 0, 6
	permute w6, 0, 5
	permute w5, 0, 4
	permute w4, 0, 3
	permute w3, 0, 2
	permute w2, 0, 1
	permute w1, 0, 0

	incf    fsr0, f                 ; The FSR now points to D4
	permute w8, 7, 7
	permute w7, 7, 6
	permute w6, 7, 5
	permute w5, 7, 4
	permute w4, 7, 3
	permute w3, 7, 2
	permute w2, 7, 1
	permute w1, 7, 0

	incf    fsr0, f                 ; The FSR now points to D5
	permute w8, 5, 7
	permute w7, 5, 6
	permute w6, 5, 5
	permute w5, 5, 4
	permute w4, 5, 3
	permute w3, 5, 2
	permute w2, 5, 1
	permute w1, 5, 0

	incf    fsr0, f                 ; The FSR now points to D6
	permute w8, 3, 7
	permute w7, 3, 6
	permute w6, 3, 5
	permute w5, 3, 4
	permute w4, 3, 3
	permute w3, 3, 2
	permute w2, 3, 1
	permute w1, 3, 0

	incf    fsr0, f                 ; The FSR now points to D7
	permute w8, 1, 7
	permute w7, 1, 6
	permute w6, 1, 5
	permute w5, 1, 4
	permute w4, 1, 3
	permute w3, 1, 2
	permute w2, 1, 1
	permute w1, 1, 0

; Restore the scrambled data in darray(i) to w(i).

	MOV32   darray, w1
	MOV32   darray+4, w5

	return

;********************************************************
;* This routine performs the inverse permutation of     *
;* the 64-bit vector for DES. The inverse permutation   *
;* is performed using the matrix IP(-1) below. Both the *
;* output vectors are stored in 8 bytes. These bytes    *
;* are computed in a similar fashion.                   *
;*                                                      *
;*                      Matrix IP(-1)                   *
;*                                                      *
;*              40  8 48 16 56 24 64 32                 *
;*              39  7 47 15 55 23 63 31                 *
;*              38  6 46 14 54 22 62 30                 *
;*              37  5 45 13 53 21 61 29                 *
;*              36  4 44 12 52 20 60 28                 *
;*              35  3 43 11 51 19 59 27                 *
;*              34  2 42 10 50 18 58 26                 *
;*              33  1 41  9 49 17 57 25                 *
;*                                                      *
;********************************************************
Inverse_Permutation
	movlw   darray
	movwf   blkPtr
	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
	clrf    indf0                   ; Clear the destination block D6
	clrf    indf0                   ; Clear the destination block D7
	movfp   blkPtr, wreg            ; Reload destination address
	movwf   fsr0
	bsf     _fs1                    ; Turn off FSR0 auto increment
		
	permute w5, 0, 7                ; The FSR now points to D0
	permute w1, 0, 6
	permute w6, 0, 5
	permute w2, 0, 4
	permute w7, 0, 3
	permute w3, 0, 2
	permute w8, 0, 1
	permute w4, 0, 0

	incf    fsr0, f                 ; The FSR now points to D1
	permute w5, 1, 7
	permute w1, 1, 6
	permute w6, 1, 5
	permute w2, 1, 4
	permute w7, 1, 3
	permute w3, 1, 2
	permute w8, 1, 1
	permute w4, 1, 0

	incf    fsr0, f                 ; The FSR now points to D2
	permute w5, 2, 7
	permute w1, 2, 6
	permute w6, 2, 5
	permute w2, 2, 4
	permute w7, 2, 3
	permute w3, 2, 2
	permute w8, 2, 1
	permute w4, 2, 0

	incf    fsr0, f                 ; The FSR now points to D3
	permute w5, 3, 7
	permute w1, 3, 6
	permute w6, 3, 5
	permute w2, 3, 4
	permute w7, 3, 3
	permute w3, 3, 2
	permute w8, 3, 1
	permute w4, 3, 0

	incf    fsr0, f                 ; The FSR now points to D4
	permute w5, 4, 7
	permute w1, 4, 6
	permute w6, 4, 5
	permute w2, 4, 4
	permute w7, 4, 3
	permute w3, 4, 2
	permute w8, 4, 1
	permute w4, 4, 0

	incf    fsr0, f                 ; The FSR now points to D5
	permute w5, 5, 7
	permute w1, 5, 6
	permute w6, 5, 5
	permute w2, 5, 4
	permute w7, 5, 3
	permute w3, 5, 2
	permute w8, 5, 1
	permute w4, 5, 0

	incf    fsr0, f                 ; The FSR now points to D6
	permute w5, 6, 7
	permute w1, 6, 6
	permute w6, 6, 5
	permute w2, 6, 4
	permute w7, 6, 3
	permute w3, 6, 2
	permute w8, 6, 1
	permute w4, 6, 0

	incf    fsr0, f                 ; The FSR now points to D7
	permute w5, 7, 7
	permute w1, 7, 6
	permute w6, 7, 5
	permute w2, 7, 4
	permute w7, 7, 3
	permute w3, 7, 2
	permute w8, 7, 1
	permute w4, 7, 0

; Restore the scrambled data in darray(i) to w(i).

	MOV32   darray, w1
	MOV32   darray+4, w5

	return

;********************************************************
;*                                                      *
;*       PROGRAM key_Generation                         *
;*                                                      *
;*       Program to create a 64-bit DES key from 56     *
;* actual bits, by adding odd parity for each 7 bits.   *
;* The input consists of 8 8-bit bytes.  In each byte,  *
;* the active bits are 1-7 (counting from the left).    *
;* The program adds odd parity bits in location 8 (BIT0)*
;* and outputs the resulting words as the 64-bit key.   *
;*                                                      *
;* +---------------------------------------+            *
;* | D1 | D2 | D3 | D4 | D5 | D6 | D7 | OP |            *
;* +---------------------------------------+            *
;********************************************************

; Generate subkeys from key in RAM.

Key_Generation
	movfp   k1, wreg                ; Create byte with 8-bits
	call    _parity                 ; and parity bits
	movwf   k1

	movfp   k2, wreg                ; Create byte with 8-bits
	call    _parity                 ; and parity bits
	movwf   k2

	movfp   k3, wreg                ; Create byte with 8-bits
	call    _parity                 ; and parity bits
	movwf   k3

	movfp   k4, wreg                ; Create byte with 8-bits
	call    _parity                 ; and parity bits
	movwf   k4

	movfp   k5, wreg                ; Create byte with 8-bits
	call    _parity                 ; and parity bits
	movwf   k5

	movfp   k6, wreg                ; Create byte with 8-bits
	call    _parity                 ; and parity bits
	movwf   k6

	movfp   k7, wreg                ; Create byte with 8-bits
	call    _parity                 ; and parity bits
	movwf   k7

	movfp   k8, wreg                ; Create byte with 8-bits
	call    _parity                 ; and parity bits
	movwf   k8

;********************************************************
;*                                                      *
;*        ROUTINE KeyGen                                *
;*                                                      *
;*        Program to generate the subkeys for the DES   *
;* algorithm from the supplied key.  From the key with  *
;* 56 active bits, 16 subkeys are generated for the 16  *
;* stages of encryption.  Each subkey has 48 bits       *
;* organized in eight bytes with 7 active bits each.    *
;* The 128 resulting bytes are stored in the memory.    *
;*                                                      *
;********************************************************

	call    _prmch1                 ; Permute K1-K8 and put the result back
	nop                             ; DEBUG
	call    _shift                 ; Shift K1K2K3K4 and K5K6K7K8 once
	movlw   subkey1
	movpf	wreg, blkPtr		; Prepare for indirect addressing
	call    _prmch2                 ; Generate subkey1
	nop

	call    _shift                 ; Shift K1K2K3K4 and K5K6K7K8 once
	movlw   subkey2
	movpf   wreg, blkPtr		; Prepare for indirect addressing
	call    _prmch2                 ; Generate subkey2

	call    _shift                 ; Shift K1K2K3K4 and K5K6K7K8 once
	call    _shift                 ; Shift K1K2K3K4 and K5K6K7K8 once more
	movlw   subkey3
	movpf   wreg, blkPtr		; Prepare for indirect addressing
	call    _prmch2                 ; Generate subkey3

	call    _shift                 ; Shift K1K2K3K4 and K5K6K7K8 once
	call    _shift                 ; Shift K1K2K3K4 and K5K6K7K8 once more
	movlw   subkey4
	movpf   wreg, blkPtr		; Prepare for indirect addressing
	call    _prmch2                 ; Generate subkey4

	call    _shift                 ; Shift K1K2K3K4 and K5K6K7K8 once
	call    _shift                 ; Shift K1K2K3K4 and K5K6K7K8 once more
	movlw   subkey5
	movpf   wreg, blkPtr		; Prepare for indirect addressing
	call    _prmch2                 ; Generate subkey5

	call    _shift                 ; Shift K1K2K3K4 and K5K6K7K8 once
	call    _shift                 ; Shift K1K2K3K4 and K5K6K7K8 once more
	movlw   subkey6
	movpf   wreg, blkPtr		; Prepare for indirect addressing
	call    _prmch2                 ; Generate subkey6

	call    _shift                 ; Shift K1K2K3K4 and K5K6K7K8 once
	call    _shift                 ; Shift K1K2K3K4 and K5K6K7K8 once more
	movlw   subkey7
	movpf   wreg, blkPtr		; Prepare for indirect addressing
	call    _prmch2                 ; Generate subkey7

	call    _shift                 ; Shift K1K2K3K4 and K5K6K7K8 once
	call    _shift                 ; Shift K1K2K3K4 and K5K6K7K8 once more
	movlw   subkey8
	movpf   wreg, blkPtr		; Prepare for indirect addressing
	call    _prmch2                 ; Generate subkey8

	call    _shift                 ; Shift K1K2K3K4 and K5K6K7K8 once
	movlw   subkey9
	movpf   wreg, blkPtr		; Prepare for indirect addressing
	call    _prmch2                 ; Generate subkey9

	call    _shift                 ; Shift K1K2K3K4 and K5K6K7K8 once
	call    _shift                 ; Shift K1K2K3K4 and K5K6K7K8 once more
	movlw   subkey10
	movpf   wreg, blkPtr		; Prepare for indirect addressing
	call    _prmch2                 ; Generate subkey10

	call    _shift                 ; Shift K1K2K3K4 and K5K6K7K8 once
	call    _shift                 ; Shift K1K2K3K4 and K5K6K7K8 once more
	movlw   subkey11
	movpf   wreg, blkPtr		; Prepare for indirect addressing
	call    _prmch2                 ; Generate subkey11

	call    _shift                 ; Shift K1K2K3K4 and K5K6K7K8 once
	call    _shift                 ; Shift K1K2K3K4 and K5K6K7K8 once more
	movlw   subkey12
	movpf   wreg, blkPtr		; Prepare for indirect addressing
	call    _prmch2                 ; Generate subkey12

	call    _shift                 ; Shift K1K2K3K4 and K5K6K7K8 once
	call    _shift                 ; Shift K1K2K3K4 and K5K6K7K8 once more
	movlw   subkey13
	movpf   wreg, blkPtr		; Prepare for indirect addressing
	call    _prmch2                 ; Generate subkey13

	call    _shift                 ; Shift K1K2K3K4 and K5K6K7K8 once
	call    _shift                 ; Shift K1K2K3K4 and K5K6K7K8 once more
	movlw   subkey14
	movpf   wreg, blkPtr		; Prepare for indirect addressing
	call    _prmch2                 ; Generate subkey14

	call    _shift                 ; Shift K1K2K3K4 and K5K6K7K8 once
	call    _shift                 ; Shift K1K2K3K4 and K5K6K7K8 once more
	movlw   subkey15
	movpf   wreg, blkPtr		; Prepare for indirect addressing
	call    _prmch2                 ; Generate subkey15

	call    _shift                 ; Shift K1K2K3K4 and K5K6K7K8 once
	movlw   subkey16
	movpf   wreg, blkPtr		; Prepare for indirect addressing
	call    _prmch2                 ; Generate subkey16

	return

⌨️ 快捷键说明

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