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

📄 des.asm

📁 microchip网站上找的pic18F458单片机的示例代码
💻 ASM
📖 第 1 页 / 共 4 页
字号:
;********************************************************
;*                                                      *
;*       SUBROUTINE _parity (23 cycles)                 *
;*                                                      *
;*    Subroutine to convert a word of 7 active bits to  *
;* an 8-bit byte by adding parity bits after the first  *
;* 7 (1-7) bits.                                        *
;*                                                      *
;********************************************************

_parity
	movwf   kin
	movwf   temp

	rlncf   kin, f                  ; Add bits 1-7 and put
	xorwf   kin, w                  ;  the result back in bit 1
					;  of w.

	rlncf   kin, f                  ; Shift #2
	xorwf   kin, w

	rlncf   kin, f                  ; Shift #3
	xorwf   kin, w

	rlncf   kin, f                  ; Shift #4
	xorwf   kin, w

	rlncf   kin, f                  ; Shift #5
	xorwf   kin, w

	rlncf   kin, f                  ; Shift #6
	xorwf   kin, w
	andlw   MPARITY                 ; Isolate the parity bit

	btfss	_z			; If non-zero then we are
	goto	SetParity		;  already odd parity.

	movwf   bit
	clrf    _carry                  ; Clear carry bit
	rlcf    bit, f                  ; Move parity bit to lsb
	rlcf    bit, w
	iorwf   temp, w

	return

SetParity
	movfp	temp, wreg
	return

;********************************************************
;*                                                      *
;*        SUBROUTINE _shift                             *
;*                                                      *
;* Subroutine to shift the words in darray left by one  *
;* position circularly. Each byte contains 7 active     *
;* bits in the 7 leftmost locations                     *
;*                                                      *
;********************************************************

_shift
	bcf     _carry                  ; Reset the carry bit
	rlcf    darray+3                ; Start shifting C0
	rlcf    darray+2
	rlcf    darray+1
	rlcf    darray+0
	btfsc	_carry
	bsf	darray+3, bit4

	bcf     _carry                  ; Reset the carry bit
	rlcf    darray+7                ; Start shifting D0
	rlcf    darray+6
	rlcf    darray+5
	rlcf    darray+4
	btfsc   _carry
	bsf     darray+7, bit4

	return

;************************************************************************
;*                                                                      *
;*        SUBROUTINE PRMCH1                                             *
;*                                                                      *
;*  Subroutine to implement the permuted choice 1 of the DES for the    *
;* generation of the subkeys.  The 56 bits of the permuted key are      *
;* stored in the rightmost 7 bits of eight bytes, K1-K8. The            *
;* permutation is performed according to the matrix PC-1 given below.   *
;*                                                                      *
;*                  Matrix PC-1                                         *
;*                                                                      *
;*             57 49 41 33 25 17  9                                     *
;*              1 58 50 42 34 26 18                                     *
;*             10  2 59 51 43 35 27                                     *
;*             19 11  3 60 52 44 36                                     *
;*                                                                      *
;*             63 55 47 39 31 23 15                                     *
;*              7 62 54 46 38 30 22                                     *
;*             14  6 61 53 45 37 29                                     *
;*             21 13  5 28 20 12  4                                     *
;*                                                                      *
;*         Kx MSb                                                Kx LSb *
;* K1      D1<0>   D2<1>   D3<2>   D7<6>   D7<2>   D6<1>   D5<0>   p1   *
;* K2      D0<6>   D2<0>   D3<1>   D7<5>   D7<1>   D6<0>   D4<6>   p2   *
;* K3      D0<5>   D1<6>   D3<0>   D7<4>   D7<0>   D5<6>   D4<5>   p3   *
;* K4      D0<4>   D1<5>   D2<6>   D7<3>   D6<6>   D5<5>   D4<4>   p4   *
;* K5      D0<3>   D1<4>   D2<5>   D3<6>   D6<5>   D5<4>   D4<3>   p5   *
;* K6      D0<2>   D1<3>   D2<4>   D3<5>   D6<4>   D5<3>   D4<2>   p6   *
;* K7      D0<1>   D1<2>   D2<3>   D3<4>   D6<3>   D5<2>   D4<1>   p7   *
;* K8      D0<0>   D1<1>   D2<2>   D3<3>   D6<2>   D5<1>   D4<0>   p8   *
;*                                                                      *
;* blkPtr is the indirect address where the D0-D7 bytes will be         *
;* placed, This register must be loaded with the desired value          *
;* before calling (executing) the Kx to Dx translation.                 *
;*                                                                      *
;* The translation program requires:                                    *
;*     Program Memory   0x084 bytes    (not including return)           *
;*     Cycles           0x084 cycles   (not including call or return)   *
;*     Data Memory      1 byte         (register for ptr to Dx block)   *
;*                      8 bytes        (Key bytes)                      *
;*                      (8 * N) bytes  (# Dx bytes)                     *
;************************************************************************
;* These are the constants (K) and the 1st and 2nd permutations         *
;* (D and D'). The K values can be used to verify the permutations      *
;* D and D'.                                                            *
;*                                                                      *
;*                       D      D'                      D     D'        *
;*      K1 = 1111 111x  55h    AAh     K1 = 0000 000x  AAh   55h        *
;*      K2 = 0000 000x  55h    AAh     K2 = 1111 111x  AAh   55h        *
;*      K3 = 1111 111x  55h    AAh     K3 = 0000 000x  AAh   55h        *
;*      K4 = 0000 000x  50h    A0h     K4 = 1111 111x  A0h   50h        *
;*      K5 = 1111 111x  55h    AAh     K5 = 0000 000x  AAh   55h        *
;*      K6 = 0000 000x  55h    AAh     K6 = 1111 111x  AAh   55h        *
;*      K7 = 1111 111x  55h    AAh     K7 = 0000 000x  AAh   55h        *
;*      K8 = 0000 000x  50h    A0h     K8 = 1111 111x  A0h   50h        *
;*                                                                      *
;*                                                                      *
;*                       D      D'                      D     D'        *
;*      K1 = 1010 101x  55h    ABh     K1 = 0101 010x  AAh   54h        *
;*      K2 = 0101 010x  AAh    54h     K2 = 1010 101x  55h   ABh        *
;*      K3 = 1010 101x  55h    ABh     K3 = 0101 010x  AAh   54h        *
;*      K4 = 0101 010x  A0h    40h     K4 = 1010 101x  50h   B0h        *
;*      K5 = 1010 101x  55h    ABh     K5 = 0101 010x  AAh   54h        *
;*      K6 = 0101 010x  AAh    54h     K6 = 1010 101x  55h   ABh        *
;*      K7 = 1010 101x  55h    ABh     K7 = 0101 010x  AAh   54h        *
;*      K8 = 0101 010x  A0h    40h     K8 = 1010 101x  50h   B0h        *
;************************************************************************

_prmch1
	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 k8, 7, 7                ; The FSR now points to D0
	permute k7, 7, 6
	permute k6, 7, 5
	permute k5, 7, 4
	permute k4, 7, 3
	permute k3, 7, 2
	permute k2, 7, 1
	permute k1, 7, 0

	incf    fsr0, f                 ; The FSR now points to D1
	permute k8, 6, 7
	permute k7, 6, 6
	permute k6, 6, 5
	permute k5, 6, 4
	permute k4, 6, 3
	permute k3, 6, 2
	permute k2, 6, 1
	permute k1, 6, 0

	incf    fsr0, f                 ; The FSR now points to D2
	permute k8, 5, 7
	permute k7, 5, 6
	permute k6, 5, 5
	permute k5, 5, 4
	permute k4, 5, 3
	permute k3, 5, 2
	permute k2, 5, 1
	permute k1, 5, 0

	incf    fsr0, f                 ; The FSR now points to D3
	permute k8, 4, 7
	permute k7, 4, 6
	permute k6, 4, 5
	permute k5, 4, 4

	incf    fsr0, f                 ; The FSR now points to D4
	permute k8, 1, 7
	permute k7, 1, 6
	permute k6, 1, 5
	permute k5, 1, 4
	permute k4, 1, 3
	permute k3, 1, 2
	permute k2, 1, 1
	permute k1, 1, 0

	incf    fsr0, f                 ; The FSR now points to D5
	permute k8, 2, 7
	permute k7, 2, 6
	permute k6, 2, 5
	permute k5, 2, 4
	permute k4, 2, 3
	permute k3, 2, 2
	permute k2, 2, 1
	permute k1, 2, 0

	incf    fsr0, f                 ; The FSR now points to D6
	permute k8, 3, 7
	permute k7, 3, 6
	permute k6, 3, 5
	permute k5, 3, 4
	permute k4, 3, 3
	permute k3, 3, 2
	permute k2, 3, 1
	permute k1, 3, 0

	incf    fsr0, f                 ; The FSR now points to D7
	permute k4, 4, 7
	permute k3, 4, 6
	permute k2, 4, 5
	permute k1, 4, 4

	return

;************************************************************************
;*                                                                      *
;*        SUBROUTINE _prmch2                                            *
;*                                                                      *
;*        Subroutine to implement the permuted choice 2 of the DES for  *
;* the generation of the subkeys.  The 48 bits of the subkey are stored *
;* in the leftmost 7 bits of each byte, TMP1-TMP4.  The permutation     *
;* is performed according to the matrix PC-2, given below. The result   *
;* is saved in the subkey array.                                        *
;*                                                                      *
;*                  Matrix PC-2                                         *
;*                                                                      *
;*              14 17 11 24  1  5                                       *
;*               3 28 15  6 21 10                                       *
;*              23 19 12  4 26  8                                       *
;*              16  7 27 20 13  2                                       *
;*              41 52 31 37 47 55                                       *
;*              30 40 51 45 33 48                                       *
;*              44 49 39 56 34 53                                       *
;*              46 42 50 36 29 32                                       *
;*                                                                      *
;* The result is stored directly in subkey(i) array with all bits being *
;* significant.                                                         *
;*                                                                      *
;*      darray[0]       01 02 03 04 05 06 07 08                         *
;*      darray[1]       09 10 11 12 13 14 15 16                         *
;*      darray[2]       17 18 19 20 21 22 23 24                         *
;*      darray[3]       25 26 27 28 xx xx xx xx                         *
;*      darray[4]       29 30 31 32 33 34 35 36                         *
;*      darray[5]       37 38 39 40 41 42 43 44                         *
;*      darray[6]       45 46 47 48 49 50 51 52                         *
;*      darray[7]       53 54 55 56 xx xx xx xx                         *
;*                                                                      *
;* Program Memory:      xx words                                        *
;* Data Memory:         yy bytes                                        *
;* Cycles:              zz cycles (include return, but not call)        *
;*                                                                      *
;************************************************************************

_prmch2
	movlw   0xcf                    ; Do not change fsr1 status
	andwf   alusta, f
	bsf     _fs0
	movfp   blkPtr, wreg            ; Load destination address
	movwf   fsr0
	clrf    indf0
	clrf    indf0
	clrf    indf0
	clrf    indf0
	clrf    indf0
	clrf	indf0
	movfp   blkPtr, wreg            ; Reload destination address
	movwf   fsr0
	bsf     _fs1                    ; Turn off FSR0 auto increment

	permute darray+1, 2, 7          ; 14
	permute darray+2, 7, 6          ; 17
	permute darray+1, 5, 5          ; 11
	permute darray+2, 0, 4          ; 24
	permute darray+0, 7, 3          ; 1
	permute darray+0, 3, 2          ; 5
	permute darray+0, 5, 1          ; 3
	permute darray+3, 4, 0          ; 28

	incf    fsr0, f
	permute darray+1, 1, 7          ; 15
	permute darray+0, 2, 6          ; 6
	permute darray+2, 3, 5          ; 21
	permute darray+1, 6, 4          ; 10
	permute darray+2, 1, 3          ; 23
	permute darray+2, 5, 2          ; 19
	permute darray+1, 4, 1          ; 12
	permute darray+0, 4, 0          ; 4

	incf    fsr0, f
	permute darray+3, 6, 7          ; 26
	permute darray+0, 0, 6          ; 8
	permute darray+1, 0, 5          ; 16
	permute darray+0, 1, 4          ; 7
	permute darray+3, 5, 3          ; 27
	permute darray+2, 4, 2          ; 20
	permute darray+1, 3, 1          ; 13
	permute darray+0, 6, 0          ; 2

	incf    fsr0, f
	permute darray+5, 3, 7          ; 41
	permute darray+6, 0, 6          ; 52
	permute darray+4, 5, 5          ; 31
	permute darray+5, 7, 4          ; 37
	permute darray+6, 5, 3          ; 47
	permute darray+7, 5, 2          ; 55
	permute darray+4, 6, 1          ; 30
	permute darray+5, 4, 0          ; 40

	incf    fsr0, f
	permute darray+6, 1, 7          ; 51
	permute darray+6, 7, 6          ; 45
	permute darray+4, 3, 5          ; 33
	permute darray+6, 4, 4          ; 48
	permute darray+5, 0, 3          ; 44
	permute darray+6, 3, 2          ; 49
	permute darray+5, 5, 1          ; 39
	permute darray+7, 4, 0          ; 56

	incf    fsr0, f
	permute darray+4, 2, 7          ; 34
	permute darray+7, 7, 6          ; 53
	permute darray+6, 6, 5          ; 46
	permute darray+5, 2, 4          ; 42
	permute darray+6, 2, 3          ; 50
	permute darray+4, 0, 2          ; 36
	permute darray+4, 7, 1          ; 29
	permute darray+4, 4, 0          ; 32

	return

;********************************************************
;* Table constants used in the DES program              *
;*                                                      *
;* S-Matrices to be used in the computation of the      *
;* function F(Ri,Ki+1).                                 *
;********************************************************

Offset1
	dw      14,  4, 13,  1,  2, 15, 11,  8,  3, 10,  6, 12,  5,  9,  0,  7
	dw       0, 15,  7,  4, 14,  2, 13,  1, 10,  6, 12, 11,  9,  5,  3,  8
	dw       4,  1, 14,  8, 13,  6,  2, 11, 15, 12,  9,  7,  3, 10,  5,  0
	dw      15, 12,  8,  2,  4,  9,  1,  7,  5, 11,  3, 14, 10,  0,  6, 13
	
Offset2
	dw      15,  1,  8, 14,  6, 11,  3,  4,  9,  7,  2, 13, 12,  0,  5, 10
	dw       3, 13,  4,  7, 15,  2,  8, 14, 12,  0,  1, 10,  6,  9, 11,  5
	dw       0, 14,  7, 11, 10,  4, 13,  1,  5,  8, 12,  6,  9,  3,  2, 15
	dw      13,  8, 10,  1,  3, 15,  4,  2, 11,  6,  7, 12,  0,  5, 14,  9

Offset3
	dw      10,  0,  9, 14,  6,  3, 15,  5,  1, 13, 12,  7, 11,  4,  2,  8
	dw      13,  7,  0,  9,  3,  4,  6, 10,  2,  8,  5, 14, 12, 11, 15,  1
	dw      13,  6,  4,  9,  8, 15,  3,  0, 11,  1,  2, 12,  5, 10, 14,  7
	dw       1, 10, 13,  0,  6,  9,  8,  7,  4, 15, 14,  3, 11,  5,  2, 12

Offset4
	dw       7, 13, 14,  3,  0,  6,  9, 10,  1,  2,  8,  5, 11, 12,  4, 15
	dw      13,  8, 11,  5,  6, 15,  0,  3,  4,  7,  2, 12,  1, 10, 14,  9
	dw      10,  6,  9,  0, 12, 11,  7, 13, 15,  1,  3, 14,  5,  2,  8,  4
	dw       3, 15,  0,  6, 10,  1, 13,  8,  9,  4,  5, 11, 12,  7,  2, 14

Offset5
	dw       2, 12,  4,  1,  7, 10, 11,  6,  8,  5,  3, 15, 13,  0, 14,  9
	dw      14, 11,  2, 12,  4,  7, 13,  1,  5,  0, 15, 10,  3,  9,  8,  6
	dw       4,  2,  1, 11, 10, 13,  7,  8, 15,  9, 12,  5,  6,  3,  0, 14
	dw      11,  8, 12,  7,  1, 14,  2, 13,  6, 15,  0,  9, 10,  4,  5,  3

Offset6
	dw      12,  1, 10, 15,  9,  2,  6,  8,  0, 13,  3,  4, 14,  7,  5, 11
	dw      10, 15,  4,  2,  7, 12,  9,  5,  6,  1, 13, 14,  0, 11,  3,  8
	dw       9, 14, 15,  5,  2,  8, 12,  3,  7,  0,  4, 10,  1, 13, 11,  6
	dw       4,  3,  2, 12,  9,  5, 15, 10, 11, 14,  1,  7,  6,  0,  8, 13

Offset7
	dw       4, 11,  2, 14, 15,  0,  8, 13,  3, 12,  9,  7,  5, 10,  6,  1
	dw      13,  0, 11,  7,  4,  9,  1, 10, 14,  3,  5, 12,  2, 15,  8,  6
	dw       1,  4, 11, 13, 12,  3,  7, 14, 10, 15,  6,  8,  0,  5,  9,  2
	dw       6, 11, 13,  8,  1,  4, 10,  7,  9,  5,  0, 15, 14,  2,  3, 12

Offset8
	dw      13,  2,  8,  4,  6, 15, 11,  1, 10,  9,  3, 14,  5,  0, 12,  7
	dw       1, 15, 13,  8, 10,  3,  7,  4, 12,  5,  6, 11,  0, 14,  9,  2
	dw       7, 11,  4,  1,  9, 12, 14,  2,  0,  6, 10, 13, 15,  3,  5,  8
	dw       2,  1, 14,  7,  4, 10,  8, 13, 15, 12,  9,  0,  3,  5,  6, 11

;********************************************************
;*		NIST provided vectors can be found in 			*
;*		NBS Special Publication 500-20 with the			*
;*		title "VALIDATING THE CORRECTNESS OF HARDWARE	*
;*		IMPLEMENTATIONS OF THE NBS DATA ENCRYPTION		*
;*		STANDARD".										*
;********************************************************

Cipher
;	data	0x0000, 0x0000, 0x0000, 0x0000		; NIST Vector #1
;	data	0x4d49, 0xdb15, 0x3291, 0x9c9f		; NIST Vector #2
	data	0x3055, 0x3228, 0x6d6f, 0x295a		; NIST Vector #3 (pg 33)

My_Key
;	data	0x1031, 0x6e02, 0x8c8f, 0x3b4a		; NIST Vector #1 (82dcbafbdeab6602)
;	data	0x0101, 0x0101, 0x0101, 0x0101		; NIST Vector #2 (0000000001000000)
	data	0x1c58, 0x7f1c, 0x1392, 0x4fef		; NIST Vector #3 (63fac0d034d9f793)
	END

⌨️ 快捷键说明

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