Основы криптографии
Дипломная работа - Компьютеры, программирование
Другие дипломы по предмету Компьютеры, программирование
i] ;set bits for this nybbleedx,4 ; (E function)edi,4*64 ;sizeof(long)*sizeof(table)
bx,dx ;nybble 8bl,[esi] ;XOR with key bitsbx,11111100besieax, [ebx+edi] ;set bits for this nybble
eax,ebp ;left half^= f(right half,keytbl[n]);esi,16 ;point to next keytableebp,ecx ;old right half becomes new left half[loopcount]dxlp1
esi, offset p2edx,eaxeax,ebpl_permute
mov ebx,ebp
//mov dword ptr[block],ebpebp
mov [block],ebx
mov dword ptr[block+4],edi
;;pop ebpediesiddone
;----------------------------------------------------------------------_block:esiediebp
esi, offset p1edx,dword ptr[block] ;get input for l_permuteeax,dword ptr[block+4]l_permute
[loopcount],16 ;yes, this is the full 16 round DES
//ifdef TRIPLE_DES
// mov esi,[de_keyptr]
//elseesi,[keyptr]
//endifesi,15*8+7eax, edi ;ip.L=op.R
//align 4:and ebx,0ffffhedx,eax ;f() input is ip.Lecx,edx ;op.R=ip.Ledxeax,eax ;output gets zeroedi,offset sp_table + 7*4*64 ;point to s&p boxesedx,3 ;set up bits 32,1,2,3,4,5 shl 2
;THE F() FUNCTION (decrypt)bx,dx ;nybble 1bl,[esi] ;XOR with key bitsbx,11111100besieax, [ebx+edi] ;set bits for this nybbleedx,4 ;(see E Bit-Selection Table)edi,4*64 ;sizeof(long)*sizeof(table)
bx,dx ;nybble 2bl,[esi] ;XOR with key bitsbx,11111100besieax, [ebx+edi] ;set bits for this nybbleedx,4 ;(see E Bit-Selection Table)edi,4*64 ;sizeof(long)*sizeof(table)
bx,dx ;nybble 3bl,[esi] ;XOR with key bitsbx,11111100besieax, [ebx+edi] ;set bits for this nybbleedx,4 ;(see E Bit-Selection Table)edi,4*64 ;sizeof(long)*sizeof(table)
bx,dx ;nybble 4bl,[esi] ;XOR with key bitsbx,11111100besieax, [ebx+edi] ;set bits for this nybbleedx,4 ;(see E Bit-Selection Table)edi,4*64 ;sizeof(long)*sizeof(table)
bx,dx ;nybble 5bl,[esi] ;XOR with key bitsbx,11111100bsieax, [ebx+edi] ;set bits for this nybbleedx,4 ;(see E Bit-Selection Table)edi,4*64 ;sizeof(long)*sizeof(table)
bx,dx ;nybble 6bl,[esi] ;XOR with key bitsbx,11111100besieax, [ebx+edi] ;set bits for this nybbleedx,4 ;(see E Bit-Selection Table)edi,4*64 ;sizeof(long)*sizeof(table)
bx,dx ;nybble 7bl,[esi] ;XOR with key bitsbx,11111100besieax, [ebx+edi] ;set bits for this nybbleedx,4 ;(see E Bit-Selection Table)di,4*64 ;sizeof(long)*sizeof(table)
bx,dx ;nybble 8bl,[esi] ;XOR with key bitsbx,11111100besieax, [ebx+edi] ;set bits for this nybble
eax,ebp ;ip.R ^ f(ip.L,keytbl[n]);ebp,ecx ;ip.R=op.R;[loopcount]dxlp2
esi,offset p2 ;set up for inverse permutationedx,eax ;get inputeax,ebpl_permute
;;mov dword ptr[block],ebp
;;mov dword ptr[block+4],edi
mov ebx,ebpebp
mov [block],ebx
mov [block+4],edi
ediesiddone
_permute:
; do high dword
movzx ebx,dx ;nybble 1bx,3bx,1111000bedx,1ebp,[esi+ebx] ;output highedi,[esi+4+ebx] ;output lowesi,8*16
;@@2:bx,dx ;nybble 2bx,1111000bedx,4ebp,[esi+ebx]edi,[esi+4+ebx]esi,8*16
bx,dx ;nybble 3bx,1111000bedx,4ebp,[esi+ebx]edi,[esi+4+ebx]esi,8*16
bx,dx ;nybble 4bx,1111000bedx,4ebp,[esi+ebx]edi,[esi+4+ebx]esi,8*16
bx,dx ;nybble 5bx,1111000bedx,4ebp,[esi+ebx]edi,[esi+4+ebx]esi,8*16
bx,dx ;nybble 6bx,1111000bedx,4ebp,[esi+ebx]edi,[esi+4+ebx]esi,8*16
bx,dx ;nybble 7bx,1111000bedx,4ebp,[esi+ebx]edi,[esi+4+ebx]esi,8*16
bx,dx ;nybble 8bx,1111000bebp,[esi+ebx]edi,[esi+4+ebx]esi,8*16
; do low dwordbx,ax ;nybble 1bx,3 ;index * 8bx,1111000beax,1ebp,[esi+ebx]edi,[esi+4+ebx]esi,8*16
;@@3:bx, ax ;nybble 2bx,1111000beax,4 ;next nybbleebp,[esi+ebx]edi,[esi+4+ebx]esi,8*16
bx, ax ;nybble 3bx,1111000beax,4 ;next nybbleebp,[esi+ebx]edi,[esi+4+ebx]esi,8*16
bx, ax ;nybble 4bx,1111000beax,4 ;next nybbleebp,[esi+ebx]edi,[esi+4+ebx]esi,8*16
bx, ax ;nybble 5bx,1111000beax,4 ;next nybbleebp,[esi+ebx]edi,[esi+4+ebx]esi,8*16
bx, ax ;nybble 6bx,1111000beax,4 ;next nybbleebp,[esi+ebx]edi,[esi+4+ebx]esi,8*16
bx, ax ;nybble 7bx,1111000beax,4 ;next nybbleebp,[esi+ebx]edi,[esi+4+ebx]esi,8*16
bx, ax ;nybble 8bx,1111000bebp,[esi+ebx]edi,[esi+4+ebx]
:
}
bl[0]=block[0];
bl[1]=block[1];
}
permute(long *op,long *ip, char *tbl,int n)
{
_asm
{
;permute proc near
; arg op:dataptr, ip:dataptr, tbl:dataptr, n:word
; push bp
; mov bp,sp
; push si
; push di
ebx,[op]esi,[tbl] ;permutation table to siedi,offset Pmask ;setting-bits in didword ptr[ebx],0dword ptr[ebx+4],0 ;output=0ebx,[ip]eax,[ebx]edx,[ebx+4] ;input to edx:eaxecx,[n] ;count to cx
: mov ebx,[esi] ;get high dword perm biteax,ebx ;is this bit set?pmis_hitebx,[esi+4] ;get low dword perm bitedx,ebx ;any action?pmno_hit
_hit:esiesi,[op] ;point to outputebx,[edi] ;get setting-bit[esi],ebx ;set the bitebx,[edi+4] ;do for both halves[esi+4],ebx ;set bitesi
_hit:esi,8 ;next permutation entryedi,8 ;next setting-bits entrypm1
; pop di
; pop si
; mov sp,bp
; pop bp
}
}
//;----------------------------------------------------------------------
//; Rotate 28 bits (for KS function)
//; void rotate(long *input,int count);
rotate(long *input,int count)
//rotate proc near
// arg input:dataptr,count:word
{
_asm
{
mov ecx,[count]
mov ebx, [input]
mov eax,[ebx]
bswap eax ;get bits in good order
xor ebx,ebx
shld ebx, eax,cl ;shift high bits to bl
shl eax,cl
shl ebx,4 ;move from bit 32 to bit 28
add eax,ebx
mov ebx,[input]
bswap eax ;reverse bytes...
mov [ebx],eax ;...so they store correctly
}
}
set_table(char*input,int num,long*tbl)
{
_asm
{
;----------------------------------------------------------------------
; void set_table(char*input,int n,long*keytbl);
; Take a 48-bit input, and make 8 bytes of 6 bits each.
; Output goes into tbl[].
;align 16
;proc set_table near
; arg input:dataptr,num:word, tbl:dataptr
;push bp
;mov bp,sp
;push di
edi, [tbl]eax,[num]bl,8bledi,eax ;now di points to proper offset in keytblebx,[input]dx,word ptr[ebx+4]edx ;edx highbits now has highbits of key sched.ebx,[ebx]ebx ;ebx gets 48 bits of key sched.cx,8 ;loop count
: shld eax,ebx,6 ;get next 6 bitsebx,edx,6edx,6ax,2 ;pre-shift for sizeof(long) in f() functional,11111100b[edi],aledistble1
;pop di
;mov sp,bp
;pop bp
}
}
des_key_sched(char * password, unsigned char *keytble)
{
static char *input;
static unsigned char *tbl;
static unsigned long block1[2];
static unsigned long temp [2];
static loopcount;
static unsigned short r_sched[]=
{
1,1,2,2,2,2,2,2,1,2,2,2,2,2,2,1
};
input=password;
tbl=keytble;
_asm
{
;----------------------------------------------------------------------
; void schedule(char *password long keytbl[]);
; Expand a password into 16 rounds of Key Schedule.
; Output goes into extern keytbl[16][8]
; (This is not a time-critical function, and I simply
; rewrote the c source into assembler, to make
; DES programming a 2-step process-- schedule the key,
; encrypt/decrypt the block.)
;align 16
;proc schedule
; arg input:dataptr, tbl:dataptr
ebp
;mov bp,spedi
64offset PC1tblebx,[input]ebxoffset temppermute ;select 56 bitssp,8*2
ecx,16[loopcount],ecxedi,edi
:ebx,ediebx,1ebx,offset r_schedeax,[word ptr ebx]eax ;save for 2nd rotate
push eax ;arg for rotate func.offset temprotateesp,4*2
offset temp+4rotateesp,4*2
48offset PC2tbl ;perm tableoffset temp ;inputoffset block1 ;outputpermuteesp,8*2
eax,[tbl]eaxedi ;table numberoffset block1 ;inputset_tableesp,6*2
edi[loopcount]sch1
ebx,[input] ;erase the password spacecx,4ax,ax: mov [ebx],axebxebxsch2
ediebpscdone
;;set_table:
:
}
}
// Scramdisk specific code.....
//key schedules
// Three key triple des...
desencipher(unsigned long *b, int slot)
{
des_encrypt(b,sdisk_des_keys[slot].e,DES_ENCRYPT);
}
threedesencipher(unsigned long *b, int slot)
{
des_encrypt