📄 des.inc
字号:
movf left+2,w movwf right+2 movf left+3,w movwf right+3 movf newr+0,w movwf left+0 movf newr+1,w movwf left+1 movf newr+2,w movwf left+2 movf newr+3,w movwf left+3 return; On entry:; ciphertext in left:right; permuted key in keyl:keyr; On exit:; plaintext in left:right; call = 2 cycles; 4 calls to roundr @ 324 cycles = 1296 cycles; 12 calls to roundr2 @ 344 cycles = 4128 cycles; goto swaplr = 20 cycles; -----------; total = 5446 cyclesdesinvcore: call roundr ; round 1 call roundr2 ; round 2 call roundr2 ; round 3 call roundr2 ; round 4 call roundr2 ; round 5 call roundr2 ; round 6 call roundr2 ; round 7 call roundr ; round 8 call roundr2 ; round 9 call roundr2 ; round 10 call roundr2 ; round 11 call roundr2 ; round 12 call roundr2 ; round 13 call roundr2 ; round 14 call roundr ; round 15 call roundr ; round 16 goto swaplr;-----------------------------------------------------------------------------; key rotation subroutines;-----------------------------------------------------------------------------; The 16 rounds of the DES each use a different 48-bit subkey, which is; generated by independently rotating the two 28-bit halves of the 56-bit key,; then using Permuted Choice 2 to extract the 48 bit subkey.;; Since the PIC doesn't have enough RAM to store precomputed subkeys, the; rotations are done on the fly using these subroutines. The Permuted Choice; 2 function is done as part of the xorkey macro calls.; On entry and exit, the current subkey is in keyl:keyr.; keyrotl takes 18 cycles including callkeyrotl: bcf status,c rlf keyl+3 rlf keyl+2 rlf keyl+1 rlf keyl+0 btfsc status,c bsf k28 bcf status,c rlf keyr+3 rlf keyr+2 rlf keyr+1 rlf keyr+0 btfsc status,c bsf k56 return; keyrotr takes 20 cycles including callkeyrotr: bcf status,c rrf keyl+0 rrf keyl+1 rrf keyl+2 rrf keyl+3 btfsc keyl+3,3 bsf k01 bcf keyl+3,3 bcf status,c rrf keyr+0 rrf keyr+1 rrf keyr+2 rrf keyr+3 btfsc keyr+3,3 bsf k29 bcf keyr+3,3 return;-----------------------------------------------------------------------------; DES round macros;-----------------------------------------------------------------------------; The gethi and getlo macros implement six bit sections of the Expansion; Permutation.; 6 cyclesgethi macro rbyte,b20r,b20b,b10r,b10b swapf rbyte,w andlw 0x0f btfsc b20r,b20b iorlw 0x20 btfsc b10r,b10b iorlw 0x10 endm; 6 cyclesgetlo macro rbyte,b20r,b20b,b10r,b10b movf rbyte,w andlw 0x0f btfsc b20r,b20b iorlw 0x20 btfsc b10r,b10b iorlw 0x10 endm; The xorkey macro is used to exclusive-or the 6-bit result of the Expansion; Permuation in the W register (as generated by the gethi or getlo macros; above) with the appropriate 6 bits of the current subkey (as defined by; Permuted Choice 2). The arguments are the six subkey bits to be used.; 12 cyclesxorkey macro b20r,b20b,b10r,b10b,b08r,b08b,b04r,b04b,b02r,b02b,b01r,b01b btfsc b20r,b20b xorlw 0x20 btfsc b10r,b10b xorlw 0x10 btfsc b08r,b08b xorlw 0x08 btfsc b04r,b04b xorlw 0x04 btfsc b02r,b02b xorlw 0x02 btfsc b01r,b01b xorlw 0x01 endm; The corehi and corelo macros are used to perform the S-box lookup and; the P-box permutation. The only difference between the two macros is; which half of the sbox table value is used. The corehi macro must be; used for S-boxes 1, 3, 5, and 7, while the corelo macro must be used for; S-boxes 2, 4, 6, and 8.; The first argument to the macro is the entry point of the S-box table to; be used.; The second through fifth arguments to the macro are the bits that the; P-box permutes the S-box outputs into, in order from the most to the least; significant.; 15 cycles for sbox12; 16 cycles for otherscorehi macro sbox,b7r,b7b,b6r,b6b,b5r,b5b,b4r,b4b call sbox ; 6 cycles for sbox12, ; 7 cycles for others movwf temp btfsc temp,7 bsf b7r,b7b btfsc temp,6 bsf b6r,b6b btfsc temp,5 bsf b5r,b5b btfsc temp,4 bsf b4r,b4b endm; 15 cycles for sbox12; 16 cycles for otherscorelo macro sbox,b3r,b3b,b2r,b2b,b1r,b1b,b0r,b0b call sbox ; 6 cycles for sbox12, ; 7 cycles for others movwf temp btfsc temp,3 bsf b3r,b3b btfsc temp,2 bsf b2r,b2b btfsc temp,1 bsf b1r,b1b btfsc temp,0 bsf b0r,b0b endm;-----------------------------------------------------------------------------; one round of DES (with various key rotations);-----------------------------------------------------------------------------; roundr2 = 344 cycles; roundr = 324 cyclesroundr2: call round call keyrotr ; 20 cycles goto keyrotr ; 20 cyclesroundr: call round ; 302 cycles goto keyrotr ; 20 cycles; roundl2 = 338 cycles including call; roundl = 320 cycles including callroundl2: call keyrotl ; 18 cyclesroundl: call keyrotl ; 18 cycles; fall into round; xxx cycles including call; call = 2 cycles; clear newr = 4 cycles; 2 sboxes @ 33 cycles = 66 cycles; 6 sboxes @ 34 cycles = 204 cycles; xor = 8 cycles; swap = 16 cycles; return = 2 cycles; ----------; total = 302 cyclesround:; clear newr to make it easy to do the P-box permutation (by simply setting; bits) clrf newr+0 clrf newr+1 clrf newr+2 clrf newr+3 ; do s-box 1 gethi right+0,r32,r05 ; 6 cycles xorkey k14,k05,k17,k11,k24,k01 ; 12 cycles corehi sbox12,nr09,nr17,nr23,nr31 ; 15 cycles; do s-box 2 getlo right+0,r04,r09 ; 6 cycles xorkey k03,k10,k28,k15,k06,k21 ; 12 cycles corelo sbox12,nr13,nr28,nr02,nr18 ; 15 cycles; do s-box 3 gethi right+1,r08,r13 ; 6 cycles xorkey k23,k08,k19,k12,k04,k26 ; 12 cycles corehi sbox34,nr24,nr16,nr30,nr06 ; 16 cycles; do s-box 4 getlo right+1,r12,r17 ; 6 cycles xorkey k16,k02,k07,k27,k20,k13 ; 12 cycles corelo sbox34,nr26,nr20,nr10,nr01 ; 16 cycles; do s-box 5 gethi right+2,r16,r21 ; 6 cycles xorkey k41,k55,k52,k31,k37,k47 ; 12 cycles corehi sbox56,nr08,nr14,nr25,nr03 ; 16 cycles; do s-box 6 getlo right+2,r20,r25 ; 6 cycles xorkey k30,k48,k40,k51,k45,k33 ; 12 cycles corelo sbox56,nr04,nr29,nr11,nr19 ; 16 cycles; do s-box 7 gethi right+3,r24,r29 ; 6 cycles xorkey k44,k53,k49,k39,k56,k34 ; 12 cycles corehi sbox78,nr32,nr12,nr22,nr07 ; 16 cycles; do s-box 8 getlo right+3,r28,r01 ; 6 cycles xorkey k46,k32,k42,k50,k36,k29 ; 12 cycles corelo sbox78,nr05,nr27,nr15,nr21 ; 16 cycles; now exclusive-or the old left with the new right to complete the; computation movf left+0,w xorwf newr+0 movf left+1,w xorwf newr+1 movf left+2,w xorwf newr+2 movf left+3,w xorwf newr+3; now swap the stuff around movf right+0,w movwf left+0 movf right+1,w movwf left+1 movf right+2,w movwf left+2 movf right+3,w movwf left+3 movf newr+0,w movwf right+0 movf newr+1,w movwf right+1 movf newr+2,w movwf right+2 movf newr+3,w movwf right+3 return;-----------------------------------------------------------------------------; s-box table lookup functions;-----------------------------------------------------------------------------; Because of the way they are accessed, all of the S-box data tables must; reside within the same page of ROM. Since the S-box data consumes; exactly 256 words of memory, the actual entry points to the table lookup; functions must be stored in a different page. As usual on the midrange; PIC processors, it is important that PCLATH be set to the correct page; prior to calling the table lookup functions.; 6 cycles including callsbox12: movwf pcl; 7 cycles including callsbox34: iorlw 040h movwf pcl; 7 cycles including callsbox56: iorlw 080h movwf pcl; 7 cycles including callsbox78: iorlw 0c0h movwf pcl;-----------------------------------------------------------------------------; s-box data tables;----------------------------------------------------------------------------- org ($+0xff)&~0xff ; force page alignmentsboxpage equ ($>>8); Since the output width of each S-box is four bits, each S-box table as; stored in ROM contains two S-boxes; each data byte is split between the two; with the most significant four bits of the byte storing the output of the; lower numbered sbox. For example, the most significant four bits of each; byte of sbox12 are used to store S-box 1, and the least significant four; bits are used to store S-box 2.; S boxes one and two dt 0xef, 0x41, 0xd8, 0x1e, 0x26, 0xfb, 0xb3, 0x84 dt 0x39, 0xa7, 0x62, 0xcd, 0x5c, 0x90, 0x05, 0x7a dt 0x03, 0xfd, 0x74, 0x47, 0xef, 0x22, 0xd8, 0x1e dt 0xac, 0x60, 0xc1, 0xba, 0x96, 0x59, 0x3b, 0x85 dt 0x40, 0x1e, 0xe7, 0x8b, 0xda, 0x64, 0x2d, 0xb1 dt 0xf5, 0xc8, 0x9c, 0x76, 0x39, 0xa3, 0x52, 0x0f dt 0xfd, 0xc8, 0x8a, 0x21, 0x43, 0x9f, 0x14, 0x72 dt 0x5b, 0xb6, 0x37, 0xec, 0xa0, 0x05, 0x6e, 0xd9; S boxes three and four dt 0xa7, 0x0d, 0x9e, 0xe3, 0x60, 0x36, 0xf9, 0x5a dt 0x11, 0xd2, 0xc8, 0x75, 0xbb, 0x4c, 0x24, 0x8f dt 0xdd, 0x78, 0x0b, 0x95, 0x36, 0x4f, 0x60, 0xa3 dt 0x24, 0x87, 0x52, 0xec, 0xc1, 0xba, 0xfe, 0x19 dt 0xda, 0x66, 0x49, 0x90, 0x8c, 0xfb, 0x37, 0x0d dt 0xbf, 0x11, 0x23, 0xce, 0x55, 0xa2, 0xe8, 0x74 dt 0x13, 0xaf, 0xd0, 0x06, 0x6a, 0x91, 0x8d, 0x78 dt 0x49, 0xf4, 0xe5, 0x3b, 0xbc, 0x57, 0x22, 0xce; S boxes five and six dt 0x2c, 0xc1, 0x4a, 0x1f, 0x79, 0xa2, 0xb6, 0x68 dt 0x80, 0x5d, 0x33, 0xf4, 0xde, 0x07, 0xe5, 0x9b dt 0xea, 0xbf, 0x24, 0xc2, 0x47, 0x7c, 0xd9, 0x15 dt 0x56, 0x01, 0xfd, 0xae, 0x30, 0x9b, 0x83, 0x68 dt 0x49, 0x2e, 0x1f, 0xb5, 0xa2, 0xd8, 0x7c, 0x83 dt 0xf7, 0x90, 0xc4, 0x5a, 0x61, 0x3d, 0x0b, 0xe6 dt 0xb4, 0x83, 0xc2, 0x7c, 0x19, 0xe5, 0x2f, 0xda dt 0x6b, 0xfe, 0x01, 0x97, 0xa6, 0x40, 0x58, 0x3d; S boxes seven and eight dt 0x4d, 0xb2, 0x28, 0xe4, 0xf6, 0x0f, 0x8b, 0xd1 dt 0x3a, 0xc9, 0x93, 0x7e, 0x55, 0xa0, 0x6c, 0x17 dt 0xd1, 0x0f, 0xbd, 0x78, 0x4a, 0x93, 0x17, 0xa4 dt 0xec, 0x35, 0x56, 0xcb, 0x20, 0xfe, 0x89, 0x62 dt 0x17, 0x4b, 0xb4, 0xd1, 0xc9, 0x3c, 0x7e, 0xe2 dt 0xa0, 0xf6, 0x6a, 0x8d, 0x0f, 0x53, 0x95, 0x28 dt 0x62, 0xb1, 0xde, 0x87, 0x14, 0x4a, 0xa8, 0x7d dt 0x9f, 0x5c, 0x09, 0xf0, 0xe3, 0x25, 0x36, 0xcb
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -