From: hackbard Date: Sun, 5 Oct 2003 11:32:30 +0000 (+0000) Subject: - X-Git-Url: https://hackdaworld.org/gitweb/?a=commitdiff_plain;h=f0de6e2ec2cef7013695bf9b5222c55413ff1f98;p=my-code%2Fcrypto.git - --- diff --git a/des.c b/des.c index 722e1cc..2101a95 100644 --- a/des.c +++ b/des.c @@ -5,6 +5,7 @@ * */ +#include #include #include "des.h" @@ -87,18 +88,32 @@ u8 data_final_perm_table[DES_DATA_BLOCK_LEN]={ 34,2,42,10,50,18,58,26,33,1,41,9,49,17,57,25 }; -int rotate_right(u8 *obj,int len) { - u8 tmp=obj[len-1]; - while(--len) obj[len]=obj[len]>>1|((obj[len-1]&1)<<7); - obj[0]=obj[0]>>1|((tmp&1)<<7);; +int rotate_right(u8 *obj) { + u8 mid=obj[3],tmp=obj[6]; + int i; + for(i=6;i>3;i++) + obj[i]=obj[i]>>1|((obj[i-1]&1)<<7); + obj[3]=(obj[3]&0xf)>>1|((tmp&1)<<3); + obj[3]|=(((obj[2]&1)<<7)|((mid&0xe0)>>1)); + for(i=2;i>0;i++) + obj[i]=obj[i]>>1|((obj[i-1]&1)<<7); + obj[0]=obj[0]>>1|((mid&0x10)<<3); return 1; } -int rotate_left(u8 *obj,int len) { - int i=0; - u8 tmp=obj[0]; - for(i=0;i>7); - obj[len-1]=obj[len-1]<<1|(tmp>>7); +int rotate_left(u8 *obj) { + u8 mid=obj[3],tmp=obj[0]; + int i; + for(i=0;i<3;i++) + obj[i]=obj[i]<<1|(obj[i+1]>>7); + obj[3]=((obj[3]&0xf0)<<1)|((tmp&0x80)>>3); + obj[3]|=(((mid&7)<<1)|(obj[4]>>7)); + for(i=4;i<6;i++) + obj[i]=(obj[i]<<1)|(obj[i+1]>>7); + obj[6]=obj[6]<<1|((mid&8)>>3); + printf("debug: "); + for(i=0;i<7;i++) printf("%02x ",obj[i]); + printf("\n"); return 1; } @@ -106,16 +121,14 @@ int table_trans(u8 *src,u8 *final,u8 *table,int len) { int i; memset(final,0,len/8); for(i=0;i>((8-table[i]%8)&7))&1)<<(7-i%8)); - return 1; + final[i/8]|=((((src[(table[i]-1)/8])>>((8-(table[i])%8)&7))&1)<<(7-i%8)); + return 1; } int subkey_trans(u8 *key,int round) { int i; - for(i=0;i>(8-len):((data[offset/8]&((1<<(offset%8))-1))<<(offset%8))|(data[(offset/8)+1]>>(len-(8-(offset%8)))); - return(sbox[box][((tmp&1)|(tmp&(1<<(len-1))<<1))*16+(tmp&((1<<(len-1))-1)>>1)]); -} - int data_s_and_p(u8 *data) { u8 s[DES_DATA_BLOCK_LEN/(8*2)]; - int i; - for(i=0;i>6)|((data[0]&4)>>2))+((data[0]&0x78)>>3)]<<4; + s[0]|=sbox[1][16*((data[0]&2)|((data[1]&0x10)>>4))+(((data[0]&1)<<3)|(data[1]&0xe0)>>5)]; + s[1]=sbox[2][16*(((data[1]&8)>>2)|((data[2]&0x40)>>6))+(((data[1]&7)<<1)|((data[2]&0x80)>>7))]<<4; + s[1]|=sbox[3][16*(((data[2]&0x20)>>4)|(data[2]&1))+((data[2]&0x1e)>>1)]; + s[2]=sbox[4][16*(((data[3]&0x80)>>6)|((data[3]&4)>>2))+((data[3]&0x78)>>3)]<<4; + s[2]|=sbox[5][16*((data[3]&2)|((data[4]&0x10)>>4))+(((data[3]&1)<<3)|(data[4]&0xe0)>>5)]; + s[3]=sbox[6][16*(((data[4]&8)>>2)|((data[5]&0x40)>>6))+(((data[4]&7)<<1)|((data[5]&0x80)>>7))]<<4; + s[3]|=sbox[7][16*(((data[5]&0x20)>>4)|(data[5]&1))+((data[5]&0x1e)>>1)]; table_trans(s,data,pbox,DES_PBOX_LEN); return 1; } @@ -193,6 +208,12 @@ int des_crypt_decrypt(u8 *src,u8 *final,u8 *key,int len,u8 mode) { - loop 16 times */ compute_subkeys(key,subkey); + printf("debug:\n"); + for(i=0;i<16;i++) { + printf("subkey %d: ",i); + for(j=0;j<6;j++) printf("%02x ",subkey[i][j]); + printf("\n"); + } /* split data to 64bit blocks, and crypt/decrypt each block depending on mode (CBC/ECB): - initial data transformation diff --git a/test.c b/test.c index 83e0694..35b75a8 100644 --- a/test.c +++ b/test.c @@ -15,8 +15,8 @@ int main() { - u8 plain[64]; - u8 crypted[64]; + u8 plain[32]; + u8 crypted[32]; u8 key[8]; int fd,i; @@ -26,34 +26,38 @@ int main() { } printf("des crypt/decrypt test:\n"); - printf("- geberating random key ...\n"); - read(fd,key,8); + printf("- generating simple/random key ...\n"); + // read(fd,key,8); + memset(key,0x90,8); printf("key: "); for(i=0;i<8;i++) printf("%02x ",key[i]); puts(""); - memset(plain,0,64); - strcpy(plain,"allyouratmels ... :)"); + memset(plain,0,32); + // strcpy(plain,"allyouratmels ... :)"); + printf("encrypting '"); + for(i=0;i<32;i++) printf("%02x%c",plain[i],i==31?'\n':' '); - printf("encrypting '%s' (ecb mode) ...\n",plain); - memset(crypted,0,64); - des_encrypt(plain,crypted,key,64,MODE_ECB); + printf("encrypting (ecb mode) ...\n"); + memset(crypted,0,32); + des_encrypt(plain,crypted,key,32,MODE_ECB); + printf("plain: "); + for(i=0;i<32;i++) printf("%02x ",plain[i]); + puts(""); printf("crypted: "); - for(i=0;i<64;i++) printf("%c",crypted[i]); + for(i=0;i<32;i++) printf("%02x ",crypted[i]); puts(""); - printf("encrypting '%s' (cbc mode) ...\n",plain); - memset(crypted,0,64); - des_encrypt(plain,crypted,key,64,MODE_CBC); + printf("decrypting (ecb mode) ...\n"); + memset(plain,0,32); + des_decrypt(crypted,plain,key,32,MODE_ECB); printf("crypted: "); - for(i=0;i<64;i++) printf("%c",crypted[i]); + for(i=0;i<32;i++) printf("%02x ",crypted[i]); puts(""); - - printf("--\n"); - printf("verify:\n"); - des_decrypt(crypted,plain,key,64,MODE_CBC); - printf("plain text: "); - for(i=0;i<64;i++) printf("%c",plain[i]); + printf("plain: "); + for(i=0;i<32;i++) printf("%02x ",plain[i]); puts(""); + + close(fd); puts("done :)");